Renamed packages to org.opendaylight.openflowjava.protocol.impl.*
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / SslContextFactory.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
3
4 import java.security.KeyStore;
5 import java.security.Security;
6
7 import javax.net.ssl.KeyManagerFactory;
8 import javax.net.ssl.SSLContext;
9
10 /**
11  * Class for setting up TLS connection.
12  *
13  * @author michal.polkorab
14  */
15 public final class SslContextFactory {
16
17     
18     // "TLS" - supports some version of TLS
19     // Use "TLSv1", "TLSv1.1", "TLSv1.2" for specific TLS version
20     private static final String PROTOCOL = "TLS";
21     private static final SSLContext SERVER_CONTEXT;
22     private static final SSLContext CLIENT_CONTEXT;
23
24     static {
25         String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
26         if (algorithm == null) {
27             algorithm = "SunX509";
28         }
29
30         SSLContext serverContext;
31         SSLContext clientContext;
32         try {
33             KeyStore ks = KeyStore.getInstance("JKS");
34             ks.load(SslKeyStore.asInputStream(),
35                     SslKeyStore.getKeyStorePassword());
36
37             KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
38             kmf.init(ks, SslKeyStore.getCertificatePassword());
39
40             serverContext = SSLContext.getInstance(PROTOCOL);
41             serverContext.init(kmf.getKeyManagers(), null, null);
42         } catch (Exception e) {
43             throw new Error(
44                     "Failed to initialize the server-side SSLContext", e);
45         }
46         try {
47             clientContext = SSLContext.getInstance(PROTOCOL);
48             clientContext.init(null, SslTrustManagerFactory.getTrustManagers(), null);
49         } catch (Exception e) {
50             throw new Error(
51                     "Failed to initialize the client-side SSLContext", e);
52         }
53
54         SERVER_CONTEXT = serverContext;
55         CLIENT_CONTEXT = clientContext;
56     }
57
58     /**
59      * @return servercontext
60      */
61     public static SSLContext getServerContext() {
62         return SERVER_CONTEXT;
63     }
64
65     /**
66      * @return cliencontext
67      */
68     public static SSLContext getClientContext() {
69         return CLIENT_CONTEXT;
70     }
71 }