Fix key format on the wire to be base64

This commit is contained in:
David Baker
2023-10-20 17:31:15 +01:00
parent 5ab706e26b
commit 9126fb3f3e
4 changed files with 8 additions and 24 deletions

View File

@@ -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<void> => {
this.onSetEncryptionKey(
await createKeyMaterialFromString(encryptionKey),
await createKeyMaterialFromBuffer(encryptionKey),
participantId,
encryptionKeyIndex,
);

View File

@@ -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<void> {
});
}
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,