Search in sources :

Example 1 with XMLSecurityEventReader

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

the class XMLSecEventTest method testwWiteAsEncodedUnicode.

@Test
public void testwWiteAsEncodedUnicode() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    Deque<XMLSecEvent> xmlSecEventDeque = new ArrayDeque<XMLSecEvent>();
    do {
        xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
    // EndDocumentEvent
    xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
    XMLSecurityEventReader xmlSecurityEventReader = new XMLSecurityEventReader(xmlSecEventDeque, 0);
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    final StringWriter stdWriter = new StringWriter();
    final StringWriter secWriter = new StringWriter();
    while (xmlEventReader.hasNext()) {
        XMLEvent stdXmlEvent = xmlEventReader.nextEvent();
        XMLEvent secXmlEvent = xmlSecurityEventReader.nextEvent();
        stdXmlEvent.writeAsEncodedUnicode(stdWriter);
        secXmlEvent.writeAsEncodedUnicode(secWriter);
    }
    Assert.assertEquals(secWriter.toString(), stdWriter.toString());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 2 with XMLSecurityEventReader

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

the class XMLSecurityEventReaderTest method testIndex.

@Test
public void testIndex() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    Deque<XMLSecEvent> xmlSecEventDeque = new ArrayDeque<XMLSecEvent>();
    do {
        xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
    // EndDocumentEvent
    xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
    int skip = 100;
    XMLSecurityEventReader xmlSecurityEventReader = new XMLSecurityEventReader(xmlSecEventDeque, skip);
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    int currentIndex = 0;
    while (xmlEventReader.hasNext()) {
        XMLEvent stdXmlEvent = xmlEventReader.nextEvent();
        if (currentIndex++ < skip) {
            continue;
        }
        XMLEvent secXmlEvent = xmlSecurityEventReader.nextEvent();
        Assert.assertEquals(stdXmlEvent.getEventType(), secXmlEvent.getEventType());
        XMLEvent stdPeekedXMLEvent = xmlEventReader.peek();
        XMLEvent secPeekedXMLEvent = xmlSecurityEventReader.peek();
        if (stdPeekedXMLEvent == null) {
            Assert.assertNull(secPeekedXMLEvent);
        } else {
            Assert.assertEquals(stdPeekedXMLEvent.getEventType(), secPeekedXMLEvent.getEventType());
        }
    }
    Assert.assertFalse(xmlEventReader.hasNext());
    Assert.assertFalse(xmlSecurityEventReader.hasNext());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ArrayDeque(java.util.ArrayDeque) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Example 3 with XMLSecurityEventReader

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

the class XMLSecurityEventReaderTest method testConformness.

@Test
public void testConformness() throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    Deque<XMLSecEvent> xmlSecEventDeque = new ArrayDeque<XMLSecEvent>();
    do {
        xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
    // EndDocumentEvent
    xmlSecEventDeque.push(XMLSecEventFactory.allocate(xmlStreamReader, null));
    XMLSecurityEventReader xmlSecurityEventReader = new XMLSecurityEventReader(xmlSecEventDeque, 0);
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/inExcl/plain-soap-1.1.xml"));
    while (xmlEventReader.hasNext()) {
        Assert.assertEquals(xmlEventReader.hasNext(), xmlSecurityEventReader.hasNext());
        XMLEvent stdXmlEvent = xmlEventReader.nextEvent();
        XMLEvent secXmlEvent = xmlSecurityEventReader.nextEvent();
        Assert.assertEquals(stdXmlEvent.getEventType(), secXmlEvent.getEventType());
        XMLEvent stdPeekedXMLEvent = xmlEventReader.peek();
        XMLEvent secPeekedXMLEvent = xmlSecurityEventReader.peek();
        if (stdPeekedXMLEvent == null) {
            Assert.assertNull(secPeekedXMLEvent);
        } else {
            Assert.assertEquals(stdPeekedXMLEvent.getEventType(), secPeekedXMLEvent.getEventType());
        }
    }
    Assert.assertFalse(xmlEventReader.hasNext());
    Assert.assertFalse(xmlSecurityEventReader.hasNext());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) ArrayDeque(java.util.ArrayDeque) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent) Test(org.junit.Test)

Example 4 with XMLSecurityEventReader

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

the class AbstractDecryptInputProcessor method parseEncryptedDataStructure.

private EncryptedDataType parseEncryptedDataStructure(boolean isSecurityHeaderEvent, XMLSecEvent xmlSecEvent, InputProcessorChain subInputProcessorChain) throws XMLStreamException, XMLSecurityException {
    Deque<XMLSecEvent> xmlSecEvents = new ArrayDeque<XMLSecEvent>();
    xmlSecEvents.push(xmlSecEvent);
    XMLSecEvent encryptedDataXMLSecEvent;
    int count = 0;
    int keyInfoCount = 0;
    do {
        subInputProcessorChain.reset();
        if (isSecurityHeaderEvent) {
            encryptedDataXMLSecEvent = subInputProcessorChain.processHeaderEvent();
        } else {
            encryptedDataXMLSecEvent = subInputProcessorChain.processEvent();
        }
        xmlSecEvents.push(encryptedDataXMLSecEvent);
        if (++count >= maximumAllowedEncryptedDataEvents) {
            throw new XMLSecurityException("stax.xmlStructureSizeExceeded", new Object[] { maximumAllowedEncryptedDataEvents });
        }
        // the keyInfoCount is necessary to prevent early while-loop abort when the KeyInfo also contains a CipherValue.
        if (encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.START_ELEMENT && encryptedDataXMLSecEvent.asStartElement().getName().equals(XMLSecurityConstants.TAG_dsig_KeyInfo)) {
            keyInfoCount++;
        } else if (encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.END_ELEMENT && encryptedDataXMLSecEvent.asEndElement().getName().equals(XMLSecurityConstants.TAG_dsig_KeyInfo)) {
            keyInfoCount--;
        }
    } while (!((encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.START_ELEMENT && encryptedDataXMLSecEvent.asStartElement().getName().equals(XMLSecurityConstants.TAG_xenc_CipherValue) || encryptedDataXMLSecEvent.getEventType() == XMLStreamConstants.END_ELEMENT && encryptedDataXMLSecEvent.asEndElement().getName().equals(XMLSecurityConstants.TAG_xenc_EncryptedData)) && keyInfoCount == 0));
    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_CipherValue));
    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_CipherData));
    xmlSecEvents.push(XMLSecEventFactory.createXmlSecEndElement(XMLSecurityConstants.TAG_xenc_EncryptedData));
    EncryptedDataType encryptedDataType;
    try {
        Unmarshaller unmarshaller = XMLSecurityConstants.getJaxbUnmarshaller(getSecurityProperties().isDisableSchemaValidation());
        @SuppressWarnings("unchecked") JAXBElement<EncryptedDataType> encryptedDataTypeJAXBElement = (JAXBElement<EncryptedDataType>) unmarshaller.unmarshal(new XMLSecurityEventReader(xmlSecEvents, 0));
        encryptedDataType = encryptedDataTypeJAXBElement.getValue();
    } catch (JAXBException e) {
        throw new XMLSecurityException(e);
    }
    return encryptedDataType;
}
Also used : EncryptedDataType(org.apache.xml.security.binding.xmlenc.EncryptedDataType) JAXBException(javax.xml.bind.JAXBException) XMLSecurityEventReader(org.apache.xml.security.stax.impl.XMLSecurityEventReader) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XMLSecEvent(org.apache.xml.security.stax.ext.stax.XMLSecEvent)

Aggregations

XMLSecurityEventReader (org.apache.xml.security.stax.impl.XMLSecurityEventReader)4 ArrayDeque (java.util.ArrayDeque)3 XMLEventReader (javax.xml.stream.XMLEventReader)3 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 XMLEvent (javax.xml.stream.events.XMLEvent)3 XMLSecEvent (org.apache.xml.security.stax.ext.stax.XMLSecEvent)3 Test (org.junit.Test)3 StringWriter (java.io.StringWriter)1 JAXBElement (javax.xml.bind.JAXBElement)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 EncryptedDataType (org.apache.xml.security.binding.xmlenc.EncryptedDataType)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1