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