Example usage for java.security KeyStoreException printStackTrace

List of usage examples for java.security KeyStoreException printStackTrace

Introduction

In this page you can find the example usage for java.security KeyStoreException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.mhise.util.MHISEUtil.java

public static boolean verifyP12StorePassword(String keyStorePath, String password, String serialNumber,
        Context ctx) {/* w ww. ja v  a  2s. c om*/
    boolean isInstalledCertificateValid = false;
    KeyStore trustStore = null;
    FileInputStream fin = null;
    try {
        trustStore = KeyStore.getInstance("PKCS12");
    } catch (KeyStoreException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    File file = new File(keyStorePath);
    if (file.exists()) {

        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            trustStore.load(fin, password.toCharArray());
            fin.close();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            MHISEUtil.displayDialog(ctx, "Invalid Password", null);
        } catch (CertificateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            MHISEUtil.displayDialog(ctx, "Invalid Password", null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            MHISEUtil.displayDialog(ctx, "Invalid Password", null);
        }

        Enumeration<String> aliases = null;
        try {
            aliases = trustStore.aliases();
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            while (aliases.hasMoreElements()) {
                String alias = aliases.nextElement();
                java.security.cert.X509Certificate cert = null;
                try {
                    cert = (X509Certificate) trustStore.getCertificate(alias);
                } catch (KeyStoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (cert.getSerialNumber().toString().equals(serialNumber)) {
                    // isInstalledCertificateValid = true; 
                    SharedPreferences sharedPreferences = ctx.getSharedPreferences(Constants.PREFS_NAME,
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();

                    editor.putString(Constants.KEY_SERIAL_NUMBER, "" + cert.getSerialNumber().toString(16));
                    editor.commit();

                    return true;
                }
            }
        } catch (NullPointerException e) {
            // TODO: handle exception
            Logger.debug("password invalid", "" + e);
        }
    }

    return isInstalledCertificateValid;
}

From source file:com.mhise.util.MHISEUtil.java

public static KeyStore loadKeyStore(SharedPreferences sharedPreferences, Context context) {

    KeyStore localTrustStore = null;
    try {/*from w  ww.  jav a  2 s . c  o m*/
        if (MobiusDroid._keyStore == null || MobiusDroid._keyStore.equals(null)) {
            localTrustStore = KeyStore.getInstance("PKCS12");

            String strStoreName = sharedPreferences.getString(Constants.KEY_CERT_NAME, null);
            File newKeystoreFile = new File(Constants.defaultP12StorePath + strStoreName);
            FileInputStream fis = new FileInputStream(newKeystoreFile);
            //FileInputStream fis = context.openFileInput(Constants.defaultP12StoreName);
            String strPassword = MHISEUtil.getStrongPassword(strStoreName);
            Log.i("Password", "" + strPassword);
            char[] password = strPassword.toCharArray();
            localTrustStore.load(fis, password);
            fis.close();
            return localTrustStore;

        } else {
            Logger.debug("MHISEUtil-->1.1", "1" + MobiusDroid._keyStore);
            return MobiusDroid._keyStore;

        }

    } catch (KeyStoreException e) {
        // TODO: handle exception
        Logger.error("MHISEUtil -->loadKeyStore", "Exception--" + e);
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Logger.error("MHISEUtil -->loadKeyStore", "Exception--" + e);
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        Logger.error("MHISEUtil -->loadKeyStore", "Exception--" + e);
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        Logger.error("MHISEUtil -->loadKeyStore", "Exception--" + e);
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Logger.error("MHISEUtil -->loadKeyStore", "Exception--" + e);
    }

    return localTrustStore;
}

From source file:com.ubergeek42.WeechatAndroid.service.RelayService.java

void trustCertificate() {
    if (untrustedCert != null) {
        try {//www.  java2s.  c om
            KeyStore.TrustedCertificateEntry x = new KeyStore.TrustedCertificateEntry(untrustedCert);
            sslKeystore.setEntry(host, x, null);
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        saveKeystore();
    }
}

From source file:com.ubergeek42.WeechatAndroid.service.RelayService.java

private void loadKeystore() {
    boolean createKeystore = false;

    // Get the ssl keystore
    File keystoreFile = new File(getDir("sslDir", Context.MODE_PRIVATE), "keystore.jks");

    try {/*from  w  ww  .j  a va 2s .c  om*/
        sslKeystore = KeyStore.getInstance("BKS");
        sslKeystore.load(new FileInputStream(keystoreFile), KEYSTORE_PASSWORD.toCharArray());
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // Should never happen
        e.printStackTrace();
    } catch (CertificateException e) {
        // Ideally never happens
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        logger.debug("Keystore not found, creating...");
        createKeystore = true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (createKeystore) {
        createKeystore();
    }
}

From source file:com.mhise.util.MHISEUtil.java

/**
*  Returns a list of the file path containing file with extensions .p12 , .pfx , .p7b 
*
*/// ww w.  j a va 2s. c  o  m
public static void movePKCS12(String path, Context ctx) {

    //Move PFX or p12 store to Mobius store 
    /*  try {   
    File certificateFile=new File(path);            
    File _mobiusDirectory = new File(Constants.defaultP12StorePath);            
    if(!_mobiusDirectory.exists())
    {
          _mobiusDirectory.mkdir();
    }            
    File newKeystoreFile = new File(Constants.defaultP12StorePath + Constants.defaultP12StoreName);            
    FileInputStream fin= new FileInputStream(certificateFile);
    FileOutputStream fout= new FileOutputStream(newKeystoreFile);            
    byte buffer[] = new byte[(int) certificateFile.length()];            
    fin.read(buffer);            
    fout.write(buffer);            
    movedPath = newKeystoreFile.getAbsolutePath();
            
                    
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
      */

    try {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //Get saved password
        SharedPreferences sharedPreferences = ctx.getSharedPreferences(Constants.PREFS_NAME,
                Context.MODE_PRIVATE);
        String strPassword = sharedPreferences.getString(Constants.KEY_PKCS12_PASSWORD, null);
        char[] password = strPassword.toCharArray();

        //Load Selected keystore
        File input = new File(path);
        FileInputStream fin = new FileInputStream(input);
        keyStore.load(fin, password);

        OutputStream fos = ctx.openFileOutput(Constants.defaultP12StoreName, Context.MODE_PRIVATE);
        keyStore.store(fos, password);
        fos.close();

    } catch (KeyStoreException e) {
        // TODO: handle exception
        Logger.debug("MHISEUtil-->makePKCS12Store", "KeyStoreException: " + e);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Logger.debug("MHISEUtil-->makePKCS12Store", "FileNotFoundException: " + e);
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        Logger.debug("MHISEUtil-->makePKCS12Store", "NoSuchAlgorithmException: " + e);
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        Logger.debug("MHISEUtil-->makePKCS12Store", "CertificateException: " + e);
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Logger.debug("MHISEUtil-->makePKCS12Store", "IOException: " + e);
        e.printStackTrace();
    }

}

From source file:cvut.fel.mobilevoting.murinrad.communications.Connection.java

/**
 * http://www.coderanch.com/t/207318/sockets/java/do-hold-Java-default-SSL a
 * getter method for outputting the defauld certificate validator
 * //from   w  w  w .j  ava2  s .  c o  m
 * @return
 */
private X509TrustManager getDefaultTrust() {
    TrustManagerFactory trustManagerFactory = null;
    try {
        trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        trustManagerFactory.init((KeyStore) null);
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("JVM Default Trust Managers:");
    for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
        System.out.println(trustManager);

        if (trustManager instanceof X509TrustManager) {
            X509TrustManager x509TrustManager = (X509TrustManager) trustManager;
            return x509TrustManager;
        }
    }
    return null;
}

From source file:com.pyj.http.AsyncHttpClient.java

private SSLSocketFactory getSSLSocketFactory() {
    SSLSocketFactory sf = null;/*from  w  ww  . j  av  a  2  s. c  o  m*/
    try {
        KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
        store.load(null, null);
        sf = new SSLSocketFactoryEx(store);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // ??
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sf;
}

From source file:org.ejbca.ui.cli.CMPKeyUpdateStressTest.java

@Override
protected void execute(String[] args) {
    final String hostName;
    final String keystoreFile;
    final String keystorePassword;
    final String certNameInKeystore;
    final int numberOfThreads;
    final int waitTime;
    final int port;
    final String urlPath;
    final String resultFilePrefix;
    if (args.length < 5) {
        System.out.println(args[0]
                + " <host name> <keystore (p12)> <keystore password> <friendlyname in keystore> [<number of threads>] [<wait time (ms) between each thread is started>] [<port>] [<URL path of servlet. use 'null' to get EJBCA (not proxy) default>] [<certificate file prefix. set this if you want all received certificates stored on files>]");
        System.out.println(//  w  w w  .j a  v a  2  s  .co m
                "EJBCA build configuration requirements: cmp.operationmode=normal, cmp.allowraverifypopo=true, cmp.allowautomatickeyupdate=true, cmp.allowupdatewithsamekey=true");
        //            System.out.println("EJBCA build configuration optional: cmp.ra.certificateprofile=KeyId cmp.ra.endentityprofile=KeyId (used when the KeyId argument should be used as profile name).");
        System.out.println(
                "Ejbca expects the following: There exists an end entity with a generated certificate. The end entity's certificate and its private key are stored in the keystore used "
                        + "in the commandline. The end entity's certificate's 'friendly name' in the keystore is the one used in the command line. Such keystore can be obtained, for example, by specifying "
                        + "the token to be 'P12' when creating the end entity and then download the keystore by choosing 'create keystore' from the public web");
        return;
    }
    hostName = args[1];
    keystoreFile = args[2];
    keystorePassword = args[3];
    certNameInKeystore = args[4];
    numberOfThreads = args.length > 5 ? Integer.parseInt(args[5].trim()) : 1;
    waitTime = args.length > 6 ? Integer.parseInt(args[6].trim()) : 0;
    port = args.length > 7 ? Integer.parseInt(args[7].trim()) : 8080;
    //        isHttp = true;
    urlPath = args.length > 8 && args[8].toLowerCase().indexOf("null") < 0 ? args[8].trim() : null;
    resultFilePrefix = args.length > 9 ? args[9].trim() : null;

    CryptoProviderTools.installBCProviderIfNotAvailable();

    Certificate cacert = null;
    Certificate extracert = null;
    PrivateKey oldCertKey = null;

    FileInputStream file_inputstream;
    try {
        file_inputstream = new FileInputStream(keystoreFile);
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(file_inputstream, keystorePassword.toCharArray());
        Key key = keyStore.getKey(certNameInKeystore, keystorePassword.toCharArray());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        oldCertKey = keyFactory.generatePrivate(keySpec);
        //extracert = keyStore.getCertificate(certNameInKeystore);

        Certificate[] certs = keyStore.getCertificateChain(certNameInKeystore);
        extracert = certs[0];
        cacert = certs[1];

    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
        System.exit(-1);
    } catch (KeyStoreException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (CertificateException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
        System.exit(-1);
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    try {
        new StressTest(hostName, port, numberOfThreads, waitTime, urlPath, resultFilePrefix, keystorePassword,
                cacert, oldCertKey, extracert);
    } catch (Exception e) {
        e.printStackTrace();
    }
}