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());
}
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());
}
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());
}
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;
}
Aggregations