Skip to content

Commit

Permalink
Remove redundant try-catch when loading PKCS12
Browse files Browse the repository at this point in the history
  • Loading branch information
kleest committed Nov 26, 2023
1 parent 45624b8 commit 24c612b
Showing 1 changed file with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import java.io.IOException
import java.security.KeyStore
import java.security.PrivateKey
import java.security.cert.X509Certificate
Expand Down Expand Up @@ -156,20 +155,16 @@ class PhoneSettingsListener : WearableListenerService(), DataClient.OnDataChange
// load TLS key
if (tlsClientCertificateData != null && tlsClientCertificateData.isNotEmpty()) {
KeyStore.getInstance("PKCS12").apply {
try {
load(tlsClientCertificateData.inputStream(), tlsClientCertificatePassword)
load(tlsClientCertificateData.inputStream(), tlsClientCertificatePassword)

val alias = aliases().nextElement()
val certificateChain = getCertificateChain(alias).filterIsInstance<X509Certificate>().toTypedArray()
val privateKey = getKey(alias, tlsClientCertificatePassword) as PrivateKey
val alias = aliases().nextElement()
val certificateChain = getCertificateChain(alias).filterIsInstance<X509Certificate>().toTypedArray()
val privateKey = getKey(alias, tlsClientCertificatePassword) as PrivateKey

// we store the TLS Client key under a static alias because there is currently
// no way to ask the user for the correct alias
keyStore.setData(KeyStoreRepositoryImpl.ALIAS, privateKey, certificateChain)
keyChainRepository.load(applicationContext)
} catch (e: IOException) {
Log.e(TAG, "Cannot load TLS client certificate", e)
}
// we store the TLS Client key under a static alias because there is currently
// no way to ask the user for the correct alias
keyStore.setData(KeyStoreRepositoryImpl.ALIAS, privateKey, certificateChain)
keyChainRepository.load(applicationContext)
}
}

Expand Down

0 comments on commit 24c612b

Please sign in to comment.