Skip to content

Commit 344ffa3

Browse files
authored
Clean up legacy stores (#4663)
* feat(legacy crypto!): keep legacy methods used in lib olm migration The rust cryto needs these legacy stores in order to do the migration from the legacy crypto to the rust crypto. We keep the following methods of the stores: - Used in `libolm_migration.ts`. - Needed in the legacy store tests. - Needed in the rust crypto test migration. * feat(legacy crypto): extract legacy crypto types in legacy stores In order to be able to delete the legacy crypto, these stores shouldn't rely on the legacy crypto. We need to extract the used types. * feat(crypto store): remove `CryptoStore` functions used only by tests * test(crypto store): use legacy `MemoryStore` type
1 parent 69db213 commit 344ffa3

File tree

6 files changed

+134
-1499
lines changed

6 files changed

+134
-1499
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,18 @@ describe("initRustCrypto", () => {
429429
expect(session.senderSigningKey).toBe(undefined);
430430
}, 10000);
431431

432-
async function encryptAndStoreSecretKey(type: string, key: Uint8Array, pickleKey: string, store: CryptoStore) {
432+
async function encryptAndStoreSecretKey(
433+
type: string,
434+
key: Uint8Array,
435+
pickleKey: string,
436+
store: MemoryCryptoStore,
437+
) {
433438
const encryptedKey = await encryptAESSecretStorageItem(encodeBase64(key), Buffer.from(pickleKey), type);
434439
store.storeSecretStorePrivateKey(undefined, type as keyof SecretStorePrivateKeys, encryptedKey);
435440
}
436441

437442
/** Create a bunch of fake Olm sessions and stash them in the DB. */
438-
function createSessions(store: CryptoStore, nDevices: number, nSessionsPerDevice: number) {
443+
function createSessions(store: MemoryCryptoStore, nDevices: number, nSessionsPerDevice: number) {
439444
for (let i = 0; i < nDevices; i++) {
440445
for (let j = 0; j < nSessionsPerDevice; j++) {
441446
const sessionData = {
@@ -450,7 +455,7 @@ describe("initRustCrypto", () => {
450455
}
451456

452457
/** Create a bunch of fake Megolm sessions and stash them in the DB. */
453-
function createMegolmSessions(store: CryptoStore, nDevices: number, nSessionsPerDevice: number) {
458+
function createMegolmSessions(store: MemoryCryptoStore, nDevices: number, nSessionsPerDevice: number) {
454459
for (let i = 0; i < nDevices; i++) {
455460
for (let j = 0; j < nSessionsPerDevice; j++) {
456461
store.storeEndToEndInboundGroupSession(

src/crypto/store/base.ts

+117-75
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { IRoomKeyRequestBody, IRoomKeyRequestRecipient } from "../index.ts";
18-
import { RoomKeyRequestState } from "../OutgoingRoomKeyRequestManager.ts";
19-
import { IOlmDevice } from "../algorithms/megolm.ts";
20-
import { TrackingStatus } from "../DeviceList.ts";
21-
import { IRoomEncryption } from "../RoomList.ts";
22-
import { IDevice } from "../deviceinfo.ts";
23-
import { ICrossSigningInfo } from "../CrossSigning.ts";
2417
import { Logger } from "../../logger.ts";
25-
import { InboundGroupSessionData } from "../OlmDevice.ts";
2618
import { MatrixEvent } from "../../models/event.ts";
27-
import { DehydrationManager } from "../dehydration.ts";
2819
import { CrossSigningKeyInfo } from "../../crypto-api/index.ts";
2920
import { AESEncryptedSecretStoragePayload } from "../../@types/AESEncryptedSecretStoragePayload.ts";
21+
import { ISignatures } from "../../@types/signed.ts";
3022

3123
/**
3224
* Internal module. Definitions for storage for the crypto module
3325
*/
3426

3527
export interface SecretStorePrivateKeys {
36-
"dehydration": {
37-
keyInfo: DehydrationManager["keyInfo"];
38-
key: AESEncryptedSecretStoragePayload;
39-
deviceDisplayName: string;
40-
time: number;
41-
} | null;
4228
"m.megolm_backup.v1": AESEncryptedSecretStoragePayload;
4329
}
4430

@@ -81,37 +67,14 @@ export interface CryptoStore {
8167
*/
8268
setMigrationState(migrationState: MigrationState): Promise<void>;
8369

84-
getOrAddOutgoingRoomKeyRequest(request: OutgoingRoomKeyRequest): Promise<OutgoingRoomKeyRequest>;
85-
getOutgoingRoomKeyRequest(requestBody: IRoomKeyRequestBody): Promise<OutgoingRoomKeyRequest | null>;
86-
getOutgoingRoomKeyRequestByState(wantedStates: number[]): Promise<OutgoingRoomKeyRequest | null>;
87-
getAllOutgoingRoomKeyRequestsByState(wantedState: number): Promise<OutgoingRoomKeyRequest[]>;
88-
getOutgoingRoomKeyRequestsByTarget(
89-
userId: string,
90-
deviceId: string,
91-
wantedStates: number[],
92-
): Promise<OutgoingRoomKeyRequest[]>;
93-
updateOutgoingRoomKeyRequest(
94-
requestId: string,
95-
expectedState: number,
96-
updates: Partial<OutgoingRoomKeyRequest>,
97-
): Promise<OutgoingRoomKeyRequest | null>;
98-
deleteOutgoingRoomKeyRequest(requestId: string, expectedState: number): Promise<OutgoingRoomKeyRequest | null>;
99-
10070
// Olm Account
10171
getAccount(txn: unknown, func: (accountPickle: string | null) => void): void;
102-
storeAccount(txn: unknown, accountPickle: string): void;
10372
getCrossSigningKeys(txn: unknown, func: (keys: Record<string, CrossSigningKeyInfo> | null) => void): void;
10473
getSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
10574
txn: unknown,
10675
func: (key: SecretStorePrivateKeys[K] | null) => void,
10776
type: K,
10877
): void;
109-
storeCrossSigningKeys(txn: unknown, keys: Record<string, CrossSigningKeyInfo>): void;
110-
storeSecretStorePrivateKey<K extends keyof SecretStorePrivateKeys>(
111-
txn: unknown,
112-
type: K,
113-
key: SecretStorePrivateKeys[K],
114-
): void;
11578

11679
// Olm Sessions
11780
countEndToEndSessions(txn: unknown, func: (count: number) => void): void;
@@ -126,11 +89,6 @@ export interface CryptoStore {
12689
txn: unknown,
12790
func: (sessions: { [sessionId: string]: ISessionInfo }) => void,
12891
): void;
129-
getAllEndToEndSessions(txn: unknown, func: (session: ISessionInfo | null) => void): void;
130-
storeEndToEndSession(deviceKey: string, sessionId: string, sessionInfo: ISessionInfo, txn: unknown): void;
131-
storeEndToEndSessionProblem(deviceKey: string, type: string, fixed: boolean): Promise<void>;
132-
getEndToEndSessionProblem(deviceKey: string, timestamp: number): Promise<IProblem | null>;
133-
filterOutNotifiedErrorDevices(devices: IOlmDevice[]): Promise<IOlmDevice[]>;
13492

13593
/**
13694
* Get a batch of end-to-end sessions from the database.
@@ -156,25 +114,6 @@ export interface CryptoStore {
156114
txn: unknown,
157115
func: (groupSession: InboundGroupSessionData | null, groupSessionWithheld: IWithheld | null) => void,
158116
): void;
159-
getAllEndToEndInboundGroupSessions(txn: unknown, func: (session: ISession | null) => void): void;
160-
addEndToEndInboundGroupSession(
161-
senderCurve25519Key: string,
162-
sessionId: string,
163-
sessionData: InboundGroupSessionData,
164-
txn: unknown,
165-
): void;
166-
storeEndToEndInboundGroupSession(
167-
senderCurve25519Key: string,
168-
sessionId: string,
169-
sessionData: InboundGroupSessionData,
170-
txn: unknown,
171-
): void;
172-
storeEndToEndInboundGroupSessionWithheld(
173-
senderCurve25519Key: string,
174-
sessionId: string,
175-
sessionData: IWithheld,
176-
txn: unknown,
177-
): void;
178117

179118
/**
180119
* Count the number of Megolm sessions in the database.
@@ -201,21 +140,8 @@ export interface CryptoStore {
201140
deleteEndToEndInboundGroupSessionsBatch(sessions: { senderKey: string; sessionId: string }[]): Promise<void>;
202141

203142
// Device Data
204-
getEndToEndDeviceData(txn: unknown, func: (deviceData: IDeviceData | null) => void): void;
205-
storeEndToEndDeviceData(deviceData: IDeviceData, txn: unknown): void;
206-
storeEndToEndRoom(roomId: string, roomInfo: IRoomEncryption, txn: unknown): void;
207143
getEndToEndRooms(txn: unknown, func: (rooms: Record<string, IRoomEncryption>) => void): void;
208-
getSessionsNeedingBackup(limit: number): Promise<ISession[]>;
209-
countSessionsNeedingBackup(txn?: unknown): Promise<number>;
210-
unmarkSessionsNeedingBackup(sessions: ISession[], txn?: unknown): Promise<void>;
211144
markSessionsNeedingBackup(sessions: ISession[], txn?: unknown): Promise<void>;
212-
addSharedHistoryInboundGroupSession(roomId: string, senderKey: string, sessionId: string, txn?: unknown): void;
213-
getSharedHistoryInboundGroupSessions(
214-
roomId: string,
215-
txn?: unknown,
216-
): Promise<[senderKey: string, sessionId: string][]>;
217-
addParkedSharedHistory(roomId: string, data: ParkedSharedHistory, txn?: unknown): void;
218-
takeParkedSharedHistory(roomId: string, txn?: unknown): Promise<ParkedSharedHistory[]>;
219145

220146
// Session key backups
221147
doTxn<T>(mode: Mode, stores: Iterable<string>, func: (txn: unknown) => T, log?: Logger): Promise<T>;
@@ -346,3 +272,119 @@ export enum MigrationState {
346272
* {@link CryptoStore#getEndToEndInboundGroupSessionsBatch}.
347273
*/
348274
export const SESSION_BATCH_SIZE = 50;
275+
276+
export interface InboundGroupSessionData {
277+
room_id: string; // eslint-disable-line camelcase
278+
/** pickled Olm.InboundGroupSession */
279+
session: string;
280+
keysClaimed?: Record<string, string>;
281+
/** Devices involved in forwarding this session to us (normally empty). */
282+
forwardingCurve25519KeyChain: string[];
283+
/** whether this session is untrusted. */
284+
untrusted?: boolean;
285+
/** whether this session exists during the room being set to shared history. */
286+
sharedHistory?: boolean;
287+
}
288+
289+
export interface ICrossSigningInfo {
290+
keys: Record<string, CrossSigningKeyInfo>;
291+
firstUse: boolean;
292+
crossSigningVerifiedBefore: boolean;
293+
}
294+
295+
/* eslint-disable camelcase */
296+
export interface IRoomEncryption {
297+
algorithm: string;
298+
rotation_period_ms?: number;
299+
rotation_period_msgs?: number;
300+
}
301+
/* eslint-enable camelcase */
302+
303+
export enum TrackingStatus {
304+
NotTracked,
305+
PendingDownload,
306+
DownloadInProgress,
307+
UpToDate,
308+
}
309+
310+
/**
311+
* possible states for a room key request
312+
*
313+
* The state machine looks like:
314+
* ```
315+
*
316+
* | (cancellation sent)
317+
* | .-------------------------------------------------.
318+
* | | |
319+
* V V (cancellation requested) |
320+
* UNSENT -----------------------------+ |
321+
* | | |
322+
* | | |
323+
* | (send successful) | CANCELLATION_PENDING_AND_WILL_RESEND
324+
* V | Λ
325+
* SENT | |
326+
* |-------------------------------- | --------------'
327+
* | | (cancellation requested with intent
328+
* | | to resend the original request)
329+
* | |
330+
* | (cancellation requested) |
331+
* V |
332+
* CANCELLATION_PENDING |
333+
* | |
334+
* | (cancellation sent) |
335+
* V |
336+
* (deleted) <---------------------------+
337+
* ```
338+
*/
339+
export enum RoomKeyRequestState {
340+
/** request not yet sent */
341+
Unsent,
342+
/** request sent, awaiting reply */
343+
Sent,
344+
/** reply received, cancellation not yet sent */
345+
CancellationPending,
346+
/**
347+
* Cancellation not yet sent and will transition to UNSENT instead of
348+
* being deleted once the cancellation has been sent.
349+
*/
350+
CancellationPendingAndWillResend,
351+
}
352+
353+
/* eslint-disable camelcase */
354+
interface IRoomKey {
355+
room_id: string;
356+
algorithm: string;
357+
}
358+
359+
/**
360+
* The parameters of a room key request. The details of the request may
361+
* vary with the crypto algorithm, but the management and storage layers for
362+
* outgoing requests expect it to have 'room_id' and 'session_id' properties.
363+
*/
364+
export interface IRoomKeyRequestBody extends IRoomKey {
365+
session_id: string;
366+
sender_key: string;
367+
}
368+
369+
/* eslint-enable camelcase */
370+
371+
export interface IRoomKeyRequestRecipient {
372+
userId: string;
373+
deviceId: string;
374+
}
375+
376+
interface IDevice {
377+
keys: Record<string, string>;
378+
algorithms: string[];
379+
verified: DeviceVerification;
380+
known: boolean;
381+
unsigned?: Record<string, any>;
382+
signatures?: ISignatures;
383+
}
384+
385+
/** State of the verification of the device. */
386+
export enum DeviceVerification {
387+
Blocked = -1,
388+
Unverified = 0,
389+
Verified = 1,
390+
}

0 commit comments

Comments
 (0)