android12 MTK平台 限定只支持TLS1.2协议

文章详细解释了在Java代码中如何使用SslServerSocket.setEnabledProtocols方法来控制SSL/TLS协议版本,并提到NativeCrypto类在决定设备支持的协议中起到关键作用,特别提到了TLSv1.3和TLSv1.2的默认配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据SslServerSocket.setEnabledProtocols()这个限定支持的协议的方法方法可知里面调用的SSLParametersImpl的setEnabledProtocols方法

 void setEnabledProtocols(String[] protocols) {
281          if (protocols == null) {
282              throw new IllegalArgumentException("protocols == null");
283          }
284          String[] filteredProtocols =
285                  filterFromProtocols(protocols, NativeCrypto.OBSOLETE_PROTOCOL_SSLV3);
286          isEnabledProtocolsFiltered = protocols.length != filteredProtocols.length;
287          enabledProtocols = NativeCrypto.checkEnabledProtocols(filteredProtocols).clone();
288      }

里面调用了NativeCrypto.checkEnabledProtocols方法,NativeCrypto主要就是用于判断设备支持的协议,所以可以做以下修改:

external/conscrypt/common/src/main/java/org/conscrypt/NativeCrypto.java

   /** Protocols to enable by default when "TLSv1.3" is requested. */
     static final String[] TLSV13_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+           // SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
-            SUPPORTED_PROTOCOL_TLSV1_3,
+            //SUPPORTED_PROTOCOL_TLSV1_3,
     };
 
     /** Protocols to enable by default when "TLSv1.2" is requested. */
     static final String[] TLSV12_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+           // SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
     };
 
@@ -991,10 +991,10 @@ public final class NativeCrypto {
 
     static final String[] DEFAULT_PROTOCOLS = TLSV13_PROTOCOLS;
     private static final String[] SUPPORTED_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+            //SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
-            SUPPORTED_PROTOCOL_TLSV1_3,
+            //SUPPORTED_PROTOCOL_TLSV1_3,
     };
 
     static String[] getSupportedProtocols() {
@@ -1044,7 +1044,7 @@ public final class NativeCrypto {
     }
 
     private static int getProtocolConstant(String protocol) {
-        if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) {
+     /**   if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) {
             return NativeConstants.TLS1_VERSION;
         } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1)) {
             return NativeConstants.TLS1_1_VERSION;
@@ -1054,6 +1054,12 @@ public final class NativeCrypto {
             return NativeConstants.TLS1_3_VERSION;
         } else {
             throw new AssertionError("Unknown protocol encountered: " + protocol);
+        } **/
+               
+               if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)) {
+            return NativeConstants.TLS1_2_VERSION;
+        }  else {
+            throw new AssertionError("Unknown protocol encountered: " + protocol);
         }
     }
 
@@ -1065,12 +1071,15 @@ public final class NativeCrypto {
             if (protocol == null) {
                 throw new IllegalArgumentException("protocols contains null");
             }
-            if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1)
+           /** if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_3)
                     && !protocol.equals(OBSOLETE_PROTOCOL_SSLV3)) {
                 throw new IllegalArgumentException("protocol " + protocol + " is not supported");
+            }**/
+                       if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)) {
+                throw new IllegalArgumentException("protocol " + protocol + " is not supported");
             }
         }
         return protocols;

external/conscrypt/repackaged/common/src/main/java/com/android/org/conscrypt/NativeCrypto.java

+
 /**
  * Provides the Java side of our JNI glue for OpenSSL.
  * <p>
@@ -1003,16 +1004,16 @@ public final class NativeCrypto {
 
     /** Protocols to enable by default when "TLSv1.3" is requested. */
     static final String[] TLSV13_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+            //SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
-            SUPPORTED_PROTOCOL_TLSV1_3,
+           // SUPPORTED_PROTOCOL_TLSV1_3,
     };
 
     /** Protocols to enable by default when "TLSv1.2" is requested. */
     static final String[] TLSV12_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+            //SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
     };
 
@@ -1024,10 +1025,10 @@ public final class NativeCrypto {
 
     static final String[] DEFAULT_PROTOCOLS = TLSV13_PROTOCOLS;
     private static final String[] SUPPORTED_PROTOCOLS = new String[] {
-            SUPPORTED_PROTOCOL_TLSV1,
-            SUPPORTED_PROTOCOL_TLSV1_1,
+            //SUPPORTED_PROTOCOL_TLSV1,
+            //SUPPORTED_PROTOCOL_TLSV1_1,
             SUPPORTED_PROTOCOL_TLSV1_2,
-            SUPPORTED_PROTOCOL_TLSV1_3,
+            //SUPPORTED_PROTOCOL_TLSV1_3,
     };
 
     static String[] getSupportedProtocols() {
@@ -1077,7 +1078,7 @@ public final class NativeCrypto {
     }
 
     private static int getProtocolConstant(String protocol) {
-        if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) {
+      /**  if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1)) {
             return NativeConstants.TLS1_VERSION;
         } else if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1)) {
             return NativeConstants.TLS1_1_VERSION;
@@ -1087,6 +1088,12 @@ public final class NativeCrypto {
             return NativeConstants.TLS1_3_VERSION;
         } else {
             throw new AssertionError("Unknown protocol encountered: " + protocol);
+        } **/
+               
+               if (protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)) {
+            return NativeConstants.TLS1_2_VERSION;
+        } else {
+            throw new AssertionError("Unknown protocol encountered: " + protocol);
         }
     }
 
@@ -1098,12 +1105,16 @@ public final class NativeCrypto {
             if (protocol == null) {
                 throw new IllegalArgumentException("protocols contains null");
             }
-            if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1)
+           /** if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_1)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)
                     && !protocol.equals(SUPPORTED_PROTOCOL_TLSV1_3)
                     && !protocol.equals(OBSOLETE_PROTOCOL_SSLV3)) {
                 throw new IllegalArgumentException("protocol " + protocol + " is not supported");
+            } **/
+                       
+                       if (!protocol.equals(SUPPORTED_PROTOCOL_TLSV1_2)) {
+                throw new IllegalArgumentException("protocol " + protocol + " is not supported");
             }
         }
         return protocols;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值