Search in sources :

Example 1 with AbstractTokenWrapper

use of org.apache.wss4j.policy.model.AbstractTokenWrapper in project cxf by apache.

the class AsymmetricBindingHandler method getEncryptBeforeSignWrapper.

private AbstractTokenWrapper getEncryptBeforeSignWrapper() {
    AbstractTokenWrapper wrapper;
    if (isRequestor()) {
        wrapper = abinding.getRecipientEncryptionToken();
        if (wrapper == null) {
            wrapper = abinding.getRecipientToken();
        }
    } else {
        wrapper = abinding.getInitiatorEncryptionToken();
        if (wrapper == null) {
            wrapper = abinding.getInitiatorToken();
        }
    }
    assertTokenWrapper(wrapper);
    return wrapper;
}
Also used : AbstractTokenWrapper(org.apache.wss4j.policy.model.AbstractTokenWrapper)

Example 2 with AbstractTokenWrapper

use of org.apache.wss4j.policy.model.AbstractTokenWrapper in project cxf by apache.

the class AsymmetricBindingHandler method doSignBeforeEncrypt.

private void doSignBeforeEncrypt() {
    try {
        AbstractTokenWrapper initiatorWrapper = abinding.getInitiatorSignatureToken();
        if (initiatorWrapper == null) {
            initiatorWrapper = abinding.getInitiatorToken();
        }
        assertTokenWrapper(initiatorWrapper);
        boolean attached = false;
        if (initiatorWrapper != null) {
            AbstractToken initiatorToken = initiatorWrapper.getToken();
            if (initiatorToken instanceof IssuedToken) {
                SecurityToken secToken = getSecurityToken();
                if (secToken == null) {
                    unassertPolicy(initiatorToken, "Security token is not found or expired");
                    return;
                } else if (isTokenRequired(initiatorToken.getIncludeTokenType())) {
                    Element el = secToken.getToken();
                    this.addEncryptedKeyElement(cloneElement(el));
                    attached = true;
                }
            } else if (initiatorToken instanceof SamlToken && isRequestor()) {
                SamlAssertionWrapper assertionWrapper = addSamlToken((SamlToken) initiatorToken);
                if (assertionWrapper != null && isTokenRequired(initiatorToken.getIncludeTokenType())) {
                    Element envelope = saaj.getSOAPPart().getEnvelope();
                    envelope = (Element) DOMUtils.getDomElement(envelope);
                    addSupportingElement(assertionWrapper.toDOM(envelope.getOwnerDocument()));
                    storeAssertionAsSecurityToken(assertionWrapper);
                }
            } else if (initiatorToken instanceof SamlToken) {
                String tokenId = getSAMLToken();
                if (tokenId == null) {
                    unassertPolicy(initiatorToken, "Security token is not found or expired");
                    return;
                }
            }
            assertToken(initiatorToken);
        }
        // Add timestamp
        List<WSEncryptionPart> sigs = new ArrayList<>();
        if (timestampEl != null) {
            WSEncryptionPart timestampPart = convertToEncryptionPart(timestampEl.getElement());
            sigs.add(timestampPart);
        }
        addSupportingTokens(sigs);
        sigs.addAll(this.getSignedParts(null));
        if (isRequestor() && initiatorWrapper != null) {
            doSignature(initiatorWrapper, sigs, attached);
            doEndorse();
        } else if (!isRequestor()) {
            // confirm sig
            addSignatureConfirmation(sigs);
            AbstractTokenWrapper recipientSignatureToken = abinding.getRecipientSignatureToken();
            if (recipientSignatureToken == null) {
                recipientSignatureToken = abinding.getRecipientToken();
            }
            if (recipientSignatureToken != null) {
                assertTokenWrapper(recipientSignatureToken);
                assertToken(recipientSignatureToken.getToken());
                doSignature(recipientSignatureToken, sigs, attached);
            }
        }
        List<WSEncryptionPart> enc = getEncryptedParts();
        // Check for signature protection
        if (abinding.isEncryptSignature()) {
            if (mainSigId != null) {
                WSEncryptionPart sigPart = new WSEncryptionPart(mainSigId, "Element");
                sigPart.setElement(bottomUpElement);
                enc.add(sigPart);
            }
            if (sigConfList != null && !sigConfList.isEmpty()) {
                enc.addAll(sigConfList);
            }
            assertPolicy(new QName(abinding.getName().getNamespaceURI(), SPConstants.ENCRYPT_SIGNATURE));
        }
        // Do encryption
        AbstractTokenWrapper encToken;
        if (isRequestor()) {
            enc.addAll(encryptedTokensList);
            encToken = abinding.getRecipientEncryptionToken();
            if (encToken == null) {
                encToken = abinding.getRecipientToken();
            }
        } else {
            encToken = abinding.getInitiatorEncryptionToken();
            if (encToken == null) {
                encToken = abinding.getInitiatorToken();
            }
        }
        doEncryption(encToken, enc, false);
        if (encToken != null) {
            assertTokenWrapper(encToken);
            assertToken(encToken.getToken());
        }
    } catch (Exception e) {
        String reason = e.getMessage();
        LOG.log(Level.WARNING, "Sign before encryption failed due to : " + reason);
        LOG.log(Level.FINE, e.getMessage(), e);
        throw new Fault(e);
    }
}
Also used : WSEncryptionPart(org.apache.wss4j.common.WSEncryptionPart) SamlToken(org.apache.wss4j.policy.model.SamlToken) QName(javax.xml.namespace.QName) IssuedToken(org.apache.wss4j.policy.model.IssuedToken) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) Fault(org.apache.cxf.interceptor.Fault) SOAPException(javax.xml.soap.SOAPException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) AbstractToken(org.apache.wss4j.policy.model.AbstractToken) AbstractTokenWrapper(org.apache.wss4j.policy.model.AbstractTokenWrapper)

Example 3 with AbstractTokenWrapper

use of org.apache.wss4j.policy.model.AbstractTokenWrapper in project cxf by apache.

the class StaxAsymmetricBindingHandler method doEncryptBeforeSign.

private void doEncryptBeforeSign() {
    try {
        AbstractTokenWrapper wrapper;
        AbstractToken encryptionToken = null;
        if (isRequestor()) {
            wrapper = abinding.getRecipientEncryptionToken();
            if (wrapper == null) {
                wrapper = abinding.getRecipientToken();
            }
        } else {
            wrapper = abinding.getInitiatorEncryptionToken();
            if (wrapper == null) {
                wrapper = abinding.getInitiatorToken();
            }
        }
        assertTokenWrapper(wrapper);
        if (wrapper != null) {
            encryptionToken = wrapper.getToken();
            assertToken(encryptionToken);
        }
        AbstractTokenWrapper initiatorWrapper = abinding.getInitiatorSignatureToken();
        if (initiatorWrapper == null) {
            initiatorWrapper = abinding.getInitiatorToken();
        }
        if (initiatorWrapper != null) {
            assertTokenWrapper(initiatorWrapper);
            AbstractToken initiatorToken = initiatorWrapper.getToken();
            if (initiatorToken instanceof IssuedToken) {
                SecurityToken sigTok = getSecurityToken();
                addIssuedToken(initiatorToken, sigTok, false, true);
                if (sigTok != null) {
                    storeSecurityToken(initiatorToken, sigTok);
                    outboundSecurityContext.remove(XMLSecurityConstants.PROP_USE_THIS_TOKEN_ID_FOR_ENCRYPTION);
                }
                // Set up CallbackHandler which wraps the configured Handler
                WSSSecurityProperties properties = getProperties();
                TokenStoreCallbackHandler callbackHandler = new TokenStoreCallbackHandler(properties.getCallbackHandler(), TokenStoreUtils.getTokenStore(message));
                properties.setCallbackHandler(callbackHandler);
            } else if (initiatorToken instanceof SamlToken) {
                addSamlToken((SamlToken) initiatorToken, false, true);
            }
        }
        List<SecurePart> encrParts = null;
        List<SecurePart> sigParts = null;
        try {
            encrParts = getEncryptedParts();
            // Signed parts are determined before encryption because encrypted signed headers
            // will not be included otherwise
            sigParts = getSignedParts();
        } catch (SOAPException ex) {
            throw new Fault(ex);
        }
        addSupportingTokens();
        if (encryptionToken != null && !encrParts.isEmpty()) {
            if (isRequestor()) {
                encrParts.addAll(encryptedTokensList);
            } else {
                addSignatureConfirmation(sigParts);
            }
            // Check for signature protection
            if (abinding.isEncryptSignature()) {
                SecurePart part = new SecurePart(new QName(XMLSecurityConstants.NS_DSIG, "Signature"), Modifier.Element);
                encrParts.add(part);
                if (signatureConfirmationAdded) {
                    SecurePart securePart = new SecurePart(WSSConstants.TAG_WSSE11_SIG_CONF, Modifier.Element);
                    encrParts.add(securePart);
                }
                assertPolicy(new QName(abinding.getName().getNamespaceURI(), SPConstants.ENCRYPT_SIGNATURE));
            }
            doEncryption(wrapper, encrParts, true);
        }
        if (timestampAdded) {
            SecurePart part = new SecurePart(new QName(WSSConstants.NS_WSU10, "Timestamp"), Modifier.Element);
            sigParts.add(part);
        }
        if (!sigParts.isEmpty()) {
            if (initiatorWrapper != null && isRequestor()) {
                doSignature(initiatorWrapper, sigParts);
            } else if (!isRequestor()) {
                AbstractTokenWrapper recipientSignatureToken = abinding.getRecipientSignatureToken();
                if (recipientSignatureToken == null) {
                    recipientSignatureToken = abinding.getRecipientToken();
                }
                if (recipientSignatureToken != null) {
                    assertTokenWrapper(recipientSignatureToken);
                    assertToken(recipientSignatureToken.getToken());
                    doSignature(recipientSignatureToken, sigParts);
                }
            }
        }
        removeSignatureIfSignedSAML();
        enforceEncryptBeforeSigningWithSignedSAML();
        prependSignatureToSC();
        putCustomTokenAfterSignature();
    } catch (Exception e) {
        String reason = e.getMessage();
        LOG.log(Level.WARNING, "Encrypt before signing failed due to : " + reason);
        throw new Fault(e);
    }
}
Also used : WSSSecurityProperties(org.apache.wss4j.stax.ext.WSSSecurityProperties) SamlToken(org.apache.wss4j.policy.model.SamlToken) QName(javax.xml.namespace.QName) IssuedToken(org.apache.wss4j.policy.model.IssuedToken) Fault(org.apache.cxf.interceptor.Fault) SOAPException(javax.xml.soap.SOAPException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) SecurePart(org.apache.xml.security.stax.ext.SecurePart) AbstractToken(org.apache.wss4j.policy.model.AbstractToken) SOAPException(javax.xml.soap.SOAPException) TokenStoreCallbackHandler(org.apache.cxf.ws.security.wss4j.TokenStoreCallbackHandler) AbstractTokenWrapper(org.apache.wss4j.policy.model.AbstractTokenWrapper)

Example 4 with AbstractTokenWrapper

use of org.apache.wss4j.policy.model.AbstractTokenWrapper in project cxf by apache.

the class SymmetricBindingHandler method doSignBeforeEncrypt.

private void doSignBeforeEncrypt() {
    AbstractTokenWrapper sigAbstractTokenWrapper = getSignatureToken();
    assertTokenWrapper(sigAbstractTokenWrapper);
    AbstractToken sigToken = sigAbstractTokenWrapper.getToken();
    String sigTokId = null;
    Element sigTokElem = null;
    try {
        SecurityToken sigTok = null;
        if (sigToken != null) {
            if (sigToken instanceof SecureConversationToken || sigToken instanceof SecurityContextToken || sigToken instanceof IssuedToken || sigToken instanceof KerberosToken || sigToken instanceof SpnegoContextToken) {
                sigTok = getSecurityToken();
            } else if (sigToken instanceof X509Token) {
                if (isRequestor()) {
                    sigTokId = setupEncryptedKey(sigAbstractTokenWrapper, sigToken);
                } else {
                    sigTok = getEncryptedKey();
                }
            } else if (sigToken instanceof UsernameToken) {
                if (isRequestor()) {
                    sigTokId = setupUTDerivedKey((UsernameToken) sigToken);
                } else {
                    sigTok = getUTDerivedKey();
                }
            }
        } else {
            unassertPolicy(sbinding, "No signature token");
            return;
        }
        if (sigTok == null && StringUtils.isEmpty(sigTokId)) {
            unassertPolicy(sigAbstractTokenWrapper, "No signature token id");
            return;
        }
        assertPolicy(sigAbstractTokenWrapper);
        if (sigTok == null) {
            sigTok = tokenStore.getToken(sigTokId);
        }
        // if (sigTok == null) {
        // REVISIT - no token?
        // }
        boolean tokIncluded = true;
        if (isTokenRequired(sigToken.getIncludeTokenType())) {
            Element el = sigTok.getToken();
            sigTokElem = cloneElement(el);
            this.addEncryptedKeyElement(sigTokElem);
        } else if (isRequestor() && sigToken instanceof X509Token) {
            Element el = sigTok.getToken();
            sigTokElem = cloneElement(el);
            this.addEncryptedKeyElement(sigTokElem);
        } else {
            tokIncluded = false;
        }
        // Add timestamp
        List<WSEncryptionPart> sigs = new ArrayList<>();
        if (timestampEl != null) {
            WSEncryptionPart timestampPart = convertToEncryptionPart(timestampEl.getElement());
            sigs.add(timestampPart);
        }
        addSupportingTokens(sigs);
        sigs.addAll(getSignedParts(null));
        if (isRequestor()) {
            if (!sigs.isEmpty()) {
                addSig(doSignature(sigs, sigAbstractTokenWrapper, sigToken, sigTok, tokIncluded));
            }
            doEndorse();
        } else {
            // confirm sig
            addSignatureConfirmation(sigs);
            if (!sigs.isEmpty()) {
                doSignature(sigs, sigAbstractTokenWrapper, sigToken, sigTok, tokIncluded);
            }
        }
        // Encryption
        AbstractTokenWrapper encrAbstractTokenWrapper = getEncryptionToken();
        AbstractToken encrToken = encrAbstractTokenWrapper.getToken();
        SecurityToken encrTok = null;
        if (sigToken.equals(encrToken)) {
            // Use the same token
            encrTok = sigTok;
        } else {
            unassertPolicy(sbinding, "Encryption token does not equal signature token");
            return;
        }
        List<WSEncryptionPart> enc = getEncryptedParts();
        // Check for signature protection
        if (sbinding.isEncryptSignature()) {
            if (mainSigId != null) {
                WSEncryptionPart sigPart = new WSEncryptionPart(mainSigId, "Element");
                sigPart.setElement(bottomUpElement);
                enc.add(sigPart);
            }
            if (sigConfList != null && !sigConfList.isEmpty()) {
                enc.addAll(sigConfList);
            }
            assertPolicy(new QName(sbinding.getName().getNamespaceURI(), SPConstants.ENCRYPT_SIGNATURE));
        }
        if (isRequestor()) {
            enc.addAll(encryptedTokensList);
        }
        doEncryption(encrAbstractTokenWrapper, encrTok, tokIncluded, enc, false);
    } catch (Exception e) {
        LOG.log(Level.FINE, e.getMessage(), e);
        throw new Fault(e);
    }
}
Also used : WSEncryptionPart(org.apache.wss4j.common.WSEncryptionPart) KerberosToken(org.apache.wss4j.policy.model.KerberosToken) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) IssuedToken(org.apache.wss4j.policy.model.IssuedToken) UsernameToken(org.apache.wss4j.policy.model.UsernameToken) WSSecUsernameToken(org.apache.wss4j.dom.message.WSSecUsernameToken) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SecureConversationToken(org.apache.wss4j.policy.model.SecureConversationToken) SpnegoContextToken(org.apache.wss4j.policy.model.SpnegoContextToken) SOAPException(javax.xml.soap.SOAPException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) AbstractToken(org.apache.wss4j.policy.model.AbstractToken) X509Token(org.apache.wss4j.policy.model.X509Token) SecurityContextToken(org.apache.wss4j.policy.model.SecurityContextToken) AbstractTokenWrapper(org.apache.wss4j.policy.model.AbstractTokenWrapper)

Example 5 with AbstractTokenWrapper

use of org.apache.wss4j.policy.model.AbstractTokenWrapper in project cxf by apache.

the class SymmetricBindingHandler method doEncryptBeforeSign.

private void doEncryptBeforeSign() {
    try {
        AbstractTokenWrapper encryptionWrapper = getEncryptionToken();
        assertTokenWrapper(encryptionWrapper);
        AbstractToken encryptionToken = encryptionWrapper.getToken();
        if (encryptionToken != null) {
            // The encryption token can be an IssuedToken or a
            // SecureConversationToken
            String tokenId = null;
            SecurityToken tok = null;
            if (encryptionToken instanceof IssuedToken || encryptionToken instanceof KerberosToken || encryptionToken instanceof SecureConversationToken || encryptionToken instanceof SecurityContextToken || encryptionToken instanceof SpnegoContextToken) {
                tok = getSecurityToken();
            } else if (encryptionToken instanceof X509Token) {
                if (isRequestor()) {
                    tokenId = setupEncryptedKey(encryptionWrapper, encryptionToken);
                } else {
                    tok = getEncryptedKey();
                }
            } else if (encryptionToken instanceof UsernameToken) {
                if (isRequestor()) {
                    tokenId = setupUTDerivedKey((UsernameToken) encryptionToken);
                } else {
                    tok = getUTDerivedKey();
                }
            }
            if (tok == null) {
                // }
                if (tokenId != null && tokenId.startsWith("#")) {
                    tokenId = tokenId.substring(1);
                }
                /*
                     * Get hold of the token from the token storage
                     */
                tok = tokenStore.getToken(tokenId);
            }
            boolean attached = false;
            if (isTokenRequired(encryptionToken.getIncludeTokenType())) {
                Element el = tok.getToken();
                this.addEncryptedKeyElement(cloneElement(el));
                attached = true;
            } else if (encryptionToken instanceof X509Token && isRequestor()) {
                Element el = tok.getToken();
                this.addEncryptedKeyElement(cloneElement(el));
                attached = true;
            }
            List<WSEncryptionPart> sigParts = new ArrayList<>();
            if (timestampEl != null) {
                WSEncryptionPart timestampPart = convertToEncryptionPart(timestampEl.getElement());
                sigParts.add(timestampPart);
            }
            addSupportingTokens(sigParts);
            sigParts.addAll(this.getSignedParts(null));
            List<WSEncryptionPart> encrParts = getEncryptedParts();
            WSSecBase encr = doEncryption(encryptionWrapper, tok, attached, encrParts, true);
            handleEncryptedSignedHeaders(encrParts, sigParts);
            if (!isRequestor()) {
                addSignatureConfirmation(sigParts);
            }
            // We should use the same key in the case of EncryptBeforeSig
            if (!sigParts.isEmpty()) {
                addSig(this.doSignature(sigParts, encryptionWrapper, encryptionToken, tok, attached));
            }
            if (isRequestor()) {
                this.doEndorse();
            }
            // Check for signature protection and encryption of UsernameToken
            if (sbinding.isEncryptSignature() || !encryptedTokensList.isEmpty() && isRequestor()) {
                List<WSEncryptionPart> secondEncrParts = new ArrayList<>();
                // Now encrypt the signature using the above token
                if (sbinding.isEncryptSignature()) {
                    if (this.mainSigId != null) {
                        WSEncryptionPart sigPart = new WSEncryptionPart(this.mainSigId, "Element");
                        sigPart.setElement(bottomUpElement);
                        secondEncrParts.add(sigPart);
                    }
                    if (sigConfList != null && !sigConfList.isEmpty()) {
                        secondEncrParts.addAll(sigConfList);
                    }
                    assertPolicy(new QName(sbinding.getName().getNamespaceURI(), SPConstants.ENCRYPT_SIGNATURE));
                }
                if (isRequestor()) {
                    secondEncrParts.addAll(encryptedTokensList);
                }
                Element secondRefList = null;
                if (encryptionToken.getDerivedKeys() == DerivedKeys.RequireDerivedKeys && !secondEncrParts.isEmpty()) {
                    secondRefList = ((WSSecDKEncrypt) encr).encryptForExternalRef(null, secondEncrParts);
                } else if (!secondEncrParts.isEmpty()) {
                    // Encrypt, get hold of the ref list and add it
                    secondRefList = ((WSSecEncrypt) encr).encryptForRef(null, secondEncrParts);
                }
                if (secondRefList != null) {
                    this.addDerivedKeyElement(secondRefList);
                }
            }
        }
    } catch (RuntimeException ex) {
        LOG.log(Level.FINE, ex.getMessage(), ex);
        throw ex;
    } catch (Exception ex) {
        LOG.log(Level.FINE, ex.getMessage(), ex);
        throw new Fault(ex);
    }
}
Also used : WSEncryptionPart(org.apache.wss4j.common.WSEncryptionPart) WSSecEncrypt(org.apache.wss4j.dom.message.WSSecEncrypt) KerberosToken(org.apache.wss4j.policy.model.KerberosToken) QName(javax.xml.namespace.QName) IssuedToken(org.apache.wss4j.policy.model.IssuedToken) Element(org.w3c.dom.Element) UsernameToken(org.apache.wss4j.policy.model.UsernameToken) WSSecUsernameToken(org.apache.wss4j.dom.message.WSSecUsernameToken) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SecureConversationToken(org.apache.wss4j.policy.model.SecureConversationToken) SpnegoContextToken(org.apache.wss4j.policy.model.SpnegoContextToken) SOAPException(javax.xml.soap.SOAPException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) WSSecBase(org.apache.wss4j.dom.message.WSSecBase) AbstractToken(org.apache.wss4j.policy.model.AbstractToken) X509Token(org.apache.wss4j.policy.model.X509Token) SecurityContextToken(org.apache.wss4j.policy.model.SecurityContextToken) AbstractTokenWrapper(org.apache.wss4j.policy.model.AbstractTokenWrapper)

Aggregations

AbstractTokenWrapper (org.apache.wss4j.policy.model.AbstractTokenWrapper)9 SOAPException (javax.xml.soap.SOAPException)8 Fault (org.apache.cxf.interceptor.Fault)8 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)8 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)8 AbstractToken (org.apache.wss4j.policy.model.AbstractToken)8 IssuedToken (org.apache.wss4j.policy.model.IssuedToken)8 QName (javax.xml.namespace.QName)7 ArrayList (java.util.ArrayList)6 WSEncryptionPart (org.apache.wss4j.common.WSEncryptionPart)4 KerberosToken (org.apache.wss4j.policy.model.KerberosToken)4 SamlToken (org.apache.wss4j.policy.model.SamlToken)4 SecureConversationToken (org.apache.wss4j.policy.model.SecureConversationToken)4 SecurityContextToken (org.apache.wss4j.policy.model.SecurityContextToken)4 SpnegoContextToken (org.apache.wss4j.policy.model.SpnegoContextToken)4 UsernameToken (org.apache.wss4j.policy.model.UsernameToken)4 X509Token (org.apache.wss4j.policy.model.X509Token)4 WSSSecurityProperties (org.apache.wss4j.stax.ext.WSSSecurityProperties)4 SecurePart (org.apache.xml.security.stax.ext.SecurePart)4 Element (org.w3c.dom.Element)4