Skip to content

Commit

Permalink
Check that decryption key matchs key backup info in `loadSessionBacku…
Browse files Browse the repository at this point in the history
…pPrivateKeyFromSecretStorage`
  • Loading branch information
florianduros committed Nov 7, 2024
1 parent c47066f commit a70ee65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/rust-crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { sleep } from "../utils.ts";
import { BackupDecryptor } from "../common-crypto/CryptoBackend.ts";
import { ImportRoomKeyProgressData, ImportRoomKeysOpts, CryptoEvent } from "../crypto-api/index.ts";
import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";
import { encodeBase64 } from "../base64.ts";

/** Authentification of the backup info, depends on algorithm */
type AuthData = KeyBackupInfo["auth_data"];
Expand Down Expand Up @@ -813,6 +814,18 @@ export async function requestKeyBackupVersion(
}
}

/**
* Checks if the provided decryption key matches the public key of the key backup info.
* @param decryptionKey - The decryption key to check.
* @param keyBackupInfo - The key backup info to check against.
* @returns `true` if the decryption key matches the key backup info, `false` otherwise.
*/
export function decryptionKeyMatchKeyBackupInfo(decryptionKey: Uint8Array, keyBackupInfo: KeyBackupInfo): boolean {
const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(decryptionKey));
const authData = <Curve25519AuthData>keyBackupInfo.auth_data;
return authData.public_key === backupDecryptionKey.megolmV1PublicKey.publicKeyBase64;
}

/**
* Counts the total number of keys present in a key backup.
* @param keyBackup - The key backup to count the keys from.
Expand Down
13 changes: 7 additions & 6 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
CrossSigningStatus,
CryptoApi,
CryptoCallbacks,
Curve25519AuthData,
DecryptionFailureCode,
DeviceVerificationStatus,
EventEncryptionInfo,
Expand Down Expand Up @@ -78,7 +77,7 @@ import { secretStorageCanAccessSecrets, secretStorageContainsCrossSigningKeys }
import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification.ts";
import { EventType, MsgType } from "../@types/event.ts";
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
import { RustBackupManager } from "./backup.ts";
import { decryptionKeyMatchKeyBackupInfo, RustBackupManager } from "./backup.ts";
import { TypedReEmitter } from "../ReEmitter.ts";
import { randomString } from "../randomstring.ts";
import { ClientStoppedError } from "../errors.ts";
Expand Down Expand Up @@ -339,13 +338,11 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
throw new Error(`getBackupDecryptor: Unsupported algorithm ${backupInfo.algorithm}`);
}

const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey));

const authData = <Curve25519AuthData>backupInfo.auth_data;
if (authData.public_key != backupDecryptionKey.megolmV1PublicKey.publicKeyBase64) {
if (!decryptionKeyMatchKeyBackupInfo(privKey, backupInfo)) {
throw new Error(`getBackupDecryptor: key backup on server does not match the decryption key`);
}

const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey));
return this.backupManager.createBackupDecryptor(backupDecryptionKey);
}

Expand Down Expand Up @@ -1220,6 +1217,10 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
throw new Error("loadSessionBackupPrivateKeyFromSecretStorage: unable to get backup version");
}

if (!decryptionKeyMatchKeyBackupInfo(decodedKey, keyBackupInfo)) {
throw new Error("loadSessionBackupPrivateKeyFromSecretStorage: decryption key does not match backup info");
}

await this.storeSessionBackupPrivateKey(decodedKey, keyBackupInfo.version);
}

Expand Down

0 comments on commit a70ee65

Please sign in to comment.