-
Notifications
You must be signed in to change notification settings - Fork 565
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
Fix If CACertificatePath Is Empty, Verification Fails #932
Open
dckorben
wants to merge
18
commits into
connamara:master
Choose a base branch
from
dckorben:develop
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 all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bcc356e
Fix: Sql Certificate Verification
dckorben 3397cdf
Inline C509Chain
dckorben b5cd42b
Lint
dckorben f808248
More Lint
dckorben c2724f3
Yet More Lint
dckorben 3ef7993
Add CheckCertificateRevocation
dckorben 21ffb1d
Add Initial SSLStreamFactory Tests
dckorben 3e64432
Test Certs with CA Chain
dckorben c7d1629
Lint
dckorben ecd2496
Fix Double Write on Entity Certs
dckorben 3c78720
Deferr to SslStream for Public Certificate Verification
dckorben 956f824
Fix Null Warning
dckorben 2b2da90
Reduce Changes
dckorben 2cfd627
Update Test Names
dckorben 538d1d4
Cleanup Usings
dckorben 71e83af
Simulate Failure
dckorben 71d36ce
Add VerifyServerCertificateChainFailsWithoutCA
dckorben fdc43fa
Rename Tests
dckorben 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
using System.Security.Authentication; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Cryptography; | ||
using NUnit.Framework; | ||
using QuickFix; | ||
using QuickFix.Logger; | ||
using QuickFix.Transport; | ||
using System; | ||
using System.IO; | ||
using System.Net.Security; | ||
using System.Net.Sockets; | ||
|
||
namespace UnitTests | ||
{ | ||
[TestFixture] | ||
public class SslStreamFactoryTest | ||
{ | ||
const string CaCertificatePath = "CaCertificate.cer"; | ||
const string DifferentCaCertificatePath = "OtherCaCertificate.cer"; | ||
const string ServerCertificatePath = "serverCertificate.cer"; | ||
const string ClientCertificatePath = "clientCertificate.cer"; | ||
|
||
X509Certificate2 CaCertificate { get; set; } = null!; | ||
X509Certificate2 ClientCertificate { get; set; } = null!; | ||
X509Certificate2 ServerCertificate { get; set; } = null!; | ||
|
||
[OneTimeSetUp] | ||
public void BuildCerts() | ||
{ | ||
var caCertificate = CreateCACertificate(); | ||
File.WriteAllBytes(CaCertificatePath, caCertificate.Export(X509ContentType.Cert)); | ||
CaCertificate = caCertificate; | ||
|
||
var serverCertificate = CreateServerCertificate(caCertificate); | ||
File.WriteAllBytes(ServerCertificatePath, serverCertificate.Export(X509ContentType.Cert)); | ||
ServerCertificate = serverCertificate; | ||
|
||
var clientCertificate = CreateClientCertificate(caCertificate); | ||
File.WriteAllBytes(ClientCertificatePath, clientCertificate.Export(X509ContentType.Cert)); | ||
ClientCertificate = clientCertificate; | ||
|
||
var differentCaCertificate = CreateCACertificate(); | ||
File.WriteAllBytes(DifferentCaCertificatePath, differentCaCertificate.Export(X509ContentType.Cert)); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void ClearCerts() | ||
{ | ||
File.Delete(CaCertificatePath); | ||
File.Delete(DifferentCaCertificatePath); | ||
File.Delete(ServerCertificatePath); | ||
File.Delete(ClientCertificatePath); | ||
} | ||
|
||
static X509Certificate2 CreateCACertificate() | ||
{ | ||
var rsa = RSA.Create(); | ||
var request = new CertificateRequest("CN=127.0.0.1 Test CA", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true)); | ||
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign, true)); | ||
request.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(request.PublicKey, false)); | ||
|
||
X509Certificate2 certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(5)); | ||
return certificate; | ||
} | ||
|
||
static X509Certificate2 CreateServerCertificate(X509Certificate2 caCertificate) | ||
{ | ||
var rsa = RSA.Create(); | ||
var request = new CertificateRequest($"CN=127.0.0.1 Server Test", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
|
||
var enhancedKeyUsages = new OidCollection | ||
{ | ||
new Oid("1.3.6.1.5.5.7.3.1"), // OID for Server Authentication | ||
}; | ||
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(enhancedKeyUsages, false)); | ||
|
||
X509Certificate2 certificate = request.Create(caCertificate, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(1), [0, 0, 0, 0, 0, 0, 0, 1]); | ||
return certificate; | ||
} | ||
|
||
static X509Certificate2 CreateClientCertificate(X509Certificate2 caCertificate) | ||
{ | ||
var rsa = RSA.Create(); | ||
var request = new CertificateRequest($"CN=127.0.0.1 Client Test", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
|
||
var enhancedKeyUsages = new OidCollection | ||
{ | ||
new Oid("1.3.6.1.5.5.7.3.2") // OID for Client Authentication | ||
}; | ||
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(enhancedKeyUsages, false)); | ||
|
||
X509Certificate2 certificate = request.Create(caCertificate, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(1), [0, 0, 0, 0, 0, 0, 0, 2]); | ||
return certificate; | ||
} | ||
|
||
[Test] | ||
public void VerifyServerCertificateChainEnhancedUsage() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ServerCertificatePath); | ||
dict.SetString(SessionSettings.SSL_CA_CERTIFICATE, CaCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.None, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.None, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.True); | ||
Assert.That(resultClient, Is.False); | ||
} | ||
|
||
[Test] | ||
public void VerifyServerCertificateChainFailsWithoutCA() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ServerCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.False); | ||
Assert.That(resultClient, Is.False); | ||
} | ||
|
||
[Test] | ||
public void VerifyServerCertificateChainFailsWithWrongCA() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ServerCertificatePath); | ||
dict.SetString(SessionSettings.SSL_CA_CERTIFICATE, DifferentCaCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ServerCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.False); | ||
Assert.That(resultClient, Is.False); | ||
} | ||
|
||
[Test] | ||
public void VerifyClientCertificateChainEnhancedUsage() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ClientCertificatePath); | ||
dict.SetString(SessionSettings.SSL_CA_CERTIFICATE, CaCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.None, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.None, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.False); | ||
Assert.That(resultClient, Is.True); | ||
} | ||
|
||
[Test] | ||
public void VerifyClientCertificateChainFailsWithoutCA() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ClientCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.False); | ||
Assert.That(resultClient, Is.False); | ||
} | ||
|
||
[Test] | ||
public void VerifyClientCertificateChainFailsWithWrongCA() | ||
{ | ||
SettingsDictionary dict = new(); | ||
dict.SetBool(SessionSettings.SSL_ENABLE, true); | ||
dict.SetBool(SessionSettings.SSL_VALIDATE_CERTIFICATES, true); | ||
dict.SetString(SessionSettings.SSL_CERTIFICATE, ClientCertificatePath); | ||
dict.SetString(SessionSettings.SSL_CA_CERTIFICATE, DifferentCaCertificatePath); | ||
|
||
var settings = new SocketSettings(); | ||
settings.Configure(dict); | ||
|
||
var logger = new NonSessionLog(new ScreenLogFactory(true, true, true)); | ||
var factory = new SslStreamFactory(settings, logger); | ||
|
||
var resultServer = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.SERVER_AUTHENTICATION_OID); | ||
var resultClient = factory.VerifyRemoteCertificate(ClientCertificate, SslPolicyErrors.RemoteCertificateChainErrors, SslStreamFactory.CLIENT_AUTHENTICATION_OID); | ||
|
||
Assert.That(resultServer, Is.False); | ||
Assert.That(resultClient, Is.False); | ||
} | ||
} | ||
} |
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.
This is the root of the problem. SslStream provides public certificate validation via SslPolicyErrors before we get here. This
return false
causes this function to fail for all public certificates. Instead, we should validate SslPolicyErrors for both public and private certs.Why this behavior changed from older versions is what I haven't figured out
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.
This logic ended up being changed by #827. It appears unintentional.
I'm surprised more people haven't run into this because with the change in logic you'll get
AuthenticationException
"The remote certificate was rejected by the providedRemoteCertificateValidationCallback
" throw.