|
| 1 | +import { Buffer } from "buffer"; |
| 2 | + |
| 3 | +enum COSEKEYS { |
| 4 | + kty = 1, // Key Type |
| 5 | + alg = 3, // Algorithm |
| 6 | + crv = -1, // Curve for EC keys |
| 7 | + x = -2, // X coordinate for EC keys |
| 8 | + y = -3, // Y coordinate for EC keys |
| 9 | +} |
| 10 | + |
| 11 | +type COSEPublicKeyMap = Map<COSEKEYS, number | Buffer>; |
| 12 | + |
| 13 | +function decodeMap(buffer: Buffer): COSEPublicKeyMap { |
| 14 | + const map = new Map<COSEKEYS, number | Buffer>(); |
| 15 | + let offset = 1; // Start after the map header |
| 16 | + |
| 17 | + const mapHeader = buffer[0]; |
| 18 | + const mapSize = mapHeader & 0x1f; // Number of pairs |
| 19 | + |
| 20 | + for (let i = 0; i < mapSize; i++) { |
| 21 | + const [key, keyLength] = decodeInt(buffer, offset); |
| 22 | + offset += keyLength; |
| 23 | + |
| 24 | + const [value, valueLength] = decodeValue(buffer, offset); |
| 25 | + offset += valueLength; |
| 26 | + |
| 27 | + map.set(key as COSEKEYS, value); |
| 28 | + } |
| 29 | + |
| 30 | + return map; |
| 31 | +} |
| 32 | + |
| 33 | +function decodeInt(buffer: Buffer, offset: number): [number, number] { |
| 34 | + const intByte = buffer[offset]; |
| 35 | + |
| 36 | + if (intByte < 24) { |
| 37 | + // Small positive integer (0–23) |
| 38 | + return [intByte, 1]; |
| 39 | + } else if (intByte === 0x18) { |
| 40 | + // 1-byte unsigned integer |
| 41 | + return [buffer[offset + 1], 2]; |
| 42 | + } else if (intByte === 0x19) { |
| 43 | + // 2-byte unsigned integer |
| 44 | + return [buffer.readUInt16BE(offset + 1), 3]; |
| 45 | + } else if (intByte >= 0x20 && intByte <= 0x37) { |
| 46 | + // Small negative integer (-1 to -24) |
| 47 | + return [-(intByte - 0x20) - 1, 1]; |
| 48 | + } else if (intByte === 0x38) { |
| 49 | + // 1-byte negative integer |
| 50 | + return [-1 - buffer[offset + 1], 2]; |
| 51 | + } else if (intByte === 0x39) { |
| 52 | + // 2-byte negative integer |
| 53 | + return [-1 - buffer.readUInt16BE(offset + 1), 3]; |
| 54 | + } else { |
| 55 | + throw new Error("Unsupported integer format"); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function decodeBytes(buffer: Buffer, offset: number): [Buffer, number] { |
| 60 | + const lengthByte = buffer[offset]; |
| 61 | + if (lengthByte >= 0x40 && lengthByte <= 0x57) { |
| 62 | + const length = lengthByte - 0x40; |
| 63 | + return [buffer.slice(offset + 1, offset + 1 + length), length + 1]; |
| 64 | + } else if (lengthByte === 0x58) { |
| 65 | + // Byte array with 1-byte length prefix |
| 66 | + const length = buffer[offset + 1]; |
| 67 | + return [buffer.slice(offset + 2, offset + 2 + length), length + 2]; |
| 68 | + } else { |
| 69 | + throw new Error("Unsupported byte format"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function decodeValue(buffer: Buffer, offset: number): [number | Buffer, number] { |
| 74 | + const type = buffer[offset]; |
| 75 | + if (type >= 0x40 && type <= 0x5f) { |
| 76 | + // Byte array |
| 77 | + return decodeBytes(buffer, offset); |
| 78 | + } else { |
| 79 | + return decodeInt(buffer, offset); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Decodes a CBOR-encoded COSE public key (from WebAuthn credential) and returns the x,y coordinates. |
| 85 | + * @param publicPasskey - CBOR-encoded COSE public key as Uint8Array |
| 86 | + * @returns Tuple of [x, y] coordinates as Buffers |
| 87 | + */ |
| 88 | +export const getPublicKeyBytesFromPasskeySignature = (publicPasskey: Uint8Array): [Buffer, Buffer] => { |
| 89 | + const cosePublicKey = decodeMap(Buffer.from(publicPasskey)); // Decodes CBOR-encoded COSE key |
| 90 | + const x = cosePublicKey.get(COSEKEYS.x) as Buffer; |
| 91 | + const y = cosePublicKey.get(COSEKEYS.y) as Buffer; |
| 92 | + |
| 93 | + return [Buffer.from(x), Buffer.from(y)]; |
| 94 | +}; |
0 commit comments