feat: Allow initiating multiple sessions for a message
This commit is contained in:
		
							parent
							
								
									399f8033a3
								
							
						
					
					
						commit
						30e3bd78cd
					
				@ -128,7 +128,7 @@ class OmemoSessionManager {
 | 
				
			|||||||
  
 | 
					  
 | 
				
			||||||
  /// Encrypt the key [plaintext] for all known bundles of [jid]. Returns a map that
 | 
					  /// Encrypt the key [plaintext] for all known bundles of [jid]. Returns a map that
 | 
				
			||||||
  /// maps the Bundle Id to the ciphertext of [plaintext].
 | 
					  /// maps the Bundle Id to the ciphertext of [plaintext].
 | 
				
			||||||
  Future<EncryptionResult> encryptToJid(String jid, String plaintext, { OmemoBundle? newSession }) async {
 | 
					  Future<EncryptionResult> encryptToJid(String jid, String plaintext, { List<OmemoBundle>? newSessions }) async {
 | 
				
			||||||
    final encryptedKeys = List<EncryptedKey>.empty(growable: true);
 | 
					    final encryptedKeys = List<EncryptedKey>.empty(growable: true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Generate the key and encrypt the plaintext
 | 
					    // Generate the key and encrypt the plaintext
 | 
				
			||||||
@ -142,9 +142,11 @@ class OmemoSessionManager {
 | 
				
			|||||||
    final hmac = await truncatedHmac(ciphertext, keys.authenticationKey);
 | 
					    final hmac = await truncatedHmac(ciphertext, keys.authenticationKey);
 | 
				
			||||||
    final concatKey = concat([key, hmac]);
 | 
					    final concatKey = concat([key, hmac]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    OmemoKeyExchange? kex;
 | 
					    final kex = <int, OmemoKeyExchange>{};
 | 
				
			||||||
    if (newSession != null) {
 | 
					    if (newSessions != null) {
 | 
				
			||||||
      kex = await addSessionFromBundle(jid, newSession.id, newSession);
 | 
					      for (final newSession in newSessions) {
 | 
				
			||||||
 | 
					        kex[newSession.id] = await addSessionFromBundle(jid, newSession.id, newSession);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    await _lock.synchronized(() async {
 | 
					    await _lock.synchronized(() async {
 | 
				
			||||||
@ -153,12 +155,13 @@ class OmemoSessionManager {
 | 
				
			|||||||
        final ratchet = _ratchetMap[deviceId]!;
 | 
					        final ratchet = _ratchetMap[deviceId]!;
 | 
				
			||||||
        final ciphertext = (await ratchet.ratchetEncrypt(concatKey)).ciphertext;
 | 
					        final ciphertext = (await ratchet.ratchetEncrypt(concatKey)).ciphertext;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (kex != null && deviceId == newSession?.id) {
 | 
					        if (kex.isNotEmpty && kex.containsKey(deviceId)) {
 | 
				
			||||||
          kex.message = OmemoAuthenticatedMessage.fromBuffer(ciphertext);
 | 
					          final k = kex[deviceId]!
 | 
				
			||||||
 | 
					            ..message = OmemoAuthenticatedMessage.fromBuffer(ciphertext);
 | 
				
			||||||
          encryptedKeys.add(
 | 
					          encryptedKeys.add(
 | 
				
			||||||
            EncryptedKey(
 | 
					            EncryptedKey(
 | 
				
			||||||
              deviceId,
 | 
					              deviceId,
 | 
				
			||||||
              base64.encode(kex.writeToBuffer()),
 | 
					              base64.encode(k.writeToBuffer()),
 | 
				
			||||||
              true,
 | 
					              true,
 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
          );
 | 
					          );
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,9 @@ void main() {
 | 
				
			|||||||
    final aliceMessage = await aliceSession.encryptToJid(
 | 
					    final aliceMessage = await aliceSession.encryptToJid(
 | 
				
			||||||
      bobJid,
 | 
					      bobJid,
 | 
				
			||||||
      messagePlaintext,
 | 
					      messagePlaintext,
 | 
				
			||||||
      newSession: await bobSession.device.toBundle(),
 | 
					      newSessions: [
 | 
				
			||||||
 | 
					        await bobSession.device.toBundle(),
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
    expect(aliceMessage.encryptedKeys.length, 1);
 | 
					    expect(aliceMessage.encryptedKeys.length, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -32,7 +34,63 @@ void main() {
 | 
				
			|||||||
    expect(messagePlaintext, bobMessage);
 | 
					    expect(messagePlaintext, bobMessage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Bob responds to Alice
 | 
					    // Bob responds to Alice
 | 
				
			||||||
    final bobResponseText = 'Oh, hello Alice!';
 | 
					    const bobResponseText = 'Oh, hello Alice!';
 | 
				
			||||||
 | 
					    final bobResponseMessage = await bobSession.encryptToJid(
 | 
				
			||||||
 | 
					      aliceJid,
 | 
				
			||||||
 | 
					      bobResponseText,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Bob sends the message to Alice
 | 
				
			||||||
 | 
					    // ...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Alice decrypts it
 | 
				
			||||||
 | 
					    final aliceReceivedMessage = await aliceSession.decryptMessage(
 | 
				
			||||||
 | 
					      bobResponseMessage.ciphertext,
 | 
				
			||||||
 | 
					      bobJid,
 | 
				
			||||||
 | 
					      bobSession.device.id,
 | 
				
			||||||
 | 
					      bobResponseMessage.encryptedKeys,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    expect(bobResponseText, aliceReceivedMessage);
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  test('Test using OMEMO sessions with only two devices for the receiver', () async {
 | 
				
			||||||
 | 
					    const aliceJid = 'alice@server.example';
 | 
				
			||||||
 | 
					    const bobJid = 'bob@other.server.example';
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					    // Alice and Bob generate their sessions
 | 
				
			||||||
 | 
					    final aliceSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
 | 
				
			||||||
 | 
					    final bobSession = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
 | 
				
			||||||
 | 
					    // Bob's other device
 | 
				
			||||||
 | 
					    final bobSession2 = await OmemoSessionManager.generateNewIdentity(opkAmount: 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Alice encrypts a message for Bob
 | 
				
			||||||
 | 
					    const messagePlaintext = 'Hello Bob!';
 | 
				
			||||||
 | 
					    final aliceMessage = await aliceSession.encryptToJid(
 | 
				
			||||||
 | 
					      bobJid,
 | 
				
			||||||
 | 
					      messagePlaintext,
 | 
				
			||||||
 | 
					      newSessions: [
 | 
				
			||||||
 | 
					        await bobSession.device.toBundle(),
 | 
				
			||||||
 | 
					        await bobSession2.device.toBundle(),
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    expect(aliceMessage.encryptedKeys.length, 2);
 | 
				
			||||||
 | 
					    expect(aliceMessage.encryptedKeys[0].kex, true);
 | 
				
			||||||
 | 
					    expect(aliceMessage.encryptedKeys[1].kex, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Alice sends the message to Bob
 | 
				
			||||||
 | 
					    // ...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Bob decrypts it
 | 
				
			||||||
 | 
					    final bobMessage = await bobSession.decryptMessage(
 | 
				
			||||||
 | 
					      aliceMessage.ciphertext,
 | 
				
			||||||
 | 
					      aliceJid,
 | 
				
			||||||
 | 
					      aliceSession.device.id,
 | 
				
			||||||
 | 
					      aliceMessage.encryptedKeys,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    expect(messagePlaintext, bobMessage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Bob responds to Alice
 | 
				
			||||||
 | 
					    const bobResponseText = 'Oh, hello Alice!';
 | 
				
			||||||
    final bobResponseMessage = await bobSession.encryptToJid(
 | 
					    final bobResponseMessage = await bobSession.encryptToJid(
 | 
				
			||||||
      aliceJid,
 | 
					      aliceJid,
 | 
				
			||||||
      bobResponseText,
 | 
					      bobResponseText,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user