From 35b47e638246f48e6a0055b68ce1aba2984043ff Mon Sep 17 00:00:00 2001 From: Ghufran Zahidi <18732053+Ghufz@users.noreply.github.com> Date: Tue, 26 Dec 2023 15:20:14 +0530 Subject: [PATCH] Fixed the HttpSigning issue for ECDSA key supplied as string. (#17459) Co-authored-by: Aanisha Mishra Co-authored-by: Vikrant Balyan (vvb) Co-authored-by: Vikrant Balyan Co-authored-by: Sebastien Rosset --- .../csharp/HttpSigningConfiguration.mustache | 22 ++++++++++++++----- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 22 ++++++++++++++----- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 16 ++++++++++---- .../Client/HttpSigningConfiguration.cs | 22 ++++++++++++++----- 9 files changed, 120 insertions(+), 42 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache index 4bc8b396e09..33791a90857 100644 --- a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache @@ -102,14 +102,13 @@ namespace {{packageName}}.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -317,6 +316,10 @@ namespace {{packageName}}.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -342,8 +345,10 @@ namespace {{packageName}}.Client private string GetECDSASignature(byte[] dataToSign) { {{#net60OrLater}} - if (!File.Exists(KeyFilePath)) - throw new Exception("key file path does not exist."); + if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; @@ -443,6 +448,11 @@ namespace {{packageName}}.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-net47/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-net48/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index d98d2815297..0685800345a 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -349,8 +352,10 @@ namespace Org.OpenAPITools.Client /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - throw new Exception("key file path does not exist."); + if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; @@ -446,6 +451,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-unityWebRequest/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 7c8668488ee..256f338e181 100644 --- a/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -412,6 +415,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true; diff --git a/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index d98d2815297..0685800345a 100644 --- a/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -110,14 +110,13 @@ namespace Org.OpenAPITools.Client const string HEADER_AUTHORIZATION = "Authorization"; //Read the api key from the file - if(string.IsNullOrEmpty(this.KeyString)) + if(File.Exists(KeyFilePath)) { this.KeyString = ReadApiKeyFromFile(KeyFilePath); } - - if(string.IsNullOrEmpty(KeyString)) + else if(string.IsNullOrEmpty(KeyString)) { - throw new Exception("No API key has been provided."); + throw new Exception("No API key has been provided. Supply it using either KeyFilePath or KeyString"); } //Hash table to store singed headers @@ -325,6 +324,10 @@ namespace Org.OpenAPITools.Client private string GetRSASignature(byte[] stringToSign) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } RSA rsa = GetRSAProviderFromPemFile(KeyString, KeyPassPhrase); if (SigningAlgorithm == "RSASSA-PSS") { @@ -349,8 +352,10 @@ namespace Org.OpenAPITools.Client /// ECDSA signature private string GetECDSASignature(byte[] dataToSign) { - if (!File.Exists(KeyFilePath)) - throw new Exception("key file path does not exist."); + if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } var keyStr = KeyString; const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----"; @@ -446,6 +451,11 @@ namespace Org.OpenAPITools.Client private RSACryptoServiceProvider GetRSAProviderFromPemFile(string keyString, SecureString keyPassPhrase = null) { + if (string.IsNullOrEmpty(KeyString)) + { + throw new Exception("No API key has been provided."); + } + const string pempubheader = "-----BEGIN PUBLIC KEY-----"; const string pempubfooter = "-----END PUBLIC KEY-----"; bool isPrivateKeyFile = true;