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.getLength(); i++) {
         Reference r = s.item(i);
         String fn = "merlin16_"+i+".html";
         System.out.println("Wrote Reference " + i + " to file " + fn);
         JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
      }

      finally
      {
         STRTransform.setSecurityStore(null);
      }

      SignedInfo info = xmlSig.getSignedInfo();
      int length = info.getLength();
      Collection<String> processed = new ArrayList<String>(length);
      try
      {
         for (int i = 0; i < length; i++)
         {
            String uri = info.item(i).getURI();
            if (uri != null && uri.length() > 1 && uri.charAt(0)=='#')
               processed.add(uri.substring(1));
         }
      }
      catch (XMLSecurityException e)

        /*
         * 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();
        Vector qvec = new Vector(numReferences);
        for (int i = 0; i < numReferences; i++) {
          Reference siRef;
          try {
            siRef = si.item(i);
          } catch (XMLSecurityException e3) {
            throw new WSSecurityException(
                WSSecurityException.FAILED_CHECK);
          }
          String uri = siRef.getURI();

                              .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 = "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);

               .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

            /*
            Transforms transforms4 = new Transforms(doc);
            XPathContainer xpath4 = new XPathContainer(doc);

            xpath4.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
            xpath4.setXPath("\n" + "ancestor-or-self::ds:Signature[@Id='" + id1 + "']" + "\n");
            transforms4.addTransform(Transforms.TRANSFORM_XPATH, xpath4.getElementPlusReturns());
            forthSigner.addDocument("#xpointer(id('firstSigner'))", transforms4, Constants.ALGO_ID_DIGEST_SHA1, null, "ds:Signature");
            */
            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();
         }
      }

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

      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 = "merlin16_"+i+".html";
         System.out.println("Wrote Reference " + i + " to file " + fn);
         JavaUtils.writeBytesToFilename(fn, r.getHTMLRepresentation().getBytes());
      }

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.