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.

123 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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/ssl_certificate.h"
  11. #include <memory>
  12. #include <string>
  13. #include <utility>
  14. #include "absl/algorithm/container.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/openssl_certificate.h"
  17. #include "rtc_base/ssl_fingerprint.h"
  18. #include "rtc_base/third_party/base64/base64.h"
  19. namespace rtc {
  20. //////////////////////////////////////////////////////////////////////
  21. // SSLCertificateStats
  22. //////////////////////////////////////////////////////////////////////
  23. SSLCertificateStats::SSLCertificateStats(
  24. std::string&& fingerprint,
  25. std::string&& fingerprint_algorithm,
  26. std::string&& base64_certificate,
  27. std::unique_ptr<SSLCertificateStats> issuer)
  28. : fingerprint(std::move(fingerprint)),
  29. fingerprint_algorithm(std::move(fingerprint_algorithm)),
  30. base64_certificate(std::move(base64_certificate)),
  31. issuer(std::move(issuer)) {}
  32. SSLCertificateStats::~SSLCertificateStats() {}
  33. //////////////////////////////////////////////////////////////////////
  34. // SSLCertificate
  35. //////////////////////////////////////////////////////////////////////
  36. std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const {
  37. // TODO(bemasc): Move this computation to a helper class that caches these
  38. // values to reduce CPU use in |StatsCollector::GetStats|. This will require
  39. // adding a fast |SSLCertificate::Equals| to detect certificate changes.
  40. std::string digest_algorithm;
  41. if (!GetSignatureDigestAlgorithm(&digest_algorithm))
  42. return nullptr;
  43. // |SSLFingerprint::Create| can fail if the algorithm returned by
  44. // |SSLCertificate::GetSignatureDigestAlgorithm| is not supported by the
  45. // implementation of |SSLCertificate::ComputeDigest|. This currently happens
  46. // with MD5- and SHA-224-signed certificates when linked to libNSS.
  47. std::unique_ptr<SSLFingerprint> ssl_fingerprint =
  48. SSLFingerprint::Create(digest_algorithm, *this);
  49. if (!ssl_fingerprint)
  50. return nullptr;
  51. std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
  52. Buffer der_buffer;
  53. ToDER(&der_buffer);
  54. std::string der_base64;
  55. Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(), &der_base64);
  56. return std::make_unique<SSLCertificateStats>(std::move(fingerprint),
  57. std::move(digest_algorithm),
  58. std::move(der_base64), nullptr);
  59. }
  60. //////////////////////////////////////////////////////////////////////
  61. // SSLCertChain
  62. //////////////////////////////////////////////////////////////////////
  63. SSLCertChain::SSLCertChain(std::unique_ptr<SSLCertificate> single_cert) {
  64. certs_.push_back(std::move(single_cert));
  65. }
  66. SSLCertChain::SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs)
  67. : certs_(std::move(certs)) {}
  68. SSLCertChain::SSLCertChain(SSLCertChain&& rhs) = default;
  69. SSLCertChain& SSLCertChain::operator=(SSLCertChain&&) = default;
  70. SSLCertChain::~SSLCertChain() = default;
  71. std::unique_ptr<SSLCertChain> SSLCertChain::Clone() const {
  72. std::vector<std::unique_ptr<SSLCertificate>> new_certs(certs_.size());
  73. absl::c_transform(
  74. certs_, new_certs.begin(),
  75. [](const std::unique_ptr<SSLCertificate>& cert)
  76. -> std::unique_ptr<SSLCertificate> { return cert->Clone(); });
  77. return std::make_unique<SSLCertChain>(std::move(new_certs));
  78. }
  79. std::unique_ptr<SSLCertificateStats> SSLCertChain::GetStats() const {
  80. // We have a linked list of certificates, starting with the first element of
  81. // |certs_| and ending with the last element of |certs_|. The "issuer" of a
  82. // certificate is the next certificate in the chain. Stats are produced for
  83. // each certificate in the list. Here, the "issuer" is the issuer's stats.
  84. std::unique_ptr<SSLCertificateStats> issuer;
  85. // The loop runs in reverse so that the |issuer| is known before the
  86. // certificate issued by |issuer|.
  87. for (ptrdiff_t i = certs_.size() - 1; i >= 0; --i) {
  88. std::unique_ptr<SSLCertificateStats> new_stats = certs_[i]->GetStats();
  89. if (new_stats) {
  90. new_stats->issuer = std::move(issuer);
  91. }
  92. issuer = std::move(new_stats);
  93. }
  94. return issuer;
  95. }
  96. // static
  97. std::unique_ptr<SSLCertificate> SSLCertificate::FromPEMString(
  98. const std::string& pem_string) {
  99. return OpenSSLCertificate::FromPEMString(pem_string);
  100. }
  101. } // namespace rtc