| 1 | import CryptoKit |
| 2 | import Foundation |
| 3 | |
| 4 | /// The device proof, byte for byte as the server builds it: |
| 5 | /// `personapi.DeviceProofMessage`, Go's `json.Marshal` of |
| 6 | /// `["equipo.person.device.v1", instance, id, challenge, nonce]`. |
| 7 | /// Go escapes <, > and &, and leaves / alone; Swift's encoders do neither, |
| 8 | /// so the array is written here. `device-proof-vectors.json`, owned by a Go |
| 9 | /// test, holds the server's bytes. |
| 10 | public enum DeviceProof { |
| 11 | public static let audience = "equipo.person.device.v1" |
| 12 | |
| 13 | public static func message(instance: String, id: String, challenge: String, nonce: String) -> Data { |
| 14 | let parts = [audience, instance, id, challenge, nonce].map(goJSONString) |
| 15 | return Data(("[" + parts.joined(separator: ",") + "]").utf8) |
| 16 | } |
| 17 | |
| 18 | /// The instance ID is the first element of the challenge, base64url JSON. |
| 19 | public static func instance(fromChallenge challenge: String) -> String? { |
| 20 | guard let data = Base64URL.decode(challenge), |
| 21 | let array = try? JSONSerialization.jsonObject(with: data) as? [Any], |
| 22 | let first = array.first as? String, !first.isEmpty else { return nil } |
| 23 | return first |
| 24 | } |
| 25 | |
| 26 | /// A fresh nonce: 256 random bits, base64url. |
| 27 | public static func nonce() -> String { |
| 28 | var bytes = [UInt8](repeating: 0, count: 32) |
| 29 | let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes) |
| 30 | precondition(status == errSecSuccess, "no system randomness") |
| 31 | return Base64URL.encode(Data(bytes)) |
| 32 | } |
| 33 | |
| 34 | /// The body for `POST /auth/devices/exchange`. |
| 35 | public struct Body: Codable, Sendable, Equatable { |
| 36 | public var id: String |
| 37 | public var deviceSecret: String |
| 38 | public var challenge: String |
| 39 | public var nonce: String |
| 40 | public var signature: String |
| 41 | } |
| 42 | |
| 43 | public static func body(for challenge: DeviceChallenge, key: Curve25519.Signing.PrivateKey, nonce: String) throws -> Body { |
| 44 | guard let instance = instance(fromChallenge: challenge.challenge) else { throw PairingError.badChallenge } |
| 45 | let signed = try key.signature(for: message(instance: instance, id: challenge.id, challenge: challenge.challenge, nonce: nonce)) |
| 46 | return Body(id: challenge.id, deviceSecret: challenge.deviceSecret, challenge: challenge.challenge, |
| 47 | nonce: nonce, signature: Base64URL.encode(signed)) |
| 48 | } |
| 49 | |
| 50 | /// Go's encoding/json string: HTML-safe,
and
escaped, the |
| 51 | /// short escapes for ", \, \n, \r and \t, \u00XX for other control bytes. |
| 52 | static func goJSONString(_ s: String) -> String { |
| 53 | var out = "\"" |
| 54 | for scalar in s.unicodeScalars { |
| 55 | switch scalar { |
| 56 | case "\"": out += "\\\"" |
| 57 | case "\\": out += "\\\\" |
| 58 | case "\n": out += "\\n" |
| 59 | case "\r": out += "\\r" |
| 60 | case "\t": out += "\\t" |
| 61 | // Go 1.22 and later write these two as short escapes. |
| 62 | case "\u{8}": out += "\\b" |
| 63 | case "\u{c}": out += "\\f" |
| 64 | case "<", ">", "&", "\u{2028}", "\u{2029}": |
| 65 | out += String(format: "\\u%04x", scalar.value) |
| 66 | default: |
| 67 | if scalar.value < 0x20 { out += String(format: "\\u%04x", scalar.value) } |
| 68 | else { out.unicodeScalars.append(scalar) } |
| 69 | } |
| 70 | } |
| 71 | return out + "\"" |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public enum Base64URL { |
| 76 | public static func encode(_ data: Data) -> String { |
| 77 | data.base64EncodedString().replacingOccurrences(of: "+", with: "-") |
| 78 | .replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "") |
| 79 | } |
| 80 | public static func decode(_ s: String) -> Data? { |
| 81 | var b = s.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") |
| 82 | while b.count % 4 != 0 { b += "=" } |
| 83 | return Data(base64Encoded: b) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public enum PairingError: Error, Sendable, Equatable { |
| 88 | case badChallenge |
| 89 | case expired |
| 90 | case refused(String) |
| 91 | } |
| 92 | |