11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// Test the honorCipherOrder property 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciif (!common.hasCrypto) 81cb0ef41Sopenharmony_ci common.skip('missing crypto'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst assert = require('assert'); 111cb0ef41Sopenharmony_ciconst mustCall = common.mustCall; 121cb0ef41Sopenharmony_ciconst tls = require('tls'); 131cb0ef41Sopenharmony_ciconst util = require('util'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// We explicitly set TLS version to 1.2 so as to be safe when the 161cb0ef41Sopenharmony_ci// default method is updated in the future 171cb0ef41Sopenharmony_ciconst SSL_Method = 'TLSv1_2_method'; 181cb0ef41Sopenharmony_ciconst localhost = '127.0.0.1'; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cifunction test(honorCipherOrder, clientCipher, expectedCipher, defaultCiphers) { 211cb0ef41Sopenharmony_ci const soptions = { 221cb0ef41Sopenharmony_ci secureProtocol: SSL_Method, 231cb0ef41Sopenharmony_ci key: fixtures.readKey('agent2-key.pem'), 241cb0ef41Sopenharmony_ci cert: fixtures.readKey('agent2-cert.pem'), 251cb0ef41Sopenharmony_ci ciphers: 'AES256-SHA256:AES128-GCM-SHA256:AES128-SHA256:' + 261cb0ef41Sopenharmony_ci 'ECDHE-RSA-AES128-GCM-SHA256', 271cb0ef41Sopenharmony_ci honorCipherOrder: honorCipherOrder, 281cb0ef41Sopenharmony_ci }; 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci const server = tls.createServer(soptions, mustCall(function(clearTextStream) { 311cb0ef41Sopenharmony_ci // End socket to send CLOSE_NOTIFY and TCP FIN packet, otherwise 321cb0ef41Sopenharmony_ci // it may hang for ~30 seconds in FIN_WAIT_1 state (at least on OSX). 331cb0ef41Sopenharmony_ci clearTextStream.end(); 341cb0ef41Sopenharmony_ci })); 351cb0ef41Sopenharmony_ci server.listen(0, localhost, mustCall(function() { 361cb0ef41Sopenharmony_ci const coptions = { 371cb0ef41Sopenharmony_ci rejectUnauthorized: false, 381cb0ef41Sopenharmony_ci secureProtocol: SSL_Method 391cb0ef41Sopenharmony_ci }; 401cb0ef41Sopenharmony_ci if (clientCipher) { 411cb0ef41Sopenharmony_ci coptions.ciphers = clientCipher; 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci const port = this.address().port; 441cb0ef41Sopenharmony_ci const savedDefaults = tls.DEFAULT_CIPHERS; 451cb0ef41Sopenharmony_ci tls.DEFAULT_CIPHERS = defaultCiphers || savedDefaults; 461cb0ef41Sopenharmony_ci const client = tls.connect(port, localhost, coptions, mustCall(function() { 471cb0ef41Sopenharmony_ci const cipher = client.getCipher(); 481cb0ef41Sopenharmony_ci client.end(); 491cb0ef41Sopenharmony_ci server.close(); 501cb0ef41Sopenharmony_ci const msg = util.format( 511cb0ef41Sopenharmony_ci 'honorCipherOrder=%j, clientCipher=%j, expect=%j, got=%j', 521cb0ef41Sopenharmony_ci honorCipherOrder, clientCipher, expectedCipher, cipher.name); 531cb0ef41Sopenharmony_ci assert.strictEqual(cipher.name, expectedCipher, msg); 541cb0ef41Sopenharmony_ci })); 551cb0ef41Sopenharmony_ci tls.DEFAULT_CIPHERS = savedDefaults; 561cb0ef41Sopenharmony_ci })); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci// Client explicitly has the preference of cipher suites, not the default. 601cb0ef41Sopenharmony_citest(false, 'AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256', 611cb0ef41Sopenharmony_ci 'AES128-GCM-SHA256'); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Server has the preference of cipher suites, and AES256-SHA256 is 641cb0ef41Sopenharmony_ci// the server's top choice. 651cb0ef41Sopenharmony_citest(true, 'AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256', 661cb0ef41Sopenharmony_ci 'AES256-SHA256'); 671cb0ef41Sopenharmony_citest(undefined, 'AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256', 681cb0ef41Sopenharmony_ci 'AES256-SHA256'); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// Server has the preference of cipher suites. AES128-GCM-SHA256 is given 711cb0ef41Sopenharmony_ci// higher priority over AES128-SHA256 among client cipher suites. 721cb0ef41Sopenharmony_citest(true, 'AES128-SHA256:AES128-GCM-SHA256', 'AES128-GCM-SHA256'); 731cb0ef41Sopenharmony_citest(undefined, 'AES128-SHA256:AES128-GCM-SHA256', 'AES128-GCM-SHA256'); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci// As client has only one cipher, server has no choice, irrespective 771cb0ef41Sopenharmony_ci// of honorCipherOrder. 781cb0ef41Sopenharmony_citest(true, 'AES128-SHA256', 'AES128-SHA256'); 791cb0ef41Sopenharmony_citest(undefined, 'AES128-SHA256', 'AES128-SHA256'); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci// Client did not explicitly set ciphers and client offers 821cb0ef41Sopenharmony_ci// tls.DEFAULT_CIPHERS. All ciphers of the server are included in the 831cb0ef41Sopenharmony_ci// default list so the negotiated cipher is selected according to the 841cb0ef41Sopenharmony_ci// server's top preference of AES256-SHA256. 851cb0ef41Sopenharmony_citest(true, tls.DEFAULT_CIPHERS, 'AES256-SHA256'); 861cb0ef41Sopenharmony_citest(true, null, 'AES256-SHA256'); 871cb0ef41Sopenharmony_citest(undefined, null, 'AES256-SHA256'); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci// Ensure that `tls.DEFAULT_CIPHERS` is used when its a limited cipher set. 901cb0ef41Sopenharmony_citest(true, null, 'ECDHE-RSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES128-GCM-SHA256'); 91