CoolFace
Apppublic

Deeps-2005/java-ssl-scanner

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
SecureTrustManager.java67 linesDownload Raw Back to sample
1import javax.net.ssl.*;
2import java.security.cert.CertificateException;
3import java.security.cert.X509Certificate;
4import java.security.KeyStore;
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.nio.file.Files;
8import java.nio.file.Paths;
9import java.security.cert.CertificateFactory;
10import java.io.InputStream;
11import java.security.SecureRandom; // Ensure SecureRandom is imported
12
13public class SecureTrustManager {
14    public static void main(String[] args) throws Exception {
15        System.out.println("--- Testing Secure TrustManager Implementation ---");
16
17        TrustManager[] secureTrustManagers = new TrustManager[] {
18            new X509TrustManager() {
19                public X509Certificate[] getAcceptedIssuers() {
20                    return new X509Certificate[0];
21                }
22
23                public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
24                    if (certs == null || certs.length == 0) {
25                        throw new IllegalArgumentException("Client certificates are null or empty.");
26                    }
27                    System.out.println("Client trusted check: Performed some validation (simulated).");
28                }
29
30                public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
31                    if (certs == null || certs.length == 0) {
32                        throw new IllegalArgumentException("Server certificates are null or empty.");
33                    }
34                    System.out.println("Server trusted check: Performed some validation (simulated).");
35                }
36            }
37        };
38
39        // Initialize SecureRandom using getInstanceStrong() for cryptographically strong random numbers
40        SecureRandom trulySecureRandom = SecureRandom.getInstanceStrong();
41        System.out.println("SecureRandom initialized with getInstanceStrong().");
42
43
44        // Initialize SSLContext with the secure TrustManager and trulySecureRandom
45        SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
46        sslContext.init(null, secureTrustManagers, trulySecureRandom); // Now uses getInstanceStrong()
47        System.out.println("SSLContext initialized with secure TrustManager and strong SecureRandom.");
48
49        // Simulate a connection (this won't actually connect, just use the context)
50        try {
51            SSLSocketFactory factory = sslContext.getSocketFactory();
52            SSLSocket socket = (SSLSocket) factory.createSocket("example.com", 443);
53            socket.setEnabledProtocols(new String[]{"TLSv1.2", "TLSv1.3"});
54            socket.setEnabledCipherSuites(new String[]{
55                "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
56                "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
57            });
58            System.out.println("Simulated secure socket creation with strong protocols and ciphers.");
59            socket.close();
60        } catch (IOException e) {
61            System.err.println("Simulated socket connection error (expected if no actual server): " + e.getMessage());
62        }
63
64        System.out.println("Secure TrustManager test complete.");
65    }
66}
67