Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #if HAVE_OPENSSL_SSL_H | 11 #if HAVE_OPENSSL_SSL_H |
| 12 | 12 |
| 13 #include "webrtc/base/opensslstreamadapter.h" | 13 #include "webrtc/base/opensslstreamadapter.h" |
| 14 | 14 |
| 15 #include <openssl/bio.h> | 15 #include <openssl/bio.h> |
| 16 #include <openssl/crypto.h> | 16 #include <openssl/crypto.h> |
| 17 #include <openssl/err.h> | 17 #include <openssl/err.h> |
| 18 #include <openssl/rand.h> | 18 #include <openssl/rand.h> |
| 19 #include <openssl/tls1.h> | 19 #include <openssl/tls1.h> |
| 20 #include <openssl/x509v3.h> | 20 #include <openssl/x509v3.h> |
| 21 #ifndef OPENSSL_IS_BORINGSSL | 21 #ifndef OPENSSL_IS_BORINGSSL |
| 22 #include <openssl/dtls1.h> | 22 #include <openssl/dtls1.h> |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 #include <memory> | 25 #include <memory> |
| 26 #include <vector> | 26 #include <vector> |
| 27 | 27 |
| 28 #include "webrtc/base/checks.h" | |
| 28 #include "webrtc/base/common.h" | 29 #include "webrtc/base/common.h" |
| 29 #include "webrtc/base/logging.h" | 30 #include "webrtc/base/logging.h" |
| 30 #include "webrtc/base/safe_conversions.h" | 31 #include "webrtc/base/safe_conversions.h" |
| 31 #include "webrtc/base/stream.h" | 32 #include "webrtc/base/stream.h" |
| 32 #include "webrtc/base/openssl.h" | 33 #include "webrtc/base/openssl.h" |
| 33 #include "webrtc/base/openssladapter.h" | 34 #include "webrtc/base/openssladapter.h" |
| 34 #include "webrtc/base/openssldigest.h" | 35 #include "webrtc/base/openssldigest.h" |
| 35 #include "webrtc/base/opensslidentity.h" | 36 #include "webrtc/base/opensslidentity.h" |
| 36 #include "webrtc/base/stringutils.h" | 37 #include "webrtc/base/stringutils.h" |
| 37 #include "webrtc/base/timeutils.h" | 38 #include "webrtc/base/timeutils.h" |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 ssl_ctx_(NULL), | 287 ssl_ctx_(NULL), |
| 287 custom_verification_succeeded_(false), | 288 custom_verification_succeeded_(false), |
| 288 ssl_mode_(SSL_MODE_TLS), | 289 ssl_mode_(SSL_MODE_TLS), |
| 289 ssl_max_version_(SSL_PROTOCOL_TLS_12) {} | 290 ssl_max_version_(SSL_PROTOCOL_TLS_12) {} |
| 290 | 291 |
| 291 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { | 292 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
| 292 Cleanup(); | 293 Cleanup(); |
| 293 } | 294 } |
| 294 | 295 |
| 295 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { | 296 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { |
| 296 ASSERT(!identity_); | 297 RTC_DCHECK(!identity_); |
| 297 identity_.reset(static_cast<OpenSSLIdentity*>(identity)); | 298 identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 298 } | 299 } |
| 299 | 300 |
| 300 void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { | 301 void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { |
| 301 role_ = role; | 302 role_ = role; |
| 302 } | 303 } |
| 303 | 304 |
| 304 std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate() | 305 std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate() |
| 305 const { | 306 const { |
| 306 return peer_certificate_ ? std::unique_ptr<SSLCertificate>( | 307 return peer_certificate_ ? std::unique_ptr<SSLCertificate>( |
| 307 peer_certificate_->GetReference()) | 308 peer_certificate_->GetReference()) |
| 308 : nullptr; | 309 : nullptr; |
| 309 } | 310 } |
| 310 | 311 |
| 311 bool OpenSSLStreamAdapter::SetPeerCertificateDigest(const std::string | 312 bool OpenSSLStreamAdapter::SetPeerCertificateDigest(const std::string |
| 312 &digest_alg, | 313 &digest_alg, |
| 313 const unsigned char* | 314 const unsigned char* |
| 314 digest_val, | 315 digest_val, |
| 315 size_t digest_len) { | 316 size_t digest_len) { |
| 316 ASSERT(!peer_certificate_); | 317 RTC_DCHECK(!peer_certificate_verified_); |
| 317 ASSERT(peer_certificate_digest_algorithm_.size() == 0); | 318 RTC_DCHECK(!have_peer_certificate_digest()); |
| 318 ASSERT(ssl_server_name_.empty()); | 319 RTC_DCHECK(!verify_certificate_using_server_name()); |
| 319 size_t expected_len; | 320 size_t expected_len; |
| 320 | 321 |
| 321 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { | 322 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { |
| 322 LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; | 323 LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; |
| 323 return false; | 324 return false; |
| 324 } | 325 } |
| 325 if (expected_len != digest_len) | 326 if (expected_len != digest_len) { |
| 326 return false; | 327 return false; |
| 328 } | |
| 327 | 329 |
| 328 peer_certificate_digest_value_.SetData(digest_val, digest_len); | 330 peer_certificate_digest_value_.SetData(digest_val, digest_len); |
| 329 peer_certificate_digest_algorithm_ = digest_alg; | 331 peer_certificate_digest_algorithm_ = digest_alg; |
| 330 | 332 |
| 333 if (!peer_certificate_) { | |
| 334 // Normal case, where the digest is set before we obtain the certificate | |
| 335 // from the handshake. | |
| 336 return true; | |
| 337 } | |
| 338 | |
| 339 if (!VerifyPeerCertificate()) { | |
| 340 Error("SetPeerCertificateDigest", -1, false); | |
| 341 return false; | |
|
pthatcher1
2016/07/27 19:32:35
This would cause SetRemoteDescription to fail base
Taylor Brandstetter
2016/08/13 00:09:53
I think you meant for this comment to be in DtlsTr
| |
| 342 } | |
| 343 | |
| 344 if (state_ == SSL_CONNECTED) { | |
| 345 // Post event to unwind stack. Caller may not be expecting this event | |
| 346 // to occur synchronously. | |
| 347 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); | |
|
pthatcher1
2016/07/27 19:32:35
It's not very clear what's going on here. It appe
Taylor Brandstetter
2016/08/13 00:09:53
ContinueSSL should have been posting the event asy
| |
| 348 } | |
| 349 | |
| 331 return true; | 350 return true; |
| 332 } | 351 } |
| 333 | 352 |
| 334 std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { | 353 std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
| 335 #ifdef OPENSSL_IS_BORINGSSL | 354 #ifdef OPENSSL_IS_BORINGSSL |
| 336 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite); | 355 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite); |
| 337 if (!ssl_cipher) { | 356 if (!ssl_cipher) { |
| 338 return std::string(); | 357 return std::string(); |
| 339 } | 358 } |
| 340 char* cipher_name = SSL_CIPHER_get_rfc_name(ssl_cipher); | 359 char* cipher_name = SSL_CIPHER_get_rfc_name(ssl_cipher); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 | 462 |
| 444 srtp_ciphers_ = internal_ciphers; | 463 srtp_ciphers_ = internal_ciphers; |
| 445 return true; | 464 return true; |
| 446 #else | 465 #else |
| 447 return false; | 466 return false; |
| 448 #endif | 467 #endif |
| 449 } | 468 } |
| 450 | 469 |
| 451 bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { | 470 bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
| 452 #ifdef HAVE_DTLS_SRTP | 471 #ifdef HAVE_DTLS_SRTP |
| 453 ASSERT(state_ == SSL_CONNECTED); | 472 RTC_DCHECK(state_ == SSL_CONNECTED); |
| 454 if (state_ != SSL_CONNECTED) | 473 if (state_ != SSL_CONNECTED) |
| 455 return false; | 474 return false; |
| 456 | 475 |
| 457 const SRTP_PROTECTION_PROFILE *srtp_profile = | 476 const SRTP_PROTECTION_PROFILE *srtp_profile = |
| 458 SSL_get_selected_srtp_profile(ssl_); | 477 SSL_get_selected_srtp_profile(ssl_); |
| 459 | 478 |
| 460 if (!srtp_profile) | 479 if (!srtp_profile) |
| 461 return false; | 480 return false; |
| 462 | 481 |
| 463 *crypto_suite = srtp_profile->id; | 482 *crypto_suite = srtp_profile->id; |
| 464 ASSERT(!SrtpCryptoSuiteToName(*crypto_suite).empty()); | 483 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty()); |
| 465 return true; | 484 return true; |
| 466 #else | 485 #else |
| 467 return false; | 486 return false; |
| 468 #endif | 487 #endif |
| 469 } | 488 } |
| 470 | 489 |
| 471 int OpenSSLStreamAdapter::StartSSLWithServer(const char* server_name) { | 490 int OpenSSLStreamAdapter::StartSSLWithServer(const char* server_name) { |
| 472 ASSERT(server_name != NULL && server_name[0] != '\0'); | 491 if (state_ != SSL_NONE) { |
| 492 // Don't allow StartSSL to be called twice. | |
| 493 return -1; | |
| 494 } | |
| 495 RTC_DCHECK(server_name != NULL && server_name[0] != '\0'); | |
| 473 ssl_server_name_ = server_name; | 496 ssl_server_name_ = server_name; |
| 474 return StartSSL(); | 497 return StartSSL(); |
| 475 } | 498 } |
| 476 | 499 |
| 477 int OpenSSLStreamAdapter::StartSSLWithPeer() { | 500 int OpenSSLStreamAdapter::StartSSLWithPeer() { |
| 478 ASSERT(ssl_server_name_.empty()); | 501 if (state_ != SSL_NONE) { |
| 502 // Don't allow StartSSL to be called twice. | |
| 503 return -1; | |
| 504 } | |
| 505 RTC_DCHECK(ssl_server_name_.empty()); | |
| 479 // It is permitted to specify peer_certificate_ only later. | 506 // It is permitted to specify peer_certificate_ only later. |
| 480 return StartSSL(); | 507 return StartSSL(); |
| 481 } | 508 } |
| 482 | 509 |
| 483 void OpenSSLStreamAdapter::SetMode(SSLMode mode) { | 510 void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
| 484 ASSERT(state_ == SSL_NONE); | 511 RTC_DCHECK(state_ == SSL_NONE); |
| 485 ssl_mode_ = mode; | 512 ssl_mode_ = mode; |
| 486 } | 513 } |
| 487 | 514 |
| 488 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { | 515 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
| 489 ASSERT(ssl_ctx_ == NULL); | 516 RTC_DCHECK(ssl_ctx_ == NULL); |
| 490 ssl_max_version_ = version; | 517 ssl_max_version_ = version; |
| 491 } | 518 } |
| 492 | 519 |
| 493 // | 520 // |
| 494 // StreamInterface Implementation | 521 // StreamInterface Implementation |
| 495 // | 522 // |
| 496 | 523 |
| 497 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, | 524 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, |
| 498 size_t* written, int* error) { | 525 size_t* written, int* error) { |
| 499 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; | 526 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
| 500 | 527 |
| 501 switch (state_) { | 528 switch (state_) { |
| 502 case SSL_NONE: | 529 case SSL_NONE: |
| 503 // pass-through in clear text | 530 // pass-through in clear text |
| 504 return StreamAdapterInterface::Write(data, data_len, written, error); | 531 return StreamAdapterInterface::Write(data, data_len, written, error); |
| 505 | 532 |
| 506 case SSL_WAIT: | 533 case SSL_WAIT: |
| 507 case SSL_CONNECTING: | 534 case SSL_CONNECTING: |
| 508 return SR_BLOCK; | 535 return SR_BLOCK; |
| 509 | 536 |
| 510 case SSL_CONNECTED: | 537 case SSL_CONNECTED: |
| 538 if (waiting_to_verify_client_cert()) { | |
| 539 return SR_BLOCK; | |
| 540 } | |
| 511 break; | 541 break; |
| 512 | 542 |
| 513 case SSL_ERROR: | 543 case SSL_ERROR: |
| 514 case SSL_CLOSED: | 544 case SSL_CLOSED: |
| 515 default: | 545 default: |
| 516 if (error) | 546 if (error) |
| 517 *error = ssl_error_code_; | 547 *error = ssl_error_code_; |
| 518 return SR_ERROR; | 548 return SR_ERROR; |
| 519 } | 549 } |
| 520 | 550 |
| 521 // OpenSSL will return an error if we try to write zero bytes | 551 // OpenSSL will return an error if we try to write zero bytes |
| 522 if (data_len == 0) { | 552 if (data_len == 0) { |
| 523 if (written) | 553 if (written) |
| 524 *written = 0; | 554 *written = 0; |
| 525 return SR_SUCCESS; | 555 return SR_SUCCESS; |
| 526 } | 556 } |
| 527 | 557 |
| 528 ssl_write_needs_read_ = false; | 558 ssl_write_needs_read_ = false; |
| 529 | 559 |
| 530 int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); | 560 int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
| 531 int ssl_error = SSL_get_error(ssl_, code); | 561 int ssl_error = SSL_get_error(ssl_, code); |
| 532 switch (ssl_error) { | 562 switch (ssl_error) { |
| 533 case SSL_ERROR_NONE: | 563 case SSL_ERROR_NONE: |
| 534 LOG(LS_VERBOSE) << " -- success"; | 564 LOG(LS_VERBOSE) << " -- success"; |
| 535 ASSERT(0 < code && static_cast<unsigned>(code) <= data_len); | 565 RTC_DCHECK(0 < code && static_cast<unsigned>(code) <= data_len); |
| 536 if (written) | 566 if (written) |
| 537 *written = code; | 567 *written = code; |
| 538 return SR_SUCCESS; | 568 return SR_SUCCESS; |
| 539 case SSL_ERROR_WANT_READ: | 569 case SSL_ERROR_WANT_READ: |
| 540 LOG(LS_VERBOSE) << " -- error want read"; | 570 LOG(LS_VERBOSE) << " -- error want read"; |
| 541 ssl_write_needs_read_ = true; | 571 ssl_write_needs_read_ = true; |
| 542 return SR_BLOCK; | 572 return SR_BLOCK; |
| 543 case SSL_ERROR_WANT_WRITE: | 573 case SSL_ERROR_WANT_WRITE: |
| 544 LOG(LS_VERBOSE) << " -- error want write"; | 574 LOG(LS_VERBOSE) << " -- error want write"; |
| 545 return SR_BLOCK; | 575 return SR_BLOCK; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 560 switch (state_) { | 590 switch (state_) { |
| 561 case SSL_NONE: | 591 case SSL_NONE: |
| 562 // pass-through in clear text | 592 // pass-through in clear text |
| 563 return StreamAdapterInterface::Read(data, data_len, read, error); | 593 return StreamAdapterInterface::Read(data, data_len, read, error); |
| 564 | 594 |
| 565 case SSL_WAIT: | 595 case SSL_WAIT: |
| 566 case SSL_CONNECTING: | 596 case SSL_CONNECTING: |
| 567 return SR_BLOCK; | 597 return SR_BLOCK; |
| 568 | 598 |
| 569 case SSL_CONNECTED: | 599 case SSL_CONNECTED: |
| 600 if (waiting_to_verify_client_cert()) { | |
| 601 return SR_BLOCK; | |
| 602 } | |
| 570 break; | 603 break; |
| 571 | 604 |
| 572 case SSL_CLOSED: | 605 case SSL_CLOSED: |
| 573 return SR_EOS; | 606 return SR_EOS; |
| 574 | 607 |
| 575 case SSL_ERROR: | 608 case SSL_ERROR: |
| 576 default: | 609 default: |
| 577 if (error) | 610 if (error) |
| 578 *error = ssl_error_code_; | 611 *error = ssl_error_code_; |
| 579 return SR_ERROR; | 612 return SR_ERROR; |
| 580 } | 613 } |
| 581 | 614 |
| 582 // Don't trust OpenSSL with zero byte reads | 615 // Don't trust OpenSSL with zero byte reads |
| 583 if (data_len == 0) { | 616 if (data_len == 0) { |
| 584 if (read) | 617 if (read) |
| 585 *read = 0; | 618 *read = 0; |
| 586 return SR_SUCCESS; | 619 return SR_SUCCESS; |
| 587 } | 620 } |
| 588 | 621 |
| 589 ssl_read_needs_write_ = false; | 622 ssl_read_needs_write_ = false; |
| 590 | 623 |
| 591 int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); | 624 int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
| 592 int ssl_error = SSL_get_error(ssl_, code); | 625 int ssl_error = SSL_get_error(ssl_, code); |
| 593 switch (ssl_error) { | 626 switch (ssl_error) { |
| 594 case SSL_ERROR_NONE: | 627 case SSL_ERROR_NONE: |
| 595 LOG(LS_VERBOSE) << " -- success"; | 628 LOG(LS_VERBOSE) << " -- success"; |
| 596 ASSERT(0 < code && static_cast<unsigned>(code) <= data_len); | 629 RTC_DCHECK(0 < code && static_cast<unsigned>(code) <= data_len); |
| 597 if (read) | 630 if (read) |
| 598 *read = code; | 631 *read = code; |
| 599 | 632 |
| 600 if (ssl_mode_ == SSL_MODE_DTLS) { | 633 if (ssl_mode_ == SSL_MODE_DTLS) { |
| 601 // Enforce atomic reads -- this is a short read | 634 // Enforce atomic reads -- this is a short read |
| 602 unsigned int pending = SSL_pending(ssl_); | 635 unsigned int pending = SSL_pending(ssl_); |
| 603 | 636 |
| 604 if (pending) { | 637 if (pending) { |
| 605 LOG(LS_INFO) << " -- short DTLS read. flushing"; | 638 LOG(LS_INFO) << " -- short DTLS read. flushing"; |
| 606 FlushInput(pending); | 639 FlushInput(pending); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 637 | 670 |
| 638 void OpenSSLStreamAdapter::FlushInput(unsigned int left) { | 671 void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 639 unsigned char buf[2048]; | 672 unsigned char buf[2048]; |
| 640 | 673 |
| 641 while (left) { | 674 while (left) { |
| 642 // This should always succeed | 675 // This should always succeed |
| 643 int toread = (sizeof(buf) < left) ? sizeof(buf) : left; | 676 int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 644 int code = SSL_read(ssl_, buf, toread); | 677 int code = SSL_read(ssl_, buf, toread); |
| 645 | 678 |
| 646 int ssl_error = SSL_get_error(ssl_, code); | 679 int ssl_error = SSL_get_error(ssl_, code); |
| 647 ASSERT(ssl_error == SSL_ERROR_NONE); | 680 RTC_DCHECK(ssl_error == SSL_ERROR_NONE); |
| 648 | 681 |
| 649 if (ssl_error != SSL_ERROR_NONE) { | 682 if (ssl_error != SSL_ERROR_NONE) { |
| 650 LOG(LS_VERBOSE) << " -- error " << code; | 683 LOG(LS_VERBOSE) << " -- error " << code; |
| 651 Error("SSL_read", (ssl_error ? ssl_error : -1), false); | 684 Error("SSL_read", (ssl_error ? ssl_error : -1), false); |
| 652 return; | 685 return; |
| 653 } | 686 } |
| 654 | 687 |
| 655 LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; | 688 LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
| 656 left -= code; | 689 left -= code; |
| 657 } | 690 } |
| 658 } | 691 } |
| 659 | 692 |
| 660 void OpenSSLStreamAdapter::Close() { | 693 void OpenSSLStreamAdapter::Close() { |
| 661 Cleanup(); | 694 Cleanup(); |
| 662 ASSERT(state_ == SSL_CLOSED || state_ == SSL_ERROR); | 695 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 663 StreamAdapterInterface::Close(); | 696 StreamAdapterInterface::Close(); |
| 664 } | 697 } |
| 665 | 698 |
| 666 StreamState OpenSSLStreamAdapter::GetState() const { | 699 StreamState OpenSSLStreamAdapter::GetState() const { |
| 667 switch (state_) { | 700 switch (state_) { |
| 668 case SSL_WAIT: | 701 case SSL_WAIT: |
| 669 case SSL_CONNECTING: | 702 case SSL_CONNECTING: |
| 670 return SS_OPENING; | 703 return SS_OPENING; |
| 671 case SSL_CONNECTED: | 704 case SSL_CONNECTED: |
| 705 if (waiting_to_verify_client_cert()) { | |
| 706 return SS_OPENING; | |
| 707 } | |
| 672 return SS_OPEN; | 708 return SS_OPEN; |
| 673 default: | 709 default: |
| 674 return SS_CLOSED; | 710 return SS_CLOSED; |
| 675 }; | 711 }; |
| 676 // not reached | 712 // not reached |
| 677 } | 713 } |
| 678 | 714 |
| 679 void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events, | 715 void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events, |
| 680 int err) { | 716 int err) { |
| 681 int events_to_signal = 0; | 717 int events_to_signal = 0; |
| 682 int signal_error = 0; | 718 int signal_error = 0; |
| 683 ASSERT(stream == this->stream()); | 719 RTC_DCHECK(stream == this->stream()); |
| 684 if ((events & SE_OPEN)) { | 720 if ((events & SE_OPEN)) { |
| 685 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; | 721 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
| 686 if (state_ != SSL_WAIT) { | 722 if (state_ != SSL_WAIT) { |
| 687 ASSERT(state_ == SSL_NONE); | 723 RTC_DCHECK(state_ == SSL_NONE); |
| 688 events_to_signal |= SE_OPEN; | 724 events_to_signal |= SE_OPEN; |
| 689 } else { | 725 } else { |
| 690 state_ = SSL_CONNECTING; | 726 state_ = SSL_CONNECTING; |
| 691 if (int err = BeginSSL()) { | 727 if (int err = BeginSSL()) { |
| 692 Error("BeginSSL", err, true); | 728 Error("BeginSSL", err, true); |
| 693 return; | 729 return; |
| 694 } | 730 } |
| 695 } | 731 } |
| 696 } | 732 } |
| 697 if ((events & (SE_READ|SE_WRITE))) { | 733 if ((events & (SE_READ|SE_WRITE))) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 716 LOG(LS_VERBOSE) << " -- onStreamReadable"; | 752 LOG(LS_VERBOSE) << " -- onStreamReadable"; |
| 717 events_to_signal |= SE_READ; | 753 events_to_signal |= SE_READ; |
| 718 } | 754 } |
| 719 } | 755 } |
| 720 } | 756 } |
| 721 if ((events & SE_CLOSE)) { | 757 if ((events & SE_CLOSE)) { |
| 722 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err << ")"; | 758 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err << ")"; |
| 723 Cleanup(); | 759 Cleanup(); |
| 724 events_to_signal |= SE_CLOSE; | 760 events_to_signal |= SE_CLOSE; |
| 725 // SE_CLOSE is the only event that uses the final parameter to OnEvent(). | 761 // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
| 726 ASSERT(signal_error == 0); | 762 RTC_DCHECK(signal_error == 0); |
| 727 signal_error = err; | 763 signal_error = err; |
| 728 } | 764 } |
| 729 if (events_to_signal) | 765 if (events_to_signal) |
| 730 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); | 766 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); |
| 731 } | 767 } |
| 732 | 768 |
| 733 int OpenSSLStreamAdapter::StartSSL() { | 769 int OpenSSLStreamAdapter::StartSSL() { |
| 734 ASSERT(state_ == SSL_NONE); | 770 RTC_DCHECK(state_ == SSL_NONE); |
| 735 | 771 |
| 736 if (StreamAdapterInterface::GetState() != SS_OPEN) { | 772 if (StreamAdapterInterface::GetState() != SS_OPEN) { |
| 737 state_ = SSL_WAIT; | 773 state_ = SSL_WAIT; |
| 738 return 0; | 774 return 0; |
| 739 } | 775 } |
| 740 | 776 |
| 741 state_ = SSL_CONNECTING; | 777 state_ = SSL_CONNECTING; |
| 742 if (int err = BeginSSL()) { | 778 if (int err = BeginSSL()) { |
| 743 Error("BeginSSL", err, false); | 779 Error("BeginSSL", err, false); |
| 744 return err; | 780 return err; |
| 745 } | 781 } |
| 746 | 782 |
| 747 return 0; | 783 return 0; |
| 748 } | 784 } |
| 749 | 785 |
| 750 int OpenSSLStreamAdapter::BeginSSL() { | 786 int OpenSSLStreamAdapter::BeginSSL() { |
| 751 ASSERT(state_ == SSL_CONNECTING); | 787 RTC_DCHECK(state_ == SSL_CONNECTING); |
| 752 // The underlying stream has open. If we are in peer-to-peer mode | 788 // At this point, we should have selected one (and only one) way of |
| 753 // then a peer certificate must have been specified by now. | 789 // validating the peer's certificate. |
| 754 ASSERT(!ssl_server_name_.empty() || | 790 RTC_DCHECK(verify_certificate_using_server_name() != |
| 755 !peer_certificate_digest_algorithm_.empty()); | 791 verify_certificate_using_peer_digest()); |
|
pthatcher1
2016/07/27 19:32:35
Doesn't this equate to the following?
RTC_DCHECK(
Taylor Brandstetter
2016/08/13 00:09:53
This checks that one and only one "mode" has been
| |
| 756 LOG(LS_INFO) << "BeginSSL: " | 792 LOG(LS_INFO) << "BeginSSL: " |
| 757 << (!ssl_server_name_.empty() ? ssl_server_name_ : | 793 << (verify_certificate_using_server_name() ? ssl_server_name_ |
|
pthatcher1
2016/07/27 19:32:35
We already verified the state_, so why not just us
Taylor Brandstetter
2016/08/13 00:09:53
I don't know what you mean. I'm using "verify_cert
| |
| 758 "with peer"); | 794 : "with peer"); |
| 759 | 795 |
| 760 BIO* bio = NULL; | 796 BIO* bio = NULL; |
| 761 | 797 |
| 762 // First set up the context | 798 // First set up the context |
| 763 ASSERT(ssl_ctx_ == NULL); | 799 RTC_DCHECK(ssl_ctx_ == NULL); |
| 764 ssl_ctx_ = SetupSSLContext(); | 800 ssl_ctx_ = SetupSSLContext(); |
| 765 if (!ssl_ctx_) | 801 if (!ssl_ctx_) |
| 766 return -1; | 802 return -1; |
| 767 | 803 |
| 768 bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); | 804 bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); |
| 769 if (!bio) | 805 if (!bio) |
| 770 return -1; | 806 return -1; |
| 771 | 807 |
| 772 ssl_ = SSL_new(ssl_ctx_); | 808 ssl_ = SSL_new(ssl_ctx_); |
| 773 if (!ssl_) { | 809 if (!ssl_) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 807 SSL_set_tmp_ecdh(ssl_, ecdh); | 843 SSL_set_tmp_ecdh(ssl_, ecdh); |
| 808 EC_KEY_free(ecdh); | 844 EC_KEY_free(ecdh); |
| 809 #endif | 845 #endif |
| 810 | 846 |
| 811 // Do the connect | 847 // Do the connect |
| 812 return ContinueSSL(); | 848 return ContinueSSL(); |
| 813 } | 849 } |
| 814 | 850 |
| 815 int OpenSSLStreamAdapter::ContinueSSL() { | 851 int OpenSSLStreamAdapter::ContinueSSL() { |
| 816 LOG(LS_VERBOSE) << "ContinueSSL"; | 852 LOG(LS_VERBOSE) << "ContinueSSL"; |
| 817 ASSERT(state_ == SSL_CONNECTING); | 853 RTC_DCHECK(state_ == SSL_CONNECTING); |
| 818 | 854 |
| 819 // Clear the DTLS timer | 855 // Clear the DTLS timer |
| 820 Thread::Current()->Clear(this, MSG_TIMEOUT); | 856 Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 821 | 857 |
| 822 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); | 858 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 823 int ssl_error; | 859 int ssl_error; |
| 824 switch (ssl_error = SSL_get_error(ssl_, code)) { | 860 switch (ssl_error = SSL_get_error(ssl_, code)) { |
| 825 case SSL_ERROR_NONE: | 861 case SSL_ERROR_NONE: |
| 826 LOG(LS_VERBOSE) << " -- success"; | 862 LOG(LS_VERBOSE) << " -- success"; |
| 827 | 863 |
| 828 if (!SSLPostConnectionCheck(ssl_, ssl_server_name_.c_str(), NULL, | 864 if (!SSLPostConnectionCheck()) { |
| 829 peer_certificate_digest_algorithm_)) { | |
| 830 LOG(LS_ERROR) << "TLS post connection check failed"; | 865 LOG(LS_ERROR) << "TLS post connection check failed"; |
| 831 return -1; | 866 return -1; |
| 832 } | 867 } |
| 833 | 868 |
| 834 state_ = SSL_CONNECTED; | 869 state_ = SSL_CONNECTED; |
| 835 StreamAdapterInterface::OnEvent(stream(), SE_OPEN|SE_READ|SE_WRITE, 0); | 870 if (!waiting_to_verify_client_cert()) { |
| 871 // We have everything we need to start the connection, so signal | |
| 872 // SE_OPEN. If we need a client certificate fingerprint and don't have | |
| 873 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest. | |
| 874 StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE, | |
| 875 0); | |
| 876 } | |
| 836 break; | 877 break; |
| 837 | 878 |
| 838 case SSL_ERROR_WANT_READ: { | 879 case SSL_ERROR_WANT_READ: { |
| 839 LOG(LS_VERBOSE) << " -- error want read"; | 880 LOG(LS_VERBOSE) << " -- error want read"; |
| 840 struct timeval timeout; | 881 struct timeval timeout; |
| 841 if (DTLSv1_get_timeout(ssl_, &timeout)) { | 882 if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 842 int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; | 883 int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; |
| 843 | 884 |
| 844 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, | 885 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, |
| 845 MSG_TIMEOUT, 0); | 886 MSG_TIMEOUT, 0); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1035 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { | 1076 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1036 SSL_CTX_free(ctx); | 1077 SSL_CTX_free(ctx); |
| 1037 return NULL; | 1078 return NULL; |
| 1038 } | 1079 } |
| 1039 } | 1080 } |
| 1040 #endif | 1081 #endif |
| 1041 | 1082 |
| 1042 return ctx; | 1083 return ctx; |
| 1043 } | 1084 } |
| 1044 | 1085 |
| 1045 int OpenSSLStreamAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { | 1086 bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
| 1046 // Get our SSL structure from the store | 1087 if (!have_peer_certificate_digest() || !peer_certificate_) { |
| 1047 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( | 1088 LOG(LS_WARNING) << "Missing digest or peer certificate."; |
| 1048 store, | 1089 return false; |
| 1049 SSL_get_ex_data_X509_STORE_CTX_idx())); | |
| 1050 OpenSSLStreamAdapter* stream = | |
| 1051 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); | |
| 1052 | |
| 1053 if (stream->peer_certificate_digest_algorithm_.empty()) { | |
| 1054 return 0; | |
| 1055 } | |
| 1056 X509* cert = X509_STORE_CTX_get_current_cert(store); | |
| 1057 int depth = X509_STORE_CTX_get_error_depth(store); | |
| 1058 | |
| 1059 // For now We ignore the parent certificates and verify the leaf against | |
| 1060 // the digest. | |
| 1061 // | |
| 1062 // TODO(jiayl): Verify the chain is a proper chain and report the chain to | |
| 1063 // |stream->peer_certificate_|. | |
| 1064 if (depth > 0) { | |
| 1065 LOG(LS_INFO) << "Ignored chained certificate at depth " << depth; | |
| 1066 return 1; | |
| 1067 } | 1090 } |
| 1068 | 1091 |
| 1069 unsigned char digest[EVP_MAX_MD_SIZE]; | 1092 unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1070 size_t digest_length; | 1093 size_t digest_length; |
| 1071 if (!OpenSSLCertificate::ComputeDigest( | 1094 if (!OpenSSLCertificate::ComputeDigest( |
| 1072 cert, | 1095 peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest, |
| 1073 stream->peer_certificate_digest_algorithm_, | 1096 sizeof(digest), &digest_length)) { |
| 1074 digest, sizeof(digest), | |
| 1075 &digest_length)) { | |
| 1076 LOG(LS_WARNING) << "Failed to compute peer cert digest."; | 1097 LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
| 1077 return 0; | 1098 return false; |
| 1078 } | 1099 } |
| 1079 | 1100 |
| 1080 Buffer computed_digest(digest, digest_length); | 1101 Buffer computed_digest(digest, digest_length); |
| 1081 if (computed_digest != stream->peer_certificate_digest_value_) { | 1102 if (computed_digest != peer_certificate_digest_value_) { |
| 1082 LOG(LS_WARNING) << "Rejected peer certificate due to mismatched digest."; | 1103 LOG(LS_WARNING) << "Rejected peer certificate due to mismatched digest."; |
| 1083 return 0; | 1104 return 0; |
| 1084 } | 1105 } |
| 1085 // Ignore any verification error if the digest matches, since there is no | 1106 // Ignore any verification error if the digest matches, since there is no |
| 1086 // value in checking the validity of a self-signed cert issued by untrusted | 1107 // value in checking the validity of a self-signed cert issued by untrusted |
| 1087 // sources. | 1108 // sources. |
| 1088 LOG(LS_INFO) << "Accepted peer certificate."; | 1109 LOG(LS_INFO) << "Accepted peer certificate."; |
| 1110 peer_certificate_verified_ = true; | |
| 1111 return true; | |
| 1112 } | |
| 1113 | |
| 1114 int OpenSSLStreamAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { | |
| 1115 // Get our SSL structure from the store | |
| 1116 SSL* ssl = reinterpret_cast<SSL*>( | |
| 1117 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx())); | |
| 1118 X509* cert = X509_STORE_CTX_get_current_cert(store); | |
| 1119 int depth = X509_STORE_CTX_get_error_depth(store); | |
| 1120 | |
| 1121 // For now we ignore the parent certificates and verify the leaf against | |
| 1122 // the digest. | |
| 1123 // | |
| 1124 // TODO(jiayl): Verify the chain is a proper chain and report the chain to | |
| 1125 // |stream->peer_certificate_|. | |
| 1126 if (depth > 0) { | |
| 1127 LOG(LS_INFO) << "Ignored chained certificate at depth " << depth; | |
| 1128 return 1; | |
| 1129 } | |
| 1130 | |
| 1131 OpenSSLStreamAdapter* stream = | |
| 1132 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); | |
| 1089 | 1133 |
| 1090 // Record the peer's certificate. | 1134 // Record the peer's certificate. |
| 1091 stream->peer_certificate_.reset(new OpenSSLCertificate(cert)); | 1135 stream->peer_certificate_.reset(new OpenSSLCertificate(cert)); |
| 1092 return 1; | 1136 |
| 1137 // If the peer certificate digest isn't known yet, we'll wait to verify | |
| 1138 // until it's known, and for now just return a success status. | |
| 1139 if (stream->peer_certificate_digest_algorithm_.empty()) { | |
| 1140 LOG(LS_INFO) << "Waiting to verify certificate until digest is known."; | |
| 1141 return 1; | |
| 1142 } | |
| 1143 | |
| 1144 return stream->VerifyPeerCertificate(); | |
| 1093 } | 1145 } |
| 1094 | 1146 |
| 1095 // This code is taken from the "Network Security with OpenSSL" | 1147 // This code is taken from the "Network Security with OpenSSL" |
| 1096 // sample in chapter 5 | 1148 // sample in chapter 5 |
| 1097 bool OpenSSLStreamAdapter::SSLPostConnectionCheck(SSL* ssl, | 1149 bool OpenSSLStreamAdapter::SSLPostConnectionCheck() { |
| 1098 const char* server_name, | |
| 1099 const X509* peer_cert, | |
| 1100 const std::string | |
| 1101 &peer_digest) { | |
| 1102 ASSERT(server_name != NULL); | |
| 1103 bool ok; | 1150 bool ok; |
| 1104 if (server_name[0] != '\0') { // traditional mode | 1151 if (verify_certificate_using_server_name()) { |
| 1105 ok = OpenSSLAdapter::VerifyServerName(ssl, server_name, ignore_bad_cert()); | 1152 RTC_DCHECK(!ssl_server_name_.empty()); |
| 1153 ok = OpenSSLAdapter::VerifyServerName(ssl_, ssl_server_name_.c_str(), | |
| 1154 ignore_bad_cert()); | |
| 1106 | 1155 |
| 1107 if (ok) { | 1156 if (ok) { |
| 1108 ok = (SSL_get_verify_result(ssl) == X509_V_OK || | 1157 ok = (SSL_get_verify_result(ssl_) == X509_V_OK || |
| 1109 custom_verification_succeeded_); | 1158 custom_verification_succeeded_); |
| 1110 } | 1159 } |
| 1111 } else { // peer-to-peer mode | 1160 } else { |
| 1112 ASSERT((peer_cert != NULL) || (!peer_digest.empty())); | 1161 RTC_DCHECK(verify_certificate_using_peer_digest()); |
| 1113 // no server name validation | 1162 // no server name validation |
| 1114 ok = true; | 1163 ok = peer_certificate_ || !client_auth_enabled(); |
| 1115 } | 1164 } |
| 1116 | 1165 |
| 1117 if (!ok && ignore_bad_cert()) { | 1166 if (!ok && ignore_bad_cert()) { |
| 1118 LOG(LS_ERROR) << "SSL_get_verify_result(ssl) = " | 1167 LOG(LS_ERROR) << "SSL_get_verify_result(ssl_) = " |
| 1119 << SSL_get_verify_result(ssl); | 1168 << SSL_get_verify_result(ssl_); |
| 1120 LOG(LS_INFO) << "Other TLS post connection checks failed."; | 1169 LOG(LS_INFO) << "Other TLS post connection checks failed."; |
| 1121 ok = true; | 1170 ok = true; |
| 1122 } | 1171 } |
| 1123 | 1172 |
| 1124 return ok; | 1173 return ok; |
| 1125 } | 1174 } |
| 1126 | 1175 |
| 1127 bool OpenSSLStreamAdapter::HaveDtls() { | 1176 bool OpenSSLStreamAdapter::HaveDtls() { |
| 1128 return true; | 1177 return true; |
| 1129 } | 1178 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1219 return true; | 1268 return true; |
| 1220 } | 1269 } |
| 1221 } | 1270 } |
| 1222 | 1271 |
| 1223 return false; | 1272 return false; |
| 1224 } | 1273 } |
| 1225 | 1274 |
| 1226 } // namespace rtc | 1275 } // namespace rtc |
| 1227 | 1276 |
| 1228 #endif // HAVE_OPENSSL_SSL_H | 1277 #endif // HAVE_OPENSSL_SSL_H |
| OLD | NEW |