Fixed the HttpSigning issue for ECDSA key supplied as string. (#17459)

Co-authored-by: Aanisha Mishra <aanisha.mishra05@gmail.com>
Co-authored-by: Vikrant Balyan (vvb) <vvb@cisco.com>
Co-authored-by: Vikrant Balyan <vvb@users.noreply.github.com>
Co-authored-by: Sebastien Rosset <serosset@cisco.com>
This commit is contained in:
Ghufran Zahidi 2023-12-26 15:20:14 +05:30 committed by GitHub
parent c252216700
commit 35b47e6382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 42 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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
/// <returns>ECDSA signature</returns>
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;

View File

@ -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;

View File

@ -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;

View File

@ -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
/// <returns>ECDSA signature</returns>
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;