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

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 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after
2515 void SSLClientSocketNSS::HandshakeCallback(PRFileDesc* socket, 2484 void SSLClientSocketNSS::HandshakeCallback(PRFileDesc* socket,
2516 void* arg) { 2485 void* arg) {
2517 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg); 2486 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg);
2518 2487
2519 that->handshake_callback_called_ = true; 2488 that->handshake_callback_called_ = true;
2520 2489
2521 that->UpdateServerCert(); 2490 that->UpdateServerCert();
2522 that->UpdateConnectionStatus(); 2491 that->UpdateConnectionStatus();
2523 } 2492 }
2524 2493
2494 // NextProtoCallback is called by NSS during the handshake, if the server
2495 // supports NPN, to select a protocol from the list that the server offered.
2496 // See the comment in net/third_party/nss/ssl/ssl.h for the meanings of the
2497 // arguments.
2498 // static
2499 SECStatus
2500 SSLClientSocketNSS::NextProtoCallback(void* arg,
2501 PRFileDesc* nss_fd,
2502 const unsigned char* protos,
2503 unsigned int protos_len,
2504 unsigned char* proto_out,
2505 unsigned int* proto_out_len) {
wtc 2011/10/18 00:58:08 Question: does this callback do the same thing as
agl 2011/10/18 16:44:43 I believe so and the tests pass.
2506 SSLClientSocketNSS* that = reinterpret_cast<SSLClientSocketNSS*>(arg);
2507
2508 // For each protocol in server preference, see if we support it.
2509 for (unsigned int i = 0; i < protos_len; ) {
2510 const size_t len = protos[i];
2511 for (std::vector<std::string>::const_iterator
2512 j = that->ssl_config_.next_protos.begin();
2513 j != that->ssl_config_.next_protos.end(); j++) {
2514 // Having very long elements in the |next_protos| vector isn't a disaster
2515 // because they'll never be selected, but it does indicate an error
2516 // somewhere.
2517 DCHECK_LT(j->size(), 256u);
2518
2519 if (j->size() == len &&
2520 memcmp(&protos[i + 1], j->data(), len) == 0) {
2521 that->next_proto_status_ = kNextProtoNegotiated;
2522 that->next_proto_ = *j;
2523 break;
2524 }
2525 }
2526
2527 if (that->next_proto_status_ == kNextProtoNegotiated)
2528 break;
2529
2530 // NSS checks that the data in |protos| is well formed, so we know that
2531 // this doesn't cause us to jump off the end of the buffer.
2532 i += len + 1;
2533 }
2534
2535 // If we didn't find a protocol, we select the first one from our list.
2536 if (that->next_proto_status_ != kNextProtoNegotiated) {
2537 that->next_proto_status_ = kNextProtoNoOverlap;
2538 that->next_proto_ = that->ssl_config_.next_protos[0];
2539 }
2540
2541 memcpy(proto_out, that->next_proto_.data(), that->next_proto_.size());
2542 *proto_out_len = that->next_proto_.size();
2543 return SECSuccess;
2544 }
2545
2525 void SSLClientSocketNSS::EnsureThreadIdAssigned() const { 2546 void SSLClientSocketNSS::EnsureThreadIdAssigned() const {
2526 base::AutoLock auto_lock(lock_); 2547 base::AutoLock auto_lock(lock_);
2527 if (valid_thread_id_ != base::kInvalidThreadId) 2548 if (valid_thread_id_ != base::kInvalidThreadId)
2528 return; 2549 return;
2529 valid_thread_id_ = base::PlatformThread::CurrentId(); 2550 valid_thread_id_ = base::PlatformThread::CurrentId();
2530 } 2551 }
2531 2552
2532 bool SSLClientSocketNSS::CalledOnValidThread() const { 2553 bool SSLClientSocketNSS::CalledOnValidThread() const {
2533 EnsureThreadIdAssigned(); 2554 EnsureThreadIdAssigned();
2534 base::AutoLock auto_lock(lock_); 2555 base::AutoLock auto_lock(lock_);
2535 return valid_thread_id_ == base::PlatformThread::CurrentId(); 2556 return valid_thread_id_ == base::PlatformThread::CurrentId();
2536 } 2557 }
2537 2558
2538 } // namespace net 2559 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698