tls: replace forEach with for

PR-URL: https://github.com/nodejs/node/pull/15053
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Brian White 2017-08-27 11:03:45 -04:00 committed by Ruben Bridgewater
parent 67d792a74f
commit 5723c4c5f0
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -73,34 +73,39 @@ exports.createSecureContext = function createSecureContext(options, context) {
var c = new SecureContext(options.secureProtocol, secureOptions, context); var c = new SecureContext(options.secureProtocol, secureOptions, context);
var i; var i;
var val;
if (context) return c; if (context) return c;
// NOTE: It's important to add CA before the cert to be able to load // NOTE: It's important to add CA before the cert to be able to load
// cert's issuer in C++ code. // cert's issuer in C++ code.
if (options.ca) { var ca = options.ca;
if (Array.isArray(options.ca)) { if (ca !== undefined) {
options.ca.forEach((ca) => { if (Array.isArray(ca)) {
validateKeyCert(ca, 'ca'); for (i = 0; i < ca.length; ++i) {
c.context.addCACert(ca); val = ca[i];
}); validateKeyCert(val, 'ca');
c.context.addCACert(val);
}
} else { } else {
validateKeyCert(options.ca, 'ca'); validateKeyCert(ca, 'ca');
c.context.addCACert(options.ca); c.context.addCACert(ca);
} }
} else { } else {
c.context.addRootCerts(); c.context.addRootCerts();
} }
if (options.cert) { var cert = options.cert;
if (Array.isArray(options.cert)) { if (cert !== undefined) {
options.cert.forEach((cert) => { if (Array.isArray(cert)) {
validateKeyCert(cert, 'cert'); for (i = 0; i < cert.length; ++i) {
c.context.setCert(cert); val = cert[i];
}); validateKeyCert(val, 'cert');
c.context.setCert(val);
}
} else { } else {
validateKeyCert(options.cert, 'cert'); validateKeyCert(cert, 'cert');
c.context.setCert(options.cert); c.context.setCert(cert);
} }
} }
@ -108,15 +113,20 @@ exports.createSecureContext = function createSecureContext(options, context) {
// `ssl_set_pkey` returns `0` when the key does not match the cert, but // `ssl_set_pkey` returns `0` when the key does not match the cert, but
// `ssl_set_cert` returns `1` and nullifies the key in the SSL structure // `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
// which leads to the crash later on. // which leads to the crash later on.
if (options.key) { var key = options.key;
if (Array.isArray(options.key)) { var passphrase = options.passphrase;
options.key.forEach((k) => { if (key !== undefined) {
validateKeyCert(k.pem || k, 'key'); if (Array.isArray(key)) {
c.context.setKey(k.pem || k, k.passphrase || options.passphrase); 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);
}
} else { } else {
validateKeyCert(options.key, 'key'); validateKeyCert(key, 'key');
c.context.setKey(options.key, options.passphrase); c.context.setKey(key, passphrase);
} }
} }
@ -152,7 +162,6 @@ exports.createSecureContext = function createSecureContext(options, context) {
if (options.pfx) { if (options.pfx) {
var pfx = options.pfx; var pfx = options.pfx;
var passphrase = options.passphrase;
if (!crypto) if (!crypto)
crypto = require('crypto'); crypto = require('crypto');