Skip to content

Encrypted PEM keys can not be parsed from memory when not using OpenSSH backend #1047

Closed

Description

@alexrobomind

Describe the bug

In pem.c , _libssh2_pem_parse_memory does not support encrypted keys, while _libssh2_pem_parse does. The entire code section to parse encrypted PEM files is missing from _libssh2_pem_parse_memory. For non-openssh backends, libssh2 does not attempt to decode the base64 data and just forwards them, so this is likely not an issue there.

To Reproduce

Authenticating with the following example key (passphrase 'testpass') from memory will fire an 'invalid base64' error in the key handling callback.

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,D5A4738309691E79

WTkh0OaxwoYyjn0D9T4+qcAemiBFYekNsX4+aEFvKOX8XfvGzwWlJt3G3oJaF5AS
3uEeQHj8VPPMq4ACL92jU6x4rLklix0zH5fTVEZ3gt61ns+Dnjh4L3K61pqRfjsa
4o/yyC0jOrl8AE5PdBw5jbXZCZPTa8rpHsbAJAODr5LvtZ4c9WXuYXhW1WmklyQE
aPr+LGT5eidIw4E2Tl4Joo1Dn65IkxwLoNe60PytuuZJGva4eoVhaCA4olSPVgsf
AeD7Sg1Uqoq2k3GK8+jo1sFslhrAYxo2Wjcy1SwE2F7+MZTPYKcrvRp6Z47JoWOo
ml36f3mqY17CEtDcTPoZ39J32b1UYRStGFbDMGGqukLGjM1nL3cJ6ozhVaxXsXwt
/pSuo3ycC1lJuGJwb7+xi0X/Sl0lMxgwYhmbTUtdhj3mnUEjVFpalRbkvt1IFQZe
LRpurMzAk2w4BrR/hg7L1r1j3LYHpBGZgjFNZiaW6kt3Gy314su26Gpi+10GQEAp
HXpfFifMLwM+UJkUcATtjN1vcqxbiAaxeCwmYQweIyrmyfsMd9vRHh6Sv/4PnQfd
gHvqCwf5lz6WXosmFqlTIMxAxrHmUGLlqD3l4O8oz9iAAjAGpyj0ovScIDQSNYyJ
4bfMtloyttmr4TItGMAObmSiNNr9qp26s02WbVHYJWt0ZXGkLx1MPofZtz6srFes
Pl/aBoFzVb5aH/Al/DLBosOfUaJ0BJpnrrUhIQ4MS3BWo2b+yl8TEsnYYZ5tq4fP
OnHnrTH1wBSVhRVMxEg0re8CBat04GlH1G9Bc1DbV6UKi3i1Rxhb0A05C+epN1ic
Tm5W39r0ekAFnNbp/skPtte18FJPdCT4EXW6tP7Ex1ExvcojYrLNzt9ZzgeDNAdF
sM8z0MWDo/lTL9vlw5K2N9Y73+SQ37wIW4TBlxktKg05eFZw/3NIfomVYRKXR8Mt
RHv9U7hG1o3lGdkMNPTzSOwUHfeRbDL3sIgZB4gGRhM8HsNtTxVYj8g4SNRtoTRK
qn5KHaz0EpuI6ECbgJt8xM5gVrjK8OTWJiz0x2xpD6fXHiMzhYNy6HaooZ2zvkZy
Y5LOmwkYgCf5Jl2xgJ6NWmrm2RuDcJMpA08iiSucRxennMVJ4TtYeJINkFJ9a4F7
s5utEnJ4rKvJfVCWLMUV7KFGQka+8kxLZRg9/LW97fZAFZ1HmElp/kxPAt2UWsjM
jw0Fyy9fwGymRHmNLjTXMB6BnrtUT3GjjR9Yzl0sWRCIuyE23S+LaqwmVbcrtHHJ
U+03kRUaFAQ2Dvo3l8okSc46HSouy+LqOW/k/gQReono+8ZML8GEoRZ5lWNdL4Qw
Q3xI+fEsQoyTPI5VMN+jzIiUyP6fz7ykYFB6aQntDgSmBaTjSxKnet6PMsN14yoG
aB4siibQxg61FFNxR/eg8d44XZUVrQ91rqch48yHmOkJRWzTOhg6MVAbcONtXBVw
2H5ENc61ZlnKqLGgekIsvtOhmP3wdln1UvkwWvBbt8f/bSEkcm2AAUNscPG8cQGz
TxbJOr1B1BvMKM+VXSQ6eOUrNblFlagl9d7qj+rJUo3OgLYFz47c9w==
-----END RSA PRIVATE KEY-----

Expected behavior

I would expect all keys that can be parsed from file to also be parseable from memory.

Version (please complete the following information):

  • OS and version: Windows 10
  • libssh2 version: master
  • crypto backend and version: WinCNG

Activity

  1. willco007 commented on May 26, 2023

    @willco007
    Member

    At first glance it seems like _libssh2_pem_parse should just be a wrapper to read the file buffer into memory and then call _libssh2_pem_parse_memory. The current function body of _libssh2_pem_parse should be moved into _libssh2_pem_parse_memory to handle parsing pem files with PWs. Seems like a straight forward solution unless I'm missing something.

  2. Sadaf-A commented on Dec 25, 2023

    @Sadaf-A

    Hey @vszakats I would like to have a go at this if you could please assign me

  3. LarsNordin-LNdata commented on Nov 26, 2025

    @LarsNordin-LNdata
    Contributor

    I worked on this and found that _libssh2_pem_parse_memory() is only used from os400qc3.c and wincng.c. I can run on windows and updated pem.c to support encrypted key and also made _libssh2_pem_parse() read the keyfile and call _libssh2_pem_parse_memory().

    During testing I discovered that WinCNG doesn't support the above keyfile (I made a new keyfile with AES encryption). When using WinCNG the LIBSSH2_MD5_ENABLE and LIBSSH2_MD5_PEM_ENABLE is not set, I modified wincng.c to include those (otherwise the LIBSSH2_MD5 will be unset in crypto_config.h). To get all md5 functions from wincng.c I included the wincng.h file in pem.c

    2 new error are added in pem.c, "Unable to decrypt PEM, unsupported algoritm" and "Unable to decrypt PEM, MD5 not enabled"

  4. willco007 commented on Apr 6, 2026

    @willco007
    Member

    Should be working with 141b035

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