-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implem factory and generic callback for tls mongo
- Loading branch information
Showing
2 changed files
with
137 additions
and
60 deletions.
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,126 @@ | ||
// This file is part of the ArmoniK project | ||
// | ||
// Copyright (C) ANEO, 2021-2024. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY, without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Security; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
|
||
namespace ArmoniK.Core.Utils; | ||
|
||
public static class CertificateValidator | ||
{ | ||
public static class CertificateValidatorFactory | ||
{ | ||
public static (RemoteCertificateValidationCallback, X509Certificate2) CreateCallback(string caFilePath, | ||
ILogger logger) | ||
{ | ||
var content = File.ReadAllText(caFilePath); | ||
var authority = X509Certificate2.CreateFromPem(content); | ||
logger.LogInformation("Loaded CA certificate from file {path}", | ||
caFilePath); | ||
var callback = ValidationCallback(logger, | ||
authority); | ||
return (callback, authority); | ||
} | ||
} | ||
|
||
public static RemoteCertificateValidationCallback ValidationCallback(ILogger logger, | ||
X509Certificate2 authority) | ||
{ | ||
return (sender, | ||
certificate, | ||
chain, | ||
sslPolicyErrors) => | ||
{ | ||
if (certificate == null || chain == null) | ||
{ | ||
logger.LogWarning("Certificate or certificate chain is null"); | ||
return false; | ||
} | ||
|
||
return ValidateServerCertificate(sender, | ||
certificate, | ||
chain, | ||
sslPolicyErrors, | ||
authority, | ||
logger); | ||
}; | ||
} | ||
|
||
public static bool ValidateServerCertificate(object sender, | ||
X509Certificate certificate, | ||
X509Chain certChain, | ||
SslPolicyErrors sslPolicyErrors, | ||
X509Certificate2 authority, | ||
ILogger logger) | ||
{ | ||
// If there is any error other than untrusted root or partial chain, fail the validation | ||
if ((sslPolicyErrors & ~SslPolicyErrors.RemoteCertificateChainErrors) != 0) | ||
{ | ||
logger.LogDebug("SSL validation failed with errors: {sslPolicyErrors}", | ||
sslPolicyErrors); | ||
return false; | ||
} | ||
|
||
if (certificate == null) | ||
{ | ||
logger.LogDebug("Certificate is null!"); | ||
return false; | ||
} | ||
|
||
if (certChain == null) | ||
{ | ||
logger.LogDebug("Certificate chain is null!"); | ||
return false; | ||
} | ||
|
||
// If there is any error other than untrusted root or partial chain, fail the validation | ||
if (certChain.ChainStatus.Any(status => status.Status is not X509ChainStatusFlags.UntrustedRoot and not X509ChainStatusFlags.PartialChain)) | ||
{ | ||
logger.LogDebug("SSL validation failed with chain status: {chainStatus}", | ||
certChain.ChainStatus); | ||
return false; | ||
} | ||
|
||
var cert = new X509Certificate2(certificate); | ||
certChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||
certChain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; | ||
|
||
certChain.ChainPolicy.ExtraStore.Add(authority); | ||
if (!certChain.Build(cert)) | ||
{ | ||
return false; | ||
} | ||
|
||
var isTrusted = certChain.ChainElements.Any(x => x.Certificate.Thumbprint == authority.Thumbprint); | ||
if (isTrusted) | ||
{ | ||
logger.LogInformation("SSL validation succeeded"); | ||
} | ||
else | ||
{ | ||
logger.LogInformation("SSL validation failed with errors: {sslPolicyErrors}", | ||
sslPolicyErrors); | ||
} | ||
|
||
return isTrusted; | ||
} | ||
} |