Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions netty/src/main/java/io/grpc/netty/GrpcSslContexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ private static ApplicationProtocolConfig selectApplicationProtocolConfig(SslProv
if (JettyTlsUtil.isJettyNpnConfigured()) {
return NPN;
}
if (JettyTlsUtil.isJava9AlpnAvailable()) {
return ALPN;
}
// Use the ALPN cause since it is prefered.
throw new IllegalArgumentException(
"Jetty ALPN/NPN has not been properly configured.",
Expand Down
41 changes: 41 additions & 0 deletions netty/src/main/java/io/grpc/netty/JettyTlsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

package io.grpc.netty;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

/**
* Utility class for determining support for Jetty TLS ALPN/NPN.
*/
Expand All @@ -26,6 +32,30 @@ private JettyTlsUtil() {
private static Throwable jettyAlpnUnavailabilityCause;
private static Throwable jettyNpnUnavailabilityCause;

private static class Java9AlpnUnavailabilityCauseHolder {

static final Throwable cause = checkAlpnAvailability();

static Throwable checkAlpnAvailability() {
try {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
SSLEngine engine = context.createSSLEngine();
Method getApplicationProtocol =
AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
@Override
public Method run() throws Exception {
return SSLEngine.class.getMethod("getApplicationProtocol");
}
});
getApplicationProtocol.invoke(engine);
return null;
} catch (Throwable t) {
return t;
}
}
}

/**
* Indicates whether or not the Jetty ALPN jar is installed in the boot classloader.
*/
Expand Down Expand Up @@ -67,4 +97,15 @@ static synchronized Throwable getJettyNpnUnavailabilityCause() {
}
return jettyNpnUnavailabilityCause;
}

/**
* Indicates whether Java 9 ALPN is available.
*/
static boolean isJava9AlpnAvailable() {
return getJava9AlpnUnavailabilityCause() == null;
}

static Throwable getJava9AlpnUnavailabilityCause() {
return Java9AlpnUnavailabilityCauseHolder.cause;
}
}
4 changes: 3 additions & 1 deletion netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public Handler newHandler(GrpcHttp2ConnectionHandler handler) {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
SSLEngine sslEngine = sslContext.newEngine(ctx.alloc(), host, port);
SSLParameters sslParams = new SSLParameters();
SSLParameters sslParams = sslEngine.getSSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
sslEngine.setSSLParameters(sslParams);
ctx.pipeline().replace(this, null, new SslHandler(sslEngine, false));
Expand Down Expand Up @@ -374,6 +374,8 @@ static void logSslEngineDetails(Level level, ChannelHandlerContext ctx, String m
builder.append(" Jetty ALPN");
} else if (JettyTlsUtil.isJettyNpnConfigured()) {
builder.append(" Jetty NPN");
} else if (JettyTlsUtil.isJava9AlpnAvailable()) {
builder.append(" JDK9 ALPN");
}
builder.append("\n TLS Protocol: ");
builder.append(engine.getSession().getProtocol());
Expand Down