[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] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 10 | #include <errno.h> |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 11 | #include <openssl/bio.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 12 | #include <openssl/err.h> |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 13 | #include <openssl/ssl.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 14 | |
[email protected] | 0f7804ec | 2011-10-07 20:04:18 | [diff] [blame] | 15 | #include "base/bind.h" |
[email protected] | f2da6ac | 2013-02-04 08:22:53 | [diff] [blame] | 16 | #include "base/callback_helpers.h" |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 17 | #include "base/environment.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 18 | #include "base/memory/singleton.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 19 | #include "base/metrics/histogram.h" |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 20 | #include "base/profiler/scoped_tracker.h" |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 21 | #include "base/strings/string_piece.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 22 | #include "base/synchronization/lock.h" |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 23 | #include "crypto/ec_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 24 | #include "crypto/openssl_util.h" |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 25 | #include "crypto/scoped_openssl_types.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 26 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 27 | #include "net/cert/cert_verifier.h" |
eranm | efbd313 | 2014-10-28 16:35:16 | [diff] [blame] | 28 | #include "net/cert/ct_ev_whitelist.h" |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 29 | #include "net/cert/ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 30 | #include "net/cert/single_request_cert_verifier.h" |
| 31 | #include "net/cert/x509_certificate_net_log_param.h" |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 32 | #include "net/cert/x509_util_openssl.h" |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 33 | #include "net/http/transport_security_state.h" |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 34 | #include "net/socket/ssl_session_cache_openssl.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 35 | #include "net/ssl/ssl_cert_request_info.h" |
| 36 | #include "net/ssl/ssl_connection_status_flags.h" |
| 37 | #include "net/ssl/ssl_info.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 38 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 39 | #if defined(OS_WIN) |
| 40 | #include "base/win/windows_version.h" |
| 41 | #endif |
| 42 | |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 43 | #if defined(USE_OPENSSL_CERTS) |
| 44 | #include "net/ssl/openssl_client_key_store.h" |
| 45 | #else |
| 46 | #include "net/ssl/openssl_platform_key.h" |
| 47 | #endif |
| 48 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 49 | namespace net { |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | // Enable this to see logging for state machine state transitions. |
| 54 | #if 0 |
[email protected] | 3b11277 | 2010-10-04 10:54:49 | [diff] [blame] | 55 | #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 56 | " jump to state " << s; \ |
| 57 | next_handshake_state_ = s; } while (0) |
| 58 | #else |
| 59 | #define GotoState(s) next_handshake_state_ = s |
| 60 | #endif |
| 61 | |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 62 | // This constant can be any non-negative/non-zero value (eg: it does not |
| 63 | // overlap with any value of the net::Error range, including net::OK). |
| 64 | const int kNoPendingReadResult = 1; |
| 65 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 66 | // If a client doesn't have a list of protocols that it supports, but |
| 67 | // the server supports NPN, choosing "http/1.1" is the best answer. |
| 68 | const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
| 69 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 70 | void FreeX509Stack(STACK_OF(X509)* ptr) { |
| 71 | sk_X509_pop_free(ptr, X509_free); |
| 72 | } |
| 73 | |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 74 | typedef crypto::ScopedOpenSSL<X509, X509_free>::Type ScopedX509; |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 75 | typedef crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>::Type |
| 76 | ScopedX509Stack; |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 77 | |
[email protected] | 8903815 | 2012-09-07 06:30:17 | [diff] [blame] | 78 | #if OPENSSL_VERSION_NUMBER < 0x1000103fL |
| 79 | // This method doesn't seem to have made it into the OpenSSL headers. |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 80 | unsigned long SSL_CIPHER_get_id(const SSL_CIPHER* cipher) { return cipher->id; } |
[email protected] | 8903815 | 2012-09-07 06:30:17 | [diff] [blame] | 81 | #endif |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 82 | |
| 83 | // Used for encoding the |connection_status| field of an SSLInfo object. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 84 | int EncodeSSLConnectionStatus(uint16 cipher_suite, |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 85 | int compression, |
| 86 | int version) { |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 87 | return cipher_suite | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 88 | ((compression & SSL_CONNECTION_COMPRESSION_MASK) << |
| 89 | SSL_CONNECTION_COMPRESSION_SHIFT) | |
| 90 | ((version & SSL_CONNECTION_VERSION_MASK) << |
| 91 | SSL_CONNECTION_VERSION_SHIFT); |
| 92 | } |
| 93 | |
| 94 | // Returns the net SSL version number (see ssl_connection_status_flags.h) for |
| 95 | // this SSL connection. |
| 96 | int GetNetSSLVersion(SSL* ssl) { |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 97 | switch (SSL_version(ssl)) { |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 98 | case SSL2_VERSION: |
| 99 | return SSL_CONNECTION_VERSION_SSL2; |
| 100 | case SSL3_VERSION: |
| 101 | return SSL_CONNECTION_VERSION_SSL3; |
| 102 | case TLS1_VERSION: |
| 103 | return SSL_CONNECTION_VERSION_TLS1; |
davidben | 1d09402 | 2014-11-05 18:55:47 | [diff] [blame] | 104 | case TLS1_1_VERSION: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 105 | return SSL_CONNECTION_VERSION_TLS1_1; |
davidben | 1d09402 | 2014-11-05 18:55:47 | [diff] [blame] | 106 | case TLS1_2_VERSION: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 107 | return SSL_CONNECTION_VERSION_TLS1_2; |
| 108 | default: |
| 109 | return SSL_CONNECTION_VERSION_UNKNOWN; |
| 110 | } |
| 111 | } |
| 112 | |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 113 | ScopedX509 OSCertHandleToOpenSSL( |
| 114 | X509Certificate::OSCertHandle os_handle) { |
| 115 | #if defined(USE_OPENSSL_CERTS) |
| 116 | return ScopedX509(X509Certificate::DupOSCertHandle(os_handle)); |
| 117 | #else // !defined(USE_OPENSSL_CERTS) |
| 118 | std::string der_encoded; |
| 119 | if (!X509Certificate::GetDEREncoded(os_handle, &der_encoded)) |
| 120 | return ScopedX509(); |
| 121 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_encoded.data()); |
| 122 | return ScopedX509(d2i_X509(NULL, &bytes, der_encoded.size())); |
| 123 | #endif // defined(USE_OPENSSL_CERTS) |
| 124 | } |
| 125 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 126 | ScopedX509Stack OSCertHandlesToOpenSSL( |
| 127 | const X509Certificate::OSCertHandles& os_handles) { |
| 128 | ScopedX509Stack stack(sk_X509_new_null()); |
| 129 | for (size_t i = 0; i < os_handles.size(); i++) { |
| 130 | ScopedX509 x509 = OSCertHandleToOpenSSL(os_handles[i]); |
| 131 | if (!x509) |
| 132 | return ScopedX509Stack(); |
| 133 | sk_X509_push(stack.get(), x509.release()); |
| 134 | } |
| 135 | return stack.Pass(); |
| 136 | } |
| 137 | |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 138 | int LogErrorCallback(const char* str, size_t len, void* context) { |
| 139 | LOG(ERROR) << base::StringPiece(str, len); |
| 140 | return 1; |
| 141 | } |
| 142 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 143 | bool IsOCSPStaplingSupported() { |
| 144 | #if defined(OS_WIN) |
| 145 | // CERT_OCSP_RESPONSE_PROP_ID is only implemented on Vista+, but it can be |
| 146 | // set on Windows XP without error. There is some overhead from the server |
| 147 | // sending the OCSP response if it supports the extension, for the subset of |
| 148 | // XP clients who will request it but be unable to use it, but this is an |
| 149 | // acceptable trade-off for simplicity of implementation. |
| 150 | return true; |
| 151 | #else |
| 152 | return false; |
| 153 | #endif |
| 154 | } |
| 155 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 156 | } // namespace |
| 157 | |
| 158 | class SSLClientSocketOpenSSL::SSLContext { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 159 | public: |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 160 | static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 161 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 162 | SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 163 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 164 | SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 165 | DCHECK(ssl); |
| 166 | SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( |
| 167 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 168 | DCHECK(socket); |
| 169 | return socket; |
| 170 | } |
| 171 | |
| 172 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { |
| 173 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | friend struct DefaultSingletonTraits<SSLContext>; |
| 178 | |
| 179 | SSLContext() { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 180 | crypto::EnsureOpenSSLInit(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 181 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 182 | DCHECK_NE(ssl_socket_data_index_, -1); |
| 183 | ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 184 | session_cache_.Reset(ssl_ctx_.get(), kDefaultSessionCacheConfig); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 185 | SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 186 | SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 187 | SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 188 | // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty. |
| 189 | // It would be better if the callback were not a global setting, |
| 190 | // but that is an OpenSSL issue. |
| 191 | SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
| 192 | NULL); |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 193 | ssl_ctx_->tlsext_channel_id_enabled_new = 1; |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 194 | |
| 195 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 196 | std::string ssl_keylog_file; |
| 197 | if (env->GetVar("SSLKEYLOGFILE", &ssl_keylog_file) && |
| 198 | !ssl_keylog_file.empty()) { |
| 199 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 200 | BIO* bio = BIO_new_file(ssl_keylog_file.c_str(), "a"); |
| 201 | if (!bio) { |
| 202 | LOG(ERROR) << "Failed to open " << ssl_keylog_file; |
| 203 | ERR_print_errors_cb(&LogErrorCallback, NULL); |
| 204 | } else { |
| 205 | SSL_CTX_set_keylog_bio(ssl_ctx_.get(), bio); |
| 206 | } |
| 207 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 210 | static std::string GetSessionCacheKey(const SSL* ssl) { |
| 211 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 212 | DCHECK(socket); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 213 | return socket->GetSessionCacheKey(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 216 | static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 217 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 218 | static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 219 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 220 | DCHECK(socket); |
| 221 | return socket->ClientCertRequestCallback(ssl); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 224 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 225 | SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
| 226 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 227 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 228 | CHECK(socket); |
| 229 | |
| 230 | return socket->CertVerifyCallback(store_ctx); |
| 231 | } |
| 232 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 233 | static int SelectNextProtoCallback(SSL* ssl, |
| 234 | unsigned char** out, unsigned char* outlen, |
| 235 | const unsigned char* in, |
| 236 | unsigned int inlen, void* arg) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 237 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 238 | return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
| 239 | } |
| 240 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 241 | // This is the index used with SSL_get_ex_data to retrieve the owner |
| 242 | // SSLClientSocketOpenSSL object from an SSL instance. |
| 243 | int ssl_socket_data_index_; |
| 244 | |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 245 | crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free>::Type ssl_ctx_; |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 246 | // |session_cache_| must be destroyed before |ssl_ctx_|. |
| 247 | SSLSessionCacheOpenSSL session_cache_; |
| 248 | }; |
| 249 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 250 | // PeerCertificateChain is a helper object which extracts the certificate |
| 251 | // chain, as given by the server, from an OpenSSL socket and performs the needed |
| 252 | // resource management. The first element of the chain is the leaf certificate |
| 253 | // and the other elements are in the order given by the server. |
| 254 | class SSLClientSocketOpenSSL::PeerCertificateChain { |
| 255 | public: |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 256 | explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 257 | PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
| 258 | ~PeerCertificateChain() {} |
| 259 | PeerCertificateChain& operator=(const PeerCertificateChain& other); |
| 260 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 261 | // Resets the PeerCertificateChain to the set of certificates in|chain|, |
| 262 | // which may be NULL, indicating to empty the store certificates. |
| 263 | // Note: If an error occurs, such as being unable to parse the certificates, |
| 264 | // this will behave as if Reset(NULL) was called. |
| 265 | void Reset(STACK_OF(X509)* chain); |
| 266 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 267 | // Note that when USE_OPENSSL is defined, OSCertHandle is X509* |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 268 | scoped_refptr<X509Certificate> AsOSChain() const; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 269 | |
| 270 | size_t size() const { |
| 271 | if (!openssl_chain_.get()) |
| 272 | return 0; |
| 273 | return sk_X509_num(openssl_chain_.get()); |
| 274 | } |
| 275 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 276 | bool empty() const { |
| 277 | return size() == 0; |
| 278 | } |
| 279 | |
| 280 | X509* Get(size_t index) const { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 281 | DCHECK_LT(index, size()); |
| 282 | return sk_X509_value(openssl_chain_.get(), index); |
| 283 | } |
| 284 | |
| 285 | private: |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 286 | ScopedX509Stack openssl_chain_; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | SSLClientSocketOpenSSL::PeerCertificateChain& |
| 290 | SSLClientSocketOpenSSL::PeerCertificateChain::operator=( |
| 291 | const PeerCertificateChain& other) { |
| 292 | if (this == &other) |
| 293 | return *this; |
| 294 | |
[email protected] | 24176af | 2014-08-14 09:31:04 | [diff] [blame] | 295 | openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 296 | return *this; |
| 297 | } |
| 298 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 299 | void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
| 300 | STACK_OF(X509)* chain) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 301 | openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 302 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 303 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 304 | scoped_refptr<X509Certificate> |
| 305 | SSLClientSocketOpenSSL::PeerCertificateChain::AsOSChain() const { |
| 306 | #if defined(USE_OPENSSL_CERTS) |
| 307 | // When OSCertHandle is typedef'ed to X509, this implementation does a short |
| 308 | // cut to avoid converting back and forth between DER and the X509 struct. |
| 309 | X509Certificate::OSCertHandles intermediates; |
| 310 | for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 311 | intermediates.push_back(sk_X509_value(openssl_chain_.get(), i)); |
| 312 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 313 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 314 | return make_scoped_refptr(X509Certificate::CreateFromHandle( |
| 315 | sk_X509_value(openssl_chain_.get(), 0), intermediates)); |
| 316 | #else |
| 317 | // DER-encode the chain and convert to a platform certificate handle. |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 318 | std::vector<base::StringPiece> der_chain; |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 319 | for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 320 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 321 | base::StringPiece der; |
| 322 | if (!x509_util::GetDER(x, &der)) |
| 323 | return NULL; |
| 324 | der_chain.push_back(der); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 325 | } |
| 326 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 327 | return make_scoped_refptr(X509Certificate::CreateFromDERCertChain(der_chain)); |
| 328 | #endif |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 329 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 330 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 331 | // static |
| 332 | SSLSessionCacheOpenSSL::Config |
| 333 | SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { |
| 334 | &GetSessionCacheKey, // key_func |
| 335 | 1024, // max_entries |
| 336 | 256, // expiration_check_count |
| 337 | 60 * 60, // timeout_seconds |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 338 | }; |
[email protected] | 31383472 | 2010-11-17 09:57:18 | [diff] [blame] | 339 | |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 340 | // static |
| 341 | void SSLClientSocket::ClearSessionCache() { |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 342 | SSLClientSocketOpenSSL::SSLContext* context = |
| 343 | SSLClientSocketOpenSSL::SSLContext::GetInstance(); |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 344 | context->session_cache()->Flush(); |
| 345 | } |
| 346 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 347 | SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 348 | scoped_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 349 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 350 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 351 | const SSLClientSocketContext& context) |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 352 | : transport_send_busy_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 353 | transport_recv_busy_(false), |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 354 | pending_read_error_(kNoPendingReadResult), |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 355 | pending_read_ssl_error_(SSL_ERROR_NONE), |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 356 | transport_read_error_(OK), |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 357 | transport_write_error_(OK), |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 358 | server_cert_chain_(new PeerCertificateChain(NULL)), |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 359 | completed_connect_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 360 | was_ever_used_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 361 | client_auth_cert_needed_(false), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 362 | cert_verifier_(context.cert_verifier), |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 363 | cert_transparency_verifier_(context.cert_transparency_verifier), |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 364 | channel_id_service_(context.channel_id_service), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 365 | ssl_(NULL), |
| 366 | transport_bio_(NULL), |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 367 | transport_(transport_socket.Pass()), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 368 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 369 | ssl_config_(ssl_config), |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 370 | ssl_session_cache_shard_(context.ssl_session_cache_shard), |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 371 | trying_cached_session_(false), |
[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 372 | next_handshake_state_(STATE_NONE), |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 373 | npn_status_(kNextProtoUnsupported), |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 374 | channel_id_xtn_negotiated_(false), |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 375 | handshake_succeeded_(false), |
[email protected] | e4738ba5 | 2014-08-07 10:07:22 | [diff] [blame] | 376 | marked_session_as_good_(false), |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 377 | transport_security_state_(context.transport_security_state), |
kulkarni.a | cd7b446 | 2014-08-28 07:41:34 | [diff] [blame] | 378 | net_log_(transport_->socket()->NetLog()), |
| 379 | weak_factory_(this) { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 380 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 381 | |
| 382 | SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 383 | Disconnect(); |
| 384 | } |
| 385 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 386 | std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const { |
| 387 | std::string result = host_and_port_.ToString(); |
| 388 | result.append("/"); |
| 389 | result.append(ssl_session_cache_shard_); |
| 390 | return result; |
| 391 | } |
| 392 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 393 | bool SSLClientSocketOpenSSL::InSessionCache() const { |
| 394 | SSLContext* context = SSLContext::GetInstance(); |
| 395 | std::string cache_key = GetSessionCacheKey(); |
| 396 | return context->session_cache()->SSLSessionIsInCache(cache_key); |
| 397 | } |
| 398 | |
| 399 | void SSLClientSocketOpenSSL::SetHandshakeCompletionCallback( |
| 400 | const base::Closure& callback) { |
| 401 | handshake_completion_callback_ = callback; |
| 402 | } |
| 403 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 404 | void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 405 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 406 | cert_request_info->host_and_port = host_and_port_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 407 | cert_request_info->cert_authorities = cert_authorities_; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 408 | cert_request_info->cert_key_types = cert_key_types_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 412 | std::string* proto) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 413 | *proto = npn_proto_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 414 | return npn_status_; |
| 415 | } |
| 416 | |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 417 | ChannelIDService* |
| 418 | SSLClientSocketOpenSSL::GetChannelIDService() const { |
| 419 | return channel_id_service_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | int SSLClientSocketOpenSSL::ExportKeyingMaterial( |
| 423 | const base::StringPiece& label, |
| 424 | bool has_context, const base::StringPiece& context, |
| 425 | unsigned char* out, unsigned int outlen) { |
| 426 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 427 | |
| 428 | int rv = SSL_export_keying_material( |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 429 | ssl_, out, outlen, label.data(), label.size(), |
| 430 | reinterpret_cast<const unsigned char*>(context.data()), |
| 431 | context.length(), context.length() > 0); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 432 | |
| 433 | if (rv != 1) { |
| 434 | int ssl_error = SSL_get_error(ssl_, rv); |
| 435 | LOG(ERROR) << "Failed to export keying material;" |
| 436 | << " returned " << rv |
| 437 | << ", SSL error code " << ssl_error; |
| 438 | return MapOpenSSLError(ssl_error, err_tracer); |
| 439 | } |
| 440 | return OK; |
| 441 | } |
| 442 | |
| 443 | int SSLClientSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 444 | NOTIMPLEMENTED(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 445 | return ERR_NOT_IMPLEMENTED; |
| 446 | } |
| 447 | |
| 448 | int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 449 | // It is an error to create an SSLClientSocket whose context has no |
| 450 | // TransportSecurityState. |
| 451 | DCHECK(transport_security_state_); |
| 452 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 453 | net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); |
| 454 | |
| 455 | // Set up new ssl object. |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 456 | int rv = Init(); |
| 457 | if (rv != OK) { |
| 458 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 459 | return rv; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Set SSL to client mode. Handshake happens in the loop below. |
| 463 | SSL_set_connect_state(ssl_); |
| 464 | |
| 465 | GotoState(STATE_HANDSHAKE); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 466 | rv = DoHandshakeLoop(OK); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 467 | if (rv == ERR_IO_PENDING) { |
| 468 | user_connect_callback_ = callback; |
| 469 | } else { |
| 470 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 471 | if (rv < OK) |
| 472 | OnHandshakeCompletion(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | return rv > OK ? OK : rv; |
| 476 | } |
| 477 | |
| 478 | void SSLClientSocketOpenSSL::Disconnect() { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 479 | // If a handshake was pending (Connect() had been called), notify interested |
| 480 | // parties that it's been aborted now. If the handshake had already |
| 481 | // completed, this is a no-op. |
| 482 | OnHandshakeCompletion(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 483 | if (ssl_) { |
| 484 | // Calling SSL_shutdown prevents the session from being marked as |
| 485 | // unresumable. |
| 486 | SSL_shutdown(ssl_); |
| 487 | SSL_free(ssl_); |
| 488 | ssl_ = NULL; |
| 489 | } |
| 490 | if (transport_bio_) { |
| 491 | BIO_free_all(transport_bio_); |
| 492 | transport_bio_ = NULL; |
| 493 | } |
| 494 | |
| 495 | // Shut down anything that may call us back. |
| 496 | verifier_.reset(); |
| 497 | transport_->socket()->Disconnect(); |
| 498 | |
| 499 | // Null all callbacks, delete all buffers. |
| 500 | transport_send_busy_ = false; |
| 501 | send_buffer_ = NULL; |
| 502 | transport_recv_busy_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 503 | recv_buffer_ = NULL; |
| 504 | |
| 505 | user_connect_callback_.Reset(); |
| 506 | user_read_callback_.Reset(); |
| 507 | user_write_callback_.Reset(); |
| 508 | user_read_buf_ = NULL; |
| 509 | user_read_buf_len_ = 0; |
| 510 | user_write_buf_ = NULL; |
| 511 | user_write_buf_len_ = 0; |
| 512 | |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 513 | pending_read_error_ = kNoPendingReadResult; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 514 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 515 | pending_read_error_info_ = OpenSSLErrorInfo(); |
| 516 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 517 | transport_read_error_ = OK; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 518 | transport_write_error_ = OK; |
| 519 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 520 | server_cert_verify_result_.Reset(); |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 521 | completed_connect_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 522 | |
| 523 | cert_authorities_.clear(); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 524 | cert_key_types_.clear(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 525 | client_auth_cert_needed_ = false; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 526 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 527 | start_cert_verification_time_ = base::TimeTicks(); |
| 528 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 529 | npn_status_ = kNextProtoUnsupported; |
| 530 | npn_proto_.clear(); |
| 531 | |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 532 | channel_id_xtn_negotiated_ = false; |
| 533 | channel_id_request_handle_.Cancel(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | bool SSLClientSocketOpenSSL::IsConnected() const { |
| 537 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 538 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 539 | return false; |
| 540 | // If an asynchronous operation is still pending. |
| 541 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 542 | return true; |
| 543 | |
| 544 | return transport_->socket()->IsConnected(); |
| 545 | } |
| 546 | |
| 547 | bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
| 548 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 549 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 550 | return false; |
| 551 | // If an asynchronous operation is still pending. |
| 552 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 553 | return false; |
| 554 | // If there is data waiting to be sent, or data read from the network that |
| 555 | // has not yet been consumed. |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 556 | if (BIO_pending(transport_bio_) > 0 || |
| 557 | BIO_wpending(transport_bio_) > 0) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
| 561 | return transport_->socket()->IsConnectedAndIdle(); |
| 562 | } |
| 563 | |
| 564 | int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { |
| 565 | return transport_->socket()->GetPeerAddress(addressList); |
| 566 | } |
| 567 | |
| 568 | int SSLClientSocketOpenSSL::GetLocalAddress(IPEndPoint* addressList) const { |
| 569 | return transport_->socket()->GetLocalAddress(addressList); |
| 570 | } |
| 571 | |
| 572 | const BoundNetLog& SSLClientSocketOpenSSL::NetLog() const { |
| 573 | return net_log_; |
| 574 | } |
| 575 | |
| 576 | void SSLClientSocketOpenSSL::SetSubresourceSpeculation() { |
| 577 | if (transport_.get() && transport_->socket()) { |
| 578 | transport_->socket()->SetSubresourceSpeculation(); |
| 579 | } else { |
| 580 | NOTREACHED(); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void SSLClientSocketOpenSSL::SetOmniboxSpeculation() { |
| 585 | if (transport_.get() && transport_->socket()) { |
| 586 | transport_->socket()->SetOmniboxSpeculation(); |
| 587 | } else { |
| 588 | NOTREACHED(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | bool SSLClientSocketOpenSSL::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 593 | return was_ever_used_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | bool SSLClientSocketOpenSSL::UsingTCPFastOpen() const { |
| 597 | if (transport_.get() && transport_->socket()) |
| 598 | return transport_->socket()->UsingTCPFastOpen(); |
| 599 | |
| 600 | NOTREACHED(); |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
| 605 | ssl_info->Reset(); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 606 | if (server_cert_chain_->empty()) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 607 | return false; |
| 608 | |
| 609 | ssl_info->cert = server_cert_verify_result_.verified_cert; |
| 610 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
| 611 | ssl_info->is_issued_by_known_root = |
| 612 | server_cert_verify_result_.is_issued_by_known_root; |
| 613 | ssl_info->public_key_hashes = |
| 614 | server_cert_verify_result_.public_key_hashes; |
| 615 | ssl_info->client_cert_sent = |
| 616 | ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
| 617 | ssl_info->channel_id_sent = WasChannelIDSent(); |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 618 | ssl_info->pinning_failure_log = pinning_failure_log_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 619 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 620 | AddSCTInfoToSSLInfo(ssl_info); |
| 621 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 622 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 623 | CHECK(cipher); |
| 624 | ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 625 | |
| 626 | ssl_info->connection_status = EncodeSSLConnectionStatus( |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 627 | static_cast<uint16>(SSL_CIPHER_get_id(cipher)), 0 /* no compression */, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 628 | GetNetSSLVersion(ssl_)); |
| 629 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 630 | if (!SSL_get_secure_renegotiation_support(ssl_)) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 631 | ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 632 | |
| 633 | if (ssl_config_.version_fallback) |
| 634 | ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
| 635 | |
| 636 | ssl_info->handshake_type = SSL_session_reused(ssl_) ? |
| 637 | SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; |
| 638 | |
| 639 | DVLOG(3) << "Encoded connection status: cipher suite = " |
| 640 | << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) |
| 641 | << " version = " |
| 642 | << SSLConnectionStatusToVersion(ssl_info->connection_status); |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | int SSLClientSocketOpenSSL::Read(IOBuffer* buf, |
| 647 | int buf_len, |
| 648 | const CompletionCallback& callback) { |
| 649 | user_read_buf_ = buf; |
| 650 | user_read_buf_len_ = buf_len; |
| 651 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 652 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 653 | |
| 654 | if (rv == ERR_IO_PENDING) { |
| 655 | user_read_callback_ = callback; |
| 656 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 657 | if (rv > 0) |
| 658 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 659 | user_read_buf_ = NULL; |
| 660 | user_read_buf_len_ = 0; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 661 | if (rv <= 0) { |
| 662 | // Failure of a read attempt may indicate a failed false start |
| 663 | // connection. |
| 664 | OnHandshakeCompletion(); |
| 665 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | return rv; |
| 669 | } |
| 670 | |
| 671 | int SSLClientSocketOpenSSL::Write(IOBuffer* buf, |
| 672 | int buf_len, |
| 673 | const CompletionCallback& callback) { |
| 674 | user_write_buf_ = buf; |
| 675 | user_write_buf_len_ = buf_len; |
| 676 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 677 | int rv = DoWriteLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 678 | |
| 679 | if (rv == ERR_IO_PENDING) { |
| 680 | user_write_callback_ = callback; |
| 681 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 682 | if (rv > 0) |
| 683 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 684 | user_write_buf_ = NULL; |
| 685 | user_write_buf_len_ = 0; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 686 | if (rv < 0) { |
| 687 | // Failure of a write attempt may indicate a failed false start |
| 688 | // connection. |
| 689 | OnHandshakeCompletion(); |
| 690 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return rv; |
| 694 | } |
| 695 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 696 | int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 697 | return transport_->socket()->SetReceiveBufferSize(size); |
| 698 | } |
| 699 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 700 | int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 701 | return transport_->socket()->SetSendBufferSize(size); |
| 702 | } |
| 703 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 704 | int SSLClientSocketOpenSSL::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 705 | DCHECK(!ssl_); |
| 706 | DCHECK(!transport_bio_); |
| 707 | |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 708 | SSLContext* context = SSLContext::GetInstance(); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 709 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 710 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 711 | ssl_ = SSL_new(context->ssl_ctx()); |
| 712 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 713 | return ERR_UNEXPECTED; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 714 | |
| 715 | if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 716 | return ERR_UNEXPECTED; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 717 | |
[email protected] | e4738ba5 | 2014-08-07 10:07:22 | [diff] [blame] | 718 | // Set an OpenSSL callback to monitor this SSL*'s connection. |
| 719 | SSL_set_info_callback(ssl_, &InfoCallback); |
| 720 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 721 | trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 722 | ssl_, GetSessionCacheKey()); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 723 | |
| 724 | BIO* ssl_bio = NULL; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 725 | // 0 => use default buffer sizes. |
| 726 | if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 727 | return ERR_UNEXPECTED; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 728 | DCHECK(ssl_bio); |
| 729 | DCHECK(transport_bio_); |
| 730 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 731 | // Install a callback on OpenSSL's end to plumb transport errors through. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 732 | BIO_set_callback(ssl_bio, BIOCallback); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 733 | BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); |
| 734 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 735 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 736 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 737 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 738 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 739 | SslSetClearMask options; |
| 740 | options.ConfigureFlag(SSL_OP_NO_SSLv2, true); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 741 | bool ssl3_enabled = (ssl_config_.version_min == SSL_PROTOCOL_VERSION_SSL3); |
| 742 | options.ConfigureFlag(SSL_OP_NO_SSLv3, !ssl3_enabled); |
| 743 | bool tls1_enabled = (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1 && |
| 744 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1); |
| 745 | options.ConfigureFlag(SSL_OP_NO_TLSv1, !tls1_enabled); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 746 | bool tls1_1_enabled = |
| 747 | (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1_1 && |
| 748 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1_1); |
| 749 | options.ConfigureFlag(SSL_OP_NO_TLSv1_1, !tls1_1_enabled); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 750 | bool tls1_2_enabled = |
| 751 | (ssl_config_.version_min <= SSL_PROTOCOL_VERSION_TLS1_2 && |
| 752 | ssl_config_.version_max >= SSL_PROTOCOL_VERSION_TLS1_2); |
| 753 | options.ConfigureFlag(SSL_OP_NO_TLSv1_2, !tls1_2_enabled); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 754 | |
[email protected] | d0f0049 | 2012-08-03 22:35:13 | [diff] [blame] | 755 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 756 | |
| 757 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 758 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 759 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 760 | SSL_set_options(ssl_, options.set_mask); |
| 761 | SSL_clear_options(ssl_, options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 762 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 763 | // Same as above, this time for the SSL mode. |
| 764 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 765 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 766 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
isherman | e5c05e1 | 2014-09-09 20:32:15 | [diff] [blame] | 767 | mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 768 | |
[email protected] | b788de0 | 2014-04-23 18:06:07 | [diff] [blame] | 769 | mode.ConfigureFlag(SSL_MODE_HANDSHAKE_CUTTHROUGH, |
| 770 | ssl_config_.false_start_enabled); |
| 771 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 772 | SSL_set_mode(ssl_, mode.set_mask); |
| 773 | SSL_clear_mode(ssl_, mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 774 | |
| 775 | // Removing ciphers by ID from OpenSSL is a bit involved as we must use the |
| 776 | // textual name with SSL_set_cipher_list because there is no public API to |
| 777 | // directly remove a cipher by ID. |
| 778 | STACK_OF(SSL_CIPHER)* ciphers = SSL_get_ciphers(ssl_); |
| 779 | DCHECK(ciphers); |
| 780 | // See SSLConfig::disabled_cipher_suites for description of the suites |
[email protected] | 9b4bc4a9 | 2013-08-20 22:59:07 | [diff] [blame] | 781 | // disabled by default. Note that !SHA256 and !SHA384 only remove HMAC-SHA256 |
| 782 | // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
| 783 | // as the handshake hash. |
| 784 | std::string command("DEFAULT:!NULL:!aNULL:!IDEA:!FZA:!SRP:!SHA256:!SHA384:" |
| 785 | "!aECDH:!AESGCM+AES256"); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 786 | // Walk through all the installed ciphers, seeing if any need to be |
| 787 | // appended to the cipher removal |command|. |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 788 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 789 | const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 790 | const uint16 id = static_cast<uint16>(SSL_CIPHER_get_id(cipher)); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 791 | // Remove any ciphers with a strength of less than 80 bits. Note the NSS |
| 792 | // implementation uses "effective" bits here but OpenSSL does not provide |
| 793 | // this detail. This only impacts Triple DES: reports 112 vs. 168 bits, |
| 794 | // both of which are greater than 80 anyway. |
| 795 | bool disable = SSL_CIPHER_get_bits(cipher, NULL) < 80; |
| 796 | if (!disable) { |
| 797 | disable = std::find(ssl_config_.disabled_cipher_suites.begin(), |
| 798 | ssl_config_.disabled_cipher_suites.end(), id) != |
| 799 | ssl_config_.disabled_cipher_suites.end(); |
| 800 | } |
| 801 | if (disable) { |
| 802 | const char* name = SSL_CIPHER_get_name(cipher); |
| 803 | DVLOG(3) << "Found cipher to remove: '" << name << "', ID: " << id |
| 804 | << " strength: " << SSL_CIPHER_get_bits(cipher, NULL); |
| 805 | command.append(":!"); |
| 806 | command.append(name); |
| 807 | } |
| 808 | } |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 809 | |
| 810 | // Disable ECDSA cipher suites on platforms that do not support ECDSA |
| 811 | // signed certificates, as servers may use the presence of such |
| 812 | // ciphersuites as a hint to send an ECDSA certificate. |
| 813 | #if defined(OS_WIN) |
| 814 | if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 815 | command.append(":!ECDSA"); |
| 816 | #endif |
| 817 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 818 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 819 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 820 | // This will almost certainly result in the socket failing to complete the |
| 821 | // handshake at which point the appropriate error is bubbled up to the client. |
| 822 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
| 823 | "returned " << rv; |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 824 | |
[email protected] | 0d0a687 | 2014-07-26 18:05:11 | [diff] [blame] | 825 | if (ssl_config_.version_fallback) |
| 826 | SSL_enable_fallback_scsv(ssl_); |
| 827 | |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 828 | // TLS channel ids. |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 829 | if (IsChannelIDEnabled(ssl_config_, channel_id_service_)) { |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 830 | SSL_enable_tls_channel_id(ssl_); |
| 831 | } |
| 832 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 833 | if (!ssl_config_.next_protos.empty()) { |
| 834 | std::vector<uint8_t> wire_protos = |
| 835 | SerializeNextProtos(ssl_config_.next_protos); |
| 836 | SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
| 837 | wire_protos.size()); |
| 838 | } |
| 839 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 840 | if (ssl_config_.signed_cert_timestamps_enabled) { |
| 841 | SSL_enable_signed_cert_timestamps(ssl_); |
| 842 | SSL_enable_ocsp_stapling(ssl_); |
| 843 | } |
| 844 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 845 | if (IsOCSPStaplingSupported()) |
| 846 | SSL_enable_ocsp_stapling(ssl_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 847 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 848 | return OK; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 849 | } |
| 850 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 851 | void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
| 852 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 853 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 854 | if (rv > 0) |
| 855 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 856 | user_read_buf_ = NULL; |
| 857 | user_read_buf_len_ = 0; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 858 | if (rv <= 0) { |
| 859 | // Failure of a read attempt may indicate a failed false start |
| 860 | // connection. |
| 861 | OnHandshakeCompletion(); |
| 862 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 863 | base::ResetAndReturn(&user_read_callback_).Run(rv); |
| 864 | } |
| 865 | |
| 866 | void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
| 867 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 868 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 869 | if (rv > 0) |
| 870 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 871 | user_write_buf_ = NULL; |
| 872 | user_write_buf_len_ = 0; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 873 | if (rv < 0) { |
| 874 | // Failure of a write attempt may indicate a failed false start |
| 875 | // connection. |
| 876 | OnHandshakeCompletion(); |
| 877 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 878 | base::ResetAndReturn(&user_write_callback_).Run(rv); |
| 879 | } |
| 880 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 881 | void SSLClientSocketOpenSSL::OnHandshakeCompletion() { |
| 882 | if (!handshake_completion_callback_.is_null()) |
| 883 | base::ResetAndReturn(&handshake_completion_callback_).Run(); |
| 884 | } |
| 885 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 886 | bool SSLClientSocketOpenSSL::DoTransportIO() { |
| 887 | bool network_moved = false; |
| 888 | int rv; |
| 889 | // Read and write as much data as possible. The loop is necessary because |
| 890 | // Write() may return synchronously. |
| 891 | do { |
| 892 | rv = BufferSend(); |
| 893 | if (rv != ERR_IO_PENDING && rv != 0) |
| 894 | network_moved = true; |
| 895 | } while (rv > 0); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 896 | if (transport_read_error_ == OK && BufferRecv() != ERR_IO_PENDING) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 897 | network_moved = true; |
| 898 | return network_moved; |
| 899 | } |
| 900 | |
| 901 | int SSLClientSocketOpenSSL::DoHandshake() { |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 902 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
| 903 | tracked_objects::ScopedTracker tracking_profile1( |
| 904 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 905 | "424386 SSLClientSocketOpenSSL::DoHandshake1")); |
| 906 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 907 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 908 | int net_error = OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 909 | int rv = SSL_do_handshake(ssl_); |
| 910 | |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 911 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
| 912 | tracked_objects::ScopedTracker tracking_profile2( |
| 913 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 914 | "424386 SSLClientSocketOpenSSL::DoHandshake2")); |
| 915 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 916 | if (client_auth_cert_needed_) { |
| 917 | net_error = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 918 | // If the handshake already succeeded (because the server requests but |
| 919 | // doesn't require a client cert), we need to invalidate the SSL session |
| 920 | // so that we won't try to resume the non-client-authenticated session in |
| 921 | // the next handshake. This will cause the server to ask for a client |
| 922 | // cert again. |
| 923 | if (rv == 1) { |
| 924 | // Remove from session cache but don't clear this connection. |
| 925 | SSL_SESSION* session = SSL_get_session(ssl_); |
| 926 | if (session) { |
| 927 | int rv = SSL_CTX_remove_session(SSL_get_SSL_CTX(ssl_), session); |
| 928 | LOG_IF(WARNING, !rv) << "Couldn't invalidate SSL session: " << session; |
| 929 | } |
| 930 | } |
| 931 | } else if (rv == 1) { |
| 932 | if (trying_cached_session_ && logging::DEBUG_MODE) { |
| 933 | DVLOG(2) << "Result of session reuse for " << host_and_port_.ToString() |
| 934 | << " is: " << (SSL_session_reused(ssl_) ? "Success" : "Fail"); |
| 935 | } |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 936 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 937 | if (ssl_config_.version_fallback && |
| 938 | ssl_config_.version_max < ssl_config_.version_fallback_min) { |
| 939 | return ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION; |
| 940 | } |
| 941 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 942 | // SSL handshake is completed. If NPN wasn't negotiated, see if ALPN was. |
| 943 | if (npn_status_ == kNextProtoUnsupported) { |
| 944 | const uint8_t* alpn_proto = NULL; |
| 945 | unsigned alpn_len = 0; |
| 946 | SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); |
| 947 | if (alpn_len > 0) { |
| 948 | npn_proto_.assign(reinterpret_cast<const char*>(alpn_proto), alpn_len); |
| 949 | npn_status_ = kNextProtoNegotiated; |
bnc | 0d28ea5 | 2014-10-13 15:15:38 | [diff] [blame] | 950 | set_negotiation_extension(kExtensionALPN); |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 954 | RecordChannelIDSupport(channel_id_service_, |
| 955 | channel_id_xtn_negotiated_, |
| 956 | ssl_config_.channel_id_enabled, |
| 957 | crypto::ECPrivateKey::IsSupported()); |
| 958 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 959 | // Only record OCSP histograms if OCSP was requested. |
| 960 | if (ssl_config_.signed_cert_timestamps_enabled || |
| 961 | IsOCSPStaplingSupported()) { |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 962 | const uint8_t* ocsp_response; |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 963 | size_t ocsp_response_len; |
| 964 | SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
| 965 | |
| 966 | set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 967 | UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
| 968 | } |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 969 | |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 970 | const uint8_t* sct_list; |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 971 | size_t sct_list_len; |
| 972 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 973 | set_signed_cert_timestamps_received(sct_list_len != 0); |
| 974 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 975 | // Verify the certificate. |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 976 | UpdateServerCert(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 977 | GotoState(STATE_VERIFY_CERT); |
| 978 | } else { |
| 979 | int ssl_error = SSL_get_error(ssl_, rv); |
| 980 | |
| 981 | if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 982 | // The server supports channel ID. Stop to look one up before returning to |
| 983 | // the handshake. |
| 984 | channel_id_xtn_negotiated_ = true; |
| 985 | GotoState(STATE_CHANNEL_ID_LOOKUP); |
| 986 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 987 | } |
| 988 | |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 989 | OpenSSLErrorInfo error_info; |
| 990 | net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 991 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 992 | // If not done, stay in this state |
| 993 | if (net_error == ERR_IO_PENDING) { |
| 994 | GotoState(STATE_HANDSHAKE); |
| 995 | } else { |
| 996 | LOG(ERROR) << "handshake failed; returned " << rv |
| 997 | << ", SSL error code " << ssl_error |
| 998 | << ", net_error " << net_error; |
| 999 | net_log_.AddEvent( |
| 1000 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 1001 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | return net_error; |
| 1005 | } |
| 1006 | |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1007 | int SSLClientSocketOpenSSL::DoChannelIDLookup() { |
| 1008 | GotoState(STATE_CHANNEL_ID_LOOKUP_COMPLETE); |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 1009 | return channel_id_service_->GetOrCreateChannelID( |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1010 | host_and_port_.host(), |
| 1011 | &channel_id_private_key_, |
| 1012 | &channel_id_cert_, |
| 1013 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1014 | base::Unretained(this)), |
| 1015 | &channel_id_request_handle_); |
| 1016 | } |
| 1017 | |
| 1018 | int SSLClientSocketOpenSSL::DoChannelIDLookupComplete(int result) { |
| 1019 | if (result < 0) |
| 1020 | return result; |
| 1021 | |
| 1022 | DCHECK_LT(0u, channel_id_private_key_.size()); |
| 1023 | // Decode key. |
| 1024 | std::vector<uint8> encrypted_private_key_info; |
| 1025 | std::vector<uint8> subject_public_key_info; |
| 1026 | encrypted_private_key_info.assign( |
| 1027 | channel_id_private_key_.data(), |
| 1028 | channel_id_private_key_.data() + channel_id_private_key_.size()); |
| 1029 | subject_public_key_info.assign( |
| 1030 | channel_id_cert_.data(), |
| 1031 | channel_id_cert_.data() + channel_id_cert_.size()); |
| 1032 | scoped_ptr<crypto::ECPrivateKey> ec_private_key( |
| 1033 | crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 1034 | ChannelIDService::kEPKIPassword, |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1035 | encrypted_private_key_info, |
| 1036 | subject_public_key_info)); |
| 1037 | if (!ec_private_key) { |
| 1038 | LOG(ERROR) << "Failed to import Channel ID."; |
| 1039 | return ERR_CHANNEL_ID_IMPORT_FAILED; |
| 1040 | } |
| 1041 | |
| 1042 | // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key |
| 1043 | // type. |
| 1044 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1045 | int rv = SSL_set1_tls_channel_id(ssl_, ec_private_key->key()); |
| 1046 | if (!rv) { |
| 1047 | LOG(ERROR) << "Failed to set Channel ID."; |
| 1048 | int err = SSL_get_error(ssl_, rv); |
| 1049 | return MapOpenSSLError(err, err_tracer); |
| 1050 | } |
| 1051 | |
| 1052 | // Return to the handshake. |
| 1053 | set_channel_id_sent(true); |
| 1054 | GotoState(STATE_HANDSHAKE); |
| 1055 | return OK; |
| 1056 | } |
| 1057 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1058 | int SSLClientSocketOpenSSL::DoVerifyCert(int result) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1059 | DCHECK(!server_cert_chain_->empty()); |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1060 | DCHECK(start_cert_verification_time_.is_null()); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1061 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1062 | GotoState(STATE_VERIFY_CERT_COMPLETE); |
| 1063 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1064 | // If the certificate is bad and has been previously accepted, use |
| 1065 | // the previous status and bypass the error. |
| 1066 | base::StringPiece der_cert; |
| 1067 | if (!x509_util::GetDER(server_cert_chain_->Get(0), &der_cert)) { |
| 1068 | NOTREACHED(); |
| 1069 | return ERR_CERT_INVALID; |
| 1070 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1071 | CertStatus cert_status; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1072 | if (ssl_config_.IsAllowedBadCert(der_cert, &cert_status)) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1073 | VLOG(1) << "Received an expected bad cert with status: " << cert_status; |
| 1074 | server_cert_verify_result_.Reset(); |
| 1075 | server_cert_verify_result_.cert_status = cert_status; |
| 1076 | server_cert_verify_result_.verified_cert = server_cert_; |
| 1077 | return OK; |
| 1078 | } |
| 1079 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1080 | // When running in a sandbox, it may not be possible to create an |
| 1081 | // X509Certificate*, as that may depend on OS functionality blocked |
| 1082 | // in the sandbox. |
| 1083 | if (!server_cert_.get()) { |
| 1084 | server_cert_verify_result_.Reset(); |
| 1085 | server_cert_verify_result_.cert_status = CERT_STATUS_INVALID; |
| 1086 | return ERR_CERT_INVALID; |
| 1087 | } |
| 1088 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1089 | start_cert_verification_time_ = base::TimeTicks::Now(); |
| 1090 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1091 | int flags = 0; |
| 1092 | if (ssl_config_.rev_checking_enabled) |
| 1093 | flags |= CertVerifier::VERIFY_REV_CHECKING_ENABLED; |
| 1094 | if (ssl_config_.verify_ev_cert) |
| 1095 | flags |= CertVerifier::VERIFY_EV_CERT; |
| 1096 | if (ssl_config_.cert_io_enabled) |
| 1097 | flags |= CertVerifier::VERIFY_CERT_IO_ENABLED; |
| 1098 | if (ssl_config_.rev_checking_required_local_anchors) |
| 1099 | flags |= CertVerifier::VERIFY_REV_CHECKING_REQUIRED_LOCAL_ANCHORS; |
| 1100 | verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); |
| 1101 | return verifier_->Verify( |
| 1102 | server_cert_.get(), |
| 1103 | host_and_port_.host(), |
| 1104 | flags, |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 1105 | // TODO(davidben): Route the CRLSet through SSLConfig so |
| 1106 | // SSLClientSocket doesn't depend on SSLConfigService. |
| 1107 | SSLConfigService::GetCRLSet().get(), |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1108 | &server_cert_verify_result_, |
| 1109 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1110 | base::Unretained(this)), |
| 1111 | net_log_); |
| 1112 | } |
| 1113 | |
| 1114 | int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
| 1115 | verifier_.reset(); |
| 1116 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1117 | if (!start_cert_verification_time_.is_null()) { |
| 1118 | base::TimeDelta verify_time = |
| 1119 | base::TimeTicks::Now() - start_cert_verification_time_; |
| 1120 | if (result == OK) { |
| 1121 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); |
| 1122 | } else { |
| 1123 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); |
| 1124 | } |
| 1125 | } |
| 1126 | |
davidben | 7c7ab60 | 2014-11-05 22:27:44 | [diff] [blame] | 1127 | if (result == OK) |
| 1128 | RecordConnectionTypeMetrics(GetNetSSLVersion(ssl_)); |
| 1129 | |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1130 | const CertStatus cert_status = server_cert_verify_result_.cert_status; |
| 1131 | if (transport_security_state_ && |
| 1132 | (result == OK || |
| 1133 | (IsCertificateError(result) && IsCertStatusMinorError(cert_status))) && |
| 1134 | !transport_security_state_->CheckPublicKeyPins( |
| 1135 | host_and_port_.host(), |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1136 | server_cert_verify_result_.is_issued_by_known_root, |
| 1137 | server_cert_verify_result_.public_key_hashes, |
| 1138 | &pinning_failure_log_)) { |
| 1139 | result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
| 1140 | } |
| 1141 | |
eranm | efbd313 | 2014-10-28 16:35:16 | [diff] [blame] | 1142 | scoped_refptr<ct::EVCertsWhitelist> ev_whitelist = |
| 1143 | SSLConfigService::GetEVCertsWhitelist(); |
| 1144 | if (server_cert_verify_result_.cert_status & CERT_STATUS_IS_EV) { |
| 1145 | if (ev_whitelist.get() && ev_whitelist->IsValid()) { |
| 1146 | const SHA256HashValue fingerprint( |
| 1147 | X509Certificate::CalculateFingerprint256( |
| 1148 | server_cert_verify_result_.verified_cert->os_cert_handle())); |
| 1149 | |
| 1150 | UMA_HISTOGRAM_BOOLEAN( |
| 1151 | "Net.SSL_EVCertificateInWhitelist", |
| 1152 | ev_whitelist->ContainsCertificateHash( |
| 1153 | std::string(reinterpret_cast<const char*>(fingerprint.data), 8))); |
| 1154 | } |
| 1155 | } |
| 1156 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1157 | if (result == OK) { |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1158 | // Only check Certificate Transparency if there were no other errors with |
| 1159 | // the connection. |
| 1160 | VerifyCT(); |
| 1161 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1162 | // TODO(joth): Work out if we need to remember the intermediate CA certs |
| 1163 | // when the server sends them to us, and do so here. |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 1164 | SSLContext::GetInstance()->session_cache()->MarkSSLSessionAsGood(ssl_); |
[email protected] | e4738ba5 | 2014-08-07 10:07:22 | [diff] [blame] | 1165 | marked_session_as_good_ = true; |
| 1166 | CheckIfHandshakeFinished(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1167 | } else { |
| 1168 | DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
| 1169 | << " (" << result << ")"; |
| 1170 | } |
| 1171 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1172 | completed_connect_ = true; |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1173 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1174 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 1175 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 1176 | return result; |
| 1177 | } |
| 1178 | |
| 1179 | void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 1180 | if (rv < OK) |
| 1181 | OnHandshakeCompletion(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1182 | if (!user_connect_callback_.is_null()) { |
| 1183 | CompletionCallback c = user_connect_callback_; |
| 1184 | user_connect_callback_.Reset(); |
| 1185 | c.Run(rv > OK ? OK : rv); |
| 1186 | } |
| 1187 | } |
| 1188 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1189 | void SSLClientSocketOpenSSL::UpdateServerCert() { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1190 | server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1191 | server_cert_ = server_cert_chain_->AsOSChain(); |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1192 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1193 | if (server_cert_.get()) { |
| 1194 | net_log_.AddEvent( |
| 1195 | NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
| 1196 | base::Bind(&NetLogX509CertificateCallback, |
| 1197 | base::Unretained(server_cert_.get()))); |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 1198 | |
| 1199 | // TODO(rsleevi): Plumb an OCSP response into the Mac system library and |
| 1200 | // update IsOCSPStaplingSupported for Mac. https://crbug.com/430714 |
| 1201 | if (IsOCSPStaplingSupported()) { |
| 1202 | #if defined(OS_WIN) |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 1203 | const uint8_t* ocsp_response_raw; |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 1204 | size_t ocsp_response_len; |
| 1205 | SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1206 | |
| 1207 | CRYPT_DATA_BLOB ocsp_response_blob; |
| 1208 | ocsp_response_blob.cbData = ocsp_response_len; |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 1209 | ocsp_response_blob.pbData = const_cast<BYTE*>(ocsp_response_raw); |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 1210 | BOOL ok = CertSetCertificateContextProperty( |
| 1211 | server_cert_->os_cert_handle(), |
| 1212 | CERT_OCSP_RESPONSE_PROP_ID, |
| 1213 | CERT_SET_PROPERTY_IGNORE_PERSIST_ERROR_FLAG, |
| 1214 | &ocsp_response_blob); |
| 1215 | if (!ok) { |
| 1216 | VLOG(1) << "Failed to set OCSP response property: " |
| 1217 | << GetLastError(); |
| 1218 | } |
| 1219 | #else |
| 1220 | NOTREACHED(); |
| 1221 | #endif |
| 1222 | } |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1223 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1224 | } |
| 1225 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1226 | void SSLClientSocketOpenSSL::VerifyCT() { |
| 1227 | if (!cert_transparency_verifier_) |
| 1228 | return; |
| 1229 | |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 1230 | const uint8_t* ocsp_response_raw; |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1231 | size_t ocsp_response_len; |
| 1232 | SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1233 | std::string ocsp_response; |
| 1234 | if (ocsp_response_len > 0) { |
| 1235 | ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1236 | ocsp_response_len); |
| 1237 | } |
| 1238 | |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame^] | 1239 | const uint8_t* sct_list_raw; |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1240 | size_t sct_list_len; |
| 1241 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list_raw, &sct_list_len); |
| 1242 | std::string sct_list; |
| 1243 | if (sct_list_len > 0) |
| 1244 | sct_list.assign(reinterpret_cast<const char*>(sct_list_raw), sct_list_len); |
| 1245 | |
| 1246 | // Note that this is a completely synchronous operation: The CT Log Verifier |
| 1247 | // gets all the data it needs for SCT verification and does not do any |
| 1248 | // external communication. |
| 1249 | int result = cert_transparency_verifier_->Verify( |
| 1250 | server_cert_verify_result_.verified_cert.get(), |
| 1251 | ocsp_response, sct_list, &ct_verify_result_, net_log_); |
| 1252 | |
| 1253 | VLOG(1) << "CT Verification complete: result " << result |
| 1254 | << " Invalid scts: " << ct_verify_result_.invalid_scts.size() |
| 1255 | << " Verified scts: " << ct_verify_result_.verified_scts.size() |
| 1256 | << " scts from unknown logs: " |
| 1257 | << ct_verify_result_.unknown_logs_scts.size(); |
| 1258 | } |
| 1259 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1260 | void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 1261 | int rv = DoHandshakeLoop(result); |
| 1262 | if (rv != ERR_IO_PENDING) { |
| 1263 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 1264 | DoConnectCallback(rv); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
| 1269 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1270 | // In handshake phase. |
| 1271 | OnHandshakeIOComplete(result); |
| 1272 | return; |
| 1273 | } |
| 1274 | |
| 1275 | // OnSendComplete may need to call DoPayloadRead while the renegotiation |
| 1276 | // handshake is in progress. |
| 1277 | int rv_read = ERR_IO_PENDING; |
| 1278 | int rv_write = ERR_IO_PENDING; |
| 1279 | bool network_moved; |
| 1280 | do { |
| 1281 | if (user_read_buf_.get()) |
| 1282 | rv_read = DoPayloadRead(); |
| 1283 | if (user_write_buf_.get()) |
| 1284 | rv_write = DoPayloadWrite(); |
| 1285 | network_moved = DoTransportIO(); |
| 1286 | } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && |
| 1287 | (user_read_buf_.get() || user_write_buf_.get()) && network_moved); |
| 1288 | |
| 1289 | // Performing the Read callback may cause |this| to be deleted. If this |
| 1290 | // happens, the Write callback should not be invoked. Guard against this by |
| 1291 | // holding a WeakPtr to |this| and ensuring it's still valid. |
| 1292 | base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); |
| 1293 | if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) |
| 1294 | DoReadCallback(rv_read); |
| 1295 | |
| 1296 | if (!guard.get()) |
| 1297 | return; |
| 1298 | |
| 1299 | if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) |
| 1300 | DoWriteCallback(rv_write); |
| 1301 | } |
| 1302 | |
| 1303 | void SSLClientSocketOpenSSL::OnRecvComplete(int result) { |
| 1304 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1305 | // In handshake phase. |
| 1306 | OnHandshakeIOComplete(result); |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Network layer received some data, check if client requested to read |
| 1311 | // decrypted data. |
| 1312 | if (!user_read_buf_.get()) |
| 1313 | return; |
| 1314 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1315 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1316 | if (rv != ERR_IO_PENDING) |
| 1317 | DoReadCallback(rv); |
| 1318 | } |
| 1319 | |
| 1320 | int SSLClientSocketOpenSSL::DoHandshakeLoop(int last_io_result) { |
| 1321 | int rv = last_io_result; |
| 1322 | do { |
| 1323 | // Default to STATE_NONE for next state. |
| 1324 | // (This is a quirk carried over from the windows |
| 1325 | // implementation. It makes reading the logs a bit harder.) |
| 1326 | // State handlers can and often do call GotoState just |
| 1327 | // to stay in the current state. |
| 1328 | State state = next_handshake_state_; |
| 1329 | GotoState(STATE_NONE); |
| 1330 | switch (state) { |
| 1331 | case STATE_HANDSHAKE: |
| 1332 | rv = DoHandshake(); |
| 1333 | break; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1334 | case STATE_CHANNEL_ID_LOOKUP: |
| 1335 | DCHECK_EQ(OK, rv); |
| 1336 | rv = DoChannelIDLookup(); |
| 1337 | break; |
| 1338 | case STATE_CHANNEL_ID_LOOKUP_COMPLETE: |
| 1339 | rv = DoChannelIDLookupComplete(rv); |
| 1340 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1341 | case STATE_VERIFY_CERT: |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1342 | DCHECK_EQ(OK, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1343 | rv = DoVerifyCert(rv); |
| 1344 | break; |
| 1345 | case STATE_VERIFY_CERT_COMPLETE: |
| 1346 | rv = DoVerifyCertComplete(rv); |
| 1347 | break; |
| 1348 | case STATE_NONE: |
| 1349 | default: |
| 1350 | rv = ERR_UNEXPECTED; |
| 1351 | NOTREACHED() << "unexpected state" << state; |
| 1352 | break; |
| 1353 | } |
| 1354 | |
| 1355 | bool network_moved = DoTransportIO(); |
| 1356 | if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 1357 | // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 1358 | // special case we keep looping even if rv is ERR_IO_PENDING because |
| 1359 | // the transport IO may allow DoHandshake to make progress. |
| 1360 | rv = OK; // This causes us to stay in the loop. |
| 1361 | } |
| 1362 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 1363 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1364 | return rv; |
| 1365 | } |
| 1366 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1367 | int SSLClientSocketOpenSSL::DoReadLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1368 | bool network_moved; |
| 1369 | int rv; |
| 1370 | do { |
| 1371 | rv = DoPayloadRead(); |
| 1372 | network_moved = DoTransportIO(); |
| 1373 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1374 | |
| 1375 | return rv; |
| 1376 | } |
| 1377 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1378 | int SSLClientSocketOpenSSL::DoWriteLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1379 | bool network_moved; |
| 1380 | int rv; |
| 1381 | do { |
| 1382 | rv = DoPayloadWrite(); |
| 1383 | network_moved = DoTransportIO(); |
| 1384 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1385 | |
| 1386 | return rv; |
| 1387 | } |
| 1388 | |
| 1389 | int SSLClientSocketOpenSSL::DoPayloadRead() { |
| 1390 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1391 | |
| 1392 | int rv; |
| 1393 | if (pending_read_error_ != kNoPendingReadResult) { |
| 1394 | rv = pending_read_error_; |
| 1395 | pending_read_error_ = kNoPendingReadResult; |
| 1396 | if (rv == 0) { |
| 1397 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1398 | rv, user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1399 | } else { |
| 1400 | net_log_.AddEvent( |
| 1401 | NetLog::TYPE_SSL_READ_ERROR, |
| 1402 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1403 | pending_read_error_info_)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1404 | } |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1405 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1406 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1407 | return rv; |
| 1408 | } |
| 1409 | |
| 1410 | int total_bytes_read = 0; |
| 1411 | do { |
| 1412 | rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
| 1413 | user_read_buf_len_ - total_bytes_read); |
| 1414 | if (rv > 0) |
| 1415 | total_bytes_read += rv; |
| 1416 | } while (total_bytes_read < user_read_buf_len_ && rv > 0); |
| 1417 | |
| 1418 | if (total_bytes_read == user_read_buf_len_) { |
| 1419 | rv = total_bytes_read; |
| 1420 | } else { |
| 1421 | // Otherwise, an error occurred (rv <= 0). The error needs to be handled |
| 1422 | // immediately, while the OpenSSL errors are still available in |
| 1423 | // thread-local storage. However, the handled/remapped error code should |
| 1424 | // only be returned if no application data was already read; if it was, the |
| 1425 | // error code should be deferred until the next call of DoPayloadRead. |
| 1426 | // |
| 1427 | // If no data was read, |*next_result| will point to the return value of |
| 1428 | // this function. If at least some data was read, |*next_result| will point |
| 1429 | // to |pending_read_error_|, to be returned in a future call to |
| 1430 | // DoPayloadRead() (e.g.: after the current data is handled). |
| 1431 | int *next_result = &rv; |
| 1432 | if (total_bytes_read > 0) { |
| 1433 | pending_read_error_ = rv; |
| 1434 | rv = total_bytes_read; |
| 1435 | next_result = &pending_read_error_; |
| 1436 | } |
| 1437 | |
| 1438 | if (client_auth_cert_needed_) { |
| 1439 | *next_result = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1440 | } else if (*next_result < 0) { |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1441 | pending_read_ssl_error_ = SSL_get_error(ssl_, *next_result); |
| 1442 | *next_result = MapOpenSSLErrorWithDetails(pending_read_ssl_error_, |
| 1443 | err_tracer, |
| 1444 | &pending_read_error_info_); |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1445 | |
| 1446 | // Many servers do not reliably send a close_notify alert when shutting |
| 1447 | // down a connection, and instead terminate the TCP connection. This is |
| 1448 | // reported as ERR_CONNECTION_CLOSED. Because of this, map the unclean |
| 1449 | // shutdown to a graceful EOF, instead of treating it as an error as it |
| 1450 | // should be. |
| 1451 | if (*next_result == ERR_CONNECTION_CLOSED) |
| 1452 | *next_result = 0; |
| 1453 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1454 | if (rv > 0 && *next_result == ERR_IO_PENDING) { |
| 1455 | // If at least some data was read from SSL_read(), do not treat |
| 1456 | // insufficient data as an error to return in the next call to |
| 1457 | // DoPayloadRead() - instead, let the call fall through to check |
| 1458 | // SSL_read() again. This is because DoTransportIO() may complete |
| 1459 | // in between the next call to DoPayloadRead(), and thus it is |
| 1460 | // important to check SSL_read() on subsequent invocations to see |
| 1461 | // if a complete record may now be read. |
| 1462 | *next_result = kNoPendingReadResult; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | if (rv >= 0) { |
| 1468 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1469 | user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1470 | } else if (rv != ERR_IO_PENDING) { |
| 1471 | net_log_.AddEvent( |
| 1472 | NetLog::TYPE_SSL_READ_ERROR, |
| 1473 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1474 | pending_read_error_info_)); |
| 1475 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1476 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1477 | } |
| 1478 | return rv; |
| 1479 | } |
| 1480 | |
| 1481 | int SSLClientSocketOpenSSL::DoPayloadWrite() { |
| 1482 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1483 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1484 | if (rv >= 0) { |
| 1485 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1486 | user_write_buf_->data()); |
| 1487 | return rv; |
| 1488 | } |
| 1489 | |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1490 | int ssl_error = SSL_get_error(ssl_, rv); |
| 1491 | OpenSSLErrorInfo error_info; |
| 1492 | int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
| 1493 | &error_info); |
| 1494 | |
| 1495 | if (net_error != ERR_IO_PENDING) { |
| 1496 | net_log_.AddEvent( |
| 1497 | NetLog::TYPE_SSL_WRITE_ERROR, |
| 1498 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1499 | } |
| 1500 | return net_error; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | int SSLClientSocketOpenSSL::BufferSend(void) { |
| 1504 | if (transport_send_busy_) |
| 1505 | return ERR_IO_PENDING; |
| 1506 | |
| 1507 | if (!send_buffer_.get()) { |
| 1508 | // Get a fresh send buffer out of the send BIO. |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 1509 | size_t max_read = BIO_pending(transport_bio_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1510 | if (!max_read) |
| 1511 | return 0; // Nothing pending in the OpenSSL write BIO. |
| 1512 | send_buffer_ = new DrainableIOBuffer(new IOBuffer(max_read), max_read); |
| 1513 | int read_bytes = BIO_read(transport_bio_, send_buffer_->data(), max_read); |
| 1514 | DCHECK_GT(read_bytes, 0); |
| 1515 | CHECK_EQ(static_cast<int>(max_read), read_bytes); |
| 1516 | } |
| 1517 | |
| 1518 | int rv = transport_->socket()->Write( |
| 1519 | send_buffer_.get(), |
| 1520 | send_buffer_->BytesRemaining(), |
| 1521 | base::Bind(&SSLClientSocketOpenSSL::BufferSendComplete, |
| 1522 | base::Unretained(this))); |
| 1523 | if (rv == ERR_IO_PENDING) { |
| 1524 | transport_send_busy_ = true; |
| 1525 | } else { |
| 1526 | TransportWriteComplete(rv); |
| 1527 | } |
| 1528 | return rv; |
| 1529 | } |
| 1530 | |
| 1531 | int SSLClientSocketOpenSSL::BufferRecv(void) { |
| 1532 | if (transport_recv_busy_) |
| 1533 | return ERR_IO_PENDING; |
| 1534 | |
| 1535 | // Determine how much was requested from |transport_bio_| that was not |
| 1536 | // actually available. |
| 1537 | size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
| 1538 | if (requested == 0) { |
| 1539 | // This is not a perfect match of error codes, as no operation is |
| 1540 | // actually pending. However, returning 0 would be interpreted as |
| 1541 | // a possible sign of EOF, which is also an inappropriate match. |
| 1542 | return ERR_IO_PENDING; |
| 1543 | } |
| 1544 | |
| 1545 | // Known Issue: While only reading |requested| data is the more correct |
| 1546 | // implementation, it has the downside of resulting in frequent reads: |
| 1547 | // One read for the SSL record header (~5 bytes) and one read for the SSL |
| 1548 | // record body. Rather than issuing these reads to the underlying socket |
| 1549 | // (and constantly allocating new IOBuffers), a single Read() request to |
| 1550 | // fill |transport_bio_| is issued. As long as an SSL client socket cannot |
| 1551 | // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL |
| 1552 | // traffic, this over-subscribed Read()ing will not cause issues. |
| 1553 | size_t max_write = BIO_ctrl_get_write_guarantee(transport_bio_); |
| 1554 | if (!max_write) |
| 1555 | return ERR_IO_PENDING; |
| 1556 | |
| 1557 | recv_buffer_ = new IOBuffer(max_write); |
| 1558 | int rv = transport_->socket()->Read( |
| 1559 | recv_buffer_.get(), |
| 1560 | max_write, |
| 1561 | base::Bind(&SSLClientSocketOpenSSL::BufferRecvComplete, |
| 1562 | base::Unretained(this))); |
| 1563 | if (rv == ERR_IO_PENDING) { |
| 1564 | transport_recv_busy_ = true; |
| 1565 | } else { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1566 | rv = TransportReadComplete(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1567 | } |
| 1568 | return rv; |
| 1569 | } |
| 1570 | |
| 1571 | void SSLClientSocketOpenSSL::BufferSendComplete(int result) { |
| 1572 | transport_send_busy_ = false; |
| 1573 | TransportWriteComplete(result); |
| 1574 | OnSendComplete(result); |
| 1575 | } |
| 1576 | |
| 1577 | void SSLClientSocketOpenSSL::BufferRecvComplete(int result) { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1578 | result = TransportReadComplete(result); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1579 | OnRecvComplete(result); |
| 1580 | } |
| 1581 | |
| 1582 | void SSLClientSocketOpenSSL::TransportWriteComplete(int result) { |
| 1583 | DCHECK(ERR_IO_PENDING != result); |
| 1584 | if (result < 0) { |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1585 | // Record the error. Save it to be reported in a future read or write on |
| 1586 | // transport_bio_'s peer. |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1587 | transport_write_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1588 | send_buffer_ = NULL; |
| 1589 | } else { |
| 1590 | DCHECK(send_buffer_.get()); |
| 1591 | send_buffer_->DidConsume(result); |
| 1592 | DCHECK_GE(send_buffer_->BytesRemaining(), 0); |
| 1593 | if (send_buffer_->BytesRemaining() <= 0) |
| 1594 | send_buffer_ = NULL; |
| 1595 | } |
| 1596 | } |
| 1597 | |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1598 | int SSLClientSocketOpenSSL::TransportReadComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1599 | DCHECK(ERR_IO_PENDING != result); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1600 | // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError |
| 1601 | // does not report success. |
| 1602 | if (result == 0) |
| 1603 | result = ERR_CONNECTION_CLOSED; |
| 1604 | if (result < 0) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1605 | DVLOG(1) << "TransportReadComplete result " << result; |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1606 | // Received an error. Save it to be reported in a future read on |
| 1607 | // transport_bio_'s peer. |
| 1608 | transport_read_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1609 | } else { |
| 1610 | DCHECK(recv_buffer_.get()); |
| 1611 | int ret = BIO_write(transport_bio_, recv_buffer_->data(), result); |
| 1612 | // A write into a memory BIO should always succeed. |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 1613 | DCHECK_EQ(result, ret); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1614 | } |
| 1615 | recv_buffer_ = NULL; |
| 1616 | transport_recv_busy_ = false; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1617 | return result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1618 | } |
| 1619 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1620 | int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl) { |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 1621 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
| 1622 | tracked_objects::ScopedTracker tracking_profile( |
| 1623 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1624 | "424386 SSLClientSocketOpenSSL::ClientCertRequestCallback")); |
| 1625 | |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1626 | DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1627 | DCHECK(ssl == ssl_); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1628 | |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1629 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); |
| 1630 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1631 | // Clear any currently configured certificates. |
| 1632 | SSL_certs_clear(ssl_); |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1633 | |
| 1634 | #if defined(OS_IOS) |
| 1635 | // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1636 | LOG(WARNING) << "Client auth is not supported"; |
| 1637 | #else // !defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1638 | if (!ssl_config_.send_client_cert) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1639 | // First pass: we know that a client certificate is needed, but we do not |
| 1640 | // have one at hand. |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1641 | client_auth_cert_needed_ = true; |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1642 | STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 1643 | for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1644 | X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
| 1645 | unsigned char* str = NULL; |
| 1646 | int length = i2d_X509_NAME(ca_name, &str); |
| 1647 | cert_authorities_.push_back(std::string( |
| 1648 | reinterpret_cast<const char*>(str), |
| 1649 | static_cast<size_t>(length))); |
| 1650 | OPENSSL_free(str); |
| 1651 | } |
| 1652 | |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1653 | const unsigned char* client_cert_types; |
[email protected] | e7e883e | 2014-07-25 06:03:08 | [diff] [blame] | 1654 | size_t num_client_cert_types = |
| 1655 | SSL_get0_certificate_types(ssl, &client_cert_types); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1656 | for (size_t i = 0; i < num_client_cert_types; i++) { |
| 1657 | cert_key_types_.push_back( |
| 1658 | static_cast<SSLClientCertType>(client_cert_types[i])); |
| 1659 | } |
| 1660 | |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1661 | return -1; // Suspends handshake. |
| 1662 | } |
| 1663 | |
| 1664 | // Second pass: a client certificate should have been selected. |
[email protected] | 13914c9 | 2013-06-13 22:42:42 | [diff] [blame] | 1665 | if (ssl_config_.client_cert.get()) { |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1666 | ScopedX509 leaf_x509 = |
| 1667 | OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
| 1668 | if (!leaf_x509) { |
| 1669 | LOG(WARNING) << "Failed to import certificate"; |
| 1670 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1671 | return -1; |
| 1672 | } |
| 1673 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1674 | ScopedX509Stack chain = OSCertHandlesToOpenSSL( |
| 1675 | ssl_config_.client_cert->GetIntermediateCertificates()); |
| 1676 | if (!chain) { |
| 1677 | LOG(WARNING) << "Failed to import intermediate certificates"; |
| 1678 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1679 | return -1; |
| 1680 | } |
| 1681 | |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1682 | // TODO(davidben): With Linux client auth support, this should be |
| 1683 | // conditioned on OS_ANDROID and then, with https://crbug.com/394131, |
| 1684 | // removed altogether. OpenSSLClientKeyStore is mostly an artifact of the |
| 1685 | // net/ client auth API lacking a private key handle. |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1686 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1687 | crypto::ScopedEVP_PKEY privkey = |
| 1688 | OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
| 1689 | ssl_config_.client_cert.get()); |
| 1690 | #else // !defined(USE_OPENSSL_CERTS) |
| 1691 | crypto::ScopedEVP_PKEY privkey = |
| 1692 | FetchClientCertPrivateKey(ssl_config_.client_cert.get()); |
| 1693 | #endif // defined(USE_OPENSSL_CERTS) |
| 1694 | if (!privkey) { |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1695 | // Could not find the private key. Fail the handshake and surface an |
| 1696 | // appropriate error to the caller. |
| 1697 | LOG(WARNING) << "Client cert found without private key"; |
| 1698 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1699 | return -1; |
[email protected] | 0c6523f | 2010-12-10 10:56:24 | [diff] [blame] | 1700 | } |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1701 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1702 | if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
| 1703 | !SSL_use_PrivateKey(ssl_, privkey.get()) || |
| 1704 | !SSL_set1_chain(ssl_, chain.get())) { |
| 1705 | LOG(WARNING) << "Failed to set client certificate"; |
| 1706 | return -1; |
| 1707 | } |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1708 | |
| 1709 | int cert_count = 1 + sk_X509_num(chain.get()); |
| 1710 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| 1711 | NetLog::IntegerCallback("cert_count", cert_count)); |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1712 | return 1; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1713 | } |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1714 | #endif // defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1715 | |
| 1716 | // Send no client certificate. |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1717 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| 1718 | NetLog::IntegerCallback("cert_count", 0)); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1719 | return 1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1720 | } |
| 1721 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1722 | int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 1723 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
| 1724 | tracked_objects::ScopedTracker tracking_profile( |
| 1725 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1726 | "424386 SSLClientSocketOpenSSL::CertVerifyCallback")); |
| 1727 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1728 | if (!completed_connect_) { |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1729 | // If the first handshake hasn't completed then we accept any certificates |
| 1730 | // because we verify after the handshake. |
| 1731 | return 1; |
| 1732 | } |
| 1733 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1734 | // Disallow the server certificate to change in a renegotiation. |
| 1735 | if (server_cert_chain_->empty()) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1736 | LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | base::StringPiece old_der, new_der; |
| 1740 | if (store_ctx->cert == NULL || |
| 1741 | !x509_util::GetDER(server_cert_chain_->Get(0), &old_der) || |
| 1742 | !x509_util::GetDER(store_ctx->cert, &new_der)) { |
| 1743 | LOG(ERROR) << "Failed to encode certificates"; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | if (old_der != new_der) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1747 | LOG(ERROR) << "Server certificate changed between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | return 1; |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1752 | } |
| 1753 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1754 | // SelectNextProtoCallback is called by OpenSSL during the handshake. If the |
| 1755 | // server supports NPN, selects a protocol from the list that the server |
| 1756 | // provides. According to third_party/openssl/openssl/ssl/ssl_lib.c, the |
| 1757 | // callback can assume that |in| is syntactically valid. |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1758 | int SSLClientSocketOpenSSL::SelectNextProtoCallback(unsigned char** out, |
| 1759 | unsigned char* outlen, |
| 1760 | const unsigned char* in, |
| 1761 | unsigned int inlen) { |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 1762 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424386 is fixed. |
| 1763 | tracked_objects::ScopedTracker tracking_profile( |
| 1764 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1765 | "424386 SSLClientSocketOpenSSL::SelectNextProtoCallback")); |
| 1766 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1767 | if (ssl_config_.next_protos.empty()) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1768 | *out = reinterpret_cast<uint8*>( |
| 1769 | const_cast<char*>(kDefaultSupportedNPNProtocol)); |
| 1770 | *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; |
| 1771 | npn_status_ = kNextProtoUnsupported; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1772 | return SSL_TLSEXT_ERR_OK; |
| 1773 | } |
| 1774 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1775 | // Assume there's no overlap between our protocols and the server's list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1776 | npn_status_ = kNextProtoNoOverlap; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1777 | |
| 1778 | // For each protocol in server preference order, see if we support it. |
| 1779 | for (unsigned int i = 0; i < inlen; i += in[i] + 1) { |
| 1780 | for (std::vector<std::string>::const_iterator |
| 1781 | j = ssl_config_.next_protos.begin(); |
| 1782 | j != ssl_config_.next_protos.end(); ++j) { |
| 1783 | if (in[i] == j->size() && |
| 1784 | memcmp(&in[i + 1], j->data(), in[i]) == 0) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1785 | // We found a match. |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1786 | *out = const_cast<unsigned char*>(in) + i + 1; |
| 1787 | *outlen = in[i]; |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1788 | npn_status_ = kNextProtoNegotiated; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1789 | break; |
| 1790 | } |
| 1791 | } |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1792 | if (npn_status_ == kNextProtoNegotiated) |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1793 | break; |
| 1794 | } |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1795 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1796 | // If we didn't find a protocol, we select the first one from our list. |
| 1797 | if (npn_status_ == kNextProtoNoOverlap) { |
| 1798 | *out = reinterpret_cast<uint8*>(const_cast<char*>( |
| 1799 | ssl_config_.next_protos[0].data())); |
| 1800 | *outlen = ssl_config_.next_protos[0].size(); |
| 1801 | } |
| 1802 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1803 | npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
[email protected] | 32e1dee | 2010-12-09 18:36:24 | [diff] [blame] | 1804 | DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
bnc | 0d28ea5 | 2014-10-13 15:15:38 | [diff] [blame] | 1805 | set_negotiation_extension(kExtensionNPN); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1806 | return SSL_TLSEXT_ERR_OK; |
| 1807 | } |
| 1808 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1809 | long SSLClientSocketOpenSSL::MaybeReplayTransportError( |
| 1810 | BIO *bio, |
| 1811 | int cmd, |
| 1812 | const char *argp, int argi, long argl, |
| 1813 | long retvalue) { |
| 1814 | if (cmd == (BIO_CB_READ|BIO_CB_RETURN) && retvalue <= 0) { |
| 1815 | // If there is no more data in the buffer, report any pending errors that |
| 1816 | // were observed. Note that both the readbuf and the writebuf are checked |
| 1817 | // for errors, since the application may have encountered a socket error |
| 1818 | // while writing that would otherwise not be reported until the application |
| 1819 | // attempted to write again - which it may never do. See |
| 1820 | // https://crbug.com/249848. |
| 1821 | if (transport_read_error_ != OK) { |
| 1822 | OpenSSLPutNetError(FROM_HERE, transport_read_error_); |
| 1823 | return -1; |
| 1824 | } |
| 1825 | if (transport_write_error_ != OK) { |
| 1826 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 1827 | return -1; |
| 1828 | } |
| 1829 | } else if (cmd == BIO_CB_WRITE) { |
| 1830 | // Because of the write buffer, this reports a failure from the previous |
| 1831 | // write payload. If the current payload fails to write, the error will be |
| 1832 | // reported in a future write or read to |bio|. |
| 1833 | if (transport_write_error_ != OK) { |
| 1834 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 1835 | return -1; |
| 1836 | } |
| 1837 | } |
| 1838 | return retvalue; |
| 1839 | } |
| 1840 | |
| 1841 | // static |
| 1842 | long SSLClientSocketOpenSSL::BIOCallback( |
| 1843 | BIO *bio, |
| 1844 | int cmd, |
| 1845 | const char *argp, int argi, long argl, |
| 1846 | long retvalue) { |
| 1847 | SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( |
| 1848 | BIO_get_callback_arg(bio)); |
| 1849 | CHECK(socket); |
| 1850 | return socket->MaybeReplayTransportError( |
| 1851 | bio, cmd, argp, argi, argl, retvalue); |
| 1852 | } |
| 1853 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1854 | // static |
| 1855 | void SSLClientSocketOpenSSL::InfoCallback(const SSL* ssl, |
| 1856 | int type, |
| 1857 | int /*val*/) { |
| 1858 | if (type == SSL_CB_HANDSHAKE_DONE) { |
| 1859 | SSLClientSocketOpenSSL* ssl_socket = |
| 1860 | SSLContext::GetInstance()->GetClientSocketFromSSL(ssl); |
| 1861 | ssl_socket->handshake_succeeded_ = true; |
| 1862 | ssl_socket->CheckIfHandshakeFinished(); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | // Determines if both the handshake and certificate verification have completed |
| 1867 | // successfully, and calls the handshake completion callback if that is the |
| 1868 | // case. |
| 1869 | // |
| 1870 | // CheckIfHandshakeFinished is called twice per connection: once after |
| 1871 | // MarkSSLSessionAsGood, when the certificate has been verified, and |
| 1872 | // once via an OpenSSL callback when the handshake has completed. On the |
| 1873 | // second call, when the certificate has been verified and the handshake |
| 1874 | // has completed, the connection's handshake completion callback is run. |
| 1875 | void SSLClientSocketOpenSSL::CheckIfHandshakeFinished() { |
| 1876 | if (handshake_succeeded_ && marked_session_as_good_) |
| 1877 | OnHandshakeCompletion(); |
| 1878 | } |
| 1879 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1880 | void SSLClientSocketOpenSSL::AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
| 1881 | for (ct::SCTList::const_iterator iter = |
| 1882 | ct_verify_result_.verified_scts.begin(); |
| 1883 | iter != ct_verify_result_.verified_scts.end(); ++iter) { |
| 1884 | ssl_info->signed_certificate_timestamps.push_back( |
| 1885 | SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_OK)); |
| 1886 | } |
| 1887 | for (ct::SCTList::const_iterator iter = |
| 1888 | ct_verify_result_.invalid_scts.begin(); |
| 1889 | iter != ct_verify_result_.invalid_scts.end(); ++iter) { |
| 1890 | ssl_info->signed_certificate_timestamps.push_back( |
| 1891 | SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_INVALID)); |
| 1892 | } |
| 1893 | for (ct::SCTList::const_iterator iter = |
| 1894 | ct_verify_result_.unknown_logs_scts.begin(); |
| 1895 | iter != ct_verify_result_.unknown_logs_scts.end(); ++iter) { |
| 1896 | ssl_info->signed_certificate_timestamps.push_back( |
| 1897 | SignedCertificateTimestampAndStatus(*iter, |
| 1898 | ct::SCT_STATUS_LOG_UNKNOWN)); |
| 1899 | } |
| 1900 | } |
| 1901 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1902 | scoped_refptr<X509Certificate> |
| 1903 | SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1904 | return server_cert_; |
| 1905 | } |
| 1906 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 1907 | } // namespace net |