@veridid/signing 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +39 -2
- package/build/SigningApi.d.mts +368 -0
- package/build/SigningApi.d.mts.map +1 -0
- package/build/SigningApi.mjs +516 -0
- package/build/SigningApi.mjs.map +1 -0
- package/build/SigningEvents.d.mts +71 -0
- package/build/SigningEvents.d.mts.map +1 -0
- package/build/SigningEvents.mjs +16 -0
- package/build/SigningEvents.mjs.map +1 -0
- package/build/SigningModule.d.mts +42 -0
- package/build/SigningModule.d.mts.map +1 -0
- package/build/SigningModule.mjs +91 -0
- package/build/SigningModule.mjs.map +1 -0
- package/build/SigningRole.d.mts +25 -0
- package/build/SigningRole.d.mts.map +1 -0
- package/build/SigningRole.mjs +27 -0
- package/build/SigningRole.mjs.map +1 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs +10 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs +7 -0
- package/build/_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateParam.mjs +9 -0
- package/build/compat.d.mts +24 -0
- package/build/compat.d.mts.map +1 -0
- package/build/compat.mjs +26 -0
- package/build/compat.mjs.map +1 -0
- package/build/delivery/ArtifactDeliveryStrategy.d.mts +74 -0
- package/build/delivery/ArtifactDeliveryStrategy.d.mts.map +1 -0
- package/build/delivery/InlineBase64Strategy.d.mts +50 -0
- package/build/delivery/InlineBase64Strategy.d.mts.map +1 -0
- package/build/delivery/InlineBase64Strategy.mjs +80 -0
- package/build/delivery/InlineBase64Strategy.mjs.map +1 -0
- package/build/delivery/index.mjs +1 -0
- package/build/errors/ArtifactTooLargeError.d.mts +12 -0
- package/build/errors/ArtifactTooLargeError.d.mts.map +1 -0
- package/build/errors/ArtifactTooLargeError.mjs +15 -0
- package/build/errors/ArtifactTooLargeError.mjs.map +1 -0
- package/build/errors/DigestMismatchError.d.mts +12 -0
- package/build/errors/DigestMismatchError.d.mts.map +1 -0
- package/build/errors/DigestMismatchError.mjs +15 -0
- package/build/errors/DigestMismatchError.mjs.map +1 -0
- package/build/errors/IdempotencyConflictError.d.mts +12 -0
- package/build/errors/IdempotencyConflictError.d.mts.map +1 -0
- package/build/errors/IdempotencyConflictError.mjs +15 -0
- package/build/errors/IdempotencyConflictError.mjs.map +1 -0
- package/build/errors/InvalidStateError.d.mts +14 -0
- package/build/errors/InvalidStateError.d.mts.map +1 -0
- package/build/errors/InvalidStateError.mjs +16 -0
- package/build/errors/InvalidStateError.mjs.map +1 -0
- package/build/errors/PolicyEnforcementError.d.mts +12 -0
- package/build/errors/PolicyEnforcementError.d.mts.map +1 -0
- package/build/errors/PolicyEnforcementError.mjs +15 -0
- package/build/errors/PolicyEnforcementError.mjs.map +1 -0
- package/build/errors/SigningError.d.mts +16 -0
- package/build/errors/SigningError.d.mts.map +1 -0
- package/build/errors/SigningError.mjs +16 -0
- package/build/errors/SigningError.mjs.map +1 -0
- package/build/errors/ThresholdNotMetError.d.mts +12 -0
- package/build/errors/ThresholdNotMetError.d.mts.map +1 -0
- package/build/errors/ThresholdNotMetError.mjs +15 -0
- package/build/errors/ThresholdNotMetError.mjs.map +1 -0
- package/build/errors/UnsupportedCanonicalizerError.d.mts +12 -0
- package/build/errors/UnsupportedCanonicalizerError.d.mts.map +1 -0
- package/build/errors/UnsupportedCanonicalizerError.mjs +16 -0
- package/build/errors/UnsupportedCanonicalizerError.mjs.map +1 -0
- package/build/errors/UnsupportedSuiteError.d.mts +12 -0
- package/build/errors/UnsupportedSuiteError.d.mts.map +1 -0
- package/build/errors/UnsupportedSuiteError.mjs +16 -0
- package/build/errors/UnsupportedSuiteError.mjs.map +1 -0
- package/build/errors/index.d.mts +9 -0
- package/build/errors/index.mjs +9 -0
- package/build/handlers/CombineMessageHandler.d.mts +22 -0
- package/build/handlers/CombineMessageHandler.d.mts.map +1 -0
- package/build/handlers/CombineMessageHandler.mjs +25 -0
- package/build/handlers/CombineMessageHandler.mjs.map +1 -0
- package/build/handlers/ConsentMessageHandler.d.mts +19 -0
- package/build/handlers/ConsentMessageHandler.d.mts.map +1 -0
- package/build/handlers/ConsentMessageHandler.mjs +21 -0
- package/build/handlers/ConsentMessageHandler.mjs.map +1 -0
- package/build/handlers/DeclineMessageHandler.d.mts +19 -0
- package/build/handlers/DeclineMessageHandler.d.mts.map +1 -0
- package/build/handlers/DeclineMessageHandler.mjs +21 -0
- package/build/handlers/DeclineMessageHandler.mjs.map +1 -0
- package/build/handlers/PartialSignatureMessageHandler.d.mts +19 -0
- package/build/handlers/PartialSignatureMessageHandler.d.mts.map +1 -0
- package/build/handlers/PartialSignatureMessageHandler.mjs +21 -0
- package/build/handlers/PartialSignatureMessageHandler.mjs.map +1 -0
- package/build/handlers/ProposeSigningMessageHandler.d.mts +19 -0
- package/build/handlers/ProposeSigningMessageHandler.d.mts.map +1 -0
- package/build/handlers/ProposeSigningMessageHandler.mjs +21 -0
- package/build/handlers/ProposeSigningMessageHandler.mjs.map +1 -0
- package/build/handlers/ProvideArtifactsMessageHandler.d.mts +19 -0
- package/build/handlers/ProvideArtifactsMessageHandler.d.mts.map +1 -0
- package/build/handlers/ProvideArtifactsMessageHandler.mjs +21 -0
- package/build/handlers/ProvideArtifactsMessageHandler.mjs.map +1 -0
- package/build/handlers/RequestSigningMessageHandler.d.mts +19 -0
- package/build/handlers/RequestSigningMessageHandler.d.mts.map +1 -0
- package/build/handlers/RequestSigningMessageHandler.mjs +21 -0
- package/build/handlers/RequestSigningMessageHandler.mjs.map +1 -0
- package/build/handlers/SigningAckMessageHandler.d.mts +22 -0
- package/build/handlers/SigningAckMessageHandler.d.mts.map +1 -0
- package/build/handlers/SigningAckMessageHandler.mjs +25 -0
- package/build/handlers/SigningAckMessageHandler.mjs.map +1 -0
- package/build/handlers/SigningProblemReportMessageHandler.d.mts +19 -0
- package/build/handlers/SigningProblemReportMessageHandler.d.mts.map +1 -0
- package/build/handlers/SigningProblemReportMessageHandler.mjs +21 -0
- package/build/handlers/SigningProblemReportMessageHandler.mjs.map +1 -0
- package/build/handlers/index.d.mts +9 -0
- package/build/handlers/index.mjs +9 -0
- package/build/index.d.mts +75 -0
- package/build/index.mjs +63 -0
- package/build/messages/CombineMessage.d.mts +32 -0
- package/build/messages/CombineMessage.d.mts.map +1 -0
- package/build/messages/CombineMessage.mjs +47 -0
- package/build/messages/CombineMessage.mjs.map +1 -0
- package/build/messages/ConsentMessage.d.mts +32 -0
- package/build/messages/ConsentMessage.d.mts.map +1 -0
- package/build/messages/ConsentMessage.mjs +46 -0
- package/build/messages/ConsentMessage.mjs.map +1 -0
- package/build/messages/DeclineMessage.d.mts +26 -0
- package/build/messages/DeclineMessage.d.mts.map +1 -0
- package/build/messages/DeclineMessage.mjs +47 -0
- package/build/messages/DeclineMessage.mjs.map +1 -0
- package/build/messages/PartialSignatureMessage.d.mts +36 -0
- package/build/messages/PartialSignatureMessage.d.mts.map +1 -0
- package/build/messages/PartialSignatureMessage.mjs +72 -0
- package/build/messages/PartialSignatureMessage.mjs.map +1 -0
- package/build/messages/ProposeSigningMessage.d.mts +27 -0
- package/build/messages/ProposeSigningMessage.d.mts.map +1 -0
- package/build/messages/ProposeSigningMessage.mjs +41 -0
- package/build/messages/ProposeSigningMessage.mjs.map +1 -0
- package/build/messages/ProvideArtifactsMessage.d.mts +62 -0
- package/build/messages/ProvideArtifactsMessage.d.mts.map +1 -0
- package/build/messages/ProvideArtifactsMessage.mjs +40 -0
- package/build/messages/ProvideArtifactsMessage.mjs.map +1 -0
- package/build/messages/RequestSigningMessage.d.mts +38 -0
- package/build/messages/RequestSigningMessage.d.mts.map +1 -0
- package/build/messages/RequestSigningMessage.mjs +62 -0
- package/build/messages/RequestSigningMessage.mjs.map +1 -0
- package/build/messages/SigningAckMessage.d.mts +26 -0
- package/build/messages/SigningAckMessage.d.mts.map +1 -0
- package/build/messages/SigningAckMessage.mjs +46 -0
- package/build/messages/SigningAckMessage.mjs.map +1 -0
- package/build/messages/SigningProblemReportMessage.d.mts +44 -0
- package/build/messages/SigningProblemReportMessage.d.mts.map +1 -0
- package/build/messages/SigningProblemReportMessage.mjs +65 -0
- package/build/messages/SigningProblemReportMessage.mjs.map +1 -0
- package/build/messages/index.d.mts +9 -0
- package/build/messages/index.mjs +9 -0
- package/build/models/AggregationResult.d.mts +85 -0
- package/build/models/AggregationResult.d.mts.map +1 -0
- package/build/models/ArtifactMetadata.d.mts +37 -0
- package/build/models/ArtifactMetadata.d.mts.map +1 -0
- package/build/models/AuthzArtifact.d.mts +178 -0
- package/build/models/AuthzArtifact.d.mts.map +1 -0
- package/build/models/Capabilities.d.mts +109 -0
- package/build/models/Capabilities.d.mts.map +1 -0
- package/build/models/JoinToken.d.mts +344 -0
- package/build/models/JoinToken.d.mts.map +1 -0
- package/build/models/JoinToken.mjs +14 -0
- package/build/models/JoinToken.mjs.map +1 -0
- package/build/models/PartialSignature.d.mts +82 -0
- package/build/models/PartialSignature.d.mts.map +1 -0
- package/build/models/PolicyContext.d.mts +55 -0
- package/build/models/PolicyContext.d.mts.map +1 -0
- package/build/models/RouterAttestation.d.mts +186 -0
- package/build/models/RouterAttestation.d.mts.map +1 -0
- package/build/models/Session.d.mts +62 -0
- package/build/models/Session.d.mts.map +1 -0
- package/build/models/Session.mjs +19 -0
- package/build/models/Session.mjs.map +1 -0
- package/build/models/SignableObject.d.mts +71 -0
- package/build/models/SignableObject.d.mts.map +1 -0
- package/build/models/SigningState.d.mts +21 -0
- package/build/models/SigningState.d.mts.map +1 -0
- package/build/models/SigningState.mjs +23 -0
- package/build/models/SigningState.mjs.map +1 -0
- package/build/models/Suite.d.mts +59 -0
- package/build/models/Suite.d.mts.map +1 -0
- package/build/models/index.d.mts +12 -0
- package/build/pdf/PdfUtils.d.mts +160 -0
- package/build/pdf/PdfUtils.d.mts.map +1 -0
- package/build/pdf/PdfUtils.mjs +283 -0
- package/build/pdf/PdfUtils.mjs.map +1 -0
- package/build/pdf/index.mjs +1 -0
- package/build/registry/aggregators/Aggregator.d.mts +85 -0
- package/build/registry/aggregators/Aggregator.d.mts.map +1 -0
- package/build/registry/aggregators/AggregatorRegistry.d.mts +55 -0
- package/build/registry/aggregators/AggregatorRegistry.d.mts.map +1 -0
- package/build/registry/aggregators/AggregatorRegistry.mjs +76 -0
- package/build/registry/aggregators/AggregatorRegistry.mjs.map +1 -0
- package/build/registry/aggregators/BundleNoneAggregator.d.mts +43 -0
- package/build/registry/aggregators/BundleNoneAggregator.d.mts.map +1 -0
- package/build/registry/aggregators/BundleNoneAggregator.mjs +65 -0
- package/build/registry/aggregators/BundleNoneAggregator.mjs.map +1 -0
- package/build/registry/aggregators/index.d.mts +3 -0
- package/build/registry/aggregators/index.mjs +2 -0
- package/build/registry/canonicalizers/Canonicalizer.d.mts +67 -0
- package/build/registry/canonicalizers/Canonicalizer.d.mts.map +1 -0
- package/build/registry/canonicalizers/CanonicalizerRegistry.d.mts +55 -0
- package/build/registry/canonicalizers/CanonicalizerRegistry.d.mts.map +1 -0
- package/build/registry/canonicalizers/CanonicalizerRegistry.mjs +78 -0
- package/build/registry/canonicalizers/CanonicalizerRegistry.mjs.map +1 -0
- package/build/registry/canonicalizers/PdfIncrementalUpdateCanonicalizer.d.mts +126 -0
- package/build/registry/canonicalizers/PdfIncrementalUpdateCanonicalizer.d.mts.map +1 -0
- package/build/registry/canonicalizers/PdfIncrementalUpdateCanonicalizer.mjs +143 -0
- package/build/registry/canonicalizers/PdfIncrementalUpdateCanonicalizer.mjs.map +1 -0
- package/build/registry/canonicalizers/RawBytesCanonicalizer.d.mts +34 -0
- package/build/registry/canonicalizers/RawBytesCanonicalizer.d.mts.map +1 -0
- package/build/registry/canonicalizers/RawBytesCanonicalizer.mjs +55 -0
- package/build/registry/canonicalizers/RawBytesCanonicalizer.mjs.map +1 -0
- package/build/registry/canonicalizers/index.d.mts +4 -0
- package/build/registry/canonicalizers/index.mjs +3 -0
- package/build/registry/index.d.mts +18 -0
- package/build/registry/index.mjs +14 -0
- package/build/registry/policies/Policy.d.mts +62 -0
- package/build/registry/policies/Policy.d.mts.map +1 -0
- package/build/registry/policies/PolicyRegistry.d.mts +54 -0
- package/build/registry/policies/PolicyRegistry.d.mts.map +1 -0
- package/build/registry/policies/PolicyRegistry.mjs +73 -0
- package/build/registry/policies/PolicyRegistry.mjs.map +1 -0
- package/build/registry/policies/TimeWindowPolicy.d.mts +85 -0
- package/build/registry/policies/TimeWindowPolicy.d.mts.map +1 -0
- package/build/registry/policies/TimeWindowPolicy.mjs +111 -0
- package/build/registry/policies/TimeWindowPolicy.mjs.map +1 -0
- package/build/registry/policies/index.d.mts +3 -0
- package/build/registry/policies/index.mjs +2 -0
- package/build/registry/suites/JwsEd25519Suite.d.mts +47 -0
- package/build/registry/suites/JwsEd25519Suite.d.mts.map +1 -0
- package/build/registry/suites/JwsEd25519Suite.mjs +101 -0
- package/build/registry/suites/JwsEd25519Suite.mjs.map +1 -0
- package/build/registry/suites/PadesBLtaSuite.d.mts +106 -0
- package/build/registry/suites/PadesBLtaSuite.d.mts.map +1 -0
- package/build/registry/suites/PadesBLtaSuite.mjs +122 -0
- package/build/registry/suites/PadesBLtaSuite.mjs.map +1 -0
- package/build/registry/suites/SignatureSuite.d.mts +128 -0
- package/build/registry/suites/SignatureSuite.d.mts.map +1 -0
- package/build/registry/suites/SignatureSuiteRegistry.d.mts +62 -0
- package/build/registry/suites/SignatureSuiteRegistry.d.mts.map +1 -0
- package/build/registry/suites/SignatureSuiteRegistry.mjs +87 -0
- package/build/registry/suites/SignatureSuiteRegistry.mjs.map +1 -0
- package/build/registry/suites/index.d.mts +4 -0
- package/build/registry/suites/index.mjs +3 -0
- package/build/repository/CounterRecord.d.mts +84 -0
- package/build/repository/CounterRecord.d.mts.map +1 -0
- package/build/repository/CounterRecord.mjs +82 -0
- package/build/repository/CounterRecord.mjs.map +1 -0
- package/build/repository/CounterRepository.d.mts +22 -0
- package/build/repository/CounterRepository.d.mts.map +1 -0
- package/build/repository/CounterRepository.mjs +51 -0
- package/build/repository/CounterRepository.mjs.map +1 -0
- package/build/repository/SigningSessionRecord.d.mts +102 -0
- package/build/repository/SigningSessionRecord.d.mts.map +1 -0
- package/build/repository/SigningSessionRecord.mjs +96 -0
- package/build/repository/SigningSessionRecord.mjs.map +1 -0
- package/build/repository/SigningSessionRepository.d.mts +30 -0
- package/build/repository/SigningSessionRepository.d.mts.map +1 -0
- package/build/repository/SigningSessionRepository.mjs +52 -0
- package/build/repository/SigningSessionRepository.mjs.map +1 -0
- package/build/repository/index.d.mts +4 -0
- package/build/repository/index.mjs +4 -0
- package/build/services/CounterService.d.mts +121 -0
- package/build/services/CounterService.d.mts.map +1 -0
- package/build/services/CounterService.mjs +176 -0
- package/build/services/CounterService.mjs.map +1 -0
- package/build/services/HPKEService.d.mts +174 -0
- package/build/services/HPKEService.d.mts.map +1 -0
- package/build/services/HPKEService.mjs +195 -0
- package/build/services/HPKEService.mjs.map +1 -0
- package/build/services/SigningService.d.mts +387 -0
- package/build/services/SigningService.d.mts.map +1 -0
- package/build/services/SigningService.mjs +1104 -0
- package/build/services/SigningService.mjs.map +1 -0
- package/build/services/index.d.mts +3 -0
- package/build/services/index.mjs +3 -0
- package/package.json +44 -3
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { CounterRepository } from "../repository/CounterRepository.mjs";
|
|
2
|
+
import { AgentContext } from "@credo-ts/core";
|
|
3
|
+
|
|
4
|
+
//#region src/services/CounterService.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Result of counter verification
|
|
7
|
+
*/
|
|
8
|
+
interface CounterVerificationResult {
|
|
9
|
+
/**
|
|
10
|
+
* Whether the counter is valid (greater than last seen)
|
|
11
|
+
*/
|
|
12
|
+
valid: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Reason for failure (if not valid)
|
|
15
|
+
*/
|
|
16
|
+
reason?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Last seen counter value
|
|
19
|
+
*/
|
|
20
|
+
lastSeenCounter?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Provided counter value
|
|
23
|
+
*/
|
|
24
|
+
providedCounter?: number;
|
|
25
|
+
}
|
|
26
|
+
declare class CounterService {
|
|
27
|
+
private counterRepository;
|
|
28
|
+
constructor(counterRepository: CounterRepository);
|
|
29
|
+
/**
|
|
30
|
+
* Get current counter value for a device
|
|
31
|
+
*
|
|
32
|
+
* Returns the last seen counter value. The next valid counter
|
|
33
|
+
* must be greater than this value.
|
|
34
|
+
*
|
|
35
|
+
* @param agentContext - Agent context
|
|
36
|
+
* @param deviceId - Device identifier
|
|
37
|
+
* @returns Current counter value (0 if device not found)
|
|
38
|
+
*/
|
|
39
|
+
getCounter(agentContext: AgentContext, deviceId: string): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Increment counter for a device
|
|
42
|
+
*
|
|
43
|
+
* Generates the next counter value for creating a new token.
|
|
44
|
+
* This should be called when creating a new join token.
|
|
45
|
+
*
|
|
46
|
+
* @param agentContext - Agent context
|
|
47
|
+
* @param deviceId - Device identifier
|
|
48
|
+
* @returns New counter value (currentCounter + 1)
|
|
49
|
+
*/
|
|
50
|
+
incrementCounter(agentContext: AgentContext, deviceId: string): Promise<number>;
|
|
51
|
+
/**
|
|
52
|
+
* Verify a counter value without updating
|
|
53
|
+
*
|
|
54
|
+
* Checks if a counter is valid (greater than last seen) but does NOT
|
|
55
|
+
* persist the counter. Use verifyAndUpdateCounter() to verify and persist.
|
|
56
|
+
*
|
|
57
|
+
* @param agentContext - Agent context
|
|
58
|
+
* @param deviceId - Device identifier
|
|
59
|
+
* @param ctr - Counter value to verify
|
|
60
|
+
* @returns Verification result
|
|
61
|
+
*/
|
|
62
|
+
verifyCounter(agentContext: AgentContext, deviceId: string, ctr: number): Promise<CounterVerificationResult>;
|
|
63
|
+
/**
|
|
64
|
+
* Verify and update counter
|
|
65
|
+
*
|
|
66
|
+
* CRITICAL: This must be called BEFORE consuming a token.
|
|
67
|
+
* If token consumption fails after this, the counter cannot be rolled back.
|
|
68
|
+
*
|
|
69
|
+
* Steps:
|
|
70
|
+
* 1. Verify counter is valid (ctr > last_seen_ctr)
|
|
71
|
+
* 2. Persist new counter value
|
|
72
|
+
* 3. Mark counter as used
|
|
73
|
+
*
|
|
74
|
+
* @param agentContext - Agent context
|
|
75
|
+
* @param deviceId - Device identifier
|
|
76
|
+
* @param ctr - Counter value to verify and update
|
|
77
|
+
* @returns Verification result
|
|
78
|
+
*/
|
|
79
|
+
verifyAndUpdateCounter(agentContext: AgentContext, deviceId: string, ctr: number): Promise<CounterVerificationResult>;
|
|
80
|
+
/**
|
|
81
|
+
* Mark a counter as used
|
|
82
|
+
*
|
|
83
|
+
* Adds the counter to the used set without updating currentCounter.
|
|
84
|
+
* This can be used for batch processing or out-of-order token consumption.
|
|
85
|
+
*
|
|
86
|
+
* @param agentContext - Agent context
|
|
87
|
+
* @param deviceId - Device identifier
|
|
88
|
+
* @param ctr - Counter value to mark as used
|
|
89
|
+
*/
|
|
90
|
+
markCounterUsed(agentContext: AgentContext, deviceId: string, ctr: number): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Reset counter for a device
|
|
93
|
+
*
|
|
94
|
+
* WARNING: This is dangerous and should only be used for testing or
|
|
95
|
+
* recovery scenarios. Resetting counters can allow replay attacks.
|
|
96
|
+
*
|
|
97
|
+
* @param agentContext - Agent context
|
|
98
|
+
* @param deviceId - Device identifier
|
|
99
|
+
* @param newCounter - New counter value (default 0)
|
|
100
|
+
*/
|
|
101
|
+
resetCounter(agentContext: AgentContext, deviceId: string, newCounter?: number): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Get counter statistics for a device
|
|
104
|
+
*
|
|
105
|
+
* Useful for monitoring and debugging.
|
|
106
|
+
*
|
|
107
|
+
* @param agentContext - Agent context
|
|
108
|
+
* @param deviceId - Device identifier
|
|
109
|
+
* @returns Counter statistics
|
|
110
|
+
*/
|
|
111
|
+
getCounterStats(agentContext: AgentContext, deviceId: string): Promise<{
|
|
112
|
+
deviceId: string;
|
|
113
|
+
currentCounter: number;
|
|
114
|
+
usedCountersCount: number;
|
|
115
|
+
createdAt: Date | undefined;
|
|
116
|
+
updatedAt: Date;
|
|
117
|
+
} | null>;
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
export { CounterService, CounterVerificationResult };
|
|
121
|
+
//# sourceMappingURL=CounterService.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CounterService.d.mts","names":[],"sources":["../../src/services/CounterService.ts"],"mappings":";;;;;;;UAwBiB,yBAAA;EAgFC;;;EA5EhB,KAAA;EAmJW;;;EA9IX,MAAA;EA6MgB;;;EAxMhB,eAAA;EAsOa;;;EAjOb,eAAA;AAAA;AAAA,cAIW,cAAA;EAAA,QACgB,iBAAA;cAAA,iBAAA,EAAmB,iBAAA;EAYjC;;;;;;;;;;EAAA,UAAA,CAAW,YAAA,EAAc,YAAA,EAAc,QAAA,WAAmB,OAAA;EA4CvD;;;;;;;;;;EA7BH,gBAAA,CAAiB,YAAA,EAAc,YAAA,EAAc,QAAA,WAAmB,OAAA;EAoG1E;;;;;;;;;;;EAxEU,aAAA,CACX,YAAA,EAAc,YAAA,EACd,QAAA,UACA,GAAA,WACC,OAAA,CAAQ,yBAAA;EAqIT;;;;;;;;;;;;;;;;EArEW,sBAAA,CACX,YAAA,EAAc,YAAA,EACd,QAAA,UACA,GAAA,WACC,OAAA,CAAQ,yBAAA;;;;;;;;;;;EAqCE,eAAA,CAAgB,YAAA,EAAc,YAAA,EAAc,QAAA,UAAkB,GAAA,WAAc,OAAA;;;;;;;;;;;EAyB5E,YAAA,CACX,YAAA,EAAc,YAAA,EACd,QAAA,UACA,UAAA,YACC,OAAA;;;;;;;;;;EAmBU,eAAA,CACX,YAAA,EAAc,YAAA,EACd,QAAA,WACC,OAAA;IACD,QAAA;IACA,cAAA;IACA,iBAAA;IACA,SAAA,EAAW,IAAA;IACX,SAAA,EAAW,IAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorateMetadata.mjs";
|
|
2
|
+
import { __decorate } from "../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs";
|
|
3
|
+
import { CounterRepository } from "../repository/CounterRepository.mjs";
|
|
4
|
+
import { injectable } from "@credo-ts/core";
|
|
5
|
+
|
|
6
|
+
//#region src/services/CounterService.ts
|
|
7
|
+
var _ref;
|
|
8
|
+
let CounterService = class CounterService {
|
|
9
|
+
constructor(counterRepository) {
|
|
10
|
+
this.counterRepository = counterRepository;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get current counter value for a device
|
|
14
|
+
*
|
|
15
|
+
* Returns the last seen counter value. The next valid counter
|
|
16
|
+
* must be greater than this value.
|
|
17
|
+
*
|
|
18
|
+
* @param agentContext - Agent context
|
|
19
|
+
* @param deviceId - Device identifier
|
|
20
|
+
* @returns Current counter value (0 if device not found)
|
|
21
|
+
*/
|
|
22
|
+
async getCounter(agentContext, deviceId) {
|
|
23
|
+
const record = await this.counterRepository.findByDeviceId(agentContext, deviceId);
|
|
24
|
+
return record ? record.currentCounter : 0;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Increment counter for a device
|
|
28
|
+
*
|
|
29
|
+
* Generates the next counter value for creating a new token.
|
|
30
|
+
* This should be called when creating a new join token.
|
|
31
|
+
*
|
|
32
|
+
* @param agentContext - Agent context
|
|
33
|
+
* @param deviceId - Device identifier
|
|
34
|
+
* @returns New counter value (currentCounter + 1)
|
|
35
|
+
*/
|
|
36
|
+
async incrementCounter(agentContext, deviceId) {
|
|
37
|
+
const record = await this.counterRepository.getOrCreate(agentContext, deviceId);
|
|
38
|
+
const newCounter = record.currentCounter + 1;
|
|
39
|
+
record.updateCounter(newCounter);
|
|
40
|
+
record.pruneOldCounters(100);
|
|
41
|
+
await this.counterRepository.update(agentContext, record);
|
|
42
|
+
return newCounter;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Verify a counter value without updating
|
|
46
|
+
*
|
|
47
|
+
* Checks if a counter is valid (greater than last seen) but does NOT
|
|
48
|
+
* persist the counter. Use verifyAndUpdateCounter() to verify and persist.
|
|
49
|
+
*
|
|
50
|
+
* @param agentContext - Agent context
|
|
51
|
+
* @param deviceId - Device identifier
|
|
52
|
+
* @param ctr - Counter value to verify
|
|
53
|
+
* @returns Verification result
|
|
54
|
+
*/
|
|
55
|
+
async verifyCounter(agentContext, deviceId, ctr) {
|
|
56
|
+
const record = await this.counterRepository.findByDeviceId(agentContext, deviceId);
|
|
57
|
+
if (!record) {
|
|
58
|
+
if (ctr <= 0) return {
|
|
59
|
+
valid: false,
|
|
60
|
+
reason: "Counter must be positive",
|
|
61
|
+
lastSeenCounter: 0,
|
|
62
|
+
providedCounter: ctr
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
valid: true,
|
|
66
|
+
lastSeenCounter: 0,
|
|
67
|
+
providedCounter: ctr
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (record.isCounterUsed(ctr)) return {
|
|
71
|
+
valid: false,
|
|
72
|
+
reason: `Counter replay detected: ${ctr} <= ${record.currentCounter}`,
|
|
73
|
+
lastSeenCounter: record.currentCounter,
|
|
74
|
+
providedCounter: ctr
|
|
75
|
+
};
|
|
76
|
+
if (ctr <= record.currentCounter) return {
|
|
77
|
+
valid: false,
|
|
78
|
+
reason: `Counter not monotonic: ${ctr} <= ${record.currentCounter}`,
|
|
79
|
+
lastSeenCounter: record.currentCounter,
|
|
80
|
+
providedCounter: ctr
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
valid: true,
|
|
84
|
+
lastSeenCounter: record.currentCounter,
|
|
85
|
+
providedCounter: ctr
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Verify and update counter
|
|
90
|
+
*
|
|
91
|
+
* CRITICAL: This must be called BEFORE consuming a token.
|
|
92
|
+
* If token consumption fails after this, the counter cannot be rolled back.
|
|
93
|
+
*
|
|
94
|
+
* Steps:
|
|
95
|
+
* 1. Verify counter is valid (ctr > last_seen_ctr)
|
|
96
|
+
* 2. Persist new counter value
|
|
97
|
+
* 3. Mark counter as used
|
|
98
|
+
*
|
|
99
|
+
* @param agentContext - Agent context
|
|
100
|
+
* @param deviceId - Device identifier
|
|
101
|
+
* @param ctr - Counter value to verify and update
|
|
102
|
+
* @returns Verification result
|
|
103
|
+
*/
|
|
104
|
+
async verifyAndUpdateCounter(agentContext, deviceId, ctr) {
|
|
105
|
+
const verificationResult = await this.verifyCounter(agentContext, deviceId, ctr);
|
|
106
|
+
if (!verificationResult.valid) return verificationResult;
|
|
107
|
+
const record = await this.counterRepository.getOrCreate(agentContext, deviceId);
|
|
108
|
+
record.updateCounter(ctr);
|
|
109
|
+
record.pruneOldCounters(100);
|
|
110
|
+
await this.counterRepository.update(agentContext, record);
|
|
111
|
+
return {
|
|
112
|
+
valid: true,
|
|
113
|
+
lastSeenCounter: record.currentCounter,
|
|
114
|
+
providedCounter: ctr
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Mark a counter as used
|
|
119
|
+
*
|
|
120
|
+
* Adds the counter to the used set without updating currentCounter.
|
|
121
|
+
* This can be used for batch processing or out-of-order token consumption.
|
|
122
|
+
*
|
|
123
|
+
* @param agentContext - Agent context
|
|
124
|
+
* @param deviceId - Device identifier
|
|
125
|
+
* @param ctr - Counter value to mark as used
|
|
126
|
+
*/
|
|
127
|
+
async markCounterUsed(agentContext, deviceId, ctr) {
|
|
128
|
+
const record = await this.counterRepository.getOrCreate(agentContext, deviceId);
|
|
129
|
+
record.usedCounters.add(ctr);
|
|
130
|
+
if (ctr > record.currentCounter) record.currentCounter = ctr;
|
|
131
|
+
record.updatedAt = /* @__PURE__ */ new Date();
|
|
132
|
+
await this.counterRepository.update(agentContext, record);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Reset counter for a device
|
|
136
|
+
*
|
|
137
|
+
* WARNING: This is dangerous and should only be used for testing or
|
|
138
|
+
* recovery scenarios. Resetting counters can allow replay attacks.
|
|
139
|
+
*
|
|
140
|
+
* @param agentContext - Agent context
|
|
141
|
+
* @param deviceId - Device identifier
|
|
142
|
+
* @param newCounter - New counter value (default 0)
|
|
143
|
+
*/
|
|
144
|
+
async resetCounter(agentContext, deviceId, newCounter = 0) {
|
|
145
|
+
const record = await this.counterRepository.getOrCreate(agentContext, deviceId);
|
|
146
|
+
record.currentCounter = newCounter;
|
|
147
|
+
record.usedCounters.clear();
|
|
148
|
+
record.updatedAt = /* @__PURE__ */ new Date();
|
|
149
|
+
await this.counterRepository.update(agentContext, record);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get counter statistics for a device
|
|
153
|
+
*
|
|
154
|
+
* Useful for monitoring and debugging.
|
|
155
|
+
*
|
|
156
|
+
* @param agentContext - Agent context
|
|
157
|
+
* @param deviceId - Device identifier
|
|
158
|
+
* @returns Counter statistics
|
|
159
|
+
*/
|
|
160
|
+
async getCounterStats(agentContext, deviceId) {
|
|
161
|
+
const record = await this.counterRepository.findByDeviceId(agentContext, deviceId);
|
|
162
|
+
if (!record) return null;
|
|
163
|
+
return {
|
|
164
|
+
deviceId: record.deviceId,
|
|
165
|
+
currentCounter: record.currentCounter,
|
|
166
|
+
usedCountersCount: record.usedCounters.size,
|
|
167
|
+
createdAt: record.createdAt,
|
|
168
|
+
updatedAt: record.updatedAt
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
CounterService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [CounterRepository])], CounterService);
|
|
173
|
+
|
|
174
|
+
//#endregion
|
|
175
|
+
export { CounterService };
|
|
176
|
+
//# sourceMappingURL=CounterService.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CounterService.mjs","names":[],"sources":["../../src/services/CounterService.ts"],"sourcesContent":["/**\n * Counter Service for Replay Protection\n *\n * Manages monotonic counters for join tokens to prevent replay attacks.\n *\n * Security Requirements (from Signing Protocol spec Lines 1409-1420):\n * - Counter MUST be strictly monotonically increasing per device\n * - Clients MUST persist last_seen_ctr(device) before consuming token\n * - Tokens with ctr <= last_seen_ctr MUST be rejected\n * - Server MUST increment counter for each new token\n *\n * Implementation Notes:\n * - Uses CounterRepository for persistent storage\n * - Thread-safe via repository transactions\n * - Supports counter pruning to prevent unbounded growth\n */\n\nimport type { AgentContext } from '@credo-ts/core'\nimport { injectable } from '@credo-ts/core'\nimport { CounterRepository } from '../repository/CounterRepository'\n\n/**\n * Result of counter verification\n */\nexport interface CounterVerificationResult {\n /**\n * Whether the counter is valid (greater than last seen)\n */\n valid: boolean\n\n /**\n * Reason for failure (if not valid)\n */\n reason?: string\n\n /**\n * Last seen counter value\n */\n lastSeenCounter?: number\n\n /**\n * Provided counter value\n */\n providedCounter?: number\n}\n\n@injectable()\nexport class CounterService {\n public constructor(private counterRepository: CounterRepository) {}\n\n /**\n * Get current counter value for a device\n *\n * Returns the last seen counter value. The next valid counter\n * must be greater than this value.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @returns Current counter value (0 if device not found)\n */\n public async getCounter(agentContext: AgentContext, deviceId: string): Promise<number> {\n const record = await this.counterRepository.findByDeviceId(agentContext, deviceId)\n return record ? record.currentCounter : 0\n }\n\n /**\n * Increment counter for a device\n *\n * Generates the next counter value for creating a new token.\n * This should be called when creating a new join token.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @returns New counter value (currentCounter + 1)\n */\n public async incrementCounter(agentContext: AgentContext, deviceId: string): Promise<number> {\n const record = await this.counterRepository.getOrCreate(agentContext, deviceId)\n\n const newCounter = record.currentCounter + 1\n\n // Update the record\n record.updateCounter(newCounter)\n\n // Prune old counters to prevent unbounded growth\n record.pruneOldCounters(100)\n\n // Save updated record\n await this.counterRepository.update(agentContext, record)\n\n return newCounter\n }\n\n /**\n * Verify a counter value without updating\n *\n * Checks if a counter is valid (greater than last seen) but does NOT\n * persist the counter. Use verifyAndUpdateCounter() to verify and persist.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @param ctr - Counter value to verify\n * @returns Verification result\n */\n public async verifyCounter(\n agentContext: AgentContext,\n deviceId: string,\n ctr: number\n ): Promise<CounterVerificationResult> {\n const record = await this.counterRepository.findByDeviceId(agentContext, deviceId)\n\n // If no record exists, any positive counter is valid\n if (!record) {\n if (ctr <= 0) {\n return {\n valid: false,\n reason: 'Counter must be positive',\n lastSeenCounter: 0,\n providedCounter: ctr,\n }\n }\n\n return {\n valid: true,\n lastSeenCounter: 0,\n providedCounter: ctr,\n }\n }\n\n // Check if counter is used\n if (record.isCounterUsed(ctr)) {\n return {\n valid: false,\n reason: `Counter replay detected: ${ctr} <= ${record.currentCounter}`,\n lastSeenCounter: record.currentCounter,\n providedCounter: ctr,\n }\n }\n\n // Check monotonic increase\n if (ctr <= record.currentCounter) {\n return {\n valid: false,\n reason: `Counter not monotonic: ${ctr} <= ${record.currentCounter}`,\n lastSeenCounter: record.currentCounter,\n providedCounter: ctr,\n }\n }\n\n return {\n valid: true,\n lastSeenCounter: record.currentCounter,\n providedCounter: ctr,\n }\n }\n\n /**\n * Verify and update counter\n *\n * CRITICAL: This must be called BEFORE consuming a token.\n * If token consumption fails after this, the counter cannot be rolled back.\n *\n * Steps:\n * 1. Verify counter is valid (ctr > last_seen_ctr)\n * 2. Persist new counter value\n * 3. Mark counter as used\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @param ctr - Counter value to verify and update\n * @returns Verification result\n */\n public async verifyAndUpdateCounter(\n agentContext: AgentContext,\n deviceId: string,\n ctr: number\n ): Promise<CounterVerificationResult> {\n // First verify\n const verificationResult = await this.verifyCounter(agentContext, deviceId, ctr)\n\n if (!verificationResult.valid) {\n return verificationResult\n }\n\n // Get or create record\n const record = await this.counterRepository.getOrCreate(agentContext, deviceId)\n\n // Update counter\n record.updateCounter(ctr)\n\n // Prune old counters\n record.pruneOldCounters(100)\n\n // CRITICAL: Persist BEFORE token consumption\n await this.counterRepository.update(agentContext, record)\n\n return {\n valid: true,\n lastSeenCounter: record.currentCounter,\n providedCounter: ctr,\n }\n }\n\n /**\n * Mark a counter as used\n *\n * Adds the counter to the used set without updating currentCounter.\n * This can be used for batch processing or out-of-order token consumption.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @param ctr - Counter value to mark as used\n */\n public async markCounterUsed(agentContext: AgentContext, deviceId: string, ctr: number): Promise<void> {\n const record = await this.counterRepository.getOrCreate(agentContext, deviceId)\n\n record.usedCounters.add(ctr)\n\n // Update currentCounter if this is higher\n if (ctr > record.currentCounter) {\n record.currentCounter = ctr\n }\n\n record.updatedAt = new Date()\n\n await this.counterRepository.update(agentContext, record)\n }\n\n /**\n * Reset counter for a device\n *\n * WARNING: This is dangerous and should only be used for testing or\n * recovery scenarios. Resetting counters can allow replay attacks.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @param newCounter - New counter value (default 0)\n */\n public async resetCounter(\n agentContext: AgentContext,\n deviceId: string,\n newCounter = 0\n ): Promise<void> {\n const record = await this.counterRepository.getOrCreate(agentContext, deviceId)\n\n record.currentCounter = newCounter\n record.usedCounters.clear()\n record.updatedAt = new Date()\n\n await this.counterRepository.update(agentContext, record)\n }\n\n /**\n * Get counter statistics for a device\n *\n * Useful for monitoring and debugging.\n *\n * @param agentContext - Agent context\n * @param deviceId - Device identifier\n * @returns Counter statistics\n */\n public async getCounterStats(\n agentContext: AgentContext,\n deviceId: string\n ): Promise<{\n deviceId: string\n currentCounter: number\n usedCountersCount: number\n createdAt: Date | undefined\n updatedAt: Date\n } | null> {\n const record = await this.counterRepository.findByDeviceId(agentContext, deviceId)\n\n if (!record) {\n return null\n }\n\n return {\n deviceId: record.deviceId,\n currentCounter: record.currentCounter,\n usedCountersCount: record.usedCounters.size,\n createdAt: record.createdAt,\n updatedAt: record.updatedAt,\n }\n }\n}\n"],"mappings":";;;;;;;AA+CO,2BAAM,eAAe;CAC1B,AAAO,YAAY,AAAQ,mBAAsC;EAAtC;;;;;;;;;;;;CAY3B,MAAa,WAAW,cAA4B,UAAmC;EACrF,MAAM,SAAS,MAAM,KAAK,kBAAkB,eAAe,cAAc,SAAS;AAClF,SAAO,SAAS,OAAO,iBAAiB;;;;;;;;;;;;CAa1C,MAAa,iBAAiB,cAA4B,UAAmC;EAC3F,MAAM,SAAS,MAAM,KAAK,kBAAkB,YAAY,cAAc,SAAS;EAE/E,MAAM,aAAa,OAAO,iBAAiB;AAG3C,SAAO,cAAc,WAAW;AAGhC,SAAO,iBAAiB,IAAI;AAG5B,QAAM,KAAK,kBAAkB,OAAO,cAAc,OAAO;AAEzD,SAAO;;;;;;;;;;;;;CAcT,MAAa,cACX,cACA,UACA,KACoC;EACpC,MAAM,SAAS,MAAM,KAAK,kBAAkB,eAAe,cAAc,SAAS;AAGlF,MAAI,CAAC,QAAQ;AACX,OAAI,OAAO,EACT,QAAO;IACL,OAAO;IACP,QAAQ;IACR,iBAAiB;IACjB,iBAAiB;IAClB;AAGH,UAAO;IACL,OAAO;IACP,iBAAiB;IACjB,iBAAiB;IAClB;;AAIH,MAAI,OAAO,cAAc,IAAI,CAC3B,QAAO;GACL,OAAO;GACP,QAAQ,4BAA4B,IAAI,MAAM,OAAO;GACrD,iBAAiB,OAAO;GACxB,iBAAiB;GAClB;AAIH,MAAI,OAAO,OAAO,eAChB,QAAO;GACL,OAAO;GACP,QAAQ,0BAA0B,IAAI,MAAM,OAAO;GACnD,iBAAiB,OAAO;GACxB,iBAAiB;GAClB;AAGH,SAAO;GACL,OAAO;GACP,iBAAiB,OAAO;GACxB,iBAAiB;GAClB;;;;;;;;;;;;;;;;;;CAmBH,MAAa,uBACX,cACA,UACA,KACoC;EAEpC,MAAM,qBAAqB,MAAM,KAAK,cAAc,cAAc,UAAU,IAAI;AAEhF,MAAI,CAAC,mBAAmB,MACtB,QAAO;EAIT,MAAM,SAAS,MAAM,KAAK,kBAAkB,YAAY,cAAc,SAAS;AAG/E,SAAO,cAAc,IAAI;AAGzB,SAAO,iBAAiB,IAAI;AAG5B,QAAM,KAAK,kBAAkB,OAAO,cAAc,OAAO;AAEzD,SAAO;GACL,OAAO;GACP,iBAAiB,OAAO;GACxB,iBAAiB;GAClB;;;;;;;;;;;;CAaH,MAAa,gBAAgB,cAA4B,UAAkB,KAA4B;EACrG,MAAM,SAAS,MAAM,KAAK,kBAAkB,YAAY,cAAc,SAAS;AAE/E,SAAO,aAAa,IAAI,IAAI;AAG5B,MAAI,MAAM,OAAO,eACf,QAAO,iBAAiB;AAG1B,SAAO,4BAAY,IAAI,MAAM;AAE7B,QAAM,KAAK,kBAAkB,OAAO,cAAc,OAAO;;;;;;;;;;;;CAa3D,MAAa,aACX,cACA,UACA,aAAa,GACE;EACf,MAAM,SAAS,MAAM,KAAK,kBAAkB,YAAY,cAAc,SAAS;AAE/E,SAAO,iBAAiB;AACxB,SAAO,aAAa,OAAO;AAC3B,SAAO,4BAAY,IAAI,MAAM;AAE7B,QAAM,KAAK,kBAAkB,OAAO,cAAc,OAAO;;;;;;;;;;;CAY3D,MAAa,gBACX,cACA,UAOQ;EACR,MAAM,SAAS,MAAM,KAAK,kBAAkB,eAAe,cAAc,SAAS;AAElF,MAAI,CAAC,OACH,QAAO;AAGT,SAAO;GACL,UAAU,OAAO;GACjB,gBAAgB,OAAO;GACvB,mBAAmB,OAAO,aAAa;GACvC,WAAW,OAAO;GAClB,WAAW,OAAO;GACnB;;;6BA5OJ,YAAY"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
//#region src/services/HPKEService.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* HPKE Service - Hybrid Public Key Encryption
|
|
4
|
+
*
|
|
5
|
+
* Implements RFC 9180 HPKE for sealed secret delivery in join tokens.
|
|
6
|
+
*
|
|
7
|
+
* Spec Requirements (Lines 759-799):
|
|
8
|
+
* - KEM: DHKEM-X25519-HKDF-SHA256 (MUST)
|
|
9
|
+
* - KDF: HKDF-SHA256 (MUST)
|
|
10
|
+
* - AEAD: AES-256-GCM or ChaCha20-Poly1305 (MUST support ≥1)
|
|
11
|
+
* - AAD: MUST include ticket_digest to bind envelope to token
|
|
12
|
+
*
|
|
13
|
+
* Security Properties:
|
|
14
|
+
* - Confidentiality: Payload encrypted with ephemeral key
|
|
15
|
+
* - Authenticity: AAD binds envelope to specific token
|
|
16
|
+
* - Forward secrecy: Ephemeral keys used for each encryption
|
|
17
|
+
* - Device binding: Sealed to specific recipient public key
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* HPKE encryption result
|
|
21
|
+
*/
|
|
22
|
+
interface HPKEEncryptResult {
|
|
23
|
+
/**
|
|
24
|
+
* Encrypted payload (ciphertext)
|
|
25
|
+
*/
|
|
26
|
+
ciphertext: Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Ephemeral public key (encapsulated key)
|
|
29
|
+
*/
|
|
30
|
+
ephemeralPublicKey: Uint8Array;
|
|
31
|
+
/**
|
|
32
|
+
* Encryption metadata
|
|
33
|
+
*/
|
|
34
|
+
metadata: {
|
|
35
|
+
kem: string;
|
|
36
|
+
kdf: string;
|
|
37
|
+
aead: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* HPKE decryption result
|
|
42
|
+
*/
|
|
43
|
+
interface HPKEDecryptResult {
|
|
44
|
+
/**
|
|
45
|
+
* Decrypted payload (plaintext)
|
|
46
|
+
*/
|
|
47
|
+
plaintext: Uint8Array;
|
|
48
|
+
}
|
|
49
|
+
declare class HPKEService {
|
|
50
|
+
/**
|
|
51
|
+
* Get CipherSuite for HPKE operations
|
|
52
|
+
*
|
|
53
|
+
* Uses the default configuration per spec:
|
|
54
|
+
* - KEM: DHKEM(P-256, HKDF-SHA256) (using P-256 as X25519 requires extension)
|
|
55
|
+
* - KDF: HKDF-SHA256
|
|
56
|
+
* - AEAD: AES-256-GCM
|
|
57
|
+
*/
|
|
58
|
+
private getCipherSuite;
|
|
59
|
+
/**
|
|
60
|
+
* Get algorithm names for metadata
|
|
61
|
+
*/
|
|
62
|
+
private getAlgorithmNames;
|
|
63
|
+
/**
|
|
64
|
+
* Compute ticket digest for AAD binding
|
|
65
|
+
*
|
|
66
|
+
* Per spec: AAD MUST include sha-256(serialize(token))
|
|
67
|
+
*
|
|
68
|
+
* @param ticketBytes - Serialized ticket bytes
|
|
69
|
+
* @returns Base64-encoded SHA-256 digest
|
|
70
|
+
*/
|
|
71
|
+
computeTicketDigest(ticketBytes: Uint8Array): string;
|
|
72
|
+
/**
|
|
73
|
+
* Serialize AAD for HPKE encryption
|
|
74
|
+
*
|
|
75
|
+
* Creates the Additional Authenticated Data that cryptographically
|
|
76
|
+
* binds the envelope to the specific token.
|
|
77
|
+
*
|
|
78
|
+
* @param aad - AAD object with ticket_digest
|
|
79
|
+
* @returns Serialized AAD bytes
|
|
80
|
+
*/
|
|
81
|
+
private serializeAAD;
|
|
82
|
+
/**
|
|
83
|
+
* Encrypt payload with HPKE
|
|
84
|
+
*
|
|
85
|
+
* Uses HPKE Base mode to seal a payload to a recipient's public key.
|
|
86
|
+
* The AAD cryptographically binds the envelope to the token digest.
|
|
87
|
+
*
|
|
88
|
+
* @param recipientPublicKey - Recipient's public key (raw bytes)
|
|
89
|
+
* @param plaintext - Payload to encrypt
|
|
90
|
+
* @param aad - Additional Authenticated Data (includes ticket_digest)
|
|
91
|
+
* @param config - Optional HPKE configuration
|
|
92
|
+
* @returns Encryption result with ciphertext and ephemeral key
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const result = await hpkeService.encrypt(
|
|
97
|
+
* alicePublicKey,
|
|
98
|
+
* Buffer.from('secret payload'),
|
|
99
|
+
* {
|
|
100
|
+
* ticket_digest: 'sha-256:abc123...',
|
|
101
|
+
* session_id: 'sess_xyz',
|
|
102
|
+
* device: 'did:ex:alice#device1'
|
|
103
|
+
* }
|
|
104
|
+
* )
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
encrypt(recipientPublicKey: Uint8Array, plaintext: Uint8Array, aad: {
|
|
108
|
+
ticket_digest: string;
|
|
109
|
+
session_id?: string;
|
|
110
|
+
device?: string;
|
|
111
|
+
}): Promise<HPKEEncryptResult>;
|
|
112
|
+
/**
|
|
113
|
+
* Decrypt payload with HPKE
|
|
114
|
+
*
|
|
115
|
+
* Uses HPKE Base mode to open a sealed envelope using the recipient's
|
|
116
|
+
* private key. Verifies AAD binding to prevent envelope transplant attacks.
|
|
117
|
+
*
|
|
118
|
+
* @param recipientPrivateKey - Recipient's private key (raw bytes)
|
|
119
|
+
* @param ciphertext - Encrypted payload
|
|
120
|
+
* @param ephemeralPublicKey - Ephemeral public key from encryption
|
|
121
|
+
* @param aad - Additional Authenticated Data (must match encryption AAD)
|
|
122
|
+
* @param config - Optional HPKE configuration
|
|
123
|
+
* @returns Decryption result with plaintext
|
|
124
|
+
*
|
|
125
|
+
* @throws Error if AAD doesn't match or decryption fails
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const result = await hpkeService.decrypt(
|
|
130
|
+
* alicePrivateKey,
|
|
131
|
+
* encryptedData,
|
|
132
|
+
* ephemeralKey,
|
|
133
|
+
* {
|
|
134
|
+
* ticket_digest: 'sha-256:abc123...',
|
|
135
|
+
* session_id: 'sess_xyz',
|
|
136
|
+
* device: 'did:ex:alice#device1'
|
|
137
|
+
* }
|
|
138
|
+
* )
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
decrypt(recipientPrivateKey: Uint8Array, ciphertext: Uint8Array, ephemeralPublicKey: Uint8Array, aad: {
|
|
142
|
+
ticket_digest: string;
|
|
143
|
+
session_id?: string;
|
|
144
|
+
device?: string;
|
|
145
|
+
}): Promise<HPKEDecryptResult>;
|
|
146
|
+
/**
|
|
147
|
+
* Generate key pair for HPKE
|
|
148
|
+
*
|
|
149
|
+
* Generates a new random key pair suitable for HPKE operations.
|
|
150
|
+
*
|
|
151
|
+
* @returns Object with public and private keys
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* const { publicKey, privateKey } = await hpkeService.generateKeyPair()
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
generateKeyPair(): Promise<{
|
|
159
|
+
publicKey: Uint8Array;
|
|
160
|
+
privateKey: Uint8Array;
|
|
161
|
+
}>;
|
|
162
|
+
/**
|
|
163
|
+
* Derive public key from private key
|
|
164
|
+
*
|
|
165
|
+
* Derives the corresponding public key from a private key.
|
|
166
|
+
*
|
|
167
|
+
* @param privateKey - Private key
|
|
168
|
+
* @returns Public key
|
|
169
|
+
*/
|
|
170
|
+
derivePublicKey(privateKey: Uint8Array): Promise<Uint8Array>;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
export { HPKEDecryptResult, HPKEEncryptResult, HPKEService };
|
|
174
|
+
//# sourceMappingURL=HPKEService.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HPKEService.d.mts","names":[],"sources":["../../src/services/HPKEService.ts"],"mappings":";;AAyBA;;;;;;;;;;;;;;AAwBA;;;;;UAxBiB,iBAAA;EAgCO;;;EA5BtB,UAAA,EAAY,UAAA;EAmHC;;;EA9Gb,kBAAA,EAAoB,UAAA;EAuKN;;;EAlKd,QAAA;IACE,GAAA;IACA,GAAA;IACA,IAAA;EAAA;AAAA;;;;UAOa,iBAAA;EA4BP;;;EAxBR,SAAA,EAAW,UAAA;AAAA;AAAA,cAIA,WAAA;EAsFW;;;;;;;;EAAA,QA7Ed,cAAA;EAgFG;;;EAAA,QArEH,iBAAA;EA4HM;;;;;;;;EAxGP,mBAAA,CAAoB,WAAA,EAAa,UAAA;EA2G7B;;;;;;;;;EAAA,QA7FH,YAAA;EA2J8C;;;;;;;;;;;;;;;;;;;;;;;;;EA5HzC,OAAA,CACX,kBAAA,EAAoB,UAAA,EACpB,SAAA,EAAW,UAAA,EACX,GAAA;IAAO,aAAA;IAAuB,UAAA;IAAqB,MAAA;EAAA,IAClD,OAAA,CAAQ,iBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqDE,OAAA,CACX,mBAAA,EAAqB,UAAA,EACrB,UAAA,EAAY,UAAA,EACZ,kBAAA,EAAoB,UAAA,EACpB,GAAA;IAAO,aAAA;IAAuB,UAAA;IAAqB,MAAA;EAAA,IAClD,OAAA,CAAQ,iBAAA;;;;;;;;;;;;;EAqCE,eAAA,CAAA,GAAmB,OAAA;IAC9B,SAAA,EAAW,UAAA;IACX,UAAA,EAAY,UAAA;EAAA;;;;;;;;;EAuBD,eAAA,CAAgB,UAAA,EAAY,UAAA,GAAa,OAAA,CAAQ,UAAA;AAAA"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { __decorate } from "../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs";
|
|
2
|
+
import { injectable } from "@credo-ts/core";
|
|
3
|
+
import { Aes256Gcm, CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
5
|
+
|
|
6
|
+
//#region src/services/HPKEService.ts
|
|
7
|
+
/**
|
|
8
|
+
* HPKE Service - Hybrid Public Key Encryption
|
|
9
|
+
*
|
|
10
|
+
* Implements RFC 9180 HPKE for sealed secret delivery in join tokens.
|
|
11
|
+
*
|
|
12
|
+
* Spec Requirements (Lines 759-799):
|
|
13
|
+
* - KEM: DHKEM-X25519-HKDF-SHA256 (MUST)
|
|
14
|
+
* - KDF: HKDF-SHA256 (MUST)
|
|
15
|
+
* - AEAD: AES-256-GCM or ChaCha20-Poly1305 (MUST support ≥1)
|
|
16
|
+
* - AAD: MUST include ticket_digest to bind envelope to token
|
|
17
|
+
*
|
|
18
|
+
* Security Properties:
|
|
19
|
+
* - Confidentiality: Payload encrypted with ephemeral key
|
|
20
|
+
* - Authenticity: AAD binds envelope to specific token
|
|
21
|
+
* - Forward secrecy: Ephemeral keys used for each encryption
|
|
22
|
+
* - Device binding: Sealed to specific recipient public key
|
|
23
|
+
*/
|
|
24
|
+
let HPKEService = class HPKEService {
|
|
25
|
+
/**
|
|
26
|
+
* Get CipherSuite for HPKE operations
|
|
27
|
+
*
|
|
28
|
+
* Uses the default configuration per spec:
|
|
29
|
+
* - KEM: DHKEM(P-256, HKDF-SHA256) (using P-256 as X25519 requires extension)
|
|
30
|
+
* - KDF: HKDF-SHA256
|
|
31
|
+
* - AEAD: AES-256-GCM
|
|
32
|
+
*/
|
|
33
|
+
getCipherSuite() {
|
|
34
|
+
return new CipherSuite({
|
|
35
|
+
kem: new DhkemP256HkdfSha256(),
|
|
36
|
+
kdf: new HkdfSha256(),
|
|
37
|
+
aead: new Aes256Gcm()
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get algorithm names for metadata
|
|
42
|
+
*/
|
|
43
|
+
getAlgorithmNames() {
|
|
44
|
+
return {
|
|
45
|
+
kem: "DHKEM-P256-HKDF-SHA256",
|
|
46
|
+
kdf: "HKDF-SHA256",
|
|
47
|
+
aead: "AES-256-GCM"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Compute ticket digest for AAD binding
|
|
52
|
+
*
|
|
53
|
+
* Per spec: AAD MUST include sha-256(serialize(token))
|
|
54
|
+
*
|
|
55
|
+
* @param ticketBytes - Serialized ticket bytes
|
|
56
|
+
* @returns Base64-encoded SHA-256 digest
|
|
57
|
+
*/
|
|
58
|
+
computeTicketDigest(ticketBytes) {
|
|
59
|
+
const hash = sha256(ticketBytes);
|
|
60
|
+
return Buffer.from(hash).toString("base64");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Serialize AAD for HPKE encryption
|
|
64
|
+
*
|
|
65
|
+
* Creates the Additional Authenticated Data that cryptographically
|
|
66
|
+
* binds the envelope to the specific token.
|
|
67
|
+
*
|
|
68
|
+
* @param aad - AAD object with ticket_digest
|
|
69
|
+
* @returns Serialized AAD bytes
|
|
70
|
+
*/
|
|
71
|
+
serializeAAD(aad) {
|
|
72
|
+
const aadString = JSON.stringify(aad);
|
|
73
|
+
return new TextEncoder().encode(aadString);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Encrypt payload with HPKE
|
|
77
|
+
*
|
|
78
|
+
* Uses HPKE Base mode to seal a payload to a recipient's public key.
|
|
79
|
+
* The AAD cryptographically binds the envelope to the token digest.
|
|
80
|
+
*
|
|
81
|
+
* @param recipientPublicKey - Recipient's public key (raw bytes)
|
|
82
|
+
* @param plaintext - Payload to encrypt
|
|
83
|
+
* @param aad - Additional Authenticated Data (includes ticket_digest)
|
|
84
|
+
* @param config - Optional HPKE configuration
|
|
85
|
+
* @returns Encryption result with ciphertext and ephemeral key
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const result = await hpkeService.encrypt(
|
|
90
|
+
* alicePublicKey,
|
|
91
|
+
* Buffer.from('secret payload'),
|
|
92
|
+
* {
|
|
93
|
+
* ticket_digest: 'sha-256:abc123...',
|
|
94
|
+
* session_id: 'sess_xyz',
|
|
95
|
+
* device: 'did:ex:alice#device1'
|
|
96
|
+
* }
|
|
97
|
+
* )
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
async encrypt(recipientPublicKey, plaintext, aad) {
|
|
101
|
+
const suite = this.getCipherSuite();
|
|
102
|
+
const aadBytes = this.serializeAAD(aad);
|
|
103
|
+
const pkR = await suite.kem.importKey("raw", recipientPublicKey, true);
|
|
104
|
+
const sender = await suite.createSenderContext({ recipientPublicKey: pkR });
|
|
105
|
+
return {
|
|
106
|
+
ciphertext: new Uint8Array(await sender.seal(plaintext, aadBytes)),
|
|
107
|
+
ephemeralPublicKey: new Uint8Array(sender.enc),
|
|
108
|
+
metadata: this.getAlgorithmNames()
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Decrypt payload with HPKE
|
|
113
|
+
*
|
|
114
|
+
* Uses HPKE Base mode to open a sealed envelope using the recipient's
|
|
115
|
+
* private key. Verifies AAD binding to prevent envelope transplant attacks.
|
|
116
|
+
*
|
|
117
|
+
* @param recipientPrivateKey - Recipient's private key (raw bytes)
|
|
118
|
+
* @param ciphertext - Encrypted payload
|
|
119
|
+
* @param ephemeralPublicKey - Ephemeral public key from encryption
|
|
120
|
+
* @param aad - Additional Authenticated Data (must match encryption AAD)
|
|
121
|
+
* @param config - Optional HPKE configuration
|
|
122
|
+
* @returns Decryption result with plaintext
|
|
123
|
+
*
|
|
124
|
+
* @throws Error if AAD doesn't match or decryption fails
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const result = await hpkeService.decrypt(
|
|
129
|
+
* alicePrivateKey,
|
|
130
|
+
* encryptedData,
|
|
131
|
+
* ephemeralKey,
|
|
132
|
+
* {
|
|
133
|
+
* ticket_digest: 'sha-256:abc123...',
|
|
134
|
+
* session_id: 'sess_xyz',
|
|
135
|
+
* device: 'did:ex:alice#device1'
|
|
136
|
+
* }
|
|
137
|
+
* )
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
async decrypt(recipientPrivateKey, ciphertext, ephemeralPublicKey, aad) {
|
|
141
|
+
const suite = this.getCipherSuite();
|
|
142
|
+
const aadBytes = this.serializeAAD(aad);
|
|
143
|
+
const skR = await suite.kem.importKey("raw", recipientPrivateKey, false);
|
|
144
|
+
try {
|
|
145
|
+
const recipient = await suite.createRecipientContext({
|
|
146
|
+
recipientKey: skR,
|
|
147
|
+
enc: ephemeralPublicKey
|
|
148
|
+
});
|
|
149
|
+
return { plaintext: new Uint8Array(await recipient.open(ciphertext, aadBytes)) };
|
|
150
|
+
} catch (error) {
|
|
151
|
+
throw new Error(`HPKE decryption failed: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Generate key pair for HPKE
|
|
156
|
+
*
|
|
157
|
+
* Generates a new random key pair suitable for HPKE operations.
|
|
158
|
+
*
|
|
159
|
+
* @returns Object with public and private keys
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const { publicKey, privateKey } = await hpkeService.generateKeyPair()
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
async generateKeyPair() {
|
|
167
|
+
const suite = this.getCipherSuite();
|
|
168
|
+
const keyPair = await suite.kem.generateKeyPair();
|
|
169
|
+
const publicKeyRaw = await suite.kem.serializePublicKey(keyPair.publicKey);
|
|
170
|
+
const privateKeyRaw = await suite.kem.serializePrivateKey(keyPair.privateKey);
|
|
171
|
+
return {
|
|
172
|
+
publicKey: new Uint8Array(publicKeyRaw),
|
|
173
|
+
privateKey: new Uint8Array(privateKeyRaw)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Derive public key from private key
|
|
178
|
+
*
|
|
179
|
+
* Derives the corresponding public key from a private key.
|
|
180
|
+
*
|
|
181
|
+
* @param privateKey - Private key
|
|
182
|
+
* @returns Public key
|
|
183
|
+
*/
|
|
184
|
+
async derivePublicKey(privateKey) {
|
|
185
|
+
const suite = this.getCipherSuite();
|
|
186
|
+
const keyPair = await suite.kem.importKey("raw", privateKey, false);
|
|
187
|
+
const publicKeyRaw = await suite.kem.serializePublicKey(keyPair.publicKey);
|
|
188
|
+
return new Uint8Array(publicKeyRaw);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
HPKEService = __decorate([injectable()], HPKEService);
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
export { HPKEService };
|
|
195
|
+
//# sourceMappingURL=HPKEService.mjs.map
|