views:

560

answers:

1

I'm trying to get an image via an https URL, and am having some problems. I generate a keystore with Java's keytool command. If I specify the common name (CN) equal to my hostname, such as CN=JONMORRA, and then try to query via my hostname, such as https://JONMORRA:8443/ then it works fine. However, if I specify the common name as my ip address, such that CN=192.168.56.1, and try to query via my ip address, such as https://192.168.56.1:8443/ then I get an error

HTTPS hostname wrong: should be <192.168.56.1>

Which is stating that my hostname is wrong, even though that's what I specified in the keystore.

I would like to use ip addresses instead of hostnames so I can query between Linux and Windows boxes without worrying about hostnames.

Why is the CN not accepting ip addresses, and how can I fix it?

Thanks

+2  A: 

This snippet might work for you:

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify(String urlHostName, SSLSession session) {
        System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
        return true;
    }
};

HttpsURLConnection.setDefaultHostnameVerifier(hv);

If you do try this code, and it doesn't work, please post what is printed for urlHostName and session.getPeerHost().

Also, why does having Windows and Linux boxes interoperating require the use of IP addresses rather than hostnames?

Vinay Sajip
I'm actually using a library to do this transfer, and I don't have access to the source code to implement this.
Jon
It doesn't matter if you don't have the source, just do this in e.g. your main class before loading any classes from the library. Unless your setting gets explicitly overridden, it should work.
Vinay Sajip
This worked perfectly.Thanks!!!
Jon
Hey thanks for the post. Very helpful. What will the effect on security be here? Will this make the service susceptible to man in the middle attacks?
sixtyfootersdude
Since the proposed verifier doesn't actually do any verification it could give rise to a security risk; I assumed this need for IP addresses was due to working in a development environment.
Vinay Sajip