/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.identity.federation.bindings.tomcat.idp;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SecretKey;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Request;
import org.apache.log4j.Logger;
import org.jboss.identity.federation.api.saml.v2.response.SAML2Response;
import org.jboss.identity.federation.bindings.config.EncryptionType;
import org.jboss.identity.federation.bindings.config.KeyProviderType;
import org.jboss.identity.federation.bindings.interfaces.TrustKeyConfigurationException;
import org.jboss.identity.federation.bindings.interfaces.TrustKeyManager;
import org.jboss.identity.federation.bindings.interfaces.TrustKeyProcessingException;
import org.jboss.identity.federation.bindings.util.RedirectBindingSignatureUtil;
import org.jboss.identity.federation.core.exceptions.ConfigurationException;
import org.jboss.identity.federation.core.exceptions.ParsingException;
import org.jboss.identity.federation.core.exceptions.ProcessingException;
import org.jboss.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
import org.jboss.identity.federation.core.saml.v2.util.DocumentUtil;
import org.jboss.identity.federation.core.saml.v2.util.SignatureUtil;
import org.jboss.identity.federation.core.util.XMLEncryptionUtil;
import org.jboss.identity.federation.saml.v2.assertion.EncryptedElementType;
import org.jboss.identity.federation.saml.v2.protocol.ResponseType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/**
* Valve at the Identity Provider that supports
* SAML2 HTTP/Redirect binding with digital signature support
* and xml encryption
* @author Anil.Saldhana@redhat.com
* @since Jan 14, 2009
*/
public class IDPRedirectWithSignatureValve extends IDPRedirectValve
{
private static Logger log = Logger.getLogger(IDPRedirectWithSignatureValve.class);
private boolean ignoreSignature = false;
private TrustKeyManager keyManager;
public IDPRedirectWithSignatureValve()
{
super();
}
/**
* Indicate whether the signature parameter in the request
* needs to be ignored
* @param val
*/
public void setIgnoreSignature(String val)
{
if(val != null && val.length() > 0)
this.ignoreSignature = Boolean.valueOf(val);
}
@Override
public void start() throws LifecycleException
{
super.start();
KeyProviderType keyProvider = this.idpConfiguration.getKeyProvider();
try
{
ClassLoader tcl = SecurityActions.getContextClassLoader();
String keyManagerClassName = keyProvider.getClassName();
if(keyManagerClassName == null)
throw new RuntimeException("KeyManager class name is null");
Class<?> clazz = tcl.loadClass(keyManagerClassName);
this.keyManager = (TrustKeyManager) clazz.newInstance();
keyManager.setAuthProperties(keyProvider.getAuth());
keyManager.setValidatingAlias(keyProvider.getValidatingAlias());
}
catch(Exception e)
{
log.error("Exception reading configuration:",e);
throw new LifecycleException(e.getLocalizedMessage());
}
log.trace("Key Provider=" + keyProvider.getClassName());
}
@Override
protected boolean validate(Request request) throws IOException, GeneralSecurityException
{
boolean result = super.validate(request);
if( result == false)
return result;
if(this.ignoreSignature)
{
log.trace("Since signature is to be ignored, validation returns");
return true;
}
String queryString = request.getQueryString();
//Check if there is a signature
byte[] sigValue = RedirectBindingSignatureUtil.getSignatureValueFromSignedURL(queryString);
if(sigValue == null)
return false;
//Construct the url again
String reqFromURL = RedirectBindingSignatureUtil.getTokenValue(queryString, "SAMLRequest");
String relayStateFromURL = RedirectBindingSignatureUtil.getTokenValue(queryString, "RelayState");
String sigAlgFromURL = RedirectBindingSignatureUtil.getTokenValue(queryString, "SigAlg");
StringBuilder sb = new StringBuilder();
sb.append("SAMLRequest=").append(reqFromURL);
if(relayStateFromURL != null && relayStateFromURL.length() > 0)
{
sb.append("&RelayState=").append(relayStateFromURL);
}
sb.append("&SigAlg=").append(sigAlgFromURL);
PublicKey validatingKey;
try
{
validatingKey = keyManager.getValidatingKey(request.getRemoteAddr());
}
catch (TrustKeyConfigurationException e)
{
throw new GeneralSecurityException(e.getCause());
}
catch (TrustKeyProcessingException e)
{
throw new GeneralSecurityException(e.getCause());
}
boolean isValid = SignatureUtil.validate(sb.toString().getBytes("UTF-8"), sigValue, validatingKey);
return isValid;
}
@Override
protected String getDestination(String urlEncodedResponse, String urlEncodedRelayState)
{
try
{
//Get the signing key
PrivateKey signingKey = keyManager.getSigningKey();
StringBuffer sb = new StringBuffer();
String url = RedirectBindingSignatureUtil.getSAMLResponseURLWithSignature(urlEncodedResponse, urlEncodedRelayState, signingKey);
sb.append("?").append(url);
return sb.toString();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
@Override
protected ResponseType getResponse(Request request, Principal userPrincipal)
throws ParsingException, ConfigurationException, ProcessingException
{
SAML2Response saml2Response = new SAML2Response();
ResponseType responseType = super.getResponse(request, userPrincipal);
//If there is a configuration to encrypt
if(this.idpConfiguration.isEncrypt())
{
//Need to encrypt the assertion
String sp = responseType.getDestination();
if(sp == null)
throw new IllegalStateException("Unable to handle encryption as SP url is null");
try
{
URL spurl = new URL(sp);
PublicKey publicKey = keyManager.getValidatingKey(spurl.getHost());
EncryptionType enc = idpConfiguration.getEncryption();
if(enc == null)
throw new IllegalStateException("EncryptionType not configured");
String encAlgo = enc.getEncAlgo().value();
int keyLength = enc.getKeySize();
//Generate a key on the fly
SecretKey sk = keyManager.getEncryptionKey(spurl.getHost(), encAlgo, keyLength);
StringWriter sw = new StringWriter();
saml2Response.marshall(responseType, sw);
Document responseDoc = DocumentUtil.getDocument(new StringReader(sw.toString()));
String assertionNS = JBossSAMLURIConstants.ASSERTION_NSURI.get();
QName assertionQName = new QName(assertionNS, "EncryptedAssertion", "saml");
Element encAssertion = XMLEncryptionUtil.encryptElementInDocument(responseDoc,
publicKey, sk, keyLength, assertionQName, true);
EncryptedElementType eet = saml2Response.getEncryptedAssertion(DocumentUtil.getNodeAsStream(encAssertion));
responseType.getAssertionOrEncryptedAssertion().set(0, eet);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JAXBException e)
{
throw new ParsingException(e);
}
catch (SAXException e)
{
throw new ParsingException(e);
}
catch (ParserConfigurationException e)
{
throw new ConfigurationException(e);
}
catch (IOException e)
{
throw new ProcessingException(e);
}
catch (TransformerFactoryConfigurationError e)
{
throw new ConfigurationException(e);
}
catch (TransformerException e)
{
throw new ProcessingException(e);
}
catch (Exception e)
{
throw new ProcessingException(e);
}
}
//Lets see how the response looks like
if(log.isTraceEnabled())
{
StringWriter sw = new StringWriter();
try
{
saml2Response.marshall(responseType, sw);
}
catch (JAXBException e)
{
log.trace(e);
}
catch (SAXException e)
{
log.trace(e);
}
log.trace("IDPRedirectValveWithSignature::Response="+sw.toString());
}
return responseType;
}
}