Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexrobomind opened this issue May 26, 2023 · 2 comments

Comments

@alexrobomind
Copy link

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
gHvqCwf5lz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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
@willco007
Copy link
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.

@Sadaf-A
Copy link

Sadaf-A commented Dec 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants