public static KeyStore openKeyStore ( File storeFile , String storePassword ) { String lc = storeFile . getName ( ) . toLowerCase ( ) ; String type = "JKS" ; String provider = null ; if ( lc . endsWith ( ".p12" ) || lc . endsWith ( ".pfx" ) ) { type = "PKCS12" ; provider = BC ; } try { KeyStore store ; if ( provider == null ) { store = KeyStore . getInstance ( type ) ; } else { store = KeyStore . getInstance ( type , provider ) ; } storeFile . getParentFile ( ) . mkdirs ( ) ; if ( storeFile . exists ( ) ) { FileInputStream fis = null ; try { fis = new FileInputStream ( storeFile ) ; store . load ( fis , storePassword . toCharArray ( ) ) ; } finally { if ( fis != null ) { fis . close ( ) ; } } } else { store . load ( null ) ; } return store ; } catch ( Exception e ) { throw new RuntimeException ( "Could not open keystore " + storeFile , e ) ; } }
Open a keystore . Store type is determined by file extension of name . If undetermined JKS is assumed . The keystore does not need to exist .