henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/openssl_identity.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 14 | #include <utility> |
| 15 | #include <vector> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 16 | |
Mirko Bonadei | e062385 | 2018-02-01 11:17:40 +0100 | [diff] [blame] | 17 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | // Must be included first before openssl headers. |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/win32.h" // NOLINT |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 20 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | |
| 22 | #include <openssl/bio.h> |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 23 | #include <openssl/bn.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #include <openssl/err.h> |
| 25 | #include <openssl/pem.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | #include <openssl/rsa.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 29 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/logging.h" |
Mirko Bonadei | a041f92 | 2018-05-23 10:22:36 +0200 | [diff] [blame] | 32 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "rtc_base/openssl.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "rtc_base/openssl_utility.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | |
| 36 | namespace rtc { |
| 37 | |
| 38 | // We could have exposed a myriad of parameters for the crypto stuff, |
| 39 | // but keeping it simple seems best. |
| 40 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | // Generate a key pair. Caller is responsible for freeing the returned object. |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 42 | static EVP_PKEY* MakeKey(const KeyParams& key_params) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 43 | RTC_LOG(LS_INFO) << "Making key pair"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | EVP_PKEY* pkey = EVP_PKEY_new(); |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 45 | if (key_params.type() == KT_RSA) { |
| 46 | int key_length = key_params.rsa_params().mod_size; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 47 | BIGNUM* exponent = BN_new(); |
| 48 | RSA* rsa = RSA_new(); |
| 49 | if (!pkey || !exponent || !rsa || |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 50 | !BN_set_word(exponent, key_params.rsa_params().pub_exp) || |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 51 | !RSA_generate_key_ex(rsa, key_length, exponent, nullptr) || |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 52 | !EVP_PKEY_assign_RSA(pkey, rsa)) { |
| 53 | EVP_PKEY_free(pkey); |
| 54 | BN_free(exponent); |
| 55 | RSA_free(rsa); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 56 | RTC_LOG(LS_ERROR) << "Failed to make RSA key pair"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 57 | return nullptr; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 58 | } |
| 59 | // ownership of rsa struct was assigned, don't free it. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | BN_free(exponent); |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 61 | } else if (key_params.type() == KT_ECDSA) { |
| 62 | if (key_params.ec_curve() == EC_NIST_P256) { |
| 63 | EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
ssaroha | bbfed52 | 2016-12-11 18:42:07 -0800 | [diff] [blame] | 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 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 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); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 75 | RTC_LOG(LS_ERROR) << "Failed to make EC key pair"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 76 | return nullptr; |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 77 | } |
| 78 | // ownership of ec_key struct was assigned, don't free it. |
| 79 | } else { |
| 80 | // Add generation of any other curves here. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 81 | EVP_PKEY_free(pkey); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 82 | RTC_LOG(LS_ERROR) << "ECDSA key requested for unknown curve"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 83 | return nullptr; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 84 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 85 | } else { |
| 86 | EVP_PKEY_free(pkey); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 87 | RTC_LOG(LS_ERROR) << "Key type requested not understood"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 88 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 90 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 91 | RTC_LOG(LS_INFO) << "Returning key pair"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | return pkey; |
| 93 | } |
| 94 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 95 | OpenSSLKeyPair* OpenSSLKeyPair::Generate(const KeyParams& key_params) { |
| 96 | EVP_PKEY* pkey = MakeKey(key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | if (!pkey) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 98 | openssl::LogSSLErrors("Generating key pair"); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 99 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | } |
| 101 | return new OpenSSLKeyPair(pkey); |
| 102 | } |
| 103 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 104 | OpenSSLKeyPair* OpenSSLKeyPair::FromPrivateKeyPEMString( |
| 105 | const std::string& pem_string) { |
| 106 | BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1); |
| 107 | if (!bio) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 108 | RTC_LOG(LS_ERROR) << "Failed to create a new BIO buffer."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
| 111 | BIO_set_mem_eof_return(bio, 0); |
| 112 | EVP_PKEY* pkey = |
| 113 | PEM_read_bio_PrivateKey(bio, nullptr, nullptr, const_cast<char*>("\0")); |
| 114 | BIO_free(bio); // Frees the BIO, but not the pointed-to string. |
| 115 | if (!pkey) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_ERROR) << "Failed to create the private key from PEM string."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 117 | return nullptr; |
| 118 | } |
| 119 | if (EVP_PKEY_missing_parameters(pkey) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 120 | RTC_LOG(LS_ERROR) |
| 121 | << "The resulting key pair is missing public key parameters."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 122 | EVP_PKEY_free(pkey); |
| 123 | return nullptr; |
| 124 | } |
| 125 | return new OpenSSLKeyPair(pkey); |
| 126 | } |
| 127 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | OpenSSLKeyPair::~OpenSSLKeyPair() { |
| 129 | EVP_PKEY_free(pkey_); |
| 130 | } |
| 131 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 132 | OpenSSLKeyPair* OpenSSLKeyPair::GetReference() { |
| 133 | AddReference(); |
| 134 | return new OpenSSLKeyPair(pkey_); |
| 135 | } |
| 136 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | void OpenSSLKeyPair::AddReference() { |
Jiayang Liu | 770cc38 | 2015-05-28 15:36:29 -0700 | [diff] [blame] | 138 | EVP_PKEY_up_ref(pkey_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | } |
| 140 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 141 | std::string OpenSSLKeyPair::PrivateKeyToPEMString() const { |
| 142 | BIO* temp_memory_bio = BIO_new(BIO_s_mem()); |
| 143 | if (!temp_memory_bio) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 144 | RTC_LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 145 | RTC_NOTREACHED(); |
| 146 | return ""; |
| 147 | } |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 148 | if (!PEM_write_bio_PrivateKey(temp_memory_bio, pkey_, nullptr, nullptr, 0, |
| 149 | nullptr, nullptr)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 150 | RTC_LOG_F(LS_ERROR) << "Failed to write private key"; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 151 | BIO_free(temp_memory_bio); |
| 152 | RTC_NOTREACHED(); |
| 153 | return ""; |
| 154 | } |
| 155 | BIO_write(temp_memory_bio, "\0", 1); |
| 156 | char* buffer; |
| 157 | BIO_get_mem_data(temp_memory_bio, &buffer); |
| 158 | std::string priv_key_str = buffer; |
| 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) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 166 | RTC_LOG_F(LS_ERROR) << "Failed to allocate temporary memory bio"; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 167 | RTC_NOTREACHED(); |
| 168 | return ""; |
| 169 | } |
| 170 | if (!PEM_write_bio_PUBKEY(temp_memory_bio, pkey_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 171 | RTC_LOG_F(LS_ERROR) << "Failed to write public key"; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 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 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 192 | OpenSSLIdentity::OpenSSLIdentity( |
| 193 | std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 194 | std::unique_ptr<OpenSSLCertificate> certificate) |
| 195 | : key_pair_(std::move(key_pair)) { |
| 196 | RTC_DCHECK(key_pair_ != nullptr); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 197 | RTC_DCHECK(certificate != nullptr); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 198 | std::vector<std::unique_ptr<SSLCertificate>> certs; |
| 199 | certs.push_back(std::move(certificate)); |
| 200 | cert_chain_.reset(new SSLCertChain(std::move(certs))); |
| 201 | } |
| 202 | |
| 203 | OpenSSLIdentity::OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 204 | std::unique_ptr<SSLCertChain> cert_chain) |
| 205 | : key_pair_(std::move(key_pair)), cert_chain_(std::move(cert_chain)) { |
| 206 | RTC_DCHECK(key_pair_ != nullptr); |
| 207 | RTC_DCHECK(cert_chain_ != nullptr); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | OpenSSLIdentity::~OpenSSLIdentity() = default; |
| 211 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 212 | OpenSSLIdentity* OpenSSLIdentity::GenerateInternal( |
| 213 | const SSLIdentityParams& params) { |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 214 | std::unique_ptr<OpenSSLKeyPair> key_pair( |
| 215 | OpenSSLKeyPair::Generate(params.key_params)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | if (key_pair) { |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 217 | std::unique_ptr<OpenSSLCertificate> certificate( |
| 218 | OpenSSLCertificate::Generate(key_pair.get(), params)); |
| 219 | if (certificate != nullptr) |
| 220 | return new OpenSSLIdentity(std::move(key_pair), std::move(certificate)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 222 | RTC_LOG(LS_INFO) << "Identity generation failed"; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 223 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 226 | OpenSSLIdentity* OpenSSLIdentity::GenerateWithExpiration( |
| 227 | const std::string& common_name, |
| 228 | const KeyParams& key_params, |
| 229 | time_t certificate_lifetime) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | SSLIdentityParams params; |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 231 | params.key_params = key_params; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | params.common_name = common_name; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 233 | time_t now = time(nullptr); |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 234 | params.not_before = now + kCertificateWindowInSeconds; |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 235 | params.not_after = now + certificate_lifetime; |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 236 | if (params.not_before > params.not_after) |
| 237 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 238 | return GenerateInternal(params); |
| 239 | } |
| 240 | |
| 241 | OpenSSLIdentity* OpenSSLIdentity::GenerateForTest( |
| 242 | const SSLIdentityParams& params) { |
| 243 | return GenerateInternal(params); |
| 244 | } |
| 245 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 246 | SSLIdentity* OpenSSLIdentity::FromPEMStrings(const std::string& private_key, |
| 247 | const std::string& certificate) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 248 | std::unique_ptr<OpenSSLCertificate> cert( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | OpenSSLCertificate::FromPEMString(certificate)); |
| 250 | if (!cert) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 251 | RTC_LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 252 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 255 | std::unique_ptr<OpenSSLKeyPair> key_pair( |
| 256 | OpenSSLKeyPair::FromPrivateKeyPEMString(private_key)); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 257 | if (!key_pair) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 258 | RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string."; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 259 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 262 | return new OpenSSLIdentity(std::move(key_pair), std::move(cert)); |
| 263 | } |
| 264 | |
| 265 | SSLIdentity* OpenSSLIdentity::FromPEMChainStrings( |
| 266 | const std::string& private_key, |
| 267 | const std::string& certificate_chain) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 268 | BIO* bio = BIO_new_mem_buf(certificate_chain.data(), |
| 269 | rtc::dchecked_cast<int>(certificate_chain.size())); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 270 | if (!bio) |
| 271 | return nullptr; |
| 272 | BIO_set_mem_eof_return(bio, 0); |
| 273 | std::vector<std::unique_ptr<SSLCertificate>> certs; |
| 274 | while (true) { |
| 275 | X509* x509 = |
| 276 | PEM_read_bio_X509(bio, nullptr, nullptr, const_cast<char*>("\0")); |
| 277 | if (x509 == nullptr) { |
| 278 | uint32_t err = ERR_peek_error(); |
| 279 | if (ERR_GET_LIB(err) == ERR_LIB_PEM && |
| 280 | ERR_GET_REASON(err) == PEM_R_NO_START_LINE) { |
| 281 | break; |
| 282 | } |
| 283 | RTC_LOG(LS_ERROR) << "Failed to parse certificate from PEM string."; |
| 284 | BIO_free(bio); |
| 285 | return nullptr; |
| 286 | } |
| 287 | certs.emplace_back(new OpenSSLCertificate(x509)); |
| 288 | X509_free(x509); |
| 289 | } |
| 290 | BIO_free(bio); |
| 291 | if (certs.empty()) { |
| 292 | RTC_LOG(LS_ERROR) << "Found no certificates in PEM string."; |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
| 296 | std::unique_ptr<OpenSSLKeyPair> key_pair( |
| 297 | OpenSSLKeyPair::FromPrivateKeyPEMString(private_key)); |
| 298 | if (!key_pair) { |
| 299 | RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string."; |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
| 303 | return new OpenSSLIdentity(std::move(key_pair), |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 304 | std::make_unique<SSLCertChain>(std::move(certs))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 305 | } |
| 306 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 307 | const OpenSSLCertificate& OpenSSLIdentity::certificate() const { |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 308 | return *static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(0)); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 311 | const SSLCertChain& OpenSSLIdentity::cert_chain() const { |
| 312 | return *cert_chain_.get(); |
| 313 | } |
| 314 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 315 | OpenSSLIdentity* OpenSSLIdentity::GetReference() const { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 316 | return new OpenSSLIdentity(absl::WrapUnique(key_pair_->GetReference()), |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 317 | cert_chain_->Clone()); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 318 | } |
| 319 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 320 | bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) { |
| 321 | // 1 is the documented success return code. |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 322 | const OpenSSLCertificate* cert = &certificate(); |
| 323 | if (SSL_CTX_use_certificate(ctx, cert->x509()) != 1 || |
| 324 | SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 325 | openssl::LogSSLErrors("Configuring key and certificate"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 326 | return false; |
| 327 | } |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 328 | // If a chain is available, use it. |
| 329 | for (size_t i = 1; i < cert_chain_->GetSize(); ++i) { |
| 330 | cert = static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(i)); |
| 331 | if (SSL_CTX_add1_chain_cert(ctx, cert->x509()) != 1) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 332 | openssl::LogSSLErrors("Configuring intermediate certificate"); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 333 | return false; |
| 334 | } |
| 335 | } |
| 336 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 340 | std::string OpenSSLIdentity::PrivateKeyToPEMString() const { |
| 341 | return key_pair_->PrivateKeyToPEMString(); |
| 342 | } |
| 343 | |
| 344 | std::string OpenSSLIdentity::PublicKeyToPEMString() const { |
| 345 | return key_pair_->PublicKeyToPEMString(); |
| 346 | } |
| 347 | |
| 348 | bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const { |
| 349 | return *this->key_pair_ == *other.key_pair_ && |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 350 | this->certificate() == other.certificate(); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const { |
| 354 | return !(*this == other); |
| 355 | } |
| 356 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 357 | } // namespace rtc |