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