crypto: Send out to-device requests to share the room key
This commit is contained in:
		
							parent
							
								
									4c44a5e108
								
							
						
					
					
						commit
						5f848093b9
					
				@ -91,6 +91,7 @@ import org.matrix.android.sdk.internal.crypto.tasks.GetDeviceInfoTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.NewUploadKeysTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask
 | 
			
		||||
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
 | 
			
		||||
@ -174,6 +175,7 @@ internal class DefaultCryptoService @Inject constructor(
 | 
			
		||||
        // Tasks
 | 
			
		||||
        private val getDevicesTask: GetDevicesTask,
 | 
			
		||||
        private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
 | 
			
		||||
        private val sendToDeviceTask: SendToDeviceTask,
 | 
			
		||||
        private val downloadKeysForUsersTask: DownloadKeysForUsersTask,
 | 
			
		||||
        private val getDeviceInfoTask: GetDeviceInfoTask,
 | 
			
		||||
        private val setDeviceNameTask: SetDeviceNameTask,
 | 
			
		||||
@ -962,9 +964,9 @@ internal class DefaultCryptoService @Inject constructor(
 | 
			
		||||
 | 
			
		||||
    private suspend fun preshareGroupSession(roomId: String, roomMembers: List<String>) {
 | 
			
		||||
        val request = olmMachine!!.getMissingSessions(roomMembers)
 | 
			
		||||
        roomId == "est"
 | 
			
		||||
 | 
			
		||||
        if (request != null) {
 | 
			
		||||
            // This request can only be a keys claim request.
 | 
			
		||||
            when (request) {
 | 
			
		||||
                is Request.KeysClaim -> {
 | 
			
		||||
                    val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys)
 | 
			
		||||
@ -975,6 +977,23 @@ internal class DefaultCryptoService @Inject constructor(
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (toDeviceRequest in olmMachine!!.shareGroupSession(roomId, roomMembers)) {
 | 
			
		||||
            // This request can only be a to-device request.
 | 
			
		||||
            when (toDeviceRequest) {
 | 
			
		||||
                is Request.ToDevice -> {
 | 
			
		||||
                    val adapter = MoshiProvider.providesMoshi().adapter<Map<String, HashMap<String, Any>>>(Map::class.java)
 | 
			
		||||
                    val body = adapter.fromJson(toDeviceRequest.body)!!
 | 
			
		||||
 | 
			
		||||
                    val userMap = MXUsersDevicesMap<Any>()
 | 
			
		||||
                    userMap.join(body)
 | 
			
		||||
 | 
			
		||||
                    val sendToDeviceParams = SendToDeviceTask.Params(toDeviceRequest.eventType, userMap)
 | 
			
		||||
                    sendToDeviceTask.execute(sendToDeviceParams)
 | 
			
		||||
                    olmMachine!!.markRequestAsSent(toDeviceRequest.requestId, RequestType.TO_DEVICE, "{}")
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // private suspend fun encrypt(roomId: String, eventType: String, content: Content) {
 | 
			
		||||
 | 
			
		||||
@ -115,6 +115,10 @@ class MXUsersDevicesMap<E> {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun join(other: Map<out String, HashMap<String, E>>) {
 | 
			
		||||
        map.putAll(other)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun toString(): String {
 | 
			
		||||
        return "MXUsersDevicesMap $map"
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -87,6 +87,10 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
 | 
			
		||||
        inner.outgoingRequests()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    suspend fun shareGroupSession(roomId: String, users: List<String>): List<Request> = withContext(Dispatchers.IO) {
 | 
			
		||||
        inner.shareGroupSession(roomId, users)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    suspend fun getMissingSessions(users: List<String>): Request? = withContext(Dispatchers.IO) {
 | 
			
		||||
        inner.getMissingSessions(users)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,8 @@ use matrix_sdk_common::{
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use matrix_sdk_crypto::{
 | 
			
		||||
    IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest,
 | 
			
		||||
    EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest,
 | 
			
		||||
    ToDeviceRequest,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError};
 | 
			
		||||
@ -358,6 +359,25 @@ impl OlmMachine {
 | 
			
		||||
            .block_on(self.inner.update_tracked_users(users.iter()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn share_group_session(&self, room_id: &str, users: Vec<String>) -> Vec<Request> {
 | 
			
		||||
        let users: Vec<UserId> = users
 | 
			
		||||
            .into_iter()
 | 
			
		||||
            .filter_map(|u| UserId::try_from(u).ok())
 | 
			
		||||
            .collect();
 | 
			
		||||
 | 
			
		||||
        let room_id = RoomId::try_from(room_id).unwrap();
 | 
			
		||||
        let requests = self
 | 
			
		||||
            .runtime
 | 
			
		||||
            .block_on(self.inner.share_group_session(
 | 
			
		||||
                &room_id,
 | 
			
		||||
                users.iter(),
 | 
			
		||||
                EncryptionSettings::default(),
 | 
			
		||||
            ))
 | 
			
		||||
            .unwrap();
 | 
			
		||||
 | 
			
		||||
        requests.into_iter().map(|r| (&*r).into()).collect()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn get_missing_sessions(
 | 
			
		||||
        &self,
 | 
			
		||||
        users: Vec<String>,
 | 
			
		||||
 | 
			
		||||
@ -84,11 +84,13 @@ interface OlmMachine {
 | 
			
		||||
 | 
			
		||||
    Device? get_device([ByRef] string user_id, [ByRef] string device_id);
 | 
			
		||||
    sequence<Device> get_user_devices([ByRef] string user_id);
 | 
			
		||||
    sequence<Request> outgoing_requests();
 | 
			
		||||
    void update_tracked_users(sequence<string> users);
 | 
			
		||||
 | 
			
		||||
    sequence<Request> outgoing_requests();
 | 
			
		||||
 | 
			
		||||
    void update_tracked_users(sequence<string> users);
 | 
			
		||||
    [Throws=CryptoStoreError]
 | 
			
		||||
    Request? get_missing_sessions(sequence<string> users);
 | 
			
		||||
    sequence<Request> share_group_session([ByRef] string room_id, sequence<string> users);
 | 
			
		||||
 | 
			
		||||
    [Throws=CryptoStoreError]
 | 
			
		||||
    void mark_request_as_sent(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user