[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing private key PK - Invalid key tag or value


On 7/4/22 12:08, Jan Kundrát wrote:
On pondělí 4. července 2022 12:02:29 CEST, Heiko Thiery wrote:
Can you comment why the header and footer need to be removed?

Just FYI, Michal is on a long-ish vacation, to be back next week.

In the meanwhile I dug into git history, and it looks like the header/footer was being removed since 2019 via https://github.com/CESNET/netopeer2/commit/3384ac45a54bad4420903da578a1ca3a79d1a7b8 . That commit doesn't explain why that's needed.

Have you tried removing that and checking whether it works for you?

If I read it correctly, your server code takes care of adding some headers back, the question is whether they add the correct ones as there are different ones for different key types and file formats and you just remove everything and add back what you believe should belong there.

https://github.com/CESNET/libnetconf2/blob/master/src/session_server_ssh.c#L49

The problem is that there are two "types" of PEM files, one of them is "legacy" PEM, which is just encoded ASN.1 sequence of the key elements and it is no rocket science to parse these key types, see an example here, which I generated using OpenSSH's ssh-keygen (sorry for a long link):

$ ssh-keygen -m PEM -f /tmp/test_rsa -t rsa -N ''

https://lapo.it/asn1js/#MIIG4wIBAAKCAYEA6w7G5s-PRDnPdDStEhzQaTbYWZ8XqHUtfGIxjBGtEuR5nk86wOE3yN8rz0OYouXgbK_0ZDAQqarjF_3QNauvf9bRcw1Exsxt33IDZLdiQx57d8cmuPZy8kHP4dp9LuyVQOjxIgNrmDfBhY5dPxtbk9E_j5QAran1gZ-e34SAqdZYE1PDqrSKR_VWXn7C2H11MTuYi-jct7Jyz-73cm_lvcdLzKsQ5CoTniRrSpozmvMUUSyJmEbuPFKUBAmrYXNss8J3lRkZ6327USxHJOV47CF9dDXl_C5eGE2B0FplqohMQ9BtiIBtx8pao_OYmS_lBbv3JiYgo48rXez_rfH8DBhu3w1D5GxkAaLbtmVm8j4myPEIi1nHhYWGJFXeMa-9tga0rWpZ8Dg9ihnNCMiiiUDVBJWbO9IarzsDtNQa4TusShlq06s85P8COd6QKl1f2mOIRmRxU5fAA0muYPIW95ZogTZXcpc__uFbP2LdmOZVDCZaz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0CgcAiufmOxLyaYCvEYUVuQv04gIbyTUoqI4dVtuezyW8FjOOAbX9l-LOwrCw9-VpG6fJbRUtm1xojU84SLa24_5jqlHcjeRc1qe2wPvHeMt2YldNgVR91MqcPRqUE8iT7_fC_5SUyR1zBJRV0sk6Zp42i4WnFI8EZUMo4ahKFV5rt17u_uSZbWKU7ArujLCjovICaJJX-lcyy0_Mf82hyjTDYTcxXsxccludQLg_8Jlbexw-igar_j7OZb_xIvXvr17kCgcEAzf2MyqFRfYKGHtpp9HUoRwLx90wzijx9ADp7rt9318AMPM6W_rt_ho_rIOxCXMeNBO8j-Buw2gT6A3eBvhiVb0OOo1tEml2jbc-qyURnkLyahKbbjyX9p9Ryiv2tFCeXC1c5RZ7l_HYuTRM5A8hOksGw4-OJ5CA5hRFkkFhf2rfQcjYWDGnLrzqISRpLalPjqV1r2EkoyKQTN23YJXxHqP1-G4DG9_O6yyuPycZY2UVX55bX8K5LJ-mJj7h4_A6hAoHAYnE_eW2RXFccog_BSVqzM8IG7dT2wnxjmKvPWzfNe0DaTXHY311VICWkD1jI_ZD0WtjkpFeRrSu9D7aCjGtyy-sPy95UVJFnJZk6BLbmzRkMtGdVKo1Hi5SeVq5ZHPxT_jEdIrfv-RrTDHnqUYkiXtwU-bsCdEmdAz7w-Pc4kbaEuP-Z4yPh77JqJSMM7NXmc99eC8iqjJ8lAe-dS09tFU3V4DW-hAwRlbrD1znC--WeAl0TTNa2WERtBm4ZMHgd

The other "type" is modern PKCS8 PEM file, which is now for some time default of OpenSSL and provides more information (mostly OID). This is the key you have:

https://lapo.it/asn1js/#MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDyME0v2x6BmN6uqMg972ACTdENCVMELQ0Au-6AXdcnVBCI2DQLCfKjuZgHLnKucEvWUIK9v_9VXTDDhPbWMrZpokBRgvv3mj6go0e0sfnKyRVNDOSONvDqdZIlRIKJOcYU0ueR2t2F5ZHL4xr_3UKo1p1JMwtJaRTGJC397a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rSEUNrGLFz7ixg56UqBZjig4Rb4pQsgpDWtYhaR9gkFC8jSD7AlVQI7Oj1-C6DjCiLF1gVzlfQujzVF0sE2i91dT4R3Jo52xBE9VPMZXXUqJa2UaysfGRaEINRhZK-_bIVVvyF_oWoNuxX0aJAoGBAMcM73tUL5Dn6Fh9ASqjqJF_pS7ZTAq7cPFv93kLy-VYaaD45Pf4e-W_YeqFX96epBddVNZyWlOFjpHVawy5qmB_i94LxBaOy4nxMWqowpQ0bqeTj3rj3rmUIdCdaQ4Ng_rD-VysOtn4JxQiZc5mDZ0zY4t88jAZtYqYHDgxQkyL

And it looks like the MbedTLS does not know how to parse these keys

There is a bit more about it in

https://crypto.stackexchange.com/questions/35093/why-ssh-gen-makes-difference-between-pem-and-pkcs8

For the question whether the headers matter or not:

https://stackoverflow.com/questions/20065304/differences-between-begin-rsa-private-key-and-begin-private-key

When going through the implementation, the openssl is much less strict about them but the mbedtls is quite picky and if you mix them, it will notwork. Moreover, libssh supports now in the mbedtls backend only the legacy PEM headers (with key type in) and the PKCS8 ones are rejected.

I refactored a bit the mbedtls implementaiton of key parsing in so it is able to parse the keys you provided in the previous email. Can you check if this solves your issue (indeed after fixing your key headers):

https://gitlab.com/libssh/libssh-mirror/-/merge_requests/260

I would like to give it a better look to cross check the various key types and formats against libgcrypt and openssl too.

Regards,
--
Jakub Jelen
Crypto Team, Security Engineering
Red Hat, Inc.

References:
Re: Parsing private key PK - Invalid key tag or valueAndreas Schneider <asn@xxxxxxxxxxxxxx>
Re: Parsing private key PK - Invalid key tag or valueHeiko Thiery <heiko.thiery@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org