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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 } | 200 } |
201 | 201 |
202 // static | 202 // static |
203 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, | 203 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, |
204 bool permanent, | 204 bool permanent, |
205 bool sensitive) { | 205 bool sensitive) { |
206 EnsureNSSInit(); | 206 EnsureNSSInit(); |
207 | 207 |
208 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 208 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
209 | 209 |
210 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); | 210 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : |
211 PK11_GetInternalKeySlot()); | |
wtc
2013/06/20 19:57:37
I think we should use GetPublicNSSKeySlot() instea
Ryan Sleevi
2013/06/20 20:01:50
This was intentional, because I do not want to use
| |
211 if (!slot.get()) | 212 if (!slot.get()) |
212 return NULL; | 213 return NULL; |
213 | 214 |
214 PK11RSAGenParams param; | 215 PK11RSAGenParams param; |
215 param.keySizeInBits = num_bits; | 216 param.keySizeInBits = num_bits; |
216 param.pe = 65537L; | 217 param.pe = 65537L; |
217 result->key_ = PK11_GenerateKeyPair(slot.get(), | 218 result->key_ = PK11_GenerateKeyPair(slot.get(), |
218 CKM_RSA_PKCS_KEY_PAIR_GEN, | 219 CKM_RSA_PKCS_KEY_PAIR_GEN, |
219 ¶m, | 220 ¶m, |
220 &result->public_key_, | 221 &result->public_key_, |
221 permanent, | 222 permanent, |
222 sensitive, | 223 sensitive, |
223 NULL); | 224 NULL); |
224 if (!result->key_) | 225 if (!result->key_) |
225 return NULL; | 226 return NULL; |
226 | 227 |
227 return result.release(); | 228 return result.release(); |
228 } | 229 } |
229 | 230 |
230 // static | 231 // static |
231 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( | 232 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( |
232 const std::vector<uint8>& input, bool permanent, bool sensitive) { | 233 const std::vector<uint8>& input, bool permanent, bool sensitive) { |
233 // This method currently leaks some memory. | 234 // This method currently leaks some memory. |
234 // See http://crbug.com/34742. | 235 // See http://crbug.com/34742. |
235 ANNOTATE_SCOPED_MEMORY_LEAK; | 236 ANNOTATE_SCOPED_MEMORY_LEAK; |
236 EnsureNSSInit(); | 237 EnsureNSSInit(); |
237 | 238 |
238 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 239 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
239 | 240 |
240 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); | 241 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : |
242 PK11_GetInternalKeySlot()); | |
241 if (!slot.get()) | 243 if (!slot.get()) |
242 return NULL; | 244 return NULL; |
243 | 245 |
244 SECItem der_private_key_info; | 246 SECItem der_private_key_info; |
245 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); | 247 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); |
246 der_private_key_info.len = input.size(); | 248 der_private_key_info.len = input.size(); |
247 // Allow the private key to be used for key unwrapping, data decryption, | 249 // Allow the private key to be used for key unwrapping, data decryption, |
248 // and signature generation. | 250 // and signature generation. |
249 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | | 251 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | |
250 KU_DIGITAL_SIGNATURE; | 252 KU_DIGITAL_SIGNATURE; |
251 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( | 253 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( |
252 slot.get(), &der_private_key_info, NULL, NULL, permanent, sensitive, | 254 slot.get(), &der_private_key_info, NULL, NULL, permanent, sensitive, |
253 key_usage, &result->key_, NULL); | 255 key_usage, &result->key_, NULL); |
254 if (rv != SECSuccess) { | 256 if (rv != SECSuccess) { |
255 NOTREACHED(); | 257 NOTREACHED(); |
256 return NULL; | 258 return NULL; |
257 } | 259 } |
258 | 260 |
259 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 261 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
260 if (!result->public_key_) { | 262 if (!result->public_key_) { |
261 NOTREACHED(); | 263 NOTREACHED(); |
262 return NULL; | 264 return NULL; |
263 } | 265 } |
264 | 266 |
265 return result.release(); | 267 return result.release(); |
266 } | 268 } |
267 | 269 |
268 } // namespace crypto | 270 } // namespace crypto |
OLD | NEW |