@@ -61,6 +61,8 @@ const noop = () => {};
6161
6262let ipServernameWarned = false ;
6363
64+ // Server side times how long a handshake is taking to protect against slow
65+ // handshakes being used for DoS.
6466function onhandshakestart ( now ) {
6567 debug ( 'onhandshakestart' ) ;
6668
@@ -120,13 +122,19 @@ function loadSession(hello) {
120122 return owner . destroy ( new ERR_SOCKET_CLOSED ( ) ) ;
121123
122124 owner . _handle . loadSession ( session ) ;
125+ // Session is loaded. End the parser to allow handshaking to continue.
123126 owner . _handle . endParser ( ) ;
124127 }
125128
126129 if ( hello . sessionId . length <= 0 ||
127130 hello . tlsTicket ||
128131 owner . server &&
129132 ! owner . server . emit ( 'resumeSession' , hello . sessionId , onSession ) ) {
133+ // Sessions without identifiers can't be resumed.
134+ // Sessions with tickets can be resumed directly from the ticket, no server
135+ // session storage is necessary.
136+ // Without a call to a resumeSession listener, a session will never be
137+ // loaded, so end the parser to allow handshaking to continue.
130138 owner . _handle . endParser ( ) ;
131139 }
132140}
@@ -215,13 +223,17 @@ function requestOCSPDone(socket) {
215223
216224
217225function onnewsession ( sessionId , session ) {
226+ debug ( 'onnewsession' ) ;
218227 const owner = this [ owner_symbol ] ;
219228
229+ // XXX(sam) no server to emit the event on, but handshake won't continue
230+ // unless newSessionDone() is called, should it be?
220231 if ( ! owner . server )
221232 return ;
222233
223234 var once = false ;
224235 const done = ( ) => {
236+ debug ( 'onnewsession done' ) ;
225237 if ( once )
226238 return ;
227239 once = true ;
@@ -312,8 +324,12 @@ function TLSSocket(socket, opts) {
312324
313325 var wrap ;
314326 if ( ( socket instanceof net . Socket && socket . _handle ) || ! socket ) {
327+ // 1. connected socket
328+ // 2. no socket, one will be created with net.Socket().connect
315329 wrap = socket ;
316330 } else {
331+ // 3. socket has no handle so is js not c++
332+ // 4. unconnected sockets are wrapped
317333 // TLS expects to interact from C++ with a net.Socket that has a C++ stream
318334 // handle, but a JS stream doesn't have one. Wrap it up to make it look like
319335 // a socket.
@@ -333,7 +349,7 @@ function TLSSocket(socket, opts) {
333349 } ) ;
334350
335351 // Proxy for API compatibility
336- this . ssl = this . _handle ;
352+ this . ssl = this . _handle ; // C++ TLSWrap object
337353
338354 this . on ( 'error' , this . _tlsError ) ;
339355
@@ -429,8 +445,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
429445 const res = tls_wrap . wrap ( externalStream ,
430446 context . context ,
431447 ! ! options . isServer ) ;
432- res . _parent = handle ;
433- res . _parentWrap = wrap ;
448+ res . _parent = handle ; // C++ "wrap" object: TCPWrap, JSStream, ...
449+ res . _parentWrap = wrap ; // JS object: net.Socket, JSStreamSocket, ...
434450 res . _secureContext = context ;
435451 res . reading = handle . reading ;
436452 this [ kRes ] = res ;
@@ -480,8 +496,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
480496
481497 this . server = options . server ;
482498
483- // For clients, we will always have either a given ca list or be using
484- // default one
499+ // Clients (!isServer) always request a cert, servers request a client cert
500+ // only on explicit configuration.
485501 const requestCert = ! ! options . requestCert || ! options . isServer ;
486502 const rejectUnauthorized = ! ! options . rejectUnauthorized ;
487503
@@ -502,6 +518,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
502518 if ( this . server ) {
503519 if ( this . server . listenerCount ( 'resumeSession' ) > 0 ||
504520 this . server . listenerCount ( 'newSession' ) > 0 ) {
521+ // Also starts the client hello parser as a side effect.
505522 ssl . enableSessionCallbacks ( ) ;
506523 }
507524 if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
@@ -709,7 +726,7 @@ TLSSocket.prototype.getCipher = function(err) {
709726// TODO: support anonymous (nocert) and PSK
710727
711728
712- function onSocketSecure ( ) {
729+ function onServerSocketSecure ( ) {
713730 if ( this . _requestCert ) {
714731 const verifyError = this . _handle . verifyError ( ) ;
715732 if ( verifyError ) {
@@ -760,7 +777,7 @@ function tlsConnectionListener(rawSocket) {
760777 SNICallback : this [ kSNICallback ] || SNICallback
761778 } ) ;
762779
763- socket . on ( 'secure' , onSocketSecure ) ;
780+ socket . on ( 'secure' , onServerSocketSecure ) ;
764781
765782 socket [ kErrorEmitted ] = false ;
766783 socket . on ( 'close' , onSocketClose ) ;
0 commit comments