@@ -59,6 +59,8 @@ const kSNICallback = Symbol('snicallback');
59
59
60
60
const noop = ( ) => { } ;
61
61
62
+ // Server side times how long a handshake is taking to protect against slow
63
+ // handshakes being used for DoS.
62
64
function onhandshakestart ( now ) {
63
65
debug ( 'onhandshakestart' ) ;
64
66
@@ -118,13 +120,19 @@ function loadSession(hello) {
118
120
return owner . destroy ( new ERR_SOCKET_CLOSED ( ) ) ;
119
121
120
122
owner . _handle . loadSession ( session ) ;
123
+ // Session is loaded. End the parser to allow handshaking to continue.
121
124
owner . _handle . endParser ( ) ;
122
125
}
123
126
124
127
if ( hello . sessionId . length <= 0 ||
125
128
hello . tlsTicket ||
126
129
owner . server &&
127
130
! owner . server . emit ( 'resumeSession' , hello . sessionId , onSession ) ) {
131
+ // Sessions without identifiers can't be resumed.
132
+ // Sessions with tickets can be resumed directly from the ticket, no server
133
+ // session storage is necessary.
134
+ // Without a call to a resumeSession listener, a session will never be
135
+ // loaded, so end the parser to allow handshaking to continue.
128
136
owner . _handle . endParser ( ) ;
129
137
}
130
138
}
@@ -219,13 +227,17 @@ function onnewsessionclient(sessionId, session) {
219
227
}
220
228
221
229
function onnewsession ( sessionId , session ) {
230
+ debug ( 'onnewsession' ) ;
222
231
const owner = this [ owner_symbol ] ;
223
232
233
+ // XXX(sam) no server to emit the event on, but handshake won't continue
234
+ // unless newSessionDone() is called, should it be?
224
235
if ( ! owner . server )
225
236
return ;
226
237
227
238
var once = false ;
228
239
const done = ( ) => {
240
+ debug ( 'onnewsession done' ) ;
229
241
if ( once )
230
242
return ;
231
243
once = true ;
@@ -316,8 +328,12 @@ function TLSSocket(socket, opts) {
316
328
317
329
var wrap ;
318
330
if ( ( socket instanceof net . Socket && socket . _handle ) || ! socket ) {
331
+ // 1. connected socket
332
+ // 2. no socket, one will be created with net.Socket().connect
319
333
wrap = socket ;
320
334
} else {
335
+ // 3. socket has no handle so it is js not c++
336
+ // 4. unconnected sockets are wrapped
321
337
// TLS expects to interact from C++ with a net.Socket that has a C++ stream
322
338
// handle, but a JS stream doesn't have one. Wrap it up to make it look like
323
339
// a socket.
@@ -337,7 +353,7 @@ function TLSSocket(socket, opts) {
337
353
} ) ;
338
354
339
355
// Proxy for API compatibility
340
- this . ssl = this . _handle ;
356
+ this . ssl = this . _handle ; // C++ TLSWrap object
341
357
342
358
this . on ( 'error' , this . _tlsError ) ;
343
359
@@ -433,8 +449,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
433
449
const res = tls_wrap . wrap ( externalStream ,
434
450
context . context ,
435
451
! ! options . isServer ) ;
436
- res . _parent = handle ;
437
- res . _parentWrap = wrap ;
452
+ res . _parent = handle ; // C++ "wrap" object: TCPWrap, JSStream, ...
453
+ res . _parentWrap = wrap ; // JS object: net.Socket, JSStreamSocket, ...
438
454
res . _secureContext = context ;
439
455
res . reading = handle . reading ;
440
456
this [ kRes ] = res ;
@@ -484,8 +500,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
484
500
485
501
this . server = options . server ;
486
502
487
- // For clients, we will always have either a given ca list or be using
488
- // default one
503
+ // Clients (!isServer) always request a cert, servers request a client cert
504
+ // only on explicit configuration.
489
505
const requestCert = ! ! options . requestCert || ! options . isServer ;
490
506
const rejectUnauthorized = ! ! options . rejectUnauthorized ;
491
507
@@ -506,6 +522,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
506
522
if ( this . server ) {
507
523
if ( this . server . listenerCount ( 'resumeSession' ) > 0 ||
508
524
this . server . listenerCount ( 'newSession' ) > 0 ) {
525
+ // Also starts the client hello parser as a side effect.
509
526
ssl . enableSessionCallbacks ( ) ;
510
527
}
511
528
if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
@@ -728,7 +745,7 @@ TLSSocket.prototype.getCipher = function(err) {
728
745
// TODO: support anonymous (nocert) and PSK
729
746
730
747
731
- function onSocketSecure ( ) {
748
+ function onServerSocketSecure ( ) {
732
749
if ( this . _requestCert ) {
733
750
const verifyError = this . _handle . verifyError ( ) ;
734
751
if ( verifyError ) {
@@ -779,7 +796,7 @@ function tlsConnectionListener(rawSocket) {
779
796
SNICallback : this [ kSNICallback ] || SNICallback
780
797
} ) ;
781
798
782
- socket . on ( 'secure' , onSocketSecure ) ;
799
+ socket . on ( 'secure' , onServerSocketSecure ) ;
783
800
784
801
socket [ kErrorEmitted ] = false ;
785
802
socket . on ( 'close' , onSocketClose ) ;
0 commit comments