[email protected] | a4965c88 | 2012-06-13 20:19:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 5 | #include "net/socket/ssl_server_socket_openssl.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 6 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 7 | #include <openssl/err.h> |
| 8 | #include <openssl/ssl.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 9 | #include <utility> |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 10 | |
| 11 | #include "base/callback_helpers.h" |
| 12 | #include "base/logging.h" |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 14 | #include "crypto/openssl_util.h" |
| 15 | #include "crypto/rsa_private_key.h" |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 16 | #include "crypto/scoped_openssl_types.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 18 | #include "net/ssl/openssl_ssl_util.h" |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 19 | #include "net/ssl/scoped_openssl_types.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 20 | |
| 21 | #define GotoState(s) next_handshake_state_ = s |
[email protected] | a4965c88 | 2012-06-13 20:19:44 | [diff] [blame] | 22 | |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 23 | namespace net { |
| 24 | |
[email protected] | a4965c88 | 2012-06-13 20:19:44 | [diff] [blame] | 25 | void EnableSSLServerSockets() { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 26 | // No-op because CreateSSLServerSocket() calls crypto::EnsureOpenSSLInit(). |
[email protected] | a4965c88 | 2012-06-13 20:19:44 | [diff] [blame] | 27 | } |
| 28 | |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 29 | scoped_ptr<SSLServerSocket> CreateSSLServerSocket( |
| 30 | scoped_ptr<StreamSocket> socket, |
| 31 | X509Certificate* certificate, |
| 32 | crypto::RSAPrivateKey* key, |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 33 | const SSLServerConfig& ssl_config) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 34 | crypto::EnsureOpenSSLInit(); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 35 | return scoped_ptr<SSLServerSocket>(new SSLServerSocketOpenSSL( |
| 36 | std::move(socket), certificate, key, ssl_config)); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | SSLServerSocketOpenSSL::SSLServerSocketOpenSSL( |
| 40 | scoped_ptr<StreamSocket> transport_socket, |
| 41 | scoped_refptr<X509Certificate> certificate, |
| 42 | crypto::RSAPrivateKey* key, |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 43 | const SSLServerConfig& ssl_config) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 44 | : transport_send_busy_(false), |
| 45 | transport_recv_busy_(false), |
| 46 | transport_recv_eof_(false), |
| 47 | user_read_buf_len_(0), |
| 48 | user_write_buf_len_(0), |
| 49 | transport_write_error_(OK), |
| 50 | ssl_(NULL), |
| 51 | transport_bio_(NULL), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 52 | transport_socket_(std::move(transport_socket)), |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 53 | ssl_config_(ssl_config), |
| 54 | cert_(certificate), |
| 55 | next_handshake_state_(STATE_NONE), |
| 56 | completed_handshake_(false) { |
| 57 | // TODO(byungchul): Need a better way to clone a key. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 58 | std::vector<uint8_t> key_bytes; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 59 | CHECK(key->ExportPrivateKey(&key_bytes)); |
| 60 | key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes)); |
| 61 | CHECK(key_.get()); |
| 62 | } |
| 63 | |
| 64 | SSLServerSocketOpenSSL::~SSLServerSocketOpenSSL() { |
| 65 | if (ssl_) { |
| 66 | // Calling SSL_shutdown prevents the session from being marked as |
| 67 | // unresumable. |
| 68 | SSL_shutdown(ssl_); |
| 69 | SSL_free(ssl_); |
| 70 | ssl_ = NULL; |
| 71 | } |
| 72 | if (transport_bio_) { |
| 73 | BIO_free_all(transport_bio_); |
| 74 | transport_bio_ = NULL; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int SSLServerSocketOpenSSL::Handshake(const CompletionCallback& callback) { |
| 79 | net_log_.BeginEvent(NetLog::TYPE_SSL_SERVER_HANDSHAKE); |
| 80 | |
| 81 | // Set up new ssl object. |
| 82 | int rv = Init(); |
| 83 | if (rv != OK) { |
| 84 | LOG(ERROR) << "Failed to initialize OpenSSL: rv=" << rv; |
| 85 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_SERVER_HANDSHAKE, rv); |
| 86 | return rv; |
| 87 | } |
| 88 | |
| 89 | // Set SSL to server mode. Handshake happens in the loop below. |
| 90 | SSL_set_accept_state(ssl_); |
| 91 | |
| 92 | GotoState(STATE_HANDSHAKE); |
| 93 | rv = DoHandshakeLoop(OK); |
| 94 | if (rv == ERR_IO_PENDING) { |
| 95 | user_handshake_callback_ = callback; |
| 96 | } else { |
| 97 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_SERVER_HANDSHAKE, rv); |
| 98 | } |
| 99 | |
| 100 | return rv > OK ? OK : rv; |
| 101 | } |
| 102 | |
| 103 | int SSLServerSocketOpenSSL::ExportKeyingMaterial( |
| 104 | const base::StringPiece& label, |
| 105 | bool has_context, |
| 106 | const base::StringPiece& context, |
| 107 | unsigned char* out, |
| 108 | unsigned int outlen) { |
| 109 | if (!IsConnected()) |
| 110 | return ERR_SOCKET_NOT_CONNECTED; |
| 111 | |
| 112 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 113 | |
| 114 | int rv = SSL_export_keying_material( |
| 115 | ssl_, out, outlen, label.data(), label.size(), |
| 116 | reinterpret_cast<const unsigned char*>(context.data()), |
| 117 | context.length(), context.length() > 0); |
| 118 | |
| 119 | if (rv != 1) { |
| 120 | int ssl_error = SSL_get_error(ssl_, rv); |
| 121 | LOG(ERROR) << "Failed to export keying material;" |
| 122 | << " returned " << rv |
| 123 | << ", SSL error code " << ssl_error; |
| 124 | return MapOpenSSLError(ssl_error, err_tracer); |
| 125 | } |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | int SSLServerSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { |
[email protected] | a7ac3c3 | 2011-06-17 19:10:15 | [diff] [blame] | 130 | NOTIMPLEMENTED(); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 131 | return ERR_NOT_IMPLEMENTED; |
| 132 | } |
| 133 | |
| 134 | int SSLServerSocketOpenSSL::Read(IOBuffer* buf, int buf_len, |
| 135 | const CompletionCallback& callback) { |
| 136 | DCHECK(user_read_callback_.is_null()); |
| 137 | DCHECK(user_handshake_callback_.is_null()); |
| 138 | DCHECK(!user_read_buf_.get()); |
| 139 | DCHECK(!callback.is_null()); |
| 140 | |
| 141 | user_read_buf_ = buf; |
| 142 | user_read_buf_len_ = buf_len; |
| 143 | |
| 144 | DCHECK(completed_handshake_); |
| 145 | |
| 146 | int rv = DoReadLoop(OK); |
| 147 | |
| 148 | if (rv == ERR_IO_PENDING) { |
| 149 | user_read_callback_ = callback; |
| 150 | } else { |
| 151 | user_read_buf_ = NULL; |
| 152 | user_read_buf_len_ = 0; |
| 153 | } |
| 154 | |
| 155 | return rv; |
| 156 | } |
| 157 | |
| 158 | int SSLServerSocketOpenSSL::Write(IOBuffer* buf, int buf_len, |
| 159 | const CompletionCallback& callback) { |
| 160 | DCHECK(user_write_callback_.is_null()); |
| 161 | DCHECK(!user_write_buf_.get()); |
| 162 | DCHECK(!callback.is_null()); |
| 163 | |
| 164 | user_write_buf_ = buf; |
| 165 | user_write_buf_len_ = buf_len; |
| 166 | |
| 167 | int rv = DoWriteLoop(OK); |
| 168 | |
| 169 | if (rv == ERR_IO_PENDING) { |
| 170 | user_write_callback_ = callback; |
| 171 | } else { |
| 172 | user_write_buf_ = NULL; |
| 173 | user_write_buf_len_ = 0; |
| 174 | } |
| 175 | return rv; |
| 176 | } |
| 177 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 178 | int SSLServerSocketOpenSSL::SetReceiveBufferSize(int32_t size) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 179 | return transport_socket_->SetReceiveBufferSize(size); |
| 180 | } |
| 181 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 182 | int SSLServerSocketOpenSSL::SetSendBufferSize(int32_t size) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 183 | return transport_socket_->SetSendBufferSize(size); |
| 184 | } |
| 185 | |
| 186 | int SSLServerSocketOpenSSL::Connect(const CompletionCallback& callback) { |
| 187 | NOTIMPLEMENTED(); |
| 188 | return ERR_NOT_IMPLEMENTED; |
| 189 | } |
| 190 | |
| 191 | void SSLServerSocketOpenSSL::Disconnect() { |
| 192 | transport_socket_->Disconnect(); |
| 193 | } |
| 194 | |
| 195 | bool SSLServerSocketOpenSSL::IsConnected() const { |
| 196 | // TODO(wtc): Find out if we should check transport_socket_->IsConnected() |
| 197 | // as well. |
| 198 | return completed_handshake_; |
| 199 | } |
| 200 | |
| 201 | bool SSLServerSocketOpenSSL::IsConnectedAndIdle() const { |
| 202 | return completed_handshake_ && transport_socket_->IsConnectedAndIdle(); |
| 203 | } |
| 204 | |
| 205 | int SSLServerSocketOpenSSL::GetPeerAddress(IPEndPoint* address) const { |
| 206 | if (!IsConnected()) |
| 207 | return ERR_SOCKET_NOT_CONNECTED; |
| 208 | return transport_socket_->GetPeerAddress(address); |
| 209 | } |
| 210 | |
| 211 | int SSLServerSocketOpenSSL::GetLocalAddress(IPEndPoint* address) const { |
| 212 | if (!IsConnected()) |
| 213 | return ERR_SOCKET_NOT_CONNECTED; |
| 214 | return transport_socket_->GetLocalAddress(address); |
| 215 | } |
| 216 | |
| 217 | const BoundNetLog& SSLServerSocketOpenSSL::NetLog() const { |
| 218 | return net_log_; |
| 219 | } |
| 220 | |
| 221 | void SSLServerSocketOpenSSL::SetSubresourceSpeculation() { |
| 222 | transport_socket_->SetSubresourceSpeculation(); |
| 223 | } |
| 224 | |
| 225 | void SSLServerSocketOpenSSL::SetOmniboxSpeculation() { |
| 226 | transport_socket_->SetOmniboxSpeculation(); |
| 227 | } |
| 228 | |
| 229 | bool SSLServerSocketOpenSSL::WasEverUsed() const { |
| 230 | return transport_socket_->WasEverUsed(); |
| 231 | } |
| 232 | |
| 233 | bool SSLServerSocketOpenSSL::UsingTCPFastOpen() const { |
| 234 | return transport_socket_->UsingTCPFastOpen(); |
| 235 | } |
| 236 | |
| 237 | bool SSLServerSocketOpenSSL::WasNpnNegotiated() const { |
| 238 | NOTIMPLEMENTED(); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | NextProto SSLServerSocketOpenSSL::GetNegotiatedProtocol() const { |
| 243 | // NPN is not supported by this class. |
| 244 | return kProtoUnknown; |
| 245 | } |
| 246 | |
| 247 | bool SSLServerSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
| 248 | NOTIMPLEMENTED(); |
| 249 | return false; |
| 250 | } |
| 251 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 252 | void SSLServerSocketOpenSSL::GetConnectionAttempts( |
| 253 | ConnectionAttempts* out) const { |
| 254 | out->clear(); |
| 255 | } |
| 256 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 257 | int64_t SSLServerSocketOpenSSL::GetTotalReceivedBytes() const { |
| 258 | return transport_socket_->GetTotalReceivedBytes(); |
| 259 | } |
| 260 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 261 | void SSLServerSocketOpenSSL::OnSendComplete(int result) { |
| 262 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 263 | // In handshake phase. |
| 264 | OnHandshakeIOComplete(result); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // TODO(byungchul): This state machine is not correct. Copy the state machine |
| 269 | // of SSLClientSocketOpenSSL::OnSendComplete() which handles it better. |
| 270 | if (!completed_handshake_) |
| 271 | return; |
| 272 | |
| 273 | if (user_write_buf_.get()) { |
| 274 | int rv = DoWriteLoop(result); |
| 275 | if (rv != ERR_IO_PENDING) |
| 276 | DoWriteCallback(rv); |
| 277 | } else { |
| 278 | // Ensure that any queued ciphertext is flushed. |
| 279 | DoTransportIO(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void SSLServerSocketOpenSSL::OnRecvComplete(int result) { |
| 284 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 285 | // In handshake phase. |
| 286 | OnHandshakeIOComplete(result); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // Network layer received some data, check if client requested to read |
| 291 | // decrypted data. |
| 292 | if (!user_read_buf_.get() || !completed_handshake_) |
| 293 | return; |
| 294 | |
| 295 | int rv = DoReadLoop(result); |
| 296 | if (rv != ERR_IO_PENDING) |
| 297 | DoReadCallback(rv); |
| 298 | } |
| 299 | |
| 300 | void SSLServerSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 301 | int rv = DoHandshakeLoop(result); |
| 302 | if (rv == ERR_IO_PENDING) |
| 303 | return; |
| 304 | |
| 305 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_SERVER_HANDSHAKE, rv); |
| 306 | if (!user_handshake_callback_.is_null()) |
| 307 | DoHandshakeCallback(rv); |
| 308 | } |
| 309 | |
| 310 | // Return 0 for EOF, |
| 311 | // > 0 for bytes transferred immediately, |
| 312 | // < 0 for error (or the non-error ERR_IO_PENDING). |
| 313 | int SSLServerSocketOpenSSL::BufferSend() { |
| 314 | if (transport_send_busy_) |
| 315 | return ERR_IO_PENDING; |
| 316 | |
| 317 | if (!send_buffer_.get()) { |
| 318 | // Get a fresh send buffer out of the send BIO. |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 319 | size_t max_read = BIO_pending(transport_bio_); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 320 | if (!max_read) |
| 321 | return 0; // Nothing pending in the OpenSSL write BIO. |
| 322 | send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); |
| 323 | int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); |
| 324 | DCHECK_GT(read_bytes, 0); |
| 325 | CHECK_EQ(static_cast<int>(max_read), read_bytes); |
| 326 | } |
| 327 | |
| 328 | int rv = transport_socket_->Write( |
| 329 | send_buffer_.get(), |
| 330 | send_buffer_->BytesRemaining(), |
| 331 | base::Bind(&SSLServerSocketOpenSSL::BufferSendComplete, |
| 332 | base::Unretained(this))); |
| 333 | if (rv == ERR_IO_PENDING) { |
| 334 | transport_send_busy_ = true; |
| 335 | } else { |
| 336 | TransportWriteComplete(rv); |
| 337 | } |
| 338 | return rv; |
| 339 | } |
| 340 | |
| 341 | void SSLServerSocketOpenSSL::BufferSendComplete(int result) { |
| 342 | transport_send_busy_ = false; |
| 343 | TransportWriteComplete(result); |
| 344 | OnSendComplete(result); |
| 345 | } |
| 346 | |
| 347 | void SSLServerSocketOpenSSL::TransportWriteComplete(int result) { |
| 348 | DCHECK(ERR_IO_PENDING != result); |
| 349 | if (result < 0) { |
| 350 | // Got a socket write error; close the BIO to indicate this upward. |
| 351 | // |
| 352 | // TODO(davidben): The value of |result| gets lost. Feed the error back into |
| 353 | // the BIO so it gets (re-)detected in OnSendComplete. Perhaps with |
| 354 | // BIO_set_callback. |
| 355 | DVLOG(1) << "TransportWriteComplete error " << result; |
| 356 | (void)BIO_shutdown_wr(SSL_get_wbio(ssl_)); |
| 357 | |
| 358 | // Match the fix for http://crbug.com/249848 in NSS by erroring future reads |
| 359 | // from the socket after a write error. |
| 360 | // |
| 361 | // TODO(davidben): Avoid having read and write ends interact this way. |
| 362 | transport_write_error_ = result; |
| 363 | (void)BIO_shutdown_wr(transport_bio_); |
| 364 | send_buffer_ = NULL; |
| 365 | } else { |
| 366 | DCHECK(send_buffer_.get()); |
| 367 | send_buffer_->DidConsume(result); |
| 368 | DCHECK_GE(send_buffer_->BytesRemaining(), 0); |
| 369 | if (send_buffer_->BytesRemaining() <= 0) |
| 370 | send_buffer_ = NULL; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | int SSLServerSocketOpenSSL::BufferRecv() { |
| 375 | if (transport_recv_busy_) |
| 376 | return ERR_IO_PENDING; |
| 377 | |
| 378 | // Determine how much was requested from |transport_bio_| that was not |
| 379 | // actually available. |
| 380 | size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
| 381 | if (requested == 0) { |
| 382 | // This is not a perfect match of error codes, as no operation is |
| 383 | // actually pending. However, returning 0 would be interpreted as |
| 384 | // a possible sign of EOF, which is also an inappropriate match. |
| 385 | return ERR_IO_PENDING; |
| 386 | } |
| 387 | |
| 388 | // Known Issue: While only reading |requested| data is the more correct |
| 389 | // implementation, it has the downside of resulting in frequent reads: |
| 390 | // One read for the SSL record header (~5 bytes) and one read for the SSL |
| 391 | // record body. Rather than issuing these reads to the underlying socket |
| 392 | // (and constantly allocating new IOBuffers), a single Read() request to |
| 393 | // fill |transport_bio_| is issued. As long as an SSL client socket cannot |
| 394 | // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL |
| 395 | // traffic, this over-subscribed Read()ing will not cause issues. |
| 396 | size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_); |
| 397 | if (!max_write) |
| 398 | return ERR_IO_PENDING; |
| 399 | |
| 400 | recv_buffer_ = new IOBuffer(max_write); |
| 401 | int rv = transport_socket_->Read( |
| 402 | recv_buffer_.get(), |
| 403 | max_write, |
| 404 | base::Bind(&SSLServerSocketOpenSSL::BufferRecvComplete, |
| 405 | base::Unretained(this))); |
| 406 | if (rv == ERR_IO_PENDING) { |
| 407 | transport_recv_busy_ = true; |
| 408 | } else { |
| 409 | rv = TransportReadComplete(rv); |
| 410 | } |
| 411 | return rv; |
| 412 | } |
| 413 | |
| 414 | void SSLServerSocketOpenSSL::BufferRecvComplete(int result) { |
| 415 | result = TransportReadComplete(result); |
| 416 | OnRecvComplete(result); |
| 417 | } |
| 418 | |
| 419 | int SSLServerSocketOpenSSL::TransportReadComplete(int result) { |
| 420 | DCHECK(ERR_IO_PENDING != result); |
| 421 | if (result <= 0) { |
| 422 | DVLOG(1) << "TransportReadComplete result " << result; |
| 423 | // Received 0 (end of file) or an error. Either way, bubble it up to the |
| 424 | // SSL layer via the BIO. TODO(joth): consider stashing the error code, to |
| 425 | // relay up to the SSL socket client (i.e. via DoReadCallback). |
| 426 | if (result == 0) |
| 427 | transport_recv_eof_ = true; |
| 428 | (void)BIO_shutdown_wr(transport_bio_); |
| 429 | } else if (transport_write_error_ < 0) { |
| 430 | // Mirror transport write errors as read failures; transport_bio_ has been |
| 431 | // shut down by TransportWriteComplete, so the BIO_write will fail, failing |
| 432 | // the CHECK. http://crbug.com/335557. |
| 433 | result = transport_write_error_; |
| 434 | } else { |
| 435 | DCHECK(recv_buffer_.get()); |
| 436 | int ret = BIO_write(transport_bio_, recv_buffer_->data(), result); |
| 437 | // A write into a memory BIO should always succeed. |
| 438 | DCHECK_EQ(result, ret); |
| 439 | } |
| 440 | recv_buffer_ = NULL; |
| 441 | transport_recv_busy_ = false; |
| 442 | return result; |
| 443 | } |
| 444 | |
| 445 | // Do as much network I/O as possible between the buffer and the |
| 446 | // transport socket. Return true if some I/O performed, false |
| 447 | // otherwise (error or ERR_IO_PENDING). |
| 448 | bool SSLServerSocketOpenSSL::DoTransportIO() { |
| 449 | bool network_moved = false; |
| 450 | int rv; |
| 451 | // Read and write as much data as possible. The loop is necessary because |
| 452 | // Write() may return synchronously. |
| 453 | do { |
| 454 | rv = BufferSend(); |
| 455 | if (rv != ERR_IO_PENDING && rv != 0) |
| 456 | network_moved = true; |
| 457 | } while (rv > 0); |
| 458 | if (!transport_recv_eof_ && BufferRecv() != ERR_IO_PENDING) |
| 459 | network_moved = true; |
| 460 | return network_moved; |
| 461 | } |
| 462 | |
| 463 | int SSLServerSocketOpenSSL::DoPayloadRead() { |
| 464 | DCHECK(user_read_buf_.get()); |
| 465 | DCHECK_GT(user_read_buf_len_, 0); |
| 466 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 467 | int rv = SSL_read(ssl_, user_read_buf_->data(), user_read_buf_len_); |
| 468 | if (rv >= 0) |
| 469 | return rv; |
| 470 | int ssl_error = SSL_get_error(ssl_, rv); |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 471 | OpenSSLErrorInfo error_info; |
| 472 | int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
| 473 | &error_info); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 474 | if (net_error != ERR_IO_PENDING) { |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 475 | net_log_.AddEvent( |
| 476 | NetLog::TYPE_SSL_READ_ERROR, |
| 477 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 478 | } |
| 479 | return net_error; |
| 480 | } |
| 481 | |
| 482 | int SSLServerSocketOpenSSL::DoPayloadWrite() { |
| 483 | DCHECK(user_write_buf_.get()); |
| 484 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 485 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
| 486 | if (rv >= 0) |
| 487 | return rv; |
| 488 | int ssl_error = SSL_get_error(ssl_, rv); |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 489 | OpenSSLErrorInfo error_info; |
| 490 | int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
| 491 | &error_info); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 492 | if (net_error != ERR_IO_PENDING) { |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 493 | net_log_.AddEvent( |
| 494 | NetLog::TYPE_SSL_WRITE_ERROR, |
| 495 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 496 | } |
| 497 | return net_error; |
| 498 | } |
| 499 | |
| 500 | int SSLServerSocketOpenSSL::DoHandshakeLoop(int last_io_result) { |
| 501 | int rv = last_io_result; |
| 502 | do { |
| 503 | // Default to STATE_NONE for next state. |
| 504 | // (This is a quirk carried over from the windows |
| 505 | // implementation. It makes reading the logs a bit harder.) |
| 506 | // State handlers can and often do call GotoState just |
| 507 | // to stay in the current state. |
| 508 | State state = next_handshake_state_; |
| 509 | GotoState(STATE_NONE); |
| 510 | switch (state) { |
| 511 | case STATE_HANDSHAKE: |
| 512 | rv = DoHandshake(); |
| 513 | break; |
| 514 | case STATE_NONE: |
| 515 | default: |
| 516 | rv = ERR_UNEXPECTED; |
| 517 | LOG(DFATAL) << "unexpected state " << state; |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | // Do the actual network I/O |
| 522 | bool network_moved = DoTransportIO(); |
| 523 | if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 524 | // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 525 | // special case we keep looping even if rv is ERR_IO_PENDING because |
| 526 | // the transport IO may allow DoHandshake to make progress. |
| 527 | rv = OK; // This causes us to stay in the loop. |
| 528 | } |
| 529 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 530 | return rv; |
| 531 | } |
| 532 | |
| 533 | int SSLServerSocketOpenSSL::DoReadLoop(int result) { |
| 534 | DCHECK(completed_handshake_); |
| 535 | DCHECK(next_handshake_state_ == STATE_NONE); |
| 536 | |
| 537 | if (result < 0) |
| 538 | return result; |
| 539 | |
| 540 | bool network_moved; |
| 541 | int rv; |
| 542 | do { |
| 543 | rv = DoPayloadRead(); |
| 544 | network_moved = DoTransportIO(); |
| 545 | } while (rv == ERR_IO_PENDING && network_moved); |
| 546 | return rv; |
| 547 | } |
| 548 | |
| 549 | int SSLServerSocketOpenSSL::DoWriteLoop(int result) { |
| 550 | DCHECK(completed_handshake_); |
| 551 | DCHECK_EQ(next_handshake_state_, STATE_NONE); |
| 552 | |
| 553 | if (result < 0) |
| 554 | return result; |
| 555 | |
| 556 | bool network_moved; |
| 557 | int rv; |
| 558 | do { |
| 559 | rv = DoPayloadWrite(); |
| 560 | network_moved = DoTransportIO(); |
| 561 | } while (rv == ERR_IO_PENDING && network_moved); |
| 562 | return rv; |
| 563 | } |
| 564 | |
| 565 | int SSLServerSocketOpenSSL::DoHandshake() { |
| 566 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 567 | int net_error = OK; |
| 568 | int rv = SSL_do_handshake(ssl_); |
| 569 | |
| 570 | if (rv == 1) { |
| 571 | completed_handshake_ = true; |
| 572 | } else { |
| 573 | int ssl_error = SSL_get_error(ssl_, rv); |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 574 | OpenSSLErrorInfo error_info; |
| 575 | net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 576 | |
| 577 | // If not done, stay in this state |
| 578 | if (net_error == ERR_IO_PENDING) { |
| 579 | GotoState(STATE_HANDSHAKE); |
| 580 | } else { |
| 581 | LOG(ERROR) << "handshake failed; returned " << rv |
| 582 | << ", SSL error code " << ssl_error |
| 583 | << ", net_error " << net_error; |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 584 | net_log_.AddEvent( |
| 585 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 586 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | return net_error; |
| 590 | } |
| 591 | |
| 592 | void SSLServerSocketOpenSSL::DoHandshakeCallback(int rv) { |
| 593 | DCHECK_NE(rv, ERR_IO_PENDING); |
| 594 | ResetAndReturn(&user_handshake_callback_).Run(rv > OK ? OK : rv); |
| 595 | } |
| 596 | |
| 597 | void SSLServerSocketOpenSSL::DoReadCallback(int rv) { |
| 598 | DCHECK(rv != ERR_IO_PENDING); |
| 599 | DCHECK(!user_read_callback_.is_null()); |
| 600 | |
| 601 | user_read_buf_ = NULL; |
| 602 | user_read_buf_len_ = 0; |
| 603 | ResetAndReturn(&user_read_callback_).Run(rv); |
| 604 | } |
| 605 | |
| 606 | void SSLServerSocketOpenSSL::DoWriteCallback(int rv) { |
| 607 | DCHECK(rv != ERR_IO_PENDING); |
| 608 | DCHECK(!user_write_callback_.is_null()); |
| 609 | |
| 610 | user_write_buf_ = NULL; |
| 611 | user_write_buf_len_ = 0; |
| 612 | ResetAndReturn(&user_write_callback_).Run(rv); |
| 613 | } |
| 614 | |
| 615 | int SSLServerSocketOpenSSL::Init() { |
| 616 | DCHECK(!ssl_); |
| 617 | DCHECK(!transport_bio_); |
| 618 | |
| 619 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 620 | |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 621 | ScopedSSL_CTX ssl_ctx(SSL_CTX_new(SSLv23_server_method())); |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 622 | |
| 623 | if (ssl_config_.require_client_cert) |
| 624 | SSL_CTX_set_verify(ssl_ctx.get(), SSL_VERIFY_PEER, NULL); |
| 625 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 626 | ssl_ = SSL_new(ssl_ctx.get()); |
| 627 | if (!ssl_) |
| 628 | return ERR_UNEXPECTED; |
| 629 | |
| 630 | BIO* ssl_bio = NULL; |
| 631 | // 0 => use default buffer sizes. |
| 632 | if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
| 633 | return ERR_UNEXPECTED; |
| 634 | DCHECK(ssl_bio); |
| 635 | DCHECK(transport_bio_); |
| 636 | |
| 637 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 638 | |
| 639 | // Set certificate and private key. |
| 640 | DCHECK(cert_->os_cert_handle()); |
[email protected] | 2b85635c | 2014-06-12 14:36:54 | [diff] [blame] | 641 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 642 | if (SSL_use_certificate(ssl_, cert_->os_cert_handle()) != 1) { |
| 643 | LOG(ERROR) << "Cannot set certificate."; |
| 644 | return ERR_UNEXPECTED; |
| 645 | } |
[email protected] | 2b85635c | 2014-06-12 14:36:54 | [diff] [blame] | 646 | #else |
| 647 | // Convert OSCertHandle to X509 structure. |
| 648 | std::string der_string; |
| 649 | if (!X509Certificate::GetDEREncoded(cert_->os_cert_handle(), &der_string)) |
| 650 | return ERR_UNEXPECTED; |
| 651 | |
| 652 | const unsigned char* der_string_array = |
| 653 | reinterpret_cast<const unsigned char*>(der_string.data()); |
| 654 | |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 655 | ScopedX509 x509(d2i_X509(NULL, &der_string_array, der_string.length())); |
[email protected] | 2b85635c | 2014-06-12 14:36:54 | [diff] [blame] | 656 | if (!x509.get()) |
| 657 | return ERR_UNEXPECTED; |
| 658 | |
| 659 | // On success, SSL_use_certificate acquires a reference to |x509|. |
| 660 | if (SSL_use_certificate(ssl_, x509.get()) != 1) { |
| 661 | LOG(ERROR) << "Cannot set certificate."; |
| 662 | return ERR_UNEXPECTED; |
| 663 | } |
| 664 | #endif // USE_OPENSSL_CERTS |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 665 | |
| 666 | DCHECK(key_->key()); |
| 667 | if (SSL_use_PrivateKey(ssl_, key_->key()) != 1) { |
| 668 | LOG(ERROR) << "Cannot set private key."; |
| 669 | return ERR_UNEXPECTED; |
| 670 | } |
| 671 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 672 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); |
| 673 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); |
| 674 | SSL_set_min_version(ssl_, ssl_config_.version_min); |
| 675 | SSL_set_max_version(ssl_, ssl_config_.version_max); |
| 676 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 677 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 678 | // set everything we care about to an absolute value. |
| 679 | SslSetClearMask options; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 680 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
| 681 | |
| 682 | SSL_set_options(ssl_, options.set_mask); |
| 683 | SSL_clear_options(ssl_, options.clear_mask); |
| 684 | |
| 685 | // Same as above, this time for the SSL mode. |
| 686 | SslSetClearMask mode; |
| 687 | |
| 688 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
| 689 | |
| 690 | SSL_set_mode(ssl_, mode.set_mask); |
| 691 | SSL_clear_mode(ssl_, mode.clear_mask); |
| 692 | |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 693 | // See SSLServerConfig::disabled_cipher_suites for description of the suites |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 694 | // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 |
| 695 | // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
| 696 | // as the handshake hash. |
| 697 | std::string command("DEFAULT:!SHA256:!SHA384:!AESGCM+AES256:!aPSK"); |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 698 | |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 699 | if (ssl_config_.require_ecdhe) |
| 700 | command.append(":!kRSA:!kDHE"); |
| 701 | |
| 702 | // Remove any disabled ciphers. |
| 703 | for (uint16_t id : ssl_config_.disabled_cipher_suites) { |
| 704 | const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); |
| 705 | if (cipher) { |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 706 | command.append(":!"); |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 707 | command.append(SSL_CIPHER_get_name(cipher)); |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 712 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 713 | // This will almost certainly result in the socket failing to complete the |
| 714 | // handshake at which point the appropriate error is bubbled up to the client. |
| 715 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command |
| 716 | << "') returned " << rv; |
| 717 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 718 | return OK; |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | } // namespace net |