1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.innergrid.api;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.security.KeyManagementException;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.UnrecoverableKeyException;
29 import java.security.cert.CertificateException;
30
31 import javax.net.ssl.KeyManagerFactory;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLSocketFactory;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.TrustManagerFactory;
36
37 import com.gridsystems.innergrid.kernel.KernelException;
38
39
40
41
42
43
44
45
46 public class AcceptCertificateOfKeyStore implements SSLConnectionInfo {
47
48
49
50
51 private File keystorefile;
52
53
54
55
56 private String keystorepassword;
57
58
59
60
61
62
63
64 public AcceptCertificateOfKeyStore(String kfilepath, String kpassword) {
65 this(new File(kfilepath), kpassword);
66 }
67
68
69
70
71
72
73
74 public AcceptCertificateOfKeyStore(File kfile, String kpassword) {
75 keystorefile = kfile;
76 keystorepassword = kpassword;
77 }
78
79
80
81
82
83
84 public String getKeystoreFile() {
85 return keystorefile.getPath();
86 }
87
88
89
90
91
92
93 public File getKeystore() {
94 return keystorefile;
95 }
96
97
98
99
100
101
102 public String getKeystorePassword() {
103 return (keystorepassword);
104 }
105
106
107
108
109
110
111
112 public SSLSocketFactory getSSLSocketFactory() throws KernelException {
113
114 File keystoreFile = getKeystore();
115 if (!keystoreFile.exists()) {
116
117 throw new CKernelException("CLT032", keystoreFile.getPath());
118 }
119
120 String keystorePass = getKeystorePassword();
121 if (keystorePass == null) {
122 keystorePass = "changeit";
123 }
124
125
126
127 KeyStore kstore = initKeyStore(keystoreFile, keystorePass);
128
129
130 final String algorithm = "SunX509";
131
132 try {
133
134
135 KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
136 kmf.init(kstore, keystorePass.toCharArray());
137
138
139 TrustManagerFactory tmf;
140 tmf = TrustManagerFactory.getInstance(algorithm);
141 tmf.init(kstore);
142
143
144 TrustManager[] tm = null;
145 tm = tmf.getTrustManagers();
146
147
148
149 final String protocol = "TLS";
150 SSLContext context = SSLContext.getInstance(protocol);
151
152
153 context.init(kmf.getKeyManagers(), tm, new java.security.SecureRandom());
154 return (SSLSocketFactory) context.getSocketFactory();
155
156 } catch (NoSuchAlgorithmException e) {
157
158 throw new CKernelException(e, "CLT031");
159 } catch (KeyStoreException e) {
160
161 throw new CKernelException(e, "CLT031");
162 } catch (UnrecoverableKeyException e) {
163
164 throw new CKernelException(e, "CLT031");
165 } catch (KeyManagementException e) {
166
167 throw new CKernelException(e, "CLT031");
168 }
169 }
170
171
172
173
174
175
176
177
178
179
180 private KeyStore initKeyStore(File keystoreFile, String keyPass)
181 throws KernelException {
182
183 String path = keystoreFile.getAbsolutePath();
184 try {
185 final String defaultKeystoreType = "JKS";
186 KeyStore kstore = KeyStore.getInstance(defaultKeystoreType);
187 InputStream istream = new FileInputStream(keystoreFile);
188 kstore.load(istream, keyPass.toCharArray());
189 return kstore;
190 } catch (FileNotFoundException fnfe) {
191 fnfe.printStackTrace();
192
193 throw new CKernelException(fnfe, "CLT032", path);
194 } catch (IOException ioe) {
195 ioe.printStackTrace();
196
197 throw new CKernelException(ioe, "CLT033", path);
198 } catch (KeyStoreException e) {
199 e.printStackTrace();
200
201 throw new CKernelException(e, "CLT031");
202 } catch (NoSuchAlgorithmException e) {
203 e.printStackTrace();
204
205 throw new CKernelException(e, "CLT031");
206 } catch (CertificateException e) {
207 e.printStackTrace();
208
209 throw new CKernelException(e, "CLT031");
210 }
211 }
212 }