Skip to content

Instantly share code, notes, and snippets.

View SteveSyfuhs's full-sized avatar

Steve Syfuhs SteveSyfuhs

View GitHub Profile
@SteveSyfuhs
SteveSyfuhs / KillPortOwner.cs
Created August 4, 2023 02:56
Finding and killing a socket owner by port
public static void KillExistingHosts(int port)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
return;
}
using (var existing = Win32.FindProcessOwner(port))
{
public class FakeRealmService : IRealmService
{
public FakeRealmService(string realm)
{
Name = realm;
}
public IRealmSettings Settings => new FakeRealmSettings();
public IPrincipalService Principals => new FakePrincipalService();
static async Task Main()
{
var builder = new HostBuilder()
.ConfigureLogging((_, factory) =>
{
factory.AddConsole(opt => opt.IncludeScopes = true);
factory.AddFilter<ConsoleLoggerProvider>(level => level >= LogLevel.Trace);
});
var host = builder.Build();
public class MyStartup : IStartupTransform
{
public void Transform(IServiceCollection services)
{
services.AddScoped<IKeyStorageService, MyStorageService>();
}
}
@SteveSyfuhs
SteveSyfuhs / IKeyStorageService.cs
Created August 22, 2017 01:28
Storage Interface
public interface IKeyStorageService
{
Task<IKeyIdentifier> AddKey(IEnclaveKey key);
Task<IEnclaveKey> GetKey(IKeyIdentifier id);
Task<IQueryable<IKeyIdentifier>> ListKeys();
}
@SteveSyfuhs
SteveSyfuhs / ICryptoProcessor.cs
Created August 22, 2017 01:27
Crypto Interface
public interface ICryptoProcessor
{
Task<IEnclaveKey> GenerateKey(string keyType);
Task<string> Encrypt(IEnclaveKey key, object value);
Task<T> Decrypt<T>(IEnclaveKey key, string ciphertext);
Task<string> Sign(IEnclaveKey key, object value);
@SteveSyfuhs
SteveSyfuhs / settings.json
Created August 22, 2017 01:23
Startup Configuration
{
"StartupTransformType": "My.Namespace.MyClass, MyAssembly",
"Server": {
"Port": "44320",
"ServerCertificate": {
"StoreName": "My",
"StoreLocation": "LocalMachine",
"Thumbprint": "84ee508fb0e1cf7c0075e20a431b6166cffc572f"
},
"ClientCertificates": [
@SteveSyfuhs
SteveSyfuhs / Client.cs
Created August 22, 2017 01:21
Enclave.NET Client
var serverCertificate = new Certificate { Thumbprint = "84ee508fb0e1cf7c0075e20a431b6166cffc572f" };
var client = new EnclaveClient(
FindTestCertificate(),
pinnedCertificates: new[] {
serverCertificate
}
);
var key = await client.GenerateKey("rsa");
internal class KerberosEndToEndMiddleware
{
private readonly SimpleKerberosValidator validator;
private readonly NextFunc next;
public KerberosEndToEndMiddleware(NextFunc next)
{
this.next = next;
@SteveSyfuhs
SteveSyfuhs / KerberosValidator
Created March 19, 2017 19:54
A few lines of code validates a ticket.
var secret = "P@ssw0rd!"; // => Encoding.Unicode.GetBytes(...)
var validator = new SimpleKerberosValidator(secret);
ClaimsIdentity identity = validator.Validate("YIIG6QYGKwYBBQUCoIIG3TCCBtmgMDAuBgkqhkiC9xI...");
Debug.Assert(identity.Name == "user@domain.com");