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