From 5419ff74eeb43298ca234ee7ba65925fba6e5d2a Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 13 Jan 2023 18:07:50 +0100 Subject: [PATCH] Using file_has_data() to check contents. Signed-off-by: Pol Henarejos --- src/hsm/cmd_derive_asym.c | 2 +- src/hsm/cmd_external_authenticate.c | 2 +- src/hsm/cmd_initialize.c | 2 +- src/hsm/cmd_mse.c | 2 +- src/hsm/cmd_puk_auth.c | 6 +++--- src/hsm/cmd_reset_retry.c | 2 +- src/hsm/cmd_signature.c | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/hsm/cmd_derive_asym.c b/src/hsm/cmd_derive_asym.c index 34d30b7..3eb2121 100644 --- a/src/hsm/cmd_derive_asym.c +++ b/src/hsm/cmd_derive_asym.c @@ -43,7 +43,7 @@ int cmd_derive_asym() { file_t *fkey; if (!isUserAuthenticated) return SW_SECURITY_STATUS_NOT_SATISFIED(); - if (!(fkey = search_dynamic_file((KEY_PREFIX << 8) | key_id)) || !fkey->data || file_get_size(fkey) == 0) + if (!(fkey = search_dynamic_file((KEY_PREFIX << 8) | key_id)) || !file_has_data(fkey)) return SW_FILE_NOT_FOUND(); if (key_has_purpose(fkey, ALGO_EC_DERIVE) == false) return SW_CONDITIONS_NOT_SATISFIED(); diff --git a/src/hsm/cmd_external_authenticate.c b/src/hsm/cmd_external_authenticate.c index df974e9..c7b9798 100644 --- a/src/hsm/cmd_external_authenticate.c +++ b/src/hsm/cmd_external_authenticate.c @@ -32,7 +32,7 @@ int cmd_external_authenticate() { if (apdu.nc == 0) return SW_WRONG_LENGTH(); file_t *ef_puk = search_by_fid(EF_PUKAUT, NULL, SPECIFY_EF); - if (!ef_puk || !ef_puk->data || file_get_size(ef_puk) == 0) + if (!file_has_data(ef_puk)) return SW_FILE_NOT_FOUND(); uint8_t *puk_data = file_get_data(ef_puk); uint8_t *input = (uint8_t *)calloc(dev_name_len+challenge_len, sizeof(uint8_t)), hash[32]; diff --git a/src/hsm/cmd_initialize.c b/src/hsm/cmd_initialize.c index d1b4b29..70057ac 100644 --- a/src/hsm/cmd_initialize.c +++ b/src/hsm/cmd_initialize.c @@ -169,7 +169,7 @@ int cmd_initialize() { if (!fdkey) return SW_EXEC_ERROR(); int ret = 0; - if (ret_mkek != CCID_OK || file_get_size(fdkey) == 0 || file_get_data(fdkey) == NULL) { + if (ret_mkek != CCID_OK || !file_has_data(fdkey)) { mbedtls_ecdsa_context ecdsa; mbedtls_ecdsa_init(&ecdsa); mbedtls_ecp_group_id ec_id = MBEDTLS_ECP_DP_SECP256R1; diff --git a/src/hsm/cmd_mse.c b/src/hsm/cmd_mse.c index c1aa8e4..d0e3a69 100644 --- a/src/hsm/cmd_mse.c +++ b/src/hsm/cmd_mse.c @@ -54,7 +54,7 @@ int cmd_mse() { file_t *ef = search_dynamic_file(EF_PUK+i); if (!ef) break; - if (ef->data == NULL || file_get_size(ef) == 0) + if (!file_has_data(ef)) break; size_t chr_len = 0; const uint8_t *chr = cvc_get_chr(file_get_data(ef), file_get_size(ef), &chr_len); diff --git a/src/hsm/cmd_puk_auth.c b/src/hsm/cmd_puk_auth.c index 48472b3..47e6250 100644 --- a/src/hsm/cmd_puk_auth.c +++ b/src/hsm/cmd_puk_auth.c @@ -22,7 +22,7 @@ int cmd_puk_auth() { uint8_t p1 = P1(apdu), p2 = P2(apdu); file_t *ef_puk = search_by_fid(EF_PUKAUT, NULL, SPECIFY_EF); - if (!ef_puk || !ef_puk->data || file_get_size(ef_puk) == 0) + if (!file_has_data(ef_puk)) return SW_FILE_NOT_FOUND(); uint8_t *puk_data = file_get_data(ef_puk); if (apdu.nc > 0) { @@ -35,7 +35,7 @@ int cmd_puk_auth() { ef = search_dynamic_file(EF_PUK+i); if (!ef) /* Never should not happen */ return SW_MEMORY_FAILURE(); - if (ef->data == NULL || file_get_size(ef) == 0) /* found first empty slot */ + if (!file_has_data(ef)) /* found first empty slot */ break; } uint8_t *tmp = (uint8_t *)calloc(file_get_size(ef_puk), sizeof(uint8_t)); @@ -64,7 +64,7 @@ int cmd_puk_auth() { file_t *ef = search_dynamic_file(EF_PUK+p2); if (!ef) return SW_INCORRECT_P1P2(); - if (ef->data == NULL || file_get_size(ef) == 0) + if (!file_has_data(ef)) return SW_REFERENCE_NOT_FOUND(); size_t chr_len = 0; const uint8_t *chr = cvc_get_chr(file_get_data(ef), file_get_size(ef), &chr_len); diff --git a/src/hsm/cmd_reset_retry.c b/src/hsm/cmd_reset_retry.c index cb4a678..131233e 100644 --- a/src/hsm/cmd_reset_retry.c +++ b/src/hsm/cmd_reset_retry.c @@ -25,7 +25,7 @@ int cmd_reset_retry() { if (!file_sopin || !file_pin1) { return SW_FILE_NOT_FOUND(); } - if (!file_sopin->data) { + if (!file_has_data(file_sopin)) { return SW_REFERENCE_NOT_FOUND(); } uint16_t opts = get_device_options(); diff --git a/src/hsm/cmd_signature.c b/src/hsm/cmd_signature.c index 0aed65a..0ccfd7f 100644 --- a/src/hsm/cmd_signature.c +++ b/src/hsm/cmd_signature.c @@ -94,7 +94,7 @@ int cmd_signature() { file_t *fkey; if (!isUserAuthenticated) return SW_SECURITY_STATUS_NOT_SATISFIED(); - if (!(fkey = search_dynamic_file((KEY_PREFIX << 8) | key_id)) || !fkey->data || file_get_size(fkey) == 0) + if (!(fkey = search_dynamic_file((KEY_PREFIX << 8) | key_id)) || !file_has_data(fkey)) return SW_FILE_NOT_FOUND(); if (get_key_counter(fkey) == 0) return SW_FILE_FULL();