|
| 1 | +package com.datastax.oss.driver.internal.core.config.scyllacloud; |
| 2 | + |
| 3 | +import com.datastax.oss.driver.api.core.ssl.SslEngineFactory; |
| 4 | +import com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.security.GeneralSecurityException; |
| 10 | +import java.security.KeyStore; |
| 11 | +import java.security.KeyStoreException; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | +import java.security.SecureRandom; |
| 14 | +import java.security.cert.CertificateException; |
| 15 | +import java.security.cert.X509Certificate; |
| 16 | +import javax.net.ssl.KeyManagerFactory; |
| 17 | +import javax.net.ssl.SSLContext; |
| 18 | +import javax.net.ssl.TrustManager; |
| 19 | +import javax.net.ssl.TrustManagerFactory; |
| 20 | +import javax.net.ssl.X509TrustManager; |
| 21 | + |
| 22 | +public class ConfigurationBundle { |
| 23 | + private final KeyStore identity; |
| 24 | + private final KeyStore trustStore; |
| 25 | + |
| 26 | + public ConfigurationBundle(KeyStore identity, KeyStore trustStore) { |
| 27 | + this.identity = identity; |
| 28 | + this.trustStore = trustStore; |
| 29 | + } |
| 30 | + |
| 31 | + public KeyStore getIdentity() { |
| 32 | + return identity; |
| 33 | + } |
| 34 | + |
| 35 | + public KeyStore getTrustStore() { |
| 36 | + return trustStore; |
| 37 | + } |
| 38 | + |
| 39 | + private void writeKeystore(String path, KeyStore ks, char[] password) |
| 40 | + throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException { |
| 41 | + File file = new File(path); |
| 42 | + OutputStream os = new FileOutputStream(file); |
| 43 | + ks.store(os, password); |
| 44 | + os.close(); |
| 45 | + } |
| 46 | + |
| 47 | + public void writeIdentity(String path, char[] password) |
| 48 | + throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { |
| 49 | + writeKeystore(path, identity, password); |
| 50 | + } |
| 51 | + |
| 52 | + public void writeTrustStore(String path, char[] password) |
| 53 | + throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { |
| 54 | + writeKeystore(path, trustStore, password); |
| 55 | + } |
| 56 | + |
| 57 | + protected SSLContext getSSLContext() throws IOException, GeneralSecurityException { |
| 58 | + KeyManagerFactory kmf = createKeyManagerFactory(identity); |
| 59 | + TrustManagerFactory tmf = createTrustManagerFactory(trustStore); |
| 60 | + SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 61 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); |
| 62 | + return sslContext; |
| 63 | + } |
| 64 | + |
| 65 | + protected SSLContext getInsecureSSLContext() throws IOException, GeneralSecurityException { |
| 66 | + KeyManagerFactory kmf = createKeyManagerFactory(identity); |
| 67 | + SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 68 | + TrustManager[] trustManager = |
| 69 | + new TrustManager[] { |
| 70 | + new X509TrustManager() { |
| 71 | + @Override |
| 72 | + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) |
| 73 | + throws CertificateException {} |
| 74 | + |
| 75 | + @Override |
| 76 | + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) |
| 77 | + throws CertificateException {} |
| 78 | + |
| 79 | + @Override |
| 80 | + public X509Certificate[] getAcceptedIssuers() { |
| 81 | + return new X509Certificate[0]; |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + sslContext.init(kmf.getKeyManagers(), trustManager, new SecureRandom()); |
| 87 | + return sslContext; |
| 88 | + } |
| 89 | + |
| 90 | + protected KeyManagerFactory createKeyManagerFactory(KeyStore ks) |
| 91 | + throws IOException, GeneralSecurityException { |
| 92 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 93 | + kmf.init(ks, "cassandra".toCharArray()); |
| 94 | + return kmf; |
| 95 | + } |
| 96 | + |
| 97 | + protected TrustManagerFactory createTrustManagerFactory(KeyStore ts) |
| 98 | + throws IOException, GeneralSecurityException { |
| 99 | + TrustManagerFactory tmf = |
| 100 | + TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 101 | + tmf.init(ts); |
| 102 | + return tmf; |
| 103 | + } |
| 104 | + |
| 105 | + public SslEngineFactory getSSLEngineFactory() throws GeneralSecurityException, IOException { |
| 106 | + return new SniSslEngineFactory(getSSLContext()); |
| 107 | + } |
| 108 | +} |
0 commit comments