Search in sources :

Example 66 with WSSecurityException

use of org.apache.wss4j.common.ext.WSSecurityException in project cxf by apache.

the class XmlEncOutInterceptor method encryptSymmetricKey.

// Apache Security XMLCipher does not support
// Certificates for encrypting the keys
protected byte[] encryptSymmetricKey(byte[] keyBytes, X509Certificate remoteCert, String keyEncAlgo, String digestAlgo) throws WSSecurityException {
    Cipher cipher = EncryptionUtils.initCipherWithCert(keyEncAlgo, digestAlgo, Cipher.ENCRYPT_MODE, remoteCert);
    int blockSize = cipher.getBlockSize();
    if (blockSize > 0 && blockSize < keyBytes.length) {
        String message = "Public key algorithm too weak to encrypt symmetric key";
        LOG.severe(message);
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "unsupportedKeyTransp", new Object[] { message });
    }
    final byte[] encryptedEphemeralKey;
    try {
        encryptedEphemeralKey = cipher.doFinal(keyBytes);
    } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException ex) {
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_ENCRYPTION, ex);
    }
    return encryptedEphemeralKey;
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException)

Example 67 with WSSecurityException

use of org.apache.wss4j.common.ext.WSSecurityException in project cxf by apache.

the class XmlEncOutInterceptor method createKeyInfoElement.

private Element createKeyInfoElement(Document encryptedDataDoc, X509Certificate remoteCert) throws Exception {
    Element keyInfoElement = encryptedDataDoc.createElementNS(SIG_NS, SIG_PREFIX + ":KeyInfo");
    String keyIdType = encProps.getEncryptionKeyIdType() == null ? RSSecurityUtils.X509_CERT : encProps.getEncryptionKeyIdType();
    final Node keyIdentifierNode;
    if (keyIdType.equals(RSSecurityUtils.X509_CERT)) {
        final byte[] data;
        try {
            data = remoteCert.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new WSSecurityException(WSSecurityException.ErrorCode.SECURITY_TOKEN_UNAVAILABLE, e, "encodeError");
        }
        Text text = encryptedDataDoc.createTextNode(org.apache.xml.security.utils.XMLUtils.encodeToString(data));
        Element cert = encryptedDataDoc.createElementNS(SIG_NS, SIG_PREFIX + ":X509Certificate");
        cert.appendChild(text);
        Element x509Data = encryptedDataDoc.createElementNS(SIG_NS, SIG_PREFIX + ":X509Data");
        x509Data.appendChild(cert);
        keyIdentifierNode = x509Data;
    } else if (keyIdType.equals(RSSecurityUtils.X509_ISSUER_SERIAL)) {
        String issuer = remoteCert.getIssuerDN().getName();
        java.math.BigInteger serialNumber = remoteCert.getSerialNumber();
        DOMX509IssuerSerial domIssuerSerial = new DOMX509IssuerSerial(encryptedDataDoc, issuer, serialNumber);
        DOMX509Data domX509Data = new DOMX509Data(encryptedDataDoc, domIssuerSerial);
        keyIdentifierNode = domX509Data.getElement();
    } else {
        throw new Exception("Unsupported key identifier:" + keyIdType);
    }
    keyInfoElement.appendChild(keyIdentifierNode);
    return keyInfoElement;
}
Also used : Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) CertificateEncodingException(java.security.cert.CertificateEncodingException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Text(org.w3c.dom.Text) DOMX509IssuerSerial(org.apache.wss4j.common.token.DOMX509IssuerSerial) DOMX509Data(org.apache.wss4j.common.token.DOMX509Data) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 68 with WSSecurityException

use of org.apache.wss4j.common.ext.WSSecurityException in project cxf by apache.

the class XmlSecInInterceptor method checkSignatureTrust.

private void checkSignatureTrust(Crypto sigCrypto, Message msg, TokenSecurityEvent<?> event) throws XMLSecurityException {
    SecurityToken token = event.getSecurityToken();
    if (token != null) {
        X509Certificate[] certs = token.getX509Certificates();
        if (certs == null && token.getPublicKey() == null && token instanceof KeyNameSecurityToken) {
            certs = getX509CertificatesForKeyName(sigCrypto, msg, (KeyNameSecurityToken) token);
        }
        PublicKey publicKey = token.getPublicKey();
        X509Certificate cert = null;
        if (certs != null && certs.length > 0) {
            cert = certs[0];
        }
        // validate trust
        try {
            new TrustValidator().validateTrust(sigCrypto, cert, publicKey, getSubjectContraints(msg));
        } catch (WSSecurityException e) {
            String error = "Signature validation failed";
            throw new XMLSecurityException("empty", new Object[] { error });
        }
        if (persistSignature) {
            msg.setContent(X509Certificate.class, cert);
        }
    }
}
Also used : SecurityToken(org.apache.xml.security.stax.securityToken.SecurityToken) KeyNameSecurityToken(org.apache.xml.security.stax.impl.securityToken.KeyNameSecurityToken) TrustValidator(org.apache.cxf.rs.security.common.TrustValidator) PublicKey(java.security.PublicKey) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) KeyNameSecurityToken(org.apache.xml.security.stax.impl.securityToken.KeyNameSecurityToken) X509Certificate(java.security.cert.X509Certificate) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 69 with WSSecurityException

use of org.apache.wss4j.common.ext.WSSecurityException in project cxf by apache.

the class EncryptionUtils method initXMLCipher.

public static XMLCipher initXMLCipher(String symEncAlgo, int mode, Key key) throws WSSecurityException {
    try {
        XMLCipher cipher = XMLCipher.getInstance(symEncAlgo);
        cipher.setSecureValidation(true);
        cipher.init(mode, key);
        return cipher;
    } catch (XMLEncryptionException ex) {
        throw new WSSecurityException(WSSecurityException.ErrorCode.UNSUPPORTED_ALGORITHM, ex);
    }
}
Also used : XMLCipher(org.apache.xml.security.encryption.XMLCipher) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 70 with WSSecurityException

use of org.apache.wss4j.common.ext.WSSecurityException in project cxf by apache.

the class AbstractSamlInHandler method createKeyInfoFromDefaultAlias.

protected SAMLKeyInfo createKeyInfoFromDefaultAlias(Crypto sigCrypto) throws WSSecurityException {
    try {
        X509Certificate[] certs = RSSecurityUtils.getCertificates(sigCrypto, sigCrypto.getDefaultX509Identifier());
        SAMLKeyInfo samlKeyInfo = new SAMLKeyInfo(new X509Certificate[] { certs[0] });
        samlKeyInfo.setPublicKey(certs[0].getPublicKey());
        return samlKeyInfo;
    } catch (Exception ex) {
        LOG.log(Level.FINE, "Error in loading the certificates: " + ex.getMessage(), ex);
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_SIGNATURE, ex);
    }
}
Also used : SAMLKeyInfo(org.apache.wss4j.common.saml.SAMLKeyInfo) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) X509Certificate(java.security.cert.X509Certificate) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) IOException(java.io.IOException)

Aggregations

WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)241 Element (org.w3c.dom.Element)72 Document (org.w3c.dom.Document)53 IOException (java.io.IOException)51 Crypto (org.apache.wss4j.common.crypto.Crypto)50 SamlAssertionWrapper (org.apache.wss4j.common.saml.SamlAssertionWrapper)39 Credential (org.apache.wss4j.dom.validate.Credential)37 RequestData (org.apache.wss4j.dom.handler.RequestData)36 X509Certificate (java.security.cert.X509Certificate)31 Response (org.opensaml.saml.saml2.core.Response)31 SAMLCallback (org.apache.wss4j.common.saml.SAMLCallback)25 DateTime (org.joda.time.DateTime)22 XMLObject (org.opensaml.core.xml.XMLObject)22 XMLStreamException (javax.xml.stream.XMLStreamException)21 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)21 Fault (org.apache.cxf.interceptor.Fault)20 SOAPException (javax.xml.soap.SOAPException)19 CallbackHandler (javax.security.auth.callback.CallbackHandler)18 ReceivedToken (org.apache.cxf.sts.request.ReceivedToken)17 InputStream (java.io.InputStream)16