OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/ssl_server_socket_openssl.h" | 5 #include "net/socket/ssl_server_socket_openssl.h" |
6 | 6 |
7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
8 #include <openssl/ssl.h> | 8 #include <openssl/ssl.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
15 #include "crypto/rsa_private_key.h" | 15 #include "crypto/rsa_private_key.h" |
16 #include "crypto/scoped_openssl_types.h" | 16 #include "crypto/scoped_openssl_types.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/cert/cert_verify_result.h" |
| 19 #include "net/cert/client_cert_verifier.h" |
| 20 #include "net/cert/x509_util_openssl.h" |
18 #include "net/ssl/openssl_ssl_util.h" | 21 #include "net/ssl/openssl_ssl_util.h" |
19 #include "net/ssl/scoped_openssl_types.h" | 22 #include "net/ssl/scoped_openssl_types.h" |
| 23 #include "net/ssl/ssl_connection_status_flags.h" |
| 24 #include "net/ssl/ssl_info.h" |
20 | 25 |
21 #define GotoState(s) next_handshake_state_ = s | 26 #define GotoState(s) next_handshake_state_ = s |
22 | 27 |
23 namespace net { | 28 namespace net { |
24 | 29 |
| 30 namespace { |
| 31 |
| 32 // Creates an X509Certificate out of the concatenation of |cert|, if non-null, |
| 33 // with |chain|. |
| 34 scoped_refptr<X509Certificate> CreateX509Certificate(X509* cert, |
| 35 STACK_OF(X509) * chain) { |
| 36 std::vector<base::StringPiece> der_chain; |
| 37 base::StringPiece der_cert; |
| 38 scoped_refptr<X509Certificate> client_cert; |
| 39 if (cert) { |
| 40 if (!x509_util::GetDER(cert, &der_cert)) |
| 41 return nullptr; |
| 42 der_chain.push_back(der_cert); |
| 43 } |
| 44 |
| 45 for (size_t i = 0; i < sk_X509_num(chain); ++i) { |
| 46 X509* x = sk_X509_value(chain, i); |
| 47 if (!x509_util::GetDER(x, &der_cert)) |
| 48 return nullptr; |
| 49 der_chain.push_back(der_cert); |
| 50 } |
| 51 |
| 52 return X509Certificate::CreateFromDERCertChain(der_chain); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
25 void EnableSSLServerSockets() { | 57 void EnableSSLServerSockets() { |
26 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit(). | 58 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit(). |
27 } | 59 } |
28 | 60 |
29 scoped_ptr<SSLServerSocket> CreateSSLServerSocket( | 61 scoped_ptr<SSLServerSocket> CreateSSLServerSocket( |
30 scoped_ptr<StreamSocket> socket, | 62 scoped_ptr<StreamSocket> socket, |
31 X509Certificate* certificate, | 63 X509Certificate* certificate, |
32 const crypto::RSAPrivateKey& key, | 64 const crypto::RSAPrivateKey& key, |
33 const SSLServerConfig& ssl_config) { | 65 const SSLServerConfig& ssl_server_config) { |
34 crypto::EnsureOpenSSLInit(); | 66 crypto::EnsureOpenSSLInit(); |
35 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL( | 67 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL( |
36 std::move(socket), certificate, key, ssl_config)); | 68 std::move(socket), certificate, key, ssl_server_config)); |
37 } | 69 } |
38 | 70 |
39 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL( | 71 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL( |
40 scoped_ptr<StreamSocket> transport_socket, | 72 scoped_ptr<StreamSocket> transport_socket, |
41 scoped_refptr<X509Certificate> certificate, | 73 scoped_refptr<X509Certificate> certificate, |
42 const crypto::RSAPrivateKey& key, | 74 const crypto::RSAPrivateKey& key, |
43 const SSLServerConfig& ssl_config) | 75 const SSLServerConfig& ssl_server_config) |
44 : transport_send_busy_(false), | 76 : transport_send_busy_(false), |
45 transport_recv_busy_(false), | 77 transport_recv_busy_(false), |
46 transport_recv_eof_(false), | 78 transport_recv_eof_(false), |
47 user_read_buf_len_(0), | 79 user_read_buf_len_(0), |
48 user_write_buf_len_(0), | 80 user_write_buf_len_(0), |
49 transport_write_error_(OK), | 81 transport_write_error_(OK), |
50 ssl_(NULL), | 82 ssl_(NULL), |
51 transport_bio_(NULL), | 83 transport_bio_(NULL), |
52 transport_socket_(std::move(transport_socket)), | 84 transport_socket_(std::move(transport_socket)), |
53 ssl_config_(ssl_config), | 85 ssl_server_config_(ssl_server_config), |
54 cert_(certificate), | 86 cert_(certificate), |
55 key_(key.Copy()), | 87 key_(key.Copy()), |
56 next_handshake_state_(STATE_NONE), | 88 next_handshake_state_(STATE_NONE), |
57 completed_handshake_(false) { | 89 completed_handshake_(false) { |
58 CHECK(key_); | 90 CHECK(key_); |
59 } | 91 } |
60 | 92 |
61 SSLServerSocketOpenSSL::~SSLServerSocketOpenSSL() { | 93 SSLServerSocketOpenSSL::~SSLServerSocketOpenSSL() { |
62 if (ssl_) { | 94 if (ssl_) { |
63 // Calling SSL_shutdown prevents the session from being marked as | 95 // Calling SSL_shutdown prevents the session from being marked as |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 NOTIMPLEMENTED(); | 267 NOTIMPLEMENTED(); |
236 return false; | 268 return false; |
237 } | 269 } |
238 | 270 |
239 NextProto SSLServerSocketOpenSSL::GetNegotiatedProtocol() const { | 271 NextProto SSLServerSocketOpenSSL::GetNegotiatedProtocol() const { |
240 // NPN is not supported by this class. | 272 // NPN is not supported by this class. |
241 return kProtoUnknown; | 273 return kProtoUnknown; |
242 } | 274 } |
243 | 275 |
244 bool SSLServerSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { | 276 bool SSLServerSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
245 NOTIMPLEMENTED(); | 277 ssl_info->Reset(); |
246 return false; | 278 if (!completed_handshake_) |
| 279 return false; |
| 280 |
| 281 ssl_info->cert = client_cert_; |
| 282 |
| 283 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 284 CHECK(cipher); |
| 285 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
| 286 |
| 287 SSLConnectionStatusSetCipherSuite( |
| 288 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), |
| 289 &ssl_info->connection_status); |
| 290 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), |
| 291 &ssl_info->connection_status); |
| 292 |
| 293 if (!SSL_get_secure_renegotiation_support(ssl_)) |
| 294 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
| 295 |
| 296 ssl_info->handshake_type = SSL_session_reused(ssl_) |
| 297 ? SSLInfo::HANDSHAKE_RESUME |
| 298 : SSLInfo::HANDSHAKE_FULL; |
| 299 |
| 300 return true; |
247 } | 301 } |
248 | 302 |
249 void SSLServerSocketOpenSSL::GetConnectionAttempts( | 303 void SSLServerSocketOpenSSL::GetConnectionAttempts( |
250 ConnectionAttempts* out) const { | 304 ConnectionAttempts* out) const { |
251 out->clear(); | 305 out->clear(); |
252 } | 306 } |
253 | 307 |
254 int64_t SSLServerSocketOpenSSL::GetTotalReceivedBytes() const { | 308 int64_t SSLServerSocketOpenSSL::GetTotalReceivedBytes() const { |
255 return transport_socket_->GetTotalReceivedBytes(); | 309 return transport_socket_->GetTotalReceivedBytes(); |
256 } | 310 } |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 return rv; | 613 return rv; |
560 } | 614 } |
561 | 615 |
562 int SSLServerSocketOpenSSL::DoHandshake() { | 616 int SSLServerSocketOpenSSL::DoHandshake() { |
563 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 617 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
564 int net_error = OK; | 618 int net_error = OK; |
565 int rv = SSL_do_handshake(ssl_); | 619 int rv = SSL_do_handshake(ssl_); |
566 | 620 |
567 if (rv == 1) { | 621 if (rv == 1) { |
568 completed_handshake_ = true; | 622 completed_handshake_ = true; |
| 623 // The results of SSL_get_peer_certificate() must be explicitly freed. |
| 624 ScopedX509 cert(SSL_get_peer_certificate(ssl_)); |
| 625 if (cert) { |
| 626 // The caller does not take ownership of SSL_get_peer_cert_chain's |
| 627 // results. |
| 628 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl_); |
| 629 client_cert_ = CreateX509Certificate(cert.get(), chain); |
| 630 if (!client_cert_.get()) |
| 631 return ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT; |
| 632 } |
569 } else { | 633 } else { |
570 int ssl_error = SSL_get_error(ssl_, rv); | 634 int ssl_error = SSL_get_error(ssl_, rv); |
571 OpenSSLErrorInfo error_info; | 635 OpenSSLErrorInfo error_info; |
572 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); | 636 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
573 | 637 |
| 638 // This hack is necessary because the mapping of SSL error codes to |
| 639 // net_errors assumes (correctly for client sockets, but erroneously for |
| 640 // server sockets) that peer cert verification failure can only occur if |
| 641 // the cert changed during a renego. crbug.com/570351 |
| 642 if (net_error == ERR_SSL_SERVER_CERT_CHANGED) |
| 643 net_error = ERR_BAD_SSL_CLIENT_AUTH_CERT; |
| 644 |
574 // If not done, stay in this state | 645 // If not done, stay in this state |
575 if (net_error == ERR_IO_PENDING) { | 646 if (net_error == ERR_IO_PENDING) { |
576 GotoState(STATE_HANDSHAKE); | 647 GotoState(STATE_HANDSHAKE); |
577 } else { | 648 } else { |
578 LOG(ERROR) << "handshake failed; returned " << rv | 649 LOG(ERROR) << "handshake failed; returned " << rv |
579 << ", SSL error code " << ssl_error | 650 << ", SSL error code " << ssl_error |
580 << ", net_error " << net_error; | 651 << ", net_error " << net_error; |
581 net_log_.AddEvent( | 652 net_log_.AddEvent( |
582 NetLog::TYPE_SSL_HANDSHAKE_ERROR, | 653 NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
583 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); | 654 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
(...skipping 24 matching lines...) Expand all Loading... |
608 user_write_buf_len_ = 0; | 679 user_write_buf_len_ = 0; |
609 base::ResetAndReturn(&user_write_callback_).Run(rv); | 680 base::ResetAndReturn(&user_write_callback_).Run(rv); |
610 } | 681 } |
611 | 682 |
612 int SSLServerSocketOpenSSL::Init() { | 683 int SSLServerSocketOpenSSL::Init() { |
613 DCHECK(!ssl_); | 684 DCHECK(!ssl_); |
614 DCHECK(!transport_bio_); | 685 DCHECK(!transport_bio_); |
615 | 686 |
616 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 687 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
617 | 688 |
618 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(SSLv23_server_method())); | 689 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(TLS_method())); |
619 | 690 int verify_mode = 0; |
620 if (ssl_config_.require_client_cert) | 691 switch (ssl_server_config_.client_cert_type) { |
621 SSL_CTX_set_verify(ssl_ctx.get(), SSL_VERIFY_PEER, NULL); | 692 case SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT: |
622 | 693 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 694 // Fall-through |
| 695 case SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT: |
| 696 verify_mode |= SSL_VERIFY_PEER; |
| 697 SSL_CTX_set_verify(ssl_ctx.get(), verify_mode, nullptr); |
| 698 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), CertVerifyCallback, |
| 699 ssl_server_config_.client_cert_verifier); |
| 700 break; |
| 701 case SSLServerConfig::ClientCertType::NO_CLIENT_CERT: |
| 702 break; |
| 703 } |
623 ssl_ = SSL_new(ssl_ctx.get()); | 704 ssl_ = SSL_new(ssl_ctx.get()); |
624 if (!ssl_) | 705 if (!ssl_) |
625 return ERR_UNEXPECTED; | 706 return ERR_UNEXPECTED; |
626 | 707 |
627 BIO* ssl_bio = NULL; | 708 BIO* ssl_bio = NULL; |
628 // 0 => use default buffer sizes. | 709 // 0 => use default buffer sizes. |
629 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) | 710 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
630 return ERR_UNEXPECTED; | 711 return ERR_UNEXPECTED; |
631 DCHECK(ssl_bio); | 712 DCHECK(ssl_bio); |
632 DCHECK(transport_bio_); | 713 DCHECK(transport_bio_); |
(...skipping 26 matching lines...) Expand all Loading... |
659 return ERR_UNEXPECTED; | 740 return ERR_UNEXPECTED; |
660 } | 741 } |
661 #endif // USE_OPENSSL_CERTS | 742 #endif // USE_OPENSSL_CERTS |
662 | 743 |
663 DCHECK(key_->key()); | 744 DCHECK(key_->key()); |
664 if (SSL_use_PrivateKey(ssl_, key_->key()) != 1) { | 745 if (SSL_use_PrivateKey(ssl_, key_->key()) != 1) { |
665 LOG(ERROR) << "Cannot set private key."; | 746 LOG(ERROR) << "Cannot set private key."; |
666 return ERR_UNEXPECTED; | 747 return ERR_UNEXPECTED; |
667 } | 748 } |
668 | 749 |
669 DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); | 750 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_min); |
670 DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); | 751 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_max); |
671 SSL_set_min_version(ssl_, ssl_config_.version_min); | 752 SSL_set_min_version(ssl_, ssl_server_config_.version_min); |
672 SSL_set_max_version(ssl_, ssl_config_.version_max); | 753 SSL_set_max_version(ssl_, ssl_server_config_.version_max); |
673 | 754 |
674 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, | 755 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
675 // set everything we care about to an absolute value. | 756 // set everything we care about to an absolute value. |
676 SslSetClearMask options; | 757 SslSetClearMask options; |
677 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); | 758 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
678 | 759 |
679 SSL_set_options(ssl_, options.set_mask); | 760 SSL_set_options(ssl_, options.set_mask); |
680 SSL_clear_options(ssl_, options.clear_mask); | 761 SSL_clear_options(ssl_, options.clear_mask); |
681 | 762 |
682 // Same as above, this time for the SSL mode. | 763 // Same as above, this time for the SSL mode. |
683 SslSetClearMask mode; | 764 SslSetClearMask mode; |
684 | 765 |
685 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); | 766 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
686 | 767 |
687 SSL_set_mode(ssl_, mode.set_mask); | 768 SSL_set_mode(ssl_, mode.set_mask); |
688 SSL_clear_mode(ssl_, mode.clear_mask); | 769 SSL_clear_mode(ssl_, mode.clear_mask); |
689 | 770 |
690 // See SSLServerConfig::disabled_cipher_suites for description of the suites | 771 // See SSLServerConfig::disabled_cipher_suites for description of the suites |
691 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 | 772 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 |
692 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 | 773 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
693 // as the handshake hash. | 774 // as the handshake hash. |
694 std::string command("DEFAULT:!SHA256:!SHA384:!AESGCM+AES256:!aPSK"); | 775 std::string command("DEFAULT:!SHA256:!SHA384:!AESGCM+AES256:!aPSK"); |
695 | 776 |
696 if (ssl_config_.require_ecdhe) | 777 if (ssl_server_config_.require_ecdhe) |
697 command.append(":!kRSA:!kDHE"); | 778 command.append(":!kRSA:!kDHE"); |
698 | 779 |
699 // Remove any disabled ciphers. | 780 // Remove any disabled ciphers. |
700 for (uint16_t id : ssl_config_.disabled_cipher_suites) { | 781 for (uint16_t id : ssl_server_config_.disabled_cipher_suites) { |
701 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); | 782 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); |
702 if (cipher) { | 783 if (cipher) { |
703 command.append(":!"); | 784 command.append(":!"); |
704 command.append(SSL_CIPHER_get_name(cipher)); | 785 command.append(SSL_CIPHER_get_name(cipher)); |
705 } | 786 } |
706 } | 787 } |
707 | 788 |
708 int rv = SSL_set_cipher_list(ssl_, command.c_str()); | 789 int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
709 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. | 790 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
710 // This will almost certainly result in the socket failing to complete the | 791 // This will almost certainly result in the socket failing to complete the |
711 // handshake at which point the appropriate error is bubbled up to the client. | 792 // handshake at which point the appropriate error is bubbled up to the client. |
712 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command | 793 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command |
713 << "') returned " << rv; | 794 << "') returned " << rv; |
714 | 795 |
| 796 if (ssl_server_config_.client_cert_type != |
| 797 SSLServerConfig::ClientCertType::NO_CLIENT_CERT && |
| 798 !ssl_server_config_.cert_authorities_.empty()) { |
| 799 ScopedX509NameStack stack(sk_X509_NAME_new_null()); |
| 800 for (const auto& authority : ssl_server_config_.cert_authorities_) { |
| 801 const uint8_t* name = reinterpret_cast<const uint8_t*>(authority.c_str()); |
| 802 const uint8_t* name_start = name; |
| 803 ScopedX509_NAME subj(d2i_X509_NAME(nullptr, &name, authority.length())); |
| 804 if (!subj || name != name_start + authority.length()) |
| 805 return ERR_UNEXPECTED; |
| 806 sk_X509_NAME_push(stack.get(), subj.release()); |
| 807 } |
| 808 SSL_set_client_CA_list(ssl_, stack.release()); |
| 809 } |
| 810 |
715 return OK; | 811 return OK; |
716 } | 812 } |
717 | 813 |
| 814 // static |
| 815 int SSLServerSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx, |
| 816 void* arg) { |
| 817 ClientCertVerifier* verifier = reinterpret_cast<ClientCertVerifier*>(arg); |
| 818 // If a verifier was not supplied, all certificates are accepted. |
| 819 if (!verifier) |
| 820 return 1; |
| 821 STACK_OF(X509)* chain = store_ctx->untrusted; |
| 822 scoped_refptr<X509Certificate> client_cert( |
| 823 CreateX509Certificate(nullptr, chain)); |
| 824 if (!client_cert.get()) { |
| 825 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED); |
| 826 return 0; |
| 827 } |
| 828 // Asynchronous completion of Verify is currently not supported. |
| 829 // http://crbug.com/347402 |
| 830 // The API for Verify supports the parts needed for async completion |
| 831 // but is currently expected to complete synchronously. |
| 832 scoped_ptr<ClientCertVerifier::Request> ignore_async; |
| 833 int res = |
| 834 verifier->Verify(client_cert.get(), CompletionCallback(), &ignore_async); |
| 835 DCHECK_NE(res, ERR_IO_PENDING); |
| 836 |
| 837 if (res != OK) { |
| 838 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED); |
| 839 return 0; |
| 840 } |
| 841 return 1; |
| 842 } |
| 843 |
718 } // namespace net | 844 } // namespace net |
OLD | NEW |