OnBehalfOf obo = fact.createOnBehalfOf(oboToken);
rst.setOnBehalfOf(obo);
}
// Handle ActAs token
Token actAsToken = (Token)stsConfig.getOtherOptions().get(STSIssuedTokenConfiguration.ACT_AS);
if (actAsToken != null){
ActAs actAs = fact.createActAs(actAsToken);
rst.setActAs(actAs);
}
// Handle LifeTime requirement
Integer lf = (Integer)stsConfig.getOtherOptions().get(STSIssuedTokenConfiguration.LIFE_TIME);
if(lf != null){
// Create Lifetime
long lfValue = lf.longValue();
if (lfValue > 0){
long currentTime = WSTrustUtil.getCurrentTimeWithOffset();
Lifetime lifetime = WSTrustUtil.createLifetime(currentTime, lfValue, wstVer);
rst.setLifetime(lifetime);
}
}
String tokenType = null;
String keyType = null;
long keySize = -1;
String signWith = null;
String encryptWith = null;
String signatureAlgorithm = null;
String encryptionAlgorithm = null;
String keyWrapAlgorithm = null;
String canonicalizationAlgorithm = null;
Claims claims = null;
if (wstVer.getNamespaceURI().equals(WSTrustVersion.WS_TRUST_13.getNamespaceURI())){
SecondaryIssuedTokenParameters sitp = stsConfig.getSecondaryIssuedTokenParameters();
if (sitp != null){
SecondaryParameters sp = fact.createSecondaryParameters();
tokenType = sitp.getTokenType();
if (tokenType != null){
sp.setTokenType(URI.create(tokenType));
}
keyType = sitp.getKeyType();
if (keyType != null){
sp.setKeyType(URI.create(keyType));
}
keySize = sitp.getKeySize();
if (keySize > 0){
sp.setKeySize(keySize);
}
/*
encryptWith = sitp.getEncryptWith();
if (encryptWith != null){
sp.setEncryptWith(URI.create(encryptWith));
}
signWith = sitp.getSignWith();
if (signWith != null){
sp.setSignWith(URI.create(signWith));
}
*/
signatureAlgorithm = sitp.getSignatureAlgorithm();
if (signatureAlgorithm != null){
sp.setSignatureAlgorithm(URI.create(signatureAlgorithm));
}
encryptionAlgorithm = sitp.getEncryptionAlgorithm();
if (encryptionAlgorithm != null){
sp.setEncryptionAlgorithm(URI.create(encryptionAlgorithm));
}
canonicalizationAlgorithm = sitp.getCanonicalizationAlgorithm();
if (canonicalizationAlgorithm != null){
sp.setCanonicalizationAlgorithm(URI.create(canonicalizationAlgorithm));
}
keyWrapAlgorithm = sitp.getKeyWrapAlgorithm();
if(keyWrapAlgorithm != null){
sp.setKeyWrapAlgorithm(URI.create(keyWrapAlgorithm));
}
claims = sitp.getClaims();
if (claims != null){
sp.setClaims(claims);
}
rst.setSecondaryParameters(sp);
}
}
if (tokenType == null){
tokenType = stsConfig.getTokenType();
if (tokenType != null){
rst.setTokenType(URI.create(tokenType));
}
}
if (keyType == null){
keyType = stsConfig.getKeyType();
if (keyType != null){
rst.setKeyType(URI.create(keyType));
}
}
if (keySize < 1){
keySize = stsConfig.getKeySize();
if (keySize > 0){
rst.setKeySize(keySize);
}
}
/*
if (encryptWith == null){
encryptWith = stsConfig.getEncryptWith();
if (encryptWith != null){
rst.setEncryptWith(URI.create(encryptWith));
}
}
if (signWith == null){
signWith = stsConfig.getSignWith();
if (signWith != null){
rst.setSignWith(URI.create(signWith));
}
}
*/
if (signatureAlgorithm == null){
signatureAlgorithm = stsConfig.getSignatureAlgorithm();
if (signatureAlgorithm != null){
rst.setSignatureAlgorithm(URI.create(signatureAlgorithm));
}
}
if (encryptionAlgorithm == null){
encryptionAlgorithm = stsConfig.getEncryptionAlgorithm();
if (encryptionAlgorithm != null){
rst.setEncryptionAlgorithm(URI.create(encryptionAlgorithm));
}
}
if (canonicalizationAlgorithm == null){
canonicalizationAlgorithm = stsConfig.getCanonicalizationAlgorithm();
if (canonicalizationAlgorithm != null){
rst.setCanonicalizationAlgorithm(URI.create(canonicalizationAlgorithm));
}
}
if (claims == null){
claims = stsConfig.getClaims();
if (claims != null){
rst.setClaims(fact.createClaims(claims));
}
}
int len = 32;
if (keySize > 0){
len = (int)keySize/8;
}
if (wstVer.getSymmetricKeyTypeURI().equals(keyType)){
final SecureRandom secRandom = new SecureRandom();
final byte[] nonce = new byte[len];
secRandom.nextBytes(nonce);
final BinarySecret binarySecret = fact.createBinarySecret(nonce, wstVer.getNonceBinarySecretTypeURI());
final Entropy entropy = fact.createEntropy(binarySecret);
rst.setEntropy(entropy);
rst.setComputedKeyAlgorithm(URI.create(wstVer.getCKPSHA1algorithmURI()));
}else if (wstVer.getPublicKeyTypeURI().equals(keyType) && keySize > 1 ){
// Create a RSA key pairs for use with UseKey
KeyPairGenerator kpg;
try{
kpg = KeyPairGenerator.getInstance("RSA");
//RSAKeyGenParameterSpec rsaSpec = new RSAKeyGenParameterSpec((int)keySize, RSAKeyGenParameterSpec.F0);
//kpg.initialize(rsaSpec);
}catch (NoSuchAlgorithmException ex){
throw new WSTrustException("Unable to create key pairs for UseKey", ex);
}
//catch (InvalidAlgorithmParameterException ex){
// throw new WSTrustException("Unable to create key pairs for UseKey", ex);
//}
kpg.initialize((int)keySize);
KeyPair keyPair = kpg.generateKeyPair();
// Create the Sig attribute Value for UseKey
// String sig = "uuid-" + UUID.randomUUID().toString();
// Create the UseKey element in RST
KeyInfo keyInfo = createKeyInfo(keyPair.getPublic());
final DocumentBuilderFactory docFactory = WSITXMLFactory.createDocumentBuilderFactory(WSITXMLFactory.DISABLE_SECURE_PROCESSING);
Document doc = null;
try{
doc = docFactory.newDocumentBuilder().newDocument();
keyInfo.marshal(new DOMStructure(doc), null);
}catch(ParserConfigurationException ex){
log.log(Level.SEVERE,
LogStringsMessages.WST_0039_ERROR_CREATING_DOCFACTORY(), ex);
throw new WSTrustException(LogStringsMessages.WST_0039_ERROR_CREATING_DOCFACTORY(), ex);
}catch(MarshalException ex){
log.log(Level.SEVERE,
LogStringsMessages.WST_0039_ERROR_CREATING_DOCFACTORY(), ex);
throw new WSTrustException(LogStringsMessages.WST_0039_ERROR_CREATING_DOCFACTORY(), ex);
}
Token token = new GenericToken(doc.getDocumentElement());
UseKey useKey = fact.createUseKey(token, null);
rst.setUseKey(useKey);
// Put the key pair and the sig in the STSConfiguration
stsConfig.getOtherOptions().put(WSTrustConstants.USE_KEY_RSA_KEY_PAIR, keyPair);