Skip to content

Commit be6a3a1

Browse files
committed
tls: add min/max protocol version options
The existing secureProtocol option only allows setting the allowed protocol to a specific version, or setting it to "all supported versions". It also used obscure strings based on OpenSSL C API functions. Directly setting the min or max is easier to use and explain. Backport-PR-URL: nodejs#24676 PR-URL: nodejs#24405 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent 156e4c8 commit be6a3a1

12 files changed

+284
-43
lines changed

doc/api/errors.md

+11
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,17 @@ recommended to use 2048 bits or larger for stronger security.
16521652
A TLS/SSL handshake timed out. In this case, the server must also abort the
16531653
connection.
16541654

1655+
<a id="ERR_TLS_INVALID_PROTOCOL_VERSION"></a>
1656+
### ERR_TLS_INVALID_PROTOCOL_VERSION
1657+
1658+
Valid TLS protocol versions are `'TLSv1'`, `'TLSv1.1'`, or `'TLSv1.2'`.
1659+
1660+
<a id="ERR_TLS_PROTOCOL_VERSION_CONFLICT"></a>
1661+
### ERR_TLS_PROTOCOL_VERSION_CONFLICT
1662+
1663+
Attempting to set a TLS protocol `minVersion` or `maxVersion` conflicts with an
1664+
attempt to set the `secureProtocol` explicitly. Use one mechanism or the other.
1665+
16551666
<a id="ERR_TLS_RENEGOTIATE"></a>
16561667
### ERR_TLS_RENEGOTIATE
16571668

doc/api/tls.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,14 @@ changes:
10691069
passphrase: <string>]}`. The object form can only occur in an array.
10701070
`object.passphrase` is optional. Encrypted keys will be decrypted with
10711071
`object.passphrase` if provided, or `options.passphrase` if it is not.
1072+
* `maxVersion` {string} Optionally set the maximum TLS version to allow. One
1073+
of `TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified along with the
1074+
`secureProtocol` option, use one or the other. **Default:** `'TLSv1.2'`.
1075+
* `minVersion` {string} Optionally set the minimum TLS version to allow. One
1076+
of `TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified along with the
1077+
`secureProtocol` option, use one or the other. It is not recommended to use
1078+
less than TLSv1.2, but it may be required for interoperability.
1079+
**Default:** `'TLSv1'`.
10721080
* `passphrase` {string} Shared passphrase used for a single private key and/or
10731081
a PFX.
10741082
* `pfx` {string|string[]|Buffer|Buffer[]|Object[]} PFX or PKCS12 encoded
@@ -1084,9 +1092,12 @@ changes:
10841092
which is not usually necessary. This should be used carefully if at all!
10851093
Value is a numeric bitmask of the `SSL_OP_*` options from
10861094
[OpenSSL Options][].
1087-
* `secureProtocol` {string} SSL method to use. The possible values are listed
1088-
as [SSL_METHODS][], use the function names as strings. For example,
1089-
`'TLSv1_2_method'` to force TLS version 1.2. **Default:** `'TLS_method'`.
1095+
* `secureProtocol` {string} The TLS protocol version to use. The possible
1096+
values are listed as [SSL_METHODS][], use the function names as strings. For
1097+
example, use `'TLSv1_1_method'` to force TLS version 1.1, or `'TLS_method'`
1098+
to allow any TLS protocol version. It is not recommended to use TLS versions
1099+
less than 1.2, but it may be required for interoperability. **Default:**
1100+
none, see `minVersion`.
10901101
* `sessionIdContext` {string} Opaque identifier used by servers to ensure
10911102
session state is not shared between applications. Unused by clients.
10921103

lib/_tls_common.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,35 @@ const { isArrayBufferView } = require('internal/util/types');
2626
const tls = require('tls');
2727
const {
2828
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED,
29-
ERR_INVALID_ARG_TYPE
29+
ERR_INVALID_ARG_TYPE,
30+
ERR_TLS_INVALID_PROTOCOL_VERSION,
31+
ERR_TLS_PROTOCOL_VERSION_CONFLICT,
3032
} = require('internal/errors').codes;
3133

32-
const { SSL_OP_CIPHER_SERVER_PREFERENCE } = process.binding('constants').crypto;
34+
const {
35+
SSL_OP_CIPHER_SERVER_PREFERENCE,
36+
TLS1_VERSION,
37+
TLS1_1_VERSION,
38+
TLS1_2_VERSION,
39+
} = process.binding('constants').crypto;
3340

3441
// Lazily loaded
3542
var crypto = null;
3643

37-
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
44+
function toV(which, v, def) {
45+
if (v == null) v = def;
46+
if (v === 'TLSv1') return TLS1_VERSION;
47+
if (v === 'TLSv1.1') return TLS1_1_VERSION;
48+
if (v === 'TLSv1.2') return TLS1_2_VERSION;
49+
throw new ERR_TLS_INVALID_PROTOCOL_VERSION(v, which);
50+
}
3851

39-
function SecureContext(secureProtocol, secureOptions, context) {
52+
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
53+
function SecureContext(secureProtocol, secureOptions, context,
54+
minVersion, maxVersion) {
4055
if (!(this instanceof SecureContext)) {
41-
return new SecureContext(secureProtocol, secureOptions, context);
56+
return new SecureContext(secureProtocol, secureOptions, context,
57+
minVersion, maxVersion);
4258
}
4359

4460
if (context) {
@@ -47,10 +63,15 @@ function SecureContext(secureProtocol, secureOptions, context) {
4763
this.context = new NativeSecureContext();
4864

4965
if (secureProtocol) {
50-
this.context.init(secureProtocol);
51-
} else {
52-
this.context.init();
66+
if (minVersion != null)
67+
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(minVersion, secureProtocol);
68+
if (maxVersion != null)
69+
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(maxVersion, secureProtocol);
5370
}
71+
72+
this.context.init(secureProtocol,
73+
toV('minimum', minVersion, tls.DEFAULT_MIN_VERSION),
74+
toV('maximum', maxVersion, tls.DEFAULT_MAX_VERSION));
5475
}
5576

5677
if (secureOptions) this.context.setOptions(secureOptions);
@@ -76,7 +97,8 @@ exports.createSecureContext = function createSecureContext(options, context) {
7697
if (options.honorCipherOrder)
7798
secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
7899

79-
const c = new SecureContext(options.secureProtocol, secureOptions, context);
100+
const c = new SecureContext(options.secureProtocol, secureOptions, context,
101+
options.minVersion, options.maxVersion);
80102
var i;
81103
var val;
82104

lib/_tls_wrap.js

+4
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,8 @@ function Server(options, listener) {
877877
ciphers: this.ciphers,
878878
ecdhCurve: this.ecdhCurve,
879879
dhparam: this.dhparam,
880+
minVersion: this.minVersion,
881+
maxVersion: this.maxVersion,
880882
secureProtocol: this.secureProtocol,
881883
secureOptions: this.secureOptions,
882884
honorCipherOrder: this.honorCipherOrder,
@@ -948,6 +950,8 @@ Server.prototype.setOptions = function(options) {
948950
if (options.clientCertEngine)
949951
this.clientCertEngine = options.clientCertEngine;
950952
if (options.ca) this.ca = options.ca;
953+
if (options.minVersion) this.minVersion = options.minVersion;
954+
if (options.maxVersion) this.maxVersion = options.maxVersion;
951955
if (options.secureProtocol) this.secureProtocol = options.secureProtocol;
952956
if (options.crl) this.crl = options.crl;
953957
if (options.ciphers) this.ciphers = options.ciphers;

lib/https.js

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ Agent.prototype.getName = function getName(options) {
188188
if (options.servername && options.servername !== options.host)
189189
name += options.servername;
190190

191+
name += ':';
192+
if (options.minVersion)
193+
name += options.minVersion;
194+
195+
name += ':';
196+
if (options.maxVersion)
197+
name += options.maxVersion;
198+
191199
name += ':';
192200
if (options.secureProtocol)
193201
name += options.secureProtocol;

lib/internal/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,10 @@ E('ERR_TLS_CERT_ALTNAME_INVALID',
870870
'Hostname/IP does not match certificate\'s altnames: %s', Error);
871871
E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error);
872872
E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout', Error);
873+
E('ERR_TLS_INVALID_PROTOCOL_VERSION',
874+
'%j is not a valid %s TLS protocol version', TypeError);
875+
E('ERR_TLS_PROTOCOL_VERSION_CONFLICT',
876+
'TLS protocol version %j conflicts with secureProtocol %j', TypeError);
873877
E('ERR_TLS_RENEGOTIATE', 'Attempt to renegotiate TLS session failed', Error);
874878
E('ERR_TLS_RENEGOTIATION_DISABLED',
875879
'TLS session renegotiation disabled for this socket', Error);

lib/tls.js

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ exports.DEFAULT_CIPHERS =
5252

5353
exports.DEFAULT_ECDH_CURVE = 'auto';
5454

55+
exports.DEFAULT_MAX_VERSION = 'TLSv1.2';
56+
57+
exports.DEFAULT_MIN_VERSION = 'TLSv1';
58+
5559
exports.getCiphers = internalUtil.cachedResult(
5660
() => internalUtil.filterDuplicateStrings(binding.getSSLCiphers(), true)
5761
);

src/node_constants.cc

+4
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,10 @@ void DefineCryptoConstants(Local<Object> target) {
12371237
NODE_DEFINE_STRING_CONSTANT(target,
12381238
"defaultCipherList",
12391239
per_process_opts->tls_cipher_list.c_str());
1240+
1241+
NODE_DEFINE_CONSTANT(target, TLS1_VERSION);
1242+
NODE_DEFINE_CONSTANT(target, TLS1_1_VERSION);
1243+
NODE_DEFINE_CONSTANT(target, TLS1_2_VERSION);
12401244
#endif
12411245
NODE_DEFINE_CONSTANT(target, INT_MAX);
12421246
}

src/node_crypto.cc

+10-3
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,15 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
395395
ASSIGN_OR_RETURN_UNWRAP(&sc, args.Holder());
396396
Environment* env = sc->env();
397397

398-
int min_version = 0;
399-
int max_version = 0;
398+
CHECK_EQ(args.Length(), 3);
399+
CHECK(args[1]->IsInt32());
400+
CHECK(args[2]->IsInt32());
401+
402+
int min_version = args[1].As<Int32>()->Value();
403+
int max_version = args[2].As<Int32>()->Value();
400404
const SSL_METHOD* method = TLS_method();
401405

402-
if (args.Length() == 1 && args[0]->IsString()) {
406+
if (args[0]->IsString()) {
403407
const node::Utf8Value sslmethod(env->isolate(), args[0]);
404408

405409
// Note that SSLv2 and SSLv3 are disallowed but SSLv23_method and friends
@@ -424,6 +428,9 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
424428
method = TLS_server_method();
425429
} else if (strcmp(*sslmethod, "SSLv23_client_method") == 0) {
426430
method = TLS_client_method();
431+
} else if (strcmp(*sslmethod, "TLS_method") == 0) {
432+
min_version = 0;
433+
max_version = 0;
427434
} else if (strcmp(*sslmethod, "TLSv1_method") == 0) {
428435
min_version = TLS1_VERSION;
429436
max_version = TLS1_VERSION;

test/fixtures/tls-connect.js

+43-26
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,45 @@ exports.connect = function connect(options, callback) {
4646
const client = {};
4747
const pair = { server, client };
4848

49-
tls.createServer(options.server, function(conn) {
50-
server.conn = conn;
51-
conn.pipe(conn);
52-
maybeCallback()
53-
}).listen(0, function() {
54-
server.server = this;
49+
try {
50+
tls.createServer(options.server, function(conn) {
51+
server.conn = conn;
52+
conn.pipe(conn);
53+
maybeCallback()
54+
}).listen(0, function() {
55+
server.server = this;
5556

56-
const optClient = util._extend({
57-
port: this.address().port,
58-
}, options.client);
57+
const optClient = util._extend({
58+
port: this.address().port,
59+
}, options.client);
5960

60-
tls.connect(optClient)
61-
.on('secureConnect', function() {
62-
client.conn = this;
63-
maybeCallback();
64-
})
65-
.on('error', function(err) {
61+
try {
62+
tls.connect(optClient)
63+
.on('secureConnect', function() {
64+
client.conn = this;
65+
maybeCallback();
66+
})
67+
.on('error', function(err) {
68+
client.err = err;
69+
client.conn = this;
70+
maybeCallback();
71+
});
72+
} catch (err) {
6673
client.err = err;
67-
client.conn = this;
68-
maybeCallback();
69-
});
70-
});
74+
// The server won't get a connection, we are done.
75+
callback(err, pair, cleanup);
76+
callback = null;
77+
}
78+
}).on('tlsClientError', function(err, sock) {
79+
server.conn = sock;
80+
server.err = err;
81+
maybeCallback();
82+
});
83+
} catch (err) {
84+
// Invalid options can throw, report the error.
85+
pair.server.err = err;
86+
callback(err, pair, () => {});
87+
}
7188

7289
function maybeCallback() {
7390
if (!callback)
@@ -76,13 +93,13 @@ exports.connect = function connect(options, callback) {
7693
const err = pair.client.err || pair.server.err;
7794
callback(err, pair, cleanup);
7895
callback = null;
79-
80-
function cleanup() {
81-
if (server.server)
82-
server.server.close();
83-
if (client.conn)
84-
client.conn.end();
85-
}
8696
}
8797
}
98+
99+
function cleanup() {
100+
if (server.server)
101+
server.server.close();
102+
if (client.conn)
103+
client.conn.end();
104+
}
88105
}

test/parallel/test-https-agent-getname.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const agent = new https.Agent();
1212
// empty options
1313
assert.strictEqual(
1414
agent.getName({}),
15-
'localhost:::::::::::::::::'
15+
'localhost:::::::::::::::::::'
1616
);
1717

1818
// pass all options arguments
@@ -40,5 +40,5 @@ const options = {
4040
assert.strictEqual(
4141
agent.getName(options),
4242
'0.0.0.0:443:192.168.1.1:ca:cert:dynamic:ciphers:key:pfx:false:localhost:' +
43-
'secureProtocol:c,r,l:false:ecdhCurve:dhparam:0:sessionIdContext'
43+
'::secureProtocol:c,r,l:false:ecdhCurve:dhparam:0:sessionIdContext'
4444
);

0 commit comments

Comments
 (0)