ln.http/cert/CertContainer.cs

60 lines
1.8 KiB
C#

// /**
// * File: CertContainer.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.IO;
namespace ln.http.cert
{
public class CertContainer
{
public string SearchPath { get; set; }
Dictionary<string, X509Certificate> certificates = new Dictionary<string, X509Certificate>();
public CertContainer(){ }
public CertContainer(string searchPath)
{
SearchPath = searchPath;
}
public void AddCertificate(string targetHost, X509Certificate certificate) => certificates[targetHost] = certificate;
public virtual X509Certificate LookupCertificate(string targetHost)
{
String p = Path.Combine(SearchPath, String.Format("{0}.pem",targetHost));
if (File.Exists(p))
{
return X509Certificate.CreateFromCertFile(p);
}
return null;
}
public X509Certificate SelectCertificate(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers)
{
if (!certificates.ContainsKey(targetHost) && (SearchPath != null))
{
X509Certificate certificate = LookupCertificate(targetHost);
if (certificate != null)
{
certificates[targetHost] = certificate;
}
else
{
return null;
}
}
return certificates[targetHost];
}
}
}