Repository navigation
Description
Ed25519Keys with comments of certain lengths cannot be loaded as of paramiko 2.4.2. Attempts to load these keys result in an "Invalid key" exception.
If the Ed25519Key is encrypted with a passphrase, and if the length of the comment mod 16 equals 13, loading the key causes an "Invalid key" exception.
If the Ed25519Key is not encrypted with a passphrase, and if the length of the comment mod 8 equals 5, loading the key causes an "Invalid key" exception.
Reproduce code:
# generate comments of lengths 0 through 63
comments = []
for n in range ( 0, 64 ): comments .append ( '_' * n )
def path ( n ): return '/tmp/key_ed25519_%s' % n
# generate the first key with a comment of length 0
passphrase = '' # an empty passphrase is much faster
os .system ( 'rm -f %s' % path(0) )
os .system ( 'ssh-keygen -t ed25519 -N "%s" -C "%s" -f %s > /dev/null' %
( passphrase, comments[0], path(0) ) )
# generate the other keys by copying the first key and
# then changing the comment
for n in range ( 1, len ( comments ) ):
os .system ( 'cp %s %s' % ( path(0), path(n) ) )
os .system ( 'ssh-keygen -c -P "%s" -C "%s" -f %s > /dev/null' %
( passphrase, comments[n], path(n) ) )
# see which keys can be loaded
for n in range ( 0, len ( comments ) ):
s = comments[n]
try:
pkey = paramiko .ed25519key .Ed25519Key .from_private_key_file (
filename=path(n), password=passphrase )
print ( ' %11s %2d %s' % ( '+', len(s), s ) )
except paramiko .ssh_exception .SSHException as se:
print ( ' %11s %2d %s' % ( str ( se ), len(s), s ) )
Note: The above code will take significantly longer to run if a non-empty passphrase is specified. Presumably the delay is due to the passphrase being repeatedly hashed in order to reduce the effectiveness of brute force passphrase attacks.
Output of the above (when run with an empty password):
+ 0
+ 1 _
+ 2 __
+ 3 ___
+ 4 ____
Invalid key 5 _____
+ 6 ______
+ 7 _______
+ 8 ________
+ 9 _________
+ 10 __________
+ 11 ___________
+ 12 ____________
Invalid key 13 _____________
+ 14 ______________
+ 15 _______________
+ 16 ________________
+ 17 _________________
+ 18 __________________
+ 19 ___________________
+ 20 ____________________
Invalid key 21 _____________________
+ 22 ______________________
+ 23 _______________________
+ 24 ________________________
+ 25 _________________________
+ 26 __________________________
+ 27 ___________________________
+ 28 ____________________________
Invalid key 29 _____________________________
+ 30 ______________________________
+ 31 _______________________________
+ 32 ________________________________
+ 33 _________________________________
+ 34 __________________________________
+ 35 ___________________________________
+ 36 ____________________________________
Invalid key 37 _____________________________________
+ 38 ______________________________________
+ 39 _______________________________________
+ 40 ________________________________________
+ 41 _________________________________________
+ 42 __________________________________________
+ 43 ___________________________________________
+ 44 ____________________________________________
Invalid key 45 _____________________________________________
+ 46 ______________________________________________
+ 47 _______________________________________________
+ 48 ________________________________________________
+ 49 _________________________________________________
+ 50 __________________________________________________
+ 51 ___________________________________________________
+ 52 ____________________________________________________
Invalid key 53 _____________________________________________________
+ 54 ______________________________________________________
+ 55 _______________________________________________________
+ 56 ________________________________________________________
+ 57 _________________________________________________________
+ 58 __________________________________________________________
+ 59 ___________________________________________________________
+ 60 ____________________________________________________________
Invalid key 61 _____________________________________________________________
+ 62 ______________________________________________________________
Activity
lae commentedon Dec 12, 2018
I was also able to reproduce this issue while attempting to use napalm (which depends on paramiko) with an Ed25519 key and a 13 character comment.
bitprophet commentedon Jan 3, 2019
I feel like I've seen other reports of this same issue, but can't easily find 'em, so this will be it for now. Thanks for the report.
I can also say I've gotten a separate OOB report of the same issue:
If I don't get to this by EOM, I'll actually be working with that reporter at a new job & will be able to confirm fix that way; though this looks like it should be trivially reproducible.
parke commentedon Jan 3, 2019
(EOM = end of month?)
The bug is trivially reproducible, is it not? I provided code that reliably reproduces the bug.
I suspect the fix is also trivial, but I did not try to understand the code enough to see exactly where the bug is. I suspect it is an off-by-one division error, possibly on some padding or something similar.
P.S. - Also, I have since learned that you may be able to edit the comment of an ED25519 key using only a text editor. (Whereas I believe the comment of an RSA key cannot be successfully edited in a text editor.) If you can edit the comment in a text editor, then reproducing the bug should be even simpler than my above example.
metabsd commentedon Feb 13, 2019
If I have problem to load my Ed25519Key this is normal!!! I generate the key from Ubuntu 16.04 with this command
ssh-keygen -o -a 100 -t ed25519metabsd commentedon Feb 13, 2019
If I specify a passphrase it's working.
lphuberdeau commentedon Feb 28, 2019
Removing unpad() entirely from the code seems to resolve the issue for me as well as with the test script provided by @parke - and all unit tests still pass.
The other key implementations appear to have no similar mechanisms. What is unpad intended to do?
ploxiln commentedon Feb 28, 2019
If the key data is not a multiple of
168, then between 1 to157 bytes (inclusive) is added such the length is a multiple of168. These extra bytes are called padding. In this scheme, the value of these bytes is incrementing - so the first padding byte is value 1, second padding byte is value 2, etc. (Some schemes pad with just 0 bytes, but for cryptographic purposes that can combine with other stuff to be a weakness.)So, unpad() assumes that the last byte is padding, and also indicates the amount of padding, and it checks that the padding meets the rule. But what if the key data has no padding added, because it was already a multiple of 8? In this case, unpad()'s assumption is wrong, the last byte is not padding, and does not indicate the amount of padding. Instead of
raise SSHException("Invalid key")it should justreturn data- because this padding check is how it knows whether the last byte was padding or not.ploxiln commentedon Feb 28, 2019
https://coolaj86.com/articles/the-openssh-private-key-format/
fix ed25519 ssh key unpad() when padding not present (not needed)
fix ed25519 ssh key unpad() when padding not present (not needed)
ploxiln commentedon Mar 2, 2019
please check that #1400 fixes this for you
lphuberdeau commentedon Mar 4, 2019
I verified locally with #1400 and it works as expected. I also cherry-picked into 2.4 and it works there as well.
Thank you for looking into it. Hopefully the fix makes it into a release soon.
fix ed25519 ssh key unpad() when padding not present (not needed)
fix ed25519 ssh key unpad() when padding not present (not needed)
fix ed25519 ssh key unpad() when padding not present (not needed)
fix ed25519 ssh key unpad() when padding not present (not needed)
fix ed25519 ssh key unpad() when padding not present (not needed)
Changelog re #1306
Add new keys and tests proving #1306
test for new-format private key with no padding
test for new-format private key with no padding