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