@gerege-systems/eid-mongolia-sdk 0.20.1 → 0.21.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/dist/validate.js CHANGED
@@ -3,338 +3,68 @@ import {
3
3
  InflateError,
4
4
  PRef,
5
5
  Reader,
6
+ StructureError,
6
7
  appearanceObjects,
8
+ checkStructure,
7
9
  inflateRaw,
8
10
  isBrowser,
9
11
  round100,
10
12
  visibleWidget
11
- } from "./chunk-J7EG3VNQ.js";
13
+ } from "./chunk-T3QLOY6J.js";
12
14
  import {
13
15
  EID_ISSUERS,
14
16
  INTERMEDIATES,
15
17
  NATIONAL_ROOT,
16
18
  OID,
19
+ SHA1_SIG,
17
20
  TAG,
21
+ ZERO_TIME,
22
+ addMs,
23
+ after,
24
+ before,
18
25
  children,
26
+ commonName,
19
27
  concat,
28
+ crlFor,
29
+ crlListed,
20
30
  digest,
21
31
  equal,
22
32
  expect,
33
+ formatTime,
34
+ freshAt,
23
35
  fromB64,
36
+ fromDate,
24
37
  fromLatin1,
38
+ fromPrecise,
39
+ hexSerial,
25
40
  intValue,
26
41
  isTSASigningCert,
42
+ isZero,
27
43
  latin1,
44
+ nameString,
45
+ ocspCertIDMatches,
46
+ ocspFreshAt,
28
47
  ocspRequest,
29
48
  oidString,
49
+ parseCRL,
30
50
  parseCertificate,
31
51
  parseOCSP,
32
52
  parseOCSPFrom,
33
53
  parseOne,
34
54
  parsePreciseTime,
55
+ pdfText,
35
56
  readNode,
36
57
  sameCertKey,
37
58
  sha256,
59
+ subjectNames,
60
+ subjectSerialNumber,
38
61
  subtle,
39
62
  toHex,
40
63
  verifySignature
41
- } from "./chunk-JV4NR4LG.js";
64
+ } from "./chunk-ISTWNY6N.js";
42
65
  import {
43
66
  EidError
44
- } from "./chunk-D5NPS7ET.js";
45
-
46
- // src/pades/validate/gofmt.ts
47
- var ZERO_TIME = { nanos: 0 };
48
- function fromPrecise(t) {
49
- return { sec: Math.floor(t.date.getTime() / 1e3), nanos: t.nanos };
50
- }
51
- function fromDate(d) {
52
- const ms = d.getTime();
53
- const sec = Math.floor(ms / 1e3);
54
- return { sec, nanos: (ms - sec * 1e3) * 1e6 };
55
- }
56
- var isZero = (t) => t.sec === void 0;
57
- function before(a, b) {
58
- return cmp(a, b) < 0;
59
- }
60
- function after(a, b) {
61
- return cmp(a, b) > 0;
62
- }
63
- function cmp(a, b) {
64
- const as = a.sec ?? -62135596800;
65
- const bs = b.sec ?? -62135596800;
66
- if (as !== bs) return as < bs ? -1 : 1;
67
- return a.nanos === b.nanos ? 0 : a.nanos < b.nanos ? -1 : 1;
68
- }
69
- function addMs(t, ms) {
70
- const total = (t.sec ?? -62135596800) * 1e3 + Math.floor(t.nanos / 1e6) + ms;
71
- const sec = Math.floor(total / 1e3);
72
- return { sec, nanos: (total - sec * 1e3) * 1e6 + t.nanos % 1e6 };
73
- }
74
- function formatTime(t) {
75
- if (t.sec === void 0) return "0001-01-01T00:00:00Z";
76
- const d = new Date(t.sec * 1e3);
77
- const p = (n, w = 2) => String(n).padStart(w, "0");
78
- let s = `${p(d.getUTCFullYear(), 4)}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}T${p(d.getUTCHours())}:${p(d.getUTCMinutes())}:${p(d.getUTCSeconds())}`;
79
- if (t.nanos > 0) s += "." + String(t.nanos).padStart(9, "0").replace(/0+$/, "");
80
- return s + "Z";
81
- }
82
- function hexSerial(intContent) {
83
- let neg = intContent.length > 0 && (intContent[0] & 128) !== 0;
84
- let v = 0n;
85
- for (const b of intContent) v = v << 8n | BigInt(b);
86
- if (neg) v -= 1n << BigInt(intContent.length * 8);
87
- neg = v < 0n;
88
- return (neg ? "-" : "") + (neg ? -v : v).toString(16).toUpperCase();
89
- }
90
- var NO_RUNE = -1;
91
- var PDF_DOC = (() => {
92
- const t = new Array(256).fill(NO_RUNE);
93
- t[9] = 9;
94
- t[10] = 10;
95
- t[13] = 13;
96
- [728, 711, 710, 729, 733, 731, 730, 732].forEach((r, i) => t[24 + i] = r);
97
- for (let i = 32; i < 127; i++) t[i] = i;
98
- [
99
- 8226,
100
- 8224,
101
- 8225,
102
- 8230,
103
- 8212,
104
- 8211,
105
- 402,
106
- 8260,
107
- 8249,
108
- 8250,
109
- 8722,
110
- 8240,
111
- 8222,
112
- 8220,
113
- 8221,
114
- 8216,
115
- 8217,
116
- 8218,
117
- 8482,
118
- 64257,
119
- 64258,
120
- 321,
121
- 338,
122
- 352,
123
- 376,
124
- 381,
125
- 305,
126
- 322,
127
- 339,
128
- 353,
129
- 382,
130
- NO_RUNE
131
- ].forEach((r, i) => t[128 + i] = r);
132
- t[160] = 8364;
133
- for (let i = 161; i <= 255; i++) t[i] = i;
134
- t[173] = NO_RUNE;
135
- return t;
136
- })();
137
- function pdfText(b) {
138
- const utf16 = b.length >= 2 && b[0] === 254 && b[1] === 255 && b.length % 2 === 0;
139
- if (!utf16 && b.every((x) => PDF_DOC[x] !== NO_RUNE)) return String.fromCodePoint(...Array.from(b, (x) => PDF_DOC[x]));
140
- if (utf16) {
141
- const u = [];
142
- for (let i = 2; i < b.length; i += 2) u.push(b[i] << 8 | b[i + 1]);
143
- return fixSurrogates(String.fromCharCode(...u));
144
- }
145
- return goString(b);
146
- }
147
- function fixSurrogates(s) {
148
- return s.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, "\uFFFD");
149
- }
150
- function goString(b) {
151
- let out = "";
152
- for (let i = 0; i < b.length; ) {
153
- const [r, n] = decodeRune(b, i);
154
- out += String.fromCodePoint(r);
155
- i += n;
156
- }
157
- return out;
158
- }
159
- function decodeRune(b, i) {
160
- const c0 = b[i];
161
- if (c0 < 128) return [c0, 1];
162
- const bad = [65533, 1];
163
- const cont = (k, lo = 128, hi = 191) => i + k < b.length && b[i + k] >= lo && b[i + k] <= hi;
164
- if (c0 >= 194 && c0 <= 223) return cont(1) ? [(c0 & 31) << 6 | b[i + 1] & 63, 2] : bad;
165
- if (c0 >= 224 && c0 <= 239) {
166
- const lo = c0 === 224 ? 160 : 128;
167
- const hi = c0 === 237 ? 159 : 191;
168
- return cont(1, lo, hi) && cont(2) ? [(c0 & 15) << 12 | (b[i + 1] & 63) << 6 | b[i + 2] & 63, 3] : bad;
169
- }
170
- if (c0 >= 240 && c0 <= 244) {
171
- const lo = c0 === 240 ? 144 : 128;
172
- const hi = c0 === 244 ? 143 : 191;
173
- return cont(1, lo, hi) && cont(2) && cont(3) ? [(c0 & 7) << 18 | (b[i + 1] & 63) << 12 | (b[i + 2] & 63) << 6 | b[i + 3] & 63, 4] : bad;
174
- }
175
- return bad;
176
- }
177
- var ATTR_NAMES = {
178
- "2.5.4.6": "C",
179
- "2.5.4.10": "O",
180
- "2.5.4.11": "OU",
181
- "2.5.4.3": "CN",
182
- "2.5.4.5": "SERIALNUMBER",
183
- "2.5.4.7": "L",
184
- "2.5.4.8": "ST",
185
- "2.5.4.9": "STREET",
186
- "2.5.4.17": "POSTALCODE"
187
- };
188
- function asn1String(n) {
189
- switch (n.tag) {
190
- case TAG.PrintableString:
191
- case TAG.IA5String:
192
- case 18:
193
- return goString(n.value);
194
- case 20:
195
- return goString(n.value);
196
- case TAG.UTF8String:
197
- return goString(n.value);
198
- case 30: {
199
- if (n.value.length % 2 !== 0) return void 0;
200
- const u = [];
201
- for (let i = 0; i < n.value.length; i += 2) u.push(n.value[i] << 8 | n.value[i + 1]);
202
- if (u.length > 0 && u[u.length - 1] === 0) u.pop();
203
- return fixSurrogates(String.fromCharCode(...u));
204
- }
205
- }
206
- return void 0;
207
- }
208
- function rdns(name) {
209
- return children(name).map(
210
- (rdn) => children(rdn).map((atv) => {
211
- const [t, v] = children(atv);
212
- return { oid: oidString(t.value), str: asn1String(v), raw: v };
213
- })
214
- );
215
- }
216
- function marshalString(s) {
217
- const bytes = new TextEncoder().encode(s);
218
- const printable = [...s].every((ch) => {
219
- const c = ch.codePointAt(0);
220
- return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 57 || " '()+,-./:=?".includes(ch);
221
- });
222
- const tag = printable ? TAG.PrintableString : TAG.UTF8String;
223
- const len = bytes.length;
224
- const hdr = len < 128 ? [tag, len] : len < 256 ? [tag, 129, len] : [tag, 130, len >> 8, len & 255];
225
- const out = new Uint8Array(hdr.length + len);
226
- out.set(hdr, 0);
227
- out.set(bytes, hdr.length);
228
- return out;
229
- }
230
- var toHex2 = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
231
- function nameString(rawName) {
232
- const seq = rdns(readNode(rawName, 0));
233
- const f = { C: [], ST: [], L: [], STREET: [], POSTALCODE: [], O: [], OU: [] };
234
- let cn = "";
235
- let sn = "";
236
- const all = [];
237
- for (const rdn of seq) {
238
- for (const a of rdn) {
239
- all.push(a);
240
- if (a.str === void 0) continue;
241
- switch (a.oid) {
242
- case "2.5.4.3":
243
- cn = a.str;
244
- break;
245
- case "2.5.4.5":
246
- sn = a.str;
247
- break;
248
- case "2.5.4.6":
249
- f.C.push(a.str);
250
- break;
251
- case "2.5.4.7":
252
- f.L.push(a.str);
253
- break;
254
- case "2.5.4.8":
255
- f.ST.push(a.str);
256
- break;
257
- case "2.5.4.9":
258
- f.STREET.push(a.str);
259
- break;
260
- case "2.5.4.17":
261
- f.POSTALCODE.push(a.str);
262
- break;
263
- case "2.5.4.10":
264
- f.O.push(a.str);
265
- break;
266
- case "2.5.4.11":
267
- f.OU.push(a.str);
268
- break;
269
- }
270
- }
271
- }
272
- const out = [];
273
- const skip = /* @__PURE__ */ new Set(["2.5.4.3", "2.5.4.5", "2.5.4.6", "2.5.4.7", "2.5.4.8", "2.5.4.9", "2.5.4.10", "2.5.4.11", "2.5.4.17"]);
274
- for (const a of all) if (!skip.has(a.oid)) out.push([a]);
275
- const add = (vals, oid) => {
276
- if (vals.length > 0) out.push(vals.map((v) => ({ oid, str: v })));
277
- };
278
- add(f.C, "2.5.4.6");
279
- add(f.ST, "2.5.4.8");
280
- add(f.L, "2.5.4.7");
281
- add(f.STREET, "2.5.4.9");
282
- add(f.POSTALCODE, "2.5.4.17");
283
- add(f.O, "2.5.4.10");
284
- add(f.OU, "2.5.4.11");
285
- if (cn) add([cn], "2.5.4.3");
286
- if (sn) add([sn], "2.5.4.5");
287
- let s = "";
288
- for (let i = 0; i < out.length; i++) {
289
- const rdn = out[out.length - 1 - i];
290
- if (i > 0) s += ",";
291
- rdn.forEach((tv, j) => {
292
- if (j > 0) s += "+";
293
- const typeName = ATTR_NAMES[tv.oid];
294
- if (!typeName) {
295
- const der = tv.str !== void 0 ? marshalString(tv.str) : tv.raw.raw;
296
- s += `${tv.oid}=#${toHex2(der)}`;
297
- return;
298
- }
299
- const v = tv.str ?? "";
300
- const chars = [...v];
301
- let byteIdx = 0;
302
- const lastByte = new TextEncoder().encode(v).length - 1;
303
- let esc = "";
304
- for (const c of chars) {
305
- let e = false;
306
- if (',+"\\<>;'.includes(c)) e = true;
307
- else if (c === " ") e = byteIdx === 0 || byteIdx === lastByte;
308
- else if (c === "#") e = byteIdx === 0;
309
- esc += e ? "\\" + c : c;
310
- byteIdx += new TextEncoder().encode(c).length;
311
- }
312
- s += `${typeName}=${esc}`;
313
- });
314
- }
315
- return s;
316
- }
317
- function commonName(rawName) {
318
- let cn = "";
319
- for (const rdn of rdns(readNode(rawName, 0))) for (const a of rdn) if (a.oid === "2.5.4.3" && a.str !== void 0) cn = a.str;
320
- return cn;
321
- }
322
- function subjectNames(rawSubject) {
323
- let given = "";
324
- let surname = "";
325
- for (const rdn of rdns(readNode(rawSubject, 0))) {
326
- for (const a of rdn) {
327
- if (a.oid === "2.5.4.42") given = a.str ?? "";
328
- if (a.oid === "2.5.4.4") surname = a.str ?? "";
329
- }
330
- }
331
- return { given, surname };
332
- }
333
- function subjectSerialNumber(rawSubject) {
334
- let sn = "";
335
- for (const rdn of rdns(readNode(rawSubject, 0))) for (const a of rdn) if (a.oid === "2.5.4.5" && a.str !== void 0) sn = a.str;
336
- return sn;
337
- }
67
+ } from "./chunk-ZB7LEWA5.js";
338
68
 
339
69
  // src/pades/validate/evaluate.ts
340
70
  var PROBLEMS = {
@@ -351,7 +81,6 @@ var PROBLEMS = {
351
81
  modifiedAfter: "modified_after_signing",
352
82
  timestampInvalid: "timestamp_invalid"
353
83
  };
354
- var SHA1_SIG = /* @__PURE__ */ new Set(["1.2.840.113549.1.1.5", "1.2.840.10045.4.1", "1.2.840.10040.4.3", "1.2.840.113549.1.1.4"]);
355
84
  async function checkSigFrom(c, parent) {
356
85
  if (parent.version === 3 && !parent.basicConstraintsValid || parent.basicConstraintsValid && !parent.isCA) return false;
357
86
  if (parent.keyUsage !== 0 && !(parent.keyUsage & 1 << 5)) return false;
@@ -489,7 +218,7 @@ var Evaluator = class {
489
218
  isIssuer(c) {
490
219
  return this.cfg.issuers.some((is) => equal(c.rawSubject, is.rawSubject) && equal(c.spki, is.spki));
491
220
  }
492
- async certInfo(c, chain) {
221
+ async certInfo(c, chain, at) {
493
222
  const info = {
494
223
  common_name: commonName(c.rawSubject),
495
224
  subject: nameString(c.rawSubject),
@@ -506,7 +235,7 @@ var Evaluator = class {
506
235
  if (!this.cfg.status) return info;
507
236
  let st;
508
237
  try {
509
- st = await this.cfg.status.certStatus(intValue(c.serial));
238
+ st = await this.cfg.status.certStatus(c, chain[1], at);
510
239
  } catch {
511
240
  return info;
512
241
  }
@@ -594,7 +323,7 @@ var Evaluator = class {
594
323
  s.chain_trusted = trusted;
595
324
  s.chain = chain ? chain.map((c) => commonName(c.rawSubject)) : [commonName(leaf.rawSubject)];
596
325
  this.lastRevokedAt = void 0;
597
- const info = await this.certInfo(leaf, chain);
326
+ const info = await this.certInfo(leaf, chain, at);
598
327
  s.certificate = info;
599
328
  if (!trusted) s.problems.push(PROBLEMS.untrusted);
600
329
  if (!["sign", "seal", "document_signer"].includes(info.usage)) s.problems.push(PROBLEMS.keyUsage);
@@ -613,7 +342,7 @@ var Evaluator = class {
613
342
  }
614
343
  if (cades && r.signingTimeAttr && info.issued_by_eid) s.problems.push(PROBLEMS.malformed);
615
344
  if (!r.coversWhole && !r.laterSignaturesOnly) s.problems.push(PROBLEMS.modifiedAfter);
616
- if (trusted) s.ltv = await ltvCoverage(r.dss, at, [chain, tsChain ?? []]);
345
+ if (trusted) s.ltv = await ltvCoverage(r.dss, at, this.cfg.now, [chain, tsChain ?? []]);
617
346
  if (cades) {
618
347
  s.signature_level = signingTime && s.ltv?.complete ? "PAdES-BASELINE-LT" : signingTime ? "PAdES-BASELINE-T" : "PAdES-BASELINE-B";
619
348
  }
@@ -695,9 +424,8 @@ function usageOf(c) {
695
424
  if (ds && known.includes("1.3.6.1.5.5.7.3.2")) return "auth";
696
425
  return "other";
697
426
  }
698
- var LTV_FRESHNESS_MS = 24 * 36e5;
699
427
  var OID_OCSP_NOCHECK = "1.3.6.1.5.5.7.48.1.5";
700
- async function ltvCoverage(dss, at, chains) {
428
+ async function ltvCoverage(dss, at, now, chains) {
701
429
  const out = { complete: false, missing: [] };
702
430
  const crls = [];
703
431
  for (const der of dss?.crls ?? []) {
@@ -711,13 +439,13 @@ async function ltvCoverage(dss, at, chains) {
711
439
  const issuer = chain[i + 1];
712
440
  if (seen.some((x) => equal(x, cert.raw))) continue;
713
441
  seen.push(cert.raw);
714
- if (!await covered(cert, issuer, at, dss?.ocsps ?? [], crls)) out.missing.push(commonName(cert.rawSubject));
442
+ if (!await covered(cert, issuer, at, now, dss?.ocsps ?? [], crls)) out.missing.push(commonName(cert.rawSubject));
715
443
  }
716
444
  }
717
445
  out.complete = out.missing.length === 0 && seen.length > 0;
718
446
  return out;
719
447
  }
720
- async function covered(cert, issuer, at, ocsps, crls) {
448
+ async function covered(cert, issuer, at, now, ocsps, crls) {
721
449
  for (const der of ocsps) {
722
450
  let resp;
723
451
  try {
@@ -725,98 +453,64 @@ async function covered(cert, issuer, at, ocsps, crls) {
725
453
  } catch {
726
454
  continue;
727
455
  }
728
- if (resp.status !== "good") continue;
456
+ if (resp.status !== "good" || !await ocspCertIDMatches(der, cert, issuer)) continue;
729
457
  if (resp.certificate && !equal(resp.certificate.raw, issuer.raw) && !resp.certificate.extensions.some((e) => e.oid === OID_OCSP_NOCHECK)) continue;
730
- const tu = fromPrecise(resp.thisUpdate);
731
- if (!before(tu, addMs(at, -LTV_FRESHNESS_MS))) return true;
732
- if (resp.nextUpdate && !before(fromDate(resp.nextUpdate), at) && !after(tu, at)) return true;
458
+ if (ocspFreshAt(resp, at, now)) return true;
733
459
  }
734
460
  for (const crl of crls) {
735
- if (!equal(crl.rawIssuer, cert.rawIssuer) || !await crlSignedBy(crl, issuer)) continue;
736
- if (crl.nextUpdate && before(crl.nextUpdate, at)) continue;
737
- const serial = intValue(cert.serial);
738
- if (crl.revoked.some((e) => e.serial === serial)) continue;
461
+ if (!await crlFor(crl, cert, issuer) || !freshAt(crl.thisUpdate, crl.nextUpdate, at, now)) continue;
462
+ if (crlListed(crl, cert)) continue;
739
463
  return true;
740
464
  }
741
465
  return false;
742
466
  }
743
- function parseCRL(der) {
744
- try {
745
- const top = children(expect(parseOne(der), TAG.SEQUENCE, "CertificateList"));
746
- const tbs = expect(top[0], TAG.SEQUENCE, "TBSCertList");
747
- const sigAlg = expect(top[1], TAG.SEQUENCE, "sigAlg");
748
- const sigBits = expect(top[2], TAG.BIT_STRING, "sig");
749
- const f = children(tbs);
750
- let i = 0;
751
- if (f[i]?.tag === TAG.INTEGER) i++;
752
- expect(f[i++], TAG.SEQUENCE, "signature");
753
- const issuer = expect(f[i++], TAG.SEQUENCE, "issuer");
754
- const tu = f[i++];
755
- if (!tu || tu.tag !== TAG.UTCTime && tu.tag !== TAG.GeneralizedTime) return void 0;
756
- let nextUpdate;
757
- if (f[i] && (f[i].tag === TAG.UTCTime || f[i].tag === TAG.GeneralizedTime)) nextUpdate = fromPrecise(parsePreciseTime(f[i++]));
758
- const revoked = [];
759
- if (f[i]?.tag === TAG.SEQUENCE) {
760
- for (const e of children(f[i++])) {
761
- const p = children(expect(e, TAG.SEQUENCE, "entry"));
762
- revoked.push({ serial: intValue(expect(p[0], TAG.INTEGER, "serial").value), time: fromPrecise(parsePreciseTime(p[1])) });
763
- }
764
- }
765
- return { rawIssuer: issuer.raw, tbs: tbs.raw, sigAlg, sig: sigBits.value.subarray(1), nextUpdate, revoked };
766
- } catch {
767
- return void 0;
768
- }
467
+ function responderOK(resp, issuer) {
468
+ if (!resp.certificate || equal(resp.certificate.raw, issuer.raw)) return true;
469
+ return resp.certificate.extKeyUsage.includes(OID.kpOCSPSigning);
769
470
  }
770
- async function crlSignedBy(crl, issuer) {
771
- if (issuer.version === 3 && !issuer.basicConstraintsValid || issuer.basicConstraintsValid && !issuer.isCA) return false;
772
- if (issuer.keyUsage !== 0 && !(issuer.keyUsage & 1 << 6)) return false;
773
- if (SHA1_SIG.has(oidString(children(crl.sigAlg)[0].value))) return false;
471
+ async function ocspFor(der, cert, issuer) {
472
+ let resp;
774
473
  try {
775
- return await verifySignature(issuer, crl.sigAlg, crl.tbs, crl.sig);
474
+ resp = await parseOCSPFrom(der, intValue(cert.serial), issuer);
776
475
  } catch {
777
- return false;
476
+ return void 0;
778
477
  }
779
- }
780
- function responderOK(resp, issuer) {
781
- if (!resp.certificate || equal(resp.certificate.raw, issuer.raw)) return true;
782
- return resp.certificate.extKeyUsage.includes(OID.kpOCSPSigning);
478
+ if (resp.serial !== intValue(cert.serial) || !await ocspCertIDMatches(der, cert, issuer) || !responderOK(resp, issuer)) return void 0;
479
+ return resp;
783
480
  }
784
481
  var DSSStatus = class {
785
- constructor(dss, issuers) {
482
+ constructor(dss, now) {
786
483
  this.dss = dss;
787
- this.issuers = issuers;
484
+ this.now = now;
788
485
  }
789
486
  dss;
790
- issuers;
791
- async certStatus(serial) {
487
+ now;
488
+ async certStatus(cert, issuer, at) {
489
+ let revoked;
490
+ const markRevoked = (t) => {
491
+ if (!revoked || before(t, revoked)) revoked = t;
492
+ };
493
+ let good = false;
792
494
  for (const der of this.dss?.ocsps ?? []) {
793
- for (const iss of this.issuers) {
794
- let resp;
795
- try {
796
- resp = await parseOCSPFrom(der, void 0, iss);
797
- } catch {
798
- continue;
799
- }
800
- if (resp.serial !== serial || !responderOK(resp, iss)) continue;
801
- if (resp.status === "good") return { issued: true, revoked: false };
802
- if (resp.status === "revoked") return { issued: true, revoked: true, revokedAt: fromPrecise(resp.revokedAt) };
803
- }
495
+ const resp = await ocspFor(der, cert, issuer);
496
+ if (!resp) continue;
497
+ if (resp.status === "revoked") markRevoked(fromPrecise(resp.revokedAt));
498
+ else if (resp.status === "good") good ||= ocspFreshAt(resp, at, this.now);
804
499
  }
805
500
  for (const der of this.dss?.crls ?? []) {
806
501
  const crl = parseCRL(der);
807
- if (!crl) continue;
808
- for (const iss of this.issuers) {
809
- if (!equal(crl.rawIssuer, iss.rawSubject) || !await crlSignedBy(crl, iss)) continue;
810
- const e = crl.revoked.find((x) => x.serial === serial);
811
- return e ? { issued: true, revoked: true, revokedAt: e.time } : { issued: true, revoked: false };
812
- }
502
+ if (!crl || !await crlFor(crl, cert, issuer)) continue;
503
+ const t = crlListed(crl, cert);
504
+ if (t) markRevoked(t);
505
+ else good ||= freshAt(crl.thisUpdate, crl.nextUpdate, at, this.now);
813
506
  }
507
+ if (revoked) return { issued: true, revoked: true, revokedAt: revoked };
508
+ if (good) return { issued: true, revoked: false };
814
509
  return { issued: false, revoked: false };
815
510
  }
816
511
  };
817
512
  var ONLINE_OCSP_MAX = 16 << 10;
818
- var CLOCK_SKEW_MS = 5 * 6e4;
819
- var OnlineStatus = class _OnlineStatus {
513
+ var OnlineStatus = class {
820
514
  constructor(f, fallback, now) {
821
515
  this.f = f;
822
516
  this.fallback = fallback;
@@ -825,89 +519,59 @@ var OnlineStatus = class _OnlineStatus {
825
519
  f;
826
520
  fallback;
827
521
  now;
828
- leaves = /* @__PURE__ */ new Map();
829
522
  cache = /* @__PURE__ */ new Map();
830
- static async create(f, raws, issuers, fallback, now) {
831
- const s = new _OnlineStatus(f, fallback, now);
832
- for (const r of raws) {
833
- let leaf;
834
- try {
835
- leaf = parseCertificate(r.leaf ?? new Uint8Array());
836
- } catch {
837
- continue;
838
- }
839
- const key = intValue(leaf.serial).toString(16);
840
- const prev = s.leaves.get(key);
841
- if (prev) {
842
- if (!equal(prev.leaf.raw, leaf.raw)) prev.ambiguous = true;
843
- continue;
844
- }
845
- let issuer;
846
- for (const is of issuers) {
847
- if (equal(leaf.rawIssuer, is.rawSubject) && await checkSigFrom(leaf, is)) {
848
- issuer = is;
849
- break;
850
- }
851
- }
852
- if (issuer) s.leaves.set(key, { leaf, issuer, ambiguous: false });
853
- }
854
- return s;
855
- }
856
- async certStatus(serial) {
857
- const key = serial.toString(16);
523
+ async certStatus(cert, issuer, at) {
524
+ const key = toHex(cert.raw) + ":" + toHex(issuer.raw);
858
525
  const hit = this.cache.get(key);
859
526
  if (hit) return hit;
860
- const li = this.leaves.get(key);
861
- if (!li || li.ambiguous || !li.issuer) return { issued: false, revoked: false };
862
- let st = await this.ocsp(li.leaf, li.issuer) ?? await this.crl(li.leaf, li.issuer);
863
- if (!st) {
864
- if (!this.fallback) return { issued: false, revoked: false };
865
- st = await this.fallback.certStatus(serial);
527
+ let st = await this.ocsp(cert, issuer) ?? await this.crl(cert, issuer);
528
+ if (this.fallback && (!st || !st.revoked)) {
529
+ const fst = await this.fallback.certStatus(cert, issuer, at);
530
+ if (!st || fst.revoked) st = fst;
866
531
  }
532
+ st ??= { issued: false, revoked: false };
867
533
  this.cache.set(key, st);
868
534
  return st;
869
535
  }
870
- async ocsp(leaf, issuer) {
536
+ async ocsp(cert, issuer) {
871
537
  let req;
872
538
  try {
873
- req = await ocspRequest(leaf, issuer);
539
+ req = await ocspRequest(cert, issuer);
874
540
  } catch {
875
541
  return void 0;
876
542
  }
877
543
  const now = this.now;
878
- for (const u of leaf.ocspServers) {
879
- let resp;
544
+ for (const u of cert.ocspServers) {
545
+ let der;
880
546
  try {
881
- const der = await this.f.do("POST", u, "application/ocsp-request", req);
882
- if (der.length > ONLINE_OCSP_MAX) continue;
883
- resp = await parseOCSPFrom(der, intValue(leaf.serial), issuer);
547
+ der = await this.f.do("POST", u, "application/ocsp-request", req);
884
548
  } catch {
885
549
  continue;
886
550
  }
887
- if (resp.serial !== intValue(leaf.serial)) continue;
551
+ if (der.length > ONLINE_OCSP_MAX) continue;
552
+ const resp = await ocspFor(der, cert, issuer);
553
+ if (!resp) continue;
888
554
  const rc = resp.certificate;
889
- if (rc && !sameCertKey(rc, issuer) && (!responderOK(resp, issuer) || !validAt(rc, now))) continue;
890
- if (after(fromPrecise(resp.thisUpdate), addMs(now, CLOCK_SKEW_MS))) continue;
891
- if (resp.nextUpdate && before(fromDate(resp.nextUpdate), now)) continue;
892
- if (resp.status === "good") return { issued: true, revoked: false };
555
+ if (rc && !sameCertKey(rc, issuer) && !validAt(rc, now)) continue;
893
556
  if (resp.status === "revoked") return { issued: true, revoked: true, revokedAt: fromPrecise(resp.revokedAt) };
557
+ if (!ocspFreshAt(resp, now, now)) continue;
558
+ if (resp.status === "good") return { issued: true, revoked: false };
894
559
  return { issued: false, revoked: false };
895
560
  }
896
561
  return void 0;
897
562
  }
898
- async crl(leaf, issuer) {
899
- for (const u of leaf.crlDPs) {
563
+ async crl(cert, issuer) {
564
+ for (const u of cert.crlDPs) {
900
565
  let crl;
901
566
  try {
902
567
  crl = parseCRL(await this.f.do("GET", u, ""));
903
568
  } catch {
904
569
  continue;
905
570
  }
906
- if (!crl || !equal(crl.rawIssuer, issuer.rawSubject) || !await crlSignedBy(crl, issuer)) continue;
907
- if (crl.nextUpdate && before(crl.nextUpdate, this.now)) continue;
908
- const serial = intValue(leaf.serial);
909
- const e = crl.revoked.find((x) => x.serial === serial);
910
- return e ? { issued: true, revoked: true, revokedAt: e.time } : { issued: true, revoked: false };
571
+ if (!crl || !await crlFor(crl, cert, issuer)) continue;
572
+ const t = crlListed(crl, cert);
573
+ if (t) return { issued: true, revoked: true, revokedAt: t };
574
+ if (freshAt(crl.thisUpdate, crl.nextUpdate, this.now, this.now)) return { issued: true, revoked: false };
911
575
  }
912
576
  return void 0;
913
577
  }
@@ -2846,7 +2510,8 @@ var MESSAGES = {
2846
2510
  not_pdf: "PDF \u0444\u0430\u0439\u043B \u0437\u0430\u0434\u0430\u0440\u0441\u0430\u043D\u0433\u04AF\u0439",
2847
2511
  encrypted: "\u0448\u0438\u0444\u0440\u043B\u044D\u0433\u0434\u0441\u044D\u043D PDF-\u0438\u0439\u0433 \u0448\u0430\u043B\u0433\u0430\u0445 \u0431\u043E\u043B\u043E\u043C\u0436\u0433\u04AF\u0439 \u2014 \u043D\u0443\u0443\u0446\u043B\u0430\u043B\u044B\u0433 \u0430\u0440\u0438\u043B\u0433\u0430\u0430\u0434 \u0434\u0430\u0445\u0438\u043D \u043E\u0440\u043E\u043B\u0434\u043E\u043D\u043E \u0443\u0443",
2848
2512
  no_signature: "\u0431\u0430\u0440\u0438\u043C\u0442\u0430\u0434 \u0446\u0430\u0445\u0438\u043C \u0433\u0430\u0440\u044B\u043D \u04AF\u0441\u044D\u0433 \u0430\u043B\u0433\u0430",
2849
- too_many: "\u0431\u0430\u0440\u0438\u043C\u0442\u0430\u0434 \u0445\u044D\u0442 \u043E\u043B\u043E\u043D \u0433\u0430\u0440\u044B\u043D \u04AF\u0441\u044D\u0433 \u0431\u0430\u0439\u043D\u0430"
2513
+ too_many: "\u0431\u0430\u0440\u0438\u043C\u0442\u0430\u0434 \u0445\u044D\u0442 \u043E\u043B\u043E\u043D \u0433\u0430\u0440\u044B\u043D \u04AF\u0441\u044D\u0433 \u0431\u0430\u0439\u043D\u0430",
2514
+ structure: "PDF-\u0438\u0439\u043D \u0431\u04AF\u0442\u044D\u0446 \u0445\u044D\u0442 \u0433\u04AF\u043D \u0434\u0430\u0432\u0445\u0430\u0440\u043B\u0430\u0441\u0430\u043D \u044D\u0441\u0432\u044D\u043B \u0445\u044D\u0442 \u0442\u04E9\u0432\u04E9\u0433\u0442\u044D\u0439"
2850
2515
  };
2851
2516
  var ExtractError = class extends Error {
2852
2517
  constructor(code) {
@@ -2855,10 +2520,16 @@ var ExtractError = class extends Error {
2855
2520
  }
2856
2521
  code;
2857
2522
  };
2858
- var ENCRYPT_REF = /\/Encrypt\s+\d+\s+\d+\s+R/;
2523
+ var ENCRYPT_REF = /\/Encrypt(?:\s+\d+\s+\d+\s+R|\s*<<)/;
2859
2524
  var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
2860
2525
  var AP_VERIFY_PREFIX2 = "https://ca.eidmongolia.mn/verify/";
2861
2526
  async function extract(doc, profile = "own") {
2527
+ try {
2528
+ checkStructure(doc);
2529
+ } catch (e) {
2530
+ if (e instanceof StructureError) throw new ExtractError("structure");
2531
+ throw e;
2532
+ }
2862
2533
  let rdr;
2863
2534
  try {
2864
2535
  rdr = new Reader(doc);
@@ -3440,10 +3111,10 @@ async function validatePdf(pdf, opts = {}) {
3440
3111
  }
3441
3112
  const now = fromDate(opts.time ?? /* @__PURE__ */ new Date());
3442
3113
  const dss = ex.sigs.find((s) => s.dss)?.dss;
3443
- let status = new DSSStatus(dss, issuers);
3114
+ let status = new DSSStatus(dss, now);
3444
3115
  if (!opts.offline) {
3445
3116
  const fetchFn = opts.fetch ?? globalThis.fetch;
3446
- status = await OnlineStatus.create(new Fetcher(fetchFn, isBrowser(), opts.signal), ex.sigs, issuers, status, now);
3117
+ status = new OnlineStatus(new Fetcher(fetchFn, isBrowser(), opts.signal), status, now);
3447
3118
  }
3448
3119
  const ev = new Evaluator({ roots, intermediates: [...embedded(INTERMEDIATES), ...extraInter], issuers, status, now });
3449
3120
  const rep = { indication: "valid", signatures: [] };