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
Expand Up @@ -137,16 +137,16 @@ namespace {{packageName}}.Client
}
#else
if (parameter.Value.Count > 1)
{ // array
{ // array
foreach (var value in parameter.Value)
{
httpValues.Add(parameter.Key + "[]", value);
}
}
else
{
httpValues.Add(parameter.Key, parameter.Value[0]);
}
}
else
{
httpValues.Add(parameter.Key, parameter.Value[0]);
}
#endif
}
var uriBuilder = new UriBuilder(string.Concat(basePath, path));
Expand Down Expand Up @@ -256,7 +256,6 @@ namespace {{packageName}}.Client
var authorizationHeaderValue = string.Format("Signature keyId=\"{0}\",algorithm=\"{1}\"",
KeyId, cryptographicScheme);


if (HttpSignatureHeader.ContainsKey(HEADER_CREATED))
{
authorizationHeaderValue += string.Format(",created={0}", HttpSignatureHeader[HEADER_CREATED]);
Expand All @@ -278,7 +277,6 @@ namespace {{packageName}}.Client
private byte[] GetStringHash(string hashName, string stringToBeHashed)
{
var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);

var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
var stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
Expand All @@ -298,7 +296,6 @@ namespace {{packageName}}.Client
{
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss);
return Convert.ToBase64String(signedbytes);

}
else if (SigningAlgorithm == "PKCS1-v15")
{
Expand Down Expand Up @@ -472,16 +469,22 @@ namespace {{packageName}}.Client

//-------- read PEM encryption info. lines and extract salt -----
if (!str.ReadLine().StartsWith("Proc-Type: 4,ENCRYPTED"))
{
return null;
}
String saltline = str.ReadLine();
if (!saltline.StartsWith("DEK-Info: DES-EDE3-CBC,"))
{
return null;
}
String saltstr = saltline.Substring(saltline.IndexOf(",") + 1).Trim();
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() == ""))
{
return null;
}

//------ remaining b64 data is encrypted RSA key ----
String encryptedstr = str.ReadToEnd();
Expand All @@ -497,7 +500,9 @@ namespace {{packageName}}.Client

byte[] deskey = GetEncryptedKey(salt, keyPassPharse, 1, 2); // count=1 (for OpenSSL implementation); 2 iterations to get at least 24 bytes
if (deskey == null)
{
return null;
}

//------ Decrypt the encrypted 3des-encrypted RSA private key ------
byte[] rsakey = DecryptKey(binkey, deskey, salt); //OpenSSL uses salt value in PEM header also as 3DES IV
Expand All @@ -519,18 +524,28 @@ namespace {{packageName}}.Client
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
{
binr.ReadByte(); //advance 1 byte
}
else if (twobytes == 0x8230)
{
binr.ReadInt16(); //advance 2 bytes
}
else
{
return null;
}

twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
{
return null;
}
bt = binr.ReadByte();
if (bt != 0x00)
{
return null;
}

//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
Expand Down Expand Up @@ -575,7 +590,10 @@ namespace {{packageName}}.Client
{
return null;
}
finally { binr.Close(); }
finally
{
binr.Close();
}
}

private int GetIntegerSize(BinaryReader binr)
Expand All @@ -585,12 +603,16 @@ namespace {{packageName}}.Client
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02) //expect integer
if (bt != 0x02) //expect integer
{
return 0;
}
bt = binr.ReadByte();

if (bt == 0x81)
{
count = binr.ReadByte(); // data size in next byte
}
else if (bt == 0x82)
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
Expand All @@ -603,7 +625,8 @@ namespace {{packageName}}.Client
count = bt; // we already have the data size
}
while (binr.ReadByte() == 0x00)
{ //remove high order zeros in data
{
//remove high order zeros in data
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current);
Expand Down Expand Up @@ -636,7 +659,9 @@ namespace {{packageName}}.Client
{
// ---- Now hash consecutively for count times ------
if (j == 0)
{
result = data00; //initialize
}
else
{
Array.Copy(result, hashtarget, result.Length);
Expand Down Expand Up @@ -714,7 +739,7 @@ namespace {{packageName}}.Client
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
/* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
}
#else
if (parameter.Value.Count > 1)
{ // array
{ // array
foreach (var value in parameter.Value)
{
httpValues.Add(parameter.Key + "[]", value);
}
}
else
{
httpValues.Add(parameter.Key, parameter.Value[0]);
}
}
else
{
httpValues.Add(parameter.Key, parameter.Value[0]);
}
#endif
}
var uriBuilder = new UriBuilder(string.Concat(basePath, path));
Expand Down Expand Up @@ -256,7 +256,6 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
var authorizationHeaderValue = string.Format("Signature keyId=\"{0}\",algorithm=\"{1}\"",
KeyId, cryptographicScheme);


if (HttpSignatureHeader.ContainsKey(HEADER_CREATED))
{
authorizationHeaderValue += string.Format(",created={0}", HttpSignatureHeader[HEADER_CREATED]);
Expand All @@ -278,7 +277,6 @@ internal Dictionary<string, string> GetHttpSignedHeader(string basePath,string m
private byte[] GetStringHash(string hashName, string stringToBeHashed)
{
var hashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);

var bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
var stringHash = hashAlgorithm.ComputeHash(bytes);
return stringHash;
Expand All @@ -298,7 +296,6 @@ private string GetRSASignature(byte[] stringToSign)
{
var signedbytes = rsa.SignHash(stringToSign, HashAlgorithm, RSASignaturePadding.Pss);
return Convert.ToBase64String(signedbytes);

}
else if (SigningAlgorithm == "PKCS1-v15")
{
Expand Down Expand Up @@ -472,16 +469,22 @@ private byte[] ConvertPrivateKeyToBytes(String instr, SecureString keyPassPharse

//-------- read PEM encryption info. lines and extract salt -----
if (!str.ReadLine().StartsWith("Proc-Type: 4,ENCRYPTED"))
{
return null;
}
String saltline = str.ReadLine();
if (!saltline.StartsWith("DEK-Info: DES-EDE3-CBC,"))
{
return null;
}
String saltstr = saltline.Substring(saltline.IndexOf(",") + 1).Trim();
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() == ""))
{
return null;
}

//------ remaining b64 data is encrypted RSA key ----
String encryptedstr = str.ReadToEnd();
Expand All @@ -497,7 +500,9 @@ private byte[] ConvertPrivateKeyToBytes(String instr, SecureString keyPassPharse

byte[] deskey = GetEncryptedKey(salt, keyPassPharse, 1, 2); // count=1 (for OpenSSL implementation); 2 iterations to get at least 24 bytes
if (deskey == null)
{
return null;
}

//------ Decrypt the encrypted 3des-encrypted RSA private key ------
byte[] rsakey = DecryptKey(binkey, deskey, salt); //OpenSSL uses salt value in PEM header also as 3DES IV
Expand All @@ -519,18 +524,28 @@ private RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
{
binr.ReadByte(); //advance 1 byte
}
else if (twobytes == 0x8230)
{
binr.ReadInt16(); //advance 2 bytes
}
else
{
return null;
}

twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
{
return null;
}
bt = binr.ReadByte();
if (bt != 0x00)
{
return null;
}

//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
Expand Down Expand Up @@ -575,7 +590,10 @@ private RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
return null;
}
finally { binr.Close(); }
finally
{
binr.Close();
}
}

private int GetIntegerSize(BinaryReader binr)
Expand All @@ -585,12 +603,16 @@ private int GetIntegerSize(BinaryReader binr)
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02) //expect integer
if (bt != 0x02) //expect integer
{
return 0;
}
bt = binr.ReadByte();

if (bt == 0x81)
{
count = binr.ReadByte(); // data size in next byte
}
else if (bt == 0x82)
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
Expand All @@ -603,7 +625,8 @@ private int GetIntegerSize(BinaryReader binr)
count = bt; // we already have the data size
}
while (binr.ReadByte() == 0x00)
{ //remove high order zeros in data
{
//remove high order zeros in data
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current);
Expand Down Expand Up @@ -636,7 +659,9 @@ private byte[] GetEncryptedKey(byte[] salt, SecureString secpswd, int count, int
{
// ---- Now hash consecutively for count times ------
if (j == 0)
{
result = data00; //initialize
}
else
{
Array.Copy(result, hashtarget, result.Length);
Expand Down Expand Up @@ -714,7 +739,7 @@ private PrivateKeyType GetKeyType(string keyFilePath)
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
/* 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
Expand Down
Loading