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

Side by Side Diff: crypto/rsa_private_key_nss.cc

Issue 7066032: Fixing FindFromPublicKeyInfo so that it searches the "Public" NSS database (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 months 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
« no previous file with comments | « crypto/nss_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 EnsureNSSInit(); 84 EnsureNSSInit();
85 85
86 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); 86 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey);
87 87
88 // First, decode and save the public key. 88 // First, decode and save the public key.
89 SECItem key_der; 89 SECItem key_der;
90 key_der.type = siBuffer; 90 key_der.type = siBuffer;
91 key_der.data = const_cast<unsigned char*>(&input[0]); 91 key_der.data = const_cast<unsigned char*>(&input[0]);
92 key_der.len = input.size(); 92 key_der.len = input.size();
93 93
94 CERTSubjectPublicKeyInfo *spki = 94 CERTSubjectPublicKeyInfo *spki =
Ryan Sleevi 2011/05/25 05:58:25 nit: CERTSubjectPublicKeyInfo *spki -> CERTSubject
Greg Spencer (Chromium) 2011/05/25 17:12:07 Done.
95 SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der); 95 SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der);
96 if (!spki) { 96 if (!spki) {
97 NOTREACHED(); 97 NOTREACHED();
98 return NULL; 98 return NULL;
99 } 99 }
100 100
101 result->public_key_ = SECKEY_ExtractPublicKey(spki); 101 result->public_key_ = SECKEY_ExtractPublicKey(spki);
102 SECKEY_DestroySubjectPublicKeyInfo(spki); 102 SECKEY_DestroySubjectPublicKeyInfo(spki);
103 if (!result->public_key_) { 103 if (!result->public_key_) {
104 NOTREACHED(); 104 NOTREACHED();
105 return NULL; 105 return NULL;
106 } 106 }
107 107
108 // Now, look for the associated private key in the user's
109 // hardware-backed NSS DB. If it's not there, consider that an
110 // error.
111 PK11SlotInfo *slot = GetPrivateNSSKeySlot();
112 if (!slot) {
113 NOTREACHED();
114 return NULL;
115 }
116
117 // Make sure the key is an RSA key. If not, that's an error 108 // Make sure the key is an RSA key. If not, that's an error
118 if (result->public_key_->keyType != rsaKey) { 109 if (result->public_key_->keyType != rsaKey) {
119 PK11_FreeSlot(slot);
120 NOTREACHED(); 110 NOTREACHED();
121 return NULL; 111 return NULL;
122 } 112 }
123 113
124 SECItem *ck_id = PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus)); 114 SECItem *ck_id = PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus));
Ryan Sleevi 2011/05/25 05:58:25 You can make this a ScopedSECItem (see src/crypto/
Greg Spencer (Chromium) 2011/05/25 17:12:07 Good point. I forgot about the scoped nss types.
125 if (!ck_id) { 115 if (!ck_id) {
126 PK11_FreeSlot(slot);
127 NOTREACHED(); 116 NOTREACHED();
128 return NULL; 117 return NULL;
129 } 118 }
130 119
120 PK11SlotInfo* slot = GetPrivateNSSKeySlot();
121 if (!slot) {
122 NOTREACHED();
123 SECITEM_FreeItem(ck_id, PR_TRUE);
124 return NULL;
125 }
126
131 // Finally...Look for the key! 127 // Finally...Look for the key!
132 result->key_ = PK11_FindKeyByKeyID(slot, ck_id, NULL); 128 result->key_ = PK11_FindKeyByKeyID(slot, ck_id, NULL);
133 129
130 // If we don't find the matching key in the private slot, then we
131 // look in the public slot.
132 if (!result->key_) {
133 PK11_FreeSlot(slot);
134 slot = GetPublicNSSKeySlot();
Ryan Sleevi 2011/05/25 05:58:25 According to the header comments for GetPublicNSSK
Greg Spencer (Chromium) 2011/05/25 17:12:07 Yes, I wrote those comments. The general policy f
135 if (!slot) {
136 NOTREACHED();
137 SECITEM_FreeItem(ck_id, PR_TRUE);
138 return NULL;
139 }
140 result->key_ = PK11_FindKeyByKeyID(slot, ck_id, NULL);
141 }
142
134 // Cleanup... 143 // Cleanup...
135 PK11_FreeSlot(slot); 144 PK11_FreeSlot(slot);
136 SECITEM_FreeItem(ck_id, PR_TRUE); 145 SECITEM_FreeItem(ck_id, PR_TRUE);
137 146
138 // If we didn't find it, that's ok. 147 // If we didn't find it, that's ok.
139 if (!result->key_) 148 if (!result->key_)
140 return NULL; 149 return NULL;
141 150
142 return result.release(); 151 return result.release();
143 } 152 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); 249 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_);
241 if (!result->public_key_) { 250 if (!result->public_key_) {
242 NOTREACHED(); 251 NOTREACHED();
243 return NULL; 252 return NULL;
244 } 253 }
245 254
246 return result.release(); 255 return result.release();
247 } 256 }
248 257
249 } // namespace crypto 258 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/nss_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698