2020-09-08 10:48:11 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} event the plaintext event (type and content property)
|
|
|
|
* @property {string} senderCurve25519Key the curve25519 sender key of the olm event
|
|
|
|
* @property {string} claimedEd25519Key The ed25519 fingerprint key retrieved from the decryption payload.
|
|
|
|
* The sender of the olm event claims this is the ed25519 fingerprint key
|
|
|
|
* that matches the curve25519 sender key.
|
|
|
|
* The caller needs to check if this key does indeed match the senderKey
|
|
|
|
* for a device with a valid signature returned from /keys/query,
|
|
|
|
* see DeviceTracker
|
|
|
|
*/
|
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
import {getDeviceEd25519Key} from "./common";
|
|
|
|
import type {DeviceKey} from "./common";
|
2022-10-28 15:41:18 +02:00
|
|
|
import type {TimelineEvent} from "../storage/types";
|
2020-09-08 10:48:11 +02:00
|
|
|
|
2022-02-15 18:19:49 +01:00
|
|
|
type DecryptedEvent = {
|
|
|
|
type?: string,
|
|
|
|
content?: Record<string, any>
|
|
|
|
}
|
2020-09-08 10:48:11 +02:00
|
|
|
|
|
|
|
export class DecryptionResult {
|
2023-02-27 18:13:53 +01:00
|
|
|
private device?: DeviceKey;
|
2022-02-15 18:19:49 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public readonly event: DecryptedEvent,
|
|
|
|
public readonly senderCurve25519Key: string,
|
2022-10-28 15:41:18 +02:00
|
|
|
public readonly claimedEd25519Key: string,
|
|
|
|
public readonly encryptedEvent?: TimelineEvent
|
2022-02-15 18:19:49 +01:00
|
|
|
) {}
|
2020-09-08 10:48:11 +02:00
|
|
|
|
2023-02-27 18:13:53 +01:00
|
|
|
setDevice(device: DeviceKey): void {
|
2022-02-15 18:19:49 +01:00
|
|
|
this.device = device;
|
2020-09-08 10:48:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:19:49 +01:00
|
|
|
get isVerified(): boolean {
|
|
|
|
if (this.device) {
|
2023-02-27 18:13:53 +01:00
|
|
|
const comesFromDevice = getDeviceEd25519Key(this.device) === this.claimedEd25519Key;
|
2020-09-08 10:48:11 +02:00
|
|
|
return comesFromDevice;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:19:49 +01:00
|
|
|
get isUnverified(): boolean {
|
|
|
|
if (this.device) {
|
2020-09-08 10:48:11 +02:00
|
|
|
return !this.isVerified;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:14:21 +01:00
|
|
|
get userId(): string | undefined {
|
2023-02-27 18:13:53 +01:00
|
|
|
return this.device?.user_id;
|
2022-02-14 17:14:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get deviceId(): string | undefined {
|
2023-02-27 18:13:53 +01:00
|
|
|
return this.device?.device_id;
|
2022-02-14 17:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:19:49 +01:00
|
|
|
get isVerificationUnknown(): boolean {
|
2022-10-28 15:33:46 +02:00
|
|
|
return !this.device;
|
2020-09-08 10:48:11 +02:00
|
|
|
}
|
|
|
|
}
|