Path: blob/master/src/java.xml.crypto/share/classes/javax/xml/crypto/dsig/XMLSignature.java
41161 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* ===========================================================================27*28* (C) Copyright IBM Corp. 2003 All Rights Reserved.29*30* ===========================================================================31*/32/*33* $Id: XMLSignature.java,v 1.10 2005/05/10 16:03:48 mullan Exp $34*/35package javax.xml.crypto.dsig;3637import javax.xml.crypto.KeySelector;38import javax.xml.crypto.KeySelectorResult;39import javax.xml.crypto.MarshalException;40import javax.xml.crypto.XMLStructure;41import javax.xml.crypto.dsig.keyinfo.KeyInfo;42import java.security.Signature;43import java.util.List;4445/**46* A representation of the XML <code>Signature</code> element as47* defined in the <a href="http://www.w3.org/TR/xmldsig-core/">48* W3C Recommendation for XML-Signature Syntax and Processing</a>.49* This class contains methods for signing and validating XML signatures50* with behavior as defined by the W3C specification. The XML Schema Definition51* is defined as:52* <pre><code>53* <element name="Signature" type="ds:SignatureType"/>54* <complexType name="SignatureType">55* <sequence>56* <element ref="ds:SignedInfo"/>57* <element ref="ds:SignatureValue"/>58* <element ref="ds:KeyInfo" minOccurs="0"/>59* <element ref="ds:Object" minOccurs="0" maxOccurs="unbounded"/>60* </sequence>61* <attribute name="Id" type="ID" use="optional"/>62* </complexType>63* </code></pre>64* <p>65* An <code>XMLSignature</code> instance may be created by invoking one of the66* {@link XMLSignatureFactory#newXMLSignature newXMLSignature} methods of the67* {@link XMLSignatureFactory} class.68*69* <p>If the contents of the underlying document containing the70* <code>XMLSignature</code> are subsequently modified, the behavior is71* undefined.72*73* <p>Note that this class is named <code>XMLSignature</code> rather than74* <code>Signature</code> to avoid naming clashes with the existing75* {@link Signature java.security.Signature} class.76*77* @see XMLSignatureFactory#newXMLSignature(SignedInfo, KeyInfo)78* @see XMLSignatureFactory#newXMLSignature(SignedInfo, KeyInfo, List, String, String)79* @author Joyce L. Leung80* @author Sean Mullan81* @author Erwin van der Koogh82* @author JSR 105 Expert Group83* @since 1.684*/85public interface XMLSignature extends XMLStructure {8687/**88* The XML Namespace URI of the W3C Recommendation for XML-Signature89* Syntax and Processing.90*/91static final String XMLNS = "http://www.w3.org/2000/09/xmldsig#";9293/**94* Validates the signature according to the95* <a href="http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation">96* core validation processing rules</a>. This method validates the97* signature using the existing state, it does not unmarshal and98* reinitialize the contents of the <code>XMLSignature</code> using the99* location information specified in the context.100*101* <p>This method only validates the signature the first time it is102* invoked. On subsequent invocations, it returns a cached result.103*104* @param validateContext the validating context105* @return <code>true</code> if the signature passed core validation,106* otherwise <code>false</code>107* @throws ClassCastException if the type of <code>validateContext</code>108* is not compatible with this <code>XMLSignature</code>109* @throws NullPointerException if <code>validateContext</code> is110* <code>null</code>111* @throws XMLSignatureException if an unexpected error occurs during112* validation that prevented the validation operation from completing113*/114boolean validate(XMLValidateContext validateContext)115throws XMLSignatureException;116117/**118* Returns the key info of this <code>XMLSignature</code>.119*120* @return the key info (may be <code>null</code> if not specified)121*/122KeyInfo getKeyInfo();123124/**125* Returns the signed info of this <code>XMLSignature</code>.126*127* @return the signed info (never <code>null</code>)128*/129SignedInfo getSignedInfo();130131/**132* Returns an {@link java.util.Collections#unmodifiableList unmodifiable133* list} of {@link XMLObject}s contained in this <code>XMLSignature</code>.134*135* @return an unmodifiable list of <code>XMLObject</code>s (may be empty136* but never <code>null</code>)137*/138List<XMLObject> getObjects();139140/**141* Returns the optional Id of this <code>XMLSignature</code>.142*143* @return the Id (may be <code>null</code> if not specified)144*/145String getId();146147/**148* Returns the signature value of this <code>XMLSignature</code>.149*150* @return the signature value151*/152SignatureValue getSignatureValue();153154/**155* Signs this <code>XMLSignature</code>.156*157* <p>If this method throws an exception, this <code>XMLSignature</code> and158* the <code>signContext</code> parameter will be left in the state that159* it was in prior to the invocation.160*161* @param signContext the signing context162* @throws ClassCastException if the type of <code>signContext</code> is163* not compatible with this <code>XMLSignature</code>164* @throws NullPointerException if <code>signContext</code> is165* <code>null</code>166* @throws MarshalException if an exception occurs while marshalling167* @throws XMLSignatureException if an unexpected exception occurs while168* generating the signature169*/170void sign(XMLSignContext signContext) throws MarshalException,171XMLSignatureException;172173/**174* Returns the result of the {@link KeySelector}, if specified, after175* this <code>XMLSignature</code> has been signed or validated.176*177* @return the key selector result, or <code>null</code> if a key178* selector has not been specified or this <code>XMLSignature</code>179* has not been signed or validated180*/181KeySelectorResult getKeySelectorResult();182183/**184* A representation of the XML <code>SignatureValue</code> element as185* defined in the <a href="http://www.w3.org/TR/xmldsig-core/">186* W3C Recommendation for XML-Signature Syntax and Processing</a>.187* The XML Schema Definition is defined as:188* <pre>189* <element name="SignatureValue" type="ds:SignatureValueType"/>190* <complexType name="SignatureValueType">191* <simpleContent>192* <extension base="base64Binary">193* <attribute name="Id" type="ID" use="optional"/>194* </extension>195* </simpleContent>196* </complexType>197* </pre>198*199* @author Sean Mullan200* @author JSR 105 Expert Group201*/202public interface SignatureValue extends XMLStructure {203/**204* Returns the optional <code>Id</code> attribute of this205* <code>SignatureValue</code>, which permits this element to be206* referenced from elsewhere.207*208* @return the <code>Id</code> attribute (may be <code>null</code> if209* not specified)210*/211String getId();212213/**214* Returns the signature value of this <code>SignatureValue</code>.215*216* @return the signature value (may be <code>null</code> if the217* <code>XMLSignature</code> has not been signed yet). Each218* invocation of this method returns a new clone of the array to219* prevent subsequent modification.220*/221byte[] getValue();222223/**224* Validates the signature value. This method performs a225* cryptographic validation of the signature calculated over the226* <code>SignedInfo</code> of the <code>XMLSignature</code>.227*228* <p>This method only validates the signature the first229* time it is invoked. On subsequent invocations, it returns a cached230* result.231*232* @return <code>true</code> if the signature was233* validated successfully; <code>false</code> otherwise234* @param validateContext the validating context235* @throws NullPointerException if <code>validateContext</code> is236* <code>null</code>237* @throws XMLSignatureException if an unexpected exception occurs while238* validating the signature239*/240boolean validate(XMLValidateContext validateContext)241throws XMLSignatureException;242}243}244245246