Commit 48e9de81 authored by justcoding121's avatar justcoding121

dictionary is thread safe (no lock needed); update BC

parent 5d778739
...@@ -155,7 +155,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -155,7 +155,7 @@ namespace Titanium.Web.Proxy.Network
string path = Path.GetDirectoryName(assemblyLocation); string path = Path.GetDirectoryName(assemblyLocation);
if (null == path) if (null == path)
throw new NullReferenceException(); throw new NullReferenceException();
return path; return path;
} }
...@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy.Network
private string GetRootCertificatePath() private string GetRootCertificatePath()
{ {
string path = GetRootCertificateDirectory(); string path = GetRootCertificateDirectory();
string fileName = PfxFilePath; string fileName = PfxFilePath;
if (fileName == string.Empty) if (fileName == string.Empty)
{ {
...@@ -195,7 +195,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -195,7 +195,7 @@ namespace Titanium.Web.Proxy.Network
{ {
return null; return null;
} }
try try
{ {
return new X509Certificate2(fileName, PfxPassword, StorageFlag); return new X509Certificate2(fileName, PfxPassword, StorageFlag);
...@@ -253,7 +253,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -253,7 +253,7 @@ namespace Titanium.Web.Proxy.Network
// ignore // ignore
} }
string fileName = GetRootCertificatePath(); string fileName = GetRootCertificatePath();
File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, PfxPassword)); File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, PfxPassword));
} }
catch (Exception e) catch (Exception e)
...@@ -317,7 +317,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -317,7 +317,7 @@ namespace Titanium.Web.Proxy.Network
var info = new ProcessStartInfo var info = new ProcessStartInfo
{ {
FileName = "certutil.exe", FileName = "certutil.exe",
Arguments = "-importPFX -p \""+ PfxPassword + "\" -f \"" + fileName + "\"", Arguments = "-importPFX -p \"" + PfxPassword + "\" -f \"" + fileName + "\"",
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = "runas",
...@@ -458,64 +458,52 @@ namespace Titanium.Web.Proxy.Network ...@@ -458,64 +458,52 @@ namespace Titanium.Web.Proxy.Network
} }
X509Certificate2 certificate = null; X509Certificate2 certificate = null;
lock (string.Intern(certificateName)) try
{ {
if (certificateCache.ContainsKey(certificateName) == false) if (!isRootCertificate && SaveFakeCertificates)
{ {
try string path = GetCertPath();
string subjectName = BCCertificateMaker.CNRemoverRegex.Replace(certificateName, string.Empty);
subjectName = subjectName.Replace("*", "$x$");
subjectName = Path.Combine(path, subjectName + ".pfx");
if (!File.Exists(subjectName))
{
certificate = MakeCertificate(certificateName, isRootCertificate);
File.WriteAllBytes(subjectName, certificate.Export(X509ContentType.Pkcs12));
}
else
{ {
if (!isRootCertificate && SaveFakeCertificates) try
{ {
string path = GetCertPath(); certificate = new X509Certificate2(subjectName, string.Empty, StorageFlag);
string subjectName = BCCertificateMaker.CNRemoverRegex.Replace(certificateName, string.Empty);
subjectName = subjectName.Replace("*", "$x$");
subjectName = Path.Combine(path, subjectName + ".pfx");
if (!File.Exists(subjectName))
{
certificate = MakeCertificate(certificateName, isRootCertificate);
File.WriteAllBytes(subjectName, certificate.Export(X509ContentType.Pkcs12));
}
else
{
try
{
certificate = new X509Certificate2(subjectName, string.Empty, StorageFlag);
}
catch /* (Exception e)*/
{
certificate = MakeCertificate(certificateName, isRootCertificate);
}
}
} }
else catch /* (Exception e)*/
{ {
certificate = MakeCertificate(certificateName, isRootCertificate); certificate = MakeCertificate(certificateName, isRootCertificate);
} }
} }
catch (Exception e)
{
exceptionFunc(e);
}
if (certificate != null && !certificateCache.ContainsKey(certificateName))
{
certificateCache.Add(certificateName, new CachedCertificate
{
Certificate = certificate
});
}
} }
else else
{ {
if (certificateCache.ContainsKey(certificateName)) certificate = MakeCertificate(certificateName, isRootCertificate);
{
var cached = certificateCache[certificateName];
cached.LastAccess = DateTime.Now;
return cached.Certificate;
}
} }
} }
catch (Exception e)
{
exceptionFunc(e);
}
if (certificate != null)
{
//this is ConcurrentDictionary
//if key exists it will silently handle; no need for locking
certificateCache.Add(certificateName, new CachedCertificate
{
Certificate = certificate
});
}
return certificate; return certificate;
} }
......
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks> <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<RootNamespace>Titanium.Web.Proxy</RootNamespace> <RootNamespace>Titanium.Web.Proxy</RootNamespace>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<SignAssembly>True</SignAssembly> <SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>StrongNameKey.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>StrongNameKey.snk</AssemblyOriginatorKeyFile>
<DelaySign>False</DelaySign> <DelaySign>False</DelaySign>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<LangVersion>7.1</LangVersion> <LangVersion>7.1</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" /> <PackageReference Include="Portable.BouncyCastle" Version="1.8.1.4" />
<PackageReference Include="StreamExtended" Version="1.0.135-beta" /> <PackageReference Include="StreamExtended" Version="1.0.135-beta" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Win32.Registry"> <PackageReference Include="Microsoft.Win32.Registry">
<Version>4.4.0</Version> <Version>4.4.0</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Security.Principal.Windows"> <PackageReference Include="System.Security.Principal.Windows">
<Version>4.4.1</Version> <Version>4.4.1</Version>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<tags></tags> <tags></tags>
<dependencies> <dependencies>
<dependency id="StreamExtended" version="1.0.110-beta" /> <dependency id="StreamExtended" version="1.0.110-beta" />
<dependency id="Portable.BouncyCastle" version="1.8.1.3" /> <dependency id="Portable.BouncyCastle" version="1.8.1.4" />
</dependencies> </dependencies>
</metadata> </metadata>
<files> <files>
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="net45" /> <package id="Portable.BouncyCastle" version="1.8.1.4" targetFramework="net45" />
<package id="StreamExtended" version="1.0.110-beta" targetFramework="net45" /> <package id="StreamExtended" version="1.0.110-beta" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
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