@@ -1143,7 +1143,7 @@ static CK_RV prvDeleteObjectFromList( CK_OBJECT_HANDLE xPalHandle )
1143
1143
{
1144
1144
CK_RV xResult = CKR_OK ;
1145
1145
int32_t lGotSemaphore = ( int32_t ) 0 ;
1146
- uint32_t ulIndex = 0 ;
1146
+ CK_ULONG ulIndex ;
1147
1147
1148
1148
lGotSemaphore = mbedtls_mutex_lock ( & xP11Context .xObjectList .xMutex );
1149
1149
@@ -1187,49 +1187,57 @@ static CK_RV prvAddObjectToList( CK_OBJECT_HANDLE xPalHandle,
1187
1187
{
1188
1188
CK_RV xResult = CKR_HOST_MEMORY ;
1189
1189
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 ) )
1197
1197
{
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 */
1198
1203
for ( ulSearchIndex = 0 ; ulSearchIndex < pkcs11configMAX_NUM_OBJECTS ; ulSearchIndex ++ )
1199
1204
{
1200
- if ( xResult == CKR_OK )
1201
- {
1202
- break ;
1203
- }
1205
+ P11Object_t * pxP11Object = & ( xP11Context .xObjectList .xObjects [ ulSearchIndex ] );
1204
1206
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 )
1215
1209
{
1210
+ ( void ) memcpy ( pxP11Object -> xLabel , pcLabel , xLabelLength );
1211
+ pxP11Object -> xLabelSize = xLabelLength ;
1212
+
1216
1213
xResult = CKR_OK ;
1214
+ * pxAppHandle = ulSearchIndex + 1 ;
1215
+
1216
+ /* Entry updated, so exit the loop. */
1217
+ break ;
1217
1218
}
1218
1219
else
1219
1220
{
1220
- /* Cannot find a free object. */
1221
+ if ( ( pxP11Object -> xHandle == CK_INVALID_HANDLE ) &&
1222
+ ( pxEmptyP11Object == NULL ) )
1223
+ {
1224
+ pxEmptyP11Object = pxP11Object ;
1225
+ ulEmptyIndex = ulSearchIndex ;
1226
+ }
1221
1227
}
1222
1228
}
1223
1229
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 ) )
1228
1233
{
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 ;
1233
1241
}
1234
1242
1235
1243
( void ) mbedtls_mutex_unlock ( & xP11Context .xObjectList .xMutex );
0 commit comments