blob: e4acd264d84e5452174a4e4e3eef06658a5782fb [file] [log] [blame]
cfroussios3b5a4e42016-05-31 11:02:181// 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 Menkeef8aab12018-07-31 14:42:1711#include "base/component_export.h"
cfroussios3b5a4e42016-05-31 11:02:1812#include "base/macros.h"
cfroussiosb013c15b2016-09-03 01:10:1613
Christos Froussios4e170cb2017-12-01 09:42:3314namespace base {
15class SequencedTaskRunner;
Christos Froussios57fe5742017-12-07 21:16:4916class WaitableEvent;
Christos Froussios4e170cb2017-12-01 09:42:3317}
18
Christos Froussios494196d2017-07-14 10:10:0419namespace os_crypt {
20struct Config;
21}
cfroussios3b5a4e42016-05-31 11:02:1822
23// An API for retrieving OSCrypt's password from the system's password storage
24// service.
Matt Menkeef8aab12018-07-31 14:42:1725class COMPONENT_EXPORT(OS_CRYPT) KeyStorageLinux {
cfroussios3b5a4e42016-05-31 11:02:1826 public:
27 KeyStorageLinux() = default;
28 virtual ~KeyStorageLinux() = default;
29
cfroussios3ea4c692016-07-18 19:15:1430 // Tries to load the appropriate key storage. Returns null if none succeed.
Matt Menkeef8aab12018-07-31 14:42:1731 static COMPONENT_EXPORT(OS_CRYPT)
32 std::unique_ptr<KeyStorageLinux> CreateService(
33 const os_crypt::Config& config);
cfroussios3b5a4e42016-05-31 11:02:1834
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 Froussios985d1aac2017-11-09 11:01:0737 std::string GetKey();
cfroussios3b5a4e42016-05-31 11:02:1838
39 protected:
Christos Froussios4e170cb2017-12-01 09:42:3340 // Get the backend's favourite task runner, or nullptr for no preference.
41 virtual base::SequencedTaskRunner* GetTaskRunner();
42
cfroussios3b5a4e42016-05-31 11:02:1843 // Loads the key storage. Returns false if the service is not available.
Christos Froussios4e170cb2017-12-01 09:42:3344 // This iwill be called on the backend's preferred thread.
cfroussios3b5a4e42016-05-31 11:02:1845 virtual bool Init() = 0;
46
Christos Froussios985d1aac2017-11-09 11:01:0747 // 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
cfroussios2e6729a42016-07-26 09:18:1251 // 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
cfroussios3b5a4e42016-05-31 11:02:1856 private:
Christos Froussios4e170cb2017-12-01 09:42:3357 // Performs Init() on the backend's preferred thread.
58 bool WaitForInitOnTaskRunner();
59
Christos Froussios57fe5742017-12-07 21:16:4960 // 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
cfroussios3b5a4e42016-05-31 11:02:1869 DISALLOW_COPY_AND_ASSIGN(KeyStorageLinux);
70};
71
72#endif // COMPONENTS_OS_CRYPT_KEY_STORAGE_LINUX_H_