blob: ddb1524f760098212ad9a0c1fe0e5e71c2e37bf5 [file] [log] [blame]
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001/*
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/ssl_certificate.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070012
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070014#include <string>
15#include <utility>
16
Steve Anton2acd1632019-03-25 13:48:30 -070017#include "absl/algorithm/container.h"
Ali Tofigh7fa90572022-03-17 15:47:49 +010018#include "absl/strings/string_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/checks.h"
Taylor Brandstetter165c6182020-12-10 16:23:03 -080020#include "rtc_base/openssl.h"
21#ifdef OPENSSL_IS_BORINGSSL
22#include "rtc_base/boringssl_identity.h"
23#else
24#include "rtc_base/openssl_identity.h"
25#endif
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/ssl_fingerprint.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "rtc_base/third_party/base64/base64.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070028
29namespace rtc {
30
31//////////////////////////////////////////////////////////////////////
32// SSLCertificateStats
33//////////////////////////////////////////////////////////////////////
34
35SSLCertificateStats::SSLCertificateStats(
36 std::string&& fingerprint,
37 std::string&& fingerprint_algorithm,
38 std::string&& base64_certificate,
Steve Antonf25303e2018-10-16 15:23:31 -070039 std::unique_ptr<SSLCertificateStats> issuer)
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070040 : fingerprint(std::move(fingerprint)),
41 fingerprint_algorithm(std::move(fingerprint_algorithm)),
42 base64_certificate(std::move(base64_certificate)),
43 issuer(std::move(issuer)) {}
44
45SSLCertificateStats::~SSLCertificateStats() {}
46
47//////////////////////////////////////////////////////////////////////
48// SSLCertificate
49//////////////////////////////////////////////////////////////////////
50
51std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const {
52 // TODO(bemasc): Move this computation to a helper class that caches these
Artem Titovcfea2182021-08-10 01:22:31 +020053 // values to reduce CPU use in `StatsCollector::GetStats`. This will require
54 // adding a fast `SSLCertificate::Equals` to detect certificate changes.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070055 std::string digest_algorithm;
56 if (!GetSignatureDigestAlgorithm(&digest_algorithm))
57 return nullptr;
58
Artem Titovcfea2182021-08-10 01:22:31 +020059 // `SSLFingerprint::Create` can fail if the algorithm returned by
60 // `SSLCertificate::GetSignatureDigestAlgorithm` is not supported by the
61 // implementation of `SSLCertificate::ComputeDigest`. This currently happens
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070062 // with MD5- and SHA-224-signed certificates when linked to libNSS.
Steve Anton4905edb2018-10-15 19:27:44 -070063 std::unique_ptr<SSLFingerprint> ssl_fingerprint =
64 SSLFingerprint::Create(digest_algorithm, *this);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070065 if (!ssl_fingerprint)
66 return nullptr;
67 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
68
69 Buffer der_buffer;
70 ToDER(&der_buffer);
71 std::string der_base64;
72 Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(), &der_base64);
73
Mirko Bonadei317a1f02019-09-17 17:06:18 +020074 return std::make_unique<SSLCertificateStats>(std::move(fingerprint),
75 std::move(digest_algorithm),
76 std::move(der_base64), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070077}
78
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070079//////////////////////////////////////////////////////////////////////
80// SSLCertChain
81//////////////////////////////////////////////////////////////////////
82
Steve Antonf25303e2018-10-16 15:23:31 -070083SSLCertChain::SSLCertChain(std::unique_ptr<SSLCertificate> single_cert) {
84 certs_.push_back(std::move(single_cert));
85}
86
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070087SSLCertChain::SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs)
88 : certs_(std::move(certs)) {}
89
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070090SSLCertChain::SSLCertChain(SSLCertChain&& rhs) = default;
91
92SSLCertChain& SSLCertChain::operator=(SSLCertChain&&) = default;
93
Steve Antonf25303e2018-10-16 15:23:31 -070094SSLCertChain::~SSLCertChain() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070095
Steve Antonf25303e2018-10-16 15:23:31 -070096std::unique_ptr<SSLCertChain> SSLCertChain::Clone() const {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070097 std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size());
Steve Anton2acd1632019-03-25 13:48:30 -070098 absl::c_transform(
99 certs_, new_certs.begin(),
Steve Antonf25303e2018-10-16 15:23:31 -0700100 [](const std::unique_ptr<SSLCertificate>& cert)
101 -> std::unique_ptr<SSLCertificate> { return cert->Clone(); });
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200102 return std::make_unique<SSLCertChain>(std::move(new_certs));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700103}
104
105std::unique_ptr<SSLCertificateStats> SSLCertChain::GetStats() const {
106 // We have a linked list of certificates, starting with the first element of
Artem Titov96e3b992021-07-26 16:03:14 +0200107 // `certs_` and ending with the last element of `certs_`. The "issuer" of a
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700108 // certificate is the next certificate in the chain. Stats are produced for
109 // each certificate in the list. Here, the "issuer" is the issuer's stats.
110 std::unique_ptr<SSLCertificateStats> issuer;
Artem Titov96e3b992021-07-26 16:03:14 +0200111 // The loop runs in reverse so that the `issuer` is known before the
112 // certificate issued by `issuer`.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700113 for (ptrdiff_t i = certs_.size() - 1; i >= 0; --i) {
114 std::unique_ptr<SSLCertificateStats> new_stats = certs_[i]->GetStats();
115 if (new_stats) {
116 new_stats->issuer = std::move(issuer);
117 }
118 issuer = std::move(new_stats);
119 }
120 return issuer;
121}
122
123// static
Steve Antonf25303e2018-10-16 15:23:31 -0700124std::unique_ptr<SSLCertificate> SSLCertificate::FromPEMString(
Ali Tofigh7fa90572022-03-17 15:47:49 +0100125 absl::string_view pem_string) {
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800126#ifdef OPENSSL_IS_BORINGSSL
127 return BoringSSLCertificate::FromPEMString(pem_string);
128#else
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700129 return OpenSSLCertificate::FromPEMString(pem_string);
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800130#endif
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700131}
132
133} // namespace rtc