fabric: Unknown exception: q must be exactly 160, 224, or 256 bits long

Due diligence #2140 appers to result in the same behavior

Describe the bug Attempting an SSH connection results in the following error: Unknown exception: q must be exactly 160, 224, or 256 bits long

To Reproduce Just run a pythonfile containing the code below. Right now, the private key is not encrypted and the config file in .ssh is empty.

Expected behaviour Successful SSH connection to the server

Environment

  • What version of the Python interpreter are you using? 3.7.10
  • What operating system are you using both client & server-side? Windows10 & Ubuntu
  • Are you using OpenSSH server or something else? OpenSSH (Windows10 default)
  • Which version or versions of the software are you using? fabric 2.6.0
    • Have you, or can you, try some older releases to pin down where the bug appeared? Not tried yet
  • How can the developers recreate the bug on their end? If possible, include a copy of your code, the command you used to invoke it, and the full output of your run (if applicable.)
from fabric import Connection

c = Connection(host='<IP>', user='<USER>', connect_kwargs={"key_filename": "C:\\Users\\<user>\\.ssh\\id_rsa"})
c.run('ls')

Output

Unknown exception: q must be exactly 160, 224, or 256 bits long
Traceback (most recent call last):
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\transport.py", line 2109, in run
    handler(self.auth_handler, m)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\auth_handler.py", line 298, in _parse_service_accept
    sig = self.private_key.sign_ssh_data(blob)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\dsskey.py", line 116, in sign_ssh_data
    ).private_key(backend=default_backend())
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 243, in private_key
    return backend.load_dsa_private_numbers(self)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 657, in load_dsa_private_numbers
    dsa._check_dsa_private_numbers(numbers)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 142, in _check_dsa_private_numbers
    _check_dsa_parameters(parameters)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 134, in _check_dsa_parameters
    raise ValueError("q must be exactly 160, 224, or 256 bits long")
ValueError: q must be exactly 160, 224, or 256 bits long

Traceback (most recent call last):
  File ".\target_setup.py", line 4, in <module>
    c.run('ls')
  File "<decorator-gen-3>", line 2, in run
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\fabric\connection.py", line 29, in opens
    self.open()
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\fabric\connection.py", line 636, in open
    self.client.connect(**kwargs)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\client.py", line 446, in connect
    passphrase,
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\client.py", line 680, in _auth
    self._transport.auth_publickey(username, key)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\transport.py", line 1580, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\auth_handler.py", line 236, in wait_for_response
    raise e
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\transport.py", line 2109, in run
    handler(self.auth_handler, m)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\auth_handler.py", line 298, in _parse_service_accept
    sig = self.private_key.sign_ssh_data(blob)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\paramiko\dsskey.py", line 116, in sign_ssh_data
    ).private_key(backend=default_backend())
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 243, in private_key
    return backend.load_dsa_private_numbers(self)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 657, in load_dsa_private_numbers
    dsa._check_dsa_private_numbers(numbers)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 142, in _check_dsa_private_numbers
    _check_dsa_parameters(parameters)
  File "C:\Users\mle\AppData\Local\Continuum\anaconda3\envs\blackthorn\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 134, in _check_dsa_parameters
    raise ValueError("q must be exactly 160, 224, or 256 bits long")
ValueError: q must be exactly 160, 224, or 256 bits long

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 4
  • Comments: 16

Commits related to this issue

Most upvoted comments

I, too, encountered the exact same error. It turned out that I was supplying a username that did not exist on the target machine. The error went away after I corrected my mistake.

Fabric 1.10.2

I am using robotframework-sshlibrary which uses paramiko. I have installed latest version of paramiko==3.2.0 but still the same error

@ploxiln But when I explicitly use paramiko==2.8.1 then it works fine. Any idea why so?

Hi, I have the same issue when I use paramiko==2.11.0 or paramiko==2.10.1 and I don’t have the issue if I use paramiko=2.8.1 It seems that PR #1606 addressed this issue successfully : is there any reason for not fixing that in the recent releases of paramiko ?

In my case I was trying to connect to the wrong host. 🤦‍♂️ Seems like whenever anything at all goes wrong with RSA authentication Paramiko tries to use the provided file as a DSA key instead and fails with this super unhelpful error. Definitely need better error handling there.

This worked for me:

    from paramiko import RSAKey
    # Establish an SSH connection to the instance
    with Connection(
        host=external_ip,
        user='ubuntu',
        connect_kwargs={
            'pkey': RSAKey.from_private_key_file('./generated_key.pem'),
            'look_for_keys': False,
        },
    ) as conn:
        # Update package list and install necessary packages
        conn.run('sudo apt-get update')

Also running into this issue on all versions of fabric and paramiko, including those listed above

Any update on this issue? I am also still getting this same error from the latest paramiko and have no issue if revert back to paramiko=2.8.1.

I, too, encountered the exact same error. It turned out that I was supplying a username that did not exist on the target machine. The error went away after I corrected my mistake.

Fabric 1.10.2

This did it for me, apparently, I was thinking I had to supply the username of the local machine

maybe luck related to refactoring done to support “new openssh format” private keys in paramiko-2.7.0