diff options
Diffstat (limited to 'client-web/source/protocol')
-rw-r--r-- | client-web/source/protocol/crypto.ts | 8 | ||||
-rw-r--r-- | client-web/source/protocol/mod.ts | 18 |
2 files changed, 16 insertions, 10 deletions
diff --git a/client-web/source/protocol/crypto.ts b/client-web/source/protocol/crypto.ts index 784bd49..ebb552a 100644 --- a/client-web/source/protocol/crypto.ts +++ b/client-web/source/protocol/crypto.ts @@ -12,7 +12,7 @@ const IV_LENGTH = 12 const CRYPTO_SALT = base64_to_buf("keksmeet/cryptosaltAAA==") const HASH_SALT = base64_to_buf("keksmeet/roomhashsaltA==") -export async function crypto_seeded_key(seed: string): Promise<CryptoKey> { +export async function derive_seeded_key(seed: string): Promise<CryptoKey> { log("crypto", "deriving crytographic key...") const seed_key = await window.crypto.subtle.importKey( "raw", @@ -32,7 +32,7 @@ export async function crypto_seeded_key(seed: string): Promise<CryptoKey> { return key } -export async function crypto_hash(input: string): Promise<string> { +export async function room_hash(input: string): Promise<string> { log("crypto", "deriving room hash...") const seed_key = await window.crypto.subtle.importKey( "raw", @@ -50,7 +50,7 @@ export async function crypto_hash(input: string): Promise<string> { return hex } -export async function crypto_encrypt(key: CryptoKey, data: string): Promise<string> { +export async function encrypt(key: CryptoKey, data: string): Promise<string> { const iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH)); const ciphertext = new Uint8Array(await window.crypto.subtle.encrypt( { name: "AES-GCM", iv }, @@ -64,7 +64,7 @@ export async function crypto_encrypt(key: CryptoKey, data: string): Promise<stri return b64; } -export async function crypt_decrypt(key: CryptoKey, data: string): Promise<string> { +export async function decrypt(key: CryptoKey, data: string): Promise<string> { try { const buf = base64_to_buf(data); const iv = buf.slice(0, IV_LENGTH); diff --git a/client-web/source/protocol/mod.ts b/client-web/source/protocol/mod.ts index e82cf94..805600d 100644 --- a/client-web/source/protocol/mod.ts +++ b/client-web/source/protocol/mod.ts @@ -6,7 +6,7 @@ import { ClientboundPacket, RelayMessage, RelayMessageWrapper, ServerboundPacket } from "../../../common/packets.d.ts" import { EventEmitter } from "../helper.ts"; import { log } from "../logger.ts" -import { crypto_encrypt, crypto_seeded_key, crypt_decrypt, crypto_hash } from "./crypto.ts" +import { encrypt, derive_seeded_key, decrypt, room_hash } from "./crypto.ts" export class SignalingConnection { websocket!: WebSocket @@ -48,8 +48,8 @@ export class SignalingConnection { async join(room: string) { this.room = room; - this.key = await crypto_seeded_key(room) - this.room_hash = await crypto_hash(room) + this.key = await derive_seeded_key(room) + this.room_hash = await room_hash(room) this.send_control({ join: { hash: this.room_hash } }) } @@ -67,8 +67,14 @@ export class SignalingConnection { this.control_handler.dispatch(packet) if (packet.init) this.my_id = packet.init.your_id; if (packet.message) { - const plain_json = await crypt_decrypt(this.key!, packet.message.message) - const plain: RelayMessageWrapper = JSON.parse(plain_json) // TODO make sure that protocol spec is met + const plain_json = await decrypt(this.key!, packet.message.message) + + let plain: RelayMessageWrapper + try { + plain = JSON.parse(plain_json) // TODO make sure that protocol spec is met + } catch (_e) { + return log({ scope: "ws", warn: true }, "somebody sent invalid json"); + } if (plain.sender == packet.message.sender) this.relay_handler.dispatch([packet.message.sender, plain.inner]) else { @@ -83,7 +89,7 @@ export class SignalingConnection { async send_relay(data: RelayMessage, recipient?: number | null) { recipient ??= undefined // null -> undefined const packet: RelayMessageWrapper = { inner: data, sender: this.my_id! } - const message = await crypto_encrypt(this.key!, JSON.stringify(packet)) + const message = await encrypt(this.key!, JSON.stringify(packet)) this.send_control({ relay: { recipient, message } }) } } |