Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CXF-8981] Added unit tests for key agreement method #1715

Merged
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 @@ -171,4 +171,13 @@ public static void cleanup() throws IOException {
}
}
}

protected int getJDKVersion() {
try {
return Integer.getInteger("java.specification.version", 0);
} catch (NumberFormatException ex) {
// ignore
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class TestPwdCallback implements CallbackHandler {
passwords.put("myalias", "myAliasPassword");
passwords.put("alice", "alicePassword");
passwords.put("username", "myAliasPassword");
passwords.put("x448", "security");
passwords.put("x25519", "security");
passwords.put("secp256r1", "security");
passwords.put("secp384r1", "security");
passwords.put("secp521r1", "security");
}

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
import org.apache.wss4j.dom.engine.WSSecurityEngineResult;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
import org.apache.wss4j.dom.handler.WSHandlerResult;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.EncryptionConstants;

import org.junit.Assume;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -70,6 +73,9 @@
public class WSS4JInOutTest extends AbstractSecurityTest {

public WSS4JInOutTest() {
// add xenc11 and dsig11 namespaces
testUtilities.addNamespace("xenc11", EncryptionConstants.EncryptionSpec11NS);
testUtilities.addNamespace("dsig11", Constants.SignatureSpec11NS);
}

@Test
Expand Down Expand Up @@ -191,6 +197,86 @@ public void testEncryption() throws Exception {
);
}

@Test
public void testEncryptionWithAgreementMethodsX448() throws Exception {
Assume.assumeTrue(getJDKVersion() >= 16);
testEncryptionWithAgreementMethod("x448", "//dsig11:DEREncodedKeyValue");
}

@Test
public void testEncryptionWithAgreementMethodsX25519() throws Exception {
Assume.assumeTrue(getJDKVersion() >= 16);
testEncryptionWithAgreementMethod("x25519", "//dsig11:DEREncodedKeyValue");
}

@Test
public void testEncryptionWithAgreementMethodsECP256r1() throws Exception {
testEncryptionWithAgreementMethod("secp256r1", "//dsig11:ECKeyValue");
}

@Test
public void testEncryptionWithAgreementMethodsECP521r1() throws Exception {
testEncryptionWithAgreementMethod("secp521r1", "//dsig11:ECKeyValue");
}

/**
* Helper method to Test encryption using the specified agreement method with various keys
*/
public void testEncryptionWithAgreementMethod(String alias, String keyElement) throws Exception {

Map<String, Object> outProperties = new HashMap<>();
outProperties.put(ConfigurationConstants.ACTION, ConfigurationConstants.ENCRYPTION);
outProperties.put(ConfigurationConstants.ENC_PROP_FILE, "wss-ecdh.properties");
outProperties.put(ConfigurationConstants.USER, alias);
outProperties.put(ConfigurationConstants.ENC_KEY_TRANSPORT, WSS4JConstants.KEYWRAP_AES128);
outProperties.put(ConfigurationConstants.ENC_KEY_AGREEMENT_METHOD, WSS4JConstants.AGREEMENT_METHOD_ECDH_ES);

Map<String, Object> inProperties = new HashMap<>();
inProperties.put(ConfigurationConstants.ACTION, ConfigurationConstants.ENCRYPTION);
inProperties.put(ConfigurationConstants.DEC_PROP_FILE, "wss-ecdh.properties");
inProperties.put(ConfigurationConstants.PW_CALLBACK_REF, new TestPwdCallback());
// assertion of existence of elements
List<String> xpaths = new ArrayList<>();
xpaths.add(keyElement);
xpaths.add("//wsse:Security");
xpaths.add("//s:Body/xenc:EncryptedData");
xpaths.add("//xenc:AgreementMethod");
xpaths.add("//xenc11:KeyDerivationMethod");
xpaths.add("//xenc11:ConcatKDFParams");
xpaths.add("//xenc:OriginatorKeyInfo");
xpaths.add("//xenc:RecipientKeyInfo");

List<WSHandlerResult> handlerResults =
getResults(makeInvocation(outProperties, xpaths, inProperties));

assertNotNull(handlerResults);
assertSame(handlerResults.size(), 1);
//
// This should contain exactly 1 protection result
//
final java.util.List<WSSecurityEngineResult> protectionResults =
handlerResults.get(0).getResults();
assertNotNull(protectionResults);
assertSame(protectionResults.size(), 1);
//
// This result should contain a reference to the decrypted element,
// which should contain the soap:Body Qname
//
final java.util.Map<String, Object> result =
protectionResults.get(0);
final java.util.List<WSDataRef> protectedElements =
CastUtils.cast((List<?>)result.get(WSSecurityEngineResult.TAG_DATA_REF_URIS));
assertNotNull(protectedElements);
assertSame(protectedElements.size(), 1);
assertEquals(
protectedElements.get(0).getName(),
new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/envelope/",
"Body"
)
);
}

@Test
public void testEncryptedUsernameToken() throws Exception {
Map<String, Object> outProperties = new HashMap<>();
Expand Down
Loading
Loading