Skip to content

Commit

Permalink
refactor: remove traces of Cryptobox
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed Jan 16, 2024
1 parent eb0be3a commit 5c49965
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 419 deletions.
3 changes: 0 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
"@wireapp/api-client": "workspace:^",
"@wireapp/commons": "workspace:^",
"@wireapp/core-crypto": "1.0.0-rc.23",
"@wireapp/cryptobox": "12.8.0",
"@wireapp/promise-queue": "workspace:^",
"@wireapp/protocol-messaging": "1.44.0",
"@wireapp/store-engine": "workspace:*",
"@wireapp/store-engine-dexie": "workspace:^",
"axios": "1.6.5",
"bazinga64": "workspace:^",
"deepmerge-ts": "5.1.0",
Expand All @@ -31,7 +29,6 @@
"zod": "3.22.4"
},
"devDependencies": {
"@faker-js/faker": "^8.0.0",
"@swc/core": "^1.3.10",
"@swc/jest": "^0.2.23",
"@types/jest": "^29.2.0",
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/Account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ const MOCK_BACKEND = {
ws: `wss://${BASE_URL}`,
};

const config = {nbPrekeys: 100, coreCryptoConfig: {wasmFilePath: ''}};

async function createAccount(): Promise<{account: Account; apiClient: APIClient}> {
const apiClient = new APIClient({urls: MOCK_BACKEND});
const account = new Account(apiClient);
const account = new Account(apiClient, config);
await account.initServices({
clientType: ClientType.TEMPORARY,
userId: '',
Expand Down Expand Up @@ -171,7 +173,8 @@ describe('Account', () => {
};
describe('"init"', () => {
it('initializes the Protocol buffers', async () => {
const account = new Account();
const apiClient = new APIClient({urls: MOCK_BACKEND});
const account = new Account(apiClient, config);

await account.initServices({clientType: ClientType.TEMPORARY, userId: ''});

Expand All @@ -189,7 +192,7 @@ describe('Account', () => {
describe('"login"', () => {
it('logs in with correct credentials', async () => {
const apiClient = new APIClient({urls: MOCK_BACKEND});
const account = new Account(apiClient);
const account = new Account(apiClient, config);

await account.initServices({clientType: ClientType.TEMPORARY, userId: ''});
const {clientType, userId} = await account.login({
Expand All @@ -204,7 +207,7 @@ describe('Account', () => {

it('does not log in with incorrect credentials', async () => {
const apiClient = new APIClient({urls: MOCK_BACKEND});
const account = new Account(apiClient);
const account = new Account(apiClient, config);
let backendError;

await account.initServices({clientType: ClientType.TEMPORARY, userId: ''});
Expand Down
38 changes: 13 additions & 25 deletions packages/core/src/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {MLSService} from './messagingProtocols/mls';
import {AcmeChallenge, E2EIServiceExternal, User} from './messagingProtocols/mls/E2EIdentityService';
import {CoreCallbacks, CoreCryptoConfig, SecretCrypto} from './messagingProtocols/mls/types';
import {NewClient, ProteusService} from './messagingProtocols/proteus';
import {CryptoClientType} from './messagingProtocols/proteus/ProteusService/CryptoClient';
import {buildClient} from './messagingProtocols/proteus/ProteusService/CryptoClient/CoreCryptoWrapper';
import {HandledEventPayload, NotificationService, NotificationSource} from './notification/';
import {createCustomEncryptedStore, createEncryptedStore, EncryptedStore} from './secretStore/encryptedStore';
import {generateSecretKey} from './secretStore/secretKeyGenerator';
Expand Down Expand Up @@ -105,10 +105,7 @@ interface AccountOptions {
*/
nbPrekeys: number;

/**
* Config for MLS and proteus devices. Will fallback to the old cryptobox logic if not provided
*/
coreCryptoConfig?: CoreCryptoConfig;
coreCryptoConfig: CoreCryptoConfig;
}

type InitOptions = {
Expand All @@ -129,7 +126,7 @@ type Events = {
export class Account extends TypedEventEmitter<Events> {
private readonly apiClient: APIClient;
private readonly logger: logdown.Logger;
private readonly coreCryptoConfig?: CoreCryptoConfig;
private readonly coreCryptoConfig: CoreCryptoConfig;
private readonly isMlsEnabled: () => Promise<boolean>;
/** this is the client the consumer is currently using. Will be set as soon as `initClient` is called and will be rest upon logout */
private currentClient?: RegisteredClient;
Expand Down Expand Up @@ -165,13 +162,13 @@ export class Account extends TypedEventEmitter<Events> {
*/
constructor(
apiClient: APIClient = new APIClient(),
private options: AccountOptions = {nbPrekeys: 100},
private options: AccountOptions,
) {
super();
this.apiClient = apiClient;
this.backendFeatures = this.apiClient.backendFeatures;
this.coreCryptoConfig = options.coreCryptoConfig;
this.isMlsEnabled = async () => !!this.coreCryptoConfig?.mls && (await this.apiClient.supportsMLS());
this.isMlsEnabled = async () => !!this.coreCryptoConfig.mls && (await this.apiClient.supportsMLS());
this.recurringTaskScheduler = new RecurringTaskScheduler({
get: async key => {
const task = await this.db?.get('recurringTasks', key);
Expand Down Expand Up @@ -226,7 +223,7 @@ export class Account extends TypedEventEmitter<Events> {

private async getE2EIStatus() {
const features = await this.apiClient.api.teams.feature.getAllFeatures();
const clientCanUseE2EI = this.coreCryptoConfig?.mls?.useE2EI;
const clientCanUseE2EI = this.coreCryptoConfig.mls?.useE2EI;
const teamCanUseE2EI = features[FEATURE_KEY.MLSE2EID]?.status === FeatureStatus.ENABLED;

return {
Expand Down Expand Up @@ -415,20 +412,11 @@ export class Account extends TypedEventEmitter<Events> {
},
};

const coreCryptoConfig = this.coreCryptoConfig;
if (coreCryptoConfig) {
const {buildClient} = await import('./messagingProtocols/proteus/ProteusService/CryptoClient/CoreCryptoWrapper');
const client = await buildClient(storeEngine, {
...baseConfig,
...coreCryptoConfig,
generateSecretKey: keyId => generateSecretKey({keyId, keySize: 16, secretsDb: encryptedStore}),
});
return [CryptoClientType.CORE_CRYPTO, client] as const;
}

const {buildClient} = await import('./messagingProtocols/proteus/ProteusService/CryptoClient/CryptoboxWrapper');
const client = buildClient(storeEngine, baseConfig);
return [CryptoClientType.CRYPTOBOX, client] as const;
return buildClient(storeEngine, {
...baseConfig,
...this.coreCryptoConfig,
generateSecretKey: keyId => generateSecretKey({keyId, keySize: 16, secretsDb: encryptedStore}),
});
}

/**
Expand All @@ -453,7 +441,7 @@ export class Account extends TypedEventEmitter<Events> {
const accountService = new AccountService(this.apiClient);
const assetService = new AssetService(this.apiClient);

const [clientType, cryptoClient] = await this.buildCryptoClient(context, this.storeEngine, this.encryptedDb);
const cryptoClient = await this.buildCryptoClient(context, this.storeEngine, this.encryptedDb);

let mlsService: MLSService | undefined;
let e2eServiceExternal: E2EIServiceExternal | undefined;
Expand All @@ -465,7 +453,7 @@ export class Account extends TypedEventEmitter<Events> {

const clientService = new ClientService(this.apiClient, proteusService, this.storeEngine);

if (clientType === CryptoClientType.CORE_CRYPTO && (await this.isMlsEnabled())) {
if (await this.isMlsEnabled()) {
mlsService = new MLSService(
this.apiClient,
cryptoClient.getNativeClient(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import {PreKey} from '@wireapp/api-client/lib/auth';

export type InitialPrekeys = {prekeys: PreKey[]; lastPrekey: PreKey};

export enum CryptoClientType {
CORE_CRYPTO,
CRYPTOBOX,
}

export interface CryptoClient<T = unknown> {
getNativeClient(): T;
encrypt(sessions: string[], plainText: Uint8Array): Promise<Map<string, Uint8Array>>;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ describe('generateDecryptionError', () => {
expect(error.message).toBe(`Decryption error from user1 (client1) (${coreCryptoError.message})`);
expect(error.code).toBe(coreCryptoError.proteusErrorCode);
});

it.each([Math.floor(Math.random() * 100), 0])('handles cryptobox error', code => {
const coreCryptoError = {code, message: 'decryption error'};
const error = generateDecryptionError(basePayload, coreCryptoError);
expect(error).toBeInstanceOf(DecryptionError);
expect(error.message).toBe(`Decryption error from user1 (client1) (${coreCryptoError.message})`);
expect(error.code).toBe(coreCryptoError.code);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,16 @@ export const ProteusErrors = {
Unknown: 999,
} as const;

type CryptoboxError = Error & {code: number};

const isCoreCryptoError = (error: any): error is CoreCryptoError => {
return 'proteusErrorCode' in error;
};
const isCryptoboxError = (error: any): error is CryptoboxError => {
return 'code' in error;
};

type SenderInfo = {clientId: string; userId: QualifiedId};
export const generateDecryptionError = (senderInfo: SenderInfo, error: any): DecryptionError => {
const {clientId, userId} = senderInfo;
const sender = `${userId.id} (${clientId})`;

const coreCryptoCode = isCoreCryptoError(error) ? error.proteusErrorCode : null;
const cryptoboxCode = isCryptoboxError(error) ? error.code : null;
const code = coreCryptoCode ?? cryptoboxCode ?? ProteusErrors.Unknown;

const code = isCoreCryptoError(error) ? error.proteusErrorCode : ProteusErrors.Unknown;
const message = `Decryption error from ${sender} (${error.message})`;

return new DecryptionError(message, code);
Expand Down
42 changes: 0 additions & 42 deletions packages/core/src/test/AccountHelper.ts

This file was deleted.

Loading

0 comments on commit 5c49965

Please sign in to comment.