mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 10:25:35 +03:00
Update rust-crypto library
This commit is contained in:
parent
859d47453c
commit
69ede523b6
10 changed files with 82 additions and 78 deletions
|
@ -20,8 +20,6 @@ interface SasVerificationTransaction : VerificationTransaction {
|
|||
|
||||
fun supportsEmoji(): Boolean
|
||||
|
||||
fun supportsDecimal(): Boolean
|
||||
|
||||
fun getEmojiCodeRepresentation(): List<EmojiRepresentation>
|
||||
|
||||
fun getDecimalCodeRepresentation(): String
|
||||
|
|
|
@ -172,7 +172,7 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
}
|
||||
} else {
|
||||
cryptoCoroutineScope.launch {
|
||||
verificationService.onEvent(event)
|
||||
verificationService.onEvent(roomId, event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -696,7 +696,7 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
this.keysBackupService.onSecretKeyGossip(secretContent.secretValue)
|
||||
}
|
||||
else -> {
|
||||
this.verificationService.onEvent(event)
|
||||
this.verificationService.onEvent(null, event)
|
||||
}
|
||||
}
|
||||
liveEventManager.get().dispatchOnLiveToDevice(event)
|
||||
|
@ -799,8 +799,8 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
|
||||
private suspend fun signatureUpload(request: Request.SignatureUpload) {
|
||||
try {
|
||||
requestSender.sendSignatureUpload(request)
|
||||
olmMachine.markRequestAsSent(request.requestId, RequestType.SIGNATURE_UPLOAD, "{}")
|
||||
val response = requestSender.sendSignatureUpload(request)
|
||||
olmMachine.markRequestAsSent(request.requestId, RequestType.SIGNATURE_UPLOAD, response)
|
||||
} catch (throwable: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(throwable, "## CRYPTO signatureUpload(): error")
|
||||
}
|
||||
|
@ -808,7 +808,9 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
|
||||
private suspend fun sendRoomMessage(request: Request.RoomMessage) {
|
||||
try {
|
||||
requestSender.sendRoomMessage(request)
|
||||
Timber.v("SendRoomMessage: $request")
|
||||
val response = requestSender.sendRoomMessage(request)
|
||||
olmMachine.markRequestAsSent(request.requestId, RequestType.ROOM_MESSAGE, response)
|
||||
} catch (throwable: Throwable) {
|
||||
Timber.tag(loggerTag.value).e(throwable, "## CRYPTO sendRoomMessage(): error")
|
||||
}
|
||||
|
@ -817,6 +819,7 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
private suspend fun sendOutgoingRequests() {
|
||||
outgoingRequestsLock.withLock {
|
||||
coroutineScope {
|
||||
Timber.v("OutgoingRequests: ${olmMachine.outgoingRequests()}")
|
||||
olmMachine.outgoingRequests().map {
|
||||
when (it) {
|
||||
is Request.KeysUpload -> {
|
||||
|
@ -992,13 +995,13 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
is Request.ToDevice -> {
|
||||
sendToDevice(cancellation)
|
||||
}
|
||||
else -> Unit
|
||||
else -> Unit
|
||||
}
|
||||
when (request) {
|
||||
is Request.ToDevice -> {
|
||||
sendToDevice(request)
|
||||
}
|
||||
else -> Unit
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ internal class OlmMachine(
|
|||
private val coroutineDispatchers: MatrixCoroutineDispatchers,
|
||||
private val moshi: Moshi
|
||||
) {
|
||||
private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString())
|
||||
private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString(), null)
|
||||
internal val verificationListeners = ArrayList<VerificationService.Listener>()
|
||||
private val flowCollectors = FlowCollectors()
|
||||
|
||||
|
@ -251,6 +251,12 @@ internal class OlmMachine(
|
|||
return response
|
||||
}
|
||||
|
||||
suspend fun receiveUnencryptedVerificationEvent(roomId: String, event: Event) = withContext(coroutineDispatchers.io) {
|
||||
val adapter = moshi.adapter(Event::class.java)
|
||||
val serializedEvent = adapter.toJson(event)
|
||||
inner.receiveUnencryptedVerificationEvent(serializedEvent, roomId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the given list of users to be tracked, triggering a key query request for them.
|
||||
*
|
||||
|
|
|
@ -69,7 +69,7 @@ internal class QrCodeVerification(
|
|||
|
||||
/** Pass the data from a scanned QR code into the QR code verification object */
|
||||
override suspend fun userHasScannedOtherQrCode(otherQrCodeText: String) {
|
||||
request.scanQrCode(otherQrCodeText)
|
||||
request.scanQrCode(otherQrCodeText)
|
||||
dispatchTxUpdated()
|
||||
}
|
||||
|
||||
|
@ -178,14 +178,14 @@ internal class QrCodeVerification(
|
|||
}
|
||||
|
||||
if (result != null) {
|
||||
sender.sendVerificationRequest(result.request)
|
||||
dispatchTxUpdated()
|
||||
|
||||
for (verificationRequest in result.requests) {
|
||||
sender.sendVerificationRequest(verificationRequest)
|
||||
}
|
||||
val signatureRequest = result.signatureRequest
|
||||
|
||||
if (signatureRequest != null) {
|
||||
sender.sendSignatureUpload(signatureRequest)
|
||||
}
|
||||
dispatchTxUpdated()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@ import org.matrix.android.sdk.internal.crypto.model.rest.KeysClaimResponse
|
|||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysQueryResponse
|
||||
import org.matrix.android.sdk.internal.crypto.model.rest.KeysUploadResponse
|
||||
import org.matrix.android.sdk.internal.crypto.model.rest.RestKeyInfo
|
||||
import org.matrix.android.sdk.internal.crypto.model.rest.SignatureUploadResponse
|
||||
import org.matrix.android.sdk.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask
|
||||
import org.matrix.android.sdk.internal.crypto.tasks.DefaultSendVerificationMessageTask
|
||||
import org.matrix.android.sdk.internal.crypto.tasks.DownloadKeysForUsersTask
|
||||
|
@ -57,6 +58,8 @@ import org.matrix.android.sdk.internal.crypto.tasks.UploadSignaturesTask
|
|||
import org.matrix.android.sdk.internal.crypto.tasks.UploadSigningKeysTask
|
||||
import org.matrix.android.sdk.internal.di.MoshiProvider
|
||||
import org.matrix.android.sdk.internal.network.parsing.CheckNumberType
|
||||
import org.matrix.android.sdk.internal.session.room.send.SendResponse
|
||||
import org.matrix.android.sdk.internal.session.room.send.queue.EventSenderProcessor
|
||||
import timber.log.Timber
|
||||
import uniffi.olm.OutgoingVerificationRequest
|
||||
import uniffi.olm.Request
|
||||
|
@ -120,35 +123,39 @@ internal class RequestSender @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun sendRoomMessage(request: OutgoingVerificationRequest.InRoom): String {
|
||||
private suspend fun sendRoomMessage(request: OutgoingVerificationRequest.InRoom): SendResponse {
|
||||
return sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId)
|
||||
}
|
||||
|
||||
suspend fun sendRoomMessage(request: Request.RoomMessage): String {
|
||||
return sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId)
|
||||
val sendResponse = sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId)
|
||||
val responseAdapter = moshi.adapter(SendResponse::class.java)
|
||||
return responseAdapter.toJson(sendResponse)
|
||||
}
|
||||
|
||||
suspend fun sendRoomMessage(eventType: String, roomId: String, content: String, transactionId: String): String {
|
||||
val adapter = moshi.adapter<Content>(Map::class.java)
|
||||
val jsonContent = adapter.fromJson(content)
|
||||
suspend fun sendRoomMessage(eventType: String, roomId: String, content: String, transactionId: String): SendResponse {
|
||||
val paramsAdapter = moshi.adapter<Content>(Map::class.java)
|
||||
val jsonContent = paramsAdapter.fromJson(content)
|
||||
val event = Event(eventType, transactionId, jsonContent, roomId = roomId)
|
||||
val params = SendVerificationMessageTask.Params(event)
|
||||
return this.sendVerificationMessageTask.get().executeRetry(params, REQUEST_RETRY_COUNT)
|
||||
return sendVerificationMessageTask.get().executeRetry(params, REQUEST_RETRY_COUNT)
|
||||
}
|
||||
|
||||
suspend fun sendSignatureUpload(request: Request.SignatureUpload) {
|
||||
sendSignatureUpload(request.body)
|
||||
suspend fun sendSignatureUpload(request: Request.SignatureUpload): String {
|
||||
return sendSignatureUpload(request.body)
|
||||
}
|
||||
|
||||
suspend fun sendSignatureUpload(request: SignatureUploadRequest) {
|
||||
sendSignatureUpload(request.body)
|
||||
suspend fun sendSignatureUpload(request: SignatureUploadRequest): String {
|
||||
return sendSignatureUpload(request.body)
|
||||
}
|
||||
|
||||
private suspend fun sendSignatureUpload(body: String) {
|
||||
val adapter = moshi.adapter<Map<String, Map<String, Any>>>(Map::class.java)
|
||||
val signatures = adapter.fromJson(body)!!
|
||||
private suspend fun sendSignatureUpload(body: String): String {
|
||||
val paramsAdapter = moshi.adapter<Map<String, Map<String, Any>>>(Map::class.java)
|
||||
val signatures = paramsAdapter.fromJson(body)!!
|
||||
val params = UploadSignaturesTask.Params(signatures)
|
||||
this.signaturesUploadTask.executeRetry(params, REQUEST_RETRY_COUNT)
|
||||
val response = signaturesUploadTask.executeRetry(params, REQUEST_RETRY_COUNT)
|
||||
val responseAdapter = moshi.adapter(SignatureUploadResponse::class.java)
|
||||
return responseAdapter.toJson(response)!!
|
||||
}
|
||||
|
||||
suspend fun uploadCrossSigningKeys(
|
||||
|
|
|
@ -130,14 +130,6 @@ internal class SasVerification(
|
|||
/** Is this verification happening over to-device messages */
|
||||
override fun isToDeviceTransport(): Boolean = inner.roomId == null
|
||||
|
||||
/** Does the verification flow support showing decimals as the short auth string */
|
||||
override fun supportsDecimal(): Boolean {
|
||||
// This is ignored anyways, throw it away?
|
||||
// The spec also mandates that devices support at least decimal and
|
||||
// the rust-sdk cancels if devices don't support it
|
||||
return true
|
||||
}
|
||||
|
||||
/** Does the verification flow support showing emojis as the short auth string */
|
||||
override fun supportsEmoji(): Boolean {
|
||||
refreshData()
|
||||
|
@ -207,17 +199,17 @@ internal class SasVerification(
|
|||
val result = withContext(coroutineDispatchers.io) {
|
||||
machine.confirmVerification(inner.otherUserId, inner.flowId)
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
sender.sendVerificationRequest(result.request)
|
||||
dispatchTxUpdated()
|
||||
|
||||
for (verificationRequest in result.requests) {
|
||||
sender.sendVerificationRequest(verificationRequest)
|
||||
}
|
||||
val signatureRequest = result.signatureRequest
|
||||
|
||||
if (signatureRequest != null) {
|
||||
sender.sendSignatureUpload(signatureRequest)
|
||||
}
|
||||
dispatchTxUpdated()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private suspend fun cancelHelper(code: CancelCode) {
|
||||
|
|
|
@ -236,7 +236,7 @@ internal class UserIdentity(
|
|||
val stringMethods = prepareMethods(methods)
|
||||
val content = olmMachine.inner().verificationRequestContent(userId, stringMethods)!!
|
||||
|
||||
val eventID = requestSender.sendRoomMessage(EventType.MESSAGE, roomId, content, transactionId)
|
||||
val eventID = requestSender.sendRoomMessage(EventType.MESSAGE, roomId, content, transactionId).eventId
|
||||
|
||||
val innerRequest = olmMachine.inner().requestVerification(userId, roomId, eventID, stringMethods)!!
|
||||
|
||||
|
|
|
@ -22,11 +22,12 @@ import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
|
|||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.session.room.RoomAPI
|
||||
import org.matrix.android.sdk.internal.session.room.send.LocalEchoRepository
|
||||
import org.matrix.android.sdk.internal.session.room.send.SendResponse
|
||||
import org.matrix.android.sdk.internal.task.Task
|
||||
import org.matrix.android.sdk.internal.util.toMatrixErrorStr
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface SendVerificationMessageTask : Task<SendVerificationMessageTask.Params, String> {
|
||||
internal interface SendVerificationMessageTask : Task<SendVerificationMessageTask.Params, SendResponse> {
|
||||
data class Params(
|
||||
val event: Event
|
||||
)
|
||||
|
@ -39,10 +40,9 @@ internal class DefaultSendVerificationMessageTask @Inject constructor(
|
|||
private val cryptoSessionInfoProvider: CryptoSessionInfoProvider,
|
||||
private val globalErrorReceiver: GlobalErrorReceiver) : SendVerificationMessageTask {
|
||||
|
||||
override suspend fun execute(params: SendVerificationMessageTask.Params): String {
|
||||
override suspend fun execute(params: SendVerificationMessageTask.Params): SendResponse {
|
||||
val event = handleEncryption(params)
|
||||
val localId = event.eventId!!
|
||||
|
||||
try {
|
||||
localEchoRepository.updateSendState(localId, event.roomId, SendState.SENDING)
|
||||
val response = executeRequest(globalErrorReceiver) {
|
||||
|
@ -54,7 +54,7 @@ internal class DefaultSendVerificationMessageTask @Inject constructor(
|
|||
)
|
||||
}
|
||||
localEchoRepository.updateSendState(localId, event.roomId, SendState.SENT)
|
||||
return response.eventId
|
||||
return response
|
||||
} catch (e: Throwable) {
|
||||
localEchoRepository.updateSendState(localId, event.roomId, SendState.UNDELIVERED, e.toMatrixErrorStr())
|
||||
throw e
|
||||
|
|
|
@ -15,14 +15,14 @@
|
|||
*/
|
||||
package org.matrix.android.sdk.internal.crypto.tasks
|
||||
|
||||
import org.matrix.android.sdk.api.failure.Failure
|
||||
import org.matrix.android.sdk.internal.crypto.api.CryptoApi
|
||||
import org.matrix.android.sdk.internal.crypto.model.rest.SignatureUploadResponse
|
||||
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.task.Task
|
||||
import javax.inject.Inject
|
||||
|
||||
internal interface UploadSignaturesTask : Task<UploadSignaturesTask.Params, Unit> {
|
||||
internal interface UploadSignaturesTask : Task<UploadSignaturesTask.Params, SignatureUploadResponse> {
|
||||
data class Params(
|
||||
val signatures: Map<String, Map<String, Any>>
|
||||
)
|
||||
|
@ -33,21 +33,13 @@ internal class DefaultUploadSignaturesTask @Inject constructor(
|
|||
private val globalErrorReceiver: GlobalErrorReceiver
|
||||
) : UploadSignaturesTask {
|
||||
|
||||
override suspend fun execute(params: UploadSignaturesTask.Params) {
|
||||
try {
|
||||
val response = executeRequest(
|
||||
globalErrorReceiver,
|
||||
canRetry = true,
|
||||
maxRetriesCount = 10
|
||||
) {
|
||||
cryptoApi.uploadSignatures(params.signatures)
|
||||
}
|
||||
if (response.failures?.isNotEmpty() == true) {
|
||||
throw Throwable(response.failures.toString())
|
||||
}
|
||||
return
|
||||
} catch (f: Failure) {
|
||||
throw f
|
||||
override suspend fun execute(params: UploadSignaturesTask.Params): SignatureUploadResponse {
|
||||
return executeRequest(
|
||||
globalErrorReceiver,
|
||||
canRetry = true,
|
||||
maxRetriesCount = 10
|
||||
) {
|
||||
cryptoApi.uploadSignatures(params.signatures)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,27 +79,33 @@ internal class RustVerificationService @Inject constructor(private val olmMachin
|
|||
|
||||
private val dispatcher = UpdateDispatcher(olmMachine.verificationListeners)
|
||||
|
||||
/** The main entry point for the verification service
|
||||
/**
|
||||
*
|
||||
* All verification related events should be forwarded through this method to
|
||||
* the verification service.
|
||||
*
|
||||
* Since events are at this point already handled by the rust-sdk through the receival
|
||||
* of the to-device events and the decryption of room events, this method mainly just
|
||||
* If the verification event is not encrypted it should be provided to the olmMachine.
|
||||
* Otherwise events are at this point already handled by the rust-sdk through the receival
|
||||
* of the to-device events and the decryption of room events. In this case this method mainly just
|
||||
* fetches the appropriate rust object that will be created or updated by the event and
|
||||
* dispatches updates to our listeners.
|
||||
*/
|
||||
internal suspend fun onEvent(event: Event) = when (event.getClearType()) {
|
||||
EventType.KEY_VERIFICATION_REQUEST -> onRequest(event, fromRoomMessage = false)
|
||||
EventType.KEY_VERIFICATION_START -> onStart(event)
|
||||
EventType.KEY_VERIFICATION_READY,
|
||||
EventType.KEY_VERIFICATION_ACCEPT,
|
||||
EventType.KEY_VERIFICATION_KEY,
|
||||
EventType.KEY_VERIFICATION_MAC,
|
||||
EventType.KEY_VERIFICATION_CANCEL,
|
||||
EventType.KEY_VERIFICATION_DONE -> onUpdate(event)
|
||||
EventType.MESSAGE -> onRoomMessage(event)
|
||||
else -> Unit
|
||||
internal suspend fun onEvent(roomId: String?, event: Event) {
|
||||
if (roomId != null && !event.isEncrypted()) {
|
||||
olmMachine.receiveUnencryptedVerificationEvent(roomId, event)
|
||||
}
|
||||
when (event.getClearType()) {
|
||||
EventType.KEY_VERIFICATION_REQUEST -> onRequest(event, fromRoomMessage = false)
|
||||
EventType.KEY_VERIFICATION_START -> onStart(event)
|
||||
EventType.KEY_VERIFICATION_READY,
|
||||
EventType.KEY_VERIFICATION_ACCEPT,
|
||||
EventType.KEY_VERIFICATION_KEY,
|
||||
EventType.KEY_VERIFICATION_MAC,
|
||||
EventType.KEY_VERIFICATION_CANCEL,
|
||||
EventType.KEY_VERIFICATION_DONE -> onUpdate(event)
|
||||
EventType.MESSAGE -> onRoomMessage(event)
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
private fun onRoomMessage(event: Event) {
|
||||
|
|
Loading…
Reference in a new issue