11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { X509Certificate } = require('crypto');
91cb0ef41Sopenharmony_ciconst tls = require('tls');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent2-key.pem');
121cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent2-cert.pem');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// TODO(@sam-github) test works with TLS1.3, rework test to add
151cb0ef41Sopenharmony_ci//   'ECDH' with 'TLS_AES_128_GCM_SHA256',
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction loadDHParam(n) {
181cb0ef41Sopenharmony_ci  return fixtures.readKey(`dh${n}.pem`);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifunction test(size, type, name, cipher) {
221cb0ef41Sopenharmony_ci  assert(cipher);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  const options = {
251cb0ef41Sopenharmony_ci    key: key,
261cb0ef41Sopenharmony_ci    cert: cert,
271cb0ef41Sopenharmony_ci    ciphers: cipher,
281cb0ef41Sopenharmony_ci    maxVersion: 'TLSv1.2',
291cb0ef41Sopenharmony_ci  };
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  if (name) options.ecdhCurve = name;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  if (type === 'DH') {
341cb0ef41Sopenharmony_ci    if (size === 'auto') {
351cb0ef41Sopenharmony_ci      options.dhparam = 'auto';
361cb0ef41Sopenharmony_ci      // The DHE parameters selected by OpenSSL depend on the strength of the
371cb0ef41Sopenharmony_ci      // certificate's key. For this test, we can assume that the modulus length
381cb0ef41Sopenharmony_ci      // of the certificate's key is equal to the size of the DHE parameter, but
391cb0ef41Sopenharmony_ci      // that is really only true for a few modulus lengths.
401cb0ef41Sopenharmony_ci      ({
411cb0ef41Sopenharmony_ci        publicKey: { asymmetricKeyDetails: { modulusLength: size } }
421cb0ef41Sopenharmony_ci      } = new X509Certificate(cert));
431cb0ef41Sopenharmony_ci    } else {
441cb0ef41Sopenharmony_ci      options.dhparam = loadDHParam(size);
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const server = tls.createServer(options, common.mustCall((conn) => {
491cb0ef41Sopenharmony_ci    assert.strictEqual(conn.getEphemeralKeyInfo(), null);
501cb0ef41Sopenharmony_ci    conn.end();
511cb0ef41Sopenharmony_ci  }));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  server.on('close', common.mustSucceed());
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
561cb0ef41Sopenharmony_ci    const client = tls.connect({
571cb0ef41Sopenharmony_ci      port: server.address().port,
581cb0ef41Sopenharmony_ci      rejectUnauthorized: false
591cb0ef41Sopenharmony_ci    }, common.mustCall(function() {
601cb0ef41Sopenharmony_ci      const ekeyinfo = client.getEphemeralKeyInfo();
611cb0ef41Sopenharmony_ci      assert.strictEqual(ekeyinfo.type, type);
621cb0ef41Sopenharmony_ci      assert.strictEqual(ekeyinfo.size, size);
631cb0ef41Sopenharmony_ci      assert.strictEqual(ekeyinfo.name, name);
641cb0ef41Sopenharmony_ci      server.close();
651cb0ef41Sopenharmony_ci    }));
661cb0ef41Sopenharmony_ci    client.on('secureConnect', common.mustCall());
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_citest(undefined, undefined, undefined, 'AES128-SHA256');
711cb0ef41Sopenharmony_citest('auto', 'DH', undefined, 'DHE-RSA-AES128-GCM-SHA256');
721cb0ef41Sopenharmony_citest(1024, 'DH', undefined, 'DHE-RSA-AES128-GCM-SHA256');
731cb0ef41Sopenharmony_citest(2048, 'DH', undefined, 'DHE-RSA-AES128-GCM-SHA256');
741cb0ef41Sopenharmony_citest(256, 'ECDH', 'prime256v1', 'ECDHE-RSA-AES128-GCM-SHA256');
751cb0ef41Sopenharmony_citest(521, 'ECDH', 'secp521r1', 'ECDHE-RSA-AES128-GCM-SHA256');
761cb0ef41Sopenharmony_citest(253, 'ECDH', 'X25519', 'ECDHE-RSA-AES128-GCM-SHA256');
771cb0ef41Sopenharmony_citest(448, 'ECDH', 'X448', 'ECDHE-RSA-AES128-GCM-SHA256');
78