micro509 0.17.0 → 0.18.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 +123 -6
- package/README.md +3 -0
- package/dist/crypto/crypto.d.ts +2 -3
- package/dist/crypto.d.ts +6 -0
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +82 -15
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/der.d.ts +1 -2
- 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/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- 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/sig-verify.d.ts +1 -2
- package/dist/internal/crypto/sig-verify.js +60 -60
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- 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/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +19 -19
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/name-fields.js +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/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 +15 -0
- package/dist/kdf/argon2-arithmetic.js.map +1 -0
- package/dist/kdf/argon2-core.js +486 -0
- package/dist/kdf/argon2-core.js.map +1 -0
- package/dist/kdf/argon2-memory.js +20 -0
- package/dist/kdf/argon2-memory.js.map +1 -0
- package/dist/kdf/argon2-portable.js +18 -0
- package/dist/kdf/argon2-portable.js.map +1 -0
- package/dist/kdf/argon2.d.ts +22 -11
- package/dist/kdf/argon2.js +32 -13
- 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/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs7.d.ts +14 -3
- package/dist/pkcs/pkcs7.js +268 -248
- 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.d.ts +6 -0
- package/dist/revocation/chain.d.ts +2 -3
- package/dist/revocation/chain.js +52 -52
- package/dist/revocation/crl.d.ts +2 -3
- package/dist/revocation/crl.js +319 -319
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +2 -3
- package/dist/revocation/ocsp.js +395 -395
- package/dist/revocation/revocation.d.ts +2 -3
- package/dist/revocation/revocation.js +22 -22
- package/dist/revocation.d.ts +12 -3
- 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.d.ts +8 -0
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +31 -31
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +3 -4
- package/dist/verify/verify.js +121 -121
- package/dist/verify.d.ts +9 -0
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- 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/name-text.d.ts +1 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/parse.d.ts +3 -4
- package/dist/x509/parse.js +423 -423
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +27 -34
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/pkcs/pkcs7.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
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 {
|
|
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 { getVerifySignatureConfigResult as S, verifySignedDataDetailed as C } from "../internal/crypto/sig-verify.js";
|
|
7
|
+
import { encodeAlgorithmIdentifier as w, getSignatureAlgorithm as T, signBytes as E } from "../internal/crypto/signing.js";
|
|
8
|
+
import { getCrypto as D } from "../internal/crypto/webcrypto.js";
|
|
9
|
+
import { base64Encode as O } from "../internal/shared/base64.js";
|
|
10
|
+
import { compareDistinguishedNames as k } from "../internal/shared/dn.js";
|
|
11
|
+
import { pemEncode as A, splitPemBlocksOrThrow as j } from "../pem/pem.js";
|
|
12
|
+
import { DECODE_REFUSAL_CODES as M, failureResult as N, rethrowIfInvariant as P } from "../result/result.js";
|
|
13
|
+
import { nameFieldKeyFromOid as F } from "../internal/x509/name-fields.js";
|
|
14
14
|
import { parseCertificateDerOrThrow as I } from "../x509/parse.js";
|
|
15
15
|
// src/pkcs/pkcs7.ts
|
|
16
16
|
/**
|
|
@@ -32,30 +32,30 @@ const L = ["PKCS7", "CMS"];
|
|
|
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) I(e);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
let t = c(e, M);
|
|
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: A("PKCS7", i),
|
|
58
|
+
base64: O(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 = e.content.slice(), 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)) I(e), addCertificate(e);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
let t = c(e, M);
|
|
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: A(u === 1 ? "PKCS7" : "CMS", g),
|
|
113
|
+
base64: O(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 = I(a);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
let t = c(e, M);
|
|
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 = T(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 (!S(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 D().subtle.digest(m.hashName, o(t)))), v;
|
|
149
149
|
try {
|
|
150
|
-
|
|
150
|
+
v = await E(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
|
+
w(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 = j(e).filter((e) => L.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
|
+
P(e);
|
|
221
|
+
let t = c(e, M);
|
|
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,40 @@ function parsePkcs7SignedDataPem(e) {
|
|
|
273
277
|
* ```
|
|
274
278
|
*/
|
|
275
279
|
async function verifyPkcs7SignedData(e, t = {}) {
|
|
276
|
-
let
|
|
277
|
-
if (!
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
280
|
+
let r, i = t.signedAttributes ?? "required";
|
|
281
|
+
if (![
|
|
282
|
+
"required",
|
|
283
|
+
"allowed",
|
|
284
|
+
"forbidden"
|
|
285
|
+
].includes(i)) return verifyPkcs7Failure("invalid_options", "Unknown signed-attribute policy");
|
|
286
|
+
try {
|
|
287
|
+
r = n(m(f(t.expectedContentTypeOid ?? y.pkcs7Data)).value);
|
|
288
|
+
} catch {
|
|
289
|
+
return verifyPkcs7Failure("invalid_options", "Expected content type must be an OID");
|
|
290
|
+
}
|
|
291
|
+
let a = normalizePkcs7SignedDataForVerification(e);
|
|
292
|
+
if (!a.ok) return a;
|
|
293
|
+
let o = a.value, s = o.encapsulatedContent ?? t.content?.slice();
|
|
294
|
+
if (s === void 0) return verifyPkcs7Failure("detached_content_required", "SignedData has no encapsulated content; supply the detached content via options.content");
|
|
295
|
+
if (o.signerInfos.length === 0) return verifyPkcs7Failure("no_signers", "SignedData contains no signer information");
|
|
296
|
+
if (o.encapsulatedContentTypeOid !== r) return verifyPkcs7Failure("content_type_mismatch", "Unexpected SignedData content type");
|
|
297
|
+
for (let e of o.signerInfos) {
|
|
298
|
+
if (!e.hasSignedAttrs && (i === "required" || o.encapsulatedContentTypeOid !== y.pkcs7Data)) return verifyPkcs7Failure("signed_attributes_required", "Signed attributes are required");
|
|
299
|
+
if (e.hasSignedAttrs && i === "forbidden") return verifyPkcs7Failure("signed_attributes_forbidden", "Signed attributes are forbidden");
|
|
300
|
+
}
|
|
301
|
+
let c = [];
|
|
302
|
+
for (let e of o.signerInfos) {
|
|
303
|
+
let t = await verifyPkcs7SignerInfo(o, e, s);
|
|
284
304
|
if (!t.ok) return t;
|
|
285
|
-
|
|
305
|
+
c.push({
|
|
286
306
|
signerInfo: e,
|
|
287
307
|
certificate: t.certificate
|
|
288
308
|
});
|
|
289
309
|
}
|
|
290
310
|
return {
|
|
291
311
|
ok: !0,
|
|
292
|
-
value:
|
|
293
|
-
signers:
|
|
312
|
+
value: o,
|
|
313
|
+
signers: c
|
|
294
314
|
};
|
|
295
315
|
}
|
|
296
316
|
/** Normalizes encoded or parsed SignedData into a freshly parsed verification input. */
|
|
@@ -309,37 +329,37 @@ async function verifyPkcs7SignerInfo(e, t, n) {
|
|
|
309
329
|
}
|
|
310
330
|
async function verifyPkcs7BareSignerInfo(e, t, n) {
|
|
311
331
|
try {
|
|
312
|
-
let r = await
|
|
332
|
+
let r = await C(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, n);
|
|
313
333
|
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");
|
|
314
334
|
} catch (e) {
|
|
315
335
|
return signatureVerificationThrowFailure(e);
|
|
316
336
|
}
|
|
317
337
|
}
|
|
318
338
|
/** Shorthand for constructing a PKCS#7 parse failure result. */
|
|
319
|
-
function pkcs7Failure(e,
|
|
320
|
-
return
|
|
339
|
+
function pkcs7Failure(e, t) {
|
|
340
|
+
return N(e, t);
|
|
321
341
|
}
|
|
322
342
|
/** Shorthand for constructing a PKCS#7 verification failure result. */
|
|
323
343
|
/** A signer's signature verification that threw: its decode refusal, else `malformed`. */
|
|
324
|
-
function signatureVerificationThrowFailure(
|
|
325
|
-
let
|
|
326
|
-
return
|
|
344
|
+
function signatureVerificationThrowFailure(e) {
|
|
345
|
+
let t = c(e, M);
|
|
346
|
+
return t === void 0 ? verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData") : verifyPkcs7Failure(t.code, t.message);
|
|
327
347
|
}
|
|
328
|
-
function verifyPkcs7Failure(e,
|
|
329
|
-
return
|
|
348
|
+
function verifyPkcs7Failure(e, t) {
|
|
349
|
+
return N(e, t);
|
|
330
350
|
}
|
|
331
351
|
/** Shorthand for constructing a PKCS#7 certificate bag creation failure result. */
|
|
332
|
-
function createCertBagFailure(e,
|
|
333
|
-
return
|
|
352
|
+
function createCertBagFailure(e, t) {
|
|
353
|
+
return N(e, t);
|
|
334
354
|
}
|
|
335
355
|
/** Shorthand for constructing a PKCS#7 SignedData creation failure result. */
|
|
336
|
-
function createPkcs7Failure(e,
|
|
337
|
-
return
|
|
356
|
+
function createPkcs7Failure(e, t) {
|
|
357
|
+
return N(e, t);
|
|
338
358
|
}
|
|
339
359
|
/** Converts PEM text to an array of DER certificate blobs, or wraps raw DER. */
|
|
340
360
|
function normalizeCertificateSource(e) {
|
|
341
361
|
if (typeof e == "string") {
|
|
342
|
-
let t =
|
|
362
|
+
let t = j(e).filter((e) => e.label === "CERTIFICATE").map((e) => new Uint8Array(e.bytes));
|
|
343
363
|
if (t.length === 0) throw Error("Certificate PEM required");
|
|
344
364
|
return t;
|
|
345
365
|
}
|
|
@@ -368,15 +388,15 @@ function contentDigestForPrivateKey(e) {
|
|
|
368
388
|
switch (t.namedCurve) {
|
|
369
389
|
case "P-256": return {
|
|
370
390
|
hashName: "SHA-256",
|
|
371
|
-
digestOid:
|
|
391
|
+
digestOid: y.sha256
|
|
372
392
|
};
|
|
373
393
|
case "P-384": return {
|
|
374
394
|
hashName: "SHA-384",
|
|
375
|
-
digestOid:
|
|
395
|
+
digestOid: y.sha384
|
|
376
396
|
};
|
|
377
397
|
case "P-521": return {
|
|
378
398
|
hashName: "SHA-512",
|
|
379
|
-
digestOid:
|
|
399
|
+
digestOid: y.sha512
|
|
380
400
|
};
|
|
381
401
|
default: return;
|
|
382
402
|
}
|
|
@@ -386,22 +406,22 @@ function contentDigestForPrivateKey(e) {
|
|
|
386
406
|
switch (t.hash.name) {
|
|
387
407
|
case "SHA-256": return {
|
|
388
408
|
hashName: "SHA-256",
|
|
389
|
-
digestOid:
|
|
409
|
+
digestOid: y.sha256
|
|
390
410
|
};
|
|
391
411
|
case "SHA-384": return {
|
|
392
412
|
hashName: "SHA-384",
|
|
393
|
-
digestOid:
|
|
413
|
+
digestOid: y.sha384
|
|
394
414
|
};
|
|
395
415
|
case "SHA-512": return {
|
|
396
416
|
hashName: "SHA-512",
|
|
397
|
-
digestOid:
|
|
417
|
+
digestOid: y.sha512
|
|
398
418
|
};
|
|
399
419
|
default: return;
|
|
400
420
|
}
|
|
401
421
|
}
|
|
402
422
|
if (t.name === "Ed25519") return {
|
|
403
423
|
hashName: "SHA-512",
|
|
404
|
-
digestOid:
|
|
424
|
+
digestOid: y.sha512
|
|
405
425
|
};
|
|
406
426
|
}
|
|
407
427
|
/**
|
|
@@ -411,7 +431,7 @@ function contentDigestForPrivateKey(e) {
|
|
|
411
431
|
* SignerInfo. The two differ only in the leading tag byte.
|
|
412
432
|
*/
|
|
413
433
|
function buildSignedAttributes(e, t) {
|
|
414
|
-
let n =
|
|
434
|
+
let n = v([_([f(y.cmsContentType), v([f(e)])]), _([f(y.cmsMessageDigest), v([p(t)])])]), r = new Uint8Array(n);
|
|
415
435
|
return r[0] = 160, {
|
|
416
436
|
setForSigning: n,
|
|
417
437
|
implicitForEmit: r
|
|
@@ -427,10 +447,10 @@ function x509CertificatesOf(e) {
|
|
|
427
447
|
* The IMPLICIT tag replaces the SEQUENCE tag, so the OID and the `ANY DEFINED BY`
|
|
428
448
|
* value are direct children of the `[3]` element.
|
|
429
449
|
*/
|
|
430
|
-
function parseOtherCertificateFormatOid(
|
|
431
|
-
let
|
|
432
|
-
if (
|
|
433
|
-
return
|
|
450
|
+
function parseOtherCertificateFormatOid(t, r) {
|
|
451
|
+
let i = a(e(t, r)[0], "otherCertFormat");
|
|
452
|
+
if (i.tag !== 6) throw Error("otherCertFormat must be an OBJECT IDENTIFIER");
|
|
453
|
+
return n(i.value);
|
|
434
454
|
}
|
|
435
455
|
/** Decodes one RFC 5652 §10.2.2 CertificateChoices alternative. */
|
|
436
456
|
function parseCertificateChoice(e, t, n) {
|
|
@@ -463,118 +483,118 @@ function parseCertificateChoice(e, t, n) {
|
|
|
463
483
|
* Parses the IMPLICIT [0] certificate set from a SignedData structure as
|
|
464
484
|
* RFC 5652 §10.2.3 `CertificateSet ::= SET OF CertificateChoices`.
|
|
465
485
|
*/
|
|
466
|
-
function parseCertificateSet(
|
|
467
|
-
return
|
|
486
|
+
function parseCertificateSet(t, n) {
|
|
487
|
+
return n === void 0 || n.tag !== 160 ? [] : e(t, n).map((e) => parseCertificateChoice(t, e, t.slice(e.start - e.headerLength, e.end)));
|
|
468
488
|
}
|
|
469
489
|
/** 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
|
-
|
|
490
|
+
function parseDigestAlgorithms(t, r) {
|
|
491
|
+
let i = [];
|
|
492
|
+
if (r.tag !== 49) throw Error("digestAlgorithms must use SET");
|
|
493
|
+
for (let a of e(t, r)) {
|
|
494
|
+
if (a.tag !== 48) throw Error("digestAlgorithm must use AlgorithmIdentifier SEQUENCE");
|
|
495
|
+
let e = t.slice(a.start - a.headerLength, a.end), r = g(e), o = r[0];
|
|
496
|
+
if (o === void 0 || r.length < 1 || r.length > 2 || o.tag !== 6) throw Error("Malformed digest AlgorithmIdentifier");
|
|
497
|
+
i.push(n(o.value));
|
|
478
498
|
}
|
|
479
|
-
return
|
|
499
|
+
return i;
|
|
480
500
|
}
|
|
481
501
|
/** 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
|
|
502
|
+
function parseSignerInfos(t, n) {
|
|
503
|
+
return e(t, n).map((e) => parseSignerInfo(t, e));
|
|
504
|
+
}
|
|
505
|
+
function parseSignerInfo(e, r) {
|
|
506
|
+
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;
|
|
507
|
+
f !== void 0 && (d += 1);
|
|
508
|
+
let p = o[d], m = o[d + 1], h = o[d + 2];
|
|
509
|
+
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");
|
|
510
|
+
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
511
|
return {
|
|
492
|
-
version:
|
|
493
|
-
signerIdentifier: parseSignerIdentifier(
|
|
494
|
-
digestAlgorithmOid:
|
|
495
|
-
digestAlgorithmName:
|
|
496
|
-
signatureAlgorithmOid:
|
|
497
|
-
signatureAlgorithmName:
|
|
498
|
-
...
|
|
499
|
-
signatureHex:
|
|
500
|
-
signature: new Uint8Array(
|
|
501
|
-
...
|
|
512
|
+
version: t(c.value),
|
|
513
|
+
signerIdentifier: parseSignerIdentifier(i.slice(l.start - l.headerLength, l.end)),
|
|
514
|
+
digestAlgorithmOid: _,
|
|
515
|
+
digestAlgorithmName: b(_),
|
|
516
|
+
signatureAlgorithmOid: S,
|
|
517
|
+
signatureAlgorithmName: x(S, C === void 0 ? void 0 : new Uint8Array(v.slice(C.start - C.headerLength, C.end))),
|
|
518
|
+
...C === void 0 ? {} : { signatureAlgorithmParametersDer: new Uint8Array(v.slice(C.start - C.headerLength, C.end)) },
|
|
519
|
+
signatureHex: s(m.value),
|
|
520
|
+
signature: new Uint8Array(m.value),
|
|
521
|
+
...f === void 0 ? { hasSignedAttrs: !1 } : {
|
|
502
522
|
hasSignedAttrs: !0,
|
|
503
|
-
signedAttrsDer: new Uint8Array(
|
|
523
|
+
signedAttrsDer: new Uint8Array(i.slice(f.start - f.headerLength, f.end))
|
|
504
524
|
}
|
|
505
525
|
};
|
|
506
526
|
}
|
|
507
|
-
function parsePkcs7AlgorithmOid(e, t,
|
|
508
|
-
let
|
|
509
|
-
return
|
|
527
|
+
function parsePkcs7AlgorithmOid(e, t, r) {
|
|
528
|
+
let i = e.slice(t.start - t.headerLength, t.end);
|
|
529
|
+
return n(a(g(i)[0], r).value);
|
|
510
530
|
}
|
|
511
531
|
/** Unwraps the OCTET STRING inside the IMPLICIT [0] encapsulated content. */
|
|
512
532
|
function extractEncapsulatedContent(e, t) {
|
|
513
533
|
if (t.tag !== 160) throw Error("Unexpected encapsulated content tag");
|
|
514
|
-
let n =
|
|
534
|
+
let n = m(e, t.start);
|
|
515
535
|
if (n.tag !== 4) throw Error("Expected encapsulated OCTET STRING");
|
|
516
536
|
if (n.end !== t.end) throw Error("Encapsulated content must hold exactly one element");
|
|
517
537
|
return n.value;
|
|
518
538
|
}
|
|
519
539
|
/** Extracts issuer Name and serial number from an issuerAndSerialNumber SEQUENCE, or subjectKeyIdentifier from [0] IMPLICIT. */
|
|
520
540
|
function parseSignerIdentifier(e) {
|
|
521
|
-
let t =
|
|
541
|
+
let t = h(e, { maxDepth: 64 });
|
|
522
542
|
if (t.tag === 128) {
|
|
523
543
|
if (t.value.length === 0) throw Error("SignerIdentifier subjectKeyIdentifier must not be empty");
|
|
524
544
|
return {
|
|
525
545
|
type: "subjectKeyIdentifier",
|
|
526
|
-
subjectKeyIdentifier:
|
|
546
|
+
subjectKeyIdentifier: s(t.value)
|
|
527
547
|
};
|
|
528
548
|
}
|
|
529
549
|
if (t.tag === 48) {
|
|
530
|
-
let t =
|
|
550
|
+
let t = g(e), n = t[0], r = t[1];
|
|
531
551
|
if (n === void 0 || r === void 0 || t.length !== 2) throw Error("SignerIdentifier issuerAndSerialNumber is malformed");
|
|
532
552
|
if (n.tag !== 48) throw Error("SignerIdentifier issuer must use Name SEQUENCE");
|
|
533
553
|
if (r.tag !== 2) throw Error("SignerIdentifier serialNumber must use INTEGER");
|
|
534
554
|
return assertImplicitSerialNumberEncoding(r.value, "SignerIdentifier serialNumber"), {
|
|
535
555
|
type: "issuerAndSerialNumber",
|
|
536
556
|
issuer: parseSignerIssuerName(e, n),
|
|
537
|
-
serialNumberHex:
|
|
557
|
+
serialNumberHex: s(r.value)
|
|
538
558
|
};
|
|
539
559
|
}
|
|
540
560
|
throw Error(`Unsupported SignerIdentifier tag: ${String(t.tag)}`);
|
|
541
561
|
}
|
|
542
562
|
/** 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:
|
|
563
|
+
function parseSignerIssuerName(t, i) {
|
|
564
|
+
let o = s(t.slice(i.start - i.headerLength, i.end)), c = [], l = [], u = {};
|
|
565
|
+
for (let o of e(t, i)) {
|
|
566
|
+
let i = [], d = {};
|
|
567
|
+
for (let s of e(t, o)) {
|
|
568
|
+
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 = F(p), g = h === void 0 ? {
|
|
569
|
+
oid: p,
|
|
570
|
+
valueTag: f.tag,
|
|
571
|
+
value: m
|
|
552
572
|
} : {
|
|
553
|
-
oid:
|
|
554
|
-
key:
|
|
555
|
-
valueTag:
|
|
556
|
-
value:
|
|
573
|
+
oid: p,
|
|
574
|
+
key: h,
|
|
575
|
+
valueTag: f.tag,
|
|
576
|
+
value: m
|
|
557
577
|
};
|
|
558
|
-
|
|
578
|
+
i.push(g), l.push(g), h !== void 0 && (d[h] === void 0 && (d[h] = m), u[h] === void 0 && (u[h] = m));
|
|
559
579
|
}
|
|
560
|
-
|
|
561
|
-
derHex:
|
|
562
|
-
attributes:
|
|
563
|
-
values:
|
|
580
|
+
c.push({
|
|
581
|
+
derHex: s(t.slice(o.start - o.headerLength, o.end)),
|
|
582
|
+
attributes: i,
|
|
583
|
+
values: d
|
|
564
584
|
});
|
|
565
585
|
}
|
|
566
586
|
return {
|
|
567
|
-
derHex:
|
|
568
|
-
rdns:
|
|
569
|
-
attributes:
|
|
570
|
-
values:
|
|
587
|
+
derHex: o,
|
|
588
|
+
rdns: c,
|
|
589
|
+
attributes: l,
|
|
590
|
+
values: u
|
|
571
591
|
};
|
|
572
592
|
}
|
|
573
593
|
/** Returns the nth child element inside a constructed ASN.1 element, or throws. */
|
|
574
594
|
function childAt(e, t, n, r) {
|
|
575
595
|
let i = t.start, a = 0;
|
|
576
596
|
for (; i < t.end;) {
|
|
577
|
-
let t =
|
|
597
|
+
let t = m(e, i);
|
|
578
598
|
if (a === n) return t;
|
|
579
599
|
i = t.end, a += 1;
|
|
580
600
|
}
|
|
@@ -583,40 +603,40 @@ function childAt(e, t, n, r) {
|
|
|
583
603
|
/** Maps a digest algorithm OID to the WebCrypto hash name. */
|
|
584
604
|
function digestAlgorithmHash(e) {
|
|
585
605
|
switch (e) {
|
|
586
|
-
case
|
|
587
|
-
case
|
|
588
|
-
case
|
|
606
|
+
case y.sha256: return "SHA-256";
|
|
607
|
+
case y.sha384: return "SHA-384";
|
|
608
|
+
case y.sha512: return "SHA-512";
|
|
589
609
|
default: throw Error(`Unsupported digest algorithm OID: ${e}`);
|
|
590
610
|
}
|
|
591
611
|
}
|
|
592
612
|
/** Parses required signed attributes and enforces uniqueness + exact-one-value shape. */
|
|
593
|
-
function parseSignedAttributeRequirements(
|
|
594
|
-
let
|
|
595
|
-
for (let
|
|
596
|
-
if (
|
|
597
|
-
if (
|
|
613
|
+
function parseSignedAttributeRequirements(t) {
|
|
614
|
+
let n = m(t), r = {};
|
|
615
|
+
for (let i of e(t, n)) parseSignedAttributeRequirement(t, i, r);
|
|
616
|
+
if (r.messageDigest === void 0) throw Error("Missing messageDigest attribute in signedAttrs");
|
|
617
|
+
if (r.contentTypeOid === void 0) throw Error("Missing contentType attribute in signedAttrs");
|
|
598
618
|
return {
|
|
599
|
-
messageDigest:
|
|
600
|
-
contentTypeOid:
|
|
619
|
+
messageDigest: r.messageDigest,
|
|
620
|
+
contentTypeOid: r.contentTypeOid
|
|
601
621
|
};
|
|
602
622
|
}
|
|
603
|
-
function parseSignedAttributeRequirement(
|
|
604
|
-
let
|
|
605
|
-
if (
|
|
606
|
-
let
|
|
607
|
-
|
|
623
|
+
function parseSignedAttributeRequirement(t, r, i) {
|
|
624
|
+
let a = t.slice(r.start - r.headerLength, r.end), o = g(a), s = o[0], c = o[1];
|
|
625
|
+
if (s === void 0 || c === void 0 || o.length !== 2 || c.tag !== 49) throw Error("Malformed signedAttrs attribute");
|
|
626
|
+
let l = n(s.value), u = e(a, c);
|
|
627
|
+
l === y.cmsMessageDigest && (i.messageDigest = parseUniqueSignedAttributeOctetString(i.messageDigest, u, "messageDigest")), l === y.cmsContentType && (i.contentTypeOid = parseUniqueSignedAttributeOid(i.contentTypeOid, u, "contentType"));
|
|
608
628
|
}
|
|
609
629
|
function parseUniqueSignedAttributeOctetString(e, t, n) {
|
|
610
630
|
if (e !== void 0 || t.length !== 1) throw Error(`${n} attribute must appear exactly once with one value`);
|
|
611
|
-
let r =
|
|
631
|
+
let r = a(t[0], `${n} attribute value`);
|
|
612
632
|
if (r.tag !== 4) throw Error(`${n} attribute value must use OCTET STRING`);
|
|
613
633
|
return r.value;
|
|
614
634
|
}
|
|
615
|
-
function parseUniqueSignedAttributeOid(e, t,
|
|
616
|
-
if (e !== void 0 || t.length !== 1) throw Error(`${
|
|
617
|
-
let
|
|
618
|
-
if (
|
|
619
|
-
return
|
|
635
|
+
function parseUniqueSignedAttributeOid(e, t, r) {
|
|
636
|
+
if (e !== void 0 || t.length !== 1) throw Error(`${r} attribute must appear exactly once with one value`);
|
|
637
|
+
let i = a(t[0], `${r} attribute value`);
|
|
638
|
+
if (i.tag !== 6) throw Error(`${r} attribute value must use OBJECT IDENTIFIER`);
|
|
639
|
+
return n(i.value);
|
|
620
640
|
}
|
|
621
641
|
/** Replaces the IMPLICIT [0] tag (0xa0) with SET OF (0x31) per RFC 5652 Section 5.4. */
|
|
622
642
|
function retagSignedAttrsAsSet(e) {
|
|
@@ -631,47 +651,47 @@ function constantTimeEqual(e, t) {
|
|
|
631
651
|
return n === 0;
|
|
632
652
|
}
|
|
633
653
|
/** Verifies a signed-attributes flow: digest match + signature over re-tagged attrs. */
|
|
634
|
-
async function verifySignedAttrs(
|
|
635
|
-
if (!
|
|
636
|
-
let
|
|
654
|
+
async function verifySignedAttrs(e, t, n, r) {
|
|
655
|
+
if (!e.hasSignedAttrs) return verifyPkcs7Failure("malformed", "Missing signedAttrs DER");
|
|
656
|
+
let i;
|
|
637
657
|
try {
|
|
638
|
-
assertImplicitSignedAttrsDer(
|
|
639
|
-
} catch (
|
|
640
|
-
|
|
641
|
-
let
|
|
642
|
-
return
|
|
658
|
+
assertImplicitSignedAttrsDer(e.signedAttrsDer), i = parseSignedAttributeRequirements(e.signedAttrsDer);
|
|
659
|
+
} catch (e) {
|
|
660
|
+
P(e);
|
|
661
|
+
let t = c(e, M);
|
|
662
|
+
return t === void 0 ? verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData") : verifyPkcs7Failure(t.code, t.message);
|
|
643
663
|
}
|
|
644
|
-
if (
|
|
645
|
-
let
|
|
664
|
+
if (i.contentTypeOid !== r) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
|
|
665
|
+
let a;
|
|
646
666
|
try {
|
|
647
|
-
let
|
|
648
|
-
|
|
667
|
+
let t = digestAlgorithmHash(e.digestAlgorithmOid);
|
|
668
|
+
a = new Uint8Array(await D().subtle.digest(t, o(n)));
|
|
649
669
|
} catch {
|
|
650
670
|
return verifyPkcs7Failure("malformed", "Unsupported digest algorithm in SignedData");
|
|
651
671
|
}
|
|
652
|
-
if (!constantTimeEqual(
|
|
653
|
-
let
|
|
672
|
+
if (!constantTimeEqual(a, i.messageDigest)) return verifyPkcs7Failure("message_digest_mismatch", "Content digest does not match messageDigest attribute");
|
|
673
|
+
let s;
|
|
654
674
|
try {
|
|
655
|
-
|
|
675
|
+
s = retagSignedAttrsAsSet(e.signedAttrsDer);
|
|
656
676
|
} catch {
|
|
657
677
|
return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
|
|
658
678
|
}
|
|
659
|
-
let
|
|
679
|
+
let l;
|
|
660
680
|
try {
|
|
661
|
-
let
|
|
662
|
-
if (!
|
|
663
|
-
|
|
681
|
+
let n = await C(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, s);
|
|
682
|
+
if (!n.ok) return n.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
|
|
683
|
+
l = n.valid;
|
|
664
684
|
} catch (e) {
|
|
665
685
|
return signatureVerificationThrowFailure(e);
|
|
666
686
|
}
|
|
667
|
-
return
|
|
687
|
+
return l ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature over signedAttrs does not verify");
|
|
668
688
|
}
|
|
669
689
|
function signerIdentifierMatches(e, t) {
|
|
670
690
|
let n = t.signerIdentifier;
|
|
671
|
-
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex &&
|
|
691
|
+
return n.type === "issuerAndSerialNumber" ? e.serialNumberHex === n.serialNumberHex && k(e.issuer, n.issuer) : e.subjectKeyIdentifier === n.subjectKeyIdentifier;
|
|
672
692
|
}
|
|
673
693
|
function assertImplicitSignedAttrsDer(e) {
|
|
674
|
-
if (
|
|
694
|
+
if (m(e).tag !== 160) throw Error("signedAttrs must use IMPLICIT [0] tag");
|
|
675
695
|
}
|
|
676
696
|
function hasReparseablePkcs7SignedData(e) {
|
|
677
697
|
return "der" in e && e.der instanceof Uint8Array;
|