Skip to content

Commit 06c9a4a

Browse files
authored
Merge pull request #34 from datachainlab/fix-key-expiration-calculation
Fix to use `output.validity.not_after + 1` as key's `expiredAt` if the client state's key expiration is zero Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents 6982290 + 01a2600 commit 06c9a4a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

contracts/LCPClientZKDCAPBase.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,16 @@ abstract contract LCPClientZKDCAPBase is LCPClientBase {
191191
}
192192

193193
// calculate the expiration time of the enclave key
194+
uint64 maxExpiredAt = output.validityNotAfter + 1;
194195
uint64 expiredAt;
195196
if (clientState.key_expiration == 0) {
196-
// If the value is 0, the validity period of the EK is `qv_output.validity.not_after`.
197-
expiredAt = output.validityNotAfter;
197+
// If the value is 0, the validity period of the EK is `output.validity.not_after` + 1.
198+
expiredAt = maxExpiredAt;
198199
} else {
199-
// If the value is greater than 0, the validity period of the EK is min(`output.validty.not_before + key_expiration`, `output.validity.not_after`).
200+
// If the value is greater than 0, the validity period of the EK is min(`output.validity.not_before + key_expiration`, `output.validity.not_after` + 1).
200201
expiredAt = output.validityNotBefore + clientState.key_expiration;
201-
if (expiredAt > output.validityNotAfter) {
202-
expiredAt = output.validityNotAfter;
202+
if (expiredAt > maxExpiredAt) {
203+
expiredAt = maxExpiredAt;
203204
}
204205
if (expiredAt <= block.timestamp) {
205206
revert LCPClientEnclaveKeyExpired();

test/LCPClientZKDCAPTest.t.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ contract LCPClientZKDCAPTest is BasicTest {
5151
// warp to the time of `output.validityNotBefore`
5252
vm.warp(output.validityNotBefore);
5353
lc.zkDCAPRegisterEnclaveKey(clientId, registerEnclaveKeyMessage(output));
54-
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter);
54+
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter + 1);
5555

5656
// if `validityNotBefore` is in the future, it should fail
5757
output = ZKDCAPTestHelper.qvOutput();
@@ -72,7 +72,7 @@ contract LCPClientZKDCAPTest is BasicTest {
7272
output.validityNotAfter = uint64(block.timestamp);
7373
output.enclaveKey = address(2);
7474
lc.zkDCAPRegisterEnclaveKey(clientId, registerEnclaveKeyMessage(output));
75-
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter);
75+
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter + 1);
7676
}
7777

7878
function testRegisterEnclaveKeyNotSetGracePeriod() public {
@@ -300,11 +300,11 @@ contract LCPClientZKDCAPTest is BasicTest {
300300
DCAPValidator.Output memory output;
301301

302302
// if `key_expiration` is 0 and the current time is within the validity period, it should succeed
303-
// and the key expiration should be set to `validityNotAfter`
303+
// and the key expiration should be set to `validityNotAfter` + 1
304304
output = ZKDCAPTestHelper.qvOutput();
305305
vm.warp(output.validityNotBefore);
306306
lc.zkDCAPRegisterEnclaveKey(clientId, registerEnclaveKeyMessage(output));
307-
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter);
307+
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter + 1);
308308
}
309309

310310
function testRegisterEnclaveKeySetKeyExpiration() public {
@@ -356,7 +356,7 @@ contract LCPClientZKDCAPTest is BasicTest {
356356
output.enclaveKey = address(3);
357357
output.validityNotAfter = output.validityNotBefore + clientState.key_expiration - 1;
358358
lc.zkDCAPRegisterEnclaveKey(clientId, registerEnclaveKeyMessage(output));
359-
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter);
359+
assertEq(lc.getEKInfo(clientId, output.enclaveKey).expiredAt, output.validityNotAfter + 1);
360360
}
361361

362362
function testRegisterEnclaveKeyInvalidZkvmType() public {

0 commit comments

Comments
 (0)