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:com.alfaariss.oa.engine.crypto.keystore.KeystoreSigningFactory.java

private KeyStore loadKeystore(Element eKeystore) throws CryptoException {
    KeyStore keystore = null;/*  ww w.  j ava 2 s . co  m*/
    try {
        String sKeystoreType = _configurationManager.getParam(eKeystore, "type");
        if (sKeystoreType == null) {
            sKeystoreType = KeyStore.getDefaultType();
            _logger.info("Could not retrieve keystore 'type' paramater, using default: " + sKeystoreType);
        }

        String sKeystoreFile = _configurationManager.getParam(eKeystore, "file");
        if (sKeystoreFile == null) {
            _logger.error("Could not retrieve keystore 'file' parameter");
            throw new CryptoException(SystemErrors.ERROR_CONFIG_READ);
        }

        // Establish real filename:
        sKeystoreFile = PathTranslator.getInstance().map(sKeystoreFile).trim();

        char[] caKeystorePassword = null;
        _sKeystorePassword = _configurationManager.getParam(eKeystore, "keystore_password");
        if (_sKeystorePassword == null) {
            _logger.info("No optional 'keystore_password' parameter supplied");
        } else {
            caKeystorePassword = _sKeystorePassword.toCharArray();
        }

        keystore = KeyStore.getInstance(sKeystoreType);
        keystore.load(new FileInputStream(sKeystoreFile), caKeystorePassword);

        _logger.info("Loaded keystore: " + sKeystoreFile);
    } catch (KeyStoreException e) {
        _logger.error("Could not load keystore", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    } catch (NoSuchAlgorithmException e) {
        _logger.error("Could not load keystore, no such algorithm", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    } catch (CertificateException e) {
        _logger.error("Could not load keystore, certificate error", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    } catch (FileNotFoundException e) {
        _logger.error("Could not load keystore, file not found", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    } catch (IOException e) {
        _logger.error("Could not load keystore, I/O error", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    } catch (ConfigurationException e) {
        _logger.error("Could not read keystore configuration", e);
        throw new CryptoException(SystemErrors.ERROR_INIT, e);
    }

    return keystore;
}

From source file:com.gs.tools.doc.extractor.core.util.HttpUtility.java

public static HttpClient getDefaultHttpsClient() {
    try {//from w w w.  j a v  a2 s .  c  om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new DefaultSecureSocketFactory(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);
        HttpConnectionParams.setConnectionTimeout(params, 300000);
        HttpConnectionParams.setSocketBufferSize(params, 10485760);
        HttpConnectionParams.setSoTimeout(params, 300000);

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

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
        HttpClientBuilder b = HttpClientBuilder.create();
        return b.build();
        //return httpClient;
    } catch (Exception e) {
        e.printStackTrace();
        return new DefaultHttpClient();
    }
}

From source file:eu.trentorise.smartcampus.network.RemoteConnector.java

private static HttpClient getAcceptAllHttpClient(HttpParams inParams) {
    HttpClient client = null;//from w  w  w. ja va 2 s.  c o  m

    HttpParams params = inParams != null ? inParams : new BasicHttpParams();

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

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

        // IMPORTANT: use CustolSSLSocketFactory for 2.2
        SSLSocketFactory sslSocketFactory = new CustomSSLSocketFactory(trustStore);
        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        registry.register(new Scheme("https", sslSocketFactory, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

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

    return client;
}

From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java

private HttpClientConnectionManager createConnectionManager() throws GeneralSecurityException, IOException {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);//from  w  ww  .j  ava 2  s. c o  m

    if (registry == null) {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        X509TrustManager x509TrustManager = null;
        for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
            if (trustManager instanceof X509TrustManager) {
                x509TrustManager = (X509TrustManager) trustManager;
                break;
            }
        }

        X509Certificate[] acceptedIssuers = x509TrustManager.getAcceptedIssuers();
        if (acceptedIssuers != null) {
            // If this is null, something is really wrong...
            int issuerNum = 1;
            for (X509Certificate cert : acceptedIssuers) {
                trustStore.setCertificateEntry("issuer" + issuerNum, cert);
                issuerNum++;
            }
        } else {
            LOG.log(Level.INFO, "Didn't find any new certificates to trust.");
        }

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        sslContextBuilder.loadTrustMaterial(trustStore,
                new KnownArcGISCertificatesTrustStrategy(new ArrayList<>(trustedCerts)));
        SSLContext sslContext = sslContextBuilder.build();
        SSLContext.setDefault(sslContext);
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                new DataStoreProxyHostnameVerifier(new ArrayList<>(trustedCerts)));

        this.registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();
    }
    return new PoolingHttpClientConnectionManager(registry);
}

From source file:org.teknux.jettybootstrap.keystore.JettyKeystore.java

private static KeyStore createKeyStore(PrivateKey privateKey, Certificate certificate, String alias,
        String password) throws JettyKeystoreException {
    try {/* w  w w  . jav a 2s  .c o m*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);

        PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(privateKey, new Certificate[] { certificate });
        keyStore.setEntry(alias, privateKeyEntry, new KeyStore.PasswordProtection(password.toCharArray()));

        return keyStore;
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
        throw new JettyKeystoreException(JettyKeystoreException.ERROR_CREATE_KEYSTORE,
                "Can not create keystore file", e);
    }
}

From source file:android.core.SSLSocketTest.java

/**
 * Regression test for 963650: javax.net.ssl.KeyManager has no implemented
 * (documented?) algorithms./*from w  w  w  . ja v a  2s . c  o m*/
 */
public void testDefaultAlgorithms() throws Exception {
    SSLContext ctx = SSLContext.getInstance("TLS");
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    KeyStore ks = KeyStore.getInstance("BKS");

    assertEquals("X509", kmf.getAlgorithm());
    assertEquals("X509", KeyManagerFactory.getDefaultAlgorithm());

    assertEquals("BKS", ks.getType());
    assertEquals("BKS", KeyStore.getDefaultType());
}

From source file:com.solace.samples.cloudfoundry.securesession.controller.SolaceController.java

/**
 * This utility function installs a certificate into the JRE's trusted
 * store. Normally you would not do this, but this is provided to
 * demonstrate how to use TLS, and have the client validate a self-signed
 * server certificate./*from   w w  w .j a  v  a 2 s. c o m*/
 *
 * @throws Exception
 */
private static void importCertificate() throws Exception {

    File file = new File(CERTIFICATE_FILE_NAME);
    logger.info("Loading certificate from " + file.getAbsolutePath());

    // This loads the KeyStore from the default location
    // (i.e. default for a Clound Foundry app) using the default password.
    FileInputStream is = new FileInputStream(TRUST_STORE);
    char[] password = TRUST_STORE_PASSWORD.toCharArray();
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(is, password);
    is.close();

    // Create an ByteArrayInputStream stream from the
    FileInputStream fis = new FileInputStream(CERTIFICATE_FILE_NAME);
    DataInputStream dis = new DataInputStream(fis);
    byte[] bytes = new byte[dis.available()];
    dis.readFully(bytes);
    dis.close();
    ByteArrayInputStream certstream = new ByteArrayInputStream(bytes);

    // This takes that Byte Array and creates a certificate out of it.
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate certs = cf.generateCertificate(certstream);

    // Finally, store the new certificate in the keystore.
    keystore.setCertificateEntry(CERTIFICATE_ALIAS, certs);

    // Save the new keystore contents
    FileOutputStream out = new FileOutputStream(TRUST_STORE);
    keystore.store(out, password);
    out.close();

}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

KeyStore loadAppKeyStore() {
    KeyStore ks;//from   w  w w  .  ja va 2s.  c  o m
    try {
        ks = KeyStore.getInstance(KeyStore.getDefaultType());
    } catch (KeyStoreException e) {
        LOGGER.log(Level.SEVERE, "getAppKeyStore()", e);
        return null;
    }
    try {
        ks.load(null, null);
        ks.load(new java.io.FileInputStream(keyStoreFile), "MTM".toCharArray());
    } catch (java.io.FileNotFoundException e) {
        LOGGER.log(Level.INFO, "getAppKeyStore(" + keyStoreFile + ") - file does not exist");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "getAppKeyStore(" + keyStoreFile + ")", e);
    }
    return ks;
}

From source file:org.apache.hadoop.gateway.services.security.impl.X509CertificateUtil.java

public static void writeCertificateToJKS(Certificate cert, final File file)
        throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

    char[] password = "changeit".toCharArray();
    ks.load(null, password);// ww w.  j  a v a 2  s  . co  m
    ks.setCertificateEntry("gateway-identity", cert);
    FileOutputStream fos = new FileOutputStream(file);
    /* Coverity Scan CID 1361992 */
    try {
        ks.store(fos, password);
    } finally {
        fos.close();
    }
}