blob: 186497836d5ba67f279c4d9d3912febb1522ede8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/openssl_identity.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070014#include <utility>
15#include <vector>
jbauch555604a2016-04-26 03:13:22 -070016
Mirko Bonadeie0623852018-02-01 11:17:40 +010017#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018// Must be included first before openssl headers.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/win32.h" // NOLINT
Yves Gerey665174f2018-06-19 15:03:05 +020020#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22#include <openssl/bio.h>
23#include <openssl/err.h>
24#include <openssl/pem.h>
Yves Gerey988cc082018-10-23 12:03:01 +020025#include <stdint.h>
26
Karl Wiberg918f50c2018-07-05 11:40:33 +020027#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/logging.h"
Mirko Bonadeia041f922018-05-23 10:22:36 +020030#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/openssl.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/openssl_utility.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
34namespace rtc {
35
Jian Cui0a8798b2017-11-16 16:58:02 -080036OpenSSLIdentity::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);
deadbeef37f5ecf2017-02-27 14:06:41 -080041 RTC_DCHECK(certificate != nullptr);
Jian Cui0a8798b2017-11-16 16:58:02 -080042 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
47OpenSSLIdentity::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.org67186fe2015-03-09 22:21:53 +000052}
53
54OpenSSLIdentity::~OpenSSLIdentity() = default;
55
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010056std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateInternal(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 const SSLIdentityParams& params) {
Taylor Brandstetter165c6182020-12-10 16:23:03 -080058 auto key_pair = OpenSSLKeyPair::Generate(params.key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 if (key_pair) {
Jian Cui0a8798b2017-11-16 16:58:02 -080060 std::unique_ptr<OpenSSLCertificate> certificate(
61 OpenSSLCertificate::Generate(key_pair.get(), params));
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010062 if (certificate != nullptr) {
63 return absl::WrapUnique(
64 new OpenSSLIdentity(std::move(key_pair), std::move(certificate)));
65 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 }
Taylor Brandstetter165c6182020-12-10 16:23:03 -080067 RTC_LOG(LS_ERROR) << "Identity generation failed";
deadbeef37f5ecf2017-02-27 14:06:41 -080068 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069}
70
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010071// static
72std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateWithExpiration(
Ali Tofigh58d861c2022-03-25 00:51:20 +010073 absl::string_view common_name,
Torbjorn Granlund1d846b22016-03-31 16:21:04 +020074 const KeyParams& key_params,
75 time_t certificate_lifetime) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 SSLIdentityParams params;
torbjorng4e572472015-10-08 09:42:49 -070077 params.key_params = key_params;
Ali Tofigh58d861c2022-03-25 00:51:20 +010078 params.common_name = std::string(common_name);
deadbeef37f5ecf2017-02-27 14:06:41 -080079 time_t now = time(nullptr);
Torbjorn Granlund1d846b22016-03-31 16:21:04 +020080 params.not_before = now + kCertificateWindowInSeconds;
torbjornge8dc0812016-02-15 09:35:54 -080081 params.not_after = now + certificate_lifetime;
Torbjorn Granlund1d846b22016-03-31 16:21:04 +020082 if (params.not_before > params.not_after)
83 return nullptr;
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010084 return CreateInternal(params);
85}
86
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010087std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateForTest(
88 const SSLIdentityParams& params) {
89 return CreateInternal(params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090}
91
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010092std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMStrings(
Ali Tofigh58d861c2022-03-25 00:51:20 +010093 absl::string_view private_key,
94 absl::string_view certificate) {
jbauch555604a2016-04-26 03:13:22 -070095 std::unique_ptr<OpenSSLCertificate> cert(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 OpenSSLCertificate::FromPEMString(certificate));
97 if (!cert) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010098 RTC_LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
hbos6b470a92016-04-28 05:14:21 -070099 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 }
101
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800102 auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key);
hbos6b470a92016-04-28 05:14:21 -0700103 if (!key_pair) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string.";
hbos6b470a92016-04-28 05:14:21 -0700105 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 }
107
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100108 return absl::WrapUnique(
109 new OpenSSLIdentity(std::move(key_pair), std::move(cert)));
Jian Cui0a8798b2017-11-16 16:58:02 -0800110}
111
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100112std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMChainStrings(
Ali Tofigh58d861c2022-03-25 00:51:20 +0100113 absl::string_view private_key,
114 absl::string_view certificate_chain) {
Yves Gerey665174f2018-06-19 15:03:05 +0200115 BIO* bio = BIO_new_mem_buf(certificate_chain.data(),
116 rtc::dchecked_cast<int>(certificate_chain.size()));
Jian Cui0a8798b2017-11-16 16:58:02 -0800117 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 Brandstetter165c6182020-12-10 16:23:03 -0800143 auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key);
Jian Cui0a8798b2017-11-16 16:58:02 -0800144 if (!key_pair) {
145 RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string.";
146 return nullptr;
147 }
148
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100149 return absl::WrapUnique(new OpenSSLIdentity(
150 std::move(key_pair), std::make_unique<SSLCertChain>(std::move(certs))));
151}
152
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000153const OpenSSLCertificate& OpenSSLIdentity::certificate() const {
Jian Cui0a8798b2017-11-16 16:58:02 -0800154 return *static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(0));
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000155}
156
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800157const SSLCertChain& OpenSSLIdentity::cert_chain() const {
158 return *cert_chain_.get();
159}
160
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100161std::unique_ptr<SSLIdentity> OpenSSLIdentity::CloneInternal() const {
162 // We cannot use std::make_unique here because the referenced OpenSSLIdentity
163 // constructor is private.
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800164 return absl::WrapUnique(
165 new OpenSSLIdentity(key_pair_->Clone(), cert_chain_->Clone()));
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000166}
167
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) {
169 // 1 is the documented success return code.
Jian Cui0a8798b2017-11-16 16:58:02 -0800170 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 Wrightd6f86e82018-05-08 13:12:25 -0700173 openssl::LogSSLErrors("Configuring key and certificate");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 return false;
175 }
Jian Cui0a8798b2017-11-16 16:58:02 -0800176 // 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 Wrightd6f86e82018-05-08 13:12:25 -0700180 openssl::LogSSLErrors("Configuring intermediate certificate");
Jian Cui0a8798b2017-11-16 16:58:02 -0800181 return false;
182 }
183 }
184
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185 return true;
186}
187
hbos6b470a92016-04-28 05:14:21 -0700188std::string OpenSSLIdentity::PrivateKeyToPEMString() const {
189 return key_pair_->PrivateKeyToPEMString();
190}
191
192std::string OpenSSLIdentity::PublicKeyToPEMString() const {
193 return key_pair_->PublicKeyToPEMString();
194}
195
196bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const {
197 return *this->key_pair_ == *other.key_pair_ &&
Jian Cui0a8798b2017-11-16 16:58:02 -0800198 this->certificate() == other.certificate();
hbos6b470a92016-04-28 05:14:21 -0700199}
200
201bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const {
202 return !(*this == other);
203}
204
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205} // namespace rtc