-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5d5b3a7
commit 0a1b61a
Showing
10 changed files
with
158 additions
and
158 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
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
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
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 |
---|---|---|
@@ -1,131 +1,131 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
using Titanium.Web.Proxy.Certificates.Cache; | ||
using Titanium.Web.Proxy.Helpers; | ||
|
||
namespace Microsoft.DevProxy; | ||
|
||
// based on https://github.com/justcoding121/titanium-web-proxy/blob/9e71608d204e5b67085656dd6b355813929801e4/src/Titanium.Web.Proxy/Certificates/Cache/DefaultCertificateDiskCache.cs | ||
public sealed class CertificateDiskCache : ICertificateCache | ||
{ | ||
private const string DefaultCertificateDirectoryName = "crts"; | ||
private const string DefaultCertificateFileExtension = ".pfx"; | ||
private const string DefaultRootCertificateFileName = "rootCert" + DefaultCertificateFileExtension; | ||
private const string ProxyConfigurationFolderName = "dev-proxy"; | ||
|
||
private string? rootCertificatePath; | ||
|
||
public Task<X509Certificate2?> LoadRootCertificateAsync(string pathOrName, string password, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) | ||
{ | ||
var path = GetRootCertificatePath(pathOrName, false); | ||
return Task.FromResult(LoadCertificate(path, password, storageFlags)); | ||
} | ||
|
||
public Task SaveRootCertificateAsync(string pathOrName, string password, X509Certificate2 certificate, CancellationToken cancellationToken) | ||
{ | ||
var path = GetRootCertificatePath(pathOrName, true); | ||
var exported = certificate.Export(X509ContentType.Pkcs12, password); | ||
File.WriteAllBytes(path, exported); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<X509Certificate2?> LoadCertificateAsync(string subjectName, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) | ||
{ | ||
var filePath = Path.Combine(GetCertificatePath(false), subjectName + DefaultCertificateFileExtension); | ||
return Task.FromResult(LoadCertificate(filePath, string.Empty, storageFlags)); | ||
} | ||
|
||
public Task SaveCertificateAsync(string subjectName, X509Certificate2 certificate, CancellationToken cancellationToken) | ||
{ | ||
var filePath = Path.Combine(GetCertificatePath(true), subjectName + DefaultCertificateFileExtension); | ||
var exported = certificate.Export(X509ContentType.Pkcs12); | ||
File.WriteAllBytes(filePath, exported); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
try | ||
{ | ||
var path = GetCertificatePath(false); | ||
if (Directory.Exists(path)) Directory.Delete(path, true); | ||
} | ||
catch (Exception) | ||
{ | ||
// do nothing | ||
} | ||
} | ||
|
||
private static X509Certificate2? LoadCertificate(string path, string password, X509KeyStorageFlags storageFlags) | ||
{ | ||
byte[] exported; | ||
|
||
if (!File.Exists(path)) return null; | ||
|
||
try | ||
{ | ||
exported = File.ReadAllBytes(path); | ||
} | ||
catch (IOException) | ||
{ | ||
// file or directory not found | ||
return null; | ||
} | ||
|
||
return new X509Certificate2(exported, password, storageFlags); | ||
} | ||
|
||
private string GetRootCertificatePath(string pathOrName, bool create) | ||
{ | ||
if (Path.IsPathRooted(pathOrName)) return pathOrName; | ||
|
||
return Path.Combine(GetRootCertificateDirectory(create), | ||
string.IsNullOrEmpty(pathOrName) ? DefaultRootCertificateFileName : pathOrName); | ||
} | ||
|
||
private string GetCertificatePath(bool create) | ||
{ | ||
var path = GetRootCertificateDirectory(create); | ||
|
||
var certPath = Path.Combine(path, DefaultCertificateDirectoryName); | ||
if (create && !Directory.Exists(certPath)) Directory.CreateDirectory(certPath); | ||
|
||
return certPath; | ||
} | ||
|
||
private string GetRootCertificateDirectory(bool create) | ||
{ | ||
if (rootCertificatePath == null) | ||
{ | ||
if (RunTime.IsUwpOnWindows) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else if (RunTime.IsLinux) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else if (RunTime.IsMac) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else | ||
{ | ||
var assemblyLocation = AppContext.BaseDirectory; | ||
|
||
var path = Path.GetDirectoryName(assemblyLocation); | ||
|
||
rootCertificatePath = path ?? throw new NullReferenceException(); | ||
} | ||
} | ||
|
||
if (create && !Directory.Exists(rootCertificatePath)) | ||
{ | ||
Directory.CreateDirectory(rootCertificatePath); | ||
} | ||
|
||
return rootCertificatePath; | ||
} | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
using Titanium.Web.Proxy.Certificates.Cache; | ||
using Titanium.Web.Proxy.Helpers; | ||
|
||
namespace Microsoft.DevProxy; | ||
|
||
// based on https://github.com/justcoding121/titanium-web-proxy/blob/9e71608d204e5b67085656dd6b355813929801e4/src/Titanium.Web.Proxy/Certificates/Cache/DefaultCertificateDiskCache.cs | ||
public sealed class CertificateDiskCache : ICertificateCache | ||
{ | ||
private const string DefaultCertificateDirectoryName = "crts"; | ||
private const string DefaultCertificateFileExtension = ".pfx"; | ||
private const string DefaultRootCertificateFileName = "rootCert" + DefaultCertificateFileExtension; | ||
private const string ProxyConfigurationFolderName = "dev-proxy"; | ||
|
||
private string? rootCertificatePath; | ||
|
||
public Task<X509Certificate2?> LoadRootCertificateAsync(string pathOrName, string password, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) | ||
{ | ||
var path = GetRootCertificatePath(pathOrName, false); | ||
return Task.FromResult(LoadCertificate(path, password, storageFlags)); | ||
} | ||
|
||
public Task SaveRootCertificateAsync(string pathOrName, string password, X509Certificate2 certificate, CancellationToken cancellationToken) | ||
{ | ||
var path = GetRootCertificatePath(pathOrName, true); | ||
var exported = certificate.Export(X509ContentType.Pkcs12, password); | ||
File.WriteAllBytes(path, exported); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<X509Certificate2?> LoadCertificateAsync(string subjectName, X509KeyStorageFlags storageFlags, CancellationToken cancellationToken) | ||
{ | ||
var filePath = Path.Combine(GetCertificatePath(false), subjectName + DefaultCertificateFileExtension); | ||
return Task.FromResult(LoadCertificate(filePath, string.Empty, storageFlags)); | ||
} | ||
|
||
public Task SaveCertificateAsync(string subjectName, X509Certificate2 certificate, CancellationToken cancellationToken) | ||
{ | ||
var filePath = Path.Combine(GetCertificatePath(true), subjectName + DefaultCertificateFileExtension); | ||
var exported = certificate.Export(X509ContentType.Pkcs12); | ||
File.WriteAllBytes(filePath, exported); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
try | ||
{ | ||
var path = GetCertificatePath(false); | ||
if (Directory.Exists(path)) Directory.Delete(path, true); | ||
} | ||
catch (Exception) | ||
{ | ||
// do nothing | ||
} | ||
} | ||
|
||
private static X509Certificate2? LoadCertificate(string path, string password, X509KeyStorageFlags storageFlags) | ||
{ | ||
byte[] exported; | ||
|
||
if (!File.Exists(path)) return null; | ||
|
||
try | ||
{ | ||
exported = File.ReadAllBytes(path); | ||
} | ||
catch (IOException) | ||
{ | ||
// file or directory not found | ||
return null; | ||
} | ||
|
||
return X509CertificateLoader.LoadPkcs12(exported, password, storageFlags); | ||
} | ||
|
||
private string GetRootCertificatePath(string pathOrName, bool create) | ||
{ | ||
if (Path.IsPathRooted(pathOrName)) return pathOrName; | ||
|
||
return Path.Combine(GetRootCertificateDirectory(create), | ||
string.IsNullOrEmpty(pathOrName) ? DefaultRootCertificateFileName : pathOrName); | ||
} | ||
|
||
private string GetCertificatePath(bool create) | ||
{ | ||
var path = GetRootCertificateDirectory(create); | ||
|
||
var certPath = Path.Combine(path, DefaultCertificateDirectoryName); | ||
if (create && !Directory.Exists(certPath)) Directory.CreateDirectory(certPath); | ||
|
||
return certPath; | ||
} | ||
|
||
private string GetRootCertificateDirectory(bool create) | ||
{ | ||
if (rootCertificatePath == null) | ||
{ | ||
if (RunTime.IsUwpOnWindows) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else if (RunTime.IsLinux) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else if (RunTime.IsMac) | ||
{ | ||
rootCertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ProxyConfigurationFolderName); | ||
} | ||
else | ||
{ | ||
var assemblyLocation = AppContext.BaseDirectory; | ||
|
||
var path = Path.GetDirectoryName(assemblyLocation); | ||
|
||
rootCertificatePath = path ?? throw new NullReferenceException(); | ||
} | ||
} | ||
|
||
if (create && !Directory.Exists(rootCertificatePath)) | ||
{ | ||
Directory.CreateDirectory(rootCertificatePath); | ||
} | ||
|
||
return rootCertificatePath; | ||
} | ||
} |
Oops, something went wrong.