Package net.shibboleth.metadata.dom.impl
Class XMLSignatureValidator
java.lang.Object
net.shibboleth.metadata.dom.impl.XMLSignatureValidator
A class that encapsulates the various stages in validation of XML signatures as methods.
Failures, and the reason for the failure, are represented by exceptions.
This code largely derives from XmlSecTool V1.2, with the abrupt program termination method of reporting issues replaced by throwing an exception. The intention is that this code might be reintegrated with XmlSecTool in a later release.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException class representing a failure to validate. -
Field Summary
FieldsModifier and TypeFieldDescriptionSet of disallowed digest algorithms.Set of disallowed signature methods.private final booleanWhether an empty reference is permitted.private static final org.slf4j.LoggerClass logger.private final PublicKeyPublic key used to verify signatures. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate org.apache.xml.security.signature.ReferenceextractReference(org.apache.xml.security.signature.XMLSignature signature) Extract the reference within the provided XML signature while ensuring that there is only one such reference, and that (if appropriate) it is not empty.getSignatureElement(Element docElement) Gets the signature element from the document.private voidmarkIdAttribute(Element docElement, org.apache.xml.security.signature.Reference reference) Reconcile the given reference with the document element, by making sure that the appropriate attribute is marked as an ID attribute.private voidvalidateSignatureReference(Element docElement, org.apache.xml.security.signature.Reference ref) Validates the reference within the XML signature by performing the following checks.private voidvalidateSignatureReferenceUri(Element expectedSignedNode, org.apache.xml.security.signature.Reference reference) Validates that the element resolved by the signature validation layer is the same as the element resolved by the DOM layer.private voidvalidateSignatureTransforms(org.apache.xml.security.signature.Reference reference) Validate the transforms included in the Signature Reference.voidverifySignature(Element docElement, Element signatureElement) Verifies that the signature on a document is valid.
-
Field Details
-
LOG
@Nonnull private static final org.slf4j.Logger LOGClass logger. -
verificationKey
Public key used to verify signatures. -
disallowedDigests
Set of disallowed digest algorithms. -
disallowedSignatureMethods
Set of disallowed signature methods. -
emptyReferencePermitted
private final boolean emptyReferencePermittedWhether an empty reference is permitted.
-
-
Constructor Details
-
XMLSignatureValidator
public XMLSignatureValidator(@Nonnull PublicKey key, @Nullable Set<String> disallowDigests, @Nullable Set<String> disallowSignatureMethods, boolean emptyRefPermitted) Constructor.- Parameters:
key- public key with which to verify signaturesdisallowDigests- set of disallowed digest algorithm URIs, ornulldisallowSignatureMethods- set of disallowed signature method URIs, ornullemptyRefPermitted- true if empty references are permitted
-
-
Method Details
-
markIdAttribute
private void markIdAttribute(@Nonnull Element docElement, @Nonnull org.apache.xml.security.signature.Reference reference) throws XMLSignatureValidator.ValidationException Reconcile the given reference with the document element, by making sure that the appropriate attribute is marked as an ID attribute.- Parameters:
docElement- document element whose appropriate attribute should be markedreference- reference which references the document element- Throws:
XMLSignatureValidator.ValidationException- if the reference is neither empty nor to a fragment
-
verifySignature
public void verifySignature(@Nonnull Element docElement, @Nonnull Element signatureElement) throws XMLSignatureValidator.ValidationException Verifies that the signature on a document is valid.- Parameters:
docElement- document element whose signature will be validatedsignatureElement- element containing the signature to be validated- Throws:
XMLSignatureValidator.ValidationException- if any of a number of invalid conditions are detected
-
extractReference
@Nonnull private org.apache.xml.security.signature.Reference extractReference(@Nonnull org.apache.xml.security.signature.XMLSignature signature) throws XMLSignatureValidator.ValidationException Extract the reference within the provided XML signature while ensuring that there is only one such reference, and that (if appropriate) it is not empty.- Parameters:
signature- signature to extract the reference from- Returns:
- the extracted reference
- Throws:
XMLSignatureValidator.ValidationException- if a reference could not be extracted
-
validateSignatureReference
private void validateSignatureReference(@Nonnull Element docElement, @Nonnull org.apache.xml.security.signature.Reference ref) throws XMLSignatureValidator.ValidationException Validates the reference within the XML signature by performing the following checks.- check that the XML signature layer resolves that reference to the same element as the DOM layer does
- check that only enveloped and, optionally, exclusive canonicalization transforms are used
- Parameters:
docElement- document elementref- reference to be verified- Throws:
XMLSignatureValidator.ValidationException- if any of the checks fail
-
validateSignatureReferenceUri
private void validateSignatureReferenceUri(@Nonnull Element expectedSignedNode, @Nonnull org.apache.xml.security.signature.Reference reference) throws XMLSignatureValidator.ValidationException Validates that the element resolved by the signature validation layer is the same as the element resolved by the DOM layer.- Parameters:
expectedSignedNode- the node expected as the result of the referencereference- the reference to be validated- Throws:
XMLSignatureValidator.ValidationException- if validation fails
-
validateSignatureTransforms
private void validateSignatureTransforms(@Nonnull org.apache.xml.security.signature.Reference reference) throws XMLSignatureValidator.ValidationException Validate the transforms included in the Signature Reference. The Reference may contain at most 2 transforms. One of them must be the Enveloped signature transform. An Exclusive Canonicalization transform (with or without comments) may also be present. No other transforms are allowed.- Parameters:
reference- the Signature reference containing the transforms to evaluate- Throws:
XMLSignatureValidator.ValidationException- if the transforms are incorrect
-
getSignatureElement
@Nullable public Element getSignatureElement(@Nonnull Element docElement) throws XMLSignatureValidator.ValidationException Gets the signature element from the document. The signature must be a child of the document root.- Parameters:
docElement- document element from which to pull the signature- Returns:
- the signature element, or null
- Throws:
XMLSignatureValidator.ValidationException- if more than one signature element is present
-