| 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> |
| 11 | 11 |
| 12 #include <list> | 12 #include <list> |
| 13 | 13 |
| 14 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
| 19 #include "crypto/nss_util_internal.h" | 19 #include "crypto/nss_util_internal.h" |
| 20 #include "crypto/scoped_nss_types.h" | 20 #include "crypto/scoped_nss_types.h" |
| 21 | 21 |
| 22 // TODO(rafaelw): Consider refactoring common functions and definitions from | 22 // TODO(rafaelw): Consider using NSS's ASN.1 encoder. |
| 23 // rsa_private_key_win.cc or using NSS's ASN.1 encoder. | |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 static bool ReadAttribute(SECKEYPrivateKey* key, | 25 static bool ReadAttribute(SECKEYPrivateKey* key, |
| 27 CK_ATTRIBUTE_TYPE type, | 26 CK_ATTRIBUTE_TYPE type, |
| 28 std::vector<uint8>* output) { | 27 std::vector<uint8>* output) { |
| 29 SECItem item; | 28 SECItem item; |
| 30 SECStatus rv; | 29 SECStatus rv; |
| 31 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); | 30 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); |
| 32 if (rv != SECSuccess) { | 31 if (rv != SECSuccess) { |
| 33 NOTREACHED(); | 32 NOTREACHED(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 RSAPrivateKey::~RSAPrivateKey() { | 45 RSAPrivateKey::~RSAPrivateKey() { |
| 47 if (key_) | 46 if (key_) |
| 48 SECKEY_DestroyPrivateKey(key_); | 47 SECKEY_DestroyPrivateKey(key_); |
| 49 if (public_key_) | 48 if (public_key_) |
| 50 SECKEY_DestroyPublicKey(public_key_); | 49 SECKEY_DestroyPublicKey(public_key_); |
| 51 } | 50 } |
| 52 | 51 |
| 53 // static | 52 // static |
| 54 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 53 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
| 55 return CreateWithParams(num_bits, | 54 return CreateWithParams(num_bits, |
| 56 PR_FALSE /* not permanent */, | 55 false /* not permanent */, |
| 57 PR_FALSE /* not sensitive */); | 56 false /* not sensitive */); |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | |
| 62 return CreateWithParams(num_bits, | |
| 63 PR_TRUE /* permanent */, | |
| 64 PR_TRUE /* sensitive */); | |
| 65 } | 57 } |
| 66 | 58 |
| 67 // static | 59 // static |
| 68 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 60 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 69 const std::vector<uint8>& input) { | 61 const std::vector<uint8>& input) { |
| 70 return CreateFromPrivateKeyInfoWithParams(input, | 62 return CreateFromPrivateKeyInfoWithParams(input, |
| 71 PR_FALSE /* not permanent */, | 63 false /* not permanent */, |
| 72 PR_FALSE /* not sensitive */); | 64 false /* not sensitive */); |
| 65 } |
| 66 |
| 67 #if defined(USE_NSS) |
| 68 // static |
| 69 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
| 70 return CreateWithParams(num_bits, |
| 71 true /* permanent */, |
| 72 true /* sensitive */); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // static | 75 // static |
| 76 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | 76 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
| 77 const std::vector<uint8>& input) { | 77 const std::vector<uint8>& input) { |
| 78 return CreateFromPrivateKeyInfoWithParams(input, | 78 return CreateFromPrivateKeyInfoWithParams(input, |
| 79 PR_TRUE /* permanent */, | 79 true /* permanent */, |
| 80 PR_TRUE /* sensitive */); | 80 true /* sensitive */); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // static | 83 // static |
| 84 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) { | 84 RSAPrivateKey* RSAPrivateKey::CreateFromKey(SECKEYPrivateKey* key) { |
| 85 DCHECK(key); | 85 DCHECK(key); |
| 86 if (SECKEY_GetPrivateKeyType(key) != rsaKey) | 86 if (SECKEY_GetPrivateKeyType(key) != rsaKey) |
| 87 return NULL; | 87 return NULL; |
| 88 RSAPrivateKey* copy = new RSAPrivateKey(); | 88 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 89 copy->key_ = SECKEY_CopyPrivateKey(key); | 89 copy->key_ = SECKEY_CopyPrivateKey(key); |
| 90 copy->public_key_ = SECKEY_ConvertToPublicKey(key); | 90 copy->public_key_ = SECKEY_ConvertToPublicKey(key); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], | 146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
| 147 ck_id.get(), NULL); | 147 ck_id.get(), NULL); |
| 148 if (result->key_) | 148 if (result->key_) |
| 149 return result.release(); | 149 return result.release(); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 // We didn't find the key. | 153 // We didn't find the key. |
| 154 return NULL; | 154 return NULL; |
| 155 } | 155 } |
| 156 #endif |
| 156 | 157 |
| 157 RSAPrivateKey* RSAPrivateKey::Copy() const { | 158 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 158 RSAPrivateKey* copy = new RSAPrivateKey(); | 159 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 159 copy->key_ = SECKEY_CopyPrivateKey(key_); | 160 copy->key_ = SECKEY_CopyPrivateKey(key_); |
| 160 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 161 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
| 161 return copy; | 162 return copy; |
| 162 } | 163 } |
| 163 | 164 |
| 164 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 165 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
| 165 PrivateKeyInfoCodec private_key_info(true); | 166 PrivateKeyInfoCodec private_key_info(true); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 195 } | 196 } |
| 196 | 197 |
| 197 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { | 198 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { |
| 198 EnsureNSSInit(); | 199 EnsureNSSInit(); |
| 199 } | 200 } |
| 200 | 201 |
| 201 // static | 202 // static |
| 202 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, | 203 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, |
| 203 bool permanent, | 204 bool permanent, |
| 204 bool sensitive) { | 205 bool sensitive) { |
| 206 #if !defined(USE_NSS) |
| 207 if (permanent) { |
| 208 NOTIMPLEMENTED(); |
| 209 return NULL; |
| 210 } |
| 211 #endif |
| 212 |
| 205 EnsureNSSInit(); | 213 EnsureNSSInit(); |
| 206 | 214 |
| 207 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 215 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 208 | 216 |
| 209 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : | 217 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : |
| 210 PK11_GetInternalSlot()); | 218 PK11_GetInternalSlot()); |
| 211 if (!slot.get()) | 219 if (!slot.get()) |
| 212 return NULL; | 220 return NULL; |
| 213 | 221 |
| 214 PK11RSAGenParams param; | 222 PK11RSAGenParams param; |
| 215 param.keySizeInBits = num_bits; | 223 param.keySizeInBits = num_bits; |
| 216 param.pe = 65537L; | 224 param.pe = 65537L; |
| 217 result->key_ = PK11_GenerateKeyPair(slot.get(), | 225 result->key_ = PK11_GenerateKeyPair(slot.get(), |
| 218 CKM_RSA_PKCS_KEY_PAIR_GEN, | 226 CKM_RSA_PKCS_KEY_PAIR_GEN, |
| 219 ¶m, | 227 ¶m, |
| 220 &result->public_key_, | 228 &result->public_key_, |
| 221 permanent, | 229 permanent, |
| 222 sensitive, | 230 sensitive, |
| 223 NULL); | 231 NULL); |
| 224 if (!result->key_) | 232 if (!result->key_) |
| 225 return NULL; | 233 return NULL; |
| 226 | 234 |
| 227 return result.release(); | 235 return result.release(); |
| 228 } | 236 } |
| 229 | 237 |
| 230 // static | 238 // static |
| 231 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( | 239 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( |
| 232 const std::vector<uint8>& input, bool permanent, bool sensitive) { | 240 const std::vector<uint8>& input, bool permanent, bool sensitive) { |
| 241 #if !defined(USE_NSS) |
| 242 if (permanent) { |
| 243 NOTIMPLEMENTED(); |
| 244 return NULL; |
| 245 } |
| 246 #endif |
| 247 |
| 233 // This method currently leaks some memory. | 248 // This method currently leaks some memory. |
| 234 // See http://crbug.com/34742. | 249 // See http://crbug.com/34742. |
| 235 ANNOTATE_SCOPED_MEMORY_LEAK; | 250 ANNOTATE_SCOPED_MEMORY_LEAK; |
| 236 EnsureNSSInit(); | 251 EnsureNSSInit(); |
| 237 | 252 |
| 238 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 253 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 239 | 254 |
| 240 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : | 255 ScopedPK11Slot slot(permanent ? GetPrivateNSSKeySlot() : |
| 241 PK11_GetInternalSlot()); | 256 PK11_GetInternalSlot()); |
| 242 if (!slot.get()) | 257 if (!slot.get()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 260 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 275 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
| 261 if (!result->public_key_) { | 276 if (!result->public_key_) { |
| 262 NOTREACHED(); | 277 NOTREACHED(); |
| 263 return NULL; | 278 return NULL; |
| 264 } | 279 } |
| 265 | 280 |
| 266 return result.release(); | 281 return result.release(); |
| 267 } | 282 } |
| 268 | 283 |
| 269 } // namespace crypto | 284 } // namespace crypto |
| OLD | NEW |