[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // OpenSSL binding for SSLClientSocket. The class layout and general principle |
| 6 | // of operation is derived from SSLClientSocketNSS. |
| 7 | |
| 8 | #include "net/socket/ssl_client_socket_openssl.h" |
| 9 | |
| 10 | #include <openssl/ssl.h> |
| 11 | #include <openssl/err.h> |
| 12 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 13 | #include "base/lock.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 15 | #include "base/openssl_util.h" |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 16 | #include "base/singleton.h" |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 17 | #include "net/base/cert_verifier.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 18 | #include "net/base/net_errors.h" |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 19 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 20 | #include "net/base/ssl_connection_status_flags.h" |
| 21 | #include "net/base/ssl_info.h" |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 22 | #include "net/socket/ssl_error_params.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // Enable this to see logging for state machine state transitions. |
| 29 | #if 0 |
[email protected] | 3b11277 | 2010-10-04 10:54:49 | [diff] [blame] | 30 | #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 31 | " jump to state " << s; \ |
| 32 | next_handshake_state_ = s; } while (0) |
| 33 | #else |
| 34 | #define GotoState(s) next_handshake_state_ = s |
| 35 | #endif |
| 36 | |
| 37 | const size_t kMaxRecvBufferSize = 4096; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 38 | const int kSessionCacheTimeoutSeconds = 60 * 60; |
| 39 | const size_t kSessionCacheMaxEntires = 1024; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 40 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 41 | // This method doesn't seemed to have made it into the OpenSSL headers. |
| 42 | unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } |
| 43 | |
| 44 | // Used for encoding the |connection_status| field of an SSLInfo object. |
| 45 | int EncodeSSLConnectionStatus(int cipher_suite, |
| 46 | int compression, |
| 47 | int version) { |
| 48 | return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK) << |
| 49 | SSL_CONNECTION_CIPHERSUITE_SHIFT) | |
| 50 | ((compression & SSL_CONNECTION_COMPRESSION_MASK) << |
| 51 | SSL_CONNECTION_COMPRESSION_SHIFT) | |
| 52 | ((version & SSL_CONNECTION_VERSION_MASK) << |
| 53 | SSL_CONNECTION_VERSION_SHIFT); |
| 54 | } |
| 55 | |
| 56 | // Returns the net SSL version number (see ssl_connection_status_flags.h) for |
| 57 | // this SSL connection. |
| 58 | int GetNetSSLVersion(SSL* ssl) { |
| 59 | switch(SSL_version(ssl)) { |
| 60 | case SSL2_VERSION: |
| 61 | return SSL_CONNECTION_VERSION_SSL2; |
| 62 | case SSL3_VERSION: |
| 63 | return SSL_CONNECTION_VERSION_SSL3; |
| 64 | case TLS1_VERSION: |
| 65 | return SSL_CONNECTION_VERSION_TLS1; |
| 66 | case 0x0302: |
| 67 | return SSL_CONNECTION_VERSION_TLS1_1; |
| 68 | case 0x0303: |
| 69 | return SSL_CONNECTION_VERSION_TLS1_2; |
| 70 | default: |
| 71 | return SSL_CONNECTION_VERSION_UNKNOWN; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | int MapOpenSSLErrorSSL() { |
| 76 | // Walk down the error stack to find the SSLerr generated reason. |
| 77 | unsigned long error_code; |
| 78 | do { |
| 79 | error_code = ERR_get_error(); |
| 80 | if (error_code == 0) |
| 81 | return ERR_SSL_PROTOCOL_ERROR; |
| 82 | } while (ERR_GET_LIB(error_code) != ERR_LIB_SSL); |
| 83 | |
| 84 | DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code) |
| 85 | << ", name: " << ERR_error_string(error_code, NULL); |
| 86 | switch (ERR_GET_REASON(error_code)) { |
| 87 | case SSL_R_READ_TIMEOUT_EXPIRED: |
| 88 | return ERR_TIMED_OUT; |
| 89 | case SSL_R_BAD_RESPONSE_ARGUMENT: |
| 90 | return ERR_INVALID_ARGUMENT; |
| 91 | case SSL_R_UNKNOWN_CERTIFICATE_TYPE: |
| 92 | case SSL_R_UNKNOWN_CIPHER_TYPE: |
| 93 | case SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE: |
| 94 | case SSL_R_UNKNOWN_PKEY_TYPE: |
| 95 | case SSL_R_UNKNOWN_REMOTE_ERROR_TYPE: |
| 96 | case SSL_R_UNKNOWN_SSL_VERSION: |
| 97 | return ERR_NOT_IMPLEMENTED; |
| 98 | // SSL_R_UNKNOWN_PROTOCOL is reported if all the protocol versions |
| 99 | // supported by the server were disabled in this socket instance. |
| 100 | case SSL_R_UNKNOWN_PROTOCOL: |
| 101 | case SSL_R_UNSUPPORTED_SSL_VERSION: |
| 102 | case SSL_R_NO_CIPHER_MATCH: |
| 103 | case SSL_R_NO_SHARED_CIPHER: |
| 104 | case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: |
| 105 | case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: |
| 106 | return ERR_SSL_VERSION_OR_CIPHER_MISMATCH; |
| 107 | case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: |
| 108 | case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: |
| 109 | case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: |
| 110 | case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: |
| 111 | case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: |
| 112 | case SSL_R_TLSV1_ALERT_ACCESS_DENIED: |
| 113 | case SSL_R_TLSV1_ALERT_UNKNOWN_CA: |
| 114 | return ERR_BAD_SSL_CLIENT_AUTH_CERT; |
| 115 | case SSL_R_BAD_DECOMPRESSION: |
| 116 | case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: |
| 117 | return ERR_SSL_DECOMPRESSION_FAILURE_ALERT; |
| 118 | case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: |
| 119 | return ERR_SSL_BAD_RECORD_MAC_ALERT; |
| 120 | case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: |
| 121 | return ERR_SSL_UNSAFE_NEGOTIATION; |
| 122 | case SSL_R_WRONG_NUMBER_OF_KEY_BITS: |
| 123 | return ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY; |
| 124 | case SSL_R_SSL_HANDSHAKE_FAILURE: |
| 125 | case SSL_R_DECRYPTION_FAILED: |
| 126 | case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: |
| 127 | case SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG: |
| 128 | case SSL_R_DIGEST_CHECK_FAILED: |
| 129 | case SSL_R_DUPLICATE_COMPRESSION_ID: |
| 130 | case SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER: |
| 131 | case SSL_R_ENCRYPTED_LENGTH_TOO_LONG: |
| 132 | case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: |
| 133 | case SSL_R_EXCESSIVE_MESSAGE_SIZE: |
| 134 | case SSL_R_EXTRA_DATA_IN_MESSAGE: |
| 135 | case SSL_R_GOT_A_FIN_BEFORE_A_CCS: |
| 136 | case SSL_R_ILLEGAL_PADDING: |
| 137 | case SSL_R_INVALID_CHALLENGE_LENGTH: |
| 138 | case SSL_R_INVALID_COMMAND: |
| 139 | case SSL_R_INVALID_PURPOSE: |
| 140 | case SSL_R_INVALID_STATUS_RESPONSE: |
| 141 | case SSL_R_INVALID_TICKET_KEYS_LENGTH: |
| 142 | case SSL_R_KEY_ARG_TOO_LONG: |
| 143 | case SSL_R_READ_WRONG_PACKET_TYPE: |
| 144 | case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: |
| 145 | // TODO(joth): SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE may be returned from the |
| 146 | // server after receiving ClientHello if there's no common supported cipher. |
| 147 | // Ideally we'd map that specific case to ERR_SSL_VERSION_OR_CIPHER_MISMATCH |
| 148 | // to match the NSS implementation. See also http://goo.gl/oMtZW |
| 149 | case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: |
| 150 | case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: |
| 151 | case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: |
| 152 | case SSL_R_TLSV1_ALERT_DECODE_ERROR: |
| 153 | case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: |
| 154 | case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: |
| 155 | case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: |
| 156 | case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: |
| 157 | case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: |
| 158 | case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: |
| 159 | case SSL_R_TLSV1_ALERT_USER_CANCELLED: |
| 160 | return ERR_SSL_PROTOCOL_ERROR; |
| 161 | default: |
| 162 | LOG(WARNING) << "Unmapped error reason: " << ERR_GET_REASON(error_code); |
| 163 | return ERR_FAILED; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Converts an OpenSSL error code into a net error code, walking the OpenSSL |
| 168 | // error stack if needed. Note that |tracer| is not currently used in the |
| 169 | // implementation, but is passed in anyway as this ensures the caller will clear |
| 170 | // any residual codes left on the error stack. |
| 171 | int MapOpenSSLError(int err, const base::OpenSSLErrStackTracer& tracer) { |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 172 | switch (err) { |
| 173 | case SSL_ERROR_WANT_READ: |
| 174 | case SSL_ERROR_WANT_WRITE: |
| 175 | return ERR_IO_PENDING; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 176 | case SSL_ERROR_SYSCALL: |
[email protected] | abc7e06d | 2010-10-06 15:40:35 | [diff] [blame] | 177 | DVLOG(1) << "OpenSSL SYSCALL error, errno " << errno; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 178 | return ERR_SSL_PROTOCOL_ERROR; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 179 | case SSL_ERROR_SSL: |
| 180 | return MapOpenSSLErrorSSL(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 181 | default: |
| 182 | // TODO(joth): Implement full mapping. |
| 183 | LOG(WARNING) << "Unknown OpenSSL error " << err; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 184 | return ERR_SSL_PROTOCOL_ERROR; |
| 185 | } |
| 186 | } |
| 187 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 188 | // We do certificate verification after handshake, so we disable the default |
| 189 | // by registering a no-op verify function. |
| 190 | int NoOpVerifyCallback(X509_STORE_CTX*, void *) { |
| 191 | DVLOG(3) << "skipping cert verify"; |
| 192 | return 1; |
| 193 | } |
| 194 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 195 | // OpenSSL manages a cache of SSL_SESSION, this class provides the application |
| 196 | // side policy for that cache about session re-use: we retain one session per |
| 197 | // unique HostPortPair. |
| 198 | class SSLSessionCache { |
| 199 | public: |
| 200 | SSLSessionCache() {} |
| 201 | |
| 202 | void OnSessionAdded(const HostPortPair& host_and_port, SSL_SESSION* session) { |
| 203 | // Declare the session cleaner-upper before the lock, so any call into |
| 204 | // OpenSSL to free the session will happen after the lock is released. |
| 205 | base::ScopedOpenSSL<SSL_SESSION, SSL_SESSION_free> session_to_free; |
| 206 | AutoLock lock(lock_); |
| 207 | |
| 208 | DCHECK_EQ(0U, session_map_.count(session)); |
| 209 | std::pair<HostPortMap::iterator, bool> res = |
| 210 | host_port_map_.insert(std::make_pair(host_and_port, session)); |
| 211 | if (!res.second) { // Already exists: replace old entry. |
| 212 | session_to_free.reset(res.first->second); |
| 213 | session_map_.erase(session_to_free.get()); |
| 214 | res.first->second = session; |
| 215 | } |
| 216 | DVLOG(2) << "Adding session " << session << " => " |
| 217 | << host_and_port.ToString() << ", new entry = " << res.second; |
| 218 | DCHECK(host_port_map_[host_and_port] == session); |
| 219 | session_map_[session] = res.first; |
| 220 | DCHECK_EQ(host_port_map_.size(), session_map_.size()); |
| 221 | DCHECK_LE(host_port_map_.size(), kSessionCacheMaxEntires); |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 222 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 223 | |
| 224 | void OnSessionRemoved(SSL_SESSION* session) { |
| 225 | // Declare the session cleaner-upper before the lock, so any call into |
| 226 | // OpenSSL to free the session will happen after the lock is released. |
| 227 | base::ScopedOpenSSL<SSL_SESSION, SSL_SESSION_free> session_to_free; |
| 228 | AutoLock lock(lock_); |
| 229 | |
| 230 | SessionMap::iterator it = session_map_.find(session); |
| 231 | if (it == session_map_.end()) |
| 232 | return; |
| 233 | DVLOG(2) << "Remove session " << session << " => " |
| 234 | << it->second->first.ToString(); |
| 235 | DCHECK(it->second->second == session); |
| 236 | host_port_map_.erase(it->second); |
| 237 | session_map_.erase(it); |
| 238 | session_to_free.reset(session); |
| 239 | DCHECK_EQ(host_port_map_.size(), session_map_.size()); |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 240 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 241 | |
| 242 | // Looks up the host:port in the cache, and if a session is found it is added |
| 243 | // to |ssl|, returning true on success. |
| 244 | bool SetSSLSession(SSL* ssl, const HostPortPair& host_and_port) { |
| 245 | AutoLock lock(lock_); |
| 246 | HostPortMap::iterator it = host_port_map_.find(host_and_port); |
| 247 | if (it == host_port_map_.end()) |
| 248 | return false; |
| 249 | DVLOG(2) << "Lookup session: " << it->second << " => " |
| 250 | << host_and_port.ToString(); |
| 251 | SSL_SESSION* session = it->second; |
| 252 | DCHECK(session); |
| 253 | DCHECK(session_map_[session] == it); |
| 254 | // Ideally we'd release |lock_| before calling into OpenSSL here, however |
| 255 | // that opens a small risk |session| will go out of scope before it is used. |
| 256 | // Alternatively we would take a temporary local refcount on |session|, |
| 257 | // except OpenSSL does not provide a public API for adding a ref (c.f. |
| 258 | // SSL_SESSION_free which decrements the ref). |
| 259 | return SSL_set_session(ssl, session) == 1; |
| 260 | } |
| 261 | |
| 262 | private: |
| 263 | // A pair of maps to allow bi-directional lookups between host:port and an |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 264 | // associated session. |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 265 | // TODO(joth): When client certificates are implemented we should key the |
| 266 | // cache on the client certificate used in addition to the host-port pair. |
| 267 | typedef std::map<HostPortPair, SSL_SESSION*> HostPortMap; |
| 268 | typedef std::map<SSL_SESSION*, HostPortMap::iterator> SessionMap; |
| 269 | HostPortMap host_port_map_; |
| 270 | SessionMap session_map_; |
| 271 | |
| 272 | // Protects access to both the above maps. |
| 273 | Lock lock_; |
| 274 | |
| 275 | DISALLOW_COPY_AND_ASSIGN(SSLSessionCache); |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 276 | }; |
| 277 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 278 | class SSLContext { |
| 279 | public: |
| 280 | static SSLContext* Get() { return Singleton<SSLContext>::get(); } |
| 281 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
| 282 | SSLSessionCache* session_cache() { return &session_cache_; } |
| 283 | |
| 284 | SSLClientSocketOpenSSL* GetClientSocketFromSSL(SSL* ssl) { |
| 285 | DCHECK(ssl); |
| 286 | SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( |
| 287 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 288 | DCHECK(socket); |
| 289 | return socket; |
| 290 | } |
| 291 | |
| 292 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { |
| 293 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 294 | } |
| 295 | |
| 296 | private: |
| 297 | friend struct DefaultSingletonTraits<SSLContext>; |
| 298 | |
| 299 | SSLContext() { |
| 300 | base::EnsureOpenSSLInit(); |
| 301 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 302 | DCHECK_NE(ssl_socket_data_index_, -1); |
| 303 | ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
| 304 | SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), NoOpVerifyCallback, NULL); |
| 305 | SSL_CTX_set_session_cache_mode(ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT); |
| 306 | SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallbackStatic); |
| 307 | SSL_CTX_sess_set_remove_cb(ssl_ctx_.get(), RemoveSessionCallbackStatic); |
| 308 | SSL_CTX_set_timeout(ssl_ctx_.get(), kSessionCacheTimeoutSeconds); |
| 309 | SSL_CTX_sess_set_cache_size(ssl_ctx_.get(), kSessionCacheMaxEntires); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 310 | SSL_CTX_set_client_cert_cb(ssl_ctx_.get(), ClientCertCallback); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { |
| 314 | return Get()->NewSessionCallback(ssl, session); |
| 315 | } |
| 316 | |
| 317 | int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
| 318 | SSLClientSocketOpenSSL* socket = GetClientSocketFromSSL(ssl); |
| 319 | session_cache_.OnSessionAdded(socket->host_and_port(), session); |
| 320 | return 1; // 1 => We took ownership of |session|. |
| 321 | } |
| 322 | |
| 323 | static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { |
| 324 | return Get()->RemoveSessionCallback(ctx, session); |
| 325 | } |
| 326 | |
| 327 | void RemoveSessionCallback(SSL_CTX* ctx, SSL_SESSION* session) { |
| 328 | DCHECK(ctx == ssl_ctx()); |
| 329 | session_cache_.OnSessionRemoved(session); |
| 330 | } |
| 331 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 332 | static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { |
| 333 | SSLClientSocketOpenSSL* socket = Get()->GetClientSocketFromSSL(ssl); |
| 334 | CHECK(socket); |
| 335 | return socket->ClientCertRequestCallback(ssl, x509, pkey); |
| 336 | } |
| 337 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 338 | // This is the index used with SSL_get_ex_data to retrieve the owner |
| 339 | // SSLClientSocketOpenSSL object from an SSL instance. |
| 340 | int ssl_socket_data_index_; |
| 341 | |
| 342 | base::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; |
| 343 | SSLSessionCache session_cache_; |
| 344 | }; |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 345 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 346 | // Utility to construct the appropriate set & clear masks for use the OpenSSL |
| 347 | // options and mode configuration functions. (SSL_set_options etc) |
| 348 | struct SslSetClearMask { |
| 349 | SslSetClearMask() : set_mask(0), clear_mask(0) {} |
| 350 | void ConfigureFlag(long flag, bool state) { |
| 351 | (state ? set_mask : clear_mask) |= flag; |
| 352 | // Make sure we haven't got any intersection in the set & clear options. |
| 353 | DCHECK_EQ(0, set_mask & clear_mask) << flag << ":" << state; |
| 354 | } |
| 355 | long set_mask; |
| 356 | long clear_mask; |
| 357 | }; |
| 358 | |
[email protected] | 3b11277 | 2010-10-04 10:54:49 | [diff] [blame] | 359 | } // namespace |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 360 | |
| 361 | SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
| 362 | ClientSocketHandle* transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 363 | const HostPortPair& host_and_port, |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 364 | const SSLConfig& ssl_config) |
| 365 | : ALLOW_THIS_IN_INITIALIZER_LIST(buffer_send_callback_( |
| 366 | this, &SSLClientSocketOpenSSL::BufferSendComplete)), |
| 367 | ALLOW_THIS_IN_INITIALIZER_LIST(buffer_recv_callback_( |
| 368 | this, &SSLClientSocketOpenSSL::BufferRecvComplete)), |
| 369 | transport_send_busy_(false), |
| 370 | transport_recv_busy_(false), |
| 371 | user_connect_callback_(NULL), |
| 372 | user_read_callback_(NULL), |
| 373 | user_write_callback_(NULL), |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 374 | completed_handshake_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 375 | client_auth_cert_needed_(false), |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 376 | ALLOW_THIS_IN_INITIALIZER_LIST(handshake_io_callback_( |
| 377 | this, &SSLClientSocketOpenSSL::OnHandshakeIOComplete)), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 378 | ssl_(NULL), |
| 379 | transport_bio_(NULL), |
| 380 | transport_(transport_socket), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 381 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 382 | ssl_config_(ssl_config), |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 383 | trying_cached_session_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 384 | net_log_(transport_socket->socket()->NetLog()) { |
| 385 | } |
| 386 | |
| 387 | SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 388 | Disconnect(); |
| 389 | } |
| 390 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 391 | bool SSLClientSocketOpenSSL::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 392 | DCHECK(!ssl_); |
| 393 | DCHECK(!transport_bio_); |
| 394 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 395 | SSLContext* context = SSLContext::Get(); |
| 396 | base::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 397 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 398 | ssl_ = SSL_new(context->ssl_ctx()); |
| 399 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 400 | return false; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 401 | |
| 402 | if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
| 403 | return false; |
| 404 | |
| 405 | trying_cached_session_ = |
| 406 | context->session_cache()->SetSSLSession(ssl_, host_and_port_); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 407 | |
| 408 | BIO* ssl_bio = NULL; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 409 | // 0 => use default buffer sizes. |
| 410 | if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 411 | return false; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 412 | DCHECK(ssl_bio); |
| 413 | DCHECK(transport_bio_); |
| 414 | |
| 415 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 416 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 417 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 418 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 419 | SslSetClearMask options; |
| 420 | options.ConfigureFlag(SSL_OP_NO_SSLv2, true); |
| 421 | options.ConfigureFlag(SSL_OP_NO_SSLv3, !ssl_config_.ssl3_enabled); |
| 422 | options.ConfigureFlag(SSL_OP_NO_TLSv1, !ssl_config_.tls1_enabled); |
| 423 | |
| 424 | #if defined(SSL_OP_NO_COMPRESSION) |
| 425 | // If TLS was disabled also disable compression, to provide maximum site |
| 426 | // compatibility in the case of protocol fallback. See http://crbug.com/31628 |
| 427 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, !ssl_config_.tls1_enabled); |
| 428 | #endif |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 429 | |
| 430 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 431 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 432 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 433 | SSL_set_options(ssl_, options.set_mask); |
| 434 | SSL_clear_options(ssl_, options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 435 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 436 | // Same as above, this time for the SSL mode. |
| 437 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 438 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 439 | #if defined(SSL_MODE_HANDSHAKE_CUTTHROUGH) |
| 440 | mode.ConfigureFlag(SSL_MODE_HANDSHAKE_CUTTHROUGH, |
| 441 | ssl_config_.false_start_enabled && |
| 442 | !SSLConfigService::IsKnownFalseStartIncompatibleServer( |
| 443 | host_and_port_.host())); |
| 444 | #endif |
| 445 | |
| 446 | #if defined(SSL_MODE_RELEASE_BUFFERS) |
| 447 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
| 448 | #endif |
| 449 | |
| 450 | #if defined(SSL_MODE_SMALL_BUFFERS) |
| 451 | mode.ConfigureFlag(SSL_MODE_SMALL_BUFFERS, true); |
| 452 | #endif |
| 453 | |
| 454 | SSL_set_mode(ssl_, mode.set_mask); |
| 455 | SSL_clear_mode(ssl_, mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 456 | |
| 457 | // Removing ciphers by ID from OpenSSL is a bit involved as we must use the |
| 458 | // textual name with SSL_set_cipher_list because there is no public API to |
| 459 | // directly remove a cipher by ID. |
| 460 | STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); |
| 461 | DCHECK(ciphers); |
| 462 | // See SSLConfig::disabled_cipher_suites for description of the suites |
| 463 | // disabled by default. |
| 464 | std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA"); |
| 465 | // Walk through all the installed ciphers, seeing if any need to be |
| 466 | // appended to the cipher removal |command|. |
| 467 | for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { |
| 468 | const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 469 | const uint16 id = SSL_CIPHER_get_id(cipher); |
| 470 | // Remove any ciphers with a strength of less than 80 bits. Note the NSS |
| 471 | // implementation uses "effective" bits here but OpenSSL does not provide |
| 472 | // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, |
| 473 | // both of which are greater than 80 anyway. |
| 474 | bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; |
| 475 | if (!disable) { |
| 476 | disable = std::find(ssl_config_.disabled_cipher_suites.begin(), |
| 477 | ssl_config_.disabled_cipher_suites.end(), id) != |
| 478 | ssl_config_.disabled_cipher_suites.end(); |
| 479 | } |
| 480 | if (disable) { |
| 481 | const char* name = SSL_CIPHER_get_name(cipher); |
| 482 | DVLOG(3) << "Found cipher to remove: '" << name << "', ID: " << id |
| 483 | << " strength: " << SSL_CIPHER_get_bits(cipher, NULL); |
| 484 | command.append(":!"); |
| 485 | command.append(name); |
| 486 | } |
| 487 | } |
| 488 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 489 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 490 | // This will almost certainly result in the socket failing to complete the |
| 491 | // handshake at which point the appropriate error is bubbled up to the client. |
| 492 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
| 493 | "returned " << rv; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 497 | int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, |
| 498 | X509** x509, |
| 499 | EVP_PKEY** pkey) { |
| 500 | DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 501 | DCHECK(ssl == ssl_); |
| 502 | DCHECK(*x509 == NULL); |
| 503 | DCHECK(*pkey == NULL); |
| 504 | |
| 505 | if (!ssl_config_.send_client_cert) { |
| 506 | client_auth_cert_needed_ = true; |
| 507 | return -1; // Suspends handshake. |
| 508 | } |
| 509 | |
| 510 | // Second pass: a client certificate should have been selected. |
| 511 | if (ssl_config_.client_cert) { |
| 512 | // TODO(joth): We need a way to lookup the private key this |
| 513 | // certificate. See http://crbug.com/64951 and example code in |
| 514 | // http://codereview.chromium.org/5195001/diff/6001/net/socket/ssl_client_socket_openssl.cc |
| 515 | NOTIMPLEMENTED(); |
| 516 | } |
| 517 | |
| 518 | // Send no client certificate. |
| 519 | return 0; |
| 520 | } |
| 521 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 522 | // SSLClientSocket methods |
| 523 | |
| 524 | void SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 525 | ssl_info->Reset(); |
| 526 | if (!server_cert_) |
| 527 | return; |
| 528 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 529 | ssl_info->cert = server_cert_; |
| 530 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
[email protected] | 2907525c | 2010-10-08 15:53:52 | [diff] [blame] | 531 | |
| 532 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 533 | CHECK(cipher); |
| 534 | ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
[email protected] | 2907525c | 2010-10-08 15:53:52 | [diff] [blame] | 535 | const COMP_METHOD* compression = SSL_get_current_compression(ssl_); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 536 | |
| 537 | ssl_info->connection_status = EncodeSSLConnectionStatus( |
| 538 | SSL_CIPHER_get_id(cipher), |
| 539 | compression ? compression->type : 0, |
| 540 | GetNetSSLVersion(ssl_)); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 541 | |
| 542 | bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_); |
| 543 | if (!peer_supports_renego_ext) |
| 544 | ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 545 | UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported", |
| 546 | implicit_cast<int>(peer_supports_renego_ext), 2); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 547 | |
| 548 | if (ssl_config_.ssl3_fallback) |
| 549 | ssl_info->connection_status |= SSL_CONNECTION_SSL3_FALLBACK; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 550 | |
| 551 | DVLOG(3) << "Encoded connection status: cipher suite = " |
| 552 | << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) |
| 553 | << " compression = " |
| 554 | << SSLConnectionStatusToCompression(ssl_info->connection_status) |
| 555 | << " version = " |
| 556 | << SSLConnectionStatusToVersion(ssl_info->connection_status); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 560 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 561 | cert_request_info->host_and_port = host_and_port_.ToString(); |
| 562 | cert_request_info->client_certs = client_certs_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
| 566 | std::string* proto) { |
| 567 | proto->clear(); |
| 568 | return kNextProtoUnsupported; |
| 569 | } |
| 570 | |
| 571 | void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
| 572 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 573 | // up front. |
| 574 | CompletionCallback* c = user_read_callback_; |
| 575 | user_read_callback_ = NULL; |
| 576 | user_read_buf_ = NULL; |
| 577 | user_read_buf_len_ = 0; |
| 578 | c->Run(rv); |
| 579 | } |
| 580 | |
| 581 | void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
| 582 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 583 | // up front. |
| 584 | CompletionCallback* c = user_write_callback_; |
| 585 | user_write_callback_ = NULL; |
| 586 | user_write_buf_ = NULL; |
| 587 | user_write_buf_len_ = 0; |
| 588 | c->Run(rv); |
| 589 | } |
| 590 | |
| 591 | // ClientSocket methods |
| 592 | |
| 593 | int SSLClientSocketOpenSSL::Connect(CompletionCallback* callback) { |
| 594 | net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); |
| 595 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 596 | // Set up new ssl object. |
| 597 | if (!Init()) { |
| 598 | net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); |
| 599 | return ERR_UNEXPECTED; |
| 600 | } |
| 601 | |
| 602 | // Set SSL to client mode. Handshake happens in the loop below. |
| 603 | SSL_set_connect_state(ssl_); |
| 604 | |
| 605 | GotoState(STATE_HANDSHAKE); |
| 606 | int rv = DoHandshakeLoop(net::OK); |
| 607 | if (rv == ERR_IO_PENDING) { |
| 608 | user_connect_callback_ = callback; |
| 609 | } else { |
| 610 | net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); |
| 611 | } |
| 612 | |
| 613 | return rv > OK ? OK : rv; |
| 614 | } |
| 615 | |
| 616 | void SSLClientSocketOpenSSL::Disconnect() { |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 617 | if (ssl_) { |
| 618 | SSL_free(ssl_); |
| 619 | ssl_ = NULL; |
| 620 | } |
| 621 | if (transport_bio_) { |
| 622 | BIO_free_all(transport_bio_); |
| 623 | transport_bio_ = NULL; |
| 624 | } |
| 625 | |
| 626 | // Shut down anything that may call us back (through buffer_send_callback_, |
| 627 | // buffer_recv_callback, or handshake_io_callback_). |
| 628 | verifier_.reset(); |
| 629 | transport_->socket()->Disconnect(); |
| 630 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 631 | // Null all callbacks, delete all buffers. |
| 632 | transport_send_busy_ = false; |
| 633 | send_buffer_ = NULL; |
| 634 | transport_recv_busy_ = false; |
| 635 | recv_buffer_ = NULL; |
| 636 | |
| 637 | user_connect_callback_ = NULL; |
| 638 | user_read_callback_ = NULL; |
| 639 | user_write_callback_ = NULL; |
| 640 | user_read_buf_ = NULL; |
| 641 | user_read_buf_len_ = 0; |
| 642 | user_write_buf_ = NULL; |
| 643 | user_write_buf_len_ = 0; |
| 644 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 645 | server_cert_verify_result_.Reset(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 646 | completed_handshake_ = false; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 647 | |
| 648 | client_certs_.clear(); |
| 649 | client_auth_cert_needed_ = false; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | int SSLClientSocketOpenSSL::DoHandshakeLoop(int last_io_result) { |
| 653 | bool network_moved; |
| 654 | int rv = last_io_result; |
| 655 | do { |
| 656 | // Default to STATE_NONE for next state. |
| 657 | // (This is a quirk carried over from the windows |
| 658 | // implementation. It makes reading the logs a bit harder.) |
| 659 | // State handlers can and often do call GotoState just |
| 660 | // to stay in the current state. |
| 661 | State state = next_handshake_state_; |
| 662 | GotoState(STATE_NONE); |
| 663 | switch (state) { |
| 664 | case STATE_NONE: |
| 665 | // we're just pumping data between the buffer and the network |
| 666 | break; |
| 667 | case STATE_HANDSHAKE: |
| 668 | rv = DoHandshake(); |
| 669 | break; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 670 | case STATE_VERIFY_CERT: |
| 671 | DCHECK(rv == OK); |
| 672 | rv = DoVerifyCert(rv); |
| 673 | break; |
| 674 | case STATE_VERIFY_CERT_COMPLETE: |
| 675 | rv = DoVerifyCertComplete(rv); |
| 676 | break; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 677 | default: |
| 678 | rv = ERR_UNEXPECTED; |
| 679 | NOTREACHED() << "unexpected state" << state; |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | // To avoid getting an ERR_IO_PENDING here after handshake complete. |
| 684 | if (next_handshake_state_ == STATE_NONE) |
| 685 | break; |
| 686 | |
| 687 | // Do the actual network I/O. |
| 688 | network_moved = DoTransportIO(); |
| 689 | } while ((rv != ERR_IO_PENDING || network_moved) && |
| 690 | next_handshake_state_ != STATE_NONE); |
| 691 | return rv; |
| 692 | } |
| 693 | |
| 694 | int SSLClientSocketOpenSSL::DoHandshake() { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 695 | base::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 696 | int net_error = net::OK; |
| 697 | int rv = SSL_do_handshake(ssl_); |
| 698 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 699 | if (client_auth_cert_needed_) { |
| 700 | net_error = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 701 | // If the handshake already succeeded (because the server requests but |
| 702 | // doesn't require a client cert), we need to invalidate the SSL session |
| 703 | // so that we won't try to resume the non-client-authenticated session in |
| 704 | // the next handshake. This will cause the server to ask for a client |
| 705 | // cert again. |
| 706 | if (rv == 1) { |
| 707 | // Remove from session cache but don't clear this connection. |
| 708 | SSL_SESSION* session = SSL_get_session(ssl_); |
| 709 | if (session) { |
| 710 | int rv = SSL_CTX_remove_session(SSL_get_SSL_CTX(ssl_), session); |
| 711 | LOG_IF(WARNING, !rv) << "Couldn't invalidate SSL session: " << session; |
| 712 | } |
| 713 | } |
| 714 | } else if (rv == 1) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 715 | if (trying_cached_session_ && logging::DEBUG_MODE) { |
| 716 | DVLOG(2) << "Result of session reuse for " << host_and_port_.ToString() |
| 717 | << " is: " << (SSL_session_reused(ssl_) ? "Success" : "Fail"); |
| 718 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 719 | // SSL handshake is completed. Let's verify the certificate. |
[email protected] | abc7e06d | 2010-10-06 15:40:35 | [diff] [blame] | 720 | const bool got_cert = !!UpdateServerCert(); |
| 721 | DCHECK(got_cert); |
| 722 | GotoState(STATE_VERIFY_CERT); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 723 | } else { |
| 724 | int ssl_error = SSL_get_error(ssl_, rv); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 725 | net_error = MapOpenSSLError(ssl_error, err_tracer); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 726 | |
| 727 | // If not done, stay in this state |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 728 | if (net_error == ERR_IO_PENDING) { |
| 729 | GotoState(STATE_HANDSHAKE); |
| 730 | } else { |
| 731 | LOG(ERROR) << "handshake failed; returned " << rv |
| 732 | << ", SSL error code " << ssl_error |
| 733 | << ", net_error " << net_error; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 734 | net_log_.AddEvent( |
| 735 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 736 | make_scoped_refptr(new SSLErrorParams(net_error, ssl_error))); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | return net_error; |
| 740 | } |
| 741 | |
| 742 | int SSLClientSocketOpenSSL::DoVerifyCert(int result) { |
| 743 | DCHECK(server_cert_); |
| 744 | GotoState(STATE_VERIFY_CERT_COMPLETE); |
| 745 | int flags = 0; |
| 746 | |
| 747 | if (ssl_config_.rev_checking_enabled) |
| 748 | flags |= X509Certificate::VERIFY_REV_CHECKING_ENABLED; |
| 749 | if (ssl_config_.verify_ev_cert) |
| 750 | flags |= X509Certificate::VERIFY_EV_CERT; |
| 751 | verifier_.reset(new CertVerifier); |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 752 | return verifier_->Verify(server_cert_, host_and_port_.host(), flags, |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 753 | &server_cert_verify_result_, |
| 754 | &handshake_io_callback_); |
| 755 | } |
| 756 | |
| 757 | int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
| 758 | verifier_.reset(); |
| 759 | |
| 760 | if (result == OK) { |
| 761 | // TODO(joth): Work out if we need to remember the intermediate CA certs |
| 762 | // when the server sends them to us, and do so here. |
[email protected] | 2907525c | 2010-10-08 15:53:52 | [diff] [blame] | 763 | } else { |
| 764 | DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
| 765 | << " (" << result << ")"; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 766 | } |
| 767 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 768 | // If we have been explicitly told to accept this certificate, override the |
| 769 | // result of verifier_.Verify. |
| 770 | // Eventually, we should cache the cert verification results so that we don't |
| 771 | // need to call verifier_.Verify repeatedly. But for now we need to do this. |
| 772 | // Alternatively, we could use the cert's status that we stored along with |
| 773 | // the cert in the allowed_bad_certs vector. |
| 774 | if (IsCertificateError(result) && |
| 775 | ssl_config_.IsAllowedBadCert(server_cert_)) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 776 | VLOG(1) << "accepting bad SSL certificate, as user told us to"; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 777 | result = OK; |
| 778 | } |
| 779 | |
| 780 | completed_handshake_ = true; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 781 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 782 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 783 | return result; |
| 784 | } |
| 785 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 786 | X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { |
| 787 | if (server_cert_) |
| 788 | return server_cert_; |
| 789 | |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 790 | base::ScopedOpenSSL<X509, X509_free> cert(SSL_get_peer_certificate(ssl_)); |
[email protected] | 2907525c | 2010-10-08 15:53:52 | [diff] [blame] | 791 | if (!cert.get()) { |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 792 | LOG(WARNING) << "SSL_get_peer_certificate returned NULL"; |
| 793 | return NULL; |
| 794 | } |
| 795 | |
[email protected] | 2907525c | 2010-10-08 15:53:52 | [diff] [blame] | 796 | // Unlike SSL_get_peer_certificate, SSL_get_peer_cert_chain does not |
| 797 | // increment the reference so sk_X509_free does not need to be called. |
| 798 | STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl_); |
| 799 | X509Certificate::OSCertHandles intermediates; |
| 800 | if (chain) { |
| 801 | for (int i = 0; i < sk_X509_num(chain); ++i) |
| 802 | intermediates.push_back(sk_X509_value(chain, i)); |
| 803 | } |
| 804 | server_cert_ = X509Certificate::CreateFromHandle( |
| 805 | cert.get(), X509Certificate::SOURCE_FROM_NETWORK, intermediates); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 806 | DCHECK(server_cert_); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 807 | |
| 808 | return server_cert_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | bool SSLClientSocketOpenSSL::DoTransportIO() { |
| 812 | bool network_moved = false; |
| 813 | int nsent = BufferSend(); |
| 814 | int nreceived = BufferRecv(); |
| 815 | network_moved = (nsent > 0 || nreceived >= 0); |
| 816 | return network_moved; |
| 817 | } |
| 818 | |
| 819 | int SSLClientSocketOpenSSL::BufferSend(void) { |
| 820 | if (transport_send_busy_) |
| 821 | return ERR_IO_PENDING; |
| 822 | |
| 823 | if (!send_buffer_) { |
| 824 | // Get a fresh send buffer out of the send BIO. |
| 825 | size_t max_read = BIO_ctrl_pending(transport_bio_); |
| 826 | if (max_read > 0) { |
| 827 | send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); |
| 828 | int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); |
| 829 | DCHECK_GT(read_bytes, 0); |
| 830 | CHECK_EQ(static_cast<int>(max_read), read_bytes); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | int rv = 0; |
| 835 | while (send_buffer_) { |
| 836 | rv = transport_->socket()->Write(send_buffer_, |
| 837 | send_buffer_->BytesRemaining(), |
| 838 | &buffer_send_callback_); |
| 839 | if (rv == ERR_IO_PENDING) { |
| 840 | transport_send_busy_ = true; |
| 841 | return rv; |
| 842 | } |
| 843 | TransportWriteComplete(rv); |
| 844 | } |
| 845 | return rv; |
| 846 | } |
| 847 | |
| 848 | void SSLClientSocketOpenSSL::BufferSendComplete(int result) { |
| 849 | transport_send_busy_ = false; |
| 850 | TransportWriteComplete(result); |
| 851 | OnSendComplete(result); |
| 852 | } |
| 853 | |
| 854 | void SSLClientSocketOpenSSL::TransportWriteComplete(int result) { |
[email protected] | abc7e06d | 2010-10-06 15:40:35 | [diff] [blame] | 855 | DCHECK(ERR_IO_PENDING != result); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 856 | if (result < 0) { |
| 857 | // Got a socket write error; close the BIO to indicate this upward. |
[email protected] | abc7e06d | 2010-10-06 15:40:35 | [diff] [blame] | 858 | DVLOG(1) << "TransportWriteComplete error " << result; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 859 | (void)BIO_shutdown_wr(transport_bio_); |
| 860 | send_buffer_ = NULL; |
| 861 | } else { |
| 862 | DCHECK(send_buffer_); |
| 863 | send_buffer_->DidConsume(result); |
| 864 | DCHECK_GE(send_buffer_->BytesRemaining(), 0); |
| 865 | if (send_buffer_->BytesRemaining() <= 0) |
| 866 | send_buffer_ = NULL; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | int SSLClientSocketOpenSSL::BufferRecv(void) { |
| 871 | if (transport_recv_busy_) |
| 872 | return ERR_IO_PENDING; |
| 873 | |
| 874 | size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_); |
| 875 | if (max_write > kMaxRecvBufferSize) |
| 876 | max_write = kMaxRecvBufferSize; |
| 877 | |
| 878 | if (!max_write) |
| 879 | return ERR_IO_PENDING; |
| 880 | |
| 881 | recv_buffer_ = new IOBuffer(max_write); |
| 882 | int rv = transport_->socket()->Read(recv_buffer_, max_write, |
| 883 | &buffer_recv_callback_); |
| 884 | if (rv == ERR_IO_PENDING) { |
| 885 | transport_recv_busy_ = true; |
| 886 | } else { |
| 887 | TransportReadComplete(rv); |
| 888 | } |
| 889 | return rv; |
| 890 | } |
| 891 | |
| 892 | void SSLClientSocketOpenSSL::BufferRecvComplete(int result) { |
| 893 | TransportReadComplete(result); |
| 894 | OnRecvComplete(result); |
| 895 | } |
| 896 | |
| 897 | void SSLClientSocketOpenSSL::TransportReadComplete(int result) { |
[email protected] | abc7e06d | 2010-10-06 15:40:35 | [diff] [blame] | 898 | DCHECK(ERR_IO_PENDING != result); |
| 899 | if (result <= 0) { |
| 900 | DVLOG(1) << "TransportReadComplete result " << result; |
| 901 | // Received 0 (end of file) or an error. Either way, bubble it up to the |
| 902 | // SSL layer via the BIO. TODO(joth): consider stashing the error code, to |
| 903 | // relay up to the SSL socket client (i.e. via DoReadCallback). |
| 904 | BIO_set_mem_eof_return(transport_bio_, 0); |
| 905 | (void)BIO_shutdown_wr(transport_bio_); |
| 906 | } else { |
| 907 | DCHECK(recv_buffer_); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 908 | int ret = BIO_write(transport_bio_, recv_buffer_->data(), result); |
| 909 | // A write into a memory BIO should always succeed. |
| 910 | CHECK_EQ(result, ret); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 911 | } |
| 912 | recv_buffer_ = NULL; |
| 913 | transport_recv_busy_ = false; |
| 914 | } |
| 915 | |
| 916 | void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
| 917 | CompletionCallback* c = user_connect_callback_; |
| 918 | user_connect_callback_ = NULL; |
| 919 | c->Run(rv > OK ? OK : rv); |
| 920 | } |
| 921 | |
| 922 | void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 923 | int rv = DoHandshakeLoop(result); |
| 924 | if (rv != ERR_IO_PENDING) { |
| 925 | net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); |
| 926 | DoConnectCallback(rv); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
| 931 | if (next_handshake_state_ != STATE_NONE) { |
| 932 | // In handshake phase. |
| 933 | OnHandshakeIOComplete(result); |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | // OnSendComplete may need to call DoPayloadRead while the renegotiation |
| 938 | // handshake is in progress. |
| 939 | int rv_read = ERR_IO_PENDING; |
| 940 | int rv_write = ERR_IO_PENDING; |
| 941 | bool network_moved; |
| 942 | do { |
| 943 | if (user_read_buf_) |
| 944 | rv_read = DoPayloadRead(); |
| 945 | if (user_write_buf_) |
| 946 | rv_write = DoPayloadWrite(); |
| 947 | network_moved = DoTransportIO(); |
| 948 | } while (rv_read == ERR_IO_PENDING && |
| 949 | rv_write == ERR_IO_PENDING && |
| 950 | network_moved); |
| 951 | |
| 952 | if (user_read_buf_ && rv_read != ERR_IO_PENDING) |
| 953 | DoReadCallback(rv_read); |
| 954 | if (user_write_buf_ && rv_write != ERR_IO_PENDING) |
| 955 | DoWriteCallback(rv_write); |
| 956 | } |
| 957 | |
| 958 | void SSLClientSocketOpenSSL::OnRecvComplete(int result) { |
| 959 | if (next_handshake_state_ != STATE_NONE) { |
| 960 | // In handshake phase. |
| 961 | OnHandshakeIOComplete(result); |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | // Network layer received some data, check if client requested to read |
| 966 | // decrypted data. |
| 967 | if (!user_read_buf_) |
| 968 | return; |
| 969 | |
| 970 | int rv = DoReadLoop(result); |
| 971 | if (rv != ERR_IO_PENDING) |
| 972 | DoReadCallback(rv); |
| 973 | } |
| 974 | |
| 975 | bool SSLClientSocketOpenSSL::IsConnected() const { |
| 976 | bool ret = completed_handshake_ && transport_->socket()->IsConnected(); |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
| 981 | bool ret = completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | int SSLClientSocketOpenSSL::GetPeerAddress(AddressList* addressList) const { |
| 986 | return transport_->socket()->GetPeerAddress(addressList); |
| 987 | } |
| 988 | |
| 989 | const BoundNetLog& SSLClientSocketOpenSSL::NetLog() const { |
| 990 | return net_log_; |
| 991 | } |
| 992 | |
| 993 | void SSLClientSocketOpenSSL::SetSubresourceSpeculation() { |
| 994 | if (transport_.get() && transport_->socket()) { |
| 995 | transport_->socket()->SetSubresourceSpeculation(); |
| 996 | } else { |
| 997 | NOTREACHED(); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | void SSLClientSocketOpenSSL::SetOmniboxSpeculation() { |
| 1002 | if (transport_.get() && transport_->socket()) { |
| 1003 | transport_->socket()->SetOmniboxSpeculation(); |
| 1004 | } else { |
| 1005 | NOTREACHED(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | bool SSLClientSocketOpenSSL::WasEverUsed() const { |
| 1010 | if (transport_.get() && transport_->socket()) |
| 1011 | return transport_->socket()->WasEverUsed(); |
| 1012 | |
| 1013 | NOTREACHED(); |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 1017 | bool SSLClientSocketOpenSSL::UsingTCPFastOpen() const { |
| 1018 | if (transport_.get() && transport_->socket()) |
| 1019 | return transport_->socket()->UsingTCPFastOpen(); |
| 1020 | |
| 1021 | NOTREACHED(); |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1025 | // Socket methods |
| 1026 | |
| 1027 | int SSLClientSocketOpenSSL::Read(IOBuffer* buf, |
| 1028 | int buf_len, |
| 1029 | CompletionCallback* callback) { |
| 1030 | user_read_buf_ = buf; |
| 1031 | user_read_buf_len_ = buf_len; |
| 1032 | |
| 1033 | int rv = DoReadLoop(OK); |
| 1034 | |
| 1035 | if (rv == ERR_IO_PENDING) { |
| 1036 | user_read_callback_ = callback; |
| 1037 | } else { |
| 1038 | user_read_buf_ = NULL; |
| 1039 | user_read_buf_len_ = 0; |
| 1040 | } |
| 1041 | |
| 1042 | return rv; |
| 1043 | } |
| 1044 | |
| 1045 | int SSLClientSocketOpenSSL::DoReadLoop(int result) { |
| 1046 | if (result < 0) |
| 1047 | return result; |
| 1048 | |
| 1049 | bool network_moved; |
| 1050 | int rv; |
| 1051 | do { |
| 1052 | rv = DoPayloadRead(); |
| 1053 | network_moved = DoTransportIO(); |
| 1054 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1055 | |
| 1056 | return rv; |
| 1057 | } |
| 1058 | |
| 1059 | int SSLClientSocketOpenSSL::Write(IOBuffer* buf, |
| 1060 | int buf_len, |
| 1061 | CompletionCallback* callback) { |
| 1062 | user_write_buf_ = buf; |
| 1063 | user_write_buf_len_ = buf_len; |
| 1064 | |
| 1065 | int rv = DoWriteLoop(OK); |
| 1066 | |
| 1067 | if (rv == ERR_IO_PENDING) { |
| 1068 | user_write_callback_ = callback; |
| 1069 | } else { |
| 1070 | user_write_buf_ = NULL; |
| 1071 | user_write_buf_len_ = 0; |
| 1072 | } |
| 1073 | |
| 1074 | return rv; |
| 1075 | } |
| 1076 | |
| 1077 | int SSLClientSocketOpenSSL::DoWriteLoop(int result) { |
| 1078 | if (result < 0) |
| 1079 | return result; |
| 1080 | |
| 1081 | bool network_moved; |
| 1082 | int rv; |
| 1083 | do { |
| 1084 | rv = DoPayloadWrite(); |
| 1085 | network_moved = DoTransportIO(); |
| 1086 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1087 | |
| 1088 | return rv; |
| 1089 | } |
| 1090 | |
| 1091 | bool SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { |
| 1092 | return transport_->socket()->SetReceiveBufferSize(size); |
| 1093 | } |
| 1094 | |
| 1095 | bool SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { |
| 1096 | return transport_->socket()->SetSendBufferSize(size); |
| 1097 | } |
| 1098 | |
| 1099 | int SSLClientSocketOpenSSL::DoPayloadRead() { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 1100 | base::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1101 | int rv = SSL_read(ssl_, user_read_buf_->data(), user_read_buf_len_); |
| 1102 | // We don't need to invalidate the non-client-authenticated SSL session |
| 1103 | // because the server will renegotiate anyway. |
| 1104 | if (client_auth_cert_needed_) |
| 1105 | return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1106 | |
| 1107 | if (rv >= 0) |
| 1108 | return rv; |
| 1109 | |
| 1110 | int err = SSL_get_error(ssl_, rv); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 1111 | return MapOpenSSLError(err, err_tracer); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | int SSLClientSocketOpenSSL::DoPayloadWrite() { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 1115 | base::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1116 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
| 1117 | |
| 1118 | if (rv >= 0) |
| 1119 | return rv; |
| 1120 | |
| 1121 | int err = SSL_get_error(ssl_, rv); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 1122 | return MapOpenSSLError(err, err_tracer); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | } // namespace net |