Search in sources :

Example 51 with KeyStore

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

the class KeyStoreEncryptionStore method main.

public static void main(String[] args) throws Exception {
    if (!args[0].equals("explode")) {
        throw new IllegalStateException();
    }
    char[] password = "notasecret".toCharArray();
    ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
    KeyStore keyStore = KeyStoreUtils.load(new File(args[1]));
    File dest = new File(args[2]);
    dest.mkdirs();
    Enumeration<String> aliases = keyStore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        if (keyStore.isKeyEntry(alias)) {
            Entry entry = keyStore.getEntry(alias, protParam);
            PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) entry;
            {
                X509Certificate[] certificateChain = toX509(privateKeyEntry.getCertificateChain());
                String encoded = CertificateUtils.toPem(certificateChain);
                File out = new File(dest, alias + ".crt");
                Files.write(encoded, out, Charsets.UTF_8);
            }
            {
                PrivateKey key = privateKeyEntry.getPrivateKey();
                String encoded = PrivateKeys.toPem(key);
                File out = new File(dest, alias + ".key");
                Files.write(encoded, out, Charsets.UTF_8);
            }
        }
        if (keyStore.isCertificateEntry(alias)) {
            Entry entry = keyStore.getEntry(alias, null);
            TrustedCertificateEntry trustedCertificateEntry = (TrustedCertificateEntry) entry;
            X509Certificate cert = (X509Certificate) trustedCertificateEntry.getTrustedCertificate();
            String encoded = CertificateUtils.toPem(cert);
            File out = new File(dest, alias + ".crt");
            Files.write(encoded, out, Charsets.UTF_8);
        }
    }
}
Also used : PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) Entry(java.security.KeyStore.Entry) File(java.io.File) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) ProtectionParameter(java.security.KeyStore.ProtectionParameter)

Example 52 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class HttpsServerTestSupport method getSSLContext.

@Override
protected SSLContext getSSLContext() throws Exception {
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, PASSWORD.toCharArray());
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    SSLContext sslcontext = SSLContext.getInstance(SECURE_SOCKET_PROTOCOL);
    sslcontext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    return sslcontext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 53 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.

protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
    Properties supplementalProperties = new Properties();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    SecureRandom sr = null;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    // Ignore
    }
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(null, null, null);
    SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
    supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
    supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
    supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
    supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
    if (sr != null) {
        supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
        supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
    }
    supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
    supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
    // Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
    String ssp = "";
    for (String protocol : socket.getSupportedProtocols()) {
        if (!"SSLv2Hello".equals(protocol)) {
            ssp = protocol;
            break;
        }
    }
    supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
    return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 54 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class KeyStoreParametersTest method testExplicitType.

public void testExplicitType() throws Exception {
    KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
    ksp.setType("jks");
    KeyStore ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
}
Also used : KeyStore(java.security.KeyStore)

Example 55 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class KeyStoreParametersTest method testValidParameters.

public void testValidParameters() throws GeneralSecurityException, IOException, URISyntaxException {
    KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
    KeyStore ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
    URL resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
    ksp.setResource(resourceUrl.toExternalForm());
    ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
    resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
    File file = new File(resourceUrl.toURI());
    ksp.setResource(file.getAbsolutePath());
    ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
}
Also used : KeyStore(java.security.KeyStore) File(java.io.File) 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