OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // OpenSSL binding for SSLClientSocket. The class layout and general principle | 5 #include "net/socket/ssl_client_socket_impl.h" |
6 // of operation is derived from SSLClientSocketNSS. | |
7 | |
8 #include "net/socket/ssl_client_socket_openssl.h" | |
9 | 6 |
10 #include <errno.h> | 7 #include <errno.h> |
11 #include <openssl/bio.h> | 8 #include <openssl/bio.h> |
12 #include <openssl/bytestring.h> | 9 #include <openssl/bytestring.h> |
13 #include <openssl/err.h> | 10 #include <openssl/err.h> |
14 #include <openssl/evp.h> | 11 #include <openssl/evp.h> |
15 #include <openssl/mem.h> | 12 #include <openssl/mem.h> |
16 #include <openssl/ssl.h> | 13 #include <openssl/ssl.h> |
17 #include <string.h> | 14 #include <string.h> |
18 | 15 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 #if defined(USE_NSS_CERTS) | 59 #if defined(USE_NSS_CERTS) |
63 #include "net/cert_net/nss_ocsp.h" | 60 #include "net/cert_net/nss_ocsp.h" |
64 #endif | 61 #endif |
65 | 62 |
66 namespace net { | 63 namespace net { |
67 | 64 |
68 namespace { | 65 namespace { |
69 | 66 |
70 // Enable this to see logging for state machine state transitions. | 67 // Enable this to see logging for state machine state transitions. |
71 #if 0 | 68 #if 0 |
72 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ | 69 #define GotoState(s) \ |
73 " jump to state " << s; \ | 70 do { \ |
74 next_handshake_state_ = s; } while (0) | 71 DVLOG(2) << (void*)this << " " << __FUNCTION__ << " jump to state " << s; \ |
| 72 next_handshake_state_ = s; \ |
| 73 } while (0) |
75 #else | 74 #else |
76 #define GotoState(s) next_handshake_state_ = s | 75 #define GotoState(s) next_handshake_state_ = s |
77 #endif | 76 #endif |
78 | 77 |
79 // This constant can be any non-negative/non-zero value (eg: it does not | 78 // This constant can be any non-negative/non-zero value (eg: it does not |
80 // overlap with any value of the net::Error range, including net::OK). | 79 // overlap with any value of the net::Error range, including net::OK). |
81 const int kNoPendingResult = 1; | 80 const int kNoPendingResult = 1; |
82 | 81 |
83 // If a client doesn't have a list of protocols that it supports, but | 82 // If a client doesn't have a list of protocols that it supports, but |
84 // the server supports NPN, choosing "http/1.1" is the best answer. | 83 // the server supports NPN, choosing "http/1.1" is the best answer. |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (capture_mode.include_cookies_and_credentials()) { | 180 if (capture_mode.include_cookies_and_credentials()) { |
182 key_to_log = base::HexEncode(raw_key.data(), raw_key.length()); | 181 key_to_log = base::HexEncode(raw_key.data(), raw_key.length()); |
183 } | 182 } |
184 dict->SetString("key", key_to_log); | 183 dict->SetString("key", key_to_log); |
185 } | 184 } |
186 return std::move(dict); | 185 return std::move(dict); |
187 } | 186 } |
188 | 187 |
189 } // namespace | 188 } // namespace |
190 | 189 |
191 class SSLClientSocketOpenSSL::SSLContext { | 190 class SSLClientSocketImpl::SSLContext { |
192 public: | 191 public: |
193 static SSLContext* GetInstance() { | 192 static SSLContext* GetInstance() { |
194 return base::Singleton<SSLContext>::get(); | 193 return base::Singleton<SSLContext>::get(); |
195 } | 194 } |
196 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 195 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
197 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 196 SSLClientSessionCacheImpl* session_cache() { return &session_cache_; } |
198 | 197 |
199 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 198 SSLClientSocketImpl* GetClientSocketFromSSL(const SSL* ssl) { |
200 DCHECK(ssl); | 199 DCHECK(ssl); |
201 SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( | 200 SSLClientSocketImpl* socket = static_cast<SSLClientSocketImpl*>( |
202 SSL_get_ex_data(ssl, ssl_socket_data_index_)); | 201 SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
203 DCHECK(socket); | 202 DCHECK(socket); |
204 return socket; | 203 return socket; |
205 } | 204 } |
206 | 205 |
207 bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { | 206 bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketImpl* socket) { |
208 return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; | 207 return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
209 } | 208 } |
210 | 209 |
211 #if !defined(OS_NACL) | 210 #if !defined(OS_NACL) |
212 void SetSSLKeyLogFile( | 211 void SetSSLKeyLogFile( |
213 const base::FilePath& path, | 212 const base::FilePath& path, |
214 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 213 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
215 DCHECK(!ssl_key_logger_); | 214 DCHECK(!ssl_key_logger_); |
216 ssl_key_logger_.reset(new SSLKeyLogger(path, task_runner)); | 215 ssl_key_logger_.reset(new SSLKeyLogger(path, task_runner)); |
217 SSL_CTX_set_keylog_callback(ssl_ctx_.get(), KeyLogCallback); | 216 SSL_CTX_set_keylog_callback(ssl_ctx_.get(), KeyLogCallback); |
218 } | 217 } |
219 #endif | 218 #endif |
220 | 219 |
221 static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; | 220 static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; |
222 | 221 |
223 private: | 222 private: |
224 friend struct base::DefaultSingletonTraits<SSLContext>; | 223 friend struct base::DefaultSingletonTraits<SSLContext>; |
225 | 224 |
226 SSLContext() : session_cache_(SSLClientSessionCacheOpenSSL::Config()) { | 225 SSLContext() : session_cache_(SSLClientSessionCacheImpl::Config()) { |
227 crypto::EnsureOpenSSLInit(); | 226 crypto::EnsureOpenSSLInit(); |
228 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); | 227 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
229 DCHECK_NE(ssl_socket_data_index_, -1); | 228 DCHECK_NE(ssl_socket_data_index_, -1); |
230 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); | 229 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
231 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); | 230 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
232 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); | 231 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
233 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); | 232 SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
234 // This stops |SSL_shutdown| from generating the close_notify message, which | 233 // This stops |SSL_shutdown| from generating the close_notify message, which |
235 // is currently not sent on the network. | 234 // is currently not sent on the network. |
236 // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. | 235 // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. |
237 SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); | 236 SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); |
238 // Note that SSL_OP_DISABLE_NPN is used to disable NPN if | 237 // Note that SSL_OP_DISABLE_NPN is used to disable NPN if |
239 // ssl_config_.next_proto is empty. | 238 // ssl_config_.next_proto is empty. |
240 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, | 239 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
241 NULL); | 240 NULL); |
242 | 241 |
243 // Disable the internal session cache. Session caching is handled | 242 // Disable the internal session cache. Session caching is handled |
244 // externally (i.e. by SSLClientSessionCacheOpenSSL). | 243 // externally (i.e. by SSLClientSessionCacheImpl). |
245 SSL_CTX_set_session_cache_mode( | 244 SSL_CTX_set_session_cache_mode( |
246 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); | 245 ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
247 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); | 246 SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
248 | 247 |
249 if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, | 248 if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, |
250 &TokenBindingAddCallback, | 249 &TokenBindingAddCallback, |
251 &TokenBindingFreeCallback, nullptr, | 250 &TokenBindingFreeCallback, nullptr, |
252 &TokenBindingParseCallback, nullptr)) { | 251 &TokenBindingParseCallback, nullptr)) { |
253 NOTREACHED(); | 252 NOTREACHED(); |
254 } | 253 } |
255 } | 254 } |
256 | 255 |
257 static int TokenBindingAddCallback(SSL* ssl, | 256 static int TokenBindingAddCallback(SSL* ssl, |
258 unsigned int extension_value, | 257 unsigned int extension_value, |
259 const uint8_t** out, | 258 const uint8_t** out, |
260 size_t* out_len, | 259 size_t* out_len, |
261 int* out_alert_value, | 260 int* out_alert_value, |
262 void* add_arg) { | 261 void* add_arg) { |
263 DCHECK_EQ(extension_value, kTbExtNum); | 262 DCHECK_EQ(extension_value, kTbExtNum); |
264 SSLClientSocketOpenSSL* socket = | 263 SSLClientSocketImpl* socket = |
265 SSLClientSocketOpenSSL::SSLContext::GetInstance() | 264 SSLClientSocketImpl::SSLContext::GetInstance()->GetClientSocketFromSSL( |
266 ->GetClientSocketFromSSL(ssl); | 265 ssl); |
267 return socket->TokenBindingAdd(out, out_len, out_alert_value); | 266 return socket->TokenBindingAdd(out, out_len, out_alert_value); |
268 } | 267 } |
269 | 268 |
270 static void TokenBindingFreeCallback(SSL* ssl, | 269 static void TokenBindingFreeCallback(SSL* ssl, |
271 unsigned extension_value, | 270 unsigned extension_value, |
272 const uint8_t* out, | 271 const uint8_t* out, |
273 void* add_arg) { | 272 void* add_arg) { |
274 DCHECK_EQ(extension_value, kTbExtNum); | 273 DCHECK_EQ(extension_value, kTbExtNum); |
275 OPENSSL_free(const_cast<unsigned char*>(out)); | 274 OPENSSL_free(const_cast<unsigned char*>(out)); |
276 } | 275 } |
277 | 276 |
278 static int TokenBindingParseCallback(SSL* ssl, | 277 static int TokenBindingParseCallback(SSL* ssl, |
279 unsigned int extension_value, | 278 unsigned int extension_value, |
280 const uint8_t* contents, | 279 const uint8_t* contents, |
281 size_t contents_len, | 280 size_t contents_len, |
282 int* out_alert_value, | 281 int* out_alert_value, |
283 void* parse_arg) { | 282 void* parse_arg) { |
284 DCHECK_EQ(extension_value, kTbExtNum); | 283 DCHECK_EQ(extension_value, kTbExtNum); |
285 SSLClientSocketOpenSSL* socket = | 284 SSLClientSocketImpl* socket = |
286 SSLClientSocketOpenSSL::SSLContext::GetInstance() | 285 SSLClientSocketImpl::SSLContext::GetInstance()->GetClientSocketFromSSL( |
287 ->GetClientSocketFromSSL(ssl); | 286 ssl); |
288 return socket->TokenBindingParse(contents, contents_len, out_alert_value); | 287 return socket->TokenBindingParse(contents, contents_len, out_alert_value); |
289 } | 288 } |
290 | 289 |
291 static int ClientCertRequestCallback(SSL* ssl, void* arg) { | 290 static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
292 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 291 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
293 DCHECK(socket); | 292 DCHECK(socket); |
294 return socket->ClientCertRequestCallback(ssl); | 293 return socket->ClientCertRequestCallback(ssl); |
295 } | 294 } |
296 | 295 |
297 static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { | 296 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg) { |
298 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( | 297 SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
299 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); | 298 store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
300 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 299 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
301 CHECK(socket); | 300 CHECK(socket); |
302 | 301 |
303 return socket->CertVerifyCallback(store_ctx); | 302 return socket->CertVerifyCallback(store_ctx); |
304 } | 303 } |
305 | 304 |
306 static int SelectNextProtoCallback(SSL* ssl, | 305 static int SelectNextProtoCallback(SSL* ssl, |
307 unsigned char** out, unsigned char* outlen, | 306 unsigned char** out, |
| 307 unsigned char* outlen, |
308 const unsigned char* in, | 308 const unsigned char* in, |
309 unsigned int inlen, void* arg) { | 309 unsigned int inlen, |
310 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 310 void* arg) { |
| 311 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
311 return socket->SelectNextProtoCallback(out, outlen, in, inlen); | 312 return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
312 } | 313 } |
313 | 314 |
314 static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { | 315 static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
315 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 316 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
316 return socket->NewSessionCallback(session); | 317 return socket->NewSessionCallback(session); |
317 } | 318 } |
318 | 319 |
319 static int PrivateKeyTypeCallback(SSL* ssl) { | 320 static int PrivateKeyTypeCallback(SSL* ssl) { |
320 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 321 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
321 return socket->PrivateKeyTypeCallback(); | 322 return socket->PrivateKeyTypeCallback(); |
322 } | 323 } |
323 | 324 |
324 static size_t PrivateKeyMaxSignatureLenCallback(SSL* ssl) { | 325 static size_t PrivateKeyMaxSignatureLenCallback(SSL* ssl) { |
325 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 326 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
326 return socket->PrivateKeyMaxSignatureLenCallback(); | 327 return socket->PrivateKeyMaxSignatureLenCallback(); |
327 } | 328 } |
328 | 329 |
329 static ssl_private_key_result_t PrivateKeySignCallback(SSL* ssl, | 330 static ssl_private_key_result_t PrivateKeySignCallback(SSL* ssl, |
330 uint8_t* out, | 331 uint8_t* out, |
331 size_t* out_len, | 332 size_t* out_len, |
332 size_t max_out, | 333 size_t max_out, |
333 const EVP_MD* md, | 334 const EVP_MD* md, |
334 const uint8_t* in, | 335 const uint8_t* in, |
335 size_t in_len) { | 336 size_t in_len) { |
336 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 337 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
337 return socket->PrivateKeySignCallback(out, out_len, max_out, md, in, | 338 return socket->PrivateKeySignCallback(out, out_len, max_out, md, in, |
338 in_len); | 339 in_len); |
339 } | 340 } |
340 | 341 |
341 static ssl_private_key_result_t PrivateKeySignCompleteCallback( | 342 static ssl_private_key_result_t PrivateKeySignCompleteCallback( |
342 SSL* ssl, | 343 SSL* ssl, |
343 uint8_t* out, | 344 uint8_t* out, |
344 size_t* out_len, | 345 size_t* out_len, |
345 size_t max_out) { | 346 size_t max_out) { |
346 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 347 SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
347 return socket->PrivateKeySignCompleteCallback(out, out_len, max_out); | 348 return socket->PrivateKeySignCompleteCallback(out, out_len, max_out); |
348 } | 349 } |
349 | 350 |
350 #if !defined(OS_NACL) | 351 #if !defined(OS_NACL) |
351 static void KeyLogCallback(const SSL* ssl, const char* line) { | 352 static void KeyLogCallback(const SSL* ssl, const char* line) { |
352 GetInstance()->ssl_key_logger_->WriteLine(line); | 353 GetInstance()->ssl_key_logger_->WriteLine(line); |
353 } | 354 } |
354 #endif | 355 #endif |
355 | 356 |
356 // This is the index used with SSL_get_ex_data to retrieve the owner | 357 // This is the index used with SSL_get_ex_data to retrieve the owner |
357 // SSLClientSocketOpenSSL object from an SSL instance. | 358 // SSLClientSocketImpl object from an SSL instance. |
358 int ssl_socket_data_index_; | 359 int ssl_socket_data_index_; |
359 | 360 |
360 ScopedSSL_CTX ssl_ctx_; | 361 ScopedSSL_CTX ssl_ctx_; |
361 | 362 |
362 #if !defined(OS_NACL) | 363 #if !defined(OS_NACL) |
363 std::unique_ptr<SSLKeyLogger> ssl_key_logger_; | 364 std::unique_ptr<SSLKeyLogger> ssl_key_logger_; |
364 #endif | 365 #endif |
365 | 366 |
366 // TODO(davidben): Use a separate cache per URLRequestContext. | 367 // TODO(davidben): Use a separate cache per URLRequestContext. |
367 // https://crbug.com/458365 | 368 // https://crbug.com/458365 |
368 // | 369 // |
369 // TODO(davidben): Sessions should be invalidated on fatal | 370 // TODO(davidben): Sessions should be invalidated on fatal |
370 // alerts. https://crbug.com/466352 | 371 // alerts. https://crbug.com/466352 |
371 SSLClientSessionCacheOpenSSL session_cache_; | 372 SSLClientSessionCacheImpl session_cache_; |
372 }; | 373 }; |
373 | 374 |
374 const SSL_PRIVATE_KEY_METHOD | 375 const SSL_PRIVATE_KEY_METHOD |
375 SSLClientSocketOpenSSL::SSLContext::kPrivateKeyMethod = { | 376 SSLClientSocketImpl::SSLContext::kPrivateKeyMethod = { |
376 &SSLClientSocketOpenSSL::SSLContext::PrivateKeyTypeCallback, | 377 &SSLClientSocketImpl::SSLContext::PrivateKeyTypeCallback, |
377 &SSLClientSocketOpenSSL::SSLContext::PrivateKeyMaxSignatureLenCallback, | 378 &SSLClientSocketImpl::SSLContext::PrivateKeyMaxSignatureLenCallback, |
378 &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCallback, | 379 &SSLClientSocketImpl::SSLContext::PrivateKeySignCallback, |
379 &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCompleteCallback, | 380 &SSLClientSocketImpl::SSLContext::PrivateKeySignCompleteCallback, |
380 }; | 381 }; |
381 | 382 |
382 // PeerCertificateChain is a helper object which extracts the certificate | 383 // PeerCertificateChain is a helper object which extracts the certificate |
383 // chain, as given by the server, from an OpenSSL socket and performs the needed | 384 // chain, as given by the server, from an OpenSSL socket and performs the needed |
384 // resource management. The first element of the chain is the leaf certificate | 385 // resource management. The first element of the chain is the leaf certificate |
385 // and the other elements are in the order given by the server. | 386 // and the other elements are in the order given by the server. |
386 class SSLClientSocketOpenSSL::PeerCertificateChain { | 387 class SSLClientSocketImpl::PeerCertificateChain { |
387 public: | 388 public: |
388 explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } | 389 explicit PeerCertificateChain(STACK_OF(X509) * chain) { Reset(chain); } |
389 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } | 390 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
390 ~PeerCertificateChain() {} | 391 ~PeerCertificateChain() {} |
391 PeerCertificateChain& operator=(const PeerCertificateChain& other); | 392 PeerCertificateChain& operator=(const PeerCertificateChain& other); |
392 | 393 |
393 // Resets the PeerCertificateChain to the set of certificates in|chain|, | 394 // Resets the PeerCertificateChain to the set of certificates in|chain|, |
394 // which may be NULL, indicating to empty the store certificates. | 395 // which may be NULL, indicating to empty the store certificates. |
395 // Note: If an error occurs, such as being unable to parse the certificates, | 396 // Note: If an error occurs, such as being unable to parse the certificates, |
396 // this will behave as if Reset(NULL) was called. | 397 // this will behave as if Reset(NULL) was called. |
397 void Reset(STACK_OF(X509)* chain); | 398 void Reset(STACK_OF(X509) * chain); |
398 | 399 |
399 // Note that when USE_OPENSSL_CERTS is defined, OSCertHandle is X509* | 400 // Note that when USE_OPENSSL_CERTS is defined, OSCertHandle is X509* |
400 scoped_refptr<X509Certificate> AsOSChain() const; | 401 scoped_refptr<X509Certificate> AsOSChain() const; |
401 | 402 |
402 size_t size() const { | 403 size_t size() const { |
403 if (!openssl_chain_.get()) | 404 if (!openssl_chain_.get()) |
404 return 0; | 405 return 0; |
405 return sk_X509_num(openssl_chain_.get()); | 406 return sk_X509_num(openssl_chain_.get()); |
406 } | 407 } |
407 | 408 |
408 bool empty() const { | 409 bool empty() const { return size() == 0; } |
409 return size() == 0; | |
410 } | |
411 | 410 |
412 X509* Get(size_t index) const { | 411 X509* Get(size_t index) const { |
413 DCHECK_LT(index, size()); | 412 DCHECK_LT(index, size()); |
414 return sk_X509_value(openssl_chain_.get(), index); | 413 return sk_X509_value(openssl_chain_.get(), index); |
415 } | 414 } |
416 | 415 |
417 private: | 416 private: |
418 ScopedX509Stack openssl_chain_; | 417 ScopedX509Stack openssl_chain_; |
419 }; | 418 }; |
420 | 419 |
421 SSLClientSocketOpenSSL::PeerCertificateChain& | 420 SSLClientSocketImpl::PeerCertificateChain& |
422 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( | 421 SSLClientSocketImpl::PeerCertificateChain::operator=( |
423 const PeerCertificateChain& other) { | 422 const PeerCertificateChain& other) { |
424 if (this == &other) | 423 if (this == &other) |
425 return *this; | 424 return *this; |
426 | 425 |
427 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); | 426 openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
428 return *this; | 427 return *this; |
429 } | 428 } |
430 | 429 |
431 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( | 430 void SSLClientSocketImpl::PeerCertificateChain::Reset(STACK_OF(X509) * chain) { |
432 STACK_OF(X509)* chain) { | |
433 openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); | 431 openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); |
434 } | 432 } |
435 | 433 |
436 scoped_refptr<X509Certificate> | 434 scoped_refptr<X509Certificate> |
437 SSLClientSocketOpenSSL::PeerCertificateChain::AsOSChain() const { | 435 SSLClientSocketImpl::PeerCertificateChain::AsOSChain() const { |
438 #if defined(USE_OPENSSL_CERTS) | 436 #if defined(USE_OPENSSL_CERTS) |
439 // When OSCertHandle is typedef'ed to X509, this implementation does a short | 437 // When OSCertHandle is typedef'ed to X509, this implementation does a short |
440 // cut to avoid converting back and forth between DER and the X509 struct. | 438 // cut to avoid converting back and forth between DER and the X509 struct. |
441 X509Certificate::OSCertHandles intermediates; | 439 X509Certificate::OSCertHandles intermediates; |
442 for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { | 440 for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { |
443 intermediates.push_back(sk_X509_value(openssl_chain_.get(), i)); | 441 intermediates.push_back(sk_X509_value(openssl_chain_.get(), i)); |
444 } | 442 } |
445 | 443 |
446 return X509Certificate::CreateFromHandle( | 444 return X509Certificate::CreateFromHandle( |
447 sk_X509_value(openssl_chain_.get(), 0), intermediates); | 445 sk_X509_value(openssl_chain_.get(), 0), intermediates); |
448 #else | 446 #else |
449 // DER-encode the chain and convert to a platform certificate handle. | 447 // DER-encode the chain and convert to a platform certificate handle. |
450 std::vector<base::StringPiece> der_chain; | 448 std::vector<base::StringPiece> der_chain; |
451 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | 449 for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
452 X509* x = sk_X509_value(openssl_chain_.get(), i); | 450 X509* x = sk_X509_value(openssl_chain_.get(), i); |
453 base::StringPiece der; | 451 base::StringPiece der; |
454 if (!x509_util::GetDER(x, &der)) | 452 if (!x509_util::GetDER(x, &der)) |
455 return NULL; | 453 return NULL; |
456 der_chain.push_back(der); | 454 der_chain.push_back(der); |
457 } | 455 } |
458 | 456 |
459 return X509Certificate::CreateFromDERCertChain(der_chain); | 457 return X509Certificate::CreateFromDERCertChain(der_chain); |
460 #endif | 458 #endif |
461 } | 459 } |
462 | 460 |
463 // static | 461 // static |
464 void SSLClientSocket::ClearSessionCache() { | 462 void SSLClientSocket::ClearSessionCache() { |
465 SSLClientSocketOpenSSL::SSLContext* context = | 463 SSLClientSocketImpl::SSLContext* context = |
466 SSLClientSocketOpenSSL::SSLContext::GetInstance(); | 464 SSLClientSocketImpl::SSLContext::GetInstance(); |
467 context->session_cache()->Flush(); | 465 context->session_cache()->Flush(); |
468 } | 466 } |
469 | 467 |
470 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( | 468 SSLClientSocketImpl::SSLClientSocketImpl( |
471 std::unique_ptr<ClientSocketHandle> transport_socket, | 469 std::unique_ptr<ClientSocketHandle> transport_socket, |
472 const HostPortPair& host_and_port, | 470 const HostPortPair& host_and_port, |
473 const SSLConfig& ssl_config, | 471 const SSLConfig& ssl_config, |
474 const SSLClientSocketContext& context) | 472 const SSLClientSocketContext& context) |
475 : transport_send_busy_(false), | 473 : transport_send_busy_(false), |
476 transport_recv_busy_(false), | 474 transport_recv_busy_(false), |
477 pending_read_error_(kNoPendingResult), | 475 pending_read_error_(kNoPendingResult), |
478 pending_read_ssl_error_(SSL_ERROR_NONE), | 476 pending_read_ssl_error_(SSL_ERROR_NONE), |
479 transport_read_error_(OK), | 477 transport_read_error_(OK), |
480 transport_write_error_(OK), | 478 transport_write_error_(OK), |
(...skipping 20 matching lines...) Expand all Loading... |
501 certificate_verified_(false), | 499 certificate_verified_(false), |
502 ssl_failure_state_(SSL_FAILURE_NONE), | 500 ssl_failure_state_(SSL_FAILURE_NONE), |
503 signature_result_(kNoPendingResult), | 501 signature_result_(kNoPendingResult), |
504 transport_security_state_(context.transport_security_state), | 502 transport_security_state_(context.transport_security_state), |
505 policy_enforcer_(context.ct_policy_enforcer), | 503 policy_enforcer_(context.ct_policy_enforcer), |
506 net_log_(transport_->socket()->NetLog()), | 504 net_log_(transport_->socket()->NetLog()), |
507 weak_factory_(this) { | 505 weak_factory_(this) { |
508 DCHECK(cert_verifier_); | 506 DCHECK(cert_verifier_); |
509 } | 507 } |
510 | 508 |
511 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 509 SSLClientSocketImpl::~SSLClientSocketImpl() { |
512 Disconnect(); | 510 Disconnect(); |
513 } | 511 } |
514 | 512 |
515 #if !defined(OS_NACL) | 513 #if !defined(OS_NACL) |
516 void SSLClientSocketOpenSSL::SetSSLKeyLogFile( | 514 void SSLClientSocketImpl::SetSSLKeyLogFile( |
517 const base::FilePath& ssl_keylog_file, | 515 const base::FilePath& ssl_keylog_file, |
518 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 516 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
519 SSLContext::GetInstance()->SetSSLKeyLogFile(ssl_keylog_file, task_runner); | 517 SSLContext::GetInstance()->SetSSLKeyLogFile(ssl_keylog_file, task_runner); |
520 } | 518 } |
521 #endif | 519 #endif |
522 | 520 |
523 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 521 void SSLClientSocketImpl::GetSSLCertRequestInfo( |
524 SSLCertRequestInfo* cert_request_info) { | 522 SSLCertRequestInfo* cert_request_info) { |
525 cert_request_info->host_and_port = host_and_port_; | 523 cert_request_info->host_and_port = host_and_port_; |
526 cert_request_info->cert_authorities = cert_authorities_; | 524 cert_request_info->cert_authorities = cert_authorities_; |
527 cert_request_info->cert_key_types = cert_key_types_; | 525 cert_request_info->cert_key_types = cert_key_types_; |
528 } | 526 } |
529 | 527 |
530 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( | 528 SSLClientSocket::NextProtoStatus SSLClientSocketImpl::GetNextProto( |
531 std::string* proto) const { | 529 std::string* proto) const { |
532 *proto = npn_proto_; | 530 *proto = npn_proto_; |
533 return npn_status_; | 531 return npn_status_; |
534 } | 532 } |
535 | 533 |
536 ChannelIDService* | 534 ChannelIDService* SSLClientSocketImpl::GetChannelIDService() const { |
537 SSLClientSocketOpenSSL::GetChannelIDService() const { | |
538 return channel_id_service_; | 535 return channel_id_service_; |
539 } | 536 } |
540 | 537 |
541 Error SSLClientSocketOpenSSL::GetSignedEKMForTokenBinding( | 538 Error SSLClientSocketImpl::GetSignedEKMForTokenBinding( |
542 crypto::ECPrivateKey* key, | 539 crypto::ECPrivateKey* key, |
543 std::vector<uint8_t>* out) { | 540 std::vector<uint8_t>* out) { |
544 // The same key will be used across multiple requests to sign the same value, | 541 // The same key will be used across multiple requests to sign the same value, |
545 // so the signature is cached. | 542 // so the signature is cached. |
546 std::string raw_public_key; | 543 std::string raw_public_key; |
547 if (!key->ExportRawPublicKey(&raw_public_key)) | 544 if (!key->ExportRawPublicKey(&raw_public_key)) |
548 return ERR_FAILED; | 545 return ERR_FAILED; |
549 SignedEkmMap::iterator it = tb_signed_ekm_map_.Get(raw_public_key); | 546 SignedEkmMap::iterator it = tb_signed_ekm_map_.Get(raw_public_key); |
550 if (it != tb_signed_ekm_map_.end()) { | 547 if (it != tb_signed_ekm_map_.end()) { |
551 *out = it->second; | 548 *out = it->second; |
(...skipping 12 matching lines...) Expand all Loading... |
564 if (!SignTokenBindingEkm( | 561 if (!SignTokenBindingEkm( |
565 base::StringPiece(reinterpret_cast<char*>(tb_ekm_buf), | 562 base::StringPiece(reinterpret_cast<char*>(tb_ekm_buf), |
566 sizeof(tb_ekm_buf)), | 563 sizeof(tb_ekm_buf)), |
567 key, out)) | 564 key, out)) |
568 return ERR_FAILED; | 565 return ERR_FAILED; |
569 | 566 |
570 tb_signed_ekm_map_.Put(raw_public_key, *out); | 567 tb_signed_ekm_map_.Put(raw_public_key, *out); |
571 return OK; | 568 return OK; |
572 } | 569 } |
573 | 570 |
574 crypto::ECPrivateKey* SSLClientSocketOpenSSL::GetChannelIDKey() const { | 571 crypto::ECPrivateKey* SSLClientSocketImpl::GetChannelIDKey() const { |
575 return channel_id_key_.get(); | 572 return channel_id_key_.get(); |
576 } | 573 } |
577 | 574 |
578 SSLFailureState SSLClientSocketOpenSSL::GetSSLFailureState() const { | 575 SSLFailureState SSLClientSocketImpl::GetSSLFailureState() const { |
579 return ssl_failure_state_; | 576 return ssl_failure_state_; |
580 } | 577 } |
581 | 578 |
582 int SSLClientSocketOpenSSL::ExportKeyingMaterial( | 579 int SSLClientSocketImpl::ExportKeyingMaterial(const base::StringPiece& label, |
583 const base::StringPiece& label, | 580 bool has_context, |
584 bool has_context, const base::StringPiece& context, | 581 const base::StringPiece& context, |
585 unsigned char* out, unsigned int outlen) { | 582 unsigned char* out, |
| 583 unsigned int outlen) { |
586 if (!IsConnected()) | 584 if (!IsConnected()) |
587 return ERR_SOCKET_NOT_CONNECTED; | 585 return ERR_SOCKET_NOT_CONNECTED; |
588 | 586 |
589 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 587 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
590 | 588 |
591 int rv = SSL_export_keying_material( | 589 int rv = SSL_export_keying_material( |
592 ssl_, out, outlen, label.data(), label.size(), | 590 ssl_, out, outlen, label.data(), label.size(), |
593 reinterpret_cast<const unsigned char*>(context.data()), context.length(), | 591 reinterpret_cast<const unsigned char*>(context.data()), context.length(), |
594 has_context ? 1 : 0); | 592 has_context ? 1 : 0); |
595 | 593 |
596 if (rv != 1) { | 594 if (rv != 1) { |
597 int ssl_error = SSL_get_error(ssl_, rv); | 595 int ssl_error = SSL_get_error(ssl_, rv); |
598 LOG(ERROR) << "Failed to export keying material;" | 596 LOG(ERROR) << "Failed to export keying material;" |
599 << " returned " << rv | 597 << " returned " << rv << ", SSL error code " << ssl_error; |
600 << ", SSL error code " << ssl_error; | |
601 return MapOpenSSLError(ssl_error, err_tracer); | 598 return MapOpenSSLError(ssl_error, err_tracer); |
602 } | 599 } |
603 return OK; | 600 return OK; |
604 } | 601 } |
605 | 602 |
606 int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { | 603 int SSLClientSocketImpl::Connect(const CompletionCallback& callback) { |
607 // It is an error to create an SSLClientSocket whose context has no | 604 // It is an error to create an SSLClientSocket whose context has no |
608 // TransportSecurityState. | 605 // TransportSecurityState. |
609 DCHECK(transport_security_state_); | 606 DCHECK(transport_security_state_); |
610 | 607 |
611 // Although StreamSocket does allow calling Connect() after Disconnect(), | 608 // Although StreamSocket does allow calling Connect() after Disconnect(), |
612 // this has never worked for layered sockets. CHECK to detect any consumers | 609 // this has never worked for layered sockets. CHECK to detect any consumers |
613 // reconnecting an SSL socket. | 610 // reconnecting an SSL socket. |
614 // | 611 // |
615 // TODO(davidben,mmenke): Remove this API feature. See | 612 // TODO(davidben,mmenke): Remove this API feature. See |
616 // https://crbug.com/499289. | 613 // https://crbug.com/499289. |
(...skipping 15 matching lines...) Expand all Loading... |
632 rv = DoHandshakeLoop(OK); | 629 rv = DoHandshakeLoop(OK); |
633 if (rv == ERR_IO_PENDING) { | 630 if (rv == ERR_IO_PENDING) { |
634 user_connect_callback_ = callback; | 631 user_connect_callback_ = callback; |
635 } else { | 632 } else { |
636 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 633 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
637 } | 634 } |
638 | 635 |
639 return rv > OK ? OK : rv; | 636 return rv > OK ? OK : rv; |
640 } | 637 } |
641 | 638 |
642 void SSLClientSocketOpenSSL::Disconnect() { | 639 void SSLClientSocketImpl::Disconnect() { |
643 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 640 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
644 | 641 |
645 if (ssl_) { | 642 if (ssl_) { |
646 // Calling SSL_shutdown prevents the session from being marked as | 643 // Calling SSL_shutdown prevents the session from being marked as |
647 // unresumable. | 644 // unresumable. |
648 SSL_shutdown(ssl_); | 645 SSL_shutdown(ssl_); |
649 SSL_free(ssl_); | 646 SSL_free(ssl_); |
650 ssl_ = NULL; | 647 ssl_ = NULL; |
651 } | 648 } |
652 if (transport_bio_) { | 649 if (transport_bio_) { |
653 BIO_free_all(transport_bio_); | 650 BIO_free_all(transport_bio_); |
654 transport_bio_ = NULL; | 651 transport_bio_ = NULL; |
655 } | 652 } |
656 | 653 |
657 disconnected_ = true; | 654 disconnected_ = true; |
658 | 655 |
659 // Shut down anything that may call us back. | 656 // Shut down anything that may call us back. |
660 cert_verifier_request_.reset(); | 657 cert_verifier_request_.reset(); |
661 transport_->socket()->Disconnect(); | 658 transport_->socket()->Disconnect(); |
662 | 659 |
663 // Null all callbacks, delete all buffers. | 660 // Null all callbacks, delete all buffers. |
664 transport_send_busy_ = false; | 661 transport_send_busy_ = false; |
665 send_buffer_ = NULL; | 662 send_buffer_ = NULL; |
666 transport_recv_busy_ = false; | 663 transport_recv_busy_ = false; |
667 recv_buffer_ = NULL; | 664 recv_buffer_ = NULL; |
668 | 665 |
669 user_connect_callback_.Reset(); | 666 user_connect_callback_.Reset(); |
670 user_read_callback_.Reset(); | 667 user_read_callback_.Reset(); |
671 user_write_callback_.Reset(); | 668 user_write_callback_.Reset(); |
672 user_read_buf_ = NULL; | 669 user_read_buf_ = NULL; |
673 user_read_buf_len_ = 0; | 670 user_read_buf_len_ = 0; |
674 user_write_buf_ = NULL; | 671 user_write_buf_ = NULL; |
675 user_write_buf_len_ = 0; | 672 user_write_buf_len_ = 0; |
676 | 673 |
677 pending_read_error_ = kNoPendingResult; | 674 pending_read_error_ = kNoPendingResult; |
678 pending_read_ssl_error_ = SSL_ERROR_NONE; | 675 pending_read_ssl_error_ = SSL_ERROR_NONE; |
679 pending_read_error_info_ = OpenSSLErrorInfo(); | 676 pending_read_error_info_ = OpenSSLErrorInfo(); |
680 | 677 |
681 transport_read_error_ = OK; | 678 transport_read_error_ = OK; |
682 transport_write_error_ = OK; | 679 transport_write_error_ = OK; |
683 | 680 |
684 server_cert_verify_result_.Reset(); | 681 server_cert_verify_result_.Reset(); |
685 completed_connect_ = false; | 682 completed_connect_ = false; |
(...skipping 10 matching lines...) Expand all Loading... |
696 tb_was_negotiated_ = false; | 693 tb_was_negotiated_ = false; |
697 session_pending_ = false; | 694 session_pending_ = false; |
698 certificate_verified_ = false; | 695 certificate_verified_ = false; |
699 channel_id_request_.Cancel(); | 696 channel_id_request_.Cancel(); |
700 ssl_failure_state_ = SSL_FAILURE_NONE; | 697 ssl_failure_state_ = SSL_FAILURE_NONE; |
701 | 698 |
702 signature_result_ = kNoPendingResult; | 699 signature_result_ = kNoPendingResult; |
703 signature_.clear(); | 700 signature_.clear(); |
704 } | 701 } |
705 | 702 |
706 bool SSLClientSocketOpenSSL::IsConnected() const { | 703 bool SSLClientSocketImpl::IsConnected() const { |
707 // If the handshake has not yet completed. | 704 // If the handshake has not yet completed. |
708 if (!completed_connect_) | 705 if (!completed_connect_) |
709 return false; | 706 return false; |
710 // If an asynchronous operation is still pending. | 707 // If an asynchronous operation is still pending. |
711 if (user_read_buf_.get() || user_write_buf_.get()) | 708 if (user_read_buf_.get() || user_write_buf_.get()) |
712 return true; | 709 return true; |
713 | 710 |
714 return transport_->socket()->IsConnected(); | 711 return transport_->socket()->IsConnected(); |
715 } | 712 } |
716 | 713 |
717 bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { | 714 bool SSLClientSocketImpl::IsConnectedAndIdle() const { |
718 // If the handshake has not yet completed. | 715 // If the handshake has not yet completed. |
719 if (!completed_connect_) | 716 if (!completed_connect_) |
720 return false; | 717 return false; |
721 // If an asynchronous operation is still pending. | 718 // If an asynchronous operation is still pending. |
722 if (user_read_buf_.get() || user_write_buf_.get()) | 719 if (user_read_buf_.get() || user_write_buf_.get()) |
723 return false; | 720 return false; |
724 | 721 |
725 // If there is data read from the network that has not yet been consumed, do | 722 // If there is data read from the network that has not yet been consumed, do |
726 // not treat the connection as idle. | 723 // not treat the connection as idle. |
727 // | 724 // |
728 // Note that this does not check |BIO_pending|, whether there is ciphertext | 725 // Note that this does not check |BIO_pending|, whether there is ciphertext |
729 // that has not yet been flushed to the network. |Write| returns early, so | 726 // that has not yet been flushed to the network. |Write| returns early, so |
730 // this can cause race conditions which cause a socket to not be treated | 727 // this can cause race conditions which cause a socket to not be treated |
731 // reusable when it should be. See https://crbug.com/466147. | 728 // reusable when it should be. See https://crbug.com/466147. |
732 if (BIO_wpending(transport_bio_) > 0) | 729 if (BIO_wpending(transport_bio_) > 0) |
733 return false; | 730 return false; |
734 | 731 |
735 return transport_->socket()->IsConnectedAndIdle(); | 732 return transport_->socket()->IsConnectedAndIdle(); |
736 } | 733 } |
737 | 734 |
738 int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { | 735 int SSLClientSocketImpl::GetPeerAddress(IPEndPoint* addressList) const { |
739 return transport_->socket()->GetPeerAddress(addressList); | 736 return transport_->socket()->GetPeerAddress(addressList); |
740 } | 737 } |
741 | 738 |
742 int SSLClientSocketOpenSSL::GetLocalAddress(IPEndPoint* addressList) const { | 739 int SSLClientSocketImpl::GetLocalAddress(IPEndPoint* addressList) const { |
743 return transport_->socket()->GetLocalAddress(addressList); | 740 return transport_->socket()->GetLocalAddress(addressList); |
744 } | 741 } |
745 | 742 |
746 const BoundNetLog& SSLClientSocketOpenSSL::NetLog() const { | 743 const BoundNetLog& SSLClientSocketImpl::NetLog() const { |
747 return net_log_; | 744 return net_log_; |
748 } | 745 } |
749 | 746 |
750 void SSLClientSocketOpenSSL::SetSubresourceSpeculation() { | 747 void SSLClientSocketImpl::SetSubresourceSpeculation() { |
751 if (transport_.get() && transport_->socket()) { | 748 if (transport_.get() && transport_->socket()) { |
752 transport_->socket()->SetSubresourceSpeculation(); | 749 transport_->socket()->SetSubresourceSpeculation(); |
753 } else { | 750 } else { |
754 NOTREACHED(); | 751 NOTREACHED(); |
755 } | 752 } |
756 } | 753 } |
757 | 754 |
758 void SSLClientSocketOpenSSL::SetOmniboxSpeculation() { | 755 void SSLClientSocketImpl::SetOmniboxSpeculation() { |
759 if (transport_.get() && transport_->socket()) { | 756 if (transport_.get() && transport_->socket()) { |
760 transport_->socket()->SetOmniboxSpeculation(); | 757 transport_->socket()->SetOmniboxSpeculation(); |
761 } else { | 758 } else { |
762 NOTREACHED(); | 759 NOTREACHED(); |
763 } | 760 } |
764 } | 761 } |
765 | 762 |
766 bool SSLClientSocketOpenSSL::WasEverUsed() const { | 763 bool SSLClientSocketImpl::WasEverUsed() const { |
767 return was_ever_used_; | 764 return was_ever_used_; |
768 } | 765 } |
769 | 766 |
770 bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { | 767 bool SSLClientSocketImpl::GetSSLInfo(SSLInfo* ssl_info) { |
771 ssl_info->Reset(); | 768 ssl_info->Reset(); |
772 if (server_cert_chain_->empty()) | 769 if (server_cert_chain_->empty()) |
773 return false; | 770 return false; |
774 | 771 |
775 ssl_info->cert = server_cert_verify_result_.verified_cert; | 772 ssl_info->cert = server_cert_verify_result_.verified_cert; |
776 ssl_info->unverified_cert = server_cert_; | 773 ssl_info->unverified_cert = server_cert_; |
777 ssl_info->cert_status = server_cert_verify_result_.cert_status; | 774 ssl_info->cert_status = server_cert_verify_result_.cert_status; |
778 ssl_info->is_issued_by_known_root = | 775 ssl_info->is_issued_by_known_root = |
779 server_cert_verify_result_.is_issued_by_known_root; | 776 server_cert_verify_result_.is_issued_by_known_root; |
780 ssl_info->public_key_hashes = | 777 ssl_info->public_key_hashes = server_cert_verify_result_.public_key_hashes; |
781 server_cert_verify_result_.public_key_hashes; | |
782 ssl_info->client_cert_sent = | 778 ssl_info->client_cert_sent = |
783 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); | 779 ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
784 ssl_info->channel_id_sent = channel_id_sent_; | 780 ssl_info->channel_id_sent = channel_id_sent_; |
785 ssl_info->token_binding_negotiated = tb_was_negotiated_; | 781 ssl_info->token_binding_negotiated = tb_was_negotiated_; |
786 ssl_info->token_binding_key_param = tb_negotiated_param_; | 782 ssl_info->token_binding_key_param = tb_negotiated_param_; |
787 ssl_info->pinning_failure_log = pinning_failure_log_; | 783 ssl_info->pinning_failure_log = pinning_failure_log_; |
788 | 784 |
789 AddCTInfoToSSLInfo(ssl_info); | 785 AddCTInfoToSSLInfo(ssl_info); |
790 | 786 |
791 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); | 787 const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
792 CHECK(cipher); | 788 CHECK(cipher); |
793 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); | 789 ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
794 ssl_info->key_exchange_info = | 790 ssl_info->key_exchange_info = |
795 SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl_)); | 791 SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl_)); |
796 | 792 |
797 SSLConnectionStatusSetCipherSuite( | 793 SSLConnectionStatusSetCipherSuite( |
798 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), | 794 static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), |
799 &ssl_info->connection_status); | 795 &ssl_info->connection_status); |
800 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), | 796 SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), |
801 &ssl_info->connection_status); | 797 &ssl_info->connection_status); |
802 | 798 |
803 if (!SSL_get_secure_renegotiation_support(ssl_)) | 799 if (!SSL_get_secure_renegotiation_support(ssl_)) |
804 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; | 800 ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
805 | 801 |
806 if (ssl_config_.version_fallback) | 802 if (ssl_config_.version_fallback) |
807 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; | 803 ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
808 | 804 |
809 ssl_info->handshake_type = SSL_session_reused(ssl_) ? | 805 ssl_info->handshake_type = SSL_session_reused(ssl_) |
810 SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; | 806 ? SSLInfo::HANDSHAKE_RESUME |
| 807 : SSLInfo::HANDSHAKE_FULL; |
811 | 808 |
812 DVLOG(3) << "Encoded connection status: cipher suite = " | 809 DVLOG(3) << "Encoded connection status: cipher suite = " |
813 << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) | 810 << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) |
814 << " version = " | 811 << " version = " |
815 << SSLConnectionStatusToVersion(ssl_info->connection_status); | 812 << SSLConnectionStatusToVersion(ssl_info->connection_status); |
816 return true; | 813 return true; |
817 } | 814 } |
818 | 815 |
819 void SSLClientSocketOpenSSL::GetConnectionAttempts( | 816 void SSLClientSocketImpl::GetConnectionAttempts(ConnectionAttempts* out) const { |
820 ConnectionAttempts* out) const { | |
821 out->clear(); | 817 out->clear(); |
822 } | 818 } |
823 | 819 |
824 int64_t SSLClientSocketOpenSSL::GetTotalReceivedBytes() const { | 820 int64_t SSLClientSocketImpl::GetTotalReceivedBytes() const { |
825 return transport_->socket()->GetTotalReceivedBytes(); | 821 return transport_->socket()->GetTotalReceivedBytes(); |
826 } | 822 } |
827 | 823 |
828 int SSLClientSocketOpenSSL::Read(IOBuffer* buf, | 824 int SSLClientSocketImpl::Read(IOBuffer* buf, |
829 int buf_len, | 825 int buf_len, |
830 const CompletionCallback& callback) { | 826 const CompletionCallback& callback) { |
831 user_read_buf_ = buf; | 827 user_read_buf_ = buf; |
832 user_read_buf_len_ = buf_len; | 828 user_read_buf_len_ = buf_len; |
833 | 829 |
834 int rv = DoReadLoop(); | 830 int rv = DoReadLoop(); |
835 | 831 |
836 if (rv == ERR_IO_PENDING) { | 832 if (rv == ERR_IO_PENDING) { |
837 user_read_callback_ = callback; | 833 user_read_callback_ = callback; |
838 } else { | 834 } else { |
839 if (rv > 0) | 835 if (rv > 0) |
840 was_ever_used_ = true; | 836 was_ever_used_ = true; |
841 user_read_buf_ = NULL; | 837 user_read_buf_ = NULL; |
842 user_read_buf_len_ = 0; | 838 user_read_buf_len_ = 0; |
843 } | 839 } |
844 | 840 |
845 return rv; | 841 return rv; |
846 } | 842 } |
847 | 843 |
848 int SSLClientSocketOpenSSL::Write(IOBuffer* buf, | 844 int SSLClientSocketImpl::Write(IOBuffer* buf, |
849 int buf_len, | 845 int buf_len, |
850 const CompletionCallback& callback) { | 846 const CompletionCallback& callback) { |
851 user_write_buf_ = buf; | 847 user_write_buf_ = buf; |
852 user_write_buf_len_ = buf_len; | 848 user_write_buf_len_ = buf_len; |
853 | 849 |
854 int rv = DoWriteLoop(); | 850 int rv = DoWriteLoop(); |
855 | 851 |
856 if (rv == ERR_IO_PENDING) { | 852 if (rv == ERR_IO_PENDING) { |
857 user_write_callback_ = callback; | 853 user_write_callback_ = callback; |
858 } else { | 854 } else { |
859 if (rv > 0) | 855 if (rv > 0) |
860 was_ever_used_ = true; | 856 was_ever_used_ = true; |
861 user_write_buf_ = NULL; | 857 user_write_buf_ = NULL; |
862 user_write_buf_len_ = 0; | 858 user_write_buf_len_ = 0; |
863 } | 859 } |
864 | 860 |
865 return rv; | 861 return rv; |
866 } | 862 } |
867 | 863 |
868 int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32_t size) { | 864 int SSLClientSocketImpl::SetReceiveBufferSize(int32_t size) { |
869 return transport_->socket()->SetReceiveBufferSize(size); | 865 return transport_->socket()->SetReceiveBufferSize(size); |
870 } | 866 } |
871 | 867 |
872 int SSLClientSocketOpenSSL::SetSendBufferSize(int32_t size) { | 868 int SSLClientSocketImpl::SetSendBufferSize(int32_t size) { |
873 return transport_->socket()->SetSendBufferSize(size); | 869 return transport_->socket()->SetSendBufferSize(size); |
874 } | 870 } |
875 | 871 |
876 int SSLClientSocketOpenSSL::Init() { | 872 int SSLClientSocketImpl::Init() { |
877 DCHECK(!ssl_); | 873 DCHECK(!ssl_); |
878 DCHECK(!transport_bio_); | 874 DCHECK(!transport_bio_); |
879 | 875 |
880 #if defined(USE_NSS_CERTS) | 876 #if defined(USE_NSS_CERTS) |
881 if (ssl_config_.cert_io_enabled) { | 877 if (ssl_config_.cert_io_enabled) { |
882 // TODO(davidben): Move this out of SSLClientSocket. See | 878 // TODO(davidben): Move this out of SSLClientSocket. See |
883 // https://crbug.com/539520. | 879 // https://crbug.com/539520. |
884 EnsureNSSHttpIOInit(); | 880 EnsureNSSHttpIOInit(); |
885 } | 881 } |
886 #endif | 882 #endif |
(...skipping 21 matching lines...) Expand all Loading... |
908 if (session) | 904 if (session) |
909 SSL_set_session(ssl_, session.get()); | 905 SSL_set_session(ssl_, session.get()); |
910 | 906 |
911 send_buffer_ = new GrowableIOBuffer(); | 907 send_buffer_ = new GrowableIOBuffer(); |
912 send_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); | 908 send_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
913 recv_buffer_ = new GrowableIOBuffer(); | 909 recv_buffer_ = new GrowableIOBuffer(); |
914 recv_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); | 910 recv_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
915 | 911 |
916 BIO* ssl_bio = NULL; | 912 BIO* ssl_bio = NULL; |
917 | 913 |
918 // SSLClientSocketOpenSSL retains ownership of the BIO buffers. | 914 // SSLClientSocketImpl retains ownership of the BIO buffers. |
919 if (!BIO_new_bio_pair_external_buf( | 915 if (!BIO_new_bio_pair_external_buf( |
920 &ssl_bio, send_buffer_->capacity(), | 916 &ssl_bio, send_buffer_->capacity(), |
921 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, | 917 reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, |
922 recv_buffer_->capacity(), | 918 recv_buffer_->capacity(), |
923 reinterpret_cast<uint8_t*>(recv_buffer_->data()))) | 919 reinterpret_cast<uint8_t*>(recv_buffer_->data()))) |
924 return ERR_UNEXPECTED; | 920 return ERR_UNEXPECTED; |
925 DCHECK(ssl_bio); | 921 DCHECK(ssl_bio); |
926 DCHECK(transport_bio_); | 922 DCHECK(transport_bio_); |
927 | 923 |
928 // Install a callback on OpenSSL's end to plumb transport errors through. | 924 // Install a callback on OpenSSL's end to plumb transport errors through. |
929 BIO_set_callback(ssl_bio, &SSLClientSocketOpenSSL::BIOCallback); | 925 BIO_set_callback(ssl_bio, &SSLClientSocketImpl::BIOCallback); |
930 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); | 926 BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); |
931 | 927 |
932 SSL_set_bio(ssl_, ssl_bio, ssl_bio); | 928 SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
933 | 929 |
934 DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); | 930 DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); |
935 DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); | 931 DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); |
936 SSL_set_min_version(ssl_, ssl_config_.version_min); | 932 SSL_set_min_version(ssl_, ssl_config_.version_min); |
937 SSL_set_max_version(ssl_, ssl_config_.version_max); | 933 SSL_set_max_version(ssl_, ssl_config_.version_max); |
938 | 934 |
939 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, | 935 // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 command.append(":!"); | 985 command.append(":!"); |
990 command.append(SSL_CIPHER_get_name(cipher)); | 986 command.append(SSL_CIPHER_get_name(cipher)); |
991 } | 987 } |
992 } | 988 } |
993 | 989 |
994 int rv = SSL_set_cipher_list(ssl_, command.c_str()); | 990 int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
995 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. | 991 // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
996 // This will almost certainly result in the socket failing to complete the | 992 // This will almost certainly result in the socket failing to complete the |
997 // handshake at which point the appropriate error is bubbled up to the client. | 993 // handshake at which point the appropriate error is bubbled up to the client. |
998 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " | 994 LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
999 "returned " << rv; | 995 "returned " |
| 996 << rv; |
1000 | 997 |
1001 // TLS channel ids. | 998 // TLS channel ids. |
1002 if (IsChannelIDEnabled(ssl_config_, channel_id_service_)) { | 999 if (IsChannelIDEnabled(ssl_config_, channel_id_service_)) { |
1003 SSL_enable_tls_channel_id(ssl_); | 1000 SSL_enable_tls_channel_id(ssl_); |
1004 } | 1001 } |
1005 | 1002 |
1006 if (!ssl_config_.alpn_protos.empty()) { | 1003 if (!ssl_config_.alpn_protos.empty()) { |
1007 // Get list of ciphers that are enabled. | 1004 // Get list of ciphers that are enabled. |
1008 STACK_OF(SSL_CIPHER)* enabled_ciphers = SSL_get_ciphers(ssl_); | 1005 STACK_OF(SSL_CIPHER)* enabled_ciphers = SSL_get_ciphers(ssl_); |
1009 DCHECK(enabled_ciphers); | 1006 DCHECK(enabled_ciphers); |
(...skipping 21 matching lines...) Expand all Loading... |
1031 SSL_enable_signed_cert_timestamps(ssl_); | 1028 SSL_enable_signed_cert_timestamps(ssl_); |
1032 SSL_enable_ocsp_stapling(ssl_); | 1029 SSL_enable_ocsp_stapling(ssl_); |
1033 } | 1030 } |
1034 | 1031 |
1035 if (cert_verifier_->SupportsOCSPStapling()) | 1032 if (cert_verifier_->SupportsOCSPStapling()) |
1036 SSL_enable_ocsp_stapling(ssl_); | 1033 SSL_enable_ocsp_stapling(ssl_); |
1037 | 1034 |
1038 return OK; | 1035 return OK; |
1039 } | 1036 } |
1040 | 1037 |
1041 void SSLClientSocketOpenSSL::DoReadCallback(int rv) { | 1038 void SSLClientSocketImpl::DoReadCallback(int rv) { |
1042 // Since Run may result in Read being called, clear |user_read_callback_| | 1039 // Since Run may result in Read being called, clear |user_read_callback_| |
1043 // up front. | 1040 // up front. |
1044 if (rv > 0) | 1041 if (rv > 0) |
1045 was_ever_used_ = true; | 1042 was_ever_used_ = true; |
1046 user_read_buf_ = NULL; | 1043 user_read_buf_ = NULL; |
1047 user_read_buf_len_ = 0; | 1044 user_read_buf_len_ = 0; |
1048 base::ResetAndReturn(&user_read_callback_).Run(rv); | 1045 base::ResetAndReturn(&user_read_callback_).Run(rv); |
1049 } | 1046 } |
1050 | 1047 |
1051 void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { | 1048 void SSLClientSocketImpl::DoWriteCallback(int rv) { |
1052 // Since Run may result in Write being called, clear |user_write_callback_| | 1049 // Since Run may result in Write being called, clear |user_write_callback_| |
1053 // up front. | 1050 // up front. |
1054 if (rv > 0) | 1051 if (rv > 0) |
1055 was_ever_used_ = true; | 1052 was_ever_used_ = true; |
1056 user_write_buf_ = NULL; | 1053 user_write_buf_ = NULL; |
1057 user_write_buf_len_ = 0; | 1054 user_write_buf_len_ = 0; |
1058 base::ResetAndReturn(&user_write_callback_).Run(rv); | 1055 base::ResetAndReturn(&user_write_callback_).Run(rv); |
1059 } | 1056 } |
1060 | 1057 |
1061 bool SSLClientSocketOpenSSL::DoTransportIO() { | 1058 bool SSLClientSocketImpl::DoTransportIO() { |
1062 bool network_moved = false; | 1059 bool network_moved = false; |
1063 int rv; | 1060 int rv; |
1064 // Read and write as much data as possible. The loop is necessary because | 1061 // Read and write as much data as possible. The loop is necessary because |
1065 // Write() may return synchronously. | 1062 // Write() may return synchronously. |
1066 do { | 1063 do { |
1067 rv = BufferSend(); | 1064 rv = BufferSend(); |
1068 if (rv != ERR_IO_PENDING && rv != 0) | 1065 if (rv != ERR_IO_PENDING && rv != 0) |
1069 network_moved = true; | 1066 network_moved = true; |
1070 } while (rv > 0); | 1067 } while (rv > 0); |
1071 if (transport_read_error_ == OK && BufferRecv() != ERR_IO_PENDING) | 1068 if (transport_read_error_ == OK && BufferRecv() != ERR_IO_PENDING) |
1072 network_moved = true; | 1069 network_moved = true; |
1073 return network_moved; | 1070 return network_moved; |
1074 } | 1071 } |
1075 | 1072 |
1076 // TODO(cbentzel): Remove including "base/threading/thread_local.h" and | 1073 // TODO(cbentzel): Remove including "base/threading/thread_local.h" and |
1077 // g_first_run_completed once crbug.com/424386 is fixed. | 1074 // g_first_run_completed once crbug.com/424386 is fixed. |
1078 base::LazyInstance<base::ThreadLocalBoolean>::Leaky g_first_run_completed = | 1075 base::LazyInstance<base::ThreadLocalBoolean>::Leaky g_first_run_completed = |
1079 LAZY_INSTANCE_INITIALIZER; | 1076 LAZY_INSTANCE_INITIALIZER; |
1080 | 1077 |
1081 int SSLClientSocketOpenSSL::DoHandshake() { | 1078 int SSLClientSocketImpl::DoHandshake() { |
1082 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1079 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
1083 | 1080 |
1084 int rv; | 1081 int rv; |
1085 | 1082 |
1086 // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386 | 1083 // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386 |
1087 // is fixed. | 1084 // is fixed. |
1088 if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) { | 1085 if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) { |
1089 rv = SSL_do_handshake(ssl_); | 1086 rv = SSL_do_handshake(ssl_); |
1090 } else { | 1087 } else { |
1091 if (g_first_run_completed.Get().Get()) { | 1088 if (g_first_run_completed.Get().Get()) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 ssl_failure_state_ = SSL_FAILURE_NEXT_PROTO; | 1155 ssl_failure_state_ = SSL_FAILURE_NEXT_PROTO; |
1159 } else { | 1156 } else { |
1160 ssl_failure_state_ = SSL_FAILURE_UNKNOWN; | 1157 ssl_failure_state_ = SSL_FAILURE_UNKNOWN; |
1161 } | 1158 } |
1162 } | 1159 } |
1163 | 1160 |
1164 GotoState(STATE_HANDSHAKE_COMPLETE); | 1161 GotoState(STATE_HANDSHAKE_COMPLETE); |
1165 return net_error; | 1162 return net_error; |
1166 } | 1163 } |
1167 | 1164 |
1168 int SSLClientSocketOpenSSL::DoHandshakeComplete(int result) { | 1165 int SSLClientSocketImpl::DoHandshakeComplete(int result) { |
1169 if (result < 0) | 1166 if (result < 0) |
1170 return result; | 1167 return result; |
1171 | 1168 |
1172 if (ssl_config_.version_fallback && | 1169 if (ssl_config_.version_fallback && |
1173 ssl_config_.version_max < ssl_config_.version_fallback_min) { | 1170 ssl_config_.version_max < ssl_config_.version_fallback_min) { |
1174 return ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION; | 1171 return ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION; |
1175 } | 1172 } |
1176 | 1173 |
1177 // Check that if token binding was negotiated, then extended master secret | 1174 // Check that if token binding was negotiated, then extended master secret |
1178 // must also be negotiated. | 1175 // must also be negotiated. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1219 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLServerKeyExchangeHash", | 1216 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLServerKeyExchangeHash", |
1220 server_key_exchange_hash); | 1217 server_key_exchange_hash); |
1221 } | 1218 } |
1222 | 1219 |
1223 // Verify the certificate. | 1220 // Verify the certificate. |
1224 UpdateServerCert(); | 1221 UpdateServerCert(); |
1225 GotoState(STATE_VERIFY_CERT); | 1222 GotoState(STATE_VERIFY_CERT); |
1226 return OK; | 1223 return OK; |
1227 } | 1224 } |
1228 | 1225 |
1229 int SSLClientSocketOpenSSL::DoChannelIDLookup() { | 1226 int SSLClientSocketImpl::DoChannelIDLookup() { |
1230 NetLog::ParametersCallback callback = base::Bind( | 1227 NetLog::ParametersCallback callback = base::Bind( |
1231 &NetLogChannelIDLookupCallback, base::Unretained(channel_id_service_)); | 1228 &NetLogChannelIDLookupCallback, base::Unretained(channel_id_service_)); |
1232 net_log_.BeginEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, callback); | 1229 net_log_.BeginEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, callback); |
1233 GotoState(STATE_CHANNEL_ID_LOOKUP_COMPLETE); | 1230 GotoState(STATE_CHANNEL_ID_LOOKUP_COMPLETE); |
1234 return channel_id_service_->GetOrCreateChannelID( | 1231 return channel_id_service_->GetOrCreateChannelID( |
1235 host_and_port_.host(), &channel_id_key_, | 1232 host_and_port_.host(), &channel_id_key_, |
1236 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, | 1233 base::Bind(&SSLClientSocketImpl::OnHandshakeIOComplete, |
1237 base::Unretained(this)), | 1234 base::Unretained(this)), |
1238 &channel_id_request_); | 1235 &channel_id_request_); |
1239 } | 1236 } |
1240 | 1237 |
1241 int SSLClientSocketOpenSSL::DoChannelIDLookupComplete(int result) { | 1238 int SSLClientSocketImpl::DoChannelIDLookupComplete(int result) { |
1242 net_log_.EndEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, | 1239 net_log_.EndEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, |
1243 base::Bind(&NetLogChannelIDLookupCompleteCallback, | 1240 base::Bind(&NetLogChannelIDLookupCompleteCallback, |
1244 channel_id_key_.get(), result)); | 1241 channel_id_key_.get(), result)); |
1245 if (result < 0) | 1242 if (result < 0) |
1246 return result; | 1243 return result; |
1247 | 1244 |
1248 // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key | 1245 // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key |
1249 // type. | 1246 // type. |
1250 DCHECK(channel_id_key_); | 1247 DCHECK(channel_id_key_); |
1251 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1248 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
1252 int rv = SSL_set1_tls_channel_id(ssl_, channel_id_key_->key()); | 1249 int rv = SSL_set1_tls_channel_id(ssl_, channel_id_key_->key()); |
1253 if (!rv) { | 1250 if (!rv) { |
1254 LOG(ERROR) << "Failed to set Channel ID."; | 1251 LOG(ERROR) << "Failed to set Channel ID."; |
1255 int err = SSL_get_error(ssl_, rv); | 1252 int err = SSL_get_error(ssl_, rv); |
1256 return MapOpenSSLError(err, err_tracer); | 1253 return MapOpenSSLError(err, err_tracer); |
1257 } | 1254 } |
1258 | 1255 |
1259 // Return to the handshake. | 1256 // Return to the handshake. |
1260 channel_id_sent_ = true; | 1257 channel_id_sent_ = true; |
1261 GotoState(STATE_HANDSHAKE); | 1258 GotoState(STATE_HANDSHAKE); |
1262 return OK; | 1259 return OK; |
1263 } | 1260 } |
1264 | 1261 |
1265 int SSLClientSocketOpenSSL::DoVerifyCert(int result) { | 1262 int SSLClientSocketImpl::DoVerifyCert(int result) { |
1266 DCHECK(!server_cert_chain_->empty()); | 1263 DCHECK(!server_cert_chain_->empty()); |
1267 DCHECK(start_cert_verification_time_.is_null()); | 1264 DCHECK(start_cert_verification_time_.is_null()); |
1268 | 1265 |
1269 GotoState(STATE_VERIFY_CERT_COMPLETE); | 1266 GotoState(STATE_VERIFY_CERT_COMPLETE); |
1270 | 1267 |
1271 // OpenSSL decoded the certificate, but the platform certificate | 1268 // OpenSSL decoded the certificate, but the platform certificate |
1272 // implementation could not. This is treated as a fatal SSL-level protocol | 1269 // implementation could not. This is treated as a fatal SSL-level protocol |
1273 // error rather than a certificate error. See https://crbug.com/91341. | 1270 // error rather than a certificate error. See https://crbug.com/91341. |
1274 if (!server_cert_.get()) | 1271 if (!server_cert_.get()) |
1275 return ERR_SSL_SERVER_CERT_BAD_FORMAT; | 1272 return ERR_SSL_SERVER_CERT_BAD_FORMAT; |
(...skipping 24 matching lines...) Expand all Loading... |
1300 } | 1297 } |
1301 | 1298 |
1302 start_cert_verification_time_ = base::TimeTicks::Now(); | 1299 start_cert_verification_time_ = base::TimeTicks::Now(); |
1303 | 1300 |
1304 return cert_verifier_->Verify( | 1301 return cert_verifier_->Verify( |
1305 server_cert_.get(), host_and_port_.host(), ocsp_response, | 1302 server_cert_.get(), host_and_port_.host(), ocsp_response, |
1306 ssl_config_.GetCertVerifyFlags(), | 1303 ssl_config_.GetCertVerifyFlags(), |
1307 // TODO(davidben): Route the CRLSet through SSLConfig so | 1304 // TODO(davidben): Route the CRLSet through SSLConfig so |
1308 // SSLClientSocket doesn't depend on SSLConfigService. | 1305 // SSLClientSocket doesn't depend on SSLConfigService. |
1309 SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, | 1306 SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, |
1310 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, | 1307 base::Bind(&SSLClientSocketImpl::OnHandshakeIOComplete, |
1311 base::Unretained(this)), | 1308 base::Unretained(this)), |
1312 &cert_verifier_request_, net_log_); | 1309 &cert_verifier_request_, net_log_); |
1313 } | 1310 } |
1314 | 1311 |
1315 int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { | 1312 int SSLClientSocketImpl::DoVerifyCertComplete(int result) { |
1316 cert_verifier_request_.reset(); | 1313 cert_verifier_request_.reset(); |
1317 | 1314 |
1318 if (!start_cert_verification_time_.is_null()) { | 1315 if (!start_cert_verification_time_.is_null()) { |
1319 base::TimeDelta verify_time = | 1316 base::TimeDelta verify_time = |
1320 base::TimeTicks::Now() - start_cert_verification_time_; | 1317 base::TimeTicks::Now() - start_cert_verification_time_; |
1321 if (result == OK) { | 1318 if (result == OK) { |
1322 UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); | 1319 UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); |
1323 } else { | 1320 } else { |
1324 UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); | 1321 UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); |
1325 } | 1322 } |
(...skipping 13 matching lines...) Expand all Loading... |
1339 | 1336 |
1340 if (result == OK) { | 1337 if (result == OK) { |
1341 // Only check Certificate Transparency if there were no other errors with | 1338 // Only check Certificate Transparency if there were no other errors with |
1342 // the connection. | 1339 // the connection. |
1343 VerifyCT(); | 1340 VerifyCT(); |
1344 | 1341 |
1345 DCHECK(!certificate_verified_); | 1342 DCHECK(!certificate_verified_); |
1346 certificate_verified_ = true; | 1343 certificate_verified_ = true; |
1347 MaybeCacheSession(); | 1344 MaybeCacheSession(); |
1348 } else { | 1345 } else { |
1349 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) | 1346 DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) << " (" |
1350 << " (" << result << ")"; | 1347 << result << ")"; |
1351 } | 1348 } |
1352 | 1349 |
1353 completed_connect_ = true; | 1350 completed_connect_ = true; |
1354 // Exit DoHandshakeLoop and return the result to the caller to Connect. | 1351 // Exit DoHandshakeLoop and return the result to the caller to Connect. |
1355 DCHECK_EQ(STATE_NONE, next_handshake_state_); | 1352 DCHECK_EQ(STATE_NONE, next_handshake_state_); |
1356 return result; | 1353 return result; |
1357 } | 1354 } |
1358 | 1355 |
1359 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { | 1356 void SSLClientSocketImpl::DoConnectCallback(int rv) { |
1360 if (!user_connect_callback_.is_null()) { | 1357 if (!user_connect_callback_.is_null()) { |
1361 CompletionCallback c = user_connect_callback_; | 1358 CompletionCallback c = user_connect_callback_; |
1362 user_connect_callback_.Reset(); | 1359 user_connect_callback_.Reset(); |
1363 c.Run(rv > OK ? OK : rv); | 1360 c.Run(rv > OK ? OK : rv); |
1364 } | 1361 } |
1365 } | 1362 } |
1366 | 1363 |
1367 void SSLClientSocketOpenSSL::UpdateServerCert() { | 1364 void SSLClientSocketImpl::UpdateServerCert() { |
1368 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); | 1365 server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
1369 server_cert_ = server_cert_chain_->AsOSChain(); | 1366 server_cert_ = server_cert_chain_->AsOSChain(); |
1370 if (server_cert_.get()) { | 1367 if (server_cert_.get()) { |
1371 net_log_.AddEvent( | 1368 net_log_.AddEvent(NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
1372 NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, | 1369 base::Bind(&NetLogX509CertificateCallback, |
1373 base::Bind(&NetLogX509CertificateCallback, | 1370 base::Unretained(server_cert_.get()))); |
1374 base::Unretained(server_cert_.get()))); | |
1375 } | 1371 } |
1376 } | 1372 } |
1377 | 1373 |
1378 void SSLClientSocketOpenSSL::VerifyCT() { | 1374 void SSLClientSocketImpl::VerifyCT() { |
1379 if (!cert_transparency_verifier_) | 1375 if (!cert_transparency_verifier_) |
1380 return; | 1376 return; |
1381 | 1377 |
1382 const uint8_t* ocsp_response_raw; | 1378 const uint8_t* ocsp_response_raw; |
1383 size_t ocsp_response_len; | 1379 size_t ocsp_response_len; |
1384 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); | 1380 SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
1385 std::string ocsp_response; | 1381 std::string ocsp_response; |
1386 if (ocsp_response_len > 0) { | 1382 if (ocsp_response_len > 0) { |
1387 ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), | 1383 ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
1388 ocsp_response_len); | 1384 ocsp_response_len); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1430 server_cert_verify_result_.cert_status &= ~CERT_STATUS_IS_EV; | 1426 server_cert_verify_result_.cert_status &= ~CERT_STATUS_IS_EV; |
1431 } | 1427 } |
1432 } | 1428 } |
1433 ct_verify_result_.cert_policy_compliance = | 1429 ct_verify_result_.cert_policy_compliance = |
1434 policy_enforcer_->DoesConformToCertPolicy( | 1430 policy_enforcer_->DoesConformToCertPolicy( |
1435 server_cert_verify_result_.verified_cert.get(), | 1431 server_cert_verify_result_.verified_cert.get(), |
1436 ct_verify_result_.verified_scts, net_log_); | 1432 ct_verify_result_.verified_scts, net_log_); |
1437 } | 1433 } |
1438 } | 1434 } |
1439 | 1435 |
1440 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { | 1436 void SSLClientSocketImpl::OnHandshakeIOComplete(int result) { |
1441 int rv = DoHandshakeLoop(result); | 1437 int rv = DoHandshakeLoop(result); |
1442 if (rv != ERR_IO_PENDING) { | 1438 if (rv != ERR_IO_PENDING) { |
1443 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 1439 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
1444 DoConnectCallback(rv); | 1440 DoConnectCallback(rv); |
1445 } | 1441 } |
1446 } | 1442 } |
1447 | 1443 |
1448 void SSLClientSocketOpenSSL::OnSendComplete(int result) { | 1444 void SSLClientSocketImpl::OnSendComplete(int result) { |
1449 if (next_handshake_state_ == STATE_HANDSHAKE) { | 1445 if (next_handshake_state_ == STATE_HANDSHAKE) { |
1450 // In handshake phase. | 1446 // In handshake phase. |
1451 OnHandshakeIOComplete(result); | 1447 OnHandshakeIOComplete(result); |
1452 return; | 1448 return; |
1453 } | 1449 } |
1454 | 1450 |
1455 // During a renegotiation, a Read call may also be blocked on a transport | 1451 // During a renegotiation, a Read call may also be blocked on a transport |
1456 // write, so retry both operations. | 1452 // write, so retry both operations. |
1457 PumpReadWriteEvents(); | 1453 PumpReadWriteEvents(); |
1458 } | 1454 } |
1459 | 1455 |
1460 void SSLClientSocketOpenSSL::OnRecvComplete(int result) { | 1456 void SSLClientSocketImpl::OnRecvComplete(int result) { |
1461 TRACE_EVENT0("net", "SSLClientSocketOpenSSL::OnRecvComplete"); | 1457 TRACE_EVENT0("net", "SSLClientSocketImpl::OnRecvComplete"); |
1462 if (next_handshake_state_ == STATE_HANDSHAKE) { | 1458 if (next_handshake_state_ == STATE_HANDSHAKE) { |
1463 // In handshake phase. | 1459 // In handshake phase. |
1464 OnHandshakeIOComplete(result); | 1460 OnHandshakeIOComplete(result); |
1465 return; | 1461 return; |
1466 } | 1462 } |
1467 | 1463 |
1468 // Network layer received some data, check if client requested to read | 1464 // Network layer received some data, check if client requested to read |
1469 // decrypted data. | 1465 // decrypted data. |
1470 if (!user_read_buf_.get()) | 1466 if (!user_read_buf_.get()) |
1471 return; | 1467 return; |
1472 | 1468 |
1473 int rv = DoReadLoop(); | 1469 int rv = DoReadLoop(); |
1474 if (rv != ERR_IO_PENDING) | 1470 if (rv != ERR_IO_PENDING) |
1475 DoReadCallback(rv); | 1471 DoReadCallback(rv); |
1476 } | 1472 } |
1477 | 1473 |
1478 int SSLClientSocketOpenSSL::DoHandshakeLoop(int last_io_result) { | 1474 int SSLClientSocketImpl::DoHandshakeLoop(int last_io_result) { |
1479 TRACE_EVENT0("net", "SSLClientSocketOpenSSL::DoHandshakeLoop"); | 1475 TRACE_EVENT0("net", "SSLClientSocketImpl::DoHandshakeLoop"); |
1480 int rv = last_io_result; | 1476 int rv = last_io_result; |
1481 do { | 1477 do { |
1482 // Default to STATE_NONE for next state. | 1478 // Default to STATE_NONE for next state. |
1483 // (This is a quirk carried over from the windows | 1479 // (This is a quirk carried over from the windows |
1484 // implementation. It makes reading the logs a bit harder.) | 1480 // implementation. It makes reading the logs a bit harder.) |
1485 // State handlers can and often do call GotoState just | 1481 // State handlers can and often do call GotoState just |
1486 // to stay in the current state. | 1482 // to stay in the current state. |
1487 State state = next_handshake_state_; | 1483 State state = next_handshake_state_; |
1488 GotoState(STATE_NONE); | 1484 GotoState(STATE_NONE); |
1489 switch (state) { | 1485 switch (state) { |
1490 case STATE_HANDSHAKE: | 1486 case STATE_HANDSHAKE: |
1491 rv = DoHandshake(); | 1487 rv = DoHandshake(); |
1492 break; | 1488 break; |
1493 case STATE_HANDSHAKE_COMPLETE: | 1489 case STATE_HANDSHAKE_COMPLETE: |
1494 rv = DoHandshakeComplete(rv); | 1490 rv = DoHandshakeComplete(rv); |
1495 break; | 1491 break; |
1496 case STATE_CHANNEL_ID_LOOKUP: | 1492 case STATE_CHANNEL_ID_LOOKUP: |
1497 DCHECK_EQ(OK, rv); | 1493 DCHECK_EQ(OK, rv); |
1498 rv = DoChannelIDLookup(); | 1494 rv = DoChannelIDLookup(); |
1499 break; | 1495 break; |
1500 case STATE_CHANNEL_ID_LOOKUP_COMPLETE: | 1496 case STATE_CHANNEL_ID_LOOKUP_COMPLETE: |
1501 rv = DoChannelIDLookupComplete(rv); | 1497 rv = DoChannelIDLookupComplete(rv); |
1502 break; | 1498 break; |
1503 case STATE_VERIFY_CERT: | 1499 case STATE_VERIFY_CERT: |
1504 DCHECK_EQ(OK, rv); | 1500 DCHECK_EQ(OK, rv); |
1505 rv = DoVerifyCert(rv); | 1501 rv = DoVerifyCert(rv); |
1506 break; | 1502 break; |
1507 case STATE_VERIFY_CERT_COMPLETE: | 1503 case STATE_VERIFY_CERT_COMPLETE: |
1508 rv = DoVerifyCertComplete(rv); | 1504 rv = DoVerifyCertComplete(rv); |
1509 break; | 1505 break; |
1510 case STATE_NONE: | 1506 case STATE_NONE: |
1511 default: | 1507 default: |
1512 rv = ERR_UNEXPECTED; | 1508 rv = ERR_UNEXPECTED; |
1513 NOTREACHED() << "unexpected state" << state; | 1509 NOTREACHED() << "unexpected state" << state; |
1514 break; | 1510 break; |
1515 } | 1511 } |
1516 | 1512 |
1517 bool network_moved = DoTransportIO(); | 1513 bool network_moved = DoTransportIO(); |
1518 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { | 1514 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
1519 // In general we exit the loop if rv is ERR_IO_PENDING. In this | 1515 // In general we exit the loop if rv is ERR_IO_PENDING. In this |
1520 // special case we keep looping even if rv is ERR_IO_PENDING because | 1516 // special case we keep looping even if rv is ERR_IO_PENDING because |
1521 // the transport IO may allow DoHandshake to make progress. | 1517 // the transport IO may allow DoHandshake to make progress. |
1522 rv = OK; // This causes us to stay in the loop. | 1518 rv = OK; // This causes us to stay in the loop. |
1523 } | 1519 } |
1524 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); | 1520 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
1525 return rv; | 1521 return rv; |
1526 } | 1522 } |
1527 | 1523 |
1528 int SSLClientSocketOpenSSL::DoReadLoop() { | 1524 int SSLClientSocketImpl::DoReadLoop() { |
1529 bool network_moved; | 1525 bool network_moved; |
1530 int rv; | 1526 int rv; |
1531 do { | 1527 do { |
1532 rv = DoPayloadRead(); | 1528 rv = DoPayloadRead(); |
1533 network_moved = DoTransportIO(); | 1529 network_moved = DoTransportIO(); |
1534 } while (rv == ERR_IO_PENDING && network_moved); | 1530 } while (rv == ERR_IO_PENDING && network_moved); |
1535 | 1531 |
1536 return rv; | 1532 return rv; |
1537 } | 1533 } |
1538 | 1534 |
1539 int SSLClientSocketOpenSSL::DoWriteLoop() { | 1535 int SSLClientSocketImpl::DoWriteLoop() { |
1540 bool network_moved; | 1536 bool network_moved; |
1541 int rv; | 1537 int rv; |
1542 do { | 1538 do { |
1543 rv = DoPayloadWrite(); | 1539 rv = DoPayloadWrite(); |
1544 network_moved = DoTransportIO(); | 1540 network_moved = DoTransportIO(); |
1545 } while (rv == ERR_IO_PENDING && network_moved); | 1541 } while (rv == ERR_IO_PENDING && network_moved); |
1546 | 1542 |
1547 return rv; | 1543 return rv; |
1548 } | 1544 } |
1549 | 1545 |
1550 int SSLClientSocketOpenSSL::DoPayloadRead() { | 1546 int SSLClientSocketImpl::DoPayloadRead() { |
1551 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1547 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
1552 | 1548 |
1553 DCHECK_LT(0, user_read_buf_len_); | 1549 DCHECK_LT(0, user_read_buf_len_); |
1554 DCHECK(user_read_buf_.get()); | 1550 DCHECK(user_read_buf_.get()); |
1555 | 1551 |
1556 int rv; | 1552 int rv; |
1557 if (pending_read_error_ != kNoPendingResult) { | 1553 if (pending_read_error_ != kNoPendingResult) { |
1558 rv = pending_read_error_; | 1554 rv = pending_read_error_; |
1559 pending_read_error_ = kNoPendingResult; | 1555 pending_read_error_ = kNoPendingResult; |
1560 if (rv == 0) { | 1556 if (rv == 0) { |
1561 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, | 1557 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
1562 rv, user_read_buf_->data()); | 1558 user_read_buf_->data()); |
1563 } else { | 1559 } else { |
1564 net_log_.AddEvent( | 1560 net_log_.AddEvent( |
1565 NetLog::TYPE_SSL_READ_ERROR, | 1561 NetLog::TYPE_SSL_READ_ERROR, |
1566 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, | 1562 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
1567 pending_read_error_info_)); | 1563 pending_read_error_info_)); |
1568 } | 1564 } |
1569 pending_read_ssl_error_ = SSL_ERROR_NONE; | 1565 pending_read_ssl_error_ = SSL_ERROR_NONE; |
1570 pending_read_error_info_ = OpenSSLErrorInfo(); | 1566 pending_read_error_info_ = OpenSSLErrorInfo(); |
1571 return rv; | 1567 return rv; |
1572 } | 1568 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1642 net_log_.AddEvent( | 1638 net_log_.AddEvent( |
1643 NetLog::TYPE_SSL_READ_ERROR, | 1639 NetLog::TYPE_SSL_READ_ERROR, |
1644 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, | 1640 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
1645 pending_read_error_info_)); | 1641 pending_read_error_info_)); |
1646 pending_read_ssl_error_ = SSL_ERROR_NONE; | 1642 pending_read_ssl_error_ = SSL_ERROR_NONE; |
1647 pending_read_error_info_ = OpenSSLErrorInfo(); | 1643 pending_read_error_info_ = OpenSSLErrorInfo(); |
1648 } | 1644 } |
1649 return rv; | 1645 return rv; |
1650 } | 1646 } |
1651 | 1647 |
1652 int SSLClientSocketOpenSSL::DoPayloadWrite() { | 1648 int SSLClientSocketImpl::DoPayloadWrite() { |
1653 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1649 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
1654 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); | 1650 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
1655 | 1651 |
1656 if (rv >= 0) { | 1652 if (rv >= 0) { |
1657 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 1653 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
1658 user_write_buf_->data()); | 1654 user_write_buf_->data()); |
1659 return rv; | 1655 return rv; |
1660 } | 1656 } |
1661 | 1657 |
1662 int ssl_error = SSL_get_error(ssl_, rv); | 1658 int ssl_error = SSL_get_error(ssl_, rv); |
1663 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) | 1659 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) |
1664 return ERR_IO_PENDING; | 1660 return ERR_IO_PENDING; |
1665 OpenSSLErrorInfo error_info; | 1661 OpenSSLErrorInfo error_info; |
1666 int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, | 1662 int net_error = |
1667 &error_info); | 1663 MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
1668 | 1664 |
1669 if (net_error != ERR_IO_PENDING) { | 1665 if (net_error != ERR_IO_PENDING) { |
1670 net_log_.AddEvent( | 1666 net_log_.AddEvent( |
1671 NetLog::TYPE_SSL_WRITE_ERROR, | 1667 NetLog::TYPE_SSL_WRITE_ERROR, |
1672 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); | 1668 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
1673 } | 1669 } |
1674 return net_error; | 1670 return net_error; |
1675 } | 1671 } |
1676 | 1672 |
1677 void SSLClientSocketOpenSSL::PumpReadWriteEvents() { | 1673 void SSLClientSocketImpl::PumpReadWriteEvents() { |
1678 int rv_read = ERR_IO_PENDING; | 1674 int rv_read = ERR_IO_PENDING; |
1679 int rv_write = ERR_IO_PENDING; | 1675 int rv_write = ERR_IO_PENDING; |
1680 bool network_moved; | 1676 bool network_moved; |
1681 do { | 1677 do { |
1682 if (user_read_buf_.get()) | 1678 if (user_read_buf_.get()) |
1683 rv_read = DoPayloadRead(); | 1679 rv_read = DoPayloadRead(); |
1684 if (user_write_buf_.get()) | 1680 if (user_write_buf_.get()) |
1685 rv_write = DoPayloadWrite(); | 1681 rv_write = DoPayloadWrite(); |
1686 network_moved = DoTransportIO(); | 1682 network_moved = DoTransportIO(); |
1687 } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && | 1683 } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && |
1688 (user_read_buf_.get() || user_write_buf_.get()) && network_moved); | 1684 (user_read_buf_.get() || user_write_buf_.get()) && network_moved); |
1689 | 1685 |
1690 // Performing the Read callback may cause |this| to be deleted. If this | 1686 // Performing the Read callback may cause |this| to be deleted. If this |
1691 // happens, the Write callback should not be invoked. Guard against this by | 1687 // happens, the Write callback should not be invoked. Guard against this by |
1692 // holding a WeakPtr to |this| and ensuring it's still valid. | 1688 // holding a WeakPtr to |this| and ensuring it's still valid. |
1693 base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); | 1689 base::WeakPtr<SSLClientSocketImpl> guard(weak_factory_.GetWeakPtr()); |
1694 if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) | 1690 if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) |
1695 DoReadCallback(rv_read); | 1691 DoReadCallback(rv_read); |
1696 | 1692 |
1697 if (!guard.get()) | 1693 if (!guard.get()) |
1698 return; | 1694 return; |
1699 | 1695 |
1700 if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) | 1696 if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) |
1701 DoWriteCallback(rv_write); | 1697 DoWriteCallback(rv_write); |
1702 } | 1698 } |
1703 | 1699 |
1704 int SSLClientSocketOpenSSL::BufferSend(void) { | 1700 int SSLClientSocketImpl::BufferSend(void) { |
1705 if (transport_send_busy_) | 1701 if (transport_send_busy_) |
1706 return ERR_IO_PENDING; | 1702 return ERR_IO_PENDING; |
1707 | 1703 |
1708 size_t buffer_read_offset; | 1704 size_t buffer_read_offset; |
1709 uint8_t* read_buf; | 1705 uint8_t* read_buf; |
1710 size_t max_read; | 1706 size_t max_read; |
1711 int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, | 1707 int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, |
1712 &buffer_read_offset, &max_read); | 1708 &buffer_read_offset, &max_read); |
1713 DCHECK_EQ(status, 1); // Should never fail. | 1709 DCHECK_EQ(status, 1); // Should never fail. |
1714 if (!max_read) | 1710 if (!max_read) |
1715 return 0; // Nothing pending in the OpenSSL write BIO. | 1711 return 0; // Nothing pending in the OpenSSL write BIO. |
1716 CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer())); | 1712 CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer())); |
1717 CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity())); | 1713 CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity())); |
1718 send_buffer_->set_offset(buffer_read_offset); | 1714 send_buffer_->set_offset(buffer_read_offset); |
1719 | 1715 |
1720 int rv = transport_->socket()->Write( | 1716 int rv = transport_->socket()->Write( |
1721 send_buffer_.get(), max_read, | 1717 send_buffer_.get(), max_read, |
1722 base::Bind(&SSLClientSocketOpenSSL::BufferSendComplete, | 1718 base::Bind(&SSLClientSocketImpl::BufferSendComplete, |
1723 base::Unretained(this))); | 1719 base::Unretained(this))); |
1724 if (rv == ERR_IO_PENDING) { | 1720 if (rv == ERR_IO_PENDING) { |
1725 transport_send_busy_ = true; | 1721 transport_send_busy_ = true; |
1726 } else { | 1722 } else { |
1727 TransportWriteComplete(rv); | 1723 TransportWriteComplete(rv); |
1728 } | 1724 } |
1729 return rv; | 1725 return rv; |
1730 } | 1726 } |
1731 | 1727 |
1732 int SSLClientSocketOpenSSL::BufferRecv(void) { | 1728 int SSLClientSocketImpl::BufferRecv(void) { |
1733 if (transport_recv_busy_) | 1729 if (transport_recv_busy_) |
1734 return ERR_IO_PENDING; | 1730 return ERR_IO_PENDING; |
1735 | 1731 |
1736 // Determine how much was requested from |transport_bio_| that was not | 1732 // Determine how much was requested from |transport_bio_| that was not |
1737 // actually available. | 1733 // actually available. |
1738 size_t requested = BIO_ctrl_get_read_request(transport_bio_); | 1734 size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
1739 if (requested == 0) { | 1735 if (requested == 0) { |
1740 // This is not a perfect match of error codes, as no operation is | 1736 // This is not a perfect match of error codes, as no operation is |
1741 // actually pending. However, returning 0 would be interpreted as | 1737 // actually pending. However, returning 0 would be interpreted as |
1742 // a possible sign of EOF, which is also an inappropriate match. | 1738 // a possible sign of EOF, which is also an inappropriate match. |
(...skipping 17 matching lines...) Expand all Loading... |
1760 DCHECK_EQ(status, 1); // Should never fail. | 1756 DCHECK_EQ(status, 1); // Should never fail. |
1761 if (!max_write) | 1757 if (!max_write) |
1762 return ERR_IO_PENDING; | 1758 return ERR_IO_PENDING; |
1763 | 1759 |
1764 CHECK_EQ(write_buf, | 1760 CHECK_EQ(write_buf, |
1765 reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer())); | 1761 reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer())); |
1766 CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity())); | 1762 CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity())); |
1767 | 1763 |
1768 recv_buffer_->set_offset(buffer_write_offset); | 1764 recv_buffer_->set_offset(buffer_write_offset); |
1769 int rv = transport_->socket()->Read( | 1765 int rv = transport_->socket()->Read( |
1770 recv_buffer_.get(), | 1766 recv_buffer_.get(), max_write, |
1771 max_write, | 1767 base::Bind(&SSLClientSocketImpl::BufferRecvComplete, |
1772 base::Bind(&SSLClientSocketOpenSSL::BufferRecvComplete, | |
1773 base::Unretained(this))); | 1768 base::Unretained(this))); |
1774 if (rv == ERR_IO_PENDING) { | 1769 if (rv == ERR_IO_PENDING) { |
1775 transport_recv_busy_ = true; | 1770 transport_recv_busy_ = true; |
1776 } else { | 1771 } else { |
1777 rv = TransportReadComplete(rv); | 1772 rv = TransportReadComplete(rv); |
1778 } | 1773 } |
1779 return rv; | 1774 return rv; |
1780 } | 1775 } |
1781 | 1776 |
1782 void SSLClientSocketOpenSSL::BufferSendComplete(int result) { | 1777 void SSLClientSocketImpl::BufferSendComplete(int result) { |
1783 TransportWriteComplete(result); | 1778 TransportWriteComplete(result); |
1784 OnSendComplete(result); | 1779 OnSendComplete(result); |
1785 } | 1780 } |
1786 | 1781 |
1787 void SSLClientSocketOpenSSL::BufferRecvComplete(int result) { | 1782 void SSLClientSocketImpl::BufferRecvComplete(int result) { |
1788 result = TransportReadComplete(result); | 1783 result = TransportReadComplete(result); |
1789 OnRecvComplete(result); | 1784 OnRecvComplete(result); |
1790 } | 1785 } |
1791 | 1786 |
1792 void SSLClientSocketOpenSSL::TransportWriteComplete(int result) { | 1787 void SSLClientSocketImpl::TransportWriteComplete(int result) { |
1793 DCHECK(ERR_IO_PENDING != result); | 1788 DCHECK(ERR_IO_PENDING != result); |
1794 int bytes_written = 0; | 1789 int bytes_written = 0; |
1795 if (result < 0) { | 1790 if (result < 0) { |
1796 // Record the error. Save it to be reported in a future read or write on | 1791 // Record the error. Save it to be reported in a future read or write on |
1797 // transport_bio_'s peer. | 1792 // transport_bio_'s peer. |
1798 transport_write_error_ = result; | 1793 transport_write_error_ = result; |
1799 } else { | 1794 } else { |
1800 bytes_written = result; | 1795 bytes_written = result; |
1801 } | 1796 } |
1802 DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written); | 1797 DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written); |
1803 int ret = BIO_zero_copy_get_read_buf_done(transport_bio_, bytes_written); | 1798 int ret = BIO_zero_copy_get_read_buf_done(transport_bio_, bytes_written); |
1804 DCHECK_EQ(1, ret); | 1799 DCHECK_EQ(1, ret); |
1805 transport_send_busy_ = false; | 1800 transport_send_busy_ = false; |
1806 } | 1801 } |
1807 | 1802 |
1808 int SSLClientSocketOpenSSL::TransportReadComplete(int result) { | 1803 int SSLClientSocketImpl::TransportReadComplete(int result) { |
1809 DCHECK(ERR_IO_PENDING != result); | 1804 DCHECK(ERR_IO_PENDING != result); |
1810 // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError | 1805 // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError |
1811 // does not report success. | 1806 // does not report success. |
1812 if (result == 0) | 1807 if (result == 0) |
1813 result = ERR_CONNECTION_CLOSED; | 1808 result = ERR_CONNECTION_CLOSED; |
1814 int bytes_read = 0; | 1809 int bytes_read = 0; |
1815 if (result < 0) { | 1810 if (result < 0) { |
1816 DVLOG(1) << "TransportReadComplete result " << result; | 1811 DVLOG(1) << "TransportReadComplete result " << result; |
1817 // Received an error. Save it to be reported in a future read on | 1812 // Received an error. Save it to be reported in a future read on |
1818 // transport_bio_'s peer. | 1813 // transport_bio_'s peer. |
1819 transport_read_error_ = result; | 1814 transport_read_error_ = result; |
1820 } else { | 1815 } else { |
1821 bytes_read = result; | 1816 bytes_read = result; |
1822 } | 1817 } |
1823 DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read); | 1818 DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read); |
1824 int ret = BIO_zero_copy_get_write_buf_done(transport_bio_, bytes_read); | 1819 int ret = BIO_zero_copy_get_write_buf_done(transport_bio_, bytes_read); |
1825 DCHECK_EQ(1, ret); | 1820 DCHECK_EQ(1, ret); |
1826 transport_recv_busy_ = false; | 1821 transport_recv_busy_ = false; |
1827 return result; | 1822 return result; |
1828 } | 1823 } |
1829 | 1824 |
1830 int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl) { | 1825 int SSLClientSocketImpl::ClientCertRequestCallback(SSL* ssl) { |
1831 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; | 1826 DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
1832 DCHECK(ssl == ssl_); | 1827 DCHECK(ssl == ssl_); |
1833 | 1828 |
1834 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); | 1829 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); |
1835 | 1830 |
1836 // Clear any currently configured certificates. | 1831 // Clear any currently configured certificates. |
1837 SSL_certs_clear(ssl_); | 1832 SSL_certs_clear(ssl_); |
1838 | 1833 |
1839 #if defined(OS_IOS) | 1834 #if defined(OS_IOS) |
1840 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). | 1835 // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
1841 LOG(WARNING) << "Client auth is not supported"; | 1836 LOG(WARNING) << "Client auth is not supported"; |
1842 #else // !defined(OS_IOS) | 1837 #else // !defined(OS_IOS) |
1843 if (!ssl_config_.send_client_cert) { | 1838 if (!ssl_config_.send_client_cert) { |
1844 // First pass: we know that a client certificate is needed, but we do not | 1839 // First pass: we know that a client certificate is needed, but we do not |
1845 // have one at hand. | 1840 // have one at hand. |
1846 STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); | 1841 STACK_OF(X509_NAME)* authorities = SSL_get_client_CA_list(ssl); |
1847 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { | 1842 for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
1848 X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); | 1843 X509_NAME* ca_name = (X509_NAME*)sk_X509_NAME_value(authorities, i); |
1849 unsigned char* str = NULL; | 1844 unsigned char* str = NULL; |
1850 int length = i2d_X509_NAME(ca_name, &str); | 1845 int length = i2d_X509_NAME(ca_name, &str); |
1851 cert_authorities_.push_back(std::string( | 1846 cert_authorities_.push_back(std::string( |
1852 reinterpret_cast<const char*>(str), | 1847 reinterpret_cast<const char*>(str), static_cast<size_t>(length))); |
1853 static_cast<size_t>(length))); | |
1854 OPENSSL_free(str); | 1848 OPENSSL_free(str); |
1855 } | 1849 } |
1856 | 1850 |
1857 const unsigned char* client_cert_types; | 1851 const unsigned char* client_cert_types; |
1858 size_t num_client_cert_types = | 1852 size_t num_client_cert_types = |
1859 SSL_get0_certificate_types(ssl, &client_cert_types); | 1853 SSL_get0_certificate_types(ssl, &client_cert_types); |
1860 for (size_t i = 0; i < num_client_cert_types; i++) { | 1854 for (size_t i = 0; i < num_client_cert_types; i++) { |
1861 cert_key_types_.push_back( | 1855 cert_key_types_.push_back( |
1862 static_cast<SSLClientCertType>(client_cert_types[i])); | 1856 static_cast<SSLClientCertType>(client_cert_types[i])); |
1863 } | 1857 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1933 return 1; | 1927 return 1; |
1934 } | 1928 } |
1935 #endif // defined(OS_IOS) | 1929 #endif // defined(OS_IOS) |
1936 | 1930 |
1937 // Send no client certificate. | 1931 // Send no client certificate. |
1938 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, | 1932 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
1939 NetLog::IntCallback("cert_count", 0)); | 1933 NetLog::IntCallback("cert_count", 0)); |
1940 return 1; | 1934 return 1; |
1941 } | 1935 } |
1942 | 1936 |
1943 int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { | 1937 int SSLClientSocketImpl::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
1944 if (!completed_connect_) { | 1938 if (!completed_connect_) { |
1945 // If the first handshake hasn't completed then we accept any certificates | 1939 // If the first handshake hasn't completed then we accept any certificates |
1946 // because we verify after the handshake. | 1940 // because we verify after the handshake. |
1947 return 1; | 1941 return 1; |
1948 } | 1942 } |
1949 | 1943 |
1950 // Disallow the server certificate to change in a renegotiation. | 1944 // Disallow the server certificate to change in a renegotiation. |
1951 if (server_cert_chain_->empty()) { | 1945 if (server_cert_chain_->empty()) { |
1952 LOG(ERROR) << "Received invalid certificate chain between handshakes"; | 1946 LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
1953 return 0; | 1947 return 0; |
(...skipping 10 matching lines...) Expand all Loading... |
1964 return 0; | 1958 return 0; |
1965 } | 1959 } |
1966 | 1960 |
1967 return 1; | 1961 return 1; |
1968 } | 1962 } |
1969 | 1963 |
1970 // SelectNextProtoCallback is called by OpenSSL during the handshake. If the | 1964 // SelectNextProtoCallback is called by OpenSSL during the handshake. If the |
1971 // server supports NPN, selects a protocol from the list that the server | 1965 // server supports NPN, selects a protocol from the list that the server |
1972 // provides. According to third_party/boringssl/src/ssl/ssl_lib.c, the | 1966 // provides. According to third_party/boringssl/src/ssl/ssl_lib.c, the |
1973 // callback can assume that |in| is syntactically valid. | 1967 // callback can assume that |in| is syntactically valid. |
1974 int SSLClientSocketOpenSSL::SelectNextProtoCallback(unsigned char** out, | 1968 int SSLClientSocketImpl::SelectNextProtoCallback(unsigned char** out, |
1975 unsigned char* outlen, | 1969 unsigned char* outlen, |
1976 const unsigned char* in, | 1970 const unsigned char* in, |
1977 unsigned int inlen) { | 1971 unsigned int inlen) { |
1978 if (ssl_config_.npn_protos.empty()) { | 1972 if (ssl_config_.npn_protos.empty()) { |
1979 *out = reinterpret_cast<uint8_t*>( | 1973 *out = reinterpret_cast<uint8_t*>( |
1980 const_cast<char*>(kDefaultSupportedNPNProtocol)); | 1974 const_cast<char*>(kDefaultSupportedNPNProtocol)); |
1981 *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; | 1975 *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; |
1982 npn_status_ = kNextProtoUnsupported; | 1976 npn_status_ = kNextProtoUnsupported; |
1983 return SSL_TLSEXT_ERR_OK; | 1977 return SSL_TLSEXT_ERR_OK; |
1984 } | 1978 } |
1985 | 1979 |
1986 // Assume there's no overlap between our protocols and the server's list. | 1980 // Assume there's no overlap between our protocols and the server's list. |
1987 npn_status_ = kNextProtoNoOverlap; | 1981 npn_status_ = kNextProtoNoOverlap; |
(...skipping 22 matching lines...) Expand all Loading... |
2010 *out = reinterpret_cast<unsigned char*>(const_cast<char*>(proto)); | 2004 *out = reinterpret_cast<unsigned char*>(const_cast<char*>(proto)); |
2011 *outlen = strlen(proto); | 2005 *outlen = strlen(proto); |
2012 } | 2006 } |
2013 | 2007 |
2014 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); | 2008 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
2015 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 2009 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
2016 set_negotiation_extension(kExtensionNPN); | 2010 set_negotiation_extension(kExtensionNPN); |
2017 return SSL_TLSEXT_ERR_OK; | 2011 return SSL_TLSEXT_ERR_OK; |
2018 } | 2012 } |
2019 | 2013 |
2020 long SSLClientSocketOpenSSL::MaybeReplayTransportError( | 2014 long SSLClientSocketImpl::MaybeReplayTransportError(BIO* bio, |
2021 BIO *bio, | 2015 int cmd, |
2022 int cmd, | 2016 const char* argp, |
2023 const char *argp, int argi, long argl, | 2017 int argi, |
2024 long retvalue) { | 2018 long argl, |
2025 if (cmd == (BIO_CB_READ|BIO_CB_RETURN) && retvalue <= 0) { | 2019 long retvalue) { |
| 2020 if (cmd == (BIO_CB_READ | BIO_CB_RETURN) && retvalue <= 0) { |
2026 // If there is no more data in the buffer, report any pending errors that | 2021 // If there is no more data in the buffer, report any pending errors that |
2027 // were observed. Note that both the readbuf and the writebuf are checked | 2022 // were observed. Note that both the readbuf and the writebuf are checked |
2028 // for errors, since the application may have encountered a socket error | 2023 // for errors, since the application may have encountered a socket error |
2029 // while writing that would otherwise not be reported until the application | 2024 // while writing that would otherwise not be reported until the application |
2030 // attempted to write again - which it may never do. See | 2025 // attempted to write again - which it may never do. See |
2031 // https://crbug.com/249848. | 2026 // https://crbug.com/249848. |
2032 if (transport_read_error_ != OK) { | 2027 if (transport_read_error_ != OK) { |
2033 OpenSSLPutNetError(FROM_HERE, transport_read_error_); | 2028 OpenSSLPutNetError(FROM_HERE, transport_read_error_); |
2034 return -1; | 2029 return -1; |
2035 } | 2030 } |
2036 if (transport_write_error_ != OK) { | 2031 if (transport_write_error_ != OK) { |
2037 OpenSSLPutNetError(FROM_HERE, transport_write_error_); | 2032 OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
2038 return -1; | 2033 return -1; |
2039 } | 2034 } |
2040 } else if (cmd == BIO_CB_WRITE) { | 2035 } else if (cmd == BIO_CB_WRITE) { |
2041 // Because of the write buffer, this reports a failure from the previous | 2036 // Because of the write buffer, this reports a failure from the previous |
2042 // write payload. If the current payload fails to write, the error will be | 2037 // write payload. If the current payload fails to write, the error will be |
2043 // reported in a future write or read to |bio|. | 2038 // reported in a future write or read to |bio|. |
2044 if (transport_write_error_ != OK) { | 2039 if (transport_write_error_ != OK) { |
2045 OpenSSLPutNetError(FROM_HERE, transport_write_error_); | 2040 OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
2046 return -1; | 2041 return -1; |
2047 } | 2042 } |
2048 } | 2043 } |
2049 return retvalue; | 2044 return retvalue; |
2050 } | 2045 } |
2051 | 2046 |
2052 // static | 2047 // static |
2053 long SSLClientSocketOpenSSL::BIOCallback( | 2048 long SSLClientSocketImpl::BIOCallback(BIO* bio, |
2054 BIO *bio, | 2049 int cmd, |
2055 int cmd, | 2050 const char* argp, |
2056 const char *argp, int argi, long argl, | 2051 int argi, |
2057 long retvalue) { | 2052 long argl, |
2058 SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( | 2053 long retvalue) { |
2059 BIO_get_callback_arg(bio)); | 2054 SSLClientSocketImpl* socket = |
| 2055 reinterpret_cast<SSLClientSocketImpl*>(BIO_get_callback_arg(bio)); |
2060 CHECK(socket); | 2056 CHECK(socket); |
2061 return socket->MaybeReplayTransportError( | 2057 return socket->MaybeReplayTransportError(bio, cmd, argp, argi, argl, |
2062 bio, cmd, argp, argi, argl, retvalue); | 2058 retvalue); |
2063 } | 2059 } |
2064 | 2060 |
2065 void SSLClientSocketOpenSSL::MaybeCacheSession() { | 2061 void SSLClientSocketImpl::MaybeCacheSession() { |
2066 // Only cache the session once both a new session has been established and the | 2062 // Only cache the session once both a new session has been established and the |
2067 // certificate has been verified. Due to False Start, these events may happen | 2063 // certificate has been verified. Due to False Start, these events may happen |
2068 // in either order. | 2064 // in either order. |
2069 if (!session_pending_ || !certificate_verified_) | 2065 if (!session_pending_ || !certificate_verified_) |
2070 return; | 2066 return; |
2071 | 2067 |
2072 SSLContext::GetInstance()->session_cache()->Insert(GetSessionCacheKey(), | 2068 SSLContext::GetInstance()->session_cache()->Insert(GetSessionCacheKey(), |
2073 SSL_get_session(ssl_)); | 2069 SSL_get_session(ssl_)); |
2074 session_pending_ = false; | 2070 session_pending_ = false; |
2075 } | 2071 } |
2076 | 2072 |
2077 int SSLClientSocketOpenSSL::NewSessionCallback(SSL_SESSION* session) { | 2073 int SSLClientSocketImpl::NewSessionCallback(SSL_SESSION* session) { |
2078 DCHECK_EQ(session, SSL_get_session(ssl_)); | 2074 DCHECK_EQ(session, SSL_get_session(ssl_)); |
2079 | 2075 |
2080 // Only sessions from the initial handshake get cached. Note this callback may | 2076 // Only sessions from the initial handshake get cached. Note this callback may |
2081 // be signaled on abbreviated handshakes if the ticket was renewed. | 2077 // be signaled on abbreviated handshakes if the ticket was renewed. |
2082 session_pending_ = true; | 2078 session_pending_ = true; |
2083 MaybeCacheSession(); | 2079 MaybeCacheSession(); |
2084 | 2080 |
2085 // OpenSSL passes a reference to |session|, but the session cache does not | 2081 // OpenSSL passes a reference to |session|, but the session cache does not |
2086 // take this reference, so release it. | 2082 // take this reference, so release it. |
2087 SSL_SESSION_free(session); | 2083 SSL_SESSION_free(session); |
2088 return 1; | 2084 return 1; |
2089 } | 2085 } |
2090 | 2086 |
2091 void SSLClientSocketOpenSSL::AddCTInfoToSSLInfo(SSLInfo* ssl_info) const { | 2087 void SSLClientSocketImpl::AddCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
2092 ssl_info->UpdateCertificateTransparencyInfo(ct_verify_result_); | 2088 ssl_info->UpdateCertificateTransparencyInfo(ct_verify_result_); |
2093 } | 2089 } |
2094 | 2090 |
2095 std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const { | 2091 std::string SSLClientSocketImpl::GetSessionCacheKey() const { |
2096 std::string result = host_and_port_.ToString(); | 2092 std::string result = host_and_port_.ToString(); |
2097 result.append("/"); | 2093 result.append("/"); |
2098 result.append(ssl_session_cache_shard_); | 2094 result.append(ssl_session_cache_shard_); |
2099 | 2095 |
2100 // Shard the session cache based on maximum protocol version. This causes | 2096 // Shard the session cache based on maximum protocol version. This causes |
2101 // fallback connections to use a separate session cache. | 2097 // fallback connections to use a separate session cache. |
2102 result.append("/"); | 2098 result.append("/"); |
2103 switch (ssl_config_.version_max) { | 2099 switch (ssl_config_.version_max) { |
2104 case SSL_PROTOCOL_VERSION_TLS1: | 2100 case SSL_PROTOCOL_VERSION_TLS1: |
2105 result.append("tls1"); | 2101 result.append("tls1"); |
(...skipping 12 matching lines...) Expand all Loading... |
2118 if (ssl_config_.deprecated_cipher_suites_enabled) | 2114 if (ssl_config_.deprecated_cipher_suites_enabled) |
2119 result.append("deprecated"); | 2115 result.append("deprecated"); |
2120 | 2116 |
2121 result.append("/"); | 2117 result.append("/"); |
2122 if (ssl_config_.channel_id_enabled) | 2118 if (ssl_config_.channel_id_enabled) |
2123 result.append("channelid"); | 2119 result.append("channelid"); |
2124 | 2120 |
2125 return result; | 2121 return result; |
2126 } | 2122 } |
2127 | 2123 |
2128 bool SSLClientSocketOpenSSL::IsRenegotiationAllowed() const { | 2124 bool SSLClientSocketImpl::IsRenegotiationAllowed() const { |
2129 if (tb_was_negotiated_) | 2125 if (tb_was_negotiated_) |
2130 return false; | 2126 return false; |
2131 | 2127 |
2132 if (npn_status_ == kNextProtoUnsupported) | 2128 if (npn_status_ == kNextProtoUnsupported) |
2133 return ssl_config_.renego_allowed_default; | 2129 return ssl_config_.renego_allowed_default; |
2134 | 2130 |
2135 NextProto next_proto = NextProtoFromString(npn_proto_); | 2131 NextProto next_proto = NextProtoFromString(npn_proto_); |
2136 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { | 2132 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
2137 if (next_proto == allowed) | 2133 if (next_proto == allowed) |
2138 return true; | 2134 return true; |
2139 } | 2135 } |
2140 return false; | 2136 return false; |
2141 } | 2137 } |
2142 | 2138 |
2143 int SSLClientSocketOpenSSL::PrivateKeyTypeCallback() { | 2139 int SSLClientSocketImpl::PrivateKeyTypeCallback() { |
2144 switch (ssl_config_.client_private_key->GetType()) { | 2140 switch (ssl_config_.client_private_key->GetType()) { |
2145 case SSLPrivateKey::Type::RSA: | 2141 case SSLPrivateKey::Type::RSA: |
2146 return EVP_PKEY_RSA; | 2142 return EVP_PKEY_RSA; |
2147 case SSLPrivateKey::Type::ECDSA: | 2143 case SSLPrivateKey::Type::ECDSA: |
2148 return EVP_PKEY_EC; | 2144 return EVP_PKEY_EC; |
2149 } | 2145 } |
2150 NOTREACHED(); | 2146 NOTREACHED(); |
2151 return EVP_PKEY_NONE; | 2147 return EVP_PKEY_NONE; |
2152 } | 2148 } |
2153 | 2149 |
2154 size_t SSLClientSocketOpenSSL::PrivateKeyMaxSignatureLenCallback() { | 2150 size_t SSLClientSocketImpl::PrivateKeyMaxSignatureLenCallback() { |
2155 return ssl_config_.client_private_key->GetMaxSignatureLengthInBytes(); | 2151 return ssl_config_.client_private_key->GetMaxSignatureLengthInBytes(); |
2156 } | 2152 } |
2157 | 2153 |
2158 ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCallback( | 2154 ssl_private_key_result_t SSLClientSocketImpl::PrivateKeySignCallback( |
2159 uint8_t* out, | 2155 uint8_t* out, |
2160 size_t* out_len, | 2156 size_t* out_len, |
2161 size_t max_out, | 2157 size_t max_out, |
2162 const EVP_MD* md, | 2158 const EVP_MD* md, |
2163 const uint8_t* in, | 2159 const uint8_t* in, |
2164 size_t in_len) { | 2160 size_t in_len) { |
2165 DCHECK_EQ(kNoPendingResult, signature_result_); | 2161 DCHECK_EQ(kNoPendingResult, signature_result_); |
2166 DCHECK(signature_.empty()); | 2162 DCHECK(signature_.empty()); |
2167 DCHECK(ssl_config_.client_private_key); | 2163 DCHECK(ssl_config_.client_private_key); |
2168 | 2164 |
2169 SSLPrivateKey::Hash hash; | 2165 SSLPrivateKey::Hash hash; |
2170 if (!EVP_MDToPrivateKeyHash(md, &hash)) { | 2166 if (!EVP_MDToPrivateKeyHash(md, &hash)) { |
2171 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); | 2167 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
2172 return ssl_private_key_failure; | 2168 return ssl_private_key_failure; |
2173 } | 2169 } |
2174 | 2170 |
2175 net_log_.BeginEvent( | 2171 net_log_.BeginEvent( |
2176 NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, | 2172 NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, |
2177 base::Bind(&NetLogPrivateKeyOperationCallback, | 2173 base::Bind(&NetLogPrivateKeyOperationCallback, |
2178 ssl_config_.client_private_key->GetType(), hash)); | 2174 ssl_config_.client_private_key->GetType(), hash)); |
2179 | 2175 |
2180 signature_result_ = ERR_IO_PENDING; | 2176 signature_result_ = ERR_IO_PENDING; |
2181 ssl_config_.client_private_key->SignDigest( | 2177 ssl_config_.client_private_key->SignDigest( |
2182 hash, base::StringPiece(reinterpret_cast<const char*>(in), in_len), | 2178 hash, base::StringPiece(reinterpret_cast<const char*>(in), in_len), |
2183 base::Bind(&SSLClientSocketOpenSSL::OnPrivateKeySignComplete, | 2179 base::Bind(&SSLClientSocketImpl::OnPrivateKeySignComplete, |
2184 weak_factory_.GetWeakPtr())); | 2180 weak_factory_.GetWeakPtr())); |
2185 return ssl_private_key_retry; | 2181 return ssl_private_key_retry; |
2186 } | 2182 } |
2187 | 2183 |
2188 ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCompleteCallback( | 2184 ssl_private_key_result_t SSLClientSocketImpl::PrivateKeySignCompleteCallback( |
2189 uint8_t* out, | 2185 uint8_t* out, |
2190 size_t* out_len, | 2186 size_t* out_len, |
2191 size_t max_out) { | 2187 size_t max_out) { |
2192 DCHECK_NE(kNoPendingResult, signature_result_); | 2188 DCHECK_NE(kNoPendingResult, signature_result_); |
2193 DCHECK(ssl_config_.client_private_key); | 2189 DCHECK(ssl_config_.client_private_key); |
2194 | 2190 |
2195 if (signature_result_ == ERR_IO_PENDING) | 2191 if (signature_result_ == ERR_IO_PENDING) |
2196 return ssl_private_key_retry; | 2192 return ssl_private_key_retry; |
2197 if (signature_result_ != OK) { | 2193 if (signature_result_ != OK) { |
2198 OpenSSLPutNetError(FROM_HERE, signature_result_); | 2194 OpenSSLPutNetError(FROM_HERE, signature_result_); |
2199 return ssl_private_key_failure; | 2195 return ssl_private_key_failure; |
2200 } | 2196 } |
2201 if (signature_.size() > max_out) { | 2197 if (signature_.size() > max_out) { |
2202 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); | 2198 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
2203 return ssl_private_key_failure; | 2199 return ssl_private_key_failure; |
2204 } | 2200 } |
2205 memcpy(out, signature_.data(), signature_.size()); | 2201 memcpy(out, signature_.data(), signature_.size()); |
2206 *out_len = signature_.size(); | 2202 *out_len = signature_.size(); |
2207 signature_.clear(); | 2203 signature_.clear(); |
2208 return ssl_private_key_success; | 2204 return ssl_private_key_success; |
2209 } | 2205 } |
2210 | 2206 |
2211 void SSLClientSocketOpenSSL::OnPrivateKeySignComplete( | 2207 void SSLClientSocketImpl::OnPrivateKeySignComplete( |
2212 Error error, | 2208 Error error, |
2213 const std::vector<uint8_t>& signature) { | 2209 const std::vector<uint8_t>& signature) { |
2214 DCHECK_EQ(ERR_IO_PENDING, signature_result_); | 2210 DCHECK_EQ(ERR_IO_PENDING, signature_result_); |
2215 DCHECK(signature_.empty()); | 2211 DCHECK(signature_.empty()); |
2216 DCHECK(ssl_config_.client_private_key); | 2212 DCHECK(ssl_config_.client_private_key); |
2217 | 2213 |
2218 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, | 2214 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, |
2219 error); | 2215 error); |
2220 | 2216 |
2221 signature_result_ = error; | 2217 signature_result_ = error; |
2222 if (signature_result_ == OK) | 2218 if (signature_result_ == OK) |
2223 signature_ = signature; | 2219 signature_ = signature; |
2224 | 2220 |
2225 if (next_handshake_state_ == STATE_HANDSHAKE) { | 2221 if (next_handshake_state_ == STATE_HANDSHAKE) { |
2226 OnHandshakeIOComplete(signature_result_); | 2222 OnHandshakeIOComplete(signature_result_); |
2227 return; | 2223 return; |
2228 } | 2224 } |
2229 | 2225 |
2230 // During a renegotiation, either Read or Write calls may be blocked on an | 2226 // During a renegotiation, either Read or Write calls may be blocked on an |
2231 // asynchronous private key operation. | 2227 // asynchronous private key operation. |
2232 PumpReadWriteEvents(); | 2228 PumpReadWriteEvents(); |
2233 } | 2229 } |
2234 | 2230 |
2235 int SSLClientSocketOpenSSL::TokenBindingAdd(const uint8_t** out, | 2231 int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out, |
2236 size_t* out_len, | 2232 size_t* out_len, |
2237 int* out_alert_value) { | 2233 int* out_alert_value) { |
2238 if (ssl_config_.token_binding_params.empty()) { | 2234 if (ssl_config_.token_binding_params.empty()) { |
2239 return 0; | 2235 return 0; |
2240 } | 2236 } |
2241 crypto::AutoCBB output; | 2237 crypto::AutoCBB output; |
2242 CBB parameters_list; | 2238 CBB parameters_list; |
2243 if (!CBB_init(output.get(), 7) || | 2239 if (!CBB_init(output.get(), 7) || |
2244 !CBB_add_u8(output.get(), kTbProtocolVersionMajor) || | 2240 !CBB_add_u8(output.get(), kTbProtocolVersionMajor) || |
2245 !CBB_add_u8(output.get(), kTbProtocolVersionMinor) || | 2241 !CBB_add_u8(output.get(), kTbProtocolVersionMinor) || |
2246 !CBB_add_u8_length_prefixed(output.get(), ¶meters_list)) { | 2242 !CBB_add_u8_length_prefixed(output.get(), ¶meters_list)) { |
2247 *out_alert_value = SSL_AD_INTERNAL_ERROR; | 2243 *out_alert_value = SSL_AD_INTERNAL_ERROR; |
2248 return -1; | 2244 return -1; |
2249 } | 2245 } |
2250 for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { | 2246 for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { |
2251 if (!CBB_add_u8(¶meters_list, ssl_config_.token_binding_params[i])) { | 2247 if (!CBB_add_u8(¶meters_list, ssl_config_.token_binding_params[i])) { |
2252 *out_alert_value = SSL_AD_INTERNAL_ERROR; | 2248 *out_alert_value = SSL_AD_INTERNAL_ERROR; |
2253 return -1; | 2249 return -1; |
2254 } | 2250 } |
2255 } | 2251 } |
2256 // |*out| will be freed by TokenBindingFreeCallback. | 2252 // |*out| will be freed by TokenBindingFreeCallback. |
2257 if (!CBB_finish(output.get(), const_cast<uint8_t**>(out), out_len)) { | 2253 if (!CBB_finish(output.get(), const_cast<uint8_t**>(out), out_len)) { |
2258 *out_alert_value = SSL_AD_INTERNAL_ERROR; | 2254 *out_alert_value = SSL_AD_INTERNAL_ERROR; |
2259 return -1; | 2255 return -1; |
2260 } | 2256 } |
2261 | 2257 |
2262 return 1; | 2258 return 1; |
2263 } | 2259 } |
2264 | 2260 |
2265 int SSLClientSocketOpenSSL::TokenBindingParse(const uint8_t* contents, | 2261 int SSLClientSocketImpl::TokenBindingParse(const uint8_t* contents, |
2266 size_t contents_len, | 2262 size_t contents_len, |
2267 int* out_alert_value) { | 2263 int* out_alert_value) { |
2268 if (completed_connect_) { | 2264 if (completed_connect_) { |
2269 // Token Binding may only be negotiated on the initial handshake. | 2265 // Token Binding may only be negotiated on the initial handshake. |
2270 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; | 2266 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
2271 return 0; | 2267 return 0; |
2272 } | 2268 } |
2273 | 2269 |
2274 CBS extension; | 2270 CBS extension; |
2275 CBS_init(&extension, contents, contents_len); | 2271 CBS_init(&extension, contents, contents_len); |
2276 | 2272 |
2277 CBS parameters_list; | 2273 CBS parameters_list; |
(...skipping 27 matching lines...) Expand all Loading... |
2305 tb_was_negotiated_ = true; | 2301 tb_was_negotiated_ = true; |
2306 return 1; | 2302 return 1; |
2307 } | 2303 } |
2308 } | 2304 } |
2309 | 2305 |
2310 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; | 2306 *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
2311 return 0; | 2307 return 0; |
2312 } | 2308 } |
2313 | 2309 |
2314 } // namespace net | 2310 } // namespace net |
OLD | NEW |