@@ -1143,7 +1143,7 @@ static CK_RV prvDeleteObjectFromList( CK_OBJECT_HANDLE xPalHandle )
11431143{
11441144 CK_RV xResult = CKR_OK ;
11451145 int32_t lGotSemaphore = ( int32_t ) 0 ;
1146- uint32_t ulIndex = 0 ;
1146+ CK_ULONG ulIndex ;
11471147
11481148 lGotSemaphore = mbedtls_mutex_lock ( & xP11Context .xObjectList .xMutex );
11491149
@@ -1187,49 +1187,57 @@ static CK_RV prvAddObjectToList( CK_OBJECT_HANDLE xPalHandle,
11871187{
11881188 CK_RV xResult = CKR_HOST_MEMORY ;
11891189
1190- /* MISRA Ref 10.5.1 [Essential type casting] */
1191- /* More details at: https://github.com/FreeRTOS/corePKCS11/blob/main/MISRA.md#rule-105 */
1192- /* coverity[misra_c_2012_rule_10_5_violation] */
1193- CK_BBOOL xObjectFound = ( CK_BBOOL ) CK_FALSE ;
1194- uint32_t ulSearchIndex = 0 ;
1195-
1196- if ( 0 == mbedtls_mutex_lock ( & xP11Context .xObjectList .xMutex ) )
1190+ if ( xLabelLength > pkcs11configMAX_LABEL_LENGTH )
1191+ {
1192+ xResult = CKR_ARGUMENTS_BAD ;
1193+ LogError ( ( "Failed to add object to internal object list: "
1194+ "xLabelLength exceeds pkcs11configMAX_LABEL_LENGTH." ) ) ;
1195+ }
1196+ else if ( 0 == mbedtls_mutex_lock ( & xP11Context .xObjectList .xMutex ) )
11971197 {
1198+ CK_ULONG ulSearchIndex ;
1199+ CK_ULONG ulEmptyIndex = 0 ;
1200+ P11Object_t * pxEmptyP11Object = NULL ;
1201+
1202+ /* Iterate over list to find an existing entry containing xPalHandle */
11981203 for ( ulSearchIndex = 0 ; ulSearchIndex < pkcs11configMAX_NUM_OBJECTS ; ulSearchIndex ++ )
11991204 {
1200- if ( xResult == CKR_OK )
1201- {
1202- break ;
1203- }
1205+ P11Object_t * pxP11Object = & ( xP11Context .xObjectList .xObjects [ ulSearchIndex ] );
12041206
1205- if ( xP11Context .xObjectList .xObjects [ ulSearchIndex ].xHandle == xPalHandle )
1206- {
1207- /* Object already exists in list. */
1208- /* MISRA Ref 10.5.1 [Essential type casting] */
1209- /* More details at: https://github.com/FreeRTOS/corePKCS11/blob/main/MISRA.md#rule-105 */
1210- /* coverity[misra_c_2012_rule_10_5_violation] */
1211- xResult = CKR_OK ;
1212- xObjectFound = ( CK_BBOOL ) CK_TRUE ;
1213- }
1214- else if ( xP11Context .xObjectList .xObjects [ ulSearchIndex ].xHandle == CK_INVALID_HANDLE )
1207+ /* Update an existing entry with the desired xPalHandle */
1208+ if ( pxP11Object -> xHandle == xPalHandle )
12151209 {
1210+ ( void ) memcpy ( pxP11Object -> xLabel , pcLabel , xLabelLength );
1211+ pxP11Object -> xLabelSize = xLabelLength ;
1212+
12161213 xResult = CKR_OK ;
1214+ * pxAppHandle = ulSearchIndex + 1 ;
1215+
1216+ /* Entry updated, so exit the loop. */
1217+ break ;
12171218 }
12181219 else
12191220 {
1220- /* Cannot find a free object. */
1221+ if ( ( pxP11Object -> xHandle == CK_INVALID_HANDLE ) &&
1222+ ( pxEmptyP11Object == NULL ) )
1223+ {
1224+ pxEmptyP11Object = pxP11Object ;
1225+ ulEmptyIndex = ulSearchIndex ;
1226+ }
12211227 }
12221228 }
12231229
1224- /* MISRA Ref 10.5.1 [Essential type casting] */
1225- /* More details at: https://github.com/FreeRTOS/corePKCS11/blob/main/MISRA.md#rule-105 */
1226- /* coverity[misra_c_2012_rule_10_5_violation] */
1227- if ( ( xResult == CKR_OK ) && ( xObjectFound == ( CK_BBOOL ) CK_FALSE ) && ( xLabelLength <= pkcs11configMAX_LABEL_LENGTH ) )
1230+ /* Check if we have reached the end of the list without writing */
1231+ if ( ( xResult != CKR_OK ) &&
1232+ ( pxEmptyP11Object != NULL ) )
12281233 {
1229- xP11Context .xObjectList .xObjects [ ulSearchIndex - 1UL ].xHandle = xPalHandle ;
1230- ( void ) memcpy ( xP11Context .xObjectList .xObjects [ ulSearchIndex - 1UL ].xLabel , pcLabel , xLabelLength );
1231- xP11Context .xObjectList .xObjects [ ulSearchIndex - 1UL ].xLabelSize = xLabelLength ;
1232- * pxAppHandle = ulSearchIndex ;
1234+ pxEmptyP11Object -> xHandle = xPalHandle ;
1235+ pxEmptyP11Object -> xLabelSize = xLabelLength ;
1236+
1237+ ( void ) memcpy ( pxEmptyP11Object -> xLabel , pcLabel , xLabelLength );
1238+
1239+ * pxAppHandle = ulEmptyIndex + 1 ;
1240+ xResult = CKR_OK ;
12331241 }
12341242
12351243 ( void ) mbedtls_mutex_unlock ( & xP11Context .xObjectList .xMutex );
0 commit comments