Unverified Commit 6d1a583b authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #520 from justcoding121/master

Beta
parents 26e77586 2a57f752
This diff is collapsed.
......@@ -2970,6 +2970,19 @@ references:
isSpec: "True"
fullName: Titanium.Web.Proxy.Network.CertificateManager.CertificateEngine
nameWithType: CertificateManager.CertificateEngine
- uid: Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage
name: CertificateStorage
href: api/Titanium.Web.Proxy.Network.CertificateManager.html#Titanium_Web_Proxy_Network_CertificateManager_CertificateStorage
commentId: P:Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage
fullName: Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage
nameWithType: CertificateManager.CertificateStorage
- uid: Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage*
name: CertificateStorage
href: api/Titanium.Web.Proxy.Network.CertificateManager.html#Titanium_Web_Proxy_Network_CertificateManager_CertificateStorage_
commentId: Overload:Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage
isSpec: "True"
fullName: Titanium.Web.Proxy.Network.CertificateManager.CertificateStorage
nameWithType: CertificateManager.CertificateStorage
- uid: Titanium.Web.Proxy.Network.CertificateManager.ClearRootCertificate
name: ClearRootCertificate()
href: api/Titanium.Web.Proxy.Network.CertificateManager.html#Titanium_Web_Proxy_Network_CertificateManager_ClearRootCertificate
......
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Titanium.Web.Proxy.Network
{
/// <summary>
/// An object that holds the cached certificate
/// </summary>
internal class CachedCertificate
internal sealed class CachedCertificate
{
internal CachedCertificate()
{
LastAccess = DateTime.Now;
}
internal X509Certificate2 Certificate { get; set; }
/// <summary>
/// last time this certificate was used
/// Usefull in determining its cache lifetime
/// Last time this certificate was used.
/// Useful in determining its cache lifetime.
/// </summary>
internal DateTime LastAccess { get; set; }
}
......
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
namespace Titanium.Web.Proxy.Network
{
internal sealed class DefaultCertificateDiskCache : ICertificateCache
{
private const string defaultCertificateDirectoryName = "crts";
private const string defaultCertificateFileExtension = ".pfx";
private const string defaultRootCertificateFileName = "rootCert" + defaultCertificateFileExtension;
private string rootCertificatePath;
private string certificatePath;
public X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags)
{
string filePath = getRootCertificatePath(name);
return loadCertificate(filePath, password, storageFlags);
}
public void SaveRootCertificate(string name, string password, X509Certificate2 certificate)
{
string filePath = getRootCertificatePath(name);
byte[] exported = certificate.Export(X509ContentType.Pkcs12, password);
File.WriteAllBytes(filePath, exported);
}
/// <inheritdoc />
public X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
{
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
return loadCertificate(filePath, string.Empty, storageFlags);
}
/// <inheritdoc />
public void SaveCertificate(string subjectName, X509Certificate2 certificate)
{
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
byte[] exported = certificate.Export(X509ContentType.Pkcs12);
File.WriteAllBytes(filePath, exported);
}
public void Clear()
{
try
{
Directory.Delete(getCertificatePath(), true);
}
catch (DirectoryNotFoundException)
{
// do nothing
}
certificatePath = null;
}
private X509Certificate2 loadCertificate(string filePath, string password, X509KeyStorageFlags storageFlags)
{
byte[] exported;
try
{
exported = File.ReadAllBytes(filePath);
}
catch (IOException)
{
// file or directory not found
return null;
}
return new X509Certificate2(exported, password, storageFlags);
}
private string getRootCertificatePath(string filePath)
{
if (Path.IsPathRooted(filePath))
{
return filePath;
}
return Path.Combine(getRootCertificateDirectory(),
string.IsNullOrEmpty(filePath) ? defaultRootCertificateFileName : filePath);
}
private string getCertificatePath()
{
if (certificatePath == null)
{
string path = getRootCertificateDirectory();
string certPath = Path.Combine(path, defaultCertificateDirectoryName);
if (!Directory.Exists(certPath))
{
Directory.CreateDirectory(certPath);
}
certificatePath = certPath;
}
return certificatePath;
}
private string getRootCertificateDirectory()
{
if (rootCertificatePath == null)
{
string assemblyLocation = GetType().Assembly.Location;
// dynamically loaded assemblies returns string.Empty location
if (assemblyLocation == string.Empty)
{
assemblyLocation = Assembly.GetEntryAssembly().Location;
}
string path = Path.GetDirectoryName(assemblyLocation);
rootCertificatePath = path ?? throw new NullReferenceException();
}
return rootCertificatePath;
}
}
}
using System.Security.Cryptography.X509Certificates;
namespace Titanium.Web.Proxy.Network
{
public interface ICertificateCache
{
/// <summary>
/// Loads the root certificate from the storage.
/// </summary>
X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags);
/// <summary>
/// Saves the root certificate to the storage.
/// </summary>
void SaveRootCertificate(string name, string password, X509Certificate2 certificate);
/// <summary>
/// Loads certificate from the storage. Returns true if certificate does not exist.
/// </summary>
X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags);
/// <summary>
/// Stores certificate into the storage.
/// </summary>
void SaveCertificate(string subjectName, X509Certificate2 certificate);
/// <summary>
/// Clears the storage.
/// </summary>
void Clear();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment