public final class XMLSignature extends SignatureElementProxy
<ds:Signature>
elements.
This is the main class that deals with creating and verifying signatures.
There are 2 types of constructors for this class. The ones that take a document, baseURI and 1 or more Java Objects. This is mostly used for signing purposes. The other constructor is the one that takes a DOM Element and a baseURI. This is used mostly with for verifying, when you have a SignatureElement. There are a few different types of methods:
Modifier and Type | Field and Description |
---|---|
static String |
ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5
HMAC - NOT Recommended HMAC-MD5
|
static String |
ALGO_ID_MAC_HMAC_RIPEMD160
HMAC - Optional HMAC-RIPEMD160
|
static String |
ALGO_ID_MAC_HMAC_SHA1
MAC - Required HMAC-SHA1
|
static String |
ALGO_ID_MAC_HMAC_SHA224
HMAC - Optional HMAC-SHA2224
|
static String |
ALGO_ID_MAC_HMAC_SHA256
HMAC - Optional HMAC-SHA256
|
static String |
ALGO_ID_MAC_HMAC_SHA384
HMAC - Optional HMAC-SHA284
|
static String |
ALGO_ID_MAC_HMAC_SHA512
HMAC - Optional HMAC-SHA512
|
static String |
ALGO_ID_SIGNATURE_DSA
Signature - Required DSAwithSHA1 (DSS)
|
static String |
ALGO_ID_SIGNATURE_DSA_SHA256
Signature - Optional DSAwithSHA256
|
static String |
ALGO_ID_SIGNATURE_ECDSA_RIPEMD160
Signature - Optional ECDSAwithRIPEMD160
|
static String |
ALGO_ID_SIGNATURE_ECDSA_SHA1
Signature - Optional ECDSAwithSHA1
|
static String |
ALGO_ID_SIGNATURE_ECDSA_SHA224
Signature - Optional ECDSAwithSHA224
|
static String |
ALGO_ID_SIGNATURE_ECDSA_SHA256
Signature - Optional ECDSAwithSHA256
|
static String |
ALGO_ID_SIGNATURE_ECDSA_SHA384
Signature - Optional ECDSAwithSHA384
|
static String |
ALGO_ID_SIGNATURE_ECDSA_SHA512
Signature - Optional ECDSAwithSHA512
|
static String |
ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5
Signature - NOT Recommended RSAwithMD5
|
static String |
ALGO_ID_SIGNATURE_RSA
Signature - Recommended RSAwithSHA1
|
static String |
ALGO_ID_SIGNATURE_RSA_PSS
Signature - Optional RSASSA-PSS
|
static String |
ALGO_ID_SIGNATURE_RSA_RIPEMD160
Signature - Optional RSAwithRIPEMD160
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA1
Signature - Recommended RSAwithSHA1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA1_MGF1
Signature - Optional RSAwithSHA1andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA224
Signature - Optional RSAwithSHA224
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA224_MGF1
Signature - Optional RSAwithSHA224andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA256
Signature - Optional RSAwithSHA256
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA256_MGF1
Signature - Optional RSAwithSHA256andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA3_224_MGF1
Signature - Optional RSAwithSHA3_224andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA3_256_MGF1
Signature - Optional RSAwithSHA3_256andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA3_384_MGF1
Signature - Optional RSAwithSHA3_384andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA3_512_MGF1
Signature - Optional RSAwithSHA3_512andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA384
Signature - Optional RSAwithSHA384
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA384_MGF1
Signature - Optional RSAwithSHA384andMGF1
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA512
Signature - Optional RSAwithSHA512
|
static String |
ALGO_ID_SIGNATURE_RSA_SHA512_MGF1
Signature - Optional RSAwithSHA512andMGF1
|
baseURI
Constructor and Description |
---|
XMLSignature(Document doc,
String baseURI,
Element signatureMethodElem,
Element canonicalizationMethodElem)
Creates a XMLSignature in a Document
|
XMLSignature(Document doc,
String baseURI,
Element signatureMethodElem,
Element canonicalizationMethodElem,
Provider provider) |
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI)
This creates a new
ds:Signature Element and adds an empty
ds:SignedInfo . |
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
int hmacOutputLength)
Constructor XMLSignature
|
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
int hmacOutputLength,
Provider provider)
Constructor XMLSignature
|
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
int hmacOutputLength,
String canonicalizationMethodURI)
Constructor XMLSignature
|
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
int hmacOutputLength,
String canonicalizationMethodURI,
Provider provider,
AlgorithmParameterSpec spec)
Constructor XMLSignature
|
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
Provider provider)
This creates a new
ds:Signature Element and adds an empty
ds:SignedInfo . |
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
String canonicalizationMethodURI)
Constructor XMLSignature
|
XMLSignature(Document doc,
String baseURI,
String signatureMethodURI,
String canonicalizationMethodURI,
Provider provider)
Constructor XMLSignature
|
XMLSignature(Element element,
String baseURI)
This will parse the element and construct the Java Objects.
|
XMLSignature(Element element,
String baseURI,
boolean secureValidation)
This will parse the element and construct the Java Objects.
|
XMLSignature(Element element,
String baseURI,
boolean secureValidation,
Provider provider)
This will parse the element and construct the Java Objects.
|
XMLSignature(Element element,
String baseURI,
Provider provider)
This will parse the element and construct the Java Objects.
|
Modifier and Type | Method and Description |
---|---|
void |
addDocument(String referenceURI)
Add a Reference with just this URI.
|
void |
addDocument(String referenceURI,
Transforms trans)
Adds a Reference with just the URI and the transforms.
|
void |
addDocument(String referenceURI,
Transforms trans,
String digestURI)
This method is a proxy method for the
Manifest.addDocument(java.lang.String, java.lang.String, org.apache.xml.security.transforms.Transforms, java.lang.String, java.lang.String, java.lang.String) method. |
void |
addDocument(String referenceURI,
Transforms trans,
String digestURI,
String referenceId,
String referenceType)
Add a Reference with full parameters to this Signature
|
void |
addKeyInfo(PublicKey pk)
Add this public key to the KeyInfo.
|
void |
addKeyInfo(X509Certificate cert)
Add an X509 Certificate to the KeyInfo.
|
void |
addResourceResolver(ResourceResolverSpi resolver)
Adds a
ResourceResolverSpi to enable the retrieval of resources. |
void |
appendObject(ObjectContainer object)
Appends an Object (not a
java.lang.Object but an Object
element) to the Signature. |
boolean |
checkSignatureValue(Key pk)
Verifies if the signature is valid by redigesting all References,
comparing those against the stored DigestValues and then checking to see
if the Signatures match on the SignedInfo.
|
boolean |
checkSignatureValue(X509Certificate cert)
Extracts the public key from the certificate and verifies if the signature
is valid by re-digesting all References, comparing those against the
stored DigestValues and then checking to see if the Signatures match on
the SignedInfo.
|
SecretKey |
createSecretKey(byte[] secretKeyBytes)
Proxy method for
SignedInfo.createSecretKey(byte[]) . |
String |
getBaseLocalName()
Get the local name of this element
|
String |
getId()
Returns the
Id attribute |
KeyInfo |
getKeyInfo()
Returns the KeyInfo child.
|
ObjectContainer |
getObjectItem(int i)
Returns the
i th ds:Object child of the signature
or null if no such ds:Object element exists. |
int |
getObjectLength()
Returns the number of all
ds:Object elements. |
byte[] |
getSignatureValue()
Returns the octet value of the SignatureValue element.
|
SignedInfo |
getSignedInfo()
Returns the completely parsed
SignedInfo object. |
void |
setFollowNestedManifests(boolean followManifests)
Signal whether Manifest should be automatically validated.
|
void |
setId(String id)
Sets the
Id attribute |
void |
sign(Key signingKey)
Digests all References in the SignedInfo, calculates the signature value
and sets it in the SignatureValue Element.
|
getBaseNamespace
addBase64Element, addBase64Text, addBigIntegerElement, addReturnToSelf, addText, addTextElement, appendOther, appendSelf, appendSelf, createElementForFamily, createElementForFamilyLocal, createText, getBaseURI, getBigIntegerFromChildElement, getBytesFromTextChild, getDefaultPrefix, getDocument, getElement, getElementPlusReturns, getFirstChild, getLocalAttribute, getTextFromChildElement, getTextFromTextChild, length, registerDefaultPrefixes, setDefaultPrefix, setDocument, setElement, setElement, setLocalAttribute, setLocalIdAttribute, setXPathNamespaceContext
public static final String ALGO_ID_MAC_HMAC_SHA1
public static final String ALGO_ID_SIGNATURE_DSA
public static final String ALGO_ID_SIGNATURE_DSA_SHA256
public static final String ALGO_ID_SIGNATURE_RSA
public static final String ALGO_ID_SIGNATURE_RSA_SHA1
public static final String ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5
public static final String ALGO_ID_SIGNATURE_RSA_RIPEMD160
public static final String ALGO_ID_SIGNATURE_RSA_SHA224
public static final String ALGO_ID_SIGNATURE_RSA_SHA256
public static final String ALGO_ID_SIGNATURE_RSA_SHA384
public static final String ALGO_ID_SIGNATURE_RSA_SHA512
public static final String ALGO_ID_SIGNATURE_RSA_SHA1_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA224_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA256_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA384_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA512_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA3_224_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA3_256_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA3_384_MGF1
public static final String ALGO_ID_SIGNATURE_RSA_SHA3_512_MGF1
public static final String ALGO_ID_MAC_HMAC_NOT_RECOMMENDED_MD5
public static final String ALGO_ID_MAC_HMAC_RIPEMD160
public static final String ALGO_ID_MAC_HMAC_SHA224
public static final String ALGO_ID_MAC_HMAC_SHA256
public static final String ALGO_ID_MAC_HMAC_SHA384
public static final String ALGO_ID_MAC_HMAC_SHA512
public static final String ALGO_ID_SIGNATURE_ECDSA_SHA1
public static final String ALGO_ID_SIGNATURE_ECDSA_SHA224
public static final String ALGO_ID_SIGNATURE_ECDSA_SHA256
public static final String ALGO_ID_SIGNATURE_ECDSA_SHA384
public static final String ALGO_ID_SIGNATURE_ECDSA_SHA512
public static final String ALGO_ID_SIGNATURE_ECDSA_RIPEMD160
public static final String ALGO_ID_SIGNATURE_RSA_PSS
public XMLSignature(Document doc, String baseURI, String signatureMethodURI) throws XMLSecurityException
ds:Signature
Element and adds an empty
ds:SignedInfo
.
The ds:SignedInfo
is initialized with the specified Signature
algorithm and Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS which is REQUIRED
by the spec. This method's main use is for creating a new signature.doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- signature algorithm to use.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, Provider provider) throws XMLSecurityException
ds:Signature
Element and adds an empty
ds:SignedInfo
.
The ds:SignedInfo
is initialized with the specified Signature
algorithm and Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS which is REQUIRED
by the spec. This method's main use is for creating a new signature.doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- signature algorithm to use.provider
- security provider to use.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, int hmacOutputLength) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.hmacOutputLength
- XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, int hmacOutputLength, Provider provider) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.hmacOutputLength
- provider
- security provider to use.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, String canonicalizationMethodURI) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.canonicalizationMethodURI
- the canonicalization algorithm to be used to c14nize the SignedInfo element.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, String canonicalizationMethodURI, Provider provider) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.canonicalizationMethodURI
- the canonicalization algorithm to be used to c14nize the SignedInfo element.provider
- security provider to use.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, int hmacOutputLength, String canonicalizationMethodURI) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.hmacOutputLength
- canonicalizationMethodURI
- the canonicalization algorithm to be used to c14nize the SignedInfo element.XMLSecurityException
public XMLSignature(Document doc, String baseURI, String signatureMethodURI, int hmacOutputLength, String canonicalizationMethodURI, Provider provider, AlgorithmParameterSpec spec) throws XMLSecurityException
doc
- Document in which the signature will be appended after creation.baseURI
- URI to be used as context for all relative URIs.signatureMethodURI
- the Signature method to be used.hmacOutputLength
- canonicalizationMethodURI
- the canonicalization algorithm to be used to c14nize the SignedInfo element.provider
- security provider to use.spec
- XMLSecurityException
public XMLSignature(Document doc, String baseURI, Element signatureMethodElem, Element canonicalizationMethodElem) throws XMLSecurityException
doc
- baseURI
- signatureMethodElem
- canonicalizationMethodElem
- XMLSecurityException
public XMLSignature(Document doc, String baseURI, Element signatureMethodElem, Element canonicalizationMethodElem, Provider provider) throws XMLSecurityException
XMLSecurityException
public XMLSignature(Element element, String baseURI) throws XMLSignatureException, XMLSecurityException
element
- ds:Signature element that contains the whole signaturebaseURI
- URI to be prepended to all relative URIsXMLSecurityException
XMLSignatureException
- if the signature is badly formattedpublic XMLSignature(Element element, String baseURI, Provider provider) throws XMLSignatureException, XMLSecurityException
element
- ds:Signature element that contains the whole signaturebaseURI
- URI to be prepended to all relative URIsprovider
- security provider to useXMLSecurityException
XMLSignatureException
- if the signature is badly formattedpublic XMLSignature(Element element, String baseURI, boolean secureValidation) throws XMLSignatureException, XMLSecurityException
element
- ds:Signature element that contains the whole signaturebaseURI
- URI to be prepended to all relative URIssecureValidation
- whether secure secureValidation is enabled or notXMLSecurityException
XMLSignatureException
- if the signature is badly formattedpublic XMLSignature(Element element, String baseURI, boolean secureValidation, Provider provider) throws XMLSignatureException, XMLSecurityException
element
- ds:Signature element that contains the whole signaturebaseURI
- URI to be prepended to all relative URIssecureValidation
- whether secure secureValidation is enabled or notprovider
- security provider to useXMLSecurityException
XMLSignatureException
- if the signature is badly formattedpublic void setId(String id)
Id
attributeid
- Id value for the id attribute on the Signature Elementpublic String getId()
Id
attributeId
attributepublic SignedInfo getSignedInfo()
SignedInfo
object.SignedInfo
object.public byte[] getSignatureValue() throws XMLSignatureException
XMLSignatureException
- If there is no contentpublic KeyInfo getKeyInfo()
public void appendObject(ObjectContainer object) throws XMLSignatureException
java.lang.Object
but an Object
element) to the Signature. Please note that this is only possible
when signing.object
- ds:Object to be appended.XMLSignatureException
- When this object is used to verify.public ObjectContainer getObjectItem(int i)
i
th ds:Object
child of the signature
or null if no such ds:Object
element exists.i
- i
th ds:Object
child of the signature
or null if no such ds:Object
element exists.public int getObjectLength()
ds:Object
elements.ds:Object
elements.public void sign(Key signingKey) throws XMLSignatureException
signingKey
- the PrivateKey
or
SecretKey
that is used to sign.XMLSignatureException
public void addResourceResolver(ResourceResolverSpi resolver)
ResourceResolverSpi
to enable the retrieval of resources.resolver
- public boolean checkSignatureValue(X509Certificate cert) throws XMLSignatureException
cert
- Certificate that contains the public key part of the keypair
that was used to sign.XMLSignatureException
public boolean checkSignatureValue(Key pk) throws XMLSignatureException
pk
- PublicKey
part of the keypair or
SecretKey
that was used to signXMLSignatureException
public void addDocument(String referenceURI, Transforms trans, String digestURI, String referenceId, String referenceType) throws XMLSignatureException
referenceURI
- URI of the resource to be signed. Can be null in
which case the dereferencing is application specific. Can be "" in which
it's the parent node (or parent document?). There can only be one "" in
each signature.trans
- Optional list of transformations to be done before digestingdigestURI
- Mandatory URI of the digesting algorithm to use.referenceId
- Optional id attribute for this ReferencereferenceType
- Optional mimetype for the URIXMLSignatureException
public void addDocument(String referenceURI, Transforms trans, String digestURI) throws XMLSignatureException
Manifest.addDocument(java.lang.String, java.lang.String, org.apache.xml.security.transforms.Transforms, java.lang.String, java.lang.String, java.lang.String)
method.referenceURI
- URI according to the XML Signature specification.trans
- List of transformations to be applied.digestURI
- URI of the digest algorithm to be used.XMLSignatureException
Manifest.addDocument(java.lang.String, java.lang.String, org.apache.xml.security.transforms.Transforms, java.lang.String, java.lang.String, java.lang.String)
public void addDocument(String referenceURI, Transforms trans) throws XMLSignatureException
referenceURI
- URI according to the XML Signature specification.trans
- List of transformations to be applied.XMLSignatureException
public void addDocument(String referenceURI) throws XMLSignatureException
referenceURI
- URI according to the XML Signature specification.XMLSignatureException
public void addKeyInfo(X509Certificate cert) throws XMLSecurityException
cert
- Certificate to be included. This should be the certificate of
the key that was used to sign.XMLSecurityException
public void addKeyInfo(PublicKey pk)
pk
- public SecretKey createSecretKey(byte[] secretKeyBytes)
SignedInfo.createSecretKey(byte[])
. If you want
to create a MAC, this method helps you to obtain the
SecretKey
from octets.secretKeyBytes
- SignedInfo.createSecretKey(byte[])
public void setFollowNestedManifests(boolean followManifests)
followManifests
- public String getBaseLocalName()
getBaseLocalName
in class ElementProxy
Copyright © 2000–2020 The Apache Software Foundation. All rights reserved.