Hi,
We are writing a client code which can accept or decline connection to the server - so for each connection I understand there is a mbedtls_ssl_context data established. Once the same is closed or not required we need to do deinitialize or free memory allocated to its member variables like - we need to free all memory allocated since we need it back else our application will run out of memory like:
os_free(mbed_ctx->handshake); os_free(mbed_ctx->transform_negotiate); os_free(mbed_ctx->session_negotiate); os_free(mbed_ctx->in_buf); os_free(mbed_ctx->out_buf);
But there are many member variables which also need to free memory if allocated and assigned to it.
Is there a function / method that can free all memory for mbedtls_ssl_context instance variable?
Thanks in advance.
Regards, Prakash
Hi Prakash,
Thanks for getting in touch.
I think what you are after is the function: void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
You can find it's implementation in ssl_tls.c.
I hope that helps you out.
Thanks, Tom
On Tue, 2024-02-27 at 20:19 +0530, Satya Prakash Prasad via mbed-tls wrote:
Hi,
We are writing a client code which can accept or decline connection to the server - so for each connection I understand there is a mbedtls_ssl_context data established. Once the same is closed or not required we need to do deinitialize or free memory allocated to its member variables like - we need to free all memory allocated since we need it back else our application will run out of memory like:
os_free(mbed_ctx->handshake); os_free(mbed_ctx->transform_negotiate); os_free(mbed_ctx->session_negotiate); os_free(mbed_ctx->in_buf); os_free(mbed_ctx->out_buf);
But there are many member variables which also need to free memory if allocated and assigned to it.
Is there a function / method that can free all memory for mbedtls_ssl_context instance variable?
Thanks in advance.
Regards, Prakash
Hi Thomas / All,
Thanks for the info - I was able to add the function as suggested however I see that the structure in concern has a lot of other member variable like as mentioned below and I actually cannot find code reference in mbedtls_ssl_free to free all of them - please confirm if calling mbedtls_ssl_free is enough such that all allocated memory for all variables in mbedtls_ssl_context is released: ..........................................................
mbedtls_ssl_config mbedtls_ssl_send_t mbedtls_ssl_recv_t mbedtls_ssl_recv_timeout_t p_bio mbedtls_ssl_session *session_in; /*!< current session data (in) */ mbedtls_ssl_session *session_out; /*!< current session data (out) */ mbedtls_ssl_session *session; /*!< negotiated session data */ mbedtls_ssl_session *session_negotiate; /*!< session data in negotiation */ mbedtls_ssl_handshake_params *handshake; /*!< params required only during the handshake process */ mbedtls_ssl_transform *transform_in; /*!< current transform params (in) */ mbedtls_ssl_transform *transform_out; /*!< current transform params (in) */ mbedtls_ssl_transform *transform; /*!< negotiated transform params */ mbedtls_ssl_transform *transform_negotiate; /*!< transform params in negotiation */ unsigned char *in_buf; /*!< input buffer */ unsigned char *in_ctr; /*!< 64-bit incoming message counter TLS: maintained by us DTLS: read from peer */ unsigned char *in_hdr; /*!< start of record header */ unsigned char *in_len; /*!< two-bytes message length field */ unsigned char *in_iv; /*!< ivlen-byte IV */ unsigned char *in_msg; /*!< message contents (in_iv+ivlen) */ unsigned char *in_offt; /*!< read offset in application data */ ..........................................................................................
Regards, Prakash
On Tue, Feb 27, 2024 at 8:43 PM Thomas Daubney via mbed-tls < mbed-tls@lists.trustedfirmware.org> wrote:
Hi Prakash,
Thanks for getting in touch.
I think what you are after is the function: void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
You can find it's implementation in ssl_tls.c.
I hope that helps you out.
Thanks, Tom
On Tue, 2024-02-27 at 20:19 +0530, Satya Prakash Prasad via mbed-tls wrote:
Hi,
We are writing a client code which can accept or decline connection to the server - so for each connection I understand there is a mbedtls_ssl_context data established. Once the same is closed or not required we need to do deinitialize or free memory allocated to its member variables like - we need to free all memory allocated since we need it back else our application will run out of memory like:
os_free(mbed_ctx->handshake); os_free(mbed_ctx->transform_negotiate); os_free(mbed_ctx->session_negotiate); os_free(mbed_ctx->in_buf); os_free(mbed_ctx->out_buf);
But there are many member variables which also need to free memory if allocated and assigned to it.
Is there a function / method that can free all memory for mbedtls_ssl_context instance variable?
Thanks in advance.
Regards, Prakash
-- mbed-tls mailing list -- mbed-tls@lists.trustedfirmware.org To unsubscribe send an email to mbed-tls-leave@lists.trustedfirmware.org
Hi Satya,
The function you're looking for is mbedtls_ssl_free(). It won't free the context structure itself, but will free all the memory that had been allocated to member variables.
Generally speaking, for each structure xxx, the mbedtls_xxx_free() function does recursively frees all ressources (memory, mutexes, whatever) held by member variables, and also securily wipes out secrets from memory, so it's good to always call that function even when there are no ressources to be freed.
Regards, Manuel. ________________________________ From: Satya Prakash Prasad via mbed-tls mbed-tls@lists.trustedfirmware.org Sent: 27 February 2024 15:49 To: mbed-tls@lists.trustedfirmware.org mbed-tls@lists.trustedfirmware.org Subject: [mbed-tls] Release all memory held by mbedtls_ssl_context
Hi,
We are writing a client code which can accept or decline connection to the server - so for each connection I understand there is a mbedtls_ssl_context data established. Once the same is closed or not required we need to do deinitialize or free memory allocated to its member variables like - we need to free all memory allocated since we need it back else our application will run out of memory like:
os_free(mbed_ctx->handshake); os_free(mbed_ctx->transform_negotiate); os_free(mbed_ctx->session_negotiate); os_free(mbed_ctx->in_buf); os_free(mbed_ctx->out_buf);
But there are many member variables which also need to free memory if allocated and assigned to it.
Is there a function / method that can free all memory for mbedtls_ssl_context instance variable?
Thanks in advance.
Regards, Prakash
mbed-tls@lists.trustedfirmware.org