-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
CheckTrustedIssuer: Fixes for invalid chains #2665
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f6ef082
CheckTrustedIssuer: Fixes for invalid chains
NickCraver 1a5219f
Add release notes
NickCraver 12345b7
PR tweaks, round 1!
NickCraver 4b8e2c0
Move to thumbprint check
NickCraver 95a216e
Revert "Move to thumbprint check"
NickCraver 4b064dd
PR tweaks, round 2
NickCraver afc4083
Catch CryptographicException specifically to ignore
NickCraver 25aec52
Dispose all certs
NickCraver 029693d
Test cleanup
NickCraver df29d39
Add .NET 8 reminder
NickCraver 8450b23
Update src/StackExchange.Redis/ConfigurationOptions.cs
NickCraver 8877bb2
Fix comment
NickCraver 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
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
58 changes: 58 additions & 0 deletions
58
tests/StackExchange.Redis.Tests/Certificates/CertValidationTests.cs
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,58 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Security; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace StackExchange.Redis.Tests; | ||
|
||
public class CertValidationTests : TestBase | ||
{ | ||
public CertValidationTests(ITestOutputHelper output) : base(output) { } | ||
|
||
[Fact] | ||
public void CheckIssuerValidity() | ||
{ | ||
// The endpoint cert is the same here | ||
var endpointCert = LoadCert(Path.Combine("Certificates", "device01.foo.com.pem")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking in certificates that will eventually expire; you could use System.Security.Cryptography.X509Certificates.CertificateRequest to just create them on the fly. (net472+/net5+) |
||
|
||
// Trusting CA explicitly | ||
var callback = ConfigurationOptions.TrustIssuerCallback(Path.Combine("Certificates", "ca.foo.com.pem")); | ||
Assert.True(callback(this, endpointCert, null, SslPolicyErrors.None)); | ||
Assert.True(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
|
||
// Trusting the remote endpoint cert directly | ||
callback = ConfigurationOptions.TrustIssuerCallback(Path.Combine("Certificates", "device01.foo.com.pem")); | ||
Assert.True(callback(this, endpointCert, null, SslPolicyErrors.None)); | ||
Assert.True(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
|
||
// Attempting to trust another CA (mismatch) | ||
callback = ConfigurationOptions.TrustIssuerCallback(Path.Combine("Certificates", "ca2.foo.com.pem")); | ||
Assert.True(callback(this, endpointCert, null, SslPolicyErrors.None)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch)); | ||
Assert.False(callback(this, endpointCert, null, SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNotAvailable)); | ||
} | ||
|
||
private static X509Certificate2 LoadCert(string certificatePath) => new X509Certificate2(File.ReadAllBytes(certificatePath)); | ||
|
||
[Fact] | ||
public void CheckIssuerArgs() | ||
{ | ||
Assert.ThrowsAny<Exception>(() => ConfigurationOptions.TrustIssuerCallback("")); | ||
|
||
var opt = new ConfigurationOptions(); | ||
Assert.Throws<ArgumentNullException>(() => opt.TrustIssuer((X509Certificate2)null!)); | ||
} | ||
} |
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,4 @@ | ||
The certificates here are randomly generated for testing only. | ||
They are not valid and only used for test validation. | ||
|
||
Please do not file security issue noise - these have no impact being public at all. |
20 changes: 20 additions & 0 deletions
20
tests/StackExchange.Redis.Tests/Certificates/ca.foo.com.pem
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,20 @@ | ||
-----BEGIN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2gqPHMQIiQcPyX1syT7yhJAyDQRYOG | ||
cHSRojNciYVotSTgyYguUJdU7vfRJ+MLfoZClzJgvNav8yUC+sSrb5RD5vQlvxzG | ||
KrJ8Hh+hpIFsmQKj5dcochKvLLd1Z748b2+FB0jtxraU | ||
-----END CERTIFICATE----- |
20 changes: 20 additions & 0 deletions
20
tests/StackExchange.Redis.Tests/Certificates/ca2.foo.com.pem
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,20 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDTzCCAjegAwIBAgIUYXv168vvB1NPg3PfoRzcNFEMaC8wDQYJKoZIhvcNAQEL | ||
BQAwNzEUMBIGA1UEAwwLY2EyLmZvby5jb20xEjAQBgNVBAoMCU15RGV2aWNlczEL | ||
MAkGA1UEBhMCVVMwHhcNMjQwMzA3MTQwMTMyWhcNNDgxMDI3MTQwMTMyWjA3MRQw | ||
EgYDVQQDDAtjYTIuZm9vLmNvbTESMBAGA1UECgwJTXlEZXZpY2VzMQswCQYDVQQG | ||
EwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOFDZ2sf8Ik/I3jD | ||
Mp4NGoo+kMY1BiRWjSKnaphfcosR2MAT/ROIVhnkzSeiQQByf34cqN/7xNkHaufr | ||
oVcMuuiyWERPoZjBqnfzLZ+93uxnyIU6DVDdNIcKcBQxyhHMfOigFhKTia6eWhrf | ||
zSaBhbkndaUsXdINBAJgSq3HDuk8bIw8MTZH0giorhIyyyqT/gjWEbzKx6Ww99qV | ||
MMcjFIvXEmD9AEaNilHD4TtwqZrZKZpnVBaQvWrCK3tCGBDyiFlUhAibchbt/JzV | ||
sK002TFfUbn41ygHmcrBVL7lSEsuT2W/PNuDOdWa6NQ2RVzYivs5jYbWV1cAvAJP | ||
HMWJkZ8CAwEAAaNTMFEwHQYDVR0OBBYEFA6ZeCMPgDEu+eIUoCxU/Q06ViyoMB8G | ||
A1UdIwQYMBaAFA6ZeCMPgDEu+eIUoCxU/Q06ViyoMA8GA1UdEwEB/wQFMAMBAf8w | ||
DQYJKoZIhvcNAQELBQADggEBAGOa/AD0JNPwvyDi9wbVU+Yktx3vfuVyOMbnUQSn | ||
nOyWd6d95rZwbeYyN908PjERQT3EMo8/O0eOpoG9I79vjbcD6LAIbxS9XdI8kK4+ | ||
D4e/DX/R85KoWSprB+VRXGqsChY0Y+4x5x2q/IoCi6+tywhzjqIlaGDYrlc688HO | ||
/+4iR9L945gpg4NT1hLnCwDYcdZ5vjv4NfgXDbGPUcEheYnfz3cHE2mYxEG9KXta | ||
f8hSj/MNNv31BzNcj4XKcDqp4Ke3ow4lAZsPPlixOxxRaLnpsKZmEYYQcLI8KVNk | ||
gdAUOSPZgzRqAag0rvVfrpyvfvlu0D9xeiBLdhaJeZCq1/s= | ||
-----END CERTIFICATE----- |
39 changes: 39 additions & 0 deletions
39
tests/StackExchange.Redis.Tests/Certificates/create_certificates.sh
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,39 @@ | ||
#!/bin/bash | ||
set -eu | ||
# Adapted from https://github.com/stewartadam/dotnet-x509-certificate-verification/blob/main/create_certificates.sh | ||
|
||
base_dir="certificates" | ||
|
||
create_ca() { | ||
local CA_CN="$1" | ||
local certificate_output="${base_dir}/${CA_CN}.pem" | ||
|
||
openssl genrsa -out "${base_dir}/${CA_CN}.key.pem" 2048 # Generate private key | ||
openssl req -x509 -new -nodes -key "${base_dir}/${CA_CN}.key.pem" -sha256 -days 9000 -out "${certificate_output}" -subj "/CN=${CA_CN}/O=MyDevices/C=US" # Generate root certificate | ||
|
||
echo -e "\nCertificate for CA ${CA_CN} saved to ${certificate_output}\n\n" | ||
} | ||
|
||
create_leaf_cert_req() { | ||
local DEVICE_CN="$1" | ||
|
||
openssl genrsa -out "${base_dir}/${DEVICE_CN}.key.pem" 2048 # new private key | ||
openssl req -new -key "${base_dir}/${DEVICE_CN}.key.pem" -out "${base_dir}/${DEVICE_CN}.csr.pem" -subj "/CN=${DEVICE_CN}/O=MyDevices/C=US" # generate signing request for the CA | ||
} | ||
|
||
sign_leaf_cert() { | ||
local DEVICE_CN="$1" | ||
local CA_CN="$2" | ||
local certificate_output="${base_dir}/${DEVICE_CN}.pem" | ||
|
||
openssl x509 -req -in "${base_dir}/${DEVICE_CN}.csr.pem" -CA ""${base_dir}/${CA_CN}.pem"" -CAkey "${base_dir}/${CA_CN}.key.pem" -set_serial 01 -out "${certificate_output}" -days 8999 -sha256 # sign the CSR | ||
|
||
echo -e "\nCertificate for ${DEVICE_CN} saved to ${certificate_output}\n\n" | ||
} | ||
|
||
mkdir -p "${base_dir}" | ||
|
||
# Create one self-issued CA + signed cert | ||
create_ca "ca.foo.com" | ||
create_leaf_cert_req "device01.foo.com" | ||
sign_leaf_cert "device01.foo.com" "ca.foo.com" |
18 changes: 18 additions & 0 deletions
18
tests/StackExchange.Redis.Tests/Certificates/device01.foo.com.pem
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,18 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIC5jCCAc4CAQEwDQYJKoZIhvcNAQELBQAwNjETMBEGA1UEAwwKY2EuZm9vLmNv | ||
bTESMBAGA1UECgwJTXlEZXZpY2VzMQswCQYDVQQGEwJVUzAeFw0yNDAzMDcxNDAx | ||
MzJaFw00ODEwMjYxNDAxMzJaMDwxGTAXBgNVBAMMEGRldmljZTAxLmZvby5jb20x | ||
EjAQBgNVBAoMCU15RGV2aWNlczELMAkGA1UEBhMCVVMwggEiMA0GCSqGSIb3DQEB | ||
AQUAA4IBDwAwggEKAoIBAQDBb4Mv87+MFVGLIWArc0wV1GH4h7Ha+49K+JAi8rtk | ||
fpQACcu3OGq5TjUuxecOz5eXDwJj/vR1rvjP0DaCuIlx4SNXXqVKooWpCLb2g4Mr | ||
IIiFcBsiaJNmhFvd92bqHOyuXsUTjkJKaLmH6nUqVIXEA/Py+jpuSFRp9N475IGZ | ||
yUUdaQUx9Ur953FagLbPVeE5Vh+NEA8vnw+ZBCQRBHlRgvSJtCAR/oznXXPdHGGZ | ||
rMWeNjl+v1iP8fZMq4vvooW0/zTVgH8lE/HJXtpaWEVeGpnOqBsgvl12WTGL5dMU | ||
n81JiI3AdUyW0ieh/5yr+OFNa/HNqGLK1NvnCDPbBFpnAgMBAAEwDQYJKoZIhvcN | ||
AQELBQADggEBAEpIIJJ7q4/EbCJog29Os9l5unn7QJon4R5TGJQIxdqDGrhXG8QA | ||
HiBGl/lFhAp9tfKvQIj4aODzMgHmDpzZmH/yhjDlquJgB4JYDDjf9UhtwUUbRDp0 | ||
rEc5VikLuTJ21hcusKALH5fgBjzplRNPH8P660FxWnv9gSWCMNaiFCCxTU91g4L3 | ||
/qZPTl5nr1j6M/+zXbndS5qlF7GkU5Kv9xEmasZ0Z65Wao6Ufhw+nczLbiLErrxD | ||
zLtTfr6WYFqzXeiPGnjTueG+1cYDjngmj2fbtjPn4W67q8Z0M/ZMj9ikr881d3zP | ||
3dzUEaGexGqvA2MCapIQ2vCCMDF33ismQts= | ||
-----END CERTIFICATE----- |
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.
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.
OK, I lied. One more comment: If you get a trusted chain as input here, you're returning that it's trusted even if the intended trust anchor was missing. So this early success is bad if you're trying to enforce pinning.
If the anchor cert is REQUIRED, then you want this instead to be
That will cause an immediate false return for:
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.
Hm. Based on the documentation I did not think the intention was pinning
StackExchange.Redis/src/StackExchange.Redis/ConfigurationOptions.cs
Line 282 in 441f89a
But a good question to ask.
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.
Since @vcsjones and I have had a discussion offline regarding what that sentence means... if it is pinning, the code's wrong. If it isn't pinning, the docs would probably be more clear by replacing "even if" with "when".
/me and @vcsjones now watch to see what Nick says the right answer should be.
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.
Playing favorites only gets me in trouble, tried going with making everyone angry instead - thoughts?
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.
So is the intention that anything that's trusted by default is acceptable, and if it's not trusted then (and only then) this anchor is checked as a fallback?
If that's the intention, my concern in the docs is with the word "even";
Sounds like pinning to me.
Sounds like a fallback.