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

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

Issue 8156001: net: rework the NPN patch. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // This file includes code SSLClientSocketNSS::DoVerifyCertComplete() derived 5 // This file includes code SSLClientSocketNSS::DoVerifyCertComplete() derived
6 // from AuthCertificateCallback() in 6 // from AuthCertificateCallback() in
7 // mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp. 7 // mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp.
8 8
9 /* ***** BEGIN LICENSE BLOCK ***** 9 /* ***** BEGIN LICENSE BLOCK *****
10 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 10 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 completed_handshake_(false), 459 completed_handshake_(false),
460 eset_mitm_detected_(false), 460 eset_mitm_detected_(false),
461 kaspersky_mitm_detected_(false), 461 kaspersky_mitm_detected_(false),
462 predicted_cert_chain_correct_(false), 462 predicted_cert_chain_correct_(false),
463 next_handshake_state_(STATE_NONE), 463 next_handshake_state_(STATE_NONE),
464 nss_fd_(NULL), 464 nss_fd_(NULL),
465 nss_bufs_(NULL), 465 nss_bufs_(NULL),
466 net_log_(transport_socket->socket()->NetLog()), 466 net_log_(transport_socket->socket()->NetLog()),
467 ssl_host_info_(ssl_host_info), 467 ssl_host_info_(ssl_host_info),
468 dns_cert_checker_(context.dns_cert_checker), 468 dns_cert_checker_(context.dns_cert_checker),
469 next_proto_status_(kNextProtoUnsupported),
469 valid_thread_id_(base::kInvalidThreadId) { 470 valid_thread_id_(base::kInvalidThreadId) {
470 EnterFunction(""); 471 EnterFunction("");
471 } 472 }
472 473
473 SSLClientSocketNSS::~SSLClientSocketNSS() { 474 SSLClientSocketNSS::~SSLClientSocketNSS() {
474 EnterFunction(""); 475 EnterFunction("");
475 Disconnect(); 476 Disconnect();
476 LeaveFunction(""); 477 LeaveFunction("");
477 } 478 }
478 479
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 context.length(), out, outlen); 547 context.length(), out, outlen);
547 if (result != SECSuccess) { 548 if (result != SECSuccess) {
548 LogFailedNSSFunction(net_log_, "SSL_ExportKeyingMaterial", ""); 549 LogFailedNSSFunction(net_log_, "SSL_ExportKeyingMaterial", "");
549 return MapNSSError(PORT_GetError()); 550 return MapNSSError(PORT_GetError());
550 } 551 }
551 return OK; 552 return OK;
552 } 553 }
553 554
554 SSLClientSocket::NextProtoStatus 555 SSLClientSocket::NextProtoStatus
555 SSLClientSocketNSS::GetNextProto(std::string* proto) { 556 SSLClientSocketNSS::GetNextProto(std::string* proto) {
556 #if defined(SSL_NEXT_PROTO_NEGOTIATED) 557 *proto = next_proto_;
557 unsigned char buf[255]; 558 return next_proto_status_;
558 int state;
559 unsigned len;
560 SECStatus rv = SSL_GetNextProto(nss_fd_, &state, buf, &len, sizeof(buf));
561 if (rv != SECSuccess) {
562 NOTREACHED() << "Error return from SSL_GetNextProto: " << rv;
563 proto->clear();
564 return kNextProtoUnsupported;
565 }
566 // We don't check for truncation because sizeof(buf) is large enough to hold
567 // the maximum protocol size.
568 switch (state) {
569 case SSL_NEXT_PROTO_NO_SUPPORT:
570 proto->clear();
571 return kNextProtoUnsupported;
572 case SSL_NEXT_PROTO_NEGOTIATED:
573 *proto = std::string(reinterpret_cast<char*>(buf), len);
574 return kNextProtoNegotiated;
575 case SSL_NEXT_PROTO_NO_OVERLAP:
576 *proto = std::string(reinterpret_cast<char*>(buf), len);
577 return kNextProtoNoOverlap;
578 default:
579 NOTREACHED() << "Unknown status from SSL_GetNextProto: " << state;
580 proto->clear();
581 return kNextProtoUnsupported;
582 }
583 #else
584 // No NPN support in the libssl that we are building with.
585 proto->clear();
586 return kNextProtoUnsupported;
587 #endif
588 } 559 }
589 560
590 int SSLClientSocketNSS::Connect(OldCompletionCallback* callback) { 561 int SSLClientSocketNSS::Connect(OldCompletionCallback* callback) {
591 EnterFunction(""); 562 EnterFunction("");
592 DCHECK(transport_.get()); 563 DCHECK(transport_.get());
593 DCHECK(next_handshake_state_ == STATE_NONE); 564 DCHECK(next_handshake_state_ == STATE_NONE);
594 DCHECK(!user_read_callback_); 565 DCHECK(!user_read_callback_);
595 DCHECK(!user_write_callback_); 566 DCHECK(!user_write_callback_);
596 DCHECK(!user_connect_callback_); 567 DCHECK(!user_connect_callback_);
597 DCHECK(!user_read_buf_); 568 DCHECK(!user_read_buf_);
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 rv = SSL_OptionSet(nss_fd_, SSL_ENABLE_RENEGOTIATION, 930 rv = SSL_OptionSet(nss_fd_, SSL_ENABLE_RENEGOTIATION,
960 SSL_RENEGOTIATE_TRANSITIONAL); 931 SSL_RENEGOTIATE_TRANSITIONAL);
961 if (rv != SECSuccess) { 932 if (rv != SECSuccess) {
962 LogFailedNSSFunction( 933 LogFailedNSSFunction(
963 net_log_, "SSL_OptionSet", "SSL_ENABLE_RENEGOTIATION"); 934 net_log_, "SSL_OptionSet", "SSL_ENABLE_RENEGOTIATION");
964 } 935 }
965 #endif // SSL_ENABLE_RENEGOTIATION 936 #endif // SSL_ENABLE_RENEGOTIATION
966 937
967 #ifdef SSL_NEXT_PROTO_NEGOTIATED 938 #ifdef SSL_NEXT_PROTO_NEGOTIATED
968 if (!ssl_config_.next_protos.empty()) { 939 if (!ssl_config_.next_protos.empty()) {
969 rv = SSL_SetNextProtoNego( 940 rv = SSL_SetNextProtoCallback(
970 nss_fd_, 941 nss_fd_, SSLClientSocketNSS::NextProtoCallback, this);
971 reinterpret_cast<const unsigned char *>(ssl_config_.next_protos.data()),
972 ssl_config_.next_protos.size());
973 if (rv != SECSuccess) 942 if (rv != SECSuccess)
974 LogFailedNSSFunction(net_log_, "SSL_SetNextProtoNego", ""); 943 LogFailedNSSFunction(net_log_, "SSL_SetNextProtoCallback", "");
975 } 944 }
976 #endif 945 #endif
977 946
978 #ifdef SSL_ENABLE_OCSP_STAPLING 947 #ifdef SSL_ENABLE_OCSP_STAPLING
979 if (IsOCSPStaplingSupported()) { 948 if (IsOCSPStaplingSupported()) {
980 rv = SSL_OptionSet(nss_fd_, SSL_ENABLE_OCSP_STAPLING, PR_TRUE); 949 rv = SSL_OptionSet(nss_fd_, SSL_ENABLE_OCSP_STAPLING, PR_TRUE);
981 if (rv != SECSuccess) { 950 if (rv != SECSuccess) {
982 LogFailedNSSFunction(net_log_, "SSL_OptionSet", 951 LogFailedNSSFunction(net_log_, "SSL_OptionSet",
983 "SSL_ENABLE_OCSP_STAPLING"); 952 "SSL_ENABLE_OCSP_STAPLING");
984 } 953 }
(...skipping 1572 matching lines...) Expand 10 before | Expand all | Expand 10 after
2557 void SSLClientSocketNSS::HandshakeCallback(PRFileDesc* socket, 2526 void SSLClientSocketNSS::HandshakeCallback(PRFileDesc* socket,
2558 void* arg) { 2527 void* arg) {
2559 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg); 2528 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg);
2560 2529
2561 that->handshake_callback_called_ = true; 2530 that->handshake_callback_called_ = true;
2562 2531
2563 that->UpdateServerCert(); 2532 that->UpdateServerCert();
2564 that->UpdateConnectionStatus(); 2533 that->UpdateConnectionStatus();
2565 } 2534 }
2566 2535
2536 // NextProtoCallback is called by NSS during the handshake, if the server
2537 // supports NPN, to select a protocol from the list that the server offered.
2538 // See the comment in net/third_party/nss/ssl/ssl.h for the meanings of the
2539 // arguments.
2540 // static
2541 SECStatus
2542 SSLClientSocketNSS::NextProtoCallback(void* arg,
2543 PRFileDesc* nss_fd,
2544 const unsigned char* protos,
2545 unsigned int protos_len,
2546 unsigned char* proto_out,
2547 unsigned int* proto_out_len) {
2548 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg);
2549
2550 // For each protocol in server preference, see if we support it.
2551 for (unsigned int i = 0; i < protos_len; ) {
2552 const size_t len = protos[i];
2553 for (std::vector<std::string>::const_iterator
2554 j = that->ssl_config_.next_protos.begin();
2555 j != that->ssl_config_.next_protos.end(); j++) {
2556 // Having very long elements in the |next_protos| vector isn't a disaster
2557 // because they'll never be selected, but it does indicate an error
2558 // somewhere.
2559 DCHECK_LT(j->size(), 256u);
2560
2561 if (j->size() == len &&
2562 memcmp(&protos[i + 1], j->data(), len) == 0) {
2563 that->next_proto_status_ = kNextProtoNegotiated;
2564 that->next_proto_ = *j;
2565 break;
2566 }
2567 }
2568
2569 if (that->next_proto_status_ == kNextProtoNegotiated)
2570 break;
2571
2572 // NSS checks that the data in |protos| is well formed, so we know that
2573 // this doesn't cause us to jump off the end of the buffer.
2574 i += len + 1;
2575 }
2576
2577 // If we didn't find a protocol, we select the first one from our list.
2578 if (that->next_proto_status_ != kNextProtoNegotiated) {
2579 that->next_proto_status_ = kNextProtoNoOverlap;
2580 that->next_proto_ = that->ssl_config_.next_protos[0];
2581 }
2582
2583 memcpy(proto_out, that->next_proto_.data(), that->next_proto_.size());
2584 *proto_out_len = that->next_proto_.size();
2585 return SECSuccess;
2586 }
2587
2567 void SSLClientSocketNSS::EnsureThreadIdAssigned() const { 2588 void SSLClientSocketNSS::EnsureThreadIdAssigned() const {
2568 base::AutoLock auto_lock(lock_); 2589 base::AutoLock auto_lock(lock_);
2569 if (valid_thread_id_ != base::kInvalidThreadId) 2590 if (valid_thread_id_ != base::kInvalidThreadId)
2570 return; 2591 return;
2571 valid_thread_id_ = base::PlatformThread::CurrentId(); 2592 valid_thread_id_ = base::PlatformThread::CurrentId();
2572 } 2593 }
2573 2594
2574 bool SSLClientSocketNSS::CalledOnValidThread() const { 2595 bool SSLClientSocketNSS::CalledOnValidThread() const {
2575 EnsureThreadIdAssigned(); 2596 EnsureThreadIdAssigned();
2576 base::AutoLock auto_lock(lock_); 2597 base::AutoLock auto_lock(lock_);
2577 return valid_thread_id_ == base::PlatformThread::CurrentId(); 2598 return valid_thread_id_ == base::PlatformThread::CurrentId();
2578 } 2599 }
2579 2600
2580 } // namespace net 2601 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698