use of org.apache.xml.security.stax.ext.stax.XMLSecEvent 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.ext.stax.XMLSecEvent in project santuario-java by apache.
the class Canonicalizer11Test method test38.
/**
* 3.8 Document Subsets and XML Attributes (modified)
*
* @see <A HREF="http://www.w3.org/TR/2007/CR-xml-c14n11-20070621/#Example-DocSubsetsXMLAttrs">the example from the spec</A>
*/
@Test
@org.junit.Ignore
public void test38() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Canonicalizer11_OmitCommentsTransformer c = new Canonicalizer11_OmitCommentsTransformer();
c.setOutputStream(baos);
XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("org/apache/xml/security/c14n/in/38_input.xml"));
XMLSecEvent xmlSecEvent = null;
while (xmlSecEventReader.hasNext()) {
xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
if (xmlSecEvent.isStartElement() && xmlSecEvent.asStartElement().getName().equals(new QName("http://www.ietf.org", "e1"))) {
break;
}
}
while (xmlSecEventReader.hasNext()) {
c.transform(xmlSecEvent);
if (xmlSecEvent.isEndElement() && xmlSecEvent.asEndElement().getName().equals(new QName("http://www.ietf.org", "e1"))) {
break;
}
xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
}
byte[] reference = getBytesFromResource(this.getClass().getClassLoader().getResource("org/apache/xml/security/c14n/in/38_c14n.xml"));
boolean equals = java.security.MessageDigest.isEqual(reference, baos.toByteArray());
if (!equals) {
System.out.println("Expected:\n" + new String(reference, StandardCharsets.UTF_8));
System.out.println("");
System.out.println("Got:\n" + new String(baos.toByteArray(), StandardCharsets.UTF_8));
}
assertTrue(equals);
}
use of org.apache.xml.security.stax.ext.stax.XMLSecEvent in project santuario-java by apache.
the class Canonicalizer11Test method c14nAndCompare.
// /**
// * The XPath data model represents data using UCS characters.
// * Implementations MUST use XML processors that support UTF-8 and UTF-16
// * and translate to the UCS character domain. For UTF-16, the leading byte
// * order mark is treated as an artifact of encoding and stripped from the
// * UCS character data (subsequent zero width non-breaking spaces appearing
// * within the UTF-16 data are not removed) [UTF-16, Section 3.2]. Support
// * for ISO-8859-1 encoding is RECOMMENDED, and all other character encodings
// * are OPTIONAL.
// *
// * $todo$ implement the test
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testTranslationFromUTF16toUTF8() throws Exception {
//
// String val =
// "<UTF16>The german &auml (which is Unicode &#xE4;): "ä"</UTF16>";
// byte utf16[] = convertToUTF16(val.getBytes());
// Canonicalizer c14n =
// Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
// byte c14nBytes[] = c14n.canonicalize(utf16);
// org.xml.sax.EntityResolver resolver = new TestVectorResolver();
// InputStream refStream = resolver.resolveEntity(
// null,
// prefix + "/in/testTranslationFromUTF16toUTF8.xml")
// .getByteStream();
// byte refBytes[] = JavaUtils.getBytesFromStream(refStream);
// boolean equal = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
//
// assertTrue("Parser does not translate to UCS character domain", equal);
// }
//
// /**
// * Method testXMLAttributes1
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes1() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included >"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method testXMLAttributes2
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes2() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='uk'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included >"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"uk\">"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method testXMLAttributes3
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void testXMLAttributes3() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included xml:lang='de'>"
// + "</included>"
// + "</notIncluded>"
// + "</notIncluded>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included xml:lang=\"de\">"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method testXMLAttributes4
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes4() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included >"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included>"
// + "<included xml:lang=\"uk\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method testXMLAttributes5
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes5() throws Exception {
// //J-
// String input = ""
// + "<included xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:space='preserve' xml:lang='uk'>"
// + "<included >"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"de\">"
// + "<included>"
// + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method testXMLAttributes6
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// public static void _testXMLAttributes6() throws Exception {
// //J-
// String input = ""
// + "<included xml:space='preserve' xml:lang='de'>"
// + "<included xml:lang='de'>"
// + "<notIncluded xml:lang='uk'>"
// + "<included>"
// + "</included>"
// + "</notIncluded>"
// + "</included>"
// + "</included>";
//
// String definedOutput = ""
// + "<included xml:lang=\"de\" xml:space=\"preserve\">"
// + "<included>"
// + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
// + "</included>"
// + "</included>"
// + "</included>";
// //J+
// assertTrue(doTestXMLAttributes(input, definedOutput));
// }
//
// /**
// * Method doTestXMLAttributes
// *
// * @param input
// * @param definedOutput
// * @param writeResultsToFile
// *
// * @throws CanonicalizationException
// * @throws java.io.FileNotFoundException
// * @throws java.io.IOException
// * @throws InvalidCanonicalizerException
// * @throws javax.xml.parsers.ParserConfigurationException
// * @throws org.xml.sax.SAXException
// * @throws javax.xml.transform.TransformerException
// */
// private static boolean doTestXMLAttributes(
// String input, String definedOutput) throws Exception {
//
// DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
//
// dfactory.setNamespaceAware(true);
// dfactory.setValidating(true);
//
// DocumentBuilder db = dfactory.newDocumentBuilder();
//
// db.setErrorHandler(new org.apache.xml.security.utils
// .IgnoreAllErrorHandler());
//
// Document doc = db.parse(new ByteArrayInputStream(input.getBytes()));
// Canonicalizer c14nizer =
// Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
// CachedXPathAPI xpathAPI = new CachedXPathAPI();
//
// //XMLUtils.circumventBug2650(doc);
//
// NodeList nodes =
// xpathAPI.selectNodeList(doc, "(//*[local-name()='included'] | //@*[parent::node()[local-name()='included']])");
// byte result[] = c14nizer.canonicalizeXPathNodeSet(nodes);
// byte defined[] = definedOutput.getBytes();
// assertEquals(definedOutput, new String(result));
// return java.security.MessageDigest.isEqual(defined, result);
// }
/**
* Method c14nAndCompare
*/
private void c14nAndCompare(URL fileIn, URL fileRef, boolean omitComments) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CanonicalizerBase canonicalizerBase;
if (omitComments) {
canonicalizerBase = new Canonicalizer11_OmitCommentsTransformer();
canonicalizerBase.setOutputStream(baos);
} else {
canonicalizerBase = new Canonicalizer11_WithCommentsTransformer();
canonicalizerBase.setOutputStream(baos);
}
XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(fileIn.openStream());
while (xmlSecEventReader.hasNext()) {
XMLSecEvent xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
canonicalizerBase.transform(xmlSecEvent);
}
// org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
// byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
byte[] refBytes = getBytesFromResource(fileRef);
// if everything is OK, result is true; we do a binary compare, byte by byte
boolean result = java.security.MessageDigest.isEqual(refBytes, baos.toByteArray());
if (!result) {
assertEquals(new String(baos.toByteArray(), StandardCharsets.UTF_8), new String(refBytes, StandardCharsets.UTF_8));
}
assertTrue(result);
}
use of org.apache.xml.security.stax.ext.stax.XMLSecEvent 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;
}
use of org.apache.xml.security.stax.ext.stax.XMLSecEvent 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);
}
}
Aggregations