@gerege-systems/eid-mongolia-sdk 0.20.1 → 0.22.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/README.md +26 -3
- package/bin/convert-assets.mjs +27 -1
- package/dist/browser.js +1 -1
- package/dist/{chunk-JV4NR4LG.js → chunk-ISTWNY6N.js} +460 -39
- package/dist/{chunk-J7EG3VNQ.js → chunk-T3QLOY6J.js} +369 -50
- package/dist/{chunk-D5NPS7ET.js → chunk-ZB7LEWA5.js} +1 -1
- package/dist/convert.js +16 -25
- package/dist/index.cjs +61 -4
- package/dist/index.d.cts +11 -2
- package/dist/index.d.ts +11 -2
- package/dist/index.js +6 -2
- package/dist/pades.cjs +589 -71
- package/dist/pades.d.cts +7 -1
- package/dist/pades.d.ts +7 -1
- package/dist/pades.js +24 -4
- package/dist/validate.cjs +663 -284
- package/dist/validate.js +101 -430
- package/package.json +2 -2
package/dist/validate.cjs
CHANGED
|
@@ -620,108 +620,6 @@ var HASHES = {
|
|
|
620
620
|
[OID.sha512]: "SHA-512"
|
|
621
621
|
};
|
|
622
622
|
|
|
623
|
-
// src/pades/ltv.ts
|
|
624
|
-
var MAX_TSA_SKEW_MS = 10 * 6e4;
|
|
625
|
-
async function ocspRequest(c, issuer) {
|
|
626
|
-
const keyHash = await sha1(issuer.publicKey);
|
|
627
|
-
const nameHash = await sha1(issuer.rawSubject);
|
|
628
|
-
const certID = seq(seq(oid(OID.sha1), nullDer()), octet(nameHash), octet(keyHash), Uint8Array.of(TAG.INTEGER, ...lenOf(c.serial.length), ...c.serial));
|
|
629
|
-
return seq(seq(seq(seq(certID))));
|
|
630
|
-
}
|
|
631
|
-
function lenOf(n) {
|
|
632
|
-
if (n < 128) return [n];
|
|
633
|
-
const out = [];
|
|
634
|
-
for (let x = n; x > 0; x = Math.floor(x / 256)) out.unshift(x & 255);
|
|
635
|
-
return [128 | out.length, ...out];
|
|
636
|
-
}
|
|
637
|
-
var OID_OCSP_BASIC = "1.3.6.1.5.5.7.48.1.1";
|
|
638
|
-
async function parseOCSP(der, serial, issuer) {
|
|
639
|
-
const top = children(expect(parseOne(der), TAG.SEQUENCE, "OCSPResponse"));
|
|
640
|
-
if (expect(top[0], TAG.ENUMERATED, "status").value[0] !== 0) throw new Error("OCSP response status");
|
|
641
|
-
const rb = children(expect(children(expect(top[1], 160, "responseBytes"))[0], TAG.SEQUENCE, "ResponseBytes"));
|
|
642
|
-
if (oidString(expect(rb[0], TAG.OID, "responseType").value) !== OID_OCSP_BASIC) throw new Error("bad OCSP response type");
|
|
643
|
-
const basic = children(expect(parseOne(expect(rb[1], TAG.OCTET_STRING, "response").value), TAG.SEQUENCE, "BasicOCSPResponse"));
|
|
644
|
-
const tbs = expect(basic[0], TAG.SEQUENCE, "tbsResponseData");
|
|
645
|
-
const sigAlg = expect(basic[1], TAG.SEQUENCE, "sigAlg");
|
|
646
|
-
const sigBits = expect(basic[2], TAG.BIT_STRING, "signature").value.subarray(1);
|
|
647
|
-
const tf = children(tbs);
|
|
648
|
-
let i = 0;
|
|
649
|
-
if (tf[0]?.tag === 160) i++;
|
|
650
|
-
const rid = tf[i++];
|
|
651
|
-
if (rid.tag !== 161 && rid.tag !== 162) throw new Error("invalid responder id tag");
|
|
652
|
-
i++;
|
|
653
|
-
const responses = children(expect(tf[i], TAG.SEQUENCE, "responses"));
|
|
654
|
-
let single;
|
|
655
|
-
if (serial !== void 0) {
|
|
656
|
-
if (responses.length === 0) throw new Error("OCSP response contains bad number of responses");
|
|
657
|
-
for (const r of responses) {
|
|
658
|
-
const f = children(r);
|
|
659
|
-
const cid2 = children(f[0]);
|
|
660
|
-
if (intValue(expect(cid2[3], TAG.INTEGER, "serial").value) === serial) {
|
|
661
|
-
single = f;
|
|
662
|
-
break;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
if (!single) throw new Error("no response matching the supplied certificate");
|
|
666
|
-
} else {
|
|
667
|
-
if (responses.length !== 1) throw new Error("OCSP response contains bad number of responses");
|
|
668
|
-
single = children(responses[0]);
|
|
669
|
-
}
|
|
670
|
-
let certificate;
|
|
671
|
-
if (basic[3]?.tag === 160) {
|
|
672
|
-
const certs = children(expect(children(basic[3])[0], TAG.SEQUENCE, "certs"));
|
|
673
|
-
if (certs[0]) certificate = parseCertificate(certs[0].raw);
|
|
674
|
-
}
|
|
675
|
-
if (certificate) {
|
|
676
|
-
if (!await verifySignature(certificate, sigAlg, tbs.raw, sigBits)) throw new Error("bad signature on embedded certificate");
|
|
677
|
-
if (issuer && !await checkSignature(issuer, certificate.sigAlg, certificate.tbs, certificate.signature)) throw new Error("bad OCSP signature");
|
|
678
|
-
} else if (issuer && !await verifySignature(issuer, sigAlg, tbs.raw, sigBits)) {
|
|
679
|
-
throw new Error("bad OCSP signature");
|
|
680
|
-
}
|
|
681
|
-
const cid = children(single[0]);
|
|
682
|
-
const hashAlg = oidString(children(cid[0])[0].value);
|
|
683
|
-
if (![OID.sha1, OID.sha256, OID.sha384, OID.sha512].includes(hashAlg)) throw new Error("unsupported issuer hash algorithm");
|
|
684
|
-
const st = single[1];
|
|
685
|
-
const status = st.tag === 128 ? "good" : st.tag === 161 ? "revoked" : st.tag === 130 ? "unknown" : void 0;
|
|
686
|
-
if (!status) throw new Error("OCSP certStatus");
|
|
687
|
-
const revokedAt = status === "revoked" ? parsePreciseTime(expect(children(st)[0], TAG.GeneralizedTime, "revocationTime")) : void 0;
|
|
688
|
-
const thisUpdate = parsePreciseTime(expect(single[2], TAG.GeneralizedTime, "thisUpdate"));
|
|
689
|
-
let nextUpdate;
|
|
690
|
-
for (const x of single.slice(3)) {
|
|
691
|
-
if (x.tag === 160) nextUpdate = parsePreciseTime(children(x)[0]).date;
|
|
692
|
-
if (x.tag === 161) {
|
|
693
|
-
for (const ext of children(children(x)[0])) {
|
|
694
|
-
const e = children(ext);
|
|
695
|
-
if (e[1]?.tag === TAG.BOOLEAN && e[1].value[0] !== 0) throw new Error("unsupported critical extension");
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
return { status, serial: intValue(expect(cid[3], TAG.INTEGER, "serial").value), thisUpdate, nextUpdate, revokedAt, certificate };
|
|
700
|
-
}
|
|
701
|
-
function sameCertKey(a, b) {
|
|
702
|
-
return equal(a.raw, b.raw) || equal(a.rawSubject, b.rawSubject) && equal(a.spki, b.spki);
|
|
703
|
-
}
|
|
704
|
-
async function parseOCSPFrom(der, serial, issuer) {
|
|
705
|
-
try {
|
|
706
|
-
return await parseOCSP(der, serial, issuer);
|
|
707
|
-
} catch (e) {
|
|
708
|
-
let alt;
|
|
709
|
-
try {
|
|
710
|
-
alt = await parseOCSP(der, serial, void 0);
|
|
711
|
-
} catch {
|
|
712
|
-
throw e;
|
|
713
|
-
}
|
|
714
|
-
if (alt.certificate && sameCertKey(alt.certificate, issuer) && await ocspSignedBy(der, issuer)) return alt;
|
|
715
|
-
throw e;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
async function ocspSignedBy(der, k) {
|
|
719
|
-
const top = children(parseOne(der));
|
|
720
|
-
const rb = children(children(top[1])[0]);
|
|
721
|
-
const basic = children(parseOne(rb[1].value));
|
|
722
|
-
return verifySignature(k, basic[1], basic[0].raw, basic[2].value.subarray(1));
|
|
723
|
-
}
|
|
724
|
-
|
|
725
623
|
// src/pades/validate/gofmt.ts
|
|
726
624
|
var ZERO_TIME = { nanos: 0 };
|
|
727
625
|
function fromPrecise(t) {
|
|
@@ -1015,6 +913,229 @@ function subjectSerialNumber(rawSubject) {
|
|
|
1015
913
|
return sn;
|
|
1016
914
|
}
|
|
1017
915
|
|
|
916
|
+
// src/pades/revocation.ts
|
|
917
|
+
var REVOCATION_MAX_AGE_MS = 24 * 36e5;
|
|
918
|
+
var REVOCATION_CLOCK_SKEW_MS = 5 * 6e4;
|
|
919
|
+
function freshAt(thisUpdate, nextUpdate, at, now) {
|
|
920
|
+
if (isZero(thisUpdate) || after(thisUpdate, addMs(now, REVOCATION_CLOCK_SKEW_MS))) return false;
|
|
921
|
+
if (!isZero(nextUpdate) && before(nextUpdate, addMs(at, -REVOCATION_CLOCK_SKEW_MS))) return false;
|
|
922
|
+
if (!before(thisUpdate, addMs(at, -REVOCATION_MAX_AGE_MS))) return true;
|
|
923
|
+
return !isZero(nextUpdate) && !after(thisUpdate, at);
|
|
924
|
+
}
|
|
925
|
+
var OCSP_HASHES = {
|
|
926
|
+
"1.3.14.3.2.26": "SHA-1",
|
|
927
|
+
"2.16.840.1.101.3.4.2.1": "SHA-256",
|
|
928
|
+
"2.16.840.1.101.3.4.2.2": "SHA-384",
|
|
929
|
+
"2.16.840.1.101.3.4.2.3": "SHA-512"
|
|
930
|
+
};
|
|
931
|
+
async function ocspCertIDMatches(der, cert, issuer) {
|
|
932
|
+
try {
|
|
933
|
+
const top = children(expect(readNode(der), TAG.SEQUENCE, "OCSPResponse"));
|
|
934
|
+
const st = expect(top[0], TAG.ENUMERATED, "status");
|
|
935
|
+
if (st.value.length !== 1 || st.value[0] !== 0) return false;
|
|
936
|
+
const rb = children(expect(children(expect(top[1], 160, "responseBytes"))[0], TAG.SEQUENCE, "ResponseBytes"));
|
|
937
|
+
expect(rb[0], TAG.OID, "responseType");
|
|
938
|
+
const basic = children(expect(readNode(expect(rb[1], TAG.OCTET_STRING, "response").value), TAG.SEQUENCE, "BasicOCSPResponse"));
|
|
939
|
+
const tf = children(expect(basic[0], TAG.SEQUENCE, "tbsResponseData"));
|
|
940
|
+
let i = 0;
|
|
941
|
+
if (tf[i]?.tag === 160) i++;
|
|
942
|
+
if (!tf[i++]) return false;
|
|
943
|
+
expect(tf[i++], TAG.GeneralizedTime, "producedAt");
|
|
944
|
+
const singles = children(expect(tf[i], TAG.SEQUENCE, "responses"));
|
|
945
|
+
const serial = intValue(cert.serial);
|
|
946
|
+
let found = false;
|
|
947
|
+
for (const single of singles) {
|
|
948
|
+
const cid = children(expect(children(expect(single, TAG.SEQUENCE, "SingleResponse"))[0], TAG.SEQUENCE, "CertID"));
|
|
949
|
+
const alg = oidString(expect(children(expect(cid[0], TAG.SEQUENCE, "hashAlgorithm"))[0], TAG.OID, "hash").value);
|
|
950
|
+
const nameHash = expect(cid[1], TAG.OCTET_STRING, "issuerNameHash").value;
|
|
951
|
+
const keyHash = expect(cid[2], TAG.OCTET_STRING, "issuerKeyHash").value;
|
|
952
|
+
if (intValue(expect(cid[3], TAG.INTEGER, "serial").value) !== serial) continue;
|
|
953
|
+
const h = OCSP_HASHES[alg];
|
|
954
|
+
if (!h) return false;
|
|
955
|
+
if (!equal(nameHash, await digest(h, issuer.rawSubject)) || !equal(keyHash, await digest(h, issuer.publicKey))) return false;
|
|
956
|
+
found = true;
|
|
957
|
+
}
|
|
958
|
+
return found;
|
|
959
|
+
} catch {
|
|
960
|
+
return false;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
var OID_AKI = "2.5.29.35";
|
|
964
|
+
var isTime = (n) => n !== void 0 && (n.tag === TAG.UTCTime || n.tag === TAG.GeneralizedTime);
|
|
965
|
+
function parseCRL(der) {
|
|
966
|
+
try {
|
|
967
|
+
const top = children(expect(parseOne(der), TAG.SEQUENCE, "CertificateList"));
|
|
968
|
+
const tbs = expect(top[0], TAG.SEQUENCE, "TBSCertList");
|
|
969
|
+
const sigAlg = expect(top[1], TAG.SEQUENCE, "sigAlg");
|
|
970
|
+
const sigBits = expect(top[2], TAG.BIT_STRING, "sig");
|
|
971
|
+
const f = children(tbs);
|
|
972
|
+
let i = 0;
|
|
973
|
+
if (f[i]?.tag === TAG.INTEGER) i++;
|
|
974
|
+
expect(f[i++], TAG.SEQUENCE, "signature");
|
|
975
|
+
const issuer = expect(f[i++], TAG.SEQUENCE, "issuer");
|
|
976
|
+
const tu = f[i++];
|
|
977
|
+
if (!isTime(tu)) return void 0;
|
|
978
|
+
const thisUpdate = fromPrecise(parsePreciseTime(tu));
|
|
979
|
+
let nextUpdate = { nanos: 0 };
|
|
980
|
+
if (isTime(f[i])) nextUpdate = fromPrecise(parsePreciseTime(f[i++]));
|
|
981
|
+
const revoked = [];
|
|
982
|
+
if (f[i]?.tag === TAG.SEQUENCE) {
|
|
983
|
+
for (const e of children(f[i++])) {
|
|
984
|
+
const p = children(expect(e, TAG.SEQUENCE, "entry"));
|
|
985
|
+
revoked.push({ serial: intValue(expect(p[0], TAG.INTEGER, "serial").value), time: fromPrecise(parsePreciseTime(p[1])) });
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
let authorityKeyId = new Uint8Array();
|
|
989
|
+
if (f[i]?.tag === 160) {
|
|
990
|
+
for (const ext of children(expect(children(f[i])[0], TAG.SEQUENCE, "extensions"))) {
|
|
991
|
+
const e = children(ext);
|
|
992
|
+
if (oidString(expect(e[0], TAG.OID, "extnID").value) !== OID_AKI) continue;
|
|
993
|
+
const k = children(parseOne(expect(e[e.length - 1], TAG.OCTET_STRING, "extnValue").value)).find((x) => x.tag === 128);
|
|
994
|
+
if (k) authorityKeyId = k.value;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return { rawIssuer: issuer.raw, tbs: tbs.raw, sigAlg, sig: sigBits.value.subarray(1), thisUpdate, nextUpdate, authorityKeyId, revoked };
|
|
998
|
+
} catch {
|
|
999
|
+
return void 0;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
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"]);
|
|
1003
|
+
async function crlSignedBy(crl, issuer) {
|
|
1004
|
+
if (issuer.version === 3 && !issuer.basicConstraintsValid || issuer.basicConstraintsValid && !issuer.isCA) return false;
|
|
1005
|
+
if (issuer.keyUsage !== 0 && !(issuer.keyUsage & 1 << 6)) return false;
|
|
1006
|
+
if (SHA1_SIG.has(oidString(children(crl.sigAlg)[0].value))) return false;
|
|
1007
|
+
try {
|
|
1008
|
+
return await verifySignature(issuer, crl.sigAlg, crl.tbs, crl.sig);
|
|
1009
|
+
} catch {
|
|
1010
|
+
return false;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
function subjectKeyId(c) {
|
|
1014
|
+
const v = c.extensions.find((e) => e.oid === "2.5.29.14")?.value;
|
|
1015
|
+
if (!v) return new Uint8Array();
|
|
1016
|
+
try {
|
|
1017
|
+
const n = parseOne(v);
|
|
1018
|
+
return n.tag === TAG.OCTET_STRING ? n.value : new Uint8Array();
|
|
1019
|
+
} catch {
|
|
1020
|
+
return new Uint8Array();
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
async function crlFor(crl, cert, issuer) {
|
|
1024
|
+
if (!equal(crl.rawIssuer, cert.rawIssuer) || !equal(crl.rawIssuer, issuer.rawSubject)) return false;
|
|
1025
|
+
const ski = subjectKeyId(issuer);
|
|
1026
|
+
if (crl.authorityKeyId.length > 0 && ski.length > 0 && !equal(crl.authorityKeyId, ski)) return false;
|
|
1027
|
+
return crlSignedBy(crl, issuer);
|
|
1028
|
+
}
|
|
1029
|
+
function crlListed(crl, cert) {
|
|
1030
|
+
const serial = intValue(cert.serial);
|
|
1031
|
+
return crl.revoked.find((e) => e.serial === serial)?.time;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// src/pades/ltv.ts
|
|
1035
|
+
var MAX_TSA_SKEW_MS = 10 * 6e4;
|
|
1036
|
+
async function ocspRequest(c, issuer) {
|
|
1037
|
+
const keyHash = await sha1(issuer.publicKey);
|
|
1038
|
+
const nameHash = await sha1(issuer.rawSubject);
|
|
1039
|
+
const certID = seq(seq(oid(OID.sha1), nullDer()), octet(nameHash), octet(keyHash), Uint8Array.of(TAG.INTEGER, ...lenOf(c.serial.length), ...c.serial));
|
|
1040
|
+
return seq(seq(seq(seq(certID))));
|
|
1041
|
+
}
|
|
1042
|
+
function lenOf(n) {
|
|
1043
|
+
if (n < 128) return [n];
|
|
1044
|
+
const out = [];
|
|
1045
|
+
for (let x = n; x > 0; x = Math.floor(x / 256)) out.unshift(x & 255);
|
|
1046
|
+
return [128 | out.length, ...out];
|
|
1047
|
+
}
|
|
1048
|
+
var OID_OCSP_BASIC = "1.3.6.1.5.5.7.48.1.1";
|
|
1049
|
+
async function parseOCSP(der, serial, issuer) {
|
|
1050
|
+
const top = children(expect(parseOne(der), TAG.SEQUENCE, "OCSPResponse"));
|
|
1051
|
+
if (expect(top[0], TAG.ENUMERATED, "status").value[0] !== 0) throw new Error("OCSP response status");
|
|
1052
|
+
const rb = children(expect(children(expect(top[1], 160, "responseBytes"))[0], TAG.SEQUENCE, "ResponseBytes"));
|
|
1053
|
+
if (oidString(expect(rb[0], TAG.OID, "responseType").value) !== OID_OCSP_BASIC) throw new Error("bad OCSP response type");
|
|
1054
|
+
const basic = children(expect(parseOne(expect(rb[1], TAG.OCTET_STRING, "response").value), TAG.SEQUENCE, "BasicOCSPResponse"));
|
|
1055
|
+
const tbs = expect(basic[0], TAG.SEQUENCE, "tbsResponseData");
|
|
1056
|
+
const sigAlg = expect(basic[1], TAG.SEQUENCE, "sigAlg");
|
|
1057
|
+
const sigBits = expect(basic[2], TAG.BIT_STRING, "signature").value.subarray(1);
|
|
1058
|
+
const tf = children(tbs);
|
|
1059
|
+
let i = 0;
|
|
1060
|
+
if (tf[0]?.tag === 160) i++;
|
|
1061
|
+
const rid = tf[i++];
|
|
1062
|
+
if (rid.tag !== 161 && rid.tag !== 162) throw new Error("invalid responder id tag");
|
|
1063
|
+
i++;
|
|
1064
|
+
const responses = children(expect(tf[i], TAG.SEQUENCE, "responses"));
|
|
1065
|
+
let single;
|
|
1066
|
+
if (serial !== void 0) {
|
|
1067
|
+
if (responses.length === 0) throw new Error("OCSP response contains bad number of responses");
|
|
1068
|
+
for (const r of responses) {
|
|
1069
|
+
const f = children(r);
|
|
1070
|
+
const cid2 = children(f[0]);
|
|
1071
|
+
if (intValue(expect(cid2[3], TAG.INTEGER, "serial").value) === serial) {
|
|
1072
|
+
single = f;
|
|
1073
|
+
break;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
if (!single) throw new Error("no response matching the supplied certificate");
|
|
1077
|
+
} else {
|
|
1078
|
+
if (responses.length !== 1) throw new Error("OCSP response contains bad number of responses");
|
|
1079
|
+
single = children(responses[0]);
|
|
1080
|
+
}
|
|
1081
|
+
let certificate;
|
|
1082
|
+
if (basic[3]?.tag === 160) {
|
|
1083
|
+
const certs = children(expect(children(basic[3])[0], TAG.SEQUENCE, "certs"));
|
|
1084
|
+
if (certs[0]) certificate = parseCertificate(certs[0].raw);
|
|
1085
|
+
}
|
|
1086
|
+
if (certificate) {
|
|
1087
|
+
if (!await verifySignature(certificate, sigAlg, tbs.raw, sigBits)) throw new Error("bad signature on embedded certificate");
|
|
1088
|
+
if (issuer && !await checkSignature(issuer, certificate.sigAlg, certificate.tbs, certificate.signature)) throw new Error("bad OCSP signature");
|
|
1089
|
+
} else if (issuer && !await verifySignature(issuer, sigAlg, tbs.raw, sigBits)) {
|
|
1090
|
+
throw new Error("bad OCSP signature");
|
|
1091
|
+
}
|
|
1092
|
+
const cid = children(single[0]);
|
|
1093
|
+
const hashAlg = oidString(children(cid[0])[0].value);
|
|
1094
|
+
if (![OID.sha1, OID.sha256, OID.sha384, OID.sha512].includes(hashAlg)) throw new Error("unsupported issuer hash algorithm");
|
|
1095
|
+
const st = single[1];
|
|
1096
|
+
const status = st.tag === 128 ? "good" : st.tag === 161 ? "revoked" : st.tag === 130 ? "unknown" : void 0;
|
|
1097
|
+
if (!status) throw new Error("OCSP certStatus");
|
|
1098
|
+
const revokedAt = status === "revoked" ? parsePreciseTime(expect(children(st)[0], TAG.GeneralizedTime, "revocationTime")) : void 0;
|
|
1099
|
+
const thisUpdate = parsePreciseTime(expect(single[2], TAG.GeneralizedTime, "thisUpdate"));
|
|
1100
|
+
let nextUpdatePrecise;
|
|
1101
|
+
for (const x of single.slice(3)) {
|
|
1102
|
+
if (x.tag === 160) nextUpdatePrecise = parsePreciseTime(children(x)[0]);
|
|
1103
|
+
if (x.tag === 161) {
|
|
1104
|
+
for (const ext of children(children(x)[0])) {
|
|
1105
|
+
const e = children(ext);
|
|
1106
|
+
if (e[1]?.tag === TAG.BOOLEAN && e[1].value[0] !== 0) throw new Error("unsupported critical extension");
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
return { status, serial: intValue(expect(cid[3], TAG.INTEGER, "serial").value), thisUpdate, nextUpdate: nextUpdatePrecise?.date, nextUpdatePrecise, revokedAt, certificate };
|
|
1111
|
+
}
|
|
1112
|
+
function sameCertKey(a, b) {
|
|
1113
|
+
return equal(a.raw, b.raw) || equal(a.rawSubject, b.rawSubject) && equal(a.spki, b.spki);
|
|
1114
|
+
}
|
|
1115
|
+
async function parseOCSPFrom(der, serial, issuer) {
|
|
1116
|
+
try {
|
|
1117
|
+
return await parseOCSP(der, serial, issuer);
|
|
1118
|
+
} catch (e) {
|
|
1119
|
+
let alt;
|
|
1120
|
+
try {
|
|
1121
|
+
alt = await parseOCSP(der, serial, void 0);
|
|
1122
|
+
} catch {
|
|
1123
|
+
throw e;
|
|
1124
|
+
}
|
|
1125
|
+
if (alt.certificate && sameCertKey(alt.certificate, issuer) && await ocspSignedBy(der, issuer)) return alt;
|
|
1126
|
+
throw e;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
async function ocspSignedBy(der, k) {
|
|
1130
|
+
const top = children(parseOne(der));
|
|
1131
|
+
const rb = children(children(top[1])[0]);
|
|
1132
|
+
const basic = children(parseOne(rb[1].value));
|
|
1133
|
+
return verifySignature(k, basic[1], basic[0].raw, basic[2].value.subarray(1));
|
|
1134
|
+
}
|
|
1135
|
+
function ocspFreshAt(resp, at, now) {
|
|
1136
|
+
return freshAt(fromPrecise(resp.thisUpdate), resp.nextUpdatePrecise ? fromPrecise(resp.nextUpdatePrecise) : ZERO_TIME, at, now);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1018
1139
|
// src/pades/validate/evaluate.ts
|
|
1019
1140
|
var PROBLEMS = {
|
|
1020
1141
|
integrity: "integrity_failed",
|
|
@@ -1030,7 +1151,6 @@ var PROBLEMS = {
|
|
|
1030
1151
|
modifiedAfter: "modified_after_signing",
|
|
1031
1152
|
timestampInvalid: "timestamp_invalid"
|
|
1032
1153
|
};
|
|
1033
|
-
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"]);
|
|
1034
1154
|
async function checkSigFrom(c, parent) {
|
|
1035
1155
|
if (parent.version === 3 && !parent.basicConstraintsValid || parent.basicConstraintsValid && !parent.isCA) return false;
|
|
1036
1156
|
if (parent.keyUsage !== 0 && !(parent.keyUsage & 1 << 5)) return false;
|
|
@@ -1168,7 +1288,7 @@ var Evaluator = class {
|
|
|
1168
1288
|
isIssuer(c) {
|
|
1169
1289
|
return this.cfg.issuers.some((is) => equal(c.rawSubject, is.rawSubject) && equal(c.spki, is.spki));
|
|
1170
1290
|
}
|
|
1171
|
-
async certInfo(c, chain) {
|
|
1291
|
+
async certInfo(c, chain, at) {
|
|
1172
1292
|
const info = {
|
|
1173
1293
|
common_name: commonName(c.rawSubject),
|
|
1174
1294
|
subject: nameString(c.rawSubject),
|
|
@@ -1185,7 +1305,7 @@ var Evaluator = class {
|
|
|
1185
1305
|
if (!this.cfg.status) return info;
|
|
1186
1306
|
let st;
|
|
1187
1307
|
try {
|
|
1188
|
-
st = await this.cfg.status.certStatus(
|
|
1308
|
+
st = await this.cfg.status.certStatus(c, chain[1], at);
|
|
1189
1309
|
} catch {
|
|
1190
1310
|
return info;
|
|
1191
1311
|
}
|
|
@@ -1273,7 +1393,7 @@ var Evaluator = class {
|
|
|
1273
1393
|
s.chain_trusted = trusted;
|
|
1274
1394
|
s.chain = chain ? chain.map((c) => commonName(c.rawSubject)) : [commonName(leaf.rawSubject)];
|
|
1275
1395
|
this.lastRevokedAt = void 0;
|
|
1276
|
-
const info = await this.certInfo(leaf, chain);
|
|
1396
|
+
const info = await this.certInfo(leaf, chain, at);
|
|
1277
1397
|
s.certificate = info;
|
|
1278
1398
|
if (!trusted) s.problems.push(PROBLEMS.untrusted);
|
|
1279
1399
|
if (!["sign", "seal", "document_signer"].includes(info.usage)) s.problems.push(PROBLEMS.keyUsage);
|
|
@@ -1292,7 +1412,7 @@ var Evaluator = class {
|
|
|
1292
1412
|
}
|
|
1293
1413
|
if (cades && r.signingTimeAttr && info.issued_by_eid) s.problems.push(PROBLEMS.malformed);
|
|
1294
1414
|
if (!r.coversWhole && !r.laterSignaturesOnly) s.problems.push(PROBLEMS.modifiedAfter);
|
|
1295
|
-
if (trusted) s.ltv = await ltvCoverage(r.dss, at, [chain, tsChain ?? []]);
|
|
1415
|
+
if (trusted) s.ltv = await ltvCoverage(r.dss, at, this.cfg.now, [chain, tsChain ?? []]);
|
|
1296
1416
|
if (cades) {
|
|
1297
1417
|
s.signature_level = signingTime && s.ltv?.complete ? "PAdES-BASELINE-LT" : signingTime ? "PAdES-BASELINE-T" : "PAdES-BASELINE-B";
|
|
1298
1418
|
}
|
|
@@ -1374,9 +1494,8 @@ function usageOf(c) {
|
|
|
1374
1494
|
if (ds && known.includes("1.3.6.1.5.5.7.3.2")) return "auth";
|
|
1375
1495
|
return "other";
|
|
1376
1496
|
}
|
|
1377
|
-
var LTV_FRESHNESS_MS = 24 * 36e5;
|
|
1378
1497
|
var OID_OCSP_NOCHECK = "1.3.6.1.5.5.7.48.1.5";
|
|
1379
|
-
async function ltvCoverage(dss, at, chains) {
|
|
1498
|
+
async function ltvCoverage(dss, at, now, chains) {
|
|
1380
1499
|
const out = { complete: false, missing: [] };
|
|
1381
1500
|
const crls = [];
|
|
1382
1501
|
for (const der of dss?.crls ?? []) {
|
|
@@ -1390,13 +1509,13 @@ async function ltvCoverage(dss, at, chains) {
|
|
|
1390
1509
|
const issuer = chain[i + 1];
|
|
1391
1510
|
if (seen.some((x) => equal(x, cert.raw))) continue;
|
|
1392
1511
|
seen.push(cert.raw);
|
|
1393
|
-
if (!await covered(cert, issuer, at, dss?.ocsps ?? [], crls)) out.missing.push(commonName(cert.rawSubject));
|
|
1512
|
+
if (!await covered(cert, issuer, at, now, dss?.ocsps ?? [], crls)) out.missing.push(commonName(cert.rawSubject));
|
|
1394
1513
|
}
|
|
1395
1514
|
}
|
|
1396
1515
|
out.complete = out.missing.length === 0 && seen.length > 0;
|
|
1397
1516
|
return out;
|
|
1398
1517
|
}
|
|
1399
|
-
async function covered(cert, issuer, at, ocsps, crls) {
|
|
1518
|
+
async function covered(cert, issuer, at, now, ocsps, crls) {
|
|
1400
1519
|
for (const der of ocsps) {
|
|
1401
1520
|
let resp;
|
|
1402
1521
|
try {
|
|
@@ -1404,98 +1523,64 @@ async function covered(cert, issuer, at, ocsps, crls) {
|
|
|
1404
1523
|
} catch {
|
|
1405
1524
|
continue;
|
|
1406
1525
|
}
|
|
1407
|
-
if (resp.status !== "good") continue;
|
|
1526
|
+
if (resp.status !== "good" || !await ocspCertIDMatches(der, cert, issuer)) continue;
|
|
1408
1527
|
if (resp.certificate && !equal(resp.certificate.raw, issuer.raw) && !resp.certificate.extensions.some((e) => e.oid === OID_OCSP_NOCHECK)) continue;
|
|
1409
|
-
|
|
1410
|
-
if (!before(tu, addMs(at, -LTV_FRESHNESS_MS))) return true;
|
|
1411
|
-
if (resp.nextUpdate && !before(fromDate(resp.nextUpdate), at) && !after(tu, at)) return true;
|
|
1528
|
+
if (ocspFreshAt(resp, at, now)) return true;
|
|
1412
1529
|
}
|
|
1413
1530
|
for (const crl of crls) {
|
|
1414
|
-
if (!
|
|
1415
|
-
if (
|
|
1416
|
-
const serial = intValue(cert.serial);
|
|
1417
|
-
if (crl.revoked.some((e) => e.serial === serial)) continue;
|
|
1531
|
+
if (!await crlFor(crl, cert, issuer) || !freshAt(crl.thisUpdate, crl.nextUpdate, at, now)) continue;
|
|
1532
|
+
if (crlListed(crl, cert)) continue;
|
|
1418
1533
|
return true;
|
|
1419
1534
|
}
|
|
1420
1535
|
return false;
|
|
1421
1536
|
}
|
|
1422
|
-
function
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
const tbs = expect(top[0], TAG.SEQUENCE, "TBSCertList");
|
|
1426
|
-
const sigAlg = expect(top[1], TAG.SEQUENCE, "sigAlg");
|
|
1427
|
-
const sigBits = expect(top[2], TAG.BIT_STRING, "sig");
|
|
1428
|
-
const f = children(tbs);
|
|
1429
|
-
let i = 0;
|
|
1430
|
-
if (f[i]?.tag === TAG.INTEGER) i++;
|
|
1431
|
-
expect(f[i++], TAG.SEQUENCE, "signature");
|
|
1432
|
-
const issuer = expect(f[i++], TAG.SEQUENCE, "issuer");
|
|
1433
|
-
const tu = f[i++];
|
|
1434
|
-
if (!tu || tu.tag !== TAG.UTCTime && tu.tag !== TAG.GeneralizedTime) return void 0;
|
|
1435
|
-
let nextUpdate;
|
|
1436
|
-
if (f[i] && (f[i].tag === TAG.UTCTime || f[i].tag === TAG.GeneralizedTime)) nextUpdate = fromPrecise(parsePreciseTime(f[i++]));
|
|
1437
|
-
const revoked = [];
|
|
1438
|
-
if (f[i]?.tag === TAG.SEQUENCE) {
|
|
1439
|
-
for (const e of children(f[i++])) {
|
|
1440
|
-
const p = children(expect(e, TAG.SEQUENCE, "entry"));
|
|
1441
|
-
revoked.push({ serial: intValue(expect(p[0], TAG.INTEGER, "serial").value), time: fromPrecise(parsePreciseTime(p[1])) });
|
|
1442
|
-
}
|
|
1443
|
-
}
|
|
1444
|
-
return { rawIssuer: issuer.raw, tbs: tbs.raw, sigAlg, sig: sigBits.value.subarray(1), nextUpdate, revoked };
|
|
1445
|
-
} catch {
|
|
1446
|
-
return void 0;
|
|
1447
|
-
}
|
|
1537
|
+
function responderOK(resp, issuer) {
|
|
1538
|
+
if (!resp.certificate || equal(resp.certificate.raw, issuer.raw)) return true;
|
|
1539
|
+
return resp.certificate.extKeyUsage.includes(OID.kpOCSPSigning);
|
|
1448
1540
|
}
|
|
1449
|
-
async function
|
|
1450
|
-
|
|
1451
|
-
if (issuer.keyUsage !== 0 && !(issuer.keyUsage & 1 << 6)) return false;
|
|
1452
|
-
if (SHA1_SIG.has(oidString(children(crl.sigAlg)[0].value))) return false;
|
|
1541
|
+
async function ocspFor(der, cert, issuer) {
|
|
1542
|
+
let resp;
|
|
1453
1543
|
try {
|
|
1454
|
-
|
|
1544
|
+
resp = await parseOCSPFrom(der, intValue(cert.serial), issuer);
|
|
1455
1545
|
} catch {
|
|
1456
|
-
return
|
|
1546
|
+
return void 0;
|
|
1457
1547
|
}
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
if (!resp.certificate || equal(resp.certificate.raw, issuer.raw)) return true;
|
|
1461
|
-
return resp.certificate.extKeyUsage.includes(OID.kpOCSPSigning);
|
|
1548
|
+
if (resp.serial !== intValue(cert.serial) || !await ocspCertIDMatches(der, cert, issuer) || !responderOK(resp, issuer)) return void 0;
|
|
1549
|
+
return resp;
|
|
1462
1550
|
}
|
|
1463
1551
|
var DSSStatus = class {
|
|
1464
|
-
constructor(dss,
|
|
1552
|
+
constructor(dss, now) {
|
|
1465
1553
|
this.dss = dss;
|
|
1466
|
-
this.
|
|
1554
|
+
this.now = now;
|
|
1467
1555
|
}
|
|
1468
1556
|
dss;
|
|
1469
|
-
|
|
1470
|
-
async certStatus(
|
|
1557
|
+
now;
|
|
1558
|
+
async certStatus(cert, issuer, at) {
|
|
1559
|
+
let revoked;
|
|
1560
|
+
const markRevoked = (t) => {
|
|
1561
|
+
if (!revoked || before(t, revoked)) revoked = t;
|
|
1562
|
+
};
|
|
1563
|
+
let good = false;
|
|
1471
1564
|
for (const der of this.dss?.ocsps ?? []) {
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
} catch {
|
|
1477
|
-
continue;
|
|
1478
|
-
}
|
|
1479
|
-
if (resp.serial !== serial || !responderOK(resp, iss)) continue;
|
|
1480
|
-
if (resp.status === "good") return { issued: true, revoked: false };
|
|
1481
|
-
if (resp.status === "revoked") return { issued: true, revoked: true, revokedAt: fromPrecise(resp.revokedAt) };
|
|
1482
|
-
}
|
|
1565
|
+
const resp = await ocspFor(der, cert, issuer);
|
|
1566
|
+
if (!resp) continue;
|
|
1567
|
+
if (resp.status === "revoked") markRevoked(fromPrecise(resp.revokedAt));
|
|
1568
|
+
else if (resp.status === "good") good ||= ocspFreshAt(resp, at, this.now);
|
|
1483
1569
|
}
|
|
1484
1570
|
for (const der of this.dss?.crls ?? []) {
|
|
1485
1571
|
const crl = parseCRL(der);
|
|
1486
|
-
if (!crl) continue;
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
return e ? { issued: true, revoked: true, revokedAt: e.time } : { issued: true, revoked: false };
|
|
1491
|
-
}
|
|
1572
|
+
if (!crl || !await crlFor(crl, cert, issuer)) continue;
|
|
1573
|
+
const t = crlListed(crl, cert);
|
|
1574
|
+
if (t) markRevoked(t);
|
|
1575
|
+
else good ||= freshAt(crl.thisUpdate, crl.nextUpdate, at, this.now);
|
|
1492
1576
|
}
|
|
1577
|
+
if (revoked) return { issued: true, revoked: true, revokedAt: revoked };
|
|
1578
|
+
if (good) return { issued: true, revoked: false };
|
|
1493
1579
|
return { issued: false, revoked: false };
|
|
1494
1580
|
}
|
|
1495
1581
|
};
|
|
1496
1582
|
var ONLINE_OCSP_MAX = 16 << 10;
|
|
1497
|
-
var
|
|
1498
|
-
var OnlineStatus = class _OnlineStatus {
|
|
1583
|
+
var OnlineStatus = class {
|
|
1499
1584
|
constructor(f, fallback, now) {
|
|
1500
1585
|
this.f = f;
|
|
1501
1586
|
this.fallback = fallback;
|
|
@@ -1504,89 +1589,59 @@ var OnlineStatus = class _OnlineStatus {
|
|
|
1504
1589
|
f;
|
|
1505
1590
|
fallback;
|
|
1506
1591
|
now;
|
|
1507
|
-
leaves = /* @__PURE__ */ new Map();
|
|
1508
1592
|
cache = /* @__PURE__ */ new Map();
|
|
1509
|
-
|
|
1510
|
-
const
|
|
1511
|
-
for (const r of raws) {
|
|
1512
|
-
let leaf;
|
|
1513
|
-
try {
|
|
1514
|
-
leaf = parseCertificate(r.leaf ?? new Uint8Array());
|
|
1515
|
-
} catch {
|
|
1516
|
-
continue;
|
|
1517
|
-
}
|
|
1518
|
-
const key = intValue(leaf.serial).toString(16);
|
|
1519
|
-
const prev = s.leaves.get(key);
|
|
1520
|
-
if (prev) {
|
|
1521
|
-
if (!equal(prev.leaf.raw, leaf.raw)) prev.ambiguous = true;
|
|
1522
|
-
continue;
|
|
1523
|
-
}
|
|
1524
|
-
let issuer;
|
|
1525
|
-
for (const is of issuers) {
|
|
1526
|
-
if (equal(leaf.rawIssuer, is.rawSubject) && await checkSigFrom(leaf, is)) {
|
|
1527
|
-
issuer = is;
|
|
1528
|
-
break;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
if (issuer) s.leaves.set(key, { leaf, issuer, ambiguous: false });
|
|
1532
|
-
}
|
|
1533
|
-
return s;
|
|
1534
|
-
}
|
|
1535
|
-
async certStatus(serial) {
|
|
1536
|
-
const key = serial.toString(16);
|
|
1593
|
+
async certStatus(cert, issuer, at) {
|
|
1594
|
+
const key = toHex(cert.raw) + ":" + toHex(issuer.raw);
|
|
1537
1595
|
const hit = this.cache.get(key);
|
|
1538
1596
|
if (hit) return hit;
|
|
1539
|
-
|
|
1540
|
-
if (
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
if (!this.fallback) return { issued: false, revoked: false };
|
|
1544
|
-
st = await this.fallback.certStatus(serial);
|
|
1597
|
+
let st = await this.ocsp(cert, issuer) ?? await this.crl(cert, issuer);
|
|
1598
|
+
if (this.fallback && (!st || !st.revoked)) {
|
|
1599
|
+
const fst = await this.fallback.certStatus(cert, issuer, at);
|
|
1600
|
+
if (!st || fst.revoked) st = fst;
|
|
1545
1601
|
}
|
|
1602
|
+
st ??= { issued: false, revoked: false };
|
|
1546
1603
|
this.cache.set(key, st);
|
|
1547
1604
|
return st;
|
|
1548
1605
|
}
|
|
1549
|
-
async ocsp(
|
|
1606
|
+
async ocsp(cert, issuer) {
|
|
1550
1607
|
let req;
|
|
1551
1608
|
try {
|
|
1552
|
-
req = await ocspRequest(
|
|
1609
|
+
req = await ocspRequest(cert, issuer);
|
|
1553
1610
|
} catch {
|
|
1554
1611
|
return void 0;
|
|
1555
1612
|
}
|
|
1556
1613
|
const now = this.now;
|
|
1557
|
-
for (const u of
|
|
1558
|
-
let
|
|
1614
|
+
for (const u of cert.ocspServers) {
|
|
1615
|
+
let der;
|
|
1559
1616
|
try {
|
|
1560
|
-
|
|
1561
|
-
if (der.length > ONLINE_OCSP_MAX) continue;
|
|
1562
|
-
resp = await parseOCSPFrom(der, intValue(leaf.serial), issuer);
|
|
1617
|
+
der = await this.f.do("POST", u, "application/ocsp-request", req);
|
|
1563
1618
|
} catch {
|
|
1564
1619
|
continue;
|
|
1565
1620
|
}
|
|
1566
|
-
if (
|
|
1621
|
+
if (der.length > ONLINE_OCSP_MAX) continue;
|
|
1622
|
+
const resp = await ocspFor(der, cert, issuer);
|
|
1623
|
+
if (!resp) continue;
|
|
1567
1624
|
const rc = resp.certificate;
|
|
1568
|
-
if (rc && !sameCertKey(rc, issuer) &&
|
|
1569
|
-
if (after(fromPrecise(resp.thisUpdate), addMs(now, CLOCK_SKEW_MS))) continue;
|
|
1570
|
-
if (resp.nextUpdate && before(fromDate(resp.nextUpdate), now)) continue;
|
|
1571
|
-
if (resp.status === "good") return { issued: true, revoked: false };
|
|
1625
|
+
if (rc && !sameCertKey(rc, issuer) && !validAt(rc, now)) continue;
|
|
1572
1626
|
if (resp.status === "revoked") return { issued: true, revoked: true, revokedAt: fromPrecise(resp.revokedAt) };
|
|
1627
|
+
if (!ocspFreshAt(resp, now, now)) continue;
|
|
1628
|
+
if (resp.status === "good") return { issued: true, revoked: false };
|
|
1573
1629
|
return { issued: false, revoked: false };
|
|
1574
1630
|
}
|
|
1575
1631
|
return void 0;
|
|
1576
1632
|
}
|
|
1577
|
-
async crl(
|
|
1578
|
-
for (const u of
|
|
1633
|
+
async crl(cert, issuer) {
|
|
1634
|
+
for (const u of cert.crlDPs) {
|
|
1579
1635
|
let crl;
|
|
1580
1636
|
try {
|
|
1581
1637
|
crl = parseCRL(await this.f.do("GET", u, ""));
|
|
1582
1638
|
} catch {
|
|
1583
1639
|
continue;
|
|
1584
1640
|
}
|
|
1585
|
-
if (!crl || !
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
return e ? { issued: true, revoked: true, revokedAt: e.time } : { issued: true, revoked: false };
|
|
1641
|
+
if (!crl || !await crlFor(crl, cert, issuer)) continue;
|
|
1642
|
+
const t = crlListed(crl, cert);
|
|
1643
|
+
if (t) return { issued: true, revoked: true, revokedAt: t };
|
|
1644
|
+
if (freshAt(crl.thisUpdate, crl.nextUpdate, this.now, this.now)) return { issued: true, revoked: false };
|
|
1590
1645
|
}
|
|
1591
1646
|
return void 0;
|
|
1592
1647
|
}
|
|
@@ -1681,23 +1736,25 @@ var InflateError = class extends Error {
|
|
|
1681
1736
|
};
|
|
1682
1737
|
var Truncated = class extends InflateError {
|
|
1683
1738
|
};
|
|
1684
|
-
|
|
1739
|
+
var InflateLimitError = class extends InflateError {
|
|
1740
|
+
};
|
|
1741
|
+
function zlibInflate(src, limit = 64 << 20, initial) {
|
|
1685
1742
|
if (src.length < 2) throw new InflateError("zlib: header \u0430\u043B\u0433\u0430");
|
|
1686
1743
|
const cmf = src[0];
|
|
1687
1744
|
const flg = src[1];
|
|
1688
1745
|
if ((cmf & 15) !== 8 || cmf >> 4 > 7 || (cmf << 8 | flg) % 31 !== 0) throw new InflateError("zlib: header \u0431\u0443\u0440\u0443\u0443");
|
|
1689
1746
|
if (flg & 32) throw new InflateError("zlib: preset dictionary");
|
|
1690
|
-
return inflateRaw(src.subarray(2), limit);
|
|
1747
|
+
return inflateRaw(src.subarray(2), limit, void 0, initial);
|
|
1691
1748
|
}
|
|
1692
|
-
function inflateRaw(src, limit = 64 << 20, info) {
|
|
1749
|
+
function inflateRaw(src, limit = 64 << 20, info, initial) {
|
|
1693
1750
|
let pos = 0;
|
|
1694
1751
|
let bitBuf = 0;
|
|
1695
1752
|
let bitCnt = 0;
|
|
1696
|
-
let out = new Uint8Array(Math.min(limit, Math.max(1024, src.length * 4)));
|
|
1753
|
+
let out = new Uint8Array(Math.min(limit, Math.max(1024, initial ?? src.length * 4)));
|
|
1697
1754
|
let len = 0;
|
|
1698
1755
|
const need = (n) => {
|
|
1699
1756
|
if (len + n <= out.length) return;
|
|
1700
|
-
if (len + n > limit) throw new
|
|
1757
|
+
if (len + n > limit) throw new InflateLimitError("inflate: \u0445\u044D\u043C\u0436\u044D\u044D\u043D\u0438\u0439 \u0445\u044F\u0437\u0433\u0430\u0430\u0440");
|
|
1701
1758
|
let cap = out.length * 2;
|
|
1702
1759
|
while (cap < len + n) cap *= 2;
|
|
1703
1760
|
const nb = new Uint8Array(Math.min(cap, limit));
|
|
@@ -2506,28 +2563,17 @@ var Reader = class {
|
|
|
2506
2563
|
};
|
|
2507
2564
|
function findStartXref(b) {
|
|
2508
2565
|
const end = b.length;
|
|
2509
|
-
let
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
if (
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
while (hi > from && (b[hi - 1] === 10 || b[hi - 1] === 13 || b[hi - 1] === 9 || b[hi - 1] === 32)) hi--;
|
|
2517
|
-
for (; ; ) {
|
|
2518
|
-
if (hi - from === 5) break;
|
|
2519
|
-
if (hi - from >= 5 && fromLatin1(b, hi - 5, hi) === "%%EOF") {
|
|
2520
|
-
eofEnd = hi;
|
|
2521
|
-
break;
|
|
2522
|
-
}
|
|
2523
|
-
if (hi - from < 5) break;
|
|
2524
|
-
hi--;
|
|
2525
|
-
}
|
|
2526
|
-
if (eofEnd >= 0) break;
|
|
2566
|
+
let hi = end;
|
|
2567
|
+
while (hi > 0 && (b[hi - 1] === 10 || b[hi - 1] === 13 || b[hi - 1] === 9 || b[hi - 1] === 32)) hi--;
|
|
2568
|
+
let idx = -1;
|
|
2569
|
+
for (let i2 = hi - 5; i2 >= 0; i2--) {
|
|
2570
|
+
if (b[i2] === 37 && b[i2 + 1] === 37 && b[i2 + 2] === 69 && b[i2 + 3] === 79 && b[i2 + 4] === 70) {
|
|
2571
|
+
idx = i2;
|
|
2572
|
+
break;
|
|
2527
2573
|
}
|
|
2528
|
-
searchSize += 200;
|
|
2529
|
-
if (searchSize > end) throw new PdfError("not a PDF file: missing %%EOF");
|
|
2530
2574
|
}
|
|
2575
|
+
if (idx < 0 || 200 * (Math.floor((end - idx) / 200) + 1) > end) throw new PdfError("not a PDF file: missing %%EOF");
|
|
2576
|
+
const eofEnd = idx + 5;
|
|
2531
2577
|
const winStart = eofEnd - 200;
|
|
2532
2578
|
if (winStart < 0) throw new PdfError("malformed PDF file: missing final startxref");
|
|
2533
2579
|
const buf = b.subarray(winStart, eofEnd);
|
|
@@ -2548,6 +2594,332 @@ function findLastLine(buf, s) {
|
|
|
2548
2594
|
}
|
|
2549
2595
|
}
|
|
2550
2596
|
|
|
2597
|
+
// src/pades/pdf/nesting.ts
|
|
2598
|
+
var MAX_NESTING = 256;
|
|
2599
|
+
var NEST_SCAN_FACTOR = 16;
|
|
2600
|
+
var MAX_OBJSTM_BYTES = 32 << 20;
|
|
2601
|
+
var MAX_XREF_SIZE = 1 << 21;
|
|
2602
|
+
var MAX_OBJSTM_CHAIN = 64;
|
|
2603
|
+
var StructureError = class extends Error {
|
|
2604
|
+
constructor() {
|
|
2605
|
+
super("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");
|
|
2606
|
+
this.name = "StructureError";
|
|
2607
|
+
}
|
|
2608
|
+
};
|
|
2609
|
+
var ENCRYPT_REF = /\/Encrypt(?:[\t\n\f\r ]+\d+[\t\n\f\r ]+\d+[\t\n\f\r ]+R|[\t\n\f\r ]*<<)/;
|
|
2610
|
+
var isSpace2 = (c) => c === 0 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32;
|
|
2611
|
+
var isDelim2 = (c) => c === 40 || c === 41 || c === 60 || c === 62 || c === 91 || c === 93 || c === 123 || c === 125 || c === 47 || c === 37;
|
|
2612
|
+
var INT_RE = /^[+-]?\d+$/;
|
|
2613
|
+
var INT64_MAX = 9223372036854775807n;
|
|
2614
|
+
var isInt = (s) => INT_RE.test(s) && BigInt(s) <= INT64_MAX && BigInt(s) >= -INT64_MAX - 1n;
|
|
2615
|
+
function parseInt64(s) {
|
|
2616
|
+
if (!INT_RE.test(s)) return 0;
|
|
2617
|
+
const v = BigInt(s);
|
|
2618
|
+
if (v > INT64_MAX) return Number(INT64_MAX);
|
|
2619
|
+
if (v < -INT64_MAX - 1n) return -Number(INT64_MAX) - 1;
|
|
2620
|
+
return Number(v);
|
|
2621
|
+
}
|
|
2622
|
+
var Lex = class {
|
|
2623
|
+
constructor(b, pos) {
|
|
2624
|
+
this.b = b;
|
|
2625
|
+
this.pos = pos;
|
|
2626
|
+
}
|
|
2627
|
+
b;
|
|
2628
|
+
pos;
|
|
2629
|
+
val = "";
|
|
2630
|
+
next() {
|
|
2631
|
+
const b = this.b;
|
|
2632
|
+
for (; ; ) {
|
|
2633
|
+
while (this.pos < b.length && isSpace2(b[this.pos])) this.pos++;
|
|
2634
|
+
if (this.pos < b.length && b[this.pos] === 37) {
|
|
2635
|
+
while (this.pos < b.length && b[this.pos] !== 13 && b[this.pos] !== 10) this.pos++;
|
|
2636
|
+
continue;
|
|
2637
|
+
}
|
|
2638
|
+
break;
|
|
2639
|
+
}
|
|
2640
|
+
if (this.pos >= b.length) return 0 /* EOF */;
|
|
2641
|
+
const c = b[this.pos++];
|
|
2642
|
+
switch (c) {
|
|
2643
|
+
case 60:
|
|
2644
|
+
if (this.pos < b.length && b[this.pos] === 60) {
|
|
2645
|
+
this.pos++;
|
|
2646
|
+
return 4 /* Dict */;
|
|
2647
|
+
}
|
|
2648
|
+
while (this.pos < b.length && b[this.pos] !== 62) this.pos++;
|
|
2649
|
+
this.pos++;
|
|
2650
|
+
return 8 /* Str */;
|
|
2651
|
+
case 40:
|
|
2652
|
+
for (let depth = 1; this.pos < b.length && depth > 0; this.pos++) {
|
|
2653
|
+
const x = b[this.pos];
|
|
2654
|
+
if (x === 40) depth++;
|
|
2655
|
+
else if (x === 41) depth--;
|
|
2656
|
+
else if (x === 92) this.pos++;
|
|
2657
|
+
}
|
|
2658
|
+
return 8 /* Str */;
|
|
2659
|
+
case 91:
|
|
2660
|
+
return 2 /* Arr */;
|
|
2661
|
+
case 93:
|
|
2662
|
+
return 3 /* ArrEnd */;
|
|
2663
|
+
case 123:
|
|
2664
|
+
case 125:
|
|
2665
|
+
this.val = String.fromCharCode(c);
|
|
2666
|
+
return 7 /* Word */;
|
|
2667
|
+
case 47: {
|
|
2668
|
+
let name = "";
|
|
2669
|
+
while (this.pos < b.length && !isDelim2(b[this.pos]) && !isSpace2(b[this.pos])) {
|
|
2670
|
+
if (b[this.pos] === 35 && this.pos + 2 < b.length) {
|
|
2671
|
+
const hx = String.fromCharCode(b[this.pos + 1], b[this.pos + 2]);
|
|
2672
|
+
if (/^[0-9a-fA-F]{2}$/.test(hx)) {
|
|
2673
|
+
name += String.fromCharCode(parseInt(hx, 16));
|
|
2674
|
+
this.pos += 3;
|
|
2675
|
+
continue;
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
name += String.fromCharCode(b[this.pos]);
|
|
2679
|
+
this.pos++;
|
|
2680
|
+
}
|
|
2681
|
+
this.val = name;
|
|
2682
|
+
return 6 /* Name */;
|
|
2683
|
+
}
|
|
2684
|
+
case 62:
|
|
2685
|
+
if (this.pos < b.length && b[this.pos] === 62) {
|
|
2686
|
+
this.pos++;
|
|
2687
|
+
return 5 /* DictEnd */;
|
|
2688
|
+
}
|
|
2689
|
+
return 1 /* Err */;
|
|
2690
|
+
case 41:
|
|
2691
|
+
return 1 /* Err */;
|
|
2692
|
+
}
|
|
2693
|
+
const start = this.pos - 1;
|
|
2694
|
+
while (this.pos < b.length && !isDelim2(b[this.pos]) && !isSpace2(b[this.pos])) this.pos++;
|
|
2695
|
+
this.val = fromLatin1(b, start, this.pos);
|
|
2696
|
+
return 7 /* Word */;
|
|
2697
|
+
}
|
|
2698
|
+
};
|
|
2699
|
+
var Scan = class {
|
|
2700
|
+
constructor(budget, inflateLeft, encrypted) {
|
|
2701
|
+
this.budget = budget;
|
|
2702
|
+
this.inflateLeft = inflateLeft;
|
|
2703
|
+
this.encrypted = encrypted;
|
|
2704
|
+
}
|
|
2705
|
+
budget;
|
|
2706
|
+
inflateLeft;
|
|
2707
|
+
encrypted;
|
|
2708
|
+
/** Дээд түвшний /Size > MAX_XREF_SIZE. */
|
|
2709
|
+
bigSize = false;
|
|
2710
|
+
/** b[start:]-аас reader-ийн нэг объектыг (давхар `N G obj`-ийг оруулаад) гүнээр шалгана. */
|
|
2711
|
+
object(b, start, allowStream) {
|
|
2712
|
+
const l = new Lex(b, start);
|
|
2713
|
+
try {
|
|
2714
|
+
this.objectBody(l, start, allowStream);
|
|
2715
|
+
} finally {
|
|
2716
|
+
this.budget -= l.pos - start;
|
|
2717
|
+
}
|
|
2718
|
+
}
|
|
2719
|
+
objectBody(l, start, allowStream) {
|
|
2720
|
+
const stack = [];
|
|
2721
|
+
const info = { typ: "", filter: "", filters: 0, parms: false, indirect: false, first: 0, nCount: 0 };
|
|
2722
|
+
for (; ; ) {
|
|
2723
|
+
if (this.budget - (l.pos - start) < 0) throw new StructureError();
|
|
2724
|
+
const tok = l.next();
|
|
2725
|
+
const top = stack.length - 1;
|
|
2726
|
+
if (top >= 0 && stack[top].dict && stack[top].expectKey) {
|
|
2727
|
+
if (tok === 5 /* DictEnd */) {
|
|
2728
|
+
stack.pop();
|
|
2729
|
+
if (stack.length === 0) {
|
|
2730
|
+
if (allowStream && (info.typ === "ObjStm" || info.indirect)) this.objStm(l, info);
|
|
2731
|
+
return;
|
|
2732
|
+
}
|
|
2733
|
+
const p = stack[stack.length - 1];
|
|
2734
|
+
p.expectKey = p.dict;
|
|
2735
|
+
continue;
|
|
2736
|
+
}
|
|
2737
|
+
if (tok === 6 /* Name */) {
|
|
2738
|
+
stack[top].key = l.val;
|
|
2739
|
+
stack[top].expectKey = false;
|
|
2740
|
+
continue;
|
|
2741
|
+
}
|
|
2742
|
+
return;
|
|
2743
|
+
}
|
|
2744
|
+
let done = false;
|
|
2745
|
+
switch (tok) {
|
|
2746
|
+
case 0 /* EOF */:
|
|
2747
|
+
case 1 /* Err */:
|
|
2748
|
+
case 5 /* DictEnd */:
|
|
2749
|
+
return;
|
|
2750
|
+
case 2 /* Arr */:
|
|
2751
|
+
case 4 /* Dict */:
|
|
2752
|
+
if (stack.length >= MAX_NESTING) throw new StructureError();
|
|
2753
|
+
if (stack.length === 1 && stack[0].dict && stack[0].key === "DecodeParms") info.parms = true;
|
|
2754
|
+
stack.push({ dict: tok === 4 /* Dict */, expectKey: tok === 4 /* Dict */, key: "" });
|
|
2755
|
+
continue;
|
|
2756
|
+
case 3 /* ArrEnd */:
|
|
2757
|
+
if (top < 0 || stack[top].dict) return;
|
|
2758
|
+
stack.pop();
|
|
2759
|
+
break;
|
|
2760
|
+
case 7 /* Word */:
|
|
2761
|
+
if (isInt(l.val)) {
|
|
2762
|
+
const save = l.pos;
|
|
2763
|
+
const saveVal = l.val;
|
|
2764
|
+
if (l.next() === 7 /* Word */ && isInt(l.val)) {
|
|
2765
|
+
const t3 = l.next();
|
|
2766
|
+
if (t3 === 7 /* Word */ && l.val === "R") {
|
|
2767
|
+
if (stack.length === 1 && stack[0].dict && ["Type", "Filter", "DecodeParms", "N", "First"].includes(stack[0].key)) info.indirect = true;
|
|
2768
|
+
done = true;
|
|
2769
|
+
} else if (t3 === 7 /* Word */ && l.val === "obj") {
|
|
2770
|
+
continue;
|
|
2771
|
+
}
|
|
2772
|
+
}
|
|
2773
|
+
if (!done) {
|
|
2774
|
+
l.pos = save;
|
|
2775
|
+
l.val = saveVal;
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
if (!done) this.noteScalar(stack, info, tok, l.val);
|
|
2779
|
+
break;
|
|
2780
|
+
case 6 /* Name */:
|
|
2781
|
+
case 8 /* Str */:
|
|
2782
|
+
this.noteScalar(stack, info, tok, l.val);
|
|
2783
|
+
break;
|
|
2784
|
+
}
|
|
2785
|
+
if (stack.length === 0) return;
|
|
2786
|
+
const f = stack[stack.length - 1];
|
|
2787
|
+
if (f.dict) f.expectKey = true;
|
|
2788
|
+
}
|
|
2789
|
+
}
|
|
2790
|
+
/** Дээд түвшний толийн (ба /Filter массивын) энгийн утгыг тэмдэглэнэ. */
|
|
2791
|
+
noteScalar(stack, info, tok, val) {
|
|
2792
|
+
if (stack.length === 1 && stack[0].dict) {
|
|
2793
|
+
switch (stack[0].key) {
|
|
2794
|
+
case "Type":
|
|
2795
|
+
if (tok === 6 /* Name */) info.typ = val;
|
|
2796
|
+
break;
|
|
2797
|
+
case "Filter":
|
|
2798
|
+
if (tok === 6 /* Name */) {
|
|
2799
|
+
info.filter = val;
|
|
2800
|
+
info.filters = 1;
|
|
2801
|
+
}
|
|
2802
|
+
break;
|
|
2803
|
+
case "DecodeParms":
|
|
2804
|
+
if (!(tok === 7 /* Word */ && val === "null")) info.parms = true;
|
|
2805
|
+
break;
|
|
2806
|
+
case "N":
|
|
2807
|
+
info.nCount = parseInt64(val);
|
|
2808
|
+
break;
|
|
2809
|
+
case "Size":
|
|
2810
|
+
if (tok === 7 /* Word */ && isInt(val) && BigInt(val) > BigInt(MAX_XREF_SIZE)) this.bigSize = true;
|
|
2811
|
+
break;
|
|
2812
|
+
case "First":
|
|
2813
|
+
info.first = parseInt64(val);
|
|
2814
|
+
break;
|
|
2815
|
+
}
|
|
2816
|
+
} else if (stack.length === 2 && stack[0].dict && stack[0].key === "Filter" && !stack[1].dict && tok === 6 /* Name */) {
|
|
2817
|
+
info.filter = val;
|
|
2818
|
+
info.filters++;
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
/** `stream`-ийн араас FlateDecode өгөгдлийг (zlib өөрөө төгсөнө) задалж, толгойн offset бүрээс объектыг шалгана. */
|
|
2822
|
+
objStm(l, info) {
|
|
2823
|
+
if (l.next() !== 7 /* Word */ || l.val !== "stream" || this.encrypted) return;
|
|
2824
|
+
if (info.indirect) throw new StructureError();
|
|
2825
|
+
if (info.typ !== "ObjStm") return;
|
|
2826
|
+
const b = l.b;
|
|
2827
|
+
let p = l.pos;
|
|
2828
|
+
if (p < b.length && b[p] === 13) p++;
|
|
2829
|
+
if (p < b.length && b[p] === 10) p++;
|
|
2830
|
+
if (info.filters === 0) {
|
|
2831
|
+
this.objStmData(b.subarray(p), info);
|
|
2832
|
+
return;
|
|
2833
|
+
}
|
|
2834
|
+
if (info.filters !== 1 || info.filter !== "FlateDecode") return;
|
|
2835
|
+
if (info.parms) throw new StructureError();
|
|
2836
|
+
let data;
|
|
2837
|
+
try {
|
|
2838
|
+
data = zlibInflate(b.subarray(p), this.inflateLeft, 64 << 10);
|
|
2839
|
+
} catch (e) {
|
|
2840
|
+
if (e instanceof InflateLimitError) throw new StructureError();
|
|
2841
|
+
return;
|
|
2842
|
+
}
|
|
2843
|
+
this.inflateLeft -= data.length;
|
|
2844
|
+
this.budget -= data.length;
|
|
2845
|
+
this.objStmData(data, info);
|
|
2846
|
+
}
|
|
2847
|
+
objStmData(data, info) {
|
|
2848
|
+
const l = new Lex(data, 0);
|
|
2849
|
+
for (let i = 0; i < info.nCount; i++) {
|
|
2850
|
+
if (l.next() === 0 /* EOF */) break;
|
|
2851
|
+
if (l.next() !== 7 /* Word */) continue;
|
|
2852
|
+
if (!isInt(l.val)) continue;
|
|
2853
|
+
const off = parseInt64(l.val);
|
|
2854
|
+
if (info.first < 0 || off < 0 || info.first + off >= data.length) continue;
|
|
2855
|
+
if (this.budget < 0) throw new StructureError();
|
|
2856
|
+
this.object(data, info.first + off, false);
|
|
2857
|
+
}
|
|
2858
|
+
if (this.budget < 0) throw new StructureError();
|
|
2859
|
+
}
|
|
2860
|
+
};
|
|
2861
|
+
function checkStructure(doc) {
|
|
2862
|
+
const text = fromLatin1(doc);
|
|
2863
|
+
const s = new Scan(NEST_SCAN_FACTOR * doc.length + (1 << 20), MAX_OBJSTM_BYTES, ENCRYPT_REF.test(text));
|
|
2864
|
+
for (const kw of ["obj", "trailer"]) {
|
|
2865
|
+
for (let i = 0; ; ) {
|
|
2866
|
+
const k = text.indexOf(kw, i);
|
|
2867
|
+
if (k < 0) break;
|
|
2868
|
+
const at = k + kw.length;
|
|
2869
|
+
i = at;
|
|
2870
|
+
if (k > 0 && !isSpace2(doc[k - 1]) && !isDelim2(doc[k - 1]) || at < doc.length && !isSpace2(doc[at]) && !isDelim2(doc[at])) continue;
|
|
2871
|
+
s.object(doc, at, true);
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
if (s.bigSize) throw new StructureError();
|
|
2875
|
+
checkObjStmGraph(doc);
|
|
2876
|
+
}
|
|
2877
|
+
function checkObjStmGraph(doc) {
|
|
2878
|
+
let r;
|
|
2879
|
+
try {
|
|
2880
|
+
r = new Reader(doc);
|
|
2881
|
+
} catch {
|
|
2882
|
+
return;
|
|
2883
|
+
}
|
|
2884
|
+
const xr = r.xref;
|
|
2885
|
+
const registered = (p) => p.id < xr.length && xr[p.id].ptr.id === p.id && xr[p.id].ptr.gen === p.gen;
|
|
2886
|
+
const inStream = (p) => registered(p) && xr[p.id].inStream;
|
|
2887
|
+
const containers = /* @__PURE__ */ new Map();
|
|
2888
|
+
for (const e of xr) {
|
|
2889
|
+
if (!e.inStream) continue;
|
|
2890
|
+
if (inStream(e.stream)) throw new StructureError();
|
|
2891
|
+
containers.set(`${e.stream.id} ${e.stream.gen}`, e.stream);
|
|
2892
|
+
}
|
|
2893
|
+
for (const c of containers.values()) {
|
|
2894
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2895
|
+
for (let p = c; registered(p); ) {
|
|
2896
|
+
const k = `${p.id} ${p.gen}`;
|
|
2897
|
+
if (seen.has(k) || seen.size > MAX_OBJSTM_CHAIN) throw new StructureError();
|
|
2898
|
+
seen.add(k);
|
|
2899
|
+
let v;
|
|
2900
|
+
try {
|
|
2901
|
+
const e = xr[p.id].ptr;
|
|
2902
|
+
v = r.resolve(e, new PRef(e.id, e.gen)).data;
|
|
2903
|
+
} catch {
|
|
2904
|
+
return;
|
|
2905
|
+
}
|
|
2906
|
+
if (!(v instanceof PStream)) break;
|
|
2907
|
+
let bad = false;
|
|
2908
|
+
walkRefs(v.hdr, 0, (x) => bad ||= inStream(x));
|
|
2909
|
+
if (bad) throw new StructureError();
|
|
2910
|
+
const ext = v.hdr.get("Extends");
|
|
2911
|
+
if (!(ext instanceof PRef)) break;
|
|
2912
|
+
p = { id: ext.id, gen: ext.gen };
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
function walkRefs(v, depth, fn) {
|
|
2917
|
+
if (depth > MAX_NESTING) return;
|
|
2918
|
+
if (v instanceof PRef) fn({ id: v.id, gen: v.gen });
|
|
2919
|
+
else if (v instanceof Map) for (const x of v.values()) walkRefs(x, depth + 1, fn);
|
|
2920
|
+
else if (Array.isArray(v)) for (const x of v) walkRefs(x, depth + 1, fn);
|
|
2921
|
+
}
|
|
2922
|
+
|
|
2551
2923
|
// src/pades/assets/fonts.ts
|
|
2552
2924
|
var REGULAR = {
|
|
2553
2925
|
psName: "NotoSans",
|
|
@@ -3059,7 +3431,7 @@ var PStream2 = class {
|
|
|
3059
3431
|
};
|
|
3060
3432
|
var isDict = (v) => v instanceof Map;
|
|
3061
3433
|
var isArr = (v) => Array.isArray(v);
|
|
3062
|
-
var
|
|
3434
|
+
var isInt2 = (v) => typeof v === "bigint";
|
|
3063
3435
|
var isName = (v, s) => v instanceof PName2 && (s === void 0 || v.v === s);
|
|
3064
3436
|
var isStr = (v) => v instanceof PStr2;
|
|
3065
3437
|
var isRef = (v) => v instanceof PRef2;
|
|
@@ -3124,7 +3496,7 @@ var PdfBudget = class {
|
|
|
3124
3496
|
const intOr = (key, def) => {
|
|
3125
3497
|
const v = dget(parms, key);
|
|
3126
3498
|
if (v === null) return def;
|
|
3127
|
-
if (
|
|
3499
|
+
if (isInt2(v)) return v;
|
|
3128
3500
|
return -1n;
|
|
3129
3501
|
};
|
|
3130
3502
|
const pred = intOr("Predictor", 1n);
|
|
@@ -3234,7 +3606,7 @@ function allGapSpace(b) {
|
|
|
3234
3606
|
}
|
|
3235
3607
|
var isDigit = (c) => c !== void 0 && c >= 48 && c <= 57;
|
|
3236
3608
|
var HEX2 = /^[0-9a-fA-F]{2}$/;
|
|
3237
|
-
var
|
|
3609
|
+
var INT_RE2 = /^[+-]?[0-9]+$/;
|
|
3238
3610
|
var FLOAT_RE = /^[+-]?([0-9]+\.[0-9]*|\.[0-9]+)$/;
|
|
3239
3611
|
var I64_MAX = (1n << 63n) - 1n;
|
|
3240
3612
|
var I64_MIN = -(1n << 63n);
|
|
@@ -3435,7 +3807,7 @@ var PdfParser = class {
|
|
|
3435
3807
|
if (!Number.isFinite(f)) throw errPDFSyntax();
|
|
3436
3808
|
return new PReal2(f);
|
|
3437
3809
|
}
|
|
3438
|
-
if (!
|
|
3810
|
+
if (!INT_RE2.test(tok)) throw errPDFSyntax();
|
|
3439
3811
|
const n = BigInt(tok);
|
|
3440
3812
|
if (n > I64_MAX || n < I64_MIN) throw errPDFSyntax();
|
|
3441
3813
|
if (n >= 0n && tok[0] !== "+") {
|
|
@@ -3471,7 +3843,7 @@ var PdfParser = class {
|
|
|
3471
3843
|
this.pos++;
|
|
3472
3844
|
let size;
|
|
3473
3845
|
const l = dget(d, "Length");
|
|
3474
|
-
if (
|
|
3846
|
+
if (isInt2(l)) size = l;
|
|
3475
3847
|
else if (isRef(l)) {
|
|
3476
3848
|
if (!length) throw errPDFSyntax();
|
|
3477
3849
|
size = length(l);
|
|
@@ -3610,11 +3982,11 @@ var PdfView = class {
|
|
|
3610
3982
|
if (!isArr(w) || w.length !== 3) throw errPDFSyntax();
|
|
3611
3983
|
const width = [];
|
|
3612
3984
|
for (const x of w) {
|
|
3613
|
-
if (!
|
|
3985
|
+
if (!isInt2(x) || x < 0n || x > 7n) throw errPDFSyntax();
|
|
3614
3986
|
width.push(Number(x));
|
|
3615
3987
|
}
|
|
3616
3988
|
const size = dget(st.dict, "Size");
|
|
3617
|
-
if (!
|
|
3989
|
+
if (!isInt2(size) || size < 0n) throw errPDFSyntax();
|
|
3618
3990
|
let index = [0n, size];
|
|
3619
3991
|
if (st.dict.has("Index")) {
|
|
3620
3992
|
const ix = dget(st.dict, "Index");
|
|
@@ -3633,7 +4005,7 @@ var PdfView = class {
|
|
|
3633
4005
|
for (let i = 0; i < index.length; i += 2) {
|
|
3634
4006
|
const first = index[i];
|
|
3635
4007
|
const count = index[i + 1];
|
|
3636
|
-
if (!
|
|
4008
|
+
if (!isInt2(first) || !isInt2(count) || first < 0n || first >= BigInt(MAX_OBJECT_NUMBER) || count < 0n || count > BigInt(Math.floor((data.length - cur) / row))) {
|
|
3637
4009
|
throw errPDFSyntax();
|
|
3638
4010
|
}
|
|
3639
4011
|
const f0 = Number(first);
|
|
@@ -3703,7 +4075,7 @@ var PdfView = class {
|
|
|
3703
4075
|
this.inLength = true;
|
|
3704
4076
|
try {
|
|
3705
4077
|
const o = this.deref(r);
|
|
3706
|
-
if (!
|
|
4078
|
+
if (!isInt2(o)) throw errPDFSyntax();
|
|
3707
4079
|
this.usedLength = true;
|
|
3708
4080
|
return o;
|
|
3709
4081
|
} finally {
|
|
@@ -3719,7 +4091,7 @@ var PdfView = class {
|
|
|
3719
4091
|
if (!(o instanceof PStream2)) throw errPDFSyntax();
|
|
3720
4092
|
const n = dget(o.dict, "N");
|
|
3721
4093
|
const first = dget(o.dict, "First");
|
|
3722
|
-
if (!
|
|
4094
|
+
if (!isInt2(n) || !isInt2(first) || !isInt2(dget(o.dict, "Length")) || !ifaceEq(dget(o.dict, "Type"), new PName2("ObjStm")) || dget(o.dict, "Extends") !== null) {
|
|
3723
4095
|
throw errPDFSyntax();
|
|
3724
4096
|
}
|
|
3725
4097
|
const data = this.budget.decode(o);
|
|
@@ -3771,7 +4143,7 @@ function openView(b, budget) {
|
|
|
3771
4143
|
if (dget(sec.trailer, "Encrypt") !== null || dget(sec.trailer, "XRefStm") !== null) throw errPDFSyntax();
|
|
3772
4144
|
if (!sec.trailer.has("Prev")) break;
|
|
3773
4145
|
const prev = dget(sec.trailer, "Prev");
|
|
3774
|
-
if (!
|
|
4146
|
+
if (!isInt2(prev) || prev < 0n || prev >= BigInt(b.length)) throw errPDFSyntax();
|
|
3775
4147
|
off = Number(prev);
|
|
3776
4148
|
}
|
|
3777
4149
|
for (const sec of v.sections) {
|
|
@@ -3779,7 +4151,7 @@ function openView(b, budget) {
|
|
|
3779
4151
|
}
|
|
3780
4152
|
v.trailer = v.sections[0].trailer;
|
|
3781
4153
|
const size = dget(v.trailer, "Size");
|
|
3782
|
-
if (!
|
|
4154
|
+
if (!isInt2(size)) throw errPDFSyntax();
|
|
3783
4155
|
for (const [num, e] of v.xref) {
|
|
3784
4156
|
if (inUse(e) && (BigInt(num) >= size || num === 0)) throw errPDFSyntax();
|
|
3785
4157
|
}
|
|
@@ -3946,14 +4318,14 @@ function pageGeometry(c, num) {
|
|
|
3946
4318
|
let rot = 0;
|
|
3947
4319
|
const r = inh("Rotate");
|
|
3948
4320
|
if (r !== null) {
|
|
3949
|
-
if (!
|
|
4321
|
+
if (!isInt2(r) || r % 90n !== 0n) throw fail();
|
|
3950
4322
|
rot = Number((r % 360n + 360n) % 360n);
|
|
3951
4323
|
}
|
|
3952
4324
|
return { box, rot };
|
|
3953
4325
|
}
|
|
3954
4326
|
function pdfNumber(x) {
|
|
3955
4327
|
let v;
|
|
3956
|
-
if (
|
|
4328
|
+
if (isInt2(x)) v = Number(x);
|
|
3957
4329
|
else if (x instanceof PReal2) v = x.v;
|
|
3958
4330
|
else return void 0;
|
|
3959
4331
|
return !Number.isNaN(v) && Math.abs(v) <= 1e7 ? v : void 0;
|
|
@@ -4224,9 +4596,9 @@ function trailerCompatible(base, later, infoOmitted) {
|
|
|
4224
4596
|
if (!isArr(a) || a.length !== 2 || !pdfEqual(a[0], bid[0])) return false;
|
|
4225
4597
|
}
|
|
4226
4598
|
const bsv = dget(base, "Size");
|
|
4227
|
-
const bs =
|
|
4599
|
+
const bs = isInt2(bsv) ? bsv : 0n;
|
|
4228
4600
|
const ls = dget(later, "Size");
|
|
4229
|
-
return
|
|
4601
|
+
return isInt2(ls) && ls >= bs;
|
|
4230
4602
|
}
|
|
4231
4603
|
var LTV_ROLES = ["catalog", "dss", "vri", "vrient", "ltvarr", "ltv", "info", "metadata"];
|
|
4232
4604
|
var Classifier = class {
|
|
@@ -4606,9 +4978,9 @@ var Classifier = class {
|
|
|
4606
4978
|
if (!pdfEqual(dget(bd, "SigFlags"), dget(fd, "SigFlags"))) {
|
|
4607
4979
|
const nf = dget(fd, "SigFlags");
|
|
4608
4980
|
const ofv = dget(bd, "SigFlags");
|
|
4609
|
-
const was =
|
|
4981
|
+
const was = isInt2(ofv);
|
|
4610
4982
|
const of = was ? ofv : 0n;
|
|
4611
|
-
if (!
|
|
4983
|
+
if (!isInt2(nf) || nf !== 1n && nf !== 3n || ofv !== null && !was || nf < of) throw errNotSignatureOnly();
|
|
4612
4984
|
}
|
|
4613
4985
|
if (!pdfEqual(dget(bd, "SigFlags"), dget(fd, "SigFlags")) || bd === null) this.sigWork = true;
|
|
4614
4986
|
if (this.own) {
|
|
@@ -4645,7 +5017,7 @@ var Classifier = class {
|
|
|
4645
5017
|
if (d.has("Type") && !ifaceEq(dget(d, "Type"), new PName2("Annot"))) throw errNotSignatureOnly();
|
|
4646
5018
|
if (d.has("Subtype") && !ifaceEq(dget(d, "Subtype"), new PName2("Widget"))) throw errNotSignatureOnly();
|
|
4647
5019
|
for (const k of ["T", "TU", "M"]) if (d.has(k) && !isStr(dget(d, k))) throw errNotSignatureOnly();
|
|
4648
|
-
for (const k of ["F", "Ff"]) if (d.has(k) && !
|
|
5020
|
+
for (const k of ["F", "Ff"]) if (d.has(k) && !isInt2(dget(d, k))) throw errNotSignatureOnly();
|
|
4649
5021
|
if (this.own) {
|
|
4650
5022
|
if (d.has("AP")) {
|
|
4651
5023
|
ownVisible(this, d, r.num);
|
|
@@ -4708,7 +5080,7 @@ var Classifier = class {
|
|
|
4708
5080
|
if (!isArr(br) || !isStr(dget(d, "Contents")) || br.length !== 4) throw errNotSignatureOnly();
|
|
4709
5081
|
const n = [];
|
|
4710
5082
|
for (const x of br) {
|
|
4711
|
-
if (!
|
|
5083
|
+
if (!isInt2(x)) throw errNotSignatureOnly();
|
|
4712
5084
|
n.push(x);
|
|
4713
5085
|
}
|
|
4714
5086
|
if (n[0] !== 0n || n[1] <= 0n || n[2] <= n[1] || n[3] < 0n || n[2] + n[3] <= BigInt(this.end)) throw errNotSignatureOnly();
|
|
@@ -4745,7 +5117,7 @@ var Classifier = class {
|
|
|
4745
5117
|
if (!isArr(a) || a.length !== 4) throw errNotSignatureOnly();
|
|
4746
5118
|
const f = [];
|
|
4747
5119
|
for (const x of a) {
|
|
4748
|
-
if (
|
|
5120
|
+
if (isInt2(x)) f.push(Number(x));
|
|
4749
5121
|
else if (x instanceof PReal2) f.push(x.v);
|
|
4750
5122
|
else throw errNotSignatureOnly();
|
|
4751
5123
|
}
|
|
@@ -4874,7 +5246,7 @@ var Classifier = class {
|
|
|
4874
5246
|
const dev = (x) => {
|
|
4875
5247
|
if (!isDict(x) || !onlyKeys2(x, "Type", "BaseVersion", "ExtensionLevel", "ExtensionRevision", "URL")) return false;
|
|
4876
5248
|
if (!(dget(x, "BaseVersion") instanceof PName2)) return false;
|
|
4877
|
-
if (!
|
|
5249
|
+
if (!isInt2(dget(x, "ExtensionLevel"))) return false;
|
|
4878
5250
|
for (const k of ["ExtensionRevision", "URL"]) if (x.has(k) && !isStr(dget(x, k))) return false;
|
|
4879
5251
|
if (x.has("Type") && !ifaceEq(dget(x, "Type"), new PName2("DeveloperExtensions"))) return false;
|
|
4880
5252
|
return true;
|
|
@@ -4922,7 +5294,8 @@ var MESSAGES = {
|
|
|
4922
5294
|
not_pdf: "PDF \u0444\u0430\u0439\u043B \u0437\u0430\u0434\u0430\u0440\u0441\u0430\u043D\u0433\u04AF\u0439",
|
|
4923
5295
|
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",
|
|
4924
5296
|
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",
|
|
4925
|
-
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"
|
|
5297
|
+
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",
|
|
5298
|
+
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"
|
|
4926
5299
|
};
|
|
4927
5300
|
var ExtractError = class extends Error {
|
|
4928
5301
|
constructor(code) {
|
|
@@ -4931,15 +5304,21 @@ var ExtractError = class extends Error {
|
|
|
4931
5304
|
}
|
|
4932
5305
|
code;
|
|
4933
5306
|
};
|
|
4934
|
-
var
|
|
5307
|
+
var ENCRYPT_REF2 = /\/Encrypt(?:\s+\d+\s+\d+\s+R|\s*<<)/;
|
|
4935
5308
|
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
4936
5309
|
var AP_VERIFY_PREFIX2 = "https://ca.eidmongolia.mn/verify/";
|
|
4937
5310
|
async function extract(doc, profile = "own") {
|
|
5311
|
+
try {
|
|
5312
|
+
checkStructure(doc);
|
|
5313
|
+
} catch (e) {
|
|
5314
|
+
if (e instanceof StructureError) throw new ExtractError("structure");
|
|
5315
|
+
throw e;
|
|
5316
|
+
}
|
|
4938
5317
|
let rdr;
|
|
4939
5318
|
try {
|
|
4940
5319
|
rdr = new Reader(doc);
|
|
4941
5320
|
} catch {
|
|
4942
|
-
throw new ExtractError(
|
|
5321
|
+
throw new ExtractError(ENCRYPT_REF2.test(fromLatin1(doc)) ? "encrypted" : "not_pdf");
|
|
4943
5322
|
}
|
|
4944
5323
|
if (rdr.trailerValue().keys().includes("Encrypt")) throw new ExtractError("encrypted");
|
|
4945
5324
|
const sigs = [];
|
|
@@ -5516,10 +5895,10 @@ async function validatePdf(pdf, opts = {}) {
|
|
|
5516
5895
|
}
|
|
5517
5896
|
const now = fromDate(opts.time ?? /* @__PURE__ */ new Date());
|
|
5518
5897
|
const dss = ex.sigs.find((s) => s.dss)?.dss;
|
|
5519
|
-
let status = new DSSStatus(dss,
|
|
5898
|
+
let status = new DSSStatus(dss, now);
|
|
5520
5899
|
if (!opts.offline) {
|
|
5521
5900
|
const fetchFn = opts.fetch ?? globalThis.fetch;
|
|
5522
|
-
status =
|
|
5901
|
+
status = new OnlineStatus(new Fetcher(fetchFn, isBrowser(), opts.signal), status, now);
|
|
5523
5902
|
}
|
|
5524
5903
|
const ev = new Evaluator({ roots, intermediates: [...embedded(INTERMEDIATES), ...extraInter], issuers, status, now });
|
|
5525
5904
|
const rep = { indication: "valid", signatures: [] };
|