@@ -62,6 +62,8 @@ const noop = () => {};
62
62
63
63
let ipServernameWarned = false ;
64
64
65
+ // Server side times how long a handshake is taking to protect against slow
66
+ // handshakes being used for DoS.
65
67
function onhandshakestart ( now ) {
66
68
debug ( 'onhandshakestart' ) ;
67
69
@@ -121,13 +123,19 @@ function loadSession(hello) {
121
123
return owner . destroy ( new ERR_SOCKET_CLOSED ( ) ) ;
122
124
123
125
owner . _handle . loadSession ( session ) ;
126
+ // Session is loaded. End the parser to allow handshaking to continue.
124
127
owner . _handle . endParser ( ) ;
125
128
}
126
129
127
130
if ( hello . sessionId . length <= 0 ||
128
131
hello . tlsTicket ||
129
132
owner . server &&
130
133
! 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.
131
139
owner . _handle . endParser ( ) ;
132
140
}
133
141
}
@@ -222,13 +230,17 @@ function onnewsessionclient(sessionId, session) {
222
230
}
223
231
224
232
function onnewsession ( sessionId , session ) {
233
+ debug ( 'onnewsession' ) ;
225
234
const owner = this [ owner_symbol ] ;
226
235
236
+ // XXX(sam) no server to emit the event on, but handshake won't continue
237
+ // unless newSessionDone() is called, should it be?
227
238
if ( ! owner . server )
228
239
return ;
229
240
230
241
var once = false ;
231
242
const done = ( ) => {
243
+ debug ( 'onnewsession done' ) ;
232
244
if ( once )
233
245
return ;
234
246
once = true ;
@@ -319,8 +331,12 @@ function TLSSocket(socket, opts) {
319
331
320
332
var wrap ;
321
333
if ( ( socket instanceof net . Socket && socket . _handle ) || ! socket ) {
334
+ // 1. connected socket
335
+ // 2. no socket, one will be created with net.Socket().connect
322
336
wrap = socket ;
323
337
} else {
338
+ // 3. socket has no handle so it is js not c++
339
+ // 4. unconnected sockets are wrapped
324
340
// TLS expects to interact from C++ with a net.Socket that has a C++ stream
325
341
// handle, but a JS stream doesn't have one. Wrap it up to make it look like
326
342
// a socket.
@@ -340,7 +356,7 @@ function TLSSocket(socket, opts) {
340
356
} ) ;
341
357
342
358
// Proxy for API compatibility
343
- this . ssl = this . _handle ;
359
+ this . ssl = this . _handle ; // C++ TLSWrap object
344
360
345
361
this . on ( 'error' , this . _tlsError ) ;
346
362
@@ -436,8 +452,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
436
452
const res = tls_wrap . wrap ( externalStream ,
437
453
context . context ,
438
454
! ! 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, ...
441
457
res . _secureContext = context ;
442
458
res . reading = handle . reading ;
443
459
this [ kRes ] = res ;
@@ -487,8 +503,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
487
503
488
504
this . server = options . server ;
489
505
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.
492
508
const requestCert = ! ! options . requestCert || ! options . isServer ;
493
509
const rejectUnauthorized = ! ! options . rejectUnauthorized ;
494
510
@@ -509,6 +525,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
509
525
if ( this . server ) {
510
526
if ( this . server . listenerCount ( 'resumeSession' ) > 0 ||
511
527
this . server . listenerCount ( 'newSession' ) > 0 ) {
528
+ // Also starts the client hello parser as a side effect.
512
529
ssl . enableSessionCallbacks ( ) ;
513
530
}
514
531
if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
@@ -736,7 +753,7 @@ TLSSocket.prototype.getCipher = function(err) {
736
753
// TODO: support anonymous (nocert) and PSK
737
754
738
755
739
- function onSocketSecure ( ) {
756
+ function onServerSocketSecure ( ) {
740
757
if ( this . _requestCert ) {
741
758
const verifyError = this . _handle . verifyError ( ) ;
742
759
if ( verifyError ) {
@@ -787,7 +804,7 @@ function tlsConnectionListener(rawSocket) {
787
804
SNICallback : this [ kSNICallback ] || SNICallback
788
805
} ) ;
789
806
790
- socket . on ( 'secure' , onSocketSecure ) ;
807
+ socket . on ( 'secure' , onServerSocketSecure ) ;
791
808
792
809
socket [ kErrorEmitted ] = false ;
793
810
socket . on ( 'close' , onSocketClose ) ;
0 commit comments