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

Side by Side Diff: net/socket/ssl_server_socket_openssl.cc

Issue 1518613002: Support for server session cache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@client_certs
Patch Set: Rebase only Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 18 #include "net/cert/cert_verify_result.h"
19 #include "net/cert/client_cert_verifier.h" 19 #include "net/cert/client_cert_verifier.h"
20 #include "net/cert/x509_util_openssl.h" 20 #include "net/cert/x509_util_openssl.h"
21 #include "net/ssl/openssl_ssl_util.h" 21 #include "net/ssl/openssl_ssl_util.h"
22 #include "net/ssl/scoped_openssl_types.h"
23 #include "net/ssl/ssl_connection_status_flags.h" 22 #include "net/ssl/ssl_connection_status_flags.h"
24 #include "net/ssl/ssl_info.h" 23 #include "net/ssl/ssl_info.h"
25 24
26 #define GotoState(s) next_handshake_state_ = s 25 #define GotoState(s) next_handshake_state_ = s
27 26
28 namespace net { 27 namespace net {
29 28
30 namespace { 29 namespace {
31 30
32 scoped_refptr<X509Certificate> CreateX509Certificate(X509* cert, 31 scoped_refptr<X509Certificate> CreateX509Certificate(X509* cert,
(...skipping 23 matching lines...) Expand all
56 scoped_refptr<X509Certificate> GetClientCert(SSL* ssl) { 55 scoped_refptr<X509Certificate> GetClientCert(SSL* ssl) {
57 X509* cert = SSL_get_peer_certificate(ssl); 56 X509* cert = SSL_get_peer_certificate(ssl);
58 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl); 57 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl);
59 return CreateX509Certificate(cert, chain); 58 return CreateX509Certificate(cert, chain);
60 } 59 }
61 60
62 void DoNothingOnCompletion(int ignore) {} 61 void DoNothingOnCompletion(int ignore) {}
63 62
64 } // namespace 63 } // namespace
65 64
65 scoped_ptr<SSLServerSocketContext> CreateSSLServerSocketContext(
66 X509Certificate* certificate,
67 const crypto::RSAPrivateKey& key,
68 const SSLServerConfig& ssl_server_config) {
69 crypto::EnsureOpenSSLInit();
70 return scoped_ptr<SSLServerSocketContext>(
71 new SSLServerSocketContextOpenSSL(certificate, key, ssl_server_config));
72 }
73
66 void EnableSSLServerSockets() { 74 void EnableSSLServerSockets() {
67 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit(). 75 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit().
68 } 76 }
69 77
70 scoped_ptr<SSLServerSocket> CreateSSLServerSocket( 78 SSLServerSocketContextOpenSSL::SSLServerSocketContextOpenSSL(
71 scoped_ptr<StreamSocket> socket, 79 scoped_refptr<X509Certificate> certificate,
72 X509Certificate* certificate,
73 const crypto::RSAPrivateKey& key, 80 const crypto::RSAPrivateKey& key,
74 const SSLServerConfig& ssl_server_config) { 81 const SSLServerConfig& ssl_server_config)
82 : ssl_ctx_(SSL_CTX_new(SSLv23_server_method())),
davidben 2016/01/22 23:57:48 Nit: It's the same thing, but this ought to be TLS
ryanchung 2016/01/29 23:28:15 Done.
83 ssl_server_config_(ssl_server_config),
84 cert_(certificate),
85 key_(key.Copy()) {
86 SSL_CTX_set_session_cache_mode(ssl_ctx_.get(), SSL_SESS_CACHE_SERVER);
87 int session_ctx_id = 0;
88 SSL_CTX_set_session_id_context(
89 ssl_ctx_.get(), (unsigned char*)&session_ctx_id, sizeof(session_ctx_id));
davidben 2016/01/22 23:57:48 unsigned char -> uint8_t. Also C++-style casts. Bu
ryanchung 2016/01/29 23:28:15 Done.
90
91 if (ssl_server_config_.require_client_cert) {
92 SSL_CTX_set_verify(ssl_ctx_.get(),
93 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
94 }
95 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(),
96 SSLServerSocketOpenSSL::CertVerifyCallback,
97 ssl_server_config_.client_cert_verifier);
98 CHECK(key_);
davidben 2016/01/22 23:57:48 This probably should be the first line.
ryanchung 2016/01/29 23:28:16 Done.
99 }
100
101 SSLServerSocketContextOpenSSL::~SSLServerSocketContextOpenSSL() {}
102
103 scoped_ptr<SSLServerSocket>
104 SSLServerSocketContextOpenSSL::CreateSSLServerSocket(
105 scoped_ptr<StreamSocket> socket) {
75 crypto::EnsureOpenSSLInit(); 106 crypto::EnsureOpenSSLInit();
107 SSL* ssl = SSL_new(ssl_ctx_.get());
76 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL( 108 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL(
77 std::move(socket), certificate, key, ssl_server_config)); 109 std::move(socket), cert_.get(), *key_, ssl_server_config_, ssl));
78 } 110 }
79 111
80 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL( 112 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL(
81 scoped_ptr<StreamSocket> transport_socket, 113 scoped_ptr<StreamSocket> transport_socket,
82 scoped_refptr<X509Certificate> certificate, 114 scoped_refptr<X509Certificate> certificate,
83 const crypto::RSAPrivateKey& key, 115 const crypto::RSAPrivateKey& key,
84 const SSLServerConfig& ssl_server_config) 116 const SSLServerConfig& ssl_server_config,
117 SSL* ssl)
85 : transport_send_busy_(false), 118 : transport_send_busy_(false),
86 transport_recv_busy_(false), 119 transport_recv_busy_(false),
87 transport_recv_eof_(false), 120 transport_recv_eof_(false),
88 user_read_buf_len_(0), 121 user_read_buf_len_(0),
89 user_write_buf_len_(0), 122 user_write_buf_len_(0),
90 transport_write_error_(OK), 123 transport_write_error_(OK),
91 ssl_(NULL), 124 ssl_(ssl),
92 transport_bio_(NULL), 125 transport_bio_(NULL),
93 transport_socket_(std::move(transport_socket)), 126 transport_socket_(std::move(transport_socket)),
94 ssl_server_config_(ssl_server_config), 127 ssl_server_config_(ssl_server_config),
95 cert_(certificate), 128 cert_(certificate),
96 key_(key.Copy()), 129 key_(key.Copy()),
97 next_handshake_state_(STATE_NONE), 130 next_handshake_state_(STATE_NONE),
98 completed_handshake_(false) { 131 completed_handshake_(false) {
99 CHECK(key_); 132 CHECK(key_);
100 } 133 }
101 134
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 void SSLServerSocketOpenSSL::DoWriteCallback(int rv) { 707 void SSLServerSocketOpenSSL::DoWriteCallback(int rv) {
675 DCHECK(rv != ERR_IO_PENDING); 708 DCHECK(rv != ERR_IO_PENDING);
676 DCHECK(!user_write_callback_.is_null()); 709 DCHECK(!user_write_callback_.is_null());
677 710
678 user_write_buf_ = NULL; 711 user_write_buf_ = NULL;
679 user_write_buf_len_ = 0; 712 user_write_buf_len_ = 0;
680 ResetAndReturn(&user_write_callback_).Run(rv); 713 ResetAndReturn(&user_write_callback_).Run(rv);
681 } 714 }
682 715
683 int SSLServerSocketOpenSSL::Init() { 716 int SSLServerSocketOpenSSL::Init() {
684 DCHECK(!ssl_);
685 DCHECK(!transport_bio_); 717 DCHECK(!transport_bio_);
686 718
687 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 719 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
688 720
689 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(SSLv23_server_method()));
690 if (ssl_server_config_.require_client_cert) {
691 SSL_CTX_set_verify(ssl_ctx.get(),
692 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
693 }
694 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), CertVerifyCallback,
695 ssl_server_config_.client_cert_verifier);
696 ssl_ = SSL_new(ssl_ctx.get());
697 if (!ssl_) 721 if (!ssl_)
698 return ERR_UNEXPECTED; 722 return ERR_UNEXPECTED;
699 723
700 BIO* ssl_bio = NULL; 724 BIO* ssl_bio = NULL;
701 // 0 => use default buffer sizes. 725 // 0 => use default buffer sizes.
702 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) 726 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0))
703 return ERR_UNEXPECTED; 727 return ERR_UNEXPECTED;
704 DCHECK(ssl_bio); 728 DCHECK(ssl_bio);
705 DCHECK(transport_bio_); 729 DCHECK(transport_bio_);
706 730
707 SSL_set_bio(ssl_, ssl_bio, ssl_bio); 731 SSL_set_bio(ssl_, ssl_bio, ssl_bio);
708 732
709 // Set certificate and private key. 733 // Set certificate and private key.
davidben 2016/01/22 23:57:48 Just about everything from hear onwards should be
ryanchung 2016/01/29 23:28:16 Done. I moved the configuration steps to the const
710 DCHECK(cert_->os_cert_handle()); 734 DCHECK(cert_->os_cert_handle());
711 #if defined(USE_OPENSSL_CERTS) 735 #if defined(USE_OPENSSL_CERTS)
712 if (SSL_use_certificate(ssl_, cert_->os_cert_handle()) != 1) { 736 if (SSL_use_certificate(ssl_, cert_->os_cert_handle()) != 1) {
713 LOG(ERROR) << "Cannot set certificate."; 737 LOG(ERROR) << "Cannot set certificate.";
714 return ERR_UNEXPECTED; 738 return ERR_UNEXPECTED;
715 } 739 }
716 #else 740 #else
717 // Convert OSCertHandle to X509 structure. 741 // Convert OSCertHandle to X509 structure.
718 std::string der_string; 742 std::string der_string;
719 if (!X509Certificate::GetDEREncoded(cert_->os_cert_handle(), &der_string)) 743 if (!X509Certificate::GetDEREncoded(cert_->os_cert_handle(), &der_string))
(...skipping 21 matching lines...) Expand all
741 765
742 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_min); 766 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_min);
743 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_max); 767 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_max);
744 SSL_set_min_version(ssl_, ssl_server_config_.version_min); 768 SSL_set_min_version(ssl_, ssl_server_config_.version_min);
745 SSL_set_max_version(ssl_, ssl_server_config_.version_max); 769 SSL_set_max_version(ssl_, ssl_server_config_.version_max);
746 770
747 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, 771 // OpenSSL defaults some options to on, others to off. To avoid ambiguity,
748 // set everything we care about to an absolute value. 772 // set everything we care about to an absolute value.
749 SslSetClearMask options; 773 SslSetClearMask options;
750 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); 774 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true);
775 options.ConfigureFlag(SSL_OP_NO_TICKET, true);
davidben 2016/01/22 23:57:48 Why turn off tickets? I can't think of any reason
ryanchung 2016/01/29 23:28:15 Done. Should not be turned off. Line removed.
751 776
752 SSL_set_options(ssl_, options.set_mask); 777 SSL_set_options(ssl_, options.set_mask);
753 SSL_clear_options(ssl_, options.clear_mask); 778 SSL_clear_options(ssl_, options.clear_mask);
754 779
755 // Same as above, this time for the SSL mode. 780 // Same as above, this time for the SSL mode.
756 SslSetClearMask mode; 781 SslSetClearMask mode;
757 782
758 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); 783 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true);
759 784
760 SSL_set_mode(ssl_, mode.set_mask); 785 SSL_set_mode(ssl_, mode.set_mask);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 int res = verifier->Verify(client_cert.get(), 843 int res = verifier->Verify(client_cert.get(),
819 base::Bind(&DoNothingOnCompletion), &ignore_async); 844 base::Bind(&DoNothingOnCompletion), &ignore_async);
820 if (res != OK) { 845 if (res != OK) {
821 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED); 846 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED);
822 return 0; 847 return 0;
823 } 848 }
824 return 1; 849 return 1;
825 } 850 }
826 851
827 } // namespace net 852 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698