public static KeyStore loadCertificates ( final File pemFile ) { try ( final PemReader pem = new PemReader ( new FileReader ( pemFile ) ) ) { final KeyStore ks = createEmptyKeyStore ( ) ; int certIndex = 0 ; Object obj ; while ( ( obj = parse ( pem . readPemObject ( ) ) ) != null ) { if ( obj instanceof Certificate ) { final Certificate cert = ( Certificate ) obj ; ks . setCertificateEntry ( "cert" + Integer . toString ( certIndex ++ ) , cert ) ; } else { throw new RuntimeException ( "Unknown PEM contents: " + obj + ". Expected a Certificate" ) ; } } return ks ; } catch ( Exception e ) { throw new RuntimeException ( "Error parsing PEM " + pemFile , e ) ; } }
Load one or more X . 509 Certificates from a PEM file