mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-11 01:36:39 +00:00
Fix RSA cipher and add support for different key sizes.
This commit is contained in:
@@ -38,29 +38,18 @@ namespace Renci.SshNet.Security.Cryptography
|
||||
/// <returns></returns>
|
||||
public override bool Verify(byte[] input, byte[] signature)
|
||||
{
|
||||
var sig = this._cipher.Decrypt(signature);
|
||||
|
||||
// TODO: Ensure that only 1 or 2 types are supported
|
||||
var position = 1;
|
||||
while (position < sig.Length && sig[position] != 0)
|
||||
position++;
|
||||
position++;
|
||||
|
||||
|
||||
var sig1 = new byte[sig.Length - position];
|
||||
|
||||
Array.Copy(sig, position, sig1, 0, sig1.Length);
|
||||
var encryptedSignature = this._cipher.Decrypt(signature);
|
||||
|
||||
var hashData = this.Hash(input);
|
||||
|
||||
var expected = DerEncode(hashData);
|
||||
|
||||
if (expected.Length != sig1.Length)
|
||||
if (expected.Length != encryptedSignature.Length)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
if (expected[i] != sig1[i])
|
||||
if (expected[i] != encryptedSignature[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,17 +69,7 @@ namespace Renci.SshNet.Security.Cryptography
|
||||
// Calculate DER string
|
||||
var derEncodedHash = DerEncode(hashData);
|
||||
|
||||
// Calculate signature
|
||||
var rsaInputBlockSize = new byte[255];
|
||||
rsaInputBlockSize[0] = 0x01;
|
||||
for (int i = 1; i < rsaInputBlockSize.Length - derEncodedHash.Length - 1; i++)
|
||||
{
|
||||
rsaInputBlockSize[i] = 0xFF;
|
||||
}
|
||||
|
||||
Array.Copy(derEncodedHash, 0, rsaInputBlockSize, rsaInputBlockSize.Length - derEncodedHash.Length, derEncodedHash.Length);
|
||||
|
||||
return this._cipher.Encrypt(rsaInputBlockSize).TrimLeadingZero().ToArray();
|
||||
return this._cipher.Encrypt(derEncodedHash).TrimLeadingZero().ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -39,7 +39,17 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
|
||||
/// <returns></returns>
|
||||
public override byte[] Encrypt(byte[] data)
|
||||
{
|
||||
return this.Transform(data);
|
||||
// Calculate signature
|
||||
var paddedBlock = new byte[this._key.Modulus.BitLength / 8 - 1];
|
||||
paddedBlock[0] = 0x01;
|
||||
for (int i = 1; i < paddedBlock.Length - data.Length - 1; i++)
|
||||
{
|
||||
paddedBlock[i] = 0xFF;
|
||||
}
|
||||
|
||||
Array.Copy(data, 0, paddedBlock, paddedBlock.Length - data.Length, data.Length);
|
||||
|
||||
return this.Transform(paddedBlock);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,9 +57,24 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
|
||||
/// </summary>
|
||||
/// <param name="data">The data.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotSupportedException">Thrown when decrypted block type is not supported.</exception>
|
||||
public override byte[] Decrypt(byte[] data)
|
||||
{
|
||||
return this.Transform(data);
|
||||
var paddedBlock = this.Transform(data);
|
||||
|
||||
if (paddedBlock[0] != 1 || paddedBlock[0] != 2)
|
||||
throw new NotSupportedException("Only block type 01 or 02 are supported.");
|
||||
|
||||
var position = 1;
|
||||
while (position < paddedBlock.Length && paddedBlock[position] != 0)
|
||||
position++;
|
||||
position++;
|
||||
|
||||
var result = new byte[paddedBlock.Length - position];
|
||||
|
||||
Array.Copy(paddedBlock, position, result, 0, result.Length);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] Transform(byte[] data)
|
||||
|
||||
Reference in New Issue
Block a user