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 | |
| 407 | Thread::Current()->PostDelayed(delay, this, MSG_TIMEOUT, 0); |
| 408 | } |
| 409 | break; |
| 410 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 411 | case SSL_ERROR_WANT_WRITE: |
| 412 | break; |
| 413 | |
| 414 | case SSL_ERROR_ZERO_RETURN: |
| 415 | default: |
| 416 | LOG(LS_WARNING) << "ContinueSSL -- error " << code; |
| 417 | return (code != 0) ? code : -1; |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | OpenSSLAdapter::Error(const char* context, int err, bool signal) { |
| 425 | LOG(LS_WARNING) << "OpenSSLAdapter::Error(" |
| 426 | << context << ", " << err << ")"; |
| 427 | state_ = SSL_ERROR; |
| 428 | SetError(err); |
| 429 | if (signal) |
| 430 | AsyncSocketAdapter::OnCloseEvent(this, err); |
| 431 | } |
| 432 | |
| 433 | void |
| 434 | OpenSSLAdapter::Cleanup() { |
| 435 | LOG(LS_INFO) << "Cleanup"; |
| 436 | |
| 437 | state_ = SSL_NONE; |
| 438 | ssl_read_needs_write_ = false; |
| 439 | ssl_write_needs_read_ = false; |
| 440 | custom_verification_succeeded_ = false; |
| 441 | |
| 442 | if (ssl_) { |
| 443 | SSL_free(ssl_); |
| 444 | ssl_ = NULL; |
| 445 | } |
| 446 | |
| 447 | if (ssl_ctx_) { |
| 448 | SSL_CTX_free(ssl_ctx_); |
| 449 | ssl_ctx_ = NULL; |
| 450 | } |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 451 | |
| 452 | // Clear the DTLS timer |
| 453 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // |
| 457 | // AsyncSocket Implementation |
| 458 | // |
| 459 | |
| 460 | int |
| 461 | OpenSSLAdapter::Send(const void* pv, size_t cb) { |
| 462 | //LOG(LS_INFO) << "OpenSSLAdapter::Send(" << cb << ")"; |
| 463 | |
| 464 | switch (state_) { |
| 465 | case SSL_NONE: |
| 466 | return AsyncSocketAdapter::Send(pv, cb); |
| 467 | |
| 468 | case SSL_WAIT: |
| 469 | case SSL_CONNECTING: |
| 470 | SetError(EWOULDBLOCK); |
| 471 | return SOCKET_ERROR; |
| 472 | |
| 473 | case SSL_CONNECTED: |
| 474 | break; |
| 475 | |
| 476 | case SSL_ERROR: |
| 477 | default: |
| 478 | return SOCKET_ERROR; |
| 479 | } |
| 480 | |
| 481 | // OpenSSL will return an error if we try to write zero bytes |
| 482 | if (cb == 0) |
| 483 | return 0; |
| 484 | |
| 485 | ssl_write_needs_read_ = false; |
| 486 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 487 | int code = SSL_write(ssl_, pv, checked_cast<int>(cb)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 488 | switch (SSL_get_error(ssl_, code)) { |
| 489 | case SSL_ERROR_NONE: |
| 490 | //LOG(LS_INFO) << " -- success"; |
| 491 | return code; |
| 492 | case SSL_ERROR_WANT_READ: |
| 493 | //LOG(LS_INFO) << " -- error want read"; |
| 494 | ssl_write_needs_read_ = true; |
| 495 | SetError(EWOULDBLOCK); |
| 496 | break; |
| 497 | case SSL_ERROR_WANT_WRITE: |
| 498 | //LOG(LS_INFO) << " -- error want write"; |
| 499 | SetError(EWOULDBLOCK); |
| 500 | break; |
| 501 | case SSL_ERROR_ZERO_RETURN: |
| 502 | //LOG(LS_INFO) << " -- remote side closed"; |
| 503 | SetError(EWOULDBLOCK); |
| 504 | // do we need to signal closure? |
| 505 | break; |
| 506 | default: |
| 507 | //LOG(LS_INFO) << " -- error " << code; |
| 508 | Error("SSL_write", (code ? code : -1), false); |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | return SOCKET_ERROR; |
| 513 | } |
| 514 | |
| 515 | int |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 516 | OpenSSLAdapter::SendTo(const void* pv, size_t cb, const SocketAddress& addr) { |
| 517 | if (socket_->GetState() == Socket::CS_CONNECTED && |
| 518 | addr == socket_->GetRemoteAddress()) { |
| 519 | return Send(pv, cb); |
| 520 | } |
| 521 | |
| 522 | SetError(ENOTCONN); |
| 523 | |
| 524 | return SOCKET_ERROR; |
| 525 | } |
| 526 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 527 | int OpenSSLAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 528 | //LOG(LS_INFO) << "OpenSSLAdapter::Recv(" << cb << ")"; |
| 529 | switch (state_) { |
| 530 | |
| 531 | case SSL_NONE: |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 532 | return AsyncSocketAdapter::Recv(pv, cb, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 533 | |
| 534 | case SSL_WAIT: |
| 535 | case SSL_CONNECTING: |
| 536 | SetError(EWOULDBLOCK); |
| 537 | return SOCKET_ERROR; |
| 538 | |
| 539 | case SSL_CONNECTED: |
| 540 | break; |
| 541 | |
| 542 | case SSL_ERROR: |
| 543 | default: |
| 544 | return SOCKET_ERROR; |
| 545 | } |
| 546 | |
| 547 | // Don't trust OpenSSL with zero byte reads |
| 548 | if (cb == 0) |
| 549 | return 0; |
| 550 | |
| 551 | ssl_read_needs_write_ = false; |
| 552 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 553 | int code = SSL_read(ssl_, pv, checked_cast<int>(cb)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 554 | switch (SSL_get_error(ssl_, code)) { |
| 555 | case SSL_ERROR_NONE: |
| 556 | //LOG(LS_INFO) << " -- success"; |
| 557 | return code; |
| 558 | case SSL_ERROR_WANT_READ: |
| 559 | //LOG(LS_INFO) << " -- error want read"; |
| 560 | SetError(EWOULDBLOCK); |
| 561 | break; |
| 562 | case SSL_ERROR_WANT_WRITE: |
| 563 | //LOG(LS_INFO) << " -- error want write"; |
| 564 | ssl_read_needs_write_ = true; |
| 565 | SetError(EWOULDBLOCK); |
| 566 | break; |
| 567 | case SSL_ERROR_ZERO_RETURN: |
| 568 | //LOG(LS_INFO) << " -- remote side closed"; |
| 569 | SetError(EWOULDBLOCK); |
| 570 | // do we need to signal closure? |
| 571 | break; |
| 572 | default: |
| 573 | //LOG(LS_INFO) << " -- error " << code; |
| 574 | Error("SSL_read", (code ? code : -1), false); |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | return SOCKET_ERROR; |
| 579 | } |
| 580 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 581 | int OpenSSLAdapter::RecvFrom(void* pv, |
| 582 | size_t cb, |
| 583 | SocketAddress* paddr, |
| 584 | int64_t* timestamp) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 585 | if (socket_->GetState() == Socket::CS_CONNECTED) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 586 | int ret = Recv(pv, cb, timestamp); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 587 | |
| 588 | *paddr = GetRemoteAddress(); |
| 589 | |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | SetError(ENOTCONN); |
| 594 | |
| 595 | return SOCKET_ERROR; |
| 596 | } |
| 597 | |
| 598 | int |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 599 | OpenSSLAdapter::Close() { |
| 600 | Cleanup(); |
| 601 | state_ = restartable_ ? SSL_WAIT : SSL_NONE; |
| 602 | return AsyncSocketAdapter::Close(); |
| 603 | } |
| 604 | |
| 605 | Socket::ConnState |
| 606 | OpenSSLAdapter::GetState() const { |
| 607 | //if (signal_close_) |
| 608 | // return CS_CONNECTED; |
| 609 | ConnState state = socket_->GetState(); |
| 610 | if ((state == CS_CONNECTED) |
| 611 | && ((state_ == SSL_WAIT) || (state_ == SSL_CONNECTING))) |
| 612 | state = CS_CONNECTING; |
| 613 | return state; |
| 614 | } |
| 615 | |
| 616 | void |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 617 | OpenSSLAdapter::OnMessage(Message* msg) { |
| 618 | if (MSG_TIMEOUT == msg->message_id) { |
| 619 | LOG(LS_INFO) << "DTLS timeout expired"; |
| 620 | DTLSv1_handle_timeout(ssl_); |
| 621 | ContinueSSL(); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | void |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 626 | OpenSSLAdapter::OnConnectEvent(AsyncSocket* socket) { |
| 627 | LOG(LS_INFO) << "OpenSSLAdapter::OnConnectEvent"; |
| 628 | if (state_ != SSL_WAIT) { |
| 629 | ASSERT(state_ == SSL_NONE); |
| 630 | AsyncSocketAdapter::OnConnectEvent(socket); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | state_ = SSL_CONNECTING; |
| 635 | if (int err = BeginSSL()) { |
| 636 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void |
| 641 | OpenSSLAdapter::OnReadEvent(AsyncSocket* socket) { |
| 642 | //LOG(LS_INFO) << "OpenSSLAdapter::OnReadEvent"; |
| 643 | |
| 644 | if (state_ == SSL_NONE) { |
| 645 | AsyncSocketAdapter::OnReadEvent(socket); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | if (state_ == SSL_CONNECTING) { |
| 650 | if (int err = ContinueSSL()) { |
| 651 | Error("ContinueSSL", err); |
| 652 | } |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | if (state_ != SSL_CONNECTED) |
| 657 | return; |
| 658 | |
| 659 | // Don't let ourselves go away during the callbacks |
| 660 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 661 | if (ssl_write_needs_read_) { |
| 662 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 663 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 664 | } |
| 665 | |
| 666 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 667 | AsyncSocketAdapter::OnReadEvent(socket); |
| 668 | } |
| 669 | |
| 670 | void |
| 671 | OpenSSLAdapter::OnWriteEvent(AsyncSocket* socket) { |
| 672 | //LOG(LS_INFO) << "OpenSSLAdapter::OnWriteEvent"; |
| 673 | |
| 674 | if (state_ == SSL_NONE) { |
| 675 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | if (state_ == SSL_CONNECTING) { |
| 680 | if (int err = ContinueSSL()) { |
| 681 | Error("ContinueSSL", err); |
| 682 | } |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | if (state_ != SSL_CONNECTED) |
| 687 | return; |
| 688 | |
| 689 | // Don't let ourselves go away during the callbacks |
| 690 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 691 | |
| 692 | if (ssl_read_needs_write_) { |
| 693 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 694 | AsyncSocketAdapter::OnReadEvent(socket); |
| 695 | } |
| 696 | |
| 697 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 698 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 699 | } |
| 700 | |
| 701 | void |
| 702 | OpenSSLAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
| 703 | LOG(LS_INFO) << "OpenSSLAdapter::OnCloseEvent(" << err << ")"; |
| 704 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 705 | } |
| 706 | |
| 707 | // This code is taken from the "Network Security with OpenSSL" |
| 708 | // sample in chapter 5 |
| 709 | |
| 710 | bool OpenSSLAdapter::VerifyServerName(SSL* ssl, const char* host, |
| 711 | bool ignore_bad_cert) { |
| 712 | if (!host) |
| 713 | return false; |
| 714 | |
| 715 | // Checking the return from SSL_get_peer_certificate here is not strictly |
| 716 | // necessary. With our setup, it is not possible for it to return |
| 717 | // NULL. However, it is good form to check the return. |
| 718 | X509* certificate = SSL_get_peer_certificate(ssl); |
| 719 | if (!certificate) |
| 720 | return false; |
| 721 | |
| 722 | // Logging certificates is extremely verbose. So it is disabled by default. |
| 723 | #ifdef LOG_CERTIFICATES |
| 724 | { |
| 725 | LOG(LS_INFO) << "Certificate from server:"; |
| 726 | BIO* mem = BIO_new(BIO_s_mem()); |
| 727 | X509_print_ex(mem, certificate, XN_FLAG_SEP_CPLUS_SPC, X509_FLAG_NO_HEADER); |
| 728 | BIO_write(mem, "\0", 1); |
| 729 | char* buffer; |
| 730 | BIO_get_mem_data(mem, &buffer); |
| 731 | LOG(LS_INFO) << buffer; |
| 732 | BIO_free(mem); |
| 733 | |
| 734 | char* cipher_description = |
| 735 | SSL_CIPHER_description(SSL_get_current_cipher(ssl), NULL, 128); |
| 736 | LOG(LS_INFO) << "Cipher: " << cipher_description; |
| 737 | OPENSSL_free(cipher_description); |
| 738 | } |
| 739 | #endif |
| 740 | |
| 741 | bool ok = false; |
| 742 | int extension_count = X509_get_ext_count(certificate); |
| 743 | for (int i = 0; i < extension_count; ++i) { |
| 744 | X509_EXTENSION* extension = X509_get_ext(certificate, i); |
| 745 | int extension_nid = OBJ_obj2nid(X509_EXTENSION_get_object(extension)); |
| 746 | |
| 747 | if (extension_nid == NID_subject_alt_name) { |
| 748 | const X509V3_EXT_METHOD* meth = X509V3_EXT_get(extension); |
| 749 | if (!meth) |
| 750 | break; |
| 751 | |
| 752 | void* ext_str = NULL; |
| 753 | |
| 754 | // We assign this to a local variable, instead of passing the address |
| 755 | // directly to ASN1_item_d2i. |
| 756 | // See http://readlist.com/lists/openssl.org/openssl-users/0/4761.html. |
| 757 | unsigned char* ext_value_data = extension->value->data; |
| 758 | |
| 759 | const unsigned char **ext_value_data_ptr = |
| 760 | (const_cast<const unsigned char **>(&ext_value_data)); |
| 761 | |
| 762 | if (meth->it) { |
| 763 | ext_str = ASN1_item_d2i(NULL, ext_value_data_ptr, |
| 764 | extension->value->length, |
| 765 | ASN1_ITEM_ptr(meth->it)); |
| 766 | } else { |
| 767 | ext_str = meth->d2i(NULL, ext_value_data_ptr, extension->value->length); |
| 768 | } |
| 769 | |
| 770 | STACK_OF(CONF_VALUE)* value = meth->i2v(meth, ext_str, NULL); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 771 | |
| 772 | // Cast to size_t to be compilable for both OpenSSL and BoringSSL. |
| 773 | for (size_t j = 0; j < static_cast<size_t>(sk_CONF_VALUE_num(value)); |
| 774 | ++j) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 775 | CONF_VALUE* nval = sk_CONF_VALUE_value(value, j); |
| 776 | // The value for nval can contain wildcards |
| 777 | if (!strcmp(nval->name, "DNS") && string_match(host, nval->value)) { |
| 778 | ok = true; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | sk_CONF_VALUE_pop_free(value, X509V3_conf_free); |
| 783 | value = NULL; |
| 784 | |
| 785 | if (meth->it) { |
| 786 | ASN1_item_free(reinterpret_cast<ASN1_VALUE*>(ext_str), |
| 787 | ASN1_ITEM_ptr(meth->it)); |
| 788 | } else { |
| 789 | meth->ext_free(ext_str); |
| 790 | } |
| 791 | ext_str = NULL; |
| 792 | } |
| 793 | if (ok) |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | char data[256]; |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 798 | X509_NAME* subject; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 799 | if (!ok |
| 800 | && ((subject = X509_get_subject_name(certificate)) != NULL) |
| 801 | && (X509_NAME_get_text_by_NID(subject, NID_commonName, |
| 802 | data, sizeof(data)) > 0)) { |
| 803 | data[sizeof(data)-1] = 0; |
| 804 | if (_stricmp(data, host) == 0) |
| 805 | ok = true; |
| 806 | } |
| 807 | |
| 808 | X509_free(certificate); |
| 809 | |
| 810 | // This should only ever be turned on for debugging and development. |
| 811 | if (!ok && ignore_bad_cert) { |
| 812 | LOG(LS_WARNING) << "TLS certificate check FAILED. " |
| 813 | << "Allowing connection anyway."; |
| 814 | ok = true; |
| 815 | } |
| 816 | |
| 817 | return ok; |
| 818 | } |
| 819 | |
| 820 | bool OpenSSLAdapter::SSLPostConnectionCheck(SSL* ssl, const char* host) { |
| 821 | bool ok = VerifyServerName(ssl, host, ignore_bad_cert()); |
| 822 | |
| 823 | if (ok) { |
| 824 | ok = (SSL_get_verify_result(ssl) == X509_V_OK || |
| 825 | custom_verification_succeeded_); |
| 826 | } |
| 827 | |
| 828 | if (!ok && ignore_bad_cert()) { |
| 829 | LOG(LS_INFO) << "Other TLS post connection checks failed."; |
| 830 | ok = true; |
| 831 | } |
| 832 | |
| 833 | return ok; |
| 834 | } |
| 835 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 836 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 837 | |
| 838 | // We only use this for tracing and so it is only needed in debug mode |
| 839 | |
| 840 | void |
| 841 | OpenSSLAdapter::SSLInfoCallback(const SSL* s, int where, int ret) { |
| 842 | const char* str = "undefined"; |
| 843 | int w = where & ~SSL_ST_MASK; |
| 844 | if (w & SSL_ST_CONNECT) { |
| 845 | str = "SSL_connect"; |
| 846 | } else if (w & SSL_ST_ACCEPT) { |
| 847 | str = "SSL_accept"; |
| 848 | } |
| 849 | if (where & SSL_CB_LOOP) { |
| 850 | LOG(LS_INFO) << str << ":" << SSL_state_string_long(s); |
| 851 | } else if (where & SSL_CB_ALERT) { |
| 852 | str = (where & SSL_CB_READ) ? "read" : "write"; |
| 853 | LOG(LS_INFO) << "SSL3 alert " << str |
| 854 | << ":" << SSL_alert_type_string_long(ret) |
| 855 | << ":" << SSL_alert_desc_string_long(ret); |
| 856 | } else if (where & SSL_CB_EXIT) { |
| 857 | if (ret == 0) { |
| 858 | LOG(LS_INFO) << str << ":failed in " << SSL_state_string_long(s); |
| 859 | } else if (ret < 0) { |
| 860 | LOG(LS_INFO) << str << ":error in " << SSL_state_string_long(s); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 865 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 866 | |
| 867 | int |
| 868 | OpenSSLAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 869 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 870 | if (!ok) { |
| 871 | char data[256]; |
| 872 | X509* cert = X509_STORE_CTX_get_current_cert(store); |
| 873 | int depth = X509_STORE_CTX_get_error_depth(store); |
| 874 | int err = X509_STORE_CTX_get_error(store); |
| 875 | |
| 876 | LOG(LS_INFO) << "Error with certificate at depth: " << depth; |
| 877 | X509_NAME_oneline(X509_get_issuer_name(cert), data, sizeof(data)); |
| 878 | LOG(LS_INFO) << " issuer = " << data; |
| 879 | X509_NAME_oneline(X509_get_subject_name(cert), data, sizeof(data)); |
| 880 | LOG(LS_INFO) << " subject = " << data; |
| 881 | LOG(LS_INFO) << " err = " << err |
| 882 | << ":" << X509_verify_cert_error_string(err); |
| 883 | } |
| 884 | #endif |
| 885 | |
| 886 | // Get our stream pointer from the store |
| 887 | SSL* ssl = reinterpret_cast<SSL*>( |
| 888 | X509_STORE_CTX_get_ex_data(store, |
| 889 | SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 890 | |
| 891 | OpenSSLAdapter* stream = |
| 892 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
| 893 | |
| 894 | if (!ok && custom_verify_callback_) { |
| 895 | void* cert = |
| 896 | reinterpret_cast<void*>(X509_STORE_CTX_get_current_cert(store)); |
| 897 | if (custom_verify_callback_(cert)) { |
| 898 | stream->custom_verification_succeeded_ = true; |
| 899 | LOG(LS_INFO) << "validated certificate using custom callback"; |
| 900 | ok = true; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Should only be used for debugging and development. |
| 905 | if (!ok && stream->ignore_bad_cert()) { |
| 906 | LOG(LS_WARNING) << "Ignoring cert error while verifying cert chain"; |
| 907 | ok = 1; |
| 908 | } |
| 909 | |
| 910 | return ok; |
| 911 | } |
| 912 | |
| 913 | bool OpenSSLAdapter::ConfigureTrustedRootCertificates(SSL_CTX* ctx) { |
| 914 | // Add the root cert that we care about to the SSL context |
| 915 | int count_of_added_certs = 0; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 916 | for (size_t i = 0; i < arraysize(kSSLCertCertificateList); i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 917 | const unsigned char* cert_buffer = kSSLCertCertificateList[i]; |
| 918 | size_t cert_buffer_len = kSSLCertCertificateSizeList[i]; |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 919 | X509* cert = d2i_X509(NULL, &cert_buffer, |
| 920 | checked_cast<long>(cert_buffer_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 921 | if (cert) { |
| 922 | int return_value = X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), cert); |
| 923 | if (return_value == 0) { |
| 924 | LOG(LS_WARNING) << "Unable to add certificate."; |
| 925 | } else { |
| 926 | count_of_added_certs++; |
| 927 | } |
| 928 | X509_free(cert); |
| 929 | } |
| 930 | } |
| 931 | return count_of_added_certs > 0; |
| 932 | } |
| 933 | |
| 934 | SSL_CTX* |
| 935 | OpenSSLAdapter::SetupSSLContext() { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 936 | SSL_CTX* ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? |
| 937 | DTLSv1_client_method() : TLSv1_client_method()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 938 | if (ctx == NULL) { |
| 939 | unsigned long error = ERR_get_error(); // NOLINT: type used by OpenSSL. |
| 940 | LOG(LS_WARNING) << "SSL_CTX creation failed: " |
| 941 | << '"' << ERR_reason_error_string(error) << "\" " |
| 942 | << "(error=" << error << ')'; |
| 943 | return NULL; |
| 944 | } |
| 945 | if (!ConfigureTrustedRootCertificates(ctx)) { |
| 946 | SSL_CTX_free(ctx); |
| 947 | return NULL; |
| 948 | } |
| 949 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 950 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 951 | SSL_CTX_set_info_callback(ctx, SSLInfoCallback); |
| 952 | #endif |
| 953 | |
| 954 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, SSLVerifyCallback); |
| 955 | SSL_CTX_set_verify_depth(ctx, 4); |
| 956 | SSL_CTX_set_cipher_list(ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
| 957 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 958 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 959 | SSL_CTX_set_read_ahead(ctx, 1); |
| 960 | } |
| 961 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 962 | return ctx; |
| 963 | } |
| 964 | |
| 965 | } // namespace rtc |
| 966 | |
| 967 | #endif // HAVE_OPENSSL_SSL_H |