Skip to content

An invalid username causes a misleading error: q must be exactly 160, 224, or 256 bits long #2244

Open

Description

@poncovka

Issue
Connection to an SSH server with a specific key file and an invalid username fails with a confusing error:
ValueError: q must be exactly 160, 224, or 256 bits long

Expected behaviour
Raise an authentication exception or other more appropriate error.

Reproducer

import paramiko
import subprocess
import logging
import sys

# Use an available server and user.
hostname="SERVER"
username="USER"

logging.basicConfig(
    stream=sys.stderr,
    level=logging.DEBUG
)

# Generate a temporary key for this reproducer.
subprocess.run([
    "ssh-keygen",
    "-f", "/tmp/ssh_key",
    "-N", "",
    "-q"
], text=True)

# Copy the temporary key to the server if you want
# to test the connection with a valid username.
subprocess.run([
    "ssh-copy-id",
    "-i", "/tmp/ssh_key",
    "-o", "UserKnownHostsFile=/dev/null",
    "-o", "StrictHostKeyChecking=no",
    f"{username}@{hostname}"
], text=True)

# Create an SSH connection for a wrong user.
with paramiko.client.SSHClient() as ssh_client:
    ssh_client.set_missing_host_key_policy(
        paramiko.AutoAddPolicy()
    )
    ssh_client.connect(
        hostname=hostname,
        username="invalid",
        key_filename="/tmp/ssh_key"
    )

Output

> python3 reproducer.py 
/tmp/ssh_key already exists.
Overwrite (y/n)? y
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/tmp/ssh_key.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added 'SERVER' (ED25519) to the list of known hosts.

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -o 'UserKnownHostsFile=/dev/null' -o 'StrictHostKeyChecking=no' 'USER@SERVER'"
and check to make sure that only the key(s) you wanted were added.

DEBUG:paramiko.transport:starting thread (client mode): 0x2be9e450
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.12.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_8.0
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_8.0)
DEBUG:paramiko.transport:=== Key exchange possibilities ===
DEBUG: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, diffie-hellman-group-exchange-sha1, diffie-hellman-group14-sha1
DEBUG:paramiko.transport:server key: rsa-sha2-512, rsa-sha2-256, ssh-rsa, ecdsa-sha2-nistp256, ssh-ed25519
DEBUG:paramiko.transport:client encrypt: [email protected], [email protected], aes256-ctr, aes256-cbc, [email protected], aes128-ctr, aes128-cbc
DEBUG:paramiko.transport:server encrypt: [email protected], [email protected], aes256-ctr, aes256-cbc, [email protected], aes128-ctr, aes128-cbc
DEBUG:paramiko.transport:client mac: [email protected], [email protected], [email protected], [email protected], hmac-sha2-256, hmac-sha1, [email protected], hmac-sha2-512
DEBUG:paramiko.transport:server mac: [email protected], [email protected], [email protected], [email protected], hmac-sha2-256, hmac-sha1, [email protected], hmac-sha2-512
DEBUG:paramiko.transport:client compress: none, [email protected]
DEBUG:paramiko.transport:server compress: none, [email protected]
DEBUG:paramiko.transport:client lang: <none>
DEBUG:paramiko.transport:server lang: <none>
DEBUG:paramiko.transport:kex follows: False
DEBUG:paramiko.transport:=== Key exchange agreements ===
DEBUG:paramiko.transport:Kex: [email protected]
DEBUG:paramiko.transport:HostKey: ssh-ed25519
DEBUG:paramiko.transport:Cipher: aes128-ctr
DEBUG:paramiko.transport:MAC: hmac-sha2-256
DEBUG:paramiko.transport:Compression: none
DEBUG:paramiko.transport:=== End of kex handshake ===
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Got EXT_INFO: {'server-sig-algs': b'ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521'}
DEBUG:paramiko.transport:Adding ssh-ed25519 host key for SERVER: b'0b8b69827a27f06cc3fa23cc2f86ffb3'
DEBUG:paramiko.transport:Trying discovered key b'10aada03286895197fdd8fb5e8ebd963' in /tmp/ssh_key
DEBUG:paramiko.transport:userauth is OK
DEBUG:paramiko.transport:Finalizing pubkey algorithm for key of type 'ssh-rsa'
DEBUG:paramiko.transport:Our pubkey algorithm list: ['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa']
DEBUG:paramiko.transport:Server-side algorithm list: ['ssh-ed25519', 'ssh-rsa', 'rsa-sha2-256', 'rsa-sha2-512', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521']
DEBUG:paramiko.transport:Agreed upon 'rsa-sha2-512' pubkey algorithm
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:Trying discovered key b'ece7e5895fc340e21bb00581b5e19efc' in /tmp/ssh_key
DEBUG:paramiko.transport:userauth is OK
ERROR:paramiko.transport:Unknown exception: q must be exactly 160, 224, or 256 bits long
ERROR:paramiko.transport:Traceback (most recent call last):
ERROR:paramiko.transport:  File "/usr/lib/python3.11/site-packages/paramiko/transport.py", line 2164, in run
ERROR:paramiko.transport:    handler(self.auth_handler, m)
ERROR:paramiko.transport:  File "/usr/lib/python3.11/site-packages/paramiko/auth_handler.py", line 395, in _parse_service_accept
ERROR:paramiko.transport:    sig = self.private_key.sign_ssh_data(blob, algorithm)
ERROR:paramiko.transport:          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR:paramiko.transport:  File "/usr/lib/python3.11/site-packages/paramiko/dsskey.py", line 117, in sign_ssh_data
ERROR:paramiko.transport:    ).private_key(backend=default_backend())
ERROR:paramiko.transport:      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR:paramiko.transport:  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 242, in private_key
ERROR:paramiko.transport:    return ossl.load_dsa_private_numbers(self)
ERROR:paramiko.transport:           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR:paramiko.transport:  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 822, in load_dsa_private_numbers
ERROR:paramiko.transport:    dsa._check_dsa_private_numbers(numbers)
ERROR:paramiko.transport:  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 283, in _check_dsa_private_numbers
ERROR:paramiko.transport:    _check_dsa_parameters(parameters)
ERROR:paramiko.transport:  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 275, in _check_dsa_parameters
ERROR:paramiko.transport:    raise ValueError("q must be exactly 160, 224, or 256 bits long")
ERROR:paramiko.transport:ValueError: q must be exactly 160, 224, or 256 bits long
ERROR:paramiko.transport:
Traceback (most recent call last):
  File "~/reproducer.py", line 31, in <module>
    ssh_client.connect(
  File "/usr/lib/python3.11/site-packages/paramiko/client.py", line 450, in connect
    self._auth(
  File "/usr/lib/python3.11/site-packages/paramiko/client.py", line 697, in _auth
    self._transport.auth_publickey(username, key)
  File "/usr/lib/python3.11/site-packages/paramiko/transport.py", line 1635, in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/paramiko/auth_handler.py", line 245, in wait_for_response
    raise e
  File "/usr/lib/python3.11/site-packages/paramiko/transport.py", line 2164, in run
    handler(self.auth_handler, m)
  File "/usr/lib/python3.11/site-packages/paramiko/auth_handler.py", line 395, in _parse_service_accept
    sig = self.private_key.sign_ssh_data(blob, algorithm)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/paramiko/dsskey.py", line 117, in sign_ssh_data
    ).private_key(backend=default_backend())
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 242, in private_key
    return ossl.load_dsa_private_numbers(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 822, in load_dsa_private_numbers
    dsa._check_dsa_private_numbers(numbers)
  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 283, in _check_dsa_private_numbers
    _check_dsa_parameters(parameters)
  File "/usr/lib64/python3.11/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py", line 275, 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

Activity

  1. changed the title [-]An invalid username causes a misleading error: `ValueError: q must be exactly 160, 224, or 256 bits long`[/-] [+]An invalid username causes a misleading error: `q must be exactly 160, 224, or 256 bits long`[/+] on May 23, 2023
  2. bskinn commented on May 24, 2023

    @bskinn
    Contributor

    Flagging for consideration in the key/auth overhaul of #387.

    @poncovka, issues where users have seen a similar "q must be exactly 160, 224, or 256 bits long" error have come up before. Can you tell, are any of them describing the same situation you've encountered?

  3. poncovka commented on May 24, 2023

    @poncovka
    Author

    @bskinn I did find these issues when I was investigating the error and tried some of the workarounds (like downgrading paramiko and generating a key of a different type). However, all these issues are about the keys. My key was fine, I just messed up the username. I believe that the root of my issue is the same and it is related to the exit strategy of the paramiko library, but the outcome is a little different.

  4. bskinn commented on May 30, 2023

    @bskinn
    Contributor

    <nod>, that was the impression I had.

    I don't think it will have helped with this, but if it's not too much trouble can you try adapting your reproducer to use the new, experimental auth_strategy machinery added in v3.2, see what happens? It would be good to poke at it in as many ways as we can think of.

    Separately, it sounds like the recommended course of action for multiple uses of SSHClient is going to become 'Please use Fabric's Connection instead' (see paramiko 3.2 CHANGELOG; scroll down to red EXPERIMENTAL notice). Any chance this would suit your needs?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions