You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
libssh2_userauth_publickey_fromfile_ex not working on Windows #1517
In Windows when I try to use libssh2_userauth_publickey_fromfile_ex to authorize a connection to a WSL Ubuntu server, I get the following error message.
It is showing your key file isn't being loaded, but there is no additional information as to where/why the failure occurs. It does look like WinCNG supports rsa-sha2-512 key types, so that shouldn't be the issue. That's about all I can tell you however.
After setting up this env locally with curl tests, it seems my WINE version (6.0.2) is missing some crypto features, and thus getting Failure establishing ssh session: -8, Unable to exchange encryption keys. The same I got with libssh2 tests. Meaning I can't replicate it locally.
After extending tests to Windows, the solution became obvious:
libssh2 with the WinCNG backend doesn't support the OpenSSH
aka RFC4716 private key format. This is the default generated by ssh-keygen for some time now.
To make it work with WinCNG the PEM format is necessary, with -m PEM.
Removed the bug label and this is probably rather an enhancement
aka missing feature.
I'm guessing it's an encrypted PEM key. It might need libssh2 explicitly
built with -DLIBSSH2_MD5_ENABLE -DLIBSSH2_MD5_PEM_ENABLE (CPPFLAGS)
and/or an encyption method understood by WinCNG possibly.
The curl test suite uses unencrypted PEM RSA keys, those do work.
ECDSA with passphrase [libssh2] 0.448838 Failure Event: -34 - Passphrase-protected ECDSA private key files are > unsupported
ECDSA without passphrase [libssh2] 0.561452 Failure Event: -16 - Unable to initialize private key from file
@vszakats Looking closer, mbedtls supports ecdsa keys in the openssh format and that is all. I implemented full support for all key types using the new format in the openssl backend, so that is what you should probably be looking at for guidance.
For example of how this works, _libssh2_rsa_new_private attempts to first parse the old pem format using the openssl backend, if that fails it will then attempt to parse the new format using _libssh2_rsa_new_openssh_private. That call uses a generic openssh pem parser _libssh2_openssh_pem_parse (you can also use _libssh2_openssh_pem_parse_memory) which all backends should use to parse the new pem format.
After the data is parsed and decrypted you then need to use the specific backend API to create the key. In the openssl backend that call is gen_publickey_from_rsa_openssh_priv_data but the specifics vary quite a bit based on backends. I'd be happy to give you guidance if you want to try to extend key parsing to other backends.
Lastly, looking at the WinCNG pem parsing, there are several locations where it can fail and they all return a -1 error so nothing of use bubbles up to the user.
Thanks @willco007. That's beyond my C skills and free time. I'm not using mbedTLS, or WinCNG. (Also have a bottomless TODO list with other curl (and some libssh2) issues.)
At this point I wouldn't advise WinCNG for serious uses, because of the unfixed flakiness issue.
No question all of these could be improved quite a bit.
You'd have a better experience if you used a fully supported backend, like OpenSSL for example. :) As you're finding out, the WinCNG support is limited at best.
I quite like the fact that I don't have to distribute an additional DLL with WinCNG. I guess OpenSSL is not linked statically. libssh2_userauth_publickey_fromfile_ex is the first issue I came across with it. All the other stuff seem to work fine.
Which are the "fully supported" backends?
I second OpenSSL, with special mention of its forks. LibreSSL has smaller footprint for example. Recent wolfSSL should also work, it uses pretty much the same codepath as OpenSSL. All can be linked statically. (Can't comment on licensing.)
As said above WinCNG may not be reliable, depending on unknown factors. It certainly is consistently flaky in CI.
You'll need to build the crypto backend and zlib statically first.
Then point ZLIB_LIBRARY to the static zlib.lib and OPENSSL_CRYPTO_LIBRARY to the static crypto.lib. Setting OPENSSL_USE_STATIC_LIBS=ON may also work.
Do I need the -DLIBSSH2_MD5_ENABLE -DLIBSSH2_MD5_PEM_ENABLE flags? What are they used for?
No. Unless you specifically want support for this insecure algo and insecure/obsolete PEM encryption method. These options are on their way out. You most likely don't want them.
During my work with issue 1047 I found that without MD5 the whole decryption part in _libssh2_pem_parse() is removed (I added the error-message). The reading of PEM files is done different in libssh2, openssl.c is not using _libssh2_pem_parse(), wuth wincng.c is
if (method) {
#if LIBSSH2_MD5_PEM
/* Set up decryption */
......
#else
_libssh2_error(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED,
"Unable to decrypt PEM, MD5 not enabled");
ret = -1;
goto out;
#endif
This patch possibly fixes this, though I have not made tests yet: #2279
edit: as for MD5 in libssh2_pem_parse(), that is how the legacy PEM
format does the encryption. For this ECDSA case the format is OpenSSH,
which does it securely and through a different function. (To make this
even more confusing there is a third, PKCS8 format, which is only supported
by libssh2 for OS/400 via local code and relies on the crypto backends
elsewhere.)
Activity
willco007 commentedon Jan 23, 2025
#define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19. Can you check the logs to see what the exact error message is?pyscripter commentedon Jan 23, 2025
How do I do that?
willco007 commentedon Jan 23, 2025
Enable tracing with a trace callback.
pyscripter commentedon Jan 23, 2025
Will do. Please, note that the documentation has a wrong signature.
pyscripter commentedon Jan 23, 2025
Tried, but I got no output. Then I noticed this:
I will have to rebuild the libraries... This will take some time.
pyscripter commentedon Jan 23, 2025
Here is the trace output: (condenced and masked)
willco007 commentedon Jan 23, 2025
It is showing your key file isn't being loaded, but there is no additional information as to where/why the failure occurs. It does look like WinCNG supports rsa-sha2-512 key types, so that shouldn't be the issue. That's about all I can tell you however.
pyscripter commentedon Jan 23, 2025
@willco007
Thank you.
Interestingly, the error is just the same if I supply a non-existent private key file name:
Does this give any clue? If on the other hand, I supply a missing public key the error message states so.
willco007 commentedon Jan 23, 2025
Unfortunately no; it pretty much returns the same error regardless of where the key read failure is at.
libssh2_trace.3: Update prototype
vszakats commentedon Mar 21, 2025
I wonder if this is causing the test failures we see in curl with the WinCNG build of libssh2 via vcpkg
libssh2[core], e.g.:https://github.com/curl/curl/actions/runs/13861554395/job/38791104386#step:15:1
details
Full job run with other similar failures:
curl-libssh2-wincng.log.txt
Also reproduced with the MSYS2 libssh2-wincng package:
curl/curl#16781
https://github.com/curl/curl/actions/runs/13990239834/job/39172667787
vszakats commentedon Mar 21, 2025
After setting up this env locally with curl tests, it seems my WINE version (6.0.2) is missing some crypto features, and thus getting
Failure establishing ssh session: -8, Unable to exchange encryption keys. The same I got with libssh2 tests. Meaning I can't replicate it locally.edit: Turns out one can just upgrade WINE :) Re-running with 9.0 did not change the outcome much:
It's also curious this doesn't happen in libssh2 CI. Though we have just one job testing this, on an older Windows runner:
https://ci.appveyor.com/project/libssh2org/libssh2/builds/51737350/job/e8eo9bui5di0pj24
I guess what may help is adding manual
printf()s throughout the codepath to see where it fails and how.vszakats commentedon Mar 21, 2025
After extending tests to Windows, the solution became obvious:
libssh2 with the WinCNG backend doesn't support the OpenSSH
aka RFC4716 private key format. This is the default generated by
ssh-keygenfor some time now.To make it work with WinCNG the PEM format is necessary, with
-m PEM.Removed the bug label and this is probably rather an enhancement
aka missing feature.
19 remaining items
pyscripter commentedon Mar 21, 2025
[libssh2] 0.092627 Userauth: Publickey authentication successfulHurray!
What about the rsa? Should I use PEM or not?
vszakats commentedon Mar 21, 2025
Should have said plain unencrypted.
I'm guessing it's an encrypted PEM key. It might need libssh2 explicitly
built with
-DLIBSSH2_MD5_ENABLE -DLIBSSH2_MD5_PEM_ENABLE(CPPFLAGS)and/or an encyption method understood by WinCNG possibly.
The curl test suite uses unencrypted PEM RSA keys, those do work.
Is this a
-DENABLE_ECDSA_WINCNG=ONbuild?pyscripter commentedon Mar 21, 2025
Yes. It now works with unencrypted ECDSA keys without -m PEP.
willco007 commentedon Mar 21, 2025
For RSA keys, WinCNG only supports the legacy format, so the flag
-m PEMis required.vszakats commentedon Mar 21, 2025
Thanks @willco007. That's beyond my C skills and free time. I'm not using mbedTLS, or WinCNG. (Also have a bottomless TODO list with other curl (and some libssh2) issues.)
At this point I wouldn't advise WinCNG for serious uses, because of the unfixed flakiness issue.
No question all of these could be improved quite a bit.
vszakats commentedon Mar 21, 2025
I second OpenSSL, with special mention of its forks. LibreSSL has smaller footprint for example. Recent wolfSSL should also work, it uses pretty much the same codepath as OpenSSL. All can be linked statically. (Can't comment on licensing.)
As said above WinCNG may not be reliable, depending on unknown factors. It certainly is consistently flaky in CI.
pyscripter commentedon Mar 21, 2025
I can create a new issue if you prefer but here are the details.
ssh-keygen -t rsa -m PEMas suggested.pyscripter commentedon Mar 21, 2025
How do I statically link with OpenSSL or LibreSSL? (I mean, so that there are no external dependencies - single self-contained DLL as with WinCNG).
I currently use the the following command file for building libssh2:
Do I need the
-DLIBSSH2_MD5_ENABLE -DLIBSSH2_MD5_PEM_ENABLEflags? What are they used for?vszakats commentedon Mar 22, 2025
You'll need to build the crypto backend and zlib statically first.
Then point
ZLIB_LIBRARYto the staticzlib.libandOPENSSL_CRYPTO_LIBRARYto the staticcrypto.lib. SettingOPENSSL_USE_STATIC_LIBS=ONmay also work.No. Unless you specifically want support for this insecure algo and insecure/obsolete PEM encryption method. These options are on their way out. You most likely don't want them.
LarsNordin-LNdata commentedon Nov 26, 2025
During my work with issue 1047 I found that without MD5 the whole decryption part in _libssh2_pem_parse() is removed (I added the error-message). The reading of PEM files is done different in libssh2, openssl.c is not using _libssh2_pem_parse(), wuth wincng.c is
if (method) {
#if LIBSSH2_MD5_PEM
/* Set up decryption */
......
#else
_libssh2_error(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED,
"Unable to decrypt PEM, MD5 not enabled");
ret = -1;
goto out;
#endif
vszakats commentedon Jul 15, 2026
This patch possibly fixes this, though I have not made tests yet:
#2279
edit: as for MD5 in
libssh2_pem_parse(), that is how the legacy PEMformat does the encryption. For this ECDSA case the format is OpenSSH,
which does it securely and through a different function. (To make this
even more confusing there is a third, PKCS8 format, which is only supported
by libssh2 for OS/400 via local code and relies on the crypto backends
elsewhere.)
wincng: reuse ECDSA key loading/parsing code from `pem.c`