diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/HttpSigningConfiguration.mustache index 7062dce507e..2e99b3c9b53 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/HttpSigningConfiguration.mustache @@ -272,10 +272,9 @@ namespace {{packageName}}.{{clientPackage}} /// private string GetECDSASignature(byte[] dataToSign) { + {{#net60OrLater}} if (!File.Exists(KeyFilePath)) - { throw new Exception("key file path does not exist."); - } var ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; var ecKeyFooter = "-----END EC PRIVATE KEY-----"; @@ -284,7 +283,6 @@ namespace {{packageName}}.{{clientPackage}} var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); -#if (NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0) var byteCount = 0; if (KeyPassPhrase != null) { @@ -305,18 +303,17 @@ namespace {{packageName}}.{{clientPackage}} } } else - { ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount); - } + var signedBytes = ecdsa.SignHash(dataToSign); var derBytes = ConvertToECDSAANS1Format(signedBytes); var signedString = System.Convert.ToBase64String(derBytes); return signedString; -#else + {{/net60OrLater}} + {{^net60OrLater}} throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above"); -#endif - + {{/net60OrLater}} } private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index f9b8fbe7c6c..48b372c987c 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -279,9 +279,7 @@ namespace Org.OpenAPITools.Client private string GetECDSASignature(byte[] dataToSign) { if (!File.Exists(KeyFilePath)) - { throw new Exception("key file path does not exist."); - } var ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; var ecKeyFooter = "-----END EC PRIVATE KEY-----"; @@ -290,7 +288,6 @@ namespace Org.OpenAPITools.Client var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); -#if (NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0) var byteCount = 0; if (KeyPassPhrase != null) { @@ -311,18 +308,13 @@ namespace Org.OpenAPITools.Client } } else - { ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount); - } + var signedBytes = ecdsa.SignHash(dataToSign); var derBytes = ConvertToECDSAANS1Format(signedBytes); var signedString = System.Convert.ToBase64String(derBytes); return signedString; -#else - throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above"); -#endif - } private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 3db983d395b..47e416a621e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -277,9 +277,7 @@ namespace Org.OpenAPITools.Client private string GetECDSASignature(byte[] dataToSign) { if (!File.Exists(KeyFilePath)) - { throw new Exception("key file path does not exist."); - } var ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; var ecKeyFooter = "-----END EC PRIVATE KEY-----"; @@ -288,7 +286,6 @@ namespace Org.OpenAPITools.Client var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); var ecdsa = ECDsa.Create(); -#if (NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0) var byteCount = 0; if (KeyPassPhrase != null) { @@ -309,18 +306,13 @@ namespace Org.OpenAPITools.Client } } else - { ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount); - } + var signedBytes = ecdsa.SignHash(dataToSign); var derBytes = ConvertToECDSAANS1Format(signedBytes); var signedString = System.Convert.ToBase64String(derBytes); return signedString; -#else - throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above"); -#endif - } private byte[] ConvertToECDSAANS1Format(byte[] signedBytes) diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 3db983d395b..53f435c05f9 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -276,51 +276,7 @@ namespace Org.OpenAPITools.Client /// private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - { - throw new Exception("key file path does not exist."); - } - - var ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; - var ecKeyFooter = "-----END EC PRIVATE KEY-----"; - var keyStr = File.ReadAllText(KeyFilePath); - var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim(); - var keyBytes = System.Convert.FromBase64String(ecKeyBase64String); - var ecdsa = ECDsa.Create(); - -#if (NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0) - var byteCount = 0; - if (KeyPassPhrase != null) - { - IntPtr unmanagedString = IntPtr.Zero; - try - { - // convert secure string to byte array - unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(KeyPassPhrase); - - string ptrToStringUni = Marshal.PtrToStringUni(unmanagedString) ?? throw new NullReferenceException(); - - ecdsa.ImportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(ptrToStringUni), keyBytes, out byteCount); - } - finally - { - if (unmanagedString != IntPtr.Zero) - Marshal.ZeroFreeBSTR(unmanagedString); - } - } - else - { - ecdsa.ImportPkcs8PrivateKey(keyBytes, out byteCount); - } - var signedBytes = ecdsa.SignHash(dataToSign); - var derBytes = ConvertToECDSAANS1Format(signedBytes); - var signedString = System.Convert.ToBase64String(derBytes); - - return signedString; -#else throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above"); -#endif - } private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)