blob: 73c843a2e705fb80694c402e580746204e31b62c [file] [log] [blame]
Steve Anton9de3aac2017-10-24 17:08:261/*
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 Anton10542f22019-01-11 17:11:0011#include "rtc_base/fake_ssl_identity.h"
Steve Anton9de3aac2017-10-24 17:08:2612
Mirko Bonadei317a1f02019-09-17 15:06:1813#include <memory>
Steve Anton9de3aac2017-10-24 17:08:2614#include <string>
15#include <utility>
16
Ali Tofigh7fa90572022-03-17 14:47:4917#include "absl/strings/string_view.h"
Steve Anton9de3aac2017-10-24 17:08:2618#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/message_digest.h"
Steve Anton9de3aac2017-10-24 17:08:2620
21namespace rtc {
22
Ali Tofigh7fa90572022-03-17 14:47:4923FakeSSLCertificate::FakeSSLCertificate(absl::string_view pem_string)
Taylor Brandstetterc3928662018-02-23 21:04:5124 : pem_string_(pem_string),
Steve Anton9de3aac2017-10-24 17:08:2625 digest_algorithm_(DIGEST_SHA_1),
Taylor Brandstetterc3928662018-02-23 21:04:5126 expiration_time_(-1) {}
Steve Anton9de3aac2017-10-24 17:08:2627
28FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
29
30FakeSSLCertificate::~FakeSSLCertificate() = default;
31
Steve Antonf25303e2018-10-16 22:23:3132std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const {
Mirko Bonadei317a1f02019-09-17 15:06:1833 return std::make_unique<FakeSSLCertificate>(*this);
Steve Anton9de3aac2017-10-24 17:08:2634}
35
36std::string FakeSSLCertificate::ToPEMString() const {
Taylor Brandstetterc3928662018-02-23 21:04:5137 return pem_string_;
Steve Anton9de3aac2017-10-24 17:08:2638}
39
40void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
41 std::string der_string;
Taylor Brandstetterc3928662018-02-23 21:04:5142 RTC_CHECK(
43 SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
Steve Anton9de3aac2017-10-24 17:08:2644 der_buffer->SetData(der_string.c_str(), der_string.size());
45}
46
47int64_t FakeSSLCertificate::CertificateExpirationTime() const {
48 return expiration_time_;
49}
50
51void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) {
52 expiration_time_ = expiration_time;
53}
54
Ali Tofigh7fa90572022-03-17 14:47:4955void FakeSSLCertificate::set_digest_algorithm(absl::string_view algorithm) {
56 digest_algorithm_ = std::string(algorithm);
Steve Anton9de3aac2017-10-24 17:08:2657}
58
59bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
60 std::string* algorithm) const {
61 *algorithm = digest_algorithm_;
62 return true;
63}
64
Ali Tofigh7fa90572022-03-17 14:47:4965bool FakeSSLCertificate::ComputeDigest(absl::string_view algorithm,
Steve Anton9de3aac2017-10-24 17:08:2666 unsigned char* digest,
67 size_t size,
68 size_t* length) const {
Taylor Brandstetterc3928662018-02-23 21:04:5169 *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
70 pem_string_.size(), digest, size);
Steve Anton9de3aac2017-10-24 17:08:2671 return (*length != 0);
72}
73
Ali Tofigh7fa90572022-03-17 14:47:4974FakeSSLIdentity::FakeSSLIdentity(absl::string_view pem_string)
Taylor Brandstetterc3928662018-02-23 21:04:5175 : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
76
77FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
78 std::vector<std::unique_ptr<SSLCertificate>> certs;
Mirko Bonadei649a4c22019-01-29 09:11:5379 certs.reserve(pem_strings.size());
Taylor Brandstetterc3928662018-02-23 21:04:5180 for (const std::string& pem_string : pem_strings) {
Mirko Bonadei317a1f02019-09-17 15:06:1881 certs.push_back(std::make_unique<FakeSSLCertificate>(pem_string));
Taylor Brandstetterc3928662018-02-23 21:04:5182 }
Mirko Bonadei317a1f02019-09-17 15:06:1883 cert_chain_ = std::make_unique<SSLCertChain>(std::move(certs));
Steve Anton9de3aac2017-10-24 17:08:2684}
85
Steve Anton9de3aac2017-10-24 17:08:2686FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert)
Mirko Bonadei317a1f02019-09-17 15:06:1887 : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {}
Taylor Brandstetterc3928662018-02-23 21:04:5188
89FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
Steve Antonf25303e2018-10-16 22:23:3190 : cert_chain_(o.cert_chain_->Clone()) {}
Taylor Brandstetterc3928662018-02-23 21:04:5191
92FakeSSLIdentity::~FakeSSLIdentity() = default;
Steve Anton9de3aac2017-10-24 17:08:2693
Harald Alvestrand8515d5a2020-03-20 21:51:3294std::unique_ptr<SSLIdentity> FakeSSLIdentity::CloneInternal() const {
95 return std::make_unique<FakeSSLIdentity>(*this);
96}
97
Taylor Brandstetterc3928662018-02-23 21:04:5198const SSLCertificate& FakeSSLIdentity::certificate() const {
99 return cert_chain_->Get(0);
100}
101
102const SSLCertChain& FakeSSLIdentity::cert_chain() const {
103 return *cert_chain_.get();
Steve Anton9de3aac2017-10-24 17:08:26104}
105
106std::string FakeSSLIdentity::PrivateKeyToPEMString() const {
Artem Titovd3251962021-11-15 15:57:07107 RTC_DCHECK_NOTREACHED(); // Not implemented.
Steve Anton9de3aac2017-10-24 17:08:26108 return "";
109}
110
111std::string FakeSSLIdentity::PublicKeyToPEMString() const {
Artem Titovd3251962021-11-15 15:57:07112 RTC_DCHECK_NOTREACHED(); // Not implemented.
Steve Anton9de3aac2017-10-24 17:08:26113 return "";
114}
115
116bool FakeSSLIdentity::operator==(const SSLIdentity& other) const {
Artem Titovd3251962021-11-15 15:57:07117 RTC_DCHECK_NOTREACHED(); // Not implemented.
Steve Anton9de3aac2017-10-24 17:08:26118 return false;
119}
120
121} // namespace rtc