From c10e5a22e4d5db70dce84dd6539561ed175ae74a Mon Sep 17 00:00:00 2001 From: Joel Guittet Date: Mon, 27 May 2024 11:17:16 +0200 Subject: [PATCH] client: allow custom identity instead of using mac address by default --- core/src/mender-api.c | 44 +++++++++++++++++++++++++++++----------- core/src/mender-client.c | 12 +++++++---- include/mender-api.h | 10 ++++----- include/mender-client.h | 16 +++++++-------- tests/src/main.c | 3 ++- 5 files changed, 55 insertions(+), 30 deletions(-) mode change 100755 => 100644 include/mender-api.h diff --git a/core/src/mender-api.c b/core/src/mender-api.c index c8afe55..589c657 100644 --- a/core/src/mender-api.c +++ b/core/src/mender-api.c @@ -100,7 +100,7 @@ mender_err_t mender_api_init(mender_api_config_t *config) { assert(NULL != config); - assert(NULL != config->mac_address); + assert(NULL != config->identity); assert(NULL != config->artifact_name); assert(NULL != config->device_type); assert(NULL != config->host); @@ -130,6 +130,9 @@ mender_err_t mender_api_perform_authentication(void) { mender_err_t ret; + cJSON * json_identity = NULL; + char * tmp = NULL; + char * identity = NULL; char * public_key_pem = NULL; char * payload = NULL; char * response = NULL; @@ -143,29 +146,40 @@ mender_api_perform_authentication(void) { goto END; } + /* Format identity */ + if (MENDER_OK != (ret = mender_utils_keystore_to_json(mender_api_config.identity, &json_identity))) { + mender_log_error("Unable to format identity"); + goto END; + } + if (NULL == (identity = cJSON_PrintUnformatted(json_identity))) { + mender_log_error("Unable to allocate memory"); + ret = MENDER_FAIL; + goto END; + } + if (NULL == (tmp = mender_utils_str_replace(identity, "\"", "\\\""))) { + mender_log_error("Unable to allocate memory"); + ret = MENDER_FAIL; + goto END; + } + identity = tmp; + /* Format payload */ if (NULL != mender_api_config.tenant_token) { if (NULL - == (payload = (char *)malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\", \"tenant_token\": \"\" }") - + strlen(mender_api_config.mac_address) + strlen(public_key_pem) + strlen(mender_api_config.tenant_token) + 1))) { + == (payload = (char *)malloc(strlen("{ \"id_data\": \"\", \"pubkey\": \"\", \"tenant_token\": \"\" }") + strlen(identity) + strlen(public_key_pem) + + strlen(mender_api_config.tenant_token) + 1))) { mender_log_error("Unable to allocate memory"); ret = MENDER_FAIL; goto END; } - sprintf(payload, - "{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\", \"tenant_token\": \"%s\" }", - mender_api_config.mac_address, - public_key_pem, - mender_api_config.tenant_token); + sprintf(payload, "{ \"id_data\": \"%s\", \"pubkey\": \"%s\", \"tenant_token\": \"%s\" }", identity, public_key_pem, mender_api_config.tenant_token); } else { - if (NULL - == (payload = (char *)malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\" }") + strlen(mender_api_config.mac_address) - + strlen(public_key_pem) + 1))) { + if (NULL == (payload = (char *)malloc(strlen("{ \"id_data\": \"\", \"pubkey\": \"\" }") + strlen(identity) + strlen(public_key_pem) + 1))) { mender_log_error("Unable to allocate memory"); ret = MENDER_FAIL; goto END; } - sprintf(payload, "{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\" }", mender_api_config.mac_address, public_key_pem); + sprintf(payload, "{ \"id_data\": \"%s\", \"pubkey\": \"%s\" }", identity, public_key_pem); } /* Sign payload */ @@ -224,6 +238,12 @@ mender_api_perform_authentication(void) { if (NULL != public_key_pem) { free(public_key_pem); } + if (NULL != identity) { + free(identity); + } + if (NULL != json_identity) { + cJSON_Delete(json_identity); + } return ret; } diff --git a/core/src/mender-client.c b/core/src/mender-client.c index aaf441b..b0a0d00 100644 --- a/core/src/mender-client.c +++ b/core/src/mender-client.c @@ -202,7 +202,7 @@ mender_err_t mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *callbacks) { assert(NULL != config); - assert(NULL != config->mac_address); + assert(NULL != config->identity); assert(NULL != config->artifact_name); assert(NULL != config->device_type); assert(NULL != callbacks); @@ -210,7 +210,10 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca mender_err_t ret; /* Save configuration */ - mender_client_config.mac_address = config->mac_address; + if (MENDER_OK != (ret = mender_utils_keystore_copy(&mender_client_config.identity, config->identity))) { + mender_log_error("Unable to copy identity"); + goto END; + } mender_client_config.artifact_name = config->artifact_name; mender_client_config.device_type = config->device_type; if ((NULL != config->host) && (strlen(config->host) > 0)) { @@ -269,7 +272,7 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca goto END; } mender_api_config_t mender_api_config = { - .mac_address = mender_client_config.mac_address, + .identity = mender_client_config.identity, .artifact_name = mender_client_config.artifact_name, .device_type = mender_client_config.device_type, .host = mender_client_config.host, @@ -380,7 +383,8 @@ mender_client_exit(void) { mender_scheduler_exit(); /* Release memory */ - mender_client_config.mac_address = NULL; + mender_utils_keystore_delete(mender_client_config.identity); + mender_client_config.identity = NULL; mender_client_config.artifact_name = NULL; mender_client_config.device_type = NULL; mender_client_config.host = NULL; diff --git a/include/mender-api.h b/include/mender-api.h old mode 100755 new mode 100644 index a421119..d016ada --- a/include/mender-api.h +++ b/include/mender-api.h @@ -38,11 +38,11 @@ extern "C" { * @brief Mender API configuration */ typedef struct { - char *mac_address; /**< MAC address of the device */ - char *artifact_name; /**< Artifact name */ - char *device_type; /**< Device type */ - char *host; /**< URL of the mender server */ - char *tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */ + mender_keystore_t *identity; /**< Identity of the device */ + char * artifact_name; /**< Artifact name */ + char * device_type; /**< Device type */ + char * host; /**< URL of the mender server */ + char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */ } mender_api_config_t; /** diff --git a/include/mender-client.h b/include/mender-client.h index 37e1d7c..9ecd711 100644 --- a/include/mender-client.h +++ b/include/mender-client.h @@ -38,14 +38,14 @@ extern "C" { * @brief Mender client configuration */ typedef struct { - char * mac_address; /**< MAC address of the device */ - char * artifact_name; /**< Artifact name */ - char * device_type; /**< Device type */ - char * host; /**< URL of the mender server */ - char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */ - int32_t authentication_poll_interval; /**< Authentication poll interval, default is 60 seconds, -1 permits to disable periodic execution */ - int32_t update_poll_interval; /**< Update poll interval, default is 1800 seconds, -1 permits to disable periodic execution */ - bool recommissioning; /**< Used to force creation of new authentication keys */ + mender_keystore_t *identity; /**< Identity of the device */ + char * artifact_name; /**< Artifact name */ + char * device_type; /**< Device type */ + char * host; /**< URL of the mender server */ + char * tenant_token; /**< Tenant token used to authenticate on the mender server (optional) */ + int32_t authentication_poll_interval; /**< Authentication poll interval, default is 60 seconds, -1 permits to disable periodic execution */ + int32_t update_poll_interval; /**< Update poll interval, default is 1800 seconds, -1 permits to disable periodic execution */ + bool recommissioning; /**< Used to force creation of new authentication keys */ } mender_client_config_t; /** diff --git a/tests/src/main.c b/tests/src/main.c index 00ee5a2..fa27022 100644 --- a/tests/src/main.c +++ b/tests/src/main.c @@ -358,7 +358,8 @@ main(int argc, char **argv) { } /* Initialize mender-client */ - mender_client_config_t mender_client_config = { .mac_address = mac_address, + mender_keystore_t identity[] = { { .name = "mac", .value = mac_address }, { .name = NULL, .value = NULL } }; + mender_client_config_t mender_client_config = { .identity = identity, .artifact_name = artifact_name, .device_type = device_type, .host = NULL,