Search in sources :

Example 1 with KeystoreProxy

use of com.adaptris.security.keystore.KeystoreProxy 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 KeystoreProxy

use of com.adaptris.security.keystore.KeystoreProxy 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 3 with KeystoreProxy

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

the class SingleEntryKeystoreBase method testImportPrivateKey.

@Test
public void testImportPrivateKey() throws Exception {
    KeystoreProxy ksp = KeystoreFactory.getDefault().create(kloc);
    ksp.load();
    try {
        ksp.importPrivateKey("", "".toCharArray(), "", "".toCharArray());
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
    try {
        ksp.importPrivateKey("", "".toCharArray(), (InputStream) null, "".toCharArray());
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
    try {
        ksp.importPrivateKey("", "".toCharArray(), (File) null, "".toCharArray());
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
}
Also used : KeystoreProxy(com.adaptris.security.keystore.KeystoreProxy) KeystoreException(com.adaptris.security.exc.KeystoreException) KeystoreException(com.adaptris.security.exc.KeystoreException) Test(org.junit.Test)

Example 4 with KeystoreProxy

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

the class SingleEntryKeystoreBase method testImportCertificateChain.

@Test
public void testImportCertificateChain() throws Exception {
    KeystoreProxy ksp = KeystoreFactory.getDefault().create(kloc);
    ksp.load();
    try {
        ksp.importCertificateChain("", "".toCharArray(), "");
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
    try {
        ksp.importCertificateChain("", "".toCharArray(), (InputStream) null);
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
    try {
        ksp.importCertificateChain("", "".toCharArray(), (File) null);
        fail("Import successful");
    } catch (Exception e) {
        assertEquals(KeystoreException.class, e.getClass());
    }
}
Also used : KeystoreProxy(com.adaptris.security.keystore.KeystoreProxy) KeystoreException(com.adaptris.security.exc.KeystoreException) KeystoreException(com.adaptris.security.exc.KeystoreException) Test(org.junit.Test)

Example 5 with KeystoreProxy

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

the class TestKeyStoreInfoChange method testChangeKeyStorePassword.

@Test
public void testChangeKeyStorePassword() {
    Certificate thisCert;
    try {
        ksm = KeystoreFactory.getDefault().create(ksi);
        ksm.load();
        String alias = config.getProperty(Config.KEYSTORE_COMMON_PRIVKEY_ALIAS);
        // Now change the password
        ksm.setKeystoreLocation(pwKsi);
        ksm.commit();
        // Now attempt to reload with the new keystore info.
        KeystoreProxy tempKsm = KeystoreFactory.getDefault().create(pwKsi);
        tempKsm.load();
        // we should be able to reread the certificate information...
        if (tempKsm.containsAlias(alias)) {
            thisCert = tempKsm.getCertificate(alias);
            assertNotNull(thisCert);
        } else {
            fail(alias + " does not exist in the specified keystore");
        }
    } catch (Exception e) {
        logR.error(e.getMessage(), e);
        fail(e.getMessage());
    }
}
Also used : KeystoreProxy(com.adaptris.security.keystore.KeystoreProxy) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Aggregations

KeystoreProxy (com.adaptris.security.keystore.KeystoreProxy)17 Test (org.junit.Test)11 KeystoreException (com.adaptris.security.exc.KeystoreException)7 Certificate (java.security.cert.Certificate)7 PrivateKey (java.security.PrivateKey)4 KeystoreLocation (com.adaptris.security.keystore.KeystoreLocation)3 CertificateBuilder (com.adaptris.security.certificate.CertificateBuilder)2 ConfiguredKeystore (com.adaptris.security.keystore.ConfiguredKeystore)2 Map (java.util.Map)2 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)1 CertException (com.adaptris.security.exc.CertException)1 DecryptException (com.adaptris.security.exc.DecryptException)1 EncryptException (com.adaptris.security.exc.EncryptException)1 VerifyException (com.adaptris.security.exc.VerifyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Random (java.util.Random)1