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 <list> | 7 #include <list> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} | 128 RSAPrivateKey::RSAPrivateKey() : provider_(NULL), key_(NULL) {} |
129 | 129 |
130 RSAPrivateKey::~RSAPrivateKey() {} | 130 RSAPrivateKey::~RSAPrivateKey() {} |
131 | 131 |
132 bool RSAPrivateKey::InitProvider() { | 132 bool RSAPrivateKey::InitProvider() { |
133 return FALSE != CryptAcquireContext(provider_.receive(), NULL, NULL, | 133 return FALSE != CryptAcquireContext(provider_.receive(), NULL, NULL, |
134 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); | 134 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); |
135 } | 135 } |
136 | 136 |
137 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 137 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 138 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); |
| 139 if (!CryptContextAddRef(provider_, NULL, 0)) { |
| 140 NOTREACHED(); |
| 141 return NULL; |
| 142 } |
| 143 copy->provider_.reset(provider_.get()); |
| 144 if (!CryptDuplicateKey(key_.get(), NULL, 0, copy->key_.receive())) |
| 145 return NULL; |
| 146 return copy.release(); |
| 147 } |
| 148 |
| 149 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
138 // Export the key | 150 // Export the key |
139 DWORD blob_length = 0; | 151 DWORD blob_length = 0; |
140 if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { | 152 if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, NULL, &blob_length)) { |
141 NOTREACHED(); | 153 NOTREACHED(); |
142 return false; | 154 return false; |
143 } | 155 } |
144 | 156 |
145 scoped_array<uint8> blob(new uint8[blob_length]); | 157 scoped_array<uint8> blob(new uint8[blob_length]); |
146 if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, blob.get(), &blob_length)) { | 158 if (!CryptExportKey(key_, 0, PRIVATEKEYBLOB, 0, blob.get(), &blob_length)) { |
147 NOTREACHED(); | 159 NOTREACHED(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 pos += mod_size; | 192 pos += mod_size; |
181 | 193 |
182 pki.public_exponent()->assign(reinterpret_cast<uint8*>(&rsa_pub_key->pubexp), | 194 pki.public_exponent()->assign(reinterpret_cast<uint8*>(&rsa_pub_key->pubexp), |
183 reinterpret_cast<uint8*>(&rsa_pub_key->pubexp) + 4); | 195 reinterpret_cast<uint8*>(&rsa_pub_key->pubexp) + 4); |
184 | 196 |
185 CHECK_EQ(pos - blob_length, reinterpret_cast<BYTE*>(publickey_struct)); | 197 CHECK_EQ(pos - blob_length, reinterpret_cast<BYTE*>(publickey_struct)); |
186 | 198 |
187 return pki.Export(output); | 199 return pki.Export(output); |
188 } | 200 } |
189 | 201 |
190 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { | 202 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) const { |
191 DWORD key_info_len; | 203 DWORD key_info_len; |
192 if (!CryptExportPublicKeyInfo( | 204 if (!CryptExportPublicKeyInfo( |
193 provider_, AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, | 205 provider_, AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
194 NULL, &key_info_len)) { | 206 NULL, &key_info_len)) { |
195 NOTREACHED(); | 207 NOTREACHED(); |
196 return false; | 208 return false; |
197 } | 209 } |
198 | 210 |
199 scoped_array<uint8> key_info(new uint8[key_info_len]); | 211 scoped_array<uint8> key_info(new uint8[key_info_len]); |
200 if (!CryptExportPublicKeyInfo( | 212 if (!CryptExportPublicKeyInfo( |
(...skipping 19 matching lines...) Expand all Loading... |
220 &encoded_length)) { | 232 &encoded_length)) { |
221 NOTREACHED(); | 233 NOTREACHED(); |
222 return false; | 234 return false; |
223 } | 235 } |
224 | 236 |
225 output->assign(encoded.get(), encoded.get() + encoded_length); | 237 output->assign(encoded.get(), encoded.get() + encoded_length); |
226 return true; | 238 return true; |
227 } | 239 } |
228 | 240 |
229 } // namespace crypto | 241 } // namespace crypto |
OLD | NEW |