Android Question read ssl certifikate from Server

Discussion in 'Android Questions' started by Siam, Aug 4, 2015.

  1. Siam

    Siam Active Member Licensed User

    Hello,

    it is possible to read the information from a ssl-certificate such as fingerprint, validity etc. from a SSL connection with the network library ?

    greetings

    Andy
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    I wasn't able to find any information about this. So I'm not sure.

    You can play with WireShark and monitor the network while you get the certificate in the browser.
     
  3. Siam

    Siam Active Member Licensed User

    hello erel,

    thanks for your feedback!

    I have a java code found with this it should be possible to examine the SSL Certificate. Maybe someone could porting to b4a? (sorry java is not my world)

    greetings

    andy


    Code:
    import java.io.IOException;
    import java.net.URL;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.Certificate;
    import java.security.cert.CertificateEncodingException;
    import javax.net.ssl.HttpsURLConnection;
    import javax.security.cert.CertificateException;

    private static String getFingerprint(String s)
            throws IOException, NoSuchAlgorithmException, CertificateException, CertificateEncodingException
        {
            s = (HttpsURLConnection)(new URL(s)).openConnection();
            s.connect();
            s = s.getServerCertificates()[
    0];
            
    MessageDigest messagedigest = MessageDigest.getInstance("SHA1");
            
    messagedigest.update(s.getEncoded());
            
    return dumpHex(messagedigest.digest());
        
    }
    private static String dumpHex(byte abyte0[])
        {
            int j = abyte0.length;
            StringBuilder stringbuilder = new StringBuilder(j * 3 - 1);
            for (int i = 0; i < j; i++)
            {
                if (i > 0)
                {
                    stringbuilder.append(' ');
                }
                stringbuilder.append(HEX_CHARS[abyte0[i] >> 4 & 0xf]);
                stringbuilder.append(HEX_CHARS[abyte0[i] & 0xf]);
            }

            return stringbuilder.toString();
        }
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Siam

    Siam Active Member Licensed User

    thx erel

    then I'm going to try my luck
     
  6. Siam

    Siam Active Member Licensed User

    :) i have the certificate :) but i'm to stupid to return only the public key from this code

    Code:
    #if java

    import java.io.IOException;
    import java.net.URL;
    import java.security.cert.Certificate;
    import java.security.cert.X509Certificate;
    import java.security.cert.CertificateEncodingException;
    import javax.security.cert.CertificateException;
    import javax.net.ssl.HttpsURLConnection;

    public Certificate chkssl(String Server)
           throws IOException, CertificateException, CertificateEncodingException
        {
            HttpsURLConnection s = (HttpsURLConnection)(new URL(Server)).openConnection();
            s.connect();
               Certificate certs = s.getServerCertificates()[0];
            BA.Log("response" + certs.getPublicKey ());   
            return certs; //"test";
        }


    #end if
    i only will have certs.getPublicKey () but i only get errors if i insert this maybe you can give me a hint ?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Which error do you get?
     
  8. Siam

    Siam Active Member Licensed User

    if i do this

    Code:
    return certs.get ();
    Code:
    src\b4a\example\main.java:471: error: incompatible types
         
    return certs.getPublicKey ();//certs; //"test";
          ^
      required: Certificate
      found:  PublicKey
    if i change this

    Code:
    public PublicKey chkssl(String Server,String Key)
    i get

    Code:
    src\b4a\example\main.java:464: error: cannot find symbol
    public PublicKey chkssl(String Server,String Key)
           ^
      symbol:   class PublicKey
      
    location: class main
    1 error
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Change PublicKey with java.security.PublicKey

    Or:
    Code:
    public byte[] chkssl(...)
     
    return certs.getPublicKey ().getEncoded();
     
  10. Siam

    Siam Active Member Licensed User

    Erel you are great !!!! i'm not finish with my man in the middle Protection but now i know a bit more what i do :D
     
  11. M. Giray Ozkan

    M. Giray Ozkan Member Licensed User

    Dear Siam,

    did you complete your MITM attack protection work? This is exactly what I need in my current project.

    Thank you
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice