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

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

Issue 274783002: Implement SSL server socket over OpenSSL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comment on patch set #1 Created 6 years, 7 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
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 #include "net/socket/ssl_server_socket_nss.h" 5 #include "net/socket/ssl_server_socket_nss.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <winsock2.h> 8 #include <winsock2.h>
9 #endif 9 #endif
10 10
(...skipping 11 matching lines...) Expand all
22 #include <nss.h> 22 #include <nss.h>
23 #include <pk11pub.h> 23 #include <pk11pub.h>
24 #include <secerr.h> 24 #include <secerr.h>
25 #include <sechash.h> 25 #include <sechash.h>
26 #include <ssl.h> 26 #include <ssl.h>
27 #include <sslerr.h> 27 #include <sslerr.h>
28 #include <sslproto.h> 28 #include <sslproto.h>
29 29
30 #include <limits> 30 #include <limits>
31 31
32 #include "base/callback_helpers.h"
32 #include "base/lazy_instance.h" 33 #include "base/lazy_instance.h"
33 #include "base/memory/ref_counted.h" 34 #include "base/memory/ref_counted.h"
34 #include "crypto/rsa_private_key.h" 35 #include "crypto/rsa_private_key.h"
35 #include "crypto/nss_util_internal.h" 36 #include "crypto/nss_util_internal.h"
36 #include "net/base/io_buffer.h" 37 #include "net/base/io_buffer.h"
37 #include "net/base/net_errors.h" 38 #include "net/base/net_errors.h"
38 #include "net/base/net_log.h" 39 #include "net/base/net_log.h"
39 #include "net/socket/nss_ssl_util.h" 40 #include "net/socket/nss_ssl_util.h"
40 #include "net/socket/ssl_error_params.h" 41 #include "net/socket/ssl_error_params.h"
41 42
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 return OK; 491 return OK;
491 } 492 }
492 493
493 void SSLServerSocketNSS::OnSendComplete(int result) { 494 void SSLServerSocketNSS::OnSendComplete(int result) {
494 if (next_handshake_state_ == STATE_HANDSHAKE) { 495 if (next_handshake_state_ == STATE_HANDSHAKE) {
495 // In handshake phase. 496 // In handshake phase.
496 OnHandshakeIOComplete(result); 497 OnHandshakeIOComplete(result);
497 return; 498 return;
498 } 499 }
499 500
501 // TODO(byungchul): This state machine is not correct. Refactor this with
502 // SSLClientSocketNSS::OnSendComplete() which handles it better.
wtc 2014/05/16 03:57:17 Nit: it's not clear what "Refactor this with SSLCl
byungchul 2014/05/16 17:49:59 Done.
500 if (!completed_handshake_) 503 if (!completed_handshake_)
501 return; 504 return;
502 505
503 if (user_write_buf_.get()) { 506 if (user_write_buf_.get()) {
504 int rv = DoWriteLoop(result); 507 int rv = DoWriteLoop(result);
505 if (rv != ERR_IO_PENDING) 508 if (rv != ERR_IO_PENDING)
506 DoWriteCallback(rv); 509 DoWriteCallback(rv);
507 } else { 510 } else {
508 // Ensure that any queued ciphertext is flushed. 511 // Ensure that any queued ciphertext is flushed.
509 DoTransportIO(); 512 DoTransportIO();
(...skipping 12 matching lines...) Expand all
522 if (!user_read_buf_.get() || !completed_handshake_) 525 if (!user_read_buf_.get() || !completed_handshake_)
523 return; 526 return;
524 527
525 int rv = DoReadLoop(result); 528 int rv = DoReadLoop(result);
526 if (rv != ERR_IO_PENDING) 529 if (rv != ERR_IO_PENDING)
527 DoReadCallback(rv); 530 DoReadCallback(rv);
528 } 531 }
529 532
530 void SSLServerSocketNSS::OnHandshakeIOComplete(int result) { 533 void SSLServerSocketNSS::OnHandshakeIOComplete(int result) {
531 int rv = DoHandshakeLoop(result); 534 int rv = DoHandshakeLoop(result);
532 if (rv != ERR_IO_PENDING) { 535 if (rv == ERR_IO_PENDING)
533 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_SERVER_HANDSHAKE, rv); 536 return;
534 if (!user_handshake_callback_.is_null()) 537
535 DoHandshakeCallback(rv); 538 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_SERVER_HANDSHAKE, rv);
536 } 539 if (!user_handshake_callback_.is_null())
540 DoHandshakeCallback(rv);
537 } 541 }
538 542
539 // Return 0 for EOF, 543 // Return 0 for EOF,
540 // > 0 for bytes transferred immediately, 544 // > 0 for bytes transferred immediately,
541 // < 0 for error (or the non-error ERR_IO_PENDING). 545 // < 0 for error (or the non-error ERR_IO_PENDING).
542 int SSLServerSocketNSS::BufferSend(void) { 546 int SSLServerSocketNSS::BufferSend(void) {
543 if (transport_send_busy_) 547 if (transport_send_busy_)
544 return ERR_IO_PENDING; 548 return ERR_IO_PENDING;
545 549
546 const char* buf1; 550 const char* buf1;
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 int rv; 722 int rv;
719 do { 723 do {
720 rv = DoPayloadRead(); 724 rv = DoPayloadRead();
721 network_moved = DoTransportIO(); 725 network_moved = DoTransportIO();
722 } while (rv == ERR_IO_PENDING && network_moved); 726 } while (rv == ERR_IO_PENDING && network_moved);
723 return rv; 727 return rv;
724 } 728 }
725 729
726 int SSLServerSocketNSS::DoWriteLoop(int result) { 730 int SSLServerSocketNSS::DoWriteLoop(int result) {
727 DCHECK(completed_handshake_); 731 DCHECK(completed_handshake_);
728 DCHECK(next_handshake_state_ == STATE_NONE); 732 DCHECK_EQ(next_handshake_state_, STATE_NONE);
729 733
730 if (result < 0) 734 if (result < 0)
731 return result; 735 return result;
732 736
733 if (!nss_bufs_) { 737 if (!nss_bufs_) {
734 LOG(DFATAL) << "!nss_bufs_"; 738 LOG(DFATAL) << "!nss_bufs_";
735 int rv = ERR_UNEXPECTED; 739 int rv = ERR_UNEXPECTED;
736 net_log_.AddEvent(NetLog::TYPE_SSL_WRITE_ERROR, 740 net_log_.AddEvent(NetLog::TYPE_SSL_WRITE_ERROR,
737 CreateNetLogSSLErrorCallback(rv, 0)); 741 CreateNetLogSSLErrorCallback(rv, 0));
738 return rv; 742 return rv;
(...skipping 26 matching lines...) Expand all
765 << ", net_error " << net_error; 769 << ", net_error " << net_error;
766 net_log_.AddEvent(NetLog::TYPE_SSL_HANDSHAKE_ERROR, 770 net_log_.AddEvent(NetLog::TYPE_SSL_HANDSHAKE_ERROR,
767 CreateNetLogSSLErrorCallback(net_error, prerr)); 771 CreateNetLogSSLErrorCallback(net_error, prerr));
768 } 772 }
769 } 773 }
770 return net_error; 774 return net_error;
771 } 775 }
772 776
773 void SSLServerSocketNSS::DoHandshakeCallback(int rv) { 777 void SSLServerSocketNSS::DoHandshakeCallback(int rv) {
774 DCHECK_NE(rv, ERR_IO_PENDING); 778 DCHECK_NE(rv, ERR_IO_PENDING);
775 779 ResetAndReturn(&user_handshake_callback_).Run(rv > OK ? OK : rv);
776 CompletionCallback c = user_handshake_callback_;
777 user_handshake_callback_.Reset();
778 c.Run(rv > OK ? OK : rv);
779 } 780 }
780 781
781 void SSLServerSocketNSS::DoReadCallback(int rv) { 782 void SSLServerSocketNSS::DoReadCallback(int rv) {
782 DCHECK(rv != ERR_IO_PENDING); 783 DCHECK(rv != ERR_IO_PENDING);
783 DCHECK(!user_read_callback_.is_null()); 784 DCHECK(!user_read_callback_.is_null());
784 785
786 user_read_buf_ = NULL;
787 user_read_buf_len_ = 0;
785 // Since Run may result in Read being called, clear |user_read_callback_| 788 // Since Run may result in Read being called, clear |user_read_callback_|
786 // up front. 789 // up front.
wtc 2014/05/16 03:57:17 Nit: We may want to delete this comment and the co
byungchul 2014/05/16 17:49:59 Done.
787 CompletionCallback c = user_read_callback_; 790 ResetAndReturn(&user_read_callback_).Run(rv);
788 user_read_callback_.Reset();
789 user_read_buf_ = NULL;
790 user_read_buf_len_ = 0;
791 c.Run(rv);
792 } 791 }
793 792
794 void SSLServerSocketNSS::DoWriteCallback(int rv) { 793 void SSLServerSocketNSS::DoWriteCallback(int rv) {
795 DCHECK(rv != ERR_IO_PENDING); 794 DCHECK(rv != ERR_IO_PENDING);
796 DCHECK(!user_write_callback_.is_null()); 795 DCHECK(!user_write_callback_.is_null());
797 796
797 user_write_buf_ = NULL;
798 user_write_buf_len_ = 0;
798 // Since Run may result in Write being called, clear |user_write_callback_| 799 // Since Run may result in Write being called, clear |user_write_callback_|
799 // up front. 800 // up front.
800 CompletionCallback c = user_write_callback_; 801 ResetAndReturn(&user_write_callback_).Run(rv);
801 user_write_callback_.Reset();
802 user_write_buf_ = NULL;
803 user_write_buf_len_ = 0;
804 c.Run(rv);
805 } 802 }
806 803
807 // static 804 // static
808 // NSS calls this if an incoming certificate needs to be verified. 805 // NSS calls this if an incoming certificate needs to be verified.
809 // Do nothing but return SECSuccess. 806 // Do nothing but return SECSuccess.
810 // This is called only in full handshake mode. 807 // This is called only in full handshake mode.
811 // Peer certificate is retrieved in HandshakeCallback() later, which is called 808 // Peer certificate is retrieved in HandshakeCallback() later, which is called
812 // in full handshake mode or in resumption handshake mode. 809 // in full handshake mode or in resumption handshake mode.
813 SECStatus SSLServerSocketNSS::OwnAuthCertHandler(void* arg, 810 SECStatus SSLServerSocketNSS::OwnAuthCertHandler(void* arg,
814 PRFileDesc* socket, 811 PRFileDesc* socket,
(...skipping 17 matching lines...) Expand all
832 // initializes the NSS base library. 829 // initializes the NSS base library.
833 EnsureNSSSSLInit(); 830 EnsureNSSSSLInit();
834 if (!NSS_IsInitialized()) 831 if (!NSS_IsInitialized())
835 return ERR_UNEXPECTED; 832 return ERR_UNEXPECTED;
836 833
837 EnableSSLServerSockets(); 834 EnableSSLServerSockets();
838 return OK; 835 return OK;
839 } 836 }
840 837
841 } // namespace net 838 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698