Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "rtc_base/openssl_key_pair.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
| 16 | #if defined(WEBRTC_WIN) |
| 17 | // Must be included first before openssl headers. |
| 18 | #include "rtc_base/win32.h" // NOLINT |
| 19 | #endif // WEBRTC_WIN |
| 20 | |
| 21 | #include <openssl/bio.h> |
| 22 | #include <openssl/bn.h> |
| 23 | #include <openssl/pem.h> |
| 24 | #include <openssl/rsa.h> |
| 25 | |
| 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/logging.h" |
| 28 | #include "rtc_base/openssl.h" |
| 29 | #include "rtc_base/openssl_utility.h" |
| 30 | |
| 31 | namespace rtc { |
| 32 | |
| 33 | // We could have exposed a myriad of parameters for the crypto stuff, |
| 34 | // but keeping it simple seems best. |
| 35 | |
| 36 | // Generate a key pair. Caller is responsible for freeing the returned object. |
| 37 | static EVP_PKEY* MakeKey(const KeyParams& key_params) { |
| 38 | RTC_LOG(LS_INFO) << "Making key pair"; |
| 39 | EVP_PKEY* pkey = EVP_PKEY_new(); |
| 40 | if (key_params.type() == KT_RSA) { |
| 41 | int key_length = key_params.rsa_params().mod_size; |
| 42 | BIGNUM* exponent = BN_new(); |
| 43 | RSA* rsa = RSA_new(); |
| 44 | if (!pkey || !exponent || !rsa || |
| 45 | !BN_set_word(exponent, key_params.rsa_params().pub_exp) || |
| 46 | !RSA_generate_key_ex(rsa, key_length, exponent, nullptr) || |
| 47 | !EVP_PKEY_assign_RSA(pkey, rsa)) { |
| 48 | EVP_PKEY_free(pkey); |
| 49 | BN_free(exponent); |
| 50 | RSA_free(rsa); |
| 51 | RTC_LOG(LS_ERROR) << "Failed to make RSA key pair"; |
| 52 | return nullptr; |
| 53 | } |
| 54 | // ownership of rsa struct was assigned, don't free it. |
| 55 | BN_free(exponent); |
| 56 | } else if (key_params.type() == KT_ECDSA) { |
| 57 | if (key_params.ec_curve() == EC_NIST_P256) { |
| 58 | EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 59 | if (!ec_key) { |
| 60 | EVP_PKEY_free(pkey); |
| 61 | RTC_LOG(LS_ERROR) << "Failed to allocate EC key"; |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | // Ensure curve name is included when EC key is serialized. |
| 66 | // Without this call, OpenSSL versions before 1.1.0 will create |
| 67 | // certificates that don't work for TLS. |
| 68 | // This is a no-op for BoringSSL and OpenSSL 1.1.0+ |
| 69 | EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE); |
| 70 | |
| 71 | if (!pkey || !ec_key || !EC_KEY_generate_key(ec_key) || |
| 72 | !EVP_PKEY_assign_EC_KEY(pkey, ec_key)) { |
| 73 | EVP_PKEY_free(pkey); |
| 74 | EC_KEY_free(ec_key); |
| 75 | RTC_LOG(LS_ERROR) << "Failed to make EC key pair"; |
| 76 | return nullptr; |
| 77 | } |
| 78 | // ownership of ec_key struct was assigned, don't free it. |
| 79 | } else { |
| 80 | // Add generation of any other curves here. |
| 81 | EVP_PKEY_free(pkey); |
| 82 | RTC_LOG(LS_ERROR) << "ECDSA key requested for unknown curve"; |
| 83 | return nullptr; |
| 84 | } |
| 85 | } else { |
| 86 | EVP_PKEY_free(pkey); |
| 87 | RTC_LOG(LS_ERROR) << "Key type requested not understood"; |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
| 91 | RTC_LOG(LS_INFO) << "Returning key pair"; |
| 92 | return pkey; |
| 93 | } |
| 94 | |
| 95 | std::unique_ptr<OpenSSLKeyPair> OpenSSLKeyPair::Generate( |
| 96 | const KeyParams& key_params) { |
| 97 | EVP_PKEY* pkey = MakeKey(key_params); |
| 98 | if (!pkey) { |
| 99 | openssl::LogSSLErrors("Generating key pair"); |
| 100 | return nullptr; |
| 101 | } |
| 102 | return std::make_unique<OpenSSLKeyPair>(pkey); |
| 103 | } |
| 104 | |
| 105 | std::unique_ptr<OpenSSLKeyPair> OpenSSLKeyPair::FromPrivateKeyPEMString( |
| 106 | const std::string& pem_string) { |
| 107 | BIO* bio = |
| 108 | BIO_new_mem_buf(const_cast<char*>(pem_string.data()), pem_string.size()); |
| 109 | if (!bio) { |
| 110 | RTC_LOG(LS_ERROR) << "Failed to create a new BIO buffer."; |
| 111 | return nullptr; |
| 112 | } |
| 113 | BIO_set_mem_eof_return(bio, 0); |
| 114 | EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr); |
| 115 | BIO_free(bio); // Frees the BIO, but not the pointed-to string. |
| 116 | if (!pkey) { |
| 117 | RTC_LOG(LS_ERROR) << "Failed to create the private key from PEM string."; |
| 118 | return nullptr; |
| 119 | } |
| 120 | if (EVP_PKEY_missing_parameters(pkey) != 0) { |
| 121 | RTC_LOG(LS_ERROR) |
| 122 | << "The resulting key pair is missing public key parameters."; |
| 123 | EVP_PKEY_free(pkey); |
| 124 | return nullptr; |
| 125 | } |
| 126 | return std::make_unique<OpenSSLKeyPair>(pkey); |
| 127 | } |
| 128 | |
| 129 | OpenSSLKeyPair::~OpenSSLKeyPair() { |
| 130 | EVP_PKEY_free(pkey_); |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<OpenSSLKeyPair> OpenSSLKeyPair::Clone() { |
| 134 | AddReference(); |
| 135 | return std::make_unique<OpenSSLKeyPair>(pkey_); |
| 136 | } |
| 137 | |
| 138 | void OpenSSLKeyPair::AddReference() { |
| 139 | EVP_PKEY_up_ref(pkey_); |
| 140 | } |
| 141 | |
| 142 | std::string OpenSSLKeyPair::PrivateKeyToPEMString() const { |
| 143 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 144 | if (!temp_memory_bio) { |
| 145 | RTC_LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 146 | RTC_NOTREACHED(); |
| 147 | return ""; |
| 148 | } |
| 149 | if (!PEM_write_bio_PrivateKey(temp_memory_bio, pkey_, nullptr, nullptr, 0, |
| 150 | nullptr, nullptr)) { |
| 151 | RTC_LOG_F(LS_ERROR) << "Failed to write private key"; |
| 152 | BIO_free(temp_memory_bio); |
| 153 | RTC_NOTREACHED(); |
| 154 | return ""; |
| 155 | } |
| 156 | char* buffer; |
| 157 | size_t len = BIO_get_mem_data(temp_memory_bio, &buffer); |
| 158 | std::string priv_key_str(buffer, len); |
| 159 | BIO_free(temp_memory_bio); |
| 160 | return priv_key_str; |
| 161 | } |
| 162 | |
| 163 | std::string OpenSSLKeyPair::PublicKeyToPEMString() const { |
| 164 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 165 | if (!temp_memory_bio) { |
| 166 | RTC_LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
| 167 | RTC_NOTREACHED(); |
| 168 | return ""; |
| 169 | } |
| 170 | if (!PEM_write_bio_PUBKEY(temp_memory_bio, pkey_)) { |
| 171 | RTC_LOG_F(LS_ERROR) << "Failed to write public key"; |
| 172 | BIO_free(temp_memory_bio); |
| 173 | RTC_NOTREACHED(); |
| 174 | return ""; |
| 175 | } |
| 176 | BIO_write(temp_memory_bio, "\0", 1); |
| 177 | char* buffer; |
| 178 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 179 | std::string pub_key_str = buffer; |
| 180 | BIO_free(temp_memory_bio); |
| 181 | return pub_key_str; |
| 182 | } |
| 183 | |
| 184 | bool OpenSSLKeyPair::operator==(const OpenSSLKeyPair& other) const { |
| 185 | return EVP_PKEY_cmp(this->pkey_, other.pkey_) == 1; |
| 186 | } |
| 187 | |
| 188 | bool OpenSSLKeyPair::operator!=(const OpenSSLKeyPair& other) const { |
| 189 | return !(*this == other); |
| 190 | } |
| 191 | |
| 192 | } // namespace rtc |