@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5
5
Please see LICENSE in the repository root for full details.
6
6
*/
7
7
8
- import { BaseKeyProvider , createKeyMaterialFromBuffer } from "livekit-client" ;
8
+ import { BaseKeyProvider , createKeyMaterialFromBuffer , importKey , KeyProviderEvent } from "livekit-client" ;
9
9
import { logger } from "matrix-js-sdk/lib/logger" ;
10
10
import {
11
11
type MatrixRTCSession ,
@@ -15,8 +15,22 @@ import {
15
15
export class MatrixKeyProvider extends BaseKeyProvider {
16
16
private rtcSession ?: MatrixRTCSession ;
17
17
18
+ private readonly onKeyRatchetComplete : ( material : ArrayBuffer , keyIndex ?: number ) => void ;
19
+
18
20
public constructor ( ) {
19
- super ( { ratchetWindowSize : 10 , keyringSize : 256 } ) ;
21
+ super ( { ratchetWindowSize : 10 , keyringSize : 10 } ) ;
22
+
23
+ this . onKeyRatchetComplete = ( material : ArrayBuffer , keyIndex ?: number ) : void => {
24
+ logger . debug ( `key ratcheted event received for index ` , keyIndex ) ;
25
+ this . rtcSession ?. onOwnKeyRatcheted ( material , keyIndex ) . catch ( ( e ) => {
26
+ logger . error (
27
+ `Failed to ratchet key for livekit room=${ this . rtcSession ?. room . roomId } keyIndex=${ keyIndex } ` ,
28
+ e ,
29
+ ) ;
30
+ } ) ;
31
+ } ;
32
+
33
+ this . on ( KeyProviderEvent . RatchetRequestCompleted , this . onKeyRatchetComplete ) ;
20
34
}
21
35
22
36
public setRTCSession ( rtcSession : MatrixRTCSession ) : void {
@@ -25,6 +39,11 @@ export class MatrixKeyProvider extends BaseKeyProvider {
25
39
MatrixRTCSessionEvent . EncryptionKeyChanged ,
26
40
this . onEncryptionKeyChanged ,
27
41
) ;
42
+ this . rtcSession . off (
43
+ MatrixRTCSessionEvent . EncryptionKeyQueryRatchetStep ,
44
+ this . doRatchetKey ,
45
+ ) ;
46
+
28
47
}
29
48
30
49
this . rtcSession = rtcSession ;
@@ -34,17 +53,27 @@ export class MatrixKeyProvider extends BaseKeyProvider {
34
53
this . onEncryptionKeyChanged ,
35
54
) ;
36
55
56
+ this . rtcSession . on (
57
+ MatrixRTCSessionEvent . EncryptionKeyQueryRatchetStep ,
58
+ this . doRatchetKey ,
59
+ ) ;
60
+
61
+
37
62
// The new session could be aware of keys of which the old session wasn't,
38
63
// so emit key changed events
39
64
this . rtcSession . reemitEncryptionKeys ( ) ;
40
65
}
41
66
67
+ private doRatchetKey = ( participantId :string , keyIndex :number ) : void => {
68
+ this . ratchetKey ( participantId , keyIndex ) ;
69
+ }
70
+
42
71
private onEncryptionKeyChanged = (
43
72
encryptionKey : Uint8Array ,
44
73
encryptionKeyIndex : number ,
45
74
participantId : string ,
46
75
) : void => {
47
- createKeyMaterialFromBuffer ( encryptionKey ) . then (
76
+ importKey ( encryptionKey , "HKDF" , 'derive' ) . then (
48
77
( keyMaterial ) => {
49
78
this . onSetEncryptionKey ( keyMaterial , participantId , encryptionKeyIndex ) ;
50
79
0 commit comments