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

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: Rebased to head Created 4 years, 10 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 // Creates an X509Certificate out of the concatenation of |cert|, if non-null, 31 // Creates an X509Certificate out of the concatenation of |cert|, if non-null,
(...skipping 12 matching lines...) Expand all
45 for (size_t i = 0; i < sk_X509_num(chain); ++i) { 44 for (size_t i = 0; i < sk_X509_num(chain); ++i) {
46 X509* x = sk_X509_value(chain, i); 45 X509* x = sk_X509_value(chain, i);
47 if (!x509_util::GetDER(x, &der_cert)) 46 if (!x509_util::GetDER(x, &der_cert))
48 return nullptr; 47 return nullptr;
49 der_chain.push_back(der_cert); 48 der_chain.push_back(der_cert);
50 } 49 }
51 50
52 return X509Certificate::CreateFromDERCertChain(der_chain); 51 return X509Certificate::CreateFromDERCertChain(der_chain);
53 } 52 }
54 53
55 } // namespace 54 class SSLServerSocketOpenSSL : public SSLServerSocket {
55 public:
56 // See comments on CreateSSLServerSocket for details of how these
57 // parameters are used.
58 SSLServerSocketOpenSSL(scoped_ptr<StreamSocket> socket,
59 X509Certificate* certificate,
60 const crypto::RSAPrivateKey& key,
61 const SSLServerConfig& ssl_server_config,
62 SSL* ssl);
63 ~SSLServerSocketOpenSSL() override;
56 64
57 void EnableSSLServerSockets() { 65 // SSLServerSocket interface.
58 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit(). 66 int Handshake(const CompletionCallback& callback) override;
59 }
60 67
61 scoped_ptr<SSLServerSocket> CreateSSLServerSocket( 68 // SSLSocket interface.
62 scoped_ptr<StreamSocket> socket, 69 int ExportKeyingMaterial(const base::StringPiece& label,
63 X509Certificate* certificate, 70 bool has_context,
64 const crypto::RSAPrivateKey& key, 71 const base::StringPiece& context,
65 const SSLServerConfig& ssl_server_config) { 72 unsigned char* out,
66 crypto::EnsureOpenSSLInit(); 73 unsigned int outlen) override;
67 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL( 74 int GetTLSUniqueChannelBinding(std::string* out) override;
68 std::move(socket), certificate, key, ssl_server_config)); 75
69 } 76 // Socket interface (via StreamSocket).
77 int Read(IOBuffer* buf,
78 int buf_len,
79 const CompletionCallback& callback) override;
80 int Write(IOBuffer* buf,
81 int buf_len,
82 const CompletionCallback& callback) override;
83 int SetReceiveBufferSize(int32_t size) override;
84 int SetSendBufferSize(int32_t size) override;
85
86 // StreamSocket implementation.
87 int Connect(const CompletionCallback& callback) override;
88 void Disconnect() override;
89 bool IsConnected() const override;
90 bool IsConnectedAndIdle() const override;
91 int GetPeerAddress(IPEndPoint* address) const override;
92 int GetLocalAddress(IPEndPoint* address) const override;
93 const BoundNetLog& NetLog() const override;
94 void SetSubresourceSpeculation() override;
95 void SetOmniboxSpeculation() override;
96 bool WasEverUsed() const override;
97 bool UsingTCPFastOpen() const override;
98 bool WasNpnNegotiated() const override;
99 NextProto GetNegotiatedProtocol() const override;
100 bool GetSSLInfo(SSLInfo* ssl_info) override;
101 void GetConnectionAttempts(ConnectionAttempts* out) const override;
102 void ClearConnectionAttempts() override {}
103 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
104 int64_t GetTotalReceivedBytes() const override;
105 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg);
106
107 private:
108 enum State {
109 STATE_NONE,
110 STATE_HANDSHAKE,
111 };
112
113 void OnSendComplete(int result);
114 void OnRecvComplete(int result);
115 void OnHandshakeIOComplete(int result);
116
117 int BufferSend();
118 void BufferSendComplete(int result);
119 void TransportWriteComplete(int result);
120 int BufferRecv();
121 void BufferRecvComplete(int result);
122 int TransportReadComplete(int result);
123 bool DoTransportIO();
124 int DoPayloadRead();
125 int DoPayloadWrite();
126
127 int DoHandshakeLoop(int last_io_result);
128 int DoReadLoop(int result);
129 int DoWriteLoop(int result);
130 int DoHandshake();
131 void DoHandshakeCallback(int result);
132 void DoReadCallback(int result);
133 void DoWriteCallback(int result);
134
135 int Init();
136 void ExtractClientCert();
137
138 // Members used to send and receive buffer.
139 bool transport_send_busy_;
140 bool transport_recv_busy_;
141 bool transport_recv_eof_;
142
143 scoped_refptr<DrainableIOBuffer> send_buffer_;
144 scoped_refptr<IOBuffer> recv_buffer_;
145
146 BoundNetLog net_log_;
147
148 CompletionCallback user_handshake_callback_;
149 CompletionCallback user_read_callback_;
150 CompletionCallback user_write_callback_;
151
152 // Used by Read function.
153 scoped_refptr<IOBuffer> user_read_buf_;
154 int user_read_buf_len_;
155
156 // Used by Write function.
157 scoped_refptr<IOBuffer> user_write_buf_;
158 int user_write_buf_len_;
159
160 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
161 // error writing to the transport socket. A value of OK indicates no error.
162 int transport_write_error_;
163
164 // OpenSSL stuff
165 SSL* ssl_;
166 BIO* transport_bio_;
167
168 // StreamSocket for sending and receiving data.
169 scoped_ptr<StreamSocket> transport_socket_;
170
171 // Options for the SSL socket.
172 SSLServerConfig ssl_server_config_;
davidben 2016/02/24 21:01:25 I'm not sure this is actually used.
ryanchung 2016/02/25 00:46:39 Done.
173
174 // Certificate for the server.
175 scoped_refptr<X509Certificate> cert_;
davidben 2016/02/24 21:01:26 Ditto.
ryanchung 2016/02/25 00:46:39 Done.
176
177 // Private key used by the server.
178 scoped_ptr<crypto::RSAPrivateKey> key_;
davidben 2016/02/24 21:01:26 Ditto.
ryanchung 2016/02/25 00:46:39 Done.
179
180 // Certificate for the client.
181 scoped_refptr<X509Certificate> client_cert_;
182
183 State next_handshake_state_;
184 bool completed_handshake_;
185
186 DISALLOW_COPY_AND_ASSIGN(SSLServerSocketOpenSSL);
187 };
70 188
71 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL( 189 SSLServerSocketOpenSSL::SSLServerSocketOpenSSL(
72 scoped_ptr<StreamSocket> transport_socket, 190 scoped_ptr<StreamSocket> transport_socket,
73 scoped_refptr<X509Certificate> certificate, 191 X509Certificate* certificate,
74 const crypto::RSAPrivateKey& key, 192 const crypto::RSAPrivateKey& key,
75 const SSLServerConfig& ssl_server_config) 193 const SSLServerConfig& ssl_server_config,
194 SSL* ssl)
76 : transport_send_busy_(false), 195 : transport_send_busy_(false),
77 transport_recv_busy_(false), 196 transport_recv_busy_(false),
78 transport_recv_eof_(false), 197 transport_recv_eof_(false),
79 user_read_buf_len_(0), 198 user_read_buf_len_(0),
80 user_write_buf_len_(0), 199 user_write_buf_len_(0),
81 transport_write_error_(OK), 200 transport_write_error_(OK),
82 ssl_(NULL), 201 ssl_(ssl),
83 transport_bio_(NULL), 202 transport_bio_(NULL),
84 transport_socket_(std::move(transport_socket)), 203 transport_socket_(std::move(transport_socket)),
85 ssl_server_config_(ssl_server_config), 204 ssl_server_config_(ssl_server_config),
86 cert_(certificate), 205 cert_(certificate),
87 key_(key.Copy()), 206 key_(key.Copy()),
88 next_handshake_state_(STATE_NONE), 207 next_handshake_state_(STATE_NONE),
89 completed_handshake_(false) { 208 completed_handshake_(false) {
90 CHECK(key_); 209 CHECK(key_);
91 } 210 }
92 211
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 return MapOpenSSLError(ssl_error, err_tracer); 272 return MapOpenSSLError(ssl_error, err_tracer);
154 } 273 }
155 return OK; 274 return OK;
156 } 275 }
157 276
158 int SSLServerSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { 277 int SSLServerSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) {
159 NOTIMPLEMENTED(); 278 NOTIMPLEMENTED();
160 return ERR_NOT_IMPLEMENTED; 279 return ERR_NOT_IMPLEMENTED;
161 } 280 }
162 281
163 int SSLServerSocketOpenSSL::Read(IOBuffer* buf, int buf_len, 282 int SSLServerSocketOpenSSL::Read(IOBuffer* buf,
283 int buf_len,
164 const CompletionCallback& callback) { 284 const CompletionCallback& callback) {
165 DCHECK(user_read_callback_.is_null()); 285 DCHECK(user_read_callback_.is_null());
166 DCHECK(user_handshake_callback_.is_null()); 286 DCHECK(user_handshake_callback_.is_null());
167 DCHECK(!user_read_buf_); 287 DCHECK(!user_read_buf_);
168 DCHECK(!callback.is_null()); 288 DCHECK(!callback.is_null());
169 289
170 user_read_buf_ = buf; 290 user_read_buf_ = buf;
171 user_read_buf_len_ = buf_len; 291 user_read_buf_len_ = buf_len;
172 292
173 DCHECK(completed_handshake_); 293 DCHECK(completed_handshake_);
174 294
175 int rv = DoReadLoop(OK); 295 int rv = DoReadLoop(OK);
176 296
177 if (rv == ERR_IO_PENDING) { 297 if (rv == ERR_IO_PENDING) {
178 user_read_callback_ = callback; 298 user_read_callback_ = callback;
179 } else { 299 } else {
180 user_read_buf_ = NULL; 300 user_read_buf_ = NULL;
181 user_read_buf_len_ = 0; 301 user_read_buf_len_ = 0;
182 } 302 }
183 303
184 return rv; 304 return rv;
185 } 305 }
186 306
187 int SSLServerSocketOpenSSL::Write(IOBuffer* buf, int buf_len, 307 int SSLServerSocketOpenSSL::Write(IOBuffer* buf,
308 int buf_len,
188 const CompletionCallback& callback) { 309 const CompletionCallback& callback) {
189 DCHECK(user_write_callback_.is_null()); 310 DCHECK(user_write_callback_.is_null());
190 DCHECK(!user_write_buf_); 311 DCHECK(!user_write_buf_);
191 DCHECK(!callback.is_null()); 312 DCHECK(!callback.is_null());
192 313
193 user_write_buf_ = buf; 314 user_write_buf_ = buf;
194 user_write_buf_len_ = buf_len; 315 user_write_buf_len_ = buf_len;
195 316
196 int rv = DoWriteLoop(OK); 317 int rv = DoWriteLoop(OK);
197 318
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 size_t max_read = BIO_pending(transport_bio_); 491 size_t max_read = BIO_pending(transport_bio_);
371 if (!max_read) 492 if (!max_read)
372 return 0; // Nothing pending in the OpenSSL write BIO. 493 return 0; // Nothing pending in the OpenSSL write BIO.
373 send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); 494 send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read);
374 int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); 495 int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read);
375 DCHECK_GT(read_bytes, 0); 496 DCHECK_GT(read_bytes, 0);
376 CHECK_EQ(static_cast<int>(max_read), read_bytes); 497 CHECK_EQ(static_cast<int>(max_read), read_bytes);
377 } 498 }
378 499
379 int rv = transport_socket_->Write( 500 int rv = transport_socket_->Write(
380 send_buffer_.get(), 501 send_buffer_.get(), send_buffer_->BytesRemaining(),
381 send_buffer_->BytesRemaining(),
382 base::Bind(&SSLServerSocketOpenSSL::BufferSendComplete, 502 base::Bind(&SSLServerSocketOpenSSL::BufferSendComplete,
383 base::Unretained(this))); 503 base::Unretained(this)));
384 if (rv == ERR_IO_PENDING) { 504 if (rv == ERR_IO_PENDING) {
385 transport_send_busy_ = true; 505 transport_send_busy_ = true;
386 } else { 506 } else {
387 TransportWriteComplete(rv); 507 TransportWriteComplete(rv);
388 } 508 }
389 return rv; 509 return rv;
390 } 510 }
391 511
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 // (and constantly allocating new IOBuffers), a single Read() request to 563 // (and constantly allocating new IOBuffers), a single Read() request to
444 // fill |transport_bio_| is issued. As long as an SSL client socket cannot 564 // fill |transport_bio_| is issued. As long as an SSL client socket cannot
445 // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL 565 // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL
446 // traffic, this over-subscribed Read()ing will not cause issues. 566 // traffic, this over-subscribed Read()ing will not cause issues.
447 size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_); 567 size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_);
448 if (!max_write) 568 if (!max_write)
449 return ERR_IO_PENDING; 569 return ERR_IO_PENDING;
450 570
451 recv_buffer_ = new IOBuffer(max_write); 571 recv_buffer_ = new IOBuffer(max_write);
452 int rv = transport_socket_->Read( 572 int rv = transport_socket_->Read(
453 recv_buffer_.get(), 573 recv_buffer_.get(), max_write,
454 max_write,
455 base::Bind(&SSLServerSocketOpenSSL::BufferRecvComplete, 574 base::Bind(&SSLServerSocketOpenSSL::BufferRecvComplete,
456 base::Unretained(this))); 575 base::Unretained(this)));
457 if (rv == ERR_IO_PENDING) { 576 if (rv == ERR_IO_PENDING) {
458 transport_recv_busy_ = true; 577 transport_recv_busy_ = true;
459 } else { 578 } else {
460 rv = TransportReadComplete(rv); 579 rv = TransportReadComplete(rv);
461 } 580 }
462 return rv; 581 return rv;
463 } 582 }
464 583
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 void SSLServerSocketOpenSSL::DoWriteCallback(int rv) { 793 void SSLServerSocketOpenSSL::DoWriteCallback(int rv) {
675 DCHECK(rv != ERR_IO_PENDING); 794 DCHECK(rv != ERR_IO_PENDING);
676 DCHECK(!user_write_callback_.is_null()); 795 DCHECK(!user_write_callback_.is_null());
677 796
678 user_write_buf_ = NULL; 797 user_write_buf_ = NULL;
679 user_write_buf_len_ = 0; 798 user_write_buf_len_ = 0;
680 base::ResetAndReturn(&user_write_callback_).Run(rv); 799 base::ResetAndReturn(&user_write_callback_).Run(rv);
681 } 800 }
682 801
683 int SSLServerSocketOpenSSL::Init() { 802 int SSLServerSocketOpenSSL::Init() {
684 DCHECK(!ssl_);
685 DCHECK(!transport_bio_); 803 DCHECK(!transport_bio_);
686 804
687 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 805 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
688 806
689 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(TLS_method()));
690 int verify_mode = 0;
691 switch (ssl_server_config_.client_cert_type) {
692 case SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT:
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 }
704 ssl_ = SSL_new(ssl_ctx.get());
705 if (!ssl_) 807 if (!ssl_)
706 return ERR_UNEXPECTED; 808 return ERR_UNEXPECTED;
707 809
708 BIO* ssl_bio = NULL; 810 BIO* ssl_bio = NULL;
709 // 0 => use default buffer sizes. 811 // 0 => use default buffer sizes.
710 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) 812 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0))
711 return ERR_UNEXPECTED; 813 return ERR_UNEXPECTED;
712 DCHECK(ssl_bio); 814 DCHECK(ssl_bio);
713 DCHECK(transport_bio_); 815 DCHECK(transport_bio_);
714 816
715 SSL_set_bio(ssl_, ssl_bio, ssl_bio); 817 SSL_set_bio(ssl_, ssl_bio, ssl_bio);
716 818
819 return OK;
820 }
821
822 // static
823 int SSLServerSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx,
824 void* arg) {
825 ClientCertVerifier* verifier = reinterpret_cast<ClientCertVerifier*>(arg);
826 // If a verifier was not supplied, all certificates are accepted.
827 if (!verifier)
828 return 1;
829 STACK_OF(X509)* chain = store_ctx->untrusted;
830 scoped_refptr<X509Certificate> client_cert(
831 CreateX509Certificate(nullptr, chain));
832 if (!client_cert.get()) {
833 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED);
834 return 0;
835 }
836 // Asynchronous completion of Verify is currently not supported.
837 // http://crbug.com/347402
838 // The API for Verify supports the parts needed for async completion
839 // but is currently expected to complete synchronously.
840 scoped_ptr<ClientCertVerifier::Request> ignore_async;
841 int res =
842 verifier->Verify(client_cert.get(), CompletionCallback(), &ignore_async);
843 DCHECK_NE(res, ERR_IO_PENDING);
844
845 if (res != OK) {
846 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED);
847 return 0;
848 }
849 return 1;
850 }
851
852 } // namespace
853
854 scoped_ptr<SSLServerContext> CreateSSLServerContext(
855 X509Certificate* certificate,
856 const crypto::RSAPrivateKey& key,
857 const SSLServerConfig& ssl_server_config) {
858 crypto::EnsureOpenSSLInit();
859 return scoped_ptr<SSLServerContext>(
860 new SSLServerContextOpenSSL(certificate, key, ssl_server_config));
861 }
862
863 SSLServerContextOpenSSL::SSLServerContextOpenSSL(
864 X509Certificate* certificate,
865 const crypto::RSAPrivateKey& key,
866 const SSLServerConfig& ssl_server_config)
867 : ssl_ctx_(SSL_CTX_new(TLS_method())),
868 ssl_server_config_(ssl_server_config),
869 cert_(certificate),
870 key_(key.Copy()) {
871 CHECK(key_);
872 SSL_CTX_set_session_cache_mode(ssl_ctx_.get(), SSL_SESS_CACHE_SERVER);
873 uint8_t session_ctx_id = 0;
874 SSL_CTX_set_session_id_context(ssl_ctx_.get(), &session_ctx_id,
875 sizeof(session_ctx_id));
876
877 int verify_mode = 0;
878 switch (ssl_server_config_.client_cert_type) {
879 case SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT:
880 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
881 // Fall-through
882 case SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT:
883 verify_mode |= SSL_VERIFY_PEER;
884 SSL_CTX_set_verify(ssl_ctx_.get(), verify_mode, nullptr);
885 SSL_CTX_set_cert_verify_callback(
886 ssl_ctx_.get(), SSLServerSocketOpenSSL::CertVerifyCallback,
887 ssl_server_config_.client_cert_verifier);
888 break;
889 case SSLServerConfig::ClientCertType::NO_CLIENT_CERT:
890 break;
891 }
892
717 // Set certificate and private key. 893 // Set certificate and private key.
718 DCHECK(cert_->os_cert_handle()); 894 DCHECK(cert_->os_cert_handle());
719 #if defined(USE_OPENSSL_CERTS) 895 #if defined(USE_OPENSSL_CERTS)
720 if (SSL_use_certificate(ssl_, cert_->os_cert_handle()) != 1) { 896 CHECK(SSL_CTX_use_certificate(ssl_ctx_.get(), cert_->os_cert_handle()) == 1);
davidben 2016/02/24 21:01:26 Nit: The == 1 is a remnant of OpenSSL returning -1
ryanchung 2016/02/25 00:46:39 Done.
721 LOG(ERROR) << "Cannot set certificate.";
722 return ERR_UNEXPECTED;
723 }
724 #else 897 #else
725 // Convert OSCertHandle to X509 structure. 898 // Convert OSCertHandle to X509 structure.
726 std::string der_string; 899 std::string der_string;
727 if (!X509Certificate::GetDEREncoded(cert_->os_cert_handle(), &der_string)) 900 CHECK(X509Certificate::GetDEREncoded(cert_->os_cert_handle(), &der_string));
728 return ERR_UNEXPECTED;
729 901
730 const unsigned char* der_string_array = 902 const unsigned char* der_string_array =
731 reinterpret_cast<const unsigned char*>(der_string.data()); 903 reinterpret_cast<const unsigned char*>(der_string.data());
732 904
733 ScopedX509 x509(d2i_X509(NULL, &der_string_array, der_string.length())); 905 ScopedX509 x509(d2i_X509(NULL, &der_string_array, der_string.length()));
734 if (!x509) 906 CHECK(x509);
735 return ERR_UNEXPECTED;
736 907
737 // On success, SSL_use_certificate acquires a reference to |x509|. 908 // On success, SSL_CTX_use_certificate acquires a reference to |x509|.
738 if (SSL_use_certificate(ssl_, x509.get()) != 1) { 909 CHECK(SSL_CTX_use_certificate(ssl_ctx_.get(), x509.get()) == 1);
davidben 2016/02/24 21:01:25 Nit: ditto
ryanchung 2016/02/25 00:46:38 Done.
739 LOG(ERROR) << "Cannot set certificate.";
740 return ERR_UNEXPECTED;
741 }
742 #endif // USE_OPENSSL_CERTS 910 #endif // USE_OPENSSL_CERTS
743 911
744 DCHECK(key_->key()); 912 DCHECK(key_->key());
745 if (SSL_use_PrivateKey(ssl_, key_->key()) != 1) { 913 CHECK(SSL_CTX_use_PrivateKey(ssl_ctx_.get(), key_->key()) == 1);
davidben 2016/02/24 21:01:26 Nit: ditto
ryanchung 2016/02/25 00:46:39 Done.
746 LOG(ERROR) << "Cannot set private key.";
747 return ERR_UNEXPECTED;
748 }
749 914
750 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_min); 915 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_min);
751 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_max); 916 DCHECK_LT(SSL3_VERSION, ssl_server_config_.version_max);
752 SSL_set_min_version(ssl_, ssl_server_config_.version_min); 917 SSL_CTX_set_min_version(ssl_ctx_.get(), ssl_server_config_.version_min);
753 SSL_set_max_version(ssl_, ssl_server_config_.version_max); 918 SSL_CTX_set_max_version(ssl_ctx_.get(), ssl_server_config_.version_max);
754 919
755 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, 920 // OpenSSL defaults some options to on, others to off. To avoid ambiguity,
756 // set everything we care about to an absolute value. 921 // set everything we care about to an absolute value.
757 SslSetClearMask options; 922 SslSetClearMask options;
758 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); 923 options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true);
759 924
760 SSL_set_options(ssl_, options.set_mask); 925 SSL_CTX_set_options(ssl_ctx_.get(), options.set_mask);
761 SSL_clear_options(ssl_, options.clear_mask); 926 SSL_CTX_clear_options(ssl_ctx_.get(), options.clear_mask);
762 927
763 // Same as above, this time for the SSL mode. 928 // Same as above, this time for the SSL mode.
764 SslSetClearMask mode; 929 SslSetClearMask mode;
765 930
766 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); 931 mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true);
767 932
768 SSL_set_mode(ssl_, mode.set_mask); 933 SSL_CTX_set_mode(ssl_ctx_.get(), mode.set_mask);
769 SSL_clear_mode(ssl_, mode.clear_mask); 934 SSL_CTX_clear_mode(ssl_ctx_.get(), mode.clear_mask);
770 935
771 // See SSLServerConfig::disabled_cipher_suites for description of the suites 936 // See SSLServerConfig::disabled_cipher_suites for description of the suites
772 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 937 // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256
773 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 938 // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384
774 // as the handshake hash. 939 // as the handshake hash.
775 std::string command("DEFAULT:!SHA256:!SHA384:!AESGCM+AES256:!aPSK"); 940 std::string command("DEFAULT:!SHA256:!SHA384:!AESGCM+AES256:!aPSK");
776 941
777 if (ssl_server_config_.require_ecdhe) 942 if (ssl_server_config_.require_ecdhe)
778 command.append(":!kRSA:!kDHE"); 943 command.append(":!kRSA:!kDHE");
779 944
780 // Remove any disabled ciphers. 945 // Remove any disabled ciphers.
781 for (uint16_t id : ssl_server_config_.disabled_cipher_suites) { 946 for (uint16_t id : ssl_server_config_.disabled_cipher_suites) {
782 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); 947 const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id);
783 if (cipher) { 948 if (cipher) {
784 command.append(":!"); 949 command.append(":!");
785 command.append(SSL_CIPHER_get_name(cipher)); 950 command.append(SSL_CIPHER_get_name(cipher));
786 } 951 }
787 } 952 }
788 953
789 int rv = SSL_set_cipher_list(ssl_, command.c_str()); 954 int rv = SSL_CTX_set_cipher_list(ssl_ctx_.get(), command.c_str());
790 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. 955 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL.
791 // This will almost certainly result in the socket failing to complete the 956 // This will almost certainly result in the socket failing to complete the
792 // handshake at which point the appropriate error is bubbled up to the client. 957 // handshake at which point the appropriate error is bubbled up to the client.
793 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command 958 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command
794 << "') returned " << rv; 959 << "') returned " << rv;
795 960
796 if (ssl_server_config_.client_cert_type != 961 if (ssl_server_config_.client_cert_type !=
797 SSLServerConfig::ClientCertType::NO_CLIENT_CERT && 962 SSLServerConfig::ClientCertType::NO_CLIENT_CERT &&
798 !ssl_server_config_.cert_authorities_.empty()) { 963 !ssl_server_config_.cert_authorities_.empty()) {
799 ScopedX509NameStack stack(sk_X509_NAME_new_null()); 964 ScopedX509NameStack stack(sk_X509_NAME_new_null());
800 for (const auto& authority : ssl_server_config_.cert_authorities_) { 965 for (const auto& authority : ssl_server_config_.cert_authorities_) {
801 const uint8_t* name = reinterpret_cast<const uint8_t*>(authority.c_str()); 966 const uint8_t* name = reinterpret_cast<const uint8_t*>(authority.c_str());
802 const uint8_t* name_start = name; 967 const uint8_t* name_start = name;
803 ScopedX509_NAME subj(d2i_X509_NAME(nullptr, &name, authority.length())); 968 ScopedX509_NAME subj(d2i_X509_NAME(nullptr, &name, authority.length()));
804 if (!subj || name != name_start + authority.length()) 969 CHECK(subj && name == name_start + authority.length());
805 return ERR_UNEXPECTED;
806 sk_X509_NAME_push(stack.get(), subj.release()); 970 sk_X509_NAME_push(stack.get(), subj.release());
807 } 971 }
808 SSL_set_client_CA_list(ssl_, stack.release()); 972 SSL_CTX_set_client_CA_list(ssl_ctx_.get(), stack.release());
809 } 973 }
810
811 return OK;
812 } 974 }
813 975
814 // static 976 SSLServerContextOpenSSL::~SSLServerContextOpenSSL() {}
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 977
837 if (res != OK) { 978 scoped_ptr<SSLServerSocket> SSLServerContextOpenSSL::CreateSSLServerSocket(
838 X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_CERT_REJECTED); 979 scoped_ptr<StreamSocket> socket) {
839 return 0; 980 crypto::EnsureOpenSSLInit();
davidben 2016/02/24 21:01:25 This needs to happen before the SSL_CTX_new call.
ryanchung 2016/02/25 00:46:39 Done. Or alternately, can we just call EnsureOpenS
840 } 981 SSL* ssl = SSL_new(ssl_ctx_.get());
841 return 1; 982 return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL(
983 std::move(socket), cert_.get(), *key_, ssl_server_config_, ssl));
davidben 2016/02/24 21:01:25 I think this comment is moot because we can just g
ryanchung 2016/02/25 00:46:39 Thanks!
984 }
985
986 void EnableSSLServerSockets() {
987 // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit().
842 } 988 }
843 989
844 } // namespace net 990 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698