|
1 | 1 | import { |
2 | 2 | decryptAndVerifySignedData, |
3 | 3 | encryptAndDetachSignData, |
| 4 | + verifySignedData, |
4 | 5 | SIGNATURE_DATE_TOLERANCE_MS, |
5 | 6 | } from '../../../../src/tss/ecdsa-dkls/commsLayer'; |
6 | 7 | import * as openpgp from 'openpgp'; |
@@ -100,32 +101,67 @@ describe('DKLS Communication Layer', function () { |
100 | 101 | }); |
101 | 102 |
|
102 | 103 | describe('signature date tolerance', function () { |
103 | | - // Key that expires in 1 second — well within the 24-hour tolerance window, |
104 | | - // so encrypt at Date.now() + 24h will see an expired key. |
105 | | - let shortLivedRecipientKey: { publicKey: string; privateKey: string }; |
106 | | - |
107 | | - before(async function () { |
108 | | - shortLivedRecipientKey = await openpgp.generateKey({ |
109 | | - userIDs: [{ name: 'shortlived', email: 'shortlived@username.com' }], |
110 | | - curve: 'secp256k1', |
111 | | - keyExpirationTime: 1, |
112 | | - }); |
| 104 | + it('should confirm tolerance constant is 24 hours', function () { |
| 105 | + SIGNATURE_DATE_TOLERANCE_MS.should.equal(24 * 60 * 60 * 1000); |
113 | 106 | }); |
114 | 107 |
|
115 | 108 | it('should reject encryption to an expired key', async function () { |
116 | | - const text = 'ffffffff'; |
| 109 | + // Key created 48h ago with a 23h lifetime → expired 25h ago. |
| 110 | + const expiredKey = await openpgp.generateKey({ |
| 111 | + userIDs: [{ name: 'expired', email: 'expired@username.com' }], |
| 112 | + curve: 'secp256k1', |
| 113 | + date: new Date(Date.now() - 48 * 60 * 60 * 1000), |
| 114 | + keyExpirationTime: 23 * 3600, |
| 115 | + }); |
117 | 116 |
|
118 | | - // Encryption checks key validity at Date.now() + 24h, which is past |
119 | | - // the 1-second key expiry, so this should be rejected. |
120 | 117 | await encryptAndDetachSignData( |
121 | | - Buffer.from(text, 'base64'), |
122 | | - shortLivedRecipientKey.publicKey, |
| 118 | + Buffer.from('ffffffff', 'base64'), |
| 119 | + expiredKey.publicKey, |
123 | 120 | senderKey.privateKey |
124 | 121 | ).should.be.rejectedWith('Error encrypting message: Primary key is expired'); |
125 | 122 | }); |
126 | 123 |
|
127 | | - it('should confirm tolerance constant is 24 hours', function () { |
128 | | - SIGNATURE_DATE_TOLERANCE_MS.should.equal(24 * 60 * 60 * 1000); |
| 124 | + it('should accept verification of a signature created by a device whose clock is ahead', async function () { |
| 125 | + // Simulate a signature created by a device whose clock is 12 hours |
| 126 | + // ahead. The verify tolerance (now + 24h) should accept it. |
| 127 | + const futureDate = new Date(Date.now() + 12 * 60 * 60 * 1000); |
| 128 | + const message = await openpgp.createMessage({ binary: Buffer.from('ffffffff', 'base64') }); |
| 129 | + const privateKey = await openpgp.readPrivateKey({ armoredKey: senderKey.privateKey }); |
| 130 | + const signature = await openpgp.sign({ |
| 131 | + message, |
| 132 | + signingKeys: privateKey, |
| 133 | + format: 'armored', |
| 134 | + detached: true, |
| 135 | + date: futureDate, |
| 136 | + config: { rejectCurves: new Set(), showVersion: false, showComment: false }, |
| 137 | + }); |
| 138 | + |
| 139 | + const result = await verifySignedData( |
| 140 | + { message: Buffer.from('ffffffff', 'base64').toString('base64'), signature }, |
| 141 | + senderKey.publicKey |
| 142 | + ); |
| 143 | + result.should.equal(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should reject verification of a signature created more than 24h in the future', async function () { |
| 147 | + // Simulate a signature from a device whose clock is 25 hours ahead. |
| 148 | + const farFutureDate = new Date(Date.now() + 25 * 60 * 60 * 1000); |
| 149 | + const message = await openpgp.createMessage({ binary: Buffer.from('ffffffff', 'base64') }); |
| 150 | + const privateKey = await openpgp.readPrivateKey({ armoredKey: senderKey.privateKey }); |
| 151 | + const signature = await openpgp.sign({ |
| 152 | + message, |
| 153 | + signingKeys: privateKey, |
| 154 | + format: 'armored', |
| 155 | + detached: true, |
| 156 | + date: farFutureDate, |
| 157 | + config: { rejectCurves: new Set(), showVersion: false, showComment: false }, |
| 158 | + }); |
| 159 | + |
| 160 | + const result = await verifySignedData( |
| 161 | + { message: Buffer.from('ffffffff', 'base64').toString('base64'), signature }, |
| 162 | + senderKey.publicKey |
| 163 | + ); |
| 164 | + result.should.equal(false); |
129 | 165 | }); |
130 | 166 | }); |
131 | 167 | }); |
0 commit comments