Skip to content

Commit 534379e

Browse files
committed
tls: in-line comments and other cleanups
1 parent 40c838e commit 534379e

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

lib/_tls_wrap.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const noop = () => {};
6161

6262
let ipServernameWarned = false;
6363

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

@@ -120,13 +122,19 @@ function loadSession(hello) {
120122
return owner.destroy(new ERR_SOCKET_CLOSED());
121123

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

126129
if (hello.sessionId.length <= 0 ||
127130
hello.tlsTicket ||
128131
owner.server &&
129132
!owner.server.emit('resumeSession', hello.sessionId, onSession)) {
133+
// Sessions without identifiers can't be resumed.
134+
// Sessions with tickets can be resumed directly from the ticket, no server
135+
// session storage is necessary.
136+
// Without a call to a resumeSession listener, a session will never be
137+
// loaded, so end the parser to allow handshaking to continue.
130138
owner._handle.endParser();
131139
}
132140
}
@@ -215,13 +223,17 @@ function requestOCSPDone(socket) {
215223

216224

217225
function onnewsession(sessionId, session) {
226+
debug('onnewsession');
218227
const owner = this[owner_symbol];
219228

229+
// XXX(sam) no server to emit the event on, but handshake won't continue
230+
// unless newSessionDone() is called, should it be?
220231
if (!owner.server)
221232
return;
222233

223234
var once = false;
224235
const done = () => {
236+
debug('onnewsession done');
225237
if (once)
226238
return;
227239
once = true;
@@ -312,8 +324,12 @@ function TLSSocket(socket, opts) {
312324

313325
var wrap;
314326
if ((socket instanceof net.Socket && socket._handle) || !socket) {
327+
// 1. connected socket
328+
// 2. no socket, one will be created with net.Socket().connect
315329
wrap = socket;
316330
} else {
331+
// 3. socket has no handle so is js not c++
332+
// 4. unconnected sockets are wrapped
317333
// TLS expects to interact from C++ with a net.Socket that has a C++ stream
318334
// handle, but a JS stream doesn't have one. Wrap it up to make it look like
319335
// a socket.
@@ -333,7 +349,7 @@ function TLSSocket(socket, opts) {
333349
});
334350

335351
// Proxy for API compatibility
336-
this.ssl = this._handle;
352+
this.ssl = this._handle; // C++ TLSWrap object
337353

338354
this.on('error', this._tlsError);
339355

@@ -429,8 +445,8 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
429445
const res = tls_wrap.wrap(externalStream,
430446
context.context,
431447
!!options.isServer);
432-
res._parent = handle;
433-
res._parentWrap = wrap;
448+
res._parent = handle; // C++ "wrap" object: TCPWrap, JSStream, ...
449+
res._parentWrap = wrap; // JS object: net.Socket, JSStreamSocket, ...
434450
res._secureContext = context;
435451
res.reading = handle.reading;
436452
this[kRes] = res;
@@ -480,8 +496,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
480496

481497
this.server = options.server;
482498

483-
// For clients, we will always have either a given ca list or be using
484-
// default one
499+
// Clients (!isServer) always request a cert, servers request a client cert
500+
// only on explicit configuration.
485501
const requestCert = !!options.requestCert || !options.isServer;
486502
const rejectUnauthorized = !!options.rejectUnauthorized;
487503

@@ -502,6 +518,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
502518
if (this.server) {
503519
if (this.server.listenerCount('resumeSession') > 0 ||
504520
this.server.listenerCount('newSession') > 0) {
521+
// Also starts the client hello parser as a side effect.
505522
ssl.enableSessionCallbacks();
506523
}
507524
if (this.server.listenerCount('OCSPRequest') > 0)
@@ -709,7 +726,7 @@ TLSSocket.prototype.getCipher = function(err) {
709726
// TODO: support anonymous (nocert) and PSK
710727

711728

712-
function onSocketSecure() {
729+
function onServerSocketSecure() {
713730
if (this._requestCert) {
714731
const verifyError = this._handle.verifyError();
715732
if (verifyError) {
@@ -760,7 +777,7 @@ function tlsConnectionListener(rawSocket) {
760777
SNICallback: this[kSNICallback] || SNICallback
761778
});
762779

763-
socket.on('secure', onSocketSecure);
780+
socket.on('secure', onServerSocketSecure);
764781

765782
socket[kErrorEmitted] = false;
766783
socket.on('close', onSocketClose);

src/node_crypto.cc

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,47 +2648,19 @@ int SSLWrap<Base>::SetCACerts(SecureContext* sc) {
26482648
}
26492649

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

src/tls_wrap.cc

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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)