cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_ |
| 6 | #define COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Matt Menke | ef8aab1 | 2018-07-31 14:42:17 | [diff] [blame] | 11 | #include "base/component_export.h" |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 12 | #include "base/macros.h" |
cfroussios | b013c15b | 2016-09-03 01:10:16 | [diff] [blame] | 13 | |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame] | 14 | namespace base { |
| 15 | class SequencedTaskRunner; |
Christos Froussios | 57fe574 | 2017-12-07 21:16:49 | [diff] [blame] | 16 | class WaitableEvent; |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame] | 17 | } |
| 18 | |
Christos Froussios | 494196d | 2017-07-14 10:10:04 | [diff] [blame] | 19 | namespace os_crypt { |
| 20 | struct Config; |
| 21 | } |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 22 | |
| 23 | // An API for retrieving OSCrypt's password from the system's password storage |
| 24 | // service. |
Matt Menke | ef8aab1 | 2018-07-31 14:42:17 | [diff] [blame] | 25 | class COMPONENT_EXPORT(OS_CRYPT) KeyStorageLinux { |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 26 | public: |
| 27 | KeyStorageLinux() = default; |
| 28 | virtual ~KeyStorageLinux() = default; |
| 29 | |
cfroussios | 3ea4c69 | 2016-07-18 19:15:14 | [diff] [blame] | 30 | // Tries to load the appropriate key storage. Returns null if none succeed. |
Matt Menke | ef8aab1 | 2018-07-31 14:42:17 | [diff] [blame] | 31 | static COMPONENT_EXPORT(OS_CRYPT) |
| 32 | std::unique_ptr<KeyStorageLinux> CreateService( |
| 33 | const os_crypt::Config& config); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 34 | |
| 35 | // Gets the encryption key from the OS password-managing library. If a key is |
| 36 | // not found, a new key will be generated, stored and returned. |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 37 | std::string GetKey(); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 38 | |
| 39 | protected: |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame] | 40 | // Get the backend's favourite task runner, or nullptr for no preference. |
| 41 | virtual base::SequencedTaskRunner* GetTaskRunner(); |
| 42 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 43 | // Loads the key storage. Returns false if the service is not available. |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame] | 44 | // This iwill be called on the backend's preferred thread. |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 45 | virtual bool Init() = 0; |
| 46 | |
Christos Froussios | 985d1aac | 2017-11-09 11:01:07 | [diff] [blame] | 47 | // The implementation of GetKey() for a specific backend. This will be called |
| 48 | // on the backend's preferred thread. |
| 49 | virtual std::string GetKeyImpl() = 0; |
| 50 | |
cfroussios | 2e6729a4 | 2016-07-26 09:18:12 | [diff] [blame] | 51 | // The name of the group, if any, containing the key. |
| 52 | static const char kFolderName[]; |
| 53 | // The name of the entry with the encryption key. |
| 54 | static const char kKey[]; |
| 55 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 56 | private: |
Christos Froussios | 4e170cb | 2017-12-01 09:42:33 | [diff] [blame] | 57 | // Performs Init() on the backend's preferred thread. |
| 58 | bool WaitForInitOnTaskRunner(); |
| 59 | |
Christos Froussios | 57fe574 | 2017-12-07 21:16:49 | [diff] [blame] | 60 | // Perform the blocking calls to the backend to get the Key. Store it in |
| 61 | // |password| and signal completion on |on_password_received|. |
| 62 | void BlockOnGetKeyImplThenSignal(base::WaitableEvent* on_password_received, |
| 63 | std::string* password); |
| 64 | |
| 65 | // Perform the blocking calls to the backend to initialise. Store the |
| 66 | // initialisation result in |success| and signal completion on |on_inited|. |
| 67 | void BlockOnInitThenSignal(base::WaitableEvent* on_inited, bool* success); |
| 68 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 69 | DISALLOW_COPY_AND_ASSIGN(KeyStorageLinux); |
| 70 | }; |
| 71 | |
| 72 | #endif // COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_ |