1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/ec_private_key.h"
12 #include "base/check_op.h"
13 #include "crypto/openssl_util.h"
14 #include "third_party/boringssl/src/include/openssl/bn.h"
15 #include "third_party/boringssl/src/include/openssl/bytestring.h"
16 #include "third_party/boringssl/src/include/openssl/ec.h"
17 #include "third_party/boringssl/src/include/openssl/ec_key.h"
18 #include "third_party/boringssl/src/include/openssl/evp.h"
19 #include "third_party/boringssl/src/include/openssl/mem.h"
20 #include "third_party/boringssl/src/include/openssl/pkcs8.h"
24 ECPrivateKey::~ECPrivateKey() = default;
27 std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() {
28 OpenSSLErrStackTracer err_tracer(FROM_HERE);
30 bssl::UniquePtr<EC_KEY> ec_key(
31 EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
32 if (!ec_key || !EC_KEY_generate_key(ec_key.get()))
35 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
36 result->key_.reset(EVP_PKEY_new());
37 if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_.get(), ec_key.get()))
40 CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_.get()));
45 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo(
46 base::span<const uint8_t> input) {
47 OpenSSLErrStackTracer err_tracer(FROM_HERE);
50 CBS_init(&cbs, input.data(), input.size());
51 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
52 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
55 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
56 result->key_ = std::move(pkey);
61 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
62 base::span<const uint8_t> encrypted_private_key_info) {
63 OpenSSLErrStackTracer err_tracer(FROM_HERE);
66 CBS_init(&cbs, encrypted_private_key_info.data(),
67 encrypted_private_key_info.size());
68 bssl::UniquePtr<EVP_PKEY> pkey(
69 PKCS8_parse_encrypted_private_key(&cbs, "", 0));
71 // Hack for reading keys generated by an older version of the OpenSSL code.
72 // Some implementations encode the empty password as "\0\0" (passwords are
73 // normally encoded in big-endian UCS-2 with a NUL terminator) and some
74 // encode as the empty string. PKCS8_parse_encrypted_private_key
75 // distinguishes the two by whether the password is nullptr.
77 CBS_init(&cbs, encrypted_private_key_info.data(),
78 encrypted_private_key_info.size());
79 pkey.reset(PKCS8_parse_encrypted_private_key(&cbs, nullptr, 0));
82 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
85 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
86 result->key_ = std::move(pkey);
90 std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const {
91 std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey());
92 copy->key_ = bssl::UpRef(key_);
96 bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
97 OpenSSLErrStackTracer err_tracer(FROM_HERE);
101 if (!CBB_init(cbb.get(), 0) ||
102 !EVP_marshal_private_key(cbb.get(), key_.get()) ||
103 !CBB_finish(cbb.get(), &der, &der_len)) {
106 output->assign(der, der + der_len);
111 bool ECPrivateKey::ExportEncryptedPrivateKey(
112 std::vector<uint8_t>* output) const {
113 OpenSSLErrStackTracer err_tracer(FROM_HERE);
115 // Encrypt the object.
116 // NOTE: NSS uses SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC
117 // so use NID_pbe_WithSHA1And3_Key_TripleDES_CBC which should be the OpenSSL
122 if (!CBB_init(cbb.get(), 0) ||
123 !PKCS8_marshal_encrypted_private_key(
124 cbb.get(), NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
125 nullptr /* cipher */, nullptr /* no password */, 0 /* pass_len */,
126 nullptr /* salt */, 0 /* salt_len */, 1 /* iterations */,
128 !CBB_finish(cbb.get(), &der, &der_len)) {
131 output->assign(der, der + der_len);
136 bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
137 OpenSSLErrStackTracer err_tracer(FROM_HERE);
141 if (!CBB_init(cbb.get(), 0) ||
142 !EVP_marshal_public_key(cbb.get(), key_.get()) ||
143 !CBB_finish(cbb.get(), &der, &der_len)) {
146 output->assign(der, der + der_len);
151 bool ECPrivateKey::ExportRawPublicKey(std::string* output) const {
152 OpenSSLErrStackTracer err_tracer(FROM_HERE);
154 std::array<uint8_t, 65> buf;
155 EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key_.get());
156 if (!EC_POINT_point2oct(EC_KEY_get0_group(ec_key),
157 EC_KEY_get0_public_key(ec_key),
158 POINT_CONVERSION_UNCOMPRESSED, buf.data(), buf.size(),
163 output->assign(buf.begin(), buf.end());
167 ECPrivateKey::ECPrivateKey() = default;
169 } // namespace crypto