Commit 2da9d74943a for nodejs
commit 2da9d74943a92ced1b0510eca5ad2a75edf6bd3b
Author: James M Snell <jasnell@gmail.com>
Date: Fri Nov 27 22:34:55 2026 +0000
net: accept DTLS 1.3 cipher suites in ciphers
TLS_ names in `ciphers` configure the DTLS 1.3 suites, as in node:tls.
Assisted-by: Claude Opus 5.5
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67837
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
diff --git a/doc/api/dtls.md b/doc/api/dtls.md
index 7b92bfebfef..5e92cce68cd 100644
--- a/doc/api/dtls.md
+++ b/doc/api/dtls.md
@@ -94,7 +94,9 @@ changes:
* `port` {number} Port to bind to. **Required.**
* `host` {string} Address to bind to. **Default:** `'0.0.0.0'`.
* `ca` {string|Buffer|string\[]|Buffer\[]} CA certificates in PEM format.
- * `ciphers` {string} OpenSSL cipher list string.
+ * `ciphers` {string} OpenSSL cipher list string. Names beginning `TLS_`
+ select DTLS 1.3 cipher suites; the rest select DTLS 1.2 cipher suites.
+ See [Protocol versions][].
* `minVersion` {string} The oldest protocol version to accept, `'DTLSv1.2'`
or `'DTLSv1.3'`. **Default:** `'DTLSv1.2'`. See [Protocol versions][].
* `maxVersion` {string} The newest protocol version to offer, `'DTLSv1.2'`
@@ -270,7 +272,9 @@ changes:
* `ca` {string|Buffer|Array} CA certificates in PEM format. When omitted,
the bundled default certificate authorities are used.
* `cert` {string|Buffer} Certificate in PEM format.
- * `ciphers` {string} OpenSSL cipher suite list.
+ * `ciphers` {string} OpenSSL cipher list string. Names beginning `TLS_`
+ select DTLS 1.3 cipher suites; the rest select DTLS 1.2 cipher suites.
+ See [Protocol versions][].
* `ecdhCurve` {string} Named curve or curve list for ECDH.
* `isServer` {boolean} Build a context for a server. **Default:** `false`.
* `key` {string|Buffer} Private key in PEM format.
@@ -377,8 +381,12 @@ What changes under DTLS 1.3:
rather than stalling; see [Pre-shared keys][].
* [`session.send()`][] accepts at most 16383 bytes, and
[`session.exportKeyingMaterial()`][] at most 255 hash lengths.
-* The `ciphers` option applies to DTLS 1.2 cipher suites only. DTLS 1.3 uses
- OpenSSL's default TLS 1.3 suites.
+* DTLS 1.3 has its own cipher suites, named like TLS 1.3's. As in `node:tls`,
+ the names in `ciphers` that begin `TLS_` configure them and the rest
+ configure DTLS 1.2. A string without any `TLS_` names leaves the default
+ DTLS 1.3 suites in place, so it does not restrict a DTLS 1.3 handshake. One
+ with only `TLS_` names raises `minVersion` to `'DTLSv1.3'`, since DTLS 1.2
+ would have nothing to negotiate, and throws if DTLS 1.3 is not enabled.
## Server Name Indication
@@ -573,9 +581,11 @@ exception.
### Cipher suites
-The default cipher list excludes PSK, so giving `psk` without `ciphers`
-enables the PSK suites. Supplying `ciphers` disables that and uses exactly
-what was asked for.
+The default DTLS 1.2 cipher list excludes PSK, so giving `psk` without `ciphers`
+enables the PSK suites. DTLS 1.3 needs no special suites for PSK, but a key
+supplied by a callback or a map can only be used with a SHA-256 suite, which the
+defaults include. Supplying `ciphers` disables that and uses exactly what was
+asked for.
A server keeps the certificate suites as well, since it may serve both kinds
of client on one port. A client does not: a client that configured a
diff --git a/lib/internal/dtls/dtls.js b/lib/internal/dtls/dtls.js
index 0239ec118eb..8f4db033b4b 100644
--- a/lib/internal/dtls/dtls.js
+++ b/lib/internal/dtls/dtls.js
@@ -9,6 +9,7 @@ const {
ArrayIsArray,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
+ ArrayPrototypePush,
FunctionPrototypeBind,
MathMax,
ObjectKeys,
@@ -16,6 +17,8 @@ const {
PromiseWithResolvers,
SafeSet,
StringPrototypeSlice,
+ StringPrototypeSplit,
+ StringPrototypeStartsWith,
SymbolAsyncDispose,
} = primordials;
@@ -849,7 +852,8 @@ function createContext(options = kEmptyObject) {
const context = new DTLSContext_(isServer);
- applyProtocolVersions(context, options.minVersion, options.maxVersion);
+ const versions = resolveProtocolVersions(options.minVersion,
+ options.maxVersion);
// Pre-shared keys (RFC 4279).
if (options.psk !== undefined) {
@@ -912,7 +916,7 @@ function createContext(options = kEmptyObject) {
// Ciphers
if (options.ciphers !== undefined) {
validateString(options.ciphers, 'options.ciphers');
- context.setCiphers(options.ciphers);
+ applyCiphers(context, options.ciphers, versions);
} else if (options.psk !== undefined) {
// The default cipher list excludes PSK, so a psk option with no ciphers
// would negotiate nothing and fail with a bare handshake_failure. Turn
@@ -926,6 +930,10 @@ function createContext(options = kEmptyObject) {
context.setCiphers(isServer ? kPSKCiphersServer : kPSKCiphersClient);
}
+ // Applied after the ciphers, which may raise the floor.
+ context.setProtocolVersions(kProtocolVersionValues[versions.min],
+ kProtocolVersionValues[versions.max]);
+
// ECDH curve (default: 'auto' = OpenSSL default selection)
const ecdhCurve = options.ecdhCurve || 'auto';
validateString(ecdhCurve, 'options.ecdhCurve');
@@ -1050,7 +1058,9 @@ const kProtocolVersionValues = [DTLS1_2_VERSION_VALUE, DTLS1_3_VERSION_VALUE];
const kDefaultMinVersion = 'DTLSv1.2';
const kDefaultMaxVersion = 'DTLSv1.2';
-function applyProtocolVersions(context, minVersion, maxVersion) {
+// Validate minVersion and maxVersion and return them as indices into
+// kProtocolVersions.
+function resolveProtocolVersions(minVersion, maxVersion) {
if (minVersion === undefined) minVersion = kDefaultMinVersion;
validateOneOf(minVersion, 'options.minVersion', kProtocolVersions);
const min = ArrayPrototypeIndexOf(kProtocolVersions, minVersion);
@@ -1070,8 +1080,47 @@ function applyProtocolVersions(context, minVersion, maxVersion) {
'options.minVersion', minVersion,
`must not be newer than options.maxVersion ('${maxVersion}')`);
}
- context.setProtocolVersions(kProtocolVersionValues[min],
- kProtocolVersionValues[max]);
+ return { min, max };
+}
+
+const kDTLS13 = 1; // Index of 'DTLSv1.3' in kProtocolVersions.
+
+// Split a cipher string the way node:tls does: TLS_ names are DTLS 1.3 cipher
+// suites, which OpenSSL configures separately, and everything else is the
+// DTLS 1.2 cipher list. Naming no DTLS 1.3 suites leaves OpenSSL's default
+// ones in place. Naming only DTLS 1.3 suites leaves DTLS 1.2 with nothing to
+// negotiate, so the floor is raised to DTLS 1.3, as node:tls raises it to
+// TLS 1.3 -- or, if DTLS 1.3 is not enabled, the string is refused.
+function applyCiphers(context, ciphers, versions) {
+ const list = [];
+ const suites = [];
+ for (const cipher of StringPrototypeSplit(ciphers, ':')) {
+ if (cipher.length === 0) continue;
+ if (StringPrototypeStartsWith(cipher, 'TLS_') ||
+ StringPrototypeStartsWith(cipher, '!TLS_')) {
+ ArrayPrototypePush(suites, cipher);
+ } else {
+ ArrayPrototypePush(list, cipher);
+ }
+ }
+
+ if (suites.length > 0) {
+ if (versions.max < kDTLS13 && list.length === 0) {
+ throw new ERR_INVALID_ARG_VALUE(
+ 'options.ciphers', ciphers,
+ 'names only DTLS 1.3 cipher suites, but DTLS 1.3 is not enabled');
+ }
+ context.setCipherSuites(ArrayPrototypeJoin(suites, ':'));
+ }
+
+ if (list.length > 0) {
+ context.setCiphers(ArrayPrototypeJoin(list, ':'));
+ } else if (suites.length > 0) {
+ versions.min = MathMax(versions.min, kDTLS13);
+ } else {
+ // Nothing at all, which OpenSSL reports for itself.
+ context.setCiphers(ciphers);
+ }
}
// Cipher list used when psk is given and ciphers is not.
diff --git a/src/dtls/dtls_context.cc b/src/dtls/dtls_context.cc
index c4d9aa52a7f..3ac2707bcbe 100644
--- a/src/dtls/dtls_context.cc
+++ b/src/dtls/dtls_context.cc
@@ -206,6 +206,7 @@ Local<FunctionTemplate> DTLSContext::GetConstructorTemplate(Environment* env) {
SetProtoMethod(isolate, tmpl, "setKey", SetKey);
SetProtoMethod(isolate, tmpl, "addCACert", AddCACert);
SetProtoMethod(isolate, tmpl, "setCiphers", SetCiphers);
+ SetProtoMethod(isolate, tmpl, "setCipherSuites", SetCipherSuites);
SetProtoMethod(isolate, tmpl, "setALPN", SetALPN);
SetProtoMethod(isolate, tmpl, "setSRTP", SetSRTP);
SetProtoMethod(isolate, tmpl, "setVerifyMode", SetVerifyMode);
@@ -239,6 +240,7 @@ void DTLSContext::RegisterExternalReferences(
registry->Register(SetKey);
registry->Register(AddCACert);
registry->Register(SetCiphers);
+ registry->Register(SetCipherSuites);
registry->Register(SetALPN);
registry->Register(SetSRTP);
registry->Register(SetVerifyMode);
@@ -484,6 +486,21 @@ void DTLSContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
}
}
+// DTLS 1.3 cipher suites, which OpenSSL configures apart from the cipher list
+// used by DTLS 1.2 and earlier: SSL_CTX_set_cipher_list() does not touch them.
+void DTLSContext::SetCipherSuites(const FunctionCallbackInfo<Value>& args) {
+ DTLSContext* ctx;
+ ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
+ Environment* env = ctx->env();
+
+ CHECK(args[0]->IsString());
+ Utf8Value suites(env->isolate(), args[0]);
+ if (SSL_CTX_set_ciphersuites(ctx->ctx_.get(), *suites) != 1) {
+ return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
+ "SSL_CTX_set_ciphersuites failed");
+ }
+}
+
void DTLSContext::SetALPN(const FunctionCallbackInfo<Value>& args) {
DTLSContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
diff --git a/src/dtls/dtls_context.h b/src/dtls/dtls_context.h
index a9b30204b3d..a29646e09bd 100644
--- a/src/dtls/dtls_context.h
+++ b/src/dtls/dtls_context.h
@@ -86,6 +86,7 @@ class DTLSContext final : public BaseObject {
static void SetKey(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AddCACert(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetCiphers(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void SetCipherSuites(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetALPN(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetSRTP(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetVerifyMode(const v8::FunctionCallbackInfo<v8::Value>& args);
diff --git a/test/parallel/test-dtls-ciphersuites.mjs b/test/parallel/test-dtls-ciphersuites.mjs
new file mode 100644
index 00000000000..06a43350d93
--- /dev/null
+++ b/test/parallel/test-dtls-ciphersuites.mjs
@@ -0,0 +1,110 @@
+// Flags: --experimental-dtls --no-warnings
+
+// Test: the ciphers option configures DTLS 1.3 cipher suites as well as the
+// DTLS 1.2 cipher list.
+//
+// OpenSSL keeps the two apart -- SSL_CTX_set_cipher_list() has no effect on
+// TLS 1.3 suites -- so, as in node:tls, TLS_ names in the string are applied
+// as DTLS 1.3 suites and everything else as the DTLS 1.2 list.
+
+import { hasCrypto, mustNotCall, skip } from '../common/index.mjs';
+import * as fixtures from '../common/fixtures.mjs';
+import assert from 'node:assert';
+
+if (!hasCrypto) {
+ skip('missing crypto');
+}
+
+if (!process.features.dtls) {
+ skip('DTLS is not enabled');
+}
+
+const { connect, createSecureContext, listen } = await import('node:dtls');
+
+const HOST = '127.0.0.1';
+const cert = fixtures.readKey('agent1-cert.pem').toString();
+const key = fixtures.readKey('agent1-key.pem').toString();
+
+const V12 = { minVersion: 'DTLSv1.2', maxVersion: 'DTLSv1.2' };
+const V13 = { minVersion: 'DTLSv1.3', maxVersion: 'DTLSv1.3' };
+const BOTH = { minVersion: 'DTLSv1.2', maxVersion: 'DTLSv1.3' };
+
+async function handshake(serverOptions, clientOptions) {
+ const serverCipher = Promise.withResolvers();
+ const endpoint = listen((session) => {
+ session.onhandshake = () => serverCipher.resolve(session.cipher.name);
+ session.onerror = () => {};
+ }, { cert, key, host: HOST, port: 0, ...serverOptions });
+ const client = connect(HOST, endpoint.address.port, {
+ rejectUnauthorized: false, handshakeTimeout: 2000, ...clientOptions,
+ });
+ try {
+ const { protocol } = await client.opened;
+ const cipher = client.cipher.name;
+ assert.strictEqual(await serverCipher.promise, cipher);
+ await client.close();
+ return { protocol, cipher };
+ } finally {
+ client.destroy();
+ await endpoint.close();
+ }
+}
+
+// A DTLS 1.3 suite is selected by name, on either side.
+for (const suite of ['TLS_AES_128_GCM_SHA256', 'TLS_CHACHA20_POLY1305_SHA256']) {
+ assert.deepStrictEqual(
+ await handshake({ ciphers: suite, ...BOTH }, V13),
+ { protocol: 'DTLSv1.3', cipher: suite });
+ assert.deepStrictEqual(
+ await handshake(BOTH, { ciphers: suite, ...V13 }),
+ { protocol: 'DTLSv1.3', cipher: suite });
+}
+
+// A mixed string configures both versions.
+{
+ const ciphers = 'ECDHE-RSA-AES128-GCM-SHA256:TLS_CHACHA20_POLY1305_SHA256';
+ assert.deepStrictEqual(
+ await handshake({ ciphers, ...BOTH }, V13),
+ { protocol: 'DTLSv1.3', cipher: 'TLS_CHACHA20_POLY1305_SHA256' });
+ assert.deepStrictEqual(
+ await handshake({ ciphers, ...BOTH }, V12),
+ { protocol: 'DTLSv1.2', cipher: 'ECDHE-RSA-AES128-GCM-SHA256' });
+}
+
+// A DTLS 1.2 list leaves the default DTLS 1.3 suites in place, so it does
+// not restrict a DTLS 1.3 handshake.
+{
+ const { protocol, cipher } = await handshake(
+ { ciphers: 'ECDHE-RSA-AES128-GCM-SHA256', ...BOTH }, V13);
+ assert.strictEqual(protocol, 'DTLSv1.3');
+ assert.match(cipher, /^TLS_/);
+}
+
+// Naming only DTLS 1.3 suites raises the floor to DTLS 1.3: DTLS 1.2 would
+// have nothing to negotiate.
+assert.strictEqual(
+ (await handshake({ ciphers: 'TLS_AES_128_GCM_SHA256', ...BOTH }, BOTH))
+ .protocol,
+ 'DTLSv1.3');
+await assert.rejects(
+ handshake({ ciphers: 'TLS_AES_128_GCM_SHA256', ...BOTH }, V12),
+ /protocol version/);
+
+// ...and is refused outright where DTLS 1.3 is not enabled.
+assert.throws(() => createSecureContext({
+ ciphers: 'TLS_AES_128_GCM_SHA256', ...V12,
+}), { code: 'ERR_INVALID_ARG_VALUE', message: /DTLS 1\.3 is not enabled/ });
+
+// No DTLS 1.3 suite in common fails the handshake.
+await assert.rejects(
+ handshake({ ciphers: 'TLS_AES_128_GCM_SHA256', ...V13 },
+ { ciphers: 'TLS_CHACHA20_POLY1305_SHA256', ...V13 }),
+ /handshake failure/);
+
+// Names OpenSSL does not know are rejected when the context is built.
+assert.throws(() => listen(mustNotCall(), {
+ cert, key, host: HOST, port: 0, ciphers: 'TLS_NOT_A_SUITE', ...V13,
+}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
+assert.throws(() => listen(mustNotCall(), {
+ cert, key, host: HOST, port: 0, ciphers: '', ...V13,
+}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });