1 23 package com.sun.enterprise.security.ssl; 24 25 import java.net.Socket ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.security.Principal ; 30 import java.security.PrivateKey ; 31 import java.security.cert.X509Certificate ; 32 import javax.net.ssl.X509KeyManager; 33 import javax.security.auth.Subject ; 34 35 import com.sun.enterprise.Switch; 36 import com.sun.enterprise.InvocationManager; 37 import com.sun.enterprise.ComponentInvocation; 38 import com.sun.enterprise.appclient.AppContainer; 39 import com.sun.enterprise.security.ClientSecurityContext; 40 import com.sun.enterprise.security.auth.login.X509CertificateCredential; 41 import com.sun.enterprise.security.auth.LoginContextDriver; 42 import com.sun.enterprise.InvocationException; 43 import java.util.logging.*; 44 import com.sun.logging.*; 45 46 53 public final class J2EEKeyManager implements X509KeyManager { 54 55 private static Logger _logger=null; 56 static { 57 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 58 } 59 60 private X509KeyManager mgr = null; 62 private String alias = null; 63 64 private Map tokenName2MgrMap = null; 65 private boolean supportTokenAlias = false; 66 67 public J2EEKeyManager(X509KeyManager mgr, String alias) { 68 this.mgr = mgr; 69 this.alias = alias; 70 71 if (mgr instanceof UnifiedX509KeyManager) { 72 UnifiedX509KeyManager umgr = (UnifiedX509KeyManager)mgr; 73 X509KeyManager[] mgrs = umgr.getX509KeyManagers(); 74 String [] tokenNames = umgr.getTokenNames(); 75 76 tokenName2MgrMap = new HashMap (); 77 for (int i = 0; i < mgrs.length; i++) { 78 if (tokenNames[i] != null) { 79 tokenName2MgrMap.put(tokenNames[i], mgrs[i]); 80 } 81 } 82 supportTokenAlias = (tokenName2MgrMap.size() > 0); 83 } 84 } 85 86 95 public String chooseClientAlias(String [] keyType, Principal [] issuers, 96 Socket socket) { 97 98 String alias = null; 99 100 if(this.alias == null){ 101 InvocationManager im = Switch.getSwitch().getInvocationManager(); 102 if(im == null) { 103 alias = mgr.chooseClientAlias(keyType, issuers, socket); 105 } else { 106 ComponentInvocation ci = im.getCurrentInvocation(); 107 108 if (ci == null) { throw new InvocationException(); 110 } 111 112 Object containerContext = ci.getContainerContext(); 113 if(containerContext != null && 114 (containerContext instanceof AppContainer)) { 115 116 ClientSecurityContext ctx = ClientSecurityContext.getCurrent(); 117 Subject s = ctx.getSubject(); 118 if(s == null) { 119 LoginContextDriver.doClientLogin(AppContainer.CERTIFICATE, 121 AppContainer.getCallbackHandler()); 122 s = ctx.getSubject(); 123 } 124 Iterator itr = s.getPrivateCredentials().iterator(); 125 while(itr.hasNext()) { 126 Object o = itr.next(); 127 if(o instanceof X509CertificateCredential) { 128 X509CertificateCredential crt = 129 (X509CertificateCredential) o; 130 alias = crt.getAlias(); 131 break; 132 } 133 } 134 } 135 } 136 }else{ 137 alias = this.alias; 138 } 139 if(_logger.isLoggable(Level.FINE)){ 140 _logger.log(Level.FINE, 141 "Choose client Alias :" + alias); 142 } 143 return alias; 144 } 145 146 155 public String chooseServerAlias(String keyType, Principal [] issuers, 156 Socket socket) { 157 158 String alias = null; 159 if(this.alias != null){ 160 alias = this.alias; 161 }else{ 162 alias = mgr.chooseServerAlias(keyType, issuers, socket); 163 } 164 if(_logger.isLoggable(Level.FINE)){ 165 _logger.log(Level.FINE,"Choosing server alias :"+ alias); 166 } 167 return alias; 168 } 169 170 175 public X509Certificate [] getCertificateChain(String alias) { 176 if(_logger.isLoggable(Level.FINE)){ 177 _logger.log(Level.FINE,"Getting certificate chain"); 178 } 179 X509KeyManager keyMgr = getManagerFromToken(alias); 180 if (keyMgr != null) { 181 String aliasName = alias.substring(alias.indexOf(':') + 1); 182 return keyMgr.getCertificateChain(aliasName); 183 } else { 184 return mgr.getCertificateChain(alias); 185 } 186 } 187 188 194 public String [] getClientAliases(String keyType, Principal [] issuers) { 195 if(_logger.isLoggable(Level.FINE)){ 196 _logger.log(Level.FINE,"Getting client aliases"); 197 } 198 return mgr.getClientAliases(keyType, issuers); 199 } 200 201 207 public String [] getServerAliases(String keyType, Principal [] issuers) { 208 if(_logger.isLoggable(Level.FINE)){ 209 _logger.log(Level.FINE,"Getting server aliases"); 210 } 211 return mgr.getServerAliases(keyType, issuers); 212 } 213 214 219 public PrivateKey getPrivateKey(String alias) { 220 if(_logger.isLoggable(Level.FINE)){ 221 _logger.log(Level.FINE,"Getting private key for alias:" + alias); 222 } 223 X509KeyManager keyMgr = getManagerFromToken(alias); 224 if (keyMgr != null) { 225 String aliasName = alias.substring(alias.indexOf(':') + 1); 226 return keyMgr.getPrivateKey(aliasName); 227 } else { 228 return mgr.getPrivateKey(alias); 229 } 230 } 231 232 233 238 private X509KeyManager getManagerFromToken(String tokenAlias) { 239 X509KeyManager keyMgr = null; 240 int ind = -1; 241 if (supportTokenAlias && tokenAlias != null && (ind = tokenAlias.indexOf(':')) != -1) { 242 String tokenName = alias.substring(0, ind); 243 keyMgr = (X509KeyManager)tokenName2MgrMap.get(tokenName); 244 } 245 return keyMgr; 246 } 247 } 248 249 | Popular Tags |