1'use strict'; 2const common = require('../common'); 3const fixtures = require('../common/fixtures'); 4const { inspect } = require('util'); 5 6// Check min/max protocol versions. 7 8const { 9 assert, connect, keys, tls 10} = require(fixtures.path('tls-connect')); 11const DEFAULT_MIN_VERSION = tls.DEFAULT_MIN_VERSION; 12const DEFAULT_MAX_VERSION = tls.DEFAULT_MAX_VERSION; 13 14 15function test(cmin, cmax, cprot, smin, smax, sprot, proto, cerr, serr) { 16 assert(proto || cerr || serr, 'test missing any expectations'); 17 18 let ciphers; 19 if (common.hasOpenSSL3 && (proto === 'TLSv1' || proto === 'TLSv1.1' || 20 proto === 'TLSv1_1_method' || proto === 'TLSv1_method' || 21 sprot === 'TLSv1_1_method' || sprot === 'TLSv1_method')) { 22 if (serr !== 'ERR_SSL_UNSUPPORTED_PROTOCOL') 23 ciphers = 'ALL@SECLEVEL=0'; 24 } 25 if (common.hasOpenSSL31 && cerr === 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION') { 26 ciphers = 'DEFAULT@SECLEVEL=0'; 27 } 28 // Report where test was called from. Strip leading garbage from 29 // at Object.<anonymous> (file:line) 30 // from the stack location, we only want the file:line part. 31 const where = inspect(new Error()).split('\n')[2].replace(/[^(]*/, ''); 32 connect({ 33 client: { 34 checkServerIdentity: (servername, cert) => { }, 35 ca: `${keys.agent1.cert}\n${keys.agent6.ca}`, 36 minVersion: cmin, 37 maxVersion: cmax, 38 secureProtocol: cprot, 39 ciphers: ciphers 40 }, 41 server: { 42 cert: keys.agent6.cert, 43 key: keys.agent6.key, 44 minVersion: smin, 45 maxVersion: smax, 46 secureProtocol: sprot, 47 ciphers: ciphers 48 }, 49 }, common.mustCall((err, pair, cleanup) => { 50 function u(_) { return _ === undefined ? 'U' : _; } 51 console.log('test:', u(cmin), u(cmax), u(cprot), u(smin), u(smax), u(sprot), 52 u(ciphers), 'expect', u(proto), u(cerr), u(serr)); 53 console.log(' ', where); 54 if (!proto) { 55 console.log('client', pair.client.err ? pair.client.err.code : undefined); 56 console.log('server', pair.server.err ? pair.server.err.code : undefined); 57 if (cerr) { 58 assert(pair.client.err); 59 // Accept these codes as aliases, the one reported depends on the 60 // OpenSSL version. 61 if (cerr === 'ERR_SSL_UNSUPPORTED_PROTOCOL' && 62 pair.client.err.code === 'ERR_SSL_VERSION_TOO_LOW') 63 cerr = 'ERR_SSL_VERSION_TOO_LOW'; 64 assert.strictEqual(pair.client.err.code, cerr); 65 } 66 if (serr) { 67 assert(pair.server.err); 68 assert.strictEqual(pair.server.err.code, serr); 69 } 70 return cleanup(); 71 } 72 73 assert.ifError(err); 74 assert.ifError(pair.server.err); 75 assert.ifError(pair.client.err); 76 assert(pair.server.conn); 77 assert(pair.client.conn); 78 assert.strictEqual(pair.client.conn.getProtocol(), proto); 79 assert.strictEqual(pair.server.conn.getProtocol(), proto); 80 return cleanup(); 81 })); 82} 83 84const U = undefined; 85 86// Default protocol is the max version. 87test(U, U, U, U, U, U, DEFAULT_MAX_VERSION); 88 89// Insecure or invalid protocols cannot be enabled. 90test(U, U, U, U, U, 'SSLv2_method', 91 U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 92test(U, U, U, U, U, 'SSLv3_method', 93 U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 94test(U, U, 'SSLv2_method', U, U, U, 95 U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 96test(U, U, 'SSLv3_method', U, U, U, 97 U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 98test(U, U, 'hokey-pokey', U, U, U, 99 U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 100test(U, U, U, U, U, 'hokey-pokey', 101 U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 102 103// Regression test: this should not crash because node should not pass the error 104// message (including unsanitized user input) to a printf-like function. 105test(U, U, U, U, U, '%s_method', 106 U, U, 'ERR_TLS_INVALID_PROTOCOL_METHOD'); 107 108// Cannot use secureProtocol and min/max versions simultaneously. 109test(U, U, U, U, 'TLSv1.2', 'TLS1_2_method', 110 U, U, 'ERR_TLS_PROTOCOL_VERSION_CONFLICT'); 111test(U, U, U, 'TLSv1.2', U, 'TLS1_2_method', 112 U, U, 'ERR_TLS_PROTOCOL_VERSION_CONFLICT'); 113test(U, 'TLSv1.2', 'TLS1_2_method', U, U, U, 114 U, 'ERR_TLS_PROTOCOL_VERSION_CONFLICT'); 115test('TLSv1.2', U, 'TLS1_2_method', U, U, U, 116 U, 'ERR_TLS_PROTOCOL_VERSION_CONFLICT'); 117 118// TLS_method means "any supported protocol". 119test(U, U, 'TLSv1_2_method', U, U, 'TLS_method', 'TLSv1.2'); 120test(U, U, 'TLSv1_1_method', U, U, 'TLS_method', 'TLSv1.1'); 121test(U, U, 'TLSv1_method', U, U, 'TLS_method', 'TLSv1'); 122test(U, U, 'TLS_method', U, U, 'TLSv1_2_method', 'TLSv1.2'); 123test(U, U, 'TLS_method', U, U, 'TLSv1_1_method', 'TLSv1.1'); 124test(U, U, 'TLS_method', U, U, 'TLSv1_method', 'TLSv1'); 125 126// OpenSSL 1.1.1 and 3.0 use a different error code and alert (sent to the 127// client) when no protocols are enabled on the server. 128const NO_PROTOCOLS_AVAILABLE_SERVER = common.hasOpenSSL3 ? 129 'ERR_SSL_NO_PROTOCOLS_AVAILABLE' : 'ERR_SSL_INTERNAL_ERROR'; 130const NO_PROTOCOLS_AVAILABLE_SERVER_ALERT = common.hasOpenSSL3 ? 131 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION' : 'ERR_SSL_TLSV1_ALERT_INTERNAL_ERROR'; 132 133// SSLv23 also means "any supported protocol" greater than the default 134// minimum (which is configurable via command line). 135if (DEFAULT_MIN_VERSION === 'TLSv1.3') { 136 test(U, U, 'TLSv1_2_method', U, U, 'SSLv23_method', 137 U, NO_PROTOCOLS_AVAILABLE_SERVER_ALERT, NO_PROTOCOLS_AVAILABLE_SERVER); 138} else { 139 test(U, U, 'TLSv1_2_method', U, U, 'SSLv23_method', 'TLSv1.2'); 140} 141 142if (DEFAULT_MIN_VERSION === 'TLSv1.3') { 143 test(U, U, 'TLSv1_1_method', U, U, 'SSLv23_method', 144 U, NO_PROTOCOLS_AVAILABLE_SERVER_ALERT, NO_PROTOCOLS_AVAILABLE_SERVER); 145 test(U, U, 'TLSv1_method', U, U, 'SSLv23_method', 146 U, NO_PROTOCOLS_AVAILABLE_SERVER_ALERT, NO_PROTOCOLS_AVAILABLE_SERVER); 147 test(U, U, 'SSLv23_method', U, U, 'TLSv1_1_method', 148 U, 'ERR_SSL_NO_PROTOCOLS_AVAILABLE', 'ERR_SSL_UNEXPECTED_MESSAGE'); 149 test(U, U, 'SSLv23_method', U, U, 'TLSv1_method', 150 U, 'ERR_SSL_NO_PROTOCOLS_AVAILABLE', 'ERR_SSL_UNEXPECTED_MESSAGE'); 151} 152 153if (DEFAULT_MIN_VERSION === 'TLSv1.2') { 154 test(U, U, 'TLSv1_1_method', U, U, 'SSLv23_method', 155 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 156 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 157 test(U, U, 'TLSv1_method', U, U, 'SSLv23_method', 158 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 159 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 160 test(U, U, 'SSLv23_method', U, U, 'TLSv1_1_method', 161 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 162 test(U, U, 'SSLv23_method', U, U, 'TLSv1_method', 163 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 164} 165 166if (DEFAULT_MIN_VERSION === 'TLSv1.1') { 167 test(U, U, 'TLSv1_1_method', U, U, 'SSLv23_method', 'TLSv1.1'); 168 test(U, U, 'TLSv1_method', U, U, 'SSLv23_method', 169 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 170 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 171 test(U, U, 'SSLv23_method', U, U, 'TLSv1_1_method', 'TLSv1.1'); 172 test(U, U, 'SSLv23_method', U, U, 'TLSv1_method', 173 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 174} 175 176if (DEFAULT_MIN_VERSION === 'TLSv1') { 177 test(U, U, 'TLSv1_1_method', U, U, 'SSLv23_method', 'TLSv1.1'); 178 test(U, U, 'TLSv1_method', U, U, 'SSLv23_method', 'TLSv1'); 179 test(U, U, 'SSLv23_method', U, U, 'TLSv1_1_method', 'TLSv1.1'); 180 test(U, U, 'SSLv23_method', U, U, 'TLSv1_method', 'TLSv1'); 181} 182 183// TLSv1 thru TLSv1.2 are only supported with explicit configuration with API or 184// CLI (--tls-v1.0 and --tls-v1.1). 185test(U, U, 'TLSv1_2_method', U, U, 'TLSv1_2_method', 'TLSv1.2'); 186test(U, U, 'TLSv1_1_method', U, U, 'TLSv1_1_method', 'TLSv1.1'); 187test(U, U, 'TLSv1_method', U, U, 'TLSv1_method', 'TLSv1'); 188 189// The default default. 190if (DEFAULT_MIN_VERSION === 'TLSv1.2') { 191 test(U, U, 'TLSv1_1_method', U, U, U, 192 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 193 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 194 test(U, U, 'TLSv1_method', U, U, U, 195 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 196 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 197 198 if (DEFAULT_MAX_VERSION === 'TLSv1.2') { 199 test(U, U, U, U, U, 'TLSv1_1_method', 200 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 201 test(U, U, U, U, U, 'TLSv1_method', 202 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 203 } else { 204 // TLS1.3 client hellos are are not understood by TLS1.1 or below. 205 test(U, U, U, U, U, 'TLSv1_1_method', 206 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 207 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 208 test(U, U, U, U, U, 'TLSv1_method', 209 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 210 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 211 } 212} 213 214// The default with --tls-v1.1. 215if (DEFAULT_MIN_VERSION === 'TLSv1.1') { 216 test(U, U, 'TLSv1_1_method', U, U, U, 'TLSv1.1'); 217 test(U, U, 'TLSv1_method', U, U, U, 218 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 219 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 220 test(U, U, U, U, U, 'TLSv1_1_method', 'TLSv1.1'); 221 222 if (DEFAULT_MAX_VERSION === 'TLSv1.2') { 223 test(U, U, U, U, U, 'TLSv1_method', 224 U, 'ERR_SSL_UNSUPPORTED_PROTOCOL', 'ERR_SSL_WRONG_VERSION_NUMBER'); 225 } else { 226 // TLS1.3 client hellos are are not understood by TLS1.1 or below. 227 test(U, U, U, U, U, 'TLSv1_method', 228 U, 'ERR_SSL_TLSV1_ALERT_PROTOCOL_VERSION', 229 'ERR_SSL_UNSUPPORTED_PROTOCOL'); 230 } 231} 232 233// The default with --tls-v1.0. 234if (DEFAULT_MIN_VERSION === 'TLSv1') { 235 test(U, U, 'TLSv1_1_method', U, U, U, 'TLSv1.1'); 236 test(U, U, 'TLSv1_method', U, U, U, 'TLSv1'); 237 test(U, U, U, U, U, 'TLSv1_1_method', 'TLSv1.1'); 238 test(U, U, U, U, U, 'TLSv1_method', 'TLSv1'); 239} 240 241// TLS min/max are respected when set with no secureProtocol. 242test('TLSv1', 'TLSv1.2', U, U, U, 'TLSv1_method', 'TLSv1'); 243test('TLSv1', 'TLSv1.2', U, U, U, 'TLSv1_1_method', 'TLSv1.1'); 244test('TLSv1', 'TLSv1.2', U, U, U, 'TLSv1_2_method', 'TLSv1.2'); 245test('TLSv1', 'TLSv1.2', U, U, U, 'TLS_method', 'TLSv1.2'); 246 247test(U, U, 'TLSv1_method', 'TLSv1', 'TLSv1.2', U, 'TLSv1'); 248test(U, U, 'TLSv1_1_method', 'TLSv1', 'TLSv1.2', U, 'TLSv1.1'); 249test(U, U, 'TLSv1_2_method', 'TLSv1', 'TLSv1.2', U, 'TLSv1.2'); 250 251test('TLSv1', 'TLSv1.1', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1.1'); 252test('TLSv1', 'TLSv1.1', U, 'TLSv1', 'TLSv1.2', U, 'TLSv1.1'); 253test('TLSv1', 'TLSv1.2', U, 'TLSv1', 'TLSv1.1', U, 'TLSv1.1'); 254test('TLSv1', 'TLSv1.3', U, 'TLSv1', 'TLSv1.1', U, 'TLSv1.1'); 255test('TLSv1', 'TLSv1', U, 'TLSv1', 'TLSv1.1', U, 'TLSv1'); 256test('TLSv1', 'TLSv1.2', U, 'TLSv1', 'TLSv1', U, 'TLSv1'); 257test('TLSv1', 'TLSv1.3', U, 'TLSv1', 'TLSv1', U, 'TLSv1'); 258test('TLSv1.1', 'TLSv1.1', U, 'TLSv1', 'TLSv1.2', U, 'TLSv1.1'); 259test('TLSv1', 'TLSv1.2', U, 'TLSv1.1', 'TLSv1.1', U, 'TLSv1.1'); 260test('TLSv1', 'TLSv1.2', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1.2'); 261 262// v-any client can connect to v-specific server 263test('TLSv1', 'TLSv1.3', U, 'TLSv1.3', 'TLSv1.3', U, 'TLSv1.3'); 264test('TLSv1', 'TLSv1.3', U, 'TLSv1.2', 'TLSv1.3', U, 'TLSv1.3'); 265test('TLSv1', 'TLSv1.3', U, 'TLSv1.2', 'TLSv1.2', U, 'TLSv1.2'); 266test('TLSv1', 'TLSv1.3', U, 'TLSv1.1', 'TLSv1.1', U, 'TLSv1.1'); 267test('TLSv1', 'TLSv1.3', U, 'TLSv1', 'TLSv1', U, 'TLSv1'); 268 269// v-specific client can connect to v-any server 270test('TLSv1.3', 'TLSv1.3', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1.3'); 271test('TLSv1.2', 'TLSv1.2', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1.2'); 272test('TLSv1.1', 'TLSv1.1', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1.1'); 273test('TLSv1', 'TLSv1', U, 'TLSv1', 'TLSv1.3', U, 'TLSv1'); 274