@forestrie/receipt-verify 0.12.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/chain-binding.d.ts +19 -0
  2. package/dist/chain-binding.d.ts.map +1 -0
  3. package/dist/chain-binding.js +1 -0
  4. package/dist/decode-chain-binding-from-genesis.d.ts +15 -0
  5. package/dist/decode-chain-binding-from-genesis.d.ts.map +1 -0
  6. package/dist/decode-chain-binding-from-genesis.js +55 -0
  7. package/dist/decode-genesis-cbor-map.d.ts +12 -0
  8. package/dist/decode-genesis-cbor-map.d.ts.map +1 -0
  9. package/dist/decode-genesis-cbor-map.js +26 -0
  10. package/dist/decode-trust-root-from-genesis.d.ts +21 -3
  11. package/dist/decode-trust-root-from-genesis.d.ts.map +1 -1
  12. package/dist/decode-trust-root-from-genesis.js +32 -23
  13. package/dist/decoded-trust-root.d.ts +17 -0
  14. package/dist/decoded-trust-root.d.ts.map +1 -0
  15. package/dist/decoded-trust-root.js +1 -0
  16. package/dist/forest-genesis-labels.d.ts +7 -0
  17. package/dist/forest-genesis-labels.d.ts.map +1 -1
  18. package/dist/forest-genesis-labels.js +7 -0
  19. package/dist/index.d.ts +35 -7
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +31 -6
  22. package/dist/resolve-delegated-verify-key.d.ts +6 -0
  23. package/dist/resolve-delegated-verify-key.d.ts.map +1 -1
  24. package/dist/resolve-delegated-verify-key.js +10 -3
  25. package/dist/session-key-endorsement.d.ts +88 -30
  26. package/dist/session-key-endorsement.d.ts.map +1 -1
  27. package/dist/session-key-endorsement.js +151 -40
  28. package/dist/verify-endorsed-leaf.d.ts +102 -0
  29. package/dist/verify-endorsed-leaf.d.ts.map +1 -0
  30. package/dist/verify-endorsed-leaf.js +189 -0
  31. package/package.json +4 -4
  32. package/src/chain-binding.ts +18 -0
  33. package/src/decode-chain-binding-from-genesis.ts +77 -0
  34. package/src/decode-genesis-cbor-map.ts +27 -0
  35. package/src/decode-trust-root-from-genesis.ts +40 -25
  36. package/src/decoded-trust-root.ts +17 -0
  37. package/src/forest-genesis-labels.ts +7 -0
  38. package/src/index.ts +61 -4
  39. package/src/resolve-delegated-verify-key.ts +12 -3
  40. package/src/session-key-endorsement.ts +218 -54
  41. package/src/verify-endorsed-leaf.ts +287 -0
@@ -1,37 +1,65 @@
1
1
  /**
2
- * Passkey session-key endorsement (devdocs ADR-0064, plan-2608-13 Q5).
2
+ * Passkey session-key endorsement, payload v2 (devdocs ADR-0064 as amended by
3
+ * ADR-0065 §3; plan-2608-14 1.2).
3
4
  *
4
5
  * A passkey log root signs ceremonies only; the per-turn user-envelope
5
- * signer is a separate session key, endorsed ONCE by the root via a COSE
6
- * Sign1 in the ADR-0063 WebAuthn envelope. This module owns both sides of
7
- * that artifact's byte-exact shape:
6
+ * signer is a separate session key, endorsed by the root via a COSE Sign1 in
7
+ * the ADR-0063 WebAuthn envelope. This module owns both sides of that
8
+ * artifact's byte-exact shape:
8
9
  *
9
10
  * - build/assemble: the client constructs the TBS, hashes its Sig_structure
10
11
  * into the WebAuthn challenge, and attaches the assertion + signature;
11
12
  * - verify: given the passkey root as trust anchor, validate the artifact
12
- * (typed, challenge-bound, fail-closed) and yield the session public key.
13
+ * (typed, challenge-bound, fail-closed) and yield the session public key
14
+ * and its validity window.
13
15
  *
14
- * The offline trust chain is then:
15
- * grant (root = passkey x‖y) → endorsement → session key → plain-ES256
16
- * per-turn leaves — zero per-leaf format change (ADR-0064 §4).
16
+ * The endorsement rides INSIDE every endorsed leaf (unprotected label
17
+ * `COSE_LABEL_SESSION_KEY_ENDORSEMENT`, -65801) and the chain
18
+ * root → endorsement → session key → leaf → receipt is walked by
19
+ * {@link verifyEndorsedLeaf} (the single offline rung, ADR-0065 §5) and by
20
+ * canopy SCRAPI admission (§4).
17
21
  *
18
- * Domain separation (ADR-0064 §2): the protected header carries the content
19
- * type below, and the payload is EXACTLY `{"sessionKey": bstr .size 64}`.
20
- * Anything else is a verification failure, never a fallback.
22
+ * Domain separation: the protected content type below names payload v2 and
23
+ * the payload is EXACTLY `{"sessionKey": bstr .size 64, "notBefore": uint,
24
+ * "notAfter": uint}` (unix milliseconds, the idtimestamp time domain).
25
+ * Anything else — including a v1 (window-less) artifact — is a verification
26
+ * failure, never a fallback (ADR-0065 §3: v1 is rejected, not grandfathered).
27
+ *
28
+ * The window itself is NOT checked here: the reference time differs per
29
+ * verifier (canopy's clock at admission; the receipted idtimestamp offline).
30
+ * Callers apply {@link checkEndorsementWindow} with the time they hold.
21
31
  */
22
32
  import { type ParsedVerifyKey } from "@forestrie/encoding";
23
33
  /**
24
- * Protected-header content type (label 3) naming the endorsement artifact.
25
- * Signed via Sig_structure, so the artifact's type is non-malleable.
34
+ * Protected-header content type (label 3) naming the v2 endorsement artifact.
35
+ * Signed via Sig_structure, so both the artifact's type AND its payload
36
+ * version are non-malleable.
37
+ */
38
+ export declare const SESSION_KEY_ENDORSEMENT_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement.v2+cbor";
39
+ /**
40
+ * The retired ADR-0064 v1 content type (payload without a window). Exported
41
+ * only so verifiers and tests can name what is being REJECTED; no code path
42
+ * accepts it.
26
43
  */
27
- export declare const SESSION_KEY_ENDORSEMENT_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement+cbor";
44
+ export declare const SESSION_KEY_ENDORSEMENT_V1_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement+cbor";
28
45
  /** Payload map key holding the endorsed session public key (x‖y, 64 bytes). */
29
46
  export declare const SESSION_KEY_PAYLOAD_KEY = "sessionKey";
47
+ /** Payload map key: window start, unix milliseconds, inclusive. */
48
+ export declare const NOT_BEFORE_PAYLOAD_KEY = "notBefore";
49
+ /** Payload map key: window end, unix milliseconds, inclusive. */
50
+ export declare const NOT_AFTER_PAYLOAD_KEY = "notAfter";
51
+ /** Default client window length: 7 days (ADR-0065 §3). */
52
+ export declare const DEFAULT_ENDORSEMENT_WINDOW_MS: number;
53
+ /** Validity window of an endorsement, unix milliseconds, both ends inclusive. */
54
+ export interface EndorsementWindow {
55
+ notBefore: number;
56
+ notAfter: number;
57
+ }
30
58
  /** To-be-signed endorsement halves plus the challenge preimage. */
31
59
  export interface SessionKeyEndorsementTbs {
32
- /** Protected header map bytes `{1: -65800, 3: cty, 4: root x}`. */
60
+ /** Protected header map bytes `{1: -65800, 3: cty(v2), 4: root x}`. */
33
61
  protectedBstr: Uint8Array;
34
- /** Payload bytes: deterministic CBOR `{"sessionKey": x‖y}`. */
62
+ /** Payload bytes: deterministic CBOR `{sessionKey, notBefore, notAfter}`. */
35
63
  payloadBstr: Uint8Array;
36
64
  /**
37
65
  * `Sig_structure` over the halves. The WebAuthn challenge MUST be
@@ -40,15 +68,19 @@ export interface SessionKeyEndorsementTbs {
40
68
  sigStructureBytes: Uint8Array;
41
69
  }
42
70
  /**
43
- * Build the endorsement TBS. The caller hashes `sigStructureBytes` into the
44
- * `navigator.credentials.get` challenge, then assembles with the assertion.
71
+ * Build the v2 endorsement TBS. The caller hashes `sigStructureBytes` into
72
+ * the `navigator.credentials.get` challenge, then assembles with the assertion.
45
73
  *
46
74
  * @param input.rootPublicKeyX - Passkey root x coordinate (kid, 32 bytes)
47
75
  * @param input.sessionPublicKeyXY - Endorsed session public key x‖y (64 bytes)
76
+ * @param input.notBefore - Window start, unix ms inclusive
77
+ * @param input.notAfter - Window end, unix ms inclusive; must exceed notBefore
48
78
  */
49
79
  export declare function buildSessionKeyEndorsementTbs(input: {
50
80
  rootPublicKeyX: Uint8Array;
51
81
  sessionPublicKeyXY: Uint8Array;
82
+ notBefore: number;
83
+ notAfter: number;
52
84
  }): SessionKeyEndorsementTbs;
53
85
  /**
54
86
  * Assemble the final endorsement COSE Sign1: the assertion rides in the
@@ -63,23 +95,26 @@ export declare function assembleSessionKeyEndorsement(input: {
63
95
  /** Low-s P1363 r‖s (64 bytes). */
64
96
  signature: Uint8Array;
65
97
  }): Uint8Array;
98
+ /** Verification failure reasons; the first check that broke. */
99
+ export type SessionKeyEndorsementFailureReason = "endorsement_malformed" | "wrong_alg" | "wrong_content_type" | "kid_invalid" | "kid_mismatch" | "payload_invalid" | "window_invalid" | "uv_required" | "signature_invalid" | "session_key_import_failed";
66
100
  /** Verification outcome; failures name the first check that broke. */
67
- export type SessionKeyEndorsementVerifyResult = {
101
+ export type SessionKeyEndorsementVerifyResult = ({
68
102
  ok: true;
69
103
  /** Endorsed session public key, raw x‖y (64 bytes). */
70
104
  sessionPublicKeyXY: Uint8Array;
71
105
  /** The same key imported for ES256 verify of per-turn leaves. */
72
106
  sessionKey: CryptoKey;
73
- } | {
107
+ } & EndorsementWindow) | {
74
108
  ok: false;
75
- reason: "endorsement_malformed" | "wrong_alg" | "wrong_content_type" | "kid_invalid" | "kid_mismatch" | "payload_invalid" | "signature_invalid" | "session_key_import_failed";
109
+ reason: SessionKeyEndorsementFailureReason;
76
110
  };
77
111
  /** Options for {@link verifySessionKeyEndorsement}. */
78
112
  export interface VerifySessionKeyEndorsementOptions {
79
113
  /**
80
- * Require the assertion's UV flag. Onboarding UV is DEPLOYMENT config,
81
- * not grant policy — the endorsement precedes the grant (ADR-0064 §3).
82
- * User presence is always required regardless.
114
+ * Require the assertion's UV flag. At admission this is the grant's
115
+ * `GF_REQUIRES_USER_VERIFICATION` (ADR-0065 §4); at DO onboarding, where
116
+ * no grant exists yet, deployment config (ADR-0064 §3). User presence is
117
+ * always required regardless.
83
118
  */
84
119
  requireUserVerification?: boolean;
85
120
  /** Emit JSON warning lines on failure paths (no secrets). */
@@ -88,16 +123,39 @@ export interface VerifySessionKeyEndorsementOptions {
88
123
  logPrefix?: string;
89
124
  }
90
125
  /**
91
- * Verify a session-key endorsement under the passkey root and yield the
92
- * endorsed session key. All the ADR-0063 envelope checks (challenge binding
93
- * to this artifact's `Sig_structure`, UP flag, ceremony type, low-s, both
94
- * fail-closed directions) run inside the shared `-65800` verify branch;
95
- * this function adds the ADR-0064 typing rules on top.
126
+ * Verify a v2 session-key endorsement under the passkey root and yield the
127
+ * endorsed session key plus its window. All the ADR-0063 envelope checks
128
+ * (challenge binding to this artifact's `Sig_structure`, UP flag, ceremony
129
+ * type, low-s, both fail-closed directions) run inside the shared `-65800`
130
+ * verify branch; this function adds the ADR-0064/0065 typing rules on top
131
+ * and reports a missing-UV rejection under its own reason so admission can
132
+ * name it (`endorsement_uv_required`, ADR-0065 §4).
96
133
  *
97
134
  * @param endorsementCbor - The endorsement COSE Sign1 bytes
98
135
  * @param rootKeys - Passkey root trust anchor(s), tried in order. Raw
99
136
  * coordinate anchors additionally pin `kid == root x`.
100
- * @param opts - UV requirement (deployment config) and failure logging
137
+ * @param opts - UV requirement and failure logging
101
138
  */
102
139
  export declare function verifySessionKeyEndorsement(endorsementCbor: Uint8Array, rootKeys: ParsedVerifyKey | ParsedVerifyKey[], opts?: VerifySessionKeyEndorsementOptions): Promise<SessionKeyEndorsementVerifyResult>;
140
+ /** Outcome of {@link checkEndorsementWindow}. */
141
+ export type EndorsementWindowCheck = {
142
+ ok: true;
143
+ } | {
144
+ ok: false;
145
+ reason: "endorsement_not_yet_valid" | "endorsement_expired";
146
+ };
147
+ /**
148
+ * Is `atMs` inside the endorsement's window? `notBefore ≤ atMs ≤ notAfter`,
149
+ * both inclusive (ADR-0065 §3). `skewMs` (default 0) tolerates a `notBefore`
150
+ * slightly in the future of a wall-clock verifier; offline verifiers
151
+ * comparing against the receipted idtimestamp should leave it at 0 — that
152
+ * comparison is exact and authoritative.
153
+ *
154
+ * Deliberately a pure function of the two numbers so callers can (and must)
155
+ * run it on every request, structurally outside any verified-endorsement
156
+ * cache (ADR-0065 §4).
157
+ */
158
+ export declare function checkEndorsementWindow(window: EndorsementWindow, atMs: number, opts?: {
159
+ skewMs?: number;
160
+ }): EndorsementWindowCheck;
103
161
  //# sourceMappingURL=session-key-endorsement.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"session-key-endorsement.d.ts","sourceRoot":"","sources":["../src/session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CAAE,GACZ;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,2BAA2B,GAAG,qBAAqB,CAAA;CAAE,CAAC;AAE/E;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,iBAAiB,EACzB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACzB,sBAAsB,CASxB"}
@@ -1,39 +1,77 @@
1
1
  /**
2
- * Passkey session-key endorsement (devdocs ADR-0064, plan-2608-13 Q5).
2
+ * Passkey session-key endorsement, payload v2 (devdocs ADR-0064 as amended by
3
+ * ADR-0065 §3; plan-2608-14 1.2).
3
4
  *
4
5
  * A passkey log root signs ceremonies only; the per-turn user-envelope
5
- * signer is a separate session key, endorsed ONCE by the root via a COSE
6
- * Sign1 in the ADR-0063 WebAuthn envelope. This module owns both sides of
7
- * that artifact's byte-exact shape:
6
+ * signer is a separate session key, endorsed by the root via a COSE Sign1 in
7
+ * the ADR-0063 WebAuthn envelope. This module owns both sides of that
8
+ * artifact's byte-exact shape:
8
9
  *
9
10
  * - build/assemble: the client constructs the TBS, hashes its Sig_structure
10
11
  * into the WebAuthn challenge, and attaches the assertion + signature;
11
12
  * - verify: given the passkey root as trust anchor, validate the artifact
12
- * (typed, challenge-bound, fail-closed) and yield the session public key.
13
+ * (typed, challenge-bound, fail-closed) and yield the session public key
14
+ * and its validity window.
13
15
  *
14
- * The offline trust chain is then:
15
- * grant (root = passkey x‖y) → endorsement → session key → plain-ES256
16
- * per-turn leaves — zero per-leaf format change (ADR-0064 §4).
16
+ * The endorsement rides INSIDE every endorsed leaf (unprotected label
17
+ * `COSE_LABEL_SESSION_KEY_ENDORSEMENT`, -65801) and the chain
18
+ * root → endorsement → session key → leaf → receipt is walked by
19
+ * {@link verifyEndorsedLeaf} (the single offline rung, ADR-0065 §5) and by
20
+ * canopy SCRAPI admission (§4).
17
21
  *
18
- * Domain separation (ADR-0064 §2): the protected header carries the content
19
- * type below, and the payload is EXACTLY `{"sessionKey": bstr .size 64}`.
20
- * Anything else is a verification failure, never a fallback.
22
+ * Domain separation: the protected content type below names payload v2 and
23
+ * the payload is EXACTLY `{"sessionKey": bstr .size 64, "notBefore": uint,
24
+ * "notAfter": uint}` (unix milliseconds, the idtimestamp time domain).
25
+ * Anything else — including a v1 (window-less) artifact — is a verification
26
+ * failure, never a fallback (ADR-0065 §3: v1 is rejected, not grandfathered).
27
+ *
28
+ * The window itself is NOT checked here: the reference time differs per
29
+ * verifier (canopy's clock at admission; the receipted idtimestamp offline).
30
+ * Callers apply {@link checkEndorsementWindow} with the time they hold.
21
31
  */
22
- import { COSE_ALG_ES256_WEBAUTHN, decodeCborDeterministic, decodeCoseSign1, encodeCborDeterministic, encodeCoseSign1Raw, encodeSigStructure, verifyCoseSign1WithParsedKey, WEBAUTHN_ENVELOPE_LABEL, } from "@forestrie/encoding";
32
+ import { COSE_ALG_ES256_WEBAUTHN, coseUnprotectedToMap, decodeCborDeterministic, decodeCoseSign1, encodeCborDeterministic, encodeCoseSign1Raw, encodeSigStructure, verifyCoseSign1WithParsedKey, WEBAUTHN_ENVELOPE_LABEL, } from "@forestrie/encoding";
23
33
  import { importEs256PublicKeyFromGrantDataXy64 } from "./decode-trust-root-cbor.js";
24
34
  /**
25
- * Protected-header content type (label 3) naming the endorsement artifact.
26
- * Signed via Sig_structure, so the artifact's type is non-malleable.
35
+ * Protected-header content type (label 3) naming the v2 endorsement artifact.
36
+ * Signed via Sig_structure, so both the artifact's type AND its payload
37
+ * version are non-malleable.
38
+ */
39
+ export const SESSION_KEY_ENDORSEMENT_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement.v2+cbor";
40
+ /**
41
+ * The retired ADR-0064 v1 content type (payload without a window). Exported
42
+ * only so verifiers and tests can name what is being REJECTED; no code path
43
+ * accepts it.
27
44
  */
28
- export const SESSION_KEY_ENDORSEMENT_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement+cbor";
45
+ export const SESSION_KEY_ENDORSEMENT_V1_CONTENT_TYPE = "application/vnd.forestrie.session-key-endorsement+cbor";
29
46
  /** Payload map key holding the endorsed session public key (x‖y, 64 bytes). */
30
47
  export const SESSION_KEY_PAYLOAD_KEY = "sessionKey";
48
+ /** Payload map key: window start, unix milliseconds, inclusive. */
49
+ export const NOT_BEFORE_PAYLOAD_KEY = "notBefore";
50
+ /** Payload map key: window end, unix milliseconds, inclusive. */
51
+ export const NOT_AFTER_PAYLOAD_KEY = "notAfter";
52
+ /** Default client window length: 7 days (ADR-0065 §3). */
53
+ export const DEFAULT_ENDORSEMENT_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
54
+ /** WebAuthn authenticatorData UV flag bit (WebAuthn L2 §6.1). */
55
+ const AUTH_FLAG_UV = 0x04;
56
+ function isUnixMs(v) {
57
+ return typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
58
+ }
59
+ /**
60
+ * Structural window check shared by the builder and the verifier: both ends
61
+ * are unsigned safe integers and the window is non-empty (ADR-0065 §3
62
+ * "malformed windows are rejected": `notAfter ≤ notBefore`).
63
+ */
64
+ function windowWellFormed(w) {
65
+ return (isUnixMs(w.notBefore) && isUnixMs(w.notAfter) && w.notAfter > w.notBefore);
66
+ }
31
67
  /**
32
- * Build the endorsement TBS. The caller hashes `sigStructureBytes` into the
33
- * `navigator.credentials.get` challenge, then assembles with the assertion.
68
+ * Build the v2 endorsement TBS. The caller hashes `sigStructureBytes` into
69
+ * the `navigator.credentials.get` challenge, then assembles with the assertion.
34
70
  *
35
71
  * @param input.rootPublicKeyX - Passkey root x coordinate (kid, 32 bytes)
36
72
  * @param input.sessionPublicKeyXY - Endorsed session public key x‖y (64 bytes)
73
+ * @param input.notBefore - Window start, unix ms inclusive
74
+ * @param input.notAfter - Window end, unix ms inclusive; must exceed notBefore
37
75
  */
38
76
  export function buildSessionKeyEndorsementTbs(input) {
39
77
  if (input.rootPublicKeyX.length !== 32) {
@@ -42,13 +80,20 @@ export function buildSessionKeyEndorsementTbs(input) {
42
80
  if (input.sessionPublicKeyXY.length !== 64) {
43
81
  throw new Error("endorsed session key must be 64 bytes (x||y)");
44
82
  }
83
+ if (!windowWellFormed(input)) {
84
+ throw new Error("endorsement window must be unsigned safe-integer unix ms with notAfter > notBefore");
85
+ }
45
86
  const protectedBstr = encodeCborDeterministic(new Map([
46
87
  [1, COSE_ALG_ES256_WEBAUTHN],
47
88
  [3, SESSION_KEY_ENDORSEMENT_CONTENT_TYPE],
48
89
  [4, input.rootPublicKeyX],
49
90
  ]));
91
+ // Deterministic CBOR sorts the keys (RFC 8949 §4.2.1); insertion order here
92
+ // is irrelevant to the bytes.
50
93
  const payloadBstr = encodeCborDeterministic(new Map([
51
94
  [SESSION_KEY_PAYLOAD_KEY, input.sessionPublicKeyXY],
95
+ [NOT_BEFORE_PAYLOAD_KEY, input.notBefore],
96
+ [NOT_AFTER_PAYLOAD_KEY, input.notAfter],
52
97
  ]));
53
98
  const sigStructureBytes = encodeSigStructure(protectedBstr, new Uint8Array(0), payloadBstr);
54
99
  return { protectedBstr, payloadBstr, sigStructureBytes };
@@ -89,16 +134,26 @@ function protectedHeaderEntries(protectedBstr) {
89
134
  }
90
135
  return null;
91
136
  }
92
- function decodePayloadSessionKeyXy(payloadBstr) {
137
+ function toNumberIfInt(v) {
138
+ // Deterministic decode may surface large ints as bigint; the window is
139
+ // unix ms (< 2^53) so a safe bigint is folded back to a number.
140
+ if (typeof v === "bigint" &&
141
+ v >= 0n &&
142
+ v <= BigInt(Number.MAX_SAFE_INTEGER)) {
143
+ return Number(v);
144
+ }
145
+ return v;
146
+ }
147
+ function decodePayloadV2(payloadBstr) {
93
148
  let decoded;
94
149
  try {
95
150
  decoded = decodeCborDeterministic(payloadBstr);
96
151
  }
97
152
  catch {
98
- return null;
153
+ return { kind: "payload_invalid" };
99
154
  }
100
- // Exactly one entry, exactly the sessionKey label, exactly 64 bytes
101
- // (ADR-0064 §2) — anything else is a failure, never a partial read.
155
+ // Exactly three entries, exactly the three labels, exactly these shapes
156
+ // (ADR-0065 §3) — anything else is a failure, never a partial read.
102
157
  let entries;
103
158
  if (decoded instanceof Map) {
104
159
  entries = [...decoded.entries()];
@@ -107,28 +162,51 @@ function decodePayloadSessionKeyXy(payloadBstr) {
107
162
  entries = Object.entries(decoded);
108
163
  }
109
164
  else {
110
- return null;
165
+ return { kind: "payload_invalid" };
111
166
  }
112
- if (entries.length !== 1)
113
- return null;
114
- const [key, value] = entries[0];
115
- if (key !== SESSION_KEY_PAYLOAD_KEY)
167
+ if (entries.length !== 3)
168
+ return { kind: "payload_invalid" };
169
+ const byKey = new Map(entries);
170
+ const sessionPublicKeyXY = byKey.get(SESSION_KEY_PAYLOAD_KEY);
171
+ if (!(sessionPublicKeyXY instanceof Uint8Array) ||
172
+ sessionPublicKeyXY.length !== 64) {
173
+ return { kind: "payload_invalid" };
174
+ }
175
+ const notBefore = toNumberIfInt(byKey.get(NOT_BEFORE_PAYLOAD_KEY));
176
+ const notAfter = toNumberIfInt(byKey.get(NOT_AFTER_PAYLOAD_KEY));
177
+ if (!isUnixMs(notBefore) || !isUnixMs(notAfter)) {
178
+ return { kind: "payload_invalid" };
179
+ }
180
+ const window = { notBefore, notAfter };
181
+ if (!windowWellFormed(window))
182
+ return { kind: "window_invalid" };
183
+ return { kind: "ok", sessionPublicKeyXY, window };
184
+ }
185
+ /** Read the assertion's flags byte from the -65800 envelope, if well-shaped. */
186
+ function envelopeFlags(unprotected) {
187
+ const envelope = coseUnprotectedToMap(unprotected).get(WEBAUTHN_ENVELOPE_LABEL);
188
+ if (!Array.isArray(envelope) || envelope.length !== 2)
116
189
  return null;
117
- if (!(value instanceof Uint8Array) || value.length !== 64)
190
+ const authenticatorData = envelope[0];
191
+ if (!(authenticatorData instanceof Uint8Array) ||
192
+ authenticatorData.length < 37) {
118
193
  return null;
119
- return value;
194
+ }
195
+ return authenticatorData[32];
120
196
  }
121
197
  /**
122
- * Verify a session-key endorsement under the passkey root and yield the
123
- * endorsed session key. All the ADR-0063 envelope checks (challenge binding
124
- * to this artifact's `Sig_structure`, UP flag, ceremony type, low-s, both
125
- * fail-closed directions) run inside the shared `-65800` verify branch;
126
- * this function adds the ADR-0064 typing rules on top.
198
+ * Verify a v2 session-key endorsement under the passkey root and yield the
199
+ * endorsed session key plus its window. All the ADR-0063 envelope checks
200
+ * (challenge binding to this artifact's `Sig_structure`, UP flag, ceremony
201
+ * type, low-s, both fail-closed directions) run inside the shared `-65800`
202
+ * verify branch; this function adds the ADR-0064/0065 typing rules on top
203
+ * and reports a missing-UV rejection under its own reason so admission can
204
+ * name it (`endorsement_uv_required`, ADR-0065 §4).
127
205
  *
128
206
  * @param endorsementCbor - The endorsement COSE Sign1 bytes
129
207
  * @param rootKeys - Passkey root trust anchor(s), tried in order. Raw
130
208
  * coordinate anchors additionally pin `kid == root x`.
131
- * @param opts - UV requirement (deployment config) and failure logging
209
+ * @param opts - UV requirement and failure logging
132
210
  */
133
211
  export async function verifySessionKeyEndorsement(endorsementCbor, rootKeys, opts) {
134
212
  const anchors = Array.isArray(rootKeys) ? rootKeys : [rootKeys];
@@ -148,9 +226,17 @@ export async function verifySessionKeyEndorsement(endorsementCbor, rootKeys, opt
148
226
  if (!(header.kid instanceof Uint8Array) || header.kid.length !== 32) {
149
227
  return { ok: false, reason: "kid_invalid" };
150
228
  }
151
- const sessionPublicKeyXY = decodePayloadSessionKeyXy(decoded.payloadBstr);
152
- if (!sessionPublicKeyXY)
153
- return { ok: false, reason: "payload_invalid" };
229
+ const payload = decodePayloadV2(decoded.payloadBstr);
230
+ if (payload.kind !== "ok")
231
+ return { ok: false, reason: payload.kind };
232
+ if (opts?.requireUserVerification) {
233
+ const flags = envelopeFlags(decoded.unprotected);
234
+ // A malformed envelope is left to the shared branch to reject; only a
235
+ // well-shaped assertion that plainly lacks UV gets the distinct reason.
236
+ if (flags !== null && (flags & AUTH_FLAG_UV) === 0) {
237
+ return { ok: false, reason: "uv_required" };
238
+ }
239
+ }
154
240
  let kidMismatch = false;
155
241
  for (const anchor of anchors) {
156
242
  // When the anchor exposes raw coordinates, the signed kid must name it —
@@ -173,16 +259,41 @@ export async function verifySessionKeyEndorsement(endorsementCbor, rootKeys, opt
173
259
  continue;
174
260
  let sessionKey;
175
261
  try {
176
- sessionKey =
177
- await importEs256PublicKeyFromGrantDataXy64(sessionPublicKeyXY);
262
+ sessionKey = await importEs256PublicKeyFromGrantDataXy64(payload.sessionPublicKeyXY);
178
263
  }
179
264
  catch {
180
265
  return { ok: false, reason: "session_key_import_failed" };
181
266
  }
182
- return { ok: true, sessionPublicKeyXY, sessionKey };
267
+ return {
268
+ ok: true,
269
+ sessionPublicKeyXY: payload.sessionPublicKeyXY,
270
+ sessionKey,
271
+ ...payload.window,
272
+ };
183
273
  }
184
274
  return {
185
275
  ok: false,
186
276
  reason: kidMismatch ? "kid_mismatch" : "signature_invalid",
187
277
  };
188
278
  }
279
+ /**
280
+ * Is `atMs` inside the endorsement's window? `notBefore ≤ atMs ≤ notAfter`,
281
+ * both inclusive (ADR-0065 §3). `skewMs` (default 0) tolerates a `notBefore`
282
+ * slightly in the future of a wall-clock verifier; offline verifiers
283
+ * comparing against the receipted idtimestamp should leave it at 0 — that
284
+ * comparison is exact and authoritative.
285
+ *
286
+ * Deliberately a pure function of the two numbers so callers can (and must)
287
+ * run it on every request, structurally outside any verified-endorsement
288
+ * cache (ADR-0065 §4).
289
+ */
290
+ export function checkEndorsementWindow(window, atMs, opts) {
291
+ const skew = opts?.skewMs ?? 0;
292
+ if (atMs + skew < window.notBefore) {
293
+ return { ok: false, reason: "endorsement_not_yet_valid" };
294
+ }
295
+ if (atMs > window.notAfter) {
296
+ return { ok: false, reason: "endorsement_expired" };
297
+ }
298
+ return { ok: true };
299
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The single offline rung for a passkey-rooted log (devdocs ADR-0065 §5,
3
+ * plan-2608-14 1.2). From public artifacts only — the log root (on-chain
4
+ * `logRootKey` == the grant's `grantData`), the exact registered leaf bytes,
5
+ * the receipt and its idtimestamp — reconstruct:
6
+ *
7
+ * root → endorsement (-65801 inside the leaf; -65800 verify under the
8
+ * root, UV per the grant flag, window from the payload)
9
+ * → session key
10
+ * → leaf: kid == session x, ES256 signature under the session key
11
+ * → receipt: leaf bytes hash to the receipted index (inclusion),
12
+ * receipted idtimestamp ∈ [notBefore, notAfter]
13
+ *
14
+ * There is exactly one route and no fallback: a leaf without an endorsement
15
+ * is not verified under the root here (that is the plain
16
+ * `verifyReceiptOffline*` path for root-signed logs), and the ADR-0064
17
+ * export-fed rung (`resolveEndorsedSessionKey` over a `/receipts` export)
18
+ * is gone — the endorsement an auditor needs is inside the committed leaf.
19
+ *
20
+ * Tampering closes both ways (ADR-0065 §5): editing the endorsement changes
21
+ * `contentHash`, so inclusion fails; substituting a different valid
22
+ * endorsement changes the session key, so the leaf signature fails.
23
+ */
24
+ import { type SessionKeyEndorsementFailureReason } from "./session-key-endorsement.js";
25
+ export interface VerifyEndorsedLeafInput {
26
+ /**
27
+ * The log root as raw P-256 x‖y (64 bytes): the grant's `grantData`, the
28
+ * value univocity binds as `logRootKey`. The endorsement's trust anchor;
29
+ * its kid is pinned to this x.
30
+ */
31
+ rootPublicKeyXY: Uint8Array;
32
+ /** The EXACT registered leaf bytes (COSE Sign1 carrying -65801). */
33
+ statementCbor: Uint8Array;
34
+ /** The receipt over that leaf. */
35
+ receiptCbor: Uint8Array;
36
+ /** The receipted idtimestamp (8 bytes, big-endian). */
37
+ idtimestampBe8: Uint8Array;
38
+ /**
39
+ * Receipt trust anchors (the sealer or, for a delegated seal, the
40
+ * delegation-cert issuer — for a user log that is the root itself, which
41
+ * signs the sealing delegation with a passkey gesture). Defaults to the
42
+ * root. See `verifyReceiptOfflineWithKeys` for the trust model.
43
+ */
44
+ trustKeys?: CryptoKey[];
45
+ }
46
+ export interface VerifyEndorsedLeafOptions {
47
+ /**
48
+ * The grant's `GF_REQUIRES_USER_VERIFICATION` (ADR-0063 §4): when set the
49
+ * endorsement's assertion must carry UV. User presence is always required.
50
+ */
51
+ requireUserVerification?: boolean;
52
+ /** Emit JSON warning lines on failure paths (no secrets). */
53
+ logFailures?: boolean;
54
+ logPrefix?: string;
55
+ }
56
+ export type VerifyEndorsedLeafStage = "endorsement" | "leaf" | "window" | "receipt";
57
+ export type VerifyEndorsedLeafResult = {
58
+ ok: true;
59
+ /** The endorsed session public key, raw x‖y (64 bytes). */
60
+ sessionPublicKeyXY: Uint8Array;
61
+ /** The endorsement window, unix ms inclusive. */
62
+ notBefore: number;
63
+ notAfter: number;
64
+ /** The receipted idtimestamp's time component, unix ms. */
65
+ leafIdtimestampMs: number;
66
+ } | {
67
+ ok: false;
68
+ stage: VerifyEndorsedLeafStage;
69
+ reason: string;
70
+ };
71
+ /** ADR-0065 §4 reason vocabulary, shared with canopy admission. */
72
+ export type EndorsementAdmissionReason = "endorsement_missing" | "endorsement_invalid" | "endorsement_root_mismatch" | "endorsement_uv_required" | "endorsement_expired" | "endorsement_not_yet_valid";
73
+ /**
74
+ * Fold the endorsement verifier's fine-grained reasons into the ADR-0065 §4
75
+ * admission vocabulary. `signature_invalid` under a coordinate anchor means
76
+ * "a well-formed endorsement that does not chain to THIS root" — the
77
+ * `endorsement_root_mismatch` case; a malformed window is
78
+ * `endorsement_expired` (§4: "window, both directions, and malformed
79
+ * windows" — no instant is inside it); everything else structural is
80
+ * `endorsement_invalid`.
81
+ */
82
+ export declare function endorsementAdmissionReason(reason: SessionKeyEndorsementFailureReason): EndorsementAdmissionReason;
83
+ /**
84
+ * Read the endorsement bytes from a leaf's unprotected header.
85
+ * `missing` when there is no -65801 entry; `invalid` when the entry is not
86
+ * a byte string (present-but-unusable is never "absent").
87
+ */
88
+ export declare function extractLeafEndorsement(statementCbor: Uint8Array): {
89
+ kind: "ok";
90
+ endorsement: Uint8Array;
91
+ kid: Uint8Array | null;
92
+ } | {
93
+ kind: "missing";
94
+ } | {
95
+ kind: "invalid";
96
+ };
97
+ /**
98
+ * Verify an endorsed leaf and its receipt from public artifacts only.
99
+ * Pure over bytes; no network (rules-of-the-road C1).
100
+ */
101
+ export declare function verifyEndorsedLeaf(input: VerifyEndorsedLeafInput, opts?: VerifyEndorsedLeafOptions): Promise<VerifyEndorsedLeafResult>;
102
+ //# sourceMappingURL=verify-endorsed-leaf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verify-endorsed-leaf.d.ts","sourceRoot":"","sources":["../src/verify-endorsed-leaf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAWH,OAAO,EAGL,KAAK,kCAAkC,EACxC,MAAM,8BAA8B,CAAC;AAMtC,MAAM,WAAW,uBAAuB;IACtC;;;;OAIG;IACH,eAAe,EAAE,UAAU,CAAC;IAC5B,oEAAoE;IACpE,aAAa,EAAE,UAAU,CAAC;IAC1B,kCAAkC;IAClC,WAAW,EAAE,UAAU,CAAC;IACxB,uDAAuD;IACvD,cAAc,EAAE,UAAU,CAAC;IAC3B;;;;;OAKG;IACH,SAAS,CAAC,EAAE,SAAS,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,6DAA6D;IAC7D,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,uBAAuB,GAC/B,aAAa,GACb,MAAM,GACN,QAAQ,GACR,SAAS,CAAC;AAEd,MAAM,MAAM,wBAAwB,GAChC;IACE,EAAE,EAAE,IAAI,CAAC;IACT,2DAA2D;IAC3D,kBAAkB,EAAE,UAAU,CAAC;IAC/B,iDAAiD;IACjD,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,2DAA2D;IAC3D,iBAAiB,EAAE,MAAM,CAAC;CAC3B,GACD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAA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