All public API functions in `src/agent.c` that take a `LIBSSH2_AGENT *`
were missing a NULL-pointer check at entry.
A caller that passes NULL (for example, after `libssh2_agent_init()`
returns NULL due to OOM) immediately dereferences the null pointer and
crashes. Every other source file in this tree already has the pattern:
`channel.c`, `sftp.c`, `session.c`, `userauth.c`, and `knownhost.c` all
guard their primary handle before touching any field. `agent.c` was the
only outlier.
Add `if(!agent)` guards consistent with the style used in the rest of
the codebase.
Closes#2635
In `kex_mlkem768x25519_sha256` the x25519 half of the hybrid shared
secret is written into a fixed 64-byte buffer with `ssh2_bn_to_bin`,
which emits only the significant bytes and drops any leading zeros. When
the x25519 secret has a leading zero byte (about 1 in 256 handshakes,
and it comes from the server's ephemeral key) the write is short, so the
tail of the buffer is hashed while still uninitialized and the value
sits left-shifted instead of filling the fixed 32-byte field. That means
an uninitialized stack read into the exchange hash and a wrong K, which
fails the signature check for mlkem768x25519-sha256, the first KEX we
offer. This zero-fills the 32-byte field and writes the value
right-aligned so it is always the full fixed-width secret. The sibling
`kex_mlkem_nistp` sizes its heap buffer to the bignum length, so it has
no uninitialized read.
Bug possibly related: https://github.com/libssh2/libssh2/pull/1644#issuecomment-4761871304
Follow-up to 3ba252e2a6#1644Closes#2633
`wcng_privatekey_from_point()` sizes the ECC private-key blob with
`sizeof(BCRYPT_ECCPRIVATE_BLOB)`, but that name is the wide blob-type
string `L"ECCPRIVATEBLOB"`, not a struct, so the `sizeof` measures the
string (30 bytes on Windows) rather than the 8-byte `BCRYPT_ECCKEY_BLOB`
header. The three `memcpy` offsets right below it, and the public-key
sibling `wcng_publickey_from_point()`, already use
`sizeof(BCRYPT_ECCKEY_BLOB)`, so the buffer ends up oversized and its
uninitialized tail is handed to `BCryptImportKeyPair()` with an inflated
`cbInput`. This is on the path that loads an OpenSSH-format ECDSA
private key through
`libssh2_userauth_publickey_frommemory()`/`fromfile()` on the CNG
backend.
Using `sizeof(BCRYPT_ECCKEY_BLOB)` lines the allocation up with the
header the copies actually write from and with the length passed to
`BCryptImportKeyPair()`. Keeping it in step with the public-key path
means the private blob is described to CNG with exactly the bytes it
holds.
Follow-up to 3e72343737#1315Closes#2631
Also:
- enable MSYS2 cache for both curl tests.
- inherit pre-installed MSYS2 in the ARM64 curl job.
- bump integration tests from mingw64 to ucrt64 env.
- switch deprecated mingw64 jobs to ucrt64/clang64.
(Keep one mingw64 and one mingw32 job.)
- silence mingw64/mingw32 deprecation warning.
Closes#2638
For slightly better performance, and better efficiency.
Saving over 1 minute. The clang-tidy build is 2x fast.
Also:
- GHA/windows: fix `windres` command-line for non-intel machines.
Closes#2637
- update `docker/build-push-action` from 7.3.0 to 7.4.0
- update `github/codeql-action/analyze` from 4.37.8 to 4.38.2
- update `github/codeql-action/init` from 4.37.8 to 4.38.2
Closes#2634
To help debugging issues.
Also:
- bump sshd LogLevel to DEBUG1, for essential details.
- bump command output buffer in `run_command_varg()`
- drop redundant condition in `run_command_varg()`
- replace `BUFSIZ` with literal sizes in `run_command_varg()`.
After this patch it's showing this in the CI log for the libgcrypt
fallout that previously needed a long time to diagnose:
```
61: ---- sshd log ----
61: No matching private key for certificate: /etc/ssh/ssh_host_mldsa44-ed25519_key-cert.pub
61: Server listening on 0.0.0.0 port 22.
61: Server listening on :: port 22.
61: debug1: sshd-session version OpenSSH_10.5, OpenSSL 3.6.3 9 Jun 2026
61: Connection from 172.17.0.1 port 51878 on 172.17.0.2 port 22 rdomain ""
61: debug1: sshd-auth version OpenSSH_10.5, OpenSSL 3.6.3 9 Jun 2026 [preauth]
61: debug1: list_hostkey_types: rsa-sha2-512,rsa-sha2-256,ssh-rsa,rsa-sha2-512-cert-v01@openssh.com,
rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,
ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com [preauth]
61: debug1: Local version string SSH-2.0-OpenSSH_10.5p1 Debian-1 [preauth]
61: debug1: Remote protocol version 2.0, remote software version libssh2_1.11.2_DEV [preauth]
61: debug1: compat_banner: no match: libssh2_1.11.2_DEV [preauth]
61: debug1: SSH2_MSG_KEXINIT sent [preauth]
61: debug1: SSH2_MSG_KEXINIT received [preauth]
61: debug1: kex: algorithm: (no match) [preauth]
61: Unable to negotiate with 172.17.0.1 port 51878: no matching key exchange method found.
Their offer: diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,
diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,
diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,ext-info-c,
kex-strict-c-v00@openssh.com [preauth]
[...]
61: ------------------
```
Follow-up to 1d0b86ac84#2484
Follow-up to 8be5716437#2174
Follow-up to 51f6259d18#1720Closes#2621
In favor of direct execution or `docker login` and `docker logout`.
Also to limit the logged-in time to the steps the require it, and to
avoid passing a secret via the command-line.
Closes#2616
To overwrite default ones with the ones matching the copied private
keys.
Fixing in sshd log:
```
Public key for /etc/ssh/ssh_host_rsa_key does not match private key
Public key for /etc/ssh/ssh_host_ecdsa_key does not match private key
Public key for /etc/ssh/ssh_host_ed25519_key does not match private key
```
Closes#2614
To reflect the support for multiple container engines, and to make
terminology more consistent and implementation-agnostic.
Also:
- add FIXME for `docker-machine ip` command that has been dropped
upstream.
Follow-up to 9bd1f16dc2#2605Closes#2609
- add `FIXTURE_CONTAINER_CMD` to the container command. Supported:
`podman`, `container`, `docker` (default).
- deprecate `OPENSSH_NO_DOCKER` env. Replaced by setting
`FIXTURE_CONTAINER_CMD` to an empty string.
- GHA: replace Docker with `podman` in a some Linux jobs.
- openssh_fixture: fixup `inspect` commands to be compatible with both
docker and podman.
- openssh_fixture: add `inspect` fallback for podman, which does not
support `HostIp` before 6.1.0.
Ref: https://github.com/podman-container-tools/podman/issues/29164
- openssh_fixture: add Apple container alternatives for `pull` and
`tag`.
- openssh_fixture: fix `run` command to suppress progress output when
using Apple container.
- openssh_fixture: if `OPENSSH_SERVER_PORT` is set, omit querying
the port number via the container tool.
`podman` seems to be a tiny bit slower than Docker in CI.
Requirements for Apple container:
- Apple container 0.8.0+ (2026-01-02). Tested with 1.4.1.
- `jq` installed in PATH.
- setting the sshd port manually on the host via `OPENSSH_SERVER_PORT`
(`container` cannot automatically assign one).
- physical macOS machine.
Example to run conainerized tests on macOS:
```sh
brew install container jq
container system start --disable-kernel-install
container system kernel set --recommended
export FIXTURE_CONTAINER_CMD=container
export OPENSSH_SERVER_PORT=<free-port-number>
export OPENSSH_SERVER_IMAGE; OPENSSH_SERVER_IMAGE=ghcr.io/libssh2/ci_tests_openssh_server:$(git rev-parse --short=20 HEAD:tests/openssh_server)
ctest -VV
```
Also tried:
- using Apple container in CI. Does not work because nested
virtualization is not supported in GHA runners.
- installing WSL on Windows. Failed with cryptic error upon
`wsl --install --distribution Debian`.
Closes#2605
To enable testing on arm64 Linux and possibly also on macOS, and
possibly possibly on arm64 Windows.
Also:
- switch to debian:testing to trigger a container rebuild.
- Dockerfile: install `adduser` explicitly. Required for debian:forky.
- Dockerfile: stop installing suggested/recommended packages.
Ref: https://github.com/docker/build-push-actionCloses#2606
To use a key type that is:
- supported by all backends.
- can be generated with the most recent `ssh-keygen` version.
- here to stay for the foreseeable future.
(DSA is scheduled for removal from the libssh2 codebase.)
Cherry-picked from #2598Closes#2604
Should no longer be necessary after fixing the root cause.
(Though we do not run these tests in CI at the moment.)
Ref: #804
Follow-up to f216bbe3aa#2583
Follow-up to bc120a343b#1023Closes#2603
Fixing:
```
x86_64-w64-mingw32-ld: src/CMakeFiles/libssh2_shared.dir/Unity/unity_0_c.c.obj: in function `userauth_hostbased_fromfile':
src/userauth.c:923:(.text+0x34c29): undefined reference to `ssh2_pub_privkey'
x86_64-w64-mingw32-ld: src/CMakeFiles/libssh2_shared.dir/Unity/unity_0_c.c.obj: in function `userauth_publickey':
src/userauth.c:1745:(.text+0x37171): undefined reference to `ssh2_pub_privkey'
collect2: error: ld returned 1 exit status
```
Note: This fixes the build, but leaves `ssh2_pub_privkey()` a no-op, because it's not implemented for ECDSA.
Closes#2600
The WinCNG backend has been missing runtime tests for a while. To
restore some level of testing, this PR make use of curl's regression
test suite (`runtests`) to run SCP/SFTP-specific tests with the
master/PR libssh2 revision at hand. Some tests need to be disabled when
using the built-in Windows OpenSSH server.
The current CI jobs reproduce the long-running WinCNG flaky failure
reported in #804 reliability.
Note, if these CI jobs make libssh2 CI a pain to use, they will be
removed. This has a reasonably fair chance to happen, based on the
experience with curl CI with Windows.
Also:
- test both ECDH and non-ECDH builds.
- test build on x64 and ARM64.
- test with both Windows built-in OpenSSH port and MSYS2's sshd.
- enable debug logging in libssh2 while running the tests.
- for extra stress, run each of the 56-57 tests 6 times.
This may be lowered in a future commit to avoid flakiness and reduce
CI load and job times.
Caveats:
- as seen in curl CI, runtests may result in hangs due to broken pipes
and error 2304 for undiscovered reasons.
Discussion: https://github.com/curl/curl/discussions/14854
- MSBuild has an undisablable "smart" feature that sometimes marks a
successful run a failure.
Refs:
a477789c43https://github.com/curl/curl/pull/18301
- these tests are different in scope than libssh2's own.
- tests sometime fail flaky with:
```
Failure Event: -43 - Failed getting banner
```
Ref: https://github.com/libssh2/libssh2/actions/runs/34134974899/job/101783681252?pr=2590
- running on modern runner machines, the Win10-introduced DH algo is
always enabled.
Ref: https://github.com/libssh2/libssh2/discussions/2049
Ref: #2583Closes#2590
- normalize fixed-width WinCNG bignums before SSH `mpint` serialization
- apply the normalization to DH public values and DH/ECDH shared secrets
- zero the removed tail bytes and add common bignum conversion tests
WinCNG returns DH public values and raw DH/ECDH shared secrets in
fixed-width buffers. The WinCNG bignum retained that buffer width as its
numeric length. When a value started with `0x00`, libssh2 included the
redundant byte in the exchange hash even though RFC 4251 requires a
canonical `mpint` representation.
The peer hashes the normalized integer, so the exchange hashes differ
and a valid server host-key signature is rejected. This appeared
intermittently as `LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE` because it
depends on the generated value.
The helper introduced here moves significant bytes to the start of the
existing allocation, explicitly clears the removed tail, and updates
only the logical length. It does not add an allocation or a new failure
path.
Reported-by: Dan Fandrich
Fixes#804
Ref: #2590 (tests)
Closes#2583
Omit direct backend-specific API use. Drop one test using negative
input, that's impossible with the internal crypto API. Using
backend-specific code in tests is highly undesired. It's also
discouraged to special-case any, in this case WinCNG had to be because
it does not implement the DH validation function.
Also:
- mbedtls: fix an internal API type difference with a cast.
- unfold a line.
Follow-up to fcf830788d#2239Closes#2595