Search in sources :

Example 16 with MessageSigner

use of org.apache.cxf.rs.security.httpsignature.MessageSigner in project cxf by apache.

the class JAXRSHTTPSignatureTest method testNonMatchingSignatureAlgorithm.

// 
// Negative tests
// 
@Test
public void testNonMatchingSignatureAlgorithm() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateSignatureInterceptor signatureFilter = new CreateSignatureInterceptor();
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    MessageSigner messageSigner = new MessageSigner("rsa-sha512", keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsig/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 17 with MessageSigner

use of org.apache.cxf.rs.security.httpsignature.MessageSigner in project cxf by apache.

the class JAXRSHTTPSignatureTest method testEmptyDigest.

@Test
public void testEmptyDigest() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateDigestTestInterceptor signatureFilter = new CreateDigestTestInterceptor();
    signatureFilter.setEmptyDigestValue(true);
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    MessageSigner messageSigner = new MessageSigner(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsig/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 18 with MessageSigner

use of org.apache.cxf.rs.security.httpsignature.MessageSigner in project cxf by apache.

the class AbstractSignatureOutFilter method createMessageSigner.

private MessageSigner createMessageSigner() {
    Properties props = KeyManagementUtils.loadSignatureOutProperties();
    if (props == null) {
        throw new SignatureException("Signature properties are not configured correctly");
    }
    Message m = PhaseInterceptorChain.getCurrentMessage();
    KeyProvider keyProvider = keyId -> KeyManagementUtils.loadPrivateKey(m, props);
    String signatureAlgorithm = (String) m.getContextualProperty(HTTPSignatureConstants.RSSEC_SIGNATURE_ALGORITHM);
    if (signatureAlgorithm == null) {
        signatureAlgorithm = DefaultSignatureConstants.SIGNING_ALGORITHM;
    }
    String keyId = (String) m.getContextualProperty(HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_KEY_ID);
    if (keyId == null) {
        keyId = props.getProperty(HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_KEY_ID);
        if (keyId == null) {
            throw new SignatureException("The signature key id is a required configuration property");
        }
    }
    List<String> signedHeaders = CastUtils.cast((List<?>) m.getContextualProperty(HTTPSignatureConstants.RSSEC_HTTP_SIGNATURE_OUT_HEADERS));
    if (signedHeaders == null) {
        signedHeaders = Collections.emptyList();
    }
    return new MessageSigner(signatureAlgorithm, keyProvider, keyId, signedHeaders);
}
Also used : KeyProvider(org.apache.cxf.rs.security.httpsignature.provider.KeyProvider) Properties(java.util.Properties) CastUtils(org.apache.cxf.helpers.CastUtils) Message(org.apache.cxf.message.Message) IOException(java.io.IOException) HashMap(java.util.HashMap) KeyProvider(org.apache.cxf.rs.security.httpsignature.provider.KeyProvider) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) DefaultSignatureConstants(org.apache.cxf.rs.security.httpsignature.utils.DefaultSignatureConstants) List(java.util.List) LogUtils(org.apache.cxf.common.logging.LogUtils) Map(java.util.Map) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) HTTPSignatureConstants(org.apache.cxf.rs.security.httpsignature.HTTPSignatureConstants) KeyManagementUtils(org.apache.cxf.rs.security.httpsignature.utils.KeyManagementUtils) Collections(java.util.Collections) SignatureException(org.apache.cxf.rs.security.httpsignature.exception.SignatureException) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) Message(org.apache.cxf.message.Message) SignatureException(org.apache.cxf.rs.security.httpsignature.exception.SignatureException) Properties(java.util.Properties)

Example 19 with MessageSigner

use of org.apache.cxf.rs.security.httpsignature.MessageSigner in project cxf by apache.

the class JAXRSHTTPSignatureTest method testMissingDigestHeader.

@Test
public void testMissingDigestHeader() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateSignatureInterceptor signatureFilter = new CreateSignatureInterceptor();
    signatureFilter.setAddDigest(false);
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    List<String> headerList = Arrays.asList("accept", "(request-target)");
    MessageSigner messageSigner = new MessageSigner(keyId -> privateKey, "alice-key-id", headerList);
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsigprops/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 20 with MessageSigner

use of org.apache.cxf.rs.security.httpsignature.MessageSigner in project cxf by apache.

the class JAXRSHTTPSignatureTest method testIncorrectDigestAlgorithm.

@Test
public void testIncorrectDigestAlgorithm() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateDigestTestInterceptor signatureFilter = new CreateDigestTestInterceptor("SHA-1");
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    MessageSigner messageSigner = new MessageSigner(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsig/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Aggregations

MessageSigner (org.apache.cxf.rs.security.httpsignature.MessageSigner)30 URL (java.net.URL)29 Response (javax.ws.rs.core.Response)29 WebClient (org.apache.cxf.jaxrs.client.WebClient)29 Book (org.apache.cxf.systest.jaxrs.security.Book)29 Test (org.junit.Test)29 KeyStore (java.security.KeyStore)28 PrivateKey (java.security.PrivateKey)28 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)20 VerifySignatureClientFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter)5 MessageVerifier (org.apache.cxf.rs.security.httpsignature.MessageVerifier)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1