Skip to content

Commit 62b4796

Browse files
committed
tls: in-line comments and other cleanups
PR-URL: #25861 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent cccc33b commit 62b4796

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
@@ -62,6 +62,8 @@ const noop = () => {};
6262

6363
let ipServernameWarned = false;
6464

65+
// Server side times how long a handshake is taking to protect against slow
66+
// handshakes being used for DoS.
6567
function onhandshakestart(now) {
6668
debug('onhandshakestart');
6769

@@ -121,13 +123,19 @@ function loadSession(hello) {
121123
return owner.destroy(new ERR_SOCKET_CLOSED());
122124

123125
owner._handle.loadSession(session);
126+
// Session is loaded. End the parser to allow handshaking to continue.
124127
owner._handle.endParser();
125128
}
126129

127130
if (hello.sessionId.length <= 0 ||
128131
hello.tlsTicket ||
129132
owner.server &&
130133
!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.
131139
owner._handle.endParser();
132140
}
133141
}
@@ -222,13 +230,17 @@ function onnewsessionclient(sessionId, session) {
222230
}
223231

224232
function onnewsession(sessionId, session) {
233+
debug('onnewsession');
225234
const owner = this[owner_symbol];
226235

236+
// XXX(sam) no server to emit the event on, but handshake won't continue
237+
// unless newSessionDone() is called, should it be?
227238
if (!owner.server)
228239
return;
229240

230241
var once = false;
231242
const done = () => {
243+
debug('onnewsession done');
232244
if (once)
233245
return;
234246
once = true;
@@ -319,8 +331,12 @@ function TLSSocket(socket, opts) {
319331

320332
var wrap;
321333
if ((socket instanceof net.Socket && socket._handle) || !socket) {
334+
// 1. connected socket
335+
// 2. no socket, one will be created with net.Socket().connect
322336
wrap = socket;
323337
} else {
338+
// 3. socket has no handle so it is js not c++
339+
// 4. unconnected sockets are wrapped
324340
// TLS expects to interact from C++ with a net.Socket that has a C++ stream
325341
// handle, but a JS stream doesn't have one. Wrap it up to make it look like
326342
// a socket.
@@ -340,7 +356,7 @@ function TLSSocket(socket, opts) {
340356
});
341357

342358
// Proxy for API compatibility
343-
this.ssl = this._handle;
359+
this.ssl = this._handle; // C++ TLSWrap object
344360

345361
this.on('error', this._tlsError);
346362

@@ -436,8 +452,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
436452
const res = tls_wrap.wrap(externalStream,
437453
context.context,
438454
!!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, ...
441457
res._secureContext = context;
442458
res.reading = handle.reading;
443459
this[kRes] = res;
@@ -487,8 +503,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
487503

488504
this.server = options.server;
489505

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.
492508
const requestCert = !!options.requestCert || !options.isServer;
493509
const rejectUnauthorized = !!options.rejectUnauthorized;
494510

@@ -509,6 +525,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
509525
if (this.server) {
510526
if (this.server.listenerCount('resumeSession') > 0 ||
511527
this.server.listenerCount('newSession') > 0) {
528+
// Also starts the client hello parser as a side effect.
512529
ssl.enableSessionCallbacks();
513530
}
514531
if (this.server.listenerCount('OCSPRequest') > 0)
@@ -736,7 +753,7 @@ TLSSocket.prototype.getCipher = function(err) {
736753
// TODO: support anonymous (nocert) and PSK
737754

738755

739-
function onSocketSecure() {
756+
function onServerSocketSecure() {
740757
if (this._requestCert) {
741758
const verifyError = this._handle.verifyError();
742759
if (verifyError) {
@@ -787,7 +804,7 @@ function tlsConnectionListener(rawSocket) {
787804
SNICallback: this[kSNICallback] || SNICallback
788805
});
789806

790-
socket.on('secure', onSocketSecure);
807+
socket.on('secure', onServerSocketSecure);
791808

792809
socket[kErrorEmitted] = false;
793810
socket.on('close', onSocketClose);

src/node_crypto.cc

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

26722672
int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
2673-
// Quoting SSL_set_verify(3ssl):
2673+
// From https://www.openssl.org/docs/man1.1.1/man3/SSL_verify_cb:
26742674
//
2675-
// The VerifyCallback function is used to control the behaviour when
2676-
// the SSL_VERIFY_PEER flag is set. It must be supplied by the
2677-
// application and receives two arguments: preverify_ok indicates,
2678-
// whether the verification of the certificate in question was passed
2679-
// (preverify_ok=1) or not (preverify_ok=0). x509_ctx is a pointer to
2680-
// the complete context used for the certificate chain verification.
2681-
//
2682-
// The certificate chain is checked starting with the deepest nesting
2683-
// level (the root CA certificate) and worked upward to the peer's
2684-
// certificate. At each level signatures and issuer attributes are
2685-
// checked. Whenever a verification error is found, the error number is
2686-
// stored in x509_ctx and VerifyCallback is called with preverify_ok=0.
2687-
// By applying X509_CTX_store_* functions VerifyCallback can locate the
2688-
// certificate in question and perform additional steps (see EXAMPLES).
2689-
// If no error is found for a certificate, VerifyCallback is called
2690-
// with preverify_ok=1 before advancing to the next level.
2691-
//
2692-
// The return value of VerifyCallback controls the strategy of the
2693-
// further verification process. If VerifyCallback returns 0, the
2694-
// verification process is immediately stopped with "verification
2695-
// failed" state. If SSL_VERIFY_PEER is set, a verification failure
2696-
// alert is sent to the peer and the TLS/SSL handshake is terminated. If
2697-
// VerifyCallback returns 1, the verification process is continued. If
2675+
// If VerifyCallback returns 1, the verification process is continued. If
26982676
// VerifyCallback always returns 1, the TLS/SSL handshake will not be
2699-
// terminated with respect to verification failures and the connection
2700-
// will be established. The calling process can however retrieve the
2701-
// error code of the last verification error using
2702-
// SSL_get_verify_result(3) or by maintaining its own error storage
2703-
// managed by VerifyCallback.
2704-
//
2705-
// If no VerifyCallback is specified, the default callback will be
2706-
// used. Its return value is identical to preverify_ok, so that any
2707-
// verification failure will lead to a termination of the TLS/SSL
2708-
// handshake with an alert message, if SSL_VERIFY_PEER is set.
2677+
// terminated with respect to verification failures and the connection will
2678+
// be established. The calling process can however retrieve the error code
2679+
// of the last verification error using SSL_get_verify_result(3) or by
2680+
// maintaining its own error storage managed by VerifyCallback.
27092681
//
2710-
// Since we cannot perform I/O quickly enough in this callback, we ignore
2711-
// all preverify_ok errors and let the handshake continue. It is
2712-
// imperative that the user use Connection::VerifyError after the
2713-
// 'secure' callback has been made.
2682+
// Since we cannot perform I/O quickly enough with X509_STORE_CTX_ APIs in
2683+
// this callback, we ignore all preverify_ok errors and let the handshake
2684+
// continue. It is imperative that the user use Connection::VerifyError after
2685+
// the 'secure' callback has been made.
27142686
return 1;
27152687
}
27162688

src/tls_wrap.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
770770
if (wrap->is_server()) {
771771
bool request_cert = args[0]->IsTrue();
772772
if (!request_cert) {
773-
// Note reject_unauthorized ignored.
773+
// If no cert is requested, there will be none to reject as unauthorized.
774774
verify_mode = SSL_VERIFY_NONE;
775775
} else {
776776
bool reject_unauthorized = args[1]->IsTrue();
@@ -779,7 +779,9 @@ void TLSWrap::SetVerifyMode(const FunctionCallbackInfo<Value>& args) {
779779
verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
780780
}
781781
} else {
782-
// Note request_cert and reject_unauthorized are ignored for clients.
782+
// Servers always send a cert if the cipher is not anonymous (anon is
783+
// disabled by default), so use VERIFY_NONE and check the cert after the
784+
// handshake has completed.
783785
verify_mode = SSL_VERIFY_NONE;
784786
}
785787

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)