Search in sources :

Example 56 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 57 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 58 with KeyStore

use of java.security.KeyStore in project httpclient by pixmob.

the class HttpRequestBuilder method loadCertificates.

private static KeyStore loadCertificates(Context context) throws IOException {
    try {
        final KeyStore localTrustStore = KeyStore.getInstance("BKS");
        final InputStream in = context.getResources().openRawResource(R.raw.hc_keystore);
        try {
            localTrustStore.load(in, null);
        } finally {
            in.close();
        }
        return localTrustStore;
    } catch (Exception e) {
        final IOException ioe = new IOException("Failed to load SSL certificates");
        ioe.initCause(e);
        throw ioe;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) KeyStore(java.security.KeyStore) GeneralSecurityException(java.security.GeneralSecurityException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 59 with KeyStore

use of java.security.KeyStore in project platformlayer by platformlayer.

the class KeystoneCliContext method getCertificateChain.

public Certificate[] getCertificateChain(String keystore, String keystoreSecret, String keyAlias) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    if (getOptions().isServerMode()) {
        throw new IllegalArgumentException("Files not supported in server mode");
    }
    if (keystoreSecret == null) {
        keystoreSecret = KeyStoreUtils.DEFAULT_KEYSTORE_SECRET;
    }
    KeyStore keyStore = KeyStoreUtils.load(new File(keystore), keystoreSecret);
    if (keyAlias == null) {
        List<String> keyAliases = KeyStoreUtils.getKeyAliases(keyStore);
        if (keyAliases.size() == 0) {
            throw new CliException("No keys found in keystore");
        }
        if (keyAliases.size() != 1) {
            System.out.println("Found keys:\n\t" + Joiner.on("\n\t").join(keyAliases));
            throw new CliException("Multiple keys found in keystore; specify --alias");
        }
        keyAlias = keyAliases.get(0);
    }
    Certificate[] certificateChain = keyStore.getCertificateChain(keyAlias);
    return certificateChain;
}
Also used : CliException(com.fathomdb.cli.CliException) KeyStore(java.security.KeyStore) File(java.io.File) Certificate(java.security.cert.Certificate)

Example 60 with KeyStore

use of java.security.KeyStore in project robovm by robovm.

the class DefaultSSLContextImpl method getTrustManagers.

// TODO javax.net.ssl.trustStoreProvider system property
TrustManager[] getTrustManagers() throws GeneralSecurityException, IOException {
    if (TRUST_MANAGERS != null) {
        return TRUST_MANAGERS;
    }
    // find TrustStore, TrustManagers
    String keystore = System.getProperty("javax.net.ssl.trustStore");
    if (keystore == null) {
        return null;
    }
    String keystorepwd = System.getProperty("javax.net.ssl.trustStorePassword");
    char[] pwd = (keystorepwd == null) ? null : keystorepwd.toCharArray();
    // TODO Defaults: jssecacerts; cacerts
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(keystore));
        ks.load(is, pwd);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    String tmfAlg = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlg);
    tmf.init(ks);
    TRUST_MANAGERS = tmf.getTrustManagers();
    return TRUST_MANAGERS;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream)

Aggregations

KeyStore (java.security.KeyStore)899 IOException (java.io.IOException)226 X509Certificate (java.security.cert.X509Certificate)216 FileInputStream (java.io.FileInputStream)186 InputStream (java.io.InputStream)177 KeyStoreException (java.security.KeyStoreException)174 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)165 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)146 Certificate (java.security.cert.Certificate)144 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)136 SSLContext (javax.net.ssl.SSLContext)130 CertificateException (java.security.cert.CertificateException)115 PrivateKey (java.security.PrivateKey)104 File (java.io.File)95 CertificateFactory (java.security.cert.CertificateFactory)80 ByteArrayInputStream (java.io.ByteArrayInputStream)78 UnrecoverableKeyException (java.security.UnrecoverableKeyException)64 Key (java.security.Key)63 TrustManager (javax.net.ssl.TrustManager)60 Test (org.junit.Test)54