Skip to content

Commit 8104496

Browse files
authored
Merge pull request #18 from contentpass/CHORE-android-8-keystore-api
Use more reliable API to access KeyStore
2 parents 3853090 + 675a53c commit 8104496

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Our SDK is available on Maven Central.
1515

1616
```groovy
17-
implementation 'de.contentpass:contentpass-android:2.2.7'
17+
implementation 'de.contentpass:contentpass-android:2.2.8'
1818
```
1919

2020
Add this to your app's `build.gradle` file's `dependencies` element.

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ kapt {
5858
extra.apply{
5959
set("PUBLISH_GROUP_ID", "de.contentpass")
6060
set("PUBLISH_ARTIFACT_ID", "contentpass-android")
61-
set("PUBLISH_VERSION", "2.2.7")
61+
set("PUBLISH_VERSION", "2.2.8")
6262
}
6363

6464
apply("${rootProject.projectDir}/scripts/publish-module.gradle")

lib/src/main/java/de/contentpass/lib/KeyStore.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,27 @@ internal class KeyStore(private val context: Context, private val propertyId: St
5050
if (!keystore.containsAlias(keyPairAlias)) {
5151
createKeyPair()
5252
}
53-
val pair = try {
54-
keystore.getEntry(keyPairAlias, null) as? VendorKeyStore.PrivateKeyEntry
53+
val recovered = try {
54+
val key = keystore.getKey(keyPairAlias, null) as? PrivateKey
55+
val cert = keystore.getCertificate(keyPairAlias)
56+
if (key != null && cert != null) {
57+
key to cert.publicKey
58+
} else {
59+
null
60+
}
5561
} catch (e: Exception) {
5662
null
5763
} ?: run {
5864
// Android 8 can throw when the entry is invalidated/corrupted; recreate it.
5965
keystore.deleteEntry(keyPairAlias)
6066
createKeyPair()
61-
keystore.getEntry(keyPairAlias, null) as VendorKeyStore.PrivateKeyEntry
67+
val key = keystore.getKey(keyPairAlias, null) as PrivateKey
68+
val cert = keystore.getCertificate(keyPairAlias)
69+
?: throw IllegalStateException("Failed to load keystore certificate after regeneration.")
70+
key to cert.publicKey
6271
}
63-
privateKey = pair.privateKey
64-
publicKey = pair.certificate.publicKey
72+
privateKey = recovered.first
73+
publicKey = recovered.second
6574
}
6675

6776
private fun createKeyPair(): KeyPair {

0 commit comments

Comments
 (0)