You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Convert crypto index to TS
This commit is contained in:
@@ -47,7 +47,8 @@ import {
|
|||||||
PREFIX_UNSTABLE,
|
PREFIX_UNSTABLE,
|
||||||
retryNetworkOperation,
|
retryNetworkOperation,
|
||||||
} from "./http-api";
|
} from "./http-api";
|
||||||
import { Crypto, DeviceInfo, fixBackupKey, isCryptoAvailable } from './crypto';
|
import { Crypto, fixBackupKey, IBootstrapCrossSigningOpts, isCryptoAvailable } from './crypto';
|
||||||
|
import { DeviceInfo } from "./crypto/DeviceInfo";
|
||||||
import { decodeRecoveryKey } from './crypto/recoverykey';
|
import { decodeRecoveryKey } from './crypto/recoverykey';
|
||||||
import { keyFromAuthData } from './crypto/key_passphrase';
|
import { keyFromAuthData } from './crypto/key_passphrase';
|
||||||
import { User } from "./models/user";
|
import { User } from "./models/user";
|
||||||
@@ -58,7 +59,6 @@ import {
|
|||||||
IKeyBackupPrepareOpts,
|
IKeyBackupPrepareOpts,
|
||||||
IKeyBackupRestoreOpts,
|
IKeyBackupRestoreOpts,
|
||||||
IKeyBackupRestoreResult,
|
IKeyBackupRestoreResult,
|
||||||
IKeyBackupTrustInfo,
|
|
||||||
IKeyBackupVersion,
|
IKeyBackupVersion,
|
||||||
} from "./crypto/keybackup";
|
} from "./crypto/keybackup";
|
||||||
import { IIdentityServerProvider } from "./@types/IIdentityServerProvider";
|
import { IIdentityServerProvider } from "./@types/IIdentityServerProvider";
|
||||||
@@ -114,7 +114,7 @@ import url from "url";
|
|||||||
import { randomString } from "./randomstring";
|
import { randomString } from "./randomstring";
|
||||||
import { ReadStream } from "fs";
|
import { ReadStream } from "fs";
|
||||||
import { WebStorageSessionStore } from "./store/session/webstorage";
|
import { WebStorageSessionStore } from "./store/session/webstorage";
|
||||||
import { BackupManager } from "./crypto/backup";
|
import { BackupManager, IKeyBackupCheck, TrustInfo } from "./crypto/backup";
|
||||||
import { DEFAULT_TREE_POWER_LEVELS_TEMPLATE, MSC3089TreeSpace } from "./models/MSC3089TreeSpace";
|
import { DEFAULT_TREE_POWER_LEVELS_TEMPLATE, MSC3089TreeSpace } from "./models/MSC3089TreeSpace";
|
||||||
|
|
||||||
export type Store = StubStore | MemoryStore | LocalIndexedDBStoreBackend | RemoteIndexedDBStoreBackend;
|
export type Store = StubStore | MemoryStore | LocalIndexedDBStoreBackend | RemoteIndexedDBStoreBackend;
|
||||||
@@ -141,6 +141,12 @@ interface IExportedDevice {
|
|||||||
deviceId: string;
|
deviceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IKeysUploadResponse {
|
||||||
|
one_time_key_counts: { // eslint-disable-line camelcase
|
||||||
|
[algorithm: string]: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export interface ICreateClientOpts {
|
export interface ICreateClientOpts {
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
|
|
||||||
@@ -836,7 +842,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// XXX: Private member access.
|
// XXX: Private member access.
|
||||||
return await this.crypto._dehydrationManager.setKeyAndQueueDehydration(
|
return await this.crypto.dehydrationManager.setKeyAndQueueDehydration(
|
||||||
key, keyInfo, deviceDisplayName,
|
key, keyInfo, deviceDisplayName,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -859,11 +865,11 @@ export class MatrixClient extends EventEmitter {
|
|||||||
logger.warn('not dehydrating device if crypto is not enabled');
|
logger.warn('not dehydrating device if crypto is not enabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await this.crypto._dehydrationManager.setKey(
|
await this.crypto.dehydrationManager.setKey(
|
||||||
key, keyInfo, deviceDisplayName,
|
key, keyInfo, deviceDisplayName,
|
||||||
);
|
);
|
||||||
// XXX: Private member access.
|
// XXX: Private member access.
|
||||||
return await this.crypto._dehydrationManager.dehydrateDevice();
|
return await this.crypto.dehydrationManager.dehydrateDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exportDevice(): Promise<IExportedDevice> {
|
public async exportDevice(): Promise<IExportedDevice> {
|
||||||
@@ -875,7 +881,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
userId: this.credentials.userId,
|
userId: this.credentials.userId,
|
||||||
deviceId: this.deviceId,
|
deviceId: this.deviceId,
|
||||||
// XXX: Private member access.
|
// XXX: Private member access.
|
||||||
olmDevice: await this.crypto._olmDevice.export(),
|
olmDevice: await this.crypto.olmDevice.export(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1239,12 +1245,12 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* Upload the device keys to the homeserver.
|
* Upload the device keys to the homeserver.
|
||||||
* @return {Promise<void>} A promise that will resolve when the keys are uploaded.
|
* @return {Promise<void>} A promise that will resolve when the keys are uploaded.
|
||||||
*/
|
*/
|
||||||
public uploadKeys(): Promise<void> {
|
public async uploadKeys(): Promise<void> {
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.crypto.uploadDeviceKeys();
|
await this.crypto.uploadDeviceKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1631,7 +1637,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* return true.
|
* return true.
|
||||||
* @return {boolean} True if cross-signing is ready to be used on this device
|
* @return {boolean} True if cross-signing is ready to be used on this device
|
||||||
*/
|
*/
|
||||||
public isCrossSigningReady(): boolean {
|
public isCrossSigningReady(): Promise<boolean> {
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -1658,10 +1664,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* auth data as an object. Can be called multiple times, first with an empty
|
* auth data as an object. Can be called multiple times, first with an empty
|
||||||
* authDict, to obtain the flows.
|
* authDict, to obtain the flows.
|
||||||
*/
|
*/
|
||||||
public bootstrapCrossSigning(opts: {
|
public bootstrapCrossSigning(opts: IBootstrapCrossSigningOpts) {
|
||||||
authUploadDeviceSigningKeys: (makeRequest: (authData: any) => void) => Promise<void>,
|
|
||||||
setupNewCrossSigning?: boolean,
|
|
||||||
}) {
|
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -1756,7 +1759,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
*
|
*
|
||||||
* @return {boolean} True if secret storage is ready to be used on this device
|
* @return {boolean} True if secret storage is ready to be used on this device
|
||||||
*/
|
*/
|
||||||
public isSecretStorageReady(): boolean {
|
public isSecretStorageReady(): Promise<boolean> {
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -1848,7 +1851,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
*
|
*
|
||||||
* @return {string} the contents of the secret
|
* @return {string} the contents of the secret
|
||||||
*/
|
*/
|
||||||
public getSecret(name: string): string {
|
public getSecret(name: string): Promise<string> {
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -1885,7 +1888,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
*
|
*
|
||||||
* @return {string} the contents of the secret
|
* @return {string} the contents of the secret
|
||||||
*/
|
*/
|
||||||
public requestSecret(name: string, devices: string[]): string {
|
public requestSecret(name: string, devices: string[]): any { // TODO types
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -1899,7 +1902,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
*
|
*
|
||||||
* @return {string} The default key ID or null if no default key ID is set
|
* @return {string} The default key ID or null if no default key ID is set
|
||||||
*/
|
*/
|
||||||
public getDefaultSecretStorageKeyId(): string {
|
public getDefaultSecretStorageKeyId(): Promise<string> {
|
||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
@@ -2075,8 +2078,8 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* trust information (as returned by isKeyBackupTrusted)
|
* trust information (as returned by isKeyBackupTrusted)
|
||||||
* in trustInfo.
|
* in trustInfo.
|
||||||
*/
|
*/
|
||||||
public checkKeyBackup(): IKeyBackupVersion {
|
public checkKeyBackup(): Promise<IKeyBackupCheck> {
|
||||||
return this.crypto._backupManager.checkKeyBackup();
|
return this.crypto.backupManager.checkKeyBackup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2117,8 +2120,8 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* ]
|
* ]
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
public isKeyBackupTrusted(info: IKeyBackupVersion): IKeyBackupTrustInfo {
|
public isKeyBackupTrusted(info: IKeyBackupVersion): Promise<TrustInfo> {
|
||||||
return this.crypto._backupManager.isKeyBackupTrusted(info);
|
return this.crypto.backupManager.isKeyBackupTrusted(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2130,7 +2133,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
if (!this.crypto) {
|
if (!this.crypto) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
return this.crypto._backupManager.getKeyBackupEnabled();
|
return this.crypto.backupManager.getKeyBackupEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2145,7 +2148,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.crypto._backupManager.enableKeyBackup(info);
|
return this.crypto.backupManager.enableKeyBackup(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2156,7 +2159,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.crypto._backupManager.disableKeyBackup();
|
this.crypto.backupManager.disableKeyBackup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2184,7 +2187,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
|
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
const { algorithm, auth_data, recovery_key, privateKey } =
|
const { algorithm, auth_data, recovery_key, privateKey } =
|
||||||
await this.crypto._backupManager.prepareKeyBackupVersion(password);
|
await this.crypto.backupManager.prepareKeyBackupVersion(password);
|
||||||
|
|
||||||
if (opts.secureSecretStorage) {
|
if (opts.secureSecretStorage) {
|
||||||
await this.storeSecret("m.megolm_backup.v1", encodeBase64(privateKey));
|
await this.storeSecret("m.megolm_backup.v1", encodeBase64(privateKey));
|
||||||
@@ -2221,7 +2224,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.crypto._backupManager.createKeyBackupVersion(info);
|
await this.crypto.backupManager.createKeyBackupVersion(info);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
algorithm: info.algorithm,
|
algorithm: info.algorithm,
|
||||||
@@ -2232,19 +2235,19 @@ export class MatrixClient extends EventEmitter {
|
|||||||
// older devices with cross-signing. This can probably go away very soon in
|
// older devices with cross-signing. This can probably go away very soon in
|
||||||
// favour of just signing with the cross-singing master key.
|
// favour of just signing with the cross-singing master key.
|
||||||
// XXX: Private member access
|
// XXX: Private member access
|
||||||
await this.crypto._signObject(data.auth_data);
|
await this.crypto.signObject(data.auth_data);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.cryptoCallbacks.getCrossSigningKey &&
|
this.cryptoCallbacks.getCrossSigningKey &&
|
||||||
// XXX: Private member access
|
// XXX: Private member access
|
||||||
this.crypto._crossSigningInfo.getId()
|
this.crypto.crossSigningInfo.getId()
|
||||||
) {
|
) {
|
||||||
// now also sign the auth data with the cross-signing master key
|
// now also sign the auth data with the cross-signing master key
|
||||||
// we check for the callback explicitly here because we still want to be able
|
// we check for the callback explicitly here because we still want to be able
|
||||||
// to create an un-cross-signed key backup if there is a cross-signing key but
|
// to create an un-cross-signed key backup if there is a cross-signing key but
|
||||||
// no callback supplied.
|
// no callback supplied.
|
||||||
// XXX: Private member access
|
// XXX: Private member access
|
||||||
await this.crypto._crossSigningInfo.signObject(data.auth_data, "master");
|
await this.crypto.crossSigningInfo.signObject(data.auth_data, "master");
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await this.http.authedRequest(
|
const res = await this.http.authedRequest(
|
||||||
@@ -2271,8 +2274,8 @@ export class MatrixClient extends EventEmitter {
|
|||||||
// If we're currently backing up to this backup... stop.
|
// If we're currently backing up to this backup... stop.
|
||||||
// (We start using it automatically in createKeyBackupVersion
|
// (We start using it automatically in createKeyBackupVersion
|
||||||
// so this is symmetrical).
|
// so this is symmetrical).
|
||||||
if (this.crypto._backupManager.version) {
|
if (this.crypto.backupManager.version) {
|
||||||
this.crypto._backupManager.disableKeyBackup();
|
this.crypto.backupManager.disableKeyBackup();
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = utils.encodeUri("/room_keys/version/$version", {
|
const path = utils.encodeUri("/room_keys/version/$version", {
|
||||||
@@ -2337,7 +2340,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.crypto._backupManager.scheduleAllGroupSessionsForBackup();
|
await this.crypto.backupManager.scheduleAllGroupSessionsForBackup();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2350,7 +2353,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.crypto._backupManager.flagAllGroupSessionsForBackup();
|
return this.crypto.backupManager.flagAllGroupSessionsForBackup();
|
||||||
}
|
}
|
||||||
|
|
||||||
public isValidRecoveryKey(recoveryKey: string): boolean {
|
public isValidRecoveryKey(recoveryKey: string): boolean {
|
||||||
@@ -2633,7 +2636,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// XXX: Private member access
|
// XXX: Private member access
|
||||||
const alg = this.crypto._getRoomDecryptor(roomId, roomEncryption.algorithm);
|
const alg = this.crypto.getRoomDecryptor(roomId, roomEncryption.algorithm);
|
||||||
if (alg.sendSharedHistoryInboundSessions) {
|
if (alg.sendSharedHistoryInboundSessions) {
|
||||||
await alg.sendSharedHistoryInboundSessions(devicesByUser);
|
await alg.sendSharedHistoryInboundSessions(devicesByUser);
|
||||||
} else {
|
} else {
|
||||||
@@ -5708,7 +5711,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
public getCrossSigningCacheCallbacks(): any { // TODO: Types
|
public getCrossSigningCacheCallbacks(): any { // TODO: Types
|
||||||
// XXX: Private member access
|
// XXX: Private member access
|
||||||
return this.crypto?._crossSigningInfo.getCacheCallbacks();
|
return this.crypto?.crossSigningInfo.getCacheCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7087,7 +7090,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
* @return {Promise} Resolves: result object. Rejects: with
|
* @return {Promise} Resolves: result object. Rejects: with
|
||||||
* an error response ({@link module:http-api.MatrixError}).
|
* an error response ({@link module:http-api.MatrixError}).
|
||||||
*/
|
*/
|
||||||
public uploadKeysRequest(content: any, opts?: any, callback?: Callback): Promise<any> { // TODO: Types
|
public uploadKeysRequest(content: any, opts?: any, callback?: Callback): Promise<IKeysUploadResponse> {
|
||||||
return this.http.authedRequest(callback, "POST", "/keys/upload", undefined, content);
|
return this.http.authedRequest(callback, "POST", "/keys/upload", undefined, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ export interface IEncryptedEventInfo {
|
|||||||
|
|
||||||
export interface IRecoveryKey {
|
export interface IRecoveryKey {
|
||||||
keyInfo: {
|
keyInfo: {
|
||||||
pubkey: Uint8Array;
|
pubkey: string;
|
||||||
passphrase?: {
|
passphrase?: {
|
||||||
algorithm: string;
|
algorithm: string;
|
||||||
iterations: number;
|
iterations: number;
|
||||||
|
|||||||
@@ -48,11 +48,16 @@ type SigInfo = {
|
|||||||
deviceTrust?: DeviceTrustLevel,
|
deviceTrust?: DeviceTrustLevel,
|
||||||
};
|
};
|
||||||
|
|
||||||
type TrustInfo = {
|
export type TrustInfo = {
|
||||||
usable: boolean, // is the backup trusted, true iff there is a sig that is valid & from a trusted device
|
usable: boolean, // is the backup trusted, true iff there is a sig that is valid & from a trusted device
|
||||||
sigs: SigInfo[],
|
sigs: SigInfo[],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface IKeyBackupCheck {
|
||||||
|
backupInfo: BackupInfo;
|
||||||
|
trustInfo: TrustInfo;
|
||||||
|
}
|
||||||
|
|
||||||
/** A function used to get the secret key for a backup.
|
/** A function used to get the secret key for a backup.
|
||||||
*/
|
*/
|
||||||
type GetKey = () => Promise<Uint8Array>;
|
type GetKey = () => Promise<Uint8Array>;
|
||||||
@@ -81,7 +86,7 @@ interface BackupAlgorithm {
|
|||||||
*/
|
*/
|
||||||
export class BackupManager {
|
export class BackupManager {
|
||||||
private algorithm: BackupAlgorithm | undefined;
|
private algorithm: BackupAlgorithm | undefined;
|
||||||
private backupInfo: BackupInfo | undefined; // The info dict from /room_keys/version
|
public backupInfo: BackupInfo | undefined; // The info dict from /room_keys/version
|
||||||
public checkedForBackup: boolean; // Have we checked the server for a backup we can use?
|
public checkedForBackup: boolean; // Have we checked the server for a backup we can use?
|
||||||
private sendingBackups: boolean; // Are we currently sending backups?
|
private sendingBackups: boolean; // Are we currently sending backups?
|
||||||
constructor(private readonly baseApis: MatrixClient, public readonly getKey: GetKey) {
|
constructor(private readonly baseApis: MatrixClient, public readonly getKey: GetKey) {
|
||||||
@@ -232,7 +237,7 @@ export class BackupManager {
|
|||||||
* trust information (as returned by isKeyBackupTrusted)
|
* trust information (as returned by isKeyBackupTrusted)
|
||||||
* in trustInfo.
|
* in trustInfo.
|
||||||
*/
|
*/
|
||||||
public async checkKeyBackup(): Promise<{backupInfo: BackupInfo, trustInfo: TrustInfo}> {
|
public async checkKeyBackup(): Promise<IKeyBackupCheck> {
|
||||||
this.checkedForBackup = false;
|
this.checkedForBackup = false;
|
||||||
return this.checkAndStart();
|
return this.checkAndStart();
|
||||||
}
|
}
|
||||||
@@ -268,7 +273,7 @@ export class BackupManager {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
const trustedPubkey = this.baseApis.crypto._sessionStore.getLocalTrustedBackupPubKey();
|
const trustedPubkey = this.baseApis.crypto.sessionStore.getLocalTrustedBackupPubKey();
|
||||||
|
|
||||||
if (backupInfo.auth_data.public_key === trustedPubkey) {
|
if (backupInfo.auth_data.public_key === trustedPubkey) {
|
||||||
logger.info("Backup public key " + trustedPubkey + " is trusted locally");
|
logger.info("Backup public key " + trustedPubkey + " is trusted locally");
|
||||||
@@ -288,12 +293,12 @@ export class BackupManager {
|
|||||||
const sigInfo: SigInfo = { deviceId: keyIdParts[1] };
|
const sigInfo: SigInfo = { deviceId: keyIdParts[1] };
|
||||||
|
|
||||||
// first check to see if it's from our cross-signing key
|
// first check to see if it's from our cross-signing key
|
||||||
const crossSigningId = this.baseApis.crypto._crossSigningInfo.getId();
|
const crossSigningId = this.baseApis.crypto.crossSigningInfo.getId();
|
||||||
if (crossSigningId === sigInfo.deviceId) {
|
if (crossSigningId === sigInfo.deviceId) {
|
||||||
sigInfo.crossSigningId = true;
|
sigInfo.crossSigningId = true;
|
||||||
try {
|
try {
|
||||||
await verifySignature(
|
await verifySignature(
|
||||||
this.baseApis.crypto._olmDevice,
|
this.baseApis.crypto.olmDevice,
|
||||||
backupInfo.auth_data,
|
backupInfo.auth_data,
|
||||||
this.baseApis.getUserId(),
|
this.baseApis.getUserId(),
|
||||||
sigInfo.deviceId,
|
sigInfo.deviceId,
|
||||||
@@ -313,7 +318,7 @@ export class BackupManager {
|
|||||||
// Now look for a sig from a device
|
// Now look for a sig from a device
|
||||||
// At some point this can probably go away and we'll just support
|
// At some point this can probably go away and we'll just support
|
||||||
// it being signed by the cross-signing master key
|
// it being signed by the cross-signing master key
|
||||||
const device = this.baseApis.crypto._deviceList.getStoredDevice(
|
const device = this.baseApis.crypto.deviceList.getStoredDevice(
|
||||||
this.baseApis.getUserId(), sigInfo.deviceId,
|
this.baseApis.getUserId(), sigInfo.deviceId,
|
||||||
);
|
);
|
||||||
if (device) {
|
if (device) {
|
||||||
@@ -323,7 +328,7 @@ export class BackupManager {
|
|||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await verifySignature(
|
await verifySignature(
|
||||||
this.baseApis.crypto._olmDevice,
|
this.baseApis.crypto.olmDevice,
|
||||||
backupInfo.auth_data,
|
backupInfo.auth_data,
|
||||||
this.baseApis.getUserId(),
|
this.baseApis.getUserId(),
|
||||||
device.deviceId,
|
device.deviceId,
|
||||||
@@ -423,12 +428,12 @@ export class BackupManager {
|
|||||||
* @returns {integer} Number of sessions backed up
|
* @returns {integer} Number of sessions backed up
|
||||||
*/
|
*/
|
||||||
private async backupPendingKeys(limit: number): Promise<number> {
|
private async backupPendingKeys(limit: number): Promise<number> {
|
||||||
const sessions = await this.baseApis.crypto._cryptoStore.getSessionsNeedingBackup(limit);
|
const sessions = await this.baseApis.crypto.cryptoStore.getSessionsNeedingBackup(limit);
|
||||||
if (!sessions.length) {
|
if (!sessions.length) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let remaining = await this.baseApis.crypto._cryptoStore.countSessionsNeedingBackup();
|
let remaining = await this.baseApis.crypto.cryptoStore.countSessionsNeedingBackup();
|
||||||
this.baseApis.crypto.emit("crypto.keyBackupSessionsRemaining", remaining);
|
this.baseApis.crypto.emit("crypto.keyBackupSessionsRemaining", remaining);
|
||||||
|
|
||||||
const data = {};
|
const data = {};
|
||||||
@@ -438,7 +443,7 @@ export class BackupManager {
|
|||||||
data[roomId] = { sessions: {} };
|
data[roomId] = { sessions: {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionData = await this.baseApis.crypto._olmDevice.exportInboundGroupSession(
|
const sessionData = await this.baseApis.crypto.olmDevice.exportInboundGroupSession(
|
||||||
session.senderKey, session.sessionId, session.sessionData,
|
session.senderKey, session.sessionId, session.sessionData,
|
||||||
);
|
);
|
||||||
sessionData.algorithm = MEGOLM_ALGORITHM;
|
sessionData.algorithm = MEGOLM_ALGORITHM;
|
||||||
@@ -446,13 +451,13 @@ export class BackupManager {
|
|||||||
const forwardedCount =
|
const forwardedCount =
|
||||||
(sessionData.forwarding_curve25519_key_chain || []).length;
|
(sessionData.forwarding_curve25519_key_chain || []).length;
|
||||||
|
|
||||||
const userId = this.baseApis.crypto._deviceList.getUserByIdentityKey(
|
const userId = this.baseApis.crypto.deviceList.getUserByIdentityKey(
|
||||||
MEGOLM_ALGORITHM, session.senderKey,
|
MEGOLM_ALGORITHM, session.senderKey,
|
||||||
);
|
);
|
||||||
const device = this.baseApis.crypto._deviceList.getDeviceByIdentityKey(
|
const device = this.baseApis.crypto.deviceList.getDeviceByIdentityKey(
|
||||||
MEGOLM_ALGORITHM, session.senderKey,
|
MEGOLM_ALGORITHM, session.senderKey,
|
||||||
);
|
);
|
||||||
const verified = this.baseApis.crypto._checkDeviceInfoTrust(userId, device).isVerified();
|
const verified = this.baseApis.crypto.checkDeviceInfoTrust(userId, device).isVerified();
|
||||||
|
|
||||||
data[roomId]['sessions'][session.sessionId] = {
|
data[roomId]['sessions'][session.sessionId] = {
|
||||||
first_message_index: sessionData.first_known_index,
|
first_message_index: sessionData.first_known_index,
|
||||||
@@ -467,8 +472,8 @@ export class BackupManager {
|
|||||||
{ rooms: data },
|
{ rooms: data },
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.baseApis.crypto._cryptoStore.unmarkSessionsNeedingBackup(sessions);
|
await this.baseApis.crypto.cryptoStore.unmarkSessionsNeedingBackup(sessions);
|
||||||
remaining = await this.baseApis.crypto._cryptoStore.countSessionsNeedingBackup();
|
remaining = await this.baseApis.crypto.cryptoStore.countSessionsNeedingBackup();
|
||||||
this.baseApis.crypto.emit("crypto.keyBackupSessionsRemaining", remaining);
|
this.baseApis.crypto.emit("crypto.keyBackupSessionsRemaining", remaining);
|
||||||
|
|
||||||
return sessions.length;
|
return sessions.length;
|
||||||
@@ -477,7 +482,7 @@ export class BackupManager {
|
|||||||
public async backupGroupSession(
|
public async backupGroupSession(
|
||||||
senderKey: string, sessionId: string,
|
senderKey: string, sessionId: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.baseApis.crypto._cryptoStore.markSessionsNeedingBackup([{
|
await this.baseApis.crypto.cryptoStore.markSessionsNeedingBackup([{
|
||||||
senderKey: senderKey,
|
senderKey: senderKey,
|
||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
}]);
|
}]);
|
||||||
@@ -509,22 +514,22 @@ export class BackupManager {
|
|||||||
* (which will be equal to the number of sessions in the store).
|
* (which will be equal to the number of sessions in the store).
|
||||||
*/
|
*/
|
||||||
public async flagAllGroupSessionsForBackup(): Promise<number> {
|
public async flagAllGroupSessionsForBackup(): Promise<number> {
|
||||||
await this.baseApis.crypto._cryptoStore.doTxn(
|
await this.baseApis.crypto.cryptoStore.doTxn(
|
||||||
'readwrite',
|
'readwrite',
|
||||||
[
|
[
|
||||||
IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS,
|
IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS,
|
||||||
IndexedDBCryptoStore.STORE_BACKUP,
|
IndexedDBCryptoStore.STORE_BACKUP,
|
||||||
],
|
],
|
||||||
(txn) => {
|
(txn) => {
|
||||||
this.baseApis.crypto._cryptoStore.getAllEndToEndInboundGroupSessions(txn, (session) => {
|
this.baseApis.crypto.cryptoStore.getAllEndToEndInboundGroupSessions(txn, (session) => {
|
||||||
if (session !== null) {
|
if (session !== null) {
|
||||||
this.baseApis.crypto._cryptoStore.markSessionsNeedingBackup([session], txn);
|
this.baseApis.crypto.cryptoStore.markSessionsNeedingBackup([session], txn);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const remaining = await this.baseApis.crypto._cryptoStore.countSessionsNeedingBackup();
|
const remaining = await this.baseApis.crypto.cryptoStore.countSessionsNeedingBackup();
|
||||||
this.baseApis.emit("crypto.keyBackupSessionsRemaining", remaining);
|
this.baseApis.emit("crypto.keyBackupSessionsRemaining", remaining);
|
||||||
return remaining;
|
return remaining;
|
||||||
}
|
}
|
||||||
@@ -534,7 +539,7 @@ export class BackupManager {
|
|||||||
* @returns {Promise<int>} Resolves to the number of sessions requiring backup
|
* @returns {Promise<int>} Resolves to the number of sessions requiring backup
|
||||||
*/
|
*/
|
||||||
public countSessionsNeedingBackup(): Promise<number> {
|
public countSessionsNeedingBackup(): Promise<number> {
|
||||||
return this.baseApis.crypto._cryptoStore.countSessionsNeedingBackup();
|
return this.baseApis.crypto.cryptoStore.countSessionsNeedingBackup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -292,7 +292,7 @@ export class DehydrationManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private stop() {
|
public stop() {
|
||||||
if (this.timeoutId) {
|
if (this.timeoutId) {
|
||||||
global.clearTimeout(this.timeoutId);
|
global.clearTimeout(this.timeoutId);
|
||||||
this.timeoutId = undefined;
|
this.timeoutId = undefined;
|
||||||
|
|||||||
3651
src/crypto/index.js
3651
src/crypto/index.js
File diff suppressed because it is too large
Load Diff
3745
src/crypto/index.ts
Normal file
3745
src/crypto/index.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -41,18 +41,7 @@ export interface IKeyBackupVersion {
|
|||||||
count: number;
|
count: number;
|
||||||
etag: string;
|
etag: string;
|
||||||
version: string; // number contained within
|
version: string; // number contained within
|
||||||
}
|
recovery_key: string; // eslint-disable-line camelcase
|
||||||
|
|
||||||
// TODO: Verify types
|
|
||||||
export interface IKeyBackupTrustInfo {
|
|
||||||
/**
|
|
||||||
* is the backup trusted, true if there is a sig that is valid & from a trusted device
|
|
||||||
*/
|
|
||||||
usable: boolean[];
|
|
||||||
sigs: {
|
|
||||||
valid: boolean[];
|
|
||||||
device: DeviceInfo[];
|
|
||||||
}[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IKeyBackupPrepareOpts {
|
export interface IKeyBackupPrepareOpts {
|
||||||
|
|||||||
Reference in New Issue
Block a user