Open Styca96 opened 2 months ago
The same issue is able to reproduced by the comment which is passed to ssh-keygen. Also, the pullreq cannot fix it.
$ ssh-keygen -N '' -C "$(python -c 'import os; os.write(1, b"\x07" * 2)')" -t ecdsa -b 521 -f /dev/shm/id_ecdsa
Generating public/private ecdsa key pair.
Your identification has been saved in /dev/shm/id_ecdsa
Your public key has been saved in /dev/shm/id_ecdsa.pub
The key fingerprint is:
SHA256:7t0wCAeyd4jDr/OLmLkQaAjYt7TMEB08eLMqNdo+4X4
The key's randomart image is:
+---[ECDSA 521]---+
| .+.. |
|....* |
|o o.++. |
|+ oB.* o |
|+= oX + S |
|+.+ + = . |
|.+ . . o o |
| .++Eo . . + |
| .*+ooo.. . . |
+----[SHA256]-----+
$ ~/venv/paramiko-dev/bin/python -c 'from paramiko import ECDSAKey as K; K.from_private_key_file("/dev/shm/id_ecdsa")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 435, in from_private_key_file
key = cls(filename=filename, password=password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/ecdsakey.py", line 127, in __init__
self._from_private_key_file(filename, password)
File "/home/jun66j5/src/paramiko/paramiko/ecdsakey.py", line 283, in _from_private_key_file
data = self._read_private_key_file("EC", filename, password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 509, in _read_private_key_file
data = self._read_private_key(tag, f, password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 540, in _read_private_key
data = self._read_private_key_openssh(lines[start:end], password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 684, in _read_private_key_openssh
return _unpad_openssh(keydata)
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 74, in _unpad_openssh
raise SSHException("Invalid key")
paramiko.ssh_exception.SSHException: Invalid key
$ ssh-keygen -N '' -C "$(python -c 'import os; os.write(1, b"\x07" * 5)')" -t ed25519 -f /dev/shm/id_ed25519 Generating public/private ed25519 key pair.
Your identification has been saved in /dev/shm/id_ed25519
Your public key has been saved in /dev/shm/id_ed25519.pub
The key fingerprint is:
SHA256:XQUGIEo0c65ug+Wn35GPQnrW1e8nr/6xWBLryT5Gjz0
The key's randomart image is:
+--[ED25519 256]--+
| .= o ....o.. |
| . * . . . |
| . . . |
| . . . |
| o S .. . |
| = . .. ..o |
| . =o..o. .++o |
| ..++..+ o+BE+|
| .+..o . oO=B+|
+----[SHA256]-----+
$ ~/venv/paramiko-dev/bin/python -c 'from paramiko import Ed25519Key as K; K.from_private_key_file("/dev/shm/id_ed25519")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 435, in from_private_key_file
key = cls(filename=filename, password=password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/ed25519key.py", line 65, in __init__
signing_key = self._parse_signing_key_data(data, password)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/ed25519key.py", line 140, in _parse_signing_key_data
message = Message(_unpad_openssh(private_data))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jun66j5/src/paramiko/paramiko/pkey.py", line 74, in _unpad_openssh
raise SSHException("Invalid key")
paramiko.ssh_exception.SSHException: Invalid key
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.4.1
What version(s) of Python are you using?
3.10.12
What operating system and version are you using?
WSL on Windows 11
If you're connecting as a client, which SSH server are you connecting to?
No response
If you're using paramiko as part of another tool, which tool/version?
No response
Expected/desired behavior
Load any private file with ECDSAKey without getting an error
Actual behavior
Sometimes when I try lo load a private ECDSAKey from a file I get an error:
The problemis is in the function
_unpad_openssh
inpkey.py
module.When the
padding_length
is equal to 0, this function return an empty list that cause an Exception. I don't kwon about padding and comment so I don't kwon if it is the correct behaviour.The key is valid if we change this line:
in this one:
How to reproduce
By using this snippet of code you can see that sometimes the load of the key fail
Output:
Anything else?
System Info: $ ssh -V OpenSSH_8.9p1 Ubuntu-3ubuntu0.10, OpenSSL 3.0.2 15 Mar 2022