henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/openssl_adapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <errno.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <openssl/bio.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include <openssl/err.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include <openssl/rand.h> |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 18 | #include <openssl/x509.h> |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 19 | #include "rtc_base/openssl.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | |
| 24 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 26 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 28 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/openssl_certificate.h" |
| 30 | #include "rtc_base/openssl_utility.h" |
| 31 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 34 | #ifndef OPENSSL_IS_BORINGSSL |
| 35 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 36 | // TODO(benwright): Use a nicer abstraction for mutex. |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 37 | |
| 38 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | #define MUTEX_TYPE HANDLE |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 40 | #define MUTEX_SETUP(x) (x) = CreateMutex(nullptr, FALSE, nullptr) |
| 41 | #define MUTEX_CLEANUP(x) CloseHandle(x) |
| 42 | #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) |
| 43 | #define MUTEX_UNLOCK(x) ReleaseMutex(x) |
| 44 | #define THREAD_ID GetCurrentThreadId() |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 45 | #elif defined(WEBRTC_POSIX) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | #define MUTEX_TYPE pthread_mutex_t |
| 47 | #define MUTEX_SETUP(x) pthread_mutex_init(&(x), nullptr) |
| 48 | #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 49 | #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 50 | #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 51 | #define THREAD_ID pthread_self() |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 52 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 53 | #error You must define mutex operations appropriate for your platform! |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | struct CRYPTO_dynlock_value { |
| 57 | MUTEX_TYPE mutex; |
| 58 | }; |
| 59 | |
| 60 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 61 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | ////////////////////////////////////////////////////////////////////// |
| 63 | // SocketBIO |
| 64 | ////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | static int socket_write(BIO* h, const char* buf, int num); |
| 67 | static int socket_read(BIO* h, char* buf, int size); |
| 68 | static int socket_puts(BIO* h, const char* str); |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 69 | static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); // NOLINT |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | static int socket_new(BIO* h); |
| 71 | static int socket_free(BIO* data); |
| 72 | |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 73 | static BIO_METHOD* BIO_socket_method() { |
| 74 | static BIO_METHOD* methods = [] { |
| 75 | BIO_METHOD* methods = BIO_meth_new(BIO_TYPE_BIO, "socket"); |
| 76 | BIO_meth_set_write(methods, socket_write); |
| 77 | BIO_meth_set_read(methods, socket_read); |
| 78 | BIO_meth_set_puts(methods, socket_puts); |
| 79 | BIO_meth_set_ctrl(methods, socket_ctrl); |
| 80 | BIO_meth_set_create(methods, socket_new); |
| 81 | BIO_meth_set_destroy(methods, socket_free); |
| 82 | return methods; |
| 83 | }(); |
| 84 | return methods; |
| 85 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 87 | static BIO* BIO_new_socket(rtc::AsyncSocket* socket) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 88 | BIO* ret = BIO_new(BIO_socket_method()); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 89 | if (ret == nullptr) { |
| 90 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | } |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 92 | BIO_set_data(ret, socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static int socket_new(BIO* b) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 97 | BIO_set_shutdown(b, 0); |
| 98 | BIO_set_init(b, 1); |
| 99 | BIO_set_data(b, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | static int socket_free(BIO* b) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 104 | if (b == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static int socket_read(BIO* b, char* out, int outl) { |
| 110 | if (!out) |
| 111 | return -1; |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 112 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(BIO_get_data(b)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | BIO_clear_retry_flags(b); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 114 | int result = socket->Recv(out, outl, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | if (result > 0) { |
| 116 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | } else if (socket->IsBlocking()) { |
| 118 | BIO_set_retry_read(b); |
| 119 | } |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | static int socket_write(BIO* b, const char* in, int inl) { |
| 124 | if (!in) |
| 125 | return -1; |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 126 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(BIO_get_data(b)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | BIO_clear_retry_flags(b); |
| 128 | int result = socket->Send(in, inl); |
| 129 | if (result > 0) { |
| 130 | return result; |
| 131 | } else if (socket->IsBlocking()) { |
| 132 | BIO_set_retry_write(b); |
| 133 | } |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | static int socket_puts(BIO* b, const char* str) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 138 | return socket_write(b, str, rtc::checked_cast<int>(strlen(str))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 141 | static long socket_ctrl(BIO* b, int cmd, long num, void* ptr) { // NOLINT |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | switch (cmd) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 143 | case BIO_CTRL_RESET: |
| 144 | return 0; |
| 145 | case BIO_CTRL_EOF: { |
| 146 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(ptr); |
| 147 | // 1 means socket closed. |
| 148 | return (socket->GetState() == rtc::AsyncSocket::CS_CLOSED) ? 1 : 0; |
| 149 | } |
| 150 | case BIO_CTRL_WPENDING: |
| 151 | case BIO_CTRL_PENDING: |
| 152 | return 0; |
| 153 | case BIO_CTRL_FLUSH: |
| 154 | return 1; |
| 155 | default: |
| 156 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 160 | static void LogSslError() { |
| 161 | // Walk down the error stack to find the SSL error. |
| 162 | uint32_t error_code; |
| 163 | const char* file; |
| 164 | int line; |
| 165 | do { |
| 166 | error_code = ERR_get_error_line(&file, &line); |
| 167 | if (ERR_GET_LIB(error_code) == ERR_LIB_SSL) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 168 | RTC_LOG(LS_ERROR) << "ERR_LIB_SSL: " << error_code << ", " << file << ":" |
| 169 | << line; |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } while (error_code != 0); |
| 173 | } |
| 174 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | ///////////////////////////////////////////////////////////////////////////// |
| 176 | // OpenSSLAdapter |
| 177 | ///////////////////////////////////////////////////////////////////////////// |
| 178 | |
| 179 | namespace rtc { |
| 180 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 181 | bool OpenSSLAdapter::InitializeSSL() { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 182 | if (!SSL_library_init()) |
| 183 | return false; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 184 | #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 185 | // Loading the error strings crashes mac_asan. Omit this debugging aid there. |
| 186 | SSL_load_error_strings(); |
| 187 | #endif |
| 188 | ERR_load_BIO_strings(); |
| 189 | OpenSSL_add_all_algorithms(); |
| 190 | RAND_poll(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 194 | bool OpenSSLAdapter::CleanupSSL() { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 198 | OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket, |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 199 | OpenSSLSessionCache* ssl_session_cache, |
| 200 | SSLCertificateVerifier* ssl_cert_verifier) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 201 | : SSLAdapter(socket), |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 202 | ssl_session_cache_(ssl_session_cache), |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 203 | ssl_cert_verifier_(ssl_cert_verifier), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 204 | state_(SSL_NONE), |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 205 | role_(SSL_CLIENT), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 206 | ssl_read_needs_write_(false), |
| 207 | ssl_write_needs_read_(false), |
| 208 | restartable_(false), |
| 209 | ssl_(nullptr), |
| 210 | ssl_ctx_(nullptr), |
| 211 | ssl_mode_(SSL_MODE_TLS), |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 212 | ignore_bad_cert_(false), |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 213 | custom_cert_verifier_status_(false) { |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 214 | // If a factory is used, take a reference on the factory's SSL_CTX. |
| 215 | // Otherwise, we'll create our own later. |
| 216 | // Either way, we'll release our reference via SSL_CTX_free() in Cleanup(). |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 217 | if (ssl_session_cache_ != nullptr) { |
| 218 | ssl_ctx_ = ssl_session_cache_->GetSSLContext(); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 219 | RTC_DCHECK(ssl_ctx_); |
| 220 | // Note: if using OpenSSL, requires version 1.1.0 or later. |
| 221 | SSL_CTX_up_ref(ssl_ctx_); |
| 222 | } |
| 223 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | |
| 225 | OpenSSLAdapter::~OpenSSLAdapter() { |
| 226 | Cleanup(); |
| 227 | } |
| 228 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 229 | void OpenSSLAdapter::SetIgnoreBadCert(bool ignore) { |
| 230 | ignore_bad_cert_ = ignore; |
| 231 | } |
| 232 | |
| 233 | void OpenSSLAdapter::SetAlpnProtocols(const std::vector<std::string>& protos) { |
| 234 | alpn_protocols_ = protos; |
| 235 | } |
| 236 | |
| 237 | void OpenSSLAdapter::SetEllipticCurves(const std::vector<std::string>& curves) { |
| 238 | elliptic_curves_ = curves; |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 241 | void OpenSSLAdapter::SetMode(SSLMode mode) { |
| 242 | RTC_DCHECK(!ssl_ctx_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 243 | RTC_DCHECK(state_ == SSL_NONE); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 244 | ssl_mode_ = mode; |
| 245 | } |
| 246 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 247 | void OpenSSLAdapter::SetCertVerifier( |
| 248 | SSLCertificateVerifier* ssl_cert_verifier) { |
| 249 | RTC_DCHECK(!ssl_ctx_); |
| 250 | ssl_cert_verifier_ = ssl_cert_verifier; |
| 251 | } |
| 252 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 253 | void OpenSSLAdapter::SetIdentity(SSLIdentity* identity) { |
| 254 | RTC_DCHECK(!identity_); |
| 255 | identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 256 | } |
| 257 | |
| 258 | void OpenSSLAdapter::SetRole(SSLRole role) { |
| 259 | role_ = role; |
| 260 | } |
| 261 | |
| 262 | AsyncSocket* OpenSSLAdapter::Accept(SocketAddress* paddr) { |
| 263 | RTC_DCHECK(role_ == SSL_SERVER); |
| 264 | AsyncSocket* socket = SSLAdapter::Accept(paddr); |
| 265 | if (!socket) { |
| 266 | return nullptr; |
| 267 | } |
| 268 | |
| 269 | SSLAdapter* adapter = SSLAdapter::Create(socket); |
| 270 | adapter->SetIdentity(identity_->GetReference()); |
| 271 | adapter->SetRole(rtc::SSL_SERVER); |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 272 | adapter->SetIgnoreBadCert(ignore_bad_cert_); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 273 | adapter->StartSSL("", false); |
| 274 | return adapter; |
| 275 | } |
| 276 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 277 | int OpenSSLAdapter::StartSSL(const char* hostname, bool restartable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 278 | if (state_ != SSL_NONE) |
| 279 | return -1; |
| 280 | |
| 281 | ssl_host_name_ = hostname; |
| 282 | restartable_ = restartable; |
| 283 | |
| 284 | if (socket_->GetState() != Socket::CS_CONNECTED) { |
| 285 | state_ = SSL_WAIT; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | state_ = SSL_CONNECTING; |
| 290 | if (int err = BeginSSL()) { |
| 291 | Error("BeginSSL", err, false); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 298 | int OpenSSLAdapter::BeginSSL() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 299 | RTC_LOG(LS_INFO) << "OpenSSLAdapter::BeginSSL: " << ssl_host_name_; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 300 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 301 | |
| 302 | int err = 0; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 303 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 305 | // First set up the context. We should either have a factory, with its own |
| 306 | // pre-existing context, or be running standalone, in which case we will |
| 307 | // need to create one, and specify |false| to disable session caching. |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 308 | if (ssl_session_cache_ == nullptr) { |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 309 | RTC_DCHECK(!ssl_ctx_); |
| 310 | ssl_ctx_ = CreateContext(ssl_mode_, false); |
| 311 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 312 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | if (!ssl_ctx_) { |
| 314 | err = -1; |
| 315 | goto ssl_error; |
| 316 | } |
| 317 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 318 | if (identity_ && !identity_->ConfigureIdentity(ssl_ctx_)) { |
| 319 | SSL_CTX_free(ssl_ctx_); |
| 320 | err = -1; |
| 321 | goto ssl_error; |
| 322 | } |
| 323 | |
Peter Boström | 0b518bf | 2016-01-27 12:35:40 +0100 | [diff] [blame] | 324 | bio = BIO_new_socket(socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 325 | if (!bio) { |
| 326 | err = -1; |
| 327 | goto ssl_error; |
| 328 | } |
| 329 | |
| 330 | ssl_ = SSL_new(ssl_ctx_); |
| 331 | if (!ssl_) { |
| 332 | err = -1; |
| 333 | goto ssl_error; |
| 334 | } |
| 335 | |
| 336 | SSL_set_app_data(ssl_, this); |
| 337 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 338 | // SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER allows different buffers to be passed |
| 339 | // into SSL_write when a record could only be partially transmitted (and thus |
| 340 | // requires another call to SSL_write to finish transmission). This allows us |
| 341 | // to copy the data into our own buffer when this occurs, since the original |
| 342 | // buffer can't safely be accessed after control exits Send. |
| 343 | // TODO(deadbeef): Do we want SSL_MODE_ENABLE_PARTIAL_WRITE? It doesn't |
| 344 | // appear Send handles partial writes properly, though maybe we never notice |
| 345 | // since we never send more than 16KB at once.. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 347 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 348 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 349 | // Enable SNI, if a hostname is supplied. |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 350 | if (!ssl_host_name_.empty()) { |
| 351 | SSL_set_tlsext_host_name(ssl_, ssl_host_name_.c_str()); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 352 | |
| 353 | // Enable session caching, if configured and a hostname is supplied. |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 354 | if (ssl_session_cache_ != nullptr) { |
| 355 | SSL_SESSION* cached = ssl_session_cache_->LookupSession(ssl_host_name_); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 356 | if (cached) { |
| 357 | if (SSL_set_session(ssl_, cached) == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 358 | RTC_LOG(LS_WARNING) << "Failed to apply SSL session from cache"; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 359 | err = -1; |
| 360 | goto ssl_error; |
| 361 | } |
| 362 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 363 | RTC_LOG(LS_INFO) << "Attempting to resume SSL session to " |
| 364 | << ssl_host_name_; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 369 | #ifdef OPENSSL_IS_BORINGSSL |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 370 | // Set a couple common TLS extensions; even though we don't use them yet. |
| 371 | SSL_enable_ocsp_stapling(ssl_); |
| 372 | SSL_enable_signed_cert_timestamps(ssl_); |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 373 | #endif |
Emad Omara | cb79d23 | 2017-07-20 16:34:34 -0700 | [diff] [blame] | 374 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 375 | if (!alpn_protocols_.empty()) { |
| 376 | std::string tls_alpn_string = TransformAlpnProtocols(alpn_protocols_); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 377 | if (!tls_alpn_string.empty()) { |
| 378 | SSL_set_alpn_protos( |
| 379 | ssl_, reinterpret_cast<const unsigned char*>(tls_alpn_string.data()), |
Mirko Bonadei | a041f92 | 2018-05-23 10:22:36 +0200 | [diff] [blame] | 380 | rtc::dchecked_cast<unsigned>(tls_alpn_string.size())); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 384 | if (!elliptic_curves_.empty()) { |
| 385 | SSL_set1_curves_list(ssl_, rtc::join(elliptic_curves_, ':').c_str()); |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 388 | // Now that the initial config is done, transfer ownership of |bio| to the |
| 389 | // SSL object. If ContinueSSL() fails, the bio will be freed in Cleanup(). |
| 390 | SSL_set_bio(ssl_, bio, bio); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 391 | bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 393 | // Do the connect. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 394 | err = ContinueSSL(); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 395 | if (err != 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 396 | goto ssl_error; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 397 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 398 | |
| 399 | return err; |
| 400 | |
| 401 | ssl_error: |
| 402 | Cleanup(); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 403 | if (bio) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 404 | BIO_free(bio); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 405 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 406 | |
| 407 | return err; |
| 408 | } |
| 409 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 410 | int OpenSSLAdapter::ContinueSSL() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 411 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 413 | // Clear the DTLS timer |
| 414 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 415 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 416 | int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 417 | switch (SSL_get_error(ssl_, code)) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 418 | case SSL_ERROR_NONE: |
| 419 | if (!SSLPostConnectionCheck(ssl_, ssl_host_name_)) { |
| 420 | RTC_LOG(LS_ERROR) << "TLS post connection check failed"; |
| 421 | // make sure we close the socket |
| 422 | Cleanup(); |
| 423 | // The connect failed so return -1 to shut down the socket |
| 424 | return -1; |
| 425 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 426 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 427 | state_ = SSL_CONNECTED; |
| 428 | AsyncSocketAdapter::OnConnectEvent(this); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 429 | // TODO(benwright): Refactor this code path. |
| 430 | // Don't let ourselves go away during the callbacks |
| 431 | // PRefPtr<OpenSSLAdapter> lock(this); |
| 432 | // RTC_LOG(LS_INFO) << " -- onStreamReadable"; |
| 433 | // AsyncSocketAdapter::OnReadEvent(this); |
| 434 | // RTC_LOG(LS_INFO) << " -- onStreamWriteable"; |
| 435 | // AsyncSocketAdapter::OnWriteEvent(this); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 436 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 437 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 438 | case SSL_ERROR_WANT_READ: |
| 439 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
| 440 | struct timeval timeout; |
| 441 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 442 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 443 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 444 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT, |
| 445 | 0); |
| 446 | } |
| 447 | break; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 448 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 449 | case SSL_ERROR_WANT_WRITE: |
| 450 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 451 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 452 | case SSL_ERROR_ZERO_RETURN: |
| 453 | default: |
| 454 | RTC_LOG(LS_WARNING) << "ContinueSSL -- error " << code; |
| 455 | return (code != 0) ? code : -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 461 | void OpenSSLAdapter::Error(const char* context, int err, bool signal) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 462 | RTC_LOG(LS_WARNING) << "OpenSSLAdapter::Error(" << context << ", " << err |
| 463 | << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 464 | state_ = SSL_ERROR; |
| 465 | SetError(err); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 466 | if (signal) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 467 | AsyncSocketAdapter::OnCloseEvent(this, err); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 468 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 471 | void OpenSSLAdapter::Cleanup() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 472 | RTC_LOG(LS_INFO) << "OpenSSLAdapter::Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 473 | |
| 474 | state_ = SSL_NONE; |
| 475 | ssl_read_needs_write_ = false; |
| 476 | ssl_write_needs_read_ = false; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 477 | custom_cert_verifier_status_ = false; |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 478 | pending_data_.Clear(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 479 | |
| 480 | if (ssl_) { |
| 481 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 482 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (ssl_ctx_) { |
| 486 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 487 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 488 | } |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 489 | identity_.reset(); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 490 | |
| 491 | // Clear the DTLS timer |
| 492 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 493 | } |
| 494 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 495 | int OpenSSLAdapter::DoSslWrite(const void* pv, size_t cb, int* error) { |
| 496 | // If we have pending data (that was previously only partially written by |
| 497 | // SSL_write), we shouldn't be attempting to write anything else. |
| 498 | RTC_DCHECK(pending_data_.empty() || pv == pending_data_.data()); |
| 499 | RTC_DCHECK(error != nullptr); |
| 500 | |
| 501 | ssl_write_needs_read_ = false; |
| 502 | int ret = SSL_write(ssl_, pv, checked_cast<int>(cb)); |
| 503 | *error = SSL_get_error(ssl_, ret); |
| 504 | switch (*error) { |
| 505 | case SSL_ERROR_NONE: |
| 506 | // Success! |
| 507 | return ret; |
| 508 | case SSL_ERROR_WANT_READ: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 509 | RTC_LOG(LS_INFO) << " -- error want read"; |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 510 | ssl_write_needs_read_ = true; |
| 511 | SetError(EWOULDBLOCK); |
| 512 | break; |
| 513 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 514 | RTC_LOG(LS_INFO) << " -- error want write"; |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 515 | SetError(EWOULDBLOCK); |
| 516 | break; |
| 517 | case SSL_ERROR_ZERO_RETURN: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 518 | SetError(EWOULDBLOCK); |
| 519 | // do we need to signal closure? |
| 520 | break; |
| 521 | case SSL_ERROR_SSL: |
| 522 | LogSslError(); |
| 523 | Error("SSL_write", ret ? ret : -1, false); |
| 524 | break; |
| 525 | default: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 526 | Error("SSL_write", ret ? ret : -1, false); |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | return SOCKET_ERROR; |
| 531 | } |
| 532 | |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 533 | /////////////////////////////////////////////////////////////////////////////// |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 534 | // AsyncSocket Implementation |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 535 | /////////////////////////////////////////////////////////////////////////////// |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 536 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 537 | int OpenSSLAdapter::Send(const void* pv, size_t cb) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 538 | switch (state_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 539 | case SSL_NONE: |
| 540 | return AsyncSocketAdapter::Send(pv, cb); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 541 | case SSL_WAIT: |
| 542 | case SSL_CONNECTING: |
| 543 | SetError(ENOTCONN); |
| 544 | return SOCKET_ERROR; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 545 | case SSL_CONNECTED: |
| 546 | break; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 547 | case SSL_ERROR: |
| 548 | default: |
| 549 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 550 | } |
| 551 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 552 | int ret; |
| 553 | int error; |
| 554 | |
| 555 | if (!pending_data_.empty()) { |
| 556 | ret = DoSslWrite(pending_data_.data(), pending_data_.size(), &error); |
| 557 | if (ret != static_cast<int>(pending_data_.size())) { |
| 558 | // We couldn't finish sending the pending data, so we definitely can't |
| 559 | // send any more data. Return with an EWOULDBLOCK error. |
| 560 | SetError(EWOULDBLOCK); |
| 561 | return SOCKET_ERROR; |
| 562 | } |
| 563 | // We completed sending the data previously passed into SSL_write! Now |
| 564 | // we're allowed to send more data. |
| 565 | pending_data_.Clear(); |
| 566 | } |
| 567 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 568 | // OpenSSL will return an error if we try to write zero bytes |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 569 | if (cb == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 570 | return 0; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 571 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 572 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 573 | ret = DoSslWrite(pv, cb, &error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 574 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 575 | // If SSL_write fails with SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE, this |
| 576 | // means the underlying socket is blocked on reading or (more typically) |
| 577 | // writing. When this happens, OpenSSL requires that the next call to |
| 578 | // SSL_write uses the same arguments (though, with |
| 579 | // SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER, the actual buffer pointer may be |
| 580 | // different). |
| 581 | // |
| 582 | // However, after Send exits, we will have lost access to data the user of |
| 583 | // this class is trying to send, and there's no guarantee that the user of |
| 584 | // this class will call Send with the same arguements when it fails. So, we |
| 585 | // buffer the data ourselves. When we know the underlying socket is writable |
| 586 | // again from OnWriteEvent (or if Send is called again before that happens), |
| 587 | // we'll retry sending this buffered data. |
deadbeef | e5dce2b | 2017-06-02 11:52:06 -0700 | [diff] [blame] | 588 | if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) { |
| 589 | // Shouldn't be able to get to this point if we already have pending data. |
| 590 | RTC_DCHECK(pending_data_.empty()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 591 | RTC_LOG(LS_WARNING) |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 592 | << "SSL_write couldn't write to the underlying socket; buffering data."; |
| 593 | pending_data_.SetData(static_cast<const uint8_t*>(pv), cb); |
| 594 | // Since we're taking responsibility for sending this data, return its full |
| 595 | // size. The user of this class can consider it sent. |
Mirko Bonadei | a041f92 | 2018-05-23 10:22:36 +0200 | [diff] [blame] | 596 | return rtc::dchecked_cast<int>(cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 597 | } |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 598 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 601 | int OpenSSLAdapter::SendTo(const void* pv, |
| 602 | size_t cb, |
| 603 | const SocketAddress& addr) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 604 | if (socket_->GetState() == Socket::CS_CONNECTED && |
| 605 | addr == socket_->GetRemoteAddress()) { |
| 606 | return Send(pv, cb); |
| 607 | } |
| 608 | |
| 609 | SetError(ENOTCONN); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 610 | return SOCKET_ERROR; |
| 611 | } |
| 612 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 613 | int OpenSSLAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 614 | switch (state_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 615 | case SSL_NONE: |
| 616 | return AsyncSocketAdapter::Recv(pv, cb, timestamp); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 617 | case SSL_WAIT: |
| 618 | case SSL_CONNECTING: |
| 619 | SetError(ENOTCONN); |
| 620 | return SOCKET_ERROR; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 621 | case SSL_CONNECTED: |
| 622 | break; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 623 | case SSL_ERROR: |
| 624 | default: |
| 625 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // Don't trust OpenSSL with zero byte reads |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 629 | if (cb == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 630 | return 0; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 631 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 632 | |
| 633 | ssl_read_needs_write_ = false; |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 634 | int code = SSL_read(ssl_, pv, checked_cast<int>(cb)); |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 635 | int error = SSL_get_error(ssl_, code); |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 636 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 637 | switch (error) { |
| 638 | case SSL_ERROR_NONE: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 639 | return code; |
| 640 | case SSL_ERROR_WANT_READ: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 641 | SetError(EWOULDBLOCK); |
| 642 | break; |
| 643 | case SSL_ERROR_WANT_WRITE: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 644 | ssl_read_needs_write_ = true; |
| 645 | SetError(EWOULDBLOCK); |
| 646 | break; |
| 647 | case SSL_ERROR_ZERO_RETURN: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 648 | SetError(EWOULDBLOCK); |
| 649 | // do we need to signal closure? |
| 650 | break; |
| 651 | case SSL_ERROR_SSL: |
| 652 | LogSslError(); |
| 653 | Error("SSL_read", (code ? code : -1), false); |
| 654 | break; |
| 655 | default: |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 656 | Error("SSL_read", (code ? code : -1), false); |
| 657 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 658 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 659 | return SOCKET_ERROR; |
| 660 | } |
| 661 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 662 | int OpenSSLAdapter::RecvFrom(void* pv, |
| 663 | size_t cb, |
| 664 | SocketAddress* paddr, |
| 665 | int64_t* timestamp) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 666 | if (socket_->GetState() == Socket::CS_CONNECTED) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 667 | int ret = Recv(pv, cb, timestamp); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 668 | *paddr = GetRemoteAddress(); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | SetError(ENOTCONN); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 673 | return SOCKET_ERROR; |
| 674 | } |
| 675 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 676 | int OpenSSLAdapter::Close() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 677 | Cleanup(); |
| 678 | state_ = restartable_ ? SSL_WAIT : SSL_NONE; |
| 679 | return AsyncSocketAdapter::Close(); |
| 680 | } |
| 681 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 682 | Socket::ConnState OpenSSLAdapter::GetState() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 683 | ConnState state = socket_->GetState(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 684 | if ((state == CS_CONNECTED) && |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 685 | ((state_ == SSL_WAIT) || (state_ == SSL_CONNECTING))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 686 | state = CS_CONNECTING; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 687 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 688 | return state; |
| 689 | } |
| 690 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 691 | bool OpenSSLAdapter::IsResumedSession() { |
| 692 | return (ssl_ && SSL_session_reused(ssl_) == 1); |
| 693 | } |
| 694 | |
| 695 | void OpenSSLAdapter::OnMessage(Message* msg) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 696 | if (MSG_TIMEOUT == msg->message_id) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 697 | RTC_LOG(LS_INFO) << "DTLS timeout expired"; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 698 | DTLSv1_handle_timeout(ssl_); |
| 699 | ContinueSSL(); |
| 700 | } |
| 701 | } |
| 702 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 703 | void OpenSSLAdapter::OnConnectEvent(AsyncSocket* socket) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 704 | RTC_LOG(LS_INFO) << "OpenSSLAdapter::OnConnectEvent"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 705 | if (state_ != SSL_WAIT) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 706 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 707 | AsyncSocketAdapter::OnConnectEvent(socket); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | state_ = SSL_CONNECTING; |
| 712 | if (int err = BeginSSL()) { |
| 713 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 714 | } |
| 715 | } |
| 716 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 717 | void OpenSSLAdapter::OnReadEvent(AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 718 | if (state_ == SSL_NONE) { |
| 719 | AsyncSocketAdapter::OnReadEvent(socket); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | if (state_ == SSL_CONNECTING) { |
| 724 | if (int err = ContinueSSL()) { |
| 725 | Error("ContinueSSL", err); |
| 726 | } |
| 727 | return; |
| 728 | } |
| 729 | |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 730 | if (state_ != SSL_CONNECTED) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 731 | return; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 732 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 733 | |
| 734 | // Don't let ourselves go away during the callbacks |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 735 | // PRefPtr<OpenSSLAdapter> lock(this); // TODO(benwright): fix this |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 736 | if (ssl_write_needs_read_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 737 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 738 | } |
| 739 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 740 | AsyncSocketAdapter::OnReadEvent(socket); |
| 741 | } |
| 742 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 743 | void OpenSSLAdapter::OnWriteEvent(AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 744 | if (state_ == SSL_NONE) { |
| 745 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | if (state_ == SSL_CONNECTING) { |
| 750 | if (int err = ContinueSSL()) { |
| 751 | Error("ContinueSSL", err); |
| 752 | } |
| 753 | return; |
| 754 | } |
| 755 | |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 756 | if (state_ != SSL_CONNECTED) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 757 | return; |
Benjamin Wright | 243cabe | 2018-10-16 01:55:28 -0700 | [diff] [blame] | 758 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 759 | |
| 760 | // Don't let ourselves go away during the callbacks |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 761 | // PRefPtr<OpenSSLAdapter> lock(this); // TODO(benwright): fix this |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 763 | if (ssl_read_needs_write_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 764 | AsyncSocketAdapter::OnReadEvent(socket); |
| 765 | } |
| 766 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 767 | // If a previous SSL_write failed due to the underlying socket being blocked, |
| 768 | // this will attempt finishing the write operation. |
| 769 | if (!pending_data_.empty()) { |
| 770 | int error; |
| 771 | if (DoSslWrite(pending_data_.data(), pending_data_.size(), &error) == |
| 772 | static_cast<int>(pending_data_.size())) { |
| 773 | pending_data_.Clear(); |
| 774 | } |
| 775 | } |
| 776 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 777 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 778 | } |
| 779 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 780 | void OpenSSLAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 781 | RTC_LOG(LS_INFO) << "OpenSSLAdapter::OnCloseEvent(" << err << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 782 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 783 | } |
| 784 | |
Benjamin Wright | 9201d1a | 2018-04-05 12:12:26 -0700 | [diff] [blame] | 785 | bool OpenSSLAdapter::SSLPostConnectionCheck(SSL* ssl, const std::string& host) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 786 | bool is_valid_cert_name = |
| 787 | openssl::VerifyPeerCertMatchesHost(ssl, host) && |
| 788 | (SSL_get_verify_result(ssl) == X509_V_OK || custom_cert_verifier_status_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 789 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 790 | if (!is_valid_cert_name && ignore_bad_cert_) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 791 | RTC_DLOG(LS_WARNING) << "Other TLS post connection checks failed. " |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 792 | "ignore_bad_cert_ set to true. Overriding name " |
| 793 | "verification failure!"; |
Benjamin Wright | 9201d1a | 2018-04-05 12:12:26 -0700 | [diff] [blame] | 794 | is_valid_cert_name = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 795 | } |
Benjamin Wright | 9201d1a | 2018-04-05 12:12:26 -0700 | [diff] [blame] | 796 | return is_valid_cert_name; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 797 | } |
| 798 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 799 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 800 | |
| 801 | // We only use this for tracing and so it is only needed in debug mode |
| 802 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 803 | void OpenSSLAdapter::SSLInfoCallback(const SSL* s, int where, int ret) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 804 | const char* str = "undefined"; |
| 805 | int w = where & ~SSL_ST_MASK; |
| 806 | if (w & SSL_ST_CONNECT) { |
| 807 | str = "SSL_connect"; |
| 808 | } else if (w & SSL_ST_ACCEPT) { |
| 809 | str = "SSL_accept"; |
| 810 | } |
| 811 | if (where & SSL_CB_LOOP) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 812 | RTC_DLOG(LS_INFO) << str << ":" << SSL_state_string_long(s); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 813 | } else if (where & SSL_CB_ALERT) { |
| 814 | str = (where & SSL_CB_READ) ? "read" : "write"; |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 815 | RTC_DLOG(LS_INFO) << "SSL3 alert " << str << ":" |
| 816 | << SSL_alert_type_string_long(ret) << ":" |
| 817 | << SSL_alert_desc_string_long(ret); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 818 | } else if (where & SSL_CB_EXIT) { |
| 819 | if (ret == 0) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 820 | RTC_DLOG(LS_INFO) << str << ":failed in " << SSL_state_string_long(s); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 821 | } else if (ret < 0) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 822 | RTC_DLOG(LS_INFO) << str << ":error in " << SSL_state_string_long(s); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 827 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 828 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 829 | int OpenSSLAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 830 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 831 | if (!ok) { |
| 832 | char data[256]; |
| 833 | X509* cert = X509_STORE_CTX_get_current_cert(store); |
| 834 | int depth = X509_STORE_CTX_get_error_depth(store); |
| 835 | int err = X509_STORE_CTX_get_error(store); |
| 836 | |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 837 | RTC_DLOG(LS_INFO) << "Error with certificate at depth: " << depth; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 838 | X509_NAME_oneline(X509_get_issuer_name(cert), data, sizeof(data)); |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 839 | RTC_DLOG(LS_INFO) << " issuer = " << data; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 840 | X509_NAME_oneline(X509_get_subject_name(cert), data, sizeof(data)); |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 841 | RTC_DLOG(LS_INFO) << " subject = " << data; |
| 842 | RTC_DLOG(LS_INFO) << " err = " << err << ":" |
| 843 | << X509_verify_cert_error_string(err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 844 | } |
| 845 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 846 | // Get our stream pointer from the store |
| 847 | SSL* ssl = reinterpret_cast<SSL*>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 848 | X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 849 | |
| 850 | OpenSSLAdapter* stream = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 851 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 852 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 853 | if (!ok && stream->ssl_cert_verifier_ != nullptr) { |
| 854 | RTC_LOG(LS_INFO) << "Invoking SSL Verify Callback."; |
| 855 | const OpenSSLCertificate cert(X509_STORE_CTX_get_current_cert(store)); |
| 856 | if (stream->ssl_cert_verifier_->Verify(cert)) { |
| 857 | stream->custom_cert_verifier_status_ = true; |
| 858 | RTC_LOG(LS_INFO) << "Validated certificate using custom callback"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 859 | ok = true; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 860 | } else { |
| 861 | RTC_LOG(LS_INFO) << "Failed to verify certificate using custom callback"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | // Should only be used for debugging and development. |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 866 | if (!ok && stream->ignore_bad_cert_) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 867 | RTC_DLOG(LS_WARNING) << "Ignoring cert error while verifying cert chain"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 868 | ok = 1; |
| 869 | } |
| 870 | |
| 871 | return ok; |
| 872 | } |
| 873 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 874 | int OpenSSLAdapter::NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session) { |
| 875 | OpenSSLAdapter* stream = |
| 876 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 877 | RTC_DCHECK(stream->ssl_session_cache_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 878 | RTC_LOG(LS_INFO) << "Caching SSL session for " << stream->ssl_host_name_; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 879 | stream->ssl_session_cache_->AddSession(stream->ssl_host_name_, session); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 880 | return 1; // We've taken ownership of the session; OpenSSL shouldn't free it. |
| 881 | } |
| 882 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 883 | SSL_CTX* OpenSSLAdapter::CreateContext(SSLMode mode, bool enable_cache) { |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 884 | // Use (D)TLS 1.2. |
| 885 | // Note: BoringSSL supports a range of versions by setting max/min version |
| 886 | // (Default V1.0 to V1.2). However (D)TLSv1_2_client_method functions used |
| 887 | // below in OpenSSL only support V1.2. |
| 888 | SSL_CTX* ctx = nullptr; |
| 889 | #ifdef OPENSSL_IS_BORINGSSL |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 890 | ctx = SSL_CTX_new(mode == SSL_MODE_DTLS ? DTLS_method() : TLS_method()); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 891 | #else |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 892 | ctx = SSL_CTX_new(mode == SSL_MODE_DTLS ? DTLSv1_2_client_method() |
| 893 | : TLSv1_2_client_method()); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 894 | #endif // OPENSSL_IS_BORINGSSL |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 895 | if (ctx == nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 896 | unsigned long error = ERR_get_error(); // NOLINT: type used by OpenSSL. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 897 | RTC_LOG(LS_WARNING) << "SSL_CTX creation failed: " << '"' |
| 898 | << ERR_reason_error_string(error) << "\" " |
| 899 | << "(error=" << error << ')'; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 900 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 901 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 902 | |
Mirko Bonadei | b889a20 | 2018-08-15 11:41:27 +0200 | [diff] [blame] | 903 | #ifndef WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 904 | if (!openssl::LoadBuiltinSSLRootCertificates(ctx)) { |
| 905 | RTC_LOG(LS_ERROR) << "SSL_CTX creation failed: Failed to load any trusted " |
| 906 | "ssl root certificates."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 907 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 908 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 909 | } |
Mirko Bonadei | b889a20 | 2018-08-15 11:41:27 +0200 | [diff] [blame] | 910 | #endif // WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 911 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 912 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 913 | SSL_CTX_set_info_callback(ctx, SSLInfoCallback); |
| 914 | #endif |
| 915 | |
| 916 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, SSLVerifyCallback); |
| 917 | SSL_CTX_set_verify_depth(ctx, 4); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 918 | // Use defaults, but disable HMAC-SHA256 and HMAC-SHA384 ciphers |
| 919 | // (note that SHA256 and SHA384 only select legacy CBC ciphers). |
| 920 | // Additionally disable HMAC-SHA1 ciphers in ECDSA. These are the remaining |
| 921 | // CBC-mode ECDSA ciphers. |
| 922 | SSL_CTX_set_cipher_list( |
| 923 | ctx, "ALL:!SHA256:!SHA384:!aPSK:!ECDSA+SHA1:!ADH:!LOW:!EXP:!MD5"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 924 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 925 | if (mode == SSL_MODE_DTLS) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 926 | SSL_CTX_set_read_ahead(ctx, 1); |
| 927 | } |
| 928 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 929 | if (enable_cache) { |
| 930 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); |
| 931 | SSL_CTX_sess_set_new_cb(ctx, &OpenSSLAdapter::NewSSLSessionCallback); |
| 932 | } |
| 933 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 934 | return ctx; |
| 935 | } |
| 936 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 937 | std::string TransformAlpnProtocols( |
| 938 | const std::vector<std::string>& alpn_protocols) { |
| 939 | // Transforms the alpn_protocols list to the format expected by |
| 940 | // Open/BoringSSL. This requires joining the protocols into a single string |
| 941 | // and prepending a character with the size of the protocol string before |
| 942 | // each protocol. |
| 943 | std::string transformed_alpn; |
| 944 | for (const std::string& proto : alpn_protocols) { |
| 945 | if (proto.size() == 0 || proto.size() > 0xFF) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 946 | RTC_LOG(LS_ERROR) << "OpenSSLAdapter::Error(" |
| 947 | << "TransformAlpnProtocols received proto with size " |
| 948 | << proto.size() << ")"; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 949 | return ""; |
| 950 | } |
| 951 | transformed_alpn += static_cast<char>(proto.size()); |
| 952 | transformed_alpn += proto; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 953 | RTC_LOG(LS_VERBOSE) << "TransformAlpnProtocols: Adding proto: " << proto; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 954 | } |
| 955 | return transformed_alpn; |
| 956 | } |
| 957 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 958 | ////////////////////////////////////////////////////////////////////// |
| 959 | // OpenSSLAdapterFactory |
| 960 | ////////////////////////////////////////////////////////////////////// |
| 961 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 962 | OpenSSLAdapterFactory::OpenSSLAdapterFactory() = default; |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 963 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 964 | OpenSSLAdapterFactory::~OpenSSLAdapterFactory() = default; |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 965 | |
| 966 | void OpenSSLAdapterFactory::SetMode(SSLMode mode) { |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 967 | RTC_DCHECK(!ssl_session_cache_); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 968 | ssl_mode_ = mode; |
| 969 | } |
| 970 | |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 971 | void OpenSSLAdapterFactory::SetCertVerifier( |
| 972 | SSLCertificateVerifier* ssl_cert_verifier) { |
| 973 | RTC_DCHECK(!ssl_session_cache_); |
| 974 | ssl_cert_verifier_ = ssl_cert_verifier; |
| 975 | } |
| 976 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 977 | OpenSSLAdapter* OpenSSLAdapterFactory::CreateAdapter(AsyncSocket* socket) { |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 978 | if (ssl_session_cache_ == nullptr) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 979 | SSL_CTX* ssl_ctx = OpenSSLAdapter::CreateContext(ssl_mode_, true); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 980 | if (ssl_ctx == nullptr) { |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 981 | return nullptr; |
| 982 | } |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 983 | // The OpenSSLSessionCache will upref the ssl_ctx. |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 984 | ssl_session_cache_ = |
| 985 | absl::make_unique<OpenSSLSessionCache>(ssl_mode_, ssl_ctx); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 986 | SSL_CTX_free(ssl_ctx); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 987 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 988 | return new OpenSSLAdapter(socket, ssl_session_cache_.get(), |
| 989 | ssl_cert_verifier_); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 992 | } // namespace rtc |