Mercurial > 510Connectbot
annotate src/org/tn5250j/framework/transport/SSL/SSLImplementation.java @ 84:8f23b05a51f7
convert ctrl keys to virtual keys; use proper android home directory
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 16 Jun 2014 11:25:37 -0700 |
parents | 011462bddcf1 |
children | 53de03150941 |
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"); |
84
8f23b05a51f7
convert ctrl keys to virtual keys; use proper android home directory
Carl Byington <carl@five-ten-sg.com>
parents:
38
diff
changeset
|
87 userKsPath = GlobalConfigure.settingsDirectory() + "keystore"; |
3 | 88 File userKsFile = new File(userKsPath); |
89 userks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
90 userks.load(userKsFile.exists() ? new FileInputStream(userKsFile) | |
91 : null, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
92 Log.d(TAG,"Initializing User Key Manager Factory"); |
3 | 93 userkmf = KeyManagerFactory.getInstance(KeyManagerFactory |
94 .getDefaultAlgorithm()); | |
95 userkmf.init(userks, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
96 Log.d(TAG,"Initializing User Trust Manager Factory"); |
3 | 97 usertmf = TrustManagerFactory.getInstance(TrustManagerFactory |
98 .getDefaultAlgorithm()); | |
99 usertmf.init(userks); | |
100 userTrustManagers = usertmf.getTrustManagers(); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
101 Log.d(TAG,"Initializing SSL Context"); |
3 | 102 sslContext = SSLContext.getInstance(sslType); |
103 sslContext.init(userkmf.getKeyManagers(), new TrustManager[] {this}, null); | |
104 } catch (Exception ex) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
105 Log.e(TAG,"Error initializing SSL [" + ex.getMessage() + "]"); |
3 | 106 } |
107 | |
108 } | |
109 | |
110 public Socket createSSLSocket(String destination, int port) { | |
111 if (sslContext == null) | |
112 throw new IllegalStateException("SSL Context Not Initialized"); | |
113 SSLSocket socket = null; | |
114 try { | |
8 | 115 target = destination + ":" + String.valueOf(port); |
3 | 116 socket = (SSLSocket) sslContext.getSocketFactory().createSocket( |
117 destination, port); | |
118 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
119 Log.e(TAG,"Error creating ssl socket [" + e.getMessage() + "]"); |
3 | 120 } |
121 return socket; | |
122 } | |
123 | |
124 // X509TrustManager Methods | |
125 | |
126 /* | |
127 * (non-Javadoc) | |
8 | 128 * |
3 | 129 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
130 */ | |
131 public X509Certificate[] getAcceptedIssuers() { | |
132 return acceptedIssuers; | |
133 } | |
134 | |
135 /* | |
136 * (non-Javadoc) | |
8 | 137 * |
3 | 138 * @see |
139 * javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert. | |
140 * X509Certificate[], java.lang.String) | |
141 */ | |
142 public void checkClientTrusted(X509Certificate[] arg0, String arg1) | |
143 throws CertificateException { | |
144 throw new SecurityException("checkClientTrusted unsupported"); | |
145 | |
146 } | |
147 | |
148 /* | |
149 * (non-Javadoc) | |
8 | 150 * |
3 | 151 * @see |
152 * javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert. | |
153 * X509Certificate[], java.lang.String) | |
154 */ | |
155 public void checkServerTrusted(X509Certificate[] chain, String type) | |
156 throws CertificateException { | |
157 try { | |
158 for (int i = 0; i < userTrustManagers.length; i++) { | |
159 if (userTrustManagers[i] instanceof X509TrustManager) { | |
160 X509TrustManager trustManager = (X509TrustManager) userTrustManagers[i]; | |
161 X509Certificate[] calist = trustManager | |
162 .getAcceptedIssuers(); | |
163 if (calist.length > 0) { | |
164 trustManager.checkServerTrusted(chain, type); | |
165 } else { | |
166 throw new CertificateException( | |
167 "Empty list of accepted issuers (a.k.a. root CA list)."); | |
168 } | |
169 } | |
170 } | |
171 return; | |
172 } catch (CertificateException ce) { | |
173 X509Certificate cert = chain[0]; | |
38
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_publickey) + cert.getPublicKey().getFormat() + "\r\n"); |
3 | 182 |
8 | 183 bridge.outputLine(manager.res.getString(R.string.host_authenticity_warning, target)); |
184 bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo)); | |
185 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate)); | |
9 | 186 if ((result == null) || (!result.booleanValue())) { |
3 | 187 throw new java.security.cert.CertificateException( |
188 "Certificate Rejected"); | |
189 } | |
190 | |
8 | 191 result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_save_certificate)); |
9 | 192 if ((result != null) && (result.booleanValue())) { |
3 | 193 try { |
194 userks.setCertificateEntry(cert.getSubjectDN().getName(), | |
195 cert); | |
196 userks.store(new FileOutputStream(userKsPath), | |
197 userksPassword); | |
198 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
199 Log.e(TAG,"Error saving certificate [" + e.getMessage() |
3 | 200 + "]"); |
201 e.printStackTrace(); | |
202 } | |
203 } | |
204 } | |
205 } | |
206 } |