Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssl on android strange issue

Tags:

android

ssl

I am trying to upload a file to some url using ssl. I use this code to set trust manager -

public static void trustAll () {
    TrustManage[] trustEverythingTrustManager = new TrustManager[] {
        new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] chain,
            String authType) throws CertificateException {
            // TODO Auto-generated method stub
            }

            public void checkServerTrusted(X509Certificate[] chain,
            String authType) throws CertificateException {
            // TODO Auto-generated method stub

            }

            public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
            }

        }
    };

    SSLContext sc;
    try {
        sc = SSLContext.getInstance("TLS");
        sc.init(null, trustEverythingTrustManager, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }
}

and this one to set host name verifier -

public static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {

    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

then i am opening connection like this -

URL url = new URL("some_url");
HttpURLConnection connection = null;
// check if this is https or just http
if (url.getProtocol().toLowerCase().equals("https")) {
    trustAll();
    HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
    https.setHostnameVerifier(DO_NOT_VERIFY);
    connection = https;
} else {
    connection = (HttpURLConnection) url.openConnection();
}

After i get the connection i use it to upload the data. Is i use http everything is ok but when i try https i get the following exception -

Write error: I/O error during system call, Broken pipe java.io.IOException: Write error: I/O error during system call, Broken pipe at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativewrite(Native Method) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$600(OpenSSLSocketImpl.java:55) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:565) at java.io.OutputStream.write(OutputStream.java:82) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection$HttpOutputStream.write(HttpURLConnection.java:652) at java.io.DataOutputStream.write(DataOutputStream.java:101)

The strange thing is that i don't get exception all the time. When i try to upload small files (200K) it's working O.K. or it's crashing very rare but when i try to upload bigger ones (more than 1MB) i get the exception almost every time.

Any ideas ?

like image 482
Mojo Risin Avatar asked Oct 25 '22 15:10

Mojo Risin


1 Answers

The guys from android reported that is bug in android sdk http://code.google.com/p/android/issues/detail?id=8625.

like image 142
Mojo Risin Avatar answered Nov 08 '22 12:11

Mojo Risin