int inclusion = token.getInclusion();
org.apache.rahas.Token tok = null;
try {
tok = rmd.getTokenStorage().getToken(id);
} catch (TrustException e) {
throw new RampartException("errorExtractingToken",
new String[]{id} ,e);
}
boolean tokenIncluded = false;
if(inclusion == SPConstants.INCLUDE_TOEKN_ALWAYS ||
((inclusion == SPConstants.INCLUDE_TOEKN_ALWAYS_TO_RECIPIENT
|| inclusion == SPConstants.INCLUDE_TOKEN_ONCE)
&& rmd.isInitiator())) {
//Add the token
rmd.getSecHeader().getSecurityHeader().appendChild(
doc.importNode((Element) tok.getToken(), true));
tokenIncluded = true;
}
Vector sigParts = new Vector();
if(this.timestampElement != null){
sigParts.add(new WSEncryptionPart(rmd.getTimestampId()));
}
if(rpd.isTokenProtection() && tokenIncluded) {
sigParts.add(new WSEncryptionPart(id));
}
if(signdParts != null) {
if(signdParts.isBody()) {
SOAPEnvelope env = rmd.getMsgContext().getEnvelope();
sigParts.add(new WSEncryptionPart(RampartUtil.addWsuIdToElement(env.getBody())));
}
ArrayList headers = signdParts.getHeaders();
for (Iterator iterator = headers.iterator(); iterator.hasNext();) {
Header header = (Header) iterator.next();
WSEncryptionPart wep = new WSEncryptionPart(header.getName(),
header.getNamespace(),
"Content");
sigParts.add(wep);
}
}
//check for derived keys
AlgorithmSuite algorithmSuite = rpd.getAlgorithmSuite();
if(token.isDerivedKeys()) {
//Create a derived key and add
try {
//Do Signature with derived keys
WSSecDKSign dkSign = new WSSecDKSign();
// Setting the AttachedReference or the UnattachedReference according to the flag
OMElement ref;
if (tokenIncluded == true) {
ref = tok.getAttachedReference();
} else {
ref = tok.getUnattachedReference();
}
if(ref != null) {
dkSign.setExternalKey(tok.getSecret(), (Element)
doc.importNode((Element) ref, true));
} else {
dkSign.setExternalKey(tok.getSecret(), tok.getId());
}
//Set the algo info
dkSign.setSignatureAlgorithm(algorithmSuite.getSymmetricSignature());
dkSign.setDerivedKeyLength(algorithmSuite.getSignatureDerivedKeyLength());
dkSign.prepare(doc);
dkSign.appendDKElementToHeader(rmd.getSecHeader());
dkSign.setParts(sigParts);
dkSign.addReferencesToSign(sigParts, rmd.getSecHeader());
//Do signature
dkSign.computeSignature();
dkSign.appendSigToHeader(rmd.getSecHeader());
return dkSign.getSignatureValue();
} catch (ConversationException e) {
throw new RampartException(
"errorInDerivedKeyTokenSignature", e);
} catch (WSSecurityException e) {
throw new RampartException(
"errorInDerivedKeyTokenSignature", e);
}
} else {
try {
WSSecSignature sig = new WSSecSignature();
sig.setWsConfig(rmd.getConfig());
sig.setCustomTokenId(tok.getId().substring(1));
sig.setCustomTokenValueType(WSConstants.WSS_SAML_NS +
WSConstants.SAML_ASSERTION_ID);
sig.setSecretKey(tok.getSecret());
sig.setSignatureAlgorithm(algorithmSuite.getAsymmetricSignature());
sig.setSignatureAlgorithm(algorithmSuite.getSymmetricSignature());
sig.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
sig.prepare(rmd.getDocument(), RampartUtil.getSignatureCrypto(rpd
.getRampartConfig(), rmd.getCustomClassLoader()),
rmd.getSecHeader());
sig.setParts(sigParts);
sig.addReferencesToSign(sigParts, rmd.getSecHeader());
//Do signature
sig.computeSignature();
//Add elements to header
this.setInsertionLocation(RampartUtil.insertSiblingAfter(
rmd,
this.getInsertionLocation(),
sig.getSignatureElement()));
return sig.getSignatureValue();
} catch (WSSecurityException e) {
throw new RampartException("errorInSignatureWithACustomToken", e);
}
}
}