OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/chromeos/login/owner_key_utils.h" | 5 #include "chrome/browser/chromeos/login/owner_key_utils.h" |
6 | 6 |
7 #include <keyhi.h> // SECKEY_CreateSubjectPublicKeyInfo() | 7 #include <keyhi.h> // SECKEY_CreateSubjectPublicKeyInfo() |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <prerror.h> // PR_GetError() | 9 #include <prerror.h> // PR_GetError() |
10 #include <secder.h> // DER_Encode() | 10 #include <secder.h> // DER_Encode() |
11 #include <secmod.h> | 11 #include <secmod.h> |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 | 14 |
15 #include "base/crypto/rsa_private_key.h" | 15 #include "base/crypto/rsa_private_key.h" |
16 #include "base/file_path.h" | 16 #include "base/file_path.h" |
17 #include "base/file_util.h" | 17 #include "base/file_util.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/nss_util_internal.h" | 19 #include "base/nss_util_internal.h" |
20 #include "base/nss_util.h" | 20 #include "base/nss_util.h" |
21 #include "base/scoped_ptr.h" | 21 #include "base/scoped_ptr.h" |
22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
| 23 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 24 #include "chrome/browser/chromeos/cros/login_library.h" |
23 | 25 |
24 using base::RSAPrivateKey; | 26 using base::RSAPrivateKey; |
25 | 27 |
26 namespace chromeos { | 28 namespace chromeos { |
27 | 29 |
28 /////////////////////////////////////////////////////////////////////////// | 30 /////////////////////////////////////////////////////////////////////////// |
29 // OwnerKeyUtils | 31 // OwnerKeyUtils |
30 | 32 |
31 // static | 33 // static |
32 OwnerKeyUtils::Factory* OwnerKeyUtils::factory_ = NULL; | 34 OwnerKeyUtils::Factory* OwnerKeyUtils::factory_ = NULL; |
33 | 35 |
34 OwnerKeyUtils::OwnerKeyUtils() {} | 36 OwnerKeyUtils::OwnerKeyUtils() {} |
35 | 37 |
36 OwnerKeyUtils::~OwnerKeyUtils() {} | 38 OwnerKeyUtils::~OwnerKeyUtils() {} |
37 | 39 |
38 /////////////////////////////////////////////////////////////////////////// | 40 /////////////////////////////////////////////////////////////////////////// |
39 // OwnerKeyUtilsImpl | 41 // OwnerKeyUtilsImpl |
40 | 42 |
41 class OwnerKeyUtilsImpl : public OwnerKeyUtils { | 43 class OwnerKeyUtilsImpl : public OwnerKeyUtils { |
42 public: | 44 public: |
43 OwnerKeyUtilsImpl(); | 45 OwnerKeyUtilsImpl(); |
44 virtual ~OwnerKeyUtilsImpl(); | |
45 | 46 |
46 RSAPrivateKey* GenerateKeyPair(); | 47 RSAPrivateKey* GenerateKeyPair(); |
47 | 48 |
48 bool ExportPublicKeyViaDbus(RSAPrivateKey* pair); | 49 bool ExportPublicKeyViaDbus(RSAPrivateKey* pair, |
| 50 LoginLibrary::Delegate<bool>* d); |
49 | 51 |
50 bool ExportPublicKeyToFile(RSAPrivateKey* pair, const FilePath& key_file); | 52 bool ExportPublicKeyToFile(RSAPrivateKey* pair, const FilePath& key_file); |
51 | 53 |
52 bool ImportPublicKey(const FilePath& key_file, | 54 bool ImportPublicKey(const FilePath& key_file, |
53 std::vector<uint8>* output); | 55 std::vector<uint8>* output); |
54 | 56 |
55 RSAPrivateKey* FindPrivateKey(const std::vector<uint8>& key); | 57 RSAPrivateKey* FindPrivateKey(const std::vector<uint8>& key); |
56 | 58 |
57 FilePath GetOwnerKeyFilePath(); | 59 FilePath GetOwnerKeyFilePath(); |
58 | 60 |
| 61 protected: |
| 62 virtual ~OwnerKeyUtilsImpl(); |
| 63 |
59 private: | 64 private: |
60 // The file outside the owner's encrypted home directory where her | 65 // The file outside the owner's encrypted home directory where her |
61 // key will live. | 66 // key will live. |
62 static const char kOwnerKeyFile[]; | 67 static const char kOwnerKeyFile[]; |
63 | 68 |
64 // Key generation parameters. | 69 // Key generation parameters. |
65 static const uint16 kKeySizeInBits; | 70 static const uint16 kKeySizeInBits; |
66 | 71 |
67 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilsImpl); | 72 DISALLOW_COPY_AND_ASSIGN(OwnerKeyUtilsImpl); |
68 }; | 73 }; |
(...skipping 17 matching lines...) Expand all Loading... |
86 // Ensure NSS is initialized. | 91 // Ensure NSS is initialized. |
87 base::EnsureNSSInit(); | 92 base::EnsureNSSInit(); |
88 } | 93 } |
89 | 94 |
90 OwnerKeyUtilsImpl::~OwnerKeyUtilsImpl() {} | 95 OwnerKeyUtilsImpl::~OwnerKeyUtilsImpl() {} |
91 | 96 |
92 RSAPrivateKey* OwnerKeyUtilsImpl::GenerateKeyPair() { | 97 RSAPrivateKey* OwnerKeyUtilsImpl::GenerateKeyPair() { |
93 return RSAPrivateKey::CreateSensitive(kKeySizeInBits); | 98 return RSAPrivateKey::CreateSensitive(kKeySizeInBits); |
94 } | 99 } |
95 | 100 |
96 bool OwnerKeyUtilsImpl::ExportPublicKeyViaDbus(RSAPrivateKey* pair) { | 101 bool OwnerKeyUtilsImpl::ExportPublicKeyViaDbus( |
| 102 RSAPrivateKey* pair, |
| 103 LoginLibrary::Delegate<bool>* d) { |
97 DCHECK(pair); | 104 DCHECK(pair); |
98 bool ok = false; | 105 bool ok = false; |
99 | 106 |
100 std::vector<uint8> to_export; | 107 std::vector<uint8> to_export; |
101 if (pair->ExportPublicKey(&to_export)) { | 108 if (pair->ExportPublicKey(&to_export)) { |
102 LOG(ERROR) << "Formatting key for export failed!"; | 109 LOG(ERROR) << "Formatting key for export failed!"; |
103 return false; | 110 return false; |
104 } | 111 } |
105 | 112 |
106 // TODO(cmasone): send the data over dbus. | 113 if (CrosLibrary::Get()->EnsureLoaded()) |
| 114 ok = CrosLibrary::Get()->GetLoginLibrary()->SetOwnerKey(to_export, d); |
| 115 |
107 return ok; | 116 return ok; |
108 } | 117 } |
109 | 118 |
110 bool OwnerKeyUtilsImpl::ExportPublicKeyToFile(RSAPrivateKey* pair, | 119 bool OwnerKeyUtilsImpl::ExportPublicKeyToFile(RSAPrivateKey* pair, |
111 const FilePath& key_file) { | 120 const FilePath& key_file) { |
112 DCHECK(pair); | 121 DCHECK(pair); |
113 bool ok = false; | 122 bool ok = false; |
114 int safe_file_size = 0; | 123 int safe_file_size = 0; |
115 | 124 |
116 std::vector<uint8> to_export; | 125 std::vector<uint8> to_export; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 RSAPrivateKey* OwnerKeyUtilsImpl::FindPrivateKey( | 167 RSAPrivateKey* OwnerKeyUtilsImpl::FindPrivateKey( |
159 const std::vector<uint8>& key) { | 168 const std::vector<uint8>& key) { |
160 return RSAPrivateKey::FindFromPublicKeyInfo(key); | 169 return RSAPrivateKey::FindFromPublicKeyInfo(key); |
161 } | 170 } |
162 | 171 |
163 FilePath OwnerKeyUtilsImpl::GetOwnerKeyFilePath() { | 172 FilePath OwnerKeyUtilsImpl::GetOwnerKeyFilePath() { |
164 return FilePath(OwnerKeyUtilsImpl::kOwnerKeyFile); | 173 return FilePath(OwnerKeyUtilsImpl::kOwnerKeyFile); |
165 } | 174 } |
166 | 175 |
167 } // namespace chromeos | 176 } // namespace chromeos |
OLD | NEW |