Search in sources :

Example 56 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project wso2-synapse by wso2.

the class RabbitMQStore method initme.

private boolean initme() {
    Set<Map.Entry<String, Object>> mapSet = parameters.entrySet();
    for (Map.Entry<String, Object> e : mapSet) {
        if (e.getValue() instanceof String) {
            properties.put(e.getKey(), e.getValue());
        }
    }
    userName = (String) parameters.get(USERNAME);
    password = (String) parameters.get(PASSWORD);
    hostName = (String) parameters.get(HOST_NAME);
    hostPort = (String) parameters.get(HOST_PORT);
    virtualHost = (String) parameters.get(VIRTUAL_HOST);
    // Possible timeouts that can be added in future if requested, should be added to the
    // setConnectionTimeout, ShutdownTimeout, RequestedHeartbeat
    connectionFactory = new ConnectionFactory();
    if (hostName != null && !hostName.equals("")) {
        connectionFactory.setHost(hostName);
    } else {
        throw new SynapseException(nameString() + " host name is not correctly defined");
    }
    int port = 0;
    try {
        port = Integer.parseInt(hostPort);
    } catch (NumberFormatException nfe) {
        logger.error("Port value for " + nameString() + " is not correctly defined" + nfe);
    }
    if (port > 0) {
        connectionFactory.setPort(port);
    } else {
        connectionFactory.setPort(DEFAULT_PORT);
        logger.info(nameString() + " port is set to default value (5672");
    }
    if (userName != null && !userName.equals("")) {
        connectionFactory.setUsername(userName);
    }
    if (password != null && !password.equals("")) {
        connectionFactory.setPassword(password);
    }
    if (virtualHost != null && !virtualHost.equals("")) {
        connectionFactory.setVirtualHost(virtualHost);
    }
    String sslEnabledS = parameters.get(SSL_ENABLED) != null ? parameters.get(SSL_ENABLED).toString() : "";
    if (!StringUtils.isEmpty(sslEnabledS)) {
        try {
            boolean sslEnabled = Boolean.parseBoolean(sslEnabledS);
            if (sslEnabled) {
                String keyStoreLocation = parameters.get(SSL_KEYSTORE_LOCATION) != null ? parameters.get(SSL_KEYSTORE_LOCATION).toString() : "";
                String keyStoreType = parameters.get(SSL_KEYSTORE_TYPE) != null ? parameters.get(SSL_KEYSTORE_TYPE).toString() : "";
                String keyStorePassword = parameters.get(SSL_KEYSTORE_PASSWORD) != null ? parameters.get(SSL_KEYSTORE_PASSWORD).toString() : "";
                String trustStoreLocation = parameters.get(SSL_TRUSTSTORE_LOCATION) != null ? parameters.get(SSL_TRUSTSTORE_LOCATION).toString() : "";
                String trustStoreType = parameters.get(SSL_TRUSTSTORE_TYPE) != null ? parameters.get(SSL_TRUSTSTORE_TYPE).toString() : "";
                String trustStorePassword = parameters.get(SSL_TRUSTSTORE_PASSWORD) != null ? parameters.get(SSL_TRUSTSTORE_PASSWORD).toString() : "";
                String sslVersion = parameters.get(SSL_VERSION) != null ? parameters.get(SSL_VERSION).toString() : "";
                if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType) || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation) || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) {
                    logger.warn("Trustore and keystore information is not provided correctly. Proceeding with default SSL configuration");
                    connectionFactory.useSslProtocol();
                } else {
                    char[] keyPassphrase = keyStorePassword.toCharArray();
                    KeyStore ks = KeyStore.getInstance(keyStoreType);
                    ks.load(new FileInputStream(keyStoreLocation), keyPassphrase);
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    kmf.init(ks, keyPassphrase);
                    char[] trustPassphrase = trustStorePassword.toCharArray();
                    KeyStore tks = KeyStore.getInstance(trustStoreType);
                    tks.load(new FileInputStream(trustStoreLocation), trustPassphrase);
                    TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    tmf.init(tks);
                    SSLContext c = SSLContext.getInstance(sslVersion);
                    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                    connectionFactory.useSslProtocol(c);
                }
            }
        } catch (Exception e) {
            logger.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e);
        }
    }
    // declaring queue
    String queueName = (String) parameters.get(QUEUE_NAME);
    if (queueName != null) {
        this.queueName = queueName;
    } else {
        String name = getName();
        String defaultQueue;
        if (name != null && !name.isEmpty()) {
            defaultQueue = name + "_Queue";
        } else {
            defaultQueue = "RabiitmqStore_" + System.currentTimeMillis() + "_Queue";
        }
        logger.warn(nameString() + ". Destination not provided. " + "Setting default destination to [" + defaultQueue + "].");
        this.queueName = defaultQueue;
    }
    exchangeName = (String) properties.get(EXCHANGE_NAME);
    routeKey = (String) properties.get(ROUTE_KEY);
    if (routeKey == null) {
        logger.warn(nameString() + ". Routing key is not provided. " + "Setting queue name " + this.queueName + " as routing key.");
        routeKey = this.queueName;
    }
    if (!newProducerConnection()) {
        logger.warn(nameString() + ". Starting with a faulty connection to the broker.");
        return false;
    }
    try {
        setQueue();
    } catch (IOException e) {
        logger.error(nameString() + " error in storage declaring queue " + queueName);
        return false;
    }
    return true;
}
Also used : SynapseException(org.apache.synapse.SynapseException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Map(java.util.Map)

Example 57 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project incubator-servicecomb-java-chassis by apache.

the class KeyStoreUtil method createKeyManagers.

public static KeyManager[] createKeyManagers(final KeyStore keystore, char[] keyvalue) {
    try {
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, keyvalue);
        return kmfactory.getKeyManagers();
    } catch (Exception e) {
        throw new IllegalArgumentException("Bad key store." + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) CRLException(java.security.cert.CRLException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 58 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project data-transfer-project by google.

the class SingleVMMain method initializeGateway.

public void initializeGateway() {
    ApiMain apiMain = new ApiMain();
    try (InputStream stream = ReferenceApiServer.class.getClassLoader().getResourceAsStream("demo-selfsigned-keystore.jks")) {
        if (stream == null) {
            throw new IllegalArgumentException("Demo keystore was not found");
        }
        // initialise the keystore
        char[] password = "password".toCharArray();
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(stream, password);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        keyManagerFactory.init(keyStore, password);
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
        trustManagerFactory.init(keyStore);
        apiMain.initializeHttps(trustManagerFactory, keyManagerFactory);
        apiMain.start();
    } catch (Exception e) {
        errorCallback.accept(e);
    }
}
Also used : InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ApiMain(org.dataportabilityproject.gateway.ApiMain) KeyStore(java.security.KeyStore) ReferenceApiServer(org.dataportabilityproject.gateway.reference.ReferenceApiServer) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 59 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project fdroidclient by f-droid.

the class LocalRepoKeyStore method addToStore.

private void addToStore(String alias, KeyPair kp, Certificate cert) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
    Certificate[] chain = { cert };
    keyStore.setKeyEntry(alias, kp.getPrivate(), "".toCharArray(), chain);
    keyStore.store(new FileOutputStream(keyStoreFile), "".toCharArray());
    /*
         * After adding an entry to the keystore we need to create a fresh
         * KeyManager by reinitializing the KeyManagerFactory with the new key
         * store content and then rewrapping the default KeyManager with our own
         */
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, "".toCharArray());
    KeyManager defaultKeyManager = keyManagerFactory.getKeyManagers()[0];
    KeyManager wrappedKeyManager = new KerplappKeyManager((X509KeyManager) defaultKeyManager);
    keyManagers = new KeyManager[] { wrappedKeyManager };
}
Also used : FileOutputStream(java.io.FileOutputStream) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 60 with KeyManagerFactory

use of javax.net.ssl.KeyManagerFactory in project incubator-gobblin by apache.

the class SSLContextFactory method createInstance.

/**
 * Create a {@link SSLContext} instance
 *
 * @param keyStoreFile a p12 or jks file depending on key store type
 * @param keyStorePassword password to access the key store
 * @param keyStoreType type of key store
 * @param trustStoreFile a jks file
 * @param trustStorePassword password to access the trust store
 */
public static SSLContext createInstance(File keyStoreFile, String keyStorePassword, String keyStoreType, File trustStoreFile, String trustStorePassword) {
    if (!keyStoreType.equalsIgnoreCase(P12_STORE_TYPE_NAME) && !keyStoreType.equalsIgnoreCase(JKS_STORE_TYPE_NAME)) {
        throw new IllegalArgumentException("Unsupported keyStoreType: " + keyStoreType);
    }
    try {
        // Load KeyStore
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(toInputStream(keyStoreFile), keyStorePassword.toCharArray());
        // Load TrustStore
        KeyStore trustStore = KeyStore.getInstance(JKS_STORE_TYPE_NAME);
        trustStore.load(toInputStream(trustStoreFile), trustStorePassword.toCharArray());
        // Set KeyManger from keyStore
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(DEFAULT_ALGORITHM);
        kmf.init(keyStore, keyStorePassword.toCharArray());
        // Set TrustManager from trustStore
        TrustManagerFactory trustFact = TrustManagerFactory.getInstance(DEFAULT_ALGORITHM);
        trustFact.init(trustStore);
        // Set Context to TLS and initialize it
        SSLContext sslContext = SSLContext.getInstance(DEFAULT_PROTOCOL);
        sslContext.init(kmf.getKeyManagers(), trustFact.getTrustManagers(), null);
        return sslContext;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

KeyManagerFactory (javax.net.ssl.KeyManagerFactory)439 KeyStore (java.security.KeyStore)322 SSLContext (javax.net.ssl.SSLContext)218 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)203 FileInputStream (java.io.FileInputStream)135 IOException (java.io.IOException)122 InputStream (java.io.InputStream)106 KeyManager (javax.net.ssl.KeyManager)104 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)79 TrustManager (javax.net.ssl.TrustManager)76 KeyStoreException (java.security.KeyStoreException)62 SecureRandom (java.security.SecureRandom)58 CertificateException (java.security.cert.CertificateException)57 UnrecoverableKeyException (java.security.UnrecoverableKeyException)54 KeyManagementException (java.security.KeyManagementException)51 File (java.io.File)37 X509Certificate (java.security.cert.X509Certificate)33 GeneralSecurityException (java.security.GeneralSecurityException)31 X509TrustManager (javax.net.ssl.X509TrustManager)29 Certificate (java.security.cert.Certificate)28