Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netty: Per-rpc call option authority verification against peer cert subject names #11724

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
fa36f83
In-progress changes.
kannanjgithub Nov 18, 2024
f31b8bc
In-progress changes that used ExtendedSSLSession.
kannanjgithub Nov 26, 2024
0152478
Authority verify in Netty transport.
kannanjgithub Nov 27, 2024
c31995e
In-progress changes for Authority verify in okhttp transport.
kannanjgithub Nov 29, 2024
5e2e22e
Netty authority verify against peer host in server cert.
kannanjgithub Nov 30, 2024
b0f86cf
unit test.
kannanjgithub Dec 1, 2024
dd95f8f
in-progress changes.
kannanjgithub Dec 2, 2024
5ba5431
nit changes and drop unintended changes.
kannanjgithub Dec 2, 2024
e42492b
nit changes and drop unintended changes.
kannanjgithub Dec 2, 2024
5285353
Cache the peer verification result.
kannanjgithub Dec 4, 2024
ea969ef
Move extraction of X509ExtendedTrustManager to utils.
kannanjgithub Dec 4, 2024
4d71cce
commit
kannanjgithub Dec 4, 2024
94172de
Fixes.
kannanjgithub Dec 4, 2024
0ddd42c
Fixes.
kannanjgithub Dec 4, 2024
909b863
Fixes.
kannanjgithub Dec 4, 2024
5109115
Fixes.
kannanjgithub Dec 4, 2024
d5f3968
nit
kannanjgithub Dec 4, 2024
24500f4
Changes.
kannanjgithub Dec 4, 2024
1e072d4
In-progress review comments.
kannanjgithub Dec 9, 2024
32104ce
Address review comments.
kannanjgithub Dec 9, 2024
95aae4a
revert unintended
kannanjgithub Dec 9, 2024
c0327b5
revert unintended
kannanjgithub Dec 9, 2024
b24a605
Address review comments.
kannanjgithub Dec 13, 2024
862b95b
Fix warning.
kannanjgithub Dec 13, 2024
8902dbd
Address review comments.
kannanjgithub Dec 14, 2024
01b0eb2
Address review comments.
kannanjgithub Dec 14, 2024
60bfa06
In progress changes.
kannanjgithub Dec 14, 2024
8dd8749
Remove duplicate definitions of createTrustManager.
kannanjgithub Dec 14, 2024
23a0822
in-progress changes.
kannanjgithub Dec 14, 2024
0a9124c
in-progress changes.
kannanjgithub Dec 14, 2024
f9305e7
in-progress changes.
kannanjgithub Dec 14, 2024
4af94ec
in-progress changes.
kannanjgithub Dec 14, 2024
ece5487
Merge branch 'grpc:master' into authorityverifyokhttp
kannanjgithub Dec 15, 2024
aa59965
Unit tests and using HostnameVerifier in per-rpc.
kannanjgithub Dec 15, 2024
20fa07d
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Dec 15, 2024
3d744d9
Review comments.
kannanjgithub Dec 16, 2024
746717e
Revert unintended changes.
kannanjgithub Dec 16, 2024
0d310b3
Revert unintended changes.
kannanjgithub Dec 16, 2024
4ef0fdb
Move NoopSslSession to io.grpc.internal under grpc-core project.
kannanjgithub Dec 18, 2024
3e2ef72
Address review comments.
kannanjgithub Dec 18, 2024
30b1e14
Address review comments.
kannanjgithub Dec 18, 2024
fdc6e94
Added flag with default false for the per-rpc authority check.
kannanjgithub Dec 19, 2024
f996474
Added flag with default false for the per-rpc authority check and rem…
kannanjgithub Dec 19, 2024
f5b3614
Update README etc to reference 1.69.0
kannanjgithub Dec 9, 2024
86b9529
:Revert "Update README etc to reference 1.69.0"
kannanjgithub Dec 20, 2024
60efd84
Fix style.
kannanjgithub Jan 7, 2025
60b1ee0
Fix style.
kannanjgithub Jan 7, 2025
d2af807
Merge branch 'master' into authoritychecktls
kannanjgithub Jan 7, 2025
2594842
Fix merge conflicts.
kannanjgithub Jan 7, 2025
916d0d5
Review comments and use reflection for X509ExtendedTrustManager.
kannanjgithub Jan 10, 2025
040035b
Include failed method name in the tls verification failed log message.
kannanjgithub Jan 13, 2025
89b24f2
Merge branch 'master' into authorityverifyokhttp
kannanjgithub Jan 13, 2025
ecbf7b7
Use reflection to access X509ExtendedTrustManager.
kannanjgithub Jan 13, 2025
26a94c9
Merge remote-tracking branch 'origin/master' into authorityverifyokhttp
kannanjgithub Jan 13, 2025
ce800f7
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Jan 13, 2025
44f2412
Address review comments.
kannanjgithub Jan 15, 2025
6449728
Address review comments.
kannanjgithub Jan 16, 2025
4273452
Address review comments.
kannanjgithub Jan 17, 2025
a9a019b
Remove the code handling for impossible exception.
kannanjgithub Jan 17, 2025
1f56282
Update comment for NoSuchMethodError.
kannanjgithub Jan 17, 2025
649f53c
Some changes based on similar comments in the authority check for Net…
kannanjgithub Jan 17, 2025
73bb42f
Animal sniffer suppress
kannanjgithub Jan 17, 2025
b8b70bf
Merge branch 'master' into authoritychecktls
kannanjgithub Jan 21, 2025
2af1cca
Ignore animal sniffer errors via annotation in tests.
kannanjgithub Jan 22, 2025
15c8161
In-progress changes to move authority verification to the ChannelHand…
kannanjgithub Jan 24, 2025
7941abc
Save sslEngine and use it later after the handshake is complete, to s…
kannanjgithub Jan 24, 2025
7fcd98d
temp testing changes
kannanjgithub Jan 29, 2025
4c50e71
Changes.
kannanjgithub Jan 30, 2025
da19b28
Merge branch 'authoritychecktls-fork' into authoritychecktls
kannanjgithub Jan 30, 2025
4e24446
Merge branch 'grpc:master' into authoritychecktls
kannanjgithub Jan 30, 2025
8576a4e
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Jan 30, 2025
0627bf4
Merge remote-tracking branch 'origin/authoritychecktls' into authorit…
kannanjgithub Jan 30, 2025
02a1041
Revert "Merge remote-tracking branch 'origin/authorityverifyokhttp' i…
kannanjgithub Jan 30, 2025
1d6f46f
Fix style
kannanjgithub Jan 30, 2025
42058ea
Revert unintended changes.
kannanjgithub Jan 31, 2025
cd93d4e
Revert unintended changes.
kannanjgithub Jan 31, 2025
1ff1f0b
Revert unintended changes.
kannanjgithub Jan 31, 2025
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
71 changes: 71 additions & 0 deletions core/src/main/java/io/grpc/internal/CertificateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedTrustManager;
import javax.security.auth.x500.X500Principal;

/**
* Contains certificate/key PEM file utility method(s) for internal usage.
*/
public class CertificateUtils {
/**
* Creates a X509ExtendedTrustManager using the provided CA certs if applicable for the
* certificate type.
*/
public static Optional<TrustManager> getX509ExtendedTrustManager(InputStream rootCerts)
throws GeneralSecurityException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
} catch (IOException ex) {
// Shouldn't really happen, as we're not loading any data.
throw new GeneralSecurityException(ex);
}
X509Certificate[] certs = CertificateUtils.getX509Certificates(rootCerts);
for (X509Certificate cert : certs) {
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
}

TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
return Arrays.stream(trustManagerFactory.getTrustManagers())
.filter(trustManager -> trustManager instanceof X509ExtendedTrustManager).findFirst();
}

private static X509Certificate[] getX509Certificates(InputStream inputStream)
throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certs = factory.generateCertificates(inputStream);
return certs.toArray(new X509Certificate[0]);
}
}
2 changes: 1 addition & 1 deletion netty/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tasks.named("jar").configure {
dependencies {
api project(':grpc-api'),
libraries.netty.codec.http2
implementation project(':grpc-core'),
implementation project(':grpc-core'),
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
libs.netty.handler.proxy,
libraries.guava,
libraries.errorprone.annotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static InternalProtocolNegotiator.ProtocolNegotiator tls(SslContext sslCo
ObjectPool<? extends Executor> executorPool,
Optional<Runnable> handshakeCompleteRunnable) {
final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext,
executorPool, handshakeCompleteRunnable);
executorPool, handshakeCompleteRunnable, null);
final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {

@Override
Expand Down Expand Up @@ -170,7 +170,7 @@ public static ChannelHandler clientTlsHandler(
ChannelHandler next, SslContext sslContext, String authority,
ChannelLogger negotiationLogger) {
return new ClientTlsHandler(next, sslContext, authority, null, negotiationLogger,
Optional.empty());
Optional.empty(), null);
}

public static class ProtocolNegotiationHandler
Expand Down
2 changes: 1 addition & 1 deletion netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
case PLAINTEXT_UPGRADE:
return ProtocolNegotiators.plaintextUpgrade();
case TLS:
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.empty());
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.empty(), null);
default:
throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType);
}
Expand Down
26 changes: 26 additions & 0 deletions netty/src/main/java/io/grpc/netty/NettyClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@
import io.netty.util.concurrent.GenericFutureListener;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.security.cert.CertificateException;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.net.ssl.SSLPeerUnverifiedException;

/**
* A Netty-based {@link ConnectionClientTransport} implementation.
*/
class NettyClientTransport implements ConnectionClientTransport {
private static final Logger logger = Logger.getLogger(NettyClientTransport.class.getName());

private final InternalLogId logId;
private final Map<ChannelOption<?>, ?> channelOptions;
Expand Down Expand Up @@ -194,6 +199,27 @@ public ClientStream newStream(
if (channel == null) {
return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
}
try {
if (callOptions.getAuthority() != null && !negotiator.mayBeVerifyAuthority(
callOptions.getAuthority())) {
String errMsg = String.format("Peer hostname verification failed for authority '%s'.",
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
callOptions.getAuthority());
logger.log(Level.FINE, errMsg);
return new FailingClientStream(Status.INTERNAL.withDescription(errMsg), tracers);
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (UnsupportedOperationException ex) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
logger.log(Level.FINE,
"Can't allow authority override in rpc when X509ExtendedTrustManager is not available.",
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
callOptions.getAuthority());
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return new FailingClientStream(Status.INTERNAL.withDescription(
"Can't allow authority override in rpc when X509ExtendedTrustManager is not available"),
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
tracers);
} catch (SSLPeerUnverifiedException | CertificateException ex) {
String errMsg = String.format("Peer hostname verification failed for authority '%s'.",
callOptions.getAuthority());
logger.log(Level.FINE, errMsg, ex);
return new FailingClientStream(Status.INTERNAL.withDescription(errMsg), tracers);
}
StatsTraceContext statsTraceCtx =
StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
return new NettyClientStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static ChannelCredentials create(SslContext sslContext) {
Preconditions.checkArgument(sslContext.isClient(),
"Server SSL context can not be used for client channel");
GrpcSslContexts.ensureAlpnAndH2Enabled(sslContext.applicationProtocolNegotiator());
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext));
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext, null));
}
}
14 changes: 14 additions & 0 deletions netty/src/main/java/io/grpc/netty/ProtocolNegotiator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.grpc.internal.ObjectPool;
import io.netty.channel.ChannelHandler;
import io.netty.util.AsciiString;
import java.security.cert.CertificateException;
import java.util.concurrent.Executor;
import javax.net.ssl.SSLPeerUnverifiedException;

/**
* An class that provides a Netty handler to control protocol negotiation.
Expand Down Expand Up @@ -63,4 +65,16 @@ interface ServerFactory {
*/
ProtocolNegotiator newNegotiator(ObjectPool<? extends Executor> offloadExecutorPool);
}

/**
* Verify the authority against peer if applicable depending on the transport credential type.
* @throws UnsupportedOperationException if the verification should happen but the required
* type of TrustManager could not be found.
* @throws SSLPeerUnverifiedException if peer verification failed
* @throws CertificateException if certificates have a problem
*/
default boolean mayBeVerifyAuthority(String authority)
throws SSLPeerUnverifiedException, CertificateException {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return true;
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading