title: Android keystore2.AndroidKeyStoreProvider throws NPE on Samsung devices with API 31
body: <p>I'm getting lots of crash reported on samsung devices running android 12 from yesterday. AndroidKeyStoreProvider throws NPE when calling X509Certificate.getPublicKey(). it's working fine on other brands devices.</p>
  <p>Key pair generation method :</p>
  <pre><code>   public void generateKeys(Context context) throws GeneratePairKeyException {
      KeyPairGenerator keyPairGenerator;
      try {
          if(KeyStoreUtil.getPrivateKey() == null){
              Calendar start = GregorianCalendar.getInstance();
              Calendar end = GregorianCalendar.getInstance();
              end.add(Calendar.YEAR, 10);
              keyPairGenerator = KeyPairGenerator.getInstance(&quot;RSA&quot;, ANDROID_KEY_STORE);
              keyPairGenerator.initialize(new KeyPairGeneratorSpec.Builder(context)
                      .setAlias(ALIAS)
                      .setSerialNumber(BigInteger.valueOf(1))
                      .setStartDate(start.getTime())
                      .setEndDate(end.getTime())
                      .setSubject(new X500Principal(&quot;CN=Pushe_CA&quot;))
                      .build());
              keyPairGenerator.generateKeyPair();
          }
      } catch (NoSuchAlgorithmException |
              NoSuchProviderException | InvalidAlgorithmParameterException e) {
          Logger.e(e);
          throw new GeneratePairKeyException(&quot;pair key not created&quot;);
      } catch (IllegalStateException e) {
          throw e;
      } catch (Exception e) {
          throw new GeneratePairKeyException(&quot;pair key not created&quot;);
      }
  }
  </code></pre>
  <p>App crashes when i'm trying to get private key like this :</p>
  <pre><code> KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
   keyStore.load(null);
   (PrivateKey) keyStore.getKey(ALIAS, null);
  </code></pre>
  <p>here's the crash log :</p>
  <blockquote>
  <p>Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.PublicKey java.security.cert.X509Certificate.getPublicKey()' on a null object reference
  at android.security.keystore2.AndroidKeyStoreProvider.makeAndroidKeyStorePublicKeyFromKeyEntryResponse(AndroidKeyStoreProvider.java:220)
  at android.security.keystore2.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:401)
  at android.security.keystore2.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:343)
  at android.security.keystore2.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:110)
  at java.security.KeyStore.getKey(KeyStore.java:1062)</p>
  </blockquote>
  <p>Any solutions?</p>