micro509 0.7.1 → 0.8.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 (110) hide show
  1. package/README.md +2 -0
  2. package/dist/index.d.ts +5 -5
  3. package/dist/index.js +15 -1
  4. package/dist/internal/asn1/asn1.js +280 -2
  5. package/dist/internal/asn1/der.js +378 -2
  6. package/dist/internal/asn1/oids.js +103 -2
  7. package/dist/internal/crypto/algorithm-names.js +58 -2
  8. package/dist/internal/crypto/ecdsa.js +68 -2
  9. package/dist/internal/crypto/hash.js +286 -2
  10. package/dist/internal/crypto/pbes2.js +213 -2
  11. package/dist/internal/crypto/rsa-pss.js +184 -2
  12. package/dist/internal/crypto/sig-verify.js +192 -2
  13. package/dist/internal/crypto/signing.d.ts +1 -2
  14. package/dist/internal/crypto/signing.js +123 -2
  15. package/dist/internal/crypto/webcrypto.js +14 -2
  16. package/dist/internal/shared/base64.js +16 -2
  17. package/dist/internal/shared/dn.js +84 -2
  18. package/dist/internal/shared/ip.js +164 -2
  19. package/dist/internal/verify/name-constraints-engine.js +450 -2
  20. package/dist/internal/verify/policy-engine.js +382 -2
  21. package/dist/internal/verify/verify-path.js +298 -2
  22. package/dist/internal/x509/extension-bits.js +132 -2
  23. package/dist/internal/x509/extension-registry.js +229 -2
  24. package/dist/internal/x509/name-fields.js +93 -2
  25. package/dist/keys/keys.d.ts +231 -7
  26. package/dist/keys/keys.js +1087 -5
  27. package/dist/keys.d.ts +2 -2
  28. package/dist/keys.js +2 -1
  29. package/dist/pem/pem.d.ts +2 -2
  30. package/dist/pem/pem.js +172 -6
  31. package/dist/pem.js +2 -1
  32. package/dist/pkcs/pfx.d.ts +4 -5
  33. package/dist/pkcs/pfx.js +395 -2
  34. package/dist/pkcs/pkcs12-mac.d.ts +2 -2
  35. package/dist/pkcs/pkcs12-mac.js +175 -2
  36. package/dist/pkcs/pkcs7.d.ts +1 -2
  37. package/dist/pkcs/pkcs7.js +653 -2
  38. package/dist/pkcs.d.ts +2 -2
  39. package/dist/pkcs.js +4 -1
  40. package/dist/result/result.d.ts +1 -2
  41. package/dist/result/result.js +111 -2
  42. package/dist/result.js +2 -1
  43. package/dist/revocation/chain.d.ts +4 -5
  44. package/dist/revocation/chain.js +536 -2
  45. package/dist/revocation/crl.d.ts +2 -2
  46. package/dist/revocation/crl.js +1119 -2
  47. package/dist/revocation/ocsp.d.ts +3 -2
  48. package/dist/revocation/ocsp.js +958 -2
  49. package/dist/revocation/revocation.d.ts +1 -2
  50. package/dist/revocation/revocation.js +261 -2
  51. package/dist/revocation.d.ts +2 -2
  52. package/dist/revocation.js +5 -1
  53. package/dist/verify/identity.d.ts +1 -2
  54. package/dist/verify/identity.js +270 -2
  55. package/dist/verify/name-constraints.d.ts +1 -2
  56. package/dist/verify/policy.d.ts +1 -2
  57. package/dist/verify/verify.d.ts +1 -2
  58. package/dist/verify/verify.js +764 -2
  59. package/dist/verify.js +3 -1
  60. package/dist/x509/certificate.d.ts +1 -2
  61. package/dist/x509/certificate.js +151 -2
  62. package/dist/x509/csr.d.ts +1 -2
  63. package/dist/x509/csr.js +71 -2
  64. package/dist/x509/extensions.d.ts +1 -2
  65. package/dist/x509/extensions.js +387 -2
  66. package/dist/x509/name.d.ts +1 -2
  67. package/dist/x509/name.js +173 -2
  68. package/dist/x509/parse.d.ts +84 -2
  69. package/dist/x509/parse.js +1090 -2
  70. package/dist/x509.d.ts +2 -2
  71. package/dist/x509.js +6 -1
  72. package/package.json +74 -34
  73. package/dist/internal/asn1/asn1.js.map +0 -1
  74. package/dist/internal/asn1/der.js.map +0 -1
  75. package/dist/internal/asn1/oids.js.map +0 -1
  76. package/dist/internal/crypto/algorithm-names.js.map +0 -1
  77. package/dist/internal/crypto/ecdsa.js.map +0 -1
  78. package/dist/internal/crypto/hash.js.map +0 -1
  79. package/dist/internal/crypto/pbes2.d.ts +0 -23
  80. package/dist/internal/crypto/pbes2.js.map +0 -1
  81. package/dist/internal/crypto/rsa-pss.js.map +0 -1
  82. package/dist/internal/crypto/sig-verify.js.map +0 -1
  83. package/dist/internal/crypto/signing.js.map +0 -1
  84. package/dist/internal/crypto/webcrypto.js.map +0 -1
  85. package/dist/internal/shared/base64.js.map +0 -1
  86. package/dist/internal/shared/dn.js.map +0 -1
  87. package/dist/internal/shared/ip.js.map +0 -1
  88. package/dist/internal/verify/name-constraints-engine.js.map +0 -1
  89. package/dist/internal/verify/policy-engine.js.map +0 -1
  90. package/dist/internal/verify/verify-path.js.map +0 -1
  91. package/dist/internal/x509/extension-bits.js.map +0 -1
  92. package/dist/internal/x509/extension-registry.js.map +0 -1
  93. package/dist/internal/x509/name-fields.js.map +0 -1
  94. package/dist/keys/keys.js.map +0 -1
  95. package/dist/pem/pem.js.map +0 -1
  96. package/dist/pkcs/pfx.js.map +0 -1
  97. package/dist/pkcs/pkcs12-mac.js.map +0 -1
  98. package/dist/pkcs/pkcs7.js.map +0 -1
  99. package/dist/result/result.js.map +0 -1
  100. package/dist/revocation/chain.js.map +0 -1
  101. package/dist/revocation/crl.js.map +0 -1
  102. package/dist/revocation/ocsp.js.map +0 -1
  103. package/dist/revocation/revocation.js.map +0 -1
  104. package/dist/verify/identity.js.map +0 -1
  105. package/dist/verify/verify.js.map +0 -1
  106. package/dist/x509/certificate.js.map +0 -1
  107. package/dist/x509/csr.js.map +0 -1
  108. package/dist/x509/extensions.js.map +0 -1
  109. package/dist/x509/name.js.map +0 -1
  110. package/dist/x509/parse.js.map +0 -1
@@ -1,2 +1,286 @@
1
- function e(e,t){return(e<<t|e>>>32-t)>>>0}function t(e,t){let n=BigInt(e.length)*8n,r=new Uint8Array(Math.ceil((e.length+1+8)/64)*64);return r.set(e),r[e.length]=128,new DataView(r.buffer,r.byteOffset,r.byteLength).setBigUint64(r.length-8,n,t),r}function n(e,t){let n=e[t];if(n===void 0)throw RangeError(`table index ${t}`);return n}function r(e,t){let n=e[t];if(n===void 0)throw RangeError(`word index ${t}`);return n}const i=[7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21],a=[3614090360,3905402710,606105819,3250441966,4118548399,1200080426,2821735955,4249261313,1770035416,2336552879,4294925233,2304563134,1804603682,4254626195,2792965006,1236535329,4129170786,3225465664,643717713,3921069994,3593408605,38016083,3634488961,3889429448,568446438,3275163606,4107603335,1163531501,2850285829,4243563512,1735328473,2368359562,4294588738,2272392833,1839030562,4259657740,2763975236,1272893353,4139469664,3200236656,681279174,3936430074,3572445317,76029189,3654602809,3873151461,530742520,3299628645,4096336452,1126891415,2878612391,4237533241,1700485571,2399980690,4293915773,2240044497,1873313359,4264355552,2734768916,1309151649,4149444226,3174756917,718787259,3951481745];function o(o){let s=t(o,!0),[c,l,u,d]=[1732584193,4023233417,2562383102,271733878],f=new Uint32Array(16);for(let t=0;t<s.length;t+=64){let o=new DataView(s.buffer,s.byteOffset+t,64);for(let e=0;e<16;e++)f[e]=o.getUint32(e*4,!0);let[p,m,h,g]=[c,l,u,d];for(let t=0;t<64;t++){let o,s;t<16?(o=(m&h|~m&g)>>>0,s=t):t<32?(o=(g&m|~g&h)>>>0,s=5*t+1&15):t<48?(o=(m^h^g)>>>0,s=3*t+5&15):(o=(h^(m|~g))>>>0,s=7*t&15),o=o+p+n(a,t)+r(f,s)>>>0;let c=g;g=h,h=m,m=m+e(o,n(i,t))>>>0,p=c}c=c+p>>>0,l=l+m>>>0,u=u+h>>>0,d=d+g>>>0}let p=new Uint8Array(16),m=new DataView(p.buffer,p.byteOffset,p.byteLength);return m.setUint32(0,c,!0),m.setUint32(4,l,!0),m.setUint32(8,u,!0),m.setUint32(12,d,!0),p}function s(n){let i=t(n,!1),[a,o,s,c,l]=[1732584193,4023233417,2562383102,271733878,3285377520],u=new Uint32Array(80),d=new DataView(i.buffer,i.byteOffset,i.byteLength);for(let t=0;t<i.length;t+=64){for(let e=0;e<16;e++)u[e]=d.getUint32(t+e*4,!1);for(let t=16;t<80;t++)u[t]=e(r(u,t-3)^r(u,t-8)^r(u,t-14)^r(u,t-16),1);let[n,i,f,p,m]=[a,o,s,c,l];for(let t=0;t<80;t++){let a,o;t<=19?(a=(i&f|~i&p)>>>0,o=1518500249):t<=39?(a=(i^f^p)>>>0,o=1859775393):t<=59?(a=(i&f|i&p|f&p)>>>0,o=2400959708):(a=(i^f^p)>>>0,o=3395469782);let s=e(n,5)+a+m+o+r(u,t)>>>0;m=p,p=f,f=e(i,30),i=n,n=s}a=a+n>>>0,o=o+i>>>0,s=s+f>>>0,c=c+p>>>0,l=l+m>>>0}let f=new Uint8Array(20),p=new DataView(f.buffer,f.byteOffset,f.byteLength);return p.setUint32(0,a,!1),p.setUint32(4,o,!1),p.setUint32(8,s,!1),p.setUint32(12,c,!1),p.setUint32(16,l,!1),f}export{o as md5,s as sha1};
2
- //# sourceMappingURL=hash.js.map
1
+ //#region src/internal/crypto/hash.ts
2
+ /**
3
+ * Synchronous digest implementations for MD5 and SHA-1, used where WebCrypto's
4
+ * async API is impractical (e.g. `EVP_BytesToKey` key derivation, PKCS#12 MAC).
5
+ *
6
+ * @module
7
+ */
8
+ /** Unsigned 32-bit left-rotate. */
9
+ function rotl(x, n) {
10
+ return (x << n | x >>> 32 - n) >>> 0;
11
+ }
12
+ /**
13
+ * Merkle–Damgård padding: append 0x80, zero-pad to 56 mod 64,
14
+ * append original bit-length as 64-bit integer.
15
+ */
16
+ function mdPad(data, littleEndian) {
17
+ const bitLen = BigInt(data.length) * 8n;
18
+ const padded = new Uint8Array(Math.ceil((data.length + 1 + 8) / 64) * 64);
19
+ padded.set(data);
20
+ padded[data.length] = 128;
21
+ new DataView(padded.buffer, padded.byteOffset, padded.byteLength).setBigUint64(padded.length - 8, bitLen, littleEndian);
22
+ return padded;
23
+ }
24
+ /** Bounds-checked read from a readonly number table. */
25
+ function tbl(arr, i) {
26
+ const v = arr[i];
27
+ if (v === void 0) throw new RangeError(`table index ${i}`);
28
+ return v;
29
+ }
30
+ /** Bounds-checked read from a Uint32Array. */
31
+ function w32(arr, i) {
32
+ const v = arr[i];
33
+ if (v === void 0) throw new RangeError(`word index ${i}`);
34
+ return v;
35
+ }
36
+ /** Per-round left-rotate amounts for MD5 (RFC 1321 §3.4). */
37
+ const MD5_S = [
38
+ 7,
39
+ 12,
40
+ 17,
41
+ 22,
42
+ 7,
43
+ 12,
44
+ 17,
45
+ 22,
46
+ 7,
47
+ 12,
48
+ 17,
49
+ 22,
50
+ 7,
51
+ 12,
52
+ 17,
53
+ 22,
54
+ 5,
55
+ 9,
56
+ 14,
57
+ 20,
58
+ 5,
59
+ 9,
60
+ 14,
61
+ 20,
62
+ 5,
63
+ 9,
64
+ 14,
65
+ 20,
66
+ 5,
67
+ 9,
68
+ 14,
69
+ 20,
70
+ 4,
71
+ 11,
72
+ 16,
73
+ 23,
74
+ 4,
75
+ 11,
76
+ 16,
77
+ 23,
78
+ 4,
79
+ 11,
80
+ 16,
81
+ 23,
82
+ 4,
83
+ 11,
84
+ 16,
85
+ 23,
86
+ 6,
87
+ 10,
88
+ 15,
89
+ 21,
90
+ 6,
91
+ 10,
92
+ 15,
93
+ 21,
94
+ 6,
95
+ 10,
96
+ 15,
97
+ 21,
98
+ 6,
99
+ 10,
100
+ 15,
101
+ 21
102
+ ];
103
+ /** Pre-computed sine-derived constants for MD5 (RFC 1321 §3.4). */
104
+ const MD5_K = [
105
+ 3614090360,
106
+ 3905402710,
107
+ 606105819,
108
+ 3250441966,
109
+ 4118548399,
110
+ 1200080426,
111
+ 2821735955,
112
+ 4249261313,
113
+ 1770035416,
114
+ 2336552879,
115
+ 4294925233,
116
+ 2304563134,
117
+ 1804603682,
118
+ 4254626195,
119
+ 2792965006,
120
+ 1236535329,
121
+ 4129170786,
122
+ 3225465664,
123
+ 643717713,
124
+ 3921069994,
125
+ 3593408605,
126
+ 38016083,
127
+ 3634488961,
128
+ 3889429448,
129
+ 568446438,
130
+ 3275163606,
131
+ 4107603335,
132
+ 1163531501,
133
+ 2850285829,
134
+ 4243563512,
135
+ 1735328473,
136
+ 2368359562,
137
+ 4294588738,
138
+ 2272392833,
139
+ 1839030562,
140
+ 4259657740,
141
+ 2763975236,
142
+ 1272893353,
143
+ 4139469664,
144
+ 3200236656,
145
+ 681279174,
146
+ 3936430074,
147
+ 3572445317,
148
+ 76029189,
149
+ 3654602809,
150
+ 3873151461,
151
+ 530742520,
152
+ 3299628645,
153
+ 4096336452,
154
+ 1126891415,
155
+ 2878612391,
156
+ 4237533241,
157
+ 1700485571,
158
+ 2399980690,
159
+ 4293915773,
160
+ 2240044497,
161
+ 1873313359,
162
+ 4264355552,
163
+ 2734768916,
164
+ 1309151649,
165
+ 4149444226,
166
+ 3174756917,
167
+ 718787259,
168
+ 3951481745
169
+ ];
170
+ /** Compute a 16-byte MD5 digest (RFC 1321). Used by `EVP_BytesToKey` key derivation. */
171
+ function md5(data) {
172
+ const padded = mdPad(data, true);
173
+ let [a0, b0, c0, d0] = [
174
+ 1732584193,
175
+ 4023233417,
176
+ 2562383102,
177
+ 271733878
178
+ ];
179
+ const M = /* @__PURE__ */ new Uint32Array(16);
180
+ for (let offset = 0; offset < padded.length; offset += 64) {
181
+ const view = new DataView(padded.buffer, padded.byteOffset + offset, 64);
182
+ for (let j = 0; j < 16; j++) M[j] = view.getUint32(j * 4, true);
183
+ let [A, B, C, D] = [
184
+ a0,
185
+ b0,
186
+ c0,
187
+ d0
188
+ ];
189
+ for (let i = 0; i < 64; i++) {
190
+ let F;
191
+ let g;
192
+ if (i < 16) {
193
+ F = (B & C | ~B & D) >>> 0;
194
+ g = i;
195
+ } else if (i < 32) {
196
+ F = (D & B | ~D & C) >>> 0;
197
+ g = 5 * i + 1 & 15;
198
+ } else if (i < 48) {
199
+ F = (B ^ C ^ D) >>> 0;
200
+ g = 3 * i + 5 & 15;
201
+ } else {
202
+ F = (C ^ (B | ~D)) >>> 0;
203
+ g = 7 * i & 15;
204
+ }
205
+ F = F + A + tbl(MD5_K, i) + w32(M, g) >>> 0;
206
+ const oldD = D;
207
+ D = C;
208
+ C = B;
209
+ B = B + rotl(F, tbl(MD5_S, i)) >>> 0;
210
+ A = oldD;
211
+ }
212
+ a0 = a0 + A >>> 0;
213
+ b0 = b0 + B >>> 0;
214
+ c0 = c0 + C >>> 0;
215
+ d0 = d0 + D >>> 0;
216
+ }
217
+ const out = /* @__PURE__ */ new Uint8Array(16);
218
+ const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);
219
+ dv.setUint32(0, a0, true);
220
+ dv.setUint32(4, b0, true);
221
+ dv.setUint32(8, c0, true);
222
+ dv.setUint32(12, d0, true);
223
+ return out;
224
+ }
225
+ /** Compute a 20-byte SHA-1 digest (RFC 3174). Used by PKCS#12 MAC verification. */
226
+ function sha1(data) {
227
+ const padded = mdPad(data, false);
228
+ let [h0, h1, h2, h3, h4] = [
229
+ 1732584193,
230
+ 4023233417,
231
+ 2562383102,
232
+ 271733878,
233
+ 3285377520
234
+ ];
235
+ const W = /* @__PURE__ */ new Uint32Array(80);
236
+ const pv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
237
+ for (let off = 0; off < padded.length; off += 64) {
238
+ for (let i = 0; i < 16; i++) W[i] = pv.getUint32(off + i * 4, false);
239
+ for (let i = 16; i < 80; i++) W[i] = rotl(w32(W, i - 3) ^ w32(W, i - 8) ^ w32(W, i - 14) ^ w32(W, i - 16), 1);
240
+ let [a, b, c, d, e] = [
241
+ h0,
242
+ h1,
243
+ h2,
244
+ h3,
245
+ h4
246
+ ];
247
+ for (let i = 0; i < 80; i++) {
248
+ let f;
249
+ let k;
250
+ if (i <= 19) {
251
+ f = (b & c | ~b & d) >>> 0;
252
+ k = 1518500249;
253
+ } else if (i <= 39) {
254
+ f = (b ^ c ^ d) >>> 0;
255
+ k = 1859775393;
256
+ } else if (i <= 59) {
257
+ f = (b & c | b & d | c & d) >>> 0;
258
+ k = 2400959708;
259
+ } else {
260
+ f = (b ^ c ^ d) >>> 0;
261
+ k = 3395469782;
262
+ }
263
+ const temp = rotl(a, 5) + f + e + k + w32(W, i) >>> 0;
264
+ e = d;
265
+ d = c;
266
+ c = rotl(b, 30);
267
+ b = a;
268
+ a = temp;
269
+ }
270
+ h0 = h0 + a >>> 0;
271
+ h1 = h1 + b >>> 0;
272
+ h2 = h2 + c >>> 0;
273
+ h3 = h3 + d >>> 0;
274
+ h4 = h4 + e >>> 0;
275
+ }
276
+ const out = /* @__PURE__ */ new Uint8Array(20);
277
+ const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);
278
+ dv.setUint32(0, h0, false);
279
+ dv.setUint32(4, h1, false);
280
+ dv.setUint32(8, h2, false);
281
+ dv.setUint32(12, h3, false);
282
+ dv.setUint32(16, h4, false);
283
+ return out;
284
+ }
285
+ //#endregion
286
+ export { md5, sha1 };
@@ -1,2 +1,213 @@
1
- import{integerFromNumber as e,nullValue as t,objectIdentifier as n,octetString as r,readSequenceChildren as i,sequence as a}from"../asn1/der.js";import{decodeIntegerNumber as o,decodeObjectIdentifier as s,toArrayBuffer as c}from"../asn1/asn1.js";import{OIDS as l}from"../asn1/oids.js";import{getCrypto as u}from"./webcrypto.js";const d=Symbol(`micro509.WrongPasswordError`);function f(e){return Object.assign(Error(e),{name:`WrongPasswordError`,[d]:!0})}function p(e){return e instanceof Error&&d in e}async function m(e,t){let n=t.iterations??1e5,r=t.salt??u().getRandomValues(new Uint8Array(16)),i=t.iv??u().getRandomValues(new Uint8Array(16)),a=t.cipher??`AES-256-CBC`,o=t.prf??`HMAC-SHA-256`;if(!Number.isInteger(n)||n<1)throw RangeError(`Invalid iterations: must be an integer >= 1, got ${n}`);if(!(r instanceof Uint8Array)||r.length<8)throw TypeError(`Invalid salt: must be Uint8Array with length >= 8, got length ${r.length}`);if(!(i instanceof Uint8Array)||i.length!==16)throw TypeError(`Invalid IV: must be Uint8Array of exactly 16 bytes, got length ${i.length}`);let s=await v(t.password,r,n,a,o,[`encrypt`]),l=new Uint8Array(await u().subtle.encrypt({name:`AES-CBC`,iv:c(i)},s,c(e)));return{algorithmIdentifierDer:g({iterations:n,salt:r,iv:i,cipher:a,prf:o}),encryptedData:l,parameters:{iterations:n,salt:r,iv:i,cipher:a,prf:o}}}async function h(e,t,n){let r=_(e),i=await v(n,r.salt,r.iterations,r.cipher,r.prf,[`decrypt`]);try{return new Uint8Array(await u().subtle.decrypt({name:`AES-CBC`,iv:c(r.iv)},i,c(t)))}catch{throw f(`Invalid password or encrypted content`)}}function g(i){let o=S(i.cipher),s=C(i.prf);return a([n(l.pbes2),a([a([n(l.pbkdf2),a([r(i.salt),e(i.iterations),e(o.keyLengthBytes),a([n(s.oid),t()])])]),a([n(o.oid),r(i.iv)])])])}function _(e){let t=i(e),n=t[0],r=t[1];if(n===void 0||r===void 0)throw Error(`Malformed PBES2 algorithm identifier`);if(s(n.value)!==l.pbes2)throw Error(`Unsupported encryption algorithm`);let a=e.slice(r.start-r.headerLength,r.end),c=i(a),u=c[0],d=c[1];if(u===void 0||d===void 0)throw Error(`Malformed PBES2 params`);let f=a.slice(u.start-u.headerLength,u.end),p=i(f),m=p[0],h=p[1];if(m===void 0||h===void 0)throw Error(`Malformed KDF params`);if(s(m.value)!==l.pbkdf2)throw Error(`Unsupported KDF`);let g=f.slice(h.start-h.headerLength,h.end),_=i(g),v=_[0],x=_[1];if(v===void 0||x===void 0||v.tag!==4)throw Error(`Malformed PBKDF2 params`);let S=_[2],C=S?.tag===2,w=C?_[3]:S,T=i(a.slice(d.start-d.headerLength,d.end)),E=T[0],D=T[1];if(E===void 0||D===void 0||D.tag!==4)throw Error(`Malformed encryption scheme`);let O=b(s(E.value));if(O===void 0)throw Error(`Unsupported content encryption scheme`);if(S!==void 0&&!C&&S.tag!==48)throw Error(`Malformed PBKDF2 params`);if(C&&o(S.value)!==O.keyLengthBytes)throw Error(`Unsupported PBKDF2 key length`);let k=y(g,w),A=o(x.value),j=new Uint8Array(v.value),M=new Uint8Array(D.value);if(A<1)throw RangeError(`Invalid PBES2 iterations: must be >= 1, got ${A}`);if(j.length<8)throw RangeError(`Invalid PBES2 salt: must be >= 8 bytes, got ${j.length}`);if(M.length!==16)throw RangeError(`Invalid PBES2 IV: must be exactly 16 bytes, got ${M.length}`);return{salt:j,iterations:A,iv:M,cipher:O.name,prf:k}}async function v(e,t,n,r,i,a){let o=S(r),s=C(i),l=await u().subtle.importKey(`raw`,new TextEncoder().encode(e),`PBKDF2`,!1,[`deriveKey`]);return u().subtle.deriveKey({name:`PBKDF2`,salt:c(t),iterations:n,hash:s.hash},l,{name:`AES-CBC`,length:o.keyLengthBits},!1,a)}function y(e,t){if(t===void 0)return`HMAC-SHA-1`;if(t.tag!==48)throw Error(`Malformed PBKDF2 PRF`);let n=i(e.slice(t.start-t.headerLength,t.end))[0];if(n===void 0)throw Error(`Malformed PBKDF2 PRF`);let r=x(s(n.value));if(r===void 0)throw Error(`Unsupported PBKDF2 PRF`);return r}function b(e){switch(e){case l.aes128Cbc:return{name:`AES-128-CBC`,oid:e,keyLengthBits:128,keyLengthBytes:16};case l.aes192Cbc:return{name:`AES-192-CBC`,oid:e,keyLengthBits:192,keyLengthBytes:24};case l.aes256Cbc:return{name:`AES-256-CBC`,oid:e,keyLengthBits:256,keyLengthBytes:32}}}function x(e){switch(e){case l.hmacWithSHA1:return`HMAC-SHA-1`;case l.hmacWithSHA256:return`HMAC-SHA-256`}}function S(e){let t=b(e===`AES-128-CBC`?l.aes128Cbc:e===`AES-192-CBC`?l.aes192Cbc:l.aes256Cbc);if(t===void 0)throw Error(`Unsupported content encryption scheme`);return t}function C(e){switch(e){case`HMAC-SHA-1`:return{oid:l.hmacWithSHA1,hash:`SHA-1`};case`HMAC-SHA-256`:return{oid:l.hmacWithSHA256,hash:`SHA-256`}}}export{h as decryptPbes2,g as encodePbes2AlgorithmIdentifier,m as encryptPbes2,p as isWrongPasswordError,_ as parsePbes2AlgorithmIdentifier,f as wrongPasswordError};
2
- //# sourceMappingURL=pbes2.js.map
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";
5
+ //#region src/internal/crypto/pbes2.ts
6
+ /**
7
+ * PBES2 password-based encryption and decryption (RFC 8018).
8
+ *
9
+ * Supports AES-CBC-128/192/256 with PBKDF2 using HMAC-SHA-1 or HMAC-SHA-256.
10
+ * Used internally by encrypted PKCS#8 and PFX flows.
11
+ *
12
+ * @module
13
+ */
14
+ /**
15
+ * Thrown when PBES2 / AES-CBC decryption fails its integrity check, which in
16
+ * practice means the supplied password was wrong (or the ciphertext was
17
+ * corrupted). Lets callers distinguish a bad password from malformed input.
18
+ */
19
+ /** Module-private brand so {@link isWrongPasswordError} cannot be fooled by look-alike errors. */
20
+ const wrongPasswordBrand = Symbol("micro509.WrongPasswordError");
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), {
24
+ name: "WrongPasswordError",
25
+ [wrongPasswordBrand]: true
26
+ });
27
+ }
28
+ /** Type guard: was decryption rejected for a wrong password / corrupt content? */
29
+ function isWrongPasswordError(value) {
30
+ return value instanceof Error && wrongPasswordBrand in value;
31
+ }
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({
44
+ name: "AES-CBC",
45
+ iv: toArrayBuffer(iv)
46
+ }, key, toArrayBuffer(data)));
47
+ return {
48
+ algorithmIdentifierDer: encodePbes2AlgorithmIdentifier({
49
+ iterations,
50
+ salt,
51
+ iv,
52
+ cipher: encryption,
53
+ prf
54
+ }),
55
+ encryptedData,
56
+ parameters: {
57
+ iterations,
58
+ salt,
59
+ iv,
60
+ cipher: encryption,
61
+ prf
62
+ }
63
+ };
64
+ }
65
+ /** 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"]);
69
+ try {
70
+ return new Uint8Array(await getCrypto().subtle.decrypt({
71
+ name: "AES-CBC",
72
+ iv: toArrayBuffer(parameters.iv)
73
+ }, key, toArrayBuffer(encryptedData)));
74
+ } catch {
75
+ throw wrongPasswordError("Invalid password or encrypted content");
76
+ }
77
+ }
78
+ /** 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)])])]);
88
+ }
89
+ /** 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}`);
130
+ return {
131
+ salt: saltValue,
132
+ iterations: iterationsValue,
133
+ iv: ivValue,
134
+ cipher: encryption.name,
135
+ prf
136
+ };
137
+ }
138
+ /** 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({
144
+ name: "PBKDF2",
145
+ salt: toArrayBuffer(salt),
146
+ iterations,
147
+ hash: prf.hash
148
+ }, passwordKey, {
149
+ name: "AES-CBC",
150
+ length: encryption.keyLengthBits
151
+ }, false, usages);
152
+ }
153
+ /** 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;
162
+ }
163
+ /** 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 {
167
+ name: "AES-128-CBC",
168
+ oid,
169
+ keyLengthBits: 128,
170
+ keyLengthBytes: 16
171
+ };
172
+ case OIDS.aes192Cbc: return {
173
+ name: "AES-192-CBC",
174
+ oid,
175
+ keyLengthBits: 192,
176
+ keyLengthBytes: 24
177
+ };
178
+ case OIDS.aes256Cbc: return {
179
+ name: "AES-256-CBC",
180
+ oid,
181
+ keyLengthBits: 256,
182
+ keyLengthBytes: 32
183
+ };
184
+ }
185
+ }
186
+ /** 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";
191
+ }
192
+ }
193
+ /** 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;
198
+ }
199
+ /** Looks up OID and WebCrypto hash name for a PRF. */
200
+ function resolvePrfProfile(name) {
201
+ switch (name) {
202
+ case "HMAC-SHA-1": return {
203
+ oid: OIDS.hmacWithSHA1,
204
+ hash: "SHA-1"
205
+ };
206
+ case "HMAC-SHA-256": return {
207
+ oid: OIDS.hmacWithSHA256,
208
+ hash: "SHA-256"
209
+ };
210
+ }
211
+ }
212
+ //#endregion
213
+ export { decryptPbes2, encodePbes2AlgorithmIdentifier, encryptPbes2, isWrongPasswordError, parsePbes2AlgorithmIdentifier, wrongPasswordError };