reference.java
来自「JAVA 所有包」· Java 代码 · 共 751 行 · 第 1/2 页
JAVA
751 行
/* * 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.io.OutputStream;import java.util.HashSet;import java.util.Set;import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm;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.Base64DecodingException;import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;import com.sun.org.apache.xml.internal.security.transforms.InvalidTransformException;import com.sun.org.apache.xml.internal.security.transforms.Transform;import com.sun.org.apache.xml.internal.security.transforms.TransformationException;import com.sun.org.apache.xml.internal.security.transforms.Transforms;import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;import com.sun.org.apache.xml.internal.security.utils.Base64;import com.sun.org.apache.xml.internal.security.utils.Constants;import com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream;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.UnsyncBufferedOutputStream;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.ResourceResolverException;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.Text;/** * Handles <code><ds:Reference></code> elements. * * This includes: * * Constuct a <CODE>ds:Reference</CODE> from an {@link org.w3c.dom.Element}. * * <p>Create a new reference</p> * <pre> * Document _doc; * MessageDigestAlgorithm sha1 = MessageDigestAlgorithm.getInstance("http://#sha1"); * Reference ref = new Reference(new XMLSignatureInput(new FileInputStream("1.gif"), * "http://localhost/1.gif", * (Transforms) null, sha1); * Element refElem = ref.toElement(_doc); * </pre> * * <p>Verify a reference</p> * <pre> * Element refElem = _doc.getElement("Reference"); // PSEUDO * Reference ref = new Reference(refElem); * String url = ref.getURI(); * ref.setData(new XMLSignatureInput(new FileInputStream(url))); * if (ref.verify()) { * System.out.println("verified"); * } * </pre> * * <pre> * <element name="Reference" type="ds:ReferenceType"/> * <complexType name="ReferenceType"> * <sequence> * <element ref="ds:Transforms" minOccurs="0"/> * <element ref="ds:DigestMethod"/> * <element ref="ds:DigestValue"/> * </sequence> * <attribute name="Id" type="ID" use="optional"/> * <attribute name="URI" type="anyURI" use="optional"/> * <attribute name="Type" type="anyURI" use="optional"/> * </complexType> * </pre> * * @author Christian Geuer-Pollmann * @see ObjectContainer * @see Manifest */public class Reference extends SignatureElementProxy { /** {@link java.util.logging} logging facility */ static java.util.logging.Logger log = java.util.logging.Logger.getLogger(Reference.class.getName()); /** Field OBJECT_URI */ public static final String OBJECT_URI = Constants.SignatureSpecNS + Constants._TAG_OBJECT; /** Field MANIFEST_URI */ public static final String MANIFEST_URI = Constants.SignatureSpecNS + Constants._TAG_MANIFEST; //J- Manifest _manifest = null; XMLSignatureInput _transformsOutput; //J+ /** * Constructor Reference * * @param doc the {@link Document} in which <code>XMLsignature</code> is placed * @param BaseURI the URI of the resource where the XML instance will be stored * @param ReferenceURI URI indicate where is data which will digested * @param manifest * @param transforms {@link Transforms} applied to data * @param messageDigestAlgorithm {@link MessageDigestAlgorithm Digest algorithm} which is applied to the data * TODO should we throw XMLSignatureException if MessageDigestAlgoURI is wrong? * @throws XMLSignatureException */ protected Reference(Document doc, String BaseURI, String ReferenceURI, Manifest manifest, Transforms transforms, String messageDigestAlgorithm) throws XMLSignatureException { super(doc); XMLUtils.addReturnToElement(this._constructionElement); this._baseURI = BaseURI; this._manifest = manifest; this.setURI(ReferenceURI); // important: The ds:Reference must be added to the associated ds:Manifest // or ds:SignedInfo _before_ the this.resolverResult() is called. // this._manifest.appendChild(this._constructionElement); // this._manifest.appendChild(this._doc.createTextNode("\n")); if (transforms != null) { this._constructionElement.appendChild(transforms.getElement()); XMLUtils.addReturnToElement(this._constructionElement); } { MessageDigestAlgorithm mda = MessageDigestAlgorithm.getInstance(this._doc, messageDigestAlgorithm); this._constructionElement.appendChild(mda.getElement()); XMLUtils.addReturnToElement(this._constructionElement); } { Element digestValueElement = XMLUtils.createElementInSignatureSpace(this._doc, Constants._TAG_DIGESTVALUE); this._constructionElement.appendChild(digestValueElement); XMLUtils.addReturnToElement(this._constructionElement); } } /** * Build a {@link Reference} from an {@link Element} * * @param element <code>Reference</code> element * @param BaseURI the URI of the resource where the XML instance was stored * @param manifest is the {@link Manifest} of {@link SignedInfo} in which the Reference occurs. We need this because the Manifest has the individual {@link ResourceResolver}s whcih have been set by the user * @throws XMLSecurityException */ protected Reference(Element element, String BaseURI, Manifest manifest) throws XMLSecurityException { super(element, BaseURI); this._manifest = manifest; } /** * Returns {@link MessageDigestAlgorithm} * * * @return {@link MessageDigestAlgorithm} * * @throws XMLSignatureException */ public MessageDigestAlgorithm getMessageDigestAlgorithm() throws XMLSignatureException { Element digestMethodElem = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), Constants._TAG_DIGESTMETHOD,0); if (digestMethodElem == null) { return null; } String uri = digestMethodElem.getAttributeNS(null, Constants._ATT_ALGORITHM); if (uri == null) { return null; } return MessageDigestAlgorithm.getInstance(this._doc, uri); } /** * Sets the <code>URI</code> of this <code>Reference</code> element * * @param URI the <code>URI</code> of this <code>Reference</code> element */ public void setURI(String URI) { if ((this._state == MODE_SIGN) && (URI != null)) { this._constructionElement.setAttributeNS(null, Constants._ATT_URI, URI); } } /** * Returns the <code>URI</code> of this <code>Reference</code> element * * @return URI the <code>URI</code> of this <code>Reference</code> element */ public String getURI() { return this._constructionElement.getAttributeNS(null, Constants._ATT_URI); } /** * Sets the <code>Id</code> attribute of this <code>Reference</code> element * * @param Id the <code>Id</code> attribute of this <code>Reference</code> element */ 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 of this <code>Reference</code> element * * @return Id the <code>Id</code> attribute of this <code>Reference</code> element */ public String getId() { return this._constructionElement.getAttributeNS(null, Constants._ATT_ID); } /** * Sets the <code>type</code> atttibute of the Reference indicate whether an <code>ds:Object</code>, <code>ds:SignatureProperty</code>, or <code>ds:Manifest</code> element * * @param Type the <code>type</code> attribute of the Reference */ public void setType(String Type) { if ((this._state == MODE_SIGN) && (Type != null)) { this._constructionElement.setAttributeNS(null, Constants._ATT_TYPE, Type); } } /** * Return the <code>type</code> atttibute of the Reference indicate whether an <code>ds:Object</code>, <code>ds:SignatureProperty</code>, or <code>ds:Manifest</code> element * * @return the <code>type</code> attribute of the Reference */ public String getType() { return this._constructionElement.getAttributeNS(null, Constants._ATT_TYPE); } /** * Method isReferenceToObject * * This returns true if the <CODE>Type</CODE> attribute of the * <CODE>Refernce</CODE> element points to a <CODE>#Object</CODE> element * * @return true if the Reference type indicates that this Reference points to an <code>Object</code> */ public boolean typeIsReferenceToObject() { if ((this.getType() != null) && this.getType().equals(Reference.OBJECT_URI)) { return true; } return false; } /** * Method isReferenceToManifest * * This returns true if the <CODE>Type</CODE> attribute of the * <CODE>Refernce</CODE> element points to a <CODE>#Manifest</CODE> element * * @return true if the Reference type indicates that this Reference points to a {@link Manifest} */ public boolean typeIsReferenceToManifest() { if ((this.getType() != null) && this.getType().equals(Reference.MANIFEST_URI)) { return true; } return false; } /** * Method setDigestValueElement * * @param digestValue */ private void setDigestValueElement(byte[] digestValue) { if (this._state == MODE_SIGN) { Element digestValueElement =XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), Constants._TAG_DIGESTVALUE,0); Node n=digestValueElement.getFirstChild(); while (n!=null) { digestValueElement.removeChild(n); n = n.getNextSibling(); } String base64codedValue = Base64.encode(digestValue); Text t = this._doc.createTextNode(base64codedValue); digestValueElement.appendChild(t); } } /** * Method generateDigestValue * * @throws ReferenceNotInitializedException * @throws XMLSignatureException */ public void generateDigestValue() throws XMLSignatureException, ReferenceNotInitializedException { if (this._state == MODE_SIGN) { this.setDigestValueElement(this.calculateDigest()); } } /** * Returns the XMLSignatureInput which is created by de-referencing the URI attribute. * @return the XMLSignatureInput of the source of this reference * @throws ReferenceNotInitializedException If the resolver found any * problem resolving the reference */ public XMLSignatureInput getContentsBeforeTransformation() throws ReferenceNotInitializedException { try { Attr URIAttr = this._constructionElement.getAttributeNodeNS(null, Constants._ATT_URI); String URI; if (URIAttr == null) { URI = null; } else { URI = URIAttr.getNodeValue(); } ResourceResolver resolver = ResourceResolver.getInstance(URIAttr, this._baseURI, this._manifest._perManifestResolvers);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?