Search in sources :

Example 1 with SignedInfoType

use of org.apache.xml.security.binding.xmldsig.SignedInfoType in project santuario-java by apache.

the class AbstractSignatureInputHandler method reparseSignedInfo.

protected Deque<XMLSecEvent> reparseSignedInfo(InputProcessorChain inputProcessorChain, XMLSecurityProperties securityProperties, SignatureType signatureType, Deque<XMLSecEvent> eventDeque, int index) throws XMLSecurityException {
    Deque<XMLSecEvent> signedInfoDeque = new ArrayDeque<XMLSecEvent>();
    try (UnsyncByteArrayOutputStream unsynchronizedByteArrayOutputStream = new UnsyncByteArrayOutputStream()) {
        Transformer transformer = XMLSecurityUtils.getTransformer(null, unsynchronizedByteArrayOutputStream, null, signatureType.getSignedInfo().getCanonicalizationMethod().getAlgorithm(), XMLSecurityConstants.DIRECTION.IN);
        Iterator<XMLSecEvent> iterator = eventDeque.descendingIterator();
        // forward to <Signature> Element
        int i = 0;
        while (i < index) {
            iterator.next();
            i++;
        }
        loop: while (iterator.hasNext()) {
            XMLSecEvent xmlSecEvent = iterator.next();
            switch(xmlSecEvent.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    if (xmlSecEvent.asStartElement().getName().equals(XMLSecurityConstants.TAG_dsig_SignedInfo)) {
                        transformer.transform(xmlSecEvent);
                        break loop;
                    }
                    break;
            }
        }
        loop: while (iterator.hasNext()) {
            XMLSecEvent xmlSecEvent = iterator.next();
            transformer.transform(xmlSecEvent);
            switch(xmlSecEvent.getEventType()) {
                case XMLStreamConstants.END_ELEMENT:
                    if (xmlSecEvent.asEndElement().getName().equals(XMLSecurityConstants.TAG_dsig_SignedInfo)) {
                        break loop;
                    }
                    break;
            }
        }
        transformer.doFinal();
        try (InputStream is = new UnsyncByteArrayInputStream(unsynchronizedByteArrayOutputStream.toByteArray())) {
            XMLStreamReader xmlStreamReader = inputProcessorChain.getSecurityContext().<XMLInputFactory>get(XMLSecurityConstants.XMLINPUTFACTORY).createXMLStreamReader(is);
            while (xmlStreamReader.hasNext()) {
                XMLSecEvent xmlSecEvent = XMLSecEventFactory.allocate(xmlStreamReader, null);
                signedInfoDeque.push(xmlSecEvent);
                xmlStreamReader.next();
            }
            @SuppressWarnings("unchecked") final SignedInfoType signedInfoType = ((JAXBElement<SignedInfoType>) parseStructure(signedInfoDeque, 0, securityProperties)).getValue();
            signatureType.setSignedInfo(signedInfoType);
            return signedInfoDeque;
        }
    } catch (XMLStreamException | IOException e) {
        throw new XMLSecurityException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream) InputStream(java.io.InputStream) SignedInfoType(org.apache.xml.security.binding.xmldsig.SignedInfoType) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) XMLStreamException(javax.xml.stream.XMLStreamException) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JAXBElement (javax.xml.bind.JAXBElement)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 SignedInfoType (org.apache.xml.security.binding.xmldsig.SignedInfoType)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)1 UnsyncByteArrayInputStream (org.apache.xml.security.utils.UnsyncByteArrayInputStream)1 UnsyncByteArrayOutputStream (org.apache.xml.security.utils.UnsyncByteArrayOutputStream)1