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