@@ -59,6 +59,8 @@ const kSNICallback = Symbol('snicallback');
5959
6060const noop = () => {};
6161
62+ // Server side times how long a handshake is taking to protect against slow
63+ // handshakes being used for DoS.
6264function onhandshakestart(now) {
6365 debug('onhandshakestart');
6466
@@ -118,13 +120,19 @@ function loadSession(hello) {
118120 return owner.destroy(new ERR_SOCKET_CLOSED());
119121
120122 owner._handle.loadSession(session);
123+ // Session is loaded. End the parser to allow handshaking to continue.
121124 owner._handle.endParser();
122125 }
123126
124127 if (hello.sessionId.length <= 0 ||
125128 hello.tlsTicket ||
126129 owner.server &&
127130 !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.
128136 owner._handle.endParser();
129137 }
130138}
@@ -219,13 +227,17 @@ function onnewsessionclient(sessionId, session) {
219227}
220228
221229function onnewsession(sessionId, session) {
230+ debug('onnewsession');
222231 const owner = this[owner_symbol];
223232
233+ // XXX(sam) no server to emit the event on, but handshake won't continue
234+ // unless newSessionDone() is called, should it be?
224235 if (!owner.server)
225236 return;
226237
227238 var once = false;
228239 const done = () => {
240+ debug('onnewsession done');
229241 if (once)
230242 return;
231243 once = true;
@@ -316,8 +328,12 @@ function TLSSocket(socket, opts) {
316328
317329 var wrap;
318330 if ((socket instanceof net.Socket && socket._handle) || !socket) {
331+ // 1. connected socket
332+ // 2. no socket, one will be created with net.Socket().connect
319333 wrap = socket;
320334 } else {
335+ // 3. socket has no handle so it is js not c++
336+ // 4. unconnected sockets are wrapped
321337 // TLS expects to interact from C++ with a net.Socket that has a C++ stream
322338 // handle, but a JS stream doesn't have one. Wrap it up to make it look like
323339 // a socket.
@@ -337,7 +353,7 @@ function TLSSocket(socket, opts) {
337353 });
338354
339355 // Proxy for API compatibility
340- this.ssl = this._handle;
356+ this.ssl = this._handle; // C++ TLSWrap object
341357
342358 this.on('error', this._tlsError);
343359
@@ -433,8 +449,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
433449 const res = tls_wrap.wrap(externalStream,
434450 context.context,
435451 !!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, ...
438454 res._secureContext = context;
439455 res.reading = handle.reading;
440456 this[kRes] = res;
@@ -484,8 +500,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
484500
485501 this.server = options.server;
486502
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.
489505 const requestCert = !!options.requestCert || !options.isServer;
490506 const rejectUnauthorized = !!options.rejectUnauthorized;
491507
@@ -506,6 +522,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
506522 if (this.server) {
507523 if (this.server.listenerCount('resumeSession') > 0 ||
508524 this.server.listenerCount('newSession') > 0) {
525+ // Also starts the client hello parser as a side effect.
509526 ssl.enableSessionCallbacks();
510527 }
511528 if (this.server.listenerCount('OCSPRequest') > 0)
@@ -728,7 +745,7 @@ TLSSocket.prototype.getCipher = function(err) {
728745// TODO: support anonymous (nocert) and PSK
729746
730747
731- function onSocketSecure () {
748+ function onServerSocketSecure () {
732749 if (this._requestCert) {
733750 const verifyError = this._handle.verifyError();
734751 if (verifyError) {
@@ -779,7 +796,7 @@ function tlsConnectionListener(rawSocket) {
779796 SNICallback: this[kSNICallback] || SNICallback
780797 });
781798
782- socket.on('secure', onSocketSecure );
799+ socket.on('secure', onServerSocketSecure );
783800
784801 socket[kErrorEmitted] = false;
785802 socket.on('close', onSocketClose);
0 commit comments