Search in sources :

Example 46 with KeyStore

use of java.security.KeyStore in project zaproxy by zaproxy.

the class DynamicSSLPanel method doImport.

/**
	 * Import Root CA certificate from other ZAP configuration files.
	 */
private void doImport() {
    if (checkExistingCertificate()) {
        // prevent overwriting
        return;
    }
    final JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);
    fc.setSelectedFile(new File(CONFIGURATION_FILENAME));
    fc.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            // config.xml or *.pem files
            return Constant.messages.getString("dynssl.filter.file");
        }

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(CONFIGURATION_FILENAME) || f.getName().toLowerCase().endsWith("pem") || f.isDirectory();
        }
    });
    final int result = fc.showOpenDialog(this);
    final File f = fc.getSelectedFile();
    if (result == JFileChooser.APPROVE_OPTION && f.exists()) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading Root CA certificate from " + f);
        }
        KeyStore ks = null;
        if (f.getName().toLowerCase().endsWith("pem")) {
            ks = convertPemFileToKeyStore(f.toPath());
        } else {
            try {
                final ZapXmlConfiguration conf = new ZapXmlConfiguration(f);
                final String rootcastr = conf.getString(DynSSLParam.PARAM_ROOT_CA);
                ks = SslCertificateUtils.string2Keystore(rootcastr);
            } catch (final Exception e) {
                logger.error("Error importing Root CA cert from config file:", e);
                JOptionPane.showMessageDialog(this, Constant.messages.getString("dynssl.message1.filecouldntloaded"), Constant.messages.getString("dynssl.message1.title"), JOptionPane.ERROR_MESSAGE);
            }
        }
        if (ks != null) {
            setRootca(ks);
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) ZapXmlConfiguration(org.zaproxy.zap.utils.ZapXmlConfiguration) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) KeyStore(java.security.KeyStore) IOException(java.io.IOException)

Example 47 with KeyStore

use of java.security.KeyStore in project zaproxy by zaproxy.

the class ExtensionDynSSL method createNewRootCa.

public void createNewRootCa() throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException {
    logger.info("Creating new root CA certificate");
    KeyStore newrootca = SslCertificateUtils.createRootCA();
    setRootCa(newrootca);
    getParams().setRootca(newrootca);
    logger.info("New root CA certificate created");
}
Also used : KeyStore(java.security.KeyStore)

Example 48 with KeyStore

use of java.security.KeyStore in project zaproxy by zaproxy.

the class SslCertificateUtils method pem2KeyStore.

public static KeyStore pem2KeyStore(byte[] certBytes, byte[] keyBytes) throws IOException, CertificateException, InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException {
    X509Certificate cert = generateCertificateFromDER(certBytes);
    RSAPrivateKey key = generatePrivateKeyFromDER(keyBytes);
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(null);
    keystore.setCertificateEntry("cert-alias", cert);
    keystore.setKeyEntry(SslCertificateService.ZAPROXY_JKS_ALIAS, key, SslCertificateService.PASSPHRASE, new Certificate[] { cert });
    return keystore;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 49 with KeyStore

use of java.security.KeyStore in project zaproxy by zaproxy.

the class SslCertificateUtils method string2Keystore.

/**
	 * @param str
	 * @return
	 * @throws KeyStoreException
	 * @throws IOException
	 * @throws CertificateException
	 * @throws NoSuchAlgorithmException
	 */
public static final KeyStore string2Keystore(String str) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    final byte[] bytes = Base64.decodeBase64(str);
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(bais, SslCertificateService.PASSPHRASE);
    bais.close();
    return ks;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KeyStore(java.security.KeyStore)

Example 50 with KeyStore

use of java.security.KeyStore in project spring-boot by spring-projects.

the class UndertowServletWebServerFactory method loadKeyStore.

private KeyStore loadKeyStore(String type, String resource, String password) throws Exception {
    type = (type == null ? "JKS" : type);
    if (resource == null) {
        return null;
    }
    KeyStore store = KeyStore.getInstance(type);
    URL url = ResourceUtils.getURL(resource);
    store.load(url.openStream(), password == null ? null : password.toCharArray());
    return store;
}
Also used : KeyStore(java.security.KeyStore) URL(java.net.URL)

Aggregations

KeyStore (java.security.KeyStore)899 IOException (java.io.IOException)226 X509Certificate (java.security.cert.X509Certificate)216 FileInputStream (java.io.FileInputStream)186 InputStream (java.io.InputStream)177 KeyStoreException (java.security.KeyStoreException)174 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)165 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)146 Certificate (java.security.cert.Certificate)144 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)136 SSLContext (javax.net.ssl.SSLContext)130 CertificateException (java.security.cert.CertificateException)115 PrivateKey (java.security.PrivateKey)104 File (java.io.File)95 CertificateFactory (java.security.cert.CertificateFactory)80 ByteArrayInputStream (java.io.ByteArrayInputStream)78 UnrecoverableKeyException (java.security.UnrecoverableKeyException)64 Key (java.security.Key)63 TrustManager (javax.net.ssl.TrustManager)60 Test (org.junit.Test)54