Search in sources :

Example 1 with KeystoreLocation

use of com.adaptris.security.keystore.KeystoreLocation in project interlok by adaptris.

the class JunitSecurityHelper method newKeystore.

private void newKeystore(String url, String commonName, char[] password) throws Exception {
    KeystoreProxy ksp = null;
    KeystoreLocation ksc = KeystoreFactory.getDefault().create(url, password);
    CertificateBuilder builder = getBuilder(commonName);
    Certificate selfCert = builder.createSelfSignedCertificate();
    PrivateKey privkey = builder.getPrivateKey();
    ksp = KeystoreFactory.getDefault().create(ksc);
    try {
        ksp.load();
    } catch (Exception e) {
    // Ignore the error...
    }
    String alias = config.getProperty(SECURITY_ALIAS);
    Certificate[] certChain = new Certificate[1];
    certChain[0] = selfCert;
    ksp.setPrivateKey(alias, privkey, password, certChain);
    ksp.commit();
}
Also used : CertificateBuilder(com.adaptris.security.certificate.CertificateBuilder) PrivateKey(java.security.PrivateKey) KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) KeystoreProxy(com.adaptris.security.keystore.KeystoreProxy) Certificate(java.security.cert.Certificate)

Example 2 with KeystoreLocation

use of com.adaptris.security.keystore.KeystoreLocation in project interlok by adaptris.

the class HttpsProduceConnection method initialiseClient.

/**
 * @see HttpClientConnection#initialiseClient(java.lang.String)
 */
@Override
public HttpClientTransport initialiseClient(String url) throws HttpException {
    HttpsClient client = new HttpsClient(url);
    try {
        if (keystore != null) {
            KeystoreFactory ksf = KeystoreFactory.getDefault();
            KeystoreLocation ksl = null;
            if (keystorePassword != null) {
                ksl = ksf.create(keystore, Password.decode(keystorePassword).toCharArray());
            } else {
                ksl = ksf.create(keystore);
            }
            char[] pkpw = PasswordOverride.discoverPrivateKeyPassword(ksl, getPrivateKeyPasswordProvider());
            if (pkpw != null) {
                client.registerPrivateKeyPassword(pkpw);
            }
            client.registerKeystore(ksf.create(ksl));
        }
    } catch (AdaptrisSecurityException e) {
        throw new HttpException(e);
    }
    client.setAlwaysTrust(alwaysTrust);
    return client;
}
Also used : KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) HttpsClient(com.adaptris.http.HttpsClient) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) KeystoreFactory(com.adaptris.security.keystore.KeystoreFactory) HttpException(com.adaptris.http.HttpException)

Example 3 with KeystoreLocation

use of com.adaptris.security.keystore.KeystoreLocation in project interlok by adaptris.

the class Config method buildKeystore.

public KeystoreLocation buildKeystore(String ksUrl, String cn, boolean overwrite) throws Exception {
    String commonName = StringUtils.defaultIfBlank(cn, config.getProperty(KEYSTORE_COMMON_PRIVKEY_ALIAS));
    KeystoreLocation ksc = KeystoreFactory.getDefault().create(ksUrl, config.getProperty(Config.KEYSTORE_COMMON_KEYSTORE_PW).toCharArray());
    KeystoreProxy ksp = KeystoreFactory.getDefault().create(ksc);
    if (ksc.exists() && overwrite == false) {
        ksp.load();
    }
    CertificateBuilder builder = getBuilder(commonName);
    Certificate selfCert = builder.createSelfSignedCertificate();
    PrivateKey privkey = builder.getPrivateKey();
    char[] password = config.getProperty(KEYSTORE_COMMON_PRIVKEY_PW).toCharArray();
    Certificate[] certChain = new Certificate[1];
    certChain[0] = selfCert;
    ksp.setPrivateKey(commonName, privkey, password, certChain);
    ksp.commit();
    return ksc;
}
Also used : CertificateBuilder(com.adaptris.security.certificate.CertificateBuilder) PrivateKey(java.security.PrivateKey) KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) KeystoreProxy(com.adaptris.security.keystore.KeystoreProxy) Certificate(java.security.cert.Certificate)

Example 4 with KeystoreLocation

use of com.adaptris.security.keystore.KeystoreLocation in project interlok by adaptris.

the class TestKeystoreLocation method testNonExistentLocalKeystore.

@Test
public void testNonExistentLocalKeystore() {
    try {
        KeystoreLocation k = KeystoreFactory.getDefault().create("file:///c:/fredblahblahblh?keystoreType=JKS", cfg.getProperty(Config.KEYSTORE_COMMON_KEYSTORE_PW).toCharArray());
        assertTrue("Keystore Location", !k.exists());
    } catch (Exception e) {
        logR.error("testLocalKeystore failed", e);
        fail(e.getMessage());
    }
}
Also used : KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) Test(org.junit.Test)

Example 5 with KeystoreLocation

use of com.adaptris.security.keystore.KeystoreLocation in project interlok by adaptris.

the class TestKeystoreLocation method testLocalKeystore.

@Test
public void testLocalKeystore() {
    try {
        KeystoreLocation k = KeystoreFactory.getDefault().create(cfg.getProperty(Config.KEYSTORE_TEST_URL), cfg.getProperty(Config.KEYSTORE_COMMON_KEYSTORE_PW).toCharArray());
        assertTrue("Keystore Location", k.exists());
        InputStream in = k.openInput();
        in.close();
    } catch (Exception e) {
        logR.error("testLocalKeystore failed", e);
        fail(e.getMessage());
    }
}
Also used : KeystoreLocation(com.adaptris.security.keystore.KeystoreLocation) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

KeystoreLocation (com.adaptris.security.keystore.KeystoreLocation)12 Test (org.junit.Test)6 KeystoreProxy (com.adaptris.security.keystore.KeystoreProxy)3 HttpException (com.adaptris.http.HttpException)2 HttpsClient (com.adaptris.http.HttpsClient)2 CertificateBuilder (com.adaptris.security.certificate.CertificateBuilder)2 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)2 KeystoreFactory (com.adaptris.security.keystore.KeystoreFactory)2 InputStream (java.io.InputStream)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 URLString (com.adaptris.util.URLString)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1