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> |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( | 271 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( |
272 PK11SlotInfo* slot, | 272 PK11SlotInfo* slot, |
273 const std::vector<uint8>& input, | 273 const std::vector<uint8>& input, |
274 bool permanent, | 274 bool permanent, |
275 bool sensitive) { | 275 bool sensitive) { |
276 if (!slot) | 276 if (!slot) |
277 return NULL; | 277 return NULL; |
278 | 278 |
279 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 279 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
280 | 280 |
| 281 ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); |
| 282 if (!arena) { |
| 283 NOTREACHED(); |
| 284 return NULL; |
| 285 } |
| 286 |
| 287 // Excess data is illegal, but NSS silently accepts it, so first ensure that |
| 288 // |input| consists of a single ASN.1 element. |
| 289 SECItem input_item; |
| 290 input_item.data = const_cast<unsigned char*>(&input.front()); |
| 291 input_item.len = input.size(); |
281 SECItem der_private_key_info; | 292 SECItem der_private_key_info; |
282 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); | 293 SECStatus rv = SEC_QuickDERDecodeItem(arena.get(), &der_private_key_info, |
283 der_private_key_info.len = input.size(); | 294 SEC_ASN1_GET(SEC_AnyTemplate), |
| 295 &input_item); |
| 296 if (rv != SECSuccess) |
| 297 return NULL; |
| 298 |
284 // Allow the private key to be used for key unwrapping, data decryption, | 299 // Allow the private key to be used for key unwrapping, data decryption, |
285 // and signature generation. | 300 // and signature generation. |
286 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | | 301 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | |
287 KU_DIGITAL_SIGNATURE; | 302 KU_DIGITAL_SIGNATURE; |
288 // TODO(davidben): PK11_ImportDERPrivateKeyInfoAndReturnKey calls NSS's | 303 rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( |
289 // SEC_ASN1DecodeItem which does not enforce that there is no trailing | |
290 // data. | |
291 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( | |
292 slot, &der_private_key_info, NULL, NULL, permanent, sensitive, | 304 slot, &der_private_key_info, NULL, NULL, permanent, sensitive, |
293 key_usage, &result->key_, NULL); | 305 key_usage, &result->key_, NULL); |
294 if (rv != SECSuccess) { | 306 if (rv != SECSuccess) |
295 NOTREACHED(); | |
296 return NULL; | 307 return NULL; |
297 } | |
298 | 308 |
299 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 309 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
300 if (!result->public_key_) { | 310 if (!result->public_key_) |
301 NOTREACHED(); | |
302 return NULL; | 311 return NULL; |
303 } | |
304 | 312 |
305 return result.release(); | 313 return result.release(); |
306 } | 314 } |
307 | 315 |
308 #if defined(USE_NSS) | 316 #if defined(USE_NSS) |
309 // static | 317 // static |
310 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { | 318 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { |
311 EnsureNSSInit(); | 319 EnsureNSSInit(); |
312 | 320 |
313 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); | 321 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); |
314 result->public_key_ = GetRSAPublicKey(input).release(); | 322 result->public_key_ = GetRSAPublicKey(input).release(); |
315 if (!result->public_key_) { | 323 if (!result->public_key_) { |
316 NOTREACHED(); | 324 NOTREACHED(); |
317 return NULL; | 325 return NULL; |
318 } | 326 } |
319 | 327 |
320 return result.release(); | 328 return result.release(); |
321 } | 329 } |
322 #endif // defined(USE_NSS) | 330 #endif // defined(USE_NSS) |
323 | 331 |
324 } // namespace crypto | 332 } // namespace crypto |
OLD | NEW |