2021-04-16 23:13:13 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
|
|
|
|
import { isNumber } from 'lodash';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction,
|
|
|
|
PreKeyRecord,
|
2021-10-26 19:15:33 +00:00
|
|
|
ProtocolAddress,
|
|
|
|
SenderKeyRecord,
|
|
|
|
SessionRecord,
|
|
|
|
SignedPreKeyRecord,
|
|
|
|
Uuid,
|
|
|
|
} from '@signalapp/signal-client';
|
|
|
|
import {
|
|
|
|
IdentityKeyStore,
|
2021-04-16 23:13:13 +00:00
|
|
|
PreKeyStore,
|
|
|
|
PrivateKey,
|
|
|
|
PublicKey,
|
2021-05-14 01:18:43 +00:00
|
|
|
SenderKeyStore,
|
|
|
|
SessionStore,
|
|
|
|
SignedPreKeyStore,
|
|
|
|
} from '@signalapp/signal-client';
|
2021-04-16 23:13:13 +00:00
|
|
|
import { freezePreKey, freezeSignedPreKey } from './SignalProtocolStore';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { Address } from './types/Address';
|
|
|
|
import { QualifiedAddress } from './types/QualifiedAddress';
|
|
|
|
import type { UUID } from './types/UUID';
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Zone } from './util/Zone';
|
2021-05-17 18:03:42 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
function encodeAddress(address: ProtocolAddress): Address {
|
2021-04-16 23:13:13 +00:00
|
|
|
const name = address.name();
|
|
|
|
const deviceId = address.deviceId();
|
2021-09-10 02:38:11 +00:00
|
|
|
return Address.create(name, deviceId);
|
2021-04-16 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
function toQualifiedAddress(
|
|
|
|
ourUuid: UUID,
|
|
|
|
address: ProtocolAddress
|
|
|
|
): QualifiedAddress {
|
|
|
|
return new QualifiedAddress(ourUuid, encodeAddress(address));
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SessionsOptions = Readonly<{
|
|
|
|
ourUuid: UUID;
|
|
|
|
zone?: Zone;
|
|
|
|
}>;
|
2021-05-17 18:03:42 +00:00
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
export class Sessions extends SessionStore {
|
2021-09-10 02:38:11 +00:00
|
|
|
private readonly ourUuid: UUID;
|
|
|
|
|
2021-05-19 21:25:56 +00:00
|
|
|
private readonly zone: Zone | undefined;
|
2021-05-17 18:03:42 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
constructor({ ourUuid, zone }: SessionsOptions) {
|
2021-05-17 18:03:42 +00:00
|
|
|
super();
|
2021-09-10 02:38:11 +00:00
|
|
|
|
|
|
|
this.ourUuid = ourUuid;
|
|
|
|
this.zone = zone;
|
2021-05-17 18:03:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async saveSession(
|
|
|
|
address: ProtocolAddress,
|
|
|
|
record: SessionRecord
|
|
|
|
): Promise<void> {
|
|
|
|
await window.textsecure.storage.protocol.storeSession(
|
2021-09-10 02:38:11 +00:00
|
|
|
toQualifiedAddress(this.ourUuid, address),
|
2021-05-17 18:03:42 +00:00
|
|
|
record,
|
2021-05-19 21:25:56 +00:00
|
|
|
{ zone: this.zone }
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getSession(name: ProtocolAddress): Promise<SessionRecord | null> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = toQualifiedAddress(this.ourUuid, name);
|
2021-04-16 23:13:13 +00:00
|
|
|
const record = await window.textsecure.storage.protocol.loadSession(
|
2021-09-10 02:38:11 +00:00
|
|
|
encodedAddress,
|
2021-05-19 21:25:56 +00:00
|
|
|
{ zone: this.zone }
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return record || null;
|
|
|
|
}
|
2021-05-25 22:40:04 +00:00
|
|
|
|
|
|
|
async getExistingSessions(
|
|
|
|
addresses: Array<ProtocolAddress>
|
|
|
|
): Promise<Array<SessionRecord>> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddresses = addresses.map(addr =>
|
|
|
|
toQualifiedAddress(this.ourUuid, addr)
|
|
|
|
);
|
2021-05-25 22:40:04 +00:00
|
|
|
return window.textsecure.storage.protocol.loadSessions(encodedAddresses, {
|
|
|
|
zone: this.zone,
|
|
|
|
});
|
|
|
|
}
|
2021-04-16 23:13:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
export type IdentityKeysOptions = Readonly<{
|
|
|
|
ourUuid: UUID;
|
|
|
|
zone?: Zone;
|
|
|
|
}>;
|
2021-05-18 00:41:28 +00:00
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
export class IdentityKeys extends IdentityKeyStore {
|
2021-09-10 02:38:11 +00:00
|
|
|
private readonly ourUuid: UUID;
|
|
|
|
|
2021-05-19 21:25:56 +00:00
|
|
|
private readonly zone: Zone | undefined;
|
2021-05-18 00:41:28 +00:00
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
constructor({ ourUuid, zone }: IdentityKeysOptions) {
|
2021-05-18 00:41:28 +00:00
|
|
|
super();
|
2021-09-10 02:38:11 +00:00
|
|
|
|
|
|
|
this.ourUuid = ourUuid;
|
2021-05-19 21:25:56 +00:00
|
|
|
this.zone = zone;
|
2021-05-18 00:41:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async getIdentityKey(): Promise<PrivateKey> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const keyPair = await window.textsecure.storage.protocol.getIdentityKeyPair(
|
|
|
|
this.ourUuid
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!keyPair) {
|
|
|
|
throw new Error('IdentityKeyStore/getIdentityKey: No identity key!');
|
|
|
|
}
|
|
|
|
return PrivateKey.deserialize(Buffer.from(keyPair.privKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getLocalRegistrationId(): Promise<number> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const id = await window.textsecure.storage.protocol.getLocalRegistrationId(
|
|
|
|
this.ourUuid
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!isNumber(id)) {
|
|
|
|
throw new Error(
|
|
|
|
'IdentityKeyStore/getLocalRegistrationId: No registration id!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getIdentity(address: ProtocolAddress): Promise<PublicKey | null> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = encodeAddress(address);
|
2021-04-16 23:13:13 +00:00
|
|
|
const key = await window.textsecure.storage.protocol.loadIdentityKey(
|
2021-09-10 02:38:11 +00:00
|
|
|
encodedAddress.uuid
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PublicKey.deserialize(Buffer.from(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveIdentity(name: ProtocolAddress, key: PublicKey): Promise<boolean> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = encodeAddress(name);
|
2021-09-24 00:49:05 +00:00
|
|
|
const publicKey = key.serialize();
|
2021-05-18 00:41:28 +00:00
|
|
|
|
2021-05-19 21:25:56 +00:00
|
|
|
// Pass `zone` to let `saveIdentity` archive sibling sessions when identity
|
2021-05-18 00:41:28 +00:00
|
|
|
// key changes.
|
2021-04-16 23:13:13 +00:00
|
|
|
return window.textsecure.storage.protocol.saveIdentity(
|
2021-09-10 02:38:11 +00:00
|
|
|
encodedAddress,
|
2021-05-18 00:41:28 +00:00
|
|
|
publicKey,
|
|
|
|
false,
|
2021-05-19 21:25:56 +00:00
|
|
|
{ zone: this.zone }
|
2021-04-16 23:13:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async isTrustedIdentity(
|
|
|
|
name: ProtocolAddress,
|
|
|
|
key: PublicKey,
|
|
|
|
direction: Direction
|
|
|
|
): Promise<boolean> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = encodeAddress(name);
|
2021-09-24 00:49:05 +00:00
|
|
|
const publicKey = key.serialize();
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
return window.textsecure.storage.protocol.isTrustedIdentity(
|
2021-09-10 02:38:11 +00:00
|
|
|
encodedAddress,
|
2021-04-16 23:13:13 +00:00
|
|
|
publicKey,
|
|
|
|
direction
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
export type PreKeysOptions = Readonly<{
|
|
|
|
ourUuid: UUID;
|
|
|
|
}>;
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
export class PreKeys extends PreKeyStore {
|
2021-09-10 02:38:11 +00:00
|
|
|
private readonly ourUuid: UUID;
|
|
|
|
|
|
|
|
constructor({ ourUuid }: PreKeysOptions) {
|
|
|
|
super();
|
|
|
|
this.ourUuid = ourUuid;
|
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async savePreKey(id: number, record: PreKeyRecord): Promise<void> {
|
|
|
|
await window.textsecure.storage.protocol.storePreKey(
|
2021-09-10 02:38:11 +00:00
|
|
|
this.ourUuid,
|
2021-04-16 23:13:13 +00:00
|
|
|
id,
|
|
|
|
freezePreKey(record)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPreKey(id: number): Promise<PreKeyRecord> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const preKey = await window.textsecure.storage.protocol.loadPreKey(
|
|
|
|
this.ourUuid,
|
|
|
|
id
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
if (preKey === undefined) {
|
|
|
|
throw new Error(`getPreKey: PreKey ${id} not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return preKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removePreKey(id: number): Promise<void> {
|
2021-09-10 02:38:11 +00:00
|
|
|
await window.textsecure.storage.protocol.removePreKey(this.ourUuid, id);
|
2021-04-16 23:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
export type SenderKeysOptions = Readonly<{
|
|
|
|
ourUuid: UUID;
|
|
|
|
}>;
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
export class SenderKeys extends SenderKeyStore {
|
2021-09-10 02:38:11 +00:00
|
|
|
private readonly ourUuid: UUID;
|
|
|
|
|
|
|
|
constructor({ ourUuid }: SenderKeysOptions) {
|
|
|
|
super();
|
|
|
|
this.ourUuid = ourUuid;
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
async saveSenderKey(
|
|
|
|
sender: ProtocolAddress,
|
|
|
|
distributionId: Uuid,
|
|
|
|
record: SenderKeyRecord
|
|
|
|
): Promise<void> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = toQualifiedAddress(this.ourUuid, sender);
|
2021-05-14 01:18:43 +00:00
|
|
|
|
|
|
|
await window.textsecure.storage.protocol.saveSenderKey(
|
|
|
|
encodedAddress,
|
|
|
|
distributionId,
|
|
|
|
record
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getSenderKey(
|
|
|
|
sender: ProtocolAddress,
|
|
|
|
distributionId: Uuid
|
|
|
|
): Promise<SenderKeyRecord | null> {
|
2021-09-10 02:38:11 +00:00
|
|
|
const encodedAddress = toQualifiedAddress(this.ourUuid, sender);
|
2021-05-14 01:18:43 +00:00
|
|
|
|
|
|
|
const senderKey = await window.textsecure.storage.protocol.getSenderKey(
|
|
|
|
encodedAddress,
|
|
|
|
distributionId
|
|
|
|
);
|
|
|
|
|
|
|
|
return senderKey || null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
export type SignedPreKeysOptions = Readonly<{
|
|
|
|
ourUuid: UUID;
|
|
|
|
}>;
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
export class SignedPreKeys extends SignedPreKeyStore {
|
2021-09-10 02:38:11 +00:00
|
|
|
private readonly ourUuid: UUID;
|
|
|
|
|
|
|
|
constructor({ ourUuid }: SignedPreKeysOptions) {
|
|
|
|
super();
|
|
|
|
this.ourUuid = ourUuid;
|
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
async saveSignedPreKey(
|
|
|
|
id: number,
|
|
|
|
record: SignedPreKeyRecord
|
|
|
|
): Promise<void> {
|
|
|
|
await window.textsecure.storage.protocol.storeSignedPreKey(
|
2021-09-10 02:38:11 +00:00
|
|
|
this.ourUuid,
|
2021-04-16 23:13:13 +00:00
|
|
|
id,
|
|
|
|
freezeSignedPreKey(record),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getSignedPreKey(id: number): Promise<SignedPreKeyRecord> {
|
|
|
|
const signedPreKey = await window.textsecure.storage.protocol.loadSignedPreKey(
|
2021-09-10 02:38:11 +00:00
|
|
|
this.ourUuid,
|
2021-04-16 23:13:13 +00:00
|
|
|
id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!signedPreKey) {
|
|
|
|
throw new Error(`getSignedPreKey: SignedPreKey ${id} not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return signedPreKey;
|
|
|
|
}
|
|
|
|
}
|