@@ -0,0 +1,3163 @@
+package org.helioviewer.jhv.io;%0A%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.security.GeneralSecurityException;%0Aimport java.security.KeyStore;%0Aimport java.security.cert.Certificate;%0Aimport java.security.cert.CertificateFactory;%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0A%0Aimport javax.net.ssl.KeyManagerFactory;%0Aimport javax.net.ssl.HttpsURLConnection;%0Aimport javax.net.ssl.SSLContext;%0Aimport javax.net.ssl.TrustManager;%0Aimport javax.net.ssl.TrustManagerFactory;%0Aimport javax.net.ssl.X509TrustManager;%0A%0Aimport org.helioviewer.jhv.base.FileUtils;%0A%0Aclass SSLCert %7B%0A%0A    static void addRootCA(String cert) %7B%0A        try %7B%0A            X509TrustManager trustManager = trustManagerForCertificates(FileUtils.getResourceInputStream(%22/cert/%22 + cert));%0A            SSLContext sslContext = SSLContext.getInstance(%22TLS%22);%0A            sslContext.init(null, new TrustManager%5B%5D%7B trustManager %7D, null);%0A            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());%0A        %7D catch (GeneralSecurityException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    private static X509TrustManager trustManagerForCertificates(InputStream in) throws GeneralSecurityException %7B%0A        CertificateFactory certificateFactory = CertificateFactory.getInstance(%22X.509%22);%0A        Collection%3C? extends Certificate%3E certificates = certificateFactory.generateCertificates(in);%0A        if (certificates.isEmpty()) %7B%0A            throw new IllegalArgumentException(%22expected non-empty set of trusted certificates%22);%0A        %7D%0A%0A        // Put the certificates a key store%0A        char%5B%5D password = %22password%22.toCharArray(); // Any password will work%0A        KeyStore keyStore = newEmptyKeyStore(password);%0A        int index = 0;%0A        for (Certificate certificate : certificates) %7B%0A            String certificateAlias = Integer.toString(index++);%0A            keyStore.setCertificateEntry(certificateAlias, certificate);%0A        %7D%0A%0A        // Use it to build an X509 trust manager%0A        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());%0A        keyManagerFactory.init(keyStore, password);%0A        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());%0A        trustManagerFactory.init(keyStore);%0A        TrustManager%5B%5D trustManagers = trustManagerFactory.getTrustManagers();%0A        if (trustManagers.length != 1 %7C%7C !(trustManagers%5B0%5D instanceof X509TrustManager)) %7B%0A            throw new IllegalStateException(%22Unexpected default trust managers:%22 + Arrays.toString(trustManagers));%0A        %7D%0A        return (X509TrustManager) trustManagers%5B0%5D;%0A    %7D%0A%0A    private static KeyStore newEmptyKeyStore(char%5B%5D password) throws GeneralSecurityException %7B%0A        try %7B%0A            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());%0A            InputStream in = null; // By convention, 'null' creates an empty key store%0A            keyStore.load(in, password);%0A            return keyStore;%0A        %7D catch (IOException e) %7B%0A            throw new AssertionError(e);%0A        %7D%0A    %7D%0A%0A%7D%0A

Add root certificate without destroying system ones