-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store app lock password securely [WPB-4695] (#2249)
- Loading branch information
Showing
12 changed files
with
437 additions
and
49 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
app/src/androidTest/java/com/wire/android/datastore/EncryptionManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.android.datastore | ||
|
||
import org.amshove.kluent.internal.assertEquals | ||
import org.amshove.kluent.internal.assertFails | ||
import org.amshove.kluent.internal.assertNotEquals | ||
import org.junit.Test | ||
|
||
class EncryptionManagerTest { | ||
|
||
@Test | ||
fun givenKeyAlias_whenEncryptingAndDecryptingWithTheSameKeyAlias_thenTheOriginalValueReturned() { | ||
val data = "dataToBeEncrypted123!" | ||
val keyAlias = "key_alias" | ||
|
||
val encryptedData = EncryptionManager.encrypt(keyAlias, data) | ||
val decryptedData = EncryptionManager.decrypt(keyAlias, encryptedData) | ||
|
||
assertNotEquals(data, encryptedData) | ||
assertEquals(data, decryptedData) | ||
} | ||
|
||
@Test | ||
fun givenTwoKeyAliases_whenEncryptingWithOneKeyAliasAndDecryptingWithOtherKeyAlias_thenExceptionThrown() { | ||
val data = "dataToBeEncrypted123!" | ||
val keyAlias1 = "key_alias1" | ||
val keyAlias2 = "key_alias2" | ||
|
||
val encryptedData = EncryptionManager.encrypt(keyAlias1, data) | ||
assertFails { EncryptionManager.decrypt(keyAlias2, encryptedData) } | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
app/src/main/kotlin/com/wire/android/datastore/EncryptionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.android.datastore | ||
|
||
import android.security.keystore.KeyGenParameterSpec | ||
import android.security.keystore.KeyProperties | ||
import android.util.Base64 | ||
import java.io.UnsupportedEncodingException | ||
import java.nio.charset.Charset | ||
import java.security.InvalidKeyException | ||
import java.security.KeyStore | ||
import javax.crypto.AEADBadTagException | ||
import javax.crypto.BadPaddingException | ||
import javax.crypto.Cipher | ||
import javax.crypto.IllegalBlockSizeException | ||
import javax.crypto.KeyGenerator | ||
import javax.crypto.SecretKey | ||
import javax.crypto.spec.GCMParameterSpec | ||
|
||
object EncryptionManager { | ||
|
||
private const val ALGORITHM = KeyProperties.KEY_ALGORITHM_AES | ||
private const val BLOCK_MODE = KeyProperties.BLOCK_MODE_GCM | ||
private const val PADDING = KeyProperties.ENCRYPTION_PADDING_NONE | ||
private const val TRANSFORMATION = "$ALGORITHM/$BLOCK_MODE/$PADDING" | ||
|
||
private val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) } | ||
private val cipher = Cipher.getInstance(TRANSFORMATION) | ||
private val charset = Charset.defaultCharset() | ||
|
||
private fun getKey(keyAlias: String): SecretKey { | ||
val existingKey = keyStore.getEntry(keyAlias, null) as? KeyStore.SecretKeyEntry | ||
return existingKey?.secretKey ?: createKey(keyAlias) | ||
} | ||
|
||
private fun createKey(keyAlias: String): SecretKey { | ||
return KeyGenerator.getInstance(ALGORITHM).apply { | ||
init( | ||
KeyGenParameterSpec.Builder( | ||
keyAlias, | ||
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT | ||
) | ||
.setBlockModes(BLOCK_MODE) | ||
.setEncryptionPaddings(PADDING) | ||
.setUserAuthenticationRequired(false) | ||
.setRandomizedEncryptionRequired(true) | ||
.build() | ||
) | ||
}.generateKey() | ||
} | ||
|
||
@Throws( | ||
UnsupportedOperationException::class, | ||
InvalidKeyException::class, | ||
IllegalStateException::class, | ||
IllegalBlockSizeException::class, | ||
BadPaddingException::class, | ||
AEADBadTagException::class, | ||
UnsupportedEncodingException::class | ||
) | ||
fun encrypt(keyAlias: String, text: String): String { | ||
cipher.init(Cipher.ENCRYPT_MODE, getKey(keyAlias)) | ||
val iv = cipher.iv | ||
val encryptedBytes = cipher.doFinal(text.toByteArray()) | ||
return listOf(encryptedBytes, iv) | ||
.map { String(Base64.encode(it, Base64.NO_WRAP), charset) } | ||
.joinToString(":") | ||
} | ||
|
||
@Throws( | ||
UnsupportedOperationException::class, | ||
InvalidKeyException::class, | ||
IllegalStateException::class, | ||
IllegalBlockSizeException::class, | ||
BadPaddingException::class, | ||
AEADBadTagException::class, | ||
UnsupportedEncodingException::class | ||
) | ||
@Suppress("MagicNumber") | ||
fun decrypt(keyAlias: String, encryptedText: String): String { | ||
val (encryptedData, iv) = encryptedText.split(":") | ||
.map { Base64.decode(it.toByteArray(charset), Base64.NO_WRAP) } | ||
.let { it[0] to it[1] } | ||
cipher.init(Cipher.DECRYPT_MODE, getKey(keyAlias), GCMParameterSpec(128, iv)) | ||
val decryptedBytes = cipher.doFinal(encryptedData) | ||
return String(decryptedBytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/kotlin/com/wire/android/feature/ObserveAppLockConfigUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.android.feature | ||
|
||
import com.wire.android.datastore.GlobalDataStore | ||
import dagger.hilt.android.scopes.ViewModelScoped | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
@ViewModelScoped | ||
class ObserveAppLockConfigUseCase @Inject constructor( | ||
private val globalDataStore: GlobalDataStore, | ||
) { | ||
|
||
operator fun invoke(): Flow<AppLockConfig> = | ||
globalDataStore.getAppLockPasscodeFlow().map { // TODO: include checking if any logged account does not enforce app-lock | ||
when { | ||
it.isNullOrEmpty() -> AppLockConfig.Disabled | ||
else -> AppLockConfig.Enabled | ||
} | ||
} | ||
} | ||
|
||
sealed class AppLockConfig(open val timeoutInSeconds: Int = DEFAULT_TIMEOUT) { | ||
data object Disabled : AppLockConfig() | ||
data object Enabled : AppLockConfig() | ||
data class EnforcedByTeam(override val timeoutInSeconds: Int) : AppLockConfig(timeoutInSeconds) | ||
|
||
companion object { | ||
const val DEFAULT_TIMEOUT = 60 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.