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.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "crypto/ec_private_key.h"
11
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include <utility>
16
17 #include "base/check_op.h"
18 #include "crypto/openssl_util.h"
19 #include "third_party/boringssl/src/include/openssl/bytestring.h"
20 #include "third_party/boringssl/src/include/openssl/ec.h"
21 #include "third_party/boringssl/src/include/openssl/ec_key.h"
22 #include "third_party/boringssl/src/include/openssl/evp.h"
23 #include "third_party/boringssl/src/include/openssl/mem.h"
24 #include "third_party/boringssl/src/include/openssl/pkcs8.h"
25
26 namespace crypto {
27
28 ECPrivateKey::~ECPrivateKey() = default;
29
30 // static
Create()31 std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() {
32 OpenSSLErrStackTracer err_tracer(FROM_HERE);
33
34 bssl::UniquePtr<EC_KEY> ec_key(
35 EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
36 if (!ec_key || !EC_KEY_generate_key(ec_key.get()))
37 return nullptr;
38
39 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
40 result->key_.reset(EVP_PKEY_new());
41 if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_.get(), ec_key.get()))
42 return nullptr;
43
44 CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_.get()));
45 return result;
46 }
47
48 // static
CreateFromPrivateKeyInfo(base::span<const uint8_t> input)49 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo(
50 base::span<const uint8_t> input) {
51 OpenSSLErrStackTracer err_tracer(FROM_HERE);
52
53 CBS cbs;
54 CBS_init(&cbs, input.data(), input.size());
55 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
56 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
57 return nullptr;
58
59 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
60 result->key_ = std::move(pkey);
61 return result;
62 }
63
64 // static
CreateFromEncryptedPrivateKeyInfo(base::span<const uint8_t> encrypted_private_key_info)65 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
66 base::span<const uint8_t> encrypted_private_key_info) {
67 OpenSSLErrStackTracer err_tracer(FROM_HERE);
68
69 CBS cbs;
70 CBS_init(&cbs, encrypted_private_key_info.data(),
71 encrypted_private_key_info.size());
72 bssl::UniquePtr<EVP_PKEY> pkey(
73 PKCS8_parse_encrypted_private_key(&cbs, "", 0));
74
75 // Hack for reading keys generated by an older version of the OpenSSL code.
76 // Some implementations encode the empty password as "\0\0" (passwords are
77 // normally encoded in big-endian UCS-2 with a NUL terminator) and some
78 // encode as the empty string. PKCS8_parse_encrypted_private_key
79 // distinguishes the two by whether the password is nullptr.
80 if (!pkey) {
81 CBS_init(&cbs, encrypted_private_key_info.data(),
82 encrypted_private_key_info.size());
83 pkey.reset(PKCS8_parse_encrypted_private_key(&cbs, nullptr, 0));
84 }
85
86 if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
87 return nullptr;
88
89 std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
90 result->key_ = std::move(pkey);
91 return result;
92 }
93
Copy() const94 std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const {
95 std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey());
96 copy->key_ = bssl::UpRef(key_);
97 return copy;
98 }
99
ExportPrivateKey(std::vector<uint8_t> * output) const100 bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
101 OpenSSLErrStackTracer err_tracer(FROM_HERE);
102 uint8_t* der;
103 size_t der_len;
104 bssl::ScopedCBB cbb;
105 if (!CBB_init(cbb.get(), 0) ||
106 !EVP_marshal_private_key(cbb.get(), key_.get()) ||
107 !CBB_finish(cbb.get(), &der, &der_len)) {
108 return false;
109 }
110 output->assign(der, der + der_len);
111 OPENSSL_free(der);
112 return true;
113 }
114
ExportPublicKey(std::vector<uint8_t> * output) const115 bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
116 OpenSSLErrStackTracer err_tracer(FROM_HERE);
117 uint8_t* der;
118 size_t der_len;
119 bssl::ScopedCBB cbb;
120 if (!CBB_init(cbb.get(), 0) ||
121 !EVP_marshal_public_key(cbb.get(), key_.get()) ||
122 !CBB_finish(cbb.get(), &der, &der_len)) {
123 return false;
124 }
125 output->assign(der, der + der_len);
126 OPENSSL_free(der);
127 return true;
128 }
129
ExportRawPublicKey(std::string * output) const130 bool ECPrivateKey::ExportRawPublicKey(std::string* output) const {
131 OpenSSLErrStackTracer err_tracer(FROM_HERE);
132
133 std::array<uint8_t, 65> buf;
134 EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key_.get());
135 if (!EC_POINT_point2oct(EC_KEY_get0_group(ec_key),
136 EC_KEY_get0_public_key(ec_key),
137 POINT_CONVERSION_UNCOMPRESSED, buf.data(), buf.size(),
138 /*ctx=*/nullptr)) {
139 return false;
140 }
141
142 output->assign(buf.begin(), buf.end());
143 return true;
144 }
145
146 ECPrivateKey::ECPrivateKey() = default;
147
148 } // namespace crypto
149