Files
Netcatty/electron/bridges/privateKeyNormalizer.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

104 lines
3.6 KiB
JavaScript

/**
* Private key normalizer.
*
* ssh2's key parser only understands OpenSSH, legacy PKCS#1/SEC1
* (`BEGIN RSA/DSA/EC PRIVATE KEY`) and PuTTY keys. It rejects PKCS#8
* (`-----BEGIN PRIVATE KEY-----` / `-----BEGIN ENCRYPTED PRIVATE KEY-----`)
* with "Unsupported key format", even though such keys are valid and accepted
* by other clients (e.g. Termius). See issue #1139.
*
* Node's crypto can read PKCS#8 and re-export RSA/EC keys in the legacy PEM
* forms ssh2 accepts, so we transparently convert them before handing the key
* to ssh2. Ed25519 (and other) PKCS#8 keys have no legacy PEM representation
* and surface a clear, actionable error instead of ssh2's opaque one.
*/
const crypto = require("node:crypto");
const { utils: sshUtils } = require("ssh2");
const PKCS8_HEADER_RE = /-----BEGIN (?:ENCRYPTED )?PRIVATE KEY-----/;
// Node asymmetricKeyType -> legacy PEM export type that ssh2 can parse.
const LEGACY_EXPORT_TYPE = {
rsa: "pkcs1",
ec: "sec1",
};
class PrivateKeyPassphraseError extends Error {
constructor(message) {
super(message || "Incorrect passphrase for private key");
this.name = "PrivateKeyPassphraseError";
this.code = "ERR_PRIVATE_KEY_PASSPHRASE";
}
}
class UnsupportedPrivateKeyError extends Error {
constructor(message) {
super(message);
this.name = "UnsupportedPrivateKeyError";
this.code = "ERR_PRIVATE_KEY_UNSUPPORTED";
}
}
/**
* Normalize a private key into a form ssh2 can parse.
*
* @param {string} privateKey - PEM private key contents.
* @param {string} [passphrase] - Passphrase, if the key is encrypted.
* @returns {{ privateKey: string, passphrase: string|undefined, converted: boolean }}
* @throws {PrivateKeyPassphraseError} Encrypted PKCS#8 with a wrong/missing passphrase.
* @throws {UnsupportedPrivateKeyError} PKCS#8 key whose type has no legacy PEM form (e.g. Ed25519).
*/
function normalizePrivateKeyForSsh2(privateKey, passphrase) {
if (typeof privateKey !== "string" || privateKey.length === 0) {
return { privateKey, passphrase, converted: false };
}
// If ssh2 already understands the key, leave it exactly as-is.
const parsed = sshUtils.parseKey(privateKey, passphrase);
if (parsed && !(parsed instanceof Error)) {
return { privateKey, passphrase, converted: false };
}
// We can only rescue PKCS#8 keys, which Node's crypto can read.
if (!PKCS8_HEADER_RE.test(privateKey)) {
return { privateKey, passphrase, converted: false };
}
const encrypted = privateKey.includes("-----BEGIN ENCRYPTED PRIVATE KEY-----");
let keyObject;
try {
keyObject = crypto.createPrivateKey(
passphrase ? { key: privateKey, passphrase } : privateKey,
);
} catch (err) {
if (encrypted) {
throw new PrivateKeyPassphraseError(
"Could not decrypt the PKCS#8 private key with the provided passphrase",
);
}
throw new UnsupportedPrivateKeyError(
`Unable to read the PKCS#8 private key: ${err.message}. ` +
"Convert it with `ssh-keygen -p -m PEM -f <key>` and try again.",
);
}
const exportType = LEGACY_EXPORT_TYPE[keyObject.asymmetricKeyType];
if (!exportType) {
throw new UnsupportedPrivateKeyError(
`Private keys of type "${keyObject.asymmetricKeyType}" in PKCS#8 format are not supported. ` +
"Convert it to OpenSSH format with `ssh-keygen -p -f <key>` and try again.",
);
}
const converted = keyObject.export({ type: exportType, format: "pem" }).toString();
return { privateKey: converted, passphrase: undefined, converted: true };
}
module.exports = {
normalizePrivateKeyForSsh2,
PrivateKeyPassphraseError,
UnsupportedPrivateKeyError,
};