1
1
/*
2
2
Copyright 2016 OpenMarket Ltd
3
- Copyright 2019-2022 The Matrix.org Foundation C.I.C.
3
+ Copyright 2019-2023 The Matrix.org Foundation C.I.C.
4
4
5
5
Licensed under the Apache License, Version 2.0 (the "License");
6
6
you may not use this file except in compliance with the License.
@@ -59,13 +59,21 @@ interface ToDeviceEvent {
59
59
type : string ;
60
60
}
61
61
62
- // encrypt an event with olm
62
+ /** encrypt an event with an existing olm session */
63
63
function encryptOlmEvent ( opts : {
64
+ /** the sender's user id */
64
65
sender ?: string ;
66
+ /** the sender's curve25519 key */
65
67
senderKey : string ;
68
+ /** the sender's ed25519 key */
69
+ senderSigningKey : string ;
70
+ /** the olm session to use for encryption */
66
71
p2pSession : Olm . Session ;
72
+ /** the recipient client */
67
73
recipient : TestClient ;
74
+ /** the payload of the message */
68
75
plaincontent ?: object ;
76
+ /** the event type of the payload */
69
77
plaintype ?: string ;
70
78
} ) : ToDeviceEvent {
71
79
expect ( opts . senderKey ) . toBeTruthy ( ) ;
@@ -78,6 +86,9 @@ function encryptOlmEvent(opts: {
78
86
recipient_keys : {
79
87
ed25519 : opts . recipient . getSigningKey ( ) ,
80
88
} ,
89
+ keys : {
90
+ ed25519 : opts . senderSigningKey ,
91
+ } ,
81
92
sender : opts . sender || "@bob:xyz" ,
82
93
type : opts . plaintype || "m.test" ,
83
94
} ;
@@ -101,7 +112,7 @@ function encryptMegolmEvent(opts: {
101
112
groupSession : Olm . OutboundGroupSession ;
102
113
plaintext ?: Partial < IEvent > ;
103
114
room_id ?: string ;
104
- } ) : Pick < IEvent , "event_id" | "content" | "type" > {
115
+ } ) : IEvent {
105
116
expect ( opts . senderKey ) . toBeTruthy ( ) ;
106
117
expect ( opts . groupSession ) . toBeTruthy ( ) ;
107
118
@@ -119,30 +130,44 @@ function encryptMegolmEvent(opts: {
119
130
expect ( opts . room_id ) . toBeTruthy ( ) ;
120
131
plaintext . room_id = opts . room_id ;
121
132
}
133
+ return encryptMegolmEventRawPlainText ( { senderKey : opts . senderKey , groupSession : opts . groupSession , plaintext } ) ;
134
+ }
122
135
136
+ function encryptMegolmEventRawPlainText ( opts : {
137
+ senderKey : string ;
138
+ groupSession : Olm . OutboundGroupSession ;
139
+ plaintext : Partial < IEvent > ;
140
+ } ) : IEvent {
123
141
return {
124
- event_id : "test_megolm_event_" + Math . random ( ) ,
142
+ event_id : "$test_megolm_event_" + Math . random ( ) ,
143
+ sender : "@not_the_real_sender:example.com" ,
144
+ origin_server_ts : 1672944778000 ,
125
145
content : {
126
146
algorithm : "m.megolm.v1.aes-sha2" ,
127
- ciphertext : opts . groupSession . encrypt ( JSON . stringify ( plaintext ) ) ,
147
+ ciphertext : opts . groupSession . encrypt ( JSON . stringify ( opts . plaintext ) ) ,
128
148
device_id : "testDevice" ,
129
149
sender_key : opts . senderKey ,
130
150
session_id : opts . groupSession . session_id ( ) ,
131
151
} ,
132
152
type : "m.room.encrypted" ,
153
+ unsigned : { } ,
133
154
} ;
134
155
}
135
156
136
- // build an encrypted room_key event to share a group session
157
+ /** build an encrypted room_key event to share a group session, using an existing olm session */
137
158
function encryptGroupSessionKey ( opts : {
138
- senderKey : string ;
139
159
recipient : TestClient ;
160
+ /** sender's olm account */
161
+ olmAccount : Olm . Account ;
162
+ /** sender's olm session with the recipient */
140
163
p2pSession : Olm . Session ;
141
164
groupSession : Olm . OutboundGroupSession ;
142
165
room_id ?: string ;
143
166
} ) : Partial < IEvent > {
167
+ const senderKeys = JSON . parse ( opts . olmAccount . identity_keys ( ) ) ;
144
168
return encryptOlmEvent ( {
145
- senderKey : opts . senderKey ,
169
+ senderKey : senderKeys . curve25519 ,
170
+ senderSigningKey : senderKeys . ed25519 ,
146
171
recipient : opts . recipient ,
147
172
p2pSession : opts . p2pSession ,
148
173
plaincontent : {
@@ -219,6 +244,7 @@ async function establishOlmSession(testClient: TestClient, peerOlmAccount: Olm.A
219
244
const p2pSession = await createOlmSession ( peerOlmAccount , testClient ) ;
220
245
const olmEvent = encryptOlmEvent ( {
221
246
senderKey : peerE2EKeys . curve25519 ,
247
+ senderSigningKey : peerE2EKeys . ed25519 ,
222
248
recipient : testClient ,
223
249
p2pSession : p2pSession ,
224
250
} ) ;
@@ -392,7 +418,9 @@ describe("megolm", () => {
392
418
testSenderKey = testE2eKeys . curve25519 ;
393
419
} ) ;
394
420
395
- afterEach ( ( ) => aliceTestClient . stop ( ) ) ;
421
+ afterEach ( async ( ) => {
422
+ await aliceTestClient . stop ( ) ;
423
+ } ) ;
396
424
397
425
it ( "Alice receives a megolm message" , async ( ) => {
398
426
await aliceTestClient . start ( ) ;
@@ -405,8 +433,8 @@ describe("megolm", () => {
405
433
406
434
// make the room_key event
407
435
const roomKeyEncrypted = encryptGroupSessionKey ( {
408
- senderKey : testSenderKey ,
409
436
recipient : aliceTestClient ,
437
+ olmAccount : testOlmAccount ,
410
438
p2pSession : p2pSession ,
411
439
groupSession : groupSession ,
412
440
room_id : ROOM_ID ,
@@ -456,8 +484,8 @@ describe("megolm", () => {
456
484
457
485
// make the room_key event, but don't send it yet
458
486
const roomKeyEncrypted = encryptGroupSessionKey ( {
459
- senderKey : testSenderKey ,
460
487
recipient : aliceTestClient ,
488
+ olmAccount : testOlmAccount ,
461
489
p2pSession : p2pSession ,
462
490
groupSession : groupSession ,
463
491
room_id : ROOM_ID ,
@@ -516,8 +544,8 @@ describe("megolm", () => {
516
544
517
545
// make the room_key event
518
546
const roomKeyEncrypted1 = encryptGroupSessionKey ( {
519
- senderKey : testSenderKey ,
520
547
recipient : aliceTestClient ,
548
+ olmAccount : testOlmAccount ,
521
549
p2pSession : p2pSession ,
522
550
groupSession : groupSession ,
523
551
room_id : ROOM_ID ,
@@ -533,8 +561,8 @@ describe("megolm", () => {
533
561
// make a second room_key event now that we have advanced the group
534
562
// session.
535
563
const roomKeyEncrypted2 = encryptGroupSessionKey ( {
536
- senderKey : testSenderKey ,
537
564
recipient : aliceTestClient ,
565
+ olmAccount : testOlmAccount ,
538
566
p2pSession : p2pSession ,
539
567
groupSession : groupSession ,
540
568
room_id : ROOM_ID ,
@@ -958,8 +986,8 @@ describe("megolm", () => {
958
986
959
987
// make the room_key event
960
988
const roomKeyEncrypted = encryptGroupSessionKey ( {
961
- senderKey : testSenderKey ,
962
989
recipient : aliceTestClient ,
990
+ olmAccount : testOlmAccount ,
963
991
p2pSession : p2pSession ,
964
992
groupSession : groupSession ,
965
993
room_id : ROOM_ID ,
@@ -1088,8 +1116,8 @@ describe("megolm", () => {
1088
1116
1089
1117
// make the room_key event
1090
1118
const roomKeyEncrypted = encryptGroupSessionKey ( {
1091
- senderKey : testSenderKey ,
1092
1119
recipient : aliceTestClient ,
1120
+ olmAccount : testOlmAccount ,
1093
1121
p2pSession : p2pSession ,
1094
1122
groupSession : groupSession ,
1095
1123
room_id : ROOM_ID ,
@@ -1101,17 +1129,11 @@ describe("megolm", () => {
1101
1129
room_id : ROOM_ID ,
1102
1130
} ;
1103
1131
1104
- const messageEncrypted = {
1105
- event_id : "test_megolm_event" ,
1106
- content : {
1107
- algorithm : "m.megolm.v1.aes-sha2" ,
1108
- ciphertext : groupSession . encrypt ( JSON . stringify ( plaintext ) ) ,
1109
- device_id : "testDevice" ,
1110
- sender_key : testSenderKey ,
1111
- session_id : groupSession . session_id ( ) ,
1112
- } ,
1113
- type : "m.room.encrypted" ,
1114
- } ;
1132
+ const messageEncrypted = encryptMegolmEventRawPlainText ( {
1133
+ senderKey : testSenderKey ,
1134
+ groupSession : groupSession ,
1135
+ plaintext : plaintext ,
1136
+ } ) ;
1115
1137
1116
1138
// Alice gets both the events in a single sync
1117
1139
const syncResponse = {
@@ -1149,8 +1171,8 @@ describe("megolm", () => {
1149
1171
1150
1172
// make the room_key event
1151
1173
const roomKeyEncrypted = encryptGroupSessionKey ( {
1152
- senderKey : testSenderKey ,
1153
1174
recipient : aliceTestClient ,
1175
+ olmAccount : testOlmAccount ,
1154
1176
p2pSession : p2pSession ,
1155
1177
groupSession : groupSession ,
1156
1178
room_id : ROOM_ID ,
@@ -1268,6 +1290,7 @@ describe("megolm", () => {
1268
1290
) ;
1269
1291
const encryptedForwardedKey = encryptOlmEvent ( {
1270
1292
sender : "@becca:localhost" ,
1293
+ senderSigningKey : beccaTestClient . getSigningKey ( ) ,
1271
1294
senderKey : beccaTestClient . getDeviceKey ( ) ,
1272
1295
recipient : aliceTestClient ,
1273
1296
p2pSession : p2pSession ,
@@ -1413,6 +1436,7 @@ describe("megolm", () => {
1413
1436
const encryptedForwardedKey = encryptOlmEvent ( {
1414
1437
sender : "@becca:localhost" ,
1415
1438
senderKey : beccaTestClient . getDeviceKey ( ) ,
1439
+ senderSigningKey : beccaTestClient . getSigningKey ( ) ,
1416
1440
recipient : aliceTestClient ,
1417
1441
p2pSession : p2pSession ,
1418
1442
plaincontent : {
0 commit comments