@@ -62,6 +62,8 @@ const noop = () => {};
6262
6363let ipServernameWarned = false ;
6464
65+ // Server side times how long a handshake is taking to protect against slow
66+ // handshakes being used for DoS.
6567function onhandshakestart ( now ) {
6668 debug ( 'onhandshakestart' ) ;
6769
@@ -121,13 +123,19 @@ function loadSession(hello) {
121123 return owner . destroy ( new ERR_SOCKET_CLOSED ( ) ) ;
122124
123125 owner . _handle . loadSession ( session ) ;
126+ // Session is loaded. End the parser to allow handshaking to continue.
124127 owner . _handle . endParser ( ) ;
125128 }
126129
127130 if ( hello . sessionId . length <= 0 ||
128131 hello . tlsTicket ||
129132 owner . server &&
130133 ! owner . server . emit ( 'resumeSession' , hello . sessionId , onSession ) ) {
134+ // Sessions without identifiers can't be resumed.
135+ // Sessions with tickets can be resumed directly from the ticket, no server
136+ // session storage is necessary.
137+ // Without a call to a resumeSession listener, a session will never be
138+ // loaded, so end the parser to allow handshaking to continue.
131139 owner . _handle . endParser ( ) ;
132140 }
133141}
@@ -222,13 +230,17 @@ function onnewsessionclient(sessionId, session) {
222230}
223231
224232function onnewsession ( sessionId , session ) {
233+ debug ( 'onnewsession' ) ;
225234 const owner = this [ owner_symbol ] ;
226235
236+ // XXX(sam) no server to emit the event on, but handshake won't continue
237+ // unless newSessionDone() is called, should it be?
227238 if ( ! owner . server )
228239 return ;
229240
230241 var once = false ;
231242 const done = ( ) => {
243+ debug ( 'onnewsession done' ) ;
232244 if ( once )
233245 return ;
234246 once = true ;
@@ -319,8 +331,12 @@ function TLSSocket(socket, opts) {
319331
320332 var wrap ;
321333 if ( ( socket instanceof net . Socket && socket . _handle ) || ! socket ) {
334+ // 1. connected socket
335+ // 2. no socket, one will be created with net.Socket().connect
322336 wrap = socket ;
323337 } else {
338+ // 3. socket has no handle so it is js not c++
339+ // 4. unconnected sockets are wrapped
324340 // TLS expects to interact from C++ with a net.Socket that has a C++ stream
325341 // handle, but a JS stream doesn't have one. Wrap it up to make it look like
326342 // a socket.
@@ -340,7 +356,7 @@ function TLSSocket(socket, opts) {
340356 } ) ;
341357
342358 // Proxy for API compatibility
343- this . ssl = this . _handle ;
359+ this . ssl = this . _handle ; // C++ TLSWrap object
344360
345361 this . on ( 'error' , this . _tlsError ) ;
346362
@@ -436,8 +452,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
436452 const res = tls_wrap . wrap ( externalStream ,
437453 context . context ,
438454 ! ! options . isServer ) ;
439- res . _parent = handle ;
440- res . _parentWrap = wrap ;
455+ res . _parent = handle ; // C++ "wrap" object: TCPWrap, JSStream, ...
456+ res . _parentWrap = wrap ; // JS object: net.Socket, JSStreamSocket, ...
441457 res . _secureContext = context ;
442458 res . reading = handle . reading ;
443459 this [ kRes ] = res ;
@@ -487,8 +503,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
487503
488504 this . server = options . server ;
489505
490- // For clients, we will always have either a given ca list or be using
491- // default one
506+ // Clients (!isServer) always request a cert, servers request a client cert
507+ // only on explicit configuration.
492508 const requestCert = ! ! options . requestCert || ! options . isServer ;
493509 const rejectUnauthorized = ! ! options . rejectUnauthorized ;
494510
@@ -509,6 +525,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
509525 if ( this . server ) {
510526 if ( this . server . listenerCount ( 'resumeSession' ) > 0 ||
511527 this . server . listenerCount ( 'newSession' ) > 0 ) {
528+ // Also starts the client hello parser as a side effect.
512529 ssl . enableSessionCallbacks ( ) ;
513530 }
514531 if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
@@ -736,7 +753,7 @@ TLSSocket.prototype.getCipher = function(err) {
736753// TODO: support anonymous (nocert) and PSK
737754
738755
739- function onSocketSecure ( ) {
756+ function onServerSocketSecure ( ) {
740757 if ( this . _requestCert ) {
741758 const verifyError = this . _handle . verifyError ( ) ;
742759 if ( verifyError ) {
@@ -787,7 +804,7 @@ function tlsConnectionListener(rawSocket) {
787804 SNICallback : this [ kSNICallback ] || SNICallback
788805 } ) ;
789806
790- socket . on ( 'secure' , onSocketSecure ) ;
807+ socket . on ( 'secure' , onServerSocketSecure ) ;
791808
792809 socket [ kErrorEmitted ] = false ;
793810 socket . on ( 'close' , onSocketClose ) ;
0 commit comments