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

3010 Commits

Author SHA1 Message Date
Dan Fandrich
3c953c05d6 Add support for AES-GCM crypto protocols (#797)
Add support for aes256-gcm@openssh.com and aes128-gcm@openssh.com
ciphers, which are the OpenSSH implementations of AES-GCM cryptography.
It is similar to RFC5647 but has changes to the MAC protocol
negotiation.  These are implemented for recent versions of OpenSSL only.

The ciphers work differently than most previous ones in two big areas:
the cipher includes its own integrated MAC, and the packet length field
in the SSH frame is left unencrypted.  The code changes necessary are
gated by flags in the LIBSSH2_CRYPT_METHOD configuration structure.

These differences mean that both the first and last parts of a block
require special handling during encryption. The first part is where the
packet length field is, which must be kept out of the encryption path
but in the authenticated part (as AAD).  The last part is where the
Authentication Tag is found, which is calculated and appended during
encryption or removed and validated on decryption. As encryption/
decryption is performed on each packet in a loop, one block at a time,
flags indicating when the first and last blocks are being processed are
passed down to the encryption layers.

The strict block-by-block encryption that occurs with other protocols is
inappropriate for AES-GCM, since the packet length shifts the first
encrypted byte 4 bytes into the block. Additionally, the final part of
the block must contain the AES-GCM's Authentication Tag, so it must be
presented to the lower encryption layer whole. These requirements mean
added code to consolidate blocks as they are passed down.

When AES-GCM is negotiated as the cipher, its built-in MAC is
automatically used as the SSH MAC so further MAC negotiation is not
necessary.  The SSH negotiation is skipped when _libssh2_mac_override()
indicates that such a cipher is in use.  The virtual MAC configuration
block mac_method_hmac_aesgcm is then used as the MAC placeholder.

This work was sponsored by Anders Borum.

Integration-patches-by: Viktor Szakats

* fix checksrc errors
* fix openssl.c warning
* fix transport.c warnings
* switch to `LIBSSH2_MIN/MAX()` from `MIN()`/`MAX()`
* fix indent
* fix libgcrypt unused warning
* fix mbedtls unused warning
* fix wincng unused warning
* fix old openssl unused variable warnings
* delete blank lines
* updates to help merging with the ETM patch
2023-04-20 15:46:44 +02:00
Viktor Szakats
d09ca26563 tidy-up: align comments [ci skip] 2023-04-20 10:01:48 +00:00
Viktor Szakats
5e3acb9da9 tidy-up: whitespace nits [ci skip] 2023-04-20 09:58:10 +00:00
Viktor Szakats
857e431648 crypto: add/fix algo guards and extend NO options
Add new guard `LIBSSH2_RSA_SHA1`. Add missing guards for `LIBSSH2_RSA`,
`LIBSSH2_DSA`.

Fix warnings when all options are disabled.

This is still not complete and it's possible to break a build with
certain crypto backends (e.g. mbedTLS) and/or combination of options.
It's not guaranteed that all bits everywhere get disabled by these
settings. Consider this a "best effort".

Add these new options to disable certain crypto elements:
- `LIBSSH2_NO_3DES`
- `LIBSSH2_NO_AES_CTR`
- `LIBSSH2_NO_BLOWFISH`
- `LIBSSH2_NO_CAST`
- `LIBSSH2_NO_ECDSA`
- `LIBSSH2_NO_RC4`
- `LIBSSH2_NO_RSA_SHA1`
- `LIBSSH2_NO_RSA`

The goal is to offer a way to disable legacy/obsolete/insecure ones.

See also: 146a25a06d `LIBSSH2_NO_HMAC_RIPEMD`
See also: 38015f4e46 `LIBSSH2_NO_DSA`
See also: be31457f30 `LIBSSH2_NO_MD5`

Closes #986
2023-04-20 09:44:56 +00:00
Viktor Szakats
9a54f212a6 scp: fix typo in comments [ci skip]
Follow-up to 0a500b3554
2023-04-19 21:55:34 +00:00
Viktor Szakats
8d10b21731 base64: do not use snprintf() on encoding
This also significantly (by 7-8x in my limited tests with a short
string) speeds up this function. The impact is still minor as this
function is only used in `knownhost.c` in release builds.

Closes #985
2023-04-19 18:28:08 +00:00
Viktor Szakats
6c01fa5bb7 wincng: constify data arg of libssh2_wincng_hash()
Tested in #979
2023-04-19 18:28:08 +00:00
Viktor Szakats
31acf57299 wincng: fix unused variables with LIBSSH2_RSA_SHA2 disabled
Tested in #979
2023-04-19 18:28:08 +00:00
Viktor Szakats
d1bf8fadf7 ci: delete config elements for unused 32-bit Linux builds
They have been disabled since d9b4222ef1

Tested in #979
2023-04-19 18:28:08 +00:00
Viktor Szakats
e8ceea0c8e ci: enable FIXTURE_TRACE_ALL_CONNECT for WinCNG tests
To hopefully help finding the WinCNG hostkey verification
intermittent failure #804.

Tested in #979
2023-04-19 18:28:07 +00:00
Viktor Szakats
3336b00f72 tests: add FIXTURE_TRACE_ALL_CONNECT option
Works like the `FIXTURE_TRACE_ALL` envvar, but enables full trace for
the connection phase only.

Also fix a possible NULL deref with `FIXTURE_TRACE_ALL` and a failed
`libssh2_session_init_ex()`.

Tested in #979
2023-04-19 18:28:07 +00:00
Viktor Szakats
2d0bd5837b ci: really enable logging in AppVeyor CMake builds
`CONFIGURATION` was never passed to the cmake command, so it had
never enabled logging when set to `Debug`.

Also `CONFIGURATION` is ambiguous depending on the "generator" used
by CMake. In case of Visual Studio, this is a build/ctest-time
setting, not a cmake-config parameter.

So set this permanently to `Release` and enable logging via our
dedicated CMake option `ENABLE_DEBUG_LOGGING`.

Tested in #979
2023-04-19 18:27:49 +00:00
Viktor Szakats
e4d827479c HACKING-CRYPTO: fix stray whitespace 2023-04-19 18:24:12 +00:00
Viktor Szakats
17801d2064 tidy-up: fix more nits
- fix indentation errors.
- reformat `cmake/FindmbedTLS.cmake`
- replace a macro with a variable in `example/sftp_RW_nonblock.c`.
- delete macOS macro `_DARWIN_USE_64_BIT_INODE` from the
  OS/400 config header, `os400/libssh2_config.h`.
- fix other minor nits.

Closes #983
2023-04-18 08:41:20 +00:00
Viktor Szakats
2213352758 mansyntax: make it work on macOS, check reqs locally
- use `gman` alias if present. This makes it work when the correct `man`
  command is provided via `brew` on macOS.

- move CMake attempts to detect tools necessary to run `mansyntax.sh`
  into the script itself.

- delete CMake TODO to move more test logic into CMake. This would make
  it CMake-specific and require maintaining it separately for each build
  tool. Just use our external script when a POSIX shell is available.

Closes #982
2023-04-18 08:20:29 +00:00
Viktor Szakats
803f19f004 cmake: dedupe setting -DHAVE_CONFIG_H
Move `libssh2_config.h` generation and setting `-DHAVE_CONFIG_H` to
the root `CMakeFile.txt`.

Also move symbol hiding setup there. It needs to be done before
generating the config file for `LIBSSH2_API` value to be set in it.

After this change the `HIDE_SYMBOLS` setting is accepted without an
annoying CMake warning when not actually building a shared libssh2 lib.

Closes #981
2023-04-18 08:20:05 +00:00
Viktor Szakats
191c4e8c71 build: assume non-blocking I/O on Windows
Drop checks from Windows builds and enable it based on `WIN32`.

This saves detection time and also makes 3rd party builds simpler.

Also:

- delete `HAVE_DISABLED_NONBLOCKING`, that we used in build tools to
  explicitly disable an explicit `#error` in `session.c`.

- replace existing `WSAEWOULDBLOCK` check for Windows support with
  `WIN32`. Cleaner with the same result.

Follow-up to f1e80d8d8c
Follow-up to 5644eea216

Closes #980
2023-04-18 08:19:34 +00:00
Viktor Szakats
01f3fbf01c ci: rename Logging to Debug in AppVeyor 2023-04-17 14:31:54 +00:00
Viktor Szakats
739e41bf40 switch to internal base64 decode that uses size_t
Make the public `libssh2_base64_decode()` a wrapper for that.
Bump up length sizes in callers.

Also fix output size calculation to first divide then multiply.

Closes #978
2023-04-17 14:30:51 +00:00
Viktor Szakats
fe64bad58f tests: switch to debian:bullseye-slim in Dockerfile
'slim' provides all we need, with less bloat.

Tested in #976

Follow-up to 78cb64a859
2023-04-16 22:50:40 +00:00
Viktor Szakats
9ecb22daab tests: build improvements and more
- rename tests to have more succint names and a more useful natural
  order.

- rename `simple` and `ssh2` in tests to have the `test_` prefix.

  This avoids a name collisions with `ssh2` in examples.

- cmake: drop the `example-` prefix for generated examples.

  Bringing their names in sync with other build tools, like autotools.

- move common auth test code into the fixture and simplify tests by
  using that.

- move feature guards from CMake to preprocessor for auth tests.

  Now it works with all build tools and it's easier to keep it in sync
  with the lib itself.

  For this we need to include `libssh2_priv.h` in tests, which in turn
  needs tweaking on the trick we use to suppress extra MSVS warnings
  when building tests and examples.

- move mbedTLS blocklist for crypto tests from CMake to the test
  fixture.

- add ed25519 hostkey tests to `test_hostkey` and `test_hostkey_hash`.

- add shell script to regenerate all test keys used for our tests.

- alpha-sort tests.

- rename `signed_*` keys to begin with `key` like the rest of the keys
  do.

- whitespace fixes.

Closes #969
2023-04-16 22:50:01 +00:00
Viktor Szakats
238def4da8 autotools: rename a variable
To match its counterpart we use for clang and to better match
the original code in curl.

Follow-up to ec0feae792

Closes #977
2023-04-16 19:18:40 +00:00
Viktor Szakats
29fd14e22a ssh2.sh: revert likely wrong quoting [ci skip]
Follow-up to 5012442850
2023-04-16 18:38:41 +00:00
Viktor Szakats
16071c225f build: add -Wbad-function-cast picky warning
Also adjust minimum gcc versions in comment.

Closes #975
2023-04-16 17:22:10 +00:00
Viktor Szakats
819573f94e tests: restore debian:bullseye in Dockerfile
Follow-up to 78cb64a859
2023-04-16 17:08:16 +00:00
Viktor Szakats
5644eea216 session: simplify preprocessor logic
- by using #elif
- by merging two blocks

Closes #972
2023-04-16 16:20:20 +00:00
Viktor Szakats
c9160bf89c tests: try debian:testing for Dockerfile
Follow-up to 78cb64a859
2023-04-16 16:16:18 +00:00
Viktor Szakats
9ffbb05b44 src: add and use LIBSSH2_MIN/MAX macros
Also for #797

Closes #974
2023-04-16 15:48:59 +00:00
Viktor Szakats
78cb64a859 tests: switch Dockerfile to debian:testing-slim
From debian:bullseye

- doesn't need manual bumps.
- is ahead of stable and should be stable enough for our purpose.
- slim is saving resources.

Closes #971
2023-04-16 15:43:43 +00:00
Viktor Szakats
f1e80d8d8c cmake: optimize non-blocking tests on WIN32/non-WIN32
Skip testing unixy methods on Windows and vice versa.

I continue to assume that CMake doesn't define `WIN32` with Cygwin
(as Cygwin doesn't define `_WIN32`/`WIN32` for C), though I haven't
tested this.

Closes #970
2023-04-15 16:15:16 +00:00
Jörgen Sigvardsson
0a500b3554 scp: option to not quote paths (#803)
A new flag named `LIBSSH2_FLAG_QUOTE_PATHS` has been added, to make
libssh2 not quote file paths sent to the remote's scp subsystem. Some
custom ssh daemons cannot handle quoted paths, and this makes this flag
useful.

Authored-by: Jörgen Sigvardsson <jorgen.sigvardsson@westermo.com>
2023-04-15 18:11:19 +02:00
Viktor Szakats
31e6d95d01 cmake: make Windows builds initialize faster
By skipping unixy header checks that always fail with
the MSVC toolchain or all Windows toolchains.

Closes #968
2023-04-15 02:51:26 +00:00
Viktor Szakats
bf3b44ca18 cmake: use a single build rule for all tests
- use the complete filename of test sources in the input list.

- build all tests with the ability to access libssh2 internals.

  This is necessary for `test_keyboard_interactive_auth_info_request`
  now and might be necessary for others in the future, e.g. to avoid
  the depreacted public base64 decoding API.

- move `test_keyboard_interactive_auth_info_request` into the main
  test build loop.

- move `simple` into the main test build loop too.

- build `ssh2` also in static mode.

- cleanup the way we detect and enable gcov.

- fix indentation.

Closes #967
2023-04-15 02:50:41 +00:00
Viktor Szakats
c627c1256b tidy-up: more whitespace in src
Closes #966
2023-04-15 02:49:52 +00:00
Viktor Szakats
5d399233d8 checksrc: fix EQUALSNULL warnings
`s/([a-z0-9._>*-]+) == NULL/!\1/g`

Closes #964
2023-04-15 02:46:09 +00:00
Viktor Szakats
cd5b114e7d Makefile.am: add new OS400 header [ci skip]
Follow-up to 6dc42e9d62
2023-04-14 19:28:36 +00:00
Viktor Szakats
1b0c93b755 checksrc: fix NOTEQUALSZERO warnings
Closes #963
2023-04-14 19:16:27 +00:00
Viktor Szakats
8b4387d0dc checksrc: fix SIZEOFNOPAREN warnings
`s/sizeof ([a-z0-9._>*-]+)/sizeof(\1)/g`

Closes #962
2023-04-14 19:15:18 +00:00
Viktor Szakats
146a25a06d crypto: add LIBSSH2_NO_HMAC_RIPEMD option
See also: 38015f4e46
See also: be31457f30

Ref: https://github.com/stribika/stribika.github.io/issues/46

Closes #965
2023-04-14 19:14:10 +00:00
Viktor Szakats
2efdb6747a tidy-up: example, tests continued
- fix skip auth if `userauthlist` is NULL.
  Closes #836 (Reported-by: @sudipm-mukherjee on github)
- fix most silenced `checksrc` warnings.
- sync examples/tests code between each other.
  (output messages, error handling, declaration order, comments)
- stop including unnecessary headers.
- always deinitialize in case of error.
- drop some redundant variables.
- add error handling where missing.
- show more error codes.
- switch `perror()` to `fprintf()`.
- fix some `printf()`s to be `fprintf()`.
- formatting.

Closes #960
2023-04-14 11:07:53 +00:00
Viktor Szakats
0162d1649c src: fix indentation of macro definitions (follow-up)
Follow-up to d5438f4ba9
2023-04-13 23:44:41 +00:00
Viktor Szakats
d5438f4ba9 src: fix indentation of macro definitions
And some comment cleanup.

Closes #958
2023-04-13 23:39:01 +00:00
Viktor Szakats
f4f5841dd5 example/ssh2_exec: drop conditional code for deprecated API 2023-04-13 15:55:26 +02:00
monnerat
6dc42e9d62 Make OS/400 implementation work again (#953)
* os400: support QADRT development files in a non-standard directory

This enables the possibility to compile libssh2 even if the ascii
runtime development files are not installed system-wide.

* userauth_kbd_packet: fix a pointer target type mismatch.

A temporary variable matching the parameter type is used before copying
to the real target and checking for overflow (that should not occur!).

* os400qc3: move and fix big number procedures

A bug added by a previous code style cleaning is fixed.
_libssh2_random() now checks and return the success status.

* os400qc3: fix cipher definition block lengths

They were wrongly set to the key size.

* Diffie-Hellman min/max modulus sizes are dependent of crypto-backend

In particular, os400qc3 limits the maximum group size to 2048-bits.
Move definitions of these parameters to crypto backend header files.

* kex: return an error if Diffie-Hellman key pair generation fails

* os400: add an ascii assert.h header file

* os400qc3: implement RSA SHA2 256/512
2023-04-13 14:08:12 +02:00
Viktor Szakats
bf85faaa92 sftp: add open functions with custom attribute support
Before this patch, libssh2 sent hardcoded `LIBSSH2_SFTP_ATTRIBUTES`
struct on handle open. This can be problematic on some special OS,
where the file size should be known on new file creation. I added
two new functions to resolve this issue.

Patch-by: @vajdaakos on github via #506

Changes compared to #506:
- drop attr size fixup in favour of #946.
- move `memcpy()` under the state where we need it.
- bump filename length type to `size_t`.
- fix filenames in documentation and other nits.

Closes #506
Closes #947
2023-04-13 11:35:21 +00:00
Viktor Szakats
ec0feae792 build: speed up and extend picky compiler options
Implement picky warnings with clang in autotools. Extend picky gcc
warnings, sync them between build tools and compilers and greatly
speed up detection in CMake.

- autotools: enable clang compiler warnings with `--enable-debug`.

- autotools: enable more gcc compiler warnings with `--enable-debug`.

- autotools/cmake: sync compiler warning options between gcc and clang.

- sync compiler warning options between autotools and cmake.

- cmake: reduce option-checks to speed up the detection phase.
  Bring them down to 3 (from 35). Leaving some checks to keep the
  CMake logic alive and for an easy way to add new options.

  clang 3.0 (2011-11-29) and gcc 2.95 (1999-07-31) now required.

- autotools logic copied from curl, with these differences:

  - delete `-Wimplicit-fallthrough=4` due to a false positive.

  - reduce `-Wformat-truncation=2` to `1` due to a false positive.

  - simplify MinGW detection for `-Wno-pedantic-ms-format`.

- cmake: show enabled picky compiler options (like autotools).

- cmake: do compile `tests/simple.c` and `tests/ssh2.c`.

- fix new compiler warnings.

- `tests/CMakeLists.txt`: fix indentation.

Original source of autotools logic:
- a8fbdb461c/acinclude.m4
- a8fbdb461c/m4/curl-compilers.m4

Notice that the autotools implementation considers Apple clang as
legacy clang 3.7. CMake detection works more accurately, at the same
time more error-prone and difficult to update due to the sparsely
documented nature of Apple clang option evolution.

Closes #952
2023-04-13 11:12:22 +00:00
Viktor Szakats
224fffb178 include: delete leading underscore from macro name
It can cause compiler warnings in 3rd-party code.

Follow-up to 59666e03f0

Closes #957
2023-04-13 11:09:35 +00:00
Viktor Szakats
1ccae7d548 ci: use OpenSSL 3 on AppVeyor VS2022 images
Closes #954
2023-04-12 09:17:54 +00:00
Viktor Szakats
73d95a055c build: be friendly with 3rd-party build tools
After recent build changes, 3rd party build that took the list of
C source to compile them as-is, stopped working as expected, due to
`blowfish.c` and crypto-backend C sources no longer expected to compile
separately but via `bcrypt_pbkdf.c` and `crypto.c`, respectively.

This patch ensures that compiling these files directly result in an
empty object instead of redundant code and duplicated symbols.

Also:
- add a compile-time error if none of the supported crypto backends
  are enabled.
- fix `libssh2_crypto_engine()` for wolfSSL and os400qc3.
  Rearrange code to avoid a hard-to-find copy of crypto-backend
  selection guards.

Follow-up to 4f0f4bff5a
Follow-up to ff3c774e03

Closes #951
2023-04-12 09:17:10 +00:00
Viktor Szakats
fe02bd2b2d sftp: calculate attr size based on attr content in sftp_open()
Improve robustness by replacing constant argument of `sftp_attrsize()`
in `sftp_open()` with the actual `flag` value read from the `attr` we
plan to transfer. Restores state of this before
37624b61e3.

Prerequisite for #947, #506.

Also improve readability a bit and link to SFTP specs. Delete comment
about version 6: The latest spec no longer features the mentioned
"DO NOT IMPLEMENT" notice.

Closes #946
2023-04-11 00:45:41 +00:00