|
1 | 1 | public static void main(String[] args) throws Exception {
|
2 |
| - { |
3 |
| - class InsecureTrustManager implements X509TrustManager { |
4 |
| - @Override |
5 |
| - public X509Certificate[] getAcceptedIssuers() { |
6 |
| - return null; |
7 |
| - } |
| 2 | + { |
| 3 | + class InsecureTrustManager implements X509TrustManager { |
| 4 | + @Override |
| 5 | + public X509Certificate[] getAcceptedIssuers() { |
| 6 | + return null; |
| 7 | + } |
8 | 8 |
|
9 |
| - @Override |
10 |
| - public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
11 |
| - // BAD: Does not verify the certificate chain, allowing any certificate. |
12 |
| - } |
| 9 | + @Override |
| 10 | + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 11 | + // BAD: Does not verify the certificate chain, allowing any certificate. |
| 12 | + } |
13 | 13 |
|
14 |
| - @Override |
15 |
| - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| 14 | + @Override |
| 15 | + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
16 | 16 |
|
17 |
| - } |
18 |
| - } |
19 |
| - SSLContext context = SSLContext.getInstance("TLS"); |
20 |
| - TrustManager[] trustManager = new TrustManager[] { new InsecureTrustManager() }; |
21 |
| - context.init(null, trustManager, null); |
22 |
| - } |
23 |
| - { |
24 |
| - SSLContext context = SSLContext.getInstance("TLS"); |
25 |
| - File certificateFile = new File("path/to/self-signed-certificate"); |
26 |
| - // Create a `KeyStore` with default type |
27 |
| - KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
28 |
| - // `keyStore` is initially empty |
29 |
| - keyStore.load(null, null); |
30 |
| - X509Certificate generatedCertificate; |
31 |
| - try (InputStream cert = new FileInputStream(certificateFile)) { |
32 |
| - generatedCertificate = (X509Certificate) CertificateFactory.getInstance("X509") |
33 |
| - .generateCertificate(cert); |
34 |
| - } |
35 |
| - // Add the self-signed certificate to the key store |
36 |
| - keyStore.setCertificateEntry(certificateFile.getName(), generatedCertificate); |
37 |
| - // Get default `TrustManagerFactory` |
38 |
| - TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
39 |
| - // Use it with our key store that trusts our self-signed certificate |
40 |
| - tmf.init(keyStore); |
41 |
| - TrustManager[] trustManagers = tmf.getTrustManagers(); |
42 |
| - context.init(null, trustManagers, null); // GOOD, we are not using a custom `TrustManager` but instead have |
43 |
| - // added the self-signed certificate we want to trust to the key |
44 |
| - // store. Note, the `trustManagers` will **only** trust this one |
45 |
| - // certificate. |
46 |
| - URL url = new URL("https://self-signed.badssl.com/"); |
47 |
| - HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); |
48 |
| - conn.setSSLSocketFactory(context.getSocketFactory()); |
49 |
| - } |
| 17 | + } |
| 18 | + } |
| 19 | + SSLContext context = SSLContext.getInstance("TLS"); |
| 20 | + TrustManager[] trustManager = new TrustManager[] { new InsecureTrustManager() }; |
| 21 | + context.init(null, trustManager, null); |
| 22 | + } |
| 23 | + { |
| 24 | + SSLContext context = SSLContext.getInstance("TLS"); |
| 25 | + File certificateFile = new File("path/to/self-signed-certificate"); |
| 26 | + // Create a `KeyStore` with default type |
| 27 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 28 | + // `keyStore` is initially empty |
| 29 | + keyStore.load(null, null); |
| 30 | + X509Certificate generatedCertificate; |
| 31 | + try (InputStream cert = new FileInputStream(certificateFile)) { |
| 32 | + generatedCertificate = (X509Certificate) CertificateFactory.getInstance("X509") |
| 33 | + .generateCertificate(cert); |
| 34 | + } |
| 35 | + // Add the self-signed certificate to the key store |
| 36 | + keyStore.setCertificateEntry(certificateFile.getName(), generatedCertificate); |
| 37 | + // Get default `TrustManagerFactory` |
| 38 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 39 | + // Use it with our key store that trusts our self-signed certificate |
| 40 | + tmf.init(keyStore); |
| 41 | + TrustManager[] trustManagers = tmf.getTrustManagers(); |
| 42 | + context.init(null, trustManagers, null); // GOOD, we are not using a custom `TrustManager` but instead have |
| 43 | + // added the self-signed certificate we want to trust to the key |
| 44 | + // store. Note, the `trustManagers` will **only** trust this one |
| 45 | + // certificate. |
| 46 | + URL url = new URL("https://self-signed.badssl.com/"); |
| 47 | + HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); |
| 48 | + conn.setSSLSocketFactory(context.getSocketFactory()); |
| 49 | + } |
50 | 50 | }
|
0 commit comments