From 8142058d88d5268592bb2e1ff0e2abd9b2a881ca Mon Sep 17 00:00:00 2001 From: Klaus Fischer <klaus.fischer@eloware.com> Date: Wed, 8 Jun 2022 13:59:10 +0200 Subject: [PATCH] Dummy JWE encryption working --- EncryptionTests/JweTest.cs | 28 ++++++++++++++++++ FitConnect/EncryptionBaseClass.cs | 49 ++++++++++++++++++++++++++++++- FitConnect/FitConnect.csproj | 1 + 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 EncryptionTests/JweTest.cs diff --git a/EncryptionTests/JweTest.cs b/EncryptionTests/JweTest.cs new file mode 100644 index 00000000..e35d310a --- /dev/null +++ b/EncryptionTests/JweTest.cs @@ -0,0 +1,28 @@ +using FitConnect; +using Microsoft.Extensions.Logging; +using NUnit.Framework; + +namespace SenderTest; + +public class JweTest { + private Sender _sender; + private ILogger<JweTest> _logger; + + + [SetUp] + public void SetUp() { + _logger = LoggerFactory.Create(cfg => cfg.AddConsole()) + .CreateLogger<JweTest>(); + + _sender = new Sender(_logger, + FitConnectEndpoints.Create(FitConnectEndpoints.EndpointType.Development)); + } + + [Test] + public void TestJwe() { + var jwe = _sender.GetTestToken(); + Assert.IsNotNull(jwe); + + _logger.LogInformation(jwe); + } +} diff --git a/FitConnect/EncryptionBaseClass.cs b/FitConnect/EncryptionBaseClass.cs index 76e7802a..d909f6c3 100644 --- a/FitConnect/EncryptionBaseClass.cs +++ b/FitConnect/EncryptionBaseClass.cs @@ -1,6 +1,9 @@ +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.Logging; +using Microsoft.IdentityModel.Tokens; namespace FitConnect; @@ -9,13 +12,57 @@ public class EncryptionBaseClass { private readonly RSA _rsa; private RSA? _publicKey; private RSA? _privateKey; + private readonly X509Certificate2? certificate; + protected EncryptionBaseClass(ILogger? logger, X509Certificate2? certificate) { _logger = logger; _rsa = RSA.Create(4096); - if (certificate != null) + if (certificate != null) { + this.certificate = certificate; ImportCertificate(certificate); + } + } + + + public string GetTestToken() { + var handler = new JwtSecurityTokenHandler(); + var token = new SecurityTokenDescriptor { + Issuer = "FitConnect", + Audience = "FitConnect", + EncryptingCredentials = + new X509EncryptingCredentials(certificate ?? + new X509Certificate2(CreateSelfSignedCertificate())), + Subject = new ClaimsIdentity(new Claim[] { + new Claim("Content", "Unencrypted content"), + }), + }; + return handler.CreateEncodedJwt(token); + } + + private X509Certificate2 CreateSelfSignedCertificate(string? exportPath = null) { + var rsa = RSA.Create(4096); + + var req = new CertificateRequest("c=DE, cn=fitconnect.de", + rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + + var cert = req.CreateSelfSigned(DateTimeOffset.Now.AddSeconds(-5), + DateTimeOffset.Now.AddYears(5)); + + if (cert.GetRSAPublicKey() == null) + throw new Exception("Certificate does not contain a public key"); + + if (cert.GetRSAPrivateKey() == null) + throw new Exception("Certificate does not contain a private key"); + + // Export the certificate to a PEM file, just for + // additional extern testing + // if (exportPath != null) { + // ExportCertificateToFile(exportPath, cert); + // } + + return cert; } diff --git a/FitConnect/FitConnect.csproj b/FitConnect/FitConnect.csproj index 69367e62..1f5958ca 100644 --- a/FitConnect/FitConnect.csproj +++ b/FitConnect/FitConnect.csproj @@ -14,6 +14,7 @@ <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" /> + <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.19.0" /> </ItemGroup> <ItemGroup> -- GitLab