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
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 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/openssl_utility.h"
  11. #if defined(WEBRTC_WIN)
  12. // Must be included first before openssl headers.
  13. #include "rtc_base/win32.h" // NOLINT
  14. #endif // WEBRTC_WIN
  15. #include <openssl/err.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/x509v3.h>
  18. #include <stddef.h>
  19. #include "rtc_base/arraysize.h"
  20. #include "rtc_base/logging.h"
  21. #include "rtc_base/numerics/safe_conversions.h"
  22. #include "rtc_base/openssl.h"
  23. #include "rtc_base/openssl_certificate.h"
  24. #ifndef WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS
  25. #include "rtc_base/ssl_roots.h"
  26. #endif // WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS
  27. namespace rtc {
  28. namespace openssl {
  29. // Holds various helper methods.
  30. namespace {
  31. void LogCertificates(SSL* ssl, X509* certificate) {
  32. // Logging certificates is extremely verbose. So it is disabled by default.
  33. #ifdef LOG_CERTIFICATES
  34. BIO* mem = BIO_new(BIO_s_mem());
  35. if (mem == nullptr) {
  36. RTC_DLOG(LS_ERROR) << "BIO_new() failed to allocate memory.";
  37. return;
  38. }
  39. RTC_DLOG(LS_INFO) << "Certificate from server:";
  40. X509_print_ex(mem, certificate, XN_FLAG_SEP_CPLUS_SPC, X509_FLAG_NO_HEADER);
  41. BIO_write(mem, "\0", 1);
  42. char* buffer = nullptr;
  43. BIO_get_mem_data(mem, &buffer);
  44. if (buffer != nullptr) {
  45. RTC_DLOG(LS_INFO) << buffer;
  46. } else {
  47. RTC_DLOG(LS_ERROR) << "BIO_get_mem_data() failed to get buffer.";
  48. }
  49. BIO_free(mem);
  50. const char* cipher_name = SSL_CIPHER_get_name(SSL_get_current_cipher(ssl));
  51. if (cipher_name != nullptr) {
  52. RTC_DLOG(LS_INFO) << "Cipher: " << cipher_name;
  53. } else {
  54. RTC_DLOG(LS_ERROR) << "SSL_CIPHER_DESCRIPTION() failed to get cipher_name.";
  55. }
  56. #endif
  57. }
  58. } // namespace
  59. bool VerifyPeerCertMatchesHost(SSL* ssl, const std::string& host) {
  60. if (host.empty()) {
  61. RTC_DLOG(LS_ERROR) << "Hostname is empty. Cannot verify peer certificate.";
  62. return false;
  63. }
  64. if (ssl == nullptr) {
  65. RTC_DLOG(LS_ERROR) << "SSL is nullptr. Cannot verify peer certificate.";
  66. return false;
  67. }
  68. X509* certificate = SSL_get_peer_certificate(ssl);
  69. if (certificate == nullptr) {
  70. RTC_DLOG(LS_ERROR)
  71. << "SSL_get_peer_certificate failed. This should never happen.";
  72. return false;
  73. }
  74. LogCertificates(ssl, certificate);
  75. bool is_valid_cert_name =
  76. X509_check_host(certificate, host.c_str(), host.size(), 0, nullptr) == 1;
  77. X509_free(certificate);
  78. return is_valid_cert_name;
  79. }
  80. void LogSSLErrors(const std::string& prefix) {
  81. char error_buf[200];
  82. unsigned long err; // NOLINT
  83. while ((err = ERR_get_error()) != 0) {
  84. ERR_error_string_n(err, error_buf, sizeof(error_buf));
  85. RTC_LOG(LS_ERROR) << prefix << ": " << error_buf << "\n";
  86. }
  87. }
  88. #ifndef WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS
  89. bool LoadBuiltinSSLRootCertificates(SSL_CTX* ctx) {
  90. int count_of_added_certs = 0;
  91. for (size_t i = 0; i < arraysize(kSSLCertCertificateList); i++) {
  92. const unsigned char* cert_buffer = kSSLCertCertificateList[i];
  93. size_t cert_buffer_len = kSSLCertCertificateSizeList[i];
  94. X509* cert = d2i_X509(nullptr, &cert_buffer,
  95. checked_cast<long>(cert_buffer_len)); // NOLINT
  96. if (cert) {
  97. int return_value = X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), cert);
  98. if (return_value == 0) {
  99. RTC_LOG(LS_WARNING) << "Unable to add certificate.";
  100. } else {
  101. count_of_added_certs++;
  102. }
  103. X509_free(cert);
  104. }
  105. }
  106. return count_of_added_certs > 0;
  107. }
  108. #endif // WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS
  109. } // namespace openssl
  110. } // namespace rtc