public static void loadX509Cert ( File certFile , KeyStore keyStore ) throws GeneralSecurityException , IOException { try ( BufferedInputStream inputStream = new BufferedInputStream ( new FileInputStream ( certFile ) ) ) { CertificateFactory certFactory = CertificateFactory . getInstance ( "X.509" ) ; int certCount = 0 ; // The file might contain multiple certs while ( inputStream . available ( ) > 0 ) { try { Certificate cert = certFactory . generateCertificate ( inputStream ) ; certCount ++ ; loadX509Cert ( cert , "neo4j.javadriver.trustedcert." + certCount , keyStore ) ; } catch ( CertificateException e ) { if ( e . getCause ( ) != null && e . getCause ( ) . getMessage ( ) . equals ( "Empty input" ) ) { // This happens if there is whitespace at the end of the certificate - we load one cert, and then try and load a // second cert, at which point we fail return ; } throw new IOException ( "Failed to load certificate from `" + certFile . getAbsolutePath ( ) + "`: " + certCount + " : " + e . getMessage ( ) , e ) ; } } } }
Load the certificates written in X . 509 format in a file to a key store .