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.
5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/stl_util.h"
18 #include "crypto/rsa_private_key.h"
19 #include "net/cert/x509_util_nss.h"
31 class OwnerKeyUtilTest;
33 class PublicKey : public base::RefCountedThreadSafe<PublicKey> {
37 std::vector<uint8>& data() { return data_; }
39 bool is_loaded() const { return !data_.empty(); }
41 std::string as_string() {
42 return std::string(reinterpret_cast<const char*>(vector_as_array(&data_)),
47 friend class base::RefCountedThreadSafe<PublicKey>;
51 std::vector<uint8> data_;
53 DISALLOW_COPY_AND_ASSIGN(PublicKey);
56 class PrivateKey : public base::RefCountedThreadSafe<PrivateKey> {
58 explicit PrivateKey(crypto::RSAPrivateKey* key);
60 crypto::RSAPrivateKey* key() { return key_.get(); }
63 friend class base::RefCountedThreadSafe<PrivateKey>;
65 virtual ~PrivateKey();
67 scoped_ptr<crypto::RSAPrivateKey> key_;
69 DISALLOW_COPY_AND_ASSIGN(PrivateKey);
72 class OwnerKeyUtil : public base::RefCountedThreadSafe<OwnerKeyUtil> {
74 // Creates an OwnerKeyUtil instance.
75 static OwnerKeyUtil* Create();
77 // Attempts to read the public key from the file system.
78 // Upon success, returns true and populates |output|. False on failure.
79 virtual bool ImportPublicKey(std::vector<uint8>* output) = 0;
81 // Looks for the private key associated with |key| in the default slot,
82 // and returns it if it can be found. Returns NULL otherwise.
83 // Caller takes ownership.
85 // TODO (ygorshenin@): this function is deprecated and should be
86 // removed, see crbug.com/372316.
87 virtual crypto::RSAPrivateKey* FindPrivateKey(
88 const std::vector<uint8>& key) = 0;
90 // Looks for the private key associated with |key| in the |slot|
91 // and returns it if it can be found. Returns NULL otherwise.
92 // Caller takes ownership.
93 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
94 const std::vector<uint8>& key,
95 PK11SlotInfo* slot) = 0;
97 // Checks whether the public key is present in the file system.
98 virtual bool IsPublicKeyPresent() = 0;
102 virtual ~OwnerKeyUtil();
105 friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
107 FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey);
110 // Implementation of OwnerKeyUtil that is used in production code.
111 class OwnerKeyUtilImpl : public OwnerKeyUtil {
113 explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file);
116 virtual bool ImportPublicKey(std::vector<uint8>* output) OVERRIDE;
117 virtual crypto::RSAPrivateKey* FindPrivateKey(
118 const std::vector<uint8>& key) OVERRIDE;
119 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
120 const std::vector<uint8>& key,
121 PK11SlotInfo* slot) OVERRIDE;
122 virtual bool IsPublicKeyPresent() OVERRIDE;
125 virtual ~OwnerKeyUtilImpl();
128 // The file that holds the public key.
129 base::FilePath key_file_;
131 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl);
134 } // namespace chromeos
136 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_