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 20 matching lines...) Expand all Loading... | |
31 if (rv != SECSuccess) { | 31 if (rv != SECSuccess) { |
32 NOTREACHED(); | 32 NOTREACHED(); |
33 return false; | 33 return false; |
34 } | 34 } |
35 | 35 |
36 output->assign(item.data, item.data + item.len); | 36 output->assign(item.data, item.data + item.len); |
37 SECITEM_FreeItem(&item, PR_FALSE); | 37 SECITEM_FreeItem(&item, PR_FALSE); |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 #if defined(USE_NSS) | |
42 struct PublicKeyInfoDeleter { | |
43 inline void operator()(CERTSubjectPublicKeyInfo* spki) { | |
44 SECKEY_DestroySubjectPublicKeyInfo(spki); | |
45 } | |
46 }; | |
47 | |
48 typedef scoped_ptr<CERTSubjectPublicKeyInfo, PublicKeyInfoDeleter> | |
49 ScopedPublicKeyInfo; | |
50 | |
51 // The function decodes RSA public key from the |input|. The caller | |
52 // takes ownership of the returned value. | |
wtc
2014/05/19 17:23:54
Nit: I think it is OK to not document the return v
ygorshenin1
2014/05/20 07:53:50
Done.
wtc
2014/05/20 16:46:39
Nit: I think we can keep the "The function decodes
ygorshenin1
2014/05/20 17:01:34
Done.
| |
53 crypto::ScopedSECKEYPublicKey GetRSAPublicKey(const std::vector<uint8>& input) { | |
54 // First, decode and save the public key. | |
55 SECItem key_der; | |
56 key_der.type = siBuffer; | |
57 key_der.data = const_cast<unsigned char*>(&input[0]); | |
58 key_der.len = input.size(); | |
59 | |
60 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); | |
61 if (!spki) | |
62 return crypto::ScopedSECKEYPublicKey(); | |
63 | |
64 crypto::ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); | |
65 | |
66 // Make sure the key is an RSA key.. If not, that's an error. | |
67 if (!result || result->keyType != rsaKey) | |
68 return crypto::ScopedSECKEYPublicKey(); | |
69 return result.Pass(); | |
70 } | |
71 #endif // defined(USE_NSS) | |
72 | |
41 } // namespace | 73 } // namespace |
42 | 74 |
43 namespace crypto { | 75 namespace crypto { |
44 | 76 |
45 RSAPrivateKey::~RSAPrivateKey() { | 77 RSAPrivateKey::~RSAPrivateKey() { |
46 if (key_) | 78 if (key_) |
47 SECKEY_DestroyPrivateKey(key_); | 79 SECKEY_DestroyPrivateKey(key_); |
48 if (public_key_) | 80 if (public_key_) |
49 SECKEY_DestroyPublicKey(public_key_); | 81 SECKEY_DestroyPublicKey(public_key_); |
50 } | 82 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 NOTREACHED(); | 137 NOTREACHED(); |
106 delete copy; | 138 delete copy; |
107 return NULL; | 139 return NULL; |
108 } | 140 } |
109 return copy; | 141 return copy; |
110 } | 142 } |
111 | 143 |
112 // static | 144 // static |
113 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( | 145 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( |
114 const std::vector<uint8>& input) { | 146 const std::vector<uint8>& input) { |
115 EnsureNSSInit(); | 147 scoped_ptr<RSAPrivateKey> result(InitPublicPart(input)); |
116 | 148 if (!result) |
117 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | |
118 | |
119 // First, decode and save the public key. | |
120 SECItem key_der; | |
121 key_der.type = siBuffer; | |
122 key_der.data = const_cast<unsigned char*>(&input[0]); | |
123 key_der.len = input.size(); | |
124 | |
125 CERTSubjectPublicKeyInfo* spki = | |
126 SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der); | |
127 if (!spki) { | |
128 NOTREACHED(); | |
129 return NULL; | 149 return NULL; |
130 } | |
131 | |
132 result->public_key_ = SECKEY_ExtractPublicKey(spki); | |
133 SECKEY_DestroySubjectPublicKeyInfo(spki); | |
134 if (!result->public_key_) { | |
135 NOTREACHED(); | |
136 return NULL; | |
137 } | |
138 | |
139 // Make sure the key is an RSA key. If not, that's an error | |
140 if (result->public_key_->keyType != rsaKey) { | |
141 NOTREACHED(); | |
142 return NULL; | |
143 } | |
144 | 150 |
145 ScopedSECItem ck_id( | 151 ScopedSECItem ck_id( |
146 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); | 152 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); |
147 if (!ck_id.get()) { | 153 if (!ck_id.get()) { |
148 NOTREACHED(); | 154 NOTREACHED(); |
149 return NULL; | 155 return NULL; |
150 } | 156 } |
151 | 157 |
152 // Search all slots in all modules for the key with the given ID. | 158 // Search all slots in all modules for the key with the given ID. |
153 AutoSECMODListReadLock auto_lock; | 159 AutoSECMODListReadLock auto_lock; |
154 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); | 160 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); |
155 for (SECMODModuleList* item = head; item != NULL; item = item->next) { | 161 for (SECMODModuleList* item = head; item != NULL; item = item->next) { |
156 int slot_count = item->module->loaded ? item->module->slotCount : 0; | 162 int slot_count = item->module->loaded ? item->module->slotCount : 0; |
157 for (int i = 0; i < slot_count; i++) { | 163 for (int i = 0; i < slot_count; i++) { |
158 // Finally...Look for the key! | 164 // Finally...Look for the key! |
159 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], | 165 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
160 ck_id.get(), NULL); | 166 ck_id.get(), NULL); |
161 if (result->key_) | 167 if (result->key_) |
162 return result.release(); | 168 return result.release(); |
163 } | 169 } |
164 } | 170 } |
165 | 171 |
166 // We didn't find the key. | 172 // We didn't find the key. |
167 return NULL; | 173 return NULL; |
168 } | 174 } |
175 | |
176 // static | |
177 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfoInSlot( | |
178 const std::vector<uint8>& input, | |
179 PK11SlotInfo* slot) { | |
180 if (!slot) | |
181 return NULL; | |
182 | |
183 scoped_ptr<RSAPrivateKey> result(InitPublicPart(input)); | |
184 if (!result) | |
185 return NULL; | |
186 | |
187 ScopedSECItem ck_id( | |
188 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); | |
189 if (!ck_id.get()) { | |
190 NOTREACHED(); | |
191 return NULL; | |
192 } | |
193 | |
194 result->key_ = PK11_FindKeyByKeyID(slot, ck_id.get(), NULL); | |
195 if (!result->key_) | |
196 return NULL; | |
197 return result.release(); | |
198 } | |
169 #endif | 199 #endif |
170 | 200 |
171 RSAPrivateKey* RSAPrivateKey::Copy() const { | 201 RSAPrivateKey* RSAPrivateKey::Copy() const { |
172 RSAPrivateKey* copy = new RSAPrivateKey(); | 202 RSAPrivateKey* copy = new RSAPrivateKey(); |
173 copy->key_ = SECKEY_CopyPrivateKey(key_); | 203 copy->key_ = SECKEY_CopyPrivateKey(key_); |
174 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 204 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
175 return copy; | 205 return copy; |
176 } | 206 } |
177 | 207 |
178 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 208 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 | 296 |
267 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 297 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
268 if (!result->public_key_) { | 298 if (!result->public_key_) { |
269 NOTREACHED(); | 299 NOTREACHED(); |
270 return NULL; | 300 return NULL; |
271 } | 301 } |
272 | 302 |
273 return result.release(); | 303 return result.release(); |
274 } | 304 } |
275 | 305 |
306 #if defined(USE_NSS) | |
307 // static | |
308 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { | |
309 EnsureNSSInit(); | |
310 | |
311 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); | |
312 result->public_key_ = GetRSAPublicKey(input).release(); | |
313 if (!result->public_key_) { | |
314 NOTREACHED(); | |
315 return NULL; | |
316 } | |
317 | |
318 return result.release(); | |
319 } | |
320 #endif // defined(USE_NSS) | |
321 | |
276 } // namespace crypto | 322 } // namespace crypto |
OLD | NEW |