| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/crypto/rsa_private_key.h" | 5 #include "base/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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 output->assign(item.data, item.data + item.len); | 35 output->assign(item.data, item.data + item.len); |
| 36 SECITEM_FreeItem(&item, PR_FALSE); | 36 SECITEM_FreeItem(&item, PR_FALSE); |
| 37 return true; | 37 return true; |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 namespace base { | 42 namespace base { |
| 43 | 43 |
| 44 // static | 44 RSAPrivateKey::~RSAPrivateKey() { |
| 45 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, | 45 if (key_) |
| 46 bool permanent, | 46 SECKEY_DestroyPrivateKey(key_); |
| 47 bool sensitive) { | 47 if (public_key_) |
| 48 base::EnsureNSSInit(); | 48 SECKEY_DestroyPublicKey(public_key_); |
| 49 | |
| 50 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | |
| 51 | |
| 52 PK11SlotInfo *slot = GetDefaultNSSKeySlot(); | |
| 53 if (!slot) | |
| 54 return NULL; | |
| 55 | |
| 56 PK11RSAGenParams param; | |
| 57 param.keySizeInBits = num_bits; | |
| 58 param.pe = 65537L; | |
| 59 result->key_ = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN, ¶m, | |
| 60 &result->public_key_, permanent, sensitive, NULL); | |
| 61 PK11_FreeSlot(slot); | |
| 62 if (!result->key_) | |
| 63 return NULL; | |
| 64 | |
| 65 return result.release(); | |
| 66 } | 49 } |
| 67 | 50 |
| 68 // static | 51 // static |
| 69 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 52 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
| 70 return CreateWithParams(num_bits, | 53 return CreateWithParams(num_bits, |
| 71 PR_FALSE /* not permanent */, | 54 PR_FALSE /* not permanent */, |
| 72 PR_FALSE /* not sensitive */); | 55 PR_FALSE /* not sensitive */); |
| 73 } | 56 } |
| 74 | 57 |
| 75 // static | 58 // static |
| 76 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | 59 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
| 77 return CreateWithParams(num_bits, | 60 return CreateWithParams(num_bits, |
| 78 PR_TRUE /* permanent */, | 61 PR_TRUE /* permanent */, |
| 79 PR_TRUE /* sensitive */); | 62 PR_TRUE /* sensitive */); |
| 80 } | 63 } |
| 81 | 64 |
| 82 // static | 65 // static |
| 83 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( | |
| 84 const std::vector<uint8>& input, bool permanent, bool sensitive) { | |
| 85 // This method currently leaks some memory. | |
| 86 // See http://crbug.com/34742. | |
| 87 ANNOTATE_SCOPED_MEMORY_LEAK; | |
| 88 base::EnsureNSSInit(); | |
| 89 | |
| 90 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | |
| 91 | |
| 92 PK11SlotInfo *slot = GetDefaultNSSKeySlot(); | |
| 93 if (!slot) | |
| 94 return NULL; | |
| 95 | |
| 96 SECItem der_private_key_info; | |
| 97 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); | |
| 98 der_private_key_info.len = input.size(); | |
| 99 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(slot, | |
| 100 &der_private_key_info, NULL, NULL, permanent, sensitive, | |
| 101 KU_DIGITAL_SIGNATURE, &result->key_, NULL); | |
| 102 PK11_FreeSlot(slot); | |
| 103 if (rv != SECSuccess) { | |
| 104 NOTREACHED(); | |
| 105 return NULL; | |
| 106 } | |
| 107 | |
| 108 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | |
| 109 if (!result->public_key_) { | |
| 110 NOTREACHED(); | |
| 111 return NULL; | |
| 112 } | |
| 113 | |
| 114 return result.release(); | |
| 115 } | |
| 116 | |
| 117 // static | |
| 118 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( | 66 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfo( |
| 119 const std::vector<uint8>& input) { | 67 const std::vector<uint8>& input) { |
| 120 return CreateFromPrivateKeyInfoWithParams(input, | 68 return CreateFromPrivateKeyInfoWithParams(input, |
| 121 PR_FALSE /* not permanent */, | 69 PR_FALSE /* not permanent */, |
| 122 PR_FALSE /* not sensitive */); | 70 PR_FALSE /* not sensitive */); |
| 123 } | 71 } |
| 124 | 72 |
| 125 // static | 73 // static |
| 126 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( | 74 RSAPrivateKey* RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo( |
| 127 const std::vector<uint8>& input) { | 75 const std::vector<uint8>& input) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 PK11_FreeSlot(slot); | 134 PK11_FreeSlot(slot); |
| 187 SECITEM_FreeItem(ck_id, PR_TRUE); | 135 SECITEM_FreeItem(ck_id, PR_TRUE); |
| 188 | 136 |
| 189 // If we didn't find it, that's ok. | 137 // If we didn't find it, that's ok. |
| 190 if (!result->key_) | 138 if (!result->key_) |
| 191 return NULL; | 139 return NULL; |
| 192 | 140 |
| 193 return result.release(); | 141 return result.release(); |
| 194 } | 142 } |
| 195 | 143 |
| 196 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { | |
| 197 EnsureNSSInit(); | |
| 198 } | |
| 199 | |
| 200 RSAPrivateKey::~RSAPrivateKey() { | |
| 201 if (key_) | |
| 202 SECKEY_DestroyPrivateKey(key_); | |
| 203 if (public_key_) | |
| 204 SECKEY_DestroyPublicKey(public_key_); | |
| 205 } | |
| 206 | 144 |
| 207 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 145 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
| 208 PrivateKeyInfoCodec private_key_info(true); | 146 PrivateKeyInfoCodec private_key_info(true); |
| 209 | 147 |
| 210 // Manually read the component attributes of the private key and build up | 148 // Manually read the component attributes of the private key and build up |
| 211 // the PrivateKeyInfo. | 149 // the PrivateKeyInfo. |
| 212 if (!ReadAttribute(key_, CKA_MODULUS, private_key_info.modulus()) || | 150 if (!ReadAttribute(key_, CKA_MODULUS, private_key_info.modulus()) || |
| 213 !ReadAttribute(key_, CKA_PUBLIC_EXPONENT, | 151 !ReadAttribute(key_, CKA_PUBLIC_EXPONENT, |
| 214 private_key_info.public_exponent()) || | 152 private_key_info.public_exponent()) || |
| 215 !ReadAttribute(key_, CKA_PRIVATE_EXPONENT, | 153 !ReadAttribute(key_, CKA_PRIVATE_EXPONENT, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 233 return false; | 171 return false; |
| 234 } | 172 } |
| 235 | 173 |
| 236 for (size_t i = 0; i < der_pubkey->len; ++i) | 174 for (size_t i = 0; i < der_pubkey->len; ++i) |
| 237 output->push_back(der_pubkey->data[i]); | 175 output->push_back(der_pubkey->data[i]); |
| 238 | 176 |
| 239 SECITEM_FreeItem(der_pubkey, PR_TRUE); | 177 SECITEM_FreeItem(der_pubkey, PR_TRUE); |
| 240 return true; | 178 return true; |
| 241 } | 179 } |
| 242 | 180 |
| 181 RSAPrivateKey::RSAPrivateKey() : key_(NULL), public_key_(NULL) { |
| 182 EnsureNSSInit(); |
| 183 } |
| 184 |
| 185 // static |
| 186 RSAPrivateKey* RSAPrivateKey::CreateWithParams(uint16 num_bits, |
| 187 bool permanent, |
| 188 bool sensitive) { |
| 189 base::EnsureNSSInit(); |
| 190 |
| 191 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 192 |
| 193 PK11SlotInfo *slot = GetDefaultNSSKeySlot(); |
| 194 if (!slot) |
| 195 return NULL; |
| 196 |
| 197 PK11RSAGenParams param; |
| 198 param.keySizeInBits = num_bits; |
| 199 param.pe = 65537L; |
| 200 result->key_ = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN, ¶m, |
| 201 &result->public_key_, permanent, sensitive, NULL); |
| 202 PK11_FreeSlot(slot); |
| 203 if (!result->key_) |
| 204 return NULL; |
| 205 |
| 206 return result.release(); |
| 207 } |
| 208 |
| 209 // static |
| 210 RSAPrivateKey* RSAPrivateKey::CreateFromPrivateKeyInfoWithParams( |
| 211 const std::vector<uint8>& input, bool permanent, bool sensitive) { |
| 212 // This method currently leaks some memory. |
| 213 // See http://crbug.com/34742. |
| 214 ANNOTATE_SCOPED_MEMORY_LEAK; |
| 215 base::EnsureNSSInit(); |
| 216 |
| 217 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 218 |
| 219 PK11SlotInfo *slot = GetDefaultNSSKeySlot(); |
| 220 if (!slot) |
| 221 return NULL; |
| 222 |
| 223 SECItem der_private_key_info; |
| 224 der_private_key_info.data = const_cast<unsigned char*>(&input.front()); |
| 225 der_private_key_info.len = input.size(); |
| 226 SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(slot, |
| 227 &der_private_key_info, NULL, NULL, permanent, sensitive, |
| 228 KU_DIGITAL_SIGNATURE, &result->key_, NULL); |
| 229 PK11_FreeSlot(slot); |
| 230 if (rv != SECSuccess) { |
| 231 NOTREACHED(); |
| 232 return NULL; |
| 233 } |
| 234 |
| 235 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
| 236 if (!result->public_key_) { |
| 237 NOTREACHED(); |
| 238 return NULL; |
| 239 } |
| 240 |
| 241 return result.release(); |
| 242 } |
| 243 |
| 243 } // namespace base | 244 } // namespace base |
| OLD | NEW |