@@ -0,0 +1,2496 @@
+package jodd.http.net;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.security.KeyStore;%0Aimport java.security.KeyStoreException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.cert.Certificate;%0Aimport java.security.cert.CertificateException;%0Aimport java.security.cert.CertificateFactory;%0A%0A/**%0A * Add a certificate to the cacerts keystore if it's not already included%0A */%0Apublic class SSLUtil %7B%0A%0A%09private static final String CACERTS_PATH = %22/jre/lib/security/cacerts%22;%0A%09private static final String CACERTS_PASSWORD = %22changeit%22;%0A%0A%09/**%0A%09 * Add a certificate to the cacerts keystore if it's not already included.%0A%09 *%0A%09 * @param alias    The alias for the certificate, if added%0A%09 * @param certFile The certificate file stream%0A%09 */%0A%0A%09public static void ensureSslCertIsInKeystore(final String alias, final String certFile)%0A%09%09%09throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException %7B%0A%09%09ensureSslCertIsInKeystore(CACERTS_PASSWORD, alias, certFile);%0A%09%7D%0A%0A%09public static void ensureSslCertIsInKeystore(final String cacertsPassword, final String alias, final String certFile)%0A%09%09%09throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException%7B%0A%0A%09%09// get default cacerts file%0A%09%09final File cacertsFile = new File(System.getProperty(%22java.home%22) + CACERTS_PATH);%0A%0A%09%09if (!cacertsFile.exists()) %7B%0A%09%09%09throw new FileNotFoundException(cacertsFile.getAbsolutePath());%0A%09%09%7D%0A%0A%09%09// load cacerts keystore%0A%09%09FileInputStream cacertsIs = new FileInputStream(cacertsFile);%0A%09%09final KeyStore cacerts = KeyStore.getInstance(KeyStore.getDefaultType());%0A%09%09cacerts.load(cacertsIs, cacertsPassword.toCharArray());%0A%09%09cacertsIs.close();%0A%0A%09%09//load certificate from input stream%0A%09%09final CertificateFactory cf = CertificateFactory.getInstance(%22X.509%22);%0A%0A%09%09final InputStream certInputStream = new FileInputStream(certFile);%0A%09%09final Certificate cert = cf.generateCertificate(certInputStream);%0A%09%09certInputStream.close();%0A%0A%09%09// check if cacerts contains the certificate%0A%09%09if (cacerts.getCertificateAlias(cert) == null) %7B%0A%0A%09%09%09// cacerts doesn't contain the certificate, add it%0A%09%09%09cacerts.setCertificateEntry(alias, cert);%0A%0A%09%09%09// write the updated cacerts keystore%0A%09%09%09FileOutputStream cacertsOs = new FileOutputStream(cacertsFile);%0A%09%09%09cacerts.store(cacertsOs, CACERTS_PASSWORD.toCharArray());%0A%09%09%09cacertsOs.close();%0A%09%09%7D%0A%09%7D%0A%7D%0A

Add SSLUtil