2017-01-03 22:16:48 +01: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 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectFreeze,
|
|
|
|
} = primordials;
|
2019-05-20 11:09:02 +02:00
|
|
|
|
2018-10-12 20:44:10 +02:00
|
|
|
const {
|
|
|
|
ERR_TLS_CERT_ALTNAME_INVALID,
|
|
|
|
ERR_OUT_OF_RANGE
|
|
|
|
} = require('internal/errors').codes;
|
2016-06-08 17:18:26 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2017-09-08 09:58:54 +02:00
|
|
|
const internalTLS = require('internal/tls');
|
2017-01-16 10:19:32 +01:00
|
|
|
internalUtil.assertCrypto();
|
2018-10-02 04:52:30 +02:00
|
|
|
const { isArrayBufferView } = require('internal/util/types');
|
2016-03-09 00:31:31 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const net = require('net');
|
2018-05-06 06:52:34 +02:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2015-01-21 17:36:59 +01:00
|
|
|
const url = require('url');
|
2019-05-20 11:09:02 +02:00
|
|
|
const { getRootCertificates, getSSLCiphers } = internalBinding('crypto');
|
2018-04-16 11:54:23 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2017-12-23 05:55:37 +01:00
|
|
|
const EventEmitter = require('events');
|
2018-04-25 02:37:43 +02:00
|
|
|
const { URL } = require('internal/url');
|
2017-12-23 05:55:37 +01:00
|
|
|
const DuplexPair = require('internal/streams/duplexpair');
|
2018-08-23 15:28:41 +02:00
|
|
|
const { canonicalizeIP } = internalBinding('cares_wrap');
|
2018-04-17 15:07:34 +02:00
|
|
|
const _tls_common = require('_tls_common');
|
|
|
|
const _tls_wrap = require('_tls_wrap');
|
2013-05-22 20:32:54 +02:00
|
|
|
|
2012-02-15 19:26:43 +01:00
|
|
|
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
|
|
|
|
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
|
2017-06-17 15:11:45 +02:00
|
|
|
// renegotiations are seen. The settings are applied to all remote client
|
2012-02-15 19:26:43 +01:00
|
|
|
// connections.
|
2012-02-19 00:01:35 +01:00
|
|
|
exports.CLIENT_RENEG_LIMIT = 3;
|
2012-02-15 19:26:43 +01:00
|
|
|
exports.CLIENT_RENEG_WINDOW = 600;
|
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
exports.DEFAULT_CIPHERS =
|
2018-10-15 01:41:32 +02:00
|
|
|
internalBinding('constants').crypto.defaultCipherList;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2017-11-07 00:08:22 +01:00
|
|
|
exports.DEFAULT_ECDH_CURVE = 'auto';
|
2014-03-07 00:27:01 +01: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 02:58:08 +01:00
|
|
|
if (getOptionValue('--tls-min-v1.0'))
|
2018-05-06 06:52:34 +02:00
|
|
|
exports.DEFAULT_MIN_VERSION = 'TLSv1';
|
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 02:58:08 +01:00
|
|
|
else if (getOptionValue('--tls-min-v1.1'))
|
2018-05-06 06:52:34 +02:00
|
|
|
exports.DEFAULT_MIN_VERSION = 'TLSv1.1';
|
2019-05-01 20:45:45 +02:00
|
|
|
else if (getOptionValue('--tls-min-v1.2'))
|
|
|
|
exports.DEFAULT_MIN_VERSION = 'TLSv1.2';
|
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 02:58:08 +01:00
|
|
|
else if (getOptionValue('--tls-min-v1.3'))
|
|
|
|
exports.DEFAULT_MIN_VERSION = 'TLSv1.3';
|
2018-05-06 06:52:34 +02:00
|
|
|
else
|
|
|
|
exports.DEFAULT_MIN_VERSION = 'TLSv1.2';
|
|
|
|
|
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 02:58:08 +01:00
|
|
|
if (getOptionValue('--tls-max-v1.3'))
|
|
|
|
exports.DEFAULT_MAX_VERSION = 'TLSv1.3';
|
|
|
|
else if (getOptionValue('--tls-max-v1.2'))
|
|
|
|
exports.DEFAULT_MAX_VERSION = 'TLSv1.2';
|
|
|
|
else
|
|
|
|
exports.DEFAULT_MAX_VERSION = 'TLSv1.3'; // Will depend on node version.
|
|
|
|
|
|
|
|
|
2017-01-13 23:28:35 +01:00
|
|
|
exports.getCiphers = internalUtil.cachedResult(
|
2019-05-20 11:09:02 +02:00
|
|
|
() => internalUtil.filterDuplicateStrings(getSSLCiphers(), true)
|
2017-01-13 23:28:35 +01:00
|
|
|
);
|
2013-03-19 00:16:55 +01:00
|
|
|
|
2019-05-20 11:09:02 +02:00
|
|
|
let rootCertificates;
|
|
|
|
|
|
|
|
function cacheRootCertificates() {
|
2019-11-22 18:04:46 +01:00
|
|
|
rootCertificates = ObjectFreeze(getRootCertificates());
|
2019-05-20 11:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperty(exports, 'rootCertificates', {
|
2019-05-20 11:09:02 +02:00
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => {
|
|
|
|
// Out-of-line caching to promote inlining the getter.
|
|
|
|
if (!rootCertificates) cacheRootCertificates();
|
|
|
|
return rootCertificates;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2011-04-14 05:53:39 +02:00
|
|
|
// Convert protocols array into valid OpenSSL protocols list
|
|
|
|
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
|
2015-04-23 08:25:15 +02:00
|
|
|
function convertProtocols(protocols) {
|
2016-09-13 12:38:29 +02:00
|
|
|
const lens = new Array(protocols.length);
|
2016-06-09 00:47:18 +02:00
|
|
|
const buff = Buffer.allocUnsafe(protocols.reduce((p, c, i) => {
|
2019-03-26 05:21:27 +01:00
|
|
|
const len = Buffer.byteLength(c);
|
2018-10-12 20:44:10 +02:00
|
|
|
if (len > 255) {
|
|
|
|
throw new ERR_OUT_OF_RANGE('The byte length of the protocol at index ' +
|
|
|
|
`${i} exceeds the maximum length.`, '<= 255', len, true);
|
|
|
|
}
|
2016-06-09 00:47:18 +02:00
|
|
|
lens[i] = len;
|
|
|
|
return p + 1 + len;
|
2015-04-23 08:25:15 +02:00
|
|
|
}, 0));
|
2011-04-14 05:53:39 +02:00
|
|
|
|
2019-11-12 16:26:40 +01:00
|
|
|
let offset = 0;
|
|
|
|
for (let i = 0, c = protocols.length; i < c; i++) {
|
2016-06-09 00:47:18 +02:00
|
|
|
buff[offset++] = lens[i];
|
|
|
|
buff.write(protocols[i], offset);
|
|
|
|
offset += lens[i];
|
|
|
|
}
|
2011-04-14 05:53:39 +02:00
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
return buff;
|
2016-01-15 09:53:11 +01:00
|
|
|
}
|
2015-04-23 08:25:15 +02:00
|
|
|
|
2018-07-11 12:36:33 +02:00
|
|
|
exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
|
2015-04-23 08:25:15 +02:00
|
|
|
// If protocols is Array - translate it into buffer
|
|
|
|
if (Array.isArray(protocols)) {
|
2016-08-10 21:16:06 +02:00
|
|
|
out.ALPNProtocols = convertProtocols(protocols);
|
2018-10-02 04:52:30 +02:00
|
|
|
} else if (isArrayBufferView(protocols)) {
|
2016-08-10 21:16:06 +02:00
|
|
|
// Copy new buffer not to be modified by user.
|
2016-01-26 00:00:06 +01:00
|
|
|
out.ALPNProtocols = Buffer.from(protocols);
|
2011-04-14 05:53:39 +02:00
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
};
|
2012-07-05 21:50:21 +02:00
|
|
|
|
2016-08-15 18:46:27 +02:00
|
|
|
function unfqdn(host) {
|
|
|
|
return host.replace(/[.]$/, '');
|
|
|
|
}
|
2012-07-11 21:54:20 +02:00
|
|
|
|
2016-08-15 18:46:27 +02:00
|
|
|
function splitHost(host) {
|
|
|
|
// String#toLowerCase() is locale-sensitive so we use
|
|
|
|
// a conservative version that only lowercases A-Z.
|
|
|
|
const replacer = (c) => String.fromCharCode(32 + c.charCodeAt(0));
|
|
|
|
return unfqdn(host).replace(/[A-Z]/g, replacer).split('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(hostParts, pattern, wildcards) {
|
|
|
|
// Empty strings, null, undefined, etc. never match.
|
|
|
|
if (!pattern)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const patternParts = splitHost(pattern);
|
|
|
|
|
|
|
|
if (hostParts.length !== patternParts.length)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Pattern has empty components, e.g. "bad..example.com".
|
|
|
|
if (patternParts.includes(''))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// RFC 6125 allows IDNA U-labels (Unicode) in names but we have no
|
|
|
|
// good way to detect their encoding or normalize them so we simply
|
|
|
|
// reject them. Control characters and blanks are rejected as well
|
|
|
|
// because nothing good can come from accepting them.
|
|
|
|
const isBad = (s) => /[^\u0021-\u007F]/u.test(s);
|
|
|
|
if (patternParts.some(isBad))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check host parts from right to left first.
|
2019-11-12 16:26:40 +01:00
|
|
|
for (let i = hostParts.length - 1; i > 0; i -= 1) {
|
2016-08-15 18:46:27 +02:00
|
|
|
if (hostParts[i] !== patternParts[i])
|
|
|
|
return false;
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2016-08-15 18:46:27 +02:00
|
|
|
|
|
|
|
const hostSubdomain = hostParts[0];
|
|
|
|
const patternSubdomain = patternParts[0];
|
|
|
|
const patternSubdomainParts = patternSubdomain.split('*');
|
|
|
|
|
|
|
|
// Short-circuit when the subdomain does not contain a wildcard.
|
|
|
|
// RFC 6125 does not allow wildcard substitution for components
|
|
|
|
// containing IDNA A-labels (Punycode) so match those verbatim.
|
|
|
|
if (patternSubdomainParts.length === 1 || patternSubdomain.includes('xn--'))
|
|
|
|
return hostSubdomain === patternSubdomain;
|
|
|
|
|
|
|
|
if (!wildcards)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// More than one wildcard is always wrong.
|
|
|
|
if (patternSubdomainParts.length > 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// *.tld wildcards are not allowed.
|
|
|
|
if (patternParts.length <= 2)
|
|
|
|
return false;
|
2012-07-11 21:54:20 +02:00
|
|
|
|
2016-08-15 18:46:27 +02:00
|
|
|
const [prefix, suffix] = patternSubdomainParts;
|
|
|
|
|
|
|
|
if (prefix.length + suffix.length > hostSubdomain.length)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!hostSubdomain.startsWith(prefix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!hostSubdomain.endsWith(suffix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 02:37:43 +02:00
|
|
|
let urlWarningEmitted = false;
|
2018-05-24 13:14:27 +02:00
|
|
|
exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
|
2016-08-15 18:46:27 +02:00
|
|
|
const subject = cert.subject;
|
|
|
|
const altNames = cert.subjectaltname;
|
|
|
|
const dnsNames = [];
|
|
|
|
const uriNames = [];
|
2016-01-12 22:04:50 +01:00
|
|
|
const ips = [];
|
2016-08-15 18:46:27 +02:00
|
|
|
|
2018-05-24 13:14:27 +02:00
|
|
|
hostname = '' + hostname;
|
2016-08-15 18:46:27 +02:00
|
|
|
|
|
|
|
if (altNames) {
|
|
|
|
for (const name of altNames.split(', ')) {
|
|
|
|
if (name.startsWith('DNS:')) {
|
|
|
|
dnsNames.push(name.slice(4));
|
|
|
|
} else if (name.startsWith('URI:')) {
|
2018-04-25 02:37:43 +02:00
|
|
|
let uri;
|
|
|
|
try {
|
|
|
|
uri = new URL(name.slice(4));
|
2018-10-12 18:50:41 +02:00
|
|
|
} catch {
|
2018-04-25 02:37:43 +02:00
|
|
|
uri = url.parse(name.slice(4));
|
|
|
|
if (!urlWarningEmitted && !process.noDeprecation) {
|
|
|
|
urlWarningEmitted = true;
|
|
|
|
process.emitWarning(
|
|
|
|
`The URI ${name.slice(4)} found in cert.subjectaltname ` +
|
|
|
|
'is not a valid URI, and is supported in the tls module ' +
|
|
|
|
'solely for compatibility.',
|
|
|
|
'DeprecationWarning', 'DEP0109');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:46:27 +02:00
|
|
|
uriNames.push(uri.hostname); // TODO(bnoordhuis) Also use scheme.
|
|
|
|
} else if (name.startsWith('IP Address:')) {
|
2017-08-10 15:11:19 +02:00
|
|
|
ips.push(canonicalizeIP(name.slice(11)));
|
2012-07-11 21:54:20 +02:00
|
|
|
}
|
2016-08-15 18:46:27 +02:00
|
|
|
}
|
2012-07-11 21:54:20 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 18:46:27 +02:00
|
|
|
let valid = false;
|
|
|
|
let reason = 'Unknown reason';
|
|
|
|
|
2018-05-24 13:14:27 +02:00
|
|
|
if (net.isIP(hostname)) {
|
|
|
|
valid = ips.includes(canonicalizeIP(hostname));
|
2016-08-15 18:46:27 +02:00
|
|
|
if (!valid)
|
2018-05-24 13:14:27 +02:00
|
|
|
reason = `IP: ${hostname} is not in the cert's list: ${ips.join(', ')}`;
|
2016-08-15 18:46:27 +02:00
|
|
|
// TODO(bnoordhuis) Also check URI SANs that are IP addresses.
|
|
|
|
} else if (subject) {
|
2018-05-24 13:14:27 +02:00
|
|
|
hostname = unfqdn(hostname); // Remove trailing dot for error messages.
|
|
|
|
const hostParts = splitHost(hostname);
|
2016-08-15 18:46:27 +02:00
|
|
|
const wildcard = (pattern) => check(hostParts, pattern, true);
|
|
|
|
const noWildcard = (pattern) => check(hostParts, pattern, false);
|
|
|
|
|
|
|
|
// Match against Common Name only if no supported identifiers are present.
|
|
|
|
if (dnsNames.length === 0 && ips.length === 0 && uriNames.length === 0) {
|
|
|
|
const cn = subject.CN;
|
|
|
|
|
|
|
|
if (Array.isArray(cn))
|
|
|
|
valid = cn.some(wildcard);
|
|
|
|
else if (cn)
|
|
|
|
valid = wildcard(cn);
|
|
|
|
|
|
|
|
if (!valid)
|
2018-05-24 13:14:27 +02:00
|
|
|
reason = `Host: ${hostname}. is not cert's CN: ${cn}`;
|
2016-08-15 18:46:27 +02:00
|
|
|
} else {
|
|
|
|
valid = dnsNames.some(wildcard) || uriNames.some(noWildcard);
|
|
|
|
if (!valid)
|
2018-05-24 13:14:27 +02:00
|
|
|
reason =
|
|
|
|
`Host: ${hostname}. is not in the cert's altnames: ${altNames}`;
|
2014-04-14 18:08:38 +02:00
|
|
|
}
|
2015-08-10 17:55:37 +02:00
|
|
|
} else {
|
|
|
|
reason = 'Cert is empty';
|
2012-07-11 21:54:20 +02:00
|
|
|
}
|
|
|
|
|
2014-04-14 18:08:38 +02:00
|
|
|
if (!valid) {
|
2019-03-18 15:41:19 +01:00
|
|
|
return new ERR_TLS_CERT_ALTNAME_INVALID(reason, hostname, cert);
|
2014-04-14 18:08:38 +02:00
|
|
|
}
|
2010-12-09 09:10:16 +01:00
|
|
|
};
|
|
|
|
|
2017-12-23 05:55:37 +01:00
|
|
|
|
|
|
|
class SecurePair extends EventEmitter {
|
|
|
|
constructor(secureContext = exports.createSecureContext(),
|
|
|
|
isServer = false,
|
|
|
|
requestCert = !isServer,
|
|
|
|
rejectUnauthorized = false,
|
|
|
|
options = {}) {
|
|
|
|
super();
|
|
|
|
const { socket1, socket2 } = new DuplexPair();
|
|
|
|
|
|
|
|
this.server = options.server;
|
|
|
|
this.credentials = secureContext;
|
|
|
|
|
|
|
|
this.encrypted = socket1;
|
2018-12-18 02:28:09 +01:00
|
|
|
this.cleartext = new exports.TLSSocket(socket2, {
|
|
|
|
secureContext,
|
|
|
|
isServer,
|
|
|
|
requestCert,
|
|
|
|
rejectUnauthorized,
|
|
|
|
...options
|
|
|
|
});
|
2017-12-23 05:55:37 +01:00
|
|
|
this.cleartext.once('secure', () => this.emit('secure'));
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.cleartext.destroy();
|
|
|
|
this.encrypted.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-08 09:58:54 +02:00
|
|
|
exports.parseCertString = internalUtil.deprecate(
|
|
|
|
internalTLS.parseCertString,
|
|
|
|
'tls.parseCertString() is deprecated. ' +
|
|
|
|
'Please use querystring.parse() instead.',
|
2017-09-21 18:42:06 +02:00
|
|
|
'DEP0076');
|
2011-01-07 19:57:39 +01:00
|
|
|
|
2018-04-17 15:07:34 +02:00
|
|
|
exports.createSecureContext = _tls_common.createSecureContext;
|
|
|
|
exports.SecureContext = _tls_common.SecureContext;
|
|
|
|
exports.TLSSocket = _tls_wrap.TLSSocket;
|
|
|
|
exports.Server = _tls_wrap.Server;
|
|
|
|
exports.createServer = _tls_wrap.createServer;
|
|
|
|
exports.connect = _tls_wrap.connect;
|
2017-02-16 23:30:29 +01:00
|
|
|
|
2017-12-23 05:55:37 +01:00
|
|
|
exports.createSecurePair = internalUtil.deprecate(
|
|
|
|
function createSecurePair(...args) {
|
|
|
|
return new SecurePair(...args);
|
|
|
|
},
|
|
|
|
'tls.createSecurePair() is deprecated. Please use ' +
|
|
|
|
'tls.TLSSocket instead.', 'DEP0064');
|