|
| 1 | +/* eslint-disable id-length */ |
| 2 | + |
| 3 | +import * as crypto from 'crypto' |
| 4 | +import { type SSHKey, type SSHSignature } from './ssh_agent_client.ts' |
| 5 | +import { readString } from './utils.ts' |
| 6 | + |
| 7 | +const sshEcCurveParam = (curve: string): { crv: string; coordLen: number } => { |
| 8 | + if (curve === 'nistp256') return { crv: 'P-256', coordLen: 32 } |
| 9 | + if (curve === 'nistp384') return { crv: 'P-384', coordLen: 48 } |
| 10 | + if (curve === 'nistp521') return { crv: 'P-521', coordLen: 66 } |
| 11 | + throw new Error(`Unsupported EC curve: ${curve}`) |
| 12 | +} |
| 13 | + |
| 14 | +const ecdsaHashAlgo = (sigType: string): string => { |
| 15 | + if (sigType === 'ecdsa-sha2-nistp256') return 'SHA256' |
| 16 | + if (sigType === 'ecdsa-sha2-nistp384') return 'SHA384' |
| 17 | + if (sigType === 'ecdsa-sha2-nistp521') return 'SHA512' |
| 18 | + throw new Error(`Unsupported ECDSA signature type: ${sigType}`) |
| 19 | +} |
| 20 | + |
| 21 | +/** Convert an SSH public key blob to a Node.js `crypto.KeyObject`. */ |
| 22 | +const parseSSHPublicKey = (key: SSHKey): crypto.KeyObject => { |
| 23 | + const blob = key.raw |
| 24 | + const type = readString(blob, 0) |
| 25 | + const keyType = type.toString('ascii') |
| 26 | + |
| 27 | + if (keyType === 'ssh-rsa') { |
| 28 | + const rsaOffset = 4 + type.length |
| 29 | + const exponent = readString(blob, rsaOffset) |
| 30 | + const modulus = readString(blob, rsaOffset + 4 + exponent.length) |
| 31 | + return crypto.createPublicKey({ |
| 32 | + key: { kty: 'RSA', n: modulus.toString('base64url'), e: exponent.toString('base64url') }, |
| 33 | + format: 'jwk', |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + if (keyType === 'ssh-ed25519') { |
| 38 | + const pubKeyBytes = readString(blob, 4 + type.length) |
| 39 | + // SPKI DER encoding for Ed25519 (OID 1.3.101.112) |
| 40 | + const spkiPrefix = Buffer.from('302a300506032b6570032100', 'hex') |
| 41 | + return crypto.createPublicKey({ |
| 42 | + key: Buffer.concat([spkiPrefix, pubKeyBytes]), |
| 43 | + format: 'der', |
| 44 | + type: 'spki', |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + if (keyType.startsWith('ecdsa-sha2-')) { |
| 49 | + const ecOffset = 4 + type.length |
| 50 | + const curveName = readString(blob, ecOffset) |
| 51 | + const point = readString(blob, ecOffset + 4 + curveName.length) |
| 52 | + const { crv, coordLen } = sshEcCurveParam(curveName.toString('ascii')) |
| 53 | + // Uncompressed EC point: 0x04 || x || y |
| 54 | + const pointX = point.subarray(1, 1 + coordLen) |
| 55 | + const pointY = point.subarray(1 + coordLen) |
| 56 | + return crypto.createPublicKey({ |
| 57 | + key: { kty: 'EC', crv, x: pointX.toString('base64url'), y: pointY.toString('base64url') }, |
| 58 | + format: 'jwk', |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + throw new Error(`Unsupported key type: ${keyType}`) |
| 63 | +} |
| 64 | + |
| 65 | +const encodeDerLength = (len: number): Buffer => { |
| 66 | + if (len < 128) return Buffer.from([len]) |
| 67 | + if (len < 256) return Buffer.from([0x81, len]) |
| 68 | + return Buffer.from([0x82, Math.floor(len / 256), len % 256]) |
| 69 | +} |
| 70 | + |
| 71 | +const encodeDerInt = (bytes: Buffer): Buffer => { |
| 72 | + // Strip leading zeros, keeping at least one byte |
| 73 | + let start = 0 |
| 74 | + while (start < bytes.length - 1 && bytes[start] === 0) start += 1 |
| 75 | + const trimmed = bytes.subarray(start) |
| 76 | + // Prepend 0x00 if high bit is set to keep the DER INTEGER positive |
| 77 | + const [firstByte] = trimmed |
| 78 | + const content = firstByte >= 0x80 ? Buffer.concat([Buffer.from([0x00]), trimmed]) : trimmed |
| 79 | + return Buffer.concat([Buffer.from([0x02]), encodeDerLength(content.length), content]) |
| 80 | +} |
| 81 | + |
| 82 | +/** Map an SSH signature to the hash algorithm and signature bytes expected by `crypto.verify`. */ |
| 83 | +const parseSSHSignature = (sig: SSHSignature): { hashAlgo: string | null; sigBytes: Buffer } => { |
| 84 | + const { type, raw } = sig |
| 85 | + |
| 86 | + if (type === 'rsa-sha2-256') return { hashAlgo: 'SHA256', sigBytes: raw } |
| 87 | + if (type === 'rsa-sha2-512') return { hashAlgo: 'SHA512', sigBytes: raw } |
| 88 | + if (type === 'ssh-rsa') return { hashAlgo: 'SHA1', sigBytes: raw } |
| 89 | + if (type === 'ssh-ed25519') return { hashAlgo: null, sigBytes: raw } |
| 90 | + |
| 91 | + if (type.startsWith('ecdsa-sha2-')) { |
| 92 | + // SSH ECDSA signature: mpint(r) || mpint(s) → DER ASN.1 SEQUENCE { INTEGER r, INTEGER s } |
| 93 | + const sigR = readString(raw, 0) |
| 94 | + const sigS = readString(raw, 4 + sigR.length) |
| 95 | + const rDer = encodeDerInt(sigR) |
| 96 | + const sDer = encodeDerInt(sigS) |
| 97 | + const seqContent = Buffer.concat([rDer, sDer]) |
| 98 | + const sigBytes = Buffer.concat([Buffer.from([0x30]), encodeDerLength(seqContent.length), seqContent]) |
| 99 | + return { hashAlgo: ecdsaHashAlgo(type), sigBytes } |
| 100 | + } |
| 101 | + |
| 102 | + throw new Error(`Unsupported signature type: ${type}`) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Verify an SSH signature against a message and public key. |
| 107 | + * |
| 108 | + * No SSH agent communication is required — this is a local crypto operation. |
| 109 | + * |
| 110 | + * @param signature - The signature to verify (from {@link sign}). |
| 111 | + * @param key - The SSH public key (from {@link getIdentities} or {@link getIdentity}). |
| 112 | + * @param data - The original message that was signed. |
| 113 | + * @returns `true` if the signature is valid, `false` otherwise. |
| 114 | + * @throws {Error} if the key type or signature type is unsupported. |
| 115 | + */ |
| 116 | +const verifySSHSignature = (signature: SSHSignature, key: SSHKey, data: Buffer): boolean => { |
| 117 | + const publicKey = parseSSHPublicKey(key) |
| 118 | + const { hashAlgo, sigBytes } = parseSSHSignature(signature) |
| 119 | + return crypto.verify(hashAlgo, data, publicKey, sigBytes) |
| 120 | +} |
| 121 | + |
| 122 | +export { parseSSHPublicKey, parseSSHSignature, verifySSHSignature } |
0 commit comments