micro509 0.14.0 → 0.16.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/CHANGELOG.md +2119 -0
- package/README.md +105 -225
- package/dist/crypto/crypto.d.ts +42 -11
- package/dist/crypto/crypto.js +53 -22
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/crypto.d.ts +2 -2
- package/dist/crypto.js +1 -2
- package/dist/der/der.d.ts +41 -33
- package/dist/der/der.js +26 -24
- package/dist/der/der.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +3 -6
- package/dist/internal/asn1/asn1.js +343 -43
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js +146 -0
- package/dist/internal/asn1/ber.js.map +1 -0
- package/dist/internal/asn1/decode-refusal.js +29 -0
- package/dist/internal/asn1/decode-refusal.js.map +1 -0
- package/dist/internal/asn1/der.d.ts +38 -41
- package/dist/internal/asn1/der.js +131 -52
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +83 -53
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids2.js +1 -2
- package/dist/internal/asn1/oids2.js.map +1 -1
- package/dist/internal/crypto/algorithm-names.js +1 -2
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js +13 -8
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js +1 -2
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +24 -11
- package/dist/internal/crypto/pbes2.js +79 -26
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/rsa-pss.js +46 -44
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +4 -7
- package/dist/internal/crypto/sig-verify.js +107 -60
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -4
- package/dist/internal/crypto/signing.js +28 -29
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/crypto/webcrypto.js +1 -2
- package/dist/internal/crypto/webcrypto.js.map +1 -1
- package/dist/internal/shared/base64.js +1 -2
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +3 -6
- package/dist/internal/shared/dn.js +99 -53
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna-tables.js +7217 -0
- package/dist/internal/shared/idna-tables.js.map +1 -0
- package/dist/internal/shared/idna.js +296 -0
- package/dist/internal/shared/idna.js.map +1 -0
- package/dist/internal/shared/ip.d.ts +4 -7
- package/dist/internal/shared/ip.js +1 -2
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js +26 -0
- package/dist/internal/shared/mailbox.js.map +1 -0
- package/dist/internal/shared/punycode.js +101 -0
- package/dist/internal/shared/punycode.js.map +1 -0
- package/dist/internal/shared/rfc3454-tables.js +1 -2
- package/dist/internal/shared/rfc3454-tables.js.map +1 -1
- package/dist/internal/shared/rfc4518-tables.js +119 -0
- package/dist/internal/shared/rfc4518-tables.js.map +1 -0
- package/dist/internal/shared/uri-host.js +102 -0
- package/dist/internal/shared/uri-host.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +246 -131
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +6 -21
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/spki-distrust.js +23 -0
- package/dist/internal/verify/spki-distrust.js.map +1 -0
- package/dist/internal/verify/verify-path.d.ts +1 -4
- package/dist/internal/verify/verify-path.js +282 -129
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +1 -2
- package/dist/internal/x509/directory-name.js.map +1 -1
- package/dist/internal/x509/extension-bits.js +2 -3
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +1 -4
- package/dist/internal/x509/extension-errors.js +1 -2
- package/dist/internal/x509/extension-errors.js.map +1 -1
- package/dist/internal/x509/extension-registry.js +107 -89
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +129 -0
- package/dist/internal/x509/general-name-profile.js.map +1 -0
- package/dist/internal/x509/general-name-tags.js +1 -2
- package/dist/internal/x509/general-name-tags.js.map +1 -1
- package/dist/internal/x509/general-name.js +128 -42
- package/dist/internal/x509/general-name.js.map +1 -1
- package/dist/internal/x509/name-fields.js +1 -2
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js +217 -0
- package/dist/internal/x509/or-address.js.map +1 -0
- package/dist/keys/keys.d.ts +122 -104
- package/dist/keys/keys.js +211 -204
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +3 -3
- package/dist/pem/pem.d.ts +14 -17
- package/dist/pem/pem.js +16 -17
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.js +1 -2
- package/dist/pkcs/pfx.d.ts +57 -32
- package/dist/pkcs/pfx.js +209 -173
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +104 -27
- package/dist/pkcs/pkcs12-mac.js +266 -79
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +43 -37
- package/dist/pkcs/pkcs7.js +218 -217
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +4 -3
- package/dist/result/result.d.ts +27 -20
- package/dist/result/result.js +8 -3
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +2 -2
- package/dist/result.js +1 -2
- package/dist/revocation/chain.d.ts +101 -23
- package/dist/revocation/chain.js +436 -182
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +131 -67
- package/dist/revocation/crl.js +443 -327
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +94 -59
- package/dist/revocation/ocsp.js +331 -293
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +47 -35
- package/dist/revocation/revocation.js +88 -57
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +3 -3
- package/dist/verify/identity.d.ts +16 -19
- package/dist/verify/identity.js +118 -89
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +1 -4
- package/dist/verify/policy.d.ts +9 -7
- package/dist/verify/verify.d.ts +115 -46
- package/dist/verify/verify.js +373 -183
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +2 -2
- package/dist/x509/certificate.d.ts +20 -21
- package/dist/x509/certificate.js +62 -46
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +5 -7
- package/dist/x509/csr.js +10 -11
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +154 -66
- package/dist/x509/extensions.js +317 -151
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +15 -7
- package/dist/x509/fingerprint.js +20 -3
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +7 -9
- package/dist/x509/name-text.js +18 -4
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +9 -11
- package/dist/x509/name.js +18 -19
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +84 -69
- package/dist/x509/parse.js +421 -400
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +3 -3
- package/dist/x509.js +3 -3
- package/package.json +76 -59
package/dist/pkcs/pkcs7.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
1
|
+
import { DECODE_REFUSAL_CODES as e, failureResult as t, rethrowIfInvariant as n } from "../result/result.js";
|
|
2
|
+
import { decodeRefusalOf as r } from "../internal/asn1/decode-refusal.js";
|
|
3
|
+
import { explicitContext as i, integer as a, integerFromNumber as o, objectIdentifier as s, octetString as c, readElement as l, readRootElement as u, readSequenceChildren as d, sequence as f, setOf as p } from "../internal/asn1/der.js";
|
|
4
|
+
import { childrenOf as m, decodeIntegerNumber as h, decodeObjectIdentifier as g, decodeString as _, hexToBytes as v, requireElement as y, toArrayBuffer as b, toHex as x } from "../internal/asn1/asn1.js";
|
|
5
|
+
import { OIDS as S } from "../internal/asn1/oids2.js";
|
|
6
|
+
import { getCrypto as C } from "../internal/crypto/webcrypto.js";
|
|
7
|
+
import { base64Encode as w } from "../internal/shared/base64.js";
|
|
8
|
+
import { pemEncode as T, splitPemBlocksOrThrow as E } from "../pem/pem.js";
|
|
9
|
+
import { getVerifySignatureConfigResult as D, verifySignedDataDetailed as O } from "../internal/crypto/sig-verify.js";
|
|
10
|
+
import { encodeAlgorithmIdentifier as k, getSignatureAlgorithm as A, signBytes as j } from "../internal/crypto/signing.js";
|
|
11
|
+
import { describeHashAlgorithm as M, describeSignatureAlgorithm as N } from "../internal/crypto/algorithm-names.js";
|
|
12
|
+
import { nameFieldKeyFromOid as P } from "../internal/x509/name-fields.js";
|
|
13
|
+
import { compareDistinguishedNames as F } from "../internal/shared/dn.js";
|
|
14
|
+
import { parseCertificateDerOrThrow as I } from "../x509/parse.js";
|
|
15
|
+
// src/pkcs/pkcs7.ts
|
|
15
16
|
/**
|
|
16
17
|
* PKCS#7/CMS certificate bags and SignedData.
|
|
17
18
|
*
|
|
@@ -24,7 +25,7 @@ import { encodeAlgorithmIdentifier as j, getSignatureAlgorithm as M, signBytes a
|
|
|
24
25
|
* @module
|
|
25
26
|
*/
|
|
26
27
|
/** RFC 7468 Section 8 armors an RFC 2315 ContentInfo; Section 9 armors an RFC 5652 one. */
|
|
27
|
-
const
|
|
28
|
+
const L = ["PKCS7", "CMS"];
|
|
28
29
|
/**
|
|
29
30
|
* Creates a degenerate PKCS#7 SignedData structure containing only
|
|
30
31
|
* certificates (no signers), returning DER, PEM, and base64 forms, or a
|
|
@@ -32,28 +33,29 @@ const P = ["PKCS7", "CMS"];
|
|
|
32
33
|
* valid PEM/DER.
|
|
33
34
|
*/
|
|
34
35
|
function createPkcs7CertBag(t) {
|
|
35
|
-
let
|
|
36
|
+
let n;
|
|
36
37
|
try {
|
|
37
|
-
|
|
38
|
-
for (let e of
|
|
39
|
-
} catch {
|
|
40
|
-
|
|
38
|
+
n = t.flatMap(normalizeCertificateSource);
|
|
39
|
+
for (let e of n) I(e);
|
|
40
|
+
} catch (t) {
|
|
41
|
+
let n = r(t, e);
|
|
42
|
+
return n === void 0 ? createCertBagFailure("invalid_certificate", "Each PKCS#7 certificate source must be valid PEM or DER") : createCertBagFailure(n.code, n.message);
|
|
41
43
|
}
|
|
42
|
-
let a = new Uint8Array(
|
|
44
|
+
let a = new Uint8Array(p(n));
|
|
43
45
|
a[0] = 160;
|
|
44
|
-
let
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
let c = f([
|
|
47
|
+
o(1),
|
|
48
|
+
p([]),
|
|
49
|
+
f([s(S.pkcs7Data)]),
|
|
48
50
|
a,
|
|
49
|
-
|
|
50
|
-
]),
|
|
51
|
+
p([])
|
|
52
|
+
]), l = f([s(S.pkcs7SignedData), i(0, c)]);
|
|
51
53
|
return {
|
|
52
54
|
ok: !0,
|
|
53
55
|
value: {
|
|
54
|
-
der:
|
|
55
|
-
pem:
|
|
56
|
-
base64:
|
|
56
|
+
der: l,
|
|
57
|
+
pem: T("PKCS7", l),
|
|
58
|
+
base64: w(l)
|
|
57
59
|
}
|
|
58
60
|
};
|
|
59
61
|
}
|
|
@@ -79,35 +81,36 @@ function createPkcs7CertBag(t) {
|
|
|
79
81
|
*/
|
|
80
82
|
async function createPkcs7SignedData(t) {
|
|
81
83
|
if (t.signers.length === 0) return createPkcs7Failure("no_signers", "createPkcs7SignedData requires at least one signer");
|
|
82
|
-
let
|
|
83
|
-
let t =
|
|
84
|
-
u.has(t) || (u.add(t),
|
|
85
|
-
}, d = /* @__PURE__ */ new Set(),
|
|
84
|
+
let n = t.encapsulatedContentTypeOid ?? S.pkcs7Data, a = t.content.slice(), l = [], u = /* @__PURE__ */ new Set(), addCertificate = (e) => {
|
|
85
|
+
let t = x(e);
|
|
86
|
+
u.has(t) || (u.add(t), l.push(e));
|
|
87
|
+
}, d = /* @__PURE__ */ new Set(), m = [];
|
|
86
88
|
for (let e of t.signers) {
|
|
87
|
-
let t = await buildPkcs7SignerInfo(e,
|
|
89
|
+
let t = await buildPkcs7SignerInfo(e, a, n);
|
|
88
90
|
if (!t.ok) return t;
|
|
89
|
-
addCertificate(t.certificateDer), d.add(t.digestOid),
|
|
91
|
+
addCertificate(t.certificateDer), d.add(t.digestOid), m.push(t.signerInfoDer);
|
|
90
92
|
}
|
|
91
|
-
for (let
|
|
92
|
-
for (let
|
|
93
|
-
} catch {
|
|
94
|
-
|
|
93
|
+
for (let n of t.additionalCertificates ?? []) try {
|
|
94
|
+
for (let e of normalizeCertificateSource(n)) I(e), addCertificate(e);
|
|
95
|
+
} catch (t) {
|
|
96
|
+
let n = r(t, e);
|
|
97
|
+
return n === void 0 ? createPkcs7Failure("invalid_certificate", "Each additional PKCS#7 certificate source must be valid PEM or DER") : createPkcs7Failure(n.code, n.message);
|
|
95
98
|
}
|
|
96
|
-
let
|
|
97
|
-
|
|
98
|
-
let
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
t.detached === !0 ?
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
]),
|
|
99
|
+
let h = n === S.pkcs7Data ? 1 : 3, g = new Uint8Array(p(l));
|
|
100
|
+
g[0] = 160;
|
|
101
|
+
let _ = f([
|
|
102
|
+
o(h),
|
|
103
|
+
p([...d].map((e) => f([s(e)]))),
|
|
104
|
+
t.detached === !0 ? f([s(n)]) : f([s(n), i(0, c(a))]),
|
|
105
|
+
g,
|
|
106
|
+
p(m)
|
|
107
|
+
]), v = f([s(S.pkcs7SignedData), i(0, _)]);
|
|
105
108
|
return {
|
|
106
109
|
ok: !0,
|
|
107
110
|
value: {
|
|
108
|
-
der:
|
|
109
|
-
pem:
|
|
110
|
-
base64:
|
|
111
|
+
der: v,
|
|
112
|
+
pem: T(h === 1 ? "PKCS7" : "CMS", v),
|
|
113
|
+
base64: w(v)
|
|
111
114
|
}
|
|
112
115
|
};
|
|
113
116
|
}
|
|
@@ -117,48 +120,49 @@ async function createPkcs7SignedData(t) {
|
|
|
117
120
|
* caller-correctable input — malformed PEM/DER, unsupported key — so each is
|
|
118
121
|
* mapped to a typed failure rather than rejecting the public Promise.
|
|
119
122
|
*/
|
|
120
|
-
async function buildPkcs7SignerInfo(
|
|
121
|
-
let
|
|
123
|
+
async function buildPkcs7SignerInfo(t, n, i) {
|
|
124
|
+
let l;
|
|
122
125
|
try {
|
|
123
|
-
|
|
126
|
+
l = normalizeCertificateSource(t.certificate);
|
|
124
127
|
} catch {
|
|
125
128
|
return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate source must be valid PEM or DER");
|
|
126
129
|
}
|
|
127
|
-
let
|
|
128
|
-
if (
|
|
129
|
-
let
|
|
130
|
+
let u = l[0];
|
|
131
|
+
if (u === void 0 || l.length !== 1) return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer must provide exactly one certificate");
|
|
132
|
+
let d;
|
|
130
133
|
try {
|
|
131
|
-
|
|
132
|
-
} catch {
|
|
133
|
-
|
|
134
|
+
d = I(u);
|
|
135
|
+
} catch (t) {
|
|
136
|
+
let n = r(t, e);
|
|
137
|
+
return n === void 0 ? createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate must be a parseable X.509 certificate") : createPkcs7Failure(n.code, n.message);
|
|
134
138
|
}
|
|
135
|
-
let
|
|
139
|
+
let p;
|
|
136
140
|
try {
|
|
137
|
-
|
|
141
|
+
p = A(t.privateKey, t.signature);
|
|
138
142
|
} catch {
|
|
139
143
|
return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key or signature profile");
|
|
140
144
|
}
|
|
141
|
-
if (!
|
|
142
|
-
let
|
|
143
|
-
if (
|
|
144
|
-
let { setForSigning:
|
|
145
|
+
if (!D(p.algorithmOid, p.parameters, d.publicKeyAlgorithmOid, d.publicKeyParametersOid, "signer").ok) return createPkcs7Failure("signer_certificate_key_mismatch", "Each PKCS#7 signer certificate must carry a public key that verifies the signer key algorithm");
|
|
146
|
+
let m = contentDigestForPrivateKey(t.privateKey);
|
|
147
|
+
if (m === void 0) return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key algorithm for content digest");
|
|
148
|
+
let { setForSigning: h, implicitForEmit: g } = buildSignedAttributes(i, new Uint8Array(await C().subtle.digest(m.hashName, b(n)))), _;
|
|
145
149
|
try {
|
|
146
|
-
_ = await
|
|
150
|
+
_ = await j(t.privateKey, p, h);
|
|
147
151
|
} catch {
|
|
148
152
|
return createPkcs7Failure("unsupported_signer_key", "Signer key could not produce a PKCS#7 signature");
|
|
149
153
|
}
|
|
150
154
|
return {
|
|
151
155
|
ok: !0,
|
|
152
|
-
signerInfoDer:
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
signerInfoDer: f([
|
|
157
|
+
o(1),
|
|
158
|
+
f([v(d.issuer.derHex), a(v(d.serialNumberHex))]),
|
|
159
|
+
f([s(m.digestOid)]),
|
|
160
|
+
g,
|
|
161
|
+
k(p),
|
|
162
|
+
c(_)
|
|
159
163
|
]),
|
|
160
|
-
digestOid:
|
|
161
|
-
certificateDer:
|
|
164
|
+
digestOid: m.digestOid,
|
|
165
|
+
certificateDer: u
|
|
162
166
|
};
|
|
163
167
|
}
|
|
164
168
|
/** Parses a DER-encoded PKCS#7 cert bag, returning the contained certificates. */
|
|
@@ -172,7 +176,7 @@ function parsePkcs7CertBagDer(e) {
|
|
|
172
176
|
/** The DER of the single `PKCS7` or `CMS` block in `pem`, or a typed failure. */
|
|
173
177
|
function contentInfoPemBytes(e) {
|
|
174
178
|
try {
|
|
175
|
-
let t =
|
|
179
|
+
let t = E(e).filter((e) => L.some((t) => t === e.label)), n = t[0];
|
|
176
180
|
return n === void 0 || t.length !== 1 ? pkcs7Failure("malformed", "Expected exactly one PKCS7 or CMS PEM block") : {
|
|
177
181
|
ok: !0,
|
|
178
182
|
bytes: n.bytes
|
|
@@ -187,51 +191,47 @@ function parsePkcs7CertBagPem(e) {
|
|
|
187
191
|
return t.ok ? parsePkcs7CertBagDer(t.bytes) : t;
|
|
188
192
|
}
|
|
189
193
|
/** Decodes a DER-encoded PKCS#7 ContentInfo expecting `signedData` content type. */
|
|
190
|
-
function parsePkcs7SignedDataDer(
|
|
194
|
+
function parsePkcs7SignedDataDer(t) {
|
|
191
195
|
try {
|
|
192
|
-
let
|
|
193
|
-
if (n === void 0 ||
|
|
194
|
-
let i =
|
|
195
|
-
if (i !==
|
|
196
|
+
let e = d(t, { maxDepth: 64 }), n = e[0], r = e[1];
|
|
197
|
+
if (n === void 0 || e.length > 2 || n.tag !== 6 || r !== void 0 && (r.tag !== 160 || m(t, r).length !== 1)) return pkcs7Failure("malformed", "Malformed PKCS#7 content info");
|
|
198
|
+
let i = g(n.value);
|
|
199
|
+
if (i !== S.pkcs7SignedData) return pkcs7Failure("not_signed_data", "PKCS#7 content is not signedData");
|
|
196
200
|
if (r === void 0) return pkcs7Failure("malformed", "PKCS#7 signedData content info carries no content");
|
|
197
|
-
let a =
|
|
198
|
-
if (!
|
|
199
|
-
if (
|
|
200
|
-
let
|
|
201
|
-
return y === void 0 ||
|
|
201
|
+
let a = childAt(t, r, 0, "signedData"), o = m(t, a), s = o[0], c = o[1], l = o[2], u = o.slice(3), f = u[u.length - 1], p = parseSignedDataOptionalFields(u.slice(0, -1));
|
|
202
|
+
if (!p.ok) return p;
|
|
203
|
+
if (s === void 0 || c === void 0 || l === void 0 || f === void 0 || f.tag !== 49) return pkcs7Failure("malformed", "Malformed SignedData");
|
|
204
|
+
let _ = t.slice(l.start - l.headerLength, l.end), v = d(_), y = v[0], b = v[1], x = parseDigestAlgorithms(t, c);
|
|
205
|
+
return y === void 0 || v.length > 2 || y.tag !== 6 ? pkcs7Failure("malformed", "Malformed EncapsulatedContentInfo") : {
|
|
202
206
|
ok: !0,
|
|
203
207
|
value: {
|
|
204
|
-
der: new Uint8Array(
|
|
208
|
+
der: new Uint8Array(t),
|
|
205
209
|
contentTypeOid: i,
|
|
206
|
-
version:
|
|
210
|
+
version: h(s.value),
|
|
207
211
|
digestAlgorithmOids: x,
|
|
208
|
-
digestAlgorithmNames: x.map((e) =>
|
|
209
|
-
encapsulatedContentTypeOid:
|
|
210
|
-
...b === void 0 ? {} : { encapsulatedContent: extractEncapsulatedContent(
|
|
211
|
-
certificateChoices: parseCertificateSet(
|
|
212
|
-
signerInfos: parseSignerInfos(
|
|
212
|
+
digestAlgorithmNames: x.map((e) => M(e)),
|
|
213
|
+
encapsulatedContentTypeOid: g(y.value),
|
|
214
|
+
...b === void 0 ? {} : { encapsulatedContent: extractEncapsulatedContent(_, b) },
|
|
215
|
+
certificateChoices: parseCertificateSet(t, p.value.certificates),
|
|
216
|
+
signerInfos: parseSignerInfos(t, f)
|
|
213
217
|
}
|
|
214
218
|
};
|
|
215
|
-
} catch {
|
|
216
|
-
|
|
219
|
+
} catch (t) {
|
|
220
|
+
n(t);
|
|
221
|
+
let i = r(t, e);
|
|
222
|
+
return i === void 0 ? pkcs7Failure("malformed", "Malformed PKCS#7 structure") : pkcs7Failure(i.code, i.message);
|
|
217
223
|
}
|
|
218
224
|
}
|
|
219
225
|
function parseSignedDataOptionalFields(e) {
|
|
220
226
|
let t, n;
|
|
221
|
-
for (let r of e) {
|
|
222
|
-
if (
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (n !== void 0) return pkcs7Failure("malformed", "SignedData CRLs field must not repeat");
|
|
230
|
-
n = r;
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
return pkcs7Failure("malformed", "Malformed SignedData optional field");
|
|
234
|
-
}
|
|
227
|
+
for (let r of e) if (r.tag === 160) {
|
|
228
|
+
if (n !== void 0) return pkcs7Failure("malformed", "SignedData certificates field must precede CRLs field");
|
|
229
|
+
if (t !== void 0) return pkcs7Failure("malformed", "SignedData certificates field must not repeat");
|
|
230
|
+
t = r;
|
|
231
|
+
} else if (r.tag === 161) {
|
|
232
|
+
if (n !== void 0) return pkcs7Failure("malformed", "SignedData CRLs field must not repeat");
|
|
233
|
+
n = r;
|
|
234
|
+
} else return pkcs7Failure("malformed", "Malformed SignedData optional field");
|
|
235
235
|
return {
|
|
236
236
|
ok: !0,
|
|
237
237
|
value: {
|
|
@@ -277,6 +277,7 @@ async function verifyPkcs7SignedData(e, t = {}) {
|
|
|
277
277
|
if (!n.ok) return n;
|
|
278
278
|
let r = n.value, i = r.encapsulatedContent ?? t.content?.slice();
|
|
279
279
|
if (i === void 0) return verifyPkcs7Failure("detached_content_required", "SignedData has no encapsulated content; supply the detached content via options.content");
|
|
280
|
+
if (r.signerInfos.length === 0) return verifyPkcs7Failure("no_signers", "SignedData contains no signer information");
|
|
280
281
|
let a = [];
|
|
281
282
|
for (let e of r.signerInfos) {
|
|
282
283
|
let t = await verifyPkcs7SignerInfo(r, e, i);
|
|
@@ -308,32 +309,37 @@ async function verifyPkcs7SignerInfo(e, t, n) {
|
|
|
308
309
|
}
|
|
309
310
|
async function verifyPkcs7BareSignerInfo(e, t, n) {
|
|
310
311
|
try {
|
|
311
|
-
let r = await
|
|
312
|
+
let r = await O(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, n);
|
|
312
313
|
return r.ok ? r.valid ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature does not verify") : r.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
|
|
313
|
-
} catch {
|
|
314
|
-
return
|
|
314
|
+
} catch (e) {
|
|
315
|
+
return signatureVerificationThrowFailure(e);
|
|
315
316
|
}
|
|
316
317
|
}
|
|
317
318
|
/** Shorthand for constructing a PKCS#7 parse failure result. */
|
|
318
|
-
function pkcs7Failure(e,
|
|
319
|
-
return
|
|
319
|
+
function pkcs7Failure(e, n) {
|
|
320
|
+
return t(e, n);
|
|
320
321
|
}
|
|
321
322
|
/** Shorthand for constructing a PKCS#7 verification failure result. */
|
|
322
|
-
|
|
323
|
-
|
|
323
|
+
/** A signer's signature verification that threw: its decode refusal, else `malformed`. */
|
|
324
|
+
function signatureVerificationThrowFailure(t) {
|
|
325
|
+
let n = r(t, e);
|
|
326
|
+
return n === void 0 ? verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData") : verifyPkcs7Failure(n.code, n.message);
|
|
327
|
+
}
|
|
328
|
+
function verifyPkcs7Failure(e, n) {
|
|
329
|
+
return t(e, n);
|
|
324
330
|
}
|
|
325
331
|
/** Shorthand for constructing a PKCS#7 certificate bag creation failure result. */
|
|
326
|
-
function createCertBagFailure(e,
|
|
327
|
-
return
|
|
332
|
+
function createCertBagFailure(e, n) {
|
|
333
|
+
return t(e, n);
|
|
328
334
|
}
|
|
329
335
|
/** Shorthand for constructing a PKCS#7 SignedData creation failure result. */
|
|
330
|
-
function createPkcs7Failure(e,
|
|
331
|
-
return
|
|
336
|
+
function createPkcs7Failure(e, n) {
|
|
337
|
+
return t(e, n);
|
|
332
338
|
}
|
|
333
339
|
/** Converts PEM text to an array of DER certificate blobs, or wraps raw DER. */
|
|
334
340
|
function normalizeCertificateSource(e) {
|
|
335
341
|
if (typeof e == "string") {
|
|
336
|
-
let t =
|
|
342
|
+
let t = E(e).filter((e) => e.label === "CERTIFICATE").map((e) => new Uint8Array(e.bytes));
|
|
337
343
|
if (t.length === 0) throw Error("Certificate PEM required");
|
|
338
344
|
return t;
|
|
339
345
|
}
|
|
@@ -362,15 +368,15 @@ function contentDigestForPrivateKey(e) {
|
|
|
362
368
|
switch (t.namedCurve) {
|
|
363
369
|
case "P-256": return {
|
|
364
370
|
hashName: "SHA-256",
|
|
365
|
-
digestOid:
|
|
371
|
+
digestOid: S.sha256
|
|
366
372
|
};
|
|
367
373
|
case "P-384": return {
|
|
368
374
|
hashName: "SHA-384",
|
|
369
|
-
digestOid:
|
|
375
|
+
digestOid: S.sha384
|
|
370
376
|
};
|
|
371
377
|
case "P-521": return {
|
|
372
378
|
hashName: "SHA-512",
|
|
373
|
-
digestOid:
|
|
379
|
+
digestOid: S.sha512
|
|
374
380
|
};
|
|
375
381
|
default: return;
|
|
376
382
|
}
|
|
@@ -380,22 +386,22 @@ function contentDigestForPrivateKey(e) {
|
|
|
380
386
|
switch (t.hash.name) {
|
|
381
387
|
case "SHA-256": return {
|
|
382
388
|
hashName: "SHA-256",
|
|
383
|
-
digestOid:
|
|
389
|
+
digestOid: S.sha256
|
|
384
390
|
};
|
|
385
391
|
case "SHA-384": return {
|
|
386
392
|
hashName: "SHA-384",
|
|
387
|
-
digestOid:
|
|
393
|
+
digestOid: S.sha384
|
|
388
394
|
};
|
|
389
395
|
case "SHA-512": return {
|
|
390
396
|
hashName: "SHA-512",
|
|
391
|
-
digestOid:
|
|
397
|
+
digestOid: S.sha512
|
|
392
398
|
};
|
|
393
399
|
default: return;
|
|
394
400
|
}
|
|
395
401
|
}
|
|
396
402
|
if (t.name === "Ed25519") return {
|
|
397
403
|
hashName: "SHA-512",
|
|
398
|
-
digestOid:
|
|
404
|
+
digestOid: S.sha512
|
|
399
405
|
};
|
|
400
406
|
}
|
|
401
407
|
/**
|
|
@@ -405,10 +411,10 @@ function contentDigestForPrivateKey(e) {
|
|
|
405
411
|
* SignerInfo. The two differ only in the leading tag byte.
|
|
406
412
|
*/
|
|
407
413
|
function buildSignedAttributes(e, t) {
|
|
408
|
-
let n =
|
|
409
|
-
return
|
|
414
|
+
let n = p([f([s(S.cmsContentType), p([s(e)])]), f([s(S.cmsMessageDigest), p([c(t)])])]), r = new Uint8Array(n);
|
|
415
|
+
return r[0] = 160, {
|
|
410
416
|
setForSigning: n,
|
|
411
|
-
implicitForEmit:
|
|
417
|
+
implicitForEmit: r
|
|
412
418
|
};
|
|
413
419
|
}
|
|
414
420
|
/** The X.509 subset of a decoded CertificateSet. */
|
|
@@ -422,16 +428,16 @@ function x509CertificatesOf(e) {
|
|
|
422
428
|
* value are direct children of the `[3]` element.
|
|
423
429
|
*/
|
|
424
430
|
function parseOtherCertificateFormatOid(e, t) {
|
|
425
|
-
let n =
|
|
431
|
+
let n = y(m(e, t)[0], "otherCertFormat");
|
|
426
432
|
if (n.tag !== 6) throw Error("otherCertFormat must be an OBJECT IDENTIFIER");
|
|
427
|
-
return
|
|
433
|
+
return g(n.value);
|
|
428
434
|
}
|
|
429
435
|
/** Decodes one RFC 5652 §10.2.2 CertificateChoices alternative. */
|
|
430
436
|
function parseCertificateChoice(e, t, n) {
|
|
431
437
|
switch (t.tag) {
|
|
432
438
|
case 48: return {
|
|
433
439
|
type: "certificate",
|
|
434
|
-
certificate:
|
|
440
|
+
certificate: I(n)
|
|
435
441
|
};
|
|
436
442
|
case 160: return {
|
|
437
443
|
type: "extendedCertificate",
|
|
@@ -458,107 +464,101 @@ function parseCertificateChoice(e, t, n) {
|
|
|
458
464
|
* RFC 5652 §10.2.3 `CertificateSet ::= SET OF CertificateChoices`.
|
|
459
465
|
*/
|
|
460
466
|
function parseCertificateSet(e, t) {
|
|
461
|
-
return t === void 0 || t.tag !== 160 ? [] :
|
|
467
|
+
return t === void 0 || t.tag !== 160 ? [] : m(e, t).map((t) => parseCertificateChoice(e, t, e.slice(t.start - t.headerLength, t.end)));
|
|
462
468
|
}
|
|
463
469
|
/** Extracts the list of digest algorithm OIDs from the digestAlgorithms SET. */
|
|
464
470
|
function parseDigestAlgorithms(e, t) {
|
|
465
471
|
let n = [];
|
|
466
472
|
if (t.tag !== 49) throw Error("digestAlgorithms must use SET");
|
|
467
|
-
for (let r of
|
|
473
|
+
for (let r of m(e, t)) {
|
|
468
474
|
if (r.tag !== 48) throw Error("digestAlgorithm must use AlgorithmIdentifier SEQUENCE");
|
|
469
|
-
let t =
|
|
470
|
-
if (
|
|
471
|
-
n.push(
|
|
475
|
+
let t = e.slice(r.start - r.headerLength, r.end), i = d(t), a = i[0];
|
|
476
|
+
if (a === void 0 || i.length < 1 || i.length > 2 || a.tag !== 6) throw Error("Malformed digest AlgorithmIdentifier");
|
|
477
|
+
n.push(g(a.value));
|
|
472
478
|
}
|
|
473
479
|
return n;
|
|
474
480
|
}
|
|
475
481
|
/** Decodes the signerInfos SET OF from a SignedData structure. */
|
|
476
482
|
function parseSignerInfos(e, t) {
|
|
477
|
-
return
|
|
483
|
+
return m(e, t).map((t) => parseSignerInfo(e, t));
|
|
478
484
|
}
|
|
479
485
|
function parseSignerInfo(e, t) {
|
|
480
|
-
let n = e.slice(t.start - t.headerLength, t.end), r =
|
|
481
|
-
|
|
482
|
-
let
|
|
483
|
-
if (i === void 0 || a === void 0 || o === void 0 ||
|
|
484
|
-
let
|
|
486
|
+
let n = e.slice(t.start - t.headerLength, t.end), r = d(n), i = r[0], a = r[1], o = r[2], s = 3, c = r[s]?.tag === 160 ? r[s] : void 0;
|
|
487
|
+
c !== void 0 && (s += 1);
|
|
488
|
+
let l = r[s], u = r[s + 1], f = r[s + 2];
|
|
489
|
+
if (i === void 0 || a === void 0 || o === void 0 || l === void 0 || u === void 0 || u.tag !== 4 || r.length > s + 3 || f !== void 0 && f.tag !== 161) throw Error("Malformed SignerInfo");
|
|
490
|
+
let p = parsePkcs7AlgorithmOid(n, o, "digest algorithm OID"), m = n.slice(l.start - l.headerLength, l.end), _ = d(m), v = g(y(_[0], "signature algorithm OID").value), b = _[1];
|
|
485
491
|
return {
|
|
486
|
-
version:
|
|
492
|
+
version: h(i.value),
|
|
487
493
|
signerIdentifier: parseSignerIdentifier(n.slice(a.start - a.headerLength, a.end)),
|
|
488
|
-
digestAlgorithmOid:
|
|
489
|
-
digestAlgorithmName:
|
|
490
|
-
signatureAlgorithmOid:
|
|
491
|
-
signatureAlgorithmName:
|
|
492
|
-
...
|
|
493
|
-
signatureHex:
|
|
494
|
-
signature: new Uint8Array(
|
|
495
|
-
...
|
|
494
|
+
digestAlgorithmOid: p,
|
|
495
|
+
digestAlgorithmName: M(p),
|
|
496
|
+
signatureAlgorithmOid: v,
|
|
497
|
+
signatureAlgorithmName: N(v, b === void 0 ? void 0 : new Uint8Array(m.slice(b.start - b.headerLength, b.end))),
|
|
498
|
+
...b === void 0 ? {} : { signatureAlgorithmParametersDer: new Uint8Array(m.slice(b.start - b.headerLength, b.end)) },
|
|
499
|
+
signatureHex: x(u.value),
|
|
500
|
+
signature: new Uint8Array(u.value),
|
|
501
|
+
...c === void 0 ? { hasSignedAttrs: !1 } : {
|
|
496
502
|
hasSignedAttrs: !0,
|
|
497
|
-
signedAttrsDer: new Uint8Array(n.slice(
|
|
503
|
+
signedAttrsDer: new Uint8Array(n.slice(c.start - c.headerLength, c.end))
|
|
498
504
|
}
|
|
499
505
|
};
|
|
500
506
|
}
|
|
501
507
|
function parsePkcs7AlgorithmOid(e, t, n) {
|
|
502
|
-
|
|
508
|
+
let r = e.slice(t.start - t.headerLength, t.end);
|
|
509
|
+
return g(y(d(r)[0], n).value);
|
|
503
510
|
}
|
|
504
511
|
/** Unwraps the OCTET STRING inside the IMPLICIT [0] encapsulated content. */
|
|
505
512
|
function extractEncapsulatedContent(e, t) {
|
|
506
513
|
if (t.tag !== 160) throw Error("Unexpected encapsulated content tag");
|
|
507
|
-
let n =
|
|
514
|
+
let n = l(e, t.start);
|
|
508
515
|
if (n.tag !== 4) throw Error("Expected encapsulated OCTET STRING");
|
|
509
516
|
if (n.end !== t.end) throw Error("Encapsulated content must hold exactly one element");
|
|
510
517
|
return n.value;
|
|
511
518
|
}
|
|
512
519
|
/** Extracts issuer Name and serial number from an issuerAndSerialNumber SEQUENCE, or subjectKeyIdentifier from [0] IMPLICIT. */
|
|
513
520
|
function parseSignerIdentifier(e) {
|
|
514
|
-
let t =
|
|
521
|
+
let t = u(e, { maxDepth: 64 });
|
|
515
522
|
if (t.tag === 128) {
|
|
516
523
|
if (t.value.length === 0) throw Error("SignerIdentifier subjectKeyIdentifier must not be empty");
|
|
517
524
|
return {
|
|
518
525
|
type: "subjectKeyIdentifier",
|
|
519
|
-
subjectKeyIdentifier:
|
|
526
|
+
subjectKeyIdentifier: x(t.value)
|
|
520
527
|
};
|
|
521
528
|
}
|
|
522
529
|
if (t.tag === 48) {
|
|
523
|
-
let t =
|
|
530
|
+
let t = d(e), n = t[0], r = t[1];
|
|
524
531
|
if (n === void 0 || r === void 0 || t.length !== 2) throw Error("SignerIdentifier issuerAndSerialNumber is malformed");
|
|
525
532
|
if (n.tag !== 48) throw Error("SignerIdentifier issuer must use Name SEQUENCE");
|
|
526
533
|
if (r.tag !== 2) throw Error("SignerIdentifier serialNumber must use INTEGER");
|
|
527
534
|
return assertImplicitSerialNumberEncoding(r.value, "SignerIdentifier serialNumber"), {
|
|
528
535
|
type: "issuerAndSerialNumber",
|
|
529
536
|
issuer: parseSignerIssuerName(e, n),
|
|
530
|
-
serialNumberHex:
|
|
537
|
+
serialNumberHex: x(r.value)
|
|
531
538
|
};
|
|
532
539
|
}
|
|
533
540
|
throw Error(`Unsupported SignerIdentifier tag: ${String(t.tag)}`);
|
|
534
541
|
}
|
|
535
|
-
const F = new TextDecoder();
|
|
536
542
|
/** Parses a Name SEQUENCE element from a PKCS#7 signer identifier into a {@linkcode ParsedName}. */
|
|
537
543
|
function parseSignerIssuerName(e, t) {
|
|
538
|
-
let n =
|
|
539
|
-
for (let n of
|
|
544
|
+
let n = x(e.slice(t.start - t.headerLength, t.end)), r = [], i = [], a = {};
|
|
545
|
+
for (let n of m(e, t)) {
|
|
540
546
|
let t = [], o = {};
|
|
541
|
-
for (let r of
|
|
542
|
-
let n =
|
|
543
|
-
try {
|
|
544
|
-
d = p(c.tag, c.value);
|
|
545
|
-
} catch {
|
|
546
|
-
d = F.decode(c.value);
|
|
547
|
-
}
|
|
548
|
-
let m = E(l), g = m === void 0 ? {
|
|
547
|
+
for (let r of m(e, n)) {
|
|
548
|
+
let n = m(e, r), s = y(n[0], "signer issuer attribute OID"), c = y(n[1], "signer issuer attribute value"), l = g(s.value), u = _(c.tag, c.value), d = P(l), f = d === void 0 ? {
|
|
549
549
|
oid: l,
|
|
550
550
|
valueTag: c.tag,
|
|
551
|
-
value:
|
|
551
|
+
value: u
|
|
552
552
|
} : {
|
|
553
553
|
oid: l,
|
|
554
|
-
key:
|
|
554
|
+
key: d,
|
|
555
555
|
valueTag: c.tag,
|
|
556
|
-
value:
|
|
556
|
+
value: u
|
|
557
557
|
};
|
|
558
|
-
t.push(
|
|
558
|
+
t.push(f), i.push(f), d !== void 0 && (o[d] === void 0 && (o[d] = u), a[d] === void 0 && (a[d] = u));
|
|
559
559
|
}
|
|
560
560
|
r.push({
|
|
561
|
-
derHex:
|
|
561
|
+
derHex: x(e.slice(n.start - n.headerLength, n.end)),
|
|
562
562
|
attributes: t,
|
|
563
563
|
values: o
|
|
564
564
|
});
|
|
@@ -572,27 +572,27 @@ function parseSignerIssuerName(e, t) {
|
|
|
572
572
|
}
|
|
573
573
|
/** Returns the nth child element inside a constructed ASN.1 element, or throws. */
|
|
574
574
|
function childAt(e, t, n, r) {
|
|
575
|
-
let i = t.start,
|
|
575
|
+
let i = t.start, a = 0;
|
|
576
576
|
for (; i < t.end;) {
|
|
577
|
-
let t =
|
|
578
|
-
if (
|
|
579
|
-
i = t.end,
|
|
577
|
+
let t = l(e, i);
|
|
578
|
+
if (a === n) return t;
|
|
579
|
+
i = t.end, a += 1;
|
|
580
580
|
}
|
|
581
581
|
throw Error(`Missing ${r}`);
|
|
582
582
|
}
|
|
583
583
|
/** Maps a digest algorithm OID to the WebCrypto hash name. */
|
|
584
584
|
function digestAlgorithmHash(e) {
|
|
585
585
|
switch (e) {
|
|
586
|
-
case
|
|
587
|
-
case
|
|
588
|
-
case
|
|
586
|
+
case S.sha256: return "SHA-256";
|
|
587
|
+
case S.sha384: return "SHA-384";
|
|
588
|
+
case S.sha512: return "SHA-512";
|
|
589
589
|
default: throw Error(`Unsupported digest algorithm OID: ${e}`);
|
|
590
590
|
}
|
|
591
591
|
}
|
|
592
592
|
/** Parses required signed attributes and enforces uniqueness + exact-one-value shape. */
|
|
593
593
|
function parseSignedAttributeRequirements(e) {
|
|
594
|
-
let t =
|
|
595
|
-
for (let r of
|
|
594
|
+
let t = l(e), n = {};
|
|
595
|
+
for (let r of m(e, t)) parseSignedAttributeRequirement(e, r, n);
|
|
596
596
|
if (n.messageDigest === void 0) throw Error("Missing messageDigest attribute in signedAttrs");
|
|
597
597
|
if (n.contentTypeOid === void 0) throw Error("Missing contentType attribute in signedAttrs");
|
|
598
598
|
return {
|
|
@@ -601,22 +601,22 @@ function parseSignedAttributeRequirements(e) {
|
|
|
601
601
|
};
|
|
602
602
|
}
|
|
603
603
|
function parseSignedAttributeRequirement(e, t, n) {
|
|
604
|
-
let r = e.slice(t.start - t.headerLength, t.end), i =
|
|
604
|
+
let r = e.slice(t.start - t.headerLength, t.end), i = d(r), a = i[0], o = i[1];
|
|
605
605
|
if (a === void 0 || o === void 0 || i.length !== 2 || o.tag !== 49) throw Error("Malformed signedAttrs attribute");
|
|
606
|
-
let
|
|
607
|
-
|
|
606
|
+
let s = g(a.value), c = m(r, o);
|
|
607
|
+
s === S.cmsMessageDigest && (n.messageDigest = parseUniqueSignedAttributeOctetString(n.messageDigest, c, "messageDigest")), s === S.cmsContentType && (n.contentTypeOid = parseUniqueSignedAttributeOid(n.contentTypeOid, c, "contentType"));
|
|
608
608
|
}
|
|
609
609
|
function parseUniqueSignedAttributeOctetString(e, t, n) {
|
|
610
610
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
611
|
-
let r =
|
|
611
|
+
let r = y(t[0], `${n} attribute value`);
|
|
612
612
|
if (r.tag !== 4) throw Error(`${n} attribute value must use OCTET STRING`);
|
|
613
613
|
return r.value;
|
|
614
614
|
}
|
|
615
615
|
function parseUniqueSignedAttributeOid(e, t, n) {
|
|
616
616
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
617
|
-
let r =
|
|
617
|
+
let r = y(t[0], `${n} attribute value`);
|
|
618
618
|
if (r.tag !== 6) throw Error(`${n} attribute value must use OBJECT IDENTIFIER`);
|
|
619
|
-
return
|
|
619
|
+
return g(r.value);
|
|
620
620
|
}
|
|
621
621
|
/** Replaces the IMPLICIT [0] tag (0xa0) with SET OF (0x31) per RFC 5652 Section 5.4. */
|
|
622
622
|
function retagSignedAttrsAsSet(e) {
|
|
@@ -631,45 +631,47 @@ function constantTimeEqual(e, t) {
|
|
|
631
631
|
return n === 0;
|
|
632
632
|
}
|
|
633
633
|
/** Verifies a signed-attributes flow: digest match + signature over re-tagged attrs. */
|
|
634
|
-
async function verifySignedAttrs(
|
|
635
|
-
if (!
|
|
636
|
-
let
|
|
634
|
+
async function verifySignedAttrs(t, i, a, o) {
|
|
635
|
+
if (!t.hasSignedAttrs) return verifyPkcs7Failure("malformed", "Missing signedAttrs DER");
|
|
636
|
+
let s;
|
|
637
637
|
try {
|
|
638
|
-
assertImplicitSignedAttrsDer(
|
|
639
|
-
} catch {
|
|
640
|
-
|
|
638
|
+
assertImplicitSignedAttrsDer(t.signedAttrsDer), s = parseSignedAttributeRequirements(t.signedAttrsDer);
|
|
639
|
+
} catch (t) {
|
|
640
|
+
n(t);
|
|
641
|
+
let i = r(t, e);
|
|
642
|
+
return i === void 0 ? verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData") : verifyPkcs7Failure(i.code, i.message);
|
|
641
643
|
}
|
|
642
|
-
if (
|
|
643
|
-
let
|
|
644
|
+
if (s.contentTypeOid !== o) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
|
|
645
|
+
let c;
|
|
644
646
|
try {
|
|
645
|
-
let
|
|
646
|
-
|
|
647
|
+
let e = digestAlgorithmHash(t.digestAlgorithmOid);
|
|
648
|
+
c = new Uint8Array(await C().subtle.digest(e, b(a)));
|
|
647
649
|
} catch {
|
|
648
650
|
return verifyPkcs7Failure("malformed", "Unsupported digest algorithm in SignedData");
|
|
649
651
|
}
|
|
650
|
-
if (!constantTimeEqual(
|
|
651
|
-
let
|
|
652
|
+
if (!constantTimeEqual(c, s.messageDigest)) return verifyPkcs7Failure("message_digest_mismatch", "Content digest does not match messageDigest attribute");
|
|
653
|
+
let l;
|
|
652
654
|
try {
|
|
653
|
-
|
|
655
|
+
l = retagSignedAttrsAsSet(t.signedAttrsDer);
|
|
654
656
|
} catch {
|
|
655
657
|
return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
|
|
656
658
|
}
|
|
657
|
-
let
|
|
659
|
+
let u;
|
|
658
660
|
try {
|
|
659
|
-
let
|
|
660
|
-
if (!
|
|
661
|
-
|
|
662
|
-
} catch {
|
|
663
|
-
return
|
|
661
|
+
let e = await O(t.signatureAlgorithmOid, t.signatureAlgorithmParametersDer, i.publicKeyAlgorithmOid, i.publicKeyParametersOid, i.subjectPublicKeyInfoDer, t.signature, l);
|
|
662
|
+
if (!e.ok) return e.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
|
|
663
|
+
u = e.valid;
|
|
664
|
+
} catch (e) {
|
|
665
|
+
return signatureVerificationThrowFailure(e);
|
|
664
666
|
}
|
|
665
|
-
return
|
|
667
|
+
return u ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature over signedAttrs does not verify");
|
|
666
668
|
}
|
|
667
669
|
function signerIdentifierMatches(e, t) {
|
|
668
670
|
let n = t.signerIdentifier;
|
|
669
|
-
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex &&
|
|
671
|
+
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex && F(e.issuer, n.issuer) : e.subjectKeyIdentifier === n.subjectKeyIdentifier;
|
|
670
672
|
}
|
|
671
673
|
function assertImplicitSignedAttrsDer(e) {
|
|
672
|
-
if (
|
|
674
|
+
if (l(e).tag !== 160) throw Error("signedAttrs must use IMPLICIT [0] tag");
|
|
673
675
|
}
|
|
674
676
|
function hasReparseablePkcs7SignedData(e) {
|
|
675
677
|
return "der" in e && e.der instanceof Uint8Array;
|
|
@@ -680,7 +682,6 @@ function assertImplicitSerialNumberEncoding(e, t) {
|
|
|
680
682
|
if (n & 128) throw Error(`${t} must be non-negative`);
|
|
681
683
|
if (e.length > 1 && n === 0 && !((e[1] ?? 0) & 128)) throw Error(`${t} must use minimal encoding`);
|
|
682
684
|
}
|
|
683
|
-
//#endregion
|
|
684
685
|
export { createPkcs7CertBag, createPkcs7SignedData, parsePkcs7CertBagDer, parsePkcs7CertBagPem, parsePkcs7SignedDataDer, parsePkcs7SignedDataPem, verifyPkcs7SignedData };
|
|
685
686
|
|
|
686
687
|
//# sourceMappingURL=pkcs7.js.map
|