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