Search in sources :

Example 6 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream in project santuario-java by apache.

the class CanonicalizerBase method engineCanonicalizeXPathNodeSetInternal.

private byte[] engineCanonicalizeXPathNodeSetInternal(Node doc) throws CanonicalizationException {
    try {
        this.canonicalizeXPathNodeSet(doc, doc);
        this.writer.flush();
        if (this.writer instanceof ByteArrayOutputStream) {
            byte[] sol = ((ByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((ByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return sol;
        } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
            byte[] result = ((UnsyncByteArrayOutputStream) this.writer).toByteArray();
            if (reset) {
                ((UnsyncByteArrayOutputStream) this.writer).reset();
            } else {
                this.writer.close();
            }
            return result;
        } else {
            this.writer.close();
        }
        return null;
    } catch (UnsupportedEncodingException ex) {
        throw new CanonicalizationException(ex);
    } catch (IOException ex) {
        throw new CanonicalizationException(ex);
    }
}
Also used : UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsyncByteArrayOutputStream(org.apache.xml.security.utils.UnsyncByteArrayOutputStream) IOException(java.io.IOException)

Example 7 with UnsyncByteArrayOutputStream

use of org.apache.xml.security.utils.UnsyncByteArrayOutputStream 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

UnsyncByteArrayOutputStream (org.apache.xml.security.utils.UnsyncByteArrayOutputStream)7 UnsyncByteArrayInputStream (org.apache.xml.security.utils.UnsyncByteArrayInputStream)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)2 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 XMLSecurityConstants (org.apache.xml.security.stax.ext.XMLSecurityConstants)2 XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)2 InputStream (java.io.InputStream)1 JAXBElement (javax.xml.bind.JAXBElement)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Base64InputStream (org.apache.commons.codec.binary.Base64InputStream)1 Base64OutputStream (org.apache.commons.codec.binary.Base64OutputStream)1 SignedInfoType (org.apache.xml.security.binding.xmldsig.SignedInfoType)1 XMLEventReaderInputProcessor (org.apache.xml.security.stax.impl.processor.input.XMLEventReaderInputProcessor)1