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
64e96e09
Commit
64e96e09
authored
Nov 19, 2017
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Common BC Certificate mager
parent
d852471a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
265 deletions
+108
-265
MainWindow.xaml.cs
Examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
+3
-0
BCCertificateMaker.NetStandard.cs
...oxy/Network/Certificate/BCCertificateMaker.NetStandard.cs
+0
-251
BCCertificateMaker.cs
Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.cs
+105
-14
No files found.
Examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
View file @
64e96e09
...
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.EventArguments;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network
;
namespace
Titanium.Web.Proxy.Examples.Wpf
{
...
...
@@ -62,7 +63,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
public
MainWindow
()
{
proxyServer
=
new
ProxyServer
();
proxyServer
.
CertificateEngine
=
CertificateEngine
.
DefaultWindows
;
proxyServer
.
TrustRootCertificate
=
true
;
proxyServer
.
CertificateManager
.
TrustRootCertificateAsAdministrator
();
proxyServer
.
ForwardToUpstreamGateway
=
true
;
var
explicitEndPoint
=
new
ExplicitProxyEndPoint
(
IPAddress
.
Any
,
8000
,
true
)
...
...
Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.NetStandard.cs
deleted
100644 → 0
View file @
d852471a
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.cs
View file @
64e96e09
#
if
NET45
using
System
;
using
System
;
using
System.IO
;
using
System.Security.Cryptography
;
using
System.Security.Cryptography.X509Certificates
;
using
System.Threading
;
using
Org.BouncyCastle.Asn1
;
...
...
@@ -102,7 +103,6 @@ namespace Titanium.Web.Proxy.Network.Certificate
// Self-sign the certificate
var
certificate
=
certificateGenerator
.
Generate
(
signatureFactory
);
var
x509Certificate
=
new
X509Certificate2
(
certificate
.
GetEncoded
());
// Corresponding private key
var
privateKeyInfo
=
PrivateKeyInfoFactory
.
CreatePrivateKeyInfo
(
subjectKeyPair
.
Private
);
...
...
@@ -118,13 +118,35 @@ namespace Titanium.Web.Proxy.Network.Certificate
var
rsaparams
=
new
RsaPrivateCrtKeyParameters
(
rsa
.
Modulus
,
rsa
.
PublicExponent
,
rsa
.
PrivateExponent
,
rsa
.
Prime1
,
rsa
.
Prime2
,
rsa
.
Exponent1
,
rsa
.
Exponent2
,
rsa
.
Coefficient
);
#if NET45
// Set private key onto certificate instance
var
x509Certificate
=
new
X509Certificate2
(
certificate
.
GetEncoded
());
x509Certificate
.
PrivateKey
=
DotNetUtilities
.
ToRSA
(
rsaparams
);
x509Certificate
.
FriendlyName
=
subjectName
;
#else
var
x509Certificate
=
WithPrivateKey
(
certificate
,
rsaparams
);
x509Certificate
.
FriendlyName
=
subjectName
;
#endif
return
x509Certificate
;
}
private
static
X509Certificate2
WithPrivateKey
(
Org
.
BouncyCastle
.
X509
.
X509Certificate
certificate
,
AsymmetricKeyParameter
privateKey
)
{
const
string
password
=
"password"
;
var
store
=
new
Pkcs12Store
();
var
entry
=
new
X509CertificateEntry
(
certificate
);
store
.
SetCertificateEntry
(
certificate
.
SubjectDN
.
ToString
(),
entry
);
store
.
SetKeyEntry
(
certificate
.
SubjectDN
.
ToString
(),
new
AsymmetricKeyEntry
(
privateKey
),
new
[]
{
entry
});
using
(
var
ms
=
new
MemoryStream
())
{
store
.
Save
(
ms
,
password
.
ToCharArray
(),
new
SecureRandom
(
new
CryptoApiRandomGenerator
()));
return
new
X509Certificate2
(
ms
.
ToArray
(),
password
,
X509KeyStorageFlags
.
Exportable
);
}
}
/// <summary>
/// Makes the certificate internal.
/// </summary>
...
...
@@ -136,18 +158,52 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// <param name="signingCertificate">The signing certificate.</param>
/// <returns>X509Certificate2 instance.</returns>
/// <exception cref="System.ArgumentException">You must specify a Signing Certificate if and only if you are not creating a root.</exception>
private
X509Certificate2
MakeCertificateInternal
(
bool
isRoot
,
string
hostName
,
string
subjectName
,
DateTime
validFrom
,
DateTime
validTo
,
X509Certificate2
signingCertificate
)
private
X509Certificate2
MakeCertificateInternal
(
bool
isRoot
,
string
hostName
,
string
subjectName
,
DateTime
validFrom
,
DateTime
validTo
,
X509Certificate2
signingCertificate
)
{
if
(
isRoot
!=
(
null
==
signingCertificate
))
{
throw
new
ArgumentException
(
"You must specify a Signing Certificate if and only if you are not creating a root."
,
nameof
(
signingCertificate
));
}
return
isRoot
?
GenerateCertificate
(
null
,
subjectName
,
subjectName
,
validFrom
,
validTo
)
:
GenerateCertificate
(
hostName
,
subjectName
,
signingCertificate
.
Subject
,
validFrom
,
validTo
,
issuerPrivateKey
:
DotNetUtilities
.
GetKeyPair
(
signingCertificate
.
PrivateKey
).
Private
);
if
(
isRoot
)
{
return
GenerateCertificate
(
null
,
subjectName
,
subjectName
,
validFrom
,
validTo
);
}
else
{
#if NET45
var
kp
=
DotNetUtilities
.
GetKeyPair
(
signingCertificate
.
PrivateKey
);
#else
var
rsa
=
signingCertificate
.
GetRSAPrivateKey
();
var
kp
=
GetRsaKeyPair
(
rsa
.
ExportParameters
(
true
));
#endif
return
GenerateCertificate
(
hostName
,
subjectName
,
signingCertificate
.
Subject
,
validFrom
,
validTo
,
issuerPrivateKey
:
kp
.
Private
);
}
}
static
AsymmetricCipherKeyPair
GetRsaKeyPair
(
RSAParameters
rp
)
{
BigInteger
modulus
=
new
BigInteger
(
1
,
rp
.
Modulus
);
BigInteger
pubExp
=
new
BigInteger
(
1
,
rp
.
Exponent
);
RsaKeyParameters
pubKey
=
new
RsaKeyParameters
(
false
,
modulus
,
pubExp
);
RsaPrivateCrtKeyParameters
privKey
=
new
RsaPrivateCrtKeyParameters
(
modulus
,
pubExp
,
new
BigInteger
(
1
,
rp
.
D
),
new
BigInteger
(
1
,
rp
.
P
),
new
BigInteger
(
1
,
rp
.
Q
),
new
BigInteger
(
1
,
rp
.
DP
),
new
BigInteger
(
1
,
rp
.
DQ
),
new
BigInteger
(
1
,
rp
.
InverseQ
));
return
new
AsymmetricCipherKeyPair
(
pubKey
,
privKey
);
}
/// <summary>
...
...
@@ -163,10 +219,10 @@ namespace Titanium.Web.Proxy.Network.Certificate
bool
switchToMtaIfNeeded
,
X509Certificate2
signingCert
=
null
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
))
{
X509Certificate2
certificate
=
null
;
#if NET45
if
(
switchToMtaIfNeeded
&&
Thread
.
CurrentThread
.
GetApartmentState
()
!=
ApartmentState
.
MTA
)
{
X509Certificate2
certificate
=
null
;
using
(
var
manualResetEvent
=
new
ManualResetEventSlim
(
false
))
{
ThreadPool
.
QueueUserWorkItem
(
o
=>
...
...
@@ -184,10 +240,45 @@ namespace Titanium.Web.Proxy.Network.Certificate
return
certificate
;
}
#endif
return
MakeCertificateInternal
(
isRoot
,
subject
,
$"CN=
{
subject
}
"
,
DateTime
.
UtcNow
.
AddDays
(-
certificateGraceDays
),
DateTime
.
UtcNow
.
AddDays
(
certificateValidDays
),
isRoot
?
null
:
signingCert
);
}
class
CryptoApiRandomGenerator
:
IRandomGenerator
{
readonly
RandomNumberGenerator
rndProv
;
public
CryptoApiRandomGenerator
()
{
rndProv
=
RandomNumberGenerator
.
Create
();
}
public
void
AddSeedMaterial
(
byte
[]
seed
)
{
}
return
MakeCertificateInternal
(
isRoot
,
subject
,
$"CN=
{
subject
}
"
,
DateTime
.
UtcNow
.
AddDays
(-
certificateGraceDays
),
DateTime
.
UtcNow
.
AddDays
(
certificateValidDays
),
isRoot
?
null
:
signingCert
);
public
void
AddSeedMaterial
(
long
seed
)
{
}
public
void
NextBytes
(
byte
[]
bytes
)
{
rndProv
.
GetBytes
(
bytes
);
}
public
void
NextBytes
(
byte
[]
bytes
,
int
start
,
int
len
)
{
if
(
start
<
0
)
throw
new
ArgumentException
(
"Start offset cannot be negative"
,
"start"
);
if
(
bytes
.
Length
<
(
start
+
len
))
throw
new
ArgumentException
(
"Byte array too small for requested offset and length"
);
if
(
bytes
.
Length
==
len
&&
start
==
0
)
{
NextBytes
(
bytes
);
}
else
{
byte
[]
tmpBuf
=
new
byte
[
len
];
NextBytes
(
tmpBuf
);
Array
.
Copy
(
tmpBuf
,
0
,
bytes
,
start
,
len
);
}
}
}
}
}
#
endif
\ No newline at end of file
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