use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.
the class Canonicalizer20010315Test method c14nAndCompare.
private boolean c14nAndCompare(String fileIn, String fileRef, String fileOut, String c14nURI, boolean validating, String xpath, Map<String, String> namespaces) throws IOException, FileNotFoundException, SAXException, ParserConfigurationException, CanonicalizationException, InvalidCanonicalizerException, TransformerException, XPathExpressionException {
DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(validating, false);
// throw away all warnings and errors
documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());
// org.xml.sax.EntityResolver resolver = new TestVectorResolver();
// documentBuilder.setEntityResolver(resolver);
// Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
Document doc = documentBuilder.parse(fileIn);
Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
byte[] c14nBytes = null;
if (xpath == null) {
c14nBytes = c14n.canonicalizeSubtree(doc);
} else {
NodeList nl = null;
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
DSNamespaceContext namespaceContext = new DSNamespaceContext(namespaces);
xPath.setNamespaceContext(namespaceContext);
nl = (NodeList) xPath.evaluate(xpath, doc, XPathConstants.NODESET);
c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
}
// org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
// byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
byte[] refBytes = JavaUtils.getBytesFromFile(fileRef);
// if everything is OK, result is true; we do a binary compare, byte by byte
boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);
if (!result) {
File f = new File(fileOut);
if (!f.exists()) {
File parent = new File(f.getParent());
parent.mkdirs();
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f);
fos.write(c14nBytes);
LOG.debug("Wrote erroneous result to file " + f.toURI().toURL().toString());
assertEquals(new String(refBytes), new String(c14nBytes));
fos.close();
}
return result;
}
use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.
the class InteropTestBase method verifyHMAC.
/**
* Method verifyHMAC
*
* @param filename
* @param resolver
* @param hmacKey
*
* @throws Exception
*/
public boolean verifyHMAC(String filename, ResourceResolverSpi resolver, boolean followManifests, byte[] hmacKey) throws Exception {
File f = new File(filename);
javax.xml.parsers.DocumentBuilder db = XMLUtils.createDocumentBuilder(false, false);
org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Signature[1]";
Element sigElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
XMLSignature signature = new XMLSignature(sigElement, f.toURI().toURL().toString());
if (resolver != null) {
signature.addResourceResolver(resolver);
}
signature.setFollowNestedManifests(followManifests);
byte[] keybytes = hmacKey;
javax.crypto.SecretKey sk = signature.createSecretKey(keybytes);
return signature.checkSignatureValue(sk);
}
use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.
the class UnknownAlgoSignatureTest method unmarshalXMLSignature.
private XMLSignature unmarshalXMLSignature(String fileName) throws ParserConfigurationException, SAXException, IOException, TransformerException, XMLSecurityException, XPathExpressionException {
File file = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
file = new File(BASEDIR + SEP + SIGNATURE_SOURCE_PATH, fileName);
} else {
file = new File(SIGNATURE_SOURCE_PATH, fileName);
}
Document doc = getDocument(file);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Signature[1]";
Element sigElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
return new XMLSignature(sigElement, file.toURI().toURL().toString());
}
use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.
the class TransformXSLTTest method test1.
/**
* Make sure Transform.performTransform does not throw NullPointerException.
* See bug 41927 for more info.
*/
@org.junit.Test
public void test1() throws Exception {
File file1 = null;
File file2 = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
file1 = new File(BASEDIR + SEP + SOURCE_PATH, SIGNATURE_FILE);
file2 = new File(BASEDIR + SEP + SOURCE_PATH, STYLESHEET_FILE);
} else {
file1 = new File(SOURCE_PATH, SIGNATURE_FILE);
file1 = new File(SOURCE_PATH, STYLESHEET_FILE);
}
Document doc1 = getDocument(file1);
Document doc2 = getDocument(file2);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Transform[1]";
Element transformEl = (Element) xpath.evaluate(expression, doc1, XPathConstants.NODE);
Transform transform = new Transform(doc1, Transforms.TRANSFORM_XSLT, transformEl.getChildNodes());
transform.performTransform(new XMLSignatureInput(doc2));
}
use of org.apache.xml.security.test.dom.DSNamespaceContext in project santuario-java by apache.
the class KeyWrapEncryptionAlgorithmTest method encrypt.
private void encrypt(EncryptedKey encryptedKey, String algorithm, Document document, List<String> localNames, Key encryptingKey) throws Exception {
XMLCipher cipher = XMLCipher.getInstance(algorithm);
cipher.init(XMLCipher.ENCRYPT_MODE, encryptingKey);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
EncryptedData builder = cipher.getEncryptedData();
KeyInfo builderKeyInfo = builder.getKeyInfo();
if (builderKeyInfo == null) {
builderKeyInfo = new KeyInfo(document);
builder.setKeyInfo(builderKeyInfo);
}
builderKeyInfo.add(encryptedKey);
for (String localName : localNames) {
String expression = "//*[local-name()='" + localName + "']";
Element elementToEncrypt = (Element) xpath.evaluate(expression, document, XPathConstants.NODE);
Assert.assertNotNull(elementToEncrypt);
document = cipher.doFinal(document, elementToEncrypt, false);
}
NodeList nodeList = document.getElementsByTagNameNS(XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(), XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart());
Assert.assertTrue(nodeList.getLength() > 0);
}
Aggregations