Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.wss4j.policy.model.AlgorithmSuite;
import org.apache.wss4j.policy.model.SecureConversationToken;
import org.apache.wss4j.policy.model.SupportingTokens;
import org.apache.xml.security.algorithms.JCEMapper;

/**
*
Expand Down Expand Up @@ -104,8 +105,10 @@ static String setupClient(STSClient client,
AlgorithmSuite suite = NegotiationUtils.getAlgorithmSuite(aim);
if (suite != null) {
client.setAlgorithmSuite(suite);
int x = suite.getAlgorithmSuiteType().getMaximumSymmetricKeyLength();
if (x < 256) {
// The secret must have exactly the length required by the encryption algorithm of the suite
int x = JCEMapper.getKeyLengthFromURI(suite.getAlgorithmSuiteType().getEncryption());
if (x >= suite.getAlgorithmSuiteType().getMinimumSymmetricKeyLength()
&& x <= suite.getAlgorithmSuiteType().getMaximumSymmetricKeyLength()) {
client.setKeySize(x);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.cxf.sts.token.delegation.TokenDelegationResponse;
import org.apache.cxf.sts.token.provider.TokenProvider;
import org.apache.cxf.sts.token.provider.TokenProviderParameters;
import org.apache.cxf.sts.token.provider.TokenProviderUtils;
import org.apache.cxf.sts.token.provider.TokenReference;
import org.apache.cxf.sts.token.realm.Relationship;
import org.apache.cxf.sts.token.realm.RelationshipResolver;
Expand Down Expand Up @@ -382,7 +383,7 @@ protected Element encryptSecret(

final SecretKey symmetricKey;
if (secret != null) {
symmetricKey = KeyUtils.prepareSecretKey(encryptionProperties.getEncryptionAlgorithm(), secret);
symmetricKey = TokenProviderUtils.createSecretKey(secret, keyRequirements, encryptionProperties);
} else {
KeyGenerator keyGen = KeyUtils.getKeyGenerator(encryptionProperties.getEncryptionAlgorithm());
symmetricKey = keyGen.generateKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ protected KeyInfoBean createKeyInfo(SubjectProviderParameters subjectProviderPar
}
Document doc = subjectProviderParameters.getDoc();
byte[] secret = subjectProviderParameters.getSecret();
return createEncryptedKeyKeyInfo(certs[0], secret, doc, encryptionProperties, crypto);
return createEncryptedKeyKeyInfo(certs[0], secret, doc, encryptionProperties, crypto,
keyRequirements);
} catch (WSSecurityException ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException(ex.getMessage(), ex);
Expand Down Expand Up @@ -327,7 +328,8 @@ protected static KeyInfoBean createEncryptedKeyKeyInfo(
byte[] secret,
Document doc,
EncryptionProperties encryptionProperties,
Crypto encryptionCrypto
Crypto encryptionCrypto,
KeyRequirements keyRequirements
) throws WSSecurityException {
KeyInfoBean keyInfo = new KeyInfoBean();

Expand All @@ -339,7 +341,7 @@ protected static KeyInfoBean createEncryptedKeyKeyInfo(

final SecretKey symmetricKey;
if (secret != null) {
symmetricKey = KeyUtils.prepareSecretKey(encryptionProperties.getEncryptionAlgorithm(), secret);
symmetricKey = TokenProviderUtils.createSecretKey(secret, keyRequirements, encryptionProperties);
} else {
KeyGenerator keyGen = KeyUtils.getKeyGenerator(encryptionProperties.getEncryptionAlgorithm());
symmetricKey = keyGen.generateKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.cxf.ws.security.wss4j.WSS4JUtils;
import org.apache.wss4j.common.ConfigurationConstants;
import org.apache.wss4j.common.WSEncryptionPart;
import org.apache.wss4j.common.WSS4JConstants;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.common.util.KeyUtils;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
Expand All @@ -58,6 +59,11 @@ public final class TokenProviderUtils {

private static final Logger LOG = LogUtils.getL7dLogger(TokenProviderUtils.class);

private static final Map<Integer, String> AES_ALGORITHMS = Map.of(
16, WSS4JConstants.AES_128,
24, WSS4JConstants.AES_192,
32, WSS4JConstants.AES_256);

private TokenProviderUtils() {
// complete
}
Expand Down Expand Up @@ -104,6 +110,36 @@ public static String extractAddressFromParticipantsEPR(Object participants) {
return null;
}

/**
* Create a SecretKey for the given issued secret. The secret was sized according to the KeySize and
* EncryptWith values of the request, so the algorithm associated with it must match that length
* rather than simply being the algorithm that the STS is configured with.
*/
public static SecretKey createSecretKey(
byte[] secret,
KeyRequirements keyRequirements,
EncryptionProperties encryptionProperties
) throws WSSecurityException {
String encryptWith = keyRequirements.getEncryptWith();
if (encryptWith == null
|| !encryptionProperties.getAcceptedEncryptionAlgorithms().contains(encryptWith)
|| KeyUtils.getKeyLength(encryptWith) != secret.length) {
encryptWith = encryptionProperties.getEncryptionAlgorithm();
}
if (KeyUtils.getKeyLength(encryptWith) != secret.length) {
// Fall back on an algorithm that matches the length of the issued secret
encryptWith = AES_ALGORITHMS.get(secret.length);
if (encryptWith == null) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE);
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Issued secret does not match the configured encryption algorithm, using: "
+ encryptWith);
}
}
return KeyUtils.prepareSecretKey(encryptWith, secret);
}

/**
* Encrypt a Token element using the given arguments.
*/
Expand Down
Loading