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