henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/openssl_identity.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 13 | #include <memory> |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 14 | #include <utility> |
| 15 | #include <vector> |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 16 | |
Mirko Bonadei | e062385 | 2018-02-01 10:17:40 | [diff] [blame] | 17 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | // Must be included first before openssl headers. |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "rtc_base/win32.h" // NOLINT |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 20 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/pem.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | |
Karl Wiberg | 918f50c | 2018-07-05 09:40:33 | [diff] [blame] | 27 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 29 | #include "rtc_base/logging.h" |
Mirko Bonadei | a041f92 | 2018-05-23 08:22:36 | [diff] [blame] | 30 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 31 | #include "rtc_base/openssl.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 32 | #include "rtc_base/openssl_utility.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 33 | |
| 34 | namespace rtc { |
| 35 | |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 36 | OpenSSLIdentity::OpenSSLIdentity( |
| 37 | std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 38 | std::unique_ptr<OpenSSLCertificate> certificate) |
| 39 | : key_pair_(std::move(key_pair)) { |
| 40 | RTC_DCHECK(key_pair_ != nullptr); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 41 | RTC_DCHECK(certificate != nullptr); |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 42 | std::vector<std::unique_ptr<SSLCertificate>> certs; |
| 43 | certs.push_back(std::move(certificate)); |
| 44 | cert_chain_.reset(new SSLCertChain(std::move(certs))); |
| 45 | } |
| 46 | |
| 47 | OpenSSLIdentity::OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 48 | std::unique_ptr<SSLCertChain> cert_chain) |
| 49 | : key_pair_(std::move(key_pair)), cert_chain_(std::move(cert_chain)) { |
| 50 | RTC_DCHECK(key_pair_ != nullptr); |
| 51 | RTC_DCHECK(cert_chain_ != nullptr); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | OpenSSLIdentity::~OpenSSLIdentity() = default; |
| 55 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 56 | std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateInternal( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 57 | const SSLIdentityParams& params) { |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 58 | auto key_pair = OpenSSLKeyPair::Generate(params.key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 59 | if (key_pair) { |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 60 | std::unique_ptr<OpenSSLCertificate> certificate( |
| 61 | OpenSSLCertificate::Generate(key_pair.get(), params)); |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 62 | if (certificate != nullptr) { |
| 63 | return absl::WrapUnique( |
| 64 | new OpenSSLIdentity(std::move(key_pair), std::move(certificate))); |
| 65 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 66 | } |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 67 | RTC_LOG(LS_ERROR) << "Identity generation failed"; |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 68 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 69 | } |
| 70 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 71 | // static |
| 72 | std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateWithExpiration( |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 73 | absl::string_view common_name, |
Torbjorn Granlund | 1d846b2 | 2016-03-31 14:21:04 | [diff] [blame] | 74 | const KeyParams& key_params, |
| 75 | time_t certificate_lifetime) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | SSLIdentityParams params; |
torbjorng | 4e57247 | 2015-10-08 16:42:49 | [diff] [blame] | 77 | params.key_params = key_params; |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 78 | params.common_name = std::string(common_name); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 79 | time_t now = time(nullptr); |
Torbjorn Granlund | 1d846b2 | 2016-03-31 14:21:04 | [diff] [blame] | 80 | params.not_before = now + kCertificateWindowInSeconds; |
torbjorng | e8dc081 | 2016-02-15 17:35:54 | [diff] [blame] | 81 | params.not_after = now + certificate_lifetime; |
Torbjorn Granlund | 1d846b2 | 2016-03-31 14:21:04 | [diff] [blame] | 82 | if (params.not_before > params.not_after) |
| 83 | return nullptr; |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 84 | return CreateInternal(params); |
| 85 | } |
| 86 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 87 | std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateForTest( |
| 88 | const SSLIdentityParams& params) { |
| 89 | return CreateInternal(params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 90 | } |
| 91 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 92 | std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMStrings( |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 93 | absl::string_view private_key, |
| 94 | absl::string_view certificate) { |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 95 | std::unique_ptr<OpenSSLCertificate> cert( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 96 | OpenSSLCertificate::FromPEMString(certificate)); |
| 97 | if (!cert) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 98 | RTC_LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string."; |
hbos | 6b470a9 | 2016-04-28 12:14:21 | [diff] [blame] | 99 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 100 | } |
| 101 | |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 102 | auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key); |
hbos | 6b470a9 | 2016-04-28 12:14:21 | [diff] [blame] | 103 | if (!key_pair) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 104 | RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string."; |
hbos | 6b470a9 | 2016-04-28 12:14:21 | [diff] [blame] | 105 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 106 | } |
| 107 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 108 | return absl::WrapUnique( |
| 109 | new OpenSSLIdentity(std::move(key_pair), std::move(cert))); |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 110 | } |
| 111 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 112 | std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMChainStrings( |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 113 | absl::string_view private_key, |
| 114 | absl::string_view certificate_chain) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 115 | BIO* bio = BIO_new_mem_buf(certificate_chain.data(), |
| 116 | rtc::dchecked_cast<int>(certificate_chain.size())); |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 117 | if (!bio) |
| 118 | return nullptr; |
| 119 | BIO_set_mem_eof_return(bio, 0); |
| 120 | std::vector<std::unique_ptr<SSLCertificate>> certs; |
| 121 | while (true) { |
| 122 | X509* x509 = |
| 123 | PEM_read_bio_X509(bio, nullptr, nullptr, const_cast<char*>("\0")); |
| 124 | if (x509 == nullptr) { |
| 125 | uint32_t err = ERR_peek_error(); |
| 126 | if (ERR_GET_LIB(err) == ERR_LIB_PEM && |
| 127 | ERR_GET_REASON(err) == PEM_R_NO_START_LINE) { |
| 128 | break; |
| 129 | } |
| 130 | RTC_LOG(LS_ERROR) << "Failed to parse certificate from PEM string."; |
| 131 | BIO_free(bio); |
| 132 | return nullptr; |
| 133 | } |
| 134 | certs.emplace_back(new OpenSSLCertificate(x509)); |
| 135 | X509_free(x509); |
| 136 | } |
| 137 | BIO_free(bio); |
| 138 | if (certs.empty()) { |
| 139 | RTC_LOG(LS_ERROR) << "Found no certificates in PEM string."; |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 143 | auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key); |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 144 | if (!key_pair) { |
| 145 | RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string."; |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 149 | return absl::WrapUnique(new OpenSSLIdentity( |
| 150 | std::move(key_pair), std::make_unique<SSLCertChain>(std::move(certs)))); |
| 151 | } |
| 152 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 153 | const OpenSSLCertificate& OpenSSLIdentity::certificate() const { |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 154 | return *static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(0)); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 155 | } |
| 156 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 157 | const SSLCertChain& OpenSSLIdentity::cert_chain() const { |
| 158 | return *cert_chain_.get(); |
| 159 | } |
| 160 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 161 | std::unique_ptr<SSLIdentity> OpenSSLIdentity::CloneInternal() const { |
| 162 | // We cannot use std::make_unique here because the referenced OpenSSLIdentity |
| 163 | // constructor is private. |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 164 | return absl::WrapUnique( |
| 165 | new OpenSSLIdentity(key_pair_->Clone(), cert_chain_->Clone())); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 166 | } |
| 167 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 168 | bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) { |
| 169 | // 1 is the documented success return code. |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 170 | const OpenSSLCertificate* cert = &certificate(); |
| 171 | if (SSL_CTX_use_certificate(ctx, cert->x509()) != 1 || |
| 172 | SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 173 | openssl::LogSSLErrors("Configuring key and certificate"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 174 | return false; |
| 175 | } |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 176 | // If a chain is available, use it. |
| 177 | for (size_t i = 1; i < cert_chain_->GetSize(); ++i) { |
| 178 | cert = static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(i)); |
| 179 | if (SSL_CTX_add1_chain_cert(ctx, cert->x509()) != 1) { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 180 | openssl::LogSSLErrors("Configuring intermediate certificate"); |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
hbos | 6b470a9 | 2016-04-28 12:14:21 | [diff] [blame] | 188 | std::string OpenSSLIdentity::PrivateKeyToPEMString() const { |
| 189 | return key_pair_->PrivateKeyToPEMString(); |
| 190 | } |
| 191 | |
| 192 | std::string OpenSSLIdentity::PublicKeyToPEMString() const { |
| 193 | return key_pair_->PublicKeyToPEMString(); |
| 194 | } |
| 195 | |
| 196 | bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const { |
| 197 | return *this->key_pair_ == *other.key_pair_ && |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 198 | this->certificate() == other.certificate(); |
hbos | 6b470a9 | 2016-04-28 12:14:21 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const { |
| 202 | return !(*this == other); |
| 203 | } |
| 204 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 205 | } // namespace rtc |