Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
5d5b32e9
Commit
5d5b32e9
authored
Mar 12, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
experiment test on build server; update certificate trust logic
parent
fabfe979
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
45 deletions
+66
-45
CertificateManagerTests.cs
...s/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
+2
-3
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+64
-42
No files found.
Tests/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
View file @
5d5b32e9
...
@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy.UnitTests
...
@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy.UnitTests
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
//cannot run this test in build server since trusting the certificate won't happen successfully
//cannot run this test in build server since trusting the certificate won't happen successfully
//
[TestMethod]
[
TestMethod
]
public
async
Task
Simple_Create_Win_Certificate_Test
()
public
async
Task
Simple_Create_Win_Certificate_Test
()
{
{
var
tasks
=
new
List
<
Task
>();
var
tasks
=
new
List
<
Task
>();
...
@@ -57,7 +57,6 @@ namespace Titanium.Web.Proxy.UnitTests
...
@@ -57,7 +57,6 @@ namespace Titanium.Web.Proxy.UnitTests
mgr
.
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
mgr
.
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
mgr
.
CreateRootCertificate
(
true
);
mgr
.
CreateRootCertificate
(
true
);
mgr
.
TrustRootCertificate
();
mgr
.
TrustRootCertificate
();
//mgr.TrustRootCertificateAsAdmin();
mgr
.
ClearIdleCertificates
();
mgr
.
ClearIdleCertificates
();
for
(
int
i
=
0
;
i
<
5
;
i
++)
for
(
int
i
=
0
;
i
<
5
;
i
++)
...
@@ -72,7 +71,7 @@ namespace Titanium.Web.Proxy.UnitTests
...
@@ -72,7 +71,7 @@ namespace Titanium.Web.Proxy.UnitTests
}
}
await
Task
.
WhenAll
(
tasks
.
ToArray
());
await
Task
.
WhenAll
(
tasks
.
ToArray
());
mgr
.
RemoveTrustedRootCertificate
();
mgr
.
StopClearIdleCertificates
();
mgr
.
StopClearIdleCertificates
();
}
}
}
}
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
5d5b32e9
...
@@ -188,7 +188,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -188,7 +188,7 @@ namespace Titanium.Web.Proxy.Network
internal
CertificateManager
(
Action
<
Exception
>
exceptionFunc
)
internal
CertificateManager
(
Action
<
Exception
>
exceptionFunc
)
{
{
this
.
exceptionFunc
=
exceptionFunc
;
this
.
exceptionFunc
=
exceptionFunc
;
if
(
RunTime
.
IsWindows
)
if
(
RunTime
.
IsWindows
)
{
{
//this is faster in Windows based on tests (see unit test project CertificateManagerTests.cs)
//this is faster in Windows based on tests (see unit test project CertificateManagerTests.cs)
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
...
@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary>
/// </summary>
/// <param name="storeLocation"></param>
/// <param name="storeLocation"></param>
/// <returns></returns>
/// <returns></returns>
private
void
TrustRootCertificate
(
StoreLocation
storeLocation
)
private
void
TrustRootCertificate
(
Store
Name
storeName
,
Store
Location
storeLocation
)
{
{
if
(
RootCertificate
==
null
)
if
(
RootCertificate
==
null
)
{
{
...
@@ -293,29 +293,25 @@ namespace Titanium.Web.Proxy.Network
...
@@ -293,29 +293,25 @@ namespace Titanium.Web.Proxy.Network
return
;
return
;
}
}
var
x509RootStore
=
new
X509Store
(
StoreName
.
Root
,
storeLocation
);
var
x509store
=
new
X509Store
(
storeName
,
storeLocation
);
var
x509PersonalStore
=
new
X509Store
(
StoreName
.
My
,
storeLocation
);
//TODO
//TODO
//also it should do not duplicate if certificate already exists
//also it should do not duplicate if certificate already exists
try
try
{
{
x509
RootS
tore
.
Open
(
OpenFlags
.
ReadWrite
);
x509
s
tore
.
Open
(
OpenFlags
.
ReadWrite
);
x509
PersonalStore
.
Open
(
OpenFlags
.
ReadWri
te
);
x509
store
.
Add
(
RootCertifica
te
);
x509RootStore
.
Add
(
RootCertificate
);
x509PersonalStore
.
Add
(
RootCertificate
);
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
exceptionFunc
(
exceptionFunc
(
new
Exception
(
"Failed to make system trust root certificate "
new
Exception
(
"Failed to make system trust root certificate "
+
$" for
{
storeLocation
}
store location. You may need admin rights."
,
e
));
+
$" for
{
store
Name
}
\\
{
store
Location
}
store location. You may need admin rights."
,
e
));
}
}
finally
finally
{
{
x509RootStore
.
Close
();
x509store
.
Close
();
x509PersonalStore
.
Close
();
}
}
}
}
...
@@ -324,7 +320,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -324,7 +320,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary>
/// </summary>
/// <param name="storeLocation"></param>
/// <param name="storeLocation"></param>
/// <returns></returns>
/// <returns></returns>
private
void
RemoveTrustedRootCertificate
(
StoreLocation
storeLocation
)
private
void
RemoveTrustedRootCertificate
(
Store
Name
storeName
,
Store
Location
storeLocation
)
{
{
if
(
RootCertificate
==
null
)
if
(
RootCertificate
==
null
)
{
{
...
@@ -335,16 +331,13 @@ namespace Titanium.Web.Proxy.Network
...
@@ -335,16 +331,13 @@ namespace Titanium.Web.Proxy.Network
return
;
return
;
}
}
var
x509RootStore
=
new
X509Store
(
StoreName
.
Root
,
storeLocation
);
var
x509Store
=
new
X509Store
(
storeName
,
storeLocation
);
var
x509PersonalStore
=
new
X509Store
(
StoreName
.
My
,
storeLocation
);
try
try
{
{
x509RootStore
.
Open
(
OpenFlags
.
ReadWrite
);
x509Store
.
Open
(
OpenFlags
.
ReadWrite
);
x509PersonalStore
.
Open
(
OpenFlags
.
ReadWrite
);
x509RootStore
.
Remove
(
RootCertificate
);
x509Store
.
Remove
(
RootCertificate
);
x509PersonalStore
.
Remove
(
RootCertificate
);
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
...
@@ -354,8 +347,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -354,8 +347,7 @@ namespace Titanium.Web.Proxy.Network
}
}
finally
finally
{
{
x509RootStore
.
Close
();
x509Store
.
Close
();
x509PersonalStore
.
Close
();
}
}
}
}
...
@@ -632,10 +624,13 @@ namespace Titanium.Web.Proxy.Network
...
@@ -632,10 +624,13 @@ namespace Titanium.Web.Proxy.Network
public
void
TrustRootCertificate
()
public
void
TrustRootCertificate
()
{
{
//current user
//current user
TrustRootCertificate
(
StoreLocation
.
CurrentUser
);
TrustRootCertificate
(
Store
Name
.
My
,
Store
Location
.
CurrentUser
);
//current system
//current system
TrustRootCertificate
(
StoreLocation
.
LocalMachine
);
TrustRootCertificate
(
StoreName
.
My
,
StoreLocation
.
LocalMachine
);
//this adds to both currentUser\Root & currentMachine\Root
TrustRootCertificate
(
StoreName
.
Root
,
StoreLocation
.
LocalMachine
);
}
}
/// <summary>
/// <summary>
...
@@ -650,20 +645,22 @@ namespace Titanium.Web.Proxy.Network
...
@@ -650,20 +645,22 @@ namespace Titanium.Web.Proxy.Network
return
false
;
return
false
;
}
}
string
f
ileName
=
Path
.
GetTempFileName
();
string
pfxF
ileName
=
Path
.
GetTempFileName
();
File
.
WriteAllBytes
(
f
ileName
,
RootCertificate
.
Export
(
X509ContentType
.
Pkcs12
,
PfxPassword
));
File
.
WriteAllBytes
(
pfxF
ileName
,
RootCertificate
.
Export
(
X509ContentType
.
Pkcs12
,
PfxPassword
));
//TODO
//currentUser\Personal
//need to figure out why this command do not add
TrustRootCertificate
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
//certificate to currentuser\personal store
var
info
=
new
ProcessStartInfo
//currentUser\Root, currentMachine\Personal & currentMachine\Root
var
info
=
new
ProcessStartInfo
()
{
{
FileName
=
"certutil.exe"
,
FileName
=
"certutil.exe"
,
Arguments
=
"-importPFX -p \""
+
PfxPassword
+
"\" -f \""
+
f
ileName
+
"\""
,
Arguments
=
"-importPFX -p \""
+
PfxPassword
+
"\" -f \""
+
pfxF
ileName
+
"\""
,
CreateNoWindow
=
true
,
CreateNoWindow
=
true
,
UseShellExecute
=
true
,
UseShellExecute
=
true
,
Verb
=
"runas"
,
Verb
=
"runas"
,
ErrorDialog
=
false
,
ErrorDialog
=
false
,
WindowStyle
=
ProcessWindowStyle
.
Hidden
};
};
try
try
...
@@ -675,8 +672,8 @@ namespace Titanium.Web.Proxy.Network
...
@@ -675,8 +672,8 @@ namespace Titanium.Web.Proxy.Network
}
}
process
.
WaitForExit
();
process
.
WaitForExit
();
File
.
Delete
(
pfxFileName
);
File
.
Delete
(
fileName
);
}
}
catch
catch
{
{
...
@@ -692,10 +689,13 @@ namespace Titanium.Web.Proxy.Network
...
@@ -692,10 +689,13 @@ namespace Titanium.Web.Proxy.Network
public
void
RemoveTrustedRootCertificate
()
public
void
RemoveTrustedRootCertificate
()
{
{
//current user
//current user
RemoveTrustedRootCertificate
(
StoreLocation
.
CurrentUser
);
RemoveTrustedRootCertificate
(
Store
Name
.
My
,
Store
Location
.
CurrentUser
);
//current system
//current system
RemoveTrustedRootCertificate
(
StoreLocation
.
LocalMachine
);
RemoveTrustedRootCertificate
(
StoreName
.
My
,
StoreLocation
.
LocalMachine
);
//this removes from both currentUser\Root & currentMachine\Root
RemoveTrustedRootCertificate
(
StoreName
.
Root
,
StoreLocation
.
LocalMachine
);
}
}
/// <summary>
/// <summary>
...
@@ -710,32 +710,54 @@ namespace Titanium.Web.Proxy.Network
...
@@ -710,32 +710,54 @@ namespace Titanium.Web.Proxy.Network
return
false
;
return
false
;
}
}
var
info
=
new
ProcessStartInfo
//currentUser\Personal
RemoveTrustedRootCertificate
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
var
infos
=
new
List
<
ProcessStartInfo
>()
{
{
//currentMachine\Personal
new
ProcessStartInfo
(){
FileName
=
"certutil.exe"
,
Arguments
=
"-delstore My \""
+
RootCertificateName
+
"\""
,
CreateNoWindow
=
true
,
UseShellExecute
=
true
,
Verb
=
"runas"
,
ErrorDialog
=
false
,
WindowStyle
=
ProcessWindowStyle
.
Hidden
},
//currentUser\Personal & currentMachine\Personal
new
ProcessStartInfo
(){
FileName
=
"certutil.exe"
,
FileName
=
"certutil.exe"
,
Arguments
=
"-delstore Root \""
+
RootCertificateName
+
"\""
,
Arguments
=
"-delstore Root \""
+
RootCertificateName
+
"\""
,
CreateNoWindow
=
true
,
CreateNoWindow
=
true
,
UseShellExecute
=
true
,
UseShellExecute
=
true
,
Verb
=
"runas"
,
Verb
=
"runas"
,
ErrorDialog
=
false
,
ErrorDialog
=
false
,
WindowStyle
=
ProcessWindowStyle
.
Hidden
}
};
};
var
success
=
true
;
try
try
{
{
var
process
=
Process
.
Start
(
info
);
foreach
(
var
info
in
infos
)
if
(
process
==
null
)
{
{
return
false
;
var
process
=
Process
.
Start
(
info
);
}
process
.
WaitForExit
();
if
(
process
==
null
)
{
success
=
false
;
}
process
.
WaitForExit
();
}
}
}
catch
catch
{
{
return
false
;
success
=
false
;
}
}
return
true
;
return
success
;
}
}
/// <summary>
/// <summary>
...
@@ -754,7 +776,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -754,7 +776,7 @@ namespace Titanium.Web.Proxy.Network
return
FindRootCertificate
(
StoreLocation
.
LocalMachine
);
return
FindRootCertificate
(
StoreLocation
.
LocalMachine
);
}
}
/// <summary>
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment