Repository navigation
Fix mbedTLS AES-CBC backend: avoid BAD_INPUT_DATA during cipher_finish() in SSH handshake - #1758
Conversation
…t_mode(), so check the cipher type against known CBC identifiers instead.
| return -1; | ||
|
|
||
| op = encrypt == 0 ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT; | ||
| op = encrypt ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT; |
There was a problem hiding this comment.
Makes me wonder how could libssh2 + mbedTLS work correctly before if this condition
was reversed all along?
There was a problem hiding this comment.
Pull request overview
Fixes libssh2’s mbedTLS crypto backend so AES-CBC negotiation doesn’t fail immediately after NEWKEYS during the first encrypted client packet, aligning cipher direction and ensuring the cipher layer does not apply/validate padding that libssh2 already handles.
Changes:
- Correct the mbedTLS cipher operation selection (encrypt vs decrypt) during cipher context initialization.
- Configure mbedTLS CBC ciphers to use
MBEDTLS_PADDING_NONEto avoidMBEDTLS_ERR_CIPHER_BAD_INPUT_DATAduringmbedtls_cipher_finish().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if(algo == MBEDTLS_CIPHER_AES_128_CBC || | ||
| algo == MBEDTLS_CIPHER_AES_192_CBC || | ||
| algo == MBEDTLS_CIPHER_AES_256_CBC || | ||
| algo == MBEDTLS_CIPHER_DES_EDE3_CBC) { | ||
| ret = mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE); |
There was a problem hiding this comment.
Padding is only disabled for a hard-coded subset of CBC cipher types. The mbedTLS backend can also advertise Blowfish-CBC (see src/mbedtls.h), and if it’s negotiated it will still run with mbedTLS’ default padding handling and can hit the same cipher_finish() BAD_INPUT_DATA failure. Consider either (a) including MBEDTLS_CIPHER_BLOWFISH_CBC under an #ifdef in this list, or (b) switching the condition to detect “CBC mode” via cipher_info/ctx instead of enumerating specific algorithms.
Description
This PR fixes a reproducible failure in the mbedTLS backend of libssh2 when negotiating AES-CBC ciphers with certain SSH servers.
When libssh2 is built with mbedTLS (tested with mbedTLS 3.6.2), the library fails to encrypt the very first SSH packet after NEWKEYS when the chosen cipher is AES-XXX-CBC. The failure manifests as:
Failure establishing SSH session: -44 libssh2 last error: Unable to ask for ssh-userauth serviceTracing the mbedTLS backend reveals the root cause:
This happens during the encryption of SSH_MSG_SERVICE_REQUEST (seqno=0), right after the KEX exchange succeeds.
The server → client traffic is irrelevant at that point; this is a pure client-side mbedTLS cipher failure.
Reproduction steps
The issue can be reproduced with:
(confirmed with OpenSSH and a proprietary embedded SSH server)
Steps:
Root cause analysis
encryptflagThe code used:
which inverted the intended direction.
The fix correctly maps the flag:
libssh2 handles SSH packet padding internally, so the cipher must operate with no padding.
Without explicitly setting this, mbedTLS rejects CBC operations during
cipher_finish().The fix adds:
This ensures that mbedTLS does not attempt to validate or add padding at the cipher layer.
Related issues
Probably, this PR fixes #793