[email protected] | ab782c9 | 2012-07-09 23:12:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 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 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 9 | |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 13 | #include "crypto/openssl_util.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 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" |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 20 | #include "third_party/boringssl/src/include/openssl/pkcs8.h" |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 21 | |
| 22 | namespace crypto { |
| 23 | |
Chris Watkins | a850a30 | 2017-11-30 03:53:49 | [diff] [blame] | 24 | ECPrivateKey::~ECPrivateKey() = default; |
[email protected] | ab782c9 | 2012-07-09 23:12:14 | [diff] [blame] | 25 | |
| 26 | // static |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 27 | std::unique_ptr<ECPrivateKey> ECPrivateKey::Create() { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 28 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 29 | |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 30 | bssl::UniquePtr<EC_KEY> ec_key( |
| 31 | EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 32 | if (!ec_key || !EC_KEY_generate_key(ec_key.get())) |
| 33 | return nullptr; |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 34 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 35 | std::unique_ptr<ECPrivateKey> result(new ECPrivateKey()); |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 36 | result->key_.reset(EVP_PKEY_new()); |
| 37 | if (!result->key_ || !EVP_PKEY_set1_EC_KEY(result->key_.get(), ec_key.get())) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 38 | return nullptr; |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 39 | |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 40 | CHECK_EQ(EVP_PKEY_EC, EVP_PKEY_id(result->key_.get())); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 41 | return result; |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // static |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 45 | std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromPrivateKeyInfo( |
| 46 | const std::vector<uint8_t>& input) { |
| 47 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 48 | |
| 49 | CBS cbs; |
| 50 | CBS_init(&cbs, input.data(), input.size()); |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 51 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs)); |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 52 | if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC) |
| 53 | return nullptr; |
| 54 | |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 55 | std::unique_ptr<ECPrivateKey> result(new ECPrivateKey()); |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 56 | result->key_ = std::move(pkey); |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 57 | return result; |
| 58 | } |
| 59 | |
| 60 | // static |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 61 | std::unique_ptr<ECPrivateKey> ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
David Benjamin | b86b0a08 | 2017-09-21 16:55:36 | [diff] [blame] | 62 | const std::vector<uint8_t>& encrypted_private_key_info) { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 63 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | dc06f75 | 2014-08-06 23:11:09 | [diff] [blame] | 64 | |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 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)); |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 70 | |
davidben | 1c02c94c | 2017-01-04 13:54:32 | [diff] [blame] | 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 |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 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 | } |
[email protected] | dc06f75 | 2014-08-06 23:11:09 | [diff] [blame] | 81 | |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 82 | if (!pkey || CBS_len(&cbs) != 0 || EVP_PKEY_id(pkey.get()) != EVP_PKEY_EC) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 83 | return nullptr; |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 84 | |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 85 | std::unique_ptr<ECPrivateKey> result(new ECPrivateKey()); |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 86 | result->key_ = std::move(pkey); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 87 | return result; |
| 88 | } |
| 89 | |
| 90 | std::unique_ptr<ECPrivateKey> ECPrivateKey::Copy() const { |
| 91 | std::unique_ptr<ECPrivateKey> copy(new ECPrivateKey()); |
agl | 5a7cadf | 2016-07-13 16:52:53 | [diff] [blame] | 92 | if (key_) { |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 93 | EVP_PKEY_up_ref(key_.get()); |
| 94 | copy->key_.reset(key_.get()); |
agl | 5a7cadf | 2016-07-13 16:52:53 | [diff] [blame] | 95 | } |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 96 | return copy; |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 97 | } |
| 98 | |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 99 | bool ECPrivateKey::ExportPrivateKey(std::vector<uint8_t>* output) const { |
| 100 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 101 | uint8_t* der; |
| 102 | size_t der_len; |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 103 | bssl::ScopedCBB cbb; |
| 104 | if (!CBB_init(cbb.get(), 0) || |
| 105 | !EVP_marshal_private_key(cbb.get(), key_.get()) || |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 106 | !CBB_finish(cbb.get(), &der, &der_len)) { |
| 107 | return false; |
| 108 | } |
| 109 | output->assign(der, der + der_len); |
| 110 | OPENSSL_free(der); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool ECPrivateKey::ExportEncryptedPrivateKey( |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 115 | std::vector<uint8_t>* output) const { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 116 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 117 | |
| 118 | // Encrypt the object. |
| 119 | // NOTE: NSS uses SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC |
| 120 | // so use NID_pbe_WithSHA1And3_Key_TripleDES_CBC which should be the OpenSSL |
| 121 | // equivalent. |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 122 | uint8_t* der; |
| 123 | size_t der_len; |
| 124 | bssl::ScopedCBB cbb; |
| 125 | if (!CBB_init(cbb.get(), 0) || |
| 126 | !PKCS8_marshal_encrypted_private_key( |
| 127 | cbb.get(), NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 128 | nullptr /* cipher */, nullptr /* no password */, 0 /* pass_len */, |
| 129 | nullptr /* salt */, 0 /* salt_len */, 1 /* iterations */, |
| 130 | key_.get()) || |
| 131 | !CBB_finish(cbb.get(), &der, &der_len)) { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 132 | return false; |
davidben | b8adc35 | 2017-04-03 21:10:32 | [diff] [blame] | 133 | } |
| 134 | output->assign(der, der + der_len); |
| 135 | OPENSSL_free(der); |
| 136 | return true; |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 137 | } |
| 138 | |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 139 | bool ECPrivateKey::ExportPublicKey(std::vector<uint8_t>* output) const { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 140 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 141 | uint8_t *der; |
| 142 | size_t der_len; |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 143 | bssl::ScopedCBB cbb; |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 144 | if (!CBB_init(cbb.get(), 0) || |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 145 | !EVP_marshal_public_key(cbb.get(), key_.get()) || |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 146 | !CBB_finish(cbb.get(), &der, &der_len)) { |
[email protected] | ac30ed0e | 2014-06-24 04:12:34 | [diff] [blame] | 147 | return false; |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 148 | } |
| 149 | output->assign(der, der + der_len); |
| 150 | OPENSSL_free(der); |
[email protected] | ac30ed0e | 2014-06-24 04:12:34 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
davidben | 212cdf6 | 2016-06-07 17:11:09 | [diff] [blame] | 154 | bool ECPrivateKey::ExportRawPublicKey(std::string* output) const { |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 155 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 156 | |
| 157 | // Export the x and y field elements as 32-byte, big-endian numbers. (This is |
| 158 | // the same as X9.62 uncompressed form without the leading 0x04 byte.) |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 159 | EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key_.get()); |
| 160 | bssl::UniquePtr<BIGNUM> x(BN_new()); |
| 161 | bssl::UniquePtr<BIGNUM> y(BN_new()); |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 162 | uint8_t buf[64]; |
| 163 | if (!x || !y || |
| 164 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 165 | EC_KEY_get0_public_key(ec_key), |
| 166 | x.get(), y.get(), nullptr) || |
| 167 | !BN_bn2bin_padded(buf, 32, x.get()) || |
| 168 | !BN_bn2bin_padded(buf + 32, 32, y.get())) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | output->assign(reinterpret_cast<const char*>(buf), sizeof(buf)); |
| 173 | return true; |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 174 | } |
| 175 | |
Chris Watkins | a850a30 | 2017-11-30 03:53:49 | [diff] [blame] | 176 | ECPrivateKey::ECPrivateKey() = default; |
[email protected] | d4db0b6 | 2013-10-17 16:09:24 | [diff] [blame] | 177 | |
[email protected] | eaa6048 | 2011-11-09 05:08:51 | [diff] [blame] | 178 | } // namespace crypto |