Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 3x 3x 33x 33x 33x 32x 16x 16x 16x 18x 18x 17x 17x 17x 17x 3x 14x 23x 11x 11x 202x 11x 102x 22x 22x 22x 22x 22x 22x 22x 11x 104x 104x 104x 104x 104x 104x 1x 1x 30x 30x 30x 30x 30x 46x 46x 46x 46x 46x 8x 19x 16x 16x 37x 16x 2x 2x 1x 11x 11x 9x 3x 3x 5x 5x 5x 5x 5x 5x 3x 3x 3x 16x 3x 15x 20x 15x 9x 9x 9x 9x 9x 8x 1x 7x 7x 7x 7x 7x 7x 7x 7x 9x 1x 16x 1x 5x 5x 2x 3x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x | import {
createHash,
createHmac,
randomBytes,
timingSafeEqual,
X509Certificate,
} from "crypto";
import { APPLE_APP_ATTEST_ROOT_CA } from "@/lib/apple-app-attest-root";
/**
* Apple App Attest, checked on the hub (ADR-054).
*
* The Klarbefund app creates an account on the hub without a session, so the
* request has to prove where it comes from. iOS generates a key in the Secure
* Enclave and asks Apple to certify it, binding the certificate to this team
* and bundle id and to a value the hub chose. A script cannot produce that; a
* genuine build of the app on a real device can.
*
* The steps are Apple's, "Validating apps that connect to your server":
* https://developer.apple.com/documentation/devicecheck/validating-apps-that-connect-to-your-server
*/
/** Team id and bundle id: the relying party an attestation names. */
const APP_ID = "38R8Z4P7S8.red.mabu.meinbefund";
/** How long a challenge may be answered. */
const CHALLENGE_MS = 5 * 60_000;
function challengeKey(): string {
const key = process.env.APP_ATTEST_SECRET ?? process.env.NEXTAUTH_SECRET;
Iif (!key) throw new Error("APP_ATTEST_SECRET or NEXTAUTH_SECRET must be set");
return key;
}
const b64url = (bytes: Buffer) => bytes.toString("base64url");
/**
* A challenge any replica can check: expiry, random bytes, and an HMAC over
* both. Nothing is stored; the in-memory pairing of #511 is why.
*/
export function issueChallenge(now: number = Date.now()): string {
const body = `${now + CHALLENGE_MS}.${b64url(randomBytes(24))}`;
const mac = createHmac("sha256", challengeKey()).update(body).digest();
return `${body}.${b64url(mac)}`;
}
/** True when the hub issued this challenge and it has not expired. */
export function checkChallenge(
challenge: string,
now: number = Date.now(),
): boolean {
const parts = challenge.split(".");
if (parts.length !== 3) return false;
const [expiry, nonce, mac] = parts;
const expected = createHmac("sha256", challengeKey())
.update(`${expiry}.${nonce}`)
.digest();
const given = Buffer.from(mac, "base64url");
if (given.length !== expected.length || !timingSafeEqual(given, expected)) {
return false;
}
return Number(expiry) > now;
}
/** What the phone hashes as the attestation's client data. */
export function clientData(challenge: string, deviceId: string): string {
return `${challenge}|${deviceId}`;
}
// ---- CBOR, as much as an attestation object uses ----
type Cbor =
| number
| bigint
| string
| Buffer
| boolean
| null
| Cbor[]
| Map<Cbor, Cbor>;
export class AttestError extends Error {}
/** Decodes one CBOR item (RFC 8949): integers, strings, arrays, maps, simple values. */
export function decodeCbor(data: Buffer): Cbor {
let at = 0;
const need = (n: number) => {
if (at + n > data.length) throw new AttestError("truncated CBOR");
};
const length = (info: number): number => {
if (info < 24) return info;
const size = { 24: 1, 25: 2, 26: 4, 27: 8 }[info];
Iif (!size) throw new AttestError("unsupported CBOR length");
need(size);
Iif (size === 8) {
const big = data.readBigUInt64BE(at);
at += 8;
if (big > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new AttestError("CBOR length too large");
}
return Number(big);
}
const value = data.readUIntBE(at, size);
at += size;
return value;
};
const item = (depth: number): Cbor => {
Iif (depth > 16) throw new AttestError("CBOR nested too deeply");
need(1);
const head = data[at++];
const major = head >> 5;
const info = head & 0x1f;
switch (major) {
case 0:
return length(info);
case 1:
return -1 - length(info);
case 2: {
const n = length(info);
need(n);
const bytes = data.subarray(at, at + n);
at += n;
return Buffer.from(bytes);
}
case 3: {
const n = length(info);
need(n);
const text = data.toString("utf8", at, at + n);
at += n;
return text;
}
case 4: {
const n = length(info);
return Array.from({ length: n }, () => item(depth + 1));
}
case 5: {
const n = length(info);
const map = new Map<Cbor, Cbor>();
for (let i = 0; i < n; i++) map.set(item(depth + 1), item(depth + 1));
return map;
}
case 7:
Iif (info === 20) return false;
if (info === 21) return true;
Eif (info === 22) return null;
throw new AttestError("unsupported CBOR simple value");
default:
throw new AttestError("unsupported CBOR type");
}
};
const value = item(0);
Iif (at !== data.length) throw new AttestError("trailing bytes after CBOR");
return value;
}
// ---- The attestation ----
/** The AAGUIDs Apple sets: a development build's key, and a distributed one's. */
const ENVIRONMENTS: Record<string, "development" | "production"> = {
[Buffer.from("appattestdevelop").toString("hex")]: "development",
[Buffer.concat([Buffer.from("appattest"), Buffer.alloc(7)]).toString("hex")]:
"production",
};
/** OID 1.2.840.113635.100.8.2, Apple's nonce extension, DER-encoded. */
const NONCE_OID = Buffer.from("06092a864886f763640802", "hex");
/**
* The nonce in the leaf certificate's Apple extension.
*
* The extension value is `SEQUENCE { [1] { OCTET STRING (32 bytes) } }`,
* which in DER is always `30 24 a1 22 04 20` and the 32 bytes. Node exposes
* no arbitrary extensions, so the encoding is found after the OID.
*/
function certificateNonce(leaf: X509Certificate): Buffer {
const der = leaf.raw;
const oid = der.indexOf(NONCE_OID);
Iif (oid < 0) throw new AttestError("the leaf certificate has no nonce");
const marker = der.indexOf(
Buffer.from("3024a1220420", "hex"),
oid + NONCE_OID.length,
);
Iif (marker < 0 || marker > oid + NONCE_OID.length + 8) {
throw new AttestError("the nonce extension is not in Apple's form");
}
return Buffer.from(der.subarray(marker + 6, marker + 6 + 32));
}
/** The leaf's EC public key as an uncompressed point, whose hash is the key id. */
function uncompressedPoint(leaf: X509Certificate): Buffer {
const jwk = leaf.publicKey.export({ format: "jwk" }) as {
x?: string;
y?: string;
};
Iif (!jwk.x || !jwk.y) throw new AttestError("the leaf key is not EC");
return Buffer.concat([
Buffer.from([4]),
Buffer.from(jwk.x, "base64url"),
Buffer.from(jwk.y, "base64url"),
]);
}
function validAt(cert: X509Certificate, now: number): boolean {
return (
new Date(cert.validFrom).getTime() <= now &&
now <= new Date(cert.validTo).getTime()
);
}
const sha256 = (...parts: (Buffer | string)[]) => {
const hash = createHash("sha256");
for (const p of parts) hash.update(p);
return hash.digest();
};
export interface Attested {
environment: "development" | "production";
/** The attested public key, for checking assertions later. */
publicKey: string;
}
/**
* Verifies an App Attest attestation, or throws AttestError with the step
* that failed. `keyId` and `attestation` are base64 as the phone sends them.
*/
export function verifyAttestation(input: {
keyId: string;
attestation: string;
clientData: string;
appId?: string;
root?: string;
now?: number;
}): Attested {
const now = input.now ?? Date.now();
const keyId = Buffer.from(input.keyId, "base64");
Iif (keyId.length !== 32) throw new AttestError("the key id is not 32 bytes");
const object = decodeCbor(Buffer.from(input.attestation, "base64"));
Iif (!(object instanceof Map)) throw new AttestError("not an attestation");
if (object.get("fmt") !== "apple-appattest") {
throw new AttestError("not an App Attest attestation");
}
const statement = object.get("attStmt");
const authData = object.get("authData");
Iif (!(statement instanceof Map) || !Buffer.isBuffer(authData)) {
throw new AttestError("the attestation is incomplete");
}
const x5c = statement.get("x5c");
Iif (!Array.isArray(x5c) || x5c.length < 2 || !x5c.every(Buffer.isBuffer)) {
throw new AttestError("the certificate chain is missing");
}
// 1. The chain: leaf, intermediate, Apple's root.
const leaf = new X509Certificate(x5c[0] as Buffer);
const intermediate = new X509Certificate(x5c[1] as Buffer);
const root = new X509Certificate(input.root ?? APPLE_APP_ATTEST_ROOT_CA);
if (
!leaf.checkIssued(intermediate) ||
!leaf.verify(intermediate.publicKey) ||
!intermediate.checkIssued(root) ||
!intermediate.verify(root.publicKey)
) {
throw new AttestError("the certificate chain does not lead to Apple");
}
if (![leaf, intermediate, root].every((c) => validAt(c, now))) {
throw new AttestError("a certificate in the chain is not valid now");
}
// 2–4. The nonce binds the attestation to this challenge and device.
const nonce = sha256(authData, sha256(input.clientData));
if (!certificateNonce(leaf).equals(nonce)) {
throw new AttestError("the attestation is for another challenge");
}
// 5. The key id is the hash of the attested public key.
if (!sha256(uncompressedPoint(leaf)).equals(keyId)) {
throw new AttestError("the key id does not match the certified key");
}
// 6–9. The authenticator data: this app, a fresh key, its environment.
Iif (authData.length < 55) {
throw new AttestError("authenticator data too short");
}
if (!authData.subarray(0, 32).equals(sha256(input.appId ?? APP_ID))) {
throw new AttestError("the attestation is for another app");
}
Iif (authData.readUInt32BE(33) !== 0) {
throw new AttestError("the key has been used before");
}
const environment = ENVIRONMENTS[authData.subarray(37, 53).toString("hex")];
Iif (!environment) throw new AttestError("unknown App Attest environment");
const idLength = authData.readUInt16BE(53);
Iif (!authData.subarray(55, 55 + idLength).equals(keyId)) {
throw new AttestError("the credential id is not the key id");
}
return {
environment,
publicKey: leaf.publicKey.export({ format: "pem", type: "spki" }) as string,
};
}
|