11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciif (!common.hasCrypto)
251cb0ef41Sopenharmony_ci  common.skip('missing crypto');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst assert = require('assert');
281cb0ef41Sopenharmony_ciconst tls = require('tls');
291cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst passKey = fixtures.readKey('rsa_private_encrypted.pem');
321cb0ef41Sopenharmony_ciconst rawKey = fixtures.readKey('rsa_private.pem');
331cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('rsa_cert.crt');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciassert(Buffer.isBuffer(passKey));
361cb0ef41Sopenharmony_ciassert(Buffer.isBuffer(cert));
371cb0ef41Sopenharmony_ciassert.strictEqual(typeof passKey.toString(), 'string');
381cb0ef41Sopenharmony_ciassert.strictEqual(typeof cert.toString(), 'string');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction onSecureConnect() {
411cb0ef41Sopenharmony_ci  return common.mustCall(function() { this.end(); });
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciconst server = tls.Server({
451cb0ef41Sopenharmony_ci  key: passKey,
461cb0ef41Sopenharmony_ci  passphrase: 'password',
471cb0ef41Sopenharmony_ci  cert: cert,
481cb0ef41Sopenharmony_ci  ca: [cert],
491cb0ef41Sopenharmony_ci  requestCert: true,
501cb0ef41Sopenharmony_ci  rejectUnauthorized: true
511cb0ef41Sopenharmony_ci});
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
541cb0ef41Sopenharmony_ci  // Buffer
551cb0ef41Sopenharmony_ci  tls.connect({
561cb0ef41Sopenharmony_ci    port: this.address().port,
571cb0ef41Sopenharmony_ci    key: passKey,
581cb0ef41Sopenharmony_ci    passphrase: 'password',
591cb0ef41Sopenharmony_ci    cert: cert,
601cb0ef41Sopenharmony_ci    rejectUnauthorized: false
611cb0ef41Sopenharmony_ci  }, onSecureConnect());
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  tls.connect({
641cb0ef41Sopenharmony_ci    port: this.address().port,
651cb0ef41Sopenharmony_ci    key: rawKey,
661cb0ef41Sopenharmony_ci    cert: cert,
671cb0ef41Sopenharmony_ci    rejectUnauthorized: false
681cb0ef41Sopenharmony_ci  }, onSecureConnect());
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  tls.connect({
711cb0ef41Sopenharmony_ci    port: this.address().port,
721cb0ef41Sopenharmony_ci    key: rawKey,
731cb0ef41Sopenharmony_ci    passphrase: 'ignored',
741cb0ef41Sopenharmony_ci    cert: cert,
751cb0ef41Sopenharmony_ci    rejectUnauthorized: false
761cb0ef41Sopenharmony_ci  }, onSecureConnect());
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  // Buffer[]
791cb0ef41Sopenharmony_ci  tls.connect({
801cb0ef41Sopenharmony_ci    port: this.address().port,
811cb0ef41Sopenharmony_ci    key: [passKey],
821cb0ef41Sopenharmony_ci    passphrase: 'password',
831cb0ef41Sopenharmony_ci    cert: [cert],
841cb0ef41Sopenharmony_ci    rejectUnauthorized: false
851cb0ef41Sopenharmony_ci  }, onSecureConnect());
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  tls.connect({
881cb0ef41Sopenharmony_ci    port: this.address().port,
891cb0ef41Sopenharmony_ci    key: [rawKey],
901cb0ef41Sopenharmony_ci    cert: [cert],
911cb0ef41Sopenharmony_ci    rejectUnauthorized: false
921cb0ef41Sopenharmony_ci  }, onSecureConnect());
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  tls.connect({
951cb0ef41Sopenharmony_ci    port: this.address().port,
961cb0ef41Sopenharmony_ci    key: [rawKey],
971cb0ef41Sopenharmony_ci    passphrase: 'ignored',
981cb0ef41Sopenharmony_ci    cert: [cert],
991cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1001cb0ef41Sopenharmony_ci  }, onSecureConnect());
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  // string
1031cb0ef41Sopenharmony_ci  tls.connect({
1041cb0ef41Sopenharmony_ci    port: this.address().port,
1051cb0ef41Sopenharmony_ci    key: passKey.toString(),
1061cb0ef41Sopenharmony_ci    passphrase: 'password',
1071cb0ef41Sopenharmony_ci    cert: cert.toString(),
1081cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1091cb0ef41Sopenharmony_ci  }, onSecureConnect());
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  tls.connect({
1121cb0ef41Sopenharmony_ci    port: this.address().port,
1131cb0ef41Sopenharmony_ci    key: rawKey.toString(),
1141cb0ef41Sopenharmony_ci    cert: cert.toString(),
1151cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1161cb0ef41Sopenharmony_ci  }, onSecureConnect());
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  tls.connect({
1191cb0ef41Sopenharmony_ci    port: this.address().port,
1201cb0ef41Sopenharmony_ci    key: rawKey.toString(),
1211cb0ef41Sopenharmony_ci    passphrase: 'ignored',
1221cb0ef41Sopenharmony_ci    cert: cert.toString(),
1231cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1241cb0ef41Sopenharmony_ci  }, onSecureConnect());
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  // String[]
1271cb0ef41Sopenharmony_ci  tls.connect({
1281cb0ef41Sopenharmony_ci    port: this.address().port,
1291cb0ef41Sopenharmony_ci    key: [passKey.toString()],
1301cb0ef41Sopenharmony_ci    passphrase: 'password',
1311cb0ef41Sopenharmony_ci    cert: [cert.toString()],
1321cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1331cb0ef41Sopenharmony_ci  }, onSecureConnect());
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  tls.connect({
1361cb0ef41Sopenharmony_ci    port: this.address().port,
1371cb0ef41Sopenharmony_ci    key: [rawKey.toString()],
1381cb0ef41Sopenharmony_ci    cert: [cert.toString()],
1391cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1401cb0ef41Sopenharmony_ci  }, onSecureConnect());
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  tls.connect({
1431cb0ef41Sopenharmony_ci    port: this.address().port,
1441cb0ef41Sopenharmony_ci    key: [rawKey.toString()],
1451cb0ef41Sopenharmony_ci    passphrase: 'ignored',
1461cb0ef41Sopenharmony_ci    cert: [cert.toString()],
1471cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1481cb0ef41Sopenharmony_ci  }, onSecureConnect());
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  // Object[]
1511cb0ef41Sopenharmony_ci  tls.connect({
1521cb0ef41Sopenharmony_ci    port: this.address().port,
1531cb0ef41Sopenharmony_ci    key: [{ pem: passKey, passphrase: 'password' }],
1541cb0ef41Sopenharmony_ci    cert: cert,
1551cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1561cb0ef41Sopenharmony_ci  }, onSecureConnect());
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  tls.connect({
1591cb0ef41Sopenharmony_ci    port: this.address().port,
1601cb0ef41Sopenharmony_ci    key: [{ pem: passKey, passphrase: 'password' }],
1611cb0ef41Sopenharmony_ci    passphrase: 'ignored',
1621cb0ef41Sopenharmony_ci    cert: cert,
1631cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1641cb0ef41Sopenharmony_ci  }, onSecureConnect());
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  tls.connect({
1671cb0ef41Sopenharmony_ci    port: this.address().port,
1681cb0ef41Sopenharmony_ci    key: [{ pem: passKey }],
1691cb0ef41Sopenharmony_ci    passphrase: 'password',
1701cb0ef41Sopenharmony_ci    cert: cert,
1711cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1721cb0ef41Sopenharmony_ci  }, onSecureConnect());
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  tls.connect({
1751cb0ef41Sopenharmony_ci    port: this.address().port,
1761cb0ef41Sopenharmony_ci    key: [{ pem: passKey.toString(), passphrase: 'password' }],
1771cb0ef41Sopenharmony_ci    cert: cert,
1781cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1791cb0ef41Sopenharmony_ci  }, onSecureConnect());
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci  tls.connect({
1821cb0ef41Sopenharmony_ci    port: this.address().port,
1831cb0ef41Sopenharmony_ci    key: [{ pem: rawKey, passphrase: 'ignored' }],
1841cb0ef41Sopenharmony_ci    cert: cert,
1851cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1861cb0ef41Sopenharmony_ci  }, onSecureConnect());
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  tls.connect({
1891cb0ef41Sopenharmony_ci    port: this.address().port,
1901cb0ef41Sopenharmony_ci    key: [{ pem: rawKey.toString(), passphrase: 'ignored' }],
1911cb0ef41Sopenharmony_ci    cert: cert,
1921cb0ef41Sopenharmony_ci    rejectUnauthorized: false
1931cb0ef41Sopenharmony_ci  }, onSecureConnect());
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  tls.connect({
1961cb0ef41Sopenharmony_ci    port: this.address().port,
1971cb0ef41Sopenharmony_ci    key: [{ pem: rawKey }],
1981cb0ef41Sopenharmony_ci    passphrase: 'ignored',
1991cb0ef41Sopenharmony_ci    cert: cert,
2001cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2011cb0ef41Sopenharmony_ci  }, onSecureConnect());
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci  tls.connect({
2041cb0ef41Sopenharmony_ci    port: this.address().port,
2051cb0ef41Sopenharmony_ci    key: [{ pem: rawKey.toString() }],
2061cb0ef41Sopenharmony_ci    passphrase: 'ignored',
2071cb0ef41Sopenharmony_ci    cert: cert,
2081cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2091cb0ef41Sopenharmony_ci  }, onSecureConnect());
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  tls.connect({
2121cb0ef41Sopenharmony_ci    port: this.address().port,
2131cb0ef41Sopenharmony_ci    key: [{ pem: rawKey }],
2141cb0ef41Sopenharmony_ci    cert: cert,
2151cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2161cb0ef41Sopenharmony_ci  }, onSecureConnect());
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci  tls.connect({
2191cb0ef41Sopenharmony_ci    port: this.address().port,
2201cb0ef41Sopenharmony_ci    key: [{ pem: rawKey.toString() }],
2211cb0ef41Sopenharmony_ci    cert: cert,
2221cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2231cb0ef41Sopenharmony_ci  }, onSecureConnect());
2241cb0ef41Sopenharmony_ci})).unref();
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ciconst errMessageDecrypt = /bad decrypt/;
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci// Missing passphrase
2291cb0ef41Sopenharmony_ciassert.throws(function() {
2301cb0ef41Sopenharmony_ci  tls.connect({
2311cb0ef41Sopenharmony_ci    port: server.address().port,
2321cb0ef41Sopenharmony_ci    key: passKey,
2331cb0ef41Sopenharmony_ci    cert: cert,
2341cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2351cb0ef41Sopenharmony_ci  });
2361cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ciassert.throws(function() {
2391cb0ef41Sopenharmony_ci  tls.connect({
2401cb0ef41Sopenharmony_ci    port: server.address().port,
2411cb0ef41Sopenharmony_ci    key: [passKey],
2421cb0ef41Sopenharmony_ci    cert: cert,
2431cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2441cb0ef41Sopenharmony_ci  });
2451cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ciassert.throws(function() {
2481cb0ef41Sopenharmony_ci  tls.connect({
2491cb0ef41Sopenharmony_ci    port: server.address().port,
2501cb0ef41Sopenharmony_ci    key: [{ pem: passKey }],
2511cb0ef41Sopenharmony_ci    cert: cert,
2521cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2531cb0ef41Sopenharmony_ci  });
2541cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci// Invalid passphrase
2571cb0ef41Sopenharmony_ciassert.throws(function() {
2581cb0ef41Sopenharmony_ci  tls.connect({
2591cb0ef41Sopenharmony_ci    port: server.address().port,
2601cb0ef41Sopenharmony_ci    key: passKey,
2611cb0ef41Sopenharmony_ci    passphrase: 'invalid',
2621cb0ef41Sopenharmony_ci    cert: cert,
2631cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2641cb0ef41Sopenharmony_ci  });
2651cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ciassert.throws(function() {
2681cb0ef41Sopenharmony_ci  tls.connect({
2691cb0ef41Sopenharmony_ci    port: server.address().port,
2701cb0ef41Sopenharmony_ci    key: [passKey],
2711cb0ef41Sopenharmony_ci    passphrase: 'invalid',
2721cb0ef41Sopenharmony_ci    cert: cert,
2731cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2741cb0ef41Sopenharmony_ci  });
2751cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ciassert.throws(function() {
2781cb0ef41Sopenharmony_ci  tls.connect({
2791cb0ef41Sopenharmony_ci    port: server.address().port,
2801cb0ef41Sopenharmony_ci    key: [{ pem: passKey }],
2811cb0ef41Sopenharmony_ci    passphrase: 'invalid',
2821cb0ef41Sopenharmony_ci    cert: cert,
2831cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2841cb0ef41Sopenharmony_ci  });
2851cb0ef41Sopenharmony_ci}, errMessageDecrypt);
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ciassert.throws(function() {
2881cb0ef41Sopenharmony_ci  tls.connect({
2891cb0ef41Sopenharmony_ci    port: server.address().port,
2901cb0ef41Sopenharmony_ci    key: [{ pem: passKey, passphrase: 'invalid' }],
2911cb0ef41Sopenharmony_ci    passphrase: 'password', // Valid but unused
2921cb0ef41Sopenharmony_ci    cert: cert,
2931cb0ef41Sopenharmony_ci    rejectUnauthorized: false
2941cb0ef41Sopenharmony_ci  });
2951cb0ef41Sopenharmony_ci}, errMessageDecrypt);
296