use of org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer in project santuario-java by apache.
the class AbstractSignatureReferenceVerifyInputProcessor method buildTransformerChain.
protected Transformer buildTransformerChain(ReferenceType referenceType, OutputStream outputStream, InputProcessorChain inputProcessorChain, InternalSignatureReferenceVerifier internalSignatureReferenceVerifier) throws XMLSecurityException {
// If no Transforms then just default to an Inclusive without comments transform
if (referenceType.getTransforms() == null || referenceType.getTransforms().getTransform().isEmpty()) {
AlgorithmSuiteSecurityEvent algorithmSuiteSecurityEvent = new AlgorithmSuiteSecurityEvent();
algorithmSuiteSecurityEvent.setAlgorithmURI(XMLSecurityConstants.NS_C14N_OMIT_COMMENTS);
algorithmSuiteSecurityEvent.setAlgorithmUsage(XMLSecurityConstants.SigTransform);
algorithmSuiteSecurityEvent.setCorrelationID(referenceType.getId());
inputProcessorChain.getSecurityContext().registerSecurityEvent(algorithmSuiteSecurityEvent);
Transformer transformer = new Canonicalizer20010315_OmitCommentsTransformer();
transformer.setOutputStream(outputStream);
return transformer;
}
List<TransformType> transformTypeList = referenceType.getTransforms().getTransform();
if (transformTypeList.size() == 1 && XMLSecurityConstants.NS_XMLDSIG_ENVELOPED_SIGNATURE.equals(transformTypeList.get(0).getAlgorithm())) {
TransformType transformType = new TransformType();
transformType.setAlgorithm(XMLSecurityConstants.NS_C14N_OMIT_COMMENTS);
transformTypeList.add(transformType);
}
if (transformTypeList.size() > maximumAllowedTransformsPerReference) {
throw new XMLSecurityException("secureProcessing.MaximumAllowedTransformsPerReference", new Object[] { transformTypeList.size(), maximumAllowedTransformsPerReference });
}
Transformer parentTransformer = null;
for (int i = transformTypeList.size() - 1; i >= 0; i--) {
TransformType transformType = transformTypeList.get(i);
String algorithm = transformType.getAlgorithm();
AlgorithmSuiteSecurityEvent algorithmSuiteSecurityEvent = new AlgorithmSuiteSecurityEvent();
algorithmSuiteSecurityEvent.setAlgorithmURI(algorithm);
algorithmSuiteSecurityEvent.setAlgorithmUsage(XMLSecurityConstants.SigTransform);
algorithmSuiteSecurityEvent.setCorrelationID(referenceType.getId());
inputProcessorChain.getSecurityContext().registerSecurityEvent(algorithmSuiteSecurityEvent);
InclusiveNamespaces inclusiveNamespacesType = XMLSecurityUtils.getQNameType(transformType.getContent(), XMLSecurityConstants.TAG_c14nExcl_InclusiveNamespaces);
Map<String, Object> transformerProperties = null;
if (inclusiveNamespacesType != null) {
transformerProperties = new HashMap<>();
transformerProperties.put(Canonicalizer20010315_Excl.INCLUSIVE_NAMESPACES_PREFIX_LIST, inclusiveNamespacesType.getPrefixList());
}
if (parentTransformer != null) {
parentTransformer = XMLSecurityUtils.getTransformer(parentTransformer, null, transformerProperties, algorithm, XMLSecurityConstants.DIRECTION.IN);
} else {
parentTransformer = XMLSecurityUtils.getTransformer(null, outputStream, transformerProperties, algorithm, XMLSecurityConstants.DIRECTION.IN);
}
}
return parentTransformer;
}
use of org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer in project santuario-java by apache.
the class Canonicalizer20010315Test 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 ommitComments) throws Exception {
CanonicalizerBase canonicalizerBase;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (ommitComments) {
canonicalizerBase = new Canonicalizer20010315_OmitCommentsTransformer();
canonicalizerBase.setOutputStream(baos);
} else {
canonicalizerBase = new Canonicalizer20010315_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.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer in project santuario-java by apache.
the class TransformCanonicalizerTest method testInputStreamToOutputStreamStreamAPI.
@Test
public void testInputStreamToOutputStreamStreamAPI() throws Exception {
Canonicalizer20010315_OmitCommentsTransformer canonicalizerTransformer = new Canonicalizer20010315_OmitCommentsTransformer();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
canonicalizerTransformer.setOutputStream(byteArrayOutputStream);
canonicalizerTransformer.transform(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
Assert.assertEquals(554, byteArrayOutputStream.size());
}
use of org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer in project santuario-java by apache.
the class TransformCanonicalizerTest method testInputStreamToXMLSecEventAPI.
@Test
public void testInputStreamToXMLSecEventAPI() throws Exception {
Canonicalizer20010315_OmitCommentsTransformer canonicalizerTransformer = new Canonicalizer20010315_OmitCommentsTransformer();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Transformer transformer = new Transformer() {
@Override
public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
}
@Override
public void setTransformer(Transformer transformer) throws XMLSecurityException {
}
@Override
public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
}
@Override
public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
return XMLSecurityConstants.TransformMethod.XMLSecEvent;
}
@Override
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
Assert.fail("unexpected call to transform(XMLSecEvent");
}
@Override
public void transform(InputStream inputStream) throws XMLStreamException {
try {
XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void doFinal() throws XMLStreamException {
}
};
canonicalizerTransformer.setTransformer(transformer);
canonicalizerTransformer.transform(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
canonicalizerTransformer.doFinal();
Assert.assertEquals(554, byteArrayOutputStream.size());
}
use of org.apache.xml.security.stax.impl.transformer.canonicalizer.Canonicalizer20010315_OmitCommentsTransformer in project santuario-java by apache.
the class TransformCanonicalizerTest method testXMLSecEventToXMLSecEventAPI.
@Test
public void testXMLSecEventToXMLSecEventAPI() throws Exception {
Canonicalizer20010315_OmitCommentsTransformer canonicalizerTransformer = new Canonicalizer20010315_OmitCommentsTransformer();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Transformer transformer = new Transformer() {
@Override
public void setOutputStream(OutputStream outputStream) throws XMLSecurityException {
}
@Override
public void setTransformer(Transformer transformer) throws XMLSecurityException {
}
@Override
public void setProperties(Map<String, Object> properties) throws XMLSecurityException {
}
@Override
public XMLSecurityConstants.TransformMethod getPreferredTransformMethod(XMLSecurityConstants.TransformMethod forInput) {
return XMLSecurityConstants.TransformMethod.XMLSecEvent;
}
@Override
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
Assert.fail("unexpected call to transform(XMLSecEvent");
}
@Override
public void transform(InputStream inputStream) throws XMLStreamException {
try {
XMLSecurityUtils.copy(inputStream, byteArrayOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void doFinal() throws XMLStreamException {
}
};
canonicalizerTransformer.setTransformer(transformer);
XMLEventReader xmlSecEventReader = xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext-base64.xml"));
while (xmlSecEventReader.hasNext()) {
XMLSecEvent xmlSecEvent = (XMLSecEvent) xmlSecEventReader.nextEvent();
canonicalizerTransformer.transform(xmlSecEvent);
}
canonicalizerTransformer.doFinal();
Assert.assertEquals(554, byteArrayOutputStream.size());
}
Aggregations