-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
kannanjgithub
wants to merge
36
commits into
grpc:master
Choose a base branch
from
kannanjgithub:authoritychecktls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
fa36f83
In-progress changes.
kannanjgithub f31b8bc
In-progress changes that used ExtendedSSLSession.
kannanjgithub 0152478
Authority verify in Netty transport.
kannanjgithub 5e2e22e
Netty authority verify against peer host in server cert.
kannanjgithub b0f86cf
unit test.
kannanjgithub 5ba5431
nit changes and drop unintended changes.
kannanjgithub e42492b
nit changes and drop unintended changes.
kannanjgithub 5285353
Cache the peer verification result.
kannanjgithub ea969ef
Move extraction of X509ExtendedTrustManager to utils.
kannanjgithub 94172de
Fixes.
kannanjgithub 0ddd42c
Fixes.
kannanjgithub 5109115
Fixes.
kannanjgithub d5f3968
nit
kannanjgithub 1e072d4
In-progress review comments.
kannanjgithub 32104ce
Address review comments.
kannanjgithub 95aae4a
revert unintended
kannanjgithub c0327b5
revert unintended
kannanjgithub b24a605
Address review comments.
kannanjgithub 862b95b
Fix warning.
kannanjgithub 8902dbd
Address review comments.
kannanjgithub 01b0eb2
Address review comments.
kannanjgithub 8dd8749
Remove duplicate definitions of createTrustManager.
kannanjgithub 3d744d9
Review comments.
kannanjgithub 4ef0fdb
Move NoopSslSession to io.grpc.internal under grpc-core project.
kannanjgithub fdc6e94
Added flag with default false for the per-rpc authority check.
kannanjgithub 60efd84
Fix style.
kannanjgithub d2af807
Merge branch 'master' into authoritychecktls
kannanjgithub 2594842
Fix merge conflicts.
kannanjgithub 916d0d5
Review comments and use reflection for X509ExtendedTrustManager.
kannanjgithub 040035b
Include failed method name in the tls verification failed log message.
kannanjgithub 44f2412
Address review comments.
kannanjgithub 6449728
Address review comments.
kannanjgithub 4273452
Address review comments.
kannanjgithub a9a019b
Remove the code handling for impossible exception.
kannanjgithub 1f56282
Update comment for NoSuchMethodError.
kannanjgithub b8b70bf
Merge branch 'master' into authoritychecktls
kannanjgithub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
core/src/main/java/io/grpc/internal/NoopSslSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.