micro509 0.17.1 → 0.19.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 +165 -6
- package/dist/crypto/crypto.d.ts +12 -4
- package/dist/crypto/crypto.js +30 -17
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/crypto.d.ts +8 -2
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der/der.js.map +1 -1
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +84 -16
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/asn1.js +10 -2
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js.map +1 -1
- package/dist/internal/asn1/decode-refusal.js.map +1 -1
- package/dist/internal/asn1/der.d.ts +1 -2
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +71 -353
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids.json.js +357 -0
- package/dist/internal/asn1/oids.json.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +1 -1
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/prime-ec.js +37 -0
- package/dist/internal/crypto/prime-ec.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +50 -50
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +41 -2
- package/dist/internal/crypto/sig-verify.js +125 -38
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/jose/base64url.js.map +1 -1
- package/dist/internal/jose/json-data.js +108 -0
- package/dist/internal/jose/json-data.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +1 -2
- package/dist/internal/jose/strict-json.js.map +1 -1
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna.js.map +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js.map +1 -1
- package/dist/internal/shared/punycode.js.map +1 -1
- package/dist/internal/shared/uri-host.js.map +1 -1
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/spki-distrust.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +234 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/directory-name.js.map +1 -1
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-errors.js.map +1 -1
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name-profile.js.map +1 -1
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/general-name.js.map +1 -1
- package/dist/internal/x509/name-fields.js +1 -1
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js.map +1 -1
- package/dist/jose/base64url.d.ts +4 -0
- package/dist/jose/base64url.js +14 -0
- package/dist/jose/base64url.js.map +1 -0
- package/dist/jose/errors.d.ts +9 -3
- package/dist/jose/errors.js.map +1 -1
- package/dist/jose/jwe.d.ts +64 -0
- package/dist/jose/jwe.js +188 -0
- package/dist/jose/jwe.js.map +1 -0
- package/dist/jose/jwks.d.ts +38 -10
- package/dist/jose/jwks.js +156 -96
- package/dist/jose/jwks.js.map +1 -1
- package/dist/jose/jws.d.ts +8 -6
- package/dist/jose/jws.js +216 -170
- package/dist/jose/jws.js.map +1 -1
- package/dist/jose/jwt.d.ts +8 -2
- package/dist/jose/jwt.js +98 -98
- package/dist/jose/jwt.js.map +1 -1
- package/dist/jose/remote-jwks.d.ts +1 -2
- package/dist/jose/remote-jwks.js +106 -108
- package/dist/jose/remote-jwks.js.map +1 -1
- package/dist/jose.d.ts +15 -4
- package/dist/jose.js +7 -5
- package/dist/kdf/argon2-arithmetic.js.map +1 -1
- package/dist/kdf/argon2-core.js.map +1 -1
- package/dist/kdf/argon2-memory.js.map +1 -1
- package/dist/kdf/argon2-portable.js.map +1 -1
- package/dist/kdf/argon2.d.ts +7 -2
- package/dist/kdf/argon2.js.map +1 -1
- package/dist/kdf.d.ts +6 -0
- package/dist/keys/keys.d.ts +31 -6
- package/dist/keys/keys.js +315 -233
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +11 -2
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +1 -2
- package/dist/pem/pem.js +40 -25
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.d.ts +6 -0
- package/dist/pkcs/pfx.d.ts +2 -3
- package/dist/pkcs/pfx.js +142 -142
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +24 -3
- package/dist/pkcs/pkcs7.js +294 -256
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +10 -1
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +6 -0
- package/dist/revocation/chain.d.ts +69 -16
- package/dist/revocation/chain.js +431 -303
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +67 -14
- package/dist/revocation/crl.js +656 -366
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +78 -24
- package/dist/revocation/ocsp.js +622 -436
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +68 -14
- package/dist/revocation/revocation.js +195 -74
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +14 -5
- package/dist/revocation.js +4 -4
- package/dist/ssh/ssh.d.ts +2 -3
- package/dist/ssh/ssh.js +165 -170
- package/dist/ssh/ssh.js.map +1 -1
- package/dist/ssh/wire.d.ts +1 -2
- package/dist/ssh/wire.js +10 -10
- package/dist/ssh/wire.js.map +1 -1
- package/dist/ssh.d.ts +8 -0
- package/dist/verify/identity.d.ts +3 -4
- package/dist/verify/identity.js +33 -33
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +85 -34
- package/dist/verify/verify.js +276 -226
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +12 -2
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +165 -165
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +1 -2
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +9 -6
- package/dist/x509/parse.js +394 -394
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +32 -33
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/pkcs/pkcs7.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { decodeRefusalOf as
|
|
3
|
-
import { explicitContext as
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { parseCertificateDerOrThrow as
|
|
1
|
+
import { childrenOf as e, decodeIntegerNumber as t, decodeObjectIdentifier as n, decodeString as r, hexToBytes as i, requireElement as a, toArrayBuffer as o, toHex as s } from "../internal/asn1/asn1.js";
|
|
2
|
+
import { decodeRefusalOf as c } from "../internal/asn1/decode-refusal.js";
|
|
3
|
+
import { explicitContext as l, integer as u, integerFromNumber as d, objectIdentifier as f, octetString as p, readElement as m, readRootElement as h, readSequenceChildren as g, sequence as _, setOf as v } from "../internal/asn1/der.js";
|
|
4
|
+
import { OIDS as y } from "../internal/asn1/oids.js";
|
|
5
|
+
import { describeHashAlgorithm as b, describeSignatureAlgorithm as x } from "../internal/crypto/algorithm-names.js";
|
|
6
|
+
import { callerVerification as S, getVerifySignatureConfigResult as C, verifySignedDataDetailed as w } from "../internal/crypto/sig-verify.js";
|
|
7
|
+
import { encodeAlgorithmIdentifier as T, getSignatureAlgorithm as E, signBytes as D } from "../internal/crypto/signing.js";
|
|
8
|
+
import { getCrypto as O } from "../internal/crypto/webcrypto.js";
|
|
9
|
+
import { base64Encode as k } from "../internal/shared/base64.js";
|
|
10
|
+
import { compareDistinguishedNames as A } from "../internal/shared/dn.js";
|
|
11
|
+
import { pemEncode as j, splitPemBlocksOrThrow as M } from "../pem/pem.js";
|
|
12
|
+
import { DECODE_REFUSAL_CODES as N, failureResult as P, rethrowIfInvariant as F } from "../result/result.js";
|
|
13
|
+
import { nameFieldKeyFromOid as I } from "../internal/x509/name-fields.js";
|
|
14
|
+
import { parseCertificateDerOrThrow as L } from "../x509/parse.js";
|
|
15
15
|
// src/pkcs/pkcs7.ts
|
|
16
16
|
/**
|
|
17
17
|
* PKCS#7/CMS certificate bags and SignedData.
|
|
@@ -25,37 +25,37 @@ import { parseCertificateDerOrThrow as I } from "../x509/parse.js";
|
|
|
25
25
|
* @module
|
|
26
26
|
*/
|
|
27
27
|
/** RFC 7468 Section 8 armors an RFC 2315 ContentInfo; Section 9 armors an RFC 5652 one. */
|
|
28
|
-
const
|
|
28
|
+
const R = ["PKCS7", "CMS"];
|
|
29
29
|
/**
|
|
30
30
|
* Creates a degenerate PKCS#7 SignedData structure containing only
|
|
31
31
|
* certificates (no signers), returning DER, PEM, and base64 forms, or a
|
|
32
32
|
* typed `invalid_certificate` failure when a certificate source is not
|
|
33
33
|
* valid PEM/DER.
|
|
34
34
|
*/
|
|
35
|
-
function createPkcs7CertBag(
|
|
36
|
-
let
|
|
35
|
+
function createPkcs7CertBag(e) {
|
|
36
|
+
let t;
|
|
37
37
|
try {
|
|
38
|
-
|
|
39
|
-
for (let e of
|
|
40
|
-
} catch (
|
|
41
|
-
let
|
|
42
|
-
return
|
|
38
|
+
t = e.flatMap(normalizeCertificateSource);
|
|
39
|
+
for (let e of t) L(e);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
let t = c(e, N);
|
|
42
|
+
return t === void 0 ? createCertBagFailure("invalid_certificate", "Each PKCS#7 certificate source must be valid PEM or DER") : createCertBagFailure(t.code, t.message);
|
|
43
43
|
}
|
|
44
|
-
let
|
|
45
|
-
|
|
46
|
-
let
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
]),
|
|
44
|
+
let n = new Uint8Array(v(t));
|
|
45
|
+
n[0] = 160;
|
|
46
|
+
let r = _([
|
|
47
|
+
d(1),
|
|
48
|
+
v([]),
|
|
49
|
+
_([f(y.pkcs7Data)]),
|
|
50
|
+
n,
|
|
51
|
+
v([])
|
|
52
|
+
]), i = _([f(y.pkcs7SignedData), l(0, r)]);
|
|
53
53
|
return {
|
|
54
54
|
ok: !0,
|
|
55
55
|
value: {
|
|
56
|
-
der:
|
|
57
|
-
pem:
|
|
58
|
-
base64:
|
|
56
|
+
der: i,
|
|
57
|
+
pem: j("PKCS7", i),
|
|
58
|
+
base64: k(i)
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
}
|
|
@@ -79,38 +79,38 @@ function createPkcs7CertBag(t) {
|
|
|
79
79
|
* certificate whose public key cannot verify the signer key's algorithm, or an
|
|
80
80
|
* unsupported signer key).
|
|
81
81
|
*/
|
|
82
|
-
async function createPkcs7SignedData(
|
|
83
|
-
if (
|
|
84
|
-
let
|
|
85
|
-
let t =
|
|
86
|
-
|
|
87
|
-
},
|
|
88
|
-
for (let
|
|
89
|
-
let
|
|
90
|
-
if (!
|
|
91
|
-
addCertificate(
|
|
82
|
+
async function createPkcs7SignedData(e) {
|
|
83
|
+
if (e.signers.length === 0) return createPkcs7Failure("no_signers", "createPkcs7SignedData requires at least one signer");
|
|
84
|
+
let t = e.encapsulatedContentTypeOid ?? y.pkcs7Data, n = new Uint8Array(e.content), r = [], i = /* @__PURE__ */ new Set(), addCertificate = (e) => {
|
|
85
|
+
let t = s(e);
|
|
86
|
+
i.has(t) || (i.add(t), r.push(e));
|
|
87
|
+
}, a = /* @__PURE__ */ new Set(), o = [];
|
|
88
|
+
for (let r of e.signers) {
|
|
89
|
+
let e = await buildPkcs7SignerInfo(r, n, t);
|
|
90
|
+
if (!e.ok) return e;
|
|
91
|
+
addCertificate(e.certificateDer), a.add(e.digestOid), o.push(e.signerInfoDer);
|
|
92
92
|
}
|
|
93
|
-
for (let
|
|
94
|
-
for (let e of normalizeCertificateSource(
|
|
95
|
-
} catch (
|
|
96
|
-
let
|
|
97
|
-
return
|
|
93
|
+
for (let t of e.additionalCertificates ?? []) try {
|
|
94
|
+
for (let e of normalizeCertificateSource(t)) L(e), addCertificate(e);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
let t = c(e, N);
|
|
97
|
+
return t === void 0 ? createPkcs7Failure("invalid_certificate", "Each additional PKCS#7 certificate source must be valid PEM or DER") : createPkcs7Failure(t.code, t.message);
|
|
98
98
|
}
|
|
99
|
-
let
|
|
100
|
-
|
|
101
|
-
let
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
]),
|
|
99
|
+
let u = t === y.pkcs7Data ? 1 : 3, m = new Uint8Array(v(r));
|
|
100
|
+
m[0] = 160;
|
|
101
|
+
let h = _([
|
|
102
|
+
d(u),
|
|
103
|
+
v([...a].map((e) => _([f(e)]))),
|
|
104
|
+
e.detached === !0 ? _([f(t)]) : _([f(t), l(0, p(n))]),
|
|
105
|
+
m,
|
|
106
|
+
v(o)
|
|
107
|
+
]), g = _([f(y.pkcs7SignedData), l(0, h)]);
|
|
108
108
|
return {
|
|
109
109
|
ok: !0,
|
|
110
110
|
value: {
|
|
111
|
-
der:
|
|
112
|
-
pem:
|
|
113
|
-
base64:
|
|
111
|
+
der: g,
|
|
112
|
+
pem: j(u === 1 ? "PKCS7" : "CMS", g),
|
|
113
|
+
base64: k(g)
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
116
|
}
|
|
@@ -120,49 +120,49 @@ async function createPkcs7SignedData(t) {
|
|
|
120
120
|
* caller-correctable input — malformed PEM/DER, unsupported key — so each is
|
|
121
121
|
* mapped to a typed failure rather than rejecting the public Promise.
|
|
122
122
|
*/
|
|
123
|
-
async function buildPkcs7SignerInfo(t, n
|
|
124
|
-
let
|
|
123
|
+
async function buildPkcs7SignerInfo(e, t, n) {
|
|
124
|
+
let r;
|
|
125
125
|
try {
|
|
126
|
-
|
|
126
|
+
r = normalizeCertificateSource(e.certificate);
|
|
127
127
|
} catch {
|
|
128
128
|
return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate source must be valid PEM or DER");
|
|
129
129
|
}
|
|
130
|
-
let
|
|
131
|
-
if (
|
|
132
|
-
let
|
|
130
|
+
let a = r[0];
|
|
131
|
+
if (a === void 0 || r.length !== 1) return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer must provide exactly one certificate");
|
|
132
|
+
let s;
|
|
133
133
|
try {
|
|
134
|
-
|
|
135
|
-
} catch (
|
|
136
|
-
let
|
|
137
|
-
return
|
|
134
|
+
s = L(a);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
let t = c(e, N);
|
|
137
|
+
return t === void 0 ? createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate must be a parseable X.509 certificate") : createPkcs7Failure(t.code, t.message);
|
|
138
138
|
}
|
|
139
|
-
let
|
|
139
|
+
let l;
|
|
140
140
|
try {
|
|
141
|
-
|
|
141
|
+
l = E(e.privateKey, e.signature);
|
|
142
142
|
} catch {
|
|
143
143
|
return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key or signature profile");
|
|
144
144
|
}
|
|
145
|
-
if (!
|
|
146
|
-
let m = contentDigestForPrivateKey(
|
|
145
|
+
if (!C(l.algorithmOid, l.parameters, s.publicKeyAlgorithmOid, s.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(e.privateKey);
|
|
147
147
|
if (m === void 0) return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key algorithm for content digest");
|
|
148
|
-
let { setForSigning: h, implicitForEmit: g } = buildSignedAttributes(
|
|
148
|
+
let { setForSigning: h, implicitForEmit: g } = buildSignedAttributes(n, new Uint8Array(await O().subtle.digest(m.hashName, o(t)))), v;
|
|
149
149
|
try {
|
|
150
|
-
|
|
150
|
+
v = await D(e.privateKey, l, h);
|
|
151
151
|
} catch {
|
|
152
152
|
return createPkcs7Failure("unsupported_signer_key", "Signer key could not produce a PKCS#7 signature");
|
|
153
153
|
}
|
|
154
154
|
return {
|
|
155
155
|
ok: !0,
|
|
156
|
-
signerInfoDer:
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
signerInfoDer: _([
|
|
157
|
+
d(1),
|
|
158
|
+
_([i(s.issuer.derHex), u(i(s.serialNumberHex))]),
|
|
159
|
+
_([f(m.digestOid)]),
|
|
160
160
|
g,
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
T(l),
|
|
162
|
+
p(v)
|
|
163
163
|
]),
|
|
164
164
|
digestOid: m.digestOid,
|
|
165
|
-
certificateDer:
|
|
165
|
+
certificateDer: a
|
|
166
166
|
};
|
|
167
167
|
}
|
|
168
168
|
/** Parses a DER-encoded PKCS#7 cert bag, returning the contained certificates. */
|
|
@@ -176,7 +176,7 @@ function parsePkcs7CertBagDer(e) {
|
|
|
176
176
|
/** The DER of the single `PKCS7` or `CMS` block in `pem`, or a typed failure. */
|
|
177
177
|
function contentInfoPemBytes(e) {
|
|
178
178
|
try {
|
|
179
|
-
let t =
|
|
179
|
+
let t = M(e).filter((e) => R.some((t) => t === e.label)), n = t[0];
|
|
180
180
|
return n === void 0 || t.length !== 1 ? pkcs7Failure("malformed", "Expected exactly one PKCS7 or CMS PEM block") : {
|
|
181
181
|
ok: !0,
|
|
182
182
|
bytes: n.bytes
|
|
@@ -191,35 +191,35 @@ function parsePkcs7CertBagPem(e) {
|
|
|
191
191
|
return t.ok ? parsePkcs7CertBagDer(t.bytes) : t;
|
|
192
192
|
}
|
|
193
193
|
/** Decodes a DER-encoded PKCS#7 ContentInfo expecting `signedData` content type. */
|
|
194
|
-
function parsePkcs7SignedDataDer(
|
|
194
|
+
function parsePkcs7SignedDataDer(r) {
|
|
195
195
|
try {
|
|
196
|
-
let
|
|
197
|
-
if (
|
|
198
|
-
let
|
|
199
|
-
if (
|
|
200
|
-
if (
|
|
201
|
-
let
|
|
202
|
-
if (!
|
|
203
|
-
if (
|
|
204
|
-
let _ =
|
|
205
|
-
return
|
|
196
|
+
let i = g(r, { maxDepth: 64 }), a = i[0], o = i[1];
|
|
197
|
+
if (a === void 0 || i.length > 2 || a.tag !== 6 || o !== void 0 && (o.tag !== 160 || e(r, o).length !== 1)) return pkcs7Failure("malformed", "Malformed PKCS#7 content info");
|
|
198
|
+
let s = n(a.value);
|
|
199
|
+
if (s !== y.pkcs7SignedData) return pkcs7Failure("not_signed_data", "PKCS#7 content is not signedData");
|
|
200
|
+
if (o === void 0) return pkcs7Failure("malformed", "PKCS#7 signedData content info carries no content");
|
|
201
|
+
let c = childAt(r, o, 0, "signedData"), l = e(r, c), u = l[0], d = l[1], f = l[2], p = l.slice(3), m = p[p.length - 1], h = parseSignedDataOptionalFields(p.slice(0, -1));
|
|
202
|
+
if (!h.ok) return h;
|
|
203
|
+
if (u === void 0 || d === void 0 || f === void 0 || m === void 0 || m.tag !== 49) return pkcs7Failure("malformed", "Malformed SignedData");
|
|
204
|
+
let _ = r.slice(f.start - f.headerLength, f.end), v = g(_), x = v[0], S = v[1], C = parseDigestAlgorithms(r, d);
|
|
205
|
+
return x === void 0 || v.length > 2 || x.tag !== 6 ? pkcs7Failure("malformed", "Malformed EncapsulatedContentInfo") : {
|
|
206
206
|
ok: !0,
|
|
207
207
|
value: {
|
|
208
|
-
der: new Uint8Array(
|
|
209
|
-
contentTypeOid:
|
|
210
|
-
version:
|
|
211
|
-
digestAlgorithmOids:
|
|
212
|
-
digestAlgorithmNames:
|
|
213
|
-
encapsulatedContentTypeOid:
|
|
214
|
-
...
|
|
215
|
-
certificateChoices: parseCertificateSet(
|
|
216
|
-
signerInfos: parseSignerInfos(
|
|
208
|
+
der: new Uint8Array(r),
|
|
209
|
+
contentTypeOid: s,
|
|
210
|
+
version: t(u.value),
|
|
211
|
+
digestAlgorithmOids: C,
|
|
212
|
+
digestAlgorithmNames: C.map((e) => b(e)),
|
|
213
|
+
encapsulatedContentTypeOid: n(x.value),
|
|
214
|
+
...S === void 0 ? {} : { encapsulatedContent: extractEncapsulatedContent(_, S) },
|
|
215
|
+
certificateChoices: parseCertificateSet(r, h.value.certificates),
|
|
216
|
+
signerInfos: parseSignerInfos(r, m)
|
|
217
217
|
}
|
|
218
218
|
};
|
|
219
|
-
} catch (
|
|
220
|
-
|
|
221
|
-
let
|
|
222
|
-
return
|
|
219
|
+
} catch (e) {
|
|
220
|
+
F(e);
|
|
221
|
+
let t = c(e, N);
|
|
222
|
+
return t === void 0 ? pkcs7Failure("malformed", "Malformed PKCS#7 structure") : pkcs7Failure(t.code, t.message);
|
|
223
223
|
}
|
|
224
224
|
}
|
|
225
225
|
function parseSignedDataOptionalFields(e) {
|
|
@@ -251,6 +251,10 @@ function parsePkcs7SignedDataPem(e) {
|
|
|
251
251
|
* Accepts PEM text, raw DER, or an already-parsed {@linkcode ParsedPkcs7SignedData}.
|
|
252
252
|
* For each signer, locates the matching certificate in the embedded set and
|
|
253
253
|
* verifies the signature (including signed-attribute digest checks per RFC 5652 Section 5.4).
|
|
254
|
+
* Defaults to id-data and requires signed attributes on every signer. Select
|
|
255
|
+
* `expectedContentTypeOid` from the application's protocol, not the input envelope.
|
|
256
|
+
* Legacy bare id-data signatures require `signedAttributes: 'allowed'` or 'forbidden'.
|
|
257
|
+
* Non-id-data always requires attributes (RFC 5652 §5.3).
|
|
254
258
|
*
|
|
255
259
|
* For a detached SignedData (absent `eContent`), pass the externally-held
|
|
256
260
|
* content via `options.content`; without it, verification fails with the
|
|
@@ -273,24 +277,31 @@ function parsePkcs7SignedDataPem(e) {
|
|
|
273
277
|
* ```
|
|
274
278
|
*/
|
|
275
279
|
async function verifyPkcs7SignedData(e, t = {}) {
|
|
276
|
-
let n =
|
|
280
|
+
let n = resolvePkcs7VerifyPolicy(t);
|
|
277
281
|
if (!n.ok) return n;
|
|
278
|
-
let r = n
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
282
|
+
let { expectedContentTypeOid: r, signedAttributes: i, verification: a } = n, o = normalizePkcs7SignedDataForVerification(e);
|
|
283
|
+
if (!o.ok) return o;
|
|
284
|
+
let s = o.value, c = s.encapsulatedContent ?? (t.content === void 0 ? void 0 : new Uint8Array(t.content));
|
|
285
|
+
if (c === void 0) return verifyPkcs7Failure("detached_content_required", "SignedData has no encapsulated content; supply the detached content via options.content");
|
|
286
|
+
if (s.signerInfos.length === 0) return verifyPkcs7Failure("no_signers", "SignedData contains no signer information");
|
|
287
|
+
if (s.encapsulatedContentTypeOid !== r) return verifyPkcs7Failure("content_type_mismatch", "Unexpected SignedData content type");
|
|
288
|
+
for (let e of s.signerInfos) {
|
|
289
|
+
if (!e.hasSignedAttrs && (i === "required" || s.encapsulatedContentTypeOid !== y.pkcs7Data)) return verifyPkcs7Failure("signed_attributes_required", "Signed attributes are required");
|
|
290
|
+
if (e.hasSignedAttrs && i === "forbidden") return verifyPkcs7Failure("signed_attributes_forbidden", "Signed attributes are forbidden");
|
|
291
|
+
}
|
|
292
|
+
let l = [];
|
|
293
|
+
for (let e of s.signerInfos) {
|
|
294
|
+
let t = await verifyPkcs7SignerInfo(s, e, c, a);
|
|
284
295
|
if (!t.ok) return t;
|
|
285
|
-
|
|
296
|
+
l.push({
|
|
286
297
|
signerInfo: e,
|
|
287
298
|
certificate: t.certificate
|
|
288
299
|
});
|
|
289
300
|
}
|
|
290
301
|
return {
|
|
291
302
|
ok: !0,
|
|
292
|
-
value:
|
|
293
|
-
signers:
|
|
303
|
+
value: s,
|
|
304
|
+
signers: l
|
|
294
305
|
};
|
|
295
306
|
}
|
|
296
307
|
/** Normalizes encoded or parsed SignedData into a freshly parsed verification input. */
|
|
@@ -298,48 +309,75 @@ function normalizePkcs7SignedDataForVerification(e) {
|
|
|
298
309
|
return typeof e == "string" ? parsePkcs7SignedDataPem(e) : e instanceof Uint8Array ? parsePkcs7SignedDataDer(e) : hasReparseablePkcs7SignedData(e) ? parsePkcs7SignedDataDer(new Uint8Array(e.der)) : verifyPkcs7Failure("malformed", "SignedData parsed input is malformed");
|
|
299
310
|
}
|
|
300
311
|
/** Resolves and verifies one signer entry in a parsed PKCS#7 SignedData value. */
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
312
|
+
/** Validates the caller policy in {@linkcode VerifyPkcs7SignedDataOptions}. */
|
|
313
|
+
function resolvePkcs7VerifyPolicy(e) {
|
|
314
|
+
let t = e.signedAttributes ?? "required";
|
|
315
|
+
if (![
|
|
316
|
+
"required",
|
|
317
|
+
"allowed",
|
|
318
|
+
"forbidden"
|
|
319
|
+
].includes(t)) return verifyPkcs7Failure("invalid_options", "Unknown signed-attribute policy");
|
|
320
|
+
let r;
|
|
321
|
+
try {
|
|
322
|
+
r = n(m(f(e.expectedContentTypeOid ?? y.pkcs7Data)).value);
|
|
323
|
+
} catch {
|
|
324
|
+
return verifyPkcs7Failure("invalid_options", "Expected content type must be an OID");
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
let n = S(e.signatureVerifiers);
|
|
328
|
+
return {
|
|
329
|
+
ok: !0,
|
|
330
|
+
expectedContentTypeOid: r,
|
|
331
|
+
signedAttributes: t,
|
|
332
|
+
verification: n
|
|
333
|
+
};
|
|
334
|
+
} catch (e) {
|
|
335
|
+
if (e instanceof RangeError) return verifyPkcs7Failure("invalid_options", e.message);
|
|
336
|
+
throw e;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async function verifyPkcs7SignerInfo(e, t, n, r) {
|
|
340
|
+
let i = x509CertificatesOf(e.certificateChoices).find((e) => signerIdentifierMatches(e, t));
|
|
341
|
+
if (i === void 0) return verifyPkcs7Failure("signer_not_found", "Signer certificate not found in SignedData certificates");
|
|
342
|
+
let a = t.hasSignedAttrs ? await verifySignedAttrs(t, i, n, e.encapsulatedContentTypeOid, r) : await verifyPkcs7BareSignerInfo(t, i, n, r);
|
|
343
|
+
return a.ok ? {
|
|
306
344
|
ok: !0,
|
|
307
|
-
certificate:
|
|
308
|
-
} :
|
|
345
|
+
certificate: i
|
|
346
|
+
} : a;
|
|
309
347
|
}
|
|
310
|
-
async function verifyPkcs7BareSignerInfo(e, t, n) {
|
|
348
|
+
async function verifyPkcs7BareSignerInfo(e, t, n, r) {
|
|
311
349
|
try {
|
|
312
|
-
let
|
|
313
|
-
return
|
|
350
|
+
let i = await w(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, 0, n, r);
|
|
351
|
+
return i.ok ? i.valid ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature does not verify") : i.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
|
|
314
352
|
} catch (e) {
|
|
315
353
|
return signatureVerificationThrowFailure(e);
|
|
316
354
|
}
|
|
317
355
|
}
|
|
318
356
|
/** Shorthand for constructing a PKCS#7 parse failure result. */
|
|
319
|
-
function pkcs7Failure(e,
|
|
320
|
-
return
|
|
357
|
+
function pkcs7Failure(e, t) {
|
|
358
|
+
return P(e, t);
|
|
321
359
|
}
|
|
322
360
|
/** Shorthand for constructing a PKCS#7 verification failure result. */
|
|
323
361
|
/** A signer's signature verification that threw: its decode refusal, else `malformed`. */
|
|
324
|
-
function signatureVerificationThrowFailure(
|
|
325
|
-
let
|
|
326
|
-
return
|
|
362
|
+
function signatureVerificationThrowFailure(e) {
|
|
363
|
+
let t = c(e, N);
|
|
364
|
+
return t === void 0 ? verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData") : verifyPkcs7Failure(t.code, t.message);
|
|
327
365
|
}
|
|
328
|
-
function verifyPkcs7Failure(e,
|
|
329
|
-
return
|
|
366
|
+
function verifyPkcs7Failure(e, t) {
|
|
367
|
+
return P(e, t);
|
|
330
368
|
}
|
|
331
369
|
/** Shorthand for constructing a PKCS#7 certificate bag creation failure result. */
|
|
332
|
-
function createCertBagFailure(e,
|
|
333
|
-
return
|
|
370
|
+
function createCertBagFailure(e, t) {
|
|
371
|
+
return P(e, t);
|
|
334
372
|
}
|
|
335
373
|
/** Shorthand for constructing a PKCS#7 SignedData creation failure result. */
|
|
336
|
-
function createPkcs7Failure(e,
|
|
337
|
-
return
|
|
374
|
+
function createPkcs7Failure(e, t) {
|
|
375
|
+
return P(e, t);
|
|
338
376
|
}
|
|
339
377
|
/** Converts PEM text to an array of DER certificate blobs, or wraps raw DER. */
|
|
340
378
|
function normalizeCertificateSource(e) {
|
|
341
379
|
if (typeof e == "string") {
|
|
342
|
-
let t =
|
|
380
|
+
let t = M(e).filter((e) => e.label === "CERTIFICATE").map((e) => new Uint8Array(e.bytes));
|
|
343
381
|
if (t.length === 0) throw Error("Certificate PEM required");
|
|
344
382
|
return t;
|
|
345
383
|
}
|
|
@@ -368,15 +406,15 @@ function contentDigestForPrivateKey(e) {
|
|
|
368
406
|
switch (t.namedCurve) {
|
|
369
407
|
case "P-256": return {
|
|
370
408
|
hashName: "SHA-256",
|
|
371
|
-
digestOid:
|
|
409
|
+
digestOid: y.sha256
|
|
372
410
|
};
|
|
373
411
|
case "P-384": return {
|
|
374
412
|
hashName: "SHA-384",
|
|
375
|
-
digestOid:
|
|
413
|
+
digestOid: y.sha384
|
|
376
414
|
};
|
|
377
415
|
case "P-521": return {
|
|
378
416
|
hashName: "SHA-512",
|
|
379
|
-
digestOid:
|
|
417
|
+
digestOid: y.sha512
|
|
380
418
|
};
|
|
381
419
|
default: return;
|
|
382
420
|
}
|
|
@@ -386,22 +424,22 @@ function contentDigestForPrivateKey(e) {
|
|
|
386
424
|
switch (t.hash.name) {
|
|
387
425
|
case "SHA-256": return {
|
|
388
426
|
hashName: "SHA-256",
|
|
389
|
-
digestOid:
|
|
427
|
+
digestOid: y.sha256
|
|
390
428
|
};
|
|
391
429
|
case "SHA-384": return {
|
|
392
430
|
hashName: "SHA-384",
|
|
393
|
-
digestOid:
|
|
431
|
+
digestOid: y.sha384
|
|
394
432
|
};
|
|
395
433
|
case "SHA-512": return {
|
|
396
434
|
hashName: "SHA-512",
|
|
397
|
-
digestOid:
|
|
435
|
+
digestOid: y.sha512
|
|
398
436
|
};
|
|
399
437
|
default: return;
|
|
400
438
|
}
|
|
401
439
|
}
|
|
402
440
|
if (t.name === "Ed25519") return {
|
|
403
441
|
hashName: "SHA-512",
|
|
404
|
-
digestOid:
|
|
442
|
+
digestOid: y.sha512
|
|
405
443
|
};
|
|
406
444
|
}
|
|
407
445
|
/**
|
|
@@ -411,7 +449,7 @@ function contentDigestForPrivateKey(e) {
|
|
|
411
449
|
* SignerInfo. The two differ only in the leading tag byte.
|
|
412
450
|
*/
|
|
413
451
|
function buildSignedAttributes(e, t) {
|
|
414
|
-
let n =
|
|
452
|
+
let n = v([_([f(y.cmsContentType), v([f(e)])]), _([f(y.cmsMessageDigest), v([p(t)])])]), r = new Uint8Array(n);
|
|
415
453
|
return r[0] = 160, {
|
|
416
454
|
setForSigning: n,
|
|
417
455
|
implicitForEmit: r
|
|
@@ -427,17 +465,17 @@ function x509CertificatesOf(e) {
|
|
|
427
465
|
* The IMPLICIT tag replaces the SEQUENCE tag, so the OID and the `ANY DEFINED BY`
|
|
428
466
|
* value are direct children of the `[3]` element.
|
|
429
467
|
*/
|
|
430
|
-
function parseOtherCertificateFormatOid(
|
|
431
|
-
let
|
|
432
|
-
if (
|
|
433
|
-
return
|
|
468
|
+
function parseOtherCertificateFormatOid(t, r) {
|
|
469
|
+
let i = a(e(t, r)[0], "otherCertFormat");
|
|
470
|
+
if (i.tag !== 6) throw Error("otherCertFormat must be an OBJECT IDENTIFIER");
|
|
471
|
+
return n(i.value);
|
|
434
472
|
}
|
|
435
473
|
/** Decodes one RFC 5652 §10.2.2 CertificateChoices alternative. */
|
|
436
474
|
function parseCertificateChoice(e, t, n) {
|
|
437
475
|
switch (t.tag) {
|
|
438
476
|
case 48: return {
|
|
439
477
|
type: "certificate",
|
|
440
|
-
certificate:
|
|
478
|
+
certificate: L(n)
|
|
441
479
|
};
|
|
442
480
|
case 160: return {
|
|
443
481
|
type: "extendedCertificate",
|
|
@@ -463,118 +501,118 @@ function parseCertificateChoice(e, t, n) {
|
|
|
463
501
|
* Parses the IMPLICIT [0] certificate set from a SignedData structure as
|
|
464
502
|
* RFC 5652 §10.2.3 `CertificateSet ::= SET OF CertificateChoices`.
|
|
465
503
|
*/
|
|
466
|
-
function parseCertificateSet(
|
|
467
|
-
return
|
|
504
|
+
function parseCertificateSet(t, n) {
|
|
505
|
+
return n === void 0 || n.tag !== 160 ? [] : e(t, n).map((e) => parseCertificateChoice(t, e, t.slice(e.start - e.headerLength, e.end)));
|
|
468
506
|
}
|
|
469
507
|
/** Extracts the list of digest algorithm OIDs from the digestAlgorithms SET. */
|
|
470
|
-
function parseDigestAlgorithms(
|
|
471
|
-
let
|
|
472
|
-
if (
|
|
473
|
-
for (let
|
|
474
|
-
if (
|
|
475
|
-
let
|
|
476
|
-
if (
|
|
477
|
-
|
|
508
|
+
function parseDigestAlgorithms(t, r) {
|
|
509
|
+
let i = [];
|
|
510
|
+
if (r.tag !== 49) throw Error("digestAlgorithms must use SET");
|
|
511
|
+
for (let a of e(t, r)) {
|
|
512
|
+
if (a.tag !== 48) throw Error("digestAlgorithm must use AlgorithmIdentifier SEQUENCE");
|
|
513
|
+
let e = t.slice(a.start - a.headerLength, a.end), r = g(e), o = r[0];
|
|
514
|
+
if (o === void 0 || r.length < 1 || r.length > 2 || o.tag !== 6) throw Error("Malformed digest AlgorithmIdentifier");
|
|
515
|
+
i.push(n(o.value));
|
|
478
516
|
}
|
|
479
|
-
return
|
|
517
|
+
return i;
|
|
480
518
|
}
|
|
481
519
|
/** Decodes the signerInfos SET OF from a SignedData structure. */
|
|
482
|
-
function parseSignerInfos(
|
|
483
|
-
return
|
|
484
|
-
}
|
|
485
|
-
function parseSignerInfo(e,
|
|
486
|
-
let
|
|
487
|
-
|
|
488
|
-
let
|
|
489
|
-
if (
|
|
490
|
-
let
|
|
520
|
+
function parseSignerInfos(t, n) {
|
|
521
|
+
return e(t, n).map((e) => parseSignerInfo(t, e));
|
|
522
|
+
}
|
|
523
|
+
function parseSignerInfo(e, r) {
|
|
524
|
+
let i = e.slice(r.start - r.headerLength, r.end), o = g(i), c = o[0], l = o[1], u = o[2], d = 3, f = o[d]?.tag === 160 ? o[d] : void 0;
|
|
525
|
+
f !== void 0 && (d += 1);
|
|
526
|
+
let p = o[d], m = o[d + 1], h = o[d + 2];
|
|
527
|
+
if (c === void 0 || l === void 0 || u === void 0 || p === void 0 || m === void 0 || m.tag !== 4 || o.length > d + 3 || h !== void 0 && h.tag !== 161) throw Error("Malformed SignerInfo");
|
|
528
|
+
let _ = parsePkcs7AlgorithmOid(i, u, "digest algorithm OID"), v = i.slice(p.start - p.headerLength, p.end), y = g(v), S = n(a(y[0], "signature algorithm OID").value), C = y[1];
|
|
491
529
|
return {
|
|
492
|
-
version:
|
|
493
|
-
signerIdentifier: parseSignerIdentifier(
|
|
494
|
-
digestAlgorithmOid:
|
|
495
|
-
digestAlgorithmName:
|
|
496
|
-
signatureAlgorithmOid:
|
|
497
|
-
signatureAlgorithmName:
|
|
498
|
-
...
|
|
499
|
-
signatureHex:
|
|
500
|
-
signature: new Uint8Array(
|
|
501
|
-
...
|
|
530
|
+
version: t(c.value),
|
|
531
|
+
signerIdentifier: parseSignerIdentifier(i.slice(l.start - l.headerLength, l.end)),
|
|
532
|
+
digestAlgorithmOid: _,
|
|
533
|
+
digestAlgorithmName: b(_),
|
|
534
|
+
signatureAlgorithmOid: S,
|
|
535
|
+
signatureAlgorithmName: x(S, C === void 0 ? void 0 : new Uint8Array(v.slice(C.start - C.headerLength, C.end))),
|
|
536
|
+
...C === void 0 ? {} : { signatureAlgorithmParametersDer: new Uint8Array(v.slice(C.start - C.headerLength, C.end)) },
|
|
537
|
+
signatureHex: s(m.value),
|
|
538
|
+
signature: new Uint8Array(m.value),
|
|
539
|
+
...f === void 0 ? { hasSignedAttrs: !1 } : {
|
|
502
540
|
hasSignedAttrs: !0,
|
|
503
|
-
signedAttrsDer: new Uint8Array(
|
|
541
|
+
signedAttrsDer: new Uint8Array(i.slice(f.start - f.headerLength, f.end))
|
|
504
542
|
}
|
|
505
543
|
};
|
|
506
544
|
}
|
|
507
|
-
function parsePkcs7AlgorithmOid(e, t,
|
|
508
|
-
let
|
|
509
|
-
return
|
|
545
|
+
function parsePkcs7AlgorithmOid(e, t, r) {
|
|
546
|
+
let i = e.slice(t.start - t.headerLength, t.end);
|
|
547
|
+
return n(a(g(i)[0], r).value);
|
|
510
548
|
}
|
|
511
549
|
/** Unwraps the OCTET STRING inside the IMPLICIT [0] encapsulated content. */
|
|
512
550
|
function extractEncapsulatedContent(e, t) {
|
|
513
551
|
if (t.tag !== 160) throw Error("Unexpected encapsulated content tag");
|
|
514
|
-
let n =
|
|
552
|
+
let n = m(e, t.start);
|
|
515
553
|
if (n.tag !== 4) throw Error("Expected encapsulated OCTET STRING");
|
|
516
554
|
if (n.end !== t.end) throw Error("Encapsulated content must hold exactly one element");
|
|
517
555
|
return n.value;
|
|
518
556
|
}
|
|
519
557
|
/** Extracts issuer Name and serial number from an issuerAndSerialNumber SEQUENCE, or subjectKeyIdentifier from [0] IMPLICIT. */
|
|
520
558
|
function parseSignerIdentifier(e) {
|
|
521
|
-
let t =
|
|
559
|
+
let t = h(e, { maxDepth: 64 });
|
|
522
560
|
if (t.tag === 128) {
|
|
523
561
|
if (t.value.length === 0) throw Error("SignerIdentifier subjectKeyIdentifier must not be empty");
|
|
524
562
|
return {
|
|
525
563
|
type: "subjectKeyIdentifier",
|
|
526
|
-
subjectKeyIdentifier:
|
|
564
|
+
subjectKeyIdentifier: s(t.value)
|
|
527
565
|
};
|
|
528
566
|
}
|
|
529
567
|
if (t.tag === 48) {
|
|
530
|
-
let t =
|
|
568
|
+
let t = g(e), n = t[0], r = t[1];
|
|
531
569
|
if (n === void 0 || r === void 0 || t.length !== 2) throw Error("SignerIdentifier issuerAndSerialNumber is malformed");
|
|
532
570
|
if (n.tag !== 48) throw Error("SignerIdentifier issuer must use Name SEQUENCE");
|
|
533
571
|
if (r.tag !== 2) throw Error("SignerIdentifier serialNumber must use INTEGER");
|
|
534
572
|
return assertImplicitSerialNumberEncoding(r.value, "SignerIdentifier serialNumber"), {
|
|
535
573
|
type: "issuerAndSerialNumber",
|
|
536
574
|
issuer: parseSignerIssuerName(e, n),
|
|
537
|
-
serialNumberHex:
|
|
575
|
+
serialNumberHex: s(r.value)
|
|
538
576
|
};
|
|
539
577
|
}
|
|
540
578
|
throw Error(`Unsupported SignerIdentifier tag: ${String(t.tag)}`);
|
|
541
579
|
}
|
|
542
580
|
/** Parses a Name SEQUENCE element from a PKCS#7 signer identifier into a {@linkcode ParsedName}. */
|
|
543
|
-
function parseSignerIssuerName(
|
|
544
|
-
let
|
|
545
|
-
for (let
|
|
546
|
-
let
|
|
547
|
-
for (let
|
|
548
|
-
let
|
|
549
|
-
oid:
|
|
550
|
-
valueTag:
|
|
551
|
-
value:
|
|
581
|
+
function parseSignerIssuerName(t, i) {
|
|
582
|
+
let o = s(t.slice(i.start - i.headerLength, i.end)), c = [], l = [], u = {};
|
|
583
|
+
for (let o of e(t, i)) {
|
|
584
|
+
let i = [], d = {};
|
|
585
|
+
for (let s of e(t, o)) {
|
|
586
|
+
let o = e(t, s), c = a(o[0], "signer issuer attribute OID"), f = a(o[1], "signer issuer attribute value"), p = n(c.value), m = r(f.tag, f.value), h = I(p), g = h === void 0 ? {
|
|
587
|
+
oid: p,
|
|
588
|
+
valueTag: f.tag,
|
|
589
|
+
value: m
|
|
552
590
|
} : {
|
|
553
|
-
oid:
|
|
554
|
-
key:
|
|
555
|
-
valueTag:
|
|
556
|
-
value:
|
|
591
|
+
oid: p,
|
|
592
|
+
key: h,
|
|
593
|
+
valueTag: f.tag,
|
|
594
|
+
value: m
|
|
557
595
|
};
|
|
558
|
-
|
|
596
|
+
i.push(g), l.push(g), h !== void 0 && (d[h] === void 0 && (d[h] = m), u[h] === void 0 && (u[h] = m));
|
|
559
597
|
}
|
|
560
|
-
|
|
561
|
-
derHex:
|
|
562
|
-
attributes:
|
|
563
|
-
values:
|
|
598
|
+
c.push({
|
|
599
|
+
derHex: s(t.slice(o.start - o.headerLength, o.end)),
|
|
600
|
+
attributes: i,
|
|
601
|
+
values: d
|
|
564
602
|
});
|
|
565
603
|
}
|
|
566
604
|
return {
|
|
567
|
-
derHex:
|
|
568
|
-
rdns:
|
|
569
|
-
attributes:
|
|
570
|
-
values:
|
|
605
|
+
derHex: o,
|
|
606
|
+
rdns: c,
|
|
607
|
+
attributes: l,
|
|
608
|
+
values: u
|
|
571
609
|
};
|
|
572
610
|
}
|
|
573
611
|
/** Returns the nth child element inside a constructed ASN.1 element, or throws. */
|
|
574
612
|
function childAt(e, t, n, r) {
|
|
575
613
|
let i = t.start, a = 0;
|
|
576
614
|
for (; i < t.end;) {
|
|
577
|
-
let t =
|
|
615
|
+
let t = m(e, i);
|
|
578
616
|
if (a === n) return t;
|
|
579
617
|
i = t.end, a += 1;
|
|
580
618
|
}
|
|
@@ -583,40 +621,40 @@ function childAt(e, t, n, r) {
|
|
|
583
621
|
/** Maps a digest algorithm OID to the WebCrypto hash name. */
|
|
584
622
|
function digestAlgorithmHash(e) {
|
|
585
623
|
switch (e) {
|
|
586
|
-
case
|
|
587
|
-
case
|
|
588
|
-
case
|
|
624
|
+
case y.sha256: return "SHA-256";
|
|
625
|
+
case y.sha384: return "SHA-384";
|
|
626
|
+
case y.sha512: return "SHA-512";
|
|
589
627
|
default: throw Error(`Unsupported digest algorithm OID: ${e}`);
|
|
590
628
|
}
|
|
591
629
|
}
|
|
592
630
|
/** Parses required signed attributes and enforces uniqueness + exact-one-value shape. */
|
|
593
|
-
function parseSignedAttributeRequirements(
|
|
594
|
-
let
|
|
595
|
-
for (let
|
|
596
|
-
if (
|
|
597
|
-
if (
|
|
631
|
+
function parseSignedAttributeRequirements(t) {
|
|
632
|
+
let n = m(t), r = {};
|
|
633
|
+
for (let i of e(t, n)) parseSignedAttributeRequirement(t, i, r);
|
|
634
|
+
if (r.messageDigest === void 0) throw Error("Missing messageDigest attribute in signedAttrs");
|
|
635
|
+
if (r.contentTypeOid === void 0) throw Error("Missing contentType attribute in signedAttrs");
|
|
598
636
|
return {
|
|
599
|
-
messageDigest:
|
|
600
|
-
contentTypeOid:
|
|
637
|
+
messageDigest: r.messageDigest,
|
|
638
|
+
contentTypeOid: r.contentTypeOid
|
|
601
639
|
};
|
|
602
640
|
}
|
|
603
|
-
function parseSignedAttributeRequirement(
|
|
604
|
-
let
|
|
605
|
-
if (
|
|
606
|
-
let
|
|
607
|
-
|
|
641
|
+
function parseSignedAttributeRequirement(t, r, i) {
|
|
642
|
+
let a = t.slice(r.start - r.headerLength, r.end), o = g(a), s = o[0], c = o[1];
|
|
643
|
+
if (s === void 0 || c === void 0 || o.length !== 2 || c.tag !== 49) throw Error("Malformed signedAttrs attribute");
|
|
644
|
+
let l = n(s.value), u = e(a, c);
|
|
645
|
+
l === y.cmsMessageDigest && (i.messageDigest = parseUniqueSignedAttributeOctetString(i.messageDigest, u, "messageDigest")), l === y.cmsContentType && (i.contentTypeOid = parseUniqueSignedAttributeOid(i.contentTypeOid, u, "contentType"));
|
|
608
646
|
}
|
|
609
647
|
function parseUniqueSignedAttributeOctetString(e, t, n) {
|
|
610
648
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
611
|
-
let r =
|
|
649
|
+
let r = a(t[0], `${n} attribute value`);
|
|
612
650
|
if (r.tag !== 4) throw Error(`${n} attribute value must use OCTET STRING`);
|
|
613
651
|
return r.value;
|
|
614
652
|
}
|
|
615
|
-
function parseUniqueSignedAttributeOid(e, t,
|
|
616
|
-
if (e !== void 0 || t.length !== 1) throw Error(`${
|
|
617
|
-
let
|
|
618
|
-
if (
|
|
619
|
-
return
|
|
653
|
+
function parseUniqueSignedAttributeOid(e, t, r) {
|
|
654
|
+
if (e !== void 0 || t.length !== 1) throw Error(`${r} attribute must appear exactly once with one value`);
|
|
655
|
+
let i = a(t[0], `${r} attribute value`);
|
|
656
|
+
if (i.tag !== 6) throw Error(`${r} attribute value must use OBJECT IDENTIFIER`);
|
|
657
|
+
return n(i.value);
|
|
620
658
|
}
|
|
621
659
|
/** Replaces the IMPLICIT [0] tag (0xa0) with SET OF (0x31) per RFC 5652 Section 5.4. */
|
|
622
660
|
function retagSignedAttrsAsSet(e) {
|
|
@@ -631,36 +669,36 @@ function constantTimeEqual(e, t) {
|
|
|
631
669
|
return n === 0;
|
|
632
670
|
}
|
|
633
671
|
/** Verifies a signed-attributes flow: digest match + signature over re-tagged attrs. */
|
|
634
|
-
async function verifySignedAttrs(t,
|
|
635
|
-
if (!
|
|
636
|
-
let
|
|
672
|
+
async function verifySignedAttrs(e, t, n, r, i) {
|
|
673
|
+
if (!e.hasSignedAttrs) return verifyPkcs7Failure("malformed", "Missing signedAttrs DER");
|
|
674
|
+
let a;
|
|
637
675
|
try {
|
|
638
|
-
assertImplicitSignedAttrsDer(
|
|
639
|
-
} catch (
|
|
640
|
-
|
|
641
|
-
let
|
|
642
|
-
return
|
|
676
|
+
assertImplicitSignedAttrsDer(e.signedAttrsDer), a = parseSignedAttributeRequirements(e.signedAttrsDer);
|
|
677
|
+
} catch (e) {
|
|
678
|
+
F(e);
|
|
679
|
+
let t = c(e, N);
|
|
680
|
+
return t === void 0 ? verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData") : verifyPkcs7Failure(t.code, t.message);
|
|
643
681
|
}
|
|
644
|
-
if (
|
|
645
|
-
let
|
|
682
|
+
if (a.contentTypeOid !== r) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
|
|
683
|
+
let s;
|
|
646
684
|
try {
|
|
647
|
-
let
|
|
648
|
-
|
|
685
|
+
let t = digestAlgorithmHash(e.digestAlgorithmOid);
|
|
686
|
+
s = new Uint8Array(await O().subtle.digest(t, o(n)));
|
|
649
687
|
} catch {
|
|
650
688
|
return verifyPkcs7Failure("malformed", "Unsupported digest algorithm in SignedData");
|
|
651
689
|
}
|
|
652
|
-
if (!constantTimeEqual(
|
|
690
|
+
if (!constantTimeEqual(s, a.messageDigest)) return verifyPkcs7Failure("message_digest_mismatch", "Content digest does not match messageDigest attribute");
|
|
653
691
|
let l;
|
|
654
692
|
try {
|
|
655
|
-
l = retagSignedAttrsAsSet(
|
|
693
|
+
l = retagSignedAttrsAsSet(e.signedAttrsDer);
|
|
656
694
|
} catch {
|
|
657
695
|
return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
|
|
658
696
|
}
|
|
659
697
|
let u;
|
|
660
698
|
try {
|
|
661
|
-
let
|
|
662
|
-
if (!
|
|
663
|
-
u =
|
|
699
|
+
let n = await w(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, 0, l, i);
|
|
700
|
+
if (!n.ok) return n.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
|
|
701
|
+
u = n.valid;
|
|
664
702
|
} catch (e) {
|
|
665
703
|
return signatureVerificationThrowFailure(e);
|
|
666
704
|
}
|
|
@@ -668,10 +706,10 @@ async function verifySignedAttrs(t, i, a, o) {
|
|
|
668
706
|
}
|
|
669
707
|
function signerIdentifierMatches(e, t) {
|
|
670
708
|
let n = t.signerIdentifier;
|
|
671
|
-
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex &&
|
|
709
|
+
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex && A(e.issuer, n.issuer) : e.subjectKeyIdentifier === n.subjectKeyIdentifier;
|
|
672
710
|
}
|
|
673
711
|
function assertImplicitSignedAttrsDer(e) {
|
|
674
|
-
if (
|
|
712
|
+
if (m(e).tag !== 160) throw Error("signedAttrs must use IMPLICIT [0] tag");
|
|
675
713
|
}
|
|
676
714
|
function hasReparseablePkcs7SignedData(e) {
|
|
677
715
|
return "der" in e && e.der instanceof Uint8Array;
|