2020-09-17 11:39:40 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2021-11-29 12:35:15 +01:00
|
|
|
import type {Key} from "./common";
|
|
|
|
import type {Platform} from "../../platform/web/Platform.js";
|
|
|
|
import type {Transaction} from "../storage/idb/Transaction";
|
2023-03-13 09:15:49 +01:00
|
|
|
import type {Storage} from "../storage/idb/Storage";
|
|
|
|
import type {AccountDataEntry} from "../storage/idb/stores/AccountDataStore";
|
2020-09-17 11:39:40 +02:00
|
|
|
|
2021-12-02 07:27:20 +01:00
|
|
|
type EncryptedData = {
|
|
|
|
iv: string;
|
|
|
|
ciphertext: string;
|
|
|
|
mac: string;
|
|
|
|
}
|
|
|
|
|
2023-03-13 09:15:49 +01:00
|
|
|
export enum DecryptionFailure {
|
|
|
|
NotEncryptedWithKey,
|
|
|
|
BadMAC,
|
|
|
|
UnsupportedAlgorithm,
|
|
|
|
}
|
|
|
|
|
|
|
|
class DecryptionError extends Error {
|
|
|
|
constructor(msg: string, public readonly reason: DecryptionFailure) {
|
|
|
|
super(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:39:40 +02:00
|
|
|
export class SecretStorage {
|
2021-11-29 12:35:15 +01:00
|
|
|
private readonly _key: Key;
|
|
|
|
private readonly _platform: Platform;
|
2023-03-24 13:42:19 +01:00
|
|
|
private readonly _storage: Storage;
|
2021-11-29 12:35:15 +01:00
|
|
|
|
2023-03-24 13:42:19 +01:00
|
|
|
constructor({key, platform, storage}: {key: Key, platform: Platform, storage: Storage}) {
|
2020-09-17 11:39:40 +02:00
|
|
|
this._key = key;
|
2021-02-11 17:29:48 +01:00
|
|
|
this._platform = platform;
|
2023-03-24 13:42:19 +01:00
|
|
|
this._storage = storage;
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 13:42:19 +01:00
|
|
|
/** this method will auto-commit any indexeddb transaction because of its use of the webcrypto api */
|
|
|
|
async hasValidKeyForAnyAccountData() {
|
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.accountData,
|
|
|
|
]);
|
2023-03-13 09:15:49 +01:00
|
|
|
const allAccountData = await txn.accountData.getAll();
|
|
|
|
for (const accountData of allAccountData) {
|
|
|
|
try {
|
|
|
|
const secret = await this._decryptAccountData(accountData);
|
|
|
|
return true; // decryption succeeded
|
|
|
|
} catch (err) {
|
2023-03-21 18:24:46 +01:00
|
|
|
if (err instanceof DecryptionError && err.reason !== DecryptionFailure.NotEncryptedWithKey) {
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-13 09:15:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-24 13:42:19 +01:00
|
|
|
/** this method will auto-commit any indexeddb transaction because of its use of the webcrypto api */
|
|
|
|
async readSecret(name: string): Promise<string | undefined> {
|
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.accountData,
|
|
|
|
]);
|
2020-09-17 11:39:40 +02:00
|
|
|
const accountData = await txn.accountData.get(name);
|
|
|
|
if (!accountData) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-13 09:15:49 +01:00
|
|
|
return await this._decryptAccountData(accountData);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _decryptAccountData(accountData: AccountDataEntry): Promise<string> {
|
2021-12-02 07:27:20 +01:00
|
|
|
const encryptedData = accountData?.content?.encrypted?.[this._key.id] as EncryptedData;
|
2020-09-17 11:39:40 +02:00
|
|
|
if (!encryptedData) {
|
2023-03-13 09:15:49 +01:00
|
|
|
throw new DecryptionError(`Secret ${accountData.type} is not encrypted for key ${this._key.id}`, DecryptionFailure.NotEncryptedWithKey);
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._key.algorithm === "m.secret_storage.v1.aes-hmac-sha2") {
|
2020-09-17 12:33:57 +02:00
|
|
|
return await this._decryptAESSecret(accountData.type, encryptedData);
|
2020-09-17 11:39:40 +02:00
|
|
|
} else {
|
2023-03-13 09:15:49 +01:00
|
|
|
throw new DecryptionError(`Unsupported algorithm for key ${this._key.id}: ${this._key.algorithm}`, DecryptionFailure.UnsupportedAlgorithm);
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 07:27:20 +01:00
|
|
|
async _decryptAESSecret(type: string, encryptedData: EncryptedData): Promise<string> {
|
2021-02-12 16:01:54 +01:00
|
|
|
const {base64, utf8} = this._platform.encoding;
|
2020-09-17 11:39:40 +02:00
|
|
|
// now derive the aes and mac key from the 4s key
|
2021-02-11 17:29:48 +01:00
|
|
|
const hkdfKey = await this._platform.crypto.derive.hkdf(
|
2020-09-17 11:39:40 +02:00
|
|
|
this._key.binaryKey,
|
|
|
|
new Uint8Array(8).buffer, //zero salt
|
2021-02-12 16:01:54 +01:00
|
|
|
utf8.encode(type), // info
|
2020-09-17 11:39:40 +02:00
|
|
|
"SHA-256",
|
|
|
|
512 // 512 bits or 64 bytes
|
|
|
|
);
|
|
|
|
const aesKey = hkdfKey.slice(0, 32);
|
|
|
|
const hmacKey = hkdfKey.slice(32);
|
|
|
|
const ciphertextBytes = base64.decode(encryptedData.ciphertext);
|
|
|
|
|
2021-02-11 17:29:48 +01:00
|
|
|
const isVerified = await this._platform.crypto.hmac.verify(
|
2020-09-17 11:39:40 +02:00
|
|
|
hmacKey, base64.decode(encryptedData.mac),
|
|
|
|
ciphertextBytes, "SHA-256");
|
|
|
|
|
|
|
|
if (!isVerified) {
|
2023-03-13 09:15:49 +01:00
|
|
|
throw new DecryptionError("Bad MAC", DecryptionFailure.BadMAC);
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:29:48 +01:00
|
|
|
const plaintextBytes = await this._platform.crypto.aes.decryptCTR({
|
2020-10-23 17:18:11 +02:00
|
|
|
key: aesKey,
|
|
|
|
iv: base64.decode(encryptedData.iv),
|
|
|
|
data: ciphertextBytes
|
|
|
|
});
|
2020-09-17 11:39:40 +02:00
|
|
|
|
2021-02-12 16:01:54 +01:00
|
|
|
return utf8.decode(plaintextBytes);
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
}
|