Example usage for java.security KeyStore getInstance

List of usage examples for java.security KeyStore getInstance

Introduction

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

Prototype

public static KeyStore getInstance(String type) throws KeyStoreException 

Source Link

Document

Returns a keystore object of the specified type.

Usage

From source file:com.netscape.cmstools.pkcs11.PKCS11KeyRemoveCLI.java

public void execute(String[] args) throws Exception {

    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        printHelp();//from  w ww  .  ja v  a2 s .  c o m
        return;
    }

    if (cmd.hasOption("verbose")) {
        PKILogger.setLevel(PKILogger.Level.INFO);

    } else if (cmd.hasOption("debug")) {
        PKILogger.setLevel(PKILogger.Level.DEBUG);
    }

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length < 1) {
        throw new Exception("Missing key ID.");
    }

    String alias = cmdArgs[0];

    String tokenName = getConfig().getTokenName();
    CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);

    KeyStore ks = KeyStore.getInstance("pkcs11");
    ks.load(new JSSLoadStoreParameter(token));

    Key key = ks.getKey(alias, null);

    if (key == null) {
        throw new Exception("Key not found: " + alias);
    }

    ks.deleteEntry(alias);
}

From source file:com.otterca.persistence.dao.KeyStoreConfiguration.java

/**
 * Get keystore containing certificates used in DAO unit tests.
 * //from w  w  w.  j a v  a2s . c om
 * @return
 * @throws KeyStoreException
 * @throws CertificateException
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
@Bean
public synchronized KeyStore getKeyStore()
        throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {

    if (keyStore == null) {
        keyStore = KeyStore.getInstance(bundle.getString("keystore.type"));
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource(bundle.getString("keystore.location"));
            is = resource.getInputStream();
            if (masterkey.containsKey("master.password")) {
                BasicTextEncryptor encryptor = new BasicTextEncryptor();
                encryptor.setPassword(masterkey.getString("master.password"));
                keyStore.load(is, encryptor.decrypt(bundle.getString("keystore.password")).toCharArray());
            } else {
                keyStore.load(is, null);
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
    return keyStore;
}

From source file:com.mobicage.rogerthat.util.http.HTTPUtil.java

private static KeyStore loadTrustStore() {
    if (sTrustStore == null) {
        String keyStorePassword = "rogerthat";
        try {//from  w  w  w .  ja  v a  2  s . com
            final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            InputStream instream = App.getContext().getAssets().open("truststore.bks");
            try {
                keyStore.load(instream, keyStorePassword.toCharArray());
            } finally {
                instream.close();
            }
            sTrustStore = keyStore;
        } catch (Exception e) {
            throw new RuntimeException("Could not load keyStore from assets dir", e);
        }
    }
    return sTrustStore;
}

From source file:com.netscape.cmstools.pkcs11.PKCS11CertShowCLI.java

public void execute(String[] args) throws Exception {

    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        printHelp();/*  w  ww .  ja  v  a 2  s . c  o  m*/
        return;
    }

    if (cmd.hasOption("verbose")) {
        PKILogger.setLevel(PKILogger.Level.INFO);

    } else if (cmd.hasOption("debug")) {
        PKILogger.setLevel(PKILogger.Level.DEBUG);
    }

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length < 1) {
        throw new Exception("Missing cert ID.");
    }

    String alias = cmdArgs[0];

    String tokenName = getConfig().getTokenName();
    CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);

    KeyStore ks = KeyStore.getInstance("pkcs11");
    ks.load(new JSSLoadStoreParameter(token));

    Certificate cert = ks.getCertificate(alias);

    if (cert == null) {
        throw new Exception("Certificate not found: " + alias);
    }

    PKCS11CertCLI.printCertInfo(alias, cert);
}

From source file:com.netscape.cmstools.pkcs11.PKCS11CertRemoveCLI.java

public void execute(String[] args) throws Exception {

    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        printHelp();/*ww w. j a v a 2 s  .  c o m*/
        return;
    }

    if (cmd.hasOption("verbose")) {
        PKILogger.setLevel(PKILogger.Level.INFO);

    } else if (cmd.hasOption("debug")) {
        PKILogger.setLevel(PKILogger.Level.DEBUG);
    }

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length < 1) {
        throw new Exception("Missing cert ID.");
    }

    String alias = cmdArgs[0];

    String tokenName = getConfig().getTokenName();
    CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);

    KeyStore ks = KeyStore.getInstance("pkcs11");
    ks.load(new JSSLoadStoreParameter(token));

    Certificate cert = ks.getCertificate(alias);

    if (cert == null) {
        throw new Exception("Certificate not found: " + alias);
    }

    ks.deleteEntry(alias);
}

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  ww. j av  a2s. co 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);
    }
}

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

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

From source file:hu.javaforum.android.soap.ssl.HttpsClientFactory.java

/**
 * Creates a DefaultHttpClient implementation with trusts all certificate.
 * //from  ww w  .ja va 2  s .c  o  m
 * @param params
 *            The HttpParams
 * @return The DefaultHttpClient implementation
 * @throws KeyManagementException
 *             KeyManagementException
 * @throws KeyStoreException
 *             KeyStoreException
 * @throws NoSuchAlgorithmException
 *             NoSuchAlgorithmException
 * @throws UnrecoverableKeyException
 *             UnrecoverableKeyException
 */
public static HttpClient createTrustAllInstance(final HttpParams params)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    try {
        return createClient(new AllTrustSSLSocketFactory(KeyStore.getInstance(KeyStore.getDefaultType())),
                params);
    } finally {
    }
}

From source file:ch.truesolutions.payit.https.EasySSLProtocolSocketFactory.java

private static SSLContext createEasySSLContext() {
    try {/*from w  ww.  j  a v a2s.c  om*/
        // DS create a KeyStore todo
        KeyStore keyStore = KeyStore.getInstance("JKS");

        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new EasyX509TrustManager(keyStore) }, null);
        return context;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e.toString());
    }
}

From source file:neembuu.release1.httpclient.NHttpClient.java

public static DefaultHttpClient getNewInstance() {
    DefaultHttpClient new_httpClient = null;
    new_httpClient = new DefaultHttpClient();
    GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings();
    HttpContext context = new BasicHttpContext();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80));

    try {//  w w w.  j a  va  2 s .  co  m
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080));
    } catch (Exception a) {
        a.printStackTrace(System.err);
    }

    context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry);
    context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
            new BasicScheme()/*file.httpClient.getAuthSchemes()*/);

    context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
            new_httpClient.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/
    );

    BasicCookieStore basicCookieStore = new BasicCookieStore();

    context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/);
    context.setAttribute(ClientContext.CREDS_PROVIDER,
            new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/);

    HttpConnection hc = new DefaultHttpClientConnection();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc);

    //System.out.println(file.httpClient.getParams().getParameter("http.useragent"));
    HttpParams httpParams = new BasicHttpParams();

    if (proxySettings != null) {
        AuthState as = new AuthState();
        as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
        as.setAuthScope(AuthScope.ANY);
        as.setAuthScheme(new BasicScheme());
        httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as);
        httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port));
    }

    new_httpClient = new DefaultHttpClient(
            new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry),
            httpParams/*file.httpClient.getParams()*/);

    if (proxySettings != null) {
        new_httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
    }

    return new_httpClient;
}