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");
}
}