micro509 0.18.0 → 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.
Files changed (99) hide show
  1. package/CHANGELOG.md +88 -1
  2. package/dist/crypto/crypto.d.ts +11 -2
  3. package/dist/crypto/crypto.js +30 -17
  4. package/dist/crypto/crypto.js.map +1 -1
  5. package/dist/crypto.d.ts +2 -2
  6. package/dist/der/der.js.map +1 -1
  7. package/dist/index.d.ts +6 -5
  8. package/dist/internal/asn1/asn1.js +10 -2
  9. package/dist/internal/asn1/asn1.js.map +1 -1
  10. package/dist/internal/asn1/ber.js.map +1 -1
  11. package/dist/internal/asn1/decode-refusal.js.map +1 -1
  12. package/dist/internal/asn1/der.js.map +1 -1
  13. package/dist/internal/asn1/oids.js.map +1 -1
  14. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  15. package/dist/internal/crypto/ecdsa.js.map +1 -1
  16. package/dist/internal/crypto/hash.js.map +1 -1
  17. package/dist/internal/crypto/pbes2.js.map +1 -1
  18. package/dist/internal/crypto/prime-ec.js.map +1 -1
  19. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  20. package/dist/internal/crypto/sig-verify.d.ts +40 -0
  21. package/dist/internal/crypto/sig-verify.js +152 -65
  22. package/dist/internal/crypto/sig-verify.js.map +1 -1
  23. package/dist/internal/crypto/signing.js.map +1 -1
  24. package/dist/internal/jose/base64url.js.map +1 -1
  25. package/dist/internal/jose/json-data.js.map +1 -1
  26. package/dist/internal/jose/strict-json.js.map +1 -1
  27. package/dist/internal/shared/base64.js.map +1 -1
  28. package/dist/internal/shared/dn.js.map +1 -1
  29. package/dist/internal/shared/idna.js.map +1 -1
  30. package/dist/internal/shared/ip.js.map +1 -1
  31. package/dist/internal/shared/mailbox.js.map +1 -1
  32. package/dist/internal/shared/punycode.js.map +1 -1
  33. package/dist/internal/shared/uri-host.js.map +1 -1
  34. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  35. package/dist/internal/verify/policy-engine.js.map +1 -1
  36. package/dist/internal/verify/spki-distrust.js.map +1 -1
  37. package/dist/internal/verify/verify-path.js +222 -116
  38. package/dist/internal/verify/verify-path.js.map +1 -1
  39. package/dist/internal/x509/directory-name.js.map +1 -1
  40. package/dist/internal/x509/extension-bits.js.map +1 -1
  41. package/dist/internal/x509/extension-errors.js.map +1 -1
  42. package/dist/internal/x509/extension-registry.js.map +1 -1
  43. package/dist/internal/x509/general-name-profile.js.map +1 -1
  44. package/dist/internal/x509/general-name.js.map +1 -1
  45. package/dist/internal/x509/name-fields.js.map +1 -1
  46. package/dist/internal/x509/or-address.js.map +1 -1
  47. package/dist/jose/base64url.js.map +1 -1
  48. package/dist/jose/errors.js.map +1 -1
  49. package/dist/jose/jwe.js.map +1 -1
  50. package/dist/jose/jwks.js.map +1 -1
  51. package/dist/jose/jws.js.map +1 -1
  52. package/dist/jose/jwt.js.map +1 -1
  53. package/dist/jose/remote-jwks.js.map +1 -1
  54. package/dist/kdf/argon2-arithmetic.js.map +1 -1
  55. package/dist/kdf/argon2-core.js.map +1 -1
  56. package/dist/kdf/argon2-memory.js.map +1 -1
  57. package/dist/kdf/argon2-portable.js.map +1 -1
  58. package/dist/kdf/argon2.js.map +1 -1
  59. package/dist/keys/keys.js.map +1 -1
  60. package/dist/pem/pem.js.map +1 -1
  61. package/dist/pkcs/pfx.js.map +1 -1
  62. package/dist/pkcs/pkcs12-mac.js +1 -1
  63. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  64. package/dist/pkcs/pkcs7.d.ts +10 -0
  65. package/dist/pkcs/pkcs7.js +106 -88
  66. package/dist/pkcs/pkcs7.js.map +1 -1
  67. package/dist/result/result.js.map +1 -1
  68. package/dist/revocation/chain.d.ts +67 -13
  69. package/dist/revocation/chain.js +429 -301
  70. package/dist/revocation/chain.js.map +1 -1
  71. package/dist/revocation/crl.d.ts +65 -11
  72. package/dist/revocation/crl.js +492 -202
  73. package/dist/revocation/crl.js.map +1 -1
  74. package/dist/revocation/ocsp.d.ts +79 -24
  75. package/dist/revocation/ocsp.js +417 -231
  76. package/dist/revocation/ocsp.js.map +1 -1
  77. package/dist/revocation/revocation.d.ts +68 -13
  78. package/dist/revocation/revocation.js +194 -73
  79. package/dist/revocation/revocation.js.map +1 -1
  80. package/dist/revocation.d.ts +4 -4
  81. package/dist/ssh/ssh.js.map +1 -1
  82. package/dist/ssh/wire.js.map +1 -1
  83. package/dist/verify/identity.d.ts +2 -2
  84. package/dist/verify/identity.js +2 -2
  85. package/dist/verify/identity.js.map +1 -1
  86. package/dist/verify/verify.d.ts +84 -32
  87. package/dist/verify/verify.js +221 -171
  88. package/dist/verify/verify.js.map +1 -1
  89. package/dist/verify.d.ts +3 -2
  90. package/dist/x509/certificate.js.map +1 -1
  91. package/dist/x509/csr.js.map +1 -1
  92. package/dist/x509/extensions.js.map +1 -1
  93. package/dist/x509/fingerprint.js.map +1 -1
  94. package/dist/x509/name-text.js.map +1 -1
  95. package/dist/x509/name.js.map +1 -1
  96. package/dist/x509/parse.d.ts +6 -2
  97. package/dist/x509/parse.js +121 -121
  98. package/dist/x509/parse.js.map +1 -1
  99. package/package.json +11 -11
@@ -1,11 +1,11 @@
1
- import { toArrayBuffer as e } from "../asn1/asn1.js";
2
- import { DECODE_LIMIT_CODES as t, rethrowDecodeRefusal as n } from "../asn1/decode-refusal.js";
3
- import { readElement as r } from "../asn1/der.js";
4
- import { OIDS as i } from "../asn1/oids.js";
5
- import { alternateEcdsaSignatureEncoding as a, derEcdsaSignatureToRaw as o } from "./ecdsa.js";
6
- import { parseRsaPssParameters as s } from "./rsa-pss.js";
7
- import { getCrypto as c } from "./webcrypto.js";
8
- import { importSpkiDerOrThrow as l } from "../../keys/keys.js";
1
+ import { canonicalizeOid as e, toArrayBuffer as t } from "../asn1/asn1.js";
2
+ import { DECODE_LIMIT_CODES as n, rethrowDecodeRefusal as r } from "../asn1/decode-refusal.js";
3
+ import { readElement as i, readSequenceChildren as a } from "../asn1/der.js";
4
+ import { OIDS as o } from "../asn1/oids.js";
5
+ import { alternateEcdsaSignatureEncoding as s, derEcdsaSignatureToRaw as c } from "./ecdsa.js";
6
+ import { parseRsaPssParameters as l } from "./rsa-pss.js";
7
+ import { getCrypto as u } from "./webcrypto.js";
8
+ import { importSpkiDerOrThrow as d } from "../../keys/keys.js";
9
9
  // src/internal/crypto/sig-verify.ts
10
10
  /**
11
11
  * Signature-verification helpers that resolve parsed algorithm OIDs into WebCrypto
@@ -13,47 +13,105 @@ import { importSpkiDerOrThrow as l } from "../../keys/keys.js";
13
13
  *
14
14
  * @module
15
15
  */
16
+ const f = /* @__PURE__ */ new Set([
17
+ o.sha256WithRSAEncryption,
18
+ o.sha384WithRSAEncryption,
19
+ o.sha512WithRSAEncryption,
20
+ o.rsassaPss,
21
+ o.ecdsaWithSHA256,
22
+ o.ecdsaWithSHA384,
23
+ o.ecdsaWithSHA512,
24
+ o.ed25519
25
+ ]);
26
+ /**
27
+ * Validates caller verifiers and indexes them by canonical OID.
28
+ *
29
+ * @throws RangeError for a key that is no encodable OID, a natively verified
30
+ * algorithm, or a value that is no function.
31
+ */
32
+ function resolveSignatureVerifiers(e) {
33
+ let t = /* @__PURE__ */ new Map();
34
+ if (e === void 0) return t;
35
+ for (let [n, r] of Object.entries(e)) {
36
+ let e = canonicalVerifierOid(n);
37
+ if (f.has(e)) throw RangeError(`signatureVerifiers cannot replace native verification of ${n}`);
38
+ if (typeof r != "function") throw RangeError(`signatureVerifiers value for ${n} is not a function`);
39
+ if (t.has(e)) throw RangeError(`signatureVerifiers names ${e} twice`);
40
+ t.set(e, r);
41
+ }
42
+ return t;
43
+ }
44
+ function canonicalVerifierOid(t) {
45
+ if (!/^\d+(?:\.\d+)+$/.test(t)) throw RangeError(`signatureVerifiers key is not an OID: ${t}`);
46
+ try {
47
+ return e(t);
48
+ } catch (e) {
49
+ throw r(e, n), RangeError(`signatureVerifiers key is not an OID: ${t}`);
50
+ }
51
+ }
52
+ /** No caller verifiers: every signature goes through WebCrypto. */
53
+ const p = { verifiers: /* @__PURE__ */ new Map() };
54
+ /** Caller verification with `verifiers` and the signer's own key parameters. */
55
+ function callerVerification(e) {
56
+ return { verifiers: resolveSignatureVerifiers(e) };
57
+ }
58
+ /** DER key parameters, or `undefined` when they are absent or NULL (RFC 5280 §6.1.4 (e)). */
59
+ function explicitParameters(e) {
60
+ if (e !== void 0) return e.length === 2 && e[0] === 5 && e[1] === 0 ? void 0 : e;
61
+ }
62
+ /** The AlgorithmIdentifier parameters of a SubjectPublicKeyInfo; a malformed one has none. */
63
+ function spkiAlgorithmParametersDer(e) {
64
+ try {
65
+ let [t] = a(e);
66
+ if (t === void 0) return;
67
+ let n = e.subarray(t.start - t.headerLength, t.end), [, r] = a(n);
68
+ return r === void 0 ? void 0 : n.subarray(r.start - r.headerLength, r.end);
69
+ } catch (e) {
70
+ r(e, n);
71
+ return;
72
+ }
73
+ }
16
74
  /** Non-throwing variant of {@linkcode getVerifySignatureConfig} — returns a typed result union. */
17
- function getVerifySignatureConfigResult(e, t, n, r, a = "issuer") {
75
+ function getVerifySignatureConfigResult(e, t, n, r, i = "issuer") {
18
76
  switch (e) {
19
- case i.sha256WithRSAEncryption: {
77
+ case o.sha256WithRSAEncryption: {
20
78
  let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-256 with RSA");
21
- return e === void 0 ? requireRsaPublicKey(n, "SHA-256", "pkcs1-v1_5", a) : e;
79
+ return e === void 0 ? requireRsaPublicKey(n, "SHA-256", "pkcs1-v1_5", i) : e;
22
80
  }
23
- case i.sha384WithRSAEncryption: {
81
+ case o.sha384WithRSAEncryption: {
24
82
  let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-384 with RSA");
25
- return e === void 0 ? requireRsaPublicKey(n, "SHA-384", "pkcs1-v1_5", a) : e;
83
+ return e === void 0 ? requireRsaPublicKey(n, "SHA-384", "pkcs1-v1_5", i) : e;
26
84
  }
27
- case i.sha512WithRSAEncryption: {
85
+ case o.sha512WithRSAEncryption: {
28
86
  let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-512 with RSA");
29
- return e === void 0 ? requireRsaPublicKey(n, "SHA-512", "pkcs1-v1_5", a) : e;
87
+ return e === void 0 ? requireRsaPublicKey(n, "SHA-512", "pkcs1-v1_5", i) : e;
30
88
  }
31
- case i.rsassaPss: return requireRsaPssVerifyConfig(t, n, a);
32
- case i.ecdsaWithSHA256: {
89
+ case o.rsassaPss: return requireRsaPssVerifyConfig(t, n, i);
90
+ case o.ecdsaWithSHA256: {
33
91
  let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-256");
34
- return e === void 0 ? requireEcPublicKey(n, r, "SHA-256", a) : e;
92
+ return e === void 0 ? requireEcPublicKey(n, r, "SHA-256", i) : e;
35
93
  }
36
- case i.ecdsaWithSHA384: {
94
+ case o.ecdsaWithSHA384: {
37
95
  let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-384");
38
- return e === void 0 ? requireEcPublicKey(n, r, "SHA-384", a) : e;
96
+ return e === void 0 ? requireEcPublicKey(n, r, "SHA-384", i) : e;
39
97
  }
40
- case i.ecdsaWithSHA512: {
98
+ case o.ecdsaWithSHA512: {
41
99
  let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-512");
42
- return e === void 0 ? requireEcPublicKey(n, r, "SHA-512", a) : e;
100
+ return e === void 0 ? requireEcPublicKey(n, r, "SHA-512", i) : e;
43
101
  }
44
- case i.ed25519: {
102
+ case o.ed25519: {
45
103
  let e = requireAbsentSignatureAlgorithmParameters(t, "Ed25519");
46
- return e === void 0 ? n === i.ed25519 ? ok({
104
+ return e === void 0 ? n === o.ed25519 ? ok({
47
105
  importAlgorithm: { kind: "ed25519" },
48
106
  verifyParams: { name: "Ed25519" }
49
- }) : unsupported("Ed25519", `requires Ed25519 ${a} public key`) : e;
107
+ }) : unsupported("Ed25519", `requires Ed25519 ${i} public key`) : e;
50
108
  }
51
109
  default: return unsupported(e, "unrecognized signature algorithm OID");
52
110
  }
53
111
  }
54
112
  /** Validate that the public key OID is `rsaEncryption` and return an RSA verification config result. */
55
- function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer", a) {
56
- return e === i.rsaEncryption ? ok({
113
+ function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer", i) {
114
+ return e === o.rsaEncryption ? ok({
57
115
  importAlgorithm: {
58
116
  kind: "rsa",
59
117
  hash: t,
@@ -61,15 +119,15 @@ function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer", a) {
61
119
  },
62
120
  verifyParams: n === "pss" ? {
63
121
  name: "RSA-PSS",
64
- saltLength: a ?? 0
122
+ saltLength: i ?? 0
65
123
  } : { name: "RSASSA-PKCS1-v1_5" }
66
124
  }) : unsupported("RSA", `requires RSA ${r} public key`);
67
125
  }
68
126
  /** Validate that the public key OID is `ecPublicKey`, resolve the curve, and return an ECDSA verification config result. */
69
127
  function requireEcPublicKey(e, t, n, r = "issuer") {
70
- if (e !== i.ecPublicKey) return unsupported("ECDSA", `requires EC ${r} public key`);
128
+ if (e !== o.ecPublicKey) return unsupported("ECDSA", `requires EC ${r} public key`);
71
129
  switch (t) {
72
- case i.prime256v1: return ok({
130
+ case o.prime256v1: return ok({
73
131
  importAlgorithm: {
74
132
  kind: "ecdsa",
75
133
  curve: "P-256"
@@ -80,7 +138,7 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
80
138
  },
81
139
  ecdsaRawSignatureBytes: 64
82
140
  });
83
- case i.secp384r1: return ok({
141
+ case o.secp384r1: return ok({
84
142
  importAlgorithm: {
85
143
  kind: "ecdsa",
86
144
  curve: "P-384"
@@ -91,7 +149,7 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
91
149
  },
92
150
  ecdsaRawSignatureBytes: 96
93
151
  });
94
- case i.secp521r1: return ok({
152
+ case o.secp521r1: return ok({
95
153
  importAlgorithm: {
96
154
  kind: "ecdsa",
97
155
  curve: "P-521"
@@ -106,45 +164,74 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
106
164
  }
107
165
  }
108
166
  /**
109
- * Non-throwing variant of {@linkcode verifySignedData} — returns a typed
110
- * {@linkcode VerifySignedDataResult} instead of throwing on unsupported algorithms
111
- * or runtime verification failures.
167
+ * Verifies a signature against the signer's SPKI and the signed bytes. Returns
168
+ * a typed {@linkcode VerifySignedDataResult} for unsupported algorithms and
169
+ * runtime verification failures.
112
170
  *
113
171
  * Tries both DER and raw ECDSA encodings when the first attempt fails.
114
172
  *
115
173
  * @throws `limit_exceeded` on algorithm parameters or a public key that exceed
116
174
  * a micro509 decoding limit.
117
175
  */
118
- async function verifySignedDataDetailed(e, r, i, a, o, s, c) {
119
- let u = getVerifySignatureConfigResult(e, r, i, a);
120
- if (!u.ok) return u;
176
+ async function verifySignedDataDetailed(e, t, i, a, o, s, c, l, u = p) {
177
+ let f = getVerifySignatureConfigResult(e, t, i, a);
178
+ if (!f.ok) {
179
+ let n = u.verifiers.get(e);
180
+ if (n === void 0) return f;
181
+ let r = u.workingParameters === void 0 ? explicitParameters(spkiAlgorithmParametersDer(o)) : u.workingParameters.parametersDer;
182
+ return await runCallerVerifier(n, {
183
+ signatureAlgorithm: {
184
+ oid: e,
185
+ ...t === void 0 ? {} : { parametersDer: new Uint8Array(t) }
186
+ },
187
+ signerSpkiDer: new Uint8Array(o),
188
+ ...r === void 0 ? {} : { signerPublicKeyParametersDer: new Uint8Array(r) },
189
+ signature: new Uint8Array(s),
190
+ signatureUnusedBits: c,
191
+ data: new Uint8Array(l)
192
+ });
193
+ }
194
+ if (c !== 0) return {
195
+ ok: !0,
196
+ valid: !1
197
+ };
121
198
  try {
122
- return await verifyWithConfiguredKey(await l(o, u.value.importAlgorithm), u.value, s, c, "auto");
199
+ return await verifyWithConfiguredKey(await d(o, f.value.importAlgorithm), f.value, s, l, "auto");
200
+ } catch (e) {
201
+ return r(e, n), verificationError(e instanceof Error ? e.message : "signature verification failed");
202
+ }
203
+ }
204
+ async function runCallerVerifier(e, t) {
205
+ try {
206
+ return {
207
+ ok: !0,
208
+ valid: await e(t) === !0
209
+ };
123
210
  } catch (e) {
124
- return n(e, t), verificationError(e instanceof Error ? e.message : "signature verification failed");
211
+ return verificationError(e instanceof Error ? `signature verifier failed: ${e.message}` : "signature verifier failed");
125
212
  }
126
213
  }
127
214
  /** Execute verification for imported SPKI and caller-owned keys with the same configuration. */
128
- async function verifyWithConfiguredKey(r, i, s, l, u) {
215
+ async function verifyWithConfiguredKey(e, i, a, o, l) {
129
216
  try {
130
- if (u === "der" && i.ecdsaRawSignatureBytes !== void 0) try {
131
- s = o(s, i.ecdsaRawSignatureBytes / 2);
217
+ if (l === "der" && i.ecdsaRawSignatureBytes !== void 0) try {
218
+ a = c(a, i.ecdsaRawSignatureBytes / 2);
132
219
  } catch (e) {
133
- return n(e, t), {
220
+ return r(e, n), {
134
221
  ok: !0,
135
222
  valid: !1
136
223
  };
137
224
  }
138
- let d = c().subtle, f = e(s), p = e(l);
139
- if (await d.verify(i.verifyParams, r, f, p)) return {
225
+ let d = u().subtle, f = t(a), p = t(o);
226
+ if (await d.verify(i.verifyParams, e, f, p)) return {
140
227
  ok: !0,
141
228
  valid: !0
142
229
  };
143
- if (u === "auto" && i.ecdsaRawSignatureBytes !== void 0) {
144
- let t = a(s, i.ecdsaRawSignatureBytes / 2);
145
- if (t !== void 0) return {
230
+ if (l === "auto" && i.ecdsaRawSignatureBytes !== void 0) {
231
+ let n = s(a, i.ecdsaRawSignatureBytes / 2);
232
+ if (n !== void 0) return {
146
233
  ok: !0,
147
- valid: await d.verify(i.verifyParams, r, e(t), p)
234
+ valid: await d.verify(i.verifyParams, e, t(n), p)
148
235
  };
149
236
  }
150
237
  return {
@@ -152,51 +239,51 @@ async function verifyWithConfiguredKey(r, i, s, l, u) {
152
239
  valid: !1
153
240
  };
154
241
  } catch (e) {
155
- return n(e, t), verificationError(e instanceof Error ? e.message : "signature verification failed");
242
+ return r(e, n), verificationError(e instanceof Error ? e.message : "signature verification failed");
156
243
  }
157
244
  }
158
245
  /** Resolve a caller-owned key without exporting it, then run the shared verifier. */
159
- async function verifySignedDataWithKeyDetailed(e, t, n, r, a, o) {
246
+ async function verifySignedDataWithKeyDetailed(e, t, n, r, i, a) {
160
247
  if (e.type !== "public" || !e.usages.includes("verify")) return verificationError("Signature verification requires a public key with verify usage");
161
248
  let s = e.algorithm, c, l;
162
249
  switch (s.name) {
163
250
  case "RSASSA-PKCS1-v1_5":
164
251
  case "RSA-PSS":
165
- c = i.rsaEncryption;
252
+ c = o.rsaEncryption;
166
253
  break;
167
254
  case "ECDSA":
168
- if (c = i.ecPublicKey, "namedCurve" in s) switch (s.namedCurve) {
255
+ if (c = o.ecPublicKey, "namedCurve" in s) switch (s.namedCurve) {
169
256
  case "P-256":
170
- l = i.prime256v1;
257
+ l = o.prime256v1;
171
258
  break;
172
259
  case "P-384":
173
- l = i.secp384r1;
260
+ l = o.secp384r1;
174
261
  break;
175
- case "P-521": l = i.secp521r1;
262
+ case "P-521": l = o.secp521r1;
176
263
  }
177
264
  break;
178
265
  case "Ed25519":
179
- c = i.ed25519;
266
+ c = o.ed25519;
180
267
  break;
181
268
  default: return unsupported(s.name, "unsupported public key algorithm");
182
269
  }
183
270
  let u = getVerifySignatureConfigResult(t, n, c, l);
184
- return u.ok ? o !== "raw" && o !== "der" ? unsupported(t, "signature encoding must be raw or der") : o === "der" && u.value.ecdsaRawSignatureBytes === void 0 ? unsupported(t, "DER signature encoding requires ECDSA") : s.name === u.value.verifyParams.name ? u.value.importAlgorithm.kind === "rsa" && (!("hash" in s) || typeof s.hash != "object" || s.hash === null || !("name" in s.hash) || s.hash.name !== u.value.importAlgorithm.hash) ? unsupported(t, "signature hash does not match the RSA public key") : await verifyWithConfiguredKey(e, u.value, r, a, o) : unsupported(t, "signature scheme does not match the public key") : u;
271
+ return u.ok ? a !== "raw" && a !== "der" ? unsupported(t, "signature encoding must be raw or der") : a === "der" && u.value.ecdsaRawSignatureBytes === void 0 ? unsupported(t, "DER signature encoding requires ECDSA") : s.name === u.value.verifyParams.name ? u.value.importAlgorithm.kind === "rsa" && (!("hash" in s) || typeof s.hash != "object" || s.hash === null || !("name" in s.hash) || s.hash.name !== u.value.importAlgorithm.hash) ? unsupported(t, "signature hash does not match the RSA public key") : await verifyWithConfiguredKey(e, u.value, r, i, a) : unsupported(t, "signature scheme does not match the public key") : u;
185
272
  }
186
273
  /** Parse RSA-PSS algorithm parameters and build a verification config, or return failure. */
187
274
  function requireRsaPssVerifyConfig(e, t, n) {
188
- let r = s(e);
275
+ let r = l(e);
189
276
  return r.ok ? requireRsaPublicKey(t, r.value.hash, "pss", n, r.value.saltLength) : unsupported("RSA-PSS", r.reason);
190
277
  }
191
278
  /** RFC 4055 §5 requires generators to encode NULL and implementations to accept absent parameters too. */
192
- function requireDerNullOrAbsentSignatureAlgorithmParameters(e, i) {
279
+ function requireDerNullOrAbsentSignatureAlgorithmParameters(e, t) {
193
280
  if (e === void 0) return;
194
281
  let a = "signature AlgorithmIdentifier parameters must be DER NULL or absent";
195
282
  try {
196
- let t = r(e);
197
- return t.tag !== 5 || t.length !== 0 || t.end !== e.length ? unsupported(i, a) : void 0;
283
+ let n = i(e);
284
+ return n.tag !== 5 || n.length !== 0 || n.end !== e.length ? unsupported(t, a) : void 0;
198
285
  } catch (e) {
199
- return n(e, t), unsupported(i, a);
286
+ return r(e, n), unsupported(t, a);
200
287
  }
201
288
  }
202
289
  function requireAbsentSignatureAlgorithmParameters(e, t) {
@@ -224,6 +311,6 @@ function verificationError(e) {
224
311
  reason: e
225
312
  };
226
313
  }
227
- export { getVerifySignatureConfigResult, requireEcPublicKey, requireRsaPublicKey, verifySignedDataDetailed, verifySignedDataWithKeyDetailed };
314
+ export { p as NO_CALLER_VERIFICATION, callerVerification, explicitParameters, getVerifySignatureConfigResult, requireEcPublicKey, requireRsaPublicKey, resolveSignatureVerifiers, spkiAlgorithmParametersDer, verifySignedDataDetailed, verifySignedDataWithKeyDetailed };
228
315
 
229
316
  //# sourceMappingURL=sig-verify.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sig-verify.js","names":["signatureAlgorithmOid","OIDS","parameters","signatureAlgorithmParametersDer","publicKeyAlgorithmOid","context","publicKeyParametersOid","algorithmOid","scheme","saltLength","parametersOid","config","importSpkiDerOrThrow","subjectPublicKeyInfoDer","signature","signedData","error","rethrowDecodeRefusal","DECODE_LIMIT_CODES","encoding","derEcdsaSignatureToRaw","subtle","getCrypto","signatureView","toArrayBuffer","dataView","key","alternate","alternateEcdsaSignatureEncoding","algorithm","oid","curveOid","parametersDer","data","parseRsaPssParameters","reason","element","readElement"],"sources":["../../../src/internal/crypto/sig-verify.ts"],"sourcesContent":["/**\n * Signature-verification helpers that resolve parsed algorithm OIDs into WebCrypto\n * verification configuration for higher-level validators.\n *\n * @module\n */\n\nimport { toArrayBuffer } from '#micro509/internal/asn1/asn1';\nimport { DECODE_LIMIT_CODES, rethrowDecodeRefusal } from '#micro509/internal/asn1/decode-refusal';\nimport { readElement } from '#micro509/internal/asn1/der';\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport {\n\talternateEcdsaSignatureEncoding,\n\tderEcdsaSignatureToRaw,\n} from '#micro509/internal/crypto/ecdsa';\nimport { parseRsaPssParameters } from '#micro509/internal/crypto/rsa-pss';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\nimport type { PublicKeyImportInput, RsaHash, RsaSignatureScheme } from '#micro509/keys/keys';\nimport { importSpkiDerOrThrow } from '#micro509/keys/keys';\n\nexport {\n\talternateEcdsaSignatureEncoding,\n\tconcatFixedWidth,\n\tderEcdsaSignatureToRaw,\n\trawEcdsaSignatureToDer,\n} from '#micro509/internal/crypto/ecdsa';\n\n/** Resolved WebCrypto parameters needed to verify a signature. */\nexport interface VerifySignatureConfig {\n\t/** Algorithm descriptor for importing the signer's public key via {@linkcode importSpkiDer}. */\n\treadonly importAlgorithm: PublicKeyImportInput;\n\t/** WebCrypto `verify()` algorithm parameter. */\n\treadonly verifyParams: Algorithm | EcdsaParams | RsaPssParams;\n\t/** When set, raw ECDSA signatures are this many bytes and may need DER/raw conversion. */\n\treadonly ecdsaRawSignatureBytes?: number;\n}\n\n/** Failure: the signature algorithm or its parameters are not supported. */\nexport interface VerifySignatureConfigFailure {\n\t/** Discriminant for the failure branch. */\n\treadonly ok: false;\n\t/** Machine-readable failure code. */\n\treadonly code: 'unsupported_signature_algorithm_parameters';\n\t/** Human-readable explanation of why the algorithm is unsupported. */\n\treadonly reason: string;\n}\n\n/** Failure: signature verification could not run to completion. */\nexport interface VerifySignedDataFailure {\n\t/** Discriminant for the failure branch. */\n\treadonly ok: false;\n\t/** Machine-readable failure code. */\n\treadonly code: 'verification_error';\n\t/** Human-readable explanation of the verification failure. */\n\treadonly reason: string;\n}\n\n/** Success branch of {@linkcode VerifySignatureConfigResult}. */\nexport interface VerifySignatureConfigSuccess {\n\t/** Discriminant for the success branch. */\n\treadonly ok: true;\n\t/** The resolved verification configuration. */\n\treadonly value: VerifySignatureConfig;\n}\n\n/** Result of resolving signature algorithm OIDs into a {@linkcode VerifySignatureConfig}. */\nexport type VerifySignatureConfigResult =\n\t| VerifySignatureConfigSuccess\n\t| VerifySignatureConfigFailure;\n\n/** Success branch of {@linkcode VerifySignedDataResult}. */\nexport interface VerifySignedDataSuccess {\n\t/** Discriminant for the success branch. */\n\treadonly ok: true;\n\t/** Whether the cryptographic signature is valid for the given data and key. */\n\treadonly valid: boolean;\n}\n\n/** Result of a full signature verification: validity, unsupported params, or a runtime verification failure. */\nexport type VerifySignedDataResult =\n\t| VerifySignedDataSuccess\n\t| VerifySignatureConfigFailure\n\t| VerifySignedDataFailure;\n\n/**\n * Resolve algorithm OIDs into a {@linkcode VerifySignatureConfig}.\n *\n * Throws on unsupported algorithms. Use {@linkcode getVerifySignatureConfigResult} for\n * a non-throwing variant.\n */\nexport function getVerifySignatureConfig(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tcontext = 'issuer',\n): VerifySignatureConfig {\n\tconst result = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tsignatureAlgorithmParametersDer,\n\t\tpublicKeyAlgorithmOid,\n\t\tpublicKeyParametersOid,\n\t\tcontext,\n\t);\n\tif (!result.ok) {\n\t\tthrow new Error(result.reason);\n\t}\n\treturn result.value;\n}\n\n/** Non-throwing variant of {@linkcode getVerifySignatureConfig} — returns a typed result union. */\nexport function getVerifySignatureConfigResult(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tcontext = 'issuer',\n): VerifySignatureConfigResult {\n\tswitch (signatureAlgorithmOid) {\n\t\tcase OIDS.sha256WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-256 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-256', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.sha384WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-384 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-384', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.sha512WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-512 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-512', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.rsassaPss:\n\t\t\treturn requireRsaPssVerifyConfig(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\tpublicKeyAlgorithmOid,\n\t\t\t\tcontext,\n\t\t\t);\n\t\tcase OIDS.ecdsaWithSHA256: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-256',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-256', context);\n\t\t}\n\t\tcase OIDS.ecdsaWithSHA384: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-384',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-384', context);\n\t\t}\n\t\tcase OIDS.ecdsaWithSHA512: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-512',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-512', context);\n\t\t}\n\t\tcase OIDS.ed25519: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'Ed25519',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\tif (publicKeyAlgorithmOid !== OIDS.ed25519) {\n\t\t\t\treturn unsupported('Ed25519', `requires Ed25519 ${context} public key`);\n\t\t\t}\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ed25519' },\n\t\t\t\tverifyParams: { name: 'Ed25519' },\n\t\t\t});\n\t\t}\n\t\tdefault:\n\t\t\treturn unsupported(signatureAlgorithmOid, 'unrecognized signature algorithm OID');\n\t}\n}\n\n/** Validate that the public key OID is `rsaEncryption` and return an RSA verification config result. */\nexport function requireRsaPublicKey(\n\talgorithmOid: string,\n\thash: RsaHash,\n\tscheme: RsaSignatureScheme = 'pkcs1-v1_5',\n\tcontext = 'issuer',\n\tsaltLength?: number,\n): VerifySignatureConfigResult {\n\tif (algorithmOid !== OIDS.rsaEncryption) {\n\t\treturn unsupported('RSA', `requires RSA ${context} public key`);\n\t}\n\tconst verifyParams: Algorithm | RsaPssParams =\n\t\tscheme === 'pss'\n\t\t\t? { name: 'RSA-PSS', saltLength: saltLength ?? 0 }\n\t\t\t: { name: 'RSASSA-PKCS1-v1_5' };\n\treturn ok({ importAlgorithm: { kind: 'rsa', hash, scheme }, verifyParams });\n}\n\n/** Validate that the public key OID is `ecPublicKey`, resolve the curve, and return an ECDSA verification config result. */\nexport function requireEcPublicKey(\n\talgorithmOid: string,\n\tparametersOid: string | undefined,\n\thash: string,\n\tcontext = 'issuer',\n): VerifySignatureConfigResult {\n\tif (algorithmOid !== OIDS.ecPublicKey) {\n\t\treturn unsupported('ECDSA', `requires EC ${context} public key`);\n\t}\n\tswitch (parametersOid) {\n\t\tcase OIDS.prime256v1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-256' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 64,\n\t\t\t});\n\t\tcase OIDS.secp384r1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-384' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 96,\n\t\t\t});\n\t\tcase OIDS.secp521r1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-521' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 132,\n\t\t\t});\n\t\tdefault:\n\t\t\treturn unsupported('ECDSA', `unsupported EC curve OID: ${parametersOid ?? 'missing'}`);\n\t}\n}\n\n/** Return the raw ECDSA signature byte count (r + s) for a given curve OID, if supported. */\nexport function curveBytes(parametersOid: string | undefined): number | undefined {\n\tswitch (parametersOid) {\n\t\tcase OIDS.prime256v1:\n\t\t\treturn 64;\n\t\tcase OIDS.secp384r1:\n\t\t\treturn 96;\n\t\tcase OIDS.secp521r1:\n\t\t\treturn 132;\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n\n/**\n * Verify a signature against the signer's SPKI and the signed TBS bytes.\n *\n * Throws on unsupported algorithms. Use {@linkcode verifySignedDataDetailed} for\n * a non-throwing variant.\n */\nexport async function verifySignedData(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tsubjectPublicKeyInfoDer: Uint8Array,\n\tsignature: Uint8Array,\n\tsignedData: Uint8Array,\n): Promise<boolean> {\n\tconst result = await verifySignedDataDetailed(\n\t\tsignatureAlgorithmOid,\n\t\tsignatureAlgorithmParametersDer,\n\t\tpublicKeyAlgorithmOid,\n\t\tpublicKeyParametersOid,\n\t\tsubjectPublicKeyInfoDer,\n\t\tsignature,\n\t\tsignedData,\n\t);\n\tif (!result.ok) {\n\t\tthrow new Error(result.reason);\n\t}\n\treturn result.valid;\n}\n\n/**\n * Non-throwing variant of {@linkcode verifySignedData} — returns a typed\n * {@linkcode VerifySignedDataResult} instead of throwing on unsupported algorithms\n * or runtime verification failures.\n *\n * Tries both DER and raw ECDSA encodings when the first attempt fails.\n *\n * @throws `limit_exceeded` on algorithm parameters or a public key that exceed\n * a micro509 decoding limit.\n */\nexport async function verifySignedDataDetailed(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tsubjectPublicKeyInfoDer: Uint8Array,\n\tsignature: Uint8Array,\n\tsignedData: Uint8Array,\n): Promise<VerifySignedDataResult> {\n\tconst config = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tsignatureAlgorithmParametersDer,\n\t\tpublicKeyAlgorithmOid,\n\t\tpublicKeyParametersOid,\n\t);\n\tif (!config.ok) {\n\t\treturn config;\n\t}\n\ttry {\n\t\tconst key = await importSpkiDerOrThrow(subjectPublicKeyInfoDer, config.value.importAlgorithm);\n\t\treturn await verifyWithConfiguredKey(key, config.value, signature, signedData, 'auto');\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn verificationError(\n\t\t\terror instanceof Error ? error.message : 'signature verification failed',\n\t\t);\n\t}\n}\n\n/** Execute verification for imported SPKI and caller-owned keys with the same configuration. */\nasync function verifyWithConfiguredKey(\n\tkey: CryptoKey,\n\tconfig: VerifySignatureConfig,\n\tsignature: Uint8Array,\n\tsignedData: Uint8Array,\n\tencoding: 'auto' | 'raw' | 'der',\n): Promise<VerifySignedDataResult> {\n\ttry {\n\t\tif (encoding === 'der' && config.ecdsaRawSignatureBytes !== undefined) {\n\t\t\ttry {\n\t\t\t\tsignature = derEcdsaSignatureToRaw(signature, config.ecdsaRawSignatureBytes / 2);\n\t\t\t} catch (error) {\n\t\t\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\t\t\treturn { ok: true, valid: false };\n\t\t\t}\n\t\t}\n\t\tconst subtle = getCrypto().subtle;\n\t\tconst signatureView = toArrayBuffer(signature);\n\t\tconst dataView = toArrayBuffer(signedData);\n\t\tif (await subtle.verify(config.verifyParams, key, signatureView, dataView)) {\n\t\t\treturn { ok: true, valid: true };\n\t\t}\n\t\tif (encoding === 'auto' && config.ecdsaRawSignatureBytes !== undefined) {\n\t\t\tconst alternate = alternateEcdsaSignatureEncoding(\n\t\t\t\tsignature,\n\t\t\t\tconfig.ecdsaRawSignatureBytes / 2,\n\t\t\t);\n\t\t\tif (alternate !== undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tok: true,\n\t\t\t\t\tvalid: await subtle.verify(config.verifyParams, key, toArrayBuffer(alternate), dataView),\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\treturn { ok: true, valid: false };\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn verificationError(\n\t\t\terror instanceof Error ? error.message : 'signature verification failed',\n\t\t);\n\t}\n}\n\n/** Resolve a caller-owned key without exporting it, then run the shared verifier. */\nexport async function verifySignedDataWithKeyDetailed(\n\tkey: CryptoKey,\n\tsignatureAlgorithmOid: string,\n\tparametersDer: Uint8Array | undefined,\n\tsignature: Uint8Array,\n\tdata: Uint8Array,\n\tencoding: 'raw' | 'der',\n): Promise<VerifySignedDataResult> {\n\tif (key.type !== 'public' || !key.usages.includes('verify')) {\n\t\treturn verificationError('Signature verification requires a public key with verify usage');\n\t}\n\tconst algorithm = key.algorithm;\n\tlet oid: string;\n\tlet curveOid: string | undefined;\n\tswitch (algorithm.name) {\n\t\tcase 'RSASSA-PKCS1-v1_5':\n\t\tcase 'RSA-PSS':\n\t\t\toid = OIDS.rsaEncryption;\n\t\t\tbreak;\n\t\tcase 'ECDSA':\n\t\t\toid = OIDS.ecPublicKey;\n\t\t\tif ('namedCurve' in algorithm) {\n\t\t\t\tswitch (algorithm.namedCurve) {\n\t\t\t\t\tcase 'P-256':\n\t\t\t\t\t\tcurveOid = OIDS.prime256v1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'P-384':\n\t\t\t\t\t\tcurveOid = OIDS.secp384r1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'P-521':\n\t\t\t\t\t\tcurveOid = OIDS.secp521r1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 'Ed25519':\n\t\t\toid = OIDS.ed25519;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn unsupported(algorithm.name, 'unsupported public key algorithm');\n\t}\n\tconst config = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tparametersDer,\n\t\toid,\n\t\tcurveOid,\n\t);\n\tif (!config.ok) return config;\n\tif (encoding !== 'raw' && encoding !== 'der') {\n\t\treturn unsupported(signatureAlgorithmOid, 'signature encoding must be raw or der');\n\t}\n\tif (encoding === 'der' && config.value.ecdsaRawSignatureBytes === undefined) {\n\t\treturn unsupported(signatureAlgorithmOid, 'DER signature encoding requires ECDSA');\n\t}\n\tif (algorithm.name !== config.value.verifyParams.name) {\n\t\treturn unsupported(signatureAlgorithmOid, 'signature scheme does not match the public key');\n\t}\n\tif (config.value.importAlgorithm.kind === 'rsa') {\n\t\tif (\n\t\t\t!('hash' in algorithm) ||\n\t\t\ttypeof algorithm.hash !== 'object' ||\n\t\t\talgorithm.hash === null ||\n\t\t\t!('name' in algorithm.hash) ||\n\t\t\talgorithm.hash.name !== config.value.importAlgorithm.hash\n\t\t) {\n\t\t\treturn unsupported(signatureAlgorithmOid, 'signature hash does not match the RSA public key');\n\t\t}\n\t}\n\treturn await verifyWithConfiguredKey(key, config.value, signature, data, encoding);\n}\n\n/** Parse RSA-PSS algorithm parameters and build a verification config, or return failure. */\nfunction requireRsaPssVerifyConfig(\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tcontext: string,\n): VerifySignatureConfigResult {\n\tconst parameters = parseRsaPssParameters(signatureAlgorithmParametersDer);\n\tif (!parameters.ok) {\n\t\treturn unsupported('RSA-PSS', parameters.reason);\n\t}\n\treturn requireRsaPublicKey(\n\t\tpublicKeyAlgorithmOid,\n\t\tparameters.value.hash,\n\t\t'pss',\n\t\tcontext,\n\t\tparameters.value.saltLength,\n\t);\n}\n\n/** RFC 4055 §5 requires generators to encode NULL and implementations to accept absent parameters too. */\nfunction requireDerNullOrAbsentSignatureAlgorithmParameters(\n\tparametersDer: Uint8Array | undefined,\n\talgorithm: string,\n): VerifySignatureConfigFailure | undefined {\n\tif (parametersDer === undefined) {\n\t\treturn undefined;\n\t}\n\tconst reason = 'signature AlgorithmIdentifier parameters must be DER NULL or absent';\n\ttry {\n\t\tconst element = readElement(parametersDer);\n\t\tif (element.tag !== 0x05 || element.length !== 0 || element.end !== parametersDer.length) {\n\t\t\treturn unsupported(algorithm, reason);\n\t\t}\n\t\treturn undefined;\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn unsupported(algorithm, reason);\n\t}\n}\n\nfunction requireAbsentSignatureAlgorithmParameters(\n\tparametersDer: Uint8Array | undefined,\n\talgorithm: string,\n): VerifySignatureConfigFailure | undefined {\n\tif (parametersDer !== undefined) {\n\t\treturn unsupported(algorithm, 'signature AlgorithmIdentifier parameters must be absent');\n\t}\n\treturn undefined;\n}\n\n/** Wrap a config in a success result. */\nfunction ok(value: VerifySignatureConfig): VerifySignatureConfigSuccess {\n\treturn { ok: true, value };\n}\n\n/** Build an unsupported-algorithm failure result. */\nfunction unsupported(algorithm: string, reason: string): VerifySignatureConfigFailure {\n\treturn {\n\t\tok: false,\n\t\tcode: 'unsupported_signature_algorithm_parameters',\n\t\treason: `${algorithm} parameters unsupported: ${reason}`,\n\t};\n}\n\nfunction verificationError(reason: string): VerifySignedDataFailure {\n\treturn {\n\t\tok: false,\n\t\tcode: 'verification_error',\n\t\treason,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA+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,QAAQ,KAAQA,EAAQ,WAAW,KAAKA,EAAQ,QAAQJ,EAAc,SAC1E,YAAYH,GAAWM,CAAM,IAErC;CACD,SAASnB,GAAO;EAEf,OADAC,EAAqBD,GAAOE,CAAkB,GACvC,YAAYW,GAAWM,CAAM;CACrC;AACD;AAEA,SAAS,0CACR,GACA,GAC2C;CAC3C,IAAIH,MAAkB,KAAA,GACrB,OAAO,YAAYH,GAAW,yDAAyD;AAGzF;;AAGA,SAAS,GAAG,GAA4D;CACvE,OAAO;EAAE,IAAI;EAAM;CAAM;AAC1B;;AAGA,SAAS,YAAY,GAAmB,GAA8C;CACrF,OAAO;EACN,IAAI;EACJ,MAAM;EACN,QAAQ,GAAGA,EAAU,2BAA2BM;CACjD;AACD;AAEA,SAAS,kBAAkB,GAAyC;CACnE,OAAO;EACN,IAAI;EACJ,MAAM;EACN;CACD;AACD"}
1
+ {"version":3,"file":"sig-verify.js","names":["canonicalizeOid","toArrayBuffer","DECODE_LIMIT_CODES","rethrowDecodeRefusal","readElement","readSequenceChildren","OIDS","alternateEcdsaSignatureEncoding","derEcdsaSignatureToRaw","parseRsaPssParameters","getCrypto","importSpkiDerOrThrow","NATIVE_SIGNATURE_ALGORITHMS","verifiers","resolved","oid","verifier","canonical","error","NO_CALLER_VERIFICATION","parametersDer","spkiDer","algorithm","algorithmDer","parameters","signatureAlgorithmOid","signatureAlgorithmParametersDer","publicKeyAlgorithmOid","publicKeyParametersOid","context","algorithmOid","hash","scheme","saltLength","parametersOid","subjectPublicKeyInfoDer","signature","signatureUnusedBits","signedData","caller","config","signerParametersDer","input","key","encoding","subtle","signatureView","dataView","alternate","data","curveOid","reason","element","value"],"sources":["../../../src/internal/crypto/sig-verify.ts"],"sourcesContent":["/**\n * Signature-verification helpers that resolve parsed algorithm OIDs into WebCrypto\n * verification configuration for higher-level validators.\n *\n * @module\n */\n\nimport { canonicalizeOid, toArrayBuffer } from '#micro509/internal/asn1/asn1';\nimport { DECODE_LIMIT_CODES, rethrowDecodeRefusal } from '#micro509/internal/asn1/decode-refusal';\nimport { readElement, readSequenceChildren } from '#micro509/internal/asn1/der';\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport {\n\talternateEcdsaSignatureEncoding,\n\tderEcdsaSignatureToRaw,\n} from '#micro509/internal/crypto/ecdsa';\nimport { parseRsaPssParameters } from '#micro509/internal/crypto/rsa-pss';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\nimport type { PublicKeyImportInput, RsaHash, RsaSignatureScheme } from '#micro509/keys/keys';\nimport { importSpkiDerOrThrow } from '#micro509/keys/keys';\n\nexport {\n\talternateEcdsaSignatureEncoding,\n\tconcatFixedWidth,\n\tderEcdsaSignatureToRaw,\n\trawEcdsaSignatureToDer,\n} from '#micro509/internal/crypto/ecdsa';\n\n/** One signature handed to a {@linkcode SignatureVerifier}. */\nexport interface SignatureVerifierInput {\n\t/** The signature algorithm as carried beside the signature. */\n\treadonly signatureAlgorithm: {\n\t\t/** Dotted-decimal signature algorithm OID. */\n\t\treadonly oid: string;\n\t\t/** DER algorithm parameters, when the signature carries them. */\n\t\treadonly parametersDer?: Uint8Array;\n\t};\n\t/** DER `SubjectPublicKeyInfo` of the signer. */\n\treadonly signerSpkiDer: Uint8Array;\n\t/**\n\t * DER parameters to verify with: RFC 5280 §6.1 `working_public_key_parameters`.\n\t * These are the signer's own key parameters, or the parameters it inherits along\n\t * the certification path when its own are absent or NULL (RFC 5280 §6.1.4 (e),\n\t * RFC 3279 §2.3.2). Absent when the path leaves none.\n\t */\n\treadonly signerPublicKeyParametersDer?: Uint8Array;\n\t/** The signature bytes as carried. */\n\treadonly signature: Uint8Array;\n\t/**\n\t * Unused bits in the final octet of `signature`, from a `signatureValue`\n\t * BIT STRING. `0` for a signature carried as octets.\n\t */\n\treadonly signatureUnusedBits: number;\n\t/** The exact bytes the signature covers. */\n\treadonly data: Uint8Array;\n}\n\n/**\n * Caller-supplied verification for a signature algorithm micro509 does not run\n * through WebCrypto. Resolves `true` only for a valid signature. A rejection is\n * reported as a verification error.\n */\nexport type SignatureVerifier = (input: SignatureVerifierInput) => Promise<boolean>;\n\n/**\n * Caller verifiers keyed by dotted-decimal signature algorithm OID. A key that\n * micro509 verifies natively throws `RangeError`, so a caller verifier never\n * replaces WebCrypto verification.\n */\nexport type SignatureVerifiers = Readonly<Record<string, SignatureVerifier>>;\n\nconst NATIVE_SIGNATURE_ALGORITHMS: ReadonlySet<string> = new Set([\n\tOIDS.sha256WithRSAEncryption,\n\tOIDS.sha384WithRSAEncryption,\n\tOIDS.sha512WithRSAEncryption,\n\tOIDS.rsassaPss,\n\tOIDS.ecdsaWithSHA256,\n\tOIDS.ecdsaWithSHA384,\n\tOIDS.ecdsaWithSHA512,\n\tOIDS.ed25519,\n]);\n\n/**\n * Validates caller verifiers and indexes them by canonical OID.\n *\n * @throws RangeError for a key that is no encodable OID, a natively verified\n * algorithm, or a value that is no function.\n */\nexport function resolveSignatureVerifiers(\n\tverifiers: SignatureVerifiers | undefined,\n): ReadonlyMap<string, SignatureVerifier> {\n\tconst resolved = new Map<string, SignatureVerifier>();\n\tif (verifiers === undefined) return resolved;\n\tfor (const [oid, verifier] of Object.entries(verifiers)) {\n\t\tconst canonical = canonicalVerifierOid(oid);\n\t\tif (NATIVE_SIGNATURE_ALGORITHMS.has(canonical)) {\n\t\t\tthrow new RangeError(`signatureVerifiers cannot replace native verification of ${oid}`);\n\t\t}\n\t\tif (typeof verifier !== 'function') {\n\t\t\tthrow new RangeError(`signatureVerifiers value for ${oid} is not a function`);\n\t\t}\n\t\tif (resolved.has(canonical)) {\n\t\t\tthrow new RangeError(`signatureVerifiers names ${canonical} twice`);\n\t\t}\n\t\tresolved.set(canonical, verifier);\n\t}\n\treturn resolved;\n}\n\nfunction canonicalVerifierOid(oid: string): string {\n\tif (!/^\\d+(?:\\.\\d+)+$/.test(oid)) {\n\t\tthrow new RangeError(`signatureVerifiers key is not an OID: ${oid}`);\n\t}\n\ttry {\n\t\treturn canonicalizeOid(oid);\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\tthrow new RangeError(`signatureVerifiers key is not an OID: ${oid}`);\n\t}\n}\n\n/** The caller verifiers for one signature, and the key parameters a certification path gives its signer. */\nexport interface CallerVerification {\n\t/** Resolved caller verifiers, keyed by canonical signature algorithm OID. */\n\treadonly verifiers: ReadonlyMap<string, SignatureVerifier>;\n\t/**\n\t * RFC 5280 §6.1 `working_public_key_parameters` of the signer along its path.\n\t * Omitted to use the parameters in the signer's own SubjectPublicKeyInfo.\n\t */\n\treadonly workingParameters?: { readonly parametersDer: Uint8Array | undefined };\n}\n\n/** No caller verifiers: every signature goes through WebCrypto. */\nexport const NO_CALLER_VERIFICATION: CallerVerification = { verifiers: new Map() };\n\n/** Caller verification with `verifiers` and the signer's own key parameters. */\nexport function callerVerification(verifiers: SignatureVerifiers | undefined): CallerVerification {\n\treturn { verifiers: resolveSignatureVerifiers(verifiers) };\n}\n\n/** DER key parameters, or `undefined` when they are absent or NULL (RFC 5280 §6.1.4 (e)). */\nexport function explicitParameters(parametersDer: Uint8Array | undefined): Uint8Array | undefined {\n\tif (parametersDer === undefined) return undefined;\n\treturn parametersDer.length === 2 && parametersDer[0] === 0x05 && parametersDer[1] === 0x00\n\t\t? undefined\n\t\t: parametersDer;\n}\n\n/** The AlgorithmIdentifier parameters of a SubjectPublicKeyInfo; a malformed one has none. */\nexport function spkiAlgorithmParametersDer(spkiDer: Uint8Array): Uint8Array | undefined {\n\ttry {\n\t\tconst [algorithm] = readSequenceChildren(spkiDer);\n\t\tif (algorithm === undefined) return undefined;\n\t\tconst algorithmDer = spkiDer.subarray(algorithm.start - algorithm.headerLength, algorithm.end);\n\t\tconst [, parameters] = readSequenceChildren(algorithmDer);\n\t\treturn parameters === undefined\n\t\t\t? undefined\n\t\t\t: algorithmDer.subarray(parameters.start - parameters.headerLength, parameters.end);\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn undefined;\n\t}\n}\n\n/** Resolved WebCrypto parameters needed to verify a signature. */\nexport interface VerifySignatureConfig {\n\t/** Algorithm descriptor for importing the signer's public key via {@linkcode importSpkiDer}. */\n\treadonly importAlgorithm: PublicKeyImportInput;\n\t/** WebCrypto `verify()` algorithm parameter. */\n\treadonly verifyParams: Algorithm | EcdsaParams | RsaPssParams;\n\t/** When set, raw ECDSA signatures are this many bytes and may need DER/raw conversion. */\n\treadonly ecdsaRawSignatureBytes?: number;\n}\n\n/** Failure: the signature algorithm or its parameters are not supported. */\nexport interface VerifySignatureConfigFailure {\n\t/** Discriminant for the failure branch. */\n\treadonly ok: false;\n\t/** Machine-readable failure code. */\n\treadonly code: 'unsupported_signature_algorithm_parameters';\n\t/** Human-readable explanation of why the algorithm is unsupported. */\n\treadonly reason: string;\n}\n\n/** Failure: signature verification could not run to completion. */\nexport interface VerifySignedDataFailure {\n\t/** Discriminant for the failure branch. */\n\treadonly ok: false;\n\t/** Machine-readable failure code. */\n\treadonly code: 'verification_error';\n\t/** Human-readable explanation of the verification failure. */\n\treadonly reason: string;\n}\n\n/** Success branch of {@linkcode VerifySignatureConfigResult}. */\nexport interface VerifySignatureConfigSuccess {\n\t/** Discriminant for the success branch. */\n\treadonly ok: true;\n\t/** The resolved verification configuration. */\n\treadonly value: VerifySignatureConfig;\n}\n\n/** Result of resolving signature algorithm OIDs into a {@linkcode VerifySignatureConfig}. */\nexport type VerifySignatureConfigResult =\n\t| VerifySignatureConfigSuccess\n\t| VerifySignatureConfigFailure;\n\n/** Success branch of {@linkcode VerifySignedDataResult}. */\nexport interface VerifySignedDataSuccess {\n\t/** Discriminant for the success branch. */\n\treadonly ok: true;\n\t/** Whether the cryptographic signature is valid for the given data and key. */\n\treadonly valid: boolean;\n}\n\n/** Result of a full signature verification: validity, unsupported params, or a runtime verification failure. */\nexport type VerifySignedDataResult =\n\t| VerifySignedDataSuccess\n\t| VerifySignatureConfigFailure\n\t| VerifySignedDataFailure;\n\n/**\n * Resolve algorithm OIDs into a {@linkcode VerifySignatureConfig}.\n *\n * Throws on unsupported algorithms. Use {@linkcode getVerifySignatureConfigResult} for\n * a non-throwing variant.\n */\nexport function getVerifySignatureConfig(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tcontext = 'issuer',\n): VerifySignatureConfig {\n\tconst result = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tsignatureAlgorithmParametersDer,\n\t\tpublicKeyAlgorithmOid,\n\t\tpublicKeyParametersOid,\n\t\tcontext,\n\t);\n\tif (!result.ok) {\n\t\tthrow new Error(result.reason);\n\t}\n\treturn result.value;\n}\n\n/** Non-throwing variant of {@linkcode getVerifySignatureConfig} — returns a typed result union. */\nexport function getVerifySignatureConfigResult(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tcontext = 'issuer',\n): VerifySignatureConfigResult {\n\tswitch (signatureAlgorithmOid) {\n\t\tcase OIDS.sha256WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-256 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-256', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.sha384WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-384 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-384', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.sha512WithRSAEncryption: {\n\t\t\tconst parameters = requireDerNullOrAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'SHA-512 with RSA',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireRsaPublicKey(publicKeyAlgorithmOid, 'SHA-512', 'pkcs1-v1_5', context);\n\t\t}\n\t\tcase OIDS.rsassaPss:\n\t\t\treturn requireRsaPssVerifyConfig(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\tpublicKeyAlgorithmOid,\n\t\t\t\tcontext,\n\t\t\t);\n\t\tcase OIDS.ecdsaWithSHA256: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-256',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-256', context);\n\t\t}\n\t\tcase OIDS.ecdsaWithSHA384: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-384',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-384', context);\n\t\t}\n\t\tcase OIDS.ecdsaWithSHA512: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'ECDSA with SHA-512',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\treturn requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, 'SHA-512', context);\n\t\t}\n\t\tcase OIDS.ed25519: {\n\t\t\tconst parameters = requireAbsentSignatureAlgorithmParameters(\n\t\t\t\tsignatureAlgorithmParametersDer,\n\t\t\t\t'Ed25519',\n\t\t\t);\n\t\t\tif (parameters !== undefined) {\n\t\t\t\treturn parameters;\n\t\t\t}\n\t\t\tif (publicKeyAlgorithmOid !== OIDS.ed25519) {\n\t\t\t\treturn unsupported('Ed25519', `requires Ed25519 ${context} public key`);\n\t\t\t}\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ed25519' },\n\t\t\t\tverifyParams: { name: 'Ed25519' },\n\t\t\t});\n\t\t}\n\t\tdefault:\n\t\t\treturn unsupported(signatureAlgorithmOid, 'unrecognized signature algorithm OID');\n\t}\n}\n\n/** Validate that the public key OID is `rsaEncryption` and return an RSA verification config result. */\nexport function requireRsaPublicKey(\n\talgorithmOid: string,\n\thash: RsaHash,\n\tscheme: RsaSignatureScheme = 'pkcs1-v1_5',\n\tcontext = 'issuer',\n\tsaltLength?: number,\n): VerifySignatureConfigResult {\n\tif (algorithmOid !== OIDS.rsaEncryption) {\n\t\treturn unsupported('RSA', `requires RSA ${context} public key`);\n\t}\n\tconst verifyParams: Algorithm | RsaPssParams =\n\t\tscheme === 'pss'\n\t\t\t? { name: 'RSA-PSS', saltLength: saltLength ?? 0 }\n\t\t\t: { name: 'RSASSA-PKCS1-v1_5' };\n\treturn ok({ importAlgorithm: { kind: 'rsa', hash, scheme }, verifyParams });\n}\n\n/** Validate that the public key OID is `ecPublicKey`, resolve the curve, and return an ECDSA verification config result. */\nexport function requireEcPublicKey(\n\talgorithmOid: string,\n\tparametersOid: string | undefined,\n\thash: string,\n\tcontext = 'issuer',\n): VerifySignatureConfigResult {\n\tif (algorithmOid !== OIDS.ecPublicKey) {\n\t\treturn unsupported('ECDSA', `requires EC ${context} public key`);\n\t}\n\tswitch (parametersOid) {\n\t\tcase OIDS.prime256v1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-256' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 64,\n\t\t\t});\n\t\tcase OIDS.secp384r1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-384' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 96,\n\t\t\t});\n\t\tcase OIDS.secp521r1:\n\t\t\treturn ok({\n\t\t\t\timportAlgorithm: { kind: 'ecdsa', curve: 'P-521' },\n\t\t\t\tverifyParams: { name: 'ECDSA', hash },\n\t\t\t\tecdsaRawSignatureBytes: 132,\n\t\t\t});\n\t\tdefault:\n\t\t\treturn unsupported('ECDSA', `unsupported EC curve OID: ${parametersOid ?? 'missing'}`);\n\t}\n}\n\n/** Return the raw ECDSA signature byte count (r + s) for a given curve OID, if supported. */\nexport function curveBytes(parametersOid: string | undefined): number | undefined {\n\tswitch (parametersOid) {\n\t\tcase OIDS.prime256v1:\n\t\t\treturn 64;\n\t\tcase OIDS.secp384r1:\n\t\t\treturn 96;\n\t\tcase OIDS.secp521r1:\n\t\t\treturn 132;\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n\n/**\n * Verifies a signature against the signer's SPKI and the signed bytes. Returns\n * a typed {@linkcode VerifySignedDataResult} for unsupported algorithms and\n * runtime verification failures.\n *\n * Tries both DER and raw ECDSA encodings when the first attempt fails.\n *\n * @throws `limit_exceeded` on algorithm parameters or a public key that exceed\n * a micro509 decoding limit.\n */\nexport async function verifySignedDataDetailed(\n\tsignatureAlgorithmOid: string,\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tpublicKeyParametersOid: string | undefined,\n\tsubjectPublicKeyInfoDer: Uint8Array,\n\tsignature: Uint8Array,\n\tsignatureUnusedBits: number,\n\tsignedData: Uint8Array,\n\tcaller: CallerVerification = NO_CALLER_VERIFICATION,\n): Promise<VerifySignedDataResult> {\n\tconst config = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tsignatureAlgorithmParametersDer,\n\t\tpublicKeyAlgorithmOid,\n\t\tpublicKeyParametersOid,\n\t);\n\tif (!config.ok) {\n\t\tconst verifier = caller.verifiers.get(signatureAlgorithmOid);\n\t\tif (verifier === undefined) {\n\t\t\treturn config;\n\t\t}\n\t\tconst signerParametersDer =\n\t\t\tcaller.workingParameters === undefined\n\t\t\t\t? explicitParameters(spkiAlgorithmParametersDer(subjectPublicKeyInfoDer))\n\t\t\t\t: caller.workingParameters.parametersDer;\n\t\treturn await runCallerVerifier(verifier, {\n\t\t\tsignatureAlgorithm: {\n\t\t\t\toid: signatureAlgorithmOid,\n\t\t\t\t...(signatureAlgorithmParametersDer === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: { parametersDer: new Uint8Array(signatureAlgorithmParametersDer) }),\n\t\t\t},\n\t\t\tsignerSpkiDer: new Uint8Array(subjectPublicKeyInfoDer),\n\t\t\t...(signerParametersDer === undefined\n\t\t\t\t? {}\n\t\t\t\t: { signerPublicKeyParametersDer: new Uint8Array(signerParametersDer) }),\n\t\t\tsignature: new Uint8Array(signature),\n\t\t\tsignatureUnusedBits,\n\t\t\tdata: new Uint8Array(signedData),\n\t\t});\n\t}\n\tif (signatureUnusedBits !== 0) {\n\t\treturn { ok: true, valid: false };\n\t}\n\ttry {\n\t\tconst key = await importSpkiDerOrThrow(subjectPublicKeyInfoDer, config.value.importAlgorithm);\n\t\treturn await verifyWithConfiguredKey(key, config.value, signature, signedData, 'auto');\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn verificationError(\n\t\t\terror instanceof Error ? error.message : 'signature verification failed',\n\t\t);\n\t}\n}\n\nasync function runCallerVerifier(\n\tverifier: SignatureVerifier,\n\tinput: SignatureVerifierInput,\n): Promise<VerifySignedDataResult> {\n\ttry {\n\t\treturn { ok: true, valid: (await verifier(input)) === true };\n\t} catch (error) {\n\t\treturn verificationError(\n\t\t\terror instanceof Error\n\t\t\t\t? `signature verifier failed: ${error.message}`\n\t\t\t\t: 'signature verifier failed',\n\t\t);\n\t}\n}\n\n/** Execute verification for imported SPKI and caller-owned keys with the same configuration. */\nasync function verifyWithConfiguredKey(\n\tkey: CryptoKey,\n\tconfig: VerifySignatureConfig,\n\tsignature: Uint8Array,\n\tsignedData: Uint8Array,\n\tencoding: 'auto' | 'raw' | 'der',\n): Promise<VerifySignedDataResult> {\n\ttry {\n\t\tif (encoding === 'der' && config.ecdsaRawSignatureBytes !== undefined) {\n\t\t\ttry {\n\t\t\t\tsignature = derEcdsaSignatureToRaw(signature, config.ecdsaRawSignatureBytes / 2);\n\t\t\t} catch (error) {\n\t\t\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\t\t\treturn { ok: true, valid: false };\n\t\t\t}\n\t\t}\n\t\tconst subtle = getCrypto().subtle;\n\t\tconst signatureView = toArrayBuffer(signature);\n\t\tconst dataView = toArrayBuffer(signedData);\n\t\tif (await subtle.verify(config.verifyParams, key, signatureView, dataView)) {\n\t\t\treturn { ok: true, valid: true };\n\t\t}\n\t\tif (encoding === 'auto' && config.ecdsaRawSignatureBytes !== undefined) {\n\t\t\tconst alternate = alternateEcdsaSignatureEncoding(\n\t\t\t\tsignature,\n\t\t\t\tconfig.ecdsaRawSignatureBytes / 2,\n\t\t\t);\n\t\t\tif (alternate !== undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tok: true,\n\t\t\t\t\tvalid: await subtle.verify(config.verifyParams, key, toArrayBuffer(alternate), dataView),\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\treturn { ok: true, valid: false };\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn verificationError(\n\t\t\terror instanceof Error ? error.message : 'signature verification failed',\n\t\t);\n\t}\n}\n\n/** Resolve a caller-owned key without exporting it, then run the shared verifier. */\nexport async function verifySignedDataWithKeyDetailed(\n\tkey: CryptoKey,\n\tsignatureAlgorithmOid: string,\n\tparametersDer: Uint8Array | undefined,\n\tsignature: Uint8Array,\n\tdata: Uint8Array,\n\tencoding: 'raw' | 'der',\n): Promise<VerifySignedDataResult> {\n\tif (key.type !== 'public' || !key.usages.includes('verify')) {\n\t\treturn verificationError('Signature verification requires a public key with verify usage');\n\t}\n\tconst algorithm = key.algorithm;\n\tlet oid: string;\n\tlet curveOid: string | undefined;\n\tswitch (algorithm.name) {\n\t\tcase 'RSASSA-PKCS1-v1_5':\n\t\tcase 'RSA-PSS':\n\t\t\toid = OIDS.rsaEncryption;\n\t\t\tbreak;\n\t\tcase 'ECDSA':\n\t\t\toid = OIDS.ecPublicKey;\n\t\t\tif ('namedCurve' in algorithm) {\n\t\t\t\tswitch (algorithm.namedCurve) {\n\t\t\t\t\tcase 'P-256':\n\t\t\t\t\t\tcurveOid = OIDS.prime256v1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'P-384':\n\t\t\t\t\t\tcurveOid = OIDS.secp384r1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'P-521':\n\t\t\t\t\t\tcurveOid = OIDS.secp521r1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 'Ed25519':\n\t\t\toid = OIDS.ed25519;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn unsupported(algorithm.name, 'unsupported public key algorithm');\n\t}\n\tconst config = getVerifySignatureConfigResult(\n\t\tsignatureAlgorithmOid,\n\t\tparametersDer,\n\t\toid,\n\t\tcurveOid,\n\t);\n\tif (!config.ok) return config;\n\tif (encoding !== 'raw' && encoding !== 'der') {\n\t\treturn unsupported(signatureAlgorithmOid, 'signature encoding must be raw or der');\n\t}\n\tif (encoding === 'der' && config.value.ecdsaRawSignatureBytes === undefined) {\n\t\treturn unsupported(signatureAlgorithmOid, 'DER signature encoding requires ECDSA');\n\t}\n\tif (algorithm.name !== config.value.verifyParams.name) {\n\t\treturn unsupported(signatureAlgorithmOid, 'signature scheme does not match the public key');\n\t}\n\tif (config.value.importAlgorithm.kind === 'rsa') {\n\t\tif (\n\t\t\t!('hash' in algorithm) ||\n\t\t\ttypeof algorithm.hash !== 'object' ||\n\t\t\talgorithm.hash === null ||\n\t\t\t!('name' in algorithm.hash) ||\n\t\t\talgorithm.hash.name !== config.value.importAlgorithm.hash\n\t\t) {\n\t\t\treturn unsupported(signatureAlgorithmOid, 'signature hash does not match the RSA public key');\n\t\t}\n\t}\n\treturn await verifyWithConfiguredKey(key, config.value, signature, data, encoding);\n}\n\n/** Parse RSA-PSS algorithm parameters and build a verification config, or return failure. */\nfunction requireRsaPssVerifyConfig(\n\tsignatureAlgorithmParametersDer: Uint8Array | undefined,\n\tpublicKeyAlgorithmOid: string,\n\tcontext: string,\n): VerifySignatureConfigResult {\n\tconst parameters = parseRsaPssParameters(signatureAlgorithmParametersDer);\n\tif (!parameters.ok) {\n\t\treturn unsupported('RSA-PSS', parameters.reason);\n\t}\n\treturn requireRsaPublicKey(\n\t\tpublicKeyAlgorithmOid,\n\t\tparameters.value.hash,\n\t\t'pss',\n\t\tcontext,\n\t\tparameters.value.saltLength,\n\t);\n}\n\n/** RFC 4055 §5 requires generators to encode NULL and implementations to accept absent parameters too. */\nfunction requireDerNullOrAbsentSignatureAlgorithmParameters(\n\tparametersDer: Uint8Array | undefined,\n\talgorithm: string,\n): VerifySignatureConfigFailure | undefined {\n\tif (parametersDer === undefined) {\n\t\treturn undefined;\n\t}\n\tconst reason = 'signature AlgorithmIdentifier parameters must be DER NULL or absent';\n\ttry {\n\t\tconst element = readElement(parametersDer);\n\t\tif (element.tag !== 0x05 || element.length !== 0 || element.end !== parametersDer.length) {\n\t\t\treturn unsupported(algorithm, reason);\n\t\t}\n\t\treturn undefined;\n\t} catch (error) {\n\t\trethrowDecodeRefusal(error, DECODE_LIMIT_CODES);\n\t\treturn unsupported(algorithm, reason);\n\t}\n}\n\nfunction requireAbsentSignatureAlgorithmParameters(\n\tparametersDer: Uint8Array | undefined,\n\talgorithm: string,\n): VerifySignatureConfigFailure | undefined {\n\tif (parametersDer !== undefined) {\n\t\treturn unsupported(algorithm, 'signature AlgorithmIdentifier parameters must be absent');\n\t}\n\treturn undefined;\n}\n\n/** Wrap a config in a success result. */\nfunction ok(value: VerifySignatureConfig): VerifySignatureConfigSuccess {\n\treturn { ok: true, value };\n}\n\n/** Build an unsupported-algorithm failure result. */\nfunction unsupported(algorithm: string, reason: string): VerifySignatureConfigFailure {\n\treturn {\n\t\tok: false,\n\t\tcode: 'unsupported_signature_algorithm_parameters',\n\t\treason: `${algorithm} parameters unsupported: ${reason}`,\n\t};\n}\n\nfunction verificationError(reason: string): VerifySignedDataFailure {\n\treturn {\n\t\tok: false,\n\t\tcode: 'verification_error',\n\t\treason,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;AAsEA,MAAMY,oBAAmD,IAAI,IAAI;CAChEN,EAAK;CACLA,EAAK;CACLA,EAAK;CACLA,EAAK;CACLA,EAAK;CACLA,EAAK;CACLA,EAAK;CACLA,EAAK;AACN,CAAC;;;;;;;AAQD,SAAgB,0BACfO,GACyC;CACzC,IAAMC,oBAAW,IAAI,IAA+B;CACpD,IAAID,MAAc,KAAA,GAAW,OAAOC;CACpC,KAAK,IAAM,CAACC,GAAKC,MAAa,OAAO,QAAQH,CAAS,GAAG;EACxD,IAAMI,IAAY,qBAAqBF,CAAG;EAC1C,IAAIH,EAA4B,IAAIK,CAAS,GAC5C,MAAU,WAAW,4DAA4DF,GAAK;EAEvF,IAAI,OAAOC,KAAa,YACvB,MAAU,WAAW,gCAAgCD,EAAI,mBAAmB;EAE7E,IAAID,EAAS,IAAIG,CAAS,GACzB,MAAU,WAAW,4BAA4BA,EAAU,OAAO;EAEnEH,EAAS,IAAIG,GAAWD,CAAQ;CACjC;CACA,OAAOF;AACR;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- {"version":3,"file":"signing.js","names":["algorithm","privateKey","profile","OIDS","nullValue","hash","parameters","rsaPssParametersForHash","saltLength","encodeRsaPssParameters","parts","objectIdentifier","input","sequence","view","data","signature","getCrypto","ecdsaSignatureToDer"],"sources":["../../../src/internal/crypto/signing.ts"],"sourcesContent":["/**\n * Signing helpers that map WebCrypto keys and signature profiles to on-wire\n * `AlgorithmIdentifier` values. Used by certificate and CSR builders.\n *\n * @module\n */\n\nimport { nullValue, objectIdentifier, sequence } from '#micro509/internal/asn1/der';\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport { ecdsaSignatureToDer } from '#micro509/internal/crypto/ecdsa';\nimport type { RsaPssHash } from '#micro509/internal/crypto/rsa-pss';\nimport { encodeRsaPssParameters, rsaPssParametersForHash } from '#micro509/internal/crypto/rsa-pss';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\n\n/**\n * Controls how the signature algorithm is chosen.\n *\n * `'auto'` (default) infers the algorithm from the key. `'rsa-pss'` forces RSA-PSS\n * padding and requires an RSA-PSS private key.\n */\nexport type SignatureProfileInput =\n\t| {\n\t\t\t/** Infer the signature algorithm from the private key. */\n\t\t\treadonly kind?: 'auto';\n\t }\n\t| {\n\t\t\t/** Force RSA-PSS padding. */\n\t\t\treadonly kind: 'rsa-pss';\n\t\t\t/** Salt length in bytes. Must match the key's hash digest size. */\n\t\t\treadonly saltLength?: 32 | 48 | 64;\n\t };\n\n/** Resolved signature algorithm: the OID/parameters for DER encoding and the WebCrypto sign params. */\nexport interface SignatureAlgorithmIdentifier {\n\t/** ASN.1 OID of the signature algorithm (e.g. sha256WithRSAEncryption). */\n\treadonly algorithmOid: string;\n\t/** DER-encoded algorithm parameters, if the algorithm requires them (e.g. RSA-PSS). */\n\treadonly parameters?: Uint8Array;\n\t/** WebCrypto `sign()` algorithm parameter. */\n\treadonly signParams: Algorithm | EcdsaParams | RsaPssParams;\n\t/** When set, the raw ECDSA signature is this many bytes and must be DER-converted. */\n\treadonly ecdsaRawSignatureBytes?: number;\n}\n\n/** Resolve a private key and optional profile into a {@linkcode SignatureAlgorithmIdentifier}. */\nexport function getSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tprofile: SignatureProfileInput = {},\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (profile.kind === 'rsa-pss') {\n\t\treturn getRsaPssSignatureAlgorithm(privateKey, profile.saltLength);\n\t}\n\tif (algorithm.name === 'RSASSA-PKCS1-v1_5') {\n\t\tif (!hasHash(algorithm)) {\n\t\t\tthrow new Error('RSA key is missing hash metadata');\n\t\t}\n\t\tswitch (algorithm.hash.name) {\n\t\t\tcase 'SHA-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha256WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha384WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-512':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha512WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported RSA hash: ${algorithm.hash.name}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signing requires an explicit signature profile');\n\t}\n\n\tif (algorithm.name === 'ECDSA') {\n\t\tif (!hasNamedCurve(algorithm)) {\n\t\t\tthrow new Error('ECDSA key is missing namedCurve metadata');\n\t\t}\n\t\tswitch (algorithm.namedCurve) {\n\t\t\tcase 'P-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA256,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-256' },\n\t\t\t\t\tecdsaRawSignatureBytes: 64,\n\t\t\t\t};\n\t\t\tcase 'P-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA384,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-384' },\n\t\t\t\t\tecdsaRawSignatureBytes: 96,\n\t\t\t\t};\n\t\t\tcase 'P-521':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA512,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-512' },\n\t\t\t\t\tecdsaRawSignatureBytes: 132,\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported curve: ${algorithm.namedCurve}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'Ed25519') {\n\t\treturn {\n\t\t\talgorithmOid: OIDS.ed25519,\n\t\t\tsignParams: { name: 'Ed25519' },\n\t\t};\n\t}\n\n\tthrow new Error(`Unsupported signing key algorithm: ${algorithm.name}`);\n}\n\n/** Build an RSA-PSS {@linkcode SignatureAlgorithmIdentifier} from an RSA-PSS private key. */\nfunction getRsaPssSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tsaltLength: number | undefined,\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (algorithm.name !== 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signature profile requires an RSA-PSS private key');\n\t}\n\tif (!hasHash(algorithm)) {\n\t\tthrow new Error('RSA-PSS key is missing hash metadata');\n\t}\n\tconst hash = rsaPssHashFromWebCryptoName(algorithm.hash.name);\n\tconst parameters = rsaPssParametersForHash(hash);\n\tif (saltLength !== undefined && saltLength !== parameters.saltLength) {\n\t\tthrow new Error(\n\t\t\t`Unsupported RSA-PSS saltLength ${saltLength} for ${hash}; expected ${parameters.saltLength}`,\n\t\t);\n\t}\n\treturn {\n\t\talgorithmOid: OIDS.rsassaPss,\n\t\tparameters: encodeRsaPssParameters(parameters),\n\t\tsignParams: {\n\t\t\tname: 'RSA-PSS',\n\t\t\tsaltLength: parameters.saltLength,\n\t\t},\n\t};\n}\n\n/** DER-encode a {@linkcode SignatureAlgorithmIdentifier} as an ASN.1 `AlgorithmIdentifier` SEQUENCE. */\nexport function encodeAlgorithmIdentifier(input: SignatureAlgorithmIdentifier): Uint8Array {\n\tconst parts = [objectIdentifier(input.algorithmOid)];\n\tif (input.parameters !== undefined) {\n\t\tparts.push(input.parameters);\n\t}\n\treturn sequence(parts);\n}\n\n/** Sign `data` and return a DER-encoded signature. ECDSA raw signatures are auto-converted to DER. */\nexport async function signBytes(\n\tprivateKey: CryptoKey,\n\talgorithm: SignatureAlgorithmIdentifier,\n\tdata: Uint8Array,\n): Promise<Uint8Array> {\n\tconst view = new Uint8Array(data);\n\tconst signature = new Uint8Array(\n\t\tawait getCrypto().subtle.sign(algorithm.signParams, privateKey, view),\n\t);\n\tif (algorithm.ecdsaRawSignatureBytes !== undefined) {\n\t\treturn ecdsaSignatureToDer(signature, algorithm.ecdsaRawSignatureBytes);\n\t}\n\treturn signature;\n}\n\n/** Type guard: does this key algorithm carry a `hash` property (RSA keys). */\nfunction hasHash(algorithm: KeyAlgorithm): algorithm is RsaHashedKeyAlgorithm {\n\treturn 'hash' in algorithm;\n}\n\n/** Type guard: does this key algorithm carry a `namedCurve` property (EC keys). */\nfunction hasNamedCurve(algorithm: KeyAlgorithm): algorithm is EcKeyAlgorithm {\n\treturn 'namedCurve' in algorithm;\n}\n\n/** Narrow a WebCrypto hash name string to the supported {@linkcode RsaPssHash} union. */\nfunction rsaPssHashFromWebCryptoName(hash: string): RsaPssHash {\n\tswitch (hash) {\n\t\tcase 'SHA-256':\n\t\t\treturn 'SHA-256';\n\t\tcase 'SHA-384':\n\t\t\treturn 'SHA-384';\n\t\tcase 'SHA-512':\n\t\t\treturn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AAR;EACC,KAAK,WACJ,OAAO;EACR,KAAK,WACJ,OAAO;EACR,KAAK,WACJ,OAAO;EACR,SACC,MAAU,MAAM,yBAAyBA,GAAM;CACjD;AACD"}
1
+ {"version":3,"file":"signing.js","names":["nullValue","objectIdentifier","sequence","OIDS","ecdsaSignatureToDer","encodeRsaPssParameters","rsaPssParametersForHash","getCrypto","privateKey","profile","algorithm","saltLength","hash","parameters","input","parts","data","view","signature"],"sources":["../../../src/internal/crypto/signing.ts"],"sourcesContent":["/**\n * Signing helpers that map WebCrypto keys and signature profiles to on-wire\n * `AlgorithmIdentifier` values. Used by certificate and CSR builders.\n *\n * @module\n */\n\nimport { nullValue, objectIdentifier, sequence } from '#micro509/internal/asn1/der';\nimport { OIDS } from '#micro509/internal/asn1/oids';\nimport { ecdsaSignatureToDer } from '#micro509/internal/crypto/ecdsa';\nimport type { RsaPssHash } from '#micro509/internal/crypto/rsa-pss';\nimport { encodeRsaPssParameters, rsaPssParametersForHash } from '#micro509/internal/crypto/rsa-pss';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\n\n/**\n * Controls how the signature algorithm is chosen.\n *\n * `'auto'` (default) infers the algorithm from the key. `'rsa-pss'` forces RSA-PSS\n * padding and requires an RSA-PSS private key.\n */\nexport type SignatureProfileInput =\n\t| {\n\t\t\t/** Infer the signature algorithm from the private key. */\n\t\t\treadonly kind?: 'auto';\n\t }\n\t| {\n\t\t\t/** Force RSA-PSS padding. */\n\t\t\treadonly kind: 'rsa-pss';\n\t\t\t/** Salt length in bytes. Must match the key's hash digest size. */\n\t\t\treadonly saltLength?: 32 | 48 | 64;\n\t };\n\n/** Resolved signature algorithm: the OID/parameters for DER encoding and the WebCrypto sign params. */\nexport interface SignatureAlgorithmIdentifier {\n\t/** ASN.1 OID of the signature algorithm (e.g. sha256WithRSAEncryption). */\n\treadonly algorithmOid: string;\n\t/** DER-encoded algorithm parameters, if the algorithm requires them (e.g. RSA-PSS). */\n\treadonly parameters?: Uint8Array;\n\t/** WebCrypto `sign()` algorithm parameter. */\n\treadonly signParams: Algorithm | EcdsaParams | RsaPssParams;\n\t/** When set, the raw ECDSA signature is this many bytes and must be DER-converted. */\n\treadonly ecdsaRawSignatureBytes?: number;\n}\n\n/** Resolve a private key and optional profile into a {@linkcode SignatureAlgorithmIdentifier}. */\nexport function getSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tprofile: SignatureProfileInput = {},\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (profile.kind === 'rsa-pss') {\n\t\treturn getRsaPssSignatureAlgorithm(privateKey, profile.saltLength);\n\t}\n\tif (algorithm.name === 'RSASSA-PKCS1-v1_5') {\n\t\tif (!hasHash(algorithm)) {\n\t\t\tthrow new Error('RSA key is missing hash metadata');\n\t\t}\n\t\tswitch (algorithm.hash.name) {\n\t\t\tcase 'SHA-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha256WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha384WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-512':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha512WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported RSA hash: ${algorithm.hash.name}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signing requires an explicit signature profile');\n\t}\n\n\tif (algorithm.name === 'ECDSA') {\n\t\tif (!hasNamedCurve(algorithm)) {\n\t\t\tthrow new Error('ECDSA key is missing namedCurve metadata');\n\t\t}\n\t\tswitch (algorithm.namedCurve) {\n\t\t\tcase 'P-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA256,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-256' },\n\t\t\t\t\tecdsaRawSignatureBytes: 64,\n\t\t\t\t};\n\t\t\tcase 'P-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA384,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-384' },\n\t\t\t\t\tecdsaRawSignatureBytes: 96,\n\t\t\t\t};\n\t\t\tcase 'P-521':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA512,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-512' },\n\t\t\t\t\tecdsaRawSignatureBytes: 132,\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported curve: ${algorithm.namedCurve}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'Ed25519') {\n\t\treturn {\n\t\t\talgorithmOid: OIDS.ed25519,\n\t\t\tsignParams: { name: 'Ed25519' },\n\t\t};\n\t}\n\n\tthrow new Error(`Unsupported signing key algorithm: ${algorithm.name}`);\n}\n\n/** Build an RSA-PSS {@linkcode SignatureAlgorithmIdentifier} from an RSA-PSS private key. */\nfunction getRsaPssSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tsaltLength: number | undefined,\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (algorithm.name !== 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signature profile requires an RSA-PSS private key');\n\t}\n\tif (!hasHash(algorithm)) {\n\t\tthrow new Error('RSA-PSS key is missing hash metadata');\n\t}\n\tconst hash = rsaPssHashFromWebCryptoName(algorithm.hash.name);\n\tconst parameters = rsaPssParametersForHash(hash);\n\tif (saltLength !== undefined && saltLength !== parameters.saltLength) {\n\t\tthrow new Error(\n\t\t\t`Unsupported RSA-PSS saltLength ${saltLength} for ${hash}; expected ${parameters.saltLength}`,\n\t\t);\n\t}\n\treturn {\n\t\talgorithmOid: OIDS.rsassaPss,\n\t\tparameters: encodeRsaPssParameters(parameters),\n\t\tsignParams: {\n\t\t\tname: 'RSA-PSS',\n\t\t\tsaltLength: parameters.saltLength,\n\t\t},\n\t};\n}\n\n/** DER-encode a {@linkcode SignatureAlgorithmIdentifier} as an ASN.1 `AlgorithmIdentifier` SEQUENCE. */\nexport function encodeAlgorithmIdentifier(input: SignatureAlgorithmIdentifier): Uint8Array {\n\tconst parts = [objectIdentifier(input.algorithmOid)];\n\tif (input.parameters !== undefined) {\n\t\tparts.push(input.parameters);\n\t}\n\treturn sequence(parts);\n}\n\n/** Sign `data` and return a DER-encoded signature. ECDSA raw signatures are auto-converted to DER. */\nexport async function signBytes(\n\tprivateKey: CryptoKey,\n\talgorithm: SignatureAlgorithmIdentifier,\n\tdata: Uint8Array,\n): Promise<Uint8Array> {\n\tconst view = new Uint8Array(data);\n\tconst signature = new Uint8Array(\n\t\tawait getCrypto().subtle.sign(algorithm.signParams, privateKey, view),\n\t);\n\tif (algorithm.ecdsaRawSignatureBytes !== undefined) {\n\t\treturn ecdsaSignatureToDer(signature, algorithm.ecdsaRawSignatureBytes);\n\t}\n\treturn signature;\n}\n\n/** Type guard: does this key algorithm carry a `hash` property (RSA keys). */\nfunction hasHash(algorithm: KeyAlgorithm): algorithm is RsaHashedKeyAlgorithm {\n\treturn 'hash' in algorithm;\n}\n\n/** Type guard: does this key algorithm carry a `namedCurve` property (EC keys). */\nfunction hasNamedCurve(algorithm: KeyAlgorithm): algorithm is EcKeyAlgorithm {\n\treturn 'namedCurve' in algorithm;\n}\n\n/** Narrow a WebCrypto hash name string to the supported {@linkcode RsaPssHash} union. */\nfunction rsaPssHashFromWebCryptoName(hash: string): RsaPssHash {\n\tswitch (hash) {\n\t\tcase 'SHA-256':\n\t\t\treturn 'SHA-256';\n\t\tcase 'SHA-384':\n\t\t\treturn 'SHA-384';\n\t\tcase 'SHA-512':\n\t\t\treturn 'SHA-512';\n\t\tdefault:\n\t\t\tthrow new Error(`Unsupported RSA hash: ${hash}`);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;AA6CA,SAAgB,sBACfQ,GACAC,IAAiC,CAAC,GACH;CAC/B,IAAMC,IAAYF,EAAW;CAC7B,IAAIC,EAAQ,SAAS,WACpB,OAAO,4BAA4BD,GAAYC,EAAQ,UAAU;CAElE,IAAIC,EAAU,SAAS,qBAAqB;EAC3C,IAAI,CAAC,QAAQA,CAAS,GACrB,MAAU,MAAM,kCAAkC;EAEnD,QAAQA,EAAU,KAAK,MAAvB;GACC,KAAK,WACJ,OAAO;IACN,cAAcP,EAAK;IACnB,YAAYH,EAAU;IACtB,YAAY,EAAE,MAAM,oBAAoB;GACzC;GACD,KAAK,WACJ,OAAO;IACN,cAAcG,EAAK;IACnB,YAAYH,EAAU;IACtB,YAAY,EAAE,MAAM,oBAAoB;GACzC;GACD,KAAK,WACJ,OAAO;IACN,cAAcG,EAAK;IACnB,YAAYH,EAAU;IACtB,YAAY,EAAE,MAAM,oBAAoB;GACzC;GACD,SACC,MAAU,MAAM,yBAAyBU,EAAU,KAAK,MAAM;EAChE;CACD;CAEA,IAAIA,EAAU,SAAS,WACtB,MAAU,MAAM,wDAAwD;CAGzE,IAAIA,EAAU,SAAS,SAAS;EAC/B,IAAI,CAAC,cAAcA,CAAS,GAC3B,MAAU,MAAM,0CAA0C;EAE3D,QAAQA,EAAU,YAAlB;GACC,KAAK,SACJ,OAAO;IACN,cAAcP,EAAK;IACnB,YAAY;KAAE,MAAM;KAAS,MAAM;IAAU;IAC7C,wBAAwB;GACzB;GACD,KAAK,SACJ,OAAO;IACN,cAAcA,EAAK;IACnB,YAAY;KAAE,MAAM;KAAS,MAAM;IAAU;IAC7C,wBAAwB;GACz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QA,GAA6D;CAC7E,OAAO,UAAUA;AAClB;;AAGA,SAAS,cAAcA,GAAsD;CAC5E,OAAO,gBAAgBA;AACxB;;AAGA,SAAS,4BAA4BE,GAA0B;CAC9D,QAAQA,GAAR;EACC,KAAK,WACJ,OAAO;EACR,KAAK,WACJ,OAAO;EACR,KAAK,WACJ,OAAO;EACR,SACC,MAAU,MAAM,yBAAyBA,GAAM;CACjD;AACD"}
@@ -1 +1 @@
1
- {"version":3,"file":"base64url.js","names":["ALPHABET","DECODE_TABLE","table","index","out","bytes","chunk","remaining","value","written","buffer","bits","code","sextet"],"sources":["../../../src/internal/jose/base64url.ts"],"sourcesContent":["const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';\n\nconst DECODE_TABLE: Int8Array = (() => {\n\tconst table = new Int8Array(128).fill(-1);\n\tfor (let index = 0; index < ALPHABET.length; index++) {\n\t\ttable[ALPHABET.charCodeAt(index)] = index;\n\t}\n\treturn table;\n})();\n\n/** Encodes bytes as unpadded base64url (RFC 4648 §5, RFC 7515 §2). */\nexport function base64UrlEncode(bytes: Uint8Array): string {\n\tlet out = '';\n\tlet index = 0;\n\tfor (; index + 2 < bytes.length; index += 3) {\n\t\tconst chunk =\n\t\t\t(bytes[index] ?? 0) * 65536 + (bytes[index + 1] ?? 0) * 256 + (bytes[index + 2] ?? 0);\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 6) & 63] ?? '';\n\t\tout += ALPHABET[chunk & 63] ?? '';\n\t}\n\tconst remaining = bytes.length - index;\n\tif (remaining === 1) {\n\t\tconst chunk = (bytes[index] ?? 0) * 65536;\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t} else if (remaining === 2) {\n\t\tconst chunk = (bytes[index] ?? 0) * 65536 + (bytes[index + 1] ?? 0) * 256;\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 6) & 63] ?? '';\n\t}\n\treturn out;\n}\n\n/**\n * Decodes canonical unpadded base64url. Returns `undefined` for any character outside the URL-safe\n * alphabet, padding, whitespace, an impossible length, or non-zero trailing bits.\n */\nexport function base64UrlDecode(value: string): Uint8Array | undefined {\n\tconst remainder = value.length % 4;\n\tif (remainder === 1) return undefined;\n\tconst bytes = new Uint8Array(Math.floor((value.length * 3) / 4));\n\tlet written = 0;\n\tlet buffer = 0;\n\tlet bits = 0;\n\tfor (let index = 0; index < value.length; index++) {\n\t\tconst code = value.charCodeAt(index);\n\t\tconst sextet = code < 128 ? (DECODE_TABLE[code] ?? -1) : -1;\n\t\tif (sextet < 0) return undefined;\n\t\tbuffer = (buffer << 6) | sextet;\n\t\tbits += 6;\n\t\tif (bits >= 8) {\n\t\t\tbits -= 8;\n\t\t\tbytes[written++] = (buffer >> bits) & 0xff;\n\t\t\tbuffer &= (1 << bits) - 1;\n\t\t}\n\t}\n\tif (buffer !== 0) return undefined;\n\treturn bytes;\n}\n"],"mappings":";AAAA,MAAMA,IAAW,oEAEXC,WAAiC;CACtC,IAAMC,qBAAQ,IAAI,UAAU,GAAG,EAAA,CAAE,KAAK,EAAE;CACxC,KAAK,IAAIC,IAAQ,GAAGA,IAAQ,IAAiB,KAC5C,EAAMH,EAAS,WAAWG,CAAK,KAAKA;CAErC,OAAOD;AACR,EAAA,CAAG;;AAGH,SAAgB,gBAAgB,GAA2B;CAC1D,IAAIE,IAAM,IACND,IAAQ;CACZ,OAAOA,IAAQ,IAAIE,EAAM,QAAQ,KAAS,GAAG;EAC5C,IAAMC,KACJD,EAAMF,MAAU,KAAK,SAASE,EAAMF,IAAQ,MAAM,KAAK,OAAOE,EAAMF,IAAQ,MAAM;EAIpF,AAHA,KAAOH,EAAUM,KAAS,KAAM,OAAO,IACvC,KAAON,EAAUM,KAAS,KAAM,OAAO,IACvC,KAAON,EAAUM,KAAS,IAAK,OAAO,IACtC,KAAON,EAASM,IAAQ,OAAO;CAChC;CACA,IAAMC,IAAYF,EAAM,SAASF;CACjC,IAAII,MAAc,GAAG;EACpB,IAAMD,KAASD,EAAMF,MAAU,KAAK;EAEpC,AADA,KAAOH,EAAUM,KAAS,KAAM,OAAO,IACvC,KAAON,EAAUM,KAAS,KAAM,OAAO;CACxC,OAAO,IAAIC,MAAc,GAAG;EAC3B,IAAMD,KAASD,EAAMF,MAAU,KAAK,SAASE,EAAMF,IAAQ,MAAM,KAAK;EAGtE,AAFA,KAAOH,EAAUM,KAAS,KAAM,OAAO,IACvC,KAAON,EAAUM,KAAS,KAAM,OAAO,IACvC,KAAON,EAAUM,KAAS,IAAK,OAAO;CACvC;CACA,OAAOF;AACR;;;;;AAMA,SAAgB,gBAAgB,GAAuC;CAEtE,IADkBI,EAAM,SAAS,KACf,GAAG;CACrB,IAAMH,IAAQ,IAAI,WAAW,KAAK,MAAOG,EAAM,SAAS,IAAK,CAAC,CAAC,GAC3DC,IAAU,GACVC,IAAS,GACTC,IAAO;CACX,KAAK,IAAIR,IAAQ,GAAGA,IAAQK,EAAM,QAAQ,KAAS;EAClD,IAAMI,IAAOJ,EAAM,WAAWL,CAAK,GAC7BU,IAASD,IAAO,MAAOX,EAAaW,MAAS,KAAM;EACzD,IAAIC,IAAS,GAAG;EAGhB,AAFA,IAAUH,KAAU,IAAKG,GACzB,KAAQ,GACJF,KAAQ,MACX,KAAQ,GACR,EAAM,OAAcD,KAAUC,IAAQ,KACtC,MAAW,KAAKA,KAAQ;CAE1B;CACA,IAAID,MAAW,GACf,OAAOL;AACR"}
1
+ {"version":3,"file":"base64url.js","names":["ALPHABET","DECODE_TABLE","table","index","bytes","out","chunk","remaining","value","written","buffer","bits","code","sextet"],"sources":["../../../src/internal/jose/base64url.ts"],"sourcesContent":["const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';\n\nconst DECODE_TABLE: Int8Array = (() => {\n\tconst table = new Int8Array(128).fill(-1);\n\tfor (let index = 0; index < ALPHABET.length; index++) {\n\t\ttable[ALPHABET.charCodeAt(index)] = index;\n\t}\n\treturn table;\n})();\n\n/** Encodes bytes as unpadded base64url (RFC 4648 §5, RFC 7515 §2). */\nexport function base64UrlEncode(bytes: Uint8Array): string {\n\tlet out = '';\n\tlet index = 0;\n\tfor (; index + 2 < bytes.length; index += 3) {\n\t\tconst chunk =\n\t\t\t(bytes[index] ?? 0) * 65536 + (bytes[index + 1] ?? 0) * 256 + (bytes[index + 2] ?? 0);\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 6) & 63] ?? '';\n\t\tout += ALPHABET[chunk & 63] ?? '';\n\t}\n\tconst remaining = bytes.length - index;\n\tif (remaining === 1) {\n\t\tconst chunk = (bytes[index] ?? 0) * 65536;\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t} else if (remaining === 2) {\n\t\tconst chunk = (bytes[index] ?? 0) * 65536 + (bytes[index + 1] ?? 0) * 256;\n\t\tout += ALPHABET[(chunk >> 18) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 12) & 63] ?? '';\n\t\tout += ALPHABET[(chunk >> 6) & 63] ?? '';\n\t}\n\treturn out;\n}\n\n/**\n * Decodes canonical unpadded base64url. Returns `undefined` for any character outside the URL-safe\n * alphabet, padding, whitespace, an impossible length, or non-zero trailing bits.\n */\nexport function base64UrlDecode(value: string): Uint8Array | undefined {\n\tconst remainder = value.length % 4;\n\tif (remainder === 1) return undefined;\n\tconst bytes = new Uint8Array(Math.floor((value.length * 3) / 4));\n\tlet written = 0;\n\tlet buffer = 0;\n\tlet bits = 0;\n\tfor (let index = 0; index < value.length; index++) {\n\t\tconst code = value.charCodeAt(index);\n\t\tconst sextet = code < 128 ? (DECODE_TABLE[code] ?? -1) : -1;\n\t\tif (sextet < 0) return undefined;\n\t\tbuffer = (buffer << 6) | sextet;\n\t\tbits += 6;\n\t\tif (bits >= 8) {\n\t\t\tbits -= 8;\n\t\t\tbytes[written++] = (buffer >> bits) & 0xff;\n\t\t\tbuffer &= (1 << bits) - 1;\n\t\t}\n\t}\n\tif (buffer !== 0) return undefined;\n\treturn bytes;\n}\n"],"mappings":";AAAA,MAAMA,IAAW,oEAEXC,WAAiC;CACtC,IAAMC,qBAAQ,IAAI,UAAU,GAAG,EAAA,CAAE,KAAK,EAAE;CACxC,KAAK,IAAIC,IAAQ,GAAGA,IAAQ,IAAiBA,KAC5CD,EAAMF,EAAS,WAAWG,CAAK,KAAKA;CAErC,OAAOD;AACR,EAAA,CAAG;;AAGH,SAAgB,gBAAgBE,GAA2B;CAC1D,IAAIC,IAAM,IACNF,IAAQ;CACZ,OAAOA,IAAQ,IAAIC,EAAM,QAAQD,KAAS,GAAG;EAC5C,IAAMG,KACJF,EAAMD,MAAU,KAAK,SAASC,EAAMD,IAAQ,MAAM,KAAK,OAAOC,EAAMD,IAAQ,MAAM;EACpFE,KAAOL,EAAUM,KAAS,KAAM,OAAO,IACvCD,KAAOL,EAAUM,KAAS,KAAM,OAAO,IACvCD,KAAOL,EAAUM,KAAS,IAAK,OAAO,IACtCD,KAAOL,EAASM,IAAQ,OAAO;CAChC;CACA,IAAMC,IAAYH,EAAM,SAASD;CACjC,IAAII,MAAc,GAAG;EACpB,IAAMD,KAASF,EAAMD,MAAU,KAAK;EACpCE,KAAOL,EAAUM,KAAS,KAAM,OAAO,IACvCD,KAAOL,EAAUM,KAAS,KAAM,OAAO;CACxC,OAAO,IAAIC,MAAc,GAAG;EAC3B,IAAMD,KAASF,EAAMD,MAAU,KAAK,SAASC,EAAMD,IAAQ,MAAM,KAAK;EACtEE,KAAOL,EAAUM,KAAS,KAAM,OA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