@@ -73,50 +73,60 @@ exports.createSecureContext = function createSecureContext(options, context) {
7373
7474 var c = new SecureContext ( options . secureProtocol , secureOptions , context ) ;
7575 var i ;
76+ var val ;
7677
7778 if ( context ) return c ;
7879
7980 // NOTE: It's important to add CA before the cert to be able to load
8081 // cert's issuer in C++ code.
81- if ( options . ca ) {
82- if ( Array . isArray ( options . ca ) ) {
83- options . ca . forEach ( ( ca ) => {
84- validateKeyCert ( ca , 'ca' ) ;
85- c . context . addCACert ( ca ) ;
86- } ) ;
82+ var ca = options . ca ;
83+ if ( ca !== undefined ) {
84+ if ( Array . isArray ( ca ) ) {
85+ for ( i = 0 ; i < ca . length ; ++ i ) {
86+ val = ca [ i ] ;
87+ validateKeyCert ( val , 'ca' ) ;
88+ c . context . addCACert ( val ) ;
89+ }
8790 } else {
88- validateKeyCert ( options . ca , 'ca' ) ;
89- c . context . addCACert ( options . ca ) ;
91+ validateKeyCert ( ca , 'ca' ) ;
92+ c . context . addCACert ( ca ) ;
9093 }
9194 } else {
9295 c . context . addRootCerts ( ) ;
9396 }
9497
95- if ( options . cert ) {
96- if ( Array . isArray ( options . cert ) ) {
97- options . cert . forEach ( ( cert ) => {
98- validateKeyCert ( cert , 'cert' ) ;
99- c . context . setCert ( cert ) ;
100- } ) ;
98+ var cert = options . cert ;
99+ if ( cert !== undefined ) {
100+ if ( Array . isArray ( cert ) ) {
101+ for ( i = 0 ; i < cert . length ; ++ i ) {
102+ val = cert [ i ] ;
103+ validateKeyCert ( val , 'cert' ) ;
104+ c . context . setCert ( val ) ;
105+ }
101106 } else {
102- validateKeyCert ( options . cert , 'cert' ) ;
103- c . context . setCert ( options . cert ) ;
107+ validateKeyCert ( cert , 'cert' ) ;
108+ c . context . setCert ( cert ) ;
104109 }
105110 }
106111
107112 // NOTE: It is important to set the key after the cert.
108113 // `ssl_set_pkey` returns `0` when the key does not match the cert, but
109114 // `ssl_set_cert` returns `1` and nullifies the key in the SSL structure
110115 // which leads to the crash later on.
111- if ( options . key ) {
112- if ( Array . isArray ( options . key ) ) {
113- options . key . forEach ( ( k ) => {
114- validateKeyCert ( k . pem || k , 'key' ) ;
115- c . context . setKey ( k . pem || k , k . passphrase || options . passphrase ) ;
116- } ) ;
116+ var key = options . key ;
117+ var passphrase = options . passphrase ;
118+ if ( key !== undefined ) {
119+ if ( Array . isArray ( key ) ) {
120+ for ( i = 0 ; i < key . length ; ++ i ) {
121+ val = key [ i ] ;
122+ // eslint-disable-next-line eqeqeq
123+ const pem = ( val != undefined && val . pem !== undefined ? val . pem : val ) ;
124+ validateKeyCert ( pem , 'key' ) ;
125+ c . context . setKey ( pem , val . passphrase || passphrase ) ;
126+ }
117127 } else {
118- validateKeyCert ( options . key , 'key' ) ;
119- c . context . setKey ( options . key , options . passphrase ) ;
128+ validateKeyCert ( key , 'key' ) ;
129+ c . context . setKey ( key , passphrase ) ;
120130 }
121131 }
122132
@@ -152,7 +162,6 @@ exports.createSecureContext = function createSecureContext(options, context) {
152162
153163 if ( options . pfx ) {
154164 var pfx = options . pfx ;
155- var passphrase = options . passphrase ;
156165
157166 if ( ! crypto )
158167 crypto = require ( 'crypto' ) ;
0 commit comments