Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [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/ssl_certificate.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 13 | #include <memory> |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <utility> |
| 16 | |
Steve Anton | 2acd163 | 2019-03-25 20:48:30 | [diff] [blame] | 17 | #include "absl/algorithm/container.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 20 | #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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "rtc_base/ssl_fingerprint.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 27 | #include "rtc_base/third_party/base64/base64.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 28 | |
| 29 | namespace rtc { |
| 30 | |
| 31 | ////////////////////////////////////////////////////////////////////// |
| 32 | // SSLCertificateStats |
| 33 | ////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | SSLCertificateStats::SSLCertificateStats( |
| 36 | std::string&& fingerprint, |
| 37 | std::string&& fingerprint_algorithm, |
| 38 | std::string&& base64_certificate, |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 39 | std::unique_ptr<SSLCertificateStats> issuer) |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 40 | : fingerprint(std::move(fingerprint)), |
| 41 | fingerprint_algorithm(std::move(fingerprint_algorithm)), |
| 42 | base64_certificate(std::move(base64_certificate)), |
| 43 | issuer(std::move(issuer)) {} |
| 44 | |
| 45 | SSLCertificateStats::~SSLCertificateStats() {} |
| 46 | |
Henrik Boström | 69d23c9 | 2022-09-26 12:13:17 | [diff] [blame] | 47 | std::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 Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 53 | ////////////////////////////////////////////////////////////////////// |
| 54 | // SSLCertificate |
| 55 | ////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const { |
| 58 | // TODO(bemasc): Move this computation to a helper class that caches these |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 59 | // values to reduce CPU use in `StatsCollector::GetStats`. This will require |
| 60 | // adding a fast `SSLCertificate::Equals` to detect certificate changes. |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 61 | std::string digest_algorithm; |
| 62 | if (!GetSignatureDigestAlgorithm(&digest_algorithm)) |
| 63 | return nullptr; |
| 64 | |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 65 | // `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 Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 68 | // with MD5- and SHA-224-signed certificates when linked to libNSS. |
Steve Anton | 4905edb | 2018-10-16 02:27:44 | [diff] [blame] | 69 | std::unique_ptr<SSLFingerprint> ssl_fingerprint = |
| 70 | SSLFingerprint::Create(digest_algorithm, *this); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 71 | 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 Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 80 | return std::make_unique<SSLCertificateStats>(std::move(fingerprint), |
| 81 | std::move(digest_algorithm), |
| 82 | std::move(der_base64), nullptr); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 83 | } |
| 84 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 85 | ////////////////////////////////////////////////////////////////////// |
| 86 | // SSLCertChain |
| 87 | ////////////////////////////////////////////////////////////////////// |
| 88 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 89 | SSLCertChain::SSLCertChain(std::unique_ptr<SSLCertificate> single_cert) { |
| 90 | certs_.push_back(std::move(single_cert)); |
| 91 | } |
| 92 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 93 | SSLCertChain::SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs) |
| 94 | : certs_(std::move(certs)) {} |
| 95 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 96 | SSLCertChain::SSLCertChain(SSLCertChain&& rhs) = default; |
| 97 | |
| 98 | SSLCertChain& SSLCertChain::operator=(SSLCertChain&&) = default; |
| 99 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 100 | SSLCertChain::~SSLCertChain() = default; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 101 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 102 | std::unique_ptr<SSLCertChain> SSLCertChain::Clone() const { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 103 | std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size()); |
Steve Anton | 2acd163 | 2019-03-25 20:48:30 | [diff] [blame] | 104 | absl::c_transform( |
| 105 | certs_, new_certs.begin(), |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 106 | [](const std::unique_ptr<SSLCertificate>& cert) |
| 107 | -> std::unique_ptr<SSLCertificate> { return cert->Clone(); }); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 108 | return std::make_unique<SSLCertChain>(std::move(new_certs)); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | std::unique_ptr<SSLCertificateStats> SSLCertChain::GetStats() const { |
| 112 | // We have a linked list of certificates, starting with the first element of |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 113 | // `certs_` and ending with the last element of `certs_`. The "issuer" of a |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 114 | // 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 Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 117 | // The loop runs in reverse so that the `issuer` is known before the |
| 118 | // certificate issued by `issuer`. |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 119 | 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 Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 130 | std::unique_ptr<SSLCertificate> SSLCertificate::FromPEMString( |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 131 | absl::string_view pem_string) { |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 132 | #ifdef OPENSSL_IS_BORINGSSL |
| 133 | return BoringSSLCertificate::FromPEMString(pem_string); |
| 134 | #else |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 135 | return OpenSSLCertificate::FromPEMString(pem_string); |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 136 | #endif |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } // namespace rtc |