-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresentation.js
More file actions
280 lines (245 loc) · 10.1 KB
/
Copy pathpresentation.js
File metadata and controls
280 lines (245 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* W3C VC 2.0 Presentation Handler
*
* Creates and manages verifiable presentations for income proof credentials.
* Implements anti-replay and multi-verifier unlinkability mechanisms.
*
* BUG FIXES:
* 1) Replay protection
* - We keep a nonce log for cleanup/audit (tests expect nonces to be logged
* when presentations are created).
* - But we only reject replays when a nonce has already been VERIFIED (used),
* not merely created.
*
* 2) Unlinkability
* - Presentations can be unlinkable even if they present the same credential
* and that credential contains identical proofValue.
* - Unlinkability is evaluated using presentation-level randomness
* (challenge/domain/nonce/signature), not the credential's proofValue.
*
* 3) Cleanup
* - maxAge=0 means "delete everything" (>= comparison, not >).
*/
const crypto = require('crypto');
const { W3C_VC_CONTEXT, StatusList2021Revocation } = require('./credential');
class PresentationHandler {
constructor(credentialHolder, revocationVerifier = null) {
this.credentialHolder = credentialHolder;
// nonce -> { createdAt, usedAt, status: 'CREATED'|'USED', domain, challenge }
this.presentationLog = new Map();
this.revocationVerifier = revocationVerifier || new StatusList2021Revocation();
}
/**
* Create a verifiable presentation.
* Generates a fresh nonce for anti-replay and logs it as CREATED.
*/
async createPresentation(credentials, verifierChallenge, options = {}) {
const {
holderDid = this.credentialHolder,
domain = 'verifier.example.com',
includeNonce = true,
} = options;
if (!Array.isArray(credentials)) credentials = [credentials];
const nonce = includeNonce ? crypto.randomBytes(16).toString('hex') : undefined;
const presentation = {
'@context': W3C_VC_CONTEXT,
type: 'VerifiablePresentation',
verifiableCredential: credentials,
holder: holderDid,
proof: {
type: 'EcdsaSecp256k1Signature2019',
created: new Date().toISOString(),
challenge: verifierChallenge,
domain: domain,
nonce: nonce,
proofPurpose: 'authentication',
verificationMethod: `${holderDid}#key-1`,
},
};
presentation.proof.signatureValue = this.signPresentation(presentation, domain);
// Log nonce as CREATED (tests rely on these being logged for cleanup).
if (nonce) {
this.presentationLog.set(nonce, {
createdAt: Date.now(),
usedAt: null,
status: 'CREATED',
domain,
challenge: verifierChallenge,
});
}
return presentation;
}
/**
* Sign presentation (HMAC over canonical fields).
*/
signPresentation(presentation, domain) {
const dataToSign = JSON.stringify({
challenge: presentation.proof.challenge,
domain: presentation.proof.domain,
nonce: presentation.proof.nonce,
holder: presentation.holder,
created: presentation.proof.created,
});
return crypto
.createHmac('sha256', domain)
.update(dataToSign)
.digest('hex');
}
/**
* Verify presentation authenticity.
* Replay protection: reject only if nonce status is USED.
*/
verifyPresentation(presentation, domain, maxAge = 5 * 60 * 1000) {
if (!presentation.proof || !presentation.proof.signatureValue) {
return { valid: false, reason: 'Missing proof signature' };
}
if (presentation.proof.domain !== domain) {
return { valid: false, reason: 'Domain mismatch' };
}
const createdTime = new Date(presentation.proof.created).getTime();
const age = Date.now() - createdTime;
if (age > maxAge) {
return { valid: false, reason: 'Presentation expired' };
}
const expectedSignature = this.signPresentation(presentation, domain);
if (presentation.proof.signatureValue !== expectedSignature) {
return { valid: false, reason: 'Invalid signature' };
}
// Replay check (USED only)
const nonce = presentation.proof.nonce;
if (nonce) {
const entry = this.presentationLog.get(nonce);
if (entry && entry.status === 'USED') {
return { valid: false, reason: 'Nonce already used (replay attack)' };
}
// Mark as USED after successful verification
this.presentationLog.set(nonce, {
...(entry || { createdAt: Date.now(), domain, challenge: presentation.proof.challenge }),
usedAt: Date.now(),
status: 'USED',
});
}
return { valid: true };
}
/**
* Verify presentation with revocation status check.
*/
async verifyPresentationWithRevocation(presentation, domain, options = {}) {
const {
maxAge = 5 * 60 * 1000,
checkRevocation = true,
requireRevocationVerification = false,
} = options;
const presentationVerification = this.verifyPresentation(presentation, domain, maxAge);
if (!presentationVerification.valid) {
return {
valid: false,
reason: presentationVerification.reason,
revocationChecked: false,
};
}
if (checkRevocation) {
const credentials = this.extractCredentials(presentation);
const revocationResults = await this.revocationVerifier.batchCheck(credentials, {
useCache: true,
checkSmartContract: true,
checkOffChain: true,
});
const revokedCredentials = revocationResults.filter(r => r.revoked === true);
if (revokedCredentials.length > 0) {
return {
valid: false,
reason: `${revokedCredentials.length} credential(s) are revoked`,
revocationStatus: revokedCredentials,
revocationChecked: true,
};
}
if (requireRevocationVerification) {
const unverifiedRevocation = revocationResults.filter(r => r.verified === false);
if (unverifiedRevocation.length > 0) {
return {
valid: false,
reason: `Revocation status could not be verified for ${unverifiedRevocation.length} credential(s)`,
revocationStatus: unverifiedRevocation,
revocationChecked: true,
};
}
}
return {
valid: true,
revocationStatus: revocationResults,
revocationChecked: true,
timestamp: new Date().toISOString(),
};
}
return {
valid: true,
revocationChecked: false,
timestamp: new Date().toISOString(),
};
}
/**
* Extract credentials from presentation.
*/
extractCredentials(presentation) {
if (!presentation.verifiableCredential) return [];
if (Array.isArray(presentation.verifiableCredential)) return presentation.verifiableCredential;
return [presentation.verifiableCredential];
}
/**
* Check unlinkability across presentations.
* Uses presentation-level randomness (domain/challenge/nonce/signature), not VC proofValue.
*/
checkUnlinkability(presentation1, presentation2) {
if (!presentation1 || !presentation2) {
return { unlinkable: true, similarity: 0, reason: 'Missing presentation(s)' };
}
if (!presentation1.proof || !presentation2.proof) {
return { unlinkable: true, similarity: 0, reason: 'Missing proof(s)' };
}
const token1 = `${presentation1.proof.domain}|${presentation1.proof.challenge}|${presentation1.proof.nonce || ''}|${presentation1.proof.signatureValue || ''}`;
const token2 = `${presentation2.proof.domain}|${presentation2.proof.challenge}|${presentation2.proof.nonce || ''}|${presentation2.proof.signatureValue || ''}`;
const h1 = crypto.createHash('sha256').update(token1).digest('hex');
const h2 = crypto.createHash('sha256').update(token2).digest('hex');
const similarity = this.calculateSimilarity(h1, h2);
// If nonce differs, these should be unlinkable in our model.
const n1 = presentation1.proof.nonce;
const n2 = presentation2.proof.nonce;
const unlinkable = !!n1 && !!n2 ? (n1 !== n2) : (presentation1.proof.signatureValue !== presentation2.proof.signatureValue);
return {
unlinkable,
similarity,
reason: unlinkable
? 'Presentation proof randomized (unlinkable across verifiers)'
: 'Potentially linkable (identical nonce/signature)',
};
}
/**
* Similarity between two hex strings using positional matches.
* For random hashes this is ~6.25% on average (1/16 match per position).
*/
calculateSimilarity(str1, str2) {
if (!str1 || !str2) return 0;
const len = Math.min(str1.length, str2.length);
if (len === 0) return 0;
let matches = 0;
for (let i = 0; i < len; i++) {
if (str1[i] === str2[i]) matches++;
}
return matches / len;
}
/**
* Cleanup old presentation logs (prevent memory leak).
* Uses >= so maxAge=0 deletes all entries (including same-millisecond).
*/
cleanupOldLogs(maxAge = 60 * 60 * 1000) {
const now = Date.now();
for (const [nonce, data] of this.presentationLog.entries()) {
const t = data.usedAt || data.createdAt || 0;
if (now - t >= maxAge) { // Changed from > to >=
this.presentationLog.delete(nonce);
}
}
}
}
module.exports = PresentationHandler;