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 20 matching lines...) Expand all Loading... |
31 if (rv != SECSuccess) { | 31 if (rv != SECSuccess) { |
32 NOTREACHED(); | 32 NOTREACHED(); |
33 return false; | 33 return false; |
34 } | 34 } |
35 | 35 |
36 output->assign(item.data, item.data + item.len); | 36 output->assign(item.data, item.data + item.len); |
37 SECITEM_FreeItem(&item, PR_FALSE); | 37 SECITEM_FreeItem(&item, PR_FALSE); |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 #if defined(USE_NSS) | 41 #if defined(USE_NSS_CERTS) |
42 struct PublicKeyInfoDeleter { | 42 struct PublicKeyInfoDeleter { |
43 inline void operator()(CERTSubjectPublicKeyInfo* spki) { | 43 inline void operator()(CERTSubjectPublicKeyInfo* spki) { |
44 SECKEY_DestroySubjectPublicKeyInfo(spki); | 44 SECKEY_DestroySubjectPublicKeyInfo(spki); |
45 } | 45 } |
46 }; | 46 }; |
47 | 47 |
48 typedef scoped_ptr<CERTSubjectPublicKeyInfo, PublicKeyInfoDeleter> | 48 typedef scoped_ptr<CERTSubjectPublicKeyInfo, PublicKeyInfoDeleter> |
49 ScopedPublicKeyInfo; | 49 ScopedPublicKeyInfo; |
50 | 50 |
51 // The function decodes RSA public key from the |input|. | 51 // The function decodes RSA public key from the |input|. |
52 crypto::ScopedSECKEYPublicKey GetRSAPublicKey(const std::vector<uint8>& input) { | 52 crypto::ScopedSECKEYPublicKey GetRSAPublicKey(const std::vector<uint8>& input) { |
53 // First, decode and save the public key. | 53 // First, decode and save the public key. |
54 SECItem key_der; | 54 SECItem key_der; |
55 key_der.type = siBuffer; | 55 key_der.type = siBuffer; |
56 key_der.data = const_cast<unsigned char*>(&input[0]); | 56 key_der.data = const_cast<unsigned char*>(&input[0]); |
57 key_der.len = input.size(); | 57 key_der.len = input.size(); |
58 | 58 |
59 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); | 59 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); |
60 if (!spki) | 60 if (!spki) |
61 return crypto::ScopedSECKEYPublicKey(); | 61 return crypto::ScopedSECKEYPublicKey(); |
62 | 62 |
63 crypto::ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); | 63 crypto::ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); |
64 | 64 |
65 // Make sure the key is an RSA key.. If not, that's an error. | 65 // Make sure the key is an RSA key.. If not, that's an error. |
66 if (!result || result->keyType != rsaKey) | 66 if (!result || result->keyType != rsaKey) |
67 return crypto::ScopedSECKEYPublicKey(); | 67 return crypto::ScopedSECKEYPublicKey(); |
68 return result.Pass(); | 68 return result.Pass(); |
69 } | 69 } |
70 #endif // defined(USE_NSS) | 70 #endif // defined(USE_NSS_CERTS) |
71 | 71 |
72 } // namespace | 72 } // namespace |
73 | 73 |
74 namespace crypto { | 74 namespace crypto { |
75 | 75 |
76 RSAPrivateKey::~RSAPrivateKey() { | 76 RSAPrivateKey::~RSAPrivateKey() { |
77 if (key_) | 77 if (key_) |
78 SECKEY_DestroyPrivateKey(key_); | 78 SECKEY_DestroyPrivateKey(key_); |
79 if (public_key_) | 79 if (public_key_) |
80 SECKEY_DestroyPublicKey(public_key_); | 80 SECKEY_DestroyPublicKey(public_key_); |
(...skipping 16 matching lines...) Expand all Loading... |
97 EnsureNSSInit(); | 97 EnsureNSSInit(); |
98 | 98 |
99 ScopedPK11Slot slot(PK11_GetInternalSlot()); | 99 ScopedPK11Slot slot(PK11_GetInternalSlot()); |
100 return CreateFromPrivateKeyInfoWithParams( | 100 return CreateFromPrivateKeyInfoWithParams( |
101 slot.get(), | 101 slot.get(), |
102 input, | 102 input, |
103 false /* not permanent */, | 103 false /* not permanent */, |
104 false /* not sensitive */); | 104 false /* not sensitive */); |
105 } | 105 } |
106 | 106 |
107 #if defined(USE_NSS) | 107 #if defined(USE_NSS_CERTS) |
108 // static | 108 // static |
109 RSAPrivateKey* RSAPrivateKey::CreateSensitive(PK11SlotInfo* slot, | 109 RSAPrivateKey* RSAPrivateKey::CreateSensitive(PK11SlotInfo* slot, |
110 uint16 num_bits) { | 110 uint16 num_bits) { |
111 return CreateWithParams(slot, | 111 return CreateWithParams(slot, |
112 num_bits, | 112 num_bits, |
113 true /* permanent */, | 113 true /* permanent */, |
114 true /* sensitive */); | 114 true /* sensitive */); |
115 } | 115 } |
116 | 116 |
117 // static | 117 // static |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 if (rv != SECSuccess) | 306 if (rv != SECSuccess) |
307 return NULL; | 307 return NULL; |
308 | 308 |
309 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 309 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
310 if (!result->public_key_) | 310 if (!result->public_key_) |
311 return NULL; | 311 return NULL; |
312 | 312 |
313 return result.release(); | 313 return result.release(); |
314 } | 314 } |
315 | 315 |
316 #if defined(USE_NSS) | 316 #if defined(USE_NSS_CERTS) |
317 // static | 317 // static |
318 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { | 318 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { |
319 EnsureNSSInit(); | 319 EnsureNSSInit(); |
320 | 320 |
321 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); | 321 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); |
322 result->public_key_ = GetRSAPublicKey(input).release(); | 322 result->public_key_ = GetRSAPublicKey(input).release(); |
323 if (!result->public_key_) { | 323 if (!result->public_key_) { |
324 NOTREACHED(); | 324 NOTREACHED(); |
325 return NULL; | 325 return NULL; |
326 } | 326 } |
327 | 327 |
328 return result.release(); | 328 return result.release(); |
329 } | 329 } |
330 #endif // defined(USE_NSS) | 330 #endif // defined(USE_NSS_CERTS) |
331 | 331 |
332 } // namespace crypto | 332 } // namespace crypto |
OLD | NEW |