micro509 0.8.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 (108) hide show
  1. package/dist/index.d.ts +4 -4
  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 +626 -447
  47. package/dist/keys/keys.js +652 -404
  48. package/dist/keys/keys.js.map +1 -0
  49. package/dist/keys.d.ts +2 -2
  50. package/dist/keys.js +2 -2
  51. package/dist/pem/pem.d.ts +46 -46
  52. package/dist/pem/pem.js +62 -81
  53. package/dist/pem/pem.js.map +1 -0
  54. package/dist/pem.js +2 -2
  55. package/dist/pkcs/pfx.d.ts +96 -80
  56. package/dist/pkcs/pfx.js +191 -231
  57. package/dist/pkcs/pfx.js.map +1 -0
  58. package/dist/pkcs/pkcs12-mac.d.ts +20 -18
  59. package/dist/pkcs/pkcs12-mac.js +90 -111
  60. package/dist/pkcs/pkcs12-mac.js.map +1 -0
  61. package/dist/pkcs/pkcs7.d.ts +118 -69
  62. package/dist/pkcs/pkcs7.js +401 -443
  63. package/dist/pkcs/pkcs7.js.map +1 -0
  64. package/dist/pkcs.d.ts +2 -2
  65. package/dist/pkcs.js +4 -4
  66. package/dist/result/result.d.ts +37 -32
  67. package/dist/result/result.js +46 -44
  68. package/dist/result/result.js.map +1 -0
  69. package/dist/result.js +2 -2
  70. package/dist/revocation/chain.d.ts +138 -119
  71. package/dist/revocation/chain.js +248 -281
  72. package/dist/revocation/chain.js.map +1 -0
  73. package/dist/revocation/crl.d.ts +125 -123
  74. package/dist/revocation/crl.js +653 -745
  75. package/dist/revocation/crl.js.map +1 -0
  76. package/dist/revocation/ocsp.d.ts +183 -177
  77. package/dist/revocation/ocsp.js +581 -638
  78. package/dist/revocation/ocsp.js.map +1 -0
  79. package/dist/revocation/revocation.d.ts +26 -26
  80. package/dist/revocation/revocation.js +104 -125
  81. package/dist/revocation/revocation.js.map +1 -0
  82. package/dist/revocation.js +5 -5
  83. package/dist/verify/identity.d.ts +42 -42
  84. package/dist/verify/identity.js +158 -167
  85. package/dist/verify/identity.js.map +1 -0
  86. package/dist/verify/name-constraints.d.ts +7 -7
  87. package/dist/verify/policy.d.ts +10 -10
  88. package/dist/verify/verify.d.ts +163 -163
  89. package/dist/verify/verify.js +396 -392
  90. package/dist/verify/verify.js.map +1 -0
  91. package/dist/verify.js +3 -3
  92. package/dist/x509/certificate.d.ts +117 -117
  93. package/dist/x509/certificate.js +51 -60
  94. package/dist/x509/certificate.js.map +1 -0
  95. package/dist/x509/csr.d.ts +25 -25
  96. package/dist/x509/csr.js +26 -30
  97. package/dist/x509/csr.js.map +1 -0
  98. package/dist/x509/extensions.d.ts +228 -184
  99. package/dist/x509/extensions.js +175 -200
  100. package/dist/x509/extensions.js.map +1 -0
  101. package/dist/x509/name.d.ts +94 -93
  102. package/dist/x509/name.js +30 -29
  103. package/dist/x509/name.js.map +1 -0
  104. package/dist/x509/parse.d.ts +185 -185
  105. package/dist/x509/parse.js +638 -676
  106. package/dist/x509/parse.js.map +1 -0
  107. package/dist/x509.js +6 -6
  108. package/package.json +79 -41
@@ -1,7 +1,7 @@
1
- import { integerFromNumber, nullValue, objectIdentifier, octetString, readSequenceChildren, sequence } from "../asn1/der.js";
2
- import { decodeIntegerNumber, decodeObjectIdentifier, toArrayBuffer } from "../asn1/asn1.js";
3
- import { OIDS } from "../asn1/oids.js";
4
- import { getCrypto } from "./webcrypto.js";
1
+ import { integerFromNumber as e, nullValue as t, objectIdentifier as n, octetString as r, readSequenceChildren as i, sequence as a } from "../asn1/der.js";
2
+ import { decodeIntegerNumber as o, decodeObjectIdentifier as s, toArrayBuffer as c } from "../asn1/asn1.js";
3
+ import { OIDS as l } from "../asn1/oids2.js";
4
+ import { getCrypto as u } from "./webcrypto.js";
5
5
  //#region src/internal/crypto/pbes2.ts
6
6
  /**
7
7
  * PBES2 password-based encryption and decryption (RFC 8018).
@@ -17,197 +17,186 @@ import { getCrypto } from "./webcrypto.js";
17
17
  * corrupted). Lets callers distinguish a bad password from malformed input.
18
18
  */
19
19
  /** Module-private brand so {@link isWrongPasswordError} cannot be fooled by look-alike errors. */
20
- const wrongPasswordBrand = Symbol("micro509.WrongPasswordError");
20
+ const d = Symbol("micro509.WrongPasswordError");
21
21
  /** Builds the branded `Error` (no class) thrown when decryption fails — a wrong password or corrupt content. */
22
- function wrongPasswordError(message) {
23
- return Object.assign(new Error(message), {
22
+ function wrongPasswordError(e) {
23
+ return Object.assign(Error(e), {
24
24
  name: "WrongPasswordError",
25
- [wrongPasswordBrand]: true
25
+ [d]: !0
26
26
  });
27
27
  }
28
28
  /** Type guard: was decryption rejected for a wrong password / corrupt content? */
29
- function isWrongPasswordError(value) {
30
- return value instanceof Error && wrongPasswordBrand in value;
29
+ function isWrongPasswordError(e) {
30
+ return e instanceof Error && d in e;
31
31
  }
32
32
  /** Encrypts `data` using PBES2 (PBKDF2 + AES-CBC) and returns ciphertext with algorithm params. */
33
- async function encryptPbes2(data, options) {
34
- const iterations = options.iterations ?? 1e5;
35
- const salt = options.salt ?? getCrypto().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
36
- const iv = options.iv ?? getCrypto().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
37
- const encryption = options.cipher ?? "AES-256-CBC";
38
- const prf = options.prf ?? "HMAC-SHA-256";
39
- if (!Number.isInteger(iterations) || iterations < 1) throw new RangeError(`Invalid iterations: must be an integer >= 1, got ${iterations}`);
40
- if (!(salt instanceof Uint8Array) || salt.length < 8) throw new TypeError(`Invalid salt: must be Uint8Array with length >= 8, got length ${salt.length}`);
41
- if (!(iv instanceof Uint8Array) || iv.length !== 16) throw new TypeError(`Invalid IV: must be Uint8Array of exactly 16 bytes, got length ${iv.length}`);
42
- const key = await deriveAesKey(options.password, salt, iterations, encryption, prf, ["encrypt"]);
43
- const encryptedData = new Uint8Array(await getCrypto().subtle.encrypt({
33
+ async function encryptPbes2(e, t) {
34
+ let n = t.iterations ?? 1e5, r = t.salt ?? u().getRandomValues(/* @__PURE__ */ new Uint8Array(16)), i = t.iv ?? u().getRandomValues(/* @__PURE__ */ new Uint8Array(16)), a = t.cipher ?? "AES-256-CBC", o = t.prf ?? "HMAC-SHA-256";
35
+ if (!Number.isInteger(n) || n < 1) throw RangeError(`Invalid iterations: must be an integer >= 1, got ${n}`);
36
+ if (!(r instanceof Uint8Array) || r.length < 8) throw TypeError(`Invalid salt: must be Uint8Array with length >= 8, got length ${r.length}`);
37
+ if (!(i instanceof Uint8Array) || i.length !== 16) throw TypeError(`Invalid IV: must be Uint8Array of exactly 16 bytes, got length ${i.length}`);
38
+ let s = await deriveAesKey(t.password, r, n, a, o, ["encrypt"]), l = new Uint8Array(await u().subtle.encrypt({
44
39
  name: "AES-CBC",
45
- iv: toArrayBuffer(iv)
46
- }, key, toArrayBuffer(data)));
40
+ iv: c(i)
41
+ }, s, c(e)));
47
42
  return {
48
43
  algorithmIdentifierDer: encodePbes2AlgorithmIdentifier({
49
- iterations,
50
- salt,
51
- iv,
52
- cipher: encryption,
53
- prf
44
+ iterations: n,
45
+ salt: r,
46
+ iv: i,
47
+ cipher: a,
48
+ prf: o
54
49
  }),
55
- encryptedData,
50
+ encryptedData: l,
56
51
  parameters: {
57
- iterations,
58
- salt,
59
- iv,
60
- cipher: encryption,
61
- prf
52
+ iterations: n,
53
+ salt: r,
54
+ iv: i,
55
+ cipher: a,
56
+ prf: o
62
57
  }
63
58
  };
64
59
  }
65
60
  /** Decrypts PBES2 ciphertext given the DER AlgorithmIdentifier and password. Throws on wrong password. */
66
- async function decryptPbes2(algorithmIdentifierDer, encryptedData, password) {
67
- const parameters = parsePbes2AlgorithmIdentifier(algorithmIdentifierDer);
68
- const key = await deriveAesKey(password, parameters.salt, parameters.iterations, parameters.cipher, parameters.prf, ["decrypt"]);
61
+ async function decryptPbes2(e, t, n) {
62
+ let r = parsePbes2AlgorithmIdentifier(e), i = await deriveAesKey(n, r.salt, r.iterations, r.cipher, r.prf, ["decrypt"]);
69
63
  try {
70
- return new Uint8Array(await getCrypto().subtle.decrypt({
64
+ return new Uint8Array(await u().subtle.decrypt({
71
65
  name: "AES-CBC",
72
- iv: toArrayBuffer(parameters.iv)
73
- }, key, toArrayBuffer(encryptedData)));
66
+ iv: c(r.iv)
67
+ }, i, c(t)));
74
68
  } catch {
75
69
  throw wrongPasswordError("Invalid password or encrypted content");
76
70
  }
77
71
  }
78
72
  /** DER-encodes a PBES2 AlgorithmIdentifier SEQUENCE from resolved parameters. */
79
- function encodePbes2AlgorithmIdentifier(parameters) {
80
- const encryption = resolveEncryptionProfile(parameters.cipher);
81
- const prf = resolvePrfProfile(parameters.prf);
82
- return sequence([objectIdentifier(OIDS.pbes2), sequence([sequence([objectIdentifier(OIDS.pbkdf2), sequence([
83
- octetString(parameters.salt),
84
- integerFromNumber(parameters.iterations),
85
- integerFromNumber(encryption.keyLengthBytes),
86
- sequence([objectIdentifier(prf.oid), nullValue()])
87
- ])]), sequence([objectIdentifier(encryption.oid), octetString(parameters.iv)])])]);
73
+ function encodePbes2AlgorithmIdentifier(i) {
74
+ let o = resolveEncryptionProfile(i.cipher), s = resolvePrfProfile(i.prf);
75
+ return a([n(l.pbes2), a([a([n(l.pbkdf2), a([
76
+ r(i.salt),
77
+ e(i.iterations),
78
+ e(o.keyLengthBytes),
79
+ a([n(s.oid), t()])
80
+ ])]), a([n(o.oid), r(i.iv)])])]);
88
81
  }
89
82
  /** Decodes a DER-encoded PBES2 AlgorithmIdentifier into structured {@linkcode Pbes2Parameters}. */
90
- function parsePbes2AlgorithmIdentifier(algorithmIdentifierDer) {
91
- const topLevel = readSequenceChildren(algorithmIdentifierDer);
92
- const oid = topLevel[0];
93
- const params = topLevel[1];
94
- if (oid === void 0 || params === void 0) throw new Error("Malformed PBES2 algorithm identifier");
95
- if (decodeObjectIdentifier(oid.value) !== OIDS.pbes2) throw new Error("Unsupported encryption algorithm");
96
- const paramsDer = algorithmIdentifierDer.slice(params.start - params.headerLength, params.end);
97
- const pbes2Params = readSequenceChildren(paramsDer);
98
- const kdf = pbes2Params[0];
99
- const scheme = pbes2Params[1];
100
- if (kdf === void 0 || scheme === void 0) throw new Error("Malformed PBES2 params");
101
- const kdfDer = paramsDer.slice(kdf.start - kdf.headerLength, kdf.end);
102
- const kdfChildren = readSequenceChildren(kdfDer);
103
- const kdfOid = kdfChildren[0];
104
- const kdfParams = kdfChildren[1];
105
- if (kdfOid === void 0 || kdfParams === void 0) throw new Error("Malformed KDF params");
106
- if (decodeObjectIdentifier(kdfOid.value) !== OIDS.pbkdf2) throw new Error("Unsupported KDF");
107
- const pbkdf2Der = kdfDer.slice(kdfParams.start - kdfParams.headerLength, kdfParams.end);
108
- const pbkdf2Params = readSequenceChildren(pbkdf2Der);
109
- const salt = pbkdf2Params[0];
110
- const iterations = pbkdf2Params[1];
111
- if (salt === void 0 || iterations === void 0 || salt.tag !== 4) throw new Error("Malformed PBKDF2 params");
112
- const keyLengthElement = pbkdf2Params[2];
113
- const hasExplicitKeyLength = keyLengthElement?.tag === 2;
114
- const prfElement = hasExplicitKeyLength ? pbkdf2Params[3] : keyLengthElement;
115
- const schemeChildren = readSequenceChildren(paramsDer.slice(scheme.start - scheme.headerLength, scheme.end));
116
- const schemeOid = schemeChildren[0];
117
- const iv = schemeChildren[1];
118
- if (schemeOid === void 0 || iv === void 0 || iv.tag !== 4) throw new Error("Malformed encryption scheme");
119
- const encryption = encryptionSchemeFromOid(decodeObjectIdentifier(schemeOid.value));
120
- if (encryption === void 0) throw new Error("Unsupported content encryption scheme");
121
- if (keyLengthElement !== void 0 && !hasExplicitKeyLength && keyLengthElement.tag !== 48) throw new Error("Malformed PBKDF2 params");
122
- if (hasExplicitKeyLength && decodeIntegerNumber(keyLengthElement.value) !== encryption.keyLengthBytes) throw new Error("Unsupported PBKDF2 key length");
123
- const prf = parsePbkdf2Prf(pbkdf2Der, prfElement);
124
- const iterationsValue = decodeIntegerNumber(iterations.value);
125
- const saltValue = new Uint8Array(salt.value);
126
- const ivValue = new Uint8Array(iv.value);
127
- if (iterationsValue < 1) throw new RangeError(`Invalid PBES2 iterations: must be >= 1, got ${iterationsValue}`);
128
- if (saltValue.length < 8) throw new RangeError(`Invalid PBES2 salt: must be >= 8 bytes, got ${saltValue.length}`);
129
- if (ivValue.length !== 16) throw new RangeError(`Invalid PBES2 IV: must be exactly 16 bytes, got ${ivValue.length}`);
83
+ function parsePbes2AlgorithmIdentifier(e) {
84
+ let { paramsDer: t, kdf: n, scheme: r } = parsePbes2OuterFields(e), { pbkdf2Der: a, pbkdf2Params: c } = parsePbes2KdfFields(t, n), l = c[0], u = c[1];
85
+ if (l === void 0 || u === void 0 || l.tag !== 4) throw Error("Malformed PBKDF2 params");
86
+ let d = c[2], f = d?.tag === 2, p = f ? c[3] : d, m = i(t.slice(r.start - r.headerLength, r.end)), h = m[0], g = m[1];
87
+ if (h === void 0 || g === void 0 || g.tag !== 4) throw Error("Malformed encryption scheme");
88
+ let _ = encryptionSchemeFromOid(s(h.value));
89
+ if (_ === void 0) throw Error("Unsupported content encryption scheme");
90
+ if (d !== void 0 && !f && d.tag !== 48) throw Error("Malformed PBKDF2 params");
91
+ if (f && o(d.value) !== _.keyLengthBytes) throw Error("Unsupported PBKDF2 key length");
92
+ let v = parsePbkdf2Prf(a, p), y = o(u.value), b = new Uint8Array(l.value), x = new Uint8Array(g.value);
93
+ if (y < 1) throw RangeError(`Invalid PBES2 iterations: must be >= 1, got ${y}`);
94
+ if (b.length < 8) throw RangeError(`Invalid PBES2 salt: must be >= 8 bytes, got ${b.length}`);
95
+ if (x.length !== 16) throw RangeError(`Invalid PBES2 IV: must be exactly 16 bytes, got ${x.length}`);
130
96
  return {
131
- salt: saltValue,
132
- iterations: iterationsValue,
133
- iv: ivValue,
134
- cipher: encryption.name,
135
- prf
97
+ salt: b,
98
+ iterations: y,
99
+ iv: x,
100
+ cipher: _.name,
101
+ prf: v
102
+ };
103
+ }
104
+ /** Extracts the KDF and encryption-scheme fields from a PBES2 AlgorithmIdentifier. */
105
+ function parsePbes2OuterFields(e) {
106
+ let t = i(e), n = t[0], r = t[1];
107
+ if (n === void 0 || r === void 0) throw Error("Malformed PBES2 algorithm identifier");
108
+ if (s(n.value) !== l.pbes2) throw Error("Unsupported encryption algorithm");
109
+ let a = e.slice(r.start - r.headerLength, r.end), o = i(a), c = o[0], u = o[1];
110
+ if (c === void 0 || u === void 0) throw Error("Malformed PBES2 params");
111
+ return {
112
+ paramsDer: a,
113
+ kdf: c,
114
+ scheme: u
115
+ };
116
+ }
117
+ function parsePbes2KdfFields(e, t) {
118
+ let n = e.slice(t.start - t.headerLength, t.end), r = i(n), a = r[0], o = r[1];
119
+ if (a === void 0 || o === void 0) throw Error("Malformed KDF params");
120
+ if (s(a.value) !== l.pbkdf2) throw Error("Unsupported KDF");
121
+ let c = n.slice(o.start - o.headerLength, o.end);
122
+ return {
123
+ pbkdf2Der: c,
124
+ pbkdf2Params: i(c)
136
125
  };
137
126
  }
138
127
  /** Derives an AES-CBC `CryptoKey` from a password via PBKDF2. */
139
- async function deriveAesKey(password, salt, iterations, encryptionName, prfName, usages) {
140
- const encryption = resolveEncryptionProfile(encryptionName);
141
- const prf = resolvePrfProfile(prfName);
142
- const passwordKey = await getCrypto().subtle.importKey("raw", new TextEncoder().encode(password), "PBKDF2", false, ["deriveKey"]);
143
- return getCrypto().subtle.deriveKey({
128
+ async function deriveAesKey(e, t, n, r, i, a) {
129
+ let o = resolveEncryptionProfile(r), s = resolvePrfProfile(i), l = await u().subtle.importKey("raw", new TextEncoder().encode(e), "PBKDF2", !1, ["deriveKey"]);
130
+ return u().subtle.deriveKey({
144
131
  name: "PBKDF2",
145
- salt: toArrayBuffer(salt),
146
- iterations,
147
- hash: prf.hash
148
- }, passwordKey, {
132
+ salt: c(t),
133
+ iterations: n,
134
+ hash: s.hash
135
+ }, l, {
149
136
  name: "AES-CBC",
150
- length: encryption.keyLengthBits
151
- }, false, usages);
137
+ length: o.keyLengthBits
138
+ }, !1, a);
152
139
  }
153
140
  /** Parses the optional PRF AlgorithmIdentifier from PBKDF2 params. Absent means HMAC-SHA-1. */
154
- function parsePbkdf2Prf(pbkdf2Der, element) {
155
- if (element === void 0) return "HMAC-SHA-1";
156
- if (element.tag !== 48) throw new Error("Malformed PBKDF2 PRF");
157
- const oid = readSequenceChildren(pbkdf2Der.slice(element.start - element.headerLength, element.end))[0];
158
- if (oid === void 0) throw new Error("Malformed PBKDF2 PRF");
159
- const prf = prfFromOid(decodeObjectIdentifier(oid.value));
160
- if (prf === void 0) throw new Error("Unsupported PBKDF2 PRF");
161
- return prf;
141
+ function parsePbkdf2Prf(e, t) {
142
+ if (t === void 0) return "HMAC-SHA-1";
143
+ if (t.tag !== 48) throw Error("Malformed PBKDF2 PRF");
144
+ let n = i(e.slice(t.start - t.headerLength, t.end))[0];
145
+ if (n === void 0) throw Error("Malformed PBKDF2 PRF");
146
+ let r = prfFromOid(s(n.value));
147
+ if (r === void 0) throw Error("Unsupported PBKDF2 PRF");
148
+ return r;
162
149
  }
163
150
  /** Maps an AES-CBC OID to the encryption profile, or `undefined` if unsupported. */
164
- function encryptionSchemeFromOid(oid) {
165
- switch (oid) {
166
- case OIDS.aes128Cbc: return {
151
+ function encryptionSchemeFromOid(e) {
152
+ switch (e) {
153
+ case l.aes128Cbc: return {
167
154
  name: "AES-128-CBC",
168
- oid,
155
+ oid: e,
169
156
  keyLengthBits: 128,
170
157
  keyLengthBytes: 16
171
158
  };
172
- case OIDS.aes192Cbc: return {
159
+ case l.aes192Cbc: return {
173
160
  name: "AES-192-CBC",
174
- oid,
161
+ oid: e,
175
162
  keyLengthBits: 192,
176
163
  keyLengthBytes: 24
177
164
  };
178
- case OIDS.aes256Cbc: return {
165
+ case l.aes256Cbc: return {
179
166
  name: "AES-256-CBC",
180
- oid,
167
+ oid: e,
181
168
  keyLengthBits: 256,
182
169
  keyLengthBytes: 32
183
170
  };
184
171
  }
185
172
  }
186
173
  /** Maps an HMAC OID to the PRF name, or `undefined` if unsupported. */
187
- function prfFromOid(oid) {
188
- switch (oid) {
189
- case OIDS.hmacWithSHA1: return "HMAC-SHA-1";
190
- case OIDS.hmacWithSHA256: return "HMAC-SHA-256";
174
+ function prfFromOid(e) {
175
+ switch (e) {
176
+ case l.hmacWithSHA1: return "HMAC-SHA-1";
177
+ case l.hmacWithSHA256: return "HMAC-SHA-256";
191
178
  }
192
179
  }
193
180
  /** Looks up the full encryption profile for a scheme name. Throws if unsupported. */
194
- function resolveEncryptionProfile(name) {
195
- const profile = encryptionSchemeFromOid(name === "AES-128-CBC" ? OIDS.aes128Cbc : name === "AES-192-CBC" ? OIDS.aes192Cbc : OIDS.aes256Cbc);
196
- if (profile === void 0) throw new Error("Unsupported content encryption scheme");
197
- return profile;
181
+ function resolveEncryptionProfile(e) {
182
+ let t = encryptionSchemeFromOid(e === "AES-128-CBC" ? l.aes128Cbc : e === "AES-192-CBC" ? l.aes192Cbc : l.aes256Cbc);
183
+ if (t === void 0) throw Error("Unsupported content encryption scheme");
184
+ return t;
198
185
  }
199
186
  /** Looks up OID and WebCrypto hash name for a PRF. */
200
- function resolvePrfProfile(name) {
201
- switch (name) {
187
+ function resolvePrfProfile(e) {
188
+ switch (e) {
202
189
  case "HMAC-SHA-1": return {
203
- oid: OIDS.hmacWithSHA1,
190
+ oid: l.hmacWithSHA1,
204
191
  hash: "SHA-1"
205
192
  };
206
193
  case "HMAC-SHA-256": return {
207
- oid: OIDS.hmacWithSHA256,
194
+ oid: l.hmacWithSHA256,
208
195
  hash: "SHA-256"
209
196
  };
210
197
  }
211
198
  }
212
199
  //#endregion
213
200
  export { decryptPbes2, encodePbes2AlgorithmIdentifier, encryptPbes2, isWrongPasswordError, parsePbes2AlgorithmIdentifier, wrongPasswordError };
201
+
202
+ //# sourceMappingURL=pbes2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbes2.js","names":[],"sources":["../../../src/internal/crypto/pbes2.ts"],"sourcesContent":["/**\n * PBES2 password-based encryption and decryption (RFC 8018).\n *\n * Supports AES-CBC-128/192/256 with PBKDF2 using HMAC-SHA-1 or HMAC-SHA-256.\n * Used internally by encrypted PKCS#8 and PFX flows.\n *\n * @module\n */\n\nimport {\n\tdecodeIntegerNumber,\n\tdecodeObjectIdentifier,\n\ttoArrayBuffer,\n} from '#micro509/internal/asn1/asn1';\nimport {\n\tintegerFromNumber,\n\tnullValue,\n\tobjectIdentifier,\n\toctetString,\n\treadSequenceChildren,\n\tsequence,\n} from '#micro509/internal/asn1/der';\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\n\n/**\n * Thrown when PBES2 / AES-CBC decryption fails its integrity check, which in\n * practice means the supplied password was wrong (or the ciphertext was\n * corrupted). Lets callers distinguish a bad password from malformed input.\n */\n/** Module-private brand so {@link isWrongPasswordError} cannot be fooled by look-alike errors. */\nconst wrongPasswordBrand = Symbol('micro509.WrongPasswordError');\n\n/** Builds the branded `Error` (no class) thrown when decryption fails — a wrong password or corrupt content. */\nexport function wrongPasswordError(message: string): Error {\n\treturn Object.assign(new Error(message), {\n\t\tname: 'WrongPasswordError',\n\t\t[wrongPasswordBrand]: true,\n\t});\n}\n\n/** Type guard: was decryption rejected for a wrong password / corrupt content? */\nexport function isWrongPasswordError(value: unknown): value is Error {\n\treturn value instanceof Error && wrongPasswordBrand in value;\n}\n\n/** AES-CBC key sizes supported by this PBES2 implementation. */\nexport type Pbes2EncryptionScheme = 'AES-128-CBC' | 'AES-192-CBC' | 'AES-256-CBC';\n\n/** PBKDF2 pseudo-random function choices. `HMAC-SHA-1` is the RFC default; `HMAC-SHA-256` is preferred. */\nexport type Pbes2Prf = 'HMAC-SHA-1' | 'HMAC-SHA-256';\n\n/** Input for `encryptPbes2`. */\nexport interface Pbes2EncryptionOptions {\n\t/** Password fed to PBKDF2 for key derivation. */\n\treadonly password: string;\n\t/** PBKDF2 iteration count. Default: `100_000`. */\n\treadonly iterations?: number;\n\t/** PBKDF2 salt. Default: 16 cryptographically random bytes. */\n\treadonly salt?: Uint8Array;\n\t/** AES-CBC initialization vector. Default: 16 cryptographically random bytes. */\n\treadonly iv?: Uint8Array;\n\t/** AES-CBC cipher. Default: `'AES-256-CBC'`. */\n\treadonly cipher?: Pbes2EncryptionScheme;\n\t/** PBKDF2 PRF. Default: `'HMAC-SHA-256'`. */\n\treadonly prf?: Pbes2Prf;\n}\n\n/** Resolved PBES2 algorithm parameters, either parsed from DER or built by `encryptPbes2`. */\nexport interface Pbes2Parameters {\n\t/** PBKDF2 iteration count. */\n\treadonly iterations: number;\n\t/** PBKDF2 salt bytes. */\n\treadonly salt: Uint8Array;\n\t/** AES-CBC initialization vector. */\n\treadonly iv: Uint8Array;\n\t/** AES-CBC key-size variant. */\n\treadonly cipher: Pbes2EncryptionScheme;\n\t/** PBKDF2 pseudo-random function. */\n\treadonly prf: Pbes2Prf;\n}\n\n/** Output of `encryptPbes2`: ciphertext plus the DER-encoded AlgorithmIdentifier. */\nexport interface Pbes2EncryptionResult {\n\t/** DER-encoded PBES2 AlgorithmIdentifier SEQUENCE (embeds KDF + cipher params). */\n\treadonly algorithmIdentifierDer: Uint8Array;\n\t/** AES-CBC ciphertext (includes PKCS#7 padding). */\n\treadonly encryptedData: Uint8Array;\n\t/** Resolved algorithm parameters used during encryption. */\n\treadonly parameters: Pbes2Parameters;\n}\n\n/** Encrypts `data` using PBES2 (PBKDF2 + AES-CBC) and returns ciphertext with algorithm params. */\nexport async function encryptPbes2(\n\tdata: Uint8Array,\n\toptions: Pbes2EncryptionOptions,\n): Promise<Pbes2EncryptionResult> {\n\tconst iterations = options.iterations ?? 100_000;\n\tconst salt = options.salt ?? getCrypto().getRandomValues(new Uint8Array(16));\n\tconst iv = options.iv ?? getCrypto().getRandomValues(new Uint8Array(16));\n\tconst encryption = options.cipher ?? 'AES-256-CBC';\n\tconst prf = options.prf ?? 'HMAC-SHA-256';\n\n\t// Validate inputs before any WebCrypto calls\n\tif (!Number.isInteger(iterations) || iterations < 1) {\n\t\tthrow new RangeError(`Invalid iterations: must be an integer >= 1, got ${iterations}`);\n\t}\n\tif (!(salt instanceof Uint8Array) || salt.length < 8) {\n\t\tthrow new TypeError(\n\t\t\t`Invalid salt: must be Uint8Array with length >= 8, got length ${salt.length}`,\n\t\t);\n\t}\n\tif (!(iv instanceof Uint8Array) || iv.length !== 16) {\n\t\tthrow new TypeError(\n\t\t\t`Invalid IV: must be Uint8Array of exactly 16 bytes, got length ${iv.length}`,\n\t\t);\n\t}\n\n\tconst key = await deriveAesKey(options.password, salt, iterations, encryption, prf, ['encrypt']);\n\tconst encryptedData = new Uint8Array(\n\t\tawait getCrypto().subtle.encrypt(\n\t\t\t{ name: 'AES-CBC', iv: toArrayBuffer(iv) },\n\t\t\tkey,\n\t\t\ttoArrayBuffer(data),\n\t\t),\n\t);\n\treturn {\n\t\talgorithmIdentifierDer: encodePbes2AlgorithmIdentifier({\n\t\t\titerations,\n\t\t\tsalt,\n\t\t\tiv,\n\t\t\tcipher: encryption,\n\t\t\tprf,\n\t\t}),\n\t\tencryptedData,\n\t\tparameters: { iterations, salt, iv, cipher: encryption, prf },\n\t};\n}\n\n/** Decrypts PBES2 ciphertext given the DER AlgorithmIdentifier and password. Throws on wrong password. */\nexport async function decryptPbes2(\n\talgorithmIdentifierDer: Uint8Array,\n\tencryptedData: Uint8Array,\n\tpassword: string,\n): Promise<Uint8Array> {\n\tconst parameters = parsePbes2AlgorithmIdentifier(algorithmIdentifierDer);\n\tconst key = await deriveAesKey(\n\t\tpassword,\n\t\tparameters.salt,\n\t\tparameters.iterations,\n\t\tparameters.cipher,\n\t\tparameters.prf,\n\t\t['decrypt'],\n\t);\n\ttry {\n\t\treturn new Uint8Array(\n\t\t\tawait getCrypto().subtle.decrypt(\n\t\t\t\t{ name: 'AES-CBC', iv: toArrayBuffer(parameters.iv) },\n\t\t\t\tkey,\n\t\t\t\ttoArrayBuffer(encryptedData),\n\t\t\t),\n\t\t);\n\t} catch {\n\t\tthrow wrongPasswordError('Invalid password or encrypted content');\n\t}\n}\n\n/** DER-encodes a PBES2 AlgorithmIdentifier SEQUENCE from resolved parameters. */\nexport function encodePbes2AlgorithmIdentifier(parameters: Pbes2Parameters): Uint8Array {\n\tconst encryption = resolveEncryptionProfile(parameters.cipher);\n\tconst prf = resolvePrfProfile(parameters.prf);\n\treturn sequence([\n\t\tobjectIdentifier(OIDS.pbes2),\n\t\tsequence([\n\t\t\tsequence([\n\t\t\t\tobjectIdentifier(OIDS.pbkdf2),\n\t\t\t\tsequence([\n\t\t\t\t\toctetString(parameters.salt),\n\t\t\t\t\tintegerFromNumber(parameters.iterations),\n\t\t\t\t\tintegerFromNumber(encryption.keyLengthBytes),\n\t\t\t\t\tsequence([objectIdentifier(prf.oid), nullValue()]),\n\t\t\t\t]),\n\t\t\t]),\n\t\t\tsequence([objectIdentifier(encryption.oid), octetString(parameters.iv)]),\n\t\t]),\n\t]);\n}\n\n/** Decodes a DER-encoded PBES2 AlgorithmIdentifier into structured {@linkcode Pbes2Parameters}. */\nexport function parsePbes2AlgorithmIdentifier(algorithmIdentifierDer: Uint8Array): Pbes2Parameters {\n\tconst { paramsDer, kdf, scheme } = parsePbes2OuterFields(algorithmIdentifierDer);\n\tconst { pbkdf2Der, pbkdf2Params } = parsePbes2KdfFields(paramsDer, kdf);\n\t// PBKDF2 params: SEQUENCE { salt OCTET STRING, iterationCount INTEGER, [keyLength INTEGER], [prf AlgorithmIdentifier] }\n\tconst salt = pbkdf2Params[0];\n\tconst iterations = pbkdf2Params[1];\n\tif (salt === undefined || iterations === undefined || salt.tag !== 0x04) {\n\t\tthrow new Error('Malformed PBKDF2 params');\n\t}\n\tconst keyLengthElement = pbkdf2Params[2];\n\tconst hasExplicitKeyLength = keyLengthElement?.tag === 0x02;\n\tconst prfElement = hasExplicitKeyLength ? pbkdf2Params[3] : keyLengthElement;\n\tconst schemeDer = paramsDer.slice(scheme.start - scheme.headerLength, scheme.end);\n\tconst schemeChildren = readSequenceChildren(schemeDer);\n\tconst schemeOid = schemeChildren[0];\n\tconst iv = schemeChildren[1];\n\tif (schemeOid === undefined || iv === undefined || iv.tag !== 0x04) {\n\t\tthrow new Error('Malformed encryption scheme');\n\t}\n\tconst encryption = encryptionSchemeFromOid(decodeObjectIdentifier(schemeOid.value));\n\tif (encryption === undefined) {\n\t\tthrow new Error('Unsupported content encryption scheme');\n\t}\n\tif (keyLengthElement !== undefined && !hasExplicitKeyLength && keyLengthElement.tag !== 0x30) {\n\t\tthrow new Error('Malformed PBKDF2 params');\n\t}\n\tif (\n\t\thasExplicitKeyLength &&\n\t\tdecodeIntegerNumber(keyLengthElement.value) !== encryption.keyLengthBytes\n\t) {\n\t\tthrow new Error('Unsupported PBKDF2 key length');\n\t}\n\tconst prf = parsePbkdf2Prf(pbkdf2Der, prfElement);\n\n\t// Validate parsed parameters before returning\n\tconst iterationsValue = decodeIntegerNumber(iterations.value);\n\tconst saltValue = new Uint8Array(salt.value);\n\tconst ivValue = new Uint8Array(iv.value);\n\n\tif (iterationsValue < 1) {\n\t\tthrow new RangeError(`Invalid PBES2 iterations: must be >= 1, got ${iterationsValue}`);\n\t}\n\tif (saltValue.length < 8) {\n\t\tthrow new RangeError(`Invalid PBES2 salt: must be >= 8 bytes, got ${saltValue.length}`);\n\t}\n\tif (ivValue.length !== 16) {\n\t\tthrow new RangeError(`Invalid PBES2 IV: must be exactly 16 bytes, got ${ivValue.length}`);\n\t}\n\n\treturn {\n\t\tsalt: saltValue,\n\t\titerations: iterationsValue,\n\t\tiv: ivValue,\n\t\tcipher: encryption.name,\n\t\tprf,\n\t};\n}\n\n/** Extracts the KDF and encryption-scheme fields from a PBES2 AlgorithmIdentifier. */\nfunction parsePbes2OuterFields(algorithmIdentifierDer: Uint8Array): {\n\treadonly paramsDer: Uint8Array;\n\treadonly kdf: ReturnType<typeof readSequenceChildren>[number];\n\treadonly scheme: ReturnType<typeof readSequenceChildren>[number];\n} {\n\tconst topLevel = readSequenceChildren(algorithmIdentifierDer);\n\tconst oid = topLevel[0];\n\tconst params = topLevel[1];\n\tif (oid === undefined || params === undefined) {\n\t\tthrow new Error('Malformed PBES2 algorithm identifier');\n\t}\n\tif (decodeObjectIdentifier(oid.value) !== OIDS.pbes2) {\n\t\tthrow new Error('Unsupported encryption algorithm');\n\t}\n\tconst paramsDer = algorithmIdentifierDer.slice(params.start - params.headerLength, params.end);\n\tconst pbes2Params = readSequenceChildren(paramsDer);\n\tconst kdf = pbes2Params[0];\n\tconst scheme = pbes2Params[1];\n\tif (kdf === undefined || scheme === undefined) {\n\t\tthrow new Error('Malformed PBES2 params');\n\t}\n\treturn { paramsDer, kdf, scheme };\n}\n\nfunction parsePbes2KdfFields(\n\tparamsDer: Uint8Array,\n\tkdf: ReturnType<typeof readSequenceChildren>[number],\n): {\n\treadonly pbkdf2Der: Uint8Array;\n\treadonly pbkdf2Params: ReturnType<typeof readSequenceChildren>;\n} {\n\tconst kdfDer = paramsDer.slice(kdf.start - kdf.headerLength, kdf.end);\n\tconst kdfChildren = readSequenceChildren(kdfDer);\n\tconst kdfOid = kdfChildren[0];\n\tconst kdfParams = kdfChildren[1];\n\tif (kdfOid === undefined || kdfParams === undefined) {\n\t\tthrow new Error('Malformed KDF params');\n\t}\n\tif (decodeObjectIdentifier(kdfOid.value) !== OIDS.pbkdf2) {\n\t\tthrow new Error('Unsupported KDF');\n\t}\n\tconst pbkdf2Der = kdfDer.slice(kdfParams.start - kdfParams.headerLength, kdfParams.end);\n\treturn { pbkdf2Der, pbkdf2Params: readSequenceChildren(pbkdf2Der) };\n}\n\n/** Derives an AES-CBC `CryptoKey` from a password via PBKDF2. */\nasync function deriveAesKey(\n\tpassword: string,\n\tsalt: Uint8Array,\n\titerations: number,\n\tencryptionName: Pbes2EncryptionScheme,\n\tprfName: Pbes2Prf,\n\tusages: KeyUsage[],\n): Promise<CryptoKey> {\n\tconst encryption = resolveEncryptionProfile(encryptionName);\n\tconst prf = resolvePrfProfile(prfName);\n\tconst passwordKey = await getCrypto().subtle.importKey(\n\t\t'raw',\n\t\tnew TextEncoder().encode(password),\n\t\t'PBKDF2',\n\t\tfalse,\n\t\t['deriveKey'],\n\t);\n\treturn getCrypto().subtle.deriveKey(\n\t\t{\n\t\t\tname: 'PBKDF2',\n\t\t\tsalt: toArrayBuffer(salt),\n\t\t\titerations,\n\t\t\thash: prf.hash,\n\t\t},\n\t\tpasswordKey,\n\t\t{ name: 'AES-CBC', length: encryption.keyLengthBits },\n\t\tfalse,\n\t\tusages,\n\t);\n}\n\n/** Parses the optional PRF AlgorithmIdentifier from PBKDF2 params. Absent means HMAC-SHA-1. */\nfunction parsePbkdf2Prf(\n\tpbkdf2Der: Uint8Array,\n\telement: ReturnType<typeof readSequenceChildren>[number] | undefined,\n): Pbes2Prf {\n\tif (element === undefined) {\n\t\treturn 'HMAC-SHA-1';\n\t}\n\tif (element.tag !== 0x30) {\n\t\tthrow new Error('Malformed PBKDF2 PRF');\n\t}\n\tconst prfDer = readSequenceChildren(\n\t\tpbkdf2Der.slice(element.start - element.headerLength, element.end),\n\t);\n\tconst oid = prfDer[0];\n\tif (oid === undefined) {\n\t\tthrow new Error('Malformed PBKDF2 PRF');\n\t}\n\tconst prf = prfFromOid(decodeObjectIdentifier(oid.value));\n\tif (prf === undefined) {\n\t\tthrow new Error('Unsupported PBKDF2 PRF');\n\t}\n\treturn prf;\n}\n\n/** Maps an AES-CBC OID to the encryption profile, or `undefined` if unsupported. */\nfunction encryptionSchemeFromOid(oid: string):\n\t| {\n\t\t\treadonly name: Pbes2EncryptionScheme;\n\t\t\treadonly oid: string;\n\t\t\treadonly keyLengthBits: 128 | 192 | 256;\n\t\t\treadonly keyLengthBytes: 16 | 24 | 32;\n\t }\n\t| undefined {\n\tswitch (oid) {\n\t\tcase OIDS.aes128Cbc:\n\t\t\treturn { name: 'AES-128-CBC', oid, keyLengthBits: 128, keyLengthBytes: 16 };\n\t\tcase OIDS.aes192Cbc:\n\t\t\treturn { name: 'AES-192-CBC', oid, keyLengthBits: 192, keyLengthBytes: 24 };\n\t\tcase OIDS.aes256Cbc:\n\t\t\treturn { name: 'AES-256-CBC', oid, keyLengthBits: 256, keyLengthBytes: 32 };\n\t}\n\treturn undefined;\n}\n\n/** Maps an HMAC OID to the PRF name, or `undefined` if unsupported. */\nfunction prfFromOid(oid: string): Pbes2Prf | undefined {\n\tswitch (oid) {\n\t\tcase OIDS.hmacWithSHA1:\n\t\t\treturn 'HMAC-SHA-1';\n\t\tcase OIDS.hmacWithSHA256:\n\t\t\treturn 'HMAC-SHA-256';\n\t}\n\treturn undefined;\n}\n\n/** Looks up the full encryption profile for a scheme name. Throws if unsupported. */\nfunction resolveEncryptionProfile(name: Pbes2EncryptionScheme): {\n\treadonly name: Pbes2EncryptionScheme;\n\treadonly oid: string;\n\treadonly keyLengthBits: 128 | 192 | 256;\n\treadonly keyLengthBytes: 16 | 24 | 32;\n} {\n\tconst profile = encryptionSchemeFromOid(\n\t\tname === 'AES-128-CBC'\n\t\t\t? OIDS.aes128Cbc\n\t\t\t: name === 'AES-192-CBC'\n\t\t\t\t? OIDS.aes192Cbc\n\t\t\t\t: OIDS.aes256Cbc,\n\t);\n\tif (profile === undefined) {\n\t\tthrow new Error('Unsupported content encryption scheme');\n\t}\n\treturn profile;\n}\n\n/** Looks up OID and WebCrypto hash name for a PRF. */\nfunction resolvePrfProfile(name: Pbes2Prf): {\n\treadonly oid: string;\n\treadonly hash: 'SHA-1' | 'SHA-256';\n} {\n\tswitch (name) {\n\t\tcase 'HMAC-SHA-1':\n\t\t\treturn { oid: OIDS.hmacWithSHA1, hash: 'SHA-1' };\n\t\tcase 'HMAC-SHA-256':\n\t\t\treturn { oid: OIDS.hmacWithSHA256, hash: 'SHA-256' };\n\t}\n}\n"],"mappings":";;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