OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <keyhi.h> | 8 #include <keyhi.h> |
9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
10 #include <secmod.h> | 10 #include <secmod.h> |
11 | 11 |
12 #include <list> | 12 #include <list> |
13 | 13 |
14 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
19 #include "crypto/nss_util_internal.h" | 19 #include "crypto/nss_util_internal.h" |
20 #include "crypto/scoped_nss_types.h" | 20 #include "crypto/scoped_nss_types.h" |
21 | 21 |
22 // TODO(rafaelw): Consider refactoring common functions and definitions from | 22 // TODO(rafaelw): Consider using NSS's ASN.1 encoder. |
23 // rsa_private_key_win.cc or using NSS's ASN.1 encoder. | |
24 namespace { | 23 namespace { |
25 | 24 |
26 static bool ReadAttribute(SECKEYPrivateKey* key, | 25 static bool ReadAttribute(SECKEYPrivateKey* key, |
27 CK_ATTRIBUTE_TYPE type, | 26 CK_ATTRIBUTE_TYPE type, |
28 std::vector<uint8>* output) { | 27 std::vector<uint8>* output) { |
29 SECItem item; | 28 SECItem item; |
30 SECStatus rv; | 29 SECStatus rv; |
31 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); | 30 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); |
32 if (rv != SECSuccess) { | 31 if (rv != SECSuccess) { |
33 NOTREACHED(); | 32 NOTREACHED(); |
(...skipping 12 matching lines...) Expand all Loading... |
46 RSAPrivateKey::~RSAPrivateKey() { | 45 RSAPrivateKey::~RSAPrivateKey() { |
47 if (key_) | 46 if (key_) |
48 SECKEY_DestroyPrivateKey(key_); | 47 SECKEY_DestroyPrivateKey(key_); |
49 if (public_key_) | 48 if (public_key_) |
50 SECKEY_DestroyPublicKey(public_key_); | 49 SECKEY_DestroyPublicKey(public_key_); |
51 } | 50 } |
52 | 51 |
53 // static | 52 // static |
54 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 53 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
55 return CreateWithParams(num_bits, | 54 return CreateWithParams(num_bits, |
56 PR_FALSE /* not permanent */, | 55 false /* not permanent */, |
57 PR_FALSE /* not sensitive */); | 56 false /* not sensitive */); |
58 } | |
59 | |
60 // static | |
61 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | |
62 return CreateWithParams(num_bits, | |
63 PR_TRUE /* permanent */, | |
64 PR_TRUE /* sensitive */); | |
65 } | 57 } |
66 | 58 |
67 // static | 59 // static |
68 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 60 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
69 const std::vector<uint8>& input) { | 61 const std::vector<uint8>& input) { |
70 return CreateFromPrivateKeyInfoWithParams(input, | 62 return CreateFromPrivateKeyInfoWithParams(input, |
71 PR_FALSE /* not permanent */, | 63 false /* not permanent */, |
72 PR_FALSE /* not sensitive */); | 64 false /* not sensitive */); |
| 65 } |
| 66 |
| 67 #if defined(USE_NSS) |
| 68 // static |
| 69 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
| 70 return CreateWithParams(num_bits, |
| 71 true /* permanent */, |
| 72 true /* sensitive */); |
73 } | 73 } |
74 | 74 |
75 // static | 75 // static |
76 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | 76 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
77 const std::vector<uint8>& input) { | 77 const std::vector<uint8>& input) { |
78 return CreateFromPrivateKeyInfoWithParams(input, | 78 return CreateFromPrivateKeyInfoWithParams(input, |
79 PR_TRUE /* permanent */, | 79 true /* permanent */, |
80 PR_TRUE /* sensitive */); | 80 true /* sensitive */); |
81 } | 81 } |
82 | 82 |
83 // static | 83 // static |
84 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) { | 84 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) { |
85 DCHECK(key); | 85 DCHECK(key); |
86 if (SECKEY_GetPrivateKeyType(key) != rsaKey) | 86 if (SECKEY_GetPrivateKeyType(key) != rsaKey) |
87 return NULL; | 87 return NULL; |
88 RSAPrivateKey* copy = new RSAPrivateKey(); | 88 RSAPrivateKey* copy = new RSAPrivateKey(); |
89 copy->key_ = SECKEY_CopyPrivateKey(key); | 89 copy->key_ = SECKEY_CopyPrivateKey(key); |
90 copy->public_key_ = SECKEY_ConvertToPublicKey(key); | 90 copy->public_key_ = SECKEY_ConvertToPublicKey(key); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], | 146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
147 ck_id.get(), NULL); | 147 ck_id.get(), NULL); |
148 if (result->key_) | 148 if (result->key_) |
149 return result.release(); | 149 return result.release(); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 // We didn't find the key. | 153 // We didn't find the key. |
154 return NULL; | 154 return NULL; |
155 } | 155 } |
| 156 #endif |
156 | 157 |
157 RSAPrivateKey* RSAPrivateKey::Copy() const { | 158 RSAPrivateKey* RSAPrivateKey::Copy() const { |
158 RSAPrivateKey* copy = new RSAPrivateKey(); | 159 RSAPrivateKey* copy = new RSAPrivateKey(); |
159 copy->key_ = SECKEY_CopyPrivateKey(key_); | 160 copy->key_ = SECKEY_CopyPrivateKey(key_); |
160 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 161 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
161 return copy; | 162 return copy; |
162 } | 163 } |
163 | 164 |
164 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 165 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
165 PrivateKeyInfoCodec private_key_info(true); | 166 PrivateKeyInfoCodec private_key_info(true); |
(...skipping 29 matching lines...) Expand all Loading... |
195 } | 196 } |
196 | 197 |
197 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { | 198 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { |
198 EnsureNSSInit(); | 199 EnsureNSSInit(); |
199 } | 200 } |
200 | 201 |
201 // static | 202 // static |
202 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, | 203 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, |
203 bool permanent, | 204 bool permanent, |
204 bool sensitive) { | 205 bool sensitive) { |
| 206 #if !defined(USE_NSS) |
| 207 if (permanent) { |
| 208 NOTIMPLEMENTED(); |
| 209 return NULL; |
| 210 } |
| 211 #endif |
| 212 |
205 EnsureNSSInit(); | 213 EnsureNSSInit(); |
206 | 214 |
207 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 215 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
208 | 216 |
209 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); | 217 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); |
210 if (!slot.get()) | 218 if (!slot.get()) |
211 return NULL; | 219 return NULL; |
212 | 220 |
213 PK11RSAGenParams param; | 221 PK11RSAGenParams param; |
214 param.keySizeInBits = num_bits; | 222 param.keySizeInBits = num_bits; |
215 param.pe = 65537L; | 223 param.pe = 65537L; |
216 result->key_ = PK11_GenerateKeyPair(slot.get(), | 224 result->key_ = PK11_GenerateKeyPair(slot.get(), |
217 CKM_RSA_PKCS_KEY_PAIR_GEN, | 225 CKM_RSA_PKCS_KEY_PAIR_GEN, |
218 ¶m, | 226 ¶m, |
219 &result->public_key_, | 227 &result->public_key_, |
220 permanent, | 228 permanent, |
221 sensitive, | 229 sensitive, |
222 NULL); | 230 NULL); |
223 if (!result->key_) | 231 if (!result->key_) |
224 return NULL; | 232 return NULL; |
225 | 233 |
226 return result.release(); | 234 return result.release(); |
227 } | 235 } |
228 | 236 |
229 // static | 237 // static |
230 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( | 238 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( |
231 const std::vector<uint8>& input, bool permanent, bool sensitive) { | 239 const std::vector<uint8>& input, bool permanent, bool sensitive) { |
| 240 #if !defined(USE_NSS) |
| 241 if (permanent) { |
| 242 NOTIMPLEMENTED(); |
| 243 return NULL; |
| 244 } |
| 245 #endif |
| 246 |
232 // This method currently leaks some memory. | 247 // This method currently leaks some memory. |
233 // See http://crbug.com/34742. | 248 // See http://crbug.com/34742. |
234 ANNOTATE_SCOPED_MEMORY_LEAK; | 249 ANNOTATE_SCOPED_MEMORY_LEAK; |
235 EnsureNSSInit(); | 250 EnsureNSSInit(); |
236 | 251 |
237 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 252 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
238 | 253 |
239 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); | 254 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); |
240 if (!slot.get()) | 255 if (!slot.get()) |
241 return NULL; | 256 return NULL; |
(...skipping 16 matching lines...) Expand all Loading... |
258 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 273 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
259 if (!result->public_key_) { | 274 if (!result->public_key_) { |
260 NOTREACHED(); | 275 NOTREACHED(); |
261 return NULL; | 276 return NULL; |
262 } | 277 } |
263 | 278 |
264 return result.release(); | 279 return result.release(); |
265 } | 280 } |
266 | 281 |
267 } // namespace crypto | 282 } // namespace crypto |
OLD | NEW |