Search in sources :

Example 16 with Reference

use of org.apache.xml.security.signature.Reference in project santuario-java by apache.

the class ExclusiveC14NInteropTest method t.

private String t(String directory, String file, boolean secureValidation) throws Exception {
    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        directory = basedir + "/" + directory;
    }
    File f = new File(directory + "/" + file);
    javax.xml.parsers.DocumentBuilder db = XMLUtils.createDocumentBuilder(false, false);
    org.w3c.dom.Document doc = db.parse(f);
    Element sigElement = (Element) doc.getElementsByTagNameNS(Constants.SignatureSpecNS, Constants._TAG_SIGNATURE).item(0);
    XMLSignature signature = new XMLSignature(sigElement, f.toURI().toURL().toString(), secureValidation);
    boolean verify = signature.checkSignatureValue(signature.getKeyInfo().getPublicKey());
    LOG.debug("   signature.checkSignatureValue finished: " + verify);
    // if (!verify) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < signature.getSignedInfo().getLength(); i++) {
        boolean refVerify = signature.getSignedInfo().getVerificationResult(i);
        if (refVerify) {
            LOG.debug("Reference " + i + " was OK");
        } else {
            sb.append(i);
            sb.append(" ");
            // JavaUtils.writeBytesToFilename(directory + "/c14n-" + i + ".apache.txt", signature.getSignedInfo().item(i).getContentsAfterTransformation().getBytes());
            // JavaUtils.writeBytesToFilename(directory + "/c14n-" + i + ".apache.html", signature.getSignedInfo().item(i).getHTMLRepresentation().getBytes());
            Reference reference = signature.getSignedInfo().item(i);
            int length = reference.getTransforms().getLength();
            String algo = reference.getTransforms().item(length - 1).getURI();
            LOG.debug("Reference " + i + " failed: " + algo);
        }
    }
    String r = sb.toString().trim();
    if (r.length() == 0) {
        return null;
    } else {
        return r;
    }
}
Also used : Reference(org.apache.xml.security.signature.Reference) Element(org.w3c.dom.Element) XMLSignature(org.apache.xml.security.signature.XMLSignature) File(java.io.File)

Aggregations

Reference (org.apache.xml.security.signature.Reference)16 Element (org.w3c.dom.Element)7 DataObjectDesc (xades4j.properties.DataObjectDesc)7 SignedInfo (org.apache.xml.security.signature.SignedInfo)6 XMLSignature (org.apache.xml.security.signature.XMLSignature)6 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)5 Document (org.w3c.dom.Document)5 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)4 ArrayList (java.util.ArrayList)3 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)3 Transforms (org.apache.xml.security.transforms.Transforms)3 XAdES4jXMLSigException (xades4j.XAdES4jXMLSigException)3 PublicKey (java.security.PublicKey)2 X509Certificate (java.security.cert.X509Certificate)2 KeyInfo (org.apache.xml.security.keys.KeyInfo)2 ReferenceData (org.apache.xml.security.signature.reference.ReferenceData)2 ReferenceNodeSetData (org.apache.xml.security.signature.reference.ReferenceNodeSetData)2 ResourceResolver (org.apache.xml.security.utils.resolver.ResourceResolver)2 ResolverAnonymous (org.apache.xml.security.utils.resolver.implementations.ResolverAnonymous)2 Test (org.junit.Test)2