|
1 | 1 | package com.zitadel; |
2 | 2 |
|
| 3 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 4 | + |
3 | 5 | import javax.annotation.Nullable; |
| 6 | +import javax.net.ssl.SSLContext; |
| 7 | +import javax.net.ssl.TrustManager; |
| 8 | +import javax.net.ssl.TrustManagerFactory; |
| 9 | +import javax.net.ssl.X509TrustManager; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.security.GeneralSecurityException; |
| 13 | +import java.security.KeyStore; |
| 14 | +import java.security.cert.CertificateFactory; |
| 15 | +import java.security.cert.X509Certificate; |
4 | 16 | import java.util.Collections; |
5 | 17 | import java.util.LinkedHashMap; |
6 | 18 | import java.util.Map; |
@@ -75,6 +87,71 @@ public String getProxyUrl() { |
75 | 87 | return proxyUrl; |
76 | 88 | } |
77 | 89 |
|
| 90 | + /** |
| 91 | + * Builds an {@link SSLContext} from these transport options. |
| 92 | + * |
| 93 | + * <p>If insecure mode is enabled, returns a trust-all SSLContext. |
| 94 | + * If a custom CA cert path is set, returns an SSLContext that trusts |
| 95 | + * both the custom CA and the system default CAs. |
| 96 | + * Otherwise returns {@code null} (use JVM defaults). |
| 97 | + * |
| 98 | + * @return the configured SSLContext, or {@code null} if no custom SSL is needed. |
| 99 | + * @throws GeneralSecurityException if SSL context creation fails. |
| 100 | + * @throws IOException if the CA cert file cannot be read. |
| 101 | + */ |
| 102 | + @Nullable |
| 103 | + @SuppressFBWarnings("PATH_TRAVERSAL_IN") |
| 104 | + public SSLContext buildSSLContext() throws GeneralSecurityException, IOException { |
| 105 | + if (insecure) { |
| 106 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 107 | + sslContext.init(null, new TrustManager[]{new InsecureTrustManager()}, null); |
| 108 | + return sslContext; |
| 109 | + } else if (caCertPath != null) { |
| 110 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 111 | + java.security.cert.Certificate caCert; |
| 112 | + try (FileInputStream fis = new FileInputStream(caCertPath)) { |
| 113 | + caCert = cf.generateCertificate(fis); |
| 114 | + } |
| 115 | + KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 116 | + ks.load(null, null); |
| 117 | + ks.setCertificateEntry("custom-ca", caCert); |
| 118 | + TrustManagerFactory defaultTmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 119 | + defaultTmf.init((KeyStore) null); |
| 120 | + int certIndex = 0; |
| 121 | + for (TrustManager tm : defaultTmf.getTrustManagers()) { |
| 122 | + if (tm instanceof X509TrustManager) { |
| 123 | + for (X509Certificate cert : ((X509TrustManager) tm).getAcceptedIssuers()) { |
| 124 | + ks.setCertificateEntry("default-" + certIndex++, cert); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 129 | + tmf.init(ks); |
| 130 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 131 | + sslContext.init(null, tmf.getTrustManagers(), null); |
| 132 | + return sslContext; |
| 133 | + } |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + @SuppressFBWarnings("WEAK_TRUST_MANAGER") |
| 138 | + private static final class InsecureTrustManager implements X509TrustManager { |
| 139 | + @Override |
| 140 | + public void checkClientTrusted(X509Certificate[] chain, String authType) { |
| 141 | + // trust all |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void checkServerTrusted(X509Certificate[] chain, String authType) { |
| 146 | + // trust all |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public X509Certificate[] getAcceptedIssuers() { |
| 151 | + return new X509Certificate[0]; |
| 152 | + } |
| 153 | + } |
| 154 | + |
78 | 155 | /** |
79 | 156 | * Builder for constructing {@link TransportOptions} instances. |
80 | 157 | */ |
|
0 commit comments