1 23 24 38 39 package com.sun.enterprise.admin.jmx.remote.https; 40 41 import javax.net.ssl.X509TrustManager; 42 import java.security.cert.X509Certificate ; 43 import java.security.cert.CertificateException ; 44 import com.sun.enterprise.admin.jmx.remote.https.AsadminTruststore; 45 import com.sun.enterprise.admin.jmx.remote.IStringManager; 46 import com.sun.enterprise.admin.jmx.remote.StringManagerFactory; 47 48 import java.io.BufferedReader ; 49 import java.io.InputStreamReader ; 50 import java.io.IOException ; 51 52 import java.util.Date ; 53 import java.util.Map ; 54 import java.text.DateFormat ; 55 56 67 public class SunOneBasicX509TrustManager implements X509TrustManager { 68 69 private final Object _alias; 70 private boolean _alreadyInvoked; 71 private CertificateException _lastCertException; 72 private RuntimeException _lastRuntimeException; 73 74 private static IStringManager _strMgr = null; 75 76 82 public SunOneBasicX509TrustManager (Object alias, Map env) { 83 if (_strMgr == null) 84 _strMgr = StringManagerFactory.getClientStringManager(SunOneBasicX509TrustManager.class, env); 85 _alias = alias; 86 _alreadyInvoked = false; 87 _lastCertException = null; 88 _lastRuntimeException = null; 89 } 90 91 96 public SunOneBasicX509TrustManager () { 97 this (null, null); 98 } 99 100 105 public void checkClientTrusted(X509Certificate [] x509Certificate, String authType) 106 throws CertificateException 107 { 108 throw new UnsupportedOperationException ("Not Implemented for Client Trust Management"); 109 } 110 111 117 public void checkServerTrusted(X509Certificate [] chain, String authType) 118 throws CertificateException 119 { 120 if (!_alreadyInvoked) { 126 _alreadyInvoked = true; 127 try { 128 checkCertificate(chain); 129 } catch (RuntimeException ex) { 130 _lastRuntimeException = ex; 131 throw ex; 132 } catch (CertificateException ex) { 133 _lastCertException = ex; 134 throw ex; 135 } 136 } else { 137 if (_lastRuntimeException != null) { 138 throw _lastRuntimeException; 139 } else if (_lastCertException != null) { 140 throw _lastCertException; 141 } 142 } 143 } 144 145 public X509Certificate [] getAcceptedIssuers() 146 { 147 return ( new X509Certificate [0] ); 148 } 149 150 155 protected boolean promptForConfirmation() 156 { 157 return true; 158 } 159 160 167 protected String promptForPassword() throws IOException 168 { 169 if (promptForConfirmation()) { 170 System.out.print(_strMgr.getString("certificateDbPrompt")); 171 BufferedReader r = new BufferedReader (new InputStreamReader (System.in)); 172 return r.readLine(); 173 } else { 174 return null; 175 } 176 } 177 178 185 protected boolean isItOKToAddCertToTrustStore(X509Certificate c) throws IOException 186 { 187 if (promptForConfirmation()) { 188 System.out.println(c.toString()); 189 System.out.print(_strMgr.getString("certificateTrustPrompt")); 190 BufferedReader r = new BufferedReader (new InputStreamReader (System.in)); 191 String result = r.readLine(); 192 if (result != null && result.equalsIgnoreCase("y")) { 193 return true; 194 } else { 195 return false; 196 } 197 } else { 198 return true; 199 } 200 } 201 202 private String getAliasName() 203 { 204 String aliasName = _alias != null ? _alias.toString() : ""; 205 DateFormat f = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); 207 aliasName += ":" + f.format(new Date ()); 208 return aliasName; 209 } 210 211 212 218 protected void checkCertificate(X509Certificate [] chain) throws RuntimeException , 219 CertificateException , IllegalArgumentException 220 { 221 if (chain == null || chain.length == 0) { 222 throw new IllegalArgumentException (_strMgr.getString( 223 "emptyServerCertificate")); 224 } 225 for (int i = 0 ; i < chain.length ; i ++) { 227 chain[i].checkValidity(); 228 } 229 try { 230 AsadminTruststore truststore = null; 231 try { 232 truststore = new AsadminTruststore(); 233 } catch (IOException ex) { 234 String password = promptForPassword(); 237 if (password != null) { 238 truststore = new AsadminTruststore(password); 239 } else { 240 throw ex; 241 } 242 } 243 if (!truststore.certificateExists(chain[0])) { 245 if (isItOKToAddCertToTrustStore(chain[0])) { 249 truststore.addCertificate(getAliasName(), chain[0]); 250 } else { 251 throw new CertificateException (_strMgr.getString( 252 "serverCertificateNotTrusted")); 253 } 254 } 255 } catch (CertificateException ex) { 256 throw ex; 257 } catch (Exception e) { 258 throw new RuntimeException (e); 259 } 260 } 261 } 262 | Popular Tags |