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.
Files changed (165) hide show
  1. package/CHANGELOG.md +2064 -0
  2. package/README.md +111 -183
  3. package/dist/crypto/crypto.d.ts +104 -0
  4. package/dist/crypto/crypto.js +105 -0
  5. package/dist/crypto/crypto.js.map +1 -0
  6. package/dist/crypto.d.ts +4 -0
  7. package/dist/crypto.js +1 -0
  8. package/dist/der/der.d.ts +41 -33
  9. package/dist/der/der.js +26 -24
  10. package/dist/der/der.js.map +1 -1
  11. package/dist/index.d.ts +18 -14
  12. package/dist/index.js +19 -17
  13. package/dist/internal/asn1/asn1.d.ts +3 -6
  14. package/dist/internal/asn1/asn1.js +355 -44
  15. package/dist/internal/asn1/asn1.js.map +1 -1
  16. package/dist/internal/asn1/ber.js +146 -0
  17. package/dist/internal/asn1/ber.js.map +1 -0
  18. package/dist/internal/asn1/decode-refusal.js +29 -0
  19. package/dist/internal/asn1/decode-refusal.js.map +1 -0
  20. package/dist/internal/asn1/der.d.ts +42 -43
  21. package/dist/internal/asn1/der.js +148 -56
  22. package/dist/internal/asn1/der.js.map +1 -1
  23. package/dist/internal/asn1/oids.js +90 -54
  24. package/dist/internal/asn1/oids.js.map +1 -1
  25. package/dist/internal/asn1/oids2.js +1 -2
  26. package/dist/internal/asn1/oids2.js.map +1 -1
  27. package/dist/internal/crypto/algorithm-names.js +5 -2
  28. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  29. package/dist/internal/crypto/ecdsa.js +13 -8
  30. package/dist/internal/crypto/ecdsa.js.map +1 -1
  31. package/dist/internal/crypto/hash.js +1 -2
  32. package/dist/internal/crypto/hash.js.map +1 -1
  33. package/dist/internal/crypto/pbes2.d.ts +51 -0
  34. package/dist/internal/crypto/pbes2.js +86 -34
  35. package/dist/internal/crypto/pbes2.js.map +1 -1
  36. package/dist/internal/crypto/rsa-pss.js +48 -44
  37. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  38. package/dist/internal/crypto/sig-verify.d.ts +28 -0
  39. package/dist/internal/crypto/sig-verify.js +111 -62
  40. package/dist/internal/crypto/sig-verify.js.map +1 -1
  41. package/dist/internal/crypto/signing.d.ts +1 -4
  42. package/dist/internal/crypto/signing.js +28 -29
  43. package/dist/internal/crypto/signing.js.map +1 -1
  44. package/dist/internal/crypto/webcrypto.js +1 -2
  45. package/dist/internal/crypto/webcrypto.js.map +1 -1
  46. package/dist/internal/shared/base64.js +8 -3
  47. package/dist/internal/shared/base64.js.map +1 -1
  48. package/dist/internal/shared/dn.d.ts +13 -0
  49. package/dist/internal/shared/dn.js +101 -48
  50. package/dist/internal/shared/dn.js.map +1 -1
  51. package/dist/internal/shared/idna-tables.js +7217 -0
  52. package/dist/internal/shared/idna-tables.js.map +1 -0
  53. package/dist/internal/shared/idna.js +296 -0
  54. package/dist/internal/shared/idna.js.map +1 -0
  55. package/dist/internal/shared/ip.d.ts +66 -0
  56. package/dist/internal/shared/ip.js +19 -3
  57. package/dist/internal/shared/ip.js.map +1 -1
  58. package/dist/internal/shared/mailbox.js +26 -0
  59. package/dist/internal/shared/mailbox.js.map +1 -0
  60. package/dist/internal/shared/punycode.js +101 -0
  61. package/dist/internal/shared/punycode.js.map +1 -0
  62. package/dist/internal/shared/rfc3454-tables.js +1 -2
  63. package/dist/internal/shared/rfc3454-tables.js.map +1 -1
  64. package/dist/internal/shared/rfc4518-tables.js +119 -0
  65. package/dist/internal/shared/rfc4518-tables.js.map +1 -0
  66. package/dist/internal/shared/uri-host.js +102 -0
  67. package/dist/internal/shared/uri-host.js.map +1 -0
  68. package/dist/internal/verify/name-constraints-engine.js +246 -131
  69. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  70. package/dist/internal/verify/policy-engine.js +6 -21
  71. package/dist/internal/verify/policy-engine.js.map +1 -1
  72. package/dist/internal/verify/verify-path.d.ts +4 -0
  73. package/dist/internal/verify/verify-path.js +264 -128
  74. package/dist/internal/verify/verify-path.js.map +1 -1
  75. package/dist/internal/x509/directory-name.js +23 -0
  76. package/dist/internal/x509/directory-name.js.map +1 -0
  77. package/dist/internal/x509/extension-bits.js +15 -13
  78. package/dist/internal/x509/extension-bits.js.map +1 -1
  79. package/dist/internal/x509/extension-errors.d.ts +3 -0
  80. package/dist/internal/x509/extension-errors.js +19 -0
  81. package/dist/internal/x509/extension-errors.js.map +1 -0
  82. package/dist/internal/x509/extension-registry.js +157 -81
  83. package/dist/internal/x509/extension-registry.js.map +1 -1
  84. package/dist/internal/x509/general-name-profile.js +129 -0
  85. package/dist/internal/x509/general-name-profile.js.map +1 -0
  86. package/dist/internal/x509/general-name-tags.js +27 -0
  87. package/dist/internal/x509/general-name-tags.js.map +1 -0
  88. package/dist/internal/x509/general-name.js +183 -0
  89. package/dist/internal/x509/general-name.js.map +1 -0
  90. package/dist/internal/x509/name-fields.js +21 -12
  91. package/dist/internal/x509/name-fields.js.map +1 -1
  92. package/dist/internal/x509/or-address.js +217 -0
  93. package/dist/internal/x509/or-address.js.map +1 -0
  94. package/dist/keys/keys.d.ts +124 -95
  95. package/dist/keys/keys.js +362 -214
  96. package/dist/keys/keys.js.map +1 -1
  97. package/dist/keys.d.ts +3 -2
  98. package/dist/keys.js +3 -2
  99. package/dist/pem/pem.d.ts +17 -19
  100. package/dist/pem/pem.js +122 -41
  101. package/dist/pem/pem.js.map +1 -1
  102. package/dist/pem.js +1 -2
  103. package/dist/pkcs/pfx.d.ts +57 -32
  104. package/dist/pkcs/pfx.js +209 -173
  105. package/dist/pkcs/pfx.js.map +1 -1
  106. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  107. package/dist/pkcs/pkcs12-mac.js +266 -79
  108. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  109. package/dist/pkcs/pkcs7.d.ts +123 -53
  110. package/dist/pkcs/pkcs7.js +314 -238
  111. package/dist/pkcs/pkcs7.js.map +1 -1
  112. package/dist/pkcs.d.ts +5 -4
  113. package/dist/result/result.d.ts +43 -28
  114. package/dist/result/result.js +27 -14
  115. package/dist/result/result.js.map +1 -1
  116. package/dist/result.d.ts +2 -2
  117. package/dist/result.js +1 -2
  118. package/dist/revocation/chain.d.ts +101 -23
  119. package/dist/revocation/chain.js +456 -206
  120. package/dist/revocation/chain.js.map +1 -1
  121. package/dist/revocation/crl.d.ts +140 -65
  122. package/dist/revocation/crl.js +691 -391
  123. package/dist/revocation/crl.js.map +1 -1
  124. package/dist/revocation/ocsp.d.ts +145 -69
  125. package/dist/revocation/ocsp.js +347 -297
  126. package/dist/revocation/ocsp.js.map +1 -1
  127. package/dist/revocation/revocation.d.ts +48 -36
  128. package/dist/revocation/revocation.js +112 -63
  129. package/dist/revocation/revocation.js.map +1 -1
  130. package/dist/revocation.d.ts +5 -5
  131. package/dist/revocation.js +5 -5
  132. package/dist/verify/identity.d.ts +18 -20
  133. package/dist/verify/identity.js +120 -90
  134. package/dist/verify/identity.js.map +1 -1
  135. package/dist/verify/name-constraints.d.ts +1 -4
  136. package/dist/verify/policy.d.ts +15 -8
  137. package/dist/verify/verify.d.ts +86 -46
  138. package/dist/verify/verify.js +309 -143
  139. package/dist/verify/verify.js.map +1 -1
  140. package/dist/verify.d.ts +3 -2
  141. package/dist/verify.js +4 -3
  142. package/dist/x509/certificate.d.ts +50 -28
  143. package/dist/x509/certificate.js +69 -36
  144. package/dist/x509/certificate.js.map +1 -1
  145. package/dist/x509/csr.d.ts +9 -8
  146. package/dist/x509/csr.js +10 -11
  147. package/dist/x509/csr.js.map +1 -1
  148. package/dist/x509/extensions.d.ts +207 -82
  149. package/dist/x509/extensions.js +561 -128
  150. package/dist/x509/extensions.js.map +1 -1
  151. package/dist/x509/fingerprint.d.ts +4 -7
  152. package/dist/x509/fingerprint.js +1 -2
  153. package/dist/x509/fingerprint.js.map +1 -1
  154. package/dist/x509/name-text.d.ts +7 -9
  155. package/dist/x509/name-text.js +20 -4
  156. package/dist/x509/name-text.js.map +1 -1
  157. package/dist/x509/name.d.ts +15 -14
  158. package/dist/x509/name.js +23 -19
  159. package/dist/x509/name.js.map +1 -1
  160. package/dist/x509/parse.d.ts +165 -64
  161. package/dist/x509/parse.js +444 -459
  162. package/dist/x509/parse.js.map +1 -1
  163. package/dist/x509.d.ts +8 -5
  164. package/dist/x509.js +10 -8
  165. package/package.json +90 -65
@@ -1,51 +1,61 @@
1
- import { concatBytes as e, explicitContext as t, integer as n, integerFromNumber as r, nullValue as i, objectIdentifier as a, octetString as o, readElement as s, readRootElement as c, readSequenceChildren as l, sequence as u, setOf as d } from "../internal/asn1/der.js";
2
- import { childrenOf as f, decodeIntegerNumber as p, decodeObjectIdentifier as m, decodeString as h, hexToBytes as g, requireElement as _, toArrayBuffer as v, toHex as y } from "../internal/asn1/asn1.js";
3
- import { OIDS as b } from "../internal/asn1/oids2.js";
4
- import { getCrypto as x } from "../internal/crypto/webcrypto.js";
5
- import { base64Encode as S } from "../internal/shared/base64.js";
6
- import { failureResult as C } from "../result/result.js";
7
- import { pemEncode as w, splitPemBlocksOrThrow as T } from "../pem/pem.js";
8
- import { describeHashAlgorithm as E, describeSignatureAlgorithm as D } from "../internal/crypto/algorithm-names.js";
9
- import { nameFieldKeyFromOid as O } from "../internal/x509/name-fields.js";
10
- import { parseCertificateDerOrThrow as k } from "../x509/parse.js";
11
- import { verifySignedDataDetailed as A } from "../internal/crypto/sig-verify.js";
12
- import { encodeAlgorithmIdentifier as j, getSignatureAlgorithm as M, signBytes as N } from "../internal/crypto/signing.js";
13
- import { compareDistinguishedNames as P } from "../internal/shared/dn.js";
14
- //#region src/pkcs/pkcs7.ts
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 2315 / RFC 5652
19
- * SignedData structures, and verifies signer signatures including signed-attribute flows.
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(n) {
30
- let i;
35
+ function createPkcs7CertBag(t) {
36
+ let n;
31
37
  try {
32
- i = n.flatMap(normalizeCertificateSource);
33
- } catch {
34
- return createCertBagFailure("invalid_certificate", "Each PKCS#7 certificate source must be valid PEM or DER");
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 o = u([
37
- r(1),
38
- d([]),
39
- u([a(b.pkcs7Data)]),
40
- t(0, e(i)),
41
- d([])
42
- ]), s = u([a(b.pkcs7SignedData), t(0, o)]);
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: s,
47
- pem: w("PKCS7", s),
48
- base64: S(s)
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, or an
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(e) {
72
- if (e.signers.length === 0) return createPkcs7Failure("no_signers", "createPkcs7SignedData requires at least one signer");
73
- let n = e.encapsulatedContentTypeOid ?? b.pkcs7Data, s = e.content.slice(), c = [], l = /* @__PURE__ */ new Set(), addCertificate = (e) => {
74
- let t = y(e);
75
- l.has(t) || (l.add(t), c.push(e));
76
- }, f = /* @__PURE__ */ new Set(), p = [];
77
- for (let t of e.signers) {
78
- let e = await buildPkcs7SignerInfo(t, s, n);
79
- if (!e.ok) return e;
80
- addCertificate(e.certificateDer), f.add(e.digestOid), p.push(e.signerInfoDer);
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 t of e.additionalCertificates ?? []) try {
83
- for (let e of normalizeCertificateSource(t)) k(e), addCertificate(e);
84
- } catch {
85
- return createPkcs7Failure("invalid_certificate", "Each additional PKCS#7 certificate source must be valid PEM or DER");
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 m = n === b.pkcs7Data ? 1 : 3, h = new Uint8Array(d(c));
88
- h[0] = 160;
89
- let g = u([
90
- r(m),
91
- d([...f].map((e) => u([a(e), i()]))),
92
- e.detached === !0 ? u([a(n)]) : u([a(n), t(0, o(s))]),
93
- h,
94
- d(p)
95
- ]), _ = u([a(b.pkcs7SignedData), t(0, g)]);
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: w("PKCS7", _),
101
- base64: S(_)
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(e, t, s) {
112
- let c;
123
+ async function buildPkcs7SignerInfo(t, n, i) {
124
+ let l;
113
125
  try {
114
- c = normalizeCertificateSource(e.certificate);
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 l = c[0];
119
- if (l === void 0 || c.length !== 1) return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer must provide exactly one certificate");
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 = k(l);
123
- } catch {
124
- return createPkcs7Failure("invalid_signer_certificate", "Each PKCS#7 signer certificate must be a parseable X.509 certificate");
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 f;
139
+ let p;
127
140
  try {
128
- f = M(e.privateKey, e.signature);
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
- let p = contentDigestForPrivateKey(e.privateKey);
133
- if (p === void 0) return createPkcs7Failure("unsupported_signer_key", "Unsupported signer key algorithm for content digest");
134
- let { setForSigning: m, implicitForEmit: h } = buildSignedAttributes(s, new Uint8Array(await x().subtle.digest(p.hashName, v(t)))), _;
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 N(e.privateKey, f, m);
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: u([
143
- r(1),
144
- u([g(d.issuer.derHex), n(g(d.serialNumberHex))]),
145
- u([a(p.digestOid), i()]),
146
- h,
147
- j(f),
148
- o(_)
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: p.digestOid,
151
- certificateDer: l
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.certificates
173
+ value: x509CertificatesOf(t.value.certificateChoices)
160
174
  } : t;
161
175
  }
162
- /** Parses a PEM-armored PKCS#7 cert bag. Expects exactly one `PKCS7` PEM block. */
163
- function parsePkcs7CertBagPem(e) {
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 = T(e).filter((e) => e.label === "PKCS7");
166
- if (t.length !== 1) return pkcs7Failure("malformed", "Expected exactly one PKCS7 PEM block");
167
- let n = t[0];
168
- return n === void 0 ? pkcs7Failure("malformed", "Missing PKCS7 block") : parsePkcs7CertBagDer(n.bytes);
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(e) {
194
+ function parsePkcs7SignedDataDer(t) {
175
195
  try {
176
- let t = l(e, { maxDepth: 64 }), n = t[0], r = t[1];
177
- if (n === void 0 || r === void 0 || t.length !== 2 || n.tag !== 6 || r.tag !== 160 || f(e, r).length !== 1) return pkcs7Failure("malformed", "Malformed PKCS#7 content info");
178
- let i = m(n.value);
179
- if (i !== b.pkcs7SignedData) return pkcs7Failure("not_signed_data", "PKCS#7 content is not signedData");
180
- let a = f(e, childAt(e, r, 0, "signedData")), o = a[0], s = a[1], c = a[2], u = a.slice(3), d = u[u.length - 1], h = parseSignedDataOptionalFields(u.slice(0, -1));
181
- if (!h.ok) return h;
182
- if (o === void 0 || s === void 0 || c === void 0 || d === void 0 || d.tag !== 49) return pkcs7Failure("malformed", "Malformed SignedData");
183
- let g = e.slice(c.start - c.headerLength, c.end), _ = l(g), v = _[0], y = _[1], x = parseDigestAlgorithms(e, s);
184
- return v === void 0 ? pkcs7Failure("malformed", "Malformed EncapsulatedContentInfo") : {
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(e),
208
+ der: new Uint8Array(t),
188
209
  contentTypeOid: i,
189
- version: p(o.value),
210
+ version: h(s.value),
190
211
  digestAlgorithmOids: x,
191
- digestAlgorithmNames: x.map((e) => E(e)),
192
- encapsulatedContentTypeOid: m(v.value),
193
- ...y === void 0 ? {} : { encapsulatedContent: extractEncapsulatedContent(g, y) },
194
- certificates: parseCertificateSet(e, h.value.certificates),
195
- signerInfos: parseSignerInfos(e, d)
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
- return pkcs7Failure("malformed", "Malformed PKCS#7 structure");
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 (r.tag === 160) {
206
- if (n !== void 0) return pkcs7Failure("malformed", "SignedData certificates field must precede CRLs field");
207
- if (t !== void 0) return pkcs7Failure("malformed", "SignedData certificates field must not repeat");
208
- t = r;
209
- continue;
210
- }
211
- if (r.tag === 161) {
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
- try {
229
- let t = T(e).filter((e) => e.label === "PKCS7"), n = t[0];
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
- * console.log('all signers verified');
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.certificates.find((e) => signerIdentifierMatches(e, t));
281
- return r === void 0 ? verifyPkcs7Failure("signer_not_found", "Signer certificate not found in SignedData certificates") : t.hasSignedAttrs ? await verifySignedAttrs(t, r, n, e.encapsulatedContentTypeOid) : await verifyPkcs7BareSignerInfo(t, r, n);
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 A(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, n);
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 verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
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, t) {
293
- return C(e, t);
319
+ function pkcs7Failure(e, n) {
320
+ return t(e, n);
294
321
  }
295
322
  /** Shorthand for constructing a PKCS#7 verification failure result. */
296
- function verifyPkcs7Failure(e, t) {
297
- return C(e, t);
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, t) {
301
- return C(e, t);
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, t) {
305
- return C(e, t);
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
- return typeof e == "string" ? T(e).filter((e) => e.label === "CERTIFICATE").map((e) => new Uint8Array(e.bytes)) : e instanceof Uint8Array ? [new Uint8Array(e)] : [new Uint8Array(e.der)];
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
- * Pairs each key with the digest used by its signature algorithm (RFC 5754):
323
- * P-256/RSA-SHA256 → SHA-256, P-384 → SHA-384, P-521 → SHA-512. Ed25519 uses
324
- * SHA-512 for the messageDigest attribute, per RFC 8419.
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: b.sha256
371
+ digestOid: S.sha256
334
372
  };
335
373
  case "P-384": return {
336
374
  hashName: "SHA-384",
337
- digestOid: b.sha384
375
+ digestOid: S.sha384
338
376
  };
339
377
  case "P-521": return {
340
378
  hashName: "SHA-512",
341
- digestOid: b.sha512
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: b.sha256
389
+ digestOid: S.sha256
352
390
  };
353
391
  case "SHA-384": return {
354
392
  hashName: "SHA-384",
355
- digestOid: b.sha384
393
+ digestOid: S.sha384
356
394
  };
357
395
  case "SHA-512": return {
358
396
  hashName: "SHA-512",
359
- digestOid: b.sha512
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: b.sha512
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 = d([u([a(b.cmsContentType), d([a(e)])]), u([a(b.cmsMessageDigest), d([o(t)])])]), r = new Uint8Array(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
- /** Parses the IMPLICIT [0] certificate set from a SignedData structure. */
383
- function parseCertificateSet(e, t) {
384
- if (t === void 0 || t.tag !== 160) return [];
385
- let n = [], r = t.start;
386
- for (; r < t.end;) {
387
- let t = s(e, r);
388
- n.push(k(e.slice(r, t.end))), r = t.end;
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
- return n;
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 f(e, t)) {
473
+ for (let r of m(e, t)) {
397
474
  if (r.tag !== 48) throw Error("digestAlgorithm must use AlgorithmIdentifier SEQUENCE");
398
- let t = l(e.slice(r.start - r.headerLength, r.end)), i = t[0];
399
- if (i === void 0 || t.length < 1 || t.length > 2 || i.tag !== 6) throw Error("Malformed digest AlgorithmIdentifier");
400
- n.push(m(i.value));
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 f(e, t).map((t) => parseSignerInfo(e, t));
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 = l(n), i = r[0], a = r[1], o = r[2], s = 3, c = r[s]?.tag === 160 ? r[s] : void 0;
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 u = r[s], d = r[s + 1], f = r[s + 2];
412
- if (i === void 0 || a === void 0 || o === void 0 || u === void 0 || d === void 0 || d.tag !== 4 || r.length > s + 3 || f !== void 0 && f.tag !== 161) throw Error("Malformed SignerInfo");
413
- let h = parsePkcs7AlgorithmOid(n, o, "digest algorithm OID"), g = n.slice(u.start - u.headerLength, u.end), v = l(g), b = m(_(v[0], "signature algorithm OID").value), x = v[1], S = parseSignerIdentifier(n.slice(a.start - a.headerLength, a.end));
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: p(i.value),
416
- ...S.issuer === void 0 ? {} : { issuer: S.issuer },
417
- ...S.serialNumberHex === void 0 ? {} : { serialNumberHex: S.serialNumberHex },
418
- ...S.subjectKeyIdentifier === void 0 ? {} : { subjectKeyIdentifier: S.subjectKeyIdentifier },
419
- digestAlgorithmOid: h,
420
- digestAlgorithmName: E(h),
421
- signatureAlgorithmOid: b,
422
- signatureAlgorithmName: D(b, x === void 0 ? void 0 : new Uint8Array(g.slice(x.start - x.headerLength, x.end))),
423
- ...x === void 0 ? {} : { signatureAlgorithmParametersDer: new Uint8Array(g.slice(x.start - x.headerLength, x.end)) },
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
- return m(_(l(e.slice(t.start - t.headerLength, t.end))[0], n).value);
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 = s(e, t.start);
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 = c(e, { maxDepth: 64 });
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 { subjectKeyIdentifier: y(t.value) };
524
+ return {
525
+ type: "subjectKeyIdentifier",
526
+ subjectKeyIdentifier: x(t.value)
527
+ };
448
528
  }
449
529
  if (t.tag === 48) {
450
- let t = l(e), n = t[0], r = t[1];
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: y(r.value)
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 = y(e.slice(t.start - t.headerLength, t.end)), r = [], i = [], a = {};
465
- for (let n of f(e, t)) {
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 f(e, n)) {
468
- let n = f(e, r), s = _(n[0], "signer issuer attribute OID"), c = _(n[1], "signer issuer attribute value"), l = m(s.value), u;
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(p), i.push(p), d !== void 0 && (o[d] === void 0 && (o[d] = u), a[d] === void 0 && (a[d] = u));
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: y(e.slice(n.start - n.headerLength, n.end)),
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 = s(e, i);
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 b.sha256: return "SHA-256";
513
- case b.sha384: return "SHA-384";
514
- case b.sha512: return "SHA-512";
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 = s(e), n = {};
521
- for (let r of f(e, t)) parseSignedAttributeRequirement(e, r, n);
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 = l(r), a = i[0], o = i[1];
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 = m(a.value), c = f(r, o);
533
- s === b.cmsMessageDigest && (n.messageDigest = parseUniqueSignedAttributeOctetString(n.messageDigest, c, "messageDigest")), s === b.cmsContentType && (n.contentTypeOid = parseUniqueSignedAttributeOid(n.contentTypeOid, c, "contentType"));
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 = _(t[0], `${n} attribute value`);
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 = _(t[0], `${n} attribute value`);
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 m(r.value);
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(e, t, n, r) {
561
- if (!e.hasSignedAttrs) return verifyPkcs7Failure("malformed", "Missing signedAttrs DER");
562
- let i;
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(e.signedAttrsDer), i = parseSignedAttributeRequirements(e.signedAttrsDer);
565
- } catch {
566
- return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
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 (i.contentTypeOid !== r) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
569
- let a;
644
+ if (s.contentTypeOid !== o) return verifyPkcs7Failure("malformed", "SignedData contentType attribute does not match");
645
+ let c;
570
646
  try {
571
- let t = digestAlgorithmHash(e.digestAlgorithmOid);
572
- a = new Uint8Array(await x().subtle.digest(t, v(n)));
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(a, i.messageDigest)) return verifyPkcs7Failure("message_digest_mismatch", "Content digest does not match messageDigest attribute");
577
- let o;
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
- o = retagSignedAttrsAsSet(e.signedAttrsDer);
655
+ l = retagSignedAttrsAsSet(t.signedAttrsDer);
580
656
  } catch {
581
657
  return verifyPkcs7Failure("malformed", "Malformed signedAttrs in SignedData");
582
658
  }
583
- let s;
659
+ let u;
584
660
  try {
585
- let n = await A(e.signatureAlgorithmOid, e.signatureAlgorithmParametersDer, t.publicKeyAlgorithmOid, t.publicKeyParametersOid, t.subjectPublicKeyInfoDer, e.signature, o);
586
- if (!n.ok) return n.code === "verification_error" ? verifyPkcs7Failure("malformed", "SignedData signature verification failed") : verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
587
- s = n.valid;
588
- } catch {
589
- return verifyPkcs7Failure("malformed", "Unsupported signature algorithm in SignedData");
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 s ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature over signedAttrs does not verify");
667
+ return u ? { ok: !0 } : verifyPkcs7Failure("signature_invalid", "SignedData signature over signedAttrs does not verify");
592
668
  }
593
669
  function signerIdentifierMatches(e, t) {
594
- return t.issuer !== void 0 || t.serialNumberHex !== void 0 ? t.issuer !== void 0 && t.serialNumberHex !== void 0 && e.serialNumberHex === t.serialNumberHex && P(e.issuer, t.issuer) : t.subjectKeyIdentifier !== void 0 && e.subjectKeyIdentifier === t.subjectKeyIdentifier;
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 (s(e).tag !== 160) throw Error("signedAttrs must use IMPLICIT [0] tag");
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