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 | |
| 11 | #if HAVE_OPENSSL_SSL_H |
| 12 | |
| 13 | #include "webrtc/base/openssladapter.h" |
| 14 | |
| 15 | #if defined(WEBRTC_POSIX) |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | |
| 19 | // Must be included first before openssl headers. |
| 20 | #include "webrtc/base/win32.h" // NOLINT |
| 21 | |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/crypto.h> |
| 24 | #include <openssl/err.h> |
| 25 | #include <openssl/opensslv.h> |
| 26 | #include <openssl/rand.h> |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 27 | #include <openssl/x509.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | #include <openssl/x509v3.h> |
| 29 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 30 | #include "webrtc/base/arraysize.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | #include "webrtc/base/common.h" |
| 32 | #include "webrtc/base/logging.h" |
| 33 | #include "webrtc/base/openssl.h" |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 34 | #include "webrtc/base/safe_conversions.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | #include "webrtc/base/sslroots.h" |
| 36 | #include "webrtc/base/stringutils.h" |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 37 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 39 | #ifndef OPENSSL_IS_BORINGSSL |
| 40 | |
| 41 | // TODO: Use a nicer abstraction for mutex. |
| 42 | |
| 43 | #if defined(WEBRTC_WIN) |
| 44 | #define MUTEX_TYPE HANDLE |
| 45 | #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL) |
| 46 | #define MUTEX_CLEANUP(x) CloseHandle(x) |
| 47 | #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) |
| 48 | #define MUTEX_UNLOCK(x) ReleaseMutex(x) |
| 49 | #define THREAD_ID GetCurrentThreadId() |
| 50 | #elif defined(WEBRTC_POSIX) |
| 51 | #define MUTEX_TYPE pthread_mutex_t |
| 52 | #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) |
| 53 | #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 54 | #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 55 | #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 56 | #define THREAD_ID pthread_self() |
| 57 | #else |
| 58 | #error You must define mutex operations appropriate for your platform! |
| 59 | #endif |
| 60 | |
| 61 | struct CRYPTO_dynlock_value { |
| 62 | MUTEX_TYPE mutex; |
| 63 | }; |
| 64 | |
| 65 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 66 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | ////////////////////////////////////////////////////////////////////// |
| 68 | // SocketBIO |
| 69 | ////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | static int socket_write(BIO* h, const char* buf, int num); |
| 72 | static int socket_read(BIO* h, char* buf, int size); |
| 73 | static int socket_puts(BIO* h, const char* str); |
| 74 | static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
| 75 | static int socket_new(BIO* h); |
| 76 | static int socket_free(BIO* data); |
| 77 | |
davidben@webrtc.org | 36d5c3c | 2015-01-22 23:06:17 +0000 | [diff] [blame] | 78 | // TODO(davidben): This should be const once BoringSSL is assumed. |
| 79 | static BIO_METHOD methods_socket = { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | BIO_TYPE_BIO, |
| 81 | "socket", |
| 82 | socket_write, |
| 83 | socket_read, |
| 84 | socket_puts, |
| 85 | 0, |
| 86 | socket_ctrl, |
| 87 | socket_new, |
| 88 | socket_free, |
| 89 | NULL, |
| 90 | }; |
| 91 | |
davidben@webrtc.org | 36d5c3c | 2015-01-22 23:06:17 +0000 | [diff] [blame] | 92 | static BIO_METHOD* BIO_s_socket2() { return(&methods_socket); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 94 | static BIO* BIO_new_socket(rtc::AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | BIO* ret = BIO_new(BIO_s_socket2()); |
| 96 | if (ret == NULL) { |
| 97 | return NULL; |
| 98 | } |
| 99 | ret->ptr = socket; |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int socket_new(BIO* b) { |
| 104 | b->shutdown = 0; |
| 105 | b->init = 1; |
| 106 | b->num = 0; // 1 means socket closed |
| 107 | b->ptr = 0; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static int socket_free(BIO* b) { |
| 112 | if (b == NULL) |
| 113 | return 0; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | static int socket_read(BIO* b, char* out, int outl) { |
| 118 | if (!out) |
| 119 | return -1; |
| 120 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(b->ptr); |
| 121 | BIO_clear_retry_flags(b); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 122 | int result = socket->Recv(out, outl, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | if (result > 0) { |
| 124 | return result; |
| 125 | } else if (result == 0) { |
| 126 | b->num = 1; |
| 127 | } else if (socket->IsBlocking()) { |
| 128 | BIO_set_retry_read(b); |
| 129 | } |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | static int socket_write(BIO* b, const char* in, int inl) { |
| 134 | if (!in) |
| 135 | return -1; |
| 136 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(b->ptr); |
| 137 | BIO_clear_retry_flags(b); |
| 138 | int result = socket->Send(in, inl); |
| 139 | if (result > 0) { |
| 140 | return result; |
| 141 | } else if (socket->IsBlocking()) { |
| 142 | BIO_set_retry_write(b); |
| 143 | } |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | static int socket_puts(BIO* b, const char* str) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 148 | return socket_write(b, str, rtc::checked_cast<int>(strlen(str))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static long socket_ctrl(BIO* b, int cmd, long num, void* ptr) { |
henrike@webrtc.org | 14abcc7 | 2014-05-16 16:54:44 +0000 | [diff] [blame] | 152 | RTC_UNUSED(num); |
| 153 | RTC_UNUSED(ptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | |
| 155 | switch (cmd) { |
| 156 | case BIO_CTRL_RESET: |
| 157 | return 0; |
| 158 | case BIO_CTRL_EOF: |
| 159 | return b->num; |
| 160 | case BIO_CTRL_WPENDING: |
| 161 | case BIO_CTRL_PENDING: |
| 162 | return 0; |
| 163 | case BIO_CTRL_FLUSH: |
| 164 | return 1; |
| 165 | default: |
| 166 | return 0; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | ///////////////////////////////////////////////////////////////////////////// |
| 171 | // OpenSSLAdapter |
| 172 | ///////////////////////////////////////////////////////////////////////////// |
| 173 | |
| 174 | namespace rtc { |
| 175 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 176 | #ifndef OPENSSL_IS_BORINGSSL |
| 177 | |
| 178 | // This array will store all of the mutexes available to OpenSSL. |
| 179 | static MUTEX_TYPE* mutex_buf = NULL; |
| 180 | |
| 181 | static void locking_function(int mode, int n, const char * file, int line) { |
| 182 | if (mode & CRYPTO_LOCK) { |
| 183 | MUTEX_LOCK(mutex_buf[n]); |
| 184 | } else { |
| 185 | MUTEX_UNLOCK(mutex_buf[n]); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static unsigned long id_function() { // NOLINT |
| 190 | // Use old-style C cast because THREAD_ID's type varies with the platform, |
| 191 | // in some cases requiring static_cast, and in others requiring |
| 192 | // reinterpret_cast. |
| 193 | return (unsigned long)THREAD_ID; // NOLINT |
| 194 | } |
| 195 | |
| 196 | static CRYPTO_dynlock_value* dyn_create_function(const char* file, int line) { |
| 197 | CRYPTO_dynlock_value* value = new CRYPTO_dynlock_value; |
| 198 | if (!value) |
| 199 | return NULL; |
| 200 | MUTEX_SETUP(value->mutex); |
| 201 | return value; |
| 202 | } |
| 203 | |
| 204 | static void dyn_lock_function(int mode, CRYPTO_dynlock_value* l, |
| 205 | const char* file, int line) { |
| 206 | if (mode & CRYPTO_LOCK) { |
| 207 | MUTEX_LOCK(l->mutex); |
| 208 | } else { |
| 209 | MUTEX_UNLOCK(l->mutex); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static void dyn_destroy_function(CRYPTO_dynlock_value* l, |
| 214 | const char* file, int line) { |
| 215 | MUTEX_CLEANUP(l->mutex); |
| 216 | delete l; |
| 217 | } |
| 218 | |
| 219 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 220 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | VerificationCallback OpenSSLAdapter::custom_verify_callback_ = NULL; |
| 222 | |
| 223 | bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 224 | if (!InitializeSSLThread() || !SSL_library_init()) |
| 225 | return false; |
| 226 | #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 227 | // Loading the error strings crashes mac_asan. Omit this debugging aid there. |
| 228 | SSL_load_error_strings(); |
| 229 | #endif |
| 230 | ERR_load_BIO_strings(); |
| 231 | OpenSSL_add_all_algorithms(); |
| 232 | RAND_poll(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 233 | custom_verify_callback_ = callback; |
| 234 | return true; |
| 235 | } |
| 236 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 237 | bool OpenSSLAdapter::InitializeSSLThread() { |
| 238 | // BoringSSL is doing the locking internally, so the callbacks are not used |
| 239 | // in this case (and are no-ops anyways). |
| 240 | #ifndef OPENSSL_IS_BORINGSSL |
| 241 | mutex_buf = new MUTEX_TYPE[CRYPTO_num_locks()]; |
| 242 | if (!mutex_buf) |
| 243 | return false; |
| 244 | for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 245 | MUTEX_SETUP(mutex_buf[i]); |
| 246 | |
| 247 | // we need to cast our id_function to return an unsigned long -- pthread_t is |
| 248 | // a pointer |
| 249 | CRYPTO_set_id_callback(id_function); |
| 250 | CRYPTO_set_locking_callback(locking_function); |
| 251 | CRYPTO_set_dynlock_create_callback(dyn_create_function); |
| 252 | CRYPTO_set_dynlock_lock_callback(dyn_lock_function); |
| 253 | CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); |
| 254 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool OpenSSLAdapter::CleanupSSL() { |
| 259 | #ifndef OPENSSL_IS_BORINGSSL |
| 260 | if (!mutex_buf) |
| 261 | return false; |
| 262 | CRYPTO_set_id_callback(NULL); |
| 263 | CRYPTO_set_locking_callback(NULL); |
| 264 | CRYPTO_set_dynlock_create_callback(NULL); |
| 265 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 266 | CRYPTO_set_dynlock_destroy_callback(NULL); |
| 267 | for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 268 | MUTEX_CLEANUP(mutex_buf[i]); |
| 269 | delete [] mutex_buf; |
| 270 | mutex_buf = NULL; |
| 271 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 272 | return true; |
| 273 | } |
| 274 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 275 | OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket) |
| 276 | : SSLAdapter(socket), |
| 277 | state_(SSL_NONE), |
| 278 | ssl_read_needs_write_(false), |
| 279 | ssl_write_needs_read_(false), |
| 280 | restartable_(false), |
| 281 | ssl_(NULL), ssl_ctx_(NULL), |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 282 | ssl_mode_(SSL_MODE_TLS), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 283 | custom_verification_succeeded_(false) { |
| 284 | } |
| 285 | |
| 286 | OpenSSLAdapter::~OpenSSLAdapter() { |
| 287 | Cleanup(); |
| 288 | } |
| 289 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 290 | void |
| 291 | OpenSSLAdapter::SetMode(SSLMode mode) { |
| 292 | ASSERT(state_ == SSL_NONE); |
| 293 | ssl_mode_ = mode; |
| 294 | } |
| 295 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 296 | int |
| 297 | OpenSSLAdapter::StartSSL(const char* hostname, bool restartable) { |
| 298 | if (state_ != SSL_NONE) |
| 299 | return -1; |
| 300 | |
| 301 | ssl_host_name_ = hostname; |
| 302 | restartable_ = restartable; |
| 303 | |
| 304 | if (socket_->GetState() != Socket::CS_CONNECTED) { |
| 305 | state_ = SSL_WAIT; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | state_ = SSL_CONNECTING; |
| 310 | if (int err = BeginSSL()) { |
| 311 | Error("BeginSSL", err, false); |
| 312 | return err; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int |
| 319 | OpenSSLAdapter::BeginSSL() { |
| 320 | LOG(LS_INFO) << "BeginSSL: " << ssl_host_name_; |
| 321 | ASSERT(state_ == SSL_CONNECTING); |
| 322 | |
| 323 | int err = 0; |
| 324 | BIO* bio = NULL; |
| 325 | |
| 326 | // First set up the context |
| 327 | if (!ssl_ctx_) |
| 328 | ssl_ctx_ = SetupSSLContext(); |
| 329 | |
| 330 | if (!ssl_ctx_) { |
| 331 | err = -1; |
| 332 | goto ssl_error; |
| 333 | } |
| 334 | |
Peter Boström | 0b518bf | 2016-01-27 12:35:40 +0100 | [diff] [blame] | 335 | bio = BIO_new_socket(socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 336 | if (!bio) { |
| 337 | err = -1; |
| 338 | goto ssl_error; |
| 339 | } |
| 340 | |
| 341 | ssl_ = SSL_new(ssl_ctx_); |
| 342 | if (!ssl_) { |
| 343 | err = -1; |
| 344 | goto ssl_error; |
| 345 | } |
| 346 | |
| 347 | SSL_set_app_data(ssl_, this); |
| 348 | |
| 349 | SSL_set_bio(ssl_, bio, bio); |
| 350 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 351 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 352 | |
| 353 | // the SSL object owns the bio now |
| 354 | bio = NULL; |
| 355 | |
| 356 | // Do the connect |
| 357 | err = ContinueSSL(); |
| 358 | if (err != 0) |
| 359 | goto ssl_error; |
| 360 | |
| 361 | return err; |
| 362 | |
| 363 | ssl_error: |
| 364 | Cleanup(); |
| 365 | if (bio) |
| 366 | BIO_free(bio); |
| 367 | |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | int |
| 372 | OpenSSLAdapter::ContinueSSL() { |
| 373 | ASSERT(state_ == SSL_CONNECTING); |
| 374 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 375 | // Clear the DTLS timer |
| 376 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 377 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 378 | int code = SSL_connect(ssl_); |
| 379 | switch (SSL_get_error(ssl_, code)) { |
| 380 | case SSL_ERROR_NONE: |
| 381 | if (!SSLPostConnectionCheck(ssl_, ssl_host_name_.c_str())) { |
| 382 | LOG(LS_ERROR) << "TLS post connection check failed"; |
| 383 | // make sure we close the socket |
| 384 | Cleanup(); |
| 385 | // The connect failed so return -1 to shut down the socket |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | state_ = SSL_CONNECTED; |
| 390 | AsyncSocketAdapter::OnConnectEvent(this); |
| 391 | #if 0 // TODO: worry about this |
| 392 | // Don't let ourselves go away during the callbacks |
| 393 | PRefPtr<OpenSSLAdapter> lock(this); |
| 394 | LOG(LS_INFO) << " -- onStreamReadable"; |
| 395 | AsyncSocketAdapter::OnReadEvent(this); |
| 396 | LOG(LS_INFO) << " -- onStreamWriteable"; |
| 397 | AsyncSocketAdapter::OnWriteEvent(this); |
| 398 | #endif |
| 399 | break; |
| 400 | |
| 401 | case SSL_ERROR_WANT_READ: |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 402 | LOG(LS_VERBOSE) << " -- error want read"; |
| 403 | struct timeval timeout; |
| 404 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 405 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; |
| 406 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 407 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT, |
| 408 | 0); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 409 | } |
| 410 | break; |
| 411 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | case SSL_ERROR_WANT_WRITE: |
| 413 | break; |
| 414 | |
| 415 | case SSL_ERROR_ZERO_RETURN: |
| 416 | default: |
| 417 | LOG(LS_WARNING) << "ContinueSSL -- error " << code; |
| 418 | return (code != 0) ? code : -1; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | OpenSSLAdapter::Error(const char* context, int err, bool signal) { |
| 426 | LOG(LS_WARNING) << "OpenSSLAdapter::Error(" |
| 427 | << context << ", " << err << ")"; |
| 428 | state_ = SSL_ERROR; |
| 429 | SetError(err); |
| 430 | if (signal) |
| 431 | AsyncSocketAdapter::OnCloseEvent(this, err); |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | OpenSSLAdapter::Cleanup() { |
| 436 | LOG(LS_INFO) << "Cleanup"; |
| 437 | |
| 438 | state_ = SSL_NONE; |
| 439 | ssl_read_needs_write_ = false; |
| 440 | ssl_write_needs_read_ = false; |
| 441 | custom_verification_succeeded_ = false; |
| 442 | |
| 443 | if (ssl_) { |
| 444 | SSL_free(ssl_); |
| 445 | ssl_ = NULL; |
| 446 | } |
| 447 | |
| 448 | if (ssl_ctx_) { |
| 449 | SSL_CTX_free(ssl_ctx_); |
| 450 | ssl_ctx_ = NULL; |
| 451 | } |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 452 | |
| 453 | // Clear the DTLS timer |
| 454 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // |
| 458 | // AsyncSocket Implementation |
| 459 | // |
| 460 | |
| 461 | int |
| 462 | OpenSSLAdapter::Send(const void* pv, size_t cb) { |
| 463 | //LOG(LS_INFO) << "OpenSSLAdapter::Send(" << cb << ")"; |
| 464 | |
| 465 | switch (state_) { |
| 466 | case SSL_NONE: |
| 467 | return AsyncSocketAdapter::Send(pv, cb); |
| 468 | |
| 469 | case SSL_WAIT: |
| 470 | case SSL_CONNECTING: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame^] | 471 | SetError(ENOTCONN); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 472 | return SOCKET_ERROR; |
| 473 | |
| 474 | case SSL_CONNECTED: |
| 475 | break; |
| 476 | |
| 477 | case SSL_ERROR: |
| 478 | default: |
| 479 | return SOCKET_ERROR; |
| 480 | } |
| 481 | |
| 482 | // OpenSSL will return an error if we try to write zero bytes |
| 483 | if (cb == 0) |
| 484 | return 0; |
| 485 | |
| 486 | ssl_write_needs_read_ = false; |
| 487 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 488 | int code = SSL_write(ssl_, pv, checked_cast<int>(cb)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 489 | switch (SSL_get_error(ssl_, code)) { |
| 490 | case SSL_ERROR_NONE: |
| 491 | //LOG(LS_INFO) << " -- success"; |
| 492 | return code; |
| 493 | case SSL_ERROR_WANT_READ: |
| 494 | //LOG(LS_INFO) << " -- error want read"; |
| 495 | ssl_write_needs_read_ = true; |
| 496 | SetError(EWOULDBLOCK); |
| 497 | break; |
| 498 | case SSL_ERROR_WANT_WRITE: |
| 499 | //LOG(LS_INFO) << " -- error want write"; |
| 500 | SetError(EWOULDBLOCK); |
| 501 | break; |
| 502 | case SSL_ERROR_ZERO_RETURN: |
| 503 | //LOG(LS_INFO) << " -- remote side closed"; |
| 504 | SetError(EWOULDBLOCK); |
| 505 | // do we need to signal closure? |
| 506 | break; |
| 507 | default: |
| 508 | //LOG(LS_INFO) << " -- error " << code; |
| 509 | Error("SSL_write", (code ? code : -1), false); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | return SOCKET_ERROR; |
| 514 | } |
| 515 | |
| 516 | int |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 517 | OpenSSLAdapter::SendTo(const void* pv, size_t cb, const SocketAddress& addr) { |
| 518 | if (socket_->GetState() == Socket::CS_CONNECTED && |
| 519 | addr == socket_->GetRemoteAddress()) { |
| 520 | return Send(pv, cb); |
| 521 | } |
| 522 | |
| 523 | SetError(ENOTCONN); |
| 524 | |
| 525 | return SOCKET_ERROR; |
| 526 | } |
| 527 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 528 | int OpenSSLAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | //LOG(LS_INFO) << "OpenSSLAdapter::Recv(" << cb << ")"; |
| 530 | switch (state_) { |
| 531 | |
| 532 | case SSL_NONE: |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 533 | return AsyncSocketAdapter::Recv(pv, cb, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 534 | |
| 535 | case SSL_WAIT: |
| 536 | case SSL_CONNECTING: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame^] | 537 | SetError(ENOTCONN); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 538 | return SOCKET_ERROR; |
| 539 | |
| 540 | case SSL_CONNECTED: |
| 541 | break; |
| 542 | |
| 543 | case SSL_ERROR: |
| 544 | default: |
| 545 | return SOCKET_ERROR; |
| 546 | } |
| 547 | |
| 548 | // Don't trust OpenSSL with zero byte reads |
| 549 | if (cb == 0) |
| 550 | return 0; |
| 551 | |
| 552 | ssl_read_needs_write_ = false; |
| 553 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 554 | int code = SSL_read(ssl_, pv, checked_cast<int>(cb)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 555 | switch (SSL_get_error(ssl_, code)) { |
| 556 | case SSL_ERROR_NONE: |
| 557 | //LOG(LS_INFO) << " -- success"; |
| 558 | return code; |
| 559 | case SSL_ERROR_WANT_READ: |
| 560 | //LOG(LS_INFO) << " -- error want read"; |
| 561 | SetError(EWOULDBLOCK); |
| 562 | break; |
| 563 | case SSL_ERROR_WANT_WRITE: |
| 564 | //LOG(LS_INFO) << " -- error want write"; |
| 565 | ssl_read_needs_write_ = true; |
| 566 | SetError(EWOULDBLOCK); |
| 567 | break; |
| 568 | case SSL_ERROR_ZERO_RETURN: |
| 569 | //LOG(LS_INFO) << " -- remote side closed"; |
| 570 | SetError(EWOULDBLOCK); |
| 571 | // do we need to signal closure? |
| 572 | break; |
| 573 | default: |
| 574 | //LOG(LS_INFO) << " -- error " << code; |
| 575 | Error("SSL_read", (code ? code : -1), false); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | return SOCKET_ERROR; |
| 580 | } |
| 581 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 582 | int OpenSSLAdapter::RecvFrom(void* pv, |
| 583 | size_t cb, |
| 584 | SocketAddress* paddr, |
| 585 | int64_t* timestamp) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 586 | if (socket_->GetState() == Socket::CS_CONNECTED) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 587 | int ret = Recv(pv, cb, timestamp); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 588 | |
| 589 | *paddr = GetRemoteAddress(); |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | SetError(ENOTCONN); |
| 595 | |
| 596 | return SOCKET_ERROR; |
| 597 | } |
| 598 | |
| 599 | int |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 600 | OpenSSLAdapter::Close() { |
| 601 | Cleanup(); |
| 602 | state_ = restartable_ ? SSL_WAIT : SSL_NONE; |
| 603 | return AsyncSocketAdapter::Close(); |
| 604 | } |
| 605 | |
| 606 | Socket::ConnState |
| 607 | OpenSSLAdapter::GetState() const { |
| 608 | //if (signal_close_) |
| 609 | // return CS_CONNECTED; |
| 610 | ConnState state = socket_->GetState(); |
| 611 | if ((state == CS_CONNECTED) |
| 612 | && ((state_ == SSL_WAIT) || (state_ == SSL_CONNECTING))) |
| 613 | state = CS_CONNECTING; |
| 614 | return state; |
| 615 | } |
| 616 | |
| 617 | void |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 618 | OpenSSLAdapter::OnMessage(Message* msg) { |
| 619 | if (MSG_TIMEOUT == msg->message_id) { |
| 620 | LOG(LS_INFO) << "DTLS timeout expired"; |
| 621 | DTLSv1_handle_timeout(ssl_); |
| 622 | ContinueSSL(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | void |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 627 | OpenSSLAdapter::OnConnectEvent(AsyncSocket* socket) { |
| 628 | LOG(LS_INFO) << "OpenSSLAdapter::OnConnectEvent"; |
| 629 | if (state_ != SSL_WAIT) { |
| 630 | ASSERT(state_ == SSL_NONE); |
| 631 | AsyncSocketAdapter::OnConnectEvent(socket); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | state_ = SSL_CONNECTING; |
| 636 | if (int err = BeginSSL()) { |
| 637 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | void |
| 642 | OpenSSLAdapter::OnReadEvent(AsyncSocket* socket) { |
| 643 | //LOG(LS_INFO) << "OpenSSLAdapter::OnReadEvent"; |
| 644 | |
| 645 | if (state_ == SSL_NONE) { |
| 646 | AsyncSocketAdapter::OnReadEvent(socket); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | if (state_ == SSL_CONNECTING) { |
| 651 | if (int err = ContinueSSL()) { |
| 652 | Error("ContinueSSL", err); |
| 653 | } |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | if (state_ != SSL_CONNECTED) |
| 658 | return; |
| 659 | |
| 660 | // Don't let ourselves go away during the callbacks |
| 661 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 662 | if (ssl_write_needs_read_) { |
| 663 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 664 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 665 | } |
| 666 | |
| 667 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 668 | AsyncSocketAdapter::OnReadEvent(socket); |
| 669 | } |
| 670 | |
| 671 | void |
| 672 | OpenSSLAdapter::OnWriteEvent(AsyncSocket* socket) { |
| 673 | //LOG(LS_INFO) << "OpenSSLAdapter::OnWriteEvent"; |
| 674 | |
| 675 | if (state_ == SSL_NONE) { |
| 676 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | if (state_ == SSL_CONNECTING) { |
| 681 | if (int err = ContinueSSL()) { |
| 682 | Error("ContinueSSL", err); |
| 683 | } |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | if (state_ != SSL_CONNECTED) |
| 688 | return; |
| 689 | |
| 690 | // Don't let ourselves go away during the callbacks |
| 691 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 692 | |
| 693 | if (ssl_read_needs_write_) { |
| 694 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 695 | AsyncSocketAdapter::OnReadEvent(socket); |
| 696 | } |
| 697 | |
| 698 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 699 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 700 | } |
| 701 | |
| 702 | void |
| 703 | OpenSSLAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
| 704 | LOG(LS_INFO) << "OpenSSLAdapter::OnCloseEvent(" << err << ")"; |
| 705 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 706 | } |
| 707 | |
| 708 | // This code is taken from the "Network Security with OpenSSL" |
| 709 | // sample in chapter 5 |
| 710 | |
| 711 | bool OpenSSLAdapter::VerifyServerName(SSL* ssl, const char* host, |
| 712 | bool ignore_bad_cert) { |
| 713 | if (!host) |
| 714 | return false; |
| 715 | |
| 716 | // Checking the return from SSL_get_peer_certificate here is not strictly |
| 717 | // necessary. With our setup, it is not possible for it to return |
| 718 | // NULL. However, it is good form to check the return. |
| 719 | X509* certificate = SSL_get_peer_certificate(ssl); |
| 720 | if (!certificate) |
| 721 | return false; |
| 722 | |
| 723 | // Logging certificates is extremely verbose. So it is disabled by default. |
| 724 | #ifdef LOG_CERTIFICATES |
| 725 | { |
| 726 | LOG(LS_INFO) << "Certificate from server:"; |
| 727 | BIO* mem = BIO_new(BIO_s_mem()); |
| 728 | X509_print_ex(mem, certificate, XN_FLAG_SEP_CPLUS_SPC, X509_FLAG_NO_HEADER); |
| 729 | BIO_write(mem, "\0", 1); |
| 730 | char* buffer; |
| 731 | BIO_get_mem_data(mem, &buffer); |
| 732 | LOG(LS_INFO) << buffer; |
| 733 | BIO_free(mem); |
| 734 | |
| 735 | char* cipher_description = |
| 736 | SSL_CIPHER_description(SSL_get_current_cipher(ssl), NULL, 128); |
| 737 | LOG(LS_INFO) << "Cipher: " << cipher_description; |
| 738 | OPENSSL_free(cipher_description); |
| 739 | } |
| 740 | #endif |
| 741 | |
| 742 | bool ok = false; |
| 743 | int extension_count = X509_get_ext_count(certificate); |
| 744 | for (int i = 0; i < extension_count; ++i) { |
| 745 | X509_EXTENSION* extension = X509_get_ext(certificate, i); |
| 746 | int extension_nid = OBJ_obj2nid(X509_EXTENSION_get_object(extension)); |
| 747 | |
| 748 | if (extension_nid == NID_subject_alt_name) { |
| 749 | const X509V3_EXT_METHOD* meth = X509V3_EXT_get(extension); |
| 750 | if (!meth) |
| 751 | break; |
| 752 | |
| 753 | void* ext_str = NULL; |
| 754 | |
| 755 | // We assign this to a local variable, instead of passing the address |
| 756 | // directly to ASN1_item_d2i. |
| 757 | // See http://readlist.com/lists/openssl.org/openssl-users/0/4761.html. |
| 758 | unsigned char* ext_value_data = extension->value->data; |
| 759 | |
| 760 | const unsigned char **ext_value_data_ptr = |
| 761 | (const_cast<const unsigned char **>(&ext_value_data)); |
| 762 | |
| 763 | if (meth->it) { |
| 764 | ext_str = ASN1_item_d2i(NULL, ext_value_data_ptr, |
| 765 | extension->value->length, |
| 766 | ASN1_ITEM_ptr(meth->it)); |
| 767 | } else { |
| 768 | ext_str = meth->d2i(NULL, ext_value_data_ptr, extension->value->length); |
| 769 | } |
| 770 | |
| 771 | STACK_OF(CONF_VALUE)* value = meth->i2v(meth, ext_str, NULL); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 772 | |
| 773 | // Cast to size_t to be compilable for both OpenSSL and BoringSSL. |
| 774 | for (size_t j = 0; j < static_cast<size_t>(sk_CONF_VALUE_num(value)); |
| 775 | ++j) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 776 | CONF_VALUE* nval = sk_CONF_VALUE_value(value, j); |
| 777 | // The value for nval can contain wildcards |
| 778 | if (!strcmp(nval->name, "DNS") && string_match(host, nval->value)) { |
| 779 | ok = true; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | sk_CONF_VALUE_pop_free(value, X509V3_conf_free); |
| 784 | value = NULL; |
| 785 | |
| 786 | if (meth->it) { |
| 787 | ASN1_item_free(reinterpret_cast<ASN1_VALUE*>(ext_str), |
| 788 | ASN1_ITEM_ptr(meth->it)); |
| 789 | } else { |
| 790 | meth->ext_free(ext_str); |
| 791 | } |
| 792 | ext_str = NULL; |
| 793 | } |
| 794 | if (ok) |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | char data[256]; |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 799 | X509_NAME* subject; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 800 | if (!ok |
| 801 | && ((subject = X509_get_subject_name(certificate)) != NULL) |
| 802 | && (X509_NAME_get_text_by_NID(subject, NID_commonName, |
| 803 | data, sizeof(data)) > 0)) { |
| 804 | data[sizeof(data)-1] = 0; |
| 805 | if (_stricmp(data, host) == 0) |
| 806 | ok = true; |
| 807 | } |
| 808 | |
| 809 | X509_free(certificate); |
| 810 | |
| 811 | // This should only ever be turned on for debugging and development. |
| 812 | if (!ok && ignore_bad_cert) { |
| 813 | LOG(LS_WARNING) << "TLS certificate check FAILED. " |
| 814 | << "Allowing connection anyway."; |
| 815 | ok = true; |
| 816 | } |
| 817 | |
| 818 | return ok; |
| 819 | } |
| 820 | |
| 821 | bool OpenSSLAdapter::SSLPostConnectionCheck(SSL* ssl, const char* host) { |
| 822 | bool ok = VerifyServerName(ssl, host, ignore_bad_cert()); |
| 823 | |
| 824 | if (ok) { |
| 825 | ok = (SSL_get_verify_result(ssl) == X509_V_OK || |
| 826 | custom_verification_succeeded_); |
| 827 | } |
| 828 | |
| 829 | if (!ok && ignore_bad_cert()) { |
| 830 | LOG(LS_INFO) << "Other TLS post connection checks failed."; |
| 831 | ok = true; |
| 832 | } |
| 833 | |
| 834 | return ok; |
| 835 | } |
| 836 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 837 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 838 | |
| 839 | // We only use this for tracing and so it is only needed in debug mode |
| 840 | |
| 841 | void |
| 842 | OpenSSLAdapter::SSLInfoCallback(const SSL* s, int where, int ret) { |
| 843 | const char* str = "undefined"; |
| 844 | int w = where & ~SSL_ST_MASK; |
| 845 | if (w & SSL_ST_CONNECT) { |
| 846 | str = "SSL_connect"; |
| 847 | } else if (w & SSL_ST_ACCEPT) { |
| 848 | str = "SSL_accept"; |
| 849 | } |
| 850 | if (where & SSL_CB_LOOP) { |
| 851 | LOG(LS_INFO) << str << ":" << SSL_state_string_long(s); |
| 852 | } else if (where & SSL_CB_ALERT) { |
| 853 | str = (where & SSL_CB_READ) ? "read" : "write"; |
| 854 | LOG(LS_INFO) << "SSL3 alert " << str |
| 855 | << ":" << SSL_alert_type_string_long(ret) |
| 856 | << ":" << SSL_alert_desc_string_long(ret); |
| 857 | } else if (where & SSL_CB_EXIT) { |
| 858 | if (ret == 0) { |
| 859 | LOG(LS_INFO) << str << ":failed in " << SSL_state_string_long(s); |
| 860 | } else if (ret < 0) { |
| 861 | LOG(LS_INFO) << str << ":error in " << SSL_state_string_long(s); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 866 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 867 | |
| 868 | int |
| 869 | OpenSSLAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 870 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 871 | if (!ok) { |
| 872 | char data[256]; |
| 873 | X509* cert = X509_STORE_CTX_get_current_cert(store); |
| 874 | int depth = X509_STORE_CTX_get_error_depth(store); |
| 875 | int err = X509_STORE_CTX_get_error(store); |
| 876 | |
| 877 | LOG(LS_INFO) << "Error with certificate at depth: " << depth; |
| 878 | X509_NAME_oneline(X509_get_issuer_name(cert), data, sizeof(data)); |
| 879 | LOG(LS_INFO) << " issuer = " << data; |
| 880 | X509_NAME_oneline(X509_get_subject_name(cert), data, sizeof(data)); |
| 881 | LOG(LS_INFO) << " subject = " << data; |
| 882 | LOG(LS_INFO) << " err = " << err |
| 883 | << ":" << X509_verify_cert_error_string(err); |
| 884 | } |
| 885 | #endif |
| 886 | |
| 887 | // Get our stream pointer from the store |
| 888 | SSL* ssl = reinterpret_cast<SSL*>( |
| 889 | X509_STORE_CTX_get_ex_data(store, |
| 890 | SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 891 | |
| 892 | OpenSSLAdapter* stream = |
| 893 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
| 894 | |
| 895 | if (!ok && custom_verify_callback_) { |
| 896 | void* cert = |
| 897 | reinterpret_cast<void*>(X509_STORE_CTX_get_current_cert(store)); |
| 898 | if (custom_verify_callback_(cert)) { |
| 899 | stream->custom_verification_succeeded_ = true; |
| 900 | LOG(LS_INFO) << "validated certificate using custom callback"; |
| 901 | ok = true; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | // Should only be used for debugging and development. |
| 906 | if (!ok && stream->ignore_bad_cert()) { |
| 907 | LOG(LS_WARNING) << "Ignoring cert error while verifying cert chain"; |
| 908 | ok = 1; |
| 909 | } |
| 910 | |
| 911 | return ok; |
| 912 | } |
| 913 | |
| 914 | bool OpenSSLAdapter::ConfigureTrustedRootCertificates(SSL_CTX* ctx) { |
| 915 | // Add the root cert that we care about to the SSL context |
| 916 | int count_of_added_certs = 0; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 917 | for (size_t i = 0; i < arraysize(kSSLCertCertificateList); i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 918 | const unsigned char* cert_buffer = kSSLCertCertificateList[i]; |
| 919 | size_t cert_buffer_len = kSSLCertCertificateSizeList[i]; |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 920 | X509* cert = d2i_X509(NULL, &cert_buffer, |
| 921 | checked_cast<long>(cert_buffer_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 922 | if (cert) { |
| 923 | int return_value = X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), cert); |
| 924 | if (return_value == 0) { |
| 925 | LOG(LS_WARNING) << "Unable to add certificate."; |
| 926 | } else { |
| 927 | count_of_added_certs++; |
| 928 | } |
| 929 | X509_free(cert); |
| 930 | } |
| 931 | } |
| 932 | return count_of_added_certs > 0; |
| 933 | } |
| 934 | |
| 935 | SSL_CTX* |
| 936 | OpenSSLAdapter::SetupSSLContext() { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 937 | SSL_CTX* ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? |
| 938 | DTLSv1_client_method() : TLSv1_client_method()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 939 | if (ctx == NULL) { |
| 940 | unsigned long error = ERR_get_error(); // NOLINT: type used by OpenSSL. |
| 941 | LOG(LS_WARNING) << "SSL_CTX creation failed: " |
| 942 | << '"' << ERR_reason_error_string(error) << "\" " |
| 943 | << "(error=" << error << ')'; |
| 944 | return NULL; |
| 945 | } |
| 946 | if (!ConfigureTrustedRootCertificates(ctx)) { |
| 947 | SSL_CTX_free(ctx); |
| 948 | return NULL; |
| 949 | } |
| 950 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 951 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 952 | SSL_CTX_set_info_callback(ctx, SSLInfoCallback); |
| 953 | #endif |
| 954 | |
| 955 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, SSLVerifyCallback); |
| 956 | SSL_CTX_set_verify_depth(ctx, 4); |
| 957 | SSL_CTX_set_cipher_list(ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
| 958 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 959 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 960 | SSL_CTX_set_read_ahead(ctx, 1); |
| 961 | } |
| 962 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 963 | return ctx; |
| 964 | } |
| 965 | |
| 966 | } // namespace rtc |
| 967 | |
| 968 | #endif // HAVE_OPENSSL_SSL_H |