Package eu.europa.esig.dss.xades
Class DSSXMLUtils
java.lang.Object
eu.europa.esig.dss.xades.DSSXMLUtils
Utility class that contains some XML related method.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
This is the default canonicalization method used for production of signatures within DSS framework.static final String
This is the default canonicalization method for XMLDSIG used for signatures and timestamps (see XMLDSIG 4.4.3.2) when one is not defined.static final String
The SPDocDigestAsInSpecification transform algorithm URI for a custom SignaturePolicy processing -
Method Summary
Modifier and TypeMethodDescriptionstatic Node
alignChildrenIndents
(Node parentNode) Aligns indents for all children of the given nodestatic byte[]
applyTransforms
(DSSDocument document, List<DSSTransform> transforms) Applies transforms on document content and returns the byte array to be used for a digest computation NOTE: returns the original document binaries, if the list oftransforms
is empty.static byte[]
applyTransforms
(Node node, List<DSSTransform> transforms) Applies transforms on the node and returns the byte array to be used for a digest computation NOTE: returns the original node binaries, if the list oftransforms
is emptystatic boolean
canCanonicalize
(String canonicalizationMethod) This method says if the framework can canonicalize an XML data with the provided method.static byte[]
canonicalize
(String canonicalizationMethod, byte[] toCanonicalizeBytes) This method canonicalizes the given array of bytes using thecanonicalizationMethod
parameter.static byte[]
canonicalizeSubtree
(String canonicalizationMethod, Node node) This method canonicalizes the givenNode
.static XAdESSignature
createCounterSignature
(Element counterSignatureElement, XAdESSignature masterSignature) Creates and returns a counter signature found in thecounterSignatureElement
static List<org.apache.xml.security.signature.Reference>
extractReferences
(org.apache.xml.security.signature.Manifest manifest) Extracts a list ofReference
s from the givenManifest
object NOTE: can be used also for aSignedInfo
elementstatic NodeList
getAllSignaturesExceptCounterSignatures
(Node documentNode) Returns a NodeList of all "ds:Signature" elements found in thedocumentNode
static String
getAttribute
(Node node, String attributeName) Returns attribute value for the given attribute name if exist, otherwise returns NULLstatic String
getCanonicalizationMethod
(String canonicalizationMethod) Returns thecanonicalizationMethod
if provided, otherwise returns the DEFAULT_CANONICALIZATION_METHODstatic Digest
getDigestAndValue
(Element element) This method extracts the Digest algorithm and value from an element of type DigestAlgAndValueTypestatic Document
getDocWithIndentedSignature
(Document documentDom, String signatureId, List<String> noIndentObjectIds) Pretty prints a signature in the given documentstatic String
getIDIdentifier
(Node node) If this method finds an attribute with the name ID (case-insensitive) then it is returned.static Node
getIndentedNode
(Node documentDom, Node xmlNode) Returns an indented xmlNodestatic PublicKey
getKeyInfoSigningCertificatePublicKey
(Element signatureElement) Extracts signing certificate's public key from KeyInfo element of a given signature if present NOTE: can return null (the value is optional)static byte[]
getNodeBytes
(Node node) Returns bytes of the givennode
static Digest
getReferenceDigest
(org.apache.xml.security.signature.Reference reference) Returns theDigest
extracted from the providedreference
static List<DigestAlgorithm>
getReferenceDigestAlgos
(Element referenceContainer) Returns a list ofDigestAlgorithm
for all references containing inside the providedreferenceContainer
static NodeList
getReferenceNodeList
(Node signatureElement) Returns a NodeList of "ds:Reference" elementsstatic byte[]
getReferenceOriginalContentBytes
(org.apache.xml.security.signature.Reference reference) Returns bytes of the original referenced datastatic ReferenceOutputType
getReferenceOutputType
(DSSReference reference) Returns the expected dereferencing output for the providedDSSReference
static ReferenceOutputType
getReferenceOutputType
(org.apache.xml.security.signature.Reference reference) Returns the expected dereferencing output for the providedReference
getReferenceTypes
(Element referenceContainer) Returns a list of reference typesstatic String
getReferenceURI
(org.apache.xml.security.signature.Reference reference) This method retrieves a URI attribute value of the given reference, when applicable NOTE: Method is used due to Apache Santuario Signature returning an empty string instead of null result.static void
incorporateDigestMethod
(Element parentElement, DigestAlgorithm digestAlgorithm, DSSNamespace namespace) This method creates the ds:DigestMethod DOM objectstatic void
incorporateDigestValue
(Element parentDom, String base64EncodedDigestBytes, DSSNamespace namespace) This method creates the ds:DigestValue DOM object.static void
incorporateTransforms
(Element parentElement, List<DSSTransform> transforms, DSSNamespace namespace) Incorporates a ds:Transforms element into the given parentelement
static Node
indentAndExtend
(Document document, Node newNode, Node oldNode) Extends the given oldNode by appending new indented childs from the given newNodestatic Node
indentAndReplace
(Document document, Node node) Indents the given node and replaces it with a new one on the documentstatic boolean
isAbleToDeReferenceContent
(org.apache.xml.security.signature.Reference reference) Checks if the original reference document content can be obtained (de-referenced)static boolean
isCounterSignature
(org.apache.xml.security.signature.Reference reference, XAdESPaths xadesPaths) Determines if the givenreference
refers to CounterSignature elementstatic boolean
isCounterSignatureReferenceType
(String referenceType) Checks if the givenreferenceType
is an etsi Countersignature typestatic boolean
This method is used to detect duplicate id valuesstatic boolean
isKeyInfoReference
(org.apache.xml.security.signature.Reference reference, Element signature) Checks if the given reference is linked to a KeyInfo elementstatic boolean
isManifestReferenceType
(String referenceType) Checks if the givenreferenceType
is an xmldsig Manifest typestatic boolean
isObjectReferenceType
(String referenceType) Checks if the givenreferenceType
is an xmldsig Object typestatic boolean
isReferencedContentAmbiguous
(Document document, String uri) Checks if the reference with theuri
occurs multiple times in thedocument
static boolean
isSameDocumentReference
(String referenceUri) XMLDSIG 4.4.3.2 The Reference Processing Model A 'same-document' reference is defined as a URI-Reference that consists of a hash sign ('#') followed by a fragment or alternatively consists of an empty URIstatic boolean
isSignaturePropertiesReference
(org.apache.xml.security.signature.Reference reference, Element signature) Checks if the given reference is linked to a SignatureProperties element or one of its SignatureProperty childrenstatic boolean
isSignedProperties
(org.apache.xml.security.signature.Reference reference, XAdESPaths xadesPaths) Determines if the givenreference
refers to SignedProperties elementstatic void
recursiveIdBrowse
(Element element) An ID attribute can only be dereferenced if it is declared in the validation context.static boolean
registerCanonicalizer
(String c14nAlgorithmURI) This method allows to register a canonicalizer.static boolean
registerTransform
(String transformURI) This method allows to register a transformation.static boolean
registerTransformWithNodeSetOutput
(String transformURI) This method allows to register a transformation resulting to a node-set output.static byte[]
serializeNode
(Node xmlNode) This method performs the serialization of the given nodestatic void
setIDIdentifier
(Element childElement) If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute.validateAgainstXSD
(XSDAbstractUtils xsdUtils, Source source) This method allows to validate an XML against the XAdES XSD schema.
-
Field Details
-
DEFAULT_DSS_C14N_METHOD
This is the default canonicalization method used for production of signatures within DSS framework. Another complication arises because of the way that the default canonicalization algorithm handles namespace declarations; frequently a signed XML document needs to be embedded in another document; in this case the original canonicalization algorithm will not yield the same result as if the document is treated alone. For this reason, the so-called Exclusive Canonicalization, which serializes XML namespace declarations independently of the surrounding XML, was created.- See Also:
-
DEFAULT_XMLDSIG_C14N_METHOD
This is the default canonicalization method for XMLDSIG used for signatures and timestamps (see XMLDSIG 4.4.3.2) when one is not defined.- See Also:
-
SP_DOC_DIGEST_AS_IN_SPECIFICATION_ALGORITHM_URI
The SPDocDigestAsInSpecification transform algorithm URI for a custom SignaturePolicy processing- See Also:
-
-
Method Details
-
registerTransform
This method allows to register a transformation.- Parameters:
transformURI
- the URI of transform- Returns:
- true if this set did not already contain the specified element
-
registerCanonicalizer
This method allows to register a canonicalizer.- Parameters:
c14nAlgorithmURI
- the URI of canonicalization algorithm- Returns:
- true if this set did not already contain the specified element
-
registerTransformWithNodeSetOutput
This method allows to register a transformation resulting to a node-set output. See XMLDSIG for more information- Parameters:
transformURI
- the URI of transform- Returns:
- true if this set did not already contain the specified element
-
indentAndReplace
Indents the given node and replaces it with a new one on the document -
indentAndExtend
Extends the given oldNode by appending new indented childs from the given newNode -
getDocWithIndentedSignature
public static Document getDocWithIndentedSignature(Document documentDom, String signatureId, List<String> noIndentObjectIds) Pretty prints a signature in the given document -
getIndentedNode
Returns an indented xmlNode -
alignChildrenIndents
Aligns indents for all children of the given node -
serializeNode
This method performs the serialization of the given node- Parameters:
xmlNode
- The node to be serialized.- Returns:
- the serialized bytes
-
canCanonicalize
This method says if the framework can canonicalize an XML data with the provided method.- Parameters:
canonicalizationMethod
- the canonicalization method to be checked- Returns:
- true if it is possible to canonicalize false otherwise
-
canonicalize
public static byte[] canonicalize(String canonicalizationMethod, byte[] toCanonicalizeBytes) throws DSSException This method canonicalizes the given array of bytes using thecanonicalizationMethod
parameter.- Parameters:
canonicalizationMethod
- canonicalization methodtoCanonicalizeBytes
- array of bytes to canonicalize- Returns:
- array of canonicalized bytes
- Throws:
DSSException
- if any error is encountered
-
canonicalizeSubtree
This method canonicalizes the givenNode
. If canonicalization method is not provided, theDEFAULT_CANONICALIZATION_METHOD
is being used- Parameters:
canonicalizationMethod
- canonicalization method (can be null)node
-Node
to canonicalize- Returns:
- array of canonicalized bytes
-
getCanonicalizationMethod
Returns thecanonicalizationMethod
if provided, otherwise returns the DEFAULT_CANONICALIZATION_METHOD- Parameters:
canonicalizationMethod
-String
canonicalization method (can be null)- Returns:
- canonicalizationMethod to be used
-
recursiveIdBrowse
An ID attribute can only be dereferenced if it is declared in the validation context. This behaviour is caused by the fact that the attribute does not have attached type of information. Another solution is to parse the XML against some DTD or XML schema. This process adds the necessary type of information to each ID attribute.- Parameters:
element
-Element
-
getIDIdentifier
If this method finds an attribute with the name ID (case-insensitive) then it is returned. If there is more than one ID attributes then the first one is returned.- Parameters:
node
- the node to be checked- Returns:
- the ID attribute value or null
-
getAttribute
Returns attribute value for the given attribute name if exist, otherwise returns NULL -
setIDIdentifier
If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute.- Parameters:
childElement
-Element
-
validateAgainstXSD
This method allows to validate an XML against the XAdES XSD schema.- Parameters:
xsdUtils
- the XSD Utils class to be usedsource
-Source
XML to validate- Returns:
- an empty list if the XSD validates the XML, error messages otherwise
-
isDuplicateIdsDetected
This method is used to detect duplicate id values- Parameters:
doc
- the document to be analyzed- Returns:
- TRUE if a duplicate id is detected
-
getNodeBytes
Returns bytes of the givennode
- Parameters:
node
-Node
to get bytes for- Returns:
- byte array
-
getReferenceOriginalContentBytes
public static byte[] getReferenceOriginalContentBytes(org.apache.xml.security.signature.Reference reference) Returns bytes of the original referenced data- Parameters:
reference
-Reference
to get bytes from- Returns:
- byte array containing original data
-
getDigestAndValue
This method extracts the Digest algorithm and value from an element of type DigestAlgAndValueType- Parameters:
element
- an Element of type DigestAlgAndValueType- Returns:
- an instance of Digest
-
isSignedProperties
public static boolean isSignedProperties(org.apache.xml.security.signature.Reference reference, XAdESPaths xadesPaths) Determines if the givenreference
refers to SignedProperties element- Parameters:
reference
-Reference
to checkxadesPaths
-XAdESPaths
- Returns:
- TRUE if the reference refers to the SignedProperties, FALSE otherwise
-
isCounterSignature
public static boolean isCounterSignature(org.apache.xml.security.signature.Reference reference, XAdESPaths xadesPaths) Determines if the givenreference
refers to CounterSignature element- Parameters:
reference
-Reference
to checkxadesPaths
-XAdESPaths
- Returns:
- TRUE if the reference refers to the CounterSignature, FALSE otherwise
-
isKeyInfoReference
public static boolean isKeyInfoReference(org.apache.xml.security.signature.Reference reference, Element signature) Checks if the given reference is linked to a KeyInfo element- Parameters:
reference
- theReference
to checksignature
- theElement
signature the given reference belongs to- Returns:
- TRUE if the reference is a KeyInfo reference, FALSE otherwise
-
isSignaturePropertiesReference
public static boolean isSignaturePropertiesReference(org.apache.xml.security.signature.Reference reference, Element signature) Checks if the given reference is linked to a SignatureProperties element or one of its SignatureProperty children- Parameters:
reference
- theReference
to checksignature
- theElement
signature the given reference belongs to- Returns:
- TRUE if the reference is a SignatureProperties reference, FALSE otherwise
-
isObjectReferenceType
Checks if the givenreferenceType
is an xmldsig Object type- Parameters:
referenceType
-String
to check the type for- Returns:
- TRUE if the provided
referenceType
is an Object type, FALSE otherwise
-
isManifestReferenceType
Checks if the givenreferenceType
is an xmldsig Manifest type- Parameters:
referenceType
-String
to check the type for- Returns:
- TRUE if the provided
referenceType
is a Manifest type, FALSE otherwise
-
isCounterSignatureReferenceType
Checks if the givenreferenceType
is an etsi Countersignature type- Parameters:
referenceType
-String
to check the type for- Returns:
- TRUE if the provided
referenceType
is a Countersignature type, FALSE otherwise
-
isSameDocumentReference
XMLDSIG 4.4.3.2 The Reference Processing Model A 'same-document' reference is defined as a URI-Reference that consists of a hash sign ('#') followed by a fragment or alternatively consists of an empty URI- Parameters:
referenceUri
-String
uri of a reference to check- Returns:
- TRUE is the URI points to a same-document, FALSE otherwise
-
getKeyInfoSigningCertificatePublicKey
Extracts signing certificate's public key from KeyInfo element of a given signature if present NOTE: can return null (the value is optional) -
createCounterSignature
public static XAdESSignature createCounterSignature(Element counterSignatureElement, XAdESSignature masterSignature) Creates and returns a counter signature found in thecounterSignatureElement
- Parameters:
counterSignatureElement
-Element
<ds:CounterSignature>
elementmasterSignature
-XAdESSignature
master signature containing the counter signature- Returns:
XAdESSignature
-
getAllSignaturesExceptCounterSignatures
Returns a NodeList of all "ds:Signature" elements found in thedocumentNode
-
getReferenceNodeList
Returns a NodeList of "ds:Reference" elements -
getReferenceOutputType
Returns the expected dereferencing output for the providedDSSReference
- Parameters:
reference
-DSSReference
to get OutputType for- Returns:
ReferenceOutputType
-
getReferenceOutputType
public static ReferenceOutputType getReferenceOutputType(org.apache.xml.security.signature.Reference reference) throws org.apache.xml.security.exceptions.XMLSecurityException Returns the expected dereferencing output for the providedReference
- Parameters:
reference
-Reference
to get OutputType for- Returns:
ReferenceOutputType
- Throws:
org.apache.xml.security.exceptions.XMLSecurityException
- if an exception occurs
-
applyTransforms
Applies transforms on the node and returns the byte array to be used for a digest computation NOTE: returns the original node binaries, if the list oftransforms
is empty- Parameters:
node
-Node
to apply transforms ontransforms
- a list ofDSSTransform
s to execute on the node- Returns:
- a byte array, representing a content obtained after transformations
-
applyTransforms
Applies transforms on document content and returns the byte array to be used for a digest computation NOTE: returns the original document binaries, if the list oftransforms
is empty. Thedocument
shall represent an XML content.- Parameters:
document
-DSSDocument
representing an XML to apply transforms ontransforms
- a list ofDSSTransform
s to execute on the node- Returns:
- a byte array, representing a content obtained after transformations
-
getReferenceDigestAlgos
Returns a list ofDigestAlgorithm
for all references containing inside the providedreferenceContainer
- Parameters:
referenceContainer
-Element
containing the ds:Reference elements- Returns:
- a list of
DigestAlgorithm
s
-
getReferenceTypes
Returns a list of reference types -
extractReferences
public static List<org.apache.xml.security.signature.Reference> extractReferences(org.apache.xml.security.signature.Manifest manifest) Extracts a list ofReference
s from the givenManifest
object NOTE: can be used also for aSignedInfo
element- Parameters:
manifest
-Manifest
- Returns:
- a list of
Reference
s
-
getReferenceDigest
Returns theDigest
extracted from the providedreference
- Parameters:
reference
-Reference
- Returns:
Digest
-
getReferenceURI
This method retrieves a URI attribute value of the given reference, when applicable NOTE: Method is used due to Apache Santuario Signature returning an empty string instead of null result.- Parameters:
reference
-Reference
to get value of URI attribute- Returns:
String
URI attribute value if available, NULL otherwise
-
isAbleToDeReferenceContent
public static boolean isAbleToDeReferenceContent(org.apache.xml.security.signature.Reference reference) Checks if the original reference document content can be obtained (de-referenced)- Parameters:
reference
-Reference
to check- Returns:
- TRUE if the de-referencing is succeeds, FALSE otherwise
-
isReferencedContentAmbiguous
Checks if the reference with theuri
occurs multiple times in thedocument
-
incorporateTransforms
public static void incorporateTransforms(Element parentElement, List<DSSTransform> transforms, DSSNamespace namespace) Incorporates a ds:Transforms element into the given parentelement
- Parameters:
parentElement
-Element
to incorporate ds:Transforms intotransforms
- a list ofDSSTransform
s to be incorporatednamespace
-DSSNamespace
to use
-
incorporateDigestMethod
public static void incorporateDigestMethod(Element parentElement, DigestAlgorithm digestAlgorithm, DSSNamespace namespace) This method creates the ds:DigestMethod DOM object<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
- Parameters:
parentElement
-Element
the parent elementdigestAlgorithm
-DigestAlgorithm
the digest algorithmnamespace
-DSSNamespace
to use
-
incorporateDigestValue
public static void incorporateDigestValue(Element parentDom, String base64EncodedDigestBytes, DSSNamespace namespace) This method creates the ds:DigestValue DOM object.<ds:DigestValue>fj8SJujSXU4fi342bdtiKVbglA0=</ds:DigestValue>
- Parameters:
parentDom
-Element
the parent elementbase64EncodedDigestBytes
-String
representing a base64-encoded Digest valuenamespace
-DSSNamespace
-