micro509 0.7.2 → 0.9.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.
- package/dist/index.d.ts +5 -5
- package/dist/index.js +15 -1
- package/dist/internal/asn1/asn1.js +280 -2
- package/dist/internal/asn1/der.js +378 -2
- package/dist/internal/asn1/oids.js +103 -2
- package/dist/internal/crypto/algorithm-names.js +58 -2
- package/dist/internal/crypto/ecdsa.js +68 -2
- package/dist/internal/crypto/hash.js +286 -2
- package/dist/internal/crypto/pbes2.js +213 -2
- package/dist/internal/crypto/rsa-pss.js +184 -2
- package/dist/internal/crypto/sig-verify.js +192 -2
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +123 -2
- package/dist/internal/crypto/webcrypto.js +14 -2
- package/dist/internal/shared/base64.js +16 -2
- package/dist/internal/shared/dn.js +84 -2
- package/dist/internal/shared/ip.js +164 -2
- package/dist/internal/verify/name-constraints-engine.js +450 -2
- package/dist/internal/verify/policy-engine.js +382 -2
- package/dist/internal/verify/verify-path.js +298 -2
- package/dist/internal/x509/extension-bits.js +132 -2
- package/dist/internal/x509/extension-registry.js +229 -2
- package/dist/internal/x509/name-fields.js +93 -2
- package/dist/keys/keys.d.ts +380 -13
- package/dist/keys/keys.js +1247 -5
- package/dist/keys.d.ts +2 -2
- package/dist/keys.js +2 -1
- package/dist/pem/pem.d.ts +2 -2
- package/dist/pem/pem.js +172 -6
- package/dist/pem.js +2 -1
- package/dist/pkcs/pfx.d.ts +4 -5
- package/dist/pkcs/pfx.js +395 -2
- package/dist/pkcs/pkcs12-mac.d.ts +2 -2
- package/dist/pkcs/pkcs12-mac.js +175 -2
- package/dist/pkcs/pkcs7.d.ts +1 -2
- package/dist/pkcs/pkcs7.js +653 -2
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -1
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js +111 -2
- package/dist/result.js +2 -1
- package/dist/revocation/chain.d.ts +4 -5
- package/dist/revocation/chain.js +536 -2
- package/dist/revocation/crl.d.ts +2 -2
- package/dist/revocation/crl.js +1119 -2
- package/dist/revocation/ocsp.d.ts +5 -4
- package/dist/revocation/ocsp.js +958 -2
- package/dist/revocation/revocation.d.ts +1 -2
- package/dist/revocation/revocation.js +261 -2
- package/dist/revocation.d.ts +2 -2
- package/dist/revocation.js +5 -1
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +270 -2
- package/dist/verify/name-constraints.d.ts +1 -2
- package/dist/verify/policy.d.ts +1 -2
- package/dist/verify/verify.d.ts +1 -2
- package/dist/verify/verify.js +764 -2
- package/dist/verify.js +3 -1
- package/dist/x509/certificate.d.ts +1 -2
- package/dist/x509/certificate.js +151 -2
- package/dist/x509/csr.d.ts +1 -2
- package/dist/x509/csr.js +71 -2
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +387 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +173 -2
- package/dist/x509/parse.d.ts +84 -2
- package/dist/x509/parse.js +1090 -2
- package/dist/x509.d.ts +2 -2
- package/dist/x509.js +6 -1
- package/package.json +74 -34
- package/dist/internal/asn1/asn1.js.map +0 -1
- package/dist/internal/asn1/der.js.map +0 -1
- package/dist/internal/asn1/oids.js.map +0 -1
- package/dist/internal/crypto/algorithm-names.js.map +0 -1
- package/dist/internal/crypto/ecdsa.js.map +0 -1
- package/dist/internal/crypto/hash.js.map +0 -1
- package/dist/internal/crypto/pbes2.d.ts +0 -23
- package/dist/internal/crypto/pbes2.js.map +0 -1
- package/dist/internal/crypto/rsa-pss.js.map +0 -1
- package/dist/internal/crypto/sig-verify.js.map +0 -1
- package/dist/internal/crypto/signing.js.map +0 -1
- package/dist/internal/crypto/webcrypto.js.map +0 -1
- package/dist/internal/shared/base64.js.map +0 -1
- package/dist/internal/shared/dn.js.map +0 -1
- package/dist/internal/shared/ip.js.map +0 -1
- package/dist/internal/verify/name-constraints-engine.js.map +0 -1
- package/dist/internal/verify/policy-engine.js.map +0 -1
- package/dist/internal/verify/verify-path.js.map +0 -1
- package/dist/internal/x509/extension-bits.js.map +0 -1
- package/dist/internal/x509/extension-registry.js.map +0 -1
- package/dist/internal/x509/name-fields.js.map +0 -1
- package/dist/keys/keys.js.map +0 -1
- package/dist/pem/pem.js.map +0 -1
- package/dist/pkcs/pfx.js.map +0 -1
- package/dist/pkcs/pkcs12-mac.js.map +0 -1
- package/dist/pkcs/pkcs7.js.map +0 -1
- package/dist/result/result.js.map +0 -1
- package/dist/revocation/chain.js.map +0 -1
- package/dist/revocation/crl.js.map +0 -1
- package/dist/revocation/ocsp.js.map +0 -1
- package/dist/revocation/revocation.js.map +0 -1
- package/dist/verify/identity.js.map +0 -1
- package/dist/verify/verify.js.map +0 -1
- package/dist/x509/certificate.js.map +0 -1
- package/dist/x509/csr.js.map +0 -1
- package/dist/x509/extensions.js.map +0 -1
- package/dist/x509/name.js.map +0 -1
- package/dist/x509/parse.js.map +0 -1
|
@@ -1,2 +1,286 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
|
2
|
-
|
|
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 };
|