From 39960bbfabff651bbe2d500a4722433813fb2180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kullai=20M=C3=A1t=C3=A9?= Date: Sun, 11 Apr 2021 08:40:48 +0200 Subject: [PATCH 1/3] Fixed for Linux --- LxCommunicator.NET/Communicator/Session.cs | 28 ++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/LxCommunicator.NET/Communicator/Session.cs b/LxCommunicator.NET/Communicator/Session.cs index d34c872..ec58ed4 100644 --- a/LxCommunicator.NET/Communicator/Session.cs +++ b/LxCommunicator.NET/Communicator/Session.cs @@ -122,7 +122,25 @@ private void GetRandomSalt(int digits) { Random random = new Random(); Salt = string.Concat(Enumerable.Range(0, digits).Select(x => random.Next(16).ToString("X"))); } - + +// Copied from other project to find fix for linux + // Fix on Linux https://github.com/bcgit/bc-csharp/issues/160 + // X509Certificate2 x509; + // if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + // { + // var rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters) subjectKeyPair.Private); + // x509 = x509i.CopyWithPrivateKey(rsa); + // x509.FriendlyName = subjectName; //System.PlatformNotSupportedException: The FriendlyName value cannot be set on Unix. + // } + // else + // { + // var parms = DotNetUtilities.ToRSAParameters(subjectKeyPair.Private as RsaPrivateCrtKeyParameters); + // var rsa = RSA.Create(); + // rsa.ImportParameters(parms); + // x509 = x509i.CopyWithPrivateKey(rsa); + // } +// End copy + /// /// Converts a string in PEM format to XML format /// @@ -131,7 +149,13 @@ private void GetRandomSalt(int digits) { private string PemToXml(string pem) { return GetXmlRsaKey(pem, obj => { var publicKey = (RsaKeyParameters)obj; - return DotNetUtilities.ToRSA(publicKey, new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); + //return DotNetUtilities.ToRSA(publicKey, new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); + if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) + return DotNetUtilities.ToRSA(publicKey, new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); + var parms = DotNetUtilities.ToRSAParameters(publicKey); + var rsa = RSA.Create(); + rsa.ImportParameters(parms); + return rsa; }, rsa => rsa.ToXmlString(false)); } From 0e73390ec5f3e3169295f29d4d39d7add5628397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kullai=20M=C3=A1t=C3=A9?= Date: Sun, 11 Apr 2021 08:42:15 +0200 Subject: [PATCH 2/3] Clean Linux fix --- LxCommunicator.NET/Communicator/Session.cs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/LxCommunicator.NET/Communicator/Session.cs b/LxCommunicator.NET/Communicator/Session.cs index ec58ed4..1ea8d5e 100644 --- a/LxCommunicator.NET/Communicator/Session.cs +++ b/LxCommunicator.NET/Communicator/Session.cs @@ -123,24 +123,6 @@ private void GetRandomSalt(int digits) { Salt = string.Concat(Enumerable.Range(0, digits).Select(x => random.Next(16).ToString("X"))); } -// Copied from other project to find fix for linux - // Fix on Linux https://github.com/bcgit/bc-csharp/issues/160 - // X509Certificate2 x509; - // if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - // { - // var rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters) subjectKeyPair.Private); - // x509 = x509i.CopyWithPrivateKey(rsa); - // x509.FriendlyName = subjectName; //System.PlatformNotSupportedException: The FriendlyName value cannot be set on Unix. - // } - // else - // { - // var parms = DotNetUtilities.ToRSAParameters(subjectKeyPair.Private as RsaPrivateCrtKeyParameters); - // var rsa = RSA.Create(); - // rsa.ImportParameters(parms); - // x509 = x509i.CopyWithPrivateKey(rsa); - // } -// End copy - /// /// Converts a string in PEM format to XML format /// From 25b00fe0afe130734bacaf6eee7ed8aadbc6302b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kullai=20M=C3=A1t=C3=A9?= Date: Tue, 13 Apr 2021 08:05:14 +0200 Subject: [PATCH 3/3] Added reference to root issue --- LxCommunicator.NET/Communicator/Session.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LxCommunicator.NET/Communicator/Session.cs b/LxCommunicator.NET/Communicator/Session.cs index 1ea8d5e..b6c361d 100644 --- a/LxCommunicator.NET/Communicator/Session.cs +++ b/LxCommunicator.NET/Communicator/Session.cs @@ -122,7 +122,7 @@ private void GetRandomSalt(int digits) { Random random = new Random(); Salt = string.Concat(Enumerable.Range(0, digits).Select(x => random.Next(16).ToString("X"))); } - + /// /// Converts a string in PEM format to XML format /// @@ -131,7 +131,7 @@ private void GetRandomSalt(int digits) { private string PemToXml(string pem) { return GetXmlRsaKey(pem, obj => { var publicKey = (RsaKeyParameters)obj; - //return DotNetUtilities.ToRSA(publicKey, new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); + // CspParameters requires Windows. Details: https://github.com/bcgit/bc-csharp/issues/160 if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) return DotNetUtilities.ToRSA(publicKey, new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); var parms = DotNetUtilities.ToRSAParameters(publicKey);