Files
Netcatty/electron/bridges/boringSslDhCompat.test.cjs
T
陈大猫andClaude Opus 4.7 3c100b0ae2 fix #1035: support diffie-hellman-group1-sha1 under BoringSSL (#1039)
Electron's BoringSSL dropped several standard MODP groups from the named
crypto.createDiffieHellmanGroup() API — notably the 1024-bit Oakley Group 2
(modp2) that backs SSH's diffie-hellman-group1-sha1. ssh2 calls
createDiffieHellmanGroup('modp2') for that kex, so connecting to legacy
network devices that only speak group1-sha1 failed with "Error: Unknown DH
group".

The underlying DH math still works on BoringSSL via createDiffieHellman()
with an explicit prime, so add a compatibility shim that wraps
createDiffieHellmanGroup and falls back to the well-known prime constants
when (and only when) the runtime can't resolve a group by name. On OpenSSL
builds the original call succeeds and the fallback is never used.

The shim is installed in main.cjs before any ssh2-using bridge loads, since
ssh2 destructures createDiffieHellmanGroup at module load. Once installed,
the existing legacy-group probe detects modp2 as supported again and offers
group1-sha1, so affected devices actually connect (still gated behind the
per-host legacy-algorithms toggle).

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 12:51:50 +08:00

77 lines
2.4 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const crypto = require("node:crypto");
const {
installBoringSslDhCompat,
MODP_GROUP_PRIMES,
} = require("./boringSslDhCompat.cjs");
test("falls back to an explicit-prime DH when the runtime lacks a named group", () => {
// Simulate BoringSSL: the named lookup throws "Unknown DH group".
const target = {
createDiffieHellmanGroup(name) {
throw new Error(`Unknown DH group: ${name}`);
},
};
assert.equal(installBoringSslDhCompat(target), true);
const dh = target.createDiffieHellmanGroup("modp2");
// The fallback group uses the exact RFC 2409 group1 prime.
assert.equal(dh.getPrime("hex").toUpperCase(), MODP_GROUP_PRIMES.modp2);
// And it performs a real, correct DH exchange.
const peer = crypto.createDiffieHellman(
Buffer.from(MODP_GROUP_PRIMES.modp2, "hex"),
Buffer.from([2]),
);
const ourPublic = dh.generateKeys();
const peerPublic = peer.generateKeys();
assert.ok(dh.computeSecret(peerPublic).equals(peer.computeSecret(ourPublic)));
});
test("uses the runtime's group when the name resolves (no fallback)", () => {
let calls = 0;
const sentinel = Symbol("native-group");
const target = {
createDiffieHellmanGroup() {
calls += 1;
return sentinel;
},
};
installBoringSslDhCompat(target);
assert.equal(target.createDiffieHellmanGroup("modp2"), sentinel);
assert.equal(calls, 1);
});
test("rethrows the original error for groups it cannot back", () => {
const target = {
createDiffieHellmanGroup() {
throw new Error("Unknown DH group");
},
};
installBoringSslDhCompat(target);
assert.throws(() => target.createDiffieHellmanGroup("modp-nonexistent"), /Unknown DH group/);
});
test("install is idempotent", () => {
const target = {
createDiffieHellmanGroup() {
return null;
},
};
assert.equal(installBoringSslDhCompat(target), true);
assert.equal(installBoringSslDhCompat(target), false);
});
test("on this (OpenSSL) runtime the real modp2 still works through the shim", () => {
// Sanity check against the actual crypto module: installing must not break the
// normal path where the runtime resolves the group by name.
const localCrypto = require("node:crypto");
installBoringSslDhCompat(localCrypto);
const dh = localCrypto.createDiffieHellmanGroup("modp2");
assert.equal(dh.getPrime("hex").toUpperCase(), MODP_GROUP_PRIMES.modp2);
});