key_storage_linux.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016 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. #ifndef COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_
  5. #define COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "components/os_crypt/key_storage_util_linux.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace base {
  12. class SequencedTaskRunner;
  13. class WaitableEvent;
  14. } // namespace base
  15. namespace os_crypt {
  16. struct Config;
  17. }
  18. // An API for retrieving OSCrypt's password from the system's password storage
  19. // service.
  20. class COMPONENT_EXPORT(OS_CRYPT) KeyStorageLinux {
  21. public:
  22. KeyStorageLinux() = default;
  23. KeyStorageLinux(const KeyStorageLinux&) = delete;
  24. KeyStorageLinux& operator=(const KeyStorageLinux&) = delete;
  25. virtual ~KeyStorageLinux() = default;
  26. // Tries to load the appropriate key storage. Returns null if none succeed.
  27. static COMPONENT_EXPORT(OS_CRYPT)
  28. std::unique_ptr<KeyStorageLinux> CreateService(
  29. const os_crypt::Config& config);
  30. // Gets the encryption key from the OS password-managing library. If a key is
  31. // not found, a new key will be generated, stored and returned.
  32. absl::optional<std::string> GetKey();
  33. protected:
  34. // Get the backend's favourite task runner, or nullptr for no preference.
  35. virtual base::SequencedTaskRunner* GetTaskRunner();
  36. // Loads the key storage. Returns false if the service is not available.
  37. // This iwill be called on the backend's preferred thread.
  38. virtual bool Init() = 0;
  39. // The implementation of GetKey() for a specific backend. This will be called
  40. // on the backend's preferred thread.
  41. virtual absl::optional<std::string> GetKeyImpl() = 0;
  42. // The name of the group, if any, containing the key.
  43. static const char kFolderName[];
  44. // The name of the entry with the encryption key.
  45. static const char kKey[];
  46. private:
  47. #if defined(USE_LIBSECRET) || defined(USE_KEYRING) || defined(USE_KWALLET)
  48. // Tries to load the appropriate key storage. Returns null if none succeed.
  49. static std::unique_ptr<KeyStorageLinux> CreateServiceInternal(
  50. os_crypt::SelectedLinuxBackend selected_backend,
  51. const os_crypt::Config& config);
  52. #endif // defined(USE_LIBSECRET) || defined(USE_KEYRING) ||
  53. // defined(USE_KWALLET)
  54. // Performs Init() on the backend's preferred thread.
  55. bool WaitForInitOnTaskRunner();
  56. // Perform the blocking calls to the backend to get the Key. Store it in
  57. // |password| and signal completion on |on_password_received|.
  58. void BlockOnGetKeyImplThenSignal(base::WaitableEvent* on_password_received,
  59. absl::optional<std::string>* password);
  60. // Perform the blocking calls to the backend to initialise. Store the
  61. // initialisation result in |success| and signal completion on |on_inited|.
  62. void BlockOnInitThenSignal(base::WaitableEvent* on_inited, bool* success);
  63. };
  64. #endif // COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_