Infinite loop in SshClient.Connect()

Feb 7, 2012 at 9:45 AM

Creating a SshClient using the key below, the call to Connect() never returns.

RsaCipher.Transform enters an infinite loop in

                while (random <= BigInteger.One || random >= max)
                    random = BigInteger.Random(bitLength);

because max<=BigInteger.One.

I don't know for sure that my key is valid (it was generated using code from the SharpSsh project which is no longer maintained).  Regardless, it might be worth checking for this case and throwing an exception instead?

 

The following key demonstrates the problem

-----BEGIN RSA PRIVATE KEY-----
MIICWAIBAAKBgM7vTmh5PM+k+PsAiaULV1I1smqfA4w/uMVzuYi5xXml4B6Klwmh
Hi7ajeitwZZP6qAmM8Uum44X88LJVLqBQhGjYXD3tUEFhC4UhMP897BQ/i6Sxxg5
pLv6p5rd4tvI2vp6rqlly8qhc0jHyw+EZYw5G68wVT0kKiDKSGENrwinAgMBAAEC
gYBWLnf7LrLSz6QZ9QleQTDmvhJGMV9q44byRNX6SVQi66BBV/q7Oey2GrOLOgIr
tbDDbeWVs2l5biRI9UxiOD7pFOnTtkq7DmXKVVeeuI8PiziKuKzVlUv1TT0zttbJ
ANZGS/9y9fbxFsEQyCUAYcjlEDAHabtmMrbMO/tewF5EcQJA/uBjvRaLkpTUOrvd
wzrj5SySifjcH7dvmbKTFXIrxriniiKCXkClN/FjwghPszbNDcezH5xxpWsfiR3l
tIAmiwJAz9jRWZ+6VZtvu0PJd35BjEB0iTG/TGRkSOwq17SAcJeS8RgG8FcGS1eK
tO54i6HSzKlSW4ruN2W9+oFwtvLF1QJAB9geGba06vj7oycfByAr3WTGycpH9A3w
OFm/nxxZbo9GUMEJJB93NkaOnG0nDsvz+aWYxLNK6B1o5g5ugGSlSwJAeDauNwkU
S8lAIW5+ivdiUA1DmcpFG5kBC9BUozgKBZUVbw53jPC4ekEiv60h2Zj6BFd2g+3i
A/Z7n/TXzWizFQJAmSvqesQzR36f4bAUMU5BjRlyy8TrwyzSSpLFeKSLUqzJjVb4
Obyam/jXV3HQ7t4yZfUxWw7gkjH4jjOgM5vk/Q==
-----END RSA PRIVATE KEY-----

Coordinator
Feb 7, 2012 at 12:41 PM

Hi,

 

Can you look at max and bitLength values when it loops and tell what they are?

 

Thanks,

Oleg

 

Feb 7, 2012 at 1:06 PM

bitLength is 1023

I wasn't sure what members of max were interesting so excuse me pasting them all in below...

I can't say for sure that the key in my OP is valid.

If it helps, you should be able to reproduce the problem by copying the key into a file:

    PrivateKeyFile pkf = new PrivateKeyFile(path_to_OP_key);
    SshClient sshClient = new SshClient("1.2.3.4", 20, "root", pkf);

 

-        max    {-34454762064519193810127628633590185169672625850943461464662295025209915281688726189824826409850010158399450044810292541657604139675348811983282501161442867135670545713433190913267552952042250333109131637380129090334251632340997231637478943197803551705062491200560542554695859835195084847309138793208321472346}    Renci.SshNet.Common.BigInteger
-        _data    {uint[32]}    uint[]
        [0]    4065392474    uint
        [1]    3744839582    uint
        [2]    2864896981    uint
        [3]    3336851663    uint
        [4]    4034632307    uint
        [5]    2360817716    uint
        [6]    2587112798    uint
        [7]    92623190    uint
        [8]    488912677    uint
        [9]    89679138    uint
        [10]    3888536388    uint
        [11]    30502200    uint
        [12]    50876335    uint
        [13]    3521870652    uint
        [14]    1254029947    uint
        [15]    1553895176    uint
        [16]    1165934062    uint
        [17]    205338283    uint
        [18]    3513020904    uint
        [19]    1608109114    uint
        [20]    1047113749    uint
        [21]    628234066    uint
        [22]    4133413329    uint
        [23]    534869352    uint
        [24]    1178240602    uint
        [25]    982271607    uint
        [26]    4235444295    uint
        [27]    3394082144    uint
        [28]    1525983405    uint
        [29]    117768054    uint
        [30]    2260938843    uint
        [31]    823177623    uint
        _sign    -1    short
        BitLength    1023    int
        IsEven    The runtime has refused to evaluate the expression at this time.    bool
        IsOne    The runtime has refused to evaluate the expression at this time.    bool
        IsPowerOfTwo    The runtime has refused to evaluate the expression at this time.    bool
        IsZero    The runtime has refused to evaluate the expression at this time.    bool
        Sign    The runtime has refused to evaluate the expression at this time.    int
+        Static members       

Coordinator
Feb 7, 2012 at 1:31 PM

hmm,

It looks like I have a problem since max should never be negative number, that explains an infinite loop.

I assume you using RSA key.

If so what is the Modulus, it should be positive too, if not, there is probably an problem with the key.

 

You can send it to me privately if you like to look into this problem if you like.

 

Hope it helps,

Thanks,

Oleg

Feb 7, 2012 at 3:52 PM

Yes, I am using a RSA key.

The modulus is negative however so I guess this implies a problem with the key.

I'm satisfied that the key is corrupt and I shouldn't be able to connect using it.  I still wonder whether it'd be worth making ssh.net proof against this by checking that (max > BigInteger.One) before entering the loop in Transform().

I'm also happy to run other tests if you like - just let me know.

Simon