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
package/dist/pkcs/pfx.js CHANGED
@@ -1,14 +1,16 @@
1
- import { explicitContext as e, integerFromNumber as t, objectIdentifier as n, octetString as r, readElement as i, readSequenceChildren as a, sequence as o, setOf as s, tlv as c } from "../internal/asn1/der.js";
2
- import { childrenOf as l, decodeIntegerNumber as u, decodeObjectIdentifier as d, toHex as f } from "../internal/asn1/asn1.js";
3
- import { OIDS as p } from "../internal/asn1/oids2.js";
4
- import { decryptPbes2 as m, encryptPbes2 as h, isWrongPasswordError as g } from "../internal/crypto/pbes2.js";
5
- import { base64Encode as _ } from "../internal/shared/base64.js";
6
- import { failureResult as v } from "../result/result.js";
7
- import { pemEncode as y, splitPemBlocksOrThrow as b } from "../pem/pem.js";
8
- import { exportPkcs8Der as x } from "../keys/keys.js";
9
- import { createPkcs12MacData as S, parsePkcs12MacData as C } from "./pkcs12-mac.js";
10
- import { parseCertificateDerOrThrow as w } from "../x509/parse.js";
11
- //#region src/pkcs/pfx.ts
1
+ import { DECODE_REFUSAL_CODES as e, failureResult as t, rethrowIfInvariant as n, throwMicro509Error as r } from "../result/result.js";
2
+ import { DECODE_LIMIT_CODES as i, decodeRefusalOf as a } from "../internal/asn1/decode-refusal.js";
3
+ import { bmpString as o, explicitContext as s, integerFromNumber as c, objectIdentifier as l, octetString as u, readElement as d, readRootElement as f, sequence as p, setOf as m, tlv as h } from "../internal/asn1/der.js";
4
+ import { decodeIntegerNumber as g, decodeObjectIdentifier as _, decodeString as v, toHex as y } from "../internal/asn1/asn1.js";
5
+ import { OIDS as b } from "../internal/asn1/oids2.js";
6
+ import { createKdfBudget as x, decryptPbes2 as S, encryptPbes2 as C, isKdfIterationLimitError as w, isWrongPasswordError as T } from "../internal/crypto/pbes2.js";
7
+ import { base64Encode as E } from "../internal/shared/base64.js";
8
+ import { pemEncode as D, splitPemBlocksOrThrow as O } from "../pem/pem.js";
9
+ import { exportPkcs8Der as k } from "../keys/keys.js";
10
+ import { berEncoding as A, berPrimitiveContent as j, berSequenceChildren as M, berStringContent as N, berToDefiniteLength as P, readBerRoot as F } from "../internal/asn1/ber.js";
11
+ import { createPkcs12MacData as I, parsePkcs12MacData as L } from "./pkcs12-mac.js";
12
+ import { parseCertificateDerOrThrow as R } from "../x509/parse.js";
13
+ // src/pkcs/pfx.ts
12
14
  /**
13
15
  * PKCS#12/PFX container creation and parsing.
14
16
  *
@@ -27,6 +29,14 @@ import { parseCertificateDerOrThrow as w } from "../x509/parse.js";
27
29
  * typed `invalid_certificate` failure when a certificate source is not a single
28
30
  * PEM/DER certificate.
29
31
  *
32
+ * When `mac` is provided, `mac.type` selects the RFC 7292 MAC (the default) or
33
+ * RFC 9879 PBMAC1.
34
+ *
35
+ * @throws {ResultError} with a {@linkcode CreatePkcs12MacDataErrorCode} when `mac.iterations`
36
+ * is out of range or `mac.password` cannot be encoded for the selected MAC, and with
37
+ * {@linkcode PfxEncoderErrorCode} `invalid_friendly_name` when a bag `friendlyName` is not a
38
+ * BMPString of 1 to 255 characters.
39
+ *
30
40
  * @example
31
41
  * ```ts
32
42
  * import { createPfx, unwrap } from 'micro509';
@@ -44,42 +54,47 @@ import { parseCertificateDerOrThrow as w } from "../x509/parse.js";
44
54
  * ```
45
55
  */
46
56
  async function createPfx(e) {
47
- let n = [], r = [];
48
- for (let t of e.certificates ?? []) {
57
+ let t = [], n = [];
58
+ for (let n of e.certificates ?? []) {
49
59
  let e;
50
60
  try {
51
- e = normalizeCertificate(t.certificate);
61
+ e = normalizeCertificate(n.certificate);
52
62
  } catch {
53
63
  return pfxCreationFailure("invalid_certificate", "Each PFX certificate source must be a single PEM or DER certificate");
54
64
  }
55
- n.push(createCertificateBag(e, t.attributes));
65
+ t.push(createCertificateBag(e, n.attributes));
56
66
  }
57
- for (let t of e.privateKeys ?? []) r.push(createPrivateKeyBag(await normalizePrivateKey(t.privateKey), t.attributes));
58
- let i = [];
59
- if (n.length > 0 && i.push(createDataContentInfo(o(n))), r.length > 0) {
60
- let t = o(r);
61
- i.push(e.encryption === void 0 ? createDataContentInfo(t) : await createEncryptedDataContentInfo(t, e.encryption));
67
+ for (let t of e.privateKeys ?? []) n.push(createPrivateKeyBag(await normalizePrivateKey(t.privateKey), t.attributes));
68
+ let r = [];
69
+ if (t.length > 0 && r.push(createDataContentInfo(p(t))), n.length > 0) {
70
+ let t = p(n);
71
+ r.push(e.encryption === void 0 ? createDataContentInfo(t) : await createEncryptedDataContentInfo(t, e.encryption));
62
72
  }
63
- let a = o(i), s = e.mac === void 0 ? void 0 : await S(a, e.mac), c = o([
64
- t(3),
65
- createDataContentInfo(a),
66
- ...s === void 0 ? [] : [s.der]
73
+ let i = p(r), a = e.mac === void 0 ? void 0 : await I(i, e.mac), o = p([
74
+ c(3),
75
+ createDataContentInfo(i),
76
+ ...a === void 0 ? [] : [a.der]
67
77
  ]);
68
78
  return {
69
79
  ok: !0,
70
80
  value: {
71
- der: c,
72
- pem: y("PKCS12", c),
73
- base64: _(c)
81
+ der: o,
82
+ pem: D("PKCS12", o),
83
+ base64: E(o)
74
84
  }
75
85
  };
76
86
  }
77
87
  /** Shorthand for constructing a PFX creation failure result. */
78
- function pfxCreationFailure(e, t) {
79
- return v(e, t);
88
+ function pfxCreationFailure(e, n) {
89
+ return t(e, n);
80
90
  }
81
91
  /**
82
- * Decodes a DER-encoded PKCS#12/PFX container into its constituent bags.
92
+ * Decodes a BER- or DER-encoded PKCS#12/PFX container into its constituent bags.
93
+ *
94
+ * The PFX, AuthenticatedSafe and SafeContents layers accept BER: indefinite and
95
+ * non-minimal lengths and constructed OCTET STRINGs. The MAC is verified over the
96
+ * AuthenticatedSafe octets as received. Certificates and private keys inside the
97
+ * bags must be DER.
83
98
  *
84
99
  * Returns a result union — check `ok` before accessing `value`.
85
100
  * Encrypted containers require `options.password`. MAC verification uses
@@ -96,39 +111,41 @@ function pfxCreationFailure(e, t) {
96
111
  * ```
97
112
  */
98
113
  async function parsePfxDer(e, t) {
114
+ return await parsePfxDerWithBudget(e, t, x(t));
115
+ }
116
+ async function parsePfxDerWithBudget(t, r, i) {
99
117
  try {
100
- let n = a(e, { maxDepth: 64 });
101
- if (n.length < 2 || n.length > 3) return pfxFailure("malformed", "Malformed PFX structure");
102
- let r = n[0], i = n[1];
103
- if (r === void 0 || i === void 0 || r.tag !== 2) return pfxFailure("malformed", "Malformed PFX structure");
104
- if (u(r.value) !== 3) return pfxFailure("malformed", "Unsupported PFX version");
105
- let o = extractContentInfoData(e.slice(i.start - i.headerLength, i.end)), s = n[2], c;
118
+ let e = M(F(t));
119
+ if (e.length < 2 || e.length > 3) return pfxFailure("malformed", "Malformed PFX structure");
120
+ let n = e[0], a = e[1];
121
+ if (n === void 0 || a === void 0) return pfxFailure("malformed", "Malformed PFX structure");
122
+ if (g(j(t, n, 2)) !== 3) return pfxFailure("malformed", "Unsupported PFX version");
123
+ let o = extractContentInfoData(t, a), s = e[2], c;
106
124
  if (s !== void 0) {
107
- let n = await C(e.slice(s.start - s.headerLength, s.end), o, t?.macPassword ?? t?.password);
108
- if (!n.ok) return pfxFailure("malformed", "Malformed PFX MacData");
109
- if (c = n.value, c.verification === "invalid") return pfxFailure("invalid_password", "Invalid PFX MAC password or corrupted content");
125
+ let e = await verifyPfxMacData(P(t, s), o, r);
126
+ if (e.error !== void 0) return e.error;
127
+ c = e.macData;
110
128
  }
111
- let l = a(o), d = [];
112
- for (let e of l) {
113
- let n = await extractSafeContents(o.slice(e.start - e.headerLength, e.end), t);
114
- if (n.error !== void 0) return n.error;
115
- let r = n.data;
116
- for (let e of a(r)) {
117
- let t = r.slice(e.start - e.headerLength, e.end);
118
- d.push(parseSafeBag(t));
119
- }
129
+ let l = [];
130
+ for (let e of M(F(o))) {
131
+ let t = await extractSafeContents(o, e, r, i);
132
+ if (t.error !== void 0) return t.error;
133
+ let n = t.data;
134
+ for (let e of M(t.root)) l.push(parseSafeBag(n, e));
120
135
  }
121
136
  return {
122
137
  ok: !0,
123
138
  value: {
124
- bags: d,
125
- certificates: d.flatMap((e) => e.kind === "certificate" ? [e.certificate] : []),
126
- privateKeys: d.flatMap((e) => e.kind === "privateKey" ? [e.pkcs8Der] : []),
139
+ bags: l,
140
+ certificates: l.flatMap((e) => e.kind === "certificate" ? [e.certificate] : []),
141
+ privateKeys: l.flatMap((e) => e.kind === "privateKey" ? [e.pkcs8Der] : []),
127
142
  ...c === void 0 ? {} : { macData: c }
128
143
  }
129
144
  };
130
- } catch {
131
- return pfxFailure("malformed", "Malformed PFX structure");
145
+ } catch (t) {
146
+ n(t);
147
+ let r = a(t, e);
148
+ return r === void 0 ? pfxFailure("malformed", "Malformed PFX structure") : pfxFailure(r.code, r.message);
132
149
  }
133
150
  }
134
151
  /**
@@ -146,137 +163,170 @@ async function parsePfxDer(e, t) {
146
163
  * }
147
164
  * ```
148
165
  */
149
- function parsePfxPem(e, t) {
150
- let n;
166
+ async function parsePfxPem(e, t) {
167
+ let n = x(t), r;
151
168
  try {
152
- let t = b(e).filter((e) => e.label === "PKCS12"), r = t[0];
153
- if (r === void 0 || t.length !== 1) return Promise.resolve(pfxFailure("malformed", "Expected exactly one PKCS12 PEM block"));
154
- n = r.bytes;
169
+ let t = O(e).filter((e) => e.label === "PKCS12"), n = t[0];
170
+ if (n === void 0 || t.length !== 1) return pfxFailure("malformed", "Expected exactly one PKCS12 PEM block");
171
+ r = n.bytes;
155
172
  } catch {
156
- return Promise.resolve(pfxFailure("malformed", "Expected exactly one PKCS12 PEM block"));
173
+ return pfxFailure("malformed", "Expected exactly one PKCS12 PEM block");
157
174
  }
158
- return parsePfxDer(n, t);
175
+ return await parsePfxDerWithBudget(r, t, n);
159
176
  }
160
177
  /** Shorthand for constructing a PFX parse failure result. */
161
- function pfxFailure(e, t) {
162
- return v(e, t);
178
+ function pfxFailure(e, n) {
179
+ return t(e, n);
163
180
  }
164
181
  /** Unwraps a `data` ContentInfo to its inner OCTET STRING payload. */
165
- function extractContentInfoData(e) {
166
- let t = a(e), n = t[0], r = t[1];
167
- if (n === void 0 || r === void 0 || t.length !== 2 || n.tag !== 6) throw Error("Malformed ContentInfo");
168
- if (d(n.value) !== p.pkcs7Data) throw Error("Only passwordless data ContentInfo is supported");
182
+ function extractContentInfoData(e, t) {
183
+ let { oid: n, content: r } = readContentInfo(e, t);
184
+ if (n !== b.pkcs7Data) throw Error("Only passwordless data ContentInfo is supported");
169
185
  return extractContextOctetString(e, r);
170
186
  }
187
+ function readContentInfo(e, t) {
188
+ let n = M(t), r = n[0], i = n[1];
189
+ if (r === void 0 || i === void 0 || n.length !== 2) throw Error("Malformed ContentInfo");
190
+ return {
191
+ oid: _(j(e, r, 6)),
192
+ content: i
193
+ };
194
+ }
171
195
  /** Extracts SafeContents bytes from a ContentInfo, decrypting if EncryptedData. */
172
- async function extractSafeContents(e, t) {
173
- let n = a(e), r = n[0], i = n[1];
174
- if (r === void 0 || i === void 0 || n.length !== 2 || r.tag !== 6) return { error: pfxFailure("malformed", "Malformed ContentInfo") };
175
- let o = d(r.value);
176
- if (o === p.pkcs7Data) return { data: extractContextOctetString(e, i) };
177
- if (o !== p.pkcs7EncryptedData) return { error: pfxFailure("malformed", "Unsupported PFX ContentInfo type") };
178
- if (t?.password === void 0) return { error: pfxFailure("password_required", "Password required for encrypted PFX content") };
179
- let s = extractContextChild(e, i), c;
196
+ async function extractSafeContents(e, t, r, o) {
197
+ let s, c;
180
198
  try {
181
- c = await decryptEncryptedData(e.slice(s.start - s.headerLength, s.end), t.password);
199
+ ({oid: s, content: c} = readContentInfo(e, t));
182
200
  } catch (e) {
183
- return g(e) ? { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") } : { error: pfxFailure("malformed", "Malformed PFX encrypted content") };
201
+ n(e);
202
+ let t = a(e, i);
203
+ return { error: t === void 0 ? pfxFailure("malformed", "Malformed ContentInfo") : pfxFailure(t.code, t.message) };
184
204
  }
205
+ if (s === b.pkcs7Data) {
206
+ let t = extractContextOctetString(e, c);
207
+ return {
208
+ data: t,
209
+ root: F(t)
210
+ };
211
+ }
212
+ if (s !== b.pkcs7EncryptedData) return { error: pfxFailure("malformed", "Unsupported PFX ContentInfo type") };
213
+ if (r?.password === void 0) return { error: pfxFailure("password_required", "Password required for encrypted PFX content") };
214
+ let l = extractContextChild(c), u;
185
215
  try {
186
- a(c);
187
- } catch {
188
- return { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") };
216
+ u = await decryptEncryptedData(e, l, r.password, o);
217
+ } catch (e) {
218
+ if (n(e), T(e)) return { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") };
219
+ if (w(e)) return { error: pfxFailure("kdf_iterations_exceeded", e.message) };
220
+ let t = a(e, i);
221
+ return { error: t === void 0 ? pfxFailure("malformed", "Malformed PFX encrypted content") : pfxFailure(t.code, t.message) };
222
+ }
223
+ try {
224
+ let e = F(u);
225
+ return M(e), {
226
+ data: u,
227
+ root: e
228
+ };
229
+ } catch (e) {
230
+ n(e);
231
+ let t = a(e, i);
232
+ return t === void 0 ? { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") } : { error: pfxFailure(t.code, t.message) };
189
233
  }
190
- return { data: c };
191
234
  }
192
235
  /** Encodes a certBag SafeBag wrapping a DER certificate with optional attributes. */
193
- function createCertificateBag(t, i) {
194
- let a = o([n(p.x509CertificateBagType), e(0, r(t))]);
195
- return o([
196
- n(p.pkcs12CertBag),
197
- e(0, a),
198
- ...encodeBagAttributes(i)
236
+ function createCertificateBag(e, t) {
237
+ let n = p([l(b.x509CertificateBagType), s(0, u(e))]);
238
+ return p([
239
+ l(b.pkcs12CertBag),
240
+ s(0, n),
241
+ ...encodeBagAttributes(t)
199
242
  ]);
200
243
  }
201
244
  /** Encodes a keyBag SafeBag wrapping PKCS#8 DER with optional attributes. */
202
- function createPrivateKeyBag(t, r) {
203
- return o([
204
- n(p.pkcs12KeyBag),
205
- e(0, t),
206
- ...encodeBagAttributes(r)
245
+ function createPrivateKeyBag(e, t) {
246
+ return p([
247
+ l(b.pkcs12KeyBag),
248
+ s(0, e),
249
+ ...encodeBagAttributes(t)
207
250
  ]);
208
251
  }
209
252
  /** Wraps raw bytes in a `pkcs7-data` ContentInfo SEQUENCE. */
210
- function createDataContentInfo(t) {
211
- return o([n(p.pkcs7Data), e(0, r(t))]);
253
+ function createDataContentInfo(e) {
254
+ return p([l(b.pkcs7Data), s(0, u(e))]);
212
255
  }
213
256
  /** PBES2-encrypts data and wraps it in a `pkcs7-encryptedData` ContentInfo. */
214
- async function createEncryptedDataContentInfo(r, i) {
215
- let a = await h(r, i), s = o([t(0), o([
216
- n(p.pkcs7Data),
217
- a.algorithmIdentifierDer,
218
- c(128, a.encryptedData)
257
+ async function createEncryptedDataContentInfo(e, t) {
258
+ let n = await C(e, t), r = p([c(0), p([
259
+ l(b.pkcs7Data),
260
+ n.algorithmIdentifierDer,
261
+ h(128, n.encryptedData)
219
262
  ])]);
220
- return o([n(p.pkcs7EncryptedData), e(0, s)]);
263
+ return p([l(b.pkcs7EncryptedData), s(0, r)]);
221
264
  }
222
265
  /** DER-encodes optional friendlyName / localKeyId into a SET OF SEQUENCE. */
223
266
  function encodeBagAttributes(e) {
224
267
  if (e === void 0) return [];
225
268
  let t = [];
226
- return e.friendlyName !== void 0 && t.push(o([n(p.friendlyName), s([bmpString(e.friendlyName)])])), e.localKeyId !== void 0 && t.push(o([n(p.localKeyId), s([r(e.localKeyId)])])), t.length === 0 ? [] : [s(t)];
269
+ return e.friendlyName !== void 0 && t.push(p([l(b.friendlyName), m([encodeFriendlyName(e.friendlyName)])])), e.localKeyId !== void 0 && t.push(p([l(b.localKeyId), m([u(e.localKeyId)])])), t.length === 0 ? [] : [m(t)];
270
+ }
271
+ function encodeFriendlyName(e) {
272
+ let refuse = () => r("invalid_friendly_name", "friendlyName must be a BMPString of 1 to 255 characters, none of them a surrogate, U+FFFE or U+FFFF");
273
+ if (e.length === 0 || e.length > 255) return refuse();
274
+ try {
275
+ return o(e);
276
+ } catch {
277
+ return refuse();
278
+ }
227
279
  }
228
- /** Decodes a single SafeBag from DER into a {@linkcode ParsedPfxBag} discriminated union. */
229
- function parseSafeBag(e) {
230
- let t = a(e), n = t[0], r = t[1], i = t[2];
231
- if (n === void 0 || r === void 0 || t.length > 3) throw Error("Malformed SafeBag");
232
- let o = d(n.value), s = parseBagAttributes(e, i);
233
- if (o === p.pkcs12CertBag) {
234
- let t = extractContextChild(e, r), n = e.slice(t.start - t.headerLength, t.end), i = a(n), c = i[0], l = i[1];
235
- if (c === void 0 || l === void 0 || i.length !== 2 || d(c.value) !== p.x509CertificateBagType) throw Error("Malformed certBag");
280
+ /** Decodes a single SafeBag into a {@linkcode ParsedPfxBag} discriminated union. */
281
+ function parseSafeBag(e, t) {
282
+ let n = M(t), r = n[0], i = n[1], a = n[2];
283
+ if (r === void 0 || i === void 0 || n.length > 3) throw Error("Malformed SafeBag");
284
+ let o = _(j(e, r, 6)), s = parseBagAttributes(e, a), c = extractContextChild(i);
285
+ if (o === b.pkcs12CertBag) {
286
+ let t = M(c), n = t[0], r = t[1];
287
+ if (n === void 0 || r === void 0 || t.length !== 2 || _(j(e, n, 6)) !== b.x509CertificateBagType) throw Error("Malformed certBag");
236
288
  return {
237
289
  kind: "certificate",
238
290
  bagId: o,
239
291
  attributes: s,
240
- certificate: w(extractContextOctetString(n, l))
292
+ certificate: R(extractContextOctetString(e, r))
241
293
  };
242
294
  }
243
- if (o === p.pkcs12KeyBag) {
244
- let t = extractContextChild(e, r);
245
- return {
295
+ if (o === b.pkcs12KeyBag) {
296
+ let t = A(e, c);
297
+ return f(t), {
246
298
  kind: "privateKey",
247
299
  bagId: o,
248
300
  attributes: s,
249
- pkcs8Der: e.slice(t.start - t.headerLength, t.end)
301
+ pkcs8Der: t
250
302
  };
251
303
  }
252
- let c = extractContextChild(e, r);
253
304
  return {
254
305
  kind: "unknown",
255
306
  bagId: o,
256
307
  attributes: s,
257
- valueDer: e.slice(c.start - c.headerLength, c.end)
308
+ valueDer: P(e, c)
258
309
  };
259
310
  }
260
311
  /** Decodes the SET OF attributes attached to a SafeBag. */
261
312
  function parseBagAttributes(e, t) {
262
313
  if (t === void 0) return { entries: [] };
314
+ if (t.tag !== 49) throw Error("Malformed PFX bag attributes");
263
315
  let n = [], r, i;
264
- for (let o of l(e, t)) {
265
- let t = e.slice(o.start - o.headerLength, o.end), s = a(t), c = s[0], u = s[1];
266
- if (c === void 0 || u === void 0 || s.length !== 2 || u.tag !== 49) throw Error("Malformed PFX bag attribute");
267
- let m = d(c.value), h = l(t, u).map((e) => t.slice(e.start - e.headerLength, e.end));
316
+ for (let a of t.children) {
317
+ let t = M(a), o = t[0], s = t[1];
318
+ if (o === void 0 || s === void 0 || t.length !== 2 || s.tag !== 49) throw Error("Malformed PFX bag attribute");
319
+ let c = _(j(e, o, 6)), l = s.children.map((t) => P(e, t));
268
320
  if (n.push({
269
- oid: m,
270
- valuesHex: h.map((e) => f(e))
271
- }), m === p.friendlyName) {
272
- let e = h[0];
273
- if (r !== void 0 || h.length !== 1 || e === void 0) throw Error("Malformed friendlyName attribute");
274
- r = decodeBmpString(e);
275
- continue;
276
- }
277
- if (m === p.localKeyId) {
278
- let e = h[0];
279
- if (i !== void 0 || h.length !== 1 || e === void 0) throw Error("Malformed localKeyId attribute");
321
+ oid: c,
322
+ valuesHex: l.map((e) => y(e))
323
+ }), c === b.friendlyName) {
324
+ let e = l[0];
325
+ if (r !== void 0 || l.length !== 1 || e === void 0) throw Error("Malformed friendlyName attribute");
326
+ r = decodeFriendlyName(e);
327
+ } else if (c === b.localKeyId) {
328
+ let e = l[0];
329
+ if (i !== void 0 || l.length !== 1 || e === void 0) throw Error("Malformed localKeyId attribute");
280
330
  i = decodeLocalKeyId(e);
281
331
  }
282
332
  }
@@ -287,69 +337,55 @@ function parseBagAttributes(e, t) {
287
337
  };
288
338
  }
289
339
  function decodeLocalKeyId(e) {
290
- let t = i(e);
340
+ let t = d(e);
291
341
  if (t.tag !== 4) throw Error("localKeyId must use OCTET STRING");
292
- return f(t.value);
342
+ return y(t.value);
293
343
  }
294
344
  /** Exports a `CryptoKey` to PKCS#8 DER, or passes raw bytes through. */
295
345
  function normalizePrivateKey(e) {
296
- return e instanceof CryptoKey ? x(e) : Promise.resolve(new Uint8Array(e));
346
+ return e instanceof CryptoKey ? k(e) : Promise.resolve(new Uint8Array(e));
297
347
  }
298
348
  /** Extracts DER bytes from a PEM string, or passes raw DER through. */
299
349
  function normalizeCertificate(e) {
300
350
  if (typeof e == "string") {
301
- let t = b(e).find((e) => e.label === "CERTIFICATE");
351
+ let t = O(e).find((e) => e.label === "CERTIFICATE");
302
352
  if (t === void 0) throw Error("Certificate PEM required");
303
353
  return new Uint8Array(t.bytes);
304
354
  }
305
355
  return e instanceof Uint8Array ? new Uint8Array(e) : new Uint8Array(e.der);
306
356
  }
357
+ /** Parses the MacData block and verifies it against the AuthenticatedSafe when a password is given. */
358
+ async function verifyPfxMacData(e, t, n) {
359
+ let r = await L(e, t, n?.macPassword ?? n?.password, n);
360
+ return r.ok ? r.value.verification === "invalid" ? { error: pfxFailure("invalid_password", "Invalid PFX MAC password or corrupted content") } : { macData: r.value } : { error: r.code === "malformed" ? pfxFailure("malformed", "Malformed PFX MacData") : pfxFailure(r.code, r.message) };
361
+ }
307
362
  /** Decrypts a PKCS#7 EncryptedData structure using PBES2 with the given password. */
308
- function decryptEncryptedData(e, t) {
309
- let n = a(e), r = n[1];
310
- if (n.length !== 2 || r === void 0) throw Error("Malformed EncryptedData");
311
- let i = n[0];
312
- if (i === void 0 || i.tag !== 2 || u(i.value) !== 0) throw Error("Malformed EncryptedData");
313
- let o = e.slice(r.start - r.headerLength, r.end), s = a(o), c = s[1], l = s[2];
314
- if (c === void 0 || l === void 0 || s.length !== 3 || s[0]?.tag !== 6) throw Error("Malformed EncryptedContentInfo");
315
- if (l.tag !== 128) throw Error("Malformed encrypted content");
316
- return m(o.slice(c.start - c.headerLength, c.end), l.value, t);
363
+ function decryptEncryptedData(e, t, n, r) {
364
+ let i = M(t), a = i[0], o = i[1];
365
+ if (i.length !== 2 || a === void 0 || o === void 0 || g(j(e, a, 2)) !== 0) throw Error("Malformed EncryptedData");
366
+ let s = M(o), c = s[0], l = s[1], u = s[2];
367
+ if (c === void 0 || l === void 0 || u === void 0 || s.length !== 3) throw Error("Malformed EncryptedContentInfo");
368
+ return j(e, c, 6), S(P(e, l), N(e, u, 128, 4), n, r);
317
369
  }
318
- /** Reads the first child of a context-tagged element, expecting an OCTET STRING. */
370
+ /** Reads the OCTET STRING inside a `[0] EXPLICIT` wrapper. */
319
371
  function extractContextOctetString(e, t) {
320
- let n = extractContextChild(e, t);
321
- if (n.tag !== 4) throw Error("Expected OCTET STRING in context value");
322
- return n.value;
372
+ return N(e, extractContextChild(t), 4);
323
373
  }
324
- /** Reads the first child element inside a context-specific constructed wrapper. */
325
- function extractContextChild(e, t) {
326
- if ((t.tag & 224) != 160) throw Error("Expected context-specific constructed value");
327
- let n = l(e, t), r = n[0];
328
- if (n.length !== 1 || r === void 0) throw Error("Expected context-specific wrapper with exactly one value");
329
- return r;
374
+ /** Reads the single child element inside a `[0] EXPLICIT` wrapper. */
375
+ function extractContextChild(e) {
376
+ if (e.tag !== 160) throw Error("Expected [0] EXPLICIT value");
377
+ let t = e.children[0];
378
+ if (e.children.length !== 1 || t === void 0) throw Error("Expected context-specific wrapper with exactly one value");
379
+ return t;
330
380
  }
331
- /** Encodes a JS string as an ASN.1 BMPString (UCS-2 big-endian, tag 0x1e). */
332
- function bmpString(e) {
333
- let t = new Uint8Array(e.length * 2);
334
- for (let n = 0; n < e.length; n += 1) {
335
- let r = e.charCodeAt(n);
336
- t[n * 2] = r >> 8, t[n * 2 + 1] = r & 255;
337
- }
338
- return c(30, t);
339
- }
340
- /** Decodes a DER-encoded BMPString (tag 0x1e) back to a JS string. */
341
- function decodeBmpString(e) {
342
- let t = i(e);
343
- if (t.tag !== 30) throw Error("Expected BMPString");
344
- if (t.value.length % 2 != 0) throw Error("BMPString must use an even number of bytes");
345
- let n = "";
346
- for (let e = 0; e < t.value.length; e += 2) {
347
- let r = t.value[e] ?? 0, i = t.value[e + 1] ?? 0;
348
- n += String.fromCharCode(r << 8 | i);
349
- }
381
+ /** RFC 2985 §5.5.1: a friendlyName value is a BMPString of SIZE (1..255). */
382
+ function decodeFriendlyName(e) {
383
+ let t = d(e);
384
+ if (t.tag !== 30) throw Error("friendlyName must be a BMPString");
385
+ let n = v(30, t.value);
386
+ if (n.length < 1 || n.length > 255) throw Error("friendlyName must hold 1 to 255 characters");
350
387
  return n;
351
388
  }
352
- //#endregion
353
389
  export { createPfx, parsePfxDer, parsePfxPem };
354
390
 
355
391
  //# sourceMappingURL=pfx.js.map