Mercurial > 510Connectbot
annotate src/org/tn5250j/framework/transport/SSL/SSLImplementation.java @ 38:011462bddcf1 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 10 Jun 2014 16:56:20 -0700 |
parents | 9ae1c889a64c |
children | 8f23b05a51f7 |
rev | line source |
---|---|
3 | 1 package org.tn5250j.framework.transport.SSL; |
2 | |
3 /* | |
4 * @(#)SSLImplementation.java | |
5 * @author Stephen M. Kennedy | |
6 * | |
7 * Copyright: Copyright (c) 2001 | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2, or (at your option) | |
12 * any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this software; see the file COPYING. If not, write to | |
21 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
22 * Boston, MA 02111-1307 USA | |
23 * | |
24 */ | |
25 | |
26 import java.io.File; | |
27 import java.io.FileInputStream; | |
28 import java.io.FileOutputStream; | |
29 import java.net.Socket; | |
30 import java.security.KeyStore; | |
31 import java.security.cert.CertificateException; | |
32 import java.security.cert.X509Certificate; | |
33 | |
34 import javax.net.ssl.KeyManagerFactory; | |
35 import javax.net.ssl.SSLContext; | |
36 import javax.net.ssl.SSLSocket; | |
37 import javax.net.ssl.TrustManager; | |
38 import javax.net.ssl.TrustManagerFactory; | |
39 import javax.net.ssl.X509TrustManager; | |
8 | 40 |
13 | 41 import com.five_ten_sg.connectbot.R; |
8 | 42 import com.five_ten_sg.connectbot.service.TerminalBridge; |
43 import com.five_ten_sg.connectbot.service.TerminalManager; | |
3 | 44 |
45 import org.tn5250j.GlobalConfigure; | |
46 import org.tn5250j.framework.transport.SSLInterface; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
47 import android.util.Log; |
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
48 |
3 | 49 |
50 /** | |
51 * <p> | |
52 * This class implements the SSLInterface and is used to create SSL socket | |
53 * instances. | |
54 * </p> | |
8 | 55 * |
3 | 56 * @author Stephen M. Kennedy <skennedy@tenthpowertech.com> |
8 | 57 * |
3 | 58 */ |
59 public class SSLImplementation implements SSLInterface, X509TrustManager { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
60 private static final String TAG = "SSLImplementation"; |
3 | 61 SSLContext sslContext = null; |
62 KeyStore userks = null; | |
63 private String userKsPath; | |
64 private char[] userksPassword = "changeit".toCharArray(); | |
65 | |
8 | 66 TerminalBridge bridge = null; |
67 TerminalManager manager = null; | |
68 String target = null; // destination:port | |
69 | |
3 | 70 KeyManagerFactory userkmf = null; |
71 | |
72 TrustManagerFactory usertmf = null; | |
73 | |
74 TrustManager[] userTrustManagers = null; | |
75 | |
76 X509Certificate[] acceptedIssuers; | |
77 | |
8 | 78 public SSLImplementation(TerminalBridge bridge, TerminalManager manager) { |
79 this.bridge = bridge; | |
80 this.manager = manager; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
81 |
3 | 82 } |
83 | |
84 public void init(String sslType) { | |
85 try { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
86 Log.d(TAG,"Initializing User KeyStore"); |
3 | 87 userKsPath = System.getProperty("user.home") + File.separator |
88 + GlobalConfigure.TN5250J_FOLDER + File.separator + "keystore"; | |
89 File userKsFile = new File(userKsPath); | |
90 userks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
91 userks.load(userKsFile.exists() ? new FileInputStream(userKsFile) | |
92 : null, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
93 Log.d(TAG,"Initializing User Key Manager Factory"); |
3 | 94 userkmf = KeyManagerFactory.getInstance(KeyManagerFactory |
95 .getDefaultAlgorithm()); | |
96 userkmf.init(userks, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
97 Log.d(TAG,"Initializing User Trust Manager Factory"); |
3 | 98 usertmf = TrustManagerFactory.getInstance(TrustManagerFactory |
99 .getDefaultAlgorithm()); | |
100 usertmf.init(userks); | |
101 userTrustManagers = usertmf.getTrustManagers(); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
102 Log.d(TAG,"Initializing SSL Context"); |
3 | 103 sslContext = SSLContext.getInstance(sslType); |
104 sslContext.init(userkmf.getKeyManagers(), new TrustManager[] {this}, null); | |
105 } catch (Exception ex) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
106 Log.e(TAG,"Error initializing SSL [" + ex.getMessage() + "]"); |
3 | 107 } |
108 | |
109 } | |
110 | |
111 public Socket createSSLSocket(String destination, int port) { | |
112 if (sslContext == null) | |
113 throw new IllegalStateException("SSL Context Not Initialized"); | |
114 SSLSocket socket = null; | |
115 try { | |
8 | 116 target = destination + ":" + String.valueOf(port); |
3 | 117 socket = (SSLSocket) sslContext.getSocketFactory().createSocket( |
118 destination, port); | |
119 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
120 Log.e(TAG,"Error creating ssl socket [" + e.getMessage() + "]"); |
3 | 121 } |
122 return socket; | |
123 } | |
124 | |
125 // X509TrustManager Methods | |
126 | |
127 /* | |
128 * (non-Javadoc) | |
8 | 129 * |
3 | 130 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
131 */ | |
132 public X509Certificate[] getAcceptedIssuers() { | |
133 return acceptedIssuers; | |
134 } | |
135 | |
136 /* | |
137 * (non-Javadoc) | |
8 | 138 * |
3 | 139 * @see |
140 * javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert. | |
141 * X509Certificate[], java.lang.String) | |
142 */ | |
143 public void checkClientTrusted(X509Certificate[] arg0, String arg1) | |
144 throws CertificateException { | |
145 throw new SecurityException("checkClientTrusted unsupported"); | |
146 | |
147 } | |
148 | |
149 /* | |
150 * (non-Javadoc) | |
8 | 151 * |
3 | 152 * @see |
153 * javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert. | |
154 * X509Certificate[], java.lang.String) | |
155 */ | |
156 public void checkServerTrusted(X509Certificate[] chain, String type) | |
157 throws CertificateException { | |
158 try { | |
159 for (int i = 0; i < userTrustManagers.length; i++) { | |
160 if (userTrustManagers[i] instanceof X509TrustManager) { | |
161 X509TrustManager trustManager = (X509TrustManager) userTrustManagers[i]; | |
162 X509Certificate[] calist = trustManager | |
163 .getAcceptedIssuers(); | |
164 if (calist.length > 0) { | |
165 trustManager.checkServerTrusted(chain, type); | |
166 } else { | |
167 throw new CertificateException( | |
168 "Empty list of accepted issuers (a.k.a. root CA list)."); | |
169 } | |
170 } | |
171 } | |
172 return; | |
173 } catch (CertificateException ce) { | |
174 X509Certificate cert = chain[0]; | |
38
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
175 String certInfo = manager.res.getString(R.string.host_cert_version) + cert.getVersion() + "\r\n"; |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
176 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_serial) + cert.getSerialNumber() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
177 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_algorithm) + cert.getSigAlgName() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
178 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_issuer) + cert.getIssuerDN().getName() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
179 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_from) + cert.getNotBefore() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
180 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_to) + cert.getNotAfter() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
181 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_dn) + cert.getSubjectDN().getName() + "\r\n"); |
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
182 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_publickey) + cert.getPublicKey().getFormat() + "\r\n"); |
3 | 183 |
8 | 184 bridge.outputLine(manager.res.getString(R.string.host_authenticity_warning, target)); |
185 bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo)); | |
186 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate)); | |
9 | 187 if ((result == null) || (!result.booleanValue())) { |
3 | 188 throw new java.security.cert.CertificateException( |
189 "Certificate Rejected"); | |
190 } | |
191 | |
8 | 192 result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_save_certificate)); |
9 | 193 if ((result != null) && (result.booleanValue())) { |
3 | 194 try { |
195 userks.setCertificateEntry(cert.getSubjectDN().getName(), | |
196 cert); | |
197 userks.store(new FileOutputStream(userKsPath), | |
198 userksPassword); | |
199 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
200 Log.e(TAG,"Error saving certificate [" + e.getMessage() |
3 | 201 + "]"); |
202 e.printStackTrace(); | |
203 } | |
204 } | |
205 } | |
206 } | |
207 } |