micro509 0.13.0 → 0.15.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 +2064 -0
- package/README.md +111 -183
- package/dist/crypto/crypto.d.ts +104 -0
- package/dist/crypto/crypto.js +105 -0
- package/dist/crypto/crypto.js.map +1 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/crypto.js +1 -0
- 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 +18 -14
- package/dist/index.js +19 -17
- package/dist/internal/asn1/asn1.d.ts +3 -6
- package/dist/internal/asn1/asn1.js +355 -44
- 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 +42 -43
- package/dist/internal/asn1/der.js +148 -56
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +90 -54
- 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 +5 -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 +51 -0
- package/dist/internal/crypto/pbes2.js +86 -34
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/rsa-pss.js +48 -44
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +28 -0
- package/dist/internal/crypto/sig-verify.js +111 -62
- 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 +8 -3
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +13 -0
- package/dist/internal/shared/dn.js +101 -48
- 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 +66 -0
- package/dist/internal/shared/ip.js +19 -3
- 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/verify-path.d.ts +4 -0
- package/dist/internal/verify/verify-path.js +264 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +23 -0
- package/dist/internal/x509/directory-name.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +15 -13
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +3 -0
- package/dist/internal/x509/extension-errors.js +19 -0
- package/dist/internal/x509/extension-errors.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +157 -81
- 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 +27 -0
- package/dist/internal/x509/general-name-tags.js.map +1 -0
- package/dist/internal/x509/general-name.js +183 -0
- package/dist/internal/x509/general-name.js.map +1 -0
- package/dist/internal/x509/name-fields.js +21 -12
- 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 +124 -95
- package/dist/keys/keys.js +362 -214
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +3 -2
- package/dist/keys.js +3 -2
- package/dist/pem/pem.d.ts +17 -19
- package/dist/pem/pem.js +122 -41
- 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 +123 -53
- package/dist/pkcs/pkcs7.js +314 -238
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +5 -4
- package/dist/result/result.d.ts +43 -28
- package/dist/result/result.js +27 -14
- 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 +456 -206
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +140 -65
- package/dist/revocation/crl.js +691 -391
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +145 -69
- package/dist/revocation/ocsp.js +347 -297
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +48 -36
- package/dist/revocation/revocation.js +112 -63
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +5 -5
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +18 -20
- package/dist/verify/identity.js +120 -90
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +1 -4
- package/dist/verify/policy.d.ts +15 -8
- package/dist/verify/verify.d.ts +86 -46
- package/dist/verify/verify.js +309 -143
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +3 -2
- package/dist/verify.js +4 -3
- package/dist/x509/certificate.d.ts +50 -28
- package/dist/x509/certificate.js +69 -36
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +9 -8
- package/dist/x509/csr.js +10 -11
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +207 -82
- package/dist/x509/extensions.js +561 -128
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +4 -7
- package/dist/x509/fingerprint.js +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +7 -9
- package/dist/x509/name-text.js +20 -4
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +15 -14
- package/dist/x509/name.js +23 -19
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +165 -64
- package/dist/x509/parse.js +444 -459
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +8 -5
- package/dist/x509.js +10 -8
- package/package.json +90 -65
package/dist/pkcs/pkcs7.js
CHANGED
|
@@ -1,51 +1,61 @@
|
|
|
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 { compareDistinguishedNames as
|
|
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
|
*
|
|
18
|
-
* Creates degenerate (signature-less) certificate bags, parses RFC
|
|
19
|
-
* SignedData
|
|
19
|
+
* Creates degenerate (signature-less) certificate bags, parses RFC 5652
|
|
20
|
+
* SignedData (and the RFC 2315 form whose contentInfo content is an OCTET
|
|
21
|
+
* STRING), and verifies signer signatures including signed-attribute flows.
|
|
22
|
+
* A CertificateSet is decoded as RFC 5652 §10.2.2 CertificateChoices, so an
|
|
23
|
+
* attribute certificate or other-format entry is preserved rather than dropped.
|
|
20
24
|
*
|
|
21
25
|
* @module
|
|
22
26
|
*/
|
|
27
|
+
/** RFC 7468 Section 8 armors an RFC 2315 ContentInfo; Section 9 armors an RFC 5652 one. */
|
|
28
|
+
const L = ["PKCS7", "CMS"];
|
|
23
29
|
/**
|
|
24
30
|
* Creates a degenerate PKCS#7 SignedData structure containing only
|
|
25
31
|
* certificates (no signers), returning DER, PEM, and base64 forms, or a
|
|
26
32
|
* typed `invalid_certificate` failure when a certificate source is not
|
|
27
33
|
* valid PEM/DER.
|
|
28
34
|
*/
|
|
29
|
-
function createPkcs7CertBag(
|
|
30
|
-
let
|
|
35
|
+
function createPkcs7CertBag(t) {
|
|
36
|
+
let n;
|
|
31
37
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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);
|
|
35
43
|
}
|
|
36
|
-
let
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
let a = new Uint8Array(p(n));
|
|
45
|
+
a[0] = 160;
|
|
46
|
+
let c = f([
|
|
47
|
+
o(1),
|
|
48
|
+
p([]),
|
|
49
|
+
f([s(S.pkcs7Data)]),
|
|
50
|
+
a,
|
|
51
|
+
p([])
|
|
52
|
+
]), l = f([s(S.pkcs7SignedData), i(0, c)]);
|
|
43
53
|
return {
|
|
44
54
|
ok: !0,
|
|
45
55
|
value: {
|
|
46
|
-
der:
|
|
47
|
-
pem:
|
|
48
|
-
base64:
|
|
56
|
+
der: l,
|
|
57
|
+
pem: T("PKCS7", l),
|
|
58
|
+
base64: w(l)
|
|
49
59
|
}
|
|
50
60
|
};
|
|
51
61
|
}
|
|
@@ -65,40 +75,42 @@ function createPkcs7CertBag(n) {
|
|
|
65
75
|
*
|
|
66
76
|
* Returns a {@linkcode CreatePkcs7SignedDataResult}: DER, PEM, and base64
|
|
67
77
|
* forms on success, or a typed failure for caller-correctable input (no
|
|
68
|
-
* signers, a signer source that is not exactly one certificate,
|
|
78
|
+
* signers, a signer source that is not exactly one certificate, a signer
|
|
79
|
+
* certificate whose public key cannot verify the signer key's algorithm, or an
|
|
69
80
|
* unsupported signer key).
|
|
70
81
|
*/
|
|
71
|
-
async function createPkcs7SignedData(
|
|
72
|
-
if (
|
|
73
|
-
let n =
|
|
74
|
-
let t =
|
|
75
|
-
|
|
76
|
-
},
|
|
77
|
-
for (let
|
|
78
|
-
let
|
|
79
|
-
if (!
|
|
80
|
-
addCertificate(
|
|
82
|
+
async function createPkcs7SignedData(t) {
|
|
83
|
+
if (t.signers.length === 0) return createPkcs7Failure("no_signers", "createPkcs7SignedData requires at least one signer");
|
|
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 = [];
|
|
88
|
+
for (let e of t.signers) {
|
|
89
|
+
let t = await buildPkcs7SignerInfo(e, a, n);
|
|
90
|
+
if (!t.ok) return t;
|
|
91
|
+
addCertificate(t.certificateDer), d.add(t.digestOid), m.push(t.signerInfoDer);
|
|
81
92
|
}
|
|
82
|
-
for (let
|
|
83
|
-
for (let e of normalizeCertificateSource(
|
|
84
|
-
} catch {
|
|
85
|
-
|
|
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);
|
|
86
98
|
}
|
|
87
|
-
let
|
|
88
|
-
|
|
89
|
-
let
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
]),
|
|
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, _)]);
|
|
96
108
|
return {
|
|
97
109
|
ok: !0,
|
|
98
110
|
value: {
|
|
99
|
-
der:
|
|
100
|
-
pem:
|
|
101
|
-
base64:
|
|
111
|
+
der: v,
|
|
112
|
+
pem: T(h === 1 ? "PKCS7" : "CMS", v),
|
|
113
|
+
base64: w(v)
|
|
102
114
|
}
|
|
103
115
|
};
|
|
104
116
|
}
|
|
@@ -108,47 +120,49 @@ async function createPkcs7SignedData(e) {
|
|
|
108
120
|
* caller-correctable input — malformed PEM/DER, unsupported key — so each is
|
|
109
121
|
* mapped to a typed failure rather than rejecting the public Promise.
|
|
110
122
|
*/
|
|
111
|
-
async function buildPkcs7SignerInfo(
|
|
112
|
-
let
|
|
123
|
+
async function buildPkcs7SignerInfo(t, n, i) {
|
|
124
|
+
let l;
|
|
113
125
|
try {
|
|
114
|
-
|
|
126
|
+
l = normalizeCertificateSource(t.certificate);
|
|
115
127
|
} catch {
|
|
116
128
|
return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate source must be valid PEM or DER");
|
|
117
129
|
}
|
|
118
|
-
let
|
|
119
|
-
if (
|
|
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");
|
|
120
132
|
let d;
|
|
121
133
|
try {
|
|
122
|
-
d =
|
|
123
|
-
} catch {
|
|
124
|
-
|
|
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);
|
|
125
138
|
}
|
|
126
|
-
let
|
|
139
|
+
let p;
|
|
127
140
|
try {
|
|
128
|
-
|
|
141
|
+
p = A(t.privateKey, t.signature);
|
|
129
142
|
} catch {
|
|
130
143
|
return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key or signature profile");
|
|
131
144
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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)))), _;
|
|
135
149
|
try {
|
|
136
|
-
_ = await
|
|
150
|
+
_ = await j(t.privateKey, p, h);
|
|
137
151
|
} catch {
|
|
138
152
|
return createPkcs7Failure("unsupported_signer_key", "Signer key could not produce a PKCS#7 signature");
|
|
139
153
|
}
|
|
140
154
|
return {
|
|
141
155
|
ok: !0,
|
|
142
|
-
signerInfoDer:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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(_)
|
|
149
163
|
]),
|
|
150
|
-
digestOid:
|
|
151
|
-
certificateDer:
|
|
164
|
+
digestOid: m.digestOid,
|
|
165
|
+
certificateDer: u
|
|
152
166
|
};
|
|
153
167
|
}
|
|
154
168
|
/** Parses a DER-encoded PKCS#7 cert bag, returning the contained certificates. */
|
|
@@ -156,65 +170,68 @@ function parsePkcs7CertBagDer(e) {
|
|
|
156
170
|
let t = parsePkcs7SignedDataDer(e);
|
|
157
171
|
return t.ok ? {
|
|
158
172
|
ok: !0,
|
|
159
|
-
value: t.value.
|
|
173
|
+
value: x509CertificatesOf(t.value.certificateChoices)
|
|
160
174
|
} : t;
|
|
161
175
|
}
|
|
162
|
-
/**
|
|
163
|
-
function
|
|
176
|
+
/** The DER of the single `PKCS7` or `CMS` block in `pem`, or a typed failure. */
|
|
177
|
+
function contentInfoPemBytes(e) {
|
|
164
178
|
try {
|
|
165
|
-
let t =
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
179
|
+
let t = E(e).filter((e) => L.some((t) => t === e.label)), n = t[0];
|
|
180
|
+
return n === void 0 || t.length !== 1 ? pkcs7Failure("malformed", "Expected exactly one PKCS7 or CMS PEM block") : {
|
|
181
|
+
ok: !0,
|
|
182
|
+
bytes: n.bytes
|
|
183
|
+
};
|
|
169
184
|
} catch {
|
|
170
|
-
return pkcs7Failure("malformed", "Expected exactly one PKCS7 PEM block");
|
|
185
|
+
return pkcs7Failure("malformed", "Expected exactly one PKCS7 or CMS PEM block");
|
|
171
186
|
}
|
|
172
187
|
}
|
|
188
|
+
/** Parses a PEM-armored PKCS#7/CMS cert bag. Expects exactly one `PKCS7` or `CMS` PEM block. */
|
|
189
|
+
function parsePkcs7CertBagPem(e) {
|
|
190
|
+
let t = contentInfoPemBytes(e);
|
|
191
|
+
return t.ok ? parsePkcs7CertBagDer(t.bytes) : t;
|
|
192
|
+
}
|
|
173
193
|
/** Decodes a DER-encoded PKCS#7 ContentInfo expecting `signedData` content type. */
|
|
174
|
-
function parsePkcs7SignedDataDer(
|
|
194
|
+
function parsePkcs7SignedDataDer(t) {
|
|
175
195
|
try {
|
|
176
|
-
let
|
|
177
|
-
if (n === void 0 ||
|
|
178
|
-
let i =
|
|
179
|
-
if (i !==
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
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");
|
|
200
|
+
if (r === void 0) return pkcs7Failure("malformed", "PKCS#7 signedData content info carries no content");
|
|
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") : {
|
|
185
206
|
ok: !0,
|
|
186
207
|
value: {
|
|
187
|
-
der: new Uint8Array(
|
|
208
|
+
der: new Uint8Array(t),
|
|
188
209
|
contentTypeOid: i,
|
|
189
|
-
version:
|
|
210
|
+
version: h(s.value),
|
|
190
211
|
digestAlgorithmOids: x,
|
|
191
|
-
digestAlgorithmNames: x.map((e) =>
|
|
192
|
-
encapsulatedContentTypeOid:
|
|
193
|
-
...
|
|
194
|
-
|
|
195
|
-
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)
|
|
196
217
|
}
|
|
197
218
|
};
|
|
198
|
-
} catch {
|
|
199
|
-
|
|
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);
|
|
200
223
|
}
|
|
201
224
|
}
|
|
202
225
|
function parseSignedDataOptionalFields(e) {
|
|
203
226
|
let t, n;
|
|
204
|
-
for (let r of e) {
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (n !== void 0) return pkcs7Failure("malformed", "SignedData CRLs field must not repeat");
|
|
213
|
-
n = r;
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
return pkcs7Failure("malformed", "Malformed SignedData optional field");
|
|
217
|
-
}
|
|
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");
|
|
218
235
|
return {
|
|
219
236
|
ok: !0,
|
|
220
237
|
value: {
|
|
@@ -223,14 +240,10 @@ function parseSignedDataOptionalFields(e) {
|
|
|
223
240
|
}
|
|
224
241
|
};
|
|
225
242
|
}
|
|
226
|
-
/** Decodes a PEM-armored PKCS#7 SignedData. Expects exactly one `PKCS7` PEM block. */
|
|
243
|
+
/** Decodes a PEM-armored PKCS#7/CMS SignedData. Expects exactly one `PKCS7` or `CMS` PEM block. */
|
|
227
244
|
function parsePkcs7SignedDataPem(e) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return n === void 0 || t.length !== 1 ? pkcs7Failure("malformed", "Expected exactly one PKCS7 PEM block") : parsePkcs7SignedDataDer(n.bytes);
|
|
231
|
-
} catch {
|
|
232
|
-
return pkcs7Failure("malformed", "Expected exactly one PKCS7 PEM block");
|
|
233
|
-
}
|
|
245
|
+
let t = contentInfoPemBytes(e);
|
|
246
|
+
return t.ok ? parsePkcs7SignedDataDer(t.bytes) : t;
|
|
234
247
|
}
|
|
235
248
|
/**
|
|
236
249
|
* Verifies all signer signatures in a PKCS#7 SignedData structure.
|
|
@@ -250,7 +263,9 @@ function parsePkcs7SignedDataPem(e) {
|
|
|
250
263
|
*
|
|
251
264
|
* const result = await verifyPkcs7SignedData(pkcs7Pem);
|
|
252
265
|
* if (result.ok) {
|
|
253
|
-
*
|
|
266
|
+
* for (const { signerInfo, certificate } of result.signers) {
|
|
267
|
+
* console.log(signerInfo.digestAlgorithmOid, certificate.subject);
|
|
268
|
+
* }
|
|
254
269
|
* }
|
|
255
270
|
*
|
|
256
271
|
* // Detached signature: supply the content externally
|
|
@@ -262,13 +277,20 @@ async function verifyPkcs7SignedData(e, t = {}) {
|
|
|
262
277
|
if (!n.ok) return n;
|
|
263
278
|
let r = n.value, i = r.encapsulatedContent ?? t.content?.slice();
|
|
264
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");
|
|
281
|
+
let a = [];
|
|
265
282
|
for (let e of r.signerInfos) {
|
|
266
283
|
let t = await verifyPkcs7SignerInfo(r, e, i);
|
|
267
284
|
if (!t.ok) return t;
|
|
285
|
+
a.push({
|
|
286
|
+
signerInfo: e,
|
|
287
|
+
certificate: t.certificate
|
|
288
|
+
});
|
|
268
289
|
}
|
|
269
290
|
return {
|
|
270
291
|
ok: !0,
|
|
271
|
-
value: r
|
|
292
|
+
value: r,
|
|
293
|
+
signers: a
|
|
272
294
|
};
|
|
273
295
|
}
|
|
274
296
|
/** Normalizes encoded or parsed SignedData into a freshly parsed verification input. */
|
|
@@ -277,36 +299,51 @@ function normalizePkcs7SignedDataForVerification(e) {
|
|
|
277
299
|
}
|
|
278
300
|
/** Resolves and verifies one signer entry in a parsed PKCS#7 SignedData value. */
|
|
279
301
|
async function verifyPkcs7SignerInfo(e, t, n) {
|
|
280
|
-
let r = e.
|
|
281
|
-
|
|
302
|
+
let r = x509CertificatesOf(e.certificateChoices).find((e) => signerIdentifierMatches(e, t));
|
|
303
|
+
if (r === void 0) return verifyPkcs7Failure("signer_not_found", "Signer certificate not found in SignedData certificates");
|
|
304
|
+
let i = t.hasSignedAttrs ? await verifySignedAttrs(t, r, n, e.encapsulatedContentTypeOid) : await verifyPkcs7BareSignerInfo(t, r, n);
|
|
305
|
+
return i.ok ? {
|
|
306
|
+
ok: !0,
|
|
307
|
+
certificate: r
|
|
308
|
+
} : i;
|
|
282
309
|
}
|
|
283
310
|
async function verifyPkcs7BareSignerInfo(e, t, n) {
|
|
284
311
|
try {
|
|
285
|
-
let r = await
|
|
312
|
+
let r = await O(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, n);
|
|
286
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");
|
|
287
|
-
} catch {
|
|
288
|
-
return
|
|
314
|
+
} catch (e) {
|
|
315
|
+
return signatureVerificationThrowFailure(e);
|
|
289
316
|
}
|
|
290
317
|
}
|
|
291
318
|
/** Shorthand for constructing a PKCS#7 parse failure result. */
|
|
292
|
-
function pkcs7Failure(e,
|
|
293
|
-
return
|
|
319
|
+
function pkcs7Failure(e, n) {
|
|
320
|
+
return t(e, n);
|
|
294
321
|
}
|
|
295
322
|
/** Shorthand for constructing a PKCS#7 verification failure result. */
|
|
296
|
-
|
|
297
|
-
|
|
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);
|
|
298
330
|
}
|
|
299
331
|
/** Shorthand for constructing a PKCS#7 certificate bag creation failure result. */
|
|
300
|
-
function createCertBagFailure(e,
|
|
301
|
-
return
|
|
332
|
+
function createCertBagFailure(e, n) {
|
|
333
|
+
return t(e, n);
|
|
302
334
|
}
|
|
303
335
|
/** Shorthand for constructing a PKCS#7 SignedData creation failure result. */
|
|
304
|
-
function createPkcs7Failure(e,
|
|
305
|
-
return
|
|
336
|
+
function createPkcs7Failure(e, n) {
|
|
337
|
+
return t(e, n);
|
|
306
338
|
}
|
|
307
339
|
/** Converts PEM text to an array of DER certificate blobs, or wraps raw DER. */
|
|
308
340
|
function normalizeCertificateSource(e) {
|
|
309
|
-
|
|
341
|
+
if (typeof e == "string") {
|
|
342
|
+
let t = E(e).filter((e) => e.label === "CERTIFICATE").map((e) => new Uint8Array(e.bytes));
|
|
343
|
+
if (t.length === 0) throw Error("Certificate PEM required");
|
|
344
|
+
return t;
|
|
345
|
+
}
|
|
346
|
+
return e instanceof Uint8Array ? [new Uint8Array(e)] : [new Uint8Array(e.der)];
|
|
310
347
|
}
|
|
311
348
|
/** Type guard: key algorithm carries an RSA `hash`. */
|
|
312
349
|
function signerHasHash(e) {
|
|
@@ -319,9 +356,10 @@ function signerHasNamedCurve(e) {
|
|
|
319
356
|
/**
|
|
320
357
|
* Resolves the content-digest hash and its OID for a signer key.
|
|
321
358
|
*
|
|
322
|
-
*
|
|
323
|
-
* P-
|
|
324
|
-
*
|
|
359
|
+
* ECDSA curve pairing follows RFC 5753 §8: P-256 → SHA-256, P-384 → SHA-384,
|
|
360
|
+
* P-521 → SHA-512. RSA uses the hash bound to the key.
|
|
361
|
+
* Digest OIDs are from RFC 5754 §2.
|
|
362
|
+
* Ed25519 uses SHA-512 for the messageDigest attribute, per RFC 8419.
|
|
325
363
|
*/
|
|
326
364
|
function contentDigestForPrivateKey(e) {
|
|
327
365
|
let t = e.algorithm;
|
|
@@ -330,15 +368,15 @@ function contentDigestForPrivateKey(e) {
|
|
|
330
368
|
switch (t.namedCurve) {
|
|
331
369
|
case "P-256": return {
|
|
332
370
|
hashName: "SHA-256",
|
|
333
|
-
digestOid:
|
|
371
|
+
digestOid: S.sha256
|
|
334
372
|
};
|
|
335
373
|
case "P-384": return {
|
|
336
374
|
hashName: "SHA-384",
|
|
337
|
-
digestOid:
|
|
375
|
+
digestOid: S.sha384
|
|
338
376
|
};
|
|
339
377
|
case "P-521": return {
|
|
340
378
|
hashName: "SHA-512",
|
|
341
|
-
digestOid:
|
|
379
|
+
digestOid: S.sha512
|
|
342
380
|
};
|
|
343
381
|
default: return;
|
|
344
382
|
}
|
|
@@ -348,22 +386,22 @@ function contentDigestForPrivateKey(e) {
|
|
|
348
386
|
switch (t.hash.name) {
|
|
349
387
|
case "SHA-256": return {
|
|
350
388
|
hashName: "SHA-256",
|
|
351
|
-
digestOid:
|
|
389
|
+
digestOid: S.sha256
|
|
352
390
|
};
|
|
353
391
|
case "SHA-384": return {
|
|
354
392
|
hashName: "SHA-384",
|
|
355
|
-
digestOid:
|
|
393
|
+
digestOid: S.sha384
|
|
356
394
|
};
|
|
357
395
|
case "SHA-512": return {
|
|
358
396
|
hashName: "SHA-512",
|
|
359
|
-
digestOid:
|
|
397
|
+
digestOid: S.sha512
|
|
360
398
|
};
|
|
361
399
|
default: return;
|
|
362
400
|
}
|
|
363
401
|
}
|
|
364
402
|
if (t.name === "Ed25519") return {
|
|
365
403
|
hashName: "SHA-512",
|
|
366
|
-
digestOid:
|
|
404
|
+
digestOid: S.sha512
|
|
367
405
|
};
|
|
368
406
|
}
|
|
369
407
|
/**
|
|
@@ -373,56 +411,93 @@ function contentDigestForPrivateKey(e) {
|
|
|
373
411
|
* SignerInfo. The two differ only in the leading tag byte.
|
|
374
412
|
*/
|
|
375
413
|
function buildSignedAttributes(e, t) {
|
|
376
|
-
let n =
|
|
414
|
+
let n = p([f([s(S.cmsContentType), p([s(e)])]), f([s(S.cmsMessageDigest), p([c(t)])])]), r = new Uint8Array(n);
|
|
377
415
|
return r[0] = 160, {
|
|
378
416
|
setForSigning: n,
|
|
379
417
|
implicitForEmit: r
|
|
380
418
|
};
|
|
381
419
|
}
|
|
382
|
-
/**
|
|
383
|
-
function
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
420
|
+
/** The X.509 subset of a decoded CertificateSet. */
|
|
421
|
+
function x509CertificatesOf(e) {
|
|
422
|
+
return e.flatMap((e) => e.type === "certificate" ? [e.certificate] : []);
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* `otherCertFormat` from an `other [3] IMPLICIT OtherCertificateFormat` entry.
|
|
426
|
+
*
|
|
427
|
+
* The IMPLICIT tag replaces the SEQUENCE tag, so the OID and the `ANY DEFINED BY`
|
|
428
|
+
* value are direct children of the `[3]` element.
|
|
429
|
+
*/
|
|
430
|
+
function parseOtherCertificateFormatOid(e, t) {
|
|
431
|
+
let n = y(m(e, t)[0], "otherCertFormat");
|
|
432
|
+
if (n.tag !== 6) throw Error("otherCertFormat must be an OBJECT IDENTIFIER");
|
|
433
|
+
return g(n.value);
|
|
434
|
+
}
|
|
435
|
+
/** Decodes one RFC 5652 §10.2.2 CertificateChoices alternative. */
|
|
436
|
+
function parseCertificateChoice(e, t, n) {
|
|
437
|
+
switch (t.tag) {
|
|
438
|
+
case 48: return {
|
|
439
|
+
type: "certificate",
|
|
440
|
+
certificate: I(n)
|
|
441
|
+
};
|
|
442
|
+
case 160: return {
|
|
443
|
+
type: "extendedCertificate",
|
|
444
|
+
der: n
|
|
445
|
+
};
|
|
446
|
+
case 161: return {
|
|
447
|
+
type: "attributeCertificateV1",
|
|
448
|
+
der: n
|
|
449
|
+
};
|
|
450
|
+
case 162: return {
|
|
451
|
+
type: "attributeCertificateV2",
|
|
452
|
+
der: n
|
|
453
|
+
};
|
|
454
|
+
case 163: return {
|
|
455
|
+
type: "other",
|
|
456
|
+
formatOid: parseOtherCertificateFormatOid(e, t),
|
|
457
|
+
der: n
|
|
458
|
+
};
|
|
459
|
+
default: throw Error(`Unsupported CertificateChoices tag: ${t.tag}`);
|
|
389
460
|
}
|
|
390
|
-
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Parses the IMPLICIT [0] certificate set from a SignedData structure as
|
|
464
|
+
* RFC 5652 §10.2.3 `CertificateSet ::= SET OF CertificateChoices`.
|
|
465
|
+
*/
|
|
466
|
+
function parseCertificateSet(e, t) {
|
|
467
|
+
return t === void 0 || t.tag !== 160 ? [] : m(e, t).map((t) => parseCertificateChoice(e, t, e.slice(t.start - t.headerLength, t.end)));
|
|
391
468
|
}
|
|
392
469
|
/** Extracts the list of digest algorithm OIDs from the digestAlgorithms SET. */
|
|
393
470
|
function parseDigestAlgorithms(e, t) {
|
|
394
471
|
let n = [];
|
|
395
472
|
if (t.tag !== 49) throw Error("digestAlgorithms must use SET");
|
|
396
|
-
for (let r of
|
|
473
|
+
for (let r of m(e, t)) {
|
|
397
474
|
if (r.tag !== 48) throw Error("digestAlgorithm must use AlgorithmIdentifier SEQUENCE");
|
|
398
|
-
let t =
|
|
399
|
-
if (
|
|
400
|
-
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));
|
|
401
478
|
}
|
|
402
479
|
return n;
|
|
403
480
|
}
|
|
404
481
|
/** Decodes the signerInfos SET OF from a SignedData structure. */
|
|
405
482
|
function parseSignerInfos(e, t) {
|
|
406
|
-
return
|
|
483
|
+
return m(e, t).map((t) => parseSignerInfo(e, t));
|
|
407
484
|
}
|
|
408
485
|
function parseSignerInfo(e, t) {
|
|
409
|
-
let n = e.slice(t.start - t.headerLength, t.end), r =
|
|
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;
|
|
410
487
|
c !== void 0 && (s += 1);
|
|
411
|
-
let
|
|
412
|
-
if (i === void 0 || a === void 0 || o === void 0 ||
|
|
413
|
-
let
|
|
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];
|
|
414
491
|
return {
|
|
415
|
-
version:
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
signatureHex: y(d.value),
|
|
425
|
-
signature: new Uint8Array(d.value),
|
|
492
|
+
version: h(i.value),
|
|
493
|
+
signerIdentifier: parseSignerIdentifier(n.slice(a.start - a.headerLength, a.end)),
|
|
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),
|
|
426
501
|
...c === void 0 ? { hasSignedAttrs: !1 } : {
|
|
427
502
|
hasSignedAttrs: !0,
|
|
428
503
|
signedAttrsDer: new Uint8Array(n.slice(c.start - c.headerLength, c.end))
|
|
@@ -430,48 +505,47 @@ function parseSignerInfo(e, t) {
|
|
|
430
505
|
};
|
|
431
506
|
}
|
|
432
507
|
function parsePkcs7AlgorithmOid(e, t, n) {
|
|
433
|
-
|
|
508
|
+
let r = e.slice(t.start - t.headerLength, t.end);
|
|
509
|
+
return g(y(d(r)[0], n).value);
|
|
434
510
|
}
|
|
435
511
|
/** Unwraps the OCTET STRING inside the IMPLICIT [0] encapsulated content. */
|
|
436
512
|
function extractEncapsulatedContent(e, t) {
|
|
437
513
|
if (t.tag !== 160) throw Error("Unexpected encapsulated content tag");
|
|
438
|
-
let n =
|
|
514
|
+
let n = l(e, t.start);
|
|
439
515
|
if (n.tag !== 4) throw Error("Expected encapsulated OCTET STRING");
|
|
516
|
+
if (n.end !== t.end) throw Error("Encapsulated content must hold exactly one element");
|
|
440
517
|
return n.value;
|
|
441
518
|
}
|
|
442
519
|
/** Extracts issuer Name and serial number from an issuerAndSerialNumber SEQUENCE, or subjectKeyIdentifier from [0] IMPLICIT. */
|
|
443
520
|
function parseSignerIdentifier(e) {
|
|
444
|
-
let t =
|
|
521
|
+
let t = u(e, { maxDepth: 64 });
|
|
445
522
|
if (t.tag === 128) {
|
|
446
523
|
if (t.value.length === 0) throw Error("SignerIdentifier subjectKeyIdentifier must not be empty");
|
|
447
|
-
return {
|
|
524
|
+
return {
|
|
525
|
+
type: "subjectKeyIdentifier",
|
|
526
|
+
subjectKeyIdentifier: x(t.value)
|
|
527
|
+
};
|
|
448
528
|
}
|
|
449
529
|
if (t.tag === 48) {
|
|
450
|
-
let t =
|
|
530
|
+
let t = d(e), n = t[0], r = t[1];
|
|
451
531
|
if (n === void 0 || r === void 0 || t.length !== 2) throw Error("SignerIdentifier issuerAndSerialNumber is malformed");
|
|
452
532
|
if (n.tag !== 48) throw Error("SignerIdentifier issuer must use Name SEQUENCE");
|
|
453
533
|
if (r.tag !== 2) throw Error("SignerIdentifier serialNumber must use INTEGER");
|
|
454
534
|
return assertImplicitSerialNumberEncoding(r.value, "SignerIdentifier serialNumber"), {
|
|
535
|
+
type: "issuerAndSerialNumber",
|
|
455
536
|
issuer: parseSignerIssuerName(e, n),
|
|
456
|
-
serialNumberHex:
|
|
537
|
+
serialNumberHex: x(r.value)
|
|
457
538
|
};
|
|
458
539
|
}
|
|
459
540
|
throw Error(`Unsupported SignerIdentifier tag: ${String(t.tag)}`);
|
|
460
541
|
}
|
|
461
|
-
const F = new TextDecoder();
|
|
462
542
|
/** Parses a Name SEQUENCE element from a PKCS#7 signer identifier into a {@linkcode ParsedName}. */
|
|
463
543
|
function parseSignerIssuerName(e, t) {
|
|
464
|
-
let n =
|
|
465
|
-
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)) {
|
|
466
546
|
let t = [], o = {};
|
|
467
|
-
for (let r of
|
|
468
|
-
let n =
|
|
469
|
-
try {
|
|
470
|
-
u = h(c.tag, c.value);
|
|
471
|
-
} catch {
|
|
472
|
-
u = F.decode(c.value);
|
|
473
|
-
}
|
|
474
|
-
let d = O(l), p = d === 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 ? {
|
|
475
549
|
oid: l,
|
|
476
550
|
valueTag: c.tag,
|
|
477
551
|
value: u
|
|
@@ -481,10 +555,10 @@ function parseSignerIssuerName(e, t) {
|
|
|
481
555
|
valueTag: c.tag,
|
|
482
556
|
value: u
|
|
483
557
|
};
|
|
484
|
-
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));
|
|
485
559
|
}
|
|
486
560
|
r.push({
|
|
487
|
-
derHex:
|
|
561
|
+
derHex: x(e.slice(n.start - n.headerLength, n.end)),
|
|
488
562
|
attributes: t,
|
|
489
563
|
values: o
|
|
490
564
|
});
|
|
@@ -500,7 +574,7 @@ function parseSignerIssuerName(e, t) {
|
|
|
500
574
|
function childAt(e, t, n, r) {
|
|
501
575
|
let i = t.start, a = 0;
|
|
502
576
|
for (; i < t.end;) {
|
|
503
|
-
let t =
|
|
577
|
+
let t = l(e, i);
|
|
504
578
|
if (a === n) return t;
|
|
505
579
|
i = t.end, a += 1;
|
|
506
580
|
}
|
|
@@ -509,16 +583,16 @@ function childAt(e, t, n, r) {
|
|
|
509
583
|
/** Maps a digest algorithm OID to the WebCrypto hash name. */
|
|
510
584
|
function digestAlgorithmHash(e) {
|
|
511
585
|
switch (e) {
|
|
512
|
-
case
|
|
513
|
-
case
|
|
514
|
-
case
|
|
586
|
+
case S.sha256: return "SHA-256";
|
|
587
|
+
case S.sha384: return "SHA-384";
|
|
588
|
+
case S.sha512: return "SHA-512";
|
|
515
589
|
default: throw Error(`Unsupported digest algorithm OID: ${e}`);
|
|
516
590
|
}
|
|
517
591
|
}
|
|
518
592
|
/** Parses required signed attributes and enforces uniqueness + exact-one-value shape. */
|
|
519
593
|
function parseSignedAttributeRequirements(e) {
|
|
520
|
-
let t =
|
|
521
|
-
for (let r of
|
|
594
|
+
let t = l(e), n = {};
|
|
595
|
+
for (let r of m(e, t)) parseSignedAttributeRequirement(e, r, n);
|
|
522
596
|
if (n.messageDigest === void 0) throw Error("Missing messageDigest attribute in signedAttrs");
|
|
523
597
|
if (n.contentTypeOid === void 0) throw Error("Missing contentType attribute in signedAttrs");
|
|
524
598
|
return {
|
|
@@ -527,22 +601,22 @@ function parseSignedAttributeRequirements(e) {
|
|
|
527
601
|
};
|
|
528
602
|
}
|
|
529
603
|
function parseSignedAttributeRequirement(e, t, n) {
|
|
530
|
-
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];
|
|
531
605
|
if (a === void 0 || o === void 0 || i.length !== 2 || o.tag !== 49) throw Error("Malformed signedAttrs attribute");
|
|
532
|
-
let s =
|
|
533
|
-
s ===
|
|
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"));
|
|
534
608
|
}
|
|
535
609
|
function parseUniqueSignedAttributeOctetString(e, t, n) {
|
|
536
610
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
537
|
-
let r =
|
|
611
|
+
let r = y(t[0], `${n} attribute value`);
|
|
538
612
|
if (r.tag !== 4) throw Error(`${n} attribute value must use OCTET STRING`);
|
|
539
613
|
return r.value;
|
|
540
614
|
}
|
|
541
615
|
function parseUniqueSignedAttributeOid(e, t, n) {
|
|
542
616
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
543
|
-
let r =
|
|
617
|
+
let r = y(t[0], `${n} attribute value`);
|
|
544
618
|
if (r.tag !== 6) throw Error(`${n} attribute value must use OBJECT IDENTIFIER`);
|
|
545
|
-
return
|
|
619
|
+
return g(r.value);
|
|
546
620
|
}
|
|
547
621
|
/** Replaces the IMPLICIT [0] tag (0xa0) with SET OF (0x31) per RFC 5652 Section 5.4. */
|
|
548
622
|
function retagSignedAttrsAsSet(e) {
|
|
@@ -557,44 +631,47 @@ function constantTimeEqual(e, t) {
|
|
|
557
631
|
return n === 0;
|
|
558
632
|
}
|
|
559
633
|
/** Verifies a signed-attributes flow: digest match + signature over re-tagged attrs. */
|
|
560
|
-
async function verifySignedAttrs(
|
|
561
|
-
if (!
|
|
562
|
-
let
|
|
634
|
+
async function verifySignedAttrs(t, i, a, o) {
|
|
635
|
+
if (!t.hasSignedAttrs) return verifyPkcs7Failure("malformed", "Missing signedAttrs DER");
|
|
636
|
+
let s;
|
|
563
637
|
try {
|
|
564
|
-
assertImplicitSignedAttrsDer(
|
|
565
|
-
} catch {
|
|
566
|
-
|
|
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);
|
|
567
643
|
}
|
|
568
|
-
if (
|
|
569
|
-
let
|
|
644
|
+
if (s.contentTypeOid !== o) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
|
|
645
|
+
let c;
|
|
570
646
|
try {
|
|
571
|
-
let
|
|
572
|
-
|
|
647
|
+
let e = digestAlgorithmHash(t.digestAlgorithmOid);
|
|
648
|
+
c = new Uint8Array(await C().subtle.digest(e, b(a)));
|
|
573
649
|
} catch {
|
|
574
650
|
return verifyPkcs7Failure("malformed", "Unsupported digest algorithm in SignedData");
|
|
575
651
|
}
|
|
576
|
-
if (!constantTimeEqual(
|
|
577
|
-
let
|
|
652
|
+
if (!constantTimeEqual(c, s.messageDigest)) return verifyPkcs7Failure("message_digest_mismatch", "Content digest does not match messageDigest attribute");
|
|
653
|
+
let l;
|
|
578
654
|
try {
|
|
579
|
-
|
|
655
|
+
l = retagSignedAttrsAsSet(t.signedAttrsDer);
|
|
580
656
|
} catch {
|
|
581
657
|
return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
|
|
582
658
|
}
|
|
583
|
-
let
|
|
659
|
+
let u;
|
|
584
660
|
try {
|
|
585
|
-
let
|
|
586
|
-
if (!
|
|
587
|
-
|
|
588
|
-
} catch {
|
|
589
|
-
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);
|
|
590
666
|
}
|
|
591
|
-
return
|
|
667
|
+
return u ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature over signedAttrs does not verify");
|
|
592
668
|
}
|
|
593
669
|
function signerIdentifierMatches(e, t) {
|
|
594
|
-
|
|
670
|
+
let n = t.signerIdentifier;
|
|
671
|
+
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex && F(e.issuer, n.issuer) : e.subjectKeyIdentifier === n.subjectKeyIdentifier;
|
|
595
672
|
}
|
|
596
673
|
function assertImplicitSignedAttrsDer(e) {
|
|
597
|
-
if (
|
|
674
|
+
if (l(e).tag !== 160) throw Error("signedAttrs must use IMPLICIT [0] tag");
|
|
598
675
|
}
|
|
599
676
|
function hasReparseablePkcs7SignedData(e) {
|
|
600
677
|
return "der" in e && e.der instanceof Uint8Array;
|
|
@@ -605,7 +682,6 @@ function assertImplicitSerialNumberEncoding(e, t) {
|
|
|
605
682
|
if (n & 128) throw Error(`${t} must be non-negative`);
|
|
606
683
|
if (e.length > 1 && n === 0 && !((e[1] ?? 0) & 128)) throw Error(`${t} must use minimal encoding`);
|
|
607
684
|
}
|
|
608
|
-
//#endregion
|
|
609
685
|
export { createPkcs7CertBag, createPkcs7SignedData, parsePkcs7CertBagDer, parsePkcs7CertBagPem, parsePkcs7SignedDataDer, parsePkcs7SignedDataPem, verifyPkcs7SignedData };
|
|
610
686
|
|
|
611
687
|
//# sourceMappingURL=pkcs7.js.map
|