Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1595)

Side by Side Diff: crypto/rsa_private_key_nss.cc

Issue 66213002: NSS: {EC,RSA}PrivateKey shouldn't call crypto::GetPublicNSSKeySlot or GetPrivateNSSKeySlot. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: gyp fixes Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 RSAPrivateKey::~RSAPrivateKey() { 45 RSAPrivateKey::~RSAPrivateKey() {
46 if (key_) 46 if (key_)
47 SECKEY_DestroyPrivateKey(key_); 47 SECKEY_DestroyPrivateKey(key_);
48 if (public_key_) 48 if (public_key_)
49 SECKEY_DestroyPublicKey(public_key_); 49 SECKEY_DestroyPublicKey(public_key_);
50 } 50 }
51 51
52 // static 52 // static
53 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { 53 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) {
54 return CreateWithParams(num_bits, 54 EnsureNSSInit();
55
56 return CreateWithParams(ScopedPK11Slot(PK11_GetInternalKeySlot()),
wtc 2013/11/11 20:56:25 IMPORTANT: this should be PK11_GetInternalSlot().
mattm 2013/11/12 02:42:44 Thanks, fixed.
57 num_bits,
55 false /* not permanent */, 58 false /* not permanent */,
56 false /* not sensitive */); 59 false /* not sensitive */);
57 } 60 }
58 61
59 // static 62 // static
60 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( 63 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo(
61 const std::vector<uint8>& input) { 64 const std::vector<uint8>& input) {
62 return CreateFromPrivateKeyInfoWithParams(input, 65 EnsureNSSInit();
63 false /* not permanent */, 66
64 false /* not sensitive */); 67 return CreateFromPrivateKeyInfoWithParams(
68 ScopedPK11Slot(PK11_GetInternalKeySlot()),
wtc 2013/11/11 20:56:25 IMPORTANT: this should be PK11_GetInternalSlot().
mattm 2013/11/12 02:42:44 Done.
69 input,
70 false /* not permanent */,
71 false /* not sensitive */);
65 } 72 }
66 73
67 #if defined(USE_NSS) 74 #if defined(USE_NSS)
68 // static 75 // static
69 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { 76 RSAPrivateKey* RSAPrivateKey::CreateSensitive(ScopedPK11Slot slot,
70 return CreateWithParams(num_bits, 77 uint16 num_bits) {
78 return CreateWithParams(slot.Pass(),
79 num_bits,
71 true /* permanent */, 80 true /* permanent */,
72 true /* sensitive */); 81 true /* sensitive */);
73 } 82 }
74 83
75 // static 84 // static
76 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( 85 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(
86 ScopedPK11Slot slot,
77 const std::vector<uint8>& input) { 87 const std::vector<uint8>& input) {
78 return CreateFromPrivateKeyInfoWithParams(input, 88 return CreateFromPrivateKeyInfoWithParams(slot.Pass(),
89 input,
79 true /* permanent */, 90 true /* permanent */,
80 true /* sensitive */); 91 true /* sensitive */);
81 } 92 }
82 93
83 // static 94 // static
84 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) { 95 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) {
85 DCHECK(key); 96 DCHECK(key);
86 if (SECKEY_GetPrivateKeyType(key) != rsaKey) 97 if (SECKEY_GetPrivateKeyType(key) != rsaKey)
87 return NULL; 98 return NULL;
88 RSAPrivateKey* copy = new RSAPrivateKey(); 99 RSAPrivateKey* copy = new RSAPrivateKey();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 204
194 output->assign(der_pubkey->data, der_pubkey->data + der_pubkey->len); 205 output->assign(der_pubkey->data, der_pubkey->data + der_pubkey->len);
195 return true; 206 return true;
196 } 207 }
197 208
198 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { 209 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) {
199 EnsureNSSInit(); 210 EnsureNSSInit();
200 } 211 }
201 212
202 // static 213 // static
203 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, 214 RSAPrivateKey* RSAPrivateKey::CreateWithParams(ScopedPK11Slot slot,
215 uint16 num_bits,
204 bool permanent, 216 bool permanent,
205 bool sensitive) { 217 bool sensitive) {
206 #if !defined(USE_NSS) 218 if (!slot.get())
207 if (permanent) {
208 NOTIMPLEMENTED();
209 return NULL; 219 return NULL;
210 }
211 #endif
wtc 2013/11/11 20:56:25 BUG?: it seems wrong to remove this check. In thi
mattm 2013/11/12 02:42:44 Hm, yeah. I guess I was thinking that since Create
wtc 2013/11/12 18:49:42 I see. Yes, you are right, we can conclude the che
mattm 2013/11/12 22:27:03 Done.
212
213 EnsureNSSInit();
214 220
215 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); 221 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
216 222
217 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() :
218 PK11_GetInternalSlot());
Ryan Sleevi 2013/11/09 02:26:47 Pretty sure ChromeOS is relying on this for TPM pr
mattm 2013/11/09 03:44:32 Hm, I found "platform/login_manager" uses RSAPriva
wtc 2013/11/11 20:58:20 Good. You already found the files that need to be
mattm 2013/11/12 02:42:44 So I don't know what the procedure is for rolling
wtc 2013/11/12 18:49:42 I don't know what the procedure is, either.
mattm 2013/11/12 22:27:03 Checked with cmasone and sent him an example patch
219 if (!slot.get())
220 return NULL;
221
222 PK11RSAGenParams param; 223 PK11RSAGenParams param;
223 param.keySizeInBits = num_bits; 224 param.keySizeInBits = num_bits;
224 param.pe = 65537L; 225 param.pe = 65537L;
225 result->key_ = PK11_GenerateKeyPair(slot.get(), 226 result->key_ = PK11_GenerateKeyPair(slot.get(),
226 CKM_RSA_PKCS_KEY_PAIR_GEN, 227 CKM_RSA_PKCS_KEY_PAIR_GEN,
227 &param, 228 &param,
228 &result->public_key_, 229 &result->public_key_,
229 permanent, 230 permanent,
230 sensitive, 231 sensitive,
231 NULL); 232 NULL);
232 if (!result->key_) 233 if (!result->key_)
233 return NULL; 234 return NULL;
234 235
235 return result.release(); 236 return result.release();
236 } 237 }
237 238
238 // static 239 // static
239 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( 240 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams(
240 const std::vector<uint8>& input, bool permanent, bool sensitive) { 241 ScopedPK11Slot slot,
241 #if !defined(USE_NSS) 242 const std::vector<uint8>& input,
242 if (permanent) { 243 bool permanent,
243 NOTIMPLEMENTED(); 244 bool sensitive) {
245 if (!slot.get())
244 return NULL; 246 return NULL;
245 }
246 #endif
wtc 2013/11/11 20:56:25 BUG?: it seems wrong to remove this check.
mattm 2013/11/12 02:42:44 Done.
247
248 // This method currently leaks some memory.
249 // See http://crbug.com/34742.
250 ANNOTATE_SCOPED_MEMORY_LEAK;
251 EnsureNSSInit();
252 247
253 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); 248 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
254 249
255 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() :
256 PK11_GetInternalSlot());
257 if (!slot.get())
258 return NULL;
259
260 SECItem der_private_key_info; 250 SECItem der_private_key_info;
261 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); 251 der_private_key_info.data = const_cast<unsigned char*>(&input.front());
262 der_private_key_info.len = input.size(); 252 der_private_key_info.len = input.size();
263 // Allow the private key to be used for key unwrapping, data decryption, 253 // Allow the private key to be used for key unwrapping, data decryption,
264 // and signature generation. 254 // and signature generation.
265 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | 255 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT |
266 KU_DIGITAL_SIGNATURE; 256 KU_DIGITAL_SIGNATURE;
267 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( 257 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(
268 slot.get(), &der_private_key_info, NULL, NULL, permanent, sensitive, 258 slot.get(), &der_private_key_info, NULL, NULL, permanent, sensitive,
269 key_usage, &result->key_, NULL); 259 key_usage, &result->key_, NULL);
270 if (rv != SECSuccess) { 260 if (rv != SECSuccess) {
271 NOTREACHED(); 261 NOTREACHED();
272 return NULL; 262 return NULL;
273 } 263 }
274 264
275 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); 265 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_);
276 if (!result->public_key_) { 266 if (!result->public_key_) {
277 NOTREACHED(); 267 NOTREACHED();
278 return NULL; 268 return NULL;
279 } 269 }
280 270
281 return result.release(); 271 return result.release();
282 } 272 }
283 273
284 } // namespace crypto 274 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698