FileDocCategorySizeDatePackage
Manifest.javaAPI DocJava SE 6 API20612Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.signature

Manifest

public class Manifest extends SignatureElementProxy
Handles <ds:Manifest> elements.

This element holds the Reference elements

author
$author: $

Fields Summary
static Logger
log
{@link java.util.logging} logging facility
List
_references
Field _references
Element[]
_referencesEl
private boolean[]
verificationResults
Field verificationResults[]
List
_signedContents
Field _signedContents
HashMap
_resolverProperties
Field _resolverProperties
List
_perManifestResolvers
Field _perManifestResolvers
Constructors Summary
public Manifest(Document doc)
Consturts {@link Manifest}

param
doc the {@link Document} in which XMLsignature is placed


                    
      

      super(doc);

      XMLUtils.addReturnToElement(this._constructionElement);

      this._references = new ArrayList();
   
public Manifest(Element element, String BaseURI)
Constructor Manifest

param
element
param
BaseURI
throws
XMLSecurityException


      super(element, BaseURI);

      // check out Reference children
      this._referencesEl = XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
         Constants._TAG_REFERENCE);
      int le = this._referencesEl.length;
      {
         if (le == 0) {

            // At least one Reference must be present. Bad.
            Object exArgs[] = { Constants._TAG_REFERENCE,
                                Constants._TAG_MANIFEST };

            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
                                   I18n.translate("xml.WrongContent", exArgs));
         }
      }

      // create Vector
      this._references = new ArrayList(le);

      for (int i = 0; i < le; i++) {
         this._references.add(null);
      }
   
Methods Summary
public voidaddDocument(java.lang.String BaseURI, java.lang.String referenceURI, com.sun.org.apache.xml.internal.security.transforms.Transforms transforms, java.lang.String digestURI, java.lang.String ReferenceId, java.lang.String ReferenceType)
This addDocument method is used to add a new resource to the signed info. A {@link com.sun.org.apache.xml.internal.security.signature.Reference} is built from the supplied values.

param
BaseURI the URI of the resource where the XML instance was stored
param
referenceURI URI attribute in Reference for specifing where data is
param
transforms com.sun.org.apache.xml.internal.security.signature.Transforms object with an ordered list of transformations to be performed.
param
digestURI The digest algorthim URI to be used.
param
ReferenceId
param
ReferenceType
throws
XMLSignatureException


      if (this._state == MODE_SIGN) {

         // the this._doc is handed implicitly by the this.getOwnerDocument()
         Reference ref = new Reference(this._doc, BaseURI, referenceURI, this,
                                       transforms, digestURI);

         if (ReferenceId != null) {
            ref.setId(ReferenceId);
         }

         if (ReferenceType != null) {
            ref.setType(ReferenceType);
         }

         // add Reference object to our cache vector
         this._references.add(ref);

         // add the Element of the Reference object to the Manifest/SignedInfo
         this._constructionElement.appendChild(ref.getElement());
         XMLUtils.addReturnToElement(this._constructionElement);
      }
   
public voidaddResourceResolver(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver resolver)
Adds Resource Resolver for retrieving resources at specified URI attribute in reference element

param
resolver {@link ResourceResolver} can provide the implemenatin subclass of {@link ResourceResolverSpi} for retrieving resource.


      if (resolver != null) {
         this._perManifestResolvers.add(resolver);
      }
   
public voidaddResourceResolver(com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi resolverSpi)
Adds Resource Resolver for retrieving resources at specified URI attribute in reference element

param
resolverSpi the implemenatin subclass of {@link ResourceResolverSpi} for retrieving resource.


      if (resolverSpi != null) {
         this._perManifestResolvers.add(new ResourceResolver(resolverSpi));
      }
   
public voidgenerateDigestValues()
The calculation of the DigestValues in the References must be after the References are already added to the document and during the signing process. This ensures that all neccesary data is in place.

throws
ReferenceNotInitializedException
throws
XMLSignatureException


      if (this._state == MODE_SIGN) {
         for (int i = 0; i < this.getLength(); i++) {

            // update the cached Reference object, the Element content is automatically updated
            Reference currentRef = (Reference) this._references.get(i);

            currentRef.generateDigestValue();
         }
      }
   
public java.lang.StringgetBaseLocalName()
Method getBaseLocalName

inheritDoc

      return Constants._TAG_MANIFEST;
   
public java.lang.StringgetId()
Returns the Id attribute

return
the Id attribute in ds:Manifest

      return this._constructionElement.getAttributeNS(null, Constants._ATT_ID);
   
public intgetLength()
Return the nonnegative number of added references.

return
the number of references

      return this._references.size();
   
public com.sun.org.apache.xml.internal.security.signature.XMLSignatureInputgetReferencedContentAfterTransformsItem(int i)
Method getReferencedContentAfterTransformsItem

param
i
return
The contents after transformation of the reference i.
throws
XMLSecurityException

      return this.item(i).getContentsAfterTransformation();
   
public com.sun.org.apache.xml.internal.security.signature.XMLSignatureInputgetReferencedContentBeforeTransformsItem(int i)
Method getReferencedContentPriorTransformsItem

param
i
return
The contents before transformation of the reference i.
throws
XMLSecurityException

      return this.item(i).getContentsBeforeTransformation();
   
public java.lang.StringgetResolverProperty(java.lang.String key)
Returns the value at specified key

param
key the key
return
the value

      return (String) this._resolverProperties.get(key);
   
public byte[]getSignedContentItem(int i)
Method getSignedContentItem

param
i
return
The signed content of the i reference.
throws
XMLSignatureException


      try {
         return this.getReferencedContentAfterTransformsItem(i).getBytes();
      } catch (IOException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (CanonicalizationException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (InvalidCanonicalizerException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (XMLSecurityException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
public intgetSignedContentLength()
Method getSignedContentLength

return
The nu,ber of references contained in this reference.

      return this.getLength();
   
public booleangetVerificationResult(int index)
After verifying a {@link Manifest} or a {@link SignedInfo} using the {@link Manifest#verifyReferences()} or {@link SignedInfo#verify()} methods, the individual results can be retrieved with this method.

param
index an index of into a {@link Manifest} or a {@link SignedInfo}
return
the results of reference validation at the specified index
throws
XMLSecurityException


      if ((index < 0) || (index > this.getLength() - 1)) {
         Object exArgs[] = { Integer.toString(index),
                             Integer.toString(this.getLength()) };
         Exception e =
            new IndexOutOfBoundsException(I18n
               .translate("signature.Verification.IndexOutOfBounds", exArgs));

         throw new XMLSecurityException("generic.EmptyMessage", e);
      }

      if (this.verificationResults == null) {
         try {
            this.verifyReferences();
         } catch (Exception ex) {
            throw new XMLSecurityException("generic.EmptyMessage", ex);
         }
      }

      return this.verificationResults[index];
   
public com.sun.org.apache.xml.internal.security.signature.Referenceitem(int i)
Return the ith reference. Valid i values are 0 to {link@ getSize}-1.

param
i Index of the requested {@link Reference}
return
the ith reference
throws
XMLSecurityException


      if (this._state == MODE_SIGN) {

         // we already have real objects
         return (Reference) this._references.get(i);
      } 
         if (this._references.get(i) == null) {

            // not yet constructed, so _we_ have to            
            Reference ref = new Reference(_referencesEl[i], this._baseURI, this);

            this._references.set(i, ref);
         }

         return (Reference) this._references.get(i);
      
   
public voidsetId(java.lang.String Id)
Sets the Id attribute

param
Id the Id attribute in ds:Manifest


      if ((this._state == MODE_SIGN) && (Id != null)) {
         this._constructionElement.setAttributeNS(null, Constants._ATT_ID, Id);
         IdResolver.registerElementById(this._constructionElement, Id);
      }
   
public voidsetResolverProperty(java.lang.String key, java.lang.String value)
Used to pass parameters like proxy servers etc to the ResourceResolver implementation.

param
key the key
param
value the value

      this._resolverProperties.put(key, value);
   
private voidsetVerificationResult(int index, boolean verify)
Method setVerificationResult

param
index
param
verify


      if (this.verificationResults == null) {
         this.verificationResults = new boolean[this.getLength()];
      }

      this.verificationResults[index] = verify;
   
public booleanverifyReferences(boolean followManifests)
Used to do a reference validation of all enclosed references using the {@link Reference#verify} method.

This step loops through all {@link Reference}s and does verify the hash values. If one or more verifications fail, the method returns false. If all verifications are successful, it returns true. The results of the individual reference validations are available by using the {@link #getVerificationResult(int)} method

param
followManifests
return
true if all References verify, false if one or more do not verify.
throws
MissingResourceFailureException if a {@link Reference} does not verify (throws a {@link com.sun.org.apache.xml.internal.security.signature.ReferenceNotInitializedException} because of an uninitialized {@link XMLSignatureInput}
see
com.sun.org.apache.xml.internal.security.signature.Reference#verify
see
com.sun.org.apache.xml.internal.security.signature.SignedInfo#verify(boolean)
see
com.sun.org.apache.xml.internal.security.signature.MissingResourceFailureException
throws
XMLSecurityException

      if (_referencesEl==null) {
        this._referencesEl =  
            XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
                         Constants._TAG_REFERENCE);
      }
   	  if (true) {
   	  	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "verify " +_referencesEl.length + " References");
        if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "I am " + (followManifests
                           ? ""
                           : "not") + " requested to follow nested Manifests");
      }
      boolean verify = true;

      if (_referencesEl.length==0) {
         throw new XMLSecurityException("empty");
      }

      this.verificationResults =
         new boolean[_referencesEl.length];

      for (int i =
              0; i < this._referencesEl.length; i++) {
         Reference currentRef =
            new Reference(_referencesEl[i], this._baseURI, this);

         this._references.set(i, currentRef);

         /* if only one item does not verify, the whole verification fails */
         try {
            boolean currentRefVerified = currentRef.verify();

            this.setVerificationResult(i, currentRefVerified);

            if (!currentRefVerified) {
               verify = false;
            }
            if (true)
            	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "The Reference has Type " + currentRef.getType());

            // was verification successful till now and do we want to verify the Manifest?
            if (verify && followManifests
                    && currentRef.typeIsReferenceToManifest()) {
               if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "We have to follow a nested Manifest");

                try {
                  XMLSignatureInput signedManifestNodes =
                    currentRef.dereferenceURIandPerformTransforms(null);
                  Set nl = signedManifestNodes.getNodeSet();
                  Manifest referencedManifest = null;
                  Iterator nlIterator = nl.iterator();

                  findManifest: while (nlIterator.hasNext()) {
                     Node n = (Node) nlIterator.next();

                     if ((n.getNodeType() == Node.ELEMENT_NODE) && ((Element) n)
                             .getNamespaceURI()
                             .equals(Constants.SignatureSpecNS) && ((Element) n)
                             .getLocalName().equals(Constants._TAG_MANIFEST)) {
                        try {
                           referencedManifest =
                              new Manifest((Element) n,
                                           signedManifestNodes.getSourceURI());

                           break findManifest;
                        } catch (XMLSecurityException ex) {

                           // Hm, seems not to be a ds:Manifest
                        }
                     }
                  }

                  if (referencedManifest == null) {

                     // The Reference stated that it points to a ds:Manifest
                     // but we did not find a ds:Manifest in the signed area
                     throw new MissingResourceFailureException("empty",
                                                               currentRef);
                  }

                  referencedManifest._perManifestResolvers =
                     this._perManifestResolvers;
                  referencedManifest._resolverProperties =
                     this._resolverProperties;

                  boolean referencedManifestValid =
                     referencedManifest.verifyReferences(followManifests);

                  if (!referencedManifestValid) {
                     verify = false;

                     log.log(java.util.logging.Level.WARNING, "The nested Manifest was invalid (bad)");
                  } else {
                     if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "The nested Manifest was valid (good)");
                  }
               } catch (IOException ex) {
                  throw new ReferenceNotInitializedException("empty", ex);
               } catch (ParserConfigurationException ex) {
                  throw new ReferenceNotInitializedException("empty", ex);
               } catch (SAXException ex) {
                  throw new ReferenceNotInitializedException("empty", ex);
               }
            }
         } catch (ReferenceNotInitializedException ex) {
            Object exArgs[] = { currentRef.getURI() };

            throw new MissingResourceFailureException(
               "signature.Verification.Reference.NoInput", exArgs, ex,
               currentRef);
         }
      }

      return verify;
   
public booleanverifyReferences()
Used to do a reference validation of all enclosed references using the {@link Reference#verify} method.

This step loops through all {@link Reference}s and does verify the hash values. If one or more verifications fail, the method returns false. If all verifications are successful, it returns true. The results of the individual reference validations are available by using the {@link #getVerificationResult(int)} method

return
true if all References verify, false if one or more do not verify.
throws
MissingResourceFailureException if a {@link Reference} does not verify (throws a {@link com.sun.org.apache.xml.internal.security.signature.ReferenceNotInitializedException} because of an uninitialized {@link XMLSignatureInput}
see
com.sun.org.apache.xml.internal.security.signature.Reference#verify
see
com.sun.org.apache.xml.internal.security.signature.SignedInfo#verify()
see
com.sun.org.apache.xml.internal.security.signature.MissingResourceFailureException
throws
XMLSecurityException

      return this.verifyReferences(false);