using passed base64 instance, fixes android 26 requirement for imports

This commit is contained in:
Adam Brown 2022-04-12 22:51:41 +01:00
parent ded1d9b5da
commit 7ea01dd292
4 changed files with 15 additions and 7 deletions

View File

@ -219,7 +219,8 @@ internal class MatrixModules(
installAuthService(credentialsStore) installAuthService(credentialsStore)
installEncryptionService(store.knownDevicesStore()) installEncryptionService(store.knownDevicesStore())
val olmAccountStore = OlmPersistenceWrapper(store.olmStore(), AndroidBase64()) val base64 = AndroidBase64()
val olmAccountStore = OlmPersistenceWrapper(store.olmStore(), base64)
val singletonFlows = SingletonFlows(coroutineDispatchers) val singletonFlows = SingletonFlows(coroutineDispatchers)
val olm = OlmWrapper( val olm = OlmWrapper(
olmStore = olmAccountStore, olmStore = olmAccountStore,
@ -239,6 +240,7 @@ internal class MatrixModules(
services.roomService().joinedMembers(it).map { it.userId } services.roomService().joinedMembers(it).map { it.userId }
} }
}, },
base64 = base64,
coroutineDispatchers = coroutineDispatchers, coroutineDispatchers = coroutineDispatchers,
) )
installMessageService(store.localEchoStore, BackgroundWorkAdapter(workModule.workScheduler())) { serviceProvider -> installMessageService(store.localEchoStore, BackgroundWorkAdapter(workModule.workScheduler())) { serviceProvider ->

View File

@ -1,5 +1,6 @@
package app.dapk.st.matrix.crypto package app.dapk.st.matrix.crypto
import app.dapk.st.core.Base64
import app.dapk.st.core.CoroutineDispatchers import app.dapk.st.core.CoroutineDispatchers
import app.dapk.st.matrix.MatrixService import app.dapk.st.matrix.MatrixService
import app.dapk.st.matrix.MatrixServiceInstaller import app.dapk.st.matrix.MatrixServiceInstaller
@ -118,6 +119,7 @@ fun MatrixServiceInstaller.installCryptoService(
credentialsStore: CredentialsStore, credentialsStore: CredentialsStore,
olm: Olm, olm: Olm,
roomMembersProvider: ServiceDepFactory<RoomMembersProvider>, roomMembersProvider: ServiceDepFactory<RoomMembersProvider>,
base64: Base64,
coroutineDispatchers: CoroutineDispatchers, coroutineDispatchers: CoroutineDispatchers,
) { ) {
this.install { (_, _, services, logger) -> this.install { (_, _, services, logger) ->
@ -148,7 +150,7 @@ fun MatrixServiceInstaller.installCryptoService(
logger logger
) )
val verificationHandler = VerificationHandler(deviceService, credentialsStore, logger, JsonCanonicalizer(), olm) val verificationHandler = VerificationHandler(deviceService, credentialsStore, logger, JsonCanonicalizer(), olm)
val roomKeyImporter = RoomKeyImporter(coroutineDispatchers) val roomKeyImporter = RoomKeyImporter(base64, coroutineDispatchers)
SERVICE_KEY to DefaultCryptoService(olmCrypto, verificationHandler, roomKeyImporter, logger) SERVICE_KEY to DefaultCryptoService(olmCrypto, verificationHandler, roomKeyImporter, logger)
} }
} }

View File

@ -1,5 +1,6 @@
package app.dapk.st.matrix.crypto.internal package app.dapk.st.matrix.crypto.internal
import app.dapk.st.core.Base64
import app.dapk.st.core.CoroutineDispatchers import app.dapk.st.core.CoroutineDispatchers
import app.dapk.st.core.withIoContext import app.dapk.st.core.withIoContext
import app.dapk.st.matrix.common.AlgorithmName import app.dapk.st.matrix.common.AlgorithmName
@ -12,7 +13,6 @@ import kotlinx.serialization.json.Json
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.*
import javax.crypto.Cipher import javax.crypto.Cipher
import javax.crypto.Mac import javax.crypto.Mac
import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.IvParameterSpec
@ -23,11 +23,13 @@ private const val HEADER_LINE = "-----BEGIN MEGOLM SESSION DATA-----"
private const val TRAILER_LINE = "-----END MEGOLM SESSION DATA-----" private const val TRAILER_LINE = "-----END MEGOLM SESSION DATA-----"
private val importJson = Json { ignoreUnknownKeys = true } private val importJson = Json { ignoreUnknownKeys = true }
class RoomKeyImporter(private val dispatchers: CoroutineDispatchers) { class RoomKeyImporter(
private val base64: Base64,
private val dispatchers: CoroutineDispatchers,
) {
suspend fun InputStream.importRoomKeys(password: String, onChunk: suspend (List<SharedRoomKey>) -> Unit): List<RoomId> { suspend fun InputStream.importRoomKeys(password: String, onChunk: suspend (List<SharedRoomKey>) -> Unit): List<RoomId> {
return dispatchers.withIoContext { return dispatchers.withIoContext {
val decoder = Base64.getDecoder()
val decryptCipher = Cipher.getInstance("AES/CTR/NoPadding") val decryptCipher = Cipher.getInstance("AES/CTR/NoPadding")
var jsonSegment = "" var jsonSegment = ""
@ -61,7 +63,7 @@ class RoomKeyImporter(private val dispatchers: CoroutineDispatchers) {
.withIndex() .withIndex()
.map { (index, it) -> .map { (index, it) ->
val line = it.joinToString(separator = "").replace("\n", "") val line = it.joinToString(separator = "").replace("\n", "")
val toByteArray = decoder.decode(line) val toByteArray = base64.decode(line)
if (index == 0) { if (index == 0) {
decryptCipher.initialize(toByteArray, password) decryptCipher.initialize(toByteArray, password)
toByteArray.copyOfRange(37, toByteArray.size).decrypt(decryptCipher).also { toByteArray.copyOfRange(37, toByteArray.size).decrypt(decryptCipher).also {

View File

@ -82,7 +82,8 @@ class TestMatrix(
installAuthService(storeModule.credentialsStore(), AuthConfig(forceHttp = false)) installAuthService(storeModule.credentialsStore(), AuthConfig(forceHttp = false))
installEncryptionService(storeModule.knownDevicesStore()) installEncryptionService(storeModule.knownDevicesStore())
val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore(), JavaBase64()) val base64 = JavaBase64()
val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore(), base64)
val olm = OlmWrapper( val olm = OlmWrapper(
olmStore = olmAccountStore, olmStore = olmAccountStore,
singletonFlows = SingletonFlows(coroutineDispatchers), singletonFlows = SingletonFlows(coroutineDispatchers),
@ -101,6 +102,7 @@ class TestMatrix(
services.roomService().joinedMembers(it).map { it.userId } services.roomService().joinedMembers(it).map { it.userId }
} }
}, },
base64 = base64,
coroutineDispatchers = coroutineDispatchers, coroutineDispatchers = coroutineDispatchers,
) )