| 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|. |
| 52 crypto::ScopedSECKEYPublicKey GetRSAPublicKey(const std::vector<uint8>& input) { |
| 53 // First, decode and save the public key. |
| 54 SECItem key_der; |
| 55 key_der.type = siBuffer; |
| 56 key_der.data = const_cast<unsigned char*>(&input[0]); |
| 57 key_der.len = input.size(); |
| 58 |
| 59 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); |
| 60 if (!spki) |
| 61 return crypto::ScopedSECKEYPublicKey(); |
| 62 |
| 63 crypto::ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); |
| 64 |
| 65 // Make sure the key is an RSA key.. If not, that's an error. |
| 66 if (!result || result->keyType != rsaKey) |
| 67 return crypto::ScopedSECKEYPublicKey(); |
| 68 return result.Pass(); |
| 69 } |
| 70 #endif // defined(USE_NSS) |
| 71 |
| 41 } // namespace | 72 } // namespace |
| 42 | 73 |
| 43 namespace crypto { | 74 namespace crypto { |
| 44 | 75 |
| 45 RSAPrivateKey::~RSAPrivateKey() { | 76 RSAPrivateKey::~RSAPrivateKey() { |
| 46 if (key_) | 77 if (key_) |
| 47 SECKEY_DestroyPrivateKey(key_); | 78 SECKEY_DestroyPrivateKey(key_); |
| 48 if (public_key_) | 79 if (public_key_) |
| 49 SECKEY_DestroyPublicKey(public_key_); | 80 SECKEY_DestroyPublicKey(public_key_); |
| 50 } | 81 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 NOTREACHED(); | 136 NOTREACHED(); |
| 106 delete copy; | 137 delete copy; |
| 107 return NULL; | 138 return NULL; |
| 108 } | 139 } |
| 109 return copy; | 140 return copy; |
| 110 } | 141 } |
| 111 | 142 |
| 112 // static | 143 // static |
| 113 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( | 144 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( |
| 114 const std::vector<uint8>& input) { | 145 const std::vector<uint8>& input) { |
| 115 EnsureNSSInit(); | 146 scoped_ptr<RSAPrivateKey> result(InitPublicPart(input)); |
| 116 | 147 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; | 148 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 | 149 |
| 145 ScopedSECItem ck_id( | 150 ScopedSECItem ck_id( |
| 146 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); | 151 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); |
| 147 if (!ck_id.get()) { | 152 if (!ck_id.get()) { |
| 148 NOTREACHED(); | 153 NOTREACHED(); |
| 149 return NULL; | 154 return NULL; |
| 150 } | 155 } |
| 151 | 156 |
| 152 // Search all slots in all modules for the key with the given ID. | 157 // Search all slots in all modules for the key with the given ID. |
| 153 AutoSECMODListReadLock auto_lock; | 158 AutoSECMODListReadLock auto_lock; |
| 154 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); | 159 SECMODModuleList* head = SECMOD_GetDefaultModuleList(); |
| 155 for (SECMODModuleList* item = head; item != NULL; item = item->next) { | 160 for (SECMODModuleList* item = head; item != NULL; item = item->next) { |
| 156 int slot_count = item->module->loaded ? item->module->slotCount : 0; | 161 int slot_count = item->module->loaded ? item->module->slotCount : 0; |
| 157 for (int i = 0; i < slot_count; i++) { | 162 for (int i = 0; i < slot_count; i++) { |
| 158 // Finally...Look for the key! | 163 // Finally...Look for the key! |
| 159 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], | 164 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
| 160 ck_id.get(), NULL); | 165 ck_id.get(), NULL); |
| 161 if (result->key_) | 166 if (result->key_) |
| 162 return result.release(); | 167 return result.release(); |
| 163 } | 168 } |
| 164 } | 169 } |
| 165 | 170 |
| 166 // We didn't find the key. | 171 // We didn't find the key. |
| 167 return NULL; | 172 return NULL; |
| 168 } | 173 } |
| 174 |
| 175 // static |
| 176 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfoInSlot( |
| 177 const std::vector<uint8>& input, |
| 178 PK11SlotInfo* slot) { |
| 179 if (!slot) |
| 180 return NULL; |
| 181 |
| 182 scoped_ptr<RSAPrivateKey> result(InitPublicPart(input)); |
| 183 if (!result) |
| 184 return NULL; |
| 185 |
| 186 ScopedSECItem ck_id( |
| 187 PK11_MakeIDFromPubKey(&(result->public_key_->u.rsa.modulus))); |
| 188 if (!ck_id.get()) { |
| 189 NOTREACHED(); |
| 190 return NULL; |
| 191 } |
| 192 |
| 193 result->key_ = PK11_FindKeyByKeyID(slot, ck_id.get(), NULL); |
| 194 if (!result->key_) |
| 195 return NULL; |
| 196 return result.release(); |
| 197 } |
| 169 #endif | 198 #endif |
| 170 | 199 |
| 171 RSAPrivateKey* RSAPrivateKey::Copy() const { | 200 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 172 RSAPrivateKey* copy = new RSAPrivateKey(); | 201 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 173 copy->key_ = SECKEY_CopyPrivateKey(key_); | 202 copy->key_ = SECKEY_CopyPrivateKey(key_); |
| 174 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 203 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
| 175 return copy; | 204 return copy; |
| 176 } | 205 } |
| 177 | 206 |
| 178 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 207 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 295 |
| 267 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 296 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
| 268 if (!result->public_key_) { | 297 if (!result->public_key_) { |
| 269 NOTREACHED(); | 298 NOTREACHED(); |
| 270 return NULL; | 299 return NULL; |
| 271 } | 300 } |
| 272 | 301 |
| 273 return result.release(); | 302 return result.release(); |
| 274 } | 303 } |
| 275 | 304 |
| 305 #if defined(USE_NSS) |
| 306 // static |
| 307 RSAPrivateKey* RSAPrivateKey::InitPublicPart(const std::vector<uint8>& input) { |
| 308 EnsureNSSInit(); |
| 309 |
| 310 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey()); |
| 311 result->public_key_ = GetRSAPublicKey(input).release(); |
| 312 if (!result->public_key_) { |
| 313 NOTREACHED(); |
| 314 return NULL; |
| 315 } |
| 316 |
| 317 return result.release(); |
| 318 } |
| 319 #endif // defined(USE_NSS) |
| 320 |
| 276 } // namespace crypto | 321 } // namespace crypto |
| OLD | NEW |