diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 6d53f908..56c47dd3 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -145,23 +145,23 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); } - if((ret = atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { + if ((ret = atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_operation: %d\n", ret); } - if((ret = atclient_notify_params_set_atkey(¶ms, &atkey)) != 0) { + if ((ret = atclient_notify_params_set_atkey(¶ms, &atkey)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_atkey: %d\n", ret); } - if((ret = atclient_notify_params_set_value(¶ms, line)) != 0) { + if ((ret = atclient_notify_params_set_value(¶ms, line)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_value: %d\n", ret); } - if((ret = atclient_notify_params_set_should_encrypt(¶ms, true)) != 0) { + if ((ret = atclient_notify_params_set_should_encrypt(¶ms, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_should_encrypt: %d\n", ret); } - if((ret = atclient_notify_params_set_notification_expiry(¶ms, 5000)) != 0) { + if ((ret = atclient_notify_params_set_notification_expiry(¶ms, 5000)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_notification_expiry: %d\n", ret); } @@ -235,7 +235,7 @@ static void *monitor_handler(void *xargs) { const char *from_atsign = args->from_atsign; pthread_mutex_lock(&monitor_mutex); - if ((ret = atclient_monitor_pkam_authenticate(monitor, atserver_host, atserver_port, atkeys, from_atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(monitor, from_atsign, atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret); goto exit; } @@ -353,7 +353,7 @@ static int reconnect_clients(atclient *monitor, atclient *ctx, const char *atser * 2. Reconnect monitor connection */ atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Reconnecting monitor connection...\n"); - if ((ret = atclient_monitor_pkam_authenticate(monitor, atserver_host, atserver_port, atkeys, from_atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(monitor, from_atsign, atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret); return ret; } diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 99993809..36880b13 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -35,9 +35,6 @@ int main(int argc, char *argv[]) { atclient monitor_conn; atclient_monitor_init(&monitor_conn); - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - atclient_monitor_response message; if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) { @@ -50,12 +47,12 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, &options)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n"); goto exit; } - if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, options.atserver_host, options.atserver_port, &atkeys, atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n"); goto exit; } @@ -122,7 +119,6 @@ exit: { free(atsign); atclient_monitor_free(&monitor_conn); atclient_monitor_response_free(&message); - atclient_pkam_authenticate_options_free(&options); return ret; } } diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index a0275256..4506ee06 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -46,9 +46,6 @@ int main(int argc, char *argv[]) { atclient_notify_params notify_params; atclient_notify_params_init(¬ify_params); - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - const char *homedir; char *atsign_input = NULL; @@ -90,7 +87,7 @@ int main(int argc, char *argv[]) { - if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, &options)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); goto exit; } @@ -139,7 +136,6 @@ int main(int argc, char *argv[]) { exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); - atclient_pkam_authenticate_options_free(&options); atclient_free(&atclient); free(atkeystr); return ret; diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index 3f056216..a2834103 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -40,9 +40,6 @@ int main(int argc, char *argv[]) { atclient monitor_conn; atclient_monitor_init(&monitor_conn); - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - atclient_monitor_response *message = NULL; if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) { @@ -55,12 +52,12 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, &options)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n"); goto exit; } - if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atserver_host, atserver_port, &atkeys, atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n"); goto exit; } @@ -133,7 +130,7 @@ int main(int argc, char *argv[]) { if (tries >= max_tries) { if (!atclient_monitor_is_connected(&monitor_conn)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "We are not connected :( attempting reconnection\n"); - if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atserver_host, atserver_port, &atkeys, atsign)) != + if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n"); continue; @@ -158,7 +155,6 @@ exit: { atclient_monitor_free(&monitor_conn); atclient_monitor_response_free(message); atclient_free(&atclient2); - atclient_pkam_authenticate_options_free(&options); return ret; } } diff --git a/examples/desktop/pkam_authenticate/develop.sh b/examples/desktop/pkam_authenticate/develop.sh index 2d4164b9..044d3c8f 100755 --- a/examples/desktop/pkam_authenticate/develop.sh +++ b/examples/desktop/pkam_authenticate/develop.sh @@ -2,10 +2,7 @@ set -eu FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[0]}")" SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")" -cd "$SCRIPT_DIRECTORY/../../../packages/atclient" -cmake -S . -B build -sudo cmake --build build --target install -pwd +"$SCRIPT_DIRECTORY/../../../tools/install.sh" cd "$SCRIPT_DIRECTORY" cmake -S . -B build cmake --build build --target all diff --git a/examples/desktop/pkam_authenticate/src/main.c b/examples/desktop/pkam_authenticate/src/main.c index 801901ff..9c3dacc6 100644 --- a/examples/desktop/pkam_authenticate/src/main.c +++ b/examples/desktop/pkam_authenticate/src/main.c @@ -1,51 +1,43 @@ #include -#include +#include #include #include #define ROOT_HOST "root.atsign.org" #define ROOT_PORT 64 -#define ATKEYSFILE_PATH "/home/sitaram/.atsign/keys/@actingqualified_key.atKeys" -#define ATSIGN "@actingqualified" +#define ATKEYS_FILE_PATH "/Users/jeremytubongbanua/.atsign/keys/@smoothalligator_key.atKeys" +#define ATSIGN "@smoothalligator" #define TAG "pkam_authenticate" int main(int argc, char **argv) { int ret = 1; - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - // 1a. read `atkeysfile` struct - atclient_atkeysfile atkeysfile; - atclient_atkeysfile_init(&atkeysfile); - ret = atclient_atkeysfile_read(&atkeysfile, ATKEYSFILE_PATH); - - if (ret != 0) { - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_file_read: %d\n", ret); + atclient_atkeysfile atkeys_file; + atclient_atkeysfile_init(&atkeys_file); - // 1b. populate `atkeys` struct atclient_atkeys atkeys; atclient_atkeys_init(&atkeys); - ret = atclient_atkeys_populate_from_atkeysfile(&atkeys, atkeysfile); - - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - if (ret != 0) { + atclient atclient; + atclient_init(&atclient); + + if ((ret = atclient_atkeysfile_from_path(&atkeys_file, ATKEYS_FILE_PATH)) != 0) { goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeysfile: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeysfile_read: %d\n", ret); - // 2. pkam auth - atclient atclient; - atclient_init(&atclient); + if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) { + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret); const char *atsign = ATSIGN; - - if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, &options)) != 0) { + + if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); goto exit; } else { @@ -55,10 +47,9 @@ int main(int argc, char **argv) { goto exit; exit: { - atclient_atkeysfile_free(&atkeysfile); + atclient_atkeysfile_free(&atkeys_file); atclient_atkeys_free(&atkeys); atclient_free(&atclient); - atclient_pkam_authenticate_options_free(&options); return 0; } -} \ No newline at end of file +} diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 8c65fe99..067c0344 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -23,9 +23,9 @@ int main(int argc, char *argv[]) { atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - const short atkeysfilepathsize = 256; - char atkeysfilepath[atkeysfilepathsize]; - memset(atkeysfilepath, 0, sizeof(char) * atkeysfilepathsize); // Clear the buffer (for safety) + const short atkeys_filepathsize = 256; + char atkeys_filepath[atkeys_filepathsize]; + memset(atkeys_filepath, 0, sizeof(char) * atkeys_filepathsize); // Clear the buffer (for safety) const size_t buffersize = 2048; char buffer[buffersize]; @@ -79,12 +79,12 @@ int main(int argc, char *argv[]) { goto exit; } - sprintf(atkeysfilepath, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign); - if ((ret = atclient_atkeys_populate_from_path(&atkeys, atkeysfilepath)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read atKeys file at path \"%s\"\n", atkeysfilepath); + sprintf(atkeys_filepath, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign); + if ((ret = atclient_atkeys_populate_from_path(&atkeys, atkeys_filepath)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read atKeys file at path \"%s\"\n", atkeys_filepath); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Successfully read atKeys file at path %s\n", atkeysfilepath); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Successfully read atKeys file at path %s\n", atkeys_filepath); ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, &options); if (ret != 0) { diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 87bf173b..c9178a39 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -17,7 +17,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atclient.c ${CMAKE_CURRENT_LIST_DIR}/src/atkey.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c - ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c + ${CMAKE_CURRENT_LIST_DIR}/src/atkeys_file.c ${CMAKE_CURRENT_LIST_DIR}/src/atnotification.c ${CMAKE_CURRENT_LIST_DIR}/src/connection_hooks.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c @@ -49,11 +49,11 @@ if(ESP_PLATFORM) TARGET ${COMPONENT_LIB} POST_BUILD COMMAND - ${CMAKE_COMMAND} -E copy_directory ${ATCLIENT_INCLUDE_DIR} - ${CMAKE_SOURCE_DIR}/include + ${CMAKE_COMMAND} -E copy_directory ${ATCLIENT_INCLUDE_DIR} + ${CMAKE_SOURCE_DIR}/include COMMAND - ${CMAKE_COMMAND} -E copy $ - ${CMAKE_SOURCE_DIR}/lib/lib${COMPONENT_NAME}.a + ${CMAKE_COMMAND} -E copy $ + ${CMAKE_SOURCE_DIR}/lib/lib${COMPONENT_NAME}.a COMMENT "Copying built archive file and header to lib directory..." ) endif() @@ -111,9 +111,9 @@ if(NOT ESP_PLATFORM) target_include_directories( ${PROJECT_NAME} PUBLIC - $ - $ - $ + $ + $ + $ ) # Link dependencies to library targets @@ -150,9 +150,8 @@ if(NOT ESP_PLATFORM) ) # EXPORT - if(NOT ATCLIENT_AS_SUBPROJECT) + if(NOT ATCLIENT_AS_SUBPROJECT AND ATCLIENT_EXPORT) # Export the library - export(PACKAGE ${PROJECT_NAME}) # install as a config.cmake install( diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index bb3afc57..d3813b26 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -1,7 +1,7 @@ #ifndef ATCLIENT_ATKEYS_H #define ATCLIENT_ATKEYS_H -#include "atclient/atkeysfile.h" +#include "atclient/atkeys_file.h" #include #include #include @@ -13,12 +13,14 @@ #define ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX 0 #define ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX 0 #define ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_ATKEYS_ENROLLMENT_ID_INDEX 0 #define ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED (VALUE_INITIALIZED << 1) #define ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED (VALUE_INITIALIZED << 2) #define ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED (VALUE_INITIALIZED << 3) #define ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEYS_ENROLLMENT_ID_INITIALIZED (VALUE_INITIALIZED << 5) /** * @brief represents the atkeys file @@ -32,20 +34,22 @@ * 4. (for rsakeys), the rsakey struct used in rsa operations. */ typedef struct atclient_atkeys { - char *pkam_public_key_base64; // base64 encoded, RSA-2048 key, decrypted + char *pkam_public_key_base64; // base64 encoded, RSA-2048 key, decrypted atchops_rsa_key_public_key pkam_public_key; // contains n, e - char *pkam_private_key_base64; // base64 encoded, RSA-2048 key, decrypted + char *pkam_private_key_base64; // base64 encoded, RSA-2048 key, decrypted atchops_rsa_key_private_key pkam_private_key; // conatins n, e, d, p, q - char *encrypt_public_key_base64; // base64 encoded, RSA-2048 key, decrypted + char *encrypt_public_key_base64; // base64 encoded, RSA-2048 key, decrypted atchops_rsa_key_public_key encrypt_public_key; // contains n, e - char *encrypt_private_key_base64; // base64 encoded, RSA-2048 key, decrypted + char *encrypt_private_key_base64; // base64 encoded, RSA-2048 key, decrypted atchops_rsa_key_private_key encrypt_private_key; // conatins n, e, d, p, q char *self_encryption_key_base64; // base64 encoded, AES-256 key, decrypted + char *enrollment_id; + uint8_t _initialized_fields[1]; // used to track which fields have been initialized } atclient_atkeys; @@ -64,46 +68,51 @@ void atclient_atkeys_init(atclient_atkeys *atkeys); void atclient_atkeys_free(atclient_atkeys *atkeys); int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkampublickeybase64len); + const size_t pkampublickeybase64len); int atclient_atkeys_set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkamprivatekeybase64len); + const size_t pkamprivatekeybase64len); int atclient_atkeys_set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encryptpublickeybase64len); + const size_t encryptpublickeybase64len); int atclient_atkeys_set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encryptprivatekeybase64len); + const size_t encryptprivatekeybase64len); int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, - const size_t selfencryptionkeybase64len); + const size_t selfencryptionkeybase64len); + +int atclient_atkeys_set_enrollment_id(atclient_atkeys *atkeys, const char *enrollment_id, const size_t enrollment_id_len); int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkampublickeybase64len); + const size_t pkampublickeybase64len); int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkamprivatekeybase64len); + const size_t pkamprivatekeybase64len); int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encryptpublickeybase64len); + const size_t encryptpublickeybase64len); int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encryptprivatekeybase64len); + const size_t encryptprivatekeybase64len); bool atclient_atkeys_is_pkam_public_key_base64_initialized(atclient_atkeys *atkeys); bool atclient_atkeys_is_pkam_private_key_base64_initialized(atclient_atkeys *atkeys); bool atclient_atkeys_is_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys); bool atclient_atkeys_is_encrypt_private_key_base64_initialized(atclient_atkeys *atkeys); bool atclient_atkeys_is_self_encryption_key_base64_initialized(atclient_atkeys *atkeys); +bool atclient_atkeys_is_enrollment_id_initialized(atclient_atkeys *atkeys); /** * @brief populates the struct by decrypting the encrypted RSA keys passed. It is assumed that the passed strings are * encrypted RSA keys that were in base64 format. * * @param atkeys the struct to populate, assumed to be intialized with atclient_atkeys_init - * @param aes_pkam_public_key_str the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 format + * @param aes_pkam_public_key_str the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 + * format * @param aes_pkam_public_key_len the length of the aes_pkam_public_key_str buffer - * @param aes_pkam_private_key_str the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 format + * @param aes_pkam_private_key_str the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 + * format * @param aes_pkam_private_key_len the length of the aes_pkam_private_key_str buffer * @param aes_encrypt_public_key_str the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 * format @@ -118,28 +127,39 @@ bool atclient_atkeys_is_self_encryption_key_base64_initialized(atclient_atkeys * int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aes_pkam_public_key_str, const size_t aes_pkam_public_key_len, const char *aes_pkam_private_key_str, const size_t aes_pkam_private_key_len, const char *aes_encrypt_public_key_str, - const size_t aes_encrypt_public_key_len, const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_public_key_len, + const char *aes_encrypt_private_key_str, const size_t aes_encrypt_private_key_len, const char *self_encryption_key_str, - const size_t self_encryption_key_len); + const size_t self_encryption_key_len, const char *enrollment_id_str, const size_t enrollment_id_str_len); /** * @brief populates the struct by decrypting the encrypted RSA keys found in a populated atclient_atkeysfile struct * - * @param atkeys the struct to populate - * @param atkeysfile the struct containing the encrypted RSA keys, typically already read from the *.atKeys file + * @param atkeys The struct to populate, assumed to be NON-NULL and initialized with atclient_atkeys_init + * @param atkeys_file the struct containing the encrypted RSA keys, typically already read from the *.atKeys file * @return int 0 on success, non-zero on failure */ -int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atclient_atkeysfile atkeysfile); +int atclient_atkeys_populate_from_atkeys_file(atclient_atkeys *atkeys, const atclient_atkeysfile *atkeys_file); /** * @brief populates the atkeys struct by reading the *.atKeys file, * decrypting the RSA keys found in it, and populating the struct with the * decrypted keys * - * @param atkeys the struct to populate + * @param atkeys The struct to populate, assumed to be NON-NULL and initialized with atclient_atkeys_init * @param path the path to the *.atKeys file * @return int 0 on success, non-zero on failure */ int atclient_atkeys_populate_from_path(atclient_atkeys *atkeys, const char *path); +/** + * @brief Populates the atkeys struct by providing the string that you would have read from the atkeys file. This file + * is useful in case your atKeys are in memory, which is common in embedded devices. + * + * @param atkeys The struct to populate, assumed to be NON-NULL and initialized with atclient_atkeys_init + * @param file_string the string that would have been read from `_key.atKeys` file + * @return int 0 on success + */ +int atclient_atkeys_populate_from_string(atclient_atkeys *atkeys, const char *file_string); + #endif diff --git a/packages/atclient/include/atclient/atkeys_file.h b/packages/atclient/include/atclient/atkeys_file.h new file mode 100644 index 00000000..e623cbbe --- /dev/null +++ b/packages/atclient/include/atclient/atkeys_file.h @@ -0,0 +1,87 @@ +#ifndef ATCLIENT_atkeys_file_H +#define ATCLIENT_atkeys_file_H + +#include +#include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INDEX 0 +#define ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INDEX 0 +#define ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX 0 +#define ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX 0 +#define ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INDEX 0 +#define ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX 0 + +#define ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INITIALIZED (VALUE_INITIALIZED << 5) + +typedef struct atclient_atkeysfile { + char *aes_pkam_public_key_str; // encrypted with self encryption key. AES decryption with self encryption key will + // reveal base64-encoded RSA key + char *aes_pkam_private_key_str; // encrypted with self encryption key. AES decryption with self encryption key will + // reveal base64-encoded RSA keyF + char *aes_encrypt_public_key_str; // encrypted with self encryption key. AES decryption with self encryption key will + // reveal base64-encoded RSA key + char *aes_encrypt_private_key_str; // encrypted with self encryption key. AES decryption with self encryption key will + // reveal base64-encoded RSA key + char *self_encryption_key_str; // base64-encoded non-encrypted self encryption key. base64 decoding will reveal + // 32-byte AES key + char *enrollment_id_str; + uint8_t _initialized_fields[1]; +} atclient_atkeysfile; + +/** + * @brief Initialize the struct. This function does not allocate the struct, but manages its memory internally. This + * function should be called before any subsequent calls. + * + * @param atkeys_file the allocated struct to be initialized. + */ +void atclient_atkeysfile_init(atclient_atkeysfile *atkeys_file); + +/** + * @brief Read from a `_key.atKeys` file path. + * + * @param atkeys_file the struct to be populated, assumed to be NON-NULL and initialized with atclient_atkeysfile_init + * @param path Example "$HOME/.atsign/keys/@alice_key.atKeys" + * @return int + */ +int atclient_atkeysfile_from_path(atclient_atkeysfile *atkeys_file, const char *path); + +/** + * @brief Read from a string. You would typically read the file first and then call this function to populate your + * *atkeys_file struct. + * + * @param atkeys_file the struct to be populated, assumed to be NON-NULL and initialized with atclient_atkeysfile_init + * @param file_string the string that is read from the `_key.atKeys` file. + * @return int + */ +int atclient_atkeysfile_from_string(atclient_atkeysfile *atkeys_file, const char *file_string); + +/** + * @brief Free the struct of any memory that was allocated during its lifetime + * + * @param atkeys_file the struct to be populated, assumed to be NON-NULL and initialized with atclient_atkeysfile_init + */ +void atclient_atkeysfile_free(atclient_atkeysfile *atkeys_file); + +bool atclient_atkeysfile_is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file); +bool atclient_atkeysfile_is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file); +bool atclient_atkeysfile_is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file); +bool atclient_atkeysfile_is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file); +bool atclient_atkeysfile_is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file); +bool atclient_atkeysfile_is_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file); + +int atclient_atkeysfile_set_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_public_key_str, const size_t aes_pkam_public_key_str_len); +int atclient_atkeysfile_set_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_private_key_str, const size_t aes_pkam_private_key_str_len); +int atclient_atkeysfile_set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_public_key_str, const size_t aes_encrypt_public_key_str_len); +int atclient_atkeysfile_set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_private_key_str, const size_t aes_encrypt_private_key_str_len); +int atclient_atkeysfile_set_self_encryption_key_str(atclient_atkeysfile *atkeys_file, const char *self_encryption_key_str, const size_t self_encryption_key_str_len); +int atclient_atkeysfile_set_enrollment_id_str(atclient_atkeysfile *atkeys_file, const char *enrollment_id_str, const size_t enrollment_id_str_len); + +#endif diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h deleted file mode 100644 index e25bb82d..00000000 --- a/packages/atclient/include/atclient/atkeysfile.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef ATCLIENT_ATKEYSFILE_H -#define ATCLIENT_ATKEYSFILE_H - -#include -#include - -#define VALUE_INITIALIZED 0b00000001 - -#define ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX 0 - -#define ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 4) - -typedef struct atclient_atkeysfile { - char *aes_pkam_public_key_str; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *aes_pkam_private_key_str; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA keyF - char *aes_encrypt_public_key_str; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *aes_encrypt_private_key_str; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *self_encryption_key_str; // base64-encoded non-encrypted self encryption key. base64 decoding will reveal 32-byte AES key - uint8_t _initialized_fields[1]; -} atclient_atkeysfile; - -void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile); -int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path); -void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile); - -#endif diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 179ba72f..8fefa33a 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -2,7 +2,6 @@ #define ATCLIENT_MONITOR_H #include "atclient/atclient.h" -#include "atclient/atkey.h" #include "atclient/atnotification.h" #include @@ -95,16 +94,13 @@ void atclient_monitor_free(atclient *monitor_conn); * @brief pkam authenticates the monitor connection * * @param monitor_conn the atclient context for the monitor connection, assumed that it is already initialized - * @param atserver_host the atserver host to use for the pkam authentication, see atclient_find_secondary_address for - * typical method to find this value. - * @param atserver_port the atserver port to use for the pkam authentication, see atclient_find_secondary_address for - * typical method to find this value. - * @param atkeys the atkeys to use for the pkam authentication * @param atsign the atsign to use for the pkam authentication + * @param atkeys the atkeys to use for the pkam authentication + * @param options the options to use for the pkam authentication * @return int 0 on success */ -int atclient_monitor_pkam_authenticate(atclient *monitor_conn, const char *atserver_host, const int atserver_port, - const atclient_atkeys *atkeys, const char *atsign); +int atclient_monitor_pkam_authenticate(atclient *monitor_conn, const char *atsign, const atclient_atkeys *atkeys, + atclient_pkam_authenticate_options *options); /** * @brief Set how long `atclient_monitor_read` should wait for a message before timing out @@ -141,7 +137,8 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex); * @note Message may be a notification, a data response, or an error response, check the type field to determine which * data field to use */ -int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message, atclient_monitor_hooks *hooks); +int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message, + atclient_monitor_hooks *hooks); /** * @brief Check if the monitor connection is still established (client is listening for notifications, and the server diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index 3d31225c..b26e1faf 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -121,10 +121,9 @@ typedef struct atclient_get_atkeys_request_options { /* * 5. Pkam auhtenticate Request Options */ - typedef struct atclient_pkam_authenticate_options { - char *at_directory_host; - int at_directory_port; + char *atdirectory_host; + int atdirectory_port; char *atserver_host; int atserver_port; uint16_t _initialized_fields[1]; @@ -250,12 +249,12 @@ void atclient_get_atkeys_request_options_unset_show_hidden(atclient_get_atkeys_r void atclient_pkam_authenticate_options_init(atclient_pkam_authenticate_options *options); void atclient_pkam_authenticate_options_free(atclient_pkam_authenticate_options *options); -bool atclient_pkam_authenticate_options_is_at_directory_host_initialized(const atclient_pkam_authenticate_options *options); -int atclient_pkam_authenticate_options_set_at_directory_host(atclient_pkam_authenticate_options *options, char *at_directory_host); +bool atclient_pkam_authenticate_options_is_atdirectory_host_initialized(const atclient_pkam_authenticate_options *options); +int atclient_pkam_authenticate_options_set_at_directory_host(atclient_pkam_authenticate_options *options, char *atdirectory_host); void atclient_pkam_authenticate_options_unset_at_directory_host(atclient_pkam_authenticate_options *options); -bool atclient_pkam_authenticate_options_is_at_directory_port_initialized(const atclient_pkam_authenticate_options *options); -int atclient_pkam_authenticate_options_set_at_directory_port(atclient_pkam_authenticate_options *options, int at_directory_port); +bool atclient_pkam_authenticate_options_is_atdirectory_port_initialized(const atclient_pkam_authenticate_options *options); +int atclient_pkam_authenticate_options_set_at_directory_port(atclient_pkam_authenticate_options *options, int atdirectory_port); void atclient_pkam_authenticate_options_unset_at_directory_port(atclient_pkam_authenticate_options *options); bool atclient_pkam_authenticate_options_is_atserver_host_initialized(const atclient_pkam_authenticate_options *options); diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index e42f6e60..cac2444b 100755 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -7,10 +7,8 @@ #include "atclient/connection.h" #include "atclient/connection_hooks.h" #include "atclient/constants.h" -#include "atclient/mbedtls.h" #include "atclient/request_options.h" #include "atclient/string_utils.h" -#include "atclient/request_options.h" #include "atlogger/atlogger.h" #include #include @@ -59,7 +57,7 @@ void atclient_free(atclient *ctx) { atclient_unset_atsign(ctx); } - if(atclient_is_atserver_connection_started(ctx)) { + if (atclient_is_atserver_connection_started(ctx)) { atclient_stop_atserver_connection(ctx); } } @@ -191,10 +189,6 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient atclient_pkam_authenticate_options *options) { int ret = 1; // error by default - if(options == NULL){ - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - } /* * 1. Validate arguments @@ -204,16 +198,6 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient return ret; } - /* - * 1.1 If atserver_host and atserver_port are null and 0 respectively, then fetch the server host and port. - */ - if (options->atserver_host == NULL && options->atserver_port == 0) { - if (atclient_utils_find_atserver_address(options->at_directory_host, options->at_directory_port, atsign, - &(options->atserver_host), &(options->atserver_port)) != 0) { - return ret; - } - } - /* * 2. Initialize variables */ @@ -224,6 +208,9 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient char *pkam_cmd = NULL; char *atsign_with_at = NULL; + char *atserver_host = NULL; + int atserver_port = 0; + const size_t recvsize = 1024; // sufficient buffer size to receive 1. host & port from atDirectory, 2. challenge from // `from:` noop_cmd, 3. pkam success message from `pkam:` noop_cmd unsigned char recv[recvsize]; @@ -254,24 +241,44 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient const char *atsign_without_at = (atsign_with_at + 1); - // Now we have two variables that we can use: `atsign_with_at` and `atsign_without_at` + /* + * 4. Get atserver_host and atserver_port + */ + bool should_free_atserver_host = false; + if (options != NULL) { + if (atclient_pkam_authenticate_options_is_atdirectory_host_initialized(options) && + atclient_pkam_authenticate_options_is_atdirectory_port_initialized(options)) { + atserver_host = options->atdirectory_host; + atserver_port = options->atdirectory_port; + } + } - // Check if the atserver host and atserver port are initialized - if (atclient_pkam_authenticate_options_is_atserver_host_initialized(options) != 0 || - atclient_pkam_authenticate_options_is_atserver_port_initialized(options) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver host and port are not set: %d\n", ret); - goto exit; + if (atserver_host == NULL || atserver_port == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, + "Missing atServer host or port. Using production atDirectory to look up atServer host and port\n"); + if ((ret = atclient_utils_find_atserver_address(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST, + ATCLIENT_ATDIRECTORY_PRODUCTION_PORT, atsign, &atserver_host, + &atserver_port)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret); + goto exit; + } + // only free this memory if it was allocated internally (by atclient_utils_find_atserver_address) + should_free_atserver_host = true; } + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atserver_host: %s\n", atserver_host); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atserver_port: %d\n", atserver_port); + /* - * 4. Start atServer connection (kill the existing connection if it exists) + * 5. Start atServer connection (kill the existing connection if it exists) */ - if ((ret = atclient_start_atserver_connection(ctx, options->atserver_host, options->atserver_port)) != 0) { + if ((ret = atclient_start_atserver_connection(ctx, atserver_host, atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_start_atserver_connection: %d\n", ret); goto exit; } /* - * 5a. Build `from:` noop_cmd + * 6a. Build `from:` cmd */ const size_t from_cmd_size = strlen("from:") + strlen(atsign_without_at) + strlen("\r\n") + 1; // "from:" has a length of 5 @@ -282,7 +289,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient snprintf(from_cmd, from_cmd_size, "from:%s\r\n", atsign_without_at); /* - * 5b. Send `from:` noop_cmd + * 6b. Send `from:` cmd */ if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)from_cmd, from_cmd_size - 1, recv, recvsize, &recv_len)) != 0) { @@ -299,7 +306,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient } /* - * 6. We got `data:` + * 7. We got `data:` * Let us sign the challenge with RSA-2048 PKAM Private Key and Base64 Encode it */ @@ -321,17 +328,28 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient } /* - * 7a. Build `pkam:` noop_cmd + * 8a. Build `pkam:` noop_cmd */ - const size_t pkam_cmd_size = strlen("pkam:") + signature_base64_len + strlen("\r\n") + 1; + size_t pkam_cmd_size = strlen("pkam:"); + if (atclient_atkeys_is_enrollment_id_initialized((atclient_atkeys *)atkeys) && atkeys->enrollment_id != NULL) { + pkam_cmd_size += strlen("enrollmentId:") + strlen(atkeys->enrollment_id) + strlen(":"); + } + pkam_cmd_size += signature_base64_len + strlen("\r\n") + 1; if ((pkam_cmd = malloc(sizeof(char) * pkam_cmd_size)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for pkam_cmd\n"); goto exit; } - snprintf(pkam_cmd, pkam_cmd_size, "pkam:%s\r\n", signature_base64); + size_t pos = 0; + pos += snprintf(pkam_cmd + pos, pkam_cmd_size - pos, "pkam:"); + + if (atclient_atkeys_is_enrollment_id_initialized((atclient_atkeys *)atkeys) && atkeys->enrollment_id != NULL) { + pos += snprintf(pkam_cmd + pos, pkam_cmd_size - pos, "enrollmentId:%s:", atkeys->enrollment_id); + } + + pos += snprintf(pkam_cmd + pos, pkam_cmd_size - pos, "%s\r\n", signature_base64); /* - * 7b. Send `pkam:` noop_cmd + * 8b. Send `pkam:` noop_cmd */ memset(recv, 0, sizeof(unsigned char) * recvsize); if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)pkam_cmd, pkam_cmd_size - 1, recv, @@ -349,7 +367,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient } /* - * 8. Set up the atclient context + * 9. Set up the atclient context */ // initialize ctx->atsign.atsign and ctx->atsign.withour_prefix_str to the newly authenticated atSign @@ -369,6 +387,9 @@ exit: { free(root_cmd); free(from_cmd); free(pkam_cmd); + if (should_free_atserver_host) { + free(atserver_host); + } return ret; } } @@ -486,4 +507,4 @@ static int atclient_pkam_authenticate_validate_arguments(const atclient *ctx, co ret = 0; exit: { return ret; } -} \ No newline at end of file +} diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 3dbfc441..1ded468d 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -1,5 +1,4 @@ #include "atclient/atkey.h" -#include "atclient/constants.h" #include "atclient/metadata.h" #include "atclient/string_utils.h" #include "atlogger/atlogger.h" @@ -61,9 +60,7 @@ int atclient_atkey_clone(atclient_atkey *dst, const atclient_atkey *src) { } ret = 0; goto exit; -exit: { - return ret; -} +exit: { return ret; } } void atclient_atkey_free(atclient_atkey *atkey) { @@ -353,9 +350,10 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { index_pos += strlen("public:"); } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SHARED_KEY) { if (!atclient_atkey_is_shared_with_initialized(atkey)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "atkey's shared_with is not initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHARED_KEY by " - "atclient_atkey_get_type\n"); + atlogger_log( + TAG, ATLOGGER_LOGGING_LEVEL_WARN, + "atkey's shared_with is not initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHARED_KEY by " + "atclient_atkey_get_type\n"); } snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "%s:", atkey->shared_with); index_pos += strlen(atkey->shared_with) + strlen(":"); @@ -571,7 +569,7 @@ atclient_atkey_type atclient_atkey_get_type(const atclient_atkey *atkey) { } int atclient_atkey_create_public_key(atclient_atkey *atkey, const char *key, const char *shared_by, - const char *namespace_str) { + const char *namespace_str) { int ret = 1; if (atkey == NULL) { @@ -625,7 +623,7 @@ exit: { return ret; } } int atclient_atkey_create_self_key(atclient_atkey *atkey, const char *key, const char *shared_by, - const char *namespace_str) { + const char *namespace_str) { int ret = 1; if (atkey == NULL) { @@ -681,7 +679,7 @@ exit: { return ret; } } int atclient_atkey_create_shared_key(atclient_atkey *atkey, const char *key, const char *shared_by, - const char *shared_with, const char *namespace_str) { + const char *shared_with, const char *namespace_str) { int ret = 1; if (atkey == NULL) { diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 3ad7aae8..32293f63 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -16,29 +16,33 @@ static bool is_pkam_private_key_base64_initialized(atclient_atkeys *atkeys); static bool is_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys); static bool is_encrypt_private_key_base64_initialized(atclient_atkeys *atkeys); static bool is_self_encryption_key_base64_initialized(atclient_atkeys *atkeys); +static bool is_enrollment_id_initialized(atclient_atkeys *atkeys); static void set_pkam_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized); static void set_pkam_private_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized); static void set_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized); static void set_encrypt_privatekey_base64_initialized(atclient_atkeys *atkeys, const bool initialized); static void set_self_encryption_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized); +static void set_enrollment_id_initialized(atclient_atkeys *atkeys, const bool initialized); static void unset_pkam_public_key_base64(atclient_atkeys *atkeys); static void unset_pkam_private_key_base64(atclient_atkeys *atkeys); static void unset_encrypt_public_key_base64(atclient_atkeys *atkeys); static void unset_encrypt_private_key_base64(atclient_atkeys *atkeys); static void unset_self_encryption_key_base64(atclient_atkeys *atkeys); +static void unset_enrollment_id(atclient_atkeys *atkeys); static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkam_public_key_len); + const size_t pkam_public_key_len); static int set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkam_private_key_len); + const size_t pkam_private_key_len); static int set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encrypt_public_key_len); + const size_t encrypt_public_key_len); static int set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encrypt_private_key_len); + const size_t encrypt_private_key_len); static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *self_encryption_key_base64, - const size_t self_encryption_key_len); + const size_t self_encryption_key_len); +static int set_enrollment_id(atclient_atkeys *atkeys, const char *enrollment_id, const size_t enrollment_id_len); void atclient_atkeys_init(atclient_atkeys *atkeys) { memset(atkeys, 0, sizeof(atclient_atkeys)); @@ -51,10 +55,11 @@ void atclient_atkeys_init(atclient_atkeys *atkeys) { atkeys->encrypt_public_key_base64 = NULL; atkeys->encrypt_private_key_base64 = NULL; atkeys->self_encryption_key_base64 = NULL; + atkeys->enrollment_id = NULL; } void atclient_atkeys_free(atclient_atkeys *atkeys) { - if(atkeys == NULL) { + if (atkeys == NULL) { return; } @@ -62,26 +67,28 @@ void atclient_atkeys_free(atclient_atkeys *atkeys) { atchops_rsa_key_private_key_free(&(atkeys->pkam_private_key)); atchops_rsa_key_public_key_free(&(atkeys->encrypt_public_key)); atchops_rsa_key_private_key_free(&(atkeys->encrypt_private_key)); - if(atclient_atkeys_is_pkam_public_key_base64_initialized(atkeys)) { + if (atclient_atkeys_is_pkam_public_key_base64_initialized(atkeys)) { unset_pkam_public_key_base64(atkeys); } - if(atclient_atkeys_is_pkam_private_key_base64_initialized(atkeys)) { + if (atclient_atkeys_is_pkam_private_key_base64_initialized(atkeys)) { unset_pkam_private_key_base64(atkeys); } - if(atclient_atkeys_is_encrypt_public_key_base64_initialized(atkeys)) { + if (atclient_atkeys_is_encrypt_public_key_base64_initialized(atkeys)) { unset_encrypt_public_key_base64(atkeys); } - if(atclient_atkeys_is_encrypt_private_key_base64_initialized(atkeys)) { + if (atclient_atkeys_is_encrypt_private_key_base64_initialized(atkeys)) { unset_encrypt_private_key_base64(atkeys); } - if(atclient_atkeys_is_self_encryption_key_base64_initialized(atkeys)) { + if (atclient_atkeys_is_self_encryption_key_base64_initialized(atkeys)) { unset_self_encryption_key_base64(atkeys); } - + if (atclient_atkeys_is_enrollment_id_initialized(atkeys)) { + unset_enrollment_id(atkeys); + } } int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkam_public_key_base64_len) { + const size_t pkam_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -103,8 +110,8 @@ int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const ch } if ((ret = set_pkam_public_key_base64(atkeys, pkam_public_key_base64, pkam_public_key_base64_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkam_public_key_base64: %d | failed to set pkam_public_key_base64\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_pkam_public_key_base64: %d | failed to set pkam_public_key_base64\n", ret); goto exit; } @@ -114,7 +121,7 @@ exit: { return ret; } } int atclient_atkeys_set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkam_private_key_base64_len) { + const size_t pkam_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -147,7 +154,7 @@ exit: { return ret; } } int atclient_atkeys_set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encrypt_public_key_base64_len) { + const size_t encrypt_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -180,7 +187,7 @@ exit: { return ret; } } int atclient_atkeys_set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encrypt_private_key_base64_len) { + const size_t encrypt_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -213,7 +220,7 @@ exit: { return ret; } } int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *self_encryption_key_base64, - const size_t self_encryption_key_base64_len) { + const size_t self_encryption_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -245,8 +252,44 @@ int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, cons exit: { return ret; } } +int atclient_atkeys_set_enrollment_id(atclient_atkeys *atkeys, const char *enrollment_id, + const size_t enrollment_id_len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (enrollment_id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enrollment_id is NULL\n"); + return ret; + } + + if (enrollment_id_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enrollment_id_len is 0\n"); + return ret; + } + + if (atclient_atkeys_is_enrollment_id_initialized(atkeys)) { + unset_enrollment_id(atkeys); + } + + if ((ret = set_enrollment_id(atkeys, enrollment_id, enrollment_id_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enrollment_id: %d | failed to set enrollment_id\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkam_public_key_base64_len) { + const size_t pkam_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -267,8 +310,8 @@ int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char return ret; } - if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), pkam_public_key_base64, pkam_public_key_base64_len)) != - 0) { + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), pkam_public_key_base64, + pkam_public_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_public_key: %d | failed to populate pkam_public_key\n", ret); goto exit; @@ -279,7 +322,7 @@ exit: { return ret; } } int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkam_private_key_base64_len) { + const size_t pkam_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -301,7 +344,7 @@ int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const cha } if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->pkam_private_key), pkam_private_key_base64, - pkam_private_key_base64_len)) != 0) { + pkam_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_private_key: %d | failed to populate pkam_private_key\n", ret); goto exit; @@ -310,7 +353,7 @@ exit: { return ret; } } int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encrypt_public_key_base64_len) { + const size_t encrypt_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -332,7 +375,7 @@ int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const c } if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->encrypt_public_key), encrypt_public_key_base64, - encrypt_public_key_base64_len)) != 0) { + encrypt_public_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_public_key: %d | failed to populate encrypt_public_key\n", ret); goto exit; @@ -344,7 +387,7 @@ exit: { return ret; } } int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encrypt_private_key_base64_len) { + const size_t encrypt_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -366,7 +409,7 @@ int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const } if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->encrypt_private_key), encrypt_private_key_base64, - encrypt_private_key_base64_len)) != 0) { + encrypt_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_private_key: %d | failed to populate encrypt_private_key\n", ret); goto exit; @@ -397,12 +440,18 @@ bool atclient_atkeys_is_self_encryption_key_base64_initialized(atclient_atkeys * return is_self_encryption_key_base64_initialized(atkeys); } +bool atclient_atkeys_is_enrollment_id_initialized(atclient_atkeys *atkeys) { + return is_enrollment_id_initialized(atkeys); +} + int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aes_pkam_public_key_str, const size_t aes_pkam_public_key_len, const char *aes_pkam_private_key_str, const size_t aes_pkam_private_key_len, const char *aes_encrypt_public_key_str, - const size_t aes_encrypt_public_key_len, const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_public_key_len, + const char *aes_encrypt_private_key_str, const size_t aes_encrypt_private_key_len, const char *self_encryption_key_str, - const size_t self_encryption_key_str_len) { + const size_t self_encryption_key_str_len, const char *enrollment_id_str, + const size_t enrollment_id_str_len) { int ret = 1; /* @@ -507,8 +556,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a /* * 3. Prepare self encryption key for use */ - if ((ret = atchops_base64_decode((unsigned char *)self_encryption_key_str, self_encryption_key_str_len, self_encryption_key, - self_encryption_key_size, &self_encryption_key_len)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)self_encryption_key_str, self_encryption_key_str_len, + self_encryption_key, self_encryption_key_size, &self_encryption_key_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding selfencryption key: %d\n", ret); goto exit; } @@ -518,12 +567,13 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a */ // 4a. self encryption key - if ((ret = atclient_atkeys_set_self_encryption_key_base64(atkeys, self_encryption_key_str, self_encryption_key_str_len)) != 0) { + if ((ret = atclient_atkeys_set_self_encryption_key_base64(atkeys, self_encryption_key_str, + self_encryption_key_str_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_self_encryption_key_base64: %d | failed to set self_encryption_key_str\n", ret); goto exit; } - + // 4b. pkam public key if ((ret = atchops_base64_decode((unsigned char *)aes_pkam_public_key_str, aes_pkam_public_key_len, rsa_key_encrypted, rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { @@ -532,7 +582,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, - rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { + rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt pkam public key\n", ret); goto exit; @@ -549,22 +599,22 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a memset(iv, 0, sizeof(unsigned char) * iv_size); // 4c. pkam private key - if ((ret = atchops_base64_decode((unsigned char *)aes_pkam_private_key_str, aes_pkam_private_key_len, rsa_key_encrypted, - rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)aes_pkam_private_key_str, aes_pkam_private_key_len, + rsa_key_encrypted, rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding pkam private key: %d\n", ret); goto exit; } if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, - rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt pkam private key\n", - ret); + rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atchops_aes_ctr_decrypt: %d | failed to decrypt pkam private key\n", ret); goto exit; } if ((ret = set_pkam_private_key_base64(atkeys, (const char *)rsa_key_decrypted, rsa_key_decrypted_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkam_private_key_base64: %d | failed to set pkamprivatekeystr\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_pkam_private_key_base64: %d | failed to set pkamprivatekeystr\n", ret); goto exit; } @@ -573,14 +623,14 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a memset(iv, 0, sizeof(unsigned char) * iv_size); // 4d. encrypt public key - if ((ret = atchops_base64_decode((unsigned char *)aes_encrypt_public_key_str, aes_encrypt_public_key_len, rsa_key_encrypted, - rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)aes_encrypt_public_key_str, aes_encrypt_public_key_len, + rsa_key_encrypted, rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding encrypt public key: %d\n", ret); goto exit; } if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, - rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { + rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt encrypt public key\n", ret); goto exit; @@ -597,14 +647,14 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a memset(iv, 0, sizeof(unsigned char) * iv_size); // 4e. encrypt private key - if ((ret = atchops_base64_decode((unsigned char *)aes_encrypt_private_key_str, aes_encrypt_private_key_len, rsa_key_encrypted, - rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)aes_encrypt_private_key_str, aes_encrypt_private_key_len, + rsa_key_encrypted, rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding encrypt private key: %d\n", ret); goto exit; } if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, - rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { + rsa_key_decrypted, rsa_key_decrypted_size, &rsa_key_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt encrypt private key\n", ret); goto exit; @@ -622,7 +672,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 5a. pkam public key if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), atkeys->pkam_public_key_base64, - strlen(atkeys->pkam_public_key_base64))) != 0) { + strlen(atkeys->pkam_public_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_public_key: %d | failed to populate pkam public key\n", ret); goto exit; @@ -630,7 +680,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 5b. pkam private key if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->pkam_private_key), atkeys->pkam_private_key_base64, - strlen(atkeys->pkam_private_key_base64))) != 0) { + strlen(atkeys->pkam_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_private_key: %d | failed to populate pkam private key\n", ret); goto exit; @@ -638,7 +688,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 5c. encrypt public key if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->encrypt_private_key), atkeys->encrypt_private_key_base64, - strlen(atkeys->encrypt_private_key_base64))) != 0) { + strlen(atkeys->encrypt_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_private_key: %d | failed to populate encrypt private key\n", ret); goto exit; @@ -646,33 +696,66 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 5d. encrypt private key if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->encrypt_public_key), atkeys->encrypt_public_key_base64, - strlen(atkeys->encrypt_public_key_base64))) != 0) { + strlen(atkeys->encrypt_public_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_public_key: %d | failed to populate encrypt public key\n", ret); goto exit; } + // 6. enrollment id, if it exists + if (enrollment_id_str != NULL && enrollment_id_str_len > 0) { + if ((ret = atclient_atkeys_set_enrollment_id(atkeys, enrollment_id_str, enrollment_id_str_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeys_set_enrollment_id: %d\n", ret); + goto exit; + } + } + ret = 0; goto exit; exit: { return ret; } } -int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atclient_atkeysfile atkeysfile) { +int atclient_atkeys_populate_from_atkeys_file(atclient_atkeys *atkeys, const atclient_atkeysfile *atkeys_file) { int ret = 1; - ret = atclient_atkeys_populate_from_strings( - atkeys, atkeysfile.aes_pkam_public_key_str, strlen(atkeysfile.aes_pkam_public_key_str), atkeysfile.aes_pkam_private_key_str, - strlen(atkeysfile.aes_pkam_private_key_str), atkeysfile.aes_encrypt_public_key_str, - strlen(atkeysfile.aes_encrypt_public_key_str), atkeysfile.aes_encrypt_private_key_str, - strlen(atkeysfile.aes_encrypt_private_key_str), atkeysfile.self_encryption_key_str, - strlen(atkeysfile.self_encryption_key_str)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_atkeys_populate_from_strings: %d | failed to populate from strings\n", ret); + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); goto exit; } + if (atkeys_file == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys_file is NULL\n"); + goto exit; + } + + if (atclient_atkeysfile_is_enrollment_id_str_initialized((atclient_atkeysfile *)atkeys_file)) { + if ((ret = atclient_atkeys_populate_from_strings( + atkeys, atkeys_file->aes_pkam_public_key_str, strlen(atkeys_file->aes_pkam_public_key_str), + atkeys_file->aes_pkam_private_key_str, strlen(atkeys_file->aes_pkam_private_key_str), + atkeys_file->aes_encrypt_public_key_str, strlen(atkeys_file->aes_encrypt_public_key_str), + atkeys_file->aes_encrypt_private_key_str, strlen(atkeys_file->aes_encrypt_private_key_str), + atkeys_file->self_encryption_key_str, strlen(atkeys_file->self_encryption_key_str), + atkeys_file->enrollment_id_str, strlen(atkeys_file->enrollment_id_str))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_atkeys_populate_from_strings: %d | failed to populate from strings\n", ret); + goto exit; + } + } else { + if ((ret = atclient_atkeys_populate_from_strings( + atkeys, atkeys_file->aes_pkam_public_key_str, strlen(atkeys_file->aes_pkam_public_key_str), + atkeys_file->aes_pkam_private_key_str, strlen(atkeys_file->aes_pkam_private_key_str), + atkeys_file->aes_encrypt_public_key_str, strlen(atkeys_file->aes_encrypt_public_key_str), + atkeys_file->aes_encrypt_private_key_str, strlen(atkeys_file->aes_encrypt_private_key_str), + atkeys_file->self_encryption_key_str, strlen(atkeys_file->self_encryption_key_str), NULL, 0)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_atkeys_populate_from_strings: %d | failed to populate from strings\n", ret); + goto exit; + } + } + goto exit; exit: { return ret; } @@ -681,48 +764,81 @@ exit: { return ret; } int atclient_atkeys_populate_from_path(atclient_atkeys *atkeys, const char *path) { int ret = 1; - atclient_atkeysfile atkeysfile; - atclient_atkeysfile_init(&atkeysfile); + atclient_atkeysfile atkeys_file; + atclient_atkeysfile_init(&atkeys_file); + + if ((ret = atclient_atkeysfile_from_path(&atkeys_file, path)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_atkeysfile_from_path: %d | failed to read file at path: %s\n", ret, path); + goto exit; + } + + if ((ret = atclient_atkeys_populate_from_atkeys_file(atkeys, &atkeys_file)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_atkeys_populate_from_atkeys_file: %d | failed to decrypt & populate struct \n", ret); + goto exit; + } + + goto exit; +exit: { + atclient_atkeysfile_free(&atkeys_file); + return ret; +} +} + +int atclient_atkeys_populate_from_string(atclient_atkeys *atkeys, const char *file_string) { + int ret = 1; + + atclient_atkeysfile atkeys_file; + atclient_atkeysfile_init(&atkeys_file); - ret = atclient_atkeysfile_read(&atkeysfile, path); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeysfile_read: %d | failed to read file at path: %s\n", - ret, path); + if ((ret = atclient_atkeysfile_from_string(&atkeys_file, file_string)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeysfile_from_string: %d\n", ret); goto exit; } - ret = atclient_atkeys_populate_from_atkeysfile(atkeys, atkeysfile); - if (ret != 0) { + if ((ret = atclient_atkeys_populate_from_atkeys_file(atkeys, &atkeys_file)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_atkeys_populate_from_atkeysfile: %d | failed to decrypt & populate struct \n", ret); + "atclient_atkeys_populate_from_atkeys_file: %d | failed to decrypt & populate struct \n", ret); goto exit; } + ret = 0; goto exit; + exit: { - atclient_atkeysfile_free(&atkeysfile); + atclient_atkeysfile_free(&atkeys_file); return ret; } } static bool is_pkam_public_key_base64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INDEX] & ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED; + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INDEX] & + ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED; } static bool is_pkam_private_key_base64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] & ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] & + ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; } static bool is_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] & + ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; } static bool is_encrypt_private_key_base64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] & + ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; } static bool is_self_encryption_key_base64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] & ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] & + ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; +} + +static bool is_enrollment_id_initialized(atclient_atkeys *atkeys) { + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENROLLMENT_ID_INDEX] & ATCLIENT_ATKEYS_ENROLLMENT_ID_INITIALIZED; } static void set_pkam_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { @@ -737,31 +853,46 @@ static void set_pkam_private_key_base64_initialized(atclient_atkeys *atkeys, con if (initialized) { atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] |= ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; } else { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] &= + ~ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; } } static void set_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] |= + ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; } else { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] &= + ~ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; } } static void set_encrypt_privatekey_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] |= + ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; } else { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] &= + ~ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; } } static void set_self_encryption_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] |= ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; + } else { + atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; + } +} + +static void set_enrollment_id_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENROLLMENT_ID_INDEX] |= ATCLIENT_ATKEYS_ENROLLMENT_ID_INITIALIZED; } else { - atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] &= ~ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENROLLMENT_ID_INDEX] &= ~ATCLIENT_ATKEYS_ENROLLMENT_ID_INITIALIZED; } } @@ -798,15 +929,23 @@ static void unset_encrypt_private_key_base64(atclient_atkeys *atkeys) { } static void unset_self_encryption_key_base64(atclient_atkeys *atkeys) { - if (is_self_encryption_key_base64_initialized(atkeys)) { + if (is_self_encryption_key_base64_initialized(atkeys) && atkeys->self_encryption_key_base64 != NULL) { free(atkeys->self_encryption_key_base64); } atkeys->self_encryption_key_base64 = NULL; set_self_encryption_key_base64_initialized(atkeys, false); } +static void unset_enrollment_id(atclient_atkeys *atkeys) { + if (is_enrollment_id_initialized(atkeys)) { + free(atkeys->enrollment_id); + } + atkeys->enrollment_id = NULL; + set_enrollment_id_initialized(atkeys, false); +} + static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, - const size_t pkam_public_key_len) { + const size_t pkam_public_key_len) { int ret = 1; if (is_pkam_public_key_base64_initialized(atkeys)) { @@ -817,8 +956,8 @@ static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_ atkeys->pkam_public_key_base64 = (char *)malloc(sizeof(char) * (pkam_publickey_size)); if (atkeys->pkam_public_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkam_public_key_base64\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "malloc: %d | failed to allocate memory for pkam_public_key_base64\n", ret); goto exit; } @@ -834,7 +973,7 @@ exit: { return ret; } } static int set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, - const size_t pkam_private_key_len) { + const size_t pkam_private_key_len) { int ret = 1; if (is_pkam_private_key_base64_initialized(atkeys)) { @@ -845,8 +984,8 @@ static int set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam atkeys->pkam_private_key_base64 = (char *)malloc(sizeof(char) * (pkam_private_key_size)); if (atkeys->pkam_private_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkam_private_key_base64\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "malloc: %d | failed to allocate memory for pkam_private_key_base64\n", ret); goto exit; } @@ -861,7 +1000,7 @@ exit: { return ret; } } static int set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, - const size_t encrypt_public_key_len) { + const size_t encrypt_public_key_len) { int ret = 1; if (is_encrypt_public_key_base64_initialized(atkeys)) { @@ -888,7 +1027,7 @@ exit: { return ret; } } static int set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, - const size_t encrypt_private_key_len) { + const size_t encrypt_private_key_len) { int ret = 1; if (is_encrypt_private_key_base64_initialized(atkeys)) { @@ -915,28 +1054,59 @@ exit: { return ret; } } static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *self_encryption_key_base64, - const size_t self_encryption_key_len) { + const size_t self_encryption_key_len) { int ret = 1; + if (self_encryption_key_base64 == NULL || self_encryption_key_len == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid self encryption key or key length\n"); + return 1; + } + if (is_self_encryption_key_base64_initialized(atkeys)) { unset_self_encryption_key_base64(atkeys); } const size_t self_encryption_key_size = self_encryption_key_len + 1; - atkeys->self_encryption_key_base64 = (char *)malloc(sizeof(char) * (self_encryption_key_size)); + atkeys->self_encryption_key_base64 = (char *)malloc(self_encryption_key_size); if (atkeys->self_encryption_key_base64 == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "malloc: %d | failed to allocate memory for self_encryption_key_base64\n", ret); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for self_encryption_key_base64\n"); + return 1; } memcpy(atkeys->self_encryption_key_base64, self_encryption_key_base64, self_encryption_key_len); - atkeys->self_encryption_key_base64[self_encryption_key_len] = '\0'; + atkeys->self_encryption_key_base64[self_encryption_key_len] = '\0'; // Null-terminate the string set_self_encryption_key_base64_initialized(atkeys, true); ret = 0; - goto exit; -exit: { return ret; } + return ret; +} + +static int set_enrollment_id(atclient_atkeys *atkeys, const char *enrollment_id, const size_t enrollment_id_len) { + int ret = 1; + + if (enrollment_id == NULL || enrollment_id_len == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid enrollment id or id length\n"); + return 1; + } + + if (is_enrollment_id_initialized(atkeys)) { + unset_enrollment_id(atkeys); + } + + // Allocate memory for the enrollment id + const size_t enrollment_id_size = enrollment_id_len + 1; + atkeys->enrollment_id = (char *)malloc(enrollment_id_size); + if (atkeys->enrollment_id == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enrollment_id\n"); + return 1; + } + + memcpy(atkeys->enrollment_id, enrollment_id, enrollment_id_len); + atkeys->enrollment_id[enrollment_id_len] = '\0'; // Null-terminate the string + + set_enrollment_id_initialized(atkeys, true); + + ret = 0; + return ret; } diff --git a/packages/atclient/src/atkeys_file.c b/packages/atclient/src/atkeys_file.c new file mode 100644 index 00000000..4679ed75 --- /dev/null +++ b/packages/atclient/src/atkeys_file.c @@ -0,0 +1,664 @@ +#include "atclient/atkeys_file.h" +#include "atlogger/atlogger.h" +#include +#include +#include +#include +#include +#include + +// represents buffer size of reading the entire atKeys file +#define FILE_READ_BUFFER_SIZE 8192 + +#define TAG "atkeys_file" + +static bool is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file); +static bool is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file); +static bool is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file); +static bool is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file); +static bool is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file); +static bool is_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file); + +static void set_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); +static void set_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); +static void set_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); +static void set_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); +static void set_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); +static void set_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized); + +static void unset_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file); +static void unset_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file); +static void unset_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file); +static void unset_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file); +static void unset_self_encryption_key_str(atclient_atkeysfile *atkeys_file); +static void unset_enrollment_id_str(atclient_atkeysfile *atkeys_file); + +static int set_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_public_key_str, + const size_t aes_pkam_publickey_str_len); +static int set_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_private_key_str, + const size_t aes_pkam_private_key_str_len); +static int set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_public_key_str, + const size_t aes_encrypt_public_key_str_len); +static int set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_private_key_str_len); +static int set_self_encryption_key_str(atclient_atkeysfile *atkeys_file, const char *self_encryption_key_str, + const size_t self_encryption_key_str_len); +static int set_enrollment_id_str(atclient_atkeysfile *atkeys_file, const char *enrollment_id_str, + const size_t enrollment_id_str_len); + +void atclient_atkeysfile_init(atclient_atkeysfile *atkeys_file) { memset(atkeys_file, 0, sizeof(atclient_atkeysfile)); } + +int atclient_atkeysfile_from_path(atclient_atkeysfile *atkeys_file, const char *path) { + int ret = 1; + unsigned char readbuf[FILE_READ_BUFFER_SIZE]; + memset(readbuf, 0, FILE_READ_BUFFER_SIZE); + + FILE *file = fopen(path, "r"); + if (file == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fopen failed\n"); + return ret; + } + + const size_t bytes_read = fread(readbuf, 1, FILE_READ_BUFFER_SIZE, file); + fclose(file); + if (bytes_read == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fread failed\n"); + return ret; + } + + ret = atclient_atkeysfile_from_string(atkeys_file, (const char *)readbuf); + return ret; +} + +int atclient_atkeysfile_from_string(atclient_atkeysfile *atkeys_file, const char *file_string) { + int ret = 1; + + cJSON *root = cJSON_Parse(file_string); + if (root == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n"); + return ret; + } + + cJSON *aes_pkam_public_key = cJSON_GetObjectItem(root, "aesPkamPublicKey"); + if (aes_pkam_public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesPkamPublicKey from JSON\n"); + goto exit; + } + + if ((ret = set_aes_pkam_public_key_str(atkeys_file, aes_pkam_public_key->valuestring, + strlen(aes_pkam_public_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_pkam_public_key_str: %d\n", ret); + goto exit; + } + + cJSON *aes_pkam_private_key = cJSON_GetObjectItem(root, "aesPkamPrivateKey"); + if (aes_pkam_private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesPkamPrivateKey from JSON\n"); + goto exit; + } + if ((ret = set_aes_pkam_private_key_str(atkeys_file, aes_pkam_private_key->valuestring, + strlen(aes_pkam_private_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_pkam_private_key_str: %d\n", ret); + goto exit; + } + + cJSON *aes_encrypt_public_key = cJSON_GetObjectItem(root, "aesEncryptPublicKey"); + if (aes_encrypt_public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesEncryptPublicKey from JSON\n"); + goto exit; + } + if ((ret = set_aes_encrypt_public_key_str(atkeys_file, aes_encrypt_public_key->valuestring, + strlen(aes_encrypt_public_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_encrypt_public_key_str: %d\n", ret); + goto exit; + } + + cJSON *aes_encrypt_private_key = cJSON_GetObjectItem(root, "aesEncryptPrivateKey"); + if (aes_encrypt_private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesEncryptPrivateKey from JSON\n"); + goto exit; + } + if ((ret = set_aes_encrypt_private_key_str(atkeys_file, aes_encrypt_private_key->valuestring, + strlen(aes_encrypt_private_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_encrypt_private_key_str: %d\n", ret); + goto exit; + } + + cJSON *self_encryption_key = cJSON_GetObjectItem(root, "selfEncryptionKey"); + if (self_encryption_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read selfEncryptionKey from JSON\n"); + goto exit; + } + if ((ret = set_self_encryption_key_str(atkeys_file, self_encryption_key->valuestring, + strlen(self_encryption_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_self_encryption_key_str: %d\n", ret); + goto exit; + } + + cJSON *enrollment_id = cJSON_GetObjectItem(root, "enrollmentId"); + if (enrollment_id != NULL) { + if ((ret = set_enrollment_id_str(atkeys_file, enrollment_id->valuestring, strlen(enrollment_id->valuestring))) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enrollment_id_str: %d\n", ret); + goto exit; + } + } + + ret = 0; + +exit: { + cJSON_Delete(root); + return ret; +} +} + +void atclient_atkeysfile_free(atclient_atkeysfile *atkeys_file) { + unset_aes_pkam_public_key_str(atkeys_file); + unset_aes_pkam_private_key_str(atkeys_file); + unset_aes_encrypt_public_key_str(atkeys_file); + unset_aes_encrypt_private_key_str(atkeys_file); + unset_self_encryption_key_str(atkeys_file); + unset_enrollment_id_str(atkeys_file); +} + +bool atclient_atkeysfile_is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_aes_pkam_public_key_str_initialized(atkeys_file); +} + +bool atclient_atkeysfile_is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_aes_pkam_private_key_str_initialized(atkeys_file); +} + +bool atclient_atkeysfile_is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_aes_encrypt_public_key_str_initialized(atkeys_file); +} + +bool atclient_atkeysfile_is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_aes_encrypt_private_key_str_initialized(atkeys_file); +} + +bool atclient_atkeysfile_is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_self_encryption_key_str_initialized(atkeys_file); +} + +bool atclient_atkeysfile_is_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file) { + return is_enrollment_id_str_initialized(atkeys_file); +} + +int atclient_atkeysfile_set_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file, + const char *aes_pkam_public_key_str, + const size_t aes_pkam_public_key_str_len) { + int ret = 1; + + if (aes_pkam_public_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_public_key_str is NULL\n"); + return ret; + } + + if (aes_pkam_public_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_public_key_str_len is 0\n"); + return ret; + } + + if (is_aes_pkam_public_key_str_initialized(atkeys_file)) { + unset_aes_pkam_public_key_str(atkeys_file); + } + + if ((ret = set_aes_pkam_public_key_str(atkeys_file, aes_pkam_public_key_str, aes_pkam_public_key_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_pkam_public_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeysfile_set_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file, + const char *aes_pkam_private_key_str, + const size_t aes_pkam_private_key_str_len) { + int ret = 1; + + if (aes_pkam_private_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_private_key_str is NULL\n"); + return ret; + } + + if (aes_pkam_private_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_private_key_str_len is 0\n"); + return ret; + } + + if (is_aes_pkam_private_key_str_initialized(atkeys_file)) { + unset_aes_pkam_private_key_str(atkeys_file); + } + + if ((ret = set_aes_pkam_private_key_str(atkeys_file, aes_pkam_private_key_str, aes_pkam_private_key_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_pkam_private_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeysfile_set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file, + const char *aes_encrypt_public_key_str, + const size_t aes_encrypt_public_key_str_len) { + int ret = 1; + + if (aes_encrypt_public_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_public_key_str is NULL\n"); + return ret; + } + + if (aes_encrypt_public_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_public_key_str_len is 0\n"); + return ret; + } + + if (is_aes_encrypt_public_key_str_initialized(atkeys_file)) { + unset_aes_encrypt_public_key_str(atkeys_file); + } + + if ((ret = set_aes_encrypt_public_key_str(atkeys_file, aes_encrypt_public_key_str, aes_encrypt_public_key_str_len)) != + 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_encrypt_public_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeysfile_set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file, + const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_private_key_str_len) { + int ret = 1; + + if (aes_encrypt_private_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_private_key_str is NULL\n"); + return ret; + } + + if (aes_encrypt_private_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_private_key_str_len is 0\n"); + return ret; + } + + if (is_aes_encrypt_private_key_str_initialized(atkeys_file)) { + unset_aes_encrypt_private_key_str(atkeys_file); + } + + if ((ret = set_aes_encrypt_private_key_str(atkeys_file, aes_encrypt_private_key_str, + aes_encrypt_private_key_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_encrypt_private_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeysfile_set_self_encryption_key_str(atclient_atkeysfile *atkeys_file, + const char *self_encryption_key_str, + const size_t self_encryption_key_str_len) { + int ret = 1; + + if (self_encryption_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_str is NULL\n"); + return ret; + } + + if (self_encryption_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_str_len is 0\n"); + return ret; + } + + if (is_self_encryption_key_str_initialized(atkeys_file)) { + unset_self_encryption_key_str(atkeys_file); + } + + if ((ret = set_self_encryption_key_str(atkeys_file, self_encryption_key_str, self_encryption_key_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_self_encryption_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeysfile_set_enrollment_id_str(atclient_atkeysfile *atkeys_file, const char *enrollment_id_str, + const size_t enrollment_id_str_len) { + int ret = 1; + + if (enrollment_id_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enrollment_id_str is NULL\n"); + return ret; + } + + if (enrollment_id_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enrollment_id_str_len is 0\n"); + return ret; + } + + if (is_enrollment_id_str_initialized(atkeys_file)) { + unset_enrollment_id_str(atkeys_file); + } + + if ((ret = set_enrollment_id_str(atkeys_file, enrollment_id_str, enrollment_id_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enrollment_id_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +static bool is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; +} + +static bool is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; +} + +static bool is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; +} + +static bool is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; +} + +static bool is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; +} + +static bool is_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INITIALIZED; +} + +static void set_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; + } +} + +static void set_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; + } +} + +static void set_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; + } +} + +static void set_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; + } +} + +static void set_self_encryption_key_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; + } +} + +static void set_enrollment_id_str_initialized(atclient_atkeysfile *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INITIALIZED; + } +} + +static void unset_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file) { + if (is_aes_pkam_public_key_str_initialized(atkeys_file)) { + free(atkeys_file->aes_pkam_public_key_str); + } + atkeys_file->aes_pkam_public_key_str = NULL; + set_aes_pkam_public_key_str_initialized(atkeys_file, false); +} + +static void unset_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file) { + if (is_aes_pkam_private_key_str_initialized(atkeys_file)) { + free(atkeys_file->aes_pkam_private_key_str); + } + atkeys_file->aes_pkam_private_key_str = NULL; + set_aes_pkam_private_key_str_initialized(atkeys_file, false); +} + +static void unset_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file) { + if (is_aes_encrypt_public_key_str_initialized(atkeys_file)) { + free(atkeys_file->aes_encrypt_public_key_str); + } + atkeys_file->aes_encrypt_public_key_str = NULL; + set_aes_encrypt_public_key_str_initialized(atkeys_file, false); +} + +static void unset_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file) { + if (is_aes_encrypt_private_key_str_initialized(atkeys_file)) { + free(atkeys_file->aes_encrypt_private_key_str); + } + atkeys_file->aes_encrypt_private_key_str = NULL; + set_aes_encrypt_private_key_str_initialized(atkeys_file, false); +} + +static void unset_self_encryption_key_str(atclient_atkeysfile *atkeys_file) { + if (is_self_encryption_key_str_initialized(atkeys_file)) { + free(atkeys_file->self_encryption_key_str); + } + atkeys_file->self_encryption_key_str = NULL; + set_self_encryption_key_str_initialized(atkeys_file, false); +} + +static void unset_enrollment_id_str(atclient_atkeysfile *atkeys_file) { + if (is_enrollment_id_str_initialized(atkeys_file)) { + free(atkeys_file->enrollment_id_str); + } + atkeys_file->enrollment_id_str = NULL; + set_enrollment_id_str_initialized(atkeys_file, false); +} + +static int set_aes_pkam_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_public_key_str, + const size_t aes_pkam_publickey_str_len) { + int ret = 1; + + if (is_aes_pkam_public_key_str_initialized(atkeys_file)) { + unset_aes_pkam_public_key_str(atkeys_file); + } + + const size_t aes_pkam_public_key_str_size = aes_pkam_publickey_str_len + 1; + if ((atkeys_file->aes_pkam_public_key_str = (char *)malloc(sizeof(char) * aes_pkam_public_key_str_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aes_pkam_public_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->aes_pkam_public_key_str, aes_pkam_public_key_str, aes_pkam_publickey_str_len); + atkeys_file->aes_pkam_public_key_str[aes_pkam_publickey_str_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_aes_pkam_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_pkam_private_key_str, + const size_t aes_pkam_private_key_str_len) { + int ret = 1; + + if (is_aes_pkam_private_key_str_initialized(atkeys_file)) { + unset_aes_pkam_private_key_str(atkeys_file); + } + + const size_t aes_pkam_private_key_str_size = aes_pkam_private_key_str_len + 1; + if ((atkeys_file->aes_pkam_private_key_str = (char *)malloc(sizeof(char) * aes_pkam_private_key_str_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aes_pkam_private_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->aes_pkam_private_key_str, aes_pkam_private_key_str, aes_pkam_private_key_str_len); + atkeys_file->aes_pkam_private_key_str[aes_pkam_private_key_str_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} +static int set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_public_key_str, + const size_t aes_encrypt_public_key_str_len) { + int ret = 1; + + if (is_aes_encrypt_public_key_str_initialized(atkeys_file)) { + unset_aes_encrypt_public_key_str(atkeys_file); + } + + const size_t aes_encrypt_public_key_str_size = aes_encrypt_public_key_str_len + 1; + if ((atkeys_file->aes_encrypt_public_key_str = (char *)malloc(sizeof(char) * aes_encrypt_public_key_str_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aes_encrypt_public_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->aes_encrypt_public_key_str, aes_encrypt_public_key_str, aes_encrypt_public_key_str_len); + atkeys_file->aes_encrypt_public_key_str[aes_encrypt_public_key_str_len] = '\0'; + + ret = 0; + goto exit; + +exit: { return ret; } +} + +static int set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeys_file, const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_private_key_str_len) { + int ret = 1; + + if (is_aes_encrypt_private_key_str_initialized(atkeys_file)) { + unset_aes_encrypt_private_key_str(atkeys_file); + } + + const size_t aes_encrypt_private_key_str_size = aes_encrypt_private_key_str_len + 1; + if ((atkeys_file->aes_encrypt_private_key_str = (char *)malloc(sizeof(char) * aes_encrypt_private_key_str_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aes_encrypt_private_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->aes_encrypt_private_key_str, aes_encrypt_private_key_str, aes_encrypt_private_key_str_len); + atkeys_file->aes_encrypt_private_key_str[aes_encrypt_private_key_str_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_self_encryption_key_str(atclient_atkeysfile *atkeys_file, const char *self_encryption_key_str, + const size_t self_encryption_key_str_len) { + int ret = 1; + + if (is_self_encryption_key_str_initialized(atkeys_file)) { + unset_self_encryption_key_str(atkeys_file); + } + + const size_t selfencryptionkeystrsize = self_encryption_key_str_len + 1; + if ((atkeys_file->self_encryption_key_str = (char *)malloc(sizeof(char) * selfencryptionkeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_self_encryption_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->self_encryption_key_str, self_encryption_key_str, self_encryption_key_str_len); + atkeys_file->self_encryption_key_str[self_encryption_key_str_len] = '\0'; + + ret = 0; + goto exit; + +exit: { return ret; } +} + +static int set_enrollment_id_str(atclient_atkeysfile *atkeys_file, const char *enrollment_id_str, + const size_t enrollment_id_str_len) { + int ret = 1; + + if (is_enrollment_id_str_initialized(atkeys_file)) { + unset_enrollment_id_str(atkeys_file); + } + + const size_t enrollment_id_str_size = enrollment_id_str_len + 1; + if ((atkeys_file->enrollment_id_str = (char *)malloc(sizeof(char) * enrollment_id_str_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_enrollment_id_str_initialized(atkeys_file, true); + memcpy(atkeys_file->enrollment_id_str, enrollment_id_str, enrollment_id_str_len); + atkeys_file->enrollment_id_str[enrollment_id_str_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c deleted file mode 100644 index ad213fad..00000000 --- a/packages/atclient/src/atkeysfile.c +++ /dev/null @@ -1,377 +0,0 @@ -#include "atclient/atkeysfile.h" -#include "atlogger/atlogger.h" -#include -#include -#include -#include -#include -#include - -// represents buffer size of reading the entire atKeys file -#define FILE_READ_BUFFER_SIZE 8192 - -#define TAG "atkeysfile" - -static bool is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeysfile); -static bool is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeysfile); - -static void set_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_self_encryption_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); - -static void unset_aes_pkam_public_key_str(atclient_atkeysfile *atkeysfile); -static void unset_aes_pkam_private_key_str(atclient_atkeysfile *atkeysfile); -static void unset_aes_encrypt_public_key_str(atclient_atkeysfile *atkeysfile); -static void unset_aes_encrypt_private_key_str(atclient_atkeysfile *atkeysfile); -static void unset_self_encryption_key_str(atclient_atkeysfile *atkeysfile); - -static int set_aes_pkam_public_key_str(atclient_atkeysfile *atkeysfile, const char *aes_pkam_public_key_str, - const size_t aes_pkam_publickey_str_len); -static int set_aes_pkam_private_key_str(atclient_atkeysfile *atkeysfile, const char *aes_pkam_private_key_str, - const size_t aes_pkam_private_key_str_len); -static int set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeysfile, const char *aes_encrypt_public_key_str, - const size_t aes_encrypt_public_key_str_len); -static int set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeysfile, const char *aes_encrypt_private_key_str, - const size_t aes_encrypt_private_key_str_len); -static int set_self_encryption_key_str(atclient_atkeysfile *atkeysfile, const char *self_encryption_key_str, - const size_t self_encryption_key_str_len); - -void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile) { memset(atkeysfile, 0, sizeof(atclient_atkeysfile)); } - -int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path) { - int ret = 1; - - unsigned char readbuf[FILE_READ_BUFFER_SIZE]; - memset(readbuf, 0, FILE_READ_BUFFER_SIZE); - - cJSON *root = NULL; - - FILE *file = fopen(path, "r"); - if (file == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fopen failed\n"); - goto exit; - } - - const size_t bytes_read = fread(readbuf, 1, FILE_READ_BUFFER_SIZE, file); - fclose(file); - if (bytes_read == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fread failed\n"); - ret = 1; - goto exit; - } - - root = cJSON_Parse((const char *) readbuf); - cJSON *aes_pkam_public_key = cJSON_GetObjectItem(root, "aesPkamPublicKey"); - cJSON *aes_pkam_private_key = cJSON_GetObjectItem(root, "aesPkamPrivateKey"); - cJSON *aes_encrypt_public_key = cJSON_GetObjectItem(root, "aesEncryptPublicKey"); - cJSON *aes_encrypt_private_key = cJSON_GetObjectItem(root, "aesEncryptPrivateKey"); - cJSON *self_encryption_key = cJSON_GetObjectItem(root, "selfEncryptionKey"); - - if (aes_pkam_private_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPrivateKey!\n"); - goto exit; - } - - if (aes_pkam_public_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPublicKey!\n"); - goto exit; - } - - if (aes_encrypt_private_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPrivateKey!\n"); - goto exit; - } - - if (aes_encrypt_public_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPublicKey!\n"); - goto exit; - } - - if (self_encryption_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading selfEncryptionKey!\n"); - goto exit; - } - - if ((ret = set_aes_pkam_public_key_str(atkeysfile, aes_pkam_public_key->valuestring, - strlen(aes_pkam_public_key->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aes_pkam_public_key_str: %d | failed to set aes_pkam_public_key_str\n", - ret); - goto exit; - } - - if ((ret = set_aes_pkam_private_key_str(atkeysfile, aes_pkam_private_key->valuestring, - strlen(aes_pkam_private_key->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_aes_pkam_private_key_str: %d | failed to set aes_pkam_private_key_str\n", ret); - goto exit; - } - - if ((ret = set_aes_encrypt_public_key_str(atkeysfile, aes_encrypt_public_key->valuestring, - strlen(aes_encrypt_public_key->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_aes_encrypt_public_key_str: %d | failed to set aes_encrypt_public_key_str\n", ret); - goto exit; - } - - if ((ret = set_aes_encrypt_private_key_str(atkeysfile, aes_encrypt_private_key->valuestring, - strlen(aes_encrypt_private_key->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_aes_encrypt_private_key_str: %d | failed to set aes_encrypt_private_key_str\n", ret); - goto exit; - } - - if ((ret = set_self_encryption_key_str(atkeysfile, self_encryption_key->valuestring, - strlen(self_encryption_key->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_self_encryption_key_str: %d | failed to set self_encryption_key_str\n", ret); - goto exit; - } - - ret = 0; - goto exit; - -exit: { - cJSON_Delete(root); - return ret; -} -} - -void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile) { - unset_aes_pkam_public_key_str(atkeysfile); - unset_aes_pkam_private_key_str(atkeysfile); - unset_aes_encrypt_public_key_str(atkeysfile); - unset_aes_encrypt_private_key_str(atkeysfile); - unset_self_encryption_key_str(atkeysfile); -} - -static bool is_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] & ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; -} - -static bool is_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] & ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; -} - -static bool is_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] & ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; -} - -static bool is_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] & ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; -} - -static bool is_self_encryption_key_str_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX] & ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; -} - -static void set_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { - if (initialized) { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; - } else { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; - } -} - -static void set_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { - if (initialized) { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; - } else { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; - } -} - -static void set_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { - if (initialized) { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; - } else { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; - } -} - -static void set_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { - if (initialized) { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; - } else { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; - } -} - -static void set_self_encryption_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { - if (initialized) { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; - } else { - atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; - } -} - -static void unset_aes_pkam_public_key_str(atclient_atkeysfile *atkeysfile) { - if (is_aes_pkam_public_key_str_initialized(atkeysfile)) { - free(atkeysfile->aes_pkam_public_key_str); - } - atkeysfile->aes_pkam_public_key_str = NULL; - set_aes_pkam_public_key_str_initialized(atkeysfile, false); -} - -static void unset_aes_pkam_private_key_str(atclient_atkeysfile *atkeysfile) { - if (is_aes_pkam_private_key_str_initialized(atkeysfile)) { - free(atkeysfile->aes_pkam_private_key_str); - } - atkeysfile->aes_pkam_private_key_str = NULL; - set_aes_pkam_private_key_str_initialized(atkeysfile, false); -} - -static void unset_aes_encrypt_public_key_str(atclient_atkeysfile *atkeysfile) { - if (is_aes_encrypt_public_key_str_initialized(atkeysfile)) { - free(atkeysfile->aes_encrypt_public_key_str); - } - atkeysfile->aes_encrypt_public_key_str = NULL; - set_aes_encrypt_public_key_str_initialized(atkeysfile, false); -} - -static void unset_aes_encrypt_private_key_str(atclient_atkeysfile *atkeysfile) { - if (is_aes_encrypt_private_key_str_initialized(atkeysfile)) { - free(atkeysfile->aes_encrypt_private_key_str); - } - atkeysfile->aes_encrypt_private_key_str = NULL; - set_aes_encrypt_private_key_str_initialized(atkeysfile, false); -} - -static void unset_self_encryption_key_str(atclient_atkeysfile *atkeysfile) { - if (is_self_encryption_key_str_initialized(atkeysfile)) { - free(atkeysfile->self_encryption_key_str); - } - atkeysfile->self_encryption_key_str = NULL; - set_self_encryption_key_str_initialized(atkeysfile, false); -} - -static int set_aes_pkam_public_key_str(atclient_atkeysfile *atkeysfile, const char *aes_pkam_public_key_str, - const size_t aes_pkam_publickey_str_len) { - int ret = 1; - - if (is_aes_pkam_public_key_str_initialized(atkeysfile)) { - unset_aes_pkam_public_key_str(atkeysfile); - } - - const size_t aes_pkam_public_key_str_size = aes_pkam_publickey_str_len + 1; - if ((atkeysfile->aes_pkam_public_key_str = (char *)malloc(sizeof(char) * aes_pkam_public_key_str_size)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - - set_aes_pkam_public_key_str_initialized(atkeysfile, true); - memcpy(atkeysfile->aes_pkam_public_key_str, aes_pkam_public_key_str, aes_pkam_publickey_str_len); - atkeysfile->aes_pkam_public_key_str[aes_pkam_publickey_str_len] = '\0'; - - ret = 0; - goto exit; -exit: { return ret; } -} - -static int set_aes_pkam_private_key_str(atclient_atkeysfile *atkeysfile, const char *aes_pkam_private_key_str, - const size_t aes_pkam_private_key_str_len) { - int ret = 1; - - if (is_aes_pkam_private_key_str_initialized(atkeysfile)) { - unset_aes_pkam_private_key_str(atkeysfile); - } - - const size_t aes_pkam_private_key_str_size = aes_pkam_private_key_str_len + 1; - if ((atkeysfile->aes_pkam_private_key_str = (char *)malloc(sizeof(char) * aes_pkam_private_key_str_size)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - - set_aes_pkam_private_key_str_initialized(atkeysfile, true); - memcpy(atkeysfile->aes_pkam_private_key_str, aes_pkam_private_key_str, aes_pkam_private_key_str_len); - atkeysfile->aes_pkam_private_key_str[aes_pkam_private_key_str_len] = '\0'; - - ret = 0; - goto exit; -exit: { return ret; } -} -static int set_aes_encrypt_public_key_str(atclient_atkeysfile *atkeysfile, const char *aes_encrypt_public_key_str, - const size_t aes_encrypt_public_key_str_len) { - int ret = 1; - - if (is_aes_encrypt_public_key_str_initialized(atkeysfile)) { - unset_aes_encrypt_public_key_str(atkeysfile); - } - - const size_t aes_encrypt_public_key_str_size = aes_encrypt_public_key_str_len + 1; - if ((atkeysfile->aes_encrypt_public_key_str = (char *)malloc(sizeof(char) * aes_encrypt_public_key_str_size)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - - set_aes_encrypt_public_key_str_initialized(atkeysfile, true); - memcpy(atkeysfile->aes_encrypt_public_key_str, aes_encrypt_public_key_str, aes_encrypt_public_key_str_len); - atkeysfile->aes_encrypt_public_key_str[aes_encrypt_public_key_str_len] = '\0'; - - ret = 0; - goto exit; - -exit: { return ret; } -} - -static int set_aes_encrypt_private_key_str(atclient_atkeysfile *atkeysfile, const char *aes_encrypt_private_key_str, - const size_t aes_encrypt_private_key_str_len) { - int ret = 1; - - if (is_aes_encrypt_private_key_str_initialized(atkeysfile)) { - unset_aes_encrypt_private_key_str(atkeysfile); - } - - const size_t aes_encrypt_private_key_str_size = aes_encrypt_private_key_str_len + 1; - if ((atkeysfile->aes_encrypt_private_key_str = (char *)malloc(sizeof(char) * aes_encrypt_private_key_str_size)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - - set_aes_encrypt_private_key_str_initialized(atkeysfile, true); - memcpy(atkeysfile->aes_encrypt_private_key_str, aes_encrypt_private_key_str, aes_encrypt_private_key_str_len); - atkeysfile->aes_encrypt_private_key_str[aes_encrypt_private_key_str_len] = '\0'; - - ret = 0; - goto exit; -exit: { return ret; } -} - -static int set_self_encryption_key_str(atclient_atkeysfile *atkeysfile, const char *self_encryption_key_str, - const size_t self_encryption_key_str_len) { - int ret = 1; - - if (is_self_encryption_key_str_initialized(atkeysfile)) { - unset_self_encryption_key_str(atkeysfile); - } - - const size_t selfencryptionkeystrsize = self_encryption_key_str_len + 1; - if ((atkeysfile->self_encryption_key_str = (char *)malloc(sizeof(char) * selfencryptionkeystrsize)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); - goto exit; - } - - set_self_encryption_key_str_initialized(atkeysfile, true); - memcpy(atkeysfile->self_encryption_key_str, self_encryption_key_str, self_encryption_key_str_len); - atkeysfile->self_encryption_key_str[self_encryption_key_str_len] = '\0'; - - ret = 0; - goto exit; - -exit: { return ret; } -} diff --git a/packages/atclient/src/atnotification.c b/packages/atclient/src/atnotification.c index 306b14d8..f86b1a5a 100644 --- a/packages/atclient/src/atnotification.c +++ b/packages/atclient/src/atnotification.c @@ -1,5 +1,4 @@ #include "atclient/atnotification.h" -#include "atclient/cjson.h" #include #include #include @@ -155,7 +154,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio cJSON *id = cJSON_GetObjectItem(root, "id"); if (id != NULL) { if (id->type != cJSON_NULL) { - if(id->type != cJSON_String) { + if (id->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification id is not a string\n"); goto exit; } @@ -191,7 +190,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio cJSON *to = cJSON_GetObjectItem(root, "to"); if (to != NULL) { if (to->type != cJSON_NULL) { - if((to->type != cJSON_String)) { + if ((to->type != cJSON_String)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification to is not a string\n"); goto exit; } @@ -209,7 +208,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio cJSON *key = cJSON_GetObjectItem(root, "key"); if (key != NULL) { if (key->type != cJSON_NULL) { - if(key->type != cJSON_String) { + if (key->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification key is not a string\n"); goto exit; } @@ -294,7 +293,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio cJSON *is_encrypted = cJSON_GetObjectItem(root, "isEncrypted"); if (is_encrypted != NULL) { - if(is_encrypted->type == cJSON_True) { + if (is_encrypted->type == cJSON_True) { if ((ret = atclient_atnotification_set_is_encrypted(notification, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); goto exit; @@ -315,7 +314,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio // get enc_key_name cJSON *enc_key_name = cJSON_GetObjectItem(metadata, "encKeyName"); if (enc_key_name != NULL) { - if(enc_key_name->type != cJSON_NULL) { + if (enc_key_name->type != cJSON_NULL) { if (enc_key_name->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_key_name is not a string\n"); goto exit; @@ -326,7 +325,8 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio } } else { atclient_atnotification_unset_enc_key_name(notification); - atclient_atnotification_enc_key_name_set_initialized(notification, true); // set it to initialized, but it is kept null + atclient_atnotification_enc_key_name_set_initialized(notification, + true); // set it to initialized, but it is kept null notification->enc_key_name = NULL; } } @@ -334,7 +334,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio // get enc_algo cJSON *enc_algo = cJSON_GetObjectItem(metadata, "encAlgo"); if (enc_algo != NULL) { - if(enc_algo->type != cJSON_NULL) { + if (enc_algo->type != cJSON_NULL) { if (enc_algo->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification enc_algo is not a string\n"); goto exit; @@ -345,7 +345,8 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio } } else { atclient_atnotification_unset_enc_algo(notification); - atclient_atnotification_enc_algo_set_initialized(notification, true); // set it to initialized, but it is kept null + atclient_atnotification_enc_algo_set_initialized(notification, + true); // set it to initialized, but it is kept null notification->enc_algo = NULL; } } @@ -353,7 +354,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio // get iv_nonce cJSON *iv_nonce = cJSON_GetObjectItem(metadata, "ivNonce"); if (iv_nonce != NULL) { - if(iv_nonce->type != cJSON_NULL) { + if (iv_nonce->type != cJSON_NULL) { if (iv_nonce->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification iv_nonce is not a string\n"); goto exit; @@ -364,7 +365,8 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio } } else { atclient_atnotification_unset_iv_nonce(notification); - atclient_atnotification_iv_nonce_set_initialized(notification, true); // set it to initialized, but it is kept null + atclient_atnotification_iv_nonce_set_initialized(notification, + true); // set it to initialized, but it is kept null notification->iv_nonce = NULL; } } @@ -372,7 +374,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio // get ske_enc_key_name cJSON *ske_enc_key_name = cJSON_GetObjectItem(metadata, "skeEncKeyName"); if (ske_enc_key_name != NULL) { - if(ske_enc_key_name->type != cJSON_NULL) { + if (ske_enc_key_name->type != cJSON_NULL) { if (ske_enc_key_name->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_key_name is not a string\n"); goto exit; @@ -383,7 +385,8 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio } } else { atclient_atnotification_unset_ske_enc_key_name(notification); - atclient_atnotification_ske_enc_key_name_set_initialized(notification, true); // set it to initialized, but it is kept null + atclient_atnotification_ske_enc_key_name_set_initialized(notification, + true); // set it to initialized, but it is kept null notification->ske_enc_key_name = NULL; } } @@ -391,7 +394,7 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio // get ske_enc_algo cJSON *ske_enc_algo = cJSON_GetObjectItem(metadata, "skeEncAlgo"); if (ske_enc_algo != NULL) { - if(ske_enc_algo->type != cJSON_NULL) { + if (ske_enc_algo->type != cJSON_NULL) { if (ske_enc_algo->type != cJSON_String) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification ske_enc_algo is not a string\n"); goto exit; @@ -402,7 +405,8 @@ int atclient_atnotification_from_cjson_node(atclient_atnotification *notificatio } } else { atclient_atnotification_unset_ske_enc_algo(notification); - atclient_atnotification_ske_enc_algo_set_initialized(notification, true); // set it to initialized, but it is kept null + atclient_atnotification_ske_enc_algo_set_initialized(notification, + true); // set it to initialized, but it is kept null notification->ske_enc_algo = NULL; } } diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 03f140b3..846e5831 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -4,7 +4,6 @@ #include "atclient/connection_hooks.h" #include "atclient/constants.h" #include "atlogger/atlogger.h" -#include "atclient/mbedtls.h" #include #include #include @@ -252,7 +251,7 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val if (try_hooks && atclient_connection_hooks_is_pre_write_initialized(ctx) && ctx->hooks->pre_write != NULL) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; - params.src = (unsigned char *) value; + params.src = (unsigned char *)value; params.src_len = value_len; params.recv = NULL; params.recv_size = 0; @@ -352,7 +351,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, if (try_hooks && atclient_connection_hooks_is_pre_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; - params.src = (unsigned char *) src; + params.src = (unsigned char *)src; params.src_len = src_len; params.recv = recv; params.recv_size = recv_size; @@ -398,7 +397,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, if (try_hooks && atclient_connection_hooks_is_post_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; - params.src = (unsigned char *) src; + params.src = (unsigned char *)src; params.src_len = src_len; params.recv = recv; params.recv_size = recv_size; @@ -428,7 +427,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, if (try_hooks && atclient_connection_hooks_is_pre_read_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; - params.src = (unsigned char *) src; + params.src = (unsigned char *)src; params.src_len = src_len; params.recv = recv; params.recv_size = recv_size; @@ -474,7 +473,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, if (try_hooks && atclient_connection_hooks_is_post_read_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; - params.src = (unsigned char *) src; + params.src = (unsigned char *)src; params.src_len = src_len; params.recv = recv; params.recv_size = recv_size; @@ -508,9 +507,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, ret = 0; goto exit; -exit: { - return ret; -} +exit: { return ret; } } int atclient_connection_disconnect(atclient_connection *ctx) { @@ -579,7 +576,8 @@ bool atclient_connection_is_connected(atclient_connection *ctx) { } if (recv_len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv_len is <= 0, connection did not respond to \"%s\"\n", command); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv_len is <= 0, connection did not respond to \"%s\"\n", + command); return false; } @@ -603,7 +601,7 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si return ret; } - if(!atclient_connection_is_connection_enabled(ctx)) { + if (!atclient_connection_is_connection_enabled(ctx)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); return ret; } @@ -612,7 +610,7 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si * 2. Variables */ size_t recv_size; - if(value_max_len == 0) { + if (value_max_len == 0) { // we read 4 KB at a time, TODO: make a constant recv_size = 4096; } else { @@ -649,7 +647,7 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si size_t pos = 0; size_t recv_len = 0; do { - if((ret = mbedtls_ssl_read(&(ctx->ssl), recv + pos, recv_size - pos)) <= 0) { + if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv + pos, recv_size - pos)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_read failed with exit code: %d\n", ret); goto exit; } @@ -658,15 +656,16 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si // check if we found the end of the message int i = pos; - while(!found_end && i-- > 0) { + while (!found_end && i-- > 0) { found_end = recv[i] == '\n' || recv[i] == '\r'; } - if(found_end) { + if (found_end) { recv_len = i; } else { - if(value_max_len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, "Message is too long, it exceeds the maximum length of %d\n", value_max_len); + if (value_max_len != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, "Message is too long, it exceeds the maximum length of %d\n", + value_max_len); recv_len = value_max_len; break; } else { @@ -675,43 +674,45 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si } } - } while(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == 0 || !found_end); + } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == 0 || !found_end); /* * 5. Print debug log */ - if(atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { + if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { unsigned char *recvcopy = NULL; - if((recvcopy = malloc(sizeof(unsigned char) * recv_len)) != NULL) { + if ((recvcopy = malloc(sizeof(unsigned char) * recv_len)) != NULL) { memcpy(recvcopy, recv, recv_len); atlogger_fix_stdout_buffer((char *)recvcopy, recv_len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, recv_len, recvcopy, reset); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, recv_len, recvcopy, + reset); free(recvcopy); } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory to pretty print the network received buffer\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "Failed to allocate memory to pretty print the network received buffer\n"); } } /* * 6. Set the value and value_len */ - if(found_end) { - if(recv_len != 0 && recv_len < recv_size) { + if (found_end) { + if (recv_len != 0 && recv_len < recv_size) { recv[recv_len] = '\0'; } } - if(value_len != NULL) { - *value_len = recv_len; - } - if(value != NULL) { - if((*value = malloc(sizeof(unsigned char) * (recv_len + 1))) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); - goto exit; - } - memcpy(*value, recv, recv_len); - (*value)[recv_len] = '\0'; + if (value_len != NULL) { + *value_len = recv_len; + } + if (value != NULL) { + if ((*value = malloc(sizeof(unsigned char) * (recv_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; } + memcpy(*value, recv, recv_len); + (*value)[recv_len] = '\0'; + } /* * 7. Call post_read hook, if it exists diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 249ef613..27b8782e 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -1,11 +1,9 @@ #include "atclient/monitor.h" #include "atclient/atclient.h" #include "atclient/atnotification.h" -#include "atclient/cjson.h" #include "atclient/connection.h" #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/mbedtls.h" #include "atclient/string_utils.h" #include #include @@ -19,7 +17,7 @@ #define TAG "atclient_monitor" -static int parse_message(const char *original, char **message_type, char **message_body); +static int parse_message(char *original, char **message_type, char **message_body); static int parse_notification(atclient_atnotification *notification, const char *messagebody); static int decrypt_notification(atclient *monitor_conn, atclient_atnotification *notification); @@ -40,14 +38,11 @@ void atclient_monitor_response_free(atclient_monitor_response *message) { void atclient_monitor_init(atclient *monitor_conn) { atclient_init(monitor_conn); } void atclient_monitor_free(atclient *monitor_conn) { atclient_free(monitor_conn); } -int atclient_monitor_pkam_authenticate(atclient *monitor_conn, const char *atserver_host, const int atserver_port, - const atclient_atkeys *atkeys, const char *atsign) { +int atclient_monitor_pkam_authenticate(atclient *monitor_conn, const char *atsign, const atclient_atkeys *atkeys, + atclient_pkam_authenticate_options *options) { int ret = 1; - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - ret = atclient_pkam_authenticate(monitor_conn, atsign, atkeys, &options); - if (ret != 0) { + if ((ret = atclient_pkam_authenticate(monitor_conn, atsign, atkeys, options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n"); goto exit; } @@ -150,10 +145,8 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } - int i = 0; - while (buffer[i] != ':') { - i++; - } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, (int)strlen(buffer), buffer, + reset); char *messagetype = NULL; char *messagebody = NULL; @@ -164,9 +157,6 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, messagetype, messagebody, - reset); - if (strcmp(messagetype, "notification") == 0) { message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_NOTIFICATION; atclient_atnotification_init(&(message->notification)); @@ -244,61 +234,43 @@ bool atclient_monitor_is_connected(atclient *monitor_conn) { // given a string notification (*original is assumed to JSON parsable), we can deduce the message_type (e.g. data, // error, notification) and return the message body which is everything after the prefix (data:, error:, notification:). // This function will modify *message_type and *message_body to point to the respective values in *original. -static int parse_message(const char *original, char **message_type, char **message_body) { +static int parse_message(char *original, char **message_type, char **message_body) { int ret = -1; - - char *original_copy = strdup(original); char *temp = NULL; char *saveptr; - temp = strtok_r(original_copy, ":", &saveptr); + // Parse the message type (everything before ':') + temp = strtok_r(original, ":", &saveptr); if (temp == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse message type\n"); goto exit; } *message_type = temp; - temp = strtok_r(NULL, "\n", &saveptr); + // The rest of the string is the message body (JSON in this case) + temp = strtok_r(NULL, "", &saveptr); // Use an empty delimiter to get the rest of the string if (temp == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse message body\n"); goto exit; } *message_body = temp; - // if message_type starts with `@`, then it will follow this format: `@@` - // extract message_type from message_type - if ((*message_type)[0] == '@') { - char *temp = strtok_r(*message_type, "@", &saveptr); - if (temp == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse message type\n"); - goto exit; - } - *message_type = strtok_r(NULL, "@", &saveptr); - if (*message_type == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse message type\n"); - goto exit; - } + // Trim leading whitespace or newlines from message_body + while (**message_body == ' ' || **message_body == '\n') { + (*message_body)++; } - // if message_body has any leading or trailing white space or new line characters, remove it - while ((*message_body)[0] == ' ' || (*message_body)[0] == '\n') { - *message_body = *message_body + 1; + // Trim trailing whitespace or newlines from message_body + size_t trail = strlen(*message_body); + while (trail > 0 && ((*message_body)[trail - 1] == ' ' || (*message_body)[trail - 1] == '\n')) { + (*message_body)[--trail] = '\0'; } - size_t trail; - do { - trail = strlen(*message_body) - 1; - if ((*message_body)[trail] == ' ' || (*message_body)[trail] == '\n') { - (*message_body)[trail] = '\0'; - } - } while ((*message_body)[trail] == ' ' || (*message_body)[trail] == '\n'); ret = 0; - goto exit; -exit: { - free(original_copy); + +exit: return ret; } -} // populates *notification given a notification "*messagebody" which has been received from atServer static int parse_notification(atclient_atnotification *notification, const char *messagebody) { diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index bc10e6ba..bb091b26 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -1,8 +1,6 @@ #include "atclient/notify.h" #include "atclient/connection.h" -#include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/mbedtls.h" #include "atclient/string_utils.h" #include #include diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c index cebb37ae..2a359fd7 100644 --- a/packages/atclient/src/notify_params.c +++ b/packages/atclient/src/notify_params.c @@ -1,6 +1,5 @@ #include "atclient/notify_params.h" #include "atchops/aes.h" -#include "atclient/constants.h" #include #include #include diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 1f75d267..d52e728c 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -1437,8 +1437,8 @@ void atclient_get_atkeys_request_options_unset_show_hidden(atclient_get_atkeys_r void atclient_pkam_authenticate_options_init(atclient_pkam_authenticate_options *options) { memset(options, 0, sizeof(atclient_pkam_authenticate_options)); - options->at_directory_host = ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_HOST; - options->at_directory_port = ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_PORT; + options->atdirectory_host = ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_HOST; + options->atdirectory_port = ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_PORT; options->atserver_host = NULL; options->atserver_port = 0; } @@ -1456,11 +1456,11 @@ void atclient_pkam_authenticate_options_free(atclient_pkam_authenticate_options * 2. Free options */ - if (atclient_pkam_authenticate_options_is_at_directory_host_initialized(options)) { + if (atclient_pkam_authenticate_options_is_atdirectory_host_initialized(options)) { atclient_pkam_authenticate_options_unset_at_directory_host(options); } - if (atclient_pkam_authenticate_options_is_at_directory_port_initialized(options)) { + if (atclient_pkam_authenticate_options_is_atdirectory_port_initialized(options)) { atclient_pkam_authenticate_options_unset_at_directory_port(options); } @@ -1473,14 +1473,14 @@ void atclient_pkam_authenticate_options_free(atclient_pkam_authenticate_options } } -bool atclient_pkam_authenticate_options_is_at_directory_host_initialized( +bool atclient_pkam_authenticate_options_is_atdirectory_host_initialized( const atclient_pkam_authenticate_options *options) { /* * 1. Validate arguments */ if (options == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_pkam_authenticate_options_is_at_directory_host_initialized: " + "atclient_pkam_authenticate_options_is_atdirectory_host_initialized: " "Invalid arguments\n"); return false; } @@ -1492,14 +1492,14 @@ bool atclient_pkam_authenticate_options_is_at_directory_host_initialized( ATCLIENT_PKAM_AUTHENTICATE_OPTIONS_AT_DIRECTORY_HOST_INITIALIZED; } -bool atclient_pkam_authenticate_options_is_at_directory_port_initialized( +bool atclient_pkam_authenticate_options_is_atdirectory_port_initialized( const atclient_pkam_authenticate_options *options) { /* * 1. Validate arguments */ if (options == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_pkam_authenticate_options_is_at_directory_port_initialized: " + "atclient_pkam_authenticate_options_is_atdirectory_port_initialized: " "Invalid arguments\n"); return false; } @@ -1561,10 +1561,10 @@ void atclient_pkam_authenticate_options_unset_at_directory_host(atclient_pkam_au /* * 2. Unsetat directory */ - if (atclient_pkam_authenticate_options_is_at_directory_host_initialized(options)) { - free(options->at_directory_host); + if (atclient_pkam_authenticate_options_is_atdirectory_host_initialized(options)) { + free(options->atdirectory_host); } - options->at_directory_host = NULL; + options->atdirectory_host = NULL; } void atclient_pkam_authenticate_options_unset_at_directory_port(atclient_pkam_authenticate_options *options) { @@ -1576,7 +1576,7 @@ void atclient_pkam_authenticate_options_unset_at_directory_port(atclient_pkam_au "atclient_pkam_authenticate_options_unset_at_directory_port: Invalid arguments\n"); return; } - options->at_directory_port = 64; + options->atdirectory_port = 64; } void atclient_pkam_authenticate_options_unset_atserver_host(atclient_pkam_authenticate_options *options) { @@ -1611,7 +1611,7 @@ void atclient_pkam_authenticate_options_unset_atserver_port(atclient_pkam_authen } int atclient_pkam_authenticate_options_set_at_directory_host(atclient_pkam_authenticate_options *options, - char *at_directory_host) { + char *atdirectory_host) { int ret = 1; /* @@ -1627,21 +1627,21 @@ int atclient_pkam_authenticate_options_set_at_directory_host(atclient_pkam_authe /* * 2. Unset at directory host, if initialized */ - if (atclient_pkam_authenticate_options_is_at_directory_host_initialized(options)) { + if (atclient_pkam_authenticate_options_is_atdirectory_host_initialized(options)) { atclient_pkam_authenticate_options_unset_at_directory_host(options); } /* * 3. Set at directory host */ - options->at_directory_host = at_directory_host; + options->atdirectory_host = atdirectory_host; ret = 0; goto exit; exit: { return ret; } } int atclient_pkam_authenticate_options_set_at_directory_port(atclient_pkam_authenticate_options *options, - int at_directory_port) { + int atdirectory_port) { int ret = 1; /* @@ -1657,14 +1657,14 @@ int atclient_pkam_authenticate_options_set_at_directory_port(atclient_pkam_authe /* * 2. Unset at directory port, if initialized */ - if (atclient_pkam_authenticate_options_is_at_directory_port_initialized(options)) { + if (atclient_pkam_authenticate_options_is_atdirectory_port_initialized(options)) { atclient_pkam_authenticate_options_unset_at_directory_host(options); } /* * 3. Set at directory port */ - options->at_directory_port = at_directory_port; + options->atdirectory_port = atdirectory_port; ret = 0; goto exit; exit: { return ret; } diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 3f9e608b..c9f7c14c 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -41,17 +41,7 @@ int functional_tests_pkam_auth(atclient *atclient, atclient_atkeys *atkeys, cons atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "functional_tests_pkam_auth Begin\n"); - char *atserver_host = NULL; - int atserver_port = -1; - - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_find_atserver_address: %d\n", ret); - goto exit; - } - - atclient_pkam_authenticate_options options; - atclient_pkam_authenticate_options_init(&options); - if ((ret = atclient_pkam_authenticate(atclient, atsign, atkeys, &options)) != 0) { + if ((ret = atclient_pkam_authenticate(atclient, atsign, atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret); goto exit; } @@ -61,7 +51,6 @@ int functional_tests_pkam_auth(atclient *atclient, atclient_atkeys *atkeys, cons goto exit; exit: { - free(atserver_host); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "functional_tests_pkam_auth End (%d)\n", ret); return ret; } diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 2fa711b3..a65ff231 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -148,7 +148,7 @@ static int monitor_pkam_auth(atclient *monitor_conn, const atclient_atkeys *atke // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Host: %s\n", atserver_host); // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Port: %d\n", atserver_port); - if ((ret = atclient_monitor_pkam_authenticate(monitor_conn, atserver_host, atserver_port, atkeys, atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(monitor_conn, atsign, atkeys, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_monitor_pkam_authenticate: %d\n", ret); goto exit; } @@ -322,8 +322,8 @@ static int test_4_re_pkam_auth_and_start_monitor(atclient *monitor_conn) { char *atserver_host = strdup(monitor_conn->atserver_connection.host); int atserver_port = monitor_conn->atserver_connection.port; - if ((ret = atclient_monitor_pkam_authenticate(monitor_conn, atserver_host, atserver_port, &(monitor_conn->atkeys), - monitor_conn->atsign)) != 0) { + if ((ret = atclient_monitor_pkam_authenticate(monitor_conn, monitor_conn->atsign, &(monitor_conn->atkeys), + NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_pkam_authenticate.c b/tests/functional_tests/tests/test_atclient_pkam_authenticate.c new file mode 100644 index 00000000..e43881b3 --- /dev/null +++ b/tests/functional_tests/tests/test_atclient_pkam_authenticate.c @@ -0,0 +1,118 @@ +#include "atclient/request_options.h" +#include "functional_tests/config.h" +#include +#include +#include +#include +#include +#include +#include +#include + +static const size_t atkeyspathsize = 1024; +static char atkeyspath[1024]; +static char *atsign; + +static int test1_pkam_no_options(); +static int test2_pkam_with_options(); +// TODO: add apkam enrollment +// - can't do this as a unit test until we have at_activate in C +// static int test3_apkam_enrollment(); + +int main() { + int ret = 0; + + atsign = FIRST_ATSIGN; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + memset(atkeyspath, 0, atkeyspathsize); + size_t atkeyspathlen = 0; + + if ((ret = functional_tests_get_atkeys_path(atsign, strlen(atsign), atkeyspath, atkeyspathsize, &atkeyspathlen)) != + 0) { + atlogger_log("pkam_authenticate main", ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atkeys_sharedwith path: %d\n", + ret); + return ret; + } + + ret += test1_pkam_no_options(); + ret += test2_pkam_with_options(); + + return ret; +} + +static int test1_pkam_no_options() { + const char *tag = "test1_pkam_no_options"; + int ret = 0; + + atclient_atkeysfile atkeys_file; + atclient_atkeysfile_init(&atkeys_file); + + atclient_atkeys atkeys; + atclient_atkeys_init(&atkeys); + + atclient atclient; + atclient_init(&atclient); + + if ((ret = atclient_atkeysfile_from_path(&atkeys_file, atkeyspath)) != 0) { + return ret; + } + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeysfile_from_string: %d\n", ret); + + if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) { + return ret; + } + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret); + + if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) { + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); + return ret; + } else { + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n"); + } + + return ret; +} + +static int test2_pkam_with_options() { + const char *tag = "test2_pkam_with_options"; + int ret = 0; + + atclient_atkeysfile atkeys_file; + atclient_atkeysfile_init(&atkeys_file); + + atclient_atkeys atkeys; + atclient_atkeys_init(&atkeys); + + atclient atclient; + atclient_init(&atclient); + + if ((ret = atclient_atkeysfile_from_path(&atkeys_file, atkeyspath)) != 0) { + return ret; + } + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeysfile_from_string: %d\n", ret); + + if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) { + return ret; + } + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret); + + atclient_pkam_authenticate_options options; + atclient_pkam_authenticate_options_init(&options); + + if ((ret = atclient_utils_find_atserver_address(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST, + ATCLIENT_ATDIRECTORY_PRODUCTION_PORT, atsign, &options.atserver_host, + &options.atserver_port)) != 0) { + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret); + return ret; + } + + if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) { + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); + return ret; + } else { + atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n"); + } + return ret; +}