/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.coheigea.cxf.jaxrs.jwe;
import java.net.URL;
import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.apache.coheigea.cxf.jaxrs.json.common.Number;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.rs.security.jose.jaxrs.JweWriterInterceptor;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.junit.AfterClass;
import org.junit.BeforeClass;
/**
* Test JAX-RS JSON Encryption. Only the client -> service request is encrypted, not the response.
*/
public class JWETest extends AbstractBusClientServerTestBase {
private static final String PORT = allocatePort(Server.class);
private static final String PORT2 = allocatePort(Server.class, 2);
private static final String PORT3 = allocatePort(Server.class, 3);
private static final String PORT4 = allocatePort(Server.class, 4);
private static final String PORT5 = allocatePort(Server.class, 5);
private static final String PORT6 = allocatePort(Server.class, 6);
@BeforeClass
public static void startServers() throws Exception {
assertTrue(
"Server failed to launch",
// run the server in the same process
// set this to false to fork
launchServer(Server.class, true)
);
Security.addProvider(new BouncyCastleProvider());
}
@AfterClass
public static void cleanup() throws Exception {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
}
@org.junit.Test
public void testEncryptionProperties() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.encryption.properties", "clientEncKeystore.properties");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
@org.junit.Test
public void testRSAEncryption() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jks");
properties.put("rs.security.keystore.password", "sspass");
properties.put("rs.security.keystore.alias", "myservicekey");
properties.put("rs.security.keystore.file", "servicestore.jks");
properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
// TODO
@org.junit.Test
@org.junit.Ignore
public void testECEncryption() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT2 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jks");
properties.put("rs.security.keystore.password", "security");
properties.put("rs.security.keystore.alias", "ECDSA");
properties.put("rs.security.keystore.file", "ecdsa.jks");
properties.put("rs.security.encryption.key.algorithm", "ECDH-ES+A128KW");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
@org.junit.Test
public void testEncryptingXMLPayload() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT3 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/xml").accept("application/xml");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.encryption.properties", "clientEncKeystore.properties");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
@org.junit.Test
public void testRSAv15Encryption() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT4 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jks");
properties.put("rs.security.keystore.password", "sspass");
properties.put("rs.security.keystore.alias", "myservicekey");
properties.put("rs.security.keystore.file", "servicestore.jks");
properties.put("rs.security.encryption.key.algorithm", "RSA1_5");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
@org.junit.Test
public void testAESKWEncryption() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT5 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jwk");
properties.put("rs.security.keystore.alias", "AesWrapKey");
properties.put("rs.security.keystore.file", "jwk.txt");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
// Include the cert in the "x5c" header
@org.junit.Test
public void testEncryptionCertificateTest() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT6 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jks");
properties.put("rs.security.keystore.password", "sspass");
properties.put("rs.security.keystore.alias", "myservicekey");
properties.put("rs.security.keystore.file", "servicestore.jks");
properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
properties.put("rs.security.encryption.include.cert", "true");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
// Include the cert digest in the "x5t" header
@org.junit.Test
public void testEncryptionCertificateSha1Test() throws Exception {
URL busFile = JWETest.class.getResource("cxf-client.xml");
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
providers.add(new JweWriterInterceptor());
String address = "http://localhost:" + PORT6 + "/doubleit/services";
WebClient client =
WebClient.create(address, providers, busFile.toString());
client.type("application/json").accept("application/json");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("rs.security.keystore.type", "jks");
properties.put("rs.security.keystore.password", "sspass");
properties.put("rs.security.keystore.alias", "myservicekey");
properties.put("rs.security.keystore.file", "servicestore.jks");
properties.put("rs.security.encryption.key.algorithm", "RSA-OAEP");
properties.put("rs.security.encryption.content.algorithm", "A128CBC-HS256");
properties.put("rs.security.encryption.include.cert.sha1", "true");
WebClient.getConfig(client).getRequestContext().putAll(properties);
Number numberToDouble = new Number();
numberToDouble.setDescription("This is the number to double");
numberToDouble.setNumber(25);
Response response = client.post(numberToDouble);
assertEquals(response.getStatus(), 200);
assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
}