From c9c290019e179d0d492187aedca94e82325f6435 Mon Sep 17 00:00:00 2001 From: Idel Pivnitskiy Date: Tue, 22 Oct 2019 23:58:10 -0700 Subject: [PATCH] Add a utility that checks if the a SslProvider supports ALPN (#9693) Motivation: We have a public utility `OpenSsl.isAlpnSupported()` that helps users to check if ALPN is available for `SslProvider.OPENSSL`. However, we do not provide a similar utility for `SslProvider.JDK`. Therefore, users who configured ALPN with `SslProvider.JDK` will get a runtime exception at the time when a new connection will be created. Modifications: - Add public `SslProvider.isAlpnSupported(SslProvider)` utility method that returns `true` if the `SslProvider` supports ALPN; - Deprecate `OpenSsl.isAlpnSupported()`; Result: Users can verify if their environment supports ALPN with `SslProvider` upfront (at bootstrap), instead of failing with runtime exception when a new connection will be created. --- .../JdkAlpnApplicationProtocolNegotiator.java | 4 ++++ .../java/io/netty/handler/ssl/OpenSsl.java | 3 +++ .../java/io/netty/handler/ssl/SslProvider.java | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java b/handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java index 25f7ac8609..7b9cbfbf47 100644 --- a/handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java +++ b/handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java @@ -148,4 +148,8 @@ public final class JdkAlpnApplicationProtocolNegotiator extends JdkBaseApplicati static boolean jdkAlpnSupported() { return PlatformDependent.javaVersion() >= 9 && Java9SslUtils.supportsAlpn(); } + + static boolean isAlpnSupported() { + return AVAILABLE; + } } diff --git a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java index d28f1e6ad6..16b927a72f 100644 --- a/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java +++ b/handler/src/main/java/io/netty/handler/ssl/OpenSsl.java @@ -406,7 +406,10 @@ public final class OpenSsl { /** * Returns {@code true} if the used version of openssl supports * ALPN. + * + * @deprecated use {@link SslProvider#isAlpnSupported(SslProvider)} with {@link SslProvider#OPENSSL}. */ + @Deprecated public static boolean isAlpnSupported() { return version() >= 0x10002000L; } diff --git a/handler/src/main/java/io/netty/handler/ssl/SslProvider.java b/handler/src/main/java/io/netty/handler/ssl/SslProvider.java index 00fc2aac93..e72cfed8d9 100644 --- a/handler/src/main/java/io/netty/handler/ssl/SslProvider.java +++ b/handler/src/main/java/io/netty/handler/ssl/SslProvider.java @@ -35,5 +35,21 @@ public enum SslProvider { * OpenSSL-based implementation which does not have finalizers and instead implements {@link ReferenceCounted}. */ @UnstableApi - OPENSSL_REFCNT + OPENSSL_REFCNT; + + /** + * Returns {@code true} if the specified {@link SslProvider} supports + * TLS ALPN Extension, {@code false} otherwise. + */ + public static boolean isAlpnSupported(final SslProvider provider) { + switch (provider) { + case JDK: + return JdkAlpnApplicationProtocolNegotiator.isAlpnSupported(); + case OPENSSL: + case OPENSSL_REFCNT: + return OpenSsl.isAlpnSupported(); + default: + throw new Error("Unknown SslProvider: " + provider); + } + } }