• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rtc_base/checks.h"
18 #include "rtc_base/message_digest.h"
19 
20 namespace rtc {
21 
FakeSSLCertificate(const std::string & pem_string)22 FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string)
23     : pem_string_(pem_string),
24       digest_algorithm_(DIGEST_SHA_1),
25       expiration_time_(-1) {}
26 
27 FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
28 
29 FakeSSLCertificate::~FakeSSLCertificate() = default;
30 
Clone() const31 std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const {
32   return std::make_unique<FakeSSLCertificate>(*this);
33 }
34 
ToPEMString() const35 std::string FakeSSLCertificate::ToPEMString() const {
36   return pem_string_;
37 }
38 
ToDER(Buffer * der_buffer) const39 void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
40   std::string der_string;
41   RTC_CHECK(
42       SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
43   der_buffer->SetData(der_string.c_str(), der_string.size());
44 }
45 
CertificateExpirationTime() const46 int64_t FakeSSLCertificate::CertificateExpirationTime() const {
47   return expiration_time_;
48 }
49 
SetCertificateExpirationTime(int64_t expiration_time)50 void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) {
51   expiration_time_ = expiration_time;
52 }
53 
set_digest_algorithm(const std::string & algorithm)54 void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) {
55   digest_algorithm_ = algorithm;
56 }
57 
GetSignatureDigestAlgorithm(std::string * algorithm) const58 bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
59     std::string* algorithm) const {
60   *algorithm = digest_algorithm_;
61   return true;
62 }
63 
ComputeDigest(const std::string & algorithm,unsigned char * digest,size_t size,size_t * length) const64 bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm,
65                                        unsigned char* digest,
66                                        size_t size,
67                                        size_t* length) const {
68   *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
69                                pem_string_.size(), digest, size);
70   return (*length != 0);
71 }
72 
FakeSSLIdentity(const std::string & pem_string)73 FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string)
74     : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
75 
FakeSSLIdentity(const std::vector<std::string> & pem_strings)76 FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
77   std::vector<std::unique_ptr<SSLCertificate>> certs;
78   certs.reserve(pem_strings.size());
79   for (const std::string& pem_string : pem_strings) {
80     certs.push_back(std::make_unique<FakeSSLCertificate>(pem_string));
81   }
82   cert_chain_ = std::make_unique<SSLCertChain>(std::move(certs));
83 }
84 
FakeSSLIdentity(const FakeSSLCertificate & cert)85 FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert)
86     : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {}
87 
FakeSSLIdentity(const FakeSSLIdentity & o)88 FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
89     : cert_chain_(o.cert_chain_->Clone()) {}
90 
91 FakeSSLIdentity::~FakeSSLIdentity() = default;
92 
CloneInternal() const93 std::unique_ptr<SSLIdentity> FakeSSLIdentity::CloneInternal() const {
94   return std::make_unique<FakeSSLIdentity>(*this);
95 }
96 
certificate() const97 const SSLCertificate& FakeSSLIdentity::certificate() const {
98   return cert_chain_->Get(0);
99 }
100 
cert_chain() const101 const SSLCertChain& FakeSSLIdentity::cert_chain() const {
102   return *cert_chain_.get();
103 }
104 
PrivateKeyToPEMString() const105 std::string FakeSSLIdentity::PrivateKeyToPEMString() const {
106   RTC_NOTREACHED();  // Not implemented.
107   return "";
108 }
109 
PublicKeyToPEMString() const110 std::string FakeSSLIdentity::PublicKeyToPEMString() const {
111   RTC_NOTREACHED();  // Not implemented.
112   return "";
113 }
114 
operator ==(const SSLIdentity & other) const115 bool FakeSSLIdentity::operator==(const SSLIdentity& other) const {
116   RTC_NOTREACHED();  // Not implemented.
117   return false;
118 }
119 
120 }  // namespace rtc
121