2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-23 09:09:05 +00:00
|
|
|
ArrayIsArray,
|
2020-10-15 20:01:59 +00:00
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeJoin,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectCreate,
|
2020-10-15 20:01:59 +00:00
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeStartsWith,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2017-09-08 07:58:54 +00:00
|
|
|
const { parseCertString } = require('internal/tls');
|
2017-09-28 07:16:41 +00:00
|
|
|
const { isArrayBufferView } = require('internal/util/types');
|
2015-01-21 16:36:59 +00:00
|
|
|
const tls = require('tls');
|
2018-03-04 21:16:24 +00:00
|
|
|
const {
|
|
|
|
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED,
|
2018-05-06 04:52:34 +00:00
|
|
|
ERR_INVALID_ARG_TYPE,
|
2020-08-08 16:01:59 +00:00
|
|
|
ERR_INVALID_ARG_VALUE,
|
2018-05-06 04:52:34 +00:00
|
|
|
ERR_TLS_INVALID_PROTOCOL_VERSION,
|
|
|
|
ERR_TLS_PROTOCOL_VERSION_CONFLICT,
|
2018-03-04 21:16:24 +00:00
|
|
|
} = require('internal/errors').codes;
|
2018-05-06 04:52:34 +00:00
|
|
|
const {
|
|
|
|
SSL_OP_CIPHER_SERVER_PREFERENCE,
|
|
|
|
TLS1_VERSION,
|
|
|
|
TLS1_1_VERSION,
|
|
|
|
TLS1_2_VERSION,
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
TLS1_3_VERSION,
|
2018-05-06 04:52:34 +00:00
|
|
|
} = internalBinding('constants').crypto;
|
2016-05-02 17:27:12 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
const {
|
|
|
|
validateString,
|
|
|
|
validateInteger,
|
|
|
|
validateInt32,
|
|
|
|
} = require('internal/validators');
|
|
|
|
|
|
|
|
const {
|
|
|
|
toBuf
|
|
|
|
} = require('internal/crypto/util');
|
2014-03-06 23:27:01 +00:00
|
|
|
|
2018-05-06 04:52:34 +00:00
|
|
|
function toV(which, v, def) {
|
|
|
|
if (v == null) v = def;
|
|
|
|
if (v === 'TLSv1') return TLS1_VERSION;
|
|
|
|
if (v === 'TLSv1.1') return TLS1_1_VERSION;
|
|
|
|
if (v === 'TLSv1.2') return TLS1_2_VERSION;
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
if (v === 'TLSv1.3') return TLS1_3_VERSION;
|
2018-05-06 04:52:34 +00:00
|
|
|
throw new ERR_TLS_INVALID_PROTOCOL_VERSION(v, which);
|
|
|
|
}
|
|
|
|
|
2018-08-21 06:54:02 +00:00
|
|
|
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
|
2018-05-06 04:52:34 +00:00
|
|
|
function SecureContext(secureProtocol, secureOptions, minVersion, maxVersion) {
|
2014-03-06 23:27:01 +00:00
|
|
|
if (!(this instanceof SecureContext)) {
|
2018-05-06 04:52:34 +00:00
|
|
|
return new SecureContext(secureProtocol, secureOptions, minVersion,
|
|
|
|
maxVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secureProtocol) {
|
|
|
|
if (minVersion != null)
|
|
|
|
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(minVersion, secureProtocol);
|
|
|
|
if (maxVersion != null)
|
|
|
|
throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(maxVersion, secureProtocol);
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 00:36:19 +00:00
|
|
|
this.context = new NativeSecureContext();
|
2018-05-06 04:52:34 +00:00
|
|
|
this.context.init(secureProtocol,
|
|
|
|
toV('minimum', minVersion, tls.DEFAULT_MIN_VERSION),
|
|
|
|
toV('maximum', maxVersion, tls.DEFAULT_MAX_VERSION));
|
2014-03-06 23:27:01 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (secureOptions) {
|
|
|
|
validateInteger(secureOptions, 'secureOptions');
|
|
|
|
this.context.setOptions(secureOptions);
|
|
|
|
}
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 05:22:19 +00:00
|
|
|
function validateKeyOrCertOption(name, value) {
|
2018-03-19 12:33:46 +00:00
|
|
|
if (typeof value !== 'string' && !isArrayBufferView(value)) {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2018-04-25 11:58:32 +00:00
|
|
|
`options.${name}`,
|
2018-03-19 12:33:46 +00:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
value
|
2017-08-13 14:24:12 +00:00
|
|
|
);
|
2018-03-19 12:33:46 +00:00
|
|
|
}
|
2017-08-13 14:24:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 23:27:01 +00:00
|
|
|
exports.SecureContext = SecureContext;
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
function setKey(context, key, passphrase) {
|
|
|
|
validateKeyOrCertOption('key', key);
|
|
|
|
if (passphrase != null)
|
|
|
|
validateString(passphrase, 'options.passphrase');
|
|
|
|
context.setKey(key, passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
function processCiphers(ciphers) {
|
|
|
|
ciphers = StringPrototypeSplit(ciphers || tls.DEFAULT_CIPHERS, ':');
|
|
|
|
|
|
|
|
const cipherList =
|
|
|
|
ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeFilter(
|
|
|
|
ciphers,
|
|
|
|
(cipher) => {
|
|
|
|
return cipher.length > 0 &&
|
|
|
|
!StringPrototypeStartsWith(cipher, 'TLS_');
|
|
|
|
}), ':');
|
|
|
|
|
|
|
|
const cipherSuites =
|
|
|
|
ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeFilter(
|
|
|
|
ciphers,
|
|
|
|
(cipher) => {
|
|
|
|
return cipher.length > 0 &&
|
|
|
|
StringPrototypeStartsWith(cipher, 'TLS_');
|
|
|
|
}), ':');
|
|
|
|
|
|
|
|
// Specifying empty cipher suites for both TLS1.2 and TLS1.3 is invalid, its
|
|
|
|
// not possible to handshake with no suites.
|
|
|
|
if (cipherSuites === '' && cipherList === '')
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('options.ciphers', ciphers);
|
|
|
|
|
|
|
|
return { cipherList, cipherSuites };
|
|
|
|
}
|
|
|
|
|
|
|
|
function addCACerts(context, ...certs) {
|
|
|
|
for (const cert of certs) {
|
|
|
|
validateKeyOrCertOption('ca', cert);
|
|
|
|
context.addCACert(cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCerts(context, ...certs) {
|
|
|
|
for (const cert of certs) {
|
|
|
|
validateKeyOrCertOption('cert', cert);
|
|
|
|
context.setCert(cert);
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 23:27:01 +00:00
|
|
|
|
2018-11-08 00:36:19 +00:00
|
|
|
exports.createSecureContext = function createSecureContext(options) {
|
2014-03-06 23:27:01 +00:00
|
|
|
if (!options) options = {};
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
const {
|
|
|
|
ca,
|
|
|
|
cert,
|
|
|
|
ciphers,
|
|
|
|
clientCertEngine,
|
|
|
|
crl,
|
|
|
|
dhparam,
|
|
|
|
ecdhCurve = tls.DEFAULT_ECDH_CURVE,
|
|
|
|
honorCipherOrder,
|
|
|
|
key,
|
|
|
|
minVersion,
|
|
|
|
maxVersion,
|
|
|
|
passphrase,
|
|
|
|
pfx,
|
|
|
|
privateKeyIdentifier,
|
|
|
|
privateKeyEngine,
|
|
|
|
secureProtocol,
|
|
|
|
sessionIdContext,
|
|
|
|
sessionTimeout,
|
|
|
|
sigalgs,
|
|
|
|
singleUse,
|
|
|
|
ticketKeys,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
let { secureOptions } = options;
|
|
|
|
|
|
|
|
if (honorCipherOrder)
|
2016-05-02 17:27:12 +00:00
|
|
|
secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
|
2014-06-25 10:47:59 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
const c = new SecureContext(secureProtocol, secureOptions,
|
|
|
|
minVersion, maxVersion);
|
2014-03-06 23:27:01 +00:00
|
|
|
|
2019-10-24 22:19:07 +00:00
|
|
|
// Add CA before the cert to be able to load cert's issuer in C++ code.
|
2020-10-15 20:01:59 +00:00
|
|
|
// NOTE(@jasnell): ca, cert, and key are permitted to be falsy, so do not
|
|
|
|
// change the checks to !== undefined checks.
|
2017-09-01 14:14:56 +00:00
|
|
|
if (ca) {
|
2020-10-15 20:01:59 +00:00
|
|
|
if (ArrayIsArray(ca))
|
|
|
|
addCACerts(c.context, ...ca);
|
|
|
|
else
|
|
|
|
addCACerts(c.context, ca);
|
2014-03-06 23:27:01 +00:00
|
|
|
} else {
|
|
|
|
c.context.addRootCerts();
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:14:56 +00:00
|
|
|
if (cert) {
|
2020-10-15 20:01:59 +00:00
|
|
|
if (ArrayIsArray(cert))
|
|
|
|
setCerts(c.context, ...cert);
|
|
|
|
else
|
|
|
|
setCerts(c.context, cert);
|
2014-09-01 14:44:57 +00:00
|
|
|
}
|
2014-04-14 17:15:57 +00:00
|
|
|
|
2019-10-24 22:19:07 +00:00
|
|
|
// Set the key after the cert.
|
2017-06-13 08:02:33 +00:00
|
|
|
// `ssl_set_pkey` returns `0` when the key does not match the cert, but
|
2014-11-24 13:17:13 +00:00
|
|
|
// `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
|
|
|
|
// which leads to the crash later on.
|
2017-09-01 14:14:56 +00:00
|
|
|
if (key) {
|
2019-11-23 09:09:05 +00:00
|
|
|
if (ArrayIsArray(key)) {
|
2019-12-14 15:30:40 +00:00
|
|
|
for (const val of key) {
|
2017-08-27 15:03:45 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
const pem = (val != undefined && val.pem !== undefined ? val.pem : val);
|
2020-10-15 20:01:59 +00:00
|
|
|
setKey(c.context, pem, val.passphrase || passphrase);
|
2017-08-27 15:03:45 +00:00
|
|
|
}
|
2014-11-24 13:17:13 +00:00
|
|
|
} else {
|
2020-10-15 20:01:59 +00:00
|
|
|
setKey(c.context, key, passphrase);
|
2014-11-24 13:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:48:44 +00:00
|
|
|
if (sigalgs !== undefined) {
|
2020-10-15 20:01:59 +00:00
|
|
|
if (typeof sigalgs !== 'string')
|
2019-09-18 14:48:44 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.sigalgs', 'string', sigalgs);
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (sigalgs === '')
|
2020-08-08 16:01:59 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('options.sigalgs', sigalgs);
|
2019-09-18 14:48:44 +00:00
|
|
|
|
|
|
|
c.context.setSigalgs(sigalgs);
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:03:23 +00:00
|
|
|
if (privateKeyIdentifier !== undefined) {
|
|
|
|
if (privateKeyEngine === undefined) {
|
|
|
|
// Engine is required when privateKeyIdentifier is present
|
2020-08-08 16:01:59 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('options.privateKeyEngine',
|
2019-08-05 10:03:23 +00:00
|
|
|
privateKeyEngine);
|
|
|
|
}
|
|
|
|
if (key) {
|
|
|
|
// Both data key and engine key can't be set at the same time
|
2020-08-08 16:01:59 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('options.privateKeyIdentifier',
|
2019-08-05 10:03:23 +00:00
|
|
|
privateKeyIdentifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof privateKeyIdentifier === 'string' &&
|
|
|
|
typeof privateKeyEngine === 'string') {
|
|
|
|
if (c.context.setEngineKey)
|
|
|
|
c.context.setEngineKey(privateKeyIdentifier, privateKeyEngine);
|
|
|
|
else
|
|
|
|
throw new ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED();
|
|
|
|
} else if (typeof privateKeyIdentifier !== 'string') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.privateKeyIdentifier',
|
|
|
|
['string', 'undefined'],
|
|
|
|
privateKeyIdentifier);
|
|
|
|
} else {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.privateKeyEngine',
|
|
|
|
['string', 'undefined'],
|
|
|
|
privateKeyEngine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (ciphers !== undefined)
|
|
|
|
validateString(ciphers, 'options.ciphers');
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
|
|
|
|
// Work around an OpenSSL API quirk. cipherList is for TLSv1.2 and below,
|
|
|
|
// cipherSuites is for TLSv1.3 (and presumably any later versions). TLSv1.3
|
|
|
|
// cipher suites all have a standard name format beginning with TLS_, so split
|
|
|
|
// the ciphers and pass them to the appropriate API.
|
2020-10-15 20:01:59 +00:00
|
|
|
const { cipherList, cipherSuites } = processCiphers(ciphers);
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
|
|
|
|
c.context.setCipherSuites(cipherSuites);
|
|
|
|
c.context.setCiphers(cipherList);
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (cipherSuites === '' &&
|
|
|
|
c.context.getMaxProto() > TLS1_2_VERSION &&
|
|
|
|
c.context.getMinProto() < TLS1_3_VERSION) {
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
c.context.setMaxProto(TLS1_2_VERSION);
|
2020-10-15 20:01:59 +00:00
|
|
|
}
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (cipherList === '' &&
|
|
|
|
c.context.getMinProto() < TLS1_3_VERSION &&
|
|
|
|
c.context.getMaxProto() > TLS1_2_VERSION) {
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
c.context.setMinProto(TLS1_3_VERSION);
|
2020-10-15 20:01:59 +00:00
|
|
|
}
|
2014-04-14 17:15:57 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
validateString(ecdhCurve, 'options.ecdhCurve');
|
|
|
|
c.context.setECDHCurve(ecdhCurve);
|
2014-04-14 17:15:57 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (dhparam !== undefined) {
|
|
|
|
validateKeyOrCertOption('dhparam', dhparam);
|
|
|
|
const warning = c.context.setDHParam(dhparam);
|
2015-05-22 09:23:57 +00:00
|
|
|
if (warning)
|
2017-02-17 23:00:20 +00:00
|
|
|
process.emitWarning(warning, 'SecurityWarning');
|
2015-05-22 09:23:57 +00:00
|
|
|
}
|
2014-08-27 09:00:13 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (crl !== undefined) {
|
|
|
|
if (ArrayIsArray(crl)) {
|
|
|
|
for (const val of crl) {
|
|
|
|
validateKeyOrCertOption('crl', val);
|
|
|
|
c.context.addCRL(val);
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-15 20:01:59 +00:00
|
|
|
validateKeyOrCertOption('crl', crl);
|
|
|
|
c.context.addCRL(crl);
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (sessionIdContext !== undefined) {
|
|
|
|
validateString(sessionIdContext, 'options.sessionIdContext');
|
|
|
|
c.context.setSessionIdContext(sessionIdContext);
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (pfx !== undefined) {
|
|
|
|
if (ArrayIsArray(pfx)) {
|
|
|
|
for (const val of pfx) {
|
|
|
|
const raw = val.buf ? val.buf : val;
|
|
|
|
const pass = val.passphrase || passphrase;
|
|
|
|
if (pass !== undefined) {
|
|
|
|
c.context.loadPKCS12(toBuf(raw), toBuf(pass));
|
2017-08-12 16:06:35 +00:00
|
|
|
} else {
|
2020-10-15 20:01:59 +00:00
|
|
|
c.context.loadPKCS12(toBuf(raw));
|
2017-08-12 16:06:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 20:01:59 +00:00
|
|
|
} else if (passphrase) {
|
|
|
|
c.context.loadPKCS12(toBuf(pfx), toBuf(passphrase));
|
2014-03-06 23:27:01 +00:00
|
|
|
} else {
|
2020-10-15 20:01:59 +00:00
|
|
|
c.context.loadPKCS12(toBuf(pfx));
|
2014-03-06 23:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:04:40 +00:00
|
|
|
// Do not keep read/write buffers in free list for OpenSSL < 1.1.0. (For
|
|
|
|
// OpenSSL 1.1.0, buffers are malloced and freed without the use of a
|
|
|
|
// freelist.)
|
2020-10-15 20:01:59 +00:00
|
|
|
if (singleUse) {
|
2015-04-27 07:39:48 +00:00
|
|
|
c.singleUse = true;
|
2015-04-26 12:19:38 +00:00
|
|
|
c.context.setFreeListLength(0);
|
2015-04-27 07:39:48 +00:00
|
|
|
}
|
2015-04-26 12:19:38 +00:00
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (clientCertEngine !== undefined) {
|
|
|
|
if (typeof c.context.setClientCertEngine !== 'function')
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED();
|
2020-10-15 20:01:59 +00:00
|
|
|
if (typeof clientCertEngine !== 'string') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.clientCertEngine',
|
|
|
|
['string', 'null', 'undefined'],
|
|
|
|
clientCertEngine);
|
|
|
|
}
|
|
|
|
c.context.setClientCertEngine(clientCertEngine);
|
2016-04-15 14:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (ticketKeys !== undefined) {
|
|
|
|
if (!isArrayBufferView(ticketKeys)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'options.ticketKeys',
|
|
|
|
['Buffer', 'TypedArray', 'DataView'],
|
|
|
|
ticketKeys);
|
|
|
|
}
|
|
|
|
if (ticketKeys.byteLength !== 48) {
|
|
|
|
throw new ERR_INVALID_ARG_VALUE(
|
|
|
|
'options.ticketKeys',
|
|
|
|
ticketKeys.byteLenth,
|
|
|
|
'must be exactly 48 bytes');
|
|
|
|
}
|
|
|
|
c.context.setTicketKeys(ticketKeys);
|
2020-06-19 16:41:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:01:59 +00:00
|
|
|
if (sessionTimeout !== undefined) {
|
|
|
|
validateInt32(sessionTimeout, 'options.sessionTimeout');
|
|
|
|
c.context.setSessionTimeout(sessionTimeout);
|
2020-06-19 16:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 23:27:01 +00:00
|
|
|
return c;
|
|
|
|
};
|
2014-04-14 17:15:57 +00:00
|
|
|
|
2018-11-08 21:40:46 +00:00
|
|
|
// Translate some fields from the handle's C-friendly format into more idiomatic
|
|
|
|
// javascript object representations before passing them back to the user. Can
|
|
|
|
// be used on any cert object, but changing the name would be semver-major.
|
2014-04-14 17:15:57 +00:00
|
|
|
exports.translatePeerCertificate = function translatePeerCertificate(c) {
|
|
|
|
if (!c)
|
|
|
|
return null;
|
|
|
|
|
2017-09-08 07:58:54 +00:00
|
|
|
if (c.issuer != null) c.issuer = parseCertString(c.issuer);
|
2017-07-24 12:36:36 +00:00
|
|
|
if (c.issuerCertificate != null && c.issuerCertificate !== c) {
|
2014-04-17 11:57:36 +00:00
|
|
|
c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
|
|
|
|
}
|
2017-09-08 07:58:54 +00:00
|
|
|
if (c.subject != null) c.subject = parseCertString(c.subject);
|
2017-07-24 12:36:36 +00:00
|
|
|
if (c.infoAccess != null) {
|
2019-11-06 11:37:42 +00:00
|
|
|
const info = c.infoAccess;
|
2019-11-22 17:04:46 +00:00
|
|
|
c.infoAccess = ObjectCreate(null);
|
2014-04-14 17:15:57 +00:00
|
|
|
|
|
|
|
// XXX: More key validation?
|
2018-06-19 21:03:33 +00:00
|
|
|
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g, (all, key, val) => {
|
2017-07-24 12:36:36 +00:00
|
|
|
if (key in c.infoAccess)
|
2014-04-14 17:15:57 +00:00
|
|
|
c.infoAccess[key].push(val);
|
|
|
|
else
|
|
|
|
c.infoAccess[key] = [val];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
};
|