Skip to content

Commit 78af627

Browse files
committed
fix(mbedtls): handle NULL ctx in ds rsa sign
1 parent ede834d commit 78af627

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

components/mbedtls/port/esp_ds/esp_rsa_sign_alt.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,20 @@ int esp_ds_rsa_sign( void *ctx,
444444
esp_err_t ds_r;
445445
int ret = -1;
446446

447-
mbedtls_rsa_context *pk = (mbedtls_rsa_context *)ctx;
447+
/* This check is done to keep the compatibility with the previous versions of the API
448+
* which allows NULL ctx. If ctx is NULL, then the default padding
449+
* MBEDTLS_RSA_PKCS_V15 is used.
450+
*/
451+
int padding = MBEDTLS_RSA_PKCS_V15;
452+
if (ctx != NULL) {
453+
mbedtls_rsa_context *rsa_ctx = (mbedtls_rsa_context *)ctx;
454+
padding = rsa_ctx->MBEDTLS_PRIVATE(padding);
455+
}
448456

449457
const size_t data_len = s_ds_data->rsa_length + 1;
450458
const size_t sig_len = data_len * FACTOR_KEYLEN_IN_BYTES;
451459

452-
if (pk->MBEDTLS_PRIVATE(padding) == MBEDTLS_RSA_PKCS_V21) {
460+
if (padding == MBEDTLS_RSA_PKCS_V21) {
453461
#ifdef CONFIG_MBEDTLS_SSL_PROTO_TLS1_3
454462
if ((ret = (rsa_rsassa_pkcs1_v21_encode(f_rng, p_rng ,md_alg, hashlen, hash, sig_len, sig ))) != 0) {
455463
ESP_LOGE(TAG, "Error in pkcs1_v21 encoding, returned %d", ret);

0 commit comments

Comments
 (0)