-
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
77
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.
+1,024
−80
Open
Changes from 31 commits
Commits
Show all changes
77 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 c31995e
In-progress changes for Authority verify in okhttp transport.
kannanjgithub 5e2e22e
Netty authority verify against peer host in server cert.
kannanjgithub b0f86cf
unit test.
kannanjgithub dd95f8f
in-progress changes.
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 4d71cce
commit
kannanjgithub 94172de
Fixes.
kannanjgithub 0ddd42c
Fixes.
kannanjgithub 909b863
Fixes.
kannanjgithub 5109115
Fixes.
kannanjgithub d5f3968
nit
kannanjgithub 24500f4
Changes.
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 60bfa06
In progress changes.
kannanjgithub 8dd8749
Remove duplicate definitions of createTrustManager.
kannanjgithub 23a0822
in-progress changes.
kannanjgithub 0a9124c
in-progress changes.
kannanjgithub f9305e7
in-progress changes.
kannanjgithub 4af94ec
in-progress changes.
kannanjgithub ece5487
Merge branch 'grpc:master' into authorityverifyokhttp
kannanjgithub aa59965
Unit tests and using HostnameVerifier in per-rpc.
kannanjgithub 20fa07d
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub 3d744d9
Review comments.
kannanjgithub 746717e
Revert unintended changes.
kannanjgithub 0d310b3
Revert unintended changes.
kannanjgithub 4ef0fdb
Move NoopSslSession to io.grpc.internal under grpc-core project.
kannanjgithub 3e2ef72
Address review comments.
kannanjgithub 30b1e14
Address review comments.
kannanjgithub fdc6e94
Added flag with default false for the per-rpc authority check.
kannanjgithub f996474
Added flag with default false for the per-rpc authority check and rem…
kannanjgithub f5b3614
Update README etc to reference 1.69.0
kannanjgithub 86b9529
:Revert "Update README etc to reference 1.69.0"
kannanjgithub 60efd84
Fix style.
kannanjgithub 60b1ee0
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 89b24f2
Merge branch 'master' into authorityverifyokhttp
kannanjgithub ecbf7b7
Use reflection to access X509ExtendedTrustManager.
kannanjgithub 26a94c9
Merge remote-tracking branch 'origin/master' into authorityverifyokhttp
kannanjgithub ce800f7
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
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 649f53c
Some changes based on similar comments in the authority check for Net…
kannanjgithub 73bb42f
Animal sniffer suppress
kannanjgithub b8b70bf
Merge branch 'master' into authoritychecktls
kannanjgithub 2af1cca
Ignore animal sniffer errors via annotation in tests.
kannanjgithub 15c8161
In-progress changes to move authority verification to the ChannelHand…
kannanjgithub 7941abc
Save sslEngine and use it later after the handshake is complete, to s…
kannanjgithub 7fcd98d
temp testing changes
kannanjgithub 4c50e71
Changes.
kannanjgithub da19b28
Merge branch 'authoritychecktls-fork' into authoritychecktls
kannanjgithub 4e24446
Merge branch 'grpc:master' into authoritychecktls
kannanjgithub 8576a4e
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub 0627bf4
Merge remote-tracking branch 'origin/authoritychecktls' into authorit…
kannanjgithub 02a1041
Revert "Merge remote-tracking branch 'origin/authorityverifyokhttp' i…
kannanjgithub 1d6f46f
Fix style
kannanjgithub 42058ea
Revert unintended changes.
kannanjgithub cd93d4e
Revert unintended changes.
kannanjgithub 1ff1f0b
Revert unintended changes.
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.
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.
When you say "it is already broken because the LB-based override only influences setAuthority()" I thought you were implying what it doesn't influence is that at the authority using time a different authority could still be set into CallOption and ClientCallImpl.start would set it into the stream.
But in the normal sequence I observe that DelayedClientTransport::reprocess occurs later then ClientCallImpl.start, so it will overwrite any earlier authority set into the stream with the authority for which the peer verification actually happens against the peer cert. So what is the problem?
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.
Also when you say we do the authority verification now in NettyClientHandler, where exactly there? At first I thought we would have a NettyClientHandler::verifyAuthority(String authority) invoked from NettyClientStream before enqueuing the CreateStreamCommand. But you said it needs to be done on the transport thread whereas the writing the command to the queue is the application thread.
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.
The problem is the authority returned by the LB policy would be used (stream.setAuthority() will put it in the headers) without being verified (because this code is looking at callOptions).
Where CreateStreamCommand is consumed:
grpc-java/netty/src/main/java/io/grpc/netty/NettyClientHandler.java
Line 582 in 67351c0
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.
Even with the suggested change to move the authority verification code to NettyClientHandler , that code can't still influence the using code for authority in
ClientCallImpl
, right? And this will require plumbing some new code to take into account any authority from picker result to be considered in ClientCallImpl.setAuthority.With both approaches we are failing real stream creation and the headers set into the pending stream such as from ClientCallImpl.setAuthority are only copied into the real stream after its creation, which would have failed if the authority didn't match.
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.
NettyClientHandler would see any changes to
setAuthority()
. Since it is sending the request on-the-wire, it will always have the "correct" authority to verify. The point is not to influence what authority is being used, but verify that the chose authority is appropriate. So, yes, the ClientCallImpl code will call setAuthority(), and the LB result might cause setAuthority(), but NettyClientHandler can fail the RPC before it is sent.PendingStream shouldn't have much to do with it. setAuthority() is after stream creation but before stream start(). It is expected for setAuthority() to be called before start(). Stream start() is the point that CreateStreamCommand is sent to NettyClientHandler.
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.
Moved logic to NettyClientHandler. Yet to write/fix unit tests (planning to do via NettyClilentTransportTest itself like before).
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.
Fixed tests.