micro509 0.17.1 → 0.19.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/CHANGELOG.md +165 -6
- package/dist/crypto/crypto.d.ts +12 -4
- package/dist/crypto/crypto.js +30 -17
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/crypto.d.ts +8 -2
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der/der.js.map +1 -1
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +84 -16
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/asn1.js +10 -2
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js.map +1 -1
- package/dist/internal/asn1/decode-refusal.js.map +1 -1
- package/dist/internal/asn1/der.d.ts +1 -2
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +71 -353
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids.json.js +357 -0
- package/dist/internal/asn1/oids.json.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +1 -1
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/prime-ec.js +37 -0
- package/dist/internal/crypto/prime-ec.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +50 -50
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +41 -2
- package/dist/internal/crypto/sig-verify.js +125 -38
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/jose/base64url.js.map +1 -1
- package/dist/internal/jose/json-data.js +108 -0
- package/dist/internal/jose/json-data.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +1 -2
- package/dist/internal/jose/strict-json.js.map +1 -1
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna.js.map +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js.map +1 -1
- package/dist/internal/shared/punycode.js.map +1 -1
- package/dist/internal/shared/uri-host.js.map +1 -1
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/spki-distrust.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +234 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/directory-name.js.map +1 -1
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-errors.js.map +1 -1
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name-profile.js.map +1 -1
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/general-name.js.map +1 -1
- package/dist/internal/x509/name-fields.js +1 -1
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js.map +1 -1
- package/dist/jose/base64url.d.ts +4 -0
- package/dist/jose/base64url.js +14 -0
- package/dist/jose/base64url.js.map +1 -0
- package/dist/jose/errors.d.ts +9 -3
- package/dist/jose/errors.js.map +1 -1
- package/dist/jose/jwe.d.ts +64 -0
- package/dist/jose/jwe.js +188 -0
- package/dist/jose/jwe.js.map +1 -0
- package/dist/jose/jwks.d.ts +38 -10
- package/dist/jose/jwks.js +156 -96
- package/dist/jose/jwks.js.map +1 -1
- package/dist/jose/jws.d.ts +8 -6
- package/dist/jose/jws.js +216 -170
- package/dist/jose/jws.js.map +1 -1
- package/dist/jose/jwt.d.ts +8 -2
- package/dist/jose/jwt.js +98 -98
- package/dist/jose/jwt.js.map +1 -1
- package/dist/jose/remote-jwks.d.ts +1 -2
- package/dist/jose/remote-jwks.js +106 -108
- package/dist/jose/remote-jwks.js.map +1 -1
- package/dist/jose.d.ts +15 -4
- package/dist/jose.js +7 -5
- package/dist/kdf/argon2-arithmetic.js.map +1 -1
- package/dist/kdf/argon2-core.js.map +1 -1
- package/dist/kdf/argon2-memory.js.map +1 -1
- package/dist/kdf/argon2-portable.js.map +1 -1
- package/dist/kdf/argon2.d.ts +7 -2
- package/dist/kdf/argon2.js.map +1 -1
- package/dist/kdf.d.ts +6 -0
- package/dist/keys/keys.d.ts +31 -6
- package/dist/keys/keys.js +315 -233
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +11 -2
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +1 -2
- package/dist/pem/pem.js +40 -25
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.d.ts +6 -0
- package/dist/pkcs/pfx.d.ts +2 -3
- package/dist/pkcs/pfx.js +142 -142
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +24 -3
- package/dist/pkcs/pkcs7.js +294 -256
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +10 -1
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +6 -0
- package/dist/revocation/chain.d.ts +69 -16
- package/dist/revocation/chain.js +431 -303
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +67 -14
- package/dist/revocation/crl.js +656 -366
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +78 -24
- package/dist/revocation/ocsp.js +622 -436
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +68 -14
- package/dist/revocation/revocation.js +195 -74
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +14 -5
- package/dist/revocation.js +4 -4
- package/dist/ssh/ssh.d.ts +2 -3
- package/dist/ssh/ssh.js +165 -170
- package/dist/ssh/ssh.js.map +1 -1
- package/dist/ssh/wire.d.ts +1 -2
- package/dist/ssh/wire.js +10 -10
- package/dist/ssh/wire.js.map +1 -1
- package/dist/ssh.d.ts +8 -0
- package/dist/verify/identity.d.ts +3 -4
- package/dist/verify/identity.js +33 -33
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +85 -34
- package/dist/verify/verify.js +276 -226
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +12 -2
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +165 -165
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +1 -2
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +9 -6
- package/dist/x509/parse.js +394 -394
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +32 -33
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/pkcs/pfx.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { OIDS as
|
|
6
|
-
import { createKdfBudget as
|
|
7
|
-
import { base64Encode as
|
|
8
|
-
import { pemEncode as D, splitPemBlocksOrThrow as O } from "../pem/pem.js";
|
|
1
|
+
import { decodeIntegerNumber as e, decodeObjectIdentifier as t, decodeString as n, toHex as r } from "../internal/asn1/asn1.js";
|
|
2
|
+
import { berEncoding as i, berPrimitiveContent as a, berSequenceChildren as o, berStringContent as s, berToDefiniteLength as c, readBerRoot as l } from "../internal/asn1/ber.js";
|
|
3
|
+
import { DECODE_LIMIT_CODES as u, decodeRefusalOf as d } from "../internal/asn1/decode-refusal.js";
|
|
4
|
+
import { bmpString as f, explicitContext as p, integerFromNumber as m, objectIdentifier as h, octetString as g, readElement as _, readRootElement as v, sequence as y, setOf as b, tlv as x } from "../internal/asn1/der.js";
|
|
5
|
+
import { OIDS as S } from "../internal/asn1/oids.js";
|
|
6
|
+
import { createKdfBudget as C, decryptPbes2 as w, encryptPbes2 as T, isKdfIterationLimitError as E, isWrongPasswordError as D } from "../internal/crypto/pbes2.js";
|
|
7
|
+
import { base64Encode as O } from "../internal/shared/base64.js";
|
|
9
8
|
import { exportPkcs8Der as k } from "../keys/keys.js";
|
|
10
|
-
import {
|
|
11
|
-
import { createPkcs12MacData as
|
|
9
|
+
import { pemEncode as A, splitPemBlocksOrThrow as j } from "../pem/pem.js";
|
|
10
|
+
import { createPkcs12MacData as M, parsePkcs12MacData as N } from "./pkcs12-mac.js";
|
|
11
|
+
import { DECODE_REFUSAL_CODES as P, failureResult as F, rethrowIfInvariant as I, throwMicro509Error as L } from "../result/result.js";
|
|
12
12
|
import { parseCertificateDerOrThrow as R } from "../x509/parse.js";
|
|
13
13
|
// src/pkcs/pfx.ts
|
|
14
14
|
/**
|
|
@@ -66,12 +66,12 @@ async function createPfx(e) {
|
|
|
66
66
|
}
|
|
67
67
|
for (let t of e.privateKeys ?? []) n.push(createPrivateKeyBag(await normalizePrivateKey(t.privateKey), t.attributes));
|
|
68
68
|
let r = [];
|
|
69
|
-
if (t.length > 0 && r.push(createDataContentInfo(
|
|
70
|
-
let t =
|
|
69
|
+
if (t.length > 0 && r.push(createDataContentInfo(y(t))), n.length > 0) {
|
|
70
|
+
let t = y(n);
|
|
71
71
|
r.push(e.encryption === void 0 ? createDataContentInfo(t) : await createEncryptedDataContentInfo(t, e.encryption));
|
|
72
72
|
}
|
|
73
|
-
let i =
|
|
74
|
-
|
|
73
|
+
let i = y(r), a = e.mac === void 0 ? void 0 : await M(i, e.mac), o = y([
|
|
74
|
+
m(3),
|
|
75
75
|
createDataContentInfo(i),
|
|
76
76
|
...a === void 0 ? [] : [a.der]
|
|
77
77
|
]);
|
|
@@ -79,14 +79,14 @@ async function createPfx(e) {
|
|
|
79
79
|
ok: !0,
|
|
80
80
|
value: {
|
|
81
81
|
der: o,
|
|
82
|
-
pem:
|
|
83
|
-
base64:
|
|
82
|
+
pem: A("PKCS12", o),
|
|
83
|
+
base64: O(o)
|
|
84
84
|
}
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
/** Shorthand for constructing a PFX creation failure result. */
|
|
88
|
-
function pfxCreationFailure(e,
|
|
89
|
-
return
|
|
88
|
+
function pfxCreationFailure(e, t) {
|
|
89
|
+
return F(e, t);
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
92
|
* Decodes a BER- or DER-encoded PKCS#12/PFX container into its constituent bags.
|
|
@@ -111,41 +111,41 @@ function pfxCreationFailure(e, n) {
|
|
|
111
111
|
* ```
|
|
112
112
|
*/
|
|
113
113
|
async function parsePfxDer(e, t) {
|
|
114
|
-
return await parsePfxDerWithBudget(e, t,
|
|
114
|
+
return await parsePfxDerWithBudget(e, t, C(t));
|
|
115
115
|
}
|
|
116
|
-
async function parsePfxDerWithBudget(t,
|
|
116
|
+
async function parsePfxDerWithBudget(t, n, r) {
|
|
117
117
|
try {
|
|
118
|
-
let
|
|
119
|
-
if (
|
|
120
|
-
let
|
|
121
|
-
if (
|
|
122
|
-
if (
|
|
123
|
-
let
|
|
124
|
-
if (
|
|
125
|
-
let e = await verifyPfxMacData(
|
|
118
|
+
let i = o(l(t));
|
|
119
|
+
if (i.length < 2 || i.length > 3) return pfxFailure("malformed", "Malformed PFX structure");
|
|
120
|
+
let s = i[0], u = i[1];
|
|
121
|
+
if (s === void 0 || u === void 0) return pfxFailure("malformed", "Malformed PFX structure");
|
|
122
|
+
if (e(a(t, s, 2)) !== 3) return pfxFailure("malformed", "Unsupported PFX version");
|
|
123
|
+
let d = extractContentInfoData(t, u), f = i[2], p;
|
|
124
|
+
if (f !== void 0) {
|
|
125
|
+
let e = await verifyPfxMacData(c(t, f), d, n);
|
|
126
126
|
if (e.error !== void 0) return e.error;
|
|
127
|
-
|
|
127
|
+
p = e.macData;
|
|
128
128
|
}
|
|
129
|
-
let
|
|
130
|
-
for (let e of
|
|
131
|
-
let t = await extractSafeContents(
|
|
129
|
+
let m = [];
|
|
130
|
+
for (let e of o(l(d))) {
|
|
131
|
+
let t = await extractSafeContents(d, e, n, r);
|
|
132
132
|
if (t.error !== void 0) return t.error;
|
|
133
|
-
let
|
|
134
|
-
for (let e of
|
|
133
|
+
let i = t.data;
|
|
134
|
+
for (let e of o(t.root)) m.push(parseSafeBag(i, e));
|
|
135
135
|
}
|
|
136
136
|
return {
|
|
137
137
|
ok: !0,
|
|
138
138
|
value: {
|
|
139
|
-
bags:
|
|
140
|
-
certificates:
|
|
141
|
-
privateKeys:
|
|
142
|
-
...
|
|
139
|
+
bags: m,
|
|
140
|
+
certificates: m.flatMap((e) => e.kind === "certificate" ? [e.certificate] : []),
|
|
141
|
+
privateKeys: m.flatMap((e) => e.kind === "privateKey" ? [e.pkcs8Der] : []),
|
|
142
|
+
...p === void 0 ? {} : { macData: p }
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
|
-
} catch (
|
|
146
|
-
|
|
147
|
-
let
|
|
148
|
-
return
|
|
145
|
+
} catch (e) {
|
|
146
|
+
I(e);
|
|
147
|
+
let t = d(e, P);
|
|
148
|
+
return t === void 0 ? pfxFailure("malformed", "Malformed PFX structure") : pfxFailure(t.code, t.message);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
@@ -164,9 +164,9 @@ async function parsePfxDerWithBudget(t, r, i) {
|
|
|
164
164
|
* ```
|
|
165
165
|
*/
|
|
166
166
|
async function parsePfxPem(e, t) {
|
|
167
|
-
let n =
|
|
167
|
+
let n = C(t), r;
|
|
168
168
|
try {
|
|
169
|
-
let t =
|
|
169
|
+
let t = j(e).filter((e) => e.label === "PKCS12"), n = t[0];
|
|
170
170
|
if (n === void 0 || t.length !== 1) return pfxFailure("malformed", "Expected exactly one PKCS12 PEM block");
|
|
171
171
|
r = n.bytes;
|
|
172
172
|
} catch {
|
|
@@ -175,171 +175,171 @@ async function parsePfxPem(e, t) {
|
|
|
175
175
|
return await parsePfxDerWithBudget(r, t, n);
|
|
176
176
|
}
|
|
177
177
|
/** Shorthand for constructing a PFX parse failure result. */
|
|
178
|
-
function pfxFailure(e,
|
|
179
|
-
return
|
|
178
|
+
function pfxFailure(e, t) {
|
|
179
|
+
return F(e, t);
|
|
180
180
|
}
|
|
181
181
|
/** Unwraps a `data` ContentInfo to its inner OCTET STRING payload. */
|
|
182
182
|
function extractContentInfoData(e, t) {
|
|
183
183
|
let { oid: n, content: r } = readContentInfo(e, t);
|
|
184
|
-
if (n !==
|
|
184
|
+
if (n !== S.pkcs7Data) throw Error("Only passwordless data ContentInfo is supported");
|
|
185
185
|
return extractContextOctetString(e, r);
|
|
186
186
|
}
|
|
187
|
-
function readContentInfo(e,
|
|
188
|
-
let
|
|
189
|
-
if (
|
|
187
|
+
function readContentInfo(e, n) {
|
|
188
|
+
let r = o(n), i = r[0], s = r[1];
|
|
189
|
+
if (i === void 0 || s === void 0 || r.length !== 2) throw Error("Malformed ContentInfo");
|
|
190
190
|
return {
|
|
191
|
-
oid:
|
|
192
|
-
content:
|
|
191
|
+
oid: t(a(e, i, 6)),
|
|
192
|
+
content: s
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
195
|
/** Extracts SafeContents bytes from a ContentInfo, decrypting if EncryptedData. */
|
|
196
|
-
async function extractSafeContents(e, t,
|
|
197
|
-
let
|
|
196
|
+
async function extractSafeContents(e, t, n, r) {
|
|
197
|
+
let i, a;
|
|
198
198
|
try {
|
|
199
|
-
({oid:
|
|
199
|
+
({oid: i, content: a} = readContentInfo(e, t));
|
|
200
200
|
} catch (e) {
|
|
201
|
-
|
|
202
|
-
let t =
|
|
201
|
+
I(e);
|
|
202
|
+
let t = d(e, u);
|
|
203
203
|
return { error: t === void 0 ? pfxFailure("malformed", "Malformed ContentInfo") : pfxFailure(t.code, t.message) };
|
|
204
204
|
}
|
|
205
|
-
if (
|
|
206
|
-
let t = extractContextOctetString(e,
|
|
205
|
+
if (i === S.pkcs7Data) {
|
|
206
|
+
let t = extractContextOctetString(e, a);
|
|
207
207
|
return {
|
|
208
208
|
data: t,
|
|
209
|
-
root:
|
|
209
|
+
root: l(t)
|
|
210
210
|
};
|
|
211
211
|
}
|
|
212
|
-
if (
|
|
213
|
-
if (
|
|
214
|
-
let
|
|
212
|
+
if (i !== S.pkcs7EncryptedData) return { error: pfxFailure("malformed", "Unsupported PFX ContentInfo type") };
|
|
213
|
+
if (n?.password === void 0) return { error: pfxFailure("password_required", "Password required for encrypted PFX content") };
|
|
214
|
+
let s = extractContextChild(a), c;
|
|
215
215
|
try {
|
|
216
|
-
|
|
216
|
+
c = await decryptEncryptedData(e, s, n.password, r);
|
|
217
217
|
} catch (e) {
|
|
218
|
-
if (
|
|
219
|
-
if (
|
|
220
|
-
let t =
|
|
218
|
+
if (I(e), D(e)) return { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") };
|
|
219
|
+
if (E(e)) return { error: pfxFailure("kdf_iterations_exceeded", e.message) };
|
|
220
|
+
let t = d(e, u);
|
|
221
221
|
return { error: t === void 0 ? pfxFailure("malformed", "Malformed PFX encrypted content") : pfxFailure(t.code, t.message) };
|
|
222
222
|
}
|
|
223
223
|
try {
|
|
224
|
-
let e =
|
|
225
|
-
return
|
|
226
|
-
data:
|
|
224
|
+
let e = l(c);
|
|
225
|
+
return o(e), {
|
|
226
|
+
data: c,
|
|
227
227
|
root: e
|
|
228
228
|
};
|
|
229
229
|
} catch (e) {
|
|
230
|
-
|
|
231
|
-
let t =
|
|
230
|
+
I(e);
|
|
231
|
+
let t = d(e, u);
|
|
232
232
|
return t === void 0 ? { error: pfxFailure("invalid_password", "Invalid PFX password or encrypted content") } : { error: pfxFailure(t.code, t.message) };
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
235
|
/** Encodes a certBag SafeBag wrapping a DER certificate with optional attributes. */
|
|
236
236
|
function createCertificateBag(e, t) {
|
|
237
|
-
let n =
|
|
238
|
-
return
|
|
239
|
-
|
|
240
|
-
|
|
237
|
+
let n = y([h(S.x509CertificateBagType), p(0, g(e))]);
|
|
238
|
+
return y([
|
|
239
|
+
h(S.pkcs12CertBag),
|
|
240
|
+
p(0, n),
|
|
241
241
|
...encodeBagAttributes(t)
|
|
242
242
|
]);
|
|
243
243
|
}
|
|
244
244
|
/** Encodes a keyBag SafeBag wrapping PKCS#8 DER with optional attributes. */
|
|
245
245
|
function createPrivateKeyBag(e, t) {
|
|
246
|
-
return
|
|
247
|
-
|
|
248
|
-
|
|
246
|
+
return y([
|
|
247
|
+
h(S.pkcs12KeyBag),
|
|
248
|
+
p(0, e),
|
|
249
249
|
...encodeBagAttributes(t)
|
|
250
250
|
]);
|
|
251
251
|
}
|
|
252
252
|
/** Wraps raw bytes in a `pkcs7-data` ContentInfo SEQUENCE. */
|
|
253
253
|
function createDataContentInfo(e) {
|
|
254
|
-
return
|
|
254
|
+
return y([h(S.pkcs7Data), p(0, g(e))]);
|
|
255
255
|
}
|
|
256
256
|
/** PBES2-encrypts data and wraps it in a `pkcs7-encryptedData` ContentInfo. */
|
|
257
257
|
async function createEncryptedDataContentInfo(e, t) {
|
|
258
|
-
let n = await
|
|
259
|
-
|
|
258
|
+
let n = await T(e, t), r = y([m(0), y([
|
|
259
|
+
h(S.pkcs7Data),
|
|
260
260
|
n.algorithmIdentifierDer,
|
|
261
|
-
|
|
261
|
+
x(128, n.encryptedData)
|
|
262
262
|
])]);
|
|
263
|
-
return
|
|
263
|
+
return y([h(S.pkcs7EncryptedData), p(0, r)]);
|
|
264
264
|
}
|
|
265
265
|
/** DER-encodes optional friendlyName / localKeyId into a SET OF SEQUENCE. */
|
|
266
266
|
function encodeBagAttributes(e) {
|
|
267
267
|
if (e === void 0) return [];
|
|
268
268
|
let t = [];
|
|
269
|
-
return e.friendlyName !== void 0 && t.push(
|
|
269
|
+
return e.friendlyName !== void 0 && t.push(y([h(S.friendlyName), b([encodeFriendlyName(e.friendlyName)])])), e.localKeyId !== void 0 && t.push(y([h(S.localKeyId), b([g(e.localKeyId)])])), t.length === 0 ? [] : [b(t)];
|
|
270
270
|
}
|
|
271
271
|
function encodeFriendlyName(e) {
|
|
272
|
-
let refuse = () =>
|
|
272
|
+
let refuse = () => L("invalid_friendly_name", "friendlyName must be a BMPString of 1 to 255 characters, none of them a surrogate, U+FFFE or U+FFFF");
|
|
273
273
|
if (e.length === 0 || e.length > 255) return refuse();
|
|
274
274
|
try {
|
|
275
|
-
return
|
|
275
|
+
return f(e);
|
|
276
276
|
} catch {
|
|
277
277
|
return refuse();
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
280
|
/** Decodes a single SafeBag into a {@linkcode ParsedPfxBag} discriminated union. */
|
|
281
|
-
function parseSafeBag(e,
|
|
282
|
-
let
|
|
283
|
-
if (
|
|
284
|
-
let
|
|
285
|
-
if (
|
|
286
|
-
let
|
|
287
|
-
if (
|
|
281
|
+
function parseSafeBag(e, n) {
|
|
282
|
+
let r = o(n), s = r[0], l = r[1], u = r[2];
|
|
283
|
+
if (s === void 0 || l === void 0 || r.length > 3) throw Error("Malformed SafeBag");
|
|
284
|
+
let d = t(a(e, s, 6)), f = parseBagAttributes(e, u), p = extractContextChild(l);
|
|
285
|
+
if (d === S.pkcs12CertBag) {
|
|
286
|
+
let n = o(p), r = n[0], i = n[1];
|
|
287
|
+
if (r === void 0 || i === void 0 || n.length !== 2 || t(a(e, r, 6)) !== S.x509CertificateBagType) throw Error("Malformed certBag");
|
|
288
288
|
return {
|
|
289
289
|
kind: "certificate",
|
|
290
|
-
bagId:
|
|
291
|
-
attributes:
|
|
292
|
-
certificate: R(extractContextOctetString(e,
|
|
290
|
+
bagId: d,
|
|
291
|
+
attributes: f,
|
|
292
|
+
certificate: R(extractContextOctetString(e, i))
|
|
293
293
|
};
|
|
294
294
|
}
|
|
295
|
-
if (
|
|
296
|
-
let t =
|
|
297
|
-
return
|
|
295
|
+
if (d === S.pkcs12KeyBag) {
|
|
296
|
+
let t = i(e, p);
|
|
297
|
+
return v(t), {
|
|
298
298
|
kind: "privateKey",
|
|
299
|
-
bagId:
|
|
300
|
-
attributes:
|
|
299
|
+
bagId: d,
|
|
300
|
+
attributes: f,
|
|
301
301
|
pkcs8Der: t
|
|
302
302
|
};
|
|
303
303
|
}
|
|
304
304
|
return {
|
|
305
305
|
kind: "unknown",
|
|
306
|
-
bagId:
|
|
307
|
-
attributes:
|
|
308
|
-
valueDer:
|
|
306
|
+
bagId: d,
|
|
307
|
+
attributes: f,
|
|
308
|
+
valueDer: c(e, p)
|
|
309
309
|
};
|
|
310
310
|
}
|
|
311
311
|
/** Decodes the SET OF attributes attached to a SafeBag. */
|
|
312
|
-
function parseBagAttributes(e,
|
|
313
|
-
if (
|
|
314
|
-
if (
|
|
315
|
-
let
|
|
316
|
-
for (let
|
|
317
|
-
let
|
|
318
|
-
if (
|
|
319
|
-
let
|
|
320
|
-
if (
|
|
321
|
-
oid:
|
|
322
|
-
valuesHex:
|
|
323
|
-
}),
|
|
324
|
-
let e =
|
|
325
|
-
if (
|
|
326
|
-
|
|
327
|
-
} else if (
|
|
328
|
-
let e =
|
|
329
|
-
if (
|
|
330
|
-
|
|
312
|
+
function parseBagAttributes(e, n) {
|
|
313
|
+
if (n === void 0) return { entries: [] };
|
|
314
|
+
if (n.tag !== 49) throw Error("Malformed PFX bag attributes");
|
|
315
|
+
let i = [], s, l;
|
|
316
|
+
for (let u of n.children) {
|
|
317
|
+
let n = o(u), d = n[0], f = n[1];
|
|
318
|
+
if (d === void 0 || f === void 0 || n.length !== 2 || f.tag !== 49) throw Error("Malformed PFX bag attribute");
|
|
319
|
+
let p = t(a(e, d, 6)), m = f.children.map((t) => c(e, t));
|
|
320
|
+
if (i.push({
|
|
321
|
+
oid: p,
|
|
322
|
+
valuesHex: m.map((e) => r(e))
|
|
323
|
+
}), p === S.friendlyName) {
|
|
324
|
+
let e = m[0];
|
|
325
|
+
if (s !== void 0 || m.length !== 1 || e === void 0) throw Error("Malformed friendlyName attribute");
|
|
326
|
+
s = decodeFriendlyName(e);
|
|
327
|
+
} else if (p === S.localKeyId) {
|
|
328
|
+
let e = m[0];
|
|
329
|
+
if (l !== void 0 || m.length !== 1 || e === void 0) throw Error("Malformed localKeyId attribute");
|
|
330
|
+
l = decodeLocalKeyId(e);
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
333
|
return {
|
|
334
|
-
entries:
|
|
335
|
-
...
|
|
336
|
-
...
|
|
334
|
+
entries: i,
|
|
335
|
+
...s === void 0 ? {} : { friendlyName: s },
|
|
336
|
+
...l === void 0 ? {} : { localKeyId: l }
|
|
337
337
|
};
|
|
338
338
|
}
|
|
339
339
|
function decodeLocalKeyId(e) {
|
|
340
|
-
let t =
|
|
340
|
+
let t = _(e);
|
|
341
341
|
if (t.tag !== 4) throw Error("localKeyId must use OCTET STRING");
|
|
342
|
-
return
|
|
342
|
+
return r(t.value);
|
|
343
343
|
}
|
|
344
344
|
/** Exports a `CryptoKey` to PKCS#8 DER, or passes raw bytes through. */
|
|
345
345
|
function normalizePrivateKey(e) {
|
|
@@ -348,7 +348,7 @@ function normalizePrivateKey(e) {
|
|
|
348
348
|
/** Extracts DER bytes from a PEM string, or passes raw DER through. */
|
|
349
349
|
function normalizeCertificate(e) {
|
|
350
350
|
if (typeof e == "string") {
|
|
351
|
-
let t =
|
|
351
|
+
let t = j(e).find((e) => e.label === "CERTIFICATE");
|
|
352
352
|
if (t === void 0) throw Error("Certificate PEM required");
|
|
353
353
|
return new Uint8Array(t.bytes);
|
|
354
354
|
}
|
|
@@ -356,20 +356,20 @@ function normalizeCertificate(e) {
|
|
|
356
356
|
}
|
|
357
357
|
/** Parses the MacData block and verifies it against the AuthenticatedSafe when a password is given. */
|
|
358
358
|
async function verifyPfxMacData(e, t, n) {
|
|
359
|
-
let r = await
|
|
359
|
+
let r = await N(e, t, n?.macPassword ?? n?.password, n);
|
|
360
360
|
return r.ok ? r.value.verification === "invalid" ? { error: pfxFailure("invalid_password", "Invalid PFX MAC password or corrupted content") } : { macData: r.value } : { error: r.code === "malformed" ? pfxFailure("malformed", "Malformed PFX MacData") : pfxFailure(r.code, r.message) };
|
|
361
361
|
}
|
|
362
362
|
/** Decrypts a PKCS#7 EncryptedData structure using PBES2 with the given password. */
|
|
363
|
-
function decryptEncryptedData(
|
|
364
|
-
let
|
|
365
|
-
if (
|
|
366
|
-
let
|
|
367
|
-
if (
|
|
368
|
-
return
|
|
363
|
+
function decryptEncryptedData(t, n, r, i) {
|
|
364
|
+
let l = o(n), u = l[0], d = l[1];
|
|
365
|
+
if (l.length !== 2 || u === void 0 || d === void 0 || e(a(t, u, 2)) !== 0) throw Error("Malformed EncryptedData");
|
|
366
|
+
let f = o(d), p = f[0], m = f[1], h = f[2];
|
|
367
|
+
if (p === void 0 || m === void 0 || h === void 0 || f.length !== 3) throw Error("Malformed EncryptedContentInfo");
|
|
368
|
+
return a(t, p, 6), w(c(t, m), s(t, h, 128, 4), r, i);
|
|
369
369
|
}
|
|
370
370
|
/** Reads the OCTET STRING inside a `[0] EXPLICIT` wrapper. */
|
|
371
371
|
function extractContextOctetString(e, t) {
|
|
372
|
-
return
|
|
372
|
+
return s(e, extractContextChild(t), 4);
|
|
373
373
|
}
|
|
374
374
|
/** Reads the single child element inside a `[0] EXPLICIT` wrapper. */
|
|
375
375
|
function extractContextChild(e) {
|
|
@@ -380,11 +380,11 @@ function extractContextChild(e) {
|
|
|
380
380
|
}
|
|
381
381
|
/** RFC 2985 §5.5.1: a friendlyName value is a BMPString of SIZE (1..255). */
|
|
382
382
|
function decodeFriendlyName(e) {
|
|
383
|
-
let t =
|
|
383
|
+
let t = _(e);
|
|
384
384
|
if (t.tag !== 30) throw Error("friendlyName must be a BMPString");
|
|
385
|
-
let
|
|
386
|
-
if (
|
|
387
|
-
return
|
|
385
|
+
let r = n(30, t.value);
|
|
386
|
+
if (r.length < 1 || r.length > 255) throw Error("friendlyName must hold 1 to 255 characters");
|
|
387
|
+
return r;
|
|
388
388
|
}
|
|
389
389
|
export { createPfx, parsePfxDer, parsePfxPem };
|
|
390
390
|
|