GeezerVison is showing github.com — tls: configure TLS 1.3 ciphers with BoringSSL · nodejs/node@0504aab
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.cc +48-4Lines changed: 48 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5114,13 +5114,57 @@ bool SSLCtxPointer::setGroups(const char* groups) { | 5114 | 5114 | } | 5115 | 5115 |
| 5116 | 5116 | bool SSLCtxPointer::setCipherSuites(const char* ciphers) { | 5117 | | -#ifndef OPENSSL_IS_BORINGSSL | 5118 | 5117 | if (!ctx_) return false; | | 5118 | +#ifndef OPENSSL_IS_BORINGSSL | 5119 | 5119 | return SSL_CTX_set_ciphersuites(ctx_.get(), ciphers); | 5120 | 5120 | #else | 5121 | | - // BoringSSL does not allow API config of TLS 1.3 cipher suites. | 5122 | | - // We treat this as a non-op. | 5123 | | - return true; | | 5121 | + constexpr std::array<uint16_t, 3> supported = { | | 5122 | + SSL_CIPHER_AES_128_GCM_SHA256, | | 5123 | + SSL_CIPHER_AES_256_GCM_SHA384, | | 5124 | + SSL_CIPHER_CHACHA20_POLY1305_SHA256, | | 5125 | + }; | | 5126 | + std::vector<uint16_t> cipher_ids; | | 5127 | + std::string_view remaining(ciphers); | | 5128 | + while (!remaining.empty()) { | | 5129 | + const size_t end = remaining.find(':'); | | 5130 | + std::string_view name = remaining.substr(0, end); | | 5131 | + const size_t first = name.find_first_not_of(" \t\n\r\f\v"); | | 5132 | + if (first != std::string_view::npos) { | | 5133 | + name = name.substr(first); | | 5134 | + name = name.substr(0, name.find_last_not_of(" \t\n\r\f\v") + 1); | | 5135 | + for (const auto id : supported) { | | 5136 | + const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); | | 5137 | + if (name == SSL_CIPHER_standard_name(cipher)) { | | 5138 | + // OpenSSL ignores unknown names and accepts duplicates, while | | 5139 | + // BoringSSL requires unique protocol IDs. | | 5140 | + if (std::find(cipher_ids.begin(), cipher_ids.end(), id) == | | 5141 | + cipher_ids.end()) { | | 5142 | + cipher_ids.push_back(id); | | 5143 | + } | | 5144 | + break; | | 5145 | + } | | 5146 | + } | | 5147 | + } | | 5148 | + if (end == std::string_view::npos) break; | | 5149 | + remaining.remove_prefix(end + 1); | | 5150 | + } | | 5151 | + if (*ciphers != '\0' && cipher_ids.empty()) { | | 5152 | + OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); | | 5153 | + return false; | | 5154 | + } | | 5155 | + std::vector<uint32_t> flags(cipher_ids.size()); | | 5156 | + if (!flags.empty() && | | 5157 | + (SSL_CTX_get_options(get()) & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) { | | 5158 | + // TLS 1.3 otherwise always uses server preference in BoringSSL. An | | 5159 | + // equal-preference group lets the client choose from the configured list. | | 5160 | + std::fill(flags.begin(), | | 5161 | + flags.end() - 1, | | 5162 | + SSL_CIPHER_FLAG_EQUAL_PREFERENCE_WITH_NEXT); | | 5163 | + } | | 5164 | + // An empty input resets BoringSSL's built-in preference order. The public | | 5165 | + // TLS API expands empty cipher strings to Node's defaults before this call. | | 5166 | + return SSL_CTX_set1_tls13_ciphers( | | 5167 | + get(), cipher_ids.data(), flags.data(), cipher_ids.size()) == 1; | 5124 | 5168 | #endif | 5125 | 5169 | } | 5126 | 5170 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/tls/wrap.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,8 @@ const { | 39 | 39 |
| 40 | 40 | assertCrypto(); | 41 | 41 |
| | 42 | +const { openssl_is_boringssl: isBoringSSL } = process.features; | | 43 | + | 42 | 44 | const { setImmediate } = require('timers'); | 43 | 45 | const assert = require('internal/assert'); | 44 | 46 | const crypto = require('crypto'); | @@ -1134,7 +1136,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) { | 1134 | 1136 | this._handle.renegotiate(); | 1135 | 1137 | } catch (err) { | 1136 | 1138 | const isBoringSSLRenegotiationUnsupported = | 1137 | | - process.features.openssl_is_boringssl && | | 1139 | + isBoringSSL && | 1138 | 1140 | err?.code === 'ERR_SSL_FUNCTION_SHOULD_NOT_HAVE_BEEN_CALLED'; | 1139 | 1141 | const error = isBoringSSLRenegotiationUnsupported ? | 1140 | 1142 | new ERR_TLS_RENEGOTIATION_UNSUPPORTED() : err; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-tls-getcipher.js +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,10 +109,7 @@ tls.createServer({ | 109 | 109 | rejectUnauthorized: false | 110 | 110 | }, common.mustCall(() => { | 111 | 111 | const cipher = client.getCipher(); | 112 | | - const expectedCipher = isBoringSSL ? | 113 | | - 'TLS_AES_128_GCM_SHA256' : | 114 | | - 'TLS_AES_256_GCM_SHA384'; | 115 | | - assert.strictEqual(cipher.name, expectedCipher); | | 112 | + assert.strictEqual(cipher.name, 'TLS_AES_256_GCM_SHA384'); | 116 | 113 | assert.strictEqual(cipher.standardName, cipher.name); | 117 | 114 | assert.strictEqual(cipher.version, isBoringSSL ? | 118 | 115 | 'TLSv1/SSLv3' : | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-tls-set-ciphers-error.js +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,7 @@ if (!common.hasCrypto) | 7 | 7 | const assert = require('assert'); | 8 | 8 | const tls = require('tls'); | 9 | 9 | const fixtures = require('../common/fixtures'); | 10 | | -const { hasOpenSSL, isBoringSSL } = require('../common/crypto'); | | 10 | +const { hasOpenSSL } = require('../common/crypto'); | 11 | 11 |
| 12 | 12 | { | 13 | 13 | const options = { | @@ -21,12 +21,8 @@ const { hasOpenSSL, isBoringSSL } = require('../common/crypto'); | 21 | 21 | assert.throws(() => tls.createServer(options, common.mustNotCall()), | 22 | 22 | /no[_ ]cipher[_ ]match/i); | 23 | 23 | options.ciphers = 'TLS_not_a_cipher'; | 24 | | - if (isBoringSSL) { | 25 | | - tls.createServer(options).close(); | 26 | | - } else { | 27 | | - assert.throws(() => tls.createServer(options, common.mustNotCall()), | 28 | | - /no[_ ]cipher[_ ]match/i); | 29 | | - } | | 24 | + assert.throws(() => tls.createServer(options, common.mustNotCall()), | | 25 | + /no[_ ]cipher[_ ]match/i); | 30 | 26 | } | 31 | 27 |
| 32 | 28 | // Cipher name matching is case-sensitive prior to OpenSSL 4.0, and | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-tls-set-ciphers.js +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,9 +7,10 @@ if (!common.hasCrypto) { | 7 | 7 | const { | 8 | 8 | hasOpenSSL, | 9 | 9 | hasFIPS, | | 10 | + isBoringSSL, | 10 | 11 | } = require('../common/crypto'); | 11 | 12 |
| 12 | | -if (!hasOpenSSL(3)) { | | 13 | +if (!hasOpenSSL(3) && !isBoringSSL) { | 13 | 14 | common.skip('missing crypto, or OpenSSL version lower than 3'); | 14 | 15 | } | 15 | 16 |
| @@ -133,10 +134,11 @@ if (hasFIPS(3)) { | 133 | 134 | test('TLS_AES_256_GCM_SHA384', 'TLS_CHACHA20_POLY1305_SHA256', | 134 | 135 | U, expectedTLSAlertError, 'ERR_SSL_NO_SHARED_CIPHER'); | 135 | 136 |
| 136 | | - test('AES256-SHA', 'AES256-SHA256', U, expectedTLSAlertError, | | 137 | + // Use TLS 1.2 suites available on both crypto backends. | | 138 | + test('AES256-SHA', 'ECDHE-RSA-AES256-GCM-SHA384', U, expectedTLSAlertError, | 137 | 139 | 'ERR_SSL_NO_SHARED_CIPHER'); | 138 | 140 | test('AES256-SHA:TLS_AES_256_GCM_SHA384', | 139 | | - 'TLS_CHACHA20_POLY1305_SHA256:AES256-SHA256', | | 141 | + 'TLS_CHACHA20_POLY1305_SHA256:ECDHE-RSA-AES256-GCM-SHA384', | 140 | 142 | U, expectedTLSAlertError, 'ERR_SSL_NO_SHARED_CIPHER'); | 141 | 143 |
| 142 | 144 | // Cipher order ignored, TLS1.3 chosen before TLS1.2. | @@ -154,7 +156,10 @@ if (hasFIPS(3)) { | 154 | 156 | // default, but work. | 155 | 157 | // However, for OpenSSL32 AES_128 is not enabled due to the | 156 | 158 | // default security level | 157 | | - if (!hasOpenSSL(3, 2)) { | | 159 | + if (isBoringSSL) { | | 160 | + // BoringSSL does not implement the TLS 1.3 CCM cipher suites. | | 161 | + test('TLS_AES_128_CCM_8_SHA256', U, U, 'ERR_SSL_NO_CIPHER_MATCH', U); | | 162 | + } else if (!hasOpenSSL(3, 2)) { | 158 | 163 | test('TLS_AES_128_CCM_8_SHA256', U, | 159 | 164 | U, 'ERR_SSL_SSLV3_ALERT_HANDSHAKE_FAILURE', 'ERR_SSL_NO_SHARED_CIPHER'); | 160 | 165 |
| |
Collapse file Copy file name to clipboard +151Lines changed: 151 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +if (!common.hasCrypto) | | 4 | + common.skip('missing crypto'); | | 5 | + | | 6 | +const assert = require('assert'); | | 7 | +const { constants } = require('crypto'); | | 8 | +const tls = require('tls'); | | 9 | +const fixtures = require('../common/fixtures'); | | 10 | +const { hasOpenSSL, isBoringSSL } = require('../common/crypto'); | | 11 | +const { spawnSyncAndAssert } = require('../common/child_process'); | | 12 | +const { connect, keys } = require(fixtures.path('tls-connect')); | | 13 | + | | 14 | +const aes128 = 'TLS_AES_128_GCM_SHA256'; | | 15 | +const aes256 = 'TLS_AES_256_GCM_SHA384'; | | 16 | +const tls13Ciphers = [aes128, aes256, 'TLS_CHACHA20_POLY1305_SHA256']; | | 17 | +const legacy = 'ECDHE-RSA-AES256-GCM-SHA384'; | | 18 | + | | 19 | +function test(clientCiphers, serverCiphers, expectedCipher, options = {}) { | | 20 | + connect({ | | 21 | + client: { | | 22 | + rejectUnauthorized: false, | | 23 | + minVersion: 'TLSv1.2', | | 24 | + maxVersion: 'TLSv1.3', | | 25 | + ciphers: clientCiphers, | | 26 | + ...options.client, | | 27 | + }, | | 28 | + server: { | | 29 | + key: keys.agent2.key, | | 30 | + cert: keys.agent2.cert, | | 31 | + minVersion: 'TLSv1.2', | | 32 | + maxVersion: 'TLSv1.3', | | 33 | + ciphers: serverCiphers, | | 34 | + ...options.server, | | 35 | + }, | | 36 | + }, common.mustCall((err, pair, cleanup) => { | | 37 | + if (expectedCipher === undefined) { | | 38 | + assert(err); | | 39 | + assert.strictEqual(pair.server.err.code, 'ERR_SSL_NO_SHARED_CIPHER'); | | 40 | + assert(pair.client.err); | | 41 | + } else { | | 42 | + assert.ifError(err); | | 43 | + const expected = Array.isArray(expectedCipher) ? expectedCipher : [expectedCipher]; | | 44 | + const cipher = pair.client.conn.getCipher().name; | | 45 | + assert(expected.includes(cipher)); | | 46 | + for (const { conn } of [pair.client, pair.server]) { | | 47 | + assert.strictEqual(conn.getCipher().name, cipher); | | 48 | + assert.strictEqual(conn.getProtocol(), | | 49 | + expectedCipher === legacy ? 'TLSv1.2' : 'TLSv1.3'); | | 50 | + } | | 51 | + } | | 52 | + cleanup(); | | 53 | + })); | | 54 | +} | | 55 | + | | 56 | +function testLegacyOnlyCipherLists() { | | 57 | + for (const honorCipherOrder of [true, false]) { | | 58 | + const options = { server: { honorCipherOrder } }; | | 59 | + // A legacy-only list retains the backend's TLS 1.3 defaults. Check that | | 60 | + // each peer can negotiate either AES suite without assuming their order. | | 61 | + test(aes128, legacy, aes128, options); | | 62 | + test(aes256, legacy, aes256, options); | | 63 | + test(legacy, aes128, aes128, options); | | 64 | + test(legacy, aes256, aes256, options); | | 65 | + test(legacy, legacy, tls13Ciphers, options); | | 66 | + } | | 67 | +} | | 68 | + | | 69 | +if (process.argv[2] !== undefined) { | | 70 | + const mode = process.argv[2]; | | 71 | + if (mode === 'runtime-defaults') { | | 72 | + tls.DEFAULT_CIPHERS = aes128; | | 73 | + } else { | | 74 | + assert.strictEqual(tls.DEFAULT_CIPHERS, | | 75 | + mode === 'cli-defaults' ? aes128 : legacy); | | 76 | + } | | 77 | + // Explicit TLS 1.3 defaults are still applied when ciphers is omitted, but | | 78 | + // must not replace the native defaults for an explicit legacy-only list. | | 79 | + test(undefined, undefined, mode === 'cli-legacy-defaults' ? tls13Ciphers : aes128); | | 80 | + testLegacyOnlyCipherLists(); | | 81 | + return; | | 82 | +} | | 83 | + | | 84 | +// Both peers must apply the configured TLS 1.3 cipher list. | | 85 | +test(aes256, undefined, aes256); | | 86 | +test(undefined, aes256, aes256); | | 87 | +test(aes128, aes256, undefined); | | 88 | + | | 89 | +// Duplicate names and trailing whitespace must not cause rejection. | | 90 | +const duplicates = `${aes256} \t:${aes256}`; | | 91 | +test(duplicates, duplicates, aes256); | | 92 | + | | 93 | +// OpenSSL 1.1.1 rejects unknown TLS 1.3 cipher names. OpenSSL 3 and BoringSSL | | 94 | +// ignore them and do not interpret exclusion rules in the TLS 1.3 list. | | 95 | +const list = `TLS_UNKNOWN:!${aes128}:${duplicates}`; | | 96 | +if (hasOpenSSL(3) || isBoringSSL) { | | 97 | + test(list, list, aes256); | | 98 | +} else { | | 99 | + assert.throws(() => tls.createSecureContext({ ciphers: list }), { | | 100 | + code: 'ERR_SSL_NO_CIPHER_MATCH', | | 101 | + }); | | 102 | +} | | 103 | +for (const ciphers of ['TLS_UNKNOWN', `!${aes128}`]) { | | 104 | + assert.throws(() => tls.createSecureContext({ ciphers }), { | | 105 | + code: 'ERR_SSL_NO_CIPHER_MATCH', | | 106 | + }); | | 107 | +} | | 108 | +if (isBoringSSL) { | | 109 | + assert.throws(() => tls.createSecureContext({ | | 110 | + ciphers: 'TLS_AES_128_CCM_SHA256', | | 111 | + }), { | | 112 | + code: 'ERR_SSL_NO_CIPHER_MATCH', | | 113 | + }); | | 114 | +} | | 115 | + | | 116 | +// Honor the server's cipher-order option in TLS 1.3 as well as TLS 1.2. | | 117 | +test(`${aes256}:${aes128}`, `${aes128}:${aes256}`, aes128, { | | 118 | + server: { honorCipherOrder: true }, | | 119 | +}); | | 120 | +test(`${aes256}:${aes128}`, `${aes128}:${aes256}`, aes256, { | | 121 | + server: { honorCipherOrder: false }, | | 122 | +}); | | 123 | +testLegacyOnlyCipherLists(); | | 124 | + | | 125 | +// TLS 1.2 configuration and explicit protocol restrictions remain effective. | | 126 | +const mixed = `${legacy}:${aes256}`; | | 127 | +test(mixed, mixed, aes256); | | 128 | +test(mixed, mixed, legacy, { | | 129 | + client: { maxVersion: 'TLSv1.2' }, | | 130 | + server: { maxVersion: 'TLSv1.2' }, | | 131 | +}); | | 132 | +test(mixed, mixed, legacy, { | | 133 | + server: { secureOptions: constants.SSL_OP_NO_TLSv1_3 }, | | 134 | +}); | | 135 | + | | 136 | +// Empty public cipher strings continue to select Node's defaults. | | 137 | +test('', aes256, aes256); | | 138 | +test(aes256, '', aes256); | | 139 | + | | 140 | +for (const [mode, ciphers] of [ | | 141 | + ['runtime-defaults', undefined], | | 142 | + ['cli-defaults', aes128], | | 143 | + ['cli-legacy-defaults', legacy], | | 144 | +]) { | | 145 | + spawnSyncAndAssert(process.execPath, [ | | 146 | + ...process.execArgv, | | 147 | + ...(ciphers === undefined ? [] : [`--tls-cipher-list=${ciphers}`]), | | 148 | + __filename, | | 149 | + mode, | | 150 | + ], {}); | | 151 | +} |
|