Skip to content

Commit cfaba00

Browse files
Fixed problem with android versions <= 4.4 (#1957)
1 parent 8d645af commit cfaba00

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.microsoft.codepush.react;
22

3+
import android.os.Build;
4+
35
import org.json.JSONObject;
46

57
import java.io.BufferedInputStream;
@@ -12,6 +14,8 @@
1214
import java.net.URL;
1315
import java.nio.ByteBuffer;
1416

17+
import javax.net.ssl.HttpsURLConnection;
18+
1519
public class CodePushUpdateManager {
1620

1721
private String mDocumentsDirectory;
@@ -163,6 +167,16 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
163167
try {
164168
URL downloadUrl = new URL(downloadUrlString);
165169
connection = (HttpURLConnection) (downloadUrl.openConnection());
170+
171+
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP &&
172+
downloadUrl.toString().startsWith("https")) {
173+
try {
174+
((HttpsURLConnection)connection).setSSLSocketFactory(new TLSSocketFactory());
175+
} catch (Exception e) {
176+
throw new CodePushUnknownException("Error set SSLSocketFactory. ", e);
177+
}
178+
}
179+
166180
connection.setRequestProperty("Accept-Encoding", "identity");
167181
bin = new BufferedInputStream(connection.getInputStream());
168182

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.microsoft.codepush.react;
2+
3+
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.Socket;
6+
import java.net.UnknownHostException;
7+
import java.security.KeyManagementException;
8+
import java.security.NoSuchAlgorithmException;
9+
10+
import javax.net.ssl.SSLContext;
11+
import javax.net.ssl.SSLSocket;
12+
import javax.net.ssl.SSLSocketFactory;
13+
14+
public class TLSSocketFactory extends SSLSocketFactory {
15+
16+
private SSLSocketFactory delegate;
17+
18+
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
19+
SSLContext context = SSLContext.getInstance("TLS");
20+
context.init(null, null, null);
21+
delegate = context.getSocketFactory();
22+
}
23+
24+
@Override
25+
public String[] getDefaultCipherSuites() {
26+
return delegate.getDefaultCipherSuites();
27+
}
28+
29+
@Override
30+
public String[] getSupportedCipherSuites() {
31+
return delegate.getSupportedCipherSuites();
32+
}
33+
34+
@Override
35+
public Socket createSocket() throws IOException {
36+
return enableTLSOnSocket(delegate.createSocket());
37+
}
38+
39+
@Override
40+
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
41+
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
42+
}
43+
44+
@Override
45+
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
46+
return enableTLSOnSocket(delegate.createSocket(host, port));
47+
}
48+
49+
@Override
50+
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
51+
throws IOException, UnknownHostException {
52+
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
53+
}
54+
55+
@Override
56+
public Socket createSocket(InetAddress host, int port) throws IOException {
57+
return enableTLSOnSocket(delegate.createSocket(host, port));
58+
}
59+
60+
@Override
61+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
62+
throws IOException {
63+
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
64+
}
65+
66+
private Socket enableTLSOnSocket(Socket socket) {
67+
if (socket != null && (socket instanceof SSLSocket)) {
68+
((SSLSocket) socket).setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" });
69+
}
70+
return socket;
71+
}
72+
}

0 commit comments

Comments
 (0)