Package org.apache.xml.security.signature

Examples of org.apache.xml.security.signature.SignedInfo


        return result;
    }
   
    private void checkReferences(XMLSignature xmlSignature) throws Exception {
        SignedInfo signedInfo = xmlSignature.getSignedInfo();
        assertTrue(signedInfo.getLength() > 0);
        for (int i = 0; i < signedInfo.getLength(); i++) {
            Reference reference = signedInfo.item(i);
            assertNotNull(reference);
            ReferenceData referenceData = reference.getReferenceData();
            assertNotNull(referenceData);
           
            if (referenceData instanceof ReferenceNodeSetData) {


        XMLUtils.outputDOMc14nWithComments(doc, f);
        f.close();
        System.out.println("Wrote signature to " + BaseURI);

        SignedInfo s = sig.getSignedInfo();

        for (int i = 0; i < s.getSignedContentLength(); i++) {
            System.out.println("################ Signed Resource " + i
                               + " ################");
            System.out.println(new String(s.getSignedContentItem(i)));
            System.out.println();
        }
    }

        XMLUtils.outputDOMc14nWithComments(doc, f);
        f.close();
        System.out.println("Wrote signature to " + BaseURI);

        SignedInfo s = sig.getSignedInfo();
        for (int i = 0; i < s.getLength(); i++) {
            Reference r = s.item(i);
            String fn = "build/merlin16_"+i+".html";
            System.out.println("Wrote Reference " + i + " to file " + fn);
            JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
        }

            System.out.println("Start signing");
            sig.sign(privateKey);
            System.out.println("Finished signing");
        }

        SignedInfo s = sig.getSignedInfo();
        for (int i = 0; i < s.getSignedContentLength(); i++) {
            System.out.println(new String(s.getSignedContentItem(i)));
        }

        FileOutputStream f = new FileOutputStream(signatureFile);

        XMLUtils.outputDOMc14nWithComments(doc, f);

        PublicKey pubKey = getPublicKey();
        assertTrue(signature.checkSignatureValue(pubKey));
       
        // Check the reference(s)
        SignedInfo signedInfo = signature.getSignedInfo();
        assertTrue(signedInfo.getLength() == 1);
        Reference reference = signedInfo.item(0);
        ReferenceData referenceData = reference.getReferenceData();
        assertNotNull(referenceData);
        assertTrue(referenceData instanceof ReferenceNodeSetData);
       
        // Test the cached Element

                        signature.createSecretKey(
                            CreateCollectableSignature.passphrase.getBytes()
                        )
                    ) ? "valid (good)" : "invalid !!!!! (bad)"));

                SignedInfo s = signature.getSignedInfo();

                for (int j = 0; j < s.getSignedContentLength(); j++) {
                    System.out.println("################ Signed Resource " + i + "/" + j
                                       + " ################");
                    System.out.println(new String(s.getSignedContentItem(j)));
                    System.out.println();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        XMLUtils.outputDOMc14nWithComments(doc, f);
        f.close();
        System.out.println("Wrote signature to " + BaseURI);

        SignedInfo s = sig.getSignedInfo();
        for (int i = 0; i < s.getLength(); i++) {
            Reference r = s.item(i);
            String fn = "build/merlin16_"+i+".html";
            System.out.println("Wrote Reference " + i + " to file " + fn);
            JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
        }

        Reference ref = null;
        try {
            XMLSignature signature = new XMLSignature(signatureElement, "", true);
           
            if (sigProps != null) {
                SignedInfo sInfo = signature.getSignedInfo();
                if (sigProps.getSignatureAlgo() != null
                    && !sigProps.getSignatureAlgo().equals(sInfo.getSignatureMethodURI())) {
                    throwFault("Signature Algorithm is not supported", null);
                }
                if (sigProps.getSignatureC14Method() != null
                    && !sigProps.getSignatureC14Method().equals(sInfo.getCanonicalizationMethodURI())) {
                    throwFault("Signature Algorithm is not supported", null);
                }
            }
           
            ref = getReference(signature);

                //
                // Now dig into the Signature element to get the elements that
                // this Signature covers. Build the QName of these Elements and
                // return them to caller
                //
                SignedInfo si = sig.getSignedInfo();
                int numReferences = si.getLength();
                for (int i = 0; i < numReferences; i++) {
                    Reference siRef;
                    try {
                        siRef = si.item(i);
                    } catch (XMLSecurityException e3) {
                        throw new WSSecurityException(
                            WSSecurityException.FAILED_CHECK, null, null, e3
                        );
                    }

                firstSigner.sign(
                    firstSigner.createSecretKey("First signer key".getBytes()));
                System.out.println("First signer: Finished signing");
            }

            SignedInfo s = firstSigner.getSignedInfo();

            for (int i = 0; i < s.getSignedContentLength(); i++) {
                System.out.println("################ Signed Resource " + i
                                   + " ################");
                System.out.println(new String(s.getSignedContentItem(i)));
                System.out.println();
            }
        }

        //////////////////////////////////////////////////////////////////
        // second signer /////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////
        {
            XMLSignature secondSigner =
                new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_MAC_HMAC_SHA1);

            secondSigner.setId(id2);
            contract.appendChild(secondSigner.getElement());

            Transforms transforms2 = new Transforms(doc);
            XPathContainer xpath2 = new XPathContainer(doc);

            xpath2.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
            xpath2.setXPath("\n" + xp2 + "\n");
            transforms2.addTransform(Transforms.TRANSFORM_XPATH,
                                     xpath2.getElementPlusReturns());
            secondSigner.addDocument("", transforms2, Constants.ALGO_ID_DIGEST_SHA1);

            {
                secondSigner.getKeyInfo().add(new KeyName(doc,
                "Second signer key"));
                System.out.println("Second signer: Start signing");
                secondSigner.sign(
                    secondSigner.createSecretKey("Second signer key".getBytes()));
                System.out.println("Second signer: Finished signing");
            }

            SignedInfo s2 = secondSigner.getSignedInfo();

            for (int i = 0; i < s2.getSignedContentLength(); i++) {
                System.out.println("################ Signed Resource " + i
                                   + " ################");
                System.out.println(new String(s2.getSignedContentItem(i)));
                System.out.println();
            }
        }

        //////////////////////////////////////////////////////////////////
        // third signer //////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////
        {
            XMLSignature thirdSigner =
                new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_MAC_HMAC_SHA1);

            thirdSigner.setId(id3);
            contract.appendChild(thirdSigner.getElement());

            Transforms transforms3 = new Transforms(doc);
            XPathContainer xpath3 = new XPathContainer(doc);

            xpath3.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
            xpath3.setXPath("\n" + xp3 + "\n");
            transforms3.addTransform(Transforms.TRANSFORM_XPATH,
                                     xpath3.getElementPlusReturns());
            thirdSigner.addDocument("", transforms3, Constants.ALGO_ID_DIGEST_SHA1);

            {
                thirdSigner.getKeyInfo().add(new KeyName(doc, "Third signer key"));
                System.out.println("Third signer: Start signing");
                thirdSigner.sign(
                    thirdSigner.createSecretKey("Third signer key".getBytes()));
                System.out.println("Third signer: Finished signing");
            }

            SignedInfo s3 = thirdSigner.getSignedInfo();

            for (int i = 0; i < s3.getSignedContentLength(); i++) {
                System.out.println("################ Signed Resource " + i
                                   + " ################");
                System.out.println(new String(s3.getSignedContentItem(i)));
                System.out.println();
            }
        }

        //////////////////////////////////////////////////////////////////
        // forth signer //////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////
        {
            XMLSignature forthSigner =
                new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_MAC_HMAC_SHA1);

            forthSigner.setId("sig4");
            contract.appendChild(forthSigner.getElement());

            {
                // first of all, add the basic document without signatures
                Transforms transforms4 = new Transforms(doc);
                XPathContainer xpath4 = new XPathContainer(doc);

                xpath4.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
                xpath4.setXPath("\n" + "not(ancestor-or-self::ds:Signature)"
                                + "\n");
                transforms4.addTransform(Transforms.TRANSFORM_XPATH,
                                         xpath4.getElementPlusReturns());
                forthSigner.addDocument("", transforms4,
                                        Constants.ALGO_ID_DIGEST_SHA1);
            }

            {
                // then add the different signatures
                forthSigner.addDocument("#xpointer(id('firstSigner'))", null,
                                        Constants.ALGO_ID_DIGEST_SHA1, null,
                "ds:Signature");
            }

            {
                forthSigner.getKeyInfo().add(new KeyName(doc, "Forth signer key"));
                System.out.println("Forth signer: Start signing");
                forthSigner.sign(
                    forthSigner.createSecretKey("Forth signer key".getBytes()));
                System.out.println("Forth signer: Finished signing");
            }

            SignedInfo s4 = forthSigner.getSignedInfo();

            for (int i = 0; i < s4.getSignedContentLength(); i++) {
                System.out.println("################ Signed Resource " + i
                                   + " ################");
                System.out.println(new String(s4.getSignedContentItem(i)));
                System.out.println();
            }
        }

        //////////////////////////////////////////////////////////////////

TOP

Related Classes of org.apache.xml.security.signature.SignedInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.