title: Java 8 KeyStore of type PKCS12 returning null for getCertificate()
body: <p>I have a strange problem. I want to use a Java KeyStore of type PKCS12, however setting the type as such causes getCertificate(...) to return null, where it would return a certificate if the type was set to JKS. Am I missing something that I should be doing for the PKCS12 instance?</p>
  
  <pre><code>import java.io.ByteArrayInputStream;
  import java.security.KeyStore;
  import java.security.Provider;
  import java.security.Security;
  import java.security.cert.CertificateFactory;
  import java.security.cert.X509Certificate;
  import java.util.Base64;
  
  public class TestingRandomStuff {
  
      public static void main(String[] args) throws Exception {
          KeyStore ks = KeyStore.getInstance("PKCS12");
          ks.load(null, null);
          String pemStr = "MIICEjCCAXsCAg36MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwGA1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNEREMRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdlYiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIwODIyMDUyNjU0WhcNMTcwODIxMDUyNjU0WjBKMQswCQYDVQQGEwJKUDEOMAwGA1UECAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBsZS5jb20wXDANBgkqhkiG9w0BAQEFAANLADBIAkEAm/xmkHmEQrurE/0re/jeFRLl8ZPjBop7uLHhnia7lQG/5zDtZIUC3RVpqDSwBuw/NTweGyuP+o8AG98HxqxTBwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBABS2TLuBeTPmcaTaUW/LCB2NYOy8GMdzR1mx8iBIu2H6/E2tiY3RIevV2OW61qY2/XRQg7YPxx3ffeUugX9F4J/iPnnu1zAxxyBy2VguKv4SWjRFoRkIfIlHX0qVviMhSlNy2ioFLy7JcPZb+v3ftDGywUqcBiVDoea0Hn+GmxZA";
          X509Certificate cert = parseCertificate(pemStr);
          ks.setCertificateEntry("CILogon Silver CA 1", cert);
          X509Certificate test = (X509Certificate) ks.getCertificate("CILogon Silver CA 1"); 
          //Test is null if KeyStore type is PKCS12, not null if type is JKS
      }
  
      private static X509Certificate parseCertificate(String pem) throws Exception {
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          byte[] decoded = Base64.getDecoder().decode(pem);
          X509Certificate c = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded));
          return c;
      }
  
  }
  </code></pre>
  
  <p>EDIT: After looking at the source code, getCertificate() is calling PKCS12KeyStore.engineGetCertificateChain(), which is converting the alias to lowercase. I don't think it should be doing this as PKCS12 can store uppercase characters. I'll have to think up a workaround for now it looks like</p>