Create a BuildVersionSdkIntProvider to be able to inject it and do some test
To merge with BuildVersionSdkIntProvider To merge with fix add module To merge with fix buildVersionSdkIntProvider
This commit is contained in:
parent
f31c44963b
commit
cef4cf09ec
@ -26,6 +26,7 @@ import org.matrix.android.sdk.internal.di.MatrixModule
|
|||||||
import org.matrix.android.sdk.internal.di.MatrixScope
|
import org.matrix.android.sdk.internal.di.MatrixScope
|
||||||
import org.matrix.android.sdk.internal.di.NetworkModule
|
import org.matrix.android.sdk.internal.di.NetworkModule
|
||||||
import org.matrix.android.sdk.internal.raw.RawModule
|
import org.matrix.android.sdk.internal.raw.RawModule
|
||||||
|
import org.matrix.android.sdk.internal.util.system.SystemModule
|
||||||
|
|
||||||
@Component(modules = [
|
@Component(modules = [
|
||||||
TestModule::class,
|
TestModule::class,
|
||||||
@ -33,6 +34,7 @@ import org.matrix.android.sdk.internal.raw.RawModule
|
|||||||
NetworkModule::class,
|
NetworkModule::class,
|
||||||
AuthModule::class,
|
AuthModule::class,
|
||||||
RawModule::class,
|
RawModule::class,
|
||||||
|
SystemModule::class,
|
||||||
TestNetworkModule::class
|
TestNetworkModule::class
|
||||||
])
|
])
|
||||||
@MatrixScope
|
@MatrixScope
|
||||||
|
@ -36,6 +36,7 @@ import org.matrix.android.sdk.internal.session.TestInterceptor
|
|||||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
import org.matrix.android.sdk.internal.task.TaskExecutor
|
||||||
import org.matrix.android.sdk.internal.util.BackgroundDetectionObserver
|
import org.matrix.android.sdk.internal.util.BackgroundDetectionObserver
|
||||||
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
||||||
|
import org.matrix.android.sdk.internal.util.system.SystemModule
|
||||||
import org.matrix.olm.OlmManager
|
import org.matrix.olm.OlmManager
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -44,6 +45,7 @@ import java.io.File
|
|||||||
NetworkModule::class,
|
NetworkModule::class,
|
||||||
AuthModule::class,
|
AuthModule::class,
|
||||||
RawModule::class,
|
RawModule::class,
|
||||||
|
SystemModule::class,
|
||||||
NoOpTestModule::class
|
NoOpTestModule::class
|
||||||
])
|
])
|
||||||
@MatrixScope
|
@MatrixScope
|
||||||
|
@ -64,6 +64,7 @@ import org.matrix.android.sdk.internal.session.user.accountdata.AccountDataModul
|
|||||||
import org.matrix.android.sdk.internal.session.widgets.WidgetModule
|
import org.matrix.android.sdk.internal.session.widgets.WidgetModule
|
||||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
import org.matrix.android.sdk.internal.task.TaskExecutor
|
||||||
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
||||||
|
import org.matrix.android.sdk.internal.util.system.SystemModule
|
||||||
|
|
||||||
@Component(dependencies = [MatrixComponent::class],
|
@Component(dependencies = [MatrixComponent::class],
|
||||||
modules = [
|
modules = [
|
||||||
@ -80,6 +81,7 @@ import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
|||||||
CacheModule::class,
|
CacheModule::class,
|
||||||
MediaModule::class,
|
MediaModule::class,
|
||||||
CryptoModule::class,
|
CryptoModule::class,
|
||||||
|
SystemModule::class,
|
||||||
PushersModule::class,
|
PushersModule::class,
|
||||||
OpenIdModule::class,
|
OpenIdModule::class,
|
||||||
WidgetModule::class,
|
WidgetModule::class,
|
||||||
|
@ -18,12 +18,14 @@
|
|||||||
|
|
||||||
package org.matrix.android.sdk.internal.session.securestorage
|
package org.matrix.android.sdk.internal.session.securestorage
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.security.KeyPairGeneratorSpec
|
import android.security.KeyPairGeneratorSpec
|
||||||
import android.security.keystore.KeyGenParameterSpec
|
import android.security.keystore.KeyGenParameterSpec
|
||||||
import android.security.keystore.KeyProperties
|
import android.security.keystore.KeyProperties
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
|
import org.matrix.android.sdk.internal.util.system.BuildVersionSdkIntProvider
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
@ -78,7 +80,10 @@ import javax.security.auth.x500.X500Principal
|
|||||||
* Important: Keys stored in the keystore can be wiped out (depends of the OS version, like for example if you
|
* Important: Keys stored in the keystore can be wiped out (depends of the OS version, like for example if you
|
||||||
* add a pin or change the schema); So you might and with a useless pile of bytes.
|
* add a pin or change the schema); So you might and with a useless pile of bytes.
|
||||||
*/
|
*/
|
||||||
internal class SecretStoringUtils @Inject constructor(private val context: Context) {
|
internal class SecretStoringUtils @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val buildVersionSdkIntProvider: BuildVersionSdkIntProvider
|
||||||
|
) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
|
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
|
||||||
@ -114,36 +119,40 @@ internal class SecretStoringUtils @Inject constructor(private val context: Conte
|
|||||||
*
|
*
|
||||||
* The secret is encrypted using the following method: AES/GCM/NoPadding
|
* The secret is encrypted using the following method: AES/GCM/NoPadding
|
||||||
*/
|
*/
|
||||||
|
@SuppressLint("NewApi")
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun securelyStoreString(secret: String, keyAlias: String): ByteArray {
|
fun securelyStoreString(secret: String, keyAlias: String): ByteArray {
|
||||||
return when {
|
return when {
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> encryptStringM(secret, keyAlias)
|
buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> encryptStringM(secret, keyAlias)
|
||||||
else -> encryptString(secret, keyAlias)
|
else -> encryptString(secret, keyAlias)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a secret that was encrypted by #securelyStoreString()
|
* Decrypt a secret that was encrypted by #securelyStoreString()
|
||||||
*/
|
*/
|
||||||
|
@SuppressLint("NewApi")
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun loadSecureSecret(encrypted: ByteArray, keyAlias: String): String {
|
fun loadSecureSecret(encrypted: ByteArray, keyAlias: String): String {
|
||||||
return when {
|
return when {
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> decryptStringM(encrypted, keyAlias)
|
buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> decryptStringM(encrypted, keyAlias)
|
||||||
else -> decryptString(encrypted, keyAlias)
|
else -> decryptString(encrypted, keyAlias)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
fun securelyStoreObject(any: Any, keyAlias: String, output: OutputStream) {
|
fun securelyStoreObject(any: Any, keyAlias: String, output: OutputStream) {
|
||||||
when {
|
when {
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> saveSecureObjectM(keyAlias, output, any)
|
buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> saveSecureObjectM(keyAlias, output, any)
|
||||||
else -> saveSecureObject(keyAlias, output, any)
|
else -> saveSecureObject(keyAlias, output, any)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
fun <T> loadSecureSecret(inputStream: InputStream, keyAlias: String): T? {
|
fun <T> loadSecureSecret(inputStream: InputStream, keyAlias: String): T? {
|
||||||
return when {
|
return when {
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> loadSecureObjectM(keyAlias, inputStream)
|
buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> loadSecureObjectM(keyAlias, inputStream)
|
||||||
else -> loadSecureObject(keyAlias, inputStream)
|
else -> loadSecureObject(keyAlias, inputStream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.matrix.android.sdk.internal.util.system
|
||||||
|
|
||||||
|
internal interface BuildVersionSdkIntProvider {
|
||||||
|
/**
|
||||||
|
* Return the current version of the Android SDK
|
||||||
|
*/
|
||||||
|
fun get(): Int
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.matrix.android.sdk.internal.util.system
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
internal class DefaultBuildVersionSdkIntProvider @Inject constructor()
|
||||||
|
: BuildVersionSdkIntProvider {
|
||||||
|
override fun get() = Build.VERSION.SDK_INT
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.matrix.android.sdk.internal.util.system
|
||||||
|
|
||||||
|
import dagger.Binds
|
||||||
|
import dagger.Module
|
||||||
|
|
||||||
|
@Module
|
||||||
|
internal abstract class SystemModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
abstract fun bindBuildVersionSdkIntProvider(provider: DefaultBuildVersionSdkIntProvider): BuildVersionSdkIntProvider
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user