Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_ | 5 #ifndef COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_ | 6 #define COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.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" | 13 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 18 #include "crypto/rsa_private_key.h" | 16 #include "components/ownership/ownership_export.h" |
| 19 #include "net/cert/x509_util_nss.h" | 17 #include "net/cert/x509_util_nss.h" |
|
wtc
2014/08/25 19:41:51
This file doesn't contain "net::", so I believe th
ygorshenin1
2014/08/26 14:53:35
x509_util_nss.h contains very useful typedef of PK
| |
| 20 | 18 |
| 21 namespace base { | |
| 22 class FilePath; | |
| 23 } | |
| 24 | |
| 25 namespace crypto { | 19 namespace crypto { |
| 26 class RSAPrivateKey; | 20 class RSAPrivateKey; |
| 27 } | 21 } |
| 28 | 22 |
| 29 namespace chromeos { | 23 namespace ownership { |
| 30 | 24 |
| 31 class OwnerKeyUtilTest; | 25 class OwnerKeyUtilTest; |
| 32 | 26 |
| 33 class PublicKey : public base::RefCountedThreadSafe<PublicKey> { | 27 class OWNERSHIP_EXPORT PublicKey |
| 28 : public base::RefCountedThreadSafe<PublicKey> { | |
| 34 public: | 29 public: |
| 35 PublicKey(); | 30 PublicKey(); |
| 36 | 31 |
| 37 std::vector<uint8>& data() { return data_; } | 32 std::vector<uint8>& data() { return data_; } |
| 38 | 33 |
| 39 bool is_loaded() const { return !data_.empty(); } | 34 bool is_loaded() const { return !data_.empty(); } |
| 40 | 35 |
| 41 std::string as_string() { | 36 std::string as_string() { |
| 42 return std::string(reinterpret_cast<const char*>(vector_as_array(&data_)), | 37 return std::string(reinterpret_cast<const char*>(vector_as_array(&data_)), |
| 43 data_.size()); | 38 data_.size()); |
| 44 } | 39 } |
| 45 | 40 |
| 46 private: | 41 private: |
| 47 friend class base::RefCountedThreadSafe<PublicKey>; | 42 friend class base::RefCountedThreadSafe<PublicKey>; |
| 48 | 43 |
| 49 virtual ~PublicKey(); | 44 virtual ~PublicKey(); |
| 50 | 45 |
| 51 std::vector<uint8> data_; | 46 std::vector<uint8> data_; |
| 52 | 47 |
| 53 DISALLOW_COPY_AND_ASSIGN(PublicKey); | 48 DISALLOW_COPY_AND_ASSIGN(PublicKey); |
| 54 }; | 49 }; |
| 55 | 50 |
| 56 class PrivateKey : public base::RefCountedThreadSafe<PrivateKey> { | 51 class OWNERSHIP_EXPORT PrivateKey |
| 52 : public base::RefCountedThreadSafe<PrivateKey> { | |
| 57 public: | 53 public: |
| 58 explicit PrivateKey(crypto::RSAPrivateKey* key); | 54 explicit PrivateKey(crypto::RSAPrivateKey* key); |
| 59 | 55 |
| 60 crypto::RSAPrivateKey* key() { return key_.get(); } | 56 crypto::RSAPrivateKey* key() { return key_.get(); } |
| 61 | 57 |
| 62 private: | 58 private: |
| 63 friend class base::RefCountedThreadSafe<PrivateKey>; | 59 friend class base::RefCountedThreadSafe<PrivateKey>; |
| 64 | 60 |
| 65 virtual ~PrivateKey(); | 61 virtual ~PrivateKey(); |
| 66 | 62 |
| 67 scoped_ptr<crypto::RSAPrivateKey> key_; | 63 scoped_ptr<crypto::RSAPrivateKey> key_; |
| 68 | 64 |
| 69 DISALLOW_COPY_AND_ASSIGN(PrivateKey); | 65 DISALLOW_COPY_AND_ASSIGN(PrivateKey); |
| 70 }; | 66 }; |
| 71 | 67 |
| 72 class OwnerKeyUtil : public base::RefCountedThreadSafe<OwnerKeyUtil> { | 68 class OWNERSHIP_EXPORT OwnerKeyUtil |
| 69 : public base::RefCountedThreadSafe<OwnerKeyUtil> { | |
| 73 public: | 70 public: |
| 74 // Creates an OwnerKeyUtil instance. | 71 // Creates an OwnerKeyUtil instance. |
| 75 static OwnerKeyUtil* Create(); | 72 static OwnerKeyUtil* Create(); |
| 76 | 73 |
| 77 // Attempts to read the public key from the file system. | 74 // Attempts to read the public key from the file system. |
| 78 // Upon success, returns true and populates |output|. False on failure. | 75 // Upon success, returns true and populates |output|. False on failure. |
| 79 virtual bool ImportPublicKey(std::vector<uint8>* output) = 0; | 76 virtual bool ImportPublicKey(std::vector<uint8>* output) = 0; |
| 80 | 77 |
| 81 // Looks for the private key associated with |key| in the |slot| | 78 // Looks for the private key associated with |key| in the |slot| |
| 82 // and returns it if it can be found. Returns NULL otherwise. | 79 // and returns it if it can be found. Returns NULL otherwise. |
| 83 // Caller takes ownership. | 80 // Caller takes ownership. |
| 84 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot( | 81 virtual crypto::RSAPrivateKey* FindPrivateKeyInSlot( |
| 85 const std::vector<uint8>& key, | 82 const std::vector<uint8>& key, |
| 86 PK11SlotInfo* slot) = 0; | 83 PK11SlotInfo* slot) = 0; |
| 87 | 84 |
| 88 // Checks whether the public key is present in the file system. | 85 // Checks whether the public key is present in the file system. |
| 89 virtual bool IsPublicKeyPresent() = 0; | 86 virtual bool IsPublicKeyPresent() = 0; |
| 90 | 87 |
| 91 protected: | 88 protected: |
| 92 OwnerKeyUtil(); | 89 OwnerKeyUtil(); |
| 93 virtual ~OwnerKeyUtil(); | 90 virtual ~OwnerKeyUtil(); |
| 94 | 91 |
| 95 private: | 92 private: |
| 96 friend class base::RefCountedThreadSafe<OwnerKeyUtil>; | 93 friend class base::RefCountedThreadSafe<OwnerKeyUtil>; |
| 97 | |
| 98 FRIEND_TEST_ALL_PREFIXES(OwnerKeyUtilTest, ExportImportPublicKey); | |
| 99 }; | 94 }; |
| 100 | 95 |
| 101 // Implementation of OwnerKeyUtil that is used in production code. | 96 } // namespace ownership |
| 102 class OwnerKeyUtilImpl : public OwnerKeyUtil { | |
| 103 public: | |
| 104 explicit OwnerKeyUtilImpl(const base::FilePath& public_key_file); | |
| 105 | 97 |
| 106 // OwnerKeyUtil: | 98 #endif // COMPONENTS_OWNERSHIP_OWNER_KEY_UTIL_H_ |
| 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; | |
| 112 | |
| 113 protected: | |
| 114 virtual ~OwnerKeyUtilImpl(); | |
| 115 | |
| 116 private: | |
| 117 // The file that holds the public key. | |
| 118 base::FilePath key_file_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilImpl); | |
| 121 }; | |
| 122 | |
| 123 } // namespace chromeos | |
| 124 | |
| 125 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_OWNER_KEY_UTIL_H_ | |
| OLD | NEW |