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.

127 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 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_fingerprint.h"
  11. #include <ctype.h>
  12. #include <cstdint>
  13. #include <memory>
  14. #include <string>
  15. #include "absl/algorithm/container.h"
  16. #include "rtc_base/logging.h"
  17. #include "rtc_base/message_digest.h"
  18. #include "rtc_base/rtc_certificate.h"
  19. #include "rtc_base/ssl_certificate.h"
  20. #include "rtc_base/ssl_identity.h"
  21. #include "rtc_base/string_encode.h"
  22. namespace rtc {
  23. SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm,
  24. const rtc::SSLIdentity* identity) {
  25. return CreateUnique(algorithm, *identity).release();
  26. }
  27. std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique(
  28. const std::string& algorithm,
  29. const rtc::SSLIdentity& identity) {
  30. return Create(algorithm, identity.certificate());
  31. }
  32. std::unique_ptr<SSLFingerprint> SSLFingerprint::Create(
  33. const std::string& algorithm,
  34. const rtc::SSLCertificate& cert) {
  35. uint8_t digest_val[64];
  36. size_t digest_len;
  37. bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val),
  38. &digest_len);
  39. if (!ret) {
  40. return nullptr;
  41. }
  42. return std::make_unique<SSLFingerprint>(
  43. algorithm, ArrayView<const uint8_t>(digest_val, digest_len));
  44. }
  45. SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
  46. const std::string& algorithm,
  47. const std::string& fingerprint) {
  48. return CreateUniqueFromRfc4572(algorithm, fingerprint).release();
  49. }
  50. std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572(
  51. const std::string& algorithm,
  52. const std::string& fingerprint) {
  53. if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm))
  54. return nullptr;
  55. if (fingerprint.empty())
  56. return nullptr;
  57. char value[rtc::MessageDigest::kMaxSize];
  58. size_t value_len = rtc::hex_decode_with_delimiter(
  59. value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':');
  60. if (!value_len)
  61. return nullptr;
  62. return std::make_unique<SSLFingerprint>(
  63. algorithm,
  64. ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len));
  65. }
  66. std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(
  67. const RTCCertificate& cert) {
  68. std::string digest_alg;
  69. if (!cert.GetSSLCertificate().GetSignatureDigestAlgorithm(&digest_alg)) {
  70. RTC_LOG(LS_ERROR)
  71. << "Failed to retrieve the certificate's digest algorithm";
  72. return nullptr;
  73. }
  74. std::unique_ptr<SSLFingerprint> fingerprint =
  75. CreateUnique(digest_alg, *cert.identity());
  76. if (!fingerprint) {
  77. RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg="
  78. << digest_alg;
  79. }
  80. return fingerprint;
  81. }
  82. SSLFingerprint::SSLFingerprint(const std::string& algorithm,
  83. ArrayView<const uint8_t> digest_view)
  84. : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {}
  85. SSLFingerprint::SSLFingerprint(const std::string& algorithm,
  86. const uint8_t* digest_in,
  87. size_t digest_len)
  88. : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {}
  89. SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
  90. : algorithm(from.algorithm), digest(from.digest) {}
  91. bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
  92. return algorithm == other.algorithm && digest == other.digest;
  93. }
  94. std::string SSLFingerprint::GetRfc4572Fingerprint() const {
  95. std::string fingerprint =
  96. rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':');
  97. absl::c_transform(fingerprint, fingerprint.begin(), ::toupper);
  98. return fingerprint;
  99. }
  100. std::string SSLFingerprint::ToString() const {
  101. std::string fp_str = algorithm;
  102. fp_str.append(" ");
  103. fp_str.append(GetRfc4572Fingerprint());
  104. return fp_str;
  105. }
  106. } // namespace rtc