| 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 #include <openssl/bytestring.h> | 7 #include <openssl/bytestring.h> |
| 8 #include <openssl/ec.h> | 8 #include <openssl/ec.h> |
| 9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
| 10 #include <openssl/mem.h> | 10 #include <openssl/mem.h> |
| 11 #include <openssl/pkcs12.h> | 11 #include <openssl/pkcs12.h> |
| 12 #include <openssl/x509.h> | 12 #include <openssl/x509.h> |
| 13 #include <stddef.h> | 13 #include <stddef.h> |
| 14 #include <stdint.h> | 14 #include <stdint.h> |
| 15 | 15 |
| 16 #include <memory> |
| 17 |
| 16 #include "base/logging.h" | 18 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "crypto/auto_cbb.h" | 19 #include "crypto/auto_cbb.h" |
| 19 #include "crypto/openssl_util.h" | 20 #include "crypto/openssl_util.h" |
| 20 #include "crypto/scoped_openssl_types.h" | 21 #include "crypto/scoped_openssl_types.h" |
| 21 | 22 |
| 22 namespace crypto { | 23 namespace crypto { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Function pointer definition, for injecting the required key export function | 27 // Function pointer definition, for injecting the required key export function |
| 27 // into ExportKeyWithBio, below. |bio| is a temporary memory BIO object, and | 28 // into ExportKeyWithBio, below. |bio| is a temporary memory BIO object, and |
| (...skipping 30 matching lines...) Expand all Loading... |
| 58 } | 59 } |
| 59 | 60 |
| 60 } // namespace | 61 } // namespace |
| 61 | 62 |
| 62 ECPrivateKey::~ECPrivateKey() { | 63 ECPrivateKey::~ECPrivateKey() { |
| 63 if (key_) | 64 if (key_) |
| 64 EVP_PKEY_free(key_); | 65 EVP_PKEY_free(key_); |
| 65 } | 66 } |
| 66 | 67 |
| 67 ECPrivateKey* ECPrivateKey::Copy() const { | 68 ECPrivateKey* ECPrivateKey::Copy() const { |
| 68 scoped_ptr<ECPrivateKey> copy(new ECPrivateKey); | 69 std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey); |
| 69 if (key_) | 70 if (key_) |
| 70 copy->key_ = EVP_PKEY_up_ref(key_); | 71 copy->key_ = EVP_PKEY_up_ref(key_); |
| 71 return copy.release(); | 72 return copy.release(); |
| 72 } | 73 } |
| 73 | 74 |
| 74 // static | 75 // static |
| 75 ECPrivateKey* ECPrivateKey::Create() { | 76 ECPrivateKey* ECPrivateKey::Create() { |
| 76 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 77 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 77 | 78 |
| 78 ScopedEC_KEY ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); | 79 ScopedEC_KEY ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 79 if (!ec_key.get() || !EC_KEY_generate_key(ec_key.get())) | 80 if (!ec_key.get() || !EC_KEY_generate_key(ec_key.get())) |
| 80 return NULL; | 81 return NULL; |
| 81 | 82 |
| 82 scoped_ptr<ECPrivateKey> result(new ECPrivateKey()); | 83 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey()); |
| 83 result->key_ = EVP_PKEY_new(); | 84 result->key_ = EVP_PKEY_new(); |
| 84 if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_, ec_key.get())) | 85 if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_, ec_key.get())) |
| 85 return NULL; | 86 return NULL; |
| 86 | 87 |
| 87 CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_type(result->key_->type)); | 88 CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_type(result->key_->type)); |
| 88 return result.release(); | 89 return result.release(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 // static | 92 // static |
| 92 ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 93 ECPrivateKey* ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 121 p8_decrypted.reset(PKCS8_decrypt_pbe( | 122 p8_decrypted.reset(PKCS8_decrypt_pbe( |
| 122 p8_encrypted.get(), | 123 p8_encrypted.get(), |
| 123 reinterpret_cast<const uint8_t*>(password.data()), | 124 reinterpret_cast<const uint8_t*>(password.data()), |
| 124 password.size())); | 125 password.size())); |
| 125 } | 126 } |
| 126 | 127 |
| 127 if (!p8_decrypted) | 128 if (!p8_decrypted) |
| 128 return NULL; | 129 return NULL; |
| 129 | 130 |
| 130 // Create a new EVP_PKEY for it. | 131 // Create a new EVP_PKEY for it. |
| 131 scoped_ptr<ECPrivateKey> result(new ECPrivateKey); | 132 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey); |
| 132 result->key_ = EVP_PKCS82PKEY(p8_decrypted.get()); | 133 result->key_ = EVP_PKCS82PKEY(p8_decrypted.get()); |
| 133 if (!result->key_ || EVP_PKEY_type(result->key_->type) != EVP_PKEY_EC) | 134 if (!result->key_ || EVP_PKEY_type(result->key_->type) != EVP_PKEY_EC) |
| 134 return NULL; | 135 return NULL; |
| 135 | 136 |
| 136 return result.release(); | 137 return result.release(); |
| 137 } | 138 } |
| 138 | 139 |
| 139 bool ECPrivateKey::ExportEncryptedPrivateKey(const std::string& password, | 140 bool ECPrivateKey::ExportEncryptedPrivateKey(const std::string& password, |
| 140 int iterations, | 141 int iterations, |
| 141 std::vector<uint8_t>* output) { | 142 std::vector<uint8_t>* output) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 return false; | 217 return false; |
| 217 } | 218 } |
| 218 output->assign(der, der + der_len); | 219 output->assign(der, der + der_len); |
| 219 OPENSSL_free(der); | 220 OPENSSL_free(der); |
| 220 return true; | 221 return true; |
| 221 } | 222 } |
| 222 | 223 |
| 223 ECPrivateKey::ECPrivateKey() : key_(NULL) {} | 224 ECPrivateKey::ECPrivateKey() : key_(NULL) {} |
| 224 | 225 |
| 225 } // namespace crypto | 226 } // namespace crypto |
| OLD | NEW |