2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-08 17:18:26 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2017-01-16 10:19:32 +01:00
|
|
|
internalUtil.assertCrypto();
|
2016-03-09 00:31:31 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const net = require('net');
|
|
|
|
const url = require('url');
|
2015-04-07 10:37:13 +02:00
|
|
|
const binding = process.binding('crypto');
|
2015-05-29 19:35:43 +02:00
|
|
|
const Buffer = require('buffer').Buffer;
|
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
|
|
|
|
// renegotations are seen. The settings are applied to all remote client
|
|
|
|
// 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;
|
|
|
|
|
2013-01-22 14:27:05 +01:00
|
|
|
exports.SLAB_BUFFER_SIZE = 10 * 1024 * 1024;
|
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
exports.DEFAULT_CIPHERS =
|
|
|
|
process.binding('constants').crypto.defaultCipherList;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
|
|
|
exports.DEFAULT_ECDH_CURVE = 'prime256v1';
|
|
|
|
|
2017-01-13 23:28:35 +01:00
|
|
|
exports.getCiphers = internalUtil.cachedResult(
|
|
|
|
() => internalUtil.filterDuplicateStrings(binding.getSSLCiphers(), true)
|
|
|
|
);
|
2013-03-19 00:16:55 +01:00
|
|
|
|
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) => {
|
|
|
|
var len = Buffer.byteLength(c);
|
|
|
|
lens[i] = len;
|
|
|
|
return p + 1 + len;
|
2015-04-23 08:25:15 +02:00
|
|
|
}, 0));
|
2011-04-14 05:53:39 +02:00
|
|
|
|
2016-06-09 00:47:18 +02:00
|
|
|
var offset = 0;
|
|
|
|
for (var i = 0, c = protocols.length; i < c; i++) {
|
|
|
|
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
|
|
|
|
2016-05-09 08:04:17 +02:00
|
|
|
exports.convertNPNProtocols = function(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.NPNProtocols = convertProtocols(protocols);
|
|
|
|
} else if (protocols instanceof Buffer) {
|
|
|
|
// Copy new buffer not to be modified by user.
|
|
|
|
out.NPNProtocols = Buffer.from(protocols);
|
2011-04-14 05:53:39 +02:00
|
|
|
}
|
2015-04-23 08:25:15 +02:00
|
|
|
};
|
2011-04-14 05:53:39 +02:00
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
exports.convertALPNProtocols = function(protocols, out) {
|
|
|
|
// If protocols is Array - translate it into buffer
|
|
|
|
if (Array.isArray(protocols)) {
|
2016-08-10 21:16:06 +02:00
|
|
|
out.ALPNProtocols = convertProtocols(protocols);
|
|
|
|
} else if (protocols instanceof Buffer) {
|
|
|
|
// 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.
|
2016-10-01 00:31:47 +02:00
|
|
|
for (var i = hostParts.length - 1; i > 0; i -= 1)
|
2016-08-15 18:46:27 +02:00
|
|
|
if (hostParts[i] !== patternParts[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.checkServerIdentity = function checkServerIdentity(host, cert) {
|
|
|
|
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
|
|
|
|
|
|
|
host = '' + host;
|
|
|
|
|
|
|
|
if (altNames) {
|
|
|
|
for (const name of altNames.split(', ')) {
|
|
|
|
if (name.startsWith('DNS:')) {
|
|
|
|
dnsNames.push(name.slice(4));
|
|
|
|
} else if (name.startsWith('URI:')) {
|
|
|
|
const uri = url.parse(name.slice(4));
|
|
|
|
uriNames.push(uri.hostname); // TODO(bnoordhuis) Also use scheme.
|
|
|
|
} else if (name.startsWith('IP Address:')) {
|
|
|
|
ips.push(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';
|
|
|
|
|
2012-07-11 21:54:20 +02:00
|
|
|
if (net.isIP(host)) {
|
2016-08-15 18:46:27 +02:00
|
|
|
valid = ips.includes(host);
|
|
|
|
if (!valid)
|
2015-10-20 22:29:18 +02:00
|
|
|
reason = `IP: ${host} 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) {
|
|
|
|
host = unfqdn(host); // Remove trailing dot for error messages.
|
|
|
|
const hostParts = splitHost(host);
|
|
|
|
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)
|
|
|
|
reason = `Host: ${host}. is not cert's CN: ${cn}`;
|
|
|
|
} else {
|
|
|
|
valid = dnsNames.some(wildcard) || uriNames.some(noWildcard);
|
|
|
|
if (!valid)
|
|
|
|
reason = `Host: ${host}. 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) {
|
2016-08-15 18:46:27 +02:00
|
|
|
const err = new Error(
|
2015-10-20 22:29:18 +02:00
|
|
|
`Hostname/IP doesn't match certificate's altnames: "${reason}"`);
|
2014-04-14 18:08:38 +02:00
|
|
|
err.reason = reason;
|
|
|
|
err.host = host;
|
|
|
|
err.cert = cert;
|
|
|
|
return err;
|
|
|
|
}
|
2010-12-09 09:10:16 +01:00
|
|
|
};
|
|
|
|
|
2011-08-26 13:00:40 +02:00
|
|
|
// Example:
|
|
|
|
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
|
2013-06-13 15:36:00 +02:00
|
|
|
exports.parseCertString = function parseCertString(s) {
|
2010-12-18 20:18:33 +01:00
|
|
|
var out = {};
|
2011-08-26 13:00:40 +02:00
|
|
|
var parts = s.split('\n');
|
|
|
|
for (var i = 0, len = parts.length; i < len; i++) {
|
2010-12-18 20:18:33 +01:00
|
|
|
var sepIndex = parts[i].indexOf('=');
|
|
|
|
if (sepIndex > 0) {
|
|
|
|
var key = parts[i].slice(0, sepIndex);
|
|
|
|
var value = parts[i].slice(sepIndex + 1);
|
2012-03-03 10:46:45 +01:00
|
|
|
if (key in out) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (!Array.isArray(out[key])) {
|
2012-03-03 10:46:45 +01:00
|
|
|
out[key] = [out[key]];
|
|
|
|
}
|
|
|
|
out[key].push(value);
|
|
|
|
} else {
|
|
|
|
out[key] = value;
|
|
|
|
}
|
2010-12-18 20:18:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2010-12-09 09:35:16 +01:00
|
|
|
};
|
2011-01-07 19:57:39 +01:00
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
// Public API
|
2014-03-07 00:27:01 +01:00
|
|
|
exports.createSecureContext = require('_tls_common').createSecureContext;
|
|
|
|
exports.SecureContext = require('_tls_common').SecureContext;
|
2013-07-03 09:46:01 +02:00
|
|
|
exports.TLSSocket = require('_tls_wrap').TLSSocket;
|
2013-06-13 15:36:00 +02:00
|
|
|
exports.Server = require('_tls_wrap').Server;
|
|
|
|
exports.createServer = require('_tls_wrap').createServer;
|
|
|
|
exports.connect = require('_tls_wrap').connect;
|
2017-02-16 23:30:29 +01:00
|
|
|
|
|
|
|
// Deprecated: DEP0064
|
2014-11-10 06:09:58 +01:00
|
|
|
exports.createSecurePair = require('_tls_legacy').createSecurePair;
|