mdip-agent-kit 0.1.3 → 0.1.5

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 (43) hide show
  1. package/AGENTS.md +4 -3
  2. package/CHANGELOG.md +86 -0
  3. package/README.md +130 -29
  4. package/bin/mdip-agent.js +55 -16
  5. package/examples/agent-handshake.js +6 -4
  6. package/examples/purchase-authorization.js +5 -2
  7. package/llms.txt +4 -1
  8. package/package.json +6 -3
  9. package/src/index.js +209 -49
  10. package/test/api-security.mjs +130 -0
  11. package/test/e2e.sh +68 -0
  12. package/test/vectors-negative.sh +232 -0
  13. package/vectors/README.md +15 -4
  14. package/vectors/decrypt-openssl.sh +11 -2
  15. package/vectors/expiry-cases.txt +64 -15
  16. package/vectors/generate.js +9 -4
  17. package/vectors/kav-1/SHA256SUMS +7 -3
  18. package/vectors/kav-1/payload.json +3 -3
  19. package/vectors/kav-1/signed.json +6 -6
  20. package/vectors/kav-1/test-key.json +7 -7
  21. package/vectors/make-mdip-fixture-2.js +13 -11
  22. package/vectors/make-mdip-fixture.js +12 -7
  23. package/vectors/mdip-challenge-1/SHA256SUMS +15 -5
  24. package/vectors/mdip-challenge-1/challenge.op.json +11 -11
  25. package/vectors/mdip-challenge-1/challenge.signed-op.bin +1 -0
  26. package/vectors/mdip-challenge-1/dids.json +3 -3
  27. package/vectors/mdip-challenge-1/responder-agent.op.json +7 -7
  28. package/vectors/mdip-challenge-1/responder-agent.signed-op.bin +1 -0
  29. package/vectors/mdip-challenge-1/response.op.json +15 -15
  30. package/vectors/mdip-challenge-1/response.signed-op.bin +1 -0
  31. package/vectors/mdip-challenge-1/verifier-agent.op.json +7 -7
  32. package/vectors/mdip-challenge-1/verifier-agent.signed-op.bin +1 -0
  33. package/vectors/mdip-challenge-1/verify-response.json +3 -3
  34. package/vectors/mdip-challenge-2/SHA256SUMS +17 -11
  35. package/vectors/mdip-challenge-2/challenge.op.json +11 -11
  36. package/vectors/mdip-challenge-2/dids.json +3 -3
  37. package/vectors/mdip-challenge-2/responder-agent.op.json +7 -7
  38. package/vectors/mdip-challenge-2/response.op.json +15 -15
  39. package/vectors/mdip-challenge-2/test-verifier.json +9 -9
  40. package/vectors/mdip-challenge-2/verifier-agent.op.json +7 -7
  41. package/vectors/mdip-challenge-2/verify-response.json +6 -6
  42. package/vectors/verify-openssl.sh +269 -40
  43. package/vectors/verify-with-kit.js +207 -10
@@ -0,0 +1,232 @@
1
+ #!/usr/bin/env bash
2
+ # Mutation tests for the offline vector checkers: every tampered copy of vectors/ must FAIL, including
3
+ # "self-consistent" forgeries where the attacker also rewrites SHA256SUMS (and dids.json) to match.
4
+ # Covers every convenience copy (op.json, dids.json, verify-response.json, test keys), the unsigned MDIP
5
+ # signature.signed field, base58 leading zeros, dotfiles/symlinks/duplicate SHA256SUMS lines, and the jq-less path.
6
+ # Also checks the clean copy passes without jq, and that a non-OpenSSL-3 binary is refused.
7
+ # Usage: bash test/vectors-negative.sh (bash + OpenSSL 3 + node; no network; the verify-with-kit.js
8
+ # cases run only after `npm install`)
9
+ set -uo pipefail
10
+ cd "$(dirname "$0")/.."
11
+ root=$(mktemp -d "${TMPDIR:-/tmp}/mdip-neg.XXXXXX"); trap 'rm -rf "$root"' EXIT
12
+ fail=0; n=0
13
+ SH=${BASH:-bash} # run the checker under the same bash as this script (e.g. /bin/bash 3.2 on macOS)
14
+ resum() { local d=$1 f; ( cd "$d" && for f in $(ls | LC_ALL=C sort); do [ "$f" = SHA256SUMS ] && continue
15
+ printf '%s %s\n' "$("${OPENSSL:-openssl}" dgst -sha256 -r "$f" | cut -d' ' -f1)" "$f"; done > "$root/sums.tmp" && mv "$root/sums.tmp" SHA256SUMS ); }
16
+ # the copy sits at $root/v next to $root/src and $root/node_modules, so verify-with-kit.js imports work unchanged
17
+ # node_modules may be hoisted (installed as a dependency): use the nearest one that has @mdip/cipher
18
+ nm=; d=$PWD; while :; do [ -d "$d/node_modules/@mdip/cipher" ] && { nm=$d/node_modules; break; }; [ "$d" = / ] && break; d=$(dirname "$d"); done
19
+ mkdir -p "$root/src"; cp src/index.js "$root/src/"; [ -n "$nm" ] && ln -s "$nm" "$root/node_modules"
20
+ fresh() { rm -rf "$root/v"; cp -R vectors "$root/v"; }
21
+ # expect_fail NAME CHECKER: CHECKER is "sh" (verify-openssl.sh), "shnojq" (same, jq hidden) or "js" (verify-with-kit.js)
22
+ havekit=0; [ -n "$nm" ] && havekit=1 # verify-with-kit.js needs `npm install`
23
+ expect_fail() { n=$((n+1)); local out
24
+ if [ "$2" = js ] && [ $havekit = 0 ]; then echo "skip (no npm install): $1 [js]"; return; fi
25
+ case $2 in js) out=$(cd "$root/v" && node verify-with-kit.js 2>&1);; shnojq) out=$(cd "$root/v" && PATH="$root/nojq" "$SH" verify-openssl.sh 2>&1);;
26
+ *) out=$("$SH" "$root/v/verify-openssl.sh" 2>&1);; esac
27
+ if [ $? -ne 0 ] && ! printf %s "$out" | grep -q '^ALL VECTORS OK$'; then echo "ok rejected: $1 ($(printf %s "$out" | grep -m1 '^FAIL' | cut -c6-))"
28
+ else echo "FAIL accepted: $1"; fail=1; fi; }
29
+ did_of() { node -e 'const c=require("crypto"),b=Buffer.concat([Buffer.from("0180041220","hex"),c.createHash("sha256").update(require("fs").readFileSync(process.argv[1])).digest()]);const A="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";let n=BigInt("0x"+b.toString("hex")),s="";while(n>0n){s=A[Number(n%58n)]+s;n/=58n}console.log("did:mdip:z"+s)' "$1"; }
30
+ # forge DIR OP JS: edit OP's signed op with JS (on `o`), rewrite signed-op.bin (JCS) + op.json (pretty) consistently,
31
+ # recompute OP's DID into dids.json and resum: a fully self-consistent forgery that keeps the original signature
32
+ forge() { node -e '
33
+ const fs=require("fs"),cr=require("crypto"),[d,k,ex]=process.argv.slice(1);
34
+ const sk=x=>Array.isArray(x)?x.map(sk):x&&typeof x==="object"?Object.fromEntries(Object.keys(x).sort().map(q=>[q,sk(x[q])])):x;
35
+ const o=JSON.parse(fs.readFileSync(d+"/"+k+".signed-op.bin"));new Function("o",ex)(o);const j=JSON.stringify(sk(o));
36
+ fs.writeFileSync(d+"/"+k+".signed-op.bin",j);fs.writeFileSync(d+"/"+k+".op.json",JSON.stringify(sk(o),null,2)+"\n");
37
+ const b=Buffer.concat([Buffer.from("0180041220","hex"),cr.createHash("sha256").update(j).digest()]);const A="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
38
+ let n=BigInt("0x"+b.toString("hex")),s="";while(n>0n){s=A[Number(n%58n)]+s;n/=58n}
39
+ const ds=JSON.parse(fs.readFileSync(d+"/dids.json"));ds[k]="did:mdip:z"+s;fs.writeFileSync(d+"/dids.json",JSON.stringify(sk(ds),null,2)+"\n");' "$@" && resum "$1"; }
40
+ # jedit FILE JS: edit a pretty JSON convenience file (on `o`), keep it sorted + pretty, resum its directory
41
+ jedit() { node -e '
42
+ const fs=require("fs"),[f,ex]=process.argv.slice(1);const sk=x=>Array.isArray(x)?x.map(sk):x&&typeof x==="object"?Object.fromEntries(Object.keys(x).sort().map(q=>[q,sk(x[q])])):x;
43
+ const o=JSON.parse(fs.readFileSync(f));new Function("o",ex)(o);fs.writeFileSync(f,JSON.stringify(sk(o),null,2)+"\n");' "$@" && resum "$(dirname "$1")"; }
44
+ both() { expect_fail "$1" sh; expect_fail "$1" js; }
45
+ sub() { local f=$1; shift; sed "$@" "$f" > "$f.tmp" && mv "$f.tmp" "$f"; }
46
+ C2=$root/v/mdip-challenge-2
47
+
48
+ # clean copy with jq hidden: a PATH of symlinks to everything on the current PATH except jq
49
+ mkdir -p "$root/nojq"; IFS=: read -r -a pdirs <<< "$PATH"
50
+ for pd in "${pdirs[@]}"; do for x in "$pd"/*; do b=${x##*/}; [ "$b" = jq ] || [ -e "$root/nojq/$b" ] || ln -s "$x" "$root/nojq/$b" 2>/dev/null; done; done
51
+ fresh; out=$(cd "$root/v" && PATH="$root/nojq" "$SH" verify-openssl.sh 2>&1)
52
+ [ $? = 0 ] && printf %s "$out" | grep -q '(no jq: the strict shape check above is the parse)' && printf %s "$out" | grep -q '^ALL VECTORS OK$' && echo "ok clean copy passes without jq (strict-shape parse)" || { echo "FAIL clean copy without jq"; fail=1; }
53
+ if [ $havekit = 0 ]; then echo "skip clean copy verify-with-kit.js (no npm install)"; else
54
+ fresh; out=$(cd "$root/v" && node verify-with-kit.js 2>&1) && printf %s "$out" | grep -q '^ALL VECTORS OK$' && echo "ok clean copy passes verify-with-kit.js" || { echo "FAIL clean copy verify-with-kit.js"; fail=1; }; fi
55
+ fresh; out=$(OPENSSL=/bin/false "$SH" "$root/v/verify-openssl.sh" 2>&1) && { echo "FAIL non-OpenSSL-3 binary accepted"; fail=1; } || echo "ok non-OpenSSL-3 binary refused"
56
+
57
+ fresh; printf 'x' >> "$C2/challenge.message.bin"; expect_fail "challenge.message.bin changed, SHA256SUMS not updated" sh
58
+ fresh; : > "$root/v/kav-1/extra.bin"; expect_fail "unlisted extra file" sh
59
+ C1=$root/v/mdip-challenge-1 K=$root/v/kav-1
60
+ fresh; forge "$C2" challenge 'o.mdip.validUntil="2026-10-27T17:52:16.038Z"'
61
+ both "forged challenge signed-op (later expiry) + op.json + recomputed DID + resummed"
62
+ fresh; forge "$C2" response 'o.mdip.validUntil="2099-01-01T00:00:00.000Z"'
63
+ both "forged response signed-op (2099 expiry) + op.json + recomputed DID + resummed [accepted by d93162d]"
64
+ fresh; forge "$C2" response 'o.signature.signed="2099-01-01T00:00:00.000Z"'
65
+ both "response signature.signed (unsigned by MDIP) moved to 2099 + recomputed DID [accepted by c41252f]"
66
+ expect_fail "response signature.signed moved to 2099 + recomputed DID" shnojq
67
+ fresh; forge "$C1" response 'o.signature.signed="2099-01-01T00:00:00.000Z"'
68
+ both "mdip-challenge-1 response signature.signed moved to 2099 + recomputed DID [accepted by c41252f]"
69
+ fresh; forge "$C2" response 'o.signature.signed="2026-10-26T16:52:16.080"'
70
+ both "response signature.signed without a zone + recomputed DID"
71
+ fresh; forge "$C2" response 'o.signature.note="relayed"'
72
+ both "extra key inside the response signature block + recomputed DID [accepted by c41252f]"
73
+ fresh; forge "$C2" verifier-agent 'o.signature.signer="did:mdip:z3v8AuabH5z2RyBFCP8KzhS98jSL8Y62JPfis8EJiBo6io6QoRA"'
74
+ both "signer added to a self-signed agent create + recomputed DID"
75
+ fresh; sub "$C2/response.plaintext.json" 's/"challenge":"did:mdip:z3v8AuaUJb/"challenge":"did:mdip:z3v8AuaUJc/'; resum "$C2"
76
+ expect_fail "response.plaintext.json names another challenge (resummed)" sh
77
+ fresh; sub "$C2/dids.json" 's/"challenge": "did:mdip:z3v8AuaUJbQT6/"challenge": "did:mdip:z3v8AuaUJbQT7/'; resum "$C2"
78
+ expect_fail "dids.json challenge DID changed (resummed)" sh
79
+ fresh; cp "$C2/verifier-agent.pub.pem" "$C2/responder-agent.pub.pem"; resum "$C2"
80
+ expect_fail "responder pub.pem replaced by verifier key (resummed)" sh
81
+ fresh; cp "$C2/challenge.sig.der" "$C2/response.sig.der"; resum "$C2"
82
+ expect_fail "response.sig.der swapped for challenge.sig.der (resummed)" sh
83
+ fresh; printf '\377' | dd of="$C2/response.cipher_receiver.bin" bs=1 seek=40 conv=notrunc 2>/dev/null; resum "$C2"
84
+ expect_fail "ciphertext byte flipped (resummed)" sh
85
+ fresh; cp "$root/v/mdip-challenge-1/verifier-agent.pub.pem" "$root/v/kav-1/pub.pem"; resum "$root/v/kav-1"
86
+ expect_fail "kav-1 pub.pem replaced (resummed)" sh
87
+ fresh; sub "$root/v/kav-1/signature.hex" 's/^2b/2c/'; resum "$root/v/kav-1"
88
+ expect_fail "kav-1 signature.hex no longer matches signature.der (resummed)" sh
89
+ fresh; sub "$root/v/expiry-cases.txt" 's/| reject | 2 h + 1 ms later/| accept | 2 h + 1 ms later/'
90
+ expect_fail "expiry table: 2 h + 1 ms later marked accept" sh
91
+ expect_fail "expiry table: 2 h + 1 ms later marked accept" js
92
+ fresh; sub "$root/v/expiry-cases.txt" 's/| reject | 2099 (forged far future)/| accept | 2099 (forged far future)/'
93
+ expect_fail "expiry table: 2099 response marked accept" sh
94
+ expect_fail "expiry table: 2099 response marked accept" js
95
+ fresh; sub "$root/v/expiry-cases.txt" 's/| accept | stock keymaster skew/| reject | stock keymaster skew/'
96
+ expect_fail "expiry table: stock keymaster skew marked reject" sh
97
+ expect_fail "expiry table: stock keymaster skew marked reject" js
98
+ fresh; sub "$root/v/expiry-cases.txt" 's/| reject | leap second/| accept | leap second/'
99
+ expect_fail "expiry table: leap second marked accept" sh
100
+ expect_fail "expiry table: leap second marked accept" js
101
+ fresh; sub "$C2/challenge.op.json" 's/"validUntil": "2026-10-26T17:52:16.038Z"/"validUntil": "2026-10-27T17:52:16.038Z"/'
102
+ expect_fail "challenge.op.json expiry changed" js
103
+ fresh; node -e 'const f=process.argv[1],o=JSON.parse(require("fs").readFileSync(f));o.signature.signer=o.controller="did:mdip:z3v8AuafFPtizoWoRCuV71sNVen1MUCrFuvfFNjhrYmasfnEFy6";require("fs").writeFileSync(f,JSON.stringify(o,null,2)+"\n")' "$C2/challenge.op.json"
104
+ expect_fail "challenge.op.json controller/signer swapped to responder" js
105
+
106
+ fresh; sub "$C2/dids.json" 's/"response": "did:mdip:z/"response": "did:mdip:z1/'; resum "$C2"
107
+ both "response DID with a leading base58 1 (extra 0x00 byte) [accepted by c41252f's verify-openssl.sh]"
108
+ fresh; sub "$C1/dids.json" 's/"challenge": "did:mdip:z/"challenge": "did:mdip:z1/'; resum "$C1"
109
+ both "mdip-challenge-1 challenge DID with a leading base58 1"
110
+ fresh; jedit "$C1/dids.json" '[o.challenge,o.response]=[o.response,o.challenge]'
111
+ both "mdip-challenge-1 dids.json challenge/response swapped [accepted by c41252f's verify-openssl.sh]"
112
+ fresh; jedit "$C1/response.op.json" 'o.created="2026-10-26T15:19:54.000Z"'
113
+ both "mdip-challenge-1 response.op.json edited (unsigned copy) [accepted by c41252f's verify-openssl.sh]"
114
+ fresh; jedit "$C2/challenge.op.json" 'o.mdip.validUntil="2099-01-01T00:00:00.000Z"'
115
+ both "mdip-challenge-2 challenge.op.json edited (unsigned copy)"
116
+ fresh; jedit "$C1/verify-response.json" 'o.match=false'
117
+ both "mdip-challenge-1 verify-response.json match=false [accepted by c41252f's verify-openssl.sh]"
118
+ fresh; jedit "$C2/verify-response.json" 'o.extra=1'
119
+ both "mdip-challenge-2 verify-response.json extra field"
120
+ fresh; jedit "$C2/verify-response.json" 'o.responseNonce="x"+o.responseNonce.slice(1)'
121
+ both "mdip-challenge-2 verify-response.json nonce changed"
122
+ fresh; jedit "$C2/test-verifier.json" 'o.privateJwk.d="A"+o.privateJwk.d.slice(1)'
123
+ both "test-verifier.json private key changed [accepted by c41252f's verify-openssl.sh]"
124
+ fresh; jedit "$C2/test-verifier.json" 'const w=o.mnemonic.split(" ");[w[0],w[1]]=[w[1],w[0]];o.mnemonic=w.join(" ")'
125
+ expect_fail "test-verifier.json mnemonic words swapped (BIP32 check is verify-with-kit.js only)" js
126
+ fresh; jedit "$C2/test-verifier.json" 'o.did=o.did.replace("z3v8Auab","z3v8Auac")'
127
+ both "test-verifier.json DID changed"
128
+ fresh; jedit "$K/signed.json" 'o.message="Known-answer vector, edited."'
129
+ both "kav-1 signed.json message edited (unsigned copy) [accepted by c41252f's verify-openssl.sh]"
130
+ fresh; jedit "$K/signed.json" 'o.signature.extra=1'
131
+ both "kav-1 signed.json extra key in signature block"
132
+ fresh; jedit "$K/payload.json" 'o.nonce="00"'
133
+ both "kav-1 payload.json edited [accepted by c41252f's verify-openssl.sh]"
134
+ fresh; jedit "$K/test-key.json" 'o.privateKeyHex="00"+o.privateKeyHex.slice(2)'
135
+ both "kav-1 test-key.json private key changed [accepted by c41252f's verify-openssl.sh]"
136
+ fresh; jedit "$K/test-key.json" 'o.label+="!"'
137
+ both "kav-1 test-key.json label no longer derives the key"
138
+ fresh; : > "$K/.hidden"; expect_fail "unlisted dotfile [accepted by c41252f]" sh
139
+ fresh; mv "$K/pub.pem" "$root/pub.pem"; ln -s "$root/pub.pem" "$K/pub.pem"; expect_fail "listed file replaced by a symlink (same bytes) [accepted by c41252f]" sh
140
+ fresh; tail -1 "$K/SHA256SUMS" >> "$K/SHA256SUMS"; expect_fail "SHA256SUMS lists a file twice" sh
141
+ fresh; sub "$C2/response.plaintext.json" 's/{"response":{/{"response":{"challenge":"did:mdip:zX",/'; resum "$C2"
142
+ both "response.plaintext.json with a duplicate challenge key (jq/sed parse differential)"
143
+ expect_fail "response.plaintext.json with a duplicate challenge key" shnojq
144
+
145
+ # --- 0.1.5 crypto/binding review: forgeries that a pre-review checker accepted ---
146
+ # high-S twin (r, n-s): OpenSSL verifies it, MDIP/noble does not
147
+ highs() { node -e 'const fs=require("fs"),f=process.argv[1],d=fs.readFileSync(f),n=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141n;let i=2;const rd=()=>{i++;const l=d[i++];const v=d.subarray(i,i+l);i+=l;return BigInt("0x"+v.toString("hex"))};const r=rd(),s=n-rd();const e=x=>{let h=x.toString(16);if(h.length%2)h="0"+h;let b=Buffer.from(h,"hex");if(b[0]&128)b=Buffer.concat([Buffer.from([0]),b]);return Buffer.concat([Buffer.from([2,b.length]),b])};const body=Buffer.concat([e(r),e(s)]);fs.writeFileSync(f,Buffer.concat([Buffer.from([48,body.length]),body]));console.log(r.toString(16).padStart(64,"0")+s.toString(16).padStart(64,"0"))' "$1"; }
148
+ fresh; highs "$root/v/kav-1/signature.der" > "$root/v/kav-1/signature.hex"; resum "$root/v/kav-1"
149
+ expect_fail "kav-1 high-S twin signature (der + hex, resummed) [accepted by c41252f]" sh
150
+ fresh; highs "$root/v/mdip-challenge-1/response.sig.der" >/dev/null; resum "$root/v/mdip-challenge-1"
151
+ expect_fail "mdip-challenge-1 response.sig.der high-S twin (resummed) [accepted by c41252f]" sh
152
+ expect_fail "mdip-challenge-1 response.sig.der high-S twin (resummed) [accepted by c41252f]" js
153
+ fresh; highs "$C2/response.sig.der" >/dev/null; resum "$C2"
154
+ expect_fail "mdip-challenge-2 response.sig.der high-S twin (resummed) [js accepted at c41252f]" js
155
+ # SHA256SUMS whose last line has no newline: the old loop never hash-checked that entry
156
+ fresh; f=$root/v/mdip-challenge-1/SHA256SUMS; printf 'x' >> "$root/v/mdip-challenge-1/verify-response.json"
157
+ { grep -v ' verify-response.json$' "$f"; printf '%064d verify-response.json' 0; } > "$f.tmp" && mv "$f.tmp" "$f"
158
+ expect_fail "SHA256SUMS last line unterminated, wrong hash for a changed file [accepted by c41252f]" sh
159
+ fresh; ln -s ../kav-1/pub.pem "$root/v/mdip-challenge-1/extra.pem"; resum "$root/v/mdip-challenge-1"
160
+ expect_fail "symlink inside a vector dir (resummed) [accepted by c41252f]" sh
161
+ fresh; sub "$root/v/kav-1/SHA256SUMS" 's/ message.bin$/ .\/message.bin/'; expect_fail "SHA256SUMS entry with a path, not a plain name" sh
162
+ # kav-1 re-pointed at the OTHER public key that also verifies (r,s) (ECDSA key recovery, recovery bit flipped)
163
+ if [ $havekit = 1 ]; then fresh; node --input-type=module -e '
164
+ import fs from "node:fs"; import crypto from "node:crypto"; import { secp256k1 } from "@noble/curves/secp256k1.js";
165
+ const [d] = process.argv.slice(1), pem = d + "/pub.pem", h = crypto.createHash("sha256").update(fs.readFileSync(d + "/message.bin")).digest();
166
+ const orig = crypto.createPublicKey(fs.readFileSync(pem, "utf8")).export({ format: "jwk" });
167
+ const b = x => Buffer.from(x.toString(16).padStart(64, "0"), "hex").toString("base64url");
168
+ for (const bit of [0, 1]) { const a = secp256k1.Signature.fromBytes(Buffer.from(fs.readFileSync(d + "/signature.hex", "utf8").trim(), "hex"), "compact").addRecoveryBit(bit).recoverPublicKey(h).toAffine();
169
+ if (b(a.x) !== orig.x) { fs.writeFileSync(pem, crypto.createPublicKey({ key: { kty: "EC", crv: "secp256k1", x: b(a.x), y: b(a.y) }, format: "jwk" }).export({ type: "spki", format: "pem" })); break; } }' "$root/v/kav-1"
170
+ openssl dgst -sha256 -verify "$root/v/kav-1/pub.pem" -signature "$root/v/kav-1/signature.der" "$root/v/kav-1/message.bin" >/dev/null 2>&1 || { echo "FAIL recovered-key setup"; fail=1; }
171
+ resum "$root/v/kav-1"
172
+ expect_fail "kav-1 pub.pem = the other recovered key that also verifies the signature (resummed) [accepted by c41252f]" sh
173
+ expect_fail "kav-1 pub.pem = the other recovered key that also verifies the signature (resummed) [accepted by c41252f]" js; fi
174
+ # kav-1 regenerated wholesale with a fresh key (message.bin unchanged, new pub.pem/sig.der/sig.hex, resummed)
175
+ fresh; k=$root/k.pem; openssl ecparam -name secp256k1 -genkey -noout -out "$k" 2>/dev/null
176
+ openssl pkey -in "$k" -pubout -out "$root/v/kav-1/pub.pem"; openssl dgst -sha256 -sign "$k" -out "$root/v/kav-1/signature.der" "$root/v/kav-1/message.bin"
177
+ openssl asn1parse -inform DER -in "$root/v/kav-1/signature.der" | sed -n 's/.*INTEGER *://p' | while read -r i; do printf %064s "$i" | tr ' A-F' '0a-f'; done > "$root/v/kav-1/signature.hex"; echo >> "$root/v/kav-1/signature.hex"; resum "$root/v/kav-1"
178
+ expect_fail "kav-1 re-signed with a fresh key (pub.pem, sig.der, hex, resummed) [accepted by c41252f]" sh
179
+ # mdip-challenge-1 has no public key, so a swapped dids.json was only caught via the controller fields
180
+ fresh; sub "$root/v/mdip-challenge-1/dids.json" 's/"response": "did:mdip:z3v8AuaXWLR/"response": "did:mdip:z3v8AuaXWLS/'; resum "$root/v/mdip-challenge-1"
181
+ expect_fail "mdip-challenge-1 dids.json response DID changed (resummed) [accepted by c41252f]" sh
182
+ expect_fail "mdip-challenge-1 dids.json response DID changed (resummed) [accepted by c41252f]" js
183
+ fresh; printf '\n\n' >> "$C2/response.plaintext.json"; resum "$C2"
184
+ expect_fail "response.plaintext.json with extra trailing newlines (resummed) [sh accepted at c41252f]" sh
185
+ fresh; sub "$root/v/kav-1/message-tampered.bin" 's/Known-answer/Unknown-answer/; s/Non-emptY/Non-empty/'; resum "$root/v/kav-1"
186
+ expect_fail "message-tampered.bin is not a one-byte change (resummed) [accepted by c41252f]" sh
187
+ expect_fail "message-tampered.bin is not a one-byte change [accepted by c41252f]" js
188
+ fresh; node -e 'const f=process.argv[1],o=JSON.parse(require("fs").readFileSync(f));o.privateJwk.d="AAAA"+o.privateJwk.d.slice(4);require("fs").writeFileSync(f,JSON.stringify(o,null,2)+"\n")' "$C2/test-verifier.json"
189
+ expect_fail "test-verifier.json privateJwk.d altered" js
190
+ fresh; sub "$root/v/mdip-challenge-1/response.op.json" 's/"signed": "2026/"signed": "2027/'
191
+ expect_fail "mdip-challenge-1 response.op.json signature.signed changed [accepted by c41252f]" js
192
+
193
+
194
+ # ---- reviewer A (0.1.5): every human-readable file must equal what the signed / derived bytes say. Each
195
+ # forgery below keeps the generator layout (sorted keys, 2-space indent) and is resummed, so only the new
196
+ # exact-content checks can catch it. [accepted by 29b81bb] = both checkers passed it at 29b81bb; [sh/js ...] = that one did;
197
+ # untagged = already rejected at 29b81bb (kept so a regression is caught).
198
+ # jmut FILE JS: edit parsed JSON `o` in place, rewrite in generator layout, resum the dir
199
+ jmut() { node -e 'const fs=require("fs"),f=process.argv[1];let o=JSON.parse(fs.readFileSync(f,"utf8"));eval(process.argv[2]);const S=v=>Array.isArray(v)?v.map(S):v&&typeof v==="object"?Object.fromEntries(Object.keys(v).sort().map(k=>[k,S(v[k])])):v;fs.writeFileSync(f,JSON.stringify(S(o),null,2)+"\n")' "$1" "$2" && resum "$(dirname "$1")"; }
200
+ fresh; jmut "$C2/response.op.json" 'o.mdip.validUntil="2099-01-01T00:00:00.000Z"'; both "mdip-challenge-2 response.op.json validUntil 2099 (resummed)"
201
+ fresh; jmut "$C1/challenge.op.json" 'o.mdip.validUntil="2099-01-01T00:00:00.000Z"'; both "mdip-challenge-1 challenge.op.json validUntil 2099 (resummed)"
202
+ fresh; jmut "$C1/response.op.json" 'o.signature.signer=o.controller.replace(/.$/,"x")'; both "mdip-challenge-1 response.op.json signature.signer changed (resummed)"
203
+ fresh; jmut "$C2/challenge.op.json" 'o.extra=1'; both "mdip-challenge-2 challenge.op.json extra member (resummed)"
204
+ fresh; jmut "$C1/verify-response.json" 'o.match=false'; both "mdip-challenge-1 verify-response.json match=false (resummed)"
205
+ fresh; jmut "$C2/verify-response.json" 'o.match=false'; both "mdip-challenge-2 verify-response.json match=false (resummed)"
206
+ fresh; jmut "$C2/verify-response.json" 'o.responseNonce="x"+o.responseNonce.slice(1)'; both "mdip-challenge-2 verify-response.json responseNonce != plaintext (resummed)"
207
+ fresh; jmut "$C2/verify-response.json" 'o.responder=o.challenge'; both "mdip-challenge-2 verify-response.json responder changed (resummed)"
208
+ fresh; jmut "$C1/dids.json" 'o.extra="did:mdip:z1"'; both "mdip-challenge-1 dids.json extra member (resummed)"
209
+ fresh; jmut "$C2/test-verifier.json" 'o.privateJwk.d="AAAA"+o.privateJwk.d.slice(4)'; both "test-verifier.json privateJwk.d altered, layout kept (resummed)"
210
+ fresh; jmut "$C2/test-verifier.json" 'o.mnemonic=o.mnemonic.replace("two ","ten ")'; both "test-verifier.json mnemonic changed (resummed) [sh accepted by 29b81bb]"
211
+ fresh; jmut "$C2/test-verifier.json" 'o.derivationPath="m/44'"'"'/0'"'"'/0'"'"'/0/1"'; both "test-verifier.json derivationPath changed (resummed)"
212
+ fresh; jmut "$K/signed.json" 'o.signature.signer="did:example:mallory"'; both "kav-1 signed.json signature.signer changed (resummed)"
213
+ fresh; jmut "$K/signed.json" 'o.signature.signed="2027-01-01T00:00:00.000Z"'; both "kav-1 signed.json signature.signed changed (resummed) [accepted by 29b81bb]"
214
+ fresh; jmut "$K/signed.json" 'o.message="pay 1000"'; both "kav-1 signed.json message changed (resummed)"
215
+ fresh; jmut "$K/payload.json" 'o.message="pay 1000"'; both "kav-1 payload.json message changed (resummed)"
216
+ fresh; jmut "$K/test-key.json" 'o.privateKeyHex="00"+o.privateKeyHex.slice(2)'; both "kav-1 test-key.json privateKeyHex changed (resummed)"
217
+ fresh; jmut "$K/test-key.json" 'o.WARNING="safe to use"'; both "kav-1 test-key.json WARNING changed (resummed) [accepted by 29b81bb]"
218
+ fresh; jmut "$C2/test-verifier.json" 'o.WARNING="safe to use"'; both "test-verifier.json WARNING changed (resummed) [accepted by 29b81bb]"
219
+ fresh; printf '\n' >> "$K/signature.hex"; resum "$K"; both "kav-1 signature.hex with an extra newline (resummed) [accepted by 29b81bb]"
220
+ fresh; printf '\n' >> "$K/message.sha256"; resum "$K"; both "kav-1 message.sha256 with an extra newline (resummed) [accepted by 29b81bb]"
221
+ fresh; printf 'junk\n' >> "$C1/responder-agent.pub.pem"; resum "$C1"; both "pub.pem with trailing junk (resummed) [accepted by 29b81bb]"
222
+ fresh; printf 'junk\n' >> "$C2/verifier-agent.PUBLIC-TEST-KEY.priv.pem"; resum "$C2"; both "PUBLIC-TEST-KEY.priv.pem with trailing junk (resummed) [accepted by 29b81bb]"
223
+ fresh; node -e 'const fs=require("fs"),f=process.argv[1],o=JSON.parse(fs.readFileSync(f,"utf8"));fs.writeFileSync(f,JSON.stringify(o,Object.keys(o).reverse(),2)+"\n")' "$C1/dids.json"; resum "$C1"
224
+ both "dids.json same members, keys not in JCS order (resummed) [js accepted by 29b81bb]"
225
+ fresh; sub "$C2/verify-response.json" 's/^}$/,"vps":[]}/'; resum "$C2"; both "verify-response.json duplicate key appended (resummed) [js accepted by 29b81bb]"
226
+ # verify-with-kit.js now enforces SHA256SUMS itself (it never read it before)
227
+ fresh; : > "$C2/.hidden"; expect_fail "unlisted dotfile [js accepted by 29b81bb]" js
228
+ fresh; printf 'x' >> "$C1/challenge.message.bin"; expect_fail "file changed, SHA256SUMS not updated" js
229
+ fresh; sed -i.bak '$d' "$K/SHA256SUMS"; rm -f "$K/SHA256SUMS.bak"; expect_fail "SHA256SUMS missing an entry [js accepted by 29b81bb]" js
230
+ fresh; tail -1 "$K/SHA256SUMS" >> "$K/SHA256SUMS"; expect_fail "SHA256SUMS duplicate entry [js accepted by 29b81bb]" js
231
+ echo "$n mutations"
232
+ [ $fail = 0 ] && echo "ALL MUTATIONS REJECTED" || { echo "MUTATION TEST FAILED"; exit 1; }
package/vectors/README.md CHANGED
@@ -6,15 +6,26 @@ main README under "Known-answer vectors (verify with stock OpenSSL, no install)"
6
6
  | Path | What it is |
7
7
  |---|---|
8
8
  | `kav-1/` | Synthetic vector: public test key, non-empty JCS payload, compact + DER signature, tampered negative case |
9
- | `mdip-challenge-1/` | Real MDIP create operations for verifier agent, responder agent, challenge and response, plus the verifier's decrypted `verifyResponse` result |
9
+ | `mdip-challenge-1/` | Real MDIP create operations for verifier agent, responder agent, challenge and response, plus the verifier's decrypted `verifyResponse` result (recorded; not re-derivable offline). Also `*.signed-op.bin` so each DID can be recomputed from its op |
10
10
  | `mdip-challenge-2/` | Real challenge -> response whose **verifier is a PUBLIC TEST identity** (mnemonic + private key published), so the encrypted response can be decrypted offline and its `challenge` field checked against the challenge DID. Also `*.signed-op.bin` so each DID can be recomputed from its op. Suggested by @homies |
11
- | `expiry-cases.txt` | Strict-expiry table: response.validUntil must be <= challenge.validUntil (equal allowed, +1 ms rejected) |
11
+ | `expiry-cases.txt` | Expiry table (`challenge \| response \| expect \| note`): response.validUntil must be <= challenge.validUntil + 2 h (stock keymaster skew allowed, +2 h + 1 ns rejected), strict RFC 3339 grammar, fail closed |
12
12
  | `decrypt-openssl.sh` | Stock-OpenSSL decrypt of an MDIP encrypted message (secp256k1 ECDH + XChaCha20-Poly1305, tag checked) |
13
- | `verify-openssl.sh` | Install-free check (bash, sha256sum, openssl). Prints `ALL VECTORS OK` and exits 0 |
14
- | `verify-with-kit.js` | Same checks via `@mdip/cipher` (needs `npm install`) plus the JSON binding fields |
13
+ | `verify-openssl.sh` | Install-free check (bash 3.2+ and OpenSSL ≥ 3.0 only; on macOS set `OPENSSL` to Homebrew's openssl@3). Prints `ALL VECTORS OK` and exits 0 |
14
+ | `verify-with-kit.js` | Same checks via `@mdip/cipher` (needs `npm install`), plus re-deriving the PUBLIC TEST verifier key from its mnemonic (BIP32; the one check the shell script can't do) |
15
+ | `*/SHA256SUMS` | sha256 of every other file in the dir, one `<64 hex> <plain name>` per line; unlisted files, dotfiles, symlinks and duplicate lines are rejected (by both checkers) |
16
+ | `*.json` | Convenience copies, pretty-printed with keys in JCS order so `verify-openssl.sh` can tie each one to the signed bytes without jq. None is trusted on its own |
15
17
  | `generate.js` | Regenerates `kav-1/` deterministically |
16
18
  | `make-mdip-fixture.js` | Re-captures `mdip-challenge-1/` against a gatekeeper (publishes throwaway DIDs) |
17
19
  | `make-mdip-fixture-2.js` | Re-captures `mdip-challenge-2/` (publishes throwaway DIDs; verifier from the published label mnemonic) |
18
20
 
21
+ `test/vectors-negative.sh` (shipped in the package) checks that tampered copies of these vectors are rejected.
22
+
23
+ Both checkers pin the known answers (kav-1 hash, signature and key label; every DID in both
24
+ challenge dirs) and require low-S signatures. Re-capturing a challenge fixture creates new DIDs, so
25
+ update the pins in `verify-openssl.sh` and `verify-with-kit.js` when you do.
26
+
27
+ MDIP does not sign `signature.signed`, so changing it yields a different, still-"valid" DID for the
28
+ same signed content. The checkers pin it for these fixtures; see the main README.
29
+
19
30
  No real secrets are in here. `kav-1/test-key.json`, `mdip-challenge-2/test-verifier.json` and
20
31
  `mdip-challenge-2/verifier-agent.PUBLIC-TEST-KEY.priv.pem` are test keys published on purpose. Never use them.
@@ -9,9 +9,16 @@
9
9
  # with nonce 00000000||nonce[16:24]. HChaCha20 is recovered from one raw ChaCha20 block
10
10
  # (keystream minus the initial state words 0-3 and 12-15).
11
11
  # Usage: decrypt-openssl.sh <receiver-priv.pem> <sender-pub.pem> <blob.bin> -> plaintext on stdout
12
- # Exit 0 only if the Poly1305 tag verifies.
12
+ # Exit 0 only if the Poly1305 tag verifies; 1 if it does not; 2 on bad usage/input or no OpenSSL 3.
13
+ # Needs OpenSSL >= 3.0 (`openssl mac`); set OPENSSL=/path/to/openssl to pick one (macOS /usr/bin/openssl is LibreSSL).
13
14
  set -euo pipefail
15
+ openssl() { command "${OPENSSL:-openssl}" "$@"; }
16
+ die() { echo "decrypt-openssl.sh: $*" >&2; exit 2; }
17
+ [ $# -eq 3 ] || die "usage: decrypt-openssl.sh <receiver-priv.pem> <sender-pub.pem> <blob.bin>"
14
18
  priv=$1 pub=$2 blob=$3
19
+ for f in "$priv" "$pub" "$blob"; do [ -f "$f" ] && [ -r "$f" ] || die "cannot read $f"; done
20
+ # LibreSSL (macOS /usr/bin/openssl) has no `openssl mac`; without this check every tag would just "fail"
21
+ case "$(openssl version 2>/dev/null)" in OpenSSL\ [3-9]*) ;; *) die "need OpenSSL >= 3.0 (have: $(openssl version 2>/dev/null || echo none)); macOS: OPENSSL=\$(brew --prefix openssl@3)/bin/openssl";; esac
15
22
  hex() { od -An -v -tx1 | tr -d ' \n'; }
16
23
  unhex() { printf "$(printf '%s' "$1" | sed 's/../\\x&/g')"; }
17
24
  le32() { local h=$1; echo $((16#${h:6:2}${h:4:2}${h:2:2}${h:0:2})); }
@@ -19,7 +26,9 @@ tole32(){ printf '%02x%02x%02x%02x' $(($1&255)) $((($1>>8)&255)) $((($1>>16)&255
19
26
  le64() { local n=$1 o= i; for i in 0 1 2 3 4 5 6 7; do o+=$(printf '%02x' $(((n>>(8*i))&255))); done; echo "$o"; }
20
27
  ks() { head -c "$3" /dev/zero | openssl enc -chacha20 -K "$1" -iv "$2" | hex; } # raw keystream
21
28
 
22
- B=$(hex < "$blob"); N=${B:0:48}; CT=${B:48:$((${#B}-48-32))}; TAG=${B: -32}
29
+ B=$(hex < "$blob")
30
+ [ ${#B} -ge 80 ] || die "$blob is $((${#B}/2)) bytes; a blob is at least nonce(24) + tag(16) = 40 bytes"
31
+ N=${B:0:48}; CT=${B:48:$((${#B}-48-32))}; TAG=${B: -32}
23
32
  X=$(openssl pkeyutl -derive -inkey "$priv" -peerkey "$pub" | hex)
24
33
  tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
25
34
  unhex "$CT" > "$tmp/ct"
@@ -1,15 +1,64 @@
1
- # Strict-expiry rule (intended semantics, enforced by mdip-agent-kit >= 0.1.3 verifyResponse):
2
- # a response MUST expire no later than the challenge it answers: response.validUntil <= challenge.validUntil
3
- # equality is ALLOWED (the kit caps a response's expiry at exactly the challenge's validUntil);
4
- # one millisecond later is REJECTED. A missing response validUntil is REJECTED when the challenge has one.
5
- # Separately, at verify time both must still be in the future (now < validUntil); that needs a clock, not a vector.
6
- # Timestamps are ISO-8601 UTC with milliseconds (what MDIP writes), so a plain string compare is exact.
7
- # challenge.validUntil response.validUntil expected
8
- 2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.930Z accept equal (mdip-challenge-1/-2 are this case)
9
- 2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.929Z accept 1 ms earlier
10
- 2026-10-26T16:41:19.930Z 2026-10-19T16:41:19.930Z accept a week earlier
11
- 2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.931Z reject 1 ms later
12
- 2026-10-26T16:41:19.930Z 2026-10-26T17:41:19.930Z reject 1 h later (keymaster's raw default, uncapped)
13
- 2026-10-26T16:41:19.930Z 2027-01-01T00:00:00.000Z reject next year
14
- 2026-10-26T16:41:19.930Z - reject response has no validUntil
15
- - 2026-10-26T16:41:19.930Z accept challenge has no validUntil (nothing to cap)
1
+ # Expiry rule (intended semantics, enforced by mdip-agent-kit verifyResponse; relaxed in 0.1.5 for stock keymaster):
2
+ # a response MUST expire no more than 2 h after the challenge it answers:
3
+ # response.validUntil <= challenge.validUntil + 2 h (RESPONSE_EXPIRY_SKEW_MS)
4
+ # Stock @mdip/keymaster 2.2.0 sets challenge.validUntil = created + 1 h and response.validUntil = responded + 1 h,
5
+ # so its responses expire a few seconds to ~1 h AFTER the challenge (2 h covers its local-time setHours across a
6
+ # DST fall-back). Those are ACCEPTED; this kit's own respond still caps at exactly the challenge's validUntil.
7
+ # 2 h + 1 ns later is REJECTED, as are far-future responses. A missing response validUntil is REJECTED when the
8
+ # challenge has one.
9
+ # Separately, at verify time BOTH must still be in the future (now < validUntil); that needs a clock, not a vector,
10
+ # and is what actually bounds when a response can be accepted (verify-with-kit.js checks it with fixed `now`s).
11
+ # Timestamps: strict RFC 3339 (MDIP writes UTC with ms): YYYY-MM-DDTHH:MM:SS[.1-9 digits](Z|+hh:mm|-hh:mm),
12
+ # uppercase T and Z, a real calendar date, hour 00-23, min/sec 00-59. Compared EXACTLY (to the nanosecond) as an
13
+ # instant, never as strings. Anything else is rejected (fail closed), identically in verify-openssl.sh (pure bash)
14
+ # and checkResponseExpiry/parseTimestampNs (JS). A malformed response validUntil is rejected even when the
15
+ # challenge has none.
16
+ # Columns are separated by "|" (a timestamp may itself contain spaces); surrounding spaces are trimmed; "-" = absent.
17
+ # challenge.validUntil | response.validUntil | expect | note
18
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930Z | accept | equal (mdip-challenge-1/-2 are this case)
19
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.929Z | accept | 1 ms earlier
20
+ 2026-10-26T16:41:19.930Z | 2026-10-19T16:41:19.930Z | accept | a week earlier
21
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.931Z | accept | 1 ms later (within the 2 h skew)
22
+ 2026-10-26T16:41:19.930Z | 2026-10-26T17:41:19.930Z | accept | 1 h later (keymaster's raw default, uncapped)
23
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:24.112Z | accept | stock keymaster skew: response 4.182 s after the challenge
24
+ 2026-10-26T16:41:19.930Z | 2026-10-26T18:41:19.930Z | accept | exactly 2 h later (skew bound, equal allowed)
25
+ 2026-10-26T16:41:19.930Z | 2026-10-26T18:41:19.931Z | reject | 2 h + 1 ms later
26
+ 2026-10-26T16:41:19.930Z | 2026-10-26T18:41:19.930000001Z | reject | 2 h + 1 ns later (exact, not truncated)
27
+ 2026-10-26T16:41:19.930Z | 2026-10-26T18:41:19.930-00:01 | reject | 2 h + 1 min later written with an offset
28
+ 2026-10-26T16:41:19.930Z | 2026-10-26T18:41:19.9305Z | reject | 2 h + 0.5 ms later (sub-ms compared, not truncated)
29
+ 2026-10-26T16:41:19.930Z | 2026-10-26T19:41:19.930+01:00 | accept | exactly 2 h later written with an offset
30
+ 2026-10-26T16:41:19.930Z | 2099-01-01T00:00:00Z | reject | 2099 (forged far future)
31
+ 2026-10-26T16:41:19.930Z | 2027-01-01T00:00:00.000Z | reject | next year (far future)
32
+ 2026-10-26T16:41:19.930Z | - | reject | response has no validUntil
33
+ - | 2026-10-26T16:41:19.930Z | accept | challenge has no validUntil (nothing to cap)
34
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19Z | accept | response without ms (0.930 s earlier)
35
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:20Z | accept | response without ms, 70 ms later (within skew)
36
+ 2026-10-26T16:41:19.930Z | 2026-10-26T09:41:19.930-07:00 | accept | same instant written with an offset
37
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930 | reject | response with no timezone (fail closed)
38
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.9305Z | accept | 0.5 ms later (within skew)
39
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930z | reject | lowercase z (RFC 3339 allows it; we fail closed)
40
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.9295Z | accept | 0.5 ms earlier (sub-ms kept, not rejected or truncated)
41
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930000001Z | accept | 1 ns later (9 fraction digits, within skew)
42
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.9300000000Z | reject | 10 fraction digits (unparseable)
43
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930+0000 | reject | offset without colon (RFC 3339 needs +hh:mm)
44
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930-00:00 | accept | -00:00 is the same UTC instant
45
+ 2026-10-27T01:41:19.930+09:00 | 2026-10-26T16:41:19.930Z | accept | challenge with offset, same instant (date rolls over)
46
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930+23:59 | accept | extreme offset, ~1 day earlier
47
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19.930+24:00 | reject | offset hour 24 (out of range)
48
+ 2026-10-26T16:41:19.930Z | 2026-10-26 16:41:19.930Z | reject | space instead of T
49
+ 2026-10-26T16:41:19.930Z | 2026-10-26t16:41:19.930Z | reject | lowercase t
50
+ 2026-10-26T16:41:19.930Z | 2026-10-26T16:41:19,930Z | reject | comma decimal
51
+ 2026-12-31T23:59:59.999Z | 2026-12-31T23:59:60Z | reject | leap second :60 (fail closed)
52
+ 2026-12-31T23:59:59.999Z | 2026-12-31T24:00:00Z | reject | 24:00 (fail closed, not next midnight)
53
+ 2026-03-02T00:00:00Z | 2026-02-30T00:00:00Z | reject | Feb 30 (not rolled over to Mar 2)
54
+ 2027-01-01T00:00:00Z | 2026-00-15T00:00:00Z | reject | month 00
55
+ 2027-02-01T00:00:00Z | 2026-13-01T00:00:00Z | reject | month 13 (not rolled over to 2027-01)
56
+ 2026-12-01T00:00:00Z | 2026-11-31T00:00:00Z | reject | Nov 31
57
+ 2026-12-01T00:00:00Z | 2026-11-00T00:00:00Z | reject | day 00
58
+ 2028-03-01T00:00:00Z | 2028-02-29T23:59:59.999Z | accept | Feb 29 in a leap year
59
+ 2100-03-01T00:00:00Z | 2100-02-29T00:00:00Z | reject | Feb 29 in 2100 (not a leap year)
60
+ 2026-10-26T16:41:19.930Z | +002026-10-26T16:41:19.930Z | reject | expanded year
61
+ 2026-10-26T16:41:19.930Z | Mon, 26 Oct 2026 16:41:19 GMT | reject | RFC 2822 date
62
+ 2026-10-26T16:41:19.930z | 2026-10-26T16:41:19.930Z | reject | challenge with lowercase z
63
+ - | 2026-10-26T16:41:19.930 | reject | no challenge cap, but response has no timezone
64
+ - | - | accept | neither has validUntil
@@ -13,6 +13,10 @@ const dir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'kav-1');
13
13
  fs.mkdirSync(dir, { recursive: true });
14
14
  const cipher = new CipherNode();
15
15
  const w = (f, data) => fs.writeFileSync(path.join(dir, f), data);
16
+ // convenience JSON copies are written with keys in JCS order (2-space indent) so the pure-bash checker can
17
+ // compare them to the signed bytes: strip indentation, '": ' -> '":' and newlines == JCS
18
+ const sortKeys = v => Array.isArray(v) ? v.map(sortKeys) : v && typeof v === 'object' ? Object.fromEntries(Object.keys(v).sort().map(k => [k, sortKeys(v[k])])) : v;
19
+ const pretty = o => JSON.stringify(sortKeys(o), null, 2) + '\n';
16
20
 
17
21
  // PUBLIC TEST KEY. Derived from a published label so anyone can re-derive it. Never use it for anything real.
18
22
  const LABEL = 'mdip-agent-kit kav-1 PUBLIC TEST KEY - never use for anything real';
@@ -44,16 +48,17 @@ const body = Buffer.concat([int(r), int(s)]);
44
48
  const der = Buffer.concat([Buffer.from([0x30, body.length]), body]);
45
49
  if (!crypto.verify('sha256', Buffer.from(canonical, 'utf8'), { key: pub, dsaEncoding: 'der' }, der)) throw new Error('node DER check failed');
46
50
 
47
- w('test-key.json', JSON.stringify({ WARNING: 'PUBLIC TEST KEY - private part is published on purpose; never use it', label: LABEL, privateKeyHex: d.toString('hex'), publicJwk, privateJwk }, null, 2) + '\n');
51
+ w('test-key.json', pretty({ WARNING: 'PUBLIC TEST KEY - private part is published on purpose; never use it', label: LABEL, privateKeyHex: d.toString('hex'), publicJwk, privateJwk }));
48
52
  w('pub.pem', pub.export({ type: 'spki', format: 'pem' }));
49
- w('payload.json', JSON.stringify(payload, null, 2) + '\n');
53
+ w('payload.json', pretty(payload));
50
54
  w('message.bin', canonical); // exact signed-over bytes: JCS(payload), UTF-8, no trailing newline
51
55
  w('message.sha256', hash + '\n');
52
56
  w('signature.hex', value + '\n'); // what MDIP stores in signature.value (64-byte compact r||s)
53
57
  w('signature.der', der);
54
- w('signed.json', JSON.stringify(signed, null, 2) + '\n');
58
+ w('signed.json', pretty(signed));
55
59
  w('message-tampered.bin', canonical.replace('Non-empty', 'Non-emptY')); // negative case: must FAIL
56
- const sums = ['message.bin', 'message-tampered.bin', 'signature.der', 'pub.pem', 'signed.json']
60
+ // SHA256SUMS lists every file in the dir (byte-sorted); verify-openssl.sh rejects unlisted files
61
+ const sums = fs.readdirSync(dir).filter(f => f !== 'SHA256SUMS').sort()
57
62
  .map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(dir, f))).digest('hex') + ' ' + f).join('\n') + '\n';
58
63
  w('SHA256SUMS', sums);
59
64
  console.log(JSON.stringify({ dir, bytes: Buffer.byteLength(canonical), hash, value }, null, 2));
@@ -1,5 +1,9 @@
1
- 4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b message.bin
2
1
  17bb3f553bd7a600ad5585015f59c770278b599921eb52b9bfd51e16aa4de476 message-tampered.bin
3
- fdc8e19ec10ced978a9dbd5ae599c35fa715d561dd1635bb5cf0360e3acd4508 signature.der
2
+ 4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b message.bin
3
+ e126e80bfa4da6bc57f74953d1afc1b5c5bc947dc59fb41a00eb6cd888673c70 message.sha256
4
+ 6a90ab3fdc4b8c8b307817c80240752cbc02b1e62e46de6d0fbcdff772088948 payload.json
4
5
  43d15804c3379b189d1d1fbfb391d3c42abf739587297bdb9987eab382bcd131 pub.pem
5
- a1e3e79bd90f57d7ac991dbd040a436e3fab1bcae648c646536193397bb88c85 signed.json
6
+ fdc8e19ec10ced978a9dbd5ae599c35fa715d561dd1635bb5cf0360e3acd4508 signature.der
7
+ 30c8569a528c8273d0d1fafb886402833be8df717458b585fb8381e71d1c58de signature.hex
8
+ 9403033e8ba9ce8ca8dbab1ccc3e6a1545122f091b28da75eb343cbbcfb66469 signed.json
9
+ cea51407b5c54577b28a4c4b2de1037863987a57077f41fc90bd9ea0d6183ed8 test-key.json
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "kit": "mdip-agent-kit",
3
- "vector": "kav-1",
4
3
  "message": "Known-answer vector for MDIP signed JSON. Non-empty on purpose.",
5
4
  "nonce": "6b61762d312d6e6f6e63652d30303031",
6
- "verifier": "did:example:kav-1-verifier",
7
- "validUntil": "2026-12-31T23:59:59.000Z"
5
+ "validUntil": "2026-12-31T23:59:59.000Z",
6
+ "vector": "kav-1",
7
+ "verifier": "did:example:kav-1-verifier"
8
8
  }
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "kit": "mdip-agent-kit",
3
- "vector": "kav-1",
4
3
  "message": "Known-answer vector for MDIP signed JSON. Non-empty on purpose.",
5
4
  "nonce": "6b61762d312d6e6f6e63652d30303031",
6
- "verifier": "did:example:kav-1-verifier",
7
- "validUntil": "2026-12-31T23:59:59.000Z",
8
5
  "signature": {
9
- "signer": "did:example:kav-1-signer",
10
- "signed": "2026-10-26T00:00:00.000Z",
11
6
  "hash": "4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b",
7
+ "signed": "2026-10-26T00:00:00.000Z",
8
+ "signer": "did:example:kav-1-signer",
12
9
  "value": "2b61bc63136caf5dbbdfee80d6717d57df09555be0f6c1b0158adc70feb0363c2067a7017b2b52fd92fd0e980650118bf5d4e07bc88ee8527f04f6404a37b4d1"
13
- }
10
+ },
11
+ "validUntil": "2026-12-31T23:59:59.000Z",
12
+ "vector": "kav-1",
13
+ "verifier": "did:example:kav-1-verifier"
14
14
  }
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "WARNING": "PUBLIC TEST KEY - private part is published on purpose; never use it",
3
3
  "label": "mdip-agent-kit kav-1 PUBLIC TEST KEY - never use for anything real",
4
- "privateKeyHex": "c1433ed3bcadb796140c3bdbe694698f70ba1cf1f57f4e4e5ab789c22a2e3732",
5
- "publicJwk": {
6
- "kty": "EC",
4
+ "privateJwk": {
7
5
  "crv": "secp256k1",
6
+ "d": "wUM-07ytt5YUDDvb5pRpj3C6HPH1f05OWreJwiouNzI",
7
+ "kty": "EC",
8
8
  "x": "oHkFvCmbWtTbv23XnA3GxISuOAevKWhEqIc6gTrGz3s",
9
9
  "y": "f03etbi1OB3A5-7hizstgooDg8O51i7J3bpDY7iDZ8M"
10
10
  },
11
- "privateJwk": {
12
- "kty": "EC",
11
+ "privateKeyHex": "c1433ed3bcadb796140c3bdbe694698f70ba1cf1f57f4e4e5ab789c22a2e3732",
12
+ "publicJwk": {
13
13
  "crv": "secp256k1",
14
+ "kty": "EC",
14
15
  "x": "oHkFvCmbWtTbv23XnA3GxISuOAevKWhEqIc6gTrGz3s",
15
- "y": "f03etbi1OB3A5-7hizstgooDg8O51i7J3bpDY7iDZ8M",
16
- "d": "wUM-07ytt5YUDDvb5pRpj3C6HPH1f05OWreJwiouNzI"
16
+ "y": "f03etbi1OB3A5-7hizstgooDg8O51i7J3bpDY7iDZ8M"
17
17
  }
18
18
  }
@@ -21,6 +21,10 @@ fs.mkdirSync(out, { recursive: true });
21
21
  const cipher = new CipherNode();
22
22
  const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'kav2-fixture-'));
23
23
  const w = (f, data) => fs.writeFileSync(path.join(out, f), data);
24
+ // convenience JSON copies are written with keys in JCS order (2-space indent) so the pure-bash checker can
25
+ // compare them to the signed bytes: strip indentation, '": ' -> '":' and newlines == JCS
26
+ const sortKeys = v => Array.isArray(v) ? v.map(sortKeys) : v && typeof v === 'object' ? Object.fromEntries(Object.keys(v).sort().map(k => [k, sortKeys(v[k])])) : v;
27
+ const pretty = o => JSON.stringify(sortKeys(o), null, 2) + '\n';
24
28
  const der = hex => {
25
29
  const int = b => { let i = 0; while (i < b.length - 1 && b[i] === 0) i++; b = b.subarray(i); if (b[0] & 0x80) b = Buffer.concat([Buffer.from([0]), b]); return Buffer.concat([Buffer.from([0x02, b.length]), b]); };
26
30
  const body = Buffer.concat([int(Buffer.from(hex.slice(0, 64), 'hex')), int(Buffer.from(hex.slice(64), 'hex'))]);
@@ -49,13 +53,12 @@ try {
49
53
  w(`${k}.pub.pem`, crypto.createPublicKey({ key: { ...ops[k].publicJwk }, format: 'jwk' }).export({ type: 'spki', format: 'pem' }));
50
54
  const priv = crypto.createPrivateKey({ key: { ...vkp.privateJwk }, format: 'jwk' });
51
55
  w('verifier-agent.PUBLIC-TEST-KEY.priv.pem', priv.export({ type: 'pkcs8', format: 'pem' }));
52
- w('test-verifier.json', JSON.stringify({
56
+ w('test-verifier.json', pretty({
53
57
  WARNING: 'PUBLIC TEST VERIFIER - wallet mnemonic and private key are published on purpose; never use them',
54
58
  label: LABEL, mnemonicEntropy: 'first 16 bytes of sha256(label)', mnemonic,
55
59
  derivationPath: "m/44'/0'/0'/0/0", did: verifier.did, publicJwk: vkp.publicJwk, privateJwk: vkp.privateJwk,
56
- }, null, 2) + '\n');
60
+ }));
57
61
 
58
- const sums = [];
59
62
  for (const [k, op] of Object.entries(ops)) {
60
63
  const copy = JSON.parse(JSON.stringify(op)); delete copy.signature;
61
64
  const msg = cipher.canonicalizeJSON(copy);
@@ -63,12 +66,11 @@ try {
63
66
  if (hash !== op.signature.hash) throw new Error(`${k}: hash mismatch`);
64
67
  const signerKey = op.mdip.type === 'agent' ? op.publicJwk : ops[op.controller === verifier.did ? 'verifier-agent' : 'responder-agent'].publicJwk;
65
68
  if (!cipher.verifySig(hash, op.signature.value, signerKey)) throw new Error(`${k}: sig fails`);
66
- const full = cipher.canonicalizeJSON(op); // DID suffix = CIDv1(dag-json, sha256(JCS(signed op)))
67
- w(`${k}.op.json`, JSON.stringify(op, null, 2) + '\n');
69
+ const full = cipher.canonicalizeJSON(op); // DID suffix = CIDv1(json 0x0200, sha256(JCS(signed op)))
70
+ w(`${k}.op.json`, pretty(op));
68
71
  w(`${k}.message.bin`, msg);
69
72
  w(`${k}.sig.der`, der(op.signature.value));
70
73
  w(`${k}.signed-op.bin`, full);
71
- sums.push(`${k}.message.bin`, `${k}.sig.der`, `${k}.signed-op.bin`);
72
74
  }
73
75
  // The response ciphertext the verifier received, and the plaintext it decrypts to (exact bytes).
74
76
  const ct = ops.response.data.encrypted.cipher_receiver;
@@ -76,11 +78,11 @@ try {
76
78
  const plaintext = cipher.decryptMessage(ops['responder-agent'].publicJwk, vkp.privateJwk, ct);
77
79
  w('response.plaintext.json', plaintext);
78
80
  if (JSON.parse(plaintext).response.challenge !== challengeDid) throw new Error('binding mismatch');
79
- sums.push('response.cipher_receiver.bin', 'response.plaintext.json');
80
- w('dids.json', JSON.stringify(names, null, 2) + '\n');
81
- w('verify-response.json', JSON.stringify(result, null, 2) + '\n');
82
- sums.push('verifier-agent.pub.pem', 'responder-agent.pub.pem', 'verifier-agent.PUBLIC-TEST-KEY.priv.pem', 'dids.json');
83
- w('SHA256SUMS', sums.map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(out, f))).digest('hex') + ' ' + f).join('\n') + '\n');
81
+ w('dids.json', pretty(names));
82
+ w('verify-response.json', pretty(result));
83
+ // every file in the dir, byte-sorted (verify-openssl.sh rejects unlisted files)
84
+ w('SHA256SUMS', fs.readdirSync(out).filter(f => f !== 'SHA256SUMS').sort()
85
+ .map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(out, f))).digest('hex') + ' ' + f).join('\n') + '\n');
84
86
  console.log(JSON.stringify({ out, names, match: result.match, plaintext }, null, 2));
85
87
  } finally {
86
88
  fs.rmSync(tmp, { recursive: true, force: true });