blob: 80a3e788878441ed1dca6897863c3db9b121362b [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
11#include "rtc_base/fakesslidentity.h"
12
13#include <algorithm>
14#include <string>
15#include <utility>
16
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Steve Anton9de3aac2017-10-24 10:08:26 -070018#include "rtc_base/checks.h"
19#include "rtc_base/messagedigest.h"
Steve Anton9de3aac2017-10-24 10:08:26 -070020
21namespace rtc {
22
Taylor Brandstetterc3928662018-02-23 13:04:51 -080023FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string)
24 : pem_string_(pem_string),
Steve Anton9de3aac2017-10-24 10:08:26 -070025 digest_algorithm_(DIGEST_SHA_1),
Taylor Brandstetterc3928662018-02-23 13:04:51 -080026 expiration_time_(-1) {}
Steve Anton9de3aac2017-10-24 10:08:26 -070027
28FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
29
30FakeSSLCertificate::~FakeSSLCertificate() = default;
31
32FakeSSLCertificate* FakeSSLCertificate::GetReference() const {
33 return new FakeSSLCertificate(*this);
34}
35
36std::string FakeSSLCertificate::ToPEMString() const {
Taylor Brandstetterc3928662018-02-23 13:04:51 -080037 return pem_string_;
Steve Anton9de3aac2017-10-24 10:08:26 -070038}
39
40void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
41 std::string der_string;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080042 RTC_CHECK(
43 SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
Steve Anton9de3aac2017-10-24 10:08:26 -070044 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
55void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) {
56 digest_algorithm_ = algorithm;
57}
58
59bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
60 std::string* algorithm) const {
61 *algorithm = digest_algorithm_;
62 return true;
63}
64
65bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm,
66 unsigned char* digest,
67 size_t size,
68 size_t* length) const {
Taylor Brandstetterc3928662018-02-23 13:04:51 -080069 *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
70 pem_string_.size(), digest, size);
Steve Anton9de3aac2017-10-24 10:08:26 -070071 return (*length != 0);
72}
73
Taylor Brandstetterc3928662018-02-23 13:04:51 -080074FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string)
75 : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
76
77FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
78 std::vector<std::unique_ptr<SSLCertificate>> certs;
79 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)
Karl Wiberg918f50c2018-07-05 11:40:33 +020086 : cert_chain_(absl::make_unique<SSLCertChain>(&cert)) {}
Taylor Brandstetterc3928662018-02-23 13:04:51 -080087
88FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
89 : cert_chain_(o.cert_chain_->UniqueCopy()) {}
90
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