1. Are you ready for the Galaxy S20? Here is everything we know so far!

HTTPS with self signed cert

Discussion in 'Android Development' started by HeavensSentSword, Feb 8, 2016.

  1. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    So i have been trying to use the android dev site's tutorial on how to use a custom trust manager to allow for connection to a self-signed server. This one https://developer.android.com/training/articles/security-ssl.html
    But when I write this line
    urlConnection.setSSLSocketFactory(context.getSocketFactory());
    it says that it cannot resolve the method setSSLSocketFactory();
    I think I have all the libs I need imported but I dont know what is going wrong here.
    Thank you for any help with this.
    Here is what I have so far.

    Code (Java):
    1. import java.io.BufferedInputStream;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.HttpURLConnection;
    6. import java.security.KeyManagementException;
    7. import java.security.KeyStore;
    8. import java.security.KeyStoreException;
    9. import java.security.NoSuchAlgorithmException;
    10. import java.security.cert.Certificate;
    11. import java.security.cert.CertificateException;
    12. import java.security.cert.CertificateFactory;
    13. import java.security.cert.X509Certificate;
    14. import javax.net.ssl.SSLContext;
    15. import javax.net.ssl.TrustManagerFactory;
    16.  
    17. /**
    18. * Created by MMILLAR on 2/8/2016.
    19. */
    20. public class CertificateHandler {
    21.     private final String myCert = "MyCA.crt";
    22.  
    23.     public void getToken(HttpURLConnection urlConnection) {
    24.  
    25.                   try {
    26.                 // Load CAs from an InputStream
    27.                 // (could be from a resource or ByteArrayInputStream or ...)
    28.                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
    29.                 // From https://www.washington.edu/itconnect/security/ca/load-der.crt
    30.                 InputStream caInput = new BufferedInputStream(new FileInputStream("myCert"));
    31.                 Certificate ca;
    32.                 try {
    33.                     ca = cf.generateCertificate(caInput);
    34.                     System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    35.                 } finally {
    36.                     caInput.close();
    37.                 }
    38.  
    39. // Create a KeyStore containing our trusted CAs
    40.                 String keyStoreType = KeyStore.getDefaultType();
    41.                 KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    42.                 keyStore.load(null, null);
    43.                 keyStore.setCertificateEntry("ca", ca);
    44.  
    45. // Create a TrustManager that trusts the CAs in our KeyStore
    46.                 String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    47.                 TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    48.                 tmf.init(keyStore);
    49.  
    50. // Create an SSLContext that uses our TrustManager
    51.                 SSLContext context = SSLContext.getInstance("TLS");
    52.                 context.init(null, tmf.getTrustManagers(), null);
    53.                 urlConnection.setSSLSocketFactory(context.getSocketFactory());
    54.  
    55.  
    56.         }catch (CertificateException e) {
    57.             e.printStackTrace();
    58.         } catch (NoSuchAlgorithmException e) {
    59.             e.printStackTrace();
    60.         } catch (IOException e) {
    61.             e.printStackTrace();
    62.         } catch (KeyManagementException e) {
    63.             e.printStackTrace();
    64.         } catch (KeyStoreException e) {
    65.             e.printStackTrace();
    66.         }
    67.  
    68.     }
    69. }
    70.  
     


  2. Best Answer:
    Post #2 by HeavensSentSword, Feb 9, 2016 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    Ok I found my issue after looking at it a lot.
    I was using URLHttpConnection and I needed URLHttpsConnection.
    Yes the s is all that I was missing.
     
Loading...
Similar Threads - HTTPS self signed
  1. BloodBlade
    Replies:
    9
    Views:
    907
  2. Olive
    Replies:
    2
    Views:
    741
  3. RicardoM
    Replies:
    1
    Views:
    541
  4. quaildog
    Replies:
    8
    Views:
    1,563
  5. Mysteryman
    Replies:
    3
    Views:
    1,121
  6. JourneymanChris
    Replies:
    0
    Views:
    1,413
  7. cowboy1611
    Replies:
    4
    Views:
    3,287
  8. bukenyafrank62
    Replies:
    2
    Views:
    921
  9. ajhk
    Replies:
    15
    Views:
    2,359
  10. Medha Apps
    Replies:
    1
    Views:
    1,449

Share This Page

Loading...