Search in sources :

Example 1 with EncryptionPartDef

use of org.apache.xml.security.stax.impl.EncryptionPartDef in project santuario-java by apache.

the class AbstractEncryptOutputProcessor method verifyEncryptionParts.

protected void verifyEncryptionParts(OutputProcessorChain outputProcessorChain) throws XMLSecurityException {
    List<EncryptionPartDef> encryptionPartDefs = outputProcessorChain.getSecurityContext().getAsList(EncryptionPartDef.class);
    Map<Object, SecurePart> dynamicSecureParts = outputProcessorChain.getSecurityContext().getAsMap(XMLSecurityConstants.ENCRYPTION_PARTS);
    Iterator<Map.Entry<Object, SecurePart>> securePartsMapIterator = dynamicSecureParts.entrySet().iterator();
    loop: while (securePartsMapIterator.hasNext()) {
        Map.Entry<Object, SecurePart> securePartEntry = securePartsMapIterator.next();
        final SecurePart securePart = securePartEntry.getValue();
        if (securePart.isRequired()) {
            for (int i = 0; encryptionPartDefs != null && i < encryptionPartDefs.size(); i++) {
                EncryptionPartDef encryptionPartDef = encryptionPartDefs.get(i);
                if (encryptionPartDef.getSecurePart() == securePart) {
                    continue loop;
                }
            }
            throw new XMLSecurityException("stax.encryption.securePartNotFound", new Object[] { securePart.getName() });
        }
    }
}
Also used : SecurePart(org.apache.xml.security.stax.ext.SecurePart) EncryptionPartDef(org.apache.xml.security.stax.impl.EncryptionPartDef) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 2 with EncryptionPartDef

use of org.apache.xml.security.stax.impl.EncryptionPartDef in project santuario-java by apache.

the class XMLEncryptOutputProcessor method processEvent.

@Override
public void processEvent(XMLSecEvent xmlSecEvent, OutputProcessorChain outputProcessorChain) throws XMLStreamException, XMLSecurityException {
    if (xmlSecEvent.getEventType() == XMLStreamConstants.START_ELEMENT) {
        XMLSecStartElement xmlSecStartElement = xmlSecEvent.asStartElement();
        // avoid double encryption when child elements matches too
        if (getActiveInternalEncryptionOutputProcessor() == null) {
            SecurePart securePart = securePartMatches(xmlSecStartElement, outputProcessorChain, XMLSecurityConstants.ENCRYPTION_PARTS);
            if (securePart != null) {
                LOG.debug("Matched encryptionPart for encryption");
                String tokenId = outputProcessorChain.getSecurityContext().get(XMLSecurityConstants.PROP_USE_THIS_TOKEN_ID_FOR_ENCRYPTION);
                SecurityTokenProvider<OutboundSecurityToken> securityTokenProvider = outputProcessorChain.getSecurityContext().getSecurityTokenProvider(tokenId);
                final OutboundSecurityToken securityToken = securityTokenProvider.getSecurityToken();
                EncryptionPartDef encryptionPartDef = new EncryptionPartDef();
                encryptionPartDef.setSecurePart(securePart);
                encryptionPartDef.setModifier(securePart.getModifier());
                encryptionPartDef.setEncRefId(IDGenerator.generateID(null));
                encryptionPartDef.setKeyId(securityTokenProvider.getId());
                encryptionPartDef.setSymmetricKey(securityToken.getSecretKey(getSecurityProperties().getEncryptionSymAlgorithm()));
                outputProcessorChain.getSecurityContext().putAsList(EncryptionPartDef.class, encryptionPartDef);
                AbstractInternalEncryptionOutputProcessor internalEncryptionOutputProcessor = createInternalEncryptionOutputProcessor(encryptionPartDef, xmlSecStartElement, outputProcessorChain.getDocumentContext().getEncoding(), (OutboundSecurityToken) securityToken.getKeyWrappingToken());
                internalEncryptionOutputProcessor.setXMLSecurityProperties(getSecurityProperties());
                internalEncryptionOutputProcessor.setAction(getAction());
                internalEncryptionOutputProcessor.init(outputProcessorChain);
                setActiveInternalEncryptionOutputProcessor(internalEncryptionOutputProcessor);
            }
        }
    }
    outputProcessorChain.processEvent(xmlSecEvent);
}
Also used : XMLSecStartElement(org.apache.xml.security.stax.ext.stax.XMLSecStartElement) EncryptionPartDef(org.apache.xml.security.stax.impl.EncryptionPartDef) OutboundSecurityToken(org.apache.xml.security.stax.securityToken.OutboundSecurityToken)

Aggregations

EncryptionPartDef (org.apache.xml.security.stax.impl.EncryptionPartDef)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 SecurePart (org.apache.xml.security.stax.ext.SecurePart)1 XMLSecStartElement (org.apache.xml.security.stax.ext.stax.XMLSecStartElement)1 OutboundSecurityToken (org.apache.xml.security.stax.securityToken.OutboundSecurityToken)1