Skip to content

Commit a3196af

Browse files
also update user's private keys
both stored among devices and encrypted using the setup code
1 parent 4397f37 commit a3196af

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

frontend/src/common/userdata.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { base64 } from 'rfc4648';
22
import backend, { DeviceDto, UserDto } from './backend';
33
import { BrowserKeys, UserKeys } from './crypto';
4+
import { JWEParser } from './jwe';
45

56
class UserData {
67

@@ -56,10 +57,7 @@ class UserData {
5657
const ecdhPublicKey = base64.parse(me.ecdhPublicKey);
5758
const ecdsaPublicKey = me.ecdsaPublicKey ? base64.parse(me.ecdsaPublicKey) : undefined;
5859
const userKeys = await UserKeys.recover(me.privateKey, setupCode, ecdhPublicKey, ecdsaPublicKey);
59-
if (!me.ecdsaPublicKey) { // Update user, if ECDSA key was missing before (added in 1.4.0)
60-
me.ecdsaPublicKey = await userKeys.encodedEcdsaPublicKey();
61-
await backend.users.putMe(me);
62-
}
60+
await this.addEcdsaKeyIfMissing(userKeys);
6361
return userKeys;
6462
}
6563

@@ -79,11 +77,26 @@ class UserData {
7977
const ecdhPublicKey = base64.parse(me.ecdhPublicKey);
8078
const ecdsaPublicKey = me.ecdsaPublicKey ? base64.parse(me.ecdsaPublicKey) : undefined;
8179
const userKeys = await UserKeys.decryptOnBrowser(browser.userPrivateKey, browserKeys.keyPair.privateKey, ecdhPublicKey, ecdsaPublicKey);
82-
if (!me.ecdsaPublicKey) { // Update user, if ECDSA key was missing before (added in 1.4.0)
80+
await this.addEcdsaKeyIfMissing(userKeys);
81+
return userKeys;
82+
}
83+
84+
/**
85+
* Updates the stored user keys, if the ECDSA key was missing before (added in 1.4.0)
86+
* @param userKeys The user keys that contain the ECDSA key
87+
*/
88+
private async addEcdsaKeyIfMissing(userKeys: UserKeys) {
89+
const me = await this.me;
90+
if (me.setupCode && !me.ecdsaPublicKey) {
91+
const payload: { setupCode: string } = await JWEParser.parse(me.setupCode).decryptEcdhEs(userKeys.ecdhKeyPair.privateKey);
8392
me.ecdsaPublicKey = await userKeys.encodedEcdsaPublicKey();
84-
await backend.users.putMe(me);
93+
me.privateKey = await userKeys.encryptWithSetupCode(payload.setupCode);
94+
await backend.users.putMe(me); // TODO: update user and devices in single transaction!
95+
for (let device of me.devices) {
96+
device.userPrivateKey = await userKeys.encryptForDevice(base64.parse(device.publicKey));
97+
await backend.devices.putDevice(device); // TODO: update user and devices in single transaction!
98+
}
8599
}
86-
return userKeys;
87100
}
88101

89102
}

0 commit comments

Comments
 (0)