micro509 0.9.0 → 0.10.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 (107) hide show
  1. package/dist/index.d.ts +3 -3
  2. package/dist/index.js +15 -15
  3. package/dist/internal/asn1/asn1.js +145 -186
  4. package/dist/internal/asn1/asn1.js.map +1 -0
  5. package/dist/internal/asn1/der.js +210 -227
  6. package/dist/internal/asn1/der.js.map +1 -0
  7. package/dist/internal/asn1/oids.js +297 -101
  8. package/dist/internal/asn1/oids.js.map +1 -0
  9. package/dist/internal/asn1/oids2.js +69 -0
  10. package/dist/internal/asn1/oids2.js.map +1 -0
  11. package/dist/internal/crypto/algorithm-names.js +37 -35
  12. package/dist/internal/crypto/algorithm-names.js.map +1 -0
  13. package/dist/internal/crypto/ecdsa.js +25 -28
  14. package/dist/internal/crypto/ecdsa.js.map +1 -0
  15. package/dist/internal/crypto/hash.js +56 -117
  16. package/dist/internal/crypto/hash.js.map +1 -0
  17. package/dist/internal/crypto/pbes2.js +122 -133
  18. package/dist/internal/crypto/pbes2.js.map +1 -0
  19. package/dist/internal/crypto/rsa-pss.js +118 -124
  20. package/dist/internal/crypto/rsa-pss.js.map +1 -0
  21. package/dist/internal/crypto/sig-verify.js +87 -99
  22. package/dist/internal/crypto/sig-verify.js.map +1 -0
  23. package/dist/internal/crypto/signing.d.ts +12 -8
  24. package/dist/internal/crypto/signing.js +53 -55
  25. package/dist/internal/crypto/signing.js.map +1 -0
  26. package/dist/internal/crypto/webcrypto.js +5 -3
  27. package/dist/internal/crypto/webcrypto.js.map +1 -0
  28. package/dist/internal/shared/base64.js +10 -9
  29. package/dist/internal/shared/base64.js.map +1 -0
  30. package/dist/internal/shared/dn.js +50 -57
  31. package/dist/internal/shared/dn.js.map +1 -0
  32. package/dist/internal/shared/ip.js +43 -53
  33. package/dist/internal/shared/ip.js.map +1 -0
  34. package/dist/internal/verify/name-constraints-engine.js +211 -239
  35. package/dist/internal/verify/name-constraints-engine.js.map +1 -0
  36. package/dist/internal/verify/policy-engine.js +228 -247
  37. package/dist/internal/verify/policy-engine.js.map +1 -0
  38. package/dist/internal/verify/verify-path.js +213 -228
  39. package/dist/internal/verify/verify-path.js.map +1 -0
  40. package/dist/internal/x509/extension-bits.js +62 -59
  41. package/dist/internal/x509/extension-bits.js.map +1 -0
  42. package/dist/internal/x509/extension-registry.js +133 -164
  43. package/dist/internal/x509/extension-registry.js.map +1 -0
  44. package/dist/internal/x509/name-fields.js +35 -41
  45. package/dist/internal/x509/name-fields.js.map +1 -0
  46. package/dist/keys/keys.d.ts +583 -547
  47. package/dist/keys/keys.js +519 -431
  48. package/dist/keys/keys.js.map +1 -0
  49. package/dist/keys.js +2 -2
  50. package/dist/pem/pem.d.ts +46 -46
  51. package/dist/pem/pem.js +62 -81
  52. package/dist/pem/pem.js.map +1 -0
  53. package/dist/pem.js +2 -2
  54. package/dist/pkcs/pfx.d.ts +96 -80
  55. package/dist/pkcs/pfx.js +191 -231
  56. package/dist/pkcs/pfx.js.map +1 -0
  57. package/dist/pkcs/pkcs12-mac.d.ts +20 -18
  58. package/dist/pkcs/pkcs12-mac.js +90 -111
  59. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  60. package/dist/pkcs/pkcs7.d.ts +118 -69
  61. package/dist/pkcs/pkcs7.js +401 -443
  62. package/dist/pkcs/pkcs7.js.map +1 -0
  63. package/dist/pkcs.d.ts +2 -2
  64. package/dist/pkcs.js +4 -4
  65. package/dist/result/result.d.ts +37 -32
  66. package/dist/result/result.js +46 -44
  67. package/dist/result/result.js.map +1 -0
  68. package/dist/result.js +2 -2
  69. package/dist/revocation/chain.d.ts +138 -119
  70. package/dist/revocation/chain.js +248 -281
  71. package/dist/revocation/chain.js.map +1 -0
  72. package/dist/revocation/crl.d.ts +125 -123
  73. package/dist/revocation/crl.js +653 -745
  74. package/dist/revocation/crl.js.map +1 -0
  75. package/dist/revocation/ocsp.d.ts +183 -177
  76. package/dist/revocation/ocsp.js +579 -636
  77. package/dist/revocation/ocsp.js.map +1 -0
  78. package/dist/revocation/revocation.d.ts +26 -26
  79. package/dist/revocation/revocation.js +104 -125
  80. package/dist/revocation/revocation.js.map +1 -0
  81. package/dist/revocation.js +5 -5
  82. package/dist/verify/identity.d.ts +42 -42
  83. package/dist/verify/identity.js +158 -167
  84. package/dist/verify/identity.js.map +1 -0
  85. package/dist/verify/name-constraints.d.ts +7 -7
  86. package/dist/verify/policy.d.ts +10 -10
  87. package/dist/verify/verify.d.ts +163 -163
  88. package/dist/verify/verify.js +396 -392
  89. package/dist/verify/verify.js.map +1 -0
  90. package/dist/verify.js +3 -3
  91. package/dist/x509/certificate.d.ts +117 -117
  92. package/dist/x509/certificate.js +51 -60
  93. package/dist/x509/certificate.js.map +1 -0
  94. package/dist/x509/csr.d.ts +25 -25
  95. package/dist/x509/csr.js +26 -30
  96. package/dist/x509/csr.js.map +1 -0
  97. package/dist/x509/extensions.d.ts +228 -184
  98. package/dist/x509/extensions.js +175 -200
  99. package/dist/x509/extensions.js.map +1 -0
  100. package/dist/x509/name.d.ts +94 -93
  101. package/dist/x509/name.js +30 -29
  102. package/dist/x509/name.js.map +1 -0
  103. package/dist/x509/parse.d.ts +185 -185
  104. package/dist/x509/parse.js +638 -676
  105. package/dist/x509/parse.js.map +1 -0
  106. package/dist/x509.js +6 -6
  107. package/package.json +79 -41
package/dist/pkcs/pfx.js CHANGED
@@ -1,13 +1,13 @@
1
- import { explicitContext, integerFromNumber, objectIdentifier, octetString, readElement, readSequenceChildren, sequence, setOf, tlv } from "../internal/asn1/der.js";
2
- import { childrenOf, decodeIntegerNumber, decodeObjectIdentifier, toHex } from "../internal/asn1/asn1.js";
3
- import { OIDS } from "../internal/asn1/oids.js";
4
- import { decryptPbes2, encryptPbes2, isWrongPasswordError } from "../internal/crypto/pbes2.js";
5
- import { base64Encode } from "../internal/shared/base64.js";
6
- import { failureResult } from "../result/result.js";
7
- import { pemEncode, splitPemBlocksOrThrow } from "../pem/pem.js";
8
- import { exportPkcs8Der } from "../keys/keys.js";
9
- import { parseCertificateDerOrThrow } from "../x509/parse.js";
10
- import { createPkcs12MacData, parsePkcs12MacData } from "./pkcs12-mac.js";
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
11
  //#region src/pkcs/pfx.ts
12
12
  /**
13
13
  * PKCS#12/PFX container creation and parsing.
@@ -43,44 +43,40 @@ import { createPkcs12MacData, parsePkcs12MacData } from "./pkcs12-mac.js";
43
43
  * // or, when inputs are already validated: const pfx = unwrap(result);
44
44
  * ```
45
45
  */
46
- async function createPfx(input) {
47
- const certificateBags = [];
48
- const privateKeyBags = [];
49
- for (const certificate of input.certificates ?? []) {
50
- let certificateDer;
46
+ async function createPfx(e) {
47
+ let n = [], r = [];
48
+ for (let t of e.certificates ?? []) {
49
+ let e;
51
50
  try {
52
- certificateDer = normalizeCertificate(certificate.certificate);
51
+ e = normalizeCertificate(t.certificate);
53
52
  } catch {
54
53
  return pfxCreationFailure("invalid_certificate", "Each PFX certificate source must be a single PEM or DER certificate");
55
54
  }
56
- certificateBags.push(createCertificateBag(certificateDer, certificate.attributes));
55
+ n.push(createCertificateBag(e, t.attributes));
57
56
  }
58
- for (const privateKey of input.privateKeys ?? []) privateKeyBags.push(createPrivateKeyBag(await normalizePrivateKey(privateKey.privateKey), privateKey.attributes));
59
- const contentInfos = [];
60
- if (certificateBags.length > 0) contentInfos.push(createDataContentInfo(sequence(certificateBags)));
61
- if (privateKeyBags.length > 0) {
62
- const safeContents = sequence(privateKeyBags);
63
- contentInfos.push(input.encryption === void 0 ? createDataContentInfo(safeContents) : await createEncryptedDataContentInfo(safeContents, input.encryption));
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));
64
62
  }
65
- const authenticatedSafe = sequence(contentInfos);
66
- const macData = input.mac === void 0 ? void 0 : await createPkcs12MacData(authenticatedSafe, input.mac);
67
- const der = sequence([
68
- integerFromNumber(3),
69
- createDataContentInfo(authenticatedSafe),
70
- ...macData === void 0 ? [] : [macData.der]
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]
71
67
  ]);
72
68
  return {
73
- ok: true,
69
+ ok: !0,
74
70
  value: {
75
- der,
76
- pem: pemEncode("PKCS12", der),
77
- base64: base64Encode(der)
71
+ der: c,
72
+ pem: y("PKCS12", c),
73
+ base64: _(c)
78
74
  }
79
75
  };
80
76
  }
81
77
  /** Shorthand for constructing a PFX creation failure result. */
82
- function pfxCreationFailure(code, message) {
83
- return failureResult(code, message);
78
+ function pfxCreationFailure(e, t) {
79
+ return v(e, t);
84
80
  }
85
81
  /**
86
82
  * Decodes a DER-encoded PKCS#12/PFX container into its constituent bags.
@@ -99,41 +95,36 @@ function pfxCreationFailure(code, message) {
99
95
  * }
100
96
  * ```
101
97
  */
102
- async function parsePfxDer(der, options) {
98
+ async function parsePfxDer(e, t) {
103
99
  try {
104
- const topLevel = readSequenceChildren(der, { maxDepth: 64 });
105
- if (topLevel.length < 2 || topLevel.length > 3) return pfxFailure("malformed", "Malformed PFX structure");
106
- const version = topLevel[0];
107
- const authSafe = topLevel[1];
108
- if (version === void 0 || authSafe === void 0 || version.tag !== 2) return pfxFailure("malformed", "Malformed PFX structure");
109
- if (decodeIntegerNumber(version.value) !== 3) return pfxFailure("malformed", "Unsupported PFX version");
110
- const authenticatedSafeOctets = extractContentInfoData(der.slice(authSafe.start - authSafe.headerLength, authSafe.end));
111
- const macElement = topLevel[2];
112
- let macData;
113
- if (macElement !== void 0) {
114
- const macResult = await parsePkcs12MacData(der.slice(macElement.start - macElement.headerLength, macElement.end), authenticatedSafeOctets, options?.macPassword ?? options?.password);
115
- if (!macResult.ok) return pfxFailure("malformed", "Malformed PFX MacData");
116
- macData = macResult.value;
117
- if (macData.verification === "invalid") return pfxFailure("invalid_password", "Invalid PFX MAC password or corrupted content");
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;
106
+ 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");
118
110
  }
119
- const authenticatedSafe = readSequenceChildren(authenticatedSafeOctets);
120
- const bags = [];
121
- for (const contentInfo of authenticatedSafe) {
122
- const safeContentsResult = await extractSafeContents(authenticatedSafeOctets.slice(contentInfo.start - contentInfo.headerLength, contentInfo.end), options);
123
- if (safeContentsResult.error !== void 0) return safeContentsResult.error;
124
- const safeContents = safeContentsResult.data;
125
- for (const bag of readSequenceChildren(safeContents)) {
126
- const bagDer = safeContents.slice(bag.start - bag.headerLength, bag.end);
127
- bags.push(parseSafeBag(bagDer));
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));
128
119
  }
129
120
  }
130
121
  return {
131
- ok: true,
122
+ ok: !0,
132
123
  value: {
133
- bags,
134
- certificates: bags.flatMap((bag) => bag.kind === "certificate" ? [bag.certificate] : []),
135
- privateKeys: bags.flatMap((bag) => bag.kind === "privateKey" ? [bag.pkcs8Der] : []),
136
- ...macData === void 0 ? {} : { macData }
124
+ bags: d,
125
+ certificates: d.flatMap((e) => e.kind === "certificate" ? [e.certificate] : []),
126
+ privateKeys: d.flatMap((e) => e.kind === "privateKey" ? [e.pkcs8Der] : []),
127
+ ...c === void 0 ? {} : { macData: c }
137
128
  }
138
129
  };
139
130
  } catch {
@@ -155,241 +146,210 @@ async function parsePfxDer(der, options) {
155
146
  * }
156
147
  * ```
157
148
  */
158
- function parsePfxPem(pem, options) {
159
- let bytes;
149
+ function parsePfxPem(e, t) {
150
+ let n;
160
151
  try {
161
- const blocks = splitPemBlocksOrThrow(pem).filter((block) => block.label === "PKCS12");
162
- const block = blocks[0];
163
- if (block === void 0 || blocks.length !== 1) return Promise.resolve(pfxFailure("malformed", "Expected exactly one PKCS12 PEM block"));
164
- bytes = block.bytes;
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;
165
155
  } catch {
166
156
  return Promise.resolve(pfxFailure("malformed", "Expected exactly one PKCS12 PEM block"));
167
157
  }
168
- return parsePfxDer(bytes, options);
158
+ return parsePfxDer(n, t);
169
159
  }
170
160
  /** Shorthand for constructing a PFX parse failure result. */
171
- function pfxFailure(code, message) {
172
- return failureResult(code, message);
161
+ function pfxFailure(e, t) {
162
+ return v(e, t);
173
163
  }
174
164
  /** Unwraps a `data` ContentInfo to its inner OCTET STRING payload. */
175
- function extractContentInfoData(contentInfoDer) {
176
- const contentInfoChildren = readSequenceChildren(contentInfoDer);
177
- const contentType = contentInfoChildren[0];
178
- const content = contentInfoChildren[1];
179
- if (contentType === void 0 || content === void 0 || contentInfoChildren.length !== 2 || contentType.tag !== 6) throw new Error("Malformed ContentInfo");
180
- if (decodeObjectIdentifier(contentType.value) !== OIDS.pkcs7Data) throw new Error("Only passwordless data ContentInfo is supported");
181
- return extractContextOctetString(contentInfoDer, content);
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");
169
+ return extractContextOctetString(e, r);
182
170
  }
183
171
  /** Extracts SafeContents bytes from a ContentInfo, decrypting if EncryptedData. */
184
- async function extractSafeContents(contentInfoDer, options) {
185
- const contentInfoChildren = readSequenceChildren(contentInfoDer);
186
- const contentType = contentInfoChildren[0];
187
- const content = contentInfoChildren[1];
188
- if (contentType === void 0 || content === void 0 || contentInfoChildren.length !== 2 || contentType.tag !== 6) return { error: pfxFailure("malformed", "Malformed ContentInfo") };
189
- const oid = decodeObjectIdentifier(contentType.value);
190
- if (oid === OIDS.pkcs7Data) return { data: extractContextOctetString(contentInfoDer, content) };
191
- if (oid !== OIDS.pkcs7EncryptedData) return { error: pfxFailure("malformed", "Unsupported PFX ContentInfo type") };
192
- if (options?.password === void 0) return { error: pfxFailure("password_required", "Password required for encrypted PFX content") };
193
- const encryptedData = extractContextChild(contentInfoDer, content);
194
- let decrypted;
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;
195
180
  try {
196
- decrypted = await decryptEncryptedData(contentInfoDer.slice(encryptedData.start - encryptedData.headerLength, encryptedData.end), options.password);
197
- } catch (error) {
198
- if (isWrongPasswordError(error)) return { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") };
199
- return { error: pfxFailure("malformed", "Malformed PFX encrypted content") };
181
+ c = await decryptEncryptedData(e.slice(s.start - s.headerLength, s.end), t.password);
182
+ } catch (e) {
183
+ return g(e) ? { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") } : { error: pfxFailure("malformed", "Malformed PFX encrypted content") };
200
184
  }
201
185
  try {
202
- readSequenceChildren(decrypted);
186
+ a(c);
203
187
  } catch {
204
188
  return { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") };
205
189
  }
206
- return { data: decrypted };
190
+ return { data: c };
207
191
  }
208
192
  /** Encodes a certBag SafeBag wrapping a DER certificate with optional attributes. */
209
- function createCertificateBag(certificateDer, attributes) {
210
- const certBag = sequence([objectIdentifier(OIDS.x509CertificateBagType), explicitContext(0, octetString(certificateDer))]);
211
- return sequence([
212
- objectIdentifier(OIDS.pkcs12CertBag),
213
- explicitContext(0, certBag),
214
- ...encodeBagAttributes(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)
215
199
  ]);
216
200
  }
217
201
  /** Encodes a keyBag SafeBag wrapping PKCS#8 DER with optional attributes. */
218
- function createPrivateKeyBag(pkcs8Der, attributes) {
219
- return sequence([
220
- objectIdentifier(OIDS.pkcs12KeyBag),
221
- explicitContext(0, pkcs8Der),
222
- ...encodeBagAttributes(attributes)
202
+ function createPrivateKeyBag(t, r) {
203
+ return o([
204
+ n(p.pkcs12KeyBag),
205
+ e(0, t),
206
+ ...encodeBagAttributes(r)
223
207
  ]);
224
208
  }
225
209
  /** Wraps raw bytes in a `pkcs7-data` ContentInfo SEQUENCE. */
226
- function createDataContentInfo(data) {
227
- return sequence([objectIdentifier(OIDS.pkcs7Data), explicitContext(0, octetString(data))]);
210
+ function createDataContentInfo(t) {
211
+ return o([n(p.pkcs7Data), e(0, r(t))]);
228
212
  }
229
213
  /** PBES2-encrypts data and wraps it in a `pkcs7-encryptedData` ContentInfo. */
230
- async function createEncryptedDataContentInfo(data, options) {
231
- const encryption = await encryptPbes2(data, options);
232
- const encryptedData = sequence([integerFromNumber(0), sequence([
233
- objectIdentifier(OIDS.pkcs7Data),
234
- encryption.algorithmIdentifierDer,
235
- tlv(128, encryption.encryptedData)
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)
236
219
  ])]);
237
- return sequence([objectIdentifier(OIDS.pkcs7EncryptedData), explicitContext(0, encryptedData)]);
220
+ return o([n(p.pkcs7EncryptedData), e(0, s)]);
238
221
  }
239
222
  /** DER-encodes optional friendlyName / localKeyId into a SET OF SEQUENCE. */
240
- function encodeBagAttributes(attributes) {
241
- if (attributes === void 0) return [];
242
- const out = [];
243
- if (attributes.friendlyName !== void 0) out.push(sequence([objectIdentifier(OIDS.friendlyName), setOf([bmpString(attributes.friendlyName)])]));
244
- if (attributes.localKeyId !== void 0) out.push(sequence([objectIdentifier(OIDS.localKeyId), setOf([octetString(attributes.localKeyId)])]));
245
- return out.length === 0 ? [] : [setOf(out)];
223
+ function encodeBagAttributes(e) {
224
+ if (e === void 0) return [];
225
+ 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)];
246
227
  }
247
228
  /** Decodes a single SafeBag from DER into a {@linkcode ParsedPfxBag} discriminated union. */
248
- function parseSafeBag(der) {
249
- const children = readSequenceChildren(der);
250
- const bagId = children[0];
251
- const bagValue = children[1];
252
- const attributeSet = children[2];
253
- if (bagId === void 0 || bagValue === void 0 || children.length > 3) throw new Error("Malformed SafeBag");
254
- const bagOid = decodeObjectIdentifier(bagId.value);
255
- const attributes = parseBagAttributes(der, attributeSet);
256
- if (bagOid === OIDS.pkcs12CertBag) {
257
- const certBag = extractContextChild(der, bagValue);
258
- const certBagDer = der.slice(certBag.start - certBag.headerLength, certBag.end);
259
- const certBagChildren = readSequenceChildren(certBagDer);
260
- const certType = certBagChildren[0];
261
- const certValue = certBagChildren[1];
262
- if (certType === void 0 || certValue === void 0 || certBagChildren.length !== 2 || decodeObjectIdentifier(certType.value) !== OIDS.x509CertificateBagType) throw new Error("Malformed certBag");
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");
263
236
  return {
264
237
  kind: "certificate",
265
- bagId: bagOid,
266
- attributes,
267
- certificate: parseCertificateDerOrThrow(extractContextOctetString(certBagDer, certValue))
238
+ bagId: o,
239
+ attributes: s,
240
+ certificate: w(extractContextOctetString(n, l))
268
241
  };
269
242
  }
270
- if (bagOid === OIDS.pkcs12KeyBag) {
271
- const pkcs8 = extractContextChild(der, bagValue);
243
+ if (o === p.pkcs12KeyBag) {
244
+ let t = extractContextChild(e, r);
272
245
  return {
273
246
  kind: "privateKey",
274
- bagId: bagOid,
275
- attributes,
276
- pkcs8Der: der.slice(pkcs8.start - pkcs8.headerLength, pkcs8.end)
247
+ bagId: o,
248
+ attributes: s,
249
+ pkcs8Der: e.slice(t.start - t.headerLength, t.end)
277
250
  };
278
251
  }
279
- const value = extractContextChild(der, bagValue);
252
+ let c = extractContextChild(e, r);
280
253
  return {
281
254
  kind: "unknown",
282
- bagId: bagOid,
283
- attributes,
284
- valueDer: der.slice(value.start - value.headerLength, value.end)
255
+ bagId: o,
256
+ attributes: s,
257
+ valueDer: e.slice(c.start - c.headerLength, c.end)
285
258
  };
286
259
  }
287
260
  /** Decodes the SET OF attributes attached to a SafeBag. */
288
- function parseBagAttributes(source, attributeSet) {
289
- if (attributeSet === void 0) return { entries: [] };
290
- const entries = [];
291
- let friendlyName;
292
- let localKeyId;
293
- for (const attribute of childrenOf(source, attributeSet)) {
294
- const attributeDer = source.slice(attribute.start - attribute.headerLength, attribute.end);
295
- const parts = readSequenceChildren(attributeDer);
296
- const oid = parts[0];
297
- const values = parts[1];
298
- if (oid === void 0 || values === void 0 || parts.length !== 2 || values.tag !== 49) throw new Error("Malformed PFX bag attribute");
299
- const attrOid = decodeObjectIdentifier(oid.value);
300
- const rawValues = childrenOf(attributeDer, values).map((value) => attributeDer.slice(value.start - value.headerLength, value.end));
301
- entries.push({
302
- oid: attrOid,
303
- valuesHex: rawValues.map((value) => toHex(value))
304
- });
305
- if (attrOid === OIDS.friendlyName) {
306
- const firstValue = rawValues[0];
307
- if (friendlyName !== void 0 || rawValues.length !== 1 || firstValue === void 0) throw new Error("Malformed friendlyName attribute");
308
- friendlyName = decodeBmpString(firstValue);
261
+ function parseBagAttributes(e, t) {
262
+ if (t === void 0) return { entries: [] };
263
+ 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));
268
+ 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);
309
275
  continue;
310
276
  }
311
- if (attrOid === OIDS.localKeyId) {
312
- const firstValue = rawValues[0];
313
- if (localKeyId !== void 0 || rawValues.length !== 1 || firstValue === void 0) throw new Error("Malformed localKeyId attribute");
314
- localKeyId = decodeLocalKeyId(firstValue);
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");
280
+ i = decodeLocalKeyId(e);
315
281
  }
316
282
  }
317
283
  return {
318
- entries,
319
- ...friendlyName === void 0 ? {} : { friendlyName },
320
- ...localKeyId === void 0 ? {} : { localKeyId }
284
+ entries: n,
285
+ ...r === void 0 ? {} : { friendlyName: r },
286
+ ...i === void 0 ? {} : { localKeyId: i }
321
287
  };
322
288
  }
323
- function decodeLocalKeyId(der) {
324
- const element = readElement(der);
325
- if (element.tag !== 4) throw new Error("localKeyId must use OCTET STRING");
326
- return toHex(element.value);
289
+ function decodeLocalKeyId(e) {
290
+ let t = i(e);
291
+ if (t.tag !== 4) throw Error("localKeyId must use OCTET STRING");
292
+ return f(t.value);
327
293
  }
328
294
  /** Exports a `CryptoKey` to PKCS#8 DER, or passes raw bytes through. */
329
- function normalizePrivateKey(source) {
330
- return source instanceof CryptoKey ? exportPkcs8Der(source) : Promise.resolve(new Uint8Array(source));
295
+ function normalizePrivateKey(e) {
296
+ return e instanceof CryptoKey ? x(e) : Promise.resolve(new Uint8Array(e));
331
297
  }
332
298
  /** Extracts DER bytes from a PEM string, or passes raw DER through. */
333
- function normalizeCertificate(source) {
334
- if (typeof source === "string") {
335
- const block = splitPemBlocksOrThrow(source).find((candidate) => candidate.label === "CERTIFICATE");
336
- if (block === void 0) throw new Error("Certificate PEM required");
337
- return new Uint8Array(block.bytes);
299
+ function normalizeCertificate(e) {
300
+ if (typeof e == "string") {
301
+ let t = b(e).find((e) => e.label === "CERTIFICATE");
302
+ if (t === void 0) throw Error("Certificate PEM required");
303
+ return new Uint8Array(t.bytes);
338
304
  }
339
- if (source instanceof Uint8Array) return new Uint8Array(source);
340
- return new Uint8Array(source.der);
305
+ return e instanceof Uint8Array ? new Uint8Array(e) : new Uint8Array(e.der);
341
306
  }
342
307
  /** Decrypts a PKCS#7 EncryptedData structure using PBES2 with the given password. */
343
- function decryptEncryptedData(encryptedDataDer, password) {
344
- const topLevel = readSequenceChildren(encryptedDataDer);
345
- const encryptedContentInfo = topLevel[1];
346
- if (topLevel.length !== 2 || encryptedContentInfo === void 0) throw new Error("Malformed EncryptedData");
347
- const version = topLevel[0];
348
- if (version === void 0 || version.tag !== 2 || decodeIntegerNumber(version.value) !== 0) throw new Error("Malformed EncryptedData");
349
- const contentInfoDer = encryptedDataDer.slice(encryptedContentInfo.start - encryptedContentInfo.headerLength, encryptedContentInfo.end);
350
- const contentInfoChildren = readSequenceChildren(contentInfoDer);
351
- const algorithm = contentInfoChildren[1];
352
- const encryptedContent = contentInfoChildren[2];
353
- if (algorithm === void 0 || encryptedContent === void 0 || contentInfoChildren.length !== 3 || contentInfoChildren[0]?.tag !== 6) throw new Error("Malformed EncryptedContentInfo");
354
- if (encryptedContent.tag !== 128) throw new Error("Malformed encrypted content");
355
- return decryptPbes2(contentInfoDer.slice(algorithm.start - algorithm.headerLength, algorithm.end), encryptedContent.value, 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);
356
317
  }
357
318
  /** Reads the first child of a context-tagged element, expecting an OCTET STRING. */
358
- function extractContextOctetString(source, element) {
359
- const child = extractContextChild(source, element);
360
- if (child.tag !== 4) throw new Error("Expected OCTET STRING in context value");
361
- return child.value;
319
+ 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;
362
323
  }
363
324
  /** Reads the first child element inside a context-specific constructed wrapper. */
364
- function extractContextChild(source, element) {
365
- if ((element.tag & 224) !== 160) throw new Error("Expected context-specific constructed value");
366
- const children = childrenOf(source, element);
367
- const child = children[0];
368
- if (children.length !== 1 || child === void 0) throw new Error("Expected context-specific wrapper with exactly one value");
369
- return child;
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;
370
330
  }
371
331
  /** Encodes a JS string as an ASN.1 BMPString (UCS-2 big-endian, tag 0x1e). */
372
- function bmpString(value) {
373
- const bytes = new Uint8Array(value.length * 2);
374
- for (let index = 0; index < value.length; index += 1) {
375
- const codePoint = value.charCodeAt(index);
376
- bytes[index * 2] = codePoint >> 8;
377
- bytes[index * 2 + 1] = codePoint & 255;
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;
378
337
  }
379
- return tlv(30, bytes);
338
+ return c(30, t);
380
339
  }
381
340
  /** Decodes a DER-encoded BMPString (tag 0x1e) back to a JS string. */
382
- function decodeBmpString(der) {
383
- const element = readElement(der);
384
- if (element.tag !== 30) throw new Error("Expected BMPString");
385
- if (element.value.length % 2 !== 0) throw new Error("BMPString must use an even number of bytes");
386
- let value = "";
387
- for (let index = 0; index < element.value.length; index += 2) {
388
- const left = element.value[index] ?? 0;
389
- const right = element.value[index + 1] ?? 0;
390
- value += String.fromCharCode(left << 8 | right);
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);
391
349
  }
392
- return value;
350
+ return n;
393
351
  }
394
352
  //#endregion
395
353
  export { createPfx, parsePfxDer, parsePfxPem };
354
+
355
+ //# sourceMappingURL=pfx.js.map