}
if (signatureToken == null) {
signatureToken = reqData.getSignatureToken();
}
WSSecSignature sign = new WSSecSignature(reqData.getWssConfig());
sign.setCustomTokenValueType(WSConstants.USERNAMETOKEN_NS + "#UsernameToken");
sign.setCustomTokenId(builder.getId());
sign.setSecretKey(builder.getDerivedKey());
sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
if (signatureToken.getDigestAlgorithm() != null) {
sign.setDigestAlgo(signatureToken.getDigestAlgorithm());
}
if (signatureToken.getSignatureAlgorithm() != null) {
sign.setSignatureAlgorithm(signatureToken.getSignatureAlgorithm());
} else {
sign.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
}
sign.prepare(doc, null, reqData.getSecHeader());
// prepend in this order: first the Signature Element and then the
// UsernameToken Element. This way the server gets the UsernameToken
// first, can check it and are prepared to compute the Signature key.
// sign.prependToHeader(reqData.getSecHeader());
// builder.prependToHeader(reqData.getSecHeader());
List<WSEncryptionPart> parts = null;
if (signatureToken.getParts().size() > 0) {
parts = signatureToken.getParts();
} else {
SOAPConstants soapConstants = reqData.getSoapConstants();
if (soapConstants == null) {
soapConstants = WSSecurityUtil.getSOAPConstants(doc.getDocumentElement());
}
parts = new ArrayList<WSEncryptionPart>();
WSEncryptionPart encP =
new WSEncryptionPart(WSConstants.ELEM_BODY, soapConstants.getEnvelopeURI(), "Content");
parts.add(encP);
}
List<javax.xml.crypto.dsig.Reference> referenceList =
sign.addReferencesToSign(parts, reqData.getSecHeader());
try {
sign.computeSignature(referenceList);
reqData.getSignatureValues().add(sign.getSignatureValue());
} catch (WSSecurityException e) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE,
"empty", e,
"WSHandler: Error during UsernameTokenSignature"
);