diff --git a/Renci.SshClient/Renci.SshNet/Security/Cryptography/CipherDigitalSignature.cs b/Renci.SshClient/Renci.SshNet/Security/Cryptography/CipherDigitalSignature.cs index ea88a02b..c5e5d67b 100644 --- a/Renci.SshClient/Renci.SshNet/Security/Cryptography/CipherDigitalSignature.cs +++ b/Renci.SshClient/Renci.SshNet/Security/Cryptography/CipherDigitalSignature.cs @@ -38,29 +38,18 @@ namespace Renci.SshNet.Security.Cryptography /// 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(); } /// diff --git a/Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs b/Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs index 5fc94f83..72c117d0 100644 --- a/Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs +++ b/Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/RsaCipher.cs @@ -39,7 +39,17 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers /// 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); } /// @@ -47,9 +57,24 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers /// /// The data. /// + /// Thrown when decrypted block type is not supported. 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)