xref: /aosp_15_r20/external/webrtc/rtc_base/fake_ssl_identity.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #include "rtc_base/fake_ssl_identity.h"
12 
13 #include <memory>
14 #include <string>
15 #include <utility>
16 
17 #include "absl/strings/string_view.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/message_digest.h"
20 
21 namespace rtc {
22 
FakeSSLCertificate(absl::string_view pem_string)23 FakeSSLCertificate::FakeSSLCertificate(absl::string_view pem_string)
24     : pem_string_(pem_string),
25       digest_algorithm_(DIGEST_SHA_1),
26       expiration_time_(-1) {}
27 
28 FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
29 
30 FakeSSLCertificate::~FakeSSLCertificate() = default;
31 
Clone() const32 std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const {
33   return std::make_unique<FakeSSLCertificate>(*this);
34 }
35 
ToPEMString() const36 std::string FakeSSLCertificate::ToPEMString() const {
37   return pem_string_;
38 }
39 
ToDER(Buffer * der_buffer) const40 void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
41   std::string der_string;
42   RTC_CHECK(
43       SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
44   der_buffer->SetData(der_string.c_str(), der_string.size());
45 }
46 
CertificateExpirationTime() const47 int64_t FakeSSLCertificate::CertificateExpirationTime() const {
48   return expiration_time_;
49 }
50 
SetCertificateExpirationTime(int64_t expiration_time)51 void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) {
52   expiration_time_ = expiration_time;
53 }
54 
set_digest_algorithm(absl::string_view algorithm)55 void FakeSSLCertificate::set_digest_algorithm(absl::string_view algorithm) {
56   digest_algorithm_ = std::string(algorithm);
57 }
58 
GetSignatureDigestAlgorithm(std::string * algorithm) const59 bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
60     std::string* algorithm) const {
61   *algorithm = digest_algorithm_;
62   return true;
63 }
64 
ComputeDigest(absl::string_view algorithm,unsigned char * digest,size_t size,size_t * length) const65 bool FakeSSLCertificate::ComputeDigest(absl::string_view algorithm,
66                                        unsigned char* digest,
67                                        size_t size,
68                                        size_t* length) const {
69   *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
70                                pem_string_.size(), digest, size);
71   return (*length != 0);
72 }
73 
FakeSSLIdentity(absl::string_view pem_string)74 FakeSSLIdentity::FakeSSLIdentity(absl::string_view pem_string)
75     : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
76 
FakeSSLIdentity(const std::vector<std::string> & pem_strings)77 FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
78   std::vector<std::unique_ptr<SSLCertificate>> certs;
79   certs.reserve(pem_strings.size());
80   for (const std::string& pem_string : pem_strings) {
81     certs.push_back(std::make_unique<FakeSSLCertificate>(pem_string));
82   }
83   cert_chain_ = std::make_unique<SSLCertChain>(std::move(certs));
84 }
85 
FakeSSLIdentity(const FakeSSLCertificate & cert)86 FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert)
87     : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {}
88 
FakeSSLIdentity(const FakeSSLIdentity & o)89 FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
90     : cert_chain_(o.cert_chain_->Clone()) {}
91 
92 FakeSSLIdentity::~FakeSSLIdentity() = default;
93 
CloneInternal() const94 std::unique_ptr<SSLIdentity> FakeSSLIdentity::CloneInternal() const {
95   return std::make_unique<FakeSSLIdentity>(*this);
96 }
97 
certificate() const98 const SSLCertificate& FakeSSLIdentity::certificate() const {
99   return cert_chain_->Get(0);
100 }
101 
cert_chain() const102 const SSLCertChain& FakeSSLIdentity::cert_chain() const {
103   return *cert_chain_.get();
104 }
105 
PrivateKeyToPEMString() const106 std::string FakeSSLIdentity::PrivateKeyToPEMString() const {
107   RTC_DCHECK_NOTREACHED();  // Not implemented.
108   return "";
109 }
110 
PublicKeyToPEMString() const111 std::string FakeSSLIdentity::PublicKeyToPEMString() const {
112   RTC_DCHECK_NOTREACHED();  // Not implemented.
113   return "";
114 }
115 
operator ==(const SSLIdentity & other) const116 bool FakeSSLIdentity::operator==(const SSLIdentity& other) const {
117   RTC_DCHECK_NOTREACHED();  // Not implemented.
118   return false;
119 }
120 
121 }  // namespace rtc
122