Repository navigation
Description
Are you using paramiko as a client or server?
Client
What feature(s) aren't working right?
Keys/auth
What version(s) of paramiko are you using?
3.5.0
What version(s) of Python are you using?
3.12.7
What operating system and version are you using?
MacOS Sequoia 15.2 Developer Beta 1
If you're connecting as a client, which SSH server are you connecting to?
OpenSSH
If you're using paramiko as part of another tool, which tool/version?
fabric 3.2.2, fabrictestbed-extensions 1.7.3
Expected/desired behavior
I am attempting to use the fabrictestbed_extensions FablibManager to create and submit a slice to my Fabric Testbed Project. In doing so, it uses paramiko to connect to the Bastion OpenSSH server using client.connect
fabrictestbed_extensions/fablib/fablib.py: FablibManager.probe_bastion_host(self) -> bool
Lines 1670 - 1677
result = bastion_client.connect(
hostname=bastion_host,
username=bastion_username,
key_filename=bastion_key_path,
passphrase=bastion_key_passphrase,
allow_agent=False,
look_for_keys=False,
)In paramiko/auth.py, this fails on line 735 with ValueError: q must be exactly 160, 224, or 256 bits long
This happens because the call to SSHClient._key_from_filepath on line 731 does not raise an SSHException in DSAKey._decode_key
The expected behavior would be for DSSKey._decode_key to raise an SSHException if the key data contains invalid values, e.g. q not having the proper bit length. This would skip the authentication attempt with the invalid key.
Actual behavior
DSSKey._decode_key successfully parses the OPENSSH RSA key file without raising an SSHException, causing an attempt to be made to authenticate with a DSSKey with invalid values/parameter
How to reproduce
- Create an RSA Private Key stored in an OPENSSH key file (starting and ending with appropriate BEGIN/END OPENSSH PRIVATE KEY tags) without a passphrase.
- Run the following code or similar
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
host = "SSH Host"
username = "SSH Username"
key_path = "Path to key file"
client.connect(
hostname=host,
username=username,
key_filename=key_path,
passphrase=None,
allow_agent=False,
look_for_keys=False
)- Get the following Exception
ValueError: q must be exactly 160, 224, or 256 bits long
Anything else?
Is there a reason to attempt to load the key file as an RSAKey, DSSKey, ECDSAKey, and a Ed25519Key after its successfully loaded the key and authenticated with it?
Beyond that, don't pem and openssh key files contain a value which defines the type of key they contain? If so, why determine the key type by trial and error decoding?
Activity
antonio-fr commentedon Sep 30, 2025
I'm facing the exact issue on a machine.
After spending some time digging into this issue, I found that it comes from a processing behavior of Paramiko. In case the RSA auth failed, it fallbacks to loading the key as a DSA, which has not a correct format (for a DSA key).
This issue is a duplicate of this one : #2048
I don't know why Paramiko doesn't detect the key type and uses trial and error, to me this is the source if the issue.
A way to solve this is load the key using the key objects, and providing this key as the pkey parameter. It is a solution in case your key is always the same type. Else it requires to perform key type detection on your side and calling the right from_private_key_file object method.
client.connect(hostname=host, username="toto", key_filename="/path/to/private.key")Becomes
I agree with you that in this case it throws a ValueError despite being a SSH connection error, and this can confuse the caller about what happens upon the SSH connection. That also confuses the user as the error message is not really what the problem is about : reports a wrong key parameters being loaded, and actually this is a connection/auth issue. That points to an issue in the private key being loaded, but in fact this is just a connection/auth issue.