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.
Files changed (137) hide show
  1. package/CHANGELOG.md +123 -6
  2. package/README.md +3 -0
  3. package/dist/crypto/crypto.d.ts +2 -3
  4. package/dist/crypto.d.ts +6 -0
  5. package/dist/der/der.d.ts +14 -4
  6. package/dist/der/der.js +19 -19
  7. package/dist/der.d.ts +26 -2
  8. package/dist/der.js +4 -4
  9. package/dist/index.d.ts +82 -15
  10. package/dist/index.js +19 -19
  11. package/dist/internal/asn1/asn1.d.ts +1 -2
  12. package/dist/internal/asn1/der.d.ts +1 -2
  13. package/dist/internal/asn1/oids.js +71 -353
  14. package/dist/internal/asn1/oids.js.map +1 -1
  15. package/dist/internal/asn1/oids.json.js +357 -0
  16. package/dist/internal/asn1/oids.json.js.map +1 -0
  17. package/dist/internal/crypto/algorithm-names.js +1 -1
  18. package/dist/internal/crypto/pbes2.d.ts +1 -2
  19. package/dist/internal/crypto/pbes2.js +70 -70
  20. package/dist/internal/crypto/prime-ec.js +37 -0
  21. package/dist/internal/crypto/prime-ec.js.map +1 -0
  22. package/dist/internal/crypto/rsa-pss.js +50 -50
  23. package/dist/internal/crypto/sig-verify.d.ts +1 -2
  24. package/dist/internal/crypto/sig-verify.js +60 -60
  25. package/dist/internal/crypto/signing.d.ts +1 -2
  26. package/dist/internal/crypto/signing.js +26 -26
  27. package/dist/internal/jose/json-data.js +108 -0
  28. package/dist/internal/jose/json-data.js.map +1 -0
  29. package/dist/internal/jose/strict-json.d.ts +1 -2
  30. package/dist/internal/shared/dn.d.ts +1 -2
  31. package/dist/internal/shared/dn.js +1 -1
  32. package/dist/internal/shared/ip.d.ts +1 -2
  33. package/dist/internal/verify/name-constraints-engine.js +64 -64
  34. package/dist/internal/verify/policy-engine.js +1 -1
  35. package/dist/internal/verify/verify-path.d.ts +1 -2
  36. package/dist/internal/verify/verify-path.js +19 -19
  37. package/dist/internal/x509/directory-name.js +10 -10
  38. package/dist/internal/x509/extension-errors.d.ts +1 -2
  39. package/dist/internal/x509/extension-registry.js +18 -18
  40. package/dist/internal/x509/general-name-profile.js +9 -9
  41. package/dist/internal/x509/general-name.js +32 -32
  42. package/dist/internal/x509/name-fields.js +1 -1
  43. package/dist/jose/base64url.d.ts +4 -0
  44. package/dist/jose/base64url.js +14 -0
  45. package/dist/jose/base64url.js.map +1 -0
  46. package/dist/jose/errors.d.ts +9 -3
  47. package/dist/jose/errors.js.map +1 -1
  48. package/dist/jose/jwe.d.ts +64 -0
  49. package/dist/jose/jwe.js +188 -0
  50. package/dist/jose/jwe.js.map +1 -0
  51. package/dist/jose/jwks.d.ts +38 -10
  52. package/dist/jose/jwks.js +156 -96
  53. package/dist/jose/jwks.js.map +1 -1
  54. package/dist/jose/jws.d.ts +8 -6
  55. package/dist/jose/jws.js +216 -170
  56. package/dist/jose/jws.js.map +1 -1
  57. package/dist/jose/jwt.d.ts +8 -2
  58. package/dist/jose/jwt.js +98 -98
  59. package/dist/jose/remote-jwks.d.ts +1 -2
  60. package/dist/jose/remote-jwks.js +106 -108
  61. package/dist/jose/remote-jwks.js.map +1 -1
  62. package/dist/jose.d.ts +15 -4
  63. package/dist/jose.js +7 -5
  64. package/dist/kdf/argon2-arithmetic.js +15 -0
  65. package/dist/kdf/argon2-arithmetic.js.map +1 -0
  66. package/dist/kdf/argon2-core.js +486 -0
  67. package/dist/kdf/argon2-core.js.map +1 -0
  68. package/dist/kdf/argon2-memory.js +20 -0
  69. package/dist/kdf/argon2-memory.js.map +1 -0
  70. package/dist/kdf/argon2-portable.js +18 -0
  71. package/dist/kdf/argon2-portable.js.map +1 -0
  72. package/dist/kdf/argon2.d.ts +22 -11
  73. package/dist/kdf/argon2.js +32 -13
  74. package/dist/kdf/argon2.js.map +1 -1
  75. package/dist/kdf.d.ts +6 -0
  76. package/dist/keys/keys.d.ts +31 -6
  77. package/dist/keys/keys.js +315 -233
  78. package/dist/keys/keys.js.map +1 -1
  79. package/dist/keys.d.ts +11 -2
  80. package/dist/keys.js +2 -2
  81. package/dist/pem/pem.d.ts +1 -2
  82. package/dist/pem/pem.js +40 -25
  83. package/dist/pem/pem.js.map +1 -1
  84. package/dist/pem.d.ts +6 -0
  85. package/dist/pkcs/pfx.d.ts +2 -3
  86. package/dist/pkcs/pfx.js +142 -142
  87. package/dist/pkcs/pkcs12-mac.d.ts +1 -2
  88. package/dist/pkcs/pkcs12-mac.js +122 -122
  89. package/dist/pkcs/pkcs7.d.ts +14 -3
  90. package/dist/pkcs/pkcs7.js +268 -248
  91. package/dist/pkcs/pkcs7.js.map +1 -1
  92. package/dist/pkcs.d.ts +10 -1
  93. package/dist/pkcs.js +4 -4
  94. package/dist/result/result.d.ts +1 -2
  95. package/dist/result.d.ts +6 -0
  96. package/dist/revocation/chain.d.ts +2 -3
  97. package/dist/revocation/chain.js +52 -52
  98. package/dist/revocation/crl.d.ts +2 -3
  99. package/dist/revocation/crl.js +319 -319
  100. package/dist/revocation/crl.js.map +1 -1
  101. package/dist/revocation/ocsp.d.ts +2 -3
  102. package/dist/revocation/ocsp.js +395 -395
  103. package/dist/revocation/revocation.d.ts +2 -3
  104. package/dist/revocation/revocation.js +22 -22
  105. package/dist/revocation.d.ts +12 -3
  106. package/dist/revocation.js +4 -4
  107. package/dist/ssh/ssh.d.ts +2 -3
  108. package/dist/ssh/ssh.js +165 -170
  109. package/dist/ssh/ssh.js.map +1 -1
  110. package/dist/ssh/wire.d.ts +1 -2
  111. package/dist/ssh/wire.js +10 -10
  112. package/dist/ssh.d.ts +8 -0
  113. package/dist/verify/identity.d.ts +1 -2
  114. package/dist/verify/identity.js +31 -31
  115. package/dist/verify/name-constraints.d.ts +9 -2
  116. package/dist/verify/policy.d.ts +9 -2
  117. package/dist/verify/verify.d.ts +3 -4
  118. package/dist/verify/verify.js +121 -121
  119. package/dist/verify.d.ts +9 -0
  120. package/dist/x509/certificate.d.ts +4 -5
  121. package/dist/x509/certificate.js +40 -40
  122. package/dist/x509/csr.d.ts +3 -4
  123. package/dist/x509/csr.js +15 -15
  124. package/dist/x509/extensions.d.ts +1 -2
  125. package/dist/x509/extensions.js +165 -165
  126. package/dist/x509/extensions.js.map +1 -1
  127. package/dist/x509/fingerprint.d.ts +1 -2
  128. package/dist/x509/name-text.d.ts +1 -2
  129. package/dist/x509/name.d.ts +1 -2
  130. package/dist/x509/name.js +15 -15
  131. package/dist/x509/parse.d.ts +3 -4
  132. package/dist/x509/parse.js +423 -423
  133. package/dist/x509.d.ts +17 -4
  134. package/dist/x509.js +8 -8
  135. package/package.json +27 -34
  136. package/dist/internal/asn1/oids2.js +0 -75
  137. package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/jose/jws.js CHANGED
@@ -1,9 +1,9 @@
1
- import { successResult as e, throwMicro509Error as t } from "../result/result.js";
2
- import { OIDS as n } from "../internal/asn1/oids2.js";
3
- import { signData as r, verifySignatureWithKey as i } from "../crypto/crypto.js";
4
- import { base64UrlDecode as a, base64UrlEncode as o } from "../internal/jose/base64url.js";
5
- import { isJsonArray as s, isJsonObject as c, parseStrictJson as l } from "../internal/jose/strict-json.js";
6
- import { joseFailure as u } from "./errors.js";
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 d = ["RS256"], f = 2048, p = {
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
- }, m = [
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(t) {
37
- if (t !== void 0 && (typeof t != "object" || !t)) return u("invalid_options", "limits must be an object");
38
- let n = { ...p };
39
- for (let e of m) {
40
- let r = t?.[e];
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 = e === "maxJsonDepth" ? 64 : 2 ** 53 - 1;
43
- if (!Number.isSafeInteger(r) || r < 1 || r > i) return u("invalid_options", `limits.${e} must be a positive safe integer`);
44
- n[e] = r;
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 e(n);
88
+ return u(t);
47
89
  }
48
- const h = new TextEncoder(), g = new TextDecoder("utf-8", {
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 u(e, t);
98
+ return l(e, t);
57
99
  }
58
- function decodeSegment(t, n, r) {
59
- if (t.length > maxEncodedLength(n)) return parseFailure("limit_exceeded", `JWS ${r} exceeds ${n} bytes`);
60
- let i = a(t);
61
- return i === void 0 ? parseFailure("invalid_encoding", `JWS ${r} is not canonical base64url`) : e(i);
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 = g.decode(t);
109
+ a = y.decode(e);
68
110
  } catch {
69
- return parseFailure("invalid_utf8", `JWS ${r} is not valid UTF-8`);
111
+ return parseFailure("invalid_utf8", `${i} ${n} is not valid UTF-8`);
70
112
  }
71
- let o = l(a, {
72
- maxDepth: n.maxJsonDepth,
73
- maxNodes: n.maxJsonNodes
113
+ let o = c(a, {
114
+ maxDepth: t.maxJsonDepth,
115
+ maxNodes: t.maxJsonNodes
74
116
  });
75
- if (!o.ok) return parseFailure(o.code, `JWS ${r}: ${o.message}`);
76
- let s = o.value;
77
- return c(s) ? e(s) : parseFailure(i, `JWS ${r} is not a JSON object`);
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(t, n) {
80
- if (!Object.hasOwn(t, n)) return e(void 0);
81
- let r = t[n];
82
- return typeof r == "string" ? e(r) : parseFailure("invalid_header", `JWS header "${n}" is not a string`);
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(t) {
85
- if (!Object.hasOwn(t, "crit")) return e([]);
86
- let n = t.crit;
87
- if (!s(n) || n.length === 0) return parseFailure("invalid_header", "JWS header \"crit\" is not a non-empty array");
88
- let r = [];
89
- for (let e of n) {
90
- if (typeof e != "string" || r.includes(e)) return parseFailure("invalid_header", "JWS header \"crit\" holds a non-string or repeated name");
91
- r.push(e);
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 e(r);
135
+ return u(n);
94
136
  }
95
- function parseHeader(t, n) {
96
- let r = decodeJsonObject(t, n, "header", "invalid_header");
97
- if (!r.ok) return r;
98
- let i = r.value, a = i.alg;
99
- if (typeof a != "string" || a.length === 0) return parseFailure("invalid_header", "JWS header \"alg\" is missing or not a string");
100
- let o = optionalString(i, "typ");
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(i, "kid");
146
+ let s = optionalString(r, "cty");
103
147
  if (!s.ok) return s;
104
- let c = optionalString(i, "cty");
105
- if (!c.ok) return c;
106
- let l = criticalNames(i);
107
- return l.ok ? i.b64 === !1 || l.value.includes("b64") ? parseFailure("unsupported_unencoded_payload", "Unencoded and detached payloads (RFC 7797) are not supported") : Object.hasOwn(i, "b64") && typeof i.b64 != "boolean" ? parseFailure("invalid_header", "JWS header \"b64\" is not a boolean") : l.value.length > 0 ? u("unsupported_critical_header", "JWS header lists critical extensions", {
108
- alg: a,
109
- crit: l.value
110
- }) : e({
111
- alg: a,
112
- ...o.value === void 0 ? {} : { typ: o.value },
113
- ...s.value === void 0 ? {} : { kid: s.value },
114
- ...c.value === void 0 ? {} : { cty: c.value },
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(t, n) {
131
- if (typeof t != "string") return parseFailure("malformed_token", "JWS token is not a string");
132
- if (t.length > n.maxTokenLength) return parseFailure("token_too_large", `JWS token exceeds ${n.maxTokenLength} characters`);
133
- let r = t.split("."), [i, a, o] = r;
134
- if (r.length !== 3 || i === void 0 || a === void 0 || o === void 0 || i.length === 0) return parseFailure("malformed_token", "JWS compact serialization needs three segments");
135
- let s = decodeSegment(i, n.maxHeaderBytes, "header");
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 = parseHeader(s.value, n);
181
+ let c = decodeSegment(i, t.maxPayloadBytes, "payload");
138
182
  if (!c.ok) return c;
139
- let l = decodeSegment(a, n.maxPayloadBytes, "payload");
140
- if (!l.ok) return l;
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: c.value,
145
- payload: l.value,
146
- signingInput: h.encode(`${i}.${a}`),
147
- signature: u.value
148
- }) : u;
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) && d.some((t) => t === 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
- * Checks a key may verify RS256 (RFC 7518 §3.3, RFC 8725 §3.1) and returns the signature length
206
- * in bytes it requires (RFC 8017 §8.2.2).
207
- */
208
- function checkRs256VerificationKey(e) {
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 inspectRs256VerificationKey(e);
252
+ return inspectJwsKey(e, t, n);
211
253
  } catch {
212
- return u("key_unsuitable", "The key cannot be inspected");
254
+ return l("key_unsuitable", "The key cannot be inspected");
213
255
  }
214
256
  }
215
- function inspectRs256VerificationKey(t) {
216
- let n = t.algorithm;
217
- return t.type !== "public" || !t.usages.includes("verify") ? u("key_unsuitable", "RS256 needs a public key with verify usage") : n.name !== "RSASSA-PKCS1-v1_5" || !("hash" in n) || typeof n.hash != "object" || n.hash === null || !("name" in n.hash) || n.hash.name !== "SHA-256" ? u("key_unsuitable", "RS256 needs an RSASSA-PKCS1-v1_5 SHA-256 key") : !("modulusLength" in n) || typeof n.modulusLength != "number" || n.modulusLength < 2048 ? u("key_unsuitable", `RS256 needs an RSA modulus of at least ${f} bits`) : !("publicExponent" in n) || !(n.publicExponent instanceof Uint8Array) || !validRsaPublicExponent(n.publicExponent) ? u("key_unsuitable", "RS256 needs a valid odd RSA public exponent of at least 3") : e(Math.ceil(n.modulusLength / 8));
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(t) {
224
- if (typeof t != "object" || !t) return u("invalid_options", "Verification options are required");
225
- let { algorithms: n, key: r, tokenType: i, limits: a, signal: o } = t;
226
- if (!Array.isArray(n) || n.length === 0) return u("invalid_options", "An explicit, non-empty algorithm list is required");
227
- let s = [];
228
- for (let e of n) {
229
- let t = d.find((t) => t === e);
230
- if (t === void 0) return u("invalid_options", "The algorithm list names an unsupported algorithm");
231
- s.push(t);
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 r != "function" && !isCryptoKey(r)) return u("invalid_options", "A key or key resolver is required");
234
- let c = normalizeTokenType(i);
235
- if (c === void 0) return u("invalid_options", "A token type policy is required");
236
- if (o !== void 0 && !(o instanceof AbortSignal)) return u("invalid_options", "signal must be an AbortSignal");
237
- let l = resolveJoseLimits(a);
238
- return l.ok ? e({
239
- algorithms: s,
240
- key: r,
241
- typeRule: c,
242
- limits: l.value,
243
- signal: o
244
- }) : l;
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(t, n) {
247
- let { key: r, signal: i } = t;
248
- if (typeof r != "function") return e(r);
249
- let a;
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
- a = await r({
252
- header: n,
253
- algorithm: n.alg,
254
- ...n.kid === void 0 ? {} : { kid: n.kid },
255
- ...i === void 0 ? {} : { signal: i }
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 i?.aborted === !0 ? u("aborted", "Key resolution was cancelled") : u("key_resolution_failed", "The key resolver failed");
302
+ return r?.aborted === !0 ? l("aborted", "Key resolution was cancelled") : l("key_resolution_failed", "The key resolver failed");
259
303
  }
260
- if (typeof a != "object" || !a) return u("key_resolution_failed", "The key resolver returned no result");
261
- if (Reflect.get(a, "ok") === !0) {
262
- let t = Reflect.get(a, "value");
263
- return isCryptoKey(t) ? e(t) : u("key_resolution_failed", "The key resolver returned no key");
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 o = Reflect.get(a, "code");
266
- switch (o) {
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 u(o, `Key resolution failed: ${o}`, resolverDetails(a));
274
- default: return u("key_resolution_failed", "The key resolver returned an unknown failure");
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. A fixed key or the resolver's key must be an RSASSA-PKCS1-v1_5 SHA-256
292
- * public key of at least 2048 bits for RS256. Header members such as `jku`, `jwk`, `x5u` and
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(t, r) {
305
- let a = parseWithLimits(t, r.limits);
306
- if (!a.ok) return a;
307
- let o = a.value, s = o.header.alg;
308
- if (s === "none") return u("unsigned_token", "Unsigned tokens are never accepted", { alg: s });
309
- if (!isAlgorithmAdmitted(s, r.algorithms)) return u("algorithm_not_allowed", "The token algorithm is not in the allowed list", { alg: s });
310
- if (!matchesTokenType(o.header, r.typeRule)) return u("token_type_mismatch", "The token \"typ\" does not match the expected type", { alg: s });
311
- let c = {
312
- ...o.header,
313
- alg: s
314
- }, l = await resolveKey(r, c);
315
- if (!l.ok) return l;
316
- let d = checkRs256VerificationKey(l.value);
317
- if (!d.ok) return d;
318
- if (o.signature.length !== d.value) return u("invalid_signature", "The signature length differs from the modulus", { alg: s });
319
- let f = u("invalid_signature", "The signature does not verify", {
320
- alg: s,
321
- ...c.kid === void 0 ? {} : { kid: c.kid }
322
- }), p = await i({
323
- publicKey: l.value,
324
- signatureAlgorithm: { oid: n.sha256WithRSAEncryption },
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: o.signature,
327
- data: o.signingInput
371
+ signature: i.signature,
372
+ data: i.signingInput
328
373
  });
329
- return !p.ok || !p.valid ? f : e({
374
+ return !f.ok || !f.valid ? d : u({
330
375
  verified: !0,
331
- header: c,
332
- payload: o.payload
376
+ header: o,
377
+ payload: i.payload
333
378
  });
334
379
  }
335
380
  function assertSigningKey(e) {
336
- let n = e.privateKey.algorithm;
337
- d.includes(e.algorithm) || t("unsupported_algorithm", "Unsupported JWS algorithm"), (e.privateKey.type !== "private" || !e.privateKey.usages.includes("sign") || n.name !== "RSASSA-PKCS1-v1_5" || !("hash" in n) || typeof n.hash != "object" || n.hash === null || !("name" in n.hash) || n.hash.name !== "SHA-256" || !("modulusLength" in n) || typeof n.modulusLength != "number" || n.modulusLength < 2048) && t("incompatible_key", `RS256 signing needs an RSASSA-PKCS1-v1_5 SHA-256 private key of at least ${f} bits`);
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(e) {
347
- assertSigningKey(e);
348
- let n = { alg: e.algorithm };
349
- for (let r of [
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 i = e.header?.[r];
355
- i !== void 0 && (typeof i != "string" && t("invalid_header", `Header "${r}" must be a string`), n[r] = i);
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 i = `${o(h.encode(JSON.stringify(n)))}.${o(e.payload)}`, { signature: a } = await r(e.privateKey, h.encode(i));
358
- return `${i}.${o(a)}`;
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 { p as DEFAULT_JOSE_LIMITS, d as JWS_ALGORITHMS, f as MIN_RS256_MODULUS_BITS, checkRs256VerificationKey, decodeJsonObject, isUncheckedPolicy, mediaTypeKey, parseCompactJws, prepareVerify, resolveJoseLimits, signCompactJws, validRsaPublicExponent, verifyCompactJws, verifyPrepared };
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