11cb0ef41Sopenharmony_ci// Flags: --no-warnings 21cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 51cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 61cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 71cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 81cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 91cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 101cb0ef41Sopenharmony_ci// following conditions: 111cb0ef41Sopenharmony_ci// 121cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 131cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 141cb0ef41Sopenharmony_ci// 151cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 161cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 171cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 181cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 191cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 201cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 211cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci'use strict'; 241cb0ef41Sopenharmony_ciconst common = require('../common'); 251cb0ef41Sopenharmony_ciif (!common.hasCrypto) 261cb0ef41Sopenharmony_ci common.skip('missing crypto'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciif (!common.opensslCli) 291cb0ef41Sopenharmony_ci common.skip('missing openssl-cli'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst assert = require('assert'); 321cb0ef41Sopenharmony_ciconst { X509Certificate } = require('crypto'); 331cb0ef41Sopenharmony_ciconst { once } = require('events'); 341cb0ef41Sopenharmony_ciconst tls = require('tls'); 351cb0ef41Sopenharmony_ciconst { execFile } = require('child_process'); 361cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent2-key.pem'); 391cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent2-cert.pem'); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci// Prefer DHE over ECDHE when possible. 421cb0ef41Sopenharmony_ciconst dheCipher = 'DHE-RSA-AES128-SHA256'; 431cb0ef41Sopenharmony_ciconst ecdheCipher = 'ECDHE-RSA-AES128-SHA256'; 441cb0ef41Sopenharmony_ciconst ciphers = `${dheCipher}:${ecdheCipher}`; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci// Test will emit a warning because the DH parameter size is < 2048 bits 471cb0ef41Sopenharmony_cicommon.expectWarning('SecurityWarning', 481cb0ef41Sopenharmony_ci 'DH parameter is less than 2048 bits'); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cifunction loadDHParam(n) { 511cb0ef41Sopenharmony_ci const keyname = `dh${n}.pem`; 521cb0ef41Sopenharmony_ci return fixtures.readKey(keyname); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifunction test(dhparam, keylen, expectedCipher) { 561cb0ef41Sopenharmony_ci const options = { 571cb0ef41Sopenharmony_ci key, 581cb0ef41Sopenharmony_ci cert, 591cb0ef41Sopenharmony_ci ciphers, 601cb0ef41Sopenharmony_ci dhparam, 611cb0ef41Sopenharmony_ci maxVersion: 'TLSv1.2', 621cb0ef41Sopenharmony_ci }; 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci const server = tls.createServer(options, (conn) => conn.end()); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci server.listen(0, '127.0.0.1', common.mustCall(() => { 671cb0ef41Sopenharmony_ci const args = ['s_client', '-connect', `127.0.0.1:${server.address().port}`, 681cb0ef41Sopenharmony_ci '-cipher', `${ciphers}:@SECLEVEL=1`]; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci execFile(common.opensslCli, args, common.mustSucceed((stdout) => { 711cb0ef41Sopenharmony_ci assert(keylen === null || 721cb0ef41Sopenharmony_ci stdout.includes(`Server Temp Key: DH, ${keylen} bits`)); 731cb0ef41Sopenharmony_ci assert(stdout.includes(`Cipher : ${expectedCipher}`)); 741cb0ef41Sopenharmony_ci server.close(); 751cb0ef41Sopenharmony_ci })); 761cb0ef41Sopenharmony_ci })); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci return once(server, 'close'); 791cb0ef41Sopenharmony_ci} 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_cifunction testCustomParam(keylen, expectedCipher) { 821cb0ef41Sopenharmony_ci const dhparam = loadDHParam(keylen); 831cb0ef41Sopenharmony_ci if (keylen === 'error') keylen = null; 841cb0ef41Sopenharmony_ci return test(dhparam, keylen, expectedCipher); 851cb0ef41Sopenharmony_ci} 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci(async () => { 881cb0ef41Sopenharmony_ci // By default, DHE is disabled while ECDHE is enabled. 891cb0ef41Sopenharmony_ci for (const dhparam of [undefined, null]) { 901cb0ef41Sopenharmony_ci await test(dhparam, null, ecdheCipher); 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci // The DHE parameters selected by OpenSSL depend on the strength of the 941cb0ef41Sopenharmony_ci // certificate's key. For this test, we can assume that the modulus length 951cb0ef41Sopenharmony_ci // of the certificate's key is equal to the size of the DHE parameter, but 961cb0ef41Sopenharmony_ci // that is really only true for a few modulus lengths. 971cb0ef41Sopenharmony_ci const { 981cb0ef41Sopenharmony_ci publicKey: { asymmetricKeyDetails: { modulusLength } } 991cb0ef41Sopenharmony_ci } = new X509Certificate(cert); 1001cb0ef41Sopenharmony_ci await test('auto', modulusLength, dheCipher); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci assert.throws(() => { 1031cb0ef41Sopenharmony_ci testCustomParam(512); 1041cb0ef41Sopenharmony_ci }, /DH parameter is less than 1024 bits/); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci // Custom DHE parameters are supported (but discouraged). 1071cb0ef41Sopenharmony_ci await testCustomParam(1024, dheCipher); 1081cb0ef41Sopenharmony_ci await testCustomParam(2048, dheCipher); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci // Invalid DHE parameters are discarded. ECDHE remains enabled. 1111cb0ef41Sopenharmony_ci await testCustomParam('error', ecdheCipher); 1121cb0ef41Sopenharmony_ci})().then(common.mustCall()); 113