-
Notifications
You must be signed in to change notification settings - Fork 1
/
interaction.linq
294 lines (241 loc) · 9.89 KB
/
interaction.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<Query Kind="Program">
<NuGetReference>BouncyCastle</NuGetReference>
<NuGetReference>Nancy</NuGetReference>
<NuGetReference>Nancy.Hosting.Self</NuGetReference>
<Namespace>System.Security.Cryptography</Namespace>
<Namespace>System.Security.Cryptography.X509Certificates</Namespace>
<Namespace>Nancy.Hosting.Self</Namespace>
<Namespace>Nancy</Namespace>
<Namespace>Org.BouncyCastle.Crypto.Prng</Namespace>
<Namespace>Org.BouncyCastle.Security</Namespace>
<Namespace>Org.BouncyCastle.X509</Namespace>
<Namespace>Org.BouncyCastle.Utilities</Namespace>
<Namespace>Org.BouncyCastle.Math</Namespace>
<Namespace>Org.BouncyCastle.Asn1.X509</Namespace>
<Namespace>Org.BouncyCastle.X509.Extension</Namespace>
<Namespace>Org.BouncyCastle.Crypto</Namespace>
<Namespace>Org.BouncyCastle.Crypto.Generators</Namespace>
<Namespace>Org.BouncyCastle.Crypto.Operators</Namespace>
<Namespace>Org.BouncyCastle.Crypto.Parameters</Namespace>
<Namespace>Org.BouncyCastle.Asn1</Namespace>
<Namespace>Org.BouncyCastle.Pkcs</Namespace>
<Namespace>Org.BouncyCastle.Asn1.Pkcs</Namespace>
<Namespace>System.Runtime.InteropServices</Namespace>
</Query>
public const int HTTPPORT = 40849;
public const int HTTPSPORT = 40850;
void Main(string[] args)
{
if (!IsUserAnAdmin())
{
//You can remove this line after first run, admin righst is required only once to setup a ssl certificate
Console.WriteLine("Admin rights is required, please run LINQPad as an administrator");
return;
}
var certSubjectName = "CsClientApp SSL Certificate";
var rootSubjectName = "CsClientApp Root CA";
SslHelper.CheckOrCreateCertificates(certSubjectName, rootSubjectName);
var hostConfigs = new HostConfiguration();
hostConfigs.UrlReservations.CreateAutomatically = true;
hostConfigs.RewriteLocalhost = false;
var uris = new Uri[]
{
new Uri($"http://localhost:{HTTPPORT}"),
new Uri($"http://127.0.0.1:{HTTPPORT}"),
new Uri($"https://localhost:{HTTPSPORT}")
};
using (var host = new NancyHost(hostConfigs, uris))
{
host.Start();
Console.WriteLine("Listening on:");
foreach (var uri in uris)
{
Console.WriteLine(uri.ToString());
}
Util.ReadLine();
}
}
[DllImport("shell32.dll")] public static extern bool IsUserAnAdmin();
public class CalcNancyModule : NancyModule
{
public CalcNancyModule()
{
After.AddItemToEndOfPipeline((ctx) => ctx.Response
.WithHeader("Access-Control-Allow-Origin", GetOrigin(ctx))
.WithHeader("Access-Control-Allow-Methods", "POST,GET")
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));
Get["/Calc"] = _ =>
{
//Simaulate hard work...
Thread.Sleep(1000);
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"{{ \"version\": \"{assemblyVersion}\" }}";
};
Get["/Calc/Add"] = _ =>
{
//Simaulate hard work...
Thread.Sleep(1000);
var num1String = Request.Query["num1"] ?? "";
var num2String = Request.Query["num2"] ?? "";
var parsed1 = int.TryParse(num1String, out int num1);
var parsed2 = int.TryParse(num2String, out int num2);
var parsed = parsed1 && parsed2;
if (parsed)
return $"{{ \"result\": \"{num1 + num2}\" }}";
else
return $"{{ \"error\": \"can't parse input values\" }}";
};
}
private string GetOrigin(NancyContext ctx)
{
return ctx.Request?.Headers["Origin"]?.FirstOrDefault() ?? "";
}
}
public static class SslHelper
{
private const int KEY_STRENGTH = 2048;
private const string ALGORITHM = "SHA256WITHRSA";
private const string AppId = "{d44dd183-66da-40da-9cc3-1663a3186039}";
public static X509Certificate2 CheckOrCreateCertificates(string certName, string rootName)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var existingCert = store.Certificates.Find(X509FindType.FindBySubjectName, certName, false);
if (existingCert.Count > 0)
{
store.Close();
return existingCert[0];
}
else
{
return CreateCertificate(certName, rootName);
}
}
private static void RegisterSslOnPort(string certThumbprint)
{
var commands = new string[]
{
$"http delete sslcert ipport=0.0.0.0:{HTTPSPORT}",
$"http add sslcert ipport=0.0.0.0:{HTTPSPORT} certhash={certThumbprint} appid=\"{AppId}\""
};
foreach (var cmd in commands)
{
var procStartInfo = new ProcessStartInfo("netsh", cmd)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = Process.Start(procStartInfo);
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
Console.WriteLine(line);
}
process.WaitForExit();
}
}
private static X509Certificate2 CreateCertificate(string certName, string rootName)
{
var rootStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
rootStore.Open(OpenFlags.ReadWrite);
var personalStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
personalStore.Open(OpenFlags.ReadWrite);
var caCert = GenerateCACertificate(rootName);
var cert = GenerateSSLCertificate(certName, caCert);
rootStore.Add(caCert);
personalStore.Add(cert);
rootStore.Close();
personalStore.Close();
RegisterSslOnPort(cert.Thumbprint);
return cert;
}
private static X509Certificate2 GenerateCACertificate(string rootName, X509Certificate2 issuer = null)
{
var randomGenerator = new CryptoApiRandomGenerator();
var random = new SecureRandom(randomGenerator);
var certificateGenerator = new X509V3CertificateGenerator();
var serialNumber = BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.ValueOf(long.MaxValue), random);
certificateGenerator.SetSerialNumber(serialNumber);
certificateGenerator.SetIssuerDN(new X509Name($"CN={rootName}"));
certificateGenerator.SetSubjectDN(new X509Name($"CN={rootName}"));
if (issuer != null)
{
var authorityKeyIdentifier = new AuthorityKeyIdentifierStructure(
DotNetUtilities.FromX509Certificate(issuer));
certificateGenerator.AddExtension(
X509Extensions.AuthorityKeyIdentifier.Id, false, authorityKeyIdentifier);
}
certificateGenerator.AddExtension(
X509Extensions.BasicConstraints.Id, true, new BasicConstraints(true));
certificateGenerator.SetNotBefore(DateTime.UtcNow.Date.AddHours(-12));
certificateGenerator.SetNotAfter(DateTime.UtcNow.Date.AddYears(100));
var keyGenerationParameters = new KeyGenerationParameters(random, KEY_STRENGTH);
var keyPairGenerator = new RsaKeyPairGenerator();
keyPairGenerator.Init(keyGenerationParameters);
var subjectKeyPair = keyPairGenerator.GenerateKeyPair();
var issuerKeyPair = issuer == null
? subjectKeyPair
: DotNetUtilities.GetKeyPair(issuer.PrivateKey);
certificateGenerator.SetPublicKey(subjectKeyPair.Public);
var signatureFactory = new Asn1SignatureFactory(ALGORITHM, issuerKeyPair.Private, random);
var certificate = certificateGenerator.Generate(signatureFactory);
return new X509Certificate2(certificate.GetEncoded())
{
PrivateKey = ToDotNetKey((RsaPrivateCrtKeyParameters)subjectKeyPair.Private)
};
}
private static X509Certificate2 GenerateSSLCertificate(string subjectName, X509Certificate2 issuer)
{
var randomGenerator = new CryptoApiRandomGenerator();
var random = new SecureRandom(randomGenerator);
var keyPairGenerator = new RsaKeyPairGenerator();
keyPairGenerator.Init(new KeyGenerationParameters(random, KEY_STRENGTH));
var keyPair = keyPairGenerator.GenerateKeyPair();
var certificateGenerator = new X509V3CertificateGenerator();
var certName = new X509Name($"CN={subjectName}");
var serialNumber = BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.ValueOf(long.MaxValue), random);
certificateGenerator.SetSerialNumber(serialNumber);
certificateGenerator.SetSubjectDN(certName);
certificateGenerator.SetIssuerDN(certName);
certificateGenerator.SetNotAfter(DateTime.Now.AddYears(100));
certificateGenerator.SetNotBefore(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
certificateGenerator.SetPublicKey(keyPair.Public);
var authorityKeyIdentifier = new AuthorityKeyIdentifierStructure(DotNetUtilities.FromX509Certificate(issuer));
certificateGenerator.AddExtension(X509Extensions.AuthorityKeyIdentifier.Id, false, authorityKeyIdentifier);
certificateGenerator.AddExtension(X509Extensions.KeyUsage.Id, false,
new KeyUsage(KeyUsage.DataEncipherment | KeyUsage.KeyEncipherment | KeyUsage.DigitalSignature));
GeneralNames subjectAltName = new GeneralNames(new GeneralName(GeneralName.DnsName, "localhost"));
certificateGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName);
certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false,
new ExtendedKeyUsage(new List<DerObjectIdentifier> { new DerObjectIdentifier("1.3.6.1.5.5.7.3.1") }));
var issuerKeyPair = issuer == null
? keyPair
: DotNetUtilities.GetKeyPair(issuer.PrivateKey);
var signatureFactory = new Asn1SignatureFactory(ALGORITHM, issuerKeyPair.Private, random);
var newCert = certificateGenerator.Generate(signatureFactory);
return new X509Certificate2(newCert.GetEncoded())
{
PrivateKey = ToDotNetKey((RsaPrivateCrtKeyParameters)keyPair.Private)
};
}
private static AsymmetricAlgorithm ToDotNetKey(RsaPrivateCrtKeyParameters privateKey)
{
var keyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
var seq = (Asn1Sequence)Asn1Object.FromByteArray(keyInfo.ParsePrivateKey().GetDerEncoded());
if (seq.Count != 9)
throw new Exception("Malformed sequence in RSA private key");
var rsa = RsaPrivateKeyStructure.GetInstance(seq);
var rsaParams = new RsaPrivateCrtKeyParameters(
rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent,
rsa.Prime1, rsa.Prime2, rsa.Exponent1,
rsa.Exponent2, rsa.Coefficient);
var cspParams = new CspParameters
{
KeyContainerName = Guid.NewGuid().ToString(),
KeyNumber = (int)KeyNumber.Exchange,
Flags = CspProviderFlags.UseMachineKeyStore
};
return DotNetUtilities.ToRSA(rsaParams, cspParams);
}
}