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