@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/README.md +17 -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 +5 -2
- package/dist/index.d.ts +5 -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
|
@@ -781,10 +781,427 @@ async function verifySignedData(sd) {
|
|
|
781
781
|
return true;
|
|
782
782
|
}
|
|
783
783
|
|
|
784
|
+
// src/pades/validate/gofmt.ts
|
|
785
|
+
var ZERO_TIME = { nanos: 0 };
|
|
786
|
+
function fromPrecise(t) {
|
|
787
|
+
return { sec: Math.floor(t.date.getTime() / 1e3), nanos: t.nanos };
|
|
788
|
+
}
|
|
789
|
+
function fromDate(d) {
|
|
790
|
+
const ms = d.getTime();
|
|
791
|
+
const sec = Math.floor(ms / 1e3);
|
|
792
|
+
return { sec, nanos: (ms - sec * 1e3) * 1e6 };
|
|
793
|
+
}
|
|
794
|
+
var isZero = (t) => t.sec === void 0;
|
|
795
|
+
function before(a, b) {
|
|
796
|
+
return cmp(a, b) < 0;
|
|
797
|
+
}
|
|
798
|
+
function after(a, b) {
|
|
799
|
+
return cmp(a, b) > 0;
|
|
800
|
+
}
|
|
801
|
+
function cmp(a, b) {
|
|
802
|
+
const as = a.sec ?? -62135596800;
|
|
803
|
+
const bs = b.sec ?? -62135596800;
|
|
804
|
+
if (as !== bs) return as < bs ? -1 : 1;
|
|
805
|
+
return a.nanos === b.nanos ? 0 : a.nanos < b.nanos ? -1 : 1;
|
|
806
|
+
}
|
|
807
|
+
function addMs(t, ms) {
|
|
808
|
+
const total = (t.sec ?? -62135596800) * 1e3 + Math.floor(t.nanos / 1e6) + ms;
|
|
809
|
+
const sec = Math.floor(total / 1e3);
|
|
810
|
+
return { sec, nanos: (total - sec * 1e3) * 1e6 + t.nanos % 1e6 };
|
|
811
|
+
}
|
|
812
|
+
function formatTime(t) {
|
|
813
|
+
if (t.sec === void 0) return "0001-01-01T00:00:00Z";
|
|
814
|
+
const d = new Date(t.sec * 1e3);
|
|
815
|
+
const p = (n, w = 2) => String(n).padStart(w, "0");
|
|
816
|
+
let s = `${p(d.getUTCFullYear(), 4)}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}T${p(d.getUTCHours())}:${p(d.getUTCMinutes())}:${p(d.getUTCSeconds())}`;
|
|
817
|
+
if (t.nanos > 0) s += "." + String(t.nanos).padStart(9, "0").replace(/0+$/, "");
|
|
818
|
+
return s + "Z";
|
|
819
|
+
}
|
|
820
|
+
function hexSerial(intContent) {
|
|
821
|
+
let neg = intContent.length > 0 && (intContent[0] & 128) !== 0;
|
|
822
|
+
let v = 0n;
|
|
823
|
+
for (const b of intContent) v = v << 8n | BigInt(b);
|
|
824
|
+
if (neg) v -= 1n << BigInt(intContent.length * 8);
|
|
825
|
+
neg = v < 0n;
|
|
826
|
+
return (neg ? "-" : "") + (neg ? -v : v).toString(16).toUpperCase();
|
|
827
|
+
}
|
|
828
|
+
var NO_RUNE = -1;
|
|
829
|
+
var PDF_DOC = (() => {
|
|
830
|
+
const t = new Array(256).fill(NO_RUNE);
|
|
831
|
+
t[9] = 9;
|
|
832
|
+
t[10] = 10;
|
|
833
|
+
t[13] = 13;
|
|
834
|
+
[728, 711, 710, 729, 733, 731, 730, 732].forEach((r, i) => t[24 + i] = r);
|
|
835
|
+
for (let i = 32; i < 127; i++) t[i] = i;
|
|
836
|
+
[
|
|
837
|
+
8226,
|
|
838
|
+
8224,
|
|
839
|
+
8225,
|
|
840
|
+
8230,
|
|
841
|
+
8212,
|
|
842
|
+
8211,
|
|
843
|
+
402,
|
|
844
|
+
8260,
|
|
845
|
+
8249,
|
|
846
|
+
8250,
|
|
847
|
+
8722,
|
|
848
|
+
8240,
|
|
849
|
+
8222,
|
|
850
|
+
8220,
|
|
851
|
+
8221,
|
|
852
|
+
8216,
|
|
853
|
+
8217,
|
|
854
|
+
8218,
|
|
855
|
+
8482,
|
|
856
|
+
64257,
|
|
857
|
+
64258,
|
|
858
|
+
321,
|
|
859
|
+
338,
|
|
860
|
+
352,
|
|
861
|
+
376,
|
|
862
|
+
381,
|
|
863
|
+
305,
|
|
864
|
+
322,
|
|
865
|
+
339,
|
|
866
|
+
353,
|
|
867
|
+
382,
|
|
868
|
+
NO_RUNE
|
|
869
|
+
].forEach((r, i) => t[128 + i] = r);
|
|
870
|
+
t[160] = 8364;
|
|
871
|
+
for (let i = 161; i <= 255; i++) t[i] = i;
|
|
872
|
+
t[173] = NO_RUNE;
|
|
873
|
+
return t;
|
|
874
|
+
})();
|
|
875
|
+
function pdfText(b) {
|
|
876
|
+
const utf16 = b.length >= 2 && b[0] === 254 && b[1] === 255 && b.length % 2 === 0;
|
|
877
|
+
if (!utf16 && b.every((x) => PDF_DOC[x] !== NO_RUNE)) return String.fromCodePoint(...Array.from(b, (x) => PDF_DOC[x]));
|
|
878
|
+
if (utf16) {
|
|
879
|
+
const u = [];
|
|
880
|
+
for (let i = 2; i < b.length; i += 2) u.push(b[i] << 8 | b[i + 1]);
|
|
881
|
+
return fixSurrogates(String.fromCharCode(...u));
|
|
882
|
+
}
|
|
883
|
+
return goString(b);
|
|
884
|
+
}
|
|
885
|
+
function fixSurrogates(s) {
|
|
886
|
+
return s.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, "\uFFFD");
|
|
887
|
+
}
|
|
888
|
+
function goString(b) {
|
|
889
|
+
let out = "";
|
|
890
|
+
for (let i = 0; i < b.length; ) {
|
|
891
|
+
const [r, n] = decodeRune(b, i);
|
|
892
|
+
out += String.fromCodePoint(r);
|
|
893
|
+
i += n;
|
|
894
|
+
}
|
|
895
|
+
return out;
|
|
896
|
+
}
|
|
897
|
+
function decodeRune(b, i) {
|
|
898
|
+
const c0 = b[i];
|
|
899
|
+
if (c0 < 128) return [c0, 1];
|
|
900
|
+
const bad = [65533, 1];
|
|
901
|
+
const cont = (k, lo = 128, hi = 191) => i + k < b.length && b[i + k] >= lo && b[i + k] <= hi;
|
|
902
|
+
if (c0 >= 194 && c0 <= 223) return cont(1) ? [(c0 & 31) << 6 | b[i + 1] & 63, 2] : bad;
|
|
903
|
+
if (c0 >= 224 && c0 <= 239) {
|
|
904
|
+
const lo = c0 === 224 ? 160 : 128;
|
|
905
|
+
const hi = c0 === 237 ? 159 : 191;
|
|
906
|
+
return cont(1, lo, hi) && cont(2) ? [(c0 & 15) << 12 | (b[i + 1] & 63) << 6 | b[i + 2] & 63, 3] : bad;
|
|
907
|
+
}
|
|
908
|
+
if (c0 >= 240 && c0 <= 244) {
|
|
909
|
+
const lo = c0 === 240 ? 144 : 128;
|
|
910
|
+
const hi = c0 === 244 ? 143 : 191;
|
|
911
|
+
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;
|
|
912
|
+
}
|
|
913
|
+
return bad;
|
|
914
|
+
}
|
|
915
|
+
var ATTR_NAMES = {
|
|
916
|
+
"2.5.4.6": "C",
|
|
917
|
+
"2.5.4.10": "O",
|
|
918
|
+
"2.5.4.11": "OU",
|
|
919
|
+
"2.5.4.3": "CN",
|
|
920
|
+
"2.5.4.5": "SERIALNUMBER",
|
|
921
|
+
"2.5.4.7": "L",
|
|
922
|
+
"2.5.4.8": "ST",
|
|
923
|
+
"2.5.4.9": "STREET",
|
|
924
|
+
"2.5.4.17": "POSTALCODE"
|
|
925
|
+
};
|
|
926
|
+
function asn1String(n) {
|
|
927
|
+
switch (n.tag) {
|
|
928
|
+
case TAG.PrintableString:
|
|
929
|
+
case TAG.IA5String:
|
|
930
|
+
case 18:
|
|
931
|
+
return goString(n.value);
|
|
932
|
+
case 20:
|
|
933
|
+
return goString(n.value);
|
|
934
|
+
case TAG.UTF8String:
|
|
935
|
+
return goString(n.value);
|
|
936
|
+
case 30: {
|
|
937
|
+
if (n.value.length % 2 !== 0) return void 0;
|
|
938
|
+
const u = [];
|
|
939
|
+
for (let i = 0; i < n.value.length; i += 2) u.push(n.value[i] << 8 | n.value[i + 1]);
|
|
940
|
+
if (u.length > 0 && u[u.length - 1] === 0) u.pop();
|
|
941
|
+
return fixSurrogates(String.fromCharCode(...u));
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return void 0;
|
|
945
|
+
}
|
|
946
|
+
function rdns(name) {
|
|
947
|
+
return children(name).map(
|
|
948
|
+
(rdn) => children(rdn).map((atv) => {
|
|
949
|
+
const [t, v] = children(atv);
|
|
950
|
+
return { oid: oidString(t.value), str: asn1String(v), raw: v };
|
|
951
|
+
})
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
function marshalString(s) {
|
|
955
|
+
const bytes = new TextEncoder().encode(s);
|
|
956
|
+
const printable = [...s].every((ch) => {
|
|
957
|
+
const c = ch.codePointAt(0);
|
|
958
|
+
return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 57 || " '()+,-./:=?".includes(ch);
|
|
959
|
+
});
|
|
960
|
+
const tag = printable ? TAG.PrintableString : TAG.UTF8String;
|
|
961
|
+
const len = bytes.length;
|
|
962
|
+
const hdr = len < 128 ? [tag, len] : len < 256 ? [tag, 129, len] : [tag, 130, len >> 8, len & 255];
|
|
963
|
+
const out = new Uint8Array(hdr.length + len);
|
|
964
|
+
out.set(hdr, 0);
|
|
965
|
+
out.set(bytes, hdr.length);
|
|
966
|
+
return out;
|
|
967
|
+
}
|
|
968
|
+
var toHex2 = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
969
|
+
function nameString(rawName) {
|
|
970
|
+
const seq2 = rdns(readNode(rawName, 0));
|
|
971
|
+
const f = { C: [], ST: [], L: [], STREET: [], POSTALCODE: [], O: [], OU: [] };
|
|
972
|
+
let cn = "";
|
|
973
|
+
let sn = "";
|
|
974
|
+
const all = [];
|
|
975
|
+
for (const rdn of seq2) {
|
|
976
|
+
for (const a of rdn) {
|
|
977
|
+
all.push(a);
|
|
978
|
+
if (a.str === void 0) continue;
|
|
979
|
+
switch (a.oid) {
|
|
980
|
+
case "2.5.4.3":
|
|
981
|
+
cn = a.str;
|
|
982
|
+
break;
|
|
983
|
+
case "2.5.4.5":
|
|
984
|
+
sn = a.str;
|
|
985
|
+
break;
|
|
986
|
+
case "2.5.4.6":
|
|
987
|
+
f.C.push(a.str);
|
|
988
|
+
break;
|
|
989
|
+
case "2.5.4.7":
|
|
990
|
+
f.L.push(a.str);
|
|
991
|
+
break;
|
|
992
|
+
case "2.5.4.8":
|
|
993
|
+
f.ST.push(a.str);
|
|
994
|
+
break;
|
|
995
|
+
case "2.5.4.9":
|
|
996
|
+
f.STREET.push(a.str);
|
|
997
|
+
break;
|
|
998
|
+
case "2.5.4.17":
|
|
999
|
+
f.POSTALCODE.push(a.str);
|
|
1000
|
+
break;
|
|
1001
|
+
case "2.5.4.10":
|
|
1002
|
+
f.O.push(a.str);
|
|
1003
|
+
break;
|
|
1004
|
+
case "2.5.4.11":
|
|
1005
|
+
f.OU.push(a.str);
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
const out = [];
|
|
1011
|
+
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"]);
|
|
1012
|
+
for (const a of all) if (!skip.has(a.oid)) out.push([a]);
|
|
1013
|
+
const add = (vals, oid2) => {
|
|
1014
|
+
if (vals.length > 0) out.push(vals.map((v) => ({ oid: oid2, str: v })));
|
|
1015
|
+
};
|
|
1016
|
+
add(f.C, "2.5.4.6");
|
|
1017
|
+
add(f.ST, "2.5.4.8");
|
|
1018
|
+
add(f.L, "2.5.4.7");
|
|
1019
|
+
add(f.STREET, "2.5.4.9");
|
|
1020
|
+
add(f.POSTALCODE, "2.5.4.17");
|
|
1021
|
+
add(f.O, "2.5.4.10");
|
|
1022
|
+
add(f.OU, "2.5.4.11");
|
|
1023
|
+
if (cn) add([cn], "2.5.4.3");
|
|
1024
|
+
if (sn) add([sn], "2.5.4.5");
|
|
1025
|
+
let s = "";
|
|
1026
|
+
for (let i = 0; i < out.length; i++) {
|
|
1027
|
+
const rdn = out[out.length - 1 - i];
|
|
1028
|
+
if (i > 0) s += ",";
|
|
1029
|
+
rdn.forEach((tv, j) => {
|
|
1030
|
+
if (j > 0) s += "+";
|
|
1031
|
+
const typeName = ATTR_NAMES[tv.oid];
|
|
1032
|
+
if (!typeName) {
|
|
1033
|
+
const der = tv.str !== void 0 ? marshalString(tv.str) : tv.raw.raw;
|
|
1034
|
+
s += `${tv.oid}=#${toHex2(der)}`;
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
const v = tv.str ?? "";
|
|
1038
|
+
const chars = [...v];
|
|
1039
|
+
let byteIdx = 0;
|
|
1040
|
+
const lastByte = new TextEncoder().encode(v).length - 1;
|
|
1041
|
+
let esc = "";
|
|
1042
|
+
for (const c of chars) {
|
|
1043
|
+
let e = false;
|
|
1044
|
+
if (',+"\\<>;'.includes(c)) e = true;
|
|
1045
|
+
else if (c === " ") e = byteIdx === 0 || byteIdx === lastByte;
|
|
1046
|
+
else if (c === "#") e = byteIdx === 0;
|
|
1047
|
+
esc += e ? "\\" + c : c;
|
|
1048
|
+
byteIdx += new TextEncoder().encode(c).length;
|
|
1049
|
+
}
|
|
1050
|
+
s += `${typeName}=${esc}`;
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
return s;
|
|
1054
|
+
}
|
|
1055
|
+
function commonName(rawName) {
|
|
1056
|
+
let cn = "";
|
|
1057
|
+
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;
|
|
1058
|
+
return cn;
|
|
1059
|
+
}
|
|
1060
|
+
function subjectNames(rawSubject) {
|
|
1061
|
+
let given = "";
|
|
1062
|
+
let surname = "";
|
|
1063
|
+
for (const rdn of rdns(readNode(rawSubject, 0))) {
|
|
1064
|
+
for (const a of rdn) {
|
|
1065
|
+
if (a.oid === "2.5.4.42") given = a.str ?? "";
|
|
1066
|
+
if (a.oid === "2.5.4.4") surname = a.str ?? "";
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
return { given, surname };
|
|
1070
|
+
}
|
|
1071
|
+
function subjectSerialNumber(rawSubject) {
|
|
1072
|
+
let sn = "";
|
|
1073
|
+
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;
|
|
1074
|
+
return sn;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// src/pades/revocation.ts
|
|
1078
|
+
var REVOCATION_MAX_AGE_MS = 24 * 36e5;
|
|
1079
|
+
var REVOCATION_CLOCK_SKEW_MS = 5 * 6e4;
|
|
1080
|
+
function freshAt(thisUpdate, nextUpdate, at, now) {
|
|
1081
|
+
if (isZero(thisUpdate) || after(thisUpdate, addMs(now, REVOCATION_CLOCK_SKEW_MS))) return false;
|
|
1082
|
+
if (!isZero(nextUpdate) && before(nextUpdate, addMs(at, -REVOCATION_CLOCK_SKEW_MS))) return false;
|
|
1083
|
+
if (!before(thisUpdate, addMs(at, -REVOCATION_MAX_AGE_MS))) return true;
|
|
1084
|
+
return !isZero(nextUpdate) && !after(thisUpdate, at);
|
|
1085
|
+
}
|
|
1086
|
+
var OCSP_HASHES = {
|
|
1087
|
+
"1.3.14.3.2.26": "SHA-1",
|
|
1088
|
+
"2.16.840.1.101.3.4.2.1": "SHA-256",
|
|
1089
|
+
"2.16.840.1.101.3.4.2.2": "SHA-384",
|
|
1090
|
+
"2.16.840.1.101.3.4.2.3": "SHA-512"
|
|
1091
|
+
};
|
|
1092
|
+
async function ocspCertIDMatches(der, cert, issuer) {
|
|
1093
|
+
try {
|
|
1094
|
+
const top = children(expect(readNode(der), TAG.SEQUENCE, "OCSPResponse"));
|
|
1095
|
+
const st = expect(top[0], TAG.ENUMERATED, "status");
|
|
1096
|
+
if (st.value.length !== 1 || st.value[0] !== 0) return false;
|
|
1097
|
+
const rb = children(expect(children(expect(top[1], 160, "responseBytes"))[0], TAG.SEQUENCE, "ResponseBytes"));
|
|
1098
|
+
expect(rb[0], TAG.OID, "responseType");
|
|
1099
|
+
const basic = children(expect(readNode(expect(rb[1], TAG.OCTET_STRING, "response").value), TAG.SEQUENCE, "BasicOCSPResponse"));
|
|
1100
|
+
const tf = children(expect(basic[0], TAG.SEQUENCE, "tbsResponseData"));
|
|
1101
|
+
let i = 0;
|
|
1102
|
+
if (tf[i]?.tag === 160) i++;
|
|
1103
|
+
if (!tf[i++]) return false;
|
|
1104
|
+
expect(tf[i++], TAG.GeneralizedTime, "producedAt");
|
|
1105
|
+
const singles = children(expect(tf[i], TAG.SEQUENCE, "responses"));
|
|
1106
|
+
const serial = intValue(cert.serial);
|
|
1107
|
+
let found = false;
|
|
1108
|
+
for (const single of singles) {
|
|
1109
|
+
const cid = children(expect(children(expect(single, TAG.SEQUENCE, "SingleResponse"))[0], TAG.SEQUENCE, "CertID"));
|
|
1110
|
+
const alg = oidString(expect(children(expect(cid[0], TAG.SEQUENCE, "hashAlgorithm"))[0], TAG.OID, "hash").value);
|
|
1111
|
+
const nameHash = expect(cid[1], TAG.OCTET_STRING, "issuerNameHash").value;
|
|
1112
|
+
const keyHash = expect(cid[2], TAG.OCTET_STRING, "issuerKeyHash").value;
|
|
1113
|
+
if (intValue(expect(cid[3], TAG.INTEGER, "serial").value) !== serial) continue;
|
|
1114
|
+
const h = OCSP_HASHES[alg];
|
|
1115
|
+
if (!h) return false;
|
|
1116
|
+
if (!equal(nameHash, await digest(h, issuer.rawSubject)) || !equal(keyHash, await digest(h, issuer.publicKey))) return false;
|
|
1117
|
+
found = true;
|
|
1118
|
+
}
|
|
1119
|
+
return found;
|
|
1120
|
+
} catch {
|
|
1121
|
+
return false;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
var OID_AKI = "2.5.29.35";
|
|
1125
|
+
var isTime = (n) => n !== void 0 && (n.tag === TAG.UTCTime || n.tag === TAG.GeneralizedTime);
|
|
1126
|
+
function parseCRL(der) {
|
|
1127
|
+
try {
|
|
1128
|
+
const top = children(expect(parseOne(der), TAG.SEQUENCE, "CertificateList"));
|
|
1129
|
+
const tbs = expect(top[0], TAG.SEQUENCE, "TBSCertList");
|
|
1130
|
+
const sigAlg = expect(top[1], TAG.SEQUENCE, "sigAlg");
|
|
1131
|
+
const sigBits = expect(top[2], TAG.BIT_STRING, "sig");
|
|
1132
|
+
const f = children(tbs);
|
|
1133
|
+
let i = 0;
|
|
1134
|
+
if (f[i]?.tag === TAG.INTEGER) i++;
|
|
1135
|
+
expect(f[i++], TAG.SEQUENCE, "signature");
|
|
1136
|
+
const issuer = expect(f[i++], TAG.SEQUENCE, "issuer");
|
|
1137
|
+
const tu = f[i++];
|
|
1138
|
+
if (!isTime(tu)) return void 0;
|
|
1139
|
+
const thisUpdate = fromPrecise(parsePreciseTime(tu));
|
|
1140
|
+
let nextUpdate = { nanos: 0 };
|
|
1141
|
+
if (isTime(f[i])) nextUpdate = fromPrecise(parsePreciseTime(f[i++]));
|
|
1142
|
+
const revoked = [];
|
|
1143
|
+
if (f[i]?.tag === TAG.SEQUENCE) {
|
|
1144
|
+
for (const e of children(f[i++])) {
|
|
1145
|
+
const p = children(expect(e, TAG.SEQUENCE, "entry"));
|
|
1146
|
+
revoked.push({ serial: intValue(expect(p[0], TAG.INTEGER, "serial").value), time: fromPrecise(parsePreciseTime(p[1])) });
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
let authorityKeyId = new Uint8Array();
|
|
1150
|
+
if (f[i]?.tag === 160) {
|
|
1151
|
+
for (const ext of children(expect(children(f[i])[0], TAG.SEQUENCE, "extensions"))) {
|
|
1152
|
+
const e = children(ext);
|
|
1153
|
+
if (oidString(expect(e[0], TAG.OID, "extnID").value) !== OID_AKI) continue;
|
|
1154
|
+
const k = children(parseOne(expect(e[e.length - 1], TAG.OCTET_STRING, "extnValue").value)).find((x) => x.tag === 128);
|
|
1155
|
+
if (k) authorityKeyId = k.value;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
return { rawIssuer: issuer.raw, tbs: tbs.raw, sigAlg, sig: sigBits.value.subarray(1), thisUpdate, nextUpdate, authorityKeyId, revoked };
|
|
1159
|
+
} catch {
|
|
1160
|
+
return void 0;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
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"]);
|
|
1164
|
+
async function crlSignedBy(crl, issuer) {
|
|
1165
|
+
if (issuer.version === 3 && !issuer.basicConstraintsValid || issuer.basicConstraintsValid && !issuer.isCA) return false;
|
|
1166
|
+
if (issuer.keyUsage !== 0 && !(issuer.keyUsage & 1 << 6)) return false;
|
|
1167
|
+
if (SHA1_SIG.has(oidString(children(crl.sigAlg)[0].value))) return false;
|
|
1168
|
+
try {
|
|
1169
|
+
return await verifySignature(issuer, crl.sigAlg, crl.tbs, crl.sig);
|
|
1170
|
+
} catch {
|
|
1171
|
+
return false;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
function subjectKeyId(c) {
|
|
1175
|
+
const v = c.extensions.find((e) => e.oid === "2.5.29.14")?.value;
|
|
1176
|
+
if (!v) return new Uint8Array();
|
|
1177
|
+
try {
|
|
1178
|
+
const n = parseOne(v);
|
|
1179
|
+
return n.tag === TAG.OCTET_STRING ? n.value : new Uint8Array();
|
|
1180
|
+
} catch {
|
|
1181
|
+
return new Uint8Array();
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
async function crlFor(crl, cert, issuer) {
|
|
1185
|
+
if (!equal(crl.rawIssuer, cert.rawIssuer) || !equal(crl.rawIssuer, issuer.rawSubject)) return false;
|
|
1186
|
+
const ski = subjectKeyId(issuer);
|
|
1187
|
+
if (crl.authorityKeyId.length > 0 && ski.length > 0 && !equal(crl.authorityKeyId, ski)) return false;
|
|
1188
|
+
return crlSignedBy(crl, issuer);
|
|
1189
|
+
}
|
|
1190
|
+
function crlListed(crl, cert) {
|
|
1191
|
+
const serial = intValue(cert.serial);
|
|
1192
|
+
return crl.revoked.find((e) => e.serial === serial)?.time;
|
|
1193
|
+
}
|
|
1194
|
+
var CertificateRevokedError = class extends Error {
|
|
1195
|
+
constructor() {
|
|
1196
|
+
super("\u0433\u044D\u0440\u0447\u0438\u043B\u0433\u044D\u044D \u0446\u0443\u0446\u043B\u0430\u0433\u0434\u0441\u0430\u043D");
|
|
1197
|
+
this.name = "CertificateRevokedError";
|
|
1198
|
+
}
|
|
1199
|
+
};
|
|
1200
|
+
|
|
784
1201
|
// src/pades/ltv.ts
|
|
785
1202
|
var MAX_TSA_SKEW_MS = 10 * 6e4;
|
|
786
1203
|
var tsMs = (t) => t.date.getTime();
|
|
787
|
-
function
|
|
1204
|
+
function after2(a, b) {
|
|
788
1205
|
const am = Math.floor(a.date.getTime() / 1e3);
|
|
789
1206
|
const bm = Math.floor(b.date.getTime() / 1e3);
|
|
790
1207
|
if (am !== bm) return am > bm;
|
|
@@ -894,9 +1311,9 @@ async function parseOCSP(der, serial, issuer) {
|
|
|
894
1311
|
if (!status) throw new Error("OCSP certStatus");
|
|
895
1312
|
const revokedAt = status === "revoked" ? parsePreciseTime(expect(children(st)[0], TAG.GeneralizedTime, "revocationTime")) : void 0;
|
|
896
1313
|
const thisUpdate = parsePreciseTime(expect(single[2], TAG.GeneralizedTime, "thisUpdate"));
|
|
897
|
-
let
|
|
1314
|
+
let nextUpdatePrecise;
|
|
898
1315
|
for (const x of single.slice(3)) {
|
|
899
|
-
if (x.tag === 160)
|
|
1316
|
+
if (x.tag === 160) nextUpdatePrecise = parsePreciseTime(children(x)[0]);
|
|
900
1317
|
if (x.tag === 161) {
|
|
901
1318
|
for (const ext of children(children(x)[0])) {
|
|
902
1319
|
const e = children(ext);
|
|
@@ -904,7 +1321,7 @@ async function parseOCSP(der, serial, issuer) {
|
|
|
904
1321
|
}
|
|
905
1322
|
}
|
|
906
1323
|
}
|
|
907
|
-
return { status, serial: intValue(expect(cid[3], TAG.INTEGER, "serial").value), thisUpdate, nextUpdate, revokedAt, certificate };
|
|
1324
|
+
return { status, serial: intValue(expect(cid[3], TAG.INTEGER, "serial").value), thisUpdate, nextUpdate: nextUpdatePrecise?.date, nextUpdatePrecise, revokedAt, certificate };
|
|
908
1325
|
}
|
|
909
1326
|
function sameCertKey(a, b) {
|
|
910
1327
|
return equal(a.raw, b.raw) || equal(a.rawSubject, b.rawSubject) && equal(a.spki, b.spki);
|
|
@@ -935,45 +1352,26 @@ function responderOK(resp, issuer) {
|
|
|
935
1352
|
}
|
|
936
1353
|
var NotFresh = class extends Error {
|
|
937
1354
|
};
|
|
1355
|
+
function ocspFreshAt(resp, at, now) {
|
|
1356
|
+
return freshAt(fromPrecise(resp.thisUpdate), resp.nextUpdatePrecise ? fromPrecise(resp.nextUpdatePrecise) : ZERO_TIME, at, now);
|
|
1357
|
+
}
|
|
938
1358
|
async function fetchOCSP(f, url, c, issuer, now, fresh) {
|
|
939
1359
|
const der = await f.do("POST", url, "application/ocsp-request", await ocspRequest(c, issuer));
|
|
940
1360
|
const resp = await parseOCSPForCert(der, c, issuer);
|
|
941
1361
|
if (!responderOK(resp, issuer)) throw new Error("OCSP responder-\u0438\u0439\u043D EKU OCSPSigning \u0431\u0438\u0448");
|
|
942
|
-
if (
|
|
943
|
-
if (
|
|
1362
|
+
if (!await ocspCertIDMatches(der, c, issuer)) throw new Error("OCSP CertID \u044D\u043D\u044D \u0433\u044D\u0440\u0447\u0438\u043B\u0433\u044D\u044D/\u043E\u043B\u0433\u043E\u0433\u0447\u0438\u0439\u043D\u0445 \u0431\u0438\u0448");
|
|
1363
|
+
if (resp.status === "revoked") throw new CertificateRevokedError();
|
|
1364
|
+
const n = fromDate(now);
|
|
1365
|
+
if (resp.status !== "good" || !ocspFreshAt(resp, n, n)) throw new Error(`OCSP status ${resp.status}`);
|
|
1366
|
+
if (fresh && !after2(resp.thisUpdate, fresh)) throw new NotFresh("OCSP thisUpdate \u043D\u044C timestamp-\u0430\u0430\u0441 \u0445\u043E\u0436\u0443\u0443 \u0431\u0438\u0448");
|
|
944
1367
|
return [der, resp];
|
|
945
1368
|
}
|
|
946
1369
|
async function crlCovers(der, c, issuer, now) {
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
const f = children(tbs);
|
|
953
|
-
let i = 0;
|
|
954
|
-
if (f[0]?.tag === TAG.INTEGER) i++;
|
|
955
|
-
i++;
|
|
956
|
-
const iss = expect(f[i++], TAG.SEQUENCE, "issuer");
|
|
957
|
-
if (!equal(iss.raw, c.rawIssuer)) return false;
|
|
958
|
-
if (issuer.version === 3 && !issuer.basicConstraintsValid) return false;
|
|
959
|
-
if (issuer.keyUsage !== 0 && !(issuer.keyUsage & 1 << 6)) return false;
|
|
960
|
-
if (!await verifySignature(issuer, sigAlg, tbs.raw, sig)) return false;
|
|
961
|
-
i++;
|
|
962
|
-
const nu = f[i];
|
|
963
|
-
if (!nu || nu.tag !== TAG.UTCTime && nu.tag !== TAG.GeneralizedTime) return false;
|
|
964
|
-
if (parsePreciseTime(nu).date.getTime() < now.getTime()) return false;
|
|
965
|
-
i++;
|
|
966
|
-
const serial = intValue(c.serial);
|
|
967
|
-
const revoked = f[i];
|
|
968
|
-
if (revoked?.tag === TAG.SEQUENCE) {
|
|
969
|
-
for (const e of children(revoked)) {
|
|
970
|
-
if (intValue(expect(children(e)[0], TAG.INTEGER, "serial").value) === serial) return false;
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return true;
|
|
974
|
-
} catch {
|
|
975
|
-
return false;
|
|
976
|
-
}
|
|
1370
|
+
const crl = parseCRL(der);
|
|
1371
|
+
if (!crl || !await crlFor(crl, c, issuer)) return false;
|
|
1372
|
+
if (crlListed(crl, c)) throw new CertificateRevokedError();
|
|
1373
|
+
const n = fromDate(now);
|
|
1374
|
+
return !isZero(crl.nextUpdate) && freshAt(crl.thisUpdate, crl.nextUpdate, n, n);
|
|
977
1375
|
}
|
|
978
1376
|
async function collectLTV(f, now, fresh, roots, chains) {
|
|
979
1377
|
const d = { certs: [], ocsps: [], crls: [] };
|
|
@@ -995,7 +1393,7 @@ async function collectLTV(f, now, fresh, roots, chains) {
|
|
|
995
1393
|
seen.add(k);
|
|
996
1394
|
addTo(d.certs, c.raw);
|
|
997
1395
|
const issuer = await issuerOf(c);
|
|
998
|
-
if (!issuer) throw new LTVError(
|
|
1396
|
+
if (!issuer) throw new LTVError(`\u0433\u0438\u043D\u0436\u0438\u0439\u043D \u0433\u044D\u0440\u0447\u0438\u043B\u0433\u044D\u044D\u043D\u0438\u0439 \u043E\u043B\u0433\u043E\u0433\u0447 \u043E\u043B\u0434\u0441\u043E\u043D\u0433\u04AF\u0439`);
|
|
999
1397
|
let ok = false;
|
|
1000
1398
|
let freshErr;
|
|
1001
1399
|
const fr = i === 0 ? fresh : void 0;
|
|
@@ -1007,6 +1405,7 @@ async function collectLTV(f, now, fresh, roots, chains) {
|
|
|
1007
1405
|
ok = true;
|
|
1008
1406
|
break;
|
|
1009
1407
|
} catch (e) {
|
|
1408
|
+
if (e instanceof CertificateRevokedError) throw e;
|
|
1010
1409
|
if (e instanceof NotFresh) freshErr = e;
|
|
1011
1410
|
}
|
|
1012
1411
|
}
|
|
@@ -1024,7 +1423,7 @@ async function collectLTV(f, now, fresh, roots, chains) {
|
|
|
1024
1423
|
}
|
|
1025
1424
|
if (!ok) {
|
|
1026
1425
|
if (freshErr) throw freshErr;
|
|
1027
|
-
throw new LTVError(
|
|
1426
|
+
throw new LTVError(`\u0433\u0438\u043D\u0436\u0438\u0439\u043D \u0433\u044D\u0440\u0447\u0438\u043B\u0433\u044D\u044D\u043D\u0434 \u0445\u04AF\u0447\u0438\u043D\u0442\u044D\u0439 OCSP/CRL \u0430\u043B\u0433\u0430`);
|
|
1028
1427
|
}
|
|
1029
1428
|
if (i === 0 && freshErr && c.ocspServers.length > 0 && !await hasFreshOCSP(d.ocsps, c, issuer, fresh)) throw freshErr;
|
|
1030
1429
|
}
|
|
@@ -1034,7 +1433,7 @@ async function collectLTV(f, now, fresh, roots, chains) {
|
|
|
1034
1433
|
async function hasFreshOCSP(list, c, issuer, fresh) {
|
|
1035
1434
|
for (const der of list) {
|
|
1036
1435
|
try {
|
|
1037
|
-
if (
|
|
1436
|
+
if (after2((await parseOCSPForCert(der, c, issuer)).thisUpdate, fresh) && await ocspCertIDMatches(der, c, issuer)) return true;
|
|
1038
1437
|
} catch {
|
|
1039
1438
|
}
|
|
1040
1439
|
}
|
|
@@ -1101,11 +1500,33 @@ export {
|
|
|
1101
1500
|
assembleCMS,
|
|
1102
1501
|
parseSignedData,
|
|
1103
1502
|
verifySignedData,
|
|
1503
|
+
ZERO_TIME,
|
|
1504
|
+
fromPrecise,
|
|
1505
|
+
fromDate,
|
|
1506
|
+
isZero,
|
|
1507
|
+
before,
|
|
1508
|
+
after,
|
|
1509
|
+
addMs,
|
|
1510
|
+
formatTime,
|
|
1511
|
+
hexSerial,
|
|
1512
|
+
pdfText,
|
|
1513
|
+
nameString,
|
|
1514
|
+
commonName,
|
|
1515
|
+
subjectNames,
|
|
1516
|
+
subjectSerialNumber,
|
|
1517
|
+
freshAt,
|
|
1518
|
+
ocspCertIDMatches,
|
|
1519
|
+
parseCRL,
|
|
1520
|
+
SHA1_SIG,
|
|
1521
|
+
crlFor,
|
|
1522
|
+
crlListed,
|
|
1523
|
+
CertificateRevokedError,
|
|
1104
1524
|
timestampToken,
|
|
1105
1525
|
ocspRequest,
|
|
1106
1526
|
parseOCSP,
|
|
1107
1527
|
sameCertKey,
|
|
1108
1528
|
parseOCSPFrom,
|
|
1529
|
+
ocspFreshAt,
|
|
1109
1530
|
LTVError,
|
|
1110
1531
|
collectLTVWithWait
|
|
1111
1532
|
};
|