Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{>partial_header}}

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
Expand Down Expand Up @@ -82,7 +84,7 @@ namespace {{packageName}}.Client
/// <param name="method">HTTP method</param>
/// <param name="path">Path</param>
/// <param name="requestOptions">Request options</param>
/// <returns></returns>
/// <returns>Http signed headers</returns>
internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string method, string path, RequestOptions requestOptions)
{
const string HEADER_REQUEST_TARGET = "(request-target)";
Expand Down Expand Up @@ -133,7 +135,7 @@ namespace {{packageName}}.Client
}
else
{
httpValues.Add(HttpUtility.UrlEncode(parameter.Key), parameter.Value[0]);
httpValues.Add(HttpUtility.UrlEncode(parameter.Key), parameter.Value[0]);
}
#else
if (parameter.Value.Count > 1)
Expand Down Expand Up @@ -178,7 +180,6 @@ namespace {{packageName}}.Client
throw new Exception(string.Format("{0} not supported", HashAlgorithm));
}


foreach (var header in HttpSigningHeader)
{
if (header.Equals(HEADER_REQUEST_TARGET))
Expand Down Expand Up @@ -236,7 +237,6 @@ namespace {{packageName}}.Client
foreach (var keyVal in HttpSignatureHeader)
{
headerValuesList.Add(string.Format("{0}: {1}", keyVal.Key, keyVal.Value));

}
//Concatenate headers value separated by new line
var headerValuesString = string.Join("\n", headerValuesList);
Expand All @@ -252,7 +252,11 @@ namespace {{packageName}}.Client
{
headerSignatureStr = GetECDSASignature(signatureStringHash);
}
var cryptographicScheme = "hs2019";
else
{
throw new Exception(string.Format("Private key type {0} not supported", keyType));
}
const string cryptographicScheme = "hs2019";
var authorizationHeaderValue = string.Format("Signature keyId=\"{0}\",algorithm=\"{1}\"",
KeyId, cryptographicScheme);

Expand All @@ -268,9 +272,7 @@ namespace {{packageName}}.Client

authorizationHeaderValue += string.Format(",headers=\"{0}\",signature=\"{1}\"",
headersKeysString, headerSignatureStr);

HttpSignedRequestHeader.Add(HEADER_AUTHORIZATION, authorizationHeaderValue);

return HttpSignedRequestHeader;
}

Expand Down Expand Up @@ -302,23 +304,26 @@ namespace {{packageName}}.Client
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(signedbytes);
}
return string.Empty;
else
{
return string.Empty;
}
}

/// <summary>
/// Gets the ECDSA signature
/// </summary>
/// <param name="dataToSign"></param>
/// <returns></returns>
/// <returns>ECDSA signature</returns>
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-----";
const string ecKeyHeader = "-----BEGIN EC PRIVATE KEY-----";
const string ecKeyFooter = "-----END EC PRIVATE KEY-----";
var keyStr = File.ReadAllText(KeyFilePath);
var ecKeyBase64String = keyStr.Replace(ecKeyHeader, "").Replace(ecKeyFooter, "").Trim();
var keyBytes = System.Convert.FromBase64String(ecKeyBase64String);
Expand Down Expand Up @@ -355,10 +360,9 @@ namespace {{packageName}}.Client
#else
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
#endif

}

private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
{
var derBytes = new List<byte>();
byte derLength = 68; //default length for ECDSA code signing bit 0x44
Expand Down Expand Up @@ -481,7 +485,7 @@ namespace {{packageName}}.Client
byte[] salt = new byte[saltstr.Length / 2];
for (int i = 0; i < salt.Length; i++)
salt[i] = Convert.ToByte(saltstr.Substring(i * 2, 2), 16);
if (!(str.ReadLine() == ""))
if (str.ReadLine() != "")
{
return null;
}
Expand Down Expand Up @@ -512,7 +516,7 @@ namespace {{packageName}}.Client

private RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
byte[] bytesModulus, bytesE, bytesD, bytesP, bytesQ, bytesDP, bytesDQ, bytesIQ;

// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey);
Expand Down Expand Up @@ -549,40 +553,40 @@ namespace {{packageName}}.Client

//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
bytesModulus = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
bytesE = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
bytesD = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
bytesP = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
bytesQ = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
bytesDP = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
bytesDQ = binr.ReadBytes(elems);

elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
bytesIQ = binr.ReadBytes(elems);

// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSAparams.Modulus = bytesModulus;
RSAparams.Exponent = bytesE;
RSAparams.D = bytesD;
RSAparams.P = bytesP;
RSAparams.Q = bytesQ;
RSAparams.DP = bytesDP;
RSAparams.DQ = bytesDQ;
RSAparams.InverseQ = bytesIQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
Expand Down Expand Up @@ -637,7 +641,7 @@ namespace {{packageName}}.Client
private byte[] GetEncryptedKey(byte[] salt, SecureString secpswd, int count, int miter)
{
IntPtr unmanagedPswd = IntPtr.Zero;
int HASHLENGTH = 16; //MD5 bytes
const int HASHLENGTH = 16; //MD5 bytes
byte[] keymaterial = new byte[HASHLENGTH * miter]; //to store concatenated Mi hashed results

byte[] psbytes = new byte[secpswd.Length];
Expand Down Expand Up @@ -670,7 +674,9 @@ namespace {{packageName}}.Client
}

for (int i = 0; i < count; i++)
{
result = md5.ComputeHash(result);
}
Array.Copy(result, 0, keymaterial, j * HASHLENGTH, result.Length); //concatenate to keymaterial
}
byte[] deskey = new byte[24];
Expand Down Expand Up @@ -708,47 +714,45 @@ namespace {{packageName}}.Client
/// Detect the key type from the pem file.
/// </summary>
/// <param name="keyFilePath">key file path in pem format</param>
/// <returns></returns>
/// <returns>Private Key Type</returns>
private PrivateKeyType GetKeyType(string keyFilePath)
{
if (!File.Exists(keyFilePath))
{
throw new Exception("Key file path does not exist.");
}

var ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
var ecPrivateKeyFooter = "END EC PRIVATE KEY";
var rsaPrivateKeyHeader = "BEGIN RSA PRIVATE KEY";
var rsaPrivateFooter = "END RSA PRIVATE KEY";
const string ecPrivateKeyHeader = "BEGIN EC PRIVATE KEY";
const string ecPrivateKeyFooter = "END EC PRIVATE KEY";
const string rsaPrivateKeyHeader = "BEGIN RSA PRIVATE KEY";
const string rsaPrivateFooter = "END RSA PRIVATE KEY";
//var pkcs8Header = "BEGIN PRIVATE KEY";
//var pkcs8Footer = "END PRIVATE KEY";
var keyType = PrivateKeyType.None;
PrivateKeyType keyType;
var key = File.ReadAllLines(keyFilePath);

if (key[0].ToString().Contains(rsaPrivateKeyHeader) &&
if (key[0].Contains(rsaPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(rsaPrivateFooter))
{
keyType = PrivateKeyType.RSA;
}
else if (key[0].ToString().Contains(ecPrivateKeyHeader) &&
else if (key[0].Contains(ecPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(ecPrivateKeyFooter))
{
keyType = PrivateKeyType.ECDSA;
}
else if (key[0].ToString().Contains(ecPrivateKeyHeader) &&
else if (key[0].Contains(ecPrivateKeyHeader) &&
key[key.Length - 1].ToString().Contains(ecPrivateKeyFooter))
{

/* this type of key can hold many type different types of private key, but here due lack of pem header
Considering this as EC key
*/
// this type of key can hold many type different types of private key, but here due lack of pem header
// Considering this as EC key
//TODO :- update the key based on oid
keyType = PrivateKeyType.ECDSA;
}
else
{
throw new Exception("Either the key is invalid or key is not supported");

}
return keyType;
}
Expand Down
Loading