Files
Netcatty/electron/bridges/privateKeyNormalizer.test.cjs
T
陈大猫andClaude Opus 4.8 053a976d37 fix(ssh): support PKCS#8 private keys by converting for ssh2 (#1146)
ssh2's key parser only accepts OpenSSH, legacy PKCS#1/SEC1 and PuTTY
keys, so PKCS#8 keys (-----BEGIN PRIVATE KEY----- / BEGIN ENCRYPTED
PRIVATE KEY) fail with "Cannot parse privateKey: Unsupported key
format" even though they are valid and work in other clients such as
Termius. (#1139)

Add privateKeyNormalizer: when ssh2 can't parse a key but it is PKCS#8,
read it with Node's crypto and re-export RSA->PKCS#1 / EC->SEC1, the
legacy PEM forms ssh2 understands. Encrypted PKCS#8 is decrypted with
the passphrase first. Ed25519 (and other) PKCS#8 keys, which have no
legacy PEM form, now surface a clear "convert with ssh-keygen" message
instead of ssh2's opaque error.

Wired into sshAuthHelper's preparePrivateKeyForAuth and
loadIdentityFileForAuth, so SSH, SFTP and port-forwarding all benefit.
Also fixes a latent bug where a correct passphrase on an encrypted
PKCS#8 key was rejected and re-prompted indefinitely.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-29 15:09:52 +08:00

93 lines
3.2 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const crypto = require("node:crypto");
const { utils: sshUtils } = require("ssh2");
const {
normalizePrivateKeyForSsh2,
PrivateKeyPassphraseError,
UnsupportedPrivateKeyError,
} = require("./privateKeyNormalizer.cjs");
function genRsa(encoding) {
return crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
privateKeyEncoding: encoding,
publicKeyEncoding: { type: "spki", format: "pem" },
}).privateKey;
}
const rsaPkcs8 = () => genRsa({ type: "pkcs8", format: "pem" });
const rsaPkcs1 = () => genRsa({ type: "pkcs1", format: "pem" });
const encryptedRsaPkcs8 = (passphrase) =>
genRsa({ type: "pkcs8", format: "pem", cipher: "aes-256-cbc", passphrase });
const ecPkcs8 = () =>
crypto.generateKeyPairSync("ec", {
namedCurve: "prime256v1",
privateKeyEncoding: { type: "pkcs8", format: "pem" },
publicKeyEncoding: { type: "spki", format: "pem" },
}).privateKey;
const ed25519Pkcs8 = () =>
crypto.generateKeyPairSync("ed25519", {
privateKeyEncoding: { type: "pkcs8", format: "pem" },
publicKeyEncoding: { type: "spki", format: "pem" },
}).privateKey;
function parseOk(key) {
const r = sshUtils.parseKey(key);
return r && !(r instanceof Error) ? r : null;
}
test("converts unencrypted RSA PKCS#8 into an ssh2-parseable key", () => {
const result = normalizePrivateKeyForSsh2(rsaPkcs8());
assert.equal(result.converted, true);
const parsed = parseOk(result.privateKey);
assert.ok(parsed, "converted key should be parseable by ssh2");
assert.equal(parsed.type, "ssh-rsa");
});
test("converts unencrypted EC PKCS#8 into an ssh2-parseable key", () => {
const result = normalizePrivateKeyForSsh2(ecPkcs8());
assert.equal(result.converted, true);
const parsed = parseOk(result.privateKey);
assert.ok(parsed);
assert.equal(parsed.type, "ecdsa-sha2-nistp256");
});
test("leaves an already-supported PKCS#1 key untouched", () => {
const key = rsaPkcs1();
const result = normalizePrivateKeyForSsh2(key);
assert.equal(result.converted, false);
assert.equal(result.privateKey, key);
});
test("decrypts and converts an encrypted RSA PKCS#8 key with the correct passphrase", () => {
const result = normalizePrivateKeyForSsh2(encryptedRsaPkcs8("secret"), "secret");
assert.equal(result.converted, true);
assert.equal(result.passphrase, undefined);
const parsed = parseOk(result.privateKey);
assert.ok(parsed);
assert.equal(parsed.type, "ssh-rsa");
});
test("throws PrivateKeyPassphraseError for encrypted PKCS#8 with a wrong passphrase", () => {
assert.throws(
() => normalizePrivateKeyForSsh2(encryptedRsaPkcs8("secret"), "wrong"),
(err) => err instanceof PrivateKeyPassphraseError,
);
});
test("throws UnsupportedPrivateKeyError for Ed25519 PKCS#8 with a conversion hint", () => {
assert.throws(
() => normalizePrivateKeyForSsh2(ed25519Pkcs8()),
(err) => err instanceof UnsupportedPrivateKeyError && /ssh-keygen/.test(err.message),
);
});
test("passes through content that is not a PKCS#8 key", () => {
const junk = "not a private key";
const result = normalizePrivateKeyForSsh2(junk);
assert.equal(result.converted, false);
assert.equal(result.privateKey, junk);
});