OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/crypto/rsa_private_key.h" | 5 #include "base/crypto/rsa_private_key.h" |
6 | 6 |
7 #include <iostream> | 7 #include <iostream> |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // static | 25 // static |
26 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 26 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
27 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 27 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
28 if (!result->InitProvider()) | 28 if (!result->InitProvider()) |
29 return NULL; | 29 return NULL; |
30 | 30 |
31 DWORD flags = CRYPT_EXPORTABLE; | 31 DWORD flags = CRYPT_EXPORTABLE; |
32 | 32 |
33 // The size is encoded as the upper 16 bits of the flags. :: sigh ::. | 33 // The size is encoded as the upper 16 bits of the flags. :: sigh ::. |
34 flags |= (num_bits << 16); | 34 flags |= (num_bits << 16); |
35 if (!CryptGenKey(result->provider_, CALG_RSA_SIGN, flags, &result->key_)) | 35 if (!CryptGenKey(result->provider_, CALG_RSA_SIGN, flags, |
| 36 result->key_.receive())) |
36 return NULL; | 37 return NULL; |
37 | 38 |
38 return result.release(); | 39 return result.release(); |
39 } | 40 } |
40 | 41 |
41 // static | 42 // static |
42 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 43 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
43 const std::vector<uint8>& input) { | 44 const std::vector<uint8>& input) { |
44 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 45 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
45 if (!result->InitProvider()) | 46 if (!result->InitProvider()) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 dest += pki.exponent1()->size(); | 89 dest += pki.exponent1()->size(); |
89 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); | 90 memcpy(dest, &pki.exponent2()->front(), pki.exponent2()->size()); |
90 dest += pki.exponent2()->size(); | 91 dest += pki.exponent2()->size(); |
91 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); | 92 memcpy(dest, &pki.coefficient()->front(), pki.coefficient()->size()); |
92 dest += pki.coefficient()->size(); | 93 dest += pki.coefficient()->size(); |
93 memcpy(dest, &pki.private_exponent()->front(), pki.private_exponent()->size())
; | 94 memcpy(dest, &pki.private_exponent()->front(), pki.private_exponent()->size())
; |
94 dest += pki.private_exponent()->size(); | 95 dest += pki.private_exponent()->size(); |
95 | 96 |
96 READ_ASSERT(dest == blob.get() + blob_size); | 97 READ_ASSERT(dest == blob.get() + blob_size); |
97 if (!CryptImportKey( | 98 if (!CryptImportKey( |
98 result->provider_, reinterpret_cast<uint8*>(public_key_struc), blob_size, | 99 result->provider_, reinterpret_cast<uint8*>(public_key_struc), |
99 NULL, CRYPT_EXPORTABLE, &result->key_)) { | 100 blob_size, NULL, CRYPT_EXPORTABLE, result->key_.receive())) { |
100 return NULL; | 101 return NULL; |
101 } | 102 } |
102 | 103 |
103 return result.release(); | 104 return result.release(); |
104 } | 105 } |
105 | 106 |
106 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} | 107 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} |
107 | 108 |
108 RSAPrivateKey::~RSAPrivateKey() { | 109 RSAPrivateKey::~RSAPrivateKey() {} |
109 if (key_) { | |
110 if (!CryptDestroyKey(key_)) | |
111 NOTREACHED(); | |
112 } | |
113 | |
114 if (provider_) { | |
115 if (!CryptReleaseContext(provider_, 0)) | |
116 NOTREACHED(); | |
117 } | |
118 } | |
119 | 110 |
120 bool RSAPrivateKey::InitProvider() { | 111 bool RSAPrivateKey::InitProvider() { |
121 return FALSE != CryptAcquireContext(&provider_, NULL, NULL, | 112 return FALSE != CryptAcquireContext(provider_.receive(), NULL, NULL, |
122 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); | 113 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); |
123 } | 114 } |
124 | 115 |
125 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 116 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
126 // Export the key | 117 // Export the key |
127 DWORD blob_length = 0; | 118 DWORD blob_length = 0; |
128 if (!CryptExportKey(key_, NULL, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { | 119 if (!CryptExportKey(key_, NULL, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { |
129 NOTREACHED(); | 120 NOTREACHED(); |
130 return false; | 121 return false; |
131 } | 122 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return false; | 202 return false; |
212 } | 203 } |
213 | 204 |
214 for (size_t i = 0; i < encoded_length; ++i) | 205 for (size_t i = 0; i < encoded_length; ++i) |
215 output->push_back(encoded[i]); | 206 output->push_back(encoded[i]); |
216 | 207 |
217 return true; | 208 return true; |
218 } | 209 } |
219 | 210 |
220 } // namespace base | 211 } // namespace base |
OLD | NEW |