[M120 Migration][VD] Enable direct rendering for TVPlus
[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 #if BUILDFLAG(IS_TIZEN_TV)
14 #include "base/logging.h"
15 #endif
16 #include "crypto/openssl_util.h"
17 #include "third_party/boringssl/src/include/openssl/bytestring.h"
18 #include "third_party/boringssl/src/include/openssl/ec.h"
19 #include "third_party/boringssl/src/include/openssl/ec_key.h"
20 #include "third_party/boringssl/src/include/openssl/evp.h"
21 #include "third_party/boringssl/src/include/openssl/mem.h"
22 #include "third_party/boringssl/src/include/openssl/pkcs8.h"
23 #if BUILDFLAG(IS_TIZEN_TV)
24 #include "third_party/boringssl/src/include/openssl/pem.h"
25 #endif
26 namespace crypto {
27
28 ECPrivateKey::~ECPrivateKey() = default;
29
30 // static
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
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
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
94 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
100 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
115 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
130 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 #if BUILDFLAG(IS_TIZEN_TV)
149 bool ECPrivateKey::ConvertPEMtoDERFromPrivateKey(const std::string& key_buffer,
150                                                  std::vector<uint8_t>* output) {
151   bssl::UniquePtr<BIO> in(
152       BIO_new_mem_buf(const_cast<char*>(key_buffer.data()), key_buffer.size()));
153   if (!in) {
154     LOG(ERROR) << "Can't create a memory BIO";
155     return false;
156   }
157
158   bssl::UniquePtr<BIO> out(BIO_new(BIO_s_mem()));
159   if (!out) {
160     LOG(ERROR) << "BIO_new for converting a format of private key failed";
161     return false;
162   }
163
164   EVP_PKEY* pkey = PEM_read_bio_PrivateKey(in.get(), NULL, NULL, NULL);
165   if (!pkey) {
166     LOG(ERROR) << "Can't load private key";
167     return false;
168   }
169
170   int ret = i2d_PKCS8PrivateKeyInfo_bio(out.get(), pkey);
171   EVP_PKEY_free(pkey);
172   if (!ret) {
173     LOG(ERROR) << "Can't convert a format of private key";
174     return false;
175   }
176
177   char* data = nullptr;
178   long len = BIO_get_mem_data(out.get(), &data);
179
180   if (!data || len < 0) {
181     LOG(ERROR) << "Can't get der data from BIO";
182     return false;
183   }
184
185   output->assign(data, data + len);
186   return true;
187 }
188 #endif
189 }  // namespace crypto