Example usage for javax.xml.crypto.dsig XMLValidateContext getProperty

List of usage examples for javax.xml.crypto.dsig XMLValidateContext getProperty

Introduction

In this page you can find the example usage for javax.xml.crypto.dsig XMLValidateContext getProperty.

Prototype

Object getProperty(String name);

Source Link

Document

Returns the value of the specified property.

Usage

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

boolean verify(Key key, SignedInfo si, byte[] sig, XMLValidateContext context)
        throws InvalidKeyException, SignatureException, XMLSignatureException {
    if (key == null || si == null || sig == null) {
        throw new NullPointerException();
    }/*from  w  w w .j  a v  a 2  s .c o  m*/

    if (!(key instanceof PublicKey)) {
        throw new InvalidKeyException("key must be PublicKey");
    }
    if (signature == null) {
        try {
            Provider p = (Provider) context.getProperty("org.jcp.xml.dsig.internal.dom.SignatureProvider");
            signature = (p == null) ? Signature.getInstance(getJCAAlgorithm())
                    : Signature.getInstance(getJCAAlgorithm(), p);
        } catch (NoSuchAlgorithmException nsae) {
            throw new XMLSignatureException(nsae);
        }
    }
    signature.initVerify((PublicKey) key);
    if (log.isDebugEnabled()) {
        log.debug("Signature provider:" + signature.getProvider());
        log.debug("verifying with key: " + key);
    }
    ((DOMSignedInfo) si).canonicalize(context, new SignerOutputStream(signature));

    try {
        Type type = getAlgorithmType();
        if (type == Type.DSA) {
            return signature.verify(convertXMLDSIGtoASN1(sig));
        } else if (type == Type.ECDSA) {
            return signature.verify(SignatureECDSA.convertXMLDSIGtoASN1(sig));
        } else {
            return signature.verify(sig);
        }
    } catch (IOException ioe) {
        throw new XMLSignatureException(ioe);
    }
}

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

public boolean validate(XMLValidateContext vc) throws XMLSignatureException {
    if (vc == null) {
        throw new NullPointerException("validateContext is null");
    }//from w w w . ja  v a  2  s .  co  m

    if (!(vc instanceof DOMValidateContext)) {
        throw new ClassCastException("validateContext must be of type DOMValidateContext");
    }

    if (validated) {
        return validationStatus;
    }

    // validate the signature
    boolean sigValidity = sv.validate(vc);
    if (!sigValidity) {
        validationStatus = false;
        validated = true;
        return validationStatus;
    }

    // validate all References
    @SuppressWarnings("unchecked")
    List<Reference> refs = this.si.getReferences();
    boolean validateRefs = true;
    for (int i = 0, size = refs.size(); validateRefs && i < size; i++) {
        Reference ref = refs.get(i);
        boolean refValid = ref.validate(vc);
        if (log.isDebugEnabled()) {
            log.debug("Reference[" + ref.getURI() + "] is valid: " + refValid);
        }
        validateRefs &= refValid;
    }
    if (!validateRefs) {
        if (log.isDebugEnabled()) {
            log.debug("Couldn't validate the References");
        }
        validationStatus = false;
        validated = true;
        return validationStatus;
    }

    // validate Manifests, if property set
    boolean validateMans = true;
    if (Boolean.TRUE.equals(vc.getProperty("org.jcp.xml.dsig.validateManifests"))) {
        for (int i = 0, size = objects.size(); validateMans && i < size; i++) {
            XMLObject xo = objects.get(i);
            @SuppressWarnings("unchecked")
            List<XMLStructure> content = xo.getContent();
            int csize = content.size();
            for (int j = 0; validateMans && j < csize; j++) {
                XMLStructure xs = content.get(j);
                if (xs instanceof Manifest) {
                    if (log.isDebugEnabled()) {
                        log.debug("validating manifest");
                    }
                    Manifest man = (Manifest) xs;
                    @SuppressWarnings("unchecked")
                    List<Reference> manRefs = man.getReferences();
                    int rsize = manRefs.size();
                    for (int k = 0; validateMans && k < rsize; k++) {
                        Reference ref = manRefs.get(k);
                        boolean refValid = ref.validate(vc);
                        if (log.isDebugEnabled()) {
                            log.debug("Manifest ref[" + ref.getURI() + "] is valid: " + refValid);
                        }
                        validateMans &= refValid;
                    }
                }
            }
        }
    }

    validationStatus = validateMans;
    validated = true;
    return validationStatus;
}