Skip to content

Commit

Permalink
feat: fetch MLS config when not available locally [WPB-8592] 馃崚 馃崚 (#2778)
Browse files Browse the repository at this point in the history
* Commit with unresolved merge conflicts

* fix: merge issues

* fix: merge issues

* fix: merge issues

* fix: merge issues

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: MohamadJaara <mohamad.jaara@wire.com>
  • Loading branch information
github-actions[bot] and MohamadJaara committed Jun 4, 2024
1 parent d262344 commit 7f3ce95
Show file tree
Hide file tree
Showing 24 changed files with 386 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ package com.wire.kalium.cryptography
import java.nio.file.Files

actual open class BaseMLSClientTest {

actual suspend fun createMLSClient(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient {
return createCoreCrypto(clientId, allowedCipherSuites, defaultCipherSuite).mlsClient(clientId)
return createCoreCrypto(clientId).mlsClient(clientId, allowedCipherSuites, defaultCipherSuite)
}

actual suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral {
val root = Files.createTempDirectory("mls").toFile()
val keyStore = root.resolve("keystore-$clientId")
return coreCryptoCentral(keyStore.absolutePath, "test", allowedCipherSuites, defaultCipherSuite)
return coreCryptoCentral(keyStore.absolutePath, "test")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ actual open class BaseProteusClientTest {

actual suspend fun createProteusClient(proteusStore: ProteusStoreRef, databaseKey: ProteusDBSecret?): ProteusClient {
return databaseKey?.let {
coreCryptoCentral(proteusStore.value, it.value, emptyList(), 0.toUShort()).proteusClient()
coreCryptoCentral(proteusStore.value, it.value).proteusClient()
} ?: cryptoboxProteusClient(proteusStore.value, testCoroutineScheduler, testCoroutineScheduler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ package com.wire.kalium.cryptography
import java.nio.file.Files

actual open class BaseMLSClientTest {

actual suspend fun createMLSClient(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient {
return createCoreCrypto(clientId, allowedCipherSuites, defaultCipherSuite).mlsClient(clientId)
return createCoreCrypto(clientId).mlsClient(clientId, allowedCipherSuites, defaultCipherSuite)
}

actual suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral {
val root = Files.createTempDirectory("mls").toFile()
val keyStore = root.resolve("keystore-$clientId")
return coreCryptoCentral(keyStore.absolutePath, "test", allowedCipherSuites, defaultCipherSuite)
return coreCryptoCentral(keyStore.absolutePath, "test")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ actual open class BaseProteusClientTest {

actual suspend fun createProteusClient(proteusStore: ProteusStoreRef, databaseKey: ProteusDBSecret?): ProteusClient {
return databaseKey?.let {
coreCryptoCentral(proteusStore.value, it.value, emptyList(), null).proteusClient()
coreCryptoCentral(proteusStore.value, it.value).proteusClient()
} ?: cryptoboxProteusClient(proteusStore.value, testCoroutineScheduler,testCoroutineScheduler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ import kotlin.time.Duration

actual suspend fun coreCryptoCentral(
rootDir: String,
databaseKey: String,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort?
databaseKey: String
): CoreCryptoCentral {
val path = "$rootDir/${CoreCryptoCentralImpl.KEYSTORE_NAME}"
NSFileManager.defaultManager.createDirectoryAtPath(rootDir, withIntermediateDirectories = true, null, null)
val coreCrypto = CoreCrypto.deferredInit(path, databaseKey, null)
coreCrypto.setCallbacks(Callbacks())
return CoreCryptoCentralImpl(coreCrypto, rootDir, defaultCipherSuite)
return CoreCryptoCentralImpl(coreCrypto, rootDir)
}

private class Callbacks : CoreCryptoCallbacks {
Expand All @@ -61,19 +59,23 @@ private class Callbacks : CoreCryptoCallbacks {

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

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

override suspend fun mlsClient(
enrollment: E2EIClient,
certificateChain: CertificateChain,
newMLSKeyPackageCount: UInt
newMLSKeyPackageCount: UInt,
defaultCipherSuite: UShort
): MLSClient {
TODO("Not yet implemented")
}
Expand All @@ -87,7 +89,8 @@ class CoreCryptoCentralImpl(
displayName: String,
handle: String,
teamId: String?,
expiry: Duration
expiry: Duration,
defaultCipherSuite: UShort
): E2EIClient {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ actual open class BaseMLSClientTest actual constructor() {
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient {
return createCoreCrypto(clientId, allowedCipherSuites, defaultCipherSuite).mlsClient(clientId)
return createCoreCrypto(clientId).mlsClient(clientId, allowedCipherSuites, defaultCipherSuite)
}

actual suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral {
val rootDir = NSURL.fileURLWithPath(NSTemporaryDirectory() + "/mls", isDirectory = true)
NSFileManager.defaultManager.createDirectoryAtURL(rootDir, true, null, null)
val keyStore = rootDir.URLByAppendingPathComponent("keystore-$clientId")!!
return coreCryptoCentral(keyStore.path!!, "test", allowedCipherSuites, defaultCipherSuite)
return coreCryptoCentral(keyStore.path!!, "test")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import platform.Foundation.NSURL

actual open class BaseProteusClientTest actual constructor() {

private val testCoroutineScheduler = TestCoroutineScheduler()

actual fun createProteusStoreRef(userId: CryptoUserID): ProteusStoreRef {
val rootDir = NSURL.fileURLWithPath(NSTemporaryDirectory() + "proteus/${userId.value}", isDirectory = true)
return ProteusStoreRef(rootDir.path!!)
Expand All @@ -35,7 +33,7 @@ actual open class BaseProteusClientTest actual constructor() {
proteusStore: ProteusStoreRef,
databaseKey: ProteusDBSecret?
): ProteusClient {
return coreCryptoCentral(proteusStore.value, "secret", emptyList(), null).proteusClient()
return coreCryptoCentral(proteusStore.value, "secret").proteusClient()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.wire.kalium.cryptography

import com.wire.crypto.Ciphersuites
import com.wire.crypto.ClientId
import com.wire.crypto.CoreCrypto
import com.wire.crypto.CoreCryptoCallbacks
Expand All @@ -28,19 +27,20 @@ import java.io.File

actual suspend fun coreCryptoCentral(
rootDir: String,
databaseKey: String,
allowedCipherSuites: Ciphersuites,
defaultCipherSuite: UShort?
databaseKey: String
): CoreCryptoCentral {
val path = "$rootDir/${CoreCryptoCentralImpl.KEYSTORE_NAME}"
File(rootDir).mkdirs()
val coreCrypto = coreCryptoDeferredInit(path, databaseKey, allowedCipherSuites, null)
val coreCrypto = coreCryptoDeferredInit(
path = path,
key = databaseKey,
ciphersuites = emptyList(),
nbKeyPackage = null
)
coreCrypto.setCallbacks(Callbacks())
return CoreCryptoCentralImpl(
cc = coreCrypto,
rootDir = rootDir,
cipherSuite = allowedCipherSuites,
defaultCipherSuite = defaultCipherSuite
rootDir = rootDir
)
}

Expand Down Expand Up @@ -73,29 +73,35 @@ private class Callbacks : CoreCryptoCallbacks {

class CoreCryptoCentralImpl(
private val cc: CoreCrypto,
private val rootDir: String,
// TODO: remove one they are removed from the CC api
private val cipherSuite: Ciphersuites,
private val defaultCipherSuite: UShort?
private val rootDir: String
) : CoreCryptoCentral {
fun getCoreCrypto() = cc

override suspend fun mlsClient(clientId: CryptoQualifiedClientId): MLSClient {
cc.mlsInit(clientId.toString().encodeToByteArray(), cipherSuite, null)
return MLSClientImpl(cc, defaultCipherSuite!!)
override suspend fun mlsClient(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient {
cc.mlsInit(
clientId.toString().encodeToByteArray(),
allowedCipherSuites,
nbKeyPackage = null
)
return MLSClientImpl(cc, defaultCipherSuite)
}

override suspend fun mlsClient(
enrollment: E2EIClient,
certificateChain: CertificateChain,
newMLSKeyPackageCount: UInt
newMLSKeyPackageCount: UInt,
defaultCipherSuite: UShort
): MLSClient {
// todo: use DPs list from here, and return alongside with the mls client
cc.e2eiMlsInitOnly(
(enrollment as E2EIClientImpl).wireE2eIdentity,
certificateChain, newMLSKeyPackageCount
)
return MLSClientImpl(cc, defaultCipherSuite!!)
return MLSClientImpl(cc, defaultCipherSuite)
}

override suspend fun proteusClient(): ProteusClient {
Expand All @@ -107,7 +113,8 @@ class CoreCryptoCentralImpl(
displayName: String,
handle: String,
teamId: String?,
expiry: kotlin.time.Duration
expiry: kotlin.time.Duration,
defaultCipherSuite: UShort
): E2EIClient {
return E2EIClientImpl(
cc.e2eiNewEnrollment(
Expand All @@ -116,7 +123,7 @@ class CoreCryptoCentralImpl(
handle,
teamId,
expiry.inWholeSeconds.toUInt(),
defaultCipherSuite!!
defaultCipherSuite
)

)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ package com.wire.kalium.cryptography
import kotlin.time.Duration

interface CoreCryptoCentral {
suspend fun mlsClient(clientId: CryptoQualifiedClientId): MLSClient
suspend fun mlsClient(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient

suspend fun mlsClient(
enrollment: E2EIClient,
certificateChain: CertificateChain,
newMLSKeyPackageCount: UInt
newMLSKeyPackageCount: UInt,
defaultCipherSuite: UShort
): MLSClient

suspend fun proteusClient(): ProteusClient
Expand All @@ -35,12 +40,14 @@ interface CoreCryptoCentral {
*
* @return wire end to end identity client
*/
@Suppress("LongParameterList")
suspend fun newAcmeEnrollment(
clientId: CryptoQualifiedClientId,
displayName: String,
handle: String,
teamId: String?,
expiry: Duration
expiry: Duration,
defaultCipherSuite: UShort
): E2EIClient

/**
Expand All @@ -65,7 +72,5 @@ interface CoreCryptoCentral {

expect suspend fun coreCryptoCentral(
rootDir: String,
databaseKey: String,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort?
databaseKey: String
): CoreCryptoCentral
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ expect open class BaseMLSClientTest() {
): MLSClient

suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class E2EIClientTest : BaseMLSClientTest() {

@Test
fun givenClient_whenCallingCheckOrderRequest_ReturnNonEmptyResult() = runTest {
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId, ALLOWED_CIPHER_SUITES, DEFAULT_CIPHER_SUITE)
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId)
val e2eiClient = createE2EIClient(ALICE1)
e2eiClient.directoryResponse(ACME_DIRECTORY_API_RESPONSE)
e2eiClient.setAccountResponse(NEW_ACCOUNT_API_RESPONSE)
Expand All @@ -130,7 +130,7 @@ class E2EIClientTest : BaseMLSClientTest() {

@Test
fun givenClient_whenCallingFinalizeRequest_ReturnNonEmptyResult() = runTest {
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId, ALLOWED_CIPHER_SUITES, DEFAULT_CIPHER_SUITE)
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId)
val e2eiClient = createE2EIClient(ALICE1)
e2eiClient.directoryResponse(ACME_DIRECTORY_API_RESPONSE)
e2eiClient.setAccountResponse(NEW_ACCOUNT_API_RESPONSE)
Expand All @@ -149,7 +149,7 @@ class E2EIClientTest : BaseMLSClientTest() {

@Test
fun givenClient_whenCallingCertificateRequest_ReturnNonEmptyResult() = runTest {
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId, ALLOWED_CIPHER_SUITES, DEFAULT_CIPHER_SUITE)
val coreCryptoCentral = createCoreCrypto(ALICE1.qualifiedClientId)
val e2eiClient = createE2EIClient(ALICE1)
e2eiClient.directoryResponse(ACME_DIRECTORY_API_RESPONSE)
e2eiClient.setAccountResponse(NEW_ACCOUNT_API_RESPONSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ package com.wire.kalium.cryptography

actual suspend fun coreCryptoCentral(
rootDir: String,
databaseKey: String,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort?
databaseKey: String
): CoreCryptoCentral = TODO("Not yet implemented")
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ actual open class BaseMLSClientTest actual constructor() {
}

actual suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ package com.wire.kalium.cryptography
import java.nio.file.Files

actual open class BaseMLSClientTest {

actual suspend fun createMLSClient(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
): MLSClient {
return createCoreCrypto(clientId, allowedCipherSuites, defaultCipherSuite).mlsClient(clientId)
return createCoreCrypto(clientId).mlsClient(clientId, allowedCipherSuites, defaultCipherSuite)
}

actual suspend fun createCoreCrypto(
clientId: CryptoQualifiedClientId,
allowedCipherSuites: List<UShort>,
defaultCipherSuite: UShort
clientId: CryptoQualifiedClientId
): CoreCryptoCentral {
val root = Files.createTempDirectory("mls").toFile()
val keyStore = root.resolve("keystore-$clientId")
return coreCryptoCentral(keyStore.absolutePath, "test", allowedCipherSuites, defaultCipherSuite)
return coreCryptoCentral(keyStore.absolutePath, "test")
}
}
Loading

0 comments on commit 7f3ce95

Please sign in to comment.