From 9126fb3f3e5e74d446200930d73432882e91894d Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 20 Oct 2023 17:31:15 +0100 Subject: [PATCH] Fix key format on the wire to be base64 --- package.json | 2 +- src/e2ee/matrixKeyProvider.ts | 6 +++--- src/matrix-utils.ts | 20 ++------------------ yarn.lock | 4 ++-- 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 59ecf93e..d68729f3 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "i18next-http-backend": "^2.0.0", "livekit-client": "^1.12.3", "lodash": "^4.17.21", - "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#9a42886e26267e0e4508bb4f40f4ab4de6db9a2e", + "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#4120641b3d3983733044967812e475bc3610d1e7", "matrix-widget-api": "^1.3.1", "normalize.css": "^8.0.1", "pako": "^2.0.4", diff --git a/src/e2ee/matrixKeyProvider.ts b/src/e2ee/matrixKeyProvider.ts index fcef3d8e..73b788d8 100644 --- a/src/e2ee/matrixKeyProvider.ts +++ b/src/e2ee/matrixKeyProvider.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { BaseKeyProvider, createKeyMaterialFromString } from "livekit-client"; +import { BaseKeyProvider, createKeyMaterialFromBuffer } from "livekit-client"; import { logger } from "matrix-js-sdk/src/logger"; import { MatrixRTCSession, @@ -56,12 +56,12 @@ export class MatrixKeyProvider extends BaseKeyProvider { } private onEncryptionKeyChanged = async ( - encryptionKey: string, + encryptionKey: Uint8Array, encryptionKeyIndex: number, participantId: string, ): Promise => { this.onSetEncryptionKey( - await createKeyMaterialFromString(encryptionKey), + await createKeyMaterialFromBuffer(encryptionKey), participantId, encryptionKeyIndex, ); diff --git a/src/matrix-utils.ts b/src/matrix-utils.ts index 52c55959..7c0966fb 100644 --- a/src/matrix-utils.ts +++ b/src/matrix-utils.ts @@ -28,6 +28,7 @@ import { GroupCallIntent, GroupCallType, } from "matrix-js-sdk/src/webrtc/groupCall"; +import { secureRandomBase64 } from "matrix-js-sdk/src/randomstring"; import type { MatrixClient } from "matrix-js-sdk/src/client"; import type { Room } from "matrix-js-sdk/src/models/room"; @@ -73,23 +74,6 @@ function waitForSync(client: MatrixClient): Promise { }); } -function secureRandomString(entropyBytes: number): string { - const key = new Uint8Array(entropyBytes); - crypto.getRandomValues(key); - // encode to base64url as this value goes into URLs - // base64url is just base64 with thw two non-alphanum characters swapped out for - // ones that can be put in a URL without encoding. Browser JS has a native impl - // for base64 encoding but only a string (there isn't one that takes a UInt8Array - // yet) so just use the built-in one and convert, replace the chars and strip the - // padding from the end (otherwise we'd need to pull in another dependency). - return btoa( - key.reduce((acc, current) => acc + String.fromCharCode(current), ""), - ) - .replace("+", "-") - .replace("/", "_") - .replace(/=*$/, ""); -} - /** * Initialises and returns a new standalone Matrix Client. * If true is passed for the 'restore' parameter, a check will be made @@ -363,7 +347,7 @@ export async function createRoom( let password; if (e2ee) { - password = secureRandomString(16); + password = secureRandomBase64(16); setLocalStorageItem( getRoomSharedKeyLocalStorageKey(result.room_id), password, diff --git a/yarn.lock b/yarn.lock index 253e6e1d..3d2988ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7042,9 +7042,9 @@ matrix-events-sdk@0.0.1: resolved "https://registry.yarnpkg.com/matrix-events-sdk/-/matrix-events-sdk-0.0.1.tgz#c8c38911e2cb29023b0bbac8d6f32e0de2c957dd" integrity sha512-1QEOsXO+bhyCroIe2/A5OwaxHvBm7EsSQ46DEDn8RBIfQwN5HWBpFvyWWR4QY0KHPPnnJdI99wgRiAl7Ad5qaA== -"matrix-js-sdk@github:matrix-org/matrix-js-sdk#9a42886e26267e0e4508bb4f40f4ab4de6db9a2e": +"matrix-js-sdk@github:matrix-org/matrix-js-sdk#4120641b3d3983733044967812e475bc3610d1e7": version "29.0.0" - resolved "https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/9a42886e26267e0e4508bb4f40f4ab4de6db9a2e" + resolved "https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/4120641b3d3983733044967812e475bc3610d1e7" dependencies: "@babel/runtime" "^7.12.5" "@matrix-org/matrix-sdk-crypto-wasm" "^2.0.0"