1 /* 2 * Copyright 2013-2016 Haiku, Inc. 3 * Copyright 2011-2015, Axel Dörfler, axeld@pinc-software.de. 4 * Copyright 2016, Rene Gollent, rene@gollent.com. 5 * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de> 6 * Distributed under the terms of the MIT License. 7 */ 8 9 10 #include <SecureSocket.h> 11 12 #ifdef OPENSSL_ENABLED 13 # include <openssl/ssl.h> 14 # include <openssl/ssl3.h> // for TRACE_SESSION_KEY only 15 # include <openssl/err.h> 16 #endif 17 18 #include <pthread.h> 19 20 #include <Certificate.h> 21 #include <FindDirectory.h> 22 #include <Path.h> 23 24 #include <AutoDeleter.h> 25 26 #include "CertificatePrivate.h" 27 28 29 //#define TRACE_SOCKET 30 #ifdef TRACE_SOCKET 31 # define TRACE(x...) printf(x) 32 #else 33 # define TRACE(x...) ; 34 #endif 35 36 //#define TRACE_SESSION_KEY 37 38 39 #ifdef OPENSSL_ENABLED 40 41 #ifdef TRACE_SESSION_KEY 42 #if OPENSSL_VERSION_NUMBER < 0x10100000L 43 /* 44 * print session id and master key in NSS keylog format (RSA 45 * Session-ID:<session id> Master-Key:<master key>) 46 */ 47 int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x) 48 { 49 size_t i; 50 51 if (x == NULL) 52 goto err; 53 if (x->session_id_length == 0 || x->master_key_length == 0) 54 goto err; 55 56 // the RSA prefix is required by the format's definition although there's 57 // nothing RSA-specific in the output, therefore, we don't have to check if 58 // the cipher suite is based on RSA 59 if (BIO_puts(bp, "RSA ") <= 0) 60 goto err; 61 62 if (BIO_puts(bp, "Session-ID:") <= 0) 63 goto err; 64 for (i = 0; i < x->session_id_length; i++) { 65 if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0) 66 goto err; 67 } 68 if (BIO_puts(bp, " Master-Key:") <= 0) 69 goto err; 70 for (i = 0; i < (size_t)x->master_key_length; i++) { 71 if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0) 72 goto err; 73 } 74 if (BIO_puts(bp, "\n") <= 0) 75 goto err; 76 77 return (1); 78 err: 79 return (0); 80 } 81 82 83 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */ 84 85 86 // print client random id and master key in NSS keylog format 87 // as session ID is not enough. 88 int SSL_SESSION_print_client_random(BIO *bp, const SSL *ssl) 89 { 90 const SSL_SESSION *x = SSL_get_session(ssl); 91 size_t i; 92 93 if (x == NULL) 94 goto err; 95 if (x->session_id_length == 0 || x->master_key_length == 0) 96 goto err; 97 98 if (BIO_puts(bp, "CLIENT_RANDOM ") <= 0) 99 goto err; 100 101 for (i = 0; i < sizeof(ssl->s3->client_random); i++) { 102 if (BIO_printf(bp, "%02X", ssl->s3->client_random[i]) <= 0) 103 goto err; 104 } 105 if (BIO_puts(bp, " ") <= 0) 106 goto err; 107 for (i = 0; i < (size_t)x->master_key_length; i++) { 108 if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0) 109 goto err; 110 } 111 if (BIO_puts(bp, "\n") <= 0) 112 goto err; 113 114 return (1); 115 err: 116 return (0); 117 } 118 119 120 #endif /* TRACE_SESSION_KEY */ 121 122 class BSecureSocket::Private { 123 public: 124 Private(); 125 ~Private(); 126 127 status_t InitCheck(); 128 status_t ErrorCode(int returnValue); 129 130 static SSL_CTX* Context(); 131 static int VerifyCallback(int ok, X509_STORE_CTX* ctx); 132 133 private: 134 static void _CreateContext(); 135 136 public: 137 SSL* fSSL; 138 BIO* fBIO; 139 static int sDataIndex; 140 141 private: 142 static SSL_CTX* sContext; 143 // FIXME When do we SSL_CTX_free it? 144 static pthread_once_t sInitOnce; 145 #ifdef TRACE_SESSION_KEY 146 public: 147 static BIO* sKeyLogBIO; 148 #endif 149 150 }; 151 152 153 /* static */ SSL_CTX* BSecureSocket::Private::sContext = NULL; 154 /* static */ int BSecureSocket::Private::sDataIndex; 155 /* static */ pthread_once_t BSecureSocket::Private::sInitOnce 156 = PTHREAD_ONCE_INIT; 157 #ifdef TRACE_SESSION_KEY 158 /* static */ BIO* BSecureSocket::Private::sKeyLogBIO = NULL; 159 #endif 160 161 162 BSecureSocket::Private::Private() 163 : 164 fSSL(NULL), 165 fBIO(BIO_new(BIO_s_socket())) 166 { 167 } 168 169 170 BSecureSocket::Private::~Private() 171 { 172 // SSL_free also frees the underlying BIO. 173 if (fSSL != NULL) 174 SSL_free(fSSL); 175 else { 176 // The SSL session was never created (Connect() was not called or 177 // failed). We must free the BIO we created in the constructor. 178 BIO_free(fBIO); 179 } 180 } 181 182 183 status_t 184 BSecureSocket::Private::InitCheck() 185 { 186 if (fBIO == NULL) 187 return B_NO_MEMORY; 188 return B_OK; 189 } 190 191 192 status_t 193 BSecureSocket::Private::ErrorCode(int returnValue) 194 { 195 int error = SSL_get_error(fSSL, returnValue); 196 switch (error) { 197 case SSL_ERROR_NONE: 198 // Shouldn't happen... 199 return B_NO_ERROR; 200 case SSL_ERROR_ZERO_RETURN: 201 // Socket is closed 202 return B_IO_ERROR; 203 case SSL_ERROR_SSL: 204 // Probably no certificate 205 return B_NOT_ALLOWED; 206 207 case SSL_ERROR_SYSCALL: 208 { 209 unsigned long error2; 210 // Check for extra errors in the error stack... 211 for (;;) { 212 error2 = ERR_get_error(); 213 if (error2 == 0) 214 break; 215 fprintf(stderr, "SSL ERR %s\n", ERR_error_string(error2, NULL)); 216 } 217 218 if (returnValue == 0) 219 { 220 // unexpected EOF, the remote host closed the socket without 221 // telling us why. 222 return ECONNREFUSED; 223 } 224 225 if (returnValue == -1) 226 { 227 fprintf(stderr, "SSL rv -1 %s\n", ERR_error_string(error, NULL)); 228 return errno; 229 } 230 231 fprintf(stderr, "SSL rv other %s\n", ERR_error_string(error, NULL)); 232 return B_ERROR; 233 } 234 235 case SSL_ERROR_WANT_READ: 236 case SSL_ERROR_WANT_WRITE: 237 case SSL_ERROR_WANT_CONNECT: 238 case SSL_ERROR_WANT_ACCEPT: 239 case SSL_ERROR_WANT_X509_LOOKUP: 240 default: 241 // TODO: translate SSL error codes! 242 fprintf(stderr, "SSL other %s\n", ERR_error_string(error, NULL)); 243 return B_ERROR; 244 } 245 } 246 247 248 /* static */ SSL_CTX* 249 BSecureSocket::Private::Context() 250 { 251 // We use lazy initialisation here, because reading certificates from disk 252 // and parsing them is a relatively long operation and uses some memory. 253 // We don't want programs that don't use SSL to waste resources with that. 254 pthread_once(&sInitOnce, _CreateContext); 255 256 return sContext; 257 } 258 259 260 /*! This is called each time a certificate verification occurs. It allows us to 261 catch failures and report them. 262 */ 263 /* static */ int 264 BSecureSocket::Private::VerifyCallback(int ok, X509_STORE_CTX* ctx) 265 { 266 // OpenSSL already checked the certificate again the certificate store for 267 // us, and tells the result of that in the ok parameter. 268 269 // If the verification succeeded, no need for any further checks. Let's 270 // proceed with the connection. 271 if (ok) 272 return ok; 273 274 // The certificate verification failed. Signal this to the BSecureSocket. 275 276 // First of all, get the affected BSecureSocket 277 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(ctx, 278 SSL_get_ex_data_X509_STORE_CTX_idx()); 279 BSecureSocket* socket = (BSecureSocket*)SSL_get_ex_data(ssl, sDataIndex); 280 281 // Get the certificate that we could not validate (this may not be the one 282 // we got from the server, but something higher up in the certificate 283 // chain) 284 X509* x509 = X509_STORE_CTX_get_current_cert(ctx); 285 BCertificate::Private* certificate 286 = new(std::nothrow) BCertificate::Private(x509); 287 288 if (certificate == NULL) 289 return 0; 290 291 int error = X509_STORE_CTX_get_error(ctx); 292 const char* message = X509_verify_cert_error_string(error); 293 294 // Let the BSecureSocket (or subclass) decide if we should continue anyway. 295 BCertificate failedCertificate(certificate); 296 return socket->CertificateVerificationFailed(failedCertificate, message); 297 } 298 299 300 #if TRACE_SSL 301 static void apps_ssl_info_callback(const SSL *s, int where, int ret) 302 { 303 const char *str; 304 int w; 305 306 w=where& ~SSL_ST_MASK; 307 308 if (w & SSL_ST_CONNECT) 309 str="SSL_connect"; 310 else if (w & SSL_ST_ACCEPT) 311 str="SSL_accept"; 312 else 313 str="undefined"; 314 315 if (where & SSL_CB_LOOP) { 316 fprintf(stderr, "%s:%s\n", str, SSL_state_string_long(s)); 317 } else if (where & SSL_CB_ALERT) { 318 str = (where & SSL_CB_READ) ? "read" : "write"; 319 fprintf(stderr, "SSL3 alert %s:%s:%s\n", 320 str, 321 SSL_alert_type_string_long(ret), 322 SSL_alert_desc_string_long(ret)); 323 } else if (where & SSL_CB_EXIT) { 324 if (ret == 0) 325 fprintf(stderr, "%s:failed in %s\n", 326 str, SSL_state_string_long(s)); 327 else if (ret < 0) { 328 fprintf(stderr, "%s:error in %s\n", 329 str, SSL_state_string_long(s)); 330 } 331 } 332 } 333 334 335 #endif 336 337 338 /* static */ void 339 BSecureSocket::Private::_CreateContext() 340 { 341 // We want SSL to report errors in human readable format. 342 SSL_load_error_strings(); 343 344 // "SSLv23" means "any SSL or TLS version". We disable SSL v2 and v3 below 345 // to keep only TLS 1.0 and above. 346 sContext = SSL_CTX_new(SSLv23_method()); 347 348 #if TRACE_SSL 349 // For debugging purposes: get all SSL messages to the standard error. 350 SSL_CTX_set_info_callback(sContext, apps_ssl_info_callback); 351 #endif 352 353 // Disable legacy protocols. They have known vulnerabilities. 354 SSL_CTX_set_options(sContext, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); 355 356 // Disable SSL/TLS compression to prevent the CRIME attack. 357 SSL_CTX_set_options(sContext, SSL_OP_NO_COMPRESSION); 358 359 // Don't bother us with ERROR_WANT_READ. 360 SSL_CTX_set_mode(sContext, SSL_MODE_AUTO_RETRY); 361 362 // Setup cipher suites. 363 // Only accept reasonably secure ones ("HIGH") and disable some known 364 // broken stuff (https://wiki.openssl.org/index.php/SSL/TLS_Client) 365 SSL_CTX_set_cipher_list(sContext, "HIGH:!aNULL:!PSK:!SRP:!MD5:!RC4"); 366 367 // Setup certificate verification 368 SSL_CTX_set_default_verify_file(sContext); 369 370 // OpenSSL 1.0.2 and later: use the alternate "trusted first" algorithm to 371 // validate certificate chains. This makes the validation stop as soon as a 372 // recognized certificate is found in the chain, instead of validating the 373 // whole chain, then seeing if the root certificate is known. 374 #ifdef X509_V_FLAG_TRUSTED_FIRST 375 X509_VERIFY_PARAM* verifyParam = X509_VERIFY_PARAM_new(); 376 X509_VERIFY_PARAM_set_flags(verifyParam, X509_V_FLAG_TRUSTED_FIRST); 377 SSL_CTX_set1_param(sContext, verifyParam); 378 379 // TODO we need to free this after freeing the SSL context (which we 380 // currently never do) 381 // X509_VERIFY_PARAM_free(verifyParam); 382 #endif 383 384 // Get an unique index number for storing application data in SSL 385 // structs. We will store a pointer to the BSecureSocket class there. 386 sDataIndex = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); 387 388 #ifdef TRACE_SESSION_KEY 389 FILE *keylog = NULL; 390 const char *logpath = getenv("SSLKEYLOGFILE"); 391 if (logpath) 392 keylog = fopen(logpath, "w+"); 393 if (keylog) { 394 fprintf(keylog, "# Key Log File generated by Haiku Network Kit\n"); 395 sKeyLogBIO = BIO_new_fp(keylog, BIO_NOCLOSE); 396 } 397 #endif 398 } 399 400 401 // # pragma mark - BSecureSocket 402 403 404 BSecureSocket::BSecureSocket() 405 : 406 fPrivate(new(std::nothrow) BSecureSocket::Private()) 407 { 408 fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY; 409 } 410 411 412 BSecureSocket::BSecureSocket(const BNetworkAddress& peer, bigtime_t timeout) 413 : 414 fPrivate(new(std::nothrow) BSecureSocket::Private()) 415 { 416 fInitStatus = fPrivate != NULL ? fPrivate->InitCheck() : B_NO_MEMORY; 417 Connect(peer, timeout); 418 } 419 420 421 BSecureSocket::BSecureSocket(const BSecureSocket& other) 422 : 423 BSocket(other) 424 { 425 fPrivate = new(std::nothrow) BSecureSocket::Private(*other.fPrivate); 426 // TODO: this won't work this way! - write working copy constructor for 427 // Private. 428 429 if (fPrivate != NULL) 430 SSL_set_ex_data(fPrivate->fSSL, Private::sDataIndex, this); 431 else 432 fInitStatus = B_NO_MEMORY; 433 434 } 435 436 437 BSecureSocket::~BSecureSocket() 438 { 439 delete fPrivate; 440 } 441 442 443 status_t 444 BSecureSocket::Accept(BAbstractSocket*& _socket) 445 { 446 int fd = -1; 447 BNetworkAddress peer; 448 status_t error = AcceptNext(fd, peer); 449 if (error != B_OK) 450 return error; 451 BSecureSocket* socket = new(std::nothrow) BSecureSocket(); 452 ObjectDeleter<BSecureSocket> socketDeleter(socket); 453 if (socket == NULL || socket->InitCheck() != B_OK) { 454 close(fd); 455 return B_NO_MEMORY; 456 } 457 458 socket->_SetTo(fd, fLocal, peer); 459 error = socket->_SetupAccept(); 460 if (error != B_OK) 461 return error; 462 463 _socket = socket; 464 socketDeleter.Detach(); 465 466 return B_OK; 467 } 468 469 470 status_t 471 BSecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout) 472 { 473 status_t status = InitCheck(); 474 if (status != B_OK) 475 return status; 476 477 status = BSocket::Connect(peer, timeout); 478 if (status != B_OK) 479 return status; 480 481 return _SetupConnect(peer.HostName().String()); 482 } 483 484 485 void 486 BSecureSocket::Disconnect() 487 { 488 if (IsConnected()) { 489 if (fPrivate->fSSL != NULL) 490 SSL_shutdown(fPrivate->fSSL); 491 492 BSocket::Disconnect(); 493 } 494 } 495 496 497 status_t 498 BSecureSocket::WaitForReadable(bigtime_t timeout) const 499 { 500 if (fInitStatus != B_OK) 501 return fInitStatus; 502 if (!IsConnected()) 503 return B_ERROR; 504 505 if (SSL_pending(fPrivate->fSSL) > 0) 506 return B_OK; 507 508 return BSocket::WaitForReadable(timeout); 509 } 510 511 512 status_t 513 BSecureSocket::InitCheck() 514 { 515 if (fPrivate == NULL) 516 return B_NO_MEMORY; 517 518 status_t state = fPrivate->InitCheck(); 519 return state; 520 } 521 522 523 bool 524 BSecureSocket::CertificateVerificationFailed(BCertificate&, const char*) 525 { 526 return false; 527 } 528 529 530 // #pragma mark - BDataIO implementation 531 532 533 ssize_t 534 BSecureSocket::Read(void* buffer, size_t size) 535 { 536 if (!IsConnected()) 537 return B_ERROR; 538 539 int bytesRead; 540 int retry; 541 do { 542 bytesRead = SSL_read(fPrivate->fSSL, buffer, size); 543 if (bytesRead > 0) 544 return bytesRead; 545 546 if (errno != EINTR) { 547 // Don't retry in cases of "no data available" for non-blocking 548 // sockets. 549 int error = SSL_get_error(fPrivate->fSSL, bytesRead); 550 if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) 551 return B_WOULD_BLOCK; 552 } 553 554 // See if the error was retryable. We may have been interrupted by 555 // a signal, in which case we will retry. But it is also possible that 556 // another error has occurred which is not retryable. openssl will 557 // decide for us here. 558 retry = BIO_should_retry(SSL_get_rbio(fPrivate->fSSL)); 559 } while (retry != 0); 560 561 return fPrivate->ErrorCode(bytesRead); 562 } 563 564 565 ssize_t 566 BSecureSocket::Write(const void* buffer, size_t size) 567 { 568 if (!IsConnected()) 569 return B_ERROR; 570 571 int bytesWritten; 572 int retry; 573 do { 574 bytesWritten = SSL_write(fPrivate->fSSL, buffer, size); 575 if (bytesWritten >= 0) 576 return bytesWritten; 577 578 if (errno != EINTR) { 579 // Don't retry in cases of "no buffer space available" for 580 // non-blocking sockets. 581 int error = SSL_get_error(fPrivate->fSSL, bytesWritten); 582 if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) 583 return B_WOULD_BLOCK; 584 } 585 586 // See if the error was retryable. We may have been interrupted by 587 // a signal, in which case we will retry. But it is also possible that 588 // another error has occurred which is not retryable. openssl will 589 // decide for us here. 590 retry = BIO_should_retry(SSL_get_wbio(fPrivate->fSSL)); 591 } while (retry != 0); 592 593 return fPrivate->ErrorCode(bytesWritten); 594 } 595 596 597 status_t 598 BSecureSocket::_SetupCommon(const char* host) 599 { 600 // Do this only after BSocket::Connect has checked wether we're already 601 // connected. We don't want to kill an existing SSL session, as that would 602 // likely crash the protocol loop for it. 603 if (fPrivate->fSSL != NULL) { 604 SSL_free(fPrivate->fSSL); 605 } 606 607 fPrivate->fSSL = SSL_new(BSecureSocket::Private::Context()); 608 if (fPrivate->fSSL == NULL) { 609 BSocket::Disconnect(); 610 return B_NO_MEMORY; 611 } 612 613 BIO_set_fd(fPrivate->fBIO, fSocket, BIO_NOCLOSE); 614 SSL_set_bio(fPrivate->fSSL, fPrivate->fBIO, fPrivate->fBIO); 615 SSL_set_ex_data(fPrivate->fSSL, Private::sDataIndex, this); 616 if (host != NULL && host[0] != '\0') { 617 SSL_set_tlsext_host_name(fPrivate->fSSL, host); 618 X509_VERIFY_PARAM_set1_host(SSL_get0_param(fPrivate->fSSL), host, 0); 619 } 620 621 return B_OK; 622 } 623 624 625 status_t 626 BSecureSocket::_SetupConnect(const char* host) 627 { 628 status_t error = _SetupCommon(host); 629 if (error != B_OK) 630 return error; 631 632 int returnValue = SSL_connect(fPrivate->fSSL); 633 if (returnValue <= 0) { 634 TRACE("SSLConnection can't connect\n"); 635 BSocket::Disconnect(); 636 return fPrivate->ErrorCode(returnValue); 637 } 638 639 #ifdef TRACE_SESSION_KEY 640 fprintf(stderr, "SSL SESSION INFO:\n"); 641 //SSL_SESSION_print_fp(stderr, SSL_get_session(fPrivate->fSSL)); 642 SSL_SESSION_print_keylog(fPrivate->sKeyLogBIO, SSL_get_session(fPrivate->fSSL)); 643 SSL_SESSION_print_client_random(fPrivate->sKeyLogBIO, fPrivate->fSSL); 644 fprintf(stderr, "\n"); 645 #endif 646 647 return B_OK; 648 } 649 650 651 status_t 652 BSecureSocket::_SetupAccept() 653 { 654 status_t error = _SetupCommon(); 655 if (error != B_OK) 656 return error; 657 658 int returnValue = SSL_accept(fPrivate->fSSL); 659 if (returnValue <= 0) { 660 TRACE("SSLConnection can't accept\n"); 661 BSocket::Disconnect(); 662 return fPrivate->ErrorCode(returnValue); 663 } 664 665 return B_OK; 666 } 667 668 669 #else // OPENSSL_ENABLED 670 671 672 // #pragma mark - No-SSL stubs 673 674 675 BSecureSocket::BSecureSocket() 676 { 677 } 678 679 680 BSecureSocket::BSecureSocket(const BNetworkAddress& peer, bigtime_t timeout) 681 { 682 fInitStatus = B_UNSUPPORTED; 683 } 684 685 686 BSecureSocket::BSecureSocket(const BSecureSocket& other) 687 : 688 BSocket(other) 689 { 690 } 691 692 693 BSecureSocket::~BSecureSocket() 694 { 695 } 696 697 698 bool 699 BSecureSocket::CertificateVerificationFailed(BCertificate& certificate, const char*) 700 { 701 (void)certificate; 702 return false; 703 } 704 705 706 status_t 707 BSecureSocket::Accept(BAbstractSocket*& _socket) 708 { 709 return B_UNSUPPORTED; 710 } 711 712 713 status_t 714 BSecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout) 715 { 716 return fInitStatus = B_UNSUPPORTED; 717 } 718 719 720 void 721 BSecureSocket::Disconnect() 722 { 723 } 724 725 726 status_t 727 BSecureSocket::WaitForReadable(bigtime_t timeout) const 728 { 729 return B_UNSUPPORTED; 730 } 731 732 733 // #pragma mark - BDataIO implementation 734 735 736 ssize_t 737 BSecureSocket::Read(void* buffer, size_t size) 738 { 739 return B_UNSUPPORTED; 740 } 741 742 743 ssize_t 744 BSecureSocket::Write(const void* buffer, size_t size) 745 { 746 return B_UNSUPPORTED; 747 } 748 749 750 status_t 751 BSecureSocket::InitCheck() 752 { 753 return B_UNSUPPORTED; 754 } 755 756 757 status_t 758 BSecureSocket::_SetupCommon(const char* host) 759 { 760 return B_UNSUPPORTED; 761 } 762 763 764 status_t 765 BSecureSocket::_SetupConnect(const char* host) 766 { 767 return B_UNSUPPORTED; 768 } 769 770 771 status_t 772 BSecureSocket::_SetupAccept() 773 { 774 return B_UNSUPPORTED; 775 } 776 777 778 #endif // !OPENSSL_ENABLED 779