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
f65cd0fd
Commit
f65cd0fd
authored
Jul 07, 2015
by
titanium007
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ArachisH-master'
parents
3ffd1f2c
d586a230
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
309 additions
and
119 deletions
+309
-119
CertificateManager.cs
Titanium.Web.Proxy/Helpers/CertificateManager.cs
+176
-0
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+16
-3
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+2
-2
Titanium.Web.Proxy.csproj
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
+115
-114
No files found.
Titanium.Web.Proxy/Helpers/CertificateManager.cs
0 → 100644
View file @
f65cd0fd
using
System
;
using
System.IO
;
using
System.Diagnostics
;
using
System.Collections.Generic
;
using
System.Security.Cryptography.X509Certificates
;
namespace
Titanium.Web.Proxy.Helpers
{
public
class
CertificateManager
{
private
const
string
CERT_CREATE_FORMAT
=
"-ss {0} -n \"CN={1}, O={2}\" -sky {3} -cy {4} -m 120 -a sha256 -eku 1.3.6.1.5.5.7.3.1 -b {5:MM/dd/yyyy} {6}"
;
private
readonly
IDictionary
<
string
,
X509Certificate2
>
_certificateCache
;
public
string
Issuer
{
get
;
private
set
;
}
public
string
RootCertificateName
{
get
;
private
set
;
}
public
X509Store
MyStore
{
get
;
private
set
;
}
public
X509Store
RootStore
{
get
;
private
set
;
}
public
CertificateManager
(
string
issuer
,
string
rootCertificateName
)
{
Issuer
=
issuer
;
RootCertificateName
=
rootCertificateName
;
MyStore
=
new
X509Store
(
StoreName
.
My
,
StoreLocation
.
CurrentUser
);
RootStore
=
new
X509Store
(
StoreName
.
Root
,
StoreLocation
.
CurrentUser
);
_certificateCache
=
new
Dictionary
<
string
,
X509Certificate2
>();
}
/// <summary>
/// Attempts to move a self-signed certificate to the root store.
/// </summary>
/// <returns>true if succeeded, else false</returns>
public
bool
CreateTrustedRootCertificate
()
{
X509Certificate2
rootCertificate
=
CreateCertificate
(
RootStore
,
RootCertificateName
);
return
rootCertificate
!=
null
;
}
/// <summary>
/// Attempts to remove the self-signed certificate from the root store.
/// </summary>
/// <returns>true if succeeded, else false</returns>
public
bool
DestroyTrustedRootCertificate
()
{
return
DestroyCertificate
(
RootStore
,
RootCertificateName
);
}
public
X509Certificate2Collection
FindCertificates
(
string
certificateSubject
)
{
return
FindCertificates
(
MyStore
,
certificateSubject
);
}
protected
virtual
X509Certificate2Collection
FindCertificates
(
X509Store
store
,
string
certificateSubject
)
{
X509Certificate2Collection
discoveredCertificates
=
store
.
Certificates
.
Find
(
X509FindType
.
FindBySubjectDistinguishedName
,
certificateSubject
,
false
);
return
discoveredCertificates
.
Count
>
0
?
discoveredCertificates
:
null
;
}
public
X509Certificate2
CreateCertificate
(
string
certificateName
)
{
return
CreateCertificate
(
MyStore
,
certificateName
);
}
protected
virtual
X509Certificate2
CreateCertificate
(
X509Store
store
,
string
certificateName
)
{
if
(
_certificateCache
.
ContainsKey
(
certificateName
))
return
_certificateCache
[
certificateName
];
lock
(
store
)
{
X509Certificate2
certificate
=
null
;
try
{
store
.
Open
(
OpenFlags
.
ReadWrite
);
string
certificateSubject
=
string
.
Format
(
"CN={0}, O={1}"
,
certificateName
,
Issuer
);
X509Certificate2Collection
certificates
=
FindCertificates
(
store
,
certificateSubject
);
if
(
certificates
!=
null
)
certificate
=
certificates
[
0
];
if
(
certificate
==
null
)
{
string
[]
args
=
new
[]
{
GetCertificateCreateArgs
(
store
,
certificateName
)
};
CreateCertificate
(
args
);
certificates
=
FindCertificates
(
store
,
certificateSubject
);
return
certificates
!=
null
?
certificates
[
0
]
:
null
;
}
return
certificate
;
}
finally
{
store
.
Close
();
if
(
certificate
!=
null
&&
!
_certificateCache
.
ContainsKey
(
certificateName
))
_certificateCache
.
Add
(
certificateName
,
certificate
);
}
}
}
protected
virtual
void
CreateCertificate
(
string
[]
args
)
{
using
(
var
process
=
new
Process
())
{
if
(!
File
.
Exists
(
"makecert.exe"
))
throw
new
Exception
(
"Unable to locate 'makecert.exe'."
);
process
.
StartInfo
.
Verb
=
"runas"
;
process
.
StartInfo
.
Arguments
=
args
!=
null
?
args
[
0
]
:
string
.
Empty
;
process
.
StartInfo
.
CreateNoWindow
=
true
;
process
.
StartInfo
.
UseShellExecute
=
false
;
process
.
StartInfo
.
FileName
=
"makecert.exe"
;
process
.
Start
();
process
.
WaitForExit
();
}
}
public
bool
DestroyCertificate
(
string
certificateName
)
{
return
DestroyCertificate
(
MyStore
,
certificateName
);
}
protected
virtual
bool
DestroyCertificate
(
X509Store
store
,
string
certificateName
)
{
lock
(
store
)
{
X509Certificate2Collection
certificates
=
null
;
try
{
store
.
Open
(
OpenFlags
.
ReadWrite
);
string
certificateSubject
=
string
.
Format
(
"CN={0}, O={1}"
,
certificateName
,
Issuer
);
certificates
=
FindCertificates
(
store
,
certificateSubject
);
if
(
certificates
!=
null
)
{
store
.
RemoveRange
(
certificates
);
certificates
=
FindCertificates
(
store
,
certificateSubject
);
}
return
certificates
==
null
;
}
finally
{
store
.
Close
();
if
(
certificates
==
null
&&
_certificateCache
.
ContainsKey
(
certificateName
))
{
_certificateCache
.
Remove
(
certificateName
);
}
}
}
}
protected
virtual
string
GetCertificateCreateArgs
(
X509Store
store
,
string
certificateName
)
{
bool
isRootCertificate
=
(
certificateName
==
RootCertificateName
);
string
certCreatArgs
=
string
.
Format
(
CERT_CREATE_FORMAT
,
store
.
Name
,
certificateName
,
Issuer
,
isRootCertificate
?
"signature"
:
"exchange"
,
isRootCertificate
?
"authority"
:
"end"
,
DateTime
.
Now
,
isRootCertificate
?
"-h 1 -r"
:
string
.
Format
(
"-pe -in \"{0}\" -is Root"
,
RootCertificateName
));
return
certCreatArgs
;
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/ProxyServer.cs
View file @
f65cd0fd
...
...
@@ -55,6 +55,13 @@ namespace Titanium.Web.Proxy
}
}
public
static
CertificateManager
CertManager
{
get
;
set
;
}
static
ProxyServer
()
{
CertManager
=
new
CertificateManager
(
"Titanium"
,
"Titanium Root Certificate Authority"
);
}
public
ProxyServer
()
{
...
...
@@ -93,7 +100,13 @@ namespace Titanium.Web.Proxy
FireFoxHelper
.
AddFirefox
();
RootCertificateName
=
RootCertificateName
==
null
?
"DO_NOT_TRUST_FiddlerRoot"
:
RootCertificateName
;
CertificateHelper
.
InstallCertificate
(
Path
.
Combine
(
System
.
IO
.
Path
.
GetDirectoryName
(
System
.
Reflection
.
Assembly
.
GetExecutingAssembly
().
Location
),
string
.
Concat
(
RootCertificateName
,
".cer"
)));
bool
certTrusted
=
CertManager
.
CreateTrustedRootCertificate
();
if
(!
certTrusted
)
{
// The user didn't want to install the self-signed certificate to the root store.
}
//CertificateHelper.InstallCertificate(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), string.Concat(RootCertificateName, ".cer")));
if
(
EnableSSL
)
{
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
f65cd0fd
...
...
@@ -120,7 +120,7 @@ namespace Titanium.Web.Proxy
return
;
}
Monitor
.
Enter
(
certificateAccessLock
);
var
_certificate
=
CertificateHelper
.
GetCertificate
(
RootCertificateName
,
tunnelHostName
);
var
_certificate
=
ProxyServer
.
CertManager
.
CreateCertificate
(
tunnelHostName
);
//
CertificateHelper.GetCertificate(RootCertificateName, tunnelHostName);
Monitor
.
Exit
(
certificateAccessLock
);
SslStream
sslStream
=
null
;
...
...
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
View file @
f65cd0fd
...
...
@@ -76,6 +76,7 @@
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
<Compile
Include=
"Helpers\CertificateManager.cs"
/>
<Compile
Include=
"Helpers\Firefox.cs"
/>
<Compile
Include=
"Helpers\SystemProxy.cs"
/>
<Compile
Include=
"RequestHandler.cs"
/>
...
...
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