-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDKS-3104 - Improvemnets in caching the key reference
- Loading branch information
1 parent
4444228
commit 26406ba
Showing
4 changed files
with
102 additions
and
80 deletions.
There are no files selected for viewing
80 changes: 0 additions & 80 deletions
80
forgerock-core/src/main/java/org/forgerock/android/auth/AndroidMEncryptor.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
forgerock-core/src/main/java/org/forgerock/android/auth/AndroidMEncryptor.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,84 @@ | ||
/* | ||
* Copyright (c) 2019 - 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
package org.forgerock.android.auth | ||
|
||
import android.security.keystore.KeyGenParameterSpec | ||
import android.security.keystore.KeyProperties | ||
import java.io.IOException | ||
import java.security.GeneralSecurityException | ||
import java.security.KeyStore | ||
import java.util.concurrent.atomic.AtomicReference | ||
import javax.crypto.Cipher | ||
import javax.crypto.KeyGenerator | ||
import javax.crypto.SecretKey | ||
|
||
/** | ||
* Provide data encryption and decryption for Android M device. | ||
*/ | ||
internal open class AndroidMEncryptor(keyAlias: String) : AbstractSymmetricEncryptor(keyAlias) { | ||
@JvmField | ||
val specBuilder: KeyGenParameterSpec.Builder = KeyGenParameterSpec.Builder( | ||
keyAlias, | ||
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT | ||
) | ||
.setBlockModes(KeyProperties.BLOCK_MODE_GCM) | ||
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) | ||
.setRandomizedEncryptionRequired(true) | ||
.setUserAuthenticationRequired(false) | ||
.setKeySize(KEY_SIZE) | ||
|
||
@Throws(GeneralSecurityException::class, IOException::class) | ||
override fun getSecretKey(): SecretKey { | ||
keyReferenceCache.get()?.let { | ||
return it | ||
} | ||
if (keyStore.containsAlias(keyAlias)) { | ||
return (keyStore.getEntry(keyAlias, null) as KeyStore.SecretKeyEntry).secretKey.also { | ||
keyReferenceCache.set(it) | ||
} | ||
} else { | ||
val keyGenerator = KeyGenerator.getInstance( | ||
KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE | ||
) | ||
keyGenerator.init(specBuilder.build()) | ||
return keyGenerator.generateKey().also { | ||
keyReferenceCache.set(it) | ||
} | ||
} | ||
} | ||
|
||
@Throws(GeneralSecurityException::class, IOException::class) | ||
public override fun init(cipher: Cipher): ByteArray { | ||
//Generate a random IV See KeyGenParameterSpec.Builder.setRandomizedEncryptionRequired | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey) | ||
return cipher.iv | ||
} | ||
|
||
@Throws(GeneralSecurityException::class, IOException::class) | ||
override fun reset() { | ||
keyStore.deleteEntry(keyAlias) | ||
keyReferenceCache.set(null) | ||
} | ||
|
||
companion object { | ||
//Hold the current key. | ||
val keyReferenceCache = AtomicReference<SecretKey>() | ||
} | ||
|
||
/** | ||
* Retrieve and load the Android KeyStore | ||
* | ||
* @return The AndroidKeyStore | ||
*/ | ||
@get:Throws(GeneralSecurityException::class, IOException::class) | ||
private val keyStore: KeyStore | ||
get() { | ||
val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE) | ||
keyStore.load(null) | ||
return keyStore | ||
} | ||
} |
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