Skip to content

Commit

Permalink
SDK-1357 Catch and retry keysetmanager creation for InvalidKeyExcepti…
Browse files Browse the repository at this point in the history
…on, too (#122)

* Catch and retry keysetmanager creation for InvalidKeyException, too

* Version bump

* Use the correct file name

* Lint/detekt
  • Loading branch information
jhaven-stytch authored Jan 16, 2024
1 parent a3229f0 commit fab805e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

ext {
PUBLISH_GROUP_ID = 'com.stytch.sdk'
PUBLISH_VERSION = '0.18.1'
PUBLISH_VERSION = '0.18.2'
PUBLISH_ARTIFACT_ID = 'sdk'
}

Expand Down
25 changes: 11 additions & 14 deletions sdk/src/main/java/com/stytch/sdk/common/EncryptionManager.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.stytch.sdk.common

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.os.Build
import com.google.crypto.tink.Aead
import com.google.crypto.tink.KeyTemplates
import com.google.crypto.tink.aead.AeadConfig
Expand All @@ -12,15 +10,15 @@ import com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException
import com.google.crypto.tink.signature.SignatureConfig
import com.stytch.sdk.common.errors.StytchChallengeSigningFailed
import com.stytch.sdk.common.errors.StytchMissingPublicKeyError
import com.stytch.sdk.common.extensions.clearPreferences
import com.stytch.sdk.common.extensions.hexStringToByteArray
import com.stytch.sdk.common.extensions.toBase64DecodedByteArray
import com.stytch.sdk.common.extensions.toBase64EncodedString
import com.stytch.sdk.common.extensions.toHexString
import java.io.File
import java.security.InvalidKeyException
import java.security.MessageDigest
import java.security.SecureRandom
import kotlin.random.Random
import org.bouncycastle.asn1.x500.style.RFC4519Style.name
import org.bouncycastle.crypto.Signer
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters
Expand Down Expand Up @@ -49,16 +47,15 @@ internal object EncryptionManager {
.withMasterKeyUri(MASTER_KEY_URI)
.build()
} catch (_: InvalidProtocolBufferException) {
// possible that the signing key was changed (happens when we're testing, shouldn't happen for developers)
// but if it does, the app gets in a bad state, so we need to destroy and recreate the preferences file
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.deleteSharedPreferences(PREF_FILE_NAME)
} else {
context.getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE).edit().clear().apply()
val dir = File(context.applicationInfo.dataDir, "shared_prefs")
File(dir, "$name.xml").delete()
}
return getOrGenerateNewAES256KeysetManager(context, keyAlias)
// Possible that the signing key was changed. This causes the preferences file to be unreadable,
// so we need to destroy and recreate it
context.clearPreferences(PREF_FILE_NAME)
getOrGenerateNewAES256KeysetManager(context, keyAlias)
} catch (_: InvalidKeyException) {
// Possible that the signing key was changed. This causes the preferences file to be unreadable,
// so we need to destroy and recreate it
context.clearPreferences(PREF_FILE_NAME)
getOrGenerateNewAES256KeysetManager(context, keyAlias)
}
}

Expand Down
11 changes: 11 additions & 0 deletions sdk/src/main/java/com/stytch/sdk/common/extensions/ContextExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stytch.sdk.common.extensions // ktlint-disable filename
import android.content.Context
import android.os.Build
import com.stytch.sdk.common.DeviceInfo
import java.io.File

internal fun Context.getDeviceInfo(): DeviceInfo {
val deviceInfo = DeviceInfo()
Expand All @@ -27,3 +28,13 @@ internal fun Context.getDeviceInfo(): DeviceInfo {
deviceInfo.screenSize = "($width,$height)"
return deviceInfo
}

internal fun Context.clearPreferences(preferencesName: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
deleteSharedPreferences(preferencesName)
} else {
getSharedPreferences(preferencesName, Context.MODE_PRIVATE).edit().clear().apply()
val dir = File(applicationInfo.dataDir, "shared_prefs")
File(dir, "$preferencesName.xml").delete()
}
}

0 comments on commit fab805e

Please sign in to comment.