Bug 1632902

Summary: openssl-1:1.1.0i-1 incorrectly identifies ~/.ssh/id_rsa as invalid
Product: [Fedora] Fedora Reporter: Jordan Pisaniello <jpisaniello>
Component: opensshAssignee: Jakub Jelen <jjelen>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 28CC: alapshin, dwalsh, fedora, jfch, jjelen, jorton, jpisaniello, lkundrak, mattias.ellert, olle, plautrba, tmraz
Target Milestone: ---Keywords: Reopened
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: openssh-7.9p1-1.fc29 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 1638260 (view as bug list) Environment:
Last Closed: 2018-11-04 06:50:34 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Key causing invalid format error none

Description Jordan Pisaniello 2018-09-25 19:28:08 UTC
Description of problem:

After updating openssl and its dependencies to:
openssl-1:1.1.0i-1.fc28.x86_64
openssl-devel-1:1.1.0i-1.fc28.x86_64
openssl-libs-1:1.1.0i-1.fc28.x86_64
RSA ssh keys (Proc-Type: 4,ENCRYPTED; DEK-Info: AES-128-CBC) are incorrectly identified as being in an invalid format


Version-Release number of selected component (if applicable):
openssl-1:1.1.0i-1.fc28.x86_64

How reproducible:
Try connecting to known host that uses your desired key after updating

Steps to Reproduce:
1. ssh user@host
2. "Load key "/home/user/.ssh/id_rsa": invalid format

Actual results:


Expected results:


Additional info:

Downgrading related packages to:
openssl-1.1.0h-3.fc28.x86_64                                                                       
openssl-devel-1.1.0h-3.fc28.x86_64
openssl-libs-1.1.0h-3.fc28.x86_64

recognizes the file as being in the correct format.

Comment 1 Tomas Mraz 2018-09-26 09:50:05 UTC
Unfortunately I cannot reproduce the issue. I have an ssh RSA key with Proc-Type: 4,ENCRYPTED; DEK-Info: AES-128-CBC that works with openssl-1.1.0i just fine. Can you please reproduce the issue with some testing key that you do not use so you could attach it here?

Comment 2 Jordan Pisaniello 2018-09-26 11:50:13 UTC
(In reply to Tomas Mraz from comment #1)
> Unfortunately I cannot reproduce the issue. I have an ssh RSA key with
> Proc-Type: 4,ENCRYPTED; DEK-Info: AES-128-CBC that works with openssl-1.1.0i
> just fine. Can you please reproduce the issue with some testing key that you
> do not use so you could attach it here?

.. strangely enough, now I cannot reproduce this either. It seems after downgrading and then upgrading to: 

openssl-1.1.0h-3                                                                       
openssl-devel-1.1.0h-3
openssl-libs-1.1.0h-3

no errors about the key's formatting were given. ... I'll make sure to reinstall packages when troubleshooting next time.

Comment 3 Tomas Mraz 2018-09-26 12:16:06 UTC
I'm closing the bug then. Please reopen if you're able to reproduce ideally with a testing key you can attach to bz.

Comment 4 Andrei Lapshin 2018-09-28 19:19:18 UTC
I have same problem with my private key.

Attempt to add key into ssh agent with ssh-add ~/.ssh/id_rsa fails with error message
Error loading key "/home/alapshin/.ssh/id_rsa": invalid format

Current versions
openssl 1:1.1.0i-1.fc28
openssl-libs 1:1.1.0i-1.fc28
openssh 7.8p1-3.fc28

Downgrade to
openssh 7.8p1-2.fc28
openssh-clients 7.8p1-2.fc28
openssl 1:1.1.0h-3.fc28
openssl-libs 1:1.1.0h-3.fc28
openssl-libs 1:1.1.0h-3.fc28

Solves this problem.
Sadly I can't attach this key since it used in some places.
But key have same header as key in original bug report
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC

Additional information:
This key was created 4 years ago at minimum. File creation date indicates that it was created on 2014-12-05 but really it could be copied from other machine and created even earlier.

Comment 5 Andrei Lapshin 2018-09-28 19:49:56 UTC
As a workaround

1. Downgrade openssl to 1:1.1.0h-3
2. Change key's password using ssh-keygen -p -f ~/.ssh/id_rsa (old password could be reused)
3. Upgrade openssl to 1:1.1.0i-1

After this steps I was able to successfully add key to ssh-agent

Comment 6 Tomas Mraz 2018-10-01 07:54:31 UTC
Does/did the password contain any special (non ASCII) characters?

Comment 7 Andrei Lapshin 2018-10-01 08:00:36 UTC
No, password contains only ASCII characters.

Comment 8 Tomas Mraz 2018-10-02 07:24:47 UTC
*** Bug 1635044 has been marked as a duplicate of this bug. ***

Comment 9 Tomas Mraz 2018-10-02 07:25:48 UTC
I am reopening since the issue is real and probably not something obscure, however to be able to fix it I still need a key that is broken so I can debug the issue.

Comment 10 Andrei Lapshin 2018-10-02 08:14:25 UTC
Created attachment 1489314 [details]
Key causing invalid format error

This is a key that is causing ssh-agent to fail with `invalid format` error.

Feel free to use as necessary (since it no longer used anywhere) however I still can't provide steps to create key from scratch to reproduce this case. I tried to create new key using Fedora 20 docker image but no luck.

Comment 11 Olle Liljenzin 2018-10-02 08:30:55 UTC
My broken key also starts with:
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,

Comment 12 Tomas Mraz 2018-10-02 08:47:18 UTC
Thank you for the broken key example. This is really helpful.

Comment 13 Tomas Mraz 2018-10-02 13:15:09 UTC
I forgot, since the key is encrypted, I also need a correct passphrase for the key to be able to debug further. Hopefully it is something you can spare.

Comment 14 Andrei Lapshin 2018-10-02 14:27:33 UTC
Tomas, I sent passphrase to your email at redhat.com. Is it ok or should I send it somewhere else?

Comment 15 Tomas Mraz 2018-10-02 14:47:23 UTC
It's OK.

This is really strange bug, because using openssl rsa or openssl pkey the key can be read fine. Only the ssh-keygen fails.

Comment 16 Tomas Mraz 2018-10-02 17:03:31 UTC
So after investigation I've found this is really openssh bug that is triggered by change of openssl behavior that is present also in openssl-1.1.1.

The problem is that openssh in sshkey_parse_private_pem_fileblob() calls PEM_read_bio_PrivateKey() without supplying passphrase callback. Thus the default one is used which provides 0 length passphrase. Previously in openssl-1.1.0h the 0 length passphrase was regarded as error but now it is not and in some cases the decryption passes correctly (i.e. the padding is accidentally decrypted OK). In such case the decrypted garbage is now parsed as private key structure which is of course corrupted and so the parser reports different error.

The openssh should call PEM_read_bio_PrivateKey() in this case with non-default callback which would always return -1. So the error returned in case of encrypted PEM files will be consistent and the further processing of the file can continue.

Comment 17 Tomas Mraz 2018-10-02 17:05:58 UTC
This also explains why openssl utilites work fine with the "invalid" key file.

You can for example use command:

openssl pkey -in invalid_key -aes128 -traditional -out new_key

to re-encrypt the file which will most probably make the padding different and so the issue is not triggered with it anymore.

Comment 18 Jordan Pisaniello 2018-10-09 17:21:28 UTC
It looks like my 'workaround' for downgrading then updating the openssl package or simply just reinstalling it didn't last.

It's possible that it didn't survive a reboot somehow. I'm getting the same invalid format error message. I will play around some more and provide more information later.

Comment 19 Olle Liljenzin 2018-10-09 18:26:05 UTC
@jorp - A workaround that worked for me was to regenerate the key after downgrading openssl, using "ssh-keygen -p" with the same password as before. The updated key is likely to work also with the newer openssl. To test the key after upgrading openssl you have to unset SSH_ASKPASS and SSH_AUTH_SOCK in the environment if you are running Gnome, otherwise ssh-agent will keep the cached key in memory. I guess that is why your old key failed first after you rebooted the machine.

Comment 20 Tomas Mraz 2018-10-10 07:03:27 UTC
You can use the instructions from comment #17 as a workaround although you can also change the format of the private key file to something more modern with:

openssl pkey -in invalid_key -aes128 -out new_key

This will change the format to PKCS#8 encrypted private key file.

Comment 21 Tomas Mraz 2018-10-10 07:05:04 UTC
This does not require downgrade of the openssl and the PKCS#8 format includes HMAC of the key so the openssh issue cannot manifest with it.

Comment 22 Jakub Jelen 2018-10-11 08:25:12 UTC
This was fixed in upstream OpenSSH and will be in the next release.

Comment 23 Fedora Update System 2018-10-19 11:07:25 UTC
openssh-7.9p1-1.fc29 has been submitted as an update to Fedora 29. https://bodhi.fedoraproject.org/updates/FEDORA-2018-5be740cab4

Comment 24 Fedora Update System 2018-10-20 19:24:03 UTC
openssh-7.9p1-1.fc29 has been pushed to the Fedora 29 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2018-5be740cab4

Comment 25 Fedora Update System 2018-11-04 06:50:34 UTC
openssh-7.9p1-1.fc29 has been pushed to the Fedora 29 stable repository. If problems still persist, please make note of it in this bug report.

Comment 26 Anderson Sasaki 2019-02-07 14:40:57 UTC
*** Bug 1636706 has been marked as a duplicate of this bug. ***