Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: webrtc/base/opensslstreamadapter.cc

Issue 2352863003: Revert of Allow the DTLS fingerprint verification to occur after the handshake. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/opensslstreamadapter.h ('k') | webrtc/base/sslstreamadapter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
29 #include "webrtc/base/common.h" 28 #include "webrtc/base/common.h"
30 #include "webrtc/base/logging.h" 29 #include "webrtc/base/logging.h"
31 #include "webrtc/base/safe_conversions.h" 30 #include "webrtc/base/safe_conversions.h"
32 #include "webrtc/base/stream.h" 31 #include "webrtc/base/stream.h"
33 #include "webrtc/base/openssl.h" 32 #include "webrtc/base/openssl.h"
34 #include "webrtc/base/openssladapter.h" 33 #include "webrtc/base/openssladapter.h"
35 #include "webrtc/base/openssldigest.h" 34 #include "webrtc/base/openssldigest.h"
36 #include "webrtc/base/opensslidentity.h" 35 #include "webrtc/base/opensslidentity.h"
37 #include "webrtc/base/stringutils.h" 36 #include "webrtc/base/stringutils.h"
38 #include "webrtc/base/timeutils.h" 37 #include "webrtc/base/timeutils.h"
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 state_(SSL_NONE), 283 state_(SSL_NONE),
285 role_(SSL_CLIENT), 284 role_(SSL_CLIENT),
286 ssl_read_needs_write_(false), 285 ssl_read_needs_write_(false),
287 ssl_write_needs_read_(false), 286 ssl_write_needs_read_(false),
288 ssl_(NULL), 287 ssl_(NULL),
289 ssl_ctx_(NULL), 288 ssl_ctx_(NULL),
290 ssl_mode_(SSL_MODE_TLS), 289 ssl_mode_(SSL_MODE_TLS),
291 ssl_max_version_(SSL_PROTOCOL_TLS_12) {} 290 ssl_max_version_(SSL_PROTOCOL_TLS_12) {}
292 291
293 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { 292 OpenSSLStreamAdapter::~OpenSSLStreamAdapter() {
294 Cleanup(0); 293 Cleanup();
295 } 294 }
296 295
297 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { 296 void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) {
298 RTC_DCHECK(!identity_); 297 ASSERT(!identity_);
299 identity_.reset(static_cast<OpenSSLIdentity*>(identity)); 298 identity_.reset(static_cast<OpenSSLIdentity*>(identity));
300 } 299 }
301 300
302 void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { 301 void OpenSSLStreamAdapter::SetServerRole(SSLRole role) {
303 role_ = role; 302 role_ = role;
304 } 303 }
305 304
306 std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate() 305 std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate()
307 const { 306 const {
308 return peer_certificate_ ? std::unique_ptr<SSLCertificate>( 307 return peer_certificate_ ? std::unique_ptr<SSLCertificate>(
309 peer_certificate_->GetReference()) 308 peer_certificate_->GetReference())
310 : nullptr; 309 : nullptr;
311 } 310 }
312 311
313 SSLPeerCertificateDigestError OpenSSLStreamAdapter::SetPeerCertificateDigest( 312 bool OpenSSLStreamAdapter::SetPeerCertificateDigest(const std::string
314 const std::string& digest_alg, 313 &digest_alg,
315 const unsigned char* digest_val, 314 const unsigned char*
316 size_t digest_len) { 315 digest_val,
317 RTC_DCHECK(!peer_certificate_verified_); 316 size_t digest_len) {
318 RTC_DCHECK(!has_peer_certificate_digest()); 317 ASSERT(!peer_certificate_);
318 ASSERT(peer_certificate_digest_algorithm_.size() == 0);
319 size_t expected_len; 319 size_t expected_len;
320 320
321 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { 321 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
322 LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; 322 LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
323 return SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM; 323 return false;
324 } 324 }
325 if (expected_len != digest_len) { 325 if (expected_len != digest_len)
326 return SSLPeerCertificateDigestError::INVALID_LENGTH; 326 return false;
327 }
328 327
329 peer_certificate_digest_value_.SetData(digest_val, digest_len); 328 peer_certificate_digest_value_.SetData(digest_val, digest_len);
330 peer_certificate_digest_algorithm_ = digest_alg; 329 peer_certificate_digest_algorithm_ = digest_alg;
331 330
332 if (!peer_certificate_) { 331 return true;
333 // Normal case, where the digest is set before we obtain the certificate
334 // from the handshake.
335 return SSLPeerCertificateDigestError::NONE;
336 }
337
338 if (!VerifyPeerCertificate()) {
339 Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
340 return SSLPeerCertificateDigestError::VERIFICATION_FAILED;
341 }
342
343 if (state_ == SSL_CONNECTED) {
344 // Post the event asynchronously to unwind the stack. The caller
345 // of ContinueSSL may be the same object listening for these
346 // events and may not be prepared for reentrancy.
347 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
348 }
349
350 return SSLPeerCertificateDigestError::NONE;
351 } 332 }
352 333
353 std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { 334 std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
354 #ifdef OPENSSL_IS_BORINGSSL 335 #ifdef OPENSSL_IS_BORINGSSL
355 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite); 336 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
356 if (!ssl_cipher) { 337 if (!ssl_cipher) {
357 return std::string(); 338 return std::string();
358 } 339 }
359 char* cipher_name = SSL_CIPHER_get_rfc_name(ssl_cipher); 340 char* cipher_name = SSL_CIPHER_get_rfc_name(ssl_cipher);
360 std::string rfc_name = std::string(cipher_name); 341 std::string rfc_name = std::string(cipher_name);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 443
463 srtp_ciphers_ = internal_ciphers; 444 srtp_ciphers_ = internal_ciphers;
464 return true; 445 return true;
465 #else 446 #else
466 return false; 447 return false;
467 #endif 448 #endif
468 } 449 }
469 450
470 bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { 451 bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
471 #ifdef HAVE_DTLS_SRTP 452 #ifdef HAVE_DTLS_SRTP
472 RTC_DCHECK(state_ == SSL_CONNECTED); 453 ASSERT(state_ == SSL_CONNECTED);
473 if (state_ != SSL_CONNECTED) 454 if (state_ != SSL_CONNECTED)
474 return false; 455 return false;
475 456
476 const SRTP_PROTECTION_PROFILE *srtp_profile = 457 const SRTP_PROTECTION_PROFILE *srtp_profile =
477 SSL_get_selected_srtp_profile(ssl_); 458 SSL_get_selected_srtp_profile(ssl_);
478 459
479 if (!srtp_profile) 460 if (!srtp_profile)
480 return false; 461 return false;
481 462
482 *crypto_suite = srtp_profile->id; 463 *crypto_suite = srtp_profile->id;
483 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty()); 464 ASSERT(!SrtpCryptoSuiteToName(*crypto_suite).empty());
484 return true; 465 return true;
485 #else 466 #else
486 return false; 467 return false;
487 #endif 468 #endif
488 } 469 }
489 470
490 bool OpenSSLStreamAdapter::IsTlsConnected() {
491 return state_ == SSL_CONNECTED;
492 }
493
494 int OpenSSLStreamAdapter::StartSSL() { 471 int OpenSSLStreamAdapter::StartSSL() {
495 if (state_ != SSL_NONE) { 472 ASSERT(state_ == SSL_NONE);
496 // Don't allow StartSSL to be called twice.
497 return -1;
498 }
499 473
500 if (StreamAdapterInterface::GetState() != SS_OPEN) { 474 if (StreamAdapterInterface::GetState() != SS_OPEN) {
501 state_ = SSL_WAIT; 475 state_ = SSL_WAIT;
502 return 0; 476 return 0;
503 } 477 }
504 478
505 state_ = SSL_CONNECTING; 479 state_ = SSL_CONNECTING;
506 if (int err = BeginSSL()) { 480 if (int err = BeginSSL()) {
507 Error("BeginSSL", err, 0, false); 481 Error("BeginSSL", err, false);
508 return err; 482 return err;
509 } 483 }
510 484
511 return 0; 485 return 0;
512 } 486 }
513 487
514 void OpenSSLStreamAdapter::SetMode(SSLMode mode) { 488 void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
515 RTC_DCHECK(state_ == SSL_NONE); 489 ASSERT(state_ == SSL_NONE);
516 ssl_mode_ = mode; 490 ssl_mode_ = mode;
517 } 491 }
518 492
519 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { 493 void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
520 RTC_DCHECK(ssl_ctx_ == NULL); 494 ASSERT(ssl_ctx_ == NULL);
521 ssl_max_version_ = version; 495 ssl_max_version_ = version;
522 } 496 }
523 497
524 // 498 //
525 // StreamInterface Implementation 499 // StreamInterface Implementation
526 // 500 //
527 501
528 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, 502 StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len,
529 size_t* written, int* error) { 503 size_t* written, int* error) {
530 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; 504 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
531 505
532 switch (state_) { 506 switch (state_) {
533 case SSL_NONE: 507 case SSL_NONE:
534 // pass-through in clear text 508 // pass-through in clear text
535 return StreamAdapterInterface::Write(data, data_len, written, error); 509 return StreamAdapterInterface::Write(data, data_len, written, error);
536 510
537 case SSL_WAIT: 511 case SSL_WAIT:
538 case SSL_CONNECTING: 512 case SSL_CONNECTING:
539 return SR_BLOCK; 513 return SR_BLOCK;
540 514
541 case SSL_CONNECTED: 515 case SSL_CONNECTED:
542 if (waiting_to_verify_peer_certificate_cert()) {
543 return SR_BLOCK;
544 }
545 break; 516 break;
546 517
547 case SSL_ERROR: 518 case SSL_ERROR:
548 case SSL_CLOSED: 519 case SSL_CLOSED:
549 default: 520 default:
550 if (error) 521 if (error)
551 *error = ssl_error_code_; 522 *error = ssl_error_code_;
552 return SR_ERROR; 523 return SR_ERROR;
553 } 524 }
554 525
555 // OpenSSL will return an error if we try to write zero bytes 526 // OpenSSL will return an error if we try to write zero bytes
556 if (data_len == 0) { 527 if (data_len == 0) {
557 if (written) 528 if (written)
558 *written = 0; 529 *written = 0;
559 return SR_SUCCESS; 530 return SR_SUCCESS;
560 } 531 }
561 532
562 ssl_write_needs_read_ = false; 533 ssl_write_needs_read_ = false;
563 534
564 int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); 535 int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
565 int ssl_error = SSL_get_error(ssl_, code); 536 int ssl_error = SSL_get_error(ssl_, code);
566 switch (ssl_error) { 537 switch (ssl_error) {
567 case SSL_ERROR_NONE: 538 case SSL_ERROR_NONE:
568 LOG(LS_VERBOSE) << " -- success"; 539 LOG(LS_VERBOSE) << " -- success";
569 RTC_DCHECK(0 < code && static_cast<unsigned>(code) <= data_len); 540 ASSERT(0 < code && static_cast<unsigned>(code) <= data_len);
570 if (written) 541 if (written)
571 *written = code; 542 *written = code;
572 return SR_SUCCESS; 543 return SR_SUCCESS;
573 case SSL_ERROR_WANT_READ: 544 case SSL_ERROR_WANT_READ:
574 LOG(LS_VERBOSE) << " -- error want read"; 545 LOG(LS_VERBOSE) << " -- error want read";
575 ssl_write_needs_read_ = true; 546 ssl_write_needs_read_ = true;
576 return SR_BLOCK; 547 return SR_BLOCK;
577 case SSL_ERROR_WANT_WRITE: 548 case SSL_ERROR_WANT_WRITE:
578 LOG(LS_VERBOSE) << " -- error want write"; 549 LOG(LS_VERBOSE) << " -- error want write";
579 return SR_BLOCK; 550 return SR_BLOCK;
580 551
581 case SSL_ERROR_ZERO_RETURN: 552 case SSL_ERROR_ZERO_RETURN:
582 default: 553 default:
583 Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false); 554 Error("SSL_write", (ssl_error ? ssl_error : -1), false);
584 if (error) 555 if (error)
585 *error = ssl_error_code_; 556 *error = ssl_error_code_;
586 return SR_ERROR; 557 return SR_ERROR;
587 } 558 }
588 // not reached 559 // not reached
589 } 560 }
590 561
591 StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len, 562 StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len,
592 size_t* read, int* error) { 563 size_t* read, int* error) {
593 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; 564 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
594 switch (state_) { 565 switch (state_) {
595 case SSL_NONE: 566 case SSL_NONE:
596 // pass-through in clear text 567 // pass-through in clear text
597 return StreamAdapterInterface::Read(data, data_len, read, error); 568 return StreamAdapterInterface::Read(data, data_len, read, error);
598 569
599 case SSL_WAIT: 570 case SSL_WAIT:
600 case SSL_CONNECTING: 571 case SSL_CONNECTING:
601 return SR_BLOCK; 572 return SR_BLOCK;
602 573
603 case SSL_CONNECTED: 574 case SSL_CONNECTED:
604 if (waiting_to_verify_peer_certificate_cert()) {
605 return SR_BLOCK;
606 }
607 break; 575 break;
608 576
609 case SSL_CLOSED: 577 case SSL_CLOSED:
610 return SR_EOS; 578 return SR_EOS;
611 579
612 case SSL_ERROR: 580 case SSL_ERROR:
613 default: 581 default:
614 if (error) 582 if (error)
615 *error = ssl_error_code_; 583 *error = ssl_error_code_;
616 return SR_ERROR; 584 return SR_ERROR;
617 } 585 }
618 586
619 // Don't trust OpenSSL with zero byte reads 587 // Don't trust OpenSSL with zero byte reads
620 if (data_len == 0) { 588 if (data_len == 0) {
621 if (read) 589 if (read)
622 *read = 0; 590 *read = 0;
623 return SR_SUCCESS; 591 return SR_SUCCESS;
624 } 592 }
625 593
626 ssl_read_needs_write_ = false; 594 ssl_read_needs_write_ = false;
627 595
628 int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); 596 int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
629 int ssl_error = SSL_get_error(ssl_, code); 597 int ssl_error = SSL_get_error(ssl_, code);
630 switch (ssl_error) { 598 switch (ssl_error) {
631 case SSL_ERROR_NONE: 599 case SSL_ERROR_NONE:
632 LOG(LS_VERBOSE) << " -- success"; 600 LOG(LS_VERBOSE) << " -- success";
633 RTC_DCHECK(0 < code && static_cast<unsigned>(code) <= data_len); 601 ASSERT(0 < code && static_cast<unsigned>(code) <= data_len);
634 if (read) 602 if (read)
635 *read = code; 603 *read = code;
636 604
637 if (ssl_mode_ == SSL_MODE_DTLS) { 605 if (ssl_mode_ == SSL_MODE_DTLS) {
638 // Enforce atomic reads -- this is a short read 606 // Enforce atomic reads -- this is a short read
639 unsigned int pending = SSL_pending(ssl_); 607 unsigned int pending = SSL_pending(ssl_);
640 608
641 if (pending) { 609 if (pending) {
642 LOG(LS_INFO) << " -- short DTLS read. flushing"; 610 LOG(LS_INFO) << " -- short DTLS read. flushing";
643 FlushInput(pending); 611 FlushInput(pending);
644 if (error) 612 if (error)
645 *error = SSE_MSG_TRUNC; 613 *error = SSE_MSG_TRUNC;
646 return SR_ERROR; 614 return SR_ERROR;
647 } 615 }
648 } 616 }
649 return SR_SUCCESS; 617 return SR_SUCCESS;
650 case SSL_ERROR_WANT_READ: 618 case SSL_ERROR_WANT_READ:
651 LOG(LS_VERBOSE) << " -- error want read"; 619 LOG(LS_VERBOSE) << " -- error want read";
652 return SR_BLOCK; 620 return SR_BLOCK;
653 case SSL_ERROR_WANT_WRITE: 621 case SSL_ERROR_WANT_WRITE:
654 LOG(LS_VERBOSE) << " -- error want write"; 622 LOG(LS_VERBOSE) << " -- error want write";
655 ssl_read_needs_write_ = true; 623 ssl_read_needs_write_ = true;
656 return SR_BLOCK; 624 return SR_BLOCK;
657 case SSL_ERROR_ZERO_RETURN: 625 case SSL_ERROR_ZERO_RETURN:
658 LOG(LS_VERBOSE) << " -- remote side closed"; 626 LOG(LS_VERBOSE) << " -- remote side closed";
659 Close(); 627 // When we're closed at SSL layer, also close the stream level which
628 // performs necessary clean up. Otherwise, a new incoming packet after
629 // this could overflow the stream buffer.
630 this->stream()->Close();
660 return SR_EOS; 631 return SR_EOS;
661 break; 632 break;
662 default: 633 default:
663 LOG(LS_VERBOSE) << " -- error " << code; 634 LOG(LS_VERBOSE) << " -- error " << code;
664 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); 635 Error("SSL_read", (ssl_error ? ssl_error : -1), false);
665 if (error) 636 if (error)
666 *error = ssl_error_code_; 637 *error = ssl_error_code_;
667 return SR_ERROR; 638 return SR_ERROR;
668 } 639 }
669 // not reached 640 // not reached
670 } 641 }
671 642
672 void OpenSSLStreamAdapter::FlushInput(unsigned int left) { 643 void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
673 unsigned char buf[2048]; 644 unsigned char buf[2048];
674 645
675 while (left) { 646 while (left) {
676 // This should always succeed 647 // This should always succeed
677 int toread = (sizeof(buf) < left) ? sizeof(buf) : left; 648 int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
678 int code = SSL_read(ssl_, buf, toread); 649 int code = SSL_read(ssl_, buf, toread);
679 650
680 int ssl_error = SSL_get_error(ssl_, code); 651 int ssl_error = SSL_get_error(ssl_, code);
681 RTC_DCHECK(ssl_error == SSL_ERROR_NONE); 652 ASSERT(ssl_error == SSL_ERROR_NONE);
682 653
683 if (ssl_error != SSL_ERROR_NONE) { 654 if (ssl_error != SSL_ERROR_NONE) {
684 LOG(LS_VERBOSE) << " -- error " << code; 655 LOG(LS_VERBOSE) << " -- error " << code;
685 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); 656 Error("SSL_read", (ssl_error ? ssl_error : -1), false);
686 return; 657 return;
687 } 658 }
688 659
689 LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; 660 LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
690 left -= code; 661 left -= code;
691 } 662 }
692 } 663 }
693 664
694 void OpenSSLStreamAdapter::Close() { 665 void OpenSSLStreamAdapter::Close() {
695 Cleanup(0); 666 Cleanup();
696 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); 667 ASSERT(state_ == SSL_CLOSED || state_ == SSL_ERROR);
697 // When we're closed at SSL layer, also close the stream level which
698 // performs necessary clean up. Otherwise, a new incoming packet after
699 // this could overflow the stream buffer.
700 StreamAdapterInterface::Close(); 668 StreamAdapterInterface::Close();
701 } 669 }
702 670
703 StreamState OpenSSLStreamAdapter::GetState() const { 671 StreamState OpenSSLStreamAdapter::GetState() const {
704 switch (state_) { 672 switch (state_) {
705 case SSL_WAIT: 673 case SSL_WAIT:
706 case SSL_CONNECTING: 674 case SSL_CONNECTING:
707 return SS_OPENING; 675 return SS_OPENING;
708 case SSL_CONNECTED: 676 case SSL_CONNECTED:
709 if (waiting_to_verify_peer_certificate_cert()) {
710 return SS_OPENING;
711 }
712 return SS_OPEN; 677 return SS_OPEN;
713 default: 678 default:
714 return SS_CLOSED; 679 return SS_CLOSED;
715 }; 680 };
716 // not reached 681 // not reached
717 } 682 }
718 683
719 void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events, 684 void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events,
720 int err) { 685 int err) {
721 int events_to_signal = 0; 686 int events_to_signal = 0;
722 int signal_error = 0; 687 int signal_error = 0;
723 RTC_DCHECK(stream == this->stream()); 688 ASSERT(stream == this->stream());
724 if ((events & SE_OPEN)) { 689 if ((events & SE_OPEN)) {
725 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; 690 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
726 if (state_ != SSL_WAIT) { 691 if (state_ != SSL_WAIT) {
727 RTC_DCHECK(state_ == SSL_NONE); 692 ASSERT(state_ == SSL_NONE);
728 events_to_signal |= SE_OPEN; 693 events_to_signal |= SE_OPEN;
729 } else { 694 } else {
730 state_ = SSL_CONNECTING; 695 state_ = SSL_CONNECTING;
731 if (int err = BeginSSL()) { 696 if (int err = BeginSSL()) {
732 Error("BeginSSL", err, 0, true); 697 Error("BeginSSL", err, true);
733 return; 698 return;
734 } 699 }
735 } 700 }
736 } 701 }
737 if ((events & (SE_READ|SE_WRITE))) { 702 if ((events & (SE_READ|SE_WRITE))) {
738 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" 703 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
739 << ((events & SE_READ) ? " SE_READ" : "") 704 << ((events & SE_READ) ? " SE_READ" : "")
740 << ((events & SE_WRITE) ? " SE_WRITE" : ""); 705 << ((events & SE_WRITE) ? " SE_WRITE" : "");
741 if (state_ == SSL_NONE) { 706 if (state_ == SSL_NONE) {
742 events_to_signal |= events & (SE_READ|SE_WRITE); 707 events_to_signal |= events & (SE_READ|SE_WRITE);
743 } else if (state_ == SSL_CONNECTING) { 708 } else if (state_ == SSL_CONNECTING) {
744 if (int err = ContinueSSL()) { 709 if (int err = ContinueSSL()) {
745 Error("ContinueSSL", err, 0, true); 710 Error("ContinueSSL", err, true);
746 return; 711 return;
747 } 712 }
748 } else if (state_ == SSL_CONNECTED) { 713 } else if (state_ == SSL_CONNECTED) {
749 if (((events & SE_READ) && ssl_write_needs_read_) || 714 if (((events & SE_READ) && ssl_write_needs_read_) ||
750 (events & SE_WRITE)) { 715 (events & SE_WRITE)) {
751 LOG(LS_VERBOSE) << " -- onStreamWriteable"; 716 LOG(LS_VERBOSE) << " -- onStreamWriteable";
752 events_to_signal |= SE_WRITE; 717 events_to_signal |= SE_WRITE;
753 } 718 }
754 if (((events & SE_WRITE) && ssl_read_needs_write_) || 719 if (((events & SE_WRITE) && ssl_read_needs_write_) ||
755 (events & SE_READ)) { 720 (events & SE_READ)) {
756 LOG(LS_VERBOSE) << " -- onStreamReadable"; 721 LOG(LS_VERBOSE) << " -- onStreamReadable";
757 events_to_signal |= SE_READ; 722 events_to_signal |= SE_READ;
758 } 723 }
759 } 724 }
760 } 725 }
761 if ((events & SE_CLOSE)) { 726 if ((events & SE_CLOSE)) {
762 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err << ")"; 727 LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err << ")";
763 Cleanup(0); 728 Cleanup();
764 events_to_signal |= SE_CLOSE; 729 events_to_signal |= SE_CLOSE;
765 // SE_CLOSE is the only event that uses the final parameter to OnEvent(). 730 // SE_CLOSE is the only event that uses the final parameter to OnEvent().
766 RTC_DCHECK(signal_error == 0); 731 ASSERT(signal_error == 0);
767 signal_error = err; 732 signal_error = err;
768 } 733 }
769 if (events_to_signal) 734 if (events_to_signal)
770 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); 735 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
771 } 736 }
772 737
773 int OpenSSLStreamAdapter::BeginSSL() { 738 int OpenSSLStreamAdapter::BeginSSL() {
774 RTC_DCHECK(state_ == SSL_CONNECTING); 739 ASSERT(state_ == SSL_CONNECTING);
775 // The underlying stream has opened. 740 // The underlying stream has opened.
741 // A peer certificate digest must have been specified by now.
742 ASSERT(!peer_certificate_digest_algorithm_.empty());
776 LOG(LS_INFO) << "BeginSSL with peer."; 743 LOG(LS_INFO) << "BeginSSL with peer.";
777 744
778 BIO* bio = NULL; 745 BIO* bio = NULL;
779 746
780 // First set up the context. 747 // First set up the context.
781 RTC_DCHECK(ssl_ctx_ == NULL); 748 ASSERT(ssl_ctx_ == NULL);
782 ssl_ctx_ = SetupSSLContext(); 749 ssl_ctx_ = SetupSSLContext();
783 if (!ssl_ctx_) 750 if (!ssl_ctx_)
784 return -1; 751 return -1;
785 752
786 bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); 753 bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
787 if (!bio) 754 if (!bio)
788 return -1; 755 return -1;
789 756
790 ssl_ = SSL_new(ssl_ctx_); 757 ssl_ = SSL_new(ssl_ctx_);
791 if (!ssl_) { 758 if (!ssl_) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 SSL_set_tmp_ecdh(ssl_, ecdh); 792 SSL_set_tmp_ecdh(ssl_, ecdh);
826 EC_KEY_free(ecdh); 793 EC_KEY_free(ecdh);
827 #endif 794 #endif
828 795
829 // Do the connect 796 // Do the connect
830 return ContinueSSL(); 797 return ContinueSSL();
831 } 798 }
832 799
833 int OpenSSLStreamAdapter::ContinueSSL() { 800 int OpenSSLStreamAdapter::ContinueSSL() {
834 LOG(LS_VERBOSE) << "ContinueSSL"; 801 LOG(LS_VERBOSE) << "ContinueSSL";
835 RTC_DCHECK(state_ == SSL_CONNECTING); 802 ASSERT(state_ == SSL_CONNECTING);
836 803
837 // Clear the DTLS timer 804 // Clear the DTLS timer
838 Thread::Current()->Clear(this, MSG_TIMEOUT); 805 Thread::Current()->Clear(this, MSG_TIMEOUT);
839 806
840 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); 807 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
841 int ssl_error; 808 int ssl_error;
842 switch (ssl_error = SSL_get_error(ssl_, code)) { 809 switch (ssl_error = SSL_get_error(ssl_, code)) {
843 case SSL_ERROR_NONE: 810 case SSL_ERROR_NONE:
844 LOG(LS_VERBOSE) << " -- success"; 811 LOG(LS_VERBOSE) << " -- success";
845 // By this point, OpenSSL should have given us a certificate, or errored 812
846 // out if one was missing. 813 if (!SSLPostConnectionCheck(ssl_, NULL,
847 RTC_DCHECK(peer_certificate_ || !client_auth_enabled()); 814 peer_certificate_digest_algorithm_)) {
815 LOG(LS_ERROR) << "TLS post connection check failed";
816 return -1;
817 }
848 818
849 state_ = SSL_CONNECTED; 819 state_ = SSL_CONNECTED;
850 if (!waiting_to_verify_peer_certificate_cert()) { 820 StreamAdapterInterface::OnEvent(stream(), SE_OPEN|SE_READ|SE_WRITE, 0);
851 // We have everything we need to start the connection, so signal
852 // SE_OPEN. If we need a client certificate fingerprint and don't have
853 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
854 //
855 // Post the event asynchronously to unwind the stack. The
856 // caller of ContinueSSL may be the same object listening
857 // for these events and may not be prepared for reentrancy.
858 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
859 }
860 break; 821 break;
861 822
862 case SSL_ERROR_WANT_READ: { 823 case SSL_ERROR_WANT_READ: {
863 LOG(LS_VERBOSE) << " -- error want read"; 824 LOG(LS_VERBOSE) << " -- error want read";
864 struct timeval timeout; 825 struct timeval timeout;
865 if (DTLSv1_get_timeout(ssl_, &timeout)) { 826 if (DTLSv1_get_timeout(ssl_, &timeout)) {
866 int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; 827 int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000;
867 828
868 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, 829 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this,
869 MSG_TIMEOUT, 0); 830 MSG_TIMEOUT, 0);
(...skipping 13 matching lines...) Expand all
883 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) { 844 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
884 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE; 845 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
885 } 846 }
886 SignalSSLHandshakeError(ssl_handshake_err); 847 SignalSSLHandshakeError(ssl_handshake_err);
887 return (ssl_error != 0) ? ssl_error : -1; 848 return (ssl_error != 0) ? ssl_error : -1;
888 } 849 }
889 850
890 return 0; 851 return 0;
891 } 852 }
892 853
893 void OpenSSLStreamAdapter::Error(const char* context, 854 void OpenSSLStreamAdapter::Error(const char* context, int err, bool signal) {
894 int err, 855 LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error("
895 uint8_t alert, 856 << context << ", " << err << ")";
896 bool signal) {
897 LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", " << err
898 << ", " << static_cast<int>(alert) << ")";
899 state_ = SSL_ERROR; 857 state_ = SSL_ERROR;
900 ssl_error_code_ = err; 858 ssl_error_code_ = err;
901 Cleanup(alert); 859 Cleanup();
902 if (signal) 860 if (signal)
903 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err); 861 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
904 } 862 }
905 863
906 void OpenSSLStreamAdapter::Cleanup(uint8_t alert) { 864 void OpenSSLStreamAdapter::Cleanup() {
907 LOG(LS_INFO) << "Cleanup"; 865 LOG(LS_INFO) << "Cleanup";
908 866
909 if (state_ != SSL_ERROR) { 867 if (state_ != SSL_ERROR) {
910 state_ = SSL_CLOSED; 868 state_ = SSL_CLOSED;
911 ssl_error_code_ = 0; 869 ssl_error_code_ = 0;
912 } 870 }
913 871
914 if (ssl_) { 872 if (ssl_) {
915 int ret; 873 int ret = SSL_shutdown(ssl_);
916 // SSL_send_fatal_alert is only available in BoringSSL. 874 if (ret < 0) {
917 #ifdef OPENSSL_IS_BORINGSSL 875 LOG(LS_WARNING) << "SSL_shutdown failed, error = "
918 if (alert) { 876 << SSL_get_error(ssl_, ret);
919 ret = SSL_send_fatal_alert(ssl_, alert);
920 if (ret < 0) {
921 LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
922 << SSL_get_error(ssl_, ret);
923 }
924 } else {
925 #endif
926 ret = SSL_shutdown(ssl_);
927 if (ret < 0) {
928 LOG(LS_WARNING) << "SSL_shutdown failed, error = "
929 << SSL_get_error(ssl_, ret);
930 }
931 #ifdef OPENSSL_IS_BORINGSSL
932 } 877 }
933 #endif 878
934 SSL_free(ssl_); 879 SSL_free(ssl_);
935 ssl_ = NULL; 880 ssl_ = NULL;
936 } 881 }
937 if (ssl_ctx_) { 882 if (ssl_ctx_) {
938 SSL_CTX_free(ssl_ctx_); 883 SSL_CTX_free(ssl_ctx_);
939 ssl_ctx_ = NULL; 884 ssl_ctx_ = NULL;
940 } 885 }
941 identity_.reset(); 886 identity_.reset();
942 peer_certificate_.reset(); 887 peer_certificate_.reset();
943 888
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { 1026 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
1082 SSL_CTX_free(ctx); 1027 SSL_CTX_free(ctx);
1083 return NULL; 1028 return NULL;
1084 } 1029 }
1085 } 1030 }
1086 #endif 1031 #endif
1087 1032
1088 return ctx; 1033 return ctx;
1089 } 1034 }
1090 1035
1091 bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
1092 if (!has_peer_certificate_digest() || !peer_certificate_) {
1093 LOG(LS_WARNING) << "Missing digest or peer certificate.";
1094 return false;
1095 }
1096
1097 unsigned char digest[EVP_MAX_MD_SIZE];
1098 size_t digest_length;
1099 if (!OpenSSLCertificate::ComputeDigest(
1100 peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest,
1101 sizeof(digest), &digest_length)) {
1102 LOG(LS_WARNING) << "Failed to compute peer cert digest.";
1103 return false;
1104 }
1105
1106 Buffer computed_digest(digest, digest_length);
1107 if (computed_digest != peer_certificate_digest_value_) {
1108 LOG(LS_WARNING) << "Rejected peer certificate due to mismatched digest.";
1109 return 0;
1110 }
1111 // Ignore any verification error if the digest matches, since there is no
1112 // value in checking the validity of a self-signed cert issued by untrusted
1113 // sources.
1114 LOG(LS_INFO) << "Accepted peer certificate.";
1115 peer_certificate_verified_ = true;
1116 return true;
1117 }
1118
1119 int OpenSSLStreamAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { 1036 int OpenSSLStreamAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) {
1120 // Get our SSL structure from the store 1037 // Get our SSL structure from the store
1121 SSL* ssl = reinterpret_cast<SSL*>( 1038 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(
1122 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx())); 1039 store,
1040 SSL_get_ex_data_X509_STORE_CTX_idx()));
1041 OpenSSLStreamAdapter* stream =
1042 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
1043
1044 if (stream->peer_certificate_digest_algorithm_.empty()) {
1045 return 0;
1046 }
1123 X509* cert = X509_STORE_CTX_get_current_cert(store); 1047 X509* cert = X509_STORE_CTX_get_current_cert(store);
1124 int depth = X509_STORE_CTX_get_error_depth(store); 1048 int depth = X509_STORE_CTX_get_error_depth(store);
1125 1049
1126 // For now we ignore the parent certificates and verify the leaf against 1050 // For now We ignore the parent certificates and verify the leaf against
1127 // the digest. 1051 // the digest.
1128 // 1052 //
1129 // TODO(jiayl): Verify the chain is a proper chain and report the chain to 1053 // TODO(jiayl): Verify the chain is a proper chain and report the chain to
1130 // |stream->peer_certificate_|. 1054 // |stream->peer_certificate_|.
1131 if (depth > 0) { 1055 if (depth > 0) {
1132 LOG(LS_INFO) << "Ignored chained certificate at depth " << depth; 1056 LOG(LS_INFO) << "Ignored chained certificate at depth " << depth;
1133 return 1; 1057 return 1;
1134 } 1058 }
1135 1059
1136 OpenSSLStreamAdapter* stream = 1060 unsigned char digest[EVP_MAX_MD_SIZE];
1137 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); 1061 size_t digest_length;
1062 if (!OpenSSLCertificate::ComputeDigest(
1063 cert,
1064 stream->peer_certificate_digest_algorithm_,
1065 digest, sizeof(digest),
1066 &digest_length)) {
1067 LOG(LS_WARNING) << "Failed to compute peer cert digest.";
1068 return 0;
1069 }
1070
1071 Buffer computed_digest(digest, digest_length);
1072 if (computed_digest != stream->peer_certificate_digest_value_) {
1073 LOG(LS_WARNING) << "Rejected peer certificate due to mismatched digest.";
1074 return 0;
1075 }
1076 // Ignore any verification error if the digest matches, since there is no
1077 // value in checking the validity of a self-signed cert issued by untrusted
1078 // sources.
1079 LOG(LS_INFO) << "Accepted peer certificate.";
1138 1080
1139 // Record the peer's certificate. 1081 // Record the peer's certificate.
1140 stream->peer_certificate_.reset(new OpenSSLCertificate(cert)); 1082 stream->peer_certificate_.reset(new OpenSSLCertificate(cert));
1083 return 1;
1084 }
1141 1085
1142 // If the peer certificate digest isn't known yet, we'll wait to verify 1086 bool OpenSSLStreamAdapter::SSLPostConnectionCheck(SSL* ssl,
1143 // until it's known, and for now just return a success status. 1087 const X509* peer_cert,
1144 if (stream->peer_certificate_digest_algorithm_.empty()) { 1088 const std::string
1145 LOG(LS_INFO) << "Waiting to verify certificate until digest is known."; 1089 &peer_digest) {
1146 return 1; 1090 ASSERT((peer_cert != NULL) || (!peer_digest.empty()));
1147 } 1091 return true;
1148
1149 return stream->VerifyPeerCertificate();
1150 } 1092 }
1151 1093
1152 bool OpenSSLStreamAdapter::HaveDtls() { 1094 bool OpenSSLStreamAdapter::HaveDtls() {
1153 return true; 1095 return true;
1154 } 1096 }
1155 1097
1156 bool OpenSSLStreamAdapter::HaveDtlsSrtp() { 1098 bool OpenSSLStreamAdapter::HaveDtlsSrtp() {
1157 #ifdef HAVE_DTLS_SRTP 1099 #ifdef HAVE_DTLS_SRTP
1158 return true; 1100 return true;
1159 #else 1101 #else
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1244 return true; 1186 return true;
1245 } 1187 }
1246 } 1188 }
1247 1189
1248 return false; 1190 return false;
1249 } 1191 }
1250 1192
1251 } // namespace rtc 1193 } // namespace rtc
1252 1194
1253 #endif // HAVE_OPENSSL_SSL_H 1195 #endif // HAVE_OPENSSL_SSL_H
OLDNEW
« no previous file with comments | « webrtc/base/opensslstreamadapter.h ('k') | webrtc/base/sslstreamadapter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698