Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/fake_ssl_identity.h" |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 12 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <utility> |
| 15 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/message_digest.h" |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 22 | FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string) |
| 23 | : pem_string_(pem_string), |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 24 | digest_algorithm_(DIGEST_SHA_1), |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 25 | expiration_time_(-1) {} |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 26 | |
| 27 | FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default; |
| 28 | |
| 29 | FakeSSLCertificate::~FakeSSLCertificate() = default; |
| 30 | |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 31 | std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const { |
| 32 | return absl::make_unique<FakeSSLCertificate>(*this); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | std::string FakeSSLCertificate::ToPEMString() const { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 36 | return pem_string_; |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void FakeSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 40 | std::string der_string; |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 41 | RTC_CHECK( |
| 42 | SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string)); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 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 { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 68 | *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(), |
| 69 | pem_string_.size(), digest, size); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 70 | return (*length != 0); |
| 71 | } |
| 72 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 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; |
Mirko Bonadei | 649a4c2 | 2019-01-29 10:11:53 +0100 | [diff] [blame] | 78 | certs.reserve(pem_strings.size()); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 79 | for (const std::string& pem_string : pem_strings) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 80 | certs.push_back(absl::make_unique<FakeSSLCertificate>(pem_string)); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 81 | } |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 82 | cert_chain_ = absl::make_unique<SSLCertChain>(std::move(certs)); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 85 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert) |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 86 | : cert_chain_(absl::make_unique<SSLCertChain>(cert.Clone())) {} |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 87 | |
| 88 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o) |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 89 | : cert_chain_(o.cert_chain_->Clone()) {} |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 90 | |
| 91 | FakeSSLIdentity::~FakeSSLIdentity() = default; |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 92 | |
| 93 | FakeSSLIdentity* FakeSSLIdentity::GetReference() const { |
| 94 | return new FakeSSLIdentity(*this); |
| 95 | } |
| 96 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 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(); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 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 |