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');
|
2015-01-21 17:36:59 +01:00
|
|
|
const tls = require('tls');
|
2017-08-13 16:24:12 +02:00
|
|
|
const errors = require('internal/errors');
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2016-05-02 19:27:12 +02:00
|
|
|
const SSL_OP_CIPHER_SERVER_PREFERENCE =
|
|
|
|
process.binding('constants').crypto.SSL_OP_CIPHER_SERVER_PREFERENCE;
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
// Lazily loaded
|
|
|
|
var crypto = null;
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const binding = process.binding('crypto');
|
|
|
|
const NativeSecureContext = binding.SecureContext;
|
2014-03-07 00:27:01 +01:00
|
|
|
|
2016-11-22 22:23:06 +01:00
|
|
|
function SecureContext(secureProtocol, secureOptions, context) {
|
2014-03-07 00:27:01 +01:00
|
|
|
if (!(this instanceof SecureContext)) {
|
2016-11-22 22:23:06 +01:00
|
|
|
return new SecureContext(secureProtocol, secureOptions, context);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
this.context = context;
|
|
|
|
} else {
|
|
|
|
this.context = new NativeSecureContext();
|
|
|
|
|
|
|
|
if (secureProtocol) {
|
|
|
|
this.context.init(secureProtocol);
|
|
|
|
} else {
|
|
|
|
this.context.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:23:06 +01:00
|
|
|
if (secureOptions) this.context.setOptions(secureOptions);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 16:24:12 +02:00
|
|
|
function validateKeyCert(value, type) {
|
|
|
|
if (typeof value !== 'string' && !ArrayBuffer.isView(value))
|
|
|
|
throw new errors.TypeError(
|
|
|
|
'ERR_INVALID_ARG_TYPE', type,
|
|
|
|
['string', 'Buffer', 'TypedArray', 'DataView']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
exports.SecureContext = SecureContext;
|
|
|
|
|
|
|
|
|
|
|
|
exports.createSecureContext = function createSecureContext(options, context) {
|
|
|
|
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
|
|
|
|
|
|
|
var c = new SecureContext(options.secureProtocol, secureOptions, context);
|
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
|
|
|
|
|
|
|
if (context) return c;
|
|
|
|
|
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.
|
2017-08-27 17:03:45 +02:00
|
|
|
var ca = options.ca;
|
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];
|
|
|
|
validateKeyCert(val, 'ca');
|
|
|
|
c.context.addCACert(val);
|
|
|
|
}
|
2014-03-07 00:27:01 +01:00
|
|
|
} else {
|
2017-08-27 17:03:45 +02:00
|
|
|
validateKeyCert(ca, 'ca');
|
|
|
|
c.context.addCACert(ca);
|
2014-03-07 00:27:01 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addRootCerts();
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:03:45 +02:00
|
|
|
var cert = options.cert;
|
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];
|
|
|
|
validateKeyCert(val, 'cert');
|
|
|
|
c.context.setCert(val);
|
|
|
|
}
|
2014-09-01 16:44:57 +02:00
|
|
|
} else {
|
2017-08-27 17:03:45 +02:00
|
|
|
validateKeyCert(cert, 'cert');
|
|
|
|
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);
|
|
|
|
validateKeyCert(pem, 'key');
|
|
|
|
c.context.setKey(pem, val.passphrase || passphrase);
|
|
|
|
}
|
2014-11-24 14:17:13 +01:00
|
|
|
} else {
|
2017-08-27 17:03:45 +02:00
|
|
|
validateKeyCert(key, 'key');
|
|
|
|
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) {
|
|
|
|
if (!crypto)
|
|
|
|
crypto = require('crypto');
|
|
|
|
|
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;
|
|
|
|
const buf = crypto._toBuf(raw);
|
|
|
|
const passphrase = pfx.passphrase || options.passphrase;
|
|
|
|
if (passphrase) {
|
|
|
|
c.context.loadPKCS12(buf, crypto._toBuf(passphrase));
|
|
|
|
} else {
|
|
|
|
c.context.loadPKCS12(buf);
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 00:27:01 +01:00
|
|
|
} else {
|
2017-08-12 18:06:35 +02:00
|
|
|
const buf = crypto._toBuf(options.pfx);
|
|
|
|
const passphrase = options.passphrase;
|
|
|
|
if (passphrase) {
|
|
|
|
c.context.loadPKCS12(buf, crypto._toBuf(passphrase));
|
|
|
|
} 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
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
return c;
|
|
|
|
};
|
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?
|
|
|
|
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g, function(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;
|
|
|
|
};
|