Skip to content

Commit fb83f84

Browse files
sam-githubaddaleax
authored andcommitted
tls: in-line comments and other cleanups
PR-URL: #25861 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Backport-PR-URL: #25968 Reviewed-By: Michael Dawson <[email protected]>
1 parent 7cf484c commit fb83f84

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

lib/_tls_wrap.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const kSNICallback = Symbol('snicallback');
5959

6060
const noop = () => {};
6161

62+
// Server side times how long a handshake is taking to protect against slow
63+
// handshakes being used for DoS.
6264
function 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

221229
function 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);

src/node_crypto.cc

+10-38
Original file line numberDiff line numberDiff line change
@@ -2643,47 +2643,19 @@ int SSLWrap<Base>::SetCACerts(SecureContext* sc) {
26432643
}
26442644

26452645
int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
2646-
// Quoting SSL_set_verify(3ssl):
2646+
// From https://www.openssl.org/docs/man1.1.1/man3/SSL_verify_cb:
26472647
//
2648-
// The VerifyCallback function is used to control the behaviour when
2649-
// the SSL_VERIFY_PEER flag is set. It must be supplied by the
2650-
// application and receives two arguments: preverify_ok indicates,
2651-
// whether the verification of the certificate in question was passed
2652-
// (preverify_ok=1) or not (preverify_ok=0). x509_ctx is a pointer to
2653-
// the complete context used for the certificate chain verification.
2654-
//
2655-
// The certificate chain is checked starting with the deepest nesting
2656-
// level (the root CA certificate) and worked upward to the peer's
2657-
// certificate. At each level signatures and issuer attributes are
2658-
// checked. Whenever a verification error is found, the error number is
2659-
// stored in x509_ctx and VerifyCallback is called with preverify_ok=0.
2660-
// By applying X509_CTX_store_* functions VerifyCallback can locate the
2661-
// certificate in question and perform additional steps (see EXAMPLES).
2662-
// If no error is found for a certificate, VerifyCallback is called
2663-
// with preverify_ok=1 before advancing to the next level.
2664-
//
2665-
// The return value of VerifyCallback controls the strategy of the
2666-
// further verification process. If VerifyCallback returns 0, the
2667-
// verification process is immediately stopped with "verification
2668-
// failed" state. If SSL_VERIFY_PEER is set, a verification failure
2669-
// alert is sent to the peer and the TLS/SSL handshake is terminated. If
2670-
// VerifyCallback returns 1, the verification process is continued. If
2648+
// If VerifyCallback returns 1, the verification process is continued. If
26712649
// VerifyCallback always returns 1, the TLS/SSL handshake will not be
2672-
// terminated with respect to verification failures and the connection
2673-
// will be established. The calling process can however retrieve the
2674-
// error code of the last verification error using
2675-
// SSL_get_verify_result(3) or by maintaining its own error storage
2676-
// managed by VerifyCallback.
2677-
//
2678-
// If no VerifyCallback is specified, the default callback will be
2679-
// used. Its return value is identical to preverify_ok, so that any
2680-
// verification failure will lead to a termination of the TLS/SSL
2681-
// handshake with an alert message, if SSL_VERIFY_PEER is set.
2650+
// terminated with respect to verification failures and the connection will
2651+
// be established. The calling process can however retrieve the error code
2652+
// of the last verification error using SSL_get_verify_result(3) or by
2653+
// maintaining its own error storage managed by VerifyCallback.
26822654
//
2683-
// Since we cannot perform I/O quickly enough in this callback, we ignore
2684-
// all preverify_ok errors and let the handshake continue. It is
2685-
// imperative that the user use Connection::VerifyError after the
2686-
// 'secure' callback has been made.
2655+
// Since we cannot perform I/O quickly enough with X509_STORE_CTX_ APIs in
2656+
// this callback, we ignore all preverify_ok errors and let the handshake
2657+
// continue. It is imperative that the user use Connection::VerifyError after
2658+
// the 'secure' callback has been made.
26872659
return 1;
26882660
}
26892661

src/tls_wrap.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
735735
if (wrap->is_server()) {
736736
bool request_cert = args[0]->IsTrue();
737737
if (!request_cert) {
738-
// Note reject_unauthorized ignored.
738+
// If no cert is requested, there will be none to reject as unauthorized.
739739
verify_mode = SSL_VERIFY_NONE;
740740
} else {
741741
bool reject_unauthorized = args[1]->IsTrue();
@@ -744,7 +744,9 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
744744
verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
745745
}
746746
} else {
747-
// Note request_cert and reject_unauthorized are ignored for clients.
747+
// Servers always send a cert if the cipher is not anonymous (anon is
748+
// disabled by default), so use VERIFY_NONE and check the cert after the
749+
// handshake has completed.
748750
verify_mode = SSL_VERIFY_NONE;
749751
}
750752

src/tls_wrap.h

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class TLSWrap : public AsyncWrap,
8080
// Reset error_ string to empty. Not related to "clear text".
8181
void ClearError() override;
8282

83+
84+
// Called by the done() callback of the 'newSession' event.
8385
void NewSessionDoneCb();
8486

8587
// Implement MemoryRetainer:
@@ -88,6 +90,8 @@ class TLSWrap : public AsyncWrap,
8890
SET_SELF_SIZE(TLSWrap)
8991

9092
protected:
93+
// Alternative to StreamListener::stream(), that returns a StreamBase instead
94+
// of a StreamResource.
9195
inline StreamBase* underlying_stream() {
9296
return static_cast<StreamBase*>(stream_);
9397
}

0 commit comments

Comments
 (0)