Class XmlDSig
- All Implemented Interfaces:
AutoCloseable
Load, inspect, canonicalize, and verify XML Digital Signatures.
Chilkat.XmlDSig loads and verifies XML Digital Signatures. It
can inspect signed XML, select among multiple signatures in one document,
verify the overall SignatureValue, verify individual
Reference digests, handle external references, extract
KeyInfo, retrieve embedded certificates or public keys,
canonicalize XML, and add an EncapsulatedTimeStamp to an
existing signature.
Load signed XML
Load an XML document containing one or more digital signatures and inspect the signature structure before verification.
Select a signature
Work with documents that contain multiple signatures by selecting the specific signature to inspect or verify.
Verify SignatureValue
Verify the cryptographic signature over the canonicalized
SignedInfo block.
Verify Reference digests
Check individual Reference digest values to confirm that
each signed item still matches what was originally signed.
Certificates and keys
Extract KeyInfo, embedded signing certificates, or public
keys used to validate the signature.
Canonicalization and timestamps
Canonicalize XML for signature analysis and add an
EncapsulatedTimeStamp when timestamping an existing
signature.
Common pattern:
Load the signed XML, select the desired signature if more than one is
present, inspect the signing certificate or public key information, verify
the SignatureValue, then verify the individual reference
digests. Use XmlDSig for verification and inspection; use
Chilkat.XmlDSigGen when creating new XML Digital Signatures.
For an extended overview, see XmlDSig Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEncapsulatedTimeStamp(JsonObject json, StringBuilder sbOut)Adds an EncapsulatedTimeStamp to the signature indicated by the Selector property.canonicalizeFragment(String xml, String fragmentId, String version, String prefixList, boolean withComments)Applies XML canonicalization to a fragment of the passed-in XML, and returns the canonicalized XML string.canonicalizeXml(String xml, String version, boolean withComments)Applies XML canonicalization to the passed-in XML, and returns the canonicalized XML string.voidgetCerts(StringArray sa)Returns the certificates found in the signature indicated by the Selector property.May contain a set of directory paths specifying where referenced external files are located.booleanIftrue, then ignore failures caused by external references not being available.Returns the KeyInfo XML for the signature indicated by the Selector property.intThe number of data objects referenced in the XML digital signature.intThe number of digital signatures found within the loaded XML.Returns the public key from the KeyInfo XML for the signature indicated by the Selector property.intIndicates the failure reason for the last call to VerifyReferenceDigest.intIf the loaded XML contains multiple signatures, this property can be set to specify which signature is in effect when calling other methods or properties.This is a catch-all property to be used for uncommon needs.booleanReturnstrueif the signature indicated by the Selector property contains an EncapsulatedTimeStamp.booleanisReferenceExternal(int index)Returnstrueif the reference atindexis external.voidloadSignature(String xmlSig)Loads an XML document containing 1 or more XML digital signatures.voidloadSignatureBd(BinData binData)Loads an XML document containing one or more XML digital signatures from the contents of a BinData object.voidloadSignatureSb(StringBuilder sbXmlSig)Loads an XML document containing one or more XML digital signatures from the contents of a StringBuilder object.referenceUri(int index)Returns the URI of the Nth reference specified byindex.voidsetExternalRefDirs(String value)Sets theExternalRefDirsproperty; seegetExternalRefDirs().voidsetHmacKey(String key, String encoding)Sets the HMAC key to be used if the Signature used an HMAC signing algorithm.voidsetHttpObj(Http http)Sets the HTTP object to be used to communicate with OCSP responders, CRL distribution points, or timestamp authority (TSA) servers if needed.voidsetIgnoreExternalRefs(boolean value)Sets theIgnoreExternalRefsproperty; seegetIgnoreExternalRefs().voidsetPublicKey(PublicKey pubKey)Sets the public key to be used for verifying the signature indicated by the Selector property.voidsetRefDataBd(int index, BinData binData)Provides the binary data for the external reference atindex.voidsetRefDataFile(int index, String path)Provides the data for the external reference atindex.voidsetRefDataSb(int index, StringBuilder sb, String charset)Provides the text data for the external reference atindex.voidsetSelector(int value)Sets theSelectorproperty; seegetSelector().voidsetUncommonOptions(String value)Sets theUncommonOptionsproperty; seegetUncommonOptions().voiduseCertVault(XmlCertVault certVault)Adds an XML certificate vault to the object's internal list of sources to be searched for certificates having public keys when verifying an XML signature.booleanverifyReferenceDigest(int index)This method allows for an application to verify the digest for each reference separately.voidverifySignature(boolean verifyReferenceDigests)Verifies the signature indicated by the Selector property.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
XmlDSig
public XmlDSig()Creates a newXmlDSigobject.
-
-
Method Details
-
getExternalRefDirs
May contain a set of directory paths specifying where referenced external files are located. Directory paths should be separated using a semicolon character. The default value of this property is the empty string which means no directories are automatically searched.This property can be used if the external file referenced in the XML signature has the same filename as the file in the local filesystem.
- Returns:
- the value of the
ExternalRefDirsproperty
-
setExternalRefDirs
Sets theExternalRefDirsproperty; seegetExternalRefDirs().- Parameters:
value- the new value (notnull)
-
getIgnoreExternalRefs
public boolean getIgnoreExternalRefs()Iftrue, then ignore failures caused by external references not being available. This allows for the XML signature to be at least partially validated if the external referenced files are not available. The default value of this property isfalse.- Returns:
- the value of the
IgnoreExternalRefsproperty
-
setIgnoreExternalRefs
public void setIgnoreExternalRefs(boolean value)Sets theIgnoreExternalRefsproperty; seegetIgnoreExternalRefs().- Parameters:
value- the new value
-
getNumReferences
public int getNumReferences()The number of data objects referenced in the XML digital signature. A data object may be self-contained within the loaded XML signature, or it may be an external URI reference. An application can check each reference to see if it's external by calling IsReferenceExternal, and can get each reference URI by calling ReferenceUri.- Returns:
- the value of the
NumReferencesproperty (read-only)
-
getNumSignatures
public int getNumSignatures()The number of digital signatures found within the loaded XML. Each digital signature is composed of XML having the following structure:_LT_Signature ID?_GT_ _LT_SignedInfo_GT_ _LT_CanonicalizationMethod/_GT_ _LT_SignatureMethod/_GT_ (_LT_Reference URI? _GT_ (_LT_Transforms_GT_)? _LT_DigestMethod_GT_ _LT_DigestValue_GT_ _LT_/Reference_GT_)+ _LT_/SignedInfo_GT_ _LT_SignatureValue_GT_ (_LT_KeyInfo_GT_)? (_LT_Object ID?_GT_)* _LT_/Signature_GT_Note: The
Signatureand other XML tags may be namespace prefixed.The Selector property is used to select which XML signature is in effect when validating or calling other methods or properties.
- Returns:
- the value of the
NumSignaturesproperty (read-only)
-
getRefFailReason
public int getRefFailReason()Indicates the failure reason for the last call to VerifyReferenceDigest. Possible values are:- 0: No failure, the reference digest was valid.
- 1: The computed digest differs from the digest stored in the XML.
- 2: An external file is referenced, but it is unavailable for computing the digest.
- 3: The index argument passed to VerifyReferenceDigest was out of range.
- 4: Unable to find the Signature.
- 5: A transformation specified some sort of XML canonicalization that is not supported.
- 99: Unknown. (Should never get this value.)
- Returns:
- the value of the
RefFailReasonproperty (read-only)
-
getSelector
public int getSelector()If the loaded XML contains multiple signatures, this property can be set to specify which signature is in effect when calling other methods or properties. In most cases, the loaded XML contains a single signature and this property will remain at the default value of 0.- Returns:
- the value of the
Selectorproperty
-
setSelector
public void setSelector(int value)Sets theSelectorproperty; seegetSelector().- Parameters:
value- the new value
-
getUncommonOptions
This is a catch-all property to be used for uncommon needs. This property defaults to the empty string, and should typically remain empty.- Returns:
- the value of the
UncommonOptionsproperty
-
setUncommonOptions
Sets theUncommonOptionsproperty; seegetUncommonOptions().- Parameters:
value- the new value (notnull)
-
addEncapsulatedTimeStamp
Adds an EncapsulatedTimeStamp to the signature indicated by the Selector property. The updated signed XML with the added EncapsulatedTimeStamp is returned insbOut. See the example below for details.- Parameters:
json- thejsonargument (notnull)sbOut- thesbOutargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
canonicalizeFragment
public String canonicalizeFragment(String xml, String fragmentId, String version, String prefixList, boolean withComments)Applies XML canonicalization to a fragment of the passed-in XML, and returns the canonicalized XML string. ThefragmentIdidentifies the XML element where output begins. It is the XML element having anidattribute equal tofragmentId. Theversionmay be one of the following:C14N-- for Inclusive Canonical XMLEXCL_C14N-- for Exclusive Canonical XML
The
prefixListonly applies when theversionis set toEXCL_C14N. TheprefixListmay be an empty string, or a SPACE separated list of namespace prefixes. It is the InclusiveNamespaces PrefixList, which is the list of namespaces that are propagated from ancestor elements for canonicalization purposes.If
withCommentsistrue, then XML comments are included in the output. IfwithCommentsisfalse, then XML comments are excluded from the output.- Parameters:
xml- thexmlargument (notnull)fragmentId- thefragmentIdargument (notnull)version- theversionargument (notnull)prefixList- theprefixListargument (notnull)withComments- thewithCommentsargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
canonicalizeXml
Applies XML canonicalization to the passed-in XML, and returns the canonicalized XML string. Theversionmay be one of the following:C14N-- for Inclusive Canonical XMLEXCL_C14N-- for Exclusive Canonical XML
If
withCommentsistrue, then XML comments are included in the output. IfwithCommentsisfalse, then XML comments are excluded from the output.- Parameters:
xml- thexmlargument (notnull)version- theversionargument (notnull)withComments- thewithCommentsargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getCerts
Returns the certificates found in the signature indicated by the Selector property. The base64 representation of each certificate is returned.- Parameters:
sa- thesaargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getKeyInfo
Returns the KeyInfo XML for the signature indicated by the Selector property. Returnsnullif no KeyInfo exists.- Returns:
- a new
Xmlowned by the caller (nevernull); close it when done - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getPublicKey
Returns the public key from the KeyInfo XML for the signature indicated by the Selector property. Returnsnullif no KeyInfo exists, or if no public key is actually contained in the KeyInfo.- Returns:
- a new
PublicKeyowned by the caller (nevernull); close it when done - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
hasEncapsulatedTimeStamp
public boolean hasEncapsulatedTimeStamp()Returnstrueif the signature indicated by the Selector property contains an EncapsulatedTimeStamp. Otherwise returnsfalse.- Returns:
- the result; this method never throws
ChilkatException
-
isReferenceExternal
public boolean isReferenceExternal(int index)Returnstrueif the reference atindexis external. Each external reference would first need to be provided by the application prior to validating the signature.- Parameters:
index- theindexargument- Returns:
- the result; this method never throws
ChilkatException
-
loadSignature
Loads an XML document containing 1 or more XML digital signatures. An application would first load XML containing digital signature(s), then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.
- Parameters:
xmlSig- thexmlSigargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadSignatureBd
Loads an XML document containing one or more XML digital signatures from the contents of a BinData object. An application would first load the XML, then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.
- Parameters:
binData- thebinDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
loadSignatureSb
Loads an XML document containing one or more XML digital signatures from the contents of a StringBuilder object. An application would first load the XML, then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.
- Parameters:
sbXmlSig- thesbXmlSigargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
referenceUri
Returns the URI of the Nth reference specified byindex. (The 1st reference is at index 0.) URI's beginning with a pound sign ('#') are internal references.- Parameters:
index- theindexargument- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setHmacKey
Sets the HMAC key to be used if the Signature used an HMAC signing algorithm. Theencodingspecifies the encoding ofkey, and can behex,base64,ascii, or any of the binary encodings supported by Chilkat in the link below.- Parameters:
key- thekeyargument (notnull)encoding- theencodingargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setHttpObj
Sets the HTTP object to be used to communicate with OCSP responders, CRL distribution points, or timestamp authority (TSA) servers if needed. Thehttpis used to send the requests, and it allows for connection related settings and timeouts to be set. For example, if HTTP or SOCKS proxies are required, these features can be specified on thehttp.- Parameters:
http- thehttpargument (notnull)
-
setPublicKey
Sets the public key to be used for verifying the signature indicated by the Selector property. A public key only needs to be explicitly provided by the application for those XML signatures where the public key is not already available within the KeyInfo of the Signature. In some cases, the KeyInfo within the Signature contains information about what public key was used for signing. The application can use this information to retrieve the matching public key and provide it via this method.- Parameters:
pubKey- thepubKeyargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setRefDataBd
Provides the binary data for the external reference atindex.- Parameters:
index- theindexargumentbinData- thebinDataargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setRefDataFile
Provides the data for the external reference atindex. When validating the signature, the data contained inpathwill be streamed to compute the digest for validation.- Parameters:
index- theindexargumentpath- thepathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setRefDataSb
Provides the text data for the external reference atindex. Thecharsetspecifies the byte representation of the text, such asutf-8,utf-16,windows-1252, etc. (If in doubt, try utf-8 first.)- Parameters:
index- theindexargumentsb- thesbargument (notnull)charset- thecharsetargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
useCertVault
Adds an XML certificate vault to the object's internal list of sources to be searched for certificates having public keys when verifying an XML signature. A single XML certificate vault may be used. If UseCertVault is called multiple times, only the last certificate vault will be used, as each call to UseCertVault will replace the certificate vault provided in previous calls.- Parameters:
certVault- thecertVaultargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
verifyReferenceDigest
public boolean verifyReferenceDigest(int index)This method allows for an application to verify the digest for each reference separately. This can be helpful if the full XMLDSIG validation fails, then one can test each referenced data's digest to see which, if any, fail to match.- Parameters:
index- theindexargument- Returns:
- the result; this method never throws
ChilkatException
-
verifySignature
public void verifySignature(boolean verifyReferenceDigests)Verifies the signature indicated by the Selector property. IfverifyReferenceDigestsistrue, then the digest of each Reference within the signature's SignedInfo is also validated.- Parameters:
verifyReferenceDigests- theverifyReferenceDigestsargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-