Skip to content
Open
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 @@ -2,6 +2,8 @@
using System;
using System.Security.Cryptography;

using Renci.SshNet.Common;

namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
Expand Down Expand Up @@ -35,7 +37,7 @@ public RsaDigitalSignature(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName)
/// <inheritdoc/>
public override bool Verify(byte[] input, byte[] signature)
{
return _key.RSA.VerifyData(input, signature, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
return _key.RSA.VerifyData(input, signature.Pad((_key.RSA.KeySize + 7) / 8), _hashAlgorithmName, RSASignaturePadding.Pkcs1);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;


using Renci.SshNet.Common;
using Renci.SshNet.Security;
using Renci.SshNet.Security.Cryptography;
using Renci.SshNet.Tests.Common;
Expand Down Expand Up @@ -126,11 +128,13 @@ public void Sha512_SignAndVerify()
}

[TestMethod]
public void SignatureDoesNotTruncateLeadingZeroes()
public void SignatureLeadingZeroes()
{
// When signing, do not shorten signatures by truncating leading zeroes.
// Shortened/unpadded signatures are accepted by some servers but not all.
// A regression test for https://github.com/sshnet/SSH.NET/issues/1388

byte[] data = { 0x41, 0xdb, 0xf3, 0x09, 0x56 };
byte[] data = { 0xe3, 0x1a, 0xb1, 0x33, 0x30 };

RsaKey rsaKey = GetRsaKey();

Expand All @@ -140,25 +144,33 @@ public void SignatureDoesNotTruncateLeadingZeroes()

CollectionAssert.AreEqual(new byte[]
{
0x00, 0xa8, 0x35, 0x24, 0xc8, 0xc1, 0x94, 0x97, 0xc3, 0xdf, 0x94, 0x32, 0x62, 0xf2, 0x12, 0x57,
0x49, 0x22, 0x7e, 0x52,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0x5d, 0x15, 0x08, 0x7e, 0xba, 0x82, 0x29, 0x3c, 0x44, 0xb9, 0x33, 0xe4, 0x51, 0x8a,
0xaa, 0x5a, 0x45, 0xb1, 0xbd, 0x4f, 0xce, 0x98, 0x3b, 0x79, 0x9c, 0x33, 0xb5, 0x2c, 0xa0, 0xa1,
0x15, 0x0b, 0x15, 0x47, 0xb6, 0x31, 0x94, 0xb0, 0xdb, 0xdb, 0x53, 0x22, 0x1f, 0xe6, 0x96, 0x88,
0x41, 0xbc, 0x6f, 0x02, 0x1b, 0xa2, 0x7e, 0xc7, 0x66, 0x06, 0x4b, 0x14, 0xc5, 0xd7, 0x90, 0x68,
0x87, 0x9e, 0xbe, 0x69, 0xaa, 0xd9, 0x5a, 0xda, 0x34, 0x11, 0x82, 0x24, 0x8c, 0x36, 0x3f, 0x44,
0x43, 0x19, 0x74, 0xe7, 0x17, 0xaf, 0x35, 0xca, 0x9c, 0xdd, 0x4c, 0xd2, 0x1a, 0xdf, 0x02, 0x26,
0xe7, 0x42, 0xbb, 0x3c, 0x21, 0xf4, 0xb2, 0xf8, 0x50, 0x7d, 0x98, 0x9c, 0x5d, 0xfa, 0x1e, 0x01,
0x2a, 0xb3, 0x6a, 0x11, 0xf8, 0x3d, 0x0b, 0xf1, 0xd0, 0x75, 0x46, 0xd3, 0x21, 0xa9, 0x10, 0xb7,
}, signedBytes);

Assert.IsTrue(digitalSignature.Verify(data, signedBytes));

// We are lenient in the opposite direction, allowing unpadded signatures.
Assert.IsTrue(digitalSignature.Verify(data, signedBytes.TrimLeadingZeros()));

signedBytes[2] = 0x00;

Assert.IsFalse(digitalSignature.Verify(data, signedBytes));
Assert.IsFalse(digitalSignature.Verify(data, signedBytes.TrimLeadingZeros()));
}

private static RsaKey GetRsaKey()
Expand Down