1. Download our Official Android App: Forums for Android!

Apps Https SSLPeerUnverifiedException

Discussion in 'Android Development' started by HeavensSentSword, Aug 1, 2016.

  1. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    I am trying to connection to a goDaddy server with at cert from them that I have installed in the raw resource folder in the app. I am trying to download a json file form it but then get this exception.
    Code (Java):
    1. javax.net.ssl.SSLPeerUnverifiedException: Hostname domaibname not verified:
    This is a common problem but I have tried to find ways to get this to work right.
    This is the class that I am using to do all the Https handling code
    Code (Java):
    1. public class MyHttpsGet extends AsyncTask<String, Void, String> {
    2.  
    3.     Context context;
    4.  
    5.     int cert;
    6.     boolean allowHost;
    7.     private String username;
    8.     private String password;
    9.  
    10.     //this is used if you need a password and username
    11.     //mainly for logins to a webserver
    12.     public MyHttpsGet(String username, String password, Context context, int cert)
    13.     {
    14.         this.context = context;
    15.         this.cert = cert;
    16.         this.allowHost = allowHost;
    17.         this.username = username;
    18.         this.password = password;
    19.  
    20.     }
    21.  
    22.     //used for image downloading
    23.     public MyHttpsGet(){}
    24.  
    25.     @Override
    26.     protected String doInBackground(String... params) {
    27.         String url = params[0];
    28.         return httpsDownloadData(url, context, cert);
    29.     }
    30.  
    31.     public String httpsDownloadData (String urlString, Context context, int certRawResId)
    32.     {
    33.         String respone = null;
    34.  
    35.         try {
    36.             // build key store with ca certificate
    37.             KeyStore keyStore = buildKeyStore(context, certRawResId);
    38.  
    39.             // Create a TrustManager that trusts the CAs in our KeyStore
    40.             String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    41.             TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    42.             tmf.init(keyStore);
    43.  
    44.             // Create an SSLContext that uses our TrustManager
    45.             SSLContext sslContext = SSLContext.getInstance("TLS");
    46.             sslContext.init(null, tmf.getTrustManagers(), null);
    47.  
    48.             // Create a connection from url
    49.             URL url = new URL(urlString);
    50.             if (username != null) {
    51.                 Authenticator.setDefault(new Authenticator() {
    52.                     @Override
    53.                     protected PasswordAuthentication getPasswordAuthentication() {
    54.                         return new PasswordAuthentication(username, password.toCharArray());
    55.                     }
    56.                 });
    57.             }
    58.             HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    59.             urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
    60.  
    61.  
    62.             int statusCode = urlConnection.getResponseCode();
    63.             Log.d("Status code: ", Integer.toString(statusCode));
    64.  
    65.  
    66.             InputStream inputStream = urlConnection.getInputStream();
    67.             if (inputStream != null) {
    68.                 respone = streamToString(inputStream);
    69.                 inputStream.close();
    70.             }
    71.  
    72.         }catch (IOException e) {
    73.             e.printStackTrace();
    74.         } catch (NoSuchAlgorithmException e) {
    75.             e.printStackTrace();
    76.         } catch (KeyStoreException e) {
    77.             e.printStackTrace();
    78.         } catch (KeyManagementException e) {
    79.             e.printStackTrace();
    80.         }
    81.  
    82.         Log.d("MyHttps Respones: ", respone);
    83.         return respone;
    84.     }
    85.  
    86.  
    87.     private static KeyStore buildKeyStore(Context context, int certRawResId){
    88.         // init a default key store
    89.         String keyStoreType = KeyStore.getDefaultType();
    90.         KeyStore keyStore = null;
    91.         try {
    92.             keyStore = KeyStore.getInstance(keyStoreType);
    93.             keyStore.load(null, null);
    94.  
    95.             // read and add certificate authority
    96.             Certificate cert = readCert(context, certRawResId);
    97.             keyStore.setCertificateEntry("ca", cert);
    98.  
    99.  
    100.         } catch (CertificateException e) {
    101.             e.printStackTrace();
    102.         } catch (NoSuchAlgorithmException e) {
    103.             e.printStackTrace();
    104.         } catch (KeyStoreException e) {
    105.             e.printStackTrace();
    106.         } catch (IOException e) {
    107.             e.printStackTrace();
    108.         }
    109.         return keyStore;
    110.  
    111.     }
    112.  
    113.     private static Certificate readCert(Context context, int certResourceId) throws IOException {
    114.  
    115.         // read certificate resource
    116.         InputStream caInput = context.getResources().openRawResource(certResourceId);
    117.  
    118.         Certificate ca = null;
    119.         try {
    120.             // generate a certificate
    121.             CertificateFactory cf = CertificateFactory.getInstance("X.509");
    122.             ca = cf.generateCertificate(caInput);
    123.         } catch (CertificateException e) {
    124.             e.printStackTrace();
    125.         } finally {
    126.             caInput.close();
    127.         }
    128.  
    129.         return ca;
    130.     }
    131.  
    132.     //this is used for downloading strings from an http or https connection
    133.     private String streamToString(InputStream is) throws IOException {
    134.  
    135.         StringBuilder sb = new StringBuilder();
    136.         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    137.         String line;
    138.         while ((line = rd.readLine()) != null) {
    139.             sb.append(line);
    140.         }
    141.  
    142.         return sb.toString();
    143.     }
    144.  
    145.  
    146. }
    147.  
    And this is how i am calling it;

    Code (Java):
    1. MyHttpsGet task = new MyHttpsGet(username, password,myContext, R.raw.gdroot_g2);
    2.             try {
    3.                 myJson = task.execute(myUrl).get();
    4.                 Log.d("Promo Json: " , myJson);
    5.             } catch (InterruptedException e) {
    6.                 e.printStackTrace();
    7.             } catch (ExecutionException e) {
    8.                 e.printStackTrace();
    9.             }
    10.             new runningMan().execute();
    Thank you for any help with this, I am stumped on where to go from here.
     

    Advertisement

    #1 HeavensSentSword, Aug 1, 2016
    Last edited: Aug 2, 2016
  2. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    i figured it out. I was using the wrong website so the domain names didn't match. So if anyone needs a easy to use https cert builder and connection class there it is.
    Have fun
     
    scary alien likes this.

Share This Page

Loading...