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 | |
| 11 | #include "rtc_base/fakesslidentity.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <string> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/messagedigest.h" |
| 19 | #include "rtc_base/ptr_util.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 23 | FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string) |
| 24 | : pem_string_(pem_string), |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 25 | digest_algorithm_(DIGEST_SHA_1), |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 26 | expiration_time_(-1) {} |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 27 | |
| 28 | FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default; |
| 29 | |
| 30 | FakeSSLCertificate::~FakeSSLCertificate() = default; |
| 31 | |
| 32 | FakeSSLCertificate* FakeSSLCertificate::GetReference() const { |
| 33 | return new FakeSSLCertificate(*this); |
| 34 | } |
| 35 | |
| 36 | std::string FakeSSLCertificate::ToPEMString() const { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 37 | return pem_string_; |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void FakeSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 41 | std::string der_string; |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 42 | RTC_CHECK( |
| 43 | SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string)); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 44 | der_buffer->SetData(der_string.c_str(), der_string.size()); |
| 45 | } |
| 46 | |
| 47 | int64_t FakeSSLCertificate::CertificateExpirationTime() const { |
| 48 | return expiration_time_; |
| 49 | } |
| 50 | |
| 51 | void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) { |
| 52 | expiration_time_ = expiration_time; |
| 53 | } |
| 54 | |
| 55 | void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) { |
| 56 | digest_algorithm_ = algorithm; |
| 57 | } |
| 58 | |
| 59 | bool FakeSSLCertificate::GetSignatureDigestAlgorithm( |
| 60 | std::string* algorithm) const { |
| 61 | *algorithm = digest_algorithm_; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm, |
| 66 | unsigned char* digest, |
| 67 | size_t size, |
| 68 | size_t* length) const { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 69 | *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(), |
| 70 | pem_string_.size(), digest, size); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 71 | return (*length != 0); |
| 72 | } |
| 73 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 74 | FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string) |
| 75 | : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {} |
| 76 | |
| 77 | FakeSSLIdentity::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) { |
| 80 | certs.push_back(MakeUnique<FakeSSLCertificate>(pem_string)); |
| 81 | } |
| 82 | cert_chain_ = MakeUnique<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) |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame^] | 86 | : cert_chain_(MakeUnique<SSLCertChain>(&cert)) {} |
| 87 | |
| 88 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o) |
| 89 | : cert_chain_(o.cert_chain_->UniqueCopy()) {} |
| 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 |