diff --git a/keyserver/flow-typed/npm/@commapp/olm_vx.x.x.js b/keyserver/flow-typed/npm/@commapp/olm_vx.x.x.js index 49a3ca089..f365eecbc 100644 --- a/keyserver/flow-typed/npm/@commapp/olm_vx.x.x.js +++ b/keyserver/flow-typed/npm/@commapp/olm_vx.x.x.js @@ -1,181 +1,182 @@ // flow-typed signature: 085f002da86534cfd8cee47ffa99dd67 // flow-typed version: <>/@commapp/olm_v3.2.4/flow_v0.182.0 declare module '@commapp/olm' { /* 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. */ declare export class Account { constructor(): void; free(): void; create(): void; identity_keys(): string; sign(message: string | Uint8Array): string; one_time_keys(): string; mark_keys_as_published(): void; max_number_of_one_time_keys(): number; generate_one_time_keys(number_of_keys: number): void; remove_one_time_keys(session: Session): void; generate_prekey(): void; prekey(): string; unpublished_prekey(): ?string; prekey_signature(): ?string; forget_old_prekey(): void; mark_prekey_as_published(): void; last_prekey_publish_time(): number; generate_fallback_key(): void; fallback_key(): string; unpublished_fallback_key(): string; forget_old_fallback_key(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; } declare export type EncryptResult = { +type: 0 | 1, // 0: PreKey, 1: Message +body: string, }; declare export class Session { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create_outbound( account: Account, their_identity_key: string, their_signing_key: string, their_prekey: string, their_prekey_signature: string, their_one_time_key: string, ): void; create_inbound(account: Account, one_time_key_message: string): void; create_inbound_from( account: Account, identity_key: string, one_time_key_message: string, ): void; session_id(): string; has_received_message(): boolean; matches_inbound(one_time_key_message: string): boolean; matches_inbound_from( identity_key: string, one_time_key_message: string, ): boolean; encrypt(plaintext: string): EncryptResult; decrypt(message_type: number, message: string): string; describe(): string; } declare export class Utility { constructor(): void; free(): void; sha256(input: string | Uint8Array): string; ed25519_verify( key: string, message: string | Uint8Array, signature: string, ): void; } declare export type DecryptResult = { +message_index: string, +plaintext: string, }; declare export class InboundGroupSession { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create(session_key: string): string; import_session(session_key: string): string; decrypt(message: string): DecryptResult; session_id(): string; first_known_index(): number; export_session(message_index: number): string; } declare export class OutboundGroupSession { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create(): void; encrypt(plaintext: string): string; session_id(): string; session_key(): string; message_index(): number; } declare export type PkEncryptionEncryptResult = { +ciphertext: string, +mac: string, +ephemeral: string, }; declare export class PkEncryption { constructor(): void; free(): void; set_recipient_key(key: string): void; encrypt(plaintext: string): PkEncryptionEncryptResult; } declare export class PkDecryption { constructor(): void; free(): void; init_with_private_key(key: Uint8Array): string; generate_key(): string; get_private_key(): Uint8Array; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): string; decrypt(ephemeral_key: string, mac: string, ciphertext: string): string; } declare export class PkSigning { constructor(): void; free(): void; init_with_seed(seed: Uint8Array): string; generate_seed(): Uint8Array; sign(message: string): string; } declare export class SAS { constructor(): void; free(): void; get_pubkey(): string; set_their_key(their_key: string): void; generate_bytes(info: string, length: number): Uint8Array; calculate_mac(input: string, info: string): string; calculate_mac_fixed_base64(input: string, info: string): string; calculate_mac_long_kdf(input: string, info: string): string; } declare export function init(opts?: Object): Promise; declare export function get_library_version(): [number, number, number]; declare export var PRIVATE_KEY_LENGTH: number; declare export default { init: typeof init, get_library_version: typeof get_library_version, PRIVATE_KEY_LENGTH: typeof PRIVATE_KEY_LENGTH, Account: typeof Account, Utility: typeof Utility, + Session: typeof Session, }; } diff --git a/keyserver/src/utils/olm-utils.test.js b/keyserver/src/utils/olm-utils.test.js index ca184b587..141e34c50 100644 --- a/keyserver/src/utils/olm-utils.test.js +++ b/keyserver/src/utils/olm-utils.test.js @@ -1,20 +1,218 @@ // @flow import olm from '@commapp/olm'; import { getOlmUtility } from '../utils/olm-utils.js'; describe('olm.Account', () => { + const alphabet = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '; + + const randomString = length => + Array.from( + { length }, + () => alphabet[Math.floor(Math.random() * alphabet.length)], + ).join(''); + + const initAccount = (mark_prekey_published = true) => { + const account = new olm.Account(); + account.create(); + account.generate_prekey(); + account.generate_one_time_keys(1); + if (mark_prekey_published) { + account.mark_prekey_as_published(); + } + return account; + }; + + const createSession = ( + aliceSession, + aliceAccount, + bobAccount, + regen = false, + forget = false, + invalid_sign = false, + ) => { + const bobOneTimeKeys = JSON.parse(bobAccount.one_time_keys()).curve25519; + bobAccount.mark_keys_as_published(); + const otk_id = Object.keys(bobOneTimeKeys)[0]; + + if (regen) { + bobAccount.generate_prekey(); + if (forget) { + bobAccount.forget_old_prekey(); + } + } + + if (invalid_sign) { + try { + aliceSession.create_outbound( + aliceAccount, + JSON.parse(bobAccount.identity_keys()).curve25519, + JSON.parse(bobAccount.identity_keys()).ed25519, + String(Object.values(JSON.parse(bobAccount.prekey()).curve25519)[0]), + bobAccount.sign(randomString(32)), + bobOneTimeKeys[otk_id], + ); + } catch (error) { + expect(error.message).toBe('OLM.BAD_SIGNATURE'); + return false; + } + + try { + aliceSession.create_outbound( + aliceAccount, + JSON.parse(bobAccount.identity_keys()).curve25519, + JSON.parse(bobAccount.identity_keys()).ed25519, + String(Object.values(JSON.parse(bobAccount.prekey()).curve25519)[0]), + randomString(43), + bobOneTimeKeys[otk_id], + ); + } catch (error) { + expect(error.message).toBe('OLM.INVALID_BASE64'); + return false; + } + } + + aliceSession.create_outbound( + aliceAccount, + JSON.parse(bobAccount.identity_keys()).curve25519, + JSON.parse(bobAccount.identity_keys()).ed25519, + String(Object.values(JSON.parse(bobAccount.prekey()).curve25519)[0]), + String(bobAccount.prekey_signature()), + bobOneTimeKeys[otk_id], + ); + + return aliceSession; + }; + + const testRatchet = (aliceSession, bobSession, bobAccount, num_msg = 1) => { + let test_text = randomString(40); + let encrypted = aliceSession.encrypt(test_text); + expect(encrypted.type).toEqual(0); + + try { + bobSession.create_inbound(bobAccount, encrypted.body); + } catch (error) { + expect(error.message).toBe('OLM.BAD_MESSAGE_KEY_ID'); + return false; + } + + bobAccount.remove_one_time_keys(bobSession); + let decrypted = bobSession.decrypt(encrypted.type, encrypted.body); + expect(decrypted).toEqual(test_text); + + test_text = randomString(40); + encrypted = bobSession.encrypt(test_text); + expect(encrypted.type).toEqual(1); + decrypted = aliceSession.decrypt(encrypted.type, encrypted.body); + expect(decrypted).toEqual(test_text); + + for (let index = 1; index < num_msg; index++) { + test_text = randomString(40); + encrypted = aliceSession.encrypt(test_text); + expect(encrypted.type).toEqual(1); + decrypted = bobSession.decrypt(encrypted.type, encrypted.body); + expect(decrypted).toEqual(test_text); + + test_text = randomString(40); + encrypted = bobSession.encrypt(test_text); + expect(encrypted.type).toEqual(1); + decrypted = aliceSession.decrypt(encrypted.type, encrypted.body); + expect(decrypted).toEqual(test_text); + } + + return true; + }; + it('should get Olm Utility', async () => { await olm.init(); const utility = getOlmUtility(); expect(utility).toBeDefined(); }); - it('should be able to generate and return prekey', async () => { + + it('should generate, regenerate, forget, and publish prekey', async () => { await olm.init(); - const account = new olm.Account(); - account.create(); + const account = initAccount(false); + + expect(account.last_prekey_publish_time()).toEqual(0); + expect(account.prekey()).toBeDefined(); + expect(account.unpublished_prekey()).toBeDefined(); + account.mark_prekey_as_published(); + const last_published = account.last_prekey_publish_time(); + expect(last_published).toBeGreaterThan(0); + + try { + console.log(account.unpublished_prekey()); + } catch (error) { + expect(error.message).toContain('NO_UNPUBLISHED_PREKEY'); + } + account.forget_old_prekey(); + account.generate_prekey(); expect(account.prekey()).toBeDefined(); + expect(account.unpublished_prekey()).toBeDefined(); + + expect(account.last_prekey_publish_time()).toEqual(last_published); + account.mark_prekey_as_published(); + expect(account.last_prekey_publish_time()).toBeGreaterThanOrEqual( + last_published, + ); + account.forget_old_prekey(); + }); + + it('should encrypt and decrypt', async () => { + await olm.init(); + const aliceAccount = initAccount(); + const bobAccount = initAccount(); + const aliceSession = new olm.Session(); + const bobSession = new olm.Session(); + + createSession(aliceSession, aliceAccount, bobAccount); + expect(testRatchet(aliceSession, bobSession, bobAccount)).toBeTrue; + }); + + it('should encrypt and decrypt, even after a prekey is rotated', async () => { + await olm.init(); + const aliceAccount = initAccount(); + const bobAccount = initAccount(); + const aliceSession = new olm.Session(); + const bobSession = new olm.Session(); + + createSession(aliceSession, aliceAccount, bobAccount, true); + expect(testRatchet(aliceSession, bobSession, bobAccount)).toBeTrue; + }); + + it('should not encrypt and decrypt, after the old prekey is forgotten', async () => { + await olm.init(); + const aliceAccount = initAccount(); + const bobAccount = initAccount(); + const aliceSession = new olm.Session(); + const bobSession = new olm.Session(); + + createSession(aliceSession, aliceAccount, bobAccount, true, true); + expect(testRatchet(aliceSession, bobSession, bobAccount)).toBeFalse; + }); + + it('should encrypt and decrypt repeatedly', async () => { + await olm.init(); + const aliceAccount = initAccount(); + const bobAccount = initAccount(); + const aliceSession = new olm.Session(); + const bobSession = new olm.Session(); + + createSession(aliceSession, aliceAccount, bobAccount, false, false); + expect(testRatchet(aliceSession, bobSession, bobAccount, 100)).toBeTrue; + }); + + it('should not encrypt and decrypt if prekey is not signed correctly', async () => { + await olm.init(); + const aliceAccount = initAccount(); + const bobAccount = initAccount(); + const aliceSession = new olm.Session(); + + expect( + createSession(aliceSession, aliceAccount, bobAccount, false, false, true), + ).toBeFalse; }); }); diff --git a/web/flow-typed/npm/@commapp/olm_vx.x.x.js b/web/flow-typed/npm/@commapp/olm_vx.x.x.js index 49a3ca089..f365eecbc 100644 --- a/web/flow-typed/npm/@commapp/olm_vx.x.x.js +++ b/web/flow-typed/npm/@commapp/olm_vx.x.x.js @@ -1,181 +1,182 @@ // flow-typed signature: 085f002da86534cfd8cee47ffa99dd67 // flow-typed version: <>/@commapp/olm_v3.2.4/flow_v0.182.0 declare module '@commapp/olm' { /* 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. */ declare export class Account { constructor(): void; free(): void; create(): void; identity_keys(): string; sign(message: string | Uint8Array): string; one_time_keys(): string; mark_keys_as_published(): void; max_number_of_one_time_keys(): number; generate_one_time_keys(number_of_keys: number): void; remove_one_time_keys(session: Session): void; generate_prekey(): void; prekey(): string; unpublished_prekey(): ?string; prekey_signature(): ?string; forget_old_prekey(): void; mark_prekey_as_published(): void; last_prekey_publish_time(): number; generate_fallback_key(): void; fallback_key(): string; unpublished_fallback_key(): string; forget_old_fallback_key(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; } declare export type EncryptResult = { +type: 0 | 1, // 0: PreKey, 1: Message +body: string, }; declare export class Session { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create_outbound( account: Account, their_identity_key: string, their_signing_key: string, their_prekey: string, their_prekey_signature: string, their_one_time_key: string, ): void; create_inbound(account: Account, one_time_key_message: string): void; create_inbound_from( account: Account, identity_key: string, one_time_key_message: string, ): void; session_id(): string; has_received_message(): boolean; matches_inbound(one_time_key_message: string): boolean; matches_inbound_from( identity_key: string, one_time_key_message: string, ): boolean; encrypt(plaintext: string): EncryptResult; decrypt(message_type: number, message: string): string; describe(): string; } declare export class Utility { constructor(): void; free(): void; sha256(input: string | Uint8Array): string; ed25519_verify( key: string, message: string | Uint8Array, signature: string, ): void; } declare export type DecryptResult = { +message_index: string, +plaintext: string, }; declare export class InboundGroupSession { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create(session_key: string): string; import_session(session_key: string): string; decrypt(message: string): DecryptResult; session_id(): string; first_known_index(): number; export_session(message_index: number): string; } declare export class OutboundGroupSession { constructor(): void; free(): void; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): void; create(): void; encrypt(plaintext: string): string; session_id(): string; session_key(): string; message_index(): number; } declare export type PkEncryptionEncryptResult = { +ciphertext: string, +mac: string, +ephemeral: string, }; declare export class PkEncryption { constructor(): void; free(): void; set_recipient_key(key: string): void; encrypt(plaintext: string): PkEncryptionEncryptResult; } declare export class PkDecryption { constructor(): void; free(): void; init_with_private_key(key: Uint8Array): string; generate_key(): string; get_private_key(): Uint8Array; pickle(key: string | Uint8Array): string; unpickle(key: string | Uint8Array, pickle: string): string; decrypt(ephemeral_key: string, mac: string, ciphertext: string): string; } declare export class PkSigning { constructor(): void; free(): void; init_with_seed(seed: Uint8Array): string; generate_seed(): Uint8Array; sign(message: string): string; } declare export class SAS { constructor(): void; free(): void; get_pubkey(): string; set_their_key(their_key: string): void; generate_bytes(info: string, length: number): Uint8Array; calculate_mac(input: string, info: string): string; calculate_mac_fixed_base64(input: string, info: string): string; calculate_mac_long_kdf(input: string, info: string): string; } declare export function init(opts?: Object): Promise; declare export function get_library_version(): [number, number, number]; declare export var PRIVATE_KEY_LENGTH: number; declare export default { init: typeof init, get_library_version: typeof get_library_version, PRIVATE_KEY_LENGTH: typeof PRIVATE_KEY_LENGTH, Account: typeof Account, Utility: typeof Utility, + Session: typeof Session, }; }