2021-10-26 18:47:46 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 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-10-27 10:26:36 +02:00
|
|
|
const DEHYDRATION_LIBOLM_PICKLE_ALGORITHM = "org.matrix.msc2697.v1.olm.libolm_pickle";
|
2021-11-25 12:55:00 +01:00
|
|
|
import {KeyDescription} from "../ssss/common";
|
2021-11-29 09:24:44 +01:00
|
|
|
import {keyFromCredentialAndDescription} from "../ssss/index";
|
2021-10-26 18:47:46 +02:00
|
|
|
|
2021-10-29 15:48:28 +02:00
|
|
|
export async function getDehydratedDevice(hsApi, olm, platform, log) {
|
2021-10-27 10:26:36 +02:00
|
|
|
try {
|
|
|
|
const response = await hsApi.getDehydratedDevice({log}).response();
|
|
|
|
if (response.device_data.algorithm === DEHYDRATION_LIBOLM_PICKLE_ALGORITHM) {
|
2021-10-29 15:48:28 +02:00
|
|
|
return new EncryptedDehydratedDevice(response, olm, platform);
|
2021-10-27 10:26:36 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name !== "HomeServerError") {
|
|
|
|
log.error = err;
|
|
|
|
}
|
|
|
|
return undefined;
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:26:36 +02:00
|
|
|
export async function uploadAccountAsDehydratedDevice(account, hsApi, key, deviceDisplayName, log) {
|
2021-10-26 18:47:46 +02:00
|
|
|
const response = await hsApi.createDehydratedDevice({
|
|
|
|
device_data: {
|
|
|
|
algorithm: DEHYDRATION_LIBOLM_PICKLE_ALGORITHM,
|
2021-11-05 20:52:50 +01:00
|
|
|
account: account.pickleWithKey(key.binaryKey.slice()),
|
2021-10-29 15:48:28 +02:00
|
|
|
passphrase: key.description?.passphraseParams || {},
|
2021-10-26 18:47:46 +02:00
|
|
|
},
|
|
|
|
initial_device_display_name: deviceDisplayName
|
|
|
|
}).response();
|
|
|
|
const deviceId = response.device_id;
|
2021-10-27 18:08:50 +02:00
|
|
|
account.setDeviceId(deviceId);
|
|
|
|
await account.uploadKeys(undefined, true, log);
|
2021-10-26 18:47:46 +02:00
|
|
|
return deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
class EncryptedDehydratedDevice {
|
2021-10-29 15:48:28 +02:00
|
|
|
constructor(dehydratedDevice, olm, platform) {
|
2021-10-26 18:47:46 +02:00
|
|
|
this._dehydratedDevice = dehydratedDevice;
|
|
|
|
this._olm = olm;
|
2021-10-29 15:48:28 +02:00
|
|
|
this._platform = platform;
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 15:48:28 +02:00
|
|
|
async decrypt(keyType, credential) {
|
|
|
|
const keyDescription = new KeyDescription("dehydrated_device", this._dehydratedDevice.device_data.passphrase);
|
|
|
|
const key = await keyFromCredentialAndDescription(keyType, credential, keyDescription, this._platform, this._olm);
|
2021-10-26 18:47:46 +02:00
|
|
|
const account = new this._olm.Account();
|
|
|
|
try {
|
|
|
|
const pickledAccount = this._dehydratedDevice.device_data.account;
|
2021-11-03 02:08:27 +01:00
|
|
|
account.unpickle(key.binaryKey.slice(), pickledAccount);
|
2021-10-29 19:17:31 +02:00
|
|
|
return new DehydratedDevice(this._dehydratedDevice, account, key);
|
2021-10-26 18:47:46 +02:00
|
|
|
} catch (err) {
|
|
|
|
account.free();
|
2021-10-27 15:08:53 +02:00
|
|
|
if (err.message === "OLM.BAD_ACCOUNT_KEY") {
|
|
|
|
return undefined;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 15:08:53 +02:00
|
|
|
|
|
|
|
get deviceId() {
|
|
|
|
return this._dehydratedDevice.device_id;
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class DehydratedDevice {
|
2021-10-29 19:17:31 +02:00
|
|
|
constructor(dehydratedDevice, account, key) {
|
2021-10-26 18:47:46 +02:00
|
|
|
this._dehydratedDevice = dehydratedDevice;
|
|
|
|
this._account = account;
|
2021-10-29 15:48:28 +02:00
|
|
|
this._key = key;
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 10:26:36 +02:00
|
|
|
async claim(hsApi, log) {
|
2021-10-26 18:47:46 +02:00
|
|
|
try {
|
|
|
|
const response = await hsApi.claimDehydratedDevice(this.deviceId, {log}).response();
|
|
|
|
return response.success;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make it clear that ownership is transfered upon calling this
|
|
|
|
adoptUnpickledOlmAccount() {
|
|
|
|
const account = this._account;
|
2021-10-28 11:52:32 +02:00
|
|
|
this._account = undefined;
|
2021-10-26 18:47:46 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
get deviceId() {
|
2021-10-27 10:26:36 +02:00
|
|
|
return this._dehydratedDevice.device_id;
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
2021-10-28 11:52:32 +02:00
|
|
|
|
2021-10-29 15:48:28 +02:00
|
|
|
get key() {
|
|
|
|
return this._key;
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:52:32 +02:00
|
|
|
dispose() {
|
|
|
|
this._account?.free();
|
|
|
|
this._account = undefined;
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|