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';
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const tls = require('tls');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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.
|
2014-03-07 00:27:01 +01:00
|
|
|
if (options.ca) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (Array.isArray(options.ca)) {
|
2016-09-26 05:59:01 +02:00
|
|
|
for (i = 0; i < options.ca.length; i++) {
|
2014-03-07 00:27:01 +01:00
|
|
|
c.context.addCACert(options.ca[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addCACert(options.ca);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addRootCerts();
|
|
|
|
}
|
|
|
|
|
2014-09-01 16:44:57 +02:00
|
|
|
if (options.cert) {
|
|
|
|
if (Array.isArray(options.cert)) {
|
2016-09-26 05:59:01 +02:00
|
|
|
for (i = 0; i < options.cert.length; i++)
|
2014-09-01 16:44:57 +02:00
|
|
|
c.context.setCert(options.cert[i]);
|
|
|
|
} else {
|
|
|
|
c.context.setCert(options.cert);
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
// `ssl_set_pkey` returns `0` when the key does not much the cert, but
|
|
|
|
// `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
|
|
|
|
// which leads to the crash later on.
|
|
|
|
if (options.key) {
|
|
|
|
if (Array.isArray(options.key)) {
|
2016-09-26 05:59:01 +02:00
|
|
|
for (i = 0; i < options.key.length; i++) {
|
|
|
|
const key = options.key[i];
|
2016-12-15 21:47:36 +01:00
|
|
|
const passphrase = key.passphrase || options.passphrase;
|
|
|
|
c.context.setKey(key.pem || key, passphrase);
|
2014-11-24 14:17:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-15 21:47:36 +01:00
|
|
|
c.context.setKey(options.key, options.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) {
|
|
|
|
var pfx = options.pfx;
|
|
|
|
var passphrase = options.passphrase;
|
|
|
|
|
|
|
|
if (!crypto)
|
|
|
|
crypto = require('crypto');
|
|
|
|
|
|
|
|
pfx = crypto._toBuf(pfx);
|
|
|
|
if (passphrase)
|
|
|
|
passphrase = crypto._toBuf(passphrase);
|
|
|
|
|
|
|
|
if (passphrase) {
|
|
|
|
c.context.loadPKCS12(pfx, passphrase);
|
|
|
|
} else {
|
|
|
|
c.context.loadPKCS12(pfx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (c.issuer) c.issuer = tls.parseCertString(c.issuer);
|
2014-04-17 13:57:36 +02:00
|
|
|
if (c.issuerCertificate && c.issuerCertificate !== c) {
|
|
|
|
c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
|
|
|
|
}
|
2014-04-14 19:15:57 +02:00
|
|
|
if (c.subject) c.subject = tls.parseCertString(c.subject);
|
|
|
|
if (c.infoAccess) {
|
|
|
|
var info = c.infoAccess;
|
|
|
|
c.infoAccess = {};
|
|
|
|
|
|
|
|
// XXX: More key validation?
|
|
|
|
info.replace(/([^\n:]*):([^\n]*)(?:\n|$)/g, function(all, key, val) {
|
|
|
|
if (key === '__proto__')
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c.infoAccess.hasOwnProperty(key))
|
|
|
|
c.infoAccess[key].push(val);
|
|
|
|
else
|
|
|
|
c.infoAccess[key] = [val];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
};
|