Skip to content

Add support for AES-GCM crypto protocols - #797

Merged
vszakats merged 16 commits into
masterfrom
dfandrich/aesgcmpr
Apr 20, 2023
Merged

vszakats merged 16 commits into
masterfrom
dfandrich/aesgcmpr

Conversation

@dfandrich

Copy link
Copy Markdown
Contributor

Add support for [email protected] and [email protected]
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.

@dfandrich
dfandrich force-pushed the dfandrich/aesgcmpr branch 2 times, most recently from 3777c10 to 9ea5486 Compare February 20, 2023 22:19
@dfandrich

Copy link
Copy Markdown
Contributor Author

One test case on one AppVeyor build is failing, and it doesn't seem to be due to flakyness. I'm trying to get to the bottom of that.

@dfandrich dfandrich changed the title Add support for AES-GCM crypto protocols WIP: Add support for AES-GCM crypto protocols Feb 21, 2023
@dfandrich
dfandrich force-pushed the dfandrich/aesgcmpr branch 6 times, most recently from 2a83548 to ebf3480 Compare February 21, 2023 05:55
@dfandrich dfandrich changed the title WIP: Add support for AES-GCM crypto protocols Add support for AES-GCM crypto protocols Feb 21, 2023
@dfandrich

Copy link
Copy Markdown
Contributor Author

I've looked into the failure some more. It's not one test that's failing and it's not failing consistently. However, the failure appears to always be during the key exchange, and it ends with these logs:

[00:05:13] 5: [libssh2] 0.951417 Transport: Packet type 33 received, length=1078
[00:05:13] 5: [libssh2] 0.951417 Transport: Looking for packet of type: 33
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's MD5 Fingerprint: 0c:0e:d1:a5:bb:10:27:5f:76:92:4c:e1:87:ce:5c:5e
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's SHA1 Fingerprint: f3:cd:59:e2:91:3f:44:22:b8:0f:7b:0a:82:b2:b8:9e:ae:44:93:87
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's SHA256 Fingerprint: kuPaSd88f5moKPUF7YI5OXpdH2KRRFl2D4ePdRD1Y6M=
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -11 - Unable to verify hostkey signature
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -8 - Unrecoverable error exchanging keys
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -8 - Unable to exchange encryption keys
[00:05:13] 5: libssh2_session_handshake failed (-8): Unable to exchange encryption keys
[00:05:13] 5: [libssh2] 1.045165 Transport: Disconnecting: reason=11, desc=test ended, lang=
[00:05:13] 5: => libssh2_transport_write plain (23 bytes)
[00:05:13] 5: 0000: 01 00 00 00 0B 00 00 00  0A 74 65 73 74 20 65 6E : .........test en
[00:05:13] 5: 0010: 64 65 64 00 00 00 00                             : ded....

It has negotiated the use of diffie-hellman-group-exchange-sha256. It seems unlikely to be related to this PR, especially since it's not even using AES-GCM, but it's hard to prove this.

@dfandrich

Copy link
Copy Markdown
Contributor Author

That AppVeyor build failed the same way on PR#800 which doesn't touch any of the files involved in the test, so I'm quite confident it is not failing due to this PR.

@vszakats

Copy link
Copy Markdown
Member

The OpenSSL deprecation warnings were silenced with 45d33da in master.

@palmin

palmin commented Mar 20, 2023

Copy link
Copy Markdown
Contributor

The OpenSSL deprecation warnings were silenced with 45d33da in master.

Sorry. Somehow I put this comment in the wrong PR.

Comment thread src/crypt.c Resolved
Comment thread src/crypt.c Resolved
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 16, 2023
vszakats added a commit that referenced this pull request Apr 16, 2023
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/transport.c Resolved
Comment thread src/openssl.c Resolved
@vszakats
vszakats force-pushed the dfandrich/aesgcmpr branch from 060e602 to 3761309 Compare April 20, 2023 10:29
@vszakats
vszakats force-pushed the dfandrich/aesgcmpr branch from 50f81ed to 4affab9 Compare April 20, 2023 13:36
@vszakats
vszakats merged commit 3c953c0 into master Apr 20, 2023
@dfandrich

dfandrich commented Apr 20, 2023 via email

Copy link
Copy Markdown
Contributor Author

vszakats added a commit that referenced this pull request Apr 21, 2023
Support for calculating MAC (message authentication code) on encrypted
data instead of plain text data.

This adds support for the following MACs:
- `[email protected]`
- `[email protected]`
- `[email protected]`

Integration-patches-by: Viktor Szakats

* rebase on master
* fix checksec warnings
* fix compiler warning
* fix indent/whitespace/eol
* rebase/manual merge onto AES-GCM patch #797
* more manual merge of `libssh2_transport_send()` based
  on dfandrich/shellfish

Fixes #582
Closes #655
Closes #987
vszakats added a commit that referenced this pull request May 3, 2023
Follow-up to 3c953c0 #797

There is pending issue with wolfSSL, where encryption/decryption is not
working (both with and without this patch). Ref: #1020

Cherry-picked from #1017
@bagder
bagder deleted the dfandrich/aesgcmpr branch May 29, 2023 10:02
vszakats added a commit that referenced this pull request Jun 24, 2024
Add workaround for the wolfSSL `EVP_Cipher(*p, NULL, NULL, 0)` bug to
make libssh2 work with wolfSSL v5.6.0 and older.

wolfSSL fixed this issue in v5.7.0:
wolfSSL/wolfssl#7143
wolfSSL/wolfssl@b0de0a1

Without our local workaround:

- v5.3.0 and older fail most tests:
  Ref: https://github.com/libssh2/libssh2/actions/runs/9646827522/job/26604211476#step:17:1263

- v5.4.0, v5.5.x, v5.6.0 fail these:
  ```
  29 - test_read-aes128-cbc (Failed)
  30 - test_read-aes128-ctr (Failed)
  32 - test_read-aes192-cbc (Failed)
  33 - test_read-aes192-ctr (Failed)
  34 - test_read-aes256-cbc (Failed)
  35 - test_read-aes256-ctr (Failed)
  ```
  Ref: https://github.com/libssh2/libssh2/actions/runs/9646827522/job/26604233819#step:17:978

Oddly enough the workaround breaks OpenSSL tests, so only enable it for
the affected wolfSSL versions.

Also add new build-from-source wolfSSL CI job to test the new codepath.

wolfSSL has a build bug where `wolfssl/options.h` and
`wolfssl/version.h` are not copied to the `install` destination with
autotools. With CMake it has a different bug where `wolfcrypt/sp_int.h`
is not copied (with v5.4.0). And another with CMake where `FIPS_mode()`
remains missing (with v5.6.0 and earlier.)

Therefore use CMake with v5.5.4 and a workaround for `FIPS_mode()`.
Another option is autotools with v5.4.0 and a workaround for `install`,
but CMake builds quicker.

Regression-from 3c953c0 #797
Fixes #1020
Fixes #1299
Assisted-by: Michael Buckley via #1394
Closes #1394 (another attempt to fix the mentioned wolfSSL bug)
Closes #1407
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Add support for [email protected] and [email protected]
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
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Support for calculating MAC (message authentication code) on encrypted
data instead of plain text data.

This adds support for the following MACs:
- `[email protected]`
- `[email protected]`
- `[email protected]`

Integration-patches-by: Viktor Szakats

* rebase on master
* fix checksec warnings
* fix compiler warning
* fix indent/whitespace/eol
* rebase/manual merge onto AES-GCM patch libssh2#797
* more manual merge of `libssh2_transport_send()` based
  on dfandrich/shellfish

Fixes libssh2#582
Closes libssh2#655
Closes libssh2#987
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Follow-up to 3c953c0 libssh2#797

There is pending issue with wolfSSL, where encryption/decryption is not
working (both with and without this patch). Ref: libssh2#1020

Cherry-picked from libssh2#1017
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Add workaround for the wolfSSL `EVP_Cipher(*p, NULL, NULL, 0)` bug to
make libssh2 work with wolfSSL v5.6.0 and older.

wolfSSL fixed this issue in v5.7.0:
wolfSSL/wolfssl#7143
wolfSSL/wolfssl@b0de0a1

Without our local workaround:

- v5.3.0 and older fail most tests:
  Ref: https://github.com/libssh2/libssh2/actions/runs/9646827522/job/26604211476#step:17:1263

- v5.4.0, v5.5.x, v5.6.0 fail these:
  ```
  29 - test_read-aes128-cbc (Failed)
  30 - test_read-aes128-ctr (Failed)
  32 - test_read-aes192-cbc (Failed)
  33 - test_read-aes192-ctr (Failed)
  34 - test_read-aes256-cbc (Failed)
  35 - test_read-aes256-ctr (Failed)
  ```
  Ref: https://github.com/libssh2/libssh2/actions/runs/9646827522/job/26604233819#step:17:978

Oddly enough the workaround breaks OpenSSL tests, so only enable it for
the affected wolfSSL versions.

Also add new build-from-source wolfSSL CI job to test the new codepath.

wolfSSL has a build bug where `wolfssl/options.h` and
`wolfssl/version.h` are not copied to the `install` destination with
autotools. With CMake it has a different bug where `wolfcrypt/sp_int.h`
is not copied (with v5.4.0). And another with CMake where `FIPS_mode()`
remains missing (with v5.6.0 and earlier.)

Therefore use CMake with v5.5.4 and a workaround for `FIPS_mode()`.
Another option is autotools with v5.4.0 and a workaround for `install`,
but CMake builds quicker.

Regression-from 3c953c0 libssh2#797
Fixes libssh2#1020
Fixes libssh2#1299
Assisted-by: Michael Buckley via libssh2#1394
Closes libssh2#1394 (another attempt to fix the mentioned wolfSSL bug)
Closes libssh2#1407
vszakats added a commit that referenced this pull request Jun 15, 2026
Spotted by GitHub Code Quality

Follow-up to 3c953c0 #797

Closes #2057
vszakats added a commit that referenced this pull request Jul 24, 2026
…ypt()`

By applying two bounds checks to non-debug builds.

Reported-by: Vladimir Eli Tokarev
Fixes GHSA-c4f7-cvfc-33j7
Follow-up to 3c953c0 #797

Closes #2401
vszakats added a commit that referenced this pull request Aug 3, 2026
To make the `SSH2_CRYPT_FLAG_*` macros greppable and easier to
recognize.

Slight downside that it makes these macros longer to type. If it becomes
an issue, the flag symbols could be shortened.

Also: move these macro from global internal header to the only source
where they are used, `transport.c`.

Follow-up to 3c953c0 #797

Closes #2495
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants