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 | |
| 23 | FakeSSLCertificate::FakeSSLCertificate(const std::string& data) |
| 24 | : data_(data), digest_algorithm_(DIGEST_SHA_1), expiration_time_(-1) {} |
| 25 | |
| 26 | FakeSSLCertificate::FakeSSLCertificate(const std::vector<std::string>& certs) |
| 27 | : data_(certs.front()), |
| 28 | digest_algorithm_(DIGEST_SHA_1), |
| 29 | expiration_time_(-1) { |
| 30 | std::vector<std::string>::const_iterator it; |
| 31 | // Skip certs[0]. |
| 32 | for (it = certs.begin() + 1; it != certs.end(); ++it) { |
| 33 | certs_.push_back(FakeSSLCertificate(*it)); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default; |
| 38 | |
| 39 | FakeSSLCertificate::~FakeSSLCertificate() = default; |
| 40 | |
| 41 | FakeSSLCertificate* FakeSSLCertificate::GetReference() const { |
| 42 | return new FakeSSLCertificate(*this); |
| 43 | } |
| 44 | |
| 45 | std::string FakeSSLCertificate::ToPEMString() const { |
| 46 | return data_; |
| 47 | } |
| 48 | |
| 49 | void FakeSSLCertificate::ToDER(Buffer* der_buffer) const { |
| 50 | std::string der_string; |
| 51 | RTC_CHECK(SSLIdentity::PemToDer(kPemTypeCertificate, data_, &der_string)); |
| 52 | der_buffer->SetData(der_string.c_str(), der_string.size()); |
| 53 | } |
| 54 | |
| 55 | int64_t FakeSSLCertificate::CertificateExpirationTime() const { |
| 56 | return expiration_time_; |
| 57 | } |
| 58 | |
| 59 | void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) { |
| 60 | expiration_time_ = expiration_time; |
| 61 | } |
| 62 | |
| 63 | void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) { |
| 64 | digest_algorithm_ = algorithm; |
| 65 | } |
| 66 | |
| 67 | bool FakeSSLCertificate::GetSignatureDigestAlgorithm( |
| 68 | std::string* algorithm) const { |
| 69 | *algorithm = digest_algorithm_; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm, |
| 74 | unsigned char* digest, |
| 75 | size_t size, |
| 76 | size_t* length) const { |
| 77 | *length = |
| 78 | rtc::ComputeDigest(algorithm, data_.c_str(), data_.size(), digest, size); |
| 79 | return (*length != 0); |
| 80 | } |
| 81 | |
| 82 | std::unique_ptr<SSLCertChain> FakeSSLCertificate::GetChain() const { |
| 83 | if (certs_.empty()) |
| 84 | return nullptr; |
| 85 | std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size()); |
| 86 | std::transform(certs_.begin(), certs_.end(), new_certs.begin(), DupCert); |
| 87 | return MakeUnique<SSLCertChain>(std::move(new_certs)); |
| 88 | } |
| 89 | |
| 90 | FakeSSLIdentity::FakeSSLIdentity(const std::string& data) : cert_(data) {} |
| 91 | |
| 92 | FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert) |
| 93 | : cert_(cert) {} |
| 94 | |
| 95 | FakeSSLIdentity* FakeSSLIdentity::GetReference() const { |
| 96 | return new FakeSSLIdentity(*this); |
| 97 | } |
| 98 | |
| 99 | const FakeSSLCertificate& FakeSSLIdentity::certificate() const { |
| 100 | return cert_; |
| 101 | } |
| 102 | |
| 103 | std::string FakeSSLIdentity::PrivateKeyToPEMString() const { |
| 104 | RTC_NOTREACHED(); // Not implemented. |
| 105 | return ""; |
| 106 | } |
| 107 | |
| 108 | std::string FakeSSLIdentity::PublicKeyToPEMString() const { |
| 109 | RTC_NOTREACHED(); // Not implemented. |
| 110 | return ""; |
| 111 | } |
| 112 | |
| 113 | bool FakeSSLIdentity::operator==(const SSLIdentity& other) const { |
| 114 | RTC_NOTREACHED(); // Not implemented. |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | } // namespace rtc |