@raiton/vault 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,501 @@
1
+ /**
2
+ * Public contracts of @raiton/vault.
3
+ *
4
+ * The module orchestrates audited cryptographic libraries and KMS providers; it
5
+ * never implements its own primitives. These types describe the lifecycle and
6
+ * the adapter boundaries consumed by the Raiton kernel.
7
+ */
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // Capabilities
11
+ // ---------------------------------------------------------------------------
12
+
13
+ export const CAPABILITIES = [
14
+ "foundation",
15
+ "keys",
16
+ "kms",
17
+ "envelope",
18
+ "field",
19
+ "blind-index",
20
+ "signing",
21
+ "secrets",
22
+ "shredding",
23
+ "pki",
24
+ "audit",
25
+ "pqc",
26
+ ] as const
27
+
28
+ export type CapabilityId = (typeof CAPABILITIES)[number]
29
+
30
+ export const CAPABILITY_CODES = {
31
+ foundation: "001",
32
+ keys: "002",
33
+ kms: "003",
34
+ envelope: "004",
35
+ field: "005",
36
+ "blind-index": "006",
37
+ signing: "007",
38
+ secrets: "008",
39
+ shredding: "009",
40
+ pki: "010",
41
+ audit: "011",
42
+ pqc: "012",
43
+ } as const
44
+
45
+ export type CapabilityCode = (typeof CAPABILITY_CODES)[CapabilityId]
46
+ export type CapabilityInput = CapabilityId | CapabilityCode | string
47
+
48
+ export interface CapabilityDefinition {
49
+ id: CapabilityId
50
+ code: CapabilityCode
51
+ label: string
52
+ summary: string
53
+ requires: readonly CapabilityId[]
54
+ aliases: readonly string[]
55
+ service: string | null
56
+ }
57
+
58
+ export const CAPABILITY_DEFINITIONS: readonly CapabilityDefinition[] = [
59
+ {
60
+ id: "foundation",
61
+ code: "001",
62
+ label: "Socle et configuration",
63
+ summary: "Noyau, configuration validée, adaptateur, plugin Raiton, diagnostic et cycle de vie.",
64
+ requires: [],
65
+ aliases: ["core", "socle", "base"],
66
+ service: "VaultKernel",
67
+ },
68
+ {
69
+ id: "keys",
70
+ code: "002",
71
+ label: "Cycle de vie des clés",
72
+ summary: "Génération, alias, versions, rotation, dépréciation, retrait et destruction.",
73
+ requires: ["foundation", "audit"],
74
+ aliases: ["key", "keyring"],
75
+ service: "KeyService",
76
+ },
77
+ {
78
+ id: "kms",
79
+ code: "003",
80
+ label: "KMS",
81
+ summary: "Contrat KMS et adaptateurs local, cloud et HSM sans exposer le matériel au cœur.",
82
+ requires: ["keys"],
83
+ aliases: ["key-management", "hsm"],
84
+ service: "KmsProvider",
85
+ },
86
+ {
87
+ id: "envelope",
88
+ code: "004",
89
+ label: "Chiffrement d'enveloppe",
90
+ summary: "Chiffrement DEK/KEK et chiffrement au repos des champs, fichiers et secrets.",
91
+ requires: ["kms"],
92
+ aliases: ["envelope-encryption", "dek", "kek"],
93
+ service: "EnvelopeService",
94
+ },
95
+ {
96
+ id: "field",
97
+ code: "005",
98
+ label: "Chiffrement de champ",
99
+ summary: "Décorateur @Encrypted(), intégration ORM, modes déterministe et aléatoire.",
100
+ requires: ["envelope"],
101
+ aliases: ["fields", "encrypted"],
102
+ service: "FieldEncryption",
103
+ },
104
+ {
105
+ id: "blind-index",
106
+ code: "006",
107
+ label: "Index aveugle",
108
+ summary: "Recherches d'égalité et jointures sur données chiffrées, avec séparation des clés.",
109
+ requires: ["field"],
110
+ aliases: ["blind-indexes", "searchable-encryption"],
111
+ service: "BlindIndexService",
112
+ },
113
+ {
114
+ id: "signing",
115
+ code: "007",
116
+ label: "Signature",
117
+ summary: "JWS/JWE/COSE, signatures détachées et canonicalisation documentée.",
118
+ requires: ["keys"],
119
+ aliases: ["sign", "jws", "jwe", "cose"],
120
+ service: "SigningService",
121
+ },
122
+ {
123
+ id: "secrets",
124
+ code: "008",
125
+ label: "Secrets",
126
+ summary: "Références de secrets, rotation, révocation et détection de fuite.",
127
+ requires: ["kms"],
128
+ aliases: ["secret", "credentials"],
129
+ service: "SecretService",
130
+ },
131
+ {
132
+ id: "shredding",
133
+ code: "009",
134
+ label: "Crypto-shredding",
135
+ summary: "Effacement RGPD par destruction de clé et preuve horodatée et signée.",
136
+ requires: ["keys"],
137
+ aliases: ["shred", "crypto-shred", "erasure"],
138
+ service: "ShreddingService",
139
+ },
140
+ {
141
+ id: "pki",
142
+ code: "010",
143
+ label: "PKI",
144
+ summary: "CSR, certificats X.509, mTLS et ACME pour les certificats de service.",
145
+ requires: ["keys"],
146
+ aliases: ["certificates", "x509", "acme", "mtls"],
147
+ service: "PkiService",
148
+ },
149
+ {
150
+ id: "audit",
151
+ code: "011",
152
+ label: "Audit",
153
+ summary: "Usage des clés, accès, rotation et preuves d'intégrité attribuables.",
154
+ requires: ["foundation"],
155
+ aliases: ["audit-trail", "evidence"],
156
+ service: "AuditService",
157
+ },
158
+ {
159
+ id: "pqc",
160
+ code: "012",
161
+ label: "Post-quantique",
162
+ summary: "Feuille de route ML-KEM, ML-DSA et mode hybride lorsque le KMS le supporte.",
163
+ requires: ["keys"],
164
+ aliases: ["post-quantum", "ml-kem", "ml-dsa"],
165
+ service: "PqcService",
166
+ },
167
+ ]
168
+
169
+ export const CAPABILITY_BY_ID: Readonly<Record<CapabilityId, CapabilityDefinition>> = Object.fromEntries(
170
+ CAPABILITY_DEFINITIONS.map(definition => [definition.id, definition]),
171
+ ) as Record<CapabilityId, CapabilityDefinition>
172
+
173
+ export const CAPABILITY_BY_CODE: Readonly<Record<string, CapabilityDefinition>> = Object.fromEntries(
174
+ CAPABILITY_DEFINITIONS.map(definition => [definition.code, definition]),
175
+ ) as Record<string, CapabilityDefinition>
176
+
177
+ /** Capabilities with a working implementation in this revision. */
178
+ export const IMPLEMENTED_CAPABILITIES: readonly CapabilityId[] = [
179
+ "foundation",
180
+ "keys",
181
+ "kms",
182
+ "envelope",
183
+ "audit",
184
+ ]
185
+
186
+ // ---------------------------------------------------------------------------
187
+ // Domain model
188
+ // ---------------------------------------------------------------------------
189
+
190
+ export type KeyUsage = "encrypt" | "decrypt" | "sign" | "verify" | "wrap" | "unwrap"
191
+ export type KeyAlgorithm = "aes-256-gcm" | "ed25519" | "ecdsa-p256" | "rsa-oaep-256"
192
+ export type KeyStatus = "active" | "deprecated" | "retired" | "destroyed"
193
+ export type KeyVersionStatus = "active" | "deprecated" | "retired" | "destroyed"
194
+ export type AuditAction =
195
+ | "key.generate"
196
+ | "key.rotate"
197
+ | "key.destroy"
198
+ | "key.use.encrypt"
199
+ | "key.use.decrypt"
200
+ | "key.use.sign"
201
+ | "key.use.verify"
202
+ | "secret.create"
203
+ | "secret.read"
204
+ | "secret.rotate"
205
+ | "secret.revoke"
206
+ | "shred.execute"
207
+ export type AuditOutcome = "success" | "failure" | "denied"
208
+
209
+ export interface RotationPolicy {
210
+ intervalDays: number
211
+ graceDays: number
212
+ autoRotate: boolean
213
+ reencrypt: boolean
214
+ }
215
+
216
+ export interface VaultKey {
217
+ id: string
218
+ tenantId?: string
219
+ alias: string
220
+ usage: KeyUsage
221
+ algorithm: KeyAlgorithm
222
+ status: KeyStatus
223
+ currentVersion: number
224
+ rotation: RotationPolicy
225
+ ownerId?: string
226
+ metadata?: Record<string, unknown>
227
+ createdAt: string
228
+ updatedAt: string
229
+ }
230
+
231
+ export interface KeyVersion {
232
+ id: string
233
+ keyId: string
234
+ version: number
235
+ materialRef: string
236
+ algorithm: KeyAlgorithm
237
+ status: KeyVersionStatus
238
+ createdAt: string
239
+ activatedAt?: string
240
+ deprecatedAt?: string
241
+ destroyedAt?: string
242
+ }
243
+
244
+ export interface EncryptedEnvelope {
245
+ keyId: string
246
+ keyAlias: string
247
+ keyVersion: number
248
+ algorithm: KeyAlgorithm
249
+ ciphertext: string
250
+ aad?: string
251
+ createdAt: string
252
+ }
253
+
254
+ export interface AuditEntry {
255
+ id: string
256
+ action: AuditAction
257
+ outcome: AuditOutcome
258
+ resourceType: "key" | "key-version" | "secret" | "shred"
259
+ resourceId: string
260
+ keyId?: string
261
+ keyVersion?: number
262
+ tenantId?: string
263
+ actorId?: string
264
+ correlationId?: string
265
+ context?: Record<string, unknown>
266
+ occurredAt: string
267
+ }
268
+
269
+ export interface VaultActor {
270
+ actorId?: string
271
+ tenantId?: string
272
+ scopes?: readonly string[]
273
+ correlationId?: string
274
+ }
275
+
276
+ export interface KeyFilter {
277
+ tenantId?: string
278
+ usage?: KeyUsage
279
+ status?: KeyStatus
280
+ alias?: string
281
+ }
282
+
283
+ export interface AuditFilter {
284
+ tenantId?: string
285
+ actorId?: string
286
+ action?: AuditAction
287
+ resourceId?: string
288
+ since?: string
289
+ limit?: number
290
+ }
291
+
292
+ // ---------------------------------------------------------------------------
293
+ // Adapter contracts
294
+ // ---------------------------------------------------------------------------
295
+
296
+ /**
297
+ * ORM-agnostic persistence contract. The core never imports an ORM.
298
+ * Implementations must be tenant-aware and fail closed on unknown tenants.
299
+ */
300
+ export interface VaultAdapter {
301
+ readonly name: string
302
+ readonly capabilities: readonly string[]
303
+
304
+ createKey(key: VaultKey): Promise<void>
305
+ updateKey(key: VaultKey): Promise<void>
306
+ deleteKey(id: string): Promise<void>
307
+ getKey(id: string): Promise<VaultKey | null>
308
+ findKeyByAlias(alias: string, tenantId?: string): Promise<VaultKey | null>
309
+ listKeys(filter?: KeyFilter): Promise<VaultKey[]>
310
+
311
+ createKeyVersion(version: KeyVersion): Promise<void>
312
+ updateKeyVersion(version: KeyVersion): Promise<void>
313
+ getKeyVersion(keyId: string, version: number): Promise<KeyVersion | null>
314
+ listKeyVersions(keyId: string): Promise<KeyVersion[]>
315
+
316
+ appendAudit(entry: AuditEntry): Promise<void>
317
+ listAudit(filter?: AuditFilter): Promise<AuditEntry[]>
318
+ }
319
+
320
+ /** KMS capabilities declared by a provider. */
321
+ export type KmsCapability =
322
+ | "generate"
323
+ | "encrypt"
324
+ | "decrypt"
325
+ | "sign"
326
+ | "verify"
327
+ | "destroy"
328
+ | "pqc"
329
+ | "hsm"
330
+
331
+ export interface KmsGenerateInput {
332
+ keyId: string
333
+ version: number
334
+ algorithm: KeyAlgorithm
335
+ tenantId?: string
336
+ }
337
+
338
+ export interface KmsKeyRef {
339
+ ref: string
340
+ algorithm: KeyAlgorithm
341
+ publicKey?: string
342
+ }
343
+
344
+ /**
345
+ * Provider contract. Key material never crosses this boundary in clear text:
346
+ * encrypt/decrypt/sign/verify happen inside the provider (local KMS, cloud KMS
347
+ * or HSM).
348
+ */
349
+ export interface KmsProvider {
350
+ readonly name: string
351
+ readonly capabilities: readonly KmsCapability[]
352
+
353
+ generate(input: KmsGenerateInput): Promise<KmsKeyRef>
354
+ encrypt(ref: string, plaintext: Uint8Array, aad?: string): Promise<string>
355
+ decrypt(ref: string, ciphertext: string, aad?: string): Promise<Uint8Array>
356
+ sign(ref: string, data: Uint8Array): Promise<string>
357
+ verify(ref: string, data: Uint8Array, signature: string): Promise<boolean>
358
+ destroy(ref: string): Promise<void>
359
+ }
360
+
361
+ // ---------------------------------------------------------------------------
362
+ // Configuration
363
+ // ---------------------------------------------------------------------------
364
+
365
+ export type VaultMode = "single" | "multi"
366
+ export type KmsProviderName = "local" | "custom"
367
+
368
+ export interface VaultKeyConfig {
369
+ defaultAlgorithm: KeyAlgorithm
370
+ rotation: RotationPolicy
371
+ }
372
+
373
+ export interface VaultKmsConfig {
374
+ provider: KmsProviderName
375
+ keyCacheSeconds: number
376
+ }
377
+
378
+ export interface VaultEnvelopeConfig {
379
+ algorithm: "aes-256-gcm"
380
+ bindAad: boolean
381
+ }
382
+
383
+ export interface VaultAuditConfig {
384
+ enabled: boolean
385
+ retentionDays: number
386
+ }
387
+
388
+ export interface VaultPersistenceConfig {
389
+ orm: string | null
390
+ dialect: "postgres" | "mysql" | "sqlite"
391
+ directory: string
392
+ }
393
+
394
+ export interface VaultConfig {
395
+ mode: VaultMode
396
+ capabilities: CapabilityId[]
397
+ keys: VaultKeyConfig
398
+ kms: VaultKmsConfig
399
+ envelope: VaultEnvelopeConfig
400
+ audit: VaultAuditConfig
401
+ persistence: VaultPersistenceConfig
402
+ }
403
+
404
+ export interface VaultConfigInput {
405
+ mode?: VaultMode
406
+ capabilities?: CapabilityInput[]
407
+ keys?: Partial<Omit<VaultKeyConfig, "rotation">> & {rotation?: Partial<RotationPolicy>}
408
+ kms?: Partial<VaultKmsConfig>
409
+ envelope?: Partial<VaultEnvelopeConfig>
410
+ audit?: Partial<VaultAuditConfig>
411
+ persistence?: Partial<VaultPersistenceConfig>
412
+ }
413
+
414
+ // ---------------------------------------------------------------------------
415
+ // Service inputs and reports
416
+ // ---------------------------------------------------------------------------
417
+
418
+ export interface GenerateKeyInput {
419
+ alias: string
420
+ tenantId?: string
421
+ usage?: KeyUsage
422
+ algorithm?: KeyAlgorithm
423
+ ownerId?: string
424
+ rotation?: Partial<RotationPolicy>
425
+ metadata?: Record<string, unknown>
426
+ actor?: VaultActor
427
+ }
428
+
429
+ export interface GeneratedKey {
430
+ key: VaultKey
431
+ version: KeyVersion
432
+ }
433
+
434
+ export interface RotateKeyInput {
435
+ key: string
436
+ tenantId?: string
437
+ actor?: VaultActor
438
+ reason?: string
439
+ }
440
+
441
+ export interface DestroyKeyInput {
442
+ key: string
443
+ tenantId?: string
444
+ actor?: VaultActor
445
+ reason?: string
446
+ }
447
+
448
+ export interface EncryptInput {
449
+ plaintext: string | Uint8Array
450
+ key: string
451
+ tenantId?: string
452
+ aad?: string
453
+ actor?: VaultActor
454
+ }
455
+
456
+ export interface DecryptInput {
457
+ envelope: EncryptedEnvelope
458
+ tenantId?: string
459
+ aad?: string
460
+ actor?: VaultActor
461
+ }
462
+
463
+ export interface SignInput {
464
+ data: string | Uint8Array
465
+ key: string
466
+ tenantId?: string
467
+ actor?: VaultActor
468
+ }
469
+
470
+ export interface VerifyInput {
471
+ data: string | Uint8Array
472
+ signature: string
473
+ key: string
474
+ tenantId?: string
475
+ actor?: VaultActor
476
+ }
477
+
478
+ export interface VaultCheck {
479
+ name: string
480
+ status: "pass" | "warn" | "fail"
481
+ message: string
482
+ remediation?: string
483
+ }
484
+
485
+ export interface VaultHealthReport {
486
+ status: "healthy" | "degraded" | "unhealthy"
487
+ mode: VaultMode
488
+ adapter: string
489
+ kms: string
490
+ capabilities: CapabilityId[]
491
+ contractVersion: string
492
+ migrations: {applied: number; pending: string[]}
493
+ keys: {total: number; active: number}
494
+ checks: VaultCheck[]
495
+ }
496
+
497
+ export interface VaultEventContext {
498
+ tenantId?: string
499
+ actorId?: string
500
+ correlationId?: string
501
+ }