Skip to content
Open
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
19 changes: 19 additions & 0 deletions doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,25 @@ TLSv1.2 and below.
console.log(tls.getCiphers()); // ['aes128-gcm-sha256', 'aes128-sha', ...]
```

## `tls.getGroups()`

<!-- YAML
added: REPLACEME
-->

* Returns: {string\[]}

Returns an array with the names of the TLS groups that can be used in the
`ecdhCurve` option of [`tls.createSecureContext()`][] and in
[`tls.DEFAULT_ECDH_CURVE`][]. Not all supported groups are enabled by default.

Requires OpenSSL 3.5 or later. Throws `ERR_CRYPTO_UNSUPPORTED_OPERATION`
otherwise.

```js
console.log(tls.getGroups()); // ['MLKEM1024', 'MLKEM512', 'MLKEM768', ...]
```

## `tls.getCertificateCompressionAlgorithms()`

<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const {
resetRootCertStore,
getUserRootCertificates,
getSSLCiphers,
getSSLGroups,
getCertificateCompressionAlgorithms,
startLoadingCertificatesOffThread,
} = internalBinding('crypto');
Expand Down Expand Up @@ -137,6 +138,10 @@ exports.getCiphers = internalUtil.cachedResult(
() => internalUtil.filterDuplicateStrings(getSSLCiphers(), true),
);

exports.getGroups = internalUtil.cachedResult(
() => internalUtil.filterDuplicateStrings(getSSLGroups()),
);

exports.getCertificateCompressionAlgorithms = internalUtil.cachedResult(
() => getCertificateCompressionAlgorithms(),
);
Expand Down
36 changes: 36 additions & 0 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,40 @@ void CipherBase::GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size()));
}

void CipherBase::GetSSLGroups(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
#ifdef SSL_CTRL_GET0_IMPLEMENTED_GROUPS
ClearErrorOnReturn clear_error_on_return;

auto ctx = SSLCtxPointer::New();
if (!ctx) {
return ThrowCryptoError(
env, clear_error_on_return.peekError(), "SSL_CTX_new");
}

STACK_OF(OPENSSL_CSTRING)* names = sk_OPENSSL_CSTRING_new_null();
if (names == nullptr) return THROW_ERR_MEMORY_ALLOCATION_FAILED(env);
auto free_names = OnScopeLeave([&] { sk_OPENSSL_CSTRING_free(names); });

if (!SSL_CTX_get0_implemented_groups(ctx.get(), 0, names)) {
return ThrowCryptoError(env,
clear_error_on_return.peekError(),
"SSL_CTX_get0_implemented_groups");
}

LocalVector<Value> arr(env->isolate());
for (int i = 0; i < sk_OPENSSL_CSTRING_num(names); i++) {
arr.push_back(
OneByteString(env->isolate(), sk_OPENSSL_CSTRING_value(names, i)));
}

args.GetReturnValue().Set(Array::New(env->isolate(), arr.data(), arr.size()));
#else
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(
env, "Listing TLS groups requires OpenSSL 3.5 or later");
#endif
}

void CipherBase::GetCiphers(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::vector<std::string> cipher_names;
Expand Down Expand Up @@ -253,6 +287,7 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
SetConstructorFunction(context, target, "CipherBase", t);

SetMethodNoSideEffect(context, target, "getSSLCiphers", GetSSLCiphers);
SetMethodNoSideEffect(context, target, "getSSLGroups", GetSSLGroups);
SetMethodNoSideEffect(context, target, "getCiphers", GetCiphers);

SetMethod(context,
Expand Down Expand Up @@ -294,6 +329,7 @@ void CipherBase::RegisterExternalReferences(
registry->Register(SetAAD);

registry->Register(GetSSLCiphers);
registry->Register(GetSSLGroups);
registry->Register(GetCiphers);

registry->Register(PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace crypto {
class CipherBase : public BaseObject {
public:
static void GetSSLCiphers(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSSLGroups(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCiphers(const v8::FunctionCallbackInfo<v8::Value>& args);

static void Initialize(Environment* env, v8::Local<v8::Object> target);
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-tls-get-groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const { hasOpenSSL, isBoringSSL } = require('../common/crypto');
const tls = require('tls');

if (isBoringSSL || !hasOpenSSL(3, 5)) {
assert.throws(() => tls.getGroups(), {
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
});
return;
}

const groups = tls.getGroups();
assert(Array.isArray(groups));
assert(groups.length > 0);
assert(groups.includes('secp256r1'));
assert(groups.includes('MLKEM768'));

// Each group is accepted by the ecdhCurve option.
for (const group of groups) {
tls.createSecureContext({ ecdhCurve: group });
}

// The returned array is a copy.
groups.length = 0;
assert.notStrictEqual(tls.getGroups().length, 0);
Loading