Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions deps/ngtcp2/ngtcp2/lib/ngtcp2_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -6808,10 +6808,18 @@ conn_recv_handshake_pkt(ngtcp2_conn *conn, const ngtcp2_path *path,
if (nread2 < 0) {
return nread2;
}

return (ngtcp2_ssize)pktlen;
}

/* Discard 0-RTT packet if we don't have a key to decrypt it. */
return (ngtcp2_ssize)pktlen;
/* Discard 0-RTT packet if we don't have a key to decrypt it and
the ClientHello has been processed. Until then (the
ClientHello spans several Initial packets, or the TLS stack
interrupted the handshake), the 0-RTT key might still be
installed, so buffer the packet. */
if (!conn->hs_pktns || conn->hs_pktns->crypto.rx.ckm) {
return (ngtcp2_ssize)pktlen;
}
}

/* Buffer re-ordered 0-RTT packet. */
Expand Down
2 changes: 2 additions & 0 deletions doc/api/dtls.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ changes:
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.
**Default:** [`tls.DEFAULT_ECDH_CURVE`][].
* `isServer` {boolean} Build a context for a server. **Default:** `false`.
* `key` {string|Buffer} Private key in PEM format.
* `maxVersion` {string} Newest protocol version to offer. See
Expand Down Expand Up @@ -1448,6 +1449,7 @@ The minimum allowed MTU is 256 bytes. The maximum is 65535.
[`session.reused`]: #sessionreused
[`session.send()`]: #sessionsenddata
[`session.session`]: #sessionsession
[`tls.DEFAULT_ECDH_CURVE`]: tls.md#tlsdefault_ecdh_curve
[`tls.TLSSocket.getPeerCertificate()`]: tls.md#tlssocketgetpeercertificatedetailed
[`tls.TLSSocket.getPeerX509Certificate()`]: tls.md#tlssocketgetpeerx509certificate
[`tls.createSecureContext()`]: tls.md#tlscreatesecurecontextoptions
Expand Down
14 changes: 4 additions & 10 deletions doc/api/quic.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,6 @@ Congestion control algorithm identifiers, for use with the
The default TLS 1.3 cipher suite list used when [`sessionOptions.ciphers`][]
is not specified.

### `quic.constants.DEFAULT_GROUPS`

* {string}

The default TLS 1.3 key-exchange group list used when
[`sessionOptions.groups`][] is not specified.

## Class: `QuicEndpoint`

A `QuicEndpoint` encapsulates the local UDP-port binding for QUIC. It can be
Expand Down Expand Up @@ -3282,9 +3275,10 @@ is available. Set to `false` to disable early data support.
added: v23.8.0
-->

* Type: {string}
* Type: {string} **Default:** [`tls.DEFAULT_ECDH_CURVE`][]

The list of supported TLS 1.3 cipher groups.
A colon-separated list of supported key exchange groups, in order of
preference, or `'auto'` for OpenSSL's default list.

#### `sessionOptions.keylog`

Expand Down Expand Up @@ -4919,7 +4913,6 @@ throughput issues caused by flow control.
[`sessionOptions.cc`]: #sessionoptionscc
[`sessionOptions.ciphers`]: #sessionoptionsciphers
[`sessionOptions.datagramDropPolicy`]: #sessionoptionsdatagramdroppolicy
[`sessionOptions.groups`]: #sessionoptionsgroups
[`sessionOptions.keylog`]: #sessionoptionskeylog
[`sessionOptions.qlog`]: #sessionoptionsqlog
[`sessionOptions.sessionTicket`]: #sessionoptionssessionticket
Expand All @@ -4943,6 +4936,7 @@ throughput issues caused by flow control.
[`stream.setPriority()`]: #streamsetpriorityoptions
[`stream.stopSending()`]: #streamstopsendingcode
[`stream.writer`]: #streamwriter
[`tls.DEFAULT_ECDH_CURVE`]: tls.md#tlsdefault_ecdh_curve
[`writer.fail()`]: #streamwriter
[`writer.fail(reason)`]: #streamwriter
[minimal HTTP/3 server]: #minimal-http3-server
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/dtls/dtls.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ const {
kSessionTicket,
} = require('internal/dtls/symbols');

const {
resolveGroups,
} = require('internal/tls/negotiation');

const {
DTLSContext: DTLSContext_,
DTLSEndpoint: DTLSEndpoint_,
Expand Down Expand Up @@ -939,10 +943,7 @@ function createContext(options = kEmptyObject) {
context.setProtocolVersions(kProtocolVersionValues[versions.min],
kProtocolVersionValues[versions.max]);

// ECDH curve (default: 'auto' = OpenSSL default selection)
const ecdhCurve = options.ecdhCurve || 'auto';
validateString(ecdhCurve, 'options.ecdhCurve');
context.setECDHCurve(ecdhCurve);
context.setECDHCurve(resolveGroups(options.ecdhCurve, 'options.ecdhCurve'));

// ALPN protocols
if (options.alpn !== undefined) {
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/quic/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const {
CC_ALGO_CUBIC_STR: CC_ALGO_CUBIC,
CC_ALGO_BBR_STR: CC_ALGO_BBR,
DEFAULT_CIPHERS,
DEFAULT_GROUPS,

// Internal constants for use by the implementation.
// These are not exposed to end users.
Expand Down Expand Up @@ -151,6 +150,10 @@ const {
InternalX509Certificate,
} = require('internal/crypto/x509');

const {
resolveGroups,
} = require('internal/tls/negotiation');

const {
FileHandle,
kHandle: kFileHandle,
Expand Down Expand Up @@ -4910,7 +4913,7 @@ function processTlsOptions(tls, forServer) {
servername,
alpn,
ciphers = DEFAULT_CIPHERS,
groups = DEFAULT_GROUPS,
groups: groupsOption,
keylog = false,
verifyClient = false,
rejectUnauthorized = true,
Expand All @@ -4932,9 +4935,7 @@ function processTlsOptions(tls, forServer) {
if (ciphers !== undefined) {
validateString(ciphers, 'options.ciphers');
}
if (groups !== undefined) {
validateString(groups, 'options.groups');
}
const groups = resolveGroups(groupsOption, 'options.groups');
validateBoolean(keylog, 'options.keylog');
validateBoolean(verifyClient, 'options.verifyClient');
validateBoolean(rejectUnauthorized, 'options.rejectUnauthorized');
Expand Down Expand Up @@ -5469,7 +5470,6 @@ module.exports = {
CC_ALGO_CUBIC,
CC_ALGO_BBR,
DEFAULT_CIPHERS,
DEFAULT_GROUPS,
// These are exported only for internal testing purposes.
getQuicStreamState,
getQuicSessionState,
Expand Down
39 changes: 39 additions & 0 deletions lib/internal/tls/negotiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const {
StringPrototypeTrim,
} = primordials;

const {
codes: {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');

const {
validateString,
} = require('internal/validators');

// The process-wide default for every (D)TLS and QUIC context. It can be
// changed at runtime, so is read when each context is created.
function getDefaultEcdhCurve() {
return require('tls').DEFAULT_ECDH_CURVE || 'auto';
}

// The option's value, or the process-wide default when it is omitted. OpenSSL
// accepts an empty list, leaving no groups and so no key exchange; only
// node:tls, for compatibility, passes allowEmpty.
function resolveGroups(groups, name, allowEmpty = false) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Plan here is a later breaking change to stop node:tls accepting '' as a valid value at all, so they're all consistent, but not today)

if (groups === undefined) {
groups = getDefaultEcdhCurve();
name = 'tls.DEFAULT_ECDH_CURVE';
}
validateString(groups, name);
if (!allowEmpty && StringPrototypeTrim(groups) === '')
throw new ERR_INVALID_ARG_VALUE(name, groups, 'must name at least one group');
return groups;
}

module.exports = {
resolveGroups,
};
16 changes: 6 additions & 10 deletions lib/internal/tls/secure-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@ const {
toBuf,
} = require('internal/crypto/util');

const {
resolveGroups,
} = require('internal/tls/negotiation');

const {
crypto: {
TLS1_2_VERSION,
TLS1_3_VERSION,
},
} = internalBinding('constants');

function getDefaultEcdhCurve() {
// We do it this way because DEFAULT_ECDH_CURVE can be
// changed by users, so we need to grab the current
// value, but we want the evaluation to be lazy.
return require('tls').DEFAULT_ECDH_CURVE || 'auto';
}

function getDefaultCiphers() {
// We do it this way because DEFAULT_CIPHERS can be
// changed by users, so we need to grab the current
Expand Down Expand Up @@ -156,7 +153,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options')
ciphers = getDefaultCiphers(),
crl,
dhparam,
ecdhCurve = getDefaultEcdhCurve(),
ecdhCurve,
key,
passphrase,
pfx,
Expand Down Expand Up @@ -270,8 +267,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options')
context.setSigalgs(sigalgs);
}

validateString(ecdhCurve, `${name}.ecdhCurve`);
context.setECDHCurve(ecdhCurve);
context.setECDHCurve(resolveGroups(ecdhCurve, `${name}.ecdhCurve`, true));

if (dhparam !== undefined && dhparam !== null) {
validateKeyOrCertOption(`${name}.dhparam`, dhparam);
Expand Down
2 changes: 0 additions & 2 deletions lib/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const {
CC_ALGO_CUBIC,
CC_ALGO_BBR,
DEFAULT_CIPHERS,
DEFAULT_GROUPS,
} = require('internal/quic/quic');

const cc = {
Expand All @@ -29,7 +28,6 @@ const cc = {
const constants = {
get cc() { return cc; },
get DEFAULT_CIPHERS() { return DEFAULT_CIPHERS; },
get DEFAULT_GROUPS() { return DEFAULT_GROUPS; },
};

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
'src/crypto/crypto_client_hello.cc',
'src/crypto/crypto_context.cc',
'src/crypto/crypto_tls_certificates.cc',
'src/crypto/crypto_tls_negotiation.cc',
'src/crypto/crypto_ec.cc',
'src/crypto/crypto_kem.cc',
'src/crypto/crypto_hmac.cc',
Expand Down Expand Up @@ -451,6 +452,7 @@
'src/crypto/crypto_tls.h',
'src/crypto/crypto_context.h',
'src/crypto/crypto_tls_certificates.h',
'src/crypto/crypto_tls_negotiation.h',
'src/crypto/crypto_ec.h',
'src/crypto/crypto_hkdf.h',
'src/crypto/crypto_pbkdf2.h',
Expand Down
3 changes: 2 additions & 1 deletion src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "crypto/crypto_common.h"
#include "crypto/crypto_pkcs12.h"
#include "crypto/crypto_tls_certificates.h"
#include "crypto/crypto_tls_negotiation.h"
#include "crypto/crypto_util.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
Expand Down Expand Up @@ -2067,7 +2068,7 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {

Utf8Value curve(env->isolate(), args[0]);

if (curve != "auto" && !SSL_CTX_set1_curves_list(sc->ctx_.get(), *curve)) {
if (!SetGroups(sc->ctx_, *curve)) {
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to set ECDH curve");
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/crypto/crypto_tls_negotiation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "crypto/crypto_tls_negotiation.h"

#include <cstring>

namespace node::crypto {

bool SetGroups(const ncrypto::SSLCtxPointer& ctx, const char* groups) {
if (strcmp(groups, "auto") == 0) return true;
return SSL_CTX_set1_groups_list(ctx.get(), groups) == 1;
}

} // namespace node::crypto
16 changes: 16 additions & 0 deletions src/crypto/crypto_tls_negotiation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SRC_CRYPTO_CRYPTO_TLS_NEGOTIATION_H_
#define SRC_CRYPTO_CRYPTO_TLS_NEGOTIATION_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "ncrypto.h"

namespace node::crypto {

// Set the supported key exchange groups. "auto" keeps OpenSSL's default list.
bool SetGroups(const ncrypto::SSLCtxPointer& ctx, const char* groups);

} // namespace node::crypto

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_CRYPTO_CRYPTO_TLS_NEGOTIATION_H_
9 changes: 4 additions & 5 deletions src/dtls/dtls_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <crypto/crypto_client_hello.h>
#include <crypto/crypto_context.h>
#include <crypto/crypto_tls_certificates.h>
#include <crypto/crypto_tls_negotiation.h>
#include <crypto/crypto_util.h>
#include <env-inl.h>
#include <memory_tracker-inl.h>
Expand Down Expand Up @@ -1081,14 +1082,12 @@ void DTLSContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Environment* env = ctx->env();

ncrypto::ClearErrorOnReturn clear_error_on_return;
CHECK(args[0]->IsString());
Utf8Value curve(env->isolate(), args[0]);

// "auto" means use OpenSSL's default curve selection.
if (strcmp(*curve, "auto") != 0) {
if (!SSL_CTX_set1_curves_list(ctx->ctx_.get(), *curve)) {
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to set ECDH curve");
}
if (!crypto::SetGroups(ctx->ctx_, *curve)) {
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to set ECDH curve");
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/quic/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4541,8 +4541,6 @@ void Session::InitPerContext(Realm* realm, Local<Object> target) {

NODE_DEFINE_STRING_CONSTANT(
target, "DEFAULT_CIPHERS", TLSContext::DEFAULT_CIPHERS);
NODE_DEFINE_STRING_CONSTANT(
target, "DEFAULT_GROUPS", TLSContext::DEFAULT_GROUPS);

#define V(name, _) IDX_STATS_SESSION_##name,
enum SessionStatsIdx { SESSION_STATS(V) IDX_STATS_SESSION_COUNT };
Expand Down
3 changes: 2 additions & 1 deletion src/quic/tlscontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <base_object-inl.h>
#include <crypto/crypto_context.h>
#include <crypto/crypto_tls_certificates.h>
#include <crypto/crypto_tls_negotiation.h>
#include <crypto/crypto_util.h>
#include <debug_utils-inl.h>
#include <env-inl.h>
Expand Down Expand Up @@ -592,7 +593,7 @@ SSLCtxPointer TLSContext::Initialize(Environment* env) {
return SSLCtxPointer();
}

if (SSL_CTX_set1_groups_list(ctx.get(), options_.groups.c_str()) != 1) {
if (!crypto::SetGroups(ctx, options_.groups.c_str())) {
validation_error_ = "Invalid cipher groups";
return SSLCtxPointer();
}
Expand Down
5 changes: 2 additions & 3 deletions src/quic/tlscontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ class TLSContext final : public MemoryRetainer,
"TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_"
"SHA256:TLS_AES_128_CCM_SHA256";
static constexpr auto DEFAULT_GROUPS = "X25519:P-256:P-384:P-521";

struct Options final : public MemoryRetainer {
// The SNI servername to use for this session. This option is only used by
Expand All @@ -226,8 +225,8 @@ class TLSContext final : public MemoryRetainer,
// The list of TLS ciphers to use for this session.
std::string ciphers = DEFAULT_CIPHERS;

// The list of TLS groups to use for this session.
std::string groups = DEFAULT_GROUPS;
// The key exchange groups. "auto" keeps OpenSSL's defaults.
std::string groups = "auto";

// TLS certificate compression (RFC 8879) preference, packed by the JS
// layer as (length | alg0<<8 | alg1<<16 | alg2<<24). Algorithm IDs match
Expand Down
Loading
Loading