From ec9a2d9b8556be8f5f88b44e18a9fc03b4cefbd6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 4 Feb 2021 17:59:41 -0600 Subject: move PBKDF2 --- src/slskd/Common/Cryptography/PBKDF2.cs | 26 ++++++++++++++++++++++++++ src/slskd/Security/PBKDF2.cs | 28 ---------------------------- 2 files changed, 26 insertions(+), 28 deletions(-) create mode 100644 src/slskd/Common/Cryptography/PBKDF2.cs delete mode 100644 src/slskd/Security/PBKDF2.cs (limited to 'src') diff --git a/src/slskd/Common/Cryptography/PBKDF2.cs b/src/slskd/Common/Cryptography/PBKDF2.cs new file mode 100644 index 00000000..4b8b83e4 --- /dev/null +++ b/src/slskd/Common/Cryptography/PBKDF2.cs @@ -0,0 +1,26 @@ +namespace slskd.Security +{ + using Microsoft.AspNetCore.Cryptography.KeyDerivation; + using System.Security.Cryptography; + + /// + /// PBKDF2/RFC 2898 implementation + /// + public static class PBKDF2 + { + /// + /// Gets a 256 bit (32 byte) key derived from the specified using PBKDF2/RFC 2898 + /// + /// + /// + public static byte[] GetKey(string password) + { + byte[] salt = new byte[16]; + int iterations = 1000; + + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); + } + } +} diff --git a/src/slskd/Security/PBKDF2.cs b/src/slskd/Security/PBKDF2.cs deleted file mode 100644 index 4a857135..00000000 --- a/src/slskd/Security/PBKDF2.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace slskd.Security -{ - using Microsoft.AspNetCore.Cryptography.KeyDerivation; - using System.Security.Cryptography; - - /// - /// PBKDF2/RFC 2898 implementation - /// - public static class PBKDF2 - { - /// - /// Gets a 256 bit (32 byte) key derived from the specified using PBKDF2/RFC 2898 - /// - /// - /// - public static byte[] GetKey(string password) - { - byte[] salt = new byte[16]; - int iterations = 1000; - - using (var rng = RandomNumberGenerator.Create()) - { - rng.GetBytes(salt); - return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); - } - } - } -} -- cgit v1.2.3 From f562cfc7b8a8f2aaad72729a8f1df6578b32821f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 4 Feb 2021 17:59:50 -0600 Subject: add X509 --- src/slskd/Common/Cryptography/X509.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/slskd/Common/Cryptography/X509.cs (limited to 'src') diff --git a/src/slskd/Common/Cryptography/X509.cs b/src/slskd/Common/Cryptography/X509.cs new file mode 100644 index 00000000..4157cffc --- /dev/null +++ b/src/slskd/Common/Cryptography/X509.cs @@ -0,0 +1,26 @@ +namespace slskd.Common.Cryptography +{ + using System; + using System.Security.Cryptography; + using System.Security.Cryptography.X509Certificates; + + public class X509 + { + public static X509Certificate2 Generate(string subject, string password, X509KeyStorageFlags x509KeyStorageFlags) + { + using RSA rsa = RSA.Create(2048); + + var request = new CertificateRequest( + new X500DistinguishedName($"CN={subject}"), + rsa, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + + var certificate = request.CreateSelfSigned( + new DateTimeOffset(DateTime.UtcNow.AddDays(-1)), + new DateTimeOffset(DateTime.UtcNow.AddDays(36500))); + + return new X509Certificate2(certificate.Export(X509ContentType.Pkcs12, password), password, x509KeyStorageFlags); + } + } +} -- cgit v1.2.3 From 795d7874532b50310bcebedf72478d414c3c0c23 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 4 Feb 2021 18:03:56 -0600 Subject: add generate-certificate --- src/slskd/Common/Configuration/Options.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Configuration/Options.cs b/src/slskd/Common/Configuration/Options.cs index f10bd5a3..19300f1f 100644 --- a/src/slskd/Common/Configuration/Options.cs +++ b/src/slskd/Common/Configuration/Options.cs @@ -157,6 +157,14 @@ Type: typeof(bool), Default: false, Description: "display environment variables"), + new( + ShortName: 'g', + LongName: "generate-certificate", + EnvironmentVariable: null, + Key: null, + Type: typeof(bool), + Default: false, + Description: "generate X509 certificate and password for HTTPs"), new( ShortName: 'd', LongName: "debug", -- cgit v1.2.3 From c251608c61796c5c829c5e6a94948a62a1de427d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 4 Feb 2021 18:16:24 -0600 Subject: add certificates --- src/slskd/Program.cs | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b6a47572..e3122d2a 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -7,14 +7,17 @@ using Serilog; using Serilog.Events; using Serilog.Sinks.Grafana.Loki; + using slskd.Common.Cryptography; using slskd.Configuration; using slskd.Validation; using System; using System.Collections.Generic; using System.IO; using System.Linq; + using System.Net; using System.Reflection; - + using System.Security.Cryptography.X509Certificates; + public static class ProgramExtensions { public static IConfigurationBuilder AddConfigurationProviders(this IConfigurationBuilder builder, string environmentVariablePrefix, string configurationFile) @@ -37,8 +40,9 @@ public class Program { - private static readonly string ConfigurationFile = "slskd.yml"; - private static readonly string EnvironmentVariablePrefix = "SLSKD_"; + private static readonly string AppName = "slskd"; + private static readonly string ConfigurationFile = $"{AppName}.yml"; + private static readonly string EnvironmentVariablePrefix = $"{AppName.ToUpperInvariant()}_"; public static Guid InvocationId { get; } = Guid.NewGuid(); public static int ProcessId { get; } = Environment.ProcessId; @@ -58,6 +62,9 @@ [CommandLineArgument('v', "version", "display version information")] private static bool ShowVersion { get; set; } + [CommandLineArgument('g', "generate-certificate", "generate X509 certificate and password for HTTPs")] + private static bool GenerateCertificate { get; set; } + private static Options Options { get; } = new Options(); private static IConfigurationRoot Configuration { get; set; } @@ -79,6 +86,12 @@ return; } + if (GenerateCertificate) + { + GenerateX509Certificate(password: Guid.NewGuid().ToString(), filename: $"{AppName}.pfx"); + return; + } + try { Configuration = new ConfigurationBuilder() @@ -178,17 +191,27 @@ } WebHost.CreateDefaultBuilder(args) + .SuppressStatusMessages(true) .ConfigureAppConfiguration((hostingContext, builder) => { builder.Sources.Clear(); builder.AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile); }) .UseSerilog() + .UseUrls() + .UseKestrel(options => + { + logger.Information($"Listening for HTTP requests at http://{IPAddress.Any}:{Options.Web.Port}/"); + options.Listen(IPAddress.Any, Options.Web.Port); + + logger.Information($"Listening for HTTPS requests at https://{IPAddress.Any}:{Options.Web.Https.Port}/"); + options.Listen(IPAddress.Any, Options.Web.Https.Port, listenOptions => + { + logger.Information($"Using randomly generated self-signed certificate"); + listenOptions.UseHttps(X509.Generate(subject: AppName, password: Guid.NewGuid().ToString(), X509KeyStorageFlags.MachineKeySet)); + }); + }) .UseStartup() - //.UseUrls( - // $"http://+:{options.Web.Port}", - // $"https://+:{options.Web.Https.Port}") - .UseUrls($"http://+:{Options.Web.Port}") .Build() .Run(); } @@ -202,6 +225,18 @@ } } + public static void GenerateX509Certificate(string password, string filename) + { + Console.WriteLine("Generating X509 certificate..."); + filename = Path.Combine(AppContext.BaseDirectory, $"{AppName}.pfx"); + + var cert = X509.Generate(subject: AppName, password, X509KeyStorageFlags.Exportable); + File.WriteAllBytes(filename, cert.Export(X509ContentType.Pkcs12, password)); + + Console.WriteLine($"Password: {password}"); + Console.WriteLine($"Certificate exported to {filename}"); + } + public static void PrintCommandLineArguments(IEnumerable