1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_ 6 #define CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "base/callback_forward.h" 14 #include "base/component_export.h" 15 #include "base/macros.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/optional.h" 18 19 namespace chromeos { 20 21 // This class is used to get the system salt from cryptohome and cache it. COMPONENT_EXPORT(CHROMEOS_CRYPTOHOME)22class COMPONENT_EXPORT(CHROMEOS_CRYPTOHOME) SystemSaltGetter { 23 public: 24 using GetSystemSaltCallback = 25 base::OnceCallback<void(const std::string& system_salt)>; 26 using RawSalt = std::vector<uint8_t>; 27 28 // Manage an explicitly initialized global instance. 29 static void Initialize(); 30 static bool IsInitialized(); 31 static void Shutdown(); 32 static SystemSaltGetter* Get(); 33 34 // Converts |salt| to a hex encoded string. 35 static std::string ConvertRawSaltToHexString(const RawSalt& salt); 36 37 // Returns system hash in hex encoded ascii format. Note: this may return 38 // an empty string (e.g. errors in D-Bus layer) 39 void GetSystemSalt(GetSystemSaltCallback callback); 40 41 // Adds another callback to be called when system salt is received. 42 // (If system salt is available, closure will be called immediately). 43 void AddOnSystemSaltReady(base::OnceClosure closure); 44 45 // Returns pointer to binary system salt if it is already known. 46 // Returns nullptr if system salt is not known. 47 // WARNING: This pointer is null early in startup. Do not assume it is valid. 48 // Prefer GetSystemSalt() above. https://crbug.com/1122674 49 const RawSalt* GetRawSalt() const; 50 51 // This is for browser tests API. 52 void SetRawSaltForTesting(const RawSalt& raw_salt); 53 54 protected: 55 SystemSaltGetter(); 56 ~SystemSaltGetter(); 57 58 private: 59 // Used to implement GetSystemSalt(). 60 void DidWaitForServiceToBeAvailable(GetSystemSaltCallback callback, 61 bool service_is_available); 62 void DidGetSystemSalt(GetSystemSaltCallback callback, 63 base::Optional<std::vector<uint8_t>> system_salt); 64 65 RawSalt raw_salt_; 66 std::string system_salt_; 67 68 // List of callbacks waiting for system salt ready event. 69 std::vector<base::OnceClosure> on_system_salt_ready_; 70 71 base::WeakPtrFactory<SystemSaltGetter> weak_ptr_factory_{this}; 72 73 DISALLOW_COPY_AND_ASSIGN(SystemSaltGetter); 74 }; 75 76 } // namespace chromeos 77 78 #endif // CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_ 79