0

I admit there is a possibility that I am not well informed about the subject, but I've done a LOADS of reading and I still can't get answer to my question.

From what I have learnt, to make communication secure with HTTPS I need to be using some sort of public key (reminds me of pgp-encryption).

My goal is to make a secured POST request from my java application (which I, in the moment it starts working, will rewrite to Android app, if it matters) to a php application accessible via https address.

Naturally I did some Google research on the topic and I got a lot of results how to make ssl connection. Non of those results used any sort of certificate/hash prints. They just use HttpsURLConnection instead of HttpURLConnection, everything else is almost identical.

Right now, almost copy paste of something I found here is this:

String httpsURL = "https://xx.yyyy.zzz/requestHandler.php?getParam1=value1&getParam2=value2";

    String query = "email=" + URLEncoder.encode("abc@xyz.com", "UTF-8");
    query+="&";
    query+="password="+URLEncoder.encode("tramtarie","UTF-8");

    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
    con.setRequestMethod("POST");

    con.setRequestProperty("Content-length",String.valueOf(query.length()));
    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    con.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
    con.setDoOutput(true);
    con.setDoInput(true);

    DataOutputStream output = new DataOutputStream(con.getOutputStream());


    output.writeBytes(query);

    output.close();


    DataInputStream input = new DataInputStream(con.getInputStream());


    for(
    int c = input.read();
    c!=-1;c=input.read())
            System.out.print((char)c);
    input.close();

    System.out.println("Resp Code:"+con.getResponseCode());
    System.out.println("Resp Message:"+con.getResponseMessage());

Which sadly does not work and ends up with this exception:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching app.elessy.cz found

This probably means that it checks the certificate and finds out that the certificate I am using does not match domain name for which is registered (it is webhosting certificate, registered for webhosting domain, not the domain I own, the only reason I am using https is to secure data for internal purposes, I do not want this site to be visited by users from outside, so this certificate should be ok).

There are two things that I just don't get about the code and everything.

  1. No code I have been able to find use MD5/SHA-1 (supposedly the public keys for message encryption?) prints or certificate, they just somehow automatically connect to https website and should work. Doesn't work for me though.
  2. Do I really need those md5/sha-1 prints that are provided to me? Or at least, what in the given context do those prints mean?

Edit:

Following the given answer and duplicate mark, I managed to get it working - in the meaning that I can communicate with application behind https. But I didnt have to use any sort of md5/sha1 print. How do I know now that it is safe? Does this protocol on his own? Like that communication is secured either way, when I use built-in java classes to connect to app behind https?

I probably do not seek for precise technical explanation, but more for an assurance that yes - the communication is safe even though I do not use (knowingly) certificate/servers public key to encrypt my messages. That it does the ssl connection for me.

Community
  • 1
  • 1
FanaticD
  • 1,416
  • 4
  • 20
  • 36
  • 1
    possible duplicate of [How to fix the "java.security.cert.CertificateException: No subject alternative names present" error?](http://stackoverflow.com/questions/19540289/how-to-fix-the-java-security-cert-certificateexception-no-subject-alternative) – ThatOneDude Jul 01 '15 at 18:53
  • 1
    Please see http://stackoverflow.com/questions/19540289/how-to-fix-the-java-security-cert-certificateexception-no-subject-alternative on how to disable the security checks. You don't need those thumbprints, they are provided by your server during SSL connection handshake. – ThatOneDude Jul 01 '15 at 18:53
  • @ssnobody I will have a look at that, thank you. But I wouldn't exactly say this is a straight duplicate. In case it helps I will mark it as one though. – FanaticD Jul 01 '15 at 19:08
  • 1
    You want to disable the certificate validation that is built-in since you have determined it is safe to do so. The error you cite indicates you are currently failing those certificate validation security checks. The linked question seems to be the same, though a comment explaining why this is not like that one might help the mods in their determination. – ThatOneDude Jul 01 '15 at 19:14
  • @ssnobody I tried to clarify this more, thank you for your help, I will upvote your comments. – FanaticD Jul 02 '15 at 10:39
  • From the other answer, openssl s_client -showcerts -connect AAA.BBB.CCC.DDD:443 will show the certs that the server is presenting. You can check that this certificate is what you were expecting. If you'd like to go the java route, see http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/ReadDebug.html – ThatOneDude Jul 02 '15 at 18:00

0 Answers0