Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: net/socket/ssl_client_socket_openssl.cc

Issue 173853014: Make OpenSSL UpdateServerCert() OS independent. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Free Openssl strings Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/ssl_client_socket_openssl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 PeerCertificateChain {
327 public:
328 explicit PeerCertificateChain(SSL* ssl);
329 PeerCertificateChain(const PeerCertificateChain& other);
330 ~PeerCertificateChain();
331 PeerCertificateChain& operator=(const PeerCertificateChain& other);
332
333 void Reset(SSL* ssl);
334 const scoped_refptr<X509Certificate>& AsNativeChain() const {
335 return native_chain_;
336 }
337
338 size_t size() const {
339 if (!openssl_chain_.get())
340 return 0;
341 return sk_X509_num(openssl_chain_.get());
342 }
343
344 // Returns the certificate chain as presented by server.
345 X509* operator[](size_t index) const {
346 DCHECK_LT(index, size());
347 return sk_X509_value(openssl_chain_.get(), index);
348 }
349
350 private:
351 static void FreeX509Stack(STACK_OF(X509) * chain) {
352 sk_X509_pop_free(chain, X509_free);
353 }
354 crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack> openssl_chain_;
355
356 scoped_refptr<X509Certificate> native_chain_;
357 };
358
359 PeerCertificateChain::PeerCertificateChain(SSL* ssl) { Reset(ssl); }
360 PeerCertificateChain::~PeerCertificateChain() {};
361
362 PeerCertificateChain::PeerCertificateChain(const PeerCertificateChain& other) {
363 *this = other;
364 }
365
366 PeerCertificateChain& PeerCertificateChain::operator=(
367 const PeerCertificateChain& other) {
368 if (this == &other)
369 return *this;
370
371 // native_chain is reference counted by scoped_refptr;
372 native_chain_ = other.native_chain_;
373
374 // Must increase the reference count manually for sk_X509_dup
375 openssl_chain_.reset(sk_X509_dup(other.openssl_chain_.get()));
376 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) {
377 X509* x = sk_X509_value(openssl_chain_.get(), i);
378 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
379 }
380 return *this;
381 }
382
383 void PeerCertificateChain::Reset(SSL* ssl) {
384 openssl_chain_.reset(NULL);
385 native_chain_ = NULL;
386
387 if (ssl == NULL)
388 return;
389
390 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl);
391 if (!chain)
392 return;
393
394 // sk_X509_dup does not increase reference count on the certs in the stack.
395 openssl_chain_.reset(sk_X509_dup(chain));
396
397 std::vector<base::StringPiece> der_chain;
398 for (int i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) {
399 X509* x = sk_X509_value(openssl_chain_.get(), i);
400
401 // We increase reference count for the certs in openssl_chain_.
402 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
403
404 unsigned char* cert_data = NULL;
405 int cert_data_length = i2d_X509(x, &cert_data);
406 if (cert_data_length && cert_data)
407 der_chain.push_back(base::StringPiece(reinterpret_cast<char*>(cert_data),
408 cert_data_length));
409 }
410
411 native_chain_ = X509Certificate::CreateFromDERCertChain(der_chain);
412
413 for (size_t i = 0; i < der_chain.size(); ++i) {
414 OPENSSL_free((void*)der_chain[i].data());
Ryan Sleevi 2014/02/27 03:07:29 You shouldn't need to cast to void* here. If you d
haavardm 2014/02/27 10:46:28 chain[i].data() is 'const char*' while OPENSSL_Fre
wtc 2014/02/27 17:54:56 Yes, this requires a cast, and const_cast<char*> i
haavardm 2014/03/11 18:43:21 Done.
415 }
416
417 if (!native_chain_->os_cert_handle() ||
418 native_chain_->GetIntermediateCertificates().size() + 1 !=
419 static_cast<size_t>(sk_X509_num(openssl_chain_.get()))) {
Ryan Sleevi 2014/02/27 03:07:29 Pathological case: What happens when .size() == nu
haavardm 2014/02/27 10:46:28 I think we would have a crash or freeze long befor
haavardm 2014/03/11 18:43:21 Done.
420 openssl_chain_.reset(NULL);
421 native_chain_ = NULL;
422 }
423 }
424 ;
425
322 // static 426 // static
323 SSLSessionCacheOpenSSL::Config 427 SSLSessionCacheOpenSSL::Config
324 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = { 428 SSLClientSocketOpenSSL::SSLContext::kDefaultSessionCacheConfig = {
325 &GetSessionCacheKey, // key_func 429 &GetSessionCacheKey, // key_func
326 1024, // max_entries 430 1024, // max_entries
327 256, // expiration_check_count 431 256, // expiration_check_count
328 60 * 60, // timeout_seconds 432 60 * 60, // timeout_seconds
329 }; 433 };
330 434
331 // static 435 // static
332 void SSLClientSocket::ClearSessionCache() { 436 void SSLClientSocket::ClearSessionCache() {
333 SSLClientSocketOpenSSL::SSLContext* context = 437 SSLClientSocketOpenSSL::SSLContext* context =
334 SSLClientSocketOpenSSL::SSLContext::GetInstance(); 438 SSLClientSocketOpenSSL::SSLContext::GetInstance();
335 context->session_cache()->Flush(); 439 context->session_cache()->Flush();
336 OpenSSLClientKeyStore::GetInstance()->Flush(); 440 OpenSSLClientKeyStore::GetInstance()->Flush();
337 } 441 }
338 442
339 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( 443 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL(
340 scoped_ptr<ClientSocketHandle> transport_socket, 444 scoped_ptr<ClientSocketHandle> transport_socket,
341 const HostPortPair& host_and_port, 445 const HostPortPair& host_and_port,
342 const SSLConfig& ssl_config, 446 const SSLConfig& ssl_config,
343 const SSLClientSocketContext& context) 447 const SSLClientSocketContext& context)
344 : transport_send_busy_(false), 448 : transport_send_busy_(false),
345 transport_recv_busy_(false), 449 transport_recv_busy_(false),
346 transport_recv_eof_(false), 450 transport_recv_eof_(false),
347 weak_factory_(this), 451 weak_factory_(this),
348 pending_read_error_(kNoPendingReadResult), 452 pending_read_error_(kNoPendingReadResult),
349 transport_write_error_(OK), 453 transport_write_error_(OK),
454 server_cert_chain_(new PeerCertificateChain(NULL)),
455 server_cert_(NULL),
350 completed_handshake_(false), 456 completed_handshake_(false),
351 client_auth_cert_needed_(false), 457 client_auth_cert_needed_(false),
352 cert_verifier_(context.cert_verifier), 458 cert_verifier_(context.cert_verifier),
353 server_bound_cert_service_(context.server_bound_cert_service), 459 server_bound_cert_service_(context.server_bound_cert_service),
354 ssl_(NULL), 460 ssl_(NULL),
355 transport_bio_(NULL), 461 transport_bio_(NULL),
356 transport_(transport_socket.Pass()), 462 transport_(transport_socket.Pass()),
357 host_and_port_(host_and_port), 463 host_and_port_(host_and_port),
358 ssl_config_(ssl_config), 464 ssl_config_(ssl_config),
359 ssl_session_cache_shard_(context.ssl_session_cache_shard), 465 ssl_session_cache_shard_(context.ssl_session_cache_shard),
360 trying_cached_session_(false), 466 trying_cached_session_(false),
361 next_handshake_state_(STATE_NONE), 467 next_handshake_state_(STATE_NONE),
362 npn_status_(kNextProtoUnsupported), 468 npn_status_(kNextProtoUnsupported),
363 channel_id_request_return_value_(ERR_UNEXPECTED), 469 channel_id_request_return_value_(ERR_UNEXPECTED),
364 channel_id_xtn_negotiated_(false), 470 channel_id_xtn_negotiated_(false),
365 net_log_(transport_->socket()->NetLog()) { 471 net_log_(transport_->socket()->NetLog()) {}
366 }
367 472
368 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { 473 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() {
369 Disconnect(); 474 Disconnect();
370 } 475 }
371 476
372 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( 477 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo(
373 SSLCertRequestInfo* cert_request_info) { 478 SSLCertRequestInfo* cert_request_info) {
374 cert_request_info->host_and_port = host_and_port_; 479 cert_request_info->host_and_port = host_and_port_;
375 cert_request_info->cert_authorities = cert_authorities_; 480 cert_request_info->cert_authorities = cert_authorities_;
376 } 481 }
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 1015
911 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { 1016 void SSLClientSocketOpenSSL::DoConnectCallback(int rv) {
912 if (!user_connect_callback_.is_null()) { 1017 if (!user_connect_callback_.is_null()) {
913 CompletionCallback c = user_connect_callback_; 1018 CompletionCallback c = user_connect_callback_;
914 user_connect_callback_.Reset(); 1019 user_connect_callback_.Reset();
915 c.Run(rv > OK ? OK : rv); 1020 c.Run(rv > OK ? OK : rv);
916 } 1021 }
917 } 1022 }
918 1023
919 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() { 1024 X509Certificate* SSLClientSocketOpenSSL::UpdateServerCert() {
920 if (server_cert_.get()) 1025 #ifdef USE_OPENSSL
921 return server_cert_.get(); 1026 server_cert_ = NULL;
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 1027 // 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. 1028 // increment the reference so sk_X509_free does not need to be called.
931 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl_); 1029 STACK_OF(X509)* chain = SSL_get_peer_cert_chain(ssl_);
932 X509Certificate::OSCertHandles intermediates; 1030 X509Certificate::OSCertHandles intermediates;
933 if (chain) { 1031 if (chain) {
934 for (int i = 0; i < sk_X509_num(chain); ++i) 1032 for (int i = 1; i < sk_X509_num(chain); ++i)
935 intermediates.push_back(sk_X509_value(chain, i)); 1033 intermediates.push_back(sk_X509_value(chain, i));
1034
1035 server_cert_ = X509Certificate::CreateFromHandle(sk_X509_value(chain, 0),
1036 intermediates);
936 } 1037 }
937 server_cert_ = X509Certificate::CreateFromHandle(cert.get(), intermediates); 1038 #else
938 DCHECK(server_cert_.get()); 1039 server_cert_chain_->Reset(ssl_);
1040 server_cert_ = server_cert_chain_->AsNativeChain();
1041 #endif
939 1042
940 return server_cert_.get(); 1043 return server_cert_.get();
941 } 1044 }
942 1045
943 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { 1046 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) {
944 int rv = DoHandshakeLoop(result); 1047 int rv = DoHandshakeLoop(result);
945 if (rv != ERR_IO_PENDING) { 1048 if (rv != ERR_IO_PENDING) {
946 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); 1049 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv);
947 DoConnectCallback(rv); 1050 DoConnectCallback(rv);
948 } 1051 }
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 } 1521 }
1419 1522
1420 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); 1523 npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen);
1421 server_protos_.assign(reinterpret_cast<const char*>(in), inlen); 1524 server_protos_.assign(reinterpret_cast<const char*>(in), inlen);
1422 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; 1525 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_;
1423 #endif 1526 #endif
1424 return SSL_TLSEXT_ERR_OK; 1527 return SSL_TLSEXT_ERR_OK;
1425 } 1528 }
1426 1529
1427 } // namespace net 1530 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/ssl_client_socket_openssl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698