manifest.java
来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 560 行 · 第 1/2 页
JAVA
560 行
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.sun.org.apache.xml.internal.security.signature;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Set;import javax.xml.parsers.ParserConfigurationException;import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;import com.sun.org.apache.xml.internal.security.transforms.Transforms;import com.sun.org.apache.xml.internal.security.utils.Constants;import com.sun.org.apache.xml.internal.security.utils.I18n;import com.sun.org.apache.xml.internal.security.utils.IdResolver;import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;import com.sun.org.apache.xml.internal.security.utils.XMLUtils;import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver;import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.SAXException;/** * Handles <code><ds:Manifest></code> elements. * <p> This element holds the <code>Reference</code> elements</p> * @author $author: $ */public class Manifest extends SignatureElementProxy { /** {@link java.util.logging} logging facility */ static java.util.logging.Logger log = java.util.logging.Logger.getLogger(Manifest.class.getName()); /** Field _references */ List _references; Element[] _referencesEl; /** Field verificationResults[] */ private boolean verificationResults[] = null; /** Field _signedContents */ List _signedContents = new ArrayList(); /** Field _resolverProperties */ HashMap _resolverProperties = new HashMap(10); /** Field _perManifestResolvers */ List _perManifestResolvers = new ArrayList(); /** * Consturts {@link Manifest} * * @param doc the {@link Document} in which <code>XMLsignature</code> is placed */ public Manifest(Document doc) { super(doc); XMLUtils.addReturnToElement(this._constructionElement); this._references = new ArrayList(); } /** * Constructor Manifest * * @param element * @param BaseURI * @throws XMLSecurityException */ public Manifest(Element element, String 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); } } /** * This <code>addDocument</code> 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 <code>URI</code> attribute in <code>Reference</code> 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 */ public void addDocument( String BaseURI, String referenceURI, Transforms transforms, String digestURI, String ReferenceId, String 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); } } /** * 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 */ public void generateDigestValues() throws XMLSignatureException, ReferenceNotInitializedException { 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(); } } } /** * Return the nonnegative number of added references. * * @return the number of references */ public int getLength() { return this._references.size(); } /** * Return the <it>i</it><sup>th</sup> reference. Valid <code>i</code> * values are 0 to <code>{link@ getSize}-1</code>. * * @param i Index of the requested {@link Reference} * @return the <it>i</it><sup>th</sup> reference * @throws XMLSecurityException */ public Reference item(int i) 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); } /** * Sets the <code>Id</code> attribute * * @param Id the <code>Id</code> attribute in <code>ds:Manifest</code> */ public void setId(String Id) { if ((this._state == MODE_SIGN) && (Id != null)) { this._constructionElement.setAttributeNS(null, Constants._ATT_ID, Id); IdResolver.registerElementById(this._constructionElement, Id); } } /** * Returns the <code>Id</code> attribute * * @return the <code>Id</code> attribute in <code>ds:Manifest</code> */ public String getId() { return this._constructionElement.getAttributeNS(null, Constants._ATT_ID); } /** * Used to do a <A HREF="http://www.w3.org/TR/xmldsig-core/#def-ValidationReference">reference * validation</A> of all enclosed references using the {@link Reference#verify} method. * * <p>This step loops through all {@link Reference}s and does verify the hash * values. If one or more verifications fail, the method returns * <code>false</code>. If <i>all</i> verifications are successful, * it returns <code>true</code>. 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 */ public boolean verifyReferences() throws MissingResourceFailureException, XMLSecurityException { return this.verifyReferences(false); } /** * Used to do a <A HREF="http://www.w3.org/TR/xmldsig-core/#def-ValidationReference">reference * validation</A> of all enclosed references using the {@link Reference#verify} method. * * <p>This step loops through all {@link Reference}s and does verify the hash * values. If one or more verifications fail, the method returns * <code>false</code>. If <i>all</i> verifications are successful, * it returns <code>true</code>. 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}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?