Skip to content

mbedtls: fix missing rsa_init when loading key from memory - #1822

Merged
willco007 merged 1 commit into
libssh2:masterfrom
P81000:fix-mbedtls-rsa-init-frommemory
Mar 30, 2026
Merged

willco007 merged 1 commit into
libssh2:masterfrom
P81000:fix-mbedtls-rsa-init-frommemory

Conversation

@P81000

@P81000 P81000 commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR fixes a critical threading issue in the mbedTLS backend when loading RSA private keys from memory. It resolves the fatal error LIBSSH2_ERROR_CALLBACK_RETURNED_ERROR (-19) that occurs during the authentication phase.

Root Cause Analysis

While debugging the _libssh2_mbedtls_rsa_sha2_sign function, I identified that the underlying mbedTLS call was failing with error code -0x001C (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA).

The issue originates in _libssh2_mbedtls_rsa_new_private_frommemory:

  1. The RSA context is allocated using mbedtls_calloc, which zero-initializes the structure.
  2. In mbedTLS (especially when compiled with threading support), the mbedtls_rsa_context contains a mutex that must be initialized via mbedtls_rsa_init.
  3. The current implementation skips this initialization, leaving the mutex in an invalid state.
  4. When mbedtls_rsa_pkcs1_sign is eventually called, it attempts to lock this uninitialized mutex, leading to the threading error and the subsequent -19 callback failure in libssh2.

Changes

  • Added the missing mbedtls_rsa_init() call in _libssh2_mbedtls_rsa_new_private_frommemory right after allocation.
  • This aligns the "from memory" implementation with the "from file" implementation (_libssh2_mbedtls_rsa_new_private), which is already correctly initialized.

Testing

  • Confirmed that RSA signatures (both SHA-1 and SHA-2) now succeed without threading errors.

This adds the missing mbedtls_rsa_init() call in _libssh2_mbedtls_rsa_new_private_frommemory, which caused MBEDTLS_ERR_THREADING_BAD_INPUT_DATA when attempting to sign data due to an uninitialized mutex.
@P81000

P81000 commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

How long it takes to a maintainer to read our PRs?

@dfandrich

Copy link
Copy Markdown
Contributor

Nobody is paid to work on libssh2, so reviews have to wait to fit into life.

@willco007
willco007 merged commit 7419595 into libssh2:master Mar 30, 2026
82 checks passed
@willco007

Copy link
Copy Markdown
Member

@P81000 Thanks for the fix!

vszakats added a commit that referenced this pull request Jun 10, 2026
The check for version 3+ is redundant after bumping the minimum to 3.1.

Follow-up to f63f68b #1727
Follow-up to 7419595 #1822

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants