Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (97 sloc) 7.48 KB
using Enclave.NET;
using Newtonsoft.Json;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using Enclave.NET.Configuration;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
private const string RC4Ticket = "YIIKbQYGKwYBBQUCoIIKYTCCCl2gMDAuBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBAGCNwICHgYKKwYBBAGCNwICCqKCCicEggojYIIKHwYJKoZIhvcSAQICAQBuggoOMIIKCqADAgEFoQMCAQ6iBwMFACAAAACjggiCYYIIfjCCCHqgAwIBBaEaGxhJREVOVElUWUlOVEVSVkVOVElPTi5DT02iLTAroAMCAQKhJDAiGwRIVFRQGxphYWRnLndpbmRvd3MubmV0Lm5zYXRjLm5ldKOCCCYwgggioAMCARehAwIBA6KCCBQEgggQj3u9OmvRqFMcpNQPVGL9GfXyt8uQXlMLzghXxhl6mLVqYSS6CwGoXwcGKqiDRXb1mll5br+naBy0ckhlnPN3LQmUNnEEk3rIOqHhKZYctiZ1QhVwatjBJ2yiEloGq+W4sl/IhHXH66y8I2mHLsuybiYodd9cUy4scQTx1S1HSFTqc7x0HyHillie/rv/oO5MBXld1kxvaKoKIQ34qZXLRBJhSD9nrEbBhlFY3vMYMgOFf3Jnz9AH23c/N9Sj82l3TbEBPxnBp7zn3FhSreghloZqxDcCbe82pUI5tdX6AIylDDprCs3fJBYvxQZG5w0IYoqJBi2T9YvBzaVH2qxTG59GDIzZ/1yL2Ux16XwrezATDZAc9Exr98eppGRqidw8oo2/wjsz92ye56BHCYN/i+F6IvuXaqTfWPYKI9Vw5xvnUodvy7SJJs+mxDAdShPyqYkI/rYbJyjECffdCBRDwuLnp6OsPdtRUpUC6uGBAtg6Zu9VYMGqJQV547em9pIuKHSd7Du4WYI1xdjIp/W2L8M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Bk0q/mf3TalaMBUpHTlDRsoy+KPItEE5htaIL+AB8xD9pIIBbTCCAWmgAwIBF6KCAWAEggFctFqM6qfEqPsGt0rJWeIFCeZJvVxCgyBMXT1NDApntPaBPQSI/r3RCNnOCyJ4uk46xD2uALdozuRfw4u7D9xWksYiGLTKSohNWIKmmUiPb7JCuuaHKk3o8HNfHZANRcr/nY2HDx9dMopF2xouf+D8q4jZIVKOKvsUQRW9WzVhi66aA5LJVNY7TseRyX6ijOdHEVMSo7jl510YCLNtIica699y6SGZHOoH4ZwMJVT7yXrOV2Y45VXz/zHU+s5+l5g1ZQBUzsqF97s2dZ+lgWvoKFa4oeXIhz0ScAZoch8cFYz6ANKqVXgWcwn1F6ZC+Bg4Wg59FISOBsLlSWVcbntYUvV4q2vXE8+hl8ee47gEfJavtewr6fZ1ZVKbKx30Nsq4uR016qgZTVn4E0N86HP5G7qAOunSWZTf6sqgO5EC7arj0mi0BJbdXywf0XZfdL+T0IX9kiIrADgK3e8K";
private static IConfigurationRoot _Configuration;
private static Server _ServerConfiguration;
public static IConfigurationRoot Configuration => _Configuration ??
(_Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", optional: true)
.AddJsonFile("settings.local.json", optional: true)
.AddEnvironmentVariables().Build());
public static Server ServerConfiguration => _ServerConfiguration ??
(_ServerConfiguration = Configuration.GetSection("Server").Get<Server>());
public static ServerCertificate ServerCertificate =>
ServerConfiguration?.ServerCertificate;
public static Certificate ClientCertificate =>
ServerConfiguration?.ClientCertificates?.FirstOrDefault();
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
if (ServerConfiguration == null)
{
Console.Error.WriteLine("Missing configuration settings file(s)");
Console.Error.WriteLine("Did you remember to copy over `settings.local.json`?");
}
MainAsync(args).Wait();
}
private static async Task MainAsync(string[] args)
{
var serverCert = ServerCertificate?.Thumbprint;
Console.WriteLine($"Server Certificate: {serverCert}");
var client = new EnclaveClient(
FindTestCertificate(),
pinnedCertificates: new[] {
new Certificate { Thumbprint = serverCert }
}
);
var key = await client.GenerateKey("rsa");
var encryptedValue = await client.Encrypt(key, new { foo = "bar" });
Console.WriteLine(encryptedValue.Value);
;
var decrypted = await client.Decrypt<object>(key, encryptedValue.Value);
Console.WriteLine(JsonConvert.SerializeObject(decrypted));
;
var keys = await client.ListKeys();
foreach (var k in keys)
{
Console.WriteLine(k.KeyId);
}
;
//var identity = await client.AuthenticateToken(KeyIdentifier.Parse("test"), "negotiate", RC4Ticket);
;
var key2 = await client.GenerateKey("ecc");
var signed = await client.Sign(key2, new { bar = "foo" });
Console.WriteLine(signed);
;
var signResult = await client.Validate(key2, signed);
Console.WriteLine(signResult);
;
}
private static X509Certificate2 FindTestCertificate()
{
var storeName = ServerCertificate?.StoreName;
var storeLocation = ServerCertificate?.StoreLocation;
var store = new X509Store(storeName.GetValueOrDefault(StoreName.My),
storeLocation.GetValueOrDefault(StoreLocation.CurrentUser));
store.Open(OpenFlags.ReadOnly);
try
{
var clientCert = ClientCertificate?.Thumbprint;
Console.WriteLine($"Client Certificate: {clientCert}");
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, clientCert, false);
if (certs.Count == 1)
{
return certs[0];
}
return null;
}
finally
{
store.Close();
}
}
}
}