Search in sources :

Example 1 with SignerOutputStream

use of org.jcp.xml.dsig.internal.SignerOutputStream in project jdk8u_jdk by JetBrains.

the class DOMSignatureMethod method verify.

boolean verify(Key key, SignedInfo si, byte[] sig, XMLValidateContext context) throws InvalidKeyException, SignatureException, XMLSignatureException {
    if (key == null || si == null || sig == null) {
        throw new NullPointerException();
    }
    if (!(key instanceof PublicKey)) {
        throw new InvalidKeyException("key must be PublicKey");
    }
    checkKeySize(context, key);
    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.isLoggable(java.util.logging.Level.FINE)) {
        log.log(java.util.logging.Level.FINE, "Signature provider:" + signature.getProvider());
        log.log(java.util.logging.Level.FINE, "verifying with key: " + key);
    }
    ((DOMSignedInfo) si).canonicalize(context, new SignerOutputStream(signature));
    try {
        Type type = getAlgorithmType();
        if (type == Type.DSA) {
            int size = ((DSAKey) key).getParams().getQ().bitLength();
            return signature.verify(JavaUtils.convertDsaXMLDSIGtoASN1(sig, size / 8));
        } else if (type == Type.ECDSA) {
            return signature.verify(SignatureECDSA.convertXMLDSIGtoASN1(sig));
        } else {
            return signature.verify(sig);
        }
    } catch (IOException ioe) {
        throw new XMLSignatureException(ioe);
    }
}
Also used : SignerOutputStream(org.jcp.xml.dsig.internal.SignerOutputStream) DSAKey(java.security.interfaces.DSAKey) IOException(java.io.IOException)

Example 2 with SignerOutputStream

use of org.jcp.xml.dsig.internal.SignerOutputStream in project jdk8u_jdk by JetBrains.

the class DOMSignatureMethod method sign.

byte[] sign(Key key, SignedInfo si, XMLSignContext context) throws InvalidKeyException, XMLSignatureException {
    if (key == null || si == null) {
        throw new NullPointerException();
    }
    if (!(key instanceof PrivateKey)) {
        throw new InvalidKeyException("key must be PrivateKey");
    }
    checkKeySize(context, key);
    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.initSign((PrivateKey) key);
    if (log.isLoggable(java.util.logging.Level.FINE)) {
        log.log(java.util.logging.Level.FINE, "Signature provider:" + signature.getProvider());
        log.log(java.util.logging.Level.FINE, "Signing with key: " + key);
    }
    ((DOMSignedInfo) si).canonicalize(context, new SignerOutputStream(signature));
    try {
        Type type = getAlgorithmType();
        if (type == Type.DSA) {
            int size = ((DSAKey) key).getParams().getQ().bitLength();
            return JavaUtils.convertDsaASN1toXMLDSIG(signature.sign(), size / 8);
        } else if (type == Type.ECDSA) {
            return SignatureECDSA.convertASN1toXMLDSIG(signature.sign());
        } else {
            return signature.sign();
        }
    } catch (SignatureException se) {
        throw new XMLSignatureException(se);
    } catch (IOException ioe) {
        throw new XMLSignatureException(ioe);
    }
}
Also used : SignerOutputStream(org.jcp.xml.dsig.internal.SignerOutputStream) IOException(java.io.IOException) DSAKey(java.security.interfaces.DSAKey)

Aggregations

IOException (java.io.IOException)2 DSAKey (java.security.interfaces.DSAKey)2 SignerOutputStream (org.jcp.xml.dsig.internal.SignerOutputStream)2