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 |slot|
82 // and returns it if it can be found. Returns NULL otherwise.
83 // Caller takes ownership.
84 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
85 const std::vector<uint8>& key,
86 PK11SlotInfo* slot) = 0;
88 // Checks whether the public key is present in the file system.
89 virtual bool IsPublicKeyPresent() = 0;
93 virtual ~OwnerKeyUtil();
96 friend class base::RefCountedThreadSafe<OwnerKeyUtil>;
98 FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey);
101 // Implementation of OwnerKeyUtil that is used in production code.
102 class OwnerKeyUtilImpl : public OwnerKeyUtil {
104 explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file);
107 virtual bool ImportPublicKey(std::vector<uint8>* output) OVERRIDE;
108 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot(
109 const std::vector<uint8>& key,
110 PK11SlotInfo* slot) OVERRIDE;
111 virtual bool IsPublicKeyPresent() OVERRIDE;
114 virtual ~OwnerKeyUtilImpl();
117 // The file that holds the public key.
118 base::FilePath key_file_;
120 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl);
123 } // namespace chromeos
125 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_