2014-05-13 18:00:26 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
|
#include "rtc_base/openssl_identity.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
2016-04-26 03:13:22 -07:00
|
|
|
|
#include <memory>
|
2018-05-08 13:12:25 -07:00
|
|
|
|
#include <utility>
|
|
|
|
|
|
#include <vector>
|
2016-04-26 03:13:22 -07:00
|
|
|
|
|
2018-02-01 11:17:40 +01:00
|
|
|
|
#if defined(WEBRTC_WIN)
|
2014-05-13 18:00:26 +00:00
|
|
|
|
// Must be included first before openssl headers.
|
2017-09-15 06:47:31 +02:00
|
|
|
|
#include "rtc_base/win32.h" // NOLINT
|
2018-06-19 15:03:05 +02:00
|
|
|
|
#endif // WEBRTC_WIN
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
#include <openssl/pem.h>
|
2018-10-23 12:03:01 +02:00
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
|
#include "absl/memory/memory.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
|
#include "rtc_base/logging.h"
|
2018-05-23 10:22:36 +02:00
|
|
|
|
#include "rtc_base/numerics/safe_conversions.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
|
#include "rtc_base/openssl.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
|
#include "rtc_base/openssl_utility.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
2017-11-16 16:58:02 -08:00
|
|
|
|
OpenSSLIdentity::OpenSSLIdentity(
|
|
|
|
|
|
std::unique_ptr<OpenSSLKeyPair> key_pair,
|
|
|
|
|
|
std::unique_ptr<OpenSSLCertificate> certificate)
|
|
|
|
|
|
: key_pair_(std::move(key_pair)) {
|
|
|
|
|
|
RTC_DCHECK(key_pair_ != nullptr);
|
2017-02-27 14:06:41 -08:00
|
|
|
|
RTC_DCHECK(certificate != nullptr);
|
2017-11-16 16:58:02 -08:00
|
|
|
|
std::vector<std::unique_ptr<SSLCertificate>> certs;
|
|
|
|
|
|
certs.push_back(std::move(certificate));
|
|
|
|
|
|
cert_chain_.reset(new SSLCertChain(std::move(certs)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OpenSSLIdentity::OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
|
|
|
|
|
|
std::unique_ptr<SSLCertChain> cert_chain)
|
|
|
|
|
|
: key_pair_(std::move(key_pair)), cert_chain_(std::move(cert_chain)) {
|
|
|
|
|
|
RTC_DCHECK(key_pair_ != nullptr);
|
|
|
|
|
|
RTC_DCHECK(cert_chain_ != nullptr);
|
2015-03-09 22:21:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OpenSSLIdentity::~OpenSSLIdentity() = default;
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateInternal(
|
2014-05-13 18:00:26 +00:00
|
|
|
|
const SSLIdentityParams& params) {
|
2020-12-10 16:23:03 -08:00
|
|
|
|
auto key_pair = OpenSSLKeyPair::Generate(params.key_params);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
if (key_pair) {
|
2017-11-16 16:58:02 -08:00
|
|
|
|
std::unique_ptr<OpenSSLCertificate> certificate(
|
|
|
|
|
|
OpenSSLCertificate::Generate(key_pair.get(), params));
|
2020-03-20 22:51:32 +01:00
|
|
|
|
if (certificate != nullptr) {
|
|
|
|
|
|
return absl::WrapUnique(
|
|
|
|
|
|
new OpenSSLIdentity(std::move(key_pair), std::move(certificate)));
|
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
}
|
2020-12-10 16:23:03 -08:00
|
|
|
|
RTC_LOG(LS_ERROR) << "Identity generation failed";
|
2017-02-27 14:06:41 -08:00
|
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
// static
|
|
|
|
|
|
std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateWithExpiration(
|
2022-03-25 00:51:20 +01:00
|
|
|
|
absl::string_view common_name,
|
2016-03-31 16:21:04 +02:00
|
|
|
|
const KeyParams& key_params,
|
|
|
|
|
|
time_t certificate_lifetime) {
|
2014-05-13 18:00:26 +00:00
|
|
|
|
SSLIdentityParams params;
|
2015-10-08 09:42:49 -07:00
|
|
|
|
params.key_params = key_params;
|
2022-03-25 00:51:20 +01:00
|
|
|
|
params.common_name = std::string(common_name);
|
2017-02-27 14:06:41 -08:00
|
|
|
|
time_t now = time(nullptr);
|
2016-03-31 16:21:04 +02:00
|
|
|
|
params.not_before = now + kCertificateWindowInSeconds;
|
2016-02-15 09:35:54 -08:00
|
|
|
|
params.not_after = now + certificate_lifetime;
|
2024-08-09 11:56:33 -07:00
|
|
|
|
if (params.not_before > params.not_after) {
|
|
|
|
|
|
RTC_LOG(LS_ERROR)
|
|
|
|
|
|
<< "Іdentity generated failed, not_before is after not_after.";
|
2016-03-31 16:21:04 +02:00
|
|
|
|
return nullptr;
|
2024-08-09 11:56:33 -07:00
|
|
|
|
}
|
2020-03-20 22:51:32 +01:00
|
|
|
|
return CreateInternal(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<OpenSSLIdentity> OpenSSLIdentity::CreateForTest(
|
|
|
|
|
|
const SSLIdentityParams& params) {
|
|
|
|
|
|
return CreateInternal(params);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMStrings(
|
2022-03-25 00:51:20 +01:00
|
|
|
|
absl::string_view private_key,
|
|
|
|
|
|
absl::string_view certificate) {
|
2016-04-26 03:13:22 -07:00
|
|
|
|
std::unique_ptr<OpenSSLCertificate> cert(
|
2014-05-13 18:00:26 +00:00
|
|
|
|
OpenSSLCertificate::FromPEMString(certificate));
|
|
|
|
|
|
if (!cert) {
|
2017-11-09 11:09:25 +01:00
|
|
|
|
RTC_LOG(LS_ERROR) << "Failed to create OpenSSLCertificate from PEM string.";
|
2016-04-28 05:14:21 -07:00
|
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-10 16:23:03 -08:00
|
|
|
|
auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key);
|
2017-11-16 16:58:02 -08:00
|
|
|
|
if (!key_pair) {
|
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string.";
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
return absl::WrapUnique(
|
|
|
|
|
|
new OpenSSLIdentity(std::move(key_pair), std::move(cert)));
|
2017-11-16 16:58:02 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
std::unique_ptr<SSLIdentity> OpenSSLIdentity::CreateFromPEMChainStrings(
|
2022-03-25 00:51:20 +01:00
|
|
|
|
absl::string_view private_key,
|
|
|
|
|
|
absl::string_view certificate_chain) {
|
2018-06-19 15:03:05 +02:00
|
|
|
|
BIO* bio = BIO_new_mem_buf(certificate_chain.data(),
|
|
|
|
|
|
rtc::dchecked_cast<int>(certificate_chain.size()));
|
2017-11-16 16:58:02 -08:00
|
|
|
|
if (!bio)
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
BIO_set_mem_eof_return(bio, 0);
|
|
|
|
|
|
std::vector<std::unique_ptr<SSLCertificate>> certs;
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
X509* x509 =
|
|
|
|
|
|
PEM_read_bio_X509(bio, nullptr, nullptr, const_cast<char*>("\0"));
|
|
|
|
|
|
if (x509 == nullptr) {
|
|
|
|
|
|
uint32_t err = ERR_peek_error();
|
|
|
|
|
|
if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
|
|
|
|
|
|
ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
|
2024-09-09 13:39:05 -07:00
|
|
|
|
err = ERR_get_error();
|
2017-11-16 16:58:02 -08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2024-08-09 11:56:33 -07:00
|
|
|
|
RTC_LOG(LS_ERROR) << "Failed to parse certificate from PEM string: "
|
|
|
|
|
|
<< ERR_reason_error_string(err);
|
2017-11-16 16:58:02 -08:00
|
|
|
|
BIO_free(bio);
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
certs.emplace_back(new OpenSSLCertificate(x509));
|
|
|
|
|
|
X509_free(x509);
|
|
|
|
|
|
}
|
|
|
|
|
|
BIO_free(bio);
|
|
|
|
|
|
if (certs.empty()) {
|
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Found no certificates in PEM string.";
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-10 16:23:03 -08:00
|
|
|
|
auto key_pair = OpenSSLKeyPair::FromPrivateKeyPEMString(private_key);
|
2016-04-28 05:14:21 -07:00
|
|
|
|
if (!key_pair) {
|
2017-11-09 11:09:25 +01:00
|
|
|
|
RTC_LOG(LS_ERROR) << "Failed to create key pair from PEM string.";
|
2016-04-28 05:14:21 -07:00
|
|
|
|
return nullptr;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
return absl::WrapUnique(new OpenSSLIdentity(
|
|
|
|
|
|
std::move(key_pair), std::make_unique<SSLCertChain>(std::move(certs))));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-09 22:21:53 +00:00
|
|
|
|
const OpenSSLCertificate& OpenSSLIdentity::certificate() const {
|
2017-11-16 16:58:02 -08:00
|
|
|
|
return *static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(0));
|
2015-03-09 22:21:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-23 13:04:51 -08:00
|
|
|
|
const SSLCertChain& OpenSSLIdentity::cert_chain() const {
|
|
|
|
|
|
return *cert_chain_.get();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 22:51:32 +01:00
|
|
|
|
std::unique_ptr<SSLIdentity> OpenSSLIdentity::CloneInternal() const {
|
|
|
|
|
|
// We cannot use std::make_unique here because the referenced OpenSSLIdentity
|
|
|
|
|
|
// constructor is private.
|
2020-12-10 16:23:03 -08:00
|
|
|
|
return absl::WrapUnique(
|
|
|
|
|
|
new OpenSSLIdentity(key_pair_->Clone(), cert_chain_->Clone()));
|
2015-03-09 22:21:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
|
bool OpenSSLIdentity::ConfigureIdentity(SSL_CTX* ctx) {
|
|
|
|
|
|
// 1 is the documented success return code.
|
2017-11-16 16:58:02 -08:00
|
|
|
|
const OpenSSLCertificate* cert = &certificate();
|
|
|
|
|
|
if (SSL_CTX_use_certificate(ctx, cert->x509()) != 1 ||
|
|
|
|
|
|
SSL_CTX_use_PrivateKey(ctx, key_pair_->pkey()) != 1) {
|
2018-05-08 13:12:25 -07:00
|
|
|
|
openssl::LogSSLErrors("Configuring key and certificate");
|
2014-05-13 18:00:26 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-11-16 16:58:02 -08:00
|
|
|
|
// If a chain is available, use it.
|
|
|
|
|
|
for (size_t i = 1; i < cert_chain_->GetSize(); ++i) {
|
|
|
|
|
|
cert = static_cast<const OpenSSLCertificate*>(&cert_chain_->Get(i));
|
|
|
|
|
|
if (SSL_CTX_add1_chain_cert(ctx, cert->x509()) != 1) {
|
2018-05-08 13:12:25 -07:00
|
|
|
|
openssl::LogSSLErrors("Configuring intermediate certificate");
|
2017-11-16 16:58:02 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-28 05:14:21 -07:00
|
|
|
|
std::string OpenSSLIdentity::PrivateKeyToPEMString() const {
|
|
|
|
|
|
return key_pair_->PrivateKeyToPEMString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string OpenSSLIdentity::PublicKeyToPEMString() const {
|
|
|
|
|
|
return key_pair_->PublicKeyToPEMString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OpenSSLIdentity::operator==(const OpenSSLIdentity& other) const {
|
|
|
|
|
|
return *this->key_pair_ == *other.key_pair_ &&
|
2017-11-16 16:58:02 -08:00
|
|
|
|
this->certificate() == other.certificate();
|
2016-04-28 05:14:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OpenSSLIdentity::operator!=(const OpenSSLIdentity& other) const {
|
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
|
} // namespace rtc
|