Search in sources :

Example 1 with XPathContainer

use of org.apache.xml.security.transforms.params.XPathContainer in project xades4j by luisgoncalves.

the class XPathTransformParamsMarshaller method marshalParameters.

@Override
public List<Node> marshalParameters(XPathTransform alg, Document doc) {
    XPathContainer xpathContainer = new XPathContainer(doc);
    xpathContainer.setXPath(alg.getXPath());
    for (Map.Entry<String, String> ns : alg.getNamespaces().entrySet()) {
        try {
            xpathContainer.setXPathNamespaceContext(ns.getKey(), ns.getValue());
        } catch (XMLSecurityException ex) {
            throw new IllegalArgumentException("Invalid namespaces for XPath query", ex);
        }
    }
    return Collections.singletonList((Node) xpathContainer.getElement());
}
Also used : XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) Map(java.util.Map) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 2 with XPathContainer

use of org.apache.xml.security.transforms.params.XPathContainer in project santuario-java by apache.

the class XMLCipherTest method testSameDocumentCipherReference.

/*
     * Test a Cipher Reference
     */
@org.junit.Test
public void testSameDocumentCipherReference() throws Exception {
    if (haveISOPadding) {
        DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
        Document d = db.newDocument();
        Element docElement = d.createElement("EncryptedDoc");
        d.appendChild(docElement);
        // Create the XMLCipher object
        cipher = XMLCipher.getInstance();
        EncryptedData ed = cipher.createEncryptedData(CipherData.REFERENCE_TYPE, "#CipherTextId");
        EncryptionMethod em = cipher.createEncryptionMethod(XMLCipher.AES_128);
        ed.setEncryptionMethod(em);
        org.apache.xml.security.encryption.Transforms xencTransforms = cipher.createTransforms(d);
        ed.getCipherData().getCipherReference().setTransforms(xencTransforms);
        org.apache.xml.security.transforms.Transforms dsTransforms = xencTransforms.getDSTransforms();
        // An XPath transform
        XPathContainer xpc = new XPathContainer(d);
        xpc.setXPath("self::text()[parent::CipherText[@Id=\"CipherTextId\"]]");
        dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_XPATH, xpc.getElementPlusReturns());
        // Add a Base64 Transforms
        dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_BASE64_DECODE);
        Element ee = cipher.martial(d, ed);
        docElement.appendChild(ee);
        // Add the cipher text
        Element encryptedElement = d.createElement("CipherText");
        encryptedElement.setAttributeNS(null, "Id", "CipherTextId");
        encryptedElement.setIdAttributeNS(null, "Id", true);
        encryptedElement.appendChild(d.createTextNode(tstBase64EncodedString));
        docElement.appendChild(encryptedElement);
        // dump(d);
        // Now the decrypt, with a brand new cipher
        XMLCipher cipherDecrypt = XMLCipher.getInstance();
        Key key = new SecretKeySpec("abcdefghijklmnop".getBytes(StandardCharsets.US_ASCII), "AES");
        cipherDecrypt.init(XMLCipher.DECRYPT_MODE, key);
        byte[] decryptBytes = cipherDecrypt.decryptToByteArray(ee);
        assertEquals("A test encrypted secret", new String(decryptBytes, StandardCharsets.US_ASCII));
    } else {
        LOG.warn("Test testSameDocumentCipherReference skipped as " + "necessary algorithms not available");
    }
}
Also used : Element(org.w3c.dom.Element) XMLCipher(org.apache.xml.security.encryption.XMLCipher) EncryptionMethod(org.apache.xml.security.encryption.EncryptionMethod) Document(org.w3c.dom.Document) XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SecretKeySpec(javax.crypto.spec.SecretKeySpec) EncryptedData(org.apache.xml.security.encryption.EncryptedData) PublicKey(java.security.PublicKey) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Example 3 with XPathContainer

use of org.apache.xml.security.transforms.params.XPathContainer in project santuario-java by apache.

the class XmlSecTest method checkXmlSignatureSoftwareStack.

private void checkXmlSignatureSoftwareStack(boolean cert) throws Exception {
    Init.init();
    DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(false);
    Document testDocument = documentBuilder.newDocument();
    Element rootElement = testDocument.createElementNS("urn:namespace", "tns:document");
    rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", "urn:namespace");
    testDocument.appendChild(rootElement);
    Element childElement = testDocument.createElementNS("urn:childnamespace", "t:child");
    childElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:t", "urn:childnamespace");
    childElement.appendChild(testDocument.createTextNode("hello world"));
    rootElement.appendChild(childElement);
    PrivateKey privateKey;
    PublicKey publicKey = null;
    X509Certificate signingCert = null;
    if (cert) {
        // get key & self-signed certificate from keystore
        String fs = System.getProperty("file.separator");
        FileInputStream fis = new FileInputStream(BASEDIR + fs + "src/test/resources" + fs + "test.jks");
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(fis, "changeit".toCharArray());
        signingCert = (X509Certificate) ks.getCertificate("mullan");
        publicKey = signingCert.getPublicKey();
        privateKey = (PrivateKey) ks.getKey("mullan", "changeit".toCharArray());
    } else {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(1024);
        KeyPair keyPair = kpg.generateKeyPair();
        publicKey = keyPair.getPublic();
        privateKey = keyPair.getPrivate();
    }
    XMLSignature signature = new XMLSignature(testDocument, "", XMLSignature.ALGO_ID_SIGNATURE_DSA, Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
    Element signatureElement = signature.getElement();
    rootElement.appendChild(signatureElement);
    Transforms transforms = new Transforms(testDocument);
    XPathContainer xpath = new XPathContainer(testDocument);
    xpath.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
    xpath.setXPath("not(ancestor-or-self::ds:Signature)");
    transforms.addTransform(Transforms.TRANSFORM_XPATH, xpath.getElementPlusReturns());
    transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
    signature.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);
    if (cert) {
        signature.addKeyInfo(signingCert);
    } else {
        signature.addKeyInfo(publicKey);
    }
    Element nsElement = testDocument.createElementNS(null, "nsElement");
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
    signature.sign(privateKey);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    xPath.setNamespaceContext(new DSNamespaceContext());
    String expression = "//ds:Signature[1]";
    Element sigElement = (Element) xPath.evaluate(expression, testDocument, XPathConstants.NODE);
    XMLSignature signatureToVerify = new XMLSignature(sigElement, "");
    boolean signResult = signatureToVerify.checkSignatureValue(publicKey);
    assertTrue(signResult);
}
Also used : XPath(javax.xml.xpath.XPath) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) KeyPairGenerator(java.security.KeyPairGenerator) Document(org.w3c.dom.Document) KeyStore(java.security.KeyStore) XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLSignature(org.apache.xml.security.signature.XMLSignature) DSNamespaceContext(org.apache.xml.security.test.dom.DSNamespaceContext)

Example 4 with XPathContainer

use of org.apache.xml.security.transforms.params.XPathContainer in project santuario-java by apache.

the class CreateSignatureTest method testEmptyNodeSet.

/**
 * Test for bug 36044 - Canonicalizing an empty node-set throws an
 * ArrayIndexOutOfBoundsException.
 */
@org.junit.Test
public void testEmptyNodeSet() throws Exception {
    Document doc = db.newDocument();
    Element envelope = doc.createElementNS("http://www.usps.gov/", "Envelope");
    envelope.appendChild(doc.createTextNode("\n"));
    doc.appendChild(envelope);
    XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_DSA);
    envelope.appendChild(sig.getElement());
    ObjectContainer object1 = new ObjectContainer(doc);
    object1.setId("object-1");
    object1.setMimeType("text/plain");
    sig.appendObject(object1);
    ObjectContainer object2 = new ObjectContainer(doc);
    object2.setId("object-2");
    object2.setMimeType("text/plain");
    object2.setEncoding("http://www.w3.org/2000/09/xmldsig#base64");
    object2.appendChild(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="));
    sig.appendObject(object2);
    Transforms transforms = new Transforms(doc);
    XPathContainer xpathC = new XPathContainer(doc);
    xpathC.setXPath("self::text()");
    transforms.addTransform(Transforms.TRANSFORM_XPATH, xpathC.getElementPlusReturns());
    sig.addDocument("#object-1", transforms, Constants.ALGO_ID_DIGEST_SHA1, null, "http://www.w3.org/2000/09/xmldsig#Object");
    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream fis = null;
    if (BASEDIR != null && !"".equals(BASEDIR)) {
        fis = new FileInputStream(BASEDIR + SEP + "src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
    } else {
        fis = new FileInputStream("src/test/resources/org/apache/xml/security/samples/input/keystore.jks");
    }
    ks.load(fis, "xmlsecurity".toCharArray());
    PrivateKey privateKey = (PrivateKey) ks.getKey("test", "xmlsecurity".toCharArray());
    sig.sign(privateKey);
}
Also used : PrivateKey(java.security.PrivateKey) XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) Document(org.w3c.dom.Document) ObjectContainer(org.apache.xml.security.signature.ObjectContainer) XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream)

Example 5 with XPathContainer

use of org.apache.xml.security.transforms.params.XPathContainer in project santuario-java by apache.

the class CreateExclC14nInteropValues method main.

/**
 * Method main
 *
 * @param unused
 * @throws Exception
 */
public static void main(String[] unused) throws Exception {
    org.apache.xml.security.Init.init();
    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = db.newDocument();
    String directory = "data/org/apache/xml/security/c14n/outExcl/";
    File signatureFile = new File(directory + "apacheSignature.xml");
    XMLSignature xmlSignature = new XMLSignature(doc, signatureFile.toURI().toURL().toString(), XMLSignature.ALGO_ID_MAC_HMAC_SHA1);
    doc.appendChild(xmlSignature.getElement());
    {
        // ref 0
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::Parent or (parent::Parent and not(self::Child)) or self::GrandChild or parent::GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("iaikTests.example1.xml", tf);
    }
    {
        // ref 1
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::Parent or (parent::Parent and not(self::Child)) or self::GrandChild or parent::GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        tf.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
        xmlSignature.addDocument("iaikTests.example1.xml", tf);
    }
    {
        // ref 2
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:default", "http://example.org");
            xc.setXPath("self::Parent or (parent::Parent and not(self::default:Child)) or self::GrandChild or parent::GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("iaikTests.example2.xml", tf);
    }
    {
        // ref 3
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:default", "http://example.org");
            xc.setXPath("self::Parent or (parent::Parent and not(self::default:Child)) or self::GrandChild or parent::GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        tf.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
        xmlSignature.addDocument("iaikTests.example2.xml", tf);
    }
    {
        // ref 4
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:default", "http://example.org/default");
            xc.setXPathNamespaceContext("xmlns:ns1", "http://example.org/ns1");
            xc.setXPath("self::default:Parent or (parent::default:Parent and not(self::default:Child)) or self::ns1:GrandChild or parent::ns1:GrandChild or self::default:GrandChild or parent::default:GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("iaikTests.example3.xml", tf);
    }
    {
        // ref 5
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:default", "http://example.org/default");
            xc.setXPathNamespaceContext("xmlns:ns1", "http://example.org/ns1");
            xc.setXPath("self::default:Parent or (parent::default:Parent and not(self::default:Child)) or self::ns1:GrandChild or parent::ns1:GrandChild or self::default:GrandChild or parent::default:GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        tf.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
        xmlSignature.addDocument("iaikTests.example3.xml", tf);
    }
    {
        // ref 6
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:ns1", "http://example.org/ns1");
            xc.setXPath("self::Parent or (parent::Parent and not(self::Child)) or self::ns1:GrandChild or parent::ns1:GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("iaikTests.example4.xml", tf);
    }
    {
        // ref 7
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPathNamespaceContext("xmlns:ns1", "http://example.org/ns1");
            xc.setXPath("self::Parent or (parent::Parent and not(self::Child)) or self::ns1:GrandChild or parent::ns1:GrandChild");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        {
            InclusiveNamespaces incNS = new InclusiveNamespaces(doc, "ns2");
            tf.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS, incNS.getElement());
        }
        xmlSignature.addDocument("iaikTests.example4.xml", tf);
    }
    {
        // ref 8
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object1";
        obj.setId(id);
        String xmlStr = "" + "<included    xml:lang='de'>" + "\n" + "<notIncluded xml:lang='de'>" + "\n" + "<notIncluded xml:lang='uk'>" + "\n" + "<included                 >" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</notIncluded>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        // ref apache_8
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 9
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object2";
        obj.setId(id);
        String xmlStr = "" + "<included    xml:lang='uk'>" + "\n" + "<notIncluded xml:lang='de'>" + "\n" + "<notIncluded xml:lang='uk'>" + "\n" + "<included                 >" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</notIncluded>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        // ref apache_8
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 10
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object3";
        obj.setId(id);
        String xmlStr = "" + "<included    xml:lang='de'>" + "\n" + "<notIncluded xml:lang='de'>" + "\n" + "<notIncluded xml:lang='uk'>" + "\n" + "<included xml:lang='de'>" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</notIncluded>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        // ref apache_8
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 11
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object4";
        obj.setId(id);
        String xmlStr = "" + "<included    xml:lang='de'>" + "\n" + "<included xml:lang='de'>" + "\n" + "<notIncluded xml:lang='uk'>" + "\n" + "<included                 >" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</included>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 12
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object5";
        obj.setId(id);
        String xmlStr = "" + "<included                         xml:lang='de'>" + "\n" + "<included xml:lang='de'>" + "\n" + "<notIncluded xml:space='preserve' xml:lang='uk'>" + "\n" + "<included                 >" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</included>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 13
        ObjectContainer obj = new ObjectContainer(doc);
        String id = "object6";
        obj.setId(id);
        String xmlStr = "" + "<included   xml:space='preserve'  xml:lang='de'>" + "\n" + "<included xml:lang='de'>" + "\n" + "<notIncluded xml:lang='uk'>" + "\n" + "<included>" + "\n" + "</included>" + "\n" + "</notIncluded>" + "\n" + "</included>" + "\n" + "</included>";
        Document importDoc = null;
        try (InputStream is = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8))) {
            importDoc = db.parse(is);
        }
        obj.getElement().appendChild(doc.createTextNode("\n"));
        obj.getElement().appendChild(doc.importNode(importDoc.getDocumentElement(), true));
        obj.getElement().appendChild(doc.createTextNode("\n"));
        xmlSignature.appendObject(obj);
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 13b
        String id = "object6";
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
            tf.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
        }
        xmlSignature.addDocument("#" + id, tf);
    }
    {
        // ref 13c
        String id = "object6";
        Transforms tf = new Transforms(doc);
        {
            XPathContainer xc = new XPathContainer(doc);
            xc.setXPath("self::node()[local-name()='included']");
            tf.addTransform(Transforms.TRANSFORM_XPATH, xc.getElement());
            tf.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
            tf.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS);
        }
        xmlSignature.addDocument("#" + id, tf);
    // xmlSignature.addDocument("#" + id, tf, org.apache.xml.security.algorithms.MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1, "ref13c", null);
    }
    String secretKey = "secret";
    xmlSignature.getKeyInfo().addKeyName("The UTF-8 octets of \"" + secretKey + "\" are used for signing (" + secretKey.length() + " octets)");
    xmlSignature.sign(xmlSignature.createSecretKey(secretKey.getBytes()));
    FileOutputStream fos = new FileOutputStream(signatureFile);
    XMLUtils.outputDOM(doc, fos);
    fos.close();
    int length = xmlSignature.getSignedInfo().getLength();
    for (int i = 0; i < length; i++) {
        String fname = directory + "c14n-" + i + "-apache.xml";
        System.out.println(fname);
        JavaUtils.writeBytesToFilename(fname, xmlSignature.getSignedInfo().getReferencedContentAfterTransformsItem(i).getBytes());
    }
    XMLSignature s = new XMLSignature(doc.getDocumentElement(), signatureFile.toURI().toURL().toString());
    boolean verify = s.checkSignatureValue(s.createSecretKey("secret".getBytes()));
    System.out.println("verify=" + verify);
    System.out.println("");
    XMLUtils.outputDOMc14nWithComments(doc, System.out);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Transforms(org.apache.xml.security.transforms.Transforms) Document(org.w3c.dom.Document) XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLSignature(org.apache.xml.security.signature.XMLSignature) FileOutputStream(java.io.FileOutputStream) InclusiveNamespaces(org.apache.xml.security.transforms.params.InclusiveNamespaces) File(java.io.File) ObjectContainer(org.apache.xml.security.signature.ObjectContainer)

Aggregations

XPathContainer (org.apache.xml.security.transforms.params.XPathContainer)5 Document (org.w3c.dom.Document)4 PrivateKey (java.security.PrivateKey)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 XMLSignature (org.apache.xml.security.signature.XMLSignature)3 Transforms (org.apache.xml.security.transforms.Transforms)3 Element (org.w3c.dom.Element)3 FileInputStream (java.io.FileInputStream)2 KeyStore (java.security.KeyStore)2 PublicKey (java.security.PublicKey)2 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 X509Certificate (java.security.cert.X509Certificate)1 Map (java.util.Map)1