Adapt to API changes in Conscrypt 1.0.0.RC11

Motivation:

In google/conscrypt#313 the Conscrypt.Engines class was removed in favor
of methods directly on Conscrypt and overloading. The Conscrypt-using
code in Netty used reflection to access the old API, that doesn't exist
anymore. And thus recent versions of Conscrypt fail to enable things
like ALPN with Netty.

Modifications:

Instead of calling Conscrypt.Engines.isConscrypt, call
Conscrypt.isConscrypt.

Result:

Conscrypt detected properly at runtime.
This commit is contained in:
Eric Anderson 2018-02-01 16:48:09 -08:00 committed by Norman Maurer
parent 5257ec49ec
commit 2923f33530

View File

@ -26,44 +26,43 @@ import java.lang.reflect.Method;
final class Conscrypt { final class Conscrypt {
// This class exists to avoid loading other conscrypt related classes using features only available in JDK8+, // This class exists to avoid loading other conscrypt related classes using features only available in JDK8+,
// because we need to maintain JDK6+ runtime compatibility. // because we need to maintain JDK6+ runtime compatibility.
private static final Class<?> ENGINES_CLASS = getEnginesClass(); private static final Class<?> CONSCRYPT_CLASS = getConscryptClass();
/** /**
* Indicates whether or not conscrypt is available on the current system. * Indicates whether or not conscrypt is available on the current system.
*/ */
static boolean isAvailable() { static boolean isAvailable() {
return ENGINES_CLASS != null && PlatformDependent.javaVersion() >= 8; return CONSCRYPT_CLASS != null && PlatformDependent.javaVersion() >= 8;
} }
static boolean isEngineSupported(SSLEngine engine) { static boolean isEngineSupported(SSLEngine engine) {
return isAvailable() && isConscryptEngine(engine, ENGINES_CLASS); return isAvailable() && isConscryptEngine(engine, CONSCRYPT_CLASS);
} }
private static Class<?> getEnginesClass() { private static Class<?> getConscryptClass() {
try { try {
// Always use bootstrap class loader. Class<?> conscryptClass = Class.forName("org.conscrypt.Conscrypt", true,
Class<?> engineClass = Class.forName("org.conscrypt.Conscrypt$Engines", true,
ConscryptAlpnSslEngine.class.getClassLoader()); ConscryptAlpnSslEngine.class.getClassLoader());
// Ensure that it also has the isConscrypt method. // Ensure that it also has the isConscrypt method.
getIsConscryptMethod(engineClass); getIsConscryptMethod(conscryptClass);
return engineClass; return conscryptClass;
} catch (Throwable ignore) { } catch (Throwable ignore) {
// Conscrypt was not loaded. // Conscrypt was not loaded.
return null; return null;
} }
} }
private static boolean isConscryptEngine(SSLEngine engine, Class<?> enginesClass) { private static boolean isConscryptEngine(SSLEngine engine, Class<?> conscryptClass) {
try { try {
Method method = getIsConscryptMethod(enginesClass); Method method = getIsConscryptMethod(conscryptClass);
return (Boolean) method.invoke(null, engine); return (Boolean) method.invoke(null, engine);
} catch (Throwable ignore) { } catch (Throwable ignore) {
return false; return false;
} }
} }
private static Method getIsConscryptMethod(Class<?> enginesClass) throws NoSuchMethodException { private static Method getIsConscryptMethod(Class<?> conscryptClass) throws NoSuchMethodException {
return enginesClass.getMethod("isConscrypt", SSLEngine.class); return conscryptClass.getMethod("isConscrypt", SSLEngine.class);
} }
private Conscrypt() { } private Conscrypt() { }