Package javax.xml.crypto.dsig

Examples of javax.xml.crypto.dsig.XMLSignatureException


            byte[] originalDigest = ref.getDigestValue();
            ref.digest(xMLValidateContext);
            byte[] calculatedDigest = ref.getDigestValue();
            if ( ! Arrays.equals(originalDigest, calculatedDigest) ) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION("Signature digest values mismatch"));
                throw new XMLSignatureException(LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION("Signature digest values mismatch"));

            }
        }
        si.setSignedInfo(_streamSI);

        // si.setCanonicalizedSI(signedInfoBytes);
       

        KeySelectorResult keySelectoResult = null;
        try {
            sm = si.getSignatureMethod();
            if(verificationKey == null){
                keySelectoResult = xMLValidateContext.getKeySelector().select(getKeyInfo(),KeySelector.Purpose.VERIFY,sm,xMLValidateContext);
                verificationKey = keySelectoResult.getKey();
            }
            if (verificationKey == null) {
                throw new XMLSignatureException("The KeySelector"+ xMLValidateContext.getKeySelector()+ " did not " +
                          "find the key used for signature verification");
            }

            if(_sp == null){
                _sp = new SignatureProcessor();
            }

            try {
                String signatureAlgo = sm.getAlgorithm();
                String algo = getRSASignatureAlgorithm(signatureAlgo);
                if (algo != null) {
                    return _sp.verifyRSASignature(verificationKey, si, getSignatureValue().getValue(), algo);
                } else if (signatureAlgo.equals(SignatureMethod.DSA_SHA1)) {
                    return _sp.verifyDSASignature(verificationKey, si, getSignatureValue().getValue());
                } else if (signatureAlgo.equals(SignatureMethod.HMAC_SHA1)) {
                    SignatureMethodParameterSpec params = (SignatureMethodParameterSpec)sm.getParameterSpec();

                    int outputLength = -1;

                    if (params != null) {
                        if (!(params instanceof HMACParameterSpec)) {
                            throw new XMLSignatureException ("SignatureMethodParameterSpec must be of type HMACParameterSpec");

                        }
                        outputLength = ((HMACParameterSpec) params).getOutputLength();
                    }
                    return _sp.verifyHMACSignature(verificationKey,si,getSignatureValue().getValue(), outputLength);
                } else {
                    throw new XMLSignatureException("Unsupported signature algorithm found");
                }
            } catch (InvalidKeyException ex) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION(ex));
                throw new XMLSignatureException(LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION(ex));
            } catch (SignatureException ex) {
                logger.log(Level.SEVERE, LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION("Signature digest values mismatch"));
                throw new XMLSignatureException(LogStringsMessages.WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION(ex));
            }
        } catch (KeySelectorException kse) {
            throw new XMLSignatureException("Cannot find verification key", kse);
        }
        //return false;
    }
View Full Code Here


        try {
            sm = si.getSignatureMethod();
            keySelectoResult = xMLSignContext.getKeySelector().select(getKeyInfo(),KeySelector.Purpose.SIGN,sm,xMLSignContext);
            signingKey = keySelectoResult.getKey();
            if (signingKey == null) {
                throw new XMLSignatureException("The KeySelector"+ xMLSignContext.getKeySelector()+ " did not " +
                          "find the key used for signing");
            }
        } catch (KeySelectorException kse) {
            throw new XMLSignatureException("Cannot find signing key", kse);
        }

        if(_sp == null){
            try {
                JAXBContext jc = JAXBUtil.getJAXBContext();
                _sp = new SignatureProcessor();
                _sp.setJAXBContext(jc);
                _sp.setCryptoContext(xMLSignContext);
            } catch (Exception ex) {
                throw new XMLSignatureException(ex);
            }
        }

        String signatureAlgo = sm.getAlgorithm();
        //SignatureValue sv=getSignatureValue();
        String algo = getRSASignatureAlgorithm(signatureAlgo);
        if(algo != null){
             try {
                com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue sigValue = new com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue();
                sigValue.setValue(_sp.performRSASign(signingKey,signedInfo,algo));
                setSignatureValue(sigValue);
                //((SignatureValueType)getSignatureValue()).setValue(_sp.performRSASign(signingKey,signedInfo));
                //((SignatureValueType)sv).setValue(_sp.performRSASign(signingKey,signedInfo));
            } catch (InvalidKeyException ex) {
                throw new XMLSignatureException(ex);
            }
        } else if(signatureAlgo.equals(SignatureMethod.DSA_SHA1)){
            try {
                com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue sigValue = new com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue();
                sigValue.setValue(_sp.performDSASign(signingKey,signedInfo));
                setSignatureValue(sigValue);
               
                //((SignatureValueType)sv).setValue(_sp.performDSASign(signingKey,signedInfo));

            } catch (InvalidKeyException ex) {
                throw new XMLSignatureException(ex);
            }

        } else if ( signatureAlgo.equals(SignatureMethod.HMAC_SHA1)) {
            SignatureMethodParameterSpec params = (SignatureMethodParameterSpec)sm.getParameterSpec();
            int outputLength = -1;
            if (params != null) {
                if (!(params instanceof HMACParameterSpec)) {
                    throw new XMLSignatureException
                              ("SignatureMethodParameterSpec must be of type HMACParameterSpec");
                }
                outputLength = ((HMACParameterSpec) params).getOutputLength();
            }

            try{
                com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue sigValue = new com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue();
                sigValue.setValue(_sp.performHMACSign(signingKey,signedInfo, outputLength));
                setSignatureValue(sigValue);
            } catch (InvalidKeyException ex) {
                throw new XMLSignatureException(ex);
            }
        } else {
             throw new XMLSignatureException("Unsupported signature algorithm found");
        }
    }
View Full Code Here

                logger.log(Level.FINE, "Digest Algorithm is "+ this.getDigestMethod().getAlgorithm());
                logger.log(Level.FINE, "Mapped Digest Algorithm is "+ algo);
            }
            _md = MessageDigest.getInstance(algo);
        } catch (NoSuchAlgorithmException nsae) {
            throw new XMLSignatureException(nsae);
        }
        dos = new DigesterOutputStream(_md);
        return dos;
    }
View Full Code Here

                }
                _md = MessageDigest.getInstance(algo);
               
            } catch (NoSuchAlgorithmException nsae) {
                logger.log(Level.SEVERE,LogStringsMessages.WSS_1760_DIGEST_INIT_ERROR(),nsae);
                throw new XMLSignatureException(nsae);
            }
        }
        _md.reset();
        DigesterOutputStream dos;
       
        //Boolean cache = (Boolean)context.getProperty("javax.xml.crypto.dsig.cacheReference");
       
        dos = new DigesterOutputStream(_md);
        OutputStream os = new UnsyncBufferedOutputStream(dos);
        Data data = dereferencedData;
        if ( transforms != null ) {
            List<Transform> transformList = ((TransformsType)transforms).getTransform();
            if ( transformList != null ) {
                for (int i = 0, size = transformList.size(); i < size; i++) {
                    Transform transform = (Transform) transformList.get(i);
                    try {
                        if (i < size - 1) {
                            data = transform.transform(data, context);
                        } else {
                            data = transform.transform(data, context, os);
                        }
                    } catch (TransformException te) {
                        logger.log(Level.SEVERE,LogStringsMessages.WSS_1759_TRANSFORM_ERROR(te.getMessage()),te);
                        throw new XMLSignatureException(te);
                    }
                }
            }
        }
       
        try {
            os.flush();
            dos.flush();
        } catch (IOException ex) {
            logger.log(Level.SEVERE,LogStringsMessages.WSS_1761_TRANSFORM_IO_ERROR(),ex);
            throw new XMLSignatureException(ex);
        }
       
       
        return dos.getDigestValue();
    }
View Full Code Here

        URIDereferencer deref = context.getURIDereferencer();
       
        try {
            data = deref.dereference(this, context);
        } catch (URIReferenceException ure) {
            throw new XMLSignatureException(ure);
        }
        return data;
    }
View Full Code Here

TOP

Related Classes of javax.xml.crypto.dsig.XMLSignatureException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.