Example usage for javax.xml.crypto.dsig Reference getDereferencedData

List of usage examples for javax.xml.crypto.dsig Reference getDereferencedData

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig Reference getDereferencedData.

Prototype

Data getDereferencedData();

Source Link

Document

Returns the dereferenced data, if reference caching is enabled.

Usage

From source file:eu.europa.ec.markt.dss.validation102853.xades.XAdESSignature.java

@Override
public SignatureCryptographicVerification checkIntegrity(DSSDocument detachedDocument) {

    final SignatureCryptographicVerification scv = new SignatureCryptographicVerification();

    final CertificateToken certToken = getSigningCertificate().getCertToken();
    if (certToken != null) {

        final PublicKey publicKey = certToken.getCertificate().getPublicKey();
        final KeySelector keySelector = KeySelector.singletonKeySelector(publicKey);

        /**/*from   ww  w.  j  av a  2 s.  c om*/
         * Creating a Validation Context<br>
         * We create an XMLValidateContext instance containing input parameters for validating the signature. Since we
         * are using DOM, we instantiate a DOMValidateContext instance (a subclass of XMLValidateContext), and pass it
         * two parameters, a KeyValueKeySelector object and a reference to the Signature element to be validated (which
         * is the first entry of the NodeList we generated earlier):
         */
        final DOMValidateContext valContext = new DOMValidateContext(keySelector, signatureElement);
        try {

            URIDereferencer dereferencer = new ExternalFileURIDereferencer(detachedDocument);
            valContext.setURIDereferencer(dereferencer);
            /**
             * This property controls whether or not the digested Reference objects will cache the dereferenced content
             * and pre-digested input for subsequent retrieval via the Reference.getDereferencedData and
             * Reference.getDigestInputStream methods. The default value if not specified is Boolean.FALSE.
             */
            valContext.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);

            /**
             * Unmarshalling the XML Signature<br>
             * We extract the contents of the Signature element into an XMLSignature object. This process is called
             * unmarshalling. The Signature element is unmarshalled using an XMLSignatureFactory object. An application
             * can obtain a DOM implementation of XMLSignatureFactory by calling the following line of code:
             */

            // These providers do not support ECDSA algorithm
            // factory = XMLSignatureFactory.getInstance("DOM");
            // factory = XMLSignatureFactory.getInstance("DOM", "XMLDSig");
            // factory = XMLSignatureFactory.getInstance("DOM", new org.jcp.xml.dsig.internal.dom.XMLDSigRI());

            // This provider support ECDSA signature
            /**
             * ApacheXMLDSig / Apache Santuario XMLDSig (DOM XMLSignatureFactory; DOM KeyInfoFactory; C14N 1.0, C14N
             * 1.1, Exclusive C14N, Base64, Enveloped, XPath, XPath2, XSLT TransformServices)<br>
             * If this library is used than the same library must be used for the URIDereferencer.
             */
            final XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM", xmlProvider);

            /**
             * We then invoke the unmarshalXMLSignature method of the factory to unmarshal an XMLSignature object, and
             * pass it the validation context we created earlier:
             */
            final XMLSignature signature = factory.unmarshalXMLSignature(valContext);
            //System.out.println("XMLSignature class: " + signature.getClass());

            // Austrian specific signature
            //org.apache.xml.security.signature.XMLSignature signature_ = null;
            // try {
            // signature_ = new org.apache.xml.security.signature.XMLSignature(signatureElement, "");
            // } catch (Exception e) {
            //
            // throw new DSSException(e);
            // }
            // signature.addResourceResolver(new XPointerResourceResolver(signatureElement));

            //signature_.getSignedInfo().verifyReferences();//getVerificationResult(1);
            /**
             * In case of org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI() provider, the ID attributes need to be set
             * manually.<br>
             * The DSSXMLUtils.recursiveIdBrowse(...) method do not take into account the XML outside of the Signature
             * tag. It prevents some signatures to be validated.<br>
             *
             * Solution: the following lines where added:
             */
            final Document document = signatureElement.getOwnerDocument();
            final Element rootElement = document.getDocumentElement();
            if (rootElement.hasAttribute(DSSXMLUtils.ID_ATTRIBUTE_NAME)) {

                valContext.setIdAttributeNS(rootElement, null, DSSXMLUtils.ID_ATTRIBUTE_NAME);
            }

            DSSXMLUtils.recursiveIdBrowse(valContext, rootElement);

            /**
             * Validating the XML Signature<br>
             * Now we are ready to validate the signature. We do this by invoking the validate method on the
             * XMLSignature object, and pass it the validation context as follows:
             */
            boolean coreValidity = false;
            try {

                coreValidity = signature.validate(valContext);
            } catch (XMLSignatureException e) {

                scv.setErrorMessage("Signature validation: " + e.getMessage());
            }
            boolean signatureValidity = coreValidity;
            boolean dataFound = true;
            boolean dataHashValid = true;

            /**
             * If the XMLSignature.validate method returns false, we can try to narrow down the cause of the failure.
             * There are two phases in core XML Signature validation: <br>
             * - Signature validation (the cryptographic verification of the signature)<br>
             * - Reference validation (the verification of the digest of each reference in the signature)<br>
             * Each phase must be successful for the signature to be valid. To check if the signature failed to
             * cryptographically validate, we can check the status, as follows:
             */

            try {

                signatureValidity = signature.getSignatureValue().validate(valContext);
            } catch (XMLSignatureException e) {

                scv.setErrorMessage(e.getMessage());
            }

            @SuppressWarnings("unchecked")
            final List<Reference> references = signature.getSignedInfo().getReferences();
            for (Reference reference : references) {

                boolean refHashValidity = false;
                try {

                    refHashValidity = reference.validate(valContext);
                } catch (XMLSignatureException e) {

                    scv.setErrorMessage(reference.getURI() + ": " + e.getMessage());
                }
                dataHashValid = dataHashValid && refHashValidity;
                if (LOG.isLoggable(Level.INFO)) {
                    LOG.info("Reference hash validity checked: " + reference.getURI() + "=" + refHashValidity);
                }
                final Data data = reference.getDereferencedData();
                dataFound = dataFound && (data != null);

                final InputStream digestInputStream = reference.getDigestInputStream();
                if (data != null && digestInputStream != null) {

                    // The references are saved for later treatment in -A level.
                    try {

                        IOUtils.copy(digestInputStream, referencesDigestOutputStream);
                    } catch (IOException e) {
                    }
                }
            }
            scv.setReferenceDataFound(dataFound);
            scv.setReferenceDataIntact(dataHashValid);
            scv.setSignatureIntegrity(signatureValidity);
        } catch (MarshalException e) {

            scv.setErrorMessage(e.getMessage());
        }
    } else {

        scv.setErrorMessage(
                "Unable to proceed with the signature cryptographic verification. There is no signing certificate!");
    }
    return scv;
}