Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/helpers/softauthn.ts9 * Module : tests10 * Repository : https://git.spboucher.ai/note-spboucher-ai11 * Project Website : https://note.spboucher.ai12 * Created : 2026-11-1713 * Last Modified : 2026-11-1714 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * TEST-ONLY software authenticator: builds WebAuthn registration/assertion responses with a P-256 key so the server-side verification45 * can be attacked deterministically (wrong origin, wrong RP ID, missing user verification, forged signature, replay, expiry).46 * The application itself never builds or parses WebAuthn structures by hand — it uses @simplewebauthn/server.47 */48import { createHash, createSign, generateKeyPairSync, randomBytes, type KeyObject } from "node:crypto";49import { isoCBOR } from "@simplewebauthn/server/helpers";5051const b64u = (b: Uint8Array | Buffer) => Buffer.from(b).toString("base64url");52const sha256 = (b: Uint8Array | Buffer | string) => createHash("sha256").update(b).digest();53export interface CeremonyOverrides { origin?: string; rpId?: string; uv?: boolean; up?: boolean; type?: string; challenge?: string; tamperSignature?: boolean; counter?: number }5455export class SoftAuthenticator {56 readonly credentialId = randomBytes(32); private key: KeyObject; private pub: KeyObject; counter = 0;57 constructor() { const kp = generateKeyPairSync("ec", { namedCurve: "P-256" }); this.key = kp.privateKey; this.pub = kp.publicKey; }58 private cose() { const jwk = this.pub.export({ format: "jwk" }) as { x: string; y: string }; return isoCBOR.encode(new Map<number, number | Uint8Array>([[1, 2], [3, -7], [-1, 1], [-2, new Uint8Array(Buffer.from(jwk.x, "base64url"))], [-3, new Uint8Array(Buffer.from(jwk.y, "base64url"))]])); }59 private flags(o: CeremonyOverrides, attested: boolean) { return (o.up === false ? 0 : 0x01) | (o.uv === false ? 0 : 0x04) | (attested ? 0x40 : 0); }60 private clientData(type: string, challenge: string, origin: string) { return Buffer.from(JSON.stringify({ type, challenge, origin, crossOrigin: false })); }6162 register(options: { challenge: string; rp: { id?: string } }, origin: string, o: CeremonyOverrides = {}) {63 const rpId = o.rpId ?? options.rp.id!; const cnt = Buffer.alloc(4); cnt.writeUInt32BE(0);64 const len = Buffer.alloc(2); len.writeUInt16BE(this.credentialId.length);65 const authData = Buffer.concat([sha256(rpId), Buffer.from([this.flags(o, true)]), cnt, Buffer.alloc(16), len, this.credentialId, Buffer.from(this.cose())]);66 const attestationObject = isoCBOR.encode(new Map<string, unknown>([["fmt", "none"], ["attStmt", new Map()], ["authData", new Uint8Array(authData)]]) as any);67 const clientDataJSON = this.clientData(o.type ?? "webauthn.create", o.challenge ?? options.challenge, o.origin ?? origin);68 return { id: b64u(this.credentialId), rawId: b64u(this.credentialId), type: "public-key" as const, response: { clientDataJSON: b64u(clientDataJSON), attestationObject: b64u(attestationObject), transports: ["internal"] }, clientExtensionResults: {}, authenticatorAttachment: "platform" };69 }70 assert(options: { challenge: string; rpId?: string }, origin: string, o: CeremonyOverrides = {}) {71 const rpId = o.rpId ?? options.rpId!; this.counter = o.counter ?? this.counter + 1; const cnt = Buffer.alloc(4); cnt.writeUInt32BE(this.counter);72 const authData = Buffer.concat([sha256(rpId), Buffer.from([this.flags(o, false)]), cnt]);73 const clientDataJSON = this.clientData(o.type ?? "webauthn.get", o.challenge ?? options.challenge, o.origin ?? origin);74 const signature = createSign("SHA256").update(Buffer.concat([authData, sha256(clientDataJSON)])).sign(this.key);75 if (o.tamperSignature) signature[signature.length - 1] ^= 0xff;76 return { id: b64u(this.credentialId), rawId: b64u(this.credentialId), type: "public-key" as const, response: { clientDataJSON: b64u(clientDataJSON), authenticatorData: b64u(authData), signature: b64u(signature) }, clientExtensionResults: {}, authenticatorAttachment: "platform" };77 }78}79