Chromium Code Reviews| 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 // OpenSSL binding for SSLClientSocket. The class layout and general principle |
| 6 // of operation is derived from SSLClientSocketNSS. | 6 // of operation is derived from SSLClientSocketNSS. |
| 7 | 7 |
| 8 #include "net/socket/ssl_client_socket_openssl.h" | 8 #include "net/socket/ssl_client_socket_openssl.h" |
| 9 | 9 |
| 10 #include <openssl/err.h> | 10 #include <openssl/err.h> |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 | 312 |
| 313 // This is the index used with SSL_get_ex_data to retrieve the owner | 313 // This is the index used with SSL_get_ex_data to retrieve the owner |
| 314 // SSLClientSocketOpenSSL object from an SSL instance. | 314 // SSLClientSocketOpenSSL object from an SSL instance. |
| 315 int ssl_socket_data_index_; | 315 int ssl_socket_data_index_; |
| 316 | 316 |
| 317 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; | 317 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_; |
| 318 // |session_cache_| must be destroyed before |ssl_ctx_|. | 318 // |session_cache_| must be destroyed before |ssl_ctx_|. |
| 319 SSLSessionCacheOpenSSL session_cache_; | 319 SSLSessionCacheOpenSSL session_cache_; |
| 320 }; | 320 }; |
| 321 | 321 |
| 322 // PeerCertificateChain is a helper object which extracts the certificate | |
| 323 // chain, as given by the server, from an OpenSSL socket and performs the needed | |
| 324 // resource management. The first element of the chain is the leaf certificate | |
| 325 // and the other elements are in the order given by the server. | |
| 326 class SSLClientSocketOpenSSL::PeerCertificateChain { | |
| 327 public: | |
| 328 explicit PeerCertificateChain(SSL* ssl) { Reset(ssl); } | |
| 329 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } | |
| 330 ~PeerCertificateChain() {} | |
| 331 PeerCertificateChain& operator=(const PeerCertificateChain& other); | |
| 332 void Reset(SSL* ssl); | |
|
Ryan Sleevi
2014/03/14 21:06:15
nit: newline after the operator=
Let's add a desc
haavardm
2014/03/17 09:21:55
Done.
| |
| 333 // Note that when USE_OPENSSL is defined, OSCertHandle is X509* | |
| 334 const scoped_refptr<X509Certificate>& AsOSChain() const { return os_chain_; } | |
| 335 | |
| 336 size_t size() const { | |
| 337 if (!openssl_chain_.get()) | |
| 338 return 0; | |
| 339 return sk_X509_num(openssl_chain_.get()); | |
| 340 } | |
| 341 | |
| 342 X509* operator[](size_t index) const { | |
| 343 DCHECK_LT(index, size()); | |
| 344 return sk_X509_value(openssl_chain_.get(), index); | |
| 345 } | |
| 346 | |
| 347 private: | |
| 348 static void FreeX509Stack(STACK_OF(X509) * chain) { | |
| 349 sk_X509_pop_free(chain, X509_free); | |
| 350 } | |
| 351 crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_; | |
|
Ryan Sleevi
2014/03/14 21:06:15
nit: newline between 350/351
haavardm
2014/03/17 09:21:55
Done.
| |
| 352 | |
| 353 scoped_refptr<X509Certificate> os_chain_; | |
| 354 }; | |
| 355 | |
| 356 SSLClientSocketOpenSSL::PeerCertificateChain& | |
| 357 SSLClientSocketOpenSSL::PeerCertificateChain::operator=( | |
| 358 const PeerCertificateChain& other) { | |
| 359 if (this == &other) | |
| 360 return *this; | |
| 361 | |
| 362 // os_chain_ is reference counted by scoped_refptr; | |
| 363 os_chain_ = other.os_chain_; | |
| 364 | |
| 365 // Must increase the reference count manually for sk_X509_dup | |
| 366 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); | |
| 367 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 368 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 369 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 370 } | |
| 371 return *this; | |
| 372 } | |
| 373 | |
| 374 #if defined(USE_OPENSSL) | |
| 375 // When OSCertHandle is typedef'ed to X509, we can do a short cut | |
| 376 // to avoid converting back and forth between der and X509 struct. | |
|
Ryan Sleevi
2014/03/14 21:06:15
comment nit: avoiding pronouns is good (see https:
haavardm
2014/03/17 09:21:55
Done.
| |
| 377 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset(SSL* ssl) { | |
| 378 openssl_chain_.reset(NULL); | |
| 379 os_chain_ = NULL; | |
| 380 | |
| 381 if (ssl == NULL) | |
| 382 return; | |
| 383 | |
| 384 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl); | |
| 385 if (!chain) | |
| 386 return; | |
| 387 | |
| 388 X509Certificate::OSCertHandles intermediates; | |
| 389 for (int i = 1; i < sk_X509_num(chain); ++i) | |
| 390 intermediates.push_back(sk_X509_value(chain, i)); | |
| 391 | |
| 392 os_chain_ = | |
| 393 X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), intermediates); | |
| 394 | |
| 395 // sk_X509_dup does not increase reference count on the certs in the stack. | |
| 396 openssl_chain_.reset(sk_X509_dup(chain)); | |
| 397 | |
| 398 std::vector<base::StringPiece> der_chain; | |
| 399 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 400 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 401 // We increase reference count for the certs in openssl_chain_. | |
| 402 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 403 } | |
| 404 } | |
| 405 #else // !defined(USE_OPENSSL) | |
| 406 void SSLClientSocketOpenSSL::PeerCertificateChain::Reset(SSL* ssl) { | |
| 407 openssl_chain_.reset(NULL); | |
| 408 os_chain_ = NULL; | |
| 409 | |
| 410 if (ssl == NULL) | |
| 411 return; | |
| 412 | |
| 413 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl); | |
| 414 if (!chain) | |
| 415 return; | |
| 416 | |
| 417 // sk_X509_dup does not increase reference count on the certs in the stack. | |
| 418 openssl_chain_.reset(sk_X509_dup(chain)); | |
| 419 | |
| 420 std::vector<base::StringPiece> der_chain; | |
| 421 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 422 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 423 | |
| 424 // We increase reference count for the certs in openssl_chain_. | |
| 425 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 426 | |
| 427 unsigned char* cert_data = NULL; | |
| 428 int cert_data_length = i2d_X509(x, &cert_data); | |
| 429 if (cert_data_length && cert_data) | |
| 430 der_chain.push_back(base::StringPiece(reinterpret_cast<char*>(cert_data), | |
| 431 cert_data_length)); | |
| 432 } | |
| 433 | |
| 434 os_chain_ = X509Certificate::CreateFromDERCertChain(der_chain); | |
| 435 | |
| 436 for (size_t i = 0; i < der_chain.size(); ++i) { | |
| 437 OPENSSL_free(const_cast<char*>(der_chain[i].data())); | |
| 438 } | |
| 439 | |
| 440 if (der_chain.size() != | |
| 441 static_cast<size_t>(sk_X509_num(openssl_chain_.get()))) { | |
| 442 openssl_chain_.reset(NULL); | |
| 443 os_chain_ = NULL; | |
| 444 } | |
| 445 } | |
| 446 #endif // USE_OPENSSL | |
| 447 | |
| 322 // static | 448 // static |
| 323 SSLSessionCacheOpenSSL::Config | 449 SSLSessionCacheOpenSSL::Config |
| 324 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { | 450 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { |
| 325 &GetSessionCacheKey, // key_func | 451 &GetSessionCacheKey, // key_func |
| 326 1024, // max_entries | 452 1024, // max_entries |
| 327 256, // expiration_check_count | 453 256, // expiration_check_count |
| 328 60 * 60, // timeout_seconds | 454 60 * 60, // timeout_seconds |
| 329 }; | 455 }; |
| 330 | 456 |
| 331 // static | 457 // static |
| 332 void SSLClientSocket::ClearSessionCache() { | 458 void SSLClientSocket::ClearSessionCache() { |
| 333 SSLClientSocketOpenSSL::SSLContext* context = | 459 SSLClientSocketOpenSSL::SSLContext* context = |
| 334 SSLClientSocketOpenSSL::SSLContext::GetInstance(); | 460 SSLClientSocketOpenSSL::SSLContext::GetInstance(); |
| 335 context->session_cache()->Flush(); | 461 context->session_cache()->Flush(); |
| 336 OpenSSLClientKeyStore::GetInstance()->Flush(); | 462 OpenSSLClientKeyStore::GetInstance()->Flush(); |
| 337 } | 463 } |
| 338 | 464 |
| 339 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( | 465 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
| 340 scoped_ptr<ClientSocketHandle> transport_socket, | 466 scoped_ptr<ClientSocketHandle> transport_socket, |
| 341 const HostPortPair& host_and_port, | 467 const HostPortPair& host_and_port, |
| 342 const SSLConfig& ssl_config, | 468 const SSLConfig& ssl_config, |
| 343 const SSLClientSocketContext& context) | 469 const SSLClientSocketContext& context) |
| 344 : transport_send_busy_(false), | 470 : transport_send_busy_(false), |
| 345 transport_recv_busy_(false), | 471 transport_recv_busy_(false), |
| 346 transport_recv_eof_(false), | 472 transport_recv_eof_(false), |
| 347 weak_factory_(this), | 473 weak_factory_(this), |
| 348 pending_read_error_(kNoPendingReadResult), | 474 pending_read_error_(kNoPendingReadResult), |
| 349 transport_write_error_(OK), | 475 transport_write_error_(OK), |
| 476 server_cert_chain_(new PeerCertificateChain(NULL)), | |
| 350 completed_handshake_(false), | 477 completed_handshake_(false), |
| 351 client_auth_cert_needed_(false), | 478 client_auth_cert_needed_(false), |
| 352 cert_verifier_(context.cert_verifier), | 479 cert_verifier_(context.cert_verifier), |
| 353 server_bound_cert_service_(context.server_bound_cert_service), | 480 server_bound_cert_service_(context.server_bound_cert_service), |
| 354 ssl_(NULL), | 481 ssl_(NULL), |
| 355 transport_bio_(NULL), | 482 transport_bio_(NULL), |
| 356 transport_(transport_socket.Pass()), | 483 transport_(transport_socket.Pass()), |
| 357 host_and_port_(host_and_port), | 484 host_and_port_(host_and_port), |
| 358 ssl_config_(ssl_config), | 485 ssl_config_(ssl_config), |
| 359 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 486 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
| 360 trying_cached_session_(false), | 487 trying_cached_session_(false), |
| 361 next_handshake_state_(STATE_NONE), | 488 next_handshake_state_(STATE_NONE), |
| 362 npn_status_(kNextProtoUnsupported), | 489 npn_status_(kNextProtoUnsupported), |
| 363 channel_id_request_return_value_(ERR_UNEXPECTED), | 490 channel_id_request_return_value_(ERR_UNEXPECTED), |
| 364 channel_id_xtn_negotiated_(false), | 491 channel_id_xtn_negotiated_(false), |
| 365 net_log_(transport_->socket()->NetLog()) { | 492 net_log_(transport_->socket()->NetLog()) {} |
| 366 } | |
| 367 | 493 |
| 368 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 494 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 369 Disconnect(); | 495 Disconnect(); |
| 370 } | 496 } |
| 371 | 497 |
| 372 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 498 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 373 SSLCertRequestInfo* cert_request_info) { | 499 SSLCertRequestInfo* cert_request_info) { |
| 374 cert_request_info->host_and_port = host_and_port_; | 500 cert_request_info->host_and_port = host_and_port_; |
| 375 cert_request_info->cert_authorities = cert_authorities_; | 501 cert_request_info->cert_authorities = cert_authorities_; |
| 376 } | 502 } |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 910 | 1036 |
| 911 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { | 1037 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
| 912 if (!user_connect_callback_.is_null()) { | 1038 if (!user_connect_callback_.is_null()) { |
| 913 CompletionCallback c = user_connect_callback_; | 1039 CompletionCallback c = user_connect_callback_; |
| 914 user_connect_callback_.Reset(); | 1040 user_connect_callback_.Reset(); |
| 915 c.Run(rv > OK ? OK : rv); | 1041 c.Run(rv > OK ? OK : rv); |
| 916 } | 1042 } |
| 917 } | 1043 } |
| 918 | 1044 |
| 919 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { | 1045 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { |
| 920 if (server_cert_.get()) | 1046 server_cert_chain_->Reset(ssl_); |
| 921 return server_cert_.get(); | 1047 server_cert_ = server_cert_chain_->AsOSChain(); |
| 922 | |
| 923 crypto::ScopedOpenSSL<X509, X509_free> cert(SSL_get_peer_certificate(ssl_)); | |
| 924 if (!cert.get()) { | |
| 925 LOG(WARNING) << "SSL_get_peer_certificate returned NULL"; | |
| 926 return NULL; | |
| 927 } | |
| 928 | |
| 929 // Unlike SSL_get_peer_certificate, SSL_get_peer_cert_chain does not | |
| 930 // increment the reference so sk_X509_free does not need to be called. | |
| 931 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl_); | |
| 932 X509Certificate::OSCertHandles intermediates; | |
| 933 if (chain) { | |
| 934 for (int i = 0; i < sk_X509_num(chain); ++i) | |
| 935 intermediates.push_back(sk_X509_value(chain, i)); | |
| 936 } | |
| 937 server_cert_ = X509Certificate::CreateFromHandle(cert.get(), intermediates); | |
| 938 DCHECK(server_cert_.get()); | |
| 939 | |
| 940 return server_cert_.get(); | 1048 return server_cert_.get(); |
| 941 } | 1049 } |
| 942 | 1050 |
| 943 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { | 1051 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 944 int rv = DoHandshakeLoop(result); | 1052 int rv = DoHandshakeLoop(result); |
| 945 if (rv != ERR_IO_PENDING) { | 1053 if (rv != ERR_IO_PENDING) { |
| 946 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 1054 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 947 DoConnectCallback(rv); | 1055 DoConnectCallback(rv); |
| 948 } | 1056 } |
| 949 } | 1057 } |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1417 *outlen = ssl_config_.next_protos[0].size(); | 1525 *outlen = ssl_config_.next_protos[0].size(); |
| 1418 } | 1526 } |
| 1419 | 1527 |
| 1420 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); | 1528 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
| 1421 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); | 1529 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); |
| 1422 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1530 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
| 1423 #endif | 1531 #endif |
| 1424 return SSL_TLSEXT_ERR_OK; | 1532 return SSL_TLSEXT_ERR_OK; |
| 1425 } | 1533 } |
| 1426 | 1534 |
| 1535 scoped_refptr<X509Certificate> | |
| 1536 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | |
| 1537 return server_cert_; | |
| 1538 } | |
| 1539 | |
| 1427 } // namespace net | 1540 } // namespace net |
| OLD | NEW |