Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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/fake_ssl_identity.h" |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 13 | #include <memory> |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <utility> |
| 16 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "rtc_base/message_digest.h" |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 23 | FakeSSLCertificate::FakeSSLCertificate(absl::string_view pem_string) |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 24 | : pem_string_(pem_string), |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 25 | digest_algorithm_(DIGEST_SHA_1), |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 26 | expiration_time_(-1) {} |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 27 | |
| 28 | FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default; |
| 29 | |
| 30 | FakeSSLCertificate::~FakeSSLCertificate() = default; |
| 31 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 32 | std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 33 | return std::make_unique<FakeSSLCertificate>(*this); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | std::string FakeSSLCertificate::ToPEMString() const { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 37 | return pem_string_; |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void FakeSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 41 | std::string der_string; |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 42 | RTC_CHECK( |
| 43 | SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string)); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 44 | der_buffer->SetData(der_string.c_str(), der_string.size()); |
| 45 | } |
| 46 | |
| 47 | int64_t FakeSSLCertificate::CertificateExpirationTime() const { |
| 48 | return expiration_time_; |
| 49 | } |
| 50 | |
| 51 | void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) { |
| 52 | expiration_time_ = expiration_time; |
| 53 | } |
| 54 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 55 | void FakeSSLCertificate::set_digest_algorithm(absl::string_view algorithm) { |
| 56 | digest_algorithm_ = std::string(algorithm); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool FakeSSLCertificate::GetSignatureDigestAlgorithm( |
| 60 | std::string* algorithm) const { |
| 61 | *algorithm = digest_algorithm_; |
| 62 | return true; |
| 63 | } |
| 64 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 65 | bool FakeSSLCertificate::ComputeDigest(absl::string_view algorithm, |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 66 | unsigned char* digest, |
| 67 | size_t size, |
| 68 | size_t* length) const { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 69 | *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(), |
| 70 | pem_string_.size(), digest, size); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 71 | return (*length != 0); |
| 72 | } |
| 73 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 74 | FakeSSLIdentity::FakeSSLIdentity(absl::string_view pem_string) |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 75 | : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {} |
| 76 | |
| 77 | FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) { |
| 78 | std::vector<std::unique_ptr<SSLCertificate>> certs; |
Mirko Bonadei | 649a4c2 | 2019-01-29 09:11:53 | [diff] [blame] | 79 | certs.reserve(pem_strings.size()); |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 80 | for (const std::string& pem_string : pem_strings) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 81 | certs.push_back(std::make_unique<FakeSSLCertificate>(pem_string)); |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 82 | } |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 83 | cert_chain_ = std::make_unique<SSLCertChain>(std::move(certs)); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 84 | } |
| 85 | |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 86 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert) |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 87 | : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {} |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 88 | |
| 89 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o) |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 90 | : cert_chain_(o.cert_chain_->Clone()) {} |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 91 | |
| 92 | FakeSSLIdentity::~FakeSSLIdentity() = default; |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 93 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 94 | std::unique_ptr<SSLIdentity> FakeSSLIdentity::CloneInternal() const { |
| 95 | return std::make_unique<FakeSSLIdentity>(*this); |
| 96 | } |
| 97 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 98 | const SSLCertificate& FakeSSLIdentity::certificate() const { |
| 99 | return cert_chain_->Get(0); |
| 100 | } |
| 101 | |
| 102 | const SSLCertChain& FakeSSLIdentity::cert_chain() const { |
| 103 | return *cert_chain_.get(); |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | std::string FakeSSLIdentity::PrivateKeyToPEMString() const { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 107 | RTC_DCHECK_NOTREACHED(); // Not implemented. |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 108 | return ""; |
| 109 | } |
| 110 | |
| 111 | std::string FakeSSLIdentity::PublicKeyToPEMString() const { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 112 | RTC_DCHECK_NOTREACHED(); // Not implemented. |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 113 | return ""; |
| 114 | } |
| 115 | |
| 116 | bool FakeSSLIdentity::operator==(const SSLIdentity& other) const { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 117 | RTC_DCHECK_NOTREACHED(); // Not implemented. |
Steve Anton | 9de3aac | 2017-10-24 17:08:26 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | } // namespace rtc |