Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 33198cc

Browse files
authored
Use new AES functions (#97)
1 parent f33e802 commit 33198cc

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

Diff for: .eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ module.exports = {
9292
"!matrix-js-sdk/src/crypto-api",
9393
"!matrix-js-sdk/src/types",
9494
"!matrix-js-sdk/src/testing",
95+
"!matrix-js-sdk/src/utils/**",
96+
"matrix-js-sdk/src/utils/internal/**",
9597
"matrix-js-sdk/lib",
9698
"matrix-js-sdk/lib/",
9799
"matrix-js-sdk/lib/**",
@@ -119,7 +121,6 @@ module.exports = {
119121
"!matrix-js-sdk/src/extensible_events_v1/PollEndEvent",
120122
"!matrix-js-sdk/src/extensible_events_v1/InvalidEventError",
121123
"!matrix-js-sdk/src/crypto",
122-
"!matrix-js-sdk/src/crypto/aes",
123124
"!matrix-js-sdk/src/crypto/keybackup",
124125
"!matrix-js-sdk/src/crypto/deviceinfo",
125126
"!matrix-js-sdk/src/crypto/dehydration",

Diff for: src/Lifecycle.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please see LICENSE files in the repository root for full details.
1111

1212
import { ReactNode } from "react";
1313
import { createClient, MatrixClient, SSOAction, OidcTokenRefresher, decodeBase64 } from "matrix-js-sdk/src/matrix";
14-
import { IEncryptedPayload } from "matrix-js-sdk/src/crypto/aes";
14+
import { AESEncryptedSecretStoragePayload } from "matrix-js-sdk/src/types";
1515
import { QueryDict } from "matrix-js-sdk/src/utils";
1616
import { logger } from "matrix-js-sdk/src/logger";
1717

@@ -472,9 +472,9 @@ export interface IStoredSession {
472472
hsUrl: string;
473473
isUrl: string;
474474
hasAccessToken: boolean;
475-
accessToken: string | IEncryptedPayload;
475+
accessToken: string | AESEncryptedSecretStoragePayload;
476476
hasRefreshToken: boolean;
477-
refreshToken?: string | IEncryptedPayload;
477+
refreshToken?: string | AESEncryptedSecretStoragePayload;
478478
userId: string;
479479
deviceId: string;
480480
isGuest: boolean;

Diff for: src/utils/tokens/tokens.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
66
Please see LICENSE files in the repository root for full details.
77
*/
88

9-
import { decryptAES, encryptAES, IEncryptedPayload } from "matrix-js-sdk/src/crypto/aes";
109
import { logger } from "matrix-js-sdk/src/logger";
10+
import decryptAESSecretStorageItem from "matrix-js-sdk/src/utils/decryptAESSecretStorageItem";
11+
import encryptAESSecretStorageItem from "matrix-js-sdk/src/utils/encryptAESSecretStorageItem";
12+
import { AESEncryptedSecretStoragePayload } from "matrix-js-sdk/src/types";
1113

1214
import * as StorageAccess from "../StorageAccess";
1315

@@ -78,7 +80,7 @@ async function pickleKeyToAesKey(pickleKey: string): Promise<Uint8Array> {
7880
*/
7981
export async function tryDecryptToken(
8082
pickleKey: string | undefined,
81-
token: IEncryptedPayload | string,
83+
token: AESEncryptedSecretStoragePayload | string,
8284
tokenName: string,
8385
): Promise<string> {
8486
if (typeof token === "string") {
@@ -92,7 +94,7 @@ export async function tryDecryptToken(
9294
}
9395

9496
const encrKey = await pickleKeyToAesKey(pickleKey);
95-
const decryptedToken = await decryptAES(token, encrKey, tokenName);
97+
const decryptedToken = await decryptAESSecretStorageItem(token, encrKey, tokenName);
9698
encrKey.fill(0);
9799
return decryptedToken;
98100
}
@@ -130,12 +132,12 @@ export async function persistTokenInStorage(
130132
}
131133

132134
if (pickleKey) {
133-
let encryptedToken: IEncryptedPayload | undefined;
135+
let encryptedToken: AESEncryptedSecretStoragePayload | undefined;
134136
if (token) {
135137
try {
136138
// try to encrypt the access token using the pickle key
137139
const encrKey = await pickleKeyToAesKey(pickleKey);
138-
encryptedToken = await encryptAES(token, encrKey, tokenName);
140+
encryptedToken = await encryptAESSecretStorageItem(token, encrKey, tokenName);
139141
encrKey.fill(0);
140142
} catch (e) {
141143
// This is likely due to the browser not having WebCrypto or somesuch.

Diff for: test/Lifecycle-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Crypto } from "@peculiar/webcrypto";
1010
import { logger } from "matrix-js-sdk/src/logger";
1111
import * as MatrixJs from "matrix-js-sdk/src/matrix";
1212
import { decodeBase64, encodeUnpaddedBase64 } from "matrix-js-sdk/src/matrix";
13-
import * as MatrixCryptoAes from "matrix-js-sdk/src/crypto/aes";
13+
import * as encryptAESSecretStorageItemModule from "matrix-js-sdk/src/utils/encryptAESSecretStorageItem";
1414
import { mocked, MockedObject } from "jest-mock";
1515
import fetchMock from "fetch-mock-jest";
1616

@@ -74,7 +74,7 @@ describe("Lifecycle", () => {
7474
delete window.crypto;
7575
window.crypto = webCrypto;
7676

77-
jest.spyOn(MatrixCryptoAes, "encryptAES").mockRestore();
77+
jest.spyOn(encryptAESSecretStorageItemModule, "default").mockRestore();
7878
});
7979

8080
afterAll(() => {
@@ -675,7 +675,7 @@ describe("Lifecycle", () => {
675675
});
676676

677677
it("should persist token when encrypting the token fails", async () => {
678-
jest.spyOn(MatrixCryptoAes, "encryptAES").mockRejectedValue("MOCK REJECT ENCRYPTAES");
678+
jest.spyOn(encryptAESSecretStorageItemModule, "default").mockRejectedValue("MOCK REJECT ENCRYPTAES");
679679
await setLoggedIn(credentials);
680680

681681
// persist the unencrypted token

0 commit comments

Comments
 (0)