fix(security): generate OpenSSH-format Ed25519 keys compatible with ssh2
This commit is contained in:
parent
3b9e084916
commit
05abda3417
1 changed files with 21 additions and 47 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import crypto from 'node:crypto';
|
import crypto from 'node:crypto';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import ssh2 from 'ssh2';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import {
|
import {
|
||||||
VaultCredentialSchema,
|
VaultCredentialSchema,
|
||||||
|
|
@ -76,34 +77,18 @@ function decryptEnvelope(envelope: VaultEnvelope, key: Buffer): VaultPayload {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildSshEd25519PublicAndFingerprint(spkiDer: Buffer, comment?: string): {
|
export function computeOpenSshFingerprint(publicKeyLine: string): string {
|
||||||
publicKey: string;
|
const parsed = ssh2.utils.parseKey(publicKeyLine.trim());
|
||||||
fingerprint: string;
|
if (parsed instanceof Error) {
|
||||||
} {
|
throw parsed;
|
||||||
// Ed25519 SPKI DER is 44 bytes: 12-byte ASN.1 header + 32-byte raw public key
|
|
||||||
if (spkiDer.length !== 44) {
|
|
||||||
throw new Error(`Unexpected Ed25519 SPKI DER length: ${spkiDer.length}`);
|
|
||||||
}
|
}
|
||||||
const rawPub = spkiDer.subarray(12);
|
const key = Array.isArray(parsed) ? parsed[0]! : parsed;
|
||||||
const algBuf = Buffer.from('ssh-ed25519', 'ascii');
|
const hash = crypto
|
||||||
const wire = Buffer.allocUnsafe(4 + algBuf.length + 4 + rawPub.length);
|
|
||||||
wire.writeUInt32BE(algBuf.length, 0);
|
|
||||||
algBuf.copy(wire, 4);
|
|
||||||
wire.writeUInt32BE(rawPub.length, 4 + algBuf.length);
|
|
||||||
rawPub.copy(wire, 8 + algBuf.length);
|
|
||||||
|
|
||||||
const publicB64 = wire.toString('base64');
|
|
||||||
const fingerprintHash = crypto
|
|
||||||
.createHash('sha256')
|
.createHash('sha256')
|
||||||
.update(wire)
|
.update(key.getPublicSSH())
|
||||||
.digest('base64')
|
.digest('base64')
|
||||||
.replace(/=+$/, '');
|
.replace(/=+$/, '');
|
||||||
|
return `SHA256:${hash}`;
|
||||||
const publicKey = comment ? `ssh-ed25519 ${publicB64} ${comment}` : `ssh-ed25519 ${publicB64}`;
|
|
||||||
return {
|
|
||||||
publicKey,
|
|
||||||
fingerprint: `SHA256:${fingerprintHash}`,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
||||||
|
|
@ -333,36 +318,25 @@ export class VaultService {
|
||||||
}): VaultCredentialMeta {
|
}): VaultCredentialMeta {
|
||||||
this.assertUnlocked();
|
this.assertUnlocked();
|
||||||
|
|
||||||
const privateKeyEncoding = options.passphrase
|
const keyPair = options.passphrase
|
||||||
? ({
|
? ssh2.utils.generateKeyPairSync('ed25519', {
|
||||||
type: 'pkcs8',
|
comment: options.comment ?? options.id,
|
||||||
format: 'pem',
|
|
||||||
cipher: 'aes-256-cbc',
|
|
||||||
passphrase: options.passphrase,
|
passphrase: options.passphrase,
|
||||||
} as const)
|
cipher: 'aes256-ctr',
|
||||||
: ({
|
rounds: 16,
|
||||||
type: 'pkcs8',
|
})
|
||||||
format: 'pem',
|
: ssh2.utils.generateKeyPairSync('ed25519', {
|
||||||
} as const);
|
comment: options.comment ?? options.id,
|
||||||
|
});
|
||||||
|
|
||||||
const { publicKey: spkiDer, privateKey: privatePem } = crypto.generateKeyPairSync(
|
const publicKey = keyPair.public.trim();
|
||||||
'ed25519',
|
const fingerprint = computeOpenSshFingerprint(publicKey);
|
||||||
{
|
|
||||||
publicKeyEncoding: { type: 'spki', format: 'der' },
|
|
||||||
privateKeyEncoding,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const { publicKey, fingerprint } = buildSshEd25519PublicAndFingerprint(
|
|
||||||
spkiDer,
|
|
||||||
options.comment ?? options.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
return this.upsertCredential({
|
return this.upsertCredential({
|
||||||
id: options.id,
|
id: options.id,
|
||||||
name: options.name,
|
name: options.name,
|
||||||
type: 'ssh-key',
|
type: 'ssh-key',
|
||||||
privateKey: privatePem,
|
privateKey: keyPair.private,
|
||||||
publicKey,
|
publicKey,
|
||||||
passphrase: options.passphrase || undefined,
|
passphrase: options.passphrase || undefined,
|
||||||
fingerprint,
|
fingerprint,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue