Example usage for javax.net.ssl SSLHandshakeException getMessage

List of usage examples for javax.net.ssl SSLHandshakeException getMessage

Introduction

In this page you can find the example usage for javax.net.ssl SSLHandshakeException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.foundstone.certinstaller.CertInstallerActivity.java

/**
 * Tests the certificate chain by making a connection with or without the
 * proxy to the specified URL/*  ww w.j  a v  a2  s.  c o  m*/
 * 
 * @param urlString
 * @param proxyIP
 * @param proxyPort
 */
private void testCertChain(final String urlString, final String proxyIP, final String proxyPort) {

    mCaCertInstalled = false;
    mSiteCertInstalled = false;

    if (TextUtils.isEmpty(urlString)) {
        Toast.makeText(getApplicationContext(), "URL is not set", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "URL is not set");
        return;
    }
    pd = ProgressDialog.show(CertInstallerActivity.this, "Testing the cert chain", "", true, false, null);

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {

            Log.d(TAG, "[+] Starting HTTPS request...");

            HttpsURLConnection urlConnection = null;

            try {
                Log.d(TAG, "[+] Set URL...");
                URL url = new URL("https://" + urlString);

                Log.d(TAG, "[+] Open Connection...");

                // The user could have ProxyDroid running
                if (!TextUtils.isEmpty(proxyIP) && !TextUtils.isEmpty(proxyPort)) {
                    Log.d(TAG, "[+] Using proxy " + proxyIP + ":" + proxyPort);
                    Proxy proxy = new Proxy(Type.HTTP,
                            new InetSocketAddress(proxyIP, Integer.parseInt(proxyPort)));
                    urlConnection = (HttpsURLConnection) url.openConnection(proxy);
                } else {
                    urlConnection = (HttpsURLConnection) url.openConnection();
                }
                urlConnection.setReadTimeout(15000);

                Log.d(TAG, "[+] Get the input stream...");
                InputStream in = urlConnection.getInputStream();
                Log.d(TAG, "[+] Create a buffered reader to read the response...");
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                final StringBuilder builder = new StringBuilder();

                String line = null;
                Log.d(TAG, "[+] Read all of the return....");
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                mResult = builder.toString();

                Log.d(TAG, mResult);

                // If everything passed we set these both to true
                mCaCertInstalled = true;
                mSiteCertInstalled = true;

                // Catch when the CA doesn't exist
                // Error: javax.net.ssl.SSLHandshakeException:
                // java.security.cert.CertPathValidatorException: Trust
                // anchor for certification path not found
            } catch (SSLHandshakeException e) {

                e.printStackTrace();

                // Catch when the hostname does not verify
                // Line 224ish
                // http://source-android.frandroid.com/libcore/luni/src/main/java/libcore/net/http/HttpConnection.java
                // http://docs.oracle.com/javase/1.4.2/docs/api/javax/net/ssl/HostnameVerifier.html#method_detail
            } catch (IOException e) {

                // Found the CA cert installed but not the site cert
                mCaCertInstalled = true;
                e.printStackTrace();
            } catch (Exception e) {
                Log.d(TAG, "[-] Some other exception: " + e.getMessage());
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            pd.dismiss();
            if (mCaCertInstalled && !mSiteCertInstalled) {
                Log.d(TAG, Boolean.toString(mCaCertInstalled));
                Toast.makeText(getApplicationContext(), "Found the CA cert installed", Toast.LENGTH_SHORT)
                        .show();
                setCaTextInstalled();
                setSiteTextNotInstalled();
                setFullTextNotInstalled();
            } else if (mCaCertInstalled && mSiteCertInstalled) {
                Toast.makeText(getApplicationContext(), "Found the CA and Site certs installed",
                        Toast.LENGTH_SHORT).show();
                setCaTextInstalled();
                setSiteTextInstalled();
                setFullTextInstalled();
            } else {
                Toast.makeText(getApplicationContext(), "No Certificates were found installed",
                        Toast.LENGTH_SHORT).show();
                setCaTextNotInstalled();
                setSiteTextNotInstalled();
                setFullTextNotInstalled();
            }
            super.onPostExecute(result);

        }

    }.execute();

}