Skip to content

Commit 6d72f6a

Browse files
committed
[#346] Resolved multiple SonarCloud results
1 parent 6650cff commit 6d72f6a

6 files changed

+108
-88
lines changed

src/core/crypto.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ uint8_t Crypto_Prep_Reply(uint8_t *reply, uint8_t appID)
265265

266266
// Fill reply with reply header
267267
reply[count++] = (sdls_frame.hdr.pvn << 5) | (sdls_frame.hdr.type << 4) | (sdls_frame.hdr.shdr << 3) |
268-
((sdls_frame.hdr.appID & 0x700 >> 8));
268+
(sdls_frame.hdr.appID & 0x700 >> 8);
269269
reply[count++] = (sdls_frame.hdr.appID & 0x00FF);
270270
reply[count++] = (sdls_frame.hdr.seq << 6) | ((sdls_frame.hdr.pktid & 0x3F00) >> 8);
271271
reply[count++] = (sdls_frame.hdr.pktid & 0x00FF);
@@ -442,12 +442,24 @@ int32_t Crypto_PDU(uint8_t *ingest, TC_t *tc_frame)
442442
break;
443443
}
444444
break;
445+
446+
default: // ERROR
447+
#ifdef PDU_DEBUG
448+
printf(KRED "Error: Crypto_PDU failed interpreting User Flag! \n" RESET);
449+
#endif
450+
break;
445451
}
446452
break;
447453

448454
case PDU_TYPE_REPLY:
449455
#ifdef PDU_DEBUG
450456
printf(KRED "Error: Crypto_PDU failed interpreting PDU Type! Received a Reply!?! \n" RESET);
457+
#endif
458+
break;
459+
460+
default:
461+
#ifdef PDU_DEBUG
462+
printf(KRED "Error: Crypto_PDU failed interpreting PDU Type!\n" RESET);
451463
#endif
452464
break;
453465
}

src/core/crypto_aos.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
8686
printf("\tVCID: 0x%04X", vcid);
8787
printf("\tMAP: %d\n", 0);
8888
printf("\tPriHdr as follows:\n\t\t");
89-
for (int i = 0; i < 6; i++)
89+
for (i = 0; i < 6; i++)
9090
{
9191
printf("%02X", (uint8_t)pTfBuffer[i]);
9292
}
@@ -120,7 +120,7 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
120120

121121
#ifdef AOS_DEBUG
122122
printf(KYEL "AOS BEFORE Apply Sec:\n\t" RESET);
123-
for (int16_t i = 0; i < current_managed_parameters_struct.max_frame_size; i++)
123+
for (i = 0; i < current_managed_parameters_struct.max_frame_size; i++)
124124
{
125125
printf("%02X", pTfBuffer[i]);
126126
}
@@ -180,6 +180,9 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
180180
case SA_AUTHENTICATED_ENCRYPTION:
181181
printf(KBLU "Creating a SDLS AOS - AUTHENTICATED ENCRYPTION!\n" RESET);
182182
break;
183+
default:
184+
printf(KRED "Failed interpreting SA Service Type\n" RESET);
185+
break;
183186
}
184187
#endif
185188

@@ -241,20 +244,18 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
241244
return status;
242245
}
243246

244-
if (sa_ptr->est == 0 && sa_ptr->ast == 1)
247+
if (sa_ptr->est == 0 && sa_ptr->ast == 1 && sa_ptr->acs_len != 0)
245248
{
246-
if (sa_ptr->acs_len != 0)
249+
if ((sa_ptr->acs == CRYPTO_MAC_CMAC_AES256 || sa_ptr->acs == CRYPTO_MAC_HMAC_SHA256 ||
250+
sa_ptr->acs == CRYPTO_MAC_HMAC_SHA512) &&
251+
sa_ptr->iv_len > 0)
247252
{
248-
if ((sa_ptr->acs == CRYPTO_MAC_CMAC_AES256 || sa_ptr->acs == CRYPTO_MAC_HMAC_SHA256 ||
249-
sa_ptr->acs == CRYPTO_MAC_HMAC_SHA512) &&
250-
sa_ptr->iv_len > 0)
251-
{
252-
status = CRYPTO_LIB_ERR_IV_NOT_SUPPORTED_FOR_ACS_ALGO;
253-
mc_if->mc_log(status);
254-
return status;
255-
}
253+
status = CRYPTO_LIB_ERR_IV_NOT_SUPPORTED_FOR_ACS_ALGO;
254+
mc_if->mc_log(status);
255+
return status;
256256
}
257257
}
258+
258259
// Start index from the transmitted portion
259260
for (i = sa_ptr->iv_len - sa_ptr->shivf_len; i < sa_ptr->iv_len; i++)
260261
{
@@ -297,8 +298,8 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
297298

298299
// Byte Magic
299300
hex_padding[0] = (pkcs_padding >> 16) & 0xFF;
300-
hex_padding[1] = (pkcs_padding >> 8) & 0xFF;
301-
hex_padding[2] = (pkcs_padding)&0xFF;
301+
hex_padding[1] = (pkcs_padding >> 8) & 0xFF;
302+
hex_padding[2] = pkcs_padding & 0xFF;
302303

303304
uint8_t padding_start = 0;
304305
padding_start = 3 - sa_ptr->shplf_len;
@@ -619,7 +620,7 @@ int32_t Crypto_AOS_ApplySecurity(uint8_t *pTfBuffer)
619620

620621
#ifdef AOS_DEBUG
621622
printf(KYEL "Printing new AOS frame:\n\t");
622-
for (int i = 0; i < current_managed_parameters_struct.max_frame_size; i++)
623+
for (i = 0; i < current_managed_parameters_struct.max_frame_size; i++)
623624
{
624625
printf("%02X", pTfBuffer[i]);
625626
}
@@ -1063,6 +1064,9 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
10631064
case SA_AUTHENTICATED_ENCRYPTION:
10641065
printf(KBLU "Processing a AOS - AUTHENTICATED ENCRYPTION!\n" RESET);
10651066
break;
1067+
default:
1068+
printf(KRED "Failed interpreting SA Service Type\n" RESET);
1069+
break;
10661070
}
10671071
#endif
10681072

@@ -1113,7 +1117,7 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
11131117
}
11141118

11151119
// Accio buffer
1116-
p_new_dec_frame = (uint8_t *)calloc(1, (len_ingest) * sizeof(uint8_t));
1120+
p_new_dec_frame = (uint8_t *)calloc(1, len_ingest * sizeof(uint8_t));
11171121
if (!p_new_dec_frame)
11181122
{
11191123
#ifdef DEBUG
@@ -1166,7 +1170,7 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
11661170

11671171
// Calculate size of the protocol data unit
11681172
// NOTE: This size itself is not the length for authentication
1169-
pdu_len = current_managed_parameters_struct.max_frame_size - (byte_idx)-sa_ptr->stmacf_len;
1173+
pdu_len = current_managed_parameters_struct.max_frame_size - byte_idx - sa_ptr->stmacf_len;
11701174
if (current_managed_parameters_struct.has_ocf == AOS_HAS_OCF)
11711175
{
11721176
pdu_len -= 4;
@@ -1378,7 +1382,6 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, uint8
13781382
else if (sa_service_type == SA_PLAINTEXT)
13791383
{
13801384
memcpy(p_new_dec_frame + byte_idx, &(p_ingest[byte_idx]), pdu_len);
1381-
byte_idx += pdu_len;
13821385
}
13831386

13841387
#ifdef AOS_DEBUG

src/core/crypto_config.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,9 @@ int32_t Crypto_Init(void)
306306
{
307307
cryptography_if = get_cryptography_interface_custom();
308308
}
309-
if (cryptography_if == NULL)
309+
if (cryptography_if == NULL && cryptography_kmc_crypto_config != NULL)
310310
{ // Note this needs to be the last option in the chain due to addition configuration required
311-
if (cryptography_kmc_crypto_config != NULL)
312-
{
313-
cryptography_if = get_cryptography_interface_kmc_crypto_service();
314-
}
311+
cryptography_if = get_cryptography_interface_kmc_crypto_service();
315312
}
316313
if (cryptography_if == NULL)
317314
{

src/core/crypto_error.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ char *Crypto_Get_Error_Code_Enum_String(int32_t crypto_error_code)
213213
{
214214
return_string = Crypto_Get_Crypto_Error_Code_String(
215215
crypto_error_code, CRYPTO_CORE_ERROR_CODES_MAX,
216-
crypto_enum_errlist_core[(crypto_error_code * (-1))]); // Cryptolib uses negative error return codes.
216+
crypto_enum_errlist_core[crypto_error_code * (-1)]); // Cryptolib uses negative error return codes.
217217
}
218218
return return_string;
219219
}

src/core/crypto_print.c

+59-53
Original file line numberDiff line numberDiff line change
@@ -211,69 +211,75 @@ void Crypto_ccsdsPrint(CCSDS_t *sdls_frame)
211211
void Crypto_saPrint(SecurityAssociation_t *sa)
212212
{
213213
int i;
214-
215-
printf("SA status: \n");
216-
printf("\t spi = %d \n", sa->spi);
217-
printf("\t sa_state = 0x%01x \n", sa->sa_state);
218-
// printf("\t gvcid[0] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[0]);
219-
// printf("\t gvcid[1] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[1]);
220-
// printf("\t gvcid[2] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[2]);
221-
// printf("\t gvcid[3] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[3]);
222-
printf("\t est = 0x%01x \n", sa->est);
223-
printf("\t ast = 0x%01x \n", sa->ast);
224-
printf("\t shivf_len = %d \n", sa->shivf_len);
225-
printf("\t shsnf_len = %d \n", sa->shsnf_len);
226-
printf("\t shplf_len = %d \n", sa->shplf_len);
227-
printf("\t stmacf_len = %d \n", sa->stmacf_len);
228-
printf("\t ecs_len = %d \n", sa->ecs_len);
229-
if (sa->ecs_len > 0)
214+
if (sa->spi < NUM_SA)
230215
{
231-
for (i = 0; i < sa->ecs_len; i++)
216+
printf("SA status: \n");
217+
printf("\t spi = %d \n", sa->spi);
218+
printf("\t sa_state = 0x%01x \n", sa->sa_state);
219+
// printf("\t gvcid[0] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[0]);
220+
// printf("\t gvcid[1] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[1]);
221+
// printf("\t gvcid[2] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[2]);
222+
// printf("\t gvcid[3] = 0x%02x \n", sa->gvcid_blk[spi].gvcid[3]);
223+
printf("\t est = 0x%01x \n", sa->est);
224+
printf("\t ast = 0x%01x \n", sa->ast);
225+
printf("\t shivf_len = %d \n", sa->shivf_len);
226+
printf("\t shsnf_len = %d \n", sa->shsnf_len);
227+
printf("\t shplf_len = %d \n", sa->shplf_len);
228+
printf("\t stmacf_len = %d \n", sa->stmacf_len);
229+
printf("\t ecs_len = %d \n", sa->ecs_len);
230+
if (sa->ecs_len > 0)
232231
{
233-
printf("\t ecs[%d] = 0x%02x \n", i, (sa->ecs + i));
232+
for (i = 0; i < sa->ecs_len; i++)
233+
{
234+
printf("\t ecs[%d] = 0x%02x \n", i, (sa->ecs + i));
235+
}
234236
}
235-
}
236-
printf("\t ekid = %d \n", sa->ekid);
237-
printf("\t ek_ref = %s \n", sa->ek_ref);
238-
printf("\t akid = %d \n", sa->akid);
239-
printf("\t ak_ref = %s \n", sa->ak_ref);
240-
printf("\t iv_len = %d \n", sa->iv_len);
241-
if (sa->iv_len > 0)
242-
{
243-
for (i = 0; i < sa->iv_len; i++)
237+
printf("\t ekid = %d \n", sa->ekid);
238+
printf("\t ek_ref = %s \n", sa->ek_ref);
239+
printf("\t akid = %d \n", sa->akid);
240+
printf("\t ak_ref = %s \n", sa->ak_ref);
241+
printf("\t iv_len = %d \n", sa->iv_len);
242+
if (sa->iv_len > 0)
244243
{
245-
printf("\t iv[%d] = 0x%02x \n", i, *(sa->iv + i));
244+
for (i = 0; i < sa->iv_len; i++)
245+
{
246+
printf("\t iv[%d] = 0x%02x \n", i, *(sa->iv + i));
247+
}
246248
}
247-
}
248-
else
249-
{
250-
printf("\t iv = %s \n", sa->iv);
251-
}
252-
printf("\t acs_len = %d \n", sa->acs_len);
253-
printf("\t acs = 0x%02x \n", sa->acs);
254-
printf("\t abm_len = %d \n", sa->abm_len);
255-
if (sa->abm_len > 0)
256-
{
257-
printf("\t abm = ");
258-
for (i = 0; i < sa->abm_len; i++)
249+
else
259250
{
260-
printf("%02x", *(sa->abm + i));
251+
printf("\t iv = %s \n", sa->iv);
261252
}
262-
printf("\n");
263-
}
264-
printf("\t arsn_len = %d \n", sa->arsn_len);
265-
if (sa->arsn_len > 0)
266-
{
267-
printf("\t arsn = ");
268-
for (i = 0; i < sa->arsn_len; i++)
253+
printf("\t acs_len = %d \n", sa->acs_len);
254+
printf("\t acs = 0x%02x \n", sa->acs);
255+
printf("\t abm_len = %d \n", sa->abm_len);
256+
if (sa->abm_len > 0)
269257
{
270-
printf("%02x", *(sa->arsn + i));
258+
printf("\t abm = ");
259+
for (i = 0; i < sa->abm_len; i++)
260+
{
261+
printf("%02x", *(sa->abm + i));
262+
}
263+
printf("\n");
264+
}
265+
printf("\t arsn_len = %d \n", sa->arsn_len);
266+
if (sa->arsn_len > 0)
267+
{
268+
printf("\t arsn = ");
269+
for (i = 0; i < sa->arsn_len; i++)
270+
{
271+
printf("%02x", *(sa->arsn + i));
272+
}
273+
printf("\n");
271274
}
272-
printf("\n");
273-
}
274275

275-
printf("\t arsnw_len = %d \n", sa->arsnw_len);
276-
printf("\t arsnw = %d \n", sa->arsnw);
276+
printf("\t arsnw_len = %d \n", sa->arsnw_len);
277+
printf("\t arsnw = %d \n", sa->arsnw);
278+
}
279+
else
280+
{
281+
printf("SPI %d does not exist\n", sa->spi);
282+
}
277283
}
278284

279285
/**

src/crypto/kmc/cryptography_interface_kmc_crypto_service.template.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ static int32_t cryptography_encrypt(uint8_t *data_out, size_t len_data_out, uint
395395
uint8_t ciphertext_found = CRYPTO_FALSE;
396396
char *ciphertext_base64 = NULL;
397397
char *ciphertext_IV_base64 = NULL;
398+
uint8_t *save_ptr;
398399
for (json_idx = 1; json_idx < parse_result; json_idx++)
399400
{
400401
if (jsoneq(chunk_write->response, &t[json_idx], "metadata") == 0)
@@ -407,15 +408,15 @@ static int32_t cryptography_encrypt(uint8_t *data_out, size_t len_data_out, uint
407408
char *line;
408409
char *token;
409410
char temp_buff[256];
410-
for (line = strtok(ciphertext_IV_base64, ","); line != NULL; line = strtok(line + strlen(line) + 1, ","))
411+
for (line = __strtok_r(ciphertext_IV_base64, ",", save_ptr); line != NULL; line = __strtok_r(line + strlen(line) + 1, ",", save_ptr))
411412
{
412413
strncpy(temp_buff, line, sizeof(temp_buff));
413414

414-
for (token = strtok(temp_buff, ":"); token != NULL; token = strtok(token + strlen(token) + 1, ":"))
415+
for (token = __strtok_r(temp_buff, ":", save_ptr); token != NULL; token = strtok_r(token + strlen(token) + 1, ":", save_ptr))
415416
{
416417
if (strcmp(token, "initialVector") == 0)
417418
{
418-
token = strtok(token + strlen(token) + 1, ":");
419+
token = __strtok_r(token + strlen(token) + 1, ":", save_ptr);
419420
char *ciphertext_token_base64 = malloc(strlen(token));
420421
size_t cipher_text_token_len = strlen(token);
421422
memcpy(ciphertext_token_base64, token, cipher_text_token_len);
@@ -1356,10 +1357,11 @@ static int32_t cryptography_aead_encrypt(uint8_t *data_out, size_t len_data_out,
13561357
return status;
13571358
}
13581359

1359-
int json_idx = 0;
1360-
uint8_t ciphertext_found = CRYPTO_FALSE;
1361-
char *ciphertext_base64 = NULL;
1362-
char *ciphertext_IV_base64 = NULL;
1360+
int json_idx = 0;
1361+
uint8_t ciphertext_found = CRYPTO_FALSE;
1362+
char *ciphertext_base64 = NULL;
1363+
char *ciphertext_IV_base64 = NULL;
1364+
uint8_t *save_ptr;
13631365
for (json_idx = 1; json_idx < parse_result; json_idx++)
13641366
{
13651367
if (jsoneq(chunk_write->response, &t[json_idx], "metadata") == 0)
@@ -1373,15 +1375,15 @@ static int32_t cryptography_aead_encrypt(uint8_t *data_out, size_t len_data_out,
13731375
char *line;
13741376
char *token;
13751377
char temp_buff[256];
1376-
for (line = strtok(ciphertext_IV_base64, ","); line != NULL; line = strtok(line + strlen(line) + 1, ","))
1378+
for (line = __strtok_r(ciphertext_IV_base64, ",", save_ptr); line != NULL; line = __strtok_r(line + strlen(line) + 1, ",", save_ptr))
13771379
{
13781380
strncpy(temp_buff, line, sizeof(temp_buff));
13791381

1380-
for (token = strtok(temp_buff, ":"); token != NULL; token = strtok(token + strlen(token) + 1, ":"))
1382+
for (token = __strtok_r(temp_buff, ":", save_ptr); token != NULL; token = __strtok_r(token + strlen(token) + 1, ":", save_ptr))
13811383
{
13821384
if (strcmp(token, "initialVector") == 0)
13831385
{
1384-
token = strtok(token + strlen(token) + 1, ":");
1386+
token = __strtok_r(token + strlen(token) + 1, ":", save_ptr);
13851387
char *ciphertext_token_base64 = malloc(strlen(token));
13861388
size_t cipher_text_token_len = strlen(token);
13871389
memcpy(ciphertext_token_base64, token, cipher_text_token_len);

0 commit comments

Comments
 (0)