Skip to content

Commit 9301b8a

Browse files
committed
tls: make deprecated tls.createSecurePair() use public API
Make the deprecated `tls.createSecurePair()` method use other public APIs only (`TLSSocket` in particular). Since `tls.createSecurePair()` has been runtime-deprecated only since Node 8, it probably isn’t quite time to remove it yet, but this patch removes almost all of the code complexity that is retained by it. The API, as it is documented, is retained. However, it is very likely that some users have come to rely on parts of undocumented API of the `SecurePair` class, especially since some of the existing tests checked for those. Therefore, this should definitely be considered a breaking change. PR-URL: #17882 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
1 parent 02fef8a commit 9301b8a

15 files changed

+90
-1871
lines changed

lib/_tls_legacy.js

-956
This file was deleted.

lib/internal/streams/duplexpair.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
const { Duplex } = require('stream');
3+
4+
const kCallback = Symbol('Callback');
5+
const kOtherSide = Symbol('Other');
6+
7+
class DuplexSocket extends Duplex {
8+
constructor() {
9+
super();
10+
this[kCallback] = null;
11+
this[kOtherSide] = null;
12+
}
13+
14+
_read() {
15+
const callback = this[kCallback];
16+
if (callback) {
17+
this[kCallback] = null;
18+
callback();
19+
}
20+
}
21+
22+
_write(chunk, encoding, callback) {
23+
this[kOtherSide][kCallback] = callback;
24+
this[kOtherSide].push(chunk);
25+
}
26+
27+
_final(callback) {
28+
this[kOtherSide].on('end', callback);
29+
this[kOtherSide].push(null);
30+
}
31+
}
32+
33+
class DuplexPair {
34+
constructor() {
35+
this.socket1 = new DuplexSocket();
36+
this.socket2 = new DuplexSocket();
37+
this.socket1[kOtherSide] = this.socket2;
38+
this.socket2[kOtherSide] = this.socket1;
39+
}
40+
}
41+
42+
module.exports = DuplexPair;

lib/tls.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const net = require('net');
3131
const url = require('url');
3232
const binding = process.binding('crypto');
3333
const Buffer = require('buffer').Buffer;
34+
const EventEmitter = require('events');
35+
const DuplexPair = require('internal/streams/duplexpair');
3436
const canonicalizeIP = process.binding('cares_wrap').canonicalizeIP;
3537

3638
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
@@ -230,6 +232,33 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
230232
}
231233
};
232234

235+
236+
class SecurePair extends EventEmitter {
237+
constructor(secureContext = exports.createSecureContext(),
238+
isServer = false,
239+
requestCert = !isServer,
240+
rejectUnauthorized = false,
241+
options = {}) {
242+
super();
243+
const { socket1, socket2 } = new DuplexPair();
244+
245+
this.server = options.server;
246+
this.credentials = secureContext;
247+
248+
this.encrypted = socket1;
249+
this.cleartext = new exports.TLSSocket(socket2, Object.assign({
250+
secureContext, isServer, requestCert, rejectUnauthorized
251+
}, options));
252+
this.cleartext.once('secure', () => this.emit('secure'));
253+
}
254+
255+
destroy() {
256+
this.cleartext.destroy();
257+
this.encrypted.destroy();
258+
}
259+
}
260+
261+
233262
exports.parseCertString = internalUtil.deprecate(
234263
internalTLS.parseCertString,
235264
'tls.parseCertString() is deprecated. ' +
@@ -243,5 +272,9 @@ exports.Server = require('_tls_wrap').Server;
243272
exports.createServer = require('_tls_wrap').createServer;
244273
exports.connect = require('_tls_wrap').connect;
245274

246-
// Deprecated: DEP0064
247-
exports.createSecurePair = require('_tls_legacy').createSecurePair;
275+
exports.createSecurePair = internalUtil.deprecate(
276+
function createSecurePair(...args) {
277+
return new SecurePair(...args);
278+
},
279+
'tls.createSecurePair() is deprecated. Please use ' +
280+
'tls.TLSSocket instead.', 'DEP0064');

node.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
'lib/timers.js',
7070
'lib/tls.js',
7171
'lib/_tls_common.js',
72-
'lib/_tls_legacy.js',
7372
'lib/_tls_wrap.js',
7473
'lib/tty.js',
7574
'lib/url.js',
@@ -140,6 +139,7 @@
140139
'lib/internal/streams/lazy_transform.js',
141140
'lib/internal/streams/async_iterator.js',
142141
'lib/internal/streams/BufferList.js',
142+
'lib/internal/streams/duplexpair.js',
143143
'lib/internal/streams/legacy.js',
144144
'lib/internal/streams/destroy.js',
145145
'lib/internal/wrap_js_stream.js',

src/async_wrap.h

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ namespace node {
6767

6868
#if HAVE_OPENSSL
6969
#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \
70-
V(SSLCONNECTION) \
7170
V(PBKDF2REQUEST) \
7271
V(RANDOMBYTESREQUEST) \
7372
V(TLSWRAP)

src/env.h

-4
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,12 @@ class ModuleWrap;
193193
V(onheaders_string, "onheaders") \
194194
V(onmessage_string, "onmessage") \
195195
V(onnewsession_string, "onnewsession") \
196-
V(onnewsessiondone_string, "onnewsessiondone") \
197196
V(onocspresponse_string, "onocspresponse") \
198197
V(ongoawaydata_string, "ongoawaydata") \
199198
V(onpriority_string, "onpriority") \
200199
V(onread_string, "onread") \
201200
V(onreadstart_string, "onreadstart") \
202201
V(onreadstop_string, "onreadstop") \
203-
V(onselect_string, "onselect") \
204202
V(onsettings_string, "onsettings") \
205203
V(onshutdown_string, "onshutdown") \
206204
V(onsignal_string, "onsignal") \
@@ -224,15 +222,13 @@ class ModuleWrap;
224222
V(raw_string, "raw") \
225223
V(read_host_object_string, "_readHostObject") \
226224
V(readable_string, "readable") \
227-
V(received_shutdown_string, "receivedShutdown") \
228225
V(refresh_string, "refresh") \
229226
V(regexp_string, "regexp") \
230227
V(rename_string, "rename") \
231228
V(replacement_string, "replacement") \
232229
V(retry_string, "retry") \
233230
V(serial_string, "serial") \
234231
V(scopeid_string, "scopeid") \
235-
V(sent_shutdown_string, "sentShutdown") \
236232
V(serial_number_string, "serialNumber") \
237233
V(service_string, "service") \
238234
V(servername_string, "servername") \

0 commit comments

Comments
 (0)