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 | 11 |
11 #include <list> | 12 #include <list> |
12 | 13 |
13 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
17 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
18 #include "crypto/nss_util_internal.h" | 19 #include "crypto/nss_util_internal.h" |
19 #include "crypto/scoped_nss_types.h" | 20 #include "crypto/scoped_nss_types.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 return NULL; | 113 return NULL; |
113 } | 114 } |
114 | 115 |
115 ScopedSECItem ck_id( | 116 ScopedSECItem ck_id( |
116 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); | 117 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); |
117 if (!ck_id.get()) { | 118 if (!ck_id.get()) { |
118 NOTREACHED(); | 119 NOTREACHED(); |
119 return NULL; | 120 return NULL; |
120 } | 121 } |
121 | 122 |
122 ScopedPK11Slot slot(GetPrivateNSSKeySlot()); | 123 // Search all slots in all modules for the key with the given ID. |
123 if (!slot.get()) { | 124 AutoSECMODListReadLock auto_lock; |
124 NOTREACHED(); | 125 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); |
125 return NULL; | 126 for (SECMODModuleList* item = head; item != NULL; item = item->next) { |
| 127 int slot_count = item->module->loaded ? item->module->slotCount : 0; |
| 128 for (int i = 0; i < slot_count; i++) { |
| 129 // Finally...Look for the key! |
| 130 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
| 131 ck_id.get(), NULL); |
| 132 if (result->key_) |
| 133 return result.release(); |
| 134 } |
126 } | 135 } |
127 | 136 |
128 // Finally...Look for the key! | 137 // We didn't find the key. |
129 result->key_ = PK11_FindKeyByKeyID(slot.get(), ck_id.get(), NULL); | 138 return NULL; |
130 | |
131 // If we don't find the matching key in the private slot, then we | |
132 // look in the public slot. | |
133 if (!result->key_) { | |
134 slot.reset(GetPublicNSSKeySlot()); | |
135 if (!slot.get()) { | |
136 NOTREACHED(); | |
137 return NULL; | |
138 } | |
139 result->key_ = PK11_FindKeyByKeyID(slot.get(), ck_id.get(), NULL); | |
140 } | |
141 | |
142 // If we didn't find it, that's ok. | |
143 if (!result->key_) | |
144 return NULL; | |
145 | |
146 return result.release(); | |
147 } | 139 } |
148 | 140 |
149 | 141 |
150 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 142 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
151 PrivateKeyInfoCodec private_key_info(true); | 143 PrivateKeyInfoCodec private_key_info(true); |
152 | 144 |
153 // Manually read the component attributes of the private key and build up | 145 // Manually read the component attributes of the private key and build up |
154 // the PrivateKeyInfo. | 146 // the PrivateKeyInfo. |
155 if (!ReadAttribute(key_, CKA_MODULUS, private_key_info.modulus()) || | 147 if (!ReadAttribute(key_, CKA_MODULUS, private_key_info.modulus()) || |
156 !ReadAttribute(key_, CKA_PUBLIC_EXPONENT, | 148 !ReadAttribute(key_, CKA_PUBLIC_EXPONENT, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 238 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
247 if (!result->public_key_) { | 239 if (!result->public_key_) { |
248 NOTREACHED(); | 240 NOTREACHED(); |
249 return NULL; | 241 return NULL; |
250 } | 242 } |
251 | 243 |
252 return result.release(); | 244 return result.release(); |
253 } | 245 } |
254 | 246 |
255 } // namespace crypto | 247 } // namespace crypto |
OLD | NEW |