/** * Convenience method to parse a PEM encoded file and return the * corresponding X509 Certificate chain. * * @param pemFile * @return certificate chain * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws IOException * @throws CertificateException */ public static X509CertificateChain readPemCertificateAndKey(File pemFile) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, CertificateException { byte[] data = FileUtil.readFile(pemFile); return readPemCertificateAndKey(data); }
public static Subject createSubject(File certKeyFile) { try { X509CertificateChain certKey = readPemCertificateAndKey(certKeyFile); return AuthenticationUtil.getSubject(certKey); } catch (InvalidKeySpecException ex) { throw new RuntimeException("failed to read RSA private key from " + certKeyFile, ex); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException("BUG: failed to create empty KeyStore", ex); } catch (IOException ex) { throw new RuntimeException("failed to read certificate file " + certKeyFile, ex); } catch (CertificateException ex) { throw new RuntimeException("failed to load certificate from file " + certKeyFile, ex); } }
try chain = readPemCertificateAndKey(pemFile);
public static void initSSL(File pemFile) { try { X509CertificateChain chain = readPemCertificateAndKey(pemFile); SSLSocketFactory sf = getSocketFactory(chain); HttpsURLConnection.setDefaultSSLSocketFactory(sf); } catch (InvalidKeySpecException ex) { throw new RuntimeException("failed to read RSA private key from " + pemFile, ex); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException("BUG: failed to create empty KeyStore", ex); } catch (FileNotFoundException ex) { throw new RuntimeException("failed to find certificate and/or key file " + pemFile, ex); } catch (IOException ex) { throw new RuntimeException("failed to read certificate file " + pemFile, ex); } catch (CertificateException ex) { throw new RuntimeException("failed to load certificate from file " + pemFile, ex); } }
return SSLUtil.readPemCertificateAndKey(certificate);