blob: 480922c6bda1bd46796b1b1d0512ead0fc222e91 [file] [log] [blame]
Steve Anton9de3aac2017-10-24 10:08:26 -07001/*
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 09:11:00 -080011#include "rtc_base/fake_ssl_identity.h"
Steve Anton9de3aac2017-10-24 10:08:26 -070012
Steve Anton9de3aac2017-10-24 10:08:26 -070013#include <string>
14#include <utility>
15
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Steve Anton9de3aac2017-10-24 10:08:26 -070017#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/message_digest.h"
Steve Anton9de3aac2017-10-24 10:08:26 -070019
20namespace rtc {
21
Taylor Brandstetterc3928662018-02-23 13:04:51 -080022FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string)
23 : pem_string_(pem_string),
Steve Anton9de3aac2017-10-24 10:08:26 -070024 digest_algorithm_(DIGEST_SHA_1),
Taylor Brandstetterc3928662018-02-23 13:04:51 -080025 expiration_time_(-1) {}
Steve Anton9de3aac2017-10-24 10:08:26 -070026
27FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
28
29FakeSSLCertificate::~FakeSSLCertificate() = default;
30
Steve Antonf25303e2018-10-16 15:23:31 -070031std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const {
32 return absl::make_unique<FakeSSLCertificate>(*this);
Steve Anton9de3aac2017-10-24 10:08:26 -070033}
34
35std::string FakeSSLCertificate::ToPEMString() const {
Taylor Brandstetterc3928662018-02-23 13:04:51 -080036 return pem_string_;
Steve Anton9de3aac2017-10-24 10:08:26 -070037}
38
39void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
40 std::string der_string;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080041 RTC_CHECK(
42 SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
Steve Anton9de3aac2017-10-24 10:08:26 -070043 der_buffer->SetData(der_string.c_str(), der_string.size());
44}
45
46int64_t FakeSSLCertificate::CertificateExpirationTime() const {
47 return expiration_time_;
48}
49
50void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) {
51 expiration_time_ = expiration_time;
52}
53
54void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) {
55 digest_algorithm_ = algorithm;
56}
57
58bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
59 std::string* algorithm) const {
60 *algorithm = digest_algorithm_;
61 return true;
62}
63
64bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm,
65 unsigned char* digest,
66 size_t size,
67 size_t* length) const {
Taylor Brandstetterc3928662018-02-23 13:04:51 -080068 *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
69 pem_string_.size(), digest, size);
Steve Anton9de3aac2017-10-24 10:08:26 -070070 return (*length != 0);
71}
72
Taylor Brandstetterc3928662018-02-23 13:04:51 -080073FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string)
74 : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
75
76FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
77 std::vector<std::unique_ptr<SSLCertificate>> certs;
Mirko Bonadei649a4c22019-01-29 10:11:53 +010078 certs.reserve(pem_strings.size());
Taylor Brandstetterc3928662018-02-23 13:04:51 -080079 for (const std::string& pem_string : pem_strings) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020080 certs.push_back(absl::make_unique<FakeSSLCertificate>(pem_string));
Taylor Brandstetterc3928662018-02-23 13:04:51 -080081 }
Karl Wiberg918f50c2018-07-05 11:40:33 +020082 cert_chain_ = absl::make_unique<SSLCertChain>(std::move(certs));
Steve Anton9de3aac2017-10-24 10:08:26 -070083}
84
Steve Anton9de3aac2017-10-24 10:08:26 -070085FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert)
Steve Antonf25303e2018-10-16 15:23:31 -070086 : cert_chain_(absl::make_unique<SSLCertChain>(cert.Clone())) {}
Taylor Brandstetterc3928662018-02-23 13:04:51 -080087
88FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
Steve Antonf25303e2018-10-16 15:23:31 -070089 : cert_chain_(o.cert_chain_->Clone()) {}
Taylor Brandstetterc3928662018-02-23 13:04:51 -080090
91FakeSSLIdentity::~FakeSSLIdentity() = default;
Steve Anton9de3aac2017-10-24 10:08:26 -070092
93FakeSSLIdentity* FakeSSLIdentity::GetReference() const {
94 return new FakeSSLIdentity(*this);
95}
96
Taylor Brandstetterc3928662018-02-23 13:04:51 -080097const SSLCertificate& FakeSSLIdentity::certificate() const {
98 return cert_chain_->Get(0);
99}
100
101const SSLCertChain& FakeSSLIdentity::cert_chain() const {
102 return *cert_chain_.get();
Steve Anton9de3aac2017-10-24 10:08:26 -0700103}
104
105std::string FakeSSLIdentity::PrivateKeyToPEMString() const {
106 RTC_NOTREACHED(); // Not implemented.
107 return "";
108}
109
110std::string FakeSSLIdentity::PublicKeyToPEMString() const {
111 RTC_NOTREACHED(); // Not implemented.
112 return "";
113}
114
115bool FakeSSLIdentity::operator==(const SSLIdentity& other) const {
116 RTC_NOTREACHED(); // Not implemented.
117 return false;
118}
119
120} // namespace rtc