summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/socket/WebSocketCertificate.java
blob: 99018e39d9277d4718733ff863e825effcb0d35f (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.simpleframework.http.socket;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

public class WebSocketCertificate {

   private final X509TrustManager trustManager;
   private final X509TrustManager[] trustManagers;
   private final KeyStoreReader keyStoreReader;
   private final SecureProtocol secureProtocol;

   public WebSocketCertificate(KeyStoreReader keyStoreReader, SecureProtocol secureProtocol) {
      this.trustManager = new AnonymousTrustManager();
      this.trustManagers = new X509TrustManager[] { trustManager };
      this.keyStoreReader = keyStoreReader;
      this.secureProtocol = secureProtocol;
   }

   public WebSocketCertificate(KeyStoreReader keyStoreReader, SecureProtocol secureProtocol, X509TrustManager trustManager) {
      this.trustManagers = new X509TrustManager[] { trustManager };
      this.keyStoreReader = keyStoreReader;
      this.secureProtocol = secureProtocol;
      this.trustManager = trustManager;
   }

   public SSLContext getContext() throws Exception {
      KeyManager[] keyManagers = keyStoreReader.getKeyManagers();
      SSLContext secureContext = secureProtocol.getContext();

      secureContext.init(keyManagers, trustManagers, null);

      return secureContext;
   }

   public SSLSocketFactory getSocketFactory() throws Exception {
      KeyManager[] keyManagers = keyStoreReader.getKeyManagers();
      SSLContext secureContext = secureProtocol.getContext();

      secureContext.init(keyManagers, trustManagers, null);

      return secureContext.getSocketFactory();
   }

   public SSLServerSocketFactory getServerSocketFactory() throws Exception {
      KeyManager[] keyManagers = keyStoreReader.getKeyManagers();
      SSLContext secureContext = secureProtocol.getContext();

      secureContext.init(keyManagers, trustManagers, null);

      return secureContext.getServerSocketFactory();
   }
   
   public static enum SecureProtocol {
      DEFAULT("Default"), 
      SSL("SSL"), 
      TLS("TLS");

      private final String protocol;

      private SecureProtocol(String protocol) {
         this.protocol = protocol;
      }

      public SSLContext getContext() throws NoSuchAlgorithmException {
         return SSLContext.getInstance(protocol);
      }
   }   
   
   public static enum KeyStoreType {
      JKS("JKS", "SunX509"), 
      PKCS12("PKCS12", "SunX509");

      private final String algorithm;
      private final String type;

      private KeyStoreType(String type, String algorithm) {
         this.algorithm = algorithm;
         this.type = type;
      }

      public String getType() {
         return type;
      }

      public KeyStore getKeyStore() throws KeyStoreException {
         return KeyStore.getInstance(type);
      }

      public KeyManagerFactory getKeyManagerFactory() throws NoSuchAlgorithmException {
         return KeyManagerFactory.getInstance(algorithm);
      }
   }
   
   public static class KeyStoreReader {

      private final KeyStoreManager keyStoreManager;
      private final String keyManagerPassword;
      private final String keyStorePassword;
      private final File keyStore;

      public KeyStoreReader(KeyStoreType keyStoreType, File keyStore, String keyStorePassword, String keyManagerPassword) {
         this.keyStoreManager = new KeyStoreManager(keyStoreType);
         this.keyManagerPassword = keyManagerPassword;
         this.keyStorePassword = keyStorePassword;
         this.keyStore = keyStore;
      }

      public KeyManager[] getKeyManagers() throws Exception {
         InputStream storeSource = new FileInputStream(keyStore);

         try {
            return keyStoreManager.getKeyManagers(storeSource, keyStorePassword, keyManagerPassword);
         } finally {
            storeSource.close();
         }
      }
   }

   public static class KeyStoreManager {

      private final KeyStoreType keyStoreType;

      public KeyStoreManager(KeyStoreType keyStoreType) {
         this.keyStoreType = keyStoreType;
      }

      public KeyManager[] getKeyManagers(InputStream keyStoreSource, String keyStorePassword, String keyManagerPassword) throws Exception {
         KeyStore keyStore = keyStoreType.getKeyStore();
         KeyManagerFactory keyManagerFactory = keyStoreType.getKeyManagerFactory();

         keyStore.load(keyStoreSource, keyManagerPassword.toCharArray());
         keyManagerFactory.init(keyStore, keyManagerPassword.toCharArray());

         return keyManagerFactory.getKeyManagers();
      }
   }
   
   public static class AnonymousTrustManager implements X509TrustManager {

      public boolean isClientTrusted(X509Certificate[] cert) {
         return true;
      }

      public boolean isServerTrusted(X509Certificate[] cert) {
         return true;
      }

      public X509Certificate[] getAcceptedIssuers() {
         return new X509Certificate[0];
      }

      public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}   

      public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
   }
}