Search in sources :

Example 1 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class SignatureTests method testProvideCertificateInHeader.

@Test
public void testProvideCertificateInHeader() throws Exception {
    setupMock();
    Exchange unsigned = getMandatoryEndpoint("direct:signature-property").createExchange();
    unsigned.getIn().setBody(payload);
    // create a keypair
    KeyStore keystore = loadKeystore();
    Certificate certificate = keystore.getCertificate("bob");
    PrivateKey pk = (PrivateKey) keystore.getKey("bob", "letmein".toCharArray());
    // sign with the private key
    unsigned.getIn().setHeader(SIGNATURE_PRIVATE_KEY, pk);
    template.send("direct:headerkey-sign", unsigned);
    // verify with the public key
    Exchange signed = getMandatoryEndpoint("direct:alias-sign").createExchange();
    signed.getIn().copyFrom(unsigned.getOut());
    signed.getIn().setHeader(SIGNATURE_PUBLIC_KEY_OR_CERT, certificate);
    template.send("direct:headerkey-verify", signed);
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 2 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class SigningProcessor method getKeyPassword.

protected char[] getKeyPassword(Exchange exchange) throws Exception {
    KeyStore keystore = config.getKeystore();
    char[] password = null;
    if (keystore != null) {
        password = exchange.getIn().getHeader(DigitalSignatureConstants.KEYSTORE_PASSWORD, char[].class);
        if (password == null) {
            password = config.getPassword();
        }
    }
    return password;
}
Also used : KeyStore(java.security.KeyStore)

Example 3 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.

protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
    Properties supplementalProperties = new Properties();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    SecureRandom sr = null;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    // Ignore
    }
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(null, null, null);
    SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
    supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
    supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
    supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
    supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
    if (sr != null) {
        supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
        supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
    }
    supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
    supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
    // Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
    String ssp = "";
    for (String protocol : socket.getSupportedProtocols()) {
        if (!"SSLv2Hello".equals(protocol)) {
            ssp = protocol;
            break;
        }
    }
    supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
    return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 4 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class KeyStoreParametersTest method testExplicitType.

public void testExplicitType() throws Exception {
    KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
    ksp.setType("jks");
    KeyStore ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
}
Also used : KeyStore(java.security.KeyStore)

Example 5 with KeyStore

use of java.security.KeyStore in project camel by apache.

the class KeyStoreParametersTest method testValidParameters.

public void testValidParameters() throws GeneralSecurityException, IOException, URISyntaxException {
    KeyStoreParameters ksp = this.createMinimalKeyStoreParameters();
    KeyStore ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
    URL resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
    ksp.setResource(resourceUrl.toExternalForm());
    ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
    resourceUrl = this.getClass().getResource("/org/apache/camel/util/jsse/localhost.ks");
    File file = new File(resourceUrl.toURI());
    ksp.setResource(file.getAbsolutePath());
    ks = ksp.createKeyStore();
    assertNotNull(ks.getCertificate("server"));
}
Also used : KeyStore(java.security.KeyStore) File(java.io.File) URL(java.net.URL)

Aggregations

KeyStore (java.security.KeyStore)738 IOException (java.io.IOException)190 X509Certificate (java.security.cert.X509Certificate)189 FileInputStream (java.io.FileInputStream)163 KeyStoreException (java.security.KeyStoreException)151 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)138 InputStream (java.io.InputStream)125 Certificate (java.security.cert.Certificate)124 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)119 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)114 SSLContext (javax.net.ssl.SSLContext)112 PrivateKey (java.security.PrivateKey)94 CertificateException (java.security.cert.CertificateException)94 File (java.io.File)82 ByteArrayInputStream (java.io.ByteArrayInputStream)75 CertificateFactory (java.security.cert.CertificateFactory)75 Key (java.security.Key)61 UnrecoverableKeyException (java.security.UnrecoverableKeyException)55 TrustManager (javax.net.ssl.TrustManager)47 KeyManagementException (java.security.KeyManagementException)40