-
Notifications
You must be signed in to change notification settings - Fork 162
feat(csharp/src/Drivers/Apache): Load certificate bundle from pem file #3303
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| using System.Net.Http; | ||
| using System.Net.Security; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2 | ||
| { | ||
|
|
@@ -138,6 +139,30 @@ static internal TlsProperties GetStandardTlsOptions(IReadOnlyDictionary<string, | |
| return tlsProperties; | ||
| } | ||
|
|
||
| public static List<X509Certificate2> LoadPemCertificates(string pemPath) | ||
|
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. How big is this file typically, and how long does it take to read and parse? Does it make sense to keep a cache of these by file path and size/last update time so that it doesn't need to be reloaded regularly? The standard ~cacert.pem is about 220kb. |
||
| { | ||
| List<X509Certificate2> certs = new(); | ||
| string pemContent = File.ReadAllText(pemPath); | ||
|
|
||
| MatchCollection matches = Regex.Matches( | ||
| pemContent, | ||
| "-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----", | ||
| RegexOptions.Singleline); | ||
|
|
||
| foreach (Match match in matches) | ||
| { | ||
| string base64 = match.Groups[1].Value | ||
| .Replace("\r", "") | ||
| .Replace("\n", "") | ||
| .Trim(); | ||
|
|
||
| byte[] rawData = Convert.FromBase64String(base64); | ||
| certs.Add(new X509Certificate2(rawData)); | ||
| } | ||
|
|
||
| return certs; | ||
| } | ||
|
|
||
| static internal bool ValidateCertificate(X509Certificate? cert, SslPolicyErrors policyErrors, TlsProperties tlsProperties) | ||
| { | ||
| if (policyErrors == SslPolicyErrors.None || tlsProperties.DisableServerCertificateValidation) | ||
|
|
@@ -155,14 +180,39 @@ static internal bool ValidateCertificate(X509Certificate? cert, SslPolicyErrors | |
| return !policyErrors.HasFlag(SslPolicyErrors.RemoteCertificateChainErrors) || (tlsProperties.AllowSelfSigned && IsSelfSigned(cert2)); | ||
| } | ||
|
|
||
| X509Certificate2 trustedRoot = new X509Certificate2(tlsProperties.TrustedCertificatePath); | ||
| X509Chain customChain = new(); | ||
| customChain.ChainPolicy.ExtraStore.Add(trustedRoot); | ||
| // "tell the X509Chain class that I do trust this root certs and it should check just the certs in the chain and nothing else" | ||
| customChain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; | ||
| var collection = LoadPemCertificates(tlsProperties.TrustedCertificatePath!); | ||
|
|
||
| foreach (var trustedCert in collection) | ||
| { | ||
| customChain.ChainPolicy.ExtraStore.Add(trustedCert); | ||
| } | ||
| customChain.ChainPolicy.RevocationMode = X509RevocationMode.Online; | ||
|
|
||
| bool chainValid = customChain.Build(cert2); | ||
| if (chainValid) | ||
| { | ||
| bool trustedBy = false; | ||
| foreach (X509ChainElement element in customChain.ChainElements) | ||
| { | ||
| foreach (X509Certificate2 ca in collection) | ||
|
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. I know that cert chains typically aren't that long, but this is n^2 behavior. Is it worth loading the certificates as a dictionary keyed by thumbprint in order to speed up the search? |
||
| { | ||
| if (element.Certificate.Thumbprint == ca.Thumbprint) | ||
|
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. Should this be a case-insensitive comparison or are thumbprints always normalized to a specific case? Is comparing just the thumbprint enough for good security? |
||
| { | ||
| trustedBy = true; | ||
| break; | ||
| } | ||
| } | ||
| if (trustedBy) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| chainValid = chainValid && trustedBy; | ||
| } | ||
|
|
||
| return chainValid || (tlsProperties.AllowSelfSigned && IsSelfSigned(cert2)); | ||
|
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. Consider modifying the check around line 178 to return early for a self-signed certificate even if |
||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.