provared 0.5.0 → 0.7.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.
package/src/check.d.ts CHANGED
@@ -5,9 +5,12 @@ export {
5
5
  checkShow,
6
6
  openChecker,
7
7
  checkSlip,
8
+ isPageAddress,
8
9
  methodAvailable,
9
10
  thumbprint,
10
11
  keySetFingerprint,
12
+ verifyConsistency,
13
+ verifyInclusion,
11
14
  blockFingerprint,
12
15
  checkHeaderChain,
13
16
  blocksInChain,
@@ -17,5 +20,6 @@ export {
17
20
  neverWords,
18
21
  LIMITS,
19
22
  REFUSAL_REASONS,
23
+ DENIAL_SOURCES,
20
24
  } from './types.js';
21
25
  export type * from './types.js';
package/src/check.js CHANGED
@@ -7,9 +7,11 @@
7
7
  export { checkBook, checkShow, openChecker, LIMITS } from './book.js';
8
8
  export { limitWords, conditionWords, neverWords } from './compare.js';
9
9
  export { REFUSAL_REASONS } from './service.js';
10
- export { checkSlip } from './slip.js';
10
+ export { DENIAL_SOURCES } from './denial.js';
11
+ export { checkSlip, isPageAddress } from './slip.js';
11
12
  export { keySetFingerprint } from './seal.js';
12
13
  export { blockFingerprint } from './blockstamp.js';
13
14
  export { checkHeaderChain, blocksInChain, MIN_BLOCKS_AFTER } from './headers.js';
14
15
  export { thumbprint } from './keys.js';
15
16
  export { methodAvailable } from './signatures.js';
17
+ export { verifyConsistency, verifyInclusion } from './tree.js';
package/src/denial.js ADDED
@@ -0,0 +1,123 @@
1
+ // The denial: the agent's own side states that it did not take an action
2
+ // that was asked for, and why. It is signed with the agent's two keys, or
3
+ // with the helper agent's under its pass. It is the agent's side's own
4
+ // word, and an agent can simply not write one.
5
+ // Format description, section 28.
6
+
7
+ import { formatTime, randomId } from './encoding.js';
8
+ import * as f from './fields.js';
9
+ import { KINDS } from './jws.js';
10
+ import { signWithKeySet, validateRequest } from './stub.js';
11
+
12
+ /** Who said no. */
13
+ export const DENIAL_SOURCES = {
14
+ check: 'The check before acting (section 20) did not allow the action.',
15
+ person: 'The person refused, or gave no approval when asked.',
16
+ rule: 'A rule of the software around the agent, outside the slip.',
17
+ };
18
+
19
+ /**
20
+ * The format's own list of finding codes: every code a checker or the check
21
+ * before acting gives (format description, section 28; the same list is in
22
+ * the list of threats, section 8). A denial whose source is "check" names
23
+ * its reasons from this list.
24
+ */
25
+ export const FINDING_CODES = Object.freeze([
26
+ 'not-json', 'bad-envelope', 'bad-base64url', 'too-large', 'unknown-type', 'bad-signatures-layout', 'payload-not-canonical',
27
+ 'payload-type-mismatch', 'bad-field', 'bad-key', 'signature-invalid', 'passkey-wrong-type', 'passkey-challenge-mismatch',
28
+ 'passkey-origin-mismatch', 'passkey-rpid-mismatch', 'passkey-user-not-present', 'passkey-user-not-verified', 'passkey-bad-data',
29
+ 'issuer-not-expected', 'issuer-origin-not-expected', 'slip-missing', 'slip-unusable', 'chain-broken', 'time-went-backwards', 'duplicate-id', 'duplicate-slip',
30
+ 'countersignature-invalid', 'countersignature-wrong-stub', 'countersignature-not-possible', 'line-not-canonical', 'unknown-entry',
31
+ 'approval-mismatch', 'approval-reused', 'approval-invalid', 'approval-not-found', 'approval-dated-after-stub', 'terms-not-found',
32
+ 'terms-unusable', 'terms-mismatch', 'pass-missing', 'pass-mismatch', 'pass-too-deep', 'cover-invalid', 'cancellation-invalid',
33
+ 'cancellation-not-found', 'acknowledgement-mismatch', 'vouching-missing', 'seal-mismatch', 'seal-chain-broken', 'sealer-not-expected', 'book-cut-short', 'seal-not-in-book', 'witness-invalid',
34
+ 'dated-after-stamp', 'stamp-bad-data', 'stamp-wrong-data', 'stamp-invalid', 'headers-invalid', 'record-not-sound', 'proof-invalid',
35
+ 'root-mismatch', 'check-failed', 'action-not-allowed', 'party-not-allowed', 'outside-valid-time', 'amount-missing', 'over-limit',
36
+ 'over-each-limit', 'over-count-limit', 'over-period-limit', 'countersignature-missing', 'approval-missing', 'prohibited',
37
+ 'pass-not-allowed', 'pass-wider', 'after-cancellation', 'outside-terms',
38
+ ]);
39
+
40
+ /** The most finding codes one denial may name. */
41
+ export const MAX_DENIAL_CODES = 16;
42
+
43
+ /**
44
+ * The reasons of a refusal by the check before acting, as a denial names
45
+ * them: the codes of its problems, then of its findings, each once, in that
46
+ * order, from the format's own list, at most 16. Where none is on the list,
47
+ * "check-failed".
48
+ * @param {{problems: {code: string}[], breaches: {code: string}[]}} answer the answer of the check before acting
49
+ * @returns {string[]}
50
+ */
51
+ export function denialCodes(answer) {
52
+ const codes = [];
53
+ for (const p of [...answer.problems, ...answer.breaches]) {
54
+ if (p && typeof p.code === 'string' && FINDING_CODES.includes(p.code) && !codes.includes(p.code)) codes.push(p.code);
55
+ }
56
+ return codes.length ? codes.slice(0, MAX_DENIAL_CODES) : ['check-failed'];
57
+ }
58
+
59
+ /**
60
+ * Confirm every member of a denial's content.
61
+ * @param {any} c
62
+ */
63
+ export function validateDenialContent(c) {
64
+ f.members(c, ['action', 'id', 'slip', 'source', 'type', 'when'], ['amount', 'codes', 'details', 'pass', 'with'], 'denial');
65
+ f.id(c.id, 'id');
66
+ f.fingerprintText(c.slip, 'slip');
67
+ if (Object.hasOwn(c, 'pass')) f.fingerprintText(c.pass, 'pass');
68
+ // What was asked for is written down as it was asked, as in a refusal: a
69
+ // reserved name that is not on the shared list may be why it was refused.
70
+ f.shortName(c.action, 'action');
71
+ // The amount, the service and the documents as in a stub; the service
72
+ // need not be one the slip names.
73
+ validateRequest({ ...c, action: 'x' });
74
+ if (typeof c.source !== 'string' || !Object.hasOwn(DENIAL_SOURCES, c.source)) throw f.fail('source', 'must be "check", "person" or "rule".');
75
+ if (c.source === 'check') {
76
+ if (!Object.hasOwn(c, 'codes')) throw f.fail('codes', 'is missing: a denial by the check before acting names its reasons.');
77
+ f.list(c.codes, 1, MAX_DENIAL_CODES, 'codes');
78
+ c.codes.forEach((code, i) => {
79
+ if (typeof code !== 'string' || !FINDING_CODES.includes(code)) throw f.fail(`codes[${i}]`, 'must be a code from the format\'s own list of finding codes.');
80
+ });
81
+ if (new Set(c.codes).size !== c.codes.length) throw f.fail('codes', 'a code is repeated.');
82
+ } else if (Object.hasOwn(c, 'codes')) {
83
+ throw f.fail('codes', 'only a denial by the check before acting names codes.');
84
+ }
85
+ f.time(c.when, 'when');
86
+ }
87
+
88
+ /**
89
+ * Write and sign a denial: the agent's side states that it did not take an
90
+ * action that was asked for, and why. It is signed with the keys of the
91
+ * agent the slip names or, where the signer is a helper agent, with the
92
+ * keys the pass names.
93
+ *
94
+ * @param {object} fields
95
+ * @param {string} fields.slip the fingerprint of the slip the agent acts under
96
+ * @param {string} [fields.pass] for a helper agent: the fingerprint of the pass it acts under
97
+ * @param {string} fields.action what was asked for
98
+ * @param {{unit: string, value: number}} [fields.amount]
99
+ * @param {string} [fields.with] the service asked for, as asked; it need not be one the slip names
100
+ * @param {{name: string, sha256: string}[]} [fields.details] fingerprints of documents, never the documents
101
+ * @param {'check'|'person'|'rule'} fields.source who said no
102
+ * @param {string[]} [fields.codes] where the source is "check": 1 to 16 finding codes, from FINDING_CODES
103
+ * @param {number|Date} [fields.when] defaults to now
104
+ * @param {any[]} privateKeys the agent's private keys, or signers: Ed25519, then ML-DSA-87
105
+ * @returns {Promise<{record: object, fingerprint: string}>}
106
+ */
107
+ export async function writeDenial(fields, privateKeys) {
108
+ const content = {
109
+ type: KINDS.denial.type,
110
+ id: fields.id ?? randomId(),
111
+ slip: fields.slip,
112
+ action: fields.action,
113
+ source: fields.source,
114
+ when: formatTime(fields.when ?? Date.now()),
115
+ };
116
+ if (fields.pass) content.pass = fields.pass;
117
+ if (fields.amount) content.amount = fields.amount;
118
+ if (fields.with) content.with = fields.with;
119
+ if (fields.details && fields.details.length) content.details = fields.details;
120
+ if (fields.codes !== undefined && fields.codes !== null) content.codes = fields.codes;
121
+ validateDenialContent(content);
122
+ return signWithKeySet('denial', content, privateKeys);
123
+ }
package/src/der.js CHANGED
@@ -81,6 +81,30 @@ export function contentOf(bytes, element) {
81
81
  return bytes.subarray(element.start, element.end);
82
82
  }
83
83
 
84
+ /**
85
+ * Whether the items of a set are in the order DER requires (X.690, section
86
+ * 11.6): their encodings compared as strings of bytes, the shorter padded
87
+ * at its end with zero bytes. Two equal encodings may stand side by side.
88
+ * @param {Uint8Array} bytes
89
+ * @param {{from: number, end: number}[]} items the elements of the set, in the order they appear
90
+ */
91
+ export function setInOrder(bytes, items) {
92
+ for (let i = 1; i < items.length; i++) {
93
+ const a = items[i - 1];
94
+ const b = items[i];
95
+ const longest = Math.max(a.end - a.from, b.end - b.from);
96
+ for (let k = 0; k < longest; k++) {
97
+ const x = a.from + k < a.end ? bytes[a.from + k] : 0;
98
+ const y = b.from + k < b.end ? bytes[b.from + k] : 0;
99
+ if (x !== y) {
100
+ if (x > y) return false;
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ return true;
106
+ }
107
+
84
108
  /** The whole element, tag and length included. */
85
109
  export function wholeOf(bytes, element) {
86
110
  return bytes.subarray(element.from, element.end);
@@ -106,6 +130,7 @@ export function timeOf(bytes, element) {
106
130
  m = /^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z$/.exec(text);
107
131
  if (m) m[1] = (Number(m[1]) < 50 ? '20' : '19') + m[1];
108
132
  } else if (element.tag === TAG.GENERALIZED_TIME) {
133
+ // eslint-disable-next-line security/detect-unsafe-regex -- no repetition inside repetition can match the same text two ways (fixed-length digits and one bounded fraction), so matching takes time in proportion to the input.
109
134
  m = /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(?:\.(\d{1,9}))?Z$/.exec(text);
110
135
  // DER allows no trailing zero in the fraction.
111
136
  if (m && m[7] && m[7].endsWith('0')) m = null;
@@ -147,25 +172,45 @@ export function ecdsaToRaw(der, size) {
147
172
  /**
148
173
  * Walk a whole element and everything inside it, and refuse anything that
149
174
  * is not strict DER: a constructed part whose items do not fill it exactly,
150
- * a whole number or an object identifier that is not in its shortest form,
151
- * a truth value that is not written in the one way DER allows. Parts this
152
- * checker does not otherwise read are still read here, so that two checkers
153
- * cannot disagree about whether a time-stamp is well formed.
175
+ * a string (of bits, bytes or characters) or a time written in parts
176
+ * (X.690, section 10.2), a set whose items are not in order (section 11.6;
177
+ * only where "holdSetOrder" is true), a whole number or an object identifier
178
+ * that is not in its shortest form, a truth value that is not written in the
179
+ * one way DER allows, a string of bits whose unused bits are not zero. Parts
180
+ * this checker does not otherwise read are still read here, so that two
181
+ * checkers cannot disagree about whether a time-stamp is well formed. What a
182
+ * part holds as bytes (the value of an octet string) is read as bytes only.
183
+ * A part under a tag that hides its kind (a context tag): written in parts,
184
+ * the parts inside it are read like any other, but the tagged part itself is
185
+ * neither refused as a string written in parts nor held to the order of a
186
+ * set, since its tag does not say which it is; written in one piece, its
187
+ * value is read as bytes only. The order of a set is held only where a
188
+ * standard requires DER; checkStamp says where, and passes false for the
189
+ * rest of a time-stamp.
154
190
  * @param {Uint8Array} bytes
155
191
  * @param {{tag: number, start: number, end: number}} element
192
+ * @param {boolean} [holdSetOrder=true] whether the items of a set must be in DER order
156
193
  */
157
- export function validateDer(bytes, element) {
194
+ export function validateDer(bytes, element, holdSetOrder = true) {
158
195
  let budget = 4000;
159
196
  const walk = (e, depth) => {
160
197
  if (--budget < 0 || depth > 24) throw badStamp('it holds too many parts, or parts nested too deeply.');
161
198
  const length = e.end - e.start;
162
199
  if (e.tag & 0x20) {
200
+ // Of the universal kinds, only a sequence and a set are written in
201
+ // parts; a string or a time is written in one piece (X.690, section 10.2).
202
+ if ((e.tag & 0xc0) === 0 && e.tag !== TAG.SEQUENCE && e.tag !== TAG.SET) {
203
+ throw badStamp('a string of bits, bytes or characters, or a time, is written in parts, which DER does not allow.');
204
+ }
205
+ const items = [];
163
206
  let at = e.start;
164
207
  while (at < e.end) {
165
208
  const child = readElement(bytes, at, e.end);
166
209
  walk(child, depth + 1);
210
+ items.push(child);
167
211
  at = child.end;
168
212
  }
213
+ if (holdSetOrder && e.tag === TAG.SET && !setInOrder(bytes, items)) throw badStamp('the items of a set are not in the order DER requires.');
169
214
  return;
170
215
  }
171
216
  const first = bytes[e.start];
@@ -185,7 +230,12 @@ export function validateDer(bytes, element) {
185
230
  } else if (e.tag === TAG.NULL) {
186
231
  if (length !== 0) throw badStamp('an empty value is not empty.');
187
232
  } else if (e.tag === TAG.BIT_STRING) {
188
- if (length === 0 || first > 7) throw badStamp('a string of bits is not written as DER allows.');
233
+ // The first byte counts the unused bits of the last byte, 0 to 7; a
234
+ // string of no bits has none; and the unused bits are zero (X.690,
235
+ // sections 8.6.2 and 11.2.1).
236
+ if (length === 0 || first > 7 || (length === 1 && first !== 0) || (length > 1 && (bytes[e.end - 1] & ((1 << first) - 1)) !== 0)) {
237
+ throw badStamp('a string of bits is not written as DER allows.');
238
+ }
189
239
  }
190
240
  };
191
241
  walk(element, 0);
package/src/dev.d.ts CHANGED
@@ -50,5 +50,5 @@ export function developmentSlip(fields: DevelopmentSlipFields): Promise<Developm
50
50
  /** The stub writer opened under a development slip, in one call. */
51
51
  export function developmentRecorder(
52
52
  fields: DevelopmentSlipFields,
53
- more?: { options?: CheckOptions; now?: () => number; countersignWithin?: number; signWithin?: number },
53
+ more?: { options?: CheckOptions; now?: () => number; countersignWithin?: number; signWithin?: number; denials?: boolean; denialsPerHour?: number },
54
54
  ): Promise<Omit<DevelopmentSlip, 'book'> & { recorder: Recorder }>;
package/src/dev.js CHANGED
@@ -110,7 +110,7 @@ export async function developmentSlip(fields = {}) {
110
110
  * The stub writer, opened under a development slip, in one call: the
111
111
  * shortest way to a first record. The same as developmentSlip followed by
112
112
  * openRecorder; "more" is passed to openRecorder (options, now,
113
- * countersignWithin, signWithin).
113
+ * countersignWithin, signWithin, denials, denialsPerHour).
114
114
  *
115
115
  * @param {object} fields the members of the slip; "actions" is required
116
116
  * @param {object} [more]
package/src/documents.js CHANGED
@@ -6,8 +6,8 @@
6
6
  // are reserved: each one on the list below says exactly which text its
7
7
  // fingerprint is made from, so that an investigator who holds that text
8
8
  // (the call's arguments, the HTTP request, the identifier a platform's own
9
- // log shows for the call) can work the fingerprint out again and match
10
- // the stub to it, without either side's help.
9
+ // log shows for the call, the tool's name and result) can work the
10
+ // fingerprint out again and match the stub to it, without either side's help.
11
11
 
12
12
  import { Refusal, fingerprint, sha256, toBase64url, utf8 } from './encoding.js';
13
13
 
@@ -23,6 +23,9 @@ export const DOCUMENT_NAMES = Object.freeze({
23
23
  'provared.request-id': "An identifier that the agent's side chose and sent with the call, exactly as sent.",
24
24
  'provared.event-id': 'An identifier that the platform gave for the call, exactly as it gave it.',
25
25
  'provared.error': "The error text that the agent's software was given, exactly as given.",
26
+ 'provared.tool-name': "The name of the tool, exactly as the agent's software called it.",
27
+ 'provared.tool-call-id': "The identifier that the model gave the tool call, exactly as the agent's software received it.",
28
+ 'provared.tool-result': "The tool's result, written in the one form of RFC 8785.",
26
29
  });
27
30
 
28
31
  /**
@@ -135,7 +138,9 @@ export async function httpResponseFingerprint(r) {
135
138
  /**
136
139
  * The fingerprint of an identifier or of an error text, exactly as given:
137
140
  * SHA-256 of its UTF-8 bytes. For "provared.trace-id", "provared.request-id",
138
- * "provared.event-id" and "provared.error".
141
+ * "provared.event-id", "provared.error", "provared.tool-name" and
142
+ * "provared.tool-call-id". A tool's result ("provared.tool-result") is
143
+ * fingerprinted as the arguments are, with argumentsFingerprint.
139
144
  * @param {string} id
140
145
  * @returns {Promise<string>}
141
146
  * @throws {Refusal} "bad-field" if it is not text, is empty, or is not well-formed Unicode
package/src/guard.js CHANGED
@@ -98,6 +98,8 @@ function onlySealThirdUnchecked(result) {
98
98
  const third = sigs.length === 3 && by('Ed25519')?.state === 'valid' && by('ML-DSA-87')?.state === 'valid' && by(SEAL_THIRD)?.state === 'unavailable';
99
99
  if (third) excused++; else note(sigs);
100
100
  stamps(e.stamps ?? []);
101
+ // The witness statements beside it: each of their signatures counts, as for any record.
102
+ for (const w of e.witnesses ?? []) note(w.signatures ?? []);
101
103
  } else if (e.kind === 'slip') {
102
104
  if (!e.signature || e.signature.state === 'unavailable') missing.add(e.signature ? e.signature.alg : 'passkey');
103
105
  else if (e.signature.state === 'unchecked') skipped = true;
@@ -111,7 +113,7 @@ function onlySealThirdUnchecked(result) {
111
113
  if (!e.signature) skipped = true;
112
114
  else if (e.signature.state === 'unavailable') missing.add(e.signature.alg);
113
115
  stamps(e.stamps ?? []);
114
- } else if (['refusal', 'terms', 'vouching', 'withdrawal', 'pass', 'acknowledgement'].includes(e.kind)) {
116
+ } else if (['refusal', 'terms', 'vouching', 'withdrawal', 'pass', 'acknowledgement', 'denial'].includes(e.kind)) {
115
117
  note(e.signatures ?? []);
116
118
  } else {
117
119
  skipped = true;
@@ -120,6 +122,15 @@ function onlySealThirdUnchecked(result) {
120
122
  for (const h of result.held ?? []) {
121
123
  stamps(h.stamps ?? []);
122
124
  for (const a of h.acknowledgements ?? []) note(a.signatures ?? []);
125
+ // A seal held elsewhere, handed over beside the book: excused as a seal in the book is.
126
+ if (h.kind === 'seal') {
127
+ const sigs = h.signatures ?? [];
128
+ const by = (method) => sigs.find((x) => x.method === method);
129
+ const third = sigs.length === 3 && by('Ed25519')?.state === 'valid' && by('ML-DSA-87')?.state === 'valid' && by(SEAL_THIRD)?.state === 'unavailable';
130
+ if (third) excused++;
131
+ else for (const x of sigs) if (x.state === 'unavailable') missing.add(x.method);
132
+ for (const w of h.witnesses ?? []) note(w.signatures ?? []);
133
+ }
123
134
  }
124
135
  return !skipped && missing.size === 0 && excused > 0 ? [SEAL_THIRD] : null;
125
136
  }
package/src/index.js CHANGED
@@ -11,8 +11,13 @@
11
11
  // writeStub (the agent), countersign, writeRefusal, writeTerms
12
12
  // (the service), writeSeal (whoever keeps the book), blockStampItem
13
13
  // (a proof from a public blockchain, to put beside a seal),
14
+ // writeWitness (a witness signs for a seal), openWitness (a witness
15
+ // that signs only for a seal that extends the last one it holds),
16
+ // openSealer (a central sealer, which seals books it is sent only
17
+ // the leaf fingerprints of: leafFingerprint),
14
18
  // prepareCancellation + assembleCancellation (the person ends a slip),
15
19
  // writeAcknowledgement (the agent's side says it was handed a cancellation),
20
+ // writeDenial (the agent's side says it did not take an action, and why),
16
21
  // writeVouching, writeWithdrawal (an organisation vouches for a name),
17
22
  // writePass (an agent hands part of its permission to a helper agent),
18
23
  // writeBook, makeShow
@@ -33,8 +38,13 @@ export { writeStub, countersign } from './stub.js';
33
38
  export { writeRefusal, writeTerms } from './service.js';
34
39
  export { writePass } from './pass.js';
35
40
  export { writeSeal } from './seal.js';
41
+ export { writeWitness, MAX_WITNESSES } from './witness.js';
42
+ export { openWitness, bookIdentity, witnessProof } from './witnessing.js';
43
+ export { openSealer, leafFingerprint } from './sealer.js';
44
+ export { consistencyProof, inclusionPath } from './tree.js';
36
45
  export { blockStampItem } from './blockstamp.js';
37
46
  export { prepareCancellation, assembleCancellation, writeAcknowledgement, writeVouching, writeWithdrawal, VOUCHED_KINDS } from './standing.js';
47
+ export { writeDenial, denialCodes, FINDING_CODES } from './denial.js';
38
48
  export { entryLine, writeBook, makeShow } from './book.js';
39
49
  export { generateKeySet, generateSealKeySet, signerFromKey } from './signatures.js';
40
50
  export { ACTION_KINDS, SHARED_ACTIONS, CONDUCT_RULES, RESERVED_PREFIX, actionKind } from './actions.js';
package/src/jws.js CHANGED
@@ -66,6 +66,14 @@ export const KINDS = {
66
66
  type: 'provared.pass.v0',
67
67
  protected: ['{"typ":"vnd.provared.pass.v0+json","alg":"Ed25519"}', '{"typ":"vnd.provared.pass.v0+json","alg":"ML-DSA-87"}'],
68
68
  },
69
+ denial: {
70
+ type: 'provared.denial.v0',
71
+ protected: ['{"typ":"vnd.provared.denial.v0+json","alg":"Ed25519"}', '{"typ":"vnd.provared.denial.v0+json","alg":"ML-DSA-87"}'],
72
+ },
73
+ witness: {
74
+ type: 'provared.witness.v0',
75
+ protected: ['{"typ":"vnd.provared.witness.v0+json","alg":"Ed25519"}', '{"typ":"vnd.provared.witness.v0+json","alg":"ML-DSA-87"}'],
76
+ },
69
77
  seal: {
70
78
  type: 'provared.seal.v0',
71
79
  protected: [
package/src/keys.js CHANGED
@@ -46,6 +46,50 @@ export function weakEd25519(bytes) {
46
46
  return y >= P || SMALL_ORDER_Y.has(y);
47
47
  }
48
48
 
49
+ // The order L of the Ed25519 base point (RFC 8032, section 5.1). The number
50
+ // S of a signature must be below it (RFC 8032, section 5.1.7); libraries
51
+ // differ in whether they check, so a checker checks itself.
52
+ const L = 2n ** 252n + 27742317777372353535851937790883648493n;
53
+
54
+ /**
55
+ * Whether bytes are an Ed25519 signature that can never be valid, whatever
56
+ * the key: not 64 bytes; or its first half (the point R) is one under which
57
+ * anyone can sign or is written in more than one way; or its second half
58
+ * (the number S, little-endian) is not below L.
59
+ * @param {Uint8Array} signature
60
+ * @returns {boolean}
61
+ */
62
+ export function badEd25519Signature(signature) {
63
+ if (signature.length !== 64) return true;
64
+ if (weakEd25519(signature.subarray(0, 32))) return true;
65
+ let s = 0n;
66
+ for (let i = 63; i >= 32; i--) s = (s << 8n) | BigInt(signature[i]);
67
+ return s >= L;
68
+ }
69
+
70
+ // The curve P-256 (FIPS 186-5, section D.1.2.3): y^2 = x^3 - 3x + b over
71
+ // the whole numbers modulo p. A key whose two halves are not a point on it
72
+ // can check no signature, and libraries report it in different ways, so a
73
+ // checker refuses it itself.
74
+ const P256_P = 2n ** 256n - 2n ** 224n + 2n ** 192n + 2n ** 96n - 1n;
75
+ const P256_B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604bn;
76
+
77
+ /**
78
+ * Whether two halves of 32 bytes each (x and y, big-endian) are a point on
79
+ * the curve P-256, each below p.
80
+ * @param {Uint8Array} x
81
+ * @param {Uint8Array} y
82
+ * @returns {boolean}
83
+ */
84
+ export function onP256(x, y) {
85
+ let X = 0n;
86
+ let Y = 0n;
87
+ for (const b of x) X = (X << 8n) | BigInt(b);
88
+ for (const b of y) Y = (Y << 8n) | BigInt(b);
89
+ if (X >= P256_P || Y >= P256_P) return false;
90
+ return (Y * Y) % P256_P === (((X * X * X - 3n * X + P256_B) % P256_P) + P256_P) % P256_P;
91
+ }
92
+
49
93
  function bad(where, why) {
50
94
  return new Refusal('bad-key', `${where}: ${why}`);
51
95
  }
@@ -94,9 +138,12 @@ export function checkKey(jwk, allowed, where) {
94
138
  if (bytesOf(jwk, 'pub', where).length !== 64) throw bad(where, 'an SLH-DSA-SHA2-256s public key is 64 bytes.');
95
139
  } else if (alg === 'ES256') {
96
140
  if (jwk.kty !== 'EC' || jwk.crv !== 'P-256') throw bad(where, 'an ES256 key has kty EC and crv P-256.');
97
- if (bytesOf(jwk, 'x', where).length !== 32 || bytesOf(jwk, 'y', where).length !== 32) {
141
+ const x = bytesOf(jwk, 'x', where);
142
+ const y = bytesOf(jwk, 'y', where);
143
+ if (x.length !== 32 || y.length !== 32) {
98
144
  throw bad(where, 'the two halves of a P-256 public key are 32 bytes each.');
99
145
  }
146
+ if (!onP256(x, y)) throw bad(where, 'a P-256 public key must be a point on the curve, each half below the prime of the curve.');
100
147
  } else {
101
148
  if (jwk.kty !== 'RSA' || jwk.e !== 'AQAB') throw bad(where, 'an RS256 key has kty RSA and e AQAB.');
102
149
  const n = bytesOf(jwk, 'n', where);