Skip to content

Seems like RSA key is being treated as DSA key when specifying filename in connection #1839

Closed as not planned

Description

@trewest

OS: Fedora 33
Python: 3.9.2
paramiko==2.7.2

Code:

username = 'user'
hostname = 'localhost'
p_key = '/home/user/.ssh/id_rsa'

client = paramiko.SSHClient()
paramiko.util.log_to_file('/home/user/paramiko.log')
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, key_filename=p_key)

Error message:

Traceback (most recent call last):
  File "/home/user/test.py", line 10, in <module>
    client.connect(hostname, username=username, key_filename=p_key)
  File "/usr/lib/python3.9/site-packages/paramiko/client.py", line 435, in connect
    self._auth(
  File "/usr/lib/python3.9/site-packages/paramiko/client.py", line 680, in _auth
    self._transport.auth_publickey(username, key)
  File "/usr/lib/python3.9/site-packages/paramiko/transport.py", line 1580, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
  File "/usr/lib/python3.9/site-packages/paramiko/auth_handler.py", line 236, in wait_for_response
    raise e
  File "/usr/lib/python3.9/site-packages/paramiko/transport.py", line 2109, in run
    handler(self.auth_handler, m)
  File "/usr/lib/python3.9/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
    sig = self.private_key.sign_ssh_data(blob)
  File "/usr/lib/python3.9/site-packages/paramiko/dsskey.py", line 108, in sign_ssh_data
    key = dsa.DSAPrivateNumbers(
  File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 250, in private_key
    return backend.load_dsa_private_numbers(self)
  File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 852, in load_dsa_private_numbers
    dsa._check_dsa_private_numbers(numbers)
  File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 147, in _check_dsa_private_numbers
    _check_dsa_parameters(parameters)
  File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 139, 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

paramiko.log contents:

DEB [20210415-13:22:01.677] thr=1   paramiko.transport: starting thread (client mode): 0x56767ee0
DEB [20210415-13:22:01.677] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.7.2
DEB [20210415-13:22:01.690] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-OpenSSH_8.4
INF [20210415-13:22:01.690] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_8.4)
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: kex algos:['curve25519-sha256', '[email protected]', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512'] server key:['rsa-sha2-512', 'rsa-sha2-256', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['[email protected]', '[email protected]', 'aes256-ctr', '[email protected]', 'aes128-ctr'] server encrypt:['[email protected]', '[email protected]', 'aes256-ctr', '[email protected]', 'aes128-ctr'] client mac:['[email protected]', '[email protected]', '[email protected]', '[email protected]', 'hmac-sha2-256', 'hmac-sha1', '[email protected]', 'hmac-sha2-512'] server mac:['[email protected]', '[email protected]', '[email protected]', '[email protected]', 'hmac-sha2-256', 'hmac-sha1', '[email protected]', 'hmac-sha2-512'] client compress:['none', '[email protected]'] server compress:['none', '[email protected]'] client lang:[''] server lang:[''] kex follows?False
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: Kex agreed: [email protected]
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: HostKey agreed: ssh-ed25519
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20210415-13:22:01.692] thr=1   paramiko.transport: Compression agreed: none
DEB [20210415-13:22:01.702] thr=1   paramiko.transport: kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEB [20210415-13:22:01.703] thr=1   paramiko.transport: Switch to new keys ...
DEB [20210415-13:22:01.703] thr=2   paramiko.transport: Adding ssh-ed25519 host key for localhost: b'cd253a4f02710db1cd015a0987e55808'
DEB [20210415-13:22:01.731] thr=2   paramiko.transport: Trying discovered key b'f04a122496865c894b5df6d1eb07bceb' in /home/user/.ssh/id_rsa
DEB [20210415-13:22:01.731] thr=1   paramiko.transport: userauth is OK
INF [20210415-13:22:01.750] thr=1   paramiko.transport: Authentication (publickey) failed.
DEB [20210415-13:22:01.751] thr=2   paramiko.transport: Trying discovered key b'd97f81e10ee73700ec5c1484ce07af50' in /home/user/.ssh/id_rsa
DEB [20210415-13:22:01.751] thr=1   paramiko.transport: userauth is OK
ERR [20210415-13:22:01.752] thr=1   paramiko.transport: Unknown exception: q must be exactly 160, 224, or 256 bits long
ERR [20210415-13:22:01.753] thr=1   paramiko.transport: Traceback (most recent call last):
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib/python3.9/site-packages/paramiko/transport.py", line 2109, in run
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     handler(self.auth_handler, m)
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib/python3.9/site-packages/paramiko/auth_handler.py", line 298, in _parse_service_accept
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     sig = self.private_key.sign_ssh_data(blob)
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib/python3.9/site-packages/paramiko/dsskey.py", line 108, in sign_ssh_data
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     key = dsa.DSAPrivateNumbers(
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 250, in private_key
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     return backend.load_dsa_private_numbers(self)
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 852, in load_dsa_private_numbers
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     dsa._check_dsa_private_numbers(numbers)
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 147, in _check_dsa_private_numbers
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     _check_dsa_parameters(parameters)
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:   File "/usr/lib64/python3.9/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 139, in _check_dsa_parameters
ERR [20210415-13:22:01.754] thr=1   paramiko.transport:     raise ValueError("q must be exactly 160, 224, or 256 bits long")
ERR [20210415-13:22:01.754] thr=1   paramiko.transport: ValueError: q must be exactly 160, 224, or 256 bits long
ERR [20210415-13:22:01.754] thr=1   paramiko.transport: 

Activity

  1. ploxiln commented on Apr 17, 2021

    @ploxiln
    Contributor

    I think this would be fixed by #1606

  2. trewest commented on Apr 17, 2021

    @trewest
    Author

    Just tried with that branch

    (.venv) λ  ~ pip freeze | grep paramiko
    paramiko-ng==2.8.4
    

    The error message now changed to:

    Traceback (most recent call last):
      File "/home/user/test.py", line 10, in <module>  
        client.connect(hostname, username=username, key_filename=p_key)
      File "/home/user/.venv/lib64/python3.9/site-packages/paramiko/client.py", line 368, in connect  
        self._auth(
      File "/home/user/.venv/lib64/python3.9/site-packages/paramiko/client.py", line 691, in _auth  
        raise saved_exception
      File "/home/user/.venv/lib64/python3.9/site-packages/paramiko/client.py", line 648, in _auth  
        self._transport.auth_publickey(username, key))
      File "/home/user/.venv/lib64/python3.9/site-packages/paramiko/transport.py", line 1446, in auth_publickey  
        return self.auth_handler.wait_for_response(my_event)
      File "/home/user/.venv/lib64/python3.9/site-packages/paramiko/auth_handler.py", line 223, in wait_for_response  
        raise e
    paramiko.ssh_exception.AuthenticationException: Authentication failed.
    

    paramiko.log:

    DEB [20210417-16:14:52.085] thr=1   paramiko.transport: starting thread (client mode): 0x3518c3a0
    DEB [20210417-16:14:52.085] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.8.4
    DEB [20210417-16:14:52.091] thr=1   paramiko.transport: Remote version/idstring: 'SSH-2.0-OpenSSH_8.4'
    INF [20210417-16:14:52.091] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_8.4)
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: kex follows? False
    kex algos: ['curve25519-sha256', '[email protected]', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512']
    server key: ['rsa-sha2-512', 'rsa-sha2-256', 'ecdsa-sha2-nistp256', 'ssh-ed25519']
    client encrypt: ['[email protected]', '[email protected]', 'aes256-ctr', '[email protected]', 'aes128-ctr']
    server encrypt: ['[email protected]', '[email protected]', 'aes256-ctr', '[email protected]', 'aes128-ctr']
    client mac: ['[email protected]', '[email protected]', '[email protected]', '[email protected]', 'hmac-sha2-256', 'hmac-sha1', '[email protected]', 'hmac-sha2-512']
    server mac: ['[email protected]', '[email protected]', '[email protected]', '[email protected]', 'hmac-sha2-256', 'hmac-sha1', '[email protected]', 'hmac-sha2-512']
    client lang: b''
    server lang: b''
    client compress: ['none', '[email protected]']
    server compress: ['none', '[email protected]']
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: Kex agreed: [email protected]
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: HostKey agreed: ssh-ed25519
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
    DEB [20210417-16:14:52.093] thr=1   paramiko.transport: Compression agreed: none
    DEB [20210417-16:14:52.097] thr=1   paramiko.transport: kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
    DEB [20210417-16:14:52.097] thr=1   paramiko.transport: Switch to new keys ...
    DEB [20210417-16:14:52.098] thr=2   paramiko.transport: Adding ssh-ed25519 host key for localhost: 371fab5f7fe300cf2cd117a4d8a023c4
    DEB [20210417-16:14:52.098] thr=1   paramiko.transport: userauth is OK
    DEB [20210417-16:14:52.107] thr=1   paramiko.transport: Authentication type (none) not permitted.
    DEB [20210417-16:14:52.107] thr=1   paramiko.transport: Allowed methods: ['publickey', 'gssapi-keyex', 'gssapi-with-mic', 'password']
    DEB [20210417-16:14:52.114] thr=2   paramiko.transport: Trying discovered key 45ea54ce1ce06d450125fdbcb9813d58 in /home/user/.ssh/id_rsa  
    DEB [20210417-16:14:52.114] thr=1   paramiko.transport: userauth is OK
    INF [20210417-16:14:52.126] thr=1   paramiko.transport: Authentication (publickey) failed.
    DEB [20210417-16:14:52.133] thr=2   paramiko.transport: Trying discovered key 45ea54ce1ce06d450125fdbcb9813d58 in /home/user/.ssh/id_rsa  
    DEB [20210417-16:14:52.133] thr=1   paramiko.transport: userauth is OK
    INF [20210417-16:14:52.145] thr=1   paramiko.transport: Authentication (publickey) failed.
    

    Just to prove that my SSH key is in authorized_keys:

    (.venv) λ  ~ ssh -i .ssh/id_rsa user@localhost echo Hello!
    Hello!
    
  3. added this to the Next bugfix milestone on Dec 23, 2021
  4. acerv commented on Apr 22, 2022

    @acerv

    Hi! I'm having the exact same issue in 2.10.3. Is it going to be fixed?

  5. bskinn commented on Apr 22, 2022

    @bskinn
    Contributor

    Hi, @acerv -- it will be, currently planned for the next bugfix release (as per the issue milestone). No specific timeline is available for that release, thought, as it depends on the balance of tasks for @bitprophet among paramiko, fabric and invoke, and what fits in his weekly chunk of OSS time.

  6. linw1995 commented on May 5, 2022

    @linw1995

    Hi! I'm having the exact same issue in 2.10.4. Is it going to be fixed? Or there are other ways to resolve this problem temporarily?

  7. bskinn commented on May 5, 2022

    @bskinn
    Contributor

    Yes, this will be fixed in an upcoming release, see my comment above. It doesn't appear that any workarounds have been identified yet; you can follow this issue to see if any get posted here.

  8. KennethWilke commented on May 5, 2022

    @KennethWilke

    Found my way here from running into the same issue. This comment mentioned that they didn't run into this issue specifically with paramiko==2.8.1:
    fabric/fabric#2182 (comment)

    This seemed to be the simplest way to resolve the problem temporarily for my use case. @linw1995 maybe this could work for you as well while we're waiting for a released fix.

  9. lgblkb commented on May 20, 2022

    @lgblkb

    This helped me:

    Fabric = 2.7.0
    paramiko = 2.6.0
    
  10. ksrangarajan commented on May 24, 2022

    @ksrangarajan

    Will be glad to know if there is any timeline available for fix to be made available?

  11. bskinn commented on May 24, 2022

    @bskinn
    Contributor

    @ksrangarajan -- It's in line for a bugfix release, but unfortunately there's no specific timeline. It depends on bitprophet's prioritization between paramiko, invoke and fabric, and on what he can fit in his weekly OSS time allotment.

  12. bskinn commented on May 24, 2022

    @bskinn
    Contributor

    For easy crossref: Per @ploxiln, this may be fixed by #1606, which in turn was aimed at fixing #1590.

  13. markios commented on Jul 20, 2022

    @markios

    Is there any update on this, or when it will likely be fixed?

  14. bskinn commented on Jul 22, 2022

    @bskinn
    Contributor

    Yeah, sorry for the delay on things key-related. Right now bitprophet's first priority is a new release that drops Python 2 and older versions of Python 3. I believe a ~complete rework of the key-handing and auth code (see, e.g., #387) is the first priority after that. No idea on a specific timeline, unfortunately -- like I noted above, his weekly OSS time is split among paramiko, invoke and fabric, and so everything is mutually prioritized among the three projects.

  15. jackcasey-visier commented on Sep 27, 2022

    @jackcasey-visier

    A workaround we've been using, is to load the key directly and pass it to the SSHClient connection via pkey.

    As an example:

    Getting key:

    def get_key(key_path: str = "./id_rsa") -> paramiko.PKey:
        with open(key_path) as f:
            return paramiko.RSAKey.from_private_key(f)
    

    Usage:

    key = get_key()
    client.connect(hostname, username=username, pkey=key)
    

    I haven't dug into the source much, but it appears this is bypassing the problematic key type detection step, and enforces use of RSA directly :)

  16. huaiguoguo commented on Jun 4, 2023

    @huaiguoguo

    I'm having the same issue in 2.12.0, when it will be fixed ?
    @bskinn @alex @bitprophet

  17. bskinn commented on Jun 5, 2023

    @bskinn
    Contributor

    I'm having the same issue in 2.12.0, when it will be fixed ?

    Work is in progress on this, in stages. Please check out the experimental new features described in the 3.2.0 paramiko CHANGELOG (including the new OpenSSHAuthStrategy implementation in Fabric 3.1.0).

    I'm not deeply familiar with what the current features enable, so I can't say for sure whether they fix this use case. We would greatly appreciate any investigation you might be able to do and report back here, though.

  18. jshen28 commented on Oct 24, 2024

    @jshen28

    I was hit by this stack trace because a wrong user name is given.

  19. cjwatson commented on Aug 13, 2025

    @cjwatson

    I happened to notice that support for DSA keys has been (reasonably!) dropped in Paramiko 4.0.0. Does that effectively close this bug, since it isn't going to mishandle RSA keys as DSA any more when it no longer has DSA support? Or is there some deeper issue as yet unaddressed?

  20. bskinn commented on Aug 13, 2025

    @bskinn
    Contributor

    Thanks for surfacing this, @cjwatson!

    Seems likely so; I'll close now, and we can revisit if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions