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';
|
|
|
|
|
2017-09-08 09:58:54 +02:00
|
|
|
const { parseCertString } = require('internal/tls');
|
2017-09-28 09:16:41 +02:00
|
|
|
const { isArrayBufferView } = require('internal/util/types');
|
2015-01-21 17:36:59 +01:00
|
|
|
const tls = require('tls');
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
|
|
|
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED,
|
|
|
|
ERR_INVALID_ARG_TYPE
|
|
|
|
} = require('internal/errors').codes;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2018-10-15 01:41:32 +02:00
|
|
|
const { SSL_OP_CIPHER_SERVER_PREFERENCE } = internalBinding('constants').crypto;
|
2016-05-02 19:27:12 +02:00
|
|
|
|
2018-08-24 10:36:43 +02:00
|
|
|
// Lazily loaded from internal/crypto/util.
|
|
|
|
let toBuf = null;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2018-08-21 08:54:02 +02:00
|
|
|
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
|
2018-11-08 01:36:19 +01:00
|
|
|
function SecureContext(secureProtocol, secureOptions) {
|
2014-03-07 00:27:01 +01:00
|
|
|
if (!(this instanceof SecureContext)) {
|
2018-11-08 01:36:19 +01:00
|
|
|
return new SecureContext(secureProtocol, secureOptions);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 01:36:19 +01:00
|
|
|
this.context = new NativeSecureContext();
|
|
|
|
this.context.init(secureProtocol);
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2016-11-22 22:23:06 +01:00
|
|
|
if (secureOptions) this.context.setOptions(secureOptions);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
|
2018-04-25 13:58:32 +02:00
|
|
|
function validateKeyCert(name, value) {
|
2018-03-19 13:33:46 +01:00
|
|
|
if (typeof value !== 'string' && !isArrayBufferView(value)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2018-04-25 13:58:32 +02:00
|
|
|
`options.${name}`,
|
2018-03-19 13:33:46 +01:00
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView'],
|
|
|
|
value
|
2017-08-13 16:24:12 +02:00
|
|
|
);
|
2018-03-19 13:33:46 +01:00
|
|
|
}
|
2017-08-13 16:24:12 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
exports.SecureContext = SecureContext;
|
|
|
|
|
|
|
|
|
2018-11-08 01:36:19 +01:00
|
|
|
exports.createSecureContext = function createSecureContext(options) {
|
2014-03-07 00:27:01 +01:00
|
|
|
if (!options) options = {};
|
|
|
|
|
2014-06-25 12:47:59 +02:00
|
|
|
var secureOptions = options.secureOptions;
|
|
|
|
if (options.honorCipherOrder)
|
2016-05-02 19:27:12 +02:00
|
|
|
secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
|
2014-06-25 12:47:59 +02:00
|
|
|
|
2018-11-08 01:36:19 +01:00
|
|
|
const c = new SecureContext(options.secureProtocol, secureOptions);
|
2016-09-26 05:59:01 +02:00
|
|
|
var i;
|
2017-08-27 17:03:45 +02:00
|
|
|
var val;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2014-04-14 19:15:57 +02:00
|
|
|
// NOTE: It's important to add CA before the cert to be able to load
|
|
|
|
// cert's issuer in C++ code.
|
2018-04-16 14:59:51 +02:00
|
|
|
const { ca } = options;
|
2017-09-01 16:14:56 +02:00
|
|
|
if (ca) {
|
2017-08-27 17:03:45 +02:00
|
|
|
if (Array.isArray(ca)) {
|
|
|
|
for (i = 0; i < ca.length; ++i) {
|
|
|
|
val = ca[i];
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('ca', val);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.addCACert(val);
|
|
|
|
}
|
2014-03-07 00:27:01 +01:00
|
|
|
} else {
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('ca', ca);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.addCACert(ca);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addRootCerts();
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:59:51 +02:00
|
|
|
const { cert } = options;
|
2017-09-01 16:14:56 +02:00
|
|
|
if (cert) {
|
2017-08-27 17:03:45 +02:00
|
|
|
if (Array.isArray(cert)) {
|
|
|
|
for (i = 0; i < cert.length; ++i) {
|
|
|
|
val = cert[i];
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('cert', val);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.setCert(val);
|
|
|
|
}
|
2014-09-01 16:44:57 +02:00
|
|
|
} else {
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('cert', cert);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.setCert(cert);
|
2014-09-01 16:44:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 19:15:57 +02:00
|
|
|
|
2014-11-24 14:17:13 +01:00
|
|
|
// NOTE: It is important to set the key after the cert.
|
2017-06-13 10:02:33 +02:00
|
|
|
// `ssl_set_pkey` returns `0` when the key does not match the cert, but
|
2014-11-24 14:17:13 +01:00
|
|
|
// `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
|
|
|
|
// which leads to the crash later on.
|
2017-08-27 17:03:45 +02:00
|
|
|
var key = options.key;
|
|
|
|
var passphrase = options.passphrase;
|
2017-09-01 16:14:56 +02:00
|
|
|
if (key) {
|
2017-08-27 17:03:45 +02:00
|
|
|
if (Array.isArray(key)) {
|
|
|
|
for (i = 0; i < key.length; ++i) {
|
|
|
|
val = key[i];
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
const pem = (val != undefined && val.pem !== undefined ? val.pem : val);
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('key', pem);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.setKey(pem, val.passphrase || passphrase);
|
|
|
|
}
|
2014-11-24 14:17:13 +01:00
|
|
|
} else {
|
2018-04-25 13:58:32 +02:00
|
|
|
validateKeyCert('key', key);
|
2017-08-27 17:03:45 +02:00
|
|
|
c.context.setKey(key, passphrase);
|
2014-11-24 14:17:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 19:15:57 +02:00
|
|
|
if (options.ciphers)
|
|
|
|
c.context.setCiphers(options.ciphers);
|
|
|
|
else
|
|
|
|
c.context.setCiphers(tls.DEFAULT_CIPHERS);
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (options.ecdhCurve === undefined)
|
2014-04-14 19:15:57 +02:00
|
|
|
c.context.setECDHCurve(tls.DEFAULT_ECDH_CURVE);
|
|
|
|
else if (options.ecdhCurve)
|
|
|
|
c.context.setECDHCurve(options.ecdhCurve);
|
|
|
|
|
2015-05-22 11:23:57 +02:00
|
|
|
if (options.dhparam) {
|
2016-09-26 05:59:01 +02:00
|
|
|
const warning = c.context.setDHParam(options.dhparam);
|
2015-05-22 11:23:57 +02:00
|
|
|
if (warning)
|
2017-02-18 00:00:20 +01:00
|
|
|
process.emitWarning(warning, 'SecurityWarning');
|
2015-05-22 11:23:57 +02:00
|
|
|
}
|
2014-08-27 11:00:13 +02:00
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
if (options.crl) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (Array.isArray(options.crl)) {
|
2016-09-26 05:59:01 +02:00
|
|
|
for (i = 0; i < options.crl.length; i++) {
|
2014-03-07 00:27:01 +01:00
|
|
|
c.context.addCRL(options.crl[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addCRL(options.crl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.sessionIdContext) {
|
|
|
|
c.context.setSessionIdContext(options.sessionIdContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.pfx) {
|
2018-08-24 10:36:43 +02:00
|
|
|
if (!toBuf)
|
|
|
|
toBuf = require('internal/crypto/util').toBuf;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2017-08-12 18:06:35 +02:00
|
|
|
if (Array.isArray(options.pfx)) {
|
|
|
|
for (i = 0; i < options.pfx.length; i++) {
|
|
|
|
const pfx = options.pfx[i];
|
|
|
|
const raw = pfx.buf ? pfx.buf : pfx;
|
2018-08-24 10:36:43 +02:00
|
|
|
const buf = toBuf(raw);
|
2017-08-12 18:06:35 +02:00
|
|
|
const passphrase = pfx.passphrase || options.passphrase;
|
|
|
|
if (passphrase) {
|
2018-08-24 10:36:43 +02:00
|
|
|
c.context.loadPKCS12(buf, toBuf(passphrase));
|
2017-08-12 18:06:35 +02:00
|
|
|
} else {
|
|
|
|
c.context.loadPKCS12(buf);
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 00:27:01 +01:00
|
|
|
} else {
|
2018-08-24 10:36:43 +02:00
|
|
|
const buf = toBuf(options.pfx);
|
2017-08-12 18:06:35 +02:00
|
|
|
const passphrase = options.passphrase;
|
|
|
|
if (passphrase) {
|
2018-08-24 10:36:43 +02:00
|
|
|
c.context.loadPKCS12(buf, toBuf(passphrase));
|
2017-08-12 18:06:35 +02:00
|
|
|
} else {
|
|
|
|
c.context.loadPKCS12(buf);
|
|
|
|
}
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:04:40 +01: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.)
|
2015-04-27 09:39:48 +02:00
|
|
|
if (options.singleUse) {
|
|
|
|
c.singleUse = true;
|
2015-04-26 14:19:38 +02:00
|
|
|
c.context.setFreeListLength(0);
|
2015-04-27 09:39:48 +02:00
|
|
|
}
|
2015-04-26 14:19:38 +02:00
|
|
|
|
2016-04-15 16:49:36 +02:00
|
|
|
if (typeof options.clientCertEngine === 'string') {
|
|
|
|
if (c.context.setClientCertEngine)
|
|
|
|
c.context.setClientCertEngine(options.clientCertEngine);
|
|
|
|
else
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED();
|
2016-04-15 16:49:36 +02:00
|
|
|
} else if (options.clientCertEngine != null) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.clientCertEngine',
|
|
|
|
['string', 'null', 'undefined'],
|
|
|
|
options.clientCertEngine);
|
2016-04-15 16:49:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
return c;
|
|
|
|
};
|
2014-04-14 19:15:57 +02:00
|
|
|
|
2018-11-08 22:40:46 +01: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 19:15:57 +02:00
|
|
|
exports.translatePeerCertificate = function translatePeerCertificate(c) {
|
|
|
|
if (!c)
|
|
|
|
return null;
|
|
|
|
|
2017-09-08 09:58:54 +02:00
|
|
|
if (c.issuer != null) c.issuer = parseCertString(c.issuer);
|
2017-07-24 14:36:36 +02:00
|
|
|
if (c.issuerCertificate != null && c.issuerCertificate !== c) {
|
2014-04-17 13:57:36 +02:00
|
|
|
c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
|
|
|
|
}
|
2017-09-08 09:58:54 +02:00
|
|
|
if (c.subject != null) c.subject = parseCertString(c.subject);
|
2017-07-24 14:36:36 +02:00
|
|
|
if (c.infoAccess != null) {
|
2014-04-14 19:15:57 +02:00
|
|
|
var info = c.infoAccess;
|
2017-07-24 14:36:36 +02:00
|
|
|
c.infoAccess = Object.create(null);
|
2014-04-14 19:15:57 +02:00
|
|
|
|
|
|
|
// XXX: More key validation?
|
2018-06-19 23:03:33 +02:00
|
|
|
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g, (all, key, val) => {
|
2017-07-24 14:36:36 +02:00
|
|
|
if (key in c.infoAccess)
|
2014-04-14 19:15:57 +02:00
|
|
|
c.infoAccess[key].push(val);
|
|
|
|
else
|
|
|
|
c.infoAccess[key] = [val];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
};
|