-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable TLS 1.1 and TLS 1.2 on Android 4.1-4.4
Summary: This is a proposed patch for issue #7192. Android 4.1-4.4 has support for TLS 1.1 and 1.2 but it is disabled by default. Because of the known security issues and more and more servers switching to TLS 1.2 only, it would be nice for react-native to enable this support. I demonstrated a demo application which showcases the problem and can be used to test this patch. All sources and documentation for it can be found here: https://github.com/bringnow/react-native-tls-test Credits to Alex Gotev (gotev) for the nice implementation. Closes #9840 Differential Revision: D4099446 Pulled By: lacker fbshipit-source-id: 94db320dce6d27f98169e63f834562360c00eef7
- Loading branch information
Showing
2 changed files
with
119 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
ReactAndroid/src/main/java/com/facebook/react/modules/network/TLSSocketFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.facebook.react.modules.network; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.security.KeyManagementException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSocketFactory; | ||
|
||
/** | ||
* | ||
* This class is needed for TLS 1.2 support on Android 4.x | ||
* | ||
* Source: http://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/ | ||
*/ | ||
public class TLSSocketFactory extends SSLSocketFactory { | ||
private SSLSocketFactory delegate; | ||
|
||
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { | ||
SSLContext context = SSLContext.getInstance("TLS"); | ||
context.init(null, null, null); | ||
delegate = context.getSocketFactory(); | ||
} | ||
|
||
@Override | ||
public String[] getDefaultCipherSuites() { | ||
return delegate.getDefaultCipherSuites(); | ||
} | ||
|
||
@Override | ||
public String[] getSupportedCipherSuites() { | ||
return delegate.getSupportedCipherSuites(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { | ||
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { | ||
return enableTLSOnSocket(delegate.createSocket(host, port)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { | ||
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress host, int port) throws IOException { | ||
return enableTLSOnSocket(delegate.createSocket(host, port)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { | ||
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort)); | ||
} | ||
|
||
private Socket enableTLSOnSocket(Socket socket) { | ||
if(socket != null && (socket instanceof SSLSocket)) { | ||
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"}); | ||
} | ||
return socket; | ||
} | ||
} |
55ebb89
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to do, what it should do. Unless I see some JAVA code guidelines not followed, but not sure what is the expectation on this repo.