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 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
36 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
5e2e22e
Netty authority verify against peer host in server cert.
kannanjgithub Nov 30, 2024
b0f86cf
unit test.
kannanjgithub Dec 1, 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
94172de
Fixes.
kannanjgithub Dec 4, 2024
0ddd42c
Fixes.
kannanjgithub Dec 4, 2024
5109115
Fixes.
kannanjgithub Dec 4, 2024
d5f3968
nit
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
8dd8749
Remove duplicate definitions of createTrustManager.
kannanjgithub Dec 14, 2024
3d744d9
Review comments.
kannanjgithub Dec 16, 2024
4ef0fdb
Move NoopSslSession to io.grpc.internal under grpc-core project.
kannanjgithub Dec 18, 2024
fdc6e94
Added flag with default false for the per-rpc authority check.
kannanjgithub Dec 19, 2024
60efd84
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
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
b8b70bf
Merge branch 'master' into authoritychecktls
kannanjgithub Jan 21, 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
66 changes: 66 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,66 @@
/*
* 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.Collection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.x500.X500Principal;

/**
* Contains certificate/key PEM file utility method(s) for internal usage.
*/
public class CertificateUtils {
/**
* Creates X509TrustManagers using the provided CA certs.
*/
public static TrustManager[] createTrustManager(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 trustManagerFactory.getTrustManagers();
}

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]);
}
}
132 changes: 132 additions & 0 deletions core/src/main/java/io/grpc/internal/NoopSslSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.security.Principal;
import java.security.cert.Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;

/** A no-op ssl session, to facilitate overriding only the required methods in specific
* implementations.
*/
public class NoopSslSession implements SSLSession {
@Override
public byte[] getId() {
return new byte[0];
}

@Override
public SSLSessionContext getSessionContext() {
return null;
}

@Override
@SuppressWarnings("deprecation")
public javax.security.cert.X509Certificate[] getPeerCertificateChain() {
throw new UnsupportedOperationException("This method is deprecated and marked for removal. "
+ "Use the getPeerCertificates() method instead.");
}

@Override
public long getCreationTime() {
return 0;
}

@Override
public long getLastAccessedTime() {
return 0;
}

@Override
public void invalidate() {
}

@Override
public boolean isValid() {
return false;
}

@Override
public void putValue(String s, Object o) {
}

@Override
public Object getValue(String s) {
return null;
}

@Override
public void removeValue(String s) {
}

@Override
public String[] getValueNames() {
return new String[0];
}

@Override
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
return new Certificate[0];
}

@Override
public Certificate[] getLocalCertificates() {
return new Certificate[0];
}

@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return null;
}

@Override
public Principal getLocalPrincipal() {
return null;
}

@Override
public String getCipherSuite() {
return null;
}

@Override
public String getProtocol() {
return null;
}

@Override
public String getPeerHost() {
return null;
}

@Override
public int getPeerPort() {
return 0;
}

@Override
public int getPacketBufferSize() {
return 0;
}

@Override
public int getApplicationBufferSize() {
return 0;
}
}
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
10 changes: 10 additions & 0 deletions netty/src/main/java/io/grpc/netty/NettyClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ public ClientStream newStream(
if (channel == null) {
return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
}
if (callOptions.getAuthority() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that we're getting this per-RPC authority separately from the data flow that uses the authority. That makes me nervous, especially for security. As the code changes, it's likely this will break. In fact, it is already broken because the LB-based override only influences setAuthority().

For using the authority, the per-RPC authority is copied to the stream in ClientCallImpl. NettyClientStream then copies it into the Netty Http2Headers (while running an application's thread).

Having the logic in NettyClientTransport has been a bit odd, but fine, and I saw why it was done to avoid plumbing of the protocol negotiator to other places. Normally we'd add RPC logic in NettyClientHandler/NettyClientStream. NettyClientTransport doesn't do much other than create/manage the Netty channel (connection). I suggest we move this logic to NettyClientHandler and forgo any cache synchronization because it will always run on the transport thread.

Status verificationStatus = negotiator.verifyAuthority(callOptions.getAuthority());
if (!verificationStatus.isOk()) {
if (GrpcUtil.getFlag("GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK", false)) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return new FailingClientStream(verificationStatus, tracers);
}
channelLogger.log(ChannelLogger.ChannelLogLevel.WARNING, "Authority '{}' specified via call" +
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
" options for rpc did not match peer certificate's subject names.");
}
}
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));
}
}
Loading
Loading