While developing a Fleet Provisioning (FP) application which runs on the Renesas RX65N MCU, I encountered a bug where the first MQTT connection after FP operatoin always fails.
The PKCS11 session is closed prematurely, before the provisioning routine is completed. (SOLVED)
- I fixed this by shifting the xPkcs11CloseSession function call to the end of provisioning routine.
- After fixing this issue, the following bug happens…
First MQTT connection following FP completion always fails, due to premature closing of PKCS11 session.
After investigation, I traced the root cause to xPkcs11CloseSession, specifically xFunctionList->C_Finalize .
This function completely de-initializes the PKCS#11 library, releasing all resources.
After this function is called, all subsequent cryptographic operations will fail, until it is reinitialized with C_Initialize.
In order to fix this issue, we removed the C_Finalize function call in xPkcs11CloseSession.
Request
Is this the intended behavior for xPkcs11CloseSession?
It seems to not work well if we want to perform further MQTT operation immediately after device provisioning.
In my application, the C_Initialize is called in transport_mbedtls_pkcs11.c#L796.
After the first connection attempt failed, PKCS11 library will be re-initialized by tlsSetup in TLS_FreeRTOS_Connect.
Is this code specific to your application? I think your need to re-factor the code like the following:
xPkcs11Init() // Does C_Initialize
xPkcs11DeInit() // Does C_Finalize
xInitializePkcs11Session // Starts the session and does not call C_Initialize
xPkcs11CloseSession // Closes the session and does not call C_Finalize