diff --git a/examples/desktop/repl/src/args.c b/examples/desktop/repl/src/args.c index a4f4a001..0c4183b5 100644 --- a/examples/desktop/repl/src/args.c +++ b/examples/desktop/repl/src/args.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #define TAG "repl_args" diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index c1a1749d..db75d710 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -217,4 +217,4 @@ static int start_repl_loop(atclient *atclient, repl_args *repl_args) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Exiting REPL loop...\n"); ret = 0; exit: { return ret; } -} \ No newline at end of file +} diff --git a/packages/atchops/tests/test_rsa_key_generate.c b/packages/atchops/tests/test_rsa_key_generate.c index 7bb433b7..802c632f 100644 --- a/packages/atchops/tests/test_rsa_key_generate.c +++ b/packages/atchops/tests/test_rsa_key_generate.c @@ -1,4 +1,6 @@ - +#include +#include +#include #include #include #include diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 8eedd767..5749fe62 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -14,6 +14,7 @@ #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_APKAM_SYMMETRIC_KEY_INDEX 0 #define ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED (VALUE_INITIALIZED << 1) @@ -21,6 +22,7 @@ #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) +#define ATCLIENT_ATKEYS_APKAM_SYMMETRIC_KEY_INITIALIZED (VALUE_INITIALIZED << 6) /** * @brief represents the atkeys file @@ -48,6 +50,7 @@ typedef struct atclient_atkeys { char *self_encryption_key_base64; // base64 encoded, AES-256 key, decrypted + char *apkam_symmetric_key_base64; char *enrollment_id; uint8_t _initialized_fields[1]; // used to track which fields have been initialized @@ -82,7 +85,11 @@ int atclient_atkeys_set_encrypt_private_key_base64(atclient_atkeys *atkeys, cons int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, 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_set_apkam_symmetric_key_base64(atclient_atkeys *atkeys, const char *apkamsymmetrickeybase64, + const size_t apkamsymmetrickeybase64len); + +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); @@ -101,6 +108,7 @@ bool atclient_atkeys_is_pkam_private_key_base64_initialized(atclient_atkeys *atk 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_apkam_symmetric_key_base64_initialized(atclient_atkeys *atkeys); bool atclient_atkeys_is_enrollment_id_initialized(atclient_atkeys *atkeys); /** @@ -122,6 +130,10 @@ bool atclient_atkeys_is_enrollment_id_initialized(atclient_atkeys *atkeys); * @param aes_encrypt_private_key_len the length of the aes_encrypt_private_key_str buffer * @param self_encryption_key_str the (decrypted) AES-256 selfencryptionkey in base64 format * @param self_encryption_key_len the length of the self_encryption_key_str buffer + * @param apkam_symmetric_key_str the (decrypted) AES-256 apkamsymmetrickey in base64 format, if this is an apkam key + * @param apkam_symmetric_key_str_len the length of the apkam_symmetric_key_str buffer, if this is an apkam key + * @param enrollment_id_str the enrollment id, if this is an apkam key + * @param enrollment_id_str_length the length of enrollment_id_str, if this is an apkam key * @return int 0 on success, non-zero on failure */ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aes_pkam_public_key_str, @@ -130,7 +142,9 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a 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 char *enrollment_id_str, const size_t enrollment_id_str_len); + const size_t self_encryption_key_str_len, const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_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_atkeys_file struct @@ -162,4 +176,8 @@ int atclient_atkeys_populate_from_path(atclient_atkeys *atkeys, const char *path */ int atclient_atkeys_populate_from_string(atclient_atkeys *atkeys, const char *file_string); +int atclient_atkeys_write_to_atkeys_file(atclient_atkeys *atkeys, atclient_atkeys_file *atkeys_file); + +int atclient_atkeys_write_to_path(atclient_atkeys *atkeys, const char *path); + #endif diff --git a/packages/atclient/include/atclient/atkeys_file.h b/packages/atclient/include/atclient/atkeys_file.h index 1a3c2949..fcc3130f 100644 --- a/packages/atclient/include/atclient/atkeys_file.h +++ b/packages/atclient/include/atclient/atkeys_file.h @@ -13,6 +13,7 @@ #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_APKAM_SYMMETRIC_KEY_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) @@ -20,8 +21,18 @@ #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) +#define ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INITIALIZED (VALUE_INITIALIZED << 6) + +#define ATCLIENT_ATKEYS_FILE_APKAM_PUBLIC_KEY_JSON_KEY "aesPkamPublicKey" +#define ATCLIENT_ATKEYS_FILE_APKAM_PRIVATE_KEY_JSON_KEY "aesPkamPrivateKey" +#define ATCLIENT_ATKEYS_FILE_DEFAULT_ENCRYPTION_PUBLIC_KEY_JSON_KEY "aesEncryptPublicKey" +#define ATCLIENT_ATKEYS_FILE_DEFAULT_ENCRYPTION_PRIVATE_KEY_JSON_KEY "aesEncryptPrivateKey" +#define ATCLIENT_ATKEYS_FILE_DEFAULT_SELF_ENCRYPTION_KEY_JSON_KEY "selfEncryptionKey" +#define ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_JSON_KEY "apkamSymmetricKey" +#define ATCLIENT_ATKEYS_FILE_APKAM_ENROLLMENT_ID_JSON_KEY "enrollmentId" typedef struct atclient_atkeys_file { + // note: `aes_` prefix means the field is encrypted with aes, not that the type of key is aes 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 @@ -32,6 +43,7 @@ typedef struct atclient_atkeys_file { // 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 *apkam_symmetric_key_str; char *enrollment_id_str; uint8_t _initialized_fields[1]; } atclient_atkeys_file; @@ -63,6 +75,14 @@ int atclient_atkeys_file_from_path(atclient_atkeys_file *atkeys_file, const char */ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const char *file_string); +/** + * @brief Write the struct to a file. + * + * @param atkeys_file the struct to be written to the file, assumed to be NON-NULL and initialized with atclient_atkeys_file_init + * @param path Example "$HOME/.atsign/keys/@alice_key.atKeys" + */ +int atclient_atkeys_file_write_to_path(atclient_atkeys_file *atkeys_file, const char *path); + /** * @brief Free the struct of any memory that was allocated during its lifetime * @@ -75,13 +95,28 @@ bool atclient_atkeys_file_is_aes_pkam_private_key_str_initialized(atclient_atkey bool atclient_atkeys_file_is_aes_encrypt_public_key_str_initialized(atclient_atkeys_file *atkeys_file); bool atclient_atkeys_file_is_aes_encrypt_private_key_str_initialized(atclient_atkeys_file *atkeys_file); bool atclient_atkeys_file_is_self_encryption_key_str_initialized(atclient_atkeys_file *atkeys_file); +bool atclient_atkeys_file_is_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file); bool atclient_atkeys_file_is_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file); -int atclient_atkeys_file_set_aes_pkam_public_key_str(atclient_atkeys_file *atkeys_file, const char *aes_pkam_public_key_str, const size_t aes_pkam_public_key_str_len); -int atclient_atkeys_file_set_aes_pkam_private_key_str(atclient_atkeys_file *atkeys_file, const char *aes_pkam_private_key_str, const size_t aes_pkam_private_key_str_len); -int atclient_atkeys_file_set_aes_encrypt_public_key_str(atclient_atkeys_file *atkeys_file, const char *aes_encrypt_public_key_str, const size_t aes_encrypt_public_key_str_len); -int atclient_atkeys_file_set_aes_encrypt_private_key_str(atclient_atkeys_file *atkeys_file, const char *aes_encrypt_private_key_str, const size_t aes_encrypt_private_key_str_len); -int atclient_atkeys_file_set_self_encryption_key_str(atclient_atkeys_file *atkeys_file, const char *self_encryption_key_str, const size_t self_encryption_key_str_len); -int atclient_atkeys_file_set_enrollment_id_str(atclient_atkeys_file *atkeys_file, const char *enrollment_id_str, const size_t enrollment_id_str_len); +int atclient_atkeys_file_set_aes_pkam_public_key_str(atclient_atkeys_file *atkeys_file, + const char *aes_pkam_public_key_str, + const size_t aes_pkam_public_key_str_len); +int atclient_atkeys_file_set_aes_pkam_private_key_str(atclient_atkeys_file *atkeys_file, + const char *aes_pkam_private_key_str, + const size_t aes_pkam_private_key_str_len); +int atclient_atkeys_file_set_aes_encrypt_public_key_str(atclient_atkeys_file *atkeys_file, + const char *aes_encrypt_public_key_str, + const size_t aes_encrypt_public_key_str_len); +int atclient_atkeys_file_set_aes_encrypt_private_key_str(atclient_atkeys_file *atkeys_file, + const char *aes_encrypt_private_key_str, + const size_t aes_encrypt_private_key_str_len); +int atclient_atkeys_file_set_self_encryption_key_str(atclient_atkeys_file *atkeys_file, + const char *self_encryption_key_str, + const size_t self_encryption_key_str_len); +int atclient_atkeys_file_set_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file, + const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_len); +int atclient_atkeys_file_set_enrollment_id_str(atclient_atkeys_file *atkeys_file, const char *enrollment_id_str, + const size_t enrollment_id_str_len); #endif diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 22fd7d5b..7c9d06f3 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -1,4 +1,5 @@ #include "atclient/atkeys.h" +#include "atclient/atkeys_file.h" #include "atlogger/atlogger.h" #include #include @@ -16,6 +17,7 @@ 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_apkam_symmetric_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); @@ -23,6 +25,7 @@ static void set_pkam_private_key_base64_initialized(atclient_atkeys *atkeys, con 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_apkam_symmetric_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); @@ -30,6 +33,7 @@ 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_apkam_symmetric_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, @@ -42,6 +46,8 @@ static int set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *e 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); +static int set_apkam_symmetric_key_base64(atclient_atkeys *atkeys, const char *apkam_symmetric_key_base64, + const size_t apkam_symmetric_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) { @@ -55,6 +61,7 @@ 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->apkam_symmetric_key_base64 = NULL; atkeys->enrollment_id = NULL; } @@ -82,6 +89,9 @@ void atclient_atkeys_free(atclient_atkeys *atkeys) { if (atclient_atkeys_is_self_encryption_key_base64_initialized(atkeys)) { unset_self_encryption_key_base64(atkeys); } + if (atclient_atkeys_is_apkam_symmetric_key_base64_initialized(atkeys)) { + unset_apkam_symmetric_key_base64(atkeys); + } if (atclient_atkeys_is_enrollment_id_initialized(atkeys)) { unset_enrollment_id(atkeys); } @@ -252,6 +262,39 @@ int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, cons exit: { return ret; } } +int atclient_atkeys_set_apkam_symmetric_key_base64(atclient_atkeys *atkeys, const char *apkam_symmetric_key_base64, + const size_t apkam_symmetric_key_base64_len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (apkam_symmetric_key_base64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "apkam_symmetric_key_base64 is NULL\n"); + return ret; + } + + if (apkam_symmetric_key_base64_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "apkam_symmetric_key_base64_len is 0\n"); + return ret; + } + + if ((ret = set_apkam_symmetric_key_base64(atkeys, apkam_symmetric_key_base64, apkam_symmetric_key_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_apkam_symmetric_key_base64: %d | failed to set apkam_symmetric_key_base64\n", ret); + goto exit; + } + + ret = 0; + goto exit; +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; @@ -440,6 +483,10 @@ bool atclient_atkeys_is_self_encryption_key_base64_initialized(atclient_atkeys * return is_self_encryption_key_base64_initialized(atkeys); } +bool atclient_atkeys_is_apkam_symmetric_key_base64_initialized(atclient_atkeys *atkeys) { + return is_apkam_symmetric_key_base64_initialized(atkeys); +} + bool atclient_atkeys_is_enrollment_id_initialized(atclient_atkeys *atkeys) { return is_enrollment_id_initialized(atkeys); } @@ -450,7 +497,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a 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 char *enrollment_id_str, + const size_t self_encryption_key_str_len, const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_len, const char *enrollment_id_str, const size_t enrollment_id_str_len) { int ret = 1; @@ -553,6 +601,12 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a memset(rsa_key_decrypted, 0, sizeof(unsigned char) * rsa_key_decrypted_size); size_t rsa_key_decrypted_len = 0; + // temporarily holds the base64-encoded non-encrypted apkam symmetric key + const size_t apkam_symmetric_key_size = ATCHOPS_AES_256 / 8; + unsigned char apkam_symmetric_key[apkam_symmetric_key_size]; + memset(apkam_symmetric_key, 0, sizeof(unsigned char) * apkam_symmetric_key_size); + size_t apkam_symmetric_key_len = 0; + /* * 3. Prepare self encryption key for use */ @@ -702,7 +756,22 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a goto exit; } - // 6. enrollment id, if it exists + // 6. apkam symmetric key, if it exists + if (apkam_symmetric_key_str != NULL && apkam_symmetric_key_str_len > 0) { + if ((ret = atchops_base64_decode((unsigned char *)apkam_symmetric_key_str, apkam_symmetric_key_str_len, + apkam_symmetric_key, apkam_symmetric_key_size, &apkam_symmetric_key_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding apkamsymmetric key: %d\n", ret); + goto exit; + } + if ((ret = atclient_atkeys_set_apkam_symmetric_key_base64(atkeys, apkam_symmetric_key_str, + apkam_symmetric_key_str_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_apkam_symmetric_key_base64: %d | failed to set apkam_symmetric_key_str\n", ret); + goto exit; + } + } + + // 7. 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); @@ -738,6 +807,7 @@ int atclient_atkeys_populate_from_atkeys_file(atclient_atkeys *atkeys, const atc 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->apkam_symmetric_key_str, strlen(atkeys_file->apkam_symmetric_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); @@ -749,7 +819,8 @@ int atclient_atkeys_populate_from_atkeys_file(atclient_atkeys *atkeys, const atc 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) { + atkeys_file->self_encryption_key_str, strlen(atkeys_file->self_encryption_key_str), NULL, 0, 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; @@ -812,6 +883,221 @@ exit: { } } +int atclient_atkeys_write_to_atkeys_file(atclient_atkeys *atkeys, atclient_atkeys_file *atkeys_file) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + // mandatory field that constitutes an atSign's atkeys + if (!atclient_atkeys_is_encrypt_private_key_base64_initialized(atkeys)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt private key is not initialized\n"); + return ret; + } + + // mandatory field that constitutes an atSign's atkeys + if (!atclient_atkeys_is_encrypt_public_key_base64_initialized(atkeys)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt public key is not initialized\n"); + return ret; + } + + // mandatory field that constitutes an atSign's atkeys + if (!atclient_atkeys_is_pkam_private_key_base64_initialized(atkeys)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam private key is not initialized\n"); + return ret; + } + + // mandatory field that constitutes an atSign's atkeys + if (!atclient_atkeys_is_pkam_public_key_base64_initialized(atkeys)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam public key is not initialized\n"); + return ret; + } + + // mandatory field that constitutes an atSign's atkeys + if (!atclient_atkeys_is_self_encryption_key_base64_initialized(atkeys)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self encryption key is not initialized\n"); + return ret; + } + + if (atkeys_file == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys_file is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; + + const size_t self_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char self_encryption_key[self_encryption_key_size]; + + const size_t rsa_key_encrypted_size = atchops_base64_encoded_size( + strlen(atkeys->pkam_private_key_base64)); // use private key as the largest buffer size + unsigned char rsa_key_encrypted[rsa_key_encrypted_size]; + size_t rsa_key_encrypted_len = 0; + + const size_t rsa_key_encrypted_base64_size = atchops_base64_encoded_size(rsa_key_encrypted_size); + unsigned char rsa_key_encrypted_base64[rsa_key_encrypted_base64_size]; + size_t rsa_key_encrypted_base64_len = 0; + + /* + * 3. Prepare self encryption key for use + */ + if ((ret = atchops_base64_decode((unsigned char *)atkeys->self_encryption_key_base64, + strlen(atkeys->self_encryption_key_base64), self_encryption_key, + self_encryption_key_size, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "base64 decode self encryption key: %d\n", ret); + goto exit; + } + + /* + * 4. Encrypt and write to atkeys file + */ + + // 4a. pkam public key + memset(iv, 0, sizeof(unsigned char) * iv_size); // Use legacy IV + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(self_encryption_key, ATCHOPS_AES_256, iv, + (unsigned char *)atkeys->pkam_public_key_base64, + strlen(atkeys->pkam_public_key_base64), rsa_key_encrypted, rsa_key_encrypted_size, + &rsa_key_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt pkam public key: %d\n", ret); + goto exit; + } + + if((ret = atchops_base64_encode(rsa_key_encrypted, rsa_key_encrypted_len, rsa_key_encrypted_base64, rsa_key_encrypted_base64_size, &rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "base64 encode pkam public key: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkeys_file_set_aes_pkam_public_key_str(atkeys_file, (const char *)rsa_key_encrypted_base64, + rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set aes pkam public key str: %d\n", ret); + goto exit; + } + + // 4b. pkam private key + memset(iv, 0, sizeof(unsigned char) * iv_size); // Use legacy IV + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(self_encryption_key, ATCHOPS_AES_256, iv, + (unsigned char *)atkeys->pkam_private_key_base64, + strlen(atkeys->pkam_private_key_base64), rsa_key_encrypted, rsa_key_encrypted_size, + &rsa_key_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt pkam private key: %d\n", ret); + goto exit; + } + + if((ret = atchops_base64_encode(rsa_key_encrypted, rsa_key_encrypted_len, rsa_key_encrypted_base64, rsa_key_encrypted_base64_size, &rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "base64 encode pkam private key: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkeys_file_set_aes_pkam_private_key_str(atkeys_file, (const char *)rsa_key_encrypted_base64, + rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set aes pkam private key str: %d\n", ret); + goto exit; + } + + // 4c. encrypt public key + memset(iv, 0, sizeof(unsigned char) * iv_size); // Use legacy IV + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(self_encryption_key, ATCHOPS_AES_256, iv, + (unsigned char *)atkeys->encrypt_public_key_base64, + strlen(atkeys->encrypt_public_key_base64), rsa_key_encrypted, + rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt encrypt public key: %d\n", ret); + goto exit; + } + + if((ret = atchops_base64_encode(rsa_key_encrypted, rsa_key_encrypted_len, rsa_key_encrypted_base64, rsa_key_encrypted_base64_size, &rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "base64 encode encrypt public key: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkeys_file_set_aes_encrypt_public_key_str(atkeys_file, (const char *)rsa_key_encrypted_base64, + rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set aes encrypt public key str: %d\n", ret); + goto exit; + } + + // 4d. encrypt private key + memset(iv, 0, sizeof(unsigned char) * iv_size); // Use legacy IV + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(self_encryption_key, ATCHOPS_AES_256, iv, + (unsigned char *)atkeys->encrypt_private_key_base64, + strlen(atkeys->encrypt_private_key_base64), rsa_key_encrypted, + rsa_key_encrypted_size, &rsa_key_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt encrypt private key: %d\n", ret); + goto exit; + } + + if((ret = atchops_base64_encode(rsa_key_encrypted, rsa_key_encrypted_len, rsa_key_encrypted_base64, rsa_key_encrypted_base64_size, &rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "base64 encode encrypt private key: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkeys_file_set_aes_encrypt_private_key_str(atkeys_file, (const char *)rsa_key_encrypted_base64, + rsa_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set aes encrypt private key str: %d\n", ret); + goto exit; + } + + // 4e. self encryption key + if ((ret = atclient_atkeys_file_set_self_encryption_key_str(atkeys_file, atkeys->self_encryption_key_base64, strlen(atkeys->self_encryption_key_base64))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set self encryption key str: %d\n", ret); + goto exit; + } + + // 4f. enrollment id (optional) + if (atclient_atkeys_is_enrollment_id_initialized(atkeys)) { + if ((ret = atclient_atkeys_file_set_enrollment_id_str(atkeys_file, atkeys->enrollment_id, strlen(atkeys->enrollment_id))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set enrollment id str: %d\n", ret); + goto exit; + } + } + + ret = 0; + +exit: { return ret; } +} + +int atclient_atkeys_write_to_path(atclient_atkeys *atkeys, const char *path) { + int ret = 1; + + atclient_atkeys_file atkeys_file; + atclient_atkeys_file_init(&atkeys_file); + + if ((ret = atclient_atkeys_write_to_atkeys_file(atkeys, &atkeys_file)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeys_write_to_atkeys_file: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkeys_file_write_to_path(&atkeys_file, path)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeys_file_to_path: %d\n", ret); + goto exit; + } + + ret = 0; +exit: { + atclient_atkeys_file_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; @@ -837,6 +1123,11 @@ static bool is_self_encryption_key_base64_initialized(atclient_atkeys *atkeys) { ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; } +static bool is_apkam_symmetric_key_base64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initialized_fields[ATCLIENT_ATKEYS_APKAM_SYMMETRIC_KEY_INDEX] & + ATCLIENT_ATKEYS_APKAM_SYMMETRIC_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; } @@ -888,6 +1179,16 @@ static void set_self_encryption_key_base64_initialized(atclient_atkeys *atkeys, } } +static void set_apkam_symmetric_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initialized_fields[ATCLIENT_ATKEYS_APKAM_SYMMETRIC_KEY_INDEX] |= + ATCLIENT_ATKEYS_APKAM_SYMMETRIC_KEY_INITIALIZED; + } else { + atkeys->_initialized_fields[ATCLIENT_ATKEYS_APKAM_SYMMETRIC_KEY_INDEX] &= + ~ATCLIENT_ATKEYS_APKAM_SYMMETRIC_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; @@ -936,6 +1237,14 @@ static void unset_self_encryption_key_base64(atclient_atkeys *atkeys) { set_self_encryption_key_base64_initialized(atkeys, false); } +static void unset_apkam_symmetric_key_base64(atclient_atkeys *atkeys) { + if (is_apkam_symmetric_key_base64_initialized(atkeys) && atkeys->apkam_symmetric_key_base64 != NULL) { + free(atkeys->apkam_symmetric_key_base64); + } + atkeys->apkam_symmetric_key_base64 = NULL; + set_apkam_symmetric_key_base64_initialized(atkeys, false); +} + static void unset_enrollment_id(atclient_atkeys *atkeys) { if (is_enrollment_id_initialized(atkeys)) { free(atkeys->enrollment_id); @@ -1082,6 +1391,35 @@ static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *s return ret; } +static int set_apkam_symmetric_key_base64(atclient_atkeys *atkeys, const char *apkam_symmetric_key_base64, + const size_t apkam_symmetric_key_len) { + int ret = 1; + + if (apkam_symmetric_key_base64 == NULL || apkam_symmetric_key_len == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid apkam symmetric key or key length\n"); + return 1; + } + + if (is_apkam_symmetric_key_base64_initialized(atkeys)) { + unset_apkam_symmetric_key_base64(atkeys); + } + + const size_t apkam_symmetric_key_size = apkam_symmetric_key_len + 1; + atkeys->apkam_symmetric_key_base64 = (char *)malloc(apkam_symmetric_key_size); + if (atkeys->apkam_symmetric_key_base64 == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for apkam_symmetric_key_base64\n"); + return 1; + } + + memcpy(atkeys->apkam_symmetric_key_base64, apkam_symmetric_key_base64, apkam_symmetric_key_len); + atkeys->apkam_symmetric_key_base64[apkam_symmetric_key_len] = '\0'; // Null-terminate the string + + set_apkam_symmetric_key_base64_initialized(atkeys, true); + + ret = 0; + return ret; +} + static int set_enrollment_id(atclient_atkeys *atkeys, const char *enrollment_id, const size_t enrollment_id_len) { int ret = 1; diff --git a/packages/atclient/src/atkeys_file.c b/packages/atclient/src/atkeys_file.c index d37724b0..8e249321 100644 --- a/packages/atclient/src/atkeys_file.c +++ b/packages/atclient/src/atkeys_file.c @@ -17,6 +17,7 @@ static bool is_aes_pkam_private_key_str_initialized(atclient_atkeys_file *atkeys static bool is_aes_encrypt_public_key_str_initialized(atclient_atkeys_file *atkeys_file); static bool is_aes_encrypt_private_key_str_initialized(atclient_atkeys_file *atkeys_file); static bool is_self_encryption_key_str_initialized(atclient_atkeys_file *atkeys_file); +static bool is_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file); static bool is_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file); static void set_aes_pkam_public_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); @@ -24,6 +25,7 @@ static void set_aes_pkam_private_key_str_initialized(atclient_atkeys_file *atkey static void set_aes_encrypt_public_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); static void set_aes_encrypt_private_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); static void set_self_encryption_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); +static void set_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); static void set_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized); static void unset_aes_pkam_public_key_str(atclient_atkeys_file *atkeys_file); @@ -31,6 +33,7 @@ static void unset_aes_pkam_private_key_str(atclient_atkeys_file *atkeys_file); static void unset_aes_encrypt_public_key_str(atclient_atkeys_file *atkeys_file); static void unset_aes_encrypt_private_key_str(atclient_atkeys_file *atkeys_file); static void unset_self_encryption_key_str(atclient_atkeys_file *atkeys_file); +static void unset_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file); static void unset_enrollment_id_str(atclient_atkeys_file *atkeys_file); static int set_aes_pkam_public_key_str(atclient_atkeys_file *atkeys_file, const char *aes_pkam_public_key_str, @@ -43,10 +46,14 @@ static int set_aes_encrypt_private_key_str(atclient_atkeys_file *atkeys_file, co const size_t aes_encrypt_private_key_str_len); static int set_self_encryption_key_str(atclient_atkeys_file *atkeys_file, const char *self_encryption_key_str, const size_t self_encryption_key_str_len); +static int set_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file, const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_len); static int set_enrollment_id_str(atclient_atkeys_file *atkeys_file, const char *enrollment_id_str, const size_t enrollment_id_str_len); -void atclient_atkeys_file_init(atclient_atkeys_file *atkeys_file) { memset(atkeys_file, 0, sizeof(atclient_atkeys_file)); } +void atclient_atkeys_file_init(atclient_atkeys_file *atkeys_file) { + memset(atkeys_file, 0, sizeof(atclient_atkeys_file)); +} int atclient_atkeys_file_from_path(atclient_atkeys_file *atkeys_file, const char *path) { int ret = 1; @@ -79,7 +86,7 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch return ret; } - cJSON *aes_pkam_public_key = cJSON_GetObjectItem(root, "aesPkamPublicKey"); + cJSON *aes_pkam_public_key = cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_APKAM_PUBLIC_KEY_JSON_KEY); if (aes_pkam_public_key == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesPkamPublicKey from JSON\n"); goto exit; @@ -91,7 +98,7 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch goto exit; } - cJSON *aes_pkam_private_key = cJSON_GetObjectItem(root, "aesPkamPrivateKey"); + cJSON *aes_pkam_private_key = cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_APKAM_PRIVATE_KEY_JSON_KEY); if (aes_pkam_private_key == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesPkamPrivateKey from JSON\n"); goto exit; @@ -102,7 +109,8 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch goto exit; } - cJSON *aes_encrypt_public_key = cJSON_GetObjectItem(root, "aesEncryptPublicKey"); + cJSON *aes_encrypt_public_key = + cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_DEFAULT_ENCRYPTION_PUBLIC_KEY_JSON_KEY); if (aes_encrypt_public_key == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesEncryptPublicKey from JSON\n"); goto exit; @@ -113,7 +121,8 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch goto exit; } - cJSON *aes_encrypt_private_key = cJSON_GetObjectItem(root, "aesEncryptPrivateKey"); + cJSON *aes_encrypt_private_key = + cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_DEFAULT_ENCRYPTION_PRIVATE_KEY_JSON_KEY); if (aes_encrypt_private_key == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read aesEncryptPrivateKey from JSON\n"); goto exit; @@ -124,7 +133,7 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch goto exit; } - cJSON *self_encryption_key = cJSON_GetObjectItem(root, "selfEncryptionKey"); + cJSON *self_encryption_key = cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_DEFAULT_SELF_ENCRYPTION_KEY_JSON_KEY); if (self_encryption_key == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read selfEncryptionKey from JSON\n"); goto exit; @@ -135,7 +144,18 @@ int atclient_atkeys_file_from_string(atclient_atkeys_file *atkeys_file, const ch goto exit; } - cJSON *enrollment_id = cJSON_GetObjectItem(root, "enrollmentId"); + cJSON *apkam_symmetric_key = cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_JSON_KEY); + if (apkam_symmetric_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read apkamSymmetricKey from JSON\n"); + goto exit; + } + if ((ret = set_apkam_symmetric_key_str(atkeys_file, apkam_symmetric_key->valuestring, + strlen(apkam_symmetric_key->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_apkam_symmetric_key_str: %d\n", ret); + goto exit; + } + + cJSON *enrollment_id = cJSON_GetObjectItem(root, ATCLIENT_ATKEYS_FILE_APKAM_ENROLLMENT_ID_JSON_KEY); if (enrollment_id != NULL) { if ((ret = set_enrollment_id_str(atkeys_file, enrollment_id->valuestring, strlen(enrollment_id->valuestring))) != 0) { @@ -152,20 +172,130 @@ exit: { } } +int atclient_atkeys_file_write_to_path(atclient_atkeys_file *atkeys_file, const char *path) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (atkeys_file == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys_file is NULL\n"); + return ret; + } + + if (!atclient_atkeys_file_is_aes_encrypt_private_key_str_initialized(atkeys_file)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_private_key_str is not initialized\n"); + return ret; + } + + if (!atclient_atkeys_file_is_aes_encrypt_public_key_str_initialized(atkeys_file)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_public_key_str is not initialized\n"); + return ret; + } + + if (!atclient_atkeys_file_is_aes_pkam_private_key_str_initialized(atkeys_file)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_private_key_str is not initialized\n"); + return ret; + } + + if (!atclient_atkeys_file_is_aes_pkam_public_key_str_initialized(atkeys_file)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_public_key_str is not initialized\n"); + return ret; + } + + if (!atclient_atkeys_file_is_self_encryption_key_str_initialized(atkeys_file)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_str is not initialized\n"); + return ret; + } + + if (path == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "path is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ + + cJSON *root = NULL; // free later + char *json_str = NULL; // free later + + root = cJSON_CreateObject(); + if (root == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_CreateObject failed\n"); + goto exit; + } + + if (is_aes_pkam_public_key_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "aesPkamPublicKey", atkeys_file->aes_pkam_public_key_str); + } + + if (is_aes_pkam_private_key_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "aesPkamPrivateKey", atkeys_file->aes_pkam_private_key_str); + } + + if (is_aes_encrypt_public_key_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "aesEncryptPublicKey", atkeys_file->aes_encrypt_public_key_str); + } + + if (is_aes_encrypt_private_key_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "aesEncryptPrivateKey", atkeys_file->aes_encrypt_private_key_str); + } + + if (is_self_encryption_key_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "selfEncryptionKey", atkeys_file->self_encryption_key_str); + } + + if (is_enrollment_id_str_initialized(atkeys_file)) { + cJSON_AddStringToObject(root, "enrollmentId", atkeys_file->enrollment_id_str); + } + + json_str = cJSON_PrintUnformatted(root); + if (json_str == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print failed\n"); + goto exit; + } + + FILE *file = fopen(path, "w"); + if (file == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fopen failed\n"); + goto exit; + } + + const size_t bytes_written = fwrite(json_str, 1, strlen(json_str), file); + fclose(file); + if (bytes_written == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fwrite failed\n"); + goto exit; + } + + ret = 0; +exit: { + if(json_str != NULL) { + free(json_str); + } + if(root != NULL) { + cJSON_Delete(root); + } + return ret; +} +} + void atclient_atkeys_file_free(atclient_atkeys_file *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_apkam_symmetric_key_str(atkeys_file); unset_enrollment_id_str(atkeys_file); } -bool atclient_atkeys_file_is_aes_pkam_public_key_str_initialized(atclient_atkeys_file *atkeys_file) { +bool atclient_atkeys_file_is_pkam_public_key_str_initialized(atclient_atkeys_file *atkeys_file) { return is_aes_pkam_public_key_str_initialized(atkeys_file); } -bool atclient_atkeys_file_is_aes_pkam_private_key_str_initialized(atclient_atkeys_file *atkeys_file) { +bool atclient_atkeys_file_is_pkam_private_key_str_initialized(atclient_atkeys_file *atkeys_file) { return is_aes_pkam_private_key_str_initialized(atkeys_file); } @@ -181,13 +311,17 @@ bool atclient_atkeys_file_is_self_encryption_key_str_initialized(atclient_atkeys return is_self_encryption_key_str_initialized(atkeys_file); } -bool atclient_atkeys_file_is_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file) { +bool atclient_atkeys_file_is_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file) { return is_enrollment_id_str_initialized(atkeys_file); } +bool atclient_atkeys_file_is_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file) { + return is_apkam_symmetric_key_str_initialized(atkeys_file); +} + int atclient_atkeys_file_set_aes_pkam_public_key_str(atclient_atkeys_file *atkeys_file, - const char *aes_pkam_public_key_str, - const size_t aes_pkam_public_key_str_len) { + 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) { @@ -218,8 +352,8 @@ exit: { return ret; } } int atclient_atkeys_file_set_aes_pkam_private_key_str(atclient_atkeys_file *atkeys_file, - const char *aes_pkam_private_key_str, - const size_t aes_pkam_private_key_str_len) { + 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) { @@ -250,8 +384,8 @@ exit: { return ret; } } int atclient_atkeys_file_set_aes_encrypt_public_key_str(atclient_atkeys_file *atkeys_file, - const char *aes_encrypt_public_key_str, - const size_t aes_encrypt_public_key_str_len) { + 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) { @@ -283,8 +417,8 @@ exit: { return ret; } } int atclient_atkeys_file_set_aes_encrypt_private_key_str(atclient_atkeys_file *atkeys_file, - const char *aes_encrypt_private_key_str, - const size_t aes_encrypt_private_key_str_len) { + 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) { @@ -316,8 +450,8 @@ exit: { return ret; } } int atclient_atkeys_file_set_self_encryption_key_str(atclient_atkeys_file *atkeys_file, - const char *self_encryption_key_str, - const size_t self_encryption_key_str_len) { + const char *self_encryption_key_str, + const size_t self_encryption_key_str_len) { int ret = 1; if (self_encryption_key_str == NULL) { @@ -347,8 +481,40 @@ int atclient_atkeys_file_set_self_encryption_key_str(atclient_atkeys_file *atkey exit: { return ret; } } +int atclient_atkeys_file_set_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file, + const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_len) { + int ret = 1; + + if (apkam_symmetric_key_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "apkam_symmetric_key_str is NULL\n"); + return ret; + } + + if (apkam_symmetric_key_str_len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "apkam_symmetric_key_str_len is 0\n"); + return ret; + } + + if (is_apkam_symmetric_key_str_initialized(atkeys_file)) { + unset_apkam_symmetric_key_str(atkeys_file); + } + + if ((ret = set_apkam_symmetric_key_str(atkeys_file, apkam_symmetric_key_str, apkam_symmetric_key_str_len)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_apkam_symmetric_key_str: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + int atclient_atkeys_file_set_enrollment_id_str(atclient_atkeys_file *atkeys_file, const char *enrollment_id_str, - const size_t enrollment_id_str_len) { + const size_t enrollment_id_str_len) { int ret = 1; if (enrollment_id_str == NULL) { @@ -403,6 +569,11 @@ static bool is_self_encryption_key_str_initialized(atclient_atkeys_file *atkeys_ ATCLIENT_ATKEYS_FILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; } +static bool is_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file) { + return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INDEX] & + ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INITIALIZED; +} + static bool is_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file) { return atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX] & ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INITIALIZED; @@ -458,6 +629,16 @@ static void set_self_encryption_key_str_initialized(atclient_atkeys_file *atkeys } } +static void set_apkam_symmetric_key_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized) { + if (initialized) { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INDEX] |= + ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INITIALIZED; + } else { + atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INDEX] &= + ~ATCLIENT_ATKEYS_FILE_APKAM_SYMMETRIC_KEY_STR_INITIALIZED; + } +} + static void set_enrollment_id_str_initialized(atclient_atkeys_file *atkeys_file, const bool initialized) { if (initialized) { atkeys_file->_initialized_fields[ATCLIENT_ATKEYS_FILE_ENROLLMENT_ID_STR_INDEX] |= @@ -508,6 +689,14 @@ static void unset_self_encryption_key_str(atclient_atkeys_file *atkeys_file) { set_self_encryption_key_str_initialized(atkeys_file, false); } +static void unset_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file) { + if (is_apkam_symmetric_key_str_initialized(atkeys_file)) { + free(atkeys_file->apkam_symmetric_key_str); + } + atkeys_file->apkam_symmetric_key_str = NULL; + set_apkam_symmetric_key_str_initialized(atkeys_file, false); +} + static void unset_enrollment_id_str(atclient_atkeys_file *atkeys_file) { if (is_enrollment_id_str_initialized(atkeys_file)) { free(atkeys_file->enrollment_id_str); @@ -639,6 +828,31 @@ static int set_self_encryption_key_str(atclient_atkeys_file *atkeys_file, const exit: { return ret; } } +static int set_apkam_symmetric_key_str(atclient_atkeys_file *atkeys_file, const char *apkam_symmetric_key_str, + const size_t apkam_symmetric_key_str_len) { + int ret = 1; + + if (is_apkam_symmetric_key_str_initialized(atkeys_file)) { + unset_apkam_symmetric_key_str(atkeys_file); + } + + const size_t apkamsymmterickeystrsize = apkam_symmetric_key_str_len + 1; + if ((atkeys_file->apkam_symmetric_key_str = (char *)malloc(sizeof(char) * apkamsymmterickeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_apkam_symmetric_key_str_initialized(atkeys_file, true); + memcpy(atkeys_file->apkam_symmetric_key_str, apkam_symmetric_key_str, apkam_symmetric_key_str_len); + atkeys_file->apkam_symmetric_key_str[apkam_symmetric_key_str_len] = '\0'; + + ret = 0; + goto exit; + +exit: { return ret; } +} + static int set_enrollment_id_str(atclient_atkeys_file *atkeys_file, const char *enrollment_id_str, const size_t enrollment_id_str_len) { int ret = 1; diff --git a/packages/atclient/tests/test_atkeys_write.c b/packages/atclient/tests/test_atkeys_write.c new file mode 100644 index 00000000..728e6914 --- /dev/null +++ b/packages/atclient/tests/test_atkeys_write.c @@ -0,0 +1,122 @@ +#include +#include +#include + +#define TAG "test_atkeys_write" + +#define ATKEYS_8INCANTEATER \ + "{\"aesPkamPublicKey\":\"+Jp1VdQuMANVkktasZhnZ9UStUMW0JMnNNYAqXWhyMHDB1PQMpCMWDzAQwEkI8/UVCY33Xot6BUTvTjMqJj/" \ + "qbwCTbth+HE32qL6dKSiub6gs+5zYhWPN7x4O+QSBxiNrXgWjvy3Gz74B9EnOZsOKUHnxRFkMpxGlL0T7zRoV/" \ + "P4IInXj9vBtLDV0EGawJEUiDhY7OLpTeYUxH0pGSlh8sjNhG1FW9DSA9eMruhLnWdMdZsJ0fdd3QGT2hReHOpcAxWcV7oFe3N5l7/" \ + "IGEQi7NcoWSl8gIn+7gcqiRAaBueQKlgV3XmkcrgUeVifWtlJPMcB7HbBiYggPKUnus2k+f+" \ + "bvNnvCBgjsfFCiyDPijTdL7S3lo9dO9bzG22c90yIDgIo78Zb7HSHVhFs4QGmtNLQSKCp/" \ + "39RTopHPMh+zFnRsj+YFBknsHUYss+LsLql+UfA7kEb3L8PM4FRd/" \ + "XF5BfiI9S+ETvwF0DdJA8jo3F744YT2hMdG8r384keSxd7Y2Vm8S7UEtTU6Fo9w9Dn0Q==\",\"aesPkamPrivateKey\":\"+" \ + "Jp1UusFOA9WsWFlm5RlUYQO7EZu9uYjNMYKrHWxo8PMBUPlFpCUcjz2QwFTO7PsLUcL7Uc78Bciqxy0htHQq8QgEaZ8hTQjsrviaKufvY6DkrJPMSu" \ + "lMoZLYbIVcT2S7xwd186mAhaFQPdlaJULFTjXy2wxBqxGvp4lqHV7JcbjQb6Qr8n4p8LJnFCvu/" \ + "1O+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g7GkddUfj4VKIcZJT2NGKEmVc6pq3WKaW3UG951yiYV0LONyEPB4mh20IbdU3r1PiIlII+sRiSmpZHk8u9/" \ + "SGNUc50ZyWri58S4UhZGMNjrc7MC2NDiNqpk3AwUjNFsd0fWW4CkCk3t6i2qZ/" \ + "qYFR2wDkFqLBbpqJjdbhFCHaRwMVoIU+2k8bTtg6UdTy7qKKAaeIHf3sZb6WsakOqb8Fu6kFOqUg1G9ImJkssA6V7EnWkWFkaDonAW7xSmKz1+" \ + "qazCLD1EdeYmtJZNn4zxg48IhyYv6+O/AA9J3Wpezkc3qOhV1HSjS1tV2/Zuu00BsJC+uPjF/" \ + "qI8ApvFKjgYb6JuztNr5eOuyjfPdfNFxNjqUo5DAMwZ2dkB1DS4H2eZPmnjo7p7gMNBJtQTKwknoW1mgaGgyKzMjyOdcMFtja4/" \ + "kgjdx71N938eGah/" \ + "MNxh6UlKOfmOHyEiHTrgPpVrbw7psQ5vREtrBUHDK2efqS4lStFjEsxyXZse8ebjJ2mdytPTD0jLps6izyTA+" \ + "ariIFEOfhAFUyPPGfHl5wpkhMklfSh6Xsdb4Ru9t6eU4QcFQoO7/ZQKu6cyrt4a+zS6r7K4CMG3EN/" \ + "qItGp3XlZdWcICVRr13CgSAZFvudjvTADA+GgpviKbja0Z33N9TW2W/SAoU\",\"selfEncryptionKey\":\"ErES7LlWlIJAUEEwfvwqhjyi/" \ + "NvTQ964uojS8KYcHvI=\",\"@8incanteater\":\"ErES7LlWlIJAUEEwfvwqhjyi/NvTQ964uojS8KYcHvI=\"}" + +#define ATKEYS_8INCANTEATER_1_FILE_PATH "@8incanteater_key1.atKeys" + +int main(int argc, char *argv[]) { + int ret = 1; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + atclient_atkeys atkeys; + atclient_atkeys_init(&atkeys); + + atclient_atkeys atkeys1; + atclient_atkeys_init(&atkeys1); + + atclient atclient1; + atclient_init(&atclient1); + + if ((ret = atclient_atkeys_populate_from_string(&atkeys, ATKEYS_8INCANTEATER)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "failed to populate atkeys from string\n"); + goto exit; + } + + if ((ret = atclient_atkeys_write_to_path(&atkeys, ATKEYS_8INCANTEATER_1_FILE_PATH))) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "failed to write to path\n"); + goto exit; + } + + if ((ret = atclient_atkeys_populate_from_path(&atkeys1, ATKEYS_8INCANTEATER_1_FILE_PATH))) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "failed to populate from path\n"); + goto exit; + } + + if ((ret = atclient_pkam_authenticate(&atclient1, "@8incanteater", &atkeys1, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "failed to pkam auth\n"); + goto exit; + } + +exit: { + atclient_atkeys_free(&atkeys); + atclient_atkeys_free(&atkeys1); + atclient_free(&atclient1); + return ret; +} +} \ No newline at end of file