0

Referred links:

http://developer.android.com/training/articles/security-ssl.html#UnknownCa http://randomizedsort.blogspot.com/2010/09/step-to-step-guide-to-programming.html

I have the following code for connecting to server with self-signed certificate (I use this answer for the keystore part) :

 try
 {
   final KeyStore ks = KeyStore.getInstance("BKS");
   final InputStream inputStream = getApplicationContext().getResources().openRawResource(R.raw.certs);
   ks.load(inputStream, getApplicationContext().getString(R.string.store_pass).toCharArray());
   inputStream.close();

   TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   tmf.init(ks);

   SSLContext context = SSLContext.getInstance("TLS");
   context.init(null, tmf.getTrustManagers(), new SecureRandom());

   URL url = new URL("https://www.mywebsite.com/");
   HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
   urlConnection.setSSLSocketFactory(context.getSocketFactory());
   InputStream in = urlConnection.getInputStream();
 }
 catch(Exception e) {
   Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
   // the toast appear empty (described in question)
 }

If I use my website's name as :

 http://www.mywebsite.com 

then in my surrounding try catch, an exception is thrown :

 com.android.okhttp.internal.http.HttpURLConnectionImpl 
 cannot be cast to javax.net.ssl.HttpsURLConnection

If I use :

 https://www.mywebsite.com

then I still get an exception, but due to some reason, the Exception object 'e' in catch(Exception e) has empty message (its not null), so I can't figure out the exception.

If I try to use :

 Log.d("TEST", e.getMessage());

then I get:

 java.lang.NullPointerException: println needs a message

Can some one point out what I am doing wrong ?

Edit:

Here's the output of e.printStackTrace()

D/TEST﹕ e.printStackTrace() :
06-18 14:20:17.493      W/System.err﹕ android.os.NetworkOnMainThreadException
06-18 14:20:17.493      W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-18 14:20:17.493      W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
06-18 14:20:17.493      W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
06-18 14:20:17.493      W/System.err﹕ at com.android.authmobile.app.TestActivity$1.onClick(TestActivity.java:90)
06-18 14:20:17.493      W/System.err﹕ at android.view.View.performClick(View.java:4438)
06-18 14:20:17.497      W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
06-18 14:20:17.497      W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-18 14:20:17.497      W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-18 14:20:17.497      W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-18 14:20:17.497      W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
06-18 14:20:17.497      W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-18 14:20:17.497      W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-18 14:20:17.497      W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-18 14:20:17.497      W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-18 14:20:17.497      W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
Jake
  • 16,329
  • 50
  • 126
  • 202
  • There is no `catch(Exception e)` in your code shown above. – CommonsWare Jun 18 '14 at 14:11
  • @CommonsWare : added, sorry about that. – Jake Jun 18 '14 at 14:13
  • Is there anything else in LogCat, such as a stack trace logged by something else? – CommonsWare Jun 18 '14 at 14:16
  • @CommonsWare Edited the exception part (added log part as well). – Jake Jun 18 '14 at 14:16
  • try e.printStackTrace() instead. – tritop Jun 18 '14 at 14:18
  • Also see http://stackoverflow.com/questions/8237080/exception-getmessage-is-null – tritop Jun 18 '14 at 14:21
  • No, I meant simply looking at LogCat, to see if something else logged a stack trace or other message before it threw the poorly-crafted message-less `Exception`. – CommonsWare Jun 18 '14 at 14:22
  • @CommonsWare I've edited the output of e.printStackTrace in the catch block. – Jake Jun 18 '14 at 14:25
  • duplicate of http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – CommonsWare Jun 18 '14 at 14:26
  • @CommonsWare One question of my approach, since I am creating a custom keystore and providing my server's pem file in my app, is this certificate pinning for SSL ? – Jake Jun 18 '14 at 15:06
  • More or less. The term "certificate pinning" is not usually used with respect to self-signed certificates. Effectively, checking the validity of a self-signed certificate accomplishes what certificate pinning would. – CommonsWare Jun 18 '14 at 15:08
  • @CommonsWare The keystore steps at the start of my sample code .. does it just trust the server certificate without validating it ? Could you point to some web link on how to check the validity of the certificate ? (I am using the step # 3 in the linked keystore answer). Thanks for all your help ! – Jake Jun 18 '14 at 15:31
  • There is no means to validate whether the file in your app is the actual keystore. The assumption is that your app is not tampered with, and that you are validating the certificate served by the *server* compared against the keystore baked into your app. – CommonsWare Jun 18 '14 at 16:19
  • @CommonsWare I asked a more concrete question of my situation here, thanks !! – Jake Jun 18 '14 at 16:26

1 Answers1

1

You're getting a NetworkOnMainThreadException. Newer versions of Android do not allow you to perform network tasks in the same thread as the UI, as they can cause your app to lock up and go non-responsive. Move your network operation to an AsyncTask. See the Android training doc for Connecting to the Network.

Dan Amato
  • 624
  • 6
  • 5