Mercurial > 510Connectbot
annotate src/org/tn5250j/framework/transport/SSL/SSLImplementation.java @ 111:6a0ad4d384ea
add debugging for function keys
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 18 Jun 2014 13:00:19 -0700 |
parents | 33eb63352be5 |
children | 77ac18bc1b2f |
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.framework.transport.SSLInterface; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
46 import android.util.Log; |
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
47 |
3 | 48 |
49 /** | |
50 * <p> | |
51 * This class implements the SSLInterface and is used to create SSL socket | |
52 * instances. | |
53 * </p> | |
8 | 54 * |
3 | 55 * @author Stephen M. Kennedy <skennedy@tenthpowertech.com> |
8 | 56 * |
3 | 57 */ |
58 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
|
59 private static final String TAG = "SSLImplementation"; |
3 | 60 SSLContext sslContext = null; |
61 KeyStore userks = null; | |
62 private String userKsPath; | |
63 private char[] userksPassword = "changeit".toCharArray(); | |
64 | |
8 | 65 TerminalBridge bridge = null; |
66 TerminalManager manager = null; | |
67 String target = null; // destination:port | |
68 | |
3 | 69 KeyManagerFactory userkmf = null; |
70 | |
71 TrustManagerFactory usertmf = null; | |
72 | |
73 TrustManager[] userTrustManagers = null; | |
74 | |
75 X509Certificate[] acceptedIssuers; | |
76 | |
8 | 77 public SSLImplementation(TerminalBridge bridge, TerminalManager manager) { |
78 this.bridge = bridge; | |
79 this.manager = manager; | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
13
diff
changeset
|
80 |
3 | 81 } |
82 | |
91
33eb63352be5
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
86
diff
changeset
|
83 public void init(String sslType, String homeDirectory) { |
3 | 84 try { |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
85 Log.d(TAG,"Initializing User KeyStore"); |
91
33eb63352be5
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
86
diff
changeset
|
86 userKsPath = homeDirectory + File.separator + "keystore"; |
3 | 87 File userKsFile = new File(userKsPath); |
88 userks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
89 userks.load(userKsFile.exists() ? new FileInputStream(userKsFile) | |
90 : null, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
91 Log.d(TAG,"Initializing User Key Manager Factory"); |
3 | 92 userkmf = KeyManagerFactory.getInstance(KeyManagerFactory |
93 .getDefaultAlgorithm()); | |
94 userkmf.init(userks, userksPassword); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
95 Log.d(TAG,"Initializing User Trust Manager Factory"); |
3 | 96 usertmf = TrustManagerFactory.getInstance(TrustManagerFactory |
97 .getDefaultAlgorithm()); | |
98 usertmf.init(userks); | |
99 userTrustManagers = usertmf.getTrustManagers(); | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
100 Log.d(TAG,"Initializing SSL Context"); |
3 | 101 sslContext = SSLContext.getInstance(sslType); |
102 sslContext.init(userkmf.getKeyManagers(), new TrustManager[] {this}, null); | |
103 } catch (Exception ex) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
104 Log.e(TAG,"Error initializing SSL [" + ex.getMessage() + "]"); |
3 | 105 } |
106 | |
107 } | |
108 | |
109 public Socket createSSLSocket(String destination, int port) { | |
110 if (sslContext == null) | |
111 throw new IllegalStateException("SSL Context Not Initialized"); | |
112 SSLSocket socket = null; | |
113 try { | |
8 | 114 target = destination + ":" + String.valueOf(port); |
3 | 115 socket = (SSLSocket) sslContext.getSocketFactory().createSocket( |
116 destination, port); | |
117 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
118 Log.e(TAG,"Error creating ssl socket [" + e.getMessage() + "]"); |
3 | 119 } |
120 return socket; | |
121 } | |
122 | |
123 // X509TrustManager Methods | |
124 | |
125 /* | |
126 * (non-Javadoc) | |
8 | 127 * |
3 | 128 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
129 */ | |
130 public X509Certificate[] getAcceptedIssuers() { | |
131 return acceptedIssuers; | |
132 } | |
133 | |
134 /* | |
135 * (non-Javadoc) | |
8 | 136 * |
3 | 137 * @see |
138 * javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert. | |
139 * X509Certificate[], java.lang.String) | |
140 */ | |
141 public void checkClientTrusted(X509Certificate[] arg0, String arg1) | |
142 throws CertificateException { | |
143 throw new SecurityException("checkClientTrusted unsupported"); | |
144 | |
145 } | |
146 | |
147 /* | |
148 * (non-Javadoc) | |
8 | 149 * |
3 | 150 * @see |
151 * javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert. | |
152 * X509Certificate[], java.lang.String) | |
153 */ | |
154 public void checkServerTrusted(X509Certificate[] chain, String type) | |
155 throws CertificateException { | |
156 try { | |
157 for (int i = 0; i < userTrustManagers.length; i++) { | |
158 if (userTrustManagers[i] instanceof X509TrustManager) { | |
159 X509TrustManager trustManager = (X509TrustManager) userTrustManagers[i]; | |
160 X509Certificate[] calist = trustManager | |
161 .getAcceptedIssuers(); | |
162 if (calist.length > 0) { | |
163 trustManager.checkServerTrusted(chain, type); | |
164 } else { | |
165 throw new CertificateException( | |
166 "Empty list of accepted issuers (a.k.a. root CA list)."); | |
167 } | |
168 } | |
169 } | |
170 return; | |
171 } catch (CertificateException ce) { | |
172 X509Certificate cert = chain[0]; | |
38
011462bddcf1
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
26
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 certInfo = certInfo.concat(manager.res.getString(R.string.host_cert_publickey) + cert.getPublicKey().getFormat() + "\r\n"); |
3 | 181 |
8 | 182 bridge.outputLine(manager.res.getString(R.string.host_authenticity_warning, target)); |
183 bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo)); | |
184 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate)); | |
9 | 185 if ((result == null) || (!result.booleanValue())) { |
3 | 186 throw new java.security.cert.CertificateException( |
187 "Certificate Rejected"); | |
188 } | |
189 | |
8 | 190 result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_save_certificate)); |
9 | 191 if ((result != null) && (result.booleanValue())) { |
3 | 192 try { |
193 userks.setCertificateEntry(cert.getSubjectDN().getName(), | |
194 cert); | |
195 userks.store(new FileOutputStream(userKsPath), | |
196 userksPassword); | |
197 } catch (Exception e) { | |
26
9ae1c889a64c
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
198 Log.e(TAG,"Error saving certificate [" + e.getMessage() |
3 | 199 + "]"); |
200 e.printStackTrace(); | |
201 } | |
202 } | |
203 } | |
204 } | |
205 } |