summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/utils/SSLUtils.java
blob: 75bb1dd6bb121e4db6bfafe4798ffd69d27abcb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package de.spline.kvm.utils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;

public class SSLUtils
{
    public static SSLSocketFactory getSocketFactory()
    {
        SSLContext ctx = null;

        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(null, getTrustManagers(), null);
        }
        catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        return ctx.getSocketFactory();
    }

    private static TrustManager[] getTrustManagers()
            throws GeneralSecurityException, IOException
    {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(loadKeyStore());
        return trustManagerFactory.getTrustManagers();
    }

    private static KeyStore loadKeyStore()
            throws GeneralSecurityException, IOException
    {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        InputStream in = ClassLoader.getSystemResourceAsStream("dfn-g2.jks");
        keyStore.load(in, "changeit".toCharArray());
        return keyStore;
    }
}