You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/fake_ssl_identity.h"
  11. #include <memory>
  12. #include <string>
  13. #include <utility>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/message_digest.h"
  16. namespace rtc {
  17. FakeSSLCertificate::FakeSSLCertificate(const std::string& pem_string)
  18. : pem_string_(pem_string),
  19. digest_algorithm_(DIGEST_SHA_1),
  20. expiration_time_(-1) {}
  21. FakeSSLCertificate::FakeSSLCertificate(const FakeSSLCertificate&) = default;
  22. FakeSSLCertificate::~FakeSSLCertificate() = default;
  23. std::unique_ptr<SSLCertificate> FakeSSLCertificate::Clone() const {
  24. return std::make_unique<FakeSSLCertificate>(*this);
  25. }
  26. std::string FakeSSLCertificate::ToPEMString() const {
  27. return pem_string_;
  28. }
  29. void FakeSSLCertificate::ToDER(Buffer* der_buffer) const {
  30. std::string der_string;
  31. RTC_CHECK(
  32. SSLIdentity::PemToDer(kPemTypeCertificate, pem_string_, &der_string));
  33. der_buffer->SetData(der_string.c_str(), der_string.size());
  34. }
  35. int64_t FakeSSLCertificate::CertificateExpirationTime() const {
  36. return expiration_time_;
  37. }
  38. void FakeSSLCertificate::SetCertificateExpirationTime(int64_t expiration_time) {
  39. expiration_time_ = expiration_time;
  40. }
  41. void FakeSSLCertificate::set_digest_algorithm(const std::string& algorithm) {
  42. digest_algorithm_ = algorithm;
  43. }
  44. bool FakeSSLCertificate::GetSignatureDigestAlgorithm(
  45. std::string* algorithm) const {
  46. *algorithm = digest_algorithm_;
  47. return true;
  48. }
  49. bool FakeSSLCertificate::ComputeDigest(const std::string& algorithm,
  50. unsigned char* digest,
  51. size_t size,
  52. size_t* length) const {
  53. *length = rtc::ComputeDigest(algorithm, pem_string_.c_str(),
  54. pem_string_.size(), digest, size);
  55. return (*length != 0);
  56. }
  57. FakeSSLIdentity::FakeSSLIdentity(const std::string& pem_string)
  58. : FakeSSLIdentity(FakeSSLCertificate(pem_string)) {}
  59. FakeSSLIdentity::FakeSSLIdentity(const std::vector<std::string>& pem_strings) {
  60. std::vector<std::unique_ptr<SSLCertificate>> certs;
  61. certs.reserve(pem_strings.size());
  62. for (const std::string& pem_string : pem_strings) {
  63. certs.push_back(std::make_unique<FakeSSLCertificate>(pem_string));
  64. }
  65. cert_chain_ = std::make_unique<SSLCertChain>(std::move(certs));
  66. }
  67. FakeSSLIdentity::FakeSSLIdentity(const FakeSSLCertificate& cert)
  68. : cert_chain_(std::make_unique<SSLCertChain>(cert.Clone())) {}
  69. FakeSSLIdentity::FakeSSLIdentity(const FakeSSLIdentity& o)
  70. : cert_chain_(o.cert_chain_->Clone()) {}
  71. FakeSSLIdentity::~FakeSSLIdentity() = default;
  72. FakeSSLIdentity* FakeSSLIdentity::GetReference() const {
  73. return new FakeSSLIdentity(*this);
  74. }
  75. const SSLCertificate& FakeSSLIdentity::certificate() const {
  76. return cert_chain_->Get(0);
  77. }
  78. const SSLCertChain& FakeSSLIdentity::cert_chain() const {
  79. return *cert_chain_.get();
  80. }
  81. std::string FakeSSLIdentity::PrivateKeyToPEMString() const {
  82. RTC_NOTREACHED(); // Not implemented.
  83. return "";
  84. }
  85. std::string FakeSSLIdentity::PublicKeyToPEMString() const {
  86. RTC_NOTREACHED(); // Not implemented.
  87. return "";
  88. }
  89. bool FakeSSLIdentity::operator==(const SSLIdentity& other) const {
  90. RTC_NOTREACHED(); // Not implemented.
  91. return false;
  92. }
  93. } // namespace rtc