micro509 0.17.0 → 0.18.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 +123 -6
- package/README.md +3 -0
- package/dist/crypto/crypto.d.ts +2 -3
- package/dist/crypto.d.ts +6 -0
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +82 -15
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/der.d.ts +1 -2
- 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/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- 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/sig-verify.d.ts +1 -2
- package/dist/internal/crypto/sig-verify.js +60 -60
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- 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/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +19 -19
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/name-fields.js +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/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 +15 -0
- package/dist/kdf/argon2-arithmetic.js.map +1 -0
- package/dist/kdf/argon2-core.js +486 -0
- package/dist/kdf/argon2-core.js.map +1 -0
- package/dist/kdf/argon2-memory.js +20 -0
- package/dist/kdf/argon2-memory.js.map +1 -0
- package/dist/kdf/argon2-portable.js +18 -0
- package/dist/kdf/argon2-portable.js.map +1 -0
- package/dist/kdf/argon2.d.ts +22 -11
- package/dist/kdf/argon2.js +32 -13
- 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/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs7.d.ts +14 -3
- package/dist/pkcs/pkcs7.js +268 -248
- 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.d.ts +6 -0
- package/dist/revocation/chain.d.ts +2 -3
- package/dist/revocation/chain.js +52 -52
- package/dist/revocation/crl.d.ts +2 -3
- package/dist/revocation/crl.js +319 -319
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +2 -3
- package/dist/revocation/ocsp.js +395 -395
- package/dist/revocation/revocation.d.ts +2 -3
- package/dist/revocation/revocation.js +22 -22
- package/dist/revocation.d.ts +12 -3
- 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.d.ts +8 -0
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +31 -31
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +3 -4
- package/dist/verify/verify.js +121 -121
- package/dist/verify.d.ts +9 -0
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- 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/name-text.d.ts +1 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/parse.d.ts +3 -4
- package/dist/x509/parse.js +423 -423
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +27 -34
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/jose/jws.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { OIDS as
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { ecdsaSignatureDerToRaw as e, signData as t, verifySignatureWithKey as n } from "../crypto/crypto.js";
|
|
2
|
+
import { OIDS as r } from "../internal/asn1/oids.js";
|
|
3
|
+
import { base64UrlDecode as i, base64UrlEncode as a } from "../internal/jose/base64url.js";
|
|
4
|
+
import { isJsonArray as o, isJsonObject as s, parseStrictJson as c } from "../internal/jose/strict-json.js";
|
|
5
|
+
import { joseFailure as l } from "./errors.js";
|
|
6
|
+
import { successResult as u, throwMicro509Error as d } from "../result/result.js";
|
|
7
7
|
// src/jose/jws.ts
|
|
8
8
|
/**
|
|
9
9
|
* Compact JWS (RFC 7515) parsing, signing and verification with an explicit algorithm and token
|
|
@@ -13,14 +13,56 @@ import { joseFailure as u } from "./errors.js";
|
|
|
13
13
|
* @module
|
|
14
14
|
*/
|
|
15
15
|
/** Every {@linkcode JwsAlgorithm}. */
|
|
16
|
-
const
|
|
16
|
+
const f = [
|
|
17
|
+
"RS256",
|
|
18
|
+
"RS384",
|
|
19
|
+
"RS512",
|
|
20
|
+
"ES256",
|
|
21
|
+
"ES384"
|
|
22
|
+
], p = 2048, m = p, h = {
|
|
23
|
+
RS256: {
|
|
24
|
+
kind: "rsa",
|
|
25
|
+
hash: "SHA-256",
|
|
26
|
+
oid: r.sha256WithRSAEncryption
|
|
27
|
+
},
|
|
28
|
+
RS384: {
|
|
29
|
+
kind: "rsa",
|
|
30
|
+
hash: "SHA-384",
|
|
31
|
+
oid: r.sha384WithRSAEncryption
|
|
32
|
+
},
|
|
33
|
+
RS512: {
|
|
34
|
+
kind: "rsa",
|
|
35
|
+
hash: "SHA-512",
|
|
36
|
+
oid: r.sha512WithRSAEncryption
|
|
37
|
+
},
|
|
38
|
+
ES256: {
|
|
39
|
+
kind: "ec",
|
|
40
|
+
hash: "SHA-256",
|
|
41
|
+
curve: "P-256",
|
|
42
|
+
coordinateBytes: 32,
|
|
43
|
+
oid: r.ecdsaWithSHA256
|
|
44
|
+
},
|
|
45
|
+
ES384: {
|
|
46
|
+
kind: "ec",
|
|
47
|
+
hash: "SHA-384",
|
|
48
|
+
curve: "P-384",
|
|
49
|
+
coordinateBytes: 48,
|
|
50
|
+
oid: r.ecdsaWithSHA384
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/** The exact key and signature profile for an admitted JWS algorithm. */
|
|
54
|
+
function jwsAlgorithmProfile(e) {
|
|
55
|
+
return h[e];
|
|
56
|
+
}
|
|
57
|
+
/** The limits applied when a caller supplies none. */
|
|
58
|
+
const g = {
|
|
17
59
|
maxTokenLength: 32768,
|
|
18
60
|
maxHeaderBytes: 4096,
|
|
19
61
|
maxPayloadBytes: 16384,
|
|
20
62
|
maxSignatureBytes: 1024,
|
|
21
63
|
maxJsonDepth: 16,
|
|
22
64
|
maxJsonNodes: 2048
|
|
23
|
-
},
|
|
65
|
+
}, _ = [
|
|
24
66
|
"maxTokenLength",
|
|
25
67
|
"maxHeaderBytes",
|
|
26
68
|
"maxPayloadBytes",
|
|
@@ -33,19 +75,19 @@ const d = ["RS256"], f = 2048, p = {
|
|
|
33
75
|
* `undefined` field takes its default; any other value must be a positive safe integer, and
|
|
34
76
|
* `maxJsonDepth` may not exceed {@linkcode MAX_JSON_DEPTH_LIMIT}.
|
|
35
77
|
*/
|
|
36
|
-
function resolveJoseLimits(
|
|
37
|
-
if (
|
|
38
|
-
let
|
|
39
|
-
for (let
|
|
40
|
-
let r =
|
|
78
|
+
function resolveJoseLimits(e) {
|
|
79
|
+
if (e !== void 0 && (typeof e != "object" || !e)) return l("invalid_options", "limits must be an object");
|
|
80
|
+
let t = { ...g };
|
|
81
|
+
for (let n of _) {
|
|
82
|
+
let r = e?.[n];
|
|
41
83
|
if (r === void 0) continue;
|
|
42
|
-
let i =
|
|
43
|
-
if (!Number.isSafeInteger(r) || r < 1 || r > i) return
|
|
44
|
-
n
|
|
84
|
+
let i = n === "maxJsonDepth" ? 64 : 2 ** 53 - 1;
|
|
85
|
+
if (!Number.isSafeInteger(r) || r < 1 || r > i) return l("invalid_options", `limits.${n} must be a positive safe integer`);
|
|
86
|
+
t[n] = r;
|
|
45
87
|
}
|
|
46
|
-
return
|
|
88
|
+
return u(t);
|
|
47
89
|
}
|
|
48
|
-
const
|
|
90
|
+
const v = new TextEncoder(), y = new TextDecoder("utf-8", {
|
|
49
91
|
fatal: !0,
|
|
50
92
|
ignoreBOM: !0
|
|
51
93
|
});
|
|
@@ -53,67 +95,67 @@ function maxEncodedLength(e) {
|
|
|
53
95
|
return Math.ceil(e * 4 / 3);
|
|
54
96
|
}
|
|
55
97
|
function parseFailure(e, t) {
|
|
56
|
-
return
|
|
98
|
+
return l(e, t);
|
|
57
99
|
}
|
|
58
|
-
function decodeSegment(t, n
|
|
59
|
-
if (
|
|
60
|
-
let
|
|
61
|
-
return
|
|
100
|
+
function decodeSegment(e, t, n) {
|
|
101
|
+
if (e.length > maxEncodedLength(t)) return parseFailure("limit_exceeded", `JWS ${n} exceeds ${t} bytes`);
|
|
102
|
+
let r = i(e);
|
|
103
|
+
return r === void 0 ? parseFailure("invalid_encoding", `JWS ${n} is not canonical base64url`) : u(r);
|
|
62
104
|
}
|
|
63
105
|
/** Decodes UTF-8 strictly and parses it as a JSON object with no duplicate members. */
|
|
64
|
-
function decodeJsonObject(t, n, r, i) {
|
|
106
|
+
function decodeJsonObject(e, t, n, r, i = "JWS") {
|
|
65
107
|
let a;
|
|
66
108
|
try {
|
|
67
|
-
a =
|
|
109
|
+
a = y.decode(e);
|
|
68
110
|
} catch {
|
|
69
|
-
return parseFailure("invalid_utf8",
|
|
111
|
+
return parseFailure("invalid_utf8", `${i} ${n} is not valid UTF-8`);
|
|
70
112
|
}
|
|
71
|
-
let o =
|
|
72
|
-
maxDepth:
|
|
73
|
-
maxNodes:
|
|
113
|
+
let o = c(a, {
|
|
114
|
+
maxDepth: t.maxJsonDepth,
|
|
115
|
+
maxNodes: t.maxJsonNodes
|
|
74
116
|
});
|
|
75
|
-
if (!o.ok) return parseFailure(o.code,
|
|
76
|
-
let
|
|
77
|
-
return
|
|
117
|
+
if (!o.ok) return parseFailure(o.code, `${i} ${n}: ${o.message}`);
|
|
118
|
+
let l = o.value;
|
|
119
|
+
return s(l) ? u(l) : parseFailure(r, `${i} ${n} is not a JSON object`);
|
|
78
120
|
}
|
|
79
|
-
function optionalString(
|
|
80
|
-
if (!Object.hasOwn(
|
|
81
|
-
let
|
|
82
|
-
return typeof
|
|
121
|
+
function optionalString(e, t) {
|
|
122
|
+
if (!Object.hasOwn(e, t)) return u(void 0);
|
|
123
|
+
let n = e[t];
|
|
124
|
+
return typeof n == "string" ? u(n) : parseFailure("invalid_header", `JWS header "${t}" is not a string`);
|
|
83
125
|
}
|
|
84
|
-
function criticalNames(
|
|
85
|
-
if (!Object.hasOwn(
|
|
86
|
-
let
|
|
87
|
-
if (!
|
|
88
|
-
let
|
|
89
|
-
for (let e of
|
|
90
|
-
if (typeof e != "string" ||
|
|
91
|
-
|
|
126
|
+
function criticalNames(e) {
|
|
127
|
+
if (!Object.hasOwn(e, "crit")) return u([]);
|
|
128
|
+
let t = e.crit;
|
|
129
|
+
if (!o(t) || t.length === 0) return parseFailure("invalid_header", "JWS header \"crit\" is not a non-empty array");
|
|
130
|
+
let n = [];
|
|
131
|
+
for (let e of t) {
|
|
132
|
+
if (typeof e != "string" || n.includes(e)) return parseFailure("invalid_header", "JWS header \"crit\" holds a non-string or repeated name");
|
|
133
|
+
n.push(e);
|
|
92
134
|
}
|
|
93
|
-
return
|
|
135
|
+
return u(n);
|
|
94
136
|
}
|
|
95
|
-
function parseHeader(
|
|
96
|
-
let
|
|
97
|
-
if (!
|
|
98
|
-
let
|
|
99
|
-
if (typeof
|
|
100
|
-
let
|
|
137
|
+
function parseHeader(e, t) {
|
|
138
|
+
let n = decodeJsonObject(e, t, "header", "invalid_header");
|
|
139
|
+
if (!n.ok) return n;
|
|
140
|
+
let r = n.value, i = r.alg;
|
|
141
|
+
if (typeof i != "string" || i.length === 0) return parseFailure("invalid_header", "JWS header \"alg\" is missing or not a string");
|
|
142
|
+
let a = optionalString(r, "typ");
|
|
143
|
+
if (!a.ok) return a;
|
|
144
|
+
let o = optionalString(r, "kid");
|
|
101
145
|
if (!o.ok) return o;
|
|
102
|
-
let s = optionalString(
|
|
146
|
+
let s = optionalString(r, "cty");
|
|
103
147
|
if (!s.ok) return s;
|
|
104
|
-
let c =
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
...
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
members: i
|
|
116
|
-
}) : l;
|
|
148
|
+
let c = criticalNames(r);
|
|
149
|
+
return c.ok ? r.b64 === !1 || c.value.includes("b64") ? parseFailure("unsupported_unencoded_payload", "Unencoded and detached payloads (RFC 7797) are not supported") : Object.hasOwn(r, "b64") && typeof r.b64 != "boolean" ? parseFailure("invalid_header", "JWS header \"b64\" is not a boolean") : c.value.length > 0 ? l("unsupported_critical_header", "JWS header lists critical extensions", {
|
|
150
|
+
alg: i,
|
|
151
|
+
crit: c.value
|
|
152
|
+
}) : u({
|
|
153
|
+
alg: i,
|
|
154
|
+
...a.value === void 0 ? {} : { typ: a.value },
|
|
155
|
+
...o.value === void 0 ? {} : { kid: o.value },
|
|
156
|
+
...s.value === void 0 ? {} : { cty: s.value },
|
|
157
|
+
members: r
|
|
158
|
+
}) : c;
|
|
117
159
|
}
|
|
118
160
|
/**
|
|
119
161
|
* Parses a compact JWS without checking its signature.
|
|
@@ -127,25 +169,25 @@ function parseCompactJws(e, t) {
|
|
|
127
169
|
let n = resolveJoseLimits(t);
|
|
128
170
|
return n.ok ? parseWithLimits(e, n.value) : n;
|
|
129
171
|
}
|
|
130
|
-
function parseWithLimits(
|
|
131
|
-
if (typeof
|
|
132
|
-
if (
|
|
133
|
-
let
|
|
134
|
-
if (
|
|
135
|
-
let
|
|
172
|
+
function parseWithLimits(e, t) {
|
|
173
|
+
if (typeof e != "string") return parseFailure("malformed_token", "JWS token is not a string");
|
|
174
|
+
if (e.length > t.maxTokenLength) return parseFailure("token_too_large", `JWS token exceeds ${t.maxTokenLength} characters`);
|
|
175
|
+
let n = e.split("."), [r, i, a] = n;
|
|
176
|
+
if (n.length !== 3 || r === void 0 || i === void 0 || a === void 0 || r.length === 0) return parseFailure("malformed_token", "JWS compact serialization needs three segments");
|
|
177
|
+
let o = decodeSegment(r, t.maxHeaderBytes, "header");
|
|
178
|
+
if (!o.ok) return o;
|
|
179
|
+
let s = parseHeader(o.value, t);
|
|
136
180
|
if (!s.ok) return s;
|
|
137
|
-
let c =
|
|
181
|
+
let c = decodeSegment(i, t.maxPayloadBytes, "payload");
|
|
138
182
|
if (!c.ok) return c;
|
|
139
|
-
let l = decodeSegment(a,
|
|
140
|
-
|
|
141
|
-
let u = decodeSegment(o, n.maxSignatureBytes, "signature");
|
|
142
|
-
return u.ok ? e({
|
|
183
|
+
let l = decodeSegment(a, t.maxSignatureBytes, "signature");
|
|
184
|
+
return l.ok ? u({
|
|
143
185
|
verified: !1,
|
|
144
|
-
header:
|
|
145
|
-
payload:
|
|
146
|
-
signingInput:
|
|
147
|
-
signature:
|
|
148
|
-
}) :
|
|
186
|
+
header: s.value,
|
|
187
|
+
payload: c.value,
|
|
188
|
+
signingInput: v.encode(`${r}.${i}`),
|
|
189
|
+
signature: l.value
|
|
190
|
+
}) : l;
|
|
149
191
|
}
|
|
150
192
|
/**
|
|
151
193
|
* Comparison key for a media type per RFC 7515 §4.1.9: type, subtype and parameter names fold to
|
|
@@ -185,7 +227,7 @@ function matchesTokenType(e, t) {
|
|
|
185
227
|
}
|
|
186
228
|
}
|
|
187
229
|
function isAlgorithmAdmitted(e, t) {
|
|
188
|
-
return t.includes(e) &&
|
|
230
|
+
return t.includes(e) && f.some((t) => t === e);
|
|
189
231
|
}
|
|
190
232
|
/** Checks the observable RSA exponent constraints in RFC 8017 section 3.1. */
|
|
191
233
|
function validRsaPublicExponent(e, t) {
|
|
@@ -201,77 +243,79 @@ function validRsaPublicExponent(e, t) {
|
|
|
201
243
|
}
|
|
202
244
|
return !1;
|
|
203
245
|
}
|
|
204
|
-
/**
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
function
|
|
246
|
+
/** Checks the key profile and returns the exact JOSE signature length (RFC 7518 §§3.3–3.4). */
|
|
247
|
+
function checkJwsVerificationKey(e, t) {
|
|
248
|
+
return checkJwsKey(e, t, "public");
|
|
249
|
+
}
|
|
250
|
+
function checkJwsKey(e, t, n) {
|
|
209
251
|
try {
|
|
210
|
-
return
|
|
252
|
+
return inspectJwsKey(e, t, n);
|
|
211
253
|
} catch {
|
|
212
|
-
return
|
|
254
|
+
return l("key_unsuitable", "The key cannot be inspected");
|
|
213
255
|
}
|
|
214
256
|
}
|
|
215
|
-
function
|
|
216
|
-
let
|
|
217
|
-
|
|
257
|
+
function inspectJwsKey(e, t, n) {
|
|
258
|
+
let r = jwsAlgorithmProfile(t), i = n === "public" ? "verify" : "sign";
|
|
259
|
+
if (e.type !== n || !e.usages.includes(i)) return l("key_unsuitable", `${t} needs a ${n} key with ${i} usage`);
|
|
260
|
+
let a = e.algorithm;
|
|
261
|
+
return r.kind === "ec" ? a.name !== "ECDSA" || !("namedCurve" in a) || a.namedCurve !== r.curve ? l("key_unsuitable", `${t} needs an ECDSA ${r.curve} key`) : u(r.coordinateBytes * 2) : a.name !== "RSASSA-PKCS1-v1_5" || !("hash" in a) || typeof a.hash != "object" || a.hash === null || !("name" in a.hash) || a.hash.name !== r.hash ? l("key_unsuitable", `${t} needs an RSASSA-PKCS1-v1_5 ${r.hash} key`) : !("modulusLength" in a) || typeof a.modulusLength != "number" || a.modulusLength < 2048 ? l("key_unsuitable", `${t} needs an RSA modulus of at least ${m} bits`) : !("publicExponent" in a) || !(a.publicExponent instanceof Uint8Array) || !validRsaPublicExponent(a.publicExponent) ? l("key_unsuitable", `${t} needs a valid odd RSA public exponent of at least 3`) : u(Math.ceil(a.modulusLength / 8));
|
|
218
262
|
}
|
|
219
263
|
function isCryptoKey(e) {
|
|
220
264
|
return e instanceof CryptoKey;
|
|
221
265
|
}
|
|
222
266
|
/** Validates and copies verification options. Everything the run reads is captured here. */
|
|
223
|
-
function prepareVerify(
|
|
224
|
-
if (typeof
|
|
225
|
-
let { algorithms:
|
|
226
|
-
if (!Array.isArray(
|
|
227
|
-
let
|
|
228
|
-
for (let e of
|
|
229
|
-
let t =
|
|
230
|
-
if (t === void 0) return
|
|
231
|
-
|
|
267
|
+
function prepareVerify(e) {
|
|
268
|
+
if (typeof e != "object" || !e) return l("invalid_options", "Verification options are required");
|
|
269
|
+
let { algorithms: t, key: n, tokenType: r, limits: i, signal: a } = e;
|
|
270
|
+
if (!Array.isArray(t) || t.length === 0) return l("invalid_options", "An explicit, non-empty algorithm list is required");
|
|
271
|
+
let o = [];
|
|
272
|
+
for (let e of t) {
|
|
273
|
+
let t = f.find((t) => t === e);
|
|
274
|
+
if (t === void 0) return l("invalid_options", "The algorithm list names an unsupported algorithm");
|
|
275
|
+
o.push(t);
|
|
232
276
|
}
|
|
233
|
-
if (typeof
|
|
234
|
-
let
|
|
235
|
-
if (
|
|
236
|
-
if (
|
|
237
|
-
let
|
|
238
|
-
return
|
|
239
|
-
algorithms:
|
|
240
|
-
key:
|
|
241
|
-
typeRule:
|
|
242
|
-
limits:
|
|
243
|
-
signal:
|
|
244
|
-
}) :
|
|
277
|
+
if (typeof n != "function" && !isCryptoKey(n)) return l("invalid_options", "A key or key resolver is required");
|
|
278
|
+
let s = normalizeTokenType(r);
|
|
279
|
+
if (s === void 0) return l("invalid_options", "A token type policy is required");
|
|
280
|
+
if (a !== void 0 && !(a instanceof AbortSignal)) return l("invalid_options", "signal must be an AbortSignal");
|
|
281
|
+
let c = resolveJoseLimits(i);
|
|
282
|
+
return c.ok ? u({
|
|
283
|
+
algorithms: o,
|
|
284
|
+
key: n,
|
|
285
|
+
typeRule: s,
|
|
286
|
+
limits: c.value,
|
|
287
|
+
signal: a
|
|
288
|
+
}) : c;
|
|
245
289
|
}
|
|
246
|
-
async function resolveKey(
|
|
247
|
-
let { key:
|
|
248
|
-
if (typeof
|
|
249
|
-
let
|
|
290
|
+
async function resolveKey(e, t) {
|
|
291
|
+
let { key: n, signal: r } = e;
|
|
292
|
+
if (typeof n != "function") return u(n);
|
|
293
|
+
let i;
|
|
250
294
|
try {
|
|
251
|
-
|
|
252
|
-
header:
|
|
253
|
-
algorithm:
|
|
254
|
-
...
|
|
255
|
-
...
|
|
295
|
+
i = await n({
|
|
296
|
+
header: t,
|
|
297
|
+
algorithm: t.alg,
|
|
298
|
+
...t.kid === void 0 ? {} : { kid: t.kid },
|
|
299
|
+
...r === void 0 ? {} : { signal: r }
|
|
256
300
|
});
|
|
257
301
|
} catch {
|
|
258
|
-
return
|
|
302
|
+
return r?.aborted === !0 ? l("aborted", "Key resolution was cancelled") : l("key_resolution_failed", "The key resolver failed");
|
|
259
303
|
}
|
|
260
|
-
if (typeof
|
|
261
|
-
if (Reflect.get(
|
|
262
|
-
let
|
|
263
|
-
return isCryptoKey(
|
|
304
|
+
if (typeof i != "object" || !i) return l("key_resolution_failed", "The key resolver returned no result");
|
|
305
|
+
if (Reflect.get(i, "ok") === !0) {
|
|
306
|
+
let e = Reflect.get(i, "value");
|
|
307
|
+
return isCryptoKey(e) ? u(e) : l("key_resolution_failed", "The key resolver returned no key");
|
|
264
308
|
}
|
|
265
|
-
let
|
|
266
|
-
switch (
|
|
309
|
+
let a = Reflect.get(i, "code");
|
|
310
|
+
switch (a) {
|
|
267
311
|
case "key_not_found":
|
|
268
312
|
case "ambiguous_key":
|
|
269
313
|
case "key_unsuitable":
|
|
270
314
|
case "key_resolution_failed":
|
|
271
315
|
case "jwks_unavailable":
|
|
272
316
|
case "jwks_malformed":
|
|
273
|
-
case "aborted": return
|
|
274
|
-
default: return
|
|
317
|
+
case "aborted": return l(a, `Key resolution failed: ${a}`, resolverDetails(i));
|
|
318
|
+
default: return l("key_resolution_failed", "The key resolver returned an unknown failure");
|
|
275
319
|
}
|
|
276
320
|
}
|
|
277
321
|
function resolverDetails(e) {
|
|
@@ -288,8 +332,9 @@ function resolverDetails(e) {
|
|
|
288
332
|
*
|
|
289
333
|
* The caller's `algorithms` list decides what is accepted; the token's `alg` only selects among
|
|
290
334
|
* them, and `none` is always refused as `unsigned_token`. The `tokenType` policy is applied before
|
|
291
|
-
* any key is resolved.
|
|
292
|
-
*
|
|
335
|
+
* any key is resolved. The key must match the admitted algorithm's RSA hash (at least 2048 bits)
|
|
336
|
+
* or ECDSA curve. ECDSA signatures must be fixed-width raw R || S, with no DER retry.
|
|
337
|
+
* Header members such as `jku`, `jwk`, `x5u` and
|
|
293
338
|
* `x5c` are never used to find a key. A resolver that throws or returns something other than a
|
|
294
339
|
* result is reported as `key_resolution_failed` without its message.
|
|
295
340
|
*
|
|
@@ -301,40 +346,41 @@ async function verifyCompactJws(e, t) {
|
|
|
301
346
|
return n.ok ? await verifyPrepared(e, n.value) : n;
|
|
302
347
|
}
|
|
303
348
|
/** Verifies `token` under options already checked by {@linkcode prepareVerify}. */
|
|
304
|
-
async function verifyPrepared(
|
|
305
|
-
let
|
|
306
|
-
if (!
|
|
307
|
-
let
|
|
308
|
-
if (
|
|
309
|
-
if (!isAlgorithmAdmitted(
|
|
310
|
-
if (!matchesTokenType(
|
|
311
|
-
let
|
|
312
|
-
...
|
|
313
|
-
alg:
|
|
314
|
-
},
|
|
315
|
-
if (!
|
|
316
|
-
let
|
|
317
|
-
if (!
|
|
318
|
-
if (
|
|
319
|
-
let
|
|
320
|
-
alg:
|
|
321
|
-
...
|
|
322
|
-
}),
|
|
323
|
-
publicKey:
|
|
324
|
-
signatureAlgorithm: { oid:
|
|
349
|
+
async function verifyPrepared(e, t) {
|
|
350
|
+
let r = parseWithLimits(e, t.limits);
|
|
351
|
+
if (!r.ok) return r;
|
|
352
|
+
let i = r.value, a = i.header.alg;
|
|
353
|
+
if (a === "none") return l("unsigned_token", "Unsigned tokens are never accepted", { alg: a });
|
|
354
|
+
if (!isAlgorithmAdmitted(a, t.algorithms)) return l("algorithm_not_allowed", "The token algorithm is not in the allowed list", { alg: a });
|
|
355
|
+
if (!matchesTokenType(i.header, t.typeRule)) return l("token_type_mismatch", "The token \"typ\" does not match the expected type", { alg: a });
|
|
356
|
+
let o = {
|
|
357
|
+
...i.header,
|
|
358
|
+
alg: a
|
|
359
|
+
}, s = await resolveKey(t, o);
|
|
360
|
+
if (!s.ok) return s;
|
|
361
|
+
let c = checkJwsVerificationKey(s.value, a);
|
|
362
|
+
if (!c.ok) return c;
|
|
363
|
+
if (i.signature.length !== c.value) return l("invalid_signature", "The signature length does not match the algorithm and key", { alg: a });
|
|
364
|
+
let d = l("invalid_signature", "The signature does not verify", {
|
|
365
|
+
alg: a,
|
|
366
|
+
...o.kid === void 0 ? {} : { kid: o.kid }
|
|
367
|
+
}), f = await n({
|
|
368
|
+
publicKey: s.value,
|
|
369
|
+
signatureAlgorithm: { oid: jwsAlgorithmProfile(a).oid },
|
|
325
370
|
signatureEncoding: "raw",
|
|
326
|
-
signature:
|
|
327
|
-
data:
|
|
371
|
+
signature: i.signature,
|
|
372
|
+
data: i.signingInput
|
|
328
373
|
});
|
|
329
|
-
return !
|
|
374
|
+
return !f.ok || !f.valid ? d : u({
|
|
330
375
|
verified: !0,
|
|
331
|
-
header:
|
|
332
|
-
payload:
|
|
376
|
+
header: o,
|
|
377
|
+
payload: i.payload
|
|
333
378
|
});
|
|
334
379
|
}
|
|
335
380
|
function assertSigningKey(e) {
|
|
336
|
-
|
|
337
|
-
|
|
381
|
+
f.includes(e.algorithm) || d("unsupported_algorithm", "Unsupported JWS algorithm");
|
|
382
|
+
let t = checkJwsKey(e.privateKey, e.algorithm, "private");
|
|
383
|
+
t.ok || d("incompatible_key", t.message);
|
|
338
384
|
}
|
|
339
385
|
/**
|
|
340
386
|
* Signs a compact JWS with an existing private `CryptoKey`.
|
|
@@ -343,20 +389,20 @@ function assertSigningKey(e) {
|
|
|
343
389
|
* (see `isResultError`) coded {@linkcode SignJoseErrorCode} when the algorithm, key or header
|
|
344
390
|
* input is invalid. Intended for issuing tokens and for building synthetic tokens in tests.
|
|
345
391
|
*/
|
|
346
|
-
async function signCompactJws(
|
|
347
|
-
assertSigningKey(
|
|
348
|
-
let n = { alg:
|
|
349
|
-
for (let
|
|
392
|
+
async function signCompactJws(n) {
|
|
393
|
+
assertSigningKey(n);
|
|
394
|
+
let r = jwsAlgorithmProfile(n.algorithm), i = { alg: n.algorithm };
|
|
395
|
+
for (let e of [
|
|
350
396
|
"typ",
|
|
351
397
|
"kid",
|
|
352
398
|
"cty"
|
|
353
399
|
]) {
|
|
354
|
-
let
|
|
355
|
-
|
|
400
|
+
let t = n.header?.[e];
|
|
401
|
+
t !== void 0 && (typeof t != "string" && d("invalid_header", `Header "${e}" must be a string`), i[e] = t);
|
|
356
402
|
}
|
|
357
|
-
let
|
|
358
|
-
return `${
|
|
403
|
+
let o = `${a(v.encode(JSON.stringify(i)))}.${a(n.payload)}`, { signature: s } = await t(n.privateKey, v.encode(o)), c = r.kind === "ec" ? e(s, r.curve) : s;
|
|
404
|
+
return `${o}.${a(c)}`;
|
|
359
405
|
}
|
|
360
|
-
export {
|
|
406
|
+
export { g as DEFAULT_JOSE_LIMITS, f as JWS_ALGORITHMS, m as MIN_JWS_RSA_MODULUS_BITS, p as MIN_RS256_MODULUS_BITS, checkJwsVerificationKey, decodeJsonObject, isUncheckedPolicy, jwsAlgorithmProfile, mediaTypeKey, parseCompactJws, prepareVerify, resolveJoseLimits, signCompactJws, validRsaPublicExponent, verifyCompactJws, verifyPrepared };
|
|
361
407
|
|
|
362
408
|
//# sourceMappingURL=jws.js.map
|