@wildo-ai/platform-config-lib 1.1.10 → 1.1.11

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 (231) hide show
  1. package/dist/esm/_unimplemented/unimplemented-field-warning.d.ts +1 -1
  2. package/dist/esm/_unimplemented/unimplemented-field-warning.js +1 -1
  3. package/dist/esm/_unimplemented/unimplemented-field-warning.js.map +1 -1
  4. package/dist/esm/blueprint/anchored-region.d.ts +83 -0
  5. package/dist/esm/blueprint/anchored-region.d.ts.map +1 -0
  6. package/dist/esm/blueprint/anchored-region.js +263 -0
  7. package/dist/esm/blueprint/anchored-region.js.map +1 -0
  8. package/dist/esm/blueprint/blueprint.schemas.d.ts +689 -0
  9. package/dist/esm/blueprint/blueprint.schemas.d.ts.map +1 -0
  10. package/dist/esm/blueprint/blueprint.schemas.js +638 -0
  11. package/dist/esm/blueprint/blueprint.schemas.js.map +1 -0
  12. package/dist/esm/bootstrap/application-backend-env.contracts.d.ts.map +1 -1
  13. package/dist/esm/bootstrap/bootstrap-planner.d.ts.map +1 -1
  14. package/dist/esm/bootstrap/bootstrap.contracts.d.ts +13 -1
  15. package/dist/esm/bootstrap/bootstrap.contracts.d.ts.map +1 -1
  16. package/dist/esm/bootstrap/bootstrap.contracts.js +6 -0
  17. package/dist/esm/bootstrap/bootstrap.contracts.js.map +1 -1
  18. package/dist/esm/bootstrap/bootstrap.schemas.d.ts +21 -3
  19. package/dist/esm/bootstrap/bootstrap.schemas.d.ts.map +1 -1
  20. package/dist/esm/bootstrap/bootstrap.schemas.js +14 -3
  21. package/dist/esm/bootstrap/bootstrap.schemas.js.map +1 -1
  22. package/dist/esm/bootstrap/platform-runtime-env.contracts.d.ts +121 -0
  23. package/dist/esm/bootstrap/platform-runtime-env.contracts.d.ts.map +1 -1
  24. package/dist/esm/bootstrap/platform-runtime-env.contracts.js +129 -0
  25. package/dist/esm/bootstrap/platform-runtime-env.contracts.js.map +1 -1
  26. package/dist/esm/bootstrap/platform-store-preconnect.d.ts.map +1 -1
  27. package/dist/esm/bootstrap/runtime-backing-service-access.d.ts.map +1 -1
  28. package/dist/esm/bootstrap/runtime-network-policy.d.ts.map +1 -1
  29. package/dist/esm/bootstrap/runtime-network-topology.d.ts.map +1 -1
  30. package/dist/esm/bootstrap/runtime-platform-access.d.ts.map +1 -1
  31. package/dist/esm/bootstrap/runtime-principal.contracts.d.ts.map +1 -1
  32. package/dist/esm/bootstrap/runtime-response-integrity.d.ts +249 -0
  33. package/dist/esm/bootstrap/runtime-response-integrity.d.ts.map +1 -0
  34. package/dist/esm/bootstrap/runtime-response-integrity.js +353 -0
  35. package/dist/esm/bootstrap/runtime-response-integrity.js.map +1 -0
  36. package/dist/esm/bootstrap/runtime-secret-env.contracts.d.ts +32 -0
  37. package/dist/esm/bootstrap/runtime-secret-env.contracts.d.ts.map +1 -0
  38. package/dist/esm/bootstrap/runtime-secret-env.contracts.js +43 -0
  39. package/dist/esm/bootstrap/runtime-secret-env.contracts.js.map +1 -0
  40. package/dist/esm/bootstrap/runtime-topology.contracts.d.ts.map +1 -1
  41. package/dist/esm/bootstrap/wildo-owner-organization.contracts.d.ts.map +1 -1
  42. package/dist/esm/canonical-json/rfc8785-canonical-json.d.ts +51 -0
  43. package/dist/esm/canonical-json/rfc8785-canonical-json.d.ts.map +1 -0
  44. package/dist/esm/canonical-json/rfc8785-canonical-json.js +231 -0
  45. package/dist/esm/canonical-json/rfc8785-canonical-json.js.map +1 -0
  46. package/dist/esm/config-authoring.entry.d.ts.map +1 -1
  47. package/dist/esm/index.d.ts +15 -0
  48. package/dist/esm/index.d.ts.map +1 -1
  49. package/dist/esm/index.js +15 -0
  50. package/dist/esm/index.js.map +1 -1
  51. package/dist/esm/key-management/approval-seal-verifier.d.ts +216 -0
  52. package/dist/esm/key-management/approval-seal-verifier.d.ts.map +1 -0
  53. package/dist/esm/key-management/approval-seal-verifier.js +233 -0
  54. package/dist/esm/key-management/approval-seal-verifier.js.map +1 -0
  55. package/dist/esm/key-management/approval-seal.contracts.d.ts +131 -0
  56. package/dist/esm/key-management/approval-seal.contracts.d.ts.map +1 -0
  57. package/dist/esm/key-management/approval-seal.contracts.js +115 -0
  58. package/dist/esm/key-management/approval-seal.contracts.js.map +1 -0
  59. package/dist/esm/key-management/data-key.contracts.d.ts +108 -0
  60. package/dist/esm/key-management/data-key.contracts.d.ts.map +1 -0
  61. package/dist/esm/key-management/data-key.contracts.js +54 -0
  62. package/dist/esm/key-management/data-key.contracts.js.map +1 -0
  63. package/dist/esm/key-management/es256-jws.d.ts +60 -0
  64. package/dist/esm/key-management/es256-jws.d.ts.map +1 -0
  65. package/dist/esm/key-management/es256-jws.js +135 -0
  66. package/dist/esm/key-management/es256-jws.js.map +1 -0
  67. package/dist/esm/key-management/key-provider.spi.d.ts +151 -0
  68. package/dist/esm/key-management/key-provider.spi.d.ts.map +1 -0
  69. package/dist/esm/key-management/key-provider.spi.js +38 -0
  70. package/dist/esm/key-management/key-provider.spi.js.map +1 -0
  71. package/dist/esm/key-management/keyring-rotation.contracts.d.ts +93 -0
  72. package/dist/esm/key-management/keyring-rotation.contracts.d.ts.map +1 -0
  73. package/dist/esm/key-management/keyring-rotation.contracts.js +101 -0
  74. package/dist/esm/key-management/keyring-rotation.contracts.js.map +1 -0
  75. package/dist/esm/key-management/keyring-status.contracts.d.ts +66 -0
  76. package/dist/esm/key-management/keyring-status.contracts.d.ts.map +1 -0
  77. package/dist/esm/key-management/keyring-status.contracts.js +45 -0
  78. package/dist/esm/key-management/keyring-status.contracts.js.map +1 -0
  79. package/dist/esm/key-management/signature-request.contracts.d.ts +95 -0
  80. package/dist/esm/key-management/signature-request.contracts.d.ts.map +1 -0
  81. package/dist/esm/key-management/signature-request.contracts.js +96 -0
  82. package/dist/esm/key-management/signature-request.contracts.js.map +1 -0
  83. package/dist/esm/key-management/signing-log-export.contracts.d.ts +216 -0
  84. package/dist/esm/key-management/signing-log-export.contracts.d.ts.map +1 -0
  85. package/dist/esm/key-management/signing-log-export.contracts.js +268 -0
  86. package/dist/esm/key-management/signing-log-export.contracts.js.map +1 -0
  87. package/dist/esm/key-management/vault-custody.contracts.d.ts +55 -0
  88. package/dist/esm/key-management/vault-custody.contracts.d.ts.map +1 -0
  89. package/dist/esm/key-management/vault-custody.contracts.js +88 -0
  90. package/dist/esm/key-management/vault-custody.contracts.js.map +1 -0
  91. package/dist/esm/lockfile/file-lock-hold.d.ts.map +1 -1
  92. package/dist/esm/lockfile/lockfile-io.d.ts +7 -9
  93. package/dist/esm/lockfile/lockfile-io.d.ts.map +1 -1
  94. package/dist/esm/lockfile/lockfile-io.js +23 -22
  95. package/dist/esm/lockfile/lockfile-io.js.map +1 -1
  96. package/dist/esm/lockfile/materialize-infra-section.d.ts +17 -14
  97. package/dist/esm/lockfile/materialize-infra-section.d.ts.map +1 -1
  98. package/dist/esm/lockfile/materialize-infra-section.js +9 -9
  99. package/dist/esm/lockfile/materialize-infra-section.js.map +1 -1
  100. package/dist/esm/lockfile/wildo-saas-lock.shared.schemas.d.ts +26 -2
  101. package/dist/esm/lockfile/wildo-saas-lock.shared.schemas.d.ts.map +1 -1
  102. package/dist/esm/lockfile/wildo-saas-lock.shared.schemas.js +27 -2
  103. package/dist/esm/lockfile/wildo-saas-lock.shared.schemas.js.map +1 -1
  104. package/dist/esm/project-config/application-public-pricing-catalog.loader.d.ts.map +1 -1
  105. package/dist/esm/project-config/application-public-pricing-catalog.loader.js +9 -2
  106. package/dist/esm/project-config/application-public-pricing-catalog.loader.js.map +1 -1
  107. package/dist/esm/project-config/application-served-public-documents.loader.d.ts +26 -0
  108. package/dist/esm/project-config/application-served-public-documents.loader.d.ts.map +1 -0
  109. package/dist/esm/project-config/application-served-public-documents.loader.js +78 -0
  110. package/dist/esm/project-config/application-served-public-documents.loader.js.map +1 -0
  111. package/dist/esm/project-config/define-framework-config.d.ts.map +1 -1
  112. package/dist/esm/project-config/define-infra-env-config.d.ts +236 -15
  113. package/dist/esm/project-config/define-infra-env-config.d.ts.map +1 -1
  114. package/dist/esm/project-config/define-infra-env-config.js +104 -91
  115. package/dist/esm/project-config/define-infra-env-config.js.map +1 -1
  116. package/dist/esm/project-config/define-minion-config.d.ts.map +1 -1
  117. package/dist/esm/project-config/define-saas-config.d.ts +133 -3
  118. package/dist/esm/project-config/define-saas-config.d.ts.map +1 -1
  119. package/dist/esm/project-config/define-saas-config.js +170 -19
  120. package/dist/esm/project-config/define-saas-config.js.map +1 -1
  121. package/dist/esm/project-config/define-worker-config.d.ts.map +1 -1
  122. package/dist/esm/project-config/hosted-infra-env-config.d.ts.map +1 -1
  123. package/dist/esm/project-config/hosted-infra-env-config.js +3 -0
  124. package/dist/esm/project-config/hosted-infra-env-config.js.map +1 -1
  125. package/dist/esm/project-config/index.d.ts +3 -0
  126. package/dist/esm/project-config/index.d.ts.map +1 -1
  127. package/dist/esm/project-config/index.js +3 -0
  128. package/dist/esm/project-config/index.js.map +1 -1
  129. package/dist/esm/project-config/loader.d.ts.map +1 -1
  130. package/dist/esm/project-config/npm-version-range.d.ts.map +1 -1
  131. package/dist/esm/project-config/npm-version-range.js +39 -16
  132. package/dist/esm/project-config/npm-version-range.js.map +1 -1
  133. package/dist/esm/project-config/owning-package-tsconfig.d.ts +15 -0
  134. package/dist/esm/project-config/owning-package-tsconfig.d.ts.map +1 -0
  135. package/dist/esm/project-config/owning-package-tsconfig.js +29 -0
  136. package/dist/esm/project-config/owning-package-tsconfig.js.map +1 -0
  137. package/dist/esm/project-config/provider-catalog-types.d.ts.map +1 -1
  138. package/dist/esm/project-config/provider-companion-discovery.d.ts +4 -4
  139. package/dist/esm/project-config/provider-companion-discovery.d.ts.map +1 -1
  140. package/dist/esm/project-config/provider-companion-discovery.js +13 -9
  141. package/dist/esm/project-config/provider-companion-discovery.js.map +1 -1
  142. package/dist/esm/project-config/provider-runtime-artifacts.d.ts +32 -3
  143. package/dist/esm/project-config/provider-runtime-artifacts.d.ts.map +1 -1
  144. package/dist/esm/project-config/provider-runtime-artifacts.js +67 -13
  145. package/dist/esm/project-config/provider-runtime-artifacts.js.map +1 -1
  146. package/dist/esm/project-config/shared/application-creation-verification.schemas.d.ts.map +1 -1
  147. package/dist/esm/project-config/shared/application-modules.schemas.d.ts.map +1 -1
  148. package/dist/esm/project-config/shared/application-modules.utils.d.ts.map +1 -1
  149. package/dist/esm/project-config/shared/auth.schemas.d.ts.map +1 -1
  150. package/dist/esm/project-config/shared/backing-service-servability.d.ts.map +1 -1
  151. package/dist/esm/project-config/shared/backing-services.schemas.d.ts.map +1 -1
  152. package/dist/esm/project-config/shared/cdn.example.d.ts.map +1 -1
  153. package/dist/esm/project-config/shared/cdn.schemas.d.ts.map +1 -1
  154. package/dist/esm/project-config/shared/csp-defaults.d.ts +4 -5
  155. package/dist/esm/project-config/shared/csp-defaults.d.ts.map +1 -1
  156. package/dist/esm/project-config/shared/csp-defaults.js +4 -5
  157. package/dist/esm/project-config/shared/csp-defaults.js.map +1 -1
  158. package/dist/esm/project-config/shared/csp-emit.d.ts.map +1 -1
  159. package/dist/esm/project-config/shared/csp.schemas.d.ts +1 -2
  160. package/dist/esm/project-config/shared/csp.schemas.d.ts.map +1 -1
  161. package/dist/esm/project-config/shared/csp.schemas.js +1 -2
  162. package/dist/esm/project-config/shared/csp.schemas.js.map +1 -1
  163. package/dist/esm/project-config/shared/data-services.schemas.d.ts.map +1 -1
  164. package/dist/esm/project-config/shared/environment.schemas.d.ts +6 -0
  165. package/dist/esm/project-config/shared/environment.schemas.d.ts.map +1 -1
  166. package/dist/esm/project-config/shared/environment.schemas.js +8 -1
  167. package/dist/esm/project-config/shared/environment.schemas.js.map +1 -1
  168. package/dist/esm/project-config/shared/infra-providers.schemas.d.ts.map +1 -1
  169. package/dist/esm/project-config/shared/infra-service.schemas.d.ts.map +1 -1
  170. package/dist/esm/project-config/shared/infrastructure-defaults.schemas.d.ts +1 -0
  171. package/dist/esm/project-config/shared/infrastructure-defaults.schemas.d.ts.map +1 -1
  172. package/dist/esm/project-config/shared/infrastructure-defaults.schemas.js +3 -0
  173. package/dist/esm/project-config/shared/infrastructure-defaults.schemas.js.map +1 -1
  174. package/dist/esm/project-config/shared/minion-resource-access.validation.d.ts +4 -0
  175. package/dist/esm/project-config/shared/minion-resource-access.validation.d.ts.map +1 -1
  176. package/dist/esm/project-config/shared/minion-resource-access.validation.js +6 -2
  177. package/dist/esm/project-config/shared/minion-resource-access.validation.js.map +1 -1
  178. package/dist/esm/project-config/shared/platform-key-management.schemas.d.ts +250 -0
  179. package/dist/esm/project-config/shared/platform-key-management.schemas.d.ts.map +1 -0
  180. package/dist/esm/project-config/shared/platform-key-management.schemas.js +194 -0
  181. package/dist/esm/project-config/shared/platform-key-management.schemas.js.map +1 -0
  182. package/dist/esm/project-config/shared/platform-services.schemas.d.ts.map +1 -1
  183. package/dist/esm/project-config/shared/platform-storage.schemas.d.ts.map +1 -1
  184. package/dist/esm/project-config/shared/schema-version.schemas.d.ts.map +1 -1
  185. package/dist/esm/project-config/shared/service-definitions.schemas.d.ts.map +1 -1
  186. package/dist/esm/project-config/shared/systems-of-record-env.schemas.d.ts.map +1 -1
  187. package/dist/esm/registry/artifact-domain.schemas.d.ts.map +1 -1
  188. package/dist/esm/registry/digest.schemas.d.ts.map +1 -1
  189. package/dist/esm/registry/index.d.ts +1 -0
  190. package/dist/esm/registry/index.d.ts.map +1 -1
  191. package/dist/esm/registry/index.js +1 -0
  192. package/dist/esm/registry/index.js.map +1 -1
  193. package/dist/esm/registry/local-checkout-source.d.ts +22 -2
  194. package/dist/esm/registry/local-checkout-source.d.ts.map +1 -1
  195. package/dist/esm/registry/local-checkout-source.js +66 -3
  196. package/dist/esm/registry/local-checkout-source.js.map +1 -1
  197. package/dist/esm/registry/lockfile-modules-section.schemas.d.ts +10 -0
  198. package/dist/esm/registry/lockfile-modules-section.schemas.d.ts.map +1 -1
  199. package/dist/esm/registry/module-artifact.schemas.d.ts +23 -0
  200. package/dist/esm/registry/module-artifact.schemas.d.ts.map +1 -1
  201. package/dist/esm/registry/module-manifest.schemas.d.ts +336 -18
  202. package/dist/esm/registry/module-manifest.schemas.d.ts.map +1 -1
  203. package/dist/esm/registry/module-manifest.schemas.js +245 -19
  204. package/dist/esm/registry/module-manifest.schemas.js.map +1 -1
  205. package/dist/esm/registry/module-search.schemas.d.ts +2 -0
  206. package/dist/esm/registry/module-search.schemas.d.ts.map +1 -1
  207. package/dist/esm/registry/module-search.schemas.js +14 -1
  208. package/dist/esm/registry/module-search.schemas.js.map +1 -1
  209. package/dist/esm/registry/registry-config.schemas.d.ts.map +1 -1
  210. package/dist/esm/registry/registry-errors.d.ts +22 -2
  211. package/dist/esm/registry/registry-errors.d.ts.map +1 -1
  212. package/dist/esm/registry/registry-errors.js +28 -2
  213. package/dist/esm/registry/registry-errors.js.map +1 -1
  214. package/dist/esm/registry/registry-keys.schemas.d.ts.map +1 -1
  215. package/dist/esm/registry/registry-section-schemas.constants.d.ts +5 -0
  216. package/dist/esm/registry/registry-section-schemas.constants.d.ts.map +1 -1
  217. package/dist/esm/registry/registry-url-resolution.d.ts +72 -0
  218. package/dist/esm/registry/registry-url-resolution.d.ts.map +1 -0
  219. package/dist/esm/registry/registry-url-resolution.js +157 -0
  220. package/dist/esm/registry/registry-url-resolution.js.map +1 -0
  221. package/dist/esm/schemas/framework-info.schemas.d.ts.map +1 -1
  222. package/dist/esm/schemas/framework-secrets.schemas.d.ts +55 -0
  223. package/dist/esm/schemas/framework-secrets.schemas.d.ts.map +1 -1
  224. package/dist/esm/schemas/framework-secrets.schemas.js +53 -0
  225. package/dist/esm/schemas/framework-secrets.schemas.js.map +1 -1
  226. package/dist/esm/schemas/platform-application-config.schemas.d.ts.map +1 -1
  227. package/dist/esm/schemas/platform-application-secrets.schemas.d.ts.map +1 -1
  228. package/dist/esm/schemas/platform-config.schemas.d.ts.map +1 -1
  229. package/dist/esm/workspace-scope/application-subject-manifest.d.ts.map +1 -1
  230. package/dist/esm/workspace-scope/index.d.ts.map +1 -1
  231. package/package.json +7 -7
@@ -0,0 +1,108 @@
1
+ import { z } from 'zod';
2
+ import { CryptoKey_Purpose, CryptoKey_Scope, type CryptoKey_VersionState } from '@wildo-ai/saas-models';
3
+ /**
4
+ * # Data keys: what the application and the custodian exchange (#2894)
5
+ *
6
+ * The application seals its stored secrets under DATA keys, and a data key is only ever stored WRAPPED by its purpose's
7
+ * wrapping key, which never leaves the custodian (apps-manager). Five requests, all on the signed apps-to-manager channel:
8
+ *
9
+ * - **issue** a new data key: the custodian mints it, wraps it under the ACTIVE wrapping version, and answers with both
10
+ * halves (the application stores the wrapped one and holds the plaintext in memory only);
11
+ * - **unwrap** a stored data key, at boot or when a process first needs it;
12
+ * - **rewrap** a data key under the ACTIVE version after a rotation, without its plaintext leaving the custodian;
13
+ * - **release** a retired wrapping version once no stored data key is wrapped under it, so it can be destroyed;
14
+ * - **read the wrapping versions** and their states, from which the application derives, at every refresh, what it
15
+ * re-wraps, which data key stops sealing (one a COMPROMISED version wrapped), and which version it releases.
16
+ *
17
+ * The custodian builds the associated data of every wrap itself ({@link dataKeyAssociatedDataOf}): the purpose, the
18
+ * scope, the scope's id and the data key's id, so a wrapped key copied to another data key, organization or application
19
+ * does not unwrap. Both sides import this module.
20
+ */
21
+ /** The associated data a data key is wrapped under. */
22
+ export declare function dataKeyAssociatedDataOf(input: {
23
+ readonly purpose: CryptoKey_Purpose;
24
+ readonly scope: CryptoKey_Scope;
25
+ readonly scopeId: string;
26
+ readonly dataKeyId: string;
27
+ }): Buffer;
28
+ export declare const DataKeyIssueRequestSchema: z.ZodObject<{
29
+ purpose: z.ZodEnum<typeof CryptoKey_Purpose>;
30
+ /** The organization whose key serves the request, when the binding is organization-scoped; ignored otherwise. */
31
+ organizationId: z.ZodOptional<z.ZodString>;
32
+ }, z.core.$strict>;
33
+ export type DataKeyIssueRequest = z.infer<typeof DataKeyIssueRequestSchema>;
34
+ export declare const DataKeyUnwrapRequestSchema: z.ZodObject<{
35
+ purpose: z.ZodEnum<typeof CryptoKey_Purpose>;
36
+ organizationId: z.ZodOptional<z.ZodString>;
37
+ kid: z.ZodString;
38
+ dataKeyId: z.ZodString;
39
+ wrapped: z.ZodString;
40
+ }, z.core.$strict>;
41
+ export type DataKeyUnwrapRequest = z.infer<typeof DataKeyUnwrapRequestSchema>;
42
+ export declare const DataKeyRewrapRequestSchema: z.ZodObject<{
43
+ purpose: z.ZodEnum<typeof CryptoKey_Purpose>;
44
+ organizationId: z.ZodOptional<z.ZodString>;
45
+ kid: z.ZodString;
46
+ dataKeyId: z.ZodString;
47
+ wrapped: z.ZodString;
48
+ }, z.core.$strict>;
49
+ export type DataKeyRewrapRequest = z.infer<typeof DataKeyRewrapRequestSchema>;
50
+ /**
51
+ * The header by which a rewrap declares that the application recorded the data key's exposure before asking (#3029),
52
+ * with the value {@link DATA_KEY_EXPOSURE_RECORDED}. The custodian refuses a rewrap off a COMPROMISED version without it
53
+ * (`exposure_unrecorded`): once the row is re-wrapped it no longer names the compromised version, and only the mark tells
54
+ * the application that the values sealed under that data key must be sealed again. A runtime from before #3029 records
55
+ * no mark and sends no header, so it is refused and the row waits for a current runtime to mark and move it.
56
+ *
57
+ * A header rather than a body field, decided: the request body is a strict object, so a custodian from before #3029
58
+ * would refuse an unknown field as `request_invalid`, which an application reads as "this runtime is refused" and stops
59
+ * sealing on. A custodian from before #3029 ignores the header, and the application marks its rows either way.
60
+ */
61
+ export declare const DATA_KEY_EXPOSURE_RECORDED_HEADER = "x-wildo-data-key-exposure-recorded";
62
+ export declare const DATA_KEY_EXPOSURE_RECORDED = "recorded";
63
+ export declare const DataKeyReleaseRequestSchema: z.ZodObject<{
64
+ purpose: z.ZodEnum<typeof CryptoKey_Purpose>;
65
+ organizationId: z.ZodOptional<z.ZodString>;
66
+ kid: z.ZodString;
67
+ }, z.core.$strict>;
68
+ export type DataKeyReleaseRequest = z.infer<typeof DataKeyReleaseRequestSchema>;
69
+ export declare const DataKeyWrappingVersionsRequestSchema: z.ZodObject<{
70
+ purpose: z.ZodEnum<typeof CryptoKey_Purpose>;
71
+ /** The organization whose key serves the request, when the binding is organization-scoped; ignored otherwise. */
72
+ organizationId: z.ZodOptional<z.ZodString>;
73
+ }, z.core.$strict>;
74
+ export type DataKeyWrappingVersionsRequest = z.infer<typeof DataKeyWrappingVersionsRequestSchema>;
75
+ /** Where a data key belongs: the keyring scope the custodian resolved for the request. */
76
+ export interface DataKeyScope {
77
+ readonly scope: CryptoKey_Scope;
78
+ readonly scopeId: string;
79
+ }
80
+ /** A new data key: its id, its wrapping version, both halves (base64url), and the scope it was wrapped for. */
81
+ export interface DataKeyIssueResponse extends DataKeyScope {
82
+ readonly dataKeyId: string;
83
+ readonly kid: string;
84
+ readonly dataKey: string;
85
+ readonly wrapped: string;
86
+ }
87
+ export interface DataKeyUnwrapResponse {
88
+ readonly dataKey: string;
89
+ }
90
+ /** One wrapping version of the keyring, as the application needs it: its `kid`, its state, and whether it was released. */
91
+ export interface DataKeyWrappingVersion {
92
+ readonly kid: string;
93
+ readonly state: CryptoKey_VersionState;
94
+ readonly released: boolean;
95
+ }
96
+ /** Every wrapping version of the keyring that serves the request (none before its first data key). */
97
+ export interface DataKeyWrappingVersionsResponse {
98
+ readonly versions: readonly DataKeyWrappingVersion[];
99
+ }
100
+ export interface DataKeyRewrapResponse {
101
+ readonly kid: string;
102
+ readonly wrapped: string;
103
+ }
104
+ export interface DataKeyReleaseResponse {
105
+ readonly kid: string;
106
+ readonly releasedAt: string;
107
+ }
108
+ //# sourceMappingURL=data-key.contracts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-key.contracts.d.ts","sourceRoot":"","sources":["../../../../src/key-management/data-key.contracts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAExG;;;;;;;;;;;;;;;;;GAiBG;AAEH,uDAAuD;AACvD,wBAAgB,uBA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C;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,2HAA2H;AAC3H,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,sBAAsB,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAED,sGAAsG;AACtG,MAAM,WAAW,+BAA+B;IAC9C,QAAQ,CAAC,QAAQ,EAAE,SAAS,sBAAsB,EAAE,CAAC;CACtD;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B"}
@@ -0,0 +1,54 @@
1
+ import { z } from 'zod';
2
+ import { CryptoKey_Purpose } from '@wildo-ai/saas-models';
3
+ /**
4
+ * # Data keys: what the application and the custodian exchange (#2894)
5
+ *
6
+ * The application seals its stored secrets under DATA keys, and a data key is only ever stored WRAPPED by its purpose's
7
+ * wrapping key, which never leaves the custodian (apps-manager). Five requests, all on the signed apps-to-manager channel:
8
+ *
9
+ * - **issue** a new data key: the custodian mints it, wraps it under the ACTIVE wrapping version, and answers with both
10
+ * halves (the application stores the wrapped one and holds the plaintext in memory only);
11
+ * - **unwrap** a stored data key, at boot or when a process first needs it;
12
+ * - **rewrap** a data key under the ACTIVE version after a rotation, without its plaintext leaving the custodian;
13
+ * - **release** a retired wrapping version once no stored data key is wrapped under it, so it can be destroyed;
14
+ * - **read the wrapping versions** and their states, from which the application derives, at every refresh, what it
15
+ * re-wraps, which data key stops sealing (one a COMPROMISED version wrapped), and which version it releases.
16
+ *
17
+ * The custodian builds the associated data of every wrap itself ({@link dataKeyAssociatedDataOf}): the purpose, the
18
+ * scope, the scope's id and the data key's id, so a wrapped key copied to another data key, organization or application
19
+ * does not unwrap. Both sides import this module.
20
+ */
21
+ /** The associated data a data key is wrapped under. */
22
+ export function dataKeyAssociatedDataOf(input) {
23
+ return Buffer.from(`wildo-data-key:v1|${input.purpose}|${input.scope}|${input.scopeId}|${input.dataKeyId}`, 'utf8');
24
+ }
25
+ const base64urlString = z.string().regex(/^[A-Za-z0-9_-]+$/);
26
+ /** Which keyring: the purpose, and the organization when the purpose's binding is organization-scoped. */
27
+ const DataKeyScopeRequestSchema = z.strictObject({
28
+ purpose: z.enum(CryptoKey_Purpose),
29
+ /** The organization whose key serves the request, when the binding is organization-scoped; ignored otherwise. */
30
+ organizationId: z.string().min(1).optional(),
31
+ });
32
+ export const DataKeyIssueRequestSchema = DataKeyScopeRequestSchema;
33
+ export const DataKeyUnwrapRequestSchema = DataKeyScopeRequestSchema.extend({
34
+ kid: z.string().min(1),
35
+ dataKeyId: z.string().min(1),
36
+ wrapped: base64urlString,
37
+ });
38
+ export const DataKeyRewrapRequestSchema = DataKeyUnwrapRequestSchema;
39
+ /**
40
+ * The header by which a rewrap declares that the application recorded the data key's exposure before asking (#3029),
41
+ * with the value {@link DATA_KEY_EXPOSURE_RECORDED}. The custodian refuses a rewrap off a COMPROMISED version without it
42
+ * (`exposure_unrecorded`): once the row is re-wrapped it no longer names the compromised version, and only the mark tells
43
+ * the application that the values sealed under that data key must be sealed again. A runtime from before #3029 records
44
+ * no mark and sends no header, so it is refused and the row waits for a current runtime to mark and move it.
45
+ *
46
+ * A header rather than a body field, decided: the request body is a strict object, so a custodian from before #3029
47
+ * would refuse an unknown field as `request_invalid`, which an application reads as "this runtime is refused" and stops
48
+ * sealing on. A custodian from before #3029 ignores the header, and the application marks its rows either way.
49
+ */
50
+ export const DATA_KEY_EXPOSURE_RECORDED_HEADER = 'x-wildo-data-key-exposure-recorded';
51
+ export const DATA_KEY_EXPOSURE_RECORDED = 'recorded';
52
+ export const DataKeyReleaseRequestSchema = DataKeyScopeRequestSchema.extend({ kid: z.string().min(1) });
53
+ export const DataKeyWrappingVersionsRequestSchema = DataKeyScopeRequestSchema;
54
+ //# sourceMappingURL=data-key.contracts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-key.contracts.js","sourceRoot":"","sources":["../../../../src/key-management/data-key.contracts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAgD,MAAM,uBAAuB,CAAC;AAExG;;;;;;;;;;;;;;;;;GAiBG;AAEH,uDAAuD;AACvD,MAAM,UAAU,uBAAuB,CAAC,KAAqI;IAC3K,OAAO,MAAM,CAAC,IAAI,CAAC,qBAAqB,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC;AACtH,CAAC;AAED,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAE7D,0GAA0G;AAC1G,MAAM,yBAAyB,GAAG,CAAC,CAAC,YAAY,CAAC;IAC/C,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC;IAClC,iHAAiH;IACjH,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;CAC7C,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,yBAAyB,GAAG,yBAAyB,CAAC;AAGnE,MAAM,CAAC,MAAM,0BAA0B,GAAG,yBAAyB,CAAC,MAAM,CAAC;IACzE,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACtB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B,OAAO,EAAE,eAAe;CACzB,CAAC,CAAC;AAGH,MAAM,CAAC,MAAM,0BAA0B,GAAG,0BAA0B,CAAC;AAGrE;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,oCAAoC,CAAC;AACtF,MAAM,CAAC,MAAM,0BAA0B,GAAG,UAAU,CAAC;AAErD,MAAM,CAAC,MAAM,2BAA2B,GAAG,yBAAyB,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AAGxG,MAAM,CAAC,MAAM,oCAAoC,GAAG,yBAAyB,CAAC","sourcesContent":["import { z } from 'zod';\nimport { CryptoKey_Purpose, CryptoKey_Scope, type CryptoKey_VersionState } from '@wildo-ai/saas-models';\n\n/**\n * # Data keys: what the application and the custodian exchange (#2894)\n *\n * The application seals its stored secrets under DATA keys, and a data key is only ever stored WRAPPED by its purpose's\n * wrapping key, which never leaves the custodian (apps-manager). Five requests, all on the signed apps-to-manager channel:\n *\n * - **issue** a new data key: the custodian mints it, wraps it under the ACTIVE wrapping version, and answers with both\n * halves (the application stores the wrapped one and holds the plaintext in memory only);\n * - **unwrap** a stored data key, at boot or when a process first needs it;\n * - **rewrap** a data key under the ACTIVE version after a rotation, without its plaintext leaving the custodian;\n * - **release** a retired wrapping version once no stored data key is wrapped under it, so it can be destroyed;\n * - **read the wrapping versions** and their states, from which the application derives, at every refresh, what it\n * re-wraps, which data key stops sealing (one a COMPROMISED version wrapped), and which version it releases.\n *\n * The custodian builds the associated data of every wrap itself ({@link dataKeyAssociatedDataOf}): the purpose, the\n * scope, the scope's id and the data key's id, so a wrapped key copied to another data key, organization or application\n * does not unwrap. Both sides import this module.\n */\n\n/** The associated data a data key is wrapped under. */\nexport function dataKeyAssociatedDataOf(input: { readonly purpose: CryptoKey_Purpose; readonly scope: CryptoKey_Scope; readonly scopeId: string; readonly dataKeyId: string }): Buffer {\n return Buffer.from(`wildo-data-key:v1|${input.purpose}|${input.scope}|${input.scopeId}|${input.dataKeyId}`, 'utf8');\n}\n\nconst base64urlString = z.string().regex(/^[A-Za-z0-9_-]+$/);\n\n/** Which keyring: the purpose, and the organization when the purpose's binding is organization-scoped. */\nconst DataKeyScopeRequestSchema = z.strictObject({\n purpose: z.enum(CryptoKey_Purpose),\n /** The organization whose key serves the request, when the binding is organization-scoped; ignored otherwise. */\n organizationId: z.string().min(1).optional(),\n});\n\nexport const DataKeyIssueRequestSchema = DataKeyScopeRequestSchema;\nexport type DataKeyIssueRequest = z.infer<typeof DataKeyIssueRequestSchema>;\n\nexport const DataKeyUnwrapRequestSchema = DataKeyScopeRequestSchema.extend({\n kid: z.string().min(1),\n dataKeyId: z.string().min(1),\n wrapped: base64urlString,\n});\nexport type DataKeyUnwrapRequest = z.infer<typeof DataKeyUnwrapRequestSchema>;\n\nexport const DataKeyRewrapRequestSchema = DataKeyUnwrapRequestSchema;\nexport type DataKeyRewrapRequest = z.infer<typeof DataKeyRewrapRequestSchema>;\n\n/**\n * The header by which a rewrap declares that the application recorded the data key's exposure before asking (#3029),\n * with the value {@link DATA_KEY_EXPOSURE_RECORDED}. The custodian refuses a rewrap off a COMPROMISED version without it\n * (`exposure_unrecorded`): once the row is re-wrapped it no longer names the compromised version, and only the mark tells\n * the application that the values sealed under that data key must be sealed again. A runtime from before #3029 records\n * no mark and sends no header, so it is refused and the row waits for a current runtime to mark and move it.\n *\n * A header rather than a body field, decided: the request body is a strict object, so a custodian from before #3029\n * would refuse an unknown field as `request_invalid`, which an application reads as \"this runtime is refused\" and stops\n * sealing on. A custodian from before #3029 ignores the header, and the application marks its rows either way.\n */\nexport const DATA_KEY_EXPOSURE_RECORDED_HEADER = 'x-wildo-data-key-exposure-recorded';\nexport const DATA_KEY_EXPOSURE_RECORDED = 'recorded';\n\nexport const DataKeyReleaseRequestSchema = DataKeyScopeRequestSchema.extend({ kid: z.string().min(1) });\nexport type DataKeyReleaseRequest = z.infer<typeof DataKeyReleaseRequestSchema>;\n\nexport const DataKeyWrappingVersionsRequestSchema = DataKeyScopeRequestSchema;\nexport type DataKeyWrappingVersionsRequest = z.infer<typeof DataKeyWrappingVersionsRequestSchema>;\n\n/** Where a data key belongs: the keyring scope the custodian resolved for the request. */\nexport interface DataKeyScope {\n readonly scope: CryptoKey_Scope;\n readonly scopeId: string;\n}\n\n/** A new data key: its id, its wrapping version, both halves (base64url), and the scope it was wrapped for. */\nexport interface DataKeyIssueResponse extends DataKeyScope {\n readonly dataKeyId: string;\n readonly kid: string;\n readonly dataKey: string;\n readonly wrapped: string;\n}\n\nexport interface DataKeyUnwrapResponse {\n readonly dataKey: string;\n}\n\n/** One wrapping version of the keyring, as the application needs it: its `kid`, its state, and whether it was released. */\nexport interface DataKeyWrappingVersion {\n readonly kid: string;\n readonly state: CryptoKey_VersionState;\n readonly released: boolean;\n}\n\n/** Every wrapping version of the keyring that serves the request (none before its first data key). */\nexport interface DataKeyWrappingVersionsResponse {\n readonly versions: readonly DataKeyWrappingVersion[];\n}\n\nexport interface DataKeyRewrapResponse {\n readonly kid: string;\n readonly wrapped: string;\n}\n\nexport interface DataKeyReleaseResponse {\n readonly kid: string;\n readonly releasedAt: string;\n}\n"]}
@@ -0,0 +1,60 @@
1
+ import { type KeyObject } from 'node:crypto';
2
+ /**
3
+ * The ES256 / JWS primitives every signer and verifier in the platform shares (#2891): the platform's answers to its
4
+ * runtimes (#2921), the keyring's signatures and the approval seal's verifier (#2903). One implementation, in a
5
+ * package with no JOSE dependency, so the offline verifier and every runtime read signatures the same way.
6
+ *
7
+ * spec-cite: RFC 7515 §5.1 (the JWS signing input), RFC 7518 §3.4 (ES256: ECDSA P-256 SHA-256, signature as the
8
+ * 64-byte concatenation R ‖ S, each left-padded to 32 bytes), RFC 7638 §3 (the JWK thumbprint over the required
9
+ * members in lexicographic order).
10
+ */
11
+ /** The public half of an ES256 key, as a JWK carrying only the members RFC 7638 hashes. */
12
+ export interface P256PublicJwk {
13
+ readonly kty: 'EC';
14
+ readonly crv: 'P-256';
15
+ readonly x: string;
16
+ readonly y: string;
17
+ }
18
+ /** The only JWS algorithm the platform signs with, pinned rather than read from a header (RFC 8725 §3.1). */
19
+ export declare const ES256_JWS_ALGORITHM = "ES256";
20
+ /** The length of a raw ES256 signature: R and S, 32 bytes each. */
21
+ export declare const ES256_RAW_SIGNATURE_BYTES = 64;
22
+ /** base64url without padding (RFC 7515 §2). */
23
+ export declare function base64url(input: Buffer | Uint8Array | string): string;
24
+ /** Whether a value is a P-256 public JWK with the members RFC 7638 hashes. */
25
+ export declare function isP256PublicJwk(value: unknown): value is P256PublicJwk;
26
+ /** The RFC 7638 thumbprint of a P-256 public key: SHA-256 over `{"crv","kty","x","y"}` in that order, base64url. */
27
+ export declare function p256JwkThumbprint(jwk: P256PublicJwk): string;
28
+ /** The P-256 public JWK of a key object (public or private); refuses any other key type. */
29
+ export declare function p256PublicJwkOf(key: KeyObject): P256PublicJwk;
30
+ /** The verification key object of a P-256 public JWK. */
31
+ export declare function p256KeyObjectOf(jwk: P256PublicJwk): KeyObject;
32
+ /** The JWS signing input, `base64url(header).base64url(payload)` (RFC 7515 §5.1), for JSON header and claims. */
33
+ export declare function jwsSigningInput(header: Readonly<Record<string, unknown>>, claims: unknown): string;
34
+ /** The SHA-256 digest of a JWS signing input over its ASCII bytes: what a remote key provider is asked to sign. */
35
+ export declare function jwsSigningInputDigest(signingInput: string): Buffer;
36
+ /** How a key provider encodes the ECDSA signatures it returns. Declared by the provider, never guessed from the bytes. */
37
+ export declare enum EcdsaSignatureEncoding {
38
+ /** R ‖ S, 32 bytes each: what JWS requires and what a JOSE-native provider returns. */
39
+ RAW = "raw",
40
+ /** The DER `SEQUENCE { INTEGER r, INTEGER s }` a cloud KMS or an HSM returns. */
41
+ DER = "der"
42
+ }
43
+ /**
44
+ * Normalize an ECDSA P-256 signature to the raw form ES256 requires: R ‖ S, each exactly 32 bytes, left-padded with
45
+ * zeros, never stripped (RFC 7518 §3.4). The encoding is the provider's declaration: guessing it from the bytes would be
46
+ * wrong for a valid raw signature whose first bytes happen to read as a DER header. Refuses a malformed signature, so
47
+ * one is never stored or returned.
48
+ */
49
+ export declare function normalizeEs256Signature(signature: Uint8Array, encoding: EcdsaSignatureEncoding): Buffer;
50
+ /**
51
+ * The low-S form of a raw ES256 signature: S replaced by n − S when S > n/2. Both forms verify; signing only the low
52
+ * one removes the trivial malleability (a second valid signature over the same input), which is why a seal's identity
53
+ * never rests on its signature bytes and the signing proxy still emits low-S only (#2903).
54
+ */
55
+ export declare function toLowSEs256Signature(signature: Uint8Array): Buffer;
56
+ /** Whether a raw ES256 signature is in the low-S form. */
57
+ export declare function isLowSEs256Signature(signature: Uint8Array): boolean;
58
+ /** Whether a raw ES256 signature verifies over a signing input under a public JWK. Verification is always local. */
59
+ export declare function verifyEs256(signingInput: string, signature: Uint8Array, publicJwk: P256PublicJwk): boolean;
60
+ //# sourceMappingURL=es256-jws.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"es256-jws.d.ts","sourceRoot":"","sources":["../../../../src/key-management/es256-jws.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAEpH;;;;;;;;GAQG;AAEH,2FAA2F;AAC3F,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,6GAA6G;AAC7G,eAAO,MAAM,mBAAmB,UAAU,CAAC;AAE3C,mEAAmE;AACnE,eAAO,MAAM,yBAAyB,KAAK,CAAC;AAE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only JWS algorithm the platform signs with, pinned rather than read from a header (RFC 8725 §3.1). */
3
+ export const ES256_JWS_ALGORITHM = 'ES256';
4
+ /** The length of a raw ES256 signature: R and S, 32 bytes each. */
5
+ export const ES256_RAW_SIGNATURE_BYTES = 64;
6
+ /** base64url without padding (RFC 7515 §2). */
7
+ export function base64url(input) {
8
+ return Buffer.from(input).toString('base64url');
9
+ }
10
+ /** Whether a value is a P-256 public JWK with the members RFC 7638 hashes. */
11
+ export function isP256PublicJwk(value) {
12
+ if (!value || typeof value !== 'object')
13
+ return false;
14
+ const jwk = value;
15
+ return jwk.kty === 'EC' && jwk.crv === 'P-256' && typeof jwk.x === 'string' && typeof jwk.y === 'string';
16
+ }
17
+ /** The RFC 7638 thumbprint of a P-256 public key: SHA-256 over `{"crv","kty","x","y"}` in that order, base64url. */
18
+ export function p256JwkThumbprint(jwk) {
19
+ const canonical = `{"crv":"${jwk.crv}","kty":"${jwk.kty}","x":"${jwk.x}","y":"${jwk.y}"}`;
20
+ return base64url(createHash('sha256').update(canonical).digest());
21
+ }
22
+ /** The P-256 public JWK of a key object (public or private); refuses any other key type. */
23
+ export function p256PublicJwkOf(key) {
24
+ const exported = (key.type === 'private' ? createPublicKey(key) : key).export({ format: 'jwk' });
25
+ if (!isP256PublicJwk(exported)) {
26
+ throw new Error('An ES256 key must be an EC P-256 key.');
27
+ }
28
+ return { kty: 'EC', crv: 'P-256', x: exported.x, y: exported.y };
29
+ }
30
+ /** The verification key object of a P-256 public JWK. */
31
+ export function p256KeyObjectOf(jwk) {
32
+ return createPublicKey({ key: { kty: 'EC', crv: 'P-256', x: jwk.x, y: jwk.y }, format: 'jwk' });
33
+ }
34
+ /** The JWS signing input, `base64url(header).base64url(payload)` (RFC 7515 §5.1), for JSON header and claims. */
35
+ export function jwsSigningInput(header, claims) {
36
+ return `${base64url(JSON.stringify(header))}.${base64url(JSON.stringify(claims))}`;
37
+ }
38
+ /** The SHA-256 digest of a JWS signing input over its ASCII bytes: what a remote key provider is asked to sign. */
39
+ export function jwsSigningInputDigest(signingInput) {
40
+ return createHash('sha256').update(Buffer.from(signingInput, 'ascii')).digest();
41
+ }
42
+ /** How a key provider encodes the ECDSA signatures it returns. Declared by the provider, never guessed from the bytes. */
43
+ export var EcdsaSignatureEncoding;
44
+ (function (EcdsaSignatureEncoding) {
45
+ /** R ‖ S, 32 bytes each: what JWS requires and what a JOSE-native provider returns. */
46
+ EcdsaSignatureEncoding["RAW"] = "raw";
47
+ /** The DER `SEQUENCE { INTEGER r, INTEGER s }` a cloud KMS or an HSM returns. */
48
+ EcdsaSignatureEncoding["DER"] = "der";
49
+ })(EcdsaSignatureEncoding || (EcdsaSignatureEncoding = {}));
50
+ /**
51
+ * Normalize an ECDSA P-256 signature to the raw form ES256 requires: R ‖ S, each exactly 32 bytes, left-padded with
52
+ * zeros, never stripped (RFC 7518 §3.4). The encoding is the provider's declaration: guessing it from the bytes would be
53
+ * wrong for a valid raw signature whose first bytes happen to read as a DER header. Refuses a malformed signature, so
54
+ * one is never stored or returned.
55
+ */
56
+ export function normalizeEs256Signature(signature, encoding) {
57
+ const bytes = Buffer.from(signature);
58
+ const fail = (why) => {
59
+ throw new Error(`Not an ES256 signature: ${why}.`);
60
+ };
61
+ if (encoding === EcdsaSignatureEncoding.RAW) {
62
+ if (bytes.length !== ES256_RAW_SIGNATURE_BYTES)
63
+ fail(`a raw signature is ${ES256_RAW_SIGNATURE_BYTES} bytes, got ${bytes.length}`);
64
+ return bytes;
65
+ }
66
+ if (bytes[0] !== 0x30)
67
+ fail('not a DER sequence');
68
+ let offset = 1;
69
+ const [sequenceLength, sequenceLengthBytes] = readDerLength(bytes, offset) ?? fail('bad sequence length');
70
+ offset += sequenceLengthBytes;
71
+ if (offset + sequenceLength !== bytes.length)
72
+ fail('the sequence length does not match the signature length');
73
+ const integers = [];
74
+ for (let index = 0; index < 2; index += 1) {
75
+ if (bytes[offset] !== 0x02)
76
+ fail('expected an INTEGER');
77
+ offset += 1;
78
+ const [integerLength, integerLengthBytes] = readDerLength(bytes, offset) ?? fail('bad integer length');
79
+ offset += integerLengthBytes;
80
+ let integer = bytes.subarray(offset, offset + integerLength);
81
+ offset += integerLength;
82
+ while (integer.length > 1 && integer[0] === 0x00)
83
+ integer = integer.subarray(1);
84
+ if (integer.length > 32)
85
+ fail('an integer is wider than 32 bytes');
86
+ integers.push(Buffer.concat([Buffer.alloc(32 - integer.length), integer]));
87
+ }
88
+ if (offset !== bytes.length)
89
+ fail('trailing bytes after the two integers');
90
+ return Buffer.concat(integers);
91
+ }
92
+ function readDerLength(bytes, offset) {
93
+ const first = bytes[offset];
94
+ if (first === undefined)
95
+ return undefined;
96
+ if (first < 0x80)
97
+ return [first, 1];
98
+ const count = first & 0x7f;
99
+ if (count === 0 || count > 2 || offset + 1 + count > bytes.length)
100
+ return undefined;
101
+ let length = 0;
102
+ for (let index = 0; index < count; index += 1)
103
+ length = (length << 8) | bytes[offset + 1 + index];
104
+ return [length, 1 + count];
105
+ }
106
+ /** The order of the P-256 group (SEC 2 §2.4.2), for the low-S form. */
107
+ const P256_ORDER = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
108
+ /**
109
+ * The low-S form of a raw ES256 signature: S replaced by n − S when S > n/2. Both forms verify; signing only the low
110
+ * one removes the trivial malleability (a second valid signature over the same input), which is why a seal's identity
111
+ * never rests on its signature bytes and the signing proxy still emits low-S only (#2903).
112
+ */
113
+ export function toLowSEs256Signature(signature) {
114
+ const bytes = Buffer.from(signature);
115
+ if (bytes.length !== ES256_RAW_SIGNATURE_BYTES)
116
+ throw new Error(`Not a raw ES256 signature: ${bytes.length} bytes.`);
117
+ const s = BigInt(`0x${bytes.subarray(32).toString('hex')}`);
118
+ if (s <= P256_ORDER >> 1n)
119
+ return bytes;
120
+ const lowS = Buffer.from((P256_ORDER - s).toString(16).padStart(64, '0'), 'hex');
121
+ return Buffer.concat([bytes.subarray(0, 32), lowS]);
122
+ }
123
+ /** Whether a raw ES256 signature is in the low-S form. */
124
+ export function isLowSEs256Signature(signature) {
125
+ if (signature.length !== ES256_RAW_SIGNATURE_BYTES)
126
+ return false;
127
+ return BigInt(`0x${Buffer.from(signature).subarray(32).toString('hex')}`) <= P256_ORDER >> 1n;
128
+ }
129
+ /** Whether a raw ES256 signature verifies over a signing input under a public JWK. Verification is always local. */
130
+ export function verifyEs256(signingInput, signature, publicJwk) {
131
+ if (signature.length !== ES256_RAW_SIGNATURE_BYTES)
132
+ return false;
133
+ return verifyWithKey('sha256', Buffer.from(signingInput, 'ascii'), { key: p256KeyObjectOf(publicJwk), dsaEncoding: 'ieee-p1363' }, signature);
134
+ }
135
+ //# sourceMappingURL=es256-jws.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"es256-jws.js","sourceRoot":"","sources":["../../../../src/key-management/es256-jws.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,IAAI,aAAa,EAAmC,MAAM,aAAa,CAAC;AAoBpH,6GAA6G;AAC7G,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC;AAE3C,mEAAmE;AACnE,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAE5C,+CAA+C;AAC/C,MAAM,UAAU,SAAS,CAAC,KAAmC;IAC3D,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IACtD,MAAM,GAAG,GAAG,KAAgC,CAAC;IAC7C,OAAO,GAAG,CAAC,GAAG,KAAK,IAAI,IAAI,GAAG,CAAC,GAAG,KAAK,OAAO,IAAI,OAAO,GAAG,CAAC,CAAC,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,CAAC,KAAK,QAAQ,CAA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CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IACjF,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,oBAAoB,CAAC,SAAqB;IACxD,IAAI,SAAS,CAAC,MAAM,KAAK,yBAAyB;QAAE,OAAO,KAAK,CAAC;IACjE,OAAO,MAAM,CAAC,KAAK,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,UAAU,IAAI,EAAE,CAAC;AAChG,CAAC;AAED,oHAAoH;AACpH,MAAM,UAAU,WAAW,CAAC,YAAoB,EAAE,SAAqB,EAAE,SAAwB;IAC/F,IAAI,SAAS,CAAC,MAAM,KAAK,yBAAyB;QAAE,OAAO,KAAK,CAAC;IACjE,OAAO,aAAa,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,EAAE,eAAe,CAAC,SAAS,CAAC,EAAE,WAAW,EAAE,YAAY,EAAE,EAAE,SAAS,CAAC,CAAC;AAChJ,CAAC","sourcesContent":["import { createHash, createPublicKey, verify as verifyWithKey, type JsonWebKey, type KeyObject } from 'node:crypto';\n\n/**\n * The ES256 / JWS primitives every signer and verifier in the platform shares (#2891): the platform's answers to its\n * runtimes (#2921), the keyring's signatures and the approval seal's verifier (#2903). One implementation, in a\n * package with no JOSE dependency, so the offline verifier and every runtime read signatures the same way.\n *\n * spec-cite: RFC 7515 §5.1 (the JWS signing input), RFC 7518 §3.4 (ES256: ECDSA P-256 SHA-256, signature as the\n * 64-byte concatenation R ‖ S, each left-padded to 32 bytes), RFC 7638 §3 (the JWK thumbprint over the required\n * members in lexicographic order).\n */\n\n/** The public half of an ES256 key, as a JWK carrying only the members RFC 7638 hashes. */\nexport interface P256PublicJwk {\n readonly kty: 'EC';\n readonly crv: 'P-256';\n readonly x: string;\n readonly y: string;\n}\n\n/** The only JWS algorithm the platform signs with, pinned rather than read from a header (RFC 8725 §3.1). */\nexport const ES256_JWS_ALGORITHM = 'ES256';\n\n/** The length of a raw ES256 signature: R and S, 32 bytes each. */\nexport const ES256_RAW_SIGNATURE_BYTES = 64;\n\n/** base64url without padding (RFC 7515 §2). */\nexport function base64url(input: Buffer | Uint8Array | string): string {\n return Buffer.from(input).toString('base64url');\n}\n\n/** Whether a value is a P-256 public JWK with the members RFC 7638 hashes. */\nexport function isP256PublicJwk(value: unknown): value is P256PublicJwk {\n if (!value || typeof value !== 'object') return false;\n const jwk = value as Record<string, unknown>;\n return jwk.kty === 'EC' && jwk.crv === 'P-256' && typeof jwk.x === 'string' && typeof jwk.y === 'string';\n}\n\n/** The RFC 7638 thumbprint of a P-256 public key: SHA-256 over `{\"crv\",\"kty\",\"x\",\"y\"}` in that order, base64url. */\nexport function p256JwkThumbprint(jwk: P256PublicJwk): string {\n const canonical = `{\"crv\":\"${jwk.crv}\",\"kty\":\"${jwk.kty}\",\"x\":\"${jwk.x}\",\"y\":\"${jwk.y}\"}`;\n return base64url(createHash('sha256').update(canonical).digest());\n}\n\n/** The P-256 public JWK of a key object (public or private); refuses any other key type. */\nexport function p256PublicJwkOf(key: KeyObject): P256PublicJwk {\n const exported = (key.type === 'private' ? createPublicKey(key) : key).export({ format: 'jwk' }) as JsonWebKey;\n if (!isP256PublicJwk(exported)) {\n throw new Error('An ES256 key must be an EC P-256 key.');\n }\n return { kty: 'EC', crv: 'P-256', x: exported.x, y: exported.y };\n}\n\n/** The verification key object of a P-256 public JWK. */\nexport function p256KeyObjectOf(jwk: P256PublicJwk): KeyObject {\n return createPublicKey({ key: { kty: 'EC', crv: 'P-256', x: jwk.x, y: jwk.y }, format: 'jwk' });\n}\n\n/** The JWS signing input, `base64url(header).base64url(payload)` (RFC 7515 §5.1), for JSON header and claims. */\nexport function jwsSigningInput(header: Readonly<Record<string, unknown>>, claims: unknown): string {\n return `${base64url(JSON.stringify(header))}.${base64url(JSON.stringify(claims))}`;\n}\n\n/** The SHA-256 digest of a JWS signing input over its ASCII bytes: what a remote key provider is asked to sign. */\nexport function jwsSigningInputDigest(signingInput: string): Buffer {\n return createHash('sha256').update(Buffer.from(signingInput, 'ascii')).digest();\n}\n\n/** How a key provider encodes the ECDSA signatures it returns. Declared by the provider, never guessed from the bytes. */\nexport enum EcdsaSignatureEncoding {\n /** R ‖ S, 32 bytes each: what JWS requires and what a JOSE-native provider returns. */\n RAW = 'raw',\n /** The DER `SEQUENCE { INTEGER r, INTEGER s }` a cloud KMS or an HSM returns. */\n DER = 'der',\n}\n\n/**\n * Normalize an ECDSA P-256 signature to the raw form ES256 requires: R ‖ S, each exactly 32 bytes, left-padded with\n * zeros, never stripped (RFC 7518 §3.4). The encoding is the provider's declaration: guessing it from the bytes would be\n * wrong for a valid raw signature whose first bytes happen to read as a DER header. Refuses a malformed signature, so\n * one is never stored or returned.\n */\nexport function normalizeEs256Signature(signature: Uint8Array, encoding: EcdsaSignatureEncoding): Buffer {\n const bytes = Buffer.from(signature);\n const fail = (why: string): never => {\n throw new Error(`Not an ES256 signature: ${why}.`);\n };\n if (encoding === EcdsaSignatureEncoding.RAW) {\n if (bytes.length !== ES256_RAW_SIGNATURE_BYTES) fail(`a raw signature is ${ES256_RAW_SIGNATURE_BYTES} bytes, got ${bytes.length}`);\n return bytes;\n }\n if (bytes[0] !== 0x30) fail('not a DER sequence');\n let offset = 1;\n const [sequenceLength, sequenceLengthBytes] = readDerLength(bytes, offset) ?? fail('bad sequence length');\n offset += sequenceLengthBytes;\n if (offset + sequenceLength !== bytes.length) fail('the sequence length does not match the signature length');\n const integers: Buffer[] = [];\n for (let index = 0; index < 2; index += 1) {\n if (bytes[offset] !== 0x02) fail('expected an INTEGER');\n offset += 1;\n const [integerLength, integerLengthBytes] = readDerLength(bytes, offset) ?? fail('bad integer length');\n offset += integerLengthBytes;\n let integer = bytes.subarray(offset, offset + integerLength);\n offset += integerLength;\n while (integer.length > 1 && integer[0] === 0x00) integer = integer.subarray(1);\n if (integer.length > 32) fail('an integer is wider than 32 bytes');\n integers.push(Buffer.concat([Buffer.alloc(32 - integer.length), integer]));\n }\n if (offset !== bytes.length) fail('trailing bytes after the two integers');\n return Buffer.concat(integers);\n}\n\nfunction readDerLength(bytes: Buffer, offset: number): [number, number] | undefined {\n const first = bytes[offset];\n if (first === undefined) return undefined;\n if (first < 0x80) return [first, 1];\n const count = first & 0x7f;\n if (count === 0 || count > 2 || offset + 1 + count > bytes.length) return undefined;\n let length = 0;\n for (let index = 0; index < count; index += 1) length = (length << 8) | (bytes[offset + 1 + index] as number);\n return [length, 1 + count];\n}\n\n/** The order of the P-256 group (SEC 2 §2.4.2), for the low-S form. */\nconst P256_ORDER = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;\n\n/**\n * The low-S form of a raw ES256 signature: S replaced by n − S when S > n/2. Both forms verify; signing only the low\n * one removes the trivial malleability (a second valid signature over the same input), which is why a seal's identity\n * never rests on its signature bytes and the signing proxy still emits low-S only (#2903).\n */\nexport function toLowSEs256Signature(signature: Uint8Array): Buffer {\n const bytes = Buffer.from(signature);\n if (bytes.length !== ES256_RAW_SIGNATURE_BYTES) throw new Error(`Not a raw ES256 signature: ${bytes.length} bytes.`);\n const s = BigInt(`0x${bytes.subarray(32).toString('hex')}`);\n if (s <= P256_ORDER >> 1n) return bytes;\n const lowS = Buffer.from((P256_ORDER - s).toString(16).padStart(64, '0'), 'hex');\n return Buffer.concat([bytes.subarray(0, 32), lowS]);\n}\n\n/** Whether a raw ES256 signature is in the low-S form. */\nexport function isLowSEs256Signature(signature: Uint8Array): boolean {\n if (signature.length !== ES256_RAW_SIGNATURE_BYTES) return false;\n return BigInt(`0x${Buffer.from(signature).subarray(32).toString('hex')}`) <= P256_ORDER >> 1n;\n}\n\n/** Whether a raw ES256 signature verifies over a signing input under a public JWK. Verification is always local. */\nexport function verifyEs256(signingInput: string, signature: Uint8Array, publicJwk: P256PublicJwk): boolean {\n if (signature.length !== ES256_RAW_SIGNATURE_BYTES) return false;\n return verifyWithKey('sha256', Buffer.from(signingInput, 'ascii'), { key: p256KeyObjectOf(publicJwk), dsaEncoding: 'ieee-p1363' }, signature);\n}\n"]}
@@ -0,0 +1,151 @@
1
+ import { type KeyObject } from 'node:crypto';
2
+ import type { CryptoKey_MaterialAccess, CryptoKey_MaterialProvenance, CryptoKey_OperationMode, CryptoKey_Primitive, CryptoKey_ProtectionLevel, CryptoKeyFloor } from '@wildo-ai/saas-models';
3
+ import type { EcdsaSignatureEncoding, P256PublicJwk } from './es256-jws';
4
+ /**
5
+ * # The key provider SPI (#2891)
6
+ *
7
+ * What a key provider implements: the engine's LOCAL provider today, Vault, cloud KMS and HSM providers in slice 2
8
+ * (#2933, #2896, #2934), a customer's own provider later (#2919). The custodian (apps-manager for platform custody)
9
+ * drives a provider through this interface and nothing else; a consumer never reaches one, it asks the key-management
10
+ * API by purpose.
11
+ *
12
+ * A provider declares, per key, how operations happen:
13
+ * - **REMOTE**: the key never leaves. Every operation is a call: `signDigest` names the exact version (no call may
14
+ * resolve "latest"), and the digest is SHA-256 over the ASCII JWS signing input (RFC 7515 §5.1).
15
+ * - **RETRIEVED**: the provider hands the material to the custodian, which operates locally. The LOCAL provider is
16
+ * RETRIEVED to its custodian only; the purpose's `heldBy` decides whether any runtime may retrieve too.
17
+ *
18
+ * Data-key wrapping (#2894): a wrapping version (`AES_256_GCM_DATA_KEY`) wraps the application's data keys. A REMOTE
19
+ * provider wraps and unwraps itself (`wrapDataKey` / `unwrapDataKey`), with the associated data bound into the wrap; a
20
+ * RETRIEVED provider hands the custodian the AES key and the custodian wraps locally ({@link wrapDataKeyLocally}). MAC is
21
+ * not on this interface: no purpose's MAC key is provider-held (#2893's keys are the application's own).
22
+ *
23
+ * Verification is never a provider call: the custodian verifies every returned signature locally against the public
24
+ * JWK of the version it named (verify-after-sign), and every verifier works from published public keys.
25
+ */
26
+ /** Names one version of one key at one provider, exactly. */
27
+ export interface KeyVersionRef {
28
+ readonly providerRef: string;
29
+ /** The provider's name for the key (a KMS key id, a Vault key name, a LOCAL key id). */
30
+ readonly keyRef: string;
31
+ /** The provider's name for the version. Never "latest". */
32
+ readonly versionRef: string;
33
+ }
34
+ /** What a provider attests about one key version, re-read at every keyring refresh. */
35
+ export interface KeyVersionAttestation {
36
+ readonly protectionLevel: CryptoKey_ProtectionLevel;
37
+ readonly materialAccess: CryptoKey_MaterialAccess;
38
+ readonly provenance: CryptoKey_MaterialProvenance;
39
+ /** False once the provider has disabled or destroyed the version's private material. */
40
+ readonly privateMaterialUsable: boolean;
41
+ }
42
+ /** A newly created key version. */
43
+ export interface CreatedKeyVersion {
44
+ readonly ref: KeyVersionRef;
45
+ readonly primitive: CryptoKey_Primitive;
46
+ /** The public half, for an asymmetric primitive. */
47
+ readonly publicJwk?: P256PublicJwk;
48
+ readonly attestation: KeyVersionAttestation;
49
+ }
50
+ /**
51
+ * What a vendor charges and allows for a provider's operations (#2896): stated, as prose a person reads, when a purpose is
52
+ * bound to the provider, so the cost of binding a busy purpose to a paid KMS is never a surprise. Each value is the
53
+ * vendor's published default at the time it was written; an account's own quota may differ, and the provider's source
54
+ * file cites where each figure comes from.
55
+ */
56
+ export interface KeyProviderVendorTerms {
57
+ /** How many signing requests the vendor serves before throttling. */
58
+ readonly signRequestQuota: string;
59
+ /** What one signing request costs. */
60
+ readonly signCost: string;
61
+ /** What holding one key version costs. */
62
+ readonly keyCost: string;
63
+ /** How a throttle shows itself, and how the keyring treats it. */
64
+ readonly throttling: string;
65
+ /**
66
+ * What wrapping a data key costs (#2894), for a provider that wraps: one wrap per data key, and one unwrap per data key
67
+ * each time an application runtime starts, so far fewer calls than signatures.
68
+ */
69
+ readonly wrapCost?: string;
70
+ /** Where the vendor records each signature, for corroboration against the signing log. */
71
+ readonly auditLog: string;
72
+ }
73
+ /** A key provider, as the custodian drives it. */
74
+ export interface KeyProvider {
75
+ readonly providerRef: string;
76
+ /** How this provider performs operations. A provider may support both; the custodian prefers REMOTE. */
77
+ readonly operationModes: readonly CryptoKey_OperationMode[];
78
+ /** The encoding of the signatures `signDigest` returns; the custodian normalizes to raw R ‖ S. */
79
+ readonly signatureEncoding: EcdsaSignatureEncoding;
80
+ /** The primitives this provider can create keys for. */
81
+ readonly primitives: readonly CryptoKey_Primitive[];
82
+ /**
83
+ * A paid vendor's terms, stated in apps-manager's log when the provider registers at boot (the moment the operator who
84
+ * declared the account reads it); absent for a provider that costs nothing per call.
85
+ */
86
+ readonly vendorTerms?: KeyProviderVendorTerms;
87
+ /**
88
+ * The vendor's quota on signatures, as a rate, and what it is shared across. apps-manager keeps its own signing under
89
+ * a share of it (a throttle answers a retryable PROVIDER_FAULT), so the relay retries instead of the vendor refusing.
90
+ * Absent: the provider states no quota (a dedicated HSM's throughput is the operator's own).
91
+ */
92
+ readonly signRequestQuota?: {
93
+ readonly perSecond: number;
94
+ readonly sharedAcross: string;
95
+ };
96
+ /**
97
+ * The most this provider's DEPLOYMENT can offer, as configured: a Google key ring creating SOFTWARE keys, an Azure
98
+ * vault making `EC` keys, a PKCS#11 token whose model is not allow-listed. The keyring refuses a purpose whose floor it
99
+ * cannot meet BEFORE creating a version, rather than creating a vendor key and destroying it on every attempt. Absent:
100
+ * only the version's own attestation is checked.
101
+ */
102
+ deploymentCeiling?(): Promise<CryptoKeyFloor>;
103
+ /** Create a new version of `keyRef` (the key itself is created with its first version). */
104
+ createKeyVersion(input: {
105
+ readonly keyRef: string;
106
+ readonly primitive: CryptoKey_Primitive;
107
+ }): Promise<CreatedKeyVersion>;
108
+ /** What the provider attests about the version now. */
109
+ describeKeyVersion(ref: KeyVersionRef): Promise<KeyVersionAttestation>;
110
+ /** The public half of an asymmetric version. */
111
+ getPublicKey(ref: KeyVersionRef): Promise<P256PublicJwk>;
112
+ /** REMOTE: sign a SHA-256 digest with exactly this version. */
113
+ signDigest?(ref: KeyVersionRef, digest: Uint8Array): Promise<Uint8Array>;
114
+ /** RETRIEVED: the private (or secret, for a wrapping key) material of exactly this version, to the custodian. */
115
+ retrieveKeyMaterial?(ref: KeyVersionRef): Promise<KeyObject>;
116
+ /**
117
+ * REMOTE: wrap a data key under exactly this wrapping version, binding `associatedData` into the wrap, so a wrapped key
118
+ * moved to another data key's row does not unwrap. The wrap must be AUTHENTICATED encryption made with a secret only the
119
+ * provider holds (AES-GCM and its vendor forms): a public-key wrap (RSA `wrapKey`) can be made by anyone holding the
120
+ * public half, so a substituted data key would unwrap (#2894 stage D review). Returns the provider's opaque wrapped form.
121
+ */
122
+ wrapDataKey?(ref: KeyVersionRef, dataKey: Uint8Array, associatedData: Uint8Array): Promise<Uint8Array>;
123
+ /** REMOTE: unwrap what {@link wrapDataKey} returned, refusing it under any other associated data. */
124
+ unwrapDataKey?(ref: KeyVersionRef, wrapped: Uint8Array, associatedData: Uint8Array): Promise<Uint8Array>;
125
+ /** Stop the version from signing; its material is kept until destroyed. */
126
+ disableKeyVersion(ref: KeyVersionRef): Promise<void>;
127
+ /** Destroy the version's private material. Irreversible; the public half is the custodian's to keep. */
128
+ destroyKeyVersion(ref: KeyVersionRef): Promise<void>;
129
+ /**
130
+ * Whether the version's private material can still be used: false when what protects it is gone (the LOCAL provider's
131
+ * key-encryption root was lost). Only a provider that can LOSE its own access implements it; absent means always.
132
+ */
133
+ materialIsReadable?(ref: KeyVersionRef): Promise<boolean>;
134
+ }
135
+ /** A data key is an AES-256 key (#2894). */
136
+ export declare const DATA_KEY_BYTES = 32;
137
+ /**
138
+ * Wrap a data key with a wrapping key the custodian holds (a RETRIEVED provider's version): AES-256-GCM with a random IV,
139
+ * the associated data authenticated, as `iv ‖ tag ‖ ciphertext`. One IV per wrap, and a wrapping key wraps few keys (one
140
+ * per data key, each used for many values), far below the 2^32 random-IV bound (NIST SP 800-38D §8.3).
141
+ */
142
+ export declare function wrapDataKeyLocally(wrappingKey: KeyObject, dataKey: Uint8Array, associatedData: Uint8Array): Uint8Array;
143
+ /** Unwrap what {@link wrapDataKeyLocally} returned; throws when the wrap, the key or the associated data differ. */
144
+ export declare function unwrapDataKeyLocally(wrappingKey: KeyObject, wrapped: Uint8Array, associatedData: Uint8Array): Uint8Array;
145
+ /**
146
+ * The `kid` of a wrapping version (#2894). A signing version's `kid` is its public key's thumbprint; a wrapping key has no
147
+ * public half, so its `kid` is derived from the version's provider references, never from its material, and names it in
148
+ * every envelope sealed under a data key it wraps.
149
+ */
150
+ export declare function wrappingKeyKidOf(ref: KeyVersionRef): string;
151
+ //# sourceMappingURL=key-provider.spi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-provider.spi.d.ts","sourceRoot":"","sources":["../../../../src/key-management/key-provider.spi.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6D,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AACxG,OAAO,KAAK,EACV,wBAAwB,EACxB,4BAA4B,EAC5B,uBAAuB,EACvB,mBAAmB,EACnB,yBAAyB,EACzB,cAAc,EACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,6DAA6D;AAC7D,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,wFAAwF;IACxF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2DAA2D;IAC3D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,uFAAuF;AACvF,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,eAAe,EAAE,yBAAyB,CAAC;IACpD,QAAQ,CAAC,cAAc,EAAE,wBAAwB,CAAC;IAClD,QAA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CAAC,CAAC;CAC3D;AAED,4CAA4C;AAC5C,eAAO,MAAM,cAAc,KAAK,CAAC;AAKjC;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,GAAG,UAAU,CAOtH;AAED,oHAAoH;AACpH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,UAAU,GAAG,UAAU,CAOxH;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAE3D"}
@@ -0,0 +1,38 @@
1
+ import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto';
2
+ /** A data key is an AES-256 key (#2894). */
3
+ export const DATA_KEY_BYTES = 32;
4
+ const LOCAL_WRAP_IV_BYTES = 12;
5
+ const LOCAL_WRAP_TAG_BYTES = 16;
6
+ /**
7
+ * Wrap a data key with a wrapping key the custodian holds (a RETRIEVED provider's version): AES-256-GCM with a random IV,
8
+ * the associated data authenticated, as `iv ‖ tag ‖ ciphertext`. One IV per wrap, and a wrapping key wraps few keys (one
9
+ * per data key, each used for many values), far below the 2^32 random-IV bound (NIST SP 800-38D §8.3).
10
+ */
11
+ export function wrapDataKeyLocally(wrappingKey, dataKey, associatedData) {
12
+ if (dataKey.length !== DATA_KEY_BYTES)
13
+ throw new Error(`A data key is ${DATA_KEY_BYTES} bytes, not ${dataKey.length}.`);
14
+ const iv = randomBytes(LOCAL_WRAP_IV_BYTES);
15
+ const cipher = createCipheriv('aes-256-gcm', wrappingKey, iv);
16
+ cipher.setAAD(associatedData);
17
+ const ciphertext = Buffer.concat([cipher.update(dataKey), cipher.final()]);
18
+ return Buffer.concat([iv, cipher.getAuthTag(), ciphertext]);
19
+ }
20
+ /** Unwrap what {@link wrapDataKeyLocally} returned; throws when the wrap, the key or the associated data differ. */
21
+ export function unwrapDataKeyLocally(wrappingKey, wrapped, associatedData) {
22
+ const bytes = Buffer.from(wrapped);
23
+ if (bytes.length !== LOCAL_WRAP_IV_BYTES + LOCAL_WRAP_TAG_BYTES + DATA_KEY_BYTES)
24
+ throw new Error('Not a locally wrapped data key.');
25
+ const decipher = createDecipheriv('aes-256-gcm', wrappingKey, bytes.subarray(0, LOCAL_WRAP_IV_BYTES));
26
+ decipher.setAAD(associatedData);
27
+ decipher.setAuthTag(bytes.subarray(LOCAL_WRAP_IV_BYTES, LOCAL_WRAP_IV_BYTES + LOCAL_WRAP_TAG_BYTES));
28
+ return Buffer.concat([decipher.update(bytes.subarray(LOCAL_WRAP_IV_BYTES + LOCAL_WRAP_TAG_BYTES)), decipher.final()]);
29
+ }
30
+ /**
31
+ * The `kid` of a wrapping version (#2894). A signing version's `kid` is its public key's thumbprint; a wrapping key has no
32
+ * public half, so its `kid` is derived from the version's provider references, never from its material, and names it in
33
+ * every envelope sealed under a data key it wraps.
34
+ */
35
+ export function wrappingKeyKidOf(ref) {
36
+ return createHash('sha256').update(`wildo-wrapping-key-id:v1|${ref.providerRef}|${ref.keyRef}|${ref.versionRef}`).digest('base64url').slice(0, 22);
37
+ }
38
+ //# sourceMappingURL=key-provider.spi.js.map