Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass the MLS public key signature algorithm when updating MLS public keys [WPB-8592] 🍒 🍒 #2776

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ actual suspend fun coreCryptoCentral(rootDir: String, databaseKey: String): Core
NSFileManager.defaultManager.createDirectoryAtPath(rootDir, withIntermediateDirectories = true, null, null)
val coreCrypto = CoreCrypto.deferredInit(path, databaseKey, null)
coreCrypto.setCallbacks(Callbacks())
return CoreCryptoCentralImpl(coreCrypto, rootDir)
return CoreCryptoCentralImpl(coreCrypto, rootDir, defaultCipherSuite)
}

private class Callbacks : CoreCryptoCallbacks {
Expand All @@ -54,11 +54,15 @@ private class Callbacks : CoreCryptoCallbacks {
}
}

class CoreCryptoCentralImpl(private val cc: CoreCrypto, private val rootDir: String) : CoreCryptoCentral {
class CoreCryptoCentralImpl(
private val cc: CoreCrypto,
private val rootDir: String,
private val defaultCipherSuite: UShort?
) : CoreCryptoCentral {

override suspend fun mlsClient(clientId: CryptoQualifiedClientId): MLSClient {
cc.mlsInit(MLSClientImpl.toUByteList(clientId.toString()))
return MLSClientImpl(cc)
return MLSClientImpl(cc, defaultCipherSuite = defaultCipherSuite!!)
}

override suspend fun mlsClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import kotlin.time.toDuration
@Suppress("TooManyFunctions")
@OptIn(ExperimentalUnsignedTypes::class)
class MLSClientImpl(
private val coreCrypto: CoreCrypto
private val coreCrypto: CoreCrypto,
private val defaultCipherSuite: UShort
) : MLSClient {

private val keyRotationDuration: Duration = 30.toDuration(DurationUnit.DAYS)
Expand All @@ -48,8 +49,8 @@ class MLSClientImpl(
override suspend fun close() {
}

override suspend fun getPublicKey(): ByteArray {
return coreCrypto.clientPublicKey().toUByteArray().asByteArray()
override suspend fun getPublicKey(): Pair<ByteArray, UShort> {
return coreCrypto.clientPublicKey().toUByteArray().asByteArray() to defaultCipherSuite
}

override suspend fun generateKeyPackages(amount: Int): List<ByteArray> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class MLSClientImpl(
coreCrypto.close()
}

override suspend fun getPublicKey(): ByteArray {
return coreCrypto.clientPublicKey(defaultCiphersuite, toCredentialType(getMLSCredentials()))
override suspend fun getPublicKey(): Pair<ByteArray, Ciphersuite> {
return coreCrypto.clientPublicKey(defaultCipherSuite, toCredentialType(getMLSCredentials())) to defaultCipherSuite
}

override suspend fun generateKeyPackages(amount: Int): List<ByteArray> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ interface MLSClient {
* Public key of the client's identity.
*
* @return public key of the client
* @return ciphersuite used for the public key
*/
suspend fun getPublicKey(): ByteArray
suspend fun getPublicKey(): Pair<ByteArray, UShort>

/**
* Generate a fresh set of key packages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MLSClientTest : BaseMLSClientTest() {
@Test
fun givenClient_whenCallingGetPublicKey_ReturnNonEmptyResult() = runTest {
val mlsClient = createClient(ALICE1)
assertTrue(mlsClient.getPublicKey().isNotEmpty())
assertTrue(mlsClient.getPublicKey().first.isNotEmpty())
}

@Test
Expand Down Expand Up @@ -207,5 +207,4 @@ class MLSClientTest : BaseMLSClientTest() {
"Carol"
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MLSClientImpl : MLSClient {
TODO("Not yet implemented")
}

override suspend fun getPublicKey(): ByteArray {
override suspend fun getPublicKey(): Pair<ByteArray, UShort> {
TODO("Not yet implemented")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.toApi
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.id.toModel
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.di.MapperProvider
import com.wire.kalium.logic.functional.Either
Expand All @@ -52,7 +53,12 @@ import kotlinx.coroutines.flow.map
@Suppress("TooManyFunctions")
interface ClientRepository {
suspend fun registerClient(param: RegisterClientParam): Either<NetworkFailure, Client>
suspend fun registerMLSClient(clientId: ClientId, publicKey: ByteArray): Either<CoreFailure, Unit>
suspend fun registerMLSClient(
clientId: ClientId,
publicKey: ByteArray,
cipherSuite: CipherSuite
): Either<CoreFailure, Unit>

suspend fun hasRegisteredMLSClient(): Either<CoreFailure, Boolean>
suspend fun persistClientId(clientId: ClientId): Either<CoreFailure, Unit>

Expand Down Expand Up @@ -204,8 +210,12 @@ class ClientDataSource(
.map { it?.let { clientMapper.fromClientEntity(it) } }
.wrapStorageRequest()

override suspend fun registerMLSClient(clientId: ClientId, publicKey: ByteArray): Either<CoreFailure, Unit> =
clientRemoteRepository.registerMLSClient(clientId, publicKey.encodeBase64())
override suspend fun registerMLSClient(
clientId: ClientId,
publicKey: ByteArray,
cipherSuite: CipherSuite
): Either<CoreFailure, Unit> =
clientRemoteRepository.registerMLSClient(clientId, publicKey.encodeBase64(), cipherSuite)
.flatMap {
wrapStorageRequest {
clientRegistrationStorage.setHasRegisteredMLSClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,37 @@ import com.wire.kalium.logic.data.client.DeleteClientParam
import com.wire.kalium.logic.data.client.RegisterClientParam
import com.wire.kalium.logic.data.client.UpdateClientCapabilitiesParam
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.id.IdMapper
import com.wire.kalium.logic.data.id.toApi
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.data.mls.signatureAlgorithm
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.di.MapperProvider
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.left
import com.wire.kalium.logic.functional.map
import com.wire.kalium.logic.wrapApiRequest
import com.wire.kalium.network.api.base.authenticated.client.ClientApi
import com.wire.kalium.network.api.base.authenticated.client.MLSPublicKeyTypeDTO
import com.wire.kalium.network.api.base.authenticated.client.SimpleClientResponse
import com.wire.kalium.network.api.base.authenticated.client.UpdateClientMlsPublicKeysRequest
import com.wire.kalium.network.api.base.model.PushTokenBody
import com.wire.kalium.network.exceptions.KaliumException
import com.wire.kalium.network.api.base.model.UserId as UserIdDTO

interface ClientRemoteRepository {
suspend fun registerClient(param: RegisterClientParam): Either<NetworkFailure, Client>
suspend fun registerMLSClient(clientId: ClientId, publicKey: String): Either<NetworkFailure, Unit>
suspend fun registerMLSClient(
clientId: ClientId,
publicKey: String,
cipherSuite: CipherSuite
): Either<NetworkFailure, Unit>

suspend fun deleteClient(param: DeleteClientParam): Either<NetworkFailure, Unit>
suspend fun registerToken(body: PushTokenBody): Either<NetworkFailure, Unit>
suspend fun deregisterToken(pid: String): Either<NetworkFailure, Unit>
suspend fun fetchOtherUserClients(
userIdList: List<UserId>
): Either<NetworkFailure, Map<UserIdDTO, List<SimpleClientResponse>>>

suspend fun updateClientCapabilities(
updateClientCapabilitiesParam: UpdateClientCapabilitiesParam,
clientID: String
Expand All @@ -58,20 +67,26 @@ class ClientRemoteDataSource(
private val clientApi: ClientApi,
private val clientConfig: ClientConfig,
private val clientMapper: ClientMapper = MapperProvider.clientMapper(),
private val idMapper: IdMapper = MapperProvider.idMapper()
) : ClientRemoteRepository {

override suspend fun registerClient(param: RegisterClientParam): Either<NetworkFailure, Client> =
wrapApiRequest { clientApi.registerClient(clientMapper.toRegisterClientRequest(clientConfig, param)) }
.map { clientResponse -> clientMapper.fromClientDto(clientResponse) }

override suspend fun registerMLSClient(clientId: ClientId, publicKey: String): Either<NetworkFailure, Unit> =
override suspend fun registerMLSClient(
clientId: ClientId,
publicKey: String,
cipherSuite: CipherSuite
): Either<NetworkFailure, Unit> = cipherSuite.signatureAlgorithm()?.let { signatureAlgorithm ->
wrapApiRequest {
clientApi.updateClientMlsPublicKeys(
UpdateClientMlsPublicKeysRequest(mapOf(Pair(MLSPublicKeyTypeDTO.ED25519, publicKey))),
UpdateClientMlsPublicKeysRequest(
mapOf(signatureAlgorithm to publicKey),
),
clientId.value
)
}
} ?: NetworkFailure.ServerMiscommunication(KaliumException.GenericError(IllegalArgumentException("Unknown cipher suite"))).left()

override suspend fun deleteClient(param: DeleteClientParam): Either<NetworkFailure, Unit> =
wrapApiRequest { clientApi.deleteClient(param.password, param.clientId.value) }
Expand All @@ -87,7 +102,7 @@ class ClientRemoteDataSource(
override suspend fun fetchOtherUserClients(
userIdList: List<UserId>
): Either<NetworkFailure, Map<UserIdDTO, List<SimpleClientResponse>>> {
val networkUserId = userIdList.map { idMapper.toNetworkUserId(it) }
val networkUserId = userIdList.map { it.toApi() }
return wrapApiRequest { clientApi.listClientsOfUsers(networkUserId) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.data.mls

import com.wire.kalium.network.api.base.authenticated.client.MLSPublicKeyTypeDTO

data class SupportedCipherSuite(
val supported: List<CipherSuite>,
val default: CipherSuite
)

@Suppress("MagicNumber", "ClassName")
sealed class CipherSuite(open val tag: Int) {
data class UNKNOWN(override val tag: Int) : CipherSuite(tag) {
override fun toString(): String {
return "UNKNOWN($tag)".uppercase()
}
}

data object MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519 : CipherSuite(1) {
override fun toString(): String {
return "MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519"
}
}

data object MLS_128_DHKEMP256_AES128GCM_SHA256_P256 : CipherSuite(2) {
override fun toString(): String {
return "MLS_128_DHKEMP256_AES128GCM_SHA256_P256"
}
}

data object MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519 :
CipherSuite(3) {
override fun toString(): String {
return "MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519"
}
}

data object MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448 : CipherSuite(4) {
override fun toString(): String {
return "MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448"

}
}

data object MLS_256_DHKEMP521_AES256GCM_SHA512_P521 : CipherSuite(5) {
override fun toString(): String {
return "MLS_256_DHKEMP521_AES256GCM_SHA512_P521"
}

}

data object MLS_256_DHKEMX448_CHACHA20POLY1305_SHA512_Ed448 : CipherSuite(6) {
override fun toString(): String {
return "MLS_256_DHKEMX448_CHACHA20POLY1305_SHA512_Ed448"
}
}

data object MLS_256_DHKEMP384_AES256GCM_SHA384_P384 : CipherSuite(7) {
override fun toString(): String {
return "MLS_256_DHKEMP384_AES256GCM_SHA384_P384"
}
}

data object MLS_128_X25519KYBER768DRAFT00_AES128GCM_SHA256_ED25519 :
CipherSuite(61489) {
override fun toString(): String {
return "MLS_128_X25519KYBER768DRAFT00_AES128GCM_SHA256_ED25519"
}
}

companion object {
fun fromTag(tag: Int): CipherSuite = when (tag) {
1 -> MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519
2 -> MLS_128_DHKEMP256_AES128GCM_SHA256_P256
3 -> MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519
4 -> MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448
5 -> MLS_256_DHKEMP521_AES256GCM_SHA512_P521
6 -> MLS_256_DHKEMX448_CHACHA20POLY1305_SHA512_Ed448
7 -> MLS_256_DHKEMP384_AES256GCM_SHA384_P384
61489 -> MLS_128_X25519KYBER768DRAFT00_AES128GCM_SHA256_ED25519
else -> UNKNOWN(tag)
}

fun fromTag(tag: UShort) = fromTag(tag.toInt())
}
}

fun CipherSuite.signatureAlgorithm(): MLSPublicKeyTypeDTO? = when (this) {
CipherSuite.MLS_128_DHKEMP256_AES128GCM_SHA256_P256 -> MLSPublicKeyTypeDTO.P256
CipherSuite.MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519 -> MLSPublicKeyTypeDTO.ED25519
CipherSuite.MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519 -> MLSPublicKeyTypeDTO.ED25519
CipherSuite.MLS_128_X25519KYBER768DRAFT00_AES128GCM_SHA256_ED25519 -> MLSPublicKeyTypeDTO.ED25519
CipherSuite.MLS_256_DHKEMP384_AES256GCM_SHA384_P384 -> MLSPublicKeyTypeDTO.P384
CipherSuite.MLS_256_DHKEMP521_AES256GCM_SHA512_P521 -> MLSPublicKeyTypeDTO.P521
CipherSuite.MLS_256_DHKEMX448_AES256GCM_SHA512_Ed448 -> MLSPublicKeyTypeDTO.ED448
CipherSuite.MLS_256_DHKEMX448_CHACHA20POLY1305_SHA512_Ed448 -> MLSPublicKeyTypeDTO.ED448
is CipherSuite.UNKNOWN -> null
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import com.wire.kalium.logic.data.client.MLSClientProvider
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.keypackage.KeyPackageLimitsProvider
import com.wire.kalium.logic.data.keypackage.KeyPackageRepository
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.onFailure
import com.wire.kalium.logic.functional.right
import com.wire.kalium.logic.wrapMLSRequest

sealed class RegisterMLSClientResult {
data object Success : RegisterMLSClientResult()
Expand Down Expand Up @@ -60,8 +62,12 @@ internal class RegisterMLSClientUseCaseImpl(
}
}.onFailure {
mlsClientProvider.getMLSClient(clientId)
}.flatMap {
clientRepository.registerMLSClient(clientId, it.getPublicKey())
}.flatMap { mlsClient ->
wrapMLSRequest {
mlsClient.getPublicKey()
}
}.flatMap { (publicKey, cipherSuite) ->
clientRepository.registerMLSClient(clientId, publicKey, CipherSuite.fromTag(cipherSuite))
}.flatMap {
keyPackageRepository.uploadNewKeyPackages(clientId, keyPackageLimitsProvider.refillAmount())
Either.Right(RegisterMLSClientResult.Success)
Expand Down
Loading
Loading