[M108 Migration] Support standard build for armv7hl architecture
[platform/framework/web/chromium-efl.git] / crypto / ec_private_key.cc
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/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"
21
22 namespace crypto {
23
24 ECPrivateKey::~ECPrivateKey() = default;
25
26 // static
27 std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() {
28   OpenSSLErrStackTracer err_tracer(FROM_HERE);
29
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()))
33     return nullptr;
34
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()))
38     return nullptr;
39
40   CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_.get()));
41   return result;
42 }
43
44 // static
45 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo(
46     base::span<const uint8_t> input) {
47   OpenSSLErrStackTracer err_tracer(FROM_HERE);
48
49   CBS cbs;
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)
53     return nullptr;
54
55   std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
56   result->key_ = std::move(pkey);
57   return result;
58 }
59
60 // static
61 std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
62     base::span<const uint8_t> encrypted_private_key_info) {
63   OpenSSLErrStackTracer err_tracer(FROM_HERE);
64
65   CBS cbs;
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));
70
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.
76   if (!pkey) {
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));
80   }
81
82   if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC)
83     return nullptr;
84
85   std::unique_ptr<ECPrivateKey> result(new ECPrivateKey());
86   result->key_ = std::move(pkey);
87   return result;
88 }
89
90 std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const {
91   std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey());
92   copy->key_ = bssl::UpRef(key_);
93   return copy;
94 }
95
96 bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const {
97   OpenSSLErrStackTracer err_tracer(FROM_HERE);
98   uint8_t* der;
99   size_t der_len;
100   bssl::ScopedCBB cbb;
101   if (!CBB_init(cbb.get(), 0) ||
102       !EVP_marshal_private_key(cbb.get(), key_.get()) ||
103       !CBB_finish(cbb.get(), &der, &der_len)) {
104     return false;
105   }
106   output->assign(der, der + der_len);
107   OPENSSL_free(der);
108   return true;
109 }
110
111 bool ECPrivateKey::ExportEncryptedPrivateKey(
112     std::vector<uint8_t>* output) const {
113   OpenSSLErrStackTracer err_tracer(FROM_HERE);
114
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
118   // equivalent.
119   uint8_t* der;
120   size_t der_len;
121   bssl::ScopedCBB cbb;
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 */,
127           key_.get()) ||
128       !CBB_finish(cbb.get(), &der, &der_len)) {
129     return false;
130   }
131   output->assign(der, der + der_len);
132   OPENSSL_free(der);
133   return true;
134 }
135
136 bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const {
137   OpenSSLErrStackTracer err_tracer(FROM_HERE);
138   uint8_t* der;
139   size_t der_len;
140   bssl::ScopedCBB cbb;
141   if (!CBB_init(cbb.get(), 0) ||
142       !EVP_marshal_public_key(cbb.get(), key_.get()) ||
143       !CBB_finish(cbb.get(), &der, &der_len)) {
144     return false;
145   }
146   output->assign(der, der + der_len);
147   OPENSSL_free(der);
148   return true;
149 }
150
151 bool ECPrivateKey::ExportRawPublicKey(std::string* output) const {
152   OpenSSLErrStackTracer err_tracer(FROM_HERE);
153
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(),
159                           /*ctx=*/nullptr)) {
160     return false;
161   }
162
163   output->assign(buf.begin(), buf.end());
164   return true;
165 }
166
167 ECPrivateKey::ECPrivateKey() = default;
168
169 }  // namespace crypto