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
7023f63b
Commit
7023f63b
authored
Apr 29, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup win certificate generator
parent
7ac94c88
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
79 deletions
+48
-79
CertificateManagerTests.cs
...s/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
+25
-25
WinCertificateMaker.cs
...nium.Web.Proxy/Network/Certificate/WinCertificateMaker.cs
+12
-32
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+11
-22
No files found.
Tests/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs
View file @
7023f63b
using
System
;
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Titanium.Web.Proxy.Network
;
...
...
@@ -12,7 +14,6 @@ namespace Titanium.Web.Proxy.UnitTests
private
static
readonly
string
[]
hostNames
=
{
"facebook.com"
,
"youtube.com"
,
"google.com"
,
"bing.com"
,
"yahoo.com"
};
private
readonly
Random
random
=
new
Random
();
[
TestMethod
]
public
async
Task
Simple_BC_Create_Certificate_Test
()
...
...
@@ -21,22 +22,21 @@ namespace Titanium.Web.Proxy.UnitTests
var
mgr
=
new
CertificateManager
(
null
,
null
,
false
,
false
,
false
,
new
Lazy
<
ExceptionHandler
>(()
=>
(
e
=>
{
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).
Value
);
mgr
.
CertificateEngine
=
CertificateEngine
.
BouncyCastle
;
Debug
.
WriteLine
(
e
.
ToString
());
Debug
.
WriteLine
(
e
.
InnerException
?.
ToString
());
})).
Value
)
{
CertificateEngine
=
CertificateEngine
.
BouncyCastle
};
mgr
.
ClearIdleCertificates
();
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
foreach
(
string
host
in
hostNames
)
tasks
.
AddRange
(
hostNames
.
Select
(
host
=>
Task
.
Run
(()
=>
{
tasks
.
Add
(
Task
.
Run
(()
=>
{
//get the connection
var
certificate
=
mgr
.
CreateCertificate
(
host
,
false
);
Assert
.
IsNotNull
(
certificate
);
}));
}
//get the connection
var
certificate
=
mgr
.
CreateCertificate
(
host
,
false
);
Assert
.
IsNotNull
(
certificate
);
})));
}
await
Task
.
WhenAll
(
tasks
.
ToArray
());
...
...
@@ -48,34 +48,34 @@ namespace Titanium.Web.Proxy.UnitTests
[
TestMethod
]
public
async
Task
Simple_Create_Win_Certificate_Test
()
{
var
tasks
=
new
List
<
Task
>();
var
mgr
=
new
CertificateManager
(
null
,
null
,
false
,
false
,
false
,
new
Lazy
<
ExceptionHandler
>(()
=>
(
e
=>
{
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).
Value
);
Debug
.
WriteLine
(
e
.
ToString
());
Debug
.
WriteLine
(
e
.
InnerException
?.
ToString
());
})).
Value
)
{
CertificateEngine
=
CertificateEngine
.
DefaultWindows
};
mgr
.
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
mgr
.
CreateRootCertificate
(
true
);
mgr
.
CreateRootCertificate
();
mgr
.
TrustRootCertificate
(
true
);
mgr
.
ClearIdleCertificates
();
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
foreach
(
string
host
in
hostNames
)
tasks
.
AddRange
(
hostNames
.
Select
(
host
=>
Task
.
Run
(()
=>
{
tasks
.
Add
(
Task
.
Run
(()
=>
{
//get the connection
var
certificate
=
mgr
.
CreateCertificate
(
host
,
false
);
Assert
.
IsNotNull
(
certificate
);
}));
}
//get the connection
var
certificate
=
mgr
.
CreateCertificate
(
host
,
false
);
Assert
.
IsNotNull
(
certificate
);
})));
}
await
Task
.
WhenAll
(
tasks
.
ToArray
());
mgr
.
RemoveTrustedRootCertificate
(
true
);
mgr
.
StopClearIdleCertificates
();
}
}
}
Titanium.Web.Proxy/Network/Certificate/WinCertificateMaker.cs
View file @
7023f63b
...
...
@@ -2,9 +2,11 @@
using
System.Reflection
;
using
System.Security.Cryptography.X509Certificates
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
Titanium.Web.Proxy.Network.Certificate
{
/// <inheritdoc />
/// <summary>
/// Certificate Maker - uses MakeCert
/// Calls COM objects using reflection
...
...
@@ -104,8 +106,8 @@ namespace Titanium.Web.Proxy.Network.Certificate
}
typeX500DN
.
InvokeMember
(
"Encode"
,
BindingFlags
.
InvokeMethod
,
null
,
x500RootCertDN
,
typeValue
);
object
sharedPrivateKey
=
null
;
object
sharedPrivateKey
=
null
;
if
(!
isRoot
)
{
sharedPrivateKey
=
this
.
sharedPrivateKey
;
...
...
@@ -276,49 +278,27 @@ namespace Titanium.Web.Proxy.Network.Certificate
bool
switchToMTAIfNeeded
,
X509Certificate2
signingCert
=
null
,
CancellationToken
cancellationToken
=
default
)
{
X509Certificate2
certificate
=
null
;
if
(
switchToMTAIfNeeded
&&
Thread
.
CurrentThread
.
GetApartmentState
()
!=
ApartmentState
.
MTA
)
{
using
(
var
manualResetEvent
=
new
ManualResetEventSlim
(
false
))
{
ThreadPool
.
QueueUserWorkItem
(
o
=>
{
try
{
certificate
=
MakeCertificateInternal
(
sSubjectCN
,
isRoot
,
false
,
signingCert
);
}
catch
(
Exception
ex
)
{
exceptionFunc
(
new
Exception
(
"Failed to create Win certificate"
,
ex
));
}
if
(!
cancellationToken
.
IsCancellationRequested
)
{
manualResetEvent
.
Set
();
}
});
manualResetEvent
.
Wait
(
TimeSpan
.
FromMinutes
(
1
),
cancellationToken
);
}
return
certificate
;
return
Task
.
Run
(()
=>
MakeCertificateInternal
(
sSubjectCN
,
isRoot
,
false
,
signingCert
),
cancellationToken
).
Result
;
}
//Subject
string
fullSubject
=
$"CN=
{
sSubjectCN
}
"
;
//Sig Algo
string
H
ashAlgo
=
"SHA256"
;
const
string
h
ashAlgo
=
"SHA256"
;
//Grace Days
int
G
raceDays
=
-
366
;
const
int
g
raceDays
=
-
366
;
//ValiDays
int
V
alidDays
=
1825
;
const
int
v
alidDays
=
1825
;
//KeyLength
int
keyLength
=
2048
;
const
int
keyLength
=
2048
;
var
graceTime
=
DateTime
.
Now
.
AddDays
(
G
raceDays
);
var
graceTime
=
DateTime
.
Now
.
AddDays
(
g
raceDays
);
var
now
=
DateTime
.
Now
;
certificate
=
MakeCertificate
(
isRoot
,
sSubjectCN
,
fullSubject
,
keyLength
,
H
ashAlgo
,
graceTime
,
now
.
AddDays
(
V
alidDays
),
isRoot
?
null
:
signingCert
);
var
certificate
=
MakeCertificate
(
isRoot
,
sSubjectCN
,
fullSubject
,
keyLength
,
h
ashAlgo
,
graceTime
,
now
.
AddDays
(
v
alidDays
),
isRoot
?
null
:
signingCert
);
return
certificate
;
}
}
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
7023f63b
...
...
@@ -79,11 +79,7 @@ namespace Titanium.Web.Proxy.Network
{
this
.
exceptionFunc
=
exceptionFunc
;
UserTrustRoot
=
userTrustRootCertificate
;
if
(
machineTrustRootCertificate
)
{
userTrustRootCertificate
=
true
;
}
UserTrustRoot
=
userTrustRootCertificate
||
machineTrustRootCertificate
;
MachineTrustRoot
=
machineTrustRootCertificate
;
TrustRootAsAdministrator
=
trustRootCertificateAsAdmin
;
...
...
@@ -338,14 +334,14 @@ namespace Titanium.Web.Proxy.Network
return
;
}
var
x509
s
tore
=
new
X509Store
(
storeName
,
storeLocation
);
var
x509
S
tore
=
new
X509Store
(
storeName
,
storeLocation
);
//TODO
//also it should do not duplicate if certificate already exists
try
{
x509
s
tore
.
Open
(
OpenFlags
.
ReadWrite
);
x509
s
tore
.
Add
(
RootCertificate
);
x509
S
tore
.
Open
(
OpenFlags
.
ReadWrite
);
x509
S
tore
.
Add
(
RootCertificate
);
}
catch
(
Exception
e
)
{
...
...
@@ -356,7 +352,7 @@ namespace Titanium.Web.Proxy.Network
}
finally
{
x509
s
tore
.
Close
();
x509
S
tore
.
Close
();
}
}
...
...
@@ -436,7 +432,7 @@ namespace Titanium.Web.Proxy.Network
if
(!
File
.
Exists
(
certificatePath
))
{
certificate
=
MakeCertificate
(
certificateName
,
isRootCertificat
e
);
certificate
=
MakeCertificate
(
certificateName
,
fals
e
);
//store as cache
Task
.
Run
(()
=>
...
...
@@ -460,7 +456,7 @@ namespace Titanium.Web.Proxy.Network
//if load failed create again
catch
{
certificate
=
MakeCertificate
(
certificateName
,
isRootCertificat
e
);
certificate
=
MakeCertificate
(
certificateName
,
fals
e
);
}
}
}
...
...
@@ -485,8 +481,7 @@ namespace Titanium.Web.Proxy.Network
internal
async
Task
<
X509Certificate2
>
CreateCertificateAsync
(
string
certificateName
)
{
//check in cache first
CachedCertificate
cached
;
if
(
certificateCache
.
TryGetValue
(
certificateName
,
out
cached
))
if
(
certificateCache
.
TryGetValue
(
certificateName
,
out
var
cached
))
{
cached
.
LastAccess
=
DateTime
.
Now
;
return
cached
.
Certificate
;
...
...
@@ -494,8 +489,7 @@ namespace Titanium.Web.Proxy.Network
//handle burst requests with same certificate name
//by checking for existing task for same certificate name
Task
<
X509Certificate2
>
task
;
if
(
pendingCertificateCreationTasks
.
TryGetValue
(
certificateName
,
out
task
))
if
(
pendingCertificateCreationTasks
.
TryGetValue
(
certificateName
,
out
var
task
))
{
return
await
task
;
}
...
...
@@ -785,12 +779,7 @@ namespace Titanium.Web.Proxy.Network
public
void
EnsureRootCertificate
(
bool
userTrustRootCertificate
,
bool
machineTrustRootCertificate
,
bool
trustRootCertificateAsAdmin
=
false
)
{
UserTrustRoot
=
userTrustRootCertificate
;
if
(
machineTrustRootCertificate
)
{
userTrustRootCertificate
=
true
;
}
UserTrustRoot
=
userTrustRootCertificate
||
machineTrustRootCertificate
;
MachineTrustRoot
=
machineTrustRootCertificate
;
TrustRootAsAdministrator
=
trustRootCertificateAsAdmin
;
...
...
@@ -909,7 +898,7 @@ namespace Titanium.Web.Proxy.Network
success
=
false
;
}
process
.
WaitForExit
();
process
?
.
WaitForExit
();
}
}
catch
...
...
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