Skip to content

Commit f31b8bc

Browse files
committed
In-progress changes that used ExtendedSSLSession.
1 parent fa36f83 commit f31b8bc

File tree

10 files changed

+417
-47
lines changed

10 files changed

+417
-47
lines changed

examples/example-tls/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ application {
7474
applicationDistribution.into('bin') {
7575
from(helloWorldTlsServer)
7676
from(helloWorldTlsClient)
77-
filePermissions {
78-
unix(0755)
79-
}
77+
fileMode = 0755
8078
}
8179
}

examples/example-tls/src/main/java/io/grpc/examples/helloworldtls/HelloWorldClientTls.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.grpc.examples.helloworldtls;
1818

19+
import io.grpc.CallOptions;
1920
import io.grpc.Channel;
2021
import io.grpc.Grpc;
2122
import io.grpc.ManagedChannel;
@@ -52,7 +53,9 @@ public void greet(String name) {
5253
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
5354
HelloReply response;
5455
try {
55-
response = blockingStub.sayHello(request);
56+
// response = blockingStub.sayHello(request);
57+
response = io.grpc.stub.ClientCalls.blockingUnaryCall(
58+
blockingStub.getChannel(), GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT.withAuthority("localhost"), request);
5659
} catch (StatusRuntimeException e) {
5760
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
5861
return;

netty/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ tasks.named("jar").configure {
1818
dependencies {
1919
api project(':grpc-api'),
2020
libraries.netty.codec.http2
21-
implementation project(':grpc-core'),
21+
implementation project(':grpc-util'),
22+
project(':grpc-core'),
2223
libs.netty.handler.proxy,
2324
libraries.guava,
2425
libraries.errorprone.annotations,

netty/src/main/java/io/grpc/netty/InternalProtocolNegotiators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslCo
4444
ObjectPool<? extends Executor> executorPool,
4545
Optional<Runnable> handshakeCompleteRunnable) {
4646
final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext,
47-
executorPool, handshakeCompleteRunnable);
47+
executorPool, handshakeCompleteRunnable, null);
4848
final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
4949

5050
@Override
@@ -170,7 +170,7 @@ public static ChannelHandler clientTlsHandler(
170170
ChannelHandler next, SslContext sslContext, String authority,
171171
ChannelLogger negotiationLogger) {
172172
return new ClientTlsHandler(next, sslContext, authority, null, negotiationLogger,
173-
Optional.empty());
173+
Optional.empty(), null);
174174
}
175175

176176
public static class ProtocolNegotiationHandler

netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
652652
case PLAINTEXT_UPGRADE:
653653
return ProtocolNegotiators.plaintextUpgrade();
654654
case TLS:
655-
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.empty());
655+
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.empty(), null);
656656
default:
657657
throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType);
658658
}

netty/src/main/java/io/grpc/netty/NettyClientTransport.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.grpc.internal.StatsTraceContext;
4646
import io.grpc.internal.TransportTracer;
4747
import io.grpc.netty.NettyChannelBuilder.LocalSocketPicker;
48+
import io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator;
4849
import io.netty.bootstrap.Bootstrap;
4950
import io.netty.channel.Channel;
5051
import io.netty.channel.ChannelFactory;
@@ -60,15 +61,20 @@
6061
import io.netty.util.concurrent.GenericFutureListener;
6162
import java.net.SocketAddress;
6263
import java.nio.channels.ClosedChannelException;
64+
import java.security.cert.CertificateException;
6365
import java.util.Map;
6466
import java.util.concurrent.Executor;
6567
import java.util.concurrent.TimeUnit;
68+
import java.util.logging.Level;
69+
import java.util.logging.Logger;
6670
import javax.annotation.Nullable;
71+
import javax.net.ssl.SSLPeerUnverifiedException;
6772

6873
/**
6974
* A Netty-based {@link ConnectionClientTransport} implementation.
7075
*/
7176
class NettyClientTransport implements ConnectionClientTransport {
77+
private static final Logger logger = Logger.getLogger(NettyClientTransport.class.getName());
7278

7379
private final InternalLogId logId;
7480
private final Map<ChannelOption<?>, ?> channelOptions;
@@ -194,6 +200,23 @@ public ClientStream newStream(
194200
if (channel == null) {
195201
return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
196202
}
203+
if (negotiator instanceof ClientTlsProtocolNegotiator && callOptions.getAuthority() != null) {
204+
ClientTlsProtocolNegotiator clientTlsProtocolNegotiator =
205+
(ClientTlsProtocolNegotiator) negotiator;
206+
if (!clientTlsProtocolNegotiator.canVerifyAuthorityOverride()) {
207+
return new FailingClientStream(Status.INTERNAL.withDescription(
208+
"Can't allow authority override in rpc when X509ExtendedTrustManager is not available"),
209+
tracers);
210+
}
211+
try {
212+
clientTlsProtocolNegotiator.verifyAuthorityAllowedForPeerCert(callOptions.getAuthority());
213+
} catch (SSLPeerUnverifiedException | CertificateException e) {
214+
logger.log(Level.FINE, "Peer hostname verification failed for authority '{}'.",
215+
callOptions.getAuthority());
216+
return new FailingClientStream(Status.INTERNAL.withDescription(
217+
"Peer hostname verification failed for authority"), tracers);
218+
}
219+
}
197220
StatsTraceContext statsTraceCtx =
198221
StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
199222
return new NettyClientStream(

netty/src/main/java/io/grpc/netty/NettySslContextChannelCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public static ChannelCredentials create(SslContext sslContext) {
3434
Preconditions.checkArgument(sslContext.isClient(),
3535
"Server SSL context can not be used for client channel");
3636
GrpcSslContexts.ensureAlpnAndH2Enabled(sslContext.applicationProtocolNegotiator());
37-
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext));
37+
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext, null));
3838
}
3939
}

0 commit comments

Comments
 (0)