[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [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 | |
| 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 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 10 | #include <openssl/err.h> |
[email protected] | 8903815 | 2012-09-07 06:30:17 | [diff] [blame] | 11 | #include <openssl/opensslv.h> |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 12 | #include <openssl/ssl.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 13 | |
[email protected] | 0f7804ec | 2011-10-07 20:04:18 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | f2da6ac | 2013-02-04 08:22:53 | [diff] [blame] | 15 | #include "base/callback_helpers.h" |
[email protected] | b74d0f5 | 2014-01-23 00:44:40 | [diff] [blame] | 16 | #include "base/debug/alias.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/singleton.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 18 | #include "base/metrics/histogram.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 20 | #include "crypto/ec_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 21 | #include "crypto/openssl_util.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 22 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 23 | #include "net/cert/cert_verifier.h" |
| 24 | #include "net/cert/single_request_cert_verifier.h" |
| 25 | #include "net/cert/x509_certificate_net_log_param.h" |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 26 | #include "net/socket/ssl_error_params.h" |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 27 | #include "net/socket/ssl_session_cache_openssl.h" |
[email protected] | 5cf6759 | 2013-04-02 17:42:12 | [diff] [blame] | 28 | #include "net/ssl/openssl_client_key_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 29 | #include "net/ssl/ssl_cert_request_info.h" |
| 30 | #include "net/ssl/ssl_connection_status_flags.h" |
| 31 | #include "net/ssl/ssl_info.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 32 | |
| 33 | namespace net { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // Enable this to see logging for state machine state transitions. |
| 38 | #if 0 |
[email protected] | 3b11277 | 2010-10-04 10:54:49 | [diff] [blame] | 39 | #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 40 | " jump to state " << s; \ |
| 41 | next_handshake_state_ = s; } while (0) |
| 42 | #else |
| 43 | #define GotoState(s) next_handshake_state_ = s |
| 44 | #endif |
| 45 | |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 46 | // This constant can be any non-negative/non-zero value (eg: it does not |
| 47 | // overlap with any value of the net::Error range, including net::OK). |
| 48 | const int kNoPendingReadResult = 1; |
| 49 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 50 | // If a client doesn't have a list of protocols that it supports, but |
| 51 | // the server supports NPN, choosing "http/1.1" is the best answer. |
| 52 | const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
| 53 | |
[email protected] | 8903815 | 2012-09-07 06:30:17 | [diff] [blame] | 54 | #if OPENSSL_VERSION_NUMBER < 0x1000103fL |
| 55 | // This method doesn't seem to have made it into the OpenSSL headers. |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 56 | unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } |
[email protected] | 8903815 | 2012-09-07 06:30:17 | [diff] [blame] | 57 | #endif |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 58 | |
| 59 | // Used for encoding the |connection_status| field of an SSLInfo object. |
| 60 | int EncodeSSLConnectionStatus(int cipher_suite, |
| 61 | int compression, |
| 62 | int version) { |
| 63 | return ((cipher_suite & SSL_CONNECTION_CIPHERSUITE_MASK) << |
| 64 | SSL_CONNECTION_CIPHERSUITE_SHIFT) | |
| 65 | ((compression & SSL_CONNECTION_COMPRESSION_MASK) << |
| 66 | SSL_CONNECTION_COMPRESSION_SHIFT) | |
| 67 | ((version & SSL_CONNECTION_VERSION_MASK) << |
| 68 | SSL_CONNECTION_VERSION_SHIFT); |
| 69 | } |
| 70 | |
| 71 | // Returns the net SSL version number (see ssl_connection_status_flags.h) for |
| 72 | // this SSL connection. |
| 73 | int GetNetSSLVersion(SSL* ssl) { |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 74 | switch (SSL_version(ssl)) { |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 75 | case SSL2_VERSION: |
| 76 | return SSL_CONNECTION_VERSION_SSL2; |
| 77 | case SSL3_VERSION: |
| 78 | return SSL_CONNECTION_VERSION_SSL3; |
| 79 | case TLS1_VERSION: |
| 80 | return SSL_CONNECTION_VERSION_TLS1; |
| 81 | case 0x0302: |
| 82 | return SSL_CONNECTION_VERSION_TLS1_1; |
| 83 | case 0x0303: |
| 84 | return SSL_CONNECTION_VERSION_TLS1_2; |
| 85 | default: |
| 86 | return SSL_CONNECTION_VERSION_UNKNOWN; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | int MapOpenSSLErrorSSL() { |
| 91 | // Walk down the error stack to find the SSLerr generated reason. |
| 92 | unsigned long error_code; |
| 93 | do { |
| 94 | error_code = ERR_get_error(); |
| 95 | if (error_code == 0) |
| 96 | return ERR_SSL_PROTOCOL_ERROR; |
| 97 | } while (ERR_GET_LIB(error_code) != ERR_LIB_SSL); |
| 98 | |
| 99 | DVLOG(1) << "OpenSSL SSL error, reason: " << ERR_GET_REASON(error_code) |
| 100 | << ", name: " << ERR_error_string(error_code, NULL); |
| 101 | switch (ERR_GET_REASON(error_code)) { |
| 102 | case SSL_R_READ_TIMEOUT_EXPIRED: |
| 103 | return ERR_TIMED_OUT; |
| 104 | case SSL_R_BAD_RESPONSE_ARGUMENT: |
| 105 | return ERR_INVALID_ARGUMENT; |
| 106 | case SSL_R_UNKNOWN_CERTIFICATE_TYPE: |
| 107 | case SSL_R_UNKNOWN_CIPHER_TYPE: |
| 108 | case SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE: |
| 109 | case SSL_R_UNKNOWN_PKEY_TYPE: |
| 110 | case SSL_R_UNKNOWN_REMOTE_ERROR_TYPE: |
| 111 | case SSL_R_UNKNOWN_SSL_VERSION: |
| 112 | return ERR_NOT_IMPLEMENTED; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 113 | case SSL_R_UNSUPPORTED_SSL_VERSION: |
| 114 | case SSL_R_NO_CIPHER_MATCH: |
| 115 | case SSL_R_NO_SHARED_CIPHER: |
| 116 | case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: |
| 117 | case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: |
[email protected] | 8bc5092 | 2012-12-10 19:39:43 | [diff] [blame] | 118 | case SSL_R_UNSUPPORTED_PROTOCOL: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 119 | return ERR_SSL_VERSION_OR_CIPHER_MISMATCH; |
| 120 | case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: |
| 121 | case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: |
| 122 | case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: |
| 123 | case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: |
| 124 | case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: |
| 125 | case SSL_R_TLSV1_ALERT_ACCESS_DENIED: |
| 126 | case SSL_R_TLSV1_ALERT_UNKNOWN_CA: |
| 127 | return ERR_BAD_SSL_CLIENT_AUTH_CERT; |
| 128 | case SSL_R_BAD_DECOMPRESSION: |
| 129 | case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: |
| 130 | return ERR_SSL_DECOMPRESSION_FAILURE_ALERT; |
| 131 | case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: |
| 132 | return ERR_SSL_BAD_RECORD_MAC_ALERT; |
[email protected] | e1c53dd6 | 2013-06-14 20:58:36 | [diff] [blame] | 133 | case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: |
| 134 | return ERR_SSL_DECRYPT_ERROR_ALERT; |
[email protected] | 7b8b0c0 | 2014-02-05 21:40:03 | [diff] [blame] | 135 | case SSL_R_TLSV1_UNRECOGNIZED_NAME: |
| 136 | return ERR_SSL_UNRECOGNIZED_NAME_ALERT; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 137 | case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: |
| 138 | return ERR_SSL_UNSAFE_NEGOTIATION; |
| 139 | case SSL_R_WRONG_NUMBER_OF_KEY_BITS: |
| 140 | return ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY; |
[email protected] | aa4bb689 | 2010-12-08 10:52:02 | [diff] [blame] | 141 | // SSL_R_UNKNOWN_PROTOCOL is reported if premature application data is |
| 142 | // received (see http://crbug.com/42538), and also if all the protocol |
| 143 | // versions supported by the server were disabled in this socket instance. |
| 144 | // Mapped to ERR_SSL_PROTOCOL_ERROR for compatibility with other SSL sockets |
| 145 | // in the former scenario. |
| 146 | case SSL_R_UNKNOWN_PROTOCOL: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 147 | case SSL_R_SSL_HANDSHAKE_FAILURE: |
| 148 | case SSL_R_DECRYPTION_FAILED: |
| 149 | case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: |
| 150 | case SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG: |
| 151 | case SSL_R_DIGEST_CHECK_FAILED: |
| 152 | case SSL_R_DUPLICATE_COMPRESSION_ID: |
| 153 | case SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER: |
| 154 | case SSL_R_ENCRYPTED_LENGTH_TOO_LONG: |
| 155 | case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: |
| 156 | case SSL_R_EXCESSIVE_MESSAGE_SIZE: |
| 157 | case SSL_R_EXTRA_DATA_IN_MESSAGE: |
| 158 | case SSL_R_GOT_A_FIN_BEFORE_A_CCS: |
| 159 | case SSL_R_ILLEGAL_PADDING: |
| 160 | case SSL_R_INVALID_CHALLENGE_LENGTH: |
| 161 | case SSL_R_INVALID_COMMAND: |
| 162 | case SSL_R_INVALID_PURPOSE: |
| 163 | case SSL_R_INVALID_STATUS_RESPONSE: |
| 164 | case SSL_R_INVALID_TICKET_KEYS_LENGTH: |
| 165 | case SSL_R_KEY_ARG_TOO_LONG: |
| 166 | case SSL_R_READ_WRONG_PACKET_TYPE: |
[email protected] | 63e46864 | 2013-11-28 05:18:02 | [diff] [blame] | 167 | // SSL_do_handshake reports this error when the server responds to a |
| 168 | // ClientHello with a fatal close_notify alert. |
| 169 | case SSL_AD_REASON_OFFSET + SSL_AD_CLOSE_NOTIFY: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 170 | case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: |
| 171 | // TODO(joth): SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE may be returned from the |
| 172 | // server after receiving ClientHello if there's no common supported cipher. |
| 173 | // Ideally we'd map that specific case to ERR_SSL_VERSION_OR_CIPHER_MISMATCH |
| 174 | // to match the NSS implementation. See also http://goo.gl/oMtZW |
| 175 | case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: |
| 176 | case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: |
| 177 | case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: |
| 178 | case SSL_R_TLSV1_ALERT_DECODE_ERROR: |
| 179 | case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 180 | case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: |
| 181 | case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: |
| 182 | case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: |
| 183 | case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: |
| 184 | case SSL_R_TLSV1_ALERT_USER_CANCELLED: |
| 185 | return ERR_SSL_PROTOCOL_ERROR; |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 186 | case SSL_R_CERTIFICATE_VERIFY_FAILED: |
| 187 | // The only way that the certificate verify callback can fail is if |
| 188 | // the leaf certificate changed during a renegotiation. |
| 189 | return ERR_SSL_SERVER_CERT_CHANGED; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 190 | default: |
| 191 | LOG(WARNING) << "Unmapped error reason: " << ERR_GET_REASON(error_code); |
| 192 | return ERR_FAILED; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Converts an OpenSSL error code into a net error code, walking the OpenSSL |
| 197 | // error stack if needed. Note that |tracer| is not currently used in the |
| 198 | // implementation, but is passed in anyway as this ensures the caller will clear |
| 199 | // any residual codes left on the error stack. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 200 | int MapOpenSSLError(int err, const crypto::OpenSSLErrStackTracer& tracer) { |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 201 | switch (err) { |
| 202 | case SSL_ERROR_WANT_READ: |
| 203 | case SSL_ERROR_WANT_WRITE: |
| 204 | return ERR_IO_PENDING; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 205 | case SSL_ERROR_SYSCALL: |
[email protected] | 038f9e2 | 2013-06-20 23:01:18 | [diff] [blame] | 206 | LOG(ERROR) << "OpenSSL SYSCALL error, earliest error code in " |
| 207 | "error queue: " << ERR_peek_error() << ", errno: " |
| 208 | << errno; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 209 | return ERR_SSL_PROTOCOL_ERROR; |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 210 | case SSL_ERROR_SSL: |
| 211 | return MapOpenSSLErrorSSL(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 212 | default: |
| 213 | // TODO(joth): Implement full mapping. |
| 214 | LOG(WARNING) << "Unknown OpenSSL error " << err; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 215 | return ERR_SSL_PROTOCOL_ERROR; |
| 216 | } |
| 217 | } |
| 218 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 219 | // Utility to construct the appropriate set & clear masks for use the OpenSSL |
| 220 | // options and mode configuration functions. (SSL_set_options etc) |
| 221 | struct SslSetClearMask { |
| 222 | SslSetClearMask() : set_mask(0), clear_mask(0) {} |
| 223 | void ConfigureFlag(long flag, bool state) { |
| 224 | (state ? set_mask : clear_mask) |= flag; |
| 225 | // Make sure we haven't got any intersection in the set & clear options. |
| 226 | DCHECK_EQ(0, set_mask & clear_mask) << flag << ":" << state; |
| 227 | } |
| 228 | long set_mask; |
| 229 | long clear_mask; |
| 230 | }; |
| 231 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 232 | // Compute a unique key string for the SSL session cache. |socket| is an |
| 233 | // input socket object. Return a string. |
| 234 | std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) { |
| 235 | std::string result = socket.host_and_port().ToString(); |
| 236 | result.append("/"); |
| 237 | result.append(socket.ssl_session_cache_shard()); |
| 238 | return result; |
| 239 | } |
| 240 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 241 | } // namespace |
| 242 | |
| 243 | class SSLClientSocketOpenSSL::SSLContext { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 244 | public: |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 245 | static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 246 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 247 | SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 248 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 249 | SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 250 | DCHECK(ssl); |
| 251 | SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( |
| 252 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 253 | DCHECK(socket); |
| 254 | return socket; |
| 255 | } |
| 256 | |
| 257 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { |
| 258 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 259 | } |
| 260 | |
| 261 | private: |
| 262 | friend struct DefaultSingletonTraits<SSLContext>; |
| 263 | |
| 264 | SSLContext() { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 265 | crypto::EnsureOpenSSLInit(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 266 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 267 | DCHECK_NE(ssl_socket_data_index_, -1); |
| 268 | ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 269 | session_cache_.Reset(ssl_ctx_.get(), kDefaultSessionCacheConfig); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 270 | SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 271 | SSL_CTX_set_client_cert_cb(ssl_ctx_.get(), ClientCertCallback); |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 272 | SSL_CTX_set_channel_id_cb(ssl_ctx_.get(), ChannelIDCallback); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 273 | SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 274 | // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty. |
| 275 | // It would be better if the callback were not a global setting, |
| 276 | // but that is an OpenSSL issue. |
| 277 | SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
| 278 | NULL); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 281 | static std::string GetSessionCacheKey(const SSL* ssl) { |
| 282 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 283 | DCHECK(socket); |
| 284 | return GetSocketSessionCacheKey(*socket); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 287 | static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 288 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 289 | static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 290 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 291 | CHECK(socket); |
| 292 | return socket->ClientCertRequestCallback(ssl, x509, pkey); |
| 293 | } |
| 294 | |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 295 | static void ChannelIDCallback(SSL* ssl, EVP_PKEY** pkey) { |
| 296 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 297 | CHECK(socket); |
| 298 | socket->ChannelIDRequestCallback(ssl, pkey); |
| 299 | } |
| 300 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 301 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 302 | SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
| 303 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 304 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 305 | CHECK(socket); |
| 306 | |
| 307 | return socket->CertVerifyCallback(store_ctx); |
| 308 | } |
| 309 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 310 | static int SelectNextProtoCallback(SSL* ssl, |
| 311 | unsigned char** out, unsigned char* outlen, |
| 312 | const unsigned char* in, |
| 313 | unsigned int inlen, void* arg) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 314 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 315 | return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
| 316 | } |
| 317 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 318 | // This is the index used with SSL_get_ex_data to retrieve the owner |
| 319 | // SSLClientSocketOpenSSL object from an SSL instance. |
| 320 | int ssl_socket_data_index_; |
| 321 | |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 322 | crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 323 | // |session_cache_| must be destroyed before |ssl_ctx_|. |
| 324 | SSLSessionCacheOpenSSL session_cache_; |
| 325 | }; |
| 326 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 327 | // PeerCertificateChain is a helper object which extracts the certificate |
| 328 | // chain, as given by the server, from an OpenSSL socket and performs the needed |
| 329 | // resource management. The first element of the chain is the leaf certificate |
| 330 | // and the other elements are in the order given by the server. |
| 331 | class SSLClientSocketOpenSSL::PeerCertificateChain { |
| 332 | public: |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 333 | explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 334 | PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
| 335 | ~PeerCertificateChain() {} |
| 336 | PeerCertificateChain& operator=(const PeerCertificateChain& other); |
| 337 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 338 | // Resets the PeerCertificateChain to the set of certificates in|chain|, |
| 339 | // which may be NULL, indicating to empty the store certificates. |
| 340 | // Note: If an error occurs, such as being unable to parse the certificates, |
| 341 | // this will behave as if Reset(NULL) was called. |
| 342 | void Reset(STACK_OF(X509)* chain); |
| 343 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 344 | // Note that when USE_OPENSSL is defined, OSCertHandle is X509* |
| 345 | const scoped_refptr<X509Certificate>& AsOSChain() const { return os_chain_; } |
| 346 | |
| 347 | size_t size() const { |
| 348 | if (!openssl_chain_.get()) |
| 349 | return 0; |
| 350 | return sk_X509_num(openssl_chain_.get()); |
| 351 | } |
| 352 | |
| 353 | X509* operator[](size_t index) const { |
| 354 | DCHECK_LT(index, size()); |
| 355 | return sk_X509_value(openssl_chain_.get(), index); |
| 356 | } |
| 357 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 358 | bool IsValid() { return os_chain_.get() && openssl_chain_.get(); } |
| 359 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 360 | private: |
| 361 | static void FreeX509Stack(STACK_OF(X509)* cert_chain) { |
| 362 | sk_X509_pop_free(cert_chain, X509_free); |
| 363 | } |
| 364 | |
| 365 | friend class crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>; |
| 366 | |
| 367 | crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_; |
| 368 | |
| 369 | scoped_refptr<X509Certificate> os_chain_; |
| 370 | }; |
| 371 | |
| 372 | SSLClientSocketOpenSSL::PeerCertificateChain& |
| 373 | SSLClientSocketOpenSSL::PeerCertificateChain::operator=( |
| 374 | const PeerCertificateChain& other) { |
| 375 | if (this == &other) |
| 376 | return *this; |
| 377 | |
| 378 | // os_chain_ is reference counted by scoped_refptr; |
| 379 | os_chain_ = other.os_chain_; |
| 380 | |
| 381 | // Must increase the reference count manually for sk_X509_dup |
| 382 | openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); |
| 383 | for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 384 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
| 385 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 386 | } |
| 387 | return *this; |
| 388 | } |
| 389 | |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 390 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 391 | // When OSCertHandle is typedef'ed to X509, this implementation does a short cut |
| 392 | // to avoid converting back and forth between der and X509 struct. |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 393 | void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
| 394 | STACK_OF(X509)* chain) { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 395 | openssl_chain_.reset(NULL); |
| 396 | os_chain_ = NULL; |
| 397 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 398 | if (!chain) |
| 399 | return; |
| 400 | |
| 401 | X509Certificate::OSCertHandles intermediates; |
| 402 | for (int i = 1; i < sk_X509_num(chain); ++i) |
| 403 | intermediates.push_back(sk_X509_value(chain, i)); |
| 404 | |
| 405 | os_chain_ = |
| 406 | X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates); |
| 407 | |
| 408 | // sk_X509_dup does not increase reference count on the certs in the stack. |
| 409 | openssl_chain_.reset(sk_X509_dup(chain)); |
| 410 | |
| 411 | std::vector<base::StringPiece> der_chain; |
| 412 | for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 413 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
| 414 | // Increase the reference count for the certs in openssl_chain_. |
| 415 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 416 | } |
| 417 | } |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 418 | #else // !defined(USE_OPENSSL_CERTS) |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 419 | void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
| 420 | STACK_OF(X509)* chain) { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 421 | openssl_chain_.reset(NULL); |
| 422 | os_chain_ = NULL; |
| 423 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 424 | if (!chain) |
| 425 | return; |
| 426 | |
| 427 | // sk_X509_dup does not increase reference count on the certs in the stack. |
| 428 | openssl_chain_.reset(sk_X509_dup(chain)); |
| 429 | |
| 430 | std::vector<base::StringPiece> der_chain; |
| 431 | for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 432 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
| 433 | |
| 434 | // Increase the reference count for the certs in openssl_chain_. |
| 435 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 436 | |
| 437 | unsigned char* cert_data = NULL; |
| 438 | int cert_data_length = i2d_X509(x, &cert_data); |
| 439 | if (cert_data_length && cert_data) |
| 440 | der_chain.push_back(base::StringPiece(reinterpret_cast<char*>(cert_data), |
| 441 | cert_data_length)); |
| 442 | } |
| 443 | |
| 444 | os_chain_ = X509Certificate::CreateFromDERCertChain(der_chain); |
| 445 | |
| 446 | for (size_t i = 0; i < der_chain.size(); ++i) { |
| 447 | OPENSSL_free(const_cast<char*>(der_chain[i].data())); |
| 448 | } |
| 449 | |
| 450 | if (der_chain.size() != |
| 451 | static_cast<size_t>(sk_X509_num(openssl_chain_.get()))) { |
| 452 | openssl_chain_.reset(NULL); |
| 453 | os_chain_ = NULL; |
| 454 | } |
| 455 | } |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 456 | #endif // defined(USE_OPENSSL_CERTS) |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 457 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 458 | // static |
| 459 | SSLSessionCacheOpenSSL::Config |
| 460 | SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { |
| 461 | &GetSessionCacheKey, // key_func |
| 462 | 1024, // max_entries |
| 463 | 256, // expiration_check_count |
| 464 | 60 * 60, // timeout_seconds |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 465 | }; |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 466 | |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 467 | // static |
| 468 | void SSLClientSocket::ClearSessionCache() { |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 469 | SSLClientSocketOpenSSL::SSLContext* context = |
| 470 | SSLClientSocketOpenSSL::SSLContext::GetInstance(); |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 471 | context->session_cache()->Flush(); |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 472 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | f3875bf | 2014-02-17 18:48:25 | [diff] [blame] | 473 | OpenSSLClientKeyStore::GetInstance()->Flush(); |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 474 | #endif |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 477 | SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 478 | scoped_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 479 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 480 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 481 | const SSLClientSocketContext& context) |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 482 | : transport_send_busy_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 483 | transport_recv_busy_(false), |
[email protected] | a85197e | 2012-05-22 19:07:28 | [diff] [blame] | 484 | transport_recv_eof_(false), |
[email protected] | be90ba3 | 2013-05-13 20:05:25 | [diff] [blame] | 485 | weak_factory_(this), |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 486 | pending_read_error_(kNoPendingReadResult), |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 487 | transport_write_error_(OK), |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 488 | server_cert_chain_(new PeerCertificateChain(NULL)), |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 489 | completed_handshake_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 490 | was_ever_used_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 491 | client_auth_cert_needed_(false), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 492 | cert_verifier_(context.cert_verifier), |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 493 | server_bound_cert_service_(context.server_bound_cert_service), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 494 | ssl_(NULL), |
| 495 | transport_bio_(NULL), |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 496 | transport_(transport_socket.Pass()), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 497 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 498 | ssl_config_(ssl_config), |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 499 | ssl_session_cache_shard_(context.ssl_session_cache_shard), |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 500 | trying_cached_session_(false), |
[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 501 | next_handshake_state_(STATE_NONE), |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 502 | npn_status_(kNextProtoUnsupported), |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 503 | channel_id_request_return_value_(ERR_UNEXPECTED), |
| 504 | channel_id_xtn_negotiated_(false), |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 505 | net_log_(transport_->socket()->NetLog()) {} |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 506 | |
| 507 | SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 508 | Disconnect(); |
| 509 | } |
| 510 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 511 | void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 512 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 513 | cert_request_info->host_and_port = host_and_port_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 514 | cert_request_info->cert_authorities = cert_authorities_; |
| 515 | } |
| 516 | |
| 517 | SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
| 518 | std::string* proto, std::string* server_protos) { |
| 519 | *proto = npn_proto_; |
| 520 | *server_protos = server_protos_; |
| 521 | return npn_status_; |
| 522 | } |
| 523 | |
| 524 | ServerBoundCertService* |
| 525 | SSLClientSocketOpenSSL::GetServerBoundCertService() const { |
| 526 | return server_bound_cert_service_; |
| 527 | } |
| 528 | |
| 529 | int SSLClientSocketOpenSSL::ExportKeyingMaterial( |
| 530 | const base::StringPiece& label, |
| 531 | bool has_context, const base::StringPiece& context, |
| 532 | unsigned char* out, unsigned int outlen) { |
| 533 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 534 | |
| 535 | int rv = SSL_export_keying_material( |
| 536 | ssl_, out, outlen, const_cast<char*>(label.data()), |
| 537 | label.size(), |
| 538 | reinterpret_cast<unsigned char*>(const_cast<char*>(context.data())), |
| 539 | context.length(), |
| 540 | context.length() > 0); |
| 541 | |
| 542 | if (rv != 1) { |
| 543 | int ssl_error = SSL_get_error(ssl_, rv); |
| 544 | LOG(ERROR) << "Failed to export keying material;" |
| 545 | << " returned " << rv |
| 546 | << ", SSL error code " << ssl_error; |
| 547 | return MapOpenSSLError(ssl_error, err_tracer); |
| 548 | } |
| 549 | return OK; |
| 550 | } |
| 551 | |
| 552 | int SSLClientSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { |
| 553 | return ERR_NOT_IMPLEMENTED; |
| 554 | } |
| 555 | |
| 556 | int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { |
| 557 | net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); |
| 558 | |
| 559 | // Set up new ssl object. |
| 560 | if (!Init()) { |
| 561 | int result = ERR_UNEXPECTED; |
| 562 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, result); |
| 563 | return result; |
| 564 | } |
| 565 | |
| 566 | // Set SSL to client mode. Handshake happens in the loop below. |
| 567 | SSL_set_connect_state(ssl_); |
| 568 | |
| 569 | GotoState(STATE_HANDSHAKE); |
| 570 | int rv = DoHandshakeLoop(net::OK); |
| 571 | if (rv == ERR_IO_PENDING) { |
| 572 | user_connect_callback_ = callback; |
| 573 | } else { |
| 574 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 575 | } |
| 576 | |
| 577 | return rv > OK ? OK : rv; |
| 578 | } |
| 579 | |
| 580 | void SSLClientSocketOpenSSL::Disconnect() { |
| 581 | if (ssl_) { |
| 582 | // Calling SSL_shutdown prevents the session from being marked as |
| 583 | // unresumable. |
| 584 | SSL_shutdown(ssl_); |
| 585 | SSL_free(ssl_); |
| 586 | ssl_ = NULL; |
| 587 | } |
| 588 | if (transport_bio_) { |
| 589 | BIO_free_all(transport_bio_); |
| 590 | transport_bio_ = NULL; |
| 591 | } |
| 592 | |
| 593 | // Shut down anything that may call us back. |
| 594 | verifier_.reset(); |
| 595 | transport_->socket()->Disconnect(); |
| 596 | |
| 597 | // Null all callbacks, delete all buffers. |
| 598 | transport_send_busy_ = false; |
| 599 | send_buffer_ = NULL; |
| 600 | transport_recv_busy_ = false; |
| 601 | transport_recv_eof_ = false; |
| 602 | recv_buffer_ = NULL; |
| 603 | |
| 604 | user_connect_callback_.Reset(); |
| 605 | user_read_callback_.Reset(); |
| 606 | user_write_callback_.Reset(); |
| 607 | user_read_buf_ = NULL; |
| 608 | user_read_buf_len_ = 0; |
| 609 | user_write_buf_ = NULL; |
| 610 | user_write_buf_len_ = 0; |
| 611 | |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 612 | pending_read_error_ = kNoPendingReadResult; |
| 613 | transport_write_error_ = OK; |
| 614 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 615 | server_cert_verify_result_.Reset(); |
| 616 | completed_handshake_ = false; |
| 617 | |
| 618 | cert_authorities_.clear(); |
| 619 | client_auth_cert_needed_ = false; |
| 620 | } |
| 621 | |
| 622 | bool SSLClientSocketOpenSSL::IsConnected() const { |
| 623 | // If the handshake has not yet completed. |
| 624 | if (!completed_handshake_) |
| 625 | return false; |
| 626 | // If an asynchronous operation is still pending. |
| 627 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 628 | return true; |
| 629 | |
| 630 | return transport_->socket()->IsConnected(); |
| 631 | } |
| 632 | |
| 633 | bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
| 634 | // If the handshake has not yet completed. |
| 635 | if (!completed_handshake_) |
| 636 | return false; |
| 637 | // If an asynchronous operation is still pending. |
| 638 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 639 | return false; |
| 640 | // If there is data waiting to be sent, or data read from the network that |
| 641 | // has not yet been consumed. |
| 642 | if (BIO_ctrl_pending(transport_bio_) > 0 || |
| 643 | BIO_ctrl_wpending(transport_bio_) > 0) { |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | return transport_->socket()->IsConnectedAndIdle(); |
| 648 | } |
| 649 | |
| 650 | int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { |
| 651 | return transport_->socket()->GetPeerAddress(addressList); |
| 652 | } |
| 653 | |
| 654 | int SSLClientSocketOpenSSL::GetLocalAddress(IPEndPoint* addressList) const { |
| 655 | return transport_->socket()->GetLocalAddress(addressList); |
| 656 | } |
| 657 | |
| 658 | const BoundNetLog& SSLClientSocketOpenSSL::NetLog() const { |
| 659 | return net_log_; |
| 660 | } |
| 661 | |
| 662 | void SSLClientSocketOpenSSL::SetSubresourceSpeculation() { |
| 663 | if (transport_.get() && transport_->socket()) { |
| 664 | transport_->socket()->SetSubresourceSpeculation(); |
| 665 | } else { |
| 666 | NOTREACHED(); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | void SSLClientSocketOpenSSL::SetOmniboxSpeculation() { |
| 671 | if (transport_.get() && transport_->socket()) { |
| 672 | transport_->socket()->SetOmniboxSpeculation(); |
| 673 | } else { |
| 674 | NOTREACHED(); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | bool SSLClientSocketOpenSSL::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 679 | return was_ever_used_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | bool SSLClientSocketOpenSSL::UsingTCPFastOpen() const { |
| 683 | if (transport_.get() && transport_->socket()) |
| 684 | return transport_->socket()->UsingTCPFastOpen(); |
| 685 | |
| 686 | NOTREACHED(); |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
| 691 | ssl_info->Reset(); |
| 692 | if (!server_cert_.get()) |
| 693 | return false; |
| 694 | |
| 695 | ssl_info->cert = server_cert_verify_result_.verified_cert; |
| 696 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
| 697 | ssl_info->is_issued_by_known_root = |
| 698 | server_cert_verify_result_.is_issued_by_known_root; |
| 699 | ssl_info->public_key_hashes = |
| 700 | server_cert_verify_result_.public_key_hashes; |
| 701 | ssl_info->client_cert_sent = |
| 702 | ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
| 703 | ssl_info->channel_id_sent = WasChannelIDSent(); |
| 704 | |
| 705 | RecordChannelIDSupport(server_bound_cert_service_, |
| 706 | channel_id_xtn_negotiated_, |
| 707 | ssl_config_.channel_id_enabled, |
| 708 | crypto::ECPrivateKey::IsSupported()); |
| 709 | |
| 710 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 711 | CHECK(cipher); |
| 712 | ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
| 713 | const COMP_METHOD* compression = SSL_get_current_compression(ssl_); |
| 714 | |
| 715 | ssl_info->connection_status = EncodeSSLConnectionStatus( |
| 716 | SSL_CIPHER_get_id(cipher), |
| 717 | compression ? compression->type : 0, |
| 718 | GetNetSSLVersion(ssl_)); |
| 719 | |
| 720 | bool peer_supports_renego_ext = !!SSL_get_secure_renegotiation_support(ssl_); |
| 721 | if (!peer_supports_renego_ext) |
| 722 | ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
| 723 | UMA_HISTOGRAM_ENUMERATION("Net.RenegotiationExtensionSupported", |
| 724 | implicit_cast<int>(peer_supports_renego_ext), 2); |
| 725 | |
| 726 | if (ssl_config_.version_fallback) |
| 727 | ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
| 728 | |
| 729 | ssl_info->handshake_type = SSL_session_reused(ssl_) ? |
| 730 | SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; |
| 731 | |
| 732 | DVLOG(3) << "Encoded connection status: cipher suite = " |
| 733 | << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) |
| 734 | << " version = " |
| 735 | << SSLConnectionStatusToVersion(ssl_info->connection_status); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | int SSLClientSocketOpenSSL::Read(IOBuffer* buf, |
| 740 | int buf_len, |
| 741 | const CompletionCallback& callback) { |
| 742 | user_read_buf_ = buf; |
| 743 | user_read_buf_len_ = buf_len; |
| 744 | |
| 745 | int rv = DoReadLoop(OK); |
| 746 | |
| 747 | if (rv == ERR_IO_PENDING) { |
| 748 | user_read_callback_ = callback; |
| 749 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 750 | if (rv > 0) |
| 751 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 752 | user_read_buf_ = NULL; |
| 753 | user_read_buf_len_ = 0; |
| 754 | } |
| 755 | |
| 756 | return rv; |
| 757 | } |
| 758 | |
| 759 | int SSLClientSocketOpenSSL::Write(IOBuffer* buf, |
| 760 | int buf_len, |
| 761 | const CompletionCallback& callback) { |
| 762 | user_write_buf_ = buf; |
| 763 | user_write_buf_len_ = buf_len; |
| 764 | |
| 765 | int rv = DoWriteLoop(OK); |
| 766 | |
| 767 | if (rv == ERR_IO_PENDING) { |
| 768 | user_write_callback_ = callback; |
| 769 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 770 | if (rv > 0) |
| 771 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 772 | user_write_buf_ = NULL; |
| 773 | user_write_buf_len_ = 0; |
| 774 | } |
| 775 | |
| 776 | return rv; |
| 777 | } |
| 778 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 779 | int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 780 | return transport_->socket()->SetReceiveBufferSize(size); |
| 781 | } |
| 782 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 783 | int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 784 | return transport_->socket()->SetSendBufferSize(size); |
| 785 | } |
| 786 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 787 | bool SSLClientSocketOpenSSL::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 788 | DCHECK(!ssl_); |
| 789 | DCHECK(!transport_bio_); |
| 790 | |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 791 | SSLContext* context = SSLContext::GetInstance(); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 792 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 793 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 794 | ssl_ = SSL_new(context->ssl_ctx()); |
| 795 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 796 | return false; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 797 | |
| 798 | if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
| 799 | return false; |
| 800 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 801 | trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( |
| 802 | ssl_, GetSocketSessionCacheKey(*this)); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 803 | |
| 804 | BIO* ssl_bio = NULL; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 805 | // 0 => use default buffer sizes. |
| 806 | if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 807 | return false; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 808 | DCHECK(ssl_bio); |
| 809 | DCHECK(transport_bio_); |
| 810 | |
| 811 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 812 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 813 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 814 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 815 | SslSetClearMask options; |
| 816 | options.ConfigureFlag(SSL_OP_NO_SSLv2, true); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 817 | bool ssl3_enabled = (ssl_config_.version_min == SSL_PROTOCOL_VERSION_SSL3); |
| 818 | options.ConfigureFlag(SSL_OP_NO_SSLv3, !ssl3_enabled); |
| 819 | bool tls1_enabled = (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1 && |
| 820 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1); |
| 821 | options.ConfigureFlag(SSL_OP_NO_TLSv1, !tls1_enabled); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 822 | bool tls1_1_enabled = |
| 823 | (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1_1 && |
| 824 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1_1); |
| 825 | options.ConfigureFlag(SSL_OP_NO_TLSv1_1, !tls1_1_enabled); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 826 | bool tls1_2_enabled = |
| 827 | (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1_2 && |
| 828 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1_2); |
| 829 | options.ConfigureFlag(SSL_OP_NO_TLSv1_2, !tls1_2_enabled); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 830 | |
[email protected] | d0f0049 | 2012-08-03 22:35:13 | [diff] [blame] | 831 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 832 | |
| 833 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 834 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 835 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 836 | SSL_set_options(ssl_, options.set_mask); |
| 837 | SSL_clear_options(ssl_, options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 838 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 839 | // Same as above, this time for the SSL mode. |
| 840 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 841 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 842 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 843 | |
[email protected] | b788de0 | 2014-04-23 18:06:07 | [diff] [blame^] | 844 | mode.ConfigureFlag(SSL_MODE_HANDSHAKE_CUTTHROUGH, |
| 845 | ssl_config_.false_start_enabled); |
| 846 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 847 | SSL_set_mode(ssl_, mode.set_mask); |
| 848 | SSL_clear_mode(ssl_, mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 849 | |
| 850 | // Removing ciphers by ID from OpenSSL is a bit involved as we must use the |
| 851 | // textual name with SSL_set_cipher_list because there is no public API to |
| 852 | // directly remove a cipher by ID. |
| 853 | STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); |
| 854 | DCHECK(ciphers); |
| 855 | // See SSLConfig::disabled_cipher_suites for description of the suites |
[email protected] | 9b4bc4a9 | 2013-08-20 22:59:07 | [diff] [blame] | 856 | // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 |
| 857 | // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
| 858 | // as the handshake hash. |
| 859 | std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:" |
| 860 | "!aECDH:!AESGCM+AES256"); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 861 | // Walk through all the installed ciphers, seeing if any need to be |
| 862 | // appended to the cipher removal |command|. |
| 863 | for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { |
| 864 | const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 865 | const uint16 id = SSL_CIPHER_get_id(cipher); |
| 866 | // Remove any ciphers with a strength of less than 80 bits. Note the NSS |
| 867 | // implementation uses "effective" bits here but OpenSSL does not provide |
| 868 | // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, |
| 869 | // both of which are greater than 80 anyway. |
| 870 | bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; |
| 871 | if (!disable) { |
| 872 | disable = std::find(ssl_config_.disabled_cipher_suites.begin(), |
| 873 | ssl_config_.disabled_cipher_suites.end(), id) != |
| 874 | ssl_config_.disabled_cipher_suites.end(); |
| 875 | } |
| 876 | if (disable) { |
| 877 | const char* name = SSL_CIPHER_get_name(cipher); |
| 878 | DVLOG(3) << "Found cipher to remove: '" << name << "', ID: " << id |
| 879 | << " strength: " << SSL_CIPHER_get_bits(cipher, NULL); |
| 880 | command.append(":!"); |
| 881 | command.append(name); |
| 882 | } |
| 883 | } |
| 884 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 885 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 886 | // This will almost certainly result in the socket failing to complete the |
| 887 | // handshake at which point the appropriate error is bubbled up to the client. |
| 888 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
| 889 | "returned " << rv; |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 890 | |
| 891 | // TLS channel ids. |
| 892 | if (IsChannelIDEnabled(ssl_config_, server_bound_cert_service_)) { |
| 893 | SSL_enable_tls_channel_id(ssl_); |
| 894 | } |
| 895 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 896 | return true; |
| 897 | } |
| 898 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 899 | void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
| 900 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 901 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 902 | if (rv > 0) |
| 903 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 904 | user_read_buf_ = NULL; |
| 905 | user_read_buf_len_ = 0; |
| 906 | base::ResetAndReturn(&user_read_callback_).Run(rv); |
| 907 | } |
| 908 | |
| 909 | void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
| 910 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 911 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 912 | if (rv > 0) |
| 913 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 914 | user_write_buf_ = NULL; |
| 915 | user_write_buf_len_ = 0; |
| 916 | base::ResetAndReturn(&user_write_callback_).Run(rv); |
| 917 | } |
| 918 | |
| 919 | bool SSLClientSocketOpenSSL::DoTransportIO() { |
| 920 | bool network_moved = false; |
| 921 | int rv; |
| 922 | // Read and write as much data as possible. The loop is necessary because |
| 923 | // Write() may return synchronously. |
| 924 | do { |
| 925 | rv = BufferSend(); |
| 926 | if (rv != ERR_IO_PENDING && rv != 0) |
| 927 | network_moved = true; |
| 928 | } while (rv > 0); |
| 929 | if (!transport_recv_eof_ && BufferRecv() != ERR_IO_PENDING) |
| 930 | network_moved = true; |
| 931 | return network_moved; |
| 932 | } |
| 933 | |
| 934 | int SSLClientSocketOpenSSL::DoHandshake() { |
| 935 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 936 | int net_error = net::OK; |
| 937 | int rv = SSL_do_handshake(ssl_); |
| 938 | |
| 939 | if (client_auth_cert_needed_) { |
| 940 | net_error = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 941 | // If the handshake already succeeded (because the server requests but |
| 942 | // doesn't require a client cert), we need to invalidate the SSL session |
| 943 | // so that we won't try to resume the non-client-authenticated session in |
| 944 | // the next handshake. This will cause the server to ask for a client |
| 945 | // cert again. |
| 946 | if (rv == 1) { |
| 947 | // Remove from session cache but don't clear this connection. |
| 948 | SSL_SESSION* session = SSL_get_session(ssl_); |
| 949 | if (session) { |
| 950 | int rv = SSL_CTX_remove_session(SSL_get_SSL_CTX(ssl_), session); |
| 951 | LOG_IF(WARNING, !rv) << "Couldn't invalidate SSL session: " << session; |
| 952 | } |
| 953 | } |
| 954 | } else if (rv == 1) { |
| 955 | if (trying_cached_session_ && logging::DEBUG_MODE) { |
| 956 | DVLOG(2) << "Result of session reuse for " << host_and_port_.ToString() |
| 957 | << " is: " << (SSL_session_reused(ssl_) ? "Success" : "Fail"); |
| 958 | } |
| 959 | // SSL handshake is completed. Let's verify the certificate. |
| 960 | const bool got_cert = !!UpdateServerCert(); |
| 961 | DCHECK(got_cert); |
| 962 | net_log_.AddEvent( |
| 963 | NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
| 964 | base::Bind(&NetLogX509CertificateCallback, |
| 965 | base::Unretained(server_cert_.get()))); |
| 966 | GotoState(STATE_VERIFY_CERT); |
| 967 | } else { |
| 968 | int ssl_error = SSL_get_error(ssl_, rv); |
| 969 | |
| 970 | if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
| 971 | // The server supports TLS channel id and the lookup is asynchronous. |
| 972 | // Retrieve the error from the call to |server_bound_cert_service_|. |
| 973 | net_error = channel_id_request_return_value_; |
| 974 | } else { |
| 975 | net_error = MapOpenSSLError(ssl_error, err_tracer); |
| 976 | } |
| 977 | |
| 978 | // If not done, stay in this state |
| 979 | if (net_error == ERR_IO_PENDING) { |
| 980 | GotoState(STATE_HANDSHAKE); |
| 981 | } else { |
| 982 | LOG(ERROR) << "handshake failed; returned " << rv |
| 983 | << ", SSL error code " << ssl_error |
| 984 | << ", net_error " << net_error; |
| 985 | net_log_.AddEvent( |
| 986 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 987 | CreateNetLogSSLErrorCallback(net_error, ssl_error)); |
| 988 | } |
| 989 | } |
| 990 | return net_error; |
| 991 | } |
| 992 | |
| 993 | int SSLClientSocketOpenSSL::DoVerifyCert(int result) { |
| 994 | DCHECK(server_cert_.get()); |
| 995 | GotoState(STATE_VERIFY_CERT_COMPLETE); |
| 996 | |
| 997 | CertStatus cert_status; |
| 998 | if (ssl_config_.IsAllowedBadCert(server_cert_.get(), &cert_status)) { |
| 999 | VLOG(1) << "Received an expected bad cert with status: " << cert_status; |
| 1000 | server_cert_verify_result_.Reset(); |
| 1001 | server_cert_verify_result_.cert_status = cert_status; |
| 1002 | server_cert_verify_result_.verified_cert = server_cert_; |
| 1003 | return OK; |
| 1004 | } |
| 1005 | |
| 1006 | int flags = 0; |
| 1007 | if (ssl_config_.rev_checking_enabled) |
| 1008 | flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; |
| 1009 | if (ssl_config_.verify_ev_cert) |
| 1010 | flags |= CertVerifier::VERIFY_EV_CERT; |
| 1011 | if (ssl_config_.cert_io_enabled) |
| 1012 | flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; |
| 1013 | if (ssl_config_.rev_checking_required_local_anchors) |
| 1014 | flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; |
| 1015 | verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); |
| 1016 | return verifier_->Verify( |
| 1017 | server_cert_.get(), |
| 1018 | host_and_port_.host(), |
| 1019 | flags, |
| 1020 | NULL /* no CRL set */, |
| 1021 | &server_cert_verify_result_, |
| 1022 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1023 | base::Unretained(this)), |
| 1024 | net_log_); |
| 1025 | } |
| 1026 | |
| 1027 | int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
| 1028 | verifier_.reset(); |
| 1029 | |
| 1030 | if (result == OK) { |
| 1031 | // TODO(joth): Work out if we need to remember the intermediate CA certs |
| 1032 | // when the server sends them to us, and do so here. |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 1033 | SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1034 | } else { |
| 1035 | DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
| 1036 | << " (" << result << ")"; |
| 1037 | } |
| 1038 | |
| 1039 | completed_handshake_ = true; |
| 1040 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 1041 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 1042 | return result; |
| 1043 | } |
| 1044 | |
| 1045 | void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
| 1046 | if (!user_connect_callback_.is_null()) { |
| 1047 | CompletionCallback c = user_connect_callback_; |
| 1048 | user_connect_callback_.Reset(); |
| 1049 | c.Run(rv > OK ? OK : rv); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1054 | server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1055 | server_cert_ = server_cert_chain_->AsOSChain(); |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1056 | |
| 1057 | if (!server_cert_chain_->IsValid()) |
| 1058 | DVLOG(1) << "UpdateServerCert received invalid certificate chain from peer"; |
| 1059 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1060 | return server_cert_.get(); |
| 1061 | } |
| 1062 | |
| 1063 | void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 1064 | int rv = DoHandshakeLoop(result); |
| 1065 | if (rv != ERR_IO_PENDING) { |
| 1066 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 1067 | DoConnectCallback(rv); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
| 1072 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1073 | // In handshake phase. |
| 1074 | OnHandshakeIOComplete(result); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | // OnSendComplete may need to call DoPayloadRead while the renegotiation |
| 1079 | // handshake is in progress. |
| 1080 | int rv_read = ERR_IO_PENDING; |
| 1081 | int rv_write = ERR_IO_PENDING; |
| 1082 | bool network_moved; |
| 1083 | do { |
| 1084 | if (user_read_buf_.get()) |
| 1085 | rv_read = DoPayloadRead(); |
| 1086 | if (user_write_buf_.get()) |
| 1087 | rv_write = DoPayloadWrite(); |
| 1088 | network_moved = DoTransportIO(); |
| 1089 | } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && |
| 1090 | (user_read_buf_.get() || user_write_buf_.get()) && network_moved); |
| 1091 | |
| 1092 | // Performing the Read callback may cause |this| to be deleted. If this |
| 1093 | // happens, the Write callback should not be invoked. Guard against this by |
| 1094 | // holding a WeakPtr to |this| and ensuring it's still valid. |
| 1095 | base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); |
| 1096 | if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) |
| 1097 | DoReadCallback(rv_read); |
| 1098 | |
| 1099 | if (!guard.get()) |
| 1100 | return; |
| 1101 | |
| 1102 | if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) |
| 1103 | DoWriteCallback(rv_write); |
| 1104 | } |
| 1105 | |
| 1106 | void SSLClientSocketOpenSSL::OnRecvComplete(int result) { |
| 1107 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1108 | // In handshake phase. |
| 1109 | OnHandshakeIOComplete(result); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | // Network layer received some data, check if client requested to read |
| 1114 | // decrypted data. |
| 1115 | if (!user_read_buf_.get()) |
| 1116 | return; |
| 1117 | |
| 1118 | int rv = DoReadLoop(result); |
| 1119 | if (rv != ERR_IO_PENDING) |
| 1120 | DoReadCallback(rv); |
| 1121 | } |
| 1122 | |
| 1123 | int SSLClientSocketOpenSSL::DoHandshakeLoop(int last_io_result) { |
| 1124 | int rv = last_io_result; |
| 1125 | do { |
| 1126 | // Default to STATE_NONE for next state. |
| 1127 | // (This is a quirk carried over from the windows |
| 1128 | // implementation. It makes reading the logs a bit harder.) |
| 1129 | // State handlers can and often do call GotoState just |
| 1130 | // to stay in the current state. |
| 1131 | State state = next_handshake_state_; |
| 1132 | GotoState(STATE_NONE); |
| 1133 | switch (state) { |
| 1134 | case STATE_HANDSHAKE: |
| 1135 | rv = DoHandshake(); |
| 1136 | break; |
| 1137 | case STATE_VERIFY_CERT: |
| 1138 | DCHECK(rv == OK); |
| 1139 | rv = DoVerifyCert(rv); |
| 1140 | break; |
| 1141 | case STATE_VERIFY_CERT_COMPLETE: |
| 1142 | rv = DoVerifyCertComplete(rv); |
| 1143 | break; |
| 1144 | case STATE_NONE: |
| 1145 | default: |
| 1146 | rv = ERR_UNEXPECTED; |
| 1147 | NOTREACHED() << "unexpected state" << state; |
| 1148 | break; |
| 1149 | } |
| 1150 | |
| 1151 | bool network_moved = DoTransportIO(); |
| 1152 | if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 1153 | // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 1154 | // special case we keep looping even if rv is ERR_IO_PENDING because |
| 1155 | // the transport IO may allow DoHandshake to make progress. |
| 1156 | rv = OK; // This causes us to stay in the loop. |
| 1157 | } |
| 1158 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 1159 | return rv; |
| 1160 | } |
| 1161 | |
| 1162 | int SSLClientSocketOpenSSL::DoReadLoop(int result) { |
| 1163 | if (result < 0) |
| 1164 | return result; |
| 1165 | |
| 1166 | bool network_moved; |
| 1167 | int rv; |
| 1168 | do { |
| 1169 | rv = DoPayloadRead(); |
| 1170 | network_moved = DoTransportIO(); |
| 1171 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1172 | |
| 1173 | return rv; |
| 1174 | } |
| 1175 | |
| 1176 | int SSLClientSocketOpenSSL::DoWriteLoop(int result) { |
| 1177 | if (result < 0) |
| 1178 | return result; |
| 1179 | |
| 1180 | bool network_moved; |
| 1181 | int rv; |
| 1182 | do { |
| 1183 | rv = DoPayloadWrite(); |
| 1184 | network_moved = DoTransportIO(); |
| 1185 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1186 | |
| 1187 | return rv; |
| 1188 | } |
| 1189 | |
| 1190 | int SSLClientSocketOpenSSL::DoPayloadRead() { |
| 1191 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1192 | |
| 1193 | int rv; |
| 1194 | if (pending_read_error_ != kNoPendingReadResult) { |
| 1195 | rv = pending_read_error_; |
| 1196 | pending_read_error_ = kNoPendingReadResult; |
| 1197 | if (rv == 0) { |
| 1198 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1199 | rv, user_read_buf_->data()); |
| 1200 | } |
| 1201 | return rv; |
| 1202 | } |
| 1203 | |
| 1204 | int total_bytes_read = 0; |
| 1205 | do { |
| 1206 | rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
| 1207 | user_read_buf_len_ - total_bytes_read); |
| 1208 | if (rv > 0) |
| 1209 | total_bytes_read += rv; |
| 1210 | } while (total_bytes_read < user_read_buf_len_ && rv > 0); |
| 1211 | |
| 1212 | if (total_bytes_read == user_read_buf_len_) { |
| 1213 | rv = total_bytes_read; |
| 1214 | } else { |
| 1215 | // Otherwise, an error occurred (rv <= 0). The error needs to be handled |
| 1216 | // immediately, while the OpenSSL errors are still available in |
| 1217 | // thread-local storage. However, the handled/remapped error code should |
| 1218 | // only be returned if no application data was already read; if it was, the |
| 1219 | // error code should be deferred until the next call of DoPayloadRead. |
| 1220 | // |
| 1221 | // If no data was read, |*next_result| will point to the return value of |
| 1222 | // this function. If at least some data was read, |*next_result| will point |
| 1223 | // to |pending_read_error_|, to be returned in a future call to |
| 1224 | // DoPayloadRead() (e.g.: after the current data is handled). |
| 1225 | int *next_result = &rv; |
| 1226 | if (total_bytes_read > 0) { |
| 1227 | pending_read_error_ = rv; |
| 1228 | rv = total_bytes_read; |
| 1229 | next_result = &pending_read_error_; |
| 1230 | } |
| 1231 | |
| 1232 | if (client_auth_cert_needed_) { |
| 1233 | *next_result = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1234 | } else if (*next_result < 0) { |
| 1235 | int err = SSL_get_error(ssl_, *next_result); |
| 1236 | *next_result = MapOpenSSLError(err, err_tracer); |
| 1237 | if (rv > 0 && *next_result == ERR_IO_PENDING) { |
| 1238 | // If at least some data was read from SSL_read(), do not treat |
| 1239 | // insufficient data as an error to return in the next call to |
| 1240 | // DoPayloadRead() - instead, let the call fall through to check |
| 1241 | // SSL_read() again. This is because DoTransportIO() may complete |
| 1242 | // in between the next call to DoPayloadRead(), and thus it is |
| 1243 | // important to check SSL_read() on subsequent invocations to see |
| 1244 | // if a complete record may now be read. |
| 1245 | *next_result = kNoPendingReadResult; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (rv >= 0) { |
| 1251 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1252 | user_read_buf_->data()); |
| 1253 | } |
| 1254 | return rv; |
| 1255 | } |
| 1256 | |
| 1257 | int SSLClientSocketOpenSSL::DoPayloadWrite() { |
| 1258 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1259 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
| 1260 | |
| 1261 | if (rv >= 0) { |
| 1262 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1263 | user_write_buf_->data()); |
| 1264 | return rv; |
| 1265 | } |
| 1266 | |
| 1267 | int err = SSL_get_error(ssl_, rv); |
| 1268 | return MapOpenSSLError(err, err_tracer); |
| 1269 | } |
| 1270 | |
| 1271 | int SSLClientSocketOpenSSL::BufferSend(void) { |
| 1272 | if (transport_send_busy_) |
| 1273 | return ERR_IO_PENDING; |
| 1274 | |
| 1275 | if (!send_buffer_.get()) { |
| 1276 | // Get a fresh send buffer out of the send BIO. |
| 1277 | size_t max_read = BIO_ctrl_pending(transport_bio_); |
| 1278 | if (!max_read) |
| 1279 | return 0; // Nothing pending in the OpenSSL write BIO. |
| 1280 | send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); |
| 1281 | int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); |
| 1282 | DCHECK_GT(read_bytes, 0); |
| 1283 | CHECK_EQ(static_cast<int>(max_read), read_bytes); |
| 1284 | } |
| 1285 | |
| 1286 | int rv = transport_->socket()->Write( |
| 1287 | send_buffer_.get(), |
| 1288 | send_buffer_->BytesRemaining(), |
| 1289 | base::Bind(&SSLClientSocketOpenSSL::BufferSendComplete, |
| 1290 | base::Unretained(this))); |
| 1291 | if (rv == ERR_IO_PENDING) { |
| 1292 | transport_send_busy_ = true; |
| 1293 | } else { |
| 1294 | TransportWriteComplete(rv); |
| 1295 | } |
| 1296 | return rv; |
| 1297 | } |
| 1298 | |
| 1299 | int SSLClientSocketOpenSSL::BufferRecv(void) { |
| 1300 | if (transport_recv_busy_) |
| 1301 | return ERR_IO_PENDING; |
| 1302 | |
| 1303 | // Determine how much was requested from |transport_bio_| that was not |
| 1304 | // actually available. |
| 1305 | size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
| 1306 | if (requested == 0) { |
| 1307 | // This is not a perfect match of error codes, as no operation is |
| 1308 | // actually pending. However, returning 0 would be interpreted as |
| 1309 | // a possible sign of EOF, which is also an inappropriate match. |
| 1310 | return ERR_IO_PENDING; |
| 1311 | } |
| 1312 | |
| 1313 | // Known Issue: While only reading |requested| data is the more correct |
| 1314 | // implementation, it has the downside of resulting in frequent reads: |
| 1315 | // One read for the SSL record header (~5 bytes) and one read for the SSL |
| 1316 | // record body. Rather than issuing these reads to the underlying socket |
| 1317 | // (and constantly allocating new IOBuffers), a single Read() request to |
| 1318 | // fill |transport_bio_| is issued. As long as an SSL client socket cannot |
| 1319 | // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL |
| 1320 | // traffic, this over-subscribed Read()ing will not cause issues. |
| 1321 | size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_); |
| 1322 | if (!max_write) |
| 1323 | return ERR_IO_PENDING; |
| 1324 | |
| 1325 | recv_buffer_ = new IOBuffer(max_write); |
| 1326 | int rv = transport_->socket()->Read( |
| 1327 | recv_buffer_.get(), |
| 1328 | max_write, |
| 1329 | base::Bind(&SSLClientSocketOpenSSL::BufferRecvComplete, |
| 1330 | base::Unretained(this))); |
| 1331 | if (rv == ERR_IO_PENDING) { |
| 1332 | transport_recv_busy_ = true; |
| 1333 | } else { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1334 | rv = TransportReadComplete(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1335 | } |
| 1336 | return rv; |
| 1337 | } |
| 1338 | |
| 1339 | void SSLClientSocketOpenSSL::BufferSendComplete(int result) { |
| 1340 | transport_send_busy_ = false; |
| 1341 | TransportWriteComplete(result); |
| 1342 | OnSendComplete(result); |
| 1343 | } |
| 1344 | |
| 1345 | void SSLClientSocketOpenSSL::BufferRecvComplete(int result) { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1346 | result = TransportReadComplete(result); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1347 | OnRecvComplete(result); |
| 1348 | } |
| 1349 | |
| 1350 | void SSLClientSocketOpenSSL::TransportWriteComplete(int result) { |
| 1351 | DCHECK(ERR_IO_PENDING != result); |
| 1352 | if (result < 0) { |
| 1353 | // Got a socket write error; close the BIO to indicate this upward. |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1354 | // |
| 1355 | // TODO(davidben): The value of |result| gets lost. Feed the error back into |
| 1356 | // the BIO so it gets (re-)detected in OnSendComplete. Perhaps with |
| 1357 | // BIO_set_callback. |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1358 | DVLOG(1) << "TransportWriteComplete error " << result; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1359 | (void)BIO_shutdown_wr(SSL_get_wbio(ssl_)); |
| 1360 | |
| 1361 | // Match the fix for http://crbug.com/249848 in NSS by erroring future reads |
| 1362 | // from the socket after a write error. |
| 1363 | // |
| 1364 | // TODO(davidben): Avoid having read and write ends interact this way. |
| 1365 | transport_write_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1366 | (void)BIO_shutdown_wr(transport_bio_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1367 | send_buffer_ = NULL; |
| 1368 | } else { |
| 1369 | DCHECK(send_buffer_.get()); |
| 1370 | send_buffer_->DidConsume(result); |
| 1371 | DCHECK_GE(send_buffer_->BytesRemaining(), 0); |
| 1372 | if (send_buffer_->BytesRemaining() <= 0) |
| 1373 | send_buffer_ = NULL; |
| 1374 | } |
| 1375 | } |
| 1376 | |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1377 | int SSLClientSocketOpenSSL::TransportReadComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1378 | DCHECK(ERR_IO_PENDING != result); |
| 1379 | if (result <= 0) { |
| 1380 | DVLOG(1) << "TransportReadComplete result " << result; |
| 1381 | // Received 0 (end of file) or an error. Either way, bubble it up to the |
| 1382 | // SSL layer via the BIO. TODO(joth): consider stashing the error code, to |
| 1383 | // relay up to the SSL socket client (i.e. via DoReadCallback). |
| 1384 | if (result == 0) |
| 1385 | transport_recv_eof_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1386 | (void)BIO_shutdown_wr(transport_bio_); |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1387 | } else if (transport_write_error_ < 0) { |
| 1388 | // Mirror transport write errors as read failures; transport_bio_ has been |
| 1389 | // shut down by TransportWriteComplete, so the BIO_write will fail, failing |
| 1390 | // the CHECK. http://crbug.com/335557. |
| 1391 | result = transport_write_error_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1392 | } else { |
| 1393 | DCHECK(recv_buffer_.get()); |
| 1394 | int ret = BIO_write(transport_bio_, recv_buffer_->data(), result); |
| 1395 | // A write into a memory BIO should always succeed. |
[email protected] | b74d0f5 | 2014-01-23 00:44:40 | [diff] [blame] | 1396 | // Force values on the stack for http://crbug.com/335557 |
| 1397 | base::debug::Alias(&result); |
| 1398 | base::debug::Alias(&ret); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1399 | CHECK_EQ(result, ret); |
| 1400 | } |
| 1401 | recv_buffer_ = NULL; |
| 1402 | transport_recv_busy_ = false; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1403 | return result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1404 | } |
| 1405 | |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1406 | int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl, |
| 1407 | X509** x509, |
| 1408 | EVP_PKEY** pkey) { |
| 1409 | DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1410 | DCHECK(ssl == ssl_); |
| 1411 | DCHECK(*x509 == NULL); |
| 1412 | DCHECK(*pkey == NULL); |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 1413 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1414 | if (!ssl_config_.send_client_cert) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1415 | // First pass: we know that a client certificate is needed, but we do not |
| 1416 | // have one at hand. |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1417 | client_auth_cert_needed_ = true; |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1418 | STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
| 1419 | for (int i = 0; i < sk_X509_NAME_num(authorities); i++) { |
| 1420 | X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
| 1421 | unsigned char* str = NULL; |
| 1422 | int length = i2d_X509_NAME(ca_name, &str); |
| 1423 | cert_authorities_.push_back(std::string( |
| 1424 | reinterpret_cast<const char*>(str), |
| 1425 | static_cast<size_t>(length))); |
| 1426 | OPENSSL_free(str); |
| 1427 | } |
| 1428 | |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1429 | return -1; // Suspends handshake. |
| 1430 | } |
| 1431 | |
| 1432 | // Second pass: a client certificate should have been selected. |
[email protected] | 13914c9 | 2013-06-13 22:42:42 | [diff] [blame] | 1433 | if (ssl_config_.client_cert.get()) { |
[email protected] | ede323ea | 2013-03-02 22:54:41 | [diff] [blame] | 1434 | // A note about ownership: FetchClientCertPrivateKey() increments |
| 1435 | // the reference count of the EVP_PKEY. Ownership of this reference |
| 1436 | // is passed directly to OpenSSL, which will release the reference |
| 1437 | // using EVP_PKEY_free() when the SSL object is destroyed. |
| 1438 | OpenSSLClientKeyStore::ScopedEVP_PKEY privkey; |
| 1439 | if (OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
| 1440 | ssl_config_.client_cert.get(), &privkey)) { |
[email protected] | 0c6523f | 2010-12-10 10:56:24 | [diff] [blame] | 1441 | // TODO(joth): (copied from NSS) We should wait for server certificate |
| 1442 | // verification before sending our credentials. See http://crbug.com/13934 |
| 1443 | *x509 = X509Certificate::DupOSCertHandle( |
| 1444 | ssl_config_.client_cert->os_cert_handle()); |
[email protected] | ede323ea | 2013-03-02 22:54:41 | [diff] [blame] | 1445 | *pkey = privkey.release(); |
[email protected] | 0c6523f | 2010-12-10 10:56:24 | [diff] [blame] | 1446 | return 1; |
| 1447 | } |
| 1448 | LOG(WARNING) << "Client cert found without private key"; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1449 | } |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 1450 | #else // !defined(USE_OPENSSL_CERTS) |
| 1451 | // OS handling of client certificates is not yet implemented. |
| 1452 | NOTIMPLEMENTED(); |
| 1453 | #endif // defined(USE_OPENSSL_CERTS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1454 | |
| 1455 | // Send no client certificate. |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 1459 | void SSLClientSocketOpenSSL::ChannelIDRequestCallback(SSL* ssl, |
| 1460 | EVP_PKEY** pkey) { |
| 1461 | DVLOG(3) << "OpenSSL ChannelIDRequestCallback called"; |
| 1462 | DCHECK_EQ(ssl, ssl_); |
| 1463 | DCHECK(!*pkey); |
| 1464 | |
| 1465 | channel_id_xtn_negotiated_ = true; |
| 1466 | if (!channel_id_private_key_.size()) { |
| 1467 | channel_id_request_return_value_ = |
| 1468 | server_bound_cert_service_->GetOrCreateDomainBoundCert( |
| 1469 | host_and_port_.host(), |
| 1470 | &channel_id_private_key_, |
| 1471 | &channel_id_cert_, |
| 1472 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1473 | base::Unretained(this)), |
| 1474 | &channel_id_request_handle_); |
| 1475 | if (channel_id_request_return_value_ != OK) |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | // Decode key. |
| 1480 | std::vector<uint8> encrypted_private_key_info; |
| 1481 | std::vector<uint8> subject_public_key_info; |
| 1482 | encrypted_private_key_info.assign( |
| 1483 | channel_id_private_key_.data(), |
| 1484 | channel_id_private_key_.data() + channel_id_private_key_.size()); |
| 1485 | subject_public_key_info.assign( |
| 1486 | channel_id_cert_.data(), |
| 1487 | channel_id_cert_.data() + channel_id_cert_.size()); |
| 1488 | scoped_ptr<crypto::ECPrivateKey> ec_private_key( |
| 1489 | crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
| 1490 | ServerBoundCertService::kEPKIPassword, |
| 1491 | encrypted_private_key_info, |
| 1492 | subject_public_key_info)); |
[email protected] | 5f72f244 | 2014-01-25 00:10:57 | [diff] [blame] | 1493 | if (!ec_private_key) |
| 1494 | return; |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 1495 | set_channel_id_sent(true); |
| 1496 | *pkey = EVP_PKEY_dup(ec_private_key->key()); |
| 1497 | } |
| 1498 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1499 | int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
| 1500 | if (!completed_handshake_) { |
| 1501 | // If the first handshake hasn't completed then we accept any certificates |
| 1502 | // because we verify after the handshake. |
| 1503 | return 1; |
| 1504 | } |
| 1505 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1506 | CHECK(server_cert_.get()); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1507 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1508 | PeerCertificateChain chain(store_ctx->chain); |
| 1509 | if (chain.IsValid() && server_cert_->Equals(chain.AsOSChain())) |
| 1510 | return 1; |
| 1511 | |
| 1512 | if (!chain.IsValid()) |
| 1513 | LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
| 1514 | else |
| 1515 | LOG(ERROR) << "Server certificate changed between handshakes"; |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
| 1518 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1519 | // SelectNextProtoCallback is called by OpenSSL during the handshake. If the |
| 1520 | // server supports NPN, selects a protocol from the list that the server |
| 1521 | // provides. According to third_party/openssl/openssl/ssl/ssl_lib.c, the |
| 1522 | // callback can assume that |in| is syntactically valid. |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1523 | int SSLClientSocketOpenSSL::SelectNextProtoCallback(unsigned char** out, |
| 1524 | unsigned char* outlen, |
| 1525 | const unsigned char* in, |
| 1526 | unsigned int inlen) { |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1527 | if (ssl_config_.next_protos.empty()) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1528 | *out = reinterpret_cast<uint8*>( |
| 1529 | const_cast<char*>(kDefaultSupportedNPNProtocol)); |
| 1530 | *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; |
| 1531 | npn_status_ = kNextProtoUnsupported; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1532 | return SSL_TLSEXT_ERR_OK; |
| 1533 | } |
| 1534 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1535 | // Assume there's no overlap between our protocols and the server's list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1536 | npn_status_ = kNextProtoNoOverlap; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1537 | |
| 1538 | // For each protocol in server preference order, see if we support it. |
| 1539 | for (unsigned int i = 0; i < inlen; i += in[i] + 1) { |
| 1540 | for (std::vector<std::string>::const_iterator |
| 1541 | j = ssl_config_.next_protos.begin(); |
| 1542 | j != ssl_config_.next_protos.end(); ++j) { |
| 1543 | if (in[i] == j->size() && |
| 1544 | memcmp(&in[i + 1], j->data(), in[i]) == 0) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1545 | // We found a match. |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1546 | *out = const_cast<unsigned char*>(in) + i + 1; |
| 1547 | *outlen = in[i]; |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1548 | npn_status_ = kNextProtoNegotiated; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1549 | break; |
| 1550 | } |
| 1551 | } |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1552 | if (npn_status_ == kNextProtoNegotiated) |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1553 | break; |
| 1554 | } |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1555 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1556 | // If we didn't find a protocol, we select the first one from our list. |
| 1557 | if (npn_status_ == kNextProtoNoOverlap) { |
| 1558 | *out = reinterpret_cast<uint8*>(const_cast<char*>( |
| 1559 | ssl_config_.next_protos[0].data())); |
| 1560 | *outlen = ssl_config_.next_protos[0].size(); |
| 1561 | } |
| 1562 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1563 | npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
[email protected] | 55e973d | 2011-12-05 23:03:24 | [diff] [blame] | 1564 | server_protos_.assign(reinterpret_cast<const char*>(in), inlen); |
[email protected] | 32e1dee | 2010-12-09 18:36:24 | [diff] [blame] | 1565 | DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1566 | return SSL_TLSEXT_ERR_OK; |
| 1567 | } |
| 1568 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1569 | scoped_refptr<X509Certificate> |
| 1570 | SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1571 | return server_cert_; |
| 1572 | } |
| 1573 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 1574 | } // namespace net |