SPB Git

Repository Public

DevPasskey.swift 3.6 KB 54 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Authentication/DevPasskey.swift3// Description: DEVELOPMENT VARIANT ONLY (not compiled in Release): a software WebAuthn authenticator (P-256, CryptoKit) for the local throwaway stack, whose relying party is 'localhost' and cannot use system passkeys. It produces real assertions that the real API and signer verify.4#if SPW_DEV5import CryptoKit6import Foundation7import SPWalletCore89final class DevPasskey: PasskeyAuthenticator, @unchecked Sendable {10    private let key: P256.Signing.PrivateKey11    private let origin: String12    private let lock = NSLock()13    private var counter: UInt32 = 014    /// Set by tests to simulate the owner dismissing the sheet.15    nonisolated(unsafe) static var refuse = false1617    init(origin: String) {18        self.origin = origin19        let file = AppEnvironment.supportDirectory.appendingPathComponent("dev-passkey.key")20        if let d = try? Data(contentsOf: file), let k = try? P256.Signing.PrivateKey(rawRepresentation: d) { key = k }21        else { key = P256.Signing.PrivateKey(); try? key.rawRepresentation.write(to: file, options: [.atomic, .completeFileProtection]) }22    }23    static func reset() { try? FileManager.default.removeItem(at: AppEnvironment.supportDirectory.appendingPathComponent("dev-passkey.key")) }2425    private var xy: (Data, Data) { let raw = key.publicKey.rawRepresentation; return (raw.prefix(32), raw.suffix(32)) }26    private var cose: Data { let (x, y) = xy; return Data([0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20]) + x + Data([0x22, 0x58, 0x20]) + y }27    var credentialId: Data { Data(SHA256.hash(data: xy.0)).prefix(20) }2829    private func clientData(_ type: String, _ challenge: String) -> Data {30        Data(#"{"type":"\#(type)","challenge":"\#(challenge)","origin":"\#(origin)","crossOrigin":false}"#.utf8)31    }3233    func assert(_ options: PasskeyRequestOptions, rpId: String) async throws -> PasskeyAssertion {34        if Self.refuse { throw PasskeyError.cancelled }35        if let allowed = options.allowCredentials, !allowed.isEmpty, !allowed.contains(where: { $0.idData == credentialId }) { throw PasskeyError.noCredential }36        let n: UInt32 = lock.withLock { counter += 1; return counter + UInt32(Date().timeIntervalSince1970) - 1_700_000_000 }37        let cd = clientData("webauthn.get", options.challenge)38        var auth = Data(SHA256.hash(data: Data(rpId.utf8))); auth.append(0x05); withUnsafeBytes(of: n.bigEndian) { auth.append(contentsOf: $0) }39        let sig = try key.signature(for: auth + Data(SHA256.hash(data: cd)))40        return PasskeyAssertion(credentialId: credentialId, clientDataJSON: cd, authenticatorData: auth, signature: sig.derRepresentation, userHandle: nil)41    }4243    func register(_ options: PasskeyCreationOptions, rpId: String) async throws -> PasskeyRegistration {44        let cd = clientData("webauthn.create", options.challenge), id = credentialId45        var auth = Data(SHA256.hash(data: Data(rpId.utf8))); auth.append(0x45); auth.append(Data(count: 4)); auth.append(Data(count: 16))46        auth.append(contentsOf: [UInt8(id.count >> 8), UInt8(id.count & 0xff)]); auth.append(id); auth.append(cose)47        // attestation "none": {fmt: "none", attStmt: {}, authData: bstr}48        var att = Data([0xa3, 0x63, 0x66, 0x6d, 0x74, 0x64, 0x6e, 0x6f, 0x6e, 0x65, 0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, 0xa0, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61])49        att.append(contentsOf: [0x58, UInt8(auth.count)]); att.append(auth)50        return PasskeyRegistration(credentialId: id, clientDataJSON: cd, attestationObject: att, transports: ["internal"])51    }52}53#endif54
Files main
Menu

Theme