Skip to content

Commit 59779de

Browse files
committed
tls: add tlsSocket.getNegotiatedGroup()
Return the TLS group negotiated for key agreement. Unlike getEphemeralKeyInfo(), it also works on server sockets. Assisted-by: Claude Opus 5.5 Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 6545c34 commit 59779de

5 files changed

Lines changed: 96 additions & 2 deletions

File tree

‎doc/api/tls.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,9 @@ added: v5.0.0
12711271
Returns an object describing ephemeral key agreement in [perfect forward
12721272
secrecy][] on a client connection. It returns an empty object when the key
12731273
agreement is not ephemeral. As this is only supported on a client socket;
1274-
`null` is returned if called on a server socket. The supported types are `'DH'`,
1275-
`'ECDH'`, and `'TLSGroup'`. For `'DH'` and `'ECDH'`, the object describes peer
1274+
`null` is returned if called on a server socket. Use
1275+
[`tlsSocket.getNegotiatedGroup()`][] to get the negotiated group on a server
1276+
socket. The supported types are `'DH'`, `'ECDH'`, and `'TLSGroup'`. For `'DH'` and `'ECDH'`, the object describes peer
12761277
temporary key parameters. For `'TLSGroup'`, the object identifies the negotiated
12771278
TLS Supported Group used for key agreement when a peer temporary key object is
12781279
not available.
@@ -1302,6 +1303,23 @@ provided by SSL/TLS is not desired or is not enough.
13021303
Corresponds to the `SSL_get_finished` routine in OpenSSL and may be used
13031304
to implement the `tls-unique` channel binding from [RFC 5929][].
13041305

1306+
### `tlsSocket.getNegotiatedGroup()`
1307+
1308+
<!-- YAML
1309+
added: REPLACEME
1310+
-->
1311+
1312+
* Returns: {string|undefined} The name of the TLS group used for key
1313+
agreement, or `undefined` if no group was negotiated.
1314+
1315+
Returns the TLS Supported Group negotiated for key agreement, for example
1316+
`'X25519MLKEM768'`, `'x25519'`, or `'secp256r1'`. Unlike
1317+
[`tlsSocket.getEphemeralKeyInfo()`][], this method works on both client and
1318+
server sockets. `undefined` is returned when the key agreement does not use a
1319+
group, such as with TLSv1.2 RSA key exchange, or before the handshake has
1320+
completed. With TLSv1.2 DHE key exchange, only the server socket reports the
1321+
group.
1322+
13051323
### `tlsSocket.getPeerCertificate([detailed])`
13061324

13071325
<!-- YAML
@@ -2714,6 +2732,8 @@ added: v0.11.3
27142732
[`tls.getCACertificates()`]: #tlsgetcacertificatestype
27152733
[`tls.getCiphers()`]: #tlsgetciphers
27162734
[`tls.rootCertificates`]: #tlsrootcertificates
2735+
[`tlsSocket.getEphemeralKeyInfo()`]: #tlssocketgetephemeralkeyinfo
2736+
[`tlsSocket.getNegotiatedGroup()`]: #tlssocketgetnegotiatedgroup
27172737
[`tlsSocket.renegotiate()`]: #tlssocketrenegotiateoptions-callback
27182738
[`x509.checkHost()`]: crypto.md#x509checkhostname-options
27192739
[asn1.js]: https://www.npmjs.com/package/asn1.js

‎lib/internal/tls/wrap.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ function makeSocketMethodProxy(name) {
13821382
'getSharedSigalgs',
13831383
'getEphemeralKeyInfo',
13841384
'getFinished',
1385+
'getNegotiatedGroup',
13851386
'getPeerFinished',
13861387
'getProtocol',
13871388
'getTLSTicket',

‎src/crypto/crypto_tls.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,17 @@ void TLSWrap::GetEphemeralKeyInfo(const FunctionCallbackInfo<Value>& args) {
23632363
// ERR_get_error())
23642364
}
23652365

2366+
void TLSWrap::GetNegotiatedGroup(const FunctionCallbackInfo<Value>& args) {
2367+
TLSWrap* w;
2368+
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2369+
CHECK(w->ssl_);
2370+
2371+
if (auto group = w->ssl_.getNegotiatedGroup()) {
2372+
args.GetReturnValue().Set(OneByteString(
2373+
args.GetIsolate(), group.value().data(), group.value().length()));
2374+
}
2375+
}
2376+
23662377
void TLSWrap::GetProtocol(const FunctionCallbackInfo<Value>& args) {
23672378
TLSWrap* w;
23682379
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
@@ -2520,6 +2531,8 @@ void TLSWrap::Initialize(
25202531
SetProtoMethodNoSideEffect(
25212532
isolate, t, "getEphemeralKeyInfo", GetEphemeralKeyInfo);
25222533
SetProtoMethodNoSideEffect(isolate, t, "getFinished", GetFinished);
2534+
SetProtoMethodNoSideEffect(
2535+
isolate, t, "getNegotiatedGroup", GetNegotiatedGroup);
25232536
SetProtoMethodNoSideEffect(
25242537
isolate, t, "getPeerCertificate", GetPeerCertificate);
25252538
SetProtoMethodNoSideEffect(
@@ -2590,6 +2603,7 @@ void TLSWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
25902603
registry->Register(GetPeerX509Certificate);
25912604
registry->Register(HasPeerCertificate);
25922605
registry->Register(GetPeerFinished);
2606+
registry->Register(GetNegotiatedGroup);
25932607
registry->Register(GetProtocol);
25942608
registry->Register(GetSession);
25952609
registry->Register(GetSharedSigalgs);

‎src/crypto/crypto_tls.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ class TLSWrap : public AsyncWrap,
258258
static void GetEphemeralKeyInfo(
259259
const v8::FunctionCallbackInfo<v8::Value>& args);
260260
static void GetFinished(const v8::FunctionCallbackInfo<v8::Value>& args);
261+
static void GetNegotiatedGroup(
262+
const v8::FunctionCallbackInfo<v8::Value>& args);
261263
static void GetPeerCertificate(
262264
const v8::FunctionCallbackInfo<v8::Value>& args);
263265
static void GetPeerX509Certificate(
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const { hasOpenSSL, hasFIPS, isBoringSSL } = require('../common/crypto');
7+
8+
if (isBoringSSL)
9+
common.skip('group names differ on BoringSSL');
10+
11+
const assert = require('assert');
12+
const fixtures = require('../common/fixtures');
13+
const tls = require('tls');
14+
15+
const key = fixtures.readKey('agent2-key.pem');
16+
const cert = fixtures.readKey('agent2-cert.pem');
17+
18+
const tests = [
19+
[{ ecdhCurve: 'P-256' }, {}, 'secp256r1'],
20+
[{ ecdhCurve: 'P-384' }, {}, 'secp384r1'],
21+
[{ ecdhCurve: 'P-256' },
22+
{ maxVersion: 'TLSv1.2', ciphers: 'ECDHE-RSA-AES256-GCM-SHA384' },
23+
'secp256r1'],
24+
];
25+
26+
if (!hasFIPS(3)) {
27+
tests.push([{},
28+
{ maxVersion: 'TLSv1.2', ciphers: 'AES256-SHA256' },
29+
undefined]);
30+
}
31+
32+
if (hasOpenSSL(3, 5) && !hasFIPS()) {
33+
tests.push([{}, {}, 'X25519MLKEM768']);
34+
}
35+
36+
function test([serverOptions, clientOptions, expected]) {
37+
const options = { key, cert, ...serverOptions };
38+
const server = tls.createServer(options, common.mustCall((conn) => {
39+
assert.strictEqual(conn.getNegotiatedGroup(), expected);
40+
conn.end();
41+
}));
42+
43+
server.listen(0, common.mustCall(() => {
44+
const client = tls.connect({
45+
port: server.address().port,
46+
rejectUnauthorized: false,
47+
...clientOptions,
48+
}, common.mustCall(() => {
49+
assert.strictEqual(client.getNegotiatedGroup(), expected);
50+
client.end();
51+
server.close();
52+
}));
53+
assert.strictEqual(client.getNegotiatedGroup(), undefined);
54+
}));
55+
}
56+
57+
tests.forEach(test);

0 commit comments

Comments
 (0)