1 9 package org.jboss.net.axis.security.handler; 10 11 import java.security.KeyStore ; 12 import java.security.cert.X509Certificate ; 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Vector ; 19 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 import javax.security.auth.x500.X500Principal ; 23 import javax.security.auth.Subject ; 24 import javax.xml.namespace.QName ; 25 import javax.xml.soap.SOAPHeader ; 26 import javax.xml.soap.SOAPHeaderElement ; 27 28 import org.apache.axis.AxisFault; 29 import org.apache.axis.Message; 30 import org.apache.axis.MessageContext; 31 import org.apache.log4j.Logger; 32 import org.apache.ws.axis.security.WSDoAllConstants; 33 import org.apache.ws.axis.security.WSDoAllReceiver; 34 import org.apache.ws.axis.security.WSDoAllReceiverResult; 35 import org.apache.ws.axis.security.util.AxisUtil; 36 import org.apache.ws.security.WSConstants; 37 import org.apache.ws.security.WSSecurityEngineResult; 38 import org.apache.ws.security.components.crypto.Crypto; 39 import org.jboss.net.axis.security.JBossCrypto; 40 import org.jboss.net.axis.security.JBossCryptoFactory; 41 import org.jboss.net.axis.security.SecurityConstants; 42 import org.jboss.security.SecurityAssociation; 43 import org.jboss.security.SecurityDomain; 44 import org.jboss.security.SimplePrincipal; 45 46 57 public class WSSRequestHandler extends WSDoAllReceiver 58 { 59 protected Logger log = Logger.getLogger(this.getClass()); 60 61 64 JBossCrypto sigCrypto = null; 65 66 SecurityDomain domain = null; 67 68 71 74 public boolean canHandleBlock(QName arg0) 75 { 76 return SecurityConstants.SECURITY_HEADER_QNAME.equals(arg0); 77 } 78 79 82 public List getUnderstoodHeaders() 83 { 84 ArrayList understood = new ArrayList (); 85 understood.add(SecurityConstants.SECURITY_HEADER_QNAME); 86 return understood; 87 } 88 89 92 public void invoke(MessageContext mc) throws AxisFault 93 { 94 if (log.isDebugEnabled()) 95 log.debug("Enter: invoke(MessageContext)"); 96 97 String action = (String ) getOption(WSDoAllConstants.ACTION); 98 if (action == null) 99 action = (String ) mc.getProperty(WSDoAllConstants.ACTION); 100 if (action == null) 101 throw new AxisFault("WSSRequestHandler: No action defined"); 102 int doAction = AxisUtil.decodeAction(action, new Vector ()); 103 try 104 { 105 super.invoke(mc); 106 } 107 catch (AxisFault e) 108 { 109 log.fatal("Failed to handle security header", e); 110 throw e; 111 } 112 113 Message sm = mc.getCurrentMessage(); 114 SOAPHeader sHeader = null; 115 try 116 { 117 sHeader = sm.getSOAPEnvelope().getHeader(); 118 } 119 catch (Exception ex) 120 { 121 throw new AxisFault("WSRequestHandler: cannot get SOAP header", ex); 123 } 124 125 129 String actor = (String ) getOption(WSDoAllConstants.ACTOR); 130 Iterator headers = sHeader.examineHeaderElements(actor); 131 SOAPHeaderElement headerElement = null; 132 while (headers.hasNext()) 133 { 134 headerElement = (SOAPHeaderElement ) headers.next(); 135 if (headerElement.getLocalName().equals(WSConstants.WSSE_LN) 136 && headerElement.getNamespaceURI().equals(WSConstants.WSSE_NS)) 137 { 138 break; 139 } 140 } 141 if (headerElement != null) 142 ((org.apache.axis.message.SOAPHeaderElementAxisImpl) headerElement).setProcessed(true); 143 144 if (((doAction & WSConstants.SIGN) == WSConstants.SIGN) || ((doAction & WSConstants.UT) == WSConstants.UT)) 146 { 147 if (!"true".equals(getOption("skipAuthentication"))) 150 authenticate(mc, actor); 151 } 152 153 if (log.isDebugEnabled()) 154 { 155 log.debug("\n\tHeader Element: " + headerElement.getLocalName() + "\n\t\tisProcessed: " 156 + ((org.apache.axis.message.SOAPHeaderElementAxisImpl) headerElement).isProcessed() + "\n\t\tmustUnderstand: " 157 + ((org.apache.axis.message.SOAPHeaderElementAxisImpl) headerElement).getMustUnderstand()); 158 log.debug("Exit: invoke(MessageContext)"); 159 } 160 } 161 162 170 protected void authenticate(MessageContext mc, String actor) throws AxisFault 171 { 172 if (log.isDebugEnabled()) 173 log.debug("Enter: authenticate(MessageContext)"); 174 178 String alias = null; 179 X509Certificate [] certs = null; 180 SimplePrincipal sp = null; 181 Vector results = null; 182 if ((results = (Vector ) mc.getProperty(WSDoAllConstants.RECV_RESULTS)) != null) 183 { 184 WSDoAllReceiverResult actorResult = null; 186 for (Iterator i = results.iterator(); i.hasNext();) 187 { 188 WSDoAllReceiverResult result = (WSDoAllReceiverResult) i.next(); 189 if (result.getActor() == actor) 190 { 191 actorResult = result; 192 if (log.isDebugEnabled()) 193 log.debug("Found results for actor: " + actor); 194 break; 195 } 196 } 197 200 202 X500Principal sigPrincipal = null; 203 X509Certificate credential = null; 204 205 Vector engineResults = actorResult.getResults(); 206 for (Iterator iter = engineResults.iterator(); iter.hasNext();) 207 { 208 WSSecurityEngineResult result = (WSSecurityEngineResult) iter.next(); 209 if (result.getAction() == WSConstants.SIGN) 210 { 211 credential = result.getCertificate(); 214 sigPrincipal = credential.getSubjectX500Principal(); 215 } 216 } 217 218 try 219 { 220 alias = sigCrypto.getAliasForX500Principal(sigPrincipal); 221 sp = new SimplePrincipal(alias); 222 } 223 catch (Exception e) 224 { 225 if (sigPrincipal == null) 227 log.warn("No Principal was found in the message."); 228 else 229 log.warn("Unable to determine alias for the principal: " + sigPrincipal.getName(), e); 230 } 231 232 if (log.isDebugEnabled()) 233 log.debug("attempting to authenticate using " + alias + ":" + credential.getSubjectDN().getName()); 234 Subject subject = new Subject (); 235 if (!domain.isValid(new SimplePrincipal(alias), credential, subject)) 236 { 237 throw new AxisFault("Server.Unauthenticated", org.apache.axis.utils.Messages.getMessage("cantAuth01", sp 238 .getName()), null, null); 239 } 240 else 241 { 242 mc.setProperty(MessageContext.AUTHUSER, subject); 243 Map signers = (Map ) mc.getProperty(SecurityConstants.MC_REQ_SIGNERS); 245 if (signers == null) 246 signers = new HashMap (5); 247 signers.put(actor, alias); 248 mc.setProperty(SecurityConstants.MC_REQ_SIGNERS, signers); 249 } 250 } 251 252 if (log.isDebugEnabled()) 253 log.debug("Exit: authenticate(MessageContext)"); 254 } 255 256 protected Crypto loadSignatureCrypto() throws AxisFault 257 { 258 if (log.isDebugEnabled()) 259 log.debug("Loading the Signature Crypto Class"); 260 if (domain == null) 263 getSecurityDomain(); 264 266 KeyStore truststore = domain.getTrustStore(); 267 if (truststore == null) 268 throw new AxisFault("WSSReceiverHandler: No truststore available."); 269 String cryptoClass; 270 if ((cryptoClass = (String ) getOption(SecurityConstants.HANDLER_CRYPTO_CLASS)) == null) 271 cryptoClass = "org.jboss.net.axis.security.JBoss14Crypto"; 272 sigCrypto = JBossCryptoFactory.getInstance(cryptoClass, truststore); 273 return sigCrypto; 274 } 275 276 protected Crypto loadDecryptionCrypto() throws AxisFault 277 { 278 if (log.isDebugEnabled()) 279 log.debug("Loading the Decryption Crypto Class"); 280 if (domain == null) 281 getSecurityDomain(); 282 KeyStore keystore = domain.getKeyStore(); 284 if (keystore == null) 285 throw new AxisFault("WSSReceiverHandler: No keystore available."); 286 String cryptoClass; 287 if ((cryptoClass = (String ) getOption(SecurityConstants.HANDLER_CRYPTO_CLASS)) == null) 288 cryptoClass = "org.jboss.net.axis.security.JBoss14Crypto"; 289 return JBossCryptoFactory.getInstance(cryptoClass, keystore); 290 } 291 292 private void getSecurityDomain() throws AxisFault 293 { 294 String sd; 296 if ((sd = (String ) getOption(SecurityConstants.HANDLER_SEC_DOMAIN)) == null) 297 sd = "java:/jaas/other"; 298 if (log.isDebugEnabled()) 299 log.debug("WSSReceiveHandler, securityDomain=" + sd); 300 try 301 { 302 Object tempDomain = new InitialContext ().lookup(sd); 303 if (tempDomain != null && tempDomain instanceof SecurityDomain) 304 domain = (SecurityDomain) tempDomain; 305 else 306 { 307 log.fatal("The SecurityManager named " + sd + " is not a SecurityDomain"); 309 throw new AxisFault("WSSReceiverHandler: No security domain is available."); 310 } 311 } 312 catch (NamingException e) 313 { 314 throw new AxisFault("Unable to find the securityDomain named: " + sd, e); 315 } 316 } 317 } 318 | Popular Tags |