Skip to content

Commit

Permalink
Add guards for mbedtls_psa_crypto_free()
Browse files Browse the repository at this point in the history
Signed-off-by: Przemek Stekiel <[email protected]>
  • Loading branch information
mprse committed Apr 25, 2023
1 parent 8fa17b6 commit d4d049b
Show file tree
Hide file tree
Showing 32 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions programs/fuzz/fuzz_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_ssl_config_free(&conf);
mbedtls_ssl_free(&ssl);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#else
(void) Data;
Expand Down
2 changes: 2 additions & 0 deletions programs/fuzz/fuzz_dtlsclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_ssl_config_free(&conf);
mbedtls_ssl_free(&ssl);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#else
(void) Data;
Expand Down
2 changes: 2 additions & 0 deletions programs/fuzz/fuzz_dtlsserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_ssl_config_free(&conf);
mbedtls_ssl_free(&ssl);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#else
(void) Data;
Expand Down
4 changes: 3 additions & 1 deletion programs/fuzz/fuzz_privkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
abort();
}
}
#if defined(MBEDTLS_USE_PSA_CRYPTO)
exit:
mbedtls_pk_free(&pk);
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_pk_free(&pk);
#else
(void) Data;
(void) Size;
Expand Down
4 changes: 3 additions & 1 deletion programs/fuzz/fuzz_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
abort();
}
}
#if defined(MBEDTLS_USE_PSA_CRYPTO)
exit:
mbedtls_pk_free(&pk);
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_pk_free(&pk);
#else
(void) Data;
(void) Size;
Expand Down
4 changes: 3 additions & 1 deletion programs/fuzz/fuzz_x509crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
if (ret == 0) {
ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
}
#if defined(MBEDTLS_USE_PSA_CRYPTO)
exit:
mbedtls_x509_crl_free(&crl);
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_x509_crl_free(&crl);
#else
(void) Data;
(void) Size;
Expand Down
4 changes: 3 additions & 1 deletion programs/fuzz/fuzz_x509crt.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
if (ret == 0) {
ret = mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ", &crt);
}
#if defined(MBEDTLS_USE_PSA_CRYPTO)
exit:
mbedtls_x509_crt_free(&crt);
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_x509_crt_free(&crt);
#else
(void) Data;
(void) Size;
Expand Down
4 changes: 3 additions & 1 deletion programs/fuzz/fuzz_x509csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
if (ret == 0) {
ret = mbedtls_x509_csr_info((char *) buf, sizeof(buf) - 1, " ", &csr);
}
#if defined(MBEDTLS_USE_PSA_CRYPTO)
exit:
mbedtls_x509_csr_free(&csr);
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_x509_csr_free(&csr);
#else
(void) Data;
(void) Size;
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/gen_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/key_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ int main(int argc, char *argv[])
#endif

mbedtls_pk_free(&pk);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/key_app_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ int main(int argc, char *argv[])
mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);

mbedtls_pk_free(&key);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/pk_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctr_drbg);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/pk_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctr_drbg);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/pk_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/pk_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ int main(int argc, char *argv[])

exit:
mbedtls_pk_free(&pk);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/rsa_sign_pss.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/pkey/rsa_verify_pss.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ int main(int argc, char *argv[])

exit:
mbedtls_pk_free(&pk);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/dtls_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ int main(int argc, char *argv[])
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/dtls_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ int main(void)
#endif
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
printf(" Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/mini_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ int main(void)
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_free(&ca);
#endif
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

mbedtls_exit(ret);
}
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_client1.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ int main(void)
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_context_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,9 @@ int main(int argc, char *argv[])
printf("Finished. No valid base64 code found\n");
}

#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_fork_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ int main(void)
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_mail_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,9 @@ int main(int argc, char *argv[])
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_pthread_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ int main(void)
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free();
#endif
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/ssl/ssl_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ int main(void)
#endif
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/x509/cert_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ int main(int argc, char *argv[])
#endif
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/x509/cert_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/x509/cert_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,9 @@ int main(int argc, char *argv[])
mbedtls_mpi_free(&serial);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/x509/crl_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ int main(int argc, char *argv[])

exit:
mbedtls_x509_crl_free(&crl);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
2 changes: 2 additions & 0 deletions programs/x509/load_roots.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ int main(int argc, char *argv[])
exit_code = MBEDTLS_EXIT_SUCCESS;

exit:
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_exit(exit_code);
}
#endif /* necessary configuration */
2 changes: 2 additions & 0 deletions programs/x509/req_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ int main(int argc, char *argv[])

exit:
mbedtls_x509_csr_free(&csr);
#if defined(MBEDTLS_USE_PSA_CRYPTO)
mbedtls_psa_crypto_free();
#endif /* MBEDTLS_USE_PSA_CRYPTO */

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down

0 comments on commit d4d049b

Please sign in to comment.