1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-01-27 00:18:12 +03:00
Commit Graph

1597 Commits

Author SHA1 Message Date
skuodi
6746b78970 mbedtls: Bug fix for gen_publickey_from_rsa (#1560)
Notes:
* Bug fix for gen_publickey_from_rsa
Increment buffer write pointer after writing modulus and after writing exponent.
Add 1 to modulus(bignum) size.

Credit:
skuodi
2025-03-20 13:47:47 -07:00
Viktor Szakats
df0563a857 cmake: make Find modules use INTERFACE
- move dependency properties (libs, libdirs, C flags, header dirs,
  pkg-config module names) from global lists to imported target
  `INTERFACE` properties. Rework FInd modules to return their results
  like this and update the libssh2 build process to use it. It makes
  Find modules re-usable from the cmake-config script by libssh2
  consumers, to integrate with libssh2 dependencies.

- define libssh2 dependencies as "imported targets" by the name:
  `libssh2::<depname>`, e.g. `libssh2::libgcrypt`.

- cmake-config: add fall-back logic for CMake without
  CMP0099 (v3.17 2020-03-20) to set lib directories.

- generate `libssh2.pc` based on imported target properties (instead of
  global lists).

- add target property dump debug function.

- ci/GHA: also test cmake integration on macOS.

Follow-up to 96d7f404e7 #1534

Closes #1535
2025-03-19 18:31:19 +01:00
Viktor Szakats
c03eb8d379 comp: use z_const when zlib supports it
Also switch to zlib-specific types where missing.

Closes #1555
2025-03-11 01:38:17 +01:00
Will Cosgrove
c090b696c6 Session null checks to quiet fuzzer (#1556)
Update session.c. session.h

Make fuzzer happy on `session_startup()` with null session.
2025-03-04 17:11:09 -08:00
oss-patch
1ac1ff4438 kex.c: fix possible null pointer dereference in diffie_hellman_sha_algo() (#1508)
Notes:
PR Description:

Bug Type: Segv on unknown address
Summary: A vulnerability was discovered in the libssh2 program where an invalid memory access occurred due to dereferencing a NULL pointer. The issue was identified in the diffie_hellman_sha_algo function within kex.c.
Fix Summary: The patch resolves the issue by introducing a NULL pointer check for session->hostkey before dereferencing it. If the pointer is NULL, a corresponding error is returned, and the function exits gracefully. This fix ensures the program does not attempt to access invalid memory. The patch improves the security and stability of the program by preventing segmentation faults caused by invalid memory access.
2025-02-28 09:47:47 -08:00
Will Cosgrove
4beed72458 Return error if user KEX methods are invalid #1553 (#1554)
Notes:
Fixes #1553. Restores error case if user passes in invalid KEX method value to libssh2_session_method_pref.

Credit:
Amy Lin
2025-02-28 09:32:30 -08:00
Will Cosgrove
8cad398b0f _libssh2_base64_decode() handle malformed data (#1551)
Make sure to allocate enough space for malformed base64 encoded data.  Changed signed size values to unsigned to match input so we can't possibly truncate input.

Credit:
Stanislav Osipov
2025-02-27 11:46:52 -08:00
Will Cosgrove
2fb5803ce1 Fix possible double free if key in error cases (#1550)
Notes:
Fix possible double free of key in certain error cases. #1549

Credit:
Stanislav Osipov
2025-02-26 10:07:33 -08:00
Viktor Szakats
9642a96ef7 cmake: add missing quotes
For consistent initialization for `_sys_libdirs` and just for consistent
formatting in the other case.

Ref: https://github.com/curl/curl/pull/16233#issuecomment-2642603412
Follow-up to 3de8731ef2 #1540
2025-02-07 12:58:02 +01:00
Viktor Szakats
3de8731ef2 cmake: exclude CMAKE_C_IMPLICIT_LINK_DIRECTORIES from libssh2.pc
Co-authored-by: Kai Pastor
Ref: f72b848092
Ref: https://github.com/curl/curl/pull/16233
2025-02-07 12:01:35 +01:00
Viktor Szakats
d92de5951a cmake: initialize variables where missing
As detected using `cmake --warn-uninitialized`.

It also lists documented CMake global variables, which is unexpected:
`CMAKE_MODULE_PATH`, `CMAKE_IMPORT_LIBRARY_SUFFIX`.
I'd expect CMake to initialize its namespace.

https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-warn-uninitialized

Closes #1539
2025-02-06 03:43:37 +01:00
Viktor Szakats
9da0ca3cde cmake: normalize before matching paths with syspaths
Requires CMake 3.20:
https://cmake.org/cmake/help/latest/command/cmake_path.html

Co-authored-by: Kai Pastor
Ref: 307e1f9878
Closes #1538
2025-02-06 01:11:04 +01:00
Viktor Szakats
eea97dbf0a cmake: ZLIB linkage tidy-ups
- drop adding redundant ZLIB manual include headers.
  It's done now via `ZLIB::ZLIB`.
  Follow-up to 96d7f404e7 #1534

- src: replace missed `ZLIB_LIBRARIES` with `ZLIB::ZLIB`.
  Follow-up to 96d7f404e7 #1534

- src: drop always true `ZLIB_FOUND` check.

Cherry-picked from #1535
2025-02-05 12:47:23 +01:00
Viktor Szakats
96d7f404e7 cmake: make libssh2-config work with all TLS-backends
CMake:

- Find*: set `<modulename>_FOUND` for compatibility when found via
  `pkg-config`. E.g. `MbedTLS_FOUND`.
  `find_package_handle_standard_args()` sets both `<MODULENAME>_FOUND`
  and `<Modulename>_FOUND` when detecting the dependency. Some CMake
  code relies on this and 3rd-party code may rely on it too. Make sure
  to set the latter variant when detecting the dependency via
  `pkg-config`, where we don't call
  `find_package_handle_standard_args()`.

  CMake sets these variable to `TRUE` (not `ON` or `1`). Replicate this
  for compatibility.

- libssh2-config.cmake: inherit default `LIBSSH2_USE_PKGCONFIG`.
  Follow-up to a3aa6b4ca8 #1525

- document variables consumed by `libssh2-config.cmake.in`.

- `libssh2-config.cmake`: fix to link to non-OpenSSL crypto backends.
  This is most likely not how this is supposed to be done, but better
  than failing.
  What's the canonical way to do this, and how OpenSSL and zlib does it
  is yet to be figured out.

- use `ZLIB::ZLIB` to reference zlib.
- use `IN ITEMS` where missed.
- harmonize variable dump output formats.

CMake `find_package` integration tests:

- extend to all crypto backends (was: OpenSSL).
- show libssh2 variables set by `find_package()`.
- stop building examples and tests for the consumed package.
  For performance.
- enable zlib, for coverage.
- be verbose when building the test targets.

ci/GHA:

- add packaged mbedTLS (2.x) build to Linux matrix.
- alphasort some tests.

Follow-up to d9c2e550ca #1460
Follow-up to 82b09f9b3a #1322

Closes #1534
2025-02-04 00:39:38 +01:00
Viktor Szakats
d033c1eaf6 cmake: tidy up string append and list prepend syntax
- `set(VAR "${VAR}<value>")` ->
  `string(APPEND VAR "<value>")`
  Available since CMake 3.4:
  https://cmake.org/cmake/help/latest/command/string.html#append

- `set(VAR "${VAR2}-or-<value>;${VAR}")` ->
  `set(VAR "${VAR2}-or-<value>" ${VAR})`

- prefer dash-style MSVC option.
  Follow-up to 854cfa8292 #1524

Closes #1533
2025-02-02 02:57:52 +01:00
Viktor Szakats
b89858b83d disable deprecated algos by default
- MD5-based MACs and hashes: `hmac-md5`, `hmac-md5-96`,
  `LIBSSH2_HOSTKEY_HASH_MD5`
    You can enable it now with `-DLIBSSH2_MD5_ENABLE`.
    Disabled by default since OpenSSH 7.2 (2016-02-29).
- 3DES cipher: `3des-cbc`
    You can enable it now with `-DLIBSSH2_3DES_ENABLE`.
    Disabled by default since OpenSSH 7.4 (2016-12-19).
- RIPEMD-160 MACs: `hmac-ripemd160`, `hmac-ripemd160@openssh.com`
    You can enable it now with `-DLIBSSH2_HMAC_RIPEMD_ENABLE`.
    Removed in OpenSSH 7.6 (2017-10-03).
- Blowfish cipher: `blowfish-cbc`
    You can enable it now with `-DLIBSSH2_BLOWFISH_ENABLE`.
    Removed in OpenSSH 7.6 (2017-10-03).
- RC4 ciphers: `arcfour`, `arcfour128`
    You can enable it now with `-DLIBSSH2_RC4_ENABLE`.
    Removed in OpenSSH 7.6 (2017-10-03).
- CAST cipher: `cast128-cbc`
    You can enable it now with `-DLIBSSH2_CAST_ENABLE`.
    Removed in OpenSSH 7.6 (2017-10-03).

- old-style, MD5-based encrypted private keys.
    You can enable it now with `-DLIBSSH2_MD5_PEM_ENABLE`.

CI runs:
before:
https://github.com/libssh2/libssh2/actions/runs/13066267976/job/36459081012
https://ci.appveyor.com/project/libssh2org/libssh2/builds/51426618
after:
https://github.com/libssh2/libssh2/actions/runs/13071320635/job/36473418776?pr=1531
https://ci.appveyor.com/project/libssh2org/libssh2/builds/51428270

Closes #1531
2025-01-31 13:04:33 +01:00
Viktor Szakats
5cca650b1d tidy-up: prefer #ifdef / #ifndef (formatting)
Closes #1532
2025-01-31 13:04:32 +01:00
Viktor Szakats
784446b6c5 build: add support for clang-cl, add CI job
- ci/appveyor: add clang-cl job.
- ci/appvayor: optimize setting an env.
- build: fix clang-cl builds.
- build: fix `-Wcast-function-type` compiler warnings for OpenSSL 3.
- build: use `stdint.h` with MSVC when supported.
- src: use `PRId64` for MSVC where supported.
- src: avoid recursive macro definition for `recv()` and `send()`.
- session: silence `-Wcast-function-type` for `libssh2_session_callback_set2()`.
  Sadly this function is still not fully warning-clean, and it
  seems we'd need separate setter-getters for each callback
  to avoid all warnings.

Closes #1484
2025-01-31 05:13:18 +01:00
Viktor Szakats
131480d2d5 wincng: move _libssh2_aes_ctr_increment() from misc, make static
Cherry-picked from #1484
2025-01-31 02:07:08 +01:00
Viktor Szakats
cf3aac1aba libssh2_base64_decode: add deprecation warning
Cherry-picked from #1484
2025-01-31 02:04:59 +01:00
Viktor Szakats
7027604505 libssh2_scp_send, libssh2_scp_send_ex: add deprecation warning
Also:
- switch to non-deprecated alternative in examples.
- add pointers and deprecation warning to libssh2_scp_send man page.

Cherry-picked from #1484
2025-01-31 02:04:59 +01:00
Viktor Szakats
4fbd592314 openssl: fix to build with wolfSSL without AES-CTR
Cherry-picked from #1484
2025-01-31 02:04:59 +01:00
Viktor Szakats
90a686ef3a gitignore: compress rules
Cherry-picked from #1484
2025-01-31 02:00:28 +01:00
Viktor Szakats
2da28146a0 openssl: make it compatible with wolfSSL built without OPENSSL_ALL
Cherry-picked from #1484
2025-01-31 01:50:15 +01:00
Viktor Szakats
606c102e52 build: enable -Wcast-qual, fix fallouts
- enable compiler warning `-Wcast-qual`.
- add `LIBSSH2_UNCONST()` macro to strip const where absolutely
  necessary to avoid compiler warnings.
- fix const stripping by constifying where necessary.
- fix const stripping by using `LIBSSH2_UNCONST()`.
- libgcrypt.h: drop unnecessary casts.
- openssl: fix to use new `BIO_new_mem_buf()` parameter types
  with wolfSSL.

Cherry-picked from #1484
Closes #1527
2025-01-31 00:57:26 +01:00
Viktor Szakats
a3aa6b4ca8 cmake: misc improvements, add LIBSSH2_USE_PKGCONFIG option
- show platform flags (via curl).
- add `LIBSSH2_USE_PKGCONFIG` option to control whether to use
  `pkg-config` to find dependencies.
- set `.pc` names withing the Find modules.
- add `mbedcrypto` to `libssh2.pc` only when detected via `pkg-config`.
  Workaround for older mbedtls versions and non-CMake mbedTLS builds
  (as of mbedTLS 3.6.2) that don't emit an `mbedcrypto.pc` file.
- set header paths relative to the project root (tidy-up).
- use `-isystem` for crypto backend and zlib header paths.
  To match autotools.
- sync header path order with autotools.
- rename local variables to underscore-lowercase.
- minor tidy-ups.

Cherry-picked from #1484
Closes #1525
2025-01-30 23:36:39 +01:00
Viktor Szakats
854cfa8292 build: prepare builds for clang-cl, add cmake ossfuzz support
- cmake: add support to build ossfuzz.
  Enable with `-DBUILD_OSSFUZZ=ON`.
  Also supports `-DLIB_FUZZING_ENGINE=` like autotools does.
- check for `__clang__` when suppressing warnings in source. Necessary
  for clang-cl, which set `__clang__`, but doesn't set `__GNU__`.
- cmake: optimize out 4 picky warning option detections with gcc.
- cmake: bring `-pedantic-error`, `-Wall` use closer to curl's.
- cmake: set `-Wno-language-extension-token` for clang-cl.
- cmake: escape only the necessary `-W` options for clang-cl.
- cmake: apply picky warnings to C++.
- cmake: replace `unset(VAR)` with `set(VAR "")` for init.
- cmake: prefer dash-style MSVC options.
- cmake: simplify `MATCHES` expression.
- cmake: formatting/whitespace.
- ci/GHA: bump `actions/upload-artifact` to v4

Closes #1524
2025-01-30 22:01:49 +01:00
Viktor Szakats
af5c9fb88f openssl: fix indentation 2025-01-29 19:00:51 +01:00
Will Cosgrove
73ac65f79b userauth.c: fix typo from #1516
When making style fixes I inverted the NULL check logic.
2025-01-23 08:06:25 -08:00
Will Cosgrove
49837fd756 userauth.c: fix possible memory leaks #1504 (#1516)
* userauth.c: fix possible memory leaks #1504

Notes:
Fix possible memory leaks if `userauth_list()` is called more than once, e.g. an auth error case. 

Author:
Will Cosgrove

Credit:
pyscripter
2025-01-22 16:53:14 -08:00
Viktor Szakats
21228cffb8 transport: fix indentation of comments
Cherry-picked from #1484
2024-11-15 01:55:18 +01:00
Viktor Szakats
c78d54c28e transport: stop passing newline to the trace handler via debugdump()
The trace handler is called from two places in libssh2. One of them was
passing a newline at the end of the trace message string, the other one
was not.

When the trace handler feature was introduced, a newline was passed both
via `debugdump()` and `libssh2_debug()`:
44eba0c993 (2010-01-15)

Shortly after a commit deleted the newline for `libssh2_debug()`:
0f0652a309 (2010-06-23)

This patch re-syncs behaviour between the traceback callbacks by
dropping the newline for trace handler calls made from `debugdump()`.

Reported-by: Chris Emsen
Fixes #1485
Follow-up to 0f0652a309
Closes #1492
2024-11-14 20:00:14 +01:00
Viktor Szakats
114923f1ac tidy-up: unsigned const char -> const unsigned char
For consistency with rest of the code.

Closes #1487
2024-11-02 00:37:59 +01:00
Viktor Szakats
5d03b4f94a cmake: build but don't install static lib in certain conditions
Building 3 tests require static libssh2 lib. Some may prefer not to
create the static lib, yet prefer to build all tests, including those
3 that require it.

Detect such intent by looking for an explicit `BUILD_TESTING=ON` and
`BUILD_STATIC_LIBS=OFF`, then build the static lib anyway but without
installing it.

Reported-by: Eli Schwartz
Fixes #1450
Closes #1469
2024-10-28 21:10:32 +01:00
Anders Borum
1a9e8811f7 session: support server banners up to 8192 bytes (was: 256)
If server had banner exceeding 256 bytes there wasn't enough room in
`_LIBSSH2_SESSION.banner_TxRx_banner`. Only the first 256 bytes would be
read making the first packet read fail but also dooming key exchange as
`session->remote.banner` didn't include everything.

This change bumps the banner buffer to 8KB to match OpenSSH.

Fixes #1442
Closes #1443
2024-10-08 23:04:12 +02:00
Viktor Szakats
c87f129630 cmake: generate LIBSSH2_PC_LIBS_PRIVATE dynamically
Generate `LIBSSH2_PC_LIBS_PRIVATE` from `LIBSSH2_LIBS`.

Also add extra libdirs (`-L`) to `Libs` and `Libs.private`.

Logic copied from curl.

Closes #1466
2024-10-04 11:24:34 +02:00
Viktor Szakats
14b1b9d021 cmake: add comment about ibssh2.pc.in variables [ci skip] 2024-10-04 02:25:15 +02:00
Viktor Szakats
d70cee360e cmake: support absolute CMAKE_INSTALL_INCLUDEDIR/CMAKE_INSTALL_LIBDIR
in `libssh2.pc`.

Also use `${exec_prefix}` (instead of `${prefix}`) as a base for `libdir`.

Closes #1465
2024-10-04 02:21:02 +02:00
Viktor Szakats
0fce9dcc29 cmake: rename two variables and initialize them
- `LIBRARIES` -> `LIBSSH2_LIBS`
- `SOCKET_LIBRARIES` -> `LIBSSH2_LIBS_SOCKET`

Also initialize them before use.

Cherry-picked from #1445
Closes #1464
2024-10-03 21:57:48 +02:00
Daniel Stenberg
b942bad190 openssl: free allocated resources when using openssl3
Reproduces consistently with curl test case 638

Closes #1459
2024-09-30 11:43:18 +02:00
Viktor Szakats
1117b677a0 checksrc: update, check all sources, fix fallouts
update from curl:
cff75acfec/scripts/checksrc.pl

Closes #1457
2024-09-28 11:41:52 +02:00
dksslq
8d3bc19b3b Fix memory leaks in _libssh2_ecdsa_curve_name_with_octal_new and _libssh2_ecdsa_verify (#1449)
Better error handling in`_libssh2_ecdsa_curve_name_with_octal_new` and `_libssh2_ecdsa_verify` to prevent leaks.

Credit: dksslq <dksslq@github.com>
2024-09-27 08:54:22 -07:00
rolag
de0048759b Fix unstable connections over nonblocking sockets (#1454)
The `send_existing()` function allows partially sent packets to be sent
fully before any further packets are sent. Originally this returned
`LIBSSH2_ERROR_BAD_USE` when a different caller or thread tried to send
an existing packet created by a different caller or thread causing the
connection to disconnect. Commit 33dddd2f8a removed the return
allowing any caller to continue sending another caller's packet. This
caused connection instability as discussed in #1397 and confused the
client and server causing occasional duplicate packets to be sent and
giving the error `rcvd too much data` as discussed in #1431. We return
`LIBSSH2_ERROR_EAGAIN` instead to allow existing callers to finish
sending their own packets.

Fixes #1397
Fixes #1431
Related #720

Credit: klux21, rolag
2024-09-27 08:52:47 -07:00
Will Cosgrove
b346541847 Prevent possible double free of hostkey (#1452)
NULL server hostkey based on fuzzer failure case.
2024-09-10 09:35:26 -07:00
Viktor Szakats
9d9ee7807d cmake: tidy up syntax, minor improvements
- make internal variables underscore-lowercase.
- unfold lines.
- fold lines setting header directories.
- fix indent.
- drop interim variable `EXAMPLES`.
- initialize some variables before populating them.
- clear a variable after use.
- add `libssh2_dumpvars()` function for debugging.
- allow to override default `CMAKE_UNITY_BUILD_BATCH_SIZE`.
- bump up default `CMAKE_UNITY_BUILD_BATCH_SIZE` to 0 (was 32).
- tidy up option descriptions.

Closes #1446
2024-09-07 21:27:55 +02:00
Viktor Szakats
570de0f23f cmake: rename mbedTLS and wolfSSL Find modules
To match the curl ones.

Cherry-picked from #1445
2024-08-18 12:57:04 +02:00
Michael Buckley
492bc543bb Implement chacha20-poly1305@openssh.com
Probably the biggest and potentially most controversial change we have
to upstream.

Because earlier versions of OpenSSL implemented the algorithm before
standardization, using an older version of OpenSSL can cause problems
connecting to OpenSSH servers. Because of this, we use the public domain
reference implementation instead of the crypto backends, just like
OpenSSH does.

We've been holding this one for a few years. We were about to upstream
it around the same time as aes128gcm landed upstream, and the two
changes were completely incompatible. Honestly, it took me weeks to
reconcile these two implementations, and it could be much better.

Our original implementation changed every crypt method to decrypt the
entire message at once. the AESGCM implementation instead went with this
firstlast design, where a firstlast paramater indicates whether this is
the first or last call to the crypt method for each message. That added
a lot of bookkeeping overhead, and wasn't compatible with the chacha
public domain implementation.

As far as I could tell, OpenSSH uses the technique of decrypting the
entire message in one go, and doesn't have anything like firstlast.
However, I could not get out aes128gcm implementation to work that way,
nor could I get the chacha implementation to work with firstlast, so I
split it down the middle and let each implementation work differently.
It's kind of a mess, and probably should be cleaned up, but I don't have
the time to spend on it anymore, and it's probably better to have
everything upstream.

Fixes #584
Closes #1426
2024-08-06 10:56:23 +02:00
Viktor Szakats
29a4b607d8 tidy-up: do/while formatting
Also fix an indentation and delete empty lines.

Closes #1440
2024-08-06 10:56:22 +02:00
Viktor Szakats
8ae1b2d742 wolfssl: drop header path hack
The wolfSSL OpenSSL headers reside in `wolfssl/openssl/*.h`.

Before this patch the wolfSSL OpenSSL compatibilty header includes were
shared with the native OpenSSL codepath, and used `openssl/*h`. For
wolfSSL builds this required a hack to append the
`<wolfssl-prefix>/wolfssl` directory to the header search path, to find
the headers.

This patch changes the source to use the correct header references,
allowing to drop the header path hack.

Also fix to use the correct variable to set up the header path in CMake:
`WOLFSSL_INCLUDE_DIRS` (was: `WOLFSSL_INCLUDE_DIR`, without the `S`)

Closes #1439
2024-08-06 10:56:22 +02:00
Viktor Szakats
2bb46d4481 cmake: add quotes, delete ending dirseps
Follow-up to 3fa5282d62 #1166
Closes #1437
2024-08-06 10:56:22 +02:00