/*
* First get the SOAP envelope as document, then create a security
* header and insert into the document (Envelope)
*/
Document doc = unsignedEnvelope.getAsDocument();
SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(doc
.getDocumentElement());
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
Vector sigParts = new Vector();
Vector encPartsInternal = new Vector();
Vector encPartsExternal = new Vector();
/*
* Check is a timestamp is required. If yes create one and add its Id to
* signed parts. According to WSP a timestamp must be signed
*/
WSSecTimestamp timestamp = null;
if (wpd.isIncludeTimestamp()) {
timestamp = new WSSecTimestamp();
timestamp.prepare(doc);
sigParts.add(new WSEncryptionPart(timestamp.getId()));
}
/*
* Check for a recipient token. If one is avaliable use it as token to
* encrypt data to the recipient. This is according to WSP
* specification. Most of the data is extracted from the
* WSS4JPolicyData, only the user info (name/alias of the certificate in
* the keystore) must be provided by some other means.
*/
WSSecEncrypt recEncrypt = null;
WSS4JPolicyToken recToken = null;
if ((recToken = wpd.getRecipientToken()) != null) {
recEncrypt = new WSSecEncrypt();
recEncrypt.setUserInfo("wss4jcert");
recEncrypt.setKeyIdentifierType(recToken.getKeyIdentifier());
recEncrypt.setSymmetricEncAlgorithm(recToken.getEncAlgorithm());
recEncrypt.setKeyEnc(recToken.getEncTransportAlgorithm());
recEncrypt.prepare(doc, cryptoSKI);
}
/*
* Check for an initiator token. If one is avaliable use it as token to
* sign data. This is according to WSP specification. Most of the data
* is extracted from the WSS4JPolicyData, only the user info (name/alias
* of the certificate in the keystore) must be provided by some other
* means.
*
* If SignatureProtection is enabled add the signature to the encrypted
* parts vector. In any case the signature must be in the internal
* ReferenceList (this list is a child of the EncryptedKey element).
*
* If TokenProtection is enabled add an appropriate signature reference.
*
* TODO Check / enable for STRTransform
*/
WSSecSignature iniSignature = null;
WSS4JPolicyToken iniToken = null;
if ((iniToken = wpd.getInitiatorToken()) != null) {
iniSignature = new WSSecSignature();
iniSignature.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e",
"security");
iniSignature.setKeyIdentifierType(iniToken.getKeyIdentifier());
iniSignature.setSignatureAlgorithm(iniToken.getSigAlgorithm());
iniSignature.prepare(doc, crypto, secHeader);
if (wpd.isSignatureProtection()) {
encPartsInternal.add(new WSEncryptionPart(iniSignature.getId(),
"Element"));
}
if (wpd.isTokenProtection()) {
sigParts.add(new WSEncryptionPart("Token", null, null));
}
}
Element body = WSSecurityUtil.findBodyElement(doc, soapConstants);
if (body == null) {
System.out
.println("No SOAP Body found - illegal message structure. Processing terminated");
return;
}
WSEncryptionPart bodyPart = new WSEncryptionPart("Body", soapConstants
.getEnvelopeURI(), "Content");
/*
* Check the protection order. If Encrypt before signing then first take
* all parts and elements to encrypt and encrypt them. Take their ids