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