Search code examples
androidimageviewuniversal-image-loaderimage-loadingsslhandshakeexception

Universal-Image-Loader | SSLHandshakeException: Handshake failed


I have a ListView with some content (TextViews, ImageView...) in the items. I'm using UIL by Nostra to load the images in the items but some of them fail to load. This is what do I get, when i call Log.v(String.valueOf(failReason.getCause()); :

11-16 23:52:20.447: V/javax.net.ssl.SSLHandshakeException: Handshake failed(17467): failz
11-16 23:52:20.657: V/NativeCrypto(17467): SSL handshake aborted: ssl=0x15fd758: Failure in SSL library, usually a protocol error
11-16 23:52:20.657: V/NativeCrypto(17467): error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:762 0x4c2ed485:0x00000000)
11-16 23:52:21.207: V/NativeCrypto(17467): SSL handshake aborted: ssl=0x1562468: Failure in SSL library, usually a protocol error
11-16 23:52:21.207: V/NativeCrypto(17467): error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:762 0x4c2ed485:0x00000000)

Don't you know, why is there this problem or how can I solve it?

This is one example image, which doesn't get loaded:

http://bigparty.cz/photos/headlinefoto/13.jpg

(I can attach a Log with the whole error - error which UIL automatically puts to Log)


Solution

  • If I'm right you have to create a certificate, sign it and include it in your app. Or change the server configuration (further information here).

    Otherwise you can trust every handshake within your app. This is not the best approach, but really useful during implementation.

    Include this class in your project

    public class SSLCertificateHandler {
    
    protected static final String TAG = "NukeSSLCerts";
    
    /**
     * Enables https connections
     */
    public static void nuke() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                    return myTrustedAnchors;
                }
    
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
    
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };
    
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
        } catch (Exception e) {
        }
    }
    
    }
    

    Extend your Application, and call the 'nuke' function in your onCreate

    public class YOURApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        //...
    
        // trust all SSL -> HTTPS connection
        SSLCertificateHandler.nuke();
    }
    

    I found this code in SO, but can't find the link at the moment....