| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ec_private_key.h" | 5 #include "crypto/ec_private_key.h" |
| 6 | 6 |
| 7 extern "C" { | 7 extern "C" { |
| 8 // Work around NSS missing SEC_BEGIN_PROTOS in secmodt.h. This must come before | 8 // Work around NSS missing SEC_BEGIN_PROTOS in secmodt.h. This must come before |
| 9 // other NSS headers. | 9 // other NSS headers. |
| 10 #include <secmodt.h> | 10 #include <secmodt.h> |
| 11 } | 11 } |
| 12 | 12 |
| 13 #include <cryptohi.h> | 13 #include <cryptohi.h> |
| 14 #include <keyhi.h> | 14 #include <keyhi.h> |
| 15 #include <pk11pub.h> | 15 #include <pk11pub.h> |
| 16 #include <secmod.h> | 16 #include <secmod.h> |
| 17 #include <stddef.h> | 17 #include <stddef.h> |
| 18 #include <stdint.h> | 18 #include <stdint.h> |
| 19 | 19 |
| 20 #include <memory> |
| 21 |
| 20 #include "base/logging.h" | 22 #include "base/logging.h" |
| 21 #include "base/memory/scoped_ptr.h" | |
| 22 #include "crypto/nss_util.h" | 23 #include "crypto/nss_util.h" |
| 23 #include "crypto/nss_util_internal.h" | 24 #include "crypto/nss_util_internal.h" |
| 24 #include "crypto/scoped_nss_types.h" | 25 #include "crypto/scoped_nss_types.h" |
| 25 #include "crypto/third_party/nss/chromium-nss.h" | 26 #include "crypto/third_party/nss/chromium-nss.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 static bool AppendAttribute(SECKEYPrivateKey* key, | 30 static bool AppendAttribute(SECKEYPrivateKey* key, |
| 30 CK_ATTRIBUTE_TYPE type, | 31 CK_ATTRIBUTE_TYPE type, |
| 31 std::vector<uint8_t>* output) { | 32 std::vector<uint8_t>* output) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 54 } | 55 } |
| 55 | 56 |
| 56 // static | 57 // static |
| 57 ECPrivateKey* ECPrivateKey::Create() { | 58 ECPrivateKey* ECPrivateKey::Create() { |
| 58 EnsureNSSInit(); | 59 EnsureNSSInit(); |
| 59 | 60 |
| 60 ScopedPK11Slot slot(PK11_GetInternalSlot()); | 61 ScopedPK11Slot slot(PK11_GetInternalSlot()); |
| 61 if (!slot) | 62 if (!slot) |
| 62 return nullptr; | 63 return nullptr; |
| 63 | 64 |
| 64 scoped_ptr<ECPrivateKey> result(new ECPrivateKey); | 65 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey); |
| 65 | 66 |
| 66 SECOidData* oid_data = SECOID_FindOIDByTag(SEC_OID_SECG_EC_SECP256R1); | 67 SECOidData* oid_data = SECOID_FindOIDByTag(SEC_OID_SECG_EC_SECP256R1); |
| 67 if (!oid_data) { | 68 if (!oid_data) { |
| 68 DLOG(ERROR) << "SECOID_FindOIDByTag: " << PORT_GetError(); | 69 DLOG(ERROR) << "SECOID_FindOIDByTag: " << PORT_GetError(); |
| 69 return nullptr; | 70 return nullptr; |
| 70 } | 71 } |
| 71 | 72 |
| 72 // SECKEYECParams is a SECItem containing the DER encoded ASN.1 ECParameters | 73 // SECKEYECParams is a SECItem containing the DER encoded ASN.1 ECParameters |
| 73 // value. For a named curve, that is just the OBJECT IDENTIFIER of the curve. | 74 // value. For a named curve, that is just the OBJECT IDENTIFIER of the curve. |
| 74 // In addition to the oid data, the encoding requires one byte for the ASN.1 | 75 // In addition to the oid data, the encoding requires one byte for the ASN.1 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 104 ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 105 ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 105 const std::string& password, | 106 const std::string& password, |
| 106 const std::vector<uint8_t>& encrypted_private_key_info, | 107 const std::vector<uint8_t>& encrypted_private_key_info, |
| 107 const std::vector<uint8_t>& subject_public_key_info) { | 108 const std::vector<uint8_t>& subject_public_key_info) { |
| 108 EnsureNSSInit(); | 109 EnsureNSSInit(); |
| 109 | 110 |
| 110 ScopedPK11Slot slot(PK11_GetInternalSlot()); | 111 ScopedPK11Slot slot(PK11_GetInternalSlot()); |
| 111 if (!slot) | 112 if (!slot) |
| 112 return nullptr; | 113 return nullptr; |
| 113 | 114 |
| 114 scoped_ptr<ECPrivateKey> result(new ECPrivateKey); | 115 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey); |
| 115 | 116 |
| 116 SECItem encoded_spki = { | 117 SECItem encoded_spki = { |
| 117 siBuffer, | 118 siBuffer, |
| 118 const_cast<unsigned char*>(&subject_public_key_info[0]), | 119 const_cast<unsigned char*>(&subject_public_key_info[0]), |
| 119 static_cast<unsigned>(subject_public_key_info.size()) | 120 static_cast<unsigned>(subject_public_key_info.size()) |
| 120 }; | 121 }; |
| 121 CERTSubjectPublicKeyInfo* decoded_spki = SECKEY_DecodeDERSubjectPublicKeyInfo( | 122 CERTSubjectPublicKeyInfo* decoded_spki = SECKEY_DecodeDERSubjectPublicKeyInfo( |
| 122 &encoded_spki); | 123 &encoded_spki); |
| 123 if (!decoded_spki) { | 124 if (!decoded_spki) { |
| 124 DLOG(ERROR) << "SECKEY_DecodeDERSubjectPublicKeyInfo: " << PORT_GetError(); | 125 DLOG(ERROR) << "SECKEY_DecodeDERSubjectPublicKeyInfo: " << PORT_GetError(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 << PORT_GetError(); | 218 << PORT_GetError(); |
| 218 SECKEY_DestroyPublicKey(*public_key); | 219 SECKEY_DestroyPublicKey(*public_key); |
| 219 *public_key = NULL; | 220 *public_key = NULL; |
| 220 return false; | 221 return false; |
| 221 } | 222 } |
| 222 | 223 |
| 223 return true; | 224 return true; |
| 224 } | 225 } |
| 225 | 226 |
| 226 ECPrivateKey* ECPrivateKey::Copy() const { | 227 ECPrivateKey* ECPrivateKey::Copy() const { |
| 227 scoped_ptr<ECPrivateKey> copy(new ECPrivateKey); | 228 std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey); |
| 228 if (key_) { | 229 if (key_) { |
| 229 copy->key_ = SECKEY_CopyPrivateKey(key_); | 230 copy->key_ = SECKEY_CopyPrivateKey(key_); |
| 230 if (!copy->key_) | 231 if (!copy->key_) |
| 231 return NULL; | 232 return NULL; |
| 232 } | 233 } |
| 233 if (public_key_) { | 234 if (public_key_) { |
| 234 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 235 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
| 235 if (!copy->public_key_) | 236 if (!copy->public_key_) |
| 236 return NULL; | 237 return NULL; |
| 237 } | 238 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // parameters. | 317 // parameters. |
| 317 output->clear(); | 318 output->clear(); |
| 318 return AppendAttribute(key_, CKA_VALUE, output) && | 319 return AppendAttribute(key_, CKA_VALUE, output) && |
| 319 output->size() == 32 && | 320 output->size() == 32 && |
| 320 AppendAttribute(key_, CKA_EC_PARAMS, output); | 321 AppendAttribute(key_, CKA_EC_PARAMS, output); |
| 321 } | 322 } |
| 322 | 323 |
| 323 ECPrivateKey::ECPrivateKey() : key_(NULL), public_key_(NULL) {} | 324 ECPrivateKey::ECPrivateKey() : key_(NULL), public_key_(NULL) {} |
| 324 | 325 |
| 325 } // namespace crypto | 326 } // namespace crypto |
| OLD | NEW |