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
24 changes: 22 additions & 2 deletions doc/api/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,9 @@ added: v5.0.0
Returns an object describing ephemeral key agreement in [perfect forward
secrecy][] on a client connection. It returns an empty object when the key
agreement is not ephemeral. As this is only supported on a client socket;
`null` is returned if called on a server socket. The supported types are `'DH'`,
`'ECDH'`, and `'TLSGroup'`. For `'DH'` and `'ECDH'`, the object describes peer
`null` is returned if called on a server socket. Use
[`tlsSocket.getNegotiatedGroup()`][] to get the negotiated group on a server
socket. The supported types are `'DH'`, `'ECDH'`, and `'TLSGroup'`. For `'DH'` and `'ECDH'`, the object describes peer
temporary key parameters. For `'TLSGroup'`, the object identifies the negotiated
TLS Supported Group used for key agreement when a peer temporary key object is
not available.
Expand Down Expand Up @@ -1302,6 +1303,23 @@ provided by SSL/TLS is not desired or is not enough.
Corresponds to the `SSL_get_finished` routine in OpenSSL and may be used
to implement the `tls-unique` channel binding from [RFC 5929][].

### `tlsSocket.getNegotiatedGroup()`

<!-- YAML
added: REPLACEME
-->

* Returns: {string|undefined} The name of the TLS group used for key
agreement, or `undefined` if no group was negotiated.

Returns the TLS Supported Group negotiated for key agreement, for example
`'X25519MLKEM768'`, `'x25519'`, or `'secp256r1'`. Unlike
[`tlsSocket.getEphemeralKeyInfo()`][], this method works on both client and
server sockets. `undefined` is returned when the key agreement does not use a
group, such as with TLSv1.2 RSA key exchange, or before the handshake has
completed. With TLSv1.2 DHE key exchange, only the server socket reports the
group.

### `tlsSocket.getPeerCertificate([detailed])`

<!-- YAML
Expand Down Expand Up @@ -2714,6 +2732,8 @@ added: v0.11.3
[`tls.getCACertificates()`]: #tlsgetcacertificatestype
[`tls.getCiphers()`]: #tlsgetciphers
[`tls.rootCertificates`]: #tlsrootcertificates
[`tlsSocket.getEphemeralKeyInfo()`]: #tlssocketgetephemeralkeyinfo
[`tlsSocket.getNegotiatedGroup()`]: #tlssocketgetnegotiatedgroup
[`tlsSocket.renegotiate()`]: #tlssocketrenegotiateoptions-callback
[`x509.checkHost()`]: crypto.md#x509checkhostname-options
[asn1.js]: https://www.npmjs.com/package/asn1.js
Expand Down
1 change: 1 addition & 0 deletions lib/internal/tls/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ function makeSocketMethodProxy(name) {
'getSharedSigalgs',
'getEphemeralKeyInfo',
'getFinished',
'getNegotiatedGroup',
'getPeerFinished',
'getProtocol',
'getTLSTicket',
Expand Down
30 changes: 30 additions & 0 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,33 @@ void TLSWrap::GetEphemeralKeyInfo(const FunctionCallbackInfo<Value>& args) {
// ERR_get_error())
}

void TLSWrap::GetNegotiatedGroup(const FunctionCallbackInfo<Value>& args) {
TLSWrap* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
CHECK(w->ssl_);

// Before the handshake there is no session. SSL_get0_group_name() before
// OpenSSL 4.1, and SSL_get_negotiated_group() in OpenSSL 3.0, read it
// without checking.
SSL* ssl = w->ssl_.get();
if (SSL_get_session(ssl) == nullptr) return;

if (auto group = w->ssl_.getNegotiatedGroup()) {
args.GetReturnValue().Set(OneByteString(
args.GetIsolate(), group.value().data(), group.value().length()));
return;
}

#if !NCRYPTO_USE_BORINGSSL
// SSL_get0_group_name() requires OpenSSL 3.2.
const int nid = SSL_get_negotiated_group(ssl);
if (nid == NID_undef) return;
if (const char* name = SSL_group_to_name(ssl, nid)) {
args.GetReturnValue().Set(OneByteString(args.GetIsolate(), name));
}
#endif
}

void TLSWrap::GetProtocol(const FunctionCallbackInfo<Value>& args) {
TLSWrap* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
Expand Down Expand Up @@ -2520,6 +2547,8 @@ void TLSWrap::Initialize(
SetProtoMethodNoSideEffect(
isolate, t, "getEphemeralKeyInfo", GetEphemeralKeyInfo);
SetProtoMethodNoSideEffect(isolate, t, "getFinished", GetFinished);
SetProtoMethodNoSideEffect(
isolate, t, "getNegotiatedGroup", GetNegotiatedGroup);
SetProtoMethodNoSideEffect(
isolate, t, "getPeerCertificate", GetPeerCertificate);
SetProtoMethodNoSideEffect(
Expand Down Expand Up @@ -2590,6 +2619,7 @@ void TLSWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetPeerX509Certificate);
registry->Register(HasPeerCertificate);
registry->Register(GetPeerFinished);
registry->Register(GetNegotiatedGroup);
registry->Register(GetProtocol);
registry->Register(GetSession);
registry->Register(GetSharedSigalgs);
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ class TLSWrap : public AsyncWrap,
static void GetEphemeralKeyInfo(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetFinished(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetNegotiatedGroup(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPeerCertificate(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPeerX509Certificate(
Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-tls-get-negotiated-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

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

if (isBoringSSL)
common.skip('group names differ on BoringSSL');

const assert = require('assert');
const fixtures = require('../common/fixtures');
const tls = require('tls');

const key = fixtures.readKey('agent2-key.pem');
const cert = fixtures.readKey('agent2-cert.pem');

const tests = [
[{ ecdhCurve: 'P-256' }, {}, 'secp256r1'],
[{ ecdhCurve: 'P-384' }, {}, 'secp384r1'],
[{ ecdhCurve: 'P-256' },
{ maxVersion: 'TLSv1.2', ciphers: 'ECDHE-RSA-AES256-GCM-SHA384' },
'secp256r1'],
];

if (!hasFIPS(3)) {
tests.push([{},
{ maxVersion: 'TLSv1.2', ciphers: 'AES256-SHA256' },
undefined]);
}

if (hasOpenSSL(3, 5) && !hasFIPS()) {
tests.push([{}, {}, 'X25519MLKEM768']);
}

function test([serverOptions, clientOptions, expected]) {
const options = { key, cert, ...serverOptions };
const server = tls.createServer(options, common.mustCall((conn) => {
assert.strictEqual(conn.getNegotiatedGroup(), expected);
conn.end();
}));

server.listen(0, common.mustCall(() => {
const client = tls.connect({
port: server.address().port,
rejectUnauthorized: false,
...clientOptions,
}, common.mustCall(() => {
assert.strictEqual(client.getNegotiatedGroup(), expected);
client.end();
server.close();
}));
assert.strictEqual(client.getNegotiatedGroup(), undefined);
}));
}

tests.forEach(test);
Loading