Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/Support/SoftwareAuthenticator.swift3// Description: TEST ONLY — in-process WebAuthn authenticator (P-256, "none" attestation, user verification asserted) used to run real ceremonies against the test backend without Touch ID.4import CryptoKit5import Foundation6@testable import VQuantCore78final class SoftwareAuthenticator: PasskeyAuthenticator, @unchecked Sendable {9 let origin: String10 private let lock = NSLock()11 private var keys: [Data: (key: P256.Signing.PrivateKey, userId: Data, counter: UInt32)] = [:]12 private var failure: Error?13 private var count = 01415 init(origin: String) { self.origin = origin }16 var assertions: Int { lock.withLock { count } }17 func failNext(_ e: Error) { lock.withLock { failure = e } }18 func forgetAll() { lock.withLock { keys.removeAll() } }1920 private func clientData(type: String, challenge: Data) -> Data {21 Data(#"{"type":"\#(type)","challenge":"\#(challenge.base64URLEncodedString())","origin":"\#(origin)","crossOrigin":false}"#.utf8)22 }23 private static func be(_ v: UInt32) -> [UInt8] { [UInt8(v >> 24), UInt8((v >> 16) & 0xff), UInt8((v >> 8) & 0xff), UInt8(v & 0xff)] }24 private static func cborBytes(_ d: Data) -> [UInt8] {25 d.count < 24 ? [0x40 | UInt8(d.count)] + d : d.count < 256 ? [0x58, UInt8(d.count)] + d : [0x59, UInt8(d.count >> 8), UInt8(d.count & 0xff)] + d26 }27 private static func cborText(_ s: String) -> [UInt8] { [0x60 | UInt8(s.utf8.count)] + Array(s.utf8) }28 private func takeFailure() -> Error? { lock.withLock { let e = failure; failure = nil; return e } }2930 func register(_ request: RegistrationRequest) async throws -> PasskeyRegistration {31 if let e = takeFailure() { throw e }32 let key = P256.Signing.PrivateKey()33 let credId = Data((0..<32).map { _ in UInt8.random(in: 0...255) })34 let raw = key.publicKey.rawRepresentation35 let x = raw.prefix(32), y = raw.suffix(32)36 // COSE_Key {1: 2 (EC2), 3: -7 (ES256), -1: 1 (P-256), -2: x, -3: y}37 let cose: [UInt8] = [0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21] + Self.cborBytes(Data(x)) + [0x22] + Self.cborBytes(Data(y))38 var authData = Data(SHA256.hash(data: Data(request.rpId.utf8)))39 authData.append(0x45) // UP | UV | AT40 authData.append(contentsOf: Self.be(0))41 authData.append(Data(count: 16))42 authData.append(contentsOf: [UInt8(credId.count >> 8), UInt8(credId.count & 0xff)])43 authData.append(credId); authData.append(contentsOf: cose)44 let att: [UInt8] = [0xa3] + Self.cborText("fmt") + Self.cborText("none") + Self.cborText("attStmt") + [0xa0] + Self.cborText("authData") + Self.cborBytes(authData)45 lock.withLock { keys[credId] = (key, request.userId, 0) }46 return PasskeyRegistration(credentialId: credId, clientDataJSON: clientData(type: "webauthn.create", challenge: request.challenge), attestationObject: Data(att), transports: ["internal"])47 }4849 func assert(_ request: AssertionRequest) async throws -> PasskeyAssertion {50 if let e = takeFailure() { throw e }51 let entry = lock.withLock { () -> (Data, P256.Signing.PrivateKey, Data, UInt32)? in52 let id = request.allowCredentials.first(where: { keys[$0] != nil }) ?? (request.allowCredentials.isEmpty ? keys.keys.first : nil)53 guard let id, var e = keys[id] else { return nil }54 e.counter += 1; keys[id] = e; count += 155 return (id, e.key, e.userId, e.counter)56 }57 guard let (id, key, userId, counter) = entry else { throw AuthError(.cancelled, "No passkey for this site.") }58 var authData = Data(SHA256.hash(data: Data(request.rpId.utf8)))59 authData.append(0x05) // UP | UV60 authData.append(contentsOf: Self.be(counter))61 let cdj = clientData(type: "webauthn.get", challenge: request.challenge)62 let sig = try key.signature(for: authData + Data(SHA256.hash(data: cdj)))63 return PasskeyAssertion(credentialId: id, clientDataJSON: cdj, authenticatorData: authData, signature: sig.derRepresentation, userHandle: userId)64 }65}66