Skip to content

Commit 71d966a

Browse files
hamdikahlounEgor
authored andcommitted
[video_player] Fix SSLProtocolException for low API version (flutter#3122)
1 parent ddb335d commit 71d966a

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

packages/video_player/video_player/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.11.1
2+
3+
* Enable TLSv1.1 & TLSv1.2 for API 19 and below.
4+
15
## 0.11.0
26

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

packages/video_player/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package io.flutter.plugins.videoplayer;
66

77
import android.content.Context;
8+
import android.os.Build;
89
import android.util.Log;
910
import android.util.LongSparseArray;
1011
import io.flutter.embedding.engine.loader.FlutterLoader;
@@ -20,6 +21,9 @@
2021
import io.flutter.plugins.videoplayer.Messages.VideoPlayerApi;
2122
import io.flutter.plugins.videoplayer.Messages.VolumeMessage;
2223
import io.flutter.view.TextureRegistry;
24+
import java.security.KeyManagementException;
25+
import java.security.NoSuchAlgorithmException;
26+
import javax.net.ssl.HttpsURLConnection;
2327

2428
/** Android platform implementation of the VideoPlayerPlugin. */
2529
public class VideoPlayerPlugin implements FlutterPlugin, VideoPlayerApi {
@@ -56,6 +60,19 @@ public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registra
5660

5761
@Override
5862
public void onAttachedToEngine(FlutterPluginBinding binding) {
63+
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
64+
try {
65+
HttpsURLConnection.setDefaultSSLSocketFactory(new CustomSSLSocketFactory());
66+
} catch (KeyManagementException | NoSuchAlgorithmException e) {
67+
Log.w(
68+
TAG,
69+
"Failed to enable TLSv1.1 and TLSv1.2 Protocols for API level 19 and below.\n"
70+
+ "For more information about Socket Security, please consult the following link:\n"
71+
+ "https://developer.android.com/reference/javax/net/ssl/SSLSocket",
72+
e);
73+
}
74+
}
75+
5976
@SuppressWarnings("deprecation")
6077
final FlutterLoader flutterLoader = FlutterLoader.getInstance();
6178
this.flutterState =

packages/video_player/video_player/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Flutter plugin for displaying inline video with other Flutter
44
# 0.10.y+z is compatible with 1.0.0, if you land a breaking change bump
55
# the version to 2.0.0.
66
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
7-
version: 0.11.0
7+
version: 0.11.1
88
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player
99

1010
flutter:

0 commit comments

Comments
 (0)