title: AndroidKeyStore KeyPairGenerator Crashes On Small Number of Devices
body: <p>My application only targets Android 6.0+. In my application I generate a RSA key in the <code>AndroidKeyStore</code> with the following:</p>
  
  <pre><code>KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
  kpg.initialize(new KeyGenParameterSpec.Builder(
      "myKey", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
      .setCertificateSubject(new X500Principal("CN=myKey"))
      .setDigests("SHA-1")
      .setEncryptionPaddings("OAEPPadding")
      .build());
  
  KeyPair kp = kpg.generateKeyPair();
  </code></pre>
  
  <p>This works well on 20+ devices that we have tested and nearly 100% percent of our users.</p>
  
  <p>However, there is a small number of users that that application crashes for when <code>kpg.generateKeyPair()</code> is executed with the following:</p>
  
  <pre><code>java.security.ProviderException: Failed to load generated key pair from keystore
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:518)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:470)
  at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:699)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.n_onCreate(Native Method)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.onCreate(MainApplication.java:34)
  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5448)
  at android.app.ActivityThread.-wrap2(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6186)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
  Caused by: java.security.UnrecoverableKeyException: Failed to obtain X.509 form of public key
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:230)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:259)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:509)
  ... 14 more
  Caused by: android.security.KeyStoreException: -22
  at android.security.KeyStore.getKeyStoreException(KeyStore.java:676)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:231)
  ... 16 more
  
  
  java.security.UnrecoverableKeyException: Failed to obtain X.509 form of public key
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:230)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:259)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:509)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:470)
  at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:699)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.n_onCreate(Native Method)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.onCreate(MainApplication.java:34)
  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5448)
  at android.app.ActivityThread.-wrap2(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6186)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
  Caused by: android.security.KeyStoreException: -22
  at android.security.KeyStore.getKeyStoreException(KeyStore.java:676)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:231)
  ... 16 more
  
  
  android.security.KeyStoreException: -22
  at android.security.KeyStore.getKeyStoreException(KeyStore.java:676)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:231)
  at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:259)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.loadKeystoreKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:509)
  at android.security.keystore.AndroidKeyStoreKeyPairGeneratorSpi.generateKeyPair(AndroidKeyStoreKeyPairGeneratorSpi.java:470)
  at java.security.KeyPairGenerator$Delegate.generateKeyPair(KeyPairGenerator.java:699)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.n_onCreate(Native Method)
  at md5fb78b69c5ddbc157f4db38fd738139a6.MainApplication.onCreate(MainApplication.java:34)
  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5448)
  at android.app.ActivityThread.-wrap2(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1564)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6186)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
  </code></pre>
  
  <p>The only things I can find on the internet about this <code>Failed to obtain X.509 form of public key</code> error are a few articles with no real solutions. See:</p>
  
  <ul>
  <li><a href="https://github.com/AzureAD/azure-activedirectory-library-for-android/issues/740" rel="noreferrer">https://github.com/AzureAD/azure-activedirectory-library-for-android/issues/740</a></li>
  <li><a href="https://jira.lineageos.org/browse/BUGBASH-454" rel="noreferrer">https://jira.lineageos.org/browse/BUGBASH-454</a></li>
  </ul>
  
  <p>The few reports that we have received are from users with OnePlus devices on Android 7.1, which is also one of the devices mentioned in the above articles but there are definitely other devices affected as well.</p>
  
  <ul>
  <li>Why is this happening?</li>
  <li>Are there any workarounds?</li>
  </ul>