| 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 |
| 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 |
| 31 | std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const { |
| 32 | return std::make_unique<FakeSSLCertificate>(*this); |
| 33 | } |
| 34 |
| 35 | std::string FakeSSLCertificate::ToPEMString() const { |
| 36 | return pem_string_; |
| 37 | } |
| 38 |
| 39 | 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 |
| 46 | int64_t FakeSSLCertificate::CertificateExpirationTime() const { |
| 47 | return expiration_time_; |
| 48 | } |
| 49 |
| 50 | void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) { |
| 51 | expiration_time_ = expiration_time; |
| 52 | } |
| 53 |
| 54 | void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) { |
| 55 | digest_algorithm_ = algorithm; |
| 56 | } |
| 57 |
| 58 | bool FakeSSLCertificate::GetSignatureDigestAlgorithm( |
| 59 | std::string* algorithm) const { |
| 60 | *algorithm = digest_algorithm_; |
| 61 | return true; |
| 62 | } |
| 63 |
| 64 | 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 |
| 73 | FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string) |
| 74 | : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {} |
| 75 |
| 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 |
| 85 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert) |
| 86 | : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {} |
| 87 |
| 88 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o) |
| 89 | : cert_chain_(o.cert_chain_->Clone()) {} |
| 90 |
| 91 | FakeSSLIdentity::~FakeSSLIdentity() = default; |
| 92 |
| 93 | std::unique_ptr<SSLIdentity> FakeSSLIdentity::CloneInternal() const { |
| 94 | return std::make_unique<FakeSSLIdentity>(*this); |
| 95 | } |
| 96 |
| 97 | const SSLCertificate& FakeSSLIdentity::certificate() const { |
| 98 | return cert_chain_->Get(0); |
| 99 | } |
| 100 |
| 101 | const SSLCertChain& FakeSSLIdentity::cert_chain() const { |
| 102 | return *cert_chain_.get(); |
| 103 | } |
| 104 |
| 105 | std::string FakeSSLIdentity::PrivateKeyToPEMString() const { |
| 106 | RTC_NOTREACHED(); // Not implemented. |
| 107 | return ""; |
| 108 | } |
| 109 |
| 110 | std::string FakeSSLIdentity::PublicKeyToPEMString() const { |
| 111 | RTC_NOTREACHED(); // Not implemented. |
| 112 | return ""; |
| 113 | } |
| 114 |
| 115 | bool FakeSSLIdentity::operator==(const SSLIdentity& other) const { |
| 116 | RTC_NOTREACHED(); // Not implemented. |
| 117 | return false; |
| 118 | } |
| 119 |
| 120 | } // namespace rtc |
| 121 |