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
4047a006
Commit
4047a006
authored
May 15, 2017
by
Jehonathan Thomas
Committed by
GitHub
May 15, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #224 from honfika/develop
RootCertificate creation improvements
parents
b6d18a61
d6a2f049
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
30 deletions
+94
-30
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+9
-0
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+62
-7
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+21
-21
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+2
-2
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
4047a006
...
...
@@ -17,6 +17,12 @@ namespace Titanium.Web.Proxy.Examples.Basic
public
ProxyTestController
()
{
proxyServer
=
new
ProxyServer
();
//generate root certificate without storing it in file system
//proxyServer.CertificateEngine = Network.CertificateEngine.BouncyCastle;
//proxyServer.CertificateManager.CreateTrustedRootCertificate(false);
//proxyServer.CertificateManager.TrustRootCertificate();
proxyServer
.
ExceptionFunc
=
exception
=>
Console
.
WriteLine
(
exception
.
Message
);
proxyServer
.
TrustRootCertificate
=
true
;
...
...
@@ -92,6 +98,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
proxyServer
.
ClientCertificateSelectionCallback
-=
OnCertificateSelection
;
proxyServer
.
Stop
();
//remove the generated certificates
//proxyServer.CertificateManager.RemoveTrustedRootCertificates();
}
//intecept & cancel redirect or update requests
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
4047a006
...
...
@@ -29,9 +29,9 @@ namespace Titanium.Web.Proxy.Network
/// <summary>
/// A class to manage SSL certificates used by this proxy server
/// </summary>
internal
class
CertificateManager
:
IDisposable
public
class
CertificateManager
:
IDisposable
{
public
CertificateEngine
Engine
internal
CertificateEngine
Engine
{
get
{
return
engine
;
}
set
...
...
@@ -164,10 +164,13 @@ namespace Titanium.Web.Proxy.Network
/// <summary>
/// Attempts to create a RootCertificate
/// </summary>
/// <returns>true if succeeded, else false</returns>
internal
bool
CreateTrustedRootCertificate
()
/// <param name="persistToFile">if set to <c>true</c> try to load/save the certificate from rootCert.pfx.</param>
/// <returns>
/// true if succeeded, else false
/// </returns>
public
bool
CreateTrustedRootCertificate
(
bool
persistToFile
=
true
)
{
if
(
RootCertificate
==
null
)
if
(
persistToFile
&&
RootCertificate
==
null
)
{
RootCertificate
=
LoadRootCertificate
();
}
...
...
@@ -186,7 +189,7 @@ namespace Titanium.Web.Proxy.Network
exceptionFunc
(
e
);
}
if
(
RootCertificate
!=
null
)
if
(
persistToFile
&&
RootCertificate
!=
null
)
{
try
{
...
...
@@ -205,7 +208,7 @@ namespace Titanium.Web.Proxy.Network
/// <summary>
/// Trusts the root certificate.
/// </summary>
internal
void
TrustRootCertificate
()
public
void
TrustRootCertificate
()
{
//current user
TrustRootCertificate
(
StoreLocation
.
CurrentUser
);
...
...
@@ -214,6 +217,18 @@ namespace Titanium.Web.Proxy.Network
TrustRootCertificate
(
StoreLocation
.
LocalMachine
);
}
/// <summary>
/// Removes the trusted certificates.
/// </summary>
public
void
RemoveTrustedRootCertificates
()
{
//current user
RemoveTrustedRootCertificates
(
StoreLocation
.
CurrentUser
);
//current system
RemoveTrustedRootCertificates
(
StoreLocation
.
LocalMachine
);
}
/// <summary>
/// Create an SSL certificate
/// </summary>
...
...
@@ -336,6 +351,46 @@ namespace Titanium.Web.Proxy.Network
}
}
/// <summary>
/// Remove the Root Certificate trust
/// </summary>
/// <param name="storeLocation"></param>
/// <returns></returns>
internal
void
RemoveTrustedRootCertificates
(
StoreLocation
storeLocation
)
{
if
(
RootCertificate
==
null
)
{
exceptionFunc
(
new
Exception
(
"Could not set root certificate"
+
" as system proxy since it is null or empty."
));
return
;
}
X509Store
x509RootStore
=
new
X509Store
(
StoreName
.
Root
,
storeLocation
);
var
x509PersonalStore
=
new
X509Store
(
StoreName
.
My
,
storeLocation
);
try
{
x509RootStore
.
Open
(
OpenFlags
.
ReadWrite
);
x509PersonalStore
.
Open
(
OpenFlags
.
ReadWrite
);
x509RootStore
.
Remove
(
RootCertificate
);
x509PersonalStore
.
Remove
(
RootCertificate
);
}
catch
(
Exception
e
)
{
exceptionFunc
(
new
Exception
(
"Failed to make system trust root certificate "
+
$" for
{
storeLocation
}
store location. You may need admin rights."
,
e
));
}
finally
{
x509RootStore
.
Close
();
x509PersonalStore
.
Close
();
}
}
public
void
Dispose
()
{
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
4047a006
...
...
@@ -24,11 +24,6 @@ namespace Titanium.Web.Proxy
/// </summary>
private
bool
proxyRunning
{
get
;
set
;
}
/// <summary>
/// Manages certificates used by this proxy
/// </summary>
private
CertificateManager
certificateManager
{
get
;
set
;
}
/// <summary>
/// An default exception log func
/// </summary>
...
...
@@ -64,13 +59,18 @@ namespace Titanium.Web.Proxy
/// </summary>
public
int
BufferSize
{
get
;
set
;
}
=
8192
;
/// <summary>
/// Manages certificates used by this proxy
/// </summary>
public
CertificateManager
CertificateManager
{
get
;
}
/// <summary>
/// The root certificate
/// </summary>
public
X509Certificate2
RootCertificate
{
get
{
return
c
ertificateManager
.
RootCertificate
;
}
set
{
c
ertificateManager
.
RootCertificate
=
value
;
}
get
{
return
C
ertificateManager
.
RootCertificate
;
}
set
{
C
ertificateManager
.
RootCertificate
=
value
;
}
}
/// <summary>
...
...
@@ -79,8 +79,8 @@ namespace Titanium.Web.Proxy
/// </summary>
public
string
RootCertificateIssuerName
{
get
{
return
c
ertificateManager
.
Issuer
;
}
set
{
certificateManager
.
RootCertificateName
=
value
;
}
get
{
return
C
ertificateManager
.
Issuer
;
}
set
{
CertificateManager
.
Issuer
=
value
;
}
}
/// <summary>
...
...
@@ -92,8 +92,8 @@ namespace Titanium.Web.Proxy
/// </summary>
public
string
RootCertificateName
{
get
{
return
c
ertificateManager
.
RootCertificateName
;
}
set
{
certificateManager
.
Issuer
=
value
;
}
get
{
return
C
ertificateManager
.
RootCertificateName
;
}
set
{
CertificateManager
.
RootCertificateName
=
value
;
}
}
/// <summary>
...
...
@@ -121,8 +121,8 @@ namespace Titanium.Web.Proxy
/// </summary>
public
CertificateEngine
CertificateEngine
{
get
{
return
c
ertificateManager
.
Engine
;
}
set
{
c
ertificateManager
.
Engine
=
value
;
}
get
{
return
C
ertificateManager
.
Engine
;
}
set
{
C
ertificateManager
.
Engine
=
value
;
}
}
/// <summary>
...
...
@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy
new
FireFoxProxySettingsManager
();
#endif
c
ertificateManager
=
new
CertificateManager
(
ExceptionFunc
);
C
ertificateManager
=
new
CertificateManager
(
ExceptionFunc
);
if
(
rootCertificateName
!=
null
)
{
RootCertificateName
=
rootCertificateName
;
...
...
@@ -356,7 +356,7 @@ namespace Titanium.Web.Proxy
EnsureRootCertificate
();
//If certificate was trusted by the machine
if
(
c
ertificateManager
.
CertValidated
)
if
(
C
ertificateManager
.
CertValidated
)
{
systemProxySettingsManager
.
SetHttpsProxy
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
...
...
@@ -435,7 +435,7 @@ namespace Titanium.Web.Proxy
Listen
(
endPoint
);
}
c
ertificateManager
.
ClearIdleCertificates
(
CertificateCacheTimeOutMinutes
);
C
ertificateManager
.
ClearIdleCertificates
(
CertificateCacheTimeOutMinutes
);
proxyRunning
=
true
;
}
...
...
@@ -468,7 +468,7 @@ namespace Titanium.Web.Proxy
ProxyEndPoints
.
Clear
();
c
ertificateManager
?.
StopClearIdleCertificates
();
C
ertificateManager
?.
StopClearIdleCertificates
();
proxyRunning
=
false
;
}
...
...
@@ -483,7 +483,7 @@ namespace Titanium.Web.Proxy
Stop
();
}
c
ertificateManager
?.
Dispose
();
C
ertificateManager
?.
Dispose
();
}
/// <summary>
...
...
@@ -543,13 +543,13 @@ namespace Titanium.Web.Proxy
private
void
EnsureRootCertificate
()
{
if
(!
c
ertificateManager
.
CertValidated
)
if
(!
C
ertificateManager
.
CertValidated
)
{
c
ertificateManager
.
CreateTrustedRootCertificate
();
C
ertificateManager
.
CreateTrustedRootCertificate
();
if
(
TrustRootCertificate
)
{
c
ertificateManager
.
TrustRootCertificate
();
C
ertificateManager
.
TrustRootCertificate
();
}
}
}
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
4047a006
...
...
@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy
{
sslStream
=
new
SslStream
(
clientStream
,
true
);
var
certificate
=
endPoint
.
GenericCertificate
??
c
ertificateManager
.
CreateCertificate
(
httpRemoteUri
.
Host
,
false
);
var
certificate
=
endPoint
.
GenericCertificate
??
C
ertificateManager
.
CreateCertificate
(
httpRemoteUri
.
Host
,
false
);
//Successfully managed to authenticate the client using the fake certificate
await
sslStream
.
AuthenticateAsServerAsync
(
certificate
,
false
,
...
...
@@ -177,7 +177,7 @@ namespace Titanium.Web.Proxy
var
sslStream
=
new
SslStream
(
clientStream
,
true
);
//implement in future once SNI supported by SSL stream, for now use the same certificate
var
certificate
=
c
ertificateManager
.
CreateCertificate
(
endPoint
.
GenericCertificateName
,
false
);
var
certificate
=
C
ertificateManager
.
CreateCertificate
(
endPoint
.
GenericCertificateName
,
false
);
try
{
...
...
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