blob: bf6832c5e65239838d37cf10df6ecc7e43ebc9e6 [file] [log] [blame]
Benjamin Wright9201d1a2018-04-05 12:12:26 -07001/*
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
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070011#include "rtc_base/opensslutility.h"
12
13#include <memory>
Benjamin Wright9201d1a2018-04-05 12:12:26 -070014
15#if defined(WEBRTC_POSIX)
16#include <unistd.h>
17#endif
18
19#if defined(WEBRTC_WIN)
20// Must be included first before openssl headers.
21#include "rtc_base/win32.h" // NOLINT
22#endif // WEBRTC_WIN
23
24#include <openssl/bio.h>
25#include <openssl/crypto.h>
Jiawei Oue635e982018-07-26 11:31:28 -070026#include <openssl/err.h>
Benjamin Wright9201d1a2018-04-05 12:12:26 -070027#include <openssl/x509.h>
28#include <openssl/x509v3.h>
29
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070030#include "rtc_base/arraysize.h"
Benjamin Wright9201d1a2018-04-05 12:12:26 -070031#include "rtc_base/checks.h"
32#include "rtc_base/logging.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070033#include "rtc_base/numerics/safe_conversions.h"
Benjamin Wright9201d1a2018-04-05 12:12:26 -070034#include "rtc_base/openssl.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070035#include "rtc_base/opensslcertificate.h"
Benjamin Wrighta7087e32018-05-29 17:46:04 -070036#ifdef WEBRTC_BUILT_IN_SSL_ROOT_CERTIFICATES
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070037#include "rtc_base/sslroots.h"
Benjamin Wrighta7087e32018-05-29 17:46:04 -070038#endif // WEBRTC_BUILT_IN_SSL_ROOT_CERTIFICATES
Benjamin Wright9201d1a2018-04-05 12:12:26 -070039
40namespace rtc {
41namespace openssl {
42
43// Holds various helper methods.
44namespace {
45void LogCertificates(SSL* ssl, X509* certificate) {
46// Logging certificates is extremely verbose. So it is disabled by default.
47#ifdef LOG_CERTIFICATES
48 BIO* mem = BIO_new(BIO_s_mem());
49 if (mem == nullptr) {
50 RTC_DLOG(LS_ERROR) << "BIO_new() failed to allocate memory.";
51 return;
52 }
53
54 RTC_DLOG(LS_INFO) << "Certificate from server:";
55 X509_print_ex(mem, certificate, XN_FLAG_SEP_CPLUS_SPC, X509_FLAG_NO_HEADER);
56 BIO_write(mem, "\0", 1);
57
58 char* buffer = nullptr;
59 BIO_get_mem_data(mem, &buffer);
60 if (buffer != nullptr) {
61 RTC_DLOG(LS_INFO) << buffer;
62 } else {
63 RTC_DLOG(LS_ERROR) << "BIO_get_mem_data() failed to get buffer.";
64 }
65 BIO_free(mem);
66
67 const char* cipher_name = SSL_CIPHER_get_name(SSL_get_current_cipher(ssl));
68 if (cipher_name != nullptr) {
69 RTC_DLOG(LS_INFO) << "Cipher: " << cipher_name;
70 } else {
71 RTC_DLOG(LS_ERROR) << "SSL_CIPHER_DESCRIPTION() failed to get cipher_name.";
72 }
73#endif
74}
75} // namespace
76
77bool VerifyPeerCertMatchesHost(SSL* ssl, const std::string& host) {
78 if (host.empty()) {
79 RTC_DLOG(LS_ERROR) << "Hostname is empty. Cannot verify peer certificate.";
80 return false;
81 }
82
83 if (ssl == nullptr) {
84 RTC_DLOG(LS_ERROR) << "SSL is nullptr. Cannot verify peer certificate.";
85 return false;
86 }
87
88 X509* certificate = SSL_get_peer_certificate(ssl);
89 if (certificate == nullptr) {
90 RTC_DLOG(LS_ERROR)
91 << "SSL_get_peer_certificate failed. This should never happen.";
92 return false;
93 }
94
95 LogCertificates(ssl, certificate);
96
97 bool is_valid_cert_name =
98 X509_check_host(certificate, host.c_str(), host.size(), 0, nullptr) == 1;
99 X509_free(certificate);
100 return is_valid_cert_name;
101}
102
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700103void LogSSLErrors(const std::string& prefix) {
104 char error_buf[200];
105 unsigned long err; // NOLINT
106
107 while ((err = ERR_get_error()) != 0) {
108 ERR_error_string_n(err, error_buf, sizeof(error_buf));
109 RTC_LOG(LS_ERROR) << prefix << ": " << error_buf << "\n";
110 }
111}
112
Benjamin Wrighta7087e32018-05-29 17:46:04 -0700113#ifdef WEBRTC_BUILT_IN_SSL_ROOT_CERTIFICATES
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700114bool LoadBuiltinSSLRootCertificates(SSL_CTX* ctx) {
115 int count_of_added_certs = 0;
116 for (size_t i = 0; i < arraysize(kSSLCertCertificateList); i++) {
117 const unsigned char* cert_buffer = kSSLCertCertificateList[i];
118 size_t cert_buffer_len = kSSLCertCertificateSizeList[i];
119 X509* cert = d2i_X509(nullptr, &cert_buffer,
120 checked_cast<long>(cert_buffer_len)); // NOLINT
121 if (cert) {
122 int return_value = X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), cert);
123 if (return_value == 0) {
124 RTC_LOG(LS_WARNING) << "Unable to add certificate.";
125 } else {
126 count_of_added_certs++;
127 }
128 X509_free(cert);
129 }
130 }
131 return count_of_added_certs > 0;
132}
Benjamin Wrighta7087e32018-05-29 17:46:04 -0700133#endif // WEBRTC_BUILT_IN_SSL_ROOT_CERTIFICATES
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700134
Benjamin Wright9201d1a2018-04-05 12:12:26 -0700135} // namespace openssl
136} // namespace rtc