1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl;
17
18 import io.netty5.util.internal.PlatformDependent;
19
20 import javax.net.ssl.SSLEngine;
21 import java.lang.invoke.MethodHandle;
22 import java.lang.invoke.MethodHandles;
23 import java.lang.invoke.MethodType;
24
25
26
27
28 final class Conscrypt {
29
30
31
32 private static final MethodHandle IS_CONSCRYPT_SSLENGINE;
33
34 static {
35 MethodHandle isConscryptSSLEngine = null;
36
37
38 if (PlatformDependent.javaVersion() < 15 || PlatformDependent.isAndroid()) {
39 try {
40 MethodHandles.Lookup lookup = MethodHandles.lookup();
41 Class<?> providerClass = Class.forName("org.conscrypt.OpenSSLProvider", true,
42 PlatformDependent.getClassLoader(ConscryptAlpnSslEngine.class));
43 lookup.findConstructor(providerClass, MethodType.methodType(void.class)).invoke();
44
45 Class<?> conscryptClass = Class.forName("org.conscrypt.Conscrypt", true,
46 PlatformDependent.getClassLoader(ConscryptAlpnSslEngine.class));
47 isConscryptSSLEngine = lookup.findStatic(conscryptClass, "isConscrypt",
48 MethodType.methodType(boolean.class, SSLEngine.class));
49 } catch (Throwable ignore) {
50
51 }
52 }
53 IS_CONSCRYPT_SSLENGINE = isConscryptSSLEngine;
54 }
55
56
57
58
59 static boolean isAvailable() {
60 return IS_CONSCRYPT_SSLENGINE != null;
61 }
62
63
64
65
66 static boolean isEngineSupported(SSLEngine engine) {
67 try {
68 return IS_CONSCRYPT_SSLENGINE != null && (boolean) IS_CONSCRYPT_SSLENGINE.invokeExact(engine);
69 } catch (IllegalAccessException ignore) {
70 return false;
71 } catch (Throwable ex) {
72 throw new RuntimeException(ex);
73 }
74 }
75
76 private Conscrypt() { }
77 }