comparison src/org/tn5250j/framework/transport/SSL/X509CertificateTrustManager.java @ 112:77ac18bc1b2f

cleanup java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 18 Jun 2014 13:03:01 -0700
parents 51f34b9de232
children
comparison
equal deleted inserted replaced
111:6a0ad4d384ea 112:77ac18bc1b2f
44 * @deprecated. no longer used. 44 * @deprecated. no longer used.
45 * 45 *
46 */ 46 */
47 public class X509CertificateTrustManager implements X509TrustManager { 47 public class X509CertificateTrustManager implements X509TrustManager {
48 48
49 KeyStore ks = null; 49 KeyStore ks = null;
50 TrustManager[] trustManagers; 50 TrustManager[] trustManagers;
51 TerminalBridge bridge = null; 51 TerminalBridge bridge = null;
52 TerminalManager manager = null; 52 TerminalManager manager = null;
53 53
54 public X509CertificateTrustManager(TrustManager[] managers, KeyStore keyStore, TerminalBridge bridge, TerminalManager manager) { 54 public X509CertificateTrustManager(TrustManager[] managers, KeyStore keyStore, TerminalBridge bridge, TerminalManager manager) {
55 this.bridge = bridge; 55 this.bridge = bridge;
56 this.manager = manager; 56 this.manager = manager;
57 trustManagers = managers; 57 trustManagers = managers;
58 ks = keyStore; 58 ks = keyStore;
59 } 59 }
60 60
61 public void checkClientTrusted(X509Certificate[] chain, String type) throws CertificateException { 61 public void checkClientTrusted(X509Certificate[] chain, String type) throws CertificateException {
62 throw new SecurityException("checkClientTrusted unsupported"); 62 throw new SecurityException("checkClientTrusted unsupported");
63 } 63 }
64 64
65 65
66 /** 66 /**
67 * Checks the server certificate. If it isn't trusted by the trust manager 67 * Checks the server certificate. If it isn't trusted by the trust manager
68 * passed to the constructor, then the user will be prompted to accept the 68 * passed to the constructor, then the user will be prompted to accept the
69 * certificate. 69 * certificate.
70 */ 70 */
71 public void checkServerTrusted(X509Certificate[] chain, String type) 71 public void checkServerTrusted(X509Certificate[] chain, String type)
72 throws CertificateException { 72 throws CertificateException {
73 try { 73 try {
74 for (int i=0; i<trustManagers.length; i++) { 74 for (int i = 0; i < trustManagers.length; i++) {
75 if (trustManagers[i] instanceof X509TrustManager) 75 if (trustManagers[i] instanceof X509TrustManager)
76 ((X509TrustManager)trustManagers[i]).checkServerTrusted(chain,type); 76 ((X509TrustManager)trustManagers[i]).checkServerTrusted(chain, type);
77 } 77 }
78 return;
79 } catch (CertificateException ce) {
80 X509Certificate cert = chain[0];
81 String certInfo = "Version: " + cert.getVersion() + "\n";
82 certInfo = certInfo.concat("Serial Number: " + cert.getSerialNumber()+"\n");
83 certInfo = certInfo.concat("Signature Algorithm: " + cert.getSigAlgName()+"\n");
84 certInfo = certInfo.concat("Issuer: " + cert.getIssuerDN().getName()+"\n");
85 certInfo = certInfo.concat("Valid From: " + cert.getNotBefore()+"\n");
86 certInfo = certInfo.concat("Valid To: " + cert.getNotAfter()+"\n");
87 certInfo = certInfo.concat("Subject DN: " + cert.getSubjectDN().getName()+"\n");
88 certInfo = certInfo.concat("Public Key: " + cert.getPublicKey().getFormat()+"\n");
89 78
90 bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo)); 79 return;
91 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate)); 80 }
92 if ((result == null) || (!result.booleanValue())) { 81 catch (CertificateException ce) {
93 throw new java.security.cert.CertificateException("Certificate Not Accepted"); 82 X509Certificate cert = chain[0];
94 } 83 String certInfo = "Version: " + cert.getVersion() + "\n";
95 } 84 certInfo = certInfo.concat("Serial Number: " + cert.getSerialNumber() + "\n");
96 } 85 certInfo = certInfo.concat("Signature Algorithm: " + cert.getSigAlgName() + "\n");
86 certInfo = certInfo.concat("Issuer: " + cert.getIssuerDN().getName() + "\n");
87 certInfo = certInfo.concat("Valid From: " + cert.getNotBefore() + "\n");
88 certInfo = certInfo.concat("Valid To: " + cert.getNotAfter() + "\n");
89 certInfo = certInfo.concat("Subject DN: " + cert.getSubjectDN().getName() + "\n");
90 certInfo = certInfo.concat("Public Key: " + cert.getPublicKey().getFormat() + "\n");
91 bridge.outputLine(manager.res.getString(R.string.host_certificate, certInfo));
92 Boolean result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_accept_certificate));
97 93
98 public X509Certificate[] getAcceptedIssuers() { 94 if ((result == null) || (!result.booleanValue())) {
99 ArrayList<X509Certificate> list = new ArrayList<X509Certificate>(10); 95 throw new java.security.cert.CertificateException("Certificate Not Accepted");
100 for (int i=0; i<trustManagers.length; i++) { 96 }
101 if (trustManagers[i] instanceof X509TrustManager) 97 }
102 list.addAll(Arrays.asList(((X509TrustManager)trustManagers[i]).getAcceptedIssuers())); 98 }
103 } 99
104 X509Certificate[] acceptedIssuers = new X509Certificate[list.size()]; 100 public X509Certificate[] getAcceptedIssuers() {
105 acceptedIssuers = list.toArray(acceptedIssuers); 101 ArrayList<X509Certificate> list = new ArrayList<X509Certificate>(10);
106 return acceptedIssuers; 102
107 } 103 for (int i = 0; i < trustManagers.length; i++) {
104 if (trustManagers[i] instanceof X509TrustManager)
105 list.addAll(Arrays.asList(((X509TrustManager)trustManagers[i]).getAcceptedIssuers()));
106 }
107
108 X509Certificate[] acceptedIssuers = new X509Certificate[list.size()];
109 acceptedIssuers = list.toArray(acceptedIssuers);
110 return acceptedIssuers;
111 }
108 } 112 }