Example usage for java.security KeyStore getDefaultType

List of usage examples for java.security KeyStore getDefaultType

Introduction

In this page you can find the example usage for java.security KeyStore getDefaultType.

Prototype

public static final String getDefaultType() 

Source Link

Document

Returns the default keystore type as specified by the keystore.type security property, or the string "jks" (acronym for "Java keystore" ) if no such property exists.

Usage

From source file:org.seedstack.seed.crypto.internal.EncryptionServiceFactoryTest.java

/**
 * Test method for/*w w  w. j ava  2s . c  o m*/
 * {@link org.seedstack.seed.crypto.internal.EncryptionServiceFactory#createEncryptionService(org.seedstack.seed.crypto.internal.KeyStoreDefinition, org.seedstack.seed.crypto.internal.CertificateDefinition)}
 * .
 *
 * @throws Exception if an error occurred
 */
@Test
public void testCreateEncryptionService(@Mocked final KeyStoreDefinition keyStoreDefinition,
        @Mocked final CertificateDefinition certificateDefinition, @Mocked final KeyStore keyStore,
        @Mocked final FileInputStream file,
        @SuppressWarnings("unused") @Mocked final EncryptionServiceImpl asymetricCrypting) throws Exception {
    new Expectations() {
        final String pathToKeystore = "pathToKeystore";

        {
            keyStoreDefinition.getPath();
            returns(pathToKeystore);

            KeyStore.getInstance(KeyStore.getDefaultType());
            returns(keyStore);

            new FileInputStream(pathToKeystore);
            returns(file);

            keyStoreDefinition.getPassword();
            returns("password");
        }
    };

    EncryptionServiceFactory factory = new EncryptionServiceFactory();
    factory.createEncryptionService(keyStoreDefinition, certificateDefinition);

    new Verifications() {
        {
            new EncryptionServiceImpl(keyStore, certificateDefinition);
            times = 1;
        }
    };
}

From source file:com.vmware.identity.idm.IdmDataCreator.java

private static KeyPair readKeyStore(CredentialDescriptor cd) throws IOException {
    KeyPair kp = null;//from   ww w . j a  v  a  2s . c  o  m
    InputStream is = null;

    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        char[] stsKeystorePassword = cd.getPassword().toCharArray();
        is = getInputStream(cd.getFilename());
        ks.load(is, stsKeystorePassword);

        kp = new KeyPair();
        kp.setCertificateChain(Arrays.asList(ks.getCertificateChain(cd.getAlias())));
        kp.setPrivateKey((PrivateKey) ks.getKey(cd.getAlias(), stsKeystorePassword));
    } catch (Exception e) {
        logger.debug("Caught exception while reading keystore {}", e.toString());
    } finally {
        if (is != null) {
            is.close();
        }
    }

    return kp;
}

From source file:sit.web.client.HTTPTrustHelper.java

/**
 * from/*from  ww w .java 2 s  . c  o m*/
 * http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https
 *
 * @param charset
 * @param port
 * @return
 */
public static HttpClient getNewHttpClient(Charset charset, int port) {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, charset.name());

        SchemeRegistry registry = new SchemeRegistry();
        //registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, port));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:org.eclipse.mylyn.internal.commons.net.PollingSslProtocolSocketFactory.java

public PollingSslProtocolSocketFactory() {
    KeyManager[] keymanagers = null;
    if (System.getProperty(KEY_STORE) != null && System.getProperty(KEY_STORE_PASSWORD) != null) {
        try {//from  w ww  . j  a v  a2s.  co m
            String type = System.getProperty(KEY_STORE_TYPE, KeyStore.getDefaultType());
            KeyStore keyStore = KeyStore.getInstance(type);
            char[] password = System.getProperty(KEY_STORE_PASSWORD).toCharArray();
            keyStore.load(new FileInputStream(System.getProperty(KEY_STORE)), password);
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, password);
            keymanagers = keyManagerFactory.getKeyManagers();
        } catch (Exception e) {
            CommonsNetPlugin.log(IStatus.ERROR, "Could not initialize keystore", e); //$NON-NLS-1$
        }
    }

    hasKeyManager = keymanagers != null;

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL"); //$NON-NLS-1$
        sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null);
        this.socketFactory = sslContext.getSocketFactory();
    } catch (Exception e) {
        CommonsNetPlugin.log(IStatus.ERROR, "Could not initialize SSL context", e); //$NON-NLS-1$
    }
}

From source file:com.dalaran.async.task.http.AbstractHTTPService.java

public static HttpClient getNewHttpClient() {
    try {/*w  ww.j  a  v  a  2  s . c o m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:org.projectforge.business.ldap.MyTrustManager.java

public MyTrustManager() {
    try {/*  w ww. j a  v  a  2  s  .com*/
        final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        // create a TrustManager using our KeyStore
        final TrustManagerFactory factory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        factory.init(keyStore);
        this.trustManager = getX509TrustManager(factory.getTrustManagers());
    } catch (final KeyStoreException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final NoSuchAlgorithmException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final CertificateException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final IOException ex) {
        log.error("Exception encountered " + ex, ex);
    }
}

From source file:org.jasig.cas.authentication.FileTrustStoreSslSocketFactory.java

/**
 * Instantiates a new trusted proxy authentication trust store ssl socket factory.
 * Defaults to <code>TLSv1</code> and {@link SSLConnectionSocketFactory#BROWSER_COMPATIBLE_HOSTNAME_VERIFIER}
 * for the supported protocols and hostname verification.
 * @param trustStoreFile the trust store file
 * @param trustStorePassword the trust store password
 *///from  www .j  a  v  a  2  s. c o  m
public FileTrustStoreSslSocketFactory(final File trustStoreFile, final String trustStorePassword) {
    this(trustStoreFile, trustStorePassword, KeyStore.getDefaultType());
}

From source file:de.betterform.connector.http.ssl.BetterFORMTrustManager.java

private TrustManager[] getCustomX509TrustManagers(final URL url, final String password)
        throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException,
        UnrecoverableKeyException {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());

    if (url == null) {
        throw new IllegalArgumentException("BetterFORMTrustManager: Keystore url may not be null");
    }/*from  www . j a v  a  2s.  c o m*/

    LOGGER.debug("BetterFORMTrustManager: initializing custom key store");
    KeyStore customKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream is = null;
    try {
        is = url.openStream();
        customKeystore.load(is, password != null ? password.toCharArray() : null);
    } finally {
        if (is != null)
            is.close();
    }

    trustManagerFactory.init(customKeystore);

    TrustManager[] customX509TrustManagers = trustManagerFactory.getTrustManagers();
    for (int i = 0; i < customX509TrustManagers.length; i++) {
        if (customX509TrustManagers[i] instanceof X509TrustManager) {
            customX509TrustManagers[i] = new AuthSSLX509TrustManager(
                    (X509TrustManager) customX509TrustManagers[i]);
        }
    }
    return customX509TrustManagers;
}

From source file:org.wso2.identity.integration.common.clients.sso.saml.query.ClientSignKeyDataHolder.java

/**
 * Constructor method/* w  w w .j a  v  a2 s . c om*/
 * @param keyStorePath path to the key store
 * @param password password of keystore
 * @param keyAlias key alias of keystore
 * @throws Exception if, Algorithm fails, input stream fails
 */
public ClientSignKeyDataHolder(String keyStorePath, String password, String keyAlias) throws Exception {

    Certificate[] certificates;
    InputStream is = null;

    try {
        File file = new File(keyStorePath);
        is = new FileInputStream(file);
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(is, password.toCharArray());

        privateKey = (PrivateKey) keystore.getKey(keyAlias, password.toCharArray());

        certificates = keystore.getCertificateChain(keyAlias);

        issuerCerts = new X509Certificate[certificates.length];

        int i = 0;
        for (Certificate certificate : certificates) {
            issuerCerts[i++] = (X509Certificate) certificate;
        }

        signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA;

        publicKey = issuerCerts[0].getPublicKey();
        String pubKeyAlgo = publicKey.getAlgorithm();
        if (DSA_ENCRYPTION_ALGORITHM.equalsIgnoreCase(pubKeyAlgo)) {
            signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
        }
    } catch (CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException
            | IOException e) {
        String mgs = "Error while initializing credentials";
        log.error(mgs, e);
        throw new Exception(mgs);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error("Unable to close input stream", e);
            }
        }
    }
}

From source file:jp.pigumer.mqtt.Client.java

Optional<KeyStore> loadKeyStore() {
    X509Certificate cert;//from   ww  w.ja v  a  2  s . c o m

    if (caFile == null) {
        return Optional.empty();
    }
    try (InputStream is = caFile.getInputStream()) {
        InputStreamReader isr = new InputStreamReader(is);
        PEMParser parser = new PEMParser(isr);
        X509CertificateHolder holder = (X509CertificateHolder) parser.readObject();
        cert = new JcaX509CertificateConverter().getCertificate(holder);
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", cert);
        return Optional.of(keyStore);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "failed load", e);
        return Optional.empty();
    }
}