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.
|
|
|
|
*/
|
|
|
|
|
2020-09-17 17:55:35 +02:00
|
|
|
import base64 from "../../../lib/base64-arraybuffer/index.js";
|
2020-09-17 11:39:40 +02:00
|
|
|
|
|
|
|
export class SecretStorage {
|
2020-10-26 15:44:11 +01:00
|
|
|
constructor({key, crypto}) {
|
2020-09-17 11:39:40 +02:00
|
|
|
this._key = key;
|
2020-10-26 15:44:11 +01:00
|
|
|
this._crypto = crypto;
|
2020-09-17 11:39:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:58:46 +02:00
|
|
|
async readSecret(name, txn) {
|
2020-09-17 11:39:40 +02:00
|
|
|
const accountData = await txn.accountData.get(name);
|
|
|
|
if (!accountData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const encryptedData = accountData?.content?.encrypted?.[this._key.id];
|
|
|
|
if (!encryptedData) {
|
|
|
|
throw new Error(`Secret ${accountData.type} is not encrypted for key ${this._key.id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
throw new Error(`Unsupported algorithm for key ${this._key.id}: ${this._key.algorithm}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _decryptAESSecret(type, encryptedData) {
|
|
|
|
// TODO: we should we move this to platform specific code
|
|
|
|
const textEncoder = new TextEncoder();
|
|
|
|
const textDecoder = new TextDecoder();
|
|
|
|
// now derive the aes and mac key from the 4s key
|
2020-10-26 15:44:11 +01:00
|
|
|
const hkdfKey = await this._crypto.derive.hkdf(
|
2020-09-17 11:39:40 +02:00
|
|
|
this._key.binaryKey,
|
|
|
|
new Uint8Array(8).buffer, //zero salt
|
|
|
|
textEncoder.encode(type), // info
|
|
|
|
"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);
|
|
|
|
|
2020-10-26 15:44:11 +01:00
|
|
|
const isVerified = await this._crypto.hmac.verify(
|
2020-09-17 11:39:40 +02:00
|
|
|
hmacKey, base64.decode(encryptedData.mac),
|
|
|
|
ciphertextBytes, "SHA-256");
|
|
|
|
|
|
|
|
if (!isVerified) {
|
|
|
|
throw new Error("Bad MAC");
|
|
|
|
}
|
|
|
|
|
2020-10-26 17:08:29 +01:00
|
|
|
const plaintextBytes = await this._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
|
|
|
|
|
|
|
return textDecoder.decode(plaintextBytes);
|
|
|
|
}
|
|
|
|
}
|