Example usage for javax.xml.crypto.dsig XMLSignature getSignedInfo

List of usage examples for javax.xml.crypto.dsig XMLSignature getSignedInfo

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig XMLSignature getSignedInfo.

Prototype

SignedInfo getSignedInfo();

Source Link

Document

Returns the signed info of this XMLSignature.

Usage

From source file:cl.nic.dte.util.XMLUtil.java

/**
 * Verifica si una firma XML embedida es válida según define
 * el est&aacute;ndar XML Signature (<a
 * href="http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation">Core
 * Validation</a>), y si el certificado era v&aacute;lido en la fecha dada.
 * <p>//w  ww. j  a  v a 2  s. c  om
 * 
 * Esta rutina <b>NO</b> verifica si el certificado embedido en
 * &lt;KeyInfo&gt; es v&aacute;lido (eso debe verificarlo con la autoridad
 * certificadora que emiti&oacute; el certificado), pero si verifica que la
 * llave utilizada para verificar corresponde a la contenida en el
 * certificado.
 * 
 * @param xml
 *            el nodo &lt;Signature&gt;
 * @param date
 *            una fecha en la que se verifica la validez del certificado
 * @return el resultado de la verificaci&oacute;n
 * 
 * @see javax.xml.crypto.dsig.XMLSignature#sign(javax.xml.crypto.dsig.XMLSignContext)
 * @see cl.nic.dte.VerifyResult
 * @see cl.nic.dte.extension.DTEDefTypeExtensionHandler
 * @see #getCertificate(XMLSignature)
 */
@SuppressWarnings("unchecked")
public static VerifyResult verifySignature(XMLSignature signature, DOMValidateContext valContext) {

    try {

        KeyValueKeySelector ksel = (KeyValueKeySelector) valContext.getKeySelector();
        X509Certificate x509 = getCertificate(signature);

        // Verifica que un certificado bien embedido
        if (x509 == null) {
            return (new VerifyResult(VerifyResult.XML_SIGNATURE_WRONG, false,
                    Utilities.verificationLabels.getString("XML_SIGNATURE_ERROR_NO509")));
        }

        // Validate the XMLSignature
        boolean coreValidity = signature.validate(valContext);

        // Check core validation status
        if (coreValidity == false) {
            boolean sv = signature.getSignatureValue().validate(valContext);
            if (!sv)
                return new VerifyResult(VerifyResult.XML_SIGNATURE_WRONG, false,
                        Utilities.verificationLabels.getString("XML_SIGNATURE_BAD_VALUE"));

            // check the validation status of each Reference
            String message = "";

            for (Reference ref : (List<Reference>) signature.getSignedInfo().getReferences()) {
                if (!ref.validate(valContext)) {
                    message += Utilities.verificationLabels.getString("XML_SIGNATURE_BAD_REFERENCE");
                    message = message.replaceAll("%1",
                            new String(Base64.encodeBase64(ref.getCalculatedDigestValue())));
                    message = message.replaceAll("%2", new String(Base64.encodeBase64(ref.getDigestValue())));
                    message += "\n";
                }
            }
            return new VerifyResult(VerifyResult.XML_SIGNATURE_WRONG, false, message);
        }

        // Verifica que la llave del certificado corresponde a la usada para
        // la firma
        if (!ksel.getPk().equals(x509.getPublicKey())) {
            String message = Utilities.verificationLabels.getString("XML_SIGNATURE_ERROR_BADKEY");
            return (new VerifyResult(VerifyResult.XML_SIGNATURE_WRONG, false, message));
        }

        return new VerifyResult(VerifyResult.XML_SIGNATURE_OK, true, null);
    } catch (XMLSignatureException e) {
        return (new VerifyResult(VerifyResult.XML_SIGNATURE_WRONG, false,
                Utilities.verificationLabels.getString("XML_SIGNATURE_ERROR_UNKNOWN") + ": " + e.getMessage()));
    }

}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureVerifier.java

@SuppressWarnings("unchecked")
public boolean isValidOOXMLSignature(XMLSignature xmlSignature, byte[] document)
        throws IOException, TransformerException, SAXException, ParserConfigurationException {

    // check c18n == http://www.w3.org/TR/2001/REC-xml-c14n-20010315
    if (!xmlSignature.getSignedInfo().getCanonicalizationMethod().getAlgorithm()
            .equals(CanonicalizationMethod.INCLUSIVE)) {
        LOG.error("Invalid c18n method on OOXML Signature");
        return false;
    }/* w  w w.jav  a  2  s.  c o m*/

    List<Reference> refs = xmlSignature.getSignedInfo().getReferences();

    // check #idPackageObject reference
    Reference idPackageObjectRef = findReferenceFromURI(refs, "#idPackageObject");
    if (null == idPackageObjectRef) {
        LOG.error("No \"idPackageObject\" reference found!");
        return false;
    }

    // check idPackageObject element
    XMLObject idPackageObject = findObject(xmlSignature, "idPackageObject");
    if (null == idPackageObject) {
        LOG.error("No \"idPackageObject\" object found!");
        return false;
    }
    if (!isIdPackageObjectValid(xmlSignature.getId(), idPackageObject, document)) {
        LOG.error("Invalid \"idPackageObject\".");
        return false;
    }

    // check #idOfficeObject reference
    Reference idOfficeObjectRef = findReferenceFromURI(refs, "#idOfficeObject");
    if (null == idOfficeObjectRef) {
        LOG.error("No \"idOfficeObject\" reference found!");
        return false;
    }

    // check idOfficeObject element
    XMLObject idOfficeObject = findObject(xmlSignature, "idOfficeObject");
    if (null == idOfficeObject) {
        LOG.error("No \"idOfficeObject\" object found!");
        return false;
    }
    if (!isIdOfficeObjectValid(xmlSignature.getId(), idOfficeObject)) {
        LOG.error("Invalid \"idOfficeObject\".");
        return false;
    }

    return true;
}

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

@Override
public List<AdvancedSignature> getCounterSignatures() {
    // see ETSI TS 101 903 V1.4.2 (2010-12) pp. 38/39/40

    try {//from  w  w  w .j  a  va 2  s  . com
        NodeList counterSigs = XMLUtils.getNodeList(signatureElement,
                "./ds:Object/xades:QualifyingProperties/xades:UnsignedProperties/xades:UnsignedSignatureProperties"
                        + "/xades:CounterSignature");
        if (counterSigs == null) {
            return null;
        }

        List<AdvancedSignature> xadesList = new ArrayList<AdvancedSignature>();

        for (int i = 0; i < counterSigs.getLength(); i++) {
            Element counterSigEl = (Element) counterSigs.item(i);
            Element signatureEl = XMLUtils.getElement(counterSigEl, "./ds:Signature");

            // Verify that the element is a proper signature by trying to build a XAdESSignature out of it
            XAdESSignature xCounterSig = new XAdESSignature(signatureEl);

            // Verify that there is a ds:Reference element with a Type set to: http://uri.etsi.org/01903#CountersignedSignature
            // (as per the XAdES spec)
            XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM");
            XMLSignature signature = factory.unmarshalXMLSignature(new DOMStructure(signatureEl));

            LOG.info("Verifying countersignature References");
            for (Object refobj : signature.getSignedInfo().getReferences()) {
                Reference ref = (Reference) refobj;
                if (ref.getType() != null
                        && ref.getType().equals("http://uri.etsi.org/01903#CountersignedSignature")) {
                    // Ok, this seems to be a countersignature

                    // Verify that the digest is that of the signature value
                    if (ref.validate(new DOMValidateContext(xCounterSig.getSigningCertificate().getPublicKey(),
                            XMLUtils.getElement(signatureElement, "./ds:SignatureValue")))) {

                        LOG.info("Reference verification succeeded, adding countersignature");
                        xadesList.add(xCounterSig);
                    } else {
                        LOG.warning(
                                "Skipping countersignature because the Reference doesn't contain a hash of the embedding SignatureValue");
                    }

                    break;
                }
            }
        }

        return xadesList;
    } catch (XPathExpressionException e) {
        throw new EncodingException(MSG.COUNTERSIGNATURE_ENCODING);
    } catch (MarshalException e) {
        throw new EncodingException(MSG.COUNTERSIGNATURE_ENCODING);
    } catch (XMLSignatureException e) {
        throw new EncodingException(MSG.COUNTERSIGNATURE_ENCODING);
    }

}

From source file:com.fujitsu.dc.common.auth.token.TransCellAccessToken.java

/**
 * TransCellAccessToken????.//from  www  .  j  a  v  a2 s.  c om
 * @param token 
 * @return TransCellAccessToken(?)
 * @throws AbstractOAuth2Token.TokenParseException ?
 * @throws AbstractOAuth2Token.TokenDsigException ???
 * @throws AbstractOAuth2Token.TokenRootCrtException CA?
 */
public static TransCellAccessToken parse(final String token) throws AbstractOAuth2Token.TokenParseException,
        AbstractOAuth2Token.TokenDsigException, AbstractOAuth2Token.TokenRootCrtException {
    try {
        byte[] samlBytes = DcCoreUtils.decodeBase64Url(token);
        ByteArrayInputStream bais = new ByteArrayInputStream(samlBytes);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = null;
        try {
            builder = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // ????????????
            throw new RuntimeException(e);
        }

        Document doc = builder.parse(bais);

        Element assertion = doc.getDocumentElement();
        Element issuer = (Element) (doc.getElementsByTagName("Issuer").item(0));
        Element subject = (Element) (assertion.getElementsByTagName("Subject").item(0));
        Element subjectNameID = (Element) (subject.getElementsByTagName("NameID").item(0));
        String id = assertion.getAttribute("ID");
        String issuedAtStr = assertion.getAttribute("IssueInstant");

        DateTime dt = new DateTime(issuedAtStr);

        NodeList audienceList = assertion.getElementsByTagName("Audience");
        Element aud1 = (Element) (audienceList.item(0));
        String target = aud1.getTextContent();
        String schema = null;
        if (audienceList.getLength() > 1) {
            Element aud2 = (Element) (audienceList.item(1));
            schema = aud2.getTextContent();
        }

        List<Role> roles = new ArrayList<Role>();
        NodeList attrList = assertion.getElementsByTagName("AttributeValue");
        for (int i = 0; i < attrList.getLength(); i++) {
            Element attv = (Element) (attrList.item(i));
            roles.add(new Role(new URL(attv.getTextContent())));
        }

        NodeList nl = assertion.getElementsByTagName("Signature");
        if (nl.getLength() == 0) {
            throw new TokenParseException("Cannot find Signature element");
        }
        Element signatureElement = (Element) nl.item(0);

        // ???????TokenDsigException??
        // Create a DOMValidateContext and specify a KeySelector
        // and document context.
        X509KeySelector x509KeySelector = new X509KeySelector(issuer.getTextContent());
        DOMValidateContext valContext = new DOMValidateContext(x509KeySelector, signatureElement);

        // Unmarshal the XMLSignature.
        XMLSignature signature;
        try {
            signature = xmlSignatureFactory.unmarshalXMLSignature(valContext);
        } catch (MarshalException e) {
            throw new TokenDsigException(e.getMessage(), e);
        }

        // CA??
        try {
            x509KeySelector.readRoot(x509RootCertificateFileNames);
        } catch (CertificateException e) {
            // CA????????500
            throw new TokenRootCrtException(e.getMessage(), e);
        }

        // Validate the XMLSignature x509.
        boolean coreValidity;
        try {
            coreValidity = signature.validate(valContext);
        } catch (XMLSignatureException e) {
            if (e.getCause().getClass() == new KeySelectorException().getClass()) {
                throw new TokenDsigException(e.getCause().getMessage(), e.getCause());
            }
            throw new TokenDsigException(e.getMessage(), e);
        }

        // http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation

        // Check core validation status.
        if (!coreValidity) {
            // ??
            boolean isDsigValid;
            try {
                isDsigValid = signature.getSignatureValue().validate(valContext);
            } catch (XMLSignatureException e) {
                throw new TokenDsigException(e.getMessage(), e);
            }
            if (!isDsigValid) {
                throw new TokenDsigException("Failed signature validation");
            }

            // 
            Iterator i = signature.getSignedInfo().getReferences().iterator();
            for (int j = 0; i.hasNext(); j++) {
                boolean refValid;
                try {
                    refValid = ((Reference) i.next()).validate(valContext);
                } catch (XMLSignatureException e) {
                    throw new TokenDsigException(e.getMessage(), e);
                }
                if (!refValid) {
                    throw new TokenDsigException("Failed to validate reference [" + j + "]");
                }
            }
            throw new TokenDsigException("Signature failed core validation. unkwnon reason.");
        }
        return new TransCellAccessToken(id, dt.getMillis(), issuer.getTextContent(),
                subjectNameID.getTextContent(), target, roles, schema);
    } catch (UnsupportedEncodingException e) {
        throw new TokenParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new TokenParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new TokenParseException(e.getMessage(), e);
    }
}

From source file:io.personium.common.auth.token.TransCellAccessToken.java

/**
 * TransCellAccessToken????./*from   w w w  .  j  a  v  a2  s . com*/
 * @param token 
 * @return TransCellAccessToken(?)
 * @throws AbstractOAuth2Token.TokenParseException ?
 * @throws AbstractOAuth2Token.TokenDsigException ???
 * @throws AbstractOAuth2Token.TokenRootCrtException CA?
 */
public static TransCellAccessToken parse(final String token) throws AbstractOAuth2Token.TokenParseException,
        AbstractOAuth2Token.TokenDsigException, AbstractOAuth2Token.TokenRootCrtException {
    try {
        byte[] samlBytes = PersoniumCoreUtils.decodeBase64Url(token);
        ByteArrayInputStream bais = new ByteArrayInputStream(samlBytes);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = null;
        try {
            builder = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // ????????????
            throw new RuntimeException(e);
        }

        Document doc = builder.parse(bais);

        Element assertion = doc.getDocumentElement();
        Element issuer = (Element) (doc.getElementsByTagName("Issuer").item(0));
        Element subject = (Element) (assertion.getElementsByTagName("Subject").item(0));
        Element subjectNameID = (Element) (subject.getElementsByTagName("NameID").item(0));
        String id = assertion.getAttribute("ID");
        String issuedAtStr = assertion.getAttribute("IssueInstant");

        DateTime dt = new DateTime(issuedAtStr);

        NodeList audienceList = assertion.getElementsByTagName("Audience");
        Element aud1 = (Element) (audienceList.item(0));
        String target = aud1.getTextContent();
        String schema = null;
        if (audienceList.getLength() > 1) {
            Element aud2 = (Element) (audienceList.item(1));
            schema = aud2.getTextContent();
        }

        List<Role> roles = new ArrayList<Role>();
        NodeList attrList = assertion.getElementsByTagName("AttributeValue");
        for (int i = 0; i < attrList.getLength(); i++) {
            Element attv = (Element) (attrList.item(i));
            roles.add(new Role(new URL(attv.getTextContent())));
        }

        NodeList nl = assertion.getElementsByTagName("Signature");
        if (nl.getLength() == 0) {
            throw new TokenParseException("Cannot find Signature element");
        }
        Element signatureElement = (Element) nl.item(0);

        // ???????TokenDsigException??
        // Create a DOMValidateContext and specify a KeySelector
        // and document context.
        X509KeySelector x509KeySelector = new X509KeySelector(issuer.getTextContent());
        DOMValidateContext valContext = new DOMValidateContext(x509KeySelector, signatureElement);

        // Unmarshal the XMLSignature.
        XMLSignature signature;
        try {
            signature = xmlSignatureFactory.unmarshalXMLSignature(valContext);
        } catch (MarshalException e) {
            throw new TokenDsigException(e.getMessage(), e);
        }

        // CA??
        try {
            x509KeySelector.readRoot(x509RootCertificateFileNames);
        } catch (CertificateException e) {
            // CA????????500
            throw new TokenRootCrtException(e.getMessage(), e);
        }

        // Validate the XMLSignature x509.
        boolean coreValidity;
        try {
            coreValidity = signature.validate(valContext);
        } catch (XMLSignatureException e) {
            if (e.getCause().getClass() == new KeySelectorException().getClass()) {
                throw new TokenDsigException(e.getCause().getMessage(), e.getCause());
            }
            throw new TokenDsigException(e.getMessage(), e);
        }

        // http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation

        // Check core validation status.
        if (!coreValidity) {
            // ??
            boolean isDsigValid;
            try {
                isDsigValid = signature.getSignatureValue().validate(valContext);
            } catch (XMLSignatureException e) {
                throw new TokenDsigException(e.getMessage(), e);
            }
            if (!isDsigValid) {
                throw new TokenDsigException("Failed signature validation");
            }

            // 
            Iterator i = signature.getSignedInfo().getReferences().iterator();
            for (int j = 0; i.hasNext(); j++) {
                boolean refValid;
                try {
                    refValid = ((Reference) i.next()).validate(valContext);
                } catch (XMLSignatureException e) {
                    throw new TokenDsigException(e.getMessage(), e);
                }
                if (!refValid) {
                    throw new TokenDsigException("Failed to validate reference [" + j + "]");
                }
            }
            throw new TokenDsigException("Signature failed core validation. unkwnon reason.");
        }
        return new TransCellAccessToken(id, dt.getMillis(), issuer.getTextContent(),
                subjectNameID.getTextContent(), target, roles, schema);
    } catch (UnsupportedEncodingException e) {
        throw new TokenParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new TokenParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new TokenParseException(e.getMessage(), e);
    }
}

From source file:com.vmware.identity.sts.ws.SignatureValidator.java

/**
 * Validate references present in the XmlSignature.
 * @param xmlSignature the xml signature whose references are to be validated. not null.
 * @param valContext validation context used to validate the signature itself. not null.
 * @param document document the signature belongs to. not null.
 * @param timestampNode the timestamp node of the soap security header within the document.
 * @throws XMLSignatureException when the validation fails.
 */// w  w  w.  j  a  va  2s.  com
private void validateSignatureReferences(XMLSignature xmlSignature, DOMValidateContext valContext,
        Document document, Node timestampNode) throws XMLSignatureException {

    assert xmlSignature != null;
    assert valContext != null;
    assert document != null;
    assert timestampNode != null;

    //    If a signature is applied to a request then it must include:
    //    Either the <S11:Body>, or the WS-Trust element as a direct child of the <S11:Body>
    //    The <wsu:Timestamp>, if present in the <S11:Header>. 
    //        (in fact this must be present as per same spec, and SOAPHeaderExtractor validates it)

    Node soapBody = getSoapBody(document);
    Node wsTrustNode = getWsTrustNode(soapBody);
    boolean foundTimestampElement = false;
    boolean foundBodyOrWSTrustElement = false;

    List<Reference> references = xmlSignature.getSignedInfo().getReferences();
    if ((references == null) || (references.size() == 0)) {
        throw new XMLSignatureException("Signature's SignInfo does not contain any references.");
    }

    for (Reference reference : references) {

        if (reference != null) {
            validateReferenceTransforms(reference);
            validateReferenceUri(reference);
            // note: order is important, we should not try to validate digests
            // before we checked expected transforms, and uri etc.
            if (!reference.validate(valContext)) {
                throw new XMLSignatureException(
                        String.format("Signature reference '%s' is invalid.", reference.getURI()));
            }

            if (!foundTimestampElement || !foundBodyOrWSTrustElement) {
                String id = org.jcp.xml.dsig.internal.dom.Utils.parseIdFromSameDocumentURI(reference.getURI());
                Node referencedNode = document.getElementById(id);
                foundTimestampElement = (foundTimestampElement) || (timestampNode.isSameNode(referencedNode));
                foundBodyOrWSTrustElement = (foundBodyOrWSTrustElement) || (soapBody.isSameNode(referencedNode))
                        || (wsTrustNode.isSameNode(referencedNode));
            }
        }
    } // for each reference

    if (!foundTimestampElement || !foundBodyOrWSTrustElement) {
        throw new XMLSignatureException(
                "Signature must include <wsu:Timestamp> and either SoapBody, or the WSTrust element within it.");
    }
}

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

@Override
public List<AdvancedSignature> getCounterSignatures() {

    // see ETSI TS 101 903 V1.4.2 (2010-12) pp. 38/39/40

    try {//from w  ww.  j  a  v  a 2s .  co m
        NodeList counterSigs = DSSXMLUtils.getNodeList(signatureElement, XPATH_COUNTER_SIGNATURE);
        if (counterSigs == null) {
            return null;
        }

        List<AdvancedSignature> xadesList = new ArrayList<AdvancedSignature>();

        for (int i = 0; i < counterSigs.getLength(); i++) {

            Element counterSigEl = (Element) counterSigs.item(i);
            Element signatureEl = DSSXMLUtils.getElement(counterSigEl, XPATH_SIGNATURE);

            // Verify that the element is a proper signature by trying to build a XAdESSignature out of it
            XAdESSignature xCounterSig = new XAdESSignature(signatureEl, certPool);

            /*
             * Verify that there is a ds:Reference element with a Type set to:
             * http://uri.etsi.org/01903#CountersignedSignature (as per the XAdES spec)
             */
            XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM");
            javax.xml.crypto.dsig.XMLSignature signature = factory
                    .unmarshalXMLSignature(new DOMStructure(signatureEl));

            LOG.info("Verifying countersignature References");
            for (Object refobj : signature.getSignedInfo().getReferences()) {

                Reference ref = (Reference) refobj;
                if (ref.getType() != null && ref.getType().equals(XADES_COUNTERSIGNED_SIGNATURE)) {

                    // Ok, this seems to be a CounterSignature
                    // Verify that the digest is that of the signature value
                    CertificateToken certToken = xCounterSig.getSigningCertificate().getCertToken();
                    PublicKey publicKey = certToken.getCertificate().getPublicKey();
                    if (ref.validate(new DOMValidateContext(publicKey,
                            DSSXMLUtils.getElement(signatureElement, XPATH_SIGNATURE_VALUE)))) {

                        LOG.info("Reference verification succeeded, adding countersignature");
                        xadesList.add(xCounterSig);
                    } else {

                        LOG.warning(
                                "Skipping countersignature because the Reference doesn't contain a hash of the embedding SignatureValue");
                    }
                    break;
                }
            }
        }
        return xadesList;
    } catch (MarshalException e) {

        throw new EncodingException(MSG.COUNTERSIGNATURE_ENCODING, e);
    } catch (XMLSignatureException e) {

        throw new EncodingException(MSG.COUNTERSIGNATURE_ENCODING, e);
    }
}

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   w  w  w . ja v a  2 s.  c  o  m*/
         * 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;
}

From source file:org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignature.java

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }/*from w w w.j a  va 2  s. c o  m*/

    if (!(o instanceof XMLSignature)) {
        return false;
    }
    XMLSignature osig = (XMLSignature) o;

    boolean idEqual = (id == null ? osig.getId() == null : id.equals(osig.getId()));
    boolean keyInfoEqual = (ki == null ? osig.getKeyInfo() == null : ki.equals(osig.getKeyInfo()));

    return (idEqual && keyInfoEqual && sv.equals(osig.getSignatureValue()) && si.equals(osig.getSignedInfo())
            && objects.equals(osig.getObjects()));
}

From source file:org.apache.juddi.v3.client.cryptor.DigSigUtil.java

private boolean verifySignature(Element element, PublicKey validatingKey,
        AtomicReference<String> OutReadableErrorMessage) {
    if (OutReadableErrorMessage == null) {
        OutReadableErrorMessage = new AtomicReference<String>();
    }/*  www . j a v  a  2  s  . co  m*/
    XMLSignatureFactory fac = initXMLSigFactory();
    NodeList nl = element.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
    if (nl.getLength() == 0) {
        throw new RuntimeException("Cannot find Signature element");
    }
    DOMValidateContext valContext = new DOMValidateContext(validatingKey, nl.item(0));
    try {
        valContext.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
        XMLSignature signature = fac.unmarshalXMLSignature(valContext);
        boolean coreValidity = signature.validate(valContext);
        // Check core validation status.
        if (coreValidity == false) {
            logger.warn("Signature failed core validation");
            boolean sv = signature.getSignatureValue().validate(valContext);
            logger.debug("signature validation status: " + sv);
            OutReadableErrorMessage
                    .set("signature validation failed: " + sv + "." + OutReadableErrorMessage.get());
            // Check the validation status of each Reference.
            @SuppressWarnings("unchecked")
            Iterator<Reference> i = signature.getSignedInfo().getReferences().iterator();
            //System.out.println("---------------------------------------------");
            for (int j = 0; i.hasNext(); j++) {
                Reference ref = (Reference) i.next();
                boolean refValid = ref.validate(valContext);
                logger.debug(j);
                logger.debug("ref[" + j + "] validity status: " + refValid);
                if (!refValid) {
                    OutReadableErrorMessage
                            .set("signature reference " + j + " invalid. " + OutReadableErrorMessage.get());
                }
                logger.debug("Ref type: " + ref.getType() + ", URI: " + ref.getURI());
                for (Object xform : ref.getTransforms()) {
                    logger.debug("Transform: " + xform);
                }
                String calcDigValStr = digestToString(ref.getCalculatedDigestValue());
                String expectedDigValStr = digestToString(ref.getDigestValue());
                logger.warn("    Calc Digest: " + calcDigValStr);
                logger.warn("Expected Digest: " + expectedDigValStr);
                if (!calcDigValStr.equalsIgnoreCase(expectedDigValStr)) {
                    OutReadableErrorMessage.set(
                            "digest mismatch for signature ref " + j + "." + OutReadableErrorMessage.get());
                }
            }
        } else {
            logger.info("Signature passed core validation");
        }
        return coreValidity;
    } catch (Exception e) {
        OutReadableErrorMessage
                .set("signature validation failed: " + e.getMessage() + OutReadableErrorMessage.get());
        logger.fatal(e);
        return false;
    }
}