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 | |
| 332 PeerCertificateChain& operator=(const PeerCertificateChain& other) { | |
| 333 if (this == &other) | |
| 334 return *this; | |
| 335 | |
| 336 // os_chain_ is reference counted by scoped_refptr; | |
| 337 os_chain_ = other.os_chain_; | |
| 338 | |
| 339 // Must increase the reference count manually for sk_X509_dup | |
| 340 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get())); | |
| 341 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 342 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 343 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 344 } | |
| 345 return *this; | |
| 346 } | |
| 347 | |
| 348 #if defined(USE_OPENSSL) | |
|
Ryan Sleevi
2014/03/12 23:12:03
STYLE: Rather than putting this all inline - espec
haavardm
2014/03/13 10:32:02
Done.
| |
| 349 // When OSCertHandle is typedef'ed to X509, we can do a short cut | |
| 350 // to avoid converting back and forth between der and X509 struct. | |
| 351 void Reset(SSL* ssl) { | |
| 352 openssl_chain_.reset(NULL); | |
| 353 os_chain_ = NULL; | |
| 354 | |
| 355 if (ssl == NULL) | |
| 356 return; | |
| 357 | |
| 358 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl); | |
| 359 if (!chain) | |
| 360 return; | |
| 361 | |
| 362 X509Certificate::OSCertHandles intermediates; | |
| 363 for (int i = 1; i < sk_X509_num(chain); ++i) | |
| 364 intermediates.push_back(sk_X509_value(chain, i)); | |
| 365 | |
| 366 os_chain_ = X509Certificate::CreateFromHandle(sk_X509_value(chain, 0), | |
| 367 intermediates); | |
| 368 | |
| 369 // sk_X509_dup does not increase reference count on the certs in the stack. | |
| 370 openssl_chain_.reset(sk_X509_dup(chain)); | |
| 371 | |
| 372 std::vector<base::StringPiece> der_chain; | |
| 373 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 374 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 375 // We increase reference count for the certs in openssl_chain_. | |
| 376 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 377 } | |
| 378 } | |
| 379 #else // !defined(USE_OPENSSL) | |
| 380 void Reset(SSL* ssl) { | |
| 381 openssl_chain_.reset(NULL); | |
| 382 os_chain_ = NULL; | |
| 383 | |
| 384 if (ssl == NULL) | |
| 385 return; | |
| 386 | |
| 387 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl); | |
| 388 if (!chain) | |
| 389 return; | |
| 390 | |
| 391 // sk_X509_dup does not increase reference count on the certs in the stack. | |
| 392 openssl_chain_.reset(sk_X509_dup(chain)); | |
| 393 | |
| 394 std::vector<base::StringPiece> der_chain; | |
| 395 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { | |
| 396 X509* x = sk_X509_value(openssl_chain_.get(), i); | |
| 397 | |
| 398 // We increase reference count for the certs in openssl_chain_. | |
| 399 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
| 400 | |
| 401 unsigned char* cert_data = NULL; | |
| 402 int cert_data_length = i2d_X509(x, &cert_data); | |
| 403 if (cert_data_length && cert_data) | |
| 404 der_chain.push_back(base::StringPiece( | |
| 405 reinterpret_cast<char*>(cert_data), cert_data_length)); | |
| 406 } | |
| 407 | |
| 408 os_chain_ = X509Certificate::CreateFromDERCertChain(der_chain); | |
| 409 | |
| 410 for (size_t i = 0; i < der_chain.size(); ++i) { | |
| 411 OPENSSL_free(const_cast<char*>(der_chain[i].data())); | |
| 412 } | |
| 413 | |
| 414 if (der_chain.size() != | |
| 415 static_cast<size_t>(sk_X509_num(openssl_chain_.get()))) { | |
| 416 openssl_chain_.reset(NULL); | |
| 417 os_chain_ = NULL; | |
| 418 } | |
| 419 } | |
| 420 #endif // USE_OPENSSL | |
| 421 | |
| 422 // Note that when USE_OPENSSL defined OSCertHandle is X509* | |
| 423 const scoped_refptr<X509Certificate>& AsOSChain() const { return os_chain_; } | |
| 424 | |
| 425 size_t size() const { | |
| 426 if (!openssl_chain_.get()) | |
| 427 return 0; | |
| 428 return sk_X509_num(openssl_chain_.get()); | |
| 429 } | |
| 430 | |
| 431 X509* operator[](size_t index) const { | |
| 432 DCHECK_LT(index, size()); | |
| 433 return sk_X509_value(openssl_chain_.get(), index); | |
| 434 } | |
| 435 | |
| 436 private: | |
| 437 static void FreeX509Stack(STACK_OF(X509) * chain) { | |
| 438 sk_X509_pop_free(chain, X509_free); | |
| 439 } | |
| 440 crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_; | |
| 441 | |
| 442 scoped_refptr<X509Certificate> os_chain_; | |
| 443 }; | |
| 444 | |
| 322 // static | 445 // static |
| 323 SSLSessionCacheOpenSSL::Config | 446 SSLSessionCacheOpenSSL::Config |
| 324 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { | 447 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { |
| 325 &GetSessionCacheKey, // key_func | 448 &GetSessionCacheKey, // key_func |
| 326 1024, // max_entries | 449 1024, // max_entries |
| 327 256, // expiration_check_count | 450 256, // expiration_check_count |
| 328 60 * 60, // timeout_seconds | 451 60 * 60, // timeout_seconds |
| 329 }; | 452 }; |
| 330 | 453 |
| 331 // static | 454 // static |
| 332 void SSLClientSocket::ClearSessionCache() { | 455 void SSLClientSocket::ClearSessionCache() { |
| 333 SSLClientSocketOpenSSL::SSLContext* context = | 456 SSLClientSocketOpenSSL::SSLContext* context = |
| 334 SSLClientSocketOpenSSL::SSLContext::GetInstance(); | 457 SSLClientSocketOpenSSL::SSLContext::GetInstance(); |
| 335 context->session_cache()->Flush(); | 458 context->session_cache()->Flush(); |
| 336 OpenSSLClientKeyStore::GetInstance()->Flush(); | 459 OpenSSLClientKeyStore::GetInstance()->Flush(); |
| 337 } | 460 } |
| 338 | 461 |
| 339 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( | 462 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
| 340 scoped_ptr<ClientSocketHandle> transport_socket, | 463 scoped_ptr<ClientSocketHandle> transport_socket, |
| 341 const HostPortPair& host_and_port, | 464 const HostPortPair& host_and_port, |
| 342 const SSLConfig& ssl_config, | 465 const SSLConfig& ssl_config, |
| 343 const SSLClientSocketContext& context) | 466 const SSLClientSocketContext& context) |
| 344 : transport_send_busy_(false), | 467 : transport_send_busy_(false), |
| 345 transport_recv_busy_(false), | 468 transport_recv_busy_(false), |
| 346 transport_recv_eof_(false), | 469 transport_recv_eof_(false), |
| 347 weak_factory_(this), | 470 weak_factory_(this), |
| 348 pending_read_error_(kNoPendingReadResult), | 471 pending_read_error_(kNoPendingReadResult), |
| 349 transport_write_error_(OK), | 472 transport_write_error_(OK), |
| 473 server_cert_chain_(new PeerCertificateChain(NULL)), | |
| 350 completed_handshake_(false), | 474 completed_handshake_(false), |
| 351 client_auth_cert_needed_(false), | 475 client_auth_cert_needed_(false), |
| 352 cert_verifier_(context.cert_verifier), | 476 cert_verifier_(context.cert_verifier), |
| 353 server_bound_cert_service_(context.server_bound_cert_service), | 477 server_bound_cert_service_(context.server_bound_cert_service), |
| 354 ssl_(NULL), | 478 ssl_(NULL), |
| 355 transport_bio_(NULL), | 479 transport_bio_(NULL), |
| 356 transport_(transport_socket.Pass()), | 480 transport_(transport_socket.Pass()), |
| 357 host_and_port_(host_and_port), | 481 host_and_port_(host_and_port), |
| 358 ssl_config_(ssl_config), | 482 ssl_config_(ssl_config), |
| 359 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 483 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
| 360 trying_cached_session_(false), | 484 trying_cached_session_(false), |
| 361 next_handshake_state_(STATE_NONE), | 485 next_handshake_state_(STATE_NONE), |
| 362 npn_status_(kNextProtoUnsupported), | 486 npn_status_(kNextProtoUnsupported), |
| 363 channel_id_request_return_value_(ERR_UNEXPECTED), | 487 channel_id_request_return_value_(ERR_UNEXPECTED), |
| 364 channel_id_xtn_negotiated_(false), | 488 channel_id_xtn_negotiated_(false), |
| 365 net_log_(transport_->socket()->NetLog()) { | 489 net_log_(transport_->socket()->NetLog()) {} |
| 366 } | |
| 367 | 490 |
| 368 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 491 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 369 Disconnect(); | 492 Disconnect(); |
| 370 } | 493 } |
| 371 | 494 |
| 372 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 495 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 373 SSLCertRequestInfo* cert_request_info) { | 496 SSLCertRequestInfo* cert_request_info) { |
| 374 cert_request_info->host_and_port = host_and_port_; | 497 cert_request_info->host_and_port = host_and_port_; |
| 375 cert_request_info->cert_authorities = cert_authorities_; | 498 cert_request_info->cert_authorities = cert_authorities_; |
| 376 } | 499 } |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 910 | 1033 |
| 911 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { | 1034 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
| 912 if (!user_connect_callback_.is_null()) { | 1035 if (!user_connect_callback_.is_null()) { |
| 913 CompletionCallback c = user_connect_callback_; | 1036 CompletionCallback c = user_connect_callback_; |
| 914 user_connect_callback_.Reset(); | 1037 user_connect_callback_.Reset(); |
| 915 c.Run(rv > OK ? OK : rv); | 1038 c.Run(rv > OK ? OK : rv); |
| 916 } | 1039 } |
| 917 } | 1040 } |
| 918 | 1041 |
| 919 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { | 1042 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { |
| 920 if (server_cert_.get()) | 1043 server_cert_chain_->Reset(ssl_); |
| 921 return server_cert_.get(); | 1044 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(); | 1045 return server_cert_.get(); |
| 941 } | 1046 } |
| 942 | 1047 |
| 943 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { | 1048 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 944 int rv = DoHandshakeLoop(result); | 1049 int rv = DoHandshakeLoop(result); |
| 945 if (rv != ERR_IO_PENDING) { | 1050 if (rv != ERR_IO_PENDING) { |
| 946 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 1051 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 947 DoConnectCallback(rv); | 1052 DoConnectCallback(rv); |
| 948 } | 1053 } |
| 949 } | 1054 } |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1417 *outlen = ssl_config_.next_protos[0].size(); | 1522 *outlen = ssl_config_.next_protos[0].size(); |
| 1418 } | 1523 } |
| 1419 | 1524 |
| 1420 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); | 1525 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
| 1421 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); | 1526 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); |
| 1422 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1527 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
| 1423 #endif | 1528 #endif |
| 1424 return SSL_TLSEXT_ERR_OK; | 1529 return SSL_TLSEXT_ERR_OK; |
| 1425 } | 1530 } |
| 1426 | 1531 |
| 1532 const X509Certificate* | |
| 1533 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | |
| 1534 return server_cert_; | |
| 1535 } | |
| 1536 | |
| 1427 } // namespace net | 1537 } // namespace net |
| OLD | NEW |