blob: d1fd57fca58a8e8e0433fac50685f95fbc4be29b [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
Henrik Boström69d23c92022-09-26 14:13:17 +020047std::unique_ptr<SSLCertificateStats> SSLCertificateStats::Copy() const {
48 return std::make_unique<SSLCertificateStats>(
49 std::string(fingerprint), std::string(fingerprint_algorithm),
50 std::string(base64_certificate), issuer ? issuer->Copy() : nullptr);
51}
52
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070053//////////////////////////////////////////////////////////////////////
54// SSLCertificate
55//////////////////////////////////////////////////////////////////////
56
57std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const {
58 // TODO(bemasc): Move this computation to a helper class that caches these
Artem Titovcfea2182021-08-10 01:22:31 +020059 // values to reduce CPU use in `StatsCollector::GetStats`. This will require
60 // adding a fast `SSLCertificate::Equals` to detect certificate changes.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070061 std::string digest_algorithm;
62 if (!GetSignatureDigestAlgorithm(&digest_algorithm))
63 return nullptr;
64
Artem Titovcfea2182021-08-10 01:22:31 +020065 // `SSLFingerprint::Create` can fail if the algorithm returned by
66 // `SSLCertificate::GetSignatureDigestAlgorithm` is not supported by the
67 // implementation of `SSLCertificate::ComputeDigest`. This currently happens
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070068 // with MD5- and SHA-224-signed certificates when linked to libNSS.
Steve Anton4905edb2018-10-15 19:27:44 -070069 std::unique_ptr<SSLFingerprint> ssl_fingerprint =
70 SSLFingerprint::Create(digest_algorithm, *this);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070071 if (!ssl_fingerprint)
72 return nullptr;
73 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
74
75 Buffer der_buffer;
76 ToDER(&der_buffer);
77 std::string der_base64;
78 Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(), &der_base64);
79
Mirko Bonadei317a1f02019-09-17 17:06:18 +020080 return std::make_unique<SSLCertificateStats>(std::move(fingerprint),
81 std::move(digest_algorithm),
82 std::move(der_base64), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070083}
84
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070085//////////////////////////////////////////////////////////////////////
86// SSLCertChain
87//////////////////////////////////////////////////////////////////////
88
Steve Antonf25303e2018-10-16 15:23:31 -070089SSLCertChain::SSLCertChain(std::unique_ptr<SSLCertificate> single_cert) {
90 certs_.push_back(std::move(single_cert));
91}
92
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070093SSLCertChain::SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs)
94 : certs_(std::move(certs)) {}
95
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070096SSLCertChain::SSLCertChain(SSLCertChain&& rhs) = default;
97
98SSLCertChain& SSLCertChain::operator=(SSLCertChain&&) = default;
99
Steve Antonf25303e2018-10-16 15:23:31 -0700100SSLCertChain::~SSLCertChain() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700101
Steve Antonf25303e2018-10-16 15:23:31 -0700102std::unique_ptr<SSLCertChain> SSLCertChain::Clone() const {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700103 std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size());
Steve Anton2acd1632019-03-25 13:48:30 -0700104 absl::c_transform(
105 certs_, new_certs.begin(),
Steve Antonf25303e2018-10-16 15:23:31 -0700106 [](const std::unique_ptr<SSLCertificate>& cert)
107 -> std::unique_ptr<SSLCertificate> { return cert->Clone(); });
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200108 return std::make_unique<SSLCertChain>(std::move(new_certs));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700109}
110
111std::unique_ptr<SSLCertificateStats> SSLCertChain::GetStats() const {
112 // We have a linked list of certificates, starting with the first element of
Artem Titov96e3b992021-07-26 16:03:14 +0200113 // `certs_` and ending with the last element of `certs_`. The "issuer" of a
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700114 // certificate is the next certificate in the chain. Stats are produced for
115 // each certificate in the list. Here, the "issuer" is the issuer's stats.
116 std::unique_ptr<SSLCertificateStats> issuer;
Artem Titov96e3b992021-07-26 16:03:14 +0200117 // The loop runs in reverse so that the `issuer` is known before the
118 // certificate issued by `issuer`.
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700119 for (ptrdiff_t i = certs_.size() - 1; i >= 0; --i) {
120 std::unique_ptr<SSLCertificateStats> new_stats = certs_[i]->GetStats();
121 if (new_stats) {
122 new_stats->issuer = std::move(issuer);
123 }
124 issuer = std::move(new_stats);
125 }
126 return issuer;
127}
128
129// static
Steve Antonf25303e2018-10-16 15:23:31 -0700130std::unique_ptr<SSLCertificate> SSLCertificate::FromPEMString(
Ali Tofigh7fa90572022-03-17 15:47:49 +0100131 absl::string_view pem_string) {
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800132#ifdef OPENSSL_IS_BORINGSSL
133 return BoringSSLCertificate::FromPEMString(pem_string);
134#else
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700135 return OpenSSLCertificate::FromPEMString(pem_string);
Taylor Brandstetter165c6182020-12-10 16:23:03 -0800136#endif
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700137}
138
139} // namespace rtc