throws XWSSecurityException {
String algo = wssContext.getAlgorithmSuite().getSymmetricKeyAlgorithm();
AuthenticationTokenPolicy.UsernameTokenBinding untBinding = new AuthenticationTokenPolicy.UsernameTokenBinding();
String decodedSalt = token.getSalt();
if (decodedSalt == null) {
throw new XWSSecurityException("Salt retrieved from UsernameToken is null");
}
byte[] salt = null;
try {
salt = Base64.decode(decodedSalt);
} catch (Base64DecodingException ex) {
logger.log(Level.SEVERE, com.sun.xml.wss.logging.LogStringsMessages.WSS_0144_UNABLETO_DECODE_BASE_64_DATA(ex), ex);
throw new XWSSecurityException("exception during decoding the salt ");
}
String password = null;
try {
password = wssContext.getSecurityEnvironment().authenticateUser(wssContext.getExtraneousProperties(), token.getUsernameValue());
} catch (XWSSecurityException ex) {
throw new XWSSecurityException("exception during retrieving the password using the username");
}
if (password == null) {
throw new XWSSecurityException("Password retrieved from UsernameToken is null");
}
String iterate = token.getIterations();
if (iterate == null) {
throw new XWSSecurityException("Value of Iterations retrieved from UsernameToken is null");
}
int iterations = Integer.parseInt(iterate);
PasswordDerivedKey pdk = new PasswordDerivedKey();
SecretKey sKey = null;
byte[] verifySignature = null;
if ((purpose == Purpose.DECRYPT)) {
salt[0] = MessageConstants.VALUE_FOR_ENCRYPTION;
if (isSymmetric) {
try {
verifySignature = pdk.generate160BitKey(password, iterations, salt);
} catch (UnsupportedEncodingException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1381_ERROR_GENERATING_160_BITKEY(), ex);
throw new XWSSecurityException("error during generating 160 bit key ");
}
untBinding.setSecretKey(verifySignature);
sKey = untBinding.getSecretKey(SecurityUtil.getSecretKeyAlgorithm(algo));
untBinding.setSecretKey(sKey);
wssContext.setUsernameTokenBinding(untBinding);
byte[] secretKey = untBinding.getSecretKey().getEncoded();
SecretKey key = pdk.generate16ByteKeyforEncryption(secretKey);
sKey = key;
} else {
byte[] decSignature = null;
try {
decSignature = pdk.generate160BitKey(password, iterations, salt);
} catch (UnsupportedEncodingException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1381_ERROR_GENERATING_160_BITKEY(), ex);
throw new XWSSecurityException("error during generating 160 bit key ");
}
byte[] keyof128Bits = new byte[16];
for (int i = 0; i < 16; i++) {
keyof128Bits[i] = decSignature[i];
}
untBinding.setSecretKey(keyof128Bits);
sKey = untBinding.getSecretKey(SecurityUtil.getSecretKeyAlgorithm(algo));
untBinding.setSecretKey(sKey);
}
} else if (purpose == Purpose.VERIFY) {
salt[0] = MessageConstants.VALUE_FOR_SIGNATURE;
try {
verifySignature = pdk.generate160BitKey(password, iterations, salt);
} catch (UnsupportedEncodingException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1381_ERROR_GENERATING_160_BITKEY(), ex);
throw new XWSSecurityException("error during generating 160 bit key ");
}
untBinding.setSecretKey(verifySignature);
sKey = untBinding.getSecretKey(SecurityUtil.getSecretKeyAlgorithm(algo));
untBinding.setSecretKey(sKey);
//return sKey;
} else {
//handles RequiredDerivedKeys case
salt[0] = MessageConstants.VALUE_FOR_ENCRYPTION;
byte[] key = null;
try {
key = pdk.generate160BitKey(password, iterations, salt);
} catch (UnsupportedEncodingException ex) {
logger.log(Level.SEVERE, LogStringsMessages.WSS_1381_ERROR_GENERATING_160_BITKEY(), ex);
throw new XWSSecurityException("error during generating 160 bit key ");
}
byte[] sKeyof16ByteLength = new byte[16];
for (int i = 0; i < 16; i++) {
sKeyof16ByteLength[i] = key[i];
}