Download server/softkey.js from abr00kx/taper: direct link, hf CLI and curl.
- Browser
- Download file 5.15 kB
-
https://huggingface.co/abr00kx/taper/resolve/main/server/softkey.js
- Command line
-
hf download hf://abr00kx/taper/server/softkey.js
-
curl -L -o softkey.js https://huggingface.co/abr00kx/taper/resolve/main/server/softkey.js
5.15 kB
| // A software FIDO2 authenticator for tests: builds what a YubiKey would | |
| // send to navigator.credentials.create() and get(). Not for real use. | |
| import { createHash, generateKeyPairSync, randomBytes, sign as cryptoSign } from 'node:crypto'; | |
| const sha256 = (b) => createHash('sha256').update(b).digest(); | |
| const b64u = (b) => Buffer.from(b).toString('base64url'); | |
| // CBOR encoder for what WebAuthn needs. | |
| export function cbor(v) { | |
| const head = (major, n) => { | |
| if (n < 24) return Buffer.from([(major << 5) | n]); | |
| if (n < 256) return Buffer.from([(major << 5) | 24, n]); | |
| if (n < 65536) { const b = Buffer.alloc(3); b[0] = (major << 5) | 25; b.writeUInt16BE(n, 1); return b; } | |
| const b = Buffer.alloc(5); b[0] = (major << 5) | 26; b.writeUInt32BE(n, 1); return b; | |
| }; | |
| if (typeof v === 'number') return v >= 0 ? head(0, v) : head(1, -1 - v); | |
| if (typeof v === 'string') { const t = Buffer.from(v); return Buffer.concat([head(3, t.length), t]); } | |
| if (Buffer.isBuffer(v)) return Buffer.concat([head(2, v.length), v]); | |
| if (Array.isArray(v)) return Buffer.concat([head(4, v.length), ...v.map(cbor)]); | |
| if (v instanceof Map) return Buffer.concat([head(5, v.size), ...[...v].flatMap(([k, x]) => [cbor(k), cbor(x)])]); | |
| throw new Error('cbor: unsupported value'); | |
| } | |
| export class SoftKey { | |
| /** | |
| * @param attestation 'none' | 'self' (packed, no chain) | 'x5c' (packed with `chain`) | 'u2f' (fido-u2f with `chain`) | |
| * @param chain {leafPrivateKey (KeyObject), certs: [DER Buffers]} | |
| * @param counter starting signature counter (0 = a key that never counts) | |
| * @param backupEligible set the BE flag, as a synced passkey would | |
| */ | |
| constructor({ attestation = 'none', chain = null, counter = 0, backupEligible = false } = {}) { | |
| const { publicKey, privateKey } = generateKeyPairSync('ec', { namedCurve: 'P-256' }); | |
| this.privateKey = privateKey; | |
| const jwk = publicKey.export({ format: 'jwk' }); | |
| this.x = Buffer.from(jwk.x, 'base64url'); | |
| this.y = Buffer.from(jwk.y, 'base64url'); | |
| this.credentialId = randomBytes(32); | |
| this.attestation = attestation; | |
| this.chain = chain; | |
| this.counter = counter; | |
| this.backupEligible = backupEligible; | |
| this.cose = cbor(new Map([[1, 2], [3, -7], [-1, 1], [-2, this.x], [-3, this.y]])); | |
| } | |
| get id() { return b64u(this.credentialId); } | |
| clientData(type, challenge, origin) { | |
| return Buffer.from(JSON.stringify({ type, challenge, origin, crossOrigin: false })); | |
| } | |
| flags({ attested }) { return 0x01 | (attested ? 0x40 : 0) | (this.backupEligible ? 0x08 : 0); } | |
| create(options, origin, { rpId = options.rp.id, challenge = options.challenge } = {}) { | |
| const clientDataJSON = this.clientData('webauthn.create', challenge, origin); | |
| const counter = Buffer.alloc(4); | |
| const authData = Buffer.concat([ | |
| sha256(rpId), Buffer.from([this.flags({ attested: true })]), counter, | |
| Buffer.alloc(16, 7), // AAGUID: a recognisable value that must never be stored | |
| Buffer.from([this.credentialId.length >> 8, this.credentialId.length & 255]), this.credentialId, this.cose, | |
| ]); | |
| const hash = sha256(clientDataJSON); | |
| let fmt = 'none'; | |
| let attStmt = new Map(); | |
| if (this.attestation === 'self') { | |
| fmt = 'packed'; | |
| attStmt = new Map([['alg', -7], ['sig', cryptoSign('sha256', Buffer.concat([authData, hash]), { key: this.privateKey, dsaEncoding: 'der' })]]); | |
| } else if (this.attestation === 'x5c') { | |
| fmt = 'packed'; | |
| attStmt = new Map([ | |
| ['alg', -7], | |
| ['sig', cryptoSign('sha256', Buffer.concat([authData, hash]), { key: this.chain.leafPrivateKey, dsaEncoding: 'der' })], | |
| ['x5c', this.chain.certs], | |
| ]); | |
| } else if (this.attestation === 'u2f') { | |
| fmt = 'fido-u2f'; | |
| const data = Buffer.concat([Buffer.from([0]), sha256(rpId), hash, this.credentialId, Buffer.from([4]), this.x, this.y]); | |
| attStmt = new Map([['sig', cryptoSign('sha256', data, { key: this.chain.leafPrivateKey, dsaEncoding: 'der' })], ['x5c', this.chain.certs.slice(0, 1)]]); | |
| } | |
| return { | |
| id: this.id, rawId: this.id, type: 'public-key', | |
| response: { | |
| clientDataJSON: b64u(clientDataJSON), | |
| attestationObject: b64u(cbor(new Map([['fmt', fmt], ['attStmt', attStmt], ['authData', authData]]))), | |
| }, | |
| }; | |
| } | |
| get(options, origin, { rpId = options.rpId, challenge = options.challenge, counter } = {}) { | |
| if (counter !== undefined) this.counter = counter; | |
| else if (this.counter > 0) this.counter++; | |
| const clientDataJSON = this.clientData('webauthn.get', challenge, origin); | |
| const count = Buffer.alloc(4); | |
| count.writeUInt32BE(this.counter); | |
| const authenticatorData = Buffer.concat([sha256(rpId), Buffer.from([this.flags({ attested: false })]), count]); | |
| const signature = cryptoSign('sha256', Buffer.concat([authenticatorData, sha256(clientDataJSON)]), { key: this.privateKey, dsaEncoding: 'der' }); | |
| return { | |
| id: this.id, rawId: this.id, type: 'public-key', | |
| response: { clientDataJSON: b64u(clientDataJSON), authenticatorData: b64u(authenticatorData), signature: b64u(signature) }, | |
| }; | |
| } | |
| } | |