Skip to content

Commit

Permalink
use new JWE parser in userdata.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Jun 6, 2024
1 parent 0de22c9 commit 917fedc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions frontend/src/common/userdata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { base64 } from 'rfc4648';
import backend, { DeviceDto, UserDto } from './backend';
import { BrowserKeys, UserKeys } from './crypto';
import { JWEParser } from './jwe';
import { JWE, Recipient } from './jwe';

class UserData {

Expand Down Expand Up @@ -120,16 +120,26 @@ class UserData {
return userKeys;
}

public async decryptSetupCode(userKeys: UserKeys): Promise<string> {
const me = await this.me;
if (me.setupCode) {
const payload: { setupCode: string } = await JWE.parseCompact(me.setupCode).decrypt(Recipient.ecdhEs('org.cryptomator.hub.userkey', userKeys.ecdhKeyPair.privateKey));
return payload.setupCode;
} else {
throw new Error('User not set up yet.');
}
}

/**
* Updates the stored user keys, if the ECDSA key was missing before (added in 1.4.0)
* @param userKeys The user keys that contain the ECDSA key
*/
private async addEcdsaKeyIfMissing(userKeys: UserKeys) {
const me = await this.me;
if (me.setupCode && !me.ecdsaPublicKey) {
const payload: { setupCode: string } = await JWEParser.parse(me.setupCode).decryptEcdhEs(userKeys.ecdhKeyPair.privateKey);
const setupCode = await this.decryptSetupCode(userKeys);
me.ecdsaPublicKey = await userKeys.encodedEcdsaPublicKey();
me.privateKey = await userKeys.encryptWithSetupCode(payload.setupCode);
me.privateKey = await userKeys.encryptWithSetupCode(setupCode);
for (const device of me.devices) {
device.userPrivateKey = await userKeys.encryptForDevice(base64.parse(device.publicKey));
}
Expand Down

0 comments on commit 917fedc

Please sign in to comment.