Example usage for java.security KeyStore load

List of usage examples for java.security KeyStore load

Introduction

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

Prototype

public final void load(InputStream stream, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException 

Source Link

Document

Loads this KeyStore from the given input stream.

Usage

From source file:org.owasp.goatdroid.herdfinancial.requestresponse.CustomSSLSocketFactory.java

public static HttpClient getNewHttpClient() {
    try {// www.j  av  a2s . c  om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        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);

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

From source file:com.gmail.nagamatu.radiko.installer.MySSLSocketFactory.java

public static HttpClient getNewHttpClient() {
    try {//from  w  ww.  j a va  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", PlainSocketFactory.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:com.threerings.getdown.tools.Digester.java

/**
 * Creates a digest file in the specified application directory.
 *///from w w w.j  av a2  s.  co m
public static void signDigest(File appdir, File storePath, String storePass, String storeAlias)
        throws IOException, GeneralSecurityException {
    File inputFile = new File(appdir, Digest.DIGEST_FILE);
    File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX);

    FileInputStream storeInput = null, dataInput = null;
    FileOutputStream signatureOutput = null;
    try {
        // initialize the keystore
        KeyStore store = KeyStore.getInstance("JKS");
        storeInput = new FileInputStream(storePath);
        store.load(storeInput, storePass.toCharArray());
        PrivateKey key = (PrivateKey) store.getKey(storeAlias, storePass.toCharArray());

        // sign the digest file
        Signature sig = Signature.getInstance("SHA1withRSA");
        dataInput = new FileInputStream(inputFile);
        byte[] buffer = new byte[8192];
        int length;

        sig.initSign(key);
        while ((length = dataInput.read(buffer)) != -1) {
            sig.update(buffer, 0, length);
        }

        // Write out the signature
        signatureOutput = new FileOutputStream(signatureFile);
        String signed = new String(Base64.encodeBase64(sig.sign()));
        signatureOutput.write(signed.getBytes("utf8"));

    } finally {
        StreamUtil.close(signatureOutput);
        StreamUtil.close(dataInput);
        StreamUtil.close(storeInput);
    }
}

From source file:org.zywx.wbpalmstar.platform.certificates.Http.java

public static HNetSSLSocketFactory getSSLSocketFactory() {
    HNetSSLSocketFactory ssSocketFactory = null;
    try {/*from w w w.  ja  v  a  2 s  .c  o  m*/
        KeyStore keyStore = KeyStore.getInstance(keyType);
        keyStore.load(null, null);
        ssSocketFactory = new HNetSSLSocketFactory(keyStore, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ssSocketFactory;
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java

public static KeyStore getPKCS12KeyStore(String alias, Certificate[] certChain, KeyPair keyPair, char[] passwd)
        throws Exception {

    PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier) keyPair.getPrivate();
    bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));
    SubjectKeyIdentifier pubKeyId = new JcaX509ExtensionUtils().createSubjectKeyIdentifier(keyPair.getPublic());
    bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, pubKeyId);
    KeyStore store = KeyStore.getInstance(KEY_STORE_TYPE, JCE_PROVIDER);
    store.load(null, null);
    store.setKeyEntry(alias, keyPair.getPrivate(), passwd, certChain);
    return store;
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java

public static KeyStore readPKCS12KeyStore(String alias, Certificate[] chain, KeyPair keyPair, char[] pwd)
        throws Exception {
    PKCS12SafeBagBuilder BagBuilder = new JcaPKCS12SafeBagBuilder((X509Certificate) chain[0]);
    BagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(alias));
    SubjectKeyIdentifier pubKeyId = new JcaX509ExtensionUtils().createSubjectKeyIdentifier(keyPair.getPublic());
    BagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

    KeyStore store = KeyStore.getInstance(KEY_STORE_TYPE, JCE_PROVIDER);
    store.load(null, null);
    store.setKeyEntry(alias, keyPair.getPrivate(), pwd, chain);

    return store;
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java

public static KeyStore coverString2KeyStore(String base64PKS, String password) throws CertException {

    byte[] keyStoreByte = Base64.decodeBase64(base64PKS);
    InputStream istream = StreamUtil.bytes2Stream(keyStoreByte);
    try {//from  ww  w.j  a  va  2 s  .  c om
        KeyStore keyStore = KeyStore.getInstance(PKCS12_STORE_TYPE);
        keyStore.load(istream, password.toCharArray());
        istream.close();
        return keyStore;
    } catch (KeyStoreException e) {
        throw new CertException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (CertificateException e) {
        throw new CertException(e);
    } catch (IOException e) {
        throw new CertException(e);
    }
}

From source file:com.splunk.shuttl.archiver.http.InsecureHttpClientFactory.java

private static KeyStore getTrustStore() {
    try {/*from  w  ww  .jav a2  s  .  c  o  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        return trustStore;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:gobblin.security.ssl.SSLContextFactory.java

/**
 * Create a {@link SSLContext} instance//from  w ww . ja  va2  s.  co  m
 *
 * @param keyStoreFile a p12 or jks file depending on key store type
 * @param keyStorePassword password to access the key store
 * @param keyStoreType type of key store
 * @param trustStoreFile a jks file
 * @param trustStorePassword password to access the trust store
 */
public static SSLContext createInstance(File keyStoreFile, String keyStorePassword, String keyStoreType,
        File trustStoreFile, String trustStorePassword) {
    if (!keyStoreType.equalsIgnoreCase(P12_STORE_TYPE_NAME)
            && !keyStoreType.equalsIgnoreCase(JKS_STORE_TYPE_NAME)) {
        throw new IllegalArgumentException("Unsupported keyStoreType: " + keyStoreType);
    }

    try {
        // Load KeyStore
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(toInputStream(keyStoreFile), keyStorePassword.toCharArray());

        // Load TrustStore
        KeyStore trustStore = KeyStore.getInstance(JKS_STORE_TYPE_NAME);
        trustStore.load(toInputStream(trustStoreFile), trustStorePassword.toCharArray());

        // Set KeyManger from keyStore
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(DEFAULT_ALGORITHM);
        kmf.init(keyStore, keyStorePassword.toCharArray());

        // Set TrustManager from trustStore
        TrustManagerFactory trustFact = TrustManagerFactory.getInstance(DEFAULT_ALGORITHM);
        trustFact.init(trustStore);

        // Set Context to TLS and initialize it
        SSLContext sslContext = SSLContext.getInstance(DEFAULT_PROTOCOL);
        sslContext.init(kmf.getKeyManagers(), trustFact.getTrustManagers(), null);

        return sslContext;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java

public static KeyStore createPCSK12KeyStore(String alias, Key key, char[] pwd, Certificate[] chain)
        throws CertException {

    try {//from w w w  .j a v a 2 s  . c  o m
        KeyStore keyStore = KeyStore.getInstance(PKCS12_STORE_TYPE);
        keyStore.load(null, pwd);
        if (pwd == null) {
            keyStore.setKeyEntry(alias, key.getEncoded(), chain);
        } else {
            keyStore.setKeyEntry(alias, key, pwd, chain);
        }
        return keyStore;
    } catch (KeyStoreException e) {
        throw new CertException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (CertificateException e) {
        throw new CertException(e);
    } catch (IOException e) {
        throw new CertException(e);
    }
}