From 60d117b8f5ee6aecde5aaf3103c79253cc712a48 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 8 Jul 2024 16:13:59 -0400 Subject: [PATCH 001/193] fix: atkey compound namespacing --- packages/atclient/src/atkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index f7b8e268..5c78eae7 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -164,7 +164,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); goto exit; } - char *namespacestr = strtok_r(NULL, ".", &saveptr2); + char *namespacestr = strtok_r(NULL, "", &saveptr2); if (namespacestr == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. %s atkey is probably incomplete\n", atkeystr); From b0b3159d0bf84f3cc1d9c4c04b33f95a64fb6b40 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 8 Jul 2024 16:14:09 -0400 Subject: [PATCH 002/193] test: test_atclient_atkey_from_string --- .../atclient/tests/test_atkey_from_string.c | 331 +++++++++++------- 1 file changed, 204 insertions(+), 127 deletions(-) diff --git a/packages/atclient/tests/test_atkey_from_string.c b/packages/atclient/tests/test_atkey_from_string.c index 6314740e..e1af8e27 100644 --- a/packages/atclient/tests/test_atkey_from_string.c +++ b/packages/atclient/tests/test_atkey_from_string.c @@ -1,8 +1,8 @@ #include "atclient/atkey.h" #include "atlogger/atlogger.h" #include -#include #include +#include #define TAG "test_atkey_from_string" @@ -15,6 +15,7 @@ #define TEST_ATKEY_FROM_STRING_1C "public:name.wavi@jeremy" // 1D. cached public key with namespace #define TEST_ATKEY_FROM_STRING_1D "cached:public:name.wavi@jeremy" + // Test 2: shared keys // 2A: non-cached shared key with namespace #define TEST_ATKEY_FROM_STRING_2A "@alice:name.wavi@bob" @@ -24,9 +25,15 @@ #define TEST_ATKEY_FROM_STRING_2C "@bob:name@alice" // 2D: cached shared key with namespace #define TEST_ATKEY_FROM_STRING_2D "cached:@bob:name.wavi@alice" +// 2E: non-cached shared key with compounding namespace +#define TEST_ATKEY_FROM_STRING_2E "@alice:name.vpsx.sshnp.abcd.efgh@xavierbob123" +// 2F: cached shared key with compounding namespace +#define TEST_ATKEY_FROM_STRING_2F "cached:@jeremy:name.vps1.sshnp@xavier" + // Test 3: private hidden keys // 3A: private hidden key #define TEST_ATKEY_FROM_STRING_3A "_lastnotificationid@alice123_4😘" + // Test 4: self keys // 4A: self key with no namespace #define TEST_ATKEY_FROM_STRING_4A "name@alice" @@ -56,25 +63,23 @@ static int test1a() { if (atkey.metadata.ispublic != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atkey.atkeytype); goto exit; } if (strncmp(atkey.name.str, "publickey", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", - atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.name.str); goto exit; } if (strncmp(atkey.sharedby.str, "@bob", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby.str); goto exit; } ret = 0; @@ -108,42 +113,39 @@ static int test1b() { if (atkey.metadata.ispublic != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } if (strncmp(atkey.name.str, "publickey", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", - atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.name.str); ret = 1; goto exit; } if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (atkey.namespacestr.len != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + atkey.namespacestr.len); ret = 1; goto exit; } if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); ret = 1; goto exit; } @@ -180,14 +182,14 @@ static int test1c() { if (atkey.metadata.ispublic != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -200,21 +202,20 @@ static int test1c() { if (strncmp(atkey.sharedby.str, "@jeremy", atkey.sharedby.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + atkey.sharedby.str); ret = 1; goto exit; } if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); ret = 1; goto exit; } if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr.str); ret = 1; goto exit; } @@ -250,14 +251,14 @@ static int test1d() { if (atkey.metadata.ispublic != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -270,23 +271,22 @@ static int test1d() { if (strncmp(atkey.sharedby.str, "@jeremy", atkey.sharedby.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + atkey.sharedby.str); ret = 1; goto exit; } if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr.str); ret = 1; goto exit; } @@ -323,42 +323,40 @@ static int test2a() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } if (strncmp(atkey.name.str, "name.wavi", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name.wavi, it is \"%s\"\n", - atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name.wavi, it is \"%s\"\n", atkey.name.str); ret = 1; goto exit; } if (strncmp(atkey.sharedby.str, "@bob", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (strncmp(atkey.sharedwith.str, "@alice", atkey.sharedwith.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @alice, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr.str); ret = 1; goto exit; } @@ -394,14 +392,14 @@ static int test2b() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -413,22 +411,21 @@ static int test2b() { } if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (atkey.namespacestr.len != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + atkey.namespacestr.len); ret = 1; goto exit; } @@ -461,14 +458,14 @@ static int test2c() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -480,22 +477,21 @@ static int test2c() { } if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (atkey.namespacestr.len != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + atkey.namespacestr.len); ret = 1; goto exit; } @@ -531,14 +527,14 @@ static int test2d() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -550,22 +546,21 @@ static int test2d() { } if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr.str); ret = 1; goto exit; } @@ -578,6 +573,104 @@ exit: { } } +static int test2e() { + int ret = 1; + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + + if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2E, strlen(TEST_ATKEY_FROM_STRING_2E))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); + goto exit; + } + + // @alice:name.vpsx.sshnp.abcd.efgh@xavierbob123 + if(strcmp(atkey.sharedby.str, "@xavierbob123") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby.str); + goto exit; + } + + if(strcmp(atkey.sharedwith.str, "@alice") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith.str); + goto exit; + } + + if(strcmp(atkey.name.str, "name") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.name.str); + goto exit; + } + + if(strcmp(atkey.namespacestr.str, "vpsx.sshnp.abcd.efgh") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", atkey.namespacestr.str); + goto exit; + } + + ret = 0; + goto exit; +exit: { + atclient_atkey_free(&atkey); + return ret; +} +} + +static int test2f() { + int ret = 1; + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + + if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2F, strlen(TEST_ATKEY_FROM_STRING_2F))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); + goto exit; + } + + // cached:@jeremy:name.vps1.sshnp@xavier + + if(!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized when it should be\n"); + goto exit; + } + + if(atkey.metadata.iscached != true) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is false when it should be true\n"); + goto exit; + } + + if(strcmp(atkey.sharedby.str, "@xavier") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby.str); + goto exit; + } + + if(strcmp(atkey.sharedwith.str, "@jeremy") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith.str); + goto exit; + } + + if(strcmp(atkey.name.str, "name") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.name.str); + goto exit; + } + + if(strcmp(atkey.namespacestr.str, "vps1.sshnp") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", atkey.namespacestr.str); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + static int test3a() { int ret = 1; @@ -601,42 +694,41 @@ static int test3a() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey.atkeytype); + "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey.atkeytype); ret = 1; goto exit; } if (strncmp(atkey.name.str, "_lastnotificationid", atkey.name.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not _lastnotificationid, it is \"%s\"\n", - atkey.name.str); + atkey.name.str); ret = 1; goto exit; } if (strncmp(atkey.sharedby.str, "@alice123_4😘", atkey.sharedby.len) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice123_4😘, it is \"%s\"\n", - atkey.sharedby.str); + atkey.sharedby.str); ret = 1; goto exit; } if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); ret = 1; goto exit; } if (atkey.namespacestr.len != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + atkey.namespacestr.len); ret = 1; goto exit; } @@ -673,14 +765,14 @@ static int test4a() { if (atkey.metadata.ispublic != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atkey.metadata.ispublic); ret = 1; goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } @@ -692,26 +784,24 @@ static int test4a() { } if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); ret = 1; goto exit; } if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } if (atkey.namespacestr.len != 0 && strlen(atkey.namespacestr.str) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + atkey.namespacestr.len); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.str is not empty, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr.str); ret = 1; goto exit; } @@ -740,51 +830,50 @@ static int test4b() { } if (atkey.metadata.iscached != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); goto exit; } if (atkey.metadata.ispublic != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", + atkey.metadata.ispublic); goto exit; } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", atkey.atkeytype); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey.atkeytype); goto exit; } if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); goto exit; } if (strncmp(atkey.sharedby.str, "@jeremy_0", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy_0, it is \"%s\"\n", - atkey.sharedby.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy_0, it is \"%s\"\n", + atkey.sharedby.str); goto exit; } if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - atkey.sharedwith.len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", + atkey.sharedwith.str); goto exit; } if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", + atkey.namespacestr.str); goto exit; } @@ -802,84 +891,72 @@ int main() { atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); // test 1a. cached public key (cached:public:publickey@bob) - ret = test1a(); - if (ret != 0) { + if ((ret = test1a()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a failed\n"); - ret = 1; goto exit; } // test 1b. non-cached public key (public:publickey@alice) - ret = test1b(); - if (ret != 0) { + if ((ret = test1b()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b failed\n"); - ret = 1; goto exit; } // test 1c - ret = test1c(); - if (ret != 0) { + if ((ret = test1c()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c failed\n"); - ret = 1; goto exit; } // test 1d - ret = test1d(); - if (ret != 0) { + if ((ret = test1d()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d failed\n"); - ret = 1; goto exit; } // test 2a. non-cached sharedkey with namespace (@alice:name.wavi@bob) - ret = test2a(); - if (ret != 0) { + if ((ret = test2a()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a failed\n"); - ret = 1; goto exit; } - ret = test2b(); - if (ret != 0) { + if ((ret = test2b()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b failed\n"); - ret = 1; goto exit; } - ret = test2c(); - if (ret != 0) { + if ((ret = test2c()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c failed\n"); - ret = 1; goto exit; } - ret = test2d(); - if (ret != 0) { + if ((ret = test2d()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d failed\n"); - ret = 1; goto exit; } - ret = test3a(); - if (ret != 0) { + if ((ret = test2e()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2e failed\n"); + goto exit; + } + + if ((ret = test2f()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2f failed\n"); + goto exit; + } + + if ((ret = test3a()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a failed\n"); - ret = 1; goto exit; } - ret = test4a(); - if (ret != 0) { + if ((ret = test4a()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a failed\n"); - ret = 1; goto exit; } - ret = test4b(); - if (ret != 0) { + if ((ret = test4b()) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b failed\n"); - ret = 1; goto exit; } From 02ef073df0a24ee0ae66cecc6d00972bdc714299 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 8 Jul 2024 16:14:14 -0400 Subject: [PATCH 003/193] feat: /scan in repl --- examples/desktop/repl/src/main.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 3098b5ac..a2b24c33 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -124,6 +124,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "No command entered\n"); continue; } + command[strcspn(command, "\n")] = 0; if (strcmp(command, "/exit") == 0) { loop = false; @@ -178,10 +179,38 @@ int main(int argc, char *argv[]) { } get_end: { atclient_atkey_free(&atkey); } + } else if (strcmp(command, "/scan") == 0) { + char *regex = NULL; + char *saveptr = NULL; + regex = strtok_r(NULL, " ", &saveptr); + atclient_atkey *arr = NULL; + size_t arrlen = 0; + if ((ret = atclient_get_atkeys(&atclient, regex, true, 8192, &arr, &arrlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); + goto scan_end; + } + const size_t atkeybufsize = 1024; + char atkeybuf[1024]; + size_t atkey_buf_len; + for (size_t i = 0; i < arrlen; i++) { + memset(atkeybuf, 0, sizeof(char) * atkeybufsize); + atclient_atkey_to_string(&arr[i], atkeybuf, atkeybufsize, &atkey_buf_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atKey[%i]: \'%s\'\n", i, atkeybuf); + } + scan_end: { + for (size_t i = 0; i < arrlen; i++) { + atclient_atkey_free(&arr[i]); + } + free(arr); + } + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unknown command: %s\n", command); } } + memset(buffer, 0, sizeof(char) * buffersize); memset(recv, 0, sizeof(unsigned char) * recvsize); + } while (loop); ret = 0; From 18625590fdf4a24705ccad56e3d04b1bf1bdf5ee Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 8 Jul 2024 16:14:22 -0400 Subject: [PATCH 004/193] chore: atclient_get_atkeys debugging logging --- packages/atclient/src/atclient_get_atkeys.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 23bb8f41..08f380c7 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -62,6 +62,9 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi goto exit; } + // log recevied bytes + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv was %lu bytes long\n", recvlen); + // 2. parse response if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { ret = 1; From f89933898f7e089b27f751a91e7f3897cb057844 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 9 Jul 2024 10:31:46 -0400 Subject: [PATCH 005/193] feat: repl /deleteall command --- examples/desktop/repl/src/main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index a2b24c33..92bf5d4b 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -203,6 +203,30 @@ int main(int argc, char *argv[]) { } free(arr); } + } else if(strcmp(command, "/deleteall") == 0) { + atclient_atkey *arr = NULL; + size_t arrlen = 0; + if((ret = atclient_get_atkeys(&atclient, NULL, true, 8192, &arr, &arrlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); + goto deleteall_end; + } + char buf[4096]; + size_t bufolen; + for(size_t i = 0; i < arrlen; i++) { + if((ret = atclient_delete(&atclient, &arr[i])) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d | failed to delete atKey\n", ret); + continue; + } + memset(buf, 0, sizeof(char) * 4096); + atclient_atkey_to_string(&arr[i], buf, 4096, &bufolen); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Deleted atKey: %s\n", buf); + } + deleteall_end: { + for (size_t i = 0; i < arrlen; i++) { + atclient_atkey_free(&arr[i]); + } + free(arr); + } } else { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unknown command: %s\n", command); } From 75ee67c5df83e25cba999c90f81399b65d07dd2e Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 09:42:14 -0400 Subject: [PATCH 006/193] refactor: atclient_atkey_metadata_to_protocol_str --- packages/atclient/include/atclient/metadata.h | 10 +- packages/atclient/src/atclient_put.c | 65 +++++++---- packages/atclient/src/metadata.c | 33 +++--- packages/atclient/src/notify.c | 108 +++++++++--------- 4 files changed, 120 insertions(+), 96 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 839b5df7..82120c64 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -307,13 +307,13 @@ size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *me * update:meta and notify) * * @param metadata the metadata struct to read from - * @param metadatastr the buffer to write the metadata to - * @param metadatastrsize the allocated length of the metadatastr buffer - * @param metadatastrlen the length of the metadata string written to metadatastr once the operation is complete + * @param metadatastr a double pointer to the string that will be allocated and written to. This function will allocate + * the memory dynamically for you. All you have to do is pass the address to a pointer where the function will give you + * an address for you. If this function returns a zero (which means success), then it is the caller's responsibility to + * free the pointer. * @return int 0 on success */ -int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char *metadatastr, - const size_t metadatastrsize, size_t *metadatastrlen); +int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadatastr); bool atclient_atkey_metadata_is_createdby_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_updatedby_initialized(const atclient_atkey_metadata *metadata); diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 02e4462b..76770fa8 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -16,19 +16,45 @@ #define TAG "atclient_put" int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { - if (atclient->async_read) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); - return 1; - } int ret = 1; + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + return ret; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + return ret; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + return ret; + } + + if (valuelen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is 0\n"); + return ret; + } + // make sure shared by is atclient->atsign.atsign if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); return ret; } + if (atclient->async_read) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); + return ret; + } + // 1. initialize variables const size_t atkeystrsize = ATCLIENT_ATKEY_FULL_LEN; char atkeystr[atkeystrsize]; @@ -36,7 +62,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c size_t atkeystrlen = 0; const size_t recvsize = 4096; - unsigned char *recv; + unsigned char *recv = NULL; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); memset(recv, 0, sizeof(unsigned char) * recvsize); @@ -47,16 +73,11 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; memset(iv, 0, sizeof(unsigned char) * ivsize); - const size_t ivbase64size = 64; + const size_t ivbase64size = atchops_base64_encoded_size(ivsize); char ivbase64[ivbase64size]; memset(ivbase64, 0, sizeof(char) * ivbase64size); size_t ivbase64len = 0; - const size_t metadataprotocolstrsize = 2048; - char metadataprotocolstr[metadataprotocolstrsize]; - memset(metadataprotocolstr, 0, sizeof(char) * metadataprotocolstrsize); - size_t metadataprotocolstrlen = 0; - const size_t ciphertextsize = 4096; unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); @@ -72,6 +93,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(sharedenckeybase64, 0, sizeof(char) * sharedenckeybase64size); char *cmdbuffer = NULL; + char *metadata_protocol_str = NULL; // 2. build update: command ret = atclient_atkey_to_string(atkey, atkeystr, atkeystrsize, &atkeystrlen); @@ -80,13 +102,6 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } - ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), metadataprotocolstr, metadataprotocolstrsize, - &metadataprotocolstrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); - goto exit; - } - if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // no encryption memcpy(ciphertextbase64, value, valuelen); @@ -188,20 +203,21 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c size_t cmdbufferlen = strlen(" update:\r\n") + atkeystrlen + ciphertextbase64len + 1; // + 1 for null terminator - ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), metadataprotocolstr, metadataprotocolstrsize, - &metadataprotocolstrlen); + ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadata_protocol_str)); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); goto exit; } - if (metadataprotocolstrlen > 0) { - cmdbufferlen += metadataprotocolstrlen; + const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); + + if (metadata_protocol_str_len > 0) { + cmdbufferlen += metadata_protocol_str_len; } cmdbuffer = malloc(sizeof(char) * cmdbufferlen); memset(cmdbuffer, 0, sizeof(char) * cmdbufferlen); - snprintf(cmdbuffer, cmdbufferlen, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadataprotocolstr, + snprintf(cmdbuffer, cmdbufferlen, "update%.*s:%.*s %.*s\r\n", (int) metadata_protocol_str_len, metadata_protocol_str, (int)atkeystrlen, atkeystr, (int)ciphertextbase64len, ciphertextbase64); ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbufferlen - 1, recv, @@ -232,6 +248,7 @@ exit: { free(recv); } free(cmdbuffer); + free(metadata_protocol_str); return ret; } } diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index f9c63725..48a532f5 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -726,16 +726,26 @@ size_t atclient_atkey_metadata_skeencalgo_strlen(const atclient_atkey_metadata * + metadata->skeencalgo.len; } -int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char *metadatastr, - const size_t metadatastrsize, size_t *metadatastrlen) { +int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadatastr) { int ret = 1; - size_t pos = 0; - size_t len = atclient_atkey_metadata_protocol_strlen(metadata); - if (len > metadatastrsize) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr buffer too small: %lu > %lu\n", len, metadatastrsize); - return 1; + + if (metadata == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + goto exit; } + if (metadatastr == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr is NULL\n"); + goto exit; + } + + const size_t metadatastrsize = atclient_atkey_metadata_protocol_strlen(metadata) + 1; + const size_t expected_metadatastr_len = metadatastrsize - 1; + size_t pos = 0; + + *metadatastr = malloc(sizeof(char) * metadatastrsize); + memset(*metadatastr, 0, sizeof(char) * metadatastrsize); + if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { sprintf(metadatastr + pos, ":ttl:%ld", metadata->ttl); pos += 5 + long_strlen(metadata->ttl); @@ -836,18 +846,15 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad pos += 12 + metadata->skeencalgo.len; } - if (strlen(metadatastr) != len) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(metadatastr), - len); + if (strlen(metadatastr) != (expected_metadatastr_len - 1)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(metadatastr), + (expected_metadatastr_len - 1)); goto exit; } - *metadatastrlen = len; - ret = 0; goto exit; - exit: { return ret; } } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index a4dc24f0..ddb39332 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -52,13 +52,13 @@ void atclient_notify_params_free(atclient_notify_params *params) { memset(params int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notification_id) { - int res = 1; + int ret = 1; if (ctx->async_read) { atlogger_log( TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify cannot be called from an async_read=true atclient, it will cause a race condition.\n"); - return res; + return ret; } // holds the value to be added to the cmd, could be plaintext or ciphertext @@ -93,75 +93,75 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific size_t sharedenckeylen; if (params->sharedenckeybase64 != NULL) { - res = atchops_base64_decode((unsigned char *)params->sharedenckeybase64, strlen(params->sharedenckeybase64), + ret = atchops_base64_decode((unsigned char *)params->sharedenckeybase64, strlen(params->sharedenckeybase64), sharedenckey, sharedenckeysize, &sharedenckeylen); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", res); - return res; + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", ret); + return ret; } if (sharedenckeylen != sharedenckeysize) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed. Expected %lu but got %lu\n", sharedenckeysize, sharedenckeylen); - res = 1; - return res; + ret = 1; + return ret; } } else { atclient_atsign recipient; const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; unsigned char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); - if ((res = atclient_atsign_init(&recipient, params->atkey->sharedwith.str)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with code %d\n", res); - return res; + if ((ret = atclient_atsign_init(&recipient, params->atkey->sharedwith.str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with code %d\n", ret); + return ret; } - if ((res = atclient_get_shared_encryption_key_shared_by_me(ctx, &recipient, (char *)sharedenckeybase64, true)) != + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, &recipient, (char *)sharedenckeybase64, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", res); + "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", ret); atclient_atsign_free(&recipient); - return res; + return ret; } - if ((res = atchops_base64_decode(sharedenckeybase64, strlen((char *)sharedenckeybase64), sharedenckey, + if ((ret = atchops_base64_decode(sharedenckeybase64, strlen((char *)sharedenckeybase64), sharedenckey, sharedenckeysize, &sharedenckeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", res); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", ret); atclient_atsign_free(&recipient); - return res; + return ret; } atclient_atsign_free(&recipient); } unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - res = atchops_iv_generate(iv); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate failed with code %d\n", res); - return res; + ret = atchops_iv_generate(iv); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate failed with code %d\n", ret); + return ret; } - res = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, ivbase64, ivbase64size, &ivbase64len); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode failed with code %d\n", res); - return res; + ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, ivbase64, ivbase64size, &ivbase64len); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode failed with code %d\n", ret); + return ret; } - res = atclient_atkey_metadata_set_ivnonce(&(params->atkey->metadata), (char *)ivbase64, ivbase64len); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce failed with code %d\n", res); - return res; + ret = atclient_atkey_metadata_set_ivnonce(&(params->atkey->metadata), (char *)ivbase64, ivbase64len); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce failed with code %d\n", ret); + return ret; } - res = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)params->value, + ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)params->value, strlen(params->value), ciphertext, ciphertextsize, &ciphertextlen); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt failed with code %d\n", res); - return res; + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt failed with code %d\n", ret); + return ret; } - res = + ret = atchops_base64_encode(ciphertext, ciphertextlen, ciphertextbase64, ciphertextbase64size, &ciphertextbase64len); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode failed with code %d\n", res); - return res; + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode failed with code %d\n", ret); + return ret; } cmdvalue = malloc(sizeof(char) * (ciphertextbase64len + 1)); @@ -177,9 +177,9 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific size_t cmdlen = 0; - res = generate_cmd(params, cmdvalue, cmdvaluelen, &cmd, &cmdlen); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "generate_cmd failed with code %d\n", res); + ret = generate_cmd(params, cmdvalue, cmdvaluelen, &cmd, &cmdlen); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "generate_cmd failed with code %d\n", ret); goto exit; } @@ -192,9 +192,9 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific } size_t recvlen = 0; - res = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)cmd, cmdlen, recv, recvsize, &recvlen); - if (res != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send failed with code %d\n", res); + ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)cmd, cmdlen, recv, recvsize, &recvlen); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send failed with code %d\n", ret); goto exit; } else if (ctx->async_read) { goto exit; @@ -212,10 +212,10 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific strncpy(notification_id, data, datalen); notification_id[datalen] = '\0'; } - res = 0; + ret = 0; } else { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unexpected response: %.*s\n", (int)recvlen, recv); - res = 1; + ret = 1; } exit: { @@ -224,7 +224,7 @@ exit: { } free(cmdvalue); free(cmd); - return res; + return ret; } } @@ -288,7 +288,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval int res = 1; char *cmd = NULL; - char *metadatastr = NULL; + char *metadata_protocol_str = NULL; size_t atkeylen = 0; size_t metadatastrlen = 0; @@ -348,21 +348,21 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval off += strlen(":ttln:") + long_strlen(params->notification_expiry); } - size_t metadatastrolen; - if ((res = atclient_atkey_metadata_to_protocol_str(¶ms->atkey->metadata, cmd + off, metadatastrlen, - &metadatastrolen)) != 0) { + if ((res = atclient_atkey_metadata_to_protocol_str(¶ms->atkey->metadata, &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str failed with code: %d\n", res); goto exit; } - if (metadatastrolen != metadatastrlen) { + snprintf(cmd + off, cmdsize - off, "%s", metadata_protocol_str); + + if (strlen(metadata_protocol_str) != metadatastrlen) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str mismatch. Expected %lu but got %lu\n", metadatastrlen, - metadatastrolen); + strlen(metadata_protocol_str)); res = 1; goto exit; } - off += metadatastrolen; + off += strlen(metadata_protocol_str); snprintf(cmd + off, metadatastrlen, ":"); off += strlen(":"); @@ -404,7 +404,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval goto exit; exit: { - free(metadatastr); + free(metadata_protocol_str); return res; } } From 451bcde151203501a743eaa0b5daefb549142e43 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 09:57:17 -0400 Subject: [PATCH 007/193] wip: aes_ctr expected strlen/size --- packages/atchops/include/atchops/aesctr.h | 16 ++++++++++++++++ packages/atchops/src/aesctr.c | 10 ++++++++++ packages/atclient/src/atclient_put.c | 6 +++--- packages/atclient/src/metadata.c | 4 ++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/atchops/include/atchops/aesctr.h b/packages/atchops/include/atchops/aesctr.h index 2ec28f7d..bbe3ef85 100644 --- a/packages/atchops/include/atchops/aesctr.h +++ b/packages/atchops/include/atchops/aesctr.h @@ -40,4 +40,20 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size const unsigned char *ciphertext, const size_t ciphertextlen, unsigned char *plaintext, const size_t plaintextsize, size_t *plaintextlen); +/** + * @brief Used to calculate the length of the ciphertext buffer given the plaintext length + * + * @param plaintextlen the length of the plaintext string + * @return a sufficient length of the ciphertext buffer + */ +size_t atchops_aes_ctr_ciphertext_size(const size_t plaintextlen); + +/** + * @brief Used to calculate the length of the plaintext buffer given the ciphertext length + * + * @param ciphertextlen the length of the ciphertext string + * @return a sufficient length of the plaintext buffer + */ +size_t atchops_aes_ctr_plaintext_size(const size_t ciphertextlen); + #endif diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c index 54e8706f..dd83a985 100644 --- a/packages/atchops/src/aesctr.c +++ b/packages/atchops/src/aesctr.c @@ -119,3 +119,13 @@ exit: { return ret; } } + +size_t atchops_aes_ctr_ciphertext_size(const size_t plaintextlen) +{ + return (plaintextlen + 15) & ~0xF; +} + +size_t atchops_aes_ctr_plaintext_size(const size_t ciphertextlen) +{ + return (ciphertextlen + 15) & ~0xF; +} diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 76770fa8..da73daca 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -78,17 +78,17 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(ivbase64, 0, sizeof(char) * ivbase64size); size_t ivbase64len = 0; - const size_t ciphertextsize = 4096; + const size_t ciphertextsize = atchops_aes_ctr_ciphertext_size(valuelen); unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); size_t ciphertextlen = 0; - const size_t ciphertextbase64size = 4096; + const size_t ciphertextbase64size = atchops_base64_encoded_size(ciphertextsize) + 1; char ciphertextbase64[ciphertextbase64size]; memset(ciphertextbase64, 0, sizeof(char) * ciphertextbase64size); size_t ciphertextbase64len = 0; - const size_t sharedenckeybase64size = 45; + const size_t sharedenckeybase64size = atchops_base64_encoded_size(ATCHOPS_AES_256 / 8) + 1; char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(char) * sharedenckeybase64size); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 48a532f5..548aec9f 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -846,10 +846,10 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad pos += 12 + metadata->skeencalgo.len; } - if (strlen(metadatastr) != (expected_metadatastr_len - 1)) { + if (strlen(metadatastr) != (expected_metadatastr_len)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(metadatastr), - (expected_metadatastr_len - 1)); + (expected_metadatastr_len)); goto exit; } From 4d5059db50bcb6a451138b209b8f99e32dde88e8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 12:46:42 -0400 Subject: [PATCH 008/193] fix: metadata --- packages/atclient/src/metadata.c | 42 ++++++++++++++++---------------- packages/atclient/src/notify.c | 10 ++++---- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 548aec9f..e07eb2fb 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -747,23 +747,23 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad memset(*metadatastr, 0, sizeof(char) * metadatastrsize); if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { - sprintf(metadatastr + pos, ":ttl:%ld", metadata->ttl); + sprintf(*metadatastr + pos, ":ttl:%ld", metadata->ttl); pos += 5 + long_strlen(metadata->ttl); } if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { - sprintf(metadatastr + pos, ":ttb:%ld", metadata->ttb); + sprintf(*metadatastr + pos, ":ttb:%ld", metadata->ttb); pos += 5 + long_strlen(metadata->ttb); } if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { - sprintf(metadatastr + pos, ":ttr:%ld", metadata->ttr); + sprintf(*metadatastr + pos, ":ttr:%ld", metadata->ttr); pos += 5 + long_strlen(metadata->ttr); } if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { if (metadata->ccd) { - sprintf(metadatastr + pos, ":ccd:true"); + sprintf(*metadatastr + pos, ":ccd:true"); pos += 9; } else { sprintf(metadatastr + pos, ":ccd:false"); @@ -773,82 +773,82 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad if (atclient_atkey_metadata_is_isbinary_initialized(metadata)) { if (metadata->isbinary) { - sprintf(metadatastr + pos, ":isBinary:true"); + sprintf(*metadatastr + pos, ":isBinary:true"); pos += 14; } else { - sprintf(metadatastr + pos, ":isBinary:false"); + sprintf(*metadatastr + pos, ":isBinary:false"); pos += 15; } } if (atclient_atkey_metadata_is_isencrypted_initialized(metadata)) { if (metadata->isencrypted) { - sprintf(metadatastr + pos, ":isEncrypted:true"); + sprintf(*metadatastr + pos, ":isEncrypted:true"); pos += 17; } else { - sprintf(metadatastr + pos, ":isEncrypted:false"); + sprintf(*metadatastr + pos, ":isEncrypted:false"); pos += 18; } } if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - sprintf(metadatastr + pos, ":dataSignature:%s", metadata->datasignature.str); + sprintf(*metadatastr + pos, ":dataSignature:%s", metadata->datasignature.str); pos += 15 + metadata->datasignature.len; } if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - sprintf(metadatastr + pos, ":sharedKeyStatus:%s", metadata->sharedkeystatus.str); + sprintf(*metadatastr + pos, ":sharedKeyStatus:%s", metadata->sharedkeystatus.str); pos += 17 + metadata->sharedkeystatus.len; } if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - sprintf(metadatastr + pos, ":sharedKeyEnc:%s", metadata->sharedkeyenc.str); + sprintf(*metadatastr + pos, ":sharedKeyEnc:%s", metadata->sharedkeyenc.str); pos += 14 + metadata->sharedkeyenc.len; } if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - sprintf(metadatastr + pos, ":hash:%s", metadata->pubkeyhash.str); + sprintf(*metadatastr + pos, ":hash:%s", metadata->pubkeyhash.str); pos += 6 + metadata->pubkeyhash.len; } if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - sprintf(metadatastr + pos, ":algo:%s", metadata->pubkeyalgo.str); + sprintf(*metadatastr + pos, ":algo:%s", metadata->pubkeyalgo.str); pos += 6 + metadata->pubkeyalgo.len; } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - sprintf(metadatastr + pos, ":encoding:%s", metadata->encoding.str); + sprintf(*metadatastr + pos, ":encoding:%s", metadata->encoding.str); pos += 10 + metadata->encoding.len; } if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - sprintf(metadatastr + pos, ":encKeyName:%s", metadata->enckeyname.str); + sprintf(*metadatastr + pos, ":encKeyName:%s", metadata->enckeyname.str); pos += 12 + metadata->enckeyname.len; } if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - sprintf(metadatastr + pos, ":encAlgo:%s", metadata->encalgo.str); + sprintf(*metadatastr + pos, ":encAlgo:%s", metadata->encalgo.str); pos += 9 + metadata->encalgo.len; } if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - sprintf(metadatastr + pos, ":ivNonce:%s", metadata->ivnonce.str); + sprintf(*metadatastr + pos, ":ivNonce:%s", metadata->ivnonce.str); pos += 9 + metadata->ivnonce.len; } if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - sprintf(metadatastr + pos, ":skeEncKeyName:%s", metadata->skeenckeyname.str); + sprintf(*metadatastr + pos, ":skeEncKeyName:%s", metadata->skeenckeyname.str); pos += 15 + metadata->skeenckeyname.len; } if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - sprintf(metadatastr + pos, ":skeEncAlgo:%s", metadata->skeencalgo.str); + sprintf(*metadatastr + pos, ":skeEncAlgo:%s", metadata->skeencalgo.str); pos += 12 + metadata->skeencalgo.len; } - if (strlen(metadatastr) != (expected_metadatastr_len)) { + if (strlen(*metadatastr) != (expected_metadatastr_len)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(metadatastr), + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(*metadatastr), (expected_metadatastr_len)); goto exit; } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index ddb39332..8da44836 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -118,7 +118,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", ret); - atclient_atsign_free(&recipient); + atclient_atsign_free(&recipient); return ret; } if ((ret = atchops_base64_decode(sharedenckeybase64, strlen((char *)sharedenckeybase64), sharedenckey, @@ -297,16 +297,16 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval cmdsize = calculate_cmd_size(params, cmdvaluelen, &atkeylen, &metadatastrlen); if (cmdsize <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "calculate_cmd_size failed with code %d\n", cmdsize); res = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "calculate_cmd_size failed with code %d\n", cmdsize); goto exit; } // Step 3 allocate the buffer cmd = malloc(sizeof(char) * cmdsize); if (cmd == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); res = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } memset(cmd, 0, sizeof(char) * cmdsize); @@ -348,7 +348,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval off += strlen(":ttln:") + long_strlen(params->notification_expiry); } - if ((res = atclient_atkey_metadata_to_protocol_str(¶ms->atkey->metadata, &metadata_protocol_str)) != 0) { + if ((res = atclient_atkey_metadata_to_protocol_str(&(params->atkey->metadata), &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str failed with code: %d\n", res); goto exit; @@ -356,10 +356,10 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval snprintf(cmd + off, cmdsize - off, "%s", metadata_protocol_str); if (strlen(metadata_protocol_str) != metadatastrlen) { + res = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str mismatch. Expected %lu but got %lu\n", metadatastrlen, strlen(metadata_protocol_str)); - res = 1; goto exit; } off += strlen(metadata_protocol_str); From 438015ab0201b607d247862511694e1b16d628b4 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 12:46:59 -0400 Subject: [PATCH 009/193] chore: optimize atclient_put --- packages/atchops/src/aesctr.c | 4 ++-- packages/atclient/src/atclient_put.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c index dd83a985..f9a98e87 100644 --- a/packages/atchops/src/aesctr.c +++ b/packages/atchops/src/aesctr.c @@ -122,10 +122,10 @@ exit: { size_t atchops_aes_ctr_ciphertext_size(const size_t plaintextlen) { - return (plaintextlen + 15) & ~0xF; + return ((plaintextlen + 15) & ~0xF) + 16; } size_t atchops_aes_ctr_plaintext_size(const size_t ciphertextlen) { - return (ciphertextlen + 15) & ~0xF; + return ((ciphertextlen + 15) & ~0xF) + 16; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index da73daca..95013b86 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -73,12 +73,12 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; memset(iv, 0, sizeof(unsigned char) * ivsize); - const size_t ivbase64size = atchops_base64_encoded_size(ivsize); + const size_t ivbase64size = atchops_base64_encoded_size(ivsize) + 1; char ivbase64[ivbase64size]; memset(ivbase64, 0, sizeof(char) * ivbase64size); size_t ivbase64len = 0; - const size_t ciphertextsize = atchops_aes_ctr_ciphertext_size(valuelen); + const size_t ciphertextsize = atchops_aes_ctr_ciphertext_size(valuelen) + 1; unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); size_t ciphertextlen = 0; From 5afa32f1db13ac959598e6281589d5a83bb7cc42 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 12:53:27 -0400 Subject: [PATCH 010/193] fix: test_atkey_metadata --- packages/atclient/tests/test_atkey_metadata.c | 86 ++++++++----------- 1 file changed, 35 insertions(+), 51 deletions(-) diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index 9cd12e3a..eb0d8302 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -1,7 +1,7 @@ #include "atclient/metadata.h" #include "atlogger/atlogger.h" -#include #include +#include // example: // "metaData":{ @@ -49,63 +49,59 @@ static int test_atkey_metadata_from_jsonstr() { if (strncmp(metadata.createdby.atsign, "@qt_thermostat", strlen("@qt_thermostat")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdby.str != @qt_thermostat: %s", - metadata.createdby.atsign); + metadata.createdby.atsign); ret = 1; goto exit; } if (strncmp(metadata.updatedby.atsign, "@qt_thermostat", strlen("@qt_thermostat")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedby.atsign != @qt_thermostat: %s", - metadata.updatedby.atsign); + metadata.updatedby.atsign); ret = 1; goto exit; } if (metadata.createdat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat.len <= 0: %lu", - metadata.createdat.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat.len <= 0: %lu", metadata.createdat.len); ret = 1; goto exit; } if (strncmp(metadata.createdat.str, "2024-02-17 19:54:12.037Z", strlen("2024-02-17 19:54:12.037Z")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat.str != 2024-02-17 19:54:12.037Z: %s", - metadata.createdat.str); + metadata.createdat.str); ret = 1; goto exit; } if (metadata.updatedat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat.len <= 0: %lu", - metadata.updatedat.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat.len <= 0: %lu", metadata.updatedat.len); ret = 1; goto exit; } if (strncmp(metadata.updatedat.str, "2024-02-17 19:54:12.037Z", strlen("2024-02-17 19:54:12.037Z")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat.str != 2024-02-17 19:54:12.037Z: %s", - metadata.updatedat.str); + metadata.updatedat.str); ret = 1; goto exit; } if (metadata.expiresat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat.len <= 0: %lu", - metadata.expiresat.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat.len <= 0: %lu", metadata.expiresat.len); ret = 1; goto exit; } if (strncmp(metadata.expiresat.str, "2024-02-17 19:55:38.437Z", strlen("2024-02-17 19:55:38.437Z")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat.str != 2024-02-17 19:55:38.437Z: %s", - metadata.expiresat.str); + metadata.expiresat.str); ret = 1; goto exit; } if (metadata.status.len != strlen("active")) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status.len != strlen(active): %lu", - metadata.status.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status.len != strlen(active): %lu", metadata.status.len); ret = 1; goto exit; } @@ -147,64 +143,56 @@ static int test_atkey_metadata_from_jsonstr() { } if (metadata.availableat.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.availableat.len != 0: %lu", - metadata.availableat.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.availableat.len != 0: %lu", metadata.availableat.len); ret = 1; goto exit; } if (metadata.refreshat.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.refreshat.len != 0: %lu", - metadata.refreshat.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.refreshat.len != 0: %lu", metadata.refreshat.len); ret = 1; goto exit; } if (metadata.datasignature.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.datasignature.len != 0: %lu", - metadata.datasignature.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.datasignature.len != 0: %lu", metadata.datasignature.len); ret = 1; goto exit; } if (metadata.sharedkeystatus.len != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.sharedkeystatus.len != 0: %lu", - metadata.sharedkeystatus.len); + metadata.sharedkeystatus.len); ret = 1; goto exit; } if (metadata.sharedkeyenc.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.sharedkeyenc.len != 0: %lu", - metadata.sharedkeyenc.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.sharedkeyenc.len != 0: %lu", metadata.sharedkeyenc.len); ret = 1; goto exit; } if (metadata.pubkeyhash.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyhash.len != 0: %lu", - metadata.pubkeyhash.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyhash.len != 0: %lu", metadata.pubkeyhash.len); ret = 1; goto exit; } if (metadata.pubkeyalgo.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyalgo.len != 0: %lu", - metadata.pubkeyalgo.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyalgo.len != 0: %lu", metadata.pubkeyalgo.len); ret = 1; goto exit; } if (metadata.encoding.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.encoding.len != 0: %lu", - metadata.encoding.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.encoding.len != 0: %lu", metadata.encoding.len); ret = 1; goto exit; } if (metadata.enckeyname.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.enckeyname.len != 0: %lu", - metadata.enckeyname.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.enckeyname.len != 0: %lu", metadata.enckeyname.len); ret = 1; goto exit; } @@ -222,15 +210,13 @@ static int test_atkey_metadata_from_jsonstr() { } if (metadata.skeenckeyname.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeenckeyname.len != 0: %lu", - metadata.skeenckeyname.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeenckeyname.len != 0: %lu", metadata.skeenckeyname.len); ret = 1; goto exit; } if (metadata.skeencalgo.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeencalgo.len != 0: %lu", - metadata.skeencalgo.len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeencalgo.len != 0: %lu", metadata.skeencalgo.len); ret = 1; goto exit; } @@ -258,37 +244,35 @@ static int test_atkey_metadata_to_protocolstr() { atclient_atkey_metadata_set_iscached(&metadata, true); atclient_atkey_metadata_set_ivnonce(&metadata, "abcdefghijk", strlen("abcdefghijk")); - const size_t protocolfragmentsize = 1024; - char protocolfragment[protocolfragmentsize]; - memset(protocolfragment, 0, sizeof(char) * protocolfragmentsize); - size_t protocolfragmentlen = 0; + char *protocolfragment = NULL; + const size_t expected_protocolframent_len = atclient_atkey_metadata_protocol_strlen(&metadata); - ret = - atclient_atkey_metadata_to_protocol_str(&metadata, protocolfragment, protocolfragmentsize, &protocolfragmentlen); + ret = atclient_atkey_metadata_to_protocol_str(&metadata, &protocolfragment); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr failed"); goto exit; } - if (strlen(protocolfragment) != protocolfragmentlen) { + const size_t actual_protocolfragment_len = strlen(protocolfragment); + + if (actual_protocolfragment_len != expected_protocolframent_len) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "strlen(protocolfragment) != protocolfragmentlen: %lu != %lu", strlen(protocolfragment), - protocolfragmentlen); + "actual_protocolfragment_len != expected_protocolframent_len: %lu != %lu", actual_protocolfragment_len, + expected_protocolframent_len); ret = 1; goto exit; } - if (protocolfragmentlen != expectedlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "protocolfragmentlen != expectedlen: %lu != %lu", - protocolfragmentlen, expectedlen); + if (actual_protocolfragment_len != expectedlen) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "actual_protocolfragment_len != expectedlen: %lu != %lu", + actual_protocolfragment_len, expectedlen); ret = 1; goto exit; } - if (strncmp(protocolfragment, expected, expectedlen) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "strncmp(protocolfragment, expected, expectedlen) != 0: %s != %s", protocolfragment, - expected); + if (strcmp(protocolfragment, expected) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strncmp(protocolfragment, expected) != 0: %s != %s", + protocolfragment, expected); ret = 1; goto exit; } From 15ae36f63bdab8b3fd6fd7c955caa1836a1c6d86 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 12:56:50 -0400 Subject: [PATCH 011/193] docs: atclient_put --- packages/atclient/src/atclient_put.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 95013b86..903f6a4f 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -61,7 +61,8 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(atkeystr, 0, sizeof(char) * atkeystrsize); size_t atkeystrlen = 0; - const size_t recvsize = 4096; + const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to + // receive commmit id from `update:` unsigned char *recv = NULL; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); @@ -217,7 +218,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c cmdbuffer = malloc(sizeof(char) * cmdbufferlen); memset(cmdbuffer, 0, sizeof(char) * cmdbufferlen); - snprintf(cmdbuffer, cmdbufferlen, "update%.*s:%.*s %.*s\r\n", (int) metadata_protocol_str_len, metadata_protocol_str, + snprintf(cmdbuffer, cmdbufferlen, "update%.*s:%.*s %.*s\r\n", (int)metadata_protocol_str_len, metadata_protocol_str, (int)atkeystrlen, atkeystr, (int)ciphertextbase64len, ciphertextbase64); ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbufferlen - 1, recv, From d0bb66b524b1286a2973a9db805deedd68cb5b34 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 13:07:53 -0400 Subject: [PATCH 012/193] feat: atclient_put args check function --- packages/atclient/src/atclient_put.c | 88 +++++++++++++++++----------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 903f6a4f..7a7076f2 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -15,47 +15,22 @@ #define TAG "atclient_put" +static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid); + int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { int ret = 1; - if (atclient == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - return ret; - } - - if (atkey == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + /* + * 1. Check if valid arguments were passed + */ + if((ret = atclient_put_valid_args_check(atclient, atkey, value, valuelen, commitid)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_valid_args_check: %d\n", ret); return ret; } - if (value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - return ret; - } - - if (valuelen == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is 0\n"); - return ret; - } - - // make sure shared by is atclient->atsign.atsign - if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); - return ret; - } - - if (atclient->async_read) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); - return ret; - } - - // 1. initialize variables + /* + * 2. Allocate variables + */ const size_t atkeystrsize = ATCLIENT_ATKEY_FULL_LEN; char atkeystr[atkeystrsize]; memset(atkeystr, 0, sizeof(char) * atkeystrsize); @@ -253,3 +228,46 @@ exit: { return ret; } } + +static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) +{ +{ + int ret = 1; + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + return ret; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + return ret; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + return ret; + } + + if (valuelen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is 0\n"); + return ret; + } + + // make sure shared by is atclient->atsign.atsign + if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); + return ret; + } + + if (atclient->async_read) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); + return ret; + } +} +} \ No newline at end of file From f974d8c51d8eee6d080544bef71933679f436f81 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 21:28:42 -0400 Subject: [PATCH 013/193] refactor: atclient_atkey_to_string --- packages/atclient/include/atclient/atkey.h | 8 +- packages/atclient/src/atclient_delete.c | 9 +- .../atclient/src/atclient_get_publickey.c | 11 +- packages/atclient/src/atclient_get_selfkey.c | 13 +- .../atclient/src/atclient_get_sharedkey.c | 17 +- packages/atclient/src/atclient_put.c | 9 +- packages/atclient/src/atkey.c | 192 ++++++++-------- packages/atclient/src/notify.c | 10 +- packages/atclient/tests/test_atkey_create.c | 83 ++++--- .../atclient/tests/test_atkey_to_string.c | 216 +++++++++++------- 10 files changed, 293 insertions(+), 275 deletions(-) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index ad87b804..2dfad3fa 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -66,13 +66,11 @@ size_t atclient_atkey_strlen(const atclient_atkey *atkey); * @brief convert an atkey struct to its string format * * @param atkey atkey struct to read, assumed that this was already initialized via atclient_atkey_init - * @param atkeystr buffer to write to, assumed that this was already allocated - * @param atkeystrsize buffer allocated size - * @param atkeystrlen the written (output) length of the atkeystr + * @param atkeystr a double pointer to the atkey string, this will be allocated by this function and is the + * responsibility of the caller of this function to free it * @return int 0 on success */ -int atclient_atkey_to_string(const atclient_atkey *atkey, char *atkeystr, const size_t atkeystrsize, - size_t *atkeystrlen); +int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr); /** * @brief Populate an atkey struct representing a PublicKey AtKey with null terminated strings. An example of a Public diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index f9bfd025..f0f5065b 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -15,9 +15,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { atclient_atstr cmdbuffer; atclient_atstr_init_literal(&cmdbuffer, ATCLIENT_ATKEY_FULL_LEN + strlen("delete:"), "delete:"); - char atkeystr[ATCLIENT_ATKEY_FULL_LEN]; - memset(atkeystr, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); - size_t atkeystrlen = 0; + char *atkeystr = NULL; const size_t recvsize = 4096; unsigned char *recv; @@ -27,13 +25,13 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { } size_t recvlen = 0; - ret = atclient_atkey_to_string(atkey, atkeystr, ATCLIENT_ATKEY_FULL_LEN, &atkeystrlen); + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - ret = atclient_atstr_append(&cmdbuffer, "%.*s\n", (int)atkeystrlen, atkeystr); + ret = atclient_atstr_append(&cmdbuffer, "%.*s\n", (int)strlen(atkeystr), atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append: %d\n", ret); goto exit; @@ -62,6 +60,7 @@ exit: { free(recv); } atclient_atstr_free(&cmdbuffer); + free(atkeystr); return ret; } } diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index bf159ca4..d8e2f733 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -24,9 +24,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu } // 1. initialize variables - const size_t atkeystrlen = ATCLIENT_ATKEY_FULL_LEN; - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, atkeystrlen); + char *atkeystr = NULL; const size_t recvsize = valuesize; unsigned char recv[recvsize]; @@ -38,7 +36,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu char *metadatastr = NULL; // 2. build plookup: command - ret = atclient_atkey_to_string(atkey, atkeystr.str, atkeystr.size, &atkeystr.len); + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; @@ -50,11 +48,11 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu } char *atkeystrwithoutpublic = NULL; - char *ptr = strstr(atkeystr.str, "public:"); + char *ptr = strstr(atkeystr, "public:"); if (ptr != NULL) { atkeystrwithoutpublic = ptr + strlen("public:"); } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not find \"public:\" from string \"%s\"\n", atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not find \"public:\" from string \"%s\"\n", atkeystr); goto exit; } @@ -130,6 +128,7 @@ exit: { } free(metadatastr); free(cmdbuffer); + free(atkeystr); return ret; } } diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 2a57e9e0..a4765a9f 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -29,10 +29,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, int ret = 1; // 1. initialize variables - const size_t atkeystrsize = ATCLIENT_ATKEY_FULL_LEN; - char atkeystr[atkeystrsize]; - memset(atkeystr, 0, sizeof(char) * atkeystrsize); - size_t atkeystrlen = 0; + char *atkeystr = NULL; const size_t recvsize = valuesize; unsigned char recv[recvsize]; @@ -51,18 +48,19 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, char *valueraw = NULL; // 2. build llookup: command - ret = atclient_atkey_to_string(atkey, atkeystr, atkeystrsize, &atkeystrlen); - + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + + const size_t atkeystrlen = strlen(atkeystr); const size_t cmdbuffersize = strlen("llookup:all:\r\n") + atkeystrlen + 1; cmdbuffer = (char *)malloc(sizeof(char) * cmdbuffersize); memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); - snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int)atkeystrlen, atkeystr); + snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int) atkeystrlen, atkeystr); // 3. send llookup: command ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, @@ -168,6 +166,7 @@ exit: { } free(valueraw); free(cmdbuffer); + free(atkeystr); return ret; } } diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 36bd9c21..65cdaee2 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -64,7 +64,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke int ret = 1; short enc_key_mem = 0; - char *atkey_str_buff = NULL; + char *atkeystr = NULL; char *command = NULL; char *response_prefix = NULL; unsigned char *recv = NULL; @@ -100,24 +100,20 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke goto exit; } - // atclient_atkey_to_string buffer - const size_t atkey_str_buf_len = 4096; - atkey_str_buff = calloc(atkey_str_buf_len, sizeof(char)); - size_t atkey_out_len = 0; - - ret = atclient_atkey_to_string(atkey, atkey_str_buff, atkey_str_buf_len, &atkey_out_len); + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); // build command // command_prefix = "llookup:all:" const short command_prefix_len = 12; - const size_t command_len = command_prefix_len + atkey_out_len + 3; + const size_t command_len = command_prefix_len + atkeystrlen + 3; command = malloc(command_len * sizeof(char)); - snprintf(command, command_len, "llookup:all:%s\r\n", atkey_str_buff); + snprintf(command, command_len, "llookup:all:%s\r\n", atkeystr); // send command and recv response const size_t recvsize = 4096; @@ -233,14 +229,13 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke exit: { if (enc_key_mem) free(enc_key); - if (atkey_str_buff) - free(atkey_str_buff); if (command) free(command); if (recv) free(recv); if (response_prefix) free(response_prefix); + free(atkeystr); return ret; } } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 7a7076f2..41751507 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -31,10 +31,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 2. Allocate variables */ - const size_t atkeystrsize = ATCLIENT_ATKEY_FULL_LEN; - char atkeystr[atkeystrsize]; - memset(atkeystr, 0, sizeof(char) * atkeystrsize); - size_t atkeystrlen = 0; + char *atkeystr = NULL; const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to // receive commmit id from `update:` @@ -72,11 +69,12 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c char *metadata_protocol_str = NULL; // 2. build update: command - ret = atclient_atkey_to_string(atkey, atkeystr, atkeystrsize, &atkeystrlen); + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // no encryption @@ -225,6 +223,7 @@ exit: { } free(cmdbuffer); free(metadata_protocol_str); + free(atkeystr); return ret; } } diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 5c78eae7..9f958da1 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -106,8 +106,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons // shift tokens array to the left token = strtok_r(NULL, ":", &saveptr); if (token == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", - atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } @@ -122,8 +121,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons } token = strtok_r(NULL, ":", &saveptr); if (token == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", - atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } @@ -138,8 +136,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons // set atkey->name token = strtok_r(token, "@", &saveptr); if (token == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", - atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } @@ -153,8 +150,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons char *saveptr2; char *name = strtok_r(nameandnamespacestr, ".", &saveptr2); if (name == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name is NULL. %s atkey is probably incomplete\n", - atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } @@ -166,8 +162,8 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons } char *namespacestr = strtok_r(NULL, "", &saveptr2); if (namespacestr == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "namespacestr is NULL. %s atkey is probably incomplete\n", atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. %s atkey is probably incomplete\n", + atkeystr); ret = 1; goto exit; } @@ -189,8 +185,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons // set atkey->sharedby token = strtok_r(NULL, "", &saveptr); if (token == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", - atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } @@ -217,116 +212,113 @@ exit: { } size_t atclient_atkey_strlen(const atclient_atkey *atkey) { - - // TODO: I created this function to optimize the notify memory usage - // obviously, I am creating an unnecessary buffer here just to get the length - // which means there is a lot of memory being wasted here - // later on we need to refactor this and atclient_atkey_to_string away from - // using atclient_atstr to see real memory savings - // however, the priority is a working notify, and this is the best way to - // solve it immediately - - const size_t atkeystrsize = ATCLIENT_ATKEY_FULL_LEN + 1; - char atkeystr[atkeystrsize]; - memset(atkeystr, 0, sizeof(char) * (atkeystrsize)); - size_t atkeystrlen = 0; - atclient_atkey_to_string(atkey, atkeystr, atkeystrsize, &atkeystrlen); - return atkeystrlen; + size_t len = 0; + if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { + len += strlen("cached:"); + } + if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic) { + len += strlen("public:"); + } + if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + len += atkey->sharedwith.len + strlen(":"); + } + len += atkey->name.len; + if (atkey->namespacestr.len > 0) { + len += strlen(".") + atkey->namespacestr.len; + } + len += atkey->sharedby.len; + return len; } -int atclient_atkey_to_string(const atclient_atkey *atkey, char *atkeystr, const size_t atkeystrsize, - size_t *atkeystrlen) { +int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { int ret = 1; - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + size_t index_pos = 0; - if (atkey->metadata.iscached) { - ret = atclient_atstr_append(&string, "cached:"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append_literal failed\n"); - goto exit; - } + const size_t atkeystrsize = atclient_atkey_strlen(atkey) + 1; + *atkeystr = (char *)malloc(sizeof(char) * atkeystrsize); + if (*atkeystr == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto error_exit; } + memset(*atkeystr, 0, sizeof(char) * atkeystrsize); - if (atkey->metadata.ispublic && atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - ret = atclient_atstr_append(&string, "public:"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append_literal failed\n"); - goto exit; - } + if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "cached:"); + index_pos += strlen("cached:"); + } + + if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic && + atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); + index_pos += strlen("public:"); } else if (atkey->metadata.ispublic || atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "either atkey's metadata ispublic is not set to 1 or atkey's atkeytype is not set to " - "ATCLIENT_ATKEY_TYPE_PUBLICKEY for atkey: %.*s\n", - (int)atkey->name.len, atkey->name.str); + "either atkey's metadata ispublic is not set to 1 or atkey's atkeytype is not set to " + "ATCLIENT_ATKEY_TYPE_PUBLICKEY for atkey: %.*s\n", + (int)atkey->name.len, atkey->name.str); + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); + index_pos += strlen("public:"); } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - ret = atclient_atstr_append(&string, "%.*s:", (int)atkey->sharedwith.len, atkey->sharedwith.str); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append_literal failed\n"); - goto exit; - } - } else if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY || atkey->atkeytype == ATCLIENT_ATKEY_TYPE_UNKNOWN) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's atkeytype is %d: %.*s\n", atkey->atkeytype, - (int)atkey->name.len, atkey->name.str); + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s:", (int)atkey->sharedwith.len, + atkey->sharedwith.str); + index_pos += (atkey->sharedwith.len) + strlen(":"); + } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_UNKNOWN) { ret = 1; - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's atkeytype is %d: %.*s\n", atkey->atkeytype, + (int)atkey->name.len, atkey->name.str); + goto error_exit; } if (atkey->name.str == NULL || atkey->name.len == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey's name is NULL or empty. atkey.name.str: \"%s\", atkey.name.len: %d\n", - atkey->name.str, atkey->name.len); ret = 1; - goto exit; - } - ret = atclient_atstr_append(&string, atkey->name.str, atkey->name.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append failed\n"); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey's name is NULL or empty. atkey.name.str: \"%s\", atkey.name.len: %d\n", atkey->name.str, + atkey->name.len); + goto error_exit; } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "4\n"); + + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->name.len, atkey->name.str); + index_pos += atkey->name.len; + if (atkey->namespacestr.len > 0) { - ret = atclient_atstr_append(&string, "."); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append_literal failed\n"); - goto exit; - } - ret = atclient_atstr_append(&string, atkey->namespacestr.str, atkey->namespacestr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append failed\n"); - goto exit; - } + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, ".%.*s", (int)atkey->namespacestr.len, + atkey->namespacestr.str); + index_pos += strlen(".") + atkey->namespacestr.len; } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "5\n"); + if (atkey->sharedby.str == NULL || atkey->sharedby.len == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey's sharedby is NULL or empty. atkey.sharedby.str: \"%s\", atkey.sharedby.len: %d\n", - atkey->sharedby.str, atkey->sharedby.len); ret = 1; - goto exit; - } - ret = atclient_atstr_append(&string, "%.*s", (int)atkey->sharedby.len, atkey->sharedby.str); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append failed\n"); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey's sharedby is NULL or empty. atkey.sharedby.str: \"%s\", atkey.sharedby.len: %d\n", + atkey->sharedby.str, atkey->sharedby.len); + goto error_exit; } - if (string.len > atkeystrsize) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystr is too small\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "6\n"); + + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->sharedby.len, atkey->sharedby.str); + index_pos += (int)atkey->sharedby.len; + + if (strlen(*atkeystr) > atkeystrsize) { ret = 1; - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkeystr is too small. Conditiion strlen(atkey)str > atkeystrsize: %d > %d, is true\n", + strlen(*atkeystr), atkeystrsize); + goto error_exit; } - memcpy(atkeystr, string.str, string.len); - *atkeystrlen = string.len; - ret = 0; goto exit; -exit: { - atclient_atstr_free(&string); - return ret; +error_exit: { + free(*atkeystr); + goto exit; } +exit: { return ret; } } int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, @@ -334,15 +326,13 @@ int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, con int ret = 1; if (name == NULL || sharedby == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "name or sharedby is NULL. These are required arguments.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name or sharedby is NULL. These are required arguments.\n"); ret = 1; goto exit; } if (namelen == 0 || sharedbylen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "namelen or sharedbylen is 0. These are required arguments.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namelen or sharedbylen is 0. These are required arguments.\n"); ret = 1; goto exit; } @@ -380,15 +370,13 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const int ret = 1; if (name == NULL || sharedby == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "name or sharedby is NULL. These are required arguments.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name or sharedby is NULL. These are required arguments.\n"); ret = 1; goto exit; } if (namelen == 0 || sharedbylen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "namelen or sharedbylen is 0. These are required arguments.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namelen or sharedbylen is 0. These are required arguments.\n"); ret = 1; goto exit; } @@ -432,14 +420,14 @@ int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *name, con if (name == NULL || sharedby == NULL || sharedwith == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "name, sharedby, or sharedwith is NULL. These are required arguments.\n"); + "name, sharedby, or sharedwith is NULL. These are required arguments.\n"); ret = 1; goto exit; } if (namelen == 0 || sharedbylen == 0 || sharedwithlen == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "namelen, sharedbylen, or sharedwithlen is 0. These are required arguments.\n"); + "namelen, sharedbylen, or sharedwithlen is 0. These are required arguments.\n"); ret = 1; goto exit; } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 8da44836..a2356142 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -290,6 +290,8 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval char *cmd = NULL; char *metadata_protocol_str = NULL; + char *atkeystr = NULL; + size_t atkeylen = 0; size_t metadatastrlen = 0; @@ -367,14 +369,14 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval snprintf(cmd + off, metadatastrlen, ":"); off += strlen(":"); - size_t atkeyolen; - if ((res = atclient_atkey_to_string(params->atkey, cmd + off, atkeylen, &atkeyolen)) != 0) { + if ((res = atclient_atkey_to_string(params->atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed with code: %d\n", res); return res; } - if (atkeylen != atkeyolen) { + const size_t atkeystrlen = strlen(atkeystr); + if (atkeylen != atkeystrlen) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string mismatch. Expected %lu but got %lu\n", - atkeylen, atkeyolen); + atkeylen, atkeystrlen); goto exit; } off += atkeylen; diff --git a/packages/atclient/tests/test_atkey_create.c b/packages/atclient/tests/test_atkey_create.c index df423486..8746e915 100644 --- a/packages/atclient/tests/test_atkey_create.c +++ b/packages/atclient/tests/test_atkey_create.c @@ -2,6 +2,7 @@ #include "atclient/constants.h" #include "atlogger/atlogger.h" #include +#include #include #define TAG "test_atkey_create" @@ -12,12 +13,10 @@ static int test_create_publickey() { atclient_atkey atkey; atclient_atkey_init(&atkey); - char atkeystr[ATCLIENT_ATKEY_FULL_LEN + 1]; - memset(atkeystr, 0, ATCLIENT_ATKEY_FULL_LEN + 1); + char *atkeystr = NULL; const char *expected = "public:test@alice"; const size_t expectedlen = strlen(expected); - size_t expectedolen = 0; ret = atclient_atkey_create_publickey(&atkey, "test", strlen("test"), "@alice", strlen("@alice"), NULL, 0); if (ret != 0) { @@ -26,32 +25,32 @@ static int test_create_publickey() { } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey type is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } if (strcmp(atkey.name.str, "test") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not test, it is \"%s\"\n", - atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not test, it is \"%s\"\n", atkey.name.str); ret = 1; goto exit; } if (strcmp(atkey.sharedby.str, "@alice") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + atkey.sharedby.str); ret = 1; goto exit; } - ret = atclient_atkey_to_string(&atkey, atkeystr, ATCLIENT_ATKEY_FULL_LEN, &expectedolen); + ret = atclient_atkey_to_string(&atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); ret = 1; goto exit; } + const size_t expectedolen = strlen(atkeystr); if (strcmp(atkeystr, expected) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystr is not %s, it is %s\n", expected, atkeystr); @@ -60,8 +59,7 @@ static int test_create_publickey() { } if (expectedolen != expectedlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expectedolen is not %lu, it is %lu\n", expectedlen, - expectedolen); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expectedolen is not %lu, it is %lu\n", expectedlen, expectedolen); ret = 1; goto exit; } @@ -70,6 +68,7 @@ static int test_create_publickey() { goto exit; exit: { atclient_atkey_free(&atkey); + free(atkeystr); return ret; } } @@ -80,8 +79,7 @@ static int test_create_selfkey() { atclient_atkey atkey; atclient_atkey_init(&atkey); - char atkeystr[ATCLIENT_ATKEY_FULL_LEN]; - memset(atkeystr, 0, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; const char *expected = "name@jeremy"; const size_t expectedlen = strlen(expected); @@ -93,33 +91,32 @@ static int test_create_selfkey() { } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey.atkeytype); ret = 1; goto exit; } if (strcmp(atkey.name.str, "name") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", - atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", atkey.name.str); ret = 1; goto exit; } if (strcmp(atkey.sharedby.str, "@jeremy") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + atkey.sharedby.str); ret = 1; goto exit; } - size_t atkeystrolen = 0; - ret = atclient_atkey_to_string(&atkey, atkeystr, ATCLIENT_ATKEY_FULL_LEN, &atkeystrolen); + ret = atclient_atkey_to_string(&atkey, &atkeystr); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrolen = strlen(atkeystr); if (strcmp(atkeystr, expected) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystr is not %s, it is %s\n", expected, atkeystr); @@ -128,15 +125,17 @@ static int test_create_selfkey() { } if (atkeystrolen != expectedlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, - atkeystrolen); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, atkeystrolen); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(atkeystr); + return ret; +} } static int test_create_sharedkey() { @@ -145,8 +144,7 @@ static int test_create_sharedkey() { atclient_atkey atkey; atclient_atkey_init(&atkey); - char atkeystr[ATCLIENT_ATKEY_FULL_LEN]; - memset(atkeystr, 0, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; const char *expected = "@jeremy:name.wavi@chess69lovely"; const size_t expectedlen = strlen(expected); @@ -159,64 +157,65 @@ static int test_create_sharedkey() { } if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", atkey.atkeytype); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey.atkeytype); goto exit; } if (strcmp(atkey.name.str, "name") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", - atkey.name.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", atkey.name.str); goto exit; } if (strcmp(atkey.sharedby.str, "@jeremy") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @jeremy, it is \"%s\"\n", + atkey.sharedby.str); goto exit; } if (strcmp(atkey.sharedwith.str, "@chess69lovely") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.sharedwith.str is not @chess69lovely, it is \"%s\"\n", atkey.sharedwith.str); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not @chess69lovely, it is \"%s\"\n", + atkey.sharedwith.str); goto exit; } if (strcmp(atkey.namespacestr.str, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwithname.str is not wavi, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith.str); ret = 1; goto exit; } - size_t atkeystrolen = 0; - ret = atclient_atkey_to_string(&atkey, atkeystr, ATCLIENT_ATKEY_FULL_LEN, &atkeystrolen); + ret = atclient_atkey_to_string(&atkey, &atkeystr); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrolen = strlen(atkeystr); if (strcmp(atkeystr, expected) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystr is not %s, it is %s\n", expected, atkeystr); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystr is not %s, it is %s\n", expected, atkeystr); goto exit; } if (atkeystrolen != expectedlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, - atkeystrolen); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, atkeystrolen); goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(atkeystr); + return ret; +} } int main() { diff --git a/packages/atclient/tests/test_atkey_to_string.c b/packages/atclient/tests/test_atkey_to_string.c index ccc3e48e..b7f30d99 100644 --- a/packages/atclient/tests/test_atkey_to_string.c +++ b/packages/atclient/tests/test_atkey_to_string.c @@ -2,8 +2,9 @@ #include "atclient/constants.h" #include "atlogger/atlogger.h" #include -#include #include +#include +#include #define TAG "test_atkey_to_string" @@ -37,17 +38,18 @@ static int test1a() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1A; const size_t expectedlen = strlen(expected); - atkey.metadata.iscached = true; - atkey.metadata.ispublic = true; + atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); + atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; ret = atclient_atstr_set_literal(&(atkey.name), "publickey"); @@ -62,16 +64,15 @@ static int test1a() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } @@ -80,7 +81,8 @@ static int test1a() { goto exit; exit: { atclient_atkey_free(&atkey); - atclient_atstr_free(&string); + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a Ended:%d\n", ret); return ret; } } @@ -88,16 +90,17 @@ exit: { static int test1b() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1B; // "public:publickey@alice" const size_t expectedlen = strlen(expected); - atkey.metadata.ispublic = true; + atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; ret = atclient_atstr_set_literal(&(atkey.name), "publickey"); @@ -112,38 +115,42 @@ static int test1b() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b Ended:%d\n", ret); + return ret; +} } static int test1c() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1C; // "public:name.wavi@jeremy" const size_t expectedlen = strlen(expected); - atkey.metadata.ispublic = true; + atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; ret = atclient_atstr_set_literal(&(atkey.name), "name"); @@ -164,39 +171,43 @@ static int test1c() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c Ended:%d\n", ret); + return ret; +} } static int test1d() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1D; // "cached:public:name.wavi@jeremy" const size_t expectedlen = strlen(expected); - atkey.metadata.iscached = true; - atkey.metadata.ispublic = true; + atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); + atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; ret = atclient_atstr_set_literal(&(atkey.name), "name"); @@ -217,33 +228,37 @@ static int test1d() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d Ended:%d\n", ret); + return ret; +} } static int test2a() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2A; // "@alice:name.wavi@bob" const size_t expectedlen = strlen(expected); @@ -274,7 +289,7 @@ static int test2a() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; @@ -282,22 +297,27 @@ static int test2a() { ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a Ended:%d\n", ret); + return ret; +} } static int test2b() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2B; // "cached:@bob:name@alice" const size_t expectedlen = strlen(expected); - atkey.metadata.iscached = true; + atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@bob"); @@ -318,33 +338,37 @@ static int test2b() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b Ended:%d\n", ret); + return ret; +} } static int test2c() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2C; // "@bob:name@alice" const size_t expectedlen = strlen(expected); @@ -369,7 +393,7 @@ static int test2c() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; @@ -382,32 +406,36 @@ static int test2c() { goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c Ended:%d\n", ret); + return ret; +} } static int test2d() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2D; // "cached:@bob:name.wavi@alice" const size_t expectedlen = strlen(expected); - atkey.metadata.iscached = true; + atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@bob"); @@ -434,33 +462,37 @@ static int test2d() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d Ended:%d\n", ret); + return ret; +} } static int test3a() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_3A; // "_lastnotificationid@alice123_4😘" const size_t expectedlen = strlen(expected); @@ -479,7 +511,7 @@ static int test3a() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; @@ -487,32 +519,36 @@ static int test3a() { if (atkey.namespacestr.len > 0 || strlen(atkey.namespacestr.str) > 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr.len > 0: %d or strlen(%s) > 0\n", - atkey.namespacestr.len, atkey.namespacestr.str); + atkey.namespacestr.len, atkey.namespacestr.str); ret = 1; goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a Ended:%d\n", ret); + return ret; +} } static int test4a() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr string; - atclient_atstr_init(&string, ATCLIENT_ATKEY_FULL_LEN); + char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_4A; // "name@alice" const size_t expectedlen = strlen(expected); @@ -531,7 +567,7 @@ static int test4a() { goto exit; } - ret = atclient_atkey_to_string(&atkey, string.str, string.size, &string.len); + ret = atclient_atkey_to_string(&atkey, &string); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; @@ -539,27 +575,32 @@ static int test4a() { if (atkey.namespacestr.len > 0 || strlen(atkey.namespacestr.str) > 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr.len > 0: %d or strlen(%s) > 0\n", - atkey.namespacestr.len, atkey.namespacestr.str); + atkey.namespacestr.len, atkey.namespacestr.str); ret = 1; goto exit; } - ret = strncmp(string.str, expected, expectedlen); + ret = strcmp(string, expected); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - string.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; goto exit; -exit: { return ret; } +exit: { + free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a Ended:%d\n", ret); + return ret; +} } static int test4b() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Starting...\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -570,19 +611,16 @@ static int test4b() { const char *expected = TEST_ATKEY_TO_STRING_4B; // "name.wavi@jeremy_0" - atclient_atstr actual; - atclient_atstr_init(&actual, ATCLIENT_ATKEY_FULL_LEN); - - ret = atclient_atkey_to_string(&atkey, actual.str, actual.size, &actual.len); + char *atkeystr = NULL; + ret = atclient_atkey_to_string(&atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - if (strncmp(actual.str, expected, strlen(expected)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, - actual.str); + if (strcmp(atkeystr, expected) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, atkeystr); ret = 1; goto exit; } @@ -591,14 +629,16 @@ static int test4b() { goto exit; exit: { atclient_atkey_free(&atkey); - atclient_atstr_free(&actual); - return ret; } + free(atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Ended:%d\n", ret); + return ret; +} } int main() { int ret = 1; - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_ERROR); + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); ret = test1a(); if (ret != 0) { From b4c2cc9d365cec182c7059a10df01f23222f51c4 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 21:46:01 -0400 Subject: [PATCH 014/193] fix: examples that use atclient_atkey_to_string --- examples/desktop/crud/get_publickey.c | 11 ++++++----- examples/desktop/crud/get_selfkey.c | 19 ++++++++++--------- examples/desktop/crud/get_sharedkey.c | 10 +++++----- examples/desktop/crud/put_publickey.c | 11 ++++++----- examples/desktop/crud/put_selfkey.c | 14 ++++++++------ examples/desktop/crud/put_sharedkey.c | 9 +++++---- 6 files changed, 40 insertions(+), 34 deletions(-) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index 28a5901c..ab522b6b 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -47,8 +47,7 @@ int main() { atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; char *atserver_host = NULL; int atserver_port = -1; @@ -69,13 +68,14 @@ int main() { goto exit; } - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, - (int)atkeystr.len, atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, + atkeystr); ret = atclient_get_publickey(&atclient, &atkey, value, valuelen, &valueolen, true); if (ret != 0) { @@ -107,6 +107,7 @@ exit : { atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index d5a124d4..8406db4b 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -49,10 +49,10 @@ int main() { atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } @@ -74,13 +74,14 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Created self key\n"); } - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string\n"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, - (int)atkeystr.len, atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)(atkeystrlen), + atkeystr); ret = atclient_get_selfkey(&atclient, &atkey, value, valuelen, &(valueolen)); if (ret != 0) { @@ -88,18 +89,18 @@ int main() { goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value.str (%lu): \"%.*s\"\n", valueolen, (int)valueolen, - value); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value.str (%lu): \"%.*s\"\n", valueolen, (int)valueolen, value); ret = 0; goto exit; -exit : { +exit: { atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); atclient_atstr_free(&atkeystr); atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 4b7a2b48..cbfee0ce 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -63,8 +63,7 @@ int main() { atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATSIGN_ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); @@ -90,13 +89,13 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Created shared key\n"); } - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string\n"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, (int)atkeystr.len, - atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int) atkeystrlen, atkeystr); ret = atclient_get_sharedkey(&atclient, &atkey, value.str, value.size, &value.len, NULL, false); if (ret != 0) { @@ -140,6 +139,7 @@ exit: { atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index a26a4d51..1445e7e8 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -49,8 +49,7 @@ int main() { atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); @@ -71,13 +70,14 @@ int main() { atclient_atkey_metadata_set_ttl(&atkey.metadata, 60 * 1000 * 10); // 10 minutes atclient_atkey_metadata_set_ccd(&atkey.metadata, true); - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, - (int)atkeystr.len, atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, + (int)atkeystrlen, atkeystr); if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); @@ -101,6 +101,7 @@ exit: { atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index eb3e17e5..cb391d6c 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -49,10 +49,10 @@ int main() { atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } @@ -70,13 +70,14 @@ int main() { atclient_atkey_metadata_set_ttl(&atkey.metadata, 60 * 1000 * 10); // 10 minutes - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, (int)atkeystr.len, - atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, + atkeystr); if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); @@ -99,6 +100,7 @@ exit: { atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index 6aacc618..a9f74eb9 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -52,8 +52,7 @@ int main() atclient_atkeys_init(&atkeys); atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); @@ -72,12 +71,13 @@ int main() atclient_atkey_metadata_set_ttl(&atkey.metadata, 60*1000*10); // 10 minutes - if((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Putting atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, (int) atkeystr.len, atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Putting atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int) atkeystrlen, atkeystr); if((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); @@ -95,6 +95,7 @@ exit: { atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); + free(atkeystr); return ret; } } From ae7675b097693cadb00097b824ee3af7d83a1acc Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:07:06 -0400 Subject: [PATCH 015/193] fix: events errors --- examples/desktop/events/notify.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 7635b848..0c7aabac 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -43,8 +43,7 @@ int main(int argc, char *argv[]) { atclient_atkeys atkeys; atclient_atkeys_init(&atkeys); - atclient_atstr atkeystr; - atclient_atstr_init(&atkeystr, ATCLIENT_ATKEY_FULL_LEN); + char *atkeystr = NULL; atclient_notify_params notify_params; atclient_notify_params_init(¬ify_params); @@ -116,13 +115,14 @@ int main(int argc, char *argv[]) { atclient_atkey_metadata_set_ccd(&atkey.metadata, true); - if ((ret = atclient_atkey_to_string(&atkey, atkeystr.str, atkeystr.size, &atkeystr.len)) != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string\n"); goto exit; } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystr.len, (int)atkeystr.len, - atkeystr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, + atkeystr); notify_params.atkey = &atkey; notify_params.value = ATKEY_VALUE; @@ -143,6 +143,7 @@ exit: { atclient_atkey_free(&atkey); free(atserver_host); atclient_free(&atclient); + free(atkeystr); return ret; } } From 561f19e6c012d75a06b15d331ec3f03a8f396f86 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:07:52 -0400 Subject: [PATCH 016/193] fix: functional test error --- .../functional_tests/tests/test_atclient_get_atkeys.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_get_atkeys.c b/tests/functional_tests/tests/test_atclient_get_atkeys.c index 9da586b3..99ca9122 100644 --- a/tests/functional_tests/tests/test_atclient_get_atkeys.c +++ b/tests/functional_tests/tests/test_atclient_get_atkeys.c @@ -79,14 +79,11 @@ static int test_1_atclient_get_atkeys(atclient *ctx, const char *scan_regex, con goto exit; } - char buf[4096]; - memset(buf, 0, sizeof(char) * 4096); - size_t bufolen = 0; - for(size_t i = 0; i < atkey_array_len; i++) { - atclient_atkey_to_string(&atkey_array[i], buf, 4096, &bufolen); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkey_array[%zu]: %s\n", i, buf); - memset(buf, 0, sizeof(char) * 4096); + char *atkeystr = NULL; + atclient_atkey_to_string(&atkey_array[i], &atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkey_array[%zu]: %s\n", i, atkeystr); + free(atkeystr); } for (size_t i = 0; i < atkey_array_len; i++) { From af56d3ef4360e7ec8f047fd55042dc789e506992 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:20:01 -0400 Subject: [PATCH 017/193] fix: repl errors --- examples/desktop/repl/src/main.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 92bf5d4b..2cf12b48 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -189,13 +189,12 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); goto scan_end; } - const size_t atkeybufsize = 1024; - char atkeybuf[1024]; - size_t atkey_buf_len; + char *atkeystr = NULL; for (size_t i = 0; i < arrlen; i++) { - memset(atkeybuf, 0, sizeof(char) * atkeybufsize); - atclient_atkey_to_string(&arr[i], atkeybuf, atkeybufsize, &atkey_buf_len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atKey[%i]: \'%s\'\n", i, atkeybuf); + atclient_atkey_to_string(&arr[i], &atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atKey[%i]: \'%s\'\n", i, atkeystr); + free(atkeystr); + atkeystr = NULL; } scan_end: { for (size_t i = 0; i < arrlen; i++) { @@ -210,16 +209,16 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); goto deleteall_end; } - char buf[4096]; - size_t bufolen; + char *atkeystr = NULL; for(size_t i = 0; i < arrlen; i++) { if((ret = atclient_delete(&atclient, &arr[i])) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d | failed to delete atKey\n", ret); continue; } - memset(buf, 0, sizeof(char) * 4096); - atclient_atkey_to_string(&arr[i], buf, 4096, &bufolen); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Deleted atKey: %s\n", buf); + atclient_atkey_to_string(&arr[i], &atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Deleted atKey: %s\n", atkeystr); + free(atkeystr); + atkeystr = NULL; } deleteall_end: { for (size_t i = 0; i < arrlen; i++) { From b9aa92dbfa3252efa09b77ca44c73f71c24757d2 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:28:21 -0400 Subject: [PATCH 018/193] chore: remove logging --- packages/atclient/src/atkey.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 9f958da1..30c6984f 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -278,8 +278,6 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { goto error_exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "4\n"); - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->name.len, atkey->name.str); index_pos += atkey->name.len; @@ -289,8 +287,6 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { index_pos += strlen(".") + atkey->namespacestr.len; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "5\n"); - if (atkey->sharedby.str == NULL || atkey->sharedby.len == 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, @@ -299,8 +295,6 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { goto error_exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "6\n"); - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->sharedby.len, atkey->sharedby.str); index_pos += (int)atkey->sharedby.len; From 5d5f911a6bb94796c08036d2d0615c709478f7a0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:29:46 -0400 Subject: [PATCH 019/193] chore: --- .../tests/test_atclient_selfkey.c | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index c4bb87ba..90b94533 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -46,52 +46,52 @@ int main() { } if ((ret = functional_tests_pkam_auth(&atclient, &atkeys, ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n"); goto exit; } if ((ret = test_1_should_not_exist(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_1_should_not_exist"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_1_should_not_exist\n"); goto exit; } if ((ret = test_2_put(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_2_put"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_2_put\n"); goto exit; } if ((ret = test_3_get(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_3_get"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_3_get\n"); goto exit; } if ((ret = test_4_delete(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_4_delete"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_4_delete\n"); goto exit; } if ((ret = test_5_should_not_exist(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_5_should_not_exist"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_5_should_not_exist\n"); goto exit; } if ((ret = test_6_put_with_metadata(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_6_put_with_metadata"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_6_put_with_metadata\n"); goto exit; } if ((ret = test_7_get_with_metadata(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_7_get_with_metadata"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_7_get_with_metadata\n"); goto exit; } if ((ret = test_8_delete(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_8_delete"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_8_delete\n"); goto exit; } if ((ret = test_9_should_not_exist(&atclient)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_9_should_not_exist"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed test_9_should_not_exist\n"); goto exit; } From 234bbd63525a6f607bf75f109a31b356a09f78a5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:36:25 -0400 Subject: [PATCH 020/193] fix: --- examples/desktop/crud/get_publickey.c | 1 - examples/desktop/crud/get_selfkey.c | 1 - examples/desktop/crud/get_sharedkey.c | 1 - examples/desktop/crud/put_publickey.c | 1 - examples/desktop/crud/put_selfkey.c | 1 - examples/desktop/crud/put_sharedkey.c | 1 - examples/desktop/events/notify.c | 1 - .../atclient/src/atclient_get_publickey.c | 1 - packages/atclient/src/atclient_put.c | 13 ++++--- .../tests/test_atclient_publickey.c | 38 +++++++++---------- 10 files changed, 26 insertions(+), 33 deletions(-) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index ab522b6b..a76b8b63 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -101,7 +101,6 @@ int main() { ret = 0; goto exit; exit : { - atclient_atstr_free(&atkeystr); atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); atclient_atsign_free(&atsign); diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index 8406db4b..dac5bda2 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -96,7 +96,6 @@ int main() { exit: { atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); - atclient_atstr_free(&atkeystr); atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index cbfee0ce..8d1b9215 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -135,7 +135,6 @@ exit: { atclient_atstr_free(&value); atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); - atclient_atstr_free(&atkeystr); atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index 1445e7e8..ce1f8895 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -95,7 +95,6 @@ int main() { ret = 0; goto exit; exit: { - atclient_atstr_free(&atkeystr); atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); atclient_atsign_free(&atsign); diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index cb391d6c..eb679160 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -94,7 +94,6 @@ int main() { ret = 0; goto exit; exit: { - atclient_atstr_free(&atkeystr); atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); atclient_atsign_free(&atsign); diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index a9f74eb9..94c53fd5 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -89,7 +89,6 @@ int main() ret = 0; goto exit; exit: { - atclient_atstr_free(&atkeystr); atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); atclient_atsign_free(&atsign); diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 0c7aabac..373cf7be 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -138,7 +138,6 @@ int main(int argc, char *argv[]) { ret = 0; goto exit; exit: { - atclient_atstr_free(&atkeystr); atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); free(atserver_host); diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index d8e2f733..b8034a9f 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -122,7 +122,6 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu ret = 0; goto exit; exit: { - atclient_atstr_free(&atkeystr); if (root != NULL) { cJSON_Delete(root); } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 41751507..4f590e4f 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -15,7 +15,8 @@ #define TAG "atclient_put" -static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid); +static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, + const size_t valuelen, int *commitid); int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { int ret = 1; @@ -23,7 +24,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 1. Check if valid arguments were passed */ - if((ret = atclient_put_valid_args_check(atclient, atkey, value, valuelen, commitid)) != 0) { + if ((ret = atclient_put_valid_args_check(atclient, atkey, value, valuelen, commitid)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_valid_args_check: %d\n", ret); return ret; } @@ -228,9 +229,8 @@ exit: { } } -static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) -{ -{ +static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, + const size_t valuelen, int *commitid) { int ret = 1; if (atclient == NULL) { ret = 1; @@ -268,5 +268,6 @@ static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atk "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); return ret; } -} + ret = 0; + return ret; } \ No newline at end of file diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index 7765e224..93b4e439 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -113,12 +113,12 @@ static int test_1_put(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put\n"); goto exit; } @@ -148,18 +148,18 @@ static int test_2_get(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } if ((ret = atclient_get_publickey(atclient, &atkey, value, valuesize, &valuelen, false)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey\n"); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); if (strcmp(value, ATKEY_VALUE) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed value comparison"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed value comparison\n"); goto exit; } @@ -184,12 +184,12 @@ static int test_3_delete(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } if ((ret = atclient_delete(atclient, &atkey)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } @@ -235,7 +235,7 @@ static int test_5_put_with_metadata(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -246,7 +246,7 @@ static int test_5_put_with_metadata(atclient *atclient) { atclient_atkey_metadata_set_isbinary(&(atkey.metadata), ATKEY_ISBINARY); if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put\n"); goto exit; } @@ -276,12 +276,12 @@ static int test_6_get_with_metadata(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } if ((ret = atclient_get_publickey(atclient, &atkey, value, valuesize, &valuelen, true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey\n"); goto exit; } @@ -292,35 +292,35 @@ static int test_6_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); - if (atkey.metadata.ccd != ATKEY_CCD) { + if (atclient_atkey_metadata_is_ccd_initialized(&(atkey.metadata)) && atkey.metadata.ccd != ATKEY_CCD) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed ccd comparison, got %d and expected %d\n", atkey.metadata.ccd, ATKEY_CCD); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ccd: %d\n", atkey.metadata.ccd); - if (atkey.metadata.ttl != ATKEY_TTL) { + if (atclient_atkey_metadata_is_ttl_initialized(&(atkey.metadata)) && atkey.metadata.ttl != ATKEY_TTL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed ttl comparison, got %d and expected %d\n", atkey.metadata.ttl, ATKEY_TTL); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttl: %d\n", atkey.metadata.ttl); - if (atkey.metadata.ttr != ATKEY_TTR) { + if (atclient_atkey_metadata_is_ttr_initialized(&(atkey.metadata)) && atkey.metadata.ttr != ATKEY_TTR) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed ttr comparison, got %d and expected %d\n", atkey.metadata.ttr, ATKEY_TTR); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttr: %d\n", atkey.metadata.ttr); - if (atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { + if (atclient_atkey_metadata_is_isencrypted_initialized(&(atkey.metadata)) && atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed isencrypted comparison, got %d and expected %d\n", atkey.metadata.isencrypted, ATKEY_ISENCRYPTED); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.isencrypted: %d\n", atkey.metadata.isencrypted); - if (atkey.metadata.isbinary != ATKEY_ISBINARY) { + if (atclient_atkey_metadata_is_isbinary_initialized(&(atkey.metadata)) && atkey.metadata.isbinary != ATKEY_ISBINARY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed isbinary comparison, got %d and expected %d\n", atkey.metadata.isbinary, ATKEY_ISBINARY); goto exit; @@ -346,12 +346,12 @@ static int test_7_delete(atclient *atclient) { if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } if ((ret = atclient_delete(atclient, &atkey)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } @@ -369,7 +369,7 @@ static int test_8_should_not_exist(atclient *atclient) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_8_should_not_exist Begin\n"); if ((ret = functional_tests_publickey_exists(atclient, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed functional_tests_atkey_should_not_exist"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed functional_tests_atkey_should_not_exist\n"); goto exit; } From 51064329a1aa84c02d0997952e84f32d60c466ea Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 11 Jul 2024 22:40:21 -0400 Subject: [PATCH 021/193] fix: --- packages/atclient/src/notify.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index a2356142..3abaa4c3 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -366,9 +366,6 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval } off += strlen(metadata_protocol_str); - snprintf(cmd + off, metadatastrlen, ":"); - off += strlen(":"); - if ((res = atclient_atkey_to_string(params->atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed with code: %d\n", res); return res; @@ -379,7 +376,8 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval atkeylen, atkeystrlen); goto exit; } - off += atkeylen; + snprintf(cmd + off, cmdsize - off, ":%s", atkeystr); + off += strlen(":") + atkeylen; if (cmdvaluelen > 0) { snprintf(cmd + off, cmdsize - off, ":%.*s", (int)cmdvaluelen, cmdvalue); From 945b3f542ec7dbb3b0c6c368dafa5e2b90ffe23a Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 10:07:19 -0400 Subject: [PATCH 022/193] feat: optimize and clean atclient_put --- packages/atclient/src/atclient_put.c | 168 +++++++++++++++------------ 1 file changed, 91 insertions(+), 77 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 4f590e4f..d1b66367 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -33,15 +33,13 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c * 2. Allocate variables */ char *atkeystr = NULL; + size_t atkeystrlen = 0; - const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to - // receive commmit id from `update:` - unsigned char *recv = NULL; - if (!atclient->async_read) { - recv = malloc(sizeof(unsigned char) * recvsize); - memset(recv, 0, sizeof(unsigned char) * recvsize); - } - size_t recvlen = 0; + char *cmdbuffer = NULL; + size_t cmdbuffersize = 0; + + char *metadataprotocolstr = NULL; + size_t metadataprotocolstrlen = 0; const short ivsize = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; @@ -62,53 +60,82 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(ciphertextbase64, 0, sizeof(char) * ciphertextbase64size); size_t ciphertextbase64len = 0; - const size_t sharedenckeybase64size = atchops_base64_encoded_size(ATCHOPS_AES_256 / 8) + 1; + const size_t sharedenckeysize = ATCHOPS_AES_256 / 8; + unsigned char sharedenckey[sharedenckeysize]; + memset(sharedenckey, 0, sizeof(unsigned char) * sharedenckeysize); + size_t sharedenckeylen = 0; + + const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(char) * sharedenckeybase64size); + size_t sharedenckeybase64len = 0; - char *cmdbuffer = NULL; - char *metadata_protocol_str = NULL; + const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to + // receive commmit id from `update:` + unsigned char *recv = NULL; + if (!atclient->async_read) { + recv = malloc(sizeof(unsigned char) * recvsize); + memset(recv, 0, sizeof(unsigned char) * recvsize); + } + size_t recvlen = 0; - // 2. build update: command - ret = atclient_atkey_to_string(atkey, &atkeystr); - if (ret != 0) { + /* + * 3. Build `update:` command + * 3a. Build the AtKey string and Metadata Protocol String + * 3b. Encrypt the value, if needed. + * 3c. Build the command + */ + + // 3a. Build the AtKey string and Metadata Protocol String + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - const size_t atkeystrlen = strlen(atkeystr); + atkeystrlen = strlen(atkeystr); + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadataprotocolstr))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); + goto exit; + } + metadataprotocolstrlen = strlen(metadataprotocolstr); + + // 3b. Encrypt the value, if needed. + // > 3b.1 If the AtKey is a publickey, no encryption is needed. + // > 3b.2 If the AtKey is a selfkey, encrypt with self encryption key. + // > 3b.3 If the AtKey is a sharedkey, encrypt with shared encryption key. + // > > 3b.3a If the shared encryption key doesn't exist, create one for us and one for the other person. + // > > 3b.3b If the shared encryption key does exist, encrypt with it. if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - // no encryption + // 3b.1 no encryption memcpy(ciphertextbase64, value, valuelen); ciphertextbase64len = valuelen; } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SELFKEY) { + // 3b.2 encrypt with self encryption key const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; unsigned char selfencryptionkey[selfencryptionkeysize]; - memset(selfencryptionkey, 0, sizeof(unsigned char) * (ATCHOPS_AES_256 / 8)); + memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); size_t selfencryptionkeylen = 0; - ret = atchops_base64_decode((const unsigned char *)atclient->atkeys.selfencryptionkeystr.str, - atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, selfencryptionkeysize, - &selfencryptionkeylen); - if (ret != 0) { + + if ((ret = atchops_base64_decode((const unsigned char *)atclient->atkeys.selfencryptionkeystr.str, + atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, + selfencryptionkeysize, &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - ret = atchops_aesctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, - ciphertextsize, &ciphertextlen); - if (ret != 0) { + if ((ret = atchops_aesctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, + ciphertext, ciphertextsize, &ciphertextlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); goto exit; } - ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, - &ciphertextbase64len); - if (ret != 0) { + if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, + &ciphertextbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - // encrypt with shared encryption key + // 3b.3 encrypt with shared encryption key // get our AES shared key // if it doesn't exist, create one for us and create one for the other person @@ -117,51 +144,41 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c atclient_atsign recipient; atclient_atsign_init(&recipient, atkey->sharedwith.str); - ret = atclient_get_shared_encryption_key_shared_by_me(atclient, &recipient, sharedenckeybase64, true); - if (ret != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, &recipient, sharedenckeybase64, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); goto error_cleanup; } - // encrypt with shared encryption key - ret = atchops_iv_generate(iv); - if (ret != 0) { + if ((ret = atchops_iv_generate(iv)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); goto error_cleanup; } - ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)ivbase64, ivbase64size, &ivbase64len); - if (ret != 0) { + if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)ivbase64, ivbase64size, + &ivbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto error_cleanup; } - ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64, ivbase64len); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64, ivbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); goto error_cleanup; } - unsigned char sharedenckey[ATCHOPS_AES_256 / 8]; - memset(sharedenckey, 0, sizeof(unsigned char) * (ATCHOPS_AES_256 / 8)); - size_t sharedenckeylen = 0; - ret = atchops_base64_decode((unsigned char *)sharedenckeybase64, strlen(sharedenckeybase64), sharedenckey, - sizeof(sharedenckey), &sharedenckeylen); - if (ret != 0) { + if ((ret = atchops_base64_decode((unsigned char *)sharedenckeybase64, strlen(sharedenckeybase64), sharedenckey, + sizeof(sharedenckey), &sharedenckeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto error_cleanup; } - ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, - ciphertextsize, &ciphertextlen); - if (ret != 0) { + if ((ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, + ciphertextsize, &ciphertextlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); goto error_cleanup; } - ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, - &ciphertextbase64len); - if (ret != 0) { + if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, + &ciphertextbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto error_cleanup; } @@ -176,28 +193,21 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c non_error_cleanup: { atclient_atsign_free(&recipient); } } - size_t cmdbufferlen = strlen(" update:\r\n") + atkeystrlen + ciphertextbase64len + 1; // + 1 for null terminator + // 3c. Build the command - ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadata_protocol_str)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); - goto exit; - } - - const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); - - if (metadata_protocol_str_len > 0) { - cmdbufferlen += metadata_protocol_str_len; - } - cmdbuffer = malloc(sizeof(char) * cmdbufferlen); - memset(cmdbuffer, 0, sizeof(char) * cmdbufferlen); - - snprintf(cmdbuffer, cmdbufferlen, "update%.*s:%.*s %.*s\r\n", (int)metadata_protocol_str_len, metadata_protocol_str, + cmdbuffersize = strlen("update: \r\n") + metadataprotocolstrlen + atkeystrlen + ciphertextbase64len + + 1; // + 1 for null terminator + cmdbuffer = malloc(sizeof(char) * cmdbuffersize); + memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); + snprintf(cmdbuffer, cmdbuffersize, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadataprotocolstr, (int)atkeystrlen, atkeystr, (int)ciphertextbase64len, ciphertextbase64); - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbufferlen - 1, recv, - recvsize, &recvlen); - if (ret != 0) { + /* + * 4. Send the command + */ + + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, + recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } else if (atclient->async_read) { @@ -211,6 +221,10 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } + /* + * 5. Return the commit id. + */ + if (commitid != NULL) { char *recvwithoutdata = (char *)recv + 5; *commitid = atoi(recvwithoutdata); @@ -223,7 +237,7 @@ exit: { free(recv); } free(cmdbuffer); - free(metadata_protocol_str); + free(metadataprotocolstr); free(atkeystr); return ret; } @@ -235,39 +249,39 @@ static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atk if (atclient == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - return ret; + goto exit; } if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); - return ret; + goto exit; } if (value == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - return ret; + goto exit; } if (valuelen == 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is 0\n"); - return ret; + goto exit; } - // make sure shared by is atclient->atsign.atsign if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); - return ret; + goto exit; } if (atclient->async_read) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); - return ret; + goto exit; } ret = 0; - return ret; + goto exit; +exit: { return ret; } } \ No newline at end of file From 9d40cbab0a82a76983665590a8dfa47d1d609b48 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 11:09:43 -0400 Subject: [PATCH 023/193] refactor: atclient_get_sharedkey --- .../atclient/src/atclient_get_sharedkey.c | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 65cdaee2..43c4974e 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -109,11 +109,9 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // build command // command_prefix = "llookup:all:" - const short command_prefix_len = 12; - - const size_t command_len = command_prefix_len + atkeystrlen + 3; - command = malloc(command_len * sizeof(char)); - snprintf(command, command_len, "llookup:all:%s\r\n", atkeystr); + const size_t commandsize = strlen("llookup:all:") + atkeystrlen + strlen("\r\n") + 1; + command = malloc(commandsize * sizeof(char)); + snprintf(command, commandsize, "llookup:all:%s\r\n", atkeystr); // send command and recv response const size_t recvsize = 4096; @@ -121,8 +119,8 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen = 0; - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, strlen((char *)command), - recv, recvsize, &recvlen); + ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, + recvsize, &recvlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -227,14 +225,9 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke ret = 0; goto exit; exit: { - if (enc_key_mem) - free(enc_key); - if (command) - free(command); - if (recv) - free(recv); - if (response_prefix) - free(response_prefix); + free(enc_key); + free(command); + free(recv); free(atkeystr); return ret; } From 7303537e8cad1ad8b18d1f179768e7724b57c8fe Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 11:09:51 -0400 Subject: [PATCH 024/193] fix: atclient_put --- packages/atclient/src/atclient_put.c | 55 ++++++++++++++-------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index d1b66367..378be06a 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -81,36 +81,22 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 3. Build `update:` command - * 3a. Build the AtKey string and Metadata Protocol String - * 3b. Encrypt the value, if needed. - * 3c. Build the command + * 3a. Encrypt the value, if needed. + * 3b. Build the command */ - // 3a. Build the AtKey string and Metadata Protocol String - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); - goto exit; - } - atkeystrlen = strlen(atkeystr); - - if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadataprotocolstr))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); - goto exit; - } - metadataprotocolstrlen = strlen(metadataprotocolstr); - - // 3b. Encrypt the value, if needed. - // > 3b.1 If the AtKey is a publickey, no encryption is needed. - // > 3b.2 If the AtKey is a selfkey, encrypt with self encryption key. - // > 3b.3 If the AtKey is a sharedkey, encrypt with shared encryption key. - // > > 3b.3a If the shared encryption key doesn't exist, create one for us and one for the other person. - // > > 3b.3b If the shared encryption key does exist, encrypt with it. + // 3a. Encrypt the value, if needed. + // > 3a.1 If the AtKey is a publickey, no encryption is needed. + // > 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. + // > 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. + // > > If the shared encryption key doesn't exist, create one for us and one for the other person. + // > > If the shared encryption key does exist, encrypt with it. if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - // 3b.1 no encryption + // 3a.1 no encryption memcpy(ciphertextbase64, value, valuelen); ciphertextbase64len = valuelen; } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SELFKEY) { - // 3b.2 encrypt with self encryption key + // 3a.2 encrypt with self encryption key const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; unsigned char selfencryptionkey[selfencryptionkeysize]; memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); @@ -135,7 +121,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - // 3b.3 encrypt with shared encryption key + // 3A.3 encrypt with shared encryption key // get our AES shared key // if it doesn't exist, create one for us and create one for the other person @@ -193,7 +179,19 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c non_error_cleanup: { atclient_atsign_free(&recipient); } } - // 3c. Build the command + // 3b. Build the command + + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); + goto exit; + } + atkeystrlen = strlen(atkeystr); + + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadataprotocolstr))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); + goto exit; + } + metadataprotocolstrlen = strlen(metadataprotocolstr); cmdbuffersize = strlen("update: \r\n") + metadataprotocolstrlen + atkeystrlen + ciphertextbase64len + 1; // + 1 for null terminator @@ -205,12 +203,13 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 4. Send the command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; - } else if (atclient->async_read) { + } + + if (atclient->async_read) { goto exit; } From 126efad4c7679c566706a91e947fedeef9ce179c Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 11:11:14 -0400 Subject: [PATCH 025/193] docs: atclient_put --- packages/atclient/src/atclient_put.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 378be06a..045993a4 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -81,16 +81,15 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 3. Build `update:` command - * 3a. Encrypt the value, if needed. - * 3b. Build the command + * 3a. Encrypt the value, if needed. + * 3a.1 If the AtKey is a publickey, no encryption is needed. + * 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. + * 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. + * If the shared encryption key doesn't exist, create one for us and one for the other person. + * If the shared encryption key does exist, encrypt with it. + * 3b. Build the command */ - // 3a. Encrypt the value, if needed. - // > 3a.1 If the AtKey is a publickey, no encryption is needed. - // > 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. - // > 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. - // > > If the shared encryption key doesn't exist, create one for us and one for the other person. - // > > If the shared encryption key does exist, encrypt with it. if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // 3a.1 no encryption memcpy(ciphertextbase64, value, valuelen); @@ -121,7 +120,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - // 3A.3 encrypt with shared encryption key + // 3aA.3 encrypt with shared encryption key // get our AES shared key // if it doesn't exist, create one for us and create one for the other person From 23fb4126a94d52cbe2c242c4c178c48f7123b9c2 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 15:06:21 -0400 Subject: [PATCH 026/193] chore: delete atbytes.c and atstr.c --- packages/atclient/CMakeLists.txt | 2 - packages/atclient/src/atbytes.c | 62 ---------------- packages/atclient/src/atstr.c | 123 ------------------------------- 3 files changed, 187 deletions(-) delete mode 100644 packages/atclient/src/atbytes.c delete mode 100644 packages/atclient/src/atstr.c diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index cfd2d8c5..895fa765 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -5,7 +5,6 @@ option(ATCLIENT_BUILD_TESTS "Build tests for atclient" OFF) set(ATCLIENT_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include) set( ATCLIENT_SOURCES - ${CMAKE_CURRENT_LIST_DIR}/src/atbytes.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_publickey.c @@ -18,7 +17,6 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c ${CMAKE_CURRENT_LIST_DIR}/src/atsign.c - ${CMAKE_CURRENT_LIST_DIR}/src/atstr.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c diff --git a/packages/atclient/src/atbytes.c b/packages/atclient/src/atbytes.c deleted file mode 100644 index c0605e4d..00000000 --- a/packages/atclient/src/atbytes.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "atclient/atbytes.h" -#include "atclient/atstr.h" -#include "atlogger/atlogger.h" -#include -#include -#include - -#define TAG "atbytes" - -void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen) { - memset(atbytes, 0, sizeof(atclient_atbytes)); - atbytes->size = atbyteslen; - atbytes->bytes = malloc(sizeof(unsigned char) * atbytes->size); - atbytes->len = 0; -} - -void atclient_atbytes_reset(atclient_atbytes *atbytes) { - memset(atbytes->bytes, 0, sizeof(unsigned char) * atbytes->size); - atbytes->len = 0; -} - -int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen) { - int ret = 1; - if (byteslen > atbytes->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "byteslen is greater than atbyteslen. byteslen: %lu, atbyteslen: %lu\n", byteslen, - atbytes->size); - ret = 1; - goto exit; - } - memcpy(atbytes->bytes, bytes, byteslen); - atbytes->len = byteslen; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen) { - int ret = 1; - if (strlen > atbytes->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "strlen is greater than atbyteslen. strlen: %lu, atbyteslen: %lu\n", strlen, atbytes->size); - ret = 1; - goto exit; - } - memcpy(atbytes->bytes, (unsigned char *)str, strlen); - atbytes->len = strlen; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr) { - int ret = atclient_atbytes_convert(atbytes, atstr.str, atstr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atbytes_convert failed"); - goto exit; - } -exit: { return ret; } -} - -void atclient_atbytes_free(atclient_atbytes *atbytes) { free(atbytes->bytes); } diff --git a/packages/atclient/src/atstr.c b/packages/atclient/src/atstr.c deleted file mode 100644 index f4fc71ad..00000000 --- a/packages/atclient/src/atstr.c +++ /dev/null @@ -1,123 +0,0 @@ -#include "atclient/atstr.h" -#include "atlogger/atlogger.h" -#include -#include -#include -#include -#include - -#define TAG "atstr" - -void atclient_atstr_init(atclient_atstr *atstr, const size_t bufferlen) { - memset(atstr, 0, sizeof(atclient_atstr)); - atstr->size = bufferlen; - atstr->str = (char *)malloc(sizeof(char) * atstr->size); - memset(atstr->str, 0, sizeof(char) * atstr->size); - atstr->len = 0; -} - -int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t bufferlen, const char *format, ...) { - int ret = 1; - atclient_atstr_init(atstr, bufferlen); - ret = atclient_atstr_set_literal(atstr, format); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed"); - goto exit; - } - goto exit; -exit: { return ret; } -} - -void atclient_atstr_reset(atclient_atstr *atstr) { - memset(atstr->str, 0, atstr->size); - atstr->len = 0; -} - -int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...) { - int ret = 1; - if (atstr->str == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atstr->str is NULL\n"); - goto exit; - } - va_list args; - va_start(args, format); - ret = vsnprintf(atstr->str, atstr->size, format, args); - va_end(args); // Add va_end() to properly handle variadic arguments - if (ret < 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); - goto exit; - } - atstr->len = ret; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t len) { - int ret = 1; - - if (len > atstr->size) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "len > atstr->len (%d > %d)\n", len, atstr->size); - goto exit; - } - - memcpy(atstr->str, str, len); - atstr->len = len; - - ret = 0; - goto exit; - -exit: { return ret; } -} - -int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data) { - int ret = 1; - ret = atclient_atstr_set(atstr, data->str, data->len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed"); - goto exit; - } - goto exit; -exit: { return ret; } -} - -int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, - const size_t end) { - int ret = 1; - if (start > original.len || end > original.len) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start or end is greater than original.len\n"); - goto exit; - } - if (start > end) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start is greater than end\n"); - goto exit; - } - if (end - start > substring->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "end - start > substring->len\n"); - goto exit; - } - memcpy(substring->str, original.str + start, end - start); - substring->len = end - start; - ret = 0; - goto exit; - -exit: { return ret; } -} - -int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...) { - int ret = 1; - va_list args; - va_start(args, format); - ret = vsnprintf(atstr->str + atstr->len, atstr->size - atstr->len, format, args); - if (ret < 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); - goto exit; - } - atstr->len += ret; - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_atstr_free(atclient_atstr *atstr) { free(atstr->str); } From 6e6c1fc74f419f9fe2e9d116aa3748e8278e3a7e Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 15:09:32 -0400 Subject: [PATCH 027/193] chore: delete atbytes.h and atstr.h --- packages/atclient/include/atclient/atbytes.h | 67 -------------- packages/atclient/include/atclient/atstr.h | 95 -------------------- 2 files changed, 162 deletions(-) delete mode 100644 packages/atclient/include/atclient/atbytes.h delete mode 100644 packages/atclient/include/atclient/atstr.h diff --git a/packages/atclient/include/atclient/atbytes.h b/packages/atclient/include/atclient/atbytes.h deleted file mode 100644 index ecbc16cc..00000000 --- a/packages/atclient/include/atclient/atbytes.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef ATCLIENT_ATBYTES_H -#define ATCLIENT_ATBYTES_H - -#include "atclient/atstr.h" -#include - -/** - * @brief Represents a buffer of bytes. Similar to atclient_atstr - */ -typedef struct atclient_atbytes { - size_t size; // the allocated length of the buffer - unsigned char *bytes; // the buffer of bytes (pointer to the first byte in the buffer on the heap) - size_t len; // the output length of the buffer -} atclient_atbytes; - -/** - * @brief Initializes atbytes on the heap - * - * @param atbytes the atbytes to initialize - * @param atbyteslen the buffer length to allocate on the heap - */ -void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen); - -/** - * @brief Reset atbytes to all zeroes - * - * @param atbytes the atbytes to reset - */ -void atclient_atbytes_reset(atclient_atbytes *atbytes); - -/** - * @brief Set atbytes to the given bytes - * - * @param atbytes the atbytes to set - * @param bytes the bytes to set - * @param byteslen the length of the bytes - * @return int 0 on success, non-zero on failure - */ -int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen); - -/** - * @brief Convert a string to atbytes - * - * @param atbytes the atbytes to convert to - * @param str the string to convert from - * @param strlen the length of the string - * @return int 0 on success - */ -int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen); - -/** - * @brief Converts an atstr to atbytes - * - * @param atbytes atbytes to write to - * @param atstr the atstr to copy from - * @return int 0 on success - */ -int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr); - -/** - * @brief Free atbytes on the heap. Should be called once atbytes is no longer needed - * - * @param atbytes the atbytes to free - */ -void atclient_atbytes_free(atclient_atbytes *atbytes); - -#endif diff --git a/packages/atclient/include/atclient/atstr.h b/packages/atclient/include/atclient/atstr.h deleted file mode 100644 index ddda4a55..00000000 --- a/packages/atclient/include/atclient/atstr.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef ATCLIENT_ATSTR_H -#define ATCLIENT_ATSTR_H - -#include - -/** - * @brief Represents a string that is allocated on the heap - */ -typedef struct atclient_atstr { - size_t size; // buffer length - char *str; // string - size_t len; // output length (length of string) -} atclient_atstr; - -/** - * @brief Initialize an atstr - * - * @param atstr pointer to atstr to initialize - * @param buffersize length of buffer to allocate in bytes (recommended to use a number that is a power of 2). the - * buffersize is the length of the buffer generated. we do not add 1 for null terminator. - */ -void atclient_atstr_init(atclient_atstr *atstr, const size_t buffersize); - -/** - * @brief initialize an atstr with a literal string - * - * @param atstr the atstr struct to populate - * @param str the string to set atstr to. best to use string literals here. (or a null-terminated string) - */ -int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t buffersize, const char *format, ...); - -/** - * @brief set an atstr to an empty string - * - * @param atstr the atstr struct to reset - */ -void atclient_atstr_reset(atclient_atstr *atstr); - -/** - * @brief set an atstr to a string - * - * @param atstr the atstr struct to populate - * @param str the string to set atstr to - * @param size the length of the string - */ -int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t size); - -/** - * @brief Set an atstr to a literal string, assumed to be null-terminated - * - * @param atstr atstr struct to populate - * @param str null-terminated string to set atstr to. Best to use string literals here - */ -int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...); - -/** - * @brief Copy what is in an atstr to another atstr - * - * @param atstr pointer that is being overwritten - * @param data pointer that is being copied from - */ -int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data); - -/** - * @brief Free an atstr - * - * @param atstr pointer to atstr to free from the heap - */ -void atclient_atstr_free(atclient_atstr *atstr); - -/** - * @brief Copy what is in original to substring and then set substring to a substring of original - * - * @param substring the atstr to set to the substring. Assumed that this is already initialized (via - * atclient_atstr_init) - * @param original the atstr to get the substring from. Assumed that this is already initialized (via - * atclient_atstr_init) - * @param start the start index of the substring - * @param end the end index of the substring - * @return int 0 on success, non-zero on failure - */ -int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, - const size_t end); - -/** - * @brief Append a string to an atstr - * - * @param atstr the atstr to append to - * @param format the format of the string to append - * @param ... the arguments to format - * @return int 0 on success - */ -int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...); - -#endif From d7a5f4b9ac32daa24257a15c40250046a249c385 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:08:55 -0400 Subject: [PATCH 028/193] chore: remove atstr from get_sharedkey example --- examples/desktop/crud/get_sharedkey.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 8d1b9215..f85dd6d5 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -42,10 +42,10 @@ int main() { atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - const size_t valuelen = 1024; - atclient_atstr value; - atclient_atstr_init(&value, valuelen); - + const size_t valuesize = 1024; + char value[valuesize]; + memset(value, 0, sizeof(char) * valuesize); + size_t valuelen; char *atserver_host = NULL; int atserver_port = -1; @@ -97,20 +97,20 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int) atkeystrlen, atkeystr); - ret = atclient_get_sharedkey(&atclient, &atkey, value.str, value.size, &value.len, NULL, false); + ret = atclient_get_sharedkey(&atclient, &atkey, value, valuesize, valuelen, NULL, false); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared key"); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value.str (%lu): \"%.*s\"\n", value.len, (int)value.len, value.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value.str (%lu): \"%.*s\"\n", valuelen, (int)valuelen, value); const size_t value_hash_len = 32; unsigned char *value_hash = calloc(value_hash_len, sizeof(unsigned char)); memset(value_hash, 0, value_hash_len); size_t value_hash_olen = 0; - ret = atchops_sha_hash(ATCHOPS_MD_SHA256, (const unsigned char *)value.str, value.len, value_hash); + ret = atchops_sha_hash(ATCHOPS_MD_SHA256, (const unsigned char *)value, valuelen, value_hash); if (ret != 0) { printf("atchops_sha_hash (failed): %d\n", ret); goto exit; @@ -132,7 +132,6 @@ int main() { free(hex_value_hash); goto exit; exit: { - atclient_atstr_free(&value); atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); atclient_atsign_free(&atsign); From 659f0a75c92fe891b466b740425953675d9ef51f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:09:08 -0400 Subject: [PATCH 029/193] chore: remove unused function in connection.c --- .../atclient/include/atclient/connection.h | 11 ------ packages/atclient/src/connection.c | 34 ------------------- 2 files changed, 45 deletions(-) diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index b1fb0797..3de3a001 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -121,17 +121,6 @@ bool atclient_connection_is_connected(atclient_connection *ctx); */ void atclient_connection_free(atclient_connection *ctx); -/** - * @brief get the host and port from a url - * - * @param host a pointer to an atclient_atstr to store the host, will hold "root.atsign.org" after the function call, - * for example. Assumed that this is already initialized via atclient_atstr_init(&host) - * @param port a pointer to an int to store the port, will hold 64 after the function call, for example - * @param url the url to parse (example "root.atsign.org:64") - * @return int 0 on success, otherwise error - */ -int atclient_connection_get_host_and_port(atclient_atstr *host, int *port, const atclient_atstr url); - /** * @brief Initialize the hooks memory allocation * diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index e26cb221..c1c6173f 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -452,40 +452,6 @@ void atclient_connection_free(atclient_connection *ctx) { ctx->_should_be_connected = false; } -int atclient_connection_get_host_and_port(atclient_atstr *host, int *port, const atclient_atstr url) { - int ret = 1; - - char *colon = strchr(url.str, ':'); - if (colon == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "no colon in url\n"); - ret = 1; - goto exit; - } - - int hostlen = colon - url.str; - if (hostlen > ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "hostlen > ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE\n"); - ret = 1; - goto exit; - } - - strncpy(host->str, url.str, hostlen); - host->size = hostlen; - host->str[hostlen] = '\0'; - *port = atoi(colon + 1); - if (*port == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "port is 0\n"); - ret = 1; - goto exit; - } - - ret = 0; - - goto exit; - -exit: { return ret; } -} - void atclient_connection_enable_hooks(atclient_connection *ctx) { ctx->hooks = malloc(sizeof(atclient_connection_hooks)); memset(ctx->hooks, 0, sizeof(atclient_connection_hooks)); From 5287e4a4ca8bd2673154c86e4db786e8f64b18e4 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:11:54 -0400 Subject: [PATCH 030/193] revert: "docs: atclient_put" This reverts commit 126efad4c7679c566706a91e947fedeef9ce179c. --- packages/atclient/src/atclient_put.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 045993a4..378be06a 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -81,15 +81,16 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 3. Build `update:` command - * 3a. Encrypt the value, if needed. - * 3a.1 If the AtKey is a publickey, no encryption is needed. - * 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. - * 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. - * If the shared encryption key doesn't exist, create one for us and one for the other person. - * If the shared encryption key does exist, encrypt with it. - * 3b. Build the command + * 3a. Encrypt the value, if needed. + * 3b. Build the command */ + // 3a. Encrypt the value, if needed. + // > 3a.1 If the AtKey is a publickey, no encryption is needed. + // > 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. + // > 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. + // > > If the shared encryption key doesn't exist, create one for us and one for the other person. + // > > If the shared encryption key does exist, encrypt with it. if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // 3a.1 no encryption memcpy(ciphertextbase64, value, valuelen); @@ -120,7 +121,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - // 3aA.3 encrypt with shared encryption key + // 3A.3 encrypt with shared encryption key // get our AES shared key // if it doesn't exist, create one for us and create one for the other person From 96bc4efc6f79ae31f4cc54cd6f1d58ba089fdfb3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:12:23 -0400 Subject: [PATCH 031/193] revert: "revert: "docs: atclient_put"" This reverts commit 5287e4a4ca8bd2673154c86e4db786e8f64b18e4. --- packages/atclient/src/atclient_put.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 378be06a..045993a4 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -81,16 +81,15 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 3. Build `update:` command - * 3a. Encrypt the value, if needed. - * 3b. Build the command + * 3a. Encrypt the value, if needed. + * 3a.1 If the AtKey is a publickey, no encryption is needed. + * 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. + * 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. + * If the shared encryption key doesn't exist, create one for us and one for the other person. + * If the shared encryption key does exist, encrypt with it. + * 3b. Build the command */ - // 3a. Encrypt the value, if needed. - // > 3a.1 If the AtKey is a publickey, no encryption is needed. - // > 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. - // > 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. - // > > If the shared encryption key doesn't exist, create one for us and one for the other person. - // > > If the shared encryption key does exist, encrypt with it. if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // 3a.1 no encryption memcpy(ciphertextbase64, value, valuelen); @@ -121,7 +120,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - // 3A.3 encrypt with shared encryption key + // 3aA.3 encrypt with shared encryption key // get our AES shared key // if it doesn't exist, create one for us and create one for the other person From ea15ebb4047335789fda992ce683a8748edaeada Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:12:40 -0400 Subject: [PATCH 032/193] revert: "chore: delete atbytes.h and atstr.h" This reverts commit 6e6c1fc74f419f9fe2e9d116aa3748e8278e3a7e. --- packages/atclient/include/atclient/atbytes.h | 67 ++++++++++++++ packages/atclient/include/atclient/atstr.h | 95 ++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 packages/atclient/include/atclient/atbytes.h create mode 100644 packages/atclient/include/atclient/atstr.h diff --git a/packages/atclient/include/atclient/atbytes.h b/packages/atclient/include/atclient/atbytes.h new file mode 100644 index 00000000..ecbc16cc --- /dev/null +++ b/packages/atclient/include/atclient/atbytes.h @@ -0,0 +1,67 @@ +#ifndef ATCLIENT_ATBYTES_H +#define ATCLIENT_ATBYTES_H + +#include "atclient/atstr.h" +#include + +/** + * @brief Represents a buffer of bytes. Similar to atclient_atstr + */ +typedef struct atclient_atbytes { + size_t size; // the allocated length of the buffer + unsigned char *bytes; // the buffer of bytes (pointer to the first byte in the buffer on the heap) + size_t len; // the output length of the buffer +} atclient_atbytes; + +/** + * @brief Initializes atbytes on the heap + * + * @param atbytes the atbytes to initialize + * @param atbyteslen the buffer length to allocate on the heap + */ +void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen); + +/** + * @brief Reset atbytes to all zeroes + * + * @param atbytes the atbytes to reset + */ +void atclient_atbytes_reset(atclient_atbytes *atbytes); + +/** + * @brief Set atbytes to the given bytes + * + * @param atbytes the atbytes to set + * @param bytes the bytes to set + * @param byteslen the length of the bytes + * @return int 0 on success, non-zero on failure + */ +int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen); + +/** + * @brief Convert a string to atbytes + * + * @param atbytes the atbytes to convert to + * @param str the string to convert from + * @param strlen the length of the string + * @return int 0 on success + */ +int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen); + +/** + * @brief Converts an atstr to atbytes + * + * @param atbytes atbytes to write to + * @param atstr the atstr to copy from + * @return int 0 on success + */ +int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr); + +/** + * @brief Free atbytes on the heap. Should be called once atbytes is no longer needed + * + * @param atbytes the atbytes to free + */ +void atclient_atbytes_free(atclient_atbytes *atbytes); + +#endif diff --git a/packages/atclient/include/atclient/atstr.h b/packages/atclient/include/atclient/atstr.h new file mode 100644 index 00000000..ddda4a55 --- /dev/null +++ b/packages/atclient/include/atclient/atstr.h @@ -0,0 +1,95 @@ +#ifndef ATCLIENT_ATSTR_H +#define ATCLIENT_ATSTR_H + +#include + +/** + * @brief Represents a string that is allocated on the heap + */ +typedef struct atclient_atstr { + size_t size; // buffer length + char *str; // string + size_t len; // output length (length of string) +} atclient_atstr; + +/** + * @brief Initialize an atstr + * + * @param atstr pointer to atstr to initialize + * @param buffersize length of buffer to allocate in bytes (recommended to use a number that is a power of 2). the + * buffersize is the length of the buffer generated. we do not add 1 for null terminator. + */ +void atclient_atstr_init(atclient_atstr *atstr, const size_t buffersize); + +/** + * @brief initialize an atstr with a literal string + * + * @param atstr the atstr struct to populate + * @param str the string to set atstr to. best to use string literals here. (or a null-terminated string) + */ +int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t buffersize, const char *format, ...); + +/** + * @brief set an atstr to an empty string + * + * @param atstr the atstr struct to reset + */ +void atclient_atstr_reset(atclient_atstr *atstr); + +/** + * @brief set an atstr to a string + * + * @param atstr the atstr struct to populate + * @param str the string to set atstr to + * @param size the length of the string + */ +int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t size); + +/** + * @brief Set an atstr to a literal string, assumed to be null-terminated + * + * @param atstr atstr struct to populate + * @param str null-terminated string to set atstr to. Best to use string literals here + */ +int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...); + +/** + * @brief Copy what is in an atstr to another atstr + * + * @param atstr pointer that is being overwritten + * @param data pointer that is being copied from + */ +int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data); + +/** + * @brief Free an atstr + * + * @param atstr pointer to atstr to free from the heap + */ +void atclient_atstr_free(atclient_atstr *atstr); + +/** + * @brief Copy what is in original to substring and then set substring to a substring of original + * + * @param substring the atstr to set to the substring. Assumed that this is already initialized (via + * atclient_atstr_init) + * @param original the atstr to get the substring from. Assumed that this is already initialized (via + * atclient_atstr_init) + * @param start the start index of the substring + * @param end the end index of the substring + * @return int 0 on success, non-zero on failure + */ +int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, + const size_t end); + +/** + * @brief Append a string to an atstr + * + * @param atstr the atstr to append to + * @param format the format of the string to append + * @param ... the arguments to format + * @return int 0 on success + */ +int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...); + +#endif From 2d5a84d9f08a31b1f23b6f284f982590bee7151b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:12:49 -0400 Subject: [PATCH 033/193] revert: "chore: delete atbytes.c and atstr.c" This reverts commit 23fb4126a94d52cbe2c242c4c178c48f7123b9c2. --- packages/atclient/CMakeLists.txt | 2 + packages/atclient/src/atbytes.c | 62 ++++++++++++++++ packages/atclient/src/atstr.c | 123 +++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 packages/atclient/src/atbytes.c create mode 100644 packages/atclient/src/atstr.c diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 895fa765..cfd2d8c5 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -5,6 +5,7 @@ option(ATCLIENT_BUILD_TESTS "Build tests for atclient" OFF) set(ATCLIENT_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include) set( ATCLIENT_SOURCES + ${CMAKE_CURRENT_LIST_DIR}/src/atbytes.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_publickey.c @@ -17,6 +18,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c ${CMAKE_CURRENT_LIST_DIR}/src/atsign.c + ${CMAKE_CURRENT_LIST_DIR}/src/atstr.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c diff --git a/packages/atclient/src/atbytes.c b/packages/atclient/src/atbytes.c new file mode 100644 index 00000000..c0605e4d --- /dev/null +++ b/packages/atclient/src/atbytes.c @@ -0,0 +1,62 @@ +#include "atclient/atbytes.h" +#include "atclient/atstr.h" +#include "atlogger/atlogger.h" +#include +#include +#include + +#define TAG "atbytes" + +void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen) { + memset(atbytes, 0, sizeof(atclient_atbytes)); + atbytes->size = atbyteslen; + atbytes->bytes = malloc(sizeof(unsigned char) * atbytes->size); + atbytes->len = 0; +} + +void atclient_atbytes_reset(atclient_atbytes *atbytes) { + memset(atbytes->bytes, 0, sizeof(unsigned char) * atbytes->size); + atbytes->len = 0; +} + +int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen) { + int ret = 1; + if (byteslen > atbytes->size) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "byteslen is greater than atbyteslen. byteslen: %lu, atbyteslen: %lu\n", byteslen, + atbytes->size); + ret = 1; + goto exit; + } + memcpy(atbytes->bytes, bytes, byteslen); + atbytes->len = byteslen; + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen) { + int ret = 1; + if (strlen > atbytes->size) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "strlen is greater than atbyteslen. strlen: %lu, atbyteslen: %lu\n", strlen, atbytes->size); + ret = 1; + goto exit; + } + memcpy(atbytes->bytes, (unsigned char *)str, strlen); + atbytes->len = strlen; + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr) { + int ret = atclient_atbytes_convert(atbytes, atstr.str, atstr.len); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atbytes_convert failed"); + goto exit; + } +exit: { return ret; } +} + +void atclient_atbytes_free(atclient_atbytes *atbytes) { free(atbytes->bytes); } diff --git a/packages/atclient/src/atstr.c b/packages/atclient/src/atstr.c new file mode 100644 index 00000000..f4fc71ad --- /dev/null +++ b/packages/atclient/src/atstr.c @@ -0,0 +1,123 @@ +#include "atclient/atstr.h" +#include "atlogger/atlogger.h" +#include +#include +#include +#include +#include + +#define TAG "atstr" + +void atclient_atstr_init(atclient_atstr *atstr, const size_t bufferlen) { + memset(atstr, 0, sizeof(atclient_atstr)); + atstr->size = bufferlen; + atstr->str = (char *)malloc(sizeof(char) * atstr->size); + memset(atstr->str, 0, sizeof(char) * atstr->size); + atstr->len = 0; +} + +int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t bufferlen, const char *format, ...) { + int ret = 1; + atclient_atstr_init(atstr, bufferlen); + ret = atclient_atstr_set_literal(atstr, format); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed"); + goto exit; + } + goto exit; +exit: { return ret; } +} + +void atclient_atstr_reset(atclient_atstr *atstr) { + memset(atstr->str, 0, atstr->size); + atstr->len = 0; +} + +int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...) { + int ret = 1; + if (atstr->str == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atstr->str is NULL\n"); + goto exit; + } + va_list args; + va_start(args, format); + ret = vsnprintf(atstr->str, atstr->size, format, args); + va_end(args); // Add va_end() to properly handle variadic arguments + if (ret < 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); + goto exit; + } + atstr->len = ret; + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t len) { + int ret = 1; + + if (len > atstr->size) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "len > atstr->len (%d > %d)\n", len, atstr->size); + goto exit; + } + + memcpy(atstr->str, str, len); + atstr->len = len; + + ret = 0; + goto exit; + +exit: { return ret; } +} + +int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data) { + int ret = 1; + ret = atclient_atstr_set(atstr, data->str, data->len); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed"); + goto exit; + } + goto exit; +exit: { return ret; } +} + +int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, + const size_t end) { + int ret = 1; + if (start > original.len || end > original.len) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start or end is greater than original.len\n"); + goto exit; + } + if (start > end) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start is greater than end\n"); + goto exit; + } + if (end - start > substring->size) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "end - start > substring->len\n"); + goto exit; + } + memcpy(substring->str, original.str + start, end - start); + substring->len = end - start; + ret = 0; + goto exit; + +exit: { return ret; } +} + +int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...) { + int ret = 1; + va_list args; + va_start(args, format); + ret = vsnprintf(atstr->str + atstr->len, atstr->size - atstr->len, format, args); + if (ret < 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); + goto exit; + } + atstr->len += ret; + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_atstr_free(atclient_atstr *atstr) { free(atstr->str); } From 5fffb47fe7ebda539115d17248ec54e7ae01a4d3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:14:56 -0400 Subject: [PATCH 034/193] chore: remove unused atkeysfile function --- .../atclient/include/atclient/atkeysfile.h | 1 - packages/atclient/src/atkeysfile.c | 90 ------------------- 2 files changed, 91 deletions(-) diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h index ce74e6a6..7395fb7f 100644 --- a/packages/atclient/include/atclient/atkeysfile.h +++ b/packages/atclient/include/atclient/atkeysfile.h @@ -14,7 +14,6 @@ typedef struct atclient_atkeysfile { void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile); int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path); -int atclient_atkeysfile_write(const atclient_atkeysfile *atkeysfile, const char *path, const char *atsign); void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile); #endif diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c index 3c7c0b8d..49cd7b75 100644 --- a/packages/atclient/src/atkeysfile.c +++ b/packages/atclient/src/atkeysfile.c @@ -136,96 +136,6 @@ exit: { } } -int atclient_atkeysfile_write(const atclient_atkeysfile *atkeysfile, const char *path, const char *atsign) { - int ret = 1; - - // guarantee that all values are null terminated and are of correct length - atclient_atstr aespkampublickey; - atclient_atstr_init(&aespkampublickey, atkeysfile->aespkampublickeystr.len + 1); - - atclient_atstr aespkamprivatekey; - atclient_atstr_init(&aespkamprivatekey, atkeysfile->aespkamprivatekeystr.len + 1); - - atclient_atstr aesencryptprivatekey; - atclient_atstr_init(&aesencryptprivatekey, atkeysfile->aesencryptprivatekeystr.len + 1); - - atclient_atstr aesencryptpublickey; - atclient_atstr_init(&aesencryptpublickey, atkeysfile->aesencryptpublickeystr.len + 1); - - atclient_atstr selfencryptionkey; - atclient_atstr_init(&selfencryptionkey, atkeysfile->selfencryptionkeystr.len + 1); - - // create cJSON object - cJSON *root = cJSON_CreateObject(); - cJSON_AddStringToObject(root, "aesPkamPrivateKey", aespkamprivatekey.str); - cJSON_AddStringToObject(root, "aesPkamPublicKey", aespkampublickey.str); - cJSON_AddStringToObject(root, "aesEncryptPrivateKey", aesencryptprivatekey.str); - cJSON_AddStringToObject(root, "aesEncryptPublicKey", aesencryptpublickey.str); - cJSON_AddStringToObject(root, "selfEncryptionKey", selfencryptionkey.str); - - ret = atclient_atstr_set(&aespkampublickey, atkeysfile->aespkampublickeystr.str, atkeysfile->aespkampublickeystr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aespkampublickeystr\n", - ret); - goto exit; - } - - ret = atclient_atstr_set(&aespkamprivatekey, atkeysfile->aespkamprivatekeystr.str, - atkeysfile->aespkamprivatekeystr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aespkamprivatekeystr\n", - ret); - goto exit; - } - - ret = atclient_atstr_set(&aesencryptprivatekey, atkeysfile->aesencryptprivatekeystr.str, - atkeysfile->aesencryptprivatekeystr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aesencryptprivatekeystr\n", - ret); - goto exit; - } - - ret = atclient_atstr_set(&aesencryptpublickey, atkeysfile->aesencryptpublickeystr.str, - atkeysfile->aesencryptpublickeystr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aesencryptpublickeystr\n", - ret); - goto exit; - } - - ret = atclient_atstr_set(&selfencryptionkey, atkeysfile->selfencryptionkeystr.str, - atkeysfile->selfencryptionkeystr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set selfencryptionkeystr\n", - ret); - goto exit; - } - - // check that atkeysfile has populated values - if (atkeysfile->aespkamprivatekeystr.len == 0 || atkeysfile->aespkampublickeystr.len == 0 || - atkeysfile->aesencryptprivatekeystr.len == 0 || atkeysfile->aesencryptpublickeystr.len == 0 || - atkeysfile->selfencryptionkeystr.len == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeysfile has not been populated with values\n"); - ret = 1; - goto exit; - } - - ret = 0; - - goto exit; - -exit: { - atclient_atstr_free(&aespkamprivatekey); - atclient_atstr_free(&aespkampublickey); - atclient_atstr_free(&aesencryptprivatekey); - atclient_atstr_free(&aesencryptpublickey); - atclient_atstr_free(&selfencryptionkey); - cJSON_Delete(root); - return ret; -} -} - void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile) { atclient_atstr_free(&(atkeysfile->aespkamprivatekeystr)); atclient_atstr_free(&(atkeysfile->aespkampublickeystr)); From 2582759721300076e4bfcfca7cd2df8f12cd1c08 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:15:17 -0400 Subject: [PATCH 035/193] refactor: metadata.h string to char * --- packages/atclient/include/atclient/metadata.h | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 82120c64..3bdf1abc 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -98,7 +98,7 @@ typedef struct atclient_atkey_metadata { // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr status; + char *status; // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. @@ -109,29 +109,29 @@ typedef struct atclient_atkey_metadata { // This field is derived from the [ttl] value. If the ttl value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr expiresat; + char *expiresat; // Date and time representing when the atkey will be available at (in UTC date/time format). // This field is derived from the [ttb] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr availableat; + char *availableat; // Date and time repreesnts when the atkey will refresh at (in UTC date/time format). // This field is derived from the [ttr] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr refreshat; + char *refreshat; // Date and time representing when the atkey was created at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // this field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr createdat; + char *createdat; // Date and time representing when the atkey was last updated at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr updatedat; // date and time representing when the key was last updated, read only + char *updatedat; // date and time representing when the key was last updated, read only // ispublic=true means this key is accessible by all atSigns and contains non-encrypted data. // ispublic=false means this key is only accessible by either sharedWith or sharedBy @@ -194,18 +194,18 @@ typedef struct atclient_atkey_metadata { // the data came from the owner of the public/private keypair // This field is read from protocol string and can also be set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr datasignature; + char *datasignature; // Represents the status of the shared key. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr sharedkeystatus; + char *sharedkeystatus; // Stores the sharedkey that the data is encrypted with. This is only set if sharedWith is set. The contents will be // encrypted using the public key of the sharedWith atSign. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr sharedkeyenc; + char *sharedkeyenc; // Regarding the following 2 fields... // The pubkey pair stores the hash of the encryption public key used to encrypt the [sharedKeyEnc]. The hash is used @@ -216,17 +216,17 @@ typedef struct atclient_atkey_metadata { // The hash of the public key used to encrypt the [sharedKeyEnc]. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr pubkeyhash; + char *pubkeyhash; // The algorithm used to hash the public key used to encrypt the [sharedKeyEnc] (e.g. "sha256" or "sha512") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr pubkeyalgo; + char *pubkeyalgo; // The type of encoding the value is (e.g. "base64") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr encoding; + char *encoding; // The name of the key used to encrypt the value. If not provided, use sharedKeyEnc in the metadata. If sharedKeyEnc // is not provided, use the default shared key. If enckeyname is provided, just the key name must be provided example @@ -234,28 +234,28 @@ typedef struct atclient_atkey_metadata { // must be only be 'shared_key.wavi' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr enckeyname; + char *enckeyname; // The name of the algorithm used to encrypt the value. For data, the default algorithm is 'AES/SIC/PKCS7Padding', for // cryptographic keys, the default algorithm is 'RSA' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr encalgo; + char *encalgo; // The initialization vector or nonce used when the data was encrypted with the shared symmetric encryption key // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr ivnonce; + char *ivnonce; // TODO: info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr skeenckeyname; + char *skeenckeyname; // TODO: info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr skeencalgo; + char *skeencalgo; // Holds the metadata fields that have not been initialized (0) or have been initialized (1) uint8_t initializedfields[4]; From 5600a47e887230cefbe80a621583a57f4777a4f5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:15:31 -0400 Subject: [PATCH 036/193] refactor: use char * and remove atstr in atclient_delete.c --- packages/atclient/src/atclient_delete.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index f0f5065b..19d74641 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -12,10 +12,14 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { int ret = 1; - atclient_atstr cmdbuffer; - atclient_atstr_init_literal(&cmdbuffer, ATCLIENT_ATKEY_FULL_LEN + strlen("delete:"), "delete:"); + size_t pos = 0; + + const size_t cmdbuffersize = strlen("delete:") + ATCLIENT_ATKEY_FULL_LEN + strlen("\r\n") + 1; + char cmdbuffer[cmdbuffersize]; + memset(cmdbuffer, 0, sizeof(char) * (cmdbuffersize)); char *atkeystr = NULL; + size_t atkeystrlen = 0; const size_t recvsize = 4096; unsigned char *recv; @@ -25,20 +29,20 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { } size_t recvlen = 0; + snprintf(cmdbuffer + pos, cmdbuffersize - pos, "delete:"); + pos += strlen("delete:"); + ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + atkeystrlen = strlen(atkeystr); - ret = atclient_atstr_append(&cmdbuffer, "%.*s\n", (int)strlen(atkeystr), atkeystr); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_append: %d\n", ret); - goto exit; - } + snprintf(cmdbuffer + pos, cmdbuffersize - pos, "%.*s\r\n", (int)atkeystrlen, atkeystr); - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer.str, cmdbuffer.len, recv, - 4096, &recvlen); + ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, + recvsize, &recvlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -59,7 +63,6 @@ exit: { if (!atclient->async_read) { free(recv); } - atclient_atstr_free(&cmdbuffer); free(atkeystr); return ret; } From 2890a0f4665d83dadfacd48462456d24b06374a3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:17:21 -0400 Subject: [PATCH 037/193] revert: "refactor: metadata.h string to char *" This reverts commit 2582759721300076e4bfcfca7cd2df8f12cd1c08. --- packages/atclient/include/atclient/metadata.h | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 3bdf1abc..82120c64 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -98,7 +98,7 @@ typedef struct atclient_atkey_metadata { // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *status; + atclient_atstr status; // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. @@ -109,29 +109,29 @@ typedef struct atclient_atkey_metadata { // This field is derived from the [ttl] value. If the ttl value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *expiresat; + atclient_atstr expiresat; // Date and time representing when the atkey will be available at (in UTC date/time format). // This field is derived from the [ttb] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *availableat; + atclient_atstr availableat; // Date and time repreesnts when the atkey will refresh at (in UTC date/time format). // This field is derived from the [ttr] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *refreshat; + atclient_atstr refreshat; // Date and time representing when the atkey was created at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // this field is read by the protocol and populated by the SDK for the developer to read from only. - char *createdat; + atclient_atstr createdat; // Date and time representing when the atkey was last updated at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *updatedat; // date and time representing when the key was last updated, read only + atclient_atstr updatedat; // date and time representing when the key was last updated, read only // ispublic=true means this key is accessible by all atSigns and contains non-encrypted data. // ispublic=false means this key is only accessible by either sharedWith or sharedBy @@ -194,18 +194,18 @@ typedef struct atclient_atkey_metadata { // the data came from the owner of the public/private keypair // This field is read from protocol string and can also be set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *datasignature; + atclient_atstr datasignature; // Represents the status of the shared key. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *sharedkeystatus; + atclient_atstr sharedkeystatus; // Stores the sharedkey that the data is encrypted with. This is only set if sharedWith is set. The contents will be // encrypted using the public key of the sharedWith atSign. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *sharedkeyenc; + atclient_atstr sharedkeyenc; // Regarding the following 2 fields... // The pubkey pair stores the hash of the encryption public key used to encrypt the [sharedKeyEnc]. The hash is used @@ -216,17 +216,17 @@ typedef struct atclient_atkey_metadata { // The hash of the public key used to encrypt the [sharedKeyEnc]. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *pubkeyhash; + atclient_atstr pubkeyhash; // The algorithm used to hash the public key used to encrypt the [sharedKeyEnc] (e.g. "sha256" or "sha512") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *pubkeyalgo; + atclient_atstr pubkeyalgo; // The type of encoding the value is (e.g. "base64") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *encoding; + atclient_atstr encoding; // The name of the key used to encrypt the value. If not provided, use sharedKeyEnc in the metadata. If sharedKeyEnc // is not provided, use the default shared key. If enckeyname is provided, just the key name must be provided example @@ -234,28 +234,28 @@ typedef struct atclient_atkey_metadata { // must be only be 'shared_key.wavi' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *enckeyname; + atclient_atstr enckeyname; // The name of the algorithm used to encrypt the value. For data, the default algorithm is 'AES/SIC/PKCS7Padding', for // cryptographic keys, the default algorithm is 'RSA' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *encalgo; + atclient_atstr encalgo; // The initialization vector or nonce used when the data was encrypted with the shared symmetric encryption key // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *ivnonce; + atclient_atstr ivnonce; // TODO: info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *skeenckeyname; + atclient_atstr skeenckeyname; // TODO: info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *skeencalgo; + atclient_atstr skeencalgo; // Holds the metadata fields that have not been initialized (0) or have been initialized (1) uint8_t initializedfields[4]; From 69412b7a0b0a30a8068c5e08278388b5cf189ca0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:26:15 -0400 Subject: [PATCH 038/193] fix: atclient_delete --- packages/atclient/src/atclient_delete.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 19d74641..27cbe153 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -14,9 +14,8 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { size_t pos = 0; - const size_t cmdbuffersize = strlen("delete:") + ATCLIENT_ATKEY_FULL_LEN + strlen("\r\n") + 1; - char cmdbuffer[cmdbuffersize]; - memset(cmdbuffer, 0, sizeof(char) * (cmdbuffersize)); + size_t cmdbuffersize; + char *cmdbuffer = NULL; char *atkeystr = NULL; size_t atkeystrlen = 0; @@ -29,8 +28,6 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { } size_t recvlen = 0; - snprintf(cmdbuffer + pos, cmdbuffersize - pos, "delete:"); - pos += strlen("delete:"); ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { @@ -39,7 +36,9 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { } atkeystrlen = strlen(atkeystr); - snprintf(cmdbuffer + pos, cmdbuffersize - pos, "%.*s\r\n", (int)atkeystrlen, atkeystr); + cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; + cmdbuffer = malloc(sizeof(char) * cmdbuffersize); + snprintf(cmdbuffer, cmdbuffersize, "delete:%.*s\r\n", (int)atkeystrlen, atkeystr); ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, recvsize, &recvlen); @@ -64,6 +63,7 @@ exit: { free(recv); } free(atkeystr); + free(cmdbuffer); return ret; } } From 8c66fa4358bccaab892e7473138c0118818ee798 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 12 Jul 2024 16:37:31 -0400 Subject: [PATCH 039/193] chore: clean up atclient_delete.c --- packages/atclient/src/atclient_delete.c | 59 +++++++++++++++++++++---- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 27cbe153..31a71aeb 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -9,15 +9,27 @@ #define TAG "atclient_delete" +static int atclient_delete_validate_arguments(atclient *atclient, const atclient_atkey *atkey); + int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { int ret = 1; - size_t pos = 0; + /* + * 1. Check arguments + */ + if ((ret = atclient_delete_validate_arguments(atclient, atkey)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete_validate_arguments: %d\n", ret); + goto exit; + } + /* + * 2. Initialize variables + */ size_t cmdbuffersize; - char *cmdbuffer = NULL; + char *cmdbuffer = NULL; // free later - char *atkeystr = NULL; + char *atkeystr = NULL; // free later size_t atkeystrlen = 0; const size_t recvsize = 4096; @@ -28,9 +40,11 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { } size_t recvlen = 0; + /* + * 3. Build delete command + */ - ret = atclient_atkey_to_string(atkey, &atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } @@ -40,12 +54,17 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { cmdbuffer = malloc(sizeof(char) * cmdbuffersize); snprintf(cmdbuffer, cmdbuffersize, "delete:%.*s\r\n", (int)atkeystrlen, atkeystr); - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, - recvsize, &recvlen); - if (ret != 0) { + /* + * 4. Send command + */ + + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, + recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; - } else if (atclient->async_read) { + } + + if (atclient->async_read) { goto exit; } @@ -67,3 +86,25 @@ exit: { return ret; } } + +static int atclient_delete_validate_arguments(atclient *atclient, const atclient_atkey *atkey) { + int ret = 1; + + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + // TODO use a function in atclient_atkey to check if atkey is complete + + ret = 0; + goto exit; +exit: { return ret; } +} \ No newline at end of file From 8f48b8014a51c567e7d648aee6c44639670eb448 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 15 Jul 2024 13:25:53 -0400 Subject: [PATCH 040/193] feat: handle malloc == null --- packages/atchops/src/aesctr.c | 19 +- packages/atchops/src/rsakey.c | 34 ++- packages/atchops/tests/test_aesctr_decrypt.c | 4 + packages/atclient/include/atclient/monitor.h | 32 +-- packages/atclient/src/atbytes.c | 2 +- packages/atclient/src/atclient.c | 5 + packages/atclient/src/atclient_delete.c | 10 + packages/atclient/src/atclient_get_atkeys.c | 116 +++++--- .../atclient/src/atclient_get_publickey.c | 5 + packages/atclient/src/atclient_get_selfkey.c | 10 + .../atclient/src/atclient_get_sharedkey.c | 30 +++ packages/atclient/src/atclient_put.c | 10 + packages/atclient/src/atsign.c | 5 + packages/atclient/src/connection.c | 2 +- .../atclient/src/encryption_key_helpers.c | 11 +- packages/atclient/src/metadata.c | 5 +- packages/atclient/src/monitor.c | 247 ++++++++++++++---- 17 files changed, 424 insertions(+), 123 deletions(-) diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c index f9a98e87..aa200ed8 100644 --- a/packages/atchops/src/aesctr.c +++ b/packages/atchops/src/aesctr.c @@ -5,8 +5,10 @@ #include #include #include +#include +#include -#include // TODO remove +#define TAG "atchops_aesctr" int atchops_aesctr_encrypt(const unsigned char *key, const enum atchops_aes_size keybits, unsigned char *iv, const unsigned char *plaintext, // plaintext to encrypt @@ -32,6 +34,11 @@ int atchops_aesctr_encrypt(const unsigned char *key, const enum atchops_aes_size // printf("plaintext_paddedlen: %lu = %d + %d\n", plaintextpaddedlen, plaintextlen, numpadbytestoadd); plaintextpadded = malloc(sizeof(unsigned char) * (plaintextpaddedlen + 1)); + if(plaintextpadded == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); + goto exit; + } memcpy(plaintextpadded, plaintext, plaintextlen); memset(plaintextpadded + plaintextlen, padval, numpadbytestoadd); plaintextpadded[plaintextpaddedlen] = '\0'; @@ -73,10 +80,20 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size size_t nc_off = 0; unsigned char *stream_block = malloc(sizeof(unsigned char) * 16); + if(stream_block == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for stream_block\n"); + goto exit; + } memset(stream_block, 0, sizeof(unsigned char) * 16); size_t plaintextpaddedsize = plaintextsize + 16; unsigned char *plaintextpadded = malloc(sizeof(unsigned char) * plaintextpaddedsize); + if(plaintextpadded == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); + goto exit; + } memset(plaintextpadded, 0, plaintextpaddedsize); size_t plaintextpaddedlen = 0; diff --git a/packages/atchops/src/rsakey.c b/packages/atchops/src/rsakey.c index ad74c49b..1d9d07fd 100644 --- a/packages/atchops/src/rsakey.c +++ b/packages/atchops/src/rsakey.c @@ -11,21 +11,21 @@ void atchops_rsakey_publickey_init(atchops_rsakey_publickey *publickey) { memset(publickey, 0, sizeof(atchops_rsakey_publickey)); publickey->n.len = BASE64_DECODED_KEY_BUFFER_SIZE; - publickey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->n.len); + publickey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->n.len); // TODO handle null publickey->e.len = BASE64_DECODED_KEY_BUFFER_SIZE; - publickey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->e.len); + publickey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->e.len); // TODO handle null } void atchops_rsakey_publickey_clone(atchops_rsakey_publickey *dst, atchops_rsakey_publickey *src) { memset(dst, 0, sizeof(atchops_rsakey_publickey)); dst->n.len = src->n.len; - dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); + dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); // TODO handle null memcpy(dst->n.value, src->n.value, dst->n.len); dst->e.len = src->e.len; - dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); + dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); // TODO handle null memcpy(dst->e.value, src->e.value, dst->e.len); } @@ -38,16 +38,16 @@ void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey) { memset(privatekey, 0, sizeof(atchops_rsakey_privatekey)); privatekey->n.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->n.value = malloc(sizeof(unsigned char) * privatekey->n.len); + privatekey->n.value = malloc(sizeof(unsigned char) * privatekey->n.len); // TODO handle null privatekey->e.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->e.value = malloc(sizeof(unsigned char) * privatekey->e.len); + privatekey->e.value = malloc(sizeof(unsigned char) * privatekey->e.len); // TODO handle null privatekey->d.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->d.value = malloc(sizeof(unsigned char) * privatekey->d.len); + privatekey->d.value = malloc(sizeof(unsigned char) * privatekey->d.len); // TODO handle null privatekey->p.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->p.value = malloc(sizeof(unsigned char) * privatekey->p.len); + privatekey->p.value = malloc(sizeof(unsigned char) * privatekey->p.len); // TODO handle null privatekey->q.len = BASE64_DECODED_KEY_BUFFER_SIZE; privatekey->q.value = malloc(sizeof(unsigned char) * privatekey->q.len); @@ -56,23 +56,23 @@ void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey) { void atchops_rsakey_privatekey_clone(atchops_rsakey_privatekey *dst, atchops_rsakey_privatekey *src) { memset(dst, 0, sizeof(atchops_rsakey_privatekey)); dst->n.len = src->n.len; - dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); + dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); // TODO handle null memcpy(dst->n.value, src->n.value, dst->n.len); dst->e.len = src->e.len; - dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); + dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); // TODO handle null memcpy(dst->e.value, src->e.value, dst->e.len); dst->d.len = src->d.len; - dst->d.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->d.len); + dst->d.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->d.len); // TODO handle null memcpy(dst->d.value, src->d.value, dst->d.len); dst->p.len = src->p.len; - dst->p.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->p.len); + dst->p.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->p.len); // TODO handle null memcpy(dst->p.value, src->p.value, dst->p.len); dst->q.len = src->q.len; - dst->q.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->q.len); + dst->q.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->q.len); // TODO handle null memcpy(dst->q.value, src->q.value, dst->q.len); } @@ -126,6 +126,10 @@ int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickey, const } seq = malloc(sizeof(mbedtls_asn1_sequence)); + if(seq == NULL) { + ret = 1; + goto exit; + } memset(seq, 0, sizeof(mbedtls_asn1_sequence)); ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER); if (ret != 0) { @@ -193,6 +197,10 @@ int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekey, co } seq = malloc(sizeof(mbedtls_asn1_sequence)); + if(seq == NULL) { + ret = 1; + goto exit; + } memset(seq, 0, sizeof(mbedtls_asn1_sequence)); ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER); if (ret != 0) { diff --git a/packages/atchops/tests/test_aesctr_decrypt.c b/packages/atchops/tests/test_aesctr_decrypt.c index 91211b5b..77afbb72 100644 --- a/packages/atchops/tests/test_aesctr_decrypt.c +++ b/packages/atchops/tests/test_aesctr_decrypt.c @@ -47,6 +47,10 @@ int main() { size_t plaintextlen = 0; unsigned char *iv = malloc(sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + if(iv == NULL) { + printf("malloc (failed): %d\n", ret); + goto exit; + } memset(iv, 0, ATCHOPS_IV_BUFFER_SIZE); // keys in the atKeys file are encrypted with AES with IV {0} * 16 const size_t ciphertextsize = 8192; diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 469b2ad5..75caa760 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -131,30 +131,30 @@ void atclient_atnotification_free_decryptedvaluelen(atclient_atnotification *not // Setters for the fields, these functions check if the field is initialized before setting the value (and overwrites if // it is) -void atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen); -void atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen); -void atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen); -void atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); -void atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); -void atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen); +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen); +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen); +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, const size_t operationlen); -void atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis); -void atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, +int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis); +int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, const size_t messageTypelen); -void atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted); -void atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, +int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted); +int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, const size_t encKeyNamelen); -void atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, +int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, const size_t encAlgolen); -void atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, +int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, const size_t ivNoncelen); -void atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, +int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, const size_t skeEncKeyNamelen); -void atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, +int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, const size_t skeEncAlgolen); -void atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, +int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const unsigned char *decryptedvalue, const size_t decryptedvaluelen); -void atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, +int atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, const size_t decryptedvaluelen); /** diff --git a/packages/atclient/src/atbytes.c b/packages/atclient/src/atbytes.c index c0605e4d..bd39b74e 100644 --- a/packages/atclient/src/atbytes.c +++ b/packages/atclient/src/atbytes.c @@ -10,7 +10,7 @@ void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen) { memset(atbytes, 0, sizeof(atclient_atbytes)); atbytes->size = atbyteslen; - atbytes->bytes = malloc(sizeof(unsigned char) * atbytes->size); + atbytes->bytes = malloc(sizeof(unsigned char) * atbytes->size); // TODO handle null atbytes->len = 0; } diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 766969e0..77f7a3a1 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -179,6 +179,11 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { const size_t recvsize = 64; if (!heartbeat_conn->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); + if(recv == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + ret = 1; + goto exit; + } memset(recv, 0, sizeof(unsigned char) * recvsize); } size_t recvlen = 0; diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 31a71aeb..4cc1eb6e 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -36,6 +36,11 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { unsigned char *recv; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); + if(recv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + goto exit; + } memset(recv, 0, sizeof(unsigned char) * recvsize); } size_t recvlen = 0; @@ -52,6 +57,11 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; cmdbuffer = malloc(sizeof(char) * cmdbuffersize); + if(cmdbuffer == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + goto exit; + } snprintf(cmdbuffer, cmdbuffersize, "delete:%.*s\r\n", (int)atkeystrlen, atkeystr); /* diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 08f380c7..a084f42b 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -1,71 +1,65 @@ #include #include #include -#include #include #include +#include #define TAG "atclient_get_atkeys" +static int atclient_get_atkeys_validate_arguments(atclient *atclient, const char *regex, const bool showhidden, + atclient_atkey **atkey, size_t *output_array_len); + int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhidden, const size_t recvbuffersize, atclient_atkey **atkey, size_t *output_array_len) { int ret = 1; - // check to make sure null ptr wasn't provided - if (atkey == NULL || output_array_len == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey or output_array_len is NULL. These should be pointers\n"); - return ret; - } - - // check to make sure atclient is not null - if(atclient == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - return ret; - } - - if(!atclient->_atserver_connection_started) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); - return ret; - } - - if(!atclient->_atsign_is_allocated) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not allocated. Make sure to PKAM authenticate first\n"); + /* + * 1. Validate arguments + */ + if ((ret = atclient_get_atkeys_validate_arguments(atclient, regex, showhidden, atkey, output_array_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_validate_arguments: %d\n", ret); return ret; } - if(regex == NULL) { + if (regex == NULL) { regex = ""; } - // scan:showHidden:true - const size_t scan_command_len = strlen("scan") + (showhidden ? strlen(":showHidden:true") : 0) + - (strlen(regex) > 0 ? (strlen(" ") + strlen(regex)) : 0) + strlen("\r\n") + 1; - char scan_command[scan_command_len]; - snprintf(scan_command, scan_command_len, "scan%s%s%s\r\n", showhidden ? ":showHidden:true" : "", - strlen(regex) > 0 ? " " : "", regex); + /* + * 2. Variables + */ + const size_t scancmdsize = strlen("scan") + (showhidden ? strlen(":showHidden:true") : 0) + + (strlen(regex) > 0 ? (strlen(" ") + strlen(regex)) : 0) + strlen("\r\n") + 1; + char scancmd[scancmdsize]; const unsigned char recv[recvbuffersize]; size_t recvlen = 0; - cJSON *root = NULL; + cJSON *root = NULL; // free later - // 1. send scan command - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scan_command, - scan_command_len - 1, recv, recvbuffersize, &recvlen)) != 0) { + /* + * 3. Build scan command + */ + snprintf(scancmd, scancmdsize, "scan%s%s%s\r\n", showhidden ? ":showHidden:true" : "", strlen(regex) > 0 ? " " : "", + regex); + + /* + * 4. Send scan command + */ + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scancmd, scancmdsize - 1, recv, + recvbuffersize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - // log recevied bytes atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv was %lu bytes long\n", recvlen); - // 2. parse response + /* + * 5. Parse response + */ if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -82,8 +76,6 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi goto exit; } - // 3. populate atkey array - // root looks likes ["atkey1", "atkey2", ...] if (!cJSON_IsArray(root)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "root is not an array\n"); @@ -91,15 +83,15 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi } *output_array_len = cJSON_GetArraySize(root); - *atkey = malloc(sizeof(atclient_atkey) * *output_array_len); + *atkey = malloc(sizeof(atclient_atkey) * (*output_array_len)); if (*atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } - for(size_t i = 0; i < *output_array_len; i++) { - atclient_atkey_init(&(*atkey)[i]); + for (size_t i = 0; i < *output_array_len; i++) { + atclient_atkey_init(&((*atkey)[i])); } for (size_t i = 0; i < *output_array_len; i++) { @@ -126,12 +118,50 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi ret = 0; goto exit; exit1: { - for(size_t i = 0; i < *output_array_len; i++) { + for (size_t i = 0; i < *output_array_len; i++) { atclient_atkey_free(&(*atkey)[i]); } free(*atkey); *atkey = NULL; *output_array_len = 0; } +exit: { + cJSON_Delete(root); + return ret; +} +} + +static int atclient_get_atkeys_validate_arguments(atclient *atclient, const char *regex, const bool showhidden, + atclient_atkey **atkey, size_t *output_array_len) { + int ret = 1; + + // check to make sure null ptr wasn't provided + if (atkey == NULL || output_array_len == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey or output_array_len is NULL. These should be pointers\n"); + goto exit; + } + + // check to make sure atclient is not null + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (!atclient->_atserver_connection_started) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + goto exit; + } + + if (!atclient->_atsign_is_allocated) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not allocated. Make sure to PKAM authenticate first\n"); + goto exit; + } + + ret = 0; + goto exit; exit: { return ret; } } \ No newline at end of file diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index b8034a9f..2be135a5 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -59,6 +59,11 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu const size_t cmdbuffersize = strlen("plookup:all:\r\n") + (bypasscachestr != NULL ? strlen(bypasscachestr) : 0) + strlen(atkeystrwithoutpublic) + 1; cmdbuffer = malloc(sizeof(char) * cmdbuffersize); + if(cmdbuffer == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + goto exit; + } memset(cmdbuffer, 0, cmdbuffersize); snprintf(cmdbuffer, cmdbuffersize, "plookup:%sall:%s\r\n", bypasscachestr != NULL ? bypasscachestr : "", atkeystrwithoutpublic); diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index a4765a9f..c64a8ad6 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -58,6 +58,11 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t cmdbuffersize = strlen("llookup:all:\r\n") + atkeystrlen + 1; cmdbuffer = (char *)malloc(sizeof(char) * cmdbuffersize); + if(cmdbuffer == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + goto exit; + } memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int) atkeystrlen, atkeystr); @@ -140,6 +145,11 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, // holds base64 decoded value. Once decoded, it is encrypted cipher text bytes that need to be decrypted const size_t valuerawsize = atchops_base64_decoded_size(strlen(data->valuestring)); valueraw = (char *)malloc(sizeof(char) * valuerawsize); + if(valueraw == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); + goto exit; + } memset(valueraw, 0, sizeof(char) * valuerawsize); size_t valuerawlen = 0; diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 43c4974e..40497eff 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -81,6 +81,11 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke if (enc_key == NULL) { enc_key_mem = 1; enc_key = malloc(45); + if(enc_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); + goto exit; + } atclient_atsign recipient; ret = atclient_atsign_init(&recipient, atkey->sharedwith.str); if (ret != 0) { @@ -111,6 +116,11 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // command_prefix = "llookup:all:" const size_t commandsize = strlen("llookup:all:") + atkeystrlen + strlen("\r\n") + 1; command = malloc(commandsize * sizeof(char)); + if(command == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + goto exit; + } snprintf(command, commandsize, "llookup:all:%s\r\n", atkeystr); // send command and recv response @@ -132,6 +142,11 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; response_prefix = malloc(response_prefix_len * sizeof(char)); + if(response_prefix == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); + goto exit; + } snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); if (atclient_stringutils_starts_with(response, recvsize, response_prefix, response_prefix_len)) { @@ -199,6 +214,11 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke const size_t valuerawsize = strlen(data->valuestring) * 4; // most likely enough space after base64 decode valueraw = malloc(sizeof(char) * valuerawsize); + if(valueraw == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); + goto exit; + } memset(valueraw, 0, sizeof(char) * valuerawsize); size_t valuerawlen = 0; @@ -254,6 +274,11 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *enc_key = shared_enc_key; if (enc_key == NULL) { enc_key = malloc(45); + if(enc_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); + goto exit; + } atclient_atsign recipient; ret = atclient_atsign_init(&recipient, atkey->sharedby.str); if (ret != 0) { @@ -304,6 +329,11 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; response_prefix = malloc(response_prefix_len * sizeof(char)); + if(response_prefix == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); + goto exit; + } snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); if (atclient_stringutils_starts_with(response, recvlen, response_prefix, response_prefix_len)) { diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 045993a4..db61ce26 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -75,6 +75,11 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c unsigned char *recv = NULL; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); + if(recv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + goto exit; + } memset(recv, 0, sizeof(unsigned char) * recvsize); } size_t recvlen = 0; @@ -195,6 +200,11 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c cmdbuffersize = strlen("update: \r\n") + metadataprotocolstrlen + atkeystrlen + ciphertextbase64len + 1; // + 1 for null terminator cmdbuffer = malloc(sizeof(char) * cmdbuffersize); + if(cmdbuffer == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + goto exit; + } memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); snprintf(cmdbuffer, cmdbuffersize, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadataprotocolstr, (int)atkeystrlen, atkeystr, (int)ciphertextbase64len, ciphertextbase64); diff --git a/packages/atclient/src/atsign.c b/packages/atclient/src/atsign.c index 5cb36286..10a34545 100644 --- a/packages/atclient/src/atsign.c +++ b/packages/atclient/src/atsign.c @@ -21,6 +21,11 @@ int atclient_atsign_init(atclient_atsign *atsign, const char *atsign_str) { memset(atsign, 0, sizeof(atclient_atsign)); atsign->atsign = malloc(strlen(atsign_str) + 1); + if(atsign->atsign == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for atsign->atsign\n"); + return ret; + } size_t atolen = 0; ret = atclient_atsign_with_at_symbol(atsign->atsign, maxatlen, &(atolen), atsign_str, strlen(atsign_str)); diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index c1c6173f..45870792 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -453,7 +453,7 @@ void atclient_connection_free(atclient_connection *ctx) { } void atclient_connection_enable_hooks(atclient_connection *ctx) { - ctx->hooks = malloc(sizeof(atclient_connection_hooks)); + ctx->hooks = malloc(sizeof(atclient_connection_hooks)); // TODO handle malloc failure memset(ctx->hooks, 0, sizeof(atclient_connection_hooks)); ctx->hooks->readonly_src = true; ctx->_is_hooks_enabled = true; diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index 03135e18..a8f18912 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -338,13 +338,22 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie const size_t cmdbuffersize1 = strlen("update:shared_key. \r\n") + strlen(sharedwith->without_prefix_str) + strlen(sharedby->atsign) + 1 + sharedenckeybase64encryptedforuslen; cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1); + if(cmdbuffer1 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer1\n"); + goto exit; + } snprintf(cmdbuffer1, cmdbuffersize1, "update:shared_key.%s%s %s\r\n", sharedwith->without_prefix_str, sharedby->atsign, sharedenckeybase64encryptedforus); // 5b. for them (update:shared_key.sharedwith@sharedby \r\n) const size_t cmdbuffersize2 = strlen("update::shared_key \r\n") + strlen(sharedby->atsign) + strlen(sharedwith->atsign) + 1 + sharedenckeybase64encryptedforthemlen; - cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2); + if((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer2\n"); + goto exit; + } snprintf(cmdbuffer2, cmdbuffersize2, "update:%s:shared_key%s %s\r\n", sharedwith->atsign, sharedby->atsign, sharedenckeybase64encryptedforthem); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index e07eb2fb..929b0ed8 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -743,7 +743,10 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad const size_t expected_metadatastr_len = metadatastrsize - 1; size_t pos = 0; - *metadatastr = malloc(sizeof(char) * metadatastrsize); + if((*metadatastr = malloc(sizeof(char) * metadatastrsize)) == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } memset(*metadatastr, 0, sizeof(char) * metadatastrsize); if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index a3899f57..6866776e 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -362,108 +362,180 @@ void atclient_atnotification_free_decryptedvaluelen(atclient_atnotification *not atclient_atnotification_decryptedvaluelen_set_initialized(notification, false); } -void atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { + int ret = 1; if (atclient_atnotification_id_is_initialized(notification)) { atclient_atnotification_free_id(notification); } notification->id = malloc(sizeof(char) * (idlen + 1)); + if (notification->id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification id\n"); + goto exit; + } memcpy(notification->id, id, idlen); *(notification->id + idlen) = '\0'; atclient_atnotification_id_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { + int ret = 1; if (atclient_atnotification_from_is_initialized(notification)) { atclient_atnotification_free_from(notification); } notification->from = malloc(sizeof(char) * (fromlen + 1)); + if (notification->from == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification from\n"); + goto exit; + } memcpy(notification->from, from, fromlen); *(notification->from + fromlen) = '\0'; atclient_atnotification_from_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { + int ret = 1; if (atclient_atnotification_to_is_initialized(notification)) { atclient_atnotification_free_to(notification); } notification->to = malloc(sizeof(char) * (tolen + 1)); + if (notification->to == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification to\n"); + goto exit; + } memcpy(notification->to, to, tolen); *(notification->to + tolen) = '\0'; atclient_atnotification_to_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { + int ret = 1; if (atclient_atnotification_key_is_initialized(notification)) { atclient_atnotification_free_key(notification); } notification->key = malloc(sizeof(char) * (keylen + 1)); + if (notification->key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification key\n"); + goto exit; + } memcpy(notification->key, key, keylen); *(notification->key + keylen) = '\0'; atclient_atnotification_key_set_initialized(notification, true); + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, - const size_t valuelen) { +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen) { + int ret = 1; if (atclient_atnotification_value_is_initialized(notification)) { atclient_atnotification_free_value(notification); } notification->value = malloc(sizeof(char) * (valuelen + 1)); + if (notification->value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification value\n"); + goto exit; + } memcpy(notification->value, value, valuelen); *(notification->value + valuelen) = '\0'; atclient_atnotification_value_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen) { +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, + const size_t operationlen) { + int ret = 1; if (atclient_atnotification_operation_is_initialized(notification)) { atclient_atnotification_free_operation(notification); } notification->operation = malloc(sizeof(char) * (operationlen + 1)); + if (notification->operation == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification operation\n"); + goto exit; + } memcpy(notification->operation, operation, operationlen); *(notification->operation + operationlen) = '\0'; atclient_atnotification_operation_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis) { +int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis) { if (atclient_atnotification_epochMillis_is_initialized(notification)) { atclient_atnotification_free_epochMillis(notification); } notification->epochMillis = epochMillis; atclient_atnotification_epochMillis_set_initialized(notification, true); + return 0; } -void atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, +int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, const size_t messageTypelen) { + int ret = 1; if (atclient_atnotification_messageType_is_initialized(notification)) { atclient_atnotification_free_messageType(notification); } notification->messageType = malloc(sizeof(char) * (messageTypelen + 1)); + if (notification->messageType == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification messageType\n"); + goto exit; + } memcpy(notification->messageType, messageType, messageTypelen); *(notification->messageType + messageTypelen) = '\0'; atclient_atnotification_messageType_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted) { +int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted) { if (atclient_atnotification_isEncrypted_is_initialized(notification)) { atclient_atnotification_free_isEncrypted(notification); } notification->isEncrypted = isEncrypted; atclient_atnotification_isEncrypted_set_initialized(notification, true); + return 0; } -void atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen) { +int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, + const size_t encKeyNamelen) { + int ret = 1; if (atclient_atnotification_encKeyName_is_initialized(notification)) { atclient_atnotification_free_encKeyName(notification); } notification->encKeyName = malloc(sizeof(char) * (encKeyNamelen + 1)); + if (notification->encKeyName == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification encKeyName\n"); + goto exit; + } memcpy(notification->encKeyName, encKeyName, encKeyNamelen); *(notification->encKeyName + encKeyNamelen) = '\0'; atclient_atnotification_encKeyName_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen) { +int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, + const size_t encAlgolen) { + int ret = 1; if (atclient_atnotification_encAlgo_is_initialized(notification)) { atclient_atnotification_free_encAlgo(notification); } @@ -471,59 +543,102 @@ void atclient_atnotification_set_encAlgo(atclient_atnotification *notification, memcpy(notification->encAlgo, encAlgo, encAlgolen); *(notification->encAlgo + encAlgolen) = '\0'; atclient_atnotification_encAlgo_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen) { +int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, + const size_t ivNoncelen) { + int ret = 1; if (atclient_atnotification_ivNonce_is_initialized(notification)) { atclient_atnotification_free_ivNonce(notification); } notification->ivNonce = malloc(sizeof(char) * (ivNoncelen + 1)); + if (notification->ivNonce == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ivNonce\n"); + goto exit; + } memcpy(notification->ivNonce, ivNonce, ivNoncelen); *(notification->ivNonce + ivNoncelen) = '\0'; atclient_atnotification_ivNonce_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen) { +int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, + const size_t skeEncKeyNamelen) { + int ret = 1; if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { atclient_atnotification_free_skeEncKeyName(notification); } notification->skeEncKeyName = malloc(sizeof(char) * (skeEncKeyNamelen + 1)); + if (notification->skeEncKeyName == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncKeyName\n"); + goto exit; + } memcpy(notification->skeEncKeyName, skeEncKeyName, skeEncKeyNamelen); *(notification->skeEncKeyName + skeEncKeyNamelen) = '\0'; atclient_atnotification_skeEncKeyName_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen) { +int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, + const size_t skeEncAlgolen) { + int ret = 1; if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { atclient_atnotification_free_skeEncAlgo(notification); } notification->skeEncAlgo = malloc(sizeof(char) * (skeEncAlgolen + 1)); + if (notification->skeEncAlgo == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncAlgo\n"); + goto exit; + } memcpy(notification->skeEncAlgo, skeEncAlgo, skeEncAlgolen); *(notification->skeEncAlgo + skeEncAlgolen) = '\0'; atclient_atnotification_skeEncAlgo_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, - const unsigned char *decryptedvalue, const size_t decryptedvaluelen) { +int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, + const unsigned char *decryptedvalue, const size_t decryptedvaluelen) { + int ret = 1; if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { atclient_atnotification_free_decryptedvalue(notification); } notification->decryptedvalue = malloc(sizeof(unsigned char) * (decryptedvaluelen + 1)); + if (notification->decryptedvalue == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification decryptedvalue\n"); + goto exit; + } memcpy(notification->decryptedvalue, decryptedvalue, decryptedvaluelen); notification->decryptedvalue[decryptedvaluelen] = '\0'; atclient_atnotification_decryptedvalue_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } -void atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, - const size_t decryptedvaluelen) { +int atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, + const size_t decryptedvaluelen) { + int ret = 1; if (atclient_atnotification_decryptedvaluelen_is_initialized(notification)) { atclient_atnotification_free_decryptedvaluelen(notification); } notification->decryptedvaluelen = decryptedvaluelen; atclient_atnotification_decryptedvaluelen_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } } void atclient_monitor_message_init(atclient_monitor_message *message) { @@ -615,9 +730,9 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m size_t chunks = 0; const size_t chunksize = ATCLIENT_MONITOR_BUFFER_LEN; - + buffer = malloc(sizeof(char) * chunksize); - if(buffer == NULL) { + if (buffer == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for buffer\n"); goto exit; } @@ -642,7 +757,7 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m } chunks = chunks + 1; } - if(ret <= 0) { // you should reconnect... + if (ret <= 0) { // you should reconnect... message->type = ATCLIENT_MONITOR_ERROR_READ; message->error_read.error_code = ret; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Read nothing from the monitor connection: %d\n", ret); @@ -708,11 +823,9 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } } else { - atclient_atnotification_set_decryptedvalue(&(message->notification), - (unsigned char *)message->notification.value, + atclient_atnotification_set_decryptedvalue(&(message->notification), (unsigned char *)message->notification.value, strlen(message->notification.value)); - atclient_atnotification_set_decryptedvaluelen(&(message->notification), - strlen(message->notification.value)); + atclient_atnotification_set_decryptedvaluelen(&(message->notification), strlen(message->notification.value)); } } else if (strcmp(messagetype, "data") == 0) { message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE; @@ -823,7 +936,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_id(notification, val, vallen); + if((ret = atclient_atnotification_set_id(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification id\n"); + goto exit; + } } cJSON *from = cJSON_GetObjectItem(root, "from"); @@ -835,7 +951,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_from(notification, val, vallen); + if((ret = atclient_atnotification_set_from(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification from\n"); + goto exit; + } } cJSON *to = cJSON_GetObjectItem(root, "to"); @@ -847,7 +966,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_to(notification, val, vallen); + if((ret = atclient_atnotification_set_to(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification to\n"); + goto exit; + } } cJSON *key = cJSON_GetObjectItem(root, "key"); @@ -859,7 +981,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_key(notification, val, vallen); + if((ret = atclient_atnotification_set_key(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification key\n"); + goto exit; + } } cJSON *value = cJSON_GetObjectItem(root, "value"); @@ -871,7 +996,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_value(notification, val, vallen); + if((ret = atclient_atnotification_set_value(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification value\n"); + goto exit; + } } cJSON *operation = cJSON_GetObjectItem(root, "operation"); @@ -883,12 +1011,18 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_operation(notification, val, vallen); + if((ret = atclient_atnotification_set_operation(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification operation\n"); + goto exit; + } } cJSON *epochMillis = cJSON_GetObjectItem(root, "epochMillis"); if (epochMillis != NULL) { - atclient_atnotification_set_epochMillis(notification, epochMillis->valueint); + if((ret = atclient_atnotification_set_epochMillis(notification, epochMillis->valueint)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epochMillis\n"); + goto exit; + } } cJSON *messageType = cJSON_GetObjectItem(root, "messageType"); @@ -900,12 +1034,18 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_messageType(notification, val, vallen); + if((ret = atclient_atnotification_set_messageType(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification messageType\n"); + goto exit; + } } cJSON *isEncrypted = cJSON_GetObjectItem(root, "isEncrypted"); if (isEncrypted != NULL) { - atclient_atnotification_set_isEncrypted(notification, isEncrypted->valueint); + if((ret = atclient_atnotification_set_isEncrypted(notification, isEncrypted->valueint)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification isEncrypted\n"); + goto exit; + } } cJSON *metadata = cJSON_GetObjectItem(root, "metadata"); @@ -920,7 +1060,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_encKeyName(notification, val, vallen); + if((ret = atclient_atnotification_set_encKeyName(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encKeyName\n"); + goto exit; + } } // get encAlgo @@ -933,7 +1076,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_encAlgo(notification, val, vallen); + if((ret = atclient_atnotification_set_encAlgo(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encAlgo\n"); + goto exit; + } } // get ivNonce @@ -946,7 +1092,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_ivNonce(notification, val, vallen); + if((ret = atclient_atnotification_set_ivNonce(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ivNonce\n"); + goto exit; + } } // get skeEncKeyName @@ -959,7 +1108,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_skeEncKeyName(notification, val, vallen); + if((ret = atclient_atnotification_set_skeEncKeyName(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncKeyName\n"); + goto exit; + } } // get skeEncAlgo @@ -972,7 +1124,10 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - atclient_atnotification_set_skeEncAlgo(notification, val, vallen); + if((ret = atclient_atnotification_set_skeEncAlgo(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncAlgo\n"); + goto exit; + } } } From f80412a8d75664b1c9a87aa8fe6b6973468b76bc Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 15 Jul 2024 13:34:20 -0400 Subject: [PATCH 041/193] fix: unit test failure --- packages/atchops/tests/test_aesctr_decrypt.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/atchops/tests/test_aesctr_decrypt.c b/packages/atchops/tests/test_aesctr_decrypt.c index 77afbb72..c5ed5308 100644 --- a/packages/atchops/tests/test_aesctr_decrypt.c +++ b/packages/atchops/tests/test_aesctr_decrypt.c @@ -46,13 +46,6 @@ int main() { memset(plaintext, 0, plaintextsize); size_t plaintextlen = 0; - unsigned char *iv = malloc(sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - if(iv == NULL) { - printf("malloc (failed): %d\n", ret); - goto exit; - } - memset(iv, 0, ATCHOPS_IV_BUFFER_SIZE); // keys in the atKeys file are encrypted with AES with IV {0} * 16 - const size_t ciphertextsize = 8192; unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); @@ -62,6 +55,13 @@ int main() { memset(key, 0, sizeof(unsigned char) * 32); size_t keylen = 0; + unsigned char *iv = malloc(sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + if(iv == NULL) { + printf("malloc (failed): %d\n", ret); + goto exit; + } + memset(iv, 0, ATCHOPS_IV_BUFFER_SIZE); // keys in the atKeys file are encrypted with AES with IV {0} * 16 + ret = atchops_base64_decode(CIPHERTEXTBASE64, strlen(CIPHERTEXTBASE64), ciphertext, ciphertextsize, &ciphertextlen); if(ret != 0) { printf("atchops_base64_decode (failed): %d\n", ret); From 27135e382b11116e113958a98fa44ff0b56b6985 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 16 Jul 2024 21:05:33 -0400 Subject: [PATCH 042/193] feat: cleaned up atclient_get_publickey.c --- .../atclient/src/atclient_get_publickey.c | 101 +++++++++++++----- 1 file changed, 75 insertions(+), 26 deletions(-) diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index 2be135a5..bcd24959 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -8,22 +8,25 @@ #define TAG "atclient_get_publickey" +static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, + const size_t valuesize, size_t *valuelen, bool bypasscache); + int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen, bool bypasscache) { - if (atclient->async_read) { - atlogger_log( - TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_publickey cannot be called from an async_read atclient, it will cause a race condition\n"); - return 1; - } int ret = 1; - if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->atkeytype != ATKEYTYPE_PUBLIC\n"); - return 1; + /* + * 1. Validate arguments + */ + if ((ret = atclient_get_publickey_validate_arguments(atclient, atkey, value, valuesize, valuelen, bypasscache)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_publickey_validate_arguments: %d\n", ret); + return ret; } - // 1. initialize variables + /* + * 2. Initialize variables + */ char *atkeystr = NULL; const size_t recvsize = valuesize; @@ -35,41 +38,41 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu char *cmdbuffer = NULL; char *metadatastr = NULL; - // 2. build plookup: command + /* + * 3. Build `plookup:` command + */ ret = atclient_atkey_to_string(atkey, &atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - char *bypasscachestr = NULL; - if (bypasscache) { - bypasscachestr = "bypassCache:true:"; - } - char *atkeystrwithoutpublic = NULL; char *ptr = strstr(atkeystr, "public:"); if (ptr != NULL) { atkeystrwithoutpublic = ptr + strlen("public:"); } else { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not find \"public:\" from string \"%s\"\n", atkeystr); goto exit; } - const size_t cmdbuffersize = strlen("plookup:all:\r\n") + (bypasscachestr != NULL ? strlen(bypasscachestr) : 0) + + const size_t cmdbuffersize = strlen("plookup:all:\r\n") + (bypasscache ? strlen("bypassCache:true:") : 0) + strlen(atkeystrwithoutpublic) + 1; cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if(cmdbuffer == NULL) { + if (cmdbuffer == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); goto exit; } memset(cmdbuffer, 0, cmdbuffersize); - snprintf(cmdbuffer, cmdbuffersize, "plookup:%sall:%s\r\n", bypasscachestr != NULL ? bypasscachestr : "", + snprintf(cmdbuffer, cmdbuffersize, "plookup:%sall:%s\r\n", bypasscache ? "bypassCache:true:" : "", atkeystrwithoutpublic); const size_t cmdbufferlen = strlen(cmdbuffer); - // 3. send plookup: command + /* + * 4. Send `plookup:` command + */ ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbufferlen, recv, recvsize, &recvlen); if (ret != 0) { @@ -77,9 +80,9 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; } - // 4. parse response - - // 4a. if recv does not start with "data:", we probably got an error + /* + * 5. Parse response + */ if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -96,7 +99,6 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; } - // 4b. set *value and *valuelen cJSON *data = cJSON_GetObjectItem(root, "data"); if (data == NULL) { ret = 1; @@ -104,11 +106,13 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; } - memset(value, 0, valuesize); + /* + * 6. Return data to caller + */ memcpy(value, data->valuestring, strlen(data->valuestring)); *valuelen = strlen(value); - // 4c. write to atkey->metadata + // 6b. write to atkey->metadata cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); if (metadata == NULL) { ret = 1; @@ -136,3 +140,48 @@ exit: { return ret; } } + +static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, + const size_t valuesize, size_t *valuelen, bool bypasscache) { + int ret = 1; + + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a public key\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (valuesize == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + goto exit; + } + + if (valuelen == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} \ No newline at end of file From 2571e6a2c7ed7edd82d85f5e4ed74c005cf15644 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 16 Jul 2024 21:05:43 -0400 Subject: [PATCH 043/193] chore: cleaned up atclient_get_atkeys.c --- packages/atclient/src/atclient_get_atkeys.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index a084f42b..03d0e06a 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -99,25 +99,25 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi if (!cJSON_IsString(atkey_json)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_json is not a string\n"); - goto exit1; + goto atkeys_allocated_error; } const char *atkey_str = cJSON_GetStringValue(atkey_json); if (atkey_str == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetStringValue failed\n"); - goto exit1; + goto atkeys_allocated_error; } if ((ret = atclient_atkey_from_string(&(*atkey)[i], atkey_str, strlen(atkey_str))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); - goto exit1; + goto atkeys_allocated_error; } } ret = 0; goto exit; -exit1: { +atkeys_allocated_error: { // error cleanup for (size_t i = 0; i < *output_array_len; i++) { atclient_atkey_free(&(*atkey)[i]); } From 63df4bdf2666612928f4627524c6eeb4778a4e72 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 16 Jul 2024 21:21:28 -0400 Subject: [PATCH 044/193] feat: optimize and clean up atclient_pkam_authenticate --- packages/atclient/src/atclient.c | 114 +++++++++++++++++++++++++------ 1 file changed, 94 insertions(+), 20 deletions(-) diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 77f7a3a1..971a533b 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -23,6 +23,9 @@ #define TAG "atclient" static int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport); +static int atclient_pkam_authenticate_validate_arguments(const atclient *ctx, const char *atserver_host, + const int atserver_port, const atclient_atkeys *atkeys, + const char *atsign); void atclient_init(atclient *ctx) { memset(ctx, 0, sizeof(atclient)); @@ -46,62 +49,95 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i int ret = 1; // error by default + /* + * 1. Validate arguments + */ + if ((ret = atclient_pkam_authenticate_validate_arguments(ctx, atserver_host, atserver_port, atkeys, atsign)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate_validate_arguments: %d\n", ret); + return ret; + } + + /* + * 2. Initialize variables + */ + + // free later char *rootcmd = NULL; char *fromcmd = NULL; char *pkamcmd = NULL; - char *atsign_with_at_symbol = NULL; + char *atsign_with_at = NULL; - const size_t recvsize = 1024; + const size_t recvsize = 1024; // sufficient buffer size to receive 1. host & port from atDirectory, 2. challenge from + // `from:` command, 3. pkam success message from `pkam:` command unsigned char recv[recvsize]; memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen; - const size_t challengesize = 256; + const size_t challengesize = 256; // sufficient buffer size to hold the challenge received from `from:` command char challenge[challengesize]; memset(challenge, 0, sizeof(char) * challengesize); size_t challengelen = 0; - const size_t signaturesize = 256; + const size_t signaturesize = 256; // RSA-2048 signature always generates a 256 byte signature unsigned char signature[signaturesize]; memset(signature, 0, sizeof(unsigned char) * signaturesize); - const size_t signaturebase64size = 2048; + const size_t signaturebase64size = atchops_base64_encoded_size(signaturesize); unsigned char signaturebase64[signaturebase64size]; memset(signaturebase64, 0, sizeof(unsigned char) * signaturebase64size); size_t signaturebase64len = 0; - if (ctx == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); - goto exit; - } - - if ((ret = atclient_stringutils_atsign_with_at_symbol(atsign, strlen(atsign), &(atsign_with_at_symbol))) != 0) { + /* + * 3. Ensure that the atsign has the @ symbol. + */ + if ((ret = atclient_stringutils_atsign_with_at_symbol(atsign, strlen(atsign), &(atsign_with_at))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at_symbol: %d\n", ret); goto exit; } - const char *atsign_without_at_str = (atsign_with_at_symbol + 1); + 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. Start atServer connection (kill the existing connection if it exists) + */ 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:` command + */ const size_t fromcmdsize = - strlen("from:") + strlen(atsign_without_at_str) + strlen("\r\n") + 1; // "from:" has a length of 5 + strlen("from:") + strlen(atsign_without_at) + strlen("\r\n") + 1; // "from:" has a length of 5 fromcmd = malloc(sizeof(char) * fromcmdsize); if (fromcmd == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for fromcmd\n"); goto exit; } - snprintf(fromcmd, fromcmdsize, "from:%s\r\n", atsign_without_at_str); + snprintf(fromcmd, fromcmdsize, "from:%s\r\n", atsign_without_at); + /* + * 5b. Send `from:` command + */ if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)fromcmd, fromcmdsize - 1, recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } + if(!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", strlen("data:"))) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); + goto exit; + } + + /* + * 6. We got `data:` + * Let us sign the challenge with RSA-2048 PKAM Private Key and Base64 Encode it + */ memcpy(challenge, recv, recvlen); // remove data: memmove(challenge, challenge + 5, recvlen - 5); @@ -114,12 +150,16 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } + // base64 encode it if ((ret = atchops_base64_encode(signature, signaturesize, signaturebase64, signaturebase64size, &signaturebase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } + /* + * 7a. Build `pkam:` command + */ const size_t pkamcmdsize = strlen("pkam:") + signaturebase64len + strlen("\r\n") + 1; pkamcmd = malloc(sizeof(char) * pkamcmdsize); if (pkamcmd == NULL) { @@ -127,6 +167,10 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } snprintf(pkamcmd, pkamcmdsize, "pkam:%s\r\n", signaturebase64); + + /* + * 7b. Send `pkam:` command + */ memset(recv, 0, sizeof(unsigned char) * recvsize); if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)pkamcmd, pkamcmdsize - 1, recv, recvsize, &recvlen)) != 0) { @@ -142,12 +186,16 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } + /* + * 8. Set up the atclient context + */ + // initialize ctx->atsign.atsign and ctx->atsign.withour_prefix_str to the newly authenticated atSign if (ctx->_atsign_is_allocated) { atclient_atsign_free(&(ctx->atsign)); ctx->_atsign_is_allocated = false; } - if ((ret = atclient_atsign_init(&(ctx->atsign), atsign_with_at_symbol) != 0)) { + if ((ret = atclient_atsign_init(&(ctx->atsign), atsign_with_at) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init: %d\n", ret); goto exit; } @@ -160,7 +208,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; exit: { - free(atsign_with_at_symbol); + free(atsign_with_at); free(rootcmd); free(fromcmd); free(pkamcmd); @@ -179,7 +227,7 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { const size_t recvsize = 64; if (!heartbeat_conn->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); - if(recv == NULL) { + if (recv == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); ret = 1; goto exit; @@ -253,7 +301,33 @@ static int atclient_start_atserver_connection(atclient *ctx, const char *seconda goto exit; -exit: { - return ret; -} +exit: { return ret; } } + +static int atclient_pkam_authenticate_validate_arguments(const atclient *ctx, const char *atserver_host, + const int atserver_port, const atclient_atkeys *atkeys, + const char *atsign) { + int ret = 1; + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (atserver_host == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver_host is NULL\n"); + goto exit; + } + + if (atkeys == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + goto exit; + } + + if (atsign == NULL || strlen(atsign) == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is NULL or the length is 0\n"); + goto exit; + } + + ret = 0; +exit: { return ret; } +} \ No newline at end of file From 8aea585ead68250afeff2fdd4016e5bb71e75822 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 16 Jul 2024 21:29:20 -0400 Subject: [PATCH 045/193] fix: failing test --- packages/atclient/src/atclient_get_publickey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index bcd24959..a6c3865b 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -157,7 +157,7 @@ static int atclient_get_publickey_validate_arguments(atclient *atclient, atclien goto exit; } - if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a public key\n"); goto exit; From b673f5567138b7edf9353376851768b42d2cada3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 17 Jul 2024 09:35:22 -0400 Subject: [PATCH 046/193] feat: remove strlen from atclient_atsign_without/with_at_symbol --- packages/atclient/include/atclient/stringutils.h | 12 ++++-------- packages/atclient/src/atclient.c | 2 +- packages/atclient/src/atclient_get_atkeys.c | 2 +- packages/atclient/src/atclient_utils.c | 2 +- packages/atclient/src/stringutils.c | 8 ++++---- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/stringutils.h index 8ab3191a..586b9c7c 100644 --- a/packages/atclient/include/atclient/stringutils.h +++ b/packages/atclient/include/atclient/stringutils.h @@ -41,24 +41,20 @@ int atclient_stringutils_ends_with(const char *string, const size_t stringlen, c /** * @brief generate a new string with the atsign and the guaranteed @ symbol * - * @param original_atsign the original atsign - * @param original_atsign_len the length of the original atsign + * @param original_atsign the original atsign, assumed to be null-terminated * @param output_atsign_with_at_symbol the output atsign with the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, const size_t original_atsign_len, - char **output_atsign_with_at_symbol); +int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char **output_atsign_with_at_symbol); /** * @brief generate a new string with the atsign and the guaranteed @ symbol * - * @param original_atsign the original atsign - * @param original_atsign_len the length of the original atsign + * @param original_atsign the original atsign, assumed to be null-terminated * @param output_atsign_without_at_symbol the output atsign without the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, const size_t original_atsign_len, - char **output_atsign_without_at_symbol); +int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, char **output_atsign_without_at_symbol); /** * @brief get the length of a long if it were converted to a string diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 971a533b..2580c3ea 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -90,7 +90,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i /* * 3. Ensure that the atsign has the @ symbol. */ - if ((ret = atclient_stringutils_atsign_with_at_symbol(atsign, strlen(atsign), &(atsign_with_at))) != 0) { + if ((ret = atclient_stringutils_atsign_with_at_symbol(atsign, &(atsign_with_at))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at_symbol: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 03d0e06a..7dc6413c 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -35,7 +35,7 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi (strlen(regex) > 0 ? (strlen(" ") + strlen(regex)) : 0) + strlen("\r\n") + 1; char scancmd[scancmdsize]; - const unsigned char recv[recvbuffersize]; + unsigned char recv[recvbuffersize]; size_t recvlen = 0; cJSON *root = NULL; // free later diff --git a/packages/atclient/src/atclient_utils.c b/packages/atclient/src/atclient_utils.c index de7bc155..ce2f1de9 100644 --- a/packages/atclient/src/atclient_utils.c +++ b/packages/atclient/src/atclient_utils.c @@ -31,7 +31,7 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int goto exit; } - if ((ret = atclient_stringutils_atsign_without_at_symbol(atsign, strlen(atsign), &atsign_without_at_symbol)) != 0) { + if ((ret = atclient_stringutils_atsign_without_at_symbol(atsign, &atsign_without_at_symbol)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at_symbol: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/stringutils.c index 5dca2ca7..63879864 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/stringutils.c @@ -99,13 +99,13 @@ int atclient_stringutils_ends_with(const char *string, const size_t stringlen, c exit: { return ret; } } -int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, const size_t original_atsign_len, - char **output_atsign_with_at_symbol) { +int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char **output_atsign_with_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; goto exit; } + const size_t original_atsign_len = strlen(original_atsign); if (original_atsign_len == 0) { ret = -1; goto exit; @@ -140,13 +140,13 @@ int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, cons exit: { return ret; } } -int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, const size_t original_atsign_len, - char **output_atsign_without_at_symbol) { +int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, char **output_atsign_without_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; goto exit; } + const size_t original_atsign_len = strlen(original_atsign); if (original_atsign_len == 0) { ret = -1; goto exit; From 0d6d739fbc0f8e6f9d1e123baa5f86cc0355073d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 17 Jul 2024 09:35:51 -0400 Subject: [PATCH 047/193] chore: remove `symbol` from function name --- packages/atclient/include/atclient/stringutils.h | 4 ++-- packages/atclient/src/atclient.c | 4 ++-- packages/atclient/src/atclient_utils.c | 4 ++-- packages/atclient/src/stringutils.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/stringutils.h index 586b9c7c..8900dc65 100644 --- a/packages/atclient/include/atclient/stringutils.h +++ b/packages/atclient/include/atclient/stringutils.h @@ -45,7 +45,7 @@ int atclient_stringutils_ends_with(const char *string, const size_t stringlen, c * @param output_atsign_with_at_symbol the output atsign with the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char **output_atsign_with_at_symbol); +int atclient_stringutils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol); /** * @brief generate a new string with the atsign and the guaranteed @ symbol @@ -54,7 +54,7 @@ int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char * @param output_atsign_without_at_symbol the output atsign without the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, char **output_atsign_without_at_symbol); +int atclient_stringutils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol); /** * @brief get the length of a long if it were converted to a string diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 2580c3ea..e1fe039a 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -90,8 +90,8 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i /* * 3. Ensure that the atsign has the @ symbol. */ - if ((ret = atclient_stringutils_atsign_with_at_symbol(atsign, &(atsign_with_at))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at_symbol: %d\n", ret); + if ((ret = atclient_stringutils_atsign_with_at(atsign, &(atsign_with_at))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_utils.c b/packages/atclient/src/atclient_utils.c index ce2f1de9..5829e1f2 100644 --- a/packages/atclient/src/atclient_utils.c +++ b/packages/atclient/src/atclient_utils.c @@ -31,8 +31,8 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int goto exit; } - if ((ret = atclient_stringutils_atsign_without_at_symbol(atsign, &atsign_without_at_symbol)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at_symbol: %d\n", ret); + if ((ret = atclient_stringutils_atsign_without_at(atsign, &atsign_without_at_symbol)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/stringutils.c index 63879864..d13f1dec 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/stringutils.c @@ -99,7 +99,7 @@ int atclient_stringutils_ends_with(const char *string, const size_t stringlen, c exit: { return ret; } } -int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char **output_atsign_with_at_symbol) { +int atclient_stringutils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; @@ -140,7 +140,7 @@ int atclient_stringutils_atsign_with_at_symbol(const char *original_atsign, char exit: { return ret; } } -int atclient_stringutils_atsign_without_at_symbol(const char *original_atsign, char **output_atsign_without_at_symbol) { +int atclient_stringutils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; From efe3ee22de4160fe237ab56873c75d6219578276 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 17 Jul 2024 15:48:25 -0400 Subject: [PATCH 048/193] wip: --- packages/atclient/include/atclient/atkey.h | 193 +++++- .../atclient/include/atclient/stringutils.h | 21 +- packages/atclient/src/atclient_delete.c | 7 +- packages/atclient/src/atclient_get_atkeys.c | 4 +- .../atclient/src/atclient_get_publickey.c | 6 +- packages/atclient/src/atclient_get_selfkey.c | 95 ++- packages/atclient/src/atkey.c | 596 ++++++++++++------ packages/atclient/src/stringutils.c | 58 +- 8 files changed, 683 insertions(+), 297 deletions(-) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index 2dfad3fa..c61d151e 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -5,6 +5,18 @@ #include "atclient/metadata.h" #include +#define VALUE_INITIALIZED 0b00000001 + +#define ATKEY_KEY_INDEX 0 +#define ATKEY_NAMESPACESTR_INDEX 0 +#define ATKEY_SHAREDBY_INDEX 0 +#define ATKEY_SHAREDWITH_INDEX 0 + +#define ATKEY_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATKEY_NAMESPACESTR_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATKEY_SHAREDBY_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATKEY_SHAREDWITH_INITIALIZED (VALUE_INITIALIZED << 3) + typedef enum atclient_atkey_type { ATCLIENT_ATKEY_TYPE_UNKNOWN = 0, ATCLIENT_ATKEY_TYPE_PUBLICKEY, @@ -13,14 +25,12 @@ typedef enum atclient_atkey_type { } atclient_atkey_type; typedef struct atclient_atkey { - // TODO: remove atkey_type and replace it with a policy function that infers atkeytype given atclient_atkey & atclient - atclient_atkey_type atkeytype; + char *key; + char *namespacestr; + char *sharedby; + char *sharedwith; - // TODO: this should be called atkey.key to be consistent with dart - atclient_atstr name; - atclient_atstr namespacestr; - atclient_atstr sharedby; - atclient_atstr sharedwith; + u_int8_t _initializedfields[1]; // internal field to track which fields are allocated atclient_atkey_metadata metadata; @@ -40,6 +50,16 @@ void atclient_atkey_init(atclient_atkey *atkey); */ void atclient_atkey_free(atclient_atkey *atkey); +/** + * @brief get the length of the atkey string + * + * @param atkey atkey struct to read, assumed that this was already initialized via atclient_atkey_init + * @return size_t the length of the atkey string + * + * @note this excludes the null terminator and metadata string fragement + */ +size_t atclient_atkey_strlen(const atclient_atkey *atkey); + /** * @brief populate an atkey struct given a null terminated string. Be sure to call the atclient_atkey_init function * before calling this function. @@ -50,17 +70,7 @@ void atclient_atkey_free(atclient_atkey *atkey); * @return int 0 on success, that a struct was able to be created from the string. (the string followed proper key * nomenclature) */ -int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, const size_t atkeylen); - -/** - * @brief get the length of the atkey string - * - * @param atkey atkey struct to read, assumed that this was already initialized via atclient_atkey_init - * @return size_t the length of the atkey string - * - * @note this excludes the null terminator and metadata string fragement - */ -size_t atclient_atkey_strlen(const atclient_atkey *atkey); +int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr); /** * @brief convert an atkey struct to its string format @@ -72,6 +82,132 @@ size_t atclient_atkey_strlen(const atclient_atkey *atkey); */ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr); +/** + * @brief Returns true if atkey->key is initialized and allocated, and the value is safe for reading + * + * @param atkey the atkey struct to read + * @return true, if the key is initialized and allocated and is safe for reading + * @return false, if the key is not initialized and not allocated and is not safe for reading, high chance that it holds + * garbage + */ +bool atclient_atkey_is_key_initialized(const atclient_atkey *atkey); + +/** + * @brief Returns true if atkey->namespacestr is initialized and allocated, and the value is safe for reading + * + * @param atkey the atkey struct to read + * @return true, if the namespacestr is initialized and allocated and is safe for reading + * @return false, if the namespacestr is not initialized and not allocated and is not safe for reading, high chance that + * it holds garbage + */ +bool atclient_atkey_is_namespacestr_initialized(const atclient_atkey *atkey); + +/** + * @brief Returns true if atkey->sharedby is initialized and allocated, and the value is safe for reading + * + * @param atkey the atkey struct to read + * @return true, if the sharedby is initialized and allocated and is safe for reading + * @return false, if the sharedby is not initialized and not allocated and is not safe for reading, high chance that it + * holds garbage + */ +bool atclient_atkey_is_sharedby_initialized(const atclient_atkey *atkey); + +/** + * @brief Returns true if atkey->sharedwith is initialized and allocated, and the value is safe for reading + * + * @param atkey the atkey struct to read + * @return true, if the sharedwith is initialized and allocated and is safe for reading + * @return false, if the sharedwith is not initialized and not allocated and is not safe for reading, high chance that + * it holds garbage + */ +bool atclient_atkey_is_sharedwith_initialized(const atclient_atkey *atkey); + +/** + * @brief Dynamically allocates memory and duplicates key to be stored in the atkey struct. If this is allocated, then + * it is freed with atclient_atkey_free by the end of life of your struct + * + * @param atkey The atkey struct that will hold the key, assumed that this was already initialized via + * atclient_atkey_init + * @param key The key string, for example if my atkey is "@alice:phone.wavi@bob", then the key is "phone" + * @return int 0 on success, otherwise error (most likely malloc error) + */ +int atclient_atkey_set_key(atclient_atkey *atkey, const char *key); + +/** + * @brief Dynamically allocates memory and duplicates namespacestr to be stored in the atkey struct. If this is + * allocated, then it is freed with atclient_atkey_free by the end of life of your struct + * + * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via + * atclient_atkey_init + * @param namespacestr (mandatory, NON-NULL) The namespace of your application, e.g. "banking_app" (NULLABLE) + * @return int 0 on success, otherwise error (most likely malloc error) + */ +int atclient_atkey_set_namespacestr(atclient_atkey *atkey, const char *namespacestr); + +/** + * @brief Dynamically allocates memory and duplicates sharedby to be stored in the atkey struct. If this is allocated, + * then it is freed with atclient_atkey_free by the end of life of your struct + * + * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via + * atclient_atkey_init + * @param sharedby (mandatory, NON-NULL) The sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" + * @return int 0 on success, otherwise error (most likely malloc error) + */ +int atclient_atkey_set_sharedby(atclient_atkey *atkey, const char *sharedby); + +/** + * @brief Dynamically allocates memory and duplicates sharedwith to be stored in the atkey struct. If this is allocated, + * then it is freed with atclient_atkey_free by the end of life of your struct + * + * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via + * atclient_atkey_init + * @param sharedwith (mandatory, NON-NULL) The sharedwith atsign, atsign you are going to share it with, e.g. "@bob" + * @return int 0 on success, otherwise error (most likely malloc error) + */ +int atclient_atkey_set_sharedwith(atclient_atkey *atkey, const char *sharedwith); + +/** + * @brief Frees the memory allocated for the key in the atkey struct. This is already called by atclient_atkey_free and + * should only be used for advanced pruposes, if you want to free the key before the end of life of your struct + * + * @param atkey the atkey struct that holds the allocated atkey->key memory + */ +void atclient_atkey_unset_key(atclient_atkey *atkey); + +/** + * @brief Frees the memory allocated for the namespacestr in the atkey struct. This is already called by + * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the namespacestr before the + * end of life of your struct + * + * @param atkey the atkey struct that holds the allocated atkey->namespacestr memory + */ +void atclient_atkey_unset_namespacestr(atclient_atkey *atkey); + +/** + * @brief Free the memory allocated for the sharedby in the atkey struct. This is already called by atclient_atkey_free + * and should only be used for advanced pruposes, if you want to free the sharedby before the end of life of your struct + * + * @param atkey the atkey struct that holds the allocated atkey->sharedby memory + */ +void atclient_atkey_unset_sharedby(atclient_atkey *atkey); + +/** + * @brief Frees the memory allocated for the sharedwith in the atkey struct. This is already called by + * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the sharedwith before the end + * of life of your struct + * + * @param atkey the atkey struct that holds the allocated atkey->sharedwith memory + */ +void atclient_atkey_unset_sharedwith(atclient_atkey *atkey); + +/** + * @brief Evaluates the type of the atkey struct + * + * @param atkey the atkey struct to evaluate + * @return atclient_atkey_type the type of the atkey struct, returns ATCLIENT_ATKEY_TYPE_UNKNOWN if an error occurred + */ +atclient_atkey_type atclient_atkey_get_type(const atclient_atkey *atkey); + /** * @brief Populate an atkey struct representing a PublicKey AtKey with null terminated strings. An example of a Public * AtKey would be 'public:name.namespace@alice'. Public AtKeys typically hold unencrypted values and can be seen by @@ -79,15 +215,11 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr); * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name the name of the atkey, e.g.: "name" - * @param namelen the length of the name (use strlen in most cases) * @param sharedby the sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" - * @param sharedbylen the length of the sharedby (use strlen in most cases) * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) - * @param namespacestrlen the length of the namespacestr (use strlen in most cases) * @return int 0 on success */ -int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *namespacestr, const size_t namespacestrlen); +int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, const char *sharedby, const char *namespacestr); /** * @brief Populate an atkey struct representing a SelfKey AtKey with null terminated strings. An example of a SelfKey @@ -96,15 +228,11 @@ int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, con * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name the name of the atkey, e.g.: "name" - * @param namelen the length of the name (use strlen in most cases) * @param sharedby the sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" - * @param sharedbylen the length of the sharedby (use strlen in most cases) * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) - * @param namespacestrlen the length of the namespacestr (use strlen in most cases) * @return int 0 on success */ -int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *namespacestr, const size_t namespacestrlen); +int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const char *sharedby, const char *namespacestr); /** * @brief Populate an atkey struct representing a SharedKey AtKey given null terminated strings. An example of a @@ -114,17 +242,12 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name name of your key, e.g. "name" - * @param namelen the length of the name (use strlen in most cases) * @param sharedby the shared by atsign, e.g. "@alice" - * @param sharedbylen the length of the sharedby (use strlen in most cases) * @param sharedwith the sharedwith atsign, atsign you are going to share it with, e.g. "@bob" - * @param sharedwithlen the length of the sharedwith (use strlen in most cases) * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) - * @param namespacestrlen the length of the namespacestr (use strlen in most cases) * @return int 0 on success */ -int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *sharedwith, const size_t sharedwithlen, - const char *namespacestr, const size_t namespacestrlen); +int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *name, const char *sharedby, + const char *sharedwith, const char *namespacestr); #endif diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/stringutils.h index 8900dc65..af80fa9d 100644 --- a/packages/atclient/include/atclient/stringutils.h +++ b/packages/atclient/include/atclient/stringutils.h @@ -1,6 +1,7 @@ #ifndef ATCLIENT_STRINGUTILS_H #define ATCLIENT_STRINGUTILS_H +#include #include /** @@ -17,26 +18,24 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string size_t *outlen); /** - * @brief returns 1 (true) if the string starts with the prefix, 0 (false) otherwise + * @brief check if string starts with prefix * * @param string the string to check - * @param stringlen the length of the string (use strlen(string) if it is null-terminated) * @param prefix the prefix to check for - * @return int 1 (true) if the string starts with the prefix, 0 (false) otherwise + * @return true, if string starts with `prefix`, where `prefix` is a valid substring of `string` + * @return false, otherwise */ -int atclient_stringutils_starts_with(const char *string, const size_t stringlen, const char *prefix, - const size_t prefixlen); +bool atclient_stringutils_starts_with(const char *string, const char *prefix); /** - * @brief returns 1 (true) if the string ends with the suffix, 0 (false) otherwise + * @brief check if string ends with suffix * * @param string the string to check - * @param stringlen the length of the string (use strlen(string) if it is null-terminated) - * @param suffix the suffix to check for - * @return int 1 (true) if the string ends with the suffix, 0 (false) otherwise + * @param suffix suffix to check for + * @return true, if string ends with `suffix`, where `suffix` is a valid substring of `string` + * @return false, otherwise */ -int atclient_stringutils_ends_with(const char *string, const size_t stringlen, const char *suffix, - const size_t suffixlen); +bool atclient_stringutils_ends_with(const char *string, const char *suffix); /** * @brief generate a new string with the atsign and the guaranteed @ symbol diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 4cc1eb6e..0b97a557 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -36,7 +36,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { unsigned char *recv; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); - if(recv == NULL) { + if (recv == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); goto exit; @@ -57,7 +57,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if(cmdbuffer == NULL) { + if (cmdbuffer == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); goto exit; @@ -67,7 +67,6 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { /* * 4. Send command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); @@ -78,7 +77,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 7dc6413c..b00305fd 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -60,7 +60,7 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi /* * 5. Parse response */ - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); @@ -109,7 +109,7 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi goto atkeys_allocated_error; } - if ((ret = atclient_atkey_from_string(&(*atkey)[i], atkey_str, strlen(atkey_str))) != 0) { + if ((ret = atclient_atkey_from_string(&(*atkey)[i], atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); goto atkeys_allocated_error; } diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index a6c3865b..d643a8c9 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -83,7 +83,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu /* * 5. Parse response */ - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); @@ -157,7 +157,9 @@ static int atclient_get_publickey_validate_arguments(atclient *atclient, atclien goto exit; } - if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a public key\n"); goto exit; diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index c64a8ad6..9b51bd8f 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -14,21 +14,23 @@ #define TAG "atclient_get_selfkey" +static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t valuesize, const size_t *valuelen); + int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen) { - if (atclient->async_read) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_selfkey cannot be called from an async_read atclient, it will cause a race condition\n"); - return 1; - } - if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->atkeytype != ATKEYTYPE_SELF\n"); - return 1; - } - int ret = 1; - // 1. initialize variables + /* + * 1. Validate arguments + */ + if((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, valuesize, valuelen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey_valid_arguments: %d\n", ret); + return ret; + } + + /* + * 2. Initialize variables + */ char *atkeystr = NULL; const size_t recvsize = valuesize; @@ -76,7 +78,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, } // 4. parse response - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); @@ -180,3 +182,72 @@ exit: { return ret; } } + +static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t valuesize, const size_t *valuelen) { + int ret = 1; + + if(atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if(!atclient->_atsign_is_allocated) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient->_atsign_is_allocated is false\n"); + goto exit; + } + + if(!atclient->_atserver_connection_started) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + goto exit; + } + + if (atclient->async_read) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_selfkey cannot be called from an async_read atclient, it will cause a race condition\n"); + return 1; + } + + if(atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if(!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not initialized\n"); + goto exit; + } + + if(atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is shared by someone else\n"); + goto exit; + } + + if(value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if(valuesize == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + goto exit; + } + + if(valuelen == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + goto exit; + } + + 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 30c6984f..48f01a96 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -15,21 +15,50 @@ void atclient_atkey_init(atclient_atkey *atkey) { memset(atkey, 0, sizeof(atclient_atkey)); - atclient_atstr_init(&(atkey->name), ATCLIENT_ATKEY_KEY_LEN + 1); - atclient_atstr_init(&(atkey->namespacestr), ATCLIENT_ATKEY_NAMESPACE_LEN + 1); - atclient_atstr_init(&(atkey->sharedby), ATCLIENT_ATKEY_FULL_LEN); - atclient_atstr_init(&(atkey->sharedwith), ATCLIENT_ATKEY_FULL_LEN); - + atkey->key = NULL; + atkey->sharedby = NULL; + atkey->sharedwith = NULL; + atkey->namespacestr = NULL; + memset(atkey->_initializedfields, 0, sizeof(atkey->_initializedfields)); atclient_atkey_metadata_init(&(atkey->metadata)); } -void atclient_atkey_free(atclient_atkey *atkey) { - free(atkey->name.str); - free(atkey->namespacestr.str); - free(atkey->sharedwith.str); - free(atkey->sharedby.str); +void atclient_atkey_free(atclient_atkey *atkey) { atclient_atkey_metadata_free(&atkey->metadata); } - atclient_atkey_metadata_free(&atkey->metadata); +size_t atclient_atkey_strlen(const atclient_atkey *atkey) { + if (atkey == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + return -1; + } + if (!atclient_atkey_is_key_initialized(atkey) || !atclient_atkey_is_sharedby_initialized(atkey)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->sharedby is not initialized\n"); + return -1; + } + if (strlen(atkey->key) <= 0 || strlen(atkey->sharedby) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->sharedby is empty\n"); + return -1; + } + atclient_atkey_type type = atclient_atkey_get_type(atkey); + if (type == ATCLIENT_ATKEY_TYPE_UNKNOWN) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is unknown\n"); + return -1; + } + size_t len = 0; + if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { + len += strlen("cached:"); + } + if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic) { + len += strlen("public:"); + } + if (type == ATCLIENT_ATKEY_TYPE_SHAREDKEY && atclient_atkey_is_sharedwith_initialized(atkey)) { + len += strlen(atkey->sharedwith) + strlen(":"); + } + len += strlen(atkey->key); + if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespacestr) > 0) { + len += strlen(".") + strlen(atkey->namespacestr); + } + len += strlen(atkey->sharedby); + return len; } int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, const size_t atkeylen) { @@ -75,6 +104,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons // namespace = NULL // cached = false int ret = 1; + char *sharedby_withat = NULL; char *saveptr; char *copy = strndup(atkeystr, atkeylen); if (copy == NULL) { @@ -82,12 +112,12 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons goto exit; } char *token = strtok_r(copy, ":", &saveptr); - size_t tokenlen = strlen(token); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tokens[0] is NULL\n"); ret = 1; goto exit; } + size_t tokenlen = strlen(token); // check if cached if (strncmp(token, "cached", strlen("cached")) == 0) { atclient_atkey_metadata_set_iscached(&(atkey->metadata), true); @@ -99,10 +129,9 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons } } - if (atclient_stringutils_starts_with(token, tokenlen, "public", strlen("public")) == 1) { + if (atclient_stringutils_starts_with(token, "public")) { // it is a public key atclient_atkey_metadata_set_ispublic(&(atkey->metadata), true); - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; // shift tokens array to the left token = strtok_r(NULL, ":", &saveptr); if (token == NULL) { @@ -110,29 +139,25 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons ret = 1; goto exit; } - } else if (atclient_stringutils_starts_with(token, tokenlen, "@", strlen("@")) == 1) { + } else if (atclient_stringutils_starts_with(token, "@")) { // it is a shared key - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; // set sharedwith - ret = atclient_atstr_set_literal(&(atkey->sharedwith), token, tokenlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedwith(atkey, token)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); goto exit; } token = strtok_r(NULL, ":", &saveptr); if (token == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); goto exit; } - } else if (atclient_stringutils_starts_with(token, tokenlen, "_", strlen("_")) == 1) { + } else if (atclient_stringutils_starts_with(token, "_")) { // it is an internal key - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; - atclient_atkey_metadata_set_ishidden(&(atkey->metadata), true); } else { // it is a self key - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; } + // set atkey->name token = strtok_r(token, "@", &saveptr); if (token == NULL) { @@ -141,47 +166,45 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons goto exit; } tokenlen = strlen(token); - char nameandnamespacestr[ATCLIENT_ATKEY_COMPOSITE_LEN + 1]; - memset(nameandnamespacestr, 0, sizeof(char) * ATCLIENT_ATKEY_COMPOSITE_LEN + 1); - memcpy(nameandnamespacestr, token, tokenlen); - char *check = strchr(nameandnamespacestr, '.'); - if (check != NULL && *(check + 1) != '\0') { - // there is a namespace + + const size_t compositesize = ATCLIENT_ATKEY_COMPOSITE_LEN + 1; + char composite[compositesize]; + memset(composite, 0, sizeof(char) * compositesize); + memcpy(composite, token, tokenlen); + + char *check = strchr(composite, '.'); + if (check != NULL && *(check + 1) != '\0') { // there is a namespace char *saveptr2; - char *name = strtok_r(nameandnamespacestr, ".", &saveptr2); - if (name == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name is NULL. %s atkey is probably incomplete\n", atkeystr); + + char *key = strtok_r(composite, ".", &saveptr2); + if (key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; goto exit; } - size_t namelen = strlen(name); - ret = atclient_atstr_set_literal(&(atkey->name), name, namelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(atkey, key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } + char *namespacestr = strtok_r(NULL, "", &saveptr2); if (namespacestr == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. %s atkey is probably incomplete\n", atkeystr); - ret = 1; goto exit; } - size_t namespacestrlen = strlen(namespacestr); - ret = atclient_atstr_set_literal(&(atkey->namespacestr), namespacestr, namespacestrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - } else { - // there is no namespace - size_t namelen = strlen(nameandnamespacestr); - ret = atclient_atstr_set_literal(&(atkey->name), nameandnamespacestr, namelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + } else { // there is no namespace + if ((ret = atclient_atkey_set_key(atkey, composite)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } } + // set atkey->sharedby token = strtok_r(NULL, "", &saveptr); if (token == NULL) { @@ -190,51 +213,61 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons goto exit; } tokenlen = strlen(token); - char sharedbystr[ATCLIENT_ATKEY_FULL_LEN + 1]; - memset(sharedbystr, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN + 1); - size_t sharedbystrlen = 0; - ret = atclient_atsign_with_at_symbol(sharedbystr, ATCLIENT_ATSIGN_FULL_LEN, &sharedbystrlen, token, tokenlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_with_at_symbol failed\n"); + + if ((ret = atclient_stringutils_atsign_with_at(token, &sharedby_withat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey->sharedby), sharedbystr, sharedbystrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + + if ((ret = atclient_atkey_set_sharedby(atkey, sharedby_withat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } + ret = 0; goto exit; exit: { free(copy); + free(sharedby_withat); return ret; } } -size_t atclient_atkey_strlen(const atclient_atkey *atkey) { - size_t len = 0; - if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { - len += strlen("cached:"); +int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { + int ret = 1; + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + return ret; } - if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic) { - len += strlen("public:"); + + if (atkeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "**atkeystr is NULL\n"); + return ret; } - if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - len += atkey->sharedwith.len + strlen(":"); + + atclient_atkey_type atkeytype = atclient_atkey_get_type(atkey); + + if (atkeytype == ATCLIENT_ATKEY_TYPE_UNKNOWN) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeytype is unknown\n"); + return ret; } - len += atkey->name.len; - if (atkey->namespacestr.len > 0) { - len += strlen(".") + atkey->namespacestr.len; + + if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey->sharedby is not initialized or strlen(atkey->sharedby) <= 0. AtKey is incomplete.\n"); + return ret; } - len += atkey->sharedby.len; - return len; -} -int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { - int ret = 1; + if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey->key is not initialized or strlen(atkey->key) <= 0. AtKey is incomplete.\n"); + return ret; + } size_t index_pos = 0; - const size_t atkeystrsize = atclient_atkey_strlen(atkey) + 1; *atkeystr = (char *)malloc(sizeof(char) * atkeystrsize); if (*atkeystr == NULL) { @@ -248,108 +281,278 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { index_pos += strlen("cached:"); } - if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic && - atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); - index_pos += strlen("public:"); - } else if (atkey->metadata.ispublic || atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "either atkey's metadata ispublic is not set to 1 or atkey's atkeytype is not set to " - "ATCLIENT_ATKEY_TYPE_PUBLICKEY for atkey: %.*s\n", - (int)atkey->name.len, atkey->name.str); + if (atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + if (!atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) || !atkey->metadata.ispublic) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, + "atkey's metadata ispublic is either not initialized or false, even though it was deemed a " + "ATCLIENT_ATKEY_TYPE_PUBLICKEY by atclient_atkey_get_type\n"); + } snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); index_pos += strlen("public:"); - } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s:", (int)atkey->sharedwith.len, - atkey->sharedwith.str); - index_pos += (atkey->sharedwith.len) + strlen(":"); - } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_UNKNOWN) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's atkeytype is %d: %.*s\n", atkey->atkeytype, - (int)atkey->name.len, atkey->name.str); - goto error_exit; + } else if (atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + if (atclient_atkey_is_sharedwith_initialized(atkey)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, + "atkey's sharedwith is initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHAREDKEY by " + "atclient_atkey_get_type\n"); + } + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s:", atkey->sharedwith); + index_pos += strlen(atkey->sharedwith) + strlen(":"); } - if (atkey->name.str == NULL || atkey->name.len == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey's name is NULL or empty. atkey.name.str: \"%s\", atkey.name.len: %d\n", atkey->name.str, - atkey->name.len); - goto error_exit; + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s", atkey->key); + index_pos += strlen(atkey->key); + + if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespacestr) > 0) { + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, ".%s", atkey->namespacestr); + index_pos += strlen(".") + strlen(atkey->namespacestr); } - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->name.len, atkey->name.str); - index_pos += atkey->name.len; + snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s", atkey->sharedby); + index_pos += strlen(atkey->sharedby); - if (atkey->namespacestr.len > 0) { - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, ".%.*s", (int)atkey->namespacestr.len, - atkey->namespacestr.str); - index_pos += strlen(".") + atkey->namespacestr.len; + if (index_pos != atkeystrsize - 1) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, + "index_pos != atkeystrsize - 1 (%d != %d - 1). The predicted `atkeystrsize` variable was not " + "evaluated correctly.\n", + index_pos, atkeystrsize); } - if (atkey->sharedby.str == NULL || atkey->sharedby.len == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey's sharedby is NULL or empty. atkey.sharedby.str: \"%s\", atkey.sharedby.len: %d\n", - atkey->sharedby.str, atkey->sharedby.len); - goto error_exit; - } + ret = 0; + goto exit; +error_exit: { + free(*atkeystr); + goto exit; +} +exit: { return ret; } +} - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%.*s", (int)atkey->sharedby.len, atkey->sharedby.str); - index_pos += (int)atkey->sharedby.len; +bool atclient_atkey_is_key_initialized(const atclient_atkey *atkey) { + return (atkey->_initializedfields[ATKEY_KEY_INDEX] & ATKEY_KEY_INITIALIZED); +} - if (strlen(*atkeystr) > atkeystrsize) { +bool atclient_atkey_is_namespacestr_initialized(const atclient_atkey *atkey) { + return (atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] & ATKEY_NAMESPACESTR_INITIALIZED); +} + +bool atclient_atkey_is_sharedby_initialized(const atclient_atkey *atkey) { + return (atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] & ATKEY_SHAREDBY_INITIALIZED); +} + +bool atclient_atkey_is_sharedwith_initialized(const atclient_atkey *atkey) { + return (atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] & ATKEY_SHAREDWITH_INITIALIZED); +} + +int atclient_atkey_set_key(atclient_atkey *atkey, const char *key) { + int ret = 1; + if (atkey == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkeystr is too small. Conditiion strlen(atkey)str > atkeystrsize: %d > %d, is true\n", - strlen(*atkeystr), atkeystrsize); - goto error_exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); + goto exit; + } + if (key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL. This is a required argument.\n"); + goto exit; } + if (atclient_atkey_is_key_initialized(atkey)) { + free(atkey->key); + atkey->key = NULL; + atkey->_initializedfields[ATKEY_KEY_INDEX] &= ~ATKEY_KEY_INITIALIZED; + } + atkey->key = strdup(key); + if (atkey->key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); + ret = 1; + goto exit; + } + atkey->_initializedfields[ATKEY_KEY_INDEX] |= ATKEY_KEY_INITIALIZED; + ret = 0; + goto exit; +exit: { return ret; } +} +int atclient_atkey_set_namespacestr(atclient_atkey *atkey, const char *namespacestr) { + int ret = 1; + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); + goto exit; + } + if (namespacestr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. This is a required argument.\n"); + goto exit; + } + if (atclient_atkey_is_namespacestr_initialized(atkey)) { + free(atkey->namespacestr); + atkey->namespacestr = NULL; + atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] &= ~ATKEY_NAMESPACESTR_INITIALIZED; + } + atkey->namespacestr = strdup(namespacestr); + if (atkey->namespacestr == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); + ret = 1; + goto exit; + } + atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] |= ATKEY_NAMESPACESTR_INITIALIZED; ret = 0; goto exit; -error_exit: { - free(*atkeystr); +exit: { return ret; } +} + +int atclient_atkey_set_sharedby(atclient_atkey *atkey, const char *sharedby) { + int ret = 1; + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); + goto exit; + } + if (sharedby == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + goto exit; + } + if (atclient_atkey_is_sharedby_initialized(atkey)) { + free(atkey->sharedby); + atkey->sharedby = NULL; + atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] &= ~ATKEY_SHAREDBY_INITIALIZED; + } + atkey->sharedby = strdup(sharedby); + if (atkey->sharedby == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); + ret = 1; + goto exit; + } + atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] |= ATKEY_SHAREDBY_INITIALIZED; + ret = 0; goto exit; +exit: { return ret; } } + +int atclient_atkey_set_sharedwith(atclient_atkey *atkey, const char *sharedwith) { + int ret = 1; + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); + goto exit; + } + if (sharedwith == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL. This is a required argument.\n"); + goto exit; + } + if (atclient_atkey_is_sharedwith_initialized(atkey)) { + free(atkey->sharedwith); + atkey->sharedwith = NULL; + atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] &= ~ATKEY_SHAREDWITH_INITIALIZED; + } + atkey->sharedwith = strdup(sharedwith); + if (atkey->sharedwith == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); + ret = 1; + goto exit; + } + atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] |= ATKEY_SHAREDWITH_INITIALIZED; + ret = 0; + goto exit; exit: { return ret; } } -int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *namespacestr, const size_t namespacestrlen) { +void atclient_atkey_unset_key(atclient_atkey *atkey) { + if (atclient_atkey_is_key_initialized(atkey)) { + free(atkey->key); + atkey->key = NULL; + atkey->_initializedfields[ATKEY_KEY_INDEX] &= ~ATKEY_KEY_INITIALIZED; + } +} + +void atclient_atkey_unset_namespacestr(atclient_atkey *atkey) { + if (atclient_atkey_is_namespacestr_initialized(atkey)) { + free(atkey->namespacestr); + atkey->namespacestr = NULL; + atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] &= ~ATKEY_NAMESPACESTR_INITIALIZED; + } +} + +void atclient_atkey_unset_sharedby(atclient_atkey *atkey) { + if (atclient_atkey_is_sharedby_initialized(atkey)) { + free(atkey->sharedby); + atkey->sharedby = NULL; + atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] &= ~ATKEY_SHAREDBY_INITIALIZED; + } +} + +void atclient_atkey_unset_sharedwith(atclient_atkey *atkey) { + if (atclient_atkey_is_sharedwith_initialized(atkey)) { + free(atkey->sharedwith); + atkey->sharedwith = NULL; + atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] &= ~ATKEY_SHAREDWITH_INITIALIZED; + } +} + +atclient_atkey_type atclient_atkey_get_type(const atclient_atkey *atkey) { + if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic && + !atclient_atkey_is_sharedwith_initialized(atkey)) { + return ATCLIENT_ATKEY_TYPE_PUBLICKEY; + } + if (atclient_atkey_is_sharedby_initialized(atkey) && atclient_atkey_is_sharedwith_initialized(atkey)) { + if (strcmp(atkey->sharedby, atkey->sharedwith) == 0) { // special case + return ATCLIENT_ATKEY_TYPE_SELFKEY; + } + return ATCLIENT_ATKEY_TYPE_SHAREDKEY; + } + + if (atclient_atkey_is_sharedby_initialized(atkey) && !atclient_atkey_is_sharedwith_initialized(atkey)) { + return ATCLIENT_ATKEY_TYPE_SELFKEY; + } + return ATCLIENT_ATKEY_TYPE_UNKNOWN; +} + +int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *key, const char *sharedby, + const char *namespacestr) { int ret = 1; - if (name == NULL || sharedby == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name or sharedby is NULL. These are required arguments.\n"); + if (atkey == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - - if (namelen == 0 || sharedbylen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namelen or sharedbylen is 0. These are required arguments.\n"); + if (key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL. This is a required argument.\n"); + goto exit; + } + if (strlen(key) == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is empty. This is a required argument.\n"); + goto exit; + } + if (sharedby == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + goto exit; + } + if (strlen(sharedby) == 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); goto exit; } - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; atclient_atkey_metadata_set_ispublic(&(atkey->metadata), true); - ret = atclient_atstr_set_literal(&(atkey->name), name, namelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(atkey, key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey->sharedby), sharedby, sharedbylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } if (namespacestr != NULL) { - ret = atclient_atstr_set_literal(&(atkey->namespacestr), namespacestr, namespacestrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } } @@ -359,46 +562,54 @@ int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, con exit: { return ret; } } -int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *namespacestr, const size_t namespacestrlen) { +int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *key, const char *sharedby, + const char *namespacestr) { int ret = 1; - if (name == NULL || sharedby == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "name or sharedby is NULL. These are required arguments.\n"); + if (atkey == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - if (namelen == 0 || sharedbylen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namelen or sharedbylen is 0. These are required arguments.\n"); + if (key == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL. This is a required argument.\n"); goto exit; } - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; + if (strlen(key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is empty. This is a required argument.\n"); + goto exit; + } - ret = atclient_atstr_set_literal(&(atkey->name), "%.*s", (int)namelen, name); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if (sharedby == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); goto exit; } - if (namespacestr != NULL) { - if (namespacestrlen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestrlen is 0. This is a required argument.\n"); - ret = 1; - goto exit; - } - ret = atclient_atstr_set_literal(&(atkey->namespacestr), "%.*s", (int)namespacestrlen, namespacestr); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if (strlen(sharedby) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); + goto exit; + } + + if ((ret = atclient_atkey_set_key(atkey, key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); + goto exit; + } + + if (namespacestr != NULL && strlen(namespacestr) > 0) { + if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } } - ret = atclient_atstr_set_literal(&(atkey->sharedby), "%.*s", (int)sharedbylen, sharedby); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } @@ -407,50 +618,71 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const exit: { return ret; } } -int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *name, const size_t namelen, const char *sharedby, - const size_t sharedbylen, const char *sharedwith, const size_t sharedwithlen, - const char *namespacestr, const size_t namespacestrlen) { +int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *key, const char *sharedby, + const char *sharedwith, const char *namespacestr) { int ret = 1; - if (name == NULL || sharedby == NULL || sharedwith == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "name, sharedby, or sharedwith is NULL. These are required arguments.\n"); + if (atkey == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - if (namelen == 0 || sharedbylen == 0 || sharedwithlen == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "namelen, sharedbylen, or sharedwithlen is 0. These are required arguments.\n"); + if (key == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL. This is a required argument.\n"); goto exit; } - atkey->atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; + if (strlen(key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is empty. This is a required argument.\n"); + goto exit; + } - ret = atclient_atstr_set(&(atkey->sharedwith), sharedwith, sharedwithlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed\n"); + if (sharedby == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); goto exit; } - ret = atclient_atstr_set(&(atkey->name), name, namelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed\n"); + if (strlen(sharedby) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); goto exit; } - if (namespacestr != NULL) { - ret = atclient_atstr_set(&(atkey->namespacestr), namespacestr, namespacestrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed\n"); + if (sharedwith == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL. This is a required argument.\n"); + goto exit; + } + + if (strlen(sharedwith) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is empty. This is a required argument.\n"); + goto exit; + } + + if ((ret = atclient_atkey_set_sharedwith(atkey, sharedwith)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + goto exit; + } + + if ((ret = atclient_atkey_set_key(atkey, key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); + goto exit; + } + + if (namespacestr != NULL && strlen(namespacestr) <= 0) { + if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } } - ret = atclient_atstr_set(&(atkey->sharedby), sharedby, sharedbylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed\n"); + if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/stringutils.c index d13f1dec..92511903 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/stringutils.c @@ -3,6 +3,7 @@ #include #include #include +#include int atclient_stringutils_trim_whitespace(const char *string, const size_t stringlen, char *out, const size_t outsize, size_t *outlen) { @@ -44,59 +45,18 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string exit: { return ret; } } -int atclient_stringutils_starts_with(const char *string, const size_t stringlen, const char *prefix, - const size_t prefixlen) { - int ret = -1; - if (string == NULL || prefix == NULL) { - ret = -1; - goto exit; - } - - if (stringlen == 0 || prefixlen == 0) { - ret = -1; - goto exit; - } - - if (stringlen < prefixlen) { - ret = -1; - goto exit; - } - - ret = strncmp(string, prefix, strlen(prefix)); - if (ret == 0) { - ret = 1; // true - } else if (ret != 0) { - ret = 0; // false - } - - goto exit; -exit: { return ret; } +bool atclient_stringutils_starts_with(const char *string, const char *prefix) { + return strncmp(string, prefix, strlen(prefix)) == 0; } -int atclient_stringutils_ends_with(const char *string, const size_t stringlen, const char *suffix, - const size_t suffixlen) { - int ret = -1; - if (string == NULL || suffix == NULL) { - ret = -1; - goto exit; - } - if (stringlen == 0 || suffixlen == 0) { - ret = -1; - goto exit; - } - if (stringlen < suffixlen) { - ret = -1; - goto exit; - } - ret = strncmp(string + stringlen - suffixlen, suffix, suffixlen); - if (ret == 0) { - ret = 1; // true - } else if (ret != 0) { - ret = 0; // false +bool atclient_stringutils_ends_with(const char *string, const char *suffix) { + const size_t string_len = strlen(string); + const size_t suffix_len = strlen(suffix); + if (suffix_len > string_len) { + return false; } + return strncmp(string + string_len - suffix_len, suffix, suffix_len) == 0; - goto exit; -exit: { return ret; } } int atclient_stringutils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol) { From eafd8e1bd234bd8477c75ad4b602a71c00d21041 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 09:49:13 -0400 Subject: [PATCH 049/193] wip: --- packages/atclient/src/atclient.c | 8 +- .../atclient/src/atclient_get_sharedkey.c | 137 +++- packages/atclient/src/atclient_put.c | 32 +- packages/atclient/src/atkey.c | 4 +- .../atclient/src/encryption_key_helpers.c | 31 +- packages/atclient/src/monitor.c | 2 +- packages/atclient/src/notify.c | 4 +- packages/atclient/tests/test_atkey_create.c | 64 +- .../atclient/tests/test_atkey_from_string.c | 673 ++++++++++-------- .../atclient/tests/test_atkey_to_string.c | 459 ++++++------ packages/atclient/tests/test_stringutils.c | 31 +- tools/run_ctest.sh | 2 +- 12 files changed, 810 insertions(+), 637 deletions(-) diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index e1fe039a..2cd67a54 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -128,7 +128,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } - if(!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", strlen("data:"))) { + if(!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); goto exit; @@ -179,7 +179,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i } // check for data:success - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:success", strlen("data:success"))) { + if (!atclient_stringutils_starts_with((char *)recv, "data:success")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:success\"\n", (int)recvlen, recv); @@ -246,8 +246,8 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { goto exit; } - if (!atclient_stringutils_starts_with((const char *)ptr, recvlen, "data:ok", strlen("data:ok")) && - !atclient_stringutils_ends_with((const char *)ptr, recvlen, "data:ok", strlen("data:ok"))) { + if (!atclient_stringutils_starts_with((const char *)ptr, "data:ok") && + !atclient_stringutils_ends_with((const char *)ptr, "data:ok")) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to receive heartbeat response\n"); ret = -1; goto exit; diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 40497eff..e6a14a9c 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -12,6 +12,88 @@ #define TAG "atclient_get_sharedkey" +static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, char *value, + const size_t valuesize, size_t *valuelen, char *shared_enc_key) { + int ret = 1; + + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (!atclient->_atserver_connection_started) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + goto exit; + } + + if (!atclient->_atsign_is_allocated) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient->_atsign_is_allocated is false\n"); + goto exit; + } + + if (atclient->async_read) { + ret = 1; + atlogger_log( + TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_sharedkey cannot be called from an async_read atclient, it will cause a race condition\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_sharedwith_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is not initialized or is empty\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (valuesize == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + goto exit; + } + + if (valuelen == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + goto exit; + } + + ret = 0; +exit: { return ret; } +} + static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen, char *shared_enc_key, @@ -24,20 +106,15 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen, char *shared_enc_key, const bool create_new_encryption_key_shared_by_me_if_not_found) { - if (atclient->async_read) { - atlogger_log( - TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_sharedkey cannot be called from an async_read atclient, it will cause a race condition\n"); - return 1; - } int ret = 1; - if (atkey->atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY\n"); + if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, valuesize, valuelen, shared_enc_key)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_validate_arguments: %d\n", ret); return ret; } - if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { + if (strcmp(atkey->sharedby, atclient->atsign.atsign) != 0) { // && (!atkey->metadata.iscached && !atkey->metadata.ispublic) ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key); if (ret != 0) { @@ -81,13 +158,13 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke if (enc_key == NULL) { enc_key_mem = 1; enc_key = malloc(45); - if(enc_key == NULL) { + if (enc_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); goto exit; } atclient_atsign recipient; - ret = atclient_atsign_init(&recipient, atkey->sharedwith.str); + ret = atclient_atsign_init(&recipient, atkey->sharedwith); if (ret != 0) { goto exit; } @@ -116,7 +193,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // command_prefix = "llookup:all:" const size_t commandsize = strlen("llookup:all:") + atkeystrlen + strlen("\r\n") + 1; command = malloc(commandsize * sizeof(char)); - if(command == NULL) { + if (command == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); goto exit; @@ -142,23 +219,24 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; response_prefix = malloc(response_prefix_len * sizeof(char)); - if(response_prefix == NULL) { + memset(response_prefix, 0, sizeof(char) * response_prefix_len); + if (response_prefix == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); - if (atclient_stringutils_starts_with(response, recvsize, response_prefix, response_prefix_len)) { + if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; } - if (atclient_stringutils_ends_with(response, recvsize, response_prefix, response_prefix_len)) { + if (atclient_stringutils_ends_with(response, response_prefix)) { response[strlen(response) - response_prefix_len - 1] = '\0'; } // Truncate response : "data:" - if (atclient_stringutils_starts_with(response, recvsize, "data:", 5)) { + if (atclient_stringutils_starts_with(response, "data:")) { response = response + 5; unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; @@ -214,7 +292,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke const size_t valuerawsize = strlen(data->valuestring) * 4; // most likely enough space after base64 decode valueraw = malloc(sizeof(char) * valuerawsize); - if(valueraw == NULL) { + if (valueraw == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); goto exit; @@ -274,13 +352,13 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *enc_key = shared_enc_key; if (enc_key == NULL) { enc_key = malloc(45); - if(enc_key == NULL) { + if (enc_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); goto exit; } atclient_atsign recipient; - ret = atclient_atsign_init(&recipient, atkey->sharedby.str); + ret = atclient_atsign_init(&recipient, atkey->sharedby); if (ret != 0) { goto exit; } @@ -295,9 +373,9 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at size_t namespace_len = 0; short extra_point_len = 0; // "." before namespace - if (atkey->namespacestr.str != NULL && atkey->namespacestr.str[0] != '\0') { - namespace = atkey->namespacestr.str; - namespace_len = atkey->namespacestr.len; + if (atkey->namespacestr != NULL && atkey->namespacestr[0] != '\0') { + namespace = atkey->namespacestr; + namespace_len = strlen(atkey->namespacestr); extra_point_len = 1; } @@ -305,15 +383,14 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at // command_prefix = "lookup:" const short command_prefix_len = 11; const size_t command_len = - command_prefix_len + atkey->name.len + extra_point_len + namespace_len + atkey->sharedby.len + 3; + command_prefix_len + strlen(atkey->key) + extra_point_len + namespace_len + strlen(atkey->sharedby) + 3; command = calloc(command_len, sizeof(char)); - snprintf(command, command_len, "lookup:all:%s%s%s%s\r\n", atkey->name.str, extra_point_len ? "." : "", namespace, - atkey->sharedby.str); + snprintf(command, command_len, "lookup:all:%s%s%s%s\r\n", atkey->key, extra_point_len ? "." : "", namespace, + atkey->sharedby); // send command and recv response const size_t recvsize = 4096; recv = calloc(recvsize, sizeof(unsigned char)); - memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen = 0; ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, strlen((char *)command), @@ -329,23 +406,23 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; response_prefix = malloc(response_prefix_len * sizeof(char)); - if(response_prefix == NULL) { + if (response_prefix == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); - if (atclient_stringutils_starts_with(response, recvlen, response_prefix, response_prefix_len)) { + if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; } - if (atclient_stringutils_ends_with(response, recvlen, response_prefix, response_prefix_len)) { + if (atclient_stringutils_ends_with(response, response_prefix)) { response[strlen(response) - response_prefix_len - 1] = '\0'; } // Truncate response : "data:" - if (atclient_stringutils_starts_with(response, recvlen, "data:", 5)) { + if (atclient_stringutils_starts_with(response, "data:")) { response = response + 5; unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index db61ce26..3adda02b 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -95,11 +95,13 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c * 3b. Build the command */ - if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); + + if (atkey_type == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { // 3a.1 no encryption memcpy(ciphertextbase64, value, valuelen); ciphertextbase64len = valuelen; - } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SELFKEY) { + } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SELFKEY) { // 3a.2 encrypt with self encryption key const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; unsigned char selfencryptionkey[selfencryptionkeysize]; @@ -124,7 +126,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } - } else if (atkey->atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { // 3aA.3 encrypt with shared encryption key // get our AES shared key @@ -132,7 +134,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c // create one for us -> encrypted with our self encryption key // create one for the other person -> encrypted with their public encryption key atclient_atsign recipient; - atclient_atsign_init(&recipient, atkey->sharedwith.str); + atclient_atsign_init(&recipient, atkey->sharedwith); if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, &recipient, sharedenckeybase64, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); @@ -222,7 +224,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); @@ -260,6 +262,18 @@ static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atk goto exit; } + if(!atclient->_atsign_is_allocated) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient's atsign is not allocated\n"); + goto exit; + } + + if(!atclient->_atserver_connection_started) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient's atserver connection is not started\n"); + goto exit; + } + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); @@ -278,7 +292,13 @@ static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atk goto exit; } - if (strncmp(atkey->sharedby.str, atclient->atsign.atsign, atkey->sharedby.len) != 0) { + if(!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not initialized or is empty\n"); + goto exit; + } + + if (strcmp(atkey->sharedby, atclient->atsign.atsign) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); goto exit; diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 48f01a96..20633389 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -61,7 +61,7 @@ size_t atclient_atkey_strlen(const atclient_atkey *atkey) { return len; } -int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, const size_t atkeylen) { +int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { // 6 scenarios: // 1. PublicKey: "public:name.wavi@smoothalligator" // name == "name" @@ -106,7 +106,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr, cons int ret = 1; char *sharedby_withat = NULL; char *saveptr; - char *copy = strndup(atkeystr, atkeylen); + char *copy = strdup(atkeystr); if (copy == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdndup failed\n"); goto exit; diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index a8f18912..57bd3ee6 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -42,17 +42,17 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien snprintf(responseprefix, responseprefixsize, "@%s@", ctx->atsign.without_prefix_str); const short responseprefixlen = (short)strlen(response); - if (atclient_stringutils_starts_with(response, responseprefixlen, responseprefix, responseprefixlen)) { + if (atclient_stringutils_starts_with(response, responseprefix)) { response = response + responseprefixlen; } short responselen = (short)strlen(response); - if (atclient_stringutils_ends_with(response, responselen, responseprefix, responseprefixlen)) { + if (atclient_stringutils_ends_with(response, responseprefix)) { response[responselen - responseprefixlen - 1] = '\0'; } // does my atSign already have the recipient's shared key? - if (atclient_stringutils_starts_with(response, responselen, "data:", 5)) { + if (atclient_stringutils_starts_with(response, "data:")) { response = response + 5; responselen = (short)strlen(response); @@ -83,8 +83,7 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien memcpy(enc_key_shared_by_me, plaintext, plaintextsize); } - else if (atclient_stringutils_starts_with((char *)recv, recvsize, "error:AT0015-key not found", - strlen("error:AT0015-key not found"))) { + else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { // or do I need to create, store and share a new shared key? if (create_new_if_not_found) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Creating new shared encryption key for %s\n", recipient->atsign); @@ -134,16 +133,16 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atcl char response_prefix[response_prefix_len]; snprintf(response_prefix, response_prefix_len, "@%s@", ctx->atsign.without_prefix_str); - if (atclient_stringutils_starts_with(response, recvsize, response_prefix, response_prefix_len)) { + if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; } - if (atclient_stringutils_ends_with(response, recvsize, response_prefix, response_prefix_len)) { + if (atclient_stringutils_ends_with(response, response_prefix)) { response[strlen(response) - response_prefix_len - 1] = '\0'; } // does my atSign already have the recipient's shared key? - if (atclient_stringutils_starts_with(response, recvsize, "data:", 5)) { + if (atclient_stringutils_starts_with(response, "data:")) { response = response + 5; @@ -173,8 +172,7 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atcl } memcpy(enc_key_shared_by_other, plaintext, plaintextlen); - } else if (atclient_stringutils_starts_with((char *)recv, recvsize, "error:AT0015-key not found", - strlen("error:AT0015-key not found"))) { + } else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { // There is nothing we can do, except wait for the recipient to share a new key // We want to mark this situation with a easily distinguishable return value ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; @@ -211,11 +209,10 @@ int atclient_get_public_encryption_key(atclient *ctx, const atclient_atsign *ats char *response = (char *)recv; - if (atclient_stringutils_starts_with(response, recvlen, "data:", 5)) { + if (atclient_stringutils_starts_with(response, "data:")) { response = response + 5; memcpy(public_encryption_key, response, 1024); - } else if (atclient_stringutils_starts_with((char *)recv, recvlen, "error:AT0015-key not found", - strlen("error:AT0015-key not found"))) { + } else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); ret = 1; return ret; @@ -338,7 +335,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie const size_t cmdbuffersize1 = strlen("update:shared_key. \r\n") + strlen(sharedwith->without_prefix_str) + strlen(sharedby->atsign) + 1 + sharedenckeybase64encryptedforuslen; cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1); - if(cmdbuffer1 == NULL) { + if (cmdbuffer1 == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer1\n"); goto exit; @@ -349,7 +346,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie // 5b. for them (update:shared_key.sharedwith@sharedby \r\n) const size_t cmdbuffersize2 = strlen("update::shared_key \r\n") + strlen(sharedby->atsign) + strlen(sharedwith->atsign) + 1 + sharedenckeybase64encryptedforthemlen; - if((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { + if ((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer2\n"); goto exit; @@ -366,7 +363,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie } // check if the key was successfully stored - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); @@ -384,7 +381,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie } // check if the key was successfully stored - if (!atclient_stringutils_starts_with((char *)recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 6866776e..88719755 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -1191,7 +1191,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not // 2. get iv if (atclient_atnotification_ivNonce_is_initialized(notification) && - !atclient_stringutils_starts_with(notification->ivNonce, strlen(notification->ivNonce), "null", strlen("null"))) { + !atclient_stringutils_starts_with(notification->ivNonce, "null")) { size_t ivlen; ret = atchops_base64_decode((unsigned char *)notification->ivNonce, strlen(notification->ivNonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 3abaa4c3..ca41088e 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -110,7 +110,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; unsigned char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); - if ((ret = atclient_atsign_init(&recipient, params->atkey->sharedwith.str)) != 0) { + if ((ret = atclient_atsign_init(&recipient, params->atkey->sharedwith)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with code %d\n", ret); return ret; } @@ -200,7 +200,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific goto exit; } // if starts with data: - if (atclient_stringutils_starts_with((char *)recv, recvlen, "data:", strlen("data:"))) { + if (atclient_stringutils_starts_with((char *)recv, "data:")) { if (notification_id != NULL) { // if not null, then they care about the notification id // parse the notification id char *data = (char *)recv + strlen("data:"); diff --git a/packages/atclient/tests/test_atkey_create.c b/packages/atclient/tests/test_atkey_create.c index 8746e915..6da6ea99 100644 --- a/packages/atclient/tests/test_atkey_create.c +++ b/packages/atclient/tests/test_atkey_create.c @@ -18,28 +18,29 @@ static int test_create_publickey() { const char *expected = "public:test@alice"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_publickey(&atkey, "test", strlen("test"), "@alice", strlen("@alice"), NULL, 0); + ret = atclient_atkey_create_publickey(&atkey, "test", "@alice", NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", - atkey.atkeytype); + atkey_type); ret = 1; goto exit; } - if (strcmp(atkey.name.str, "test") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not test, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "test") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not test, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strcmp(atkey.sharedby.str, "@alice") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @alice, it is \"%s\"\n", - atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @alice, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } @@ -84,28 +85,29 @@ static int test_create_selfkey() { const char *expected = "name@jeremy"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_selfkey(&atkey, "name", strlen("name"), "@jeremy", strlen("@jeremy"), NULL, 0); + ret = atclient_atkey_create_selfkey(&atkey, "name", "@jeremy", NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", - atkey.atkeytype); + atkey_type); ret = 1; goto exit; } - if (strcmp(atkey.name.str, "name") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strcmp(atkey.sharedby.str, "@jeremy") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } @@ -149,43 +151,43 @@ static int test_create_sharedkey() { const char *expected = "@jeremy:name.wavi@chess69lovely"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_sharedkey(&atkey, "name", strlen("name"), "@jeremy", strlen("@jeremy"), "@chess69lovely", - strlen("@chess69lovely"), "wavi", strlen("wavi")); + ret = atclient_atkey_create_sharedkey(&atkey, "name", "@jeremy", "@chess69lovely", "wavi"); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey.atkeytype); + atkey_type); goto exit; } - if (strcmp(atkey.name.str, "name") != 0) { + if (strcmp(atkey.key, "name") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name.str is not name, it is \"%s\"\n", atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strcmp(atkey.sharedby.str, "@jeremy") != 0) { + if (strcmp(atkey.sharedby, "@jeremy") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (strcmp(atkey.sharedwith.str, "@chess69lovely") != 0) { + if (strcmp(atkey.sharedwith, "@chess69lovely") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not @chess69lovely, it is \"%s\"\n", - atkey.sharedwith.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @chess69lovely, it is \"%s\"\n", + atkey.sharedwith); goto exit; } - if (strcmp(atkey.namespacestr.str, "wavi") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwithname.str is not wavi, it is \"%s\"\n", - atkey.sharedwith.str); + if (strcmp(atkey.namespacestr, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwithname is not wavi, it is \"%s\"\n", + atkey.sharedwith); ret = 1; goto exit; } diff --git a/packages/atclient/tests/test_atkey_from_string.c b/packages/atclient/tests/test_atkey_from_string.c index e1af8e27..62a38eab 100644 --- a/packages/atclient/tests/test_atkey_from_string.c +++ b/packages/atclient/tests/test_atkey_from_string.c @@ -31,7 +31,7 @@ #define TEST_ATKEY_FROM_STRING_2F "cached:@jeremy:name.vps1.sshnp@xavier" // Test 3: private hidden keys -// 3A: private hidden key +// 3A: private hidden key without namespace #define TEST_ATKEY_FROM_STRING_3A "_lastnotificationid@alice123_4😘" // Test 4: self keys @@ -40,8 +40,97 @@ // 4B: self key with namespace #define TEST_ATKEY_FROM_STRING_4B "name.wavi@jeremy_0" +static int test1a_cached_publickey_without_namespace(); +static int test1b_publickey_without_namespace(); +static int test1c_publickey_with_namespace(); +static int test1d_cached_publickey_with_namespace(); +static int test2a_sharedkey_with_namespace(); +static int test2b_cached_sharedkey_without_namespace(); +static int test2c_sharedkey_without_namespace(); +static int test2d_cached_sharedkey_with_namespace(); +static int test2e_sharedkey_with_compounding_namespace(); +static int test2f_cached_sharedkey_with_compounding_namespace(); +static int test3a_privatehiddenkey_without_namespace(); +static int test4a_selfkey_without_namespace(); +static int test4b_selfkey_with_namespace(); + +int main() { + int ret = 1; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); + + if ((ret = test1a_cached_publickey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a_cached_publickey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test1b_publickey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b_publickey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test1c_publickey_with_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c_publickey_with_namespace failed\n"); + goto exit; + } + + if ((ret = test1d_cached_publickey_with_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d_cached_publickey_with_namespace failed\n"); + goto exit; + } + + if ((ret = test2a_sharedkey_with_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a_sharedkey_with_namespace failed\n"); + goto exit; + } + + if ((ret = test2b_cached_sharedkey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b_cached_sharedkey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test2c_sharedkey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c_sharedkey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test2d_cached_sharedkey_with_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d_cached_sharedkey_with_namespace failed\n"); + goto exit; + } + + if ((ret = test2e_sharedkey_with_compounding_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2e_sharedkey_with_compounding_namespace failed\n"); + goto exit; + } + + if ((ret = test2f_cached_sharedkey_with_compounding_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2f_cached_sharedkey_with_compounding_namespace failed\n"); + goto exit; + } + + if ((ret = test3a_privatehiddenkey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a_privatehiddenkey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test4a_selfkey_without_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a_selfkey_without_namespace failed\n"); + goto exit; + } + + if ((ret = test4b_selfkey_with_namespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b_selfkey_with_namespace failed\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + // cached:public:publickey@bob -static int test1a() { +static int test1a_cached_publickey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_1A; @@ -50,7 +139,7 @@ static int test1a() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; @@ -67,19 +156,21 @@ static int test1a() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", - atkey.atkeytype); + atkey_type); goto exit; } - if (strncmp(atkey.name.str, "publickey", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "publickey") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.key); goto exit; } - if (strncmp(atkey.sharedby.str, "@bob", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby); goto exit; } ret = 0; @@ -90,16 +181,15 @@ exit: { } } -static int test1b() { +static int test1b_publickey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_1B; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; @@ -118,104 +208,144 @@ static int test1b() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", - atkey.atkeytype); + atkey_type); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized when it should be\n"); goto exit; } - if (strncmp(atkey.name.str, "publickey", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "publickey") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not publickey, it is \"%s\"\n", atkey.key); goto exit; } - if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); goto exit; } - if (atkey.namespacestr.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + if (atclient_atkey_is_sharedby_initialized(&atkey) && strcmp(atkey.sharedby, "@alice") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); + goto exit; + } + + if (!tclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; } ret = 0; goto exit; - exit: { atclient_atkey_free(&atkey); return ret; } } -static int test1c() { +static int test1c_publickey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_1C; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { + if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + goto exit; + } + + if (atkey.metadata.iscached != false) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + goto exit; + } + + if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != 1) { + if (atkey.metadata.ispublic != true) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", atkey.metadata.ispublic); ret = 1; goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", - atkey.atkeytype); + atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if(!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.sharedby.str, "@jeremy", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + if (strcmp(atkey.sharedby, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + ret = 1; + goto exit; + } + if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + goto exit; + } + + if (strcmp(atkey.namespacestr, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not \'wavi\', it is \"%s\"\n", + atkey.namespacestr); + ret = 1; + goto exit; + } + + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized, when it should not be\n"); ret = 1; goto exit; } @@ -228,27 +358,37 @@ exit: { } } -static int test1d() { +static int test1d_cached_publickey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_1D; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } + if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + goto exit; + } + if (atkey.metadata.iscached != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); ret = 1; goto exit; } + if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); + goto exit; + } + if (atkey.metadata.ispublic != 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", atkey.metadata.ispublic); @@ -256,41 +396,58 @@ static int test1d() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", - atkey.atkeytype); + atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if(!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.sharedby.str, "@jeremy", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", - atkey.sharedby.str); + if (strcmp(atkey.key, "name") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { + if (strcmp(atkey.sharedby, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + ret = 1; + goto exit; + } + + if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + goto exit; + } + + if (strcmp(atkey.namespacestr, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr); ret = 1; goto exit; } + if(atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is initialized, when it should not be\n"); + goto exit; + } + ret = 0; goto exit; @@ -300,63 +457,98 @@ exit: { } } -static int test2a() { +static int test2a_sharedkey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2A; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { + if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + goto exit; + } + + if (atkey.metadata.iscached != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); ret = 1; goto exit; } - if (atkey.metadata.ispublic != 0) { + if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); + goto exit; + } + + if (atkey.metadata.ispublic != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", atkey.metadata.ispublic); ret = 1; goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey.atkeytype); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey_type); + ret = 1; + goto exit; + } + + if(!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); + goto exit; + } + + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name.wavi", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name.wavi, it is \"%s\"\n", atkey.name.str); + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.sharedby.str, "@bob", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } - if (strncmp(atkey.sharedwith.str, "@alice", atkey.sharedwith.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @alice, it is \"%s\"\n", - atkey.sharedwith.str); + if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); goto exit; } - if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { + if (strcmp(atkey.sharedwith, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @alice, it is \"%s\"\n", atkey.sharedwith); + ret = 1; + goto exit; + } + + if(!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + goto exit; + } + + if (strcmp(atkey.namespacestr, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr); ret = 1; goto exit; } @@ -369,7 +561,7 @@ exit: { } } -static int test2b() { +static int test2b_cached_sharedkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2B; @@ -378,7 +570,7 @@ static int test2b() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; @@ -397,36 +589,37 @@ static int test2b() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey.atkeytype); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + if (strcmp(atkey.sharedwith, "@bob") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); goto exit; } - if (atkey.namespacestr.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey.namespacestr is initialized when it is not supposed to be\n"); goto exit; } @@ -435,7 +628,7 @@ static int test2b() { exit: { return ret; } } -static int test2c() { +static int test2c_sharedkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2C; @@ -444,7 +637,7 @@ static int test2c() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; @@ -463,36 +656,37 @@ static int test2c() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey.atkeytype); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "name") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } - if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + if (strcmp(atkey.sharedwith, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); ret = 1; goto exit; } - if (atkey.namespacestr.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey.namespacestr is initialized when it is not supposed to be\n"); goto exit; } @@ -504,7 +698,7 @@ exit: { } } -static int test2d() { +static int test2d_cached_sharedkey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2D; @@ -513,54 +707,55 @@ static int test2d() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 1) { + if (atkey.metadata.iscached != true) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); ret = 1; goto exit; } - if (atkey.metadata.ispublic != 0) { + if (atkey.metadata.ispublic != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", atkey.metadata.ispublic); ret = 1; goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey.atkeytype); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey_type); goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "name") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (strncmp(atkey.sharedwith.str, "@bob", atkey.sharedwith.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", - atkey.sharedwith.str); + if (strcmp(atkey.sharedwith, "@bob") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); goto exit; } - if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { + if (strcmp(atkey.namespacestr, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr); ret = 1; goto exit; } @@ -573,39 +768,40 @@ exit: { } } -static int test2e() { +static int test2e_sharedkey_with_compounding_namespace() { int ret = 1; atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2E, strlen(TEST_ATKEY_FROM_STRING_2E))) != 0) { + if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2E)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); goto exit; } // @alice:name.vpsx.sshnp.abcd.efgh@xavierbob123 - if(strcmp(atkey.sharedby.str, "@xavierbob123") != 0) { + if (strcmp(atkey.sharedby, "@xavierbob123") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); goto exit; } - if(strcmp(atkey.sharedwith.str, "@alice") != 0) { + if (strcmp(atkey.sharedwith, "@alice") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); goto exit; } - if(strcmp(atkey.name.str, "name") != 0) { + if (strcmp(atkey.key, "name") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is %s\n", atkey.key); goto exit; } - if(strcmp(atkey.namespacestr.str, "vpsx.sshnp.abcd.efgh") != 0) { + if (strcmp(atkey.namespacestr, "vpsx.sshnp.abcd.efgh") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", atkey.namespacestr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", + atkey.namespacestr); goto exit; } @@ -617,52 +813,53 @@ exit: { } } -static int test2f() { +static int test2f_cached_sharedkey_with_compounding_namespace() { int ret = 1; atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2F, strlen(TEST_ATKEY_FROM_STRING_2F))) != 0) { + if ((ret = atclient_atkey_from_string(&atkey, TEST_ATKEY_FROM_STRING_2F)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed: %d\n", ret); goto exit; } // cached:@jeremy:name.vps1.sshnp@xavier - if(!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized when it should be\n"); goto exit; } - if(atkey.metadata.iscached != true) { + if (atkey.metadata.iscached != true) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is false when it should be true\n"); goto exit; } - if(strcmp(atkey.sharedby.str, "@xavier") != 0) { + if (strcmp(atkey.sharedby, "@xavier") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); goto exit; } - if(strcmp(atkey.sharedwith.str, "@jeremy") != 0) { + if (strcmp(atkey.sharedwith, "@jeremy") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); goto exit; } - if(strcmp(atkey.name.str, "name") != 0) { + if (strcmp(atkey.key, "name") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.key); goto exit; } - if(strcmp(atkey.namespacestr.str, "vps1.sshnp") != 0) { + if (strcmp(atkey.namespacestr, "vps1.sshnp") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", atkey.namespacestr.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", + atkey.namespacestr); goto exit; } @@ -671,16 +868,15 @@ static int test2f() { exit: { return ret; } } -static int test3a() { +static int test3a_privatehiddenkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_3A; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; @@ -699,37 +895,37 @@ static int test3a() { goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey.atkeytype); + "atkey_type is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "_lastnotificationid", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not _lastnotificationid, it is \"%s\"\n", - atkey.name.str); + if (strcmp(atkey.key, "_lastnotificationid") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not _lastnotificationid, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strncmp(atkey.sharedby.str, "@alice123_4😘", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice123_4😘, it is \"%s\"\n", - atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice123_4😘") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice123_4😘, it is \"%s\"\n", + atkey.sharedby); goto exit; } - if (atkey.sharedwith.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); + if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; } - if (atkey.namespacestr.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); + if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); goto exit; } @@ -742,67 +938,66 @@ exit: { } } -static int test4a() { +static int test4a_selfkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_4A; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { + if (atclient_atkey_metadata_is_iscached_initialized(&(atkey.metadata)) && atkey.metadata.iscached != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); ret = 1; goto exit; } - if (atkey.metadata.ispublic != 0) { + if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey.metadata)) && atkey.metadata.ispublic != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", atkey.metadata.ispublic); ret = 1; goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", - atkey.atkeytype); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey_type); ret = 1; goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strncmp(atkey.sharedby.str, "@alice", atkey.sharedby.len) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby.str); + if (strcmp(atkey.sharedby, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } - if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); + if (strlen(atkey.sharedwith) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", + strlen(atkey.sharedwith)); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); + atkey.sharedwith); ret = 1; goto exit; } - if (atkey.namespacestr.len != 0 && strlen(atkey.namespacestr.str) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.len is not 0, it is %lu\n", - atkey.namespacestr.len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr.str is not empty, it is \"%s\"\n", - atkey.namespacestr.str); + if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); goto exit; } @@ -814,66 +1009,64 @@ exit: { } } -static int test4b() { +static int test4b_selfkey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_4B; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr, atkeystrlen); + ret = atclient_atkey_from_string(&atkey, atkeystr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { + if (atkey.metadata.iscached != false) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); goto exit; } - if (atkey.metadata.ispublic != 0) { + if (atkey.metadata.ispublic != false) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", atkey.metadata.ispublic); goto exit; } - if (atkey.atkeytype != ATCLIENT_ATKEY_TYPE_SELFKEY) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", - atkey.atkeytype); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey_type); goto exit; } - if (strncmp(atkey.name.str, "name", atkey.name.len) != 0) { + if (strcmp(atkey.key, "name") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.name.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strncmp(atkey.sharedby.str, "@jeremy_0", atkey.sharedby.len) != 0) { + if (strcmp(atkey.sharedby, "@jeremy_0") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy_0, it is \"%s\"\n", - atkey.sharedby.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy_0, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (atkey.sharedwith.len != 0 && strlen(atkey.sharedwith.str) != 0) { + if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", atkey.sharedwith.len); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith.str); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; } - if (strncmp(atkey.namespacestr.str, "wavi", atkey.namespacestr.len) != 0) { + if (strcmp(atkey.namespacestr, "wavi") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr.str); + atkey.namespacestr); goto exit; } @@ -884,83 +1077,3 @@ exit: { return ret; } } - -int main() { - int ret = 1; - - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); - - // test 1a. cached public key (cached:public:publickey@bob) - if ((ret = test1a()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a failed\n"); - goto exit; - } - - // test 1b. non-cached public key (public:publickey@alice) - if ((ret = test1b()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b failed\n"); - goto exit; - } - - // test 1c - if ((ret = test1c()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c failed\n"); - goto exit; - } - - // test 1d - if ((ret = test1d()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d failed\n"); - goto exit; - } - - // test 2a. non-cached sharedkey with namespace (@alice:name.wavi@bob) - if ((ret = test2a()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a failed\n"); - goto exit; - } - - if ((ret = test2b()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b failed\n"); - goto exit; - } - - if ((ret = test2c()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c failed\n"); - goto exit; - } - - if ((ret = test2d()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d failed\n"); - goto exit; - } - - if ((ret = test2e()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2e failed\n"); - goto exit; - } - - if ((ret = test2f()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2f failed\n"); - goto exit; - } - - if ((ret = test3a()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a failed\n"); - goto exit; - } - - if ((ret = test4a()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a failed\n"); - goto exit; - } - - if ((ret = test4b()) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b failed\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} diff --git a/packages/atclient/tests/test_atkey_to_string.c b/packages/atclient/tests/test_atkey_to_string.c index b7f30d99..bd9e4c07 100644 --- a/packages/atclient/tests/test_atkey_to_string.c +++ b/packages/atclient/tests/test_atkey_to_string.c @@ -35,6 +35,95 @@ // 4B: self key with namespace #define TEST_ATKEY_TO_STRING_4B "name.wavi@jeremy_0" +static int test1a(); +static int test1b(); +static int test1c(); +static int test1d(); +static int test2a(); +static int test2b(); +static int test2c(); +static int test2d(); +static int test3a(); +static int test4a(); +static int test4b(); + +int main() { + int ret = 1; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + ret = test1a(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a failed\n"); + goto exit; + } + + ret = test1b(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b failed\n"); + goto exit; + } + + ret = test1c(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c failed\n"); + goto exit; + } + + ret = test1d(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d failed\n"); + goto exit; + } + + ret = test2a(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a failed\n"); + goto exit; + } + + ret = test2b(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b failed\n"); + goto exit; + } + + ret = test2c(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c failed\n"); + goto exit; + } + + ret = test2d(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d failed\n"); + goto exit; + } + + ret = test3a(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a failed\n"); + goto exit; + } + + ret = test4a(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a failed\n"); + goto exit; + } + + ret = test4b(); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b failed\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + + static int test1a() { int ret = 1; @@ -50,35 +139,29 @@ static int test1a() { atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "publickey"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "publickey")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@bob"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; - goto exit; exit: { atclient_atkey_free(&atkey); free(string); @@ -101,30 +184,24 @@ static int test1b() { const size_t expectedlen = strlen(expected); atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "publickey"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "publickey")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } @@ -151,36 +228,29 @@ static int test1c() { const size_t expectedlen = strlen(expected); atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.namespacestr), "wavi"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@jeremy"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(&(atkey), "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } @@ -188,6 +258,7 @@ static int test1c() { goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c Ended:%d\n", ret); return ret; } @@ -208,43 +279,36 @@ static int test1d() { atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_PUBLICKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.namespacestr), "wavi"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(&(atkey), "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@jeremy"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } ret = 0; - goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d Ended:%d\n", ret); return ret; } @@ -261,44 +325,41 @@ static int test2a() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2A; // "@alice:name.wavi@bob" - const size_t expectedlen = strlen(expected); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; - - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@bob"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.namespacestr), "wavi"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedwith(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } + if ((ret = strcmp(string, expected)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); + goto exit; + } + ret = 0; - goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a Ended:%d\n", ret); return ret; } @@ -315,39 +376,31 @@ static int test2b() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2B; // "cached:@bob:name@alice" - const size_t expectedlen = strlen(expected); atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; - ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@bob"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } @@ -355,6 +408,7 @@ static int test2b() { goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b Ended:%d\n", ret); return ret; } @@ -371,45 +425,36 @@ static int test2c() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2C; // "@bob:name@alice" - const size_t expectedlen = strlen(expected); - - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@bob"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } // namespace should be empty - if (atkey.namespacestr.len > 0 || strlen(atkey.namespacestr.str) > 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr.len > 0: %d\n", atkey.namespacestr.len); + if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } @@ -433,52 +478,43 @@ static int test2d() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_2D; // "cached:@bob:name.wavi@alice" - const size_t expectedlen = strlen(expected); atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SHAREDKEY; - ret = atclient_atstr_set_literal(&(atkey.sharedwith), "@bob"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.namespacestr), "wavi"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); - ret = 1; goto exit; } ret = 0; - goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d Ended:%d\n", ret); return ret; } @@ -495,46 +531,44 @@ static int test3a() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_3A; // "_lastnotificationid@alice123_4😘" - const size_t expectedlen = strlen(expected); - - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "_lastnotificationid"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "_lastnotificationid")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice123_4😘"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice123_4😘")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); + if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); goto exit; } - if (atkey.namespacestr.len > 0 || strlen(atkey.namespacestr.str) > 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr.len > 0: %d or strlen(%s) > 0\n", - atkey.namespacestr.len, atkey.namespacestr.str); + if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); + goto exit; + } + + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; } ret = 0; - goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a Ended:%d\n", ret); return ret; } @@ -551,37 +585,35 @@ static int test4a() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_4A; // "name@alice" - const size_t expectedlen = strlen(expected); - - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; - ret = atclient_atstr_set_literal(&(atkey.name), "name"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - ret = atclient_atstr_set_literal(&(atkey.sharedby), "@alice"); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed\n"); + if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = atclient_atkey_to_string(&atkey, &string); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &string)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - if (atkey.namespacestr.len > 0 || strlen(atkey.namespacestr.str) > 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr.len > 0: %d or strlen(%s) > 0\n", - atkey.namespacestr.len, atkey.namespacestr.str); + if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); goto exit; } - ret = strcmp(string, expected); - if (ret != 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); + goto exit; + } + + if ((ret = strcmp(string, expected)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, string); ret = 1; goto exit; @@ -591,6 +623,7 @@ static int test4a() { goto exit; exit: { free(string); + atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a Ended:%d\n", ret); return ret; } @@ -604,109 +637,41 @@ static int test4b() { atclient_atkey atkey; atclient_atkey_init(&atkey); - atclient_atstr_set_literal(&(atkey.name), "name"); - atclient_atstr_set_literal(&(atkey.namespacestr), "wavi"); - atclient_atstr_set_literal(&(atkey.sharedby), "@jeremy_0"); - atkey.atkeytype = ATCLIENT_ATKEY_TYPE_SELFKEY; - const char *expected = TEST_ATKEY_TO_STRING_4B; // "name.wavi@jeremy_0" char *atkeystr = NULL; - ret = atclient_atkey_to_string(&atkey, &atkeystr); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); + if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - if (strcmp(atkeystr, expected) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, atkeystr); - ret = 1; + if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy_0")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); goto exit; } - ret = 0; - goto exit; -exit: { - atclient_atkey_free(&atkey); - free(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Ended:%d\n", ret); - return ret; -} -} - -int main() { - int ret = 1; - - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - - ret = test1a(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a failed\n"); + if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; } - ret = test1b(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b failed\n"); - goto exit; - } - - ret = test1c(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c failed\n"); - goto exit; - } - - ret = test1d(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d failed\n"); - goto exit; - } - - ret = test2a(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a failed\n"); - goto exit; - } - - ret = test2b(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b failed\n"); - goto exit; - } - - ret = test2c(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c failed\n"); - goto exit; - } - - ret = test2d(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d failed\n"); - goto exit; - } - - ret = test3a(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a failed\n"); - goto exit; - } - - ret = test4a(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a failed\n"); + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string failed\n"); goto exit; } - ret = test4b(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b failed\n"); + if ((ret = strcmp(atkeystr, expected)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "expected: \"%s\", actual: \"%s\"\n", expected, atkeystr); goto exit; } ret = 0; - goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + free(atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Ended:%d\n", ret); + return ret; +} } + diff --git a/packages/atclient/tests/test_stringutils.c b/packages/atclient/tests/test_stringutils.c index b53cbff8..bb64cda0 100644 --- a/packages/atclient/tests/test_stringutils.c +++ b/packages/atclient/tests/test_stringutils.c @@ -29,19 +29,19 @@ int main() { int startswith; // 1a. @bob starts with @ - startswith = atclient_stringutils_starts_with(string, strlen(string), "@", strlen("@")); + startswith = atclient_stringutils_starts_with(string, "@"); if (startswith != 1) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "@"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, + string, "@"); ret = 1; goto exit; } // 1b. @bob does not start with 123 - startswith = atclient_stringutils_starts_with(string, strlen(string), "123", strlen("123")); + startswith = atclient_stringutils_starts_with(string, "123"); if (startswith != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "bob"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, + string, "bob"); ret = 1; goto exit; } @@ -49,19 +49,19 @@ int main() { int endswith; strcpy(string, "root.atsign.org:64"); // 2a. root.atsign.org:64 ends with 64 - endswith = atclient_stringutils_ends_with(string, strlen(string), "64", strlen("64")); + endswith = atclient_stringutils_ends_with(string, "64"); if (endswith != 1) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", - ret, string, "64"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, + string, "64"); ret = 1; goto exit; } // 2b. root.atsign.org:64 does not end with org - endswith = atclient_stringutils_ends_with(string, strlen(string), "org", strlen("org")); + endswith = atclient_stringutils_ends_with(string, "org"); if (endswith != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", - ret, string, "org"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, + string, "org"); ret = 1; goto exit; } @@ -71,15 +71,14 @@ int main() { const char *expectedresult = "scan jeremy_0"; ret = atclient_stringutils_trim_whitespace(string, strlen(string), out, outsize, &outlen); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: %d | %s\n", ret, - string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: %d | %s\n", ret, string); ret = 1; goto exit; } if (strncmp(out, expectedresult, strlen(expectedresult)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: \"%s\" != \"%s\"\n", - string, expectedresult); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: \"%s\" != \"%s\"\n", string, + expectedresult); ret = 1; goto exit; } diff --git a/tools/run_ctest.sh b/tools/run_ctest.sh index f467c845..5b89deb1 100755 --- a/tools/run_ctest.sh +++ b/tools/run_ctest.sh @@ -4,7 +4,7 @@ FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[0]}")" SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")" cd "$SCRIPT_DIRECTORY" cd .. -cmake -S . -B build -DATSDK_BUILD_TESTS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON +cmake -S . -B build -DATSDK_BUILD_TESTS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug cmake --build build --target all run_test() { From 9c99be3804d8a57faf988457f0f0feb4a8a03c03 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:03:38 -0400 Subject: [PATCH 050/193] fix: failing tests in test_atkey_create.c --- packages/atclient/src/atkey.c | 2 +- packages/atclient/tests/test_atkey_create.c | 137 +++++++++++++++----- 2 files changed, 103 insertions(+), 36 deletions(-) diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 20633389..1a0122f7 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -674,7 +674,7 @@ int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *key, cons goto exit; } - if (namespacestr != NULL && strlen(namespacestr) <= 0) { + if (namespacestr != NULL && strlen(namespacestr) > 0) { if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); goto exit; diff --git a/packages/atclient/tests/test_atkey_create.c b/packages/atclient/tests/test_atkey_create.c index 6da6ea99..3e6bdda2 100644 --- a/packages/atclient/tests/test_atkey_create.c +++ b/packages/atclient/tests/test_atkey_create.c @@ -7,9 +7,40 @@ #define TAG "test_atkey_create" -static int test_create_publickey() { +static int test1_create_publickey(); +static int test2_create_selfkey(); +static int test3_create_sharedkey(); + +int main() { int ret = 1; + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + if ((ret = test1_create_publickey()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1_create_publickey: %d\n", ret); + goto exit; + } + + if ((ret = test2_create_selfkey()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2_create_selfkey: %d\n", ret); + goto exit; + } + + if ((ret = test3_create_sharedkey()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3_create_sharedkey: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int test1_create_publickey() { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1_create_publickey Begin\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -18,8 +49,7 @@ static int test_create_publickey() { const char *expected = "public:test@alice"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_publickey(&atkey, "test", "@alice", NULL); - if (ret != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, "test", "@alice", NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } @@ -33,20 +63,31 @@ static int test_create_publickey() { goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey key is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "test") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not test, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@alice") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @alice, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } - ret = atclient_atkey_to_string(&atkey, &atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); ret = 1; goto exit; @@ -66,17 +107,19 @@ static int test_create_publickey() { } ret = 0; - goto exit; exit: { atclient_atkey_free(&atkey); free(atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1_create_publickey End: %d\n", ret); return ret; } } -static int test_create_selfkey() { +static int test2_create_selfkey() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2_create_selfkey Begin\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -100,24 +143,36 @@ static int test_create_selfkey() { goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey key is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@jeremy") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); ret = 1; goto exit; } - ret = atclient_atkey_to_string(&atkey, &atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrolen = strlen(atkeystr); if (strcmp(atkeystr, expected) != 0) { @@ -127,8 +182,8 @@ static int test_create_selfkey() { } if (atkeystrolen != expectedlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, atkeystrolen); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeystrolen is not %lu, it is %lu\n", expectedlen, atkeystrolen); goto exit; } @@ -136,13 +191,17 @@ static int test_create_selfkey() { goto exit; exit: { free(atkeystr); + atclient_atkey_free(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2_create_selfkey End: %d\n", ret); return ret; } } -static int test_create_sharedkey() { +static int test3_create_sharedkey() { int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3_create_sharedkey Begin\n"); + atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -151,8 +210,7 @@ static int test_create_sharedkey() { const char *expected = "@jeremy:name.wavi@chess69lovely"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_sharedkey(&atkey, "name", "@jeremy", "@chess69lovely", "wavi"); - if (ret != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, "name", "@chess69lovely", "@jeremy", "wavi")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); goto exit; } @@ -166,25 +224,49 @@ static int test_create_sharedkey() { goto exit; } + if(!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey key is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strcmp(atkey.sharedby, "@jeremy") != 0) { + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + goto exit; + } + + if (strcmp(atkey.sharedby, "@chess69lovely") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @chess69lovely, it is \"%s\"\n", atkey.sharedby); goto exit; } - if (strcmp(atkey.sharedwith, "@chess69lovely") != 0) { + if(!atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @chess69lovely, it is \"%s\"\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedwith is not initialized when it should be\n"); + goto exit; + } + + if (strcmp(atkey.sharedwith, "@jeremy") != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is \"%s\"\n", atkey.sharedwith); goto exit; } + if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey namespace is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.namespacestr, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwithname is not wavi, it is \"%s\"\n", atkey.sharedwith); @@ -192,12 +274,12 @@ static int test_create_sharedkey() { goto exit; } - ret = atclient_atkey_to_string(&atkey, &atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } + const size_t atkeystrolen = strlen(atkeystr); if (strcmp(atkeystr, expected) != 0) { @@ -216,22 +298,7 @@ static int test_create_sharedkey() { goto exit; exit: { free(atkeystr); + atclient_atkey_free(&atkey); return ret; } } - -int main() { - int ret = 1; - - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - - ret = test_create_publickey(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_create_publickey: %d\n", ret); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} From c1e3bd2fd3df08a04e36b647d87bb27cf52f404b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:10:50 -0400 Subject: [PATCH 051/193] fix: test_atkey_from_string failing tests --- .../atclient/tests/test_atkey_from_string.c | 348 ++++++++++-------- 1 file changed, 199 insertions(+), 149 deletions(-) diff --git a/packages/atclient/tests/test_atkey_from_string.c b/packages/atclient/tests/test_atkey_from_string.c index 62a38eab..b231fbe4 100644 --- a/packages/atclient/tests/test_atkey_from_string.c +++ b/packages/atclient/tests/test_atkey_from_string.c @@ -189,15 +189,15 @@ static int test1b_publickey_without_namespace() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + + if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); goto exit; } @@ -247,7 +247,7 @@ static int test1b_publickey_without_namespace() { goto exit; } - if (!tclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; @@ -274,19 +274,7 @@ static int test1c_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); - goto exit; - } - - if (atkey.metadata.iscached != false) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); - goto exit; - } - - if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { + if (!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); goto exit; @@ -308,7 +296,7 @@ static int test1c_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_is_key_initialized(&atkey)) { + if (!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; @@ -320,7 +308,7 @@ static int test1c_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; @@ -331,7 +319,7 @@ static int test1c_publickey_with_namespace() { ret = 1; goto exit; } - if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); goto exit; @@ -371,28 +359,28 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { + if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); goto exit; } - if (atkey.metadata.iscached != 1) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + if (atkey.metadata.iscached != true) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); goto exit; } - if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { + if (!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != 1) { + if (atkey.metadata.ispublic != true) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", atkey.metadata.ispublic); - ret = 1; goto exit; } @@ -405,7 +393,7 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_is_key_initialized(&atkey)) { + if (!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; @@ -417,7 +405,7 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; @@ -429,7 +417,7 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); goto exit; @@ -442,9 +430,9 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if(atclient_atkey_is_sharedby_initialized(&atkey)) { + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is initialized, when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized, when it should not be\n"); goto exit; } @@ -470,31 +458,6 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if(!atclient_atkey_metadata_is_iscached_initialized(&atkey)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); - goto exit; - } - - if (atkey.metadata.iscached != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); - ret = 1; - goto exit; - } - - if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); - goto exit; - } - - if (atkey.metadata.ispublic != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); - ret = 1; - goto exit; - } - const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { @@ -504,7 +467,7 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if(!atclient_atkey_is_key_initialized(&atkey)) { + if (!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; @@ -516,7 +479,7 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; @@ -528,7 +491,7 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); goto exit; @@ -540,7 +503,7 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if(!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); goto exit; @@ -565,27 +528,24 @@ static int test2b_cached_sharedkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2B; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 1) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + if (atkey.metadata.iscached != true) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); goto exit; } @@ -598,18 +558,36 @@ static int test2b_cached_sharedkey_without_namespace() { goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@alice") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); goto exit; } + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedwith, "@bob") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); @@ -625,49 +603,49 @@ static int test2b_cached_sharedkey_without_namespace() { ret = 0; goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + return ret; +} } static int test2c_sharedkey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2C; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atkey_type); ret = 1; goto exit; } - if (atkey.metadata.ispublic != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + if (!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", - atkey_type); + if (strcmp(atkey.key, "name") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strcmp(atkey.key, "name") != 0) { + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is \"%s\"\n", atkey.key); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } @@ -677,6 +655,12 @@ static int test2c_sharedkey_without_namespace() { goto exit; } + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedwith, "@bob") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); ret = 1; @@ -702,26 +686,23 @@ static int test2d_cached_sharedkey_with_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_2D; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + if (atkey.metadata.iscached != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); ret = 1; goto exit; } @@ -735,24 +716,48 @@ static int test2d_cached_sharedkey_with_namespace() { goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@alice") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); goto exit; } + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedwith, "@bob") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); goto exit; } + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + goto exit; + } + if (strcmp(atkey.namespacestr, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", atkey.namespacestr); @@ -779,25 +784,49 @@ static int test2e_sharedkey_with_compounding_namespace() { goto exit; } - // @alice:name.vpsx.sshnp.abcd.efgh@xavierbob123 + + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@xavierbob123") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); goto exit; } + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedwith, "@alice") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is %s\n", atkey.key); goto exit; } + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.namespacestr, "vpsx.sshnp.abcd.efgh") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", @@ -824,8 +853,6 @@ static int test2f_cached_sharedkey_with_compounding_namespace() { goto exit; } - // cached:@jeremy:name.vps1.sshnp@xavier - if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized when it should be\n"); @@ -838,24 +865,48 @@ static int test2f_cached_sharedkey_with_compounding_namespace() { goto exit; } + if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedby, "@xavier") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); goto exit; } + if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.sharedwith, "@jeremy") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); goto exit; } + if (!atclient_atkey_is_key_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.key, "name") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.name is not name, it is %s\n", atkey.key); goto exit; } + if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized when it should be\n"); + goto exit; + } + if (strcmp(atkey.namespacestr, "vps1.sshnp") != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", @@ -865,7 +916,10 @@ static int test2f_cached_sharedkey_with_compounding_namespace() { ret = 0; goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + return ret; +} } static int test3a_privatehiddenkey_without_namespace() { @@ -876,37 +930,35 @@ static int test3a_privatehiddenkey_without_namespace() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atkey_type is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey_type); ret = 1; goto exit; } - if (atkey.metadata.ispublic != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + if(!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey_type is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey_type); + if (strcmp(atkey.key, "_lastnotificationid") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not _lastnotificationid, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strcmp(atkey.key, "_lastnotificationid") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not _lastnotificationid, it is \"%s\"\n", atkey.key); + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } @@ -917,13 +969,13 @@ static int test3a_privatehiddenkey_without_namespace() { goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); goto exit; @@ -946,37 +998,35 @@ static int test4a_selfkey_without_namespace() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atclient_atkey_metadata_is_iscached_initialized(&(atkey.metadata)) && atkey.metadata.iscached != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey_type); goto exit; } - if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey.metadata)) && atkey.metadata.ispublic != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + if(!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", - atkey_type); + if (strcmp(atkey.key, "name") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); ret = 1; goto exit; } - if (strcmp(atkey.key, "name") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } @@ -986,16 +1036,13 @@ static int test4a_selfkey_without_namespace() { goto exit; } - if (strlen(atkey.sharedwith) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.len is not 0, it is %lu\n", - strlen(atkey.sharedwith)); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith.str is not empty, it is \"%s\"\n", - atkey.sharedwith); + if(atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); goto exit; @@ -1017,37 +1064,34 @@ static int test4b_selfkey_with_namespace() { atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey.metadata.iscached != false) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atkey_type); goto exit; } - if (atkey.metadata.ispublic != false) { + if(!atclient_atkey_is_key_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 0, it is %d\n", - atkey.metadata.ispublic); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized, when it should be\n"); goto exit; } - const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + if (strcmp(atkey.key, "name") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", - atkey_type); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); goto exit; } - if (strcmp(atkey.key, "name") != 0) { + if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not name, it is \"%s\"\n", atkey.key); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); goto exit; } @@ -1057,9 +1101,9 @@ static int test4b_selfkey_with_namespace() { goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { + if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); goto exit; } @@ -1070,6 +1114,12 @@ static int test4b_selfkey_with_namespace() { goto exit; } + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + goto exit; + } + ret = 0; goto exit; exit: { From 75e01010a46d6fb1c62c4f990e68cf7bef6f5b33 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:27:41 -0400 Subject: [PATCH 052/193] feat: update void returns to int in metadata.h/.c --- packages/atclient/include/atclient/metadata.h | 18 ++++++------- packages/atclient/src/metadata.c | 27 ++++++++++++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 82120c64..285b6525 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -375,15 +375,15 @@ size_t atclient_atkey_metadata_ivnonce_strlen(const atclient_atkey_metadata *met size_t atclient_atkey_metadata_skeenckeyname_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_skeencalgo_strlen(const atclient_atkey_metadata *metadata); -void atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); -void atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden); -void atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached); -void atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl); -void atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb); -void atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const long ttr); -void atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd); -void atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary); -void atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted); +int atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); +int atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden); +int atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached); +int atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl); +int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb); +int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const long ttr); +int atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd); +int atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary); +int atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted); int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, const size_t datasignaturelen); int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus, diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 929b0ed8..0ff1d236 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -977,67 +977,76 @@ bool atclient_atkey_metadata_is_skeencalgo_initialized(const atclient_atkey_meta return is_skeencalgo_initialized(metadata); } -void atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic) { +int atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic) { if (is_ispublic_initialized(metadata)) { unset_ispublic(metadata); } set_ispublic(metadata, ispublic); + return 0; } -void atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden) { +int atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden) { if (is_ishidden_initialized(metadata)) { unset_ishidden(metadata); } set_ishidden(metadata, ishidden); + return 0; } -void atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { +int atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { if (is_iscached_initialized(metadata)) { unset_iscached(metadata); } set_iscached(metadata, iscached); + return 0; } -void atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl) { +int atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl) { if (is_ttl_initialized(metadata)) { unset_ttl(metadata); } set_ttl(metadata, ttl); + return 0; } -void atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb) { +int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb) { if (is_ttb_initialized(metadata)) { unset_ttb(metadata); } set_ttb(metadata, ttb); + return 0; } -void atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const long ttr) { +int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const long ttr) { if (is_ttr_initialized(metadata)) { unset_ttr(metadata); } set_ttr(metadata, ttr); + return 0; } -void atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd) { +int atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd) { if (is_ccd_initialized(metadata)) { unset_ccd(metadata); } set_ccd(metadata, ccd); + return 0; } -void atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary) { +int atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary) { if (is_isbinary_initialized(metadata)) { unset_isbinary(metadata); } set_isbinary(metadata, isbinary); + return 0; } -void atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted) { +int atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted) { if (is_isencrypted_initialized(metadata)) { unset_isencrypted(metadata); } set_isencrypted(metadata, isencrypted); + return 0; } int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, From b3f422af7078012722a3a3e90bc49ab9d6195bca Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:27:48 -0400 Subject: [PATCH 053/193] fix: failing tests in test_atkey_to_string.c --- .../atclient/tests/test_atkey_to_string.c | 194 ++++++++++-------- 1 file changed, 108 insertions(+), 86 deletions(-) diff --git a/packages/atclient/tests/test_atkey_to_string.c b/packages/atclient/tests/test_atkey_to_string.c index bd9e4c07..615fa158 100644 --- a/packages/atclient/tests/test_atkey_to_string.c +++ b/packages/atclient/tests/test_atkey_to_string.c @@ -17,6 +17,7 @@ #define TEST_ATKEY_TO_STRING_1C "public:name.wavi@jeremy" // 1D. cached public key with namespace #define TEST_ATKEY_TO_STRING_1D "cached:public:name.wavi@jeremy" + // Test 2: shared keys // 2A: non-cached shared key with namespace #define TEST_ATKEY_TO_STRING_2A "@alice:name.wavi@bob" @@ -26,95 +27,97 @@ #define TEST_ATKEY_TO_STRING_2C "@bob:name@alice" // 2D: cached shared key with namespace #define TEST_ATKEY_TO_STRING_2D "cached:@bob:name.wavi@alice" + // Test 3: private hidden keys // 3A: private hidden key #define TEST_ATKEY_TO_STRING_3A "_lastnotificationid@alice123_4😘" + // Test 4: self keys // 4A: self key with no namespace #define TEST_ATKEY_TO_STRING_4A "name@alice" // 4B: self key with namespace #define TEST_ATKEY_TO_STRING_4B "name.wavi@jeremy_0" -static int test1a(); -static int test1b(); -static int test1c(); -static int test1d(); -static int test2a(); -static int test2b(); -static int test2c(); -static int test2d(); -static int test3a(); -static int test4a(); -static int test4b(); +static int test1a_cached_publickey_without_namespace(); +static int test1b_publickey_without_namespace(); +static int test1c_publickey_with_namespace(); +static int test1d_cached_publickey_with_namespace(); +static int test2a_sharedkey_with_namespace(); +static int test2b_cached_sharedkey_without_namespace(); +static int test2c_sharedkey_without_namespace(); +static int test2d_cached_sharedkey_with_namespace(); +static int test3a_privatehiddenkey(); +static int test4a_selfkey_without_namespace(); +static int test4b_selfkey_with_namespace(); int main() { int ret = 1; atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - ret = test1a(); + ret = test1a_cached_publickey_without_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1a_cached_publickey_without_namespace failed\n"); goto exit; } - ret = test1b(); + ret = test1b_publickey_without_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1b_publickey_without_namespace failed\n"); goto exit; } - ret = test1c(); + ret = test1c_publickey_with_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1c_publickey_with_namespace failed\n"); goto exit; } - ret = test1d(); + ret = test1d_cached_publickey_with_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test1d_cached_publickey_with_namespace failed\n"); goto exit; } - ret = test2a(); + ret = test2a_sharedkey_with_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2a_sharedkey_with_namespace failed\n"); goto exit; } - ret = test2b(); + ret = test2b_cached_sharedkey_without_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2b_cached_sharedkey_without_namespace failed\n"); goto exit; } - ret = test2c(); + ret = test2c_sharedkey_without_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2c_sharedkey_without_namespace failed\n"); goto exit; } - ret = test2d(); + ret = test2d_cached_sharedkey_with_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test2d_cached_sharedkey_with_namespace failed\n"); goto exit; } - ret = test3a(); + ret = test3a_privatehiddenkey(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test3a_privatehiddenkey failed\n"); goto exit; } - ret = test4a(); + ret = test4a_selfkey_without_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4a_selfkey_without_namespace failed\n"); goto exit; } - ret = test4b(); + ret = test4b_selfkey_with_namespace(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test4b_selfkey_with_namespace failed\n"); goto exit; } @@ -123,11 +126,10 @@ int main() { exit: { return ret; } } - -static int test1a() { +static int test1a_cached_publickey_without_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a_cached_publickey_without_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -135,10 +137,16 @@ static int test1a() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1A; - const size_t expectedlen = strlen(expected); - atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); - atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); + if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + goto exit; + } + + if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_key(&atkey, "publickey")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); @@ -165,15 +173,15 @@ static int test1a() { exit: { atclient_atkey_free(&atkey); free(string); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1a_cached_publickey_without_namespace Ended:%d\n", ret); return ret; } } -static int test1b() { +static int test1b_publickey_without_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b_publickey_without_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -181,9 +189,11 @@ static int test1b() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1B; // "public:publickey@alice" - const size_t expectedlen = strlen(expected); - atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); + if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_key(&atkey, "publickey")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); @@ -209,15 +219,15 @@ static int test1b() { goto exit; exit: { free(string); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1b_publickey_without_namespace Ended:%d\n", ret); return ret; } } -static int test1c() { +static int test1c_publickey_with_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c_publickey_with_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -225,9 +235,11 @@ static int test1c() { char *string = NULL; const char *expected = TEST_ATKEY_TO_STRING_1C; // "public:name.wavi@jeremy" - const size_t expectedlen = strlen(expected); - atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); + if ((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); @@ -255,19 +267,18 @@ static int test1c() { } ret = 0; - goto exit; exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1c_publickey_with_namespace Ended:%d\n", ret); return ret; } } -static int test1d() { +static int test1d_cached_publickey_with_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d_cached_publickey_with_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -277,8 +288,15 @@ static int test1d() { const char *expected = TEST_ATKEY_TO_STRING_1D; // "cached:public:name.wavi@jeremy" const size_t expectedlen = strlen(expected); - atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); - atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true); + if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + goto exit; + } + + if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_key(&atkey, "name")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); @@ -309,15 +327,15 @@ static int test1d() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test1d_cached_publickey_with_namespace Ended:%d\n", ret); return ret; } } -static int test2a() { +static int test2a_sharedkey_with_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a_sharedkey_with_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -360,15 +378,15 @@ static int test2a() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2a_sharedkey_with_namespace Ended:%d\n", ret); return ret; } } -static int test2b() { +static int test2b_cached_sharedkey_without_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b_cached_sharedkey_without_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -377,7 +395,10 @@ static int test2b() { const char *expected = TEST_ATKEY_TO_STRING_2B; // "cached:@bob:name@alice" - atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); + if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); @@ -409,15 +430,15 @@ static int test2b() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2b_cached_sharedkey_without_namespace Ended:%d\n", ret); return ret; } } -static int test2c() { +static int test2c_sharedkey_without_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c_sharedkey_without_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -446,8 +467,7 @@ static int test2c() { goto exit; } - // namespace should be empty - if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); goto exit; @@ -462,15 +482,15 @@ static int test2c() { goto exit; exit: { free(string); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2c_sharedkey_without_namespace Ended:%d\n", ret); return ret; } } -static int test2d() { +static int test2d_cached_sharedkey_with_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d_cached_sharedkey_with_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -479,7 +499,10 @@ static int test2d() { const char *expected = TEST_ATKEY_TO_STRING_2D; // "cached:@bob:name.wavi@alice" - atclient_atkey_metadata_set_iscached(&(atkey.metadata), true); + if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + goto exit; + } if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); @@ -515,15 +538,15 @@ static int test2d() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test2d_cached_sharedkey_with_namespace Ended:%d\n", ret); return ret; } } -static int test3a() { +static int test3a_privatehiddenkey() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a_privatehiddenkey Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -542,13 +565,13 @@ static int test3a() { goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); goto exit; } - if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); goto exit; @@ -569,15 +592,15 @@ static int test3a() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test3a_privatehiddenkey Ended:%d\n", ret); return ret; } } -static int test4a() { +static int test4a_selfkey_without_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a_selfkey_without_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -601,13 +624,13 @@ static int test4a() { goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey) || strlen(atkey.sharedwith) > 0) { + if (atclient_atkey_is_sharedwith_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); goto exit; } - if (atclient_atkey_is_namespacestr_initialized(&atkey) || strlen(atkey.namespacestr) > 0) { + if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); goto exit; @@ -624,15 +647,15 @@ static int test4a() { exit: { free(string); atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4a_selfkey_without_namespace Ended:%d\n", ret); return ret; } } -static int test4b() { +static int test4b_selfkey_with_namespace() { int ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Starting...\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b_selfkey_with_namespace Starting...\n"); atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -670,8 +693,7 @@ static int test4b() { exit: { atclient_atkey_free(&atkey); free(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b Ended:%d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test4b_selfkey_with_namespace Ended:%d\n", ret); return ret; } } - From 705c64ecbea0171fd43293660bf598561ada559d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:32:25 -0400 Subject: [PATCH 054/193] fix: include stdint.h --- packages/atclient/include/atclient/atkey.h | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index c61d151e..351b17f5 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -4,6 +4,7 @@ #include "atclient/atstr.h" #include "atclient/metadata.h" #include +#include #define VALUE_INITIALIZED 0b00000001 From 705d5534c5c02536a57c7f662b0e084caed297fc Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Thu, 18 Jul 2024 10:33:46 -0400 Subject: [PATCH 055/193] fix: --- packages/atclient/include/atclient/atkey.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index 351b17f5..3c4a7c71 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -31,7 +31,7 @@ typedef struct atclient_atkey { char *sharedby; char *sharedwith; - u_int8_t _initializedfields[1]; // internal field to track which fields are allocated + uint8_t _initializedfields[1]; // internal field to track which fields are allocated atclient_atkey_metadata metadata; From 450a1a88137fc76f4682459b59fbb0ffde49ca84 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 12:57:51 -0400 Subject: [PATCH 056/193] chore: clean up atclient_get_publickey --- packages/atclient/src/atclient_get_publickey.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index d643a8c9..72e3c016 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -41,8 +41,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu /* * 3. Build `plookup:` command */ - ret = atclient_atkey_to_string(atkey, &atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } @@ -122,8 +121,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu metadatastr = cJSON_Print(metadata); - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr)); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; } From b06f004f8e4c5d6fbc0d19c36a1753dc6e3c9681 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 12:58:03 -0400 Subject: [PATCH 057/193] chore: clean up atclient_get_selfkey.c --- packages/atclient/src/atclient_get_selfkey.c | 83 +++++++++++--------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 9b51bd8f..db6d813e 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -14,7 +14,8 @@ #define TAG "atclient_get_selfkey" -static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t valuesize, const size_t *valuelen); +static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, + const char *value, const size_t valuesize, const size_t *valuelen); int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen) { @@ -23,7 +24,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, /* * 1. Validate arguments */ - if((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, valuesize, valuelen)) != 0) { + if ((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, valuesize, valuelen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey_valid_arguments: %d\n", ret); return ret; } @@ -45,39 +46,44 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; + // free later cJSON *root = NULL; char *cmdbuffer = NULL; char *valueraw = NULL; - // 2. build llookup: command - ret = atclient_atkey_to_string(atkey, &atkeystr); - if (ret != 0) { + /* + * 3. Build `llookup:` command + */ + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - + const size_t atkeystrlen = strlen(atkeystr); const size_t cmdbuffersize = strlen("llookup:all:\r\n") + atkeystrlen + 1; cmdbuffer = (char *)malloc(sizeof(char) * cmdbuffersize); - if(cmdbuffer == NULL) { + if (cmdbuffer == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); goto exit; } memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); - snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int) atkeystrlen, atkeystr); + snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int)atkeystrlen, atkeystr); - // 3. send llookup: command - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, - recvsize, &recvlen); - if (ret != 0) { + /* + * 4. Send `llookup:` command + */ + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, + recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - // 4. parse response + /* + * 5. Parse response + */ if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -103,12 +109,15 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, char *metadatastr = cJSON_Print(metadata); - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr)); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; } + /** + * 6. Decrypt value + */ + if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { size_t ivlen; ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce.str, atkey->metadata.ivnonce.len, iv, @@ -136,10 +145,9 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, goto exit; } - ret = atchops_base64_decode((unsigned char *)atclient->atkeys.selfencryptionkeystr.str, - atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, selfencryptionkeysize, - &selfencryptionkeylen); - if (ret != 0) { + if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.selfencryptionkeystr.str, + atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, selfencryptionkeysize, + &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -147,7 +155,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, // holds base64 decoded value. Once decoded, it is encrypted cipher text bytes that need to be decrypted const size_t valuerawsize = atchops_base64_decoded_size(strlen(data->valuestring)); valueraw = (char *)malloc(sizeof(char) * valuerawsize); - if(valueraw == NULL) { + if (valueraw == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); goto exit; @@ -155,16 +163,14 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, memset(valueraw, 0, sizeof(char) * valuerawsize); size_t valuerawlen = 0; - ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), (unsigned char *)valueraw, - valuerawsize, &valuerawlen); - if (ret != 0) { + if ((ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), + (unsigned char *)valueraw, valuerawsize, &valuerawlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)valueraw, valuerawlen, - (unsigned char *)value, valuesize, valuelen); - if (ret != 0) { + if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)valueraw, valuerawlen, + (unsigned char *)value, valuesize, valuelen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } @@ -183,22 +189,23 @@ exit: { } } -static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t valuesize, const size_t *valuelen) { +static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, + const char *value, const size_t valuesize, const size_t *valuelen) { int ret = 1; - if(atclient == NULL) { + if (atclient == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); goto exit; } - if(!atclient->_atsign_is_allocated) { + if (!atclient->_atsign_is_allocated) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient->_atsign_is_allocated is false\n"); goto exit; } - if(!atclient->_atserver_connection_started) { + if (!atclient->_atserver_connection_started) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); goto exit; @@ -210,44 +217,42 @@ static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const return 1; } - if(atkey == NULL) { + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); goto exit; } - if(!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not initialized\n"); goto exit; } - if(atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + if (atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is shared by someone else\n"); goto exit; } - if(value == NULL) { + if (value == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); goto exit; } - if(valuesize == 0) { + if (valuesize == 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); goto exit; } - if(valuelen == NULL) { + if (valuelen == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); goto exit; } ret = 0; -exit: { - return ret; -} +exit: { return ret; } } \ No newline at end of file From ef09dc3700e6964f8cc67074aa702ebb8337c100 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 13:09:03 -0400 Subject: [PATCH 058/193] chore: clean up atchops/rsa.c --- packages/atchops/src/rsa.c | 96 +++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/packages/atchops/src/rsa.c b/packages/atchops/src/rsa.c index b7535e91..12206f5a 100644 --- a/packages/atchops/src/rsa.c +++ b/packages/atchops/src/rsa.c @@ -24,8 +24,9 @@ int atchops_rsa_sign(const atchops_rsakey_privatekey privatekey, const atchops_m if (mdtype == ATCHOPS_MD_SHA256) { hashsize = 32; } else { + ret = 1; atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa sign\n"); - return 1; + return ret; } unsigned char hash[hashsize]; @@ -40,43 +41,44 @@ int atchops_rsa_sign(const atchops_rsakey_privatekey privatekey, const atchops_m mbedtls_ctr_drbg_context ctr_drbg_ctx; mbedtls_ctr_drbg_init(&ctr_drbg_ctx); - // 1. hash the message - ret = atchops_sha_hash(mdtype, message, messagelen, hash); - if (ret != 0) { + /* + * 1. Hash the message + */ + if ((ret = atchops_sha_hash(mdtype, message, messagelen, hash)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to perform for sha hash for rsa signing\n"); goto ret; } - // 2. sign the hash with rsa private key - ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, - privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, - privatekey.e.value, privatekey.e.len); - if (ret != 0) { + /* + * 2. Prepare RSA context + */ + if ((ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, + privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, + privatekey.e.value, privatekey.e.len)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_import_raw signing\n"); goto ret; } - ret = mbedtls_rsa_complete(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_complete operation\n"); goto ret; } - ret = mbedtls_rsa_check_privkey(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_check_privkey operation\n"); goto ret; } - ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0); - if (ret != 0) { + if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_ctr_drbg_seed operation\n"); goto ret; } - ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, atchops_mbedtls_md_map[mdtype], hashsize, - hash, signature); - if (ret != 0) { + /* + * 3. Sign the hash with RSA private key + */ + if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, atchops_mbedtls_md_map[mdtype], + hashsize, hash, signature)) != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_pkcs1_sign operation\n"); goto ret; } @@ -100,9 +102,9 @@ int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_m if (mdtype == ATCHOPS_MD_SHA256) { hashsize = 32; } else { - // TODO log error, not supported hash type cause untested + ret = 1; atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa verify\n"); - return 1; + return ret; } unsigned char hash[hashsize]; @@ -111,6 +113,9 @@ int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_m mbedtls_rsa_context rsa; mbedtls_rsa_init(&rsa); + /* + * 1. Prepare RSA context + */ if ((ret = mbedtls_rsa_import_raw(&rsa, publickey.n.value, publickey.n.len, NULL, 0, NULL, 0, NULL, 0, publickey.e.value, publickey.e.len)) != 0) { goto exit; @@ -120,18 +125,22 @@ int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_m goto exit; } - // compute the hash of the input message + /* + * 2. Hash the message + */ if ((ret = atchops_sha_hash(mdtype, message, messagelen, hash) != 0)) { goto exit; } - // verify the signature + /* + * 3. Verify the signature with RSA public key + */ if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, atchops_mbedtls_md_map[mdtype], hashsize, hash, signature)) != 0) { goto exit; } + ret = 0; goto exit; - exit: { mbedtls_rsa_free(&rsa); return ret; @@ -152,7 +161,9 @@ int atchops_rsa_encrypt(const atchops_rsakey_publickey publickey, const unsigned mbedtls_ctr_drbg_context ctr_drbg_ctx; mbedtls_ctr_drbg_init(&ctr_drbg_ctx); - // 1. rsa encrypt the plain text + /* + * 1. Prepare RSA context + */ ret = mbedtls_rsa_import_raw(&rsa, publickey.n.value, publickey.n.len, NULL, -1, NULL, -1, NULL, -1, publickey.e.value, publickey.e.len); if (ret != 0) { @@ -174,6 +185,9 @@ int atchops_rsa_encrypt(const atchops_rsakey_publickey publickey, const unsigned goto exit; } + /* + * 2. Encrypt the plaintext with RSA public key + */ ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintextlen, plaintext, ciphertext); if (ret != 0) { goto exit; @@ -205,41 +219,37 @@ int atchops_rsa_decrypt(const atchops_rsakey_privatekey privatekey, const unsign mbedtls_rsa_context rsa; mbedtls_rsa_init(&rsa); - // const size_t outputsize = plaintextsize; - // unsigned char output[outputsize]; - // memset(output, 0, sizeof(unsigned char) * outputsize); - - // 1. rsa decrypt the base64 decoded ciphertext - ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, - privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, - privatekey.e.value, privatekey.e.len); - if (ret != 0) { + /* + * 1. Prepare RSA context + */ + if ((ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, + privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, + privatekey.e.value, privatekey.e.len)) != 0) { goto exit; } - ret = mbedtls_rsa_complete(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { goto exit; } - ret = mbedtls_rsa_check_privkey(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) { goto exit; } - ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0); - if (ret != 0) { + if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0)) != 0) { goto exit; } - ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintextlen, ciphertext, plaintext, - plaintextsize); - if (ret != 0) { + /* + * 2. Decrypt the ciphertext with RSA private key + */ + if ((ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintextlen, ciphertext, + plaintext, plaintextsize)) != 0) { goto exit; } + ret = 0; goto exit; - exit: { mbedtls_rsa_free(&rsa); mbedtls_ctr_drbg_free(&ctr_drbg_ctx); From 445e3effb186ea12ea90f5ed1f389b5f61c4552d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 13:09:13 -0400 Subject: [PATCH 059/193] chore: format base64.h --- packages/atchops/include/atchops/base64.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atchops/include/atchops/base64.h b/packages/atchops/include/atchops/base64.h index 397b5d57..0fc90975 100644 --- a/packages/atchops/include/atchops/base64.h +++ b/packages/atchops/include/atchops/base64.h @@ -32,7 +32,7 @@ int atchops_base64_decode(const unsigned char *src, const size_t srcsize, unsign /** * @brief calculate the size of the base64 encoded string. This function is usually called before encoding to allocate the optimal buffer size - * + * * @param plaintextsize the size of the original plain text (to be encoded) * @return size_t the output buffer size needed to store the encoded base64 string */ @@ -40,7 +40,7 @@ size_t atchops_base64_encoded_size(const size_t plaintextsize); /** * @brief calculate the size of the base64 decoded string. This function is usually called before decoding to allocate the optimal buffer size - * + * * @param encodedsize the size of the base64 encoded string (to be decoded) * @return size_t the output buffer size needed to store the decoded plain text */ From 53ada23f769a8b4ed808d47382a2c437a1aac3d8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 18:02:09 -0400 Subject: [PATCH 060/193] docs: iv.h --- packages/atchops/include/atchops/iv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atchops/include/atchops/iv.h b/packages/atchops/include/atchops/iv.h index 26da702c..793962bd 100644 --- a/packages/atchops/include/atchops/iv.h +++ b/packages/atchops/include/atchops/iv.h @@ -3,7 +3,7 @@ #include -#define ATCHOPS_IV_BUFFER_SIZE 16 // ivs are always 16 bytes long +#define ATCHOPS_IV_BUFFER_SIZE 16 // non-base64 ivs are always 16 bytes long /** * @brief Generates a random initialization vector (implied 16 bytes long) From 7529c858586ba3b556f337d5d7b73f239d51dfdc Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 18:29:19 -0400 Subject: [PATCH 061/193] feat: made rsakey.c/.h more memory efficient --- packages/atchops/include/atchops/rsakey.h | 65 ++- packages/atchops/src/rsakey.c | 482 +++++++++++++++++++--- 2 files changed, 464 insertions(+), 83 deletions(-) diff --git a/packages/atchops/include/atchops/rsakey.h b/packages/atchops/include/atchops/rsakey.h index ee92e5a2..f302e151 100644 --- a/packages/atchops/include/atchops/rsakey.h +++ b/packages/atchops/include/atchops/rsakey.h @@ -1,11 +1,13 @@ #ifndef ATCHOPS_RSAKEY_H #define ATCHOPS_RSAKEY_H +#include #include typedef struct atchops_rsakey_param { - size_t len; // length of the number in bytes - unsigned char *value; // hex byte array of the number + size_t len; // length of the number in bytes + unsigned char *value; // hex byte array of the number + bool _is_value_initialized : 1; // whether the value is allocated } atchops_rsakey_param; typedef struct atchops_rsakey_publickey { @@ -22,33 +24,26 @@ typedef struct atchops_rsakey_privatekey { } atchops_rsakey_privatekey; void atchops_rsakey_publickey_init(atchops_rsakey_publickey *publickey); +void atchops_rsakey_publickey_free(atchops_rsakey_publickey *publickey); + +void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey); +void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey); /** * @brief Deep clone an atchops_rsakey_publickey * - * @param dst the new copy of key * @param src the src from which to copy from - * - * @note this allocates memory the same way that atchops_rsakey_publickey_init - * does, do not call atchops_rsakey_publickey_init or make sure to call - * atchops_rsakey_publickey_free on dst if you are using this function + * @param dst the new copy of key */ -void atchops_rsakey_publickey_clone(atchops_rsakey_publickey *dst, atchops_rsakey_publickey *src); -void atchops_rsakey_publickey_free(atchops_rsakey_publickey *publickey); +int atchops_rsakey_publickey_clone(const atchops_rsakey_publickey *src, atchops_rsakey_publickey *dst); -void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey); /** * @brief Deep clone an atchops_rsakey_privatekey * - * @param dst the new copy of key * @param src the src from which to copy from - * - * @note this allocates memory the same way that atchops_rsakey_privatekey_init - * does, do not call atchops_rsakey_privatekey_init or make sure to call - * atchops_rsakey_privatekey_free on dst if you are using this function + * @param dst the new copy of key */ -void atchops_rsakey_privatekey_clone(atchops_rsakey_privatekey *dst, atchops_rsakey_privatekey *src); -void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_clone(const atchops_rsakey_privatekey *src, atchops_rsakey_privatekey *dst); /** * @brief Populate a public key struct from a base64 string @@ -72,4 +67,40 @@ int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickeystruct, int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekeystruct, const char *privatekeybase64, const size_t privatekeybase64len); +int atchops_rsakey_publickey_set_ne(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen, + const unsigned char *e, const size_t elen); + +bool atchops_rsakey_publickey_is_n_initialized(atchops_rsakey_publickey *publickey); +int atchops_rsakey_publickey_set_n(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen); +void atchops_rsakey_publickey_unset_n(atchops_rsakey_publickey *publickey); + +bool atchops_rsakey_publickey_is_e_initialized(atchops_rsakey_publickey *publickey); +int atchops_rsakey_publickey_set_e(atchops_rsakey_publickey *publickey, const unsigned char *e, const size_t elen); +void atchops_rsakey_publickey_unset_e(atchops_rsakey_publickey *publickey); + +int atchops_rsakey_privatekey_set_nedpq(atchops_rsakey_privatekey *privatekey, const unsigned char *n, + const size_t nlen, const unsigned char *e, const size_t elen, + const unsigned char *d, const size_t dlen, const unsigned char *p, + const size_t plen, const unsigned char *q, const size_t qlen); + +bool atchops_rsakey_privatekey_is_n_initialized(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_set_n(atchops_rsakey_privatekey *privatekey, const unsigned char *n, const size_t nlen); +void atchops_rsakey_privatekey_unset_n(atchops_rsakey_privatekey *privatekey); + +bool atchops_rsakey_privatekey_is_e_initialized(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_set_e(atchops_rsakey_privatekey *privatekey, const unsigned char *e, const size_t elen); +void atchops_rsakey_privatekey_unset_e(atchops_rsakey_privatekey *privatekey); + +bool atchops_rsakey_privatekey_is_d_initialized(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_set_d(atchops_rsakey_privatekey *privatekey, const unsigned char *d, const size_t dlen); +void atchops_rsakey_privatekey_unset_d(atchops_rsakey_privatekey *privatekey); + +bool atchops_rsakey_privatekey_is_p_initialized(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_set_p(atchops_rsakey_privatekey *privatekey, const unsigned char *p, const size_t plen); +void atchops_rsakey_privatekey_unset_p(atchops_rsakey_privatekey *privatekey); + +bool atchops_rsakey_privatekey_is_q_initialized(atchops_rsakey_privatekey *privatekey); +int atchops_rsakey_privatekey_set_q(atchops_rsakey_privatekey *privatekey, const unsigned char *q, const size_t qlen); +void atchops_rsakey_privatekey_unset_q(atchops_rsakey_privatekey *privatekey); + #endif diff --git a/packages/atchops/src/rsakey.c b/packages/atchops/src/rsakey.c index 1d9d07fd..c41e7f06 100644 --- a/packages/atchops/src/rsakey.c +++ b/packages/atchops/src/rsakey.c @@ -1,5 +1,6 @@ #include "atchops/rsakey.h" #include "atchops/base64.h" +#include #include #include #include @@ -7,81 +8,80 @@ #define BASE64_DECODED_KEY_BUFFER_SIZE 8192 // the max buffer size of a decoded RSA key +#define TAG "rsakey" + void atchops_rsakey_publickey_init(atchops_rsakey_publickey *publickey) { memset(publickey, 0, sizeof(atchops_rsakey_publickey)); - - publickey->n.len = BASE64_DECODED_KEY_BUFFER_SIZE; - publickey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->n.len); // TODO handle null - - publickey->e.len = BASE64_DECODED_KEY_BUFFER_SIZE; - publickey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * publickey->e.len); // TODO handle null -} - -void atchops_rsakey_publickey_clone(atchops_rsakey_publickey *dst, atchops_rsakey_publickey *src) { - memset(dst, 0, sizeof(atchops_rsakey_publickey)); - - dst->n.len = src->n.len; - dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); // TODO handle null - memcpy(dst->n.value, src->n.value, dst->n.len); - - dst->e.len = src->e.len; - dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); // TODO handle null - memcpy(dst->e.value, src->e.value, dst->e.len); } void atchops_rsakey_publickey_free(atchops_rsakey_publickey *publickey) { - free(publickey->n.value); - free(publickey->e.value); + atchops_rsakey_publickey_unset_n(publickey); + atchops_rsakey_publickey_unset_e(publickey); + memset(publickey, 0, sizeof(atchops_rsakey_publickey)); } void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey) { memset(privatekey, 0, sizeof(atchops_rsakey_privatekey)); +} - privatekey->n.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->n.value = malloc(sizeof(unsigned char) * privatekey->n.len); // TODO handle null +void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey) { + atchops_rsakey_privatekey_unset_n(privatekey); + atchops_rsakey_privatekey_unset_e(privatekey); + atchops_rsakey_privatekey_unset_d(privatekey); + atchops_rsakey_privatekey_unset_p(privatekey); + atchops_rsakey_privatekey_unset_q(privatekey); + memset(privatekey, 0, sizeof(atchops_rsakey_privatekey)); +} - privatekey->e.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->e.value = malloc(sizeof(unsigned char) * privatekey->e.len); // TODO handle null +int atchops_rsakey_publickey_clone(const atchops_rsakey_publickey *src, atchops_rsakey_publickey *dst) { + int ret = 1; - privatekey->d.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->d.value = malloc(sizeof(unsigned char) * privatekey->d.len); // TODO handle null + if ((ret = atchops_rsakey_publickey_set_n(dst, src->n.value, src->n.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } - privatekey->p.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->p.value = malloc(sizeof(unsigned char) * privatekey->p.len); // TODO handle null + if ((ret = atchops_rsakey_publickey_set_e(dst, src->e.value, src->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } - privatekey->q.len = BASE64_DECODED_KEY_BUFFER_SIZE; - privatekey->q.value = malloc(sizeof(unsigned char) * privatekey->q.len); + ret = 0; + goto exit; +exit: { return ret; } } -void atchops_rsakey_privatekey_clone(atchops_rsakey_privatekey *dst, atchops_rsakey_privatekey *src) { - memset(dst, 0, sizeof(atchops_rsakey_privatekey)); - dst->n.len = src->n.len; - dst->n.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->n.len); // TODO handle null - memcpy(dst->n.value, src->n.value, dst->n.len); +int atchops_rsakey_privatekey_clone(const atchops_rsakey_privatekey *src, atchops_rsakey_privatekey *dst) { + int ret = 1; - dst->e.len = src->e.len; - dst->e.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->e.len); // TODO handle null - memcpy(dst->e.value, src->e.value, dst->e.len); + if ((ret = atchops_rsakey_privatekey_set_n(dst, src->n.value, src->n.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } - dst->d.len = src->d.len; - dst->d.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->d.len); // TODO handle null - memcpy(dst->d.value, src->d.value, dst->d.len); + if ((ret = atchops_rsakey_privatekey_set_e(dst, src->e.value, src->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } - dst->p.len = src->p.len; - dst->p.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->p.len); // TODO handle null - memcpy(dst->p.value, src->p.value, dst->p.len); + if ((ret = atchops_rsakey_privatekey_set_d(dst, src->d.value, src->d.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); + goto exit; + } - dst->q.len = src->q.len; - dst->q.value = (unsigned char *)malloc(sizeof(unsigned char) * dst->q.len); // TODO handle null - memcpy(dst->q.value, src->q.value, dst->q.len); -} + if ((ret = atchops_rsakey_privatekey_set_p(dst, src->p.value, src->p.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); + goto exit; + } -void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey) { - free(privatekey->n.value); - free(privatekey->e.value); - free(privatekey->d.value); - free(privatekey->p.value); - free(privatekey->q.value); + if ((ret = atchops_rsakey_privatekey_set_q(dst, src->q.value, src->q.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } } int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickey, const char *publickeybase64, @@ -126,7 +126,7 @@ int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickey, const } seq = malloc(sizeof(mbedtls_asn1_sequence)); - if(seq == NULL) { + if (seq == NULL) { ret = 1; goto exit; } @@ -197,7 +197,7 @@ int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekey, co } seq = malloc(sizeof(mbedtls_asn1_sequence)); - if(seq == NULL) { + if (seq == NULL) { ret = 1; goto exit; } @@ -210,24 +210,34 @@ int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekey, co mbedtls_asn1_sequence *current = seq; current = current->next; - privatekey->n.len = current->buf.len; - memcpy(privatekey->n.value, current->buf.p, privatekey->n.len); + if ((ret = atchops_rsakey_privatekey_set_n(privatekey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } current = current->next; - privatekey->e.len = current->buf.len; - memcpy(privatekey->e.value, current->buf.p, privatekey->e.len); + if ((ret = atchops_rsakey_privatekey_set_e(privatekey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } current = current->next; - privatekey->d.len = current->buf.len; - memcpy(privatekey->d.value, current->buf.p, privatekey->d.len); + if ((ret = atchops_rsakey_privatekey_set_d(privatekey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } current = current->next; - privatekey->p.len = current->buf.len; - memcpy(privatekey->p.value, current->buf.p, privatekey->p.len); + if ((ret = atchops_rsakey_privatekey_set_p(privatekey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } current = current->next; - privatekey->q.len = current->buf.len; - memcpy(privatekey->q.value, current->buf.p, privatekey->q.len); + if ((ret = atchops_rsakey_privatekey_set_q(privatekey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } ret = 0; goto exit; @@ -236,3 +246,343 @@ exit: { return ret; } } + +int atchops_rsakey_publickey_set_ne(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen, + const unsigned char *e, const size_t elen) { + int ret = 1; + + if ((ret = atchops_rsakey_publickey_set_n(publickey, n, nlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsakey_publickey_set_e(publickey, e, elen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +bool atchops_rsakey_publickey_is_n_initialized(atchops_rsakey_publickey *publickey) { + return publickey->n._is_value_initialized; +} + +int atchops_rsakey_publickey_set_n(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen) { + int ret = 1; + if (n == NULL || nlen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or nlen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_publickey_unset_n(publickey); + + publickey->n.len = nlen; + + publickey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (publickey->n.len)); + if (publickey->n.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); + goto exit; + } + + publickey->n._is_value_initialized = true; + memcpy(publickey->n.value, n, nlen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_publickey_unset_n(atchops_rsakey_publickey *publickey) { + if (publickey->n._is_value_initialized) { + free(publickey->n.value); + } + publickey->n._is_value_initialized = false; + publickey->n.value = NULL; + publickey->n.len = 0; +} + +bool atchops_rsakey_publickey_is_e_initialized(atchops_rsakey_publickey *publickey) { + return publickey->e._is_value_initialized; +} + +int atchops_rsakey_publickey_set_e(atchops_rsakey_publickey *publickey, const unsigned char *e, const size_t elen) { + int ret = 1; + if (e == NULL || elen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or elen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_publickey_unset_e(publickey); + + publickey->e.len = elen; + + publickey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (publickey->e.len)); + if (publickey->e.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); + goto exit; + } + + publickey->e._is_value_initialized = true; + memcpy(publickey->e.value, e, elen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_publickey_unset_e(atchops_rsakey_publickey *publickey) { + if (publickey->e._is_value_initialized) { + free(publickey->e.value); + } + publickey->e._is_value_initialized = false; + publickey->e.value = NULL; + publickey->e.len = 0; +} + +int atchops_rsakey_privatekey_set_nedpq(atchops_rsakey_privatekey *privatekey, const unsigned char *n, + const size_t nlen, const unsigned char *e, const size_t elen, + const unsigned char *d, const size_t dlen, const unsigned char *p, + const size_t plen, const unsigned char *q, const size_t qlen) { + int ret = 1; + + if ((ret = atchops_rsakey_privatekey_set_n(privatekey, n, nlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsakey_privatekey_set_e(privatekey, e, elen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + if ((ret = atchops_rsakey_privatekey_set_d(privatekey, d, dlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); + goto exit; + } + + if ((ret = atchops_rsakey_privatekey_set_p(privatekey, p, plen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); + goto exit; + } + + if ((ret = atchops_rsakey_privatekey_set_q(privatekey, q, qlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +bool atchops_rsakey_privatekey_is_n_initialized(atchops_rsakey_privatekey *privatekey) { + return privatekey->n._is_value_initialized; +} + +int atchops_rsakey_privatekey_set_n(atchops_rsakey_privatekey *privatekey, const unsigned char *n, const size_t nlen) { + int ret = 1; + + if (n == NULL || nlen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or nlen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_privatekey_unset_n(privatekey); + + privatekey->n.len = nlen; + + privatekey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->n.len)); + if (privatekey->n.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); + goto exit; + } + + privatekey->n._is_value_initialized = true; + memcpy(privatekey->n.value, n, nlen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_privatekey_unset_n(atchops_rsakey_privatekey *privatekey) { + if (privatekey->n._is_value_initialized) { + free(privatekey->n.value); + } + privatekey->n._is_value_initialized = false; + privatekey->n.value = NULL; + privatekey->n.len = 0; +} + +bool atchops_rsakey_privatekey_is_e_initialized(atchops_rsakey_privatekey *privatekey) { + return privatekey->e._is_value_initialized; +} + +int atchops_rsakey_privatekey_set_e(atchops_rsakey_privatekey *privatekey, const unsigned char *e, const size_t elen) { + int ret = 1; + + if (e == NULL || elen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or elen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_privatekey_unset_e(privatekey); + + privatekey->e.len = elen; + + privatekey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->e.len)); + if (privatekey->e.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); + goto exit; + } + + privatekey->e._is_value_initialized = true; + memcpy(privatekey->e.value, e, elen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_privatekey_unset_e(atchops_rsakey_privatekey *privatekey) { + if (privatekey->e._is_value_initialized) { + free(privatekey->e.value); + } + privatekey->e.value = NULL; + privatekey->e.len = 0; + privatekey->e._is_value_initialized = false; +} + +bool atchops_rsakey_privatekey_is_d_initialized(atchops_rsakey_privatekey *privatekey) { + return privatekey->d._is_value_initialized; +} + +int atchops_rsakey_privatekey_set_d(atchops_rsakey_privatekey *privatekey, const unsigned char *d, const size_t dlen) { + int ret = 1; + + if (d == NULL || dlen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "d is null or dlen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_privatekey_unset_d(privatekey); + + privatekey->d.len = dlen; + + privatekey->d.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->d.len)); + if (privatekey->d.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for d value\n"); + goto exit; + } + + privatekey->d._is_value_initialized = true; + memcpy(privatekey->d.value, d, dlen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_privatekey_unset_d(atchops_rsakey_privatekey *privatekey) { + if (privatekey->d._is_value_initialized) { + free(privatekey->d.value); + } + privatekey->d._is_value_initialized = false; + privatekey->d.value = NULL; + privatekey->d.len = 0; +} + +bool atchops_rsakey_privatekey_is_p_initialized(atchops_rsakey_privatekey *privatekey) { + return privatekey->p._is_value_initialized; +} + +int atchops_rsakey_privatekey_set_p(atchops_rsakey_privatekey *privatekey, const unsigned char *p, const size_t plen) { + int ret = 1; + + if (p == NULL || plen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "p is null or plen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_privatekey_unset_p(privatekey); + + privatekey->p.len = plen; + + privatekey->p.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->p.len)); + if (privatekey->p.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for p value\n"); + goto exit; + } + + privatekey->p._is_value_initialized = true; + memcpy(privatekey->p.value, p, plen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_privatekey_unset_p(atchops_rsakey_privatekey *privatekey) { + if (privatekey->p._is_value_initialized) { + free(privatekey->p.value); + } + privatekey->p._is_value_initialized = false; + privatekey->p.value = NULL; + privatekey->p.len = 0; +} + +bool atchops_rsakey_privatekey_is_q_initialized(atchops_rsakey_privatekey *privatekey) { + return privatekey->q._is_value_initialized; +} + +int atchops_rsakey_privatekey_set_q(atchops_rsakey_privatekey *privatekey, const unsigned char *q, const size_t qlen) { + int ret = 1; + + if (q == NULL || qlen <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "q is null or qlen is less than or equal to 0\n"); + goto exit; + } + + atchops_rsakey_privatekey_unset_q(privatekey); + + privatekey->q.len = qlen; + + privatekey->q.value = (unsigned char *)malloc(sizeof(unsigned char) * qlen); + if (privatekey->q.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for q value\n"); + goto exit; + } + + privatekey->q._is_value_initialized = true; + memcpy(privatekey->q.value, q, qlen); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsakey_privatekey_unset_q(atchops_rsakey_privatekey *privatekey) { + if (privatekey->q._is_value_initialized) { + free(privatekey->q.value); + } + privatekey->q.value = NULL; + privatekey->q.len = 0; + privatekey->q._is_value_initialized = false; +} From c6def3798edfc4322825e99d0f68dfa02b01e7dd Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 18:32:01 -0400 Subject: [PATCH 062/193] fix: rsa public key unit test failure --- packages/atchops/src/rsakey.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/atchops/src/rsakey.c b/packages/atchops/src/rsakey.c index c41e7f06..93a7a1f3 100644 --- a/packages/atchops/src/rsakey.c +++ b/packages/atchops/src/rsakey.c @@ -137,12 +137,16 @@ int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickey, const } mbedtls_asn1_sequence *current = seq; - publickey->n.len = current->buf.len; - memcpy(publickey->n.value, current->buf.p, publickey->n.len); + if((ret = atchops_rsakey_publickey_set_n(publickey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } current = current->next; - publickey->e.len = current->buf.len; - memcpy(publickey->e.value, current->buf.p, publickey->e.len); + if((ret = atchops_rsakey_publickey_set_e(publickey, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } ret = 0; goto exit; From 0c3ce58a307c255c5a2a70ce4c67e94f5780b961 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 18:34:22 -0400 Subject: [PATCH 063/193] fix: atkey.c label error --- packages/atclient/src/atkey.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 1a0122f7..10932a40 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -106,6 +106,9 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { int ret = 1; char *sharedby_withat = NULL; char *saveptr; + const size_t compositesize = ATCLIENT_ATKEY_COMPOSITE_LEN + 1; + char composite[compositesize]; // holds {key}.{namespace} + memset(composite, 0, sizeof(char) * compositesize); char *copy = strdup(atkeystr); if (copy == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdndup failed\n"); @@ -167,9 +170,6 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } tokenlen = strlen(token); - const size_t compositesize = ATCLIENT_ATKEY_COMPOSITE_LEN + 1; - char composite[compositesize]; - memset(composite, 0, sizeof(char) * compositesize); memcpy(composite, token, tokenlen); char *check = strchr(composite, '.'); From ab92ad316523074a900803e8e1e89f15f944493b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:10:59 -0400 Subject: [PATCH 064/193] chore: clean up aesctr.h/.c --- packages/atchops/include/atchops/aesctr.h | 8 ++-- packages/atchops/src/aesctr.c | 52 +++++++++++------------ packages/atclient/src/atclient_put.c | 2 +- packages/atclient/src/atkey.c | 3 +- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/packages/atchops/include/atchops/aesctr.h b/packages/atchops/include/atchops/aesctr.h index bbe3ef85..5bfb3854 100644 --- a/packages/atchops/include/atchops/aesctr.h +++ b/packages/atchops/include/atchops/aesctr.h @@ -41,19 +41,19 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size const size_t plaintextsize, size_t *plaintextlen); /** - * @brief Used to calculate the length of the ciphertext buffer given the plaintext length + * @brief Used to calculate the length of the ciphertext buffer given the plaintext length. This is used when encrypting. * * @param plaintextlen the length of the plaintext string * @return a sufficient length of the ciphertext buffer */ -size_t atchops_aes_ctr_ciphertext_size(const size_t plaintextlen); +size_t atchops_aesctr_ciphertext_size(const size_t plaintextlen); /** - * @brief Used to calculate the length of the plaintext buffer given the ciphertext length + * @brief Used to calculate the a sufficient buffer size of the plaintext buffer given the ciphertext length. This is used when decrypting. * * @param ciphertextlen the length of the ciphertext string * @return a sufficient length of the plaintext buffer */ -size_t atchops_aes_ctr_plaintext_size(const size_t ciphertextlen); +size_t atchops_aesctr_plaintext_size(const size_t ciphertextlen); #endif diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c index aa200ed8..44066429 100644 --- a/packages/atchops/src/aesctr.c +++ b/packages/atchops/src/aesctr.c @@ -28,13 +28,10 @@ int atchops_aesctr_encrypt(const unsigned char *key, const enum atchops_aes_size const int numpadbytestoadd = 16 - (plaintextlen % 16); const unsigned char padval = numpadbytestoadd; - // printf("appending %d bytes of padding: 0x%02x\n", numpadbytestoadd, padval); plaintextpaddedlen = plaintextlen + numpadbytestoadd; - // printf("plaintext_paddedlen: %lu = %d + %d\n", plaintextpaddedlen, plaintextlen, numpadbytestoadd); - plaintextpadded = malloc(sizeof(unsigned char) * (plaintextpaddedlen + 1)); - if(plaintextpadded == NULL) { + if((plaintextpadded = malloc(sizeof(unsigned char) * (plaintextpaddedlen + 1))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); goto exit; @@ -78,18 +75,29 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size mbedtls_aes_context aes; mbedtls_aes_init(&aes); + unsigned char *stream_block = NULL; + unsigned char *plaintextpadded = NULL; + + /* + * 1. Prepare AES context + */ + if ((ret = mbedtls_aes_setkey_enc(&aes, key, keybits)) != 0) { + goto exit; + } + + /* + * 2. Allocate buffers required for decryption + */ size_t nc_off = 0; - unsigned char *stream_block = malloc(sizeof(unsigned char) * 16); - if(stream_block == NULL) { + if((stream_block = malloc(sizeof(unsigned char) * 16)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for stream_block\n"); goto exit; } memset(stream_block, 0, sizeof(unsigned char) * 16); - size_t plaintextpaddedsize = plaintextsize + 16; - unsigned char *plaintextpadded = malloc(sizeof(unsigned char) * plaintextpaddedsize); - if(plaintextpadded == NULL) { + const size_t plaintextpaddedsize = plaintextsize + 16; + if((plaintextpadded = malloc(sizeof(unsigned char) * plaintextpaddedsize)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); goto exit; @@ -97,52 +105,44 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size memset(plaintextpadded, 0, plaintextpaddedsize); size_t plaintextpaddedlen = 0; - // 3. AES decrypt - ret = mbedtls_aes_setkey_enc(&aes, key, keybits); - if (ret != 0) { - goto exit; - } - - ret = mbedtls_aes_crypt_ctr(&aes, ciphertextlen, &nc_off, iv, stream_block, ciphertext, plaintextpadded); - if (ret != 0) { + /* + * 3. Decrypt + */ + if ((ret = mbedtls_aes_crypt_ctr(&aes, ciphertextlen, &nc_off, iv, stream_block, ciphertext, plaintextpadded)) != 0) { goto exit; } + /* + * 4. Remove padding + */ while (*(plaintextpadded + plaintextpaddedlen++) != '\0') ; --plaintextpaddedlen; // don't count the null terminator - // 4. remove padding // IBM PKCS Padding method states that there is always at least 1 padded value: // https://www.ibm.com/docs/en/zos/2.4.0?topic=rules-pkcs-padding-method the value of the padded byte is always the // number of padded bytes to expect, padval == num_padded_bytes unsigned char padval = *(plaintextpadded + (plaintextpaddedlen - 1)); - // add null terminator for good sake - *(plaintextpadded + plaintextpaddedlen - padval) = '\0'; - *plaintextlen = plaintextpaddedlen - padval; memcpy(plaintext, plaintextpadded, *plaintextlen); goto exit; exit: { - - // free everything free(stream_block); free(plaintextpadded); mbedtls_aes_free(&aes); - return ret; } } -size_t atchops_aes_ctr_ciphertext_size(const size_t plaintextlen) +size_t atchops_aesctr_ciphertext_size(const size_t plaintextlen) { return ((plaintextlen + 15) & ~0xF) + 16; } -size_t atchops_aes_ctr_plaintext_size(const size_t ciphertextlen) +size_t atchops_aesctr_plaintext_size(const size_t ciphertextlen) { return ((ciphertextlen + 15) & ~0xF) + 16; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 3adda02b..f75ec93b 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -50,7 +50,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(ivbase64, 0, sizeof(char) * ivbase64size); size_t ivbase64len = 0; - const size_t ciphertextsize = atchops_aes_ctr_ciphertext_size(valuelen) + 1; + const size_t ciphertextsize = atchops_aesctr_ciphertext_size(valuelen) + 1; unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); size_t ciphertextlen = 0; diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 10932a40..51728c60 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -269,8 +269,7 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { size_t index_pos = 0; const size_t atkeystrsize = atclient_atkey_strlen(atkey) + 1; - *atkeystr = (char *)malloc(sizeof(char) * atkeystrsize); - if (*atkeystr == NULL) { + if ((*atkeystr = (char *)malloc(sizeof(char) * atkeystrsize)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto error_exit; } From ba182a6f10fd19ff5b73e1fbdeaf12275e212e49 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:12:34 -0400 Subject: [PATCH 065/193] chore: make iv.h/.c more minimal --- packages/atchops/include/atchops/iv.h | 10 ---------- packages/atchops/src/iv.c | 24 +++++------------------- packages/atchops/tests/test_iv.c | 7 ------- 3 files changed, 5 insertions(+), 36 deletions(-) diff --git a/packages/atchops/include/atchops/iv.h b/packages/atchops/include/atchops/iv.h index 793962bd..02a11a06 100644 --- a/packages/atchops/include/atchops/iv.h +++ b/packages/atchops/include/atchops/iv.h @@ -13,14 +13,4 @@ */ int atchops_iv_generate(unsigned char *iv); -/** - * @brief Generates a random initialization vector and encodes it in base64 - * - * @param ivbase64 the buffer to store the generated IV - * @param ivbase64size the length of the buffer - * @param ivbase64len the length of the generated IV - * @return int 0 on success, non-zero on failure - */ -int atchops_iv_generate_base64(unsigned char *ivbase64, const size_t ivbase64size, size_t *ivbase64len); - #endif diff --git a/packages/atchops/src/iv.c b/packages/atchops/src/iv.c index fa4f37f6..9e8acf35 100644 --- a/packages/atchops/src/iv.c +++ b/packages/atchops/src/iv.c @@ -1,11 +1,14 @@ #include "atchops/iv.h" #include "atchops/base64.h" #include "atchops/constants.h" +#include #include #include #include #include +#define TAG "iv" + int atchops_iv_generate(unsigned char *iv) { int ret = 1; mbedtls_entropy_context entropy; @@ -17,10 +20,12 @@ int atchops_iv_generate(unsigned char *iv) { if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)ATCHOPS_RNG_PERSONALIZATION, strlen(ATCHOPS_RNG_PERSONALIZATION))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to seed random number generator\n"); goto exit; } if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, iv, ATCHOPS_IV_BUFFER_SIZE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to generate random IV\n"); goto exit; } @@ -32,22 +37,3 @@ exit: { return ret; } } - -int atchops_iv_generate_base64(unsigned char *ivbase64, const size_t ivbase64size, size_t *ivbase64len) { - int ret = 1; - - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - - ret = atchops_iv_generate(iv); - if (ret != 0) { - goto exit; - } - - ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, ivbase64, ivbase64size, ivbase64len); - if (ret != 0) { - goto exit; - } - - goto exit; -exit: { return ret; } -} diff --git a/packages/atchops/tests/test_iv.c b/packages/atchops/tests/test_iv.c index 1638ed79..230b2a58 100644 --- a/packages/atchops/tests/test_iv.c +++ b/packages/atchops/tests/test_iv.c @@ -26,12 +26,5 @@ int main() { } printf("\n"); - ret = atchops_iv_generate_base64(ivbase64, ivbase64size, &ivbase64len); - if (ret != 0) { - printf("atchops_iv_generate_base64 (failed): %d\n", ret); - goto exit; - } - printf("ivbase64 (%lu): %s\n", ivbase64len, ivbase64); - exit: { return ret; } } From 9b472a530ed18dced2e984a90f1e195b13b59cec Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:13:54 -0400 Subject: [PATCH 066/193] chore: clean up test_iv.c --- packages/atchops/tests/test_iv.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/atchops/tests/test_iv.c b/packages/atchops/tests/test_iv.c index 230b2a58..29b03310 100644 --- a/packages/atchops/tests/test_iv.c +++ b/packages/atchops/tests/test_iv.c @@ -3,25 +3,24 @@ #include #include #include +#include + +#define TAG "test_iv" int main() { int ret = 1; - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - - const size_t ivbase64size = 1024; - unsigned char ivbase64[ivbase64size]; - memset(ivbase64, 0, sizeof(unsigned char) * ivbase64size); - size_t ivbase64len = 0; + const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[ivsize]; + memset(iv, 0, sizeof(unsigned char) * ivsize); - ret = atchops_iv_generate(iv); - if (ret != 0) { - printf("atchops_iv_generate (failed): %d\n", ret); + if ((ret = atchops_iv_generate(iv)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to generate IV\n"); goto exit; } - printf("iv: "); - for (int i = 0; i < ATCHOPS_IV_BUFFER_SIZE; i++) { + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "IV (%d bytes): ", ivsize); + for (int i = 0; i < ivsize; i++) { printf("%02x ", iv[i]); } printf("\n"); From a230442d97860f371c9ad432b64072b506f12fdb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:16:16 -0400 Subject: [PATCH 067/193] fix: functional tests helpers.c --- tests/functional_tests/lib/src/helpers.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 62ad70b0..1dbe9632 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -102,7 +102,7 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const goto exit; } - if (!atclient_stringutils_starts_with(recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "publickey does not exist: \"%s\"\n", recv); goto exit; @@ -157,7 +157,7 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c goto exit; } - if (!atclient_stringutils_starts_with(recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "selfkey does not exist: \"%s\"\n", recv); goto exit; @@ -212,7 +212,7 @@ int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const goto exit; } - if (!atclient_stringutils_starts_with(recv, recvlen, "data:", 5)) { + if (!atclient_stringutils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "sharedkey does not exist: \"%s\"\n", recv); goto exit; @@ -245,7 +245,7 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", (recipient + 1), atclient1->atsign.atsign); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkeystrtemp: \"%s\"\n", atkeystrtemp); - if ((ret = atclient_atkey_from_string(&atkeyforme, atkeystrtemp, strlen(atkeystrtemp))) != 0) { + if ((ret = atclient_atkey_from_string(&atkeyforme, atkeystrtemp)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d\n", ret); goto exit; } @@ -253,7 +253,7 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "%s:shared_key%s", recipient, atclient1->atsign.atsign); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkeystrtemp: \"%s\"\n", atkeystrtemp); - if ((ret = atclient_atkey_from_string(&atkeyforthem, atkeystrtemp, strlen(atkeystrtemp))) != 0) { + if ((ret = atclient_atkey_from_string(&atkeyforthem, atkeystrtemp)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d\n", ret); goto exit; } From b146542c885302cf39dc8d7365b90684b083789f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:18:59 -0400 Subject: [PATCH 068/193] fix: at_talk compilation errors --- examples/desktop/at_talk/src/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index f9ca31ae..78ecef4f 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -152,9 +152,7 @@ int main(int argc, char *argv[]) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), from_atsign, strlen(from_atsign), - to_atsign, strlen(to_atsign), ATKEY_NAMESPACE, - strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, from_atsign, to_atsign, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); } From 7545fe2ed60a861b8d85dfc8c0c23be096112ad6 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:22:50 -0400 Subject: [PATCH 069/193] fix: crud examples compilation errors --- examples/desktop/crud/get_publickey.c | 3 +- examples/desktop/crud/get_selfkey.c | 3 +- examples/desktop/crud/get_sharedkey.c | 11 +-- examples/desktop/crud/put_publickey.c | 10 +-- examples/desktop/crud/put_selfkey.c | 3 +- examples/desktop/crud/put_sharedkey.c | 118 +++++++++++++------------- examples/desktop/events/notify.c | 9 +- examples/desktop/repl/src/main.c | 6 +- 8 files changed, 81 insertions(+), 82 deletions(-) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index a76b8b63..792ac7fe 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -62,8 +62,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), NULL, 0)) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index dac5bda2..acdc36db 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -66,8 +66,7 @@ int main() { atclient.atkeys = atkeys; atclient.atsign = atsign; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), atsign.atsign, strlen(atsign.atsign), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign.atsign, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } else { diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index f85dd6d5..24d760ee 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -65,7 +65,8 @@ int main() { char *atkeystr = NULL; - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } @@ -80,9 +81,8 @@ int main() { atclient.atkeys = atkeys; atclient.atsign = atsign; - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), SENDER_ATSIGN, - strlen(SENDER_ATSIGN), RECIPIENT_ATSIGN, strlen(RECIPIENT_ATSIGN), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, SENDER_ATSIGN, RECIPIENT_ATSIGN, ATKEY_NAMESPACE)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create shared key\n"); goto exit; } else { @@ -95,7 +95,8 @@ int main() { } const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int) atkeystrlen, atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, + atkeystr); ret = atclient_get_sharedkey(&atclient, &atkey, value, valuesize, valuelen, NULL, false); if (ret != 0) { diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index ce1f8895..f8ea123f 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -51,7 +51,8 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } @@ -61,8 +62,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATSIGN, strlen(ATSIGN), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } @@ -76,8 +76,8 @@ int main() { } const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, - (int)atkeystrlen, atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, + atkeystr); if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index eb679160..ad150061 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -62,8 +62,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATSIGN, strlen(ATSIGN), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index 94c53fd5..5a74474e 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -1,12 +1,12 @@ -#include -#include -#include -#include -#include #include #include #include #include +#include +#include +#include +#include +#include // publickey @@ -16,7 +16,6 @@ #define ATSIGN "@soccer0" #define ATKEYS_FILE_PATH "/Users/jeremytubongbanua/.atsign/keys/@soccer0_key.atKeys" - #define ATKEY_KEY "test" #define ATKEY_NAMESPACE "dart_playground" #define ATKEY_VALUE "test value" @@ -25,76 +24,77 @@ #define ROOT_HOST "root.atsign.org" #define ROOT_PORT 64 -int main() -{ - int ret = 1; +int main() { + int ret = 1; - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - const size_t valuesize = 2048; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valueolen = 0; + const size_t valuesize = 2048; + char value[valuesize]; + memset(value, 0, sizeof(char) * valuesize); + size_t valueolen = 0; - atclient atclient; - atclient_init(&atclient); + atclient atclient; + atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + atclient_atsign atsign; + atclient_atsign_init(&atsign, ATSIGN); - char *atserver_host = NULL; - int atserver_port = -1; + char *atserver_host = NULL; + int atserver_port = -1; - atclient_atkey atkey; - atclient_atkey_init(&atkey); + atclient_atkey atkey; + atclient_atkey_init(&atkey); - atclient_atkeys atkeys; - atclient_atkeys_init(&atkeys); - atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); + atclient_atkeys atkeys; + atclient_atkeys_init(&atkeys); + atclient_atkeys_populate_from_path(&atkeys, ATKEYS_FILE_PATH); - char *atkeystr = NULL; + char *atkeystr = NULL; - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); - goto exit; - } + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); + goto exit; + } - if((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate"); - goto exit; - } + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate"); + goto exit; + } - if((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATSIGN, strlen(ATSIGN), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); - goto exit; - } + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); + goto exit; + } - atclient_atkey_metadata_set_ttl(&atkey.metadata, 60*1000*10); // 10 minutes + atclient_atkey_metadata_set_ttl(&atkey.metadata, 60 * 1000 * 10); // 10 minutes - if((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); - goto exit; - } - const size_t atkeystrlen = strlen(atkeystr); + if ((ret = atclient_atkey_to_string(&atkey, &atkeystr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert to string"); + goto exit; + } + const size_t atkeystrlen = strlen(atkeystr); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Putting atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int) atkeystrlen, atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Putting atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, + (int)atkeystrlen, atkeystr); - if((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); - goto exit; - } + if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); + goto exit; + } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Done put.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Done put.\n"); - ret = 0; - goto exit; + ret = 0; + goto exit; exit: { - atclient_atkeys_free(&atkeys); - atclient_atkey_free(&atkey); - atclient_atsign_free(&atsign); - atclient_free(&atclient); - free(atserver_host); - free(atkeystr); - return ret; + atclient_atkeys_free(&atkeys); + atclient_atkey_free(&atkey); + atclient_atsign_free(&atsign); + atclient_free(&atclient); + free(atserver_host); + free(atkeystr); + return ret; } } diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 373cf7be..f3203254 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -81,8 +81,8 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != - 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address\n"); goto exit; } @@ -106,9 +106,8 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), atsign_input, strlen(atsign_input), - other_atsign_input, strlen(other_atsign_input), ATKEY_NAMESPACE, - strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, atsign_input, other_atsign_input, ATKEY_NAMESPACE)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 2cf12b48..0b73cdd8 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -139,13 +139,15 @@ int main(int argc, char *argv[]) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_from_string(&atkey, atkeystr, strlen(atkeystr)))) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr))) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d | failed to parse atKey\n", ret); goto get_end; } - switch (atkey.atkeytype) { + const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); + + switch (atkey_type) { case ATCLIENT_ATKEY_TYPE_UNKNOWN: { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unknown atKey type\n"); goto get_end; From beddd377096c3bee46842d8356d147b7f4deb72e Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:24:14 -0400 Subject: [PATCH 070/193] fix: --- examples/desktop/crud/delete.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index 9f15684b..3d75c115 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -4,6 +4,7 @@ #include #include #include +#include #define TAG "Debug" @@ -42,13 +43,13 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), atsign.atsign, strlen(atsign.atsign), - ATKEY_NAME, strlen(ATKEY_NAME))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign.atsign, ATKEY_NAME)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create selfkey"); goto exit; } - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } From 6d70d67f668aa4ecc0bb7511dbf8dbf95cef77bb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:29:28 -0400 Subject: [PATCH 071/193] fix: functional tests compilation errors --- .../tests/test_atclient_monitor.c | 5 +- .../tests/test_atclient_notify.c | 10 +- .../tests/test_atclient_publickey.c | 28 +- .../tests/test_atclient_selfkey.c | 22 +- .../tests/test_atclient_sharedkey.c | 497 +++++++++--------- 5 files changed, 258 insertions(+), 304 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 2be212b2..f30cc0b8 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -170,9 +170,8 @@ static int send_notification(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index a891c2ec..3d4e78ad 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -158,9 +158,8 @@ static int test_1_notify(atclient *atclient, char *notification_id) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; } @@ -196,9 +195,8 @@ static int test_2_notify_long_text(atclient *atclient, char *notification_id) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), - ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index 93b4e439..bb60a2fe 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -111,8 +111,7 @@ static int test_1_put(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -145,9 +144,7 @@ static int test_2_get(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -181,9 +178,7 @@ static int test_3_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -232,9 +227,7 @@ static int test_5_put_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -273,9 +266,7 @@ static int test_6_get_with_metadata(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -313,7 +304,8 @@ static int test_6_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttr: %d\n", atkey.metadata.ttr); - if (atclient_atkey_metadata_is_isencrypted_initialized(&(atkey.metadata)) && atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { + if (atclient_atkey_metadata_is_isencrypted_initialized(&(atkey.metadata)) && + atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed isencrypted comparison, got %d and expected %d\n", atkey.metadata.isencrypted, ATKEY_ISENCRYPTED); goto exit; @@ -343,9 +335,7 @@ static int test_7_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); goto exit; } @@ -427,7 +417,7 @@ static int tear_down(atclient *atclient) { goto exit; } - if (!atclient_stringutils_starts_with(recv, recvlen, "data:", strlen("data:"))) { + if (!atclient_stringutils_starts_with(recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Failed to delete: \"%.*s\"\n", (int)recvlen, recv); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index 90b94533..ea78f88c 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -133,8 +133,7 @@ static int test_2_put(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); goto exit; } @@ -167,8 +166,7 @@ static int test_3_get(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -204,9 +202,7 @@ static int test_4_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); goto exit; } @@ -253,8 +249,7 @@ static int test_6_put_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); goto exit; } @@ -291,8 +286,7 @@ static int test_7_get_with_metadata(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -350,9 +344,7 @@ static int test_8_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, strlen(ATKEY_NAME), ATKEY_SHAREDBY, - strlen(ATKEY_SHAREDBY), ATKEY_NAMESPACE, - ATKEY_NAMESPACE == NULL ? 0 : strlen(ATKEY_NAMESPACE))) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); goto exit; } @@ -428,7 +420,7 @@ static int tear_down(atclient *atclient) { goto exit; } - if (!atclient_stringutils_starts_with(recv, recvlen, "data:", strlen("data:"))) { + if (!atclient_stringutils_starts_with(recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index c110bcf7..70801382 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -1,12 +1,12 @@ +#include "functional_tests/config.h" +#include "functional_tests/helpers.h" #include #include #include +#include #include #include #include -#include -#include "functional_tests/config.h" -#include "functional_tests/helpers.h" #define TAG "test_atclient_sharedkey" @@ -15,8 +15,8 @@ #define ATKEY_SHAREDBY FIRST_ATSIGN #define ATKEY_SHAREDWITH SECOND_ATSIGN #define ATKEY_VALUE "Hello World! :D\n" -#define ATKEY_TTL 60*1000*5 // 5 minutes -#define ATKEY_TTR -1 // DO NOT CACHE +#define ATKEY_TTL 60 * 1000 * 5 // 5 minutes +#define ATKEY_TTR -1 // DO NOT CACHE static int test_1_put(atclient *atclient); static int test_2_get_as_sharedby(atclient *atclient); @@ -24,302 +24,277 @@ static int test_3_get_as_sharedwith(atclient *atclient); static int test_4_delete(atclient *atclient); static int test_5_should_not_exist_as_sharedby(atclient *atclient); -int main(int argc, char *argv[]) -{ - int ret = 1; - - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - - char *atsign1 = FIRST_ATSIGN; - const size_t atsign1len = strlen(atsign1); - - char *atsign2 = SECOND_ATSIGN; - const size_t atsign2len = strlen(atsign2); - - atclient atclient1; - atclient_init(&atclient1); - - atclient_atkeys atkeys1; - atclient_atkeys_init(&atkeys1); - - atclient atclient2; - atclient_init(&atclient2); - - atclient_atkeys atkeys2; - atclient_atkeys_init(&atkeys2); - - if((ret = functional_tests_set_up_atkeys(&atkeys1, atsign1, atsign1len)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if((ret = test_1_put(&atclient1)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); - goto exit; - } - - if((ret = test_2_get_as_sharedby(&atclient1)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); - goto exit; - } - - if((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if((ret = test_3_get_as_sharedwith(&atclient2)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); - goto exit; - } - - if((ret = test_4_delete(&atclient1)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); - goto exit; - } - - if((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); - goto exit; - } +int main(int argc, char *argv[]) { + int ret = 1; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + char *atsign1 = FIRST_ATSIGN; + const size_t atsign1len = strlen(atsign1); + + char *atsign2 = SECOND_ATSIGN; + const size_t atsign2len = strlen(atsign2); + atclient atclient1; + atclient_init(&atclient1); + + atclient_atkeys atkeys1; + atclient_atkeys_init(&atkeys1); + + atclient atclient2; + atclient_init(&atclient2); + + atclient_atkeys atkeys2; + atclient_atkeys_init(&atkeys2); + + if ((ret = functional_tests_set_up_atkeys(&atkeys1, atsign1, atsign1len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); goto exit; + } -exit: { - if(functional_tests_tear_down_sharedenckeys(&atclient1, atsign2) != 0) - { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tear_down: %d\n", ret); - } - if(functional_tests_tear_down_sharedenckeys(&atclient2, atsign1) != 0) - { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tear_down: %d\n", ret); - } - atclient_free(&atclient1); - atclient_atkeys_free(&atkeys1); - atclient_free(&atclient2); - atclient_atkeys_free(&atkeys2); - return ret; -} -} + if ((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } -static int test_1_put(atclient *atclient) -{ - int ret = 1; + if ((ret = test_1_put(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); + goto exit; + } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_put Begin\n"); + if ((ret = test_2_get_as_sharedby(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); + goto exit; + } - atclient_atkey atkey; - atclient_atkey_init(&atkey); + if ((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); - goto exit; - } + if ((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } - atclient_atkey_metadata_set_ttl(&atkey.metadata, ATKEY_TTL); - atclient_atkey_metadata_set_ttr(&atkey.metadata, ATKEY_TTR); + if ((ret = test_3_get_as_sharedwith(&atclient2)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); + goto exit; + } - if((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "put done\n"); + if ((ret = test_4_delete(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); + goto exit; + } + if ((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); goto exit; + } + + goto exit; + exit: { - atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_put End (%d)\n", ret); - return ret; + if (functional_tests_tear_down_sharedenckeys(&atclient1, atsign2) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tear_down: %d\n", ret); + } + if (functional_tests_tear_down_sharedenckeys(&atclient2, atsign1) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tear_down: %d\n", ret); + } + atclient_free(&atclient1); + atclient_atkeys_free(&atkeys1); + atclient_free(&atclient2); + atclient_atkeys_free(&atkeys2); + return ret; } } -static int test_2_get_as_sharedby(atclient *atclient) -{ - int ret = 1; - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_get_as_sharedby Begin\n"); - - atclient_atkey atkey; - atclient_atkey_init(&atkey); - - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; - - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); - goto exit; - } - - if((ret = atclient_get_sharedkey(atclient, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); - goto exit; - } - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); - - if((ret = strcmp(value, ATKEY_VALUE)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value mismatch\n"); - goto exit; - } - - // check ttl, should be 5 minutes - if(atkey.metadata.ttl != ATKEY_TTL) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttl mismatch. Expected %d, got %d\n", ATKEY_TTL, atkey.metadata.ttl); - ret = 1; - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttl matched: %d\n", atkey.metadata.ttl); - - if(atkey.metadata.ttr != ATKEY_TTR) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttr mismatch. Expected %d, got %d\n", ATKEY_TTR, atkey.metadata.ttr); - ret = 1; - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttr matched: %d\n", atkey.metadata.ttr); +static int test_1_put(atclient *atclient) { + int ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_put Begin\n"); + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + goto exit; + } + + atclient_atkey_metadata_set_ttl(&atkey.metadata, ATKEY_TTL); + atclient_atkey_metadata_set_ttr(&atkey.metadata, ATKEY_TTR); + + if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "put done\n"); + + goto exit; exit: { - atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_get_as_sharedby End (%d)\n", ret); - return ret; + atclient_atkey_free(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_put End (%d)\n", ret); + return ret; } } -static int test_3_get_as_sharedwith(atclient *atclient2) -{ - int ret = 1; - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith Begin\n"); - - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; - - atclient_atkey atkey; - atclient_atkey_init(&atkey); - - if((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); - goto exit; - } - - - if((ret = atclient_get_sharedkey(atclient2, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); - goto exit; - } - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); - - if((ret = strcmp(value, ATKEY_VALUE)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value mismatch\n"); - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value matched: %s == %s\n", value, ATKEY_VALUE); - - if(atkey.metadata.ttl != ATKEY_TTL) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttl mismatch. Expected %d, got %d\n", ATKEY_TTL, atkey.metadata.ttl); - ret = 1; - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttl matched: %d\n", atkey.metadata.ttl); - - if(atkey.metadata.ttr != ATKEY_TTR) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttr mismatch. Expected %d, got %d\n", ATKEY_TTR, atkey.metadata.ttr); - ret = 1; - goto exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttr matched: %d\n", atkey.metadata.ttr); +static int test_2_get_as_sharedby(atclient *atclient) { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_get_as_sharedby Begin\n"); + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + const size_t valuesize = 1024; + char value[valuesize]; + memset(value, 0, sizeof(char) * valuesize); + size_t valuelen = 0; + + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + goto exit; + } + + if ((ret = atclient_get_sharedkey(atclient, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); + goto exit; + } + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); + + if ((ret = strcmp(value, ATKEY_VALUE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value mismatch\n"); + goto exit; + } + + // check ttl, should be 5 minutes + if (atkey.metadata.ttl != ATKEY_TTL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttl mismatch. Expected %d, got %d\n", ATKEY_TTL, + atkey.metadata.ttl); + ret = 1; goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttl matched: %d\n", atkey.metadata.ttl); + + if (atkey.metadata.ttr != ATKEY_TTR) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttr mismatch. Expected %d, got %d\n", ATKEY_TTR, + atkey.metadata.ttr); + ret = 1; + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttr matched: %d\n", atkey.metadata.ttr); + + goto exit; exit: { - atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith End (%d)\n", ret); - return ret; + atclient_atkey_free(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_get_as_sharedby End (%d)\n", ret); + return ret; } } -static int test_4_delete(atclient *atclient) -{ - int ret = 1; +static int test_3_get_as_sharedwith(atclient *atclient2) { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith Begin\n"); + + const size_t valuesize = 1024; + char value[valuesize]; + memset(value, 0, sizeof(char) * valuesize); + size_t valuelen = 0; + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + goto exit; + } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_4_delete Begin\n"); + if ((ret = atclient_get_sharedkey(atclient2, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); + goto exit; + } - atclient_atkey atkey; - atclient_atkey_init(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); - if((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, strlen(ATKEY_KEY), ATKEY_SHAREDBY, strlen(ATKEY_SHAREDBY), ATKEY_SHAREDWITH, strlen(ATKEY_SHAREDWITH), ATKEY_NAMESPACE, strlen(ATKEY_NAMESPACE))) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); - goto exit; - } + if ((ret = strcmp(value, ATKEY_VALUE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value mismatch\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value matched: %s == %s\n", value, ATKEY_VALUE); - if((ret = atclient_delete(atclient, &atkey)) != 0) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); - goto exit; - } + if (atkey.metadata.ttl != ATKEY_TTL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttl mismatch. Expected %d, got %d\n", ATKEY_TTL, + atkey.metadata.ttl); + ret = 1; + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttl matched: %d\n", atkey.metadata.ttl); + if (atkey.metadata.ttr != ATKEY_TTR) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttr mismatch. Expected %d, got %d\n", ATKEY_TTR, + atkey.metadata.ttr); + ret = 1; goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttr matched: %d\n", atkey.metadata.ttr); + + goto exit; exit: { - atclient_atkey_free(&atkey); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_4_delete End (%d)\n", ret); - return ret; + atclient_atkey_free(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith End (%d)\n", ret); + return ret; } } -static int test_5_should_not_exist_as_sharedby(atclient *atclient) -{ - int ret = 1; +static int test_4_delete(atclient *atclient) { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_4_delete Begin\n"); + + atclient_atkey atkey; + atclient_atkey_init(&atkey); + + if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + goto exit; + } + + if ((ret = atclient_delete(atclient, &atkey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); + goto exit; + } + + goto exit; +exit: { + atclient_atkey_free(&atkey); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_4_delete End (%d)\n", ret); + return ret; +} +} - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_5_should_not_exist Begin\n"); +static int test_5_should_not_exist_as_sharedby(atclient *atclient) { + int ret = 1; - if((ret = functional_tests_selfkey_exists(atclient, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != false) - { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "functional_tests_selfkey_exists is 0 but should be 1: %d\n", ret); - goto exit; - } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_5_should_not_exist Begin\n"); - ret = 0; + if ((ret = functional_tests_selfkey_exists(atclient, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != false) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "functional_tests_selfkey_exists is 0 but should be 1: %d\n", ret); goto exit; -exit : { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_5_should_not_exist End (%d)\n", ret); - return ret; + } + + ret = 0; + goto exit; +exit: { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_5_should_not_exist End (%d)\n", ret); + return ret; } } From 78b76611533eeb3d511d4fbcfc007935dd05b997 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:35:27 -0400 Subject: [PATCH 072/193] chore: clean up test_stringutils.c --- packages/atclient/tests/test_stringutils.c | 112 ++++++++++++++------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/packages/atclient/tests/test_stringutils.c b/packages/atclient/tests/test_stringutils.c index bb64cda0..9c1145e3 100644 --- a/packages/atclient/tests/test_stringutils.c +++ b/packages/atclient/tests/test_stringutils.c @@ -6,88 +6,126 @@ #define TAG "test_stringutils" +static int test_1_starts_with(); +static int test_2_ends_with(); +static int test_3_trim_whitespace(); + int main() { int ret = 1; - atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); - const size_t outsize = 4096; - char *out = (char *)malloc(sizeof(char) * outsize); - memset(out, 0, sizeof(char) * outsize); - size_t outlen = 0; + if ((ret = test_1_starts_with()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_starts_with: %d\n", ret); + goto exit; + } + + if ((ret = test_2_ends_with()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_ends_with: %d\n", ret); + goto exit; + } + + if ((ret = test_3_trim_whitespace()) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_trim_whitespace: %d\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} - const size_t stringsize = 4096; - char *string = (char *)malloc(sizeof(char) * stringsize); - memset(string, 0, sizeof(char) * stringsize); - strcpy(string, "@bob"); +static int test_1_starts_with() { + int ret = 1; - const size_t tokenssize = 64; - char *tokens[tokenssize]; - memset(tokens, 0, sizeof(char *) * tokenssize); // set all pointers to NULL (0 - size_t tokenslen = 0; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_starts_with Begin\n"); - int startswith; + const char *string = "@bob"; // 1a. @bob starts with @ - startswith = atclient_stringutils_starts_with(string, "@"); - if (startswith != 1) { + if (atclient_stringutils_starts_with(string, "@") != true) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "@"); - ret = 1; goto exit; } // 1b. @bob does not start with 123 - startswith = atclient_stringutils_starts_with(string, "123"); - if (startswith != 0) { + if (atclient_stringutils_starts_with(string, "123") != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "bob"); ret = 1; goto exit; } - int endswith; - strcpy(string, "root.atsign.org:64"); + ret = 0; + goto exit; +exit: { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_starts_with End\n"); + return ret; +} +} + +static int test_2_ends_with() { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_ends_with Begin\n"); + + const char *string = "root.atsign.org:64"; + // 2a. root.atsign.org:64 ends with 64 - endswith = atclient_stringutils_ends_with(string, "64"); - if (endswith != 1) { + if (atclient_stringutils_ends_with(string, "64") != true) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, string, "64"); - ret = 1; goto exit; } // 2b. root.atsign.org:64 does not end with org - endswith = atclient_stringutils_ends_with(string, "org"); - if (endswith != 0) { + if (atclient_stringutils_ends_with(string, "org") != 0) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, string, "org"); - ret = 1; goto exit; } - // 3. trim whitespace and newline - strcpy(string, " scan jeremy_0\n "); + ret = 0; + goto exit; +exit: { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_ends_with End\n"); + return ret; +} +} + +static int test_3_trim_whitespace() { + int ret = 1; + + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_trim_whitespace Begin\n"); + + const char *string = " scan jeremy_0\n "; + + const size_t outsize = 4096; + char out[outsize]; + size_t outlen = 0; + const char *expectedresult = "scan jeremy_0"; - ret = atclient_stringutils_trim_whitespace(string, strlen(string), out, outsize, &outlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: %d | %s\n", ret, string); + if ((ret = atclient_stringutils_trim_whitespace(string, strlen(string), out, outsize, &outlen)) != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: %d | %s\n", ret, string); goto exit; } - if (strncmp(out, expectedresult, strlen(expectedresult)) != 0) { + if (strcmp(out, expectedresult) != 0) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: \"%s\" != \"%s\"\n", string, expectedresult); - ret = 1; goto exit; } ret = 0; goto exit; exit: { - free(out); - free(string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_trim_whitespace End\n"); return ret; } -} +} \ No newline at end of file From a6eb640876ffc74ba1e749df701657f441908561 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 19 Jul 2024 19:38:37 -0400 Subject: [PATCH 073/193] fix: atclient_get_selfkey_valid_arguments --- packages/atclient/src/atclient_get_selfkey.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index db6d813e..14dfb1e8 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -223,15 +223,15 @@ static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const goto exit; } - if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + if (!atclient_atkey_is_key_initialized(atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.key is not initialized when it should be\n"); goto exit; } - if (atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + if (!atclient_atkey_is_sharedby_initialized(atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is shared by someone else\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); goto exit; } From a3189eed00e6618141de1828748a743e255fc564 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 11:31:41 -0400 Subject: [PATCH 074/193] feat: remove atclient_atstr from atclient_atkey_metadata --- packages/atclient/include/atclient/metadata.h | 81 +- packages/atclient/src/atclient_get_selfkey.c | 2 +- .../atclient/src/atclient_get_sharedkey.c | 4 +- packages/atclient/src/metadata.c | 791 ++++++++++-------- packages/atclient/tests/test_atkey_metadata.c | 276 ++++-- 5 files changed, 705 insertions(+), 449 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 285b6525..30e51370 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -2,8 +2,6 @@ #ifndef ATCLIENT_METADATA_H #define ATCLIENT_METADATA_H -#include "atclient/atsign.h" -#include "atclient/atstr.h" #include "cJSON.h" #include #include @@ -88,17 +86,17 @@ typedef struct atclient_atkey_metadata { // Represents the atSign who created this atkey. // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atsign createdby; + char *createdby; // Represents the atSign who last updated this atkey. // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atsign updatedby; // repreesnts the atSign who last updated this atkey. read from protocol string only + char *updatedby; // repreesnts the atSign who last updated this atkey. read from protocol string only // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr status; + char *status; // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. @@ -109,29 +107,29 @@ typedef struct atclient_atkey_metadata { // This field is derived from the [ttl] value. If the ttl value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr expiresat; + char *expiresat; // Date and time representing when the atkey will be available at (in UTC date/time format). // This field is derived from the [ttb] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr availableat; + char *availableat; // Date and time repreesnts when the atkey will refresh at (in UTC date/time format). // This field is derived from the [ttr] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr refreshat; + char *refreshat; // Date and time representing when the atkey was created at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // this field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr createdat; + char *createdat; // Date and time representing when the atkey was last updated at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - atclient_atstr updatedat; // date and time representing when the key was last updated, read only + char *updatedat; // date and time representing when the key was last updated, read only // ispublic=true means this key is accessible by all atSigns and contains non-encrypted data. // ispublic=false means this key is only accessible by either sharedWith or sharedBy @@ -194,18 +192,18 @@ typedef struct atclient_atkey_metadata { // the data came from the owner of the public/private keypair // This field is read from protocol string and can also be set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr datasignature; + char *datasignature; // Represents the status of the shared key. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr sharedkeystatus; + char *sharedkeystatus; // Stores the sharedkey that the data is encrypted with. This is only set if sharedWith is set. The contents will be // encrypted using the public key of the sharedWith atSign. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr sharedkeyenc; + char *sharedkeyenc; // Regarding the following 2 fields... // The pubkey pair stores the hash of the encryption public key used to encrypt the [sharedKeyEnc]. The hash is used @@ -216,17 +214,17 @@ typedef struct atclient_atkey_metadata { // The hash of the public key used to encrypt the [sharedKeyEnc]. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr pubkeyhash; + char *pubkeyhash; // The algorithm used to hash the public key used to encrypt the [sharedKeyEnc] (e.g. "sha256" or "sha512") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr pubkeyalgo; + char *pubkeyalgo; // The type of encoding the value is (e.g. "base64") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr encoding; + char *encoding; // The name of the key used to encrypt the value. If not provided, use sharedKeyEnc in the metadata. If sharedKeyEnc // is not provided, use the default shared key. If enckeyname is provided, just the key name must be provided example @@ -234,31 +232,31 @@ typedef struct atclient_atkey_metadata { // must be only be 'shared_key.wavi' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr enckeyname; + char *enckeyname; // The name of the algorithm used to encrypt the value. For data, the default algorithm is 'AES/SIC/PKCS7Padding', for // cryptographic keys, the default algorithm is 'RSA' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr encalgo; + char *encalgo; // The initialization vector or nonce used when the data was encrypted with the shared symmetric encryption key // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr ivnonce; + char *ivnonce; - // TODO: info about this metadata + // TODO: documentation info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr skeenckeyname; + char *skeenckeyname; - // TODO: info about this metadata + // TODO: documentation info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - atclient_atstr skeencalgo; + char *skeencalgo; - // Holds the metadata fields that have not been initialized (0) or have been initialized (1) - uint8_t initializedfields[4]; + // Internal field that holds the metadata fields that have not been initialized (0) or have been initialized (1) + uint8_t _initializedfields[4]; } atclient_atkey_metadata; /** @@ -384,28 +382,17 @@ int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const lon int atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd); int atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary); int atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted); -int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, - const size_t datasignaturelen); -int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus, - const size_t sharedkeystatuslen); -int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc, - const size_t sharedkeyenclen); -int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash, - const size_t pubkeyhashlen); -int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo, - const size_t pubkeyalgolen); -int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, const char *encoding, - const size_t encodinglen); -int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname, - const size_t enckeynamelen); -int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo, - const size_t encalgolen); -int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce, - const size_t ivnoncelen); -int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname, - const size_t skeenckeynamelen); -int atclient_atkey_metadata_set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo, - const size_t skeencalgolen); +int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature); +int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus); +int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc); +int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash); +int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo); +int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, const char *encoding); +int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname); +int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo); +int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce); +int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname); +int atclient_atkey_metadata_set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo); /** * @brief frees the metadata struct's variables that were allocated in the atclient_atkey_metadata_init function. diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 14dfb1e8..cb7f126a 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -120,7 +120,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce.str, atkey->metadata.ivnonce.len, iv, + ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index e6a14a9c..af201253 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -265,7 +265,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke // manage IV if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce.str, atkey->metadata.ivnonce.len, iv, + ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); @@ -451,7 +451,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at // manage IV if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce.str, atkey->metadata.ivnonce.len, iv, + ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 0ff1d236..1524f34c 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1,6 +1,4 @@ #include "atclient/metadata.h" -#include "atclient/atsign.h" -#include "atclient/atstr.h" #include "atclient/stringutils.h" #include "atlogger/atlogger.h" #include "cJSON.h" @@ -101,15 +99,15 @@ static void unset_ivnonce(atclient_atkey_metadata *metadata); static void unset_skeenckeyname(atclient_atkey_metadata *metadata); static void unset_skeencalgo(atclient_atkey_metadata *metadata); -static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby, const size_t createdbylen); -static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby, const size_t updatedbylen); -static int set_status(atclient_atkey_metadata *metadata, const char *status, const size_t statuslen); +static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby); +static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby); +static int set_status(atclient_atkey_metadata *metadata, const char *status); static void set_version(atclient_atkey_metadata *metadata, int version); -static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat, const size_t expiresatlen); -static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat, const size_t availableatlen); -static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat, const size_t refreshatlen); -static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat, const size_t createdatlen); -static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat, const size_t updatedatlen); +static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat); +static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat); +static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat); +static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat); +static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat); static void set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); static void set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden); static void set_iscached(atclient_atkey_metadata *metadata, const bool iscached); @@ -119,20 +117,17 @@ static void set_ttr(atclient_atkey_metadata *metadata, const long ttr); static void set_ccd(atclient_atkey_metadata *metadata, const bool ccd); static void set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary); static void set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted); -static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, - const size_t datasignaturelen); -static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus, - const size_t sharedkeystatuslen); -static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc, const size_t sharedkeyenclen); -static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash, const size_t pubkeyhashlen); -static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo, const size_t pubkeyalgolen); -static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding, const size_t encodinglen); -static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname, const size_t enckeynamelen); -static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo, const size_t encalgolen); -static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce, const size_t ivnoncelen); -static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname, - const size_t skeenckeynamelen); -static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo, const size_t skeencalgolen); +static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature); +static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus); +static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc); +static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash); +static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo); +static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding); +static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname); +static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo); +static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce); +static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname); +static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo); void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata) { memset(metadata, 0, sizeof(atclient_atkey_metadata)); @@ -160,27 +155,27 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, cJSON *createdby = cJSON_GetObjectItem(json, "createdBy"); if (createdby != NULL) { if (createdby->type != cJSON_NULL) { - set_createdby(metadata, createdby->valuestring, strlen(createdby->valuestring)); + set_createdby(metadata, createdby->valuestring); } else { - set_createdby(metadata, "null", 4); + set_createdby(metadata, "null"); } } cJSON *updatedby = cJSON_GetObjectItem(json, "updatedBy"); if (updatedby != NULL) { if (updatedby->type != cJSON_NULL) { - set_updatedby(metadata, updatedby->valuestring, strlen(updatedby->valuestring)); + set_updatedby(metadata, updatedby->valuestring); } else { - set_updatedby(metadata, "null", 4); + set_updatedby(metadata, "null"); } } cJSON *status = cJSON_GetObjectItem(json, "status"); if (status != NULL) { if (status->type != cJSON_NULL) { - set_status(metadata, status->valuestring, strlen(status->valuestring)); + set_status(metadata, status->valuestring); } else { - set_status(metadata, "null", 4); + set_status(metadata, "null"); } } @@ -196,45 +191,45 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, cJSON *expiresat = cJSON_GetObjectItem(json, "expiresAt"); if (expiresat != NULL) { if (expiresat->type != cJSON_NULL) { - set_expiresat(metadata, expiresat->valuestring, strlen(expiresat->valuestring)); + set_expiresat(metadata, expiresat->valuestring); } else { - set_expiresat(metadata, "null", 4); + set_expiresat(metadata, "null"); } } cJSON *availableat = cJSON_GetObjectItem(json, "availableAt"); if (availableat != NULL) { if (availableat->type != cJSON_NULL) { - set_availableat(metadata, availableat->valuestring, strlen(availableat->valuestring)); + set_availableat(metadata, availableat->valuestring); } else { - set_availableat(metadata, "null", 4); + set_availableat(metadata, "null"); } } cJSON *refreshat = cJSON_GetObjectItem(json, "refreshAt"); if (refreshat != NULL) { if (refreshat->type != cJSON_NULL) { - set_refreshat(metadata, refreshat->valuestring, strlen(refreshat->valuestring)); + set_refreshat(metadata, refreshat->valuestring); } else { - set_refreshat(metadata, "null", 4); + set_refreshat(metadata, "null"); } } cJSON *createdat = cJSON_GetObjectItem(json, "createdAt"); if (createdat != NULL) { if (createdat->type != cJSON_NULL) { - set_createdat(metadata, createdat->valuestring, strlen(createdat->valuestring)); + set_createdat(metadata, createdat->valuestring); } else { - set_createdat(metadata, "null", 4); + set_createdat(metadata, "null"); } } cJSON *updatedat = cJSON_GetObjectItem(json, "updatedAt"); if (updatedat != NULL) { if (updatedat->type != cJSON_NULL) { - set_updatedat(metadata, updatedat->valuestring, strlen(updatedat->valuestring)); + set_updatedat(metadata, updatedat->valuestring); } else { - set_updatedat(metadata, "null", 4); + set_updatedat(metadata, "null"); } } @@ -313,99 +308,99 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, cJSON *datasignature = cJSON_GetObjectItem(json, "dataSignature"); if (datasignature != NULL) { if (datasignature->type != cJSON_NULL) { - set_datasignature(metadata, datasignature->valuestring, strlen(datasignature->valuestring)); + set_datasignature(metadata, datasignature->valuestring); } else { - set_datasignature(metadata, "null", 4); + set_datasignature(metadata, "null"); } } cJSON *sharedkeystatus = cJSON_GetObjectItem(json, "sharedKeyStatus"); if (sharedkeystatus != NULL) { if (sharedkeystatus->type != cJSON_NULL) { - set_sharedkeystatus(metadata, sharedkeystatus->valuestring, strlen(sharedkeystatus->valuestring)); + set_sharedkeystatus(metadata, sharedkeystatus->valuestring); } else { - set_sharedkeystatus(metadata, "null", 4); + set_sharedkeystatus(metadata, "null"); } } cJSON *sharedkeyenc = cJSON_GetObjectItem(json, "sharedKeyEnc"); if (sharedkeyenc != NULL) { if (sharedkeyenc->type != cJSON_NULL) { - set_sharedkeyenc(metadata, sharedkeyenc->valuestring, strlen(sharedkeyenc->valuestring)); + set_sharedkeyenc(metadata, sharedkeyenc->valuestring); } else { - set_sharedkeyenc(metadata, "null", 4); + set_sharedkeyenc(metadata, "null"); } } cJSON *pubkeyhash = cJSON_GetObjectItem(json, "pubKeyHash"); if (pubkeyhash != NULL) { if (pubkeyhash->type != cJSON_NULL) { - set_pubkeyhash(metadata, pubkeyhash->valuestring, strlen(pubkeyhash->valuestring)); + set_pubkeyhash(metadata, pubkeyhash->valuestring); } else { - set_pubkeyhash(metadata, "null", 4); + set_pubkeyhash(metadata, "null"); } } cJSON *pubkeyalgo = cJSON_GetObjectItem(json, "pubKeyAlgo"); if (pubkeyalgo != NULL) { if (pubkeyalgo->type != cJSON_NULL) { - set_pubkeyalgo(metadata, pubkeyalgo->valuestring, strlen(pubkeyalgo->valuestring)); + set_pubkeyalgo(metadata, pubkeyalgo->valuestring); } else { - set_pubkeyalgo(metadata, "null", 4); + set_pubkeyalgo(metadata, "null"); } } cJSON *encoding = cJSON_GetObjectItem(json, "encoding"); if (encoding != NULL) { if (encoding->type != cJSON_NULL) { - set_encoding(metadata, encoding->valuestring, strlen(encoding->valuestring)); + set_encoding(metadata, encoding->valuestring); } else { - set_encoding(metadata, "null", 4); + set_encoding(metadata, "null"); } } cJSON *enckeyname = cJSON_GetObjectItem(json, "encKeyName"); if (enckeyname != NULL) { if (enckeyname->type != cJSON_NULL) { - set_enckeyname(metadata, enckeyname->valuestring, strlen(enckeyname->valuestring)); + set_enckeyname(metadata, enckeyname->valuestring); } else { - set_enckeyname(metadata, "null", 4); + set_enckeyname(metadata, "null"); } } cJSON *encalgo = cJSON_GetObjectItem(json, "encAlgo"); if (encalgo != NULL) { if (encalgo->type != cJSON_NULL) { - set_encalgo(metadata, encalgo->valuestring, strlen(encalgo->valuestring)); + set_encalgo(metadata, encalgo->valuestring); } else { - set_encalgo(metadata, "null", 4); + set_encalgo(metadata, "null"); } } cJSON *ivnonce = cJSON_GetObjectItem(json, "ivNonce"); if (ivnonce != NULL) { if (ivnonce->type != cJSON_NULL) { - set_ivnonce(metadata, ivnonce->valuestring, strlen(ivnonce->valuestring)); + set_ivnonce(metadata, ivnonce->valuestring); } else { - set_ivnonce(metadata, "null", 4); + set_ivnonce(metadata, "null"); } } cJSON *skeenckeyname = cJSON_GetObjectItem(json, "skeEncKeyName"); if (skeenckeyname != NULL) { if (skeenckeyname->type != cJSON_NULL) { - set_skeenckeyname(metadata, skeenckeyname->valuestring, strlen(skeenckeyname->valuestring)); + set_skeenckeyname(metadata, skeenckeyname->valuestring); } else { - set_skeenckeyname(metadata, "null", 4); + set_skeenckeyname(metadata, "null"); } } cJSON *skeencalgo = cJSON_GetObjectItem(json, "skeEncAlgo"); if (skeencalgo != NULL) { if (skeencalgo->type != cJSON_NULL) { - set_skeencalgo(metadata, skeencalgo->valuestring, strlen(skeencalgo->valuestring)); + set_skeencalgo(metadata, skeencalgo->valuestring); } else { - set_skeencalgo(metadata, "null", 4); + set_skeencalgo(metadata, "null"); } } } @@ -417,15 +412,15 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, cJSON *root = cJSON_CreateObject(); if (atclient_atkey_metadata_is_createdby_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdBy", metadata->createdby.atsign); + cJSON_AddStringToObject(root, "createdBy", metadata->createdby); } if (atclient_atkey_metadata_is_updatedby_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedBy", metadata->updatedby.atsign); + cJSON_AddStringToObject(root, "updatedBy", metadata->updatedby); } if (atclient_atkey_metadata_is_status_initialized(metadata)) { - cJSON_AddStringToObject(root, "status", metadata->status.str); + cJSON_AddStringToObject(root, "status", metadata->status); } if (atclient_atkey_metadata_is_version_initialized(metadata)) { @@ -433,23 +428,23 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, } if (atclient_atkey_metadata_is_expiresat_initialized(metadata)) { - cJSON_AddStringToObject(root, "expiresAt", metadata->expiresat.str); + cJSON_AddStringToObject(root, "expiresAt", metadata->expiresat); } if (atclient_atkey_metadata_is_availableat_initialized(metadata)) { - cJSON_AddStringToObject(root, "availableAt", metadata->availableat.str); + cJSON_AddStringToObject(root, "availableAt", metadata->availableat); } if (atclient_atkey_metadata_is_refreshat_initialized(metadata)) { - cJSON_AddStringToObject(root, "refreshAt", metadata->refreshat.str); + cJSON_AddStringToObject(root, "refreshAt", metadata->refreshat); } if (atclient_atkey_metadata_is_createdat_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdAt", metadata->createdat.str); + cJSON_AddStringToObject(root, "createdAt", metadata->createdat); } if (atclient_atkey_metadata_is_updatedat_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedAt", metadata->updatedat.str); + cJSON_AddStringToObject(root, "updatedAt", metadata->updatedat); } if (atclient_atkey_metadata_is_ispublic_initialized(metadata)) { @@ -489,47 +484,47 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, } if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - cJSON_AddStringToObject(root, "dataSignature", metadata->datasignature.str); + cJSON_AddStringToObject(root, "dataSignature", metadata->datasignature); } if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->sharedkeystatus.str); + cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->sharedkeystatus); } if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->sharedkeyenc.str); + cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->sharedkeyenc); } if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyHash", metadata->pubkeyhash.str); + cJSON_AddStringToObject(root, "pubKeyHash", metadata->pubkeyhash); } if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pubkeyalgo.str); + cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pubkeyalgo); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - cJSON_AddStringToObject(root, "encoding", metadata->encoding.str); + cJSON_AddStringToObject(root, "encoding", metadata->encoding); } if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - cJSON_AddStringToObject(root, "encKeyName", metadata->enckeyname.str); + cJSON_AddStringToObject(root, "encKeyName", metadata->enckeyname); } if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "encAlgo", metadata->encalgo.str); + cJSON_AddStringToObject(root, "encAlgo", metadata->encalgo); } if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - cJSON_AddStringToObject(root, "ivNonce", metadata->ivnonce.str); + cJSON_AddStringToObject(root, "ivNonce", metadata->ivnonce); } if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncKeyName", metadata->skeenckeyname.str); + cJSON_AddStringToObject(root, "skeEncKeyName", metadata->skeenckeyname); } if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncAlgo", metadata->skeencalgo.str); + cJSON_AddStringToObject(root, "skeEncAlgo", metadata->skeencalgo); } char *jsonstr = cJSON_Print(root); @@ -672,58 +667,48 @@ size_t atclient_atkey_metadata_isencrypted_strlen(const atclient_atkey_metadata } size_t atclient_atkey_metadata_datasignature_strlen(const atclient_atkey_metadata *metadata) { - return 15 // :dataSignature: - + metadata->datasignature.len; + // :dataSignature: + return strlen(":dataSignature:") + strlen(metadata->datasignature); } size_t atclient_atkey_metadata_sharedkeystatus_strlen(const atclient_atkey_metadata *metadata) { - return 17 // :sharedKeyStatus: - + metadata->sharedkeystatus.len; + return strlen(":sharedKeyStatus:") + strlen(metadata->sharedkeystatus); } size_t atclient_atkey_metadata_sharedkeyenc_strlen(const atclient_atkey_metadata *metadata) { - return 14 // :sharedKeyEnc: - + metadata->sharedkeyenc.len; + return strlen(":sharedKeyEnc:") + strlen(metadata->sharedkeyenc); } size_t atclient_atkey_metadata_pubkeyhash_strlen(const atclient_atkey_metadata *metadata) { - return 6 // :hash: - + metadata->pubkeyhash.len; + return strlen(":hash:") + strlen(metadata->pubkeyhash); } size_t atclient_atkey_metadata_pubkeyalgo_strlen(const atclient_atkey_metadata *metadata) { - return 6 // :algo: - + metadata->pubkeyalgo.len; + return strlen(":algo:") + strlen(metadata->pubkeyalgo); } size_t atclient_atkey_metadata_encoding_strlen(const atclient_atkey_metadata *metadata) { - return 10 // :encoding: - + metadata->encoding.len; + return strlen(":encoding:") + strlen(metadata->encoding); } size_t atclient_atkey_metadata_enckeyname_strlen(const atclient_atkey_metadata *metadata) { - return 12 // :encKeyName: - + metadata->enckeyname.len; + return strlen(":encKeyName:") + strlen(metadata->enckeyname); } size_t atclient_atkey_metadata_encalgo_strlen(const atclient_atkey_metadata *metadata) { - return 9 // :encAlgo: - + metadata->encalgo.len; + return strlen(":encAlgo:") + strlen(metadata->encalgo); } size_t atclient_atkey_metadata_ivnonce_strlen(const atclient_atkey_metadata *metadata) { - return 9 // :ivNonce: - + metadata->ivnonce.len; + return strlen(":ivNonce:") + strlen(metadata->ivnonce); } size_t atclient_atkey_metadata_skeenckeyname_strlen(const atclient_atkey_metadata *metadata) { - return 15 // :skeEncKeyName: - + metadata->skeenckeyname.len; + return strlen(":skeEncKeyName:") + strlen(metadata->skeenckeyname); } size_t atclient_atkey_metadata_skeencalgo_strlen(const atclient_atkey_metadata *metadata) { - return 12 // :skeEncAlgo: - + metadata->skeencalgo.len; + return strlen(":skeEncAlgo:") + strlen(metadata->skeencalgo); } int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadatastr) { @@ -743,7 +728,7 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad const size_t expected_metadatastr_len = metadatastrsize - 1; size_t pos = 0; - if((*metadatastr = malloc(sizeof(char) * metadatastrsize)) == NULL) { + if ((*metadatastr = malloc(sizeof(char) * metadatastrsize)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } @@ -795,58 +780,58 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad } if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - sprintf(*metadatastr + pos, ":dataSignature:%s", metadata->datasignature.str); - pos += 15 + metadata->datasignature.len; + sprintf(*metadatastr + pos, ":dataSignature:%s", metadata->datasignature); + pos += strlen(":dataSignature:") + strlen(metadata->datasignature); } if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - sprintf(*metadatastr + pos, ":sharedKeyStatus:%s", metadata->sharedkeystatus.str); - pos += 17 + metadata->sharedkeystatus.len; + sprintf(*metadatastr + pos, ":sharedKeyStatus:%s", metadata->sharedkeystatus); + pos += strlen(":sharedKeyStatus:") + strlen(metadata->sharedkeystatus); } if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - sprintf(*metadatastr + pos, ":sharedKeyEnc:%s", metadata->sharedkeyenc.str); - pos += 14 + metadata->sharedkeyenc.len; + sprintf(*metadatastr + pos, ":sharedKeyEnc:%s", metadata->sharedkeyenc); + pos += strlen(":sharedKeyEnc:") + strlen(metadata->sharedkeyenc); } if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - sprintf(*metadatastr + pos, ":hash:%s", metadata->pubkeyhash.str); - pos += 6 + metadata->pubkeyhash.len; + sprintf(*metadatastr + pos, ":hash:%s", metadata->pubkeyhash); + pos += strlen(":hash:") + strlen(metadata->pubkeyhash); } if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":algo:%s", metadata->pubkeyalgo.str); - pos += 6 + metadata->pubkeyalgo.len; + sprintf(*metadatastr + pos, ":algo:%s", metadata->pubkeyalgo); + pos += strlen(":algo:") + strlen(metadata->pubkeyalgo); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encoding:%s", metadata->encoding.str); - pos += 10 + metadata->encoding.len; + sprintf(*metadatastr + pos, ":encoding:%s", metadata->encoding); + pos += strlen(":encoding:") + strlen(metadata->encoding); } if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encKeyName:%s", metadata->enckeyname.str); - pos += 12 + metadata->enckeyname.len; + sprintf(*metadatastr + pos, ":encKeyName:%s", metadata->enckeyname); + pos += strlen(":encKeyName:") + strlen(metadata->enckeyname); } if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encAlgo:%s", metadata->encalgo.str); - pos += 9 + metadata->encalgo.len; + sprintf(*metadatastr + pos, ":encAlgo:%s", metadata->encalgo); + pos += strlen(":encAlgo:") + strlen(metadata->encalgo); } if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - sprintf(*metadatastr + pos, ":ivNonce:%s", metadata->ivnonce.str); - pos += 9 + metadata->ivnonce.len; + sprintf(*metadatastr + pos, ":ivNonce:%s", metadata->ivnonce); + pos += strlen(":ivNonce:") + strlen(metadata->ivnonce); } if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - sprintf(*metadatastr + pos, ":skeEncKeyName:%s", metadata->skeenckeyname.str); - pos += 15 + metadata->skeenckeyname.len; + sprintf(*metadatastr + pos, ":skeEncKeyName:%s", metadata->skeenckeyname); + pos += strlen(":skeEncKeyName:") + strlen(metadata->skeenckeyname); } if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":skeEncAlgo:%s", metadata->skeencalgo.str); - pos += 12 + metadata->skeencalgo.len; + sprintf(*metadatastr + pos, ":skeEncAlgo:%s", metadata->skeencalgo); + pos += strlen(":skeEncAlgo:") + strlen(metadata->skeencalgo); } if (strlen(*metadatastr) != (expected_metadatastr_len)) { @@ -1049,13 +1034,12 @@ int atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, c return 0; } -int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, - const size_t datasignaturelen) { +int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature) { int ret = 1; if (is_datasignature_initialized(metadata)) { unset_datasignature(metadata); } - if ((ret = set_datasignature(metadata, datasignature, datasignaturelen)) != 0) { + if ((ret = set_datasignature(metadata, datasignature)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature failed\n"); goto exit; } @@ -1064,13 +1048,12 @@ int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus, - const size_t sharedkeystatuslen) { +int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus) { int ret = 1; if (is_sharedkeystatus_initialized(metadata)) { unset_sharedkeystatus(metadata); } - if ((ret = set_sharedkeystatus(metadata, sharedkeystatus, sharedkeystatuslen)) != 0) { + if ((ret = set_sharedkeystatus(metadata, sharedkeystatus)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus failed\n"); goto exit; } @@ -1079,13 +1062,12 @@ int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadat exit: { return ret; } } -int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc, - const size_t sharedkeyenclen) { +int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc) { int ret = 1; if (is_sharedkeyenc_initialized(metadata)) { unset_sharedkeyenc(metadata); } - if ((ret = set_sharedkeyenc(metadata, sharedkeyenc, sharedkeyenclen)) != 0) { + if ((ret = set_sharedkeyenc(metadata, sharedkeyenc)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc failed\n"); goto exit; } @@ -1094,13 +1076,12 @@ int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash, - const size_t pubkeyhashlen) { +int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash) { int ret = 1; if (is_pubkeyhash_initialized(metadata)) { unset_pubkeyhash(metadata); } - if ((ret = set_pubkeyhash(metadata, pubkeyhash, pubkeyhashlen)) != 0) { + if ((ret = set_pubkeyhash(metadata, pubkeyhash)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash failed\n"); goto exit; } @@ -1109,13 +1090,12 @@ int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, co exit: { return ret; } } -int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo, - const size_t pubkeyalgolen) { +int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo) { int ret = 1; if (is_pubkeyalgo_initialized(metadata)) { unset_pubkeyalgo(metadata); } - if ((ret = set_pubkeyalgo(metadata, pubkeyalgo, pubkeyalgolen)) != 0) { + if ((ret = set_pubkeyalgo(metadata, pubkeyalgo)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo failed\n"); goto exit; } @@ -1124,14 +1104,13 @@ int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, co exit: { return ret; } } -int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, const char *encoding, - const size_t encodinglen) { +int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, const char *encoding) { int ret = 1; if (is_encoding_initialized(metadata)) { unset_encoding(metadata); } - if ((ret = set_encoding(metadata, encoding, encodinglen)) != 0) { + if ((ret = set_encoding(metadata, encoding)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding failed\n"); goto exit; } @@ -1140,13 +1119,12 @@ int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, cons exit: { return ret; } } -int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname, - const size_t enckeynamelen) { +int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname) { int ret = 1; if (is_enckeyname_initialized(metadata)) { unset_enckeyname(metadata); } - if ((ret = set_enckeyname(metadata, enckeyname, enckeynamelen)) != 0) { + if ((ret = set_enckeyname(metadata, enckeyname)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname failed\n"); goto exit; } @@ -1155,13 +1133,12 @@ int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, co exit: { return ret; } } -int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo, - const size_t encalgolen) { +int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo) { int ret = 1; if (is_encalgo_initialized(metadata)) { unset_encalgo(metadata); } - if ((ret = set_encalgo(metadata, encalgo, encalgolen)) != 0) { + if ((ret = set_encalgo(metadata, encalgo)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo failed\n"); goto exit; } @@ -1170,13 +1147,12 @@ int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const exit: { return ret; } } -int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce, - const size_t ivnoncelen) { +int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce) { int ret = 1; if (is_ivnonce_initialized(metadata)) { unset_ivnonce(metadata); } - if ((ret = set_ivnonce(metadata, ivnonce, ivnoncelen)) != 0) { + if ((ret = set_ivnonce(metadata, ivnonce)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce failed\n"); goto exit; } @@ -1185,13 +1161,12 @@ int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const exit: { return ret; } } -int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname, - const size_t skeenckeynamelen) { +int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname) { int ret = 1; if (is_skeenckeyname_initialized(metadata)) { unset_skeenckeyname(metadata); } - if ((ret = set_skeenckeyname(metadata, skeenckeyname, skeenckeynamelen)) != 0) { + if ((ret = set_skeenckeyname(metadata, skeenckeyname)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname failed\n"); goto exit; } @@ -1200,13 +1175,12 @@ int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo, - const size_t skeencalgolen) { +int atclient_atkey_metadata_set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo) { int ret = 1; if (is_skeencalgo_initialized(metadata)) { unset_skeencalgo(metadata); } - if ((ret = set_skeencalgo(metadata, skeencalgo, skeencalgolen)) != 0) { + if ((ret = set_skeencalgo(metadata, skeencalgo)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo failed\n"); goto exit; } @@ -1336,366 +1310,375 @@ void atclient_atkey_metadata_free(atclient_atkey_metadata *metadata) { } static bool is_createdby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] & ATKEY_METADATA_CREATEDBY_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] & ATKEY_METADATA_CREATEDBY_INITIALIZED); } static bool is_updatedby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] & ATKEY_METADATA_UPDATEDBY_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] & ATKEY_METADATA_UPDATEDBY_INITIALIZED); } static bool is_status_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_STATUS_INDEX] & ATKEY_METADATA_STATUS_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] & ATKEY_METADATA_STATUS_INITIALIZED); } static bool is_version_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_VERSION_INDEX] & ATKEY_METADATA_VERSION_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] & ATKEY_METADATA_VERSION_INITIALIZED); } static bool is_expiresat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] & ATKEY_METADATA_EXPIRESAT_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] & ATKEY_METADATA_EXPIRESAT_INITIALIZED); } static bool is_availableat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] & ATKEY_METADATA_AVAILABLEAT_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] & ATKEY_METADATA_AVAILABLEAT_INITIALIZED); } static bool is_refreshat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] & ATKEY_METADATA_REFRESHAT_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] & ATKEY_METADATA_REFRESHAT_INITIALIZED); } static bool is_createdat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] & ATKEY_METADATA_CREATEDAT_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] & ATKEY_METADATA_CREATEDAT_INITIALIZED); } static bool is_updatedat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] & ATKEY_METADATA_UPDATEDAT_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] & ATKEY_METADATA_UPDATEDAT_INITIALIZED); } static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] & ATKEY_METADATA_ISPUBLIC_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] & ATKEY_METADATA_ISPUBLIC_INITIALIZED); } static bool is_ishidden_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] & ATKEY_METADATA_ISHIDDEN_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] & ATKEY_METADATA_ISHIDDEN_INITIALIZED); } static bool is_iscached_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ISCACHED_INDEX] & ATKEY_METADATA_ISCACHED_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] & ATKEY_METADATA_ISCACHED_INITIALIZED); } static bool is_ttl_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_TTL_INDEX] & ATKEY_METADATA_TTL_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] & ATKEY_METADATA_TTL_INITIALIZED); } static bool is_ttb_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_TTB_INDEX] & ATKEY_METADATA_TTB_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] & ATKEY_METADATA_TTB_INITIALIZED); } static bool is_ttr_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_TTR_INDEX] & ATKEY_METADATA_TTR_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] & ATKEY_METADATA_TTR_INITIALIZED); } static bool is_ccd_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_CCD_INDEX] & ATKEY_METADATA_CCD_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] & ATKEY_METADATA_CCD_INITIALIZED); } static bool is_isbinary_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ISBINARY_INDEX] & ATKEY_METADATA_ISBINARY_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] & ATKEY_METADATA_ISBINARY_INITIALIZED); } static bool is_isencrypted_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] & ATKEY_METADATA_ISENCRYPTED_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] & ATKEY_METADATA_ISENCRYPTED_INITIALIZED); } static bool is_datasignature_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] & ATKEY_METADATA_DATASIGNATURE_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] & ATKEY_METADATA_DATASIGNATURE_INITIALIZED); } static bool is_sharedkeystatus_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & + return (metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED); } static bool is_sharedkeyenc_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] & ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] & ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); } static bool is_pubkeyhash_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] & ATKEY_METADATA_PUBKEYHASH_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] & ATKEY_METADATA_PUBKEYHASH_INITIALIZED); } static bool is_pubkeyalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] & ATKEY_METADATA_PUBKEYALGO_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] & ATKEY_METADATA_PUBKEYALGO_INITIALIZED); } static bool is_encoding_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ENCODING_INDEX] & ATKEY_METADATA_ENCODING_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] & ATKEY_METADATA_ENCODING_INITIALIZED); } static bool is_enckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] & ATKEY_METADATA_ENCKEYNAME_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] & ATKEY_METADATA_ENCKEYNAME_INITIALIZED); } static bool is_encalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_ENCALGO_INDEX] & ATKEY_METADATA_ENCALGO_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] & ATKEY_METADATA_ENCALGO_INITIALIZED); } static bool is_ivnonce_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_IVNONCE_INDEX] & ATKEY_METADATA_IVNONCE_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] & ATKEY_METADATA_IVNONCE_INITIALIZED); } static bool is_skeenckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] & ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] & ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); } static bool is_skeencalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] & ATKEY_METADATA_SKEENCALGO_INITIALIZED); + return (metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] & ATKEY_METADATA_SKEENCALGO_INITIALIZED); } static void set_is_createdby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] |= ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] |= ATKEY_METADATA_CREATEDBY_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] &= ~ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] &= ~ATKEY_METADATA_CREATEDBY_INITIALIZED; } } static void set_is_updatedby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] |= ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] |= ATKEY_METADATA_UPDATEDBY_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] &= ~ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] &= ~ATKEY_METADATA_UPDATEDBY_INITIALIZED; } } static void set_is_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_STATUS_INDEX] |= ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] |= ATKEY_METADATA_STATUS_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_STATUS_INDEX] &= ~ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] &= ~ATKEY_METADATA_STATUS_INITIALIZED; } } static void set_is_version_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_VERSION_INDEX] |= ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] |= ATKEY_METADATA_VERSION_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_VERSION_INDEX] &= ~ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] &= ~ATKEY_METADATA_VERSION_INITIALIZED; } } static void set_is_expiresat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] |= ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] |= ATKEY_METADATA_EXPIRESAT_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] &= ~ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] &= ~ATKEY_METADATA_EXPIRESAT_INITIALIZED; } } static void set_is_availableat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] |= ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] |= ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] &= ~ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] &= ~ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } } static void set_is_refreshat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] |= ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] |= ATKEY_METADATA_REFRESHAT_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] &= ~ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] &= ~ATKEY_METADATA_REFRESHAT_INITIALIZED; } } static void set_is_createdat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] |= ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] |= ATKEY_METADATA_CREATEDAT_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] &= ~ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] &= ~ATKEY_METADATA_CREATEDAT_INITIALIZED; } } static void set_is_updatedat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] |= ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] |= ATKEY_METADATA_UPDATEDAT_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] &= ~ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] &= ~ATKEY_METADATA_UPDATEDAT_INITIALIZED; } } static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] |= ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] |= ATKEY_METADATA_ISPUBLIC_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] &= ~ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] &= ~ATKEY_METADATA_ISPUBLIC_INITIALIZED; } } static void set_is_ishidden_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] |= ATKEY_METADATA_ISHIDDEN_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] |= ATKEY_METADATA_ISHIDDEN_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] &= ~ATKEY_METADATA_ISHIDDEN_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] &= ~ATKEY_METADATA_ISHIDDEN_INITIALIZED; } } static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ISCACHED_INDEX] |= ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] |= ATKEY_METADATA_ISCACHED_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ISCACHED_INDEX] &= ~ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] &= ~ATKEY_METADATA_ISCACHED_INITIALIZED; } } static void set_is_ttl_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_TTL_INDEX] |= ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] |= ATKEY_METADATA_TTL_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_TTL_INDEX] &= ~ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] &= ~ATKEY_METADATA_TTL_INITIALIZED; } } static void set_is_ttb_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_TTB_INDEX] |= ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] |= ATKEY_METADATA_TTB_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_TTB_INDEX] &= ~ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] &= ~ATKEY_METADATA_TTB_INITIALIZED; } } static void set_is_ttr_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_TTR_INDEX] |= ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] |= ATKEY_METADATA_TTR_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_TTR_INDEX] &= ~ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] &= ~ATKEY_METADATA_TTR_INITIALIZED; } } static void set_is_ccd_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_CCD_INDEX] |= ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] |= ATKEY_METADATA_CCD_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_CCD_INDEX] &= ~ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] &= ~ATKEY_METADATA_CCD_INITIALIZED; } } static void set_is_isbinary_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ISBINARY_INDEX] |= ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] |= ATKEY_METADATA_ISBINARY_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ISBINARY_INDEX] &= ~ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] &= ~ATKEY_METADATA_ISBINARY_INITIALIZED; } } static void set_is_isencrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] |= ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] |= ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] &= ~ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] &= ~ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } } static void set_is_datasignature_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] |= ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] |= ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] &= ~ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] &= ~ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } } static void set_is_sharedkeystatus_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= ~ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= ~ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } } static void set_is_sharedkeyenc_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] |= ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] |= ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] &= ~ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] &= ~ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } } static void set_is_pubkeyhash_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] |= ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] |= ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] &= ~ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] &= ~ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } } static void set_is_pubkeyalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] |= ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] |= ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] &= ~ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] &= ~ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } } static void set_is_encoding_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ENCODING_INDEX] |= ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] |= ATKEY_METADATA_ENCODING_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ENCODING_INDEX] &= ~ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] &= ~ATKEY_METADATA_ENCODING_INITIALIZED; } } static void set_is_enckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] |= ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] |= ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] &= ~ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] &= ~ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } } static void set_is_encalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_ENCALGO_INDEX] |= ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] |= ATKEY_METADATA_ENCALGO_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_ENCALGO_INDEX] &= ~ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] &= ~ATKEY_METADATA_ENCALGO_INITIALIZED; } } static void set_is_ivnonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_IVNONCE_INDEX] |= ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] |= ATKEY_METADATA_IVNONCE_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_IVNONCE_INDEX] &= ~ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] &= ~ATKEY_METADATA_IVNONCE_INITIALIZED; } } static void set_is_skeenckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= ~ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= ~ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } } static void set_is_skeencalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] |= ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] |= ATKEY_METADATA_SKEENCALGO_INITIALIZED; } else { - metadata->initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] &= ~ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] &= ~ATKEY_METADATA_SKEENCALGO_INITIALIZED; } } static void unset_createdby(atclient_atkey_metadata *metadata) { - atclient_atsign_free(&metadata->createdby); + if (is_createdby_initialized(metadata)) { + free(metadata->createdby); + } + metadata->createdby = NULL; set_is_createdby_initialized(metadata, false); } static void unset_updatedby(atclient_atkey_metadata *metadata) { - atclient_atsign_free(&metadata->updatedby); + if (is_updatedby_initialized(metadata)) { + free(metadata->updatedby); + } + metadata->updatedby = NULL; set_is_updatedby_initialized(metadata, false); } static void unset_status(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->status); + if (is_status_initialized(metadata)) { + free(metadata->status); + } + metadata->status = NULL; set_is_status_initialized(metadata, false); } @@ -1705,27 +1688,42 @@ static void unset_version(atclient_atkey_metadata *metadata) { } static void unset_expiresat(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->expiresat); + if (is_expiresat_initialized(metadata)) { + free(metadata->expiresat); + } + metadata->expiresat = NULL; set_is_expiresat_initialized(metadata, false); } static void unset_availableat(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->availableat); + if (is_availableat_initialized(metadata)) { + free(metadata->availableat); + } + metadata->availableat = NULL; set_is_availableat_initialized(metadata, false); } static void unset_refreshat(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->refreshat); + if (is_refreshat_initialized(metadata)) { + free(metadata->refreshat); + } + metadata->refreshat = NULL; set_is_refreshat_initialized(metadata, false); } static void unset_createdat(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->createdat); + if (is_createdat_initialized(metadata)) { + free(metadata->createdat); + } + metadata->createdat = NULL; set_is_createdat_initialized(metadata, false); } static void unset_updatedat(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->updatedat); + if (is_updatedat_initialized(metadata)) { + free(metadata->updatedat); + } + metadata->updatedat = NULL; set_is_updatedat_initialized(metadata, false); } @@ -1775,95 +1773,137 @@ static void unset_isencrypted(atclient_atkey_metadata *metadata) { } static void unset_datasignature(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->datasignature); + if (is_datasignature_initialized(metadata)) { + free(metadata->datasignature); + } + metadata->datasignature = NULL; set_is_datasignature_initialized(metadata, false); } static void unset_sharedkeystatus(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->sharedkeystatus); + if (is_sharedkeystatus_initialized(metadata)) { + free(metadata->sharedkeystatus); + } + metadata->sharedkeystatus = NULL; set_is_sharedkeystatus_initialized(metadata, false); } static void unset_sharedkeyenc(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->sharedkeyenc); + if (is_sharedkeyenc_initialized(metadata)) { + free(metadata->sharedkeyenc); + } + metadata->sharedkeyenc = NULL; set_is_sharedkeyenc_initialized(metadata, false); } static void unset_pubkeyhash(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->pubkeyhash); + if (is_pubkeyhash_initialized(metadata)) { + free(metadata->pubkeyhash); + } + metadata->pubkeyhash = NULL; set_is_pubkeyhash_initialized(metadata, false); } static void unset_pubkeyalgo(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->pubkeyalgo); + if (is_pubkeyalgo_initialized(metadata)) { + free(metadata->pubkeyalgo); + } + metadata->pubkeyalgo = NULL; set_is_pubkeyalgo_initialized(metadata, false); } static void unset_encoding(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->encoding); + if (is_encoding_initialized(metadata)) { + free(metadata->encoding); + } + metadata->encoding = NULL; set_is_encoding_initialized(metadata, false); } static void unset_enckeyname(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->enckeyname); + if (is_enckeyname_initialized(metadata)) { + free(metadata->enckeyname); + } set_is_enckeyname_initialized(metadata, false); } static void unset_encalgo(atclient_atkey_metadata *metadata) { - if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - atclient_atstr_free(&metadata->encalgo); + if (is_encalgo_initialized(metadata)) { + free(metadata->encalgo); } + metadata->encalgo = NULL; set_is_encalgo_initialized(metadata, false); } static void unset_ivnonce(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->ivnonce); + if (is_ivnonce_initialized(metadata)) { + free(metadata->ivnonce); + } + metadata->ivnonce = NULL; set_is_ivnonce_initialized(metadata, false); } static void unset_skeenckeyname(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->skeenckeyname); + if (is_skeenckeyname_initialized(metadata)) { + free(metadata->skeenckeyname); + } + metadata->skeenckeyname = NULL; set_is_skeenckeyname_initialized(metadata, false); } static void unset_skeencalgo(atclient_atkey_metadata *metadata) { - atclient_atstr_free(&metadata->skeencalgo); + if (is_skeencalgo_initialized(metadata)) { + free(metadata->skeencalgo); + } + metadata->skeencalgo = NULL; set_is_skeencalgo_initialized(metadata, false); } -static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby, const size_t createdbylen) { +static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby) { int ret = 1; - if ((ret = atclient_atsign_init(&metadata->createdby, createdby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with string \"%.*s\"\n", createdbylen, - createdby); + const size_t createdbylen = strlen(createdby); + const size_t createdbysize = createdbylen + 1; + if ((metadata->createdby = malloc(sizeof(char) * (createdbysize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby malloc failed\n"); goto exit; } + memcpy(metadata->createdby, createdby, createdbylen); + metadata->createdby[createdbylen] = '\0'; set_is_createdby_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby, const size_t updatedbylen) { +static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby) { int ret = 1; - if ((ret = atclient_atsign_init(&metadata->updatedby, updatedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with string \"%.*s\"\n", updatedbylen, - updatedby); + const size_t updatedbylen = strlen(updatedby); + const size_t updatedbysize = updatedbylen + 1; + if ((metadata->updatedby = malloc(sizeof(char) * (updatedbysize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby malloc failed\n"); goto exit; } + memcpy(metadata->updatedby, updatedby, updatedbylen); + metadata->updatedby[updatedbylen] = '\0'; set_is_updatedby_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_status(atclient_atkey_metadata *metadata, const char *status, const size_t statuslen) { +static int set_status(atclient_atkey_metadata *metadata, const char *status) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->status, statuslen + 1, status)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - statuslen, status); + const size_t statuslen = strlen(status); + const size_t statussize = statuslen + 1; + if ((metadata->status = malloc(sizeof(char) * (statussize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status malloc failed\n"); goto exit; } + memcpy(metadata->status, status, statuslen); + metadata->status[statuslen] = '\0'; set_is_status_initialized(metadata, true); ret = 0; goto exit; @@ -1875,65 +1915,85 @@ static void set_version(atclient_atkey_metadata *metadata, int version) { set_is_version_initialized(metadata, true); } -static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat, const size_t expiresatlen) { +static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->expiresat, expiresatlen + 1, expiresat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - expiresatlen, expiresat); + const size_t expiresatlen = strlen(expiresat); + const size_t expiresatsize = expiresatlen + 1; + if ((metadata->expiresat = malloc(sizeof(char) * (expiresatsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat malloc failed\n"); goto exit; } + memcpy(metadata->expiresat, expiresat, expiresatlen); + metadata->expiresat[expiresatlen] = '\0'; set_is_expiresat_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat, const size_t availableatlen) { +static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->availableat, availableatlen + 1, availableat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - availableatlen, availableat); + const size_t availableatlen = strlen(availableat); + const size_t availableatsize = availableatlen + 1; + if ((metadata->availableat = malloc(sizeof(char) * (availableatsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat malloc failed\n"); goto exit; } + memcpy(metadata->availableat, availableat, availableatlen); + metadata->availableat[availableatlen] = '\0'; set_is_availableat_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat, const size_t refreshatlen) { +static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->refreshat, refreshatlen + 1, refreshat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - refreshatlen, refreshat); + const size_t refreshatlen = strlen(refreshat); + const size_t refreshatsize = refreshatlen + 1; + if ((metadata->refreshat = malloc(sizeof(char) * (refreshatsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat malloc failed\n"); goto exit; } + memcpy(metadata->refreshat, refreshat, refreshatlen); + metadata->refreshat[refreshatlen] = '\0'; set_is_refreshat_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat, const size_t createdatlen) { +static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->createdat, createdatlen + 1, createdat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - createdatlen, createdat); + const size_t createdatlen = strlen(createdat); + const size_t createdatsize = createdatlen + 1; + if ((metadata->createdat = malloc(sizeof(char) * (createdatlen + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat malloc failed\n"); goto exit; } + memcpy(metadata->createdat, createdat, createdatlen); + metadata->createdat[createdatlen] = '\0'; set_is_createdat_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat, const size_t updatedatlen) { +static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->updatedat, updatedatlen + 1, updatedat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - updatedatlen, updatedat); + const size_t updatedatlen = strlen(updatedat); + const size_t updatedatsize = updatedatlen + 1; + if ((metadata->updatedat = malloc(sizeof(char) * (updatedatsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat malloc failed\n"); goto exit; } + memcpy(metadata->updatedat, updatedat, updatedatlen); + metadata->updatedat[updatedatlen] = '\0'; set_is_updatedat_initialized(metadata, true); ret = 0; goto exit; @@ -1985,146 +2045,187 @@ static void set_isencrypted(atclient_atkey_metadata *metadata, const bool isencr set_is_isencrypted_initialized(metadata, true); } -static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature, - const size_t datasignaturelen) { +static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->datasignature, datasignaturelen + 1, datasignature)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - datasignaturelen, datasignature); + const size_t datasignaturelen = strlen(datasignature); + const size_t datasignaturesize = datasignaturelen + 1; + if ((metadata->datasignature = malloc(sizeof(char) * (datasignaturesize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature malloc failed\n"); goto exit; } + memcpy(metadata->datasignature, datasignature, datasignaturelen); + metadata->datasignature[datasignaturelen] = '\0'; set_is_datasignature_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus, - const size_t sharedkeystatuslen) { +static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->sharedkeystatus, sharedkeystatuslen + 1, sharedkeystatus)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - sharedkeystatuslen, sharedkeystatus); + const size_t sharedkeystatuslen = strlen(sharedkeystatus); + const size_t sharedkeystatussize = sharedkeystatuslen + 1; + if ((metadata->sharedkeystatus = malloc(sizeof(char) * (sharedkeystatussize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus malloc failed\n"); goto exit; } + memcpy(metadata->sharedkeystatus, sharedkeystatus, sharedkeystatuslen); + metadata->sharedkeystatus[sharedkeystatuslen] = '\0'; set_is_sharedkeystatus_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc, const size_t sharedkeyenclen) { +static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->sharedkeyenc, sharedkeyenclen + 1, sharedkeyenc)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - sharedkeyenclen + 1, sharedkeyenc); + const size_t sharedkeyenclen = strlen(sharedkeyenc); + const size_t sharedkeyencsize = sharedkeyenclen + 1; + if ((metadata->sharedkeyenc = malloc(sizeof(char) * (sharedkeyencsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc malloc failed\n"); goto exit; } + memcpy(metadata->sharedkeyenc, sharedkeyenc, sharedkeyenclen); + metadata->sharedkeyenc[sharedkeyenclen] = '\0'; set_is_sharedkeyenc_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash, const size_t pubkeyhashlen) { +static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->pubkeyhash, pubkeyhashlen + 1, pubkeyhash)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - pubkeyhashlen, pubkeyhash); + const size_t pubkeyhashlen = strlen(pubkeyhash); + const size_t pubkeyhashsize = pubkeyhashlen + 1; + if ((metadata->pubkeyhash = malloc(sizeof(char) * (pubkeyhashsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash malloc failed\n"); goto exit; } + memcpy(metadata->pubkeyhash, pubkeyhash, pubkeyhashlen); + metadata->pubkeyhash[pubkeyhashlen] = '\0'; set_is_pubkeyhash_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo, const size_t pubkeyalgolen) { +static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->pubkeyalgo, pubkeyalgolen + 1, pubkeyalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - pubkeyalgolen, pubkeyalgo); + const size_t pubkeyalgolen = strlen(pubkeyalgo); + const size_t pubkeyalgosize = pubkeyalgolen + 1; + if ((metadata->pubkeyalgo = malloc(sizeof(char) * (pubkeyalgosize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo malloc failed\n"); goto exit; } + memcpy(metadata->pubkeyalgo, pubkeyalgo, pubkeyalgolen); + metadata->pubkeyalgo[pubkeyalgolen] = '\0'; set_is_pubkeyalgo_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding, const size_t encodinglen) { +static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->encoding, encodinglen + 1, encoding)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - encodinglen, encoding); + const size_t encodinglen = strlen(encoding); + const size_t encodingsize = encodinglen + 1; + if ((metadata->encoding = malloc(sizeof(char) * (encodingsize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding malloc failed\n"); goto exit; } + memcpy(metadata->encoding, encoding, encodinglen); + metadata->encoding[encodinglen] = '\0'; set_is_encoding_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname, const size_t enckeynamelen) { +static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->enckeyname, enckeynamelen + 1, enckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - enckeynamelen, enckeyname); + const size_t enckeynamelen = strlen(enckeyname); + const size_t enckeynamesize = enckeynamelen + 1; + if ((metadata->enckeyname = malloc(sizeof(char) * (enckeynamesize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname malloc failed\n"); goto exit; } + memcpy(metadata->enckeyname, enckeyname, enckeynamelen); + metadata->enckeyname[enckeynamelen] = '\0'; set_is_enckeyname_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo, const size_t encalgolen) { +static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->encalgo, encalgolen + 1, encalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - encalgolen, encalgo); + const size_t encalgolen = strlen(encalgo); + const size_t encalgosize = encalgolen + 1; + if ((metadata->encalgo = malloc(sizeof(char) * (encalgosize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo malloc failed\n"); goto exit; } + memcpy(metadata->encalgo, encalgo, encalgolen); + metadata->encalgo[encalgolen] = '\0'; set_is_encalgo_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce, const size_t ivnoncelen) { +static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->ivnonce, ivnoncelen + 1, ivnonce)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - ivnoncelen, ivnonce); + const size_t ivnoncelen = strlen(ivnonce); + const size_t ivnoncesize = ivnoncelen + 1; + if ((metadata->ivnonce = malloc(sizeof(char) * (ivnoncesize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce malloc failed\n"); goto exit; } + memcpy(metadata->ivnonce, ivnonce, ivnoncelen); + metadata->ivnonce[ivnoncelen] = '\0'; set_is_ivnonce_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname, - const size_t skeenckeynamelen) { +static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->skeenckeyname, skeenckeynamelen + 1, skeenckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - skeenckeynamelen, skeenckeyname); + const size_t skeenckeynamelen = strlen(skeenckeyname); + const size_t skeenckeynamesize = skeenckeynamelen + 1; + if ((metadata->skeenckeyname = malloc(sizeof(char) * (skeenckeynamesize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname malloc failed\n"); goto exit; } + memcpy(metadata->skeenckeyname, skeenckeyname, skeenckeynamelen); + metadata->skeenckeyname[skeenckeynamelen] = '\0'; set_is_skeenckeyname_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo, const size_t skeencalgolen) { +static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo) { int ret = 1; - if ((ret = atclient_atstr_init_literal(&metadata->skeencalgo, skeencalgolen + 1, skeencalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_init_literal failed with string \"%.*s\"\n", - skeencalgolen, skeencalgo); + const size_t skeencalgolen = strlen(skeencalgo); + const size_t skeencalgosize = skeencalgolen + 1; + if ((metadata->skeencalgo = malloc(sizeof(char) * (skeencalgosize))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo malloc failed\n"); goto exit; } + memcpy(metadata->skeencalgo, skeencalgo, skeencalgolen); + metadata->skeencalgo[skeencalgolen] = '\0'; set_is_skeencalgo_initialized(metadata, true); ret = 0; goto exit; diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index eb0d8302..aeae1f39 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -40,87 +40,163 @@ static int test_atkey_metadata_from_jsonstr() { atclient_atkey_metadata metadata; atclient_atkey_metadata_init(&metadata); - ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR, - strlen(TEST_ATKEY_METADATA_FROM_JSONSTR)); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR, + strlen(TEST_ATKEY_METADATA_FROM_JSONSTR))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed"); goto exit; } - if (strncmp(metadata.createdby.atsign, "@qt_thermostat", strlen("@qt_thermostat")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdby.str != @qt_thermostat: %s", - metadata.createdby.atsign); + if (!atclient_atkey_metadata_is_createdby_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdby_initialized failed"); + goto exit; + } + + if (strcmp(metadata.createdby, "@qt_thermostat") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdby != @qt_thermostat: %s", metadata.createdby); goto exit; } - if (strncmp(metadata.updatedby.atsign, "@qt_thermostat", strlen("@qt_thermostat")) != 0) { + if (!atclient_atkey_metadata_is_updatedby_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedby_initialized failed"); + goto exit; + } + + if (strcmp(metadata.updatedby, "@qt_thermostat") != 0) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedby.atsign != @qt_thermostat: %s", - metadata.updatedby.atsign); + metadata.updatedby); + goto exit; + } + + if (!atclient_atkey_metadata_is_createdat_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdat_initialized failed"); + goto exit; + } + + if (strlen(metadata.createdat) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.createdat) <= 0: %lu", strlen(metadata.createdat)); + ret = 1; + goto exit; + } + + if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); + goto exit; + } + + if (strcmp(metadata.createdat, "2024-02-17 19:54:12.037Z") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat != 2024-02-17 19:54:12.037Z: %s", + metadata.createdat); + ret = 1; + goto exit; + } + + if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); + goto exit; + } + + if (strlen(metadata.updatedat) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.updatedat) <= 0: %lu", strlen(metadata.updatedat)); + ret = 1; + goto exit; + } + + if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); + goto exit; + } + + if (strcmp(metadata.updatedat, "2024-02-17 19:54:12.037Z") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat != 2024-02-17 19:54:12.037Z: %s", + metadata.updatedat); ret = 1; goto exit; } - if (metadata.createdat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat.len <= 0: %lu", metadata.createdat.len); + if (!atclient_atkey_metadata_is_expiresat_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_expiresat_initialized failed"); goto exit; } - if (strncmp(metadata.createdat.str, "2024-02-17 19:54:12.037Z", strlen("2024-02-17 19:54:12.037Z")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat.str != 2024-02-17 19:54:12.037Z: %s", - metadata.createdat.str); + if (strlen(metadata.expiresat) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.expiresat) <= 0: %lu", strlen(metadata.expiresat)); ret = 1; goto exit; } - if (metadata.updatedat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat.len <= 0: %lu", metadata.updatedat.len); + if (!atclient_atkey_metadata_is_status_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_status_initialized failed"); goto exit; } - if (strncmp(metadata.updatedat.str, "2024-02-17 19:54:12.037Z", strlen("2024-02-17 19:54:12.037Z")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat.str != 2024-02-17 19:54:12.037Z: %s", - metadata.updatedat.str); + if (strcmp(metadata.expiresat, "2024-02-17 19:55:38.437Z") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat != 2024-02-17 19:55:38.437Z: %s", + metadata.expiresat); goto exit; } - if (metadata.expiresat.len <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat.len <= 0: %lu", metadata.expiresat.len); + if (!atclient_atkey_metadata_is_status_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_status_initialized failed"); goto exit; } - if (strncmp(metadata.expiresat.str, "2024-02-17 19:55:38.437Z", strlen("2024-02-17 19:55:38.437Z")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat.str != 2024-02-17 19:55:38.437Z: %s", - metadata.expiresat.str); + if (strlen(metadata.status) != strlen("active")) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status != strlen(active): %lu", strlen(metadata.status)); goto exit; } - if (metadata.status.len != strlen("active")) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status.len != strlen(active): %lu", metadata.status.len); + if (!atclient_atkey_metadata_is_version_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_version_initialized failed"); goto exit; } - if (strncmp(metadata.status.str, "active", strlen("active")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status.str != active: %s", metadata.status.str); + if (strcmp(metadata.status, "active") != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status != active: %s", metadata.status); + goto exit; + } + + if (!atclient_atkey_metadata_is_version_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_version_initialized failed"); goto exit; } if (metadata.version != 0) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.version != 0: %d", metadata.version); + goto exit; + } + + if (!atclient_atkey_metadata_is_ttl_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_ttl_initialized failed"); goto exit; } if (metadata.ttl != 86400) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.ttl != 86400: %ld", metadata.ttl); + goto exit; + } + + if (!atclient_atkey_metadata_is_isbinary_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isbinary_initialized failed"); goto exit; } @@ -130,94 +206,186 @@ static int test_atkey_metadata_from_jsonstr() { goto exit; } + if (!atclient_atkey_metadata_is_isencrypted_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isencrypted_initialized failed"); + goto exit; + } + if (metadata.isencrypted != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isencrypted != false: %d", metadata.isencrypted); ret = 1; goto exit; } + if (!atclient_atkey_metadata_is_iscached_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_iscached_initialized failed"); + goto exit; + } + if (metadata.iscached != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.iscached != false: %d", metadata.iscached); ret = 1; goto exit; } - if (metadata.availableat.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.availableat.len != 0: %lu", metadata.availableat.len); + if (!atclient_atkey_metadata_is_availableat_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isavailableat_initialized failed"); + goto exit; + } + + if (strlen(metadata.availableat) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.availableat) != 0: %lu", + strlen(metadata.availableat)); + ret = 1; + goto exit; + } + + if (strlen(metadata.refreshat) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.refreshat) != 0: %lu", strlen(metadata.refreshat)); + ret = 1; + goto exit; + } + + if (atclient_atkey_metadata_is_datasignature_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "datasignature is initialized when it should not be"); + goto exit; + } + + if (strlen(metadata.datasignature) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.datasignature != 0: %lu", + strlen(metadata.datasignature)); + ret = 1; + goto exit; + } + + if (atclient_atkey_metadata_is_sharedkeystatus_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is initialized when it should not be"); + goto exit; + } + + if (strlen(metadata.sharedkeystatus) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.sharedkeystatus) != 0: %lu", + strlen(metadata.sharedkeystatus)); + ret = 1; + goto exit; + } + + if (atclient_atkey_metadata_is_sharedkeyenc_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedkeyenc is initialized when it should not be"); + goto exit; + } + + if (strlen(metadata.sharedkeyenc) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.sharedkeyenc) != 0: %lu", + strlen(metadata.sharedkeyenc)); + ret = 1; + goto exit; + } + + if (atclient_atkey_metadata_is_pubkeyhash_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyhash is initialized when it should not be"); + goto exit; + } + + if (strlen(metadata.pubkeyhash) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.pubkeyhash) != 0: %lu", + strlen(metadata.pubkeyhash)); + ret = 1; + goto exit; + } + + if (atclient_atkey_metadata_is_pubkeyalgo_initialized(&metadata)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyalgo is initialized when it should not be"); + goto exit; + } + + if (strlen(metadata.pubkeyalgo) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.pubkeyalgo) != 0: %lu", + strlen(metadata.pubkeyalgo)); ret = 1; goto exit; } - if (metadata.refreshat.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.refreshat.len != 0: %lu", metadata.refreshat.len); + if (atclient_atkey_metadata_is_encoding_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encoding is initialized when it should not be"); goto exit; } - if (metadata.datasignature.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.datasignature.len != 0: %lu", metadata.datasignature.len); + if (strlen(metadata.encoding) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.encoding) != 0: %lu", strlen(metadata.encoding)); ret = 1; goto exit; } - if (metadata.sharedkeystatus.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.sharedkeystatus.len != 0: %lu", - metadata.sharedkeystatus.len); + if (atclient_atkey_metadata_is_enckeyname_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enckeyname is initialized when it should not be"); goto exit; } - if (metadata.sharedkeyenc.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.sharedkeyenc.len != 0: %lu", metadata.sharedkeyenc.len); + if (strlen(metadata.enckeyname) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.enckeyname) != 0: %lu", + strlen(metadata.enckeyname)); ret = 1; goto exit; } - if (metadata.pubkeyhash.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyhash.len != 0: %lu", metadata.pubkeyhash.len); + if (atclient_atkey_metadata_is_encalgo_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encalgo is initialized when it should not be"); goto exit; } - if (metadata.pubkeyalgo.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.pubkeyalgo.len != 0: %lu", metadata.pubkeyalgo.len); + if (strlen(metadata.encalgo) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.encalgo) != 0: %lu", strlen(metadata.encalgo)); ret = 1; goto exit; } - if (metadata.encoding.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.encoding.len != 0: %lu", metadata.encoding.len); + if (atclient_atkey_metadata_is_ivnonce_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivnonce is initialized when it should not be"); goto exit; } - if (metadata.enckeyname.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.enckeyname.len != 0: %lu", metadata.enckeyname.len); + if (strlen(metadata.ivnonce) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.ivnonce) != 0: %lu", strlen(metadata.ivnonce)); ret = 1; goto exit; } - if (metadata.encalgo.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.encalgo.len != 0: %lu", metadata.encalgo.len); + if (atclient_atkey_metadata_is_skeenckeyname_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeenckeyname is initialized when it should not be"); goto exit; } - if (metadata.ivnonce.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.ivnonce.len != 0: %lu", metadata.ivnonce.len); + if (strlen(metadata.skeenckeyname) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.skeenckeyname) != 0: %lu", + strlen(metadata.skeenckeyname)); ret = 1; goto exit; } - if (metadata.skeenckeyname.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeenckeyname.len != 0: %lu", metadata.skeenckeyname.len); + if (atclient_atkey_metadata_is_skeencalgo_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeencalgo is initialized when it should not be"); goto exit; } - if (metadata.skeencalgo.len != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.skeencalgo.len != 0: %lu", metadata.skeencalgo.len); + if (strlen(metadata.skeencalgo) != 0) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.skeencalgo) != 0: %lu", + strlen(metadata.skeencalgo)); goto exit; } From 61b332d1bd7afbbb08672011b6204fe1ecdc9ab9 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 11:46:21 -0400 Subject: [PATCH 075/193] feat: more atclient_atkey_metadata refactors --- packages/atclient/include/atclient/metadata.h | 14 +- .../atclient/src/atclient_get_publickey.c | 2 +- packages/atclient/src/atclient_get_selfkey.c | 2 +- .../atclient/src/atclient_get_sharedkey.c | 4 +- packages/atclient/src/atclient_put.c | 2 +- packages/atclient/src/metadata.c | 35 ++- packages/atclient/src/notify.c | 2 +- packages/atclient/tests/test_atkey_metadata.c | 232 ++++-------------- 8 files changed, 81 insertions(+), 212 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 30e51370..9e72c1e2 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -277,8 +277,7 @@ void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata); * strlen(metadatastr) * @return int 0 on success */ -int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr, - const size_t metadatastrsize); +int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr); /** * @brief Populates the metadata struct from a cJSON pointer. This function is good for debugging. * @@ -290,14 +289,13 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, * @brief Reads metadata struct and converts it to a json formatted string. This function should mostly be used for * debugging only. See atclient_atkey_metadata_to_protocolstr for a more useful function when working with atProtocol * - * @param metadata the metadata struct to convert to a string - * @param metadatastr the buffer to write the metadata to - * @param metadatastrsize the allocated length of the metadatastr buffer - * @param metadatastrlen the length of the metadata string written to metadatastr once the operation is complete + * @param metadata the metadata struct to convert to a JSON string, typically used for debugging by printing + * @param metadatastr the pointer that will be allocated and written to. This function will allocate the memory for you + * and give you an address to it. If this function returns a zero (which means success), then it is the caller's + * responsibility to free the pointer. * @return int 0 on success */ -int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char *metadatastr, - const size_t metadatastrsize, size_t *metadatastrlen); +int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char **metadatastr); size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *metadata); /** diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index 72e3c016..b15c297a 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -121,7 +121,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu metadatastr = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr))) != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index cb7f126a..20c89fe9 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -109,7 +109,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, char *metadatastr = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr))) != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index af201253..f81c03ee 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -256,7 +256,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke char *metadatastr = cJSON_Print(metadata); - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr)); + ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; @@ -442,7 +442,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *metadatastr = cJSON_Print(metadata); - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr, strlen(metadatastr)); + ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index f75ec93b..afc49fc6 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -152,7 +152,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c goto error_cleanup; } - if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64, ivbase64len)) != 0) { + if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); goto error_cleanup; } diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 1524f34c..945846cf 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -133,8 +133,7 @@ void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata) { memset(metadata, 0, sizeof(atclient_atkey_metadata)); } -int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr, - const size_t metadatastrlen) { +int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr) { int ret = 1; cJSON *root = cJSON_Parse(metadatastr); @@ -405,10 +404,22 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, } } -int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char *metadatastr, - const size_t metadatastrsize, size_t *metadatastrlen) { +int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char **metadatastr) { int ret = 1; + if(metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; + } + + if(metadatastr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr is NULL\n"); + return ret; + } + + char *jsonstr = NULL; cJSON *root = cJSON_CreateObject(); if (atclient_atkey_metadata_is_createdby_initialized(metadata)) { @@ -527,26 +538,26 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, cJSON_AddStringToObject(root, "skeEncAlgo", metadata->skeencalgo); } - char *jsonstr = cJSON_Print(root); + jsonstr = cJSON_Print(root); if (jsonstr == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print failed\n"); goto exit; } - if (strlen(jsonstr) > metadatastrsize) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr buffer too small: %lu > %lu\n", strlen(jsonstr), - metadatastrsize); - free(jsonstr); + const size_t metadatastrsize = strlen(jsonstr) + 1; + *metadatastr = (char *)malloc(sizeof(char) * metadatastrsize); + if (*metadatastr == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } + memcpy(*metadatastr, jsonstr, strlen(jsonstr)); + (*metadatastr)[strlen(jsonstr)] = '\0'; - strcpy(metadatastr, jsonstr); - *metadatastrlen = strlen(jsonstr); - free(jsonstr); ret = 0; goto exit; exit: { + free(jsonstr); cJSON_Delete(root); return ret; } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index ca41088e..87f614c6 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -144,7 +144,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific return ret; } - ret = atclient_atkey_metadata_set_ivnonce(&(params->atkey->metadata), (char *)ivbase64, ivbase64len); + ret = atclient_atkey_metadata_set_ivnonce(&(params->atkey->metadata), (char *)ivbase64); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce failed with code %d\n", ret); return ret; diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index aeae1f39..57471aad 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -2,6 +2,7 @@ #include "atlogger/atlogger.h" #include #include +#include // example: // "metaData":{ @@ -40,15 +41,14 @@ static int test_atkey_metadata_from_jsonstr() { atclient_atkey_metadata metadata; atclient_atkey_metadata_init(&metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR, - strlen(TEST_ATKEY_METADATA_FROM_JSONSTR))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed"); + if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed\n"); goto exit; } if (!atclient_atkey_metadata_is_createdby_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdby_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdby_initialized failed\n"); goto exit; } @@ -60,37 +60,25 @@ static int test_atkey_metadata_from_jsonstr() { if (!atclient_atkey_metadata_is_updatedby_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedby_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedby_initialized failed\n"); goto exit; } if (strcmp(metadata.updatedby, "@qt_thermostat") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedby.atsign != @qt_thermostat: %s", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedby.atsign != @qt_thermostat: %s\n", metadata.updatedby); goto exit; } if (!atclient_atkey_metadata_is_createdat_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdat_initialized failed"); - goto exit; - } - - if (strlen(metadata.createdat) <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.createdat) <= 0: %lu", strlen(metadata.createdat)); - ret = 1; - goto exit; - } - - if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdat_initialized failed\n"); goto exit; } if (strcmp(metadata.createdat, "2024-02-17 19:54:12.037Z") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat != 2024-02-17 19:54:12.037Z: %s", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat != 2024-02-17 19:54:12.037Z: %s\n", metadata.createdat); ret = 1; goto exit; @@ -98,24 +86,12 @@ static int test_atkey_metadata_from_jsonstr() { if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); - goto exit; - } - - if (strlen(metadata.updatedat) <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.updatedat) <= 0: %lu", strlen(metadata.updatedat)); - ret = 1; - goto exit; - } - - if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed\n"); goto exit; } if (strcmp(metadata.updatedat, "2024-02-17 19:54:12.037Z") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat != 2024-02-17 19:54:12.037Z: %s", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat != 2024-02-17 19:54:12.037Z: %s\n", metadata.updatedat); ret = 1; goto exit; @@ -123,269 +99,159 @@ static int test_atkey_metadata_from_jsonstr() { if (!atclient_atkey_metadata_is_expiresat_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_expiresat_initialized failed"); - goto exit; - } - - if (strlen(metadata.expiresat) <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.expiresat) <= 0: %lu", strlen(metadata.expiresat)); - ret = 1; - goto exit; - } - - if (!atclient_atkey_metadata_is_status_initialized(&metadata)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_status_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_expiresat_initialized failed\n"); goto exit; } if (strcmp(metadata.expiresat, "2024-02-17 19:55:38.437Z") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat != 2024-02-17 19:55:38.437Z: %s", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat != 2024-02-17 19:55:38.437Z: %s\n", metadata.expiresat); goto exit; } if (!atclient_atkey_metadata_is_status_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_status_initialized failed"); - goto exit; - } - - if (strlen(metadata.status) != strlen("active")) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status != strlen(active): %lu", strlen(metadata.status)); - goto exit; - } - - if (!atclient_atkey_metadata_is_version_initialized(&metadata)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_version_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_status_initialized failed\n"); goto exit; } if (strcmp(metadata.status, "active") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status != active: %s", metadata.status); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.status != active: %s\n", metadata.status); goto exit; } if (!atclient_atkey_metadata_is_version_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_version_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_version_initialized failed\n"); goto exit; } if (metadata.version != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.version != 0: %d", metadata.version); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.version != 0: %d\n", metadata.version); goto exit; } if (!atclient_atkey_metadata_is_ttl_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_ttl_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_ttl_initialized failed\n"); goto exit; } if (metadata.ttl != 86400) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.ttl != 86400: %ld", metadata.ttl); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.ttl != 86400: %ld\n", metadata.ttl); goto exit; } if (!atclient_atkey_metadata_is_isbinary_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isbinary_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isbinary_initialized failed\n"); goto exit; } if (metadata.isbinary != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isbinary != false: %d", metadata.isbinary); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isbinary != false: %d\n", metadata.isbinary); ret = 1; goto exit; } if (!atclient_atkey_metadata_is_isencrypted_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isencrypted_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isencrypted_initialized failed\n"); goto exit; } if (metadata.isencrypted != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isencrypted != false: %d", metadata.isencrypted); - ret = 1; - goto exit; - } - - if (!atclient_atkey_metadata_is_iscached_initialized(&metadata)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_iscached_initialized failed"); - goto exit; - } - - if (metadata.iscached != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.iscached != false: %d", metadata.iscached); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isencrypted != false: %d\n", metadata.isencrypted); ret = 1; goto exit; } - if (!atclient_atkey_metadata_is_availableat_initialized(&metadata)) { + if (atclient_atkey_metadata_is_iscached_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isavailableat_initialized failed"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_atkey_metadata_iscached_initialized was initialized when it should not be\n"); goto exit; } - if (strlen(metadata.availableat) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.availableat) != 0: %lu", - strlen(metadata.availableat)); + if (atclient_atkey_metadata_is_availableat_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isavailableat_initialized is intiialized when it should not be\n"); goto exit; } - if (strlen(metadata.refreshat) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.refreshat) != 0: %lu", strlen(metadata.refreshat)); + if(atclient_atkey_metadata_is_refreshat_initialized(&metadata)) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isrefreshat_initialized is intiialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_datasignature_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "datasignature is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.datasignature) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.datasignature != 0: %lu", - strlen(metadata.datasignature)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "datasignature is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_sharedkeystatus_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.sharedkeystatus) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.sharedkeystatus) != 0: %lu", - strlen(metadata.sharedkeystatus)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_sharedkeyenc_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedkeyenc is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.sharedkeyenc) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.sharedkeyenc) != 0: %lu", - strlen(metadata.sharedkeyenc)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedkeyenc is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_pubkeyhash_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyhash is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.pubkeyhash) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.pubkeyhash) != 0: %lu", - strlen(metadata.pubkeyhash)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyhash is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_pubkeyalgo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyalgo is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.pubkeyalgo) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.pubkeyalgo) != 0: %lu", - strlen(metadata.pubkeyalgo)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyalgo is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_encoding_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encoding is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.encoding) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.encoding) != 0: %lu", strlen(metadata.encoding)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encoding is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_enckeyname_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enckeyname is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.enckeyname) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.enckeyname) != 0: %lu", - strlen(metadata.enckeyname)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enckeyname is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_encalgo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encalgo is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.encalgo) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.encalgo) != 0: %lu", strlen(metadata.encalgo)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encalgo is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_ivnonce_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivnonce is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.ivnonce) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.ivnonce) != 0: %lu", strlen(metadata.ivnonce)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivnonce is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_skeenckeyname_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeenckeyname is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.skeenckeyname) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.skeenckeyname) != 0: %lu", - strlen(metadata.skeenckeyname)); - ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeenckeyname is initialized when it should not be\n"); goto exit; } if (atclient_atkey_metadata_is_skeencalgo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeencalgo is initialized when it should not be"); - goto exit; - } - - if (strlen(metadata.skeencalgo) != 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strlen(metadata.skeencalgo) != 0: %lu", - strlen(metadata.skeencalgo)); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeencalgo is initialized when it should not be\n"); goto exit; } @@ -410,13 +276,12 @@ static int test_atkey_metadata_to_protocolstr() { atclient_atkey_metadata_set_isbinary(&metadata, true); atclient_atkey_metadata_set_isencrypted(&metadata, true); atclient_atkey_metadata_set_iscached(&metadata, true); - atclient_atkey_metadata_set_ivnonce(&metadata, "abcdefghijk", strlen("abcdefghijk")); + atclient_atkey_metadata_set_ivnonce(&metadata, "abcdefghijk"); char *protocolfragment = NULL; const size_t expected_protocolframent_len = atclient_atkey_metadata_protocol_strlen(&metadata); - ret = atclient_atkey_metadata_to_protocol_str(&metadata, &protocolfragment); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_to_protocol_str(&metadata, &protocolfragment)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr failed"); goto exit; } @@ -459,20 +324,14 @@ static int test_atkey_metadata_to_jsonstr() { atclient_atkey_metadata metadata; atclient_atkey_metadata_init(&metadata); - const size_t jsonstrsize = 4096; - char jsonstr[jsonstrsize]; - memset(jsonstr, 0, sizeof(char) * jsonstrsize); - size_t jsonstrlen = 0; + char *jsonstr = NULL; - ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR, - strlen(TEST_ATKEY_METADATA_FROM_JSONSTR)); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed"); goto exit; } - ret = atclient_atkey_metadata_to_jsonstr(&metadata, jsonstr, jsonstrsize, &jsonstrlen); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_to_jsonstr(&metadata, &jsonstr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_jsonstr failed"); goto exit; } @@ -481,6 +340,7 @@ static int test_atkey_metadata_to_jsonstr() { goto exit; exit: { atclient_atkey_metadata_free(&metadata); + free(jsonstr); return ret; } } From 86a919a69ca16918238379d0825dd1d668995585 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 12:00:20 -0400 Subject: [PATCH 076/193] chore: made atclient_atkey_metadata_from_jsonstr return `int` (originally `void`) because it is error prone --- packages/atclient/include/atclient/metadata.h | 5 +- packages/atclient/src/metadata.c | 230 ++++++++++++++---- 2 files changed, 189 insertions(+), 46 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 9e72c1e2..0b09cc7f 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -278,13 +278,16 @@ void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata); * @return int 0 on success */ int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr); + /** * @brief Populates the metadata struct from a cJSON pointer. This function is good for debugging. * * @param metadata the metadata struct to populate * @param json the json object to populate from + * @return int 0 on success */ -void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json); +int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json); + /** * @brief Reads metadata struct and converts it to a json formatted string. This function should mostly be used for * debugging only. See atclient_atkey_metadata_to_protocolstr for a more useful function when working with atProtocol diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 945846cf..3d2bbdd6 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -141,40 +141,76 @@ int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, cons atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n"); goto exit; } - atclient_atkey_metadata_from_cjson_node(metadata, root); - cJSON_Delete(root); + + if ((ret = atclient_atkey_metadata_from_cjson_node(metadata, root)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_cjson_node: %d\n", ret); + goto exit; + } ret = 0; goto exit; -exit: { return ret; } +exit: { + cJSON_Delete(root); + return ret; } +} + +int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json) { + int ret = 1; + + if(json == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "json is NULL\n"); + goto exit; + } + + if(metadata == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + goto exit; + } -void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json) { cJSON *createdby = cJSON_GetObjectItem(json, "createdBy"); if (createdby != NULL) { if (createdby->type != cJSON_NULL) { - set_createdby(metadata, createdby->valuestring); + if ((ret = set_createdby(metadata, createdby->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + goto exit; + } } else { - set_createdby(metadata, "null"); + if ((ret = set_createdby(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + goto exit; + } } } cJSON *updatedby = cJSON_GetObjectItem(json, "updatedBy"); if (updatedby != NULL) { if (updatedby->type != cJSON_NULL) { - set_updatedby(metadata, updatedby->valuestring); + if ((ret = set_updatedby(metadata, updatedby->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + goto exit; + } } else { - set_updatedby(metadata, "null"); + if ((ret = set_updatedby(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + goto exit; + } } } cJSON *status = cJSON_GetObjectItem(json, "status"); if (status != NULL) { if (status->type != cJSON_NULL) { - set_status(metadata, status->valuestring); + if ((ret = set_status(metadata, status->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); + goto exit; + } } else { - set_status(metadata, "null"); + if ((ret = set_status(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); + goto exit; + } } } @@ -190,45 +226,75 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, cJSON *expiresat = cJSON_GetObjectItem(json, "expiresAt"); if (expiresat != NULL) { if (expiresat->type != cJSON_NULL) { - set_expiresat(metadata, expiresat->valuestring); + if ((ret = set_expiresat(metadata, expiresat->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + goto exit; + } } else { - set_expiresat(metadata, "null"); + if ((ret = set_expiresat(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + goto exit; + } } } cJSON *availableat = cJSON_GetObjectItem(json, "availableAt"); if (availableat != NULL) { if (availableat->type != cJSON_NULL) { - set_availableat(metadata, availableat->valuestring); + if ((ret = set_availableat(metadata, availableat->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + goto exit; + } } else { - set_availableat(metadata, "null"); + if ((ret = set_availableat(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + goto exit; + } } } cJSON *refreshat = cJSON_GetObjectItem(json, "refreshAt"); if (refreshat != NULL) { if (refreshat->type != cJSON_NULL) { - set_refreshat(metadata, refreshat->valuestring); + if((ret = set_refreshat(metadata, refreshat->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + goto exit; + } } else { - set_refreshat(metadata, "null"); + if((ret = set_refreshat(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + goto exit; + } } } cJSON *createdat = cJSON_GetObjectItem(json, "createdAt"); if (createdat != NULL) { if (createdat->type != cJSON_NULL) { - set_createdat(metadata, createdat->valuestring); + if((ret = set_createdat(metadata, createdat->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + goto exit; + } } else { - set_createdat(metadata, "null"); + if((ret = set_createdat(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + goto exit; + } } } cJSON *updatedat = cJSON_GetObjectItem(json, "updatedAt"); if (updatedat != NULL) { if (updatedat->type != cJSON_NULL) { - set_updatedat(metadata, updatedat->valuestring); + if((ret = set_updatedat(metadata, updatedat->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + goto exit; + } } else { - set_updatedat(metadata, "null"); + if((ret = set_updatedat(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + goto exit; + } } } @@ -307,113 +373,183 @@ void atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, cJSON *datasignature = cJSON_GetObjectItem(json, "dataSignature"); if (datasignature != NULL) { if (datasignature->type != cJSON_NULL) { - set_datasignature(metadata, datasignature->valuestring); + if((ret = set_datasignature(metadata, datasignature->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + goto exit; + } } else { - set_datasignature(metadata, "null"); + if((ret = set_datasignature(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + goto exit; + } } } cJSON *sharedkeystatus = cJSON_GetObjectItem(json, "sharedKeyStatus"); if (sharedkeystatus != NULL) { if (sharedkeystatus->type != cJSON_NULL) { - set_sharedkeystatus(metadata, sharedkeystatus->valuestring); + if((ret = set_sharedkeystatus(metadata, sharedkeystatus->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + goto exit; + } } else { - set_sharedkeystatus(metadata, "null"); + if((ret = set_sharedkeystatus(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + goto exit; + } } } cJSON *sharedkeyenc = cJSON_GetObjectItem(json, "sharedKeyEnc"); if (sharedkeyenc != NULL) { if (sharedkeyenc->type != cJSON_NULL) { - set_sharedkeyenc(metadata, sharedkeyenc->valuestring); + if((ret = set_sharedkeyenc(metadata, sharedkeyenc->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + goto exit; + } } else { - set_sharedkeyenc(metadata, "null"); + if((ret = set_sharedkeyenc(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + goto exit; + } } } cJSON *pubkeyhash = cJSON_GetObjectItem(json, "pubKeyHash"); if (pubkeyhash != NULL) { if (pubkeyhash->type != cJSON_NULL) { - set_pubkeyhash(metadata, pubkeyhash->valuestring); + if((ret = set_pubkeyhash(metadata, pubkeyhash->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + goto exit; + } } else { - set_pubkeyhash(metadata, "null"); + if((ret = set_pubkeyhash(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + goto exit; + } } } cJSON *pubkeyalgo = cJSON_GetObjectItem(json, "pubKeyAlgo"); if (pubkeyalgo != NULL) { if (pubkeyalgo->type != cJSON_NULL) { - set_pubkeyalgo(metadata, pubkeyalgo->valuestring); + if((ret = set_pubkeyalgo(metadata, pubkeyalgo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); + goto exit; + } } else { - set_pubkeyalgo(metadata, "null"); + if((ret = set_pubkeyalgo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); + goto exit; + } } } cJSON *encoding = cJSON_GetObjectItem(json, "encoding"); if (encoding != NULL) { if (encoding->type != cJSON_NULL) { - set_encoding(metadata, encoding->valuestring); + if((ret = set_encoding(metadata, encoding->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); + goto exit; + } } else { - set_encoding(metadata, "null"); + if((ret = set_encoding(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); + goto exit; + } } } cJSON *enckeyname = cJSON_GetObjectItem(json, "encKeyName"); if (enckeyname != NULL) { if (enckeyname->type != cJSON_NULL) { - set_enckeyname(metadata, enckeyname->valuestring); + if((ret = set_enckeyname(metadata, enckeyname->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + goto exit; + } } else { - set_enckeyname(metadata, "null"); + if((ret = set_enckeyname(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + goto exit; + } } } cJSON *encalgo = cJSON_GetObjectItem(json, "encAlgo"); if (encalgo != NULL) { if (encalgo->type != cJSON_NULL) { - set_encalgo(metadata, encalgo->valuestring); + if((ret = set_encalgo(metadata, encalgo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + goto exit; + } } else { - set_encalgo(metadata, "null"); + if((ret = set_encalgo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + goto exit; + } } } cJSON *ivnonce = cJSON_GetObjectItem(json, "ivNonce"); if (ivnonce != NULL) { if (ivnonce->type != cJSON_NULL) { - set_ivnonce(metadata, ivnonce->valuestring); + if((ret = set_ivnonce(metadata, ivnonce->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + goto exit; + } } else { - set_ivnonce(metadata, "null"); + if((ret = set_ivnonce(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + goto exit; + } } } cJSON *skeenckeyname = cJSON_GetObjectItem(json, "skeEncKeyName"); if (skeenckeyname != NULL) { if (skeenckeyname->type != cJSON_NULL) { - set_skeenckeyname(metadata, skeenckeyname->valuestring); + if((ret = set_skeenckeyname(metadata, skeenckeyname->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + goto exit; + } } else { - set_skeenckeyname(metadata, "null"); + if((ret = set_skeenckeyname(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + goto exit; + } } } cJSON *skeencalgo = cJSON_GetObjectItem(json, "skeEncAlgo"); if (skeencalgo != NULL) { if (skeencalgo->type != cJSON_NULL) { - set_skeencalgo(metadata, skeencalgo->valuestring); + if((ret = set_skeencalgo(metadata, skeencalgo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + goto exit; + } } else { - set_skeencalgo(metadata, "null"); + if((ret = set_skeencalgo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + goto exit; + } } } + + ret = 0; + goto exit; +exit: { return ret; } } int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char **metadatastr) { int ret = 1; - if(metadata == NULL) { + if (metadata == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); return ret; } - if(metadatastr == NULL) { + if (metadatastr == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr is NULL\n"); return ret; @@ -421,6 +557,11 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char *jsonstr = NULL; cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_CreateObject failed\n"); + goto exit; + } if (atclient_atkey_metadata_is_createdby_initialized(metadata)) { cJSON_AddStringToObject(root, "createdBy", metadata->createdby); @@ -553,7 +694,6 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, memcpy(*metadatastr, jsonstr, strlen(jsonstr)); (*metadatastr)[strlen(jsonstr)] = '\0'; - ret = 0; goto exit; exit: { From cf0f38f6759ad2874093d4a3e8ea14b50b41cb9b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 12:02:45 -0400 Subject: [PATCH 077/193] fix: crud example --- examples/desktop/crud/get_publickey.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index 792ac7fe..fc02af24 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -52,7 +52,10 @@ int main() { char *atserver_host = NULL; int atserver_port = -1; - if((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != 0) { + char *metadatajsonstr = NULL; + + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } @@ -74,32 +77,25 @@ int main() { const size_t atkeystrlen = strlen(atkeystr); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, - atkeystr); + atkeystr); - ret = atclient_get_publickey(&atclient, &atkey, value, valuelen, &valueolen, true); - if (ret != 0) { + if ((ret = atclient_get_publickey(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Data: \"%.*s\"\n", (int)valueolen, value); - char metadatajsonstr[4096]; - memset(metadatajsonstr, 0, 4096); - size_t metadatstrolen = 0; - - ret = atclient_atkey_metadata_to_jsonstr(&atkey.metadata, metadatajsonstr, 4096, &metadatstrolen); - if (ret != 0) { + if ((ret = atclient_atkey_metadata_to_jsonstr(&atkey.metadata, &metadatajsonstr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert metadata to json string"); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Metadata: \"%.*s\"\n", (int)metadatstrolen, - metadatajsonstr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Metadata: \"%.*s\"\n", metadatajsonstr); ret = 0; goto exit; -exit : { +exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); atclient_atsign_free(&atsign); From ee1d4b769d503283653312bfca26627d4db70b99 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 12:12:54 -0400 Subject: [PATCH 078/193] chore: remove isHidden --- packages/atclient/include/atclient/metadata.h | 7 --- packages/atclient/src/metadata.c | 52 ------------------- 2 files changed, 59 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 0b09cc7f..16be7ed2 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -136,10 +136,6 @@ typedef struct atclient_atkey_metadata { // This field is not written to the protocol string by the SDK. It is a strictly client-side metadata. bool ispublic : 1; - // TODO: info about this metadata - // This field is not written to the protocol string by the SDK. It is a strictly client-side metadata. - bool ishidden : 1; - // iscached=true means the key contains 'cached:', written and used by client SDK only, not written to protocol string // iscached=false means the key does not contain 'cached:' bool iscached : 1; @@ -324,7 +320,6 @@ bool atclient_atkey_metadata_is_refreshat_initialized(const atclient_atkey_metad bool atclient_atkey_metadata_is_createdat_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_updatedat_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_ispublic_initialized(const atclient_atkey_metadata *metadata); -bool atclient_atkey_metadata_is_ishidden_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_iscached_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_ttl_initialized(const atclient_atkey_metadata *metadata); bool atclient_atkey_metadata_is_ttb_initialized(const atclient_atkey_metadata *metadata); @@ -354,7 +349,6 @@ size_t atclient_atkey_metadata_refreshat_strlen(const atclient_atkey_metadata *m size_t atclient_atkey_metadata_createdat_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_updatedat_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_ispublic_strlen(const atclient_atkey_metadata *metadata); -size_t atclient_atkey_metadata_ishidden_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_iscached_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadata); size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata); @@ -375,7 +369,6 @@ size_t atclient_atkey_metadata_skeenckeyname_strlen(const atclient_atkey_metadat size_t atclient_atkey_metadata_skeencalgo_strlen(const atclient_atkey_metadata *metadata); int atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); -int atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden); int atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached); int atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl); int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 3d2bbdd6..1e20bae6 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -19,7 +19,6 @@ static bool is_refreshat_initialized(const atclient_atkey_metadata *metadata); static bool is_createdat_initialized(const atclient_atkey_metadata *metadata); static bool is_updatedat_initialized(const atclient_atkey_metadata *metadata); static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata); -static bool is_ishidden_initialized(const atclient_atkey_metadata *metadata); static bool is_iscached_initialized(const atclient_atkey_metadata *metadata); static bool is_ttl_initialized(const atclient_atkey_metadata *metadata); static bool is_ttb_initialized(const atclient_atkey_metadata *metadata); @@ -49,7 +48,6 @@ static void set_is_refreshat_initialized(atclient_atkey_metadata *metadata, bool static void set_is_createdat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_updatedat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_ishidden_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ttl_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ttb_initialized(atclient_atkey_metadata *metadata, bool is_initialized); @@ -79,7 +77,6 @@ static void unset_refreshat(atclient_atkey_metadata *metadata); static void unset_createdat(atclient_atkey_metadata *metadata); static void unset_updatedat(atclient_atkey_metadata *metadata); static void unset_ispublic(atclient_atkey_metadata *metadata); -static void unset_ishidden(atclient_atkey_metadata *metadata); static void unset_iscached(atclient_atkey_metadata *metadata); static void unset_ttl(atclient_atkey_metadata *metadata); static void unset_ttb(atclient_atkey_metadata *metadata); @@ -109,7 +106,6 @@ static int set_refreshat(atclient_atkey_metadata *metadata, const char *refresha static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat); static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat); static void set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); -static void set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden); static void set_iscached(atclient_atkey_metadata *metadata, const bool iscached); static void set_ttl(atclient_atkey_metadata *metadata, const long ttl); static void set_ttb(atclient_atkey_metadata *metadata, const long ttb); @@ -304,12 +300,6 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c // set_ispublic(metadata, ispublic->valueint); // } - // I don't think this field exists when reading metadata from atServer - // cJSON *ishidden = cJSON_GetObjectItem(root, "isHidden"); - // if(ishidden != NULL) { - // set_ishidden(metadata, ishidden->valueint); - // } - // I don't think this field exists when reading metadata from atServer // cJSON *iscached = cJSON_GetObjectItem(root, "isCached // if(iscached != NULL) { @@ -603,10 +593,6 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, cJSON_AddBoolToObject(root, "isPublic", metadata->ispublic); } - if (atclient_atkey_metadata_is_ishidden_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isHidden", metadata->ishidden); - } - if (atclient_atkey_metadata_is_iscached_initialized(metadata)) { cJSON_AddBoolToObject(root, "isCached", metadata->iscached); } @@ -1037,10 +1023,6 @@ bool atclient_atkey_metadata_is_ispublic_initialized(const atclient_atkey_metada return is_ispublic_initialized(metadata); } -bool atclient_atkey_metadata_is_ishidden_initialized(const atclient_atkey_metadata *metadata) { - return is_ishidden_initialized(metadata); -} - bool atclient_atkey_metadata_is_iscached_initialized(const atclient_atkey_metadata *metadata) { return is_iscached_initialized(metadata); } @@ -1121,14 +1103,6 @@ int atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, cons return 0; } -int atclient_atkey_metadata_set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden) { - if (is_ishidden_initialized(metadata)) { - unset_ishidden(metadata); - } - set_ishidden(metadata, ishidden); - return 0; -} - int atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { if (is_iscached_initialized(metadata)) { unset_iscached(metadata); @@ -1381,10 +1355,6 @@ void atclient_atkey_metadata_free(atclient_atkey_metadata *metadata) { unset_ispublic(metadata); } - if (is_ishidden_initialized(metadata)) { - unset_ishidden(metadata); - } - if (is_iscached_initialized(metadata)) { unset_iscached(metadata); } @@ -1500,10 +1470,6 @@ static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata) { return (metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] & ATKEY_METADATA_ISPUBLIC_INITIALIZED); } -static bool is_ishidden_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] & ATKEY_METADATA_ISHIDDEN_INITIALIZED); -} - static bool is_iscached_initialized(const atclient_atkey_metadata *metadata) { return (metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] & ATKEY_METADATA_ISCACHED_INITIALIZED); } @@ -1657,14 +1623,6 @@ static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool } } -static void set_is_ishidden_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { - if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] |= ATKEY_METADATA_ISHIDDEN_INITIALIZED; - } else { - metadata->_initializedfields[ATKEY_METADATA_ISHIDDEN_INDEX] &= ~ATKEY_METADATA_ISHIDDEN_INITIALIZED; - } -} - static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] |= ATKEY_METADATA_ISCACHED_INITIALIZED; @@ -1883,11 +1841,6 @@ static void unset_ispublic(atclient_atkey_metadata *metadata) { set_is_ispublic_initialized(metadata, false); } -static void unset_ishidden(atclient_atkey_metadata *metadata) { - metadata->ishidden = false; - set_is_ishidden_initialized(metadata, false); -} - static void unset_iscached(atclient_atkey_metadata *metadata) { metadata->iscached = false; set_is_iscached_initialized(metadata, false); @@ -2156,11 +2109,6 @@ static void set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic) set_is_ispublic_initialized(metadata, true); } -static void set_ishidden(atclient_atkey_metadata *metadata, const bool ishidden) { - metadata->ishidden = ishidden; - set_is_ishidden_initialized(metadata, true); -} - static void set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { metadata->iscached = iscached; set_is_iscached_initialized(metadata, true); From a02eaf6903090ac5f58b5477cc707db8dc0cd154 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 13:50:42 -0400 Subject: [PATCH 079/193] feat: atkeys.h/.c initializedfields --- packages/atchops/src/base64.c | 38 +- packages/atclient/include/atclient/atkeys.h | 51 +- packages/atclient/src/atclient_get_selfkey.c | 4 +- packages/atclient/src/atclient_put.c | 4 +- packages/atclient/src/atkey.c | 5 +- packages/atclient/src/atkeys.c | 512 ++++++++++++++++--- packages/atclient/src/metadata.c | 140 +---- 7 files changed, 539 insertions(+), 215 deletions(-) diff --git a/packages/atchops/src/base64.c b/packages/atchops/src/base64.c index dfa1e75c..cd200aa6 100644 --- a/packages/atchops/src/base64.c +++ b/packages/atchops/src/base64.c @@ -1,17 +1,51 @@ #include "atchops/base64.h" +#include #include #include +#define TAG "base64" + #define MAX(a, b) ((a) > (b) ? (a) : (b)) int atchops_base64_encode(const unsigned char *src, const size_t srclen, unsigned char *dst, const size_t dstsize, size_t *dstlen) { - return mbedtls_base64_encode(dst, dstsize, dstlen, src, srclen); + int ret = 1; + if (src == NULL || srclen <= 0 || dstsize <= 0) { + ret = 1; + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: Invalid arguments\n"); + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "src: %p, srclen: %d, dst: %p, dstsize: %d\n", src, srclen, dst, + dstsize); + goto exit; + } + if (dstlen == NULL) { + size_t x; // throw away variable + ret = mbedtls_base64_encode(dst, dstsize, &x, src, srclen); + } else { + ret = mbedtls_base64_encode(dst, dstsize, dstlen, src, srclen); + } + goto exit; +exit: { return ret; } } int atchops_base64_decode(const unsigned char *src, const size_t srclen, unsigned char *dst, const size_t dstsize, size_t *dstlen) { - return mbedtls_base64_decode(dst, dstsize, dstlen, src, srclen); + int ret = 1; + if (src == NULL || srclen <= 0 || dstsize <= 0) { + ret = 1; + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: Invalid arguments\n"); + // log them + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "src: %p, srclen: %d, dst: %p, dstsize: %d\n", src, srclen, dst, + dstsize); + goto exit; + } + if (dstlen == NULL) { + size_t x; // throw away variable + ret = mbedtls_base64_decode(dst, dstsize, &x, src, srclen); + } else { + ret = mbedtls_base64_decode(dst, dstsize, dstlen, src, srclen); + } + goto exit; +exit: { return ret; } } size_t atchops_base64_encoded_size(const size_t plaintextsize) { diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index c6a8f5eb..4474b4a6 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -5,6 +5,21 @@ #include "atclient/atstr.h" #include #include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define PKAMPUBLICKEY_INDEX 0 +#define PKAMPRIVATEKEY_INDEX 0 +#define ENCRYPTPUBLICKEY_INDEX 0 +#define ENCRYPTPRIVATEKEY_INDEX 0 +#define SELFENCRYPTIONKEY_INDEX 0 + +#define PKAMPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define PKAMPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 1) +#define ENCRYPTPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 2) +#define ENCRYPTPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 3) +#define SELFENCRYPTIONKEY_INITIALIZED (VALUE_INITIALIZED << 4) /** * @brief represents the atkeys file @@ -18,19 +33,21 @@ * 4. (for rsakeys), the rsakey struct used in rsa operations. */ typedef struct atclient_atkeys { - atclient_atstr pkampublickeystr; // base64 encoded, RSA-2048 key, decrypted + char *pkampublickeybase64; // base64 encoded, RSA-2048 key, decrypted atchops_rsakey_publickey pkampublickey; // contains n, e - atclient_atstr pkamprivatekeystr; // base64 encoded, RSA-2048 key, decrypted + char *pkamprivatekeybase64; // base64 encoded, RSA-2048 key, decrypted atchops_rsakey_privatekey pkamprivatekey; // conatins n, e, d, p, q - atclient_atstr encryptpublickeystr; // base64 encoded, RSA-2048 key, decrypted + char *encryptpublickeybase64; // base64 encoded, RSA-2048 key, decrypted atchops_rsakey_publickey encryptpublickey; // contains n, e - atclient_atstr encryptprivatekeystr; // base64 encoded, RSA-2048 key, decrypted + char *encryptprivatekeybase64; // base64 encoded, RSA-2048 key, decrypted atchops_rsakey_privatekey encryptprivatekey; // conatins n, e, d, p, q - atclient_atstr selfencryptionkeystr; // base64 encoded, AES-256 key, decrypted + char *selfencryptionkeybase64; // base64 encoded, AES-256 key, decrypted + + uint8_t _initializedfields[1]; // used to track which fields have been initialized } atclient_atkeys; /** @@ -40,25 +57,36 @@ typedef struct atclient_atkeys { */ void atclient_atkeys_init(atclient_atkeys *atkeys); +/** + * @brief free memory allocated by the init function + * + * @param atkeys the atkeys struct to free + */ +void atclient_atkeys_free(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 + * @param atkeys the struct to populate, assumed to be intialized with atclient_atkeys_init * @param aespkampublickeystr the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 format + * @param aespkampublickeylen the length of the aespkampublickeystr buffer * @param aespkamprivatekeystr the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 format + * @param aespkamprivatekeylen the length of the aespkamprivatekeystr buffer * @param aesencryptpublickeystr the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 * format + * @param aesencryptpublickeylen the length of the aesencryptpublickeystr buffer * @param aesencryptprivatekeystr the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 * format + * @param aesencryptprivatekeylen the length of the aesencryptprivatekeystr buffer * @param selfencryptionkeystr the (decrypted) AES-256 selfencryptionkey in base64 format + * @param selfencryptionkeylen the length of the selfencryptionkeystr buffer * @return int 0 on success, non-zero on failure */ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, const size_t aespkampublickeylen, const char *aespkamprivatekeystr, const size_t aespkamprivatekeylen, const char *aesencryptpublickeystr, - const size_t aesencryptpublickeylen, - const char *aesencryptprivatekeystr, + const size_t aesencryptpublickeylen, const char *aesencryptprivatekeystr, const size_t aesencryptprivatekeylen, const char *selfencryptionkeystr, const size_t selfencryptionkeylen); @@ -82,11 +110,4 @@ int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atcl */ int atclient_atkeys_populate_from_path(atclient_atkeys *atkeys, const char *path); -/** - * @brief free memory allocated by the init function - * - * @param atkeys the atkeys struct to free - */ -void atclient_atkeys_free(atclient_atkeys *atkeys); - #endif diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 20c89fe9..aa279644 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -145,8 +145,8 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, goto exit; } - if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.selfencryptionkeystr.str, - atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, selfencryptionkeysize, + if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.selfencryptionkeybase64, + strlen(atclient->atkeys.selfencryptionkeybase64), selfencryptionkey, selfencryptionkeysize, &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index afc49fc6..dec00bba 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -108,8 +108,8 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); size_t selfencryptionkeylen = 0; - if ((ret = atchops_base64_decode((const unsigned char *)atclient->atkeys.selfencryptionkeystr.str, - atclient->atkeys.selfencryptionkeystr.len, selfencryptionkey, + if ((ret = atchops_base64_decode((const unsigned char *)atclient->atkeys.selfencryptionkeybase64, + strlen(atclient->atkeys.selfencryptionkeybase64), selfencryptionkey, selfencryptionkeysize, &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 51728c60..269dda4a 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -23,7 +23,10 @@ void atclient_atkey_init(atclient_atkey *atkey) { atclient_atkey_metadata_init(&(atkey->metadata)); } -void atclient_atkey_free(atclient_atkey *atkey) { atclient_atkey_metadata_free(&atkey->metadata); } +void atclient_atkey_free(atclient_atkey *atkey) { + atclient_atkey_metadata_free(&atkey->metadata); + memset(atkey, 0, sizeof(atclient_atkey)); +} size_t atclient_atkey_strlen(const atclient_atkey *atkey) { if (atkey == NULL) { diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 726fdf83..db3bb6a5 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -7,26 +7,53 @@ #include #include #include +#include #include #define TAG "atkeys" +static bool is_pkampublickeybase64_initialized(atclient_atkeys *atkeys); +static bool is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys); +static bool is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys); +static bool is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys); +static bool is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys); + +static void set_pkampublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); +static void set_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); +static void set_encryptpublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); +static void set_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); +static void set_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); + +static void unset_pkampublickeybase64(atclient_atkeys *atkeys); +static void unset_pkamprivatekeybase64(atclient_atkeys *atkeys); +static void unset_encryptpublickeybase64(atclient_atkeys *atkeys); +static void unset_encryptprivatekeybase64(atclient_atkeys *atkeys); +static void unset_selfencryptionkeybase64(atclient_atkeys *atkeys); + +static int set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeylen); +static int set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeylen); +static int set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeylen); +static int set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeylen); +static int set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, + const size_t selfencryptionkeylen); + void atclient_atkeys_init(atclient_atkeys *atkeys) { memset(atkeys, 0, sizeof(atclient_atkeys)); - - atclient_atstr_init(&(atkeys->pkampublickeystr), 4096); atchops_rsakey_publickey_init(&(atkeys->pkampublickey)); - - atclient_atstr_init(&(atkeys->pkamprivatekeystr), 4096); atchops_rsakey_privatekey_init(&(atkeys->pkamprivatekey)); - - atclient_atstr_init(&(atkeys->encryptpublickeystr), 4096); atchops_rsakey_publickey_init(&(atkeys->encryptpublickey)); - - atclient_atstr_init(&(atkeys->encryptprivatekeystr), 4096); atchops_rsakey_privatekey_init(&(atkeys->encryptprivatekey)); +} - atclient_atstr_init(&(atkeys->selfencryptionkeystr), 4096); +void atclient_atkeys_free(atclient_atkeys *atkeys) { + atchops_rsakey_publickey_free(&(atkeys->pkampublickey)); + atchops_rsakey_privatekey_free(&(atkeys->pkamprivatekey)); + atchops_rsakey_publickey_free(&(atkeys->encryptpublickey)); + atchops_rsakey_privatekey_free(&(atkeys->encryptprivatekey)); } int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, @@ -37,152 +64,251 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a const size_t selfencryptionkeystrlen) { int ret = 1; + /* + * 1. Validate arguments + */ + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (aespkampublickeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkampublickeystr is NULL\n"); + return ret; + } + + if (aespkamprivatekeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkamprivatekeystr is NULL\n"); + return ret; + } + + if (aesencryptpublickeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptpublickeystr is NULL\n"); + return ret; + } + + if (aesencryptprivatekeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptprivatekeystr is NULL\n"); + return ret; + } + + if (selfencryptionkeystr == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeystr is NULL\n"); + return ret; + } + + if (aespkampublickeylen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkampublickeylen is 0\n"); + return ret; + } + + if (aespkamprivatekeylen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkamprivatekeylen is 0\n"); + return ret; + } + + if (aesencryptpublickeylen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptpublickeylen is 0\n"); + return ret; + } + + if (aesencryptprivatekeylen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptprivatekeylen is 0\n"); + return ret; + } + + if (selfencryptionkeystrlen == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeystrlen is 0\n"); + return ret; + } + + /* + * 2. Initialize variables + */ + + // 2a. Use legacy IV + // the atKeys are encrypted with bytes of 0s unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + // holds the base64-decoded non-encrypted self encryption key + // to use for decrypting the other RSA keys const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; unsigned char selfencryptionkey[selfencryptionkeysize]; memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); size_t selfencryptionkeylen = 0; - const size_t rsakeyencryptedsize = 2048; + // temporarily holds the base64-encoded encrypted RSA key for decryption + const size_t rsakeyencryptedsize = 4096; unsigned char rsakeyencrypted[rsakeyencryptedsize]; memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); size_t rsakeyencryptedlen = 0; - ; - - // 1. decrypt *.atKeys and populate atkeys struct - // 1a. self encryption key - ret = atclient_atstr_set(&(atkeys->selfencryptionkeystr), selfencryptionkeystr, selfencryptionkeystrlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set selfencryptionkeystr\n", - ret); + // temporarily holds the base64-encoded decrypted RSA key + const size_t rsakeydecryptedsize = 4096; + unsigned char rsakeydecrypted[rsakeydecryptedsize]; + memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); + size_t rsakeydecryptedlen = 0; + + /* + * 3. Prepare self encryption key for use + */ + if ((ret = atchops_base64_decode((unsigned char *)selfencryptionkeystr, selfencryptionkeystrlen, selfencryptionkey, + selfencryptionkeysize, &selfencryptionkeylen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding selfencryption key: %d\n", ret); goto exit; } - ret = atchops_base64_decode((unsigned char *)atkeys->selfencryptionkeystr.str, atkeys->selfencryptionkeystr.len, - selfencryptionkey, selfencryptionkeysize, &selfencryptionkeylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding selfencryption key: %d\n", ret); + /* + * 4. Decrypt and populate atkeys struct + */ + + // 4a. self encryption key + if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeystr, selfencryptionkeylen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set selfencryptionkeystr\n", + ret); goto exit; } - // 1b. pkam public key - ret = atchops_base64_decode((unsigned char *)aespkampublickeystr, aespkampublickeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen); - if (ret != 0) { + // 4b. pkam public key + if ((ret = atchops_base64_decode((unsigned char *)aespkampublickeystr, aespkampublickeylen, rsakeyencrypted, + rsakeyencryptedsize, &rsakeyencryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding pkam public key: %d\n", ret); goto exit; } - ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - (unsigned char *)atkeys->pkampublickeystr.str, atkeys->pkampublickeystr.size, - &(atkeys->pkampublickeystr.len)); - if (ret != 0) { + if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, + rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d | failed to decrypt pkam public key\n", ret); goto exit; } + if ((ret = set_pkampublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set pkampublickeystr\n", ret); + goto exit; + } + memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); + memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - // 1c. pkam private key - ret = atchops_base64_decode((unsigned char *)aespkamprivatekeystr, aespkamprivatekeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen); - if (ret != 0) { + // 4c. pkam private key + if ((ret = atchops_base64_decode((unsigned char *)aespkamprivatekeystr, aespkamprivatekeylen, rsakeyencrypted, + rsakeyencryptedsize, &rsakeyencryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding pkam private key: %d\n", ret); goto exit; } - ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - (unsigned char *)atkeys->pkamprivatekeystr.str, atkeys->pkamprivatekeystr.size, - &(atkeys->pkamprivatekeystr.len)); - if (ret != 0) { + if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, + rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d | failed to decrypt pkam private key\n", ret); goto exit; } + if ((ret = set_pkamprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set pkamprivatekeystr\n", ret); + goto exit; + } + memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); + memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - // 1d. encrypt public key - ret = atchops_base64_decode((unsigned char *)aesencryptpublickeystr, aesencryptpublickeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen); - if (ret != 0) { + // 4d. encrypt public key + if ((ret = atchops_base64_decode((unsigned char *)aesencryptpublickeystr, aesencryptpublickeylen, rsakeyencrypted, + rsakeyencryptedsize, &rsakeyencryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding encrypt public key: %d\n", ret); goto exit; } - ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - (unsigned char *)atkeys->encryptpublickeystr.str, atkeys->encryptpublickeystr.size, - &(atkeys->encryptpublickeystr.len)); - if (ret != 0) { + if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, + rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d | failed to decrypt encrypt public key\n", ret); goto exit; } + if ((ret = set_encryptpublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set encryptpublickeystr\n", + ret); + goto exit; + } + memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); + memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - // 1e. encrypt private key - ret = atchops_base64_decode((unsigned char *)aesencryptprivatekeystr, aesencryptprivatekeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen); - if (ret != 0) { + // 4e. encrypt private key + if ((ret = atchops_base64_decode((unsigned char *)aesencryptprivatekeystr, aesencryptprivatekeylen, rsakeyencrypted, + rsakeyencryptedsize, &rsakeyencryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding encrypt private key: %d\n", ret); goto exit; } - ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - (unsigned char *)atkeys->encryptprivatekeystr.str, atkeys->encryptprivatekeystr.size, - &(atkeys->encryptprivatekeystr.len)); - if (ret != 0) { + if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, + rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d | failed to decrypt encrypt private key\n", ret); goto exit; } - // 2. populate rsa structs in atkeys struct (4 keys) + if ((ret = set_encryptprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set encryptprivatekeystr\n", + ret); + goto exit; + } - // 2a. pkam public key - ret = atchops_rsakey_populate_publickey(&(atkeys->pkampublickey), atkeys->pkampublickeystr.str, - atkeys->pkampublickeystr.len); - if (ret != 0) { + /* + * 5. Populate rsakey structs + */ + + // 5a. pkam public key + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkampublickey), atkeys->pkampublickeybase64, + strlen(atkeys->pkampublickeybase64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d | failed to populate pkam public key\n", ret); goto exit; } - // 2b. pkam private key - ret = atchops_rsakey_populate_privatekey(&(atkeys->pkamprivatekey), atkeys->pkamprivatekeystr.str, - atkeys->pkamprivatekeystr.len); - if (ret != 0) { + // 5b. pkam private key + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkamprivatekey), atkeys->pkamprivatekeybase64, + strlen(atkeys->pkamprivatekeybase64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_privatekey: %d | failed to populate pkam private key\n", ret); goto exit; } - // 2c. encrypt public key - ret = atchops_rsakey_populate_privatekey(&(atkeys->encryptprivatekey), atkeys->encryptprivatekeystr.str, - atkeys->encryptprivatekeystr.len); - if (ret != 0) { + // 5c. encrypt public key + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encryptprivatekey), atkeys->encryptprivatekeybase64, + strlen(atkeys->encryptprivatekeybase64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_privatekey: %d | failed to populate encrypt private key\n", ret); goto exit; } - // 2d. encrypt private key - ret = atchops_rsakey_populate_publickey(&(atkeys->encryptpublickey), atkeys->encryptpublickeystr.str, - atkeys->encryptpublickeystr.len); - if (ret != 0) { + // 5d. encrypt private key + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encryptpublickey), atkeys->encryptpublickeybase64, + strlen(atkeys->encryptpublickeybase64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d | failed to populate encrypt public key\n", ret); goto exit; } + ret = 0; goto exit; exit: { return ret; } @@ -234,14 +360,238 @@ exit: { } } -void atclient_atkeys_free(atclient_atkeys *atkeys) { - atclient_atstr_free(&(atkeys->pkampublickeystr)); - atchops_rsakey_publickey_free(&(atkeys->pkampublickey)); - atclient_atstr_free(&(atkeys->pkamprivatekeystr)); - atchops_rsakey_privatekey_free(&(atkeys->pkamprivatekey)); - atclient_atstr_free(&(atkeys->encryptpublickeystr)); - atchops_rsakey_publickey_free(&(atkeys->encryptpublickey)); - atclient_atstr_free(&(atkeys->encryptprivatekeystr)); - atchops_rsakey_privatekey_free(&(atkeys->encryptprivatekey)); - atclient_atstr_free(&(atkeys->selfencryptionkeystr)); +static bool is_pkampublickeybase64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] & PKAMPUBLICKEY_INITIALIZED; +} + +static bool is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] & PKAMPRIVATEKEY_INITIALIZED; +} + +static bool is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] & ENCRYPTPUBLICKEY_INITIALIZED; +} + +static bool is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] & ENCRYPTPRIVATEKEY_INITIALIZED; +} + +static bool is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys) { + return atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] & SELFENCRYPTIONKEY_INITIALIZED; +} + +static void set_pkampublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] |= PKAMPUBLICKEY_INITIALIZED; + } else { + atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] &= ~PKAMPUBLICKEY_INITIALIZED; + } +} + +static void set_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] |= PKAMPRIVATEKEY_INITIALIZED; + } else { + atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] &= ~PKAMPRIVATEKEY_INITIALIZED; + } +} + +static void set_encryptpublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] |= ENCRYPTPUBLICKEY_INITIALIZED; + } else { + atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] &= ~ENCRYPTPUBLICKEY_INITIALIZED; + } +} + +static void set_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] |= ENCRYPTPRIVATEKEY_INITIALIZED; + } else { + atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] &= ~ENCRYPTPRIVATEKEY_INITIALIZED; + } +} + +static void set_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { + if (initialized) { + atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] |= SELFENCRYPTIONKEY_INITIALIZED; + } else { + atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] &= ~SELFENCRYPTIONKEY_INITIALIZED; + } +} + +static void unset_pkampublickeybase64(atclient_atkeys *atkeys) { + if (is_pkampublickeybase64_initialized(atkeys)) { + free(atkeys->pkampublickeybase64); + } + atkeys->pkampublickeybase64 = NULL; + set_pkampublickeybase64_initialized(atkeys, false); +} + +static void unset_pkamprivatekeybase64(atclient_atkeys *atkeys) { + if (is_pkamprivatekeybase64_initialized(atkeys)) { + free(atkeys->pkamprivatekeybase64); + } + atkeys->pkamprivatekeybase64 = NULL; + set_pkamprivatekeybase64_initialized(atkeys, false); +} + +static void unset_encryptpublickeybase64(atclient_atkeys *atkeys) { + if (is_encryptpublickeybase64_initialized(atkeys)) { + free(atkeys->encryptpublickeybase64); + } + atkeys->encryptpublickeybase64 = NULL; + set_encryptpublickeybase64_initialized(atkeys, false); +} + +static void unset_encryptprivatekeybase64(atclient_atkeys *atkeys) { + if (is_encryptprivatekeybase64_initialized(atkeys)) { + free(atkeys->encryptprivatekeybase64); + } + atkeys->encryptprivatekeybase64 = NULL; + set_encryptprivatekeybase64_initialized(atkeys, false); +} + +static void unset_selfencryptionkeybase64(atclient_atkeys *atkeys) { + if (is_selfencryptionkeybase64_initialized(atkeys)) { + free(atkeys->selfencryptionkeybase64); + } + atkeys->selfencryptionkeybase64 = NULL; + set_selfencryptionkeybase64_initialized(atkeys, false); +} + +static int set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeylen) { + int ret = 1; + + if (is_pkampublickeybase64_initialized(atkeys)) { + unset_pkampublickeybase64(atkeys); + } + + const size_t pkampublickeysize = pkampublickeylen + 1; + atkeys->pkampublickeybase64 = (char *)malloc(sizeof(char) * (pkampublickeysize)); + if (atkeys->pkampublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkampublickeybase64\n", + ret); + goto exit; + } + + memcpy(atkeys->pkampublickeybase64, pkampublickeybase64, pkampublickeylen); + atkeys->pkampublickeybase64[pkampublickeylen] = '\0'; + + set_pkampublickeybase64_initialized(atkeys, true); + + ret = 0; + goto exit; + +exit: { return ret; } +} + +static int set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeylen) { + int ret = 1; + + if (is_pkamprivatekeybase64_initialized(atkeys)) { + unset_pkamprivatekeybase64(atkeys); + } + + const size_t pkamprivatekeysize = pkamprivatekeylen + 1; + atkeys->pkamprivatekeybase64 = (char *)malloc(sizeof(char) * (pkamprivatekeysize)); + if (atkeys->pkamprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkamprivatekeybase64\n", + ret); + goto exit; + } + + memcpy(atkeys->pkamprivatekeybase64, pkamprivatekeybase64, pkamprivatekeylen); + atkeys->pkamprivatekeybase64[pkamprivatekeylen] = '\0'; + + set_pkamprivatekeybase64_initialized(atkeys, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeylen) { + int ret = 1; + + if (is_encryptpublickeybase64_initialized(atkeys)) { + unset_encryptpublickeybase64(atkeys); + } + + const size_t encryptpublickeysize = encryptpublickeylen + 1; + atkeys->encryptpublickeybase64 = (char *)malloc(sizeof(char) * (encryptpublickeysize)); + if (atkeys->encryptpublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "malloc: %d | failed to allocate memory for encryptpublickeybase64\n", ret); + goto exit; + } + + memcpy(atkeys->encryptpublickeybase64, encryptpublickeybase64, encryptpublickeylen); + atkeys->encryptpublickeybase64[encryptpublickeylen] = '\0'; + + set_encryptpublickeybase64_initialized(atkeys, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeylen) { + int ret = 1; + + if (is_encryptprivatekeybase64_initialized(atkeys)) { + unset_encryptprivatekeybase64(atkeys); + } + + const size_t encryptprivatekeysize = encryptprivatekeylen + 1; + atkeys->encryptprivatekeybase64 = (char *)malloc(sizeof(char) * (encryptprivatekeysize)); + if (atkeys->encryptprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "malloc: %d | failed to allocate memory for encryptprivatekeybase64\n", ret); + goto exit; + } + + memcpy(atkeys->encryptprivatekeybase64, encryptprivatekeybase64, encryptprivatekeylen); + atkeys->encryptprivatekeybase64[encryptprivatekeylen] = '\0'; + + set_encryptprivatekeybase64_initialized(atkeys, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, + const size_t selfencryptionkeylen) { + int ret = 1; + + if (is_selfencryptionkeybase64_initialized(atkeys)) { + unset_selfencryptionkeybase64(atkeys); + } + + const size_t selfencryptionkeysize = selfencryptionkeylen + 1; + atkeys->selfencryptionkeybase64 = (char *)malloc(sizeof(char) * (selfencryptionkeysize)); + if (atkeys->selfencryptionkeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "malloc: %d | failed to allocate memory for selfencryptionkeybase64\n", ret); + goto exit; + } + + memcpy(atkeys->selfencryptionkeybase64, selfencryptionkeybase64, selfencryptionkeylen); + atkeys->selfencryptionkeybase64[selfencryptionkeylen] = '\0'; + + set_selfencryptionkeybase64_initialized(atkeys, true); + + ret = 0; + goto exit; +exit: { return ret; } } diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 1e20bae6..fb13e71b 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1315,118 +1315,34 @@ exit: { return ret; } } void atclient_atkey_metadata_free(atclient_atkey_metadata *metadata) { - if (is_createdby_initialized(metadata)) { - unset_createdby(metadata); - } - - if (is_updatedby_initialized(metadata)) { - unset_updatedby(metadata); - } - - if (is_status_initialized(metadata)) { - unset_status(metadata); - } - - if (is_version_initialized(metadata)) { - unset_version(metadata); - } - - if (is_expiresat_initialized(metadata)) { - unset_expiresat(metadata); - } - - if (is_availableat_initialized(metadata)) { - unset_availableat(metadata); - } - - if (is_refreshat_initialized(metadata)) { - unset_refreshat(metadata); - } - - if (is_createdat_initialized(metadata)) { - unset_createdat(metadata); - } - - if (is_updatedat_initialized(metadata)) { - unset_updatedat(metadata); - } - - if (is_ispublic_initialized(metadata)) { - unset_ispublic(metadata); - } - - if (is_iscached_initialized(metadata)) { - unset_iscached(metadata); - } - - if (is_ttl_initialized(metadata)) { - unset_ttl(metadata); - } - - if (is_ttb_initialized(metadata)) { - unset_ttb(metadata); - } - - if (is_ttr_initialized(metadata)) { - unset_ttr(metadata); - } - - if (is_ccd_initialized(metadata)) { - unset_ccd(metadata); - } - - if (is_isbinary_initialized(metadata)) { - unset_isbinary(metadata); - } - - if (is_isencrypted_initialized(metadata)) { - unset_isencrypted(metadata); - } - - if (is_datasignature_initialized(metadata)) { - unset_datasignature(metadata); - } - - if (is_sharedkeystatus_initialized(metadata)) { - unset_sharedkeystatus(metadata); - } - - if (is_sharedkeyenc_initialized(metadata)) { - unset_sharedkeyenc(metadata); - } - - if (is_pubkeyhash_initialized(metadata)) { - unset_pubkeyhash(metadata); - } - - if (is_pubkeyalgo_initialized(metadata)) { - unset_pubkeyalgo(metadata); - } - - if (is_encoding_initialized(metadata)) { - unset_encoding(metadata); - } - - if (is_enckeyname_initialized(metadata)) { - unset_enckeyname(metadata); - } - - if (is_encalgo_initialized(metadata)) { - unset_encalgo(metadata); - } - - if (is_ivnonce_initialized(metadata)) { - unset_ivnonce(metadata); - } - - if (is_skeenckeyname_initialized(metadata)) { - unset_skeenckeyname(metadata); - } - - if (is_skeencalgo_initialized(metadata)) { - unset_skeencalgo(metadata); - } - + unset_createdby(metadata); + unset_updatedby(metadata); + unset_status(metadata); + unset_version(metadata); + unset_expiresat(metadata); + unset_availableat(metadata); + unset_refreshat(metadata); + unset_createdat(metadata); + unset_updatedat(metadata); + unset_ispublic(metadata); + unset_iscached(metadata); + unset_ttl(metadata); + unset_ttb(metadata); + unset_ttr(metadata); + unset_ccd(metadata); + unset_isbinary(metadata); + unset_isencrypted(metadata); + unset_datasignature(metadata); + unset_sharedkeystatus(metadata); + unset_sharedkeyenc(metadata); + unset_pubkeyhash(metadata); + unset_pubkeyalgo(metadata); + unset_encoding(metadata); + unset_enckeyname(metadata); + unset_encalgo(metadata); + unset_ivnonce(metadata); + unset_skeenckeyname(metadata); + unset_skeencalgo(metadata); memset(metadata, 0, sizeof(atclient_atkey_metadata)); } From 8b6cafeca1ce568447d75b7031570fb7aba5ddc3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:23:46 -0400 Subject: [PATCH 080/193] feat: remove atclient_atstr from atkeysfile.h/.c --- .../atclient/include/atclient/atkeysfile.h | 26 +- packages/atclient/src/atkeysfile.c | 345 +++++++++++++++--- 2 files changed, 310 insertions(+), 61 deletions(-) diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h index 7395fb7f..503e8157 100644 --- a/packages/atclient/include/atclient/atkeysfile.h +++ b/packages/atclient/include/atclient/atkeysfile.h @@ -3,13 +3,29 @@ #include "atclient/atstr.h" #include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define AESPKAMPUBLICKEYSTR_INDEX 0 +#define AESPKAMPRIVATEKEYSTR_INDEX 0 +#define AESENCRYPTPUBLICKEYSTR_INDEX 0 +#define AESENCRYPTPRIVATEKEYSTR_INDEX 0 +#define SELFENCRYPTIONKEYSTR_INDEX 0 + +#define AESPKAMPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 0) +#define AESPKAMPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 1) +#define AESENCRYPTPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 2) +#define AESENCRYPTPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 3) +#define SELFENCRYPTIONKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 4) typedef struct atclient_atkeysfile { - atclient_atstr aespkamprivatekeystr; - atclient_atstr aespkampublickeystr; - atclient_atstr aesencryptprivatekeystr; - atclient_atstr aesencryptpublickeystr; - atclient_atstr selfencryptionkeystr; + char *aespkampublickeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key + char *aespkamprivatekeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA keyF + char *aesencryptpublickeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key + char *aesencryptprivatekeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key + char *selfencryptionkeystr; // base64-encoded non-encrypted self encryption key. base64 decoding will reveal 32-byte AES key + uint8_t _initializedfields[1]; } atclient_atkeysfile; void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile); diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c index 49cd7b75..7be7bcf0 100644 --- a/packages/atclient/src/atkeysfile.c +++ b/packages/atclient/src/atkeysfile.c @@ -2,45 +2,64 @@ #include "atclient/atstr.h" #include "atlogger/atlogger.h" #include +#include #include #include #include #include -// represents the buffer size of an encrypted RSA key in base64 format -#define BASE64_ENCRYPTED_KEY_BUFFER_SIZE 4096 - // represents buffer size of reading the entire atKeys file #define FILE_READ_BUFFER_SIZE 8192 #define TAG "atkeysfile" -void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile) { - memset(atkeysfile, 0, sizeof(atclient_atkeysfile)); +static bool is_aespkampublickeystr_initialized(atclient_atkeysfile *atkeysfile); +static bool is_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile); +static bool is_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile); +static bool is_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile); +static bool is_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile); - atclient_atstr_init(&(atkeysfile->aespkampublickeystr), BASE64_ENCRYPTED_KEY_BUFFER_SIZE); - atclient_atstr_init(&(atkeysfile->aespkamprivatekeystr), BASE64_ENCRYPTED_KEY_BUFFER_SIZE); - atclient_atstr_init(&(atkeysfile->aesencryptpublickeystr), BASE64_ENCRYPTED_KEY_BUFFER_SIZE); - atclient_atstr_init(&(atkeysfile->aesencryptprivatekeystr), BASE64_ENCRYPTED_KEY_BUFFER_SIZE); - atclient_atstr_init(&(atkeysfile->selfencryptionkeystr), BASE64_ENCRYPTED_KEY_BUFFER_SIZE); -} +static void set_aespkampublickestr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); +static void set_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); +static void set_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); +static void set_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); +static void set_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); + +static void unset_aespkampublickeystr(atclient_atkeysfile *atkeysfile); +static void unset_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile); +static void unset_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile); +static void unset_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile); +static void unset_selfencryptionkeystr(atclient_atkeysfile *atkeysfile); + +static int set_aespkampublickeystr(atclient_atkeysfile *atkeysfile, const char *aespkampublickeystr, + const size_t aespkampublickeystrlen); +static int set_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aespkamprivatekeystr, + const size_t aespkamprivatekeystrlen); +static int set_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptpublickeystr, + const size_t aesencryptpublickeystrlen); +static int set_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptprivatekeystr, + const size_t aesencryptprivatekeystrlen); +static int set_selfencryptionkeystr(atclient_atkeysfile *atkeysfile, const char *selfencryptionkeystr, + const size_t selfencryptionkeystrlen); + +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; - cJSON *root = NULL; - FILE *file = fopen(path, "r"); + unsigned char readbuf[FILE_READ_BUFFER_SIZE]; + memset(readbuf, 0, FILE_READ_BUFFER_SIZE); - atclient_atstr readbuf; - atclient_atstr_init(&readbuf, FILE_READ_BUFFER_SIZE); + cJSON *root = NULL; + FILE *file = fopen(path, "r"); if (file == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fopen failed\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fopen failed\n"); goto exit; } - const size_t bytesread = fread(readbuf.str, 1, FILE_READ_BUFFER_SIZE, file); + const size_t bytesread = fread(readbuf, 1, FILE_READ_BUFFER_SIZE, file); fclose(file); if (bytesread == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fread failed\n"); @@ -48,7 +67,7 @@ int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path) goto exit; } - root = cJSON_Parse(readbuf.str); + root = cJSON_Parse(readbuf); cJSON *aespkampublickey = cJSON_GetObjectItem(root, "aesPkamPublicKey"); cJSON *aespkamprivatekey = cJSON_GetObjectItem(root, "aesPkamPrivateKey"); cJSON *aesencryptpublickey = cJSON_GetObjectItem(root, "aesEncryptPublicKey"); @@ -56,90 +75,304 @@ int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path) cJSON *selfencryptionkey = cJSON_GetObjectItem(root, "selfEncryptionKey"); if (aespkamprivatekey == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPrivateKey!\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPrivateKey!\n"); goto exit; } if (aespkampublickey == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPublicKey!\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPublicKey!\n"); goto exit; } if (aesencryptprivatekey == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPrivateKey!\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPrivateKey!\n"); goto exit; } if (aesencryptpublickey == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPublicKey!\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPublicKey!\n"); goto exit; } if (selfencryptionkey == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading selfEncryptionKey!\n"); ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading selfEncryptionKey!\n"); goto exit; } - ret = atclient_atstr_set(&(atkeysfile->aespkampublickeystr), aespkampublickey->valuestring, - strlen(aespkampublickey->valuestring)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aespkampublickeystr\n", + if ((ret = set_aespkampublickeystr(atkeysfile, aespkampublickey->valuestring, + strlen(aespkampublickey->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aespkampublickeystr: %d | failed to set aespkampublickeystr\n", ret); goto exit; } - ret = atclient_atstr_set(&(atkeysfile->aespkamprivatekeystr), aespkamprivatekey->valuestring, - strlen(aespkamprivatekey->valuestring)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aespkamprivatekeystr\n", - ret); + if ((ret = set_aespkamprivatekeystr(atkeysfile, aespkamprivatekey->valuestring, + strlen(aespkamprivatekey->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_aespkamprivatekeystr: %d | failed to set aespkamprivatekeystr\n", ret); goto exit; } - ret = atclient_atstr_set(&(atkeysfile->aesencryptprivatekeystr), aesencryptprivatekey->valuestring, - strlen(aesencryptprivatekey->valuestring)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aesencryptprivatekeystr\n", - ret); + if ((ret = set_aesencryptpublickeystr(atkeysfile, aesencryptpublickey->valuestring, + strlen(aesencryptpublickey->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_aesencryptpublickeystr: %d | failed to set aesencryptpublickeystr\n", ret); goto exit; } - ret = atclient_atstr_set(&(atkeysfile->aesencryptpublickeystr), aesencryptpublickey->valuestring, - strlen(aesencryptpublickey->valuestring)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set aesencryptpublickeystr\n", - ret); + if ((ret = set_aesencryptprivatekeystr(atkeysfile, aesencryptprivatekey->valuestring, + strlen(aesencryptprivatekey->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_aesencryptprivatekeystr: %d | failed to set aesencryptprivatekeystr\n", ret); goto exit; } - ret = atclient_atstr_set(&(atkeysfile->selfencryptionkeystr), selfencryptionkey->valuestring, - strlen(selfencryptionkey->valuestring)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set selfencryptionkeystr\n", - ret); + if ((ret = set_selfencryptionkeystr(atkeysfile, selfencryptionkey->valuestring, + strlen(selfencryptionkey->valuestring))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_selfencryptionkeystr: %d | failed to set selfencryptionkeystr\n", ret); goto exit; } + ret = 0; goto exit; exit: { - if (root != NULL) { - cJSON_Delete(root); - } - atclient_atstr_free(&readbuf); + cJSON_Delete(root); return ret; } } void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile) { - atclient_atstr_free(&(atkeysfile->aespkamprivatekeystr)); - atclient_atstr_free(&(atkeysfile->aespkampublickeystr)); - atclient_atstr_free(&(atkeysfile->aesencryptprivatekeystr)); - atclient_atstr_free(&(atkeysfile->aesencryptpublickeystr)); - atclient_atstr_free(&(atkeysfile->selfencryptionkeystr)); + unset_aespkampublickeystr(atkeysfile); + unset_aespkamprivatekeystr(atkeysfile); + unset_aesencryptpublickeystr(atkeysfile); + unset_aesencryptprivatekeystr(atkeysfile); + unset_selfencryptionkeystr(atkeysfile); +} + +static bool is_aespkampublickeystr_initialized(atclient_atkeysfile *atkeysfile) { + return atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] & AESPKAMPUBLICKEYSTR_INTIIALIZED; +} + +static bool is_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { + return atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] & AESPKAMPRIVATEKEYSTR_INTIIALIZED; +} + +static bool is_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile) { + return atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] & AESENCRYPTPUBLICKEYSTR_INTIIALIZED; +} + +static bool is_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { + return atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] & AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; +} + +static bool is_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile) { + return atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] & SELFENCRYPTIONKEYSTR_INTIIALIZED; +} + +static void set_aespkampublickestr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { + if (initialized) { + atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] |= AESPKAMPUBLICKEYSTR_INTIIALIZED; + } else { + atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] &= ~AESPKAMPUBLICKEYSTR_INTIIALIZED; + } +} + +static void set_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { + if (initialized) { + atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] |= AESPKAMPRIVATEKEYSTR_INTIIALIZED; + } else { + atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] &= ~AESPKAMPRIVATEKEYSTR_INTIIALIZED; + } +} + +static void set_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { + if (initialized) { + atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] |= AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + } else { + atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] &= ~AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + } +} + +static void set_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { + if (initialized) { + atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] |= AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + } else { + atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] &= ~AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + } +} + +static void set_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { + if (initialized) { + atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] |= SELFENCRYPTIONKEYSTR_INTIIALIZED; + } else { + atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] &= ~SELFENCRYPTIONKEYSTR_INTIIALIZED; + } +} + +static void unset_aespkampublickeystr(atclient_atkeysfile *atkeysfile) { + if (is_aespkampublickeystr_initialized(atkeysfile)) { + free(atkeysfile->aespkampublickeystr); + } + atkeysfile->aespkampublickeystr = NULL; + set_aespkampublickestr_initialized(atkeysfile, false); +} + +static void unset_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile) { + if (is_aespkamprivatekeystr_initialized(atkeysfile)) { + free(atkeysfile->aespkamprivatekeystr); + } + atkeysfile->aespkamprivatekeystr = NULL; + set_aespkamprivatekeystr_initialized(atkeysfile, false); +} + +static void unset_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile) { + if (is_aesencryptpublickeystr_initialized(atkeysfile)) { + free(atkeysfile->aesencryptpublickeystr); + } + atkeysfile->aesencryptpublickeystr = NULL; + set_aesencryptpublickeystr_initialized(atkeysfile, false); +} + +static void unset_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile) { + if (is_aesencryptprivatekeystr_initialized(atkeysfile)) { + free(atkeysfile->aesencryptprivatekeystr); + } + atkeysfile->aesencryptprivatekeystr = NULL; + set_aesencryptprivatekeystr_initialized(atkeysfile, false); } + +static void unset_selfencryptionkeystr(atclient_atkeysfile *atkeysfile) { + if (is_selfencryptionkeystr_initialized(atkeysfile)) { + free(atkeysfile->selfencryptionkeystr); + } + atkeysfile->selfencryptionkeystr = NULL; + set_selfencryptionkeystr_initialized(atkeysfile, false); +} + +static int set_aespkampublickeystr(atclient_atkeysfile *atkeysfile, const char *aespkampublickeystr, + const size_t aespkampublickeystrlen) { + int ret = 1; + + if (is_aespkampublickeystr_initialized(atkeysfile)) { + unset_aespkampublickeystr(atkeysfile); + } + + const size_t aespkampublickeystrsize = aespkampublickeystrlen + 1; + if ((atkeysfile->aespkampublickeystr = (char *)malloc(sizeof(char) * aespkampublickeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aespkampublickestr_initialized(atkeysfile, true); + memcpy(atkeysfile->aespkampublickeystr, aespkampublickeystr, aespkampublickeystrlen); + atkeysfile->aespkampublickeystr[aespkampublickeystrlen] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aespkamprivatekeystr, + const size_t aespkamprivatekeystrlen) { + int ret = 1; + + if (is_aespkamprivatekeystr_initialized(atkeysfile)) { + unset_aespkamprivatekeystr(atkeysfile); + } + + const size_t aespkamprivatekeystrsize = aespkamprivatekeystrlen + 1; + if ((atkeysfile->aespkamprivatekeystr = (char *)malloc(sizeof(char) * aespkamprivatekeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aespkamprivatekeystr_initialized(atkeysfile, true); + memcpy(atkeysfile->aespkamprivatekeystr, aespkamprivatekeystr, aespkamprivatekeystrlen); + atkeysfile->aespkamprivatekeystr[aespkamprivatekeystrlen] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} +static int set_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptpublickeystr, + const size_t aesencryptpublickeystrlen) { + int ret = 1; + + if (is_aesencryptpublickeystr_initialized(atkeysfile)) { + unset_aesencryptpublickeystr(atkeysfile); + } + + const size_t aesencryptpublickeystrsize = aesencryptpublickeystrlen + 1; + if ((atkeysfile->aesencryptpublickeystr = (char *)malloc(sizeof(char) * aesencryptpublickeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aesencryptpublickeystr_initialized(atkeysfile, true); + memcpy(atkeysfile->aesencryptpublickeystr, aesencryptpublickeystr, aesencryptpublickeystrlen); + atkeysfile->aesencryptpublickeystr[aesencryptpublickeystrlen] = '\0'; + + ret = 0; + goto exit; + +exit: { return ret; } +} + +static int set_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptprivatekeystr, + const size_t aesencryptprivatekeystrlen) { + int ret = 1; + + if (is_aesencryptprivatekeystr_initialized(atkeysfile)) { + unset_aesencryptprivatekeystr(atkeysfile); + } + + const size_t aesencryptprivatekeystrsize = aesencryptprivatekeystrlen + 1; + if ((atkeysfile->aesencryptprivatekeystr = (char *)malloc(sizeof(char) * aesencryptprivatekeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_aesencryptprivatekeystr_initialized(atkeysfile, true); + memcpy(atkeysfile->aesencryptprivatekeystr, aesencryptprivatekeystr, aesencryptprivatekeystrlen); + atkeysfile->aesencryptprivatekeystr[aesencryptprivatekeystrlen] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int set_selfencryptionkeystr(atclient_atkeysfile *atkeysfile, const char *selfencryptionkeystr, + const size_t selfencryptionkeystrlen) { + int ret = 1; + + if (is_selfencryptionkeystr_initialized(atkeysfile)) { + unset_selfencryptionkeystr(atkeysfile); + } + + const size_t selfencryptionkeystrsize = selfencryptionkeystrlen + 1; + if ((atkeysfile->selfencryptionkeystr = (char *)malloc(sizeof(char) * selfencryptionkeystrsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + goto exit; + } + + set_selfencryptionkeystr_initialized(atkeysfile, true); + memcpy(atkeysfile->selfencryptionkeystr, selfencryptionkeystr, selfencryptionkeystrlen); + atkeysfile->selfencryptionkeystr[selfencryptionkeystrlen] = '\0'; + + ret = 0; + goto exit; + +exit: { return ret; } +} \ No newline at end of file From 4b5c9f0cdeb9899f2aeaf4e5fe0dc4098748dba2 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:26:29 -0400 Subject: [PATCH 081/193] fix: --- packages/atclient/src/atkeys.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index db3bb6a5..c478be92 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -318,10 +318,10 @@ int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atcl int ret = 1; ret = atclient_atkeys_populate_from_strings( - atkeys, atkeysfile.aespkampublickeystr.str, atkeysfile.aespkampublickeystr.len, - atkeysfile.aespkamprivatekeystr.str, atkeysfile.aespkamprivatekeystr.len, atkeysfile.aesencryptpublickeystr.str, - atkeysfile.aesencryptpublickeystr.len, atkeysfile.aesencryptprivatekeystr.str, - atkeysfile.aesencryptprivatekeystr.len, atkeysfile.selfencryptionkeystr.str, atkeysfile.selfencryptionkeystr.len); + atkeys, atkeysfile.aespkampublickeystr, strlen(atkeysfile.aespkampublickeystr), + atkeysfile.aespkamprivatekeystr, strlen(atkeysfile.aespkamprivatekeystr), atkeysfile.aesencryptpublickeystr, + strlen(atkeysfile.aesencryptpublickeystr), atkeysfile.aesencryptprivatekeystr, + strlen(atkeysfile.aesencryptprivatekeystr), atkeysfile.selfencryptionkeystr, strlen(atkeysfile.selfencryptionkeystr)); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeys_populate_from_strings: %d | failed to populate from strings\n", ret); From 73d1a0d2e7fe47a8b654286d266010944346d00f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:27:40 -0400 Subject: [PATCH 082/193] docs: log messages --- packages/atclient/src/atkeys.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index c478be92..688e1981 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -177,7 +177,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 4a. self encryption key if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeystr, selfencryptionkeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set selfencryptionkeystr\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeystr\n", ret); goto exit; } @@ -197,7 +197,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_pkampublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set pkampublickeystr\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeystr\n", ret); goto exit; } @@ -220,7 +220,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_pkamprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set pkamprivatekeystr\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeystr\n", ret); goto exit; } @@ -243,7 +243,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_encryptpublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set encryptpublickeystr\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encryptpublickeybase64: %d | failed to set encryptpublickeystr\n", ret); goto exit; } @@ -267,7 +267,7 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_encryptprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set: %d | failed to set encryptprivatekeystr\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeystr\n", ret); goto exit; } From b22389feb566030d7ff252f04ef3b37a51b7be6f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:28:02 -0400 Subject: [PATCH 083/193] chore: delete unused atstr.h/.c and atbytes.h/.c --- packages/atclient/include/atclient/atbytes.h | 67 ---------- packages/atclient/include/atclient/atstr.h | 95 -------------- packages/atclient/src/atbytes.c | 62 ---------- packages/atclient/src/atstr.c | 123 ------------------- 4 files changed, 347 deletions(-) delete mode 100644 packages/atclient/include/atclient/atbytes.h delete mode 100644 packages/atclient/include/atclient/atstr.h delete mode 100644 packages/atclient/src/atbytes.c delete mode 100644 packages/atclient/src/atstr.c diff --git a/packages/atclient/include/atclient/atbytes.h b/packages/atclient/include/atclient/atbytes.h deleted file mode 100644 index ecbc16cc..00000000 --- a/packages/atclient/include/atclient/atbytes.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef ATCLIENT_ATBYTES_H -#define ATCLIENT_ATBYTES_H - -#include "atclient/atstr.h" -#include - -/** - * @brief Represents a buffer of bytes. Similar to atclient_atstr - */ -typedef struct atclient_atbytes { - size_t size; // the allocated length of the buffer - unsigned char *bytes; // the buffer of bytes (pointer to the first byte in the buffer on the heap) - size_t len; // the output length of the buffer -} atclient_atbytes; - -/** - * @brief Initializes atbytes on the heap - * - * @param atbytes the atbytes to initialize - * @param atbyteslen the buffer length to allocate on the heap - */ -void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen); - -/** - * @brief Reset atbytes to all zeroes - * - * @param atbytes the atbytes to reset - */ -void atclient_atbytes_reset(atclient_atbytes *atbytes); - -/** - * @brief Set atbytes to the given bytes - * - * @param atbytes the atbytes to set - * @param bytes the bytes to set - * @param byteslen the length of the bytes - * @return int 0 on success, non-zero on failure - */ -int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen); - -/** - * @brief Convert a string to atbytes - * - * @param atbytes the atbytes to convert to - * @param str the string to convert from - * @param strlen the length of the string - * @return int 0 on success - */ -int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen); - -/** - * @brief Converts an atstr to atbytes - * - * @param atbytes atbytes to write to - * @param atstr the atstr to copy from - * @return int 0 on success - */ -int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr); - -/** - * @brief Free atbytes on the heap. Should be called once atbytes is no longer needed - * - * @param atbytes the atbytes to free - */ -void atclient_atbytes_free(atclient_atbytes *atbytes); - -#endif diff --git a/packages/atclient/include/atclient/atstr.h b/packages/atclient/include/atclient/atstr.h deleted file mode 100644 index ddda4a55..00000000 --- a/packages/atclient/include/atclient/atstr.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef ATCLIENT_ATSTR_H -#define ATCLIENT_ATSTR_H - -#include - -/** - * @brief Represents a string that is allocated on the heap - */ -typedef struct atclient_atstr { - size_t size; // buffer length - char *str; // string - size_t len; // output length (length of string) -} atclient_atstr; - -/** - * @brief Initialize an atstr - * - * @param atstr pointer to atstr to initialize - * @param buffersize length of buffer to allocate in bytes (recommended to use a number that is a power of 2). the - * buffersize is the length of the buffer generated. we do not add 1 for null terminator. - */ -void atclient_atstr_init(atclient_atstr *atstr, const size_t buffersize); - -/** - * @brief initialize an atstr with a literal string - * - * @param atstr the atstr struct to populate - * @param str the string to set atstr to. best to use string literals here. (or a null-terminated string) - */ -int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t buffersize, const char *format, ...); - -/** - * @brief set an atstr to an empty string - * - * @param atstr the atstr struct to reset - */ -void atclient_atstr_reset(atclient_atstr *atstr); - -/** - * @brief set an atstr to a string - * - * @param atstr the atstr struct to populate - * @param str the string to set atstr to - * @param size the length of the string - */ -int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t size); - -/** - * @brief Set an atstr to a literal string, assumed to be null-terminated - * - * @param atstr atstr struct to populate - * @param str null-terminated string to set atstr to. Best to use string literals here - */ -int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...); - -/** - * @brief Copy what is in an atstr to another atstr - * - * @param atstr pointer that is being overwritten - * @param data pointer that is being copied from - */ -int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data); - -/** - * @brief Free an atstr - * - * @param atstr pointer to atstr to free from the heap - */ -void atclient_atstr_free(atclient_atstr *atstr); - -/** - * @brief Copy what is in original to substring and then set substring to a substring of original - * - * @param substring the atstr to set to the substring. Assumed that this is already initialized (via - * atclient_atstr_init) - * @param original the atstr to get the substring from. Assumed that this is already initialized (via - * atclient_atstr_init) - * @param start the start index of the substring - * @param end the end index of the substring - * @return int 0 on success, non-zero on failure - */ -int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, - const size_t end); - -/** - * @brief Append a string to an atstr - * - * @param atstr the atstr to append to - * @param format the format of the string to append - * @param ... the arguments to format - * @return int 0 on success - */ -int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...); - -#endif diff --git a/packages/atclient/src/atbytes.c b/packages/atclient/src/atbytes.c deleted file mode 100644 index bd39b74e..00000000 --- a/packages/atclient/src/atbytes.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "atclient/atbytes.h" -#include "atclient/atstr.h" -#include "atlogger/atlogger.h" -#include -#include -#include - -#define TAG "atbytes" - -void atclient_atbytes_init(atclient_atbytes *atbytes, const size_t atbyteslen) { - memset(atbytes, 0, sizeof(atclient_atbytes)); - atbytes->size = atbyteslen; - atbytes->bytes = malloc(sizeof(unsigned char) * atbytes->size); // TODO handle null - atbytes->len = 0; -} - -void atclient_atbytes_reset(atclient_atbytes *atbytes) { - memset(atbytes->bytes, 0, sizeof(unsigned char) * atbytes->size); - atbytes->len = 0; -} - -int atclient_atbytes_set(atclient_atbytes *atbytes, const unsigned char *bytes, const size_t byteslen) { - int ret = 1; - if (byteslen > atbytes->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "byteslen is greater than atbyteslen. byteslen: %lu, atbyteslen: %lu\n", byteslen, - atbytes->size); - ret = 1; - goto exit; - } - memcpy(atbytes->bytes, bytes, byteslen); - atbytes->len = byteslen; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atbytes_convert(atclient_atbytes *atbytes, const char *str, const size_t strlen) { - int ret = 1; - if (strlen > atbytes->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "strlen is greater than atbyteslen. strlen: %lu, atbyteslen: %lu\n", strlen, atbytes->size); - ret = 1; - goto exit; - } - memcpy(atbytes->bytes, (unsigned char *)str, strlen); - atbytes->len = strlen; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atbytes_convert_atstr(atclient_atbytes *atbytes, const atclient_atstr atstr) { - int ret = atclient_atbytes_convert(atbytes, atstr.str, atstr.len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atbytes_convert failed"); - goto exit; - } -exit: { return ret; } -} - -void atclient_atbytes_free(atclient_atbytes *atbytes) { free(atbytes->bytes); } diff --git a/packages/atclient/src/atstr.c b/packages/atclient/src/atstr.c deleted file mode 100644 index f4fc71ad..00000000 --- a/packages/atclient/src/atstr.c +++ /dev/null @@ -1,123 +0,0 @@ -#include "atclient/atstr.h" -#include "atlogger/atlogger.h" -#include -#include -#include -#include -#include - -#define TAG "atstr" - -void atclient_atstr_init(atclient_atstr *atstr, const size_t bufferlen) { - memset(atstr, 0, sizeof(atclient_atstr)); - atstr->size = bufferlen; - atstr->str = (char *)malloc(sizeof(char) * atstr->size); - memset(atstr->str, 0, sizeof(char) * atstr->size); - atstr->len = 0; -} - -int atclient_atstr_init_literal(atclient_atstr *atstr, const size_t bufferlen, const char *format, ...) { - int ret = 1; - atclient_atstr_init(atstr, bufferlen); - ret = atclient_atstr_set_literal(atstr, format); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set_literal failed"); - goto exit; - } - goto exit; -exit: { return ret; } -} - -void atclient_atstr_reset(atclient_atstr *atstr) { - memset(atstr->str, 0, atstr->size); - atstr->len = 0; -} - -int atclient_atstr_set_literal(atclient_atstr *atstr, const char *format, ...) { - int ret = 1; - if (atstr->str == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atstr->str is NULL\n"); - goto exit; - } - va_list args; - va_start(args, format); - ret = vsnprintf(atstr->str, atstr->size, format, args); - va_end(args); // Add va_end() to properly handle variadic arguments - if (ret < 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); - goto exit; - } - atstr->len = ret; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atstr_set(atclient_atstr *atstr, const char *str, const size_t len) { - int ret = 1; - - if (len > atstr->size) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "len > atstr->len (%d > %d)\n", len, atstr->size); - goto exit; - } - - memcpy(atstr->str, str, len); - atstr->len = len; - - ret = 0; - goto exit; - -exit: { return ret; } -} - -int atclient_atstr_copy(atclient_atstr *atstr, atclient_atstr *data) { - int ret = 1; - ret = atclient_atstr_set(atstr, data->str, data->len); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atstr_set failed"); - goto exit; - } - goto exit; -exit: { return ret; } -} - -int atclient_atstr_substring(atclient_atstr *substring, const atclient_atstr original, const size_t start, - const size_t end) { - int ret = 1; - if (start > original.len || end > original.len) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start or end is greater than original.len\n"); - goto exit; - } - if (start > end) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "start is greater than end\n"); - goto exit; - } - if (end - start > substring->size) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "end - start > substring->len\n"); - goto exit; - } - memcpy(substring->str, original.str + start, end - start); - substring->len = end - start; - ret = 0; - goto exit; - -exit: { return ret; } -} - -int atclient_atstr_append(atclient_atstr *atstr, const char *format, ...) { - int ret = 1; - va_list args; - va_start(args, format); - ret = vsnprintf(atstr->str + atstr->len, atstr->size - atstr->len, format, args); - if (ret < 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "vsnprintf failed"); - goto exit; - } - atstr->len += ret; - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_atstr_free(atclient_atstr *atstr) { free(atstr->str); } From a272abea06d06fa8c4beaa6d82d49e2eae4621b7 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:29:36 -0400 Subject: [PATCH 084/193] fix: remove atbytes.c and atstr.c from CMakeLists.txt --- packages/atclient/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index cfd2d8c5..895fa765 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -5,7 +5,6 @@ option(ATCLIENT_BUILD_TESTS "Build tests for atclient" OFF) set(ATCLIENT_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include) set( ATCLIENT_SOURCES - ${CMAKE_CURRENT_LIST_DIR}/src/atbytes.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_publickey.c @@ -18,7 +17,6 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c ${CMAKE_CURRENT_LIST_DIR}/src/atsign.c - ${CMAKE_CURRENT_LIST_DIR}/src/atstr.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c From 067bc36be383bd137e4605a4f0a043882353ab44 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:29:49 -0400 Subject: [PATCH 085/193] fix: atkey.c incorrect bool polarity --- packages/atclient/src/atkey.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 269dda4a..34b7cf57 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -292,9 +292,9 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); index_pos += strlen("public:"); } else if (atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - if (atclient_atkey_is_sharedwith_initialized(atkey)) { + if (!atclient_atkey_is_sharedwith_initialized(atkey)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "atkey's sharedwith is initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHAREDKEY by " + "atkey's sharedwith is not initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHAREDKEY by " "atclient_atkey_get_type\n"); } snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s:", atkey->sharedwith); From 0a56a239383433d4144d2f54cb929134da630bb5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:31:38 -0400 Subject: [PATCH 086/193] fix: remove atstr.h includes --- packages/atclient/include/atclient/atkey.h | 1 - packages/atclient/include/atclient/atkeys.h | 1 - packages/atclient/include/atclient/atkeysfile.h | 1 - packages/atclient/include/atclient/connection.h | 1 - packages/atclient/src/atclient.c | 1 - packages/atclient/src/atclient_delete.c | 1 - packages/atclient/src/atclient_put.c | 1 - packages/atclient/src/atkey.c | 1 - packages/atclient/src/atkeys.c | 1 - packages/atclient/src/atkeysfile.c | 1 - 10 files changed, 10 deletions(-) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index 3c4a7c71..cfc91ba1 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -1,7 +1,6 @@ #ifndef ATCLIENT_ATKEY_H #define ATCLIENT_ATKEY_H -#include "atclient/atstr.h" #include "atclient/metadata.h" #include #include diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 4474b4a6..6b3abfd5 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -2,7 +2,6 @@ #define ATCLIENT_ATKEYS_H #include "atclient/atkeysfile.h" -#include "atclient/atstr.h" #include #include #include diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h index 503e8157..7aae2911 100644 --- a/packages/atclient/include/atclient/atkeysfile.h +++ b/packages/atclient/include/atclient/atkeysfile.h @@ -1,7 +1,6 @@ #ifndef ATCLIENT_ATKEYSFILE_H #define ATCLIENT_ATKEYSFILE_H -#include "atclient/atstr.h" #include #include diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index 3de3a001..b05b8256 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -1,7 +1,6 @@ #ifndef ATCLIENT_CONNECTION_H #define ATCLIENT_CONNECTION_H -#include "atclient/atstr.h" #include #include #include diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 2cd67a54..90b54ffb 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -5,7 +5,6 @@ #include "atclient/atclient_utils.h" #include "atclient/atkeys.h" #include "atclient/atsign.h" -#include "atclient/atstr.h" #include "atclient/connection.h" #include "atclient/constants.h" #include "atclient/stringutils.h" diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 0b97a557..be7c98f9 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -1,6 +1,5 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" -#include "atclient/atstr.h" #include "atclient/constants.h" #include "atclient/stringutils.h" #include "atlogger/atlogger.h" diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index dec00bba..803dc703 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -1,6 +1,5 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" -#include "atclient/atstr.h" #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" #include "atclient/stringutils.h" diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 34b7cf57..73f164eb 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -1,6 +1,5 @@ #include "atclient/atkey.h" #include "atclient/atsign.h" -#include "atclient/atstr.h" #include "atclient/constants.h" #include "atclient/metadata.h" #include "atclient/stringutils.h" diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 688e1981..dd9c00fb 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -1,5 +1,4 @@ #include "atclient/atkeys.h" -#include "atclient/atstr.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c index 7be7bcf0..6e06bf9f 100644 --- a/packages/atclient/src/atkeysfile.c +++ b/packages/atclient/src/atkeysfile.c @@ -1,5 +1,4 @@ #include "atclient/atkeysfile.h" -#include "atclient/atstr.h" #include "atlogger/atlogger.h" #include #include From f9c14702f334b23f1e68e655d5cb57788b423a7b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:33:03 -0400 Subject: [PATCH 087/193] fix: --- examples/desktop/repl/src/main.c | 1 - packages/atclient/src/connection.c | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 0b73cdd8..7bd4f43a 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 45870792..1864e67f 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -1,6 +1,5 @@ #include "atclient/connection.h" #include "atchops/constants.h" -#include "atclient/atstr.h" #include "atclient/cacerts.h" #include "atclient/constants.h" #include "atlogger/atlogger.h" From 3ecc02c57fbaa196000192e3965433f017e927e0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:45:19 -0400 Subject: [PATCH 088/193] chore: format monitor.h --- packages/atclient/include/atclient/monitor.h | 49 ++++++++++---------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 75caa760..33bbd834 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -1,8 +1,31 @@ #ifndef NOTIFICATION_H #define NOTIFICATION_H -#include "atclient.h" -#include "atkey.h" +#include "atclient/atclient.h" +#include "atclient/atkey.h" +#include + +#define ATCLIENT_ATNOTIFICATION_INITIALIZED 0b00000001 + +// initializedfields[0] +#define ATCLIENT_ATNOTIFICATION_ID_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) +#define ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) +#define ATCLIENT_ATNOTIFICATION_TO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) +#define ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) +#define ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) +#define ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) +#define ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) +#define ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) + +// initializedfields[1] +#define ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) +#define ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) +#define ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) +#define ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) +#define ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) +#define ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) +#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) +#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) /** * @brief Represents a notification received from the monitor connection @@ -32,28 +55,6 @@ typedef struct atclient_atnotification { uint8_t initalizedfields[2]; } atclient_atnotification; -#define ATCLIENT_ATNOTIFICATION_INITIALIZED 0b00000001 - -// initializedfields[0] -#define ATCLIENT_ATNOTIFICATION_ID_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) -#define ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) -#define ATCLIENT_ATNOTIFICATION_TO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) -#define ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) -#define ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) -#define ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) -#define ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) -#define ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) - -// initializedfields[1] -#define ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) -#define ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) -#define ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) -#define ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) -#define ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) -#define ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) -#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) -#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) - /** * @brief Initializes the atnotification to a default state, ready for use in other functions. * From 4c91edb6a6909b2efd5ca0b144a1cb7ba61f6bfe Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:45:30 -0400 Subject: [PATCH 089/193] feat: atkeys.c/.h public setters --- packages/atclient/include/atclient/atkeys.h | 28 ++ packages/atclient/src/atkeys.c | 322 +++++++++++++++++++- 2 files changed, 339 insertions(+), 11 deletions(-) diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 6b3abfd5..83ce06a2 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -63,6 +63,34 @@ void atclient_atkeys_init(atclient_atkeys *atkeys); */ void atclient_atkeys_free(atclient_atkeys *atkeys); +int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeybase64len); + +int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeybase64len); + +int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeybase64len); + +int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeybase64len); + +int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, + const size_t selfencryptionkeybase64len); + +int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeybase64len); + +int atclient_atkeys_populate_pkamprivatekey(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeybase64len); + +int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeybase64len); + +int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeybase64len); + + /** * @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. diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index dd9c00fb..adff237e 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -55,6 +55,303 @@ void atclient_atkeys_free(atclient_atkeys *atkeys) { atchops_rsakey_privatekey_free(&(atkeys->encryptprivatekey)); } +int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (pkampublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64 is NULL\n"); + return ret; + } + + if (pkampublickeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64len is 0\n"); + return ret; + } + + if ((ret = set_pkampublickeybase64(atkeys, pkampublickeybase64, pkampublickeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeybase64\n", + ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (pkamprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64 is NULL\n"); + return ret; + } + + if (pkamprivatekeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64len is 0\n"); + return ret; + } + + if ((ret = set_pkamprivatekeybase64(atkeys, pkamprivatekeybase64, pkamprivatekeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeybase64\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (encryptpublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64 is NULL\n"); + return ret; + } + + if (encryptpublickeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64len is 0\n"); + return ret; + } + + if ((ret = set_encryptpublickeybase64(atkeys, encryptpublickeybase64, encryptpublickeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_encryptpublickeybase64: %d | failed to set encryptpublickeybase64\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (encryptprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64 is NULL\n"); + return ret; + } + + if (encryptprivatekeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64len is 0\n"); + return ret; + } + + if ((ret = set_encryptprivatekeybase64(atkeys, encryptprivatekeybase64, encryptprivatekeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeybase64\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, + const size_t selfencryptionkeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (selfencryptionkeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeybase64 is NULL\n"); + return ret; + } + + if (selfencryptionkeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeybase64len is 0\n"); + return ret; + } + + if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeybase64, selfencryptionkeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeybase64\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char *pkampublickeybase64, + const size_t pkampublickeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (pkampublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64 is NULL\n"); + return ret; + } + + if (pkampublickeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64len is 0\n"); + return ret; + } + + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkampublickey), pkampublickeybase64, pkampublickeybase64)) != + 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atchops_rsakey_populate_publickey: %d | failed to populate pkampublickey\n", ret); + goto exit; + } + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_populate_pkamprivatekey(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, + const size_t pkamprivatekeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (pkamprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64 is NULL\n"); + return ret; + } + + if (pkamprivatekeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64len is 0\n"); + return ret; + } + + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkamprivatekey), pkamprivatekeybase64, + pkamprivatekeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atchops_rsakey_populate_privatekey: %d | failed to populate pkamprivatekey\n", ret); + goto exit; + } +exit: { return ret; } +} + +int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const char *encryptpublickeybase64, + const size_t encryptpublickeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (encryptpublickeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64 is NULL\n"); + return ret; + } + + if (encryptpublickeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64len is 0\n"); + return ret; + } + + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encryptpublickey), encryptpublickeybase64, + encryptpublickeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atchops_rsakey_populate_publickey: %d | failed to populate encryptpublickey\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, + const size_t encryptprivatekeybase64len) { + int ret = 1; + + if (atkeys == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeys is NULL\n"); + return ret; + } + + if (encryptprivatekeybase64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64 is NULL\n"); + return ret; + } + + if (encryptprivatekeybase64len == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64len is 0\n"); + return ret; + } + + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encryptprivatekey), encryptprivatekeybase64, + encryptprivatekeybase64len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atchops_rsakey_populate_privatekey: %d | failed to populate encryptprivatekey\n", ret); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, const size_t aespkampublickeylen, const char *aespkamprivatekeystr, const size_t aespkamprivatekeylen, const char *aesencryptpublickeystr, @@ -176,8 +473,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 4a. self encryption key if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeystr, selfencryptionkeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeystr\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeystr\n", ret); goto exit; } @@ -196,7 +493,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_pkampublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeystr\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeystr\n", + ret); goto exit; } @@ -219,7 +517,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_pkamprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeystr\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeystr\n", + ret); goto exit; } @@ -242,8 +541,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_encryptpublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encryptpublickeybase64: %d | failed to set encryptpublickeystr\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_encryptpublickeybase64: %d | failed to set encryptpublickeystr\n", ret); goto exit; } @@ -266,8 +565,8 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a } if ((ret = set_encryptprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeystr\n", - ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeystr\n", ret); goto exit; } @@ -317,10 +616,11 @@ int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atcl int ret = 1; ret = atclient_atkeys_populate_from_strings( - atkeys, atkeysfile.aespkampublickeystr, strlen(atkeysfile.aespkampublickeystr), - atkeysfile.aespkamprivatekeystr, strlen(atkeysfile.aespkamprivatekeystr), atkeysfile.aesencryptpublickeystr, + atkeys, atkeysfile.aespkampublickeystr, strlen(atkeysfile.aespkampublickeystr), atkeysfile.aespkamprivatekeystr, + strlen(atkeysfile.aespkamprivatekeystr), atkeysfile.aesencryptpublickeystr, strlen(atkeysfile.aesencryptpublickeystr), atkeysfile.aesencryptprivatekeystr, - strlen(atkeysfile.aesencryptprivatekeystr), atkeysfile.selfencryptionkeystr, strlen(atkeysfile.selfencryptionkeystr)); + strlen(atkeysfile.aesencryptprivatekeystr), atkeysfile.selfencryptionkeystr, + strlen(atkeysfile.selfencryptionkeystr)); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkeys_populate_from_strings: %d | failed to populate from strings\n", ret); From a4760e75354504d58ea9f683b6e415f768d59721 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Sat, 20 Jul 2024 14:52:48 -0400 Subject: [PATCH 090/193] feat: more refactors and public facing bool checker functions --- packages/atclient/include/atclient/atkeys.h | 27 ++++++---- .../atclient/include/atclient/atkeysfile.h | 20 ++++---- .../include/atclient/encryption_key_helpers.h | 2 - packages/atclient/src/atkeys.c | 50 +++++++++++++------ packages/atclient/src/atkeysfile.c | 30 +++++------ .../tests/test_atclient_get_atkeys.c | 2 +- 6 files changed, 77 insertions(+), 54 deletions(-) diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 83ce06a2..3592617e 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -8,17 +8,17 @@ #define VALUE_INITIALIZED 0b00000001 -#define PKAMPUBLICKEY_INDEX 0 -#define PKAMPRIVATEKEY_INDEX 0 -#define ENCRYPTPUBLICKEY_INDEX 0 -#define ENCRYPTPRIVATEKEY_INDEX 0 -#define SELFENCRYPTIONKEY_INDEX 0 - -#define PKAMPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 0) -#define PKAMPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 1) -#define ENCRYPTPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 2) -#define ENCRYPTPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 3) -#define SELFENCRYPTIONKEY_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX 0 +#define ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX 0 +#define ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX 0 +#define ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX 0 +#define ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX 0 + +#define ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED (VALUE_INITIALIZED << 4) /** * @brief represents the atkeys file @@ -90,6 +90,11 @@ int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const cha int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, const size_t encryptprivatekeybase64len); +bool atclient_atkeys_is_pkampublickeybase64_initialized(atclient_atkeys *atkeys); +bool atclient_atkeys_is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys); +bool atclient_atkeys_is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys); +bool atclient_atkeys_is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys); +bool atclient_atkeys_is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys); /** * @brief populates the struct by decrypting the encrypted RSA keys passed. It is assumed that the passed strings are diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h index 7aae2911..74dda9d3 100644 --- a/packages/atclient/include/atclient/atkeysfile.h +++ b/packages/atclient/include/atclient/atkeysfile.h @@ -6,17 +6,17 @@ #define VALUE_INITIALIZED 0b00000001 -#define AESPKAMPUBLICKEYSTR_INDEX 0 -#define AESPKAMPRIVATEKEYSTR_INDEX 0 -#define AESENCRYPTPUBLICKEYSTR_INDEX 0 -#define AESENCRYPTPRIVATEKEYSTR_INDEX 0 -#define SELFENCRYPTIONKEYSTR_INDEX 0 +#define ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX 0 +#define ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX 0 +#define ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX 0 +#define ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX 0 +#define ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX 0 -#define AESPKAMPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 0) -#define AESPKAMPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 1) -#define AESENCRYPTPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 2) -#define AESENCRYPTPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 3) -#define SELFENCRYPTIONKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 4) typedef struct atclient_atkeysfile { char *aespkampublickeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key diff --git a/packages/atclient/include/atclient/encryption_key_helpers.h b/packages/atclient/include/atclient/encryption_key_helpers.h index 11691327..bc9baace 100644 --- a/packages/atclient/include/atclient/encryption_key_helpers.h +++ b/packages/atclient/include/atclient/encryption_key_helpers.h @@ -1,11 +1,9 @@ #ifndef ATCLIENT_ENCRYPTION_KEY_HELPERS_H #define ATCLIENT_ENCRYPTION_KEY_HELPERS_H - #include "atclient/atclient.h" #include "atclient/atkey.h" #include "atclient/atkeys.h" -#include "atlogger/atlogger.h" /** * @brief Looks up the symmetric shared key which the atclient's atsign shared with the recipient's atsign. diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index adff237e..3d504d30 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -352,6 +352,26 @@ int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const ch exit: { return ret; } } +bool atclient_atkeys_is_pkampublickeybase64_initialized(atclient_atkeys *atkeys) { + return is_pkampublickeybase64_initialized(atkeys); +} + +bool atclient_atkeys_is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys) { + return is_pkamprivatekeybase64_initialized(atkeys); +} + +bool atclient_atkeys_is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys) { + return is_encryptpublickeybase64_initialized(atkeys); +} + +bool atclient_atkeys_is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys) { + return is_encryptprivatekeybase64_initialized(atkeys); +} + +bool atclient_atkeys_is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys) { + return is_selfencryptionkeybase64_initialized(atkeys); +} + int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, const size_t aespkampublickeylen, const char *aespkamprivatekeystr, const size_t aespkamprivatekeylen, const char *aesencryptpublickeystr, @@ -660,62 +680,62 @@ exit: { } static bool is_pkampublickeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] & PKAMPUBLICKEY_INITIALIZED; + return atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] & ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; } static bool is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] & PKAMPRIVATEKEY_INITIALIZED; + return atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] & ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED; } static bool is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] & ENCRYPTPUBLICKEY_INITIALIZED; + return atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED; } static bool is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] & ENCRYPTPRIVATEKEY_INITIALIZED; + return atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED; } static bool is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] & SELFENCRYPTIONKEY_INITIALIZED; + return atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] & ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED; } static void set_pkampublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] |= PKAMPUBLICKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] |= ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; } else { - atkeys->_initializedfields[PKAMPUBLICKEY_INDEX] &= ~PKAMPUBLICKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; } } static void set_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] |= PKAMPRIVATEKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] |= ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED; } else { - atkeys->_initializedfields[PKAMPRIVATEKEY_INDEX] &= ~PKAMPRIVATEKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED; } } static void set_encryptpublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] |= ENCRYPTPUBLICKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED; } else { - atkeys->_initializedfields[ENCRYPTPUBLICKEY_INDEX] &= ~ENCRYPTPUBLICKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED; } } static void set_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] |= ENCRYPTPRIVATEKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED; } else { - atkeys->_initializedfields[ENCRYPTPRIVATEKEY_INDEX] &= ~ENCRYPTPRIVATEKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED; } } static void set_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] |= SELFENCRYPTIONKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] |= ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED; } else { - atkeys->_initializedfields[SELFENCRYPTIONKEY_INDEX] &= ~SELFENCRYPTIONKEY_INITIALIZED; + atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] &= ~ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED; } } diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c index 6e06bf9f..d9b82571 100644 --- a/packages/atclient/src/atkeysfile.c +++ b/packages/atclient/src/atkeysfile.c @@ -156,62 +156,62 @@ void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile) { } static bool is_aespkampublickeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] & AESPKAMPUBLICKEYSTR_INTIIALIZED; + return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; } static bool is_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] & AESPKAMPRIVATEKEYSTR_INTIIALIZED; + return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; } static bool is_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] & AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; } static bool is_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] & AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; } static bool is_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] & SELFENCRYPTIONKEYSTR_INTIIALIZED; + return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; } static void set_aespkampublickestr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] |= AESPKAMPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; } else { - atkeysfile->_initializedfields[AESPKAMPUBLICKEYSTR_INDEX] &= ~AESPKAMPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; } } static void set_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] |= AESPKAMPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; } else { - atkeysfile->_initializedfields[AESPKAMPRIVATEKEYSTR_INDEX] &= ~AESPKAMPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; } } static void set_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] |= AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; } else { - atkeysfile->_initializedfields[AESENCRYPTPUBLICKEYSTR_INDEX] &= ~AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; } } static void set_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] |= AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; } else { - atkeysfile->_initializedfields[AESENCRYPTPRIVATEKEYSTR_INDEX] &= ~AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; } } static void set_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] |= SELFENCRYPTIONKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; } else { - atkeysfile->_initializedfields[SELFENCRYPTIONKEYSTR_INDEX] &= ~SELFENCRYPTIONKEYSTR_INTIIALIZED; + atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; } } diff --git a/tests/functional_tests/tests/test_atclient_get_atkeys.c b/tests/functional_tests/tests/test_atclient_get_atkeys.c index 99ca9122..12d16829 100644 --- a/tests/functional_tests/tests/test_atclient_get_atkeys.c +++ b/tests/functional_tests/tests/test_atclient_get_atkeys.c @@ -24,7 +24,7 @@ int main() { atclient atclient1; atclient_init(&atclient1); - atclient atkeys1; + atclient_atkeys atkeys1; atclient_atkeys_init(&atkeys1); if ((ret = functional_tests_set_up_atkeys(&atkeys1, ATSIGN1, strlen(ATSIGN1))) != 0) { From 293a8f93a0a5deac07287e6407cc22a0b281c28c Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 09:31:37 -0400 Subject: [PATCH 091/193] chore: clean up test_atkey_from_string.c --- packages/atclient/tests/test_atkey_from_string.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/atclient/tests/test_atkey_from_string.c b/packages/atclient/tests/test_atkey_from_string.c index b231fbe4..97b2f29e 100644 --- a/packages/atclient/tests/test_atkey_from_string.c +++ b/packages/atclient/tests/test_atkey_from_string.c @@ -134,23 +134,21 @@ static int test1a_cached_publickey_without_namespace() { int ret = 1; const char *atkeystr = TEST_ATKEY_FROM_STRING_1A; - const size_t atkeystrlen = strlen(atkeystr); atclient_atkey atkey; atclient_atkey_init(&atkey); - ret = atclient_atkey_from_string(&atkey, atkeystr); - if (ret != 0) { + if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n"); goto exit; } - if (atkey.metadata.iscached != 1) { + if (atkey.metadata.iscached != true) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); goto exit; } - if (atkey.metadata.ispublic != 1) { + if (atkey.metadata.ispublic != true) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", atkey.metadata.ispublic); goto exit; @@ -201,7 +199,7 @@ static int test1b_publickey_without_namespace() { goto exit; } - if (atkey.metadata.ispublic != 1) { + if (atkey.metadata.ispublic != true) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", atkey.metadata.ispublic); ret = 1; @@ -784,7 +782,6 @@ static int test2e_sharedkey_with_compounding_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); From 8a3e63c45286e13ce1bd8275fd91211825f0af83 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 12:40:30 -0400 Subject: [PATCH 092/193] feat: remove atclient_atsign usages --- examples/desktop/crud/delete.c | 10 +- examples/desktop/crud/get_publickey.c | 8 +- examples/desktop/crud/get_selfkey.c | 12 +- examples/desktop/crud/get_sharedkey.c | 11 +- examples/desktop/crud/put_publickey.c | 6 +- examples/desktop/crud/put_selfkey.c | 6 +- examples/desktop/crud/put_sharedkey.c | 8 +- examples/desktop/events/notify.c | 13 +- examples/desktop/pkam_authenticate/src/main.c | 5 +- packages/atclient/include/atclient/atclient.h | 82 ++++++++- .../atclient/include/atclient/constants.h | 2 + .../include/atclient/encryption_key_helpers.h | 27 ++- packages/atclient/src/atclient.c | 146 +++++++++++----- packages/atclient/src/atclient_get_atkeys.c | 12 +- .../atclient/src/atclient_get_publickey.c | 12 ++ packages/atclient/src/atclient_get_selfkey.c | 8 +- .../atclient/src/atclient_get_sharedkey.c | 71 +++++--- packages/atclient/src/atclient_put.c | 79 ++++----- .../atclient/src/encryption_key_helpers.c | 160 ++++++++++++++---- packages/atclient/src/notify.c | 15 +- tests/functional_tests/lib/src/helpers.c | 34 +++- .../tests/test_atclient_monitor.c | 2 +- 22 files changed, 495 insertions(+), 234 deletions(-) diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index 3d75c115..235ad476 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -29,12 +29,11 @@ int main() { int atserver_port = -1; atclient atclient; - atclient_atsign atsign; atclient_atkey atkey; atclient_atkeys atkeys; + const char *atsign = ATSIGN; atclient_init(&atclient); - atclient_atsign_init(&atsign, ATSIGN); atclient_atkey_init(&atkey); atclient_atkeys_init(&atkeys); @@ -43,18 +42,18 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign.atsign, ATKEY_NAME)) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign, ATKEY_NAME)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create selfkey"); goto exit; } - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate"); goto exit; } @@ -69,7 +68,6 @@ int main() { exit: { atclient_free(&atclient); free(atserver_host); - atclient_atsign_free(&atsign); atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); return ret; diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index fc02af24..acea2f6b 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -37,8 +37,7 @@ int main() { atclient atclient; atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -54,13 +53,13 @@ int main() { char *metadatajsonstr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate"); goto exit; } @@ -98,7 +97,6 @@ int main() { exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index acdc36db..682fd922 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -39,8 +39,7 @@ int main() { atclient atclient; atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -51,22 +50,20 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); goto exit; } else { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Successfully authenticated!\n"); } - atclient.atkeys = atkeys; - atclient.atsign = atsign; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign.atsign, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } else { @@ -95,7 +92,6 @@ int main() { exit: { atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 24d760ee..ad7d4179 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -53,8 +53,7 @@ int main() { atclient atclient; atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATCLIENT_ATSIGN); + const char *atsign = ATCLIENT_ATSIGN; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -65,22 +64,19 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, ATCLIENT_ATSIGN)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); goto exit; } else { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n"); } - atclient.atkeys = atkeys; - atclient.atsign = atsign; - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, SENDER_ATSIGN, RECIPIENT_ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create shared key\n"); @@ -135,7 +131,6 @@ int main() { exit: { atclient_atkey_free(&atkey); atclient_atkeys_free(&atkeys); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index f8ea123f..bd90a50a 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -39,8 +39,7 @@ int main() { char *atserver_host = NULL; int atserver_port = -1; - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -51,7 +50,7 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; @@ -97,7 +96,6 @@ int main() { exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index ad150061..c5d161d5 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -39,8 +39,7 @@ int main() { char *atserver_host = NULL; int atserver_port = -1; - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -51,7 +50,7 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; @@ -95,7 +94,6 @@ int main() { exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index 5a74474e..ad4c784f 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -37,8 +37,7 @@ int main() { atclient atclient; atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; char *atserver_host = NULL; int atserver_port = -1; @@ -52,13 +51,13 @@ int main() { char *atkeystr = NULL; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address"); goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate"); goto exit; } @@ -91,7 +90,6 @@ int main() { exit: { atclient_atkeys_free(&atkeys); atclient_atkey_free(&atkey); - atclient_atsign_free(&atsign); atclient_free(&atclient); free(atserver_host); free(atkeystr); diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index f3203254..d490207d 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -23,6 +23,8 @@ int main(int argc, char *argv[]) { int ret = 1; atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + const char *atsign = "@soccer0"; + const size_t valuelen = 1024; char value[valuelen]; memset(value, 0, sizeof(char) * valuelen); @@ -74,14 +76,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atsign_input: %s\n", atsign_input); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "other_atsign_input: %s\n", other_atsign_input); - atclient_atsign atsign; - ret = atclient_atsign_init(&atsign, atsign_input); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to initialize atsign\n"); - goto exit; - } - - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address\n"); goto exit; @@ -101,7 +96,7 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign.atsign)) != 0) { + if ((ret = atclient_pkam_authenticate(&atclient, atserver_host, atserver_port, &atkeys, atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n"); goto exit; } diff --git a/examples/desktop/pkam_authenticate/src/main.c b/examples/desktop/pkam_authenticate/src/main.c index bee3caf4..da29bb58 100644 --- a/examples/desktop/pkam_authenticate/src/main.c +++ b/examples/desktop/pkam_authenticate/src/main.c @@ -44,10 +44,9 @@ int main(int argc, char **argv) { atclient atclient; atclient_init(&atclient); - atclient_atsign atsign; - atclient_atsign_init(&atsign, ATSIGN); + const char *atsign = ATSIGN; - if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign.atsign, &atserver_host, &atserver_port)) != + if ((ret = atclient_utils_find_atserver_address(ROOT_HOST, ROOT_PORT, atsign, &atserver_host, &atserver_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to find atserver address\n"); goto exit; diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index b41d8e57..490289c0 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -8,22 +8,37 @@ #include #include +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_ATSIGN_INDEX 0 +#define ATCLIENT_ATSERVER_CONNECTION_INDEX 0 +#define ATCLIENT_ATKEYS_INDEX 0 + +#define ATCLIENT_ATSIGN_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATSERVER_CONNECTION_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEYS_INITIALIZED (VALUE_INITIALIZED << 2) + /** * @brief represents atclient - * + * TODO: more documentation */ typedef struct atclient { - atclient_connection atserver_connection; - bool _atserver_connection_started : 1; // internally used for memory management - - atclient_atsign atsign; - bool _atsign_is_allocated : 1; // internally used for memory management + char *atsign; + atclient_connection atserver_connection; atclient_atkeys atkeys; // Warning! async_read is an experimental feature and not fully implemented. // You should leave this set to false unless you know what you are doing. bool async_read; + + // used internally to track which fields are ready for use + // bit 0 == atsign | true == allocated and populated, false == not allocated. + // bit 1 == atserver_connection | true == connection is expected to be fully functional, false == connection was not + // started and is expected to be non-functional. + // bit 2 == atkeys | true == atkeys are populated with the necessary + // keys, false == atkeys are not populated. + uint8_t _initializedfields[1]; } atclient; /** @@ -41,6 +56,61 @@ void atclient_init(atclient *ctx); */ void atclient_free(atclient *ctx); +/** + * @brief returns true if the atsign is initialized and is ready for use and is allocated + * + * @param ctx assumed to be initialized using atclient_init + * @return true if the atsign is initialized and is ready for use and is allocated + * @return false if the atsign is not initialized and is not ready for use and is not allocated + */ +bool atclient_is_atsign_initialized(const atclient *ctx); + +/** + * @brief sets the atsign in the atclient context. This function will allocate memory for the atsign and copy the input + * atsign + * + * @param ctx the atclient context to set the atsign in, assumed to be initialized using atclient_init and non-null + * @param atsign the atsign to set in the atclient context, assumed to be NON-NULL + * @return int 0 on success + */ +int atclient_set_atsign(atclient *ctx, const char *atsign); + +/** + * @brief unsets the atsign in the atclient context. This function will free the memory allocated for the atsign + * + * @param ctx the atclient context to unset the atsign in, assumed to be initialized using atclient_init and non-null + */ +void atclient_unset_atsign(atclient *ctx); + +/** + * @brief check if the atserver connection was started and is ready for use. This function will return true if the + * connection is expected to be fully functional, it doesn't necessarily mean that the connection is still alive and + * connected. + * + * @param ctx the atclient context, assumed to be initialized using atclient_init and non-null + * @return true if the connection is expected to be fully functional and was started at least once + * @return false if connection was not started and is expected to be non-functional + */ +bool atclient_is_atserver_connection_started(const atclient *ctx); + +/** + * @brief starts the atserver connection. This function will connect to the atserver host and port. This function will + * not pkam authenticate for you + * + * @param ctx the atclient context, assumed to be initialized using atclient_init + * @param secondaryhost the host of the atserver, this string is assumed to be non-null null terminated + * @param secondaryport the port of the atserver + * @return int + */ +int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport); + +/** + * @brief Stops the atserver connection. This function will disconnect the atserver connection. + * + * @param ctx assumed to be initialized using atclient_init and non-null + */ +void atclient_stop_atserver_connection(atclient *ctx); + /** * @brief authenticate with secondary server with RSA pkam private key. it is expected atkeys has been populated with * the pkam private key and atclient context is connected to the root server diff --git a/packages/atclient/include/atclient/constants.h b/packages/atclient/include/atclient/constants.h index 277548c5..6105056f 100644 --- a/packages/atclient/include/atclient/constants.h +++ b/packages/atclient/include/atclient/constants.h @@ -22,6 +22,8 @@ #define ATCLIENT_MONITOR_BUFFER_LEN 4096 // max chunk size monitor can read at once +#define ATCLIENT_ERR_AT0015_KEY_NOT_FOUND -0x1980 + #define BLK "\e[0;30m" #define RED "\e[0;31m" #define GRN "\e[0;32m" diff --git a/packages/atclient/include/atclient/encryption_key_helpers.h b/packages/atclient/include/atclient/encryption_key_helpers.h index bc9baace..fb249df6 100644 --- a/packages/atclient/include/atclient/encryption_key_helpers.h +++ b/packages/atclient/include/atclient/encryption_key_helpers.h @@ -11,13 +11,13 @@ * recipient's atsign. * * @param ctx Initialized atclient context (required) - * @param recipient An atclient_atsign struct corresponding to the atsign with whom the key was shared (required) + * @param recipient atsign of the recipient with @ symbol (required) * @param enc_key_shared_by_me The output shared key in b64 format (required) * @param create_new_if_not_found true if in case the symmetric shared key does not exist, you would like it to be * created / false if not (required) * @return int 0 on success, error otherwise */ -int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclient_atsign *recipient, +int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *recipient_atsign, char *enc_key_shared_by_me, bool create_new_if_not_found); /** @@ -26,25 +26,22 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien * * @param ctx Initialized atclient context (required) * @param root_conn initialized root connection - * @param recipient An atclient_atsign struct corresponding to the atsign who shared the key with the atclient’s atsign - * (required) + * @param recipient the atsign of the recipient with @ symbol (required) * @param enc_key_shared_by_other the output shared key in b64 format (required) * @return int 0 on success, error otherwise */ -int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atclient_atsign *recipient, +int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char *recipient, char *enc_key_shared_by_other); /** * @brief Retreives the public encryption key of a given atsign. * * @param ctx Initialized atclient context (required) - * @param recipient An atclient_atsign struct corresponding to the atsign which public encryption key you would like to - * obtain. It may receive a NULL value, in which case, the atclient_atsign contained in the ctx parameter will be used - * (required) + * @param recipient the atsign of the recipient with @ symbol (required) * @param public_encryption_key The output public key in b64 format (required) * @return int 0 on success, error otherwise */ -int atclient_get_public_encryption_key(atclient *ctx, const atclient_atsign *atsign, +int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char *public_encryption_key); /** @@ -53,13 +50,13 @@ int atclient_get_public_encryption_key(atclient *ctx, const atclient_atsign *ats * (shared_key.other@me and @other:shared_key@me) * * @param atclient the atclient context (must be initialized and pkam_authenticated) - * @param sharedby - * @param sharedwith - * @param sharedenckeybyme - * @return int + * @param sharedby TODO: documentation + * @param sharedwith TODO: documentation + * @param sharedenckeybyme TODO: documentation + * @return int 0 on success, error otherwise */ -int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const atclient_atsign *sharedby, - const atclient_atsign *sharedwith, +int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const char *sharedby, + const char *sharedwith, char *sharedenckeybyme); #endif // ATCLIENT_ENCRYPTION_KEY_HELPERS_H diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 90b54ffb..a1886242 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -21,26 +21,102 @@ #define TAG "atclient" -static int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport); +static void atclient_set_atsign_initialized(atclient *ctx, const bool initialized); +static void atclient_set_atserver_connection_started(atclient *ctx, const bool started); static int atclient_pkam_authenticate_validate_arguments(const atclient *ctx, const char *atserver_host, const int atserver_port, const atclient_atkeys *atkeys, const char *atsign); void atclient_init(atclient *ctx) { memset(ctx, 0, sizeof(atclient)); - ctx->async_read = false; - ctx->_atserver_connection_started = false; - ctx->_atsign_is_allocated = false; } void atclient_free(atclient *ctx) { - if (ctx->_atserver_connection_started) { - atclient_connection_free(&(ctx->atserver_connection)); + if (atclient_is_atsign_initialized(ctx)) { + atclient_unset_atsign(ctx); + } + + if(atclient_is_atserver_connection_started(ctx)) { + atclient_stop_atserver_connection(ctx); + } +} + +int atclient_set_atsign(atclient *ctx, const char *atsign) { + int ret = 1; + + if (atclient_is_atsign_initialized(ctx)) { + atclient_unset_atsign(ctx); + } + + const size_t atsignlen = strlen(atsign); + const size_t atsignsize = atsignlen + 1; + if ((ctx->atsign = malloc(sizeof(char) * atsignsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for atsign\n"); + goto exit; + } + + memcpy(ctx->atsign, atsign, atsignlen); + ctx->atsign[atsignlen] = '\0'; + + atclient_set_atsign_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_unset_atsign(atclient *ctx) { + if (atclient_is_atsign_initialized(ctx)) { + free(ctx->atsign); + } + ctx->atsign = NULL; + atclient_set_atsign_initialized(ctx, false); +} + +bool atclient_is_atserver_connection_started(const atclient *ctx) { + return ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] & ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; +} + +int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport) { + int ret = 1; // error by default + + // remove hooks to preserve them across resets + atclient_connection_hooks *conn_hooks = ctx->atserver_connection.hooks; + ctx->atserver_connection.hooks = NULL; + + // clear the atserver connection + atclient_stop_atserver_connection(ctx); + + // (re) initialize the atserver connection + atclient_connection_init(&(ctx->atserver_connection), ATCLIENT_CONNECTION_TYPE_ATSERVER); + + // add back hooks + ctx->atserver_connection.hooks = conn_hooks; + + if ((ret = atclient_connection_connect(&(ctx->atserver_connection), secondaryhost, secondaryport)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_connect: %d\n", ret); + goto exit; } - if (ctx->_atsign_is_allocated) { - atclient_atsign_free(&(ctx->atsign)); + atclient_set_atserver_connection_started(ctx, true); + + ret = 0; + goto exit; + +exit: { return ret; } +} + +void atclient_stop_atserver_connection(atclient *ctx) { + if (atclient_is_atserver_connection_started(ctx)) { + atclient_connection_free(&(ctx->atserver_connection)); } + memset(&(ctx->atserver_connection), 0, sizeof(atclient_connection)); + atclient_set_atserver_connection_started(ctx, false); +} + +bool atclient_is_atsign_initialized(const atclient *ctx) { + return ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] & ATCLIENT_ATSIGN_INITIALIZED; } int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const int atserver_port, @@ -127,9 +203,10 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } - if(!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recvlen, recv); goto exit; } @@ -190,15 +267,10 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i */ // initialize ctx->atsign.atsign and ctx->atsign.withour_prefix_str to the newly authenticated atSign - if (ctx->_atsign_is_allocated) { - atclient_atsign_free(&(ctx->atsign)); - ctx->_atsign_is_allocated = false; - } - if ((ret = atclient_atsign_init(&(ctx->atsign), atsign_with_at) != 0)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init: %d\n", ret); + if((ret = atclient_set_atsign(ctx, atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_set_atsign: %d\n", ret); goto exit; } - ctx->_atsign_is_allocated = true; // set atkeys ctx->atkeys = *atkeys; @@ -269,38 +341,20 @@ void atclient_set_read_timeout(atclient *ctx, int timeout_ms) { mbedtls_ssl_conf_read_timeout(&(ctx->atserver_connection.ssl_config), timeout_ms); } -static int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport) { - int ret = 1; // error by default - - if (ctx == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); - goto exit; +static void atclient_set_atsign_initialized(atclient *ctx, const bool initialized) { + if (initialized) { + ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] |= ATCLIENT_ATSIGN_INITIALIZED; + } else { + ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] &= ~ATCLIENT_ATSIGN_INITIALIZED; } +} - // remove hooks to preserve them across resets - atclient_connection_hooks *conn_hooks = ctx->atserver_connection.hooks; - ctx->atserver_connection.hooks = NULL; - - // clear the atserver connection - atclient_connection_free(&(ctx->atserver_connection)); - ctx->_atserver_connection_started = false; - memset(&(ctx->atserver_connection), 0, sizeof(atclient_connection)); - - // (re) initialize the atserver connection - atclient_connection_init(&(ctx->atserver_connection), ATCLIENT_CONNECTION_TYPE_ATSERVER); - ctx->_atserver_connection_started = true; - - // add back hooks - ctx->atserver_connection.hooks = conn_hooks; - - if ((ret = atclient_connection_connect(&(ctx->atserver_connection), secondaryhost, secondaryport)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_connect: %d\n", ret); - goto exit; +static void atclient_set_atserver_connection_started(atclient *ctx, const bool started) { + if (started) { + ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] |= ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; + } else { + ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] &= ~ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; } - - goto exit; - -exit: { return ret; } } static int atclient_pkam_authenticate_validate_arguments(const atclient *ctx, const char *atserver_host, diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index b00305fd..cca5f749 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -7,8 +7,8 @@ #define TAG "atclient_get_atkeys" -static int atclient_get_atkeys_validate_arguments(atclient *atclient, const char *regex, const bool showhidden, - atclient_atkey **atkey, size_t *output_array_len); +static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const char *regex, const bool showhidden, + const atclient_atkey **atkey, const size_t *output_array_len); int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhidden, const size_t recvbuffersize, atclient_atkey **atkey, size_t *output_array_len) @@ -131,8 +131,8 @@ exit: { } } -static int atclient_get_atkeys_validate_arguments(atclient *atclient, const char *regex, const bool showhidden, - atclient_atkey **atkey, size_t *output_array_len) { +static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const char *regex, const bool showhidden, + const atclient_atkey **atkey, const size_t *output_array_len) { int ret = 1; // check to make sure null ptr wasn't provided @@ -149,13 +149,13 @@ static int atclient_get_atkeys_validate_arguments(atclient *atclient, const char goto exit; } - if (!atclient->_atserver_connection_started) { + if (!atclient_is_atserver_connection_started(atclient)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); goto exit; } - if (!atclient->_atsign_is_allocated) { + if (!atclient_is_atsign_initialized(atclient)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not allocated. Make sure to PKAM authenticate first\n"); goto exit; diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index b15c297a..d95da6cf 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -181,6 +181,18 @@ static int atclient_get_publickey_validate_arguments(atclient *atclient, atclien goto exit; } + if(!atclient_is_atserver_connection_started(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + goto exit; + } + + if(!atclient_is_atsign_initialized(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign not initialized\n"); + goto exit; + } + ret = 0; goto exit; exit: { return ret; } diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index aa279644..98a013be 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -199,15 +199,15 @@ static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const goto exit; } - if (!atclient->_atsign_is_allocated) { + if (!atclient_is_atsign_initialized(atclient)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient->_atsign_is_allocated is false\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized is false\n"); goto exit; } - if (!atclient->_atserver_connection_started) { + if (!atclient_is_atserver_connection_started(atclient)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atserver_connection_started is false\n"); goto exit; } diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index f81c03ee..fa17de24 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -12,8 +12,9 @@ #define TAG "atclient_get_sharedkey" -static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, char *shared_enc_key) { +static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, + const char *value, const size_t valuesize, const size_t *valuelen, + const char *shared_enc_key) { int ret = 1; if (atclient == NULL) { @@ -22,15 +23,15 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, c goto exit; } - if (!atclient->_atserver_connection_started) { + if (!atclient_is_atserver_connection_started(atclient)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection was not started\n"); goto exit; } - if (!atclient->_atsign_is_allocated) { + if (!atclient_is_atsign_initialized(atclient)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient->_atsign_is_allocated is false\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized was not set\n"); goto exit; } @@ -114,7 +115,14 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu return ret; } - if (strcmp(atkey->sharedby, atclient->atsign.atsign) != 0) { + char *client_atsign_with_at = NULL; + + if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if (strcmp(atkey->sharedby, client_atsign_with_at) != 0) { // && (!atkey->metadata.iscached && !atkey->metadata.ispublic) ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key); if (ret != 0) { @@ -131,7 +139,10 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu } goto exit; -exit: { return ret; } +exit: { + free(client_atsign_with_at); + return ret; +} } static int @@ -145,6 +156,8 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke char *command = NULL; char *response_prefix = NULL; unsigned char *recv = NULL; + char *client_atsign_with_at = NULL; + char *client_atsign_without_at = NULL; const size_t enckeysize = ATCHOPS_AES_256 / 8; unsigned char enckey[enckeysize]; @@ -163,19 +176,24 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); goto exit; } - atclient_atsign recipient; - ret = atclient_atsign_init(&recipient, atkey->sharedwith); - if (ret != 0) { - goto exit; - } - ret = atclient_get_shared_encryption_key_shared_by_me(atclient, &recipient, enc_key, - create_new_encryption_key_shared_by_me_if_not_found); - if (ret != 0) { + char *recipient = atkey->sharedwith; + if ((ret = atclient_get_shared_encryption_key_shared_by_me( + atclient, recipient, enc_key, create_new_encryption_key_shared_by_me_if_not_found)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); goto exit; } } + if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if ((ret = atclient_stringutils_atsign_without_at(atclient->atsign, &client_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + goto exit; + } + ret = atchops_base64_decode((unsigned char *)enc_key, strlen(enc_key), enckey, enckeysize, &enckeylen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); @@ -214,7 +232,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke } char *response = (char *)recv; - short atsign_with_at_len = (short)strlen(atclient->atsign.atsign); + short atsign_with_at_len = (short)strlen(client_atsign_with_at); // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; @@ -225,7 +243,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } - snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); + snprintf(response_prefix, response_prefix_len, "@%s@", client_atsign_without_at); if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; @@ -338,6 +356,8 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *command = NULL; unsigned char *recv = NULL; char *response_prefix = NULL; + char *client_atsign_with_at = NULL; + char *client_atsign_without_at = NULL; const size_t enckeysize = ATCHOPS_AES_256 / 8; unsigned char enckey[enckeysize]; @@ -369,6 +389,16 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at } } + if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if ((ret = atclient_stringutils_atsign_without_at(atclient->atsign, &client_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + goto exit; + } + char *namespace = ""; size_t namespace_len = 0; short extra_point_len = 0; // "." before namespace @@ -401,7 +431,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at } char *response = (char *)recv; - short atsign_with_at_len = (short)strlen(atclient->atsign.atsign); + short atsign_with_at_len = (short)strlen(client_atsign_with_at); // Truncate response: "@" + myatsign + "@" int response_prefix_len = atsign_with_at_len + 2; @@ -411,7 +441,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } - snprintf(response_prefix, response_prefix_len, "@%s@", atclient->atsign.without_prefix_str); + snprintf(response_prefix, response_prefix_len, "@%s@", client_atsign_without_at); if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; @@ -511,6 +541,7 @@ exit: { free(recv); if (response_prefix) free(response_prefix); + free(client_atsign_with_at); return ret; } } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 803dc703..aa339b1b 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -14,17 +14,17 @@ #define TAG "atclient_put" -static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, - const size_t valuelen, int *commitid); +static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, + const size_t valuelen, const int *commitid); -int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { +int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { int ret = 1; /* * 1. Check if valid arguments were passed */ - if ((ret = atclient_put_valid_args_check(atclient, atkey, value, valuelen, commitid)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_valid_args_check: %d\n", ret); + if ((ret = atclient_put_validate_args(ctx, atkey, value, valuelen, commitid)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_validate_args: %d\n", ret); return ret; } @@ -72,9 +72,9 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to // receive commmit id from `update:` unsigned char *recv = NULL; - if (!atclient->async_read) { + if (!ctx->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); - if(recv == NULL) { + if (recv == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); goto exit; @@ -107,8 +107,8 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); size_t selfencryptionkeylen = 0; - if ((ret = atchops_base64_decode((const unsigned char *)atclient->atkeys.selfencryptionkeybase64, - strlen(atclient->atkeys.selfencryptionkeybase64), selfencryptionkey, + if ((ret = atchops_base64_decode((const unsigned char *)ctx->atkeys.selfencryptionkeybase64, + strlen(ctx->atkeys.selfencryptionkeybase64), selfencryptionkey, selfencryptionkeysize, &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; @@ -132,56 +132,46 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c // if it doesn't exist, create one for us and create one for the other person // create one for us -> encrypted with our self encryption key // create one for the other person -> encrypted with their public encryption key - atclient_atsign recipient; - atclient_atsign_init(&recipient, atkey->sharedwith); + char *recipient_atsign = atkey->sharedwith; - if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, &recipient, sharedenckeybase64, true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); - goto error_cleanup; + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign, sharedenckeybase64, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + goto exit; } if ((ret = atchops_iv_generate(iv)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); - goto error_cleanup; + goto exit; } if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)ivbase64, ivbase64size, &ivbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto error_cleanup; + goto exit; } if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); - goto error_cleanup; + goto exit; } if ((ret = atchops_base64_decode((unsigned char *)sharedenckeybase64, strlen(sharedenckeybase64), sharedenckey, sizeof(sharedenckey), &sharedenckeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto error_cleanup; + goto exit; } if ((ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, ciphertextsize, &ciphertextlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); - goto error_cleanup; + goto exit; } if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, &ciphertextbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto error_cleanup; + goto exit; } - - goto non_error_cleanup; - - error_cleanup: { - atclient_atsign_free(&recipient); - goto exit; - } - - non_error_cleanup: { atclient_atsign_free(&recipient); } } // 3b. Build the command @@ -201,7 +191,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c cmdbuffersize = strlen("update: \r\n") + metadataprotocolstrlen + atkeystrlen + ciphertextbase64len + 1; // + 1 for null terminator cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if(cmdbuffer == NULL) { + if (cmdbuffer == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); goto exit; @@ -213,13 +203,13 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c /* * 4. Send the command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, - recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, + recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - if (atclient->async_read) { + if (ctx->async_read) { goto exit; } @@ -242,7 +232,7 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c ret = 0; goto exit; exit: { - if (!atclient->async_read) { + if (!ctx->async_read) { free(recv); } free(cmdbuffer); @@ -252,24 +242,25 @@ exit: { } } -static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atkey, const char *value, - const size_t valuelen, int *commitid) { + +static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, + const size_t valuelen, const int *commitid) { int ret = 1; - if (atclient == NULL) { + if (ctx == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); goto exit; } - if(!atclient->_atsign_is_allocated) { + if (!atclient_is_atserver_connection_started(ctx)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient's atsign is not allocated\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); goto exit; } - if(!atclient->_atserver_connection_started) { + if (!atclient_is_atsign_initialized(ctx)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient's atserver connection is not started\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not allocated. Make sure to PKAM authenticate first\n"); goto exit; } @@ -291,19 +282,19 @@ static int atclient_put_valid_args_check(atclient *atclient, atclient_atkey *atk goto exit; } - if(!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not initialized or is empty\n"); goto exit; } - if (strcmp(atkey->sharedby, atclient->atsign.atsign) != 0) { + if (strcmp(atkey->sharedby, ctx->atsign) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); goto exit; } - if (atclient->async_read) { + if (ctx->async_read) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); goto exit; diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index 57bd3ee6..a43b58fd 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -3,24 +3,50 @@ #include "atchops/base64.h" #include "atclient/atclient.h" #include "atclient/atkeys.h" +#include "atclient/constants.h" #include "atclient/stringutils.h" #include "atlogger/atlogger.h" #include #include #define TAG "encryption_key_helpers" -#define ATCLIENT_ERR_AT0015_KEY_NOT_FOUND -0x1980 -int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclient_atsign *recipient, +int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *recipient_atsign, char *enc_key_shared_by_me, bool create_new_if_not_found) { int ret = 1; + char *sender_atsign_with_at = NULL; + char *sender_atsign_without_at = NULL; + + char *recipient_atsign_with_at = NULL; + char *recipient_atsign_without_at = NULL; + + if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(sender_atsign_with_at, &sender_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(recipient_atsign_with_at, &recipient_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + // llookup:shared_key.recipient_atsign@myatsign - const short commandsize = strlen("llookup:shared_key.") + strlen(recipient->without_prefix_str) + - strlen(ctx->atsign.atsign) + strlen("\r\n") + 1; + const short commandsize = + strlen("llookup:shared_key.") + strlen(recipient_atsign_without_at) + strlen(sender_atsign_with_at) + strlen("\r\n") + 1; char command[commandsize]; memset(command, 0, sizeof(char) * commandsize); - snprintf(command, commandsize, "llookup:shared_key.%s%s\r\n", recipient->without_prefix_str, ctx->atsign.atsign); + snprintf(command, commandsize, "llookup:shared_key.%s%s\r\n", recipient_atsign_without_at, sender_atsign_with_at); const size_t recvsize = 1024; unsigned char recv[recvsize]; @@ -36,10 +62,10 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien char *response = (char *)recv; // Truncate response: "@" + myatsign + "@" - const short responseprefixsize = strlen(ctx->atsign.atsign) + 3; + const short responseprefixsize = strlen(sender_atsign_with_at) + 3; char responseprefix[responseprefixsize]; memset(responseprefix, 0, sizeof(char) * responseprefixsize); - snprintf(responseprefix, responseprefixsize, "@%s@", ctx->atsign.without_prefix_str); + snprintf(responseprefix, responseprefixsize, "@%s@", sender_atsign_without_at); const short responseprefixlen = (short)strlen(response); if (atclient_stringutils_starts_with(response, responseprefix)) { @@ -86,9 +112,10 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { // or do I need to create, store and share a new shared key? if (create_new_if_not_found) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Creating new shared encryption key for %s\n", recipient->atsign); - ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, &(ctx->atsign), recipient, - enc_key_shared_by_me); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Creating new shared encryption key for %s\n", + recipient_atsign_with_at); + ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, sender_atsign_with_at, + recipient_atsign_with_at, enc_key_shared_by_me); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_create_shared_encryption_key: %d\n", ret); return ret; @@ -102,18 +129,44 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const atclien return 0; } -int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atclient_atsign *recipient, +int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char *recipient_atsign, char *enc_key_shared_by_other) { int ret = 1; + char *sender_atsign_with_at = NULL; + char *sender_atsign_without_at = NULL; + + char *recipient_atsign_with_at = NULL; + char *recipient_atsign_without_at = NULL; + + if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(sender_atsign_with_at, &sender_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(recipient_atsign_with_at, &recipient_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + // llookup:cached:@myatsign:shared_key@recipient_atsign // lookup:shared_key@recipient_atsign char *command_prefix = "lookup:shared_key@"; const short command_prefix_len = 18; - short commandsize = command_prefix_len + strlen(recipient->without_prefix_str) + 3; + short commandsize = command_prefix_len + strlen(recipient_atsign_without_at) + 3; char command[commandsize]; - snprintf(command, commandsize, "lookup:shared_key@%s\r\n", recipient->without_prefix_str); + snprintf(command, commandsize, "lookup:shared_key@%s\r\n", recipient_atsign_without_at); const size_t recvsize = 1024; unsigned char recv[recvsize]; @@ -129,9 +182,9 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atcl char *response = (char *)recv; // Truncate response: "@" + myatsign + "@" - short response_prefix_len = (short)strlen(ctx->atsign.without_prefix_str) + 3; + short response_prefix_len = (short)strlen(sender_atsign_without_at) + 3; char response_prefix[response_prefix_len]; - snprintf(response_prefix, response_prefix_len, "@%s@", ctx->atsign.without_prefix_str); + snprintf(response_prefix, response_prefix_len, "@%s@", sender_atsign_without_at); if (atclient_stringutils_starts_with(response, response_prefix)) { response = response + response_prefix_len; @@ -182,18 +235,28 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const atcl return 0; } -int atclient_get_public_encryption_key(atclient *ctx, const atclient_atsign *atsign, char *public_encryption_key) { +int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char *public_encryption_key) { int ret = 1; + bool should_free_atsign_with_at = false; + char *atsign_with_at = ctx->atsign; + + if (atsign != NULL) { + if ((ret = atclient_stringutils_atsign_with_at(atsign, &atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + should_free_atsign_with_at = true; + } + // plookup:publickey@atsign char *command_prefix = "plookup:publickey"; const short command_prefix_len = 17; - const atclient_atsign *pub_enc_key_atsign = atsign != NULL ? atsign : &ctx->atsign; - short command_len = command_prefix_len + strlen(pub_enc_key_atsign->atsign) + 3; + short command_len = command_prefix_len + strlen(atsign_with_at) + 3; char command[command_len]; - snprintf(command, command_len, "plookup:publickey%s\r\n", pub_enc_key_atsign->atsign); + snprintf(command, command_len, "plookup:publickey%s\r\n", atsign_with_at); // execute command const size_t recvlen = 1024; @@ -218,12 +281,17 @@ int atclient_get_public_encryption_key(atclient *ctx, const atclient_atsign *ats return ret; } - return 0; + ret = 0; +exit: { + if (should_free_atsign_with_at) { + free(atsign_with_at); + } + return ret; +} } -int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const atclient_atsign *sharedby, - const atclient_atsign *sharedwith, - char *sharedenckeybyme) { +int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const char *sharedby, + const char *sharedwith, char *sharedenckeybyme) { int ret = 1; // 1. variables @@ -268,9 +336,35 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie char *cmdbuffer1 = NULL; char *cmdbuffer2 = NULL; + char *sharedby_atsign_with_at = NULL; + char *sharedby_atsign_without_at = NULL; + + char *sharedwith_atsign_with_at = NULL; + char *sharedwith_atsign_without_at = NULL; + atchops_rsakey_publickey publickeystruct; atchops_rsakey_publickey_init(&publickeystruct); + if ((ret = atclient_stringutils_atsign_with_at(sharedby, &sharedby_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(sharedby_atsign_with_at, &sharedby_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_with_at(sharedwith, &sharedwith_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + return ret; + } + + if ((ret = atclient_stringutils_atsign_without_at(sharedwith_atsign_with_at, &sharedwith_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + return ret; + } + // 2. generate shared encryption key ret = atchops_aes_generate_keybase64(sharedenckeybase64, sharedenckeybase64size, &sharedenckeybase64len, ATCHOPS_AES_256); @@ -332,27 +426,27 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie // 5. prep protocol commands // 5a. for us (update:shared_key.sharedby@sharedwith \r\n) - const size_t cmdbuffersize1 = strlen("update:shared_key. \r\n") + strlen(sharedwith->without_prefix_str) + - strlen(sharedby->atsign) + 1 + sharedenckeybase64encryptedforuslen; + const size_t cmdbuffersize1 = strlen("update:shared_key. \r\n") + strlen(sharedwith_atsign_without_at) + + strlen(sharedby_atsign_with_at) + 1 + sharedenckeybase64encryptedforuslen; cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1); if (cmdbuffer1 == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer1\n"); goto exit; } - snprintf(cmdbuffer1, cmdbuffersize1, "update:shared_key.%s%s %s\r\n", sharedwith->without_prefix_str, - sharedby->atsign, sharedenckeybase64encryptedforus); + snprintf(cmdbuffer1, cmdbuffersize1, "update:shared_key.%s%s %s\r\n", sharedwith_atsign_without_at, + sharedby_atsign_with_at, sharedenckeybase64encryptedforus); // 5b. for them (update:shared_key.sharedwith@sharedby \r\n) - const size_t cmdbuffersize2 = strlen("update::shared_key \r\n") + strlen(sharedby->atsign) + - strlen(sharedwith->atsign) + 1 + sharedenckeybase64encryptedforthemlen; + const size_t cmdbuffersize2 = strlen("update::shared_key \r\n") + strlen(sharedby_atsign_with_at) + + strlen(sharedwith_atsign_with_at) + 1 + sharedenckeybase64encryptedforthemlen; if ((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer2\n"); goto exit; } - snprintf(cmdbuffer2, cmdbuffersize2, "update:%s:shared_key%s %s\r\n", sharedwith->atsign, sharedby->atsign, - sharedenckeybase64encryptedforthem); + snprintf(cmdbuffer2, cmdbuffersize2, "update:%s:shared_key%s %s\r\n", sharedwith_atsign_with_at, + sharedby_atsign_with_at, sharedenckeybase64encryptedforthem); // 6. put "encrypted for us" into key store ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer1, cmdbuffersize1 - 1, @@ -398,6 +492,10 @@ exit: { atchops_rsakey_publickey_free(&publickeystruct); free(cmdbuffer1); free(cmdbuffer2); + free(sharedby_atsign_with_at); + free(sharedby_atsign_without_at); + free(sharedwith_atsign_with_at); + free(sharedwith_atsign_without_at); return ret; } } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 87f614c6..ee3a2168 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -106,28 +106,29 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific return ret; } } else { - atclient_atsign recipient; + char *recipient = NULL; const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; unsigned char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); - if ((ret = atclient_atsign_init(&recipient, params->atkey->sharedwith)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init failed with code %d\n", ret); + if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", + ret); return ret; } - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, &recipient, (char *)sharedenckeybase64, true)) != + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient, (char *)sharedenckeybase64, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", ret); - atclient_atsign_free(&recipient); + free(recipient); return ret; } if ((ret = atchops_base64_decode(sharedenckeybase64, strlen((char *)sharedenckeybase64), sharedenckey, sharedenckeysize, &sharedenckeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", ret); - atclient_atsign_free(&recipient); + free(recipient); return ret; } - atclient_atsign_free(&recipient); + free(recipient); } unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 1dbe9632..d57c033f 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -241,9 +241,35 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re atclient_atkey atkeyforthem; atclient_atkey_init(&atkeyforthem); + + char *client_atsign_with_at = NULL; + char *client_atsign_without_at = NULL; + + char *recipient_atsign_with_at = NULL; + char *recipient_atsign_without_at = NULL; + + if((ret = atclient_stringutils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if((ret = atclient_stringutils_atsign_without_at(atclient1->atsign, &client_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + goto exit; + } + + if((ret = atclient_stringutils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if((ret = atclient_stringutils_atsign_without_at(recipient, &recipient_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + goto exit; + } memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); - snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", (recipient + 1), atclient1->atsign.atsign); + snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", recipient_atsign_without_at, client_atsign_with_at); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkeystrtemp: \"%s\"\n", atkeystrtemp); if ((ret = atclient_atkey_from_string(&atkeyforme, atkeystrtemp)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d\n", ret); @@ -251,7 +277,7 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re } memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); - snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "%s:shared_key%s", recipient, atclient1->atsign.atsign); + snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "%s:shared_key%s", recipient_atsign_with_at, client_atsign_with_at); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkeystrtemp: \"%s\"\n", atkeystrtemp); if ((ret = atclient_atkey_from_string(&atkeyforthem, atkeystrtemp)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d\n", ret); @@ -276,6 +302,10 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re exit: { atclient_atkey_free(&atkeyforme); atclient_atkey_free(&atkeyforthem); + free(client_atsign_with_at); + free(client_atsign_without_at); + free(recipient_atsign_with_at); + free(recipient_atsign_without_at); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down 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 f30cc0b8..b3e9a0d8 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -311,7 +311,7 @@ static int test_4_re_pkam_auth_and_start_monitor(atclient *monitor_conn) { const 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.atsign)) != 0) { + monitor_conn->atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM: %d\n", ret); goto exit; } From afd5e66baaa71d832c8f738ea3273908d1281a40 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 12:46:29 -0400 Subject: [PATCH 093/193] fix: --- .../atclient/src/atclient_get_sharedkey.c | 19 ++++++++++--------- packages/atclient/src/monitor.c | 6 ++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index fa17de24..925df81e 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -116,14 +116,19 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu } char *client_atsign_with_at = NULL; + char *sharedby_atsign_with_at = NULL; if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - if (strcmp(atkey->sharedby, client_atsign_with_at) != 0) { - // && (!atkey->metadata.iscached && !atkey->metadata.ispublic) + if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedby, &sharedby_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } + + if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_other_with_me: %d\n", ret); @@ -141,6 +146,7 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; exit: { free(client_atsign_with_at); + free(sharedby_atsign_with_at); return ret; } } @@ -377,13 +383,8 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); goto exit; } - atclient_atsign recipient; - ret = atclient_atsign_init(&recipient, atkey->sharedby); - if (ret != 0) { - goto exit; - } - ret = atclient_get_shared_encryption_key_shared_by_other(atclient, &recipient, enc_key); - if (ret != 0) { + char *recipient = atkey->sharedby; + if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, recipient, enc_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 88719755..7e7b269a 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -1145,8 +1145,7 @@ exit: { static int decrypt_notification(atclient *atclient, atclient_atnotification *notification) { int ret = 1; - atclient_atsign atsignfrom; - atclient_atsign_init(&atsignfrom, notification->from); + char *from_atsign = notification->from; unsigned char *decryptedvaluetemp = NULL; @@ -1211,7 +1210,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 3. get shared encryption key to decrypt - ret = atclient_get_shared_encryption_key_shared_by_other(atclient, &atsignfrom, (char *)sharedenckeybase64); + ret = atclient_get_shared_encryption_key_shared_by_other(atclient, from_atsign, (char *)sharedenckeybase64); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared encryption key\n"); goto exit; @@ -1257,7 +1256,6 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not ret = 0; goto exit; exit: { - atclient_atsign_free(&atsignfrom); free(decryptedvaluetemp); return ret; } From d759711fb25e82733cc89528b8d532e1fc85c530 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 12:52:23 -0400 Subject: [PATCH 094/193] fix: --- packages/atclient/src/atclient_get_sharedkey.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 925df81e..5cf87a79 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -195,7 +195,7 @@ atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atke goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(atclient->atsign, &client_atsign_without_at)) != 0) { + if ((ret = atclient_stringutils_atsign_without_at(client_atsign_with_at, &client_atsign_without_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); goto exit; } @@ -395,7 +395,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(atclient->atsign, &client_atsign_without_at)) != 0) { + if ((ret = atclient_stringutils_atsign_without_at(client_atsign_with_at, &client_atsign_without_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); goto exit; } From 9c3ae3254a155faceed192a5e35b628ebc657bd1 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 12:55:23 -0400 Subject: [PATCH 095/193] chore: clean up atclient_get_sharedkey --- packages/atclient/src/atclient_get_sharedkey.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 5cf87a79..160260dc 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -129,15 +129,13 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key); - if (ret != 0) { + if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, valuesize, valuelen, shared_enc_key, - false); - if (ret != 0) { + if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, valuesize, valuelen, shared_enc_key, + false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_me_with_other: %d\n", ret); goto exit; } From 5d8898cf546348830a16675e9dc9e3823ba91908 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:10:59 -0400 Subject: [PATCH 096/193] wip: cleaning up encryption_key_helpers --- packages/atchops/include/atchops/aes.h | 12 - packages/atchops/src/aes.c | 23 - packages/atclient/include/atclient/atclient.h | 13 +- .../include/atclient/encryption_key_helpers.h | 55 +- .../atclient/src/atclient_get_sharedkey.c | 734 +++++++++-------- packages/atclient/src/atclient_put.c | 8 +- .../atclient/src/encryption_key_helpers.c | 743 ++++++++++-------- packages/atclient/src/notify.c | 11 +- .../tests/test_atclient_sharedkey.c | 4 +- 9 files changed, 828 insertions(+), 775 deletions(-) diff --git a/packages/atchops/include/atchops/aes.h b/packages/atchops/include/atchops/aes.h index 2e924c49..6ff05a4b 100644 --- a/packages/atchops/include/atchops/aes.h +++ b/packages/atchops/include/atchops/aes.h @@ -18,16 +18,4 @@ enum atchops_aes_size { */ int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size keybits); -/** - * @brief Generate an AES key of size keylen bits encoded in base 64 - * - * @param keybase64 key buffer to hold base64 string - * @param keybase64size the allocated length of the key buffer - * @param keybase64len the written length of the key buffer - * @param keybits the AES key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) - * @return int 0 on success - */ -int atchops_aes_generate_keybase64(unsigned char *keybase64, const size_t keybase64size, - size_t *keybase64len, const enum atchops_aes_size keybits); - #endif diff --git a/packages/atchops/src/aes.c b/packages/atchops/src/aes.c index 461a267a..1f626ce9 100644 --- a/packages/atchops/src/aes.c +++ b/packages/atchops/src/aes.c @@ -39,26 +39,3 @@ exit: { return ret; } } - -int atchops_aes_generate_keybase64(unsigned char *keybase64, const size_t keybase64size, - size_t *keybase64len, const enum atchops_aes_size keybits) { - int ret = 1; - - const size_t keysize = keybits / 8; - unsigned char key[keysize]; - memset(key, 0, keysize); - - ret = atchops_aes_generate_key(key, keybits); - if (ret != 0) { - goto exit; - } - - ret = atchops_base64_encode(key, keysize, keybase64, keybase64size, keybase64len); - if (ret != 0) { - goto exit; - } - - goto exit; - -exit: { return ret; } -} diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 490289c0..8342fb80 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -204,22 +204,17 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * associated with your value. * * @param atclient The atclient context (must satisfy the two conditions stated above) - * @param root_conn initialized root connection * @param atkey The populated atkey to get the value from (must satisfy the two conditions stated above) * @param value The buffer to hold value gotten from atServer * @param valuesize The buffer length allocated for the value * @param valuelen The output length of the value gotten from atServer - * @param shared_enc_key The correct shared encryption key (get_encryption_key_shared_by_me or - * get_encryption_key_shared_by_other, depending on the case). If NULL is provided, the method will check - * create_new_encryption_key_shared_by_me_if_not_found parameter. - * @param create_new_encryption_key_shared_by_me_if_not_found If NULL is provided for the shared_enc_key parameter, the - * function will attempt to retrieve it from the at_server. This parameter can be used to determine whether the key - * should be created (if it hasn’t already been) or not. + * @param shared_encryption_key The correct shared encryption key (get_encryption_key_shared_by_me or + * get_encryption_key_shared_by_other, depending on the case). If NULL is provided, it will get it for you, if it + * exists. * @return int 0 on success */ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, char *shared_enc_key, - const bool create_new_encryption_key_shared_by_me_if_not_found); + size_t *valuelen, const unsigned char *shared_encryption_key); /** * @brief Delete an atkey from your atserver diff --git a/packages/atclient/include/atclient/encryption_key_helpers.h b/packages/atclient/include/atclient/encryption_key_helpers.h index fb249df6..78b4fd5c 100644 --- a/packages/atclient/include/atclient/encryption_key_helpers.h +++ b/packages/atclient/include/atclient/encryption_key_helpers.h @@ -5,6 +5,20 @@ #include "atclient/atkey.h" #include "atclient/atkeys.h" +/** + * @brief Retreives the public encryption key of a given atsign. + * + * @param ctx Initialized atclient context. Assumed to be non-null, initialized with atclient_init, and pkam + * authenticated using atclient_pkam_authenticate (required) + * @param recipient the atsign of the recipient with @ symbol (required) + * @param public_encryption_key The output rsa public key base64 encoded and non-encrypted (required). Essentially what + * you get from doing a plookup. Mandatory field, non-null. The caller is responsible for freeing the memory. If the + * return code is 0, then the memory is allocated and the caller is responsible for freeing it and is safe for reading + * and usage. + * @return int 0 on success, error otherwise + */ +int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char **public_encryption_key); + /** * @brief Looks up the symmetric shared key which the atclient's atsign shared with the recipient's atsign. * If no key is found and create_new_if_not_found is true, it will create, store and share a new one with the @@ -12,13 +26,13 @@ * * @param ctx Initialized atclient context (required) * @param recipient atsign of the recipient with @ symbol (required) - * @param enc_key_shared_by_me The output shared key in b64 format (required) - * @param create_new_if_not_found true if in case the symmetric shared key does not exist, you would like it to be - * created / false if not (required) + * @param shared_encryption_key_shared_by_me (required to receive output) the non-encrypted non-base64-encoded shared + * encryption AES 256 key in raw bytes. Expected to be non-null and has 32 bytes of memory allocated to this address to + * hold key. * @return int 0 on success, error otherwise */ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *recipient_atsign, - char *enc_key_shared_by_me, bool create_new_if_not_found); + unsigned char *shared_encryption_key_shared_by_me); /** * @brief Looks up the symmetric shared key which the recipient's atsign shared with atclient's atsign. @@ -27,36 +41,29 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r * @param ctx Initialized atclient context (required) * @param root_conn initialized root connection * @param recipient the atsign of the recipient with @ symbol (required) - * @param enc_key_shared_by_other the output shared key in b64 format (required) + * @param shared_encryption_key_shared_by_other (required to receive output) the non-encrypted non-base64-encoded shared + * encryption AES 256 key in raw bytes.. Expected to be non-null and has 32 bytes of memory allocated to this address to + * hold key. * @return int 0 on success, error otherwise */ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char *recipient, - char *enc_key_shared_by_other); - -/** - * @brief Retreives the public encryption key of a given atsign. - * - * @param ctx Initialized atclient context (required) - * @param recipient the atsign of the recipient with @ symbol (required) - * @param public_encryption_key The output public key in b64 format (required) - * @return int 0 on success, error otherwise - */ -int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, - char *public_encryption_key); + unsigned char *shared_encryption_key_shared_by_other); /** * @brief Creates a shared encryption key pair and puts it in your atServer. One is made for you to use for encrypting * data to send to other, and the other is for the recipient to use to decrypt the data that you sent * (shared_key.other@me and @other:shared_key@me) * - * @param atclient the atclient context (must be initialized and pkam_authenticated) - * @param sharedby TODO: documentation - * @param sharedwith TODO: documentation - * @param sharedenckeybyme TODO: documentation + * @param atclient non-null atclient context (must be initialized and pkam_authenticated) + * @param sharedby non-null atSign (with @ symbol) and null-terminated. Example "@bob" + * @param sharedwith non-null atSign (with @ symbol) and null-terminated. Example "@bob" + * @param shared_encryption_key_shared_by_me_with_other holds the output shared encryption key that was created by this + * function, to be shared with the recipient. Expected to be non-null and has 32 bytes of memory allocated to this + * address to hold key. This key is not base64 encoded and not encrypted and is in raw bytes. * @return int 0 on success, error otherwise */ -int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const char *sharedby, - const char *sharedwith, - char *sharedenckeybyme); +int atclient_create_shared_encryption_key_pair_for_me_and_other( + atclient *atclient, const char *sharedby, const char *sharedwith, + unsigned char *shared_encryption_key_shared_by_me_with_other); #endif // ATCLIENT_ENCRYPTION_KEY_HELPERS_H diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 160260dc..a640f59d 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -14,103 +14,22 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t valuesize, const size_t *valuelen, - const char *shared_enc_key) { - int ret = 1; - - if (atclient == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - goto exit; - } - - if (!atclient_is_atserver_connection_started(atclient)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection was not started\n"); - goto exit; - } - - if (!atclient_is_atsign_initialized(atclient)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized was not set\n"); - goto exit; - } - - if (atclient->async_read) { - ret = 1; - atlogger_log( - TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_sharedkey cannot be called from an async_read atclient, it will cause a race condition\n"); - goto exit; - } - - if (atkey == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); - goto exit; - } - - if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); - goto exit; - } - - if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is not initialized or is empty\n"); - goto exit; - } - - if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is not initialized or is empty\n"); - goto exit; - } - - if (!atclient_atkey_is_sharedwith_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is not initialized or is empty\n"); - goto exit; - } + const unsigned char *shared_encryption_key); - if (value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - goto exit; - } - - if (valuesize == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); - goto exit; - } - - if (valuelen == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); - goto exit; - } - - ret = 0; -exit: { return ret; } -} - -static int -atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, char *shared_enc_key, - const bool create_new_encryption_key_shared_by_me_if_not_found); +static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, + const size_t valuesize, size_t *valuelen, + const unsigned char *shared_encryption_key); static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen, - char *shared_enc_key); + const unsigned char *shared_encryption_key); int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, char *shared_enc_key, - const bool create_new_encryption_key_shared_by_me_if_not_found) { + size_t *valuelen, const unsigned char *shared_encryption_key) { int ret = 1; - if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, valuesize, valuelen, shared_enc_key)) != - 0) { + if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, valuesize, valuelen, + shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_validate_arguments: %d\n", ret); return ret; } @@ -129,13 +48,14 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, shared_enc_key)) != 0) { + if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, + shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, valuesize, valuelen, shared_enc_key, - false)) != 0) { + if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, valuesize, valuelen, + shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_me_with_other: %d\n", ret); goto exit; } @@ -149,398 +69,462 @@ exit: { } } -static int -atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, char *shared_enc_key, - const bool create_new_encryption_key_shared_by_me_if_not_found) { +static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, + const size_t valuesize, size_t *valuelen, + const unsigned char *shared_encryption_key) { int ret = 1; - short enc_key_mem = 0; + /* + * 1. Validate arguments + */ + // TODO + + /* + * 2. Variables + */ + char *client_atsign_with_at = NULL; + char *recipient_atsign_with_at = NULL; char *atkeystr = NULL; char *command = NULL; - char *response_prefix = NULL; - unsigned char *recv = NULL; - char *client_atsign_with_at = NULL; - char *client_atsign_without_at = NULL; - - const size_t enckeysize = ATCHOPS_AES_256 / 8; - unsigned char enckey[enckeysize]; - memset(enckey, 0, sizeof(unsigned char) * enckeysize); - size_t enckeylen = 0; - - char *valueraw = NULL; - - // check shared key - char *enc_key = shared_enc_key; - if (enc_key == NULL) { - enc_key_mem = 1; - enc_key = malloc(45); - if (enc_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); - goto exit; - } - char *recipient = atkey->sharedwith; - if ((ret = atclient_get_shared_encryption_key_shared_by_me( - atclient, recipient, enc_key, create_new_encryption_key_shared_by_me_if_not_found)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); - goto exit; - } - } + const size_t recvsize = 4096; + unsigned char recv[recvsize]; + size_t recvlen = 0; + + const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[ivsize]; + + char *value_raw_encrypted = NULL; + char *value_raw = NULL; + + /* + * 3. Format atSigns + */ if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(client_atsign_with_at, &client_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - ret = atchops_base64_decode((unsigned char *)enc_key, strlen(enc_key), enckey, enckeysize, &enckeylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; + /* + * 4. Get shared_encryption_key, if necessary + */ + unsigned char shared_encryption_key_to_use[ATCHOPS_AES_256 / 8]; + memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * ATCHOPS_AES_256 / 8); + if (shared_encryption_key != NULL) { + memcpy(shared_encryption_key_to_use, shared_encryption_key, ATCHOPS_AES_256 / 8); + } else { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->sharedwith, + shared_encryption_key_to_use)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + goto exit; + } } - ret = atclient_atkey_to_string(atkey, &atkeystr); - if (ret != 0) { + /* + * 5. Build `llookup:all:` command + */ + if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } const size_t atkeystrlen = strlen(atkeystr); - // build command - // command_prefix = "llookup:all:" const size_t commandsize = strlen("llookup:all:") + atkeystrlen + strlen("\r\n") + 1; - command = malloc(commandsize * sizeof(char)); - if (command == NULL) { + if ((command = malloc(sizeof(char) * commandsize)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); goto exit; } snprintf(command, commandsize, "llookup:all:%s\r\n", atkeystr); - // send command and recv response - const size_t recvsize = 4096; - recv = calloc(recvsize, sizeof(unsigned char)); - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; - - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, - recvsize, &recvlen); - if (ret != 0) { + /* + * 6. Send llookup:all: command + */ + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, + recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } + /* + * 7. Parse response + */ char *response = (char *)recv; - short atsign_with_at_len = (short)strlen(client_atsign_with_at); - // Truncate response: "@" + myatsign + "@" - int response_prefix_len = atsign_with_at_len + 2; - response_prefix = malloc(response_prefix_len * sizeof(char)); - memset(response_prefix, 0, sizeof(char) * response_prefix_len); - if (response_prefix == NULL) { + if (!atclient_stringutils_starts_with(response, "data:")) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } - snprintf(response_prefix, response_prefix_len, "@%s@", client_atsign_without_at); - if (atclient_stringutils_starts_with(response, response_prefix)) { - response = response + response_prefix_len; + char *response_without_data = response + 5; + + const cJSON *root = cJSON_Parse(response); + if (root == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); + goto exit; } - if (atclient_stringutils_ends_with(response, response_prefix)) { - response[strlen(response) - response_prefix_len - 1] = '\0'; + cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); + goto exit; } - // Truncate response : "data:" - if (atclient_stringutils_starts_with(response, "data:")) { - response = response + 5; - - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - const cJSON *root = cJSON_Parse(response); - if (root == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); - goto exit; - } + char *metadatastr = cJSON_Print(metadata); - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); - goto exit; - } + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + goto exit; + } - char *metadatastr = cJSON_Print(metadata); + cJSON *data = cJSON_GetObjectItem(root, "data"); + if (data == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); + goto exit; + } - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + /* + * 8. Set IV in the AtKey + */ + if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, + ATCHOPS_IV_BUFFER_SIZE, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } + } else { + memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + } - // manage IV - if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { - size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, - ATCHOPS_IV_BUFFER_SIZE, &ivlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } - - if (ivlen != ATCHOPS_IV_BUFFER_SIZE) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivlen != ATCHOPS_IV_BUFFER_SIZE (%d != %d)\n", ivlen, - ATCHOPS_IV_BUFFER_SIZE); - goto exit; - } - } else { - // use legacy IV - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - } - - cJSON *data = cJSON_GetObjectItem(root, "data"); - if (data == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); - goto exit; - } + /* + * 9. Decrypt data + */ + const char *value_raw_encrypted_base64 = data->valuestring; + const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); - const size_t valuerawsize = strlen(data->valuestring) * 4; // most likely enough space after base64 decode - valueraw = malloc(sizeof(char) * valuerawsize); - if (valueraw == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); - goto exit; - } - memset(valueraw, 0, sizeof(char) * valuerawsize); - size_t valuerawlen = 0; + const size_t value_raw_encrypted_size = atchops_base64_decoded_size(strlen(value_raw_encrypted_base64_len)); + if ((value_raw_encrypted = malloc(sizeof(char) * value_raw_encrypted_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw_encrypted\n"); + goto exit; + } - ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), - (unsigned char *)valueraw, valuerawsize, &valuerawlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } + memset(value_raw_encrypted, 0, sizeof(char) * value_raw_encrypted_size); + size_t value_raw_encrypted_len = 0; + if ((ret = atchops_base64_decode(value_raw_encrypted_base64, value_raw_encrypted_base64_len, + (unsigned char *)value_raw_encrypted, value_raw_encrypted_size, + &value_raw_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; + } - // decrypt response data - ret = atchops_aesctr_decrypt(enckey, ATCHOPS_AES_256, iv, (unsigned char *)valueraw, valuerawlen, - (unsigned char *)value, valuesize, valuelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); - goto exit; - } - } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); + const size_t value_raw_size = atchops_aesctr_plaintext_size(value_raw_encrypted_len); + if ((value_raw = malloc(sizeof(char) * value_raw_size)) == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); + goto exit; + } + memset(value_raw, 0, sizeof(char) * value_raw_size); + size_t value_raw_len = 0; + + if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, + (unsigned char *)value_raw_encrypted, value_raw_encrypted_len, + (unsigned char *)value_raw, value_raw_size, &valuelen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } ret = 0; goto exit; exit: { - free(enc_key); + free(client_atsign_with_at); + free(recipient_atsign_with_at); + free(value_raw); + free(atkeystr); free(command); free(recv); - free(atkeystr); return ret; } } static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, size_t *valuelen, - char *shared_enc_key) { + const unsigned char *shared_encryption_key) { int ret = 1; + + /* + * 1. Validate arguments + */ + // TODO + + /* + * 2. Variables + */ + char *sender_atsign_with_at = NULL; + char *recipient_atsign_with_at = NULL; + + const size_t recvsize = 4096; + unsigned char recv[recvsize]; + char *command = NULL; - unsigned char *recv = NULL; - char *response_prefix = NULL; - char *client_atsign_with_at = NULL; - char *client_atsign_without_at = NULL; - - const size_t enckeysize = ATCHOPS_AES_256 / 8; - unsigned char enckey[enckeysize]; - memset(enckey, 0, sizeof(unsigned char) * enckeysize); - size_t enckeylen = 0; - - const size_t valuerawsize = valuesize * 4; - unsigned char valueraw[valuerawsize]; - memset(valueraw, 0, sizeof(unsigned char) * valuerawsize); - size_t valuerawlen = 0; - - char *enc_key = shared_enc_key; - if (enc_key == NULL) { - enc_key = malloc(45); - if (enc_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for enc_key\n"); - goto exit; - } - char *recipient = atkey->sharedby; - if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, recipient, enc_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_encryption_key_shared_by_me: %d\n", ret); - goto exit; - } - } + cJSON *root = NULL; - if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[ivsize]; + + unsigned char *value_raw_encryted = NULL; + unsigned char *value_raw = NULL; + + + /* + * 3. Format atSigns + */ + if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedby, &sender_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(client_atsign_with_at, &client_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - char *namespace = ""; - size_t namespace_len = 0; - short extra_point_len = 0; // "." before namespace - - if (atkey->namespacestr != NULL && atkey->namespacestr[0] != '\0') { - namespace = atkey->namespacestr; - namespace_len = strlen(atkey->namespacestr); - extra_point_len = 1; + /* + * 4. Get shared_encryption_key, if necessary + */ + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key_to_use[shared_encryption_key_size]; + if (shared_encryption_key == NULL) { + if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, atkey->sharedby, + shared_encryption_key_to_use)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_other: %d\n", ret); + goto exit; + } + } else { + memcpy(shared_encryption_key_to_use, shared_encryption_key, shared_encryption_key_size); } - // build command - // command_prefix = "lookup:" - const short command_prefix_len = 11; - const size_t command_len = - command_prefix_len + strlen(atkey->key) + extra_point_len + namespace_len + strlen(atkey->sharedby) + 3; - command = calloc(command_len, sizeof(char)); - snprintf(command, command_len, "lookup:all:%s%s%s%s\r\n", atkey->key, extra_point_len ? "." : "", namespace, - atkey->sharedby); + /* + * 5. Build lookup: command + */ + const size_t commandsize = strlen("lookup:") + strlen(atkey->key) + strlen(sender_atsign_with_at) + ("\r\n") + 1; + if ((command = malloc(sizeof(char) * commandsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + goto exit; + } + snprintf(command, commandsize, "lookup:%s%s\r\n", atkey->key, sender_atsign_with_at); - // send command and recv response - const size_t recvsize = 4096; - recv = calloc(recvsize, sizeof(unsigned char)); + /* + * 6. Send lookup: command + */ + memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen = 0; - - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, strlen((char *)command), - recv, recvsize, &recvlen); - if (ret != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, + recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } + /* + * 7. Parse response + */ char *response = (char *)recv; - short atsign_with_at_len = (short)strlen(client_atsign_with_at); - // Truncate response: "@" + myatsign + "@" - int response_prefix_len = atsign_with_at_len + 2; - response_prefix = malloc(response_prefix_len * sizeof(char)); - if (response_prefix == NULL) { + // Truncate response : "data:" + if (!atclient_stringutils_starts_with(response, "data:")) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for response_prefix\n"); goto exit; } - snprintf(response_prefix, response_prefix_len, "@%s@", client_atsign_without_at); - if (atclient_stringutils_starts_with(response, response_prefix)) { - response = response + response_prefix_len; + char *response_without_data = response + 5; + + root = cJSON_Parse(response); + if (root == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); + goto exit; } - if (atclient_stringutils_ends_with(response, response_prefix)) { - response[strlen(response) - response_prefix_len - 1] = '\0'; + cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); + goto exit; } - // Truncate response : "data:" - if (atclient_stringutils_starts_with(response, "data:")) { - response = response + 5; - - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - const cJSON *root = cJSON_Parse(response); - if (root == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); - goto exit; - } + char *metadatastr = cJSON_Print(metadata); - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); - goto exit; - } + if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + goto exit; + } - char *metadatastr = cJSON_Print(metadata); + cJSON *data = cJSON_GetObjectItem(root, "data"); + if (data == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); + goto exit; + } - ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + /* + * 8. Set IV + */ + if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, + ivsize, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } + } else { + memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + } - // manage IV - if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { - size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, - ATCHOPS_IV_BUFFER_SIZE, &ivlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } - - if (ivlen != ATCHOPS_IV_BUFFER_SIZE) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivlen != ivlen (%d != %d)\n", ivlen, ATCHOPS_IV_BUFFER_SIZE); - goto exit; - } - } else { - // use legacy IV - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - } + /* + * 9. Decrypt data + */ - cJSON *data = cJSON_GetObjectItem(root, "data"); - if (data == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); - goto exit; - } + const unsigned char *value_raw_encrypted_base64 = (unsigned char *)data->valuestring; + const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); - // decrypt response data - ret = atchops_base64_decode((unsigned char *)enc_key, strlen(enc_key), enckey, enckeysize, &enckeylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } - - ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), valueraw, valuerawsize, - &valuerawlen); - if ((ret != 0) || (valuerawlen == 0)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d | or valuerawlen: %d\n", ret, - valuerawlen); - goto exit; - } + // 9a. base64 decode + const value_raw_encryted_size = atchops_base64_decoded_size(value_raw_encrypted_base64_len); + if ((value_raw_encryted = malloc(sizeof(unsigned char) * value_raw_encryted_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw_encryted\n"); + goto exit; + } + memset(value_raw_encryted, 0, sizeof(unsigned char) * value_raw_encryted_size); + size_t value_raw_encryted_len = 0; + if ((ret = atchops_base64_decode(value_raw_encrypted_base64, value_raw_encrypted_base64_len, value_raw_encryted, + value_raw_encryted_size, &value_raw_encryted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; + } - ret = atchops_aesctr_decrypt(enckey, ATCHOPS_AES_256, iv, valueraw, valuerawlen, (unsigned char *)value, valuesize, - valuelen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); - goto exit; - } - } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); + // 9b. aes decrypt + const size_t value_raw_size = atchops_aesctr_plaintext_size(value_raw_encryted_len); + if ((value_raw = malloc(sizeof(unsigned char) * value_raw_size)) == NULL) { ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); + goto exit; + } + memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); + size_t value_raw_len = 0; + if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encryted, + value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } + /* + * 10. Set value + */ + memcpy(value, value_raw, value_raw_len); + ret = 0; goto exit; exit: { - if (command) - free(command); - if (recv) - free(recv); - if (response_prefix) - free(response_prefix); - free(client_atsign_with_at); + free(sender_atsign_with_at); + free(recipient_atsign_with_at); + free(value_raw_encryted); + free(value_raw); + free(command); + free(metadatastr); + cJSON_Delete(root); return ret; } } + +static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, + const char *value, const size_t valuesize, const size_t *valuelen, + const unsigned char *shared_encryption_key) { + int ret = 1; + + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (!atclient_is_atserver_connection_started(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection was not started\n"); + goto exit; + } + + if (!atclient_is_atsign_initialized(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized was not set\n"); + goto exit; + } + + if (atclient->async_read) { + ret = 1; + atlogger_log( + TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_sharedkey cannot be called from an async_read atclient, it will cause a race condition\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_sharedwith_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is not initialized or is empty\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (valuesize == 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + goto exit; + } + + if (valuelen == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + goto exit; + } + + ret = 0; +exit: { return ret; } +} diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index aa339b1b..65593b77 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -134,7 +134,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const // create one for the other person -> encrypted with their public encryption key char *recipient_atsign = atkey->sharedwith; - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign, sharedenckeybase64, true)) != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign, sharedenckey)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); goto exit; } @@ -155,12 +155,6 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const goto exit; } - if ((ret = atchops_base64_decode((unsigned char *)sharedenckeybase64, strlen(sharedenckeybase64), sharedenckey, - sizeof(sharedenckey), &sharedenckeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } - if ((ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, ciphertextsize, &ciphertextlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index a43b58fd..cbfeb7cf 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -11,452 +11,570 @@ #define TAG "encryption_key_helpers" -int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *recipient_atsign, - char *enc_key_shared_by_me, bool create_new_if_not_found) { - int ret = 1; - - char *sender_atsign_with_at = NULL; - char *sender_atsign_without_at = NULL; +int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char **public_encryption_key) { - char *recipient_atsign_with_at = NULL; - char *recipient_atsign_without_at = NULL; + int ret = 1; - if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); return ret; } - if ((ret = atclient_stringutils_atsign_without_at(sender_atsign_with_at, &sender_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if (atsign == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is NULL\n"); return ret; } - if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if (public_encryption_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_encryption_key is NULL\n"); return ret; } - if ((ret = atclient_stringutils_atsign_without_at(recipient_atsign_with_at, &recipient_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); - return ret; - } + /* + * 2. Variables + */ - // llookup:shared_key.recipient_atsign@myatsign - const short commandsize = - strlen("llookup:shared_key.") + strlen(recipient_atsign_without_at) + strlen(sender_atsign_with_at) + strlen("\r\n") + 1; - char command[commandsize]; - memset(command, 0, sizeof(char) * commandsize); - snprintf(command, commandsize, "llookup:shared_key.%s%s\r\n", recipient_atsign_without_at, sender_atsign_with_at); + char *atsign_with_at = NULL; + char *atsign_without_at = NULL; - const size_t recvsize = 1024; + char *command = NULL; + + const size_t recvsize = 1024; // sufficient buffer size to receive the public key unsigned char recv[recvsize]; memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen = 0; - ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, commandsize - 1, recv, recvsize, - &recvlen); - if (ret != 0) { - return ret; + /* + * 3. Generate plookup command + */ + if ((ret = atclient_stringutils_atsign_with_at(atsign, &atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; } - char *response = (char *)recv; - - // Truncate response: "@" + myatsign + "@" - const short responseprefixsize = strlen(sender_atsign_with_at) + 3; - char responseprefix[responseprefixsize]; - memset(responseprefix, 0, sizeof(char) * responseprefixsize); - snprintf(responseprefix, responseprefixsize, "@%s@", sender_atsign_without_at); - const short responseprefixlen = (short)strlen(response); - - if (atclient_stringutils_starts_with(response, responseprefix)) { - response = response + responseprefixlen; + if ((ret = atclient_stringutils_atsign_without_at(atsign_with_at, &atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + goto exit; } - short responselen = (short)strlen(response); - if (atclient_stringutils_ends_with(response, responseprefix)) { - response[responselen - responseprefixlen - 1] = '\0'; + const size_t commandsize = strlen("plookup:publickey") + strlen(atsign_with_at) + strlen("\r\n") + 1; + if ((command = (char *)malloc(sizeof(char) * commandsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + goto exit; } + snprintf(command, commandsize, "plookup:publickey%s\r\n", atsign_with_at); - // does my atSign already have the recipient's shared key? - if (atclient_stringutils_starts_with(response, "data:")) { - response = response + 5; - responselen = (short)strlen(response); - - // 44 + 1 - const size_t plaintextsize = 45; - unsigned char plaintext[plaintextsize]; - memset(plaintext, 0, sizeof(unsigned char) * plaintextsize); - size_t plaintextlen = 0; + /* + * 4. Send command to atserver + */ + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, commandsize - 1, recv, + recvsize, &recvlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; + } - const size_t responserawsize = 512; - unsigned char responseraw[responserawsize]; - memset(responseraw, 0, sizeof(unsigned char) * responserawsize); - size_t responserawlen = 0; + /* + * 5. Parse repsonse + */ + char *response = (char *)recv; - ret = atchops_base64_decode((unsigned char *)response, responselen, responseraw, responserawsize, &responserawlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - return ret; + if (!atclient_stringutils_starts_with(response, "data:")) { + if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { + ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); + goto exit; } + } - ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, responseraw, responserawlen, plaintext, plaintextsize, - &plaintextlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); - return ret; - } + char *response_without_data = response + 5; // skip "data:" - memcpy(enc_key_shared_by_me, plaintext, plaintextsize); - } - - else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { - // or do I need to create, store and share a new shared key? - if (create_new_if_not_found) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Creating new shared encryption key for %s\n", - recipient_atsign_with_at); - ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, sender_atsign_with_at, - recipient_atsign_with_at, enc_key_shared_by_me); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_create_shared_encryption_key: %d\n", ret); - return ret; - } - } else { - ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; - return ret; - } + /* + * 6. Allocate memory for public_encryption_key and give output to caller + */ + const size_t public_encryption_key_size = strlen(response_without_data) + 1; + if ((*public_encryption_key = (char *)malloc(sizeof(char) * public_encryption_key_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for public_encryption_key\n"); + goto exit; } - return 0; + memcpy(*public_encryption_key, response_without_data, public_encryption_key_size); + *public_encryption_key[public_encryption_key_size - 1] = '\0'; + + ret = 0; +exit: { return ret; } } -int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char *recipient_atsign, - char *enc_key_shared_by_other) { +int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *recipient_atsign, + unsigned char *shared_encryption_key_shared_by_me) { int ret = 1; - char *sender_atsign_with_at = NULL; - char *sender_atsign_without_at = NULL; - - char *recipient_atsign_with_at = NULL; - char *recipient_atsign_without_at = NULL; + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } - if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if (recipient_atsign == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recipient_atsign is NULL\n"); return ret; } - if ((ret = atclient_stringutils_atsign_without_at(sender_atsign_with_at, &sender_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if (shared_encryption_key_shared_by_me == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_encryption_key_shared_by_me is NULL\n"); return ret; } - if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + /* + * 2. Variables + */ + char *sender_atsign_with_at = NULL; + char *recipient_atsign_with_at = NULL; + + char *command = NULL; + + const size_t recvsize = 1024; + unsigned char recv[recvsize]; + + const size_t key_raw_encrypted_size = 1024; + unsigned char key_raw_encrypted[key_raw_encrypted_size]; + memset(key_raw_encrypted, 0, sizeof(unsigned char) * key_raw_encrypted_size); + size_t key_raw_encrypted_len = 0; + + const size_t key_raw_decrypted_size = 1024; + unsigned char key_raw_decrypted[key_raw_decrypted_size]; + memset(key_raw_decrypted, 0, sizeof(unsigned char) * key_raw_decrypted_size); + size_t key_raw_decrypted_len = 0; + + /* + * 3. Build llookup: command + */ + if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); - return ret; + goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(recipient_atsign_with_at, &recipient_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); - return ret; + if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; } - // llookup:cached:@myatsign:shared_key@recipient_atsign - // lookup:shared_key@recipient_atsign - char *command_prefix = "lookup:shared_key@"; - const short command_prefix_len = 18; + char *sender_atsign_without_at = sender_atsign_with_at + 1; + char *recipient_atsign_without_at = recipient_atsign_with_at + 1; - short commandsize = command_prefix_len + strlen(recipient_atsign_without_at) + 3; - char command[commandsize]; - snprintf(command, commandsize, "lookup:shared_key@%s\r\n", recipient_atsign_without_at); + // llookup:shared_key.recipient_atsign@myatsign + const short commandsize = strlen("llookup:shared_key.") + strlen(recipient_atsign_without_at) + + strlen(sender_atsign_with_at) + strlen("\r\n") + 1; + if ((command = (char *)malloc(sizeof(char) * commandsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + goto exit; + } + snprintf(command, commandsize, "llookup:shared_key.%s%s\r\n", recipient_atsign_without_at, sender_atsign_with_at); - const size_t recvsize = 1024; - unsigned char recv[recvsize]; + /* + * 4. Send command to atserver + */ memset(recv, 0, sizeof(unsigned char) * recvsize); size_t recvlen = 0; - - ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, strlen((char *)command), recv, - recvsize, &recvlen); - if (ret != 0) { - return ret; + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, commandsize - 1, recv, + recvsize, &recvlen)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; } + /* + * 5. Parse response + */ char *response = (char *)recv; - // Truncate response: "@" + myatsign + "@" - short response_prefix_len = (short)strlen(sender_atsign_without_at) + 3; - char response_prefix[response_prefix_len]; - snprintf(response_prefix, response_prefix_len, "@%s@", sender_atsign_without_at); - - if (atclient_stringutils_starts_with(response, response_prefix)) { - response = response + response_prefix_len; + if (!atclient_stringutils_starts_with(response, "data:")) { + if (atclient_stringutils_starts_with(response, "error:AT0015-key not found")) { + ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); + goto exit; + } } - if (atclient_stringutils_ends_with(response, response_prefix)) { - response[strlen(response) - response_prefix_len - 1] = '\0'; + char *response_without_data = response + 5; // skip "data:" + const size_t response_without_data_len = strlen(response_without_data); + + /* + * 6. Decrypt and return it + */ + if ((ret = atchops_base64_decode((unsigned char *)response_without_data, response_without_data_len, key_raw_encrypted, + key_raw_encrypted_size, &key_raw_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; } - // does my atSign already have the recipient's shared key? - if (atclient_stringutils_starts_with(response, "data:")) { + if ((ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, key_raw_encrypted, key_raw_encrypted_len, + key_raw_decrypted, key_raw_decrypted_size, &key_raw_decrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); + goto exit; + } - response = response + 5; + if ((ret = atchops_base64_decode((unsigned char *)key_raw_decrypted, key_raw_decrypted_len, + shared_encryption_key_shared_by_me, ATCHOPS_AES_256 / 8, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; + } - // 44 + 1 - const size_t plaintextlen = 45; - unsigned char plaintext[plaintextlen]; - memset(plaintext, 0, plaintextlen); - size_t plaintextolen = 0; +exit: { + free(command); + free(sender_atsign_with_at); + free(recipient_atsign_with_at); + return ret; +} +} - const size_t responserawsize = 1024; - unsigned char responseraw[responserawsize]; - memset(responseraw, 0, sizeof(unsigned char) * responserawsize); - size_t responserawlen = 0; +int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char *recipient_atsign, + unsigned char *shared_encryption_key_shared_by_other) { + int ret = 1; - ret = atchops_base64_decode((unsigned char *)response, strlen(response), responseraw, responserawsize, - &responserawlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - return ret; - } + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } - ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, responseraw, responserawlen, plaintext, plaintextlen, - &plaintextolen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); - return ret; - } + if (recipient_atsign == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recipient_atsign is NULL\n"); + return ret; + } - memcpy(enc_key_shared_by_other, plaintext, plaintextlen); - } else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { - // There is nothing we can do, except wait for the recipient to share a new key - // We want to mark this situation with a easily distinguishable return value - ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; + if (shared_encryption_key_shared_by_other == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_encryption_key_shared_by_other is NULL\n"); return ret; } - return 0; -} + /* + * 2. Variables + */ + char *sender_atsign_with_at = NULL; + char *recipient_atsign_with_at = NULL; + char *command = NULL; -int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char *public_encryption_key) { + const size_t recvsize = 1024; + unsigned char recv[recvsize]; - int ret = 1; + const size_t shared_encryption_key_encrypted_base64_size = 1024; + unsigned char shared_encryption_key_encrypted_base64[shared_encryption_key_encrypted_base64_size]; - bool should_free_atsign_with_at = false; - char *atsign_with_at = ctx->atsign; + const size_t shared_encryption_key_encrypted_size = 1024; + unsigned char shared_encryption_key_encrypted[shared_encryption_key_encrypted_size]; - if (atsign != NULL) { - if ((ret = atclient_stringutils_atsign_with_at(atsign, &atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); - return ret; - } - should_free_atsign_with_at = true; + /* + * 3. Build lookup: command + */ + if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; } - // plookup:publickey@atsign - char *command_prefix = "plookup:publickey"; - const short command_prefix_len = 17; + if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + goto exit; + } - short command_len = command_prefix_len + strlen(atsign_with_at) + 3; - char command[command_len]; - snprintf(command, command_len, "plookup:publickey%s\r\n", atsign_with_at); + short commandsize = strlen("lookup:shared_key") + strlen(recipient_atsign_with_at) + strlen("\r\n") + 1; + if ((command = (char *)malloc(sizeof(char) * commandsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + goto exit; + } + snprintf(command, commandsize, "lookup:shared_key%s\r\n", recipient_atsign_with_at); - // execute command - const size_t recvlen = 1024; - unsigned char recv[recvlen]; - memset(recv, 0, sizeof(unsigned char) * recvlen); - size_t olen = 0; + /* + * 4. Send command to atserver + */ + memset(recv, 0, sizeof(unsigned char) * recvsize); + size_t recvlen = 0; - ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, strlen((char *)command), recv, - recvlen, &olen); - if (ret != 0) { + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, strlen((char *)command), + recv, recvsize, &recvlen)) != 0) { return ret; } + /* + * 5. Parse response + */ char *response = (char *)recv; + char *response_without_data = response + 5; // skip "data:" - if (atclient_stringutils_starts_with(response, "data:")) { - response = response + 5; - memcpy(public_encryption_key, response, 1024); - } else if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); - ret = 1; + if (!atclient_stringutils_starts_with(response, "data:")) { + if (atclient_stringutils_starts_with(response, "error:AT0015-key not found")) { + ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key not found\n", ret); + return ret; + } + } + memset(shared_encryption_key_encrypted_base64, 0, + sizeof(unsigned char) * shared_encryption_key_encrypted_base64_size); + size_t shared_encryption_key_encrypted_base64_len = 0; + + if ((ret = atchops_base64_decode((unsigned char *)response_without_data, strlen(response_without_data), + shared_encryption_key_encrypted_base64, shared_encryption_key_encrypted_base64_size, + &shared_encryption_key_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); return ret; } - ret = 0; -exit: { - if (should_free_atsign_with_at) { - free(atsign_with_at); + memset(shared_encryption_key_encrypted, 0, sizeof(unsigned char) * shared_encryption_key_encrypted_size); + size_t shared_encryption_key_encrypted_len = 0; + + if ((ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, shared_encryption_key_encrypted_base64, + shared_encryption_key_encrypted_base64_len, shared_encryption_key_encrypted, + shared_encryption_key_encrypted_size, &shared_encryption_key_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); + return ret; } + + const size_t shared_encryption_key_shared_by_other_size = ATCHOPS_AES_256 / 8; + if ((ret = atchops_base64_decode(shared_encryption_key_encrypted, shared_encryption_key_encrypted_len, + shared_encryption_key_shared_by_other, shared_encryption_key_shared_by_other_size, + NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + return ret; + } + +exit: { + free(command); + free(sender_atsign_with_at); + free(recipient_atsign_with_at); return ret; } } -int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclient, const char *sharedby, - const char *sharedwith, char *sharedenckeybyme) { +int atclient_create_shared_encryption_key_pair_for_me_and_other( + atclient *atclient, const char *sharedby, const char *sharedwith, + unsigned char *shared_encryption_key_shared_by_me_with_other) { + int ret = 1; - // 1. variables - // holds unencrypted shared encryption key in base 64 format - const size_t sharedenckeybase64size = 64; - unsigned char sharedenckeybase64[sharedenckeybase64size]; - memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); - size_t sharedenckeybase64len = 0; + /* + * 1. Validate arguments + */ + if (atclient == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + return ret; + } - // encrypted for us - const size_t sharedenckeyencryptedforussize = 512; - unsigned char sharedenckeyencryptedforus[sharedenckeyencryptedforussize]; - memset(sharedenckeyencryptedforus, 0, sizeof(unsigned char) * sharedenckeyencryptedforussize); - size_t sharedenckeyencryptedforuslen = 0; + if (sharedby == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL\n"); + return ret; + } - const size_t sharedenckeybase64encryptedforussize = 512; - char sharedenckeybase64encryptedforus[sharedenckeybase64encryptedforussize]; - memset(sharedenckeybase64encryptedforus, 0, sizeof(char) * sharedenckeybase64encryptedforussize); - size_t sharedenckeybase64encryptedforuslen = 0; + if (sharedwith == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL\n"); + return ret; + } - // encrypted for them - const size_t sharedenckeyencryptedforthemsize = 512; - unsigned char sharedenckeyencryptedforthem[sharedenckeyencryptedforthemsize]; - memset(sharedenckeyencryptedforthem, 0, sizeof(unsigned char) * sharedenckeyencryptedforthemsize); - size_t sharedenckeyencryptedforthemlen = 0; - - const size_t sharedenckeybase64encryptedforthemsize = 512; - char sharedenckeybase64encryptedforthem[sharedenckeybase64encryptedforthemsize]; - memset(sharedenckeybase64encryptedforthem, 0, sizeof(char) * sharedenckeybase64encryptedforthemsize); - size_t sharedenckeybase64encryptedforthemlen = 0; - - // their public encyrption key - const size_t publickeybase64size = 1024; - char publickeybase64[publickeybase64size]; - memset(publickeybase64, 0, sizeof(char) * publickeybase64size); - - const size_t recvsize = 2048; - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + if (shared_encryption_key_shared_by_me_with_other == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_encryption_key_shared_by_me_with_other is NULL\n"); + return ret; + } - char *cmdbuffer1 = NULL; - char *cmdbuffer2 = NULL; + /* + * 2. Variables + */ + // holds formatted atSigns char *sharedby_atsign_with_at = NULL; - char *sharedby_atsign_without_at = NULL; - char *sharedwith_atsign_with_at = NULL; - char *sharedwith_atsign_without_at = NULL; + // holds their public key in base64 format, non-encrypted + char *publickeybase64 = NULL; + + // holds their public key in base64 format, non-encrypted (struct) atchops_rsakey_publickey publickeystruct; atchops_rsakey_publickey_init(&publickeystruct); + // the original AES-256 key + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key[shared_encryption_key_size]; + + // the original AES-256 key (base64 encoded) + const size_t shared_encryption_key_base64_size = atchops_base64_encoded_size(shared_encryption_key_size); + unsigned char shared_encryption_key_base64[shared_encryption_key_base64_size]; + + // encrypted for us + const size_t shared_encryption_key_base64_encrypted_for_us_size = 512; + unsigned char shared_encryption_key_base64_encrypted_for_us[shared_encryption_key_base64_encrypted_for_us_size]; + + // encrypted for us (base64 encoded) + const size_t shared_encryption_key_base64_encrypted_for_us_base64_size = + atchops_base64_encoded_size(shared_encryption_key_base64_encrypted_for_us_size); + unsigned char + shared_encryption_key_base64_encrypted_for_us_base64[shared_encryption_key_base64_encrypted_for_us_base64_size]; + + // encrypted for them + const size_t shared_encryption_key_base64_encrypted_for_them_size = 512; + unsigned char shared_encryption_key_base64_encrypted_for_them[shared_encryption_key_base64_encrypted_for_them_size]; + + // encrypted for them (base64 encoded) + const size_t shared_encryption_key_base64_encrypted_for_them_base64_size = + atchops_base64_encoded_size(shared_encryption_key_base64_encrypted_for_them_size); + unsigned char shared_encryption_key_base64_encrypted_for_them_base64 + [shared_encryption_key_base64_encrypted_for_them_base64_size]; + + char *cmdbuffer1 = NULL; // for us (update:shared_key.sharedwith@sharedby command) + char *cmdbuffer2 = NULL; // for them (update:@sharedwith:shared_key@sharedby command) + + const size_t recvsize = 256; // sufficient to receive response from a update: command + unsigned char recv[recvsize]; + memset(recv, 0, sizeof(unsigned char) * recvsize); + size_t recvlen = 0; + + /* + * 2. Ensure atSigns start with `@` symbol + */ if ((ret = atclient_stringutils_atsign_with_at(sharedby, &sharedby_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); - return ret; - } - - if ((ret = atclient_stringutils_atsign_without_at(sharedby_atsign_with_at, &sharedby_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); - return ret; + goto exit; } + char *sharedby_atsign_without_at = sharedby_atsign_with_at + 1; if ((ret = atclient_stringutils_atsign_with_at(sharedwith, &sharedwith_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); - return ret; + goto exit; } + char *sharedwith_atsign_without_at = sharedwith_atsign_with_at + 1; - if ((ret = atclient_stringutils_atsign_without_at(sharedwith_atsign_with_at, &sharedwith_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); - return ret; + /* + * 3. Get publickey of sharedwith atSign + */ + if ((ret = atclient_get_public_encryption_key(atclient, sharedwith_atsign_with_at, &publickeybase64)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_encryption_key: %d\n", ret); + goto exit; } - // 2. generate shared encryption key - ret = atchops_aes_generate_keybase64(sharedenckeybase64, sharedenckeybase64size, &sharedenckeybase64len, - ATCHOPS_AES_256); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_generate_keybase64: %d\n", ret); - return ret; + if ((ret = atchops_rsakey_populate_publickey(&publickeystruct, publickeybase64, strlen(publickeybase64))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d\n", ret); + goto exit; } - // 3. encrypt for us (with encrypt public key) - ret = - atchops_rsa_encrypt(atclient->atkeys.encryptpublickey, (unsigned char *)sharedenckeybase64, sharedenckeybase64len, - sharedenckeyencryptedforus, sharedenckeyencryptedforussize, &sharedenckeyencryptedforuslen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "failed to encrypt shared enc key for us | atchops_rsa_encrypt: %d\n", ret); - return ret; + /* + * 4. Generate 32-byte AES key + */ + memset(shared_encryption_key, 0, sizeof(unsigned char) * shared_encryption_key_size); + if ((ret = atchops_aes_generate_key(shared_encryption_key, ATCHOPS_AES_256)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_generate_key: %d\n", ret); + goto exit; } - ret = atchops_base64_encode(sharedenckeyencryptedforus, sharedenckeyencryptedforuslen, - (unsigned char *)sharedenckeybase64encryptedforus, sharedenckeybase64encryptedforussize, - &sharedenckeybase64encryptedforuslen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "failed to base64 encode shared enc key for us | atchops_base64_encode: %d\n", ret); - return ret; + memset(shared_encryption_key_base64, 0, sizeof(char) * shared_encryption_key_base64_size); + size_t shared_encryption_key_base64_len = 0; + if ((ret = atchops_base64_encode(shared_encryption_key, shared_encryption_key_size, shared_encryption_key_base64, + shared_encryption_key_base64_size, &shared_encryption_key_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto exit; } - // 4. encrypt for them (with their rsa public encryption key) - ret = atclient_get_public_encryption_key(atclient, sharedwith, publickeybase64); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_encryption_key: %d\n", ret); - return ret; + /* + * 5. Do encryption stuff + * a. Encrypt for us + * b. Encrypt for them + */ + + // 5a. Encrypt for us + size_t shared_encryption_key_base64_encrypted_for_us_len = 0; + memset(shared_encryption_key_base64_encrypted_for_us, 0, + sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_us_size); + if ((ret = atchops_rsa_encrypt(atclient->atkeys.encryptpublickey, (unsigned char *)shared_encryption_key_base64, + shared_encryption_key_base64_len, shared_encryption_key_base64_encrypted_for_us, + shared_encryption_key_base64_encrypted_for_us_size, + &shared_encryption_key_base64_encrypted_for_us_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "failed to encrypt shared enc key for us | atchops_rsa_encrypt: %d\n", ret); + goto exit; } - // create a rsakey public (atchops) - ret = atchops_rsakey_populate_publickey(&publickeystruct, publickeybase64, strlen(publickeybase64)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d\n", ret); - return ret; + size_t shared_encryption_key_base64_encrypted_for_us_base64_len = 0; + memset(shared_encryption_key_base64_encrypted_for_us_base64, 0, + sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_us_base64_size); + if ((ret = atchops_base64_encode(shared_encryption_key_base64_encrypted_for_us, + shared_encryption_key_base64_encrypted_for_us_len, + shared_encryption_key_base64_encrypted_for_us_base64, + shared_encryption_key_base64_encrypted_for_us_base64_size, + &shared_encryption_key_base64_encrypted_for_us_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "failed to base64 encode shared enc key for us | atchops_base64_encode: %d\n", ret); + goto exit; } - // encrypt the symmetric key with their rsa public key - ret = atchops_rsa_encrypt(publickeystruct, (unsigned char *)sharedenckeybase64, sharedenckeybase64len, - sharedenckeyencryptedforthem, sharedenckeyencryptedforthemsize, - &sharedenckeyencryptedforthemlen); - if (ret != 0) { + // 5b. Encrypt for them + size_t shared_encryption_key_base64_encrypted_for_them_len = 0; + memset(shared_encryption_key_base64_encrypted_for_them, 0, + sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_them_size); + if ((ret = atchops_rsa_encrypt(publickeystruct, shared_encryption_key_base64, shared_encryption_key_base64_len, + shared_encryption_key_base64_encrypted_for_them, + shared_encryption_key_base64_encrypted_for_them_size, + &shared_encryption_key_base64_encrypted_for_them_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_encrypt: %d\n", ret); - return ret; + goto exit; } - ret = atchops_base64_encode(sharedenckeyencryptedforthem, sharedenckeyencryptedforthemlen, - (unsigned char *)sharedenckeybase64encryptedforthem, - sharedenckeybase64encryptedforthemsize, &sharedenckeybase64encryptedforthemlen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "failed to base64 encode shared enc key for them | atchops_base64_encode: %d\n", ret); + size_t shared_encryption_key_base64_encrypted_for_them_base64_len = 0; + memset(shared_encryption_key_base64_encrypted_for_them_base64, 0, + sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_them_base64_size); + if ((ret = atchops_base64_encode(shared_encryption_key_base64_encrypted_for_them, + shared_encryption_key_base64_encrypted_for_them_len, + shared_encryption_key_base64_encrypted_for_them_base64, + shared_encryption_key_base64_encrypted_for_them_base64_size, + &shared_encryption_key_base64_encrypted_for_them_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } - // 5. prep protocol commands - // 5a. for us (update:shared_key.sharedby@sharedwith \r\n) - const size_t cmdbuffersize1 = strlen("update:shared_key. \r\n") + strlen(sharedwith_atsign_without_at) + - strlen(sharedby_atsign_with_at) + 1 + sharedenckeybase64encryptedforuslen; - cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1); - if (cmdbuffer1 == NULL) { + /* + * 6. Build `update:` commands + */ + // 6a. `update:shared_key.recipient_atsign@myatsign ` + const size_t cmdbuffersize1 = strlen("update:shared_key.") + strlen(sharedwith_atsign_without_at) + + strlen(sharedby_atsign_with_at) + strlen(" ") + + shared_encryption_key_base64_encrypted_for_us_base64_len + strlen("\r\n") + 1; + if ((cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer1\n"); goto exit; } snprintf(cmdbuffer1, cmdbuffersize1, "update:shared_key.%s%s %s\r\n", sharedwith_atsign_without_at, - sharedby_atsign_with_at, sharedenckeybase64encryptedforus); + sharedby_atsign_with_at, shared_encryption_key_base64_encrypted_for_us_base64); + + // 6b. `update:@sharedwith:shared_key@sharedby ` + const size_t cmdbuffersize2 = strlen("update:") + strlen(sharedwith_atsign_with_at) + strlen(":shared_key") + + strlen(sharedby_atsign_with_at) + strlen(" ") + + shared_encryption_key_base64_encrypted_for_them_base64_len + strlen("\r\n") + 1; - // 5b. for them (update:shared_key.sharedwith@sharedby \r\n) - const size_t cmdbuffersize2 = strlen("update::shared_key \r\n") + strlen(sharedby_atsign_with_at) + - strlen(sharedwith_atsign_with_at) + 1 + sharedenckeybase64encryptedforthemlen; if ((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer2\n"); goto exit; } snprintf(cmdbuffer2, cmdbuffersize2, "update:%s:shared_key%s %s\r\n", sharedwith_atsign_with_at, - sharedby_atsign_with_at, sharedenckeybase64encryptedforthem); + sharedby_atsign_with_at, shared_encryption_key_base64_encrypted_for_them_base64); - // 6. put "encrypted for us" into key store - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer1, cmdbuffersize1 - 1, - recv, recvsize, &recvlen); - if (ret != 0) { + /* + * 7. Send commands to atserver + */ + + // 7a. Our key + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer1, cmdbuffersize1 - 1, + recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - // check if the key was successfully stored if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -465,16 +583,15 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie } memset(recv, 0, sizeof(unsigned char) * recvsize); + recvlen = 0; - // 6. put "encrypted for them" into key store - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer2, cmdbuffersize2 - 1, - recv, recvsize, &recvlen); - if (ret != 0) { + // 7b. Their key + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer2, cmdbuffersize2 - 1, + recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - // check if the key was successfully stored if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -482,20 +599,20 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other(atclient *atclie goto exit; } - // 7. return shared encryption key by me - memcpy(sharedenckeybyme, sharedenckeybase64, sharedenckeybase64len); + /* + * 8. Return output + */ + memcpy(shared_encryption_key_shared_by_me_with_other, shared_encryption_key, shared_encryption_key_size); ret = 0; goto exit; - exit: { + free(sharedby_atsign_with_at); + free(sharedwith_atsign_with_at); atchops_rsakey_publickey_free(&publickeystruct); + free(publickeybase64); free(cmdbuffer1); free(cmdbuffer2); - free(sharedby_atsign_with_at); - free(sharedby_atsign_without_at); - free(sharedwith_atsign_with_at); - free(sharedwith_atsign_without_at); return ret; } } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index ee3a2168..fdbd6957 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -107,27 +107,18 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific } } else { char *recipient = NULL; - const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; - unsigned char sharedenckeybase64[sharedenckeybase64size]; - memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", ret); return ret; } - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient, (char *)sharedenckeybase64, true)) != + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient, sharedenckey)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", ret); free(recipient); return ret; } - if ((ret = atchops_base64_decode(sharedenckeybase64, strlen((char *)sharedenckeybase64), sharedenckey, - sharedenckeysize, &sharedenckeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", ret); - free(recipient); - return ret; - } free(recipient); } diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 70801382..7f022183 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -161,7 +161,7 @@ static int test_2_get_as_sharedby(atclient *atclient) { goto exit; } - if ((ret = atclient_get_sharedkey(atclient, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) { + if ((ret = atclient_get_sharedkey(atclient, &atkey, value, valuesize, &valuelen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -217,7 +217,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { goto exit; } - if ((ret = atclient_get_sharedkey(atclient2, &atkey, value, valuesize, &valuelen, NULL, false)) != 0) { + if ((ret = atclient_get_sharedkey(atclient2, &atkey, value, valuesize, &valuelen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } From 4a721fd101f61588a5aec7feb1bcab1fb680f569 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:42:48 -0400 Subject: [PATCH 097/193] feat: cleaned up encryption_key_helpers --- .../include/atclient/encryption_key_helpers.h | 3 +- .../atclient/src/atclient_get_sharedkey.c | 42 ++++++++++++++----- packages/atclient/src/atclient_put.c | 36 ++++++++++++---- .../atclient/src/encryption_key_helpers.c | 23 +++++----- packages/atclient/src/notify.c | 23 ++++++---- 5 files changed, 83 insertions(+), 44 deletions(-) diff --git a/packages/atclient/include/atclient/encryption_key_helpers.h b/packages/atclient/include/atclient/encryption_key_helpers.h index 78b4fd5c..a47c73c7 100644 --- a/packages/atclient/include/atclient/encryption_key_helpers.h +++ b/packages/atclient/include/atclient/encryption_key_helpers.h @@ -55,7 +55,6 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char * (shared_key.other@me and @other:shared_key@me) * * @param atclient non-null atclient context (must be initialized and pkam_authenticated) - * @param sharedby non-null atSign (with @ symbol) and null-terminated. Example "@bob" * @param sharedwith non-null atSign (with @ symbol) and null-terminated. Example "@bob" * @param shared_encryption_key_shared_by_me_with_other holds the output shared encryption key that was created by this * function, to be shared with the recipient. Expected to be non-null and has 32 bytes of memory allocated to this @@ -63,7 +62,7 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char * @return int 0 on success, error otherwise */ int atclient_create_shared_encryption_key_pair_for_me_and_other( - atclient *atclient, const char *sharedby, const char *sharedwith, + atclient *atclient, const char *sharedwith, unsigned char *shared_encryption_key_shared_by_me_with_other); #endif // ATCLIENT_ENCRYPTION_KEY_HELPERS_H diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index a640f59d..f272c8f6 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -97,6 +97,8 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at char *value_raw_encrypted = NULL; char *value_raw = NULL; + const cJSON *root = NULL; + /* * 3. Format atSigns */ @@ -164,7 +166,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at char *response_without_data = response + 5; - const cJSON *root = cJSON_Parse(response); + root = cJSON_Parse(response_without_data); if (root == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); @@ -211,7 +213,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at const char *value_raw_encrypted_base64 = data->valuestring; const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); - const size_t value_raw_encrypted_size = atchops_base64_decoded_size(strlen(value_raw_encrypted_base64_len)); + const size_t value_raw_encrypted_size = atchops_base64_decoded_size(value_raw_encrypted_base64_len); if ((value_raw_encrypted = malloc(sizeof(char) * value_raw_encrypted_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw_encrypted\n"); @@ -235,14 +237,19 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at } memset(value_raw, 0, sizeof(char) * value_raw_size); size_t value_raw_len = 0; - if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, (unsigned char *)value_raw_encrypted, value_raw_encrypted_len, - (unsigned char *)value_raw, value_raw_size, &valuelen)) != 0) { + (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } + /* + * 10. Set value + */ + memcpy(value, value_raw, value_raw_len); + *valuelen = value_raw_len; + ret = 0; goto exit; exit: { @@ -251,7 +258,8 @@ exit: { free(value_raw); free(atkeystr); free(command); - free(recv); + free(metadatastr); + cJSON_Delete(root); return ret; } } @@ -276,7 +284,9 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at unsigned char recv[recvsize]; char *command = NULL; + cJSON *root = NULL; + char *metadatastr = NULL; const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[ivsize]; @@ -284,7 +294,6 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at unsigned char *value_raw_encryted = NULL; unsigned char *value_raw = NULL; - /* * 3. Format atSigns */ @@ -316,13 +325,22 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 5. Build lookup: command */ - const size_t commandsize = strlen("lookup:") + strlen(atkey->key) + strlen(sender_atsign_with_at) + ("\r\n") + 1; - if ((command = malloc(sizeof(char) * commandsize)) == NULL) { + size_t commandsize = strlen("lookup:") + strlen(atkey->key); + bool namespace_exists = atclient_atkey_is_namespacestr_initialized(atkey); + if (namespace_exists) { + commandsize += strlen(".") + strlen(atkey->namespacestr); + } + commandsize += strlen(sender_atsign_with_at) + strlen("\r\n") + 1; + if ((command = (char *)malloc(sizeof(char) * commandsize)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); goto exit; } - snprintf(command, commandsize, "lookup:%s%s\r\n", atkey->key, sender_atsign_with_at); + if (namespace_exists) { + snprintf(command, commandsize, "lookup:%s.%s%s\r\n", atkey->key, atkey->namespacestr, sender_atsign_with_at); + } else { + snprintf(command, commandsize, "lookup:%s%s\r\n", atkey->key, sender_atsign_with_at); + } /* * 6. Send lookup: command @@ -363,7 +381,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at goto exit; } - char *metadatastr = cJSON_Print(metadata); + metadatastr = cJSON_Print(metadata); if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); @@ -422,7 +440,8 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encryted, - value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { + value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, + &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } @@ -431,6 +450,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at * 10. Set value */ memcpy(value, value_raw, value_raw_len); + *valuelen = value_raw_len; ret = 0; goto exit; diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 65593b77..dd9d20bd 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -132,41 +132,60 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const // if it doesn't exist, create one for us and create one for the other person // create one for us -> encrypted with our self encryption key // create one for the other person -> encrypted with their public encryption key - char *recipient_atsign = atkey->sharedwith; + char *recipient_atsign_with_at = NULL; - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign, sharedenckey)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); - goto exit; + if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_concat_at_sign_with_at: %d\n", ret); + goto shared_key_exit; + } + + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, sharedenckey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Creating shared encryption key\n"); + if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, + sharedenckey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); + goto shared_key_exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Created shared encryption key successfully\n"); } if ((ret = atchops_iv_generate(iv)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); - goto exit; + goto shared_key_exit; } if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)ivbase64, ivbase64size, &ivbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto exit; + goto shared_key_exit; } if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); - goto exit; + goto shared_key_exit; } if ((ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, ciphertextsize, &ciphertextlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); - goto exit; + goto shared_key_exit; } if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, &ciphertextbase64len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto shared_key_exit; + } + + shared_key_exit: { + free(recipient_atsign_with_at); + if (ret != 0) { goto exit; } } + } // 3b. Build the command @@ -236,7 +255,6 @@ exit: { } } - static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, const size_t valuelen, const int *commitid) { int ret = 1; diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index cbfeb7cf..a13f9a0c 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -95,15 +95,16 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char * /* * 6. Allocate memory for public_encryption_key and give output to caller */ - const size_t public_encryption_key_size = strlen(response_without_data) + 1; + const size_t public_encryption_key_len = strlen(response_without_data); + const size_t public_encryption_key_size = public_encryption_key_len + 1; if ((*public_encryption_key = (char *)malloc(sizeof(char) * public_encryption_key_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for public_encryption_key\n"); goto exit; } - memcpy(*public_encryption_key, response_without_data, public_encryption_key_size); - *public_encryption_key[public_encryption_key_size - 1] = '\0'; + memcpy(*public_encryption_key, response_without_data, public_encryption_key_len); + (*public_encryption_key)[public_encryption_key_len] = '\0'; ret = 0; exit: { return ret; } @@ -198,7 +199,7 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r if (!atclient_stringutils_starts_with(response, "data:")) { if (atclient_stringutils_starts_with(response, "error:AT0015-key not found")) { ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d; error:AT0015-key not found\n", ret); goto exit; } } @@ -356,7 +357,7 @@ exit: { } int atclient_create_shared_encryption_key_pair_for_me_and_other( - atclient *atclient, const char *sharedby, const char *sharedwith, + atclient *atclient, const char *recipient_atsign, unsigned char *shared_encryption_key_shared_by_me_with_other) { int ret = 1; @@ -369,13 +370,9 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( return ret; } - if (sharedby == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL\n"); - return ret; - } - if (sharedwith == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL\n"); + if (recipient_atsign == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recipient_atsign is NULL\n"); return ret; } @@ -438,13 +435,13 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( /* * 2. Ensure atSigns start with `@` symbol */ - if ((ret = atclient_stringutils_atsign_with_at(sharedby, &sharedby_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &sharedby_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } char *sharedby_atsign_without_at = sharedby_atsign_with_at + 1; - if ((ret = atclient_stringutils_atsign_with_at(sharedwith, &sharedwith_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &sharedwith_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index fdbd6957..5a8ffd61 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -106,20 +106,25 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific return ret; } } else { - char *recipient = NULL; - if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient)) != 0) { + char *recipient_atsign_with_at = NULL; + if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", ret); return ret; } - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient, sharedenckey)) != - 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_shared_encryption_key_shared_by_me failed with code %d\n", ret); - free(recipient); - return ret; + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, sharedenckey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Creating shared encryption key\n"); + if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, + sharedenckey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); + free(recipient_atsign_with_at); + return ret; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Created shared encryption key successfully\n"); } - free(recipient); + free(recipient_atsign_with_at); } unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; From 5de629a0d2c31856f1e139be56a0574977624b96 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:43:29 -0400 Subject: [PATCH 098/193] fix: --- packages/atclient/src/atclient_get_sharedkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index f272c8f6..84f6f0e2 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -367,7 +367,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *response_without_data = response + 5; - root = cJSON_Parse(response); + root = cJSON_Parse(response_without_data); if (root == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); From 619b263395361c0ac779275e024df3a29b3839cf Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:44:56 -0400 Subject: [PATCH 099/193] fix: --- packages/atclient/src/atclient_get_sharedkey.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 84f6f0e2..2b617064 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -325,7 +325,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 5. Build lookup: command */ - size_t commandsize = strlen("lookup:") + strlen(atkey->key); + size_t commandsize = strlen("lookup:all:") + strlen(atkey->key); bool namespace_exists = atclient_atkey_is_namespacestr_initialized(atkey); if (namespace_exists) { commandsize += strlen(".") + strlen(atkey->namespacestr); @@ -337,9 +337,9 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at goto exit; } if (namespace_exists) { - snprintf(command, commandsize, "lookup:%s.%s%s\r\n", atkey->key, atkey->namespacestr, sender_atsign_with_at); + snprintf(command, commandsize, "lookup:all:%s.%s%s\r\n", atkey->key, atkey->namespacestr, sender_atsign_with_at); } else { - snprintf(command, commandsize, "lookup:%s%s\r\n", atkey->key, sender_atsign_with_at); + snprintf(command, commandsize, "lookup:all:%s%s\r\n", atkey->key, sender_atsign_with_at); } /* @@ -367,8 +367,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *response_without_data = response + 5; - root = cJSON_Parse(response_without_data); - if (root == NULL) { + if ((root = cJSON_Parse(response_without_data)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); goto exit; From a432d424fac4789c432f57d054f1e5b69921075a Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:47:13 -0400 Subject: [PATCH 100/193] fix: --- packages/atclient/src/atclient_get_sharedkey.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 2b617064..f7fcc8fd 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -280,6 +280,9 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *sender_atsign_with_at = NULL; char *recipient_atsign_with_at = NULL; + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key_to_use[shared_encryption_key_size]; + const size_t recvsize = 4096; unsigned char recv[recvsize]; @@ -310,8 +313,6 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 4. Get shared_encryption_key, if necessary */ - const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; - unsigned char shared_encryption_key_to_use[shared_encryption_key_size]; if (shared_encryption_key == NULL) { if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, atkey->sharedby, shared_encryption_key_to_use)) != 0) { @@ -415,7 +416,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); // 9a. base64 decode - const value_raw_encryted_size = atchops_base64_decoded_size(value_raw_encrypted_base64_len); + const size_t value_raw_encryted_size = atchops_base64_decoded_size(value_raw_encrypted_base64_len); if ((value_raw_encryted = malloc(sizeof(unsigned char) * value_raw_encryted_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw_encryted\n"); From 2767c35fb09caa9974914600418cde1363c6b487 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:50:49 -0400 Subject: [PATCH 101/193] fix: --- packages/atclient/src/atclient_get_sharedkey.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index f7fcc8fd..58c762d5 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -84,6 +84,10 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at */ char *client_atsign_with_at = NULL; char *recipient_atsign_with_at = NULL; + + unsigned char shared_encryption_key_to_use[ATCHOPS_AES_256 / 8]; + memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * ATCHOPS_AES_256 / 8); + char *atkeystr = NULL; char *command = NULL; @@ -97,7 +101,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at char *value_raw_encrypted = NULL; char *value_raw = NULL; - const cJSON *root = NULL; + cJSON *root = NULL; /* * 3. Format atSigns @@ -115,8 +119,6 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at /* * 4. Get shared_encryption_key, if necessary */ - unsigned char shared_encryption_key_to_use[ATCHOPS_AES_256 / 8]; - memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * ATCHOPS_AES_256 / 8); if (shared_encryption_key != NULL) { memcpy(shared_encryption_key_to_use, shared_encryption_key, ATCHOPS_AES_256 / 8); } else { From 49000a269076912e0d560019fef6b2d05c1e3cdd Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:52:51 -0400 Subject: [PATCH 102/193] fix: crud example --- examples/desktop/crud/get_sharedkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index ad7d4179..9382a385 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -94,7 +94,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - ret = atclient_get_sharedkey(&atclient, &atkey, value, valuesize, valuelen, NULL, false); + ret = atclient_get_sharedkey(&atclient, &atkey, value, valuesize, valuelen, NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared key"); goto exit; From df9466b01d0e2eac1ae042020cd18a55c7dcf185 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 18:54:41 -0400 Subject: [PATCH 103/193] fix: repl example --- examples/desktop/repl/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 7bd4f43a..b453c5db 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -169,7 +169,7 @@ int main(int argc, char *argv[]) { break; } case ATCLIENT_ATKEY_TYPE_SHAREDKEY: { - if ((ret = atclient_get_sharedkey(&atclient, &atkey, recv, recvsize, &recvlen, NULL, true)) != 0) { + if ((ret = atclient_get_sharedkey(&atclient, &atkey, recv, recvsize, &recvlen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey: %d | failed to get shared key\n", ret); goto get_end; From 57b63e400e5380b004fea45e7ec97c5b488b356c Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 19:27:07 -0400 Subject: [PATCH 104/193] feat: atclient_delete now returns commit_id --- examples/desktop/crud/delete.c | 2 +- examples/desktop/repl/src/main.c | 2 +- packages/atclient/include/atclient/atclient.h | 3 +- packages/atclient/src/atclient_delete.c | 63 +++++++++++++------ tests/functional_tests/lib/src/helpers.c | 4 +- .../tests/test_atclient_publickey.c | 4 +- .../tests/test_atclient_selfkey.c | 4 +- .../tests/test_atclient_sharedkey.c | 2 +- 8 files changed, 55 insertions(+), 29 deletions(-) diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index 235ad476..fe43d1ce 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -58,7 +58,7 @@ int main() { goto exit; } - if ((ret = atclient_delete(&atclient, &atkey)) != 0) { + if ((ret = atclient_delete(&atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to delete"); goto exit; } diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index b453c5db..515299aa 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -212,7 +212,7 @@ int main(int argc, char *argv[]) { } char *atkeystr = NULL; for(size_t i = 0; i < arrlen; i++) { - if((ret = atclient_delete(&atclient, &arr[i])) != 0) { + if((ret = atclient_delete(&atclient, &arr[i], NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d | failed to delete atKey\n", ret); continue; } diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 8342fb80..750cb051 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -229,9 +229,10 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to delete from atServer (must satisfy the two conditions stated above) + * @param commit_id the output commit_id of the delete operation that the atServer returns, can be set to NULL if you do not need it * @return int 0 on success */ -int atclient_delete(atclient *atclient, const atclient_atkey *atkey); +int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit_id); /** * @brief Runs a scan of the atServer to see what atKeys you have. diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index be7c98f9..4b2d6b3d 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -8,9 +8,9 @@ #define TAG "atclient_delete" -static int atclient_delete_validate_arguments(atclient *atclient, const atclient_atkey *atkey); +static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey); -int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { +int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit_id) { int ret = 1; /* @@ -19,20 +19,17 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { if ((ret = atclient_delete_validate_arguments(atclient, atkey)) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete_validate_arguments: %d\n", ret); - goto exit; + return ret; } /* * 2. Initialize variables */ - size_t cmdbuffersize; - char *cmdbuffer = NULL; // free later - - char *atkeystr = NULL; // free later - size_t atkeystrlen = 0; + char *cmdbuffer = NULL; + char *atkeystr = NULL; - const size_t recvsize = 4096; - unsigned char *recv; + const size_t recvsize = 256; // sufficient buffer size to receive response containing commit id + unsigned char *recv = NULL; if (!atclient->async_read) { recv = malloc(sizeof(unsigned char) * recvsize); if (recv == NULL) { @@ -52,16 +49,16 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - atkeystrlen = strlen(atkeystr); + const size_t atkeystrlen = strlen(atkeystr); - cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; + const size_t cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; cmdbuffer = malloc(sizeof(char) * cmdbuffersize); if (cmdbuffer == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); goto exit; } - snprintf(cmdbuffer, cmdbuffersize, "delete:%.*s\r\n", (int)atkeystrlen, atkeystr); + snprintf(cmdbuffer, cmdbuffersize, "delete:%s\r\n", atkeystr); /* * 4. Send command @@ -76,26 +73,32 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey) { goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + char *respose = (char *)recv; + + if (!atclient_stringutils_starts_with(respose, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); goto exit; } + char *response_without_data = respose + strlen("data:"); + + if(commit_id != NULL) { + *commit_id = atoi(response_without_data); + } + ret = 0; goto exit; exit: { - if (!atclient->async_read) { - free(recv); - } + free(recv); free(atkeystr); free(cmdbuffer); return ret; } } -static int atclient_delete_validate_arguments(atclient *atclient, const atclient_atkey *atkey) { +static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey) { int ret = 1; if (atclient == NULL) { @@ -104,13 +107,35 @@ static int atclient_delete_validate_arguments(atclient *atclient, const atclient goto exit; } + if (!atclient_is_atsign_initialized(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver_connection is not connected\n"); + goto exit; + } + + if (!atclient_is_atserver_connection_started(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver_connection is not started\n"); + goto exit; + } + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); goto exit; } - // TODO use a function in atclient_atkey to check if atkey is complete + if (!atclient_atkey_is_key_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_key_initialized is false\n"); + goto exit; + } + + if (!atclient_atkey_is_sharedby_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_sharedby_initialized is false\n"); + goto exit; + } ret = 0; goto exit; diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index d57c033f..d3925246 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -284,13 +284,13 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re goto exit; } - if ((ret = atclient_delete(atclient1, &atkeyforme)) != 0) { + if ((ret = atclient_delete(atclient1, &atkeyforme, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "deleted shared enc key for me\n"); - if ((ret = atclient_delete(atclient1, &atkeyforthem)) != 0) { + if ((ret = atclient_delete(atclient1, &atkeyforthem, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index bb60a2fe..9c7898d8 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -183,7 +183,7 @@ static int test_3_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } @@ -340,7 +340,7 @@ static int test_7_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index ea78f88c..ba377c1a 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -207,7 +207,7 @@ static int test_4_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } @@ -349,7 +349,7 @@ static int test_8_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 7f022183..6a30997c 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -268,7 +268,7 @@ static int test_4_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } From 7de98c9f2f4820fdd3e365071d16dda1981d7d1e Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 19:28:35 -0400 Subject: [PATCH 105/193] fix: typo --- packages/atclient/include/atclient/version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/include/atclient/version.h b/packages/atclient/include/atclient/version.h index 460b272f..adbc3a71 100644 --- a/packages/atclient/include/atclient/version.h +++ b/packages/atclient/include/atclient/version.h @@ -1,5 +1,5 @@ -#ifndef ATCLIENT_VERSIO_H -#define ATCLIENT_VERSIO_H +#ifndef ATCLIENT_VERSION_H +#define ATCLIENT_VERSION_H #define ATSDK_VERSION "0.0.1" From 8737a2f927cbbffc56ae42b9f6f46d3d752cfe85 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 19:32:50 -0400 Subject: [PATCH 106/193] fix: typo --- packages/atclient/include/atclient/monitor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 33bbd834..9e671879 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -1,5 +1,5 @@ -#ifndef NOTIFICATION_H -#define NOTIFICATION_H +#ifndef ATCLIENT_MONITOR_H +#define ATCLIENT_MONITOR_H #include "atclient/atclient.h" #include "atclient/atkey.h" From 9d9fe4292a9c26ba796d8b54beba8b69f5657fd5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 19:33:55 -0400 Subject: [PATCH 107/193] refactor: metadata symbolic constants --- packages/atclient/include/atclient/metadata.h | 122 +++++++------ packages/atclient/src/metadata.c | 170 +++++++++--------- 2 files changed, 145 insertions(+), 147 deletions(-) diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 16be7ed2..09491318 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -9,78 +9,76 @@ #define VALUE_INITIALIZED 0b00000001 -#define ATKEY_METADATA_CREATEDBY_INDEX 0 -#define ATKEY_METADATA_UPDATEDBY_INDEX 0 -#define ATKEY_METADATA_STATUS_INDEX 0 -#define ATKEY_METADATA_VERSION_INDEX 0 -#define ATKEY_METADATA_EXPIRESAT_INDEX 0 -#define ATKEY_METADATA_AVAILABLEAT_INDEX 0 -#define ATKEY_METADATA_REFRESHAT_INDEX 0 -#define ATKEY_METADATA_CREATEDAT_INDEX 0 - -#define ATKEY_METADATA_UPDATEDAT_INDEX 1 -#define ATKEY_METADATA_ISPUBLIC_INDEX 1 -#define ATKEY_METADATA_ISHIDDEN_INDEX 1 -#define ATKEY_METADATA_ISCACHED_INDEX 1 -#define ATKEY_METADATA_TTL_INDEX 1 -#define ATKEY_METADATA_TTB_INDEX 1 -#define ATKEY_METADATA_TTR_INDEX 1 -#define ATKEY_METADATA_CCD_INDEX 1 - -#define ATKEY_METADATA_ISBINARY_INDEX 2 -#define ATKEY_METADATA_ISENCRYPTED_INDEX 2 -#define ATKEY_METADATA_DATASIGNATURE_INDEX 2 -#define ATKEY_METADATA_SHAREDKEYSTATUS_INDEX 2 -#define ATKEY_METADATA_SHAREDKEYENC_INDEX 2 -#define ATKEY_METADATA_PUBKEYHASH_INDEX 2 -#define ATKEY_METADATA_PUBKEYALGO_INDEX 2 -#define ATKEY_METADATA_ENCODING_INDEX 2 - -#define ATKEY_METADATA_ENCKEYNAME_INDEX 3 -#define ATKEY_METADATA_ENCALGO_INDEX 3 -#define ATKEY_METADATA_IVNONCE_INDEX 3 -#define ATKEY_METADATA_SKEENCKEYNAME_INDEX 3 -#define ATKEY_METADATA_SKEENCALGO_INDEX 3 +#define ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_STATUS_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_VERSION_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX 0 +#define ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX 0 + +#define ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_TTL_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_TTB_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_TTR_INDEX 1 +#define ATCLIENT_ATKEY_METADATA_CCD_INDEX 1 + +#define ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX 2 +#define ATCLIENT_ATKEY_METADATA_ENCODING_INDEX 2 + +#define ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX 3 +#define ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX 3 +#define ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX 3 +#define ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX 3 +#define ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX 3 // initializedfields[0] -#define ATKEY_METADATA_CREATEDBY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATKEY_METADATA_UPDATEDBY_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATKEY_METADATA_STATUS_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATKEY_METADATA_VERSION_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATKEY_METADATA_EXPIRESAT_INITIALIZED (VALUE_INITIALIZED << 4) -#define ATKEY_METADATA_AVAILABLEAT_INITIALIZED (VALUE_INITIALIZED << 5) -#define ATKEY_METADATA_REFRESHAT_INITIALIZED (VALUE_INITIALIZED << 6) -#define ATKEY_METADATA_CREATEDAT_INITIALIZED (VALUE_INITIALIZED << 7) +#define ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED (VALUE_INITIALIZED << 6) +#define ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED (VALUE_INITIALIZED << 7) // initializedfields[1] -#define ATKEY_METADATA_UPDATEDAT_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATKEY_METADATA_ISPUBLIC_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATKEY_METADATA_ISHIDDEN_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATKEY_METADATA_ISCACHED_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATKEY_METADATA_TTL_INITIALIZED (VALUE_INITIALIZED << 4) -#define ATKEY_METADATA_TTB_INITIALIZED (VALUE_INITIALIZED << 5) -#define ATKEY_METADATA_TTR_INITIALIZED (VALUE_INITIALIZED << 6) -#define ATKEY_METADATA_CCD_INITIALIZED (VALUE_INITIALIZED << 7) +#define ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED (VALUE_INITIALIZED << 1) + +#define ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED (VALUE_INITIALIZED << 6) +#define ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED (VALUE_INITIALIZED << 7) // initializedfields[2] -#define ATKEY_METADATA_ISBINARY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATKEY_METADATA_ISENCRYPTED_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATKEY_METADATA_DATASIGNATURE_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATKEY_METADATA_SHAREDKEYENC_INITIALIZED (VALUE_INITIALIZED << 4) -#define ATKEY_METADATA_PUBKEYHASH_INITIALIZED (VALUE_INITIALIZED << 5) -#define ATKEY_METADATA_PUBKEYALGO_INITIALIZED (VALUE_INITIALIZED << 6) -#define ATKEY_METADATA_ENCODING_INITIALIZED (VALUE_INITIALIZED << 7) +#define ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED (VALUE_INITIALIZED << 6) +#define ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED (VALUE_INITIALIZED << 7) // initializedfields[3] -#define ATKEY_METADATA_ENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATKEY_METADATA_ENCALGO_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATKEY_METADATA_IVNONCE_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATKEY_METADATA_SKEENCALGO_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED (VALUE_INITIALIZED << 4) #define DATE_STR_BUFFER_SIZE 256 // can hold most date strings found in metadata -#define GENERAL_BUFFER_SIZE 8192 // can hold most metadata strings typedef struct atclient_atkey_metadata { // Represents the atSign who created this atkey. diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index fb13e71b..c7a9bcae 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1347,339 +1347,339 @@ void atclient_atkey_metadata_free(atclient_atkey_metadata *metadata) { } static bool is_createdby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] & ATKEY_METADATA_CREATEDBY_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] & ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED); } static bool is_updatedby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] & ATKEY_METADATA_UPDATEDBY_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] & ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED); } static bool is_status_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] & ATKEY_METADATA_STATUS_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] & ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED); } static bool is_version_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] & ATKEY_METADATA_VERSION_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] & ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED); } static bool is_expiresat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] & ATKEY_METADATA_EXPIRESAT_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] & ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED); } static bool is_availableat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] & ATKEY_METADATA_AVAILABLEAT_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] & ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED); } static bool is_refreshat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] & ATKEY_METADATA_REFRESHAT_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] & ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED); } static bool is_createdat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] & ATKEY_METADATA_CREATEDAT_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] & ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED); } static bool is_updatedat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] & ATKEY_METADATA_UPDATEDAT_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] & ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED); } static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] & ATKEY_METADATA_ISPUBLIC_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] & ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED); } static bool is_iscached_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] & ATKEY_METADATA_ISCACHED_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] & ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED); } static bool is_ttl_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] & ATKEY_METADATA_TTL_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] & ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED); } static bool is_ttb_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] & ATKEY_METADATA_TTB_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] & ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED); } static bool is_ttr_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] & ATKEY_METADATA_TTR_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] & ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED); } static bool is_ccd_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] & ATKEY_METADATA_CCD_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] & ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED); } static bool is_isbinary_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] & ATKEY_METADATA_ISBINARY_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] & ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED); } static bool is_isencrypted_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] & ATKEY_METADATA_ISENCRYPTED_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] & ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED); } static bool is_datasignature_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] & ATKEY_METADATA_DATASIGNATURE_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] & ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED); } static bool is_sharedkeystatus_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & - ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & + ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED); } static bool is_sharedkeyenc_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] & ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] & ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); } static bool is_pubkeyhash_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] & ATKEY_METADATA_PUBKEYHASH_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] & ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED); } static bool is_pubkeyalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] & ATKEY_METADATA_PUBKEYALGO_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] & ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED); } static bool is_encoding_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] & ATKEY_METADATA_ENCODING_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] & ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED); } static bool is_enckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] & ATKEY_METADATA_ENCKEYNAME_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] & ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED); } static bool is_encalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] & ATKEY_METADATA_ENCALGO_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] & ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED); } static bool is_ivnonce_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] & ATKEY_METADATA_IVNONCE_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] & ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED); } static bool is_skeenckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] & ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] & ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); } static bool is_skeencalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] & ATKEY_METADATA_SKEENCALGO_INITIALIZED); + return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] & ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED); } static void set_is_createdby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] |= ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] |= ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_CREATEDBY_INDEX] &= ~ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; } } static void set_is_updatedby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] |= ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] |= ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_UPDATEDBY_INDEX] &= ~ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; } } static void set_is_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] |= ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] |= ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_STATUS_INDEX] &= ~ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] &= ~ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; } } static void set_is_version_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] |= ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] |= ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_VERSION_INDEX] &= ~ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] &= ~ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; } } static void set_is_expiresat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] |= ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] |= ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_EXPIRESAT_INDEX] &= ~ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; } } static void set_is_availableat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] |= ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] |= ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_AVAILABLEAT_INDEX] &= ~ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } } static void set_is_refreshat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] |= ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] |= ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_REFRESHAT_INDEX] &= ~ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; } } static void set_is_createdat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] |= ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] |= ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_CREATEDAT_INDEX] &= ~ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; } } static void set_is_updatedat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] |= ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] |= ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_UPDATEDAT_INDEX] &= ~ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; } } static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] |= ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] |= ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ISPUBLIC_INDEX] &= ~ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; } } static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] |= ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] |= ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ISCACHED_INDEX] &= ~ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; } } static void set_is_ttl_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] |= ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] |= ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_TTL_INDEX] &= ~ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; } } static void set_is_ttb_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] |= ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] |= ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_TTB_INDEX] &= ~ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; } } static void set_is_ttr_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] |= ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] |= ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_TTR_INDEX] &= ~ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; } } static void set_is_ccd_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] |= ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] |= ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_CCD_INDEX] &= ~ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; } } static void set_is_isbinary_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] |= ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] |= ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ISBINARY_INDEX] &= ~ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; } } static void set_is_isencrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] |= ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] |= ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ISENCRYPTED_INDEX] &= ~ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } } static void set_is_datasignature_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] |= ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] |= ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_DATASIGNATURE_INDEX] &= ~ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] &= ~ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } } static void set_is_sharedkeystatus_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= ~ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } } static void set_is_sharedkeyenc_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] |= ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] |= ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_SHAREDKEYENC_INDEX] &= ~ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } } static void set_is_pubkeyhash_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] |= ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] |= ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_PUBKEYHASH_INDEX] &= ~ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] &= ~ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } } static void set_is_pubkeyalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] |= ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_PUBKEYALGO_INDEX] &= ~ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } } static void set_is_encoding_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] |= ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ENCODING_INDEX] &= ~ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; } } static void set_is_enckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] |= ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ENCKEYNAME_INDEX] &= ~ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } } static void set_is_encalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] |= ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_ENCALGO_INDEX] &= ~ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; } } static void set_is_ivnonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] |= ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] |= ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_IVNONCE_INDEX] &= ~ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] &= ~ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; } } static void set_is_skeenckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= ~ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } } static void set_is_skeencalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] |= ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; } else { - metadata->_initializedfields[ATKEY_METADATA_SKEENCALGO_INDEX] &= ~ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; } } From 13ebf60bddeef9ec232361b5e42904cb12817a46 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 19:34:04 -0400 Subject: [PATCH 108/193] refactor: header symbolic constants --- packages/atclient/include/atclient/atclient.h | 4 ++-- packages/atclient/include/atclient/atclient_utils.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 750cb051..960fbfe2 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -1,5 +1,5 @@ -#ifndef ATCLIENT_H -#define ATCLIENT_H +#ifndef ATCLIENT_ATCLIENT_H +#define ATCLIENT_ATCLIENT_H #include "atclient/atkey.h" #include "atclient/atkeys.h" diff --git a/packages/atclient/include/atclient/atclient_utils.h b/packages/atclient/include/atclient/atclient_utils.h index 10b7a2fa..d46503cd 100644 --- a/packages/atclient/include/atclient/atclient_utils.h +++ b/packages/atclient/include/atclient/atclient_utils.h @@ -1,5 +1,5 @@ -#ifndef ATCLIENT_UTILS_H -#define ATCLIENT_UTILS_H +#ifndef ATCLIENT_ATCLIENT_UTILS_H +#define ATCLIENT_ATCLIENT_UTILS_H #include "atclient/atkeys.h" From 20e0ea38d060a8aed7b13f1405281aaa40e9f49f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 22 Jul 2024 20:17:34 -0400 Subject: [PATCH 109/193] refactor: extract atnotification to its own files and rename monitor message to "atclient_monitor_response" --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- packages/atclient/CMakeLists.txt | 1 + .../include/atclient/atnotification.h | 171 +++++ packages/atclient/include/atclient/monitor.h | 189 +---- packages/atclient/src/atnotification.c | 646 ++++++++++++++++++ packages/atclient/src/monitor.c | 629 +---------------- .../tests/test_atclient_monitor.c | 11 +- 9 files changed, 846 insertions(+), 807 deletions(-) create mode 100644 packages/atclient/include/atclient/atnotification.h create mode 100644 packages/atclient/src/atnotification.c diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 78ecef4f..0dd6d94f 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -243,7 +243,7 @@ static void *monitor_handler(void *xargs) { int tries = 1; while (true) { - atclient_monitor_message message; + atclient_monitor_response message; atclient_monitor_message_init(&message); pthread_mutex_lock(&monitor_mutex); diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 11f5e01a..10ed67ff 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) { atclient monitor_conn; atclient_monitor_init(&monitor_conn); - atclient_monitor_message message; + atclient_monitor_response message; if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atsign input (Example: \'./monitor -a @bob\')\n"); diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index 4dde75de..260987db 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { atclient monitor_conn; atclient_monitor_init(&monitor_conn); - atclient_monitor_message *message = NULL; + atclient_monitor_response *message = NULL; if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atsign input (Example: \'./monitor -a @bob\')\n"); diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 895fa765..f3d74566 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -16,6 +16,7 @@ set( ${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/atnotification.c ${CMAKE_CURRENT_LIST_DIR}/src/atsign.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c diff --git a/packages/atclient/include/atclient/atnotification.h b/packages/atclient/include/atclient/atnotification.h new file mode 100644 index 00000000..2d91fac2 --- /dev/null +++ b/packages/atclient/include/atclient/atnotification.h @@ -0,0 +1,171 @@ +#ifndef ATCLIENT_ATNOTIFICATION_H +#define ATCLIENT_ATNOTIFICATION_H + +#include +#include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_ATNOTIFICATION_ID_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_FROM_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_TO_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_KEY_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_VALUE_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_OPERATION_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX 0 +#define ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX 0 + +#define ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX 1 +#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX 1 + +// initializedfields[0] +#define ATCLIENT_ATNOTIFICATION_ID_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATNOTIFICATION_TO_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED (VALUE_INITIALIZED << 6) +#define ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED (VALUE_INITIALIZED << 7) + +// initializedfields[1] +#define ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED (VALUE_INITIALIZED << 6) + +/** + * @brief Represents a notification received from the monitor connection + */ +typedef struct atclient_atnotification { + // _initalizedfields[0] + char *id; // holds notification id, typically a 36 + 1 null terminated string + char *from; // holds the from atSign (who sent the notification) + char *to; // holds the to atSign (who the notification is for) + char *key; // holds the key of the notification (e.g. "@bob:location.app@alice") + char *value; // holds the value that is read from the notification, this would typically be base64 encoded and + // encrypted, see decryptedvalue for the decrypted value + char *operation; // holds the operation of the notification (e.g. "update", "delete") + size_t epochMillis; // holds the epoch time in milliseconds when the notification was sent + char *messageType; // holds the message type of the notification (e.g. "data", "error") + + // _initalizedfields[1] + bool isEncrypted : 1; + char *encKeyName; // in metaData + char *encAlgo; // in metaData + char *ivNonce; // in metaData + char *skeEncKeyName; // in metaData + char *skeEncAlgo; // in metaData + char *decryptedvalue; // if isEncrypted, this will be the decrypted value + + uint8_t _initializedfields[2]; +} atclient_atnotification; + +/** + * @brief Initializes the atnotification to a default state, ready for use in other functions. + * + * Example use: + * atclient_atnotification notification; + * atclient_atnotification_init(¬ification); + * + * @param notification pointer to the atnotification to initialize, it is assumed that the memory for this struct has + * already been allocated (either statically or dynamically, it is up to the caller to make this decision) + */ +void atclient_atnotification_init(atclient_atnotification *notification); + +/** + * @brief Frees the memory allocated for the atnotification. _init or any subsequent functions may have allocated memory + * in this context and it is the caller's responsibility to free this memory. + * + * @param notification the atnotification to free, assumed to already have been allocated by the caller and initialized + * by the caller using _init + */ +void atclient_atnotification_free(atclient_atnotification *notification); + +// Check if a field is initialized +bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification); + +// Set a field as initialized or not +void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, const bool initialized); + +// Free a field, some fields are dynamically allocated when set +void atclient_atnotification_unset_id(atclient_atnotification *notification); +void atclient_atnotification_unset_from(atclient_atnotification *notification); +void atclient_atnotification_unset_to(atclient_atnotification *notification); +void atclient_atnotification_unset_key(atclient_atnotification *notification); +void atclient_atnotification_unset_value(atclient_atnotification *notification); +void atclient_atnotification_unset_operation(atclient_atnotification *notification); +void atclient_atnotification_unset_epochMillis(atclient_atnotification *notification); +void atclient_atnotification_unset_messageType(atclient_atnotification *notification); +void atclient_atnotification_unset_isEncrypted(atclient_atnotification *notification); +void atclient_atnotification_unset_encKeyName(atclient_atnotification *notification); +void atclient_atnotification_unset_encAlgo(atclient_atnotification *notification); +void atclient_atnotification_unset_ivNonce(atclient_atnotification *notification); +void atclient_atnotification_unset_skeEncKeyName(atclient_atnotification *notification); +void atclient_atnotification_unset_skeEncAlgo(atclient_atnotification *notification); +void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification); + +// Setters for the fields, these functions check if the field is initialized before setting the value (and overwrites if +// it is) +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen); +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen); +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen); +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, + const size_t operationlen); +int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis); +int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, + const size_t messageTypelen); +int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted); +int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, + const size_t encKeyNamelen); +int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, + const size_t encAlgolen); +int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, + const size_t ivNoncelen); +int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, + const size_t skeEncKeyNamelen); +int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, + const size_t skeEncAlgolen); + int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const char *decryptedvalue, + const size_t decryptedvaluelen); + +#endif // ATCLIENT_ATNOTIFICATION_H \ No newline at end of file diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 9e671879..5344e065 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -3,167 +3,14 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" +#include "atclient/atnotification.h" #include -#define ATCLIENT_ATNOTIFICATION_INITIALIZED 0b00000001 - -// initializedfields[0] -#define ATCLIENT_ATNOTIFICATION_ID_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) -#define ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) -#define ATCLIENT_ATNOTIFICATION_TO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) -#define ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) -#define ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) -#define ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) -#define ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) -#define ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) - -// initializedfields[1] -#define ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 0) -#define ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 1) -#define ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 2) -#define ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 3) -#define ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 4) -#define ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 5) -#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 6) -#define ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED (ATCLIENT_ATNOTIFICATION_INITIALIZED << 7) - -/** - * @brief Represents a notification received from the monitor connection - */ -typedef struct atclient_atnotification { - // initializedfields[0] - char *id; // holds notification id, typically a 36 + 1 null terminated string - char *from; // holds the from atSign (who sent the notification) - char *to; // holds the to atSign (who the notification is for) - char *key; // holds the key of the notification (e.g. "@bob:location.app@alice") - char *value; // holds the value that is read from the notification, this would typically be base64 encoded and - // encrypted, see decryptedvalue for the decrypted value - char *operation; // holds the operation of the notification (e.g. "update", "delete") - size_t epochMillis; // holds the epoch time in milliseconds when the notification was sent - char *messageType; // holds the message type of the notification (e.g. "data", "error") - - // initalizedfields[1] - bool isEncrypted : 1; - char *encKeyName; // in metaData - char *encAlgo; // in metaData - char *ivNonce; // in metaData - char *skeEncKeyName; // in metaData - char *skeEncAlgo; // in metaData - unsigned char *decryptedvalue; // if isEncrypted, this will be the decrypted value - size_t decryptedvaluelen; // represents the length of the decrypted value - - uint8_t initalizedfields[2]; -} atclient_atnotification; - -/** - * @brief Initializes the atnotification to a default state, ready for use in other functions. - * - * Example use: - * atclient_atnotification notification; - * atclient_atnotification_init(¬ification); - * - * @param notification pointer to the atnotification to initialize, it is assumed that the memory for this struct has - * already been allocated (either statically or dynamically, it is up to the caller to make this decision) - */ -void atclient_atnotification_init(atclient_atnotification *notification); - -/** - * @brief Frees the memory allocated for the atnotification. _init or any subsequent functions may have allocated memory - * in this context and it is the caller's responsibility to free this memory. - * - * @param notification the atnotification to free, assumed to already have been allocated by the caller and initialized - * by the caller using _init - */ -void atclient_atnotification_free(atclient_atnotification *notification); - -// Check if a field is initialized -bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_decryptedvaluelen_is_initialized(const atclient_atnotification *notification); - -// Set a field as initialized or not -void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, bool initialized); -void atclient_atnotification_decryptedvaluelen_set_initialized(atclient_atnotification *notification, bool initialized); - -// Free a field, some fields are dynamically allocated when set -void atclient_atnotification_free_id(atclient_atnotification *notification); -void atclient_atnotification_free_from(atclient_atnotification *notification); -void atclient_atnotification_free_to(atclient_atnotification *notification); -void atclient_atnotification_free_key(atclient_atnotification *notification); -void atclient_atnotification_free_value(atclient_atnotification *notification); -void atclient_atnotification_free_operation(atclient_atnotification *notification); -void atclient_atnotification_free_epochMillis(atclient_atnotification *notification); -void atclient_atnotification_free_messageType(atclient_atnotification *notification); -void atclient_atnotification_free_isEncrypted(atclient_atnotification *notification); -void atclient_atnotification_free_encKeyName(atclient_atnotification *notification); -void atclient_atnotification_free_encAlgo(atclient_atnotification *notification); -void atclient_atnotification_free_ivNonce(atclient_atnotification *notification); -void atclient_atnotification_free_skeEncKeyName(atclient_atnotification *notification); -void atclient_atnotification_free_skeEncAlgo(atclient_atnotification *notification); -void atclient_atnotification_free_decryptedvalue(atclient_atnotification *notification); -void atclient_atnotification_free_decryptedvaluelen(atclient_atnotification *notification); - -// Setters for the fields, these functions check if the field is initialized before setting the value (and overwrites if -// it is) -int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen); -int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen); -int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen); -int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); -int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); -int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen); -int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis); -int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, - const size_t messageTypelen); -int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted); -int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen); -int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen); -int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen); -int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen); -int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen); -int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, - const unsigned char *decryptedvalue, const size_t decryptedvaluelen); -int atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, - const size_t decryptedvaluelen); - /** * @brief Represents a message received from the monitor connection, typically derived from the prefix of the response * (e.g. "data:ok"'s message type would be "data" = ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE) - * */ -enum atclient_monitor_message_type { +enum atclient_monitor_response_type { // the following 4 enums help indicate what type of message was received from the monitor connection and which field // of the union to access ATCLIENT_MONITOR_MESSAGE_TYPE_NONE, @@ -180,11 +27,11 @@ enum atclient_monitor_message_type { }; // Represents error information when `ATCLIENT_MONITOR_ERROR_READ` is the message type given by atclient_monitor_read -typedef struct atclient_monitor_message_error_read { +typedef struct atclient_monitor_response_error_read { int error_code; // if 0, then the connection should be disposed of immediately, as it is of no use anymore, // if MBEDTLS_ERR_SSL_TIMEOUT, then a read timeout occurred, // else if < 0, then an error occurred when reading from the SSL connection. -} atclient_monitor_message_error_read; +} atclient_monitor_response_error_read; /** * @brief Represents a message received from the monitor connection @@ -193,26 +40,31 @@ typedef struct atclient_monitor_message_error_read { * reading this field will tell you which data field of the union to access. Example, if type is * ATCLIENT_MONITOR_MESSAGE_TYPE_NOTIFICATION,t then you should access the notification field of the union */ -typedef struct atclient_monitor_message { - enum atclient_monitor_message_type type; +typedef struct atclient_monitor_response { + enum atclient_monitor_response_type type; union { atclient_atnotification notification; // when is_notification is true char *data_response; // message of the data response (e.g. "ok", when "data:ok" is received) char *error_response; // message of the error_response - atclient_monitor_message_error_read error_read; + atclient_monitor_response_error_read error_read; }; -} atclient_monitor_message; +} atclient_monitor_response; + +typedef struct atclient_monitor_hooks { + int (*pre_decrypt_notification)(void); + int (*post_decrypt_notification)(int); +} atclient_monitor_hooks; /** * @brief Initializes the monitor message to a default state, ready for use in other functions. * * Example: - * atclient_monitor_message message; + * atclient_monitor_response message; * atclient_monitor_message_init(&message); * * @param message the message to initialize, it is assumed that the memory for this struct has already been allocated */ -void atclient_monitor_message_init(atclient_monitor_message *message); +void atclient_monitor_message_init(atclient_monitor_response *message); /** * @brief Initializes the monitor message to a default state, ready for use in other functions. @@ -220,7 +72,7 @@ void atclient_monitor_message_init(atclient_monitor_message *message); * @param message the message to free, it is assumed that the memory for this struct has already been allocated and was * previous called with atclient_monitor_message_init */ -void atclient_monitor_message_free(atclient_monitor_message *message); +void atclient_monitor_message_free(atclient_monitor_response *message); /** * @brief Initializes the monitor connection. It is recommended that this be called before any other monitor functions. @@ -262,11 +114,6 @@ int atclient_monitor_pkam_authenticate(atclient *monitor_conn, const char *atser */ void atclient_monitor_set_read_timeout(atclient *monitor_conn, const int timeout_ms); -typedef struct atclient_monitor_hooks { - int (*pre_decrypt_notification)(void); - int (*post_decrypt_notification)(int); -} atclient_monitor_hooks; - /** * @brief Sends the monitor command to the atserver to start monitoring notifications, assumed that the monitor atclient * context is already pkam authenticated @@ -285,7 +132,7 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size * @param atclient the atclient context for the atclient connection, it is advised that this connection an entirely * separate connection from the monitor_conn to avoid colliding messages when reading. it is assumed that this is * initialized and pkam authenticated. - * @param message A pointer to the initialized atclient_monitor_message. It is up to + * @param message A pointer to the initialized atclient_monitor_response. It is up to * the caller to allocate memory to this struct, call atclient_monitor_message_init before passing to this function, * then call atclient_monitor_free use. This function populates the message struct with the notification, data response, * or error response read from the monitor connection. @@ -294,7 +141,7 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size * @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_message *message, +int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message, atclient_monitor_hooks *hooks); /** diff --git a/packages/atclient/src/atnotification.c b/packages/atclient/src/atnotification.c new file mode 100644 index 00000000..f168b898 --- /dev/null +++ b/packages/atclient/src/atnotification.c @@ -0,0 +1,646 @@ +#include "atclient/atnotification.h" +#include +#include +#include + +#define TAG "atnotification" + +void atclient_atnotification_init(atclient_atnotification *notification) { + memset(notification, 0, sizeof(atclient_atnotification)); +} + +void atclient_atnotification_free(atclient_atnotification *notification) { + if (atclient_atnotification_id_is_initialized(notification)) { + atclient_atnotification_unset_id(notification); + } + if (atclient_atnotification_from_is_initialized(notification)) { + atclient_atnotification_unset_from(notification); + } + if (atclient_atnotification_to_is_initialized(notification)) { + atclient_atnotification_unset_to(notification); + } + if (atclient_atnotification_key_is_initialized(notification)) { + atclient_atnotification_unset_key(notification); + } + if (atclient_atnotification_value_is_initialized(notification)) { + atclient_atnotification_unset_value(notification); + } + if (atclient_atnotification_operation_is_initialized(notification)) { + atclient_atnotification_unset_operation(notification); + } + if (atclient_atnotification_epochMillis_is_initialized(notification)) { + atclient_atnotification_unset_epochMillis(notification); + } + if (atclient_atnotification_messageType_is_initialized(notification)) { + atclient_atnotification_unset_messageType(notification); + } + if (atclient_atnotification_isEncrypted_is_initialized(notification)) { + atclient_atnotification_unset_isEncrypted(notification); + } + if (atclient_atnotification_encKeyName_is_initialized(notification)) { + atclient_atnotification_unset_encKeyName(notification); + } + if (atclient_atnotification_encAlgo_is_initialized(notification)) { + atclient_atnotification_unset_encAlgo(notification); + } + if (atclient_atnotification_ivNonce_is_initialized(notification)) { + atclient_atnotification_unset_ivNonce(notification); + } + if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { + atclient_atnotification_unset_skeEncKeyName(notification); + } + if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { + atclient_atnotification_unset_skeEncAlgo(notification); + } + if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { + atclient_atnotification_unset_decryptedvalue(notification); + } +} + +bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); +} + +bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & + ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); +} + +bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); +} + +bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & + ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); +} + +bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & + ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); +} + +bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & + ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); +} + +bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & + ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); +} + +bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & + ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); +} + +bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & + ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); +} + +bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & + ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); +} + +bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & + ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); +} + +bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & + ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); +} + +bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & + ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); +} + +bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & + ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); +} + +bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification) { + return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & + ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); +} + +void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + } +} + +void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + } +} + +void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + } +} + +void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + } +} + +void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] &= ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + } +} + +void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] |= + ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; + } +} + +void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] |= + ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; + } +} + +void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] |= + ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; + } else { + notification->_initializedfields[0] &= ~ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; + } +} + +void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] |= + ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; + } +} + +void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] |= + ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; + } else { + notification->_initializedfields[1] &= ~ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; + } +} + +void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] |= + ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; + } +} + +void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] |= + ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; + } +} + +void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] |= + ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; + } +} + +void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] |= + ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; + } +} + +void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, bool initialized) { + if (initialized) { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] |= + ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; + } else { + notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] &= + ~ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; + } +} + +void atclient_atnotification_unset_id(atclient_atnotification *notification) { + if (atclient_atnotification_id_is_initialized(notification)) { + free(notification->id); + } + notification->id = NULL; + atclient_atnotification_id_set_initialized(notification, false); + atclient_atnotification_id_set_initialized(notification, false); +} + +void atclient_atnotification_unset_from(atclient_atnotification *notification) { + if (atclient_atnotification_from_is_initialized(notification)) { + free(notification->from); + } + notification->from = NULL; + atclient_atnotification_from_set_initialized(notification, false); +} + +void atclient_atnotification_unset_to(atclient_atnotification *notification) { + if(atclient_atnotification_to_is_initialized(notification)) { + free(notification->to); + } + notification->to = NULL; + atclient_atnotification_to_set_initialized(notification, false); +} +void atclient_atnotification_unset_key(atclient_atnotification *notification) { + if(atclient_atnotification_key_is_initialized(notification)) { + free(notification->key); + } + notification->key = NULL; + atclient_atnotification_key_set_initialized(notification, false); +} +void atclient_atnotification_unset_value(atclient_atnotification *notification) { + if (atclient_atnotification_value_is_initialized(notification)) { + free(notification->value); + } + notification->value = NULL; + atclient_atnotification_value_set_initialized(notification, false); +} +void atclient_atnotification_unset_operation(atclient_atnotification *notification) { + if(atclient_atnotification_operation_is_initialized(notification)) { + free(notification->operation); + } + notification->operation = NULL; + atclient_atnotification_operation_set_initialized(notification, false); +} +void atclient_atnotification_unset_epochMillis(atclient_atnotification *notification) { + notification->epochMillis = 0; + atclient_atnotification_epochMillis_set_initialized(notification, false); +} + +void atclient_atnotification_unset_messageType(atclient_atnotification *notification) { + if(atclient_atnotification_messageType_is_initialized(notification)) { + free(notification->messageType); + } + notification->messageType = NULL; + atclient_atnotification_messageType_set_initialized(notification, false); +} + +void atclient_atnotification_unset_isEncrypted(atclient_atnotification *notification) { + notification->isEncrypted = false; + atclient_atnotification_isEncrypted_set_initialized(notification, false); +} + +void atclient_atnotification_unset_encKeyName(atclient_atnotification *notification) { + if(atclient_atnotification_encKeyName_is_initialized(notification)) { + free(notification->encKeyName); + } + notification->encKeyName = NULL; + atclient_atnotification_encKeyName_set_initialized(notification, false); +} + +void atclient_atnotification_unset_encAlgo(atclient_atnotification *notification) { + if(atclient_atnotification_encAlgo_is_initialized(notification)) { + free(notification->encAlgo); + } + notification->encAlgo = NULL; + atclient_atnotification_encAlgo_set_initialized(notification, false); +} + +void atclient_atnotification_unset_ivNonce(atclient_atnotification *notification) { + if(atclient_atnotification_ivNonce_is_initialized(notification)) { + free(notification->ivNonce); + } + notification->ivNonce = NULL; + atclient_atnotification_ivNonce_set_initialized(notification, false); +} + +void atclient_atnotification_unset_skeEncKeyName(atclient_atnotification *notification) { + if(atclient_atnotification_skeEncKeyName_is_initialized(notification)) { + free(notification->skeEncKeyName); + } + notification->skeEncKeyName = NULL; + atclient_atnotification_skeEncKeyName_set_initialized(notification, false); +} + +void atclient_atnotification_unset_skeEncAlgo(atclient_atnotification *notification) { + if(atclient_atnotification_skeEncAlgo_is_initialized(notification)) { + free(notification->skeEncAlgo); + } + notification->skeEncAlgo = NULL; + atclient_atnotification_skeEncAlgo_set_initialized(notification, false); +} + +void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification) { + if(atclient_atnotification_decryptedvalue_is_initialized(notification)) { + free(notification->decryptedvalue); + } + notification->decryptedvalue = NULL; + atclient_atnotification_decryptedvalue_set_initialized(notification, false); +} + +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { + int ret = 1; + if (atclient_atnotification_id_is_initialized(notification)) { + atclient_atnotification_unset_id(notification); + } + notification->id = malloc(sizeof(char) * (idlen + 1)); + if (notification->id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification id\n"); + goto exit; + } + memcpy(notification->id, id, idlen); + *(notification->id + idlen) = '\0'; + atclient_atnotification_id_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { + int ret = 1; + if (atclient_atnotification_from_is_initialized(notification)) { + atclient_atnotification_unset_from(notification); + } + notification->from = malloc(sizeof(char) * (fromlen + 1)); + if (notification->from == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification from\n"); + goto exit; + } + memcpy(notification->from, from, fromlen); + *(notification->from + fromlen) = '\0'; + atclient_atnotification_from_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { + int ret = 1; + if (atclient_atnotification_to_is_initialized(notification)) { + atclient_atnotification_unset_to(notification); + } + notification->to = malloc(sizeof(char) * (tolen + 1)); + if (notification->to == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification to\n"); + goto exit; + } + memcpy(notification->to, to, tolen); + *(notification->to + tolen) = '\0'; + atclient_atnotification_to_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { + int ret = 1; + if (atclient_atnotification_key_is_initialized(notification)) { + atclient_atnotification_unset_key(notification); + } + notification->key = malloc(sizeof(char) * (keylen + 1)); + if (notification->key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification key\n"); + goto exit; + } + memcpy(notification->key, key, keylen); + *(notification->key + keylen) = '\0'; + atclient_atnotification_key_set_initialized(notification, true); + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen) { + int ret = 1; + if (atclient_atnotification_value_is_initialized(notification)) { + atclient_atnotification_unset_value(notification); + } + notification->value = malloc(sizeof(char) * (valuelen + 1)); + if (notification->value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification value\n"); + goto exit; + } + memcpy(notification->value, value, valuelen); + *(notification->value + valuelen) = '\0'; + atclient_atnotification_value_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, + const size_t operationlen) { + int ret = 1; + if (atclient_atnotification_operation_is_initialized(notification)) { + atclient_atnotification_unset_operation(notification); + } + notification->operation = malloc(sizeof(char) * (operationlen + 1)); + if (notification->operation == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification operation\n"); + goto exit; + } + memcpy(notification->operation, operation, operationlen); + *(notification->operation + operationlen) = '\0'; + atclient_atnotification_operation_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis) { + if (atclient_atnotification_epochMillis_is_initialized(notification)) { + atclient_atnotification_unset_epochMillis(notification); + } + notification->epochMillis = epochMillis; + atclient_atnotification_epochMillis_set_initialized(notification, true); + return 0; +} + +int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, + const size_t messageTypelen) { + int ret = 1; + if (atclient_atnotification_messageType_is_initialized(notification)) { + atclient_atnotification_unset_messageType(notification); + } + notification->messageType = malloc(sizeof(char) * (messageTypelen + 1)); + if (notification->messageType == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification messageType\n"); + goto exit; + } + memcpy(notification->messageType, messageType, messageTypelen); + *(notification->messageType + messageTypelen) = '\0'; + atclient_atnotification_messageType_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted) { + if (atclient_atnotification_isEncrypted_is_initialized(notification)) { + atclient_atnotification_unset_isEncrypted(notification); + } + notification->isEncrypted = isEncrypted; + atclient_atnotification_isEncrypted_set_initialized(notification, true); + return 0; +} + +int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, + const size_t encKeyNamelen) { + int ret = 1; + if (atclient_atnotification_encKeyName_is_initialized(notification)) { + atclient_atnotification_unset_encKeyName(notification); + } + notification->encKeyName = malloc(sizeof(char) * (encKeyNamelen + 1)); + if (notification->encKeyName == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification encKeyName\n"); + goto exit; + } + memcpy(notification->encKeyName, encKeyName, encKeyNamelen); + *(notification->encKeyName + encKeyNamelen) = '\0'; + atclient_atnotification_encKeyName_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, + const size_t encAlgolen) { + int ret = 1; + if (atclient_atnotification_encAlgo_is_initialized(notification)) { + atclient_atnotification_unset_encAlgo(notification); + } + notification->encAlgo = malloc(sizeof(char) * (encAlgolen + 1)); + memcpy(notification->encAlgo, encAlgo, encAlgolen); + *(notification->encAlgo + encAlgolen) = '\0'; + atclient_atnotification_encAlgo_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, + const size_t ivNoncelen) { + int ret = 1; + if (atclient_atnotification_ivNonce_is_initialized(notification)) { + atclient_atnotification_unset_ivNonce(notification); + } + notification->ivNonce = malloc(sizeof(char) * (ivNoncelen + 1)); + if (notification->ivNonce == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ivNonce\n"); + goto exit; + } + memcpy(notification->ivNonce, ivNonce, ivNoncelen); + *(notification->ivNonce + ivNoncelen) = '\0'; + atclient_atnotification_ivNonce_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, + const size_t skeEncKeyNamelen) { + int ret = 1; + if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { + atclient_atnotification_unset_skeEncKeyName(notification); + } + notification->skeEncKeyName = malloc(sizeof(char) * (skeEncKeyNamelen + 1)); + if (notification->skeEncKeyName == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncKeyName\n"); + goto exit; + } + memcpy(notification->skeEncKeyName, skeEncKeyName, skeEncKeyNamelen); + *(notification->skeEncKeyName + skeEncKeyNamelen) = '\0'; + atclient_atnotification_skeEncKeyName_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, + const size_t skeEncAlgolen) { + int ret = 1; + if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { + atclient_atnotification_unset_skeEncAlgo(notification); + } + notification->skeEncAlgo = malloc(sizeof(char) * (skeEncAlgolen + 1)); + if (notification->skeEncAlgo == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncAlgo\n"); + goto exit; + } + memcpy(notification->skeEncAlgo, skeEncAlgo, skeEncAlgolen); + *(notification->skeEncAlgo + skeEncAlgolen) = '\0'; + atclient_atnotification_skeEncAlgo_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, + const char *decryptedvalue, const size_t decryptedvaluelen) { + int ret = 1; + if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { + atclient_atnotification_unset_decryptedvalue(notification); + } + notification->decryptedvalue = malloc(sizeof(unsigned char) * (decryptedvaluelen + 1)); + if (notification->decryptedvalue == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification decryptedvalue\n"); + goto exit; + } + memcpy(notification->decryptedvalue, decryptedvalue, decryptedvaluelen); + notification->decryptedvalue[decryptedvaluelen] = '\0'; + atclient_atnotification_decryptedvalue_set_initialized(notification, true); + ret = 0; + goto exit; +exit: { return ret; } +} diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 7e7b269a..b5b424a0 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -1,4 +1,5 @@ #include "atclient/monitor.h" +#include "atclient/atnotification.h" #include "atclient/atclient.h" #include "atclient/connection.h" #include "atclient/constants.h" @@ -23,629 +24,11 @@ static int parse_message(char *original, char **message_type, char **message_bod static int parse_notification(atclient_atnotification *notification, const char *messagebody); static int decrypt_notification(atclient *monitor_conn, atclient_atnotification *notification); -void atclient_atnotification_init(atclient_atnotification *notification) { - memset(notification, 0, sizeof(atclient_atnotification)); +void atclient_monitor_message_init(atclient_monitor_response *message) { + memset(message, 0, sizeof(atclient_monitor_response)); } -void atclient_atnotification_free(atclient_atnotification *notification) { - if (atclient_atnotification_id_is_initialized(notification)) { - atclient_atnotification_free_id(notification); - } - if (atclient_atnotification_from_is_initialized(notification)) { - atclient_atnotification_free_from(notification); - } - if (atclient_atnotification_to_is_initialized(notification)) { - atclient_atnotification_free_to(notification); - } - if (atclient_atnotification_key_is_initialized(notification)) { - atclient_atnotification_free_key(notification); - } - if (atclient_atnotification_value_is_initialized(notification)) { - atclient_atnotification_free_value(notification); - } - if (atclient_atnotification_operation_is_initialized(notification)) { - atclient_atnotification_free_operation(notification); - } - if (atclient_atnotification_epochMillis_is_initialized(notification)) { - atclient_atnotification_free_epochMillis(notification); - } - if (atclient_atnotification_messageType_is_initialized(notification)) { - atclient_atnotification_free_messageType(notification); - } - if (atclient_atnotification_isEncrypted_is_initialized(notification)) { - atclient_atnotification_free_isEncrypted(notification); - } - if (atclient_atnotification_encKeyName_is_initialized(notification)) { - atclient_atnotification_free_encKeyName(notification); - } - if (atclient_atnotification_encAlgo_is_initialized(notification)) { - atclient_atnotification_free_encAlgo(notification); - } - if (atclient_atnotification_ivNonce_is_initialized(notification)) { - atclient_atnotification_free_ivNonce(notification); - } - if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { - atclient_atnotification_free_skeEncKeyName(notification); - } - if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { - atclient_atnotification_free_skeEncAlgo(notification); - } - if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { - atclient_atnotification_free_decryptedvalue(notification); - } - if (atclient_atnotification_decryptedvaluelen_is_initialized(notification)) { - atclient_atnotification_free_decryptedvaluelen(notification); - } -} - -bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_INITIALIZED); -} - -bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); -} - -bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); -} - -bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); -} - -bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); -} - -bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); -} - -bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); -} - -bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[0] & ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); -} - -bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); -} - -bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); -} - -bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); -} - -bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); -} - -bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); -} - -bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); -} - -bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); -} - -bool atclient_atnotification_decryptedvaluelen_is_initialized(const atclient_atnotification *notification) { - return (notification->initalizedfields[1] & ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED); -} - -void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; - } -} - -void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; - } -} - -void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; - } -} - -void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; - } -} - -void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; - } -} - -void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; - } -} - -void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; - } -} - -void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[0] |= ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; - } else { - notification->initalizedfields[0] &= ~ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; - } -} - -void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; - } -} - -void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; - } -} - -void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; - } -} - -void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; - } -} - -void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; - } -} - -void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; - } -} - -void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; - } -} - -void atclient_atnotification_decryptedvaluelen_set_initialized(atclient_atnotification *notification, - bool initialized) { - if (initialized) { - notification->initalizedfields[1] |= ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED; - } else { - notification->initalizedfields[1] &= ~ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUELEN_INITIALIZED; - } -} - -void atclient_atnotification_free_id(atclient_atnotification *notification) { - free(notification->id); - notification->id = NULL; - atclient_atnotification_id_set_initialized(notification, false); -} - -void atclient_atnotification_free_from(atclient_atnotification *notification) { - free(notification->from); - notification->from = NULL; - atclient_atnotification_from_set_initialized(notification, false); -} - -void atclient_atnotification_free_to(atclient_atnotification *notification) { - free(notification->to); - notification->to = NULL; - atclient_atnotification_to_set_initialized(notification, false); -} -void atclient_atnotification_free_key(atclient_atnotification *notification) { - free(notification->key); - notification->key = NULL; - atclient_atnotification_key_set_initialized(notification, false); -} -void atclient_atnotification_free_value(atclient_atnotification *notification) { - if (atclient_atnotification_value_is_initialized(notification)) { - free(notification->value); - notification->value = NULL; - } - atclient_atnotification_value_set_initialized(notification, false); -} -void atclient_atnotification_free_operation(atclient_atnotification *notification) { - free(notification->operation); - notification->operation = NULL; - atclient_atnotification_operation_set_initialized(notification, false); -} -void atclient_atnotification_free_epochMillis(atclient_atnotification *notification) { - notification->epochMillis = 0; - atclient_atnotification_epochMillis_set_initialized(notification, false); -} - -void atclient_atnotification_free_messageType(atclient_atnotification *notification) { - free(notification->messageType); - notification->messageType = NULL; - atclient_atnotification_messageType_set_initialized(notification, false); -} - -void atclient_atnotification_free_isEncrypted(atclient_atnotification *notification) { - notification->isEncrypted = false; - atclient_atnotification_isEncrypted_set_initialized(notification, false); -} - -void atclient_atnotification_free_encKeyName(atclient_atnotification *notification) { - free(notification->encKeyName); - notification->encKeyName = NULL; - atclient_atnotification_encKeyName_set_initialized(notification, false); -} - -void atclient_atnotification_free_encAlgo(atclient_atnotification *notification) { - free(notification->encAlgo); - notification->encAlgo = NULL; - atclient_atnotification_encAlgo_set_initialized(notification, false); -} - -void atclient_atnotification_free_ivNonce(atclient_atnotification *notification) { - free(notification->ivNonce); - notification->ivNonce = NULL; - atclient_atnotification_ivNonce_set_initialized(notification, false); -} - -void atclient_atnotification_free_skeEncKeyName(atclient_atnotification *notification) { - free(notification->skeEncKeyName); - notification->skeEncKeyName = NULL; - atclient_atnotification_skeEncKeyName_set_initialized(notification, false); -} - -void atclient_atnotification_free_skeEncAlgo(atclient_atnotification *notification) { - free(notification->skeEncAlgo); - notification->skeEncAlgo = NULL; - atclient_atnotification_skeEncAlgo_set_initialized(notification, false); -} - -void atclient_atnotification_free_decryptedvalue(atclient_atnotification *notification) { - free(notification->decryptedvalue); - notification->decryptedvalue = NULL; - atclient_atnotification_decryptedvalue_set_initialized(notification, false); -} - -void atclient_atnotification_free_decryptedvaluelen(atclient_atnotification *notification) { - notification->decryptedvaluelen = 0; - atclient_atnotification_decryptedvaluelen_set_initialized(notification, false); -} - -int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { - int ret = 1; - if (atclient_atnotification_id_is_initialized(notification)) { - atclient_atnotification_free_id(notification); - } - notification->id = malloc(sizeof(char) * (idlen + 1)); - if (notification->id == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification id\n"); - goto exit; - } - memcpy(notification->id, id, idlen); - *(notification->id + idlen) = '\0'; - atclient_atnotification_id_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { - int ret = 1; - if (atclient_atnotification_from_is_initialized(notification)) { - atclient_atnotification_free_from(notification); - } - notification->from = malloc(sizeof(char) * (fromlen + 1)); - if (notification->from == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification from\n"); - goto exit; - } - memcpy(notification->from, from, fromlen); - *(notification->from + fromlen) = '\0'; - atclient_atnotification_from_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { - int ret = 1; - if (atclient_atnotification_to_is_initialized(notification)) { - atclient_atnotification_free_to(notification); - } - notification->to = malloc(sizeof(char) * (tolen + 1)); - if (notification->to == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification to\n"); - goto exit; - } - memcpy(notification->to, to, tolen); - *(notification->to + tolen) = '\0'; - atclient_atnotification_to_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { - int ret = 1; - if (atclient_atnotification_key_is_initialized(notification)) { - atclient_atnotification_free_key(notification); - } - notification->key = malloc(sizeof(char) * (keylen + 1)); - if (notification->key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification key\n"); - goto exit; - } - memcpy(notification->key, key, keylen); - *(notification->key + keylen) = '\0'; - atclient_atnotification_key_set_initialized(notification, true); - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen) { - int ret = 1; - if (atclient_atnotification_value_is_initialized(notification)) { - atclient_atnotification_free_value(notification); - } - notification->value = malloc(sizeof(char) * (valuelen + 1)); - if (notification->value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification value\n"); - goto exit; - } - memcpy(notification->value, value, valuelen); - *(notification->value + valuelen) = '\0'; - atclient_atnotification_value_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen) { - int ret = 1; - if (atclient_atnotification_operation_is_initialized(notification)) { - atclient_atnotification_free_operation(notification); - } - notification->operation = malloc(sizeof(char) * (operationlen + 1)); - if (notification->operation == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification operation\n"); - goto exit; - } - memcpy(notification->operation, operation, operationlen); - *(notification->operation + operationlen) = '\0'; - atclient_atnotification_operation_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis) { - if (atclient_atnotification_epochMillis_is_initialized(notification)) { - atclient_atnotification_free_epochMillis(notification); - } - notification->epochMillis = epochMillis; - atclient_atnotification_epochMillis_set_initialized(notification, true); - return 0; -} - -int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, - const size_t messageTypelen) { - int ret = 1; - if (atclient_atnotification_messageType_is_initialized(notification)) { - atclient_atnotification_free_messageType(notification); - } - notification->messageType = malloc(sizeof(char) * (messageTypelen + 1)); - if (notification->messageType == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification messageType\n"); - goto exit; - } - memcpy(notification->messageType, messageType, messageTypelen); - *(notification->messageType + messageTypelen) = '\0'; - atclient_atnotification_messageType_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted) { - if (atclient_atnotification_isEncrypted_is_initialized(notification)) { - atclient_atnotification_free_isEncrypted(notification); - } - notification->isEncrypted = isEncrypted; - atclient_atnotification_isEncrypted_set_initialized(notification, true); - return 0; -} - -int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen) { - int ret = 1; - if (atclient_atnotification_encKeyName_is_initialized(notification)) { - atclient_atnotification_free_encKeyName(notification); - } - notification->encKeyName = malloc(sizeof(char) * (encKeyNamelen + 1)); - if (notification->encKeyName == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification encKeyName\n"); - goto exit; - } - memcpy(notification->encKeyName, encKeyName, encKeyNamelen); - *(notification->encKeyName + encKeyNamelen) = '\0'; - atclient_atnotification_encKeyName_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen) { - int ret = 1; - if (atclient_atnotification_encAlgo_is_initialized(notification)) { - atclient_atnotification_free_encAlgo(notification); - } - notification->encAlgo = malloc(sizeof(char) * (encAlgolen + 1)); - memcpy(notification->encAlgo, encAlgo, encAlgolen); - *(notification->encAlgo + encAlgolen) = '\0'; - atclient_atnotification_encAlgo_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen) { - int ret = 1; - if (atclient_atnotification_ivNonce_is_initialized(notification)) { - atclient_atnotification_free_ivNonce(notification); - } - notification->ivNonce = malloc(sizeof(char) * (ivNoncelen + 1)); - if (notification->ivNonce == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ivNonce\n"); - goto exit; - } - memcpy(notification->ivNonce, ivNonce, ivNoncelen); - *(notification->ivNonce + ivNoncelen) = '\0'; - atclient_atnotification_ivNonce_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen) { - int ret = 1; - if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { - atclient_atnotification_free_skeEncKeyName(notification); - } - notification->skeEncKeyName = malloc(sizeof(char) * (skeEncKeyNamelen + 1)); - if (notification->skeEncKeyName == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncKeyName\n"); - goto exit; - } - memcpy(notification->skeEncKeyName, skeEncKeyName, skeEncKeyNamelen); - *(notification->skeEncKeyName + skeEncKeyNamelen) = '\0'; - atclient_atnotification_skeEncKeyName_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen) { - int ret = 1; - if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { - atclient_atnotification_free_skeEncAlgo(notification); - } - notification->skeEncAlgo = malloc(sizeof(char) * (skeEncAlgolen + 1)); - if (notification->skeEncAlgo == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncAlgo\n"); - goto exit; - } - memcpy(notification->skeEncAlgo, skeEncAlgo, skeEncAlgolen); - *(notification->skeEncAlgo + skeEncAlgolen) = '\0'; - atclient_atnotification_skeEncAlgo_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, - const unsigned char *decryptedvalue, const size_t decryptedvaluelen) { - int ret = 1; - if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { - atclient_atnotification_free_decryptedvalue(notification); - } - notification->decryptedvalue = malloc(sizeof(unsigned char) * (decryptedvaluelen + 1)); - if (notification->decryptedvalue == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification decryptedvalue\n"); - goto exit; - } - memcpy(notification->decryptedvalue, decryptedvalue, decryptedvaluelen); - notification->decryptedvalue[decryptedvaluelen] = '\0'; - atclient_atnotification_decryptedvalue_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atnotification_set_decryptedvaluelen(atclient_atnotification *notification, - const size_t decryptedvaluelen) { - int ret = 1; - if (atclient_atnotification_decryptedvaluelen_is_initialized(notification)) { - atclient_atnotification_free_decryptedvaluelen(notification); - } - notification->decryptedvaluelen = decryptedvaluelen; - atclient_atnotification_decryptedvaluelen_set_initialized(notification, true); - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_monitor_message_init(atclient_monitor_message *message) { - memset(message, 0, sizeof(atclient_monitor_message)); -} - -void atclient_monitor_message_free(atclient_monitor_message *message) { +void atclient_monitor_message_free(atclient_monitor_response *message) { if (message->type == ATCLIENT_MONITOR_MESSAGE_TYPE_NOTIFICATION) { atclient_atnotification_free(&(message->notification)); } else if (message->type == ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE) { @@ -721,7 +104,7 @@ exit: { } } -int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_message *message, +int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message, atclient_monitor_hooks *hooks) { int ret = -1; @@ -825,7 +208,6 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m } else { atclient_atnotification_set_decryptedvalue(&(message->notification), (unsigned char *)message->notification.value, strlen(message->notification.value)); - atclient_atnotification_set_decryptedvaluelen(&(message->notification), strlen(message->notification.value)); } } else if (strcmp(messagetype, "data") == 0) { message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE; @@ -1251,7 +633,6 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not // 5. set decrypted value atclient_atnotification_set_decryptedvalue(notification, decryptedvaluetemp, decryptedvaluetemplen); - atclient_atnotification_set_decryptedvaluelen(notification, decryptedvaluetemplen); ret = 0; goto exit; diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index b3e9a0d8..b6ad137b 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -193,7 +193,7 @@ exit: { static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) { int ret = 1; - atclient_monitor_message message; + atclient_monitor_response message; atclient_monitor_message_init(&message); const int max_tries = 10; @@ -212,14 +212,7 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) continue; } - if (!atclient_atnotification_decryptedvaluelen_is_initialized(&(message.notification))) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted value length is not initialized\n"); - tries++; - continue; - } - - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted Value (%lu): %s\n", - (int)message.notification.decryptedvaluelen, message.notification.decryptedvalue); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted Value: %s\n",message.notification.decryptedvalue); // compare the decrypted value with the expected value if (strcmp(message.notification.decryptedvalue, ATKEY_VALUE) != 0) { From 43e4f6b05e6a956212307a62c7c954f4c8c5814b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 09:48:51 -0400 Subject: [PATCH 110/193] feat: atnotification.h/.c and notify_params.h/.c --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- packages/atclient/CMakeLists.txt | 1 + packages/atclient/include/atclient/atkey.h | 9 + .../include/atclient/atnotification.h | 112 ++--- packages/atclient/include/atclient/metadata.h | 12 + packages/atclient/include/atclient/notify.h | 73 +-- .../atclient/include/atclient/notify_params.h | 155 ++++++ packages/atclient/src/atkey.c | 47 +- packages/atclient/src/atnotification.c | 228 ++++----- packages/atclient/src/metadata.c | 189 ++++++++ packages/atclient/src/monitor.c | 24 +- packages/atclient/src/notify.c | 105 ++-- packages/atclient/src/notify_params.c | 448 ++++++++++++++++++ .../tests/test_atclient_monitor.c | 23 +- .../tests/test_atclient_notify.c | 40 +- 17 files changed, 1145 insertions(+), 327 deletions(-) create mode 100644 packages/atclient/include/atclient/notify_params.h create mode 100644 packages/atclient/src/notify_params.c diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 0dd6d94f..a332157c 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -258,7 +258,7 @@ static void *monitor_handler(void *xargs) { // We received a stats notification. Ignore it. break; } - if (atclient_atnotification_decryptedvalue_is_initialized(&(message.notification))) { + if (atclient_atnotification_is_decryptedvalue_initialized(&(message.notification))) { const atclient_atnotification *notification = &(message.notification); printf("\n%s%s%s: %s\n", HGRN, notification->from, reset, notification->decryptedvalue); printf("%s%s%s: ", HBLU, from_atsign, reset); diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 10ed67ff..4c145d1f 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -93,7 +93,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Received stats notification, ignoring it.\n"); break; } - if (atclient_atnotification_decryptedvalue_is_initialized(&message.notification)) { + if (atclient_atnotification_is_decryptedvalue_initialized(&message.notification)) { // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Message id: %s\n", message->notification.id); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decryptedvalue: \"%s\"\n", message.notification.decryptedvalue); diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index 260987db..00b11eb4 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Received stats notification, ignoring it.\n"); break; } - if (atclient_atnotification_decryptedvalue_is_initialized(&message->notification)) { + if (atclient_atnotification_is_decryptedvalue_initialized(&message->notification)) { // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Message id: %s\n", message->notification.id); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decryptedvalue: \"%s\"\n", message->notification.decryptedvalue); diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index f3d74566..8fb05201 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -23,6 +23,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c ${CMAKE_CURRENT_LIST_DIR}/src/monitor.c ${CMAKE_CURRENT_LIST_DIR}/src/notify.c + ${CMAKE_CURRENT_LIST_DIR}/src/notify_params.c ${CMAKE_CURRENT_LIST_DIR}/src/stringutils.c ) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index cfc91ba1..adb2cf57 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -43,6 +43,15 @@ typedef struct atclient_atkey { */ void atclient_atkey_init(atclient_atkey *atkey); +/** + * @brief Clones an atkey struct. The function will allocate new memory on everything + * + * @param dst the atkey struct to clone to, assumed to be already initialized via atclient_atkey_init + * @param src the atkey struct to clone from, assumed to be already initialized via atclient_atkey_init + * @return int 0 on success + */ +int atclient_atkey_clone(atclient_atkey *dst, const atclient_atkey *src); + /** * @brief free an atkey struct. This function should be called at the end of an atkey's life * diff --git a/packages/atclient/include/atclient/atnotification.h b/packages/atclient/include/atclient/atnotification.h index 2d91fac2..6a3e75cd 100644 --- a/packages/atclient/include/atclient/atnotification.h +++ b/packages/atclient/include/atclient/atnotification.h @@ -1,9 +1,9 @@ #ifndef ATCLIENT_ATNOTIFICATION_H #define ATCLIENT_ATNOTIFICATION_H +#include #include #include -#include #define VALUE_INITIALIZED 0b00000001 @@ -60,11 +60,11 @@ typedef struct atclient_atnotification { // _initalizedfields[1] bool isEncrypted : 1; - char *encKeyName; // in metaData - char *encAlgo; // in metaData - char *ivNonce; // in metaData - char *skeEncKeyName; // in metaData - char *skeEncAlgo; // in metaData + char *encKeyName; // in metaData + char *encAlgo; // in metaData + char *ivNonce; // in metaData + char *skeEncKeyName; // in metaData + char *skeEncAlgo; // in metaData char *decryptedvalue; // if isEncrypted, this will be the decrypted value uint8_t _initializedfields[2]; @@ -92,21 +92,21 @@ void atclient_atnotification_init(atclient_atnotification *notification); void atclient_atnotification_free(atclient_atnotification *notification); // Check if a field is initialized -bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_from_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_to_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_epochmillis_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_messagetype_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_isencrypted_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_enckeyname_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_encalgo_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_ivnonce_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_skeenckeyname_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_skeencalgo_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_decryptedvalue_initialized(const atclient_atnotification *notification); // Set a field as initialized or not void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized); @@ -115,15 +115,17 @@ void atclient_atnotification_to_set_initialized(atclient_atnotification *notific void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized); void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, const bool initialized); void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_epochmillis_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_messagetype_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_isencrypted_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_enckeyname_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_encalgo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_ivnonce_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_skeenckeyname_set_initialized(atclient_atnotification *notification, + const bool initialized); +void atclient_atnotification_skeencalgo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, + const bool initialized); // Free a field, some fields are dynamically allocated when set void atclient_atnotification_unset_id(atclient_atnotification *notification); @@ -132,14 +134,14 @@ void atclient_atnotification_unset_to(atclient_atnotification *notification); void atclient_atnotification_unset_key(atclient_atnotification *notification); void atclient_atnotification_unset_value(atclient_atnotification *notification); void atclient_atnotification_unset_operation(atclient_atnotification *notification); -void atclient_atnotification_unset_epochMillis(atclient_atnotification *notification); -void atclient_atnotification_unset_messageType(atclient_atnotification *notification); -void atclient_atnotification_unset_isEncrypted(atclient_atnotification *notification); -void atclient_atnotification_unset_encKeyName(atclient_atnotification *notification); -void atclient_atnotification_unset_encAlgo(atclient_atnotification *notification); -void atclient_atnotification_unset_ivNonce(atclient_atnotification *notification); -void atclient_atnotification_unset_skeEncKeyName(atclient_atnotification *notification); -void atclient_atnotification_unset_skeEncAlgo(atclient_atnotification *notification); +void atclient_atnotification_unset_epochmillis(atclient_atnotification *notification); +void atclient_atnotification_unset_messagetype(atclient_atnotification *notification); +void atclient_atnotification_unset_isencrypted(atclient_atnotification *notification); +void atclient_atnotification_unset_enckeyname(atclient_atnotification *notification); +void atclient_atnotification_unset_encalgo(atclient_atnotification *notification); +void atclient_atnotification_unset_ivnonce(atclient_atnotification *notification); +void atclient_atnotification_unset_skeenckeyname(atclient_atnotification *notification); +void atclient_atnotification_unset_skeencalgo(atclient_atnotification *notification); void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification); // Setters for the fields, these functions check if the field is initialized before setting the value (and overwrites if @@ -150,22 +152,22 @@ int atclient_atnotification_set_to(atclient_atnotification *notification, const int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen); -int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis); -int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, - const size_t messageTypelen); -int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted); -int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen); -int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen); -int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen); -int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen); -int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen); - int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const char *decryptedvalue, - const size_t decryptedvaluelen); + const size_t operationlen); +int atclient_atnotification_set_epochmillis(atclient_atnotification *notification, const size_t epochMillis); +int atclient_atnotification_set_messagetype(atclient_atnotification *notification, const char *messageType, + const size_t messageTypelen); +int atclient_atnotification_set_isencrypted(atclient_atnotification *notification, const bool isEncrypted); +int atclient_atnotification_set_enckeyname(atclient_atnotification *notification, const char *encKeyName, + const size_t encKeyNamelen); +int atclient_atnotification_set_encalgo(atclient_atnotification *notification, const char *encAlgo, + const size_t encAlgolen); +int atclient_atnotification_set_ivnonce(atclient_atnotification *notification, const char *ivNonce, + const size_t ivNoncelen); +int atclient_atnotification_set_skeenckeyname(atclient_atnotification *notification, const char *skeEncKeyName, + const size_t skeEncKeyNamelen); +int atclient_atnotification_set_skeencalgo(atclient_atnotification *notification, const char *skeEncAlgo, + const size_t skeEncAlgolen); +int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const char *decryptedvalue, + const size_t decryptedvaluelen); #endif // ATCLIENT_ATNOTIFICATION_H \ No newline at end of file diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 09491318..87bc0a7e 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -261,6 +261,18 @@ typedef struct atclient_atkey_metadata { */ void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata); +/** + * @brief Will clone the metadata struct from src to dst. New memory will be allocated for the strings in the dst + * struct. + * + * @param dst the destination metadata struct to clone to, this struct should be initialized using + * atclient_atkey_metadata_init + * @param src the source metadata struct to clone from, this struct should be initialized using + * atclient_atkey_metadata_init + * @return int 0 on success + */ +int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_atkey_metadata *src); + /** * @brief Populates the metadata struct from a string. This function is good for debugging. * diff --git a/packages/atclient/include/atclient/notify.h b/packages/atclient/include/atclient/notify.h index dfe5d7df..4f406d3b 100644 --- a/packages/atclient/include/atclient/notify.h +++ b/packages/atclient/include/atclient/notify.h @@ -1,76 +1,9 @@ #ifndef ATCLIENT_NOTIFY_H #define ATCLIENT_NOTIFY_H -#include "atclient.h" -#include "atkey.h" +#include "atclient/atclient.h" +#include "atclient/notify_params.h" -// For sending notifications -enum atclient_notify_operation { - ATCLIENT_NOTIFY_OPERATION_NONE, - ATCLIENT_NOTIFY_OPERATION_UPDATE, - ATCLIENT_NOTIFY_OPERATION_DELETE -}; - -static const char *atclient_notify_operation_str[] = { - [ATCLIENT_NOTIFY_OPERATION_UPDATE] = "update", - [ATCLIENT_NOTIFY_OPERATION_DELETE] = "delete", -}; - -enum atclient_notify_message_type { - ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE, - ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY, - ATCLIENT_NOTIFY_MESSAGE_TYPE_TEXT -}; - -static const char *atclient_notify_message_type_str[] = { - [ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY] = "key", - [ATCLIENT_NOTIFY_MESSAGE_TYPE_TEXT] = "text", // legacy -}; - -enum atclient_notify_priority { - ATCLIENT_NOTIFY_PRIORITY_NONE, - ATCLIENT_NOTIFY_PRIORITY_LOW, - ATCLIENT_NOTIFY_PRIORITY_MEDIUM, - ATCLIENT_NOTIFY_PRIORITY_HIGH -}; - -static const char *atclient_notify_priority_str[] = { - [ATCLIENT_NOTIFY_PRIORITY_LOW] = "low", - [ATCLIENT_NOTIFY_PRIORITY_MEDIUM] = "medium", - [ATCLIENT_NOTIFY_PRIORITY_HIGH] = "high", -}; - -enum atclient_notify_strategy { - ATCLIENT_NOTIFY_STRATEGY_NONE, - ATCLIENT_NOTIFY_STRATEGY_ALL, - ATCLIENT_NOTIFY_STRATEGY_LATEST -}; - -static const char *atclient_notify_strategy_str[] = { - [ATCLIENT_NOTIFY_STRATEGY_ALL] = "all", - [ATCLIENT_NOTIFY_STRATEGY_LATEST] = "latest", -}; - -typedef struct atclient_notify_params { - char id[37]; // uuid v4 + '\0' - atclient_atkey *atkey; - char *value; - bool shouldencrypt; - enum atclient_notify_operation operation; - enum atclient_notify_message_type message_type; - enum atclient_notify_priority priority; - enum atclient_notify_strategy strategy; - int latest_n; - char *notifier; - unsigned long notification_expiry; - char *sharedenckeybase64; -} atclient_notify_params; - -void atclient_notify_params_init(atclient_notify_params *params); -void atclient_notify_params_create(atclient_notify_params *params, const enum atclient_notify_operation operation, - atclient_atkey *atkey, const char *value, const bool shouldencrypt); -void atclient_notify_params_free(atclient_notify_params *params); - -int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notification_id); +int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notification_id); #endif // ATCLIENT_NOTIFY_H diff --git a/packages/atclient/include/atclient/notify_params.h b/packages/atclient/include/atclient/notify_params.h new file mode 100644 index 00000000..c32a3a9d --- /dev/null +++ b/packages/atclient/include/atclient/notify_params.h @@ -0,0 +1,155 @@ +#ifndef ATCLIENT_NOTIFY_PARAMS_H +#define ATCLIENT_NOTIFY_PARAMS_H + +#include "atclient/atclient.h" +#include "atclient/atkey.h" + +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_NOTIFY_PARAMS_ID_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX 0 +#define ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX 0 + +#define ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX 1 +#define ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX 1 +#define ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX 1 +#define ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX 1 + +#define ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED (VALUE_INITIALIZED << 5) +#define ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED (VALUE_INITIALIZED << 6) +#define ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED (VALUE_INITIALIZED << 7) + +#define ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 3) + +enum atclient_notify_operation { + ATCLIENT_NOTIFY_OPERATION_NONE, + ATCLIENT_NOTIFY_OPERATION_UPDATE, + ATCLIENT_NOTIFY_OPERATION_DELETE +}; + +static const char *atclient_notify_operation_str[] = { + [ATCLIENT_NOTIFY_OPERATION_UPDATE] = "update", + [ATCLIENT_NOTIFY_OPERATION_DELETE] = "delete", +}; + +enum atclient_notify_message_type { + ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE, + ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY, + ATCLIENT_NOTIFY_MESSAGE_TYPE_TEXT +}; + +static const char *atclient_notify_message_type_str[] = { + [ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY] = "key", + [ATCLIENT_NOTIFY_MESSAGE_TYPE_TEXT] = "text", // legacy +}; + +enum atclient_notify_priority { + ATCLIENT_NOTIFY_PRIORITY_NONE, + ATCLIENT_NOTIFY_PRIORITY_LOW, + ATCLIENT_NOTIFY_PRIORITY_MEDIUM, + ATCLIENT_NOTIFY_PRIORITY_HIGH +}; + +static const char *atclient_notify_priority_str[] = { + [ATCLIENT_NOTIFY_PRIORITY_LOW] = "low", + [ATCLIENT_NOTIFY_PRIORITY_MEDIUM] = "medium", + [ATCLIENT_NOTIFY_PRIORITY_HIGH] = "high", +}; + +enum atclient_notify_strategy { + ATCLIENT_NOTIFY_STRATEGY_NONE, + ATCLIENT_NOTIFY_STRATEGY_ALL, + ATCLIENT_NOTIFY_STRATEGY_LATEST +}; + +static const char *atclient_notify_strategy_str[] = { + [ATCLIENT_NOTIFY_STRATEGY_ALL] = "all", + [ATCLIENT_NOTIFY_STRATEGY_LATEST] = "latest", +}; + +typedef struct atclient_notify_params { + char *id; + atclient_atkey *atkey; + char *value; + bool should_encrypt; + enum atclient_notify_operation operation; + enum atclient_notify_message_type message_type; + enum atclient_notify_priority priority; + enum atclient_notify_strategy strategy; + int latest_n; + char *notifier; + unsigned long notification_expiry; + unsigned char *shared_encryption_key; + + uint8_t _initializedfields[2]; +} atclient_notify_params; + +void atclient_notify_params_init(atclient_notify_params *params); +void atclient_notify_params_free(atclient_notify_params *params); + +bool atclient_notify_params_is_id_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_atkey_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_value_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_shouldencrypt_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_operation_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_message_type_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_priority_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_latest_n_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_notifier_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_notification_expiry_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_shared_encryption_key_initialized(atclient_notify_params *params); + +void atclient_notify_params_set_id_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_atkey_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_value_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_shouldencrypt_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_operation_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_message_type_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_priority_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_strategy_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_latest_n_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_notifier_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_notification_expiry_initialized(atclient_notify_params *params, const bool initialized); +void atclient_notify_params_set_shared_encryption_key_initialized(atclient_notify_params *params, const bool initialized); + +int atclient_notify_params_set_id(atclient_notify_params *params, const char *id, const size_t id_len); +int atclient_notify_params_set_atkey(atclient_notify_params *params, const atclient_atkey *atkey); +int atclient_notify_params_set_value(atclient_notify_params *params, const char *value, const size_t value_len); +int atclient_notify_params_set_should_encrypt(atclient_notify_params *params, const bool should_encrypt); +int atclient_notify_params_set_operation(atclient_notify_params *params, const enum atclient_notify_operation operation); +int atclient_notify_params_set_message_type(atclient_notify_params *params, const enum atclient_notify_message_type message_type); +int atclient_notify_params_set_priority(atclient_notify_params *params, const enum atclient_notify_priority priority); +int atclient_notify_params_set_strategy(atclient_notify_params *params, const enum atclient_notify_strategy strategy); +int atclient_notify_params_set_latest_n(atclient_notify_params *params, const int latest_n); +int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier, const size_t notifier_len); +int atclient_notify_params_set_notification_expiry(atclient_notify_params *params, const unsigned long notification_expiry); +int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, const unsigned char *shared_encryption_key, const size_t shared_encryption_key_len); + +void atclient_notify_params_unset_id(atclient_notify_params *params); +void atclient_notify_params_unset_atkey(atclient_notify_params *params); +void atclient_notify_params_unset_value(atclient_notify_params *params); +void atclient_notify_params_unset_should_encrypt(atclient_notify_params *params); +void atclient_notify_params_unset_operation(atclient_notify_params *params); +void atclient_notify_params_unset_message_type(atclient_notify_params *params); +void atclient_notify_params_unset_priority(atclient_notify_params *params); +void atclient_notify_params_unset_strategy(atclient_notify_params *params); +void atclient_notify_params_unset_latest_n(atclient_notify_params *params); +void atclient_notify_params_unset_notifier(atclient_notify_params *params); +void atclient_notify_params_unset_notification_expiry(atclient_notify_params *params); +void atclient_notify_params_unset_shared_encryption_key(atclient_notify_params *params); + +#endif // ATCLIENT_NOTIFY_PARAMS_H diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 73f164eb..8d944415 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -22,8 +22,53 @@ void atclient_atkey_init(atclient_atkey *atkey) { atclient_atkey_metadata_init(&(atkey->metadata)); } +int atclient_atkey_clone(atclient_atkey *dst, const atclient_atkey *src) { + int ret = 1; + if (dst == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "dst is NULL\n"); + goto exit; + } + if (src == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is NULL\n"); + goto exit; + } + if (atclient_atkey_is_key_initialized(src)) { + if ((ret = atclient_atkey_set_key(dst, src->key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); + goto exit; + } + } + if (atclient_atkey_is_sharedby_initialized(src)) { + if ((ret = atclient_atkey_set_sharedby(dst, src->sharedby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + goto exit; + } + } + if (atclient_atkey_is_sharedwith_initialized(src)) { + if ((ret = atclient_atkey_set_sharedwith(dst, src->sharedwith)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + goto exit; + } + } + if (atclient_atkey_is_namespacestr_initialized(src)) { + if ((ret = atclient_atkey_set_namespacestr(dst, src->namespacestr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + goto exit; + } + } + if ((ret = atclient_atkey_metadata_clone(&(dst->metadata), &(src->metadata))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_clone failed\n"); + goto exit; + } + ret = 0; + goto exit; +exit: { + return ret; +} +} + void atclient_atkey_free(atclient_atkey *atkey) { - atclient_atkey_metadata_free(&atkey->metadata); + atclient_atkey_metadata_free(&(atkey->metadata)); memset(atkey, 0, sizeof(atclient_atkey)); } diff --git a/packages/atclient/src/atnotification.c b/packages/atclient/src/atnotification.c index f168b898..16209c93 100644 --- a/packages/atclient/src/atnotification.c +++ b/packages/atclient/src/atnotification.c @@ -10,122 +10,122 @@ void atclient_atnotification_init(atclient_atnotification *notification) { } void atclient_atnotification_free(atclient_atnotification *notification) { - if (atclient_atnotification_id_is_initialized(notification)) { + if (atclient_atnotification_is_id_initialized(notification)) { atclient_atnotification_unset_id(notification); } - if (atclient_atnotification_from_is_initialized(notification)) { + if (atclient_atnotification_is_from_initialized(notification)) { atclient_atnotification_unset_from(notification); } - if (atclient_atnotification_to_is_initialized(notification)) { + if (atclient_atnotification_is_to_initialized(notification)) { atclient_atnotification_unset_to(notification); } - if (atclient_atnotification_key_is_initialized(notification)) { + if (atclient_atnotification_is_key_initialized(notification)) { atclient_atnotification_unset_key(notification); } - if (atclient_atnotification_value_is_initialized(notification)) { + if (atclient_atnotification_is_value_initialized(notification)) { atclient_atnotification_unset_value(notification); } - if (atclient_atnotification_operation_is_initialized(notification)) { + if (atclient_atnotification_is_operation_initialized(notification)) { atclient_atnotification_unset_operation(notification); } - if (atclient_atnotification_epochMillis_is_initialized(notification)) { - atclient_atnotification_unset_epochMillis(notification); + if (atclient_atnotification_is_epochmillis_initialized(notification)) { + atclient_atnotification_unset_epochmillis(notification); } - if (atclient_atnotification_messageType_is_initialized(notification)) { - atclient_atnotification_unset_messageType(notification); + if (atclient_atnotification_is_messagetype_initialized(notification)) { + atclient_atnotification_unset_messagetype(notification); } - if (atclient_atnotification_isEncrypted_is_initialized(notification)) { - atclient_atnotification_unset_isEncrypted(notification); + if (atclient_atnotification_is_isencrypted_initialized(notification)) { + atclient_atnotification_unset_isencrypted(notification); } - if (atclient_atnotification_encKeyName_is_initialized(notification)) { - atclient_atnotification_unset_encKeyName(notification); + if (atclient_atnotification_is_enckeyname_initialized(notification)) { + atclient_atnotification_unset_enckeyname(notification); } - if (atclient_atnotification_encAlgo_is_initialized(notification)) { - atclient_atnotification_unset_encAlgo(notification); + if (atclient_atnotification_is_encalgo_initialized(notification)) { + atclient_atnotification_unset_encalgo(notification); } - if (atclient_atnotification_ivNonce_is_initialized(notification)) { - atclient_atnotification_unset_ivNonce(notification); + if (atclient_atnotification_is_ivnonce_initialized(notification)) { + atclient_atnotification_unset_ivnonce(notification); } - if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { - atclient_atnotification_unset_skeEncKeyName(notification); + if (atclient_atnotification_is_skeenckeyname_initialized(notification)) { + atclient_atnotification_unset_skeenckeyname(notification); } - if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { - atclient_atnotification_unset_skeEncAlgo(notification); + if (atclient_atnotification_is_skeencalgo_initialized(notification)) { + atclient_atnotification_unset_skeencalgo(notification); } - if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { + if (atclient_atnotification_is_decryptedvalue_initialized(notification)) { atclient_atnotification_unset_decryptedvalue(notification); } } -bool atclient_atnotification_id_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); } -bool atclient_atnotification_from_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_from_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); } -bool atclient_atnotification_to_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_to_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); } -bool atclient_atnotification_key_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); } -bool atclient_atnotification_value_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); } -bool atclient_atnotification_operation_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); } -bool atclient_atnotification_epochMillis_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_epochmillis_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); } -bool atclient_atnotification_messageType_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_messagetype_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); } -bool atclient_atnotification_isEncrypted_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_isencrypted_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); } -bool atclient_atnotification_encKeyName_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_enckeyname_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_encAlgo_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_encalgo_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); } -bool atclient_atnotification_ivNonce_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_ivnonce_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); } -bool atclient_atnotification_skeEncKeyName_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_skeenckeyname_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_skeEncAlgo_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_skeencalgo_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); } -bool atclient_atnotification_decryptedvalue_is_initialized(const atclient_atnotification *notification) { +bool atclient_atnotification_is_decryptedvalue_initialized(const atclient_atnotification *notification) { return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); } @@ -180,7 +180,7 @@ void atclient_atnotification_operation_set_initialized(atclient_atnotification * } } -void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_epochmillis_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] |= ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; @@ -190,7 +190,7 @@ void atclient_atnotification_epochMillis_set_initialized(atclient_atnotification } } -void atclient_atnotification_messageType_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_messagetype_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] |= ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; @@ -199,7 +199,7 @@ void atclient_atnotification_messageType_set_initialized(atclient_atnotification } } -void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_isencrypted_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] |= ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; @@ -209,7 +209,7 @@ void atclient_atnotification_isEncrypted_set_initialized(atclient_atnotification } } -void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_enckeyname_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] |= ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; @@ -218,7 +218,7 @@ void atclient_atnotification_encKeyName_set_initialized(atclient_atnotification } } -void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_encalgo_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] |= ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; @@ -228,7 +228,7 @@ void atclient_atnotification_encAlgo_set_initialized(atclient_atnotification *no } } -void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_ivnonce_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] |= ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; @@ -238,7 +238,7 @@ void atclient_atnotification_ivNonce_set_initialized(atclient_atnotification *no } } -void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_skeenckeyname_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] |= ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; @@ -248,7 +248,7 @@ void atclient_atnotification_skeEncKeyName_set_initialized(atclient_atnotificati } } -void atclient_atnotification_skeEncAlgo_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_skeencalgo_set_initialized(atclient_atnotification *notification, bool initialized) { if (initialized) { notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] |= ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; @@ -269,7 +269,7 @@ void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotificat } void atclient_atnotification_unset_id(atclient_atnotification *notification) { - if (atclient_atnotification_id_is_initialized(notification)) { + if (atclient_atnotification_is_id_initialized(notification)) { free(notification->id); } notification->id = NULL; @@ -278,7 +278,7 @@ void atclient_atnotification_unset_id(atclient_atnotification *notification) { } void atclient_atnotification_unset_from(atclient_atnotification *notification) { - if (atclient_atnotification_from_is_initialized(notification)) { + if (atclient_atnotification_is_from_initialized(notification)) { free(notification->from); } notification->from = NULL; @@ -286,93 +286,93 @@ void atclient_atnotification_unset_from(atclient_atnotification *notification) { } void atclient_atnotification_unset_to(atclient_atnotification *notification) { - if(atclient_atnotification_to_is_initialized(notification)) { + if(atclient_atnotification_is_to_initialized(notification)) { free(notification->to); } notification->to = NULL; atclient_atnotification_to_set_initialized(notification, false); } void atclient_atnotification_unset_key(atclient_atnotification *notification) { - if(atclient_atnotification_key_is_initialized(notification)) { + if(atclient_atnotification_is_key_initialized(notification)) { free(notification->key); } notification->key = NULL; atclient_atnotification_key_set_initialized(notification, false); } void atclient_atnotification_unset_value(atclient_atnotification *notification) { - if (atclient_atnotification_value_is_initialized(notification)) { + if (atclient_atnotification_is_value_initialized(notification)) { free(notification->value); } notification->value = NULL; atclient_atnotification_value_set_initialized(notification, false); } void atclient_atnotification_unset_operation(atclient_atnotification *notification) { - if(atclient_atnotification_operation_is_initialized(notification)) { + if(atclient_atnotification_is_operation_initialized(notification)) { free(notification->operation); } notification->operation = NULL; atclient_atnotification_operation_set_initialized(notification, false); } -void atclient_atnotification_unset_epochMillis(atclient_atnotification *notification) { +void atclient_atnotification_unset_epochmillis(atclient_atnotification *notification) { notification->epochMillis = 0; - atclient_atnotification_epochMillis_set_initialized(notification, false); + atclient_atnotification_epochmillis_set_initialized(notification, false); } -void atclient_atnotification_unset_messageType(atclient_atnotification *notification) { - if(atclient_atnotification_messageType_is_initialized(notification)) { +void atclient_atnotification_unset_messagetype(atclient_atnotification *notification) { + if(atclient_atnotification_is_messagetype_initialized(notification)) { free(notification->messageType); } notification->messageType = NULL; - atclient_atnotification_messageType_set_initialized(notification, false); + atclient_atnotification_messagetype_set_initialized(notification, false); } -void atclient_atnotification_unset_isEncrypted(atclient_atnotification *notification) { +void atclient_atnotification_unset_isencrypted(atclient_atnotification *notification) { notification->isEncrypted = false; - atclient_atnotification_isEncrypted_set_initialized(notification, false); + atclient_atnotification_isencrypted_set_initialized(notification, false); } -void atclient_atnotification_unset_encKeyName(atclient_atnotification *notification) { - if(atclient_atnotification_encKeyName_is_initialized(notification)) { +void atclient_atnotification_unset_enckeyname(atclient_atnotification *notification) { + if(atclient_atnotification_is_enckeyname_initialized(notification)) { free(notification->encKeyName); } notification->encKeyName = NULL; - atclient_atnotification_encKeyName_set_initialized(notification, false); + atclient_atnotification_enckeyname_set_initialized(notification, false); } -void atclient_atnotification_unset_encAlgo(atclient_atnotification *notification) { - if(atclient_atnotification_encAlgo_is_initialized(notification)) { +void atclient_atnotification_unset_encalgo(atclient_atnotification *notification) { + if(atclient_atnotification_is_encalgo_initialized(notification)) { free(notification->encAlgo); } notification->encAlgo = NULL; - atclient_atnotification_encAlgo_set_initialized(notification, false); + atclient_atnotification_encalgo_set_initialized(notification, false); } -void atclient_atnotification_unset_ivNonce(atclient_atnotification *notification) { - if(atclient_atnotification_ivNonce_is_initialized(notification)) { +void atclient_atnotification_unset_ivnonce(atclient_atnotification *notification) { + if(atclient_atnotification_is_ivnonce_initialized(notification)) { free(notification->ivNonce); } notification->ivNonce = NULL; - atclient_atnotification_ivNonce_set_initialized(notification, false); + atclient_atnotification_ivnonce_set_initialized(notification, false); } -void atclient_atnotification_unset_skeEncKeyName(atclient_atnotification *notification) { - if(atclient_atnotification_skeEncKeyName_is_initialized(notification)) { +void atclient_atnotification_unset_skeenckeyname(atclient_atnotification *notification) { + if(atclient_atnotification_is_skeenckeyname_initialized(notification)) { free(notification->skeEncKeyName); } notification->skeEncKeyName = NULL; - atclient_atnotification_skeEncKeyName_set_initialized(notification, false); + atclient_atnotification_skeenckeyname_set_initialized(notification, false); } -void atclient_atnotification_unset_skeEncAlgo(atclient_atnotification *notification) { - if(atclient_atnotification_skeEncAlgo_is_initialized(notification)) { +void atclient_atnotification_unset_skeencalgo(atclient_atnotification *notification) { + if(atclient_atnotification_is_skeencalgo_initialized(notification)) { free(notification->skeEncAlgo); } notification->skeEncAlgo = NULL; - atclient_atnotification_skeEncAlgo_set_initialized(notification, false); + atclient_atnotification_skeencalgo_set_initialized(notification, false); } void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification) { - if(atclient_atnotification_decryptedvalue_is_initialized(notification)) { + if(atclient_atnotification_is_decryptedvalue_initialized(notification)) { free(notification->decryptedvalue); } notification->decryptedvalue = NULL; @@ -381,7 +381,7 @@ void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notif int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { int ret = 1; - if (atclient_atnotification_id_is_initialized(notification)) { + if (atclient_atnotification_is_id_initialized(notification)) { atclient_atnotification_unset_id(notification); } notification->id = malloc(sizeof(char) * (idlen + 1)); @@ -400,7 +400,7 @@ exit: { return ret; } int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { int ret = 1; - if (atclient_atnotification_from_is_initialized(notification)) { + if (atclient_atnotification_is_from_initialized(notification)) { atclient_atnotification_unset_from(notification); } notification->from = malloc(sizeof(char) * (fromlen + 1)); @@ -419,7 +419,7 @@ exit: { return ret; } int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { int ret = 1; - if (atclient_atnotification_to_is_initialized(notification)) { + if (atclient_atnotification_is_to_initialized(notification)) { atclient_atnotification_unset_to(notification); } notification->to = malloc(sizeof(char) * (tolen + 1)); @@ -438,7 +438,7 @@ exit: { return ret; } int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { int ret = 1; - if (atclient_atnotification_key_is_initialized(notification)) { + if (atclient_atnotification_is_key_initialized(notification)) { atclient_atnotification_unset_key(notification); } notification->key = malloc(sizeof(char) * (keylen + 1)); @@ -456,7 +456,7 @@ exit: { return ret; } int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen) { int ret = 1; - if (atclient_atnotification_value_is_initialized(notification)) { + if (atclient_atnotification_is_value_initialized(notification)) { atclient_atnotification_unset_value(notification); } notification->value = malloc(sizeof(char) * (valuelen + 1)); @@ -476,7 +476,7 @@ exit: { return ret; } int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, const size_t operationlen) { int ret = 1; - if (atclient_atnotification_operation_is_initialized(notification)) { + if (atclient_atnotification_is_operation_initialized(notification)) { atclient_atnotification_unset_operation(notification); } notification->operation = malloc(sizeof(char) * (operationlen + 1)); @@ -493,20 +493,20 @@ int atclient_atnotification_set_operation(atclient_atnotification *notification, exit: { return ret; } } -int atclient_atnotification_set_epochMillis(atclient_atnotification *notification, const size_t epochMillis) { - if (atclient_atnotification_epochMillis_is_initialized(notification)) { - atclient_atnotification_unset_epochMillis(notification); +int atclient_atnotification_set_epochmillis(atclient_atnotification *notification, const size_t epochMillis) { + if (atclient_atnotification_is_epochmillis_initialized(notification)) { + atclient_atnotification_unset_epochmillis(notification); } notification->epochMillis = epochMillis; - atclient_atnotification_epochMillis_set_initialized(notification, true); + atclient_atnotification_epochmillis_set_initialized(notification, true); return 0; } -int atclient_atnotification_set_messageType(atclient_atnotification *notification, const char *messageType, +int atclient_atnotification_set_messagetype(atclient_atnotification *notification, const char *messageType, const size_t messageTypelen) { int ret = 1; - if (atclient_atnotification_messageType_is_initialized(notification)) { - atclient_atnotification_unset_messageType(notification); + if (atclient_atnotification_is_messagetype_initialized(notification)) { + atclient_atnotification_unset_messagetype(notification); } notification->messageType = malloc(sizeof(char) * (messageTypelen + 1)); if (notification->messageType == NULL) { @@ -516,26 +516,26 @@ int atclient_atnotification_set_messageType(atclient_atnotification *notificatio } memcpy(notification->messageType, messageType, messageTypelen); *(notification->messageType + messageTypelen) = '\0'; - atclient_atnotification_messageType_set_initialized(notification, true); + atclient_atnotification_messagetype_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_isEncrypted(atclient_atnotification *notification, const bool isEncrypted) { - if (atclient_atnotification_isEncrypted_is_initialized(notification)) { - atclient_atnotification_unset_isEncrypted(notification); +int atclient_atnotification_set_isencrypted(atclient_atnotification *notification, const bool isEncrypted) { + if (atclient_atnotification_is_isencrypted_initialized(notification)) { + atclient_atnotification_unset_isencrypted(notification); } notification->isEncrypted = isEncrypted; - atclient_atnotification_isEncrypted_set_initialized(notification, true); + atclient_atnotification_isencrypted_set_initialized(notification, true); return 0; } -int atclient_atnotification_set_encKeyName(atclient_atnotification *notification, const char *encKeyName, +int atclient_atnotification_set_enckeyname(atclient_atnotification *notification, const char *encKeyName, const size_t encKeyNamelen) { int ret = 1; - if (atclient_atnotification_encKeyName_is_initialized(notification)) { - atclient_atnotification_unset_encKeyName(notification); + if (atclient_atnotification_is_enckeyname_initialized(notification)) { + atclient_atnotification_unset_enckeyname(notification); } notification->encKeyName = malloc(sizeof(char) * (encKeyNamelen + 1)); if (notification->encKeyName == NULL) { @@ -544,32 +544,32 @@ int atclient_atnotification_set_encKeyName(atclient_atnotification *notification } memcpy(notification->encKeyName, encKeyName, encKeyNamelen); *(notification->encKeyName + encKeyNamelen) = '\0'; - atclient_atnotification_encKeyName_set_initialized(notification, true); + atclient_atnotification_enckeyname_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_encAlgo(atclient_atnotification *notification, const char *encAlgo, +int atclient_atnotification_set_encalgo(atclient_atnotification *notification, const char *encAlgo, const size_t encAlgolen) { int ret = 1; - if (atclient_atnotification_encAlgo_is_initialized(notification)) { - atclient_atnotification_unset_encAlgo(notification); + if (atclient_atnotification_is_encalgo_initialized(notification)) { + atclient_atnotification_unset_encalgo(notification); } notification->encAlgo = malloc(sizeof(char) * (encAlgolen + 1)); memcpy(notification->encAlgo, encAlgo, encAlgolen); *(notification->encAlgo + encAlgolen) = '\0'; - atclient_atnotification_encAlgo_set_initialized(notification, true); + atclient_atnotification_encalgo_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, const char *ivNonce, +int atclient_atnotification_set_ivnonce(atclient_atnotification *notification, const char *ivNonce, const size_t ivNoncelen) { int ret = 1; - if (atclient_atnotification_ivNonce_is_initialized(notification)) { - atclient_atnotification_unset_ivNonce(notification); + if (atclient_atnotification_is_ivnonce_initialized(notification)) { + atclient_atnotification_unset_ivnonce(notification); } notification->ivNonce = malloc(sizeof(char) * (ivNoncelen + 1)); if (notification->ivNonce == NULL) { @@ -579,17 +579,17 @@ int atclient_atnotification_set_ivNonce(atclient_atnotification *notification, c } memcpy(notification->ivNonce, ivNonce, ivNoncelen); *(notification->ivNonce + ivNoncelen) = '\0'; - atclient_atnotification_ivNonce_set_initialized(notification, true); + atclient_atnotification_ivnonce_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notification, const char *skeEncKeyName, +int atclient_atnotification_set_skeenckeyname(atclient_atnotification *notification, const char *skeEncKeyName, const size_t skeEncKeyNamelen) { int ret = 1; - if (atclient_atnotification_skeEncKeyName_is_initialized(notification)) { - atclient_atnotification_unset_skeEncKeyName(notification); + if (atclient_atnotification_is_skeenckeyname_initialized(notification)) { + atclient_atnotification_unset_skeenckeyname(notification); } notification->skeEncKeyName = malloc(sizeof(char) * (skeEncKeyNamelen + 1)); if (notification->skeEncKeyName == NULL) { @@ -599,17 +599,17 @@ int atclient_atnotification_set_skeEncKeyName(atclient_atnotification *notificat } memcpy(notification->skeEncKeyName, skeEncKeyName, skeEncKeyNamelen); *(notification->skeEncKeyName + skeEncKeyNamelen) = '\0'; - atclient_atnotification_skeEncKeyName_set_initialized(notification, true); + atclient_atnotification_skeenckeyname_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification, const char *skeEncAlgo, +int atclient_atnotification_set_skeencalgo(atclient_atnotification *notification, const char *skeEncAlgo, const size_t skeEncAlgolen) { int ret = 1; - if (atclient_atnotification_skeEncAlgo_is_initialized(notification)) { - atclient_atnotification_unset_skeEncAlgo(notification); + if (atclient_atnotification_is_skeencalgo_initialized(notification)) { + atclient_atnotification_unset_skeencalgo(notification); } notification->skeEncAlgo = malloc(sizeof(char) * (skeEncAlgolen + 1)); if (notification->skeEncAlgo == NULL) { @@ -619,7 +619,7 @@ int atclient_atnotification_set_skeEncAlgo(atclient_atnotification *notification } memcpy(notification->skeEncAlgo, skeEncAlgo, skeEncAlgolen); *(notification->skeEncAlgo + skeEncAlgolen) = '\0'; - atclient_atnotification_skeEncAlgo_set_initialized(notification, true); + atclient_atnotification_skeencalgo_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } @@ -628,7 +628,7 @@ exit: { return ret; } int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const char *decryptedvalue, const size_t decryptedvaluelen) { int ret = 1; - if (atclient_atnotification_decryptedvalue_is_initialized(notification)) { + if (atclient_atnotification_is_decryptedvalue_initialized(notification)) { atclient_atnotification_unset_decryptedvalue(notification); } notification->decryptedvalue = malloc(sizeof(unsigned char) * (decryptedvaluelen + 1)); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index c7a9bcae..1b5647d7 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -129,6 +129,195 @@ void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata) { memset(metadata, 0, sizeof(atclient_atkey_metadata)); } +int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_atkey_metadata *src) { + int ret = 1; + + if (dst == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "dst is NULL\n"); + goto exit; + } + + if (src == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is NULL\n"); + goto exit; + } + + if (atclient_atkey_metadata_is_createdby_initialized(src)) { + if ((ret = set_createdby(dst, src->createdby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_updatedby_initialized(src)) { + if((ret = set_updatedby(dst, src->updatedby)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_status_initialized(src)) { + if((ret = set_status(dst, src->status)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_version_initialized(src)) { + set_version(dst, src->version); + } + + if(atclient_atkey_metadata_is_expiresat_initialized(src)) { + if((ret = set_expiresat(dst, src->expiresat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_availableat_initialized(src)) { + if((ret = set_availableat(dst, src->availableat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_refreshat_initialized(src)) { + if((ret = set_refreshat(dst, src->refreshat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_createdat_initialized(src)) { + if((ret = set_createdat(dst, src->createdat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_updatedat_initialized(src)) { + if((ret = set_updatedat(dst, src->updatedat)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_ispublic_initialized(src)) { + set_ispublic(dst, src->ispublic); + } + + if(atclient_atkey_metadata_is_iscached_initialized(src)) { + set_iscached(dst, src->iscached); + } + + if(atclient_atkey_metadata_is_ttl_initialized(src)) { + set_ttl(dst, src->ttl); + } + + if(atclient_atkey_metadata_is_ttb_initialized(src)) { + set_ttb(dst, src->ttb); + } + + if(atclient_atkey_metadata_is_ttr_initialized(src)) { + set_ttr(dst, src->ttr); + } + + if(atclient_atkey_metadata_is_ccd_initialized(src)) { + set_ccd(dst, src->ccd); + } + + if(atclient_atkey_metadata_is_isbinary_initialized(src)) { + set_isbinary(dst, src->isbinary); + } + + if(atclient_atkey_metadata_is_isencrypted_initialized(src)) { + set_isencrypted(dst, src->isencrypted); + } + + if(atclient_atkey_metadata_is_datasignature_initialized(src)) { + if((ret = set_datasignature(dst, src->datasignature)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_sharedkeystatus_initialized(src)) { + if((ret = set_sharedkeystatus(dst, src->sharedkeystatus)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_sharedkeyenc_initialized(src)) { + if((ret = set_sharedkeyenc(dst, src->sharedkeyenc)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_pubkeyhash_initialized(src)) { + if((ret = set_pubkeyhash(dst, src->pubkeyhash)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_pubkeyalgo_initialized(src)) { + if((ret = set_pubkeyalgo(dst, src->pubkeyalgo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_encoding_initialized(src)) { + if((ret = set_encoding(dst, src->encoding)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_enckeyname_initialized(src)) { + if((ret = set_enckeyname(dst, src->enckeyname)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_encalgo_initialized(src)) { + if((ret = set_encalgo(dst, src->encalgo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_ivnonce_initialized(src)) { + if((ret = set_ivnonce(dst, src->ivnonce)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_skeenckeyname_initialized(src)) { + if((ret = set_skeenckeyname(dst, src->skeenckeyname)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + goto exit; + } + } + + if(atclient_atkey_metadata_is_skeencalgo_initialized(src)) { + if((ret = set_skeencalgo(dst, src->skeencalgo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + goto exit; + } + } + + ret = 0; + goto exit; +exit: { + return ret; +} +} + int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr) { int ret = 1; diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index b5b424a0..a7ccbd3e 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -173,7 +173,7 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m messagebody); goto exit; } - if (atclient_atnotification_isEncrypted_is_initialized(&(message->notification)) && + if (atclient_atnotification_is_isencrypted_initialized(&(message->notification)) && message->notification.isEncrypted == true) { // if key contains \"shared_key\", could be in the middle of string, ignore it if (strstr(message->notification.key, "shared_key") != NULL) { @@ -401,7 +401,7 @@ static int parse_notification(atclient_atnotification *notification, const char cJSON *epochMillis = cJSON_GetObjectItem(root, "epochMillis"); if (epochMillis != NULL) { - if((ret = atclient_atnotification_set_epochMillis(notification, epochMillis->valueint)) != 0) { + if((ret = atclient_atnotification_set_epochmillis(notification, epochMillis->valueint)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epochMillis\n"); goto exit; } @@ -416,7 +416,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_messageType(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_messagetype(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification messageType\n"); goto exit; } @@ -424,7 +424,7 @@ static int parse_notification(atclient_atnotification *notification, const char cJSON *isEncrypted = cJSON_GetObjectItem(root, "isEncrypted"); if (isEncrypted != NULL) { - if((ret = atclient_atnotification_set_isEncrypted(notification, isEncrypted->valueint)) != 0) { + if((ret = atclient_atnotification_set_isencrypted(notification, isEncrypted->valueint)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification isEncrypted\n"); goto exit; } @@ -442,7 +442,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_encKeyName(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_enckeyname(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encKeyName\n"); goto exit; } @@ -458,7 +458,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_encAlgo(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_encalgo(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encAlgo\n"); goto exit; } @@ -474,7 +474,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_ivNonce(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_ivnonce(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ivNonce\n"); goto exit; } @@ -490,7 +490,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_skeEncKeyName(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_skeenckeyname(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncKeyName\n"); goto exit; } @@ -506,7 +506,7 @@ static int parse_notification(atclient_atnotification *notification, const char val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_skeEncAlgo(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_skeencalgo(notification, val, vallen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncAlgo\n"); goto exit; } @@ -553,13 +553,13 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not // 1. make sure everything we need is there // 1a. check if value is initialized - if (!atclient_atnotification_value_is_initialized(notification)) { + if (!atclient_atnotification_is_value_initialized(notification)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Value is not initialized. Nothing was found to decrypt.\n"); goto exit; } // 1b. some warnings - if (!atclient_atnotification_isEncrypted_is_initialized(notification)) { + if (!atclient_atnotification_is_isencrypted_initialized(notification)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, "isEncrypted field was found to be uninitialized, we don't know for sure if we're decrypting " "something that's even encrypted.\n"); @@ -571,7 +571,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 2. get iv - if (atclient_atnotification_ivNonce_is_initialized(notification) && + if (atclient_atnotification_is_ivnonce_initialized(notification) && !atclient_stringutils_starts_with(notification->ivNonce, "null")) { size_t ivlen; ret = atchops_base64_decode((unsigned char *)notification->ivNonce, strlen(notification->ivNonce), iv, diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 5a8ffd61..07edab4d 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -15,6 +15,8 @@ #include #include +#define TAG "atclient_notify" + #define MAX(a, b) ((a) > (b) ? (a) : (b)) static int generate_cmd(const atclient_notify_params *params, const char *cmdvalue, const size_t cmdvaluelen, @@ -22,36 +24,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval static size_t calculate_cmd_size(const atclient_notify_params *params, const size_t cmdvaluelen, size_t *atkeyolen, size_t *medatastrolen); -#define TAG "atclient_notify" - -void atclient_notify_params_init(atclient_notify_params *params) { - memset(params, 0, sizeof(atclient_notify_params)); - memset(params->id, 0, sizeof(char) * 37); // uuid v4 + '\0' - params->atkey = NULL; - params->value = NULL; - params->operation = ATCLIENT_NOTIFY_OPERATION_NONE; - params->message_type = ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY; - params->priority = ATCLIENT_NOTIFY_PRIORITY_LOW; - params->strategy = ATCLIENT_NOTIFY_STRATEGY_ALL; - params->latest_n = 1; - params->notifier = ATCLIENT_DEFAULT_NOTIFIER; - params->notification_expiry = 24 * 60 * 60 * 1000; // 24 hours in milliseconds - params->shouldencrypt = true; - params->sharedenckeybase64 = NULL; -} - -void atclient_notify_params_create(atclient_notify_params *params, enum atclient_notify_operation operation, - atclient_atkey *atkey, const char *value, bool shouldencrypt) { - params->operation = operation; - params->atkey = atkey; - params->value = (char *)value; - params->shouldencrypt = shouldencrypt; -} - -void atclient_notify_params_free(atclient_notify_params *params) { memset(params, 0, sizeof(atclient_notify_params)); } - -int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notification_id) { - +int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notification_id) { int ret = 1; if (ctx->async_read) { @@ -70,7 +43,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific size_t cmdsize = 0; // Step 1 encrypt the value if needed - if (params->value != NULL && params->shouldencrypt) { + if (params->value != NULL && params->should_encrypt) { const size_t ciphertextsize = (size_t)(((strlen(params->value) * 2) + 15) / 16) * 16; // round up to the next multiple of 16 unsigned char ciphertext[ciphertextsize]; @@ -92,20 +65,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific memset(sharedenckey, 0, sizeof(unsigned char) * sharedenckeysize); size_t sharedenckeylen; - if (params->sharedenckeybase64 != NULL) { - ret = atchops_base64_decode((unsigned char *)params->sharedenckeybase64, strlen(params->sharedenckeybase64), - sharedenckey, sharedenckeysize, &sharedenckeylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed with code %d\n", ret); - return ret; - } - if (sharedenckeylen != sharedenckeysize) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedenckeybase64 decode failed. Expected %lu but got %lu\n", - sharedenckeysize, sharedenckeylen); - ret = 1; - return ret; - } - } else { + if (params->shared_encryption_key == NULL) { char *recipient_atsign_with_at = NULL; if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", @@ -165,7 +125,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific memcpy(cmdvalue, ciphertextbase64, ciphertextbase64len); cmdvalue[ciphertextbase64len] = '\0'; cmdvaluelen = ciphertextbase64len; - } else if (params->value != NULL && !params->shouldencrypt) { + } else if (params->value != NULL && !params->should_encrypt) { cmdvaluelen = strlen(params->value); cmdvalue = malloc(sizeof(char) * (cmdvaluelen + 1)); memcpy(cmdvalue, params->value, cmdvaluelen); @@ -206,8 +166,13 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char *notific atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Notification id too long\n"); return 1; } - strncpy(notification_id, data, datalen); - notification_id[datalen] = '\0'; + *notification_id = malloc(sizeof(char) * (datalen + 1)); + if (*notification_id == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); + return 1; + } + memcpy(*notification_id, data, datalen); + (*notification_id)[datalen] = '\0'; } ret = 0; } else { @@ -233,49 +198,53 @@ static size_t calculate_cmd_size(const atclient_notify_params *params, const siz cmdsize += strlen("notify"); - if (strlen(params->id) > 0) { + if (atclient_notify_params_is_id_initialized(params) && strlen(params->id) > 0) { cmdsize += strlen(":id:") + strlen(params->id); // ":id:" + 36 char uuid } - if (params->operation != ATCLIENT_NOTIFY_OPERATION_NONE) { + if (atclient_notify_params_is_operation_initialized(params) && params->operation != ATCLIENT_NOTIFY_OPERATION_NONE) { cmdsize += strlen(":") + strlen(atclient_notify_operation_str[params->operation]); // ":update" | ":delete" } - if (params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { + if (atclient_notify_params_is_message_type_initialized(params) && + params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { cmdsize += strlen(":messageType:") + strlen(atclient_notify_message_type_str[params->message_type]); // ":messageType" + ":text" | ":key" } - if (params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { + if (atclient_notify_params_is_priority_initialized(params) && params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { cmdsize += strlen(":priority:") + strlen(atclient_notify_priority_str[params->priority]); // ":priority" + ":low" | ":medium" | ":high" } - if (params->strategy != ATCLIENT_NOTIFY_STRATEGY_NONE) { + if (atclient_notify_params_is_strategy_initialized(params) && params->strategy != ATCLIENT_NOTIFY_STRATEGY_NONE) { cmdsize += strlen(":strategy:") + strlen(atclient_notify_strategy_str[params->strategy]); // ":strategy" + ":all" | ":latest" } - if (params->notification_expiry > 0) { + if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { cmdsize += strlen(":ttln:") + long_strlen(params->notification_expiry); // :$ttln } - const size_t metadatastrlen = atclient_atkey_metadata_protocol_strlen(¶ms->atkey->metadata); - cmdsize += strlen(":") + metadatastrlen; // :$metadata + if (atclient_notify_params_is_atkey_initialized(params)) { - const size_t atkeylen = atclient_atkey_strlen(params->atkey); - cmdsize += strlen(":") + atkeylen; // :$atkey + const size_t metadatastrlen = atclient_atkey_metadata_protocol_strlen(¶ms->atkey->metadata); + cmdsize += strlen(":") + metadatastrlen; // :$metadata - if (cmdvaluelen > 0) { - cmdsize += strlen(":") + cmdvaluelen; // :$value + const size_t atkeylen = atclient_atkey_strlen(params->atkey); + cmdsize += strlen(":") + atkeylen; // :$atkey + + if (cmdvaluelen > 0) { + cmdsize += strlen(":") + cmdvaluelen; // :$value + } + *atkeyolen = atkeylen; + *medatastrolen = metadatastrlen; } cmdsize += strlen("\r\n"); cmdsize += 1; // null terminator - *atkeyolen = atkeylen; - *medatastrolen = metadatastrlen; return cmdsize; } @@ -317,32 +286,32 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval snprintf(cmd + off, cmdsize - off, "notify"); off += strlen("notify"); - if (strlen(params->id) > 0) { + if (atclient_notify_params_is_id_initialized(params) && strlen(params->id) > 0) { snprintf(cmd + off, cmdsize - off, ":id:%s", params->id); off += strlen(":id:") + strlen(params->id); } - if (params->operation != ATCLIENT_NOTIFY_OPERATION_NONE) { + if (atclient_notify_params_is_operation_initialized(params) && params->operation != ATCLIENT_NOTIFY_OPERATION_NONE) { snprintf(cmd + off, cmdsize - off, ":%s", atclient_notify_operation_str[params->operation]); off += strlen(":") + strlen(atclient_notify_operation_str[params->operation]); } - if (params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { + if (atclient_notify_params_is_message_type_initialized(params) && params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { snprintf(cmd + off, cmdsize - off, ":messageType:%s", atclient_notify_message_type_str[params->message_type]); off += strlen(":messageType:") + strlen(atclient_notify_message_type_str[params->message_type]); } - if (params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { + if (atclient_notify_params_is_priority_initialized(params) && params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { snprintf(cmd + off, cmdsize - off, ":priority:%s", atclient_notify_priority_str[params->priority]); off += strlen(":priority:") + strlen(atclient_notify_priority_str[params->priority]); } - if (params->strategy != ATCLIENT_NOTIFY_STRATEGY_NONE) { + if (atclient_notify_params_is_strategy_initialized(params) && params->strategy != ATCLIENT_NOTIFY_STRATEGY_NONE) { snprintf(cmd + off, cmdsize - off, ":strategy:%s", atclient_notify_strategy_str[params->strategy]); off += strlen(":strategy:") + strlen(atclient_notify_strategy_str[params->strategy]); } - if (params->notification_expiry > 0) { + if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { snprintf(cmd + off, cmdsize - off, ":ttln:%lu", params->notification_expiry); off += strlen(":ttln:") + long_strlen(params->notification_expiry); } diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c new file mode 100644 index 00000000..0aede01c --- /dev/null +++ b/packages/atclient/src/notify_params.c @@ -0,0 +1,448 @@ +#include "atclient/notify_params.h" +#include "atclient/constants.h" +#include +#include +#include + +#define TAG "atclient_notify_params" + +void atclient_notify_params_init(atclient_notify_params *params) { + memset(params, 0, sizeof(atclient_notify_params)); + params->id = NULL; + params->atkey = NULL; + params->value = NULL; + params->operation = ATCLIENT_NOTIFY_OPERATION_NONE; + params->message_type = ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY; + params->priority = ATCLIENT_NOTIFY_PRIORITY_LOW; + params->strategy = ATCLIENT_NOTIFY_STRATEGY_ALL; + params->latest_n = 1; + params->notifier = ATCLIENT_DEFAULT_NOTIFIER; + params->notification_expiry = 24 * 60 * 60 * 1000; // 24 hours in milliseconds + params->should_encrypt = true; + params->shared_encryption_key = NULL; + memset(params->_initializedfields, 0, sizeof(params->_initializedfields)); +} + +void atclient_notify_params_free(atclient_notify_params *params) { + atclient_notify_params_unset_id(params); + atclient_notify_params_unset_atkey(params); + atclient_notify_params_unset_value(params); + atclient_notify_params_unset_operation(params); + atclient_notify_params_unset_message_type(params); + atclient_notify_params_unset_priority(params); + atclient_notify_params_unset_strategy(params); + atclient_notify_params_unset_latest_n(params); + atclient_notify_params_unset_notifier(params); + atclient_notify_params_unset_notification_expiry(params); + atclient_notify_params_unset_should_encrypt(params); + atclient_notify_params_unset_shared_encryption_key(params); + memset(params, 0, sizeof(atclient_notify_params)); +} + +bool atclient_notify_params_is_id_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] & ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; +} + +bool atclient_notify_params_is_atkey_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; +} + +bool atclient_notify_params_is_value_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] & ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; +} + +bool atclient_notify_params_is_shouldencrypt_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] & + ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; +} + +bool atclient_notify_params_is_operation_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] & + ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; +} + +bool atclient_notify_params_is_message_type_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] & + ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; +} + +bool atclient_notify_params_is_priority_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] & + ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; +} + +bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] & + ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; +} + +bool atclient_notify_params_is_latest_n_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] & + ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; +} + +bool atclient_notify_params_is_notifier_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] & + ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; +} + +bool atclient_notify_params_is_notification_expiry_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] & + ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; +} + +bool atclient_notify_params_is_shared_encryption_key_initialized(atclient_notify_params *params) { + return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] & + ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; +} + +void atclient_notify_params_set_id_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; + } +} + +void atclient_notify_params_set_atkey_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; + } +} + +void atclient_notify_params_set_value_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] |= ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; + } +} + +void atclient_notify_params_set_shouldencrypt_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] |= + ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] &= + ~ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; + } +} + +void atclient_notify_params_set_operation_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] |= ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; + } +} + +void atclient_notify_params_set_message_type_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] |= + ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] &= + ~ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; + } +} + +void atclient_notify_params_set_priority_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; + } +} + +void atclient_notify_params_set_strategy_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; + } +} + +void atclient_notify_params_set_latest_n_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] |= ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; + } +} + +void atclient_notify_params_set_notifier_initialized(atclient_notify_params *params, const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] |= ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; + } +} + +void atclient_notify_params_set_notification_expiry_initialized(atclient_notify_params *params, + const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] |= + ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] &= + ~ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; + } +} + +void atclient_notify_params_set_shared_encryption_key_initialized(atclient_notify_params *params, + const bool initialized) { + if (initialized) { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } else { + params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } +} + +int atclient_notify_params_set_id(atclient_notify_params *params, const char *id, const size_t id_len) { + int ret = 1; + if (id_len != 36) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid id length: %lu, should be a UUIDv4\n", id_len); + goto exit; + } + + if (atclient_notify_params_is_id_initialized(params)) { + atclient_notify_params_unset_id(params); + } + + const size_t id_size = id_len + 1; + if ((params->id = (char *)malloc(sizeof(char) * id_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for id\n"); + goto exit; + } + + memcpy(params->id, id, id_len); + params->id[id_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_notify_params_set_atkey(atclient_notify_params *params, const atclient_atkey *atkey) { + int ret = 1; + if (atclient_notify_params_is_atkey_initialized(params)) { + atclient_notify_params_unset_atkey(params); + } + + if ((params->atkey = (atclient_atkey *)malloc(sizeof(atclient_atkey))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for atkey\n"); + goto exit; + } + + atclient_atkey_init(params->atkey); + if ((ret = atclient_atkey_clone(params->atkey, atkey)) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_clone: %d\n", ret); + goto exit; + } + atclient_notify_params_set_atkey_initialized(params, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_notify_params_set_value(atclient_notify_params *params, const char *value, const size_t value_len) { + int ret = 1; + + if (atclient_notify_params_is_value_initialized(params)) { + atclient_notify_params_unset_value(params); + } + + if ((params->value = (char *)malloc(sizeof(char) * (value_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; + } + memcpy(params->value, value, value_len); + params->value[value_len] = '\0'; + + atclient_notify_params_set_value_initialized(params, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_notify_params_set_should_encrypt(atclient_notify_params *params, const bool should_encrypt) { + params->should_encrypt = should_encrypt; + atclient_notify_params_set_shouldencrypt_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_operation(atclient_notify_params *params, + const enum atclient_notify_operation operation) { + params->operation = operation; + atclient_notify_params_set_operation_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_message_type(atclient_notify_params *params, + const enum atclient_notify_message_type message_type) { + params->message_type = message_type; + atclient_notify_params_set_message_type_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_priority(atclient_notify_params *params, const enum atclient_notify_priority priority) { + params->priority = priority; + atclient_notify_params_set_priority_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_strategy(atclient_notify_params *params, const enum atclient_notify_strategy strategy) { + params->strategy = strategy; + atclient_notify_params_set_strategy_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_latest_n(atclient_notify_params *params, const int latest_n) { + params->latest_n = latest_n; + atclient_notify_params_set_latest_n_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier, + const size_t notifier_len) { + int ret = 1; + + if (atclient_notify_params_is_notifier_initialized(params)) { + atclient_notify_params_unset_notifier(params); + } + + if ((params->notifier = (char *)malloc(sizeof(char) * (notifier_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notifier\n"); + goto exit; + } + + memcpy(params->notifier, notifier, notifier_len); + params->notifier[notifier_len] = '\0'; + + atclient_notify_params_set_notifier_initialized(params, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atclient_notify_params_set_notification_expiry(atclient_notify_params *params, + const unsigned long notification_expiry) { + params->notification_expiry = notification_expiry; + atclient_notify_params_set_notification_expiry_initialized(params, true); + return 0; +} + +int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, + const unsigned char *shared_encryption_key, + const size_t shared_encryption_key_len) { + int ret = 1; + + if (atclient_notify_params_is_shared_encryption_key_initialized(params)) { + atclient_notify_params_unset_shared_encryption_key(params); + } + + if ((params->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_len)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for shared_encryption_key\n"); + goto exit; + } + + memcpy(params->shared_encryption_key, shared_encryption_key, shared_encryption_key_len); + params->shared_encryption_key[shared_encryption_key_len] = '\0'; + + atclient_notify_params_set_shared_encryption_key_initialized(params, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_notify_params_unset_id(atclient_notify_params *params) { + if (atclient_notify_params_is_id_initialized(params)) { + free(params->id); + } + params->id = NULL; + atclient_notify_params_set_id_initialized(params, false); +} + +void atclient_notify_params_unset_atkey(atclient_notify_params *params) { + if (atclient_notify_params_is_atkey_initialized(params)) { + atclient_atkey_free(params->atkey); + free(params->atkey); + } + params->atkey = NULL; + atclient_notify_params_set_atkey_initialized(params, false); +} + +void atclient_notify_params_unset_value(atclient_notify_params *params) { + if (atclient_notify_params_is_value_initialized(params)) { + free(params->value); + } + params->value = NULL; + atclient_notify_params_set_value_initialized(params, false); +} + +void atclient_notify_params_unset_should_encrypt(atclient_notify_params *params) { + params->should_encrypt = true; + atclient_notify_params_set_shouldencrypt_initialized(params, false); +} + +void atclient_notify_params_unset_operation(atclient_notify_params *params) { + params->operation = ATCLIENT_NOTIFY_OPERATION_NONE; + atclient_notify_params_set_operation_initialized(params, false); +} + +void atclient_notify_params_unset_message_type(atclient_notify_params *params) { + params->message_type = ATCLIENT_NOTIFY_MESSAGE_TYPE_KEY; + atclient_notify_params_set_message_type_initialized(params, false); +} + +void atclient_notify_params_unset_priority(atclient_notify_params *params) { + params->priority = ATCLIENT_NOTIFY_PRIORITY_LOW; + atclient_notify_params_set_priority_initialized(params, false); +} + +void atclient_notify_params_unset_strategy(atclient_notify_params *params) { + params->strategy = ATCLIENT_NOTIFY_STRATEGY_ALL; + atclient_notify_params_set_strategy_initialized(params, false); +} + +void atclient_notify_params_unset_latest_n(atclient_notify_params *params) { + params->latest_n = 1; + atclient_notify_params_set_latest_n_initialized(params, false); +} + +void atclient_notify_params_unset_notifier(atclient_notify_params *params) { + if (atclient_notify_params_is_notifier_initialized(params)) { + free(params->notifier); + } + params->notifier = ATCLIENT_DEFAULT_NOTIFIER; + atclient_notify_params_set_notifier_initialized(params, false); +} + +void atclient_notify_params_unset_notification_expiry(atclient_notify_params *params) { + params->notification_expiry = 24 * 60 * 60 * 1000; + atclient_notify_params_set_notification_expiry_initialized(params, false); +} + +void atclient_notify_params_unset_shared_encryption_key(atclient_notify_params *params) { + if (atclient_notify_params_is_shared_encryption_key_initialized(params)) { + free(params->shared_encryption_key); + } + params->shared_encryption_key = NULL; + atclient_notify_params_set_shared_encryption_key_initialized(params, false); +} diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index b6ad137b..19d9fb0d 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -176,7 +176,26 @@ static int send_notification(atclient *atclient) { goto exit; } - atclient_notify_params_create(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE, &atkey, ATKEY_VALUE, true); + if((ret = atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set operation: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_atkey(¶ms, &atkey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_should_encrypt(¶ms, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set should_encrypt: %d\n", ret); + goto exit; + } + params.notification_expiry = 1000; if ((ret = atclient_notify(atclient, ¶ms, NULL)) != 0) { @@ -206,7 +225,7 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) continue; } - if (!atclient_atnotification_decryptedvalue_is_initialized(&(message.notification))) { + if (!atclient_atnotification_is_decryptedvalue_initialized(&(message.notification))) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted value is not initialized\n"); tries++; continue; diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index 3d4e78ad..d151e049 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -164,7 +164,25 @@ static int test_1_notify(atclient *atclient, char *notification_id) { goto exit; } - atclient_notify_params_create(¶ms, ATNOTIFICATION_OPERATION, &atkey, ATKEY_VALUE, true); + if ((ret = atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set operation: %d\n", ret); + goto exit; + } + + if ((ret = atclient_notify_params_set_atkey(¶ms, &atkey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey: %d\n", ret); + goto exit; + } + + if ((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); + goto exit; + } + + if ((ret = atclient_notify_params_set_should_encrypt(¶ms, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set should_encrypt: %d\n", ret); + goto exit; + } if ((ret = atclient_notify(atclient, ¶ms, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to notify: %d\n", ret); @@ -201,7 +219,25 @@ static int test_2_notify_long_text(atclient *atclient, char *notification_id) { goto exit; } - atclient_notify_params_create(¶ms, ATNOTIFICATION_OPERATION, &atkey, VERY_LONG_TEXT, true); + if((ret = atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set operation: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_atkey(¶ms, &atkey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_value(¶ms, VERY_LONG_TEXT, strlen(VERY_LONG_TEXT))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); + goto exit; + } + + if((ret = atclient_notify_params_set_should_encrypt(¶ms, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set should_encrypt: %d\n", ret); + goto exit; + } if ((ret = atclient_notify(atclient, ¶ms, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to notify: %d\n", ret); From 8300d3cd79f56d855d9add874820b5ed1971d77d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 09:53:53 -0400 Subject: [PATCH 111/193] fix: examples --- examples/desktop/at_talk/src/main.c | 21 +++++++++++++++++++-- examples/desktop/events/notify.c | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index a332157c..521c3a4a 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -156,8 +156,25 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); } - atclient_notify_params_create(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE, &atkey, line, true); - params.notification_expiry = 5000; + 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) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_atkey: %d\n", ret); + } + + if((ret = atclient_notify_params_set_value(¶ms, line, strlen(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) { + 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) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_notification_expiry: %d\n", ret); + } pthread_mutex_lock(&client_mutex); if ((ret = atclient_notify(&atclient1, ¶ms, NULL)) != 0) { diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index d490207d..4f8175b9 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -122,6 +122,21 @@ int main(int argc, char *argv[]) { notify_params.value = ATKEY_VALUE; notify_params.operation = ATCLIENT_NOTIFY_OPERATION_UPDATE; + if((ret = atclient_notify_params_set_atkey(¬ify_params, &atkey)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey\n"); + goto exit; + } + + if((ret = atclient_notify_params_set_value(¬ify_params, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value\n"); + goto exit; + } + + if((ret = atclient_notify_params_set_operation(¬ify_params, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set operation\n"); + goto exit; + } + if ((ret = atclient_notify(&atclient, ¬ify_params, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to notify\n"); goto exit; From 43fe45f548032c30f89a65d668a66c51e8ad98b8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 09:54:34 -0400 Subject: [PATCH 112/193] fix: --- examples/desktop/events/notify.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 4f8175b9..9ec18c62 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -118,10 +118,6 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - notify_params.atkey = &atkey; - notify_params.value = ATKEY_VALUE; - notify_params.operation = ATCLIENT_NOTIFY_OPERATION_UPDATE; - if((ret = atclient_notify_params_set_atkey(¬ify_params, &atkey)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey\n"); goto exit; From 95aaf7eb7503783aa2f9067b983fc71baecb8515 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 11:55:51 -0400 Subject: [PATCH 113/193] chore: snake case --- examples/desktop/at_talk/src/main.c | 8 +- examples/desktop/crud/delete.c | 2 +- examples/desktop/crud/get_publickey.c | 4 +- examples/desktop/crud/get_selfkey.c | 2 +- examples/desktop/crud/get_sharedkey.c | 2 +- examples/desktop/crud/put_publickey.c | 2 +- examples/desktop/crud/put_selfkey.c | 2 +- examples/desktop/crud/put_sharedkey.c | 2 +- examples/desktop/events/monitor.c | 6 +- examples/desktop/events/notify.c | 2 +- examples/desktop/events/resilient_monitor.c | 6 +- examples/desktop/repl/src/main.c | 6 +- packages/atclient/include/atclient/atclient.h | 50 +- packages/atclient/include/atclient/atkey.h | 124 +- packages/atclient/include/atclient/atkeys.h | 104 +- .../atclient/include/atclient/atkeysfile.h | 32 +- .../include/atclient/atnotification.h | 116 +- .../atclient/include/atclient/connection.h | 14 +- .../include/atclient/encryption_key_helpers.h | 4 +- packages/atclient/include/atclient/metadata.h | 213 +- .../atclient/include/atclient/notify_params.h | 14 +- .../atclient/include/atclient/stringutils.h | 8 +- packages/atclient/include/atclient/version.h | 2 +- packages/atclient/src/atclient.c | 126 +- packages/atclient/src/atclient_delete.c | 42 +- packages/atclient/src/atclient_get_atkeys.c | 8 +- .../atclient/src/atclient_get_publickey.c | 94 +- packages/atclient/src/atclient_get_selfkey.c | 127 +- .../atclient/src/atclient_get_sharedkey.c | 162 +- packages/atclient/src/atclient_put.c | 176 +- packages/atclient/src/atclient_utils.c | 34 +- packages/atclient/src/atkey.c | 403 ++-- packages/atclient/src/atkeys.c | 633 ++--- packages/atclient/src/atkeysfile.c | 312 +-- packages/atclient/src/atnotification.c | 492 ++-- .../atclient/src/encryption_key_helpers.c | 104 +- packages/atclient/src/metadata.c | 2053 +++++++++-------- packages/atclient/src/monitor.c | 134 +- packages/atclient/src/notify.c | 16 +- packages/atclient/src/notify_params.c | 103 +- packages/atclient/src/stringutils.c | 16 +- packages/atclient/tests/test_atkey_create.c | 58 +- .../atclient/tests/test_atkey_from_string.c | 354 +-- packages/atclient/tests/test_atkey_metadata.c | 132 +- .../atclient/tests/test_atkey_to_string.c | 126 +- .../lib/include/functional_tests/helpers.h | 6 +- tests/functional_tests/lib/src/helpers.c | 18 +- .../tests/test_atclient_monitor.c | 8 +- .../tests/test_atclient_notify.c | 4 +- .../tests/test_atclient_publickey.c | 46 +- .../tests/test_atclient_selfkey.c | 38 +- .../tests/test_atclient_sharedkey.c | 16 +- 52 files changed, 3351 insertions(+), 3215 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 521c3a4a..9c166bd9 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -152,8 +152,8 @@ int main(int argc, char *argv[]) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, from_atsign, to_atsign, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_NAME, from_atsign, to_atsign, ATKEY_NAMESPACE)) != 0) { + 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) { @@ -275,9 +275,9 @@ static void *monitor_handler(void *xargs) { // We received a stats notification. Ignore it. break; } - if (atclient_atnotification_is_decryptedvalue_initialized(&(message.notification))) { + if (atclient_atnotification_is_decrypted_value_initialized(&(message.notification))) { const atclient_atnotification *notification = &(message.notification); - printf("\n%s%s%s: %s\n", HGRN, notification->from, reset, notification->decryptedvalue); + printf("\n%s%s%s: %s\n", HGRN, notification->from, reset, notification->decrypted_value); printf("%s%s%s: ", HBLU, from_atsign, reset); fflush(stdout); } diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index fe43d1ce..a7f8d8ad 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -42,7 +42,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign, ATKEY_NAME)) != 0) { + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, atsign, ATKEY_NAME)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create selfkey"); goto exit; } diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index acea2f6b..ff125fcd 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -64,7 +64,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, NULL)) != 0) { + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } @@ -85,7 +85,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Data: \"%.*s\"\n", (int)valueolen, value); - if ((ret = atclient_atkey_metadata_to_jsonstr(&atkey.metadata, &metadatajsonstr)) != 0) { + if ((ret = atclient_atkey_metadata_to_json_str(&atkey.metadata, &metadatajsonstr)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to convert metadata to json string"); goto exit; } diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index 682fd922..18e8b122 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -63,7 +63,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Successfully authenticated!\n"); } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, atsign, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, atsign, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } else { diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 9382a385..319955a1 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -77,7 +77,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n"); } - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_NAME, SENDER_ATSIGN, RECIPIENT_ATSIGN, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_NAME, SENDER_ATSIGN, RECIPIENT_ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create shared key\n"); goto exit; diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index bd90a50a..95e3b2b6 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -61,7 +61,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index c5d161d5..92a56a2e 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -61,7 +61,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_KEY, ATSIGN, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index ad4c784f..f1c78221 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -62,7 +62,7 @@ int main() { goto exit; } - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATSIGN, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATSIGN, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key"); goto exit; } diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 4c145d1f..4d316396 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -93,10 +93,10 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Received stats notification, ignoring it.\n"); break; } - if (atclient_atnotification_is_decryptedvalue_initialized(&message.notification)) { + if (atclient_atnotification_is_decrypted_value_initialized(&message.notification)) { // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Message id: %s\n", message->notification.id); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decryptedvalue: \"%s\"\n", - message.notification.decryptedvalue); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decrypted_value: \"%s\"\n", + message.notification.decrypted_value); } break; } diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 9ec18c62..ca6b05f4 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -101,7 +101,7 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, atsign_input, other_atsign_input, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, atsign_input, other_atsign_input, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index 00b11eb4..dcdf6c32 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -97,10 +97,10 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Received stats notification, ignoring it.\n"); break; } - if (atclient_atnotification_is_decryptedvalue_initialized(&message->notification)) { + if (atclient_atnotification_is_decrypted_value_initialized(&message->notification)) { // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Message id: %s\n", message->notification.id); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decryptedvalue: \"%s\"\n", - message->notification.decryptedvalue); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "decrypted_value: \"%s\"\n", + message->notification.decrypted_value); } tries = 1; break; diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 515299aa..0277be53 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -151,7 +151,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unknown atKey type\n"); goto get_end; } - case ATCLIENT_ATKEY_TYPE_PUBLICKEY: { + case ATCLIENT_ATKEY_TYPE_PUBLIC_KEY: { if ((ret = atclient_get_publickey(&atclient, &atkey, recv, recvsize, &recvlen, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_publickey: %d | failed to get public key\n", ret); @@ -160,7 +160,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Value: \"%s\"\n", recv); break; } - case ATCLIENT_ATKEY_TYPE_SELFKEY: { + case ATCLIENT_ATKEY_TYPE_SELF_KEY: { if ((ret = atclient_get_selfkey(&atclient, &atkey, recv, recvsize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey: %d | failed to get self key\n", ret); goto get_end; @@ -168,7 +168,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Value: \"%s\"\n", recv); break; } - case ATCLIENT_ATKEY_TYPE_SHAREDKEY: { + case ATCLIENT_ATKEY_TYPE_SHARED_KEY: { if ((ret = atclient_get_sharedkey(&atclient, &atkey, recv, recvsize, &recvlen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey: %d | failed to get shared key\n", ret); diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 960fbfe2..aafa2493 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -38,7 +38,7 @@ typedef struct atclient { // started and is expected to be non-functional. // bit 2 == atkeys | true == atkeys are populated with the necessary // keys, false == atkeys are not populated. - uint8_t _initializedfields[1]; + uint8_t _initialized_fields[1]; } atclient; /** @@ -135,18 +135,18 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, sharedby, sharedwith, etc,.) depending on what kind of atkey you want + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want * to be associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to put the value into (must satisfy the two conditions stated above) * @param value the value to put into atServer - * @param valuelen the length of the value (most of the time you will use strlen() on a null-terminated string for + * @param value_len the length of the value (most of the time you will use strlen() on a null-terminated string for * this value) - * @param commitid (optional) the output commitid of the put operation that the atServer returns + * @param commit_id (optional) the output commit_id of the put operation that the atServer returns * @return int 0 on success */ -int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid); +int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t value_len, int *commit_id); /** * @brief Get a string value from your atServer. @@ -156,18 +156,18 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, sharedby, sharedwith, etc,.) depending on what kind of atkey you want to be + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be * associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) * @param value the buffer to hold value gotten from atServer - * @param valuesize the buffer length allocated for the value - * @param valuelen the output length of the value gotten from atServer + * @param value_size the buffer length allocated for the value + * @param value_len the output length of the value gotten from atServer * @return int 0 on success */ -int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen); +int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len); /** * @brief Get a publickey from your atServer or another atServer @@ -177,20 +177,20 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, sharedby, sharedwith, etc,.) depending on what kind of atkey you want to be + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be * associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) * @param value the buffer to hold value gotten from atServer - * @param valuesize the buffer length allocated for the value - * @param valuelen the output length of the value gotten from atServer - * @param bypasscache true if you want to bypass the cached publickey, that might be on your atServer, and get the most + * @param value_size the buffer length allocated for the value + * @param value_len the output length of the value gotten from atServer + * @param bypass_cache true if you want to bypass the cached publickey, that might be on your atServer, and get the most * up-to-date value straight from the atServer that the publickey sits on, false otherwise * @return int 0 on success */ -int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, bool bypasscache); +int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, bool bypass_cache); /** * @brief Get a sharedkey either shared by you or shared with you and receive the decrypted plaintext value. @@ -200,21 +200,21 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, sharedby, sharedwith, etc,.) depending on what kind of atkey you want to be + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be * associated with your value. * * @param atclient The atclient context (must satisfy the two conditions stated above) * @param atkey The populated atkey to get the value from (must satisfy the two conditions stated above) * @param value The buffer to hold value gotten from atServer - * @param valuesize The buffer length allocated for the value - * @param valuelen The output length of the value gotten from atServer + * @param value_size The buffer length allocated for the value + * @param value_len The output length of the value gotten from atServer * @param shared_encryption_key The correct shared encryption key (get_encryption_key_shared_by_me or * get_encryption_key_shared_by_other, depending on the case). If NULL is provided, it will get it for you, if it * exists. * @return int 0 on success */ -int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, const unsigned char *shared_encryption_key); +int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, const unsigned char *shared_encryption_key); /** * @brief Delete an atkey from your atserver @@ -224,7 +224,7 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, sharedby, sharedwith, etc,.) depending on what kind of atkey you want to be + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be * associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) @@ -239,14 +239,14 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit * * @param atclient the initialized and pkam_authenticated atclient context * @param regex (input): the regex pattern for what keys to filter - * @param showhidden (input): true to show "hidden" keys (which are keys that begin with an _), false otherwise - * @param recvbuffersize (input): the size of the buffer to receive the response from the atServer + * @param show_hidden (input): true to show "hidden" keys (which are keys that begin with an _), false otherwise + * @param recv_buffer_size (input): the size of the buffer to receive the response from the atServer * @param atkey (output): a double pointer which will be a pointer to the first atkey in the array. caller of this * function is responsible for each individual atkey in the array using `atclient_atkey_free` * @param output_array_len (output): the overall size of the array that was allocated * @return int 0 on success */ -int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhidden, const size_t recvbuffersize, +int atclient_get_atkeys(atclient *atclient, const char *regex, const bool show_hidden, const size_t recv_buffer_size, atclient_atkey **atkey, size_t *output_array_len); /** diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index adb2cf57..5e13b778 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -7,30 +7,30 @@ #define VALUE_INITIALIZED 0b00000001 -#define ATKEY_KEY_INDEX 0 -#define ATKEY_NAMESPACESTR_INDEX 0 -#define ATKEY_SHAREDBY_INDEX 0 -#define ATKEY_SHAREDWITH_INDEX 0 +#define ATCLIENT_ATKEY_KEY_INDEX 0 +#define ATCLIENT_ATKEY_NAMESPACE_STR_INDEX 0 +#define ATCLIENT_ATKEY_SHARED_BY_INDEX 0 +#define ATCLIENT_ATKEY_SHARED_WITH_INDEX 0 -#define ATKEY_KEY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATKEY_NAMESPACESTR_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATKEY_SHAREDBY_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATKEY_SHAREDWITH_INITIALIZED (VALUE_INITIALIZED << 3) +#define ATCLIENT_ATKEY_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_ATKEY_NAMESPACE_STR_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_ATKEY_SHARED_BY_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_ATKEY_SHARED_WITH_INITIALIZED (VALUE_INITIALIZED << 3) typedef enum atclient_atkey_type { ATCLIENT_ATKEY_TYPE_UNKNOWN = 0, - ATCLIENT_ATKEY_TYPE_PUBLICKEY, - ATCLIENT_ATKEY_TYPE_SELFKEY, - ATCLIENT_ATKEY_TYPE_SHAREDKEY, + ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, + ATCLIENT_ATKEY_TYPE_SELF_KEY, + ATCLIENT_ATKEY_TYPE_SHARED_KEY, } atclient_atkey_type; typedef struct atclient_atkey { char *key; - char *namespacestr; - char *sharedby; - char *sharedwith; + char *namespace_str; + char *shared_by; + char *shared_with; - uint8_t _initializedfields[1]; // internal field to track which fields are allocated + uint8_t _initialized_fields[1]; // internal field to track which fields are allocated atclient_atkey_metadata metadata; @@ -102,34 +102,34 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr); bool atclient_atkey_is_key_initialized(const atclient_atkey *atkey); /** - * @brief Returns true if atkey->namespacestr is initialized and allocated, and the value is safe for reading + * @brief Returns true if atkey->namespace_str is initialized and allocated, and the value is safe for reading * * @param atkey the atkey struct to read - * @return true, if the namespacestr is initialized and allocated and is safe for reading - * @return false, if the namespacestr is not initialized and not allocated and is not safe for reading, high chance that + * @return true, if the namespace_str is initialized and allocated and is safe for reading + * @return false, if the namespace_str is not initialized and not allocated and is not safe for reading, high chance that * it holds garbage */ bool atclient_atkey_is_namespacestr_initialized(const atclient_atkey *atkey); /** - * @brief Returns true if atkey->sharedby is initialized and allocated, and the value is safe for reading + * @brief Returns true if atkey->shared_by is initialized and allocated, and the value is safe for reading * * @param atkey the atkey struct to read - * @return true, if the sharedby is initialized and allocated and is safe for reading - * @return false, if the sharedby is not initialized and not allocated and is not safe for reading, high chance that it + * @return true, if the shared_by is initialized and allocated and is safe for reading + * @return false, if the shared_by is not initialized and not allocated and is not safe for reading, high chance that it * holds garbage */ -bool atclient_atkey_is_sharedby_initialized(const atclient_atkey *atkey); +bool atclient_atkey_is_shared_by_initialized(const atclient_atkey *atkey); /** - * @brief Returns true if atkey->sharedwith is initialized and allocated, and the value is safe for reading + * @brief Returns true if atkey->shared_with is initialized and allocated, and the value is safe for reading * * @param atkey the atkey struct to read - * @return true, if the sharedwith is initialized and allocated and is safe for reading - * @return false, if the sharedwith is not initialized and not allocated and is not safe for reading, high chance that - * it holds garbage + * @return true, if the shared_with is initialized and allocated and is safe for reading + * @return false, if the shared_with is not initialized and not allocated and is not safe for reading, high chance that + * it holds garbage 🤠 */ -bool atclient_atkey_is_sharedwith_initialized(const atclient_atkey *atkey); +bool atclient_atkey_is_shared_with_initialized(const atclient_atkey *atkey); /** * @brief Dynamically allocates memory and duplicates key to be stored in the atkey struct. If this is allocated, then @@ -143,37 +143,37 @@ bool atclient_atkey_is_sharedwith_initialized(const atclient_atkey *atkey); int atclient_atkey_set_key(atclient_atkey *atkey, const char *key); /** - * @brief Dynamically allocates memory and duplicates namespacestr to be stored in the atkey struct. If this is + * @brief Dynamically allocates memory and duplicates namespace_str to be stored in the atkey struct. If this is * allocated, then it is freed with atclient_atkey_free by the end of life of your struct * * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via * atclient_atkey_init - * @param namespacestr (mandatory, NON-NULL) The namespace of your application, e.g. "banking_app" (NULLABLE) + * @param namespace_str (mandatory, NON-NULL) The namespace of your application, e.g. "banking_app" (NULLABLE) * @return int 0 on success, otherwise error (most likely malloc error) */ -int atclient_atkey_set_namespacestr(atclient_atkey *atkey, const char *namespacestr); +int atclient_atkey_set_namespace_str(atclient_atkey *atkey, const char *namespace_str); /** - * @brief Dynamically allocates memory and duplicates sharedby to be stored in the atkey struct. If this is allocated, + * @brief Dynamically allocates memory and duplicates shared_by to be stored in the atkey struct. If this is allocated, * then it is freed with atclient_atkey_free by the end of life of your struct * * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via * atclient_atkey_init - * @param sharedby (mandatory, NON-NULL) The sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" + * @param shared_by (mandatory, NON-NULL) The shared_by (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" * @return int 0 on success, otherwise error (most likely malloc error) */ -int atclient_atkey_set_sharedby(atclient_atkey *atkey, const char *sharedby); +int atclient_atkey_set_shared_by(atclient_atkey *atkey, const char *shared_by); /** - * @brief Dynamically allocates memory and duplicates sharedwith to be stored in the atkey struct. If this is allocated, + * @brief Dynamically allocates memory and duplicates shared_with to be stored in the atkey struct. If this is allocated, * then it is freed with atclient_atkey_free by the end of life of your struct * * @param atkey (mandatory, NON-NULL) The atkey struct to populate, assumed that this was already initialized via * atclient_atkey_init - * @param sharedwith (mandatory, NON-NULL) The sharedwith atsign, atsign you are going to share it with, e.g. "@bob" + * @param shared_with (mandatory, NON-NULL) The shared_with atsign, atsign you are going to share it with, e.g. "@bob" * @return int 0 on success, otherwise error (most likely malloc error) */ -int atclient_atkey_set_sharedwith(atclient_atkey *atkey, const char *sharedwith); +int atclient_atkey_set_shared_with(atclient_atkey *atkey, const char *shared_with); /** * @brief Frees the memory allocated for the key in the atkey struct. This is already called by atclient_atkey_free and @@ -184,30 +184,30 @@ int atclient_atkey_set_sharedwith(atclient_atkey *atkey, const char *sharedwith) void atclient_atkey_unset_key(atclient_atkey *atkey); /** - * @brief Frees the memory allocated for the namespacestr in the atkey struct. This is already called by - * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the namespacestr before the + * @brief Frees the memory allocated for the namespace_str in the atkey struct. This is already called by + * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the namespace_str before the * end of life of your struct * - * @param atkey the atkey struct that holds the allocated atkey->namespacestr memory + * @param atkey the atkey struct that holds the allocated atkey->namespace_str memory */ -void atclient_atkey_unset_namespacestr(atclient_atkey *atkey); +void atclient_atkey_unset_namespace_str(atclient_atkey *atkey); /** - * @brief Free the memory allocated for the sharedby in the atkey struct. This is already called by atclient_atkey_free - * and should only be used for advanced pruposes, if you want to free the sharedby before the end of life of your struct + * @brief Free the memory allocated for the shared_by in the atkey struct. This is already called by atclient_atkey_free + * and should only be used for advanced pruposes, if you want to free the shared_by before the end of life of your struct * - * @param atkey the atkey struct that holds the allocated atkey->sharedby memory + * @param atkey the atkey struct that holds the allocated atkey->shared_by memory */ -void atclient_atkey_unset_sharedby(atclient_atkey *atkey); +void atclient_atkey_unset_shared_by(atclient_atkey *atkey); /** - * @brief Frees the memory allocated for the sharedwith in the atkey struct. This is already called by - * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the sharedwith before the end + * @brief Frees the memory allocated for the shared_with in the atkey struct. This is already called by + * atclient_atkey_free and should only be used for advanced pruposes, if you want to free the shared_with before the end * of life of your struct * - * @param atkey the atkey struct that holds the allocated atkey->sharedwith memory + * @param atkey the atkey struct that holds the allocated atkey->shared_with memory */ -void atclient_atkey_unset_sharedwith(atclient_atkey *atkey); +void atclient_atkey_unset_shared_with(atclient_atkey *atkey); /** * @brief Evaluates the type of the atkey struct @@ -224,39 +224,39 @@ atclient_atkey_type atclient_atkey_get_type(const atclient_atkey *atkey); * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name the name of the atkey, e.g.: "name" - * @param sharedby the sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" - * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) + * @param shared_by the shared_by (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" + * @param namespace_str the namespace of your application, e.g. "banking_app" (NULLABLE) * @return int 0 on success */ -int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *name, const char *sharedby, const char *namespacestr); +int atclient_atkey_create_public_key(atclient_atkey *atkey, const char *name, const char *shared_by, const char *namespace_str); /** * @brief Populate an atkey struct representing a SelfKey AtKey with null terminated strings. An example of a SelfKey - * AtKey would be 'name.namespace@alice'. SelfKeys can only be accessible by the sharedby (creator) atsign. Be sure to + * AtKey would be 'name.namespace@alice'. SelfKeys can only be accessible by the shared_by (creator) atsign. Be sure to * call the atclient_atkey_init function before calling this function. * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name the name of the atkey, e.g.: "name" - * @param sharedby the sharedby (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" - * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) + * @param shared_by the shared_by (creator/pkam authenticated atsign) of the atkey, e.g.: "@alice" + * @param namespace_str the namespace of your application, e.g. "banking_app" (NULLABLE) * @return int 0 on success */ -int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *name, const char *sharedby, const char *namespacestr); +int atclient_atkey_create_self_key(atclient_atkey *atkey, const char *name, const char *shared_by, const char *namespace_str); /** * @brief Populate an atkey struct representing a SharedKey AtKey given null terminated strings. An example of a - * SharedKey AtKey would be '@sharedwith:name.namesapce@sharedby'. SharedKeys can only be accessible by the sharedwith - * and sharedby atsigns, as they are encrypted with a shared AES key which is encrypted with the each of their RSA keys. + * SharedKey AtKey would be '@shared_with:name.namesapce@shared_by'. SharedKeys can only be accessible by the shared_with + * and shared_by atsigns, as they are encrypted with a shared AES key which is encrypted with the each of their RSA keys. * Be sure to call the atclient_atkey_init function before calling this function. * * @param atkey the atkey struct to populate, assumed that this was already initialized via atclient_atkey_init * @param name name of your key, e.g. "name" - * @param sharedby the shared by atsign, e.g. "@alice" - * @param sharedwith the sharedwith atsign, atsign you are going to share it with, e.g. "@bob" - * @param namespacestr the namespace of your application, e.g. "banking_app" (NULLABLE) + * @param shared_by the shared by atsign, e.g. "@alice" + * @param shared_with the shared_with atsign, atsign you are going to share it with, e.g. "@bob" + * @param namespace_str the namespace of your application, e.g. "banking_app" (NULLABLE) * @return int 0 on success */ -int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *name, const char *sharedby, - const char *sharedwith, const char *namespacestr); +int atclient_atkey_create_shared_key(atclient_atkey *atkey, const char *name, const char *shared_by, + const char *shared_with, const char *namespace_str); #endif diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 3592617e..2707a75c 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -8,17 +8,17 @@ #define VALUE_INITIALIZED 0b00000001 -#define ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX 0 -#define ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX 0 -#define ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX 0 -#define ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX 0 -#define ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX 0 - -#define ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED (VALUE_INITIALIZED << 2) -#define ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED (VALUE_INITIALIZED << 3) -#define ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED (VALUE_INITIALIZED << 4) +#define ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INDEX 0 +#define ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX 0 +#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_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) /** * @brief represents the atkeys file @@ -32,21 +32,21 @@ * 4. (for rsakeys), the rsakey struct used in rsa operations. */ typedef struct atclient_atkeys { - char *pkampublickeybase64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_publickey pkampublickey; // contains n, e + char *pkam_publickey_base64; // base64 encoded, RSA-2048 key, decrypted + atchops_rsakey_publickey pkam_public_key; // contains n, e - char *pkamprivatekeybase64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_privatekey pkamprivatekey; // conatins n, e, d, p, q + char *pkam_private_key_base64; // base64 encoded, RSA-2048 key, decrypted + atchops_rsakey_privatekey pkam_private_key; // conatins n, e, d, p, q - char *encryptpublickeybase64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_publickey encryptpublickey; // contains n, e + char *encrypt_public_key_base64; // base64 encoded, RSA-2048 key, decrypted + atchops_rsakey_publickey encrypt_public_key; // contains n, e - char *encryptprivatekeybase64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_privatekey encryptprivatekey; // conatins n, e, d, p, q + char *encrypt_private_key_base64; // base64 encoded, RSA-2048 key, decrypted + atchops_rsakey_privatekey encrypt_private_key; // conatins n, e, d, p, q - char *selfencryptionkeybase64; // base64 encoded, AES-256 key, decrypted + char *self_encryption_key_base64; // base64 encoded, AES-256 key, decrypted - uint8_t _initializedfields[1]; // used to track which fields have been initialized + uint8_t _initialized_fields[1]; // used to track which fields have been initialized } atclient_atkeys; /** @@ -63,64 +63,64 @@ void atclient_atkeys_init(atclient_atkeys *atkeys); */ void atclient_atkeys_free(atclient_atkeys *atkeys); -int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, +int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, const size_t pkampublickeybase64len); -int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, +int atclient_atkeys_set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, const size_t pkamprivatekeybase64len); -int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, +int atclient_atkeys_set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, const size_t encryptpublickeybase64len); -int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, +int atclient_atkeys_set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, const size_t encryptprivatekeybase64len); -int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, +int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, const size_t selfencryptionkeybase64len); -int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char *pkampublickeybase64, +int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_publickey_base64, const size_t pkampublickeybase64len); -int atclient_atkeys_populate_pkamprivatekey(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, +int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const char *pkam_private_key_base64, const size_t pkamprivatekeybase64len); -int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const char *encryptpublickeybase64, +int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, const size_t encryptpublickeybase64len); -int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, +int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, const size_t encryptprivatekeybase64len); -bool atclient_atkeys_is_pkampublickeybase64_initialized(atclient_atkeys *atkeys); -bool atclient_atkeys_is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys); -bool atclient_atkeys_is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys); -bool atclient_atkeys_is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys); -bool atclient_atkeys_is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys); +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); /** * @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 aespkampublickeystr the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 format - * @param aespkampublickeylen the length of the aespkampublickeystr buffer - * @param aespkamprivatekeystr the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 format - * @param aespkamprivatekeylen the length of the aespkamprivatekeystr buffer - * @param aesencryptpublickeystr the encrypted RSA public key (encrypted with AES-256 selfencryptionkey) in base64 + * @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_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 - * @param aesencryptpublickeylen the length of the aesencryptpublickeystr buffer - * @param aesencryptprivatekeystr the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 + * @param aes_encrypt_public_key_len the length of the aes_encrypt_public_key_str buffer + * @param aes_encrypt_private_key_str the encrypted RSA private key (encrypted with AES-256 selfencryptionkey) in base64 * format - * @param aesencryptprivatekeylen the length of the aesencryptprivatekeystr buffer - * @param selfencryptionkeystr the (decrypted) AES-256 selfencryptionkey in base64 format - * @param selfencryptionkeylen the length of the selfencryptionkeystr buffer + * @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 * @return int 0 on success, non-zero on failure */ -int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, - const size_t aespkampublickeylen, const char *aespkamprivatekeystr, - const size_t aespkamprivatekeylen, const char *aesencryptpublickeystr, - const size_t aesencryptpublickeylen, const char *aesencryptprivatekeystr, - const size_t aesencryptprivatekeylen, const char *selfencryptionkeystr, - const size_t selfencryptionkeylen); +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_private_key_len, const char *self_encryption_key_str, + const size_t self_encryption_key_len); /** * @brief populates the struct by decrypting the encrypted RSA keys found in a populated atclient_atkeysfile struct @@ -138,7 +138,7 @@ int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atcl * * @param atkeys the struct to populate * @param path the path to the *.atKeys file - * @return int + * @return int 0 on success, non-zero on failure */ int atclient_atkeys_populate_from_path(atclient_atkeys *atkeys, const char *path); diff --git a/packages/atclient/include/atclient/atkeysfile.h b/packages/atclient/include/atclient/atkeysfile.h index 74dda9d3..e25bb82d 100644 --- a/packages/atclient/include/atclient/atkeysfile.h +++ b/packages/atclient/include/atclient/atkeysfile.h @@ -6,25 +6,25 @@ #define VALUE_INITIALIZED 0b00000001 -#define ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX 0 -#define ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX 0 +#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_AESPKAMPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 0) -#define ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 1) -#define ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 2) -#define ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 3) -#define ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED (VALUE_INITIALIZED << 4) +#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 *aespkampublickeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *aespkamprivatekeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA keyF - char *aesencryptpublickeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *aesencryptprivatekeystr; // encrypted with self encryption key. AES decryption with self encryption key will reveal base64-encoded RSA key - char *selfencryptionkeystr; // base64-encoded non-encrypted self encryption key. base64 decoding will reveal 32-byte AES key - uint8_t _initializedfields[1]; + 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); diff --git a/packages/atclient/include/atclient/atnotification.h b/packages/atclient/include/atclient/atnotification.h index 6a3e75cd..62b54e77 100644 --- a/packages/atclient/include/atclient/atnotification.h +++ b/packages/atclient/include/atclient/atnotification.h @@ -53,21 +53,21 @@ typedef struct atclient_atnotification { char *to; // holds the to atSign (who the notification is for) char *key; // holds the key of the notification (e.g. "@bob:location.app@alice") char *value; // holds the value that is read from the notification, this would typically be base64 encoded and - // encrypted, see decryptedvalue for the decrypted value + // encrypted, see decrypted_value for the decrypted value char *operation; // holds the operation of the notification (e.g. "update", "delete") - size_t epochMillis; // holds the epoch time in milliseconds when the notification was sent - char *messageType; // holds the message type of the notification (e.g. "data", "error") + size_t epoch_millis; // holds the epoch time in milliseconds when the notification was sent + char *message_type; // holds the message type of the notification (e.g. "data", "error") // _initalizedfields[1] - bool isEncrypted : 1; - char *encKeyName; // in metaData - char *encAlgo; // in metaData - char *ivNonce; // in metaData - char *skeEncKeyName; // in metaData - char *skeEncAlgo; // in metaData - char *decryptedvalue; // if isEncrypted, this will be the decrypted value - - uint8_t _initializedfields[2]; + bool is_encrypted : 1; + char *enc_key_name; // in metaData + char *enc_algo; // in metaData + char *iv_nonce; // in metaData + char *ske_enc_key_name; // in metaData + char *ske_enc_algo; // in metaData + char *decrypted_value; // if is_encrypted, this will be the decrypted value + + uint8_t _initialized_fields[2]; } atclient_atnotification; /** @@ -98,15 +98,15 @@ bool atclient_atnotification_is_to_initialized(const atclient_atnotification *no bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification); bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification); bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_epochmillis_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_messagetype_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_isencrypted_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_enckeyname_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_encalgo_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_ivnonce_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_skeenckeyname_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_skeencalgo_initialized(const atclient_atnotification *notification); -bool atclient_atnotification_is_decryptedvalue_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_epoch_millis_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_message_type_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_is_encrypted_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_enc_key_name_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_enc_algo_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_iv_nonce_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_ske_enc_key_name_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_ske_enc_algo_initialized(const atclient_atnotification *notification); +bool atclient_atnotification_is_decrypted_value_initialized(const atclient_atnotification *notification); // Set a field as initialized or not void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized); @@ -115,16 +115,16 @@ void atclient_atnotification_to_set_initialized(atclient_atnotification *notific void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized); void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, const bool initialized); void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_epochmillis_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_messagetype_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_isencrypted_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_enckeyname_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_encalgo_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_ivnonce_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_skeenckeyname_set_initialized(atclient_atnotification *notification, +void atclient_atnotification_epoch_millis_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_message_type_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_is_encrypted_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_enc_key_name_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_enc_algo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_iv_nonce_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_ske_enc_key_name_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_skeencalgo_set_initialized(atclient_atnotification *notification, const bool initialized); -void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, +void atclient_atnotification_ske_enc_algo_set_initialized(atclient_atnotification *notification, const bool initialized); +void atclient_atnotification_decrypted_value_set_initialized(atclient_atnotification *notification, const bool initialized); // Free a field, some fields are dynamically allocated when set @@ -134,40 +134,32 @@ void atclient_atnotification_unset_to(atclient_atnotification *notification); void atclient_atnotification_unset_key(atclient_atnotification *notification); void atclient_atnotification_unset_value(atclient_atnotification *notification); void atclient_atnotification_unset_operation(atclient_atnotification *notification); -void atclient_atnotification_unset_epochmillis(atclient_atnotification *notification); -void atclient_atnotification_unset_messagetype(atclient_atnotification *notification); -void atclient_atnotification_unset_isencrypted(atclient_atnotification *notification); -void atclient_atnotification_unset_enckeyname(atclient_atnotification *notification); -void atclient_atnotification_unset_encalgo(atclient_atnotification *notification); -void atclient_atnotification_unset_ivnonce(atclient_atnotification *notification); -void atclient_atnotification_unset_skeenckeyname(atclient_atnotification *notification); -void atclient_atnotification_unset_skeencalgo(atclient_atnotification *notification); -void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification); +void atclient_atnotification_unset_epoch_millis(atclient_atnotification *notification); +void atclient_atnotification_unset_message_type(atclient_atnotification *notification); +void atclient_atnotification_unset_is_encrypted(atclient_atnotification *notification); +void atclient_atnotification_unset_enc_key_name(atclient_atnotification *notification); +void atclient_atnotification_unset_enc_algo(atclient_atnotification *notification); +void atclient_atnotification_unset_iv_nonce(atclient_atnotification *notification); +void atclient_atnotification_unset_ske_enc_key_name(atclient_atnotification *notification); +void atclient_atnotification_unset_ske_enc_algo(atclient_atnotification *notification); +void atclient_atnotification_unset_decrypted_value(atclient_atnotification *notification); // Setters for the fields, these functions check if the field is initialized before setting the value (and overwrites if // it is) -int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen); -int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen); -int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen); -int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen); -int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen); -int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen); -int atclient_atnotification_set_epochmillis(atclient_atnotification *notification, const size_t epochMillis); -int atclient_atnotification_set_messagetype(atclient_atnotification *notification, const char *messageType, - const size_t messageTypelen); -int atclient_atnotification_set_isencrypted(atclient_atnotification *notification, const bool isEncrypted); -int atclient_atnotification_set_enckeyname(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen); -int atclient_atnotification_set_encalgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen); -int atclient_atnotification_set_ivnonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen); -int atclient_atnotification_set_skeenckeyname(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen); -int atclient_atnotification_set_skeencalgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen); -int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, const char *decryptedvalue, - const size_t decryptedvaluelen); +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id); +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from); +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to); +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key); +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value); +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation); +int atclient_atnotification_set_epoch_millis(atclient_atnotification *notification, const size_t epoch_millis); +int atclient_atnotification_set_message_type(atclient_atnotification *notification, const char *message_type); +int atclient_atnotification_set_is_encrypted(atclient_atnotification *notification, const bool is_encrypted); +int atclient_atnotification_set_enc_key_name(atclient_atnotification *notification, const char *enc_key_name); +int atclient_atnotification_set_enc_algo(atclient_atnotification *notification, const char *enc_algo); +int atclient_atnotification_set_iv_nonce(atclient_atnotification *notification, const char *iv_nonce); +int atclient_atnotification_set_ske_enc_key_name(atclient_atnotification *notification, const char *ske_enc_key_name); +int atclient_atnotification_set_ske_enc_algo(atclient_atnotification *notification, const char *ske_enc_algo); +int atclient_atnotification_set_decrypted_value(atclient_atnotification *notification, const char *decrypted_value); #endif // ATCLIENT_ATNOTIFICATION_H \ No newline at end of file diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index b05b8256..51d44f9e 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -16,8 +16,8 @@ typedef enum atclient_connection_type { ATCLIENT_CONNECTION_TYPE_ATSERVER // uses 'noop:0\r\n' to check if it is connected } atclient_connection_type; -typedef int(atclient_connection_send_hook)(const unsigned char *src, const size_t srclen, unsigned char *recv, - const size_t recvsize, size_t *recvlen); +typedef int(atclient_connection_send_hook)(const unsigned char *src, const size_t src_len, unsigned char *recv, + const size_t recv_size, size_t *recv_len); typedef enum atclient_connection_hook_type { ATCLIENT_CONNECTION_HOOK_TYPE_NONE = 0, @@ -86,16 +86,16 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons * * @param ctx the connection which was initialized (via the init function) and connected (via the connect function) * @param src the data to send - * @param srclen the length of the data to send + * @param src_len the length of the data to send * @param recv the buffer to receive data - * @param recvsize the length of the buffer to receive data - * @param recvlen the length of the data received (output) + * @param recv_size the length of the buffer to receive data + * @param recv_len the length of the data received (output) * @return int 0 on success, otherwise error * * @note if recv is NULL, then this function will skip reading the response */ -int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, const size_t srclen, - unsigned char *recv, const size_t recvsize, size_t *recvlen); +int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, const size_t src_len, + unsigned char *recv, const size_t recv_size, size_t *recv_len); /** * @brief disconnect a connection diff --git a/packages/atclient/include/atclient/encryption_key_helpers.h b/packages/atclient/include/atclient/encryption_key_helpers.h index a47c73c7..ca2b1baf 100644 --- a/packages/atclient/include/atclient/encryption_key_helpers.h +++ b/packages/atclient/include/atclient/encryption_key_helpers.h @@ -55,14 +55,14 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char * (shared_key.other@me and @other:shared_key@me) * * @param atclient non-null atclient context (must be initialized and pkam_authenticated) - * @param sharedwith non-null atSign (with @ symbol) and null-terminated. Example "@bob" + * @param shared_with non-null atSign (with @ symbol) and null-terminated. Example "@bob" * @param shared_encryption_key_shared_by_me_with_other holds the output shared encryption key that was created by this * function, to be shared with the recipient. Expected to be non-null and has 32 bytes of memory allocated to this * address to hold key. This key is not base64 encoded and not encrypted and is in raw bytes. * @return int 0 on success, error otherwise */ int atclient_create_shared_encryption_key_pair_for_me_and_other( - atclient *atclient, const char *sharedwith, + atclient *atclient, const char *shared_with, unsigned char *shared_encryption_key_shared_by_me_with_other); #endif // ATCLIENT_ENCRYPTION_KEY_HELPERS_H diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 87bc0a7e..8bf3edd3 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -84,12 +84,12 @@ typedef struct atclient_atkey_metadata { // Represents the atSign who created this atkey. // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *createdby; + char *created_by; // Represents the atSign who last updated this atkey. // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *updatedby; // repreesnts the atSign who last updated this atkey. read from protocol string only + char *updated_by; // repreesnts the atSign who last updated this atkey. read from protocol string only // TODO: info about this metadata // This field is read from protocol string only and is not meant to be written to by the developer. @@ -105,38 +105,38 @@ typedef struct atclient_atkey_metadata { // This field is derived from the [ttl] value. If the ttl value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *expiresat; + char *expires_at; // Date and time representing when the atkey will be available at (in UTC date/time format). // This field is derived from the [ttb] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *availableat; + char *available_at; // Date and time repreesnts when the atkey will refresh at (in UTC date/time format). // This field is derived from the [ttr] value. If the ttr value does not exist, then this field should not exist // either. This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *refreshat; + char *refresh_at; // Date and time representing when the atkey was created at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // this field is read by the protocol and populated by the SDK for the developer to read from only. - char *createdat; + char *created_at; // Date and time representing when the atkey was last updated at (in UTC date/time format). // This field is read from protocol string only and is not meant to be written to by the developer. // This field is read by the protocol and populated by the SDK for the developer to read from only. - char *updatedat; // date and time representing when the key was last updated, read only + char *updated_at; // date and time representing when the key was last updated, read only - // ispublic=true means this key is accessible by all atSigns and contains non-encrypted data. - // ispublic=false means this key is only accessible by either sharedWith or sharedBy + // is_public=true means this key is accessible by all atSigns and contains non-encrypted data. + // is_public=false means this key is only accessible by either sharedWith or sharedBy // This field is not written to the protocol string by the SDK. It is a strictly client-side metadata. - bool ispublic : 1; + bool is_public : 1; - // iscached=true means the key contains 'cached:', written and used by client SDK only, not written to protocol string - // iscached=false means the key does not contain 'cached:' - bool iscached : 1; + // is_cached=true means the key contains 'cached:', written and used by client SDK only, not written to protocol string + // is_cached=false means the key does not contain 'cached:' + bool is_cached : 1; // This field is not written to the protocol string by the SDK. It is a strictly client-side metadata. // Time to live in milliseconds. @@ -170,34 +170,34 @@ typedef struct atclient_atkey_metadata { // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) bool ccd : 1; - // isbinary=true means atkey stores binary data - // isbinary=false means atkey stores non-binary data (like plain text) + // is_binary=true means atkey stores binary data + // is_binary=false means atkey stores non-binary data (like plain text) // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - bool isbinary : 1; + bool is_binary : 1; - // isencrypted=true means the value is encrypted, most commonly used for sharedkeys - // isencrypted=false means the value is not encrypted + // is_encrypted=true means the value is encrypted, most commonly used for sharedkeys + // is_encrypted=false means the value is not encrypted // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - bool isencrypted : 1; + bool is_encrypted : 1; // Public data is signed using the key owner's encryptPrivateKey and the result is stored here. This is to ensure that // the data came from the owner of the public/private keypair // This field is read from protocol string and can also be set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *datasignature; + char *data_signature; // Represents the status of the shared key. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *sharedkeystatus; + char *shared_key_status; // Stores the sharedkey that the data is encrypted with. This is only set if sharedWith is set. The contents will be // encrypted using the public key of the sharedWith atSign. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *sharedkeyenc; + char *shared_key_enc; // Regarding the following 2 fields... // The pubkey pair stores the hash of the encryption public key used to encrypt the [sharedKeyEnc]. The hash is used @@ -208,12 +208,12 @@ typedef struct atclient_atkey_metadata { // The hash of the public key used to encrypt the [sharedKeyEnc]. // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *pubkeyhash; + char *pub_key_hash; // The algorithm used to hash the public key used to encrypt the [sharedKeyEnc] (e.g. "sha256" or "sha512") // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *pubkeyalgo; + char *pub_key_algo; // The type of encoding the value is (e.g. "base64") // This field is read from protocol string and set by the developer. @@ -221,36 +221,36 @@ typedef struct atclient_atkey_metadata { char *encoding; // The name of the key used to encrypt the value. If not provided, use sharedKeyEnc in the metadata. If sharedKeyEnc - // is not provided, use the default shared key. If enckeyname is provided, just the key name must be provided example + // is not provided, use the default shared key. If enc_key_name is provided, just the key name must be provided example // without the sharedWith suffix and sharedBy prefix, nor visibility prefix. Example '@bob:shared_key.wavi@alice', // must be only be 'shared_key.wavi' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *enckeyname; + char *enc_key_name; // The name of the algorithm used to encrypt the value. For data, the default algorithm is 'AES/SIC/PKCS7Padding', for // cryptographic keys, the default algorithm is 'RSA' // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *encalgo; + char *enc_algo; // The initialization vector or nonce used when the data was encrypted with the shared symmetric encryption key // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *ivnonce; + char *iv_nonce; // TODO: documentation info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *skeenckeyname; + char *ske_enc_key_name; // TODO: documentation info about this metadata // This field is read from protocol string and set by the developer. // This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr) - char *skeencalgo; + char *ske_enc_algo; // Internal field that holds the metadata fields that have not been initialized (0) or have been initialized (1) - uint8_t _initializedfields[4]; + uint8_t _initialized_fields[4]; } atclient_atkey_metadata; /** @@ -277,13 +277,13 @@ int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_a * @brief Populates the metadata struct from a string. This function is good for debugging. * * @param metadata the metadata struct to populate - * @param metadatastr the metadata string (usually taken from meta commands such as llookup:meta: or + * @param metadata_str the metadata string (usually taken from meta commands such as llookup:meta: or * plookup:meta:atsign = malloc(sizeof(char) * atsignsize)) == NULL) { + const size_t atsign_len = strlen(atsign); + const size_t atsign_size = atsign_len + 1; + if ((ctx->atsign = malloc(sizeof(char) * atsign_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for atsign\n"); goto exit; } - memcpy(ctx->atsign, atsign, atsignlen); - ctx->atsign[atsignlen] = '\0'; + memcpy(ctx->atsign, atsign, atsign_len); + ctx->atsign[atsign_len] = '\0'; atclient_set_atsign_initialized(ctx, true); @@ -75,10 +75,10 @@ void atclient_unset_atsign(atclient *ctx) { } bool atclient_is_atserver_connection_started(const atclient *ctx) { - return ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] & ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; + return ctx->_initialized_fields[ATCLIENT_ATSERVER_CONNECTION_INDEX] & ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; } -int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, const int secondaryport) { +int atclient_start_atserver_connection(atclient *ctx, const char *secondary_host, const int secondary_port) { int ret = 1; // error by default // remove hooks to preserve them across resets @@ -94,7 +94,7 @@ int atclient_start_atserver_connection(atclient *ctx, const char *secondaryhost, // add back hooks ctx->atserver_connection.hooks = conn_hooks; - if ((ret = atclient_connection_connect(&(ctx->atserver_connection), secondaryhost, secondaryport)) != 0) { + if ((ret = atclient_connection_connect(&(ctx->atserver_connection), secondary_host, secondary_port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_connect: %d\n", ret); goto exit; } @@ -116,7 +116,7 @@ void atclient_stop_atserver_connection(atclient *ctx) { } bool atclient_is_atsign_initialized(const atclient *ctx) { - return ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] & ATCLIENT_ATSIGN_INITIALIZED; + return ctx->_initialized_fields[ATCLIENT_ATSIGN_INDEX] & ATCLIENT_ATSIGN_INITIALIZED; } int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const int atserver_port, @@ -137,30 +137,30 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i */ // free later - char *rootcmd = NULL; - char *fromcmd = NULL; - char *pkamcmd = NULL; + char *root_cmd = NULL; + char *from_cmd = NULL; + char *pkam_cmd = NULL; char *atsign_with_at = NULL; const size_t recvsize = 1024; // sufficient buffer size to receive 1. host & port from atDirectory, 2. challenge from - // `from:` command, 3. pkam success message from `pkam:` command + // `from:` noop_cmd, 3. pkam success message from `pkam:` noop_cmd unsigned char recv[recvsize]; memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen; + size_t recv_len; - const size_t challengesize = 256; // sufficient buffer size to hold the challenge received from `from:` command - char challenge[challengesize]; - memset(challenge, 0, sizeof(char) * challengesize); - size_t challengelen = 0; + const size_t challenge_size = 256; // sufficient buffer size to hold the challenge received from `from:` noop_cmd + char challenge[challenge_size]; + memset(challenge, 0, sizeof(char) * challenge_size); + size_t challenge_len = 0; - const size_t signaturesize = 256; // RSA-2048 signature always generates a 256 byte signature - unsigned char signature[signaturesize]; - memset(signature, 0, sizeof(unsigned char) * signaturesize); + const size_t signature_size = 256; // RSA-2048 signature always generates a 256 byte signature + unsigned char signature[signature_size]; + memset(signature, 0, sizeof(unsigned char) * signature_size); - const size_t signaturebase64size = atchops_base64_encoded_size(signaturesize); - unsigned char signaturebase64[signaturebase64size]; - memset(signaturebase64, 0, sizeof(unsigned char) * signaturebase64size); - size_t signaturebase64len = 0; + const size_t signature_base64_size = atchops_base64_encoded_size(signature_size); + unsigned char signature_base64[signature_base64_size]; + memset(signature_base64, 0, sizeof(unsigned char) * signature_base64_size); + size_t signature_base64_len = 0; /* * 3. Ensure that the atsign has the @ symbol. @@ -183,22 +183,21 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i } /* - * 5a. Build `from:` command + * 5a. Build `from:` noop_cmd */ - const size_t fromcmdsize = + const size_t from_cmd_size = strlen("from:") + strlen(atsign_without_at) + strlen("\r\n") + 1; // "from:" has a length of 5 - fromcmd = malloc(sizeof(char) * fromcmdsize); - if (fromcmd == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for fromcmd\n"); + if ((from_cmd = malloc(sizeof(char) * from_cmd_size)) == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for from_cmd\n"); goto exit; } - snprintf(fromcmd, fromcmdsize, "from:%s\r\n", atsign_without_at); + snprintf(from_cmd, from_cmd_size, "from:%s\r\n", atsign_without_at); /* - * 5b. Send `from:` command + * 5b. Send `from:` noop_cmd */ - if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)fromcmd, fromcmdsize - 1, recv, - recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)from_cmd, from_cmd_size - 1, recv, + recvsize, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -206,7 +205,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -214,42 +213,41 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i * 6. We got `data:` * Let us sign the challenge with RSA-2048 PKAM Private Key and Base64 Encode it */ - memcpy(challenge, recv, recvlen); + memcpy(challenge, recv, recv_len); // remove data: - memmove(challenge, challenge + 5, recvlen - 5); - challengelen = recvlen - 5; + memmove(challenge, challenge + 5, recv_len - 5); + challenge_len = recv_len - 5; // sign - if ((ret = atchops_rsa_sign(atkeys->pkamprivatekey, ATCHOPS_MD_SHA256, (unsigned char *)challenge, challengelen, + if ((ret = atchops_rsa_sign(atkeys->pkam_private_key, ATCHOPS_MD_SHA256, (unsigned char *)challenge, challenge_len, signature)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_sign: %d\n", ret); goto exit; } // base64 encode it - if ((ret = atchops_base64_encode(signature, signaturesize, signaturebase64, signaturebase64size, - &signaturebase64len)) != 0) { + if ((ret = atchops_base64_encode(signature, signature_size, signature_base64, signature_base64_size, + &signature_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } /* - * 7a. Build `pkam:` command + * 7a. Build `pkam:` noop_cmd */ - const size_t pkamcmdsize = strlen("pkam:") + signaturebase64len + strlen("\r\n") + 1; - pkamcmd = malloc(sizeof(char) * pkamcmdsize); - if (pkamcmd == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for pkamcmd\n"); + const size_t pkam_cmd_size = strlen("pkam:") + 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(pkamcmd, pkamcmdsize, "pkam:%s\r\n", signaturebase64); + snprintf(pkam_cmd, pkam_cmd_size, "pkam:%s\r\n", signature_base64); /* - * 7b. Send `pkam:` command + * 7b. Send `pkam:` noop_cmd */ memset(recv, 0, sizeof(unsigned char) * recvsize); - if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)pkamcmd, pkamcmdsize - 1, recv, - recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)pkam_cmd, pkam_cmd_size - 1, recv, + recvsize, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -258,7 +256,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i if (!atclient_stringutils_starts_with((char *)recv, "data:success")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:success\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -280,9 +278,9 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; exit: { free(atsign_with_at); - free(rootcmd); - free(fromcmd); - free(pkamcmd); + free(root_cmd); + free(from_cmd); + free(pkam_cmd); return ret; } } @@ -292,8 +290,8 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { unsigned char *recv = NULL; - const char *command = "noop:0\r\n"; - const size_t commandlen = strlen(command); + const char *noop_cmd = "noop:0\r\n"; + const size_t noop_cmd_len = strlen(noop_cmd); const size_t recvsize = 64; if (!heartbeat_conn->async_read) { @@ -305,13 +303,13 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { } memset(recv, 0, sizeof(unsigned char) * recvsize); } - size_t recvlen = 0; + size_t recv_len = 0; char *ptr = (char *)recv; - ret = atclient_connection_send(&heartbeat_conn->atserver_connection, (unsigned char *)command, commandlen, recv, - recvsize, &recvlen); + ret = atclient_connection_send(&heartbeat_conn->atserver_connection, (unsigned char *)noop_cmd, noop_cmd_len, recv, + recvsize, &recv_len); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to send noop command: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to send noop noop_cmd: %d\n", ret); goto exit; } else if (heartbeat_conn->async_read) { goto exit; @@ -343,17 +341,17 @@ void atclient_set_read_timeout(atclient *ctx, int timeout_ms) { static void atclient_set_atsign_initialized(atclient *ctx, const bool initialized) { if (initialized) { - ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] |= ATCLIENT_ATSIGN_INITIALIZED; + ctx->_initialized_fields[ATCLIENT_ATSIGN_INDEX] |= ATCLIENT_ATSIGN_INITIALIZED; } else { - ctx->_initializedfields[ATCLIENT_ATSIGN_INDEX] &= ~ATCLIENT_ATSIGN_INITIALIZED; + ctx->_initialized_fields[ATCLIENT_ATSIGN_INDEX] &= ~ATCLIENT_ATSIGN_INITIALIZED; } } static void atclient_set_atserver_connection_started(atclient *ctx, const bool started) { if (started) { - ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] |= ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; + ctx->_initialized_fields[ATCLIENT_ATSERVER_CONNECTION_INDEX] |= ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; } else { - ctx->_initializedfields[ATCLIENT_ATSERVER_CONNECTION_INDEX] &= ~ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; + ctx->_initialized_fields[ATCLIENT_ATSERVER_CONNECTION_INDEX] &= ~ATCLIENT_ATSERVER_CONNECTION_INITIALIZED; } } diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 4b2d6b3d..ae24c13f 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -25,46 +25,46 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit /* * 2. Initialize variables */ - char *cmdbuffer = NULL; - char *atkeystr = NULL; + char *delete_cmd = NULL; + char *atkey_str = NULL; - const size_t recvsize = 256; // sufficient buffer size to receive response containing commit id + const size_t recv_size = 256; // sufficient buffer size to receive response containing commit id unsigned char *recv = NULL; if (!atclient->async_read) { - recv = malloc(sizeof(unsigned char) * recvsize); + recv = malloc(sizeof(unsigned char) * recv_size); if (recv == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); goto exit; } - memset(recv, 0, sizeof(unsigned char) * recvsize); + memset(recv, 0, sizeof(unsigned char) * recv_size); } - size_t recvlen = 0; + size_t recv_len = 0; /* * 3. Build delete command */ - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - const size_t atkeystrlen = strlen(atkeystr); + const size_t atkey_strlen = strlen(atkey_str); - const size_t cmdbuffersize = strlen("delete:") + atkeystrlen + strlen("\r\n") + 1; - cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if (cmdbuffer == NULL) { + const size_t delete_cmd_size = strlen("delete:") + atkey_strlen + strlen("\r\n") + 1; + delete_cmd = malloc(sizeof(char) * delete_cmd_size); + if (delete_cmd == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for delete_cmd\n"); goto exit; } - snprintf(cmdbuffer, cmdbuffersize, "delete:%s\r\n", atkeystr); + snprintf(delete_cmd, delete_cmd_size, "delete:%s\r\n", atkey_str); /* * 4. Send command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, - recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)delete_cmd, delete_cmd_size - 1, + recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -78,7 +78,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit if (!atclient_stringutils_starts_with(respose, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -87,13 +87,13 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit if(commit_id != NULL) { *commit_id = atoi(response_without_data); } - + ret = 0; goto exit; exit: { free(recv); - free(atkeystr); - free(cmdbuffer); + free(atkey_str); + free(delete_cmd); return ret; } } @@ -131,9 +131,9 @@ static int atclient_delete_validate_arguments(const atclient *atclient, const at goto exit; } - if (!atclient_atkey_is_sharedby_initialized(atkey)) { + if (!atclient_atkey_is_shared_by_initialized(atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_sharedby_initialized is false\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_shared_by_initialized is false\n"); goto exit; } diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index cca5f749..c55c5201 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -31,9 +31,9 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi /* * 2. Variables */ - const size_t scancmdsize = strlen("scan") + (showhidden ? strlen(":showHidden:true") : 0) + + const size_t scan_cmd_buf_size = strlen("scan") + (showhidden ? strlen(":showHidden:true") : 0) + (strlen(regex) > 0 ? (strlen(" ") + strlen(regex)) : 0) + strlen("\r\n") + 1; - char scancmd[scancmdsize]; + char scan_cmd[scan_cmd_buf_size]; unsigned char recv[recvbuffersize]; size_t recvlen = 0; @@ -43,13 +43,13 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi /* * 3. Build scan command */ - snprintf(scancmd, scancmdsize, "scan%s%s%s\r\n", showhidden ? ":showHidden:true" : "", strlen(regex) > 0 ? " " : "", + snprintf(scan_cmd, scan_cmd_buf_size, "scan%s%s%s\r\n", showhidden ? ":showHidden:true" : "", strlen(regex) > 0 ? " " : "", regex); /* * 4. Send scan command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scancmd, scancmdsize - 1, recv, + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scan_cmd, scan_cmd_buf_size - 1, recv, recvbuffersize, &recvlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_publickey.c index d95da6cf..d42fbe61 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_publickey.c @@ -9,16 +9,16 @@ #define TAG "atclient_get_publickey" static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, bool bypasscache); + const size_t value_size, size_t *value_len, bool bypass_cache); -int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, bool bypasscache) { +int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, bool bypass_cache) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_publickey_validate_arguments(atclient, atkey, value, valuesize, valuelen, bypasscache)) != + if ((ret = atclient_get_publickey_validate_arguments(atclient, atkey, value, value_size, value_len, bypass_cache)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_publickey_validate_arguments: %d\n", ret); return ret; @@ -27,54 +27,53 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu /* * 2. Initialize variables */ - char *atkeystr = NULL; + char *atkey_str = NULL; - const size_t recvsize = valuesize; - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + const size_t recv_size = value_size; + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; cJSON *root = NULL; - char *cmdbuffer = NULL; - char *metadatastr = NULL; + char *plookup_cmd = NULL; + char *metadata_str = NULL; /* * 3. Build `plookup:` command */ - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - char *atkeystrwithoutpublic = NULL; - char *ptr = strstr(atkeystr, "public:"); + char *atkey_str_without_public = NULL; + char *ptr = strstr(atkey_str, "public:"); if (ptr != NULL) { - atkeystrwithoutpublic = ptr + strlen("public:"); + atkey_str_without_public = ptr + strlen("public:"); } else { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not find \"public:\" from string \"%s\"\n", atkeystr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not find \"public:\" from string \"%s\"\n", atkey_str); goto exit; } - const size_t cmdbuffersize = strlen("plookup:all:\r\n") + (bypasscache ? strlen("bypassCache:true:") : 0) + - strlen(atkeystrwithoutpublic) + 1; - cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if (cmdbuffer == NULL) { + const size_t plookup_cmd_size = strlen("plookup:all:\r\n") + (bypass_cache ? strlen("bypassCache:true:") : 0) + + strlen(atkey_str_without_public) + 1; + plookup_cmd = malloc(sizeof(char) * plookup_cmd_size); + if (plookup_cmd == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plookup_cmd\n"); goto exit; } - memset(cmdbuffer, 0, cmdbuffersize); - snprintf(cmdbuffer, cmdbuffersize, "plookup:%sall:%s\r\n", bypasscache ? "bypassCache:true:" : "", - atkeystrwithoutpublic); - const size_t cmdbufferlen = strlen(cmdbuffer); + memset(plookup_cmd, 0, plookup_cmd_size); + snprintf(plookup_cmd, plookup_cmd_size, "plookup:%sall:%s\r\n", bypass_cache ? "bypassCache:true:" : "", + atkey_str_without_public); + const size_t cmdbufferlen = strlen(plookup_cmd); /* * 4. Send `plookup:` command */ - ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbufferlen, recv, - recvsize, &recvlen); - if (ret != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)plookup_cmd, cmdbufferlen, + recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -82,16 +81,17 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu /* * 5. Parse response */ - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + char *response = (char *)recv; + if (!atclient_stringutils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } - char *recvwithoutdata = (char *)recv + 5; + char *response_without_data = response + 5; - root = cJSON_Parse(recvwithoutdata); + root = cJSON_Parse(response_without_data); if (root == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); @@ -109,7 +109,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * 6. Return data to caller */ memcpy(value, data->valuestring, strlen(data->valuestring)); - *valuelen = strlen(value); + *value_len = strlen(value); // 6b. write to atkey->metadata cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); @@ -119,10 +119,10 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; } - metadatastr = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -132,15 +132,15 @@ exit: { if (root != NULL) { cJSON_Delete(root); } - free(metadatastr); - free(cmdbuffer); - free(atkeystr); + free(metadata_str); + free(plookup_cmd); + free(atkey_str); return ret; } } static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, bool bypasscache) { + const size_t value_size, size_t *value_len, bool bypass_cache) { int ret = 1; if (atclient == NULL) { @@ -157,7 +157,7 @@ static int atclient_get_publickey_validate_arguments(atclient *atclient, atclien const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a public key\n"); goto exit; @@ -169,25 +169,25 @@ static int atclient_get_publickey_validate_arguments(atclient *atclient, atclien goto exit; } - if (valuesize == 0) { + if (value_size == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); goto exit; } - if (valuelen == NULL) { + if (value_len == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); goto exit; } - if(!atclient_is_atserver_connection_started(atclient)) { + if (!atclient_is_atserver_connection_started(atclient)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); goto exit; } - if(!atclient_is_atsign_initialized(atclient)) { + if (!atclient_is_atsign_initialized(atclient)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign not initialized\n"); goto exit; diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 98a013be..27d4701e 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -15,16 +15,16 @@ #define TAG "atclient_get_selfkey" static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t valuesize, const size_t *valuelen); + const char *value, const size_t value_size, const size_t *value_len); -int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen) { +int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, valuesize, valuelen)) != 0) { + if ((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, value_size, value_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey_valid_arguments: %d\n", ret); return ret; } @@ -32,51 +32,52 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, /* * 2. Initialize variables */ - char *atkeystr = NULL; + char *atkey_str = NULL; - const size_t recvsize = valuesize; - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + const size_t recv_size = value_size; + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; - const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; // 32 byte = 256 bits - unsigned char selfencryptionkey[selfencryptionkeysize]; - memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); - size_t selfencryptionkeylen = 0; + const size_t self_encryption_size = ATCHOPS_AES_256 / 8; // 32 byte = 256 bits + unsigned char self_encryption_key[self_encryption_size]; + memset(self_encryption_key, 0, sizeof(unsigned char) * self_encryption_size); + size_t self_encryption_key_len = 0; unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; // free later cJSON *root = NULL; - char *cmdbuffer = NULL; - char *valueraw = NULL; + char *llookup_cmd = NULL; + char *value_raw = NULL; + char *metadata_str = NULL; /* * 3. Build `llookup:` command */ - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - const size_t atkeystrlen = strlen(atkeystr); + const size_t atkey_strlen = strlen(atkey_str); - const size_t cmdbuffersize = strlen("llookup:all:\r\n") + atkeystrlen + 1; - cmdbuffer = (char *)malloc(sizeof(char) * cmdbuffersize); - if (cmdbuffer == NULL) { + const size_t llookup_cmd_size = strlen("llookup:all:\r\n") + atkey_strlen + 1; + llookup_cmd = (char *)malloc(sizeof(char) * llookup_cmd_size); + if (llookup_cmd == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for llookup_cmd\n"); goto exit; } - memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); + memset(llookup_cmd, 0, sizeof(char) * llookup_cmd_size); - snprintf(cmdbuffer, cmdbuffersize, "llookup:all:%.*s\r\n", (int)atkeystrlen, atkeystr); + snprintf(llookup_cmd, llookup_cmd_size, "llookup:all:%.*s\r\n", (int)atkey_strlen, atkey_str); /* * 4. Send `llookup:` command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, - recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, + llookup_cmd_size - 1, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -84,17 +85,17 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, /* * 5. Parse response */ - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + char *response = (char *)recv; + if (!atclient_stringutils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } - char *recvwithoutdata = (char *)recv + 5; + char *response_without_data = (char *)recv + 5; - root = cJSON_Parse(recvwithoutdata); - if (root == NULL) { + if ((root = cJSON_Parse(response_without_data)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); goto exit; @@ -107,32 +108,22 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, goto exit; } - char *metadatastr = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } /** * 6. Decrypt value */ - - if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { - size_t ivlen; - ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, - ATCHOPS_IV_BUFFER_SIZE, &ivlen); - if (ret != 0) { + if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, + ATCHOPS_IV_BUFFER_SIZE, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - - if (ivlen != ATCHOPS_IV_BUFFER_SIZE) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivlen != ATCHOPS_IV_BUFFER_SIZE (%d != %d)\n", ivlen, - ATCHOPS_IV_BUFFER_SIZE); - goto exit; - } } else { // use legacy IV memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); @@ -145,32 +136,31 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, goto exit; } - if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.selfencryptionkeybase64, - strlen(atclient->atkeys.selfencryptionkeybase64), selfencryptionkey, selfencryptionkeysize, - &selfencryptionkeylen)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.self_encryption_key_base64, + strlen(atclient->atkeys.self_encryption_key_base64), self_encryption_key, + self_encryption_size, &self_encryption_key_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } // holds base64 decoded value. Once decoded, it is encrypted cipher text bytes that need to be decrypted - const size_t valuerawsize = atchops_base64_decoded_size(strlen(data->valuestring)); - valueraw = (char *)malloc(sizeof(char) * valuerawsize); - if (valueraw == NULL) { + const size_t value_raw_size = atchops_base64_decoded_size(strlen(data->valuestring)); + if ((value_raw = (char *)malloc(sizeof(char) * value_raw_size)) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for valueraw\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); goto exit; } - memset(valueraw, 0, sizeof(char) * valuerawsize); - size_t valuerawlen = 0; + memset(value_raw, 0, sizeof(char) * value_raw_size); + size_t value_raw_len = 0; if ((ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), - (unsigned char *)valueraw, valuerawsize, &valuerawlen)) != 0) { + (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)valueraw, valuerawlen, - (unsigned char *)value, valuesize, valuelen)) != 0) { + if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw, value_raw_len, + (unsigned char *)value, value_size, value_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } @@ -182,15 +172,16 @@ exit: { if (root != NULL) { cJSON_Delete(root); } - free(valueraw); - free(cmdbuffer); - free(atkeystr); + free(value_raw); + free(llookup_cmd); + free(atkey_str); + free(metadata_str); return ret; } } static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t valuesize, const size_t *valuelen) { + const char *value, const size_t value_size, const size_t *value_len) { int ret = 1; if (atclient == NULL) { @@ -229,9 +220,9 @@ static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const goto exit; } - if (!atclient_atkey_is_sharedby_initialized(atkey)) { + if (!atclient_atkey_is_shared_by_initialized(atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized when it should be\n"); goto exit; } @@ -241,15 +232,15 @@ static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const goto exit; } - if (valuesize == 0) { + if (value_size == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); goto exit; } - if (valuelen == NULL) { + if (value_len == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); goto exit; } diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index 58c762d5..c01828be 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -13,22 +13,22 @@ #define TAG "atclient_get_sharedkey" static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t valuesize, const size_t *valuelen, + const char *value, const size_t value_size, const size_t *value_len, const unsigned char *shared_encryption_key); static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, + const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key); static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, + const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key); -int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t valuesize, - size_t *valuelen, const unsigned char *shared_encryption_key) { +int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; - if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, valuesize, valuelen, + if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_validate_arguments: %d\n", ret); return ret; @@ -42,19 +42,19 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedby, &sharedby_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_by, &sharedby_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, valuesize, valuelen, + if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, valuesize, valuelen, + if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_me_with_other: %d\n", ret); goto exit; @@ -70,7 +70,7 @@ exit: { } static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, + const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -88,21 +88,23 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at unsigned char shared_encryption_key_to_use[ATCHOPS_AES_256 / 8]; memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * ATCHOPS_AES_256 / 8); - char *atkeystr = NULL; - char *command = NULL; + char *atkey_str = NULL; + char *llookup_cmd = NULL; - const size_t recvsize = 4096; - unsigned char recv[recvsize]; - size_t recvlen = 0; + const size_t recv_size = 4096; + unsigned char recv[recv_size]; + size_t recv_len = 0; - const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; - unsigned char iv[ivsize]; + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; char *value_raw_encrypted = NULL; char *value_raw = NULL; cJSON *root = NULL; + char *metadata_str = NULL; + /* * 3. Format atSigns */ @@ -111,7 +113,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } @@ -122,7 +124,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at if (shared_encryption_key != NULL) { memcpy(shared_encryption_key_to_use, shared_encryption_key, ATCHOPS_AES_256 / 8); } else { - if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->sharedwith, + if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->shared_with, shared_encryption_key_to_use)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); goto exit; @@ -130,27 +132,27 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at } /* - * 5. Build `llookup:all:` command + * 5. Build `llookup:all:` llookup_cmd */ - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - const size_t atkeystrlen = strlen(atkeystr); + const size_t atkey_strlen = strlen(atkey_str); - const size_t commandsize = strlen("llookup:all:") + atkeystrlen + strlen("\r\n") + 1; - if ((command = malloc(sizeof(char) * commandsize)) == NULL) { + const size_t llookup_cmd_size = strlen("llookup:all:") + atkey_strlen + strlen("\r\n") + 1; + if ((llookup_cmd = malloc(sizeof(char) * llookup_cmd_size)) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for llookup_cmd\n"); goto exit; } - snprintf(command, commandsize, "llookup:all:%s\r\n", atkeystr); + snprintf(llookup_cmd, llookup_cmd_size, "llookup:all:%s\r\n", atkey_str); /* - * 6. Send llookup:all: command + * 6. Send llookup:all: llookup_cmd */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, - recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, llookup_cmd_size - 1, recv, + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -182,10 +184,10 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at goto exit; } - char *metadatastr = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -199,8 +201,8 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at /* * 8. Set IV in the AtKey */ - if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, + if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, ATCHOPS_IV_BUFFER_SIZE, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; @@ -250,7 +252,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at * 10. Set value */ memcpy(value, value_raw, value_raw_len); - *valuelen = value_raw_len; + *value_len = value_raw_len; ret = 0; goto exit; @@ -258,16 +260,16 @@ exit: { free(client_atsign_with_at); free(recipient_atsign_with_at); free(value_raw); - free(atkeystr); - free(command); - free(metadatastr); + free(atkey_str); + free(llookup_cmd); + free(metadata_str); cJSON_Delete(root); return ret; } } static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t valuesize, size_t *valuelen, + const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -285,16 +287,16 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; unsigned char shared_encryption_key_to_use[shared_encryption_key_size]; - const size_t recvsize = 4096; - unsigned char recv[recvsize]; + const size_t recv_size = 4096; + unsigned char recv[recv_size]; - char *command = NULL; + char *llookup_cmd = NULL; cJSON *root = NULL; - char *metadatastr = NULL; + char *metadata_str = NULL; - const size_t ivsize = ATCHOPS_IV_BUFFER_SIZE; - unsigned char iv[ivsize]; + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; unsigned char *value_raw_encryted = NULL; unsigned char *value_raw = NULL; @@ -302,12 +304,12 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 3. Format atSigns */ - if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedby, &sender_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_by, &sender_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); goto exit; } @@ -316,7 +318,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at * 4. Get shared_encryption_key, if necessary */ if (shared_encryption_key == NULL) { - if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, atkey->sharedby, + if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, atkey->shared_by, shared_encryption_key_to_use)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_other: %d\n", ret); goto exit; @@ -326,32 +328,32 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at } /* - * 5. Build lookup: command + * 5. Build lookup: llookup_cmd */ - size_t commandsize = strlen("lookup:all:") + strlen(atkey->key); + size_t llookup_cmd_size = strlen("lookup:all:") + strlen(atkey->key); bool namespace_exists = atclient_atkey_is_namespacestr_initialized(atkey); if (namespace_exists) { - commandsize += strlen(".") + strlen(atkey->namespacestr); + llookup_cmd_size += strlen(".") + strlen(atkey->namespace_str); } - commandsize += strlen(sender_atsign_with_at) + strlen("\r\n") + 1; - if ((command = (char *)malloc(sizeof(char) * commandsize)) == NULL) { + llookup_cmd_size += strlen(sender_atsign_with_at) + strlen("\r\n") + 1; + if ((llookup_cmd = (char *)malloc(sizeof(char) * llookup_cmd_size)) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for command\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for llookup_cmd\n"); goto exit; } if (namespace_exists) { - snprintf(command, commandsize, "lookup:all:%s.%s%s\r\n", atkey->key, atkey->namespacestr, sender_atsign_with_at); + snprintf(llookup_cmd, llookup_cmd_size, "lookup:all:%s.%s%s\r\n", atkey->key, atkey->namespace_str, sender_atsign_with_at); } else { - snprintf(command, commandsize, "lookup:all:%s%s\r\n", atkey->key, sender_atsign_with_at); + snprintf(llookup_cmd, llookup_cmd_size, "lookup:all:%s%s\r\n", atkey->key, sender_atsign_with_at); } /* - * 6. Send lookup: command + * 6. Send lookup: llookup_cmd */ - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)command, commandsize - 1, recv, - recvsize, &recvlen)) != 0) { + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, llookup_cmd_size - 1, recv, + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -383,10 +385,10 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at goto exit; } - metadatastr = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&(atkey->metadata), metadatastr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr: %d\n", ret); + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -400,9 +402,9 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 8. Set IV */ - if (atclient_atkey_metadata_is_ivnonce_initialized(&atkey->metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.ivnonce, strlen(atkey->metadata.ivnonce), iv, - ivsize, NULL)) != 0) { + if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, + iv_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -452,7 +454,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at * 10. Set value */ memcpy(value, value_raw, value_raw_len); - *valuelen = value_raw_len; + *value_len = value_raw_len; ret = 0; goto exit; @@ -461,15 +463,15 @@ exit: { free(recipient_atsign_with_at); free(value_raw_encryted); free(value_raw); - free(command); - free(metadatastr); + free(llookup_cmd); + free(metadata_str); cJSON_Delete(root); return ret; } } static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t valuesize, const size_t *valuelen, + const char *value, const size_t value_size, const size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -505,7 +507,7 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, c goto exit; } - if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); goto exit; @@ -517,15 +519,15 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, c goto exit; } - if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->key) <= 0) { + if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->key) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is not initialized or is empty\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is not initialized or is empty\n"); goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(atkey) || strlen(atkey->key) <= 0) { + if (!atclient_atkey_is_shared_with_initialized(atkey) || strlen(atkey->key) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is not initialized or is empty\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is not initialized or is empty\n"); goto exit; } @@ -535,15 +537,15 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, c goto exit; } - if (valuesize == 0) { + if (value_size == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuesize is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); goto exit; } - if (valuelen == NULL) { + if (value_len == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); goto exit; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index dd9d20bd..41290063 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -15,15 +15,15 @@ #define TAG "atclient_put" static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const size_t valuelen, const int *commitid); + const size_t value_len, const int *commit_id); -int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const size_t valuelen, int *commitid) { +int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const size_t value_len, int *commit_id) { int ret = 1; /* * 1. Check if valid arguments were passed */ - if ((ret = atclient_put_validate_args(ctx, atkey, value, valuelen, commitid)) != 0) { + if ((ret = atclient_put_validate_args(ctx, atkey, value, value_len, commit_id)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_validate_args: %d\n", ret); return ret; } @@ -31,57 +31,50 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const /* * 2. Allocate variables */ - char *atkeystr = NULL; - size_t atkeystrlen = 0; + char *atkey_str = NULL; + char *update_cmd = NULL; + char *metadata_protocol_str = NULL; - char *cmdbuffer = NULL; - size_t cmdbuffersize = 0; + const short iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; + memset(iv, 0, sizeof(unsigned char) * iv_size); - char *metadataprotocolstr = NULL; - size_t metadataprotocolstrlen = 0; + const size_t iv_base64_size = atchops_base64_encoded_size(iv_size) + 1; + char iv_base64[iv_base64_size]; + memset(iv_base64, 0, sizeof(char) * iv_base64_size); - const short ivsize = ATCHOPS_IV_BUFFER_SIZE; - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - memset(iv, 0, sizeof(unsigned char) * ivsize); - - const size_t ivbase64size = atchops_base64_encoded_size(ivsize) + 1; - char ivbase64[ivbase64size]; - memset(ivbase64, 0, sizeof(char) * ivbase64size); - size_t ivbase64len = 0; - - const size_t ciphertextsize = atchops_aesctr_ciphertext_size(valuelen) + 1; - unsigned char ciphertext[ciphertextsize]; - memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); - size_t ciphertextlen = 0; - - const size_t ciphertextbase64size = atchops_base64_encoded_size(ciphertextsize) + 1; - char ciphertextbase64[ciphertextbase64size]; - memset(ciphertextbase64, 0, sizeof(char) * ciphertextbase64size); - size_t ciphertextbase64len = 0; - - const size_t sharedenckeysize = ATCHOPS_AES_256 / 8; - unsigned char sharedenckey[sharedenckeysize]; - memset(sharedenckey, 0, sizeof(unsigned char) * sharedenckeysize); - size_t sharedenckeylen = 0; - - const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize) + 1; - char sharedenckeybase64[sharedenckeybase64size]; - memset(sharedenckeybase64, 0, sizeof(char) * sharedenckeybase64size); - size_t sharedenckeybase64len = 0; - - const size_t recvsize = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to - // receive commmit id from `update:` + const size_t ciphertext_size = atchops_aesctr_ciphertext_size(value_len) + 1; + unsigned char ciphertext[ciphertext_size]; + memset(ciphertext, 0, sizeof(unsigned char) * ciphertext_size); + size_t ciphertext_len = 0; + + const size_t ciphertext_base64_size = atchops_base64_encoded_size(ciphertext_size) + 1; + char ciphertext_base64[ciphertext_base64_size]; + memset(ciphertext_base64, 0, sizeof(char) * ciphertext_base64_size); + size_t ciphertextbase64_len = 0; + + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key[shared_encryption_key_size]; + memset(shared_encryption_key, 0, sizeof(unsigned char) * shared_encryption_key_size); + + const size_t shared_encryption_key_base64_size = atchops_base64_encoded_size(shared_encryption_key_size) + 1; + char shared_encryption_key_base64[shared_encryption_key_base64_size]; + memset(shared_encryption_key_base64, 0, sizeof(char) * shared_encryption_key_base64_size); + size_t shared_encryption_key_base64_len = 0; + + const size_t recv_size = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to + // receive commmit id from `update:` unsigned char *recv = NULL; if (!ctx->async_read) { - recv = malloc(sizeof(unsigned char) * recvsize); + recv = malloc(sizeof(unsigned char) * recv_size); if (recv == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); goto exit; } - memset(recv, 0, sizeof(unsigned char) * recvsize); + memset(recv, 0, sizeof(unsigned char) * recv_size); } - size_t recvlen = 0; + size_t recv_len = 0; /* * 3. Build `update:` command @@ -96,36 +89,36 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); - if (atkey_type == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + if (atkey_type == ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { // 3a.1 no encryption - memcpy(ciphertextbase64, value, valuelen); - ciphertextbase64len = valuelen; - } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SELFKEY) { + memcpy(ciphertext_base64, value, value_len); + ciphertextbase64_len = value_len; + } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SELF_KEY) { // 3a.2 encrypt with self encryption key const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; unsigned char selfencryptionkey[selfencryptionkeysize]; memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); size_t selfencryptionkeylen = 0; - if ((ret = atchops_base64_decode((const unsigned char *)ctx->atkeys.selfencryptionkeybase64, - strlen(ctx->atkeys.selfencryptionkeybase64), selfencryptionkey, + if ((ret = atchops_base64_decode((const unsigned char *)ctx->atkeys.self_encryption_key_base64, + strlen(ctx->atkeys.self_encryption_key_base64), selfencryptionkey, selfencryptionkeysize, &selfencryptionkeylen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - if ((ret = atchops_aesctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, - ciphertext, ciphertextsize, &ciphertextlen)) != 0) { + if ((ret = atchops_aesctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + ciphertext, ciphertext_size, &ciphertext_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); goto exit; } - if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, - &ciphertextbase64len)) != 0) { + if ((ret = atchops_base64_encode(ciphertext, ciphertext_len, (unsigned char *)ciphertext_base64, + ciphertext_base64_size, &ciphertextbase64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } - } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SHARED_KEY) { // 3aA.3 encrypt with shared encryption key // get our AES shared key @@ -134,16 +127,17 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const // create one for the other person -> encrypted with their public encryption key char *recipient_atsign_with_at = NULL; - if ((ret = atclient_stringutils_atsign_with_at(atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_concat_at_sign_with_at: %d\n", ret); goto shared_key_exit; } - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, sharedenckey)) != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, shared_encryption_key)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Creating shared encryption key\n"); if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, - sharedenckey)) != 0) { + shared_encryption_key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); goto shared_key_exit; @@ -156,25 +150,25 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const goto shared_key_exit; } - if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)ivbase64, ivbase64size, - &ivbase64len)) != 0) { + if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)iv_base64, iv_base64_size, NULL)) != + 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto shared_key_exit; } - if ((ret = atclient_atkey_metadata_set_ivnonce(&(atkey->metadata), ivbase64)) != 0) { + if ((ret = atclient_atkey_metadata_set_iv_nonce(&(atkey->metadata), iv_base64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); goto shared_key_exit; } - if ((ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)value, valuelen, ciphertext, - ciphertextsize, &ciphertextlen)) != 0) { + if ((ret = atchops_aesctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + ciphertext, ciphertext_size, &ciphertext_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); goto shared_key_exit; } - if ((ret = atchops_base64_encode(ciphertext, ciphertextlen, (unsigned char *)ciphertextbase64, ciphertextbase64size, - &ciphertextbase64len)) != 0) { + if ((ret = atchops_base64_encode(ciphertext, ciphertext_len, (unsigned char *)ciphertext_base64, + ciphertext_base64_size, &ciphertextbase64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto shared_key_exit; } @@ -189,35 +183,35 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const // 3b. Build the command - if ((ret = atclient_atkey_to_string(atkey, &atkeystr)) != 0) { + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); goto exit; } - atkeystrlen = strlen(atkeystr); + const size_t atkey_strlen = strlen(atkey_str); - if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadataprotocolstr))) != 0) { + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadata_protocol_str))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); goto exit; } - metadataprotocolstrlen = strlen(metadataprotocolstr); + const size_t metadataprotocolstrlen = strlen(metadata_protocol_str); - cmdbuffersize = strlen("update: \r\n") + metadataprotocolstrlen + atkeystrlen + ciphertextbase64len + - 1; // + 1 for null terminator - cmdbuffer = malloc(sizeof(char) * cmdbuffersize); - if (cmdbuffer == NULL) { + const size_t update_cmd_size = strlen("update: \r\n") + metadataprotocolstrlen + atkey_strlen + ciphertextbase64_len + + 1; // + 1 for null terminator + update_cmd = malloc(sizeof(char) * update_cmd_size); + if (update_cmd == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); goto exit; } - memset(cmdbuffer, 0, sizeof(char) * cmdbuffersize); - snprintf(cmdbuffer, cmdbuffersize, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadataprotocolstr, - (int)atkeystrlen, atkeystr, (int)ciphertextbase64len, ciphertextbase64); + memset(update_cmd, 0, sizeof(char) * update_cmd_size); + snprintf(update_cmd, update_cmd_size, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadata_protocol_str, + (int)atkey_strlen, atkey_str, (int)ciphertextbase64_len, ciphertext_base64); /* * 4. Send the command */ - if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)cmdbuffer, cmdbuffersize - 1, recv, - recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)update_cmd, update_cmd_size - 1, + recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -229,7 +223,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -237,9 +231,9 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const * 5. Return the commit id. */ - if (commitid != NULL) { + if (commit_id != NULL) { char *recvwithoutdata = (char *)recv + 5; - *commitid = atoi(recvwithoutdata); + *commit_id = atoi(recvwithoutdata); } ret = 0; @@ -248,15 +242,15 @@ exit: { if (!ctx->async_read) { free(recv); } - free(cmdbuffer); - free(metadataprotocolstr); - free(atkeystr); + free(update_cmd); + free(metadata_protocol_str); + free(atkey_str); return ret; } } static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const size_t valuelen, const int *commitid) { + const size_t value_len, const int *commit_id) { int ret = 1; if (ctx == NULL) { ret = 1; @@ -288,21 +282,21 @@ static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey goto exit; } - if (valuelen == 0) { + if (value_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "valuelen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is 0\n"); goto exit; } - if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->shared_by) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not initialized or is empty\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's shared_by is not initialized or is empty\n"); goto exit; } - if (strcmp(atkey->sharedby, ctx->atsign) != 0) { + if (strcmp(atkey->shared_by, ctx->atsign) != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's sharedby is not atclient's atsign\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's shared_by is not atclient's atsign\n"); goto exit; } diff --git a/packages/atclient/src/atclient_utils.c b/packages/atclient/src/atclient_utils.c index 5829e1f2..34b21807 100644 --- a/packages/atclient/src/atclient_utils.c +++ b/packages/atclient/src/atclient_utils.c @@ -18,10 +18,10 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int atclient_connection atdirectory_conn; atclient_connection_init(&atdirectory_conn, ATCLIENT_CONNECTION_TYPE_ATDIRECTORY); - const size_t recvsize = 1024; - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + const size_t recv_size = 1024; + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; char *atsign_without_at_symbol = NULL; char *cmd = NULL; @@ -36,22 +36,22 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int goto exit; } - size_t cmdsize = (strlen(atsign_without_at_symbol)) + strlen("\n") + 1; - cmd = malloc(sizeof(char) * cmdsize); + const size_t cmd_size = (strlen(atsign_without_at_symbol)) + strlen("\n") + 1; + cmd = malloc(sizeof(char) * cmd_size); if (cmd == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmd\n"); ret = 1; goto exit; } - snprintf(cmd, cmdsize, "%s\n", atsign_without_at_symbol); + snprintf(cmd, cmd_size, "%s\n", atsign_without_at_symbol); - if ((ret = atclient_connection_send(&atdirectory_conn, (unsigned char *)cmd, cmdsize - 1, recv, recvsize, - &recvlen)) != 0) { + if ((ret = atclient_connection_send(&atdirectory_conn, (unsigned char *)cmd, cmd_size - 1, recv, recv_size, + &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - if (recvlen == 0) { + if (recv_len == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "No data received from atdirectory\n"); ret = 1; goto exit; @@ -60,8 +60,8 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int // recv has something like `228aafb0-94d3-5aa2-a3b3-e36af115480d.swarm0002.atsign.zone:6943` // we need to split it into host and port char *host = strtok((char *)recv, ":"); - char *portstr = strtok(NULL, ":"); - if (portstr == NULL) { + char *port_str = strtok(NULL, ":"); + if (port_str == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to parse port from atdirectory response\n"); ret = 1; goto exit; @@ -74,7 +74,7 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int ret = 1; goto exit; } - *atserver_port = atoi(portstr); + *atserver_port = atoi(port_str); ret = 0; goto exit; @@ -93,12 +93,12 @@ int atclient_utils_populate_atkeys_from_homedir(atclient_atkeys *atkeys, const c struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; - const size_t atkeyspathsize = strlen(homedir) + strlen("/.atsign/keys/") + strlen(atsign) + strlen("_key.atKeys") + 1; - char atkeyspath[atkeyspathsize]; + const size_t atkeys_path_size = strlen(homedir) + strlen("/.atsign/keys/") + strlen(atsign) + strlen("_key.atKeys") + 1; + char atkeys_path[atkeys_path_size]; - snprintf(atkeyspath, atkeyspathsize, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign); + snprintf(atkeys_path, atkeys_path_size, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign); - if ((ret = atclient_atkeys_populate_from_path(atkeys, atkeyspath)) != 0) { + if ((ret = atclient_atkeys_populate_from_path(atkeys, atkeys_path)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to populate atkeys from path: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 8d944415..066c982e 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -15,10 +15,10 @@ void atclient_atkey_init(atclient_atkey *atkey) { memset(atkey, 0, sizeof(atclient_atkey)); atkey->key = NULL; - atkey->sharedby = NULL; - atkey->sharedwith = NULL; - atkey->namespacestr = NULL; - memset(atkey->_initializedfields, 0, sizeof(atkey->_initializedfields)); + atkey->shared_by = NULL; + atkey->shared_with = NULL; + atkey->namespace_str = NULL; + memset(atkey->_initialized_fields, 0, sizeof(atkey->_initialized_fields)); atclient_atkey_metadata_init(&(atkey->metadata)); } @@ -38,21 +38,21 @@ int atclient_atkey_clone(atclient_atkey *dst, const atclient_atkey *src) { goto exit; } } - if (atclient_atkey_is_sharedby_initialized(src)) { - if ((ret = atclient_atkey_set_sharedby(dst, src->sharedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if (atclient_atkey_is_shared_by_initialized(src)) { + if ((ret = atclient_atkey_set_shared_by(dst, src->shared_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } } - if (atclient_atkey_is_sharedwith_initialized(src)) { - if ((ret = atclient_atkey_set_sharedwith(dst, src->sharedwith)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if (atclient_atkey_is_shared_with_initialized(src)) { + if ((ret = atclient_atkey_set_shared_with(dst, src->shared_with)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } } if (atclient_atkey_is_namespacestr_initialized(src)) { - if ((ret = atclient_atkey_set_namespacestr(dst, src->namespacestr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(dst, src->namespace_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } } @@ -77,12 +77,12 @@ size_t atclient_atkey_strlen(const atclient_atkey *atkey) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); return -1; } - if (!atclient_atkey_is_key_initialized(atkey) || !atclient_atkey_is_sharedby_initialized(atkey)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->sharedby is not initialized\n"); + if (!atclient_atkey_is_key_initialized(atkey) || !atclient_atkey_is_shared_by_initialized(atkey)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->shared_by is not initialized\n"); return -1; } - if (strlen(atkey->key) <= 0 || strlen(atkey->sharedby) <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->sharedby is empty\n"); + if (strlen(atkey->key) <= 0 || strlen(atkey->shared_by) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->key or atkey->shared_by is empty\n"); return -1; } atclient_atkey_type type = atclient_atkey_get_type(atkey); @@ -91,20 +91,20 @@ size_t atclient_atkey_strlen(const atclient_atkey *atkey) { return -1; } size_t len = 0; - if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { + if (atclient_atkey_metadata_is_is_cached_initialized(&(atkey->metadata)) && atkey->metadata.is_cached) { len += strlen("cached:"); } - if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic) { + if (atclient_atkey_metadata_is_is_public_initialized(&(atkey->metadata)) && atkey->metadata.is_public) { len += strlen("public:"); } - if (type == ATCLIENT_ATKEY_TYPE_SHAREDKEY && atclient_atkey_is_sharedwith_initialized(atkey)) { - len += strlen(atkey->sharedwith) + strlen(":"); + if (type == ATCLIENT_ATKEY_TYPE_SHARED_KEY && atclient_atkey_is_shared_with_initialized(atkey)) { + len += strlen(atkey->shared_with) + strlen(":"); } len += strlen(atkey->key); - if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespacestr) > 0) { - len += strlen(".") + strlen(atkey->namespacestr); + if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespace_str) > 0) { + len += strlen(".") + strlen(atkey->namespace_str); } - len += strlen(atkey->sharedby); + len += strlen(atkey->shared_by); return len; } @@ -112,56 +112,73 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { // 6 scenarios: // 1. PublicKey: "public:name.wavi@smoothalligator" // name == "name" - // sharedby = "smoothalligator" - // sharedwith = NULL + // shared_by = "smoothalligator" + // shared_with = NULL // namespace = "wavi" // type = "public" // cached = false // 2. PublicKey (cached): "cached:public:name.wavi@smoothalligator" // name == "name" - // sharedby = "smoothalligator" - // sharedwith = NULL + // shared_by = "smoothalligator" + // shared_with = NULL // namespace = "wavi" // type = "public" // cached = true // 3. SharedKey: "@foo:name.wavi@bar" // name == "name" - // sharedby = "bar" - // sharedwith = "foo" + // shared_by = "bar" + // shared_with = "foo" // namespace = "wavi" // cached = false // 4. SharedKey (cached): "cached:@bar:name.wavi@foo" // name == "name" - // sharedby = "foo" - // sharedwith = "bar" + // shared_by = "foo" + // shared_with = "bar" // namespace = "wavi" // cached = true // 5. SelfKey: "name.wavi@smoothalligator" // name == "name" - // sharedby = "smoothalligator" - // sharedwith = NULL + // shared_by = "smoothalligator" + // shared_with = NULL // namespace = "wavi" // cached = false // more scenarios // 6. No Namespace, SelfKey: // "name@smoothalligator" // name == "name" - // sharedby = "smoothalligator" - // sharedwith = NULL + // shared_by = "smoothalligator" + // shared_with = NULL // namespace = NULL // cached = false int ret = 1; - char *sharedby_withat = NULL; - char *saveptr; - const size_t compositesize = ATCLIENT_ATKEY_COMPOSITE_LEN + 1; - char composite[compositesize]; // holds {key}.{namespace} - memset(composite, 0, sizeof(char) * compositesize); - char *copy = strdup(atkeystr); - if (copy == NULL) { + + /* + * 1. Validate Arguments + */ + // TODO + + /* + * 2. Variables + */ + + char *shared_by_with_at = NULL; + char *save_ptr; + + const size_t composite_size = ATCLIENT_ATKEY_COMPOSITE_LEN + 1; + char composite[composite_size]; // holds {key}.{namespace} + memset(composite, 0, sizeof(char) * composite_size); + + char *copy = NULL; + + /* + * 3. + */ + if ((copy = strdup(atkeystr)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdndup failed\n"); goto exit; } - char *token = strtok_r(copy, ":", &saveptr); + + char *token = strtok_r(copy, ":", &save_ptr); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tokens[0] is NULL\n"); ret = 1; @@ -170,8 +187,8 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { size_t tokenlen = strlen(token); // check if cached if (strncmp(token, "cached", strlen("cached")) == 0) { - atclient_atkey_metadata_set_iscached(&(atkey->metadata), true); - token = strtok_r(NULL, ":", &saveptr); + atclient_atkey_metadata_set_is_cached(&(atkey->metadata), true); + token = strtok_r(NULL, ":", &save_ptr); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tokens[1] is NULL\n"); ret = 1; @@ -181,9 +198,9 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { if (atclient_stringutils_starts_with(token, "public")) { // it is a public key - atclient_atkey_metadata_set_ispublic(&(atkey->metadata), true); + atclient_atkey_metadata_set_is_public(&(atkey->metadata), true); // shift tokens array to the left - token = strtok_r(NULL, ":", &saveptr); + token = strtok_r(NULL, ":", &save_ptr); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; @@ -191,12 +208,12 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } } else if (atclient_stringutils_starts_with(token, "@")) { // it is a shared key - // set sharedwith - if ((ret = atclient_atkey_set_sharedwith(atkey, token)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + // set shared_with + if ((ret = atclient_atkey_set_shared_with(atkey, token)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } - token = strtok_r(NULL, ":", &saveptr); + token = strtok_r(NULL, ":", &save_ptr); if (token == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); @@ -209,7 +226,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } // set atkey->name - token = strtok_r(token, "@", &saveptr); + token = strtok_r(token, "@", &save_ptr); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; @@ -234,15 +251,15 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { goto exit; } - char *namespacestr = strtok_r(NULL, "", &saveptr2); - if (namespacestr == NULL) { + char *namespace_str = strtok_r(NULL, "", &saveptr2); + if (namespace_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. %s atkey is probably incomplete\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespace_str is NULL. %s atkey is probably incomplete\n", atkeystr); goto exit; } - if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(atkey, namespace_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } } else { // there is no namespace @@ -252,8 +269,8 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } } - // set atkey->sharedby - token = strtok_r(NULL, "", &saveptr); + // set atkey->shared_by + token = strtok_r(NULL, "", &save_ptr); if (token == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); ret = 1; @@ -261,13 +278,13 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } tokenlen = strlen(token); - if ((ret = atclient_stringutils_atsign_with_at(token, &sharedby_withat)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(token, &shared_by_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedby(atkey, sharedby_withat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(atkey, shared_by_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -275,7 +292,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { goto exit; exit: { free(copy); - free(sharedby_withat); + free(shared_by_with_at); return ret; } } @@ -295,16 +312,16 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { return ret; } - atclient_atkey_type atkeytype = atclient_atkey_get_type(atkey); + atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); - if (atkeytype == ATCLIENT_ATKEY_TYPE_UNKNOWN) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkeytype is unknown\n"); + if (atkey_type == ATCLIENT_ATKEY_TYPE_UNKNOWN) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is unknown\n"); return ret; } - if (!atclient_atkey_is_sharedby_initialized(atkey) || strlen(atkey->sharedby) <= 0) { + if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->shared_by) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey->sharedby is not initialized or strlen(atkey->sharedby) <= 0. AtKey is incomplete.\n"); + "atkey->shared_by is not initialized or strlen(atkey->shared_by) <= 0. AtKey is incomplete.\n"); return ret; } @@ -315,52 +332,52 @@ int atclient_atkey_to_string(const atclient_atkey *atkey, char **atkeystr) { } size_t index_pos = 0; - const size_t atkeystrsize = atclient_atkey_strlen(atkey) + 1; - if ((*atkeystr = (char *)malloc(sizeof(char) * atkeystrsize)) == NULL) { + const size_t atkey_str_size = atclient_atkey_strlen(atkey) + 1; + if ((*atkeystr = (char *)malloc(sizeof(char) * atkey_str_size)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto error_exit; } - memset(*atkeystr, 0, sizeof(char) * atkeystrsize); + memset(*atkeystr, 0, sizeof(char) * atkey_str_size); - if (atclient_atkey_metadata_is_iscached_initialized(&(atkey->metadata)) && atkey->metadata.iscached) { - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "cached:"); + if (atclient_atkey_metadata_is_is_cached_initialized(&(atkey->metadata)) && atkey->metadata.is_cached) { + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "cached:"); index_pos += strlen("cached:"); } - if (atkeytype == ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - if (!atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) || !atkey->metadata.ispublic) { + if (atkey_type == ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + if (!atclient_atkey_metadata_is_is_public_initialized(&(atkey->metadata)) || !atkey->metadata.is_public) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "atkey's metadata ispublic is either not initialized or false, even though it was deemed a " - "ATCLIENT_ATKEY_TYPE_PUBLICKEY by atclient_atkey_get_type\n"); + "atkey's metadata is_public is either not initialized or false, even though it was deemed a " + "ATCLIENT_ATKEY_TYPE_PUBLIC_KEY by atclient_atkey_get_type\n"); } - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "public:"); + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "public:"); index_pos += strlen("public:"); - } else if (atkeytype == ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - if (!atclient_atkey_is_sharedwith_initialized(atkey)) { + } 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 sharedwith is not initialized, even though it was deemed a ATCLIENT_ATKEY_TYPE_SHAREDKEY by " + "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, atkeystrsize - index_pos, "%s:", atkey->sharedwith); - index_pos += strlen(atkey->sharedwith) + strlen(":"); + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "%s:", atkey->shared_with); + index_pos += strlen(atkey->shared_with) + strlen(":"); } - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s", atkey->key); + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "%s", atkey->key); index_pos += strlen(atkey->key); - if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespacestr) > 0) { - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, ".%s", atkey->namespacestr); - index_pos += strlen(".") + strlen(atkey->namespacestr); + if (atclient_atkey_is_namespacestr_initialized(atkey) && strlen(atkey->namespace_str) > 0) { + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, ".%s", atkey->namespace_str); + index_pos += strlen(".") + strlen(atkey->namespace_str); } - snprintf(*atkeystr + index_pos, atkeystrsize - index_pos, "%s", atkey->sharedby); - index_pos += strlen(atkey->sharedby); + snprintf(*atkeystr + index_pos, atkey_str_size - index_pos, "%s", atkey->shared_by); + index_pos += strlen(atkey->shared_by); - if (index_pos != atkeystrsize - 1) { + if (index_pos != atkey_str_size - 1) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "index_pos != atkeystrsize - 1 (%d != %d - 1). The predicted `atkeystrsize` variable was not " + "index_pos != atkey_str_size - 1 (%d != %d - 1). The predicted `atkey_str_size` variable was not " "evaluated correctly.\n", - index_pos, atkeystrsize); + index_pos, atkey_str_size); } ret = 0; @@ -373,19 +390,19 @@ exit: { return ret; } } bool atclient_atkey_is_key_initialized(const atclient_atkey *atkey) { - return (atkey->_initializedfields[ATKEY_KEY_INDEX] & ATKEY_KEY_INITIALIZED); + return (atkey->_initialized_fields[ATCLIENT_ATKEY_KEY_INDEX] & ATCLIENT_ATKEY_KEY_INITIALIZED); } bool atclient_atkey_is_namespacestr_initialized(const atclient_atkey *atkey) { - return (atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] & ATKEY_NAMESPACESTR_INITIALIZED); + return (atkey->_initialized_fields[ATCLIENT_ATKEY_NAMESPACE_STR_INDEX] & ATCLIENT_ATKEY_NAMESPACE_STR_INITIALIZED); } -bool atclient_atkey_is_sharedby_initialized(const atclient_atkey *atkey) { - return (atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] & ATKEY_SHAREDBY_INITIALIZED); +bool atclient_atkey_is_shared_by_initialized(const atclient_atkey *atkey) { + return (atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_BY_INDEX] & ATCLIENT_ATKEY_SHARED_BY_INITIALIZED); } -bool atclient_atkey_is_sharedwith_initialized(const atclient_atkey *atkey) { - return (atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] & ATKEY_SHAREDWITH_INITIALIZED); +bool atclient_atkey_is_shared_with_initialized(const atclient_atkey *atkey) { + return (atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_WITH_INDEX] & ATCLIENT_ATKEY_SHARED_WITH_INITIALIZED); } int atclient_atkey_set_key(atclient_atkey *atkey, const char *key) { @@ -403,7 +420,7 @@ int atclient_atkey_set_key(atclient_atkey *atkey, const char *key) { if (atclient_atkey_is_key_initialized(atkey)) { free(atkey->key); atkey->key = NULL; - atkey->_initializedfields[ATKEY_KEY_INDEX] &= ~ATKEY_KEY_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_KEY_INDEX] &= ~ATCLIENT_ATKEY_KEY_INITIALIZED; } atkey->key = strdup(key); if (atkey->key == NULL) { @@ -411,94 +428,94 @@ int atclient_atkey_set_key(atclient_atkey *atkey, const char *key) { ret = 1; goto exit; } - atkey->_initializedfields[ATKEY_KEY_INDEX] |= ATKEY_KEY_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_KEY_INDEX] |= ATCLIENT_ATKEY_KEY_INITIALIZED; ret = 0; goto exit; exit: { return ret; } } -int atclient_atkey_set_namespacestr(atclient_atkey *atkey, const char *namespacestr) { +int atclient_atkey_set_namespace_str(atclient_atkey *atkey, const char *namespace_str) { int ret = 1; if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - if (namespacestr == NULL) { + if (namespace_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespace_str is NULL. This is a required argument.\n"); goto exit; } if (atclient_atkey_is_namespacestr_initialized(atkey)) { - free(atkey->namespacestr); - atkey->namespacestr = NULL; - atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] &= ~ATKEY_NAMESPACESTR_INITIALIZED; + free(atkey->namespace_str); + atkey->namespace_str = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_NAMESPACE_STR_INDEX] &= ~ATCLIENT_ATKEY_NAMESPACE_STR_INITIALIZED; } - atkey->namespacestr = strdup(namespacestr); - if (atkey->namespacestr == NULL) { + atkey->namespace_str = strdup(namespace_str); + if (atkey->namespace_str == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); ret = 1; goto exit; } - atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] |= ATKEY_NAMESPACESTR_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_NAMESPACE_STR_INDEX] |= ATCLIENT_ATKEY_NAMESPACE_STR_INITIALIZED; ret = 0; goto exit; exit: { return ret; } } -int atclient_atkey_set_sharedby(atclient_atkey *atkey, const char *sharedby) { +int atclient_atkey_set_shared_by(atclient_atkey *atkey, const char *shared_by) { int ret = 1; if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - if (sharedby == NULL) { + if (shared_by == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is NULL. This is a required argument.\n"); goto exit; } - if (atclient_atkey_is_sharedby_initialized(atkey)) { - free(atkey->sharedby); - atkey->sharedby = NULL; - atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] &= ~ATKEY_SHAREDBY_INITIALIZED; + if (atclient_atkey_is_shared_by_initialized(atkey)) { + free(atkey->shared_by); + atkey->shared_by = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_BY_INDEX] &= ~ATCLIENT_ATKEY_SHARED_BY_INITIALIZED; } - atkey->sharedby = strdup(sharedby); - if (atkey->sharedby == NULL) { + atkey->shared_by = strdup(shared_by); + if (atkey->shared_by == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); ret = 1; goto exit; } - atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] |= ATKEY_SHAREDBY_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_BY_INDEX] |= ATCLIENT_ATKEY_SHARED_BY_INITIALIZED; ret = 0; goto exit; exit: { return ret; } } -int atclient_atkey_set_sharedwith(atclient_atkey *atkey, const char *sharedwith) { +int atclient_atkey_set_shared_with(atclient_atkey *atkey, const char *shared_with) { int ret = 1; if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL. This is a required argument.\n"); goto exit; } - if (sharedwith == NULL) { + if (shared_with == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is NULL. This is a required argument.\n"); goto exit; } - if (atclient_atkey_is_sharedwith_initialized(atkey)) { - free(atkey->sharedwith); - atkey->sharedwith = NULL; - atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] &= ~ATKEY_SHAREDWITH_INITIALIZED; + if (atclient_atkey_is_shared_with_initialized(atkey)) { + free(atkey->shared_with); + atkey->shared_with = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_WITH_INDEX] &= ~ATCLIENT_ATKEY_SHARED_WITH_INITIALIZED; } - atkey->sharedwith = strdup(sharedwith); - if (atkey->sharedwith == NULL) { + atkey->shared_with = strdup(shared_with); + if (atkey->shared_with == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "strdup failed\n"); ret = 1; goto exit; } - atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] |= ATKEY_SHAREDWITH_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_WITH_INDEX] |= ATCLIENT_ATKEY_SHARED_WITH_INITIALIZED; ret = 0; goto exit; exit: { return ret; } @@ -508,54 +525,54 @@ void atclient_atkey_unset_key(atclient_atkey *atkey) { if (atclient_atkey_is_key_initialized(atkey)) { free(atkey->key); atkey->key = NULL; - atkey->_initializedfields[ATKEY_KEY_INDEX] &= ~ATKEY_KEY_INITIALIZED; + atkey->_initialized_fields[ATCLIENT_ATKEY_KEY_INDEX] &= ~ATCLIENT_ATKEY_KEY_INITIALIZED; } } -void atclient_atkey_unset_namespacestr(atclient_atkey *atkey) { +void atclient_atkey_unset_namespace_str(atclient_atkey *atkey) { if (atclient_atkey_is_namespacestr_initialized(atkey)) { - free(atkey->namespacestr); - atkey->namespacestr = NULL; - atkey->_initializedfields[ATKEY_NAMESPACESTR_INDEX] &= ~ATKEY_NAMESPACESTR_INITIALIZED; + free(atkey->namespace_str); + atkey->namespace_str = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_NAMESPACE_STR_INDEX] &= ~ATCLIENT_ATKEY_NAMESPACE_STR_INITIALIZED; } } -void atclient_atkey_unset_sharedby(atclient_atkey *atkey) { - if (atclient_atkey_is_sharedby_initialized(atkey)) { - free(atkey->sharedby); - atkey->sharedby = NULL; - atkey->_initializedfields[ATKEY_SHAREDBY_INDEX] &= ~ATKEY_SHAREDBY_INITIALIZED; +void atclient_atkey_unset_shared_by(atclient_atkey *atkey) { + if (atclient_atkey_is_shared_by_initialized(atkey)) { + free(atkey->shared_by); + atkey->shared_by = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_BY_INDEX] &= ~ATCLIENT_ATKEY_SHARED_BY_INITIALIZED; } } -void atclient_atkey_unset_sharedwith(atclient_atkey *atkey) { - if (atclient_atkey_is_sharedwith_initialized(atkey)) { - free(atkey->sharedwith); - atkey->sharedwith = NULL; - atkey->_initializedfields[ATKEY_SHAREDWITH_INDEX] &= ~ATKEY_SHAREDWITH_INITIALIZED; +void atclient_atkey_unset_shared_with(atclient_atkey *atkey) { + if (atclient_atkey_is_shared_with_initialized(atkey)) { + free(atkey->shared_with); + atkey->shared_with = NULL; + atkey->_initialized_fields[ATCLIENT_ATKEY_SHARED_WITH_INDEX] &= ~ATCLIENT_ATKEY_SHARED_WITH_INITIALIZED; } } atclient_atkey_type atclient_atkey_get_type(const atclient_atkey *atkey) { - if (atclient_atkey_metadata_is_ispublic_initialized(&(atkey->metadata)) && atkey->metadata.ispublic && - !atclient_atkey_is_sharedwith_initialized(atkey)) { - return ATCLIENT_ATKEY_TYPE_PUBLICKEY; + if (atclient_atkey_metadata_is_is_public_initialized(&(atkey->metadata)) && atkey->metadata.is_public && + !atclient_atkey_is_shared_with_initialized(atkey)) { + return ATCLIENT_ATKEY_TYPE_PUBLIC_KEY; } - if (atclient_atkey_is_sharedby_initialized(atkey) && atclient_atkey_is_sharedwith_initialized(atkey)) { - if (strcmp(atkey->sharedby, atkey->sharedwith) == 0) { // special case - return ATCLIENT_ATKEY_TYPE_SELFKEY; + if (atclient_atkey_is_shared_by_initialized(atkey) && atclient_atkey_is_shared_with_initialized(atkey)) { + if (strcmp(atkey->shared_by, atkey->shared_with) == 0) { // special case + return ATCLIENT_ATKEY_TYPE_SELF_KEY; } - return ATCLIENT_ATKEY_TYPE_SHAREDKEY; + return ATCLIENT_ATKEY_TYPE_SHARED_KEY; } - if (atclient_atkey_is_sharedby_initialized(atkey) && !atclient_atkey_is_sharedwith_initialized(atkey)) { - return ATCLIENT_ATKEY_TYPE_SELFKEY; + if (atclient_atkey_is_shared_by_initialized(atkey) && !atclient_atkey_is_shared_with_initialized(atkey)) { + return ATCLIENT_ATKEY_TYPE_SELF_KEY; } return ATCLIENT_ATKEY_TYPE_UNKNOWN; } -int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *key, const char *sharedby, - const char *namespacestr) { +int atclient_atkey_create_public_key(atclient_atkey *atkey, const char *key, const char *shared_by, + const char *namespace_str) { int ret = 1; if (atkey == NULL) { @@ -573,32 +590,32 @@ int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *key, cons atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is empty. This is a required argument.\n"); goto exit; } - if (sharedby == NULL) { + if (shared_by == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is NULL. This is a required argument.\n"); goto exit; } - if (strlen(sharedby) == 0) { + if (strlen(shared_by) == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is empty. This is a required argument.\n"); goto exit; } - atclient_atkey_metadata_set_ispublic(&(atkey->metadata), true); + atclient_atkey_metadata_set_is_public(&(atkey->metadata), true); if ((ret = atclient_atkey_set_key(atkey, key)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_key failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(atkey, shared_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if (namespacestr != NULL) { - if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if (namespace_str != NULL) { + if ((ret = atclient_atkey_set_namespace_str(atkey, namespace_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } } @@ -608,8 +625,8 @@ int atclient_atkey_create_publickey(atclient_atkey *atkey, const char *key, cons exit: { return ret; } } -int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *key, const char *sharedby, - const char *namespacestr) { +int atclient_atkey_create_self_key(atclient_atkey *atkey, const char *key, const char *shared_by, + const char *namespace_str) { int ret = 1; if (atkey == NULL) { @@ -630,15 +647,15 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *key, const goto exit; } - if (sharedby == NULL) { + if (shared_by == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is NULL. This is a required argument.\n"); goto exit; } - if (strlen(sharedby) <= 0) { + if (strlen(shared_by) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is empty. This is a required argument.\n"); goto exit; } @@ -647,15 +664,15 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *key, const goto exit; } - if (namespacestr != NULL && strlen(namespacestr) > 0) { - if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if (namespace_str != NULL && strlen(namespace_str) > 0) { + if ((ret = atclient_atkey_set_namespace_str(atkey, namespace_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } } - if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(atkey, shared_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -664,8 +681,8 @@ int atclient_atkey_create_selfkey(atclient_atkey *atkey, const char *key, const exit: { return ret; } } -int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *key, const char *sharedby, - const char *sharedwith, const char *namespacestr) { +int atclient_atkey_create_shared_key(atclient_atkey *atkey, const char *key, const char *shared_by, + const char *shared_with, const char *namespace_str) { int ret = 1; if (atkey == NULL) { @@ -686,32 +703,32 @@ int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *key, cons goto exit; } - if (sharedby == NULL) { + if (shared_by == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is NULL. This is a required argument.\n"); goto exit; } - if (strlen(sharedby) <= 0) { + if (strlen(shared_by) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is empty. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is empty. This is a required argument.\n"); goto exit; } - if (sharedwith == NULL) { + if (shared_with == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is NULL. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is NULL. This is a required argument.\n"); goto exit; } - if (strlen(sharedwith) <= 0) { + if (strlen(shared_with) <= 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is empty. This is a required argument.\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is empty. This is a required argument.\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedwith(atkey, sharedwith)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if ((ret = atclient_atkey_set_shared_with(atkey, shared_with)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } @@ -720,15 +737,15 @@ int atclient_atkey_create_sharedkey(atclient_atkey *atkey, const char *key, cons goto exit; } - if (namespacestr != NULL && strlen(namespacestr) > 0) { - if ((ret = atclient_atkey_set_namespacestr(atkey, namespacestr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if (namespace_str != NULL && strlen(namespace_str) > 0) { + if ((ret = atclient_atkey_set_namespace_str(atkey, namespace_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } } - if ((ret = atclient_atkey_set_sharedby(atkey, sharedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(atkey, shared_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 3d504d30..15dca08e 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -11,52 +11,52 @@ #define TAG "atkeys" -static bool is_pkampublickeybase64_initialized(atclient_atkeys *atkeys); -static bool is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys); -static bool is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys); -static bool is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys); -static bool is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys); - -static void set_pkampublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); -static void set_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); -static void set_encryptpublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); -static void set_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); -static void set_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys, const bool initialized); - -static void unset_pkampublickeybase64(atclient_atkeys *atkeys); -static void unset_pkamprivatekeybase64(atclient_atkeys *atkeys); -static void unset_encryptpublickeybase64(atclient_atkeys *atkeys); -static void unset_encryptprivatekeybase64(atclient_atkeys *atkeys); -static void unset_selfencryptionkeybase64(atclient_atkeys *atkeys); - -static int set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, - const size_t pkampublickeylen); -static int set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, - const size_t pkamprivatekeylen); -static int set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, - const size_t encryptpublickeylen); -static int set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, - const size_t encryptprivatekeylen); -static int set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, - const size_t selfencryptionkeylen); +static bool is_pkam_public_key_base64_initialized(atclient_atkeys *atkeys); +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 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 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 int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, + 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); +static int set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, + 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); +static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *self_encryption_key_base64, + const size_t self_encryption_key_len); void atclient_atkeys_init(atclient_atkeys *atkeys) { memset(atkeys, 0, sizeof(atclient_atkeys)); - atchops_rsakey_publickey_init(&(atkeys->pkampublickey)); - atchops_rsakey_privatekey_init(&(atkeys->pkamprivatekey)); - atchops_rsakey_publickey_init(&(atkeys->encryptpublickey)); - atchops_rsakey_privatekey_init(&(atkeys->encryptprivatekey)); + atchops_rsakey_publickey_init(&(atkeys->pkam_public_key)); + atchops_rsakey_privatekey_init(&(atkeys->pkam_private_key)); + atchops_rsakey_publickey_init(&(atkeys->encrypt_public_key)); + atchops_rsakey_privatekey_init(&(atkeys->encrypt_private_key)); } void atclient_atkeys_free(atclient_atkeys *atkeys) { - atchops_rsakey_publickey_free(&(atkeys->pkampublickey)); - atchops_rsakey_privatekey_free(&(atkeys->pkamprivatekey)); - atchops_rsakey_publickey_free(&(atkeys->encryptpublickey)); - atchops_rsakey_privatekey_free(&(atkeys->encryptprivatekey)); + atchops_rsakey_publickey_free(&(atkeys->pkam_public_key)); + atchops_rsakey_privatekey_free(&(atkeys->pkam_private_key)); + atchops_rsakey_publickey_free(&(atkeys->encrypt_public_key)); + atchops_rsakey_privatekey_free(&(atkeys->encrypt_private_key)); } -int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, - const size_t pkampublickeybase64len) { +int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, + const size_t pkam_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -65,20 +65,20 @@ int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char return ret; } - if (pkampublickeybase64 == NULL) { + if (pkam_publickey_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_publickey_base64 is NULL\n"); return ret; } - if (pkampublickeybase64len == 0) { + if (pkam_public_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_public_key_base64_len is 0\n"); return ret; } - if ((ret = set_pkampublickeybase64(atkeys, pkampublickeybase64, pkampublickeybase64len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeybase64\n", + if ((ret = set_pkam_public_key_base64(atkeys, pkam_publickey_base64, pkam_public_key_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkam_public_key_base64: %d | failed to set pkam_publickey_base64\n", ret); goto exit; } @@ -88,8 +88,8 @@ int atclient_atkeys_set_pkampublickeybase64(atclient_atkeys *atkeys, const char exit: { return ret; } } -int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, - const size_t pkamprivatekeybase64len) { +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) { int ret = 1; if (atkeys == NULL) { @@ -98,21 +98,21 @@ int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char return ret; } - if (pkamprivatekeybase64 == NULL) { + if (pkam_private_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_private_key_base64 is NULL\n"); return ret; } - if (pkamprivatekeybase64len == 0) { + if (pkam_private_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_private_key_base64_len is 0\n"); return ret; } - if ((ret = set_pkamprivatekeybase64(atkeys, pkamprivatekeybase64, pkamprivatekeybase64len)) != 0) { + if ((ret = set_pkam_private_key_base64(atkeys, pkam_private_key_base64, pkam_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeybase64\n", ret); + "set_pkam_private_key_base64: %d | failed to set pkam_private_key_base64\n", ret); goto exit; } @@ -121,8 +121,8 @@ int atclient_atkeys_set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char exit: { return ret; } } -int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, - const size_t encryptpublickeybase64len) { +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) { int ret = 1; if (atkeys == NULL) { @@ -131,21 +131,21 @@ int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const ch return ret; } - if (encryptpublickeybase64 == NULL) { + if (encrypt_public_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_public_key_base64 is NULL\n"); return ret; } - if (encryptpublickeybase64len == 0) { + if (encrypt_public_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_public_key_base64_len is 0\n"); return ret; } - if ((ret = set_encryptpublickeybase64(atkeys, encryptpublickeybase64, encryptpublickeybase64len)) != 0) { + if ((ret = set_encrypt_public_key_base64(atkeys, encrypt_public_key_base64, encrypt_public_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_encryptpublickeybase64: %d | failed to set encryptpublickeybase64\n", ret); + "set_encrypt_public_key_base64: %d | failed to set encrypt_public_key_base64\n", ret); goto exit; } @@ -154,8 +154,8 @@ int atclient_atkeys_set_encryptpublickeybase64(atclient_atkeys *atkeys, const ch exit: { return ret; } } -int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, - const size_t encryptprivatekeybase64len) { +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) { int ret = 1; if (atkeys == NULL) { @@ -164,21 +164,21 @@ int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const c return ret; } - if (encryptprivatekeybase64 == NULL) { + if (encrypt_private_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_private_key_base64 is NULL\n"); return ret; } - if (encryptprivatekeybase64len == 0) { + if (encrypt_private_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_private_key_base64_len is 0\n"); return ret; } - if ((ret = set_encryptprivatekeybase64(atkeys, encryptprivatekeybase64, encryptprivatekeybase64len)) != 0) { + if ((ret = set_encrypt_private_key_base64(atkeys, encrypt_private_key_base64, encrypt_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeybase64\n", ret); + "set_encrypt_private_key_base64: %d | failed to set encrypt_private_key_base64\n", ret); goto exit; } @@ -187,8 +187,8 @@ int atclient_atkeys_set_encryptprivatekeybase64(atclient_atkeys *atkeys, const c exit: { return ret; } } -int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, - const size_t selfencryptionkeybase64len) { +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) { int ret = 1; if (atkeys == NULL) { @@ -197,21 +197,21 @@ int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const c return ret; } - if (selfencryptionkeybase64 == NULL) { + if (self_encryption_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_base64 is NULL\n"); return ret; } - if (selfencryptionkeybase64len == 0) { + if (self_encryption_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_base64_len is 0\n"); return ret; } - if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeybase64, selfencryptionkeybase64len)) != 0) { + if ((ret = set_self_encryption_key_base64(atkeys, self_encryption_key_base64, self_encryption_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeybase64\n", ret); + "set_self_encryption_key_base64: %d | failed to set self_encryption_key_base64\n", ret); goto exit; } @@ -220,8 +220,8 @@ int atclient_atkeys_set_selfencryptionkeybase64(atclient_atkeys *atkeys, const c exit: { return ret; } } -int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char *pkampublickeybase64, - const size_t pkampublickeybase64len) { +int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_publickey_base64, + const size_t pkam_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -230,22 +230,22 @@ int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char * return ret; } - if (pkampublickeybase64 == NULL) { + if (pkam_publickey_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_publickey_base64 is NULL\n"); return ret; } - if (pkampublickeybase64len == 0) { + if (pkam_public_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkampublickeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_public_key_base64_len is 0\n"); return ret; } - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkampublickey), pkampublickeybase64, pkampublickeybase64)) != + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkam_public_key), pkam_publickey_base64, pkam_publickey_base64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate pkampublickey\n", ret); + "atchops_rsakey_populate_publickey: %d | failed to populate pkam_public_key\n", ret); goto exit; } ret = 0; @@ -253,8 +253,8 @@ int atclient_atkeys_populate_pkampublickey(atclient_atkeys *atkeys, const char * exit: { return ret; } } -int atclient_atkeys_populate_pkamprivatekey(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, - const size_t pkamprivatekeybase64len) { +int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const char *pkam_private_key_base64, + const size_t pkam_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -263,29 +263,29 @@ int atclient_atkeys_populate_pkamprivatekey(atclient_atkeys *atkeys, const char return ret; } - if (pkamprivatekeybase64 == NULL) { + if (pkam_private_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_private_key_base64 is NULL\n"); return ret; } - if (pkamprivatekeybase64len == 0) { + if (pkam_private_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkamprivatekeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_private_key_base64_len is 0\n"); return ret; } - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkamprivatekey), pkamprivatekeybase64, - pkamprivatekeybase64len)) != 0) { + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkam_private_key), pkam_private_key_base64, + pkam_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate pkamprivatekey\n", ret); + "atchops_rsakey_populate_privatekey: %d | failed to populate pkam_private_key\n", ret); goto exit; } exit: { return ret; } } -int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const char *encryptpublickeybase64, - const size_t encryptpublickeybase64len) { +int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, + const size_t encrypt_public_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -294,22 +294,22 @@ int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const cha return ret; } - if (encryptpublickeybase64 == NULL) { + if (encrypt_public_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_public_key_base64 is NULL\n"); return ret; } - if (encryptpublickeybase64len == 0) { + if (encrypt_public_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptpublickeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_public_key_base64_len is 0\n"); return ret; } - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encryptpublickey), encryptpublickeybase64, - encryptpublickeybase64len)) != 0) { + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encrypt_public_key), encrypt_public_key_base64, + encrypt_public_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate encryptpublickey\n", ret); + "atchops_rsakey_populate_publickey: %d | failed to populate encrypt_public_key\n", ret); goto exit; } @@ -318,8 +318,8 @@ int atclient_atkeys_populate_encryptpublickey(atclient_atkeys *atkeys, const cha exit: { return ret; } } -int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, - const size_t encryptprivatekeybase64len) { +int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, + const size_t encrypt_private_key_base64_len) { int ret = 1; if (atkeys == NULL) { @@ -328,22 +328,22 @@ int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const ch return ret; } - if (encryptprivatekeybase64 == NULL) { + if (encrypt_private_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_private_key_base64 is NULL\n"); return ret; } - if (encryptprivatekeybase64len == 0) { + if (encrypt_private_key_base64_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encryptprivatekeybase64len is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encrypt_private_key_base64_len is 0\n"); return ret; } - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encryptprivatekey), encryptprivatekeybase64, - encryptprivatekeybase64len)) != 0) { + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encrypt_private_key), encrypt_private_key_base64, + encrypt_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate encryptprivatekey\n", ret); + "atchops_rsakey_populate_privatekey: %d | failed to populate encrypt_private_key\n", ret); goto exit; } @@ -352,32 +352,32 @@ int atclient_atkeys_populate_encryptprivatekey(atclient_atkeys *atkeys, const ch exit: { return ret; } } -bool atclient_atkeys_is_pkampublickeybase64_initialized(atclient_atkeys *atkeys) { - return is_pkampublickeybase64_initialized(atkeys); +bool atclient_atkeys_is_pkam_public_key_base64_initialized(atclient_atkeys *atkeys) { + return is_pkam_public_key_base64_initialized(atkeys); } -bool atclient_atkeys_is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return is_pkamprivatekeybase64_initialized(atkeys); +bool atclient_atkeys_is_pkam_private_key_base64_initialized(atclient_atkeys *atkeys) { + return is_pkam_private_key_base64_initialized(atkeys); } -bool atclient_atkeys_is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys) { - return is_encryptpublickeybase64_initialized(atkeys); +bool atclient_atkeys_is_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys) { + return is_encrypt_public_key_base64_initialized(atkeys); } -bool atclient_atkeys_is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return is_encryptprivatekeybase64_initialized(atkeys); +bool atclient_atkeys_is_encrypt_private_key_base64_initialized(atclient_atkeys *atkeys) { + return is_encrypt_private_key_base64_initialized(atkeys); } -bool atclient_atkeys_is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys) { - return is_selfencryptionkeybase64_initialized(atkeys); +bool atclient_atkeys_is_self_encryption_key_base64_initialized(atclient_atkeys *atkeys) { + return is_self_encryption_key_base64_initialized(atkeys); } -int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *aespkampublickeystr, - const size_t aespkampublickeylen, const char *aespkamprivatekeystr, - const size_t aespkamprivatekeylen, const char *aesencryptpublickeystr, - const size_t aesencryptpublickeylen, const char *aesencryptprivatekeystr, - const size_t aesencryptprivatekeylen, const char *selfencryptionkeystr, - const size_t selfencryptionkeystrlen) { +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_private_key_len, const char *self_encryption_key_str, + const size_t self_encryption_key_str_len) { int ret = 1; /* @@ -390,63 +390,63 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a return ret; } - if (aespkampublickeystr == NULL) { + if (aes_pkam_public_key_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkampublickeystr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_public_key_str is NULL\n"); return ret; } - if (aespkamprivatekeystr == NULL) { + if (aes_pkam_private_key_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkamprivatekeystr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_private_key_str is NULL\n"); return ret; } - if (aesencryptpublickeystr == NULL) { + if (aes_encrypt_public_key_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptpublickeystr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_public_key_str is NULL\n"); return ret; } - if (aesencryptprivatekeystr == NULL) { + if (aes_encrypt_private_key_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptprivatekeystr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_private_key_str is NULL\n"); return ret; } - if (selfencryptionkeystr == NULL) { + if (self_encryption_key_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeystr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_str is NULL\n"); return ret; } - if (aespkampublickeylen == 0) { + if (aes_pkam_public_key_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkampublickeylen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_public_key_len is 0\n"); return ret; } - if (aespkamprivatekeylen == 0) { + if (aes_pkam_private_key_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aespkamprivatekeylen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_pkam_private_key_len is 0\n"); return ret; } - if (aesencryptpublickeylen == 0) { + if (aes_encrypt_public_key_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptpublickeylen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_public_key_len is 0\n"); return ret; } - if (aesencryptprivatekeylen == 0) { + if (aes_encrypt_private_key_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aesencryptprivatekeylen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "aes_encrypt_private_key_len is 0\n"); return ret; } - if (selfencryptionkeystrlen == 0) { + if (self_encryption_key_str_len == 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "selfencryptionkeystrlen is 0\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "self_encryption_key_str_len is 0\n"); return ret; } @@ -456,33 +456,34 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a // 2a. Use legacy IV // the atKeys are encrypted with bytes of 0s - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; + memset(iv, 0, sizeof(unsigned char) * iv_size); // holds the base64-decoded non-encrypted self encryption key // to use for decrypting the other RSA keys - const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; - unsigned char selfencryptionkey[selfencryptionkeysize]; - memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); - size_t selfencryptionkeylen = 0; + const size_t self_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char self_encryption_key[self_encryption_key_size]; + memset(self_encryption_key, 0, sizeof(unsigned char) * self_encryption_key_size); + size_t self_encryption_key_len = 0; // temporarily holds the base64-encoded encrypted RSA key for decryption - const size_t rsakeyencryptedsize = 4096; - unsigned char rsakeyencrypted[rsakeyencryptedsize]; - memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); - size_t rsakeyencryptedlen = 0; + const size_t rsa_key_encrypted_size = 4096; + unsigned char rsa_key_encrypted[rsa_key_encrypted_size]; + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + size_t rsa_key_encrypted_len = 0; // temporarily holds the base64-encoded decrypted RSA key - const size_t rsakeydecryptedsize = 4096; - unsigned char rsakeydecrypted[rsakeydecryptedsize]; - memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); - size_t rsakeydecryptedlen = 0; + const size_t rsa_key_decrypted_size = 4096; + unsigned char rsa_key_decrypted[rsa_key_decrypted_size]; + memset(rsa_key_decrypted, 0, sizeof(unsigned char) * rsa_key_decrypted_size); + size_t rsa_key_decrypted_len = 0; /* * 3. Prepare self encryption key for use */ - if ((ret = atchops_base64_decode((unsigned char *)selfencryptionkeystr, selfencryptionkeystrlen, selfencryptionkey, - selfencryptionkeysize, &selfencryptionkeylen)) != 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; } @@ -492,101 +493,101 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a */ // 4a. self encryption key - if ((ret = set_selfencryptionkeybase64(atkeys, selfencryptionkeystr, selfencryptionkeylen)) != 0) { + if ((ret = set_self_encryption_key_base64(atkeys, self_encryption_key_str, self_encryption_key_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_selfencryptionkeybase64: %d | failed to set selfencryptionkeystr\n", ret); + "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 *)aespkampublickeystr, aespkampublickeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen)) != 0) { + 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) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "tried base64 decoding pkam public key: %d\n", ret); goto exit; } - if ((ret = atchops_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { + if ((ret = atchops_aesctr_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_aesctr_decrypt: %d | failed to decrypt pkam public key\n", ret); goto exit; } - if ((ret = set_pkampublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkampublickeybase64: %d | failed to set pkampublickeystr\n", + if ((ret = set_pkam_public_key_base64(atkeys, (const char *)rsa_key_decrypted, rsa_key_decrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkam_public_key_base64: %d | failed to set pkampublickeystr\n", ret); goto exit; } - memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); - memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + memset(rsa_key_decrypted, 0, sizeof(unsigned char) * rsa_key_decrypted_size); + memset(iv, 0, sizeof(unsigned char) * iv_size); // 4c. pkam private key - if ((ret = atchops_base64_decode((unsigned char *)aespkamprivatekeystr, aespkamprivatekeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen)) != 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_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { + if ((ret = atchops_aesctr_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_aesctr_decrypt: %d | failed to decrypt pkam private key\n", ret); goto exit; } - if ((ret = set_pkamprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkamprivatekeybase64: %d | failed to set pkamprivatekeystr\n", + 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); goto exit; } - memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); - memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + memset(rsa_key_decrypted, 0, sizeof(unsigned char) * rsa_key_decrypted_size); + memset(iv, 0, sizeof(unsigned char) * iv_size); // 4d. encrypt public key - if ((ret = atchops_base64_decode((unsigned char *)aesencryptpublickeystr, aesencryptpublickeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen)) != 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_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { + if ((ret = atchops_aesctr_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_aesctr_decrypt: %d | failed to decrypt encrypt public key\n", ret); goto exit; } - if ((ret = set_encryptpublickeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + if ((ret = set_encrypt_public_key_base64(atkeys, (const char *)rsa_key_decrypted, rsa_key_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_encryptpublickeybase64: %d | failed to set encryptpublickeystr\n", ret); + "set_encrypt_public_key_base64: %d | failed to set encryptpublickeystr\n", ret); goto exit; } - memset(rsakeyencrypted, 0, sizeof(unsigned char) * rsakeyencryptedsize); - memset(rsakeydecrypted, 0, sizeof(unsigned char) * rsakeydecryptedsize); - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(rsa_key_encrypted, 0, sizeof(unsigned char) * rsa_key_encrypted_size); + memset(rsa_key_decrypted, 0, sizeof(unsigned char) * rsa_key_decrypted_size); + memset(iv, 0, sizeof(unsigned char) * iv_size); // 4e. encrypt private key - if ((ret = atchops_base64_decode((unsigned char *)aesencryptprivatekeystr, aesencryptprivatekeylen, rsakeyencrypted, - rsakeyencryptedsize, &rsakeyencryptedlen)) != 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_aesctr_decrypt(selfencryptionkey, ATCHOPS_AES_256, iv, rsakeyencrypted, rsakeyencryptedlen, - rsakeydecrypted, rsakeydecryptedsize, &rsakeydecryptedlen)) != 0) { + if ((ret = atchops_aesctr_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_aesctr_decrypt: %d | failed to decrypt encrypt private key\n", ret); goto exit; } - if ((ret = set_encryptprivatekeybase64(atkeys, (const char *)rsakeydecrypted, rsakeydecryptedlen)) != 0) { + if ((ret = set_encrypt_private_key_base64(atkeys, (const char *)rsa_key_decrypted, rsa_key_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "set_encryptprivatekeybase64: %d | failed to set encryptprivatekeystr\n", ret); + "set_encrypt_private_key_base64: %d | failed to set encryptprivatekeystr\n", ret); goto exit; } @@ -595,32 +596,32 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a */ // 5a. pkam public key - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkampublickey), atkeys->pkampublickeybase64, - strlen(atkeys->pkampublickeybase64))) != 0) { + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkam_public_key), atkeys->pkam_publickey_base64, + strlen(atkeys->pkam_publickey_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d | failed to populate pkam public key\n", ret); goto exit; } // 5b. pkam private key - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkamprivatekey), atkeys->pkamprivatekeybase64, - strlen(atkeys->pkamprivatekeybase64))) != 0) { + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkam_private_key), atkeys->pkam_private_key_base64, + strlen(atkeys->pkam_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_privatekey: %d | failed to populate pkam private key\n", ret); goto exit; } // 5c. encrypt public key - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encryptprivatekey), atkeys->encryptprivatekeybase64, - strlen(atkeys->encryptprivatekeybase64))) != 0) { + if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encrypt_private_key), atkeys->encrypt_private_key_base64, + strlen(atkeys->encrypt_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_privatekey: %d | failed to populate encrypt private key\n", ret); goto exit; } // 5d. encrypt private key - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encryptpublickey), atkeys->encryptpublickeybase64, - strlen(atkeys->encryptpublickeybase64))) != 0) { + if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encrypt_public_key), atkeys->encrypt_public_key_base64, + strlen(atkeys->encrypt_public_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d | failed to populate encrypt public key\n", ret); goto exit; @@ -636,11 +637,11 @@ int atclient_atkeys_populate_from_atkeysfile(atclient_atkeys *atkeys, const atcl int ret = 1; ret = atclient_atkeys_populate_from_strings( - atkeys, atkeysfile.aespkampublickeystr, strlen(atkeysfile.aespkampublickeystr), atkeysfile.aespkamprivatekeystr, - strlen(atkeysfile.aespkamprivatekeystr), atkeysfile.aesencryptpublickeystr, - strlen(atkeysfile.aesencryptpublickeystr), atkeysfile.aesencryptprivatekeystr, - strlen(atkeysfile.aesencryptprivatekeystr), atkeysfile.selfencryptionkeystr, - strlen(atkeysfile.selfencryptionkeystr)); + 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); @@ -679,127 +680,127 @@ exit: { } } -static bool is_pkampublickeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] & ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; +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; } -static bool is_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] & ATCLIENT_ATKEYS_PKAMPRIVATEKEY_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; } -static bool is_encryptpublickeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_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; } -static bool is_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] & ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_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; } -static bool is_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys) { - return atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] & ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_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; } -static void set_pkampublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { +static void set_pkam_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] |= ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INDEX] |= ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED; } else { - atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPUBLICKEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAMPUBLICKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAM_PUBLIC_KEY_INITIALIZED; } } -static void set_pkamprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { +static void set_pkam_private_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] |= ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] |= ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; } else { - atkeys->_initializedfields[ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAMPRIVATEKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INDEX] &= ~ATCLIENT_ATKEYS_PKAM_PRIVATE_KEY_INITIALIZED; } } -static void set_encryptpublickeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { +static void set_encrypt_public_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; } else { - atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPTPUBLICKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPT_PUBLIC_KEY_INITIALIZED; } } -static void set_encryptprivatekeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { +static void set_encrypt_privatekey_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] |= ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; } else { - atkeys->_initializedfields[ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPTPRIVATEKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INDEX] &= ~ATCLIENT_ATKEYS_ENCRYPT_PRIVATE_KEY_INITIALIZED; } } -static void set_selfencryptionkeybase64_initialized(atclient_atkeys *atkeys, const bool initialized) { +static void set_self_encryption_key_base64_initialized(atclient_atkeys *atkeys, const bool initialized) { if (initialized) { - atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] |= ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] |= ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; } else { - atkeys->_initializedfields[ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INDEX] &= ~ATCLIENT_ATKEYS_SELFENCRYPTIONKEY_INITIALIZED; + atkeys->_initialized_fields[ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INDEX] &= ~ATCLIENT_ATKEYS_SELF_ENCRYPTION_KEY_INITIALIZED; } } -static void unset_pkampublickeybase64(atclient_atkeys *atkeys) { - if (is_pkampublickeybase64_initialized(atkeys)) { - free(atkeys->pkampublickeybase64); +static void unset_pkam_public_key_base64(atclient_atkeys *atkeys) { + if (is_pkam_public_key_base64_initialized(atkeys)) { + free(atkeys->pkam_publickey_base64); } - atkeys->pkampublickeybase64 = NULL; - set_pkampublickeybase64_initialized(atkeys, false); + atkeys->pkam_publickey_base64 = NULL; + set_pkam_public_key_base64_initialized(atkeys, false); } -static void unset_pkamprivatekeybase64(atclient_atkeys *atkeys) { - if (is_pkamprivatekeybase64_initialized(atkeys)) { - free(atkeys->pkamprivatekeybase64); +static void unset_pkam_private_key_base64(atclient_atkeys *atkeys) { + if (is_pkam_private_key_base64_initialized(atkeys)) { + free(atkeys->pkam_private_key_base64); } - atkeys->pkamprivatekeybase64 = NULL; - set_pkamprivatekeybase64_initialized(atkeys, false); + atkeys->pkam_private_key_base64 = NULL; + set_pkam_private_key_base64_initialized(atkeys, false); } -static void unset_encryptpublickeybase64(atclient_atkeys *atkeys) { - if (is_encryptpublickeybase64_initialized(atkeys)) { - free(atkeys->encryptpublickeybase64); +static void unset_encrypt_public_key_base64(atclient_atkeys *atkeys) { + if (is_encrypt_public_key_base64_initialized(atkeys)) { + free(atkeys->encrypt_public_key_base64); } - atkeys->encryptpublickeybase64 = NULL; - set_encryptpublickeybase64_initialized(atkeys, false); + atkeys->encrypt_public_key_base64 = NULL; + set_encrypt_public_key_base64_initialized(atkeys, false); } -static void unset_encryptprivatekeybase64(atclient_atkeys *atkeys) { - if (is_encryptprivatekeybase64_initialized(atkeys)) { - free(atkeys->encryptprivatekeybase64); +static void unset_encrypt_private_key_base64(atclient_atkeys *atkeys) { + if (is_encrypt_private_key_base64_initialized(atkeys)) { + free(atkeys->encrypt_private_key_base64); } - atkeys->encryptprivatekeybase64 = NULL; - set_encryptprivatekeybase64_initialized(atkeys, false); + atkeys->encrypt_private_key_base64 = NULL; + set_encrypt_privatekey_base64_initialized(atkeys, false); } -static void unset_selfencryptionkeybase64(atclient_atkeys *atkeys) { - if (is_selfencryptionkeybase64_initialized(atkeys)) { - free(atkeys->selfencryptionkeybase64); +static void unset_self_encryption_key_base64(atclient_atkeys *atkeys) { + if (is_self_encryption_key_base64_initialized(atkeys)) { + free(atkeys->self_encryption_key_base64); } - atkeys->selfencryptionkeybase64 = NULL; - set_selfencryptionkeybase64_initialized(atkeys, false); + atkeys->self_encryption_key_base64 = NULL; + set_self_encryption_key_base64_initialized(atkeys, false); } -static int set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampublickeybase64, - const size_t pkampublickeylen) { +static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, + const size_t pkam_public_key_len) { int ret = 1; - if (is_pkampublickeybase64_initialized(atkeys)) { - unset_pkampublickeybase64(atkeys); + if (is_pkam_public_key_base64_initialized(atkeys)) { + unset_pkam_public_key_base64(atkeys); } - const size_t pkampublickeysize = pkampublickeylen + 1; - atkeys->pkampublickeybase64 = (char *)malloc(sizeof(char) * (pkampublickeysize)); - if (atkeys->pkampublickeybase64 == NULL) { + const size_t pkam_publickey_size = pkam_public_key_len + 1; + atkeys->pkam_publickey_base64 = (char *)malloc(sizeof(char) * (pkam_publickey_size)); + if (atkeys->pkam_publickey_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkampublickeybase64\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkam_publickey_base64\n", ret); goto exit; } - memcpy(atkeys->pkampublickeybase64, pkampublickeybase64, pkampublickeylen); - atkeys->pkampublickeybase64[pkampublickeylen] = '\0'; + memcpy(atkeys->pkam_publickey_base64, pkam_publickey_base64, pkam_public_key_len); + atkeys->pkam_publickey_base64[pkam_public_key_len] = '\0'; - set_pkampublickeybase64_initialized(atkeys, true); + set_pkam_public_key_base64_initialized(atkeys, true); ret = 0; goto exit; @@ -807,108 +808,108 @@ static int set_pkampublickeybase64(atclient_atkeys *atkeys, const char *pkampubl exit: { return ret; } } -static int set_pkamprivatekeybase64(atclient_atkeys *atkeys, const char *pkamprivatekeybase64, - const size_t pkamprivatekeylen) { +static int set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, + const size_t pkam_private_key_len) { int ret = 1; - if (is_pkamprivatekeybase64_initialized(atkeys)) { - unset_pkamprivatekeybase64(atkeys); + if (is_pkam_private_key_base64_initialized(atkeys)) { + unset_pkam_private_key_base64(atkeys); } - const size_t pkamprivatekeysize = pkamprivatekeylen + 1; - atkeys->pkamprivatekeybase64 = (char *)malloc(sizeof(char) * (pkamprivatekeysize)); - if (atkeys->pkamprivatekeybase64 == NULL) { + const size_t pkam_private_key_size = pkam_private_key_len + 1; + 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 pkamprivatekeybase64\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkam_private_key_base64\n", ret); goto exit; } - memcpy(atkeys->pkamprivatekeybase64, pkamprivatekeybase64, pkamprivatekeylen); - atkeys->pkamprivatekeybase64[pkamprivatekeylen] = '\0'; + memcpy(atkeys->pkam_private_key_base64, pkam_private_key_base64, pkam_private_key_len); + atkeys->pkam_private_key_base64[pkam_private_key_len] = '\0'; - set_pkamprivatekeybase64_initialized(atkeys, true); + set_pkam_private_key_base64_initialized(atkeys, true); ret = 0; goto exit; exit: { return ret; } } -static int set_encryptpublickeybase64(atclient_atkeys *atkeys, const char *encryptpublickeybase64, - const size_t encryptpublickeylen) { +static int set_encrypt_public_key_base64(atclient_atkeys *atkeys, const char *encrypt_public_key_base64, + const size_t encrypt_public_key_len) { int ret = 1; - if (is_encryptpublickeybase64_initialized(atkeys)) { - unset_encryptpublickeybase64(atkeys); + if (is_encrypt_public_key_base64_initialized(atkeys)) { + unset_encrypt_public_key_base64(atkeys); } - const size_t encryptpublickeysize = encryptpublickeylen + 1; - atkeys->encryptpublickeybase64 = (char *)malloc(sizeof(char) * (encryptpublickeysize)); - if (atkeys->encryptpublickeybase64 == NULL) { + const size_t encrypt_public_key_size = encrypt_public_key_len + 1; + atkeys->encrypt_public_key_base64 = (char *)malloc(sizeof(char) * (encrypt_public_key_size)); + if (atkeys->encrypt_public_key_base64 == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "malloc: %d | failed to allocate memory for encryptpublickeybase64\n", ret); + "malloc: %d | failed to allocate memory for encrypt_public_key_base64\n", ret); goto exit; } - memcpy(atkeys->encryptpublickeybase64, encryptpublickeybase64, encryptpublickeylen); - atkeys->encryptpublickeybase64[encryptpublickeylen] = '\0'; + memcpy(atkeys->encrypt_public_key_base64, encrypt_public_key_base64, encrypt_public_key_len); + atkeys->encrypt_public_key_base64[encrypt_public_key_len] = '\0'; - set_encryptpublickeybase64_initialized(atkeys, true); + set_encrypt_public_key_base64_initialized(atkeys, true); ret = 0; goto exit; exit: { return ret; } } -static int set_encryptprivatekeybase64(atclient_atkeys *atkeys, const char *encryptprivatekeybase64, - const size_t encryptprivatekeylen) { +static int set_encrypt_private_key_base64(atclient_atkeys *atkeys, const char *encrypt_private_key_base64, + const size_t encrypt_private_key_len) { int ret = 1; - if (is_encryptprivatekeybase64_initialized(atkeys)) { - unset_encryptprivatekeybase64(atkeys); + if (is_encrypt_private_key_base64_initialized(atkeys)) { + unset_encrypt_private_key_base64(atkeys); } - const size_t encryptprivatekeysize = encryptprivatekeylen + 1; - atkeys->encryptprivatekeybase64 = (char *)malloc(sizeof(char) * (encryptprivatekeysize)); - if (atkeys->encryptprivatekeybase64 == NULL) { + const size_t encrypt_private_key_size = encrypt_private_key_len + 1; + atkeys->encrypt_private_key_base64 = (char *)malloc(sizeof(char) * (encrypt_private_key_size)); + if (atkeys->encrypt_private_key_base64 == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "malloc: %d | failed to allocate memory for encryptprivatekeybase64\n", ret); + "malloc: %d | failed to allocate memory for encrypt_private_key_base64\n", ret); goto exit; } - memcpy(atkeys->encryptprivatekeybase64, encryptprivatekeybase64, encryptprivatekeylen); - atkeys->encryptprivatekeybase64[encryptprivatekeylen] = '\0'; + memcpy(atkeys->encrypt_private_key_base64, encrypt_private_key_base64, encrypt_private_key_len); + atkeys->encrypt_private_key_base64[encrypt_private_key_len] = '\0'; - set_encryptprivatekeybase64_initialized(atkeys, true); + set_encrypt_privatekey_base64_initialized(atkeys, true); ret = 0; goto exit; exit: { return ret; } } -static int set_selfencryptionkeybase64(atclient_atkeys *atkeys, const char *selfencryptionkeybase64, - const size_t selfencryptionkeylen) { +static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *self_encryption_key_base64, + const size_t self_encryption_key_len) { int ret = 1; - if (is_selfencryptionkeybase64_initialized(atkeys)) { - unset_selfencryptionkeybase64(atkeys); + if (is_self_encryption_key_base64_initialized(atkeys)) { + unset_self_encryption_key_base64(atkeys); } - const size_t selfencryptionkeysize = selfencryptionkeylen + 1; - atkeys->selfencryptionkeybase64 = (char *)malloc(sizeof(char) * (selfencryptionkeysize)); - if (atkeys->selfencryptionkeybase64 == NULL) { + 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)); + if (atkeys->self_encryption_key_base64 == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "malloc: %d | failed to allocate memory for selfencryptionkeybase64\n", ret); + "malloc: %d | failed to allocate memory for self_encryption_key_base64\n", ret); goto exit; } - memcpy(atkeys->selfencryptionkeybase64, selfencryptionkeybase64, selfencryptionkeylen); - atkeys->selfencryptionkeybase64[selfencryptionkeylen] = '\0'; + memcpy(atkeys->self_encryption_key_base64, self_encryption_key_base64, self_encryption_key_len); + atkeys->self_encryption_key_base64[self_encryption_key_len] = '\0'; - set_selfencryptionkeybase64_initialized(atkeys, true); + set_self_encryption_key_base64_initialized(atkeys, true); ret = 0; goto exit; diff --git a/packages/atclient/src/atkeysfile.c b/packages/atclient/src/atkeysfile.c index d9b82571..63b7955d 100644 --- a/packages/atclient/src/atkeysfile.c +++ b/packages/atclient/src/atkeysfile.c @@ -12,34 +12,34 @@ #define TAG "atkeysfile" -static bool is_aespkampublickeystr_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile); -static bool is_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile); -static bool is_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile); - -static void set_aespkampublickestr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); -static void set_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized); - -static void unset_aespkampublickeystr(atclient_atkeysfile *atkeysfile); -static void unset_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile); -static void unset_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile); -static void unset_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile); -static void unset_selfencryptionkeystr(atclient_atkeysfile *atkeysfile); - -static int set_aespkampublickeystr(atclient_atkeysfile *atkeysfile, const char *aespkampublickeystr, - const size_t aespkampublickeystrlen); -static int set_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aespkamprivatekeystr, - const size_t aespkamprivatekeystrlen); -static int set_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptpublickeystr, - const size_t aesencryptpublickeystrlen); -static int set_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptprivatekeystr, - const size_t aesencryptprivatekeystrlen); -static int set_selfencryptionkeystr(atclient_atkeysfile *atkeysfile, const char *selfencryptionkeystr, - const size_t selfencryptionkeystrlen); +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)); } @@ -58,83 +58,83 @@ int atclient_atkeysfile_read(atclient_atkeysfile *atkeysfile, const char *path) goto exit; } - const size_t bytesread = fread(readbuf, 1, FILE_READ_BUFFER_SIZE, file); + const size_t bytes_read = fread(readbuf, 1, FILE_READ_BUFFER_SIZE, file); fclose(file); - if (bytesread == 0) { + if (bytes_read == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "fread failed\n"); ret = 1; goto exit; } root = cJSON_Parse(readbuf); - cJSON *aespkampublickey = cJSON_GetObjectItem(root, "aesPkamPublicKey"); - cJSON *aespkamprivatekey = cJSON_GetObjectItem(root, "aesPkamPrivateKey"); - cJSON *aesencryptpublickey = cJSON_GetObjectItem(root, "aesEncryptPublicKey"); - cJSON *aesencryptprivatekey = cJSON_GetObjectItem(root, "aesEncryptPrivateKey"); - cJSON *selfencryptionkey = cJSON_GetObjectItem(root, "selfEncryptionKey"); + 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 (aespkamprivatekey == NULL) { + if (aes_pkam_private_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPrivateKey!\n"); goto exit; } - if (aespkampublickey == NULL) { + if (aes_pkam_public_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesPkamPublicKey!\n"); goto exit; } - if (aesencryptprivatekey == NULL) { + if (aes_encrypt_private_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPrivateKey!\n"); goto exit; } - if (aesencryptpublickey == NULL) { + if (aes_encrypt_public_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading aesEncryptPublicKey!\n"); goto exit; } - if (selfencryptionkey == NULL) { + if (self_encryption_key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Error reading selfEncryptionKey!\n"); goto exit; } - if ((ret = set_aespkampublickeystr(atkeysfile, aespkampublickey->valuestring, - strlen(aespkampublickey->valuestring))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_aespkampublickeystr: %d | failed to set aespkampublickeystr\n", + 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_aespkamprivatekeystr(atkeysfile, aespkamprivatekey->valuestring, - strlen(aespkamprivatekey->valuestring))) != 0) { + 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_aespkamprivatekeystr: %d | failed to set aespkamprivatekeystr\n", ret); + "set_aes_pkam_private_key_str: %d | failed to set aes_pkam_private_key_str\n", ret); goto exit; } - if ((ret = set_aesencryptpublickeystr(atkeysfile, aesencryptpublickey->valuestring, - strlen(aesencryptpublickey->valuestring))) != 0) { + 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_aesencryptpublickeystr: %d | failed to set aesencryptpublickeystr\n", ret); + "set_aes_encrypt_public_key_str: %d | failed to set aes_encrypt_public_key_str\n", ret); goto exit; } - if ((ret = set_aesencryptprivatekeystr(atkeysfile, aesencryptprivatekey->valuestring, - strlen(aesencryptprivatekey->valuestring))) != 0) { + 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_aesencryptprivatekeystr: %d | failed to set aesencryptprivatekeystr\n", ret); + "set_aes_encrypt_private_key_str: %d | failed to set aes_encrypt_private_key_str\n", ret); goto exit; } - if ((ret = set_selfencryptionkeystr(atkeysfile, selfencryptionkey->valuestring, - strlen(selfencryptionkey->valuestring))) != 0) { + 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_selfencryptionkeystr: %d | failed to set selfencryptionkeystr\n", ret); + "set_self_encryption_key_str: %d | failed to set self_encryption_key_str\n", ret); goto exit; } @@ -148,178 +148,178 @@ exit: { } void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile) { - unset_aespkampublickeystr(atkeysfile); - unset_aespkamprivatekeystr(atkeysfile); - unset_aesencryptpublickeystr(atkeysfile); - unset_aesencryptprivatekeystr(atkeysfile); - unset_selfencryptionkeystr(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_aespkampublickeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; +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_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; +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_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; +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_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; +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_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile) { - return atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] & ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; +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_aespkampublickestr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { +static void set_aes_pkam_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; } else { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESPKAMPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_PKAM_PUBLIC_KEY_STR_INITIALIZED; } } -static void set_aespkamprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { +static void set_aes_pkam_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; } else { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESPKAMPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_PKAM_PRIVATE_KEY_STR_INITIALIZED; } } -static void set_aesencryptpublickeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { +static void set_aes_encrypt_public_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; } else { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESENCRYPTPUBLICKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PUBLIC_KEY_STR_INITIALIZED; } } -static void set_aesencryptprivatekeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { +static void set_aes_encrypt_private_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; } else { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AESENCRYPTPRIVATEKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_AES_ENCRYPT_PRIVATE_KEY_STR_INITIALIZED; } } -static void set_selfencryptionkeystr_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { +static void set_self_encryption_key_str_initialized(atclient_atkeysfile *atkeysfile, const bool initialized) { if (initialized) { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] |= ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX] |= ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; } else { - atkeysfile->_initializedfields[ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INDEX] &= ~ATCLIENT_ATKEYSFILE_SELFENCRYPTIONKEYSTR_INTIIALIZED; + atkeysfile->_initialized_fields[ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INDEX] &= ~ATCLIENT_ATKEYSFILE_SELF_ENCRYPTION_KEY_STR_INITIALIZED; } } -static void unset_aespkampublickeystr(atclient_atkeysfile *atkeysfile) { - if (is_aespkampublickeystr_initialized(atkeysfile)) { - free(atkeysfile->aespkampublickeystr); +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->aespkampublickeystr = NULL; - set_aespkampublickestr_initialized(atkeysfile, false); + atkeysfile->aes_pkam_public_key_str = NULL; + set_aes_pkam_public_key_str_initialized(atkeysfile, false); } -static void unset_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile) { - if (is_aespkamprivatekeystr_initialized(atkeysfile)) { - free(atkeysfile->aespkamprivatekeystr); +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->aespkamprivatekeystr = NULL; - set_aespkamprivatekeystr_initialized(atkeysfile, false); + atkeysfile->aes_pkam_private_key_str = NULL; + set_aes_pkam_private_key_str_initialized(atkeysfile, false); } -static void unset_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile) { - if (is_aesencryptpublickeystr_initialized(atkeysfile)) { - free(atkeysfile->aesencryptpublickeystr); +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->aesencryptpublickeystr = NULL; - set_aesencryptpublickeystr_initialized(atkeysfile, false); + atkeysfile->aes_encrypt_public_key_str = NULL; + set_aes_encrypt_public_key_str_initialized(atkeysfile, false); } -static void unset_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile) { - if (is_aesencryptprivatekeystr_initialized(atkeysfile)) { - free(atkeysfile->aesencryptprivatekeystr); +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->aesencryptprivatekeystr = NULL; - set_aesencryptprivatekeystr_initialized(atkeysfile, false); + atkeysfile->aes_encrypt_private_key_str = NULL; + set_aes_encrypt_private_key_str_initialized(atkeysfile, false); } -static void unset_selfencryptionkeystr(atclient_atkeysfile *atkeysfile) { - if (is_selfencryptionkeystr_initialized(atkeysfile)) { - free(atkeysfile->selfencryptionkeystr); +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->selfencryptionkeystr = NULL; - set_selfencryptionkeystr_initialized(atkeysfile, false); + atkeysfile->self_encryption_key_str = NULL; + set_self_encryption_key_str_initialized(atkeysfile, false); } -static int set_aespkampublickeystr(atclient_atkeysfile *atkeysfile, const char *aespkampublickeystr, - const size_t aespkampublickeystrlen) { +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_aespkampublickeystr_initialized(atkeysfile)) { - unset_aespkampublickeystr(atkeysfile); + if (is_aes_pkam_public_key_str_initialized(atkeysfile)) { + unset_aes_pkam_public_key_str(atkeysfile); } - const size_t aespkampublickeystrsize = aespkampublickeystrlen + 1; - if ((atkeysfile->aespkampublickeystr = (char *)malloc(sizeof(char) * aespkampublickeystrsize)) == NULL) { + 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_aespkampublickestr_initialized(atkeysfile, true); - memcpy(atkeysfile->aespkampublickeystr, aespkampublickeystr, aespkampublickeystrlen); - atkeysfile->aespkampublickeystr[aespkampublickeystrlen] = '\0'; + 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_aespkamprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aespkamprivatekeystr, - const size_t aespkamprivatekeystrlen) { +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_aespkamprivatekeystr_initialized(atkeysfile)) { - unset_aespkamprivatekeystr(atkeysfile); + if (is_aes_pkam_private_key_str_initialized(atkeysfile)) { + unset_aes_pkam_private_key_str(atkeysfile); } - const size_t aespkamprivatekeystrsize = aespkamprivatekeystrlen + 1; - if ((atkeysfile->aespkamprivatekeystr = (char *)malloc(sizeof(char) * aespkamprivatekeystrsize)) == NULL) { + 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_aespkamprivatekeystr_initialized(atkeysfile, true); - memcpy(atkeysfile->aespkamprivatekeystr, aespkamprivatekeystr, aespkamprivatekeystrlen); - atkeysfile->aespkamprivatekeystr[aespkamprivatekeystrlen] = '\0'; + 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_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptpublickeystr, - const size_t aesencryptpublickeystrlen) { +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_aesencryptpublickeystr_initialized(atkeysfile)) { - unset_aesencryptpublickeystr(atkeysfile); + if (is_aes_encrypt_public_key_str_initialized(atkeysfile)) { + unset_aes_encrypt_public_key_str(atkeysfile); } - const size_t aesencryptpublickeystrsize = aesencryptpublickeystrlen + 1; - if ((atkeysfile->aesencryptpublickeystr = (char *)malloc(sizeof(char) * aesencryptpublickeystrsize)) == NULL) { + 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_aesencryptpublickeystr_initialized(atkeysfile, true); - memcpy(atkeysfile->aesencryptpublickeystr, aesencryptpublickeystr, aesencryptpublickeystrlen); - atkeysfile->aesencryptpublickeystr[aesencryptpublickeystrlen] = '\0'; + 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; @@ -327,51 +327,51 @@ static int set_aesencryptpublickeystr(atclient_atkeysfile *atkeysfile, const cha exit: { return ret; } } -static int set_aesencryptprivatekeystr(atclient_atkeysfile *atkeysfile, const char *aesencryptprivatekeystr, - const size_t aesencryptprivatekeystrlen) { +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_aesencryptprivatekeystr_initialized(atkeysfile)) { - unset_aesencryptprivatekeystr(atkeysfile); + if (is_aes_encrypt_private_key_str_initialized(atkeysfile)) { + unset_aes_encrypt_private_key_str(atkeysfile); } - const size_t aesencryptprivatekeystrsize = aesencryptprivatekeystrlen + 1; - if ((atkeysfile->aesencryptprivatekeystr = (char *)malloc(sizeof(char) * aesencryptprivatekeystrsize)) == NULL) { + 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_aesencryptprivatekeystr_initialized(atkeysfile, true); - memcpy(atkeysfile->aesencryptprivatekeystr, aesencryptprivatekeystr, aesencryptprivatekeystrlen); - atkeysfile->aesencryptprivatekeystr[aesencryptprivatekeystrlen] = '\0'; + 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_selfencryptionkeystr(atclient_atkeysfile *atkeysfile, const char *selfencryptionkeystr, - const size_t selfencryptionkeystrlen) { +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_selfencryptionkeystr_initialized(atkeysfile)) { - unset_selfencryptionkeystr(atkeysfile); + if (is_self_encryption_key_str_initialized(atkeysfile)) { + unset_self_encryption_key_str(atkeysfile); } - const size_t selfencryptionkeystrsize = selfencryptionkeystrlen + 1; - if ((atkeysfile->selfencryptionkeystr = (char *)malloc(sizeof(char) * selfencryptionkeystrsize)) == NULL) { + 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_selfencryptionkeystr_initialized(atkeysfile, true); - memcpy(atkeysfile->selfencryptionkeystr, selfencryptionkeystr, selfencryptionkeystrlen); - atkeysfile->selfencryptionkeystr[selfencryptionkeystrlen] = '\0'; + 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; } -} \ No newline at end of file +} diff --git a/packages/atclient/src/atnotification.c b/packages/atclient/src/atnotification.c index 16209c93..cb0e88be 100644 --- a/packages/atclient/src/atnotification.c +++ b/packages/atclient/src/atnotification.c @@ -28,242 +28,242 @@ void atclient_atnotification_free(atclient_atnotification *notification) { if (atclient_atnotification_is_operation_initialized(notification)) { atclient_atnotification_unset_operation(notification); } - if (atclient_atnotification_is_epochmillis_initialized(notification)) { - atclient_atnotification_unset_epochmillis(notification); + if (atclient_atnotification_is_epoch_millis_initialized(notification)) { + atclient_atnotification_unset_epoch_millis(notification); } - if (atclient_atnotification_is_messagetype_initialized(notification)) { - atclient_atnotification_unset_messagetype(notification); + if (atclient_atnotification_is_message_type_initialized(notification)) { + atclient_atnotification_unset_message_type(notification); } - if (atclient_atnotification_is_isencrypted_initialized(notification)) { - atclient_atnotification_unset_isencrypted(notification); + if (atclient_atnotification_is_is_encrypted_initialized(notification)) { + atclient_atnotification_unset_is_encrypted(notification); } - if (atclient_atnotification_is_enckeyname_initialized(notification)) { - atclient_atnotification_unset_enckeyname(notification); + if (atclient_atnotification_is_enc_key_name_initialized(notification)) { + atclient_atnotification_unset_enc_key_name(notification); } - if (atclient_atnotification_is_encalgo_initialized(notification)) { - atclient_atnotification_unset_encalgo(notification); + if (atclient_atnotification_is_enc_algo_initialized(notification)) { + atclient_atnotification_unset_enc_algo(notification); } - if (atclient_atnotification_is_ivnonce_initialized(notification)) { - atclient_atnotification_unset_ivnonce(notification); + if (atclient_atnotification_is_iv_nonce_initialized(notification)) { + atclient_atnotification_unset_iv_nonce(notification); } - if (atclient_atnotification_is_skeenckeyname_initialized(notification)) { - atclient_atnotification_unset_skeenckeyname(notification); + if (atclient_atnotification_is_ske_enc_key_name_initialized(notification)) { + atclient_atnotification_unset_ske_enc_key_name(notification); } - if (atclient_atnotification_is_skeencalgo_initialized(notification)) { - atclient_atnotification_unset_skeencalgo(notification); + if (atclient_atnotification_is_ske_enc_algo_initialized(notification)) { + atclient_atnotification_unset_ske_enc_algo(notification); } - if (atclient_atnotification_is_decryptedvalue_initialized(notification)) { - atclient_atnotification_unset_decryptedvalue(notification); + if (atclient_atnotification_is_decrypted_value_initialized(notification)) { + atclient_atnotification_unset_decrypted_value(notification); } } bool atclient_atnotification_is_id_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] & ATCLIENT_ATNOTIFICATION_ID_INITIALIZED); } bool atclient_atnotification_is_from_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] & ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED); } bool atclient_atnotification_is_to_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] & ATCLIENT_ATNOTIFICATION_TO_INITIALIZED); } bool atclient_atnotification_is_key_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] & ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED); } bool atclient_atnotification_is_value_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] & ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED); } bool atclient_atnotification_is_operation_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] & ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED); } -bool atclient_atnotification_is_epochmillis_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & +bool atclient_atnotification_is_epoch_millis_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] & ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED); } -bool atclient_atnotification_is_messagetype_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & +bool atclient_atnotification_is_message_type_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] & ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED); } -bool atclient_atnotification_is_isencrypted_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & +bool atclient_atnotification_is_is_encrypted_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] & ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED); } -bool atclient_atnotification_is_enckeyname_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & +bool atclient_atnotification_is_enc_key_name_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] & ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_is_encalgo_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & +bool atclient_atnotification_is_enc_algo_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] & ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED); } -bool atclient_atnotification_is_ivnonce_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & +bool atclient_atnotification_is_iv_nonce_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] & ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED); } -bool atclient_atnotification_is_skeenckeyname_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & +bool atclient_atnotification_is_ske_enc_key_name_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] & ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED); } -bool atclient_atnotification_is_skeencalgo_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & +bool atclient_atnotification_is_ske_enc_algo_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] & ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED); } -bool atclient_atnotification_is_decryptedvalue_initialized(const atclient_atnotification *notification) { - return (notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & +bool atclient_atnotification_is_decrypted_value_initialized(const atclient_atnotification *notification) { + return (notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] & ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED); } -void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_id_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] |= ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ID_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ID_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ID_INITIALIZED; } } -void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_from_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] |= ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_FROM_INDEX] &= ~ATCLIENT_ATNOTIFICATION_FROM_INITIALIZED; } } -void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_to_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] |= ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_TO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_TO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_TO_INITIALIZED; } } -void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_key_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] |= ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_KEY_INDEX] &= ~ATCLIENT_ATNOTIFICATION_KEY_INITIALIZED; } } -void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_value_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] &= ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_VALUE_INDEX] &= ~ATCLIENT_ATNOTIFICATION_VALUE_INITIALIZED; } } -void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_operation_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] |= ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_OPERATION_INDEX] &= ~ATCLIENT_ATNOTIFICATION_OPERATION_INITIALIZED; } } -void atclient_atnotification_epochmillis_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_epoch_millis_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] |= ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INDEX] &= ~ATCLIENT_ATNOTIFICATION_EPOCHMILLIS_INITIALIZED; } } -void atclient_atnotification_messagetype_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_message_type_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INDEX] |= ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; } else { - notification->_initializedfields[0] &= ~ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; + notification->_initialized_fields[0] &= ~ATCLIENT_ATNOTIFICATION_MESSAGETYPE_INITIALIZED; } } -void atclient_atnotification_isencrypted_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_is_encrypted_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] |= ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ISENCRYPTED_INITIALIZED; } } -void atclient_atnotification_enckeyname_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_enc_key_name_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INDEX] |= ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; } else { - notification->_initializedfields[1] &= ~ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; + notification->_initialized_fields[1] &= ~ATCLIENT_ATNOTIFICATION_ENCKEYNAME_INITIALIZED; } } -void atclient_atnotification_encalgo_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_enc_algo_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] |= ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_ENCALGO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_ENCALGO_INITIALIZED; } } -void atclient_atnotification_ivnonce_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_iv_nonce_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] |= ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_IVNONCE_INDEX] &= ~ATCLIENT_ATNOTIFICATION_IVNONCE_INITIALIZED; } } -void atclient_atnotification_skeenckeyname_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_ske_enc_key_name_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] |= ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INDEX] &= ~ATCLIENT_ATNOTIFICATION_SKEENCKEYNAME_INITIALIZED; } } -void atclient_atnotification_skeencalgo_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_ske_enc_algo_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] |= ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_SKEENCALGO_INDEX] &= ~ATCLIENT_ATNOTIFICATION_SKEENCALGO_INITIALIZED; } } -void atclient_atnotification_decryptedvalue_set_initialized(atclient_atnotification *notification, bool initialized) { +void atclient_atnotification_decrypted_value_set_initialized(atclient_atnotification *notification, const bool initialized) { if (initialized) { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] |= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] |= ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; } else { - notification->_initializedfields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] &= + notification->_initialized_fields[ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INDEX] &= ~ATCLIENT_ATNOTIFICATION_DECRYPTEDVALUE_INITIALIZED; } } @@ -286,14 +286,14 @@ void atclient_atnotification_unset_from(atclient_atnotification *notification) { } void atclient_atnotification_unset_to(atclient_atnotification *notification) { - if(atclient_atnotification_is_to_initialized(notification)) { + if (atclient_atnotification_is_to_initialized(notification)) { free(notification->to); } notification->to = NULL; atclient_atnotification_to_set_initialized(notification, false); } void atclient_atnotification_unset_key(atclient_atnotification *notification) { - if(atclient_atnotification_is_key_initialized(notification)) { + if (atclient_atnotification_is_key_initialized(notification)) { free(notification->key); } notification->key = NULL; @@ -303,343 +303,361 @@ void atclient_atnotification_unset_value(atclient_atnotification *notification) if (atclient_atnotification_is_value_initialized(notification)) { free(notification->value); } - notification->value = NULL; + notification->value = NULL; atclient_atnotification_value_set_initialized(notification, false); } void atclient_atnotification_unset_operation(atclient_atnotification *notification) { - if(atclient_atnotification_is_operation_initialized(notification)) { + if (atclient_atnotification_is_operation_initialized(notification)) { free(notification->operation); } notification->operation = NULL; atclient_atnotification_operation_set_initialized(notification, false); } -void atclient_atnotification_unset_epochmillis(atclient_atnotification *notification) { - notification->epochMillis = 0; - atclient_atnotification_epochmillis_set_initialized(notification, false); +void atclient_atnotification_unset_epoch_millis(atclient_atnotification *notification) { + notification->epoch_millis = 0; + atclient_atnotification_epoch_millis_set_initialized(notification, false); } -void atclient_atnotification_unset_messagetype(atclient_atnotification *notification) { - if(atclient_atnotification_is_messagetype_initialized(notification)) { - free(notification->messageType); +void atclient_atnotification_unset_message_type(atclient_atnotification *notification) { + if (atclient_atnotification_is_message_type_initialized(notification)) { + free(notification->message_type); } - notification->messageType = NULL; - atclient_atnotification_messagetype_set_initialized(notification, false); + notification->message_type = NULL; + atclient_atnotification_message_type_set_initialized(notification, false); } -void atclient_atnotification_unset_isencrypted(atclient_atnotification *notification) { - notification->isEncrypted = false; - atclient_atnotification_isencrypted_set_initialized(notification, false); +void atclient_atnotification_unset_is_encrypted(atclient_atnotification *notification) { + notification->is_encrypted = false; + atclient_atnotification_is_encrypted_set_initialized(notification, false); } -void atclient_atnotification_unset_enckeyname(atclient_atnotification *notification) { - if(atclient_atnotification_is_enckeyname_initialized(notification)) { - free(notification->encKeyName); +void atclient_atnotification_unset_enc_key_name(atclient_atnotification *notification) { + if (atclient_atnotification_is_enc_key_name_initialized(notification)) { + free(notification->enc_key_name); } - notification->encKeyName = NULL; - atclient_atnotification_enckeyname_set_initialized(notification, false); + notification->enc_key_name = NULL; + atclient_atnotification_enc_key_name_set_initialized(notification, false); } -void atclient_atnotification_unset_encalgo(atclient_atnotification *notification) { - if(atclient_atnotification_is_encalgo_initialized(notification)) { - free(notification->encAlgo); +void atclient_atnotification_unset_enc_algo(atclient_atnotification *notification) { + if (atclient_atnotification_is_enc_algo_initialized(notification)) { + free(notification->enc_algo); } - notification->encAlgo = NULL; - atclient_atnotification_encalgo_set_initialized(notification, false); + notification->enc_algo = NULL; + atclient_atnotification_enc_algo_set_initialized(notification, false); } -void atclient_atnotification_unset_ivnonce(atclient_atnotification *notification) { - if(atclient_atnotification_is_ivnonce_initialized(notification)) { - free(notification->ivNonce); +void atclient_atnotification_unset_iv_nonce(atclient_atnotification *notification) { + if (atclient_atnotification_is_iv_nonce_initialized(notification)) { + free(notification->iv_nonce); } - notification->ivNonce = NULL; - atclient_atnotification_ivnonce_set_initialized(notification, false); + notification->iv_nonce = NULL; + atclient_atnotification_iv_nonce_set_initialized(notification, false); } -void atclient_atnotification_unset_skeenckeyname(atclient_atnotification *notification) { - if(atclient_atnotification_is_skeenckeyname_initialized(notification)) { - free(notification->skeEncKeyName); +void atclient_atnotification_unset_ske_enc_key_name(atclient_atnotification *notification) { + if (atclient_atnotification_is_ske_enc_key_name_initialized(notification)) { + free(notification->ske_enc_key_name); } - notification->skeEncKeyName = NULL; - atclient_atnotification_skeenckeyname_set_initialized(notification, false); + notification->ske_enc_key_name = NULL; + atclient_atnotification_ske_enc_key_name_set_initialized(notification, false); } -void atclient_atnotification_unset_skeencalgo(atclient_atnotification *notification) { - if(atclient_atnotification_is_skeencalgo_initialized(notification)) { - free(notification->skeEncAlgo); +void atclient_atnotification_unset_ske_enc_algo(atclient_atnotification *notification) { + if (atclient_atnotification_is_ske_enc_algo_initialized(notification)) { + free(notification->ske_enc_algo); } - notification->skeEncAlgo = NULL; - atclient_atnotification_skeencalgo_set_initialized(notification, false); + notification->ske_enc_algo = NULL; + atclient_atnotification_ske_enc_algo_set_initialized(notification, false); } -void atclient_atnotification_unset_decryptedvalue(atclient_atnotification *notification) { - if(atclient_atnotification_is_decryptedvalue_initialized(notification)) { - free(notification->decryptedvalue); +void atclient_atnotification_unset_decrypted_value(atclient_atnotification *notification) { + if (atclient_atnotification_is_decrypted_value_initialized(notification)) { + free(notification->decrypted_value); } - notification->decryptedvalue = NULL; - atclient_atnotification_decryptedvalue_set_initialized(notification, false); + notification->decrypted_value = NULL; + atclient_atnotification_decrypted_value_set_initialized(notification, false); } -int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id, const size_t idlen) { +int atclient_atnotification_set_id(atclient_atnotification *notification, const char *id) { int ret = 1; if (atclient_atnotification_is_id_initialized(notification)) { atclient_atnotification_unset_id(notification); } - notification->id = malloc(sizeof(char) * (idlen + 1)); + const size_t id_len = strlen(id); + const size_t id_size = id_len + 1; + notification->id = malloc(sizeof(char) * (id_size)); if (notification->id == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification id\n"); goto exit; } - memcpy(notification->id, id, idlen); - *(notification->id + idlen) = '\0'; + memcpy(notification->id, id, id_len); + *(notification->id + id_len) = '\0'; atclient_atnotification_id_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from, const size_t fromlen) { +int atclient_atnotification_set_from(atclient_atnotification *notification, const char *from) { int ret = 1; if (atclient_atnotification_is_from_initialized(notification)) { atclient_atnotification_unset_from(notification); } - notification->from = malloc(sizeof(char) * (fromlen + 1)); + const size_t from_len = strlen(from); + const size_t from_size = from_len + 1; + notification->from = malloc(sizeof(char) * (from_size)); if (notification->from == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification from\n"); goto exit; } - memcpy(notification->from, from, fromlen); - *(notification->from + fromlen) = '\0'; + memcpy(notification->from, from, from_len); + *(notification->from + from_len) = '\0'; atclient_atnotification_from_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to, const size_t tolen) { +int atclient_atnotification_set_to(atclient_atnotification *notification, const char *to) { int ret = 1; if (atclient_atnotification_is_to_initialized(notification)) { atclient_atnotification_unset_to(notification); } - notification->to = malloc(sizeof(char) * (tolen + 1)); + const size_t to_len = strlen(to); + const size_t to_size = to_len + 1; + notification->to = malloc(sizeof(char) * (to_size)); if (notification->to == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification to\n"); goto exit; } - memcpy(notification->to, to, tolen); - *(notification->to + tolen) = '\0'; + memcpy(notification->to, to, to_len); + *(notification->to + to_len) = '\0'; atclient_atnotification_to_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key, const size_t keylen) { +int atclient_atnotification_set_key(atclient_atnotification *notification, const char *key) { int ret = 1; if (atclient_atnotification_is_key_initialized(notification)) { atclient_atnotification_unset_key(notification); } - notification->key = malloc(sizeof(char) * (keylen + 1)); + const size_t key_len = strlen(key); + const size_t key_size = key_len + 1; + notification->key = malloc(sizeof(char) * (key_size)); if (notification->key == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification key\n"); goto exit; } - memcpy(notification->key, key, keylen); - *(notification->key + keylen) = '\0'; + memcpy(notification->key, key, key_len); + *(notification->key + key_len) = '\0'; atclient_atnotification_key_set_initialized(notification, true); goto exit; exit: { return ret; } } -int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value, const size_t valuelen) { +int atclient_atnotification_set_value(atclient_atnotification *notification, const char *value) { int ret = 1; if (atclient_atnotification_is_value_initialized(notification)) { atclient_atnotification_unset_value(notification); } - notification->value = malloc(sizeof(char) * (valuelen + 1)); + const size_t value_len = strlen(value); + const size_t value_size = value_len + 1; + notification->value = malloc(sizeof(char) * (value_size)); if (notification->value == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification value\n"); goto exit; } - memcpy(notification->value, value, valuelen); - *(notification->value + valuelen) = '\0'; + memcpy(notification->value, value, value_len); + *(notification->value + value_len) = '\0'; atclient_atnotification_value_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation, - const size_t operationlen) { +int atclient_atnotification_set_operation(atclient_atnotification *notification, const char *operation) { int ret = 1; if (atclient_atnotification_is_operation_initialized(notification)) { atclient_atnotification_unset_operation(notification); } - notification->operation = malloc(sizeof(char) * (operationlen + 1)); + const size_t operation_len = strlen(operation); + const size_t operation_size = operation_len + 1; + notification->operation = malloc(sizeof(char) * (operation_size)); if (notification->operation == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification operation\n"); goto exit; } - memcpy(notification->operation, operation, operationlen); - *(notification->operation + operationlen) = '\0'; + memcpy(notification->operation, operation, operation_len); + *(notification->operation + operation_len) = '\0'; atclient_atnotification_operation_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_epochmillis(atclient_atnotification *notification, const size_t epochMillis) { - if (atclient_atnotification_is_epochmillis_initialized(notification)) { - atclient_atnotification_unset_epochmillis(notification); +int atclient_atnotification_set_epoch_millis(atclient_atnotification *notification, const size_t epoch_millis) { + if (atclient_atnotification_is_epoch_millis_initialized(notification)) { + atclient_atnotification_unset_epoch_millis(notification); } - notification->epochMillis = epochMillis; - atclient_atnotification_epochmillis_set_initialized(notification, true); + notification->epoch_millis = epoch_millis; + atclient_atnotification_epoch_millis_set_initialized(notification, true); return 0; } -int atclient_atnotification_set_messagetype(atclient_atnotification *notification, const char *messageType, - const size_t messageTypelen) { +int atclient_atnotification_set_message_type(atclient_atnotification *notification, const char *message_type) { int ret = 1; - if (atclient_atnotification_is_messagetype_initialized(notification)) { - atclient_atnotification_unset_messagetype(notification); + if (atclient_atnotification_is_message_type_initialized(notification)) { + atclient_atnotification_unset_message_type(notification); } - notification->messageType = malloc(sizeof(char) * (messageTypelen + 1)); - if (notification->messageType == NULL) { + const size_t message_type_len = strlen(message_type); + const size_t message_type_size = message_type_len + 1; + notification->message_type = malloc(sizeof(char) * (message_type_size)); + if (notification->message_type == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification messageType\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification message_type\n"); goto exit; } - memcpy(notification->messageType, messageType, messageTypelen); - *(notification->messageType + messageTypelen) = '\0'; - atclient_atnotification_messagetype_set_initialized(notification, true); + memcpy(notification->message_type, message_type, message_type_len); + *(notification->message_type + message_type_len) = '\0'; + atclient_atnotification_message_type_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_isencrypted(atclient_atnotification *notification, const bool isEncrypted) { - if (atclient_atnotification_is_isencrypted_initialized(notification)) { - atclient_atnotification_unset_isencrypted(notification); +int atclient_atnotification_set_is_encrypted(atclient_atnotification *notification, const bool is_encrypted) { + if (atclient_atnotification_is_is_encrypted_initialized(notification)) { + atclient_atnotification_unset_is_encrypted(notification); } - notification->isEncrypted = isEncrypted; - atclient_atnotification_isencrypted_set_initialized(notification, true); + notification->is_encrypted = is_encrypted; + atclient_atnotification_is_encrypted_set_initialized(notification, true); return 0; } -int atclient_atnotification_set_enckeyname(atclient_atnotification *notification, const char *encKeyName, - const size_t encKeyNamelen) { +int atclient_atnotification_set_enc_key_name(atclient_atnotification *notification, const char *enc_key_name) { int ret = 1; - if (atclient_atnotification_is_enckeyname_initialized(notification)) { - atclient_atnotification_unset_enckeyname(notification); - } - notification->encKeyName = malloc(sizeof(char) * (encKeyNamelen + 1)); - if (notification->encKeyName == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification encKeyName\n"); + if (atclient_atnotification_is_enc_key_name_initialized(notification)) { + atclient_atnotification_unset_enc_key_name(notification); + } + const size_t enc_key_name_len = strlen(enc_key_name); + const size_t enc_key_name_size = enc_key_name_len + 1; + notification->enc_key_name = malloc(sizeof(char) * (enc_key_name_size)); + if (notification->enc_key_name == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification enc_key_name\n"); goto exit; } - memcpy(notification->encKeyName, encKeyName, encKeyNamelen); - *(notification->encKeyName + encKeyNamelen) = '\0'; - atclient_atnotification_enckeyname_set_initialized(notification, true); + memcpy(notification->enc_key_name, enc_key_name, enc_key_name_len); + *(notification->enc_key_name + enc_key_name_len) = '\0'; + atclient_atnotification_enc_key_name_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_encalgo(atclient_atnotification *notification, const char *encAlgo, - const size_t encAlgolen) { +int atclient_atnotification_set_enc_algo(atclient_atnotification *notification, const char *enc_algo) { int ret = 1; - if (atclient_atnotification_is_encalgo_initialized(notification)) { - atclient_atnotification_unset_encalgo(notification); - } - notification->encAlgo = malloc(sizeof(char) * (encAlgolen + 1)); - memcpy(notification->encAlgo, encAlgo, encAlgolen); - *(notification->encAlgo + encAlgolen) = '\0'; - atclient_atnotification_encalgo_set_initialized(notification, true); + if (atclient_atnotification_is_enc_algo_initialized(notification)) { + atclient_atnotification_unset_enc_algo(notification); + } + const size_t enc_algo_len = strlen(enc_algo); + const size_t enc_algo_size = enc_algo_len + 1; + notification->enc_algo = malloc(sizeof(char) * (enc_algo_size)); + memcpy(notification->enc_algo, enc_algo, enc_algo_len); + *(notification->enc_algo + enc_algo_len) = '\0'; + atclient_atnotification_enc_algo_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_ivnonce(atclient_atnotification *notification, const char *ivNonce, - const size_t ivNoncelen) { +int atclient_atnotification_set_iv_nonce(atclient_atnotification *notification, const char *iv_nonce) { int ret = 1; - if (atclient_atnotification_is_ivnonce_initialized(notification)) { - atclient_atnotification_unset_ivnonce(notification); + if (atclient_atnotification_is_iv_nonce_initialized(notification)) { + atclient_atnotification_unset_iv_nonce(notification); } - notification->ivNonce = malloc(sizeof(char) * (ivNoncelen + 1)); - if (notification->ivNonce == NULL) { + const size_t iv_nonce_size = strlen(iv_nonce); + const size_t iv_nonce_len = iv_nonce_size + 1; + notification->iv_nonce = malloc(sizeof(char) * (iv_nonce_size)); + if (notification->iv_nonce == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ivNonce\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification iv_nonce\n"); goto exit; } - memcpy(notification->ivNonce, ivNonce, ivNoncelen); - *(notification->ivNonce + ivNoncelen) = '\0'; - atclient_atnotification_ivnonce_set_initialized(notification, true); + memcpy(notification->iv_nonce, iv_nonce, iv_nonce_len); + *(notification->iv_nonce + iv_nonce_len) = '\0'; + atclient_atnotification_iv_nonce_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_skeenckeyname(atclient_atnotification *notification, const char *skeEncKeyName, - const size_t skeEncKeyNamelen) { +int atclient_atnotification_set_ske_enc_key_name(atclient_atnotification *notification, const char *ske_enc_key_name) { int ret = 1; - if (atclient_atnotification_is_skeenckeyname_initialized(notification)) { - atclient_atnotification_unset_skeenckeyname(notification); + if (atclient_atnotification_is_ske_enc_key_name_initialized(notification)) { + atclient_atnotification_unset_ske_enc_key_name(notification); } - notification->skeEncKeyName = malloc(sizeof(char) * (skeEncKeyNamelen + 1)); - if (notification->skeEncKeyName == NULL) { + const size_t ske_enc_key_name_len = strlen(ske_enc_key_name); + const size_t ske_enc_key_name_size = ske_enc_key_name_len + 1; + notification->ske_enc_key_name = malloc(sizeof(char) * (ske_enc_key_name_size)); + if (notification->ske_enc_key_name == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncKeyName\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ske_enc_key_name\n"); goto exit; } - memcpy(notification->skeEncKeyName, skeEncKeyName, skeEncKeyNamelen); - *(notification->skeEncKeyName + skeEncKeyNamelen) = '\0'; - atclient_atnotification_skeenckeyname_set_initialized(notification, true); + memcpy(notification->ske_enc_key_name, ske_enc_key_name, ske_enc_key_name_len); + *(notification->ske_enc_key_name + ske_enc_key_name_len) = '\0'; + atclient_atnotification_ske_enc_key_name_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_skeencalgo(atclient_atnotification *notification, const char *skeEncAlgo, - const size_t skeEncAlgolen) { +int atclient_atnotification_set_ske_enc_algo(atclient_atnotification *notification, const char *ske_enc_algo) { int ret = 1; - if (atclient_atnotification_is_skeencalgo_initialized(notification)) { - atclient_atnotification_unset_skeencalgo(notification); + if (atclient_atnotification_is_ske_enc_algo_initialized(notification)) { + atclient_atnotification_unset_ske_enc_algo(notification); } - notification->skeEncAlgo = malloc(sizeof(char) * (skeEncAlgolen + 1)); - if (notification->skeEncAlgo == NULL) { + const size_t ske_enc_algo_len = strlen(ske_enc_algo); + const size_t ske_enc_algo_size = ske_enc_algo_len + 1; + notification->ske_enc_algo = malloc(sizeof(char) * (ske_enc_algo_size)); + if (notification->ske_enc_algo == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification skeEncAlgo\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification ske_enc_algo\n"); goto exit; } - memcpy(notification->skeEncAlgo, skeEncAlgo, skeEncAlgolen); - *(notification->skeEncAlgo + skeEncAlgolen) = '\0'; - atclient_atnotification_skeencalgo_set_initialized(notification, true); + memcpy(notification->ske_enc_algo, ske_enc_algo, ske_enc_algo_len); + *(notification->ske_enc_algo + ske_enc_algo_len) = '\0'; + atclient_atnotification_ske_enc_algo_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } } -int atclient_atnotification_set_decryptedvalue(atclient_atnotification *notification, - const char *decryptedvalue, const size_t decryptedvaluelen) { +int atclient_atnotification_set_decrypted_value(atclient_atnotification *notification, const char *decrypted_value) { int ret = 1; - if (atclient_atnotification_is_decryptedvalue_initialized(notification)) { - atclient_atnotification_unset_decryptedvalue(notification); + if (atclient_atnotification_is_decrypted_value_initialized(notification)) { + atclient_atnotification_unset_decrypted_value(notification); } - notification->decryptedvalue = malloc(sizeof(unsigned char) * (decryptedvaluelen + 1)); - if (notification->decryptedvalue == NULL) { + const size_t decrypted_value_len = strlen(decrypted_value); + const size_t decrypted_value_size = decrypted_value_len + 1; + notification->decrypted_value = malloc(sizeof(unsigned char) * (decrypted_value_size)); + if (notification->decrypted_value == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification decryptedvalue\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notification decrypted_value\n"); goto exit; } - memcpy(notification->decryptedvalue, decryptedvalue, decryptedvaluelen); - notification->decryptedvalue[decryptedvaluelen] = '\0'; - atclient_atnotification_decryptedvalue_set_initialized(notification, true); + memcpy(notification->decrypted_value, decrypted_value, decrypted_value_len); + notification->decrypted_value[decrypted_value_len] = '\0'; + atclient_atnotification_decrypted_value_set_initialized(notification, true); ret = 0; goto exit; exit: { return ret; } diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index a13f9a0c..9b9ce210 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -42,10 +42,10 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char * char *command = NULL; - const size_t recvsize = 1024; // sufficient buffer size to receive the public key - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + const size_t recv_size = 1024; // sufficient buffer size to receive the public key + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; /* * 3. Generate plookup command @@ -72,7 +72,7 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char * * 4. Send command to atserver */ if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, commandsize - 1, recv, - recvsize, &recvlen)) != 0) { + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -140,8 +140,8 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r char *command = NULL; - const size_t recvsize = 1024; - unsigned char recv[recvsize]; + const size_t recv_size = 1024; + unsigned char recv[recv_size]; const size_t key_raw_encrypted_size = 1024; unsigned char key_raw_encrypted[key_raw_encrypted_size]; @@ -182,10 +182,10 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r /* * 4. Send command to atserver */ - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, commandsize - 1, recv, - recvsize, &recvlen)) != 0) { + recv_size, &recv_len)) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -216,7 +216,7 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r goto exit; } - if ((ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, key_raw_encrypted, key_raw_encrypted_len, + if ((ret = atchops_rsa_decrypt(ctx->atkeys.encrypt_private_key, key_raw_encrypted, key_raw_encrypted_len, key_raw_decrypted, key_raw_decrypted_size, &key_raw_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); goto exit; @@ -265,8 +265,8 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char char *recipient_atsign_with_at = NULL; char *command = NULL; - const size_t recvsize = 1024; - unsigned char recv[recvsize]; + const size_t recv_size = 1024; + unsigned char recv[recv_size]; const size_t shared_encryption_key_encrypted_base64_size = 1024; unsigned char shared_encryption_key_encrypted_base64[shared_encryption_key_encrypted_base64_size]; @@ -298,11 +298,11 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char /* * 4. Send command to atserver */ - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)command, strlen((char *)command), - recv, recvsize, &recvlen)) != 0) { + recv, recv_size, &recv_len)) != 0) { return ret; } @@ -333,7 +333,7 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char memset(shared_encryption_key_encrypted, 0, sizeof(unsigned char) * shared_encryption_key_encrypted_size); size_t shared_encryption_key_encrypted_len = 0; - if ((ret = atchops_rsa_decrypt(ctx->atkeys.encryptprivatekey, shared_encryption_key_encrypted_base64, + if ((ret = atchops_rsa_decrypt(ctx->atkeys.encrypt_private_key, shared_encryption_key_encrypted_base64, shared_encryption_key_encrypted_base64_len, shared_encryption_key_encrypted, shared_encryption_key_encrypted_size, &shared_encryption_key_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); @@ -390,11 +390,11 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( char *sharedwith_atsign_with_at = NULL; // holds their public key in base64 format, non-encrypted - char *publickeybase64 = NULL; + char *public_key_base64 = NULL; // holds their public key in base64 format, non-encrypted (struct) - atchops_rsakey_publickey publickeystruct; - atchops_rsakey_publickey_init(&publickeystruct); + atchops_rsakey_publickey public_key_struct; + atchops_rsakey_publickey_init(&public_key_struct); // the original AES-256 key const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; @@ -424,13 +424,13 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( unsigned char shared_encryption_key_base64_encrypted_for_them_base64 [shared_encryption_key_base64_encrypted_for_them_base64_size]; - char *cmdbuffer1 = NULL; // for us (update:shared_key.sharedwith@sharedby command) - char *cmdbuffer2 = NULL; // for them (update:@sharedwith:shared_key@sharedby command) + char *update_cmd_for_us = NULL; // for us (update:shared_key.shared_with@shared_by command) + char *update_cmd_for_them = NULL; // for them (update:@shared_with:shared_key@shared_by command) - const size_t recvsize = 256; // sufficient to receive response from a update: command - unsigned char recv[recvsize]; - memset(recv, 0, sizeof(unsigned char) * recvsize); - size_t recvlen = 0; + const size_t recv_size = 256; // sufficient to receive response from a update: command + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; /* * 2. Ensure atSigns start with `@` symbol @@ -448,14 +448,14 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( char *sharedwith_atsign_without_at = sharedwith_atsign_with_at + 1; /* - * 3. Get publickey of sharedwith atSign + * 3. Get publickey of shared_with atSign */ - if ((ret = atclient_get_public_encryption_key(atclient, sharedwith_atsign_with_at, &publickeybase64)) != 0) { + if ((ret = atclient_get_public_encryption_key(atclient, sharedwith_atsign_with_at, &public_key_base64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_encryption_key: %d\n", ret); goto exit; } - if ((ret = atchops_rsakey_populate_publickey(&publickeystruct, publickeybase64, strlen(publickeybase64))) != 0) { + if ((ret = atchops_rsakey_populate_publickey(&public_key_struct, public_key_base64, strlen(public_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d\n", ret); goto exit; } @@ -487,7 +487,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( size_t shared_encryption_key_base64_encrypted_for_us_len = 0; memset(shared_encryption_key_base64_encrypted_for_us, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_us_size); - if ((ret = atchops_rsa_encrypt(atclient->atkeys.encryptpublickey, (unsigned char *)shared_encryption_key_base64, + if ((ret = atchops_rsa_encrypt(atclient->atkeys.encrypt_public_key, (unsigned char *)shared_encryption_key_base64, shared_encryption_key_base64_len, shared_encryption_key_base64_encrypted_for_us, shared_encryption_key_base64_encrypted_for_us_size, &shared_encryption_key_base64_encrypted_for_us_len)) != 0) { @@ -513,7 +513,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( size_t shared_encryption_key_base64_encrypted_for_them_len = 0; memset(shared_encryption_key_base64_encrypted_for_them, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_them_size); - if ((ret = atchops_rsa_encrypt(publickeystruct, shared_encryption_key_base64, shared_encryption_key_base64_len, + if ((ret = atchops_rsa_encrypt(public_key_struct, shared_encryption_key_base64, shared_encryption_key_base64_len, shared_encryption_key_base64_encrypted_for_them, shared_encryption_key_base64_encrypted_for_them_size, &shared_encryption_key_base64_encrypted_for_them_len)) != 0) { @@ -537,28 +537,28 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( * 6. Build `update:` commands */ // 6a. `update:shared_key.recipient_atsign@myatsign ` - const size_t cmdbuffersize1 = strlen("update:shared_key.") + strlen(sharedwith_atsign_without_at) + + const size_t update_cmd_for_us_size = strlen("update:shared_key.") + strlen(sharedwith_atsign_without_at) + strlen(sharedby_atsign_with_at) + strlen(" ") + shared_encryption_key_base64_encrypted_for_us_base64_len + strlen("\r\n") + 1; - if ((cmdbuffer1 = (char *)malloc(sizeof(char) * cmdbuffersize1)) == NULL) { + if ((update_cmd_for_us = (char *)malloc(sizeof(char) * update_cmd_for_us_size)) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer1\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd_for_us\n"); goto exit; } - snprintf(cmdbuffer1, cmdbuffersize1, "update:shared_key.%s%s %s\r\n", sharedwith_atsign_without_at, + snprintf(update_cmd_for_us, update_cmd_for_us_size, "update:shared_key.%s%s %s\r\n", sharedwith_atsign_without_at, sharedby_atsign_with_at, shared_encryption_key_base64_encrypted_for_us_base64); - // 6b. `update:@sharedwith:shared_key@sharedby ` - const size_t cmdbuffersize2 = strlen("update:") + strlen(sharedwith_atsign_with_at) + strlen(":shared_key") + + // 6b. `update:@shared_with:shared_key@shared_by ` + const size_t update_cmd_for_them_size = strlen("update:") + strlen(sharedwith_atsign_with_at) + strlen(":shared_key") + strlen(sharedby_atsign_with_at) + strlen(" ") + shared_encryption_key_base64_encrypted_for_them_base64_len + strlen("\r\n") + 1; - if ((cmdbuffer2 = (char *)malloc(sizeof(char) * cmdbuffersize2)) == NULL) { + if ((update_cmd_for_them = (char *)malloc(sizeof(char) * update_cmd_for_them_size)) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for cmdbuffer2\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd_for_them\n"); goto exit; } - snprintf(cmdbuffer2, cmdbuffersize2, "update:%s:shared_key%s %s\r\n", sharedwith_atsign_with_at, + snprintf(update_cmd_for_them, update_cmd_for_them_size, "update:%s:shared_key%s %s\r\n", sharedwith_atsign_with_at, sharedby_atsign_with_at, shared_encryption_key_base64_encrypted_for_them_base64); /* @@ -566,8 +566,8 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( */ // 7a. Our key - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer1, cmdbuffersize1 - 1, - recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)update_cmd_for_us, update_cmd_for_us_size - 1, + recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -575,16 +575,16 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } - memset(recv, 0, sizeof(unsigned char) * recvsize); - recvlen = 0; + memset(recv, 0, sizeof(unsigned char) * recv_size); + recv_len = 0; // 7b. Their key - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)cmdbuffer2, cmdbuffersize2 - 1, - recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)update_cmd_for_them, update_cmd_for_them_size - 1, + recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -592,7 +592,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( if (!atclient_stringutils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -606,10 +606,10 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( exit: { free(sharedby_atsign_with_at); free(sharedwith_atsign_with_at); - atchops_rsakey_publickey_free(&publickeystruct); - free(publickeybase64); - free(cmdbuffer1); - free(cmdbuffer2); + atchops_rsakey_publickey_free(&public_key_struct); + free(public_key_base64); + free(update_cmd_for_us); + free(update_cmd_for_them); return ret; } } diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 1b5647d7..955125bf 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -9,121 +9,121 @@ #define TAG "metadata" -static bool is_createdby_initialized(const atclient_atkey_metadata *metadata); -static bool is_updatedby_initialized(const atclient_atkey_metadata *metadata); +static bool is_created_by_initialized(const atclient_atkey_metadata *metadata); +static bool is_updated_by_initialized(const atclient_atkey_metadata *metadata); static bool is_status_initialized(const atclient_atkey_metadata *metadata); static bool is_version_initialized(const atclient_atkey_metadata *metadata); -static bool is_expiresat_initialized(const atclient_atkey_metadata *metadata); -static bool is_availableat_initialized(const atclient_atkey_metadata *metadata); -static bool is_refreshat_initialized(const atclient_atkey_metadata *metadata); -static bool is_createdat_initialized(const atclient_atkey_metadata *metadata); -static bool is_updatedat_initialized(const atclient_atkey_metadata *metadata); -static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata); -static bool is_iscached_initialized(const atclient_atkey_metadata *metadata); +static bool is_expires_at_initialized(const atclient_atkey_metadata *metadata); +static bool is_available_at_initialized(const atclient_atkey_metadata *metadata); +static bool is_refresh_at_initialized(const atclient_atkey_metadata *metadata); +static bool is_created_at_initialized(const atclient_atkey_metadata *metadata); +static bool is_updated_at_initialized(const atclient_atkey_metadata *metadata); +static bool is_is_public_initialized(const atclient_atkey_metadata *metadata); +static bool is_is_cached_initialized(const atclient_atkey_metadata *metadata); static bool is_ttl_initialized(const atclient_atkey_metadata *metadata); static bool is_ttb_initialized(const atclient_atkey_metadata *metadata); static bool is_ttr_initialized(const atclient_atkey_metadata *metadata); static bool is_ccd_initialized(const atclient_atkey_metadata *metadata); -static bool is_isbinary_initialized(const atclient_atkey_metadata *metadata); -static bool is_isencrypted_initialized(const atclient_atkey_metadata *metadata); -static bool is_datasignature_initialized(const atclient_atkey_metadata *metadata); -static bool is_sharedkeystatus_initialized(const atclient_atkey_metadata *metadata); -static bool is_sharedkeyenc_initialized(const atclient_atkey_metadata *metadata); -static bool is_pubkeyhash_initialized(const atclient_atkey_metadata *metadata); -static bool is_pubkeyalgo_initialized(const atclient_atkey_metadata *metadata); +static bool is_is_binary_initialized(const atclient_atkey_metadata *metadata); +static bool is_is_encrypted_initialized(const atclient_atkey_metadata *metadata); +static bool is_data_signature_initialized(const atclient_atkey_metadata *metadata); +static bool is_shared_key_status_initialized(const atclient_atkey_metadata *metadata); +static bool is_shared_key_enc_initialized(const atclient_atkey_metadata *metadata); +static bool is_pub_key_hash_initialized(const atclient_atkey_metadata *metadata); +static bool is_pub_key_algo_initialized(const atclient_atkey_metadata *metadata); static bool is_encoding_initialized(const atclient_atkey_metadata *metadata); -static bool is_enckeyname_initialized(const atclient_atkey_metadata *metadata); -static bool is_encalgo_initialized(const atclient_atkey_metadata *metadata); -static bool is_ivnonce_initialized(const atclient_atkey_metadata *metadata); -static bool is_skeenckeyname_initialized(const atclient_atkey_metadata *metadata); -static bool is_skeencalgo_initialized(const atclient_atkey_metadata *metadata); - -static void set_is_createdby_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_updatedby_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static bool is_enc_key_name_initialized(const atclient_atkey_metadata *metadata); +static bool is_enc_algo_initialized(const atclient_atkey_metadata *metadata); +static bool is_iv_nonce_initialized(const atclient_atkey_metadata *metadata); +static bool is_ske_enc_key_name_initialized(const atclient_atkey_metadata *metadata); +static bool is_ske_enc_algo_initialized(const atclient_atkey_metadata *metadata); + +static void set_is_created_by_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_updated_by_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_version_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_availableat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_expiresat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_refreshat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_createdat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_updatedat_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_available_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_expires_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_refresh_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_created_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_updated_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_is_public_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_is_cached_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ttl_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ttb_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ttr_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_ccd_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_isbinary_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_isencrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_datasignature_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_sharedkeystatus_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_sharedkeyenc_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_pubkeyhash_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_pubkeyalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_is_binary_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_is_encrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_data_signature_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_shared_key_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_shared_key_enc_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_pub_key_hash_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_pub_key_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); static void set_is_encoding_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_enckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_encalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_ivnonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_skeenckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized); -static void set_is_skeencalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); - -static void unset_createdby(atclient_atkey_metadata *metadata); -static void unset_updatedby(atclient_atkey_metadata *metadata); +static void set_is_enc_key_name_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_enc_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_iv_nonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_ske_enc_key_name_initialized(atclient_atkey_metadata *metadata, bool is_initialized); +static void set_is_ske_enc_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized); + +static void unset_created_by(atclient_atkey_metadata *metadata); +static void unset_updated_by(atclient_atkey_metadata *metadata); static void unset_status(atclient_atkey_metadata *metadata); static void unset_version(atclient_atkey_metadata *metadata); -static void unset_expiresat(atclient_atkey_metadata *metadata); -static void unset_availableat(atclient_atkey_metadata *metadata); -static void unset_refreshat(atclient_atkey_metadata *metadata); -static void unset_createdat(atclient_atkey_metadata *metadata); -static void unset_updatedat(atclient_atkey_metadata *metadata); -static void unset_ispublic(atclient_atkey_metadata *metadata); -static void unset_iscached(atclient_atkey_metadata *metadata); +static void unset_expires_at(atclient_atkey_metadata *metadata); +static void unset_available_at(atclient_atkey_metadata *metadata); +static void unset_refresh_at(atclient_atkey_metadata *metadata); +static void unset_created_at(atclient_atkey_metadata *metadata); +static void unset_updated_at(atclient_atkey_metadata *metadata); +static void unset_is_public(atclient_atkey_metadata *metadata); +static void unset_is_cached(atclient_atkey_metadata *metadata); static void unset_ttl(atclient_atkey_metadata *metadata); static void unset_ttb(atclient_atkey_metadata *metadata); static void unset_ttr(atclient_atkey_metadata *metadata); static void unset_ccd(atclient_atkey_metadata *metadata); -static void unset_isbinary(atclient_atkey_metadata *metadata); -static void unset_isencrypted(atclient_atkey_metadata *metadata); -static void unset_datasignature(atclient_atkey_metadata *metadata); -static void unset_sharedkeystatus(atclient_atkey_metadata *metadata); -static void unset_sharedkeyenc(atclient_atkey_metadata *metadata); -static void unset_pubkeyhash(atclient_atkey_metadata *metadata); -static void unset_pubkeyalgo(atclient_atkey_metadata *metadata); +static void unset_is_binary(atclient_atkey_metadata *metadata); +static void unset_is_encrypted(atclient_atkey_metadata *metadata); +static void unset_data_signature(atclient_atkey_metadata *metadata); +static void unset_shared_key_status(atclient_atkey_metadata *metadata); +static void unset_shared_key_enc(atclient_atkey_metadata *metadata); +static void unset_pub_key_hash(atclient_atkey_metadata *metadata); +static void unset_pub_key_algo(atclient_atkey_metadata *metadata); static void unset_encoding(atclient_atkey_metadata *metadata); -static void unset_enckeyname(atclient_atkey_metadata *metadata); -static void unset_encalgo(atclient_atkey_metadata *metadata); -static void unset_ivnonce(atclient_atkey_metadata *metadata); -static void unset_skeenckeyname(atclient_atkey_metadata *metadata); -static void unset_skeencalgo(atclient_atkey_metadata *metadata); - -static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby); -static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby); +static void unset_enc_key_name(atclient_atkey_metadata *metadata); +static void unset_enc_algo(atclient_atkey_metadata *metadata); +static void unset_iv_nonce(atclient_atkey_metadata *metadata); +static void unset_ske_enc_key_name(atclient_atkey_metadata *metadata); +static void unset_ske_enc_algo(atclient_atkey_metadata *metadata); + +static int set_created_by(atclient_atkey_metadata *metadata, const char *created_by); +static int set_updated_by(atclient_atkey_metadata *metadata, const char *updated_by); static int set_status(atclient_atkey_metadata *metadata, const char *status); static void set_version(atclient_atkey_metadata *metadata, int version); -static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat); -static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat); -static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat); -static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat); -static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat); -static void set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic); -static void set_iscached(atclient_atkey_metadata *metadata, const bool iscached); +static int set_expires_at(atclient_atkey_metadata *metadata, const char *expires_at); +static int set_available_at(atclient_atkey_metadata *metadata, const char *available_at); +static int set_refresh_at(atclient_atkey_metadata *metadata, const char *refresh_at); +static int set_created_at(atclient_atkey_metadata *metadata, const char *created_at); +static int set_updated_at(atclient_atkey_metadata *metadata, const char *updated_at); +static void set_is_public(atclient_atkey_metadata *metadata, const bool is_public); +static void set_is_cached(atclient_atkey_metadata *metadata, const bool is_cached); static void set_ttl(atclient_atkey_metadata *metadata, const long ttl); static void set_ttb(atclient_atkey_metadata *metadata, const long ttb); static void set_ttr(atclient_atkey_metadata *metadata, const long ttr); static void set_ccd(atclient_atkey_metadata *metadata, const bool ccd); -static void set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary); -static void set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted); -static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature); -static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus); -static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc); -static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash); -static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo); +static void set_is_binary(atclient_atkey_metadata *metadata, const bool is_binary); +static void set_is_encrypted(atclient_atkey_metadata *metadata, const bool is_encrypted); +static int set_data_signature(atclient_atkey_metadata *metadata, const char *data_signature); +static int set_shared_key_status(atclient_atkey_metadata *metadata, const char *shared_key_status); +static int set_shared_key_enc(atclient_atkey_metadata *metadata, const char *shared_key_enc); +static int set_pub_key_hash(atclient_atkey_metadata *metadata, const char *pub_key_hash); +static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pub_key_algo); static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding); -static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname); -static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo); -static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce); -static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname); -static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo); +static int set_enc_key_name(atclient_atkey_metadata *metadata, const char *enc_key_name); +static int set_enc_algo(atclient_atkey_metadata *metadata, const char *enc_algo); +static int set_iv_nonce(atclient_atkey_metadata *metadata, const char *iv_nonce); +static int set_ske_enc_key_name(atclient_atkey_metadata *metadata, const char *ske_enc_key_name); +static int set_ske_enc_algo(atclient_atkey_metadata *metadata, const char *ske_enc_algo); void atclient_atkey_metadata_init(atclient_atkey_metadata *metadata) { memset(metadata, 0, sizeof(atclient_atkey_metadata)); @@ -142,186 +142,202 @@ int atclient_atkey_metadata_clone(atclient_atkey_metadata *dst, const atclient_a goto exit; } - if (atclient_atkey_metadata_is_createdby_initialized(src)) { - if ((ret = set_createdby(dst, src->createdby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + if (atclient_atkey_metadata_is_created_by_initialized(src)) { + if ((ret = set_created_by(dst, src->created_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_updatedby_initialized(src)) { - if((ret = set_updatedby(dst, src->updatedby)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + if (atclient_atkey_metadata_is_updated_by_initialized(src)) { + if ((ret = set_updated_by(dst, src->updated_by)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_status_initialized(src)) { - if((ret = set_status(dst, src->status)) != 0) { + if (atclient_atkey_metadata_is_status_initialized(src)) { + if ((ret = set_status(dst, src->status)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_status: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_version_initialized(src)) { + if (atclient_atkey_metadata_is_version_initialized(src)) { set_version(dst, src->version); } - if(atclient_atkey_metadata_is_expiresat_initialized(src)) { - if((ret = set_expiresat(dst, src->expiresat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + if (atclient_atkey_metadata_is_expires_at_initialized(src)) { + if ((ret = set_expires_at(dst, src->expires_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_availableat_initialized(src)) { - if((ret = set_availableat(dst, src->availableat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + if (atclient_atkey_metadata_is_available_at_initialized(src)) { + if ((ret = set_available_at(dst, src->available_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_refreshat_initialized(src)) { - if((ret = set_refreshat(dst, src->refreshat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + if (atclient_atkey_metadata_is_refresh_at_initialized(src)) { + if ((ret = set_refresh_at(dst, src->refresh_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_createdat_initialized(src)) { - if((ret = set_createdat(dst, src->createdat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + if (atclient_atkey_metadata_is_created_at_initialized(src)) { + if ((ret = set_created_at(dst, src->created_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_updatedat_initialized(src)) { - if((ret = set_updatedat(dst, src->updatedat)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + if (atclient_atkey_metadata_is_updated_at_initialized(src)) { + if ((ret = set_updated_at(dst, src->updated_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_ispublic_initialized(src)) { - set_ispublic(dst, src->ispublic); + if (atclient_atkey_metadata_is_is_public_initialized(src)) { + set_is_public(dst, src->is_public); } - if(atclient_atkey_metadata_is_iscached_initialized(src)) { - set_iscached(dst, src->iscached); + if (atclient_atkey_metadata_is_is_cached_initialized(src)) { + set_is_cached(dst, src->is_cached); } - if(atclient_atkey_metadata_is_ttl_initialized(src)) { + if (atclient_atkey_metadata_is_ttl_initialized(src)) { set_ttl(dst, src->ttl); } - if(atclient_atkey_metadata_is_ttb_initialized(src)) { + if (atclient_atkey_metadata_is_ttb_initialized(src)) { set_ttb(dst, src->ttb); } - if(atclient_atkey_metadata_is_ttr_initialized(src)) { + if (atclient_atkey_metadata_is_ttr_initialized(src)) { set_ttr(dst, src->ttr); } - if(atclient_atkey_metadata_is_ccd_initialized(src)) { + if (atclient_atkey_metadata_is_ccd_initialized(src)) { set_ccd(dst, src->ccd); } - if(atclient_atkey_metadata_is_isbinary_initialized(src)) { - set_isbinary(dst, src->isbinary); + if (atclient_atkey_metadata_is_is_binary_initialized(src)) { + set_is_binary(dst, src->is_binary); } - if(atclient_atkey_metadata_is_isencrypted_initialized(src)) { - set_isencrypted(dst, src->isencrypted); + if (atclient_atkey_metadata_is_is_encrypted_initialized(src)) { + set_is_encrypted(dst, src->is_encrypted); } - if(atclient_atkey_metadata_is_datasignature_initialized(src)) { - if((ret = set_datasignature(dst, src->datasignature)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + if (atclient_atkey_metadata_is_data_signature_initialized(src)) { + if ((ret = set_data_signature(dst, src->data_signature)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_sharedkeystatus_initialized(src)) { - if((ret = set_sharedkeystatus(dst, src->sharedkeystatus)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + if (atclient_atkey_metadata_is_shared_key_status_initialized(src)) { + if ((ret = set_shared_key_status(dst, src->shared_key_status)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_sharedkeyenc_initialized(src)) { - if((ret = set_sharedkeyenc(dst, src->sharedkeyenc)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + if (atclient_atkey_metadata_is_shared_key_enc_initialized(src)) { + if ((ret = set_shared_key_enc(dst, src->shared_key_enc)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_pubkeyhash_initialized(src)) { - if((ret = set_pubkeyhash(dst, src->pubkeyhash)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + if (atclient_atkey_metadata_is_pub_key_hash_initialized(src)) { + if ((ret = set_pub_key_hash(dst, src->pub_key_hash)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_pubkeyalgo_initialized(src)) { - if((ret = set_pubkeyalgo(dst, src->pubkeyalgo)) != 0) { + if (atclient_atkey_metadata_is_pub_key_algo_initialized(src)) { + if ((ret = set_pubkeyalgo(dst, src->pub_key_algo)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_encoding_initialized(src)) { - if((ret = set_encoding(dst, src->encoding)) != 0) { + if (atclient_atkey_metadata_is_encoding_initialized(src)) { + if ((ret = set_encoding(dst, src->encoding)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_enckeyname_initialized(src)) { - if((ret = set_enckeyname(dst, src->enckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + if (atclient_atkey_metadata_is_enc_key_name_initialized(src)) { + if ((ret = set_enc_key_name(dst, src->enc_key_name)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_encalgo_initialized(src)) { - if((ret = set_encalgo(dst, src->encalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + if (atclient_atkey_metadata_is_enc_algo_initialized(src)) { + if ((ret = set_enc_algo(dst, src->enc_algo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_ivnonce_initialized(src)) { - if((ret = set_ivnonce(dst, src->ivnonce)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + if (atclient_atkey_metadata_is_iv_nonce_initialized(src)) { + if ((ret = set_iv_nonce(dst, src->iv_nonce)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_skeenckeyname_initialized(src)) { - if((ret = set_skeenckeyname(dst, src->skeenckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(src)) { + if ((ret = set_ske_enc_key_name(dst, src->ske_enc_key_name)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); goto exit; } } - if(atclient_atkey_metadata_is_skeencalgo_initialized(src)) { - if((ret = set_skeencalgo(dst, src->skeencalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(src)) { + if ((ret = set_ske_enc_algo(dst, src->ske_enc_algo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); goto exit; } } ret = 0; goto exit; -exit: { - return ret; -} +exit: { return ret; } } -int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, const char *metadatastr) { +int atclient_atkey_metadata_from_json_str(atclient_atkey_metadata *metadata, const char *metadata_str) { int ret = 1; - cJSON *root = cJSON_Parse(metadatastr); + /* + * 1. Validate arguments + */ + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); + return ret; + } + + if (metadata_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); + return ret; + } + + /* + * 2. Parse JSON string + */ + cJSON *root = cJSON_Parse(metadata_str); if (root == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n"); goto exit; @@ -334,7 +350,6 @@ int atclient_atkey_metadata_from_jsonstr(atclient_atkey_metadata *metadata, cons ret = 0; goto exit; - exit: { cJSON_Delete(root); return ret; @@ -344,41 +359,51 @@ exit: { int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json) { int ret = 1; - if(json == NULL) { + /* + * 1. Validate arguments + */ + + if (json == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "json is NULL\n"); - goto exit; + return ret; } - if(metadata == NULL) { + if (metadata == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - goto exit; + return ret; } - cJSON *createdby = cJSON_GetObjectItem(json, "createdBy"); - if (createdby != NULL) { - if (createdby->type != cJSON_NULL) { - if ((ret = set_createdby(metadata, createdby->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + /* + * 2. Parse JSON node + */ + + cJSON *created_by = cJSON_GetObjectItem(json, "createdBy"); + if (created_by != NULL) { + if (created_by->type != cJSON_NULL) { + if ((ret = set_created_by(metadata, created_by->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); goto exit; } } else { - if ((ret = set_createdby(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby: %d\n", ret); + if ((ret = set_created_by(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by: %d\n", ret); goto exit; } } } - cJSON *updatedby = cJSON_GetObjectItem(json, "updatedBy"); - if (updatedby != NULL) { - if (updatedby->type != cJSON_NULL) { - if ((ret = set_updatedby(metadata, updatedby->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + cJSON *updated_by = cJSON_GetObjectItem(json, "updatedBy"); + if (updated_by != NULL) { + if (updated_by->type != cJSON_NULL) { + if ((ret = set_updated_by(metadata, updated_by->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); goto exit; } } else { - if ((ret = set_updatedby(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby: %d\n", ret); + if ((ret = set_updated_by(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by: %d\n", ret); goto exit; } } @@ -408,91 +433,91 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *expiresat = cJSON_GetObjectItem(json, "expiresAt"); - if (expiresat != NULL) { - if (expiresat->type != cJSON_NULL) { - if ((ret = set_expiresat(metadata, expiresat->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + cJSON *expires_at = cJSON_GetObjectItem(json, "expiresAt"); + if (expires_at != NULL) { + if (expires_at->type != cJSON_NULL) { + if ((ret = set_expires_at(metadata, expires_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); goto exit; } } else { - if ((ret = set_expiresat(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat: %d\n", ret); + if ((ret = set_expires_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at: %d\n", ret); goto exit; } } } - cJSON *availableat = cJSON_GetObjectItem(json, "availableAt"); - if (availableat != NULL) { - if (availableat->type != cJSON_NULL) { - if ((ret = set_availableat(metadata, availableat->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + cJSON *available_at = cJSON_GetObjectItem(json, "availableAt"); + if (available_at != NULL) { + if (available_at->type != cJSON_NULL) { + if ((ret = set_available_at(metadata, available_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); goto exit; } } else { - if ((ret = set_availableat(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat: %d\n", ret); + if ((ret = set_available_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at: %d\n", ret); goto exit; } } } - cJSON *refreshat = cJSON_GetObjectItem(json, "refreshAt"); - if (refreshat != NULL) { - if (refreshat->type != cJSON_NULL) { - if((ret = set_refreshat(metadata, refreshat->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + cJSON *refresh_at = cJSON_GetObjectItem(json, "refreshAt"); + if (refresh_at != NULL) { + if (refresh_at->type != cJSON_NULL) { + if ((ret = set_refresh_at(metadata, refresh_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); goto exit; } } else { - if((ret = set_refreshat(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat: %d\n", ret); + if ((ret = set_refresh_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at: %d\n", ret); goto exit; } } } - cJSON *createdat = cJSON_GetObjectItem(json, "createdAt"); - if (createdat != NULL) { - if (createdat->type != cJSON_NULL) { - if((ret = set_createdat(metadata, createdat->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + cJSON *created_at = cJSON_GetObjectItem(json, "createdAt"); + if (created_at != NULL) { + if (created_at->type != cJSON_NULL) { + if ((ret = set_created_at(metadata, created_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); goto exit; } } else { - if((ret = set_createdat(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat: %d\n", ret); + if ((ret = set_created_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at: %d\n", ret); goto exit; } } } - cJSON *updatedat = cJSON_GetObjectItem(json, "updatedAt"); - if (updatedat != NULL) { - if (updatedat->type != cJSON_NULL) { - if((ret = set_updatedat(metadata, updatedat->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + cJSON *updated_at = cJSON_GetObjectItem(json, "updatedAt"); + if (updated_at != NULL) { + if (updated_at->type != cJSON_NULL) { + if ((ret = set_updated_at(metadata, updated_at->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); goto exit; } } else { - if((ret = set_updatedat(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat: %d\n", ret); + if ((ret = set_updated_at(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at: %d\n", ret); goto exit; } } } // I don't think this field exists when reading metadata from atServer - // cJSON *ispublic = cJSON_GetObjectItem(root, "isPublic"); - // if(ispublic != NULL) { - // set_ispublic(metadata, ispublic->valueint); + // cJSON *is_public = cJSON_GetObjectItem(root, "isPublic"); + // if(is_public != NULL) { + // set_is_public(metadata, is_public->valueint); // } // I don't think this field exists when reading metadata from atServer - // cJSON *iscached = cJSON_GetObjectItem(root, "isCached - // if(iscached != NULL) { - // set_iscached(metadata, iscached->valueint); + // cJSON *is_cached = cJSON_GetObjectItem(root, "isCached + // if(is_cached != NULL) { + // set_is_cached(metadata, is_cached->valueint); // } cJSON *ttl = cJSON_GetObjectItem(json, "ttl"); @@ -531,93 +556,93 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *isbinary = cJSON_GetObjectItem(json, "isBinary"); - if (isbinary != NULL) { - if (isbinary->type != cJSON_NULL) { - set_isbinary(metadata, isbinary->valueint); + cJSON *is_binary = cJSON_GetObjectItem(json, "isBinary"); + if (is_binary != NULL) { + if (is_binary->type != cJSON_NULL) { + set_is_binary(metadata, is_binary->valueint); } else { - set_isbinary(metadata, 0); + set_is_binary(metadata, 0); } } - cJSON *isencrypted = cJSON_GetObjectItem(json, "isEncrypted"); - if (isencrypted != NULL) { - if (isencrypted->type != cJSON_NULL) { - set_isencrypted(metadata, isencrypted->valueint); + cJSON *is_encrypted = cJSON_GetObjectItem(json, "is_encrypted"); + if (is_encrypted != NULL) { + if (is_encrypted->type != cJSON_NULL) { + set_is_encrypted(metadata, is_encrypted->valueint); } else { - set_isencrypted(metadata, 0); + set_is_encrypted(metadata, 0); } } - cJSON *datasignature = cJSON_GetObjectItem(json, "dataSignature"); - if (datasignature != NULL) { - if (datasignature->type != cJSON_NULL) { - if((ret = set_datasignature(metadata, datasignature->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + cJSON *data_signature = cJSON_GetObjectItem(json, "dataSignature"); + if (data_signature != NULL) { + if (data_signature->type != cJSON_NULL) { + if ((ret = set_data_signature(metadata, data_signature->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); goto exit; } } else { - if((ret = set_datasignature(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature: %d\n", ret); + if ((ret = set_data_signature(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature: %d\n", ret); goto exit; } } } - cJSON *sharedkeystatus = cJSON_GetObjectItem(json, "sharedKeyStatus"); - if (sharedkeystatus != NULL) { - if (sharedkeystatus->type != cJSON_NULL) { - if((ret = set_sharedkeystatus(metadata, sharedkeystatus->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + cJSON *shared_key_status = cJSON_GetObjectItem(json, "sharedKeyStatus"); + if (shared_key_status != NULL) { + if (shared_key_status->type != cJSON_NULL) { + if ((ret = set_shared_key_status(metadata, shared_key_status->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); goto exit; } } else { - if((ret = set_sharedkeystatus(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus: %d\n", ret); + if ((ret = set_shared_key_status(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status: %d\n", ret); goto exit; } } } - cJSON *sharedkeyenc = cJSON_GetObjectItem(json, "sharedKeyEnc"); - if (sharedkeyenc != NULL) { - if (sharedkeyenc->type != cJSON_NULL) { - if((ret = set_sharedkeyenc(metadata, sharedkeyenc->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + cJSON *shared_key_enc = cJSON_GetObjectItem(json, "sharedKeyEnc"); + if (shared_key_enc != NULL) { + if (shared_key_enc->type != cJSON_NULL) { + if ((ret = set_shared_key_enc(metadata, shared_key_enc->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); goto exit; } } else { - if((ret = set_sharedkeyenc(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc: %d\n", ret); + if ((ret = set_shared_key_enc(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc: %d\n", ret); goto exit; } } } - cJSON *pubkeyhash = cJSON_GetObjectItem(json, "pubKeyHash"); - if (pubkeyhash != NULL) { - if (pubkeyhash->type != cJSON_NULL) { - if((ret = set_pubkeyhash(metadata, pubkeyhash->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + cJSON *pub_key_hash = cJSON_GetObjectItem(json, "pubKeyHash"); + if (pub_key_hash != NULL) { + if (pub_key_hash->type != cJSON_NULL) { + if ((ret = set_pub_key_hash(metadata, pub_key_hash->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); goto exit; } } else { - if((ret = set_pubkeyhash(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash: %d\n", ret); + if ((ret = set_pub_key_hash(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash: %d\n", ret); goto exit; } } } - cJSON *pubkeyalgo = cJSON_GetObjectItem(json, "pubKeyAlgo"); - if (pubkeyalgo != NULL) { - if (pubkeyalgo->type != cJSON_NULL) { - if((ret = set_pubkeyalgo(metadata, pubkeyalgo->valuestring)) != 0) { + cJSON *pub_key_algo = cJSON_GetObjectItem(json, "pubKeyAlgo"); + if (pub_key_algo != NULL) { + if (pub_key_algo->type != cJSON_NULL) { + if ((ret = set_pubkeyalgo(metadata, pub_key_algo->valuestring)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); goto exit; } } else { - if((ret = set_pubkeyalgo(metadata, "null")) != 0) { + if ((ret = set_pubkeyalgo(metadata, "null")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo: %d\n", ret); goto exit; } @@ -627,88 +652,88 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c cJSON *encoding = cJSON_GetObjectItem(json, "encoding"); if (encoding != NULL) { if (encoding->type != cJSON_NULL) { - if((ret = set_encoding(metadata, encoding->valuestring)) != 0) { + if ((ret = set_encoding(metadata, encoding->valuestring)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); goto exit; } } else { - if((ret = set_encoding(metadata, "null")) != 0) { + if ((ret = set_encoding(metadata, "null")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding: %d\n", ret); goto exit; } } } - cJSON *enckeyname = cJSON_GetObjectItem(json, "encKeyName"); - if (enckeyname != NULL) { - if (enckeyname->type != cJSON_NULL) { - if((ret = set_enckeyname(metadata, enckeyname->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + cJSON *enc_key_name = cJSON_GetObjectItem(json, "enc_key_name"); + if (enc_key_name != NULL) { + if (enc_key_name->type != cJSON_NULL) { + if ((ret = set_enc_key_name(metadata, enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); goto exit; } } else { - if((ret = set_enckeyname(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname: %d\n", ret); + if ((ret = set_enc_key_name(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name: %d\n", ret); goto exit; } } } - cJSON *encalgo = cJSON_GetObjectItem(json, "encAlgo"); - if (encalgo != NULL) { - if (encalgo->type != cJSON_NULL) { - if((ret = set_encalgo(metadata, encalgo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + cJSON *enc_algo = cJSON_GetObjectItem(json, "enc_algo"); + if (enc_algo != NULL) { + if (enc_algo->type != cJSON_NULL) { + if ((ret = set_enc_algo(metadata, enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); goto exit; } } else { - if((ret = set_encalgo(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo: %d\n", ret); + if ((ret = set_enc_algo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo: %d\n", ret); goto exit; } } } - cJSON *ivnonce = cJSON_GetObjectItem(json, "ivNonce"); - if (ivnonce != NULL) { - if (ivnonce->type != cJSON_NULL) { - if((ret = set_ivnonce(metadata, ivnonce->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + cJSON *iv_nonce = cJSON_GetObjectItem(json, "iv_nonce"); + if (iv_nonce != NULL) { + if (iv_nonce->type != cJSON_NULL) { + if ((ret = set_iv_nonce(metadata, iv_nonce->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); goto exit; } } else { - if((ret = set_ivnonce(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce: %d\n", ret); + if ((ret = set_iv_nonce(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce: %d\n", ret); goto exit; } } } - cJSON *skeenckeyname = cJSON_GetObjectItem(json, "skeEncKeyName"); - if (skeenckeyname != NULL) { - if (skeenckeyname->type != cJSON_NULL) { - if((ret = set_skeenckeyname(metadata, skeenckeyname->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + cJSON *ske_enc_key_name = cJSON_GetObjectItem(json, "ske_enc_key_name"); + if (ske_enc_key_name != NULL) { + if (ske_enc_key_name->type != cJSON_NULL) { + if ((ret = set_ske_enc_key_name(metadata, ske_enc_key_name->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); goto exit; } } else { - if((ret = set_skeenckeyname(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname: %d\n", ret); + if ((ret = set_ske_enc_key_name(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name: %d\n", ret); goto exit; } } } - cJSON *skeencalgo = cJSON_GetObjectItem(json, "skeEncAlgo"); - if (skeencalgo != NULL) { - if (skeencalgo->type != cJSON_NULL) { - if((ret = set_skeencalgo(metadata, skeencalgo->valuestring)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + cJSON *ske_enc_algo = cJSON_GetObjectItem(json, "ske_enc_algo"); + if (ske_enc_algo != NULL) { + if (ske_enc_algo->type != cJSON_NULL) { + if ((ret = set_ske_enc_algo(metadata, ske_enc_algo->valuestring)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); goto exit; } } else { - if((ret = set_skeencalgo(metadata, "null")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo: %d\n", ret); + if ((ret = set_ske_enc_algo(metadata, "null")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo: %d\n", ret); goto exit; } } @@ -719,22 +744,28 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c exit: { return ret; } } -int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, char **metadatastr) { +int atclient_atkey_metadata_to_json_str(const atclient_atkey_metadata *metadata, char **metadata_str) { int ret = 1; + /* + * 1. Validate arguments + */ if (metadata == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); return ret; } - if (metadatastr == NULL) { + if (metadata_str == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); return ret; } - char *jsonstr = NULL; + /* + * 2. Create JSON string + */ + char *json_str = NULL; cJSON *root = cJSON_CreateObject(); if (root == NULL) { ret = 1; @@ -742,12 +773,12 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, goto exit; } - if (atclient_atkey_metadata_is_createdby_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdBy", metadata->createdby); + if (atclient_atkey_metadata_is_created_by_initialized(metadata)) { + cJSON_AddStringToObject(root, "createdBy", metadata->created_by); } - if (atclient_atkey_metadata_is_updatedby_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedBy", metadata->updatedby); + if (atclient_atkey_metadata_is_updated_by_initialized(metadata)) { + cJSON_AddStringToObject(root, "updatedBy", metadata->updated_by); } if (atclient_atkey_metadata_is_status_initialized(metadata)) { @@ -758,32 +789,32 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, cJSON_AddNumberToObject(root, "version", metadata->version); } - if (atclient_atkey_metadata_is_expiresat_initialized(metadata)) { - cJSON_AddStringToObject(root, "expiresAt", metadata->expiresat); + if (atclient_atkey_metadata_is_expires_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "expiresAt", metadata->expires_at); } - if (atclient_atkey_metadata_is_availableat_initialized(metadata)) { - cJSON_AddStringToObject(root, "availableAt", metadata->availableat); + if (atclient_atkey_metadata_is_available_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "availableAt", metadata->available_at); } - if (atclient_atkey_metadata_is_refreshat_initialized(metadata)) { - cJSON_AddStringToObject(root, "refreshAt", metadata->refreshat); + if (atclient_atkey_metadata_is_refresh_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "refreshAt", metadata->refresh_at); } - if (atclient_atkey_metadata_is_createdat_initialized(metadata)) { - cJSON_AddStringToObject(root, "createdAt", metadata->createdat); + if (atclient_atkey_metadata_is_created_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "createdAt", metadata->created_at); } - if (atclient_atkey_metadata_is_updatedat_initialized(metadata)) { - cJSON_AddStringToObject(root, "updatedAt", metadata->updatedat); + if (atclient_atkey_metadata_is_updated_at_initialized(metadata)) { + cJSON_AddStringToObject(root, "updatedAt", metadata->updated_at); } - if (atclient_atkey_metadata_is_ispublic_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isPublic", metadata->ispublic); + if (atclient_atkey_metadata_is_is_public_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isPublic", metadata->is_public); } - if (atclient_atkey_metadata_is_iscached_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isCached", metadata->iscached); + if (atclient_atkey_metadata_is_is_cached_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isCached", metadata->is_cached); } if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { @@ -802,83 +833,93 @@ int atclient_atkey_metadata_to_jsonstr(const atclient_atkey_metadata *metadata, cJSON_AddBoolToObject(root, "ccd", metadata->ccd); } - if (atclient_atkey_metadata_is_isbinary_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isBinary", metadata->isbinary); + if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isBinary", metadata->is_binary); } - if (atclient_atkey_metadata_is_isencrypted_initialized(metadata)) { - cJSON_AddBoolToObject(root, "isEncrypted", metadata->isencrypted); + if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + cJSON_AddBoolToObject(root, "isEncrypted", metadata->is_encrypted); } - if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - cJSON_AddStringToObject(root, "dataSignature", metadata->datasignature); + if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + cJSON_AddStringToObject(root, "dataSignature", metadata->data_signature); } - if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->sharedkeystatus); + if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + cJSON_AddStringToObject(root, "sharedKeyStatus", metadata->shared_key_status); } - if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->sharedkeyenc); + if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + cJSON_AddStringToObject(root, "sharedKeyEnc", metadata->shared_key_enc); } - if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyHash", metadata->pubkeyhash); + if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + cJSON_AddStringToObject(root, "pubKeyHash", metadata->pub_key_hash); } - if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pubkeyalgo); + if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "pubKeyAlgo", metadata->pub_key_algo); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { cJSON_AddStringToObject(root, "encoding", metadata->encoding); } - if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - cJSON_AddStringToObject(root, "encKeyName", metadata->enckeyname); + if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + cJSON_AddStringToObject(root, "encKeyName", metadata->enc_key_name); } - if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "encAlgo", metadata->encalgo); + if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "encAlgo", metadata->enc_algo); } - if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - cJSON_AddStringToObject(root, "ivNonce", metadata->ivnonce); + if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + cJSON_AddStringToObject(root, "ivNonce", metadata->iv_nonce); } - if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncKeyName", metadata->skeenckeyname); + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + cJSON_AddStringToObject(root, "skeEncKeyName", metadata->ske_enc_key_name); } - if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - cJSON_AddStringToObject(root, "skeEncAlgo", metadata->skeencalgo); + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + cJSON_AddStringToObject(root, "skeEncAlgo", metadata->ske_enc_algo); } - jsonstr = cJSON_Print(root); - if (jsonstr == NULL) { + json_str = cJSON_Print(root); + if (json_str == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print failed\n"); goto exit; } - const size_t metadatastrsize = strlen(jsonstr) + 1; - *metadatastr = (char *)malloc(sizeof(char) * metadatastrsize); - if (*metadatastr == NULL) { + const size_t metadata_str_size = strlen(json_str) + 1; + *metadata_str = (char *)malloc(sizeof(char) * metadata_str_size); + if (*metadata_str == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } - memcpy(*metadatastr, jsonstr, strlen(jsonstr)); - (*metadatastr)[strlen(jsonstr)] = '\0'; + memcpy(*metadata_str, json_str, strlen(json_str)); + (*metadata_str)[strlen(json_str)] = '\0'; ret = 0; goto exit; exit: { - free(jsonstr); + free(json_str); cJSON_Delete(root); return ret; } } size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *metadata) { + /* + * 1. Validate arguments + */ + if(metadata == NULL) { + return 0; + } + + /* + * 2. Calculate length + */ long len = 0; if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { len += atclient_atkey_metadata_ttl_strlen(metadata); @@ -896,273 +937,283 @@ size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *me len += atclient_atkey_metadata_ccd_strlen(metadata); } - if (atclient_atkey_metadata_is_isbinary_initialized(metadata)) { - len += atclient_atkey_metadata_isbinary_strlen(metadata); + if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + len += atclient_atkey_metadata_is_binary_strlen(metadata); } - if (atclient_atkey_metadata_is_isencrypted_initialized(metadata)) { - len += atclient_atkey_metadata_isencrypted_strlen(metadata); + if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + len += atclient_atkey_metadata_is_encrypted_strlen(metadata); } - if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - len += atclient_atkey_metadata_datasignature_strlen(metadata); + if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + len += atclient_atkey_metadata_data_signature_strlen(metadata); } - if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - len += atclient_atkey_metadata_sharedkeystatus_strlen(metadata); + if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + len += atclient_atkey_metadata_shared_key_status_strlen(metadata); } - if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - len += atclient_atkey_metadata_sharedkeyenc_strlen(metadata); + if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + len += atclient_atkey_metadata_shared_key_enc_strlen(metadata); } - if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - len += atclient_atkey_metadata_pubkeyhash_strlen(metadata); + if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + len += atclient_atkey_metadata_pub_key_hash_strlen(metadata); } - if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - len += atclient_atkey_metadata_pubkeyalgo_strlen(metadata); + if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + len += atclient_atkey_metadata_pub_key_algo_strlen(metadata); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { len += atclient_atkey_metadata_encoding_strlen(metadata); } - if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - len += atclient_atkey_metadata_enckeyname_strlen(metadata); + if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + len += atclient_atkey_metadata_enc_key_name_strlen(metadata); } - if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - len += atclient_atkey_metadata_encalgo_strlen(metadata); + if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + len += atclient_atkey_metadata_enc_algo_strlen(metadata); } - if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - len += atclient_atkey_metadata_ivnonce_strlen(metadata); + if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + len += atclient_atkey_metadata_iv_nonce_strlen(metadata); } - if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - len += atclient_atkey_metadata_skeenckeyname_strlen(metadata); + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + len += atclient_atkey_metadata_ske_enc_key_name_strlen(metadata); } - if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - len += atclient_atkey_metadata_skeencalgo_strlen(metadata); + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + len += atclient_atkey_metadata_ske_enc_algo_strlen(metadata); } return len; } size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadata) { - return 5 // :ttl: + return strlen(":ttl:") // :ttl: + long_strlen(metadata->ttl); } size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { - return 5 // :ttb: + return strlen(":ttb:") // :ttb: + long_strlen(metadata->ttb); } size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { - return 5 // :ttr: + return strlen(":ttr:") // :ttr: + long_strlen(metadata->ttr); } size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { if (metadata->ccd) { - return 9; // :ccd:true + return strlen(":ccd:true"); // :ccd:true } else { - return 10; // :ccd:false + return strlen(":ccd:false"); // :ccd:false } return 0; } -size_t atclient_atkey_metadata_isbinary_strlen(const atclient_atkey_metadata *metadata) { - if (metadata->isbinary) { - return 14; // :isBinary:true +size_t atclient_atkey_metadata_is_binary_strlen(const atclient_atkey_metadata *metadata) { + if (metadata->is_binary) { + return strlen(":isBinary:true"); } else { - return 15; // :isBinary:false + return strlen(":isBinary:false"); } } -size_t atclient_atkey_metadata_isencrypted_strlen(const atclient_atkey_metadata *metadata) { - if (metadata->isencrypted) { - return 17; // :isEncrypted:true +size_t atclient_atkey_metadata_is_encrypted_strlen(const atclient_atkey_metadata *metadata) { + if (metadata->is_encrypted) { + return strlen(":isEncrypted:true"); } else { - return 18; // :isEncrypted:false + return strlen(":isEncrypted:false"); } - return 0; } -size_t atclient_atkey_metadata_datasignature_strlen(const atclient_atkey_metadata *metadata) { +size_t atclient_atkey_metadata_data_signature_strlen(const atclient_atkey_metadata *metadata) { // :dataSignature: - return strlen(":dataSignature:") + strlen(metadata->datasignature); + return strlen(":dataSignature:") + strlen(metadata->data_signature); } -size_t atclient_atkey_metadata_sharedkeystatus_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":sharedKeyStatus:") + strlen(metadata->sharedkeystatus); +size_t atclient_atkey_metadata_shared_key_status_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":sharedKeyStatus:") + strlen(metadata->shared_key_status); } -size_t atclient_atkey_metadata_sharedkeyenc_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":sharedKeyEnc:") + strlen(metadata->sharedkeyenc); +size_t atclient_atkey_metadata_shared_key_enc_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":sharedKeyEnc:") + strlen(metadata->shared_key_enc); } -size_t atclient_atkey_metadata_pubkeyhash_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":hash:") + strlen(metadata->pubkeyhash); +size_t atclient_atkey_metadata_pub_key_hash_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":hash:") + strlen(metadata->pub_key_hash); } -size_t atclient_atkey_metadata_pubkeyalgo_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":algo:") + strlen(metadata->pubkeyalgo); +size_t atclient_atkey_metadata_pub_key_algo_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":algo:") + strlen(metadata->pub_key_algo); } size_t atclient_atkey_metadata_encoding_strlen(const atclient_atkey_metadata *metadata) { return strlen(":encoding:") + strlen(metadata->encoding); } -size_t atclient_atkey_metadata_enckeyname_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":encKeyName:") + strlen(metadata->enckeyname); +size_t atclient_atkey_metadata_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":encKeyName:") + strlen(metadata->enc_key_name); } -size_t atclient_atkey_metadata_encalgo_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":encAlgo:") + strlen(metadata->encalgo); +size_t atclient_atkey_metadata_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":encAlgo:") + strlen(metadata->enc_algo); } -size_t atclient_atkey_metadata_ivnonce_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":ivNonce:") + strlen(metadata->ivnonce); +size_t atclient_atkey_metadata_iv_nonce_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":ivNonce:") + strlen(metadata->iv_nonce); } -size_t atclient_atkey_metadata_skeenckeyname_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":skeEncKeyName:") + strlen(metadata->skeenckeyname); +size_t atclient_atkey_metadata_ske_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":skeEncKeyName:") + strlen(metadata->ske_enc_key_name); } -size_t atclient_atkey_metadata_skeencalgo_strlen(const atclient_atkey_metadata *metadata) { - return strlen(":skeEncAlgo:") + strlen(metadata->skeencalgo); +size_t atclient_atkey_metadata_ske_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + return strlen(":skeEncAlgo:") + strlen(metadata->ske_enc_algo); } -int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadatastr) { +int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metadata, char **metadata_str) { int ret = 1; + /* + * 1. Validate arguments + */ if (metadata == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata is NULL\n"); - goto exit; + return ret; } - if (metadatastr == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr is NULL\n"); - goto exit; + if (metadata_str == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str is NULL\n"); + return ret; } - const size_t metadatastrsize = atclient_atkey_metadata_protocol_strlen(metadata) + 1; - const size_t expected_metadatastr_len = metadatastrsize - 1; + /* + * 2. Create protocol string + */ + const size_t metadata_str_size = atclient_atkey_metadata_protocol_strlen(metadata) + 1; + const size_t expected_metadatastr_len = metadata_str_size - 1; size_t pos = 0; - if ((*metadatastr = malloc(sizeof(char) * metadatastrsize)) == NULL) { + if ((*metadata_str = malloc(sizeof(char) * metadata_str_size)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc failed\n"); goto exit; } - memset(*metadatastr, 0, sizeof(char) * metadatastrsize); + memset(*metadata_str, 0, sizeof(char) * metadata_str_size); if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { - sprintf(*metadatastr + pos, ":ttl:%ld", metadata->ttl); + sprintf(*metadata_str + pos, ":ttl:%ld", metadata->ttl); pos += 5 + long_strlen(metadata->ttl); } if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { - sprintf(*metadatastr + pos, ":ttb:%ld", metadata->ttb); + sprintf(*metadata_str + pos, ":ttb:%ld", metadata->ttb); pos += 5 + long_strlen(metadata->ttb); } if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { - sprintf(*metadatastr + pos, ":ttr:%ld", metadata->ttr); + sprintf(*metadata_str + pos, ":ttr:%ld", metadata->ttr); pos += 5 + long_strlen(metadata->ttr); } if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { if (metadata->ccd) { - sprintf(*metadatastr + pos, ":ccd:true"); + sprintf(*metadata_str + pos, ":ccd:true"); pos += 9; } else { - sprintf(metadatastr + pos, ":ccd:false"); + sprintf(metadata_str + pos, ":ccd:false"); pos += 10; } } - if (atclient_atkey_metadata_is_isbinary_initialized(metadata)) { - if (metadata->isbinary) { - sprintf(*metadatastr + pos, ":isBinary:true"); + if (atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + if (metadata->is_binary) { + sprintf(*metadata_str + pos, ":isBinary:true"); pos += 14; } else { - sprintf(*metadatastr + pos, ":isBinary:false"); + sprintf(*metadata_str + pos, ":isBinary:false"); pos += 15; } } - if (atclient_atkey_metadata_is_isencrypted_initialized(metadata)) { - if (metadata->isencrypted) { - sprintf(*metadatastr + pos, ":isEncrypted:true"); + if (atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + if (metadata->is_encrypted) { + sprintf(*metadata_str + pos, ":isEncrypted:true"); pos += 17; } else { - sprintf(*metadatastr + pos, ":isEncrypted:false"); + sprintf(*metadata_str + pos, ":isEncrypted:false"); pos += 18; } } - if (atclient_atkey_metadata_is_datasignature_initialized(metadata)) { - sprintf(*metadatastr + pos, ":dataSignature:%s", metadata->datasignature); - pos += strlen(":dataSignature:") + strlen(metadata->datasignature); + if (atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + sprintf(*metadata_str + pos, ":dataSignature:%s", metadata->data_signature); + pos += strlen(":dataSignature:") + strlen(metadata->data_signature); } - if (atclient_atkey_metadata_is_sharedkeystatus_initialized(metadata)) { - sprintf(*metadatastr + pos, ":sharedKeyStatus:%s", metadata->sharedkeystatus); - pos += strlen(":sharedKeyStatus:") + strlen(metadata->sharedkeystatus); + if (atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + sprintf(*metadata_str + pos, ":sharedKeyStatus:%s", metadata->shared_key_status); + pos += strlen(":sharedKeyStatus:") + strlen(metadata->shared_key_status); } - if (atclient_atkey_metadata_is_sharedkeyenc_initialized(metadata)) { - sprintf(*metadatastr + pos, ":sharedKeyEnc:%s", metadata->sharedkeyenc); - pos += strlen(":sharedKeyEnc:") + strlen(metadata->sharedkeyenc); + if (atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + sprintf(*metadata_str + pos, ":sharedKeyEnc:%s", metadata->shared_key_enc); + pos += strlen(":sharedKeyEnc:") + strlen(metadata->shared_key_enc); } - if (atclient_atkey_metadata_is_pubkeyhash_initialized(metadata)) { - sprintf(*metadatastr + pos, ":hash:%s", metadata->pubkeyhash); - pos += strlen(":hash:") + strlen(metadata->pubkeyhash); + if (atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + sprintf(*metadata_str + pos, ":hash:%s", metadata->pub_key_hash); + pos += strlen(":hash:") + strlen(metadata->pub_key_hash); } - if (atclient_atkey_metadata_is_pubkeyalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":algo:%s", metadata->pubkeyalgo); - pos += strlen(":algo:") + strlen(metadata->pubkeyalgo); + if (atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + sprintf(*metadata_str + pos, ":algo:%s", metadata->pub_key_algo); + pos += strlen(":algo:") + strlen(metadata->pub_key_algo); } if (atclient_atkey_metadata_is_encoding_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encoding:%s", metadata->encoding); + sprintf(*metadata_str + pos, ":encoding:%s", metadata->encoding); pos += strlen(":encoding:") + strlen(metadata->encoding); } - if (atclient_atkey_metadata_is_enckeyname_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encKeyName:%s", metadata->enckeyname); - pos += strlen(":encKeyName:") + strlen(metadata->enckeyname); + if (atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + sprintf(*metadata_str + pos, ":encKeyName:%s", metadata->enc_key_name); + pos += strlen(":encKeyName:") + strlen(metadata->enc_key_name); } - if (atclient_atkey_metadata_is_encalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":encAlgo:%s", metadata->encalgo); - pos += strlen(":encAlgo:") + strlen(metadata->encalgo); + if (atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + sprintf(*metadata_str + pos, ":encAlgo:%s", metadata->enc_algo); + pos += strlen(":encAlgo:") + strlen(metadata->enc_algo); } - if (atclient_atkey_metadata_is_ivnonce_initialized(metadata)) { - sprintf(*metadatastr + pos, ":ivNonce:%s", metadata->ivnonce); - pos += strlen(":ivNonce:") + strlen(metadata->ivnonce); + if (atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + sprintf(*metadata_str + pos, ":ivNonce:%s", metadata->iv_nonce); + pos += strlen(":ivNonce:") + strlen(metadata->iv_nonce); } - if (atclient_atkey_metadata_is_skeenckeyname_initialized(metadata)) { - sprintf(*metadatastr + pos, ":skeEncKeyName:%s", metadata->skeenckeyname); - pos += strlen(":skeEncKeyName:") + strlen(metadata->skeenckeyname); + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + sprintf(*metadata_str + pos, ":skeEncKeyName:%s", metadata->ske_enc_key_name); + pos += strlen(":skeEncKeyName:") + strlen(metadata->ske_enc_key_name); } - if (atclient_atkey_metadata_is_skeencalgo_initialized(metadata)) { - sprintf(*metadatastr + pos, ":skeEncAlgo:%s", metadata->skeencalgo); - pos += strlen(":skeEncAlgo:") + strlen(metadata->skeencalgo); + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + sprintf(*metadata_str + pos, ":skeEncAlgo:%s", metadata->ske_enc_algo); + pos += strlen(":skeEncAlgo:") + strlen(metadata->ske_enc_algo); } - if (strlen(*metadatastr) != (expected_metadatastr_len)) { + /* + * 3. Do a sanity check + */ + if (strlen(*metadata_str) != (expected_metadatastr_len)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadatastr length mismatch: %lu != %lu\n", strlen(*metadatastr), + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata_str length mismatch: %lu != %lu\n", strlen(*metadata_str), (expected_metadatastr_len)); goto exit; } @@ -1172,12 +1223,12 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad exit: { return ret; } } -bool atclient_atkey_metadata_is_createdby_initialized(const atclient_atkey_metadata *metadata) { - return is_createdby_initialized(metadata); +bool atclient_atkey_metadata_is_created_by_initialized(const atclient_atkey_metadata *metadata) { + return is_created_by_initialized(metadata); } -bool atclient_atkey_metadata_is_updatedby_initialized(const atclient_atkey_metadata *metadata) { - return is_updatedby_initialized(metadata); +bool atclient_atkey_metadata_is_updated_by_initialized(const atclient_atkey_metadata *metadata) { + return is_updated_by_initialized(metadata); } bool atclient_atkey_metadata_is_status_initialized(const atclient_atkey_metadata *metadata) { @@ -1188,32 +1239,32 @@ bool atclient_atkey_metadata_is_version_initialized(const atclient_atkey_metadat return is_version_initialized(metadata); } -bool atclient_atkey_metadata_is_availableat_initialized(const atclient_atkey_metadata *metadata) { - return is_availableat_initialized(metadata); +bool atclient_atkey_metadata_is_available_at_initialized(const atclient_atkey_metadata *metadata) { + return is_available_at_initialized(metadata); } -bool atclient_atkey_metadata_is_expiresat_initialized(const atclient_atkey_metadata *metadata) { - return is_expiresat_initialized(metadata); +bool atclient_atkey_metadata_is_expires_at_initialized(const atclient_atkey_metadata *metadata) { + return is_expires_at_initialized(metadata); } -bool atclient_atkey_metadata_is_refreshat_initialized(const atclient_atkey_metadata *metadata) { - return is_refreshat_initialized(metadata); +bool atclient_atkey_metadata_is_refresh_at_initialized(const atclient_atkey_metadata *metadata) { + return is_refresh_at_initialized(metadata); } -bool atclient_atkey_metadata_is_createdat_initialized(const atclient_atkey_metadata *metadata) { - return is_createdat_initialized(metadata); +bool atclient_atkey_metadata_is_created_at_initialized(const atclient_atkey_metadata *metadata) { + return is_created_at_initialized(metadata); } -bool atclient_atkey_metadata_is_updatedat_initialized(const atclient_atkey_metadata *metadata) { - return is_updatedat_initialized(metadata); +bool atclient_atkey_metadata_is_updated_at_initialized(const atclient_atkey_metadata *metadata) { + return is_updated_at_initialized(metadata); } -bool atclient_atkey_metadata_is_ispublic_initialized(const atclient_atkey_metadata *metadata) { - return is_ispublic_initialized(metadata); +bool atclient_atkey_metadata_is_is_public_initialized(const atclient_atkey_metadata *metadata) { + return is_is_public_initialized(metadata); } -bool atclient_atkey_metadata_is_iscached_initialized(const atclient_atkey_metadata *metadata) { - return is_iscached_initialized(metadata); +bool atclient_atkey_metadata_is_is_cached_initialized(const atclient_atkey_metadata *metadata) { + return is_is_cached_initialized(metadata); } bool atclient_atkey_metadata_is_ttl_initialized(const atclient_atkey_metadata *metadata) { @@ -1232,71 +1283,71 @@ bool atclient_atkey_metadata_is_ccd_initialized(const atclient_atkey_metadata *m return is_ccd_initialized(metadata); } -bool atclient_atkey_metadata_is_isbinary_initialized(const atclient_atkey_metadata *metadata) { - return is_isbinary_initialized(metadata); +bool atclient_atkey_metadata_is_is_binary_initialized(const atclient_atkey_metadata *metadata) { + return is_is_binary_initialized(metadata); } -bool atclient_atkey_metadata_is_isencrypted_initialized(const atclient_atkey_metadata *metadata) { - return is_isencrypted_initialized(metadata); +bool atclient_atkey_metadata_is_is_encrypted_initialized(const atclient_atkey_metadata *metadata) { + return is_is_encrypted_initialized(metadata); } -bool atclient_atkey_metadata_is_datasignature_initialized(const atclient_atkey_metadata *metadata) { - return is_datasignature_initialized(metadata); +bool atclient_atkey_metadata_is_data_signature_initialized(const atclient_atkey_metadata *metadata) { + return is_data_signature_initialized(metadata); } -bool atclient_atkey_metadata_is_sharedkeystatus_initialized(const atclient_atkey_metadata *metadata) { - return is_sharedkeystatus_initialized(metadata); +bool atclient_atkey_metadata_is_shared_key_status_initialized(const atclient_atkey_metadata *metadata) { + return is_shared_key_status_initialized(metadata); } -bool atclient_atkey_metadata_is_sharedkeyenc_initialized(const atclient_atkey_metadata *metadata) { - return is_sharedkeyenc_initialized(metadata); +bool atclient_atkey_metadata_is_shared_key_enc_initialized(const atclient_atkey_metadata *metadata) { + return is_shared_key_enc_initialized(metadata); } -bool atclient_atkey_metadata_is_pubkeyhash_initialized(const atclient_atkey_metadata *metadata) { - return is_pubkeyhash_initialized(metadata); +bool atclient_atkey_metadata_is_pub_key_hash_initialized(const atclient_atkey_metadata *metadata) { + return is_pub_key_hash_initialized(metadata); } -bool atclient_atkey_metadata_is_pubkeyalgo_initialized(const atclient_atkey_metadata *metadata) { - return is_pubkeyalgo_initialized(metadata); +bool atclient_atkey_metadata_is_pub_key_algo_initialized(const atclient_atkey_metadata *metadata) { + return is_pub_key_algo_initialized(metadata); } bool atclient_atkey_metadata_is_encoding_initialized(const atclient_atkey_metadata *metadata) { return is_encoding_initialized(metadata); } -bool atclient_atkey_metadata_is_enckeyname_initialized(const atclient_atkey_metadata *metadata) { - return is_enckeyname_initialized(metadata); +bool atclient_atkey_metadata_is_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { + return is_enc_key_name_initialized(metadata); } -bool atclient_atkey_metadata_is_encalgo_initialized(const atclient_atkey_metadata *metadata) { - return is_encalgo_initialized(metadata); +bool atclient_atkey_metadata_is_enc_algo_initialized(const atclient_atkey_metadata *metadata) { + return is_enc_algo_initialized(metadata); } -bool atclient_atkey_metadata_is_ivnonce_initialized(const atclient_atkey_metadata *metadata) { - return is_ivnonce_initialized(metadata); +bool atclient_atkey_metadata_is_iv_nonce_initialized(const atclient_atkey_metadata *metadata) { + return is_iv_nonce_initialized(metadata); } -bool atclient_atkey_metadata_is_skeenckeyname_initialized(const atclient_atkey_metadata *metadata) { - return is_skeenckeyname_initialized(metadata); +bool atclient_atkey_metadata_is_ske_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { + return is_ske_enc_key_name_initialized(metadata); } -bool atclient_atkey_metadata_is_skeencalgo_initialized(const atclient_atkey_metadata *metadata) { - return is_skeencalgo_initialized(metadata); +bool atclient_atkey_metadata_is_ske_enc_algo_initialized(const atclient_atkey_metadata *metadata) { + return is_ske_enc_algo_initialized(metadata); } -int atclient_atkey_metadata_set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic) { - if (is_ispublic_initialized(metadata)) { - unset_ispublic(metadata); +int atclient_atkey_metadata_set_is_public(atclient_atkey_metadata *metadata, const bool is_public) { + if (is_is_public_initialized(metadata)) { + unset_is_public(metadata); } - set_ispublic(metadata, ispublic); + set_is_public(metadata, is_public); return 0; } -int atclient_atkey_metadata_set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { - if (is_iscached_initialized(metadata)) { - unset_iscached(metadata); +int atclient_atkey_metadata_set_is_cached(atclient_atkey_metadata *metadata, const bool is_cached) { + if (is_is_cached_initialized(metadata)) { + unset_is_cached(metadata); } - set_iscached(metadata, iscached); + set_is_cached(metadata, is_cached); return 0; } @@ -1332,29 +1383,29 @@ int atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const boo return 0; } -int atclient_atkey_metadata_set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary) { - if (is_isbinary_initialized(metadata)) { - unset_isbinary(metadata); +int atclient_atkey_metadata_set_is_binary(atclient_atkey_metadata *metadata, const bool is_binary) { + if (is_is_binary_initialized(metadata)) { + unset_is_binary(metadata); } - set_isbinary(metadata, isbinary); + set_is_binary(metadata, is_binary); return 0; } -int atclient_atkey_metadata_set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted) { - if (is_isencrypted_initialized(metadata)) { - unset_isencrypted(metadata); +int atclient_atkey_metadata_set_is_encrypted(atclient_atkey_metadata *metadata, const bool is_encrypted) { + if (is_is_encrypted_initialized(metadata)) { + unset_is_encrypted(metadata); } - set_isencrypted(metadata, isencrypted); + set_is_encrypted(metadata, is_encrypted); return 0; } -int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature) { +int atclient_atkey_metadata_set_data_signature(atclient_atkey_metadata *metadata, const char *data_signature) { int ret = 1; - if (is_datasignature_initialized(metadata)) { - unset_datasignature(metadata); + if (is_data_signature_initialized(metadata)) { + unset_data_signature(metadata); } - if ((ret = set_datasignature(metadata, datasignature)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature failed\n"); + if ((ret = set_data_signature(metadata, data_signature)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature failed\n"); goto exit; } ret = 0; @@ -1362,13 +1413,13 @@ int atclient_atkey_metadata_set_datasignature(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus) { +int atclient_atkey_metadata_set_shared_key_status(atclient_atkey_metadata *metadata, const char *shared_key_status) { int ret = 1; - if (is_sharedkeystatus_initialized(metadata)) { - unset_sharedkeystatus(metadata); + if (is_shared_key_status_initialized(metadata)) { + unset_shared_key_status(metadata); } - if ((ret = set_sharedkeystatus(metadata, sharedkeystatus)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus failed\n"); + if ((ret = set_shared_key_status(metadata, shared_key_status)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status failed\n"); goto exit; } ret = 0; @@ -1376,13 +1427,13 @@ int atclient_atkey_metadata_set_sharedkeystatus(atclient_atkey_metadata *metadat exit: { return ret; } } -int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc) { +int atclient_atkey_metadata_set_shared_key_enc(atclient_atkey_metadata *metadata, const char *shared_key_enc) { int ret = 1; - if (is_sharedkeyenc_initialized(metadata)) { - unset_sharedkeyenc(metadata); + if (is_shared_key_enc_initialized(metadata)) { + unset_shared_key_enc(metadata); } - if ((ret = set_sharedkeyenc(metadata, sharedkeyenc)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc failed\n"); + if ((ret = set_shared_key_enc(metadata, shared_key_enc)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc failed\n"); goto exit; } ret = 0; @@ -1390,13 +1441,13 @@ int atclient_atkey_metadata_set_sharedkeyenc(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash) { +int atclient_atkey_metadata_set_pub_key_hash(atclient_atkey_metadata *metadata, const char *pub_key_hash) { int ret = 1; - if (is_pubkeyhash_initialized(metadata)) { - unset_pubkeyhash(metadata); + if (is_pub_key_hash_initialized(metadata)) { + unset_pub_key_hash(metadata); } - if ((ret = set_pubkeyhash(metadata, pubkeyhash)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash failed\n"); + if ((ret = set_pub_key_hash(metadata, pub_key_hash)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash failed\n"); goto exit; } ret = 0; @@ -1404,12 +1455,12 @@ int atclient_atkey_metadata_set_pubkeyhash(atclient_atkey_metadata *metadata, co exit: { return ret; } } -int atclient_atkey_metadata_set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo) { +int atclient_atkey_metadata_set_pub_key_algo(atclient_atkey_metadata *metadata, const char *pub_key_algo) { int ret = 1; - if (is_pubkeyalgo_initialized(metadata)) { - unset_pubkeyalgo(metadata); + if (is_pub_key_algo_initialized(metadata)) { + unset_pub_key_algo(metadata); } - if ((ret = set_pubkeyalgo(metadata, pubkeyalgo)) != 0) { + if ((ret = set_pubkeyalgo(metadata, pub_key_algo)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo failed\n"); goto exit; } @@ -1433,13 +1484,13 @@ int atclient_atkey_metadata_set_encoding(atclient_atkey_metadata *metadata, cons exit: { return ret; } } -int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname) { +int atclient_atkey_metadata_set_enc_key_name(atclient_atkey_metadata *metadata, const char *enc_key_name) { int ret = 1; - if (is_enckeyname_initialized(metadata)) { - unset_enckeyname(metadata); + if (is_enc_key_name_initialized(metadata)) { + unset_enc_key_name(metadata); } - if ((ret = set_enckeyname(metadata, enckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname failed\n"); + if ((ret = set_enc_key_name(metadata, enc_key_name)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name failed\n"); goto exit; } ret = 0; @@ -1447,13 +1498,13 @@ int atclient_atkey_metadata_set_enckeyname(atclient_atkey_metadata *metadata, co exit: { return ret; } } -int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo) { +int atclient_atkey_metadata_set_enc_algo(atclient_atkey_metadata *metadata, const char *enc_algo) { int ret = 1; - if (is_encalgo_initialized(metadata)) { - unset_encalgo(metadata); + if (is_enc_algo_initialized(metadata)) { + unset_enc_algo(metadata); } - if ((ret = set_encalgo(metadata, encalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo failed\n"); + if ((ret = set_enc_algo(metadata, enc_algo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo failed\n"); goto exit; } ret = 0; @@ -1461,13 +1512,13 @@ int atclient_atkey_metadata_set_encalgo(atclient_atkey_metadata *metadata, const exit: { return ret; } } -int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce) { +int atclient_atkey_metadata_set_iv_nonce(atclient_atkey_metadata *metadata, const char *iv_nonce) { int ret = 1; - if (is_ivnonce_initialized(metadata)) { - unset_ivnonce(metadata); + if (is_iv_nonce_initialized(metadata)) { + unset_iv_nonce(metadata); } - if ((ret = set_ivnonce(metadata, ivnonce)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce failed\n"); + if ((ret = set_iv_nonce(metadata, iv_nonce)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce failed\n"); goto exit; } ret = 0; @@ -1475,13 +1526,13 @@ int atclient_atkey_metadata_set_ivnonce(atclient_atkey_metadata *metadata, const exit: { return ret; } } -int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname) { +int atclient_atkey_metadata_set_ske_enc_key_name(atclient_atkey_metadata *metadata, const char *ske_enc_key_name) { int ret = 1; - if (is_skeenckeyname_initialized(metadata)) { - unset_skeenckeyname(metadata); + if (is_ske_enc_key_name_initialized(metadata)) { + unset_ske_enc_key_name(metadata); } - if ((ret = set_skeenckeyname(metadata, skeenckeyname)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname failed\n"); + if ((ret = set_ske_enc_key_name(metadata, ske_enc_key_name)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name failed\n"); goto exit; } ret = 0; @@ -1489,13 +1540,13 @@ int atclient_atkey_metadata_set_skeenckeyname(atclient_atkey_metadata *metadata, exit: { return ret; } } -int atclient_atkey_metadata_set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo) { +int atclient_atkey_metadata_set_ske_enc_algo(atclient_atkey_metadata *metadata, const char *ske_enc_algo) { int ret = 1; - if (is_skeencalgo_initialized(metadata)) { - unset_skeencalgo(metadata); + if (is_ske_enc_algo_initialized(metadata)) { + unset_ske_enc_algo(metadata); } - if ((ret = set_skeencalgo(metadata, skeencalgo)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo failed\n"); + if ((ret = set_ske_enc_algo(metadata, ske_enc_algo)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo failed\n"); goto exit; } ret = 0; @@ -1504,388 +1555,454 @@ exit: { return ret; } } void atclient_atkey_metadata_free(atclient_atkey_metadata *metadata) { - unset_createdby(metadata); - unset_updatedby(metadata); + unset_created_by(metadata); + unset_updated_by(metadata); unset_status(metadata); unset_version(metadata); - unset_expiresat(metadata); - unset_availableat(metadata); - unset_refreshat(metadata); - unset_createdat(metadata); - unset_updatedat(metadata); - unset_ispublic(metadata); - unset_iscached(metadata); + unset_expires_at(metadata); + unset_available_at(metadata); + unset_refresh_at(metadata); + unset_created_at(metadata); + unset_updated_at(metadata); + unset_is_public(metadata); + unset_is_cached(metadata); unset_ttl(metadata); unset_ttb(metadata); unset_ttr(metadata); unset_ccd(metadata); - unset_isbinary(metadata); - unset_isencrypted(metadata); - unset_datasignature(metadata); - unset_sharedkeystatus(metadata); - unset_sharedkeyenc(metadata); - unset_pubkeyhash(metadata); - unset_pubkeyalgo(metadata); + unset_is_binary(metadata); + unset_is_encrypted(metadata); + unset_data_signature(metadata); + unset_shared_key_status(metadata); + unset_shared_key_enc(metadata); + unset_pub_key_hash(metadata); + unset_pub_key_algo(metadata); unset_encoding(metadata); - unset_enckeyname(metadata); - unset_encalgo(metadata); - unset_ivnonce(metadata); - unset_skeenckeyname(metadata); - unset_skeencalgo(metadata); + unset_enc_key_name(metadata); + unset_enc_algo(metadata); + unset_iv_nonce(metadata); + unset_ske_enc_key_name(metadata); + unset_ske_enc_algo(metadata); memset(metadata, 0, sizeof(atclient_atkey_metadata)); } -static bool is_createdby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] & ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED); +static bool is_created_by_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] & + ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED); } -static bool is_updatedby_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] & ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED); +static bool is_updated_by_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] & + ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED); } static bool is_status_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] & ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] & + ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED); } static bool is_version_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] & ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] & + ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED); } -static bool is_expiresat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] & ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED); +static bool is_expires_at_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] & + ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED); } -static bool is_availableat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] & ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED); +static bool is_available_at_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] & + ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED); } -static bool is_refreshat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] & ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED); +static bool is_refresh_at_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] & + ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED); } -static bool is_createdat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] & ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED); +static bool is_created_at_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] & + ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED); } -static bool is_updatedat_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] & ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED); +static bool is_updated_at_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] & + ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED); } -static bool is_ispublic_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] & ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED); +static bool is_is_public_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] & + ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED); } -static bool is_iscached_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] & ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED); +static bool is_is_cached_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] & + ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED); } static bool is_ttl_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] & ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] & ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED); } static bool is_ttb_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] & ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] & ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED); } static bool is_ttr_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] & ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] & ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED); } static bool is_ccd_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] & ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] & ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED); } -static bool is_isbinary_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] & ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED); +static bool is_is_binary_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] & + ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED); } -static bool is_isencrypted_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] & ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED); +static bool is_is_encrypted_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] & + ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED); } -static bool is_datasignature_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] & ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED); +static bool is_data_signature_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] & + ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED); } -static bool is_sharedkeystatus_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & +static bool is_shared_key_status_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] & ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED); } -static bool is_sharedkeyenc_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] & ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); +static bool is_shared_key_enc_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] & + ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED); } -static bool is_pubkeyhash_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] & ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED); +static bool is_pub_key_hash_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] & + ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED); } -static bool is_pubkeyalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] & ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED); +static bool is_pub_key_algo_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] & + ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED); } static bool is_encoding_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] & ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED); + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] & + ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED); } -static bool is_enckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] & ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED); +static bool is_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] & + ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED); } -static bool is_encalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] & ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED); +static bool is_enc_algo_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] & + ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED); } -static bool is_ivnonce_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] & ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED); +static bool is_iv_nonce_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] & + ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED); } -static bool is_skeenckeyname_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] & ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); +static bool is_ske_enc_key_name_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] & + ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED); } -static bool is_skeencalgo_initialized(const atclient_atkey_metadata *metadata) { - return (metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] & ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED); +static bool is_ske_enc_algo_initialized(const atclient_atkey_metadata *metadata) { + return (metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] & + ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED); } -static void set_is_createdby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_created_by_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] |= ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] |= + ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDBY_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_CREATEDBY_INITIALIZED; } } -static void set_is_updatedby_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_updated_by_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] |= ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] |= + ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDBY_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_UPDATEDBY_INITIALIZED; } } static void set_is_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] |= ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] |= ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] &= ~ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_STATUS_INDEX] &= ~ATCLIENT_ATKEY_METADATA_STATUS_INITIALIZED; } } static void set_is_version_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] |= ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] |= ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] &= ~ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_VERSION_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_VERSION_INITIALIZED; } } -static void set_is_expiresat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_expires_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] |= ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] |= + ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_EXPIRESAT_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_EXPIRESAT_INITIALIZED; } } -static void set_is_availableat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_available_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] |= ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] |= + ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_AVAILABLEAT_INITIALIZED; } } -static void set_is_refreshat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_refresh_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] |= ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] |= + ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_REFRESHAT_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_REFRESHAT_INITIALIZED; } } -static void set_is_createdat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_created_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] |= ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] |= + ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CREATEDAT_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_CREATEDAT_INITIALIZED; } } -static void set_is_updatedat_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_updated_at_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] |= ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] |= + ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] &= ~ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_UPDATEDAT_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_UPDATEDAT_INITIALIZED; } } -static void set_is_ispublic_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_is_public_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] |= ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] |= + ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISPUBLIC_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ISPUBLIC_INITIALIZED; } } -static void set_is_iscached_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_is_cached_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] |= ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] |= + ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISCACHED_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ISCACHED_INITIALIZED; } } static void set_is_ttl_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] |= ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] |= ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTL_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTL_INITIALIZED; } } static void set_is_ttb_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] |= ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] |= ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTB_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTB_INITIALIZED; } } static void set_is_ttr_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] |= ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] |= ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_TTR_INDEX] &= ~ATCLIENT_ATKEY_METADATA_TTR_INITIALIZED; } } static void set_is_ccd_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] |= ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] |= ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_CCD_INDEX] &= ~ATCLIENT_ATKEY_METADATA_CCD_INITIALIZED; } } -static void set_is_isbinary_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_is_binary_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] |= ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] |= + ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISBINARY_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ISBINARY_INITIALIZED; } } -static void set_is_isencrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_is_encrypted_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] |= ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] |= + ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ISENCRYPTED_INITIALIZED; } } -static void set_is_datasignature_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_data_signature_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] |= ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] |= + ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] &= ~ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_DATASIGNATURE_INITIALIZED; } } -static void set_is_sharedkeystatus_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_shared_key_status_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] |= + ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_SHAREDKEYSTATUS_INITIALIZED; } } -static void set_is_sharedkeyenc_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_shared_key_enc_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] |= ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] |= + ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_SHAREDKEYENC_INITIALIZED; } } -static void set_is_pubkeyhash_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_pub_key_hash_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] |= ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] |= + ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] &= ~ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_PUBKEYHASH_INITIALIZED; } } -static void set_is_pubkeyalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_pub_key_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] |= + ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_PUBKEYALGO_INITIALIZED; } } static void set_is_encoding_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] |= + ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCODING_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ENCODING_INITIALIZED; } } -static void set_is_enckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_enc_key_name_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] |= + ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ENCKEYNAME_INITIALIZED; } } -static void set_is_encalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_enc_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_ENCALGO_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_ENCALGO_INITIALIZED; } } -static void set_is_ivnonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_iv_nonce_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] |= ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] |= ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] &= ~ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_IVNONCE_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_IVNONCE_INITIALIZED; } } -static void set_is_skeenckeyname_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_ske_enc_key_name_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] |= + ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_SKEENCKEYNAME_INITIALIZED; } } -static void set_is_skeencalgo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { +static void set_is_ske_enc_algo_initialized(atclient_atkey_metadata *metadata, bool is_initialized) { if (is_initialized) { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] |= ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] |= + ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; } else { - metadata->_initializedfields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] &= ~ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; + metadata->_initialized_fields[ATCLIENT_ATKEY_METADATA_SKEENCALGO_INDEX] &= + ~ATCLIENT_ATKEY_METADATA_SKEENCALGO_INITIALIZED; } } -static void unset_createdby(atclient_atkey_metadata *metadata) { - if (is_createdby_initialized(metadata)) { - free(metadata->createdby); +static void unset_created_by(atclient_atkey_metadata *metadata) { + if (is_created_by_initialized(metadata)) { + free(metadata->created_by); } - metadata->createdby = NULL; - set_is_createdby_initialized(metadata, false); + metadata->created_by = NULL; + set_is_created_by_initialized(metadata, false); } -static void unset_updatedby(atclient_atkey_metadata *metadata) { - if (is_updatedby_initialized(metadata)) { - free(metadata->updatedby); +static void unset_updated_by(atclient_atkey_metadata *metadata) { + if (is_updated_by_initialized(metadata)) { + free(metadata->updated_by); } - metadata->updatedby = NULL; - set_is_updatedby_initialized(metadata, false); + metadata->updated_by = NULL; + set_is_updated_by_initialized(metadata, false); } static void unset_status(atclient_atkey_metadata *metadata) { @@ -1901,54 +2018,54 @@ static void unset_version(atclient_atkey_metadata *metadata) { set_is_version_initialized(metadata, false); } -static void unset_expiresat(atclient_atkey_metadata *metadata) { - if (is_expiresat_initialized(metadata)) { - free(metadata->expiresat); +static void unset_expires_at(atclient_atkey_metadata *metadata) { + if (is_expires_at_initialized(metadata)) { + free(metadata->expires_at); } - metadata->expiresat = NULL; - set_is_expiresat_initialized(metadata, false); + metadata->expires_at = NULL; + set_is_expires_at_initialized(metadata, false); } -static void unset_availableat(atclient_atkey_metadata *metadata) { - if (is_availableat_initialized(metadata)) { - free(metadata->availableat); +static void unset_available_at(atclient_atkey_metadata *metadata) { + if (is_available_at_initialized(metadata)) { + free(metadata->available_at); } - metadata->availableat = NULL; - set_is_availableat_initialized(metadata, false); + metadata->available_at = NULL; + set_is_available_at_initialized(metadata, false); } -static void unset_refreshat(atclient_atkey_metadata *metadata) { - if (is_refreshat_initialized(metadata)) { - free(metadata->refreshat); +static void unset_refresh_at(atclient_atkey_metadata *metadata) { + if (is_refresh_at_initialized(metadata)) { + free(metadata->refresh_at); } - metadata->refreshat = NULL; - set_is_refreshat_initialized(metadata, false); + metadata->refresh_at = NULL; + set_is_refresh_at_initialized(metadata, false); } -static void unset_createdat(atclient_atkey_metadata *metadata) { - if (is_createdat_initialized(metadata)) { - free(metadata->createdat); +static void unset_created_at(atclient_atkey_metadata *metadata) { + if (is_created_at_initialized(metadata)) { + free(metadata->created_at); } - metadata->createdat = NULL; - set_is_createdat_initialized(metadata, false); + metadata->created_at = NULL; + set_is_created_at_initialized(metadata, false); } -static void unset_updatedat(atclient_atkey_metadata *metadata) { - if (is_updatedat_initialized(metadata)) { - free(metadata->updatedat); +static void unset_updated_at(atclient_atkey_metadata *metadata) { + if (is_updated_at_initialized(metadata)) { + free(metadata->updated_at); } - metadata->updatedat = NULL; - set_is_updatedat_initialized(metadata, false); + metadata->updated_at = NULL; + set_is_updated_at_initialized(metadata, false); } -static void unset_ispublic(atclient_atkey_metadata *metadata) { - metadata->ispublic = false; - set_is_ispublic_initialized(metadata, false); +static void unset_is_public(atclient_atkey_metadata *metadata) { + metadata->is_public = false; + set_is_is_public_initialized(metadata, false); } -static void unset_iscached(atclient_atkey_metadata *metadata) { - metadata->iscached = false; - set_is_iscached_initialized(metadata, false); +static void unset_is_cached(atclient_atkey_metadata *metadata) { + metadata->is_cached = false; + set_is_is_cached_initialized(metadata, false); } static void unset_ttl(atclient_atkey_metadata *metadata) { @@ -1971,54 +2088,54 @@ static void unset_ccd(atclient_atkey_metadata *metadata) { set_is_ccd_initialized(metadata, false); } -static void unset_isbinary(atclient_atkey_metadata *metadata) { - metadata->isbinary = false; - set_is_isbinary_initialized(metadata, false); +static void unset_is_binary(atclient_atkey_metadata *metadata) { + metadata->is_binary = false; + set_is_is_binary_initialized(metadata, false); } -static void unset_isencrypted(atclient_atkey_metadata *metadata) { - metadata->isencrypted = false; - set_is_isencrypted_initialized(metadata, false); +static void unset_is_encrypted(atclient_atkey_metadata *metadata) { + metadata->is_encrypted = false; + set_is_is_encrypted_initialized(metadata, false); } -static void unset_datasignature(atclient_atkey_metadata *metadata) { - if (is_datasignature_initialized(metadata)) { - free(metadata->datasignature); +static void unset_data_signature(atclient_atkey_metadata *metadata) { + if (is_data_signature_initialized(metadata)) { + free(metadata->data_signature); } - metadata->datasignature = NULL; - set_is_datasignature_initialized(metadata, false); + metadata->data_signature = NULL; + set_is_data_signature_initialized(metadata, false); } -static void unset_sharedkeystatus(atclient_atkey_metadata *metadata) { - if (is_sharedkeystatus_initialized(metadata)) { - free(metadata->sharedkeystatus); +static void unset_shared_key_status(atclient_atkey_metadata *metadata) { + if (is_shared_key_status_initialized(metadata)) { + free(metadata->shared_key_status); } - metadata->sharedkeystatus = NULL; - set_is_sharedkeystatus_initialized(metadata, false); + metadata->shared_key_status = NULL; + set_is_shared_key_status_initialized(metadata, false); } -static void unset_sharedkeyenc(atclient_atkey_metadata *metadata) { - if (is_sharedkeyenc_initialized(metadata)) { - free(metadata->sharedkeyenc); +static void unset_shared_key_enc(atclient_atkey_metadata *metadata) { + if (is_shared_key_enc_initialized(metadata)) { + free(metadata->shared_key_enc); } - metadata->sharedkeyenc = NULL; - set_is_sharedkeyenc_initialized(metadata, false); + metadata->shared_key_enc = NULL; + set_is_shared_key_enc_initialized(metadata, false); } -static void unset_pubkeyhash(atclient_atkey_metadata *metadata) { - if (is_pubkeyhash_initialized(metadata)) { - free(metadata->pubkeyhash); +static void unset_pub_key_hash(atclient_atkey_metadata *metadata) { + if (is_pub_key_hash_initialized(metadata)) { + free(metadata->pub_key_hash); } - metadata->pubkeyhash = NULL; - set_is_pubkeyhash_initialized(metadata, false); + metadata->pub_key_hash = NULL; + set_is_pub_key_hash_initialized(metadata, false); } -static void unset_pubkeyalgo(atclient_atkey_metadata *metadata) { - if (is_pubkeyalgo_initialized(metadata)) { - free(metadata->pubkeyalgo); +static void unset_pub_key_algo(atclient_atkey_metadata *metadata) { + if (is_pub_key_algo_initialized(metadata)) { + free(metadata->pub_key_algo); } - metadata->pubkeyalgo = NULL; - set_is_pubkeyalgo_initialized(metadata, false); + metadata->pub_key_algo = NULL; + set_is_pub_key_algo_initialized(metadata, false); } static void unset_encoding(atclient_atkey_metadata *metadata) { @@ -2029,74 +2146,74 @@ static void unset_encoding(atclient_atkey_metadata *metadata) { set_is_encoding_initialized(metadata, false); } -static void unset_enckeyname(atclient_atkey_metadata *metadata) { - if (is_enckeyname_initialized(metadata)) { - free(metadata->enckeyname); +static void unset_enc_key_name(atclient_atkey_metadata *metadata) { + if (is_enc_key_name_initialized(metadata)) { + free(metadata->enc_key_name); } - set_is_enckeyname_initialized(metadata, false); + set_is_enc_key_name_initialized(metadata, false); } -static void unset_encalgo(atclient_atkey_metadata *metadata) { - if (is_encalgo_initialized(metadata)) { - free(metadata->encalgo); +static void unset_enc_algo(atclient_atkey_metadata *metadata) { + if (is_enc_algo_initialized(metadata)) { + free(metadata->enc_algo); } - metadata->encalgo = NULL; - set_is_encalgo_initialized(metadata, false); + metadata->enc_algo = NULL; + set_is_enc_algo_initialized(metadata, false); } -static void unset_ivnonce(atclient_atkey_metadata *metadata) { - if (is_ivnonce_initialized(metadata)) { - free(metadata->ivnonce); +static void unset_iv_nonce(atclient_atkey_metadata *metadata) { + if (is_iv_nonce_initialized(metadata)) { + free(metadata->iv_nonce); } - metadata->ivnonce = NULL; - set_is_ivnonce_initialized(metadata, false); + metadata->iv_nonce = NULL; + set_is_iv_nonce_initialized(metadata, false); } -static void unset_skeenckeyname(atclient_atkey_metadata *metadata) { - if (is_skeenckeyname_initialized(metadata)) { - free(metadata->skeenckeyname); +static void unset_ske_enc_key_name(atclient_atkey_metadata *metadata) { + if (is_ske_enc_key_name_initialized(metadata)) { + free(metadata->ske_enc_key_name); } - metadata->skeenckeyname = NULL; - set_is_skeenckeyname_initialized(metadata, false); + metadata->ske_enc_key_name = NULL; + set_is_ske_enc_key_name_initialized(metadata, false); } -static void unset_skeencalgo(atclient_atkey_metadata *metadata) { - if (is_skeencalgo_initialized(metadata)) { - free(metadata->skeencalgo); +static void unset_ske_enc_algo(atclient_atkey_metadata *metadata) { + if (is_ske_enc_algo_initialized(metadata)) { + free(metadata->ske_enc_algo); } - metadata->skeencalgo = NULL; - set_is_skeencalgo_initialized(metadata, false); + metadata->ske_enc_algo = NULL; + set_is_ske_enc_algo_initialized(metadata, false); } -static int set_createdby(atclient_atkey_metadata *metadata, const char *createdby) { +static int set_created_by(atclient_atkey_metadata *metadata, const char *created_by) { int ret = 1; - const size_t createdbylen = strlen(createdby); - const size_t createdbysize = createdbylen + 1; - if ((metadata->createdby = malloc(sizeof(char) * (createdbysize))) == NULL) { + const size_t created_by_len = strlen(created_by); + const size_t created_by_size = created_by_len + 1; + if ((metadata->created_by = malloc(sizeof(char) * (created_by_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdby malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_by malloc failed\n"); goto exit; } - memcpy(metadata->createdby, createdby, createdbylen); - metadata->createdby[createdbylen] = '\0'; - set_is_createdby_initialized(metadata, true); + memcpy(metadata->created_by, created_by, created_by_len); + metadata->created_by[created_by_len] = '\0'; + set_is_created_by_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_updatedby(atclient_atkey_metadata *metadata, const char *updatedby) { +static int set_updated_by(atclient_atkey_metadata *metadata, const char *updated_by) { int ret = 1; - const size_t updatedbylen = strlen(updatedby); - const size_t updatedbysize = updatedbylen + 1; - if ((metadata->updatedby = malloc(sizeof(char) * (updatedbysize))) == NULL) { + const size_t updated_by_len = strlen(updated_by); + const size_t updated_by_size = updated_by_len + 1; + if ((metadata->updated_by = malloc(sizeof(char) * (updated_by_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedby malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_by malloc failed\n"); goto exit; } - memcpy(metadata->updatedby, updatedby, updatedbylen); - metadata->updatedby[updatedbylen] = '\0'; - set_is_updatedby_initialized(metadata, true); + memcpy(metadata->updated_by, updated_by, updated_by_len); + metadata->updated_by[updated_by_len] = '\0'; + set_is_updated_by_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } @@ -2124,99 +2241,99 @@ static void set_version(atclient_atkey_metadata *metadata, int version) { set_is_version_initialized(metadata, true); } -static int set_expiresat(atclient_atkey_metadata *metadata, const char *expiresat) { +static int set_expires_at(atclient_atkey_metadata *metadata, const char *expires_at) { int ret = 1; - const size_t expiresatlen = strlen(expiresat); - const size_t expiresatsize = expiresatlen + 1; - if ((metadata->expiresat = malloc(sizeof(char) * (expiresatsize))) == NULL) { + const size_t expires_at_len = strlen(expires_at); + const size_t expires_at_size = expires_at_len + 1; + if ((metadata->expires_at = malloc(sizeof(char) * (expires_at_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expiresat malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_expires_at malloc failed\n"); goto exit; } - memcpy(metadata->expiresat, expiresat, expiresatlen); - metadata->expiresat[expiresatlen] = '\0'; - set_is_expiresat_initialized(metadata, true); + memcpy(metadata->expires_at, expires_at, expires_at_len); + metadata->expires_at[expires_at_len] = '\0'; + set_is_expires_at_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_availableat(atclient_atkey_metadata *metadata, const char *availableat) { +static int set_available_at(atclient_atkey_metadata *metadata, const char *available_at) { int ret = 1; - const size_t availableatlen = strlen(availableat); - const size_t availableatsize = availableatlen + 1; - if ((metadata->availableat = malloc(sizeof(char) * (availableatsize))) == NULL) { + const size_t available_at_len = strlen(available_at); + const size_t available_at_size = available_at_len + 1; + if ((metadata->available_at = malloc(sizeof(char) * (available_at_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_availableat malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_available_at malloc failed\n"); goto exit; } - memcpy(metadata->availableat, availableat, availableatlen); - metadata->availableat[availableatlen] = '\0'; - set_is_availableat_initialized(metadata, true); + memcpy(metadata->available_at, available_at, available_at_len); + metadata->available_at[available_at_len] = '\0'; + set_is_available_at_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_refreshat(atclient_atkey_metadata *metadata, const char *refreshat) { +static int set_refresh_at(atclient_atkey_metadata *metadata, const char *refresh_at) { int ret = 1; - const size_t refreshatlen = strlen(refreshat); - const size_t refreshatsize = refreshatlen + 1; - if ((metadata->refreshat = malloc(sizeof(char) * (refreshatsize))) == NULL) { + const size_t refresh_at_len = strlen(refresh_at); + const size_t refresh_at_size = refresh_at_len + 1; + if ((metadata->refresh_at = malloc(sizeof(char) * (refresh_at_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refreshat malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_refresh_at malloc failed\n"); goto exit; } - memcpy(metadata->refreshat, refreshat, refreshatlen); - metadata->refreshat[refreshatlen] = '\0'; - set_is_refreshat_initialized(metadata, true); + memcpy(metadata->refresh_at, refresh_at, refresh_at_len); + metadata->refresh_at[refresh_at_len] = '\0'; + set_is_refresh_at_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_createdat(atclient_atkey_metadata *metadata, const char *createdat) { +static int set_created_at(atclient_atkey_metadata *metadata, const char *created_at) { int ret = 1; - const size_t createdatlen = strlen(createdat); - const size_t createdatsize = createdatlen + 1; - if ((metadata->createdat = malloc(sizeof(char) * (createdatlen + 1))) == NULL) { + const size_t created_at_len = strlen(created_at); + const size_t created_at_size = created_at_len + 1; + if ((metadata->created_at = malloc(sizeof(char) * (created_at_len + 1))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_createdat malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_created_at malloc failed\n"); goto exit; } - memcpy(metadata->createdat, createdat, createdatlen); - metadata->createdat[createdatlen] = '\0'; - set_is_createdat_initialized(metadata, true); + memcpy(metadata->created_at, created_at, created_at_len); + metadata->created_at[created_at_len] = '\0'; + set_is_created_at_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_updatedat(atclient_atkey_metadata *metadata, const char *updatedat) { +static int set_updated_at(atclient_atkey_metadata *metadata, const char *updated_at) { int ret = 1; - const size_t updatedatlen = strlen(updatedat); - const size_t updatedatsize = updatedatlen + 1; - if ((metadata->updatedat = malloc(sizeof(char) * (updatedatsize))) == NULL) { + const size_t updated_at_len = strlen(updated_at); + const size_t updated_at_size = updated_at_len + 1; + if ((metadata->updated_at = malloc(sizeof(char) * (updated_at_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updatedat malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_updated_at malloc failed\n"); goto exit; } - memcpy(metadata->updatedat, updatedat, updatedatlen); - metadata->updatedat[updatedatlen] = '\0'; - set_is_updatedat_initialized(metadata, true); + memcpy(metadata->updated_at, updated_at, updated_at_len); + metadata->updated_at[updated_at_len] = '\0'; + set_is_updated_at_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static void set_ispublic(atclient_atkey_metadata *metadata, const bool ispublic) { - metadata->ispublic = ispublic; - set_is_ispublic_initialized(metadata, true); +static void set_is_public(atclient_atkey_metadata *metadata, const bool is_public) { + metadata->is_public = is_public; + set_is_is_public_initialized(metadata, true); } -static void set_iscached(atclient_atkey_metadata *metadata, const bool iscached) { - metadata->iscached = iscached; - set_is_iscached_initialized(metadata, true); +static void set_is_cached(atclient_atkey_metadata *metadata, const bool is_cached) { + metadata->is_cached = is_cached; + set_is_is_cached_initialized(metadata, true); } static void set_ttl(atclient_atkey_metadata *metadata, const long ttl) { @@ -2239,96 +2356,96 @@ static void set_ccd(atclient_atkey_metadata *metadata, const bool ccd) { set_is_ccd_initialized(metadata, true); } -static void set_isbinary(atclient_atkey_metadata *metadata, const bool isbinary) { - metadata->isbinary = isbinary; - set_is_isbinary_initialized(metadata, true); +static void set_is_binary(atclient_atkey_metadata *metadata, const bool is_binary) { + metadata->is_binary = is_binary; + set_is_is_binary_initialized(metadata, true); } -static void set_isencrypted(atclient_atkey_metadata *metadata, const bool isencrypted) { - metadata->isencrypted = isencrypted; - set_is_isencrypted_initialized(metadata, true); +static void set_is_encrypted(atclient_atkey_metadata *metadata, const bool is_encrypted) { + metadata->is_encrypted = is_encrypted; + set_is_is_encrypted_initialized(metadata, true); } -static int set_datasignature(atclient_atkey_metadata *metadata, const char *datasignature) { +static int set_data_signature(atclient_atkey_metadata *metadata, const char *data_signature) { int ret = 1; - const size_t datasignaturelen = strlen(datasignature); - const size_t datasignaturesize = datasignaturelen + 1; - if ((metadata->datasignature = malloc(sizeof(char) * (datasignaturesize))) == NULL) { + const size_t data_signature_len = strlen(data_signature); + const size_t data_signature_size = data_signature_len + 1; + if ((metadata->data_signature = malloc(sizeof(char) * (data_signature_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_datasignature malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_data_signature malloc failed\n"); goto exit; } - memcpy(metadata->datasignature, datasignature, datasignaturelen); - metadata->datasignature[datasignaturelen] = '\0'; - set_is_datasignature_initialized(metadata, true); + memcpy(metadata->data_signature, data_signature, data_signature_len); + metadata->data_signature[data_signature_len] = '\0'; + set_is_data_signature_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_sharedkeystatus(atclient_atkey_metadata *metadata, const char *sharedkeystatus) { +static int set_shared_key_status(atclient_atkey_metadata *metadata, const char *shared_key_status) { int ret = 1; - const size_t sharedkeystatuslen = strlen(sharedkeystatus); - const size_t sharedkeystatussize = sharedkeystatuslen + 1; - if ((metadata->sharedkeystatus = malloc(sizeof(char) * (sharedkeystatussize))) == NULL) { + const size_t shared_key_status_len = strlen(shared_key_status); + const size_t shared_key_status_size = shared_key_status_len + 1; + if ((metadata->shared_key_status = malloc(sizeof(char) * (shared_key_status_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeystatus malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_status malloc failed\n"); goto exit; } - memcpy(metadata->sharedkeystatus, sharedkeystatus, sharedkeystatuslen); - metadata->sharedkeystatus[sharedkeystatuslen] = '\0'; - set_is_sharedkeystatus_initialized(metadata, true); + memcpy(metadata->shared_key_status, shared_key_status, shared_key_status_len); + metadata->shared_key_status[shared_key_status_len] = '\0'; + set_is_shared_key_status_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_sharedkeyenc(atclient_atkey_metadata *metadata, const char *sharedkeyenc) { +static int set_shared_key_enc(atclient_atkey_metadata *metadata, const char *shared_key_enc) { int ret = 1; - const size_t sharedkeyenclen = strlen(sharedkeyenc); - const size_t sharedkeyencsize = sharedkeyenclen + 1; - if ((metadata->sharedkeyenc = malloc(sizeof(char) * (sharedkeyencsize))) == NULL) { + const size_t shared_key_enc_len = strlen(shared_key_enc); + const size_t shared_key_enc_size = shared_key_enc_len + 1; + if ((metadata->shared_key_enc = malloc(sizeof(char) * (shared_key_enc_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_sharedkeyenc malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_shared_key_enc malloc failed\n"); goto exit; } - memcpy(metadata->sharedkeyenc, sharedkeyenc, sharedkeyenclen); - metadata->sharedkeyenc[sharedkeyenclen] = '\0'; - set_is_sharedkeyenc_initialized(metadata, true); + memcpy(metadata->shared_key_enc, shared_key_enc, shared_key_enc_len); + metadata->shared_key_enc[shared_key_enc_len] = '\0'; + set_is_shared_key_enc_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_pubkeyhash(atclient_atkey_metadata *metadata, const char *pubkeyhash) { +static int set_pub_key_hash(atclient_atkey_metadata *metadata, const char *pub_key_hash) { int ret = 1; - const size_t pubkeyhashlen = strlen(pubkeyhash); - const size_t pubkeyhashsize = pubkeyhashlen + 1; - if ((metadata->pubkeyhash = malloc(sizeof(char) * (pubkeyhashsize))) == NULL) { + const size_t pub_key_hash_len = strlen(pub_key_hash); + const size_t pub_key_hash_size = pub_key_hash_len + 1; + if ((metadata->pub_key_hash = malloc(sizeof(char) * (pub_key_hash_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyhash malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pub_key_hash malloc failed\n"); goto exit; } - memcpy(metadata->pubkeyhash, pubkeyhash, pubkeyhashlen); - metadata->pubkeyhash[pubkeyhashlen] = '\0'; - set_is_pubkeyhash_initialized(metadata, true); + memcpy(metadata->pub_key_hash, pub_key_hash, pub_key_hash_len); + metadata->pub_key_hash[pub_key_hash_len] = '\0'; + set_is_pub_key_hash_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pubkeyalgo) { +static int set_pubkeyalgo(atclient_atkey_metadata *metadata, const char *pub_key_algo) { int ret = 1; - const size_t pubkeyalgolen = strlen(pubkeyalgo); - const size_t pubkeyalgosize = pubkeyalgolen + 1; - if ((metadata->pubkeyalgo = malloc(sizeof(char) * (pubkeyalgosize))) == NULL) { + const size_t pub_key_algo_len = strlen(pub_key_algo); + const size_t pub_key_algo_size = pub_key_algo_len + 1; + if ((metadata->pub_key_algo = malloc(sizeof(char) * (pub_key_algo_size))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pubkeyalgo malloc failed\n"); goto exit; } - memcpy(metadata->pubkeyalgo, pubkeyalgo, pubkeyalgolen); - metadata->pubkeyalgo[pubkeyalgolen] = '\0'; - set_is_pubkeyalgo_initialized(metadata, true); + memcpy(metadata->pub_key_algo, pub_key_algo, pub_key_algo_len); + metadata->pub_key_algo[pub_key_algo_len] = '\0'; + set_is_pub_key_algo_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } @@ -2336,101 +2453,101 @@ exit: { return ret; } static int set_encoding(atclient_atkey_metadata *metadata, const char *encoding) { int ret = 1; - const size_t encodinglen = strlen(encoding); - const size_t encodingsize = encodinglen + 1; - if ((metadata->encoding = malloc(sizeof(char) * (encodingsize))) == NULL) { + const size_t encoding_len = strlen(encoding); + const size_t encoding_size = encoding_len + 1; + if ((metadata->encoding = malloc(sizeof(char) * (encoding_size))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encoding malloc failed\n"); goto exit; } - memcpy(metadata->encoding, encoding, encodinglen); - metadata->encoding[encodinglen] = '\0'; + memcpy(metadata->encoding, encoding, encoding_len); + metadata->encoding[encoding_len] = '\0'; set_is_encoding_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_enckeyname(atclient_atkey_metadata *metadata, const char *enckeyname) { +static int set_enc_key_name(atclient_atkey_metadata *metadata, const char *enc_key_name) { int ret = 1; - const size_t enckeynamelen = strlen(enckeyname); - const size_t enckeynamesize = enckeynamelen + 1; - if ((metadata->enckeyname = malloc(sizeof(char) * (enckeynamesize))) == NULL) { + const size_t enc_key_name_len = strlen(enc_key_name); + const size_t enc_key_name_size = enc_key_name_len + 1; + if ((metadata->enc_key_name = malloc(sizeof(char) * (enc_key_name_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enckeyname malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_key_name malloc failed\n"); goto exit; } - memcpy(metadata->enckeyname, enckeyname, enckeynamelen); - metadata->enckeyname[enckeynamelen] = '\0'; - set_is_enckeyname_initialized(metadata, true); + memcpy(metadata->enc_key_name, enc_key_name, enc_key_name_len); + metadata->enc_key_name[enc_key_name_len] = '\0'; + set_is_enc_key_name_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_encalgo(atclient_atkey_metadata *metadata, const char *encalgo) { +static int set_enc_algo(atclient_atkey_metadata *metadata, const char *enc_algo) { int ret = 1; - const size_t encalgolen = strlen(encalgo); - const size_t encalgosize = encalgolen + 1; - if ((metadata->encalgo = malloc(sizeof(char) * (encalgosize))) == NULL) { + const size_t enc_algo_len = strlen(enc_algo); + const size_t enc_algo_size = enc_algo_len + 1; + if ((metadata->enc_algo = malloc(sizeof(char) * (enc_algo_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_encalgo malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_enc_algo malloc failed\n"); goto exit; } - memcpy(metadata->encalgo, encalgo, encalgolen); - metadata->encalgo[encalgolen] = '\0'; - set_is_encalgo_initialized(metadata, true); + memcpy(metadata->enc_algo, enc_algo, enc_algo_len); + metadata->enc_algo[enc_algo_len] = '\0'; + set_is_enc_algo_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_ivnonce(atclient_atkey_metadata *metadata, const char *ivnonce) { +static int set_iv_nonce(atclient_atkey_metadata *metadata, const char *iv_nonce) { int ret = 1; - const size_t ivnoncelen = strlen(ivnonce); - const size_t ivnoncesize = ivnoncelen + 1; - if ((metadata->ivnonce = malloc(sizeof(char) * (ivnoncesize))) == NULL) { + const size_t iv_nonce_len = strlen(iv_nonce); + const size_t iv_nonce_size = iv_nonce_len + 1; + if ((metadata->iv_nonce = malloc(sizeof(char) * (iv_nonce_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ivnonce malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_iv_nonce malloc failed\n"); goto exit; } - memcpy(metadata->ivnonce, ivnonce, ivnoncelen); - metadata->ivnonce[ivnoncelen] = '\0'; - set_is_ivnonce_initialized(metadata, true); + memcpy(metadata->iv_nonce, iv_nonce, iv_nonce_len); + metadata->iv_nonce[iv_nonce_len] = '\0'; + set_is_iv_nonce_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_skeenckeyname(atclient_atkey_metadata *metadata, const char *skeenckeyname) { +static int set_ske_enc_key_name(atclient_atkey_metadata *metadata, const char *ske_enc_key_name) { int ret = 1; - const size_t skeenckeynamelen = strlen(skeenckeyname); - const size_t skeenckeynamesize = skeenckeynamelen + 1; - if ((metadata->skeenckeyname = malloc(sizeof(char) * (skeenckeynamesize))) == NULL) { + const size_t ske_enc_key_name_len = strlen(ske_enc_key_name); + const size_t ske_enc_key_name_size = ske_enc_key_name_len + 1; + if ((metadata->ske_enc_key_name = malloc(sizeof(char) * (ske_enc_key_name_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeenckeyname malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_key_name malloc failed\n"); goto exit; } - memcpy(metadata->skeenckeyname, skeenckeyname, skeenckeynamelen); - metadata->skeenckeyname[skeenckeynamelen] = '\0'; - set_is_skeenckeyname_initialized(metadata, true); + memcpy(metadata->ske_enc_key_name, ske_enc_key_name, ske_enc_key_name_len); + metadata->ske_enc_key_name[ske_enc_key_name_len] = '\0'; + set_is_ske_enc_key_name_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } } -static int set_skeencalgo(atclient_atkey_metadata *metadata, const char *skeencalgo) { +static int set_ske_enc_algo(atclient_atkey_metadata *metadata, const char *ske_enc_algo) { int ret = 1; - const size_t skeencalgolen = strlen(skeencalgo); - const size_t skeencalgosize = skeencalgolen + 1; - if ((metadata->skeencalgo = malloc(sizeof(char) * (skeencalgosize))) == NULL) { + const size_t ske_enc_algo_len = strlen(ske_enc_algo); + const size_t ske_enc_algo_size = ske_enc_algo_len + 1; + if ((metadata->ske_enc_algo = malloc(sizeof(char) * (ske_enc_algo_size))) == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_skeencalgo malloc failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_ske_enc_algo malloc failed\n"); goto exit; } - memcpy(metadata->skeencalgo, skeencalgo, skeencalgolen); - metadata->skeencalgo[skeencalgolen] = '\0'; - set_is_skeencalgo_initialized(metadata, true); + memcpy(metadata->ske_enc_algo, ske_enc_algo, ske_enc_algo_len); + metadata->ske_enc_algo[ske_enc_algo_len] = '\0'; + set_is_ske_enc_algo_initialized(metadata, true); ret = 0; goto exit; exit: { return ret; } diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index a7ccbd3e..701886c4 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -173,8 +173,8 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m messagebody); goto exit; } - if (atclient_atnotification_is_isencrypted_initialized(&(message->notification)) && - message->notification.isEncrypted == true) { + if (atclient_atnotification_is_is_encrypted_initialized(&(message->notification)) && + message->notification.is_encrypted == true) { // if key contains \"shared_key\", could be in the middle of string, ignore it if (strstr(message->notification.key, "shared_key") != NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Ignoring shared_key\n"); @@ -206,7 +206,7 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } } else { - atclient_atnotification_set_decryptedvalue(&(message->notification), (unsigned char *)message->notification.value, + atclient_atnotification_set_decrypted_value(&(message->notification), (unsigned char *)message->notification.value, strlen(message->notification.value)); } } else if (strcmp(messagetype, "data") == 0) { @@ -399,115 +399,115 @@ static int parse_notification(atclient_atnotification *notification, const char } } - cJSON *epochMillis = cJSON_GetObjectItem(root, "epochMillis"); - if (epochMillis != NULL) { - if((ret = atclient_atnotification_set_epochmillis(notification, epochMillis->valueint)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epochMillis\n"); + cJSON *epoch_millis = cJSON_GetObjectItem(root, "epoch_millis"); + if (epoch_millis != NULL) { + if((ret = atclient_atnotification_set_epoch_millis(notification, epoch_millis->valueint)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification epoch_millis\n"); goto exit; } } - cJSON *messageType = cJSON_GetObjectItem(root, "messageType"); - if (messageType != NULL) { - if (messageType->type != cJSON_NULL) { - val = messageType->valuestring; - vallen = strlen(messageType->valuestring); + cJSON *message_type = cJSON_GetObjectItem(root, "message_type"); + if (message_type != NULL) { + if (message_type->type != cJSON_NULL) { + val = message_type->valuestring; + vallen = strlen(message_type->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_messagetype(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification messageType\n"); + if((ret = atclient_atnotification_set_message_type(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification message_type\n"); goto exit; } } - cJSON *isEncrypted = cJSON_GetObjectItem(root, "isEncrypted"); - if (isEncrypted != NULL) { - if((ret = atclient_atnotification_set_isencrypted(notification, isEncrypted->valueint)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification isEncrypted\n"); + cJSON *is_encrypted = cJSON_GetObjectItem(root, "is_encrypted"); + if (is_encrypted != NULL) { + if((ret = atclient_atnotification_set_is_encrypted(notification, is_encrypted->valueint)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification is_encrypted\n"); goto exit; } } cJSON *metadata = cJSON_GetObjectItem(root, "metadata"); if (metadata != NULL) { - // get encKeyName - cJSON *encKeyName = cJSON_GetObjectItem(metadata, "encKeyName"); - if (encKeyName != NULL) { - if (encKeyName->type != cJSON_NULL) { - val = encKeyName->valuestring; - vallen = strlen(encKeyName->valuestring); + // get enc_key_name + cJSON *enc_key_name = cJSON_GetObjectItem(metadata, "enc_key_name"); + if (enc_key_name != NULL) { + if (enc_key_name->type != cJSON_NULL) { + val = enc_key_name->valuestring; + vallen = strlen(enc_key_name->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_enckeyname(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encKeyName\n"); + if((ret = atclient_atnotification_set_enc_key_name(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_key_name\n"); goto exit; } } - // get encAlgo - cJSON *encAlgo = cJSON_GetObjectItem(metadata, "encAlgo"); - if (encAlgo != NULL) { - if (encAlgo->type != cJSON_NULL) { - val = encAlgo->valuestring; - vallen = strlen(encAlgo->valuestring); + // get enc_algo + cJSON *enc_algo = cJSON_GetObjectItem(metadata, "enc_algo"); + if (enc_algo != NULL) { + if (enc_algo->type != cJSON_NULL) { + val = enc_algo->valuestring; + vallen = strlen(enc_algo->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_encalgo(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification encAlgo\n"); + if((ret = atclient_atnotification_set_enc_algo(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_algo\n"); goto exit; } } - // get ivNonce - cJSON *ivNonce = cJSON_GetObjectItem(metadata, "ivNonce"); - if (ivNonce != NULL) { - if (ivNonce->type != cJSON_NULL) { - val = ivNonce->valuestring; - vallen = strlen(ivNonce->valuestring); + // get iv_nonce + cJSON *iv_nonce = cJSON_GetObjectItem(metadata, "iv_nonce"); + if (iv_nonce != NULL) { + if (iv_nonce->type != cJSON_NULL) { + val = iv_nonce->valuestring; + vallen = strlen(iv_nonce->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_ivnonce(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ivNonce\n"); + if((ret = atclient_atnotification_set_iv_nonce(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification iv_nonce\n"); goto exit; } } - // get skeEncKeyName - cJSON *skeEncKeyName = cJSON_GetObjectItem(metadata, "skeEncKeyName"); - if (skeEncKeyName != NULL) { - if (skeEncKeyName->type != cJSON_NULL) { - val = skeEncKeyName->valuestring; - vallen = strlen(skeEncKeyName->valuestring); + // get ske_enc_key_name + cJSON *ske_enc_key_name = cJSON_GetObjectItem(metadata, "ske_enc_key_name"); + if (ske_enc_key_name != NULL) { + if (ske_enc_key_name->type != cJSON_NULL) { + val = ske_enc_key_name->valuestring; + vallen = strlen(ske_enc_key_name->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_skeenckeyname(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncKeyName\n"); + if((ret = atclient_atnotification_set_ske_enc_key_name(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_key_name\n"); goto exit; } } - // get skeEncAlgo - cJSON *skeEncAlgo = cJSON_GetObjectItem(metadata, "skeEncAlgo"); - if (skeEncAlgo != NULL) { - if (skeEncAlgo->type != cJSON_NULL) { - val = skeEncAlgo->valuestring; - vallen = strlen(skeEncAlgo->valuestring); + // get ske_enc_algo + cJSON *ske_enc_algo = cJSON_GetObjectItem(metadata, "ske_enc_algo"); + if (ske_enc_algo != NULL) { + if (ske_enc_algo->type != cJSON_NULL) { + val = ske_enc_algo->valuestring; + vallen = strlen(ske_enc_algo->valuestring); } else { val = "null"; vallen = strlen("null"); } - if((ret = atclient_atnotification_set_skeencalgo(notification, val, vallen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification skeEncAlgo\n"); + if((ret = atclient_atnotification_set_ske_enc_algo(notification, val, vallen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_algo\n"); goto exit; } } @@ -523,7 +523,7 @@ exit: { } // after calling `parse_notification`, the *notification struct will be partially filled, all that is left to do is -// decrypt notification->value and put the result in notification->decryptedvalue +// decrypt notification->value and put the result in notification->decrypted_value static int decrypt_notification(atclient *atclient, atclient_atnotification *notification) { int ret = 1; @@ -559,22 +559,22 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 1b. some warnings - if (!atclient_atnotification_is_isencrypted_initialized(notification)) { + if (!atclient_atnotification_is_is_encrypted_initialized(notification)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "isEncrypted field was found to be uninitialized, we don't know for sure if we're decrypting " + "is_encrypted field was found to be uninitialized, we don't know for sure if we're decrypting " "something that's even encrypted.\n"); } else { - if (!notification->isEncrypted) { + if (!notification->is_encrypted) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, - "isEncrypted is false, we may be trying to decrypt some unencrypted plain text.\n"); + "is_encrypted is false, we may be trying to decrypt some unencrypted plain text.\n"); } } // 2. get iv - if (atclient_atnotification_is_ivnonce_initialized(notification) && - !atclient_stringutils_starts_with(notification->ivNonce, "null")) { + if (atclient_atnotification_is_iv_nonce_initialized(notification) && + !atclient_stringutils_starts_with(notification->iv_nonce, "null")) { size_t ivlen; - ret = atchops_base64_decode((unsigned char *)notification->ivNonce, strlen(notification->ivNonce), iv, + ret = atchops_base64_decode((unsigned char *)notification->iv_nonce, strlen(notification->iv_nonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to decode iv\n"); @@ -632,7 +632,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 5. set decrypted value - atclient_atnotification_set_decryptedvalue(notification, decryptedvaluetemp, decryptedvaluetemplen); + atclient_atnotification_set_decrypted_value(notification, decryptedvaluetemp, decryptedvaluetemplen); ret = 0; goto exit; diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 07edab4d..72e393e5 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -67,7 +67,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi if (params->shared_encryption_key == NULL) { char *recipient_atsign_with_at = NULL; - if ((ret = atclient_stringutils_atsign_with_at(params->atkey->sharedwith, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_stringutils_atsign_with_at(params->atkey->shared_with, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", ret); return ret; @@ -101,9 +101,9 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi return ret; } - ret = atclient_atkey_metadata_set_ivnonce(&(params->atkey->metadata), (char *)ivbase64); + ret = atclient_atkey_metadata_set_iv_nonce(&(params->atkey->metadata), (char *)ivbase64); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce failed with code %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce failed with code %d\n", ret); return ret; } @@ -193,7 +193,7 @@ exit: { static size_t calculate_cmd_size(const atclient_notify_params *params, const size_t cmdvaluelen, size_t *atkeyolen, size_t *medatastrolen) { // notify command will look something like this: - // "notify[:messageType:][:priority:][:strategy:][:ttln:]<:atkey_metadata>:[:]\r\n" + // "notify[:message_type:][:priority:][:strategy:][:ttln:]<:atkey_metadata>:[:]\r\n" size_t cmdsize = 0; cmdsize += strlen("notify"); @@ -208,8 +208,8 @@ static size_t calculate_cmd_size(const atclient_notify_params *params, const siz if (atclient_notify_params_is_message_type_initialized(params) && params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { - cmdsize += strlen(":messageType:") + - strlen(atclient_notify_message_type_str[params->message_type]); // ":messageType" + ":text" | ":key" + cmdsize += strlen(":message_type:") + + strlen(atclient_notify_message_type_str[params->message_type]); // ":message_type" + ":text" | ":key" } if (atclient_notify_params_is_priority_initialized(params) && params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { @@ -297,8 +297,8 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval } if (atclient_notify_params_is_message_type_initialized(params) && params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { - snprintf(cmd + off, cmdsize - off, ":messageType:%s", atclient_notify_message_type_str[params->message_type]); - off += strlen(":messageType:") + strlen(atclient_notify_message_type_str[params->message_type]); + snprintf(cmd + off, cmdsize - off, ":message_type:%s", atclient_notify_message_type_str[params->message_type]); + off += strlen(":message_type:") + strlen(atclient_notify_message_type_str[params->message_type]); } if (atclient_notify_params_is_priority_initialized(params) && params->priority != ATCLIENT_NOTIFY_PRIORITY_NONE) { diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c index 0aede01c..20ffb4eb 100644 --- a/packages/atclient/src/notify_params.c +++ b/packages/atclient/src/notify_params.c @@ -20,7 +20,7 @@ void atclient_notify_params_init(atclient_notify_params *params) { params->notification_expiry = 24 * 60 * 60 * 1000; // 24 hours in milliseconds params->should_encrypt = true; params->shared_encryption_key = NULL; - memset(params->_initializedfields, 0, sizeof(params->_initializedfields)); + memset(params->_initialized_fields, 0, sizeof(params->_initialized_fields)); } void atclient_notify_params_free(atclient_notify_params *params) { @@ -40,153 +40,153 @@ void atclient_notify_params_free(atclient_notify_params *params) { } bool atclient_notify_params_is_id_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] & ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] & ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; } bool atclient_notify_params_is_atkey_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; } bool atclient_notify_params_is_value_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] & ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] & ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; } -bool atclient_notify_params_is_shouldencrypt_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] & +bool atclient_notify_params_is_should_encrypt_initialized(atclient_notify_params *params) { + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] & ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; } bool atclient_notify_params_is_operation_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] & ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } bool atclient_notify_params_is_message_type_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] & ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; } bool atclient_notify_params_is_priority_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] & ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; } bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] & ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; } bool atclient_notify_params_is_latest_n_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] & ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; } bool atclient_notify_params_is_notifier_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] & ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; } bool atclient_notify_params_is_notification_expiry_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] & ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; } bool atclient_notify_params_is_shared_encryption_key_initialized(atclient_notify_params *params) { - return params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] & + return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; } void atclient_notify_params_set_id_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; } } void atclient_notify_params_set_atkey_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; } } void atclient_notify_params_set_value_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] |= ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] |= ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; } } -void atclient_notify_params_set_shouldencrypt_initialized(atclient_notify_params *params, const bool initialized) { +void atclient_notify_params_set_should_encrypt_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] |= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] |= ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] &= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; } } void atclient_notify_params_set_operation_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] |= ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] |= ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } } void atclient_notify_params_set_message_type_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] |= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] |= ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] &= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; } } void atclient_notify_params_set_priority_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; } } void atclient_notify_params_set_strategy_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; } } void atclient_notify_params_set_latest_n_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] |= ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] |= ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; } } void atclient_notify_params_set_notifier_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] |= ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] |= ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; } } void atclient_notify_params_set_notification_expiry_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] |= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] &= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; } } @@ -194,26 +194,22 @@ void atclient_notify_params_set_notification_expiry_initialized(atclient_notify_ void atclient_notify_params_set_shared_encryption_key_initialized(atclient_notify_params *params, const bool initialized) { if (initialized) { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] |= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] |= ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; } else { - params->_initializedfields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] &= + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; } } -int atclient_notify_params_set_id(atclient_notify_params *params, const char *id, const size_t id_len) { +int atclient_notify_params_set_id(atclient_notify_params *params, const char *id) { int ret = 1; - if (id_len != 36) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid id length: %lu, should be a UUIDv4\n", id_len); - goto exit; - } if (atclient_notify_params_is_id_initialized(params)) { atclient_notify_params_unset_id(params); } + const size_t id_len = strlen(id); const size_t id_size = id_len + 1; if ((params->id = (char *)malloc(sizeof(char) * id_size)) == NULL) { ret = 1; @@ -254,14 +250,16 @@ int atclient_notify_params_set_atkey(atclient_notify_params *params, const atcli exit: { return ret; } } -int atclient_notify_params_set_value(atclient_notify_params *params, const char *value, const size_t value_len) { +int atclient_notify_params_set_value(atclient_notify_params *params, const char *value) { int ret = 1; if (atclient_notify_params_is_value_initialized(params)) { atclient_notify_params_unset_value(params); } - if ((params->value = (char *)malloc(sizeof(char) * (value_len + 1))) == NULL) { + const size_t value_len = strlen(value); + const size_t value_size = value_len + 1; + if ((params->value = (char *)malloc(sizeof(char) * (value_size))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); goto exit; @@ -278,7 +276,7 @@ exit: { return ret; } int atclient_notify_params_set_should_encrypt(atclient_notify_params *params, const bool should_encrypt) { params->should_encrypt = should_encrypt; - atclient_notify_params_set_shouldencrypt_initialized(params, true); + atclient_notify_params_set_should_encrypt_initialized(params, true); return 0; } @@ -314,15 +312,16 @@ int atclient_notify_params_set_latest_n(atclient_notify_params *params, const in return 0; } -int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier, - const size_t notifier_len) { +int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier) { int ret = 1; if (atclient_notify_params_is_notifier_initialized(params)) { atclient_notify_params_unset_notifier(params); } - if ((params->notifier = (char *)malloc(sizeof(char) * (notifier_len + 1))) == NULL) { + const size_t notifier_len = strlen(notifier); + const size_t notifier_size = notifier_len + 1; + if ((params->notifier = (char *)malloc(sizeof(char) * (notifier_size))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for notifier\n"); goto exit; @@ -398,7 +397,7 @@ void atclient_notify_params_unset_value(atclient_notify_params *params) { void atclient_notify_params_unset_should_encrypt(atclient_notify_params *params) { params->should_encrypt = true; - atclient_notify_params_set_shouldencrypt_initialized(params, false); + atclient_notify_params_set_should_encrypt_initialized(params, false); } void atclient_notify_params_unset_operation(atclient_notify_params *params) { diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/stringutils.c index 92511903..6471a989 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/stringutils.c @@ -5,8 +5,8 @@ #include #include -int atclient_stringutils_trim_whitespace(const char *string, const size_t stringlen, char *out, const size_t outsize, - size_t *outlen) { +int atclient_stringutils_trim_whitespace(const char *string, const size_t string_len, char *out, const size_t out_size, + size_t *out_len) { int ret = 1; if (string == NULL) { @@ -14,7 +14,7 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string goto exit; } - if (stringlen == 0) { + if (string_len == 0) { ret = 1; goto exit; } @@ -25,20 +25,20 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string start++; } - const char *end = string + stringlen - 1; + const char *end = string + string_len - 1; while (end > start && (*end == ' ' || *end == '\t' || *end == '\n')) { end--; } - *outlen = end - start + 1; + *out_len = end - start + 1; - if (*outlen >= outsize) { + if (*out_len >= out_size) { ret = 1; goto exit; } - strncpy(out, start, *outlen); - out[*outlen] = '\0'; + strncpy(out, start, *out_len); + out[*out_len] = '\0'; ret = 0; goto exit; diff --git a/packages/atclient/tests/test_atkey_create.c b/packages/atclient/tests/test_atkey_create.c index 3e6bdda2..bd109f99 100644 --- a/packages/atclient/tests/test_atkey_create.c +++ b/packages/atclient/tests/test_atkey_create.c @@ -49,15 +49,15 @@ static int test1_create_publickey() { const char *expected = "public:test@alice"; const size_t expectedlen = strlen(expected); - if ((ret = atclient_atkey_create_publickey(&atkey, "test", "@alice", NULL)) != 0) { + if ((ret = atclient_atkey_create_public_key(&atkey, "test", "@alice", NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create public key\n"); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -75,14 +75,14 @@ static int test1_create_publickey() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey shared_by is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby.str is not @alice, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by.str is not @alice, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } @@ -128,16 +128,16 @@ static int test2_create_selfkey() { const char *expected = "name@jeremy"; const size_t expectedlen = strlen(expected); - ret = atclient_atkey_create_selfkey(&atkey, "name", "@jeremy", NULL); + ret = atclient_atkey_create_self_key(&atkey, "name", "@jeremy", NULL); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_self_key: %d\n", ret); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELF_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SELF_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -155,14 +155,14 @@ static int test2_create_selfkey() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey shared_by is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@jeremy") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } @@ -210,16 +210,16 @@ static int test3_create_sharedkey() { const char *expected = "@jeremy:name.wavi@chess69lovely"; const size_t expectedlen = strlen(expected); - if ((ret = atclient_atkey_create_sharedkey(&atkey, "name", "@chess69lovely", "@jeremy", "wavi")) != 0) { + if ((ret = atclient_atkey_create_shared_key(&atkey, "name", "@chess69lovely", "@jeremy", "wavi")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey type is not ATCLIENT_ATKEY_TYPE_SHARED_KEY, it is %d\n", atkey_type); goto exit; } @@ -236,28 +236,28 @@ static int test3_create_sharedkey() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if(!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey shared_by is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@chess69lovely") != 0) { + if (strcmp(atkey.shared_by, "@chess69lovely") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @chess69lovely, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @chess69lovely, it is \"%s\"\n", atkey.shared_by); goto exit; } - if(!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if(!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey sharedwith is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey shared_with is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@jeremy") != 0) { + if (strcmp(atkey.shared_with, "@jeremy") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is \"%s\"\n", - atkey.sharedwith); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @jeremy, it is \"%s\"\n", + atkey.shared_with); goto exit; } @@ -267,9 +267,9 @@ static int test3_create_sharedkey() { goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { + if (strcmp(atkey.namespace_str, "wavi") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwithname is not wavi, it is \"%s\"\n", - atkey.sharedwith); + atkey.shared_with); ret = 1; goto exit; } diff --git a/packages/atclient/tests/test_atkey_from_string.c b/packages/atclient/tests/test_atkey_from_string.c index 97b2f29e..d7d3ffd9 100644 --- a/packages/atclient/tests/test_atkey_from_string.c +++ b/packages/atclient/tests/test_atkey_from_string.c @@ -143,21 +143,21 @@ static int test1a_cached_publickey_without_namespace() { goto exit; } - if (atkey.metadata.iscached != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + if (atkey.metadata.is_cached != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not 1\n"); goto exit; } - if (atkey.metadata.ispublic != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + if (atkey.metadata.is_public != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not 1, it is %d\n", + atkey.metadata.is_public); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, it is %d\n", atkey_type); goto exit; } @@ -167,8 +167,8 @@ static int test1a_cached_publickey_without_namespace() { goto exit; } - if (strcmp(atkey.sharedby, "@bob") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @bob, it is \"%s\"\n", atkey.shared_by); goto exit; } ret = 0; @@ -193,24 +193,24 @@ static int test1b_publickey_without_namespace() { } - if(!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { + if(!atclient_atkey_metadata_is_is_public_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + if (atkey.metadata.is_public != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not 1, it is %d\n", + atkey.metadata.is_public); ret = 1; goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, it is %d\n", atkey_type); goto exit; } @@ -227,27 +227,27 @@ static int test1b_publickey_without_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized when it should be\n"); goto exit; } - if (atclient_atkey_is_sharedby_initialized(&atkey) && strcmp(atkey.sharedby, "@alice") != 0) { + if (atclient_atkey_is_shared_by_initialized(&atkey) && strcmp(atkey.shared_by, "@alice") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice, it is \"%s\"\n", atkey.shared_by); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized when it should not be\n"); goto exit; } @@ -272,23 +272,23 @@ static int test1c_publickey_with_namespace() { goto exit; } - if (!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_public_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + if (atkey.metadata.is_public != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not 1, it is %d\n", + atkey.metadata.is_public); ret = 1; goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -306,32 +306,32 @@ static int test1c_publickey_with_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@jeremy") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not \'wavi\', it is \"%s\"\n", - atkey.namespacestr); + if (strcmp(atkey.namespace_str, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not \'wavi\', it is \"%s\"\n", + atkey.namespace_str); ret = 1; goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized, when it should not be\n"); + if (atclient_atkey_is_shared_with_initialized(&atkey)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized, when it should not be\n"); ret = 1; goto exit; } @@ -357,35 +357,35 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_cached_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not initialized\n"); goto exit; } - if (atkey.metadata.iscached != true) { + if (atkey.metadata.is_cached != true) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not 1\n"); goto exit; } - if (!atclient_atkey_metadata_is_ispublic_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_public_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not initialized\n"); goto exit; } - if (atkey.metadata.ispublic != true) { + if (atkey.metadata.is_public != true) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n", - atkey.metadata.ispublic); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_public is not 1, it is %d\n", + atkey.metadata.is_public); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLICKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLICKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.atkeytype is not ATCLIENT_ATKEY_TYPE_PUBLIC_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -403,34 +403,34 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@jeremy") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@jeremy") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr); + if (strcmp(atkey.namespace_str, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not wavi, it is \"%s\"\n", + atkey.namespace_str); ret = 1; goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized, when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized, when it should not be\n"); goto exit; } @@ -458,8 +458,8 @@ static int test2a_sharedkey_with_namespace() { const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHARED_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -477,39 +477,39 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@bob") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @bob, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @bob, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@alice") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @alice, it is \"%s\"\n", atkey.sharedwith); + if (strcmp(atkey.shared_with, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @alice, it is \"%s\"\n", atkey.shared_with); ret = 1; goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr); + if (strcmp(atkey.namespace_str, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not wavi, it is \"%s\"\n", + atkey.namespace_str); ret = 1; goto exit; } @@ -535,22 +535,22 @@ static int test2b_cached_sharedkey_without_namespace() { goto exit; } - if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_cached_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not initialized\n"); goto exit; } - if (atkey.metadata.iscached != true) { + if (atkey.metadata.is_cached != true) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not 1\n"); goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHARED_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -568,34 +568,34 @@ static int test2b_cached_sharedkey_without_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice") != 0) { + if (strcmp(atkey.shared_by, "@alice") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice, it is \"%s\"\n", atkey.shared_by); goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@bob") != 0) { + if (strcmp(atkey.shared_with, "@bob") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @bob, it is \"%s\"\n", atkey.shared_with); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.namespacestr is initialized when it is not supposed to be\n"); + "atkey.namespace_str is initialized when it is not supposed to be\n"); goto exit; } @@ -622,8 +622,8 @@ static int test2c_sharedkey_without_namespace() { const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHARED_KEY, it is %d\n", atkey_type); ret = 1; goto exit; @@ -641,26 +641,26 @@ static int test2c_sharedkey_without_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@bob") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); + if (strcmp(atkey.shared_with, "@bob") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @bob, it is \"%s\"\n", atkey.shared_with); ret = 1; goto exit; } @@ -668,7 +668,7 @@ static int test2c_sharedkey_without_namespace() { if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atkey.namespacestr is initialized when it is not supposed to be\n"); + "atkey.namespace_str is initialized when it is not supposed to be\n"); goto exit; } @@ -693,23 +693,23 @@ static int test2d_cached_sharedkey_with_namespace() { goto exit; } - if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_cached_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not initialized\n"); goto exit; } - if (atkey.metadata.iscached != true) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n"); + if (atkey.metadata.is_cached != true) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not 1\n"); ret = 1; goto exit; } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SHAREDKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHAREDKEY, it is %d\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SHARED_KEY, it is %d\n", atkey_type); goto exit; } @@ -726,39 +726,39 @@ static int test2d_cached_sharedkey_with_namespace() { goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice") != 0) { + if (strcmp(atkey.shared_by, "@alice") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice, it is \"%s\"\n", atkey.shared_by); goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@bob") != 0) { + if (strcmp(atkey.shared_with, "@bob") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @bob, it is \"%s\"\n", atkey.sharedwith); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @bob, it is \"%s\"\n", atkey.shared_with); goto exit; } if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr); + if (strcmp(atkey.namespace_str, "wavi") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not wavi, it is \"%s\"\n", + atkey.namespace_str); ret = 1; goto exit; } @@ -782,27 +782,27 @@ static int test2e_sharedkey_with_compounding_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if(!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@xavierbob123") != 0) { + if (strcmp(atkey.shared_by, "@xavierbob123") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy, it is %s\n", atkey.shared_by); goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@alice") != 0) { + if (strcmp(atkey.shared_with, "@alice") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @jeremy, it is %s\n", atkey.shared_with); goto exit; } @@ -820,14 +820,14 @@ static int test2e_sharedkey_with_compounding_namespace() { if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "vpsx.sshnp.abcd.efgh") != 0) { + if (strcmp(atkey.namespace_str, "vpsx.sshnp.abcd.efgh") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", - atkey.namespacestr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not vps1.sshnp, it is %s\n", + atkey.namespace_str); goto exit; } @@ -850,39 +850,39 @@ static int test2f_cached_sharedkey_with_compounding_namespace() { goto exit; } - if (!atclient_atkey_metadata_is_iscached_initialized(&atkey.metadata)) { + if (!atclient_atkey_metadata_is_is_cached_initialized(&atkey.metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is not initialized when it should be\n"); goto exit; } - if (atkey.metadata.iscached != true) { + if (atkey.metadata.is_cached != true) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is false when it should be true\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_cached is false when it should be true\n"); goto exit; } - if (!atclient_atkey_is_sharedby_initialized(&atkey)) { + if (!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@xavier") != 0) { + if (strcmp(atkey.shared_by, "@xavier") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy, it is %s\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy, it is %s\n", atkey.shared_by); goto exit; } - if (!atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (!atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.sharedwith, "@jeremy") != 0) { + if (strcmp(atkey.shared_with, "@jeremy") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is not @jeremy, it is %s\n", atkey.sharedwith); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is not @jeremy, it is %s\n", atkey.shared_with); goto exit; } @@ -900,14 +900,14 @@ static int test2f_cached_sharedkey_with_compounding_namespace() { if (!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "vps1.sshnp") != 0) { + if (strcmp(atkey.namespace_str, "vps1.sshnp") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not vps1.sshnp, it is %s\n", - atkey.namespacestr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not vps1.sshnp, it is %s\n", + atkey.namespace_str); goto exit; } @@ -934,7 +934,7 @@ static int test3a_privatehiddenkey_without_namespace() { const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELF_KEY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_PRIVATEHIDDENKEY, it is %d\n", atkey_type); ret = 1; @@ -953,28 +953,28 @@ static int test3a_privatehiddenkey_without_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if(!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice123_4😘") != 0) { + if (strcmp(atkey.shared_by, "@alice123_4😘") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice123_4😘, it is \"%s\"\n", - atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice123_4😘, it is \"%s\"\n", + atkey.shared_by); goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized when it should not be\n"); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is initialized when it should not be\n"); goto exit; } @@ -1002,9 +1002,9 @@ static int test4a_selfkey_without_namespace() { const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELF_KEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELF_KEY, it is %d\n", atkey_type); goto exit; } @@ -1021,27 +1021,27 @@ static int test4a_selfkey_without_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if(!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@alice") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @alice, it is \"%s\"\n", atkey.sharedby); + if (strcmp(atkey.shared_by, "@alice") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @alice, it is \"%s\"\n", atkey.shared_by); ret = 1; goto exit; } - if(atclient_atkey_is_sharedwith_initialized(&atkey)) { + if(atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized when it should not be\n"); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is initialized when it should not be\n"); goto exit; } @@ -1067,9 +1067,9 @@ static int test4b_selfkey_with_namespace() { } const atclient_atkey_type atkey_type = atclient_atkey_get_type(&atkey); - if (atkey_type != ATCLIENT_ATKEY_TYPE_SELFKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SELF_KEY) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELFKEY, it is %d\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATCLIENT_ATKEY_TYPE_SELF_KEY, it is %d\n", atkey_type); goto exit; } @@ -1086,34 +1086,34 @@ static int test4b_selfkey_with_namespace() { goto exit; } - if(!atclient_atkey_is_sharedby_initialized(&atkey)) { + if(!atclient_atkey_is_shared_by_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.sharedby, "@jeremy_0") != 0) { + if (strcmp(atkey.shared_by, "@jeremy_0") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedby is not @jeremy_0, it is \"%s\"\n", atkey.sharedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_by is not @jeremy_0, it is \"%s\"\n", atkey.shared_by); goto exit; } if(!atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not initialized, when it should be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not initialized, when it should be\n"); goto exit; } - if (strcmp(atkey.namespacestr, "wavi") != 0) { + if (strcmp(atkey.namespace_str, "wavi") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespacestr is not wavi, it is \"%s\"\n", - atkey.namespacestr); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.namespace_str is not wavi, it is \"%s\"\n", + atkey.namespace_str); goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.sharedwith is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.shared_with is initialized when it should not be\n"); goto exit; } diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index 57471aad..d4e61412 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -15,7 +15,7 @@ // "version":0, // "ttl":86400, // "isBinary":false, -// "isEncrypted":false +// "is_encrypted":false // } #define TAG "test_atkey_metadata" @@ -32,7 +32,7 @@ \"version\":0, \ \"ttl\":86400, \ \"isBinary\":false, \ - \"isEncrypted\":false \ + \"is_encrypted\":false \ }" static int test_atkey_metadata_from_jsonstr() { @@ -41,72 +41,72 @@ static int test_atkey_metadata_from_jsonstr() { atclient_atkey_metadata metadata; atclient_atkey_metadata_init(&metadata); - if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed\n"); + if ((ret = atclient_atkey_metadata_from_json_str(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str failed\n"); goto exit; } - if (!atclient_atkey_metadata_is_createdby_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_created_by_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdby_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_created_by_initialized failed\n"); goto exit; } - if (strcmp(metadata.createdby, "@qt_thermostat") != 0) { + if (strcmp(metadata.created_by, "@qt_thermostat") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdby != @qt_thermostat: %s", metadata.createdby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.created_by != @qt_thermostat: %s", metadata.created_by); goto exit; } - if (!atclient_atkey_metadata_is_updatedby_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_updated_by_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedby_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updated_by_initialized failed\n"); goto exit; } - if (strcmp(metadata.updatedby, "@qt_thermostat") != 0) { + if (strcmp(metadata.updated_by, "@qt_thermostat") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedby.atsign != @qt_thermostat: %s\n", - metadata.updatedby); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updated_by.atsign != @qt_thermostat: %s\n", + metadata.updated_by); goto exit; } - if (!atclient_atkey_metadata_is_createdat_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_created_at_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_createdat_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_created_at_initialized failed\n"); goto exit; } - if (strcmp(metadata.createdat, "2024-02-17 19:54:12.037Z") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.createdat != 2024-02-17 19:54:12.037Z: %s\n", - metadata.createdat); + if (strcmp(metadata.created_at, "2024-02-17 19:54:12.037Z") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.created_at != 2024-02-17 19:54:12.037Z: %s\n", + metadata.created_at); ret = 1; goto exit; } - if (!atclient_atkey_metadata_is_updatedat_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_updated_at_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updatedat_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_updated_at_initialized failed\n"); goto exit; } - if (strcmp(metadata.updatedat, "2024-02-17 19:54:12.037Z") != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updatedat != 2024-02-17 19:54:12.037Z: %s\n", - metadata.updatedat); + if (strcmp(metadata.updated_at, "2024-02-17 19:54:12.037Z") != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.updated_at != 2024-02-17 19:54:12.037Z: %s\n", + metadata.updated_at); ret = 1; goto exit; } - if (!atclient_atkey_metadata_is_expiresat_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_expires_at_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_expiresat_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_expires_at_initialized failed\n"); goto exit; } - if (strcmp(metadata.expiresat, "2024-02-17 19:55:38.437Z") != 0) { + if (strcmp(metadata.expires_at, "2024-02-17 19:55:38.437Z") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expiresat != 2024-02-17 19:55:38.437Z: %s\n", - metadata.expiresat); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.expires_at != 2024-02-17 19:55:38.437Z: %s\n", + metadata.expires_at); goto exit; } @@ -146,76 +146,76 @@ static int test_atkey_metadata_from_jsonstr() { goto exit; } - if (!atclient_atkey_metadata_is_isbinary_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_is_binary_initialized(&metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isbinary_initialized failed\n"); goto exit; } - if (metadata.isbinary != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isbinary != false: %d\n", metadata.isbinary); + if (metadata.is_binary != false) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.is_binary != false: %d\n", metadata.is_binary); ret = 1; goto exit; } - if (!atclient_atkey_metadata_is_isencrypted_initialized(&metadata)) { + if (!atclient_atkey_metadata_is_is_encrypted_initialized(&metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isencrypted_initialized failed\n"); goto exit; } - if (metadata.isencrypted != false) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.isencrypted != false: %d\n", metadata.isencrypted); + if (metadata.is_encrypted != false) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "metadata.is_encrypted != false: %d\n", metadata.is_encrypted); ret = 1; goto exit; } - if (atclient_atkey_metadata_is_iscached_initialized(&metadata)) { + if (atclient_atkey_metadata_is_is_cached_initialized(&metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_iscached_initialized was initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_availableat_initialized(&metadata)) { + if (atclient_atkey_metadata_is_available_at_initialized(&metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isavailableat_initialized is intiialized when it should not be\n"); goto exit; } - if(atclient_atkey_metadata_is_refreshat_initialized(&metadata)) { + if(atclient_atkey_metadata_is_refresh_at_initialized(&metadata)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isrefreshat_initialized is intiialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_datasignature_initialized(&metadata)) { + if (atclient_atkey_metadata_is_data_signature_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "datasignature is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "data_signature is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_sharedkeystatus_initialized(&metadata)) { + if (atclient_atkey_metadata_is_shared_key_status_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedby is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_sharedkeyenc_initialized(&metadata)) { + if (atclient_atkey_metadata_is_shared_key_enc_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedkeyenc is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_key_enc is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_pubkeyhash_initialized(&metadata)) { + if (atclient_atkey_metadata_is_pub_key_hash_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyhash is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pub_key_hash is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_pubkeyalgo_initialized(&metadata)) { + if (atclient_atkey_metadata_is_pub_key_algo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pubkeyalgo is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pub_key_algo is initialized when it should not be\n"); goto exit; } @@ -225,33 +225,33 @@ static int test_atkey_metadata_from_jsonstr() { goto exit; } - if (atclient_atkey_metadata_is_enckeyname_initialized(&metadata)) { + if (atclient_atkey_metadata_is_enc_key_name_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enckeyname is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enc_key_name is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_encalgo_initialized(&metadata)) { + if (atclient_atkey_metadata_is_enc_algo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "encalgo is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "enc_algo is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_ivnonce_initialized(&metadata)) { + if (atclient_atkey_metadata_is_iv_nonce_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ivnonce is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "iv_nonce is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_skeenckeyname_initialized(&metadata)) { + if (atclient_atkey_metadata_is_ske_enc_key_name_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeenckeyname is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ske_enc_key_name is initialized when it should not be\n"); goto exit; } - if (atclient_atkey_metadata_is_skeencalgo_initialized(&metadata)) { + if (atclient_atkey_metadata_is_ske_enc_algo_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "skeencalgo is initialized when it should not be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ske_enc_algo is initialized when it should not be\n"); goto exit; } @@ -266,17 +266,17 @@ exit: { static int test_atkey_metadata_to_protocolstr() { int ret = 1; - const char *expected = ":ttr:-1:isBinary:true:isEncrypted:true:ivNonce:abcdefghijk"; + const char *expected = ":ttr:-1:isBinary:true:is_encrypted:true:iv_nonce:abcdefghijk"; const size_t expectedlen = strlen(expected); atclient_atkey_metadata metadata; atclient_atkey_metadata_init(&metadata); atclient_atkey_metadata_set_ttr(&metadata, -1); - atclient_atkey_metadata_set_isbinary(&metadata, true); - atclient_atkey_metadata_set_isencrypted(&metadata, true); - atclient_atkey_metadata_set_iscached(&metadata, true); - atclient_atkey_metadata_set_ivnonce(&metadata, "abcdefghijk"); + atclient_atkey_metadata_set_is_binary(&metadata, true); + atclient_atkey_metadata_set_is_encrypted(&metadata, true); + atclient_atkey_metadata_set_is_cached(&metadata, true); + atclient_atkey_metadata_set_iv_nonce(&metadata, "abcdefghijk"); char *protocolfragment = NULL; const size_t expected_protocolframent_len = atclient_atkey_metadata_protocol_strlen(&metadata); @@ -326,13 +326,13 @@ static int test_atkey_metadata_to_jsonstr() { char *jsonstr = NULL; - if ((ret = atclient_atkey_metadata_from_jsonstr(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_jsonstr failed"); + if ((ret = atclient_atkey_metadata_from_json_str(&metadata, TEST_ATKEY_METADATA_FROM_JSONSTR)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str failed"); goto exit; } - if ((ret = atclient_atkey_metadata_to_jsonstr(&metadata, &jsonstr)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_jsonstr failed"); + if ((ret = atclient_atkey_metadata_to_json_str(&metadata, &jsonstr)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_json_str failed"); goto exit; } diff --git a/packages/atclient/tests/test_atkey_to_string.c b/packages/atclient/tests/test_atkey_to_string.c index 615fa158..3dca1078 100644 --- a/packages/atclient/tests/test_atkey_to_string.c +++ b/packages/atclient/tests/test_atkey_to_string.c @@ -138,13 +138,13 @@ static int test1a_cached_publickey_without_namespace() { const char *expected = TEST_ATKEY_TO_STRING_1A; - if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + if((ret = atclient_atkey_metadata_set_is_cached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_cached failed\n"); goto exit; } - if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + if((ret = atclient_atkey_metadata_set_is_public(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_public failed\n"); goto exit; } @@ -153,8 +153,8 @@ static int test1a_cached_publickey_without_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@bob")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -190,8 +190,8 @@ static int test1b_publickey_without_namespace() { const char *expected = TEST_ATKEY_TO_STRING_1B; // "public:publickey@alice" - if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + if((ret = atclient_atkey_metadata_set_is_public(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_public failed\n"); goto exit; } @@ -200,8 +200,8 @@ static int test1b_publickey_without_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -236,8 +236,8 @@ static int test1c_publickey_with_namespace() { const char *expected = TEST_ATKEY_TO_STRING_1C; // "public:name.wavi@jeremy" - if ((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + if ((ret = atclient_atkey_metadata_set_is_public(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_public failed\n"); goto exit; } @@ -246,13 +246,13 @@ static int test1c_publickey_with_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@jeremy")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if ((ret = atclient_atkey_set_namespacestr(&(atkey), "wavi")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(&(atkey), "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } @@ -288,13 +288,13 @@ static int test1d_cached_publickey_with_namespace() { const char *expected = TEST_ATKEY_TO_STRING_1D; // "cached:public:name.wavi@jeremy" const size_t expectedlen = strlen(expected); - if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + if((ret = atclient_atkey_metadata_set_is_cached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_cached failed\n"); goto exit; } - if((ret = atclient_atkey_metadata_set_ispublic(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ispublic failed\n"); + if((ret = atclient_atkey_metadata_set_is_public(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_public failed\n"); goto exit; } @@ -303,13 +303,13 @@ static int test1d_cached_publickey_with_namespace() { goto exit; } - if ((ret = atclient_atkey_set_namespacestr(&(atkey), "wavi")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(&(atkey), "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@jeremy")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -349,18 +349,18 @@ static int test2a_sharedkey_with_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@bob")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedwith(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if ((ret = atclient_atkey_set_shared_with(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } @@ -395,13 +395,13 @@ static int test2b_cached_sharedkey_without_namespace() { const char *expected = TEST_ATKEY_TO_STRING_2B; // "cached:@bob:name@alice" - if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + if((ret = atclient_atkey_metadata_set_is_cached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_cached failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if ((ret = atclient_atkey_set_shared_with(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } @@ -410,8 +410,8 @@ static int test2b_cached_sharedkey_without_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -447,8 +447,8 @@ static int test2c_sharedkey_without_namespace() { const char *expected = TEST_ATKEY_TO_STRING_2C; // "@bob:name@alice" - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -457,8 +457,8 @@ static int test2c_sharedkey_without_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if ((ret = atclient_atkey_set_shared_with(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } @@ -469,7 +469,7 @@ static int test2c_sharedkey_without_namespace() { if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespace_str is initialized when it isn't supposed to be\n"); goto exit; } @@ -499,13 +499,13 @@ static int test2d_cached_sharedkey_with_namespace() { const char *expected = TEST_ATKEY_TO_STRING_2D; // "cached:@bob:name.wavi@alice" - if((ret = atclient_atkey_metadata_set_iscached(&(atkey.metadata), true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iscached failed\n"); + if((ret = atclient_atkey_metadata_set_is_cached(&(atkey.metadata), true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_is_cached failed\n"); goto exit; } - if ((ret = atclient_atkey_set_sharedwith(&atkey, "@bob")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedwith failed\n"); + if ((ret = atclient_atkey_set_shared_with(&atkey, "@bob")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_with failed\n"); goto exit; } @@ -514,13 +514,13 @@ static int test2d_cached_sharedkey_with_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } @@ -560,20 +560,20 @@ static int test3a_privatehiddenkey() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice123_4😘")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice123_4😘")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is initialized when it isn't supposed to be\n"); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespace_str is initialized when it isn't supposed to be\n"); goto exit; } @@ -614,8 +614,8 @@ static int test4a_selfkey_without_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@alice")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@alice")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } @@ -624,15 +624,15 @@ static int test4a_selfkey_without_namespace() { goto exit; } - if (atclient_atkey_is_sharedwith_initialized(&atkey)) { + if (atclient_atkey_is_shared_with_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "sharedwith is initialized when it isn't supposed to be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is initialized when it isn't supposed to be\n"); goto exit; } if (atclient_atkey_is_namespacestr_initialized(&atkey)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespacestr is initialized when it isn't supposed to be\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "namespace_str is initialized when it isn't supposed to be\n"); goto exit; } @@ -669,13 +669,13 @@ static int test4b_selfkey_with_namespace() { goto exit; } - if ((ret = atclient_atkey_set_sharedby(&atkey, "@jeremy_0")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_sharedby failed\n"); + if ((ret = atclient_atkey_set_shared_by(&atkey, "@jeremy_0")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_shared_by failed\n"); goto exit; } - if ((ret = atclient_atkey_set_namespacestr(&atkey, "wavi")) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespacestr failed\n"); + if ((ret = atclient_atkey_set_namespace_str(&atkey, "wavi")) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_set_namespace_str failed\n"); goto exit; } diff --git a/tests/functional_tests/lib/include/functional_tests/helpers.h b/tests/functional_tests/lib/include/functional_tests/helpers.h index a221cc89..949ae489 100644 --- a/tests/functional_tests/lib/include/functional_tests/helpers.h +++ b/tests/functional_tests/lib/include/functional_tests/helpers.h @@ -6,10 +6,10 @@ int functional_tests_set_up_atkeys(atclient_atkeys *atkeys, const char *atsign, const size_t atsignlen); int functional_tests_pkam_auth(atclient *atclient, atclient_atkeys *atkeys, const char *atsign, const size_t atsignlen); -int functional_tests_publickey_exists(atclient *atclient, const char *key, const char *sharedby, +int functional_tests_publickey_exists(atclient *atclient, const char *key, const char *shared_by, const char *knamespace); -int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *sharedby, const char *knamespace); -int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *sharedby, const char *sharedwith, +int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *knamespace); +int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *shared_with, const char *knamespace); int functional_tests_tear_down_sharedenckeys(atclient *atclient, const char *recipient); diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index d3925246..85bce0f7 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -65,7 +65,7 @@ exit: { } } -int functional_tests_publickey_exists(atclient *atclient, const char *key, const char *sharedby, +int functional_tests_publickey_exists(atclient *atclient, const char *key, const char *shared_by, const char *knamespace) { int ret = -1; @@ -88,9 +88,9 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const size_t recvlen = 0; if (knamespace == NULL) { - snprintf(atkeystr, atkeystrsize, "%s%s", key, sharedby); + snprintf(atkeystr, atkeystrsize, "%s%s", key, shared_by); } else { - snprintf(atkeystr, atkeystrsize, "%s.%s%s", key, knamespace, sharedby); + snprintf(atkeystr, atkeystrsize, "%s.%s%s", key, knamespace, shared_by); } snprintf(command, commandsize, "plookup:%s\r\n", atkeystr); @@ -120,7 +120,7 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const exit: { return ret; } } -int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *sharedby, const char *knamespace) { +int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *knamespace) { int ret = -1; atclient_atkey atkey; @@ -142,9 +142,9 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c size_t recvlen = 0; if (knamespace == NULL) { - snprintf(atkeystr, atkeystrsize, "%s%s", key, sharedby); + snprintf(atkeystr, atkeystrsize, "%s%s", key, shared_by); } else { - snprintf(atkeystr, atkeystrsize, "%s.%s%s", key, knamespace, sharedby); + snprintf(atkeystr, atkeystrsize, "%s.%s%s", key, knamespace, shared_by); } snprintf(command, commandsize, "llookup:%s\r\n", atkeystr); @@ -174,7 +174,7 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c exit: { return ret; } } -int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *sharedby, const char *sharedwith, +int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *shared_with, const char *knamespace) { int ret = -1; @@ -197,9 +197,9 @@ int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const size_t recvlen = 0; if (knamespace == NULL) { - snprintf(atkeystr, atkeystrsize, "%s:%s%s", sharedwith, key, sharedby); + snprintf(atkeystr, atkeystrsize, "%s:%s%s", shared_with, key, shared_by); } else { - snprintf(atkeystr, atkeystrsize, "%s:%s.%s%s", sharedwith, key, knamespace, sharedby); + snprintf(atkeystr, atkeystrsize, "%s:%s.%s%s", shared_with, key, knamespace, shared_by); } snprintf(command, commandsize, "lookup:%s\r\n", atkeystr); diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 19d9fb0d..7c271fb6 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -170,7 +170,7 @@ static int send_notification(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; @@ -225,16 +225,16 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) continue; } - if (!atclient_atnotification_is_decryptedvalue_initialized(&(message.notification))) { + if (!atclient_atnotification_is_decrypted_value_initialized(&(message.notification))) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted value is not initialized\n"); tries++; continue; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted Value: %s\n",message.notification.decryptedvalue); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Decrypted Value: %s\n",message.notification.decrypted_value); // compare the decrypted value with the expected value - if (strcmp(message.notification.decryptedvalue, ATKEY_VALUE) != 0) { + if (strcmp(message.notification.decrypted_value, ATKEY_VALUE) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Decrypted value does not match expected value\n"); tries++; continue; diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index d151e049..0ed0e391 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -158,7 +158,7 @@ static int test_1_notify(atclient *atclient, char *notification_id) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; @@ -213,7 +213,7 @@ static int test_2_notify_long_text(atclient *atclient, char *notification_id) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create atkey: %d\n", ret); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index 9c7898d8..b2fff6bb 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -111,8 +111,8 @@ static int test_1_put(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } @@ -144,8 +144,8 @@ static int test_2_get(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } @@ -178,8 +178,8 @@ static int test_3_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } @@ -227,16 +227,16 @@ static int test_5_put_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } atclient_atkey_metadata_set_ccd(&(atkey.metadata), ATKEY_CCD); atclient_atkey_metadata_set_ttl(&(atkey.metadata), ATKEY_TTL); atclient_atkey_metadata_set_ttr(&(atkey.metadata), ATKEY_TTR); - atclient_atkey_metadata_set_isencrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); - atclient_atkey_metadata_set_isbinary(&(atkey.metadata), ATKEY_ISBINARY); + atclient_atkey_metadata_set_is_encrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); + atclient_atkey_metadata_set_is_binary(&(atkey.metadata), ATKEY_ISBINARY); if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put\n"); @@ -266,8 +266,8 @@ static int test_6_get_with_metadata(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } @@ -304,20 +304,20 @@ static int test_6_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttr: %d\n", atkey.metadata.ttr); - if (atclient_atkey_metadata_is_isencrypted_initialized(&(atkey.metadata)) && - atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed isencrypted comparison, got %d and expected %d\n", - atkey.metadata.isencrypted, ATKEY_ISENCRYPTED); + if (atclient_atkey_metadata_is_is_encrypted_initialized(&(atkey.metadata)) && + atkey.metadata.is_encrypted != ATKEY_ISENCRYPTED) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed is_encrypted comparison, got %d and expected %d\n", + atkey.metadata.is_encrypted, ATKEY_ISENCRYPTED); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.isencrypted: %d\n", atkey.metadata.isencrypted); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.is_encrypted: %d\n", atkey.metadata.is_encrypted); - if (atclient_atkey_metadata_is_isbinary_initialized(&(atkey.metadata)) && atkey.metadata.isbinary != ATKEY_ISBINARY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed isbinary comparison, got %d and expected %d\n", - atkey.metadata.isbinary, ATKEY_ISBINARY); + if (atclient_atkey_metadata_is_is_binary_initialized(&(atkey.metadata)) && atkey.metadata.is_binary != ATKEY_ISBINARY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed is_binary comparison, got %d and expected %d\n", + atkey.metadata.is_binary, ATKEY_ISBINARY); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.isbinary: %d\n", atkey.metadata.isbinary); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.is_binary: %d\n", atkey.metadata.is_binary); goto exit; exit: { @@ -335,8 +335,8 @@ static int test_7_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_publickey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_publickey\n"); + if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index ba377c1a..fea9493b 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -133,8 +133,8 @@ static int test_2_put(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_self_key: %d\n", ret); goto exit; } @@ -166,7 +166,7 @@ static int test_3_get(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -202,8 +202,8 @@ static int test_4_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_self_key: %d\n", ret); goto exit; } @@ -249,14 +249,14 @@ static int test_6_put_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_self_key: %d\n", ret); goto exit; } atclient_atkey_metadata_set_ttl(&(atkey.metadata), ATKEY_TTL); - atclient_atkey_metadata_set_isencrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); - atclient_atkey_metadata_set_isbinary(&(atkey.metadata), ATKEY_ISBINARY); + atclient_atkey_metadata_set_is_encrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); + atclient_atkey_metadata_set_is_binary(&(atkey.metadata), ATKEY_ISBINARY); if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); @@ -286,7 +286,7 @@ static int test_7_get_with_metadata(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -311,21 +311,21 @@ static int test_7_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttl: %d\n", atkey.metadata.ttl); - if (atkey.metadata.isencrypted != ATKEY_ISENCRYPTED) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.isencrypted: %d != %d\n", - atkey.metadata.isencrypted, true); + if (atkey.metadata.is_encrypted != ATKEY_ISENCRYPTED) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_encrypted: %d != %d\n", + atkey.metadata.is_encrypted, true); ret = 1; goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.isencrypted: %d\n", atkey.metadata.isencrypted); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.is_encrypted: %d\n", atkey.metadata.is_encrypted); - if (atkey.metadata.isbinary != ATKEY_ISBINARY) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.isbinary: %d != %d\n", atkey.metadata.isbinary, + if (atkey.metadata.is_binary != ATKEY_ISBINARY) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_binary: %d != %d\n", atkey.metadata.is_binary, false); ret = 1; goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.isbinary: %d\n", atkey.metadata.isbinary); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.is_binary: %d\n", atkey.metadata.is_binary); ret = 0; goto exit; @@ -344,8 +344,8 @@ static int test_8_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_selfkey(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_selfkey: %d\n", ret); + if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_self_key: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 6a30997c..5ca84d96 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -119,9 +119,9 @@ static int test_1_put(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); goto exit; } @@ -155,9 +155,9 @@ static int test_2_get_as_sharedby(atclient *atclient) { memset(value, 0, sizeof(char) * valuesize); size_t valuelen = 0; - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); goto exit; } @@ -211,9 +211,9 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); goto exit; } @@ -262,9 +262,9 @@ static int test_4_delete(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - if ((ret = atclient_atkey_create_sharedkey(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_sharedkey: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); goto exit; } From c414e467102c0c6c4f298b642b5ba021b4363011 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 11:59:29 -0400 Subject: [PATCH 114/193] fix: syntax errors --- .../atclient/include/atclient/notify_params.h | 2 +- packages/atclient/src/monitor.c | 53 +++++-------------- .../tests/test_atclient_monitor.c | 2 +- .../tests/test_atclient_notify.c | 4 +- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/packages/atclient/include/atclient/notify_params.h b/packages/atclient/include/atclient/notify_params.h index 90a10a64..2af30c1c 100644 --- a/packages/atclient/include/atclient/notify_params.h +++ b/packages/atclient/include/atclient/notify_params.h @@ -137,7 +137,7 @@ int atclient_notify_params_set_strategy(atclient_notify_params *params, const en int atclient_notify_params_set_latest_n(atclient_notify_params *params, const int latest_n); int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier); int atclient_notify_params_set_notification_expiry(atclient_notify_params *params, const unsigned long notification_expiry); -int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, const unsigned char *shared_encryption_key); +int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, const unsigned char *shared_encryption_key, const size_t shared_encryption_key_len); void atclient_notify_params_unset_id(atclient_notify_params *params); void atclient_notify_params_unset_atkey(atclient_notify_params *params); diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 701886c4..ace03cae 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -206,8 +206,7 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } } else { - atclient_atnotification_set_decrypted_value(&(message->notification), (unsigned char *)message->notification.value, - strlen(message->notification.value)); + atclient_atnotification_set_decrypted_value(&(message->notification), (unsigned char *)message->notification.value); } } else if (strcmp(messagetype, "data") == 0) { message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE; @@ -313,12 +312,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (id != NULL) { if (id->type != cJSON_NULL) { val = id->valuestring; - vallen = strlen(id->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_id(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_id(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification id\n"); goto exit; } @@ -328,12 +325,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (from != NULL) { if (from->type != cJSON_NULL) { val = from->valuestring; - vallen = strlen(from->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_from(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_from(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification from\n"); goto exit; } @@ -343,12 +338,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (to != NULL) { if (to->type != cJSON_NULL) { val = to->valuestring; - vallen = strlen(to->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_to(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_to(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification to\n"); goto exit; } @@ -358,12 +351,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (key != NULL) { if (key->type != cJSON_NULL) { val = key->valuestring; - vallen = strlen(key->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_key(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_key(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification key\n"); goto exit; } @@ -373,12 +364,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (value != NULL) { if (value->type != cJSON_NULL) { val = value->valuestring; - vallen = strlen(value->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_value(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_value(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification value\n"); goto exit; } @@ -388,12 +377,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (operation != NULL) { if (operation->type != cJSON_NULL) { val = operation->valuestring; - vallen = strlen(operation->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_operation(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_operation(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification operation\n"); goto exit; } @@ -411,12 +398,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (message_type != NULL) { if (message_type->type != cJSON_NULL) { val = message_type->valuestring; - vallen = strlen(message_type->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_message_type(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_message_type(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification message_type\n"); goto exit; } @@ -437,12 +422,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (enc_key_name != NULL) { if (enc_key_name->type != cJSON_NULL) { val = enc_key_name->valuestring; - vallen = strlen(enc_key_name->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_enc_key_name(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_enc_key_name(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_key_name\n"); goto exit; } @@ -453,12 +436,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (enc_algo != NULL) { if (enc_algo->type != cJSON_NULL) { val = enc_algo->valuestring; - vallen = strlen(enc_algo->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_enc_algo(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_enc_algo(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification enc_algo\n"); goto exit; } @@ -469,12 +450,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (iv_nonce != NULL) { if (iv_nonce->type != cJSON_NULL) { val = iv_nonce->valuestring; - vallen = strlen(iv_nonce->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_iv_nonce(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_iv_nonce(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification iv_nonce\n"); goto exit; } @@ -485,12 +464,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (ske_enc_key_name != NULL) { if (ske_enc_key_name->type != cJSON_NULL) { val = ske_enc_key_name->valuestring; - vallen = strlen(ske_enc_key_name->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_ske_enc_key_name(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_ske_enc_key_name(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_key_name\n"); goto exit; } @@ -501,12 +478,10 @@ static int parse_notification(atclient_atnotification *notification, const char if (ske_enc_algo != NULL) { if (ske_enc_algo->type != cJSON_NULL) { val = ske_enc_algo->valuestring; - vallen = strlen(ske_enc_algo->valuestring); } else { val = "null"; - vallen = strlen("null"); } - if((ret = atclient_atnotification_set_ske_enc_algo(notification, val, vallen)) != 0) { + if((ret = atclient_atnotification_set_ske_enc_algo(notification, val)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set notification ske_enc_algo\n"); goto exit; } @@ -632,7 +607,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 5. set decrypted value - atclient_atnotification_set_decrypted_value(notification, decryptedvaluetemp, decryptedvaluetemplen); + atclient_atnotification_set_decrypted_value(notification, decryptedvaluetemp); ret = 0; goto exit; diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 7c271fb6..e11f04e9 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -186,7 +186,7 @@ static int send_notification(atclient *atclient) { goto exit; } - if((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + if((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index 0ed0e391..ec038698 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -174,7 +174,7 @@ static int test_1_notify(atclient *atclient, char *notification_id) { goto exit; } - if ((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + if ((ret = atclient_notify_params_set_value(¶ms, ATKEY_VALUE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); goto exit; } @@ -229,7 +229,7 @@ static int test_2_notify_long_text(atclient *atclient, char *notification_id) { goto exit; } - if((ret = atclient_notify_params_set_value(¶ms, VERY_LONG_TEXT, strlen(VERY_LONG_TEXT))) != 0) { + if((ret = atclient_notify_params_set_value(¶ms, VERY_LONG_TEXT)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value: %d\n", ret); goto exit; } From 197e6c36fb9acdbb8148d7fcba4adf87d7e994a5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 12:02:06 -0400 Subject: [PATCH 115/193] fix: --- packages/atclient/src/metadata.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 955125bf..18853bbf 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -565,7 +565,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *is_encrypted = cJSON_GetObjectItem(json, "is_encrypted"); + cJSON *is_encrypted = cJSON_GetObjectItem(json, "isEncrypted"); if (is_encrypted != NULL) { if (is_encrypted->type != cJSON_NULL) { set_is_encrypted(metadata, is_encrypted->valueint); From 7c7e23725cfaf134974376841230648239bf9db6 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 12:04:48 -0400 Subject: [PATCH 116/193] fix: --- packages/atclient/src/metadata.c | 6 +++--- packages/atclient/tests/test_atkey_metadata.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 18853bbf..48b76735 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -552,7 +552,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c if (ccd->type != cJSON_NULL) { set_ccd(metadata, ccd->valueint); } else { - set_ccd(metadata, 0); + set_ccd(metadata, false); } } @@ -561,7 +561,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c if (is_binary->type != cJSON_NULL) { set_is_binary(metadata, is_binary->valueint); } else { - set_is_binary(metadata, 0); + set_is_binary(metadata, false); } } @@ -570,7 +570,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c if (is_encrypted->type != cJSON_NULL) { set_is_encrypted(metadata, is_encrypted->valueint); } else { - set_is_encrypted(metadata, 0); + set_is_encrypted(metadata, false); } } diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index d4e61412..d69eb7ef 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -148,7 +148,7 @@ static int test_atkey_metadata_from_jsonstr() { if (!atclient_atkey_metadata_is_is_binary_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isbinary_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_is_binary_initialized failed\n"); goto exit; } @@ -160,7 +160,7 @@ static int test_atkey_metadata_from_jsonstr() { if (!atclient_atkey_metadata_is_is_encrypted_initialized(&metadata)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_isencrypted_initialized failed\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_is_is_encrypted_initialized failed\n"); goto exit; } From 36ffa15c7bd9921730b29e234d5929604712564d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 12:06:07 -0400 Subject: [PATCH 117/193] fix: --- packages/atclient/tests/test_atkey_metadata.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index d69eb7ef..69feff5d 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -32,7 +32,7 @@ \"version\":0, \ \"ttl\":86400, \ \"isBinary\":false, \ - \"is_encrypted\":false \ + \"isEncrypted\":false \ }" static int test_atkey_metadata_from_jsonstr() { From 2a267b93847bf6aad09cff9e3c19523f7c29da67 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 12:07:38 -0400 Subject: [PATCH 118/193] fix: --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/notify.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 9c166bd9..503d506b 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_atkey: %d\n", ret); } - if((ret = atclient_notify_params_set_value(¶ms, line, strlen(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); } diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index ca6b05f4..08fdaec9 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) { goto exit; } - if((ret = atclient_notify_params_set_value(¬ify_params, ATKEY_VALUE, strlen(ATKEY_VALUE))) != 0) { + if((ret = atclient_notify_params_set_value(¬ify_params, ATKEY_VALUE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value\n"); goto exit; } From 206b83f17f67e8e2be167e2dc4008c87e557e3d1 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 12:11:18 -0400 Subject: [PATCH 119/193] fix: --- packages/atclient/src/metadata.c | 105 +++++++++++++++++- packages/atclient/tests/test_atkey_metadata.c | 2 +- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 48b76735..5c929cde 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -913,7 +913,7 @@ size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *me /* * 1. Validate arguments */ - if(metadata == NULL) { + if (metadata == NULL) { return 0; } @@ -993,21 +993,45 @@ size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *me } size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadata) { + if (metadata == NULL) { + return 0; + } + if (!atclient_atkey_metadata_is_ttl_initialized(metadata)) { + return 0; + } return strlen(":ttl:") // :ttl: + long_strlen(metadata->ttl); } size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_ttb_initialized(metadata)) { + return 0; + } return strlen(":ttb:") // :ttb: + long_strlen(metadata->ttb); } size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_ttr_initialized(metadata)) { + return 0; + } return strlen(":ttr:") // :ttr: + long_strlen(metadata->ttr); } size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_ccd_initialized(metadata)) { + return 0; + } if (metadata->ccd) { return strlen(":ccd:true"); // :ccd:true } else { @@ -1017,6 +1041,12 @@ size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadat } size_t atclient_atkey_metadata_is_binary_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_is_binary_initialized(metadata)) { + return 0; + } if (metadata->is_binary) { return strlen(":isBinary:true"); } else { @@ -1025,6 +1055,12 @@ size_t atclient_atkey_metadata_is_binary_strlen(const atclient_atkey_metadata *m } size_t atclient_atkey_metadata_is_encrypted_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_is_encrypted_initialized(metadata)) { + return 0; + } if (metadata->is_encrypted) { return strlen(":isEncrypted:true"); } else { @@ -1033,47 +1069,112 @@ size_t atclient_atkey_metadata_is_encrypted_strlen(const atclient_atkey_metadata } size_t atclient_atkey_metadata_data_signature_strlen(const atclient_atkey_metadata *metadata) { - // :dataSignature: + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_data_signature_initialized(metadata)) { + return 0; + } return strlen(":dataSignature:") + strlen(metadata->data_signature); } size_t atclient_atkey_metadata_shared_key_status_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_shared_key_status_initialized(metadata)) { + return 0; + } return strlen(":sharedKeyStatus:") + strlen(metadata->shared_key_status); } size_t atclient_atkey_metadata_shared_key_enc_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_shared_key_enc_initialized(metadata)) { + return 0; + } return strlen(":sharedKeyEnc:") + strlen(metadata->shared_key_enc); } size_t atclient_atkey_metadata_pub_key_hash_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_pub_key_hash_initialized(metadata)) { + return 0; + } return strlen(":hash:") + strlen(metadata->pub_key_hash); } size_t atclient_atkey_metadata_pub_key_algo_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_pub_key_algo_initialized(metadata)) { + return 0; + } return strlen(":algo:") + strlen(metadata->pub_key_algo); } size_t atclient_atkey_metadata_encoding_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_encoding_initialized(metadata)) { + return 0; + } return strlen(":encoding:") + strlen(metadata->encoding); } size_t atclient_atkey_metadata_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_enc_key_name_initialized(metadata)) { + return 0; + } return strlen(":encKeyName:") + strlen(metadata->enc_key_name); } size_t atclient_atkey_metadata_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_enc_algo_initialized(metadata)) { + return 0; + } return strlen(":encAlgo:") + strlen(metadata->enc_algo); } size_t atclient_atkey_metadata_iv_nonce_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_iv_nonce_initialized(metadata)) { + return 0; + } return strlen(":ivNonce:") + strlen(metadata->iv_nonce); } size_t atclient_atkey_metadata_ske_enc_key_name_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_ske_enc_key_name_initialized(metadata)) { + return 0; + } return strlen(":skeEncKeyName:") + strlen(metadata->ske_enc_key_name); } size_t atclient_atkey_metadata_ske_enc_algo_strlen(const atclient_atkey_metadata *metadata) { + if(metadata == NULL) { + return 0; + } + if(!atclient_atkey_metadata_is_ske_enc_algo_initialized(metadata)) { + return 0; + } return strlen(":skeEncAlgo:") + strlen(metadata->ske_enc_algo); } diff --git a/packages/atclient/tests/test_atkey_metadata.c b/packages/atclient/tests/test_atkey_metadata.c index 69feff5d..1f6cd64e 100644 --- a/packages/atclient/tests/test_atkey_metadata.c +++ b/packages/atclient/tests/test_atkey_metadata.c @@ -266,7 +266,7 @@ exit: { static int test_atkey_metadata_to_protocolstr() { int ret = 1; - const char *expected = ":ttr:-1:isBinary:true:is_encrypted:true:iv_nonce:abcdefghijk"; + const char *expected = ":ttr:-1:isBinary:true:isEncrypted:true:ivNonce:abcdefghijk"; const size_t expectedlen = strlen(expected); atclient_atkey_metadata metadata; From 2c0fde7bebd9748375c86cff109ba04c2496f2e5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 16:57:20 -0400 Subject: [PATCH 120/193] feat: more error handling in aesctr --- packages/atchops/src/aesctr.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c index 44066429..34b08eba 100644 --- a/packages/atchops/src/aesctr.c +++ b/packages/atchops/src/aesctr.c @@ -102,7 +102,7 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); goto exit; } - memset(plaintextpadded, 0, plaintextpaddedsize); + memset(plaintextpadded, 0, sizeof(unsigned char) * plaintextpaddedsize); size_t plaintextpaddedlen = 0; /* @@ -122,13 +122,18 @@ int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size // IBM PKCS Padding method states that there is always at least 1 padded value: // https://www.ibm.com/docs/en/zos/2.4.0?topic=rules-pkcs-padding-method the value of the padded byte is always the // number of padded bytes to expect, padval == num_padded_bytes - unsigned char padval = *(plaintextpadded + (plaintextpaddedlen - 1)); + unsigned char padval = plaintextpadded[plaintextpaddedlen - 1]; + + if(padval < 1 || padval > 16) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid padval: %d\n", padval); + goto exit; + } *plaintextlen = plaintextpaddedlen - padval; memcpy(plaintext, plaintextpadded, *plaintextlen); goto exit; - exit: { free(stream_block); free(plaintextpadded); From 524785de07afcc1317bcee1acea673eb05196e8e Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 16:57:52 -0400 Subject: [PATCH 121/193] fix: --- .../atclient/src/atclient_get_sharedkey.c | 38 ++++++++++--------- packages/atclient/src/metadata.c | 10 ++--- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index c01828be..b47c0212 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -85,8 +85,9 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at char *client_atsign_with_at = NULL; char *recipient_atsign_with_at = NULL; - unsigned char shared_encryption_key_to_use[ATCHOPS_AES_256 / 8]; - memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * ATCHOPS_AES_256 / 8); + const size_t shared_encryption_key_to_use_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key_to_use[shared_encryption_key_to_use_size]; + memset(shared_encryption_key_to_use, 0, sizeof(unsigned char) * shared_encryption_key_to_use_size); char *atkey_str = NULL; char *llookup_cmd = NULL; @@ -98,8 +99,8 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[iv_size]; - char *value_raw_encrypted = NULL; - char *value_raw = NULL; + unsigned char *value_raw_encrypted = NULL; + unsigned char *value_raw = NULL; cJSON *root = NULL; @@ -170,8 +171,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at char *response_without_data = response + 5; - root = cJSON_Parse(response_without_data); - if (root == NULL) { + if ((root = cJSON_Parse(response_without_data)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); goto exit; @@ -184,7 +184,11 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at goto exit; } - metadata_str = cJSON_Print(metadata); + if((metadata_str = cJSON_Print(metadata)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print: %d\n", ret); + goto exit; + } if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); @@ -203,12 +207,12 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at */ if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, - ATCHOPS_IV_BUFFER_SIZE, NULL)) != 0) { + iv_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } } else { - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(iv, 0, sizeof(unsigned char) * iv_size); } /* @@ -218,32 +222,32 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); const size_t value_raw_encrypted_size = atchops_base64_decoded_size(value_raw_encrypted_base64_len); - if ((value_raw_encrypted = malloc(sizeof(char) * value_raw_encrypted_size)) == NULL) { + if ((value_raw_encrypted = malloc(sizeof(unsigned char) * value_raw_encrypted_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw_encrypted\n"); goto exit; } - - memset(value_raw_encrypted, 0, sizeof(char) * value_raw_encrypted_size); + memset(value_raw_encrypted, 0, sizeof(unsigned char) * value_raw_encrypted_size); size_t value_raw_encrypted_len = 0; if ((ret = atchops_base64_decode(value_raw_encrypted_base64, value_raw_encrypted_base64_len, - (unsigned char *)value_raw_encrypted, value_raw_encrypted_size, + value_raw_encrypted, value_raw_encrypted_size, &value_raw_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } const size_t value_raw_size = atchops_aesctr_plaintext_size(value_raw_encrypted_len); - if ((value_raw = malloc(sizeof(char) * value_raw_size)) == NULL) { + if ((value_raw = malloc(sizeof(unsigned char) * value_raw_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); goto exit; } - memset(value_raw, 0, sizeof(char) * value_raw_size); + memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; + if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, - (unsigned char *)value_raw_encrypted, value_raw_encrypted_len, - (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { + value_raw_encrypted, value_raw_encrypted_len, + value_raw, value_raw_size, &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 5c929cde..b8765633 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -664,7 +664,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *enc_key_name = cJSON_GetObjectItem(json, "enc_key_name"); + cJSON *enc_key_name = cJSON_GetObjectItem(json, "encKeyName"); if (enc_key_name != NULL) { if (enc_key_name->type != cJSON_NULL) { if ((ret = set_enc_key_name(metadata, enc_key_name->valuestring)) != 0) { @@ -679,7 +679,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *enc_algo = cJSON_GetObjectItem(json, "enc_algo"); + cJSON *enc_algo = cJSON_GetObjectItem(json, "encAlgo"); if (enc_algo != NULL) { if (enc_algo->type != cJSON_NULL) { if ((ret = set_enc_algo(metadata, enc_algo->valuestring)) != 0) { @@ -694,7 +694,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *iv_nonce = cJSON_GetObjectItem(json, "iv_nonce"); + cJSON *iv_nonce = cJSON_GetObjectItem(json, "ivNonce"); if (iv_nonce != NULL) { if (iv_nonce->type != cJSON_NULL) { if ((ret = set_iv_nonce(metadata, iv_nonce->valuestring)) != 0) { @@ -709,7 +709,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *ske_enc_key_name = cJSON_GetObjectItem(json, "ske_enc_key_name"); + cJSON *ske_enc_key_name = cJSON_GetObjectItem(json, "skeEncKeyName"); if (ske_enc_key_name != NULL) { if (ske_enc_key_name->type != cJSON_NULL) { if ((ret = set_ske_enc_key_name(metadata, ske_enc_key_name->valuestring)) != 0) { @@ -724,7 +724,7 @@ int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, c } } - cJSON *ske_enc_algo = cJSON_GetObjectItem(json, "ske_enc_algo"); + cJSON *ske_enc_algo = cJSON_GetObjectItem(json, "skeEncAlgo"); if (ske_enc_algo != NULL) { if (ske_enc_algo->type != cJSON_NULL) { if ((ret = set_ske_enc_algo(metadata, ske_enc_algo->valuestring)) != 0) { From 4854f97fe761d087cbb62dc3a5fe6da1d7ee1ace Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 17:03:36 -0400 Subject: [PATCH 122/193] refactor: atclient_stringutils_long_strlen --- packages/atclient/include/atclient/notify_params.h | 2 +- packages/atclient/include/atclient/stringutils.h | 2 +- packages/atclient/src/metadata.c | 12 ++++++------ packages/atclient/src/stringutils.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/atclient/include/atclient/notify_params.h b/packages/atclient/include/atclient/notify_params.h index 2af30c1c..90a10a64 100644 --- a/packages/atclient/include/atclient/notify_params.h +++ b/packages/atclient/include/atclient/notify_params.h @@ -137,7 +137,7 @@ int atclient_notify_params_set_strategy(atclient_notify_params *params, const en int atclient_notify_params_set_latest_n(atclient_notify_params *params, const int latest_n); int atclient_notify_params_set_notifier(atclient_notify_params *params, const char *notifier); int atclient_notify_params_set_notification_expiry(atclient_notify_params *params, const unsigned long notification_expiry); -int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, const unsigned char *shared_encryption_key, const size_t shared_encryption_key_len); +int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, const unsigned char *shared_encryption_key); void atclient_notify_params_unset_id(atclient_notify_params *params); void atclient_notify_params_unset_atkey(atclient_notify_params *params); diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/stringutils.h index 54798426..7aab2773 100644 --- a/packages/atclient/include/atclient/stringutils.h +++ b/packages/atclient/include/atclient/stringutils.h @@ -61,6 +61,6 @@ int atclient_stringutils_atsign_without_at(const char *original_atsign, char **o * @param n the long to check the length of * @return int the string length */ -int long_strlen(long n); +int atclient_stringutils_long_strlen(long n); #endif diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index b8765633..715d40a0 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1000,7 +1000,7 @@ size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttl:") // :ttl: - + long_strlen(metadata->ttl); + + atclient_stringutils_long_strlen(metadata->ttl); } size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { @@ -1011,7 +1011,7 @@ size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttb:") // :ttb: - + long_strlen(metadata->ttb); + + atclient_stringutils_long_strlen(metadata->ttb); } size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { @@ -1022,7 +1022,7 @@ size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttr:") // :ttr: - + long_strlen(metadata->ttr); + + atclient_stringutils_long_strlen(metadata->ttr); } size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { @@ -1211,17 +1211,17 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttl:%ld", metadata->ttl); - pos += 5 + long_strlen(metadata->ttl); + pos += 5 + atclient_stringutils_long_strlen(metadata->ttl); } if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttb:%ld", metadata->ttb); - pos += 5 + long_strlen(metadata->ttb); + pos += 5 + atclient_stringutils_long_strlen(metadata->ttb); } if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttr:%ld", metadata->ttr); - pos += 5 + long_strlen(metadata->ttr); + pos += 5 + atclient_stringutils_long_strlen(metadata->ttr); } if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/stringutils.c index 6471a989..88b3648f 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/stringutils.c @@ -130,7 +130,7 @@ int atclient_stringutils_atsign_without_at(const char *original_atsign, char **o exit: { return ret; } } -int long_strlen(long n) { +int atclient_stringutils_long_strlen(long n) { // could use log10 for this, but it's probably slower... size_t len = 0; From 8871e1aad6cef370e940251614e8bbd9e2114fae Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 23 Jul 2024 17:03:41 -0400 Subject: [PATCH 123/193] refactor: set shared_encryption_key --- packages/atclient/src/notify.c | 4 ++-- packages/atclient/src/notify_params.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 72e393e5..0933fd17 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -223,7 +223,7 @@ static size_t calculate_cmd_size(const atclient_notify_params *params, const siz } if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { - cmdsize += strlen(":ttln:") + long_strlen(params->notification_expiry); // :$ttln + cmdsize += strlen(":ttln:") + atclient_stringutils_long_strlen(params->notification_expiry); // :$ttln } if (atclient_notify_params_is_atkey_initialized(params)) { @@ -313,7 +313,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { snprintf(cmd + off, cmdsize - off, ":ttln:%lu", params->notification_expiry); - off += strlen(":ttln:") + long_strlen(params->notification_expiry); + off += strlen(":ttln:") + atclient_stringutils_long_strlen(params->notification_expiry); } if ((res = atclient_atkey_metadata_to_protocol_str(&(params->atkey->metadata), &metadata_protocol_str)) != 0) { diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c index 20ffb4eb..c21915a3 100644 --- a/packages/atclient/src/notify_params.c +++ b/packages/atclient/src/notify_params.c @@ -1,4 +1,5 @@ #include "atclient/notify_params.h" +#include "atchops/aes.h" #include "atclient/constants.h" #include #include @@ -134,7 +135,8 @@ void atclient_notify_params_set_operation_initialized(atclient_notify_params *pa if (initialized) { params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] |= ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } else { - params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] &= ~ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; + params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] &= + ~ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } } @@ -345,23 +347,22 @@ int atclient_notify_params_set_notification_expiry(atclient_notify_params *param } int atclient_notify_params_set_shared_encryption_key(atclient_notify_params *params, - const unsigned char *shared_encryption_key, - const size_t shared_encryption_key_len) { + const unsigned char *shared_encryption_key) { int ret = 1; if (atclient_notify_params_is_shared_encryption_key_initialized(params)) { atclient_notify_params_unset_shared_encryption_key(params); } - if ((params->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_len)) == + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + + if ((params->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for shared_encryption_key\n"); goto exit; } - - memcpy(params->shared_encryption_key, shared_encryption_key, shared_encryption_key_len); - params->shared_encryption_key[shared_encryption_key_len] = '\0'; + memcpy(params->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); atclient_notify_params_set_shared_encryption_key_initialized(params, true); From faf2e98d4cf132c02b1784d25e3473dce75aa73d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 24 Jul 2024 10:44:18 -0400 Subject: [PATCH 124/193] chore: atchops refactors --- examples/desktop/at_talk/src/main.c | 2 +- packages/atchops/CMakeLists.txt | 4 +- packages/atchops/include/atchops/aes.h | 6 +- .../include/atchops/{aesctr.h => aes_ctr.h} | 42 +- packages/atchops/include/atchops/rsa.h | 58 +- packages/atchops/include/atchops/rsa_key.h | 113 ++ packages/atchops/include/atchops/rsakey.h | 106 -- packages/atchops/include/atchops/sha.h | 6 +- packages/atchops/include/atchops/uuid.h | 6 +- packages/atchops/src/aes.c | 36 +- packages/atchops/src/aes_ctr.c | 264 ++++ packages/atchops/src/aesctr.c | 153 --- packages/atchops/src/base64.c | 103 +- packages/atchops/src/iv.c | 21 +- packages/atchops/src/rsa.c | 268 +++- packages/atchops/src/rsa_key.c | 1124 +++++++++++++++++ packages/atchops/src/rsakey.c | 592 --------- packages/atchops/src/sha.c | 75 +- packages/atchops/src/uuid.c | 28 +- packages/atchops/tests/test_aes_generate.c | 6 +- packages/atchops/tests/test_aesctr.c | 10 +- packages/atchops/tests/test_aesctr_decrypt.c | 10 +- packages/atchops/tests/test_rsadecrypt.c | 10 +- packages/atchops/tests/test_rsaencrypt.c | 10 +- .../atchops/tests/test_rsaprivatepopulate.c | 10 +- .../atchops/tests/test_rsapublicpopulate.c | 10 +- packages/atchops/tests/test_rsasign.c | 10 +- packages/atchops/tests/test_rsaverify.c | 16 +- packages/atclient/include/atclient/atkeys.h | 8 +- packages/atclient/src/atclient.c | 2 +- packages/atclient/src/atclient_get_selfkey.c | 6 +- .../atclient/src/atclient_get_sharedkey.c | 16 +- packages/atclient/src/atclient_put.c | 12 +- packages/atclient/src/atkeys.c | 68 +- .../atclient/src/encryption_key_helpers.c | 36 +- packages/atclient/src/monitor.c | 4 +- packages/atclient/src/notify.c | 6 +- .../tests/test_atclient_monitor.c | 2 +- .../tests/test_atclient_notify.c | 2 +- 39 files changed, 2105 insertions(+), 1156 deletions(-) rename packages/atchops/include/atchops/{aesctr.h => aes_ctr.h} (54%) create mode 100644 packages/atchops/include/atchops/rsa_key.h delete mode 100644 packages/atchops/include/atchops/rsakey.h create mode 100644 packages/atchops/src/aes_ctr.c delete mode 100644 packages/atchops/src/aesctr.c create mode 100644 packages/atchops/src/rsa_key.c delete mode 100644 packages/atchops/src/rsakey.c diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 503d506b..8b7429be 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/packages/atchops/CMakeLists.txt b/packages/atchops/CMakeLists.txt index 22b31b1d..9b135eb0 100644 --- a/packages/atchops/CMakeLists.txt +++ b/packages/atchops/CMakeLists.txt @@ -5,13 +5,13 @@ option(ATCHOPS_BUILD_TESTS "Build tests for atchops" OFF) set(ATCHOPS_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include) set( ATCHOPS_SOURCES + ${CMAKE_CURRENT_LIST_DIR}/src/aes_ctr.c ${CMAKE_CURRENT_LIST_DIR}/src/aes.c - ${CMAKE_CURRENT_LIST_DIR}/src/aesctr.c ${CMAKE_CURRENT_LIST_DIR}/src/base64.c ${CMAKE_CURRENT_LIST_DIR}/src/constants.c ${CMAKE_CURRENT_LIST_DIR}/src/iv.c + ${CMAKE_CURRENT_LIST_DIR}/src/rsa_key.c ${CMAKE_CURRENT_LIST_DIR}/src/rsa.c - ${CMAKE_CURRENT_LIST_DIR}/src/rsakey.c ${CMAKE_CURRENT_LIST_DIR}/src/sha.c ${CMAKE_CURRENT_LIST_DIR}/src/uuid.c ) diff --git a/packages/atchops/include/atchops/aes.h b/packages/atchops/include/atchops/aes.h index 6ff05a4b..846bc070 100644 --- a/packages/atchops/include/atchops/aes.h +++ b/packages/atchops/include/atchops/aes.h @@ -7,15 +7,15 @@ enum atchops_aes_size { ATCHOPS_AES_NONE = 0, ATCHOPS_AES_128 = 128, // not tested ATCHOPS_AES_256 = 256, -}; +} ; /** * @brief Generate an AES key of size keylen bits * * @param key key buffer of size (keylen/8) bytes - * @param keybits key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) + * @param key_bits key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) * @return int 0 on success */ -int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size keybits); +int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size key_bits); #endif diff --git a/packages/atchops/include/atchops/aesctr.h b/packages/atchops/include/atchops/aes_ctr.h similarity index 54% rename from packages/atchops/include/atchops/aesctr.h rename to packages/atchops/include/atchops/aes_ctr.h index 5bfb3854..22d62a67 100644 --- a/packages/atchops/include/atchops/aesctr.h +++ b/packages/atchops/include/atchops/aes_ctr.h @@ -1,5 +1,5 @@ -#ifndef ATCHOPS_AESCTR_H -#define ATCHOPS_AESCTR_H +#ifndef ATCHOPS_AES_CTR_H +#define ATCHOPS_AES_CTR_H #include "atchops/aes.h" #include @@ -7,53 +7,53 @@ /** * @brief AES CTR encrypt plaintext to ciphertextbase64 (base64 encoded string) * - * @param key the AES key in raw bytes (if key is 256 bits (32 bytes) then keybits should be ATCHOPS_AES_256) - * @param keybits the AES key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) + * @param key the AES key in raw bytes (if key is 256 bits (32 bytes) then key_bits should be ATCHOPS_AES_256) + * @param key_bits the AES key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) * @param iv the initialization vector (always 16 bytes long) * @param plaintext the plaintext to encrypt - * @param plaintextlen the length of the plaintext string + * @param plaintext_len the length of the plaintext string * @param ciphertext the ciphertext buffer to write to - * @param ciphertextsize the memory allocated length of the ciphertext buffer - * @param ciphertextlen the written length of the ciphertext buffer + * @param ciphertext_size the memory allocated length of the ciphertext buffer + * @param ciphertext_len the written length of the ciphertext buffer * @return int 0 on success */ -int atchops_aesctr_encrypt(const unsigned char *key, const enum atchops_aes_size keybits, +int atchops_aes_ctr_encrypt(const unsigned char *key, const enum atchops_aes_size key_bits, unsigned char *iv, // always 16 bytes long - const unsigned char *plaintext, const size_t plaintextlen, unsigned char *ciphertext, - const size_t ciphertextsize, size_t *ciphertextlen); + const unsigned char *plaintext, const size_t plaintext_len, unsigned char *ciphertext, + const size_t ciphertext_size, size_t *ciphertext_len); /** * @brief AES CTR decrypt ciphertextbase64 to plaintext * - * @param key the AES key in raw bytes (if key is 256 bits (32 bytes) then keybits should be ATCHOPS_AES_256) - * @param keybits the AES key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) + * @param key the AES key in raw bytes (if key is 256 bits (32 bytes) then key_bits should be ATCHOPS_AES_256) + * @param key_bits the AES key length in bits (e.g. AES-256 = 256 => ATCHOPS_AES_256) * @param iv the initialization vector (always 16 bytes long) * @param ciphertext the ciphertext in raw bytes - * @param ciphertextlen the length of the ciphertext bytes buffer + * @param ciphertext_len the length of the ciphertext bytes buffer * @param plaintext the plaintext buffer to write to * @param plaintextsize the memory allocated length of the plaintext buffer - * @param plaintextlen the written length of the plaintext buffer + * @param plaintext_len the written length of the plaintext buffer * @return int 0 on success */ -int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size keybits, +int atchops_aes_ctr_decrypt(const unsigned char *key, const enum atchops_aes_size key_bits, unsigned char *iv, // always 16 bytes long - const unsigned char *ciphertext, const size_t ciphertextlen, unsigned char *plaintext, - const size_t plaintextsize, size_t *plaintextlen); + const unsigned char *ciphertext, const size_t ciphertext_len, unsigned char *plaintext, + const size_t plaintextsize, size_t *plaintext_len); /** * @brief Used to calculate the length of the ciphertext buffer given the plaintext length. This is used when encrypting. * - * @param plaintextlen the length of the plaintext string + * @param plaintext_len the length of the plaintext string * @return a sufficient length of the ciphertext buffer */ -size_t atchops_aesctr_ciphertext_size(const size_t plaintextlen); +size_t atchops_aes_ctr_ciphertext_size(const size_t plaintext_len); /** * @brief Used to calculate the a sufficient buffer size of the plaintext buffer given the ciphertext length. This is used when decrypting. * - * @param ciphertextlen the length of the ciphertext string + * @param ciphertext_len the length of the ciphertext string * @return a sufficient length of the plaintext buffer */ -size_t atchops_aesctr_plaintext_size(const size_t ciphertextlen); +size_t atchops_aes_ctr_plaintext_size(const size_t ciphertext_len); #endif diff --git a/packages/atchops/include/atchops/rsa.h b/packages/atchops/include/atchops/rsa.h index d92808a2..e8e72296 100644 --- a/packages/atchops/include/atchops/rsa.h +++ b/packages/atchops/include/atchops/rsa.h @@ -2,79 +2,75 @@ #define ATCHOPS_RSA_H #include "atchops/constants.h" -#include "atchops/rsakey.h" +#include "atchops/rsa_key.h" #include /** * @brief Sign a hashed message with an RSA private key * - * @param privatekey the private key struct to use for signing, see atchops_rsakey_populate_privatekey - * @param mdtype the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256. The message will be hashed with this + * @param private_key the private key struct to use for signing, see atchops_rsa_key_populate_private_key + * @param md_type the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256. The message will be hashed with this * algorithm before signing * @param message the message to sign - * @param messagelen the length of the message, most people use strlen() to find this length + * @param message_len the length of the message, most people use strlen() to find this length * @param signature the signature buffer to populate, must be pre-allocated. Signature size will correspond to the * specified hashing algorithm (e.g., this function expects `signature` to be a buffer of 256 bytes allocated because a * RSA-2048 key is used, which corresponds to a 2048-bit signature) * @return int 0 on success */ -int atchops_rsa_sign(const atchops_rsakey_privatekey privatekey, const atchops_md_type mdtype, - const unsigned char *message, const size_t messagelen, unsigned char *signature); +int atchops_rsa_sign(const atchops_rsa_key_private_key *private_key, const atchops_md_type md_type, + const unsigned char *message, const size_t message_len, unsigned char *signature); /** * @brief Verify a signature with an RSA public key * - * @param publickey the public key to use for verification, see atchops_rsakey_populate_publickey - * @param mdtype the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256 + * @param public_key the public key to use for verification, see atchops_rsa_key_populate_public_key + * @param md_type the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256 * @param message the original message to hash, in bytes - * @param messagelen the length of the original message, most people use strlen() to find this length + * @param message_len the length of the original message, most people use strlen() to find this length * @param signature the signature to verify, expected to be the same length as the key size (e.g. 256 bytes for 2048 RSA * modulus) * @return int 0 on success */ -int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_md_type mdtype, - const unsigned char *message, const size_t messagelen, unsigned char *signature); +int atchops_rsa_verify(const atchops_rsa_key_public_key *public_key, const atchops_md_type md_type, + const unsigned char *message, const size_t message_len, unsigned char *signature); /** * @brief Encrypt bytes with an RSA public key * - * @param publickeystruct the public key struct to use for encryption, see atchops_rsakey_populate_publickey + * @param public_key the public key struct to use for encryption, see atchops_rsa_key_populate_public_key * @param plaintext the plaintext to encrypt, in bytes - * @param plaintextlen the length of the plaintext, most people use strlen() to find this length - * @param ciphertext the ciphertext buffer to populate - * @param ciphertextsize the length of the ciphertext buffer - * @param ciphertextlen the written length of the ciphertext buffer after encryption operation has completed, should be - * the same as the key size (e.g. 256 bytes for 2048 RSA modulus) + * @param plaintext_len the length of the plaintext, most people use strlen() to find this length + * @param ciphertext the ciphertext buffer to populate, assumed to be 256 bytes long for 2048 RSA modulus * @return int 0 on success */ -int atchops_rsa_encrypt(const atchops_rsakey_publickey publickey, const unsigned char *plaintext, - const size_t plaintextlen, unsigned char *ciphertext, const size_t ciphertextsize, - size_t *ciphertextlen); +int atchops_rsa_encrypt(const atchops_rsa_key_public_key *public_key, const unsigned char *plaintext, + const size_t plaintext_len, unsigned char *ciphertext); /** * @brief Decrypt bytes with an RSA private key * - * @param privatekey the private key struct to use for decryption, see atchops_rsakey_populate_privatekey + * @param private_key the private key struct to use for decryption, see atchops_rsa_key_populate_private_key * @param ciphertext the ciphertext to decrypt, in bytes - * @param ciphertextlen the length of the ciphertext, should be the same as the key size (e.g. 256 bytes for 2048 RSA + * @param ciphertext_len the length of the ciphertext, should be the same as the key size (e.g. 256 bytes for 2048 RSA * modulus) * @param plaintext the plaintext buffer to populate - * @param plaintextsize the size of the plaintext buffer (allocated size) - * @param plaintextlen the written length of the plaintext buffer after decryption operation has completed + * @param plaintext_size the size of the plaintext buffer (allocated size) + * @param plaintext_len the written length of the plaintext buffer after decryption operation has completed * @return int 0 on success */ -int atchops_rsa_decrypt(const atchops_rsakey_privatekey privatekey, const unsigned char *ciphertext, - const size_t ciphertextlen, unsigned char *plaintext, const size_t plaintextsize, - size_t *plaintextlen); +int atchops_rsa_decrypt(const atchops_rsa_key_private_key *private_key, const unsigned char *ciphertext, + const size_t ciphertext_len, unsigned char *plaintext, const size_t plaintext_size, + size_t *plaintext_len); /** * @brief generate an RSA keypair * - * @param publickey the public key struct to populate, should be initialized first - * @param privatekey the private key struct to populate, should be initialized first + * @param public_key the public key struct to populate, should be initialized first + * @param private_key the private key struct to populate, should be initialized first * @param keysize the size of the key to generate, e.g. 2048 */ -int atchops_rsa_generate(atchops_rsakey_publickey *publickey, atchops_rsakey_privatekey *privatekey, - const atchops_md_type mdtype); +int atchops_rsa_generate(atchops_rsa_key_public_key *public_key, atchops_rsa_key_private_key *private_key, + const atchops_md_type md_type); #endif diff --git a/packages/atchops/include/atchops/rsa_key.h b/packages/atchops/include/atchops/rsa_key.h new file mode 100644 index 00000000..4705af4f --- /dev/null +++ b/packages/atchops/include/atchops/rsa_key.h @@ -0,0 +1,113 @@ +#ifndef ATCHOPS_RSA_KEY_H +#define ATCHOPS_RSA_KEY_H + +#include +#include + +typedef struct atchops_rsa_key_param { + size_t len; // length of the number in bytes + unsigned char *value; // hex byte array of the number + bool _is_value_initialized : 1; // whether the value is allocated +} atchops_rsa_key_param; + +typedef struct atchops_rsa_key_public_key { + atchops_rsa_key_param n; // modulus + atchops_rsa_key_param e; // public exponent +} atchops_rsa_key_public_key; + +typedef struct atchops_rsa_key_private_key { + atchops_rsa_key_param n; // modulus + atchops_rsa_key_param e; // public exponent + atchops_rsa_key_param d; // private exponent + atchops_rsa_key_param p; // prime 1 + atchops_rsa_key_param q; // prime 2 +} atchops_rsa_key_private_key; + +void atchops_rsa_key_public_key_init(atchops_rsa_key_public_key *public_key); +void atchops_rsa_key_public_key_free(atchops_rsa_key_public_key *public_key); + +void atchops_rsa_key_private_key_init(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_free(atchops_rsa_key_private_key *private_key); + +/** + * @brief Deep clone an atchops_rsa_key_public_key + * + * @param src the src from which to copy from + * @param dst the new copy of key + */ +int atchops_rsa_key_public_key_clone(const atchops_rsa_key_public_key *src, atchops_rsa_key_public_key *dst); + +/** + * @brief Deep clone an atchops_rsa_key_private_key + * + * @param src the src from which to copy from + * @param dst the new copy of key + */ +int atchops_rsa_key_private_key_clone(const atchops_rsa_key_private_key *src, atchops_rsa_key_private_key *dst); + +/** + * @brief Populate a public key struct from a base64 string + * + * @param public_key_struct the public key struct to populate + * @param public_key_base64 a base64 string representing an RSA 2048 Public Key + * @param public_key_base64_len the length of the base64 string + * @return int 0 on success + */ +int atchops_rsa_key_populate_public_key(atchops_rsa_key_public_key *public_key_struct, const char *public_key_base64, + const size_t public_key_base64_len); + +/** + * @brief Populate a private key struct from a base64 string + * + * @param private_key_struct the private key struct to populate + * @param private_key_base64 the base64 string representing an RSA 2048 Private Key + * @param private_key_base64_len the length of the base64 string + * @return int 0 on success + */ +int atchops_rsa_key_populate_private_key(atchops_rsa_key_private_key *private_key_struct, const char *private_key_base64, + const size_t privatekeprivate_key_base64_lenybase64len); + +int atchops_rsa_key_public_key_set_ne(atchops_rsa_key_public_key *public_key, const unsigned char *n, const size_t n_len, + const unsigned char *e, const size_t e_len); + +bool atchops_rsa_key_public_key_is_n_initialized(atchops_rsa_key_public_key *public_key); +void atchops_rsa_key_public_key_set_n_initialized(atchops_rsa_key_public_key *public_key, const bool initialized); +int atchops_rsa_key_public_key_set_n(atchops_rsa_key_public_key *public_key, const unsigned char *n, const size_t n_len); +void atchops_rsa_key_public_key_unset_n(atchops_rsa_key_public_key *public_key); + +bool atchops_rsa_key_public_key_is_e_initialized(atchops_rsa_key_public_key *public_key); +void atchops_rsa_key_public_key_set_e_initialized(atchops_rsa_key_public_key *public_key, const bool initialized); +int atchops_rsa_key_public_key_set_e(atchops_rsa_key_public_key *public_key, const unsigned char *e, const size_t e_len); +void atchops_rsa_key_public_key_unset_e(atchops_rsa_key_public_key *public_key); + +int atchops_rsa_key_private_key_set_nedpq(atchops_rsa_key_private_key *private_key, const unsigned char *n, + const size_t n_len, const unsigned char *e, const size_t e_len, + const unsigned char *d, const size_t d_len, const unsigned char *p, + const size_t p_len, const unsigned char *q, const size_t q_len); + +bool atchops_rsa_key_private_key_is_n_initialized(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_set_n_initialized(atchops_rsa_key_private_key *private_key, const bool initialized); +int atchops_rsa_key_private_key_set_n(atchops_rsa_key_private_key *private_key, const unsigned char *n, const size_t n_len); +void atchops_rsa_key_private_key_unset_n(atchops_rsa_key_private_key *private_key); + +bool atchops_rsa_key_private_key_is_e_initialized(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_set_e_initialized(atchops_rsa_key_private_key *private_key, const bool initialized); +int atchops_rsa_key_private_key_set_e(atchops_rsa_key_private_key *private_key, const unsigned char *e, const size_t e_len); +void atchops_rsa_key_private_key_unset_e(atchops_rsa_key_private_key *private_key); + +bool atchops_rsa_key_private_key_is_d_initialized(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_set_d_initialized(atchops_rsa_key_private_key *private_key, const bool initialized); +int atchops_rsa_key_private_key_set_d(atchops_rsa_key_private_key *private_key, const unsigned char *d, const size_t d_len); +void atchops_rsa_key_private_key_unset_d(atchops_rsa_key_private_key *private_key); + +bool atchops_rsa_key_private_key_is_p_initialized(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_set_p_initialized(atchops_rsa_key_private_key *private_key, const bool initialized); +int atchops_rsa_key_private_key_set_p(atchops_rsa_key_private_key *private_key, const unsigned char *p, const size_t p_len); +void atchops_rsa_key_private_key_unset_p(atchops_rsa_key_private_key *private_key); + +bool atchops_rsa_key_private_key_is_q_initialized(atchops_rsa_key_private_key *private_key); +void atchops_rsa_key_private_key_set_q_initialized(atchops_rsa_key_private_key *private_key, const bool initialized); +int atchops_rsa_key_private_key_set_q(atchops_rsa_key_private_key *private_key, const unsigned char *q, const size_t q_len); +void atchops_rsa_key_private_key_unset_q(atchops_rsa_key_private_key *private_key); + +#endif diff --git a/packages/atchops/include/atchops/rsakey.h b/packages/atchops/include/atchops/rsakey.h deleted file mode 100644 index f302e151..00000000 --- a/packages/atchops/include/atchops/rsakey.h +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef ATCHOPS_RSAKEY_H -#define ATCHOPS_RSAKEY_H - -#include -#include - -typedef struct atchops_rsakey_param { - size_t len; // length of the number in bytes - unsigned char *value; // hex byte array of the number - bool _is_value_initialized : 1; // whether the value is allocated -} atchops_rsakey_param; - -typedef struct atchops_rsakey_publickey { - atchops_rsakey_param n; // modulus - atchops_rsakey_param e; // public exponent -} atchops_rsakey_publickey; - -typedef struct atchops_rsakey_privatekey { - atchops_rsakey_param n; // modulus - atchops_rsakey_param e; // public exponent - atchops_rsakey_param d; // private exponent - atchops_rsakey_param p; // prime 1 - atchops_rsakey_param q; // prime 2 -} atchops_rsakey_privatekey; - -void atchops_rsakey_publickey_init(atchops_rsakey_publickey *publickey); -void atchops_rsakey_publickey_free(atchops_rsakey_publickey *publickey); - -void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey); -void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey); - -/** - * @brief Deep clone an atchops_rsakey_publickey - * - * @param src the src from which to copy from - * @param dst the new copy of key - */ -int atchops_rsakey_publickey_clone(const atchops_rsakey_publickey *src, atchops_rsakey_publickey *dst); - -/** - * @brief Deep clone an atchops_rsakey_privatekey - * - * @param src the src from which to copy from - * @param dst the new copy of key - */ -int atchops_rsakey_privatekey_clone(const atchops_rsakey_privatekey *src, atchops_rsakey_privatekey *dst); - -/** - * @brief Populate a public key struct from a base64 string - * - * @param publickeystruct the public key struct to populate - * @param publickeybase64 a base64 string representing an RSA 2048 Public Key - * @param publickeybase64len the length of the base64 string - * @return int 0 on success - */ -int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickeystruct, const char *publickeybase64, - const size_t publickeybase64len); - -/** - * @brief Populate a private key struct from a base64 string - * - * @param privatekeystruct the private key struct to populate - * @param privatekeybase64 the base64 string representing an RSA 2048 Private Key - * @param privatekeybase64len the length of the base64 string - * @return int 0 on success - */ -int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekeystruct, const char *privatekeybase64, - const size_t privatekeybase64len); - -int atchops_rsakey_publickey_set_ne(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen, - const unsigned char *e, const size_t elen); - -bool atchops_rsakey_publickey_is_n_initialized(atchops_rsakey_publickey *publickey); -int atchops_rsakey_publickey_set_n(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen); -void atchops_rsakey_publickey_unset_n(atchops_rsakey_publickey *publickey); - -bool atchops_rsakey_publickey_is_e_initialized(atchops_rsakey_publickey *publickey); -int atchops_rsakey_publickey_set_e(atchops_rsakey_publickey *publickey, const unsigned char *e, const size_t elen); -void atchops_rsakey_publickey_unset_e(atchops_rsakey_publickey *publickey); - -int atchops_rsakey_privatekey_set_nedpq(atchops_rsakey_privatekey *privatekey, const unsigned char *n, - const size_t nlen, const unsigned char *e, const size_t elen, - const unsigned char *d, const size_t dlen, const unsigned char *p, - const size_t plen, const unsigned char *q, const size_t qlen); - -bool atchops_rsakey_privatekey_is_n_initialized(atchops_rsakey_privatekey *privatekey); -int atchops_rsakey_privatekey_set_n(atchops_rsakey_privatekey *privatekey, const unsigned char *n, const size_t nlen); -void atchops_rsakey_privatekey_unset_n(atchops_rsakey_privatekey *privatekey); - -bool atchops_rsakey_privatekey_is_e_initialized(atchops_rsakey_privatekey *privatekey); -int atchops_rsakey_privatekey_set_e(atchops_rsakey_privatekey *privatekey, const unsigned char *e, const size_t elen); -void atchops_rsakey_privatekey_unset_e(atchops_rsakey_privatekey *privatekey); - -bool atchops_rsakey_privatekey_is_d_initialized(atchops_rsakey_privatekey *privatekey); -int atchops_rsakey_privatekey_set_d(atchops_rsakey_privatekey *privatekey, const unsigned char *d, const size_t dlen); -void atchops_rsakey_privatekey_unset_d(atchops_rsakey_privatekey *privatekey); - -bool atchops_rsakey_privatekey_is_p_initialized(atchops_rsakey_privatekey *privatekey); -int atchops_rsakey_privatekey_set_p(atchops_rsakey_privatekey *privatekey, const unsigned char *p, const size_t plen); -void atchops_rsakey_privatekey_unset_p(atchops_rsakey_privatekey *privatekey); - -bool atchops_rsakey_privatekey_is_q_initialized(atchops_rsakey_privatekey *privatekey); -int atchops_rsakey_privatekey_set_q(atchops_rsakey_privatekey *privatekey, const unsigned char *q, const size_t qlen); -void atchops_rsakey_privatekey_unset_q(atchops_rsakey_privatekey *privatekey); - -#endif diff --git a/packages/atchops/include/atchops/sha.h b/packages/atchops/include/atchops/sha.h index 4ca666e5..8a0cc0bd 100644 --- a/packages/atchops/include/atchops/sha.h +++ b/packages/atchops/include/atchops/sha.h @@ -7,14 +7,14 @@ /** * @brief SHA Hash an input buffer * - * @param mdtype the type of hash to use (e.g. MBEDTLS_MD_SHA256) + * @param md_type the type of hash to use (e.g. MBEDTLS_MD_SHA256) * @param input the input to hash (in raw bytes) - * @param inputlen the length of the input buffer (most likely strlen(input)) + * @param input_len the length of the input buffer (most likely strlen(input)) * @param output (out) the output buffer to write the hash to. The length of this buffer should correspond to the hash * type (e.g. 32 bytes for SHA256 (256 bits = 32 bytes)) * @return int 0 on success */ -int atchops_sha_hash(const atchops_md_type mdtype, const unsigned char *input, const size_t inputlen, +int atchops_sha_hash(const atchops_md_type md_type, const unsigned char *input, const size_t input_len, unsigned char *output); #endif diff --git a/packages/atchops/include/atchops/uuid.h b/packages/atchops/include/atchops/uuid.h index 75d7423a..74734eaa 100644 --- a/packages/atchops/include/atchops/uuid.h +++ b/packages/atchops/include/atchops/uuid.h @@ -13,10 +13,10 @@ int atchops_uuid_init(void); /** * @brief Generate a UUID-v4 string * - * @param uuidstr the buffer to store the UUID string - * @param uuidstrlen the length of the buffer + * @param uuid_str the buffer to store the UUID string + * @param uuid_str_len the length of the buffer * @return int 0 on success, 1 on error */ -int atchops_uuid_generate(char *uuidstr, const size_t uuidstrlen); +int atchops_uuid_generate(char *uuid_str, const size_t uuid_str_len); #endif diff --git a/packages/atchops/src/aes.c b/packages/atchops/src/aes.c index 1f626ce9..9ec7148a 100644 --- a/packages/atchops/src/aes.c +++ b/packages/atchops/src/aes.c @@ -5,12 +5,34 @@ #include #include #include +#include + +#define TAG "aes" int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size keybits) { int ret = 1; - const char *pers = ATCHOPS_RNG_PERSONALIZATION; - const size_t keybytes = keybits / 8; + /* + * 1. Validate arguments + */ + if(key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL\n"); + return ret; + } + + if(keybits != ATCHOPS_AES_256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported keybits\n"); + return ret; + } + + + /* + * 2. Variables + */ + const char *personlization = ATCHOPS_RNG_PERSONALIZATION; + const size_t key_size = keybits / 8; // note: To use the AES generator, you need to have the modules enabled in the mbedtls/config.h files // (MBEDTLS_CTR_DRBG_C and MBEDTLS_ENTROPY_C), see How do I configure Mbed TLS. @@ -22,12 +44,18 @@ int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size key mbedtls_entropy_context entropy; mbedtls_entropy_init(&entropy); - if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (unsigned char *)pers, strlen(pers))) != + /* + * 3. Seed the random number generator + */ + if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (unsigned char *)personlization, strlen(personlization))) != 0) { goto exit; } - if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, key, keybytes)) != 0) { + /* + * 4. Generate the key + */ + if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, key, key_size)) != 0) { goto exit; } diff --git a/packages/atchops/src/aes_ctr.c b/packages/atchops/src/aes_ctr.c new file mode 100644 index 00000000..3b458e81 --- /dev/null +++ b/packages/atchops/src/aes_ctr.c @@ -0,0 +1,264 @@ +#include "atchops/aes_ctr.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define TAG "aes_ctr" + +int atchops_aes_ctr_encrypt(const unsigned char *key, const enum atchops_aes_size key_bits, unsigned char *iv, + const unsigned char *plaintext, // plaintext to encrypt + const size_t plaintext_len, + unsigned char *ciphertext, // allocated buffer to populate + const size_t ciphertext_size, // number of total bytes allocated in the buffer + size_t *ciphertext_len // written actual length in the buffer +) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL\n"); + return ret; + } + + if (key_bits != ATCHOPS_AES_256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported keybits\n"); + return ret; + } + + if (iv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "iv is NULL\n"); + return ret; + } + + if (plaintext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext is NULL\n"); + return ret; + } + + if (plaintext_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext_len is less than or equal to 0\n"); + return ret; + } + + if (ciphertext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext is NULL\n"); + return ret; + } + + if (ciphertext_len == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext_len is NULL\n"); + return ret; + } + + if (ciphertext_size < plaintext_len) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext_size is less than plaintext_len\n"); + return ret; + } + + /* + * 2. Variables + */ + mbedtls_aes_context aes; + mbedtls_aes_init(&aes); + + unsigned char *plaintext_padded = NULL; // will contain the plaintext with padded trialing bytes + unsigned char stream_block[16]; + + /* + * 3. Pad plaintext + */ + size_t plaintext_padded_len; // the length of the plain text + padding (no null terminator) + + const int num_pad_bytes_to_add = 16 - (plaintext_len % 16); + const unsigned char pad_val = num_pad_bytes_to_add; + + plaintext_padded_len = plaintext_len + num_pad_bytes_to_add; + + if ((plaintext_padded = malloc(sizeof(unsigned char) * (plaintext_padded_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintext_padded\n"); + goto exit; + } + memcpy(plaintext_padded, plaintext, plaintext_len); + memset(plaintext_padded + plaintext_len, pad_val, num_pad_bytes_to_add); + plaintext_padded[plaintext_padded_len] = '\0'; + + /* + * 3. Prepare AES context + */ + if ((ret = mbedtls_aes_setkey_enc(&aes, key, key_bits)) != 0) { + goto exit; + } + + /* + * 4. Encrypt + */ + size_t nc_off = 0; + memset(stream_block, 0, sizeof(unsigned char) * 16); + memset(ciphertext, 0, sizeof(unsigned char) * ciphertext_size); // clear the buffer + if ((ret = mbedtls_aes_crypt_ctr(&aes, plaintext_padded_len, &nc_off, iv, stream_block, plaintext_padded, + ciphertext)) != 0) { + goto exit; + } + + if (ciphertext_len != NULL) { + *ciphertext_len = plaintext_padded_len; + } + + ret = 0; + goto exit; +exit: { + free(plaintext_padded); + mbedtls_aes_free(&aes); + return ret; +} +} + +int atchops_aes_ctr_decrypt(const unsigned char *key, const enum atchops_aes_size key_bits, unsigned char *iv, + const unsigned char *ciphertext, const size_t ciphertext_len, unsigned char *plaintext, + const size_t plaintext_size, size_t *plaintext_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + + if (key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL\n"); + return ret; + } + + if (key_bits != ATCHOPS_AES_256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported keybits\n"); + return ret; + } + + if (iv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "iv is NULL\n"); + return ret; + } + + if (ciphertext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext is NULL\n"); + return ret; + } + + if (ciphertext_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext_len is less than or equal to 0\n"); + return ret; + } + + if (plaintext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext is NULL\n"); + return ret; + } + + if (plaintext_size <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext_size is less than or equal to 0\n"); + return ret; + } + + if (plaintext_size < ciphertext_len) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext_size is less than ciphertext_len\n"); + return ret; + } + + /* + * 2. Variables + */ + mbedtls_aes_context aes; + mbedtls_aes_init(&aes); + + unsigned char *plaintext_padded = NULL; + unsigned char stream_block[16]; + + /* + * 3. Prepare AES context + */ + if ((ret = mbedtls_aes_setkey_enc(&aes, key, key_bits)) != 0) { + goto exit; + } + + /* + * 4. Allocate buffers required for decryption + */ + size_t nc_off = 0; + memset(stream_block, 0, sizeof(unsigned char) * 16); + + const size_t plaintextpaddedsize = plaintext_size + 16; + if ((plaintext_padded = malloc(sizeof(unsigned char) * plaintextpaddedsize)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintext_padded\n"); + goto exit; + } + memset(plaintext_padded, 0, sizeof(unsigned char) * plaintextpaddedsize); + size_t plaintext_padded_len = 0; + + /* + * 3. Decrypt + */ + if ((ret = mbedtls_aes_crypt_ctr(&aes, ciphertext_len, &nc_off, iv, stream_block, ciphertext, plaintext_padded)) != + 0) { + goto exit; + } + + /* + * 4. Remove padding + */ + while (*(plaintext_padded + plaintext_padded_len++) != '\0') + ; + --plaintext_padded_len; // don't count the null terminator + + // IBM PKCS Padding method states that there is always at least 1 padded value: + // https://www.ibm.com/docs/en/zos/2.4.0?topic=rules-pkcs-padding-method the value of the padded byte is always the + // number of padded bytes to expect, pad_val == num_padded_bytes + unsigned char pad_val = plaintext_padded[plaintext_padded_len - 1]; + + if (pad_val < 1 || pad_val > 16) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid pad_val: %d\n", pad_val); + goto exit; + } + + /* + * 5. Return outputs + */ + if (plaintext_len != NULL) { + *plaintext_len = plaintext_padded_len - pad_val; + } + memcpy(plaintext, plaintext_padded, *plaintext_len); + + goto exit; +exit: { + free(plaintext_padded); + mbedtls_aes_free(&aes); + return ret; +} +} + +size_t atchops_aes_ctr_ciphertext_size(const size_t plaintext_len) { return ((plaintext_len + 15) & ~0xF) + 16; } + +size_t atchops_aes_ctr_plaintext_size(const size_t ciphertext_len) { return ((ciphertext_len + 15) & ~0xF) + 16; } diff --git a/packages/atchops/src/aesctr.c b/packages/atchops/src/aesctr.c deleted file mode 100644 index 34b08eba..00000000 --- a/packages/atchops/src/aesctr.c +++ /dev/null @@ -1,153 +0,0 @@ -#include "atchops/aesctr.h" -#include -#include -#include -#include -#include -#include -#include -#include - -#define TAG "atchops_aesctr" - -int atchops_aesctr_encrypt(const unsigned char *key, const enum atchops_aes_size keybits, unsigned char *iv, - const unsigned char *plaintext, // plaintext to encrypt - const size_t plaintextlen, - unsigned char *ciphertext, // allocated buffer to populate - const size_t ciphertextsize, // number of total bytes allocated in the buffer - size_t *ciphertextlen // written actual length in the buffer -) { - int ret = 1; - - mbedtls_aes_context aes; - mbedtls_aes_init(&aes); - - // 1. pad the plaintext - unsigned char *plaintextpadded; // will contain the plaintext with padded trialing bytes - size_t plaintextpaddedlen; // the length of the plain text + padding (no null terminator) - - const int numpadbytestoadd = 16 - (plaintextlen % 16); - const unsigned char padval = numpadbytestoadd; - - plaintextpaddedlen = plaintextlen + numpadbytestoadd; - - if((plaintextpadded = malloc(sizeof(unsigned char) * (plaintextpaddedlen + 1))) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); - goto exit; - } - memcpy(plaintextpadded, plaintext, plaintextlen); - memset(plaintextpadded + plaintextlen, padval, numpadbytestoadd); - plaintextpadded[plaintextpaddedlen] = '\0'; - - // 2. Initialize AES key - ret = mbedtls_aes_setkey_enc(&aes, key, keybits); - if (ret != 0) { - goto exit; - } - - // 3. AES CTR Encrypt - size_t nc_off = 0; - unsigned char stream_block[16]; - memset(stream_block, 0, sizeof(unsigned char) * 16); - memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); // clear the buffer - ret = mbedtls_aes_crypt_ctr(&aes, plaintextpaddedlen, &nc_off, iv, stream_block, plaintextpadded, ciphertext); - if (ret != 0) { - goto exit; - } - - *ciphertextlen = plaintextpaddedlen; // ciphertextlen is the same as plaintextpaddedlen, the plaintext (with padding) could be something like 16 bytes long, so the ciphertext will also be 16 bytes long. - - goto exit; - -exit: { - free(plaintextpadded); - mbedtls_aes_free(&aes); - return ret; -} -} - -int atchops_aesctr_decrypt(const unsigned char *key, const enum atchops_aes_size keybits, unsigned char *iv, - const unsigned char *ciphertext, const size_t ciphertextlen, unsigned char *plaintext, - const size_t plaintextsize, size_t *plaintextlen) { - int ret = 1; - - mbedtls_aes_context aes; - mbedtls_aes_init(&aes); - - unsigned char *stream_block = NULL; - unsigned char *plaintextpadded = NULL; - - /* - * 1. Prepare AES context - */ - if ((ret = mbedtls_aes_setkey_enc(&aes, key, keybits)) != 0) { - goto exit; - } - - /* - * 2. Allocate buffers required for decryption - */ - size_t nc_off = 0; - if((stream_block = malloc(sizeof(unsigned char) * 16)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for stream_block\n"); - goto exit; - } - memset(stream_block, 0, sizeof(unsigned char) * 16); - - const size_t plaintextpaddedsize = plaintextsize + 16; - if((plaintextpadded = malloc(sizeof(unsigned char) * plaintextpaddedsize)) == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plaintextpadded\n"); - goto exit; - } - memset(plaintextpadded, 0, sizeof(unsigned char) * plaintextpaddedsize); - size_t plaintextpaddedlen = 0; - - /* - * 3. Decrypt - */ - if ((ret = mbedtls_aes_crypt_ctr(&aes, ciphertextlen, &nc_off, iv, stream_block, ciphertext, plaintextpadded)) != 0) { - goto exit; - } - - /* - * 4. Remove padding - */ - while (*(plaintextpadded + plaintextpaddedlen++) != '\0') - ; - --plaintextpaddedlen; // don't count the null terminator - - // IBM PKCS Padding method states that there is always at least 1 padded value: - // https://www.ibm.com/docs/en/zos/2.4.0?topic=rules-pkcs-padding-method the value of the padded byte is always the - // number of padded bytes to expect, padval == num_padded_bytes - unsigned char padval = plaintextpadded[plaintextpaddedlen - 1]; - - if(padval < 1 || padval > 16) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid padval: %d\n", padval); - goto exit; - } - - *plaintextlen = plaintextpaddedlen - padval; - memcpy(plaintext, plaintextpadded, *plaintextlen); - - goto exit; -exit: { - free(stream_block); - free(plaintextpadded); - mbedtls_aes_free(&aes); - return ret; -} -} - -size_t atchops_aesctr_ciphertext_size(const size_t plaintextlen) -{ - return ((plaintextlen + 15) & ~0xF) + 16; -} - -size_t atchops_aesctr_plaintext_size(const size_t ciphertextlen) -{ - return ((ciphertextlen + 15) & ~0xF) + 16; -} diff --git a/packages/atchops/src/base64.c b/packages/atchops/src/base64.c index cd200aa6..5a7bbb3e 100644 --- a/packages/atchops/src/base64.c +++ b/packages/atchops/src/base64.c @@ -7,43 +7,104 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) -int atchops_base64_encode(const unsigned char *src, const size_t srclen, unsigned char *dst, const size_t dstsize, - size_t *dstlen) { +int atchops_base64_encode(const unsigned char *src, const size_t src_len, unsigned char *dst, const size_t dst_size, + size_t *dst_len) { int ret = 1; - if (src == NULL || srclen <= 0 || dstsize <= 0) { + + /* + * 1. Validate arguments + */ + if (src == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: src is NULL\n"); + return ret; + } + + if (src_len <= 0) { ret = 1; - atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: Invalid arguments\n"); - atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "src: %p, srclen: %d, dst: %p, dstsize: %d\n", src, srclen, dst, - dstsize); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: src_len is less than or equal to 0\n"); + return ret; } - if (dstlen == NULL) { + + if (dst == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: dst is NULL\n"); + return ret; + } + + if (dst_size <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: dst_size is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Encode + */ + if (dst_len == NULL) { size_t x; // throw away variable - ret = mbedtls_base64_encode(dst, dstsize, &x, src, srclen); + if((ret = mbedtls_base64_encode(dst, dst_size, &x, src, src_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: mbedtls_base64_encode failed\n"); + goto exit; + } } else { - ret = mbedtls_base64_encode(dst, dstsize, dstlen, src, srclen); + if((ret = mbedtls_base64_encode(dst, dst_size, dst_len, src, src_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: mbedtls_base64_encode failed\n"); + goto exit; + } } + ret = 0; goto exit; exit: { return ret; } } -int atchops_base64_decode(const unsigned char *src, const size_t srclen, unsigned char *dst, const size_t dstsize, - size_t *dstlen) { +int atchops_base64_decode(const unsigned char *src, const size_t src_len, unsigned char *dst, const size_t dst_size, + size_t *dst_len) { int ret = 1; - if (src == NULL || srclen <= 0 || dstsize <= 0) { + + /* + * 1. Validate arguments + */ + if(src == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: src is NULL\n"); + return ret; + } + + if(src_len <= 0) { ret = 1; - atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: Invalid arguments\n"); - // log them - atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "src: %p, srclen: %d, dst: %p, dstsize: %d\n", src, srclen, dst, - dstsize); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: src_len is less than or equal to 0\n"); + return ret; } - if (dstlen == NULL) { + + if(dst == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: dst is NULL\n"); + return ret; + } + + if(dst_size <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: dst_size is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Decode + */ + if (dst_len == NULL) { size_t x; // throw away variable - ret = mbedtls_base64_decode(dst, dstsize, &x, src, srclen); + if((ret = mbedtls_base64_decode(dst, dst_size, &x, src, src_len)) != 0) { + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: mbedtls_base64_decode failed\n"); + goto exit; + } } else { - ret = mbedtls_base64_decode(dst, dstsize, dstlen, src, srclen); + if((ret = mbedtls_base64_decode(dst, dst_size, dst_len, src, src_len)) != 0) { + atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: mbedtls_base64_decode failed\n"); + goto exit; + } } + ret = 0; goto exit; exit: { return ret; } } diff --git a/packages/atchops/src/iv.c b/packages/atchops/src/iv.c index 9e8acf35..25fab70e 100644 --- a/packages/atchops/src/iv.c +++ b/packages/atchops/src/iv.c @@ -4,19 +4,35 @@ #include #include #include -#include #include +#include #define TAG "iv" int atchops_iv_generate(unsigned char *iv) { int ret = 1; + + /* + * 1. Validate arguments + */ + if (iv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "iv is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); + /* + * 3. Seed the random number generator + */ if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)ATCHOPS_RNG_PERSONALIZATION, strlen(ATCHOPS_RNG_PERSONALIZATION))) != 0) { @@ -24,6 +40,9 @@ int atchops_iv_generate(unsigned char *iv) { goto exit; } + /* + * 4. Generate the IV + */ if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, iv, ATCHOPS_IV_BUFFER_SIZE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to generate random IV\n"); goto exit; diff --git a/packages/atchops/src/rsa.c b/packages/atchops/src/rsa.c index 12206f5a..9c6dcff5 100644 --- a/packages/atchops/src/rsa.c +++ b/packages/atchops/src/rsa.c @@ -1,5 +1,5 @@ #include "atchops/rsa.h" -#include "atchops/rsakey.h" +#include "atchops/rsa_key.h" #include "atchops/sha.h" #include "atlogger/atlogger.h" #include @@ -13,22 +13,48 @@ #include #include -#define LOGGER_TAG "ATCHOPS RSA" +#define TAG "rsa" -int atchops_rsa_sign(const atchops_rsakey_privatekey privatekey, const atchops_md_type mdtype, - const unsigned char *message, const size_t messagelen, unsigned char *signature) { +int atchops_rsa_sign(const atchops_rsa_key_private_key *private_key, const atchops_md_type md_type, + const unsigned char *message, const size_t message_len, unsigned char *signature) { int ret = 1; - size_t hashsize; + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is NULL\n"); + return ret; + } - if (mdtype == ATCHOPS_MD_SHA256) { - hashsize = 32; - } else { + if (md_type != ATCHOPS_MD_SHA256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa sign\n"); + return ret; + } + + if (message == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "message is NULL\n"); + return ret; + } + + if (message_len <= 0) { ret = 1; - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa sign\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "message_len is less than or equal to 0\n"); return ret; } + /* + * 2. Variables + */ + size_t hashsize; + + if (md_type == ATCHOPS_MD_SHA256) { + hashsize = 32; + } + unsigned char hash[hashsize]; memset(hash, 0, sizeof(unsigned char) * hashsize); @@ -42,44 +68,44 @@ int atchops_rsa_sign(const atchops_rsakey_privatekey privatekey, const atchops_m mbedtls_ctr_drbg_init(&ctr_drbg_ctx); /* - * 1. Hash the message + * 3. Hash the message */ - if ((ret = atchops_sha_hash(mdtype, message, messagelen, hash)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to perform for sha hash for rsa signing\n"); + if ((ret = atchops_sha_hash(md_type, message, message_len, hash)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to perform for sha hash for rsa signing\n"); goto ret; } /* - * 2. Prepare RSA context + * 4. Prepare RSA context */ - if ((ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, - privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, - privatekey.e.value, privatekey.e.len)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_import_raw signing\n"); + if ((ret = mbedtls_rsa_import_raw(&rsa, private_key->n.value, private_key->n.len, private_key->p.value, + private_key->p.len, private_key->q.value, private_key->q.len, private_key->d.value, + private_key->d.len, private_key->e.value, private_key->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_import_raw signing\n"); goto ret; } if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_complete operation\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_complete operation\n"); goto ret; } if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_check_privkey operation\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_check_privkey operation\n"); goto ret; } if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_ctr_drbg_seed operation\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_ctr_drbg_seed operation\n"); goto ret; } /* - * 3. Sign the hash with RSA private key + * 5. Sign the hash with RSA private key */ - if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, atchops_mbedtls_md_map[mdtype], + if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, atchops_mbedtls_md_map[md_type], hashsize, hash, signature)) != 0) { - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_pkcs1_sign operation\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_pkcs1_sign operation\n"); goto ret; } @@ -93,20 +119,52 @@ ret: { } } -int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_md_type mdtype, - const unsigned char *message, const size_t messagelen, unsigned char *signature) { +int atchops_rsa_verify(const atchops_rsa_key_public_key *public_key, const atchops_md_type md_type, + const unsigned char *message, const size_t message_len, unsigned char *signature) { int ret = 1; - size_t hashsize; + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is NULL\n"); + return ret; + } - if (mdtype == ATCHOPS_MD_SHA256) { - hashsize = 32; - } else { + if (md_type != ATCHOPS_MD_SHA256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa verify\n"); + return ret; + } + + if (message == NULL) { ret = 1; - atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported hash type for rsa verify\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "message is NULL\n"); return ret; } + if (message_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "message_len is less than or equal to 0\n"); + return ret; + } + + if (signature == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "signature is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ + size_t hashsize; + + if (md_type == ATCHOPS_MD_SHA256) { + hashsize = 32; + } + unsigned char hash[hashsize]; memset(hash, 0, sizeof(unsigned char) * hashsize); @@ -114,10 +172,10 @@ int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_m mbedtls_rsa_init(&rsa); /* - * 1. Prepare RSA context + * 3. Prepare RSA context */ - if ((ret = mbedtls_rsa_import_raw(&rsa, publickey.n.value, publickey.n.len, NULL, 0, NULL, 0, NULL, 0, - publickey.e.value, publickey.e.len)) != 0) { + if ((ret = mbedtls_rsa_import_raw(&rsa, public_key->n.value, public_key->n.len, NULL, 0, NULL, 0, NULL, 0, + public_key->e.value, public_key->e.len)) != 0) { goto exit; } @@ -126,16 +184,16 @@ int atchops_rsa_verify(const atchops_rsakey_publickey publickey, const atchops_m } /* - * 2. Hash the message + * 4. Hash the message */ - if ((ret = atchops_sha_hash(mdtype, message, messagelen, hash) != 0)) { + if ((ret = atchops_sha_hash(md_type, message, message_len, hash) != 0)) { goto exit; } /* - * 3. Verify the signature with RSA public key + * 5. Verify the signature with RSA public key */ - if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, atchops_mbedtls_md_map[mdtype], hashsize, hash, signature)) != 0) { + if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, atchops_mbedtls_md_map[md_type], hashsize, hash, signature)) != 0) { goto exit; } @@ -147,11 +205,41 @@ exit: { } } -int atchops_rsa_encrypt(const atchops_rsakey_publickey publickey, const unsigned char *plaintext, - const size_t plaintextlen, unsigned char *ciphertext, const size_t ciphertextsize, - size_t *ciphertextlen) { +int atchops_rsa_encrypt(const atchops_rsa_key_public_key *public_key, const unsigned char *plaintext, + const size_t plaintext_len, unsigned char *ciphertext) { int ret = 1; + /* + * 1. Validate arguments + */ + + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is NULL\n"); + return ret; + } + + if (plaintext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext is NULL\n"); + return ret; + } + + if (plaintext_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext_len is less than or equal to 0\n"); + return ret; + } + + if (ciphertext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ mbedtls_rsa_context rsa; mbedtls_rsa_init(&rsa); @@ -162,41 +250,35 @@ int atchops_rsa_encrypt(const atchops_rsakey_publickey publickey, const unsigned mbedtls_ctr_drbg_init(&ctr_drbg_ctx); /* - * 1. Prepare RSA context + * 3. Prepare RSA context */ - ret = mbedtls_rsa_import_raw(&rsa, publickey.n.value, publickey.n.len, NULL, -1, NULL, -1, NULL, -1, - publickey.e.value, publickey.e.len); - if (ret != 0) { + if ((ret = mbedtls_rsa_import_raw(&rsa, public_key->n.value, public_key->n.len, NULL, -1, NULL, -1, NULL, -1, + public_key->e.value, public_key->e.len)) != 0) { goto exit; } - ret = mbedtls_rsa_check_pubkey(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_check_pubkey(&rsa)) != 0) { goto exit; } - ret = mbedtls_rsa_complete(&rsa); - if (ret != 0) { + if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { goto exit; } - ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0); - if (ret != 0) { + if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0)) != 0) { goto exit; } /* - * 2. Encrypt the plaintext with RSA public key + * 4. Encrypt the plaintext with RSA public key */ - ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintextlen, plaintext, ciphertext); - if (ret != 0) { + if ((ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintext_len, plaintext, + ciphertext)) != 0) { goto exit; } - *ciphertextlen = 256; - + ret = 0; goto exit; - exit: { mbedtls_rsa_free(&rsa); mbedtls_ctr_drbg_free(&ctr_drbg_ctx); @@ -205,11 +287,47 @@ exit: { } } -int atchops_rsa_decrypt(const atchops_rsakey_privatekey privatekey, const unsigned char *ciphertext, - const size_t ciphertextlen, unsigned char *plaintext, const size_t plaintextsize, - size_t *plaintextlen) { +int atchops_rsa_decrypt(const atchops_rsa_key_private_key *private_key, const unsigned char *ciphertext, + const size_t ciphertext_len, unsigned char *plaintext, const size_t plaintextsize, + size_t *plaintext_len) { int ret = 1; + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is NULL\n"); + return ret; + } + + if (ciphertext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext is NULL\n"); + return ret; + } + + if (ciphertext_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ciphertext_len is less than or equal to 0\n"); + return ret; + } + + if (plaintext == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext is NULL\n"); + return ret; + } + + if (plaintextsize <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintextsize is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Variables + */ mbedtls_entropy_context entropy_ctx; mbedtls_entropy_init(&entropy_ctx); @@ -220,34 +338,47 @@ int atchops_rsa_decrypt(const atchops_rsakey_privatekey privatekey, const unsign mbedtls_rsa_init(&rsa); /* - * 1. Prepare RSA context + * 3. Prepare RSA context */ - if ((ret = mbedtls_rsa_import_raw(&rsa, privatekey.n.value, privatekey.n.len, privatekey.p.value, privatekey.p.len, - privatekey.q.value, privatekey.q.len, privatekey.d.value, privatekey.d.len, - privatekey.e.value, privatekey.e.len)) != 0) { + if ((ret = mbedtls_rsa_import_raw(&rsa, private_key->n.value, private_key->n.len, private_key->p.value, + private_key->p.len, private_key->q.value, private_key->q.len, private_key->d.value, + private_key->d.len, private_key->e.value, private_key->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_import_raw operation\n"); goto exit; } if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_complete operation\n"); goto exit; } if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_check_privkey operation\n"); goto exit; } if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, NULL, 0)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_ctr_drbg_seed operation\n"); goto exit; } /* - * 2. Decrypt the ciphertext with RSA private key + * 4. Decrypt the ciphertext with RSA private key */ - if ((ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, plaintextlen, ciphertext, - plaintext, plaintextsize)) != 0) { + size_t olen; + if ((ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg_ctx, &olen, ciphertext, plaintext, + plaintextsize)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to do mbedtls_rsa_pkcs1_decrypt operation\n"); goto exit; } + /* + * 5. Set the length of the plaintext + */ + if (plaintext_len != NULL) { + *plaintext_len = olen; + } + ret = 0; goto exit; exit: { @@ -258,7 +389,8 @@ exit: { } } -int atchops_rsa_generate(atchops_rsakey_publickey *publickey, atchops_rsakey_privatekey *privatekey, - const unsigned int keysize) { +int atchops_rsa_generate(atchops_rsa_key_public_key *public_key, atchops_rsa_key_private_key *private_key, + const unsigned int key_size) { + // TODO maybe also introduce `enum atchops_rsa_key_size` ? return 1; // TODO: implement } diff --git a/packages/atchops/src/rsa_key.c b/packages/atchops/src/rsa_key.c new file mode 100644 index 00000000..2741fc0f --- /dev/null +++ b/packages/atchops/src/rsa_key.c @@ -0,0 +1,1124 @@ +#include "atchops/rsa_key.h" +#include "atchops/base64.h" +#include +#include +#include +#include +#include + +#define TAG "rsa_key" + +#define BASE64_DECODED_KEY_BUFFER_SIZE 8192 // the max buffer size of a decoded RSA key + +void atchops_rsa_key_public_key_init(atchops_rsa_key_public_key *public_key) { + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return; + } + + /* + * 2. Initialize the key + */ + memset(public_key, 0, sizeof(atchops_rsa_key_public_key)); +} + +void atchops_rsa_key_public_key_free(atchops_rsa_key_public_key *public_key) { + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return; + } + + /* + * 2. Free the key + */ + if (atchops_rsa_key_public_key_is_n_initialized(public_key)) { + atchops_rsa_key_public_key_unset_n(public_key); + } + + if (atchops_rsa_key_public_key_is_e_initialized(public_key)) { + atchops_rsa_key_public_key_unset_e(public_key); + } + memset(public_key, 0, sizeof(atchops_rsa_key_public_key)); +} + +void atchops_rsa_key_private_key_init(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Initialize the key + */ + memset(private_key, 0, sizeof(atchops_rsa_key_private_key)); +} + +void atchops_rsa_key_private_key_free(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Free the key + */ + if (atchops_rsa_key_private_key_is_n_initialized(private_key)) { + atchops_rsa_key_private_key_unset_n(private_key); + } + + if (atchops_rsa_key_private_key_is_e_initialized(private_key)) { + atchops_rsa_key_private_key_unset_e(private_key); + } + + if (atchops_rsa_key_private_key_is_d_initialized(private_key)) { + atchops_rsa_key_private_key_unset_d(private_key); + } + + if (atchops_rsa_key_private_key_is_p_initialized(private_key)) { + atchops_rsa_key_private_key_unset_p(private_key); + } + + if (atchops_rsa_key_private_key_is_q_initialized(private_key)) { + atchops_rsa_key_private_key_unset_q(private_key); + } + memset(private_key, 0, sizeof(atchops_rsa_key_private_key)); +} + +int atchops_rsa_key_public_key_clone(const atchops_rsa_key_public_key *src, atchops_rsa_key_public_key *dst) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (src == NULL || dst == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src or dst is null\n"); + return ret; + } + + /* + * 2. Clone the key + */ + if ((ret = atchops_rsa_key_public_key_set_n(dst, src->n.value, src->n.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_public_key_set_e(dst, src->e.value, src->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atchops_rsa_key_private_key_clone(const atchops_rsa_key_private_key *src, atchops_rsa_key_private_key *dst) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (src == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is null\n"); + return ret; + } + + if (dst == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "dst is null\n"); + return ret; + } + + /* + * 2. Clone the key + */ + if ((ret = atchops_rsa_key_private_key_set_n(dst, src->n.value, src->n.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_e(dst, src->e.value, src->e.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_d(dst, src->d.value, src->d.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_p(dst, src->p.value, src->p.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_q(dst, src->q.value, src->q.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +int atchops_rsa_key_populate_public_key(atchops_rsa_key_public_key *public_key, const char *public_key_base64, + const size_t public_key_base64_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return ret; + } + + if (public_key_base64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key_base64 is null\n"); + return ret; + } + + if (public_key_base64_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key_base64_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Variables + */ + mbedtls_asn1_sequence *seq = NULL; // free later + + const size_t dst_size = BASE64_DECODED_KEY_BUFFER_SIZE; + unsigned char dst[dst_size]; + memset(dst, 0, sizeof(unsigned char) * dst_size); + size_t dst_len = 0; + + if ((ret = atchops_base64_decode((const unsigned char *)public_key_base64, public_key_base64_len, dst, dst_size, + &dst_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; + } + + unsigned char *p = dst; + unsigned char *end = dst + dst_len; + + size_t lengthread = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 1\n"); + goto exit; + } + + size_t lengthread2 = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread2, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 2\n"); + goto exit; + } + p = p + (lengthread2); + + size_t lengthread3 = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread3, MBEDTLS_ASN1_BIT_STRING)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 3\n"); + goto exit; + } + + if (*p == 0x00) { + p = p + 1; + } + + if ((seq = malloc(sizeof(mbedtls_asn1_sequence))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for seq\n"); + goto exit; + } + memset(seq, 0, sizeof(mbedtls_asn1_sequence)); + if ((ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get sequence of\n"); + goto exit; + } + + mbedtls_asn1_sequence *current = seq; + if ((ret = atchops_rsa_key_public_key_set_n(public_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + current = current->next; + if ((ret = atchops_rsa_key_public_key_set_e(public_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { + mbedtls_asn1_sequence_free(seq); + return ret; +} +} + +int atchops_rsa_key_populate_private_key(atchops_rsa_key_private_key *private_key, const char *private_key_base64, + const size_t private_key_base64_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return ret; + } + + if (private_key_base64 == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key_base64 is null\n"); + return ret; + } + + if (private_key_base64_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key_base64_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Variables + */ + mbedtls_asn1_sequence *seq = NULL; // free later + + const size_t dst_size = BASE64_DECODED_KEY_BUFFER_SIZE; + unsigned char dst[dst_size]; + memset(dst, 0, sizeof(unsigned char) * dst_size); + size_t dst_len = 0; + + if ((ret = atchops_base64_decode((const unsigned char *)private_key_base64, private_key_base64_len, dst, dst_size, + &dst_len)) != 0) { + goto exit; + } + + unsigned char *p = dst; + unsigned char *end = dst + dst_len; + + size_t lengthread = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 1\n"); + goto exit; + } + + size_t lengthread2 = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread2, MBEDTLS_ASN1_INTEGER)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 2\n"); + goto exit; + } + p = p + lengthread2; + + size_t lengthread3 = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread3, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 3\n"); + goto exit; + } + p = p + lengthread3; + + size_t lengthread4 = 0; + if ((ret = mbedtls_asn1_get_tag(&p, end, &lengthread4, 0x04)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get tag 4\n"); + goto exit; + } + + if ((seq = malloc(sizeof(mbedtls_asn1_sequence))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for seq\n"); + goto exit; + } + memset(seq, 0, sizeof(mbedtls_asn1_sequence)); + if ((ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER)) != 0) { + goto exit; + } + + mbedtls_asn1_sequence *current = seq; + current = current->next; + + if ((ret = atchops_rsa_key_private_key_set_n(private_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + current = current->next; + if ((ret = atchops_rsa_key_private_key_set_e(private_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + current = current->next; + if ((ret = atchops_rsa_key_private_key_set_d(private_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + current = current->next; + if ((ret = atchops_rsa_key_private_key_set_p(private_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + current = current->next; + if ((ret = atchops_rsa_key_private_key_set_q(private_key, current->buf.p, current->buf.len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { + mbedtls_asn1_sequence_free(seq); + return ret; +} +} + +int atchops_rsa_key_public_key_set_ne(atchops_rsa_key_public_key *public_key, const unsigned char *n, + const size_t n_len, const unsigned char *e, const size_t e_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return ret; + } + + if (n == NULL || n_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or n_len is less than or equal to 0\n"); + return ret; + } + + if (e == NULL || e_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or e_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Set the key + */ + if ((ret = atchops_rsa_key_public_key_set_n(public_key, n, n_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_public_key_set_e(public_key, e, e_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +bool atchops_rsa_key_public_key_is_n_initialized(atchops_rsa_key_public_key *public_key) { + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + return false; + } + + /* + * 2. Check if n is initialized + */ + return public_key->n._is_value_initialized; +} + +void atchops_rsa_key_public_key_set_n_initialized(atchops_rsa_key_public_key *public_key, const bool is_initialized) { + public_key->n._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_public_key_set_n(atchops_rsa_key_public_key *public_key, const unsigned char *n, + const size_t n_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return ret; + } + + if (n == NULL || n_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or n_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Set the key + */ + if (atchops_rsa_key_public_key_is_n_initialized(public_key)) { + atchops_rsa_key_public_key_unset_n(public_key); + } + + public_key->n.len = n_len; + + if ((public_key->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (public_key->n.len))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); + goto exit; + } + + atchops_rsa_key_public_key_set_n_initialized(public_key, true); + memcpy(public_key->n.value, n, n_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_public_key_unset_n(atchops_rsa_key_public_key *public_key) { + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_public_key_is_n_initialized(public_key)) { + free(public_key->n.value); + } + atchops_rsa_key_public_key_set_n_initialized(public_key, false); + public_key->n.value = NULL; + public_key->n.len = 0; +} + +bool atchops_rsa_key_public_key_is_e_initialized(atchops_rsa_key_public_key *public_key) { + return public_key->e._is_value_initialized; +} + +void atchops_rsa_key_public_key_set_e_initialized(atchops_rsa_key_public_key *public_key, const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return; + } + + /* + * 2. Set the key + */ + public_key->e._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_public_key_set_e(atchops_rsa_key_public_key *public_key, const unsigned char *e, + const size_t e_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (public_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "public_key is null\n"); + return ret; + } + + if (e == NULL || e_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or e_len is less than or equal to 0\n"); + goto exit; + } + + /* + * 2. Set the key + */ + + if (atchops_rsa_key_public_key_is_e_initialized(public_key)) { + atchops_rsa_key_public_key_unset_e(public_key); + } + + public_key->e.len = e_len; + if ((public_key->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (public_key->e.len))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); + goto exit; + } + + atchops_rsa_key_public_key_set_e_initialized(public_key, true); + memcpy(public_key->e.value, e, e_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_public_key_unset_e(atchops_rsa_key_public_key *public_key) { + if (atchops_rsa_key_public_key_is_e_initialized(public_key)) { + free(public_key->e.value); + } + atchops_rsa_key_public_key_set_e_initialized(public_key, false); + public_key->e.value = NULL; + public_key->e.len = 0; +} + +int atchops_rsa_key_private_key_set_nedpq(atchops_rsa_key_private_key *private_key, const unsigned char *n, + const size_t n_len, const unsigned char *e, const size_t e_len, + const unsigned char *d, const size_t d_len, const unsigned char *p, + const size_t p_len, const unsigned char *q, const size_t q_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return ret; + } + + if (n == NULL || n_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or n_len is less than or equal to 0\n"); + return ret; + } + + if (e == NULL || e_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or e_len is less than or equal to 0\n"); + return ret; + } + + if (d == NULL || d_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "d is null or d_len is less than or equal to 0\n"); + return ret; + } + + if (p == NULL || p_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "p is null or p_len is less than or equal to 0\n"); + return ret; + } + + if (q == NULL || q_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "q is null or q_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Set the key + */ + if ((ret = atchops_rsa_key_private_key_set_n(private_key, n, n_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_e(private_key, e, e_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_d(private_key, d, d_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_p(private_key, p, p_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); + goto exit; + } + + if ((ret = atchops_rsa_key_private_key_set_q(private_key, q, q_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +bool atchops_rsa_key_private_key_is_n_initialized(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + return false; + } + + /* + * 2. Check if n is initialized + */ + return private_key->n._is_value_initialized; +} + +void atchops_rsa_key_private_key_set_n_initialized(atchops_rsa_key_private_key *private_key, + const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Set the value + */ + private_key->n._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_private_key_set_n(atchops_rsa_key_private_key *private_key, const unsigned char *n, + const size_t n_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return ret; + } + + if (n == NULL || n_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or n_len is less than or equal to 0\n"); + return ret; + } + + /* + * 2. Set the key + */ + if (atchops_rsa_key_private_key_is_n_initialized(private_key)) { + atchops_rsa_key_private_key_unset_n(private_key); + } + + private_key->n.len = n_len; + if ((private_key->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (private_key->n.len))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); + goto exit; + } + + atchops_rsa_key_private_key_set_n_initialized(private_key, true); + memcpy(private_key->n.value, n, n_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_private_key_unset_n(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_private_key_is_n_initialized(private_key)) { + free(private_key->n.value); + } + atchops_rsa_key_private_key_set_n_initialized(private_key, false); + private_key->n.value = NULL; + private_key->n.len = 0; +} + +bool atchops_rsa_key_private_key_is_e_initialized(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + return false; + } + return private_key->e._is_value_initialized; +} + +void atchops_rsa_key_private_key_set_e_initialized(atchops_rsa_key_private_key *private_key, + const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Set the value + */ + private_key->e._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_private_key_set_e(atchops_rsa_key_private_key *private_key, const unsigned char *e, + const size_t e_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + goto exit; + } + + if (e == NULL || e_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or e_len is less than or equal to 0\n"); + goto exit; + } + + /* + * 2. Set the key + */ + + if (private_key->e._is_value_initialized) { + atchops_rsa_key_private_key_unset_e(private_key); + } + + private_key->e.len = e_len; + + if ((private_key->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (private_key->e.len))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); + goto exit; + } + + atchops_rsa_key_private_key_set_e_initialized(private_key, true); + memcpy(private_key->e.value, e, e_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_private_key_unset_e(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_private_key_is_e_initialized(private_key)) { + free(private_key->e.value); + } + atchops_rsa_key_private_key_set_e_initialized(private_key, false); + private_key->e.value = NULL; + private_key->e.len = 0; +} + +bool atchops_rsa_key_private_key_is_d_initialized(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + return false; + } + + /* + * 2. Check if d is initialized + */ + return private_key->d._is_value_initialized; +} + +void atchops_rsa_key_private_key_set_d_initialized(atchops_rsa_key_private_key *private_key, + const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Set the value + */ + private_key->d._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_private_key_set_d(atchops_rsa_key_private_key *private_key, const unsigned char *d, + const size_t d_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + goto exit; + } + + if (d == NULL || d_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "d is null or d_len is less than or equal to 0\n"); + goto exit; + } + + /* + * 2. Set the key + */ + if (atchops_rsa_key_private_key_is_d_initialized(private_key)) { + atchops_rsa_key_private_key_unset_d(private_key); + } + + private_key->d.len = d_len; + + if ((private_key->d.value = (unsigned char *)malloc(sizeof(unsigned char) * (private_key->d.len))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for d value\n"); + goto exit; + } + + atchops_rsa_key_private_key_set_d_initialized(private_key, true); + memcpy(private_key->d.value, d, d_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_private_key_unset_d(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_private_key_is_d_initialized(private_key)) { + free(private_key->d.value); + } + atchops_rsa_key_private_key_set_d_initialized(private_key, false); + private_key->d.value = NULL; + private_key->d.len = 0; +} + +bool atchops_rsa_key_private_key_is_p_initialized(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + return false; + } + + /* + * 2. Check if p is initialized + */ + return private_key->p._is_value_initialized; +} + +void atchops_rsa_key_private_key_set_p_initialized(atchops_rsa_key_private_key *private_key, + const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Set the value + */ + private_key->p._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_private_key_set_p(atchops_rsa_key_private_key *private_key, const unsigned char *p, + const size_t p_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + goto exit; + } + + if (p == NULL || p_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "p is null or p_len is less than or equal to 0\n"); + goto exit; + } + + /* + * 2. Set the key + */ + if (atchops_rsa_key_private_key_is_p_initialized(private_key)) { + atchops_rsa_key_private_key_unset_p(private_key); + } + + private_key->p.len = p_len; + + private_key->p.value = (unsigned char *)malloc(sizeof(unsigned char) * (private_key->p.len)); + if (private_key->p.value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for p value\n"); + goto exit; + } + + atchops_rsa_key_private_key_set_p_initialized(private_key, true); + memcpy(private_key->p.value, p, p_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_private_key_unset_p(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_private_key_is_p_initialized(private_key)) { + free(private_key->p.value); + } + atchops_rsa_key_private_key_set_p_initialized(private_key, false); + private_key->p.value = NULL; + private_key->p.len = 0; +} + +bool atchops_rsa_key_private_key_is_q_initialized(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + return false; + } + + /* + * 2. Check if q is initialized + */ + return private_key->q._is_value_initialized; +} + +void atchops_rsa_key_private_key_set_q_initialized(atchops_rsa_key_private_key *private_key, const bool is_initialized) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Set the value + */ + private_key->q._is_value_initialized = is_initialized; +} + +int atchops_rsa_key_private_key_set_q(atchops_rsa_key_private_key *private_key, const unsigned char *q, + const size_t q_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + goto exit; + } + + if (q == NULL || q_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "q is null or q_len is less than or equal to 0\n"); + goto exit; + } + + /* + * 2. Set the key + */ + if (atchops_rsa_key_private_key_is_q_initialized(private_key)) { + atchops_rsa_key_private_key_unset_q(private_key); + } + + private_key->q.len = q_len; + + if ((private_key->q.value = (unsigned char *)malloc(sizeof(unsigned char) * q_len)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for q value\n"); + goto exit; + } + + atchops_rsa_key_private_key_set_q_initialized(private_key, true); + memcpy(private_key->q.value, q, q_len); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atchops_rsa_key_private_key_unset_q(atchops_rsa_key_private_key *private_key) { + /* + * 1. Validate arguments + */ + if (private_key == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "private_key is null\n"); + return; + } + + /* + * 2. Unset the key + */ + if (atchops_rsa_key_private_key_is_q_initialized(private_key)) { + free(private_key->q.value); + } + atchops_rsa_key_private_key_set_q_initialized(private_key, false); + private_key->q.value = NULL; + private_key->q.len = 0; +} diff --git a/packages/atchops/src/rsakey.c b/packages/atchops/src/rsakey.c deleted file mode 100644 index 93a7a1f3..00000000 --- a/packages/atchops/src/rsakey.c +++ /dev/null @@ -1,592 +0,0 @@ -#include "atchops/rsakey.h" -#include "atchops/base64.h" -#include -#include -#include -#include -#include - -#define BASE64_DECODED_KEY_BUFFER_SIZE 8192 // the max buffer size of a decoded RSA key - -#define TAG "rsakey" - -void atchops_rsakey_publickey_init(atchops_rsakey_publickey *publickey) { - memset(publickey, 0, sizeof(atchops_rsakey_publickey)); -} - -void atchops_rsakey_publickey_free(atchops_rsakey_publickey *publickey) { - atchops_rsakey_publickey_unset_n(publickey); - atchops_rsakey_publickey_unset_e(publickey); - memset(publickey, 0, sizeof(atchops_rsakey_publickey)); -} - -void atchops_rsakey_privatekey_init(atchops_rsakey_privatekey *privatekey) { - memset(privatekey, 0, sizeof(atchops_rsakey_privatekey)); -} - -void atchops_rsakey_privatekey_free(atchops_rsakey_privatekey *privatekey) { - atchops_rsakey_privatekey_unset_n(privatekey); - atchops_rsakey_privatekey_unset_e(privatekey); - atchops_rsakey_privatekey_unset_d(privatekey); - atchops_rsakey_privatekey_unset_p(privatekey); - atchops_rsakey_privatekey_unset_q(privatekey); - memset(privatekey, 0, sizeof(atchops_rsakey_privatekey)); -} - -int atchops_rsakey_publickey_clone(const atchops_rsakey_publickey *src, atchops_rsakey_publickey *dst) { - int ret = 1; - - if ((ret = atchops_rsakey_publickey_set_n(dst, src->n.value, src->n.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - if ((ret = atchops_rsakey_publickey_set_e(dst, src->e.value, src->e.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} - -int atchops_rsakey_privatekey_clone(const atchops_rsakey_privatekey *src, atchops_rsakey_privatekey *dst) { - int ret = 1; - - if ((ret = atchops_rsakey_privatekey_set_n(dst, src->n.value, src->n.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_e(dst, src->e.value, src->e.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_d(dst, src->d.value, src->d.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_p(dst, src->p.value, src->p.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_q(dst, src->q.value, src->q.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} - -int atchops_rsakey_populate_publickey(atchops_rsakey_publickey *publickey, const char *publickeybase64, - const size_t publickeybase64len) { - int ret = 0; - - mbedtls_asn1_sequence *seq = NULL; // free later - - size_t dstsize = BASE64_DECODED_KEY_BUFFER_SIZE; - unsigned char dst[dstsize]; - memset(dst, 0, sizeof(unsigned char) * dstsize); - size_t dstlen = 0; - ret = atchops_base64_decode((const unsigned char *)publickeybase64, publickeybase64len, dst, dstsize, &dstlen); - if (ret != 0) { - goto exit; - } - - unsigned char *p = dst; - unsigned char *end = dst + dstlen; - - size_t lengthread = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); - if (ret != 0) { - goto exit; - } - - size_t lengthread2 = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread2, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); - if (ret != 0) { - goto exit; - } - p = p + (lengthread2); - - size_t lengthread3 = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread3, MBEDTLS_ASN1_BIT_STRING); - if (ret != 0) { - goto exit; - } - - if (*p == 0x00) { - p = p + 1; - } - - seq = malloc(sizeof(mbedtls_asn1_sequence)); - if (seq == NULL) { - ret = 1; - goto exit; - } - memset(seq, 0, sizeof(mbedtls_asn1_sequence)); - ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER); - if (ret != 0) { - goto exit; - } - - mbedtls_asn1_sequence *current = seq; - if((ret = atchops_rsakey_publickey_set_n(publickey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - current = current->next; - if((ret = atchops_rsakey_publickey_set_e(publickey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { - mbedtls_asn1_sequence_free(seq); - return ret; -} -} - -int atchops_rsakey_populate_privatekey(atchops_rsakey_privatekey *privatekey, const char *privatekeybase64, - const size_t privatekeybase64len) { - int ret = 1; - - mbedtls_asn1_sequence *seq = NULL; // free later - - const size_t dstsize = BASE64_DECODED_KEY_BUFFER_SIZE; - unsigned char dst[dstsize]; - memset(dst, 0, sizeof(unsigned char) * dstsize); - size_t dstlen = 0; - ret = atchops_base64_decode((const unsigned char *)privatekeybase64, privatekeybase64len, dst, dstsize, &dstlen); - if (ret != 0) { - goto exit; - } - - unsigned char *p = dst; - unsigned char *end = dst + dstlen; - - size_t lengthread = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); - if (ret != 0) { - goto exit; - } - - size_t lengthread2 = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread2, MBEDTLS_ASN1_INTEGER); - if (ret != 0) { - goto exit; - } - p = p + lengthread2; - - size_t lengthread3 = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread3, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); - if (ret != 0) { - goto exit; - } - p = p + lengthread3; - - size_t lengthread4 = 0; - ret = mbedtls_asn1_get_tag(&p, end, &lengthread4, 0x04); - if (ret != 0) { - goto exit; - } - - seq = malloc(sizeof(mbedtls_asn1_sequence)); - if (seq == NULL) { - ret = 1; - goto exit; - } - memset(seq, 0, sizeof(mbedtls_asn1_sequence)); - ret = mbedtls_asn1_get_sequence_of(&p, end, seq, MBEDTLS_ASN1_INTEGER); - if (ret != 0) { - goto exit; - } - - mbedtls_asn1_sequence *current = seq; - current = current->next; - - if ((ret = atchops_rsakey_privatekey_set_n(privatekey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - current = current->next; - if ((ret = atchops_rsakey_privatekey_set_e(privatekey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - current = current->next; - if ((ret = atchops_rsakey_privatekey_set_d(privatekey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - current = current->next; - if ((ret = atchops_rsakey_privatekey_set_p(privatekey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - current = current->next; - if ((ret = atchops_rsakey_privatekey_set_q(privatekey, current->buf.p, current->buf.len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { - mbedtls_asn1_sequence_free(seq); - return ret; -} -} - -int atchops_rsakey_publickey_set_ne(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen, - const unsigned char *e, const size_t elen) { - int ret = 1; - - if ((ret = atchops_rsakey_publickey_set_n(publickey, n, nlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - if ((ret = atchops_rsakey_publickey_set_e(publickey, e, elen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} - -bool atchops_rsakey_publickey_is_n_initialized(atchops_rsakey_publickey *publickey) { - return publickey->n._is_value_initialized; -} - -int atchops_rsakey_publickey_set_n(atchops_rsakey_publickey *publickey, const unsigned char *n, const size_t nlen) { - int ret = 1; - if (n == NULL || nlen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or nlen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_publickey_unset_n(publickey); - - publickey->n.len = nlen; - - publickey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (publickey->n.len)); - if (publickey->n.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); - goto exit; - } - - publickey->n._is_value_initialized = true; - memcpy(publickey->n.value, n, nlen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_publickey_unset_n(atchops_rsakey_publickey *publickey) { - if (publickey->n._is_value_initialized) { - free(publickey->n.value); - } - publickey->n._is_value_initialized = false; - publickey->n.value = NULL; - publickey->n.len = 0; -} - -bool atchops_rsakey_publickey_is_e_initialized(atchops_rsakey_publickey *publickey) { - return publickey->e._is_value_initialized; -} - -int atchops_rsakey_publickey_set_e(atchops_rsakey_publickey *publickey, const unsigned char *e, const size_t elen) { - int ret = 1; - if (e == NULL || elen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or elen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_publickey_unset_e(publickey); - - publickey->e.len = elen; - - publickey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (publickey->e.len)); - if (publickey->e.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); - goto exit; - } - - publickey->e._is_value_initialized = true; - memcpy(publickey->e.value, e, elen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_publickey_unset_e(atchops_rsakey_publickey *publickey) { - if (publickey->e._is_value_initialized) { - free(publickey->e.value); - } - publickey->e._is_value_initialized = false; - publickey->e.value = NULL; - publickey->e.len = 0; -} - -int atchops_rsakey_privatekey_set_nedpq(atchops_rsakey_privatekey *privatekey, const unsigned char *n, - const size_t nlen, const unsigned char *e, const size_t elen, - const unsigned char *d, const size_t dlen, const unsigned char *p, - const size_t plen, const unsigned char *q, const size_t qlen) { - int ret = 1; - - if ((ret = atchops_rsakey_privatekey_set_n(privatekey, n, nlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set n\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_e(privatekey, e, elen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set e\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_d(privatekey, d, dlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set d\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_p(privatekey, p, plen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set p\n"); - goto exit; - } - - if ((ret = atchops_rsakey_privatekey_set_q(privatekey, q, qlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set q\n"); - goto exit; - } - - ret = 0; - goto exit; -exit: { return ret; } -} - -bool atchops_rsakey_privatekey_is_n_initialized(atchops_rsakey_privatekey *privatekey) { - return privatekey->n._is_value_initialized; -} - -int atchops_rsakey_privatekey_set_n(atchops_rsakey_privatekey *privatekey, const unsigned char *n, const size_t nlen) { - int ret = 1; - - if (n == NULL || nlen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "n is null or nlen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_privatekey_unset_n(privatekey); - - privatekey->n.len = nlen; - - privatekey->n.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->n.len)); - if (privatekey->n.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for n value\n"); - goto exit; - } - - privatekey->n._is_value_initialized = true; - memcpy(privatekey->n.value, n, nlen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_privatekey_unset_n(atchops_rsakey_privatekey *privatekey) { - if (privatekey->n._is_value_initialized) { - free(privatekey->n.value); - } - privatekey->n._is_value_initialized = false; - privatekey->n.value = NULL; - privatekey->n.len = 0; -} - -bool atchops_rsakey_privatekey_is_e_initialized(atchops_rsakey_privatekey *privatekey) { - return privatekey->e._is_value_initialized; -} - -int atchops_rsakey_privatekey_set_e(atchops_rsakey_privatekey *privatekey, const unsigned char *e, const size_t elen) { - int ret = 1; - - if (e == NULL || elen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "e is null or elen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_privatekey_unset_e(privatekey); - - privatekey->e.len = elen; - - privatekey->e.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->e.len)); - if (privatekey->e.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for e value\n"); - goto exit; - } - - privatekey->e._is_value_initialized = true; - memcpy(privatekey->e.value, e, elen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_privatekey_unset_e(atchops_rsakey_privatekey *privatekey) { - if (privatekey->e._is_value_initialized) { - free(privatekey->e.value); - } - privatekey->e.value = NULL; - privatekey->e.len = 0; - privatekey->e._is_value_initialized = false; -} - -bool atchops_rsakey_privatekey_is_d_initialized(atchops_rsakey_privatekey *privatekey) { - return privatekey->d._is_value_initialized; -} - -int atchops_rsakey_privatekey_set_d(atchops_rsakey_privatekey *privatekey, const unsigned char *d, const size_t dlen) { - int ret = 1; - - if (d == NULL || dlen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "d is null or dlen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_privatekey_unset_d(privatekey); - - privatekey->d.len = dlen; - - privatekey->d.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->d.len)); - if (privatekey->d.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for d value\n"); - goto exit; - } - - privatekey->d._is_value_initialized = true; - memcpy(privatekey->d.value, d, dlen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_privatekey_unset_d(atchops_rsakey_privatekey *privatekey) { - if (privatekey->d._is_value_initialized) { - free(privatekey->d.value); - } - privatekey->d._is_value_initialized = false; - privatekey->d.value = NULL; - privatekey->d.len = 0; -} - -bool atchops_rsakey_privatekey_is_p_initialized(atchops_rsakey_privatekey *privatekey) { - return privatekey->p._is_value_initialized; -} - -int atchops_rsakey_privatekey_set_p(atchops_rsakey_privatekey *privatekey, const unsigned char *p, const size_t plen) { - int ret = 1; - - if (p == NULL || plen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "p is null or plen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_privatekey_unset_p(privatekey); - - privatekey->p.len = plen; - - privatekey->p.value = (unsigned char *)malloc(sizeof(unsigned char) * (privatekey->p.len)); - if (privatekey->p.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for p value\n"); - goto exit; - } - - privatekey->p._is_value_initialized = true; - memcpy(privatekey->p.value, p, plen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_privatekey_unset_p(atchops_rsakey_privatekey *privatekey) { - if (privatekey->p._is_value_initialized) { - free(privatekey->p.value); - } - privatekey->p._is_value_initialized = false; - privatekey->p.value = NULL; - privatekey->p.len = 0; -} - -bool atchops_rsakey_privatekey_is_q_initialized(atchops_rsakey_privatekey *privatekey) { - return privatekey->q._is_value_initialized; -} - -int atchops_rsakey_privatekey_set_q(atchops_rsakey_privatekey *privatekey, const unsigned char *q, const size_t qlen) { - int ret = 1; - - if (q == NULL || qlen <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "q is null or qlen is less than or equal to 0\n"); - goto exit; - } - - atchops_rsakey_privatekey_unset_q(privatekey); - - privatekey->q.len = qlen; - - privatekey->q.value = (unsigned char *)malloc(sizeof(unsigned char) * qlen); - if (privatekey->q.value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for q value\n"); - goto exit; - } - - privatekey->q._is_value_initialized = true; - memcpy(privatekey->q.value, q, qlen); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atchops_rsakey_privatekey_unset_q(atchops_rsakey_privatekey *privatekey) { - if (privatekey->q._is_value_initialized) { - free(privatekey->q.value); - } - privatekey->q.value = NULL; - privatekey->q.len = 0; - privatekey->q._is_value_initialized = false; -} diff --git a/packages/atchops/src/sha.c b/packages/atchops/src/sha.c index ded9bb1c..ca70c035 100644 --- a/packages/atchops/src/sha.c +++ b/packages/atchops/src/sha.c @@ -1,32 +1,77 @@ #include "atchops/sha.h" +#include #include #include -int atchops_sha_hash(const atchops_md_type mdtype, const unsigned char *input, const size_t inputlen, +#define TAG "sha" + +int atchops_sha_hash(const atchops_md_type md_type, const unsigned char *input, const size_t input_len, unsigned char *output) { int ret = 1; + /* + * 1. Validate arguments + */ + + if (md_type != ATCHOPS_MD_SHA256) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported md_type\n"); + return ret; + } + + if (input == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "input is NULL\n"); + return ret; + } + + if (input_len <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "input_len is less than or equal to 0\n"); + return ret; + } + + if (output == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "output is NULL\n"); + return ret; + } + + /* + * 2. Variables + */ mbedtls_md_context_t md_ctx; mbedtls_md_init(&md_ctx); - ret = mbedtls_md_setup(&md_ctx, mbedtls_md_info_from_type(atchops_mbedtls_md_map[mdtype]), 0); - if (ret != 0) - goto ret; + /* + * 3. Prepare the hash context + */ + if ((ret = mbedtls_md_setup(&md_ctx, mbedtls_md_info_from_type(atchops_mbedtls_md_map[md_type]), 0)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to setup the hash context\n"); + goto exit; + } - ret = mbedtls_md_starts(&md_ctx); - if (ret != 0) - goto ret; + /* + * 4. Hash + */ + if ((ret = mbedtls_md_starts(&md_ctx)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start the hash\n"); + goto exit; + } - ret = mbedtls_md_update(&md_ctx, input, inputlen); - if (ret != 0) - goto ret; + if ((ret = mbedtls_md_update(&md_ctx, input, input_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to update the hash\n"); + goto exit; + } - ret = mbedtls_md_finish(&md_ctx, output); - if (ret != 0) - goto ret; + if ((ret = mbedtls_md_finish(&md_ctx, output)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to finish the hash\n"); + goto exit; + } - goto ret; -ret: { + ret = 0; + goto exit; +exit: { mbedtls_md_free(&md_ctx); return ret; } diff --git a/packages/atchops/src/uuid.c b/packages/atchops/src/uuid.c index 35976e9e..7c2cce7c 100644 --- a/packages/atchops/src/uuid.c +++ b/packages/atchops/src/uuid.c @@ -1,17 +1,41 @@ #include "atchops/uuid.h" +#include +#include #include #include -#include + +#define TAG "uuid" int atchops_uuid_init(void) { return uuid4_init(); } int atchops_uuid_generate(char *uuidstr, const size_t uuidstrlen) { - int ret; + int ret = 1; + + /* + * 1. Validate arguments + */ + if (uuidstr == NULL) { + ret = 1; // UUID buffer is NULL + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "UUID buffer is NULL\n"); + goto exit; + } + + if (uuidstrlen <= 0) { + ret = 1; // UUID buffer length is less than or equal to 0 + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "UUID buffer length is less than or equal to 0\n"); + goto exit; + } + if (uuidstrlen < 37) { ret = 1; // UUID string is 36 characters long + 1 for null terminator = 37 minimum buffer length + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "UUID buffer length is less than 37\n"); goto exit; } + + /* + * 2. Generate UUID + */ uuid4_generate(uuidstr); if (strlen(uuidstr) <= 0) { ret = 1; // an error occurred regarding the UUID generation and writing it to the buffer diff --git a/packages/atchops/tests/test_aes_generate.c b/packages/atchops/tests/test_aes_generate.c index 99bcd04b..e064f5cb 100644 --- a/packages/atchops/tests/test_aes_generate.c +++ b/packages/atchops/tests/test_aes_generate.c @@ -1,5 +1,5 @@ #include "atchops/aes.h" -#include "atchops/aesctr.h" +#include "atchops/aes_ctr.h" #include "atchops/iv.h" #include #include @@ -41,7 +41,7 @@ int main() { memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - ret = atchops_aesctr_encrypt(key, ATCHOPS_AES_256, iv, (const unsigned char *)PLAINTEXT, + ret = atchops_aes_ctr_encrypt(key, ATCHOPS_AES_256, iv, (const unsigned char *)PLAINTEXT, strlen(PLAINTEXT), ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { printf("Error encrypting\n"); @@ -62,7 +62,7 @@ int main() { printf("\n"); memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - ret = atchops_aesctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, + ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, plaintext2size, &plaintext2len); if (ret != 0) { printf("Error decrypting\n"); diff --git a/packages/atchops/tests/test_aesctr.c b/packages/atchops/tests/test_aesctr.c index 4f1fa6cc..92d60144 100644 --- a/packages/atchops/tests/test_aesctr.c +++ b/packages/atchops/tests/test_aesctr.c @@ -1,4 +1,4 @@ -#include "atchops/aesctr.h" +#include "atchops/aes_ctr.h" #include "atchops/base64.h" #include "atchops/iv.h" #include @@ -38,18 +38,18 @@ int main() { goto exit; } - ret = atchops_aesctr_encrypt(key, ATCHOPS_AES_256, iv, (unsigned char *) PLAINTEXT, + ret = atchops_aes_ctr_encrypt(key, ATCHOPS_AES_256, iv, (unsigned char *) PLAINTEXT, strlen(PLAINTEXT), ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { - printf("atchops_aesctr_encrypt (failed): %d\n", ret); + printf("atchops_aes_ctr_encrypt (failed): %d\n", ret); goto exit; } memset(iv, 0, sizeof(unsigned char) * 16); - ret = atchops_aesctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, + ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, plaintext2size, &plaintext2len); if (ret != 0) { - printf("atchops_aesctr_decrypt (failed): %d\n", ret); + printf("atchops_aes_ctr_decrypt (failed): %d\n", ret); goto exit; } diff --git a/packages/atchops/tests/test_aesctr_decrypt.c b/packages/atchops/tests/test_aesctr_decrypt.c index c5ed5308..6f16b3aa 100644 --- a/packages/atchops/tests/test_aesctr_decrypt.c +++ b/packages/atchops/tests/test_aesctr_decrypt.c @@ -1,4 +1,4 @@ -#include "atchops/aesctr.h" +#include "atchops/aes_ctr.h" #include "atchops/iv.h" #include "atchops/base64.h" #include @@ -74,17 +74,17 @@ int main() { goto exit; } - ret = atchops_aesctr_decrypt(key, ATCHOPS_AES_256, iv, + ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext, plaintextsize, &plaintextlen); if (ret != 0) { - printf("atchops_aesctr_decrypt (failed): %d\n", ret); + printf("atchops_aes_ctr_decrypt (failed): %d\n", ret); goto exit; } - printf("atchops_aesctr_decrypt (success): %d\n", ret); + printf("atchops_aes_ctr_decrypt (success): %d\n", ret); if (plaintextlen <= 0) { - printf("atchops_aesctr_decrypt (failed): %d\n", ret); + printf("atchops_aes_ctr_decrypt (failed): %d\n", ret); goto exit; } printf("decrypted text: %.*s\n", (int)plaintextlen, plaintext); diff --git a/packages/atchops/tests/test_rsadecrypt.c b/packages/atchops/tests/test_rsadecrypt.c index 2eeeaac4..2d428bc1 100644 --- a/packages/atchops/tests/test_rsadecrypt.c +++ b/packages/atchops/tests/test_rsadecrypt.c @@ -43,20 +43,20 @@ int main() { const char *ciphertext = CIPHERTEXTBASE64; const size_t ciphertextlen = strlen(ciphertext); - atchops_rsakey_privatekey privatekey; - atchops_rsakey_privatekey_init(&privatekey); + atchops_rsa_key_private_key privatekey; + atchops_rsa_key_private_key_init(&privatekey); const size_t plaintextsize = 4096; unsigned char plaintext[plaintextsize]; memset(plaintext, 0, sizeof(unsigned char) * plaintextsize); size_t plaintextlen = 0; - ret = atchops_rsakey_populate_privatekey(&privatekey, privatekeybase64, privatekeybase64len); + ret = atchops_rsa_key_populate_private_key(&privatekey, privatekeybase64, privatekeybase64len); if (ret != 0) { - printf("atchops_rsakey_populate_privatekey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_private_key (failed): %d\n", ret); goto ret; } - printf("atchops_rsakey_populate_privatekey (success): %d\n", ret); + printf("atchops_rsa_key_populate_private_key (success): %d\n", ret); ret = atchops_rsa_decrypt(privatekey, (const unsigned char *)ciphertext, ciphertextlen, plaintext, plaintextsize, &plaintextlen); diff --git a/packages/atchops/tests/test_rsaencrypt.c b/packages/atchops/tests/test_rsaencrypt.c index 1f4b7d15..01caf7fa 100644 --- a/packages/atchops/tests/test_rsaencrypt.c +++ b/packages/atchops/tests/test_rsaencrypt.c @@ -30,15 +30,15 @@ int main() { memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); size_t ciphertextlen = 0; - atchops_rsakey_publickey publickey; - atchops_rsakey_publickey_init(&publickey); + atchops_rsa_key_public_key publickey; + atchops_rsa_key_public_key_init(&publickey); - ret = atchops_rsakey_populate_publickey(&publickey, publickeybase64, publickeybase64len); + ret = atchops_rsa_key_populate_public_key(&publickey, publickeybase64, publickeybase64len); if (ret != 0) { - printf("atchops_rsakey_populate_publickey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_public_key (failed): %d\n", ret); goto ret; } - printf("atchops_rsakey_populate_publickey (success): %d\n", ret); + printf("atchops_rsa_key_populate_public_key (success): %d\n", ret); ret = atchops_rsa_encrypt(publickey, (const unsigned char *)plaintext, plaintextlen, ciphertext, ciphertextsize, &ciphertextlen); diff --git a/packages/atchops/tests/test_rsaprivatepopulate.c b/packages/atchops/tests/test_rsaprivatepopulate.c index 7f60077f..0adcd5a1 100644 --- a/packages/atchops/tests/test_rsaprivatepopulate.c +++ b/packages/atchops/tests/test_rsaprivatepopulate.c @@ -1,4 +1,4 @@ -#include "atchops/rsakey.h" +#include "atchops/rsa_key.h" #include #include #include @@ -30,12 +30,12 @@ int main() { const char *privatekeybase64 = PRIVATE_KEY_BASE64; const size_t privatekeybase64len = strlen(PRIVATE_KEY_BASE64); - atchops_rsakey_privatekey privatekey; - atchops_rsakey_privatekey_init(&privatekey); + atchops_rsa_key_private_key privatekey; + atchops_rsa_key_private_key_init(&privatekey); - ret = atchops_rsakey_populate_privatekey(&privatekey, privatekeybase64, privatekeybase64len); + ret = atchops_rsa_key_populate_private_key(&privatekey, privatekeybase64, privatekeybase64len); if (ret != 0) { - printf("atchops_rsakey_populate_privatekey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_privatekey (failed): %d\n", ret); goto exit; } diff --git a/packages/atchops/tests/test_rsapublicpopulate.c b/packages/atchops/tests/test_rsapublicpopulate.c index 0718bfbe..169b449d 100644 --- a/packages/atchops/tests/test_rsapublicpopulate.c +++ b/packages/atchops/tests/test_rsapublicpopulate.c @@ -1,5 +1,5 @@ -#include "atchops/rsakey.h" +#include "atchops/rsa_key.h" #include #include #include @@ -16,12 +16,12 @@ int main() { const char *publickeybase64 = PUBLIC_KEY_BASE64; const size_t publickeybase64len = strlen(publickeybase64); - atchops_rsakey_publickey publickey; - atchops_rsakey_publickey_init(&publickey); + atchops_rsa_key_public_key publickey; + atchops_rsa_key_public_key_init(&publickey); - ret = atchops_rsakey_populate_publickey(&publickey, publickeybase64, publickeybase64len); + ret = atchops_rsa_key_populate_public_key(&publickey, publickeybase64, publickeybase64len); if (ret != 0) { - printf("atchops_rsakey_populate_publickey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_public_key (failed): %d\n", ret); goto exit; } diff --git a/packages/atchops/tests/test_rsasign.c b/packages/atchops/tests/test_rsasign.c index 4dd6a0e6..997f25ec 100644 --- a/packages/atchops/tests/test_rsasign.c +++ b/packages/atchops/tests/test_rsasign.c @@ -55,14 +55,14 @@ int main() { memset(signaturebase64, 0, sizeof(char) * SIGNATURE_BASE64_SIZE); size_t signaturebase64len = 0; - atchops_rsakey_privatekey privatekey; - atchops_rsakey_privatekey_init(&privatekey); - ret = atchops_rsakey_populate_privatekey(&privatekey, privatekeybase64, privatekeybase64len); + atchops_rsa_key_private_key privatekey; + atchops_rsa_key_private_key_init(&privatekey); + ret = atchops_rsa_key_populate_private_key(&privatekey, privatekeybase64, privatekeybase64len); if (ret != 0) { - printf("atchops_rsakey_populate_privatekey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_privatekey (failed): %d\n", ret); goto exit; } - printf("atchops_rsakey_populate_privatekey (success): %d\n", ret); + printf("atchops_rsa_key_populate_privatekey (success): %d\n", ret); ret = atchops_rsa_sign(privatekey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); if (ret != 0) { diff --git a/packages/atchops/tests/test_rsaverify.c b/packages/atchops/tests/test_rsaverify.c index 30a3e95a..514b7aa2 100644 --- a/packages/atchops/tests/test_rsaverify.c +++ b/packages/atchops/tests/test_rsaverify.c @@ -56,11 +56,11 @@ int main() { int ret = 1; - atchops_rsakey_publickey publickey; - atchops_rsakey_publickey_init(&publickey); + atchops_rsa_key_public_key publickey; + atchops_rsa_key_public_key_init(&publickey); - atchops_rsakey_privatekey privatekey; - atchops_rsakey_privatekey_init(&privatekey); + atchops_rsa_key_private_key privatekey; + atchops_rsa_key_private_key_init(&privatekey); const char *message = MESSAGE; const size_t messagelen = strlen(message); @@ -75,9 +75,9 @@ int main() { memset(signaturebase64, 0, sizeof(char) * SIGNATURE_BASE64_SIZE); size_t signaturebase64len = 0; - ret = atchops_rsakey_populate_privatekey(&privatekey, PRIVATE_KEY_BASE64, strlen(PRIVATE_KEY_BASE64)); + ret = atchops_rsa_key_populate_private_key(&privatekey, PRIVATE_KEY_BASE64, strlen(PRIVATE_KEY_BASE64)); if (ret != 0) { - printf("atchops_rsakey_populate_privatekey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_private_key (failed): %d\n", ret); goto exit; } @@ -87,9 +87,9 @@ int main() { goto exit; } - ret = atchops_rsakey_populate_publickey(&publickey, PUBLIC_KEY_BASE64, strlen(PUBLIC_KEY_BASE64)); + ret = atchops_rsa_key_populate_public_key(&publickey, PUBLIC_KEY_BASE64, strlen(PUBLIC_KEY_BASE64)); if (ret != 0) { - printf("atchops_rsakey_populate_publickey (failed): %d\n", ret); + printf("atchops_rsa_key_populate_public_key (failed): %d\n", ret); goto exit; } diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index 2707a75c..df8e58f8 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -33,16 +33,16 @@ */ typedef struct atclient_atkeys { char *pkam_publickey_base64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_publickey pkam_public_key; // contains n, e + atchops_rsa_key_public_key pkam_public_key; // contains n, e char *pkam_private_key_base64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_privatekey pkam_private_key; // conatins n, e, d, p, q + 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 - atchops_rsakey_publickey encrypt_public_key; // contains n, e + atchops_rsa_key_public_key encrypt_public_key; // contains n, e char *encrypt_private_key_base64; // base64 encoded, RSA-2048 key, decrypted - atchops_rsakey_privatekey encrypt_private_key; // conatins n, e, d, p, q + 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 diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 3319018e..91582a39 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -219,7 +219,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i challenge_len = recv_len - 5; // sign - if ((ret = atchops_rsa_sign(atkeys->pkam_private_key, ATCHOPS_MD_SHA256, (unsigned char *)challenge, challenge_len, + if ((ret = atchops_rsa_sign(&atkeys->pkam_private_key, ATCHOPS_MD_SHA256, (unsigned char *)challenge, challenge_len, signature)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_sign: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_selfkey.c index 27d4701e..fab4e2d9 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_selfkey.c @@ -3,7 +3,7 @@ #include "atclient/constants.h" #include "atclient/stringutils.h" #include -#include +#include #include #include #include @@ -159,9 +159,9 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, goto exit; } - if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw, value_raw_len, + if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw, value_raw_len, (unsigned char *)value, value_size, value_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_sharedkey.c index b47c0212..36a54e5f 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_sharedkey.c @@ -2,7 +2,7 @@ #include "atclient/encryption_key_helpers.h" #include "atclient/stringutils.h" #include -#include +#include #include #include #include @@ -229,14 +229,14 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at } memset(value_raw_encrypted, 0, sizeof(unsigned char) * value_raw_encrypted_size); size_t value_raw_encrypted_len = 0; - if ((ret = atchops_base64_decode(value_raw_encrypted_base64, value_raw_encrypted_base64_len, + if ((ret = atchops_base64_decode((unsigned char *) value_raw_encrypted_base64, value_raw_encrypted_base64_len, value_raw_encrypted, value_raw_encrypted_size, &value_raw_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - const size_t value_raw_size = atchops_aesctr_plaintext_size(value_raw_encrypted_len); + const size_t value_raw_size = atchops_aes_ctr_plaintext_size(value_raw_encrypted_len); if ((value_raw = malloc(sizeof(unsigned char) * value_raw_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); @@ -245,10 +245,10 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; - if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, + if ((ret = atchops_aes_ctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encrypted, value_raw_encrypted_len, value_raw, value_raw_size, &value_raw_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); goto exit; } @@ -439,7 +439,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at } // 9b. aes decrypt - const size_t value_raw_size = atchops_aesctr_plaintext_size(value_raw_encryted_len); + const size_t value_raw_size = atchops_aes_ctr_plaintext_size(value_raw_encryted_len); if ((value_raw = malloc(sizeof(unsigned char) * value_raw_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); @@ -447,10 +447,10 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at } memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; - if ((ret = atchops_aesctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encryted, + if ((ret = atchops_aes_ctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encryted, value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_decrypt: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 41290063..6b2f84fe 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -4,7 +4,7 @@ #include "atclient/encryption_key_helpers.h" #include "atclient/stringutils.h" #include "atlogger/atlogger.h" -#include +#include #include #include #include @@ -43,7 +43,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const char iv_base64[iv_base64_size]; memset(iv_base64, 0, sizeof(char) * iv_base64_size); - const size_t ciphertext_size = atchops_aesctr_ciphertext_size(value_len) + 1; + const size_t ciphertext_size = atchops_aes_ctr_ciphertext_size(value_len) + 1; unsigned char ciphertext[ciphertext_size]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertext_size); size_t ciphertext_len = 0; @@ -107,9 +107,9 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const goto exit; } - if ((ret = atchops_aesctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + if ((ret = atchops_aes_ctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, ciphertext, ciphertext_size, &ciphertext_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto exit; } @@ -161,9 +161,9 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const goto shared_key_exit; } - if ((ret = atchops_aesctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, ciphertext, ciphertext_size, &ciphertext_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto shared_key_exit; } diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 15dca08e..99527243 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -1,10 +1,10 @@ #include "atclient/atkeys.h" #include "atlogger/atlogger.h" -#include +#include #include #include #include -#include +#include #include #include #include @@ -42,17 +42,17 @@ static int set_self_encryption_key_base64(atclient_atkeys *atkeys, const char *s void atclient_atkeys_init(atclient_atkeys *atkeys) { memset(atkeys, 0, sizeof(atclient_atkeys)); - atchops_rsakey_publickey_init(&(atkeys->pkam_public_key)); - atchops_rsakey_privatekey_init(&(atkeys->pkam_private_key)); - atchops_rsakey_publickey_init(&(atkeys->encrypt_public_key)); - atchops_rsakey_privatekey_init(&(atkeys->encrypt_private_key)); + atchops_rsa_key_public_key_init(&(atkeys->pkam_public_key)); + atchops_rsa_key_private_key_init(&(atkeys->pkam_private_key)); + atchops_rsa_key_public_key_init(&(atkeys->encrypt_public_key)); + atchops_rsa_key_private_key_init(&(atkeys->encrypt_private_key)); } void atclient_atkeys_free(atclient_atkeys *atkeys) { - atchops_rsakey_publickey_free(&(atkeys->pkam_public_key)); - atchops_rsakey_privatekey_free(&(atkeys->pkam_private_key)); - atchops_rsakey_publickey_free(&(atkeys->encrypt_public_key)); - atchops_rsakey_privatekey_free(&(atkeys->encrypt_private_key)); + atchops_rsa_key_public_key_free(&(atkeys->pkam_public_key)); + 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)); } int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, @@ -242,10 +242,10 @@ int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char return ret; } - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkam_public_key), pkam_publickey_base64, pkam_publickey_base64)) != + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), pkam_publickey_base64, pkam_publickey_base64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate pkam_public_key\n", ret); + "atchops_rsa_key_populate_public_key: %d | failed to populate pkam_public_key\n", ret); goto exit; } ret = 0; @@ -275,10 +275,10 @@ int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const cha return ret; } - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkam_private_key), pkam_private_key_base64, + if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->pkam_private_key), pkam_private_key_base64, pkam_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate pkam_private_key\n", ret); + "atchops_rsa_key_populate_private_key: %d | failed to populate pkam_private_key\n", ret); goto exit; } exit: { return ret; } @@ -306,10 +306,10 @@ int atclient_atkeys_populate_encrypt_public_key(atclient_atkeys *atkeys, const c return ret; } - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encrypt_public_key), encrypt_public_key_base64, + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->encrypt_public_key), encrypt_public_key_base64, encrypt_public_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate encrypt_public_key\n", ret); + "atchops_rsa_key_populate_public_key: %d | failed to populate encrypt_public_key\n", ret); goto exit; } @@ -340,10 +340,10 @@ int atclient_atkeys_populate_encrypt_private_key(atclient_atkeys *atkeys, const return ret; } - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encrypt_private_key), encrypt_private_key_base64, + if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->encrypt_private_key), encrypt_private_key_base64, encrypt_private_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate encrypt_private_key\n", ret); + "atchops_rsa_key_populate_private_key: %d | failed to populate encrypt_private_key\n", ret); goto exit; } @@ -506,9 +506,9 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a goto exit; } - if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, + 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_aesctr_decrypt: %d | failed to decrypt pkam public key\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt pkam public key\n", ret); goto exit; } @@ -530,9 +530,9 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a goto exit; } - if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, + 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_aesctr_decrypt: %d | failed to decrypt pkam private key\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d | failed to decrypt pkam private key\n", ret); goto exit; } @@ -554,10 +554,10 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a goto exit; } - if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, + 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_aesctr_decrypt: %d | failed to decrypt encrypt public key\n", ret); + "atchops_aes_ctr_decrypt: %d | failed to decrypt encrypt public key\n", ret); goto exit; } @@ -578,10 +578,10 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a goto exit; } - if ((ret = atchops_aesctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, rsa_key_encrypted, rsa_key_encrypted_len, + 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_aesctr_decrypt: %d | failed to decrypt encrypt private key\n", ret); + "atchops_aes_ctr_decrypt: %d | failed to decrypt encrypt private key\n", ret); goto exit; } @@ -596,34 +596,34 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a */ // 5a. pkam public key - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->pkam_public_key), atkeys->pkam_publickey_base64, + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), atkeys->pkam_publickey_base64, strlen(atkeys->pkam_publickey_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate pkam public key\n", ret); + "atchops_rsa_key_populate_public_key: %d | failed to populate pkam public key\n", ret); goto exit; } // 5b. pkam private key - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->pkam_private_key), atkeys->pkam_private_key_base64, + if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->pkam_private_key), atkeys->pkam_private_key_base64, strlen(atkeys->pkam_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate pkam private key\n", ret); + "atchops_rsa_key_populate_private_key: %d | failed to populate pkam private key\n", ret); goto exit; } // 5c. encrypt public key - if ((ret = atchops_rsakey_populate_privatekey(&(atkeys->encrypt_private_key), atkeys->encrypt_private_key_base64, + if ((ret = atchops_rsa_key_populate_private_key(&(atkeys->encrypt_private_key), atkeys->encrypt_private_key_base64, strlen(atkeys->encrypt_private_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_privatekey: %d | failed to populate encrypt private key\n", ret); + "atchops_rsa_key_populate_private_key: %d | failed to populate encrypt private key\n", ret); goto exit; } // 5d. encrypt private key - if ((ret = atchops_rsakey_populate_publickey(&(atkeys->encrypt_public_key), atkeys->encrypt_public_key_base64, + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->encrypt_public_key), atkeys->encrypt_public_key_base64, strlen(atkeys->encrypt_public_key_base64))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atchops_rsakey_populate_publickey: %d | failed to populate encrypt public key\n", ret); + "atchops_rsa_key_populate_public_key: %d | failed to populate encrypt public key\n", ret); goto exit; } diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index 9b9ce210..88d1b6cc 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -216,7 +216,7 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r goto exit; } - if ((ret = atchops_rsa_decrypt(ctx->atkeys.encrypt_private_key, key_raw_encrypted, key_raw_encrypted_len, + if ((ret = atchops_rsa_decrypt(&ctx->atkeys.encrypt_private_key, key_raw_encrypted, key_raw_encrypted_len, key_raw_decrypted, key_raw_decrypted_size, &key_raw_decrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); goto exit; @@ -333,7 +333,7 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char memset(shared_encryption_key_encrypted, 0, sizeof(unsigned char) * shared_encryption_key_encrypted_size); size_t shared_encryption_key_encrypted_len = 0; - if ((ret = atchops_rsa_decrypt(ctx->atkeys.encrypt_private_key, shared_encryption_key_encrypted_base64, + if ((ret = atchops_rsa_decrypt(&ctx->atkeys.encrypt_private_key, shared_encryption_key_encrypted_base64, shared_encryption_key_encrypted_base64_len, shared_encryption_key_encrypted, shared_encryption_key_encrypted_size, &shared_encryption_key_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d\n", ret); @@ -393,8 +393,8 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( char *public_key_base64 = NULL; // holds their public key in base64 format, non-encrypted (struct) - atchops_rsakey_publickey public_key_struct; - atchops_rsakey_publickey_init(&public_key_struct); + atchops_rsa_key_public_key public_key_struct; + atchops_rsa_key_public_key_init(&public_key_struct); // the original AES-256 key const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; @@ -405,7 +405,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( unsigned char shared_encryption_key_base64[shared_encryption_key_base64_size]; // encrypted for us - const size_t shared_encryption_key_base64_encrypted_for_us_size = 512; + const size_t shared_encryption_key_base64_encrypted_for_us_size = 256; // rsa encryption always outputs 256 bytes (2048 bit key) TODO: constant unsigned char shared_encryption_key_base64_encrypted_for_us[shared_encryption_key_base64_encrypted_for_us_size]; // encrypted for us (base64 encoded) @@ -415,7 +415,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( shared_encryption_key_base64_encrypted_for_us_base64[shared_encryption_key_base64_encrypted_for_us_base64_size]; // encrypted for them - const size_t shared_encryption_key_base64_encrypted_for_them_size = 512; + const size_t shared_encryption_key_base64_encrypted_for_them_size = 256; // rsa encryption always outputs 256 bytes (2048 bit key) TODO: constant unsigned char shared_encryption_key_base64_encrypted_for_them[shared_encryption_key_base64_encrypted_for_them_size]; // encrypted for them (base64 encoded) @@ -455,8 +455,8 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( goto exit; } - if ((ret = atchops_rsakey_populate_publickey(&public_key_struct, public_key_base64, strlen(public_key_base64))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsakey_populate_publickey: %d\n", ret); + if ((ret = atchops_rsa_key_populate_public_key(&public_key_struct, public_key_base64, strlen(public_key_base64))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_key_populate_public_key: %d\n", ret); goto exit; } @@ -484,13 +484,10 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( */ // 5a. Encrypt for us - size_t shared_encryption_key_base64_encrypted_for_us_len = 0; memset(shared_encryption_key_base64_encrypted_for_us, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_us_size); - if ((ret = atchops_rsa_encrypt(atclient->atkeys.encrypt_public_key, (unsigned char *)shared_encryption_key_base64, - shared_encryption_key_base64_len, shared_encryption_key_base64_encrypted_for_us, - shared_encryption_key_base64_encrypted_for_us_size, - &shared_encryption_key_base64_encrypted_for_us_len)) != 0) { + if ((ret = atchops_rsa_encrypt(&atclient->atkeys.encrypt_public_key, (unsigned char *)shared_encryption_key_base64, + shared_encryption_key_base64_len, shared_encryption_key_base64_encrypted_for_us)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "failed to encrypt shared enc key for us | atchops_rsa_encrypt: %d\n", ret); goto exit; @@ -500,7 +497,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( memset(shared_encryption_key_base64_encrypted_for_us_base64, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_us_base64_size); if ((ret = atchops_base64_encode(shared_encryption_key_base64_encrypted_for_us, - shared_encryption_key_base64_encrypted_for_us_len, + shared_encryption_key_base64_encrypted_for_us_size, shared_encryption_key_base64_encrypted_for_us_base64, shared_encryption_key_base64_encrypted_for_us_base64_size, &shared_encryption_key_base64_encrypted_for_us_base64_len)) != 0) { @@ -510,13 +507,10 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( } // 5b. Encrypt for them - size_t shared_encryption_key_base64_encrypted_for_them_len = 0; memset(shared_encryption_key_base64_encrypted_for_them, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_them_size); - if ((ret = atchops_rsa_encrypt(public_key_struct, shared_encryption_key_base64, shared_encryption_key_base64_len, - shared_encryption_key_base64_encrypted_for_them, - shared_encryption_key_base64_encrypted_for_them_size, - &shared_encryption_key_base64_encrypted_for_them_len)) != 0) { + if ((ret = atchops_rsa_encrypt(&public_key_struct, shared_encryption_key_base64, shared_encryption_key_base64_len, + shared_encryption_key_base64_encrypted_for_them)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_encrypt: %d\n", ret); goto exit; } @@ -525,7 +519,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( memset(shared_encryption_key_base64_encrypted_for_them_base64, 0, sizeof(unsigned char) * shared_encryption_key_base64_encrypted_for_them_base64_size); if ((ret = atchops_base64_encode(shared_encryption_key_base64_encrypted_for_them, - shared_encryption_key_base64_encrypted_for_them_len, + shared_encryption_key_base64_encrypted_for_them_size, shared_encryption_key_base64_encrypted_for_them_base64, shared_encryption_key_base64_encrypted_for_them_base64_size, &shared_encryption_key_base64_encrypted_for_them_base64_len)) != 0) { @@ -606,7 +600,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( exit: { free(sharedby_atsign_with_at); free(sharedwith_atsign_with_at); - atchops_rsakey_publickey_free(&public_key_struct); + atchops_rsa_key_public_key_free(&public_key_struct); free(public_key_base64); free(update_cmd_for_us); free(update_cmd_for_them); diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index ace03cae..9ffd0104 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -7,7 +7,7 @@ #include "atclient/stringutils.h" #include "cJSON.h" #include -#include +#include #include #include #include @@ -599,7 +599,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not memset(decryptedvaluetemp, 0, sizeof(unsigned char) * decryptedvaluetempsize); size_t decryptedvaluetemplen = 0; - ret = atchops_aesctr_decrypt(sharedenckey, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, decryptedvaluetemp, + ret = atchops_aes_ctr_decrypt(sharedenckey, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, decryptedvaluetemp, decryptedvaluetempsize, &decryptedvaluetemplen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to decrypt value\n"); diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 0933fd17..1d430693 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -4,7 +4,7 @@ #include "atclient/encryption_key_helpers.h" #include "atclient/stringutils.h" #include -#include +#include #include #include #include @@ -107,10 +107,10 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi return ret; } - ret = atchops_aesctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)params->value, + ret = atchops_aes_ctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)params->value, strlen(params->value), ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aesctr_encrypt failed with code %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt failed with code %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 e11f04e9..12418942 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -1,7 +1,7 @@ #include "functional_tests/config.h" #include "functional_tests/helpers.h" #include -#include +#include #include #include #include diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index ec038698..b2e38a2d 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -1,7 +1,7 @@ #include "functional_tests/config.h" #include "functional_tests/helpers.h" #include -#include +#include #include #include #include From 14bbd8949550eae90a436a1d29ea3f52f78140ba Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 24 Jul 2024 11:01:12 -0400 Subject: [PATCH 125/193] fix: --- packages/atchops/src/rsa.c | 2 +- packages/atchops/tests/test_rsadecrypt.c | 2 +- packages/atchops/tests/test_rsaencrypt.c | 5 ++--- packages/atchops/tests/test_rsasign.c | 2 +- packages/atchops/tests/test_rsaverify.c | 4 ++-- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/atchops/src/rsa.c b/packages/atchops/src/rsa.c index 9c6dcff5..2d2e31a3 100644 --- a/packages/atchops/src/rsa.c +++ b/packages/atchops/src/rsa.c @@ -52,7 +52,7 @@ int atchops_rsa_sign(const atchops_rsa_key_private_key *private_key, const atcho size_t hashsize; if (md_type == ATCHOPS_MD_SHA256) { - hashsize = 32; + hashsize = 32; // TODO: constant } unsigned char hash[hashsize]; diff --git a/packages/atchops/tests/test_rsadecrypt.c b/packages/atchops/tests/test_rsadecrypt.c index 2d428bc1..ba95b16d 100644 --- a/packages/atchops/tests/test_rsadecrypt.c +++ b/packages/atchops/tests/test_rsadecrypt.c @@ -58,7 +58,7 @@ int main() { } printf("atchops_rsa_key_populate_private_key (success): %d\n", ret); - ret = atchops_rsa_decrypt(privatekey, (const unsigned char *)ciphertext, ciphertextlen, plaintext, plaintextsize, + ret = atchops_rsa_decrypt(&privatekey, (const unsigned char *)ciphertext, ciphertextlen, plaintext, plaintextsize, &plaintextlen); if (ret != 0) { printf("atchops_rsa_decrypt (failed): %d\n", ret); diff --git a/packages/atchops/tests/test_rsaencrypt.c b/packages/atchops/tests/test_rsaencrypt.c index 01caf7fa..7d6e6476 100644 --- a/packages/atchops/tests/test_rsaencrypt.c +++ b/packages/atchops/tests/test_rsaencrypt.c @@ -25,7 +25,7 @@ int main() { const char *plaintext = PLAINTEXT; const size_t plaintextlen = strlen(plaintext); - const size_t ciphertextsize = 1024; + const size_t ciphertextsize = 256; unsigned char ciphertext[ciphertextsize]; memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize); size_t ciphertextlen = 0; @@ -40,8 +40,7 @@ int main() { } printf("atchops_rsa_key_populate_public_key (success): %d\n", ret); - ret = atchops_rsa_encrypt(publickey, (const unsigned char *)plaintext, plaintextlen, ciphertext, ciphertextsize, - &ciphertextlen); + ret = atchops_rsa_encrypt(&publickey, (const unsigned char *)plaintext, plaintextlen, ciphertext); if (ret != 0) { printf("atchops_rsa_encrypt (failed): %d\n", ret); goto ret; diff --git a/packages/atchops/tests/test_rsasign.c b/packages/atchops/tests/test_rsasign.c index 997f25ec..5558733a 100644 --- a/packages/atchops/tests/test_rsasign.c +++ b/packages/atchops/tests/test_rsasign.c @@ -64,7 +64,7 @@ int main() { } printf("atchops_rsa_key_populate_privatekey (success): %d\n", ret); - ret = atchops_rsa_sign(privatekey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); + ret = atchops_rsa_sign(&privatekey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); if (ret != 0) { printf("atchops_rsa_sign (failed): %d\n", ret); goto exit; diff --git a/packages/atchops/tests/test_rsaverify.c b/packages/atchops/tests/test_rsaverify.c index 514b7aa2..803c3b9b 100644 --- a/packages/atchops/tests/test_rsaverify.c +++ b/packages/atchops/tests/test_rsaverify.c @@ -81,7 +81,7 @@ int main() { goto exit; } - ret = atchops_rsa_sign(privatekey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); + ret = atchops_rsa_sign(&privatekey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); if (ret != 0) { printf("atchops_rsa_sign (failed): %d\n", ret); goto exit; @@ -93,7 +93,7 @@ int main() { goto exit; } - ret = atchops_rsa_verify(publickey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); + ret = atchops_rsa_verify(&publickey, ATCHOPS_MD_SHA256, (const unsigned char *)message, messagelen, signature); if (ret != 0) { printf("atchops_rsa_verify (failed): %d\n", ret); goto exit; From 102cb88b40740344f533d00643b08c4acfaaaef2 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Wed, 24 Jul 2024 11:07:28 -0400 Subject: [PATCH 126/193] chore: delete atsign.c/.h --- examples/desktop/at_talk/src/main.c | 1 - examples/desktop/crud/delete.c | 1 - examples/desktop/crud/get_publickey.c | 1 - examples/desktop/crud/get_selfkey.c | 1 - examples/desktop/crud/get_sharedkey.c | 1 - examples/desktop/crud/put_publickey.c | 1 - examples/desktop/crud/put_selfkey.c | 1 - examples/desktop/crud/put_sharedkey.c | 1 - examples/desktop/events/monitor.c | 1 - examples/desktop/events/notify.c | 1 - examples/desktop/events/resilient_monitor.c | 1 - packages/atclient/CMakeLists.txt | 1 - packages/atclient/include/atclient/atclient.h | 1 - packages/atclient/include/atclient/atsign.h | 45 -------- packages/atclient/src/atclient.c | 1 - packages/atclient/src/atkey.c | 1 - packages/atclient/src/atsign.c | 107 ------------------ 17 files changed, 167 deletions(-) delete mode 100644 packages/atclient/include/atclient/atsign.h delete mode 100644 packages/atclient/src/atsign.c diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 8b7429be..aa7a51ad 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index a7f8d8ad..f1da16a2 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index ff125fcd..33ba4ab9 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index 18e8b122..d6d3feff 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 319955a1..1c8fa86a 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -1,7 +1,6 @@ #include "atchops/sha.h" #include #include -#include #include #include #include diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index 95e3b2b6..d2f4927f 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index 92a56a2e..d4b112f6 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index f1c78221..8c8c7bc7 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 4d316396..728e42b1 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/examples/desktop/events/notify.c b/examples/desktop/events/notify.c index 08fdaec9..ea609694 100644 --- a/examples/desktop/events/notify.c +++ b/examples/desktop/events/notify.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index dcdf6c32..31887631 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 8fb05201..23e22222 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -17,7 +17,6 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c ${CMAKE_CURRENT_LIST_DIR}/src/atnotification.c - ${CMAKE_CURRENT_LIST_DIR}/src/atsign.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index aafa2493..b723c153 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -3,7 +3,6 @@ #include "atclient/atkey.h" #include "atclient/atkeys.h" -#include "atclient/atsign.h" #include "atclient/connection.h" #include #include diff --git a/packages/atclient/include/atclient/atsign.h b/packages/atclient/include/atclient/atsign.h deleted file mode 100644 index d600c765..00000000 --- a/packages/atclient/include/atclient/atsign.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef ATCLIENT_ATSIGN_H -#define ATCLIENT_ATSIGN_H - -#include - -// Structure to represent the AtSign class -typedef struct atclient_atsign { - char *atsign; - char *without_prefix_str; -} atclient_atsign; - -// Function to initialize an AtSign object -int atclient_atsign_init(atclient_atsign *atsign, const char *atsign_str); - -// Function to free the memory used by an AtSign object -void atclient_atsign_free(atclient_atsign *atsign); - -/** - * @brief populates *atsign and *atsignlen with the atsign without the prefixed `@` symbol . Calling this function will - * guarantee that *atsign is always without a prefixed `@` symbol, whether if it started with one or not. - * - * @param atsign the atsign to populate (there will be no prefixed `@` symbol. example: @bob -> bob) - * @param atsignsize the buffer size - * @param atsignlen the actually written size of the atsign - * @param originalatsign the atsign that supposedly has the prefixed `@` symbol you would like to remove - * @param originalatsignlen the string length of the atsign being read - * @return int 0 on success, 1 on if atsignsize is not large enough, 2 on if the originalatsignlen length passed is <= 0. - */ -int atclient_atsign_without_at_symbol(char *atsign, const size_t atsignsize, size_t *atsignlen, - const char *originalatsign, const size_t originalatsignlen); - -/** - * @brief populates *atsign and *atsignlen with the atsign with the prefixed `@` symbol. Calling this function will - * guarantee that *atsign is always prefixed with '@' symbol, whether it started with one or not. - * @param atsign the atsign buffer to populate - * @param atsignsize the buffer size - * @param atsignlen the actually written size of the atsign - * @param originalatsign the atsign that supposedly has the prefixed `@` symbol you would like to remove - * @param originalatsignlen the string length of the atsign being read - * @return int 0 on success - */ -int atclient_atsign_with_at_symbol(char *atsign, const size_t atsignsize, size_t *atsignlen, - const char *originalatsign, const size_t originalatsignlen); - -#endif diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 91582a39..53baa3af 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -4,7 +4,6 @@ #include "atclient/atclient.h" #include "atclient/atclient_utils.h" #include "atclient/atkeys.h" -#include "atclient/atsign.h" #include "atclient/connection.h" #include "atclient/constants.h" #include "atclient/stringutils.h" diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 066c982e..5afb7151 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -1,5 +1,4 @@ #include "atclient/atkey.h" -#include "atclient/atsign.h" #include "atclient/constants.h" #include "atclient/metadata.h" #include "atclient/stringutils.h" diff --git a/packages/atclient/src/atsign.c b/packages/atclient/src/atsign.c deleted file mode 100644 index 10a34545..00000000 --- a/packages/atclient/src/atsign.c +++ /dev/null @@ -1,107 +0,0 @@ -#include "atclient/atsign.h" -#include "atclient/constants.h" -#include "atlogger/atlogger.h" -#include -#include -#include -#include - -#define TAG "atsign" - -int atclient_atsign_init(atclient_atsign *atsign, const char *atsign_str) { - int ret = 0; - - const size_t maxatlen = ATCLIENT_ATSIGN_FULL_LEN + 1; - // atsign_str is longer than expected or null/empty - if ((strlen(atsign_str) > maxatlen) || (atsign_str == NULL) || (strlen(atsign_str) == 0)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_init: %d\n", ret); - return ret; - } - - memset(atsign, 0, sizeof(atclient_atsign)); - atsign->atsign = malloc(strlen(atsign_str) + 1); - if(atsign->atsign == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for atsign->atsign\n"); - return ret; - } - - size_t atolen = 0; - ret = atclient_atsign_with_at_symbol(atsign->atsign, maxatlen, &(atolen), atsign_str, strlen(atsign_str)); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atsign_with_at_symbol failed\n"); - return ret; - } - - atsign->without_prefix_str = atsign->atsign + 1; - return ret; -} - -void atclient_atsign_free(atclient_atsign *atsign) { - free(atsign->atsign); - memset(atsign, 0, sizeof(atclient_atsign)); -} - -int atclient_atsign_without_at_symbol(char *atsign, const size_t atsignlen, size_t *atsignolen, - const char *originalatsign, const size_t originalatsignlen) { - int ret = 1; - if (atsignlen + 1 < originalatsignlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atsignlen might be too low. consider allocating more buffer space. atsignlen: %d\n", atsignlen); - ret = 1; - goto exit; - } - - if (originalatsignlen <= 0) { - ret = 2; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "originalatsignlen is <= 0: %lu\n", originalatsignlen); - goto exit; - } - - if (originalatsign[0] != '@') { - // it did not begin with an `@` to begin with - ret = 0; - goto exit; - } - - strncpy(atsign, originalatsign + 1, originalatsignlen - 1); - atsign[originalatsignlen - 1] = '\0'; - *atsignolen = originalatsignlen - 1; - ret = 0; - goto exit; -exit: { return ret; } -} - -int atclient_atsign_with_at_symbol(char *atsign, const size_t atsignlen, size_t *atsignolen, const char *originalatsign, - const size_t originalatsignlen) { - int ret = 1; - if (atsignlen + 1 < originalatsignlen) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atsignlen might be too low. consider allocating more buffer space. atsignlen: %d\n", atsignlen); - ret = 1; - goto exit; - } - - if (originalatsignlen <= 0) { - ret = 2; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "originalatsignlen is <= 0: %lu\n", originalatsignlen); - goto exit; - } - - if (originalatsign[0] == '@') { - // it already began with an x@x - strncpy(atsign, originalatsign, originalatsignlen + 1); - atsign[originalatsignlen] = '\0'; - ret = 0; - goto exit; - } - - atsign[0] = '@'; - strncpy(atsign + 1, originalatsign, originalatsignlen + 1); - atsign[originalatsignlen + 1] = '\0'; - *atsignolen = originalatsignlen + 1; - ret = 0; - goto exit; -exit: { return ret; } -} From d94fe6539b173bcfe2e6654e6bfe88ade8022d2b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 09:09:34 -0400 Subject: [PATCH 127/193] feat: request_options.c/.h --- packages/atclient/CMakeLists.txt | 1 + .../include/atclient/request_options.h | 198 ++++ packages/atclient/src/request_options.c | 970 ++++++++++++++++++ 3 files changed, 1169 insertions(+) create mode 100644 packages/atclient/include/atclient/request_options.h create mode 100644 packages/atclient/src/request_options.c diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 23e22222..f36e351b 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -23,6 +23,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/monitor.c ${CMAKE_CURRENT_LIST_DIR}/src/notify.c ${CMAKE_CURRENT_LIST_DIR}/src/notify_params.c + ${CMAKE_CURRENT_LIST_DIR}/src/request_options.c ${CMAKE_CURRENT_LIST_DIR}/src/stringutils.c ) diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h new file mode 100644 index 00000000..422b28ab --- /dev/null +++ b/packages/atclient/include/atclient/request_options.h @@ -0,0 +1,198 @@ +#ifndef ATCLIENT_REQUEST_OPTIONS_H +#define ATCLIENT_REQUEST_OPTIONS_H + +#include +#include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) + +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 1) + +#define ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 +#define ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 0) + +#define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 +#define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) + +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) + +#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 +#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 0) + +#define ATCLIENT_DELETE_REQUEST_OPTIONS_INDEX 0 +#define ATCLIENT_DELETE_REQUEST_OPTIONS_INITIALIZED (VALUE_INITIALIZED << 0) + +/* + * 1A. Put SelfKey + */ +typedef struct atclient_put_self_key_request_options { + unsigned char *shared_encryption_key; + uint8_t _initialized_fields[1]; +} atclient_put_self_key_request_options; + +/* + * 1B. Put SharedKey + */ +typedef struct atclient_put_shared_key_request_options { + unsigned char *shared_encryption_key; + bool bypass_cache; + uint8_t _initialized_fields[1]; +} atclient_put_shared_key_request_options; + +/* + * 1C. Put PublicKey + */ +typedef struct atclient_put_public_key_request_options { + bool bypass_cache; + uint8_t _initialized_fields[1]; +} atclient_put_public_key_request_options; + +/* + * 2A. Get SelfKey + */ +typedef struct atclient_get_self_key_request_options { + unsigned char *shared_encryption_key; + bool store_atkey_metadata; + uint8_t _initialized_fields[1]; +} atclient_get_self_key_request_options; + +/* + * 2B. Get SharedKey + */ +typedef struct atclient_get_shared_key_request_options { + unsigned char *shared_encryption_key; + bool store_atkey_metadata; + uint8_t _initialized_fields[1]; +} atclient_get_shared_key_request_options; + +/* + * 2C. Get PublicKey + */ +typedef struct atclient_get_public_key_request_options { + bool store_atkey_metadata; + uint8_t _initialized_fields[1]; +} atclient_get_public_key_request_options; + +/* + * 3. Delete + */ +typedef struct atclient_delete_request_options { + // empty + // future proofing + uint8_t _initialized_fields[1]; +} atclient_delete_request_options; + +/* + * 1A. Put SelfKey + */ +void atclient_put_self_key_request_options_init(atclient_put_self_key_request_options *options); +void atclient_put_self_key_request_options_free(atclient_put_self_key_request_options *options); + +bool atclient_put_self_key_request_options_is_shared_encryption_key_initialized( + const atclient_put_self_key_request_options *options); +void atclient_put_self_key_request_options_set_shared_encryption_key_initialized( + atclient_put_self_key_request_options *options, const bool initialized); +int atclient_put_self_key_request_options_set_shared_encryption_key(atclient_put_self_key_request_options *options, + const unsigned char *shared_encryption_key); +void atclient_put_self_key_request_options_unset_shared_encryption_key(atclient_put_self_key_request_options *options); + +/* + * 1B. Put SharedKey + */ +void atclient_put_shared_key_request_options_init(atclient_put_shared_key_request_options *options); +void atclient_put_shared_key_request_options_free(atclient_put_shared_key_request_options *options); + +bool atclient_put_shared_key_request_options_is_shared_encryption_key_initialized( + const atclient_put_shared_key_request_options *options); +void atclient_put_shared_key_request_options_set_shared_encryption_key_initialized( + atclient_put_shared_key_request_options *options, const bool initialized); +int atclient_put_shared_key_request_options_set_shared_encryption_key(atclient_put_shared_key_request_options *options, + const unsigned char *shared_encryption_key); +void atclient_put_shared_key_request_options_unset_shared_encryption_key( + atclient_put_shared_key_request_options *options); + +bool atclient_put_shared_key_request_options_is_bypass_cache_initialized( + const atclient_put_shared_key_request_options *options); +void atclient_put_shared_key_request_options_set_bypass_cache_initialized( + atclient_put_shared_key_request_options *options, const bool initialized); +int atclient_put_shared_key_request_options_set_bypass_cache(atclient_put_shared_key_request_options *options, + const bool bypass_cache); +void atclient_put_shared_key_request_options_unset_bypass_cache(atclient_put_shared_key_request_options *options); + +/* + * 1C. Put PublicKey + */ +void atclient_put_public_key_request_options_init(atclient_put_public_key_request_options *options); +void atclient_put_public_key_request_options_free(atclient_put_public_key_request_options *options); + +bool atclient_put_public_key_request_options_is_bypass_cache_initialized( + const atclient_put_public_key_request_options *options); +void atclient_put_public_key_request_options_set_bypass_cache_initialized( + atclient_put_public_key_request_options *options, const bool initialized); +int atclient_put_public_key_request_options_set_bypass_cache(atclient_put_public_key_request_options *options, + const bool bypass_cache); +void atclient_put_public_key_request_options_unset_bypass_cache(atclient_put_public_key_request_options *options); + +/* + * 2A. Get SelfKey + */ +void atclient_get_self_key_request_options_init(atclient_get_self_key_request_options *options); +void atclient_get_self_key_request_options_free(atclient_get_self_key_request_options *options); + +bool atclient_get_self_key_request_options_is_shared_encryption_key_initialized( + const atclient_get_self_key_request_options *options); +void atclient_get_self_key_request_options_set_shared_encryption_key_initialized( + atclient_get_self_key_request_options *options, const bool initialized); +int atclient_get_self_key_request_options_set_shared_encryption_key(atclient_get_self_key_request_options *options, + const unsigned char *shared_encryption_key); +void atclient_get_self_key_request_options_unset_shared_encryption_key(atclient_get_self_key_request_options *options); + +/* + * 2B. Get SharedKey + */ +void atclient_get_shared_key_request_options_init(atclient_get_shared_key_request_options *options); +void atclient_get_shared_key_request_options_free(atclient_get_shared_key_request_options *options); + +bool atclient_get_shared_key_request_options_is_shared_encryption_key_initialized( + const atclient_get_shared_key_request_options *options); +void atclient_get_shared_key_request_options_set_shared_encryption_key_initialized( + atclient_get_shared_key_request_options *options, const bool initialized); +int atclient_get_shared_key_request_options_set_shared_encryption_key(atclient_get_shared_key_request_options *options, + const unsigned char *shared_encryption_key); +void atclient_get_shared_key_request_options_unset_shared_encryption_key( + atclient_get_shared_key_request_options *options); + +/* + * 2C. Get PublicKey + */ +void atclient_get_public_key_request_options_init(atclient_get_public_key_request_options *options); +void atclient_get_public_key_request_options_free(atclient_get_public_key_request_options *options); + +bool atclient_get_public_key_request_options_is_store_atkey_metadata_initialized( + const atclient_get_public_key_request_options *options); +void atclient_get_public_key_request_options_set_store_atkey_metadata_initialized( + atclient_get_public_key_request_options *options, const bool initialized); +int atclient_get_public_key_request_options_set_store_atkey_metadata(atclient_get_public_key_request_options *options, + const bool store_atkey_metadata); +void atclient_get_public_key_request_options_unset_store_atkey_metadata( + atclient_get_public_key_request_options *options); + +/* + * 3. Delete + */ +void atclient_delete_request_options_init(atclient_delete_request_options *options); +void atclient_delete_request_options_free(atclient_delete_request_options *options); + +#endif diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c new file mode 100644 index 00000000..0d9606a8 --- /dev/null +++ b/packages/atclient/src/request_options.c @@ -0,0 +1,970 @@ +#include +#include +#include +#include +#include +#include + +#define TAG "request_options" + +/* + * ================= + * 1A. Put SelfKey + * ================= + */ +void atclient_put_self_key_request_options_init(atclient_put_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_self_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_put_self_key_request_options)); +} + +void atclient_put_self_key_request_options_free(atclient_put_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_self_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_put_self_key_request_options_unset_shared_encryption_key(options); + } +} + +bool atclient_put_self_key_request_options_is_shared_encryption_key_initialized( + const atclient_put_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_self_key_request_options_is_shared_encryption_key_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the shared encryption key is initialized + */ + return options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & + ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; +} +void atclient_put_self_key_request_options_set_shared_encryption_key_initialized( + atclient_put_self_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_self_key_request_options_set_shared_encryption_key_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the shared encryption key initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } +} + +int atclient_put_self_key_request_options_set_shared_encryption_key(atclient_put_self_key_request_options *options, + const unsigned char *shared_encryption_key) { + /* + * 1. Validate arguments + */ + int ret = 1; + if (options == NULL || shared_encryption_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_self_key_request_options_set_shared_encryption_key: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the shared encryption key, if necessary + */ + if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_put_self_key_request_options_unset_shared_encryption_key(options); + } + + /* + * 3. Set the shared encryption key + */ + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_self_key_request_options_set_shared_encryption_key: " + "Failed to allocate memory for shared encryption key\n"); + goto exit; + } + + atclient_put_self_key_request_options_set_shared_encryption_key_initialized(options, true); + memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_put_self_key_request_options_unset_shared_encryption_key(atclient_put_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_self_key_request_options_unset_shared_encryption_key: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the shared encryption key + */ + if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { + free(options->shared_encryption_key); + } + options->shared_encryption_key = NULL; + atclient_put_self_key_request_options_set_shared_encryption_key_initialized(options, false); +} + +/* + * ================= + * 1B. Put SharedKey + * ================= + */ + +void atclient_put_shared_key_request_options_init(atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_put_shared_key_request_options)); +} + +void atclient_put_shared_key_request_options_free(atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_put_shared_key_request_options_unset_shared_encryption_key(options); + } + + if (atclient_put_shared_key_request_options_is_bypass_cache_initialized(options)) { + atclient_put_shared_key_request_options_unset_bypass_cache(options); + } +} + +bool atclient_put_shared_key_request_options_is_shared_encryption_key_initialized( + const atclient_put_shared_key_request_options *options) { + + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_is_shared_encryption_key_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the shared encryption key is initialized + */ + return options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; +} + +void atclient_put_shared_key_request_options_set_shared_encryption_key_initialized( + atclient_put_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_shared_encryption_key_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the shared encryption key initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } +} +int atclient_put_shared_key_request_options_set_shared_encryption_key(atclient_put_shared_key_request_options *options, + const unsigned char *shared_encryption_key) { + /* + * 1. Validate arguments + */ + int ret = 1; + + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_shared_encryption_key: " + "Invalid arguments\n"); + goto exit; + } + + if (shared_encryption_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_shared_encryption_key: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the shared encryption key, if necessary + */ + if (atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_put_shared_key_request_options_unset_shared_encryption_key(options); + } + + /* + * 3. Set the shared encryption key + */ + + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_shared_encryption_key: " + "Failed to allocate memory for shared encryption key\n"); + goto exit; + } + + atclient_put_shared_key_request_options_set_shared_encryption_key_initialized(options, true); + memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_put_shared_key_request_options_unset_shared_encryption_key( + atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_unset_shared_encryption_key: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the shared encryption key + */ + if (atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + free(options->shared_encryption_key); + } + options->shared_encryption_key = NULL; + atclient_put_shared_key_request_options_set_shared_encryption_key_initialized(options, false); +} + +bool atclient_put_shared_key_request_options_is_bypass_cache_initialized( + const atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_is_bypass_cache_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the bypass cache is initialized + */ + return options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; +} + +void atclient_put_shared_key_request_options_set_bypass_cache_initialized( + atclient_put_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_bypass_cache_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the bypass cache initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= + ~ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } +} + +int atclient_put_shared_key_request_options_set_bypass_cache(atclient_put_shared_key_request_options *options, + const bool bypass_cache) { + int ret = 1; + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_bypass_cache: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the bypass cache, if necessary + */ + if (atclient_put_shared_key_request_options_is_bypass_cache_initialized(options)) { + atclient_put_shared_key_request_options_unset_bypass_cache(options); + } + + /* + * 3. Set the bypass cache + */ + options->bypass_cache = bypass_cache; + atclient_put_shared_key_request_options_set_bypass_cache_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_put_shared_key_request_options_unset_bypass_cache(atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_unset_bypass_cache: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the bypass cache + */ + options->bypass_cache = false; + atclient_put_shared_key_request_options_set_bypass_cache_initialized(options, false); +} + +/* + * ================= + * 1C. Put PublicKey + * ================= + */ +void atclient_put_public_key_request_options_init(atclient_put_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_put_public_key_request_options)); +} + +void atclient_put_public_key_request_options_free(atclient_put_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_put_public_key_request_options_is_bypass_cache_initialized(options)) { + atclient_put_public_key_request_options_unset_bypass_cache(options); + } +} + +bool atclient_put_public_key_request_options_is_bypass_cache_initialized( + const atclient_put_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_is_bypass_cache_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the bypass cache is initialized + */ + return options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & + ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; +} + +void atclient_put_public_key_request_options_set_bypass_cache_initialized( + atclient_put_public_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_set_bypass_cache_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the bypass cache initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= + ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= + ~ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } +} + +int atclient_put_public_key_request_options_set_bypass_cache(atclient_put_public_key_request_options *options, + const bool bypass_cache) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_set_bypass_cache: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the bypass cache, if necessary + */ + if (atclient_put_public_key_request_options_is_bypass_cache_initialized(options)) { + atclient_put_public_key_request_options_unset_bypass_cache(options); + } + + /* + * 3. Set the bypass cache + */ + options->bypass_cache = bypass_cache; + atclient_put_public_key_request_options_set_bypass_cache_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_put_public_key_request_options_unset_bypass_cache(atclient_put_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_public_key_request_options_unset_bypass_cache: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the bypass cache + */ + options->bypass_cache = false; + atclient_put_public_key_request_options_set_bypass_cache_initialized(options, false); +} + +/* + * ================= + * 2A. Get SelfKey + * ================= + */ +void atclient_get_self_key_request_options_init(atclient_get_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_get_self_key_request_options)); +} +void atclient_get_self_key_request_options_free(atclient_get_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_get_self_key_request_options_unset_shared_encryption_key(options); + } +} + +bool atclient_get_self_key_request_options_is_shared_encryption_key_initialized( + const atclient_get_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_is_shared_encryption_key_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the shared encryption key is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & + ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; +} + +void atclient_get_self_key_request_options_set_shared_encryption_key_initialized( + atclient_get_self_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_set_shared_encryption_key_initialized: Invalid arguments\n"); + return; + } + + /* + * 2. Set the shared encryption key initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } +} + +int atclient_get_self_key_request_options_set_shared_encryption_key(atclient_get_self_key_request_options *options, + const unsigned char *shared_encryption_key) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_set_shared_encryption_key: Invalid arguments\n"); + goto exit; + } + + if (shared_encryption_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_set_shared_encryption_key: Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the shared encryption key, if necessary + */ + if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_get_self_key_request_options_unset_shared_encryption_key(options); + } + + /* + * 3. Set the shared encryption key + */ + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_set_shared_encryption_key: Failed to allocate memory for " + "shared encryption key\n"); + goto exit; + } + + atclient_get_self_key_request_options_set_shared_encryption_key_initialized(options, true); + memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_self_key_request_options_unset_shared_encryption_key(atclient_get_self_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_self_key_request_options_unset_shared_encryption_key: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the shared encryption key + */ + if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { + free(options->shared_encryption_key); + } + options->shared_encryption_key = NULL; + atclient_get_self_key_request_options_set_shared_encryption_key_initialized(options, false); +} + +/* + * ================= + * 2B. Get SharedKey + * ================= + */ +void atclient_get_shared_key_request_options_init(atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_get_shared_key_request_options)); +} + +void atclient_get_shared_key_request_options_free(atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_get_shared_key_request_options_unset_shared_encryption_key(options); + } +} + +bool atclient_get_shared_key_request_options_is_shared_encryption_key_initialized( + const atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_is_shared_encryption_key_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the shared encryption key is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; +} +void atclient_get_shared_key_request_options_set_shared_encryption_key_initialized( + atclient_get_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_shared_encryption_key_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the shared encryption key initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= + ~ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + } +} + +int atclient_get_shared_key_request_options_set_shared_encryption_key(atclient_get_shared_key_request_options *options, + const unsigned char *shared_encryption_key) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_shared_encryption_key: " + "Invalid arguments\n"); + goto exit; + } + + if (shared_encryption_key == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_shared_encryption_key: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the shared encryption key, if necessary + */ + if (atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + atclient_get_shared_key_request_options_unset_shared_encryption_key(options); + } + + /* + * 3. Set the shared encryption key + */ + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == + NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_shared_encryption_key: " + "Failed to allocate memory for shared encryption key\n"); + goto exit; + } + + atclient_get_shared_key_request_options_set_shared_encryption_key_initialized(options, true); + memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_shared_key_request_options_unset_shared_encryption_key( + atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_unset_shared_encryption_key: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the shared encryption key + */ + if (atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(options)) { + free(options->shared_encryption_key); + } + options->shared_encryption_key = NULL; + atclient_get_shared_key_request_options_set_shared_encryption_key_initialized(options, false); +} + +/* + * ================= + * 2C. Get PublicKey + * ================= + */ +void atclient_get_public_key_request_options_init(atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_get_public_key_request_options)); +} + +void atclient_get_public_key_request_options_free(atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_get_public_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_public_key_request_options_unset_store_atkey_metadata(options); + } +} + +bool atclient_get_public_key_request_options_is_store_atkey_metadata_initialized( + const atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_is_store_atkey_metadata_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the store atkey metadata is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] & + ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; +} + +void atclient_get_public_key_request_options_set_store_atkey_metadata_initialized( + atclient_get_public_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_set_store_atkey_metadata_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the store atkey metadata initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] |= + ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] &= + ~ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; + } +} + +int atclient_get_public_key_request_options_set_store_atkey_metadata(atclient_get_public_key_request_options *options, + const bool store_atkey_metadata) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_set_store_atkey_metadata: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the store atkey metadata, if necessary + */ + if (atclient_get_public_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_public_key_request_options_unset_store_atkey_metadata(options); + } + + /* + * 3. Set the store atkey metadata + */ + options->store_atkey_metadata = store_atkey_metadata; + atclient_get_public_key_request_options_set_store_atkey_metadata_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_public_key_request_options_unset_store_atkey_metadata( + atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_unset_store_atkey_metadata: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the store atkey metadata + */ + options->store_atkey_metadata = false; + atclient_get_public_key_request_options_set_store_atkey_metadata_initialized(options, false); +} From 42d3fc770081bf02b727d30458f8de31a2e6b9a0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 09:09:45 -0400 Subject: [PATCH 128/193] chore: remove sys/time.h from notify.c --- packages/atclient/src/notify.c | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 1d430693..b97bb845 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #define TAG "atclient_notify" From a4d9bc754626b5df62443cf3967fb945ce19238f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 09:10:05 -0400 Subject: [PATCH 129/193] feat: start on atclient_connection_read/write --- .../atclient/include/atclient/connection.h | 23 +++++- packages/atclient/src/connection.c | 76 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index 51d44f9e..d3930c90 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -67,7 +67,6 @@ typedef struct atclient_connection { * @param type the type of connection to initialize, * if it is ATCLIENT_CONNECTION_TYPE_ROOT, then '\\n' will be used to check if it is connected. * if it is ATCLIENT_CONNECTION_TYPE_ATSERVER, then 'noop:0\r\n' will be used to check if it is connected - * */ void atclient_connection_init(atclient_connection *ctx, atclient_connection_type type); @@ -147,4 +146,26 @@ int atclient_connection_hooks_set(atclient_connection *ctx, atclient_connection_ * @note For performance, keep readonly_src set to true if you don't need to write access to src */ void atclient_connection_hooks_set_readonly_src(atclient_connection *ctx, bool readonly_src); + +/** + * @brief Write data to the connection + * + * @param ctx connection initialized and connected using atclient_connection_init and atclient_connection_connect + * @param value the data to write + * @param value_len the length of the data to write + * @return int 0 on success + */ +int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len); + +/** + * @brief Reads data from the connection + * + * @param ctx the connection initialized and connected using atclient_connection_init and atclient_connection_connect + * @param value a double pointer that will be allocated by the function to the data read + * @param value_len the length of the data read, will be set by the function + * @param value_max_len the maximum length of the data to read, setting this to 0 means no limit + * @return int 0 on success + */ +int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len); + #endif diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 1864e67f..971d187c 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -497,3 +497,79 @@ void atclient_connection_hooks_set_readonly_src(atclient_connection *ctx, bool r } ctx->hooks->readonly_src = readonly_src; } + +// int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len) { +// int ret = 1; + +// /* +// * 1. Validate arguments +// */ +// if(ctx == NULL) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); +// goto exit; +// } + +// if(value == NULL) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); +// goto exit; +// } + +// if(value_len == 0) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is 0\n"); +// goto exit; +// } + +// if (!ctx->_should_be_connected) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, +// "ctx->_should_be_connected should be true, but is false, you are trying to write to a non-connected " +// "connection\n"); +// goto exit; +// } + +// /* +// * 2. Write the value +// */ +// if ((ret = mbedtls_ssl_write(&(ctx->ssl), value, value_len)) <= 0) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); +// goto exit; +// } + +// /* +// * 3. Print debug log +// */ +// if(atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { +// unsigned char *valuecopy = malloc(sizeof(unsigned char) * value_len); +// if (valuecopy != NULL) { +// memcpy(valuecopy, value, value_len); +// atlogger_fix_stdout_buffer((char *)valuecopy, value_len); +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, value_len, valuecopy, reset); +// free(valuecopy); +// } else { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory to pretty print the network sent transmission\n"); +// } +// } + +// /* +// * 4. Call hooks, if they exist +// */ +// bool try_hooks = ctx->hooks != NULL && !ctx->hooks->_is_nested_call; +// if (try_hooks && ctx->hooks->post_recv != NULL) { +// ctx->hooks->_is_nested_call = true; +// ret = ctx->hooks->post_recv(src, srclen, recv, recvsize, recvlen) +// if (ctx->hooks != NULL) { +// ctx->hooks->_is_nested_call = false; +// } +// if (ret != 0) { +// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_recv hook failed with exit code: %d\n", ret); +// goto exit; +// } +// } + +// ret = 0; +// goto exit; +// exit: { +// return ret; +// } +// } + +// int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len); \ No newline at end of file From e94e97347ace98394d2c091a0da66bf5e665fc8f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 09:13:35 -0400 Subject: [PATCH 130/193] feat: update atclient_delete --- packages/atclient/include/atclient/atclient.h | 14 ++++++++------ packages/atclient/src/atclient_delete.c | 9 +++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index b723c153..7682af8e 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -199,8 +199,8 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be - * associated with your value. + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to + * be associated with your value. * * @param atclient The atclient context (must satisfy the two conditions stated above) * @param atkey The populated atkey to get the value from (must satisfy the two conditions stated above) @@ -223,15 +223,17 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be - * associated with your value. + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to + * be associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to delete from atServer (must satisfy the two conditions stated above) - * @param commit_id the output commit_id of the delete operation that the atServer returns, can be set to NULL if you do not need it + * @param options the options for the delete operation + * @param commit_id the output commit_id of the delete operation that the atServer returns, can be set to NULL if you do + * not need it * @return int 0 on success */ -int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit_id); +int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, int *commit_id); /** * @brief Runs a scan of the atServer to see what atKeys you have. diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index ae24c13f..2bf9e46c 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -4,19 +4,20 @@ #include "atclient/stringutils.h" #include "atlogger/atlogger.h" #include +#include #include #define TAG "atclient_delete" -static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey); +static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, const int *commit_id); -int atclient_delete(atclient *atclient, const atclient_atkey *atkey, int *commit_id) { +int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, int *commit_id) { int ret = 1; /* * 1. Check arguments */ - if ((ret = atclient_delete_validate_arguments(atclient, atkey)) != 0) { + if ((ret = atclient_delete_validate_arguments(atclient, atkey, options, commit_id)) != 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete_validate_arguments: %d\n", ret); return ret; @@ -98,7 +99,7 @@ exit: { } } -static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey) { +static int atclient_delete_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, const int *commit_id) { int ret = 1; if (atclient == NULL) { From 3db3a27042ea954eb3e2ba703444081092cd0f8b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 12:26:32 -0400 Subject: [PATCH 131/193] feat: make crud operations camel case and separate atclient_put into 3 --- examples/desktop/crud/get_publickey.c | 2 +- examples/desktop/crud/get_selfkey.c | 2 +- examples/desktop/crud/get_sharedkey.c | 2 +- examples/desktop/crud/put_publickey.c | 4 +- examples/desktop/crud/put_selfkey.c | 2 +- examples/desktop/repl/src/main.c | 12 +- examples/desktop/temp/CMakeLists.txt | 10 + packages/atclient/include/atclient/atclient.h | 77 ++++- .../include/atclient/request_options.h | 33 ++- .../atclient/include/atclient/stringutils.h | 12 +- packages/atclient/src/atclient.c | 12 +- packages/atclient/src/atclient_delete.c | 2 +- packages/atclient/src/atclient_get_atkeys.c | 2 +- ..._publickey.c => atclient_get_public_key.c} | 12 +- ..._get_selfkey.c => atclient_get_self_key.c} | 10 +- ..._sharedkey.c => atclient_get_shared_key.c} | 56 ++-- packages/atclient/src/atclient_put.c | 24 +- .../atclient/src/atclient_put_public_key.c | 191 ++++++++++++ packages/atclient/src/atclient_put_self_key.c | 221 ++++++++++++++ .../atclient/src/atclient_put_shared_key.c | 259 ++++++++++++++++ packages/atclient/src/atclient_utils.c | 4 +- packages/atclient/src/atkey.c | 8 +- .../atclient/src/encryption_key_helpers.c | 48 +-- packages/atclient/src/metadata.c | 12 +- packages/atclient/src/monitor.c | 2 +- packages/atclient/src/notify.c | 10 +- packages/atclient/src/request_options.c | 279 ++++++++---------- .../src/{stringutils.c => string_utils.c} | 12 +- packages/atclient/tests/test_stringutils.c | 18 +- tests/functional_tests/lib/src/helpers.c | 22 +- .../tests/test_atclient_publickey.c | 10 +- .../tests/test_atclient_selfkey.c | 6 +- .../tests/test_atclient_sharedkey.c | 4 +- 33 files changed, 1036 insertions(+), 344 deletions(-) create mode 100644 examples/desktop/temp/CMakeLists.txt rename packages/atclient/src/{atclient_get_publickey.c => atclient_get_public_key.c} (90%) rename packages/atclient/src/{atclient_get_selfkey.c => atclient_get_self_key.c} (94%) rename packages/atclient/src/{atclient_get_sharedkey.c => atclient_get_shared_key.c} (88%) create mode 100644 packages/atclient/src/atclient_put_public_key.c create mode 100644 packages/atclient/src/atclient_put_self_key.c create mode 100644 packages/atclient/src/atclient_put_shared_key.c rename packages/atclient/src/{stringutils.c => string_utils.c} (85%) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index 33ba4ab9..03362ef6 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -77,7 +77,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - if ((ret = atclient_get_publickey(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { + if ((ret = atclient_get_public_key(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index d6d3feff..afdfd0b6 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -78,7 +78,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)(atkeystrlen), atkeystr); - ret = atclient_get_selfkey(&atclient, &atkey, value, valuelen, &(valueolen)); + ret = atclient_get_self_key(&atclient, &atkey, value, valuelen, &(valueolen)); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get self key"); goto exit; diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 1c8fa86a..7ae1d8b4 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -93,7 +93,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - ret = atclient_get_sharedkey(&atclient, &atkey, value, valuesize, valuelen, NULL); + ret = atclient_get_shared_key(&atclient, &atkey, value, valuesize, valuelen, NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared key"); goto exit; diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index d2f4927f..b5164a99 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -82,8 +82,8 @@ int main() { goto exit; } - // atclient_get_publickey - if ((ret = atclient_get_publickey(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { + // atclient_get_public_key + if ((ret = atclient_get_public_key(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index d4b112f6..ea818d3f 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -81,7 +81,7 @@ int main() { goto exit; } - if ((ret = atclient_get_selfkey(&atclient, &atkey, value, valuelen, &valueolen)) != 0) { + if ((ret = atclient_get_self_key(&atclient, &atkey, value, valuelen, &valueolen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 0277be53..5ab9ee7c 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -152,8 +152,8 @@ int main(int argc, char *argv[]) { goto get_end; } case ATCLIENT_ATKEY_TYPE_PUBLIC_KEY: { - if ((ret = atclient_get_publickey(&atclient, &atkey, recv, recvsize, &recvlen, true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_publickey: %d | failed to get public key\n", + if ((ret = atclient_get_public_key(&atclient, &atkey, recv, recvsize, &recvlen, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_key: %d | failed to get public key\n", ret); goto get_end; } @@ -161,16 +161,16 @@ int main(int argc, char *argv[]) { break; } case ATCLIENT_ATKEY_TYPE_SELF_KEY: { - if ((ret = atclient_get_selfkey(&atclient, &atkey, recv, recvsize, &recvlen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey: %d | failed to get self key\n", ret); + if ((ret = atclient_get_self_key(&atclient, &atkey, recv, recvsize, &recvlen)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key: %d | failed to get self key\n", ret); goto get_end; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Value: \"%s\"\n", recv); break; } case ATCLIENT_ATKEY_TYPE_SHARED_KEY: { - if ((ret = atclient_get_sharedkey(&atclient, &atkey, recv, recvsize, &recvlen, NULL)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey: %d | failed to get shared key\n", + if ((ret = atclient_get_shared_key(&atclient, &atkey, recv, recvsize, &recvlen, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key: %d | failed to get shared key\n", ret); goto get_end; } diff --git a/examples/desktop/temp/CMakeLists.txt b/examples/desktop/temp/CMakeLists.txt new file mode 100644 index 00000000..4c5ec7a3 --- /dev/null +++ b/examples/desktop/temp/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.24) + +project (temp) + +find_package (atsdk REQUIRED) + +add_executable(main main.c) + +target_link_libraries(main atsdk::atclient) + diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 7682af8e..09ae2a6f 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -4,6 +4,7 @@ #include "atclient/atkey.h" #include "atclient/atkeys.h" #include "atclient/connection.h" +#include "atclient/request_options.h" #include #include @@ -139,13 +140,56 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to put the value into (must satisfy the two conditions stated above) - * @param value the value to put into atServer - * @param value_len the length of the value (most of the time you will use strlen() on a null-terminated string for - * this value) - * @param commit_id (optional) the output commit_id of the put operation that the atServer returns + * @param value the value to put into atServer, assumed to be non-null and null-terminated + * @param commit_id (optional) the output commit_id of the put operation that the atServer returns, can be NULL if you + * don't care about the commit_id * @return int 0 on success */ -int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, const size_t value_len, int *commit_id); +int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int **commit_id); + +/** + * @brief Put a string value into a self key into your atServer. Putting a self key is a private value and is encrypted only for you + * + * @param ctx the atclient context, must be initialized with atclient_init() and authenticated via + * atclient_pkam_authenticate() + * @param atkey the atkey to put the value into, must be initialized with atclient_atkey_init() and have populated + * values (sharedby, key and optionally namespace) + * @param value the value to put into the atServer, assumed to be non-null and null-terminated + * @param request_options the options for the put operation, can be NULL if you don't need to set any options + * @param commit_id the output commit_id of the put operation that the atServer returns, can be NULL if you don't care + * about the commit_id + * @return int 0 on success + */ +int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_self_key_request_options *request_options, int *commit_id); + +/** + * @brief Put a string value into a shared key into your atServer. Putting a shared key is a shared value and is encrypted for you and the person you shared it with + * + * @param ctx the atclient context, must be initialized with atclient_init() and authenticated via + * atclient_pkam_authenticate() + * @param atkey the atkey to put the value into, must be initialized with atclient_atkey_init() and have populated + * values (sharedby, key and optionally namespace) + * @param value the value to put into the atServer, assumed to be non-null and null-terminated + * @param request_options the options for the put operation, can be NULL if you don't need to set any options + * @param commit_id the output commit_id of the put operation that the atServer returns, can be NULL if you don't care + * about the commit_id + * @return int 0 on success + */ +int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_shared_key_request_options *request_options, int *commit_id); + +/** + * @brief Put a string value into a public key into your atServer. Putting a public key is a public value and not encrypted + * + * @param ctx the atclient context, must be initialized with atclient_init() and authenticated via atclient_pkam_authenticate() + * @param atkey the atkey to put the value into, must be initialized with atclient_atkey_init() and have populated values (sharedby, key and optionally namespace) + * @param value the value to put into the atServer, assumed to be non-null and null-terminated + * @param request_options the options for the put operation, can be NULL if you don't need to set any options + * @param commit_id the output commit_id of the put operation that the atServer returns, can be NULL if you don't care about the commit_id + * @return int 0 on success + */ +int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *value, const atclient_put_public_key_request_options *request_options, int *commit_id); /** * @brief Get a string value from your atServer. @@ -155,8 +199,8 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be - * associated with your value. + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to + * be associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) @@ -165,8 +209,8 @@ int atclient_put(atclient *atclient, atclient_atkey *atkey, const char *value, c * @param value_len the output length of the value gotten from atServer * @return int 0 on success */ -int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len); +int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len); /** * @brief Get a publickey from your atServer or another atServer @@ -176,8 +220,8 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, * * `atkey` must satisfy the following condition before calling this function: * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to be - * associated with your value. + * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want to + * be associated with your value. * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) @@ -188,8 +232,8 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, * up-to-date value straight from the atServer that the publickey sits on, false otherwise * @return int 0 on success */ -int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, bool bypass_cache); +int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, bool bypass_cache); /** * @brief Get a sharedkey either shared by you or shared with you and receive the decrypted plaintext value. @@ -212,8 +256,8 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * exists. * @return int 0 on success */ -int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, const unsigned char *shared_encryption_key); +int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, + size_t *value_len, const unsigned char *shared_encryption_key); /** * @brief Delete an atkey from your atserver @@ -233,7 +277,8 @@ int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *valu * not need it * @return int 0 on success */ -int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, int *commit_id); +int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atclient_delete_request_options *options, + int *commit_id); /** * @brief Runs a scan of the atServer to see what atKeys you have. diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index 422b28ab..ea865097 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -38,8 +38,8 @@ * 1A. Put SelfKey */ typedef struct atclient_put_self_key_request_options { - unsigned char *shared_encryption_key; - uint8_t _initialized_fields[1]; + // empty for now + // kept for future proofing } atclient_put_self_key_request_options; /* @@ -47,6 +47,7 @@ typedef struct atclient_put_self_key_request_options { */ typedef struct atclient_put_shared_key_request_options { unsigned char *shared_encryption_key; + unsigned char *iv; bool bypass_cache; uint8_t _initialized_fields[1]; } atclient_put_shared_key_request_options; @@ -63,7 +64,6 @@ typedef struct atclient_put_public_key_request_options { * 2A. Get SelfKey */ typedef struct atclient_get_self_key_request_options { - unsigned char *shared_encryption_key; bool store_atkey_metadata; uint8_t _initialized_fields[1]; } atclient_get_self_key_request_options; @@ -100,14 +100,6 @@ typedef struct atclient_delete_request_options { void atclient_put_self_key_request_options_init(atclient_put_self_key_request_options *options); void atclient_put_self_key_request_options_free(atclient_put_self_key_request_options *options); -bool atclient_put_self_key_request_options_is_shared_encryption_key_initialized( - const atclient_put_self_key_request_options *options); -void atclient_put_self_key_request_options_set_shared_encryption_key_initialized( - atclient_put_self_key_request_options *options, const bool initialized); -int atclient_put_self_key_request_options_set_shared_encryption_key(atclient_put_self_key_request_options *options, - const unsigned char *shared_encryption_key); -void atclient_put_self_key_request_options_unset_shared_encryption_key(atclient_put_self_key_request_options *options); - /* * 1B. Put SharedKey */ @@ -151,13 +143,13 @@ void atclient_put_public_key_request_options_unset_bypass_cache(atclient_put_pub void atclient_get_self_key_request_options_init(atclient_get_self_key_request_options *options); void atclient_get_self_key_request_options_free(atclient_get_self_key_request_options *options); -bool atclient_get_self_key_request_options_is_shared_encryption_key_initialized( +bool atclient_get_self_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_self_key_request_options *options); -void atclient_get_self_key_request_options_set_shared_encryption_key_initialized( +void atclient_get_self_key_request_options_set_store_atkey_metadata_initialized( atclient_get_self_key_request_options *options, const bool initialized); -int atclient_get_self_key_request_options_set_shared_encryption_key(atclient_get_self_key_request_options *options, - const unsigned char *shared_encryption_key); -void atclient_get_self_key_request_options_unset_shared_encryption_key(atclient_get_self_key_request_options *options); +int atclient_get_self_key_request_options_set_store_atkey_metadata(atclient_get_self_key_request_options *options, + const bool store_atkey_metadata); +void atclient_get_self_key_request_options_unset_store_atkey_metadata(atclient_get_self_key_request_options *options); /* * 2B. Get SharedKey @@ -174,6 +166,15 @@ int atclient_get_shared_key_request_options_set_shared_encryption_key(atclient_g void atclient_get_shared_key_request_options_unset_shared_encryption_key( atclient_get_shared_key_request_options *options); +bool atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized( + const atclient_get_shared_key_request_options *options); +void atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized( + atclient_get_shared_key_request_options *options, const bool initialized); +int atclient_get_shared_key_request_options_set_store_atkey_metadata(atclient_get_shared_key_request_options *options, + const bool store_atkey_metadata); +void atclient_get_shared_key_request_options_unset_store_atkey_metadata( + atclient_get_shared_key_request_options *options); + /* * 2C. Get PublicKey */ diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/stringutils.h index 7aab2773..49a3e8ad 100644 --- a/packages/atclient/include/atclient/stringutils.h +++ b/packages/atclient/include/atclient/stringutils.h @@ -14,7 +14,7 @@ * @param out_len the output length of the output buffer that is actually used * @return int 0 on success, non-zero on failure */ -int atclient_stringutils_trim_whitespace(const char *string, const size_t stringlen, char *out, const size_t out_size, +int atclient_string_utils_trim_whitespace(const char *string, const size_t stringlen, char *out, const size_t out_size, size_t *out_len); /** @@ -25,7 +25,7 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string * @return true, if string starts with `prefix`, where `prefix` is a valid substring of `string` * @return false, otherwise */ -bool atclient_stringutils_starts_with(const char *string, const char *prefix); +bool atclient_string_utils_starts_with(const char *string, const char *prefix); /** * @brief check if string ends with suffix @@ -35,7 +35,7 @@ bool atclient_stringutils_starts_with(const char *string, const char *prefix); * @return true, if string ends with `suffix`, where `suffix` is a valid substring of `string` * @return false, otherwise */ -bool atclient_stringutils_ends_with(const char *string, const char *suffix); +bool atclient_string_utils_ends_with(const char *string, const char *suffix); /** * @brief generate a new string with the atsign and the guaranteed @ symbol @@ -44,7 +44,7 @@ bool atclient_stringutils_ends_with(const char *string, const char *suffix); * @param output_atsign_with_at_symbol the output atsign with the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol); +int atclient_string_utils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol); /** * @brief generate a new string with the atsign and the guaranteed @ symbol @@ -53,7 +53,7 @@ int atclient_stringutils_atsign_with_at(const char *original_atsign, char **outp * @param output_atsign_without_at_symbol the output atsign without the @ symbol, must be freed by the caller * @return int 0 on success */ -int atclient_stringutils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol); +int atclient_string_utils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol); /** * @brief get the length of a long if it were converted to a string @@ -61,6 +61,6 @@ int atclient_stringutils_atsign_without_at(const char *original_atsign, char **o * @param n the long to check the length of * @return int the string length */ -int atclient_stringutils_long_strlen(long n); +int atclient_string_utils_long_strlen(long n); #endif diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 53baa3af..3f717fa0 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -164,8 +164,8 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i /* * 3. Ensure that the atsign has the @ symbol. */ - if ((ret = atclient_stringutils_atsign_with_at(atsign, &(atsign_with_at))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atsign, &(atsign_with_at))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -201,7 +201,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); @@ -252,7 +252,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i } // check for data:success - if (!atclient_stringutils_starts_with((char *)recv, "data:success")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:success")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:success\"\n", (int)recv_len, recv); @@ -314,8 +314,8 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) { goto exit; } - if (!atclient_stringutils_starts_with((const char *)ptr, "data:ok") && - !atclient_stringutils_ends_with((const char *)ptr, "data:ok")) { + if (!atclient_string_utils_starts_with((const char *)ptr, "data:ok") && + !atclient_string_utils_ends_with((const char *)ptr, "data:ok")) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to receive heartbeat response\n"); ret = -1; goto exit; diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 2bf9e46c..23e2716f 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -76,7 +76,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atcli char *respose = (char *)recv; - if (!atclient_stringutils_starts_with(respose, "data:")) { + if (!atclient_string_utils_starts_with(respose, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index c55c5201..78652bf4 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -60,7 +60,7 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi /* * 5. Parse response */ - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recvlen, recv); diff --git a/packages/atclient/src/atclient_get_publickey.c b/packages/atclient/src/atclient_get_public_key.c similarity index 90% rename from packages/atclient/src/atclient_get_publickey.c rename to packages/atclient/src/atclient_get_public_key.c index d42fbe61..f36a6419 100644 --- a/packages/atclient/src/atclient_get_publickey.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -8,19 +8,19 @@ #define TAG "atclient_get_publickey" -static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_public_key_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, bool bypass_cache); -int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, +int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, bool bypass_cache) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_publickey_validate_arguments(atclient, atkey, value, value_size, value_len, bypass_cache)) != + if ((ret = atclient_get_public_key_validate_arguments(atclient, atkey, value, value_size, value_len, bypass_cache)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_publickey_validate_arguments: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_key_validate_arguments: %d\n", ret); return ret; } @@ -82,7 +82,7 @@ int atclient_get_publickey(atclient *atclient, atclient_atkey *atkey, char *valu * 5. Parse response */ char *response = (char *)recv; - if (!atclient_stringutils_starts_with(response, "data:")) { + if (!atclient_string_utils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); @@ -139,7 +139,7 @@ exit: { } } -static int atclient_get_publickey_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_public_key_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, bool bypass_cache) { int ret = 1; diff --git a/packages/atclient/src/atclient_get_selfkey.c b/packages/atclient/src/atclient_get_self_key.c similarity index 94% rename from packages/atclient/src/atclient_get_selfkey.c rename to packages/atclient/src/atclient_get_self_key.c index fab4e2d9..ebc06d79 100644 --- a/packages/atclient/src/atclient_get_selfkey.c +++ b/packages/atclient/src/atclient_get_self_key.c @@ -14,7 +14,7 @@ #define TAG "atclient_get_selfkey" -static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t value_size, const size_t *value_len); int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, @@ -24,8 +24,8 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, /* * 1. Validate arguments */ - if ((ret = atclient_get_selfkey_valid_arguments(atclient, atkey, value, value_size, value_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_selfkey_valid_arguments: %d\n", ret); + if ((ret = atclient_get_self_key_valid_arguments(atclient, atkey, value, value_size, value_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_valid_arguments: %d\n", ret); return ret; } @@ -86,7 +86,7 @@ int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, * 5. Parse response */ char *response = (char *)recv; - if (!atclient_stringutils_starts_with(response, "data:")) { + if (!atclient_string_utils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); @@ -180,7 +180,7 @@ exit: { } } -static int atclient_get_selfkey_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t value_size, const size_t *value_len) { int ret = 1; diff --git a/packages/atclient/src/atclient_get_sharedkey.c b/packages/atclient/src/atclient_get_shared_key.c similarity index 88% rename from packages/atclient/src/atclient_get_sharedkey.c rename to packages/atclient/src/atclient_get_shared_key.c index 36a54e5f..98d76cc8 100644 --- a/packages/atclient/src/atclient_get_sharedkey.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -12,51 +12,51 @@ #define TAG "atclient_get_sharedkey" -static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t value_size, const size_t *value_len, const unsigned char *shared_encryption_key); -static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key); -static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key); -int atclient_get_sharedkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, +int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; - if ((ret = atclient_get_sharedkey_validate_arguments(atclient, atkey, value, value_size, value_len, + if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_validate_arguments: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_validate_arguments: %d\n", ret); return ret; } char *client_atsign_with_at = NULL; char *sharedby_atsign_with_at = NULL; - if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_by, &sharedby_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_by, &sharedby_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - if ((ret = atclient_get_sharedkey_shared_by_other_with_me(atclient, atkey, value, value_size, value_len, + if ((ret = atclient_get_shared_key_shared_by_other_with_me(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_other_with_me: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - if ((ret = atclient_get_sharedkey_shared_by_me_with_other(atclient, atkey, value, value_size, value_len, + if ((ret = atclient_get_shared_key_shared_by_me_with_other(atclient, atkey, value, value_size, value_len, shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_sharedkey_shared_by_me_with_other: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_me_with_other: %d\n", ret); goto exit; } } @@ -69,7 +69,7 @@ exit: { } } -static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -109,13 +109,13 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at /* * 3. Format atSigns */ - if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atclient->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -163,7 +163,7 @@ static int atclient_get_sharedkey_shared_by_me_with_other(atclient *atclient, at */ char *response = (char *)recv; - if (!atclient_stringutils_starts_with(response, "data:")) { + if (!atclient_string_utils_starts_with(response, "data:")) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); ret = 1; goto exit; @@ -272,7 +272,7 @@ exit: { } } -static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, +static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -308,13 +308,13 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at /* * 3. Format atSigns */ - if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_by, &sender_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_by, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -368,7 +368,7 @@ static int atclient_get_sharedkey_shared_by_other_with_me(atclient *atclient, at char *response = (char *)recv; // Truncate response : "data:" - if (!atclient_stringutils_starts_with(response, "data:")) { + if (!atclient_string_utils_starts_with(response, "data:")) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n"); ret = 1; goto exit; @@ -474,7 +474,7 @@ exit: { } } -static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t value_size, const size_t *value_len, const unsigned char *shared_encryption_key) { int ret = 1; @@ -501,7 +501,7 @@ static int atclient_get_sharedkey_validate_arguments(const atclient *atclient, c ret = 1; atlogger_log( TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_sharedkey cannot be called from an async_read atclient, it will cause a race condition\n"); + "atclient_get_shared_key cannot be called from an async_read atclient, it will cause a race condition\n"); goto exit; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 6b2f84fe..3efae955 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -15,15 +15,15 @@ #define TAG "atclient_put" static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const size_t value_len, const int *commit_id); + const int **commit_id); -int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const size_t value_len, int *commit_id) { +int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int *commit_id) { int ret = 1; /* * 1. Check if valid arguments were passed */ - if ((ret = atclient_put_validate_args(ctx, atkey, value, value_len, commit_id)) != 0) { + if ((ret = atclient_put_validate_args(ctx, atkey, value, commit_id)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_validate_args: %d\n", ret); return ret; } @@ -31,6 +31,8 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const /* * 2. Allocate variables */ + const size_t value_len = strlen(value); + char *atkey_str = NULL; char *update_cmd = NULL; char *metadata_protocol_str = NULL; @@ -108,7 +110,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const } if ((ret = atchops_aes_ctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, - ciphertext, ciphertext_size, &ciphertext_len)) != 0) { + ciphertext, ciphertext_size, &ciphertext_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto exit; } @@ -127,7 +129,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const // create one for the other person -> encrypted with their public encryption key char *recipient_atsign_with_at = NULL; - if ((ret = atclient_stringutils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_concat_at_sign_with_at: %d\n", ret); goto shared_key_exit; } @@ -162,7 +164,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const } if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, - ciphertext, ciphertext_size, &ciphertext_len)) != 0) { + ciphertext, ciphertext_size, &ciphertext_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto shared_key_exit; } @@ -220,7 +222,7 @@ int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, const goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); @@ -250,7 +252,7 @@ exit: { } static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const size_t value_len, const int *commit_id) { + const int **commit_id) { int ret = 1; if (ctx == NULL) { ret = 1; @@ -282,12 +284,6 @@ static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey goto exit; } - if (value_len == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is 0\n"); - goto exit; - } - if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->shared_by) <= 0) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's shared_by is not initialized or is empty\n"); diff --git a/packages/atclient/src/atclient_put_public_key.c b/packages/atclient/src/atclient_put_public_key.c new file mode 100644 index 00000000..b40cf591 --- /dev/null +++ b/packages/atclient/src/atclient_put_public_key.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TAG "atclient_put_public_key" + +static int atclient_put_public_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, + const char *value, + const atclient_put_public_key_request_options *request_options, + const int *commit_id); + +int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_public_key_request_options *request_options, int *commit_id) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if ((ret = atclient_put_public_key_validate_arguments(ctx, atkey, value, request_options, commit_id)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_public_key_validate_arguments: %d\n", ret); + return ret; + } + + /* + * 2. Variables + */ + char *atkey_str = NULL; + char *metadata_protocol_str = NULL; + char *update_cmd = NULL; + + const size_t recv_size = 256; + unsigned char *recv = NULL; + if (!ctx->async_read) { + if ((recv = malloc(sizeof(unsigned char) * recv_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + goto exit; + } + memset(recv, 0, sizeof(unsigned char) * recv_size); + } + size_t recv_len = 0; + + /* + * 3. Build update command + */ + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); + goto exit; + } + + if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); + goto exit; + } + + const size_t atkey_str_len = strlen(atkey_str); + const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); + + const size_t update_cmd_size = + strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + strlen(value) + strlen("\r\n") + 1; + if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); + goto exit; + } + snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + const size_t update_cmd_len = update_cmd_size - 1; + + /* + * 4. Send update command + */ + if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + &recv_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; + } + + if (ctx->async_read) { + goto exit; + } + + char *response = (char *)recv; + + /* + * 5. Receive commit id + */ + if (commit_id != NULL) { + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + + char *response_without_data = response + strlen("data:"); + + *commit_id = atoi(response_without_data); + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int atclient_put_public_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, + const char *value, + const atclient_put_public_key_request_options *request_options, + const int *commit_id) { + int ret = 1; + + char *client_atsign_with_at = NULL; + char *shared_by_atsign_with_at = NULL; + + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (request_options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); + goto exit; + } + + if (commit_id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); + goto exit; + } + + const atkey_type = atclient_atkey_get_type(atkey); + + if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATKEY_TYPE_PUBLICKEY\n"); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_key_initialized is false\n"); + goto exit; + } + + if (!atclient_atkey_is_shared_by_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_shared_by_initialized is false\n"); + goto exit; + } + + if ((ret = atclient_string_utils_atsign_with_at(ctx->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at failed\n"); + goto exit; + } + + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_by, &shared_by_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at failed\n"); + goto exit; + } + + if (strcmp(client_atsign_with_at, shared_by_atsign_with_at) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "client_atsign_with_at and shared_by_atsign_with_at are not equal\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c new file mode 100644 index 00000000..9e517ade --- /dev/null +++ b/packages/atclient/src/atclient_put_self_key.c @@ -0,0 +1,221 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TAG "atclient_put_self_key" + +static int atclient_put_self_key_validate_arguments(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_self_key_request_options *request_options, + int *commit_id); + +int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_self_key_request_options *request_options, int *commit_id) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if ((ret = atclient_put_self_key_validate_arguments(ctx, atkey, value, request_options, commit_id)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_self_key_validate_arguments: %d\n", ret); + return ret; + } + + /* + * 2. Variables + */ + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key[shared_encryption_key_size]; + + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; + + const size_t iv_base64_size = atchops_base64_encoded_size(iv_size); + char iv_base64[iv_base64_size]; + + const size_t value_len = strlen(value); + + const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(value_len); + unsigned char value_encrypted[value_encrypted_size]; + + const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size); + char value_encrypted_base64[value_encrypted_base64_size]; + + char *update_cmd = NULL; + char *metadata_protocol_str = NULL; + char *atkey_str = NULL; + + const size_t recv_size = 256; + unsigned char *recv = NULL; + if (!ctx->async_read) { + if ((recv = malloc(sizeof(unsigned char) * recv_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + goto exit; + } + memset(recv, 0, sizeof(unsigned char) * recv_size); + } + size_t recv_len = 0; + + /* + * 3. Get the shared_encrytion_key to use + */ + if (!atclient_atkeys_is_self_encryption_key_base64_initialized(&(ctx->atkeys))) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Self encryption key is not initialized\n"); + goto exit; + } + + if ((ret = atchops_base64_decode((unsigned char *)ctx->atkeys.self_encryption_key_base64, + strlen(ctx->atkeys.self_encryption_key_base64), shared_encryption_key, + shared_encryption_key_size, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); + goto exit; + } + + /* + * 4. Generate IV + */ + if ((ret = atchops_iv_generate(iv)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); + goto exit; + } + + memset(iv_base64, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + if ((ret = atchops_base64_encode(iv, iv_size, iv_base64, iv_base64_size, NULL)) != 0) { + } + + if ((ret = atclient_atkey_metadata_set_iv_nonce(atkey, iv_base64)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce: %d\n", ret); + goto exit; + } + + /* + * 5. Encrypt value + */ + size_t value_encrypted_len = 0; + memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + value_encrypted, value_encrypted_size, &value_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); + goto exit; + } + + size_t value_encrypted_base64_len = 0; + memset(value_encrypted_base64, 0, sizeof(char) * value_encrypted_base64_size); + if ((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, value_encrypted_base64, + value_encrypted_base64_size, &value_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto exit; + } + + /* + * 6. Build update command + */ + + // metadata protocol string + if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); + goto exit; + } + const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); + + // atkey string + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); + goto exit; + } + const size_t atkey_str_len = strlen(atkey_str); + + // update: command + const size_t update_cmd_size = + strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + value_len + strlen("\r\n") + 1; + if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); + goto exit; + } + memset(update_cmd, 0, sizeof(char) * update_cmd_size); + snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + const size_t update_cmd_len = update_cmd_size - 1; + + /* + * 7. Send update command + */ + if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + &recv_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; + } + + if (ctx->async_read) { + goto exit; + } + + char *response = (char *)recv; + + /* + * 5. Receive commit id + */ + if (commit_id != NULL) { + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + + char *response_without_data = response + strlen("data:"); + + *commit_id = atoi(response_without_data); + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int atclient_put_self_key_validate_arguments(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_self_key_request_options *request_options, + int *commit_id) { + int ret = 1; + + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (request_options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); + goto exit; + } + + if (commit_id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { return ret; } +} \ No newline at end of file diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c new file mode 100644 index 00000000..db48e2ff --- /dev/null +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -0,0 +1,259 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TAG "atclient_put_self_key" + +static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, + const char *value, + const atclient_put_shared_key_request_options *request_options, + const int *commit_id); + +int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *value, + const atclient_put_shared_key_request_options *request_options, int *commit_id) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if ((ret = atclient_put_self_key_validate_arguments(ctx, atkey, value, request_options, commit_id)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_self_key_validate_arguments: %d\n", ret); + return ret; + } + + /* + * 2. Variables + */ + char *recipient_atsign_with_at = NULL; + + const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char shared_encryption_key[shared_encryption_key_size]; + + const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(strlen(value)); + unsigned char value_encrypted[value_encrypted_size]; + + const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size); + char value_encrypted_base64[value_encrypted_base64_size]; + + char *update_cmd = NULL; + char *metadata_protocol_str = NULL; + char *atkey_str = NULL; + + const size_t recv_size = 256; + unsigned char *recv = NULL; + if (!ctx->async_read) { + if ((recv = malloc(sizeof(unsigned char) * recv_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); + goto exit; + } + memset(recv, 0, sizeof(unsigned char) * recv_size); + } + size_t recv_len = 0; + + /* + * 3. Get shared_encryption_key to use + */ + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); + goto exit; + } + + if (request_options != NULL && + atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { + memcpy(shared_encryption_key, request_options->shared_encryption_key, shared_encryption_key_size); + } else if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, + shared_encryption_key)) != 0) { + if (ret != ATCLIENT_ERR_AT0015_KEY_NOT_FOUND) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + goto exit; + } + if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, + shared_encryption_key)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); + goto exit; + } + } else { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "could not find shared encryption key\n"); + goto exit; + } + + /* + * 4. Encrypt value + */ + size_t value_encrypted_len = 0; + memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); + if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, value, strlen(value), value_encrypted, + value_encrypted_size, &value_encrypted_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); + goto exit; + } + + size_t value_encrypted_base64_len = 0; + memset(value_encrypted_base64, 0, sizeof(char) * value_encrypted_base64_size); + if((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, value_encrypted_base64, value_encrypted_base64_size, &value_encrypted_base64_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto exit; + } + + /* + * 5. Create update command + */ + if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); + goto exit; + } + const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); + + if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); + goto exit; + } + const size_t atkey_str_len = strlen(atkey_str); + + const size_t update_cmd_size = + strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + value_encrypted_base64_len + strlen("\r\n") + 1; + if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); + goto exit; + } + + snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + const size_t update_cmd_len = update_cmd_size - 1; + + /* + * 6. Send update command + */ + if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + &recv_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; + } + + if (ctx->async_read) { + goto exit; + } + + char *response = (char *)recv; + + /* + * 7. Return commit id + */ + if (commit_id != NULL) { + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + + char *response_without_data = response + strlen("data:"); + + *commit_id = atoi(response_without_data); + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, + const char *value, + const atclient_put_shared_key_request_options *request_options, + const int *commit_id) { + int ret = 1; + + char *shared_by_formatted = NULL; + char *ctx_atsign_formatted = NULL; + + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (value == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (request_options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); + goto exit; + } + + if (commit_id == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); + goto exit; + } + + const atkey_type = atclient_atkey_get_type(atkey); + + if (atkey_type != ATKEY_TYPE_SHAREDKEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATKEY_TYPE_SHAREDKEY\n"); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_key_initialized is false\n"); + goto exit; + } + + if (!atclient_atkey_is_shared_by_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_is_shared_by_initialized is false\n"); + goto exit; + } + + if (!atclient_atkey_is_shared_with_initialized(atkey)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey is_shared_with_initialized is false\n"); + goto exit; + } + + if ((ret = atclient_string_utils_atsign_with_at(ctx->atsign, &ctx_atsign_formatted)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); + goto exit; + } + + if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_by, &shared_by_formatted)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); + goto exit; + } + + if (strcmp(ctx_atsign_formatted, shared_by_formatted) != 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey->shared_by is not equal to ctx->atsign\n"); + goto exit; + } + + ret = 0; + goto exit; +exit: { + free(shared_by_formatted); + free(ctx_atsign_formatted); + return ret; +} +} \ No newline at end of file diff --git a/packages/atclient/src/atclient_utils.c b/packages/atclient/src/atclient_utils.c index 34b21807..184a8039 100644 --- a/packages/atclient/src/atclient_utils.c +++ b/packages/atclient/src/atclient_utils.c @@ -31,8 +31,8 @@ int atclient_utils_find_atserver_address(const char *atdirectory_host, const int goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(atsign, &atsign_without_at_symbol)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_without_at(atsign, &atsign_without_at_symbol)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 5afb7151..5ef89fac 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -195,7 +195,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } } - if (atclient_stringutils_starts_with(token, "public")) { + if (atclient_string_utils_starts_with(token, "public")) { // it is a public key atclient_atkey_metadata_set_is_public(&(atkey->metadata), true); // shift tokens array to the left @@ -205,7 +205,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { ret = 1; goto exit; } - } else if (atclient_stringutils_starts_with(token, "@")) { + } else if (atclient_string_utils_starts_with(token, "@")) { // it is a shared key // set shared_with if ((ret = atclient_atkey_set_shared_with(atkey, token)) != 0) { @@ -218,7 +218,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "token is NULL. %s atkey is probably incomplete\n", atkeystr); goto exit; } - } else if (atclient_stringutils_starts_with(token, "_")) { + } else if (atclient_string_utils_starts_with(token, "_")) { // it is an internal key } else { // it is a self key @@ -277,7 +277,7 @@ int atclient_atkey_from_string(atclient_atkey *atkey, const char *atkeystr) { } tokenlen = strlen(token); - if ((ret = atclient_stringutils_atsign_with_at(token, &shared_by_with_at)) != 0) { + if ((ret = atclient_string_utils_atsign_with_at(token, &shared_by_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed\n"); goto exit; } diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index 88d1b6cc..8eb5e7f6 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -50,13 +50,13 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char * /* * 3. Generate plookup command */ - if ((ret = atclient_stringutils_atsign_with_at(atsign, &atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atsign, &atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_without_at(atsign_with_at, &atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_without_at(atsign_with_at, &atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); goto exit; } @@ -82,8 +82,8 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char * */ char *response = (char *)recv; - if (!atclient_stringutils_starts_with(response, "data:")) { - if (atclient_stringutils_starts_with((char *)recv, "error:AT0015-key not found")) { + if (!atclient_string_utils_starts_with(response, "data:")) { + if (atclient_string_utils_starts_with((char *)recv, "error:AT0015-key not found")) { ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_rsa_decrypt: %d; error:AT0015-key not found\n", ret); goto exit; @@ -156,13 +156,13 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r /* * 3. Build llookup: command */ - if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -196,8 +196,8 @@ int atclient_get_shared_encryption_key_shared_by_me(atclient *ctx, const char *r */ char *response = (char *)recv; - if (!atclient_stringutils_starts_with(response, "data:")) { - if (atclient_stringutils_starts_with(response, "error:AT0015-key not found")) { + if (!atclient_string_utils_starts_with(response, "data:")) { + if (atclient_string_utils_starts_with(response, "error:AT0015-key not found")) { ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d; error:AT0015-key not found\n", ret); goto exit; @@ -277,13 +277,13 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char /* * 3. Build lookup: command */ - if ((ret = atclient_stringutils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(ctx->atsign, &sender_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(recipient_atsign, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -312,8 +312,8 @@ int atclient_get_shared_encryption_key_shared_by_other(atclient *ctx, const char char *response = (char *)recv; char *response_without_data = response + 5; // skip "data:" - if (!atclient_stringutils_starts_with(response, "data:")) { - if (atclient_stringutils_starts_with(response, "error:AT0015-key not found")) { + if (!atclient_string_utils_starts_with(response, "data:")) { + if (atclient_string_utils_starts_with(response, "error:AT0015-key not found")) { ret = ATCLIENT_ERR_AT0015_KEY_NOT_FOUND; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key not found\n", ret); return ret; @@ -435,14 +435,14 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( /* * 2. Ensure atSigns start with `@` symbol */ - if ((ret = atclient_stringutils_atsign_with_at(atclient->atsign, &sharedby_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(atclient->atsign, &sharedby_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } char *sharedby_atsign_without_at = sharedby_atsign_with_at + 1; - if ((ret = atclient_stringutils_atsign_with_at(recipient_atsign, &sharedwith_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if ((ret = atclient_string_utils_atsign_with_at(recipient_atsign, &sharedwith_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } char *sharedwith_atsign_without_at = sharedwith_atsign_with_at + 1; @@ -566,7 +566,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); @@ -583,7 +583,7 @@ int atclient_create_shared_encryption_key_pair_for_me_and_other( goto exit; } - if (!atclient_stringutils_starts_with((char *)recv, "data:")) { + if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", (int)recv_len, recv); diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 715d40a0..61d12b51 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1000,7 +1000,7 @@ size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttl:") // :ttl: - + atclient_stringutils_long_strlen(metadata->ttl); + + atclient_string_utils_long_strlen(metadata->ttl); } size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) { @@ -1011,7 +1011,7 @@ size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttb:") // :ttb: - + atclient_stringutils_long_strlen(metadata->ttb); + + atclient_string_utils_long_strlen(metadata->ttb); } size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) { @@ -1022,7 +1022,7 @@ size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadat return 0; } return strlen(":ttr:") // :ttr: - + atclient_stringutils_long_strlen(metadata->ttr); + + atclient_string_utils_long_strlen(metadata->ttr); } size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) { @@ -1211,17 +1211,17 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad if (atclient_atkey_metadata_is_ttl_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttl:%ld", metadata->ttl); - pos += 5 + atclient_stringutils_long_strlen(metadata->ttl); + pos += 5 + atclient_string_utils_long_strlen(metadata->ttl); } if (atclient_atkey_metadata_is_ttb_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttb:%ld", metadata->ttb); - pos += 5 + atclient_stringutils_long_strlen(metadata->ttb); + pos += 5 + atclient_string_utils_long_strlen(metadata->ttb); } if (atclient_atkey_metadata_is_ttr_initialized(metadata)) { sprintf(*metadata_str + pos, ":ttr:%ld", metadata->ttr); - pos += 5 + atclient_stringutils_long_strlen(metadata->ttr); + pos += 5 + atclient_string_utils_long_strlen(metadata->ttr); } if (atclient_atkey_metadata_is_ccd_initialized(metadata)) { diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 9ffd0104..e60b2ee3 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -547,7 +547,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not // 2. get iv if (atclient_atnotification_is_iv_nonce_initialized(notification) && - !atclient_stringutils_starts_with(notification->iv_nonce, "null")) { + !atclient_string_utils_starts_with(notification->iv_nonce, "null")) { size_t ivlen; ret = atchops_base64_decode((unsigned char *)notification->iv_nonce, strlen(notification->iv_nonce), iv, ATCHOPS_IV_BUFFER_SIZE, &ivlen); diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index b97bb845..4aa43e93 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -66,8 +66,8 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi if (params->shared_encryption_key == NULL) { char *recipient_atsign_with_at = NULL; - if ((ret = atclient_stringutils_atsign_with_at(params->atkey->shared_with, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at failed with code %d\n", + if ((ret = atclient_string_utils_atsign_with_at(params->atkey->shared_with, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at failed with code %d\n", ret); return ret; } @@ -156,7 +156,7 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi goto exit; } // if starts with data: - if (atclient_stringutils_starts_with((char *)recv, "data:")) { + if (atclient_string_utils_starts_with((char *)recv, "data:")) { if (notification_id != NULL) { // if not null, then they care about the notification id // parse the notification id char *data = (char *)recv + strlen("data:"); @@ -222,7 +222,7 @@ static size_t calculate_cmd_size(const atclient_notify_params *params, const siz } if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { - cmdsize += strlen(":ttln:") + atclient_stringutils_long_strlen(params->notification_expiry); // :$ttln + cmdsize += strlen(":ttln:") + atclient_string_utils_long_strlen(params->notification_expiry); // :$ttln } if (atclient_notify_params_is_atkey_initialized(params)) { @@ -312,7 +312,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { snprintf(cmd + off, cmdsize - off, ":ttln:%lu", params->notification_expiry); - off += strlen(":ttln:") + atclient_stringutils_long_strlen(params->notification_expiry); + off += strlen(":ttln:") + atclient_string_utils_long_strlen(params->notification_expiry); } if ((res = atclient_atkey_metadata_to_protocol_str(&(params->atkey->metadata), &metadata_protocol_str)) != 0) { diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 0d9606a8..77e9edb5 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -39,112 +39,6 @@ void atclient_put_self_key_request_options_free(atclient_put_self_key_request_op /* * 2. Free the options */ - if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { - atclient_put_self_key_request_options_unset_shared_encryption_key(options); - } -} - -bool atclient_put_self_key_request_options_is_shared_encryption_key_initialized( - const atclient_put_self_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_self_key_request_options_is_shared_encryption_key_initialized: Invalid arguments\n"); - return false; - } - - /* - * 2. Check if the shared encryption key is initialized - */ - return options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & - ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; -} -void atclient_put_self_key_request_options_set_shared_encryption_key_initialized( - atclient_put_self_key_request_options *options, const bool initialized) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_self_key_request_options_set_shared_encryption_key_initialized: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Set the shared encryption key initialized - */ - if (initialized) { - options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= - ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; - } else { - options->_initialized_fields[ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= - ~ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; - } -} - -int atclient_put_self_key_request_options_set_shared_encryption_key(atclient_put_self_key_request_options *options, - const unsigned char *shared_encryption_key) { - /* - * 1. Validate arguments - */ - int ret = 1; - if (options == NULL || shared_encryption_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_self_key_request_options_set_shared_encryption_key: " - "Invalid arguments\n"); - goto exit; - } - - /* - * 2. Unset the shared encryption key, if necessary - */ - if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { - atclient_put_self_key_request_options_unset_shared_encryption_key(options); - } - - /* - * 3. Set the shared encryption key - */ - const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; - if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == - NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_self_key_request_options_set_shared_encryption_key: " - "Failed to allocate memory for shared encryption key\n"); - goto exit; - } - - atclient_put_self_key_request_options_set_shared_encryption_key_initialized(options, true); - memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_put_self_key_request_options_unset_shared_encryption_key(atclient_put_self_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_self_key_request_options_unset_shared_encryption_key: Invalid arguments\n"); - return; - } - - /* - * 2. Unset the shared encryption key - */ - if (atclient_put_self_key_request_options_is_shared_encryption_key_initialized(options)) { - free(options->shared_encryption_key); - } - options->shared_encryption_key = NULL; - atclient_put_self_key_request_options_set_shared_encryption_key_initialized(options, false); } /* @@ -567,54 +461,54 @@ void atclient_get_self_key_request_options_free(atclient_get_self_key_request_op /* * 2. Free the options */ - if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { - atclient_get_self_key_request_options_unset_shared_encryption_key(options); + if (atclient_get_self_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_self_key_request_options_unset_store_atkey_metadata(options); } } -bool atclient_get_self_key_request_options_is_shared_encryption_key_initialized( +bool atclient_get_self_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_self_key_request_options *options) { /* * 1. Validate arguments */ if (options == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_is_shared_encryption_key_initialized: Invalid arguments\n"); + "atclient_get_self_key_request_options_is_store_atkey_metadata_initialized: Invalid arguments\n"); return false; } /* - * 2. Check if the shared encryption key is initialized + * 2. Check if the store atkey metadata is initialized */ - return options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & - ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + return options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] & + ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; } -void atclient_get_self_key_request_options_set_shared_encryption_key_initialized( +void atclient_get_self_key_request_options_set_store_atkey_metadata_initialized( atclient_get_self_key_request_options *options, const bool initialized) { /* * 1. Validate arguments */ if (options == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_set_shared_encryption_key_initialized: Invalid arguments\n"); + "atclient_get_self_key_request_options_set_store_atkey_metadata_initialized: Invalid arguments\n"); return; } /* - * 2. Set the shared encryption key initialized + * 2. Set the store atkey metadata initialized */ if (initialized) { - options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] |= - ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] |= + ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; } else { - options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] &= - ~ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; + options->_initialized_fields[ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] &= + ~ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; } } -int atclient_get_self_key_request_options_set_shared_encryption_key(atclient_get_self_key_request_options *options, - const unsigned char *shared_encryption_key) { +int atclient_get_self_key_request_options_set_store_atkey_metadata(atclient_get_self_key_request_options *options, + const bool store_atkey_metadata) { int ret = 1; /* @@ -623,63 +517,43 @@ int atclient_get_self_key_request_options_set_shared_encryption_key(atclient_get if (options == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_set_shared_encryption_key: Invalid arguments\n"); - goto exit; - } - - if (shared_encryption_key == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_set_shared_encryption_key: Invalid arguments\n"); - goto exit; + "atclient_get_self_key_request_options_set_store_atkey_metadata: Invalid arguments\n"); + return ret; } /* - * 2. Unset the shared encryption key, if necessary + * 2. Unset the store atkey metadata, if necessary */ - if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { - atclient_get_self_key_request_options_unset_shared_encryption_key(options); + if (atclient_get_self_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_self_key_request_options_unset_store_atkey_metadata(options); } /* - * 3. Set the shared encryption key + * 3. Set the store atkey metadata */ - const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; - if ((options->shared_encryption_key = (unsigned char *)malloc(sizeof(unsigned char) * shared_encryption_key_size)) == - NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_set_shared_encryption_key: Failed to allocate memory for " - "shared encryption key\n"); - goto exit; - } - - atclient_get_self_key_request_options_set_shared_encryption_key_initialized(options, true); - memcpy(options->shared_encryption_key, shared_encryption_key, shared_encryption_key_size); + options->store_atkey_metadata = store_atkey_metadata; + atclient_get_self_key_request_options_set_store_atkey_metadata_initialized(options, true); ret = 0; goto exit; exit: { return ret; } } -void atclient_get_self_key_request_options_unset_shared_encryption_key(atclient_get_self_key_request_options *options) { +void atclient_get_self_key_request_options_unset_store_atkey_metadata(atclient_get_self_key_request_options *options) { /* * 1. Validate arguments */ if (options == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_self_key_request_options_unset_shared_encryption_key: Invalid arguments\n"); + "atclient_get_self_key_request_options_unset_store_atkey_metadata: Invalid arguments\n"); return; } /* - * 2. Unset the shared encryption key + * 2. Unset the store atkey metadata */ - if (atclient_get_self_key_request_options_is_shared_encryption_key_initialized(options)) { - free(options->shared_encryption_key); - } - options->shared_encryption_key = NULL; - atclient_get_self_key_request_options_set_shared_encryption_key_initialized(options, false); + options->store_atkey_metadata = false; + atclient_get_self_key_request_options_set_store_atkey_metadata_initialized(options, false); } /* @@ -835,6 +709,101 @@ void atclient_get_shared_key_request_options_unset_shared_encryption_key( atclient_get_shared_key_request_options_set_shared_encryption_key_initialized(options, false); } +bool atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized( + const atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the store atkey metadata is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] & + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; +} + +void atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized( + atclient_get_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the store atkey metadata initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] |= + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX] &= + ~ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED; + } +} + +int atclient_get_shared_key_request_options_set_store_atkey_metadata(atclient_get_shared_key_request_options *options, + const bool store_atkey_metadata) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_store_atkey_metadata: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the store atkey metadata, if necessary + */ + if (atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_shared_key_request_options_unset_store_atkey_metadata(options); + } + + /* + * 3. Set the store atkey metadata + */ + options->store_atkey_metadata = store_atkey_metadata; + atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_shared_key_request_options_unset_store_atkey_metadata( + atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_unset_store_atkey_metadata: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the store atkey metadata + */ + options->store_atkey_metadata = false; + atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized(options, false); +} + /* * ================= * 2C. Get PublicKey diff --git a/packages/atclient/src/stringutils.c b/packages/atclient/src/string_utils.c similarity index 85% rename from packages/atclient/src/stringutils.c rename to packages/atclient/src/string_utils.c index 88b3648f..1642e395 100644 --- a/packages/atclient/src/stringutils.c +++ b/packages/atclient/src/string_utils.c @@ -5,7 +5,7 @@ #include #include -int atclient_stringutils_trim_whitespace(const char *string, const size_t string_len, char *out, const size_t out_size, +int atclient_string_utils_trim_whitespace(const char *string, const size_t string_len, char *out, const size_t out_size, size_t *out_len) { int ret = 1; @@ -45,11 +45,11 @@ int atclient_stringutils_trim_whitespace(const char *string, const size_t string exit: { return ret; } } -bool atclient_stringutils_starts_with(const char *string, const char *prefix) { +bool atclient_string_utils_starts_with(const char *string, const char *prefix) { return strncmp(string, prefix, strlen(prefix)) == 0; } -bool atclient_stringutils_ends_with(const char *string, const char *suffix) { +bool atclient_string_utils_ends_with(const char *string, const char *suffix) { const size_t string_len = strlen(string); const size_t suffix_len = strlen(suffix); if (suffix_len > string_len) { @@ -59,7 +59,7 @@ bool atclient_stringutils_ends_with(const char *string, const char *suffix) { } -int atclient_stringutils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol) { +int atclient_string_utils_atsign_with_at(const char *original_atsign, char **output_atsign_with_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; @@ -100,7 +100,7 @@ int atclient_stringutils_atsign_with_at(const char *original_atsign, char **outp exit: { return ret; } } -int atclient_stringutils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol) { +int atclient_string_utils_atsign_without_at(const char *original_atsign, char **output_atsign_without_at_symbol) { int ret = -1; if (original_atsign == NULL) { ret = -1; @@ -130,7 +130,7 @@ int atclient_stringutils_atsign_without_at(const char *original_atsign, char **o exit: { return ret; } } -int atclient_stringutils_long_strlen(long n) { +int atclient_string_utils_long_strlen(long n) { // could use log10 for this, but it's probably slower... size_t len = 0; diff --git a/packages/atclient/tests/test_stringutils.c b/packages/atclient/tests/test_stringutils.c index 9c1145e3..bd0c3599 100644 --- a/packages/atclient/tests/test_stringutils.c +++ b/packages/atclient/tests/test_stringutils.c @@ -43,7 +43,7 @@ static int test_1_starts_with() { const char *string = "@bob"; // 1a. @bob starts with @ - if (atclient_stringutils_starts_with(string, "@") != true) { + if (atclient_string_utils_starts_with(string, "@") != true) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "@"); @@ -51,7 +51,7 @@ static int test_1_starts_with() { } // 1b. @bob does not start with 123 - if (atclient_stringutils_starts_with(string, "123") != false) { + if (atclient_string_utils_starts_with(string, "123") != false) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_starts_with: %d | %s starts with %s\n", ret, string, "bob"); ret = 1; @@ -74,17 +74,17 @@ static int test_2_ends_with() { const char *string = "root.atsign.org:64"; // 2a. root.atsign.org:64 ends with 64 - if (atclient_stringutils_ends_with(string, "64") != true) { + if (atclient_string_utils_ends_with(string, "64") != true) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_ends_with: %d | %s ends with %s\n", ret, string, "64"); goto exit; } // 2b. root.atsign.org:64 does not end with org - if (atclient_stringutils_ends_with(string, "org") != 0) { + if (atclient_string_utils_ends_with(string, "org") != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_ends_with: %d | %s ends with %s\n", ret, + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_ends_with: %d | %s ends with %s\n", ret, string, "org"); goto exit; } @@ -109,15 +109,15 @@ static int test_3_trim_whitespace() { size_t outlen = 0; const char *expectedresult = "scan jeremy_0"; - if ((ret = atclient_stringutils_trim_whitespace(string, strlen(string), out, outsize, &outlen)) != 0) { + if ((ret = atclient_string_utils_trim_whitespace(string, strlen(string), out, outsize, &outlen)) != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: %d | %s\n", ret, string); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_trim_whitespace: %d | %s\n", ret, string); goto exit; } if (strcmp(out, expectedresult) != 0) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_trim_whitespace: \"%s\" != \"%s\"\n", string, + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_trim_whitespace: \"%s\" != \"%s\"\n", string, expectedresult); goto exit; } diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 85bce0f7..6273808e 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -102,7 +102,7 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const goto exit; } - if (!atclient_stringutils_starts_with(recv, "data:")) { + if (!atclient_string_utils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "publickey does not exist: \"%s\"\n", recv); goto exit; @@ -157,7 +157,7 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c goto exit; } - if (!atclient_stringutils_starts_with(recv, "data:")) { + if (!atclient_string_utils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "selfkey does not exist: \"%s\"\n", recv); goto exit; @@ -212,7 +212,7 @@ int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const goto exit; } - if (!atclient_stringutils_starts_with(recv, "data:")) { + if (!atclient_string_utils_starts_with(recv, "data:")) { ret = false; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "sharedkey does not exist: \"%s\"\n", recv); goto exit; @@ -248,23 +248,23 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re char *recipient_atsign_with_at = NULL; char *recipient_atsign_without_at = NULL; - if((ret = atclient_stringutils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if((ret = atclient_string_utils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if((ret = atclient_stringutils_atsign_without_at(atclient1->atsign, &client_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if((ret = atclient_string_utils_atsign_without_at(atclient1->atsign, &client_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); goto exit; } - if((ret = atclient_stringutils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_with_at: %d\n", ret); + if((ret = atclient_string_utils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if((ret = atclient_stringutils_atsign_without_at(recipient, &recipient_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_atsign_without_at: %d\n", ret); + if((ret = atclient_string_utils_atsign_without_at(recipient, &recipient_atsign_without_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index b2fff6bb..bf6ad7b0 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -149,8 +149,8 @@ static int test_2_get(atclient *atclient) { goto exit; } - if ((ret = atclient_get_publickey(atclient, &atkey, value, valuesize, &valuelen, false)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey\n"); + if ((ret = atclient_get_public_key(atclient, &atkey, value, valuesize, &valuelen, false)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); @@ -271,8 +271,8 @@ static int test_6_get_with_metadata(atclient *atclient) { goto exit; } - if ((ret = atclient_get_publickey(atclient, &atkey, value, valuesize, &valuelen, true)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_publickey\n"); + if ((ret = atclient_get_public_key(atclient, &atkey, value, valuesize, &valuelen, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } @@ -417,7 +417,7 @@ static int tear_down(atclient *atclient) { goto exit; } - if (!atclient_stringutils_starts_with(recv, "data:")) { + if (!atclient_string_utils_starts_with(recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Failed to delete: \"%.*s\"\n", (int)recvlen, recv); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index fea9493b..ef06fc41 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -171,7 +171,7 @@ static int test_3_get(atclient *atclient) { goto exit; } - if ((ret = atclient_get_selfkey(atclient, &atkey, value, valuesize, &valuelen)) != 0) { + if ((ret = atclient_get_self_key(atclient, &atkey, value, valuesize, &valuelen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -291,7 +291,7 @@ static int test_7_get_with_metadata(atclient *atclient) { goto exit; } - if ((ret = atclient_get_selfkey(atclient, &atkey, value, valuesize, &valuelen)) != 0) { + if ((ret = atclient_get_self_key(atclient, &atkey, value, valuesize, &valuelen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -420,7 +420,7 @@ static int tear_down(atclient *atclient) { goto exit; } - if (!atclient_stringutils_starts_with(recv, "data:")) { + if (!atclient_string_utils_starts_with(recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 5ca84d96..89eaffc6 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -161,7 +161,7 @@ static int test_2_get_as_sharedby(atclient *atclient) { goto exit; } - if ((ret = atclient_get_sharedkey(atclient, &atkey, value, valuesize, &valuelen, NULL)) != 0) { + if ((ret = atclient_get_shared_key(atclient, &atkey, value, valuesize, &valuelen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -217,7 +217,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { goto exit; } - if ((ret = atclient_get_sharedkey(atclient2, &atkey, value, valuesize, &valuelen, NULL)) != 0) { + if ((ret = atclient_get_shared_key(atclient2, &atkey, value, valuesize, &valuelen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } From 01514fde61b501ac40cea708bebac1d59cccbe99 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:04:04 -0400 Subject: [PATCH 132/193] fix: failing tests --- packages/atclient/include/atclient/atclient.h | 2 +- .../{stringutils.h => string_utils.h} | 0 packages/atclient/src/atclient.c | 2 +- packages/atclient/src/atclient_delete.c | 2 +- packages/atclient/src/atclient_get_atkeys.c | 2 +- .../atclient/src/atclient_get_public_key.c | 2 +- packages/atclient/src/atclient_get_self_key.c | 6 +- .../atclient/src/atclient_get_shared_key.c | 2 +- packages/atclient/src/atclient_put.c | 2 +- .../atclient/src/atclient_put_public_key.c | 40 ++++------ packages/atclient/src/atclient_put_self_key.c | 41 +++++----- .../atclient/src/atclient_put_shared_key.c | 76 +++++++++++-------- packages/atclient/src/atclient_utils.c | 2 +- packages/atclient/src/atkey.c | 2 +- .../atclient/src/encryption_key_helpers.c | 2 +- packages/atclient/src/metadata.c | 2 +- packages/atclient/src/monitor.c | 2 +- packages/atclient/src/notify.c | 2 +- packages/atclient/src/request_options.c | 1 + packages/atclient/src/string_utils.c | 2 +- packages/atclient/tests/test_stringutils.c | 2 +- tests/functional_tests/lib/src/helpers.c | 6 +- .../tests/test_atclient_monitor.c | 2 +- .../tests/test_atclient_notify.c | 2 +- .../tests/test_atclient_publickey.c | 10 +-- .../tests/test_atclient_selfkey.c | 10 +-- .../tests/test_atclient_sharedkey.c | 4 +- 27 files changed, 112 insertions(+), 116 deletions(-) rename packages/atclient/include/atclient/{stringutils.h => string_utils.h} (100%) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 09ae2a6f..11898978 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -145,7 +145,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const i * don't care about the commit_id * @return int 0 on success */ -int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int **commit_id); +int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int *commit_id); /** * @brief Put a string value into a self key into your atServer. Putting a self key is a private value and is encrypted only for you diff --git a/packages/atclient/include/atclient/stringutils.h b/packages/atclient/include/atclient/string_utils.h similarity index 100% rename from packages/atclient/include/atclient/stringutils.h rename to packages/atclient/include/atclient/string_utils.h diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 3f717fa0..945b6c57 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -6,7 +6,7 @@ #include "atclient/atkeys.h" #include "atclient/connection.h" #include "atclient/constants.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/atclient_delete.c b/packages/atclient/src/atclient_delete.c index 23e2716f..9fb3f8d0 100644 --- a/packages/atclient/src/atclient_delete.c +++ b/packages/atclient/src/atclient_delete.c @@ -1,7 +1,7 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" #include "atclient/constants.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 78652bf4..f1d53b31 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/packages/atclient/src/atclient_get_public_key.c b/packages/atclient/src/atclient_get_public_key.c index f36a6419..e9596970 100644 --- a/packages/atclient/src/atclient_get_public_key.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -1,7 +1,7 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" #include "atclient/constants.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include diff --git a/packages/atclient/src/atclient_get_self_key.c b/packages/atclient/src/atclient_get_self_key.c index ebc06d79..ef8a0a31 100644 --- a/packages/atclient/src/atclient_get_self_key.c +++ b/packages/atclient/src/atclient_get_self_key.c @@ -1,7 +1,7 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" #include "atclient/constants.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include @@ -17,7 +17,7 @@ static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, const char *value, const size_t value_size, const size_t *value_len); -int atclient_get_selfkey(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, +int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len) { int ret = 1; @@ -204,7 +204,7 @@ static int atclient_get_self_key_valid_arguments(const atclient *atclient, const if (atclient->async_read) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_selfkey cannot be called from an async_read atclient, it will cause a race condition\n"); + "atclient_get_self_key cannot be called from an async_read atclient, it will cause a race condition\n"); return 1; } diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index 98d76cc8..975f4620 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -1,6 +1,6 @@ #include "atclient/atclient.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index 3efae955..ce521ca7 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -2,7 +2,7 @@ #include "atclient/atkey.h" #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/atclient_put_public_key.c b/packages/atclient/src/atclient_put_public_key.c index b40cf591..06939b49 100644 --- a/packages/atclient/src/atclient_put_public_key.c +++ b/packages/atclient/src/atclient_put_public_key.c @@ -55,7 +55,7 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va goto exit; } - if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); goto exit; } @@ -64,19 +64,19 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); const size_t update_cmd_size = - strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + strlen(value) + strlen("\r\n") + 1; + strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + strlen(value) + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); goto exit; } - snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", metadata_protocol_str, atkey_str, value); const size_t update_cmd_len = update_cmd_size - 1; /* * 4. Send update command */ - if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -88,19 +88,19 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va char *response = (char *)recv; + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + + char *response_without_data = response + strlen("data:"); + /* * 5. Receive commit id */ if (commit_id != NULL) { - if (!atclient_string_utils_starts_with(response, "data:")) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); - goto exit; - } - - char *response_without_data = response + strlen("data:"); - *commit_id = atoi(response_without_data); } @@ -124,6 +124,8 @@ static int atclient_put_public_key_validate_arguments(const atclient *ctx, const goto exit; } + // TODO atclient checks + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); @@ -136,18 +138,6 @@ static int atclient_put_public_key_validate_arguments(const atclient *ctx, const goto exit; } - if (request_options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); - goto exit; - } - - if (commit_id == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); - goto exit; - } - const atkey_type = atclient_atkey_get_type(atkey); if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index 9e517ade..cb1658a6 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -119,7 +120,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu */ // metadata protocol string - if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); goto exit; } @@ -134,20 +135,20 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu // update: command const size_t update_cmd_size = - strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + value_len + strlen("\r\n") + 1; + strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + value_len + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); goto exit; } memset(update_cmd, 0, sizeof(char) * update_cmd_size); - snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", atkey_str, metadata_protocol_str, value); const size_t update_cmd_len = update_cmd_size - 1; /* * 7. Send update command */ - if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -159,19 +160,19 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu char *response = (char *)recv; + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + + char *response_without_data = response + strlen("data:"); + /* * 5. Receive commit id */ if (commit_id != NULL) { - if (!atclient_string_utils_starts_with(response, "data:")) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); - goto exit; - } - - char *response_without_data = response + strlen("data:"); - *commit_id = atoi(response_without_data); } @@ -191,6 +192,8 @@ static int atclient_put_self_key_validate_arguments(atclient *ctx, atclient_atke goto exit; } + // TODO atclient checks + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); @@ -203,17 +206,7 @@ static int atclient_put_self_key_validate_arguments(atclient *ctx, atclient_atke goto exit; } - if (request_options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); - goto exit; - } - - if (commit_id == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); - goto exit; - } + // TODO more checks ret = 0; goto exit; diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index db48e2ff..3458fa8c 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -10,7 +11,7 @@ #include #include -#define TAG "atclient_put_self_key" +#define TAG "atclient_put_shared_key" static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, const char *value, @@ -24,8 +25,8 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va /* * 1. Validate arguments */ - if ((ret = atclient_put_self_key_validate_arguments(ctx, atkey, value, request_options, commit_id)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_self_key_validate_arguments: %d\n", ret); + if ((ret = atclient_put_shared_key_validate_arguments(ctx, atkey, value, request_options, commit_id)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_shared_key_validate_arguments: %d\n", ret); return ret; } @@ -37,6 +38,12 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; unsigned char shared_encryption_key[shared_encryption_key_size]; + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; + unsigned char iv[iv_size]; + + const size_t iv_base64_size = atchops_base64_encoded_size(iv_size); + char iv_base64[iv_base64_size]; + const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(strlen(value)); unsigned char value_encrypted[value_encrypted_size]; @@ -88,12 +95,29 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va goto exit; } + if((ret = atchops_iv_generate(iv)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); + goto exit; + } + + memset(iv_base64, 0, sizeof(char) * iv_base64_size); + if((ret = atchops_base64_encode(iv, iv_size, (unsigned char *) iv_base64, iv_base64_size, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto exit; + } + + if((ret = atclient_atkey_metadata_set_iv_nonce(&(atkey->metadata), iv_base64)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce: %d\n", ret); + goto exit; + } + /* * 4. Encrypt value */ + const size_t value_len = strlen(value); size_t value_encrypted_len = 0; memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); - if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, value, strlen(value), value_encrypted, + if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, value, value_len, value_encrypted, value_encrypted_size, &value_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto exit; @@ -101,7 +125,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va size_t value_encrypted_base64_len = 0; memset(value_encrypted_base64, 0, sizeof(char) * value_encrypted_base64_size); - if((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, value_encrypted_base64, value_encrypted_base64_size, &value_encrypted_base64_len)) != 0) { + if((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, (unsigned char *) value_encrypted_base64, value_encrypted_base64_size, &value_encrypted_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } @@ -109,7 +133,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va /* * 5. Create update command */ - if ((ret = atclient_atkey_metadata_to_protocol_str(atkey, &metadata_protocol_str)) != 0) { + if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); goto exit; } @@ -122,20 +146,20 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t atkey_str_len = strlen(atkey_str); const size_t update_cmd_size = - strlen("update:") + atkey_str_len + metadata_protocol_str_len + strlen(": ") + value_encrypted_base64_len + strlen("\r\n") + 1; + strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + value_encrypted_base64_len + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); goto exit; } - snprintf(update_cmd, update_cmd_size, "update:%s%s: %s\r\n", atkey_str, metadata_protocol_str, value); + snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", metadata_protocol_str, atkey_str, value_encrypted_base64); const size_t update_cmd_len = update_cmd_size - 1; /* * 6. Send update command */ - if ((ret = atclient_connection_send(&ctx->atserver_connection, update_cmd, update_cmd_len, recv, recv_size, + if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; @@ -146,20 +170,18 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } char *response = (char *)recv; + if (!atclient_string_utils_starts_with(response, "data:")) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", + (int)recv_len, recv); + goto exit; + } + char *response_without_data = response + strlen("data:"); /* * 7. Return commit id */ if (commit_id != NULL) { - if (!atclient_string_utils_starts_with(response, "data:")) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); - goto exit; - } - - char *response_without_data = response + strlen("data:"); - *commit_id = atoi(response_without_data); } @@ -183,6 +205,8 @@ static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const goto exit; } + // TODO atclient checks + if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); @@ -195,21 +219,9 @@ static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const goto exit; } - if (request_options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "request_options is NULL\n"); - goto exit; - } - - if (commit_id == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "commit_id is NULL\n"); - goto exit; - } - - const atkey_type = atclient_atkey_get_type(atkey); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); - if (atkey_type != ATKEY_TYPE_SHAREDKEY) { + if (atkey_type != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey_type is not ATKEY_TYPE_SHAREDKEY\n"); goto exit; diff --git a/packages/atclient/src/atclient_utils.c b/packages/atclient/src/atclient_utils.c index 184a8039..2c47bb30 100644 --- a/packages/atclient/src/atclient_utils.c +++ b/packages/atclient/src/atclient_utils.c @@ -1,7 +1,7 @@ #include "atclient/atclient_utils.h" #include "atclient/atkeys.h" #include "atclient/connection.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include diff --git a/packages/atclient/src/atkey.c b/packages/atclient/src/atkey.c index 5ef89fac..4e7700d1 100644 --- a/packages/atclient/src/atkey.c +++ b/packages/atclient/src/atkey.c @@ -1,7 +1,7 @@ #include "atclient/atkey.h" #include "atclient/constants.h" #include "atclient/metadata.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/encryption_key_helpers.c b/packages/atclient/src/encryption_key_helpers.c index 8eb5e7f6..5426483f 100644 --- a/packages/atclient/src/encryption_key_helpers.c +++ b/packages/atclient/src/encryption_key_helpers.c @@ -4,7 +4,7 @@ #include "atclient/atclient.h" #include "atclient/atkeys.h" #include "atclient/constants.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include diff --git a/packages/atclient/src/metadata.c b/packages/atclient/src/metadata.c index 61d12b51..ed73745b 100644 --- a/packages/atclient/src/metadata.c +++ b/packages/atclient/src/metadata.c @@ -1,5 +1,5 @@ #include "atclient/metadata.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include "cJSON.h" #include diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index e60b2ee3..8fc63844 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -4,7 +4,7 @@ #include "atclient/connection.h" #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "cJSON.h" #include #include diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 4aa43e93..3d647e92 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -2,7 +2,7 @@ #include "atclient/connection.h" #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 77e9edb5..8b45c71a 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -4,6 +4,7 @@ #include #include #include +#include #define TAG "request_options" diff --git a/packages/atclient/src/string_utils.c b/packages/atclient/src/string_utils.c index 1642e395..d0a75762 100644 --- a/packages/atclient/src/string_utils.c +++ b/packages/atclient/src/string_utils.c @@ -1,4 +1,4 @@ -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include #include #include diff --git a/packages/atclient/tests/test_stringutils.c b/packages/atclient/tests/test_stringutils.c index bd0c3599..6ec019c7 100644 --- a/packages/atclient/tests/test_stringutils.c +++ b/packages/atclient/tests/test_stringutils.c @@ -1,4 +1,4 @@ -#include "atclient/stringutils.h" +#include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include #include // IWYU pragma: keep diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 6273808e..30b04462 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include @@ -284,13 +284,13 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re goto exit; } - if ((ret = atclient_delete(atclient1, &atkeyforme, NULL)) != 0) { + if ((ret = atclient_delete(atclient1, &atkeyforme, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "deleted shared enc key for me\n"); - if ((ret = atclient_delete(atclient1, &atkeyforthem, NULL)) != 0) { + if ((ret = atclient_delete(atclient1, &atkeyforthem, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 12418942..e1a96f2b 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tests/functional_tests/tests/test_atclient_notify.c b/tests/functional_tests/tests/test_atclient_notify.c index b2e38a2d..5c931c1d 100644 --- a/tests/functional_tests/tests/test_atclient_notify.c +++ b/tests/functional_tests/tests/test_atclient_notify.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index bf6ad7b0..9ed8dd41 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -1,7 +1,7 @@ #include "functional_tests/config.h" #include "functional_tests/helpers.h" #include -#include +#include #include #include #include @@ -116,7 +116,7 @@ static int test_1_put(atclient *atclient) { goto exit; } - if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + if ((ret = atclient_put_public_key(atclient, &atkey, ATKEY_VALUE, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put\n"); goto exit; } @@ -183,7 +183,7 @@ static int test_3_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } @@ -238,7 +238,7 @@ static int test_5_put_with_metadata(atclient *atclient) { atclient_atkey_metadata_set_is_encrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); atclient_atkey_metadata_set_is_binary(&(atkey.metadata), ATKEY_ISBINARY); - if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + if ((ret = atclient_put_public_key(atclient, &atkey, ATKEY_VALUE, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_put\n"); goto exit; } @@ -340,7 +340,7 @@ static int test_7_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_delete\n"); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index ef06fc41..61fe5a44 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -1,7 +1,7 @@ #include "functional_tests/config.h" #include "functional_tests/helpers.h" #include -#include +#include #include #include #include @@ -138,7 +138,7 @@ static int test_2_put(atclient *atclient) { goto exit; } - if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + if ((ret = atclient_put_self_key(atclient, &atkey, ATKEY_VALUE, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); goto exit; } @@ -207,7 +207,7 @@ static int test_4_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } @@ -258,7 +258,7 @@ static int test_6_put_with_metadata(atclient *atclient) { atclient_atkey_metadata_set_is_encrypted(&(atkey.metadata), ATKEY_ISENCRYPTED); atclient_atkey_metadata_set_is_binary(&(atkey.metadata), ATKEY_ISBINARY); - if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + if ((ret = atclient_put_self_key(atclient, &atkey, ATKEY_VALUE, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); goto exit; } @@ -349,7 +349,7 @@ static int test_8_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 89eaffc6..3927f40f 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -128,7 +128,7 @@ static int test_1_put(atclient *atclient) { atclient_atkey_metadata_set_ttl(&atkey.metadata, ATKEY_TTL); atclient_atkey_metadata_set_ttr(&atkey.metadata, ATKEY_TTR); - if ((ret = atclient_put(atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL)) != 0) { + if ((ret = atclient_put_shared_key(atclient, &atkey, ATKEY_VALUE, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put: %d\n", ret); goto exit; } @@ -268,7 +268,7 @@ static int test_4_delete(atclient *atclient) { goto exit; } - if ((ret = atclient_delete(atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d\n", ret); goto exit; } From da9fef3935a741467efe0dd23ba6d4ec8295c099 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:07:46 -0400 Subject: [PATCH 133/193] fix: updated CMakeLists.txt --- packages/atclient/CMakeLists.txt | 11 +++++++---- packages/atclient/src/atclient_get_public_key.c | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index f36e351b..a8f3b5ba 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -7,9 +7,12 @@ set( ATCLIENT_SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/atclient_delete.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_atkeys.c - ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_publickey.c - ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_selfkey.c - ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_sharedkey.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_public_key.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_self_key.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_get_shared_key.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_public_key.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_self_key.c + ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_shared_key.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_utils.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient.c @@ -24,7 +27,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/notify.c ${CMAKE_CURRENT_LIST_DIR}/src/notify_params.c ${CMAKE_CURRENT_LIST_DIR}/src/request_options.c - ${CMAKE_CURRENT_LIST_DIR}/src/stringutils.c + ${CMAKE_CURRENT_LIST_DIR}/src/string_utils.c ) # Project setup diff --git a/packages/atclient/src/atclient_get_public_key.c b/packages/atclient/src/atclient_get_public_key.c index e9596970..5b6bb065 100644 --- a/packages/atclient/src/atclient_get_public_key.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -6,7 +6,7 @@ #include #include -#define TAG "atclient_get_publickey" +#define TAG "atclient_get_public_key" static int atclient_get_public_key_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, size_t *value_len, bool bypass_cache); From e62338f5e70b179a5699e3c8b0c5526bb9877454 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:11:43 -0400 Subject: [PATCH 134/193] fix: --- tests/functional_tests/lib/src/helpers.c | 33 ++++++++++++++---------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 30b04462..9f0bd454 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -232,6 +232,23 @@ exit: { return ret; } int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *recipient) { int ret = 1; + if(atclient1 == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if(recipient == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recipient is NULL\n"); + goto exit; + } + + if(!atclient_is_atsign_initialized(atclient1)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is not initialized\n"); + goto exit; + } + + // TODO check if pkam authenticated + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down Begin\n"); char atkeystrtemp[ATCLIENT_ATKEY_FULL_LEN]; @@ -243,30 +260,20 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re atclient_atkey_init(&atkeyforthem); char *client_atsign_with_at = NULL; - char *client_atsign_without_at = NULL; - char *recipient_atsign_with_at = NULL; - char *recipient_atsign_without_at = NULL; if((ret = atclient_string_utils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if((ret = atclient_string_utils_atsign_without_at(atclient1->atsign, &client_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); - goto exit; - } - if((ret = atclient_string_utils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if((ret = atclient_string_utils_atsign_without_at(recipient, &recipient_atsign_without_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_without_at: %d\n", ret); - goto exit; - } + char *client_atsign_without_at = client_atsign_with_at + 1; + char *recipient_atsign_without_at = recipient_atsign_with_at + 1; memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", recipient_atsign_without_at, client_atsign_with_at); @@ -303,9 +310,7 @@ exit: { atclient_atkey_free(&atkeyforme); atclient_atkey_free(&atkeyforthem); free(client_atsign_with_at); - free(client_atsign_without_at); free(recipient_atsign_with_at); - free(recipient_atsign_without_at); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down End (%d)\n", ret); return ret; } From 50b2e32cfd074c08943eba9ff638eee01b719039 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:13:10 -0400 Subject: [PATCH 135/193] fix: examples --- examples/desktop/crud/delete.c | 2 +- examples/desktop/crud/put_publickey.c | 2 +- examples/desktop/crud/put_selfkey.c | 2 +- examples/desktop/crud/put_sharedkey.c | 2 +- examples/desktop/repl/src/main.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/desktop/crud/delete.c b/examples/desktop/crud/delete.c index f1da16a2..8312d28b 100644 --- a/examples/desktop/crud/delete.c +++ b/examples/desktop/crud/delete.c @@ -57,7 +57,7 @@ int main() { goto exit; } - if ((ret = atclient_delete(&atclient, &atkey, NULL)) != 0) { + if ((ret = atclient_delete(&atclient, &atkey, NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to delete"); goto exit; } diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index b5164a99..dc8b4544 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -77,7 +77,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { + if ((ret = atclient_put_public_key(&atclient, &atkey, ATKEY_VALUE, NULL, NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); goto exit; } diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index ea818d3f..2fe461b5 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -76,7 +76,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { + if ((ret = atclient_put_self_key(&atclient, &atkey, ATKEY_VALUE, NULL, NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); goto exit; } diff --git a/examples/desktop/crud/put_sharedkey.c b/examples/desktop/crud/put_sharedkey.c index 8c8c7bc7..e8a639be 100644 --- a/examples/desktop/crud/put_sharedkey.c +++ b/examples/desktop/crud/put_sharedkey.c @@ -77,7 +77,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Putting atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - if ((ret = atclient_put(&atclient, &atkey, ATKEY_VALUE, strlen(ATKEY_VALUE), NULL) != 0)) { + if ((ret = atclient_put_shared_key(&atclient, &atkey, ATKEY_VALUE, NULL, NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put public key"); goto exit; } diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 5ab9ee7c..4ee3a64c 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -212,7 +212,7 @@ int main(int argc, char *argv[]) { } char *atkeystr = NULL; for(size_t i = 0; i < arrlen; i++) { - if((ret = atclient_delete(&atclient, &arr[i], NULL)) != 0) { + if((ret = atclient_delete(&atclient, &arr[i], NULL, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_delete: %d | failed to delete atKey\n", ret); continue; } From b02188410e9c8b9dcb3ce3ad1a9a2657910cd0b5 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:14:01 -0400 Subject: [PATCH 136/193] fix: self key --- packages/atclient/src/atclient_put_self_key.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index cb1658a6..14f6c7f3 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -135,14 +135,14 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu // update: command const size_t update_cmd_size = - strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + value_len + strlen("\r\n") + 1; + strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + value_encrypted_base64_len + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); goto exit; } memset(update_cmd, 0, sizeof(char) * update_cmd_size); - snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", atkey_str, metadata_protocol_str, value); + snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", atkey_str, metadata_protocol_str, value_encrypted_base64); const size_t update_cmd_len = update_cmd_size - 1; /* From 82a402da2b62d616fb51bc1ef91679cdd11d8e61 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:18:42 -0400 Subject: [PATCH 137/193] fix: warnings --- .../atclient/include/atclient/notify_params.h | 24 +++++++++---------- packages/atclient/src/notify_params.c | 22 ++++++++--------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/atclient/include/atclient/notify_params.h b/packages/atclient/include/atclient/notify_params.h index 90a10a64..69c81878 100644 --- a/packages/atclient/include/atclient/notify_params.h +++ b/packages/atclient/include/atclient/notify_params.h @@ -100,18 +100,18 @@ typedef struct atclient_notify_params { void atclient_notify_params_init(atclient_notify_params *params); void atclient_notify_params_free(atclient_notify_params *params); -bool atclient_notify_params_is_id_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_atkey_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_value_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_should_encrypt_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_operation_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_message_type_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_priority_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_latest_n_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_notifier_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_notification_expiry_initialized(atclient_notify_params *params); -bool atclient_notify_params_is_shared_encryption_key_initialized(atclient_notify_params *params); +bool atclient_notify_params_is_id_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_atkey_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_value_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_should_encrypt_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_operation_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_message_type_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_priority_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_strategy_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_latest_n_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_notifier_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_notification_expiry_initialized(const atclient_notify_params *params); +bool atclient_notify_params_is_shared_encryption_key_initialized(const atclient_notify_params *params); void atclient_notify_params_set_id_initialized(atclient_notify_params *params, const bool initialized); void atclient_notify_params_set_atkey_initialized(atclient_notify_params *params, const bool initialized); diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c index c21915a3..df3aeeea 100644 --- a/packages/atclient/src/notify_params.c +++ b/packages/atclient/src/notify_params.c @@ -40,34 +40,34 @@ void atclient_notify_params_free(atclient_notify_params *params) { memset(params, 0, sizeof(atclient_notify_params)); } -bool atclient_notify_params_is_id_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_id_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ID_INDEX] & ATCLIENT_NOTIFY_PARAMS_ID_INITIALIZED; } -bool atclient_notify_params_is_atkey_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_atkey_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_ATKEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_ATKEY_INITIALIZED; } -bool atclient_notify_params_is_value_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_value_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_VALUE_INDEX] & ATCLIENT_NOTIFY_PARAMS_VALUE_INITIALIZED; } -bool atclient_notify_params_is_should_encrypt_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_should_encrypt_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INDEX] & ATCLIENT_NOTIFY_PARAMS_SHOULD_ENCRYPT_INITIALIZED; } -bool atclient_notify_params_is_operation_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_operation_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_OPERATION_INDEX] & ATCLIENT_NOTIFY_PARAMS_OPERATION_INITIALIZED; } -bool atclient_notify_params_is_message_type_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_message_type_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INDEX] & ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; } -bool atclient_notify_params_is_priority_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_priority_initialized(const const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] & ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; } @@ -77,22 +77,22 @@ bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *para ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; } -bool atclient_notify_params_is_latest_n_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_latest_n_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_LATEST_N_INDEX] & ATCLIENT_NOTIFY_PARAMS_LATEST_N_INITIALIZED; } -bool atclient_notify_params_is_notifier_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_notifier_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INDEX] & ATCLIENT_NOTIFY_PARAMS_NOTIFIER_INITIALIZED; } -bool atclient_notify_params_is_notification_expiry_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_notification_expiry_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INDEX] & ATCLIENT_NOTIFY_PARAMS_NOTIFICATION_EXPIRY_INITIALIZED; } -bool atclient_notify_params_is_shared_encryption_key_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_shared_encryption_key_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INDEX] & ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED; } From 6a4be25dadcd93d3fc13231d6f9961247275425f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:19:18 -0400 Subject: [PATCH 138/193] fix: --- packages/atclient/src/atclient_put_self_key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index 14f6c7f3..929f7033 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -142,7 +142,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu goto exit; } memset(update_cmd, 0, sizeof(char) * update_cmd_size); - snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", atkey_str, metadata_protocol_str, value_encrypted_base64); + snprintf(update_cmd, update_cmd_size, "update%s:%s %s\r\n", metadata_protocol_str, atkey_str, value_encrypted_base64); const size_t update_cmd_len = update_cmd_size - 1; /* From b9b287caef7e4bfaf1b123641a43a4d66477bb9d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:21:06 -0400 Subject: [PATCH 139/193] fix: --- packages/atclient/src/atclient_put_shared_key.c | 5 +++-- packages/atclient/src/notify_params.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index 3458fa8c..e7e408a0 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -44,7 +44,9 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t iv_base64_size = atchops_base64_encoded_size(iv_size); char iv_base64[iv_base64_size]; - const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(strlen(value)); + const size_t value_len = strlen(value); + + const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(value_len); unsigned char value_encrypted[value_encrypted_size]; const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size); @@ -114,7 +116,6 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va /* * 4. Encrypt value */ - const size_t value_len = strlen(value); size_t value_encrypted_len = 0; memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, value, value_len, value_encrypted, diff --git a/packages/atclient/src/notify_params.c b/packages/atclient/src/notify_params.c index df3aeeea..b8be497c 100644 --- a/packages/atclient/src/notify_params.c +++ b/packages/atclient/src/notify_params.c @@ -67,12 +67,12 @@ bool atclient_notify_params_is_message_type_initialized(const atclient_notify_pa ATCLIENT_NOTIFY_PARAMS_MESSAGE_TYPE_INITIALIZED; } -bool atclient_notify_params_is_priority_initialized(const const atclient_notify_params *params) { +bool atclient_notify_params_is_priority_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_PRIORITY_INDEX] & ATCLIENT_NOTIFY_PARAMS_PRIORITY_INITIALIZED; } -bool atclient_notify_params_is_strategy_initialized(atclient_notify_params *params) { +bool atclient_notify_params_is_strategy_initialized(const atclient_notify_params *params) { return params->_initialized_fields[ATCLIENT_NOTIFY_PARAMS_STRATEGY_INDEX] & ATCLIENT_NOTIFY_PARAMS_STRATEGY_INITIALIZED; } From 398dbb8d07166fe2a1bc1aa155486b4fb32aab6f Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 13:27:20 -0400 Subject: [PATCH 140/193] fix: --- packages/atclient/src/atclient_put_self_key.c | 2 +- packages/atclient/src/atclient_put_shared_key.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index 929f7033..c837b225 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -91,7 +91,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu if ((ret = atchops_base64_encode(iv, iv_size, iv_base64, iv_base64_size, NULL)) != 0) { } - if ((ret = atclient_atkey_metadata_set_iv_nonce(atkey, iv_base64)) != 0) { + if ((ret = atclient_atkey_metadata_set_iv_nonce(&(atkey->metadata), iv_base64)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce: %d\n", ret); goto exit; } diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index e7e408a0..d832eda6 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -97,6 +97,9 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va goto exit; } + /* + * 4. Generate IV + */ if((ret = atchops_iv_generate(iv)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); goto exit; @@ -114,7 +117,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } /* - * 4. Encrypt value + * 5. Encrypt value */ size_t value_encrypted_len = 0; memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); @@ -132,7 +135,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } /* - * 5. Create update command + * 6. Create update command */ if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &metadata_protocol_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocol_str: %d\n", ret); @@ -158,7 +161,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t update_cmd_len = update_cmd_size - 1; /* - * 6. Send update command + * 7. Send update command */ if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, &recv_len)) != 0) { @@ -179,8 +182,9 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } char *response_without_data = response + strlen("data:"); + /* - * 7. Return commit id + * 8. Return commit id */ if (commit_id != NULL) { *commit_id = atoi(response_without_data); From 7cf6b0d42929b79cfa92712cfc4346b80ed0578b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:00:46 -0400 Subject: [PATCH 141/193] fix: --- packages/atclient/src/atkeys.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 99527243..84f521ab 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -493,12 +493,12 @@ int atclient_atkeys_populate_from_strings(atclient_atkeys *atkeys, const char *a */ // 4a. self encryption key - if ((ret = set_self_encryption_key_base64(atkeys, self_encryption_key_str, self_encryption_key_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) { From 486ac0297f7c2da383f4d7972700e32562e645cd Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:01:58 -0400 Subject: [PATCH 142/193] fix: --- packages/atclient/src/atclient_get_self_key.c | 35 ++++++++++++++++++- packages/atclient/src/atclient_put_self_key.c | 30 +++++++++++----- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/packages/atclient/src/atclient_get_self_key.c b/packages/atclient/src/atclient_get_self_key.c index ef8a0a31..91d26b34 100644 --- a/packages/atclient/src/atclient_get_self_key.c +++ b/packages/atclient/src/atclient_get_self_key.c @@ -138,7 +138,7 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value if ((ret = atchops_base64_decode((unsigned char *)atclient->atkeys.self_encryption_key_base64, strlen(atclient->atkeys.self_encryption_key_base64), self_encryption_key, - self_encryption_size, &self_encryption_key_len)) != 0) { + self_encryption_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -153,12 +153,45 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value memset(value_raw, 0, sizeof(char) * value_raw_size); size_t value_raw_len = 0; + // log data->valuestring + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "data->valuestring: %s\n", data->valuestring); + + // log data->valuestring length + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "data->valuestring length: %zu\n", strlen(data->valuestring)); + if ((ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } + // log self_encryption_Key + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "self_encryption_key: "); + for (size_t i = 0; i < self_encryption_size; i++) { + printf("%02x ", self_encryption_key[i]); + } + printf("\n"); + + // log iv + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "iv: "); + for (size_t i = 0; i < ATCHOPS_IV_BUFFER_SIZE; i++) { + printf("%02x ", iv[i]); + } + printf("\n"); + + // log value_raw + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_raw: "); + for (size_t i = 0; i < value_raw_len; i++) { + printf("%02x ", value_raw[i]); + } + printf("\n"); + + // log value_raw_len + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_raw_len: %zu\n", value_raw_len); + + // log value %p, value_size, and value_len + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value: %p, value_size: %zu, value_len: %zu\n", value, value_size, + *value_len); if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw, value_raw_len, (unsigned char *)value, value_size, value_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index c837b225..bea5a910 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -30,13 +30,13 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu /* * 2. Variables */ - const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; - unsigned char shared_encryption_key[shared_encryption_key_size]; + const size_t self_encryption_key_size = ATCHOPS_AES_256 / 8; + unsigned char self_encryption_key[self_encryption_key_size]; const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[iv_size]; - const size_t iv_base64_size = atchops_base64_encoded_size(iv_size); + const size_t iv_base64_size = atchops_base64_encoded_size(iv_size) + 1; char iv_base64[iv_base64_size]; const size_t value_len = strlen(value); @@ -44,7 +44,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(value_len); unsigned char value_encrypted[value_encrypted_size]; - const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size); + const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size) + 1; char value_encrypted_base64[value_encrypted_base64_size]; char *update_cmd = NULL; @@ -73,8 +73,8 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu } if ((ret = atchops_base64_decode((unsigned char *)ctx->atkeys.self_encryption_key_base64, - strlen(ctx->atkeys.self_encryption_key_base64), shared_encryption_key, - shared_encryption_key_size, NULL)) != 0) { + strlen(ctx->atkeys.self_encryption_key_base64), self_encryption_key, + self_encryption_key_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -87,8 +87,10 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu goto exit; } - memset(iv_base64, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(iv_base64, 0, sizeof(unsigned char) * iv_base64_size); if ((ret = atchops_base64_encode(iv, iv_size, iv_base64, iv_base64_size, NULL)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); + goto exit; } if ((ret = atclient_atkey_metadata_set_iv_nonce(&(atkey->metadata), iv_base64)) != 0) { @@ -100,13 +102,23 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu * 5. Encrypt value */ size_t value_encrypted_len = 0; - memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); - if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, + if ((ret = atchops_aes_ctr_encrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, value_encrypted, value_encrypted_size, &value_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto exit; } + // log value_encrypted + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_encrypted: "); + for (size_t i = 0; i < value_encrypted_len; i++) { + printf("%02x ", value_encrypted[i]); + } + printf("\n"); + + // log value_encrypted_len + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_encrypted_len: %zu\n", value_encrypted_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_encrypted_size: %zu\n", value_encrypted_size); + size_t value_encrypted_base64_len = 0; memset(value_encrypted_base64, 0, sizeof(char) * value_encrypted_base64_size); if ((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, value_encrypted_base64, From 298e05a46962a6daf9a84da3ca2d754b7e8726f7 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:06:12 -0400 Subject: [PATCH 143/193] fix: --- packages/atclient/src/atclient_put_shared_key.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index d832eda6..90d9da3b 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -41,7 +41,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[iv_size]; - const size_t iv_base64_size = atchops_base64_encoded_size(iv_size); + const size_t iv_base64_size = atchops_base64_encoded_size(iv_size) + 1; char iv_base64[iv_base64_size]; const size_t value_len = strlen(value); @@ -49,7 +49,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t value_encrypted_size = atchops_aes_ctr_ciphertext_size(value_len); unsigned char value_encrypted[value_encrypted_size]; - const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size); + const size_t value_encrypted_base64_size = atchops_base64_encoded_size(value_encrypted_size) + 1; char value_encrypted_base64[value_encrypted_base64_size]; char *update_cmd = NULL; @@ -174,6 +174,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } char *response = (char *)recv; + if (!atclient_string_utils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", @@ -182,7 +183,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va } char *response_without_data = response + strlen("data:"); - + /* * 8. Return commit id */ From 0d515d32aeeee4fc77694c74a118b0324ed1e8e3 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:18:31 -0400 Subject: [PATCH 144/193] feat: free things --- .../atclient/src/atclient_put_public_key.c | 24 +++++++++++++------ packages/atclient/src/atclient_put_self_key.c | 20 ++++++++++------ .../atclient/src/atclient_put_shared_key.c | 8 ++++++- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/atclient/src/atclient_put_public_key.c b/packages/atclient/src/atclient_put_public_key.c index 06939b49..8d849c58 100644 --- a/packages/atclient/src/atclient_put_public_key.c +++ b/packages/atclient/src/atclient_put_public_key.c @@ -63,8 +63,8 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va const size_t atkey_str_len = strlen(atkey_str); const size_t metadata_protocol_str_len = strlen(metadata_protocol_str); - const size_t update_cmd_size = - strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + strlen(value) + strlen("\r\n") + 1; + const size_t update_cmd_size = strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + + strlen(" ") + strlen(value) + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); @@ -76,8 +76,8 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va /* * 4. Send update command */ - if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, - &recv_len)) != 0) { + if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *)update_cmd, update_cmd_len, recv, + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -91,7 +91,7 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va if (!atclient_string_utils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); + (int)recv_len, recv); goto exit; } @@ -106,7 +106,13 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va ret = 0; goto exit; -exit: { return ret; } +exit: { + free(recv); + free(atkey_str); + free(metadata_protocol_str); + free(update_cmd); + return ret; +} } static int atclient_put_public_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, @@ -177,5 +183,9 @@ static int atclient_put_public_key_validate_arguments(const atclient *ctx, const ret = 0; goto exit; -exit: { return ret; } +exit: { + free(client_atsign_with_at); + free(shared_by_atsign_with_at); + return ret; +} } diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index bea5a910..7e227d6a 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -2,9 +2,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -146,8 +146,8 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu const size_t atkey_str_len = strlen(atkey_str); // update: command - const size_t update_cmd_size = - strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + strlen(" ") + value_encrypted_base64_len + strlen("\r\n") + 1; + const size_t update_cmd_size = strlen("update") + metadata_protocol_str_len + strlen(":") + atkey_str_len + + strlen(" ") + value_encrypted_base64_len + strlen("\r\n") + 1; if ((update_cmd = malloc(sizeof(char) * update_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); @@ -160,8 +160,8 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu /* * 7. Send update command */ - if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *) update_cmd, update_cmd_len, recv, recv_size, - &recv_len)) != 0) { + if ((ret = atclient_connection_send(&ctx->atserver_connection, (unsigned char *)update_cmd, update_cmd_len, recv, + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -175,7 +175,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu if (!atclient_string_utils_starts_with(response, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); + (int)recv_len, recv); goto exit; } @@ -190,7 +190,13 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu ret = 0; goto exit; -exit: { return ret; } +exit: { + free(recv); + free(update_cmd); + free(metadata_protocol_str); + free(atkey_str); + return ret; +} } static int atclient_put_self_key_validate_arguments(atclient *ctx, atclient_atkey *atkey, const char *value, diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index 90d9da3b..89862ee4 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -193,7 +193,13 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va ret = 0; goto exit; -exit: { return ret; } +exit: { + free(recipient_atsign_with_at); + free(update_cmd); + free(metadata_protocol_str); + free(atkey_str); + free(recv); + return ret; } } static int atclient_put_shared_key_validate_arguments(const atclient *ctx, const atclient_atkey *atkey, From 328c95e1615219c588fb6136bcf014c71ab69110 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:21:20 -0400 Subject: [PATCH 145/193] feat: free more stuff --- packages/atclient/src/atkeys.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 84f521ab..ca4b253c 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -46,13 +46,34 @@ void atclient_atkeys_init(atclient_atkeys *atkeys) { atchops_rsa_key_private_key_init(&(atkeys->pkam_private_key)); atchops_rsa_key_public_key_init(&(atkeys->encrypt_public_key)); atchops_rsa_key_private_key_init(&(atkeys->encrypt_private_key)); + atkeys->self_encryption_key_base64 = NULL; } void atclient_atkeys_free(atclient_atkeys *atkeys) { + if(atkeys == NULL) { + return; + } + atchops_rsa_key_public_key_free(&(atkeys->pkam_public_key)); 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)) { + unset_pkam_public_key_base64(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)) { + unset_encrypt_public_key_base64(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)) { + unset_self_encryption_key_base64(atkeys); + } + } int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, From 7f378271f00dc461f1e09b1b4ca3837fe3848beb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:22:10 -0400 Subject: [PATCH 146/193] refactor: public_key --- packages/atclient/include/atclient/atkeys.h | 6 +-- packages/atclient/src/atkeys.c | 44 +++++++++++---------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/packages/atclient/include/atclient/atkeys.h b/packages/atclient/include/atclient/atkeys.h index df8e58f8..bb3afc57 100644 --- a/packages/atclient/include/atclient/atkeys.h +++ b/packages/atclient/include/atclient/atkeys.h @@ -32,7 +32,7 @@ * 4. (for rsakeys), the rsakey struct used in rsa operations. */ typedef struct atclient_atkeys { - char *pkam_publickey_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 @@ -63,7 +63,7 @@ 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_publickey_base64, +int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, const size_t pkampublickeybase64len); int atclient_atkeys_set_pkam_private_key_base64(atclient_atkeys *atkeys, const char *pkam_private_key_base64, @@ -78,7 +78,7 @@ 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_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_publickey_base64, +int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_public_key_base64, const size_t pkampublickeybase64len); int atclient_atkeys_populate_pkam_private_key(atclient_atkeys *atkeys, const char *pkam_private_key_base64, diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index ca4b253c..0f3cee02 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -29,7 +29,7 @@ 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 int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, +static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, 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); @@ -46,6 +46,10 @@ void atclient_atkeys_init(atclient_atkeys *atkeys) { atchops_rsa_key_private_key_init(&(atkeys->pkam_private_key)); atchops_rsa_key_public_key_init(&(atkeys->encrypt_public_key)); atchops_rsa_key_private_key_init(&(atkeys->encrypt_private_key)); + atkeys->pkam_public_key_base64 = NULL; + atkeys->pkam_private_key_base64 = NULL; + atkeys->encrypt_public_key_base64 = NULL; + atkeys->encrypt_private_key_base64 = NULL; atkeys->self_encryption_key_base64 = NULL; } @@ -76,7 +80,7 @@ void atclient_atkeys_free(atclient_atkeys *atkeys) { } -int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, +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) { int ret = 1; @@ -86,9 +90,9 @@ int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const ch return ret; } - if (pkam_publickey_base64 == NULL) { + if (pkam_public_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_publickey_base64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_public_key_base64 is NULL\n"); return ret; } @@ -98,8 +102,8 @@ int atclient_atkeys_set_pkam_public_key_base64(atclient_atkeys *atkeys, const ch return ret; } - if ((ret = set_pkam_public_key_base64(atkeys, pkam_publickey_base64, pkam_public_key_base64_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_pkam_public_key_base64: %d | failed to set pkam_publickey_base64\n", + 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); goto exit; } @@ -241,7 +245,7 @@ int atclient_atkeys_set_self_encryption_key_base64(atclient_atkeys *atkeys, cons exit: { return ret; } } -int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_publickey_base64, +int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char *pkam_public_key_base64, const size_t pkam_public_key_base64_len) { int ret = 1; @@ -251,9 +255,9 @@ int atclient_atkeys_populate_pkam_public_key(atclient_atkeys *atkeys, const char return ret; } - if (pkam_publickey_base64 == NULL) { + if (pkam_public_key_base64 == NULL) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_publickey_base64 is NULL\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pkam_public_key_base64 is NULL\n"); return ret; } @@ -263,7 +267,7 @@ 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_publickey_base64, pkam_publickey_base64)) != + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), pkam_public_key_base64, 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); @@ -617,8 +621,8 @@ 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_publickey_base64, - strlen(atkeys->pkam_publickey_base64))) != 0) { + if ((ret = atchops_rsa_key_populate_public_key(&(atkeys->pkam_public_key), atkeys->pkam_public_key_base64, + 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; @@ -763,9 +767,9 @@ static void set_self_encryption_key_base64_initialized(atclient_atkeys *atkeys, static void unset_pkam_public_key_base64(atclient_atkeys *atkeys) { if (is_pkam_public_key_base64_initialized(atkeys)) { - free(atkeys->pkam_publickey_base64); + free(atkeys->pkam_public_key_base64); } - atkeys->pkam_publickey_base64 = NULL; + atkeys->pkam_public_key_base64 = NULL; set_pkam_public_key_base64_initialized(atkeys, false); } @@ -801,7 +805,7 @@ static void unset_self_encryption_key_base64(atclient_atkeys *atkeys) { set_self_encryption_key_base64_initialized(atkeys, false); } -static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_publickey_base64, +static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_public_key_base64, const size_t pkam_public_key_len) { int ret = 1; @@ -810,16 +814,16 @@ static int set_pkam_public_key_base64(atclient_atkeys *atkeys, const char *pkam_ } const size_t pkam_publickey_size = pkam_public_key_len + 1; - atkeys->pkam_publickey_base64 = (char *)malloc(sizeof(char) * (pkam_publickey_size)); - if (atkeys->pkam_publickey_base64 == NULL) { + 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_publickey_base64\n", + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "malloc: %d | failed to allocate memory for pkam_public_key_base64\n", ret); goto exit; } - memcpy(atkeys->pkam_publickey_base64, pkam_publickey_base64, pkam_public_key_len); - atkeys->pkam_publickey_base64[pkam_public_key_len] = '\0'; + memcpy(atkeys->pkam_public_key_base64, pkam_public_key_base64, pkam_public_key_len); + atkeys->pkam_public_key_base64[pkam_public_key_len] = '\0'; set_pkam_public_key_base64_initialized(atkeys, true); From 72477754b56e22f9a4b0dcd7e334722a9a315bb8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:23:18 -0400 Subject: [PATCH 147/193] test: debug CI --- .github/workflows/tests.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 59a1ce9b..6fdf6ecd 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -40,6 +40,11 @@ jobs: cmake -S . -B build sudo cmake --build build --target install + - name: Debug temp + working-directory: tests/functional_tests + run: | + tests/functional_tests/tools/run_test.sh test_atclient_sharedkey + - name: Build and Run Functional Tests working-directory: tests/functional_tests run: | From f0d13dc51f87a06b8f6e8958853e32e2723377d8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 14:24:33 -0400 Subject: [PATCH 148/193] ci: --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 6fdf6ecd..f37b2e0d 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -43,7 +43,7 @@ jobs: - name: Debug temp working-directory: tests/functional_tests run: | - tests/functional_tests/tools/run_test.sh test_atclient_sharedkey + ./tools/run_test.sh test_atclient_sharedkey - name: Build and Run Functional Tests working-directory: tests/functional_tests From 2f17827f0e16b5f4f526f5af40617e83a2b7f2b8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:23:01 -0400 Subject: [PATCH 149/193] ci: --- .../tests/test_atclient_sharedkey.c | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 3927f40f..56858438 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -52,45 +52,45 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if ((ret = test_1_put(&atclient1)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); - goto exit; - } - - if ((ret = test_2_get_as_sharedby(&atclient1)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); - goto exit; - } - - if ((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if ((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - goto exit; - } - - if ((ret = test_3_get_as_sharedwith(&atclient2)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); - goto exit; - } - - if ((ret = test_4_delete(&atclient1)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); - goto exit; - } - - if ((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); - goto exit; - } + // if ((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + // goto exit; + // } + + // if ((ret = test_1_put(&atclient1)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); + // goto exit; + // } + + // if ((ret = test_2_get_as_sharedby(&atclient1)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); + // goto exit; + // } + + // if ((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + // goto exit; + // } + + // if ((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + // goto exit; + // } + + // if ((ret = test_3_get_as_sharedwith(&atclient2)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); + // goto exit; + // } + + // if ((ret = test_4_delete(&atclient1)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); + // goto exit; + // } + + // if ((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) { + // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); + // goto exit; + // } goto exit; From 9d45b3996ab7e6d1f88409354b8996f6361c3520 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:25:31 -0400 Subject: [PATCH 150/193] ci: --- tests/functional_tests/lib/src/helpers.c | 39 ++++++---- .../tests/test_atclient_sharedkey.c | 78 +++++++++---------- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index 9f0bd454..b2a9bc1a 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -120,7 +120,8 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const exit: { return ret; } } -int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *knamespace) { +int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *shared_by, + const char *knamespace) { int ret = -1; atclient_atkey atkey; @@ -174,8 +175,8 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c exit: { return ret; } } -int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *shared_by, const char *shared_with, - const char *knamespace) { +int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *shared_by, + const char *shared_with, const char *knamespace) { int ret = -1; atclient_atkey atkey; @@ -232,42 +233,45 @@ exit: { return ret; } int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *recipient) { int ret = 1; - if(atclient1 == NULL) { + if (atclient1 == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - goto exit; + return ret; } - if(recipient == NULL) { + if (recipient == NULL) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recipient is NULL\n"); - goto exit; + return ret; } - if(!atclient_is_atsign_initialized(atclient1)) { + if (!atclient_is_atsign_initialized(atclient1)) { + ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is not initialized\n"); - goto exit; + return ret; } // TODO check if pkam authenticated - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down Begin\n"); - - char atkeystrtemp[ATCLIENT_ATKEY_FULL_LEN]; - atclient_atkey atkeyforme; atclient_atkey_init(&atkeyforme); atclient_atkey atkeyforthem; atclient_atkey_init(&atkeyforthem); - + + char atkeystrtemp[ATCLIENT_ATKEY_FULL_LEN]; + char *client_atsign_with_at = NULL; char *recipient_atsign_with_at = NULL; - if((ret = atclient_string_utils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down Begin\n"); + + if ((ret = atclient_string_utils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } - if((ret = atclient_string_utils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { + if ((ret = atclient_string_utils_atsign_with_at(recipient, &recipient_atsign_with_at)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_string_utils_atsign_with_at: %d\n", ret); goto exit; } @@ -276,7 +280,8 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re char *recipient_atsign_without_at = recipient_atsign_with_at + 1; memset(atkeystrtemp, 0, sizeof(char) * ATCLIENT_ATKEY_FULL_LEN); - snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", recipient_atsign_without_at, client_atsign_with_at); + snprintf(atkeystrtemp, ATCLIENT_ATKEY_FULL_LEN, "shared_key.%s%s", recipient_atsign_without_at, + client_atsign_with_at); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkeystrtemp: \"%s\"\n", atkeystrtemp); if ((ret = atclient_atkey_from_string(&atkeyforme, atkeystrtemp)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string: %d\n", ret); diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 56858438..3927f40f 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -52,45 +52,45 @@ int main(int argc, char *argv[]) { goto exit; } - // if ((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - // goto exit; - // } - - // if ((ret = test_1_put(&atclient1)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); - // goto exit; - // } - - // if ((ret = test_2_get_as_sharedby(&atclient1)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); - // goto exit; - // } - - // if ((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - // goto exit; - // } - - // if ((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); - // goto exit; - // } - - // if ((ret = test_3_get_as_sharedwith(&atclient2)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); - // goto exit; - // } - - // if ((ret = test_4_delete(&atclient1)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); - // goto exit; - // } - - // if ((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) { - // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); - // goto exit; - // } + if ((ret = functional_tests_pkam_auth(&atclient1, &atkeys1, atsign1, strlen(atsign1))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } + + if ((ret = test_1_put(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_1_put: %d\n", ret); + goto exit; + } + + if ((ret = test_2_get_as_sharedby(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_2_get_as_sharedby: %d\n", ret); + goto exit; + } + + if ((ret = functional_tests_set_up_atkeys(&atkeys2, atsign2, atsign2len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } + + if ((ret = functional_tests_pkam_auth(&atclient2, &atkeys2, atsign2, strlen(atsign2))) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "set_up: %d\n", ret); + goto exit; + } + + if ((ret = test_3_get_as_sharedwith(&atclient2)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_get_as_sharedwith: %d\n", ret); + goto exit; + } + + if ((ret = test_4_delete(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_delete: %d\n", ret); + goto exit; + } + + if ((ret = test_5_should_not_exist_as_sharedby(&atclient1)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_5_should_not_exist: %d\n", ret); + goto exit; + } goto exit; From 2c875b10e39c4bf28a334634e0c6eb035b368743 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:30:06 -0400 Subject: [PATCH 151/193] chore: --- tests/functional_tests/lib/src/helpers.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/functional_tests/lib/src/helpers.c b/tests/functional_tests/lib/src/helpers.c index b2a9bc1a..61633b6f 100644 --- a/tests/functional_tests/lib/src/helpers.c +++ b/tests/functional_tests/lib/src/helpers.c @@ -20,8 +20,7 @@ int functional_tests_set_up_atkeys(atclient_atkeys *atkeys, const char *atsign, memset(atkeyspath, 0, atkeyspathsize); size_t atkeyspathlen = 0; - ret = functional_tests_get_atkeys_path(atsign, atsignlen, atkeyspath, atkeyspathsize, &atkeyspathlen); - if (ret != 0) { + if ((ret = functional_tests_get_atkeys_path(atsign, atsignlen, atkeyspath, atkeyspathsize, &atkeyspathlen)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atkeys_sharedwith path: %d\n", ret); goto exit; } @@ -54,6 +53,7 @@ int functional_tests_pkam_auth(atclient *atclient, atclient_atkeys *atkeys, cons atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret); goto exit; } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "pkam authenticated\n"); goto exit; @@ -117,7 +117,10 @@ int functional_tests_publickey_exists(atclient *atclient, const char *key, const ret = true; goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + return ret; +} } int functional_tests_selfkey_exists(atclient *atclient, const char *key, const char *shared_by, @@ -172,7 +175,9 @@ int functional_tests_selfkey_exists(atclient *atclient, const char *key, const c ret = true; goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + return ret; } } int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const char *shared_by, @@ -227,7 +232,9 @@ int functional_tests_sharedkey_exists(atclient *atclient, const char *key, const ret = true; goto exit; -exit: { return ret; } +exit: { + atclient_atkey_free(&atkey); + return ret; } } int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *recipient) { @@ -247,7 +254,7 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re if (!atclient_is_atsign_initialized(atclient1)) { ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is not initialized\n"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not initialized\n"); return ret; } @@ -259,11 +266,11 @@ int functional_tests_tear_down_sharedenckeys(atclient *atclient1, const char *re atclient_atkey atkeyforthem; atclient_atkey_init(&atkeyforthem); - char atkeystrtemp[ATCLIENT_ATKEY_FULL_LEN]; - char *client_atsign_with_at = NULL; char *recipient_atsign_with_at = NULL; + char atkeystrtemp[ATCLIENT_ATKEY_FULL_LEN]; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "tear_down Begin\n"); if ((ret = atclient_string_utils_atsign_with_at(atclient1->atsign, &client_atsign_with_at)) != 0) { From 00763c296ea8ca848cba3959bc76006d5b68aa98 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:37:23 -0400 Subject: [PATCH 152/193] fix: --- .../atclient/src/atclient_put_shared_key.c | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index 89862ee4..2c757e80 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -79,22 +79,20 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va if (request_options != NULL && atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { memcpy(shared_encryption_key, request_options->shared_encryption_key, shared_encryption_key_size); - } else if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, + } else { + if((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, shared_encryption_key)) != 0) { - if (ret != ATCLIENT_ERR_AT0015_KEY_NOT_FOUND) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); - goto exit; - } - if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + goto exit; + } else if (ret == ATCLIENT_ERR_AT0015_KEY_NOT_FOUND) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Did not find shared_encryption_key_shared_by_me.. Creating key pair for me and other...\n"); + if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); - goto exit; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); + goto exit; + } } - } else { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "could not find shared encryption key\n"); - goto exit; } /* From 0b23db56f77fd184b099c743b39b21d5749d4fd6 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:38:45 -0400 Subject: [PATCH 153/193] fix: --- packages/atclient/src/atclient_put_shared_key.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index 2c757e80..82618d06 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -80,11 +80,9 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va atclient_put_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { memcpy(shared_encryption_key, request_options->shared_encryption_key, shared_encryption_key_size); } else { - if((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, - shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); - goto exit; - } else if (ret == ATCLIENT_ERR_AT0015_KEY_NOT_FOUND) { + ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, + shared_encryption_key); + if (ret == ATCLIENT_ERR_AT0015_KEY_NOT_FOUND) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Did not find shared_encryption_key_shared_by_me.. Creating key pair for me and other...\n"); if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, shared_encryption_key)) != 0) { @@ -92,6 +90,9 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); goto exit; } + } else if(ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); + goto exit; } } @@ -278,4 +279,4 @@ exit: { free(ctx_atsign_formatted); return ret; } -} \ No newline at end of file +} From 31fdd16e444ec5589da9d11706e8966ba27f8ff0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:57:09 -0400 Subject: [PATCH 154/193] feat: function signature atclient_get_atkeys --- packages/atclient/include/atclient/atclient.h | 3 +- .../include/atclient/request_options.h | 38 +++ packages/atclient/src/atclient_get_atkeys.c | 71 ++++-- packages/atclient/src/request_options.c | 236 +++++++++++++++++- .../tests/test_atclient_get_atkeys.c | 8 +- 5 files changed, 327 insertions(+), 29 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 11898978..e20b9968 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -292,8 +292,7 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atcli * @param output_array_len (output): the overall size of the array that was allocated * @return int 0 on success */ -int atclient_get_atkeys(atclient *atclient, const char *regex, const bool show_hidden, const size_t recv_buffer_size, - atclient_atkey **atkey, size_t *output_array_len); +int atclient_get_atkeys(atclient *atclient, atclient_atkey **atkey, size_t *output_array_len, const atclient_get_atkeys_request_options *request_options); /** * @brief Send a heartbeat (noop) diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index ea865097..6ca05766 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -34,6 +34,12 @@ #define ATCLIENT_DELETE_REQUEST_OPTIONS_INDEX 0 #define ATCLIENT_DELETE_REQUEST_OPTIONS_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INDEX 0 +#define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INDEX 0 + +#define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INITIALIZED (VALUE_INITIALIZED << 1) + /* * 1A. Put SelfKey */ @@ -94,6 +100,16 @@ typedef struct atclient_delete_request_options { uint8_t _initialized_fields[1]; } atclient_delete_request_options; +/* + * 4. Get_AtKeys Request Options + */ + +typedef struct atclient_get_atkeys_request_options { + char *regex; + bool show_hidden; + uint8_t _initialized_fields[1]; +} atclient_get_atkeys_request_options; + /* * 1A. Put SelfKey */ @@ -196,4 +212,26 @@ void atclient_get_public_key_request_options_unset_store_atkey_metadata( void atclient_delete_request_options_init(atclient_delete_request_options *options); void atclient_delete_request_options_free(atclient_delete_request_options *options); + +/* + * 4. Get_AtKeys Request Options + */ + +void atclient_get_atkeys_request_options_init(atclient_get_atkeys_request_options *options); +void atclient_get_atkeys_request_options_free(atclient_get_atkeys_request_options *options); + +bool atclient_get_atkeys_request_options_is_regex_initialized( + const atclient_get_atkeys_request_options *options); +void atclient_get_atkeys_request_options_set_regex_initialized(atclient_get_atkeys_request_options *options, + const bool initialized); +int atclient_get_atkeys_request_options_set_regex(atclient_get_atkeys_request_options *options, const char *regex); +void atclient_get_atkeys_request_options_unset_regex(atclient_get_atkeys_request_options *options); + +bool atclient_get_atkeys_request_options_is_show_hidden_initialized( + const atclient_get_atkeys_request_options *options); +void atclient_get_atkeys_request_options_set_show_hidden_initialized(atclient_get_atkeys_request_options *options, + const bool initialized); +int atclient_get_atkeys_request_options_set_show_hidden(atclient_get_atkeys_request_options *options, const bool show_hidden); +void atclient_get_atkeys_request_options_unset_show_hidden(atclient_get_atkeys_request_options *options); + #endif diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index f1d53b31..8500c5e2 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -7,55 +7,83 @@ #define TAG "atclient_get_atkeys" -static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const char *regex, const bool showhidden, - const atclient_atkey **atkey, const size_t *output_array_len); +static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const atclient_atkey **atkey, const size_t *output_array_len, const atclient_get_atkeys_request_options *request_options); -int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhidden, const size_t recvbuffersize, - atclient_atkey **atkey, size_t *output_array_len) - -{ +int atclient_get_atkeys(atclient *atclient, atclient_atkey **atkey, size_t *output_array_len, const atclient_get_atkeys_request_options *request_options) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_atkeys_validate_arguments(atclient, regex, showhidden, atkey, output_array_len)) != 0) { + if ((ret = atclient_get_atkeys_validate_arguments(atclient, atkey, output_array_len, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_validate_arguments: %d\n", ret); return ret; } - if (regex == NULL) { - regex = ""; + if(request_options != NULL) { + } /* * 2. Variables */ - const size_t scan_cmd_buf_size = strlen("scan") + (showhidden ? strlen(":showHidden:true") : 0) + - (strlen(regex) > 0 ? (strlen(" ") + strlen(regex)) : 0) + strlen("\r\n") + 1; - char scan_cmd[scan_cmd_buf_size]; + size_t scan_cmd_size = strlen("scan"); + + if(request_options != NULL) { + if(atclient_get_atkeys_request_options_is_show_hidden_initialized(request_options)) { + if(request_options->show_hidden) { + scan_cmd_size += strlen(":showHidden:true"); + } else { + scan_cmd_size += strlen(":showHidden:false"); + } + } + if(atclient_get_atkeys_request_options_is_regex_initialized(request_options)) { + scan_cmd_size += strlen(" ") + strlen(request_options->regex); + } + } + + scan_cmd_size += strlen("\r\n") + 1; - unsigned char recv[recvbuffersize]; - size_t recvlen = 0; + + char scan_cmd[scan_cmd_size]; + + const size_t recv_size = 8192; // TODO change using atclient_connection_read which will handle realloc + unsigned char recv[recv_size]; + size_t recv_len = 0; cJSON *root = NULL; // free later /* * 3. Build scan command */ - snprintf(scan_cmd, scan_cmd_buf_size, "scan%s%s%s\r\n", showhidden ? ":showHidden:true" : "", strlen(regex) > 0 ? " " : "", - regex); + size_t pos = 0; + pos += snprintf(scan_cmd + pos, scan_cmd_size - pos, "scan"); + if(request_options != NULL) { + if(atclient_get_atkeys_request_options_is_show_hidden_initialized(request_options)) { + if(request_options->show_hidden) { + pos += snprintf(scan_cmd + pos, scan_cmd_size - pos, ":showHidden:true"); + } else { + pos += snprintf(scan_cmd + pos, scan_cmd_size - pos, ":showHidden:false"); + } + } + if(atclient_get_atkeys_request_options_is_regex_initialized(request_options)) { + pos += snprintf(scan_cmd + pos, scan_cmd_size - pos, " %s", request_options->regex); + } + } + pos += snprintf(scan_cmd + pos, scan_cmd_size - pos, "\r\n"); + + const size_t scan_cmd_len = pos; /* * 4. Send scan command */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scan_cmd, scan_cmd_buf_size - 1, recv, - recvbuffersize, &recvlen)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)scan_cmd, scan_cmd_len, recv, + recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv was %lu bytes long\n", recvlen); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv was %lu bytes long\n", recv_len); /* * 5. Parse response @@ -63,7 +91,7 @@ int atclient_get_atkeys(atclient *atclient, const char *regex, const bool showhi if (!atclient_string_utils_starts_with((char *)recv, "data:")) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recvlen, recv); + (int)recv_len, recv); goto exit; } @@ -131,8 +159,7 @@ exit: { } } -static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const char *regex, const bool showhidden, - const atclient_atkey **atkey, const size_t *output_array_len) { +static int atclient_get_atkeys_validate_arguments(const atclient *atclient, const atclient_atkey **atkey, const size_t *output_array_len, const atclient_get_atkeys_request_options *request_options) { int ret = 1; // check to make sure null ptr wasn't provided diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 8b45c71a..8e1d2fed 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #define TAG "request_options" @@ -938,3 +938,237 @@ void atclient_get_public_key_request_options_unset_store_atkey_metadata( options->store_atkey_metadata = false; atclient_get_public_key_request_options_set_store_atkey_metadata_initialized(options, false); } + +void atclient_get_atkeys_request_options_init(atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_request_options_init: Invalid arguments\n"); + return; + } + + /* + * 2. Initialize the options + */ + memset(options, 0, sizeof(atclient_get_atkeys_request_options)); +} + +void atclient_get_atkeys_request_options_free(atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_request_options_free: Invalid arguments\n"); + return; + } + + /* + * 2. Free the options + */ + if (atclient_get_atkeys_request_options_is_regex_initialized(options)) { + atclient_get_atkeys_request_options_unset_regex(options); + } + + if (atclient_get_atkeys_request_options_is_show_hidden_initialized(options)) { + atclient_get_atkeys_request_options_unset_show_hidden(options); + } +} + +bool atclient_get_atkeys_request_options_is_regex_initialized(const atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_is_regex_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the regex is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INDEX] & + ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INITIALIZED; +} + +void atclient_get_atkeys_request_options_set_regex_initialized(atclient_get_atkeys_request_options *options, + const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_regex_initialized: Invalid arguments\n"); + return; + } + + /* + * 2. Set the regex initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INDEX] |= + ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INDEX] &= + ~ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INITIALIZED; + } +} + +int atclient_get_atkeys_request_options_set_regex(atclient_get_atkeys_request_options *options, const char *regex) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_regex: Invalid arguments\n"); + return ret; + } + + if (regex == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_regex: Invalid arguments\n"); + return ret; + } + + /* + * 2. Unset the regex, if necessary + */ + if (atclient_get_atkeys_request_options_is_regex_initialized(options)) { + atclient_get_atkeys_request_options_unset_regex(options); + } + + /* + * 3. Set the regex + */ + const size_t regex_len = strlen(regex); + const size_t regex_size = regex_len + 1; + if ((options->regex = (char *)malloc(sizeof(char) * regex_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_regex: Failed to allocate memory for regex\n"); + return ret; + } + + atclient_get_atkeys_request_options_set_regex_initialized(options, true); + memcpy(options->regex, regex, regex_len); + options->regex[regex_len] = '\0'; + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_atkeys_request_options_unset_regex(atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_unset_regex: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the regex + */ + if (atclient_get_atkeys_request_options_is_regex_initialized(options)) { + free(options->regex); + } + options->regex = NULL; + atclient_get_atkeys_request_options_set_regex_initialized(options, false); +} + +bool atclient_get_atkeys_request_options_is_show_hidden_initialized( + const atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_is_show_hidden_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the show hidden is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INDEX] & + ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INITIALIZED; +} + +void atclient_get_atkeys_request_options_set_show_hidden_initialized(atclient_get_atkeys_request_options *options, + const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_show_hidden_initialized: Invalid arguments\n"); + return; + } + + /* + * 2. Set the show hidden initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INDEX] |= + ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INDEX] &= + ~ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INITIALIZED; + } +} + +int atclient_get_atkeys_request_options_set_show_hidden(atclient_get_atkeys_request_options *options, + const bool show_hidden) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_set_show_hidden: Invalid arguments\n"); + return ret; + } + + /* + * 2. Unset the show hidden, if necessary + */ + if (atclient_get_atkeys_request_options_is_show_hidden_initialized(options)) { + atclient_get_atkeys_request_options_unset_show_hidden(options); + } + + /* + * 3. Set the show hidden + */ + options->show_hidden = show_hidden; + atclient_get_atkeys_request_options_set_show_hidden_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_atkeys_request_options_unset_show_hidden(atclient_get_atkeys_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_atkeys_request_options_unset_show_hidden: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the show hidden + */ + options->show_hidden = false; + atclient_get_atkeys_request_options_set_show_hidden_initialized(options, false); +} diff --git a/tests/functional_tests/tests/test_atclient_get_atkeys.c b/tests/functional_tests/tests/test_atclient_get_atkeys.c index 12d16829..9f1d435e 100644 --- a/tests/functional_tests/tests/test_atclient_get_atkeys.c +++ b/tests/functional_tests/tests/test_atclient_get_atkeys.c @@ -74,7 +74,7 @@ static int test_1_atclient_get_atkeys(atclient *ctx, const char *scan_regex, con atclient_atkey *atkey_array = NULL; size_t atkey_array_len = 0; - if ((ret = atclient_get_atkeys(ctx, scan_regex, showhidden, 8192, &atkey_array, &atkey_array_len)) != 0) { + if ((ret = atclient_get_atkeys(ctx, &atkey_array, &atkey_array_len, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys failed: %d", ret); goto exit; } @@ -103,7 +103,7 @@ static int test_2_atclient_get_atkeys_null(atclient *ctx, const char *scan_regex atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test_2_atclient_get_atkeys_null\n"); - if (atclient_get_atkeys(ctx, scan_regex, showhidden, 8192, NULL, NULL) == 0) { + if (atclient_get_atkeys(ctx, NULL, NULL, NULL) == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys passed when it should not have: %d\n", ret); ret = 1; goto exit; @@ -128,7 +128,7 @@ static int test_3_atclient_get_atkeys_null_ctx(const char *scan_regex, const boo atclient_atkey *arr = NULL; size_t arrlen = 0; - if (atclient_get_atkeys(NULL, scan_regex, showhidden, 8192, &arr, &arrlen) == 0) { + if (atclient_get_atkeys(NULL, &arr, &arrlen, NULL) == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys passed when it should not have: %d\n", ret); ret = 1; goto exit; @@ -153,7 +153,7 @@ static int test_4_atclient_get_atkeys_null_regex(atclient *ctx, const char *scan atclient_atkey *arr = NULL; size_t arrlen = 0; - if ((ret = atclient_get_atkeys(ctx, NULL, showhidden, 8192, &arr, &arrlen) != 0)) { + if ((ret = atclient_get_atkeys(ctx, &arr, &arrlen, NULL) != 0)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys failed: %d\n", ret); goto exit; } From d412e9d68e83b8b7c0452be9908cd0d60d8d19d0 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 18:59:14 -0400 Subject: [PATCH 155/193] ci: remove debug temp --- .github/workflows/tests.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index f37b2e0d..59a1ce9b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -40,11 +40,6 @@ jobs: cmake -S . -B build sudo cmake --build build --target install - - name: Debug temp - working-directory: tests/functional_tests - run: | - ./tools/run_test.sh test_atclient_sharedkey - - name: Build and Run Functional Tests working-directory: tests/functional_tests run: | From 7ecc9213f17c596ec05e31937090438975bac153 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:01:24 -0400 Subject: [PATCH 156/193] fix: repl example --- examples/desktop/repl/src/main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 4ee3a64c..8abc384b 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -181,12 +181,22 @@ int main(int argc, char *argv[]) { get_end: { atclient_atkey_free(&atkey); } } else if (strcmp(command, "/scan") == 0) { + atclient_get_atkeys_request_options request_options; + atclient_get_atkeys_request_options_init(&request_options); char *regex = NULL; char *saveptr = NULL; regex = strtok_r(NULL, " ", &saveptr); + if(regex != NULL) { + regex[strcspn(regex, "\n")] = 0; + + if((ret = atclient_get_atkeys_request_options_set_regex(&request_options, regex)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_request_options_set_regex: %d | failed to set regex\n", ret); + goto scan_end; + } + } atclient_atkey *arr = NULL; size_t arrlen = 0; - if ((ret = atclient_get_atkeys(&atclient, regex, true, 8192, &arr, &arrlen)) != 0) { + if ((ret = atclient_get_atkeys(&atclient, &arr, &arrlen, &request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); goto scan_end; } @@ -202,11 +212,12 @@ int main(int argc, char *argv[]) { atclient_atkey_free(&arr[i]); } free(arr); + atclient_get_atkeys_request_options_free(&request_options); } } else if(strcmp(command, "/deleteall") == 0) { atclient_atkey *arr = NULL; size_t arrlen = 0; - if((ret = atclient_get_atkeys(&atclient, NULL, true, 8192, &arr, &arrlen)) != 0) { + if((ret = atclient_get_atkeys(&atclient, &arr, &arrlen, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys: %d | failed to get atKeys\n", ret); goto deleteall_end; } From c64e2a68831092f4304a9b0c9a0576ba49102b20 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:28:16 -0400 Subject: [PATCH 157/193] refactor: atclient_get_shared_key --- packages/atclient/include/atclient/atclient.h | 11 +- .../atclient/src/atclient_get_shared_key.c | 310 ++++++++++-------- 2 files changed, 168 insertions(+), 153 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index e20b9968..92533e77 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -248,16 +248,11 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val * * @param atclient The atclient context (must satisfy the two conditions stated above) * @param atkey The populated atkey to get the value from (must satisfy the two conditions stated above) - * @param value The buffer to hold value gotten from atServer - * @param value_size The buffer length allocated for the value - * @param value_len The output length of the value gotten from atServer - * @param shared_encryption_key The correct shared encryption key (get_encryption_key_shared_by_me or - * get_encryption_key_shared_by_other, depending on the case). If NULL is provided, it will get it for you, if it - * exists. + * @param request_options The options for the get operation, can be NULL if you don't need to set any options + * @param value A pointer that will be allocated for you to hold value gotten from atServer, can be NULL if you don't need the value, if it is non-null, caller is responsible for freeing the memory * @return int 0 on success */ -int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, const unsigned char *shared_encryption_key); +int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, const atclient_get_shared_key_request_options *request_options, char **value); /** * @brief Delete an atkey from your atserver diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index 975f4620..68491185 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -13,23 +13,24 @@ #define TAG "atclient_get_sharedkey" static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t value_size, const size_t *value_len, - const unsigned char *shared_encryption_key); + const atclient_get_shared_key_request_options *request_options, + const char **value); -static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, - const unsigned char *shared_encryption_key); +static int +atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, + char **value); -static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, - const unsigned char *shared_encryption_key); +static int +atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, + char **value); -int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, const unsigned char *shared_encryption_key) { +int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, char **value) { int ret = 1; - if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, value, value_size, value_len, - shared_encryption_key)) != 0) { + if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, request_options, value)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_validate_arguments: %d\n", ret); return ret; } @@ -48,14 +49,12 @@ int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char *val } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - if ((ret = atclient_get_shared_key_shared_by_other_with_me(atclient, atkey, value, value_size, value_len, - shared_encryption_key)) != 0) { + if ((ret = atclient_get_shared_key_shared_by_other_with_me(atclient, atkey, request_options, value)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - if ((ret = atclient_get_shared_key_shared_by_me_with_other(atclient, atkey, value, value_size, value_len, - shared_encryption_key)) != 0) { + if ((ret = atclient_get_shared_key_shared_by_me_with_other(atclient, atkey, request_options, value)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_me_with_other: %d\n", ret); goto exit; } @@ -69,9 +68,75 @@ exit: { } } -static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, - const unsigned char *shared_encryption_key) { +static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, + const char **value) { + int ret = 1; + + if (atclient == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); + goto exit; + } + + if (!atclient_is_atserver_connection_started(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection was not started\n"); + goto exit; + } + + if (!atclient_is_atsign_initialized(atclient)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized was not set\n"); + goto exit; + } + + if (atclient->async_read) { + ret = 1; + atlogger_log( + TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key cannot be called from an async_read atclient, it will cause a race condition\n"); + goto exit; + } + + if (atkey == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); + goto exit; + } + + if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); + goto exit; + } + + if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is not initialized or is empty\n"); + goto exit; + } + + if (!atclient_atkey_is_shared_with_initialized(atkey) || strlen(atkey->key) <= 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is not initialized or is empty\n"); + goto exit; + } + + ret = 0; +exit: { return ret; } +} + +static int +atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, + char **value) { int ret = 1; /* @@ -106,6 +171,9 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a char *metadata_str = NULL; + atclient_atkey_metadata metadata; + atclient_atkey_metadata_init(&metadata); + /* * 3. Format atSigns */ @@ -122,11 +190,10 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a /* * 4. Get shared_encryption_key, if necessary */ - if (shared_encryption_key != NULL) { - memcpy(shared_encryption_key_to_use, shared_encryption_key, ATCHOPS_AES_256 / 8); + if(request_options != NULL && atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { + memcpy(shared_encryption_key_to_use, request_options->shared_encryption_key, shared_encryption_key_to_use_size); } else { - if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->shared_with, - shared_encryption_key_to_use)) != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->shared_with, shared_encryption_key_to_use)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); goto exit; } @@ -152,8 +219,8 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a /* * 6. Send llookup:all: llookup_cmd */ - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, llookup_cmd_size - 1, recv, - recv_size, &recv_len)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, + llookup_cmd_size - 1, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -177,20 +244,20 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a goto exit; } - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { + cJSON *metadata_json = cJSON_GetObjectItem(root, "metaData"); + if (metadata_json == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); goto exit; } - if((metadata_str = cJSON_Print(metadata)) == NULL) { + if ((metadata_str = cJSON_Print(metadata_json)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Print: %d\n", ret); goto exit; } - if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + if ((ret = atclient_atkey_metadata_from_json_str(&metadata, metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -205,8 +272,8 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a /* * 8. Set IV in the AtKey */ - if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, + if (atclient_atkey_metadata_is_iv_nonce_initialized(&metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)metadata.iv_nonce, strlen(metadata.iv_nonce), iv, iv_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; @@ -229,9 +296,8 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a } memset(value_raw_encrypted, 0, sizeof(unsigned char) * value_raw_encrypted_size); size_t value_raw_encrypted_len = 0; - if ((ret = atchops_base64_decode((unsigned char *) value_raw_encrypted_base64, value_raw_encrypted_base64_len, - value_raw_encrypted, value_raw_encrypted_size, - &value_raw_encrypted_len)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)value_raw_encrypted_base64, value_raw_encrypted_base64_len, + value_raw_encrypted, value_raw_encrypted_size, &value_raw_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -245,9 +311,8 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; - if ((ret = atchops_aes_ctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, - value_raw_encrypted, value_raw_encrypted_len, - value_raw, value_raw_size, &value_raw_len)) != 0) { + if ((ret = atchops_aes_ctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encrypted, + value_raw_encrypted_len, value_raw, value_raw_size, &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); goto exit; } @@ -255,8 +320,23 @@ static int atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, a /* * 10. Set value */ - memcpy(value, value_raw, value_raw_len); - *value_len = value_raw_len; + + if(request_options != NULL && atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(request_options) && request_options->store_atkey_metadata) { + if((ret = atclient_atkey_metadata_from_json_str(&atkey->metadata, metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_copy: %d\n", ret); + goto exit; + } + } + + if(value != NULL) { + if ((*value = malloc(sizeof(char) * (value_raw_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; + } + memcpy(*value, value_raw, value_raw_len); + (*value)[value_raw_len] = '\0'; + } ret = 0; goto exit; @@ -268,13 +348,15 @@ exit: { free(llookup_cmd); free(metadata_str); cJSON_Delete(root); + atclient_atkey_metadata_free(&metadata); return ret; } } -static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, - const unsigned char *shared_encryption_key) { +static int +atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, + const atclient_get_shared_key_request_options *request_options, + char **value) { int ret = 1; /* @@ -291,7 +373,7 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; unsigned char shared_encryption_key_to_use[shared_encryption_key_size]; - const size_t recv_size = 4096; + const size_t recv_size = 8192; // TODO use atclient_connection_read to realloc stuff unsigned char recv[recv_size]; char *llookup_cmd = NULL; @@ -299,6 +381,9 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a cJSON *root = NULL; char *metadata_str = NULL; + atclient_atkey_metadata metadata; + atclient_atkey_metadata_init(&metadata); + const size_t iv_size = ATCHOPS_IV_BUFFER_SIZE; unsigned char iv[iv_size]; @@ -321,14 +406,15 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a /* * 4. Get shared_encryption_key, if necessary */ - if (shared_encryption_key == NULL) { + if (request_options != NULL && + atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { + memcpy(shared_encryption_key_to_use, request_options->shared_encryption_key, shared_encryption_key_size); + } else { if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, atkey->shared_by, shared_encryption_key_to_use)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_other: %d\n", ret); goto exit; } - } else { - memcpy(shared_encryption_key_to_use, shared_encryption_key, shared_encryption_key_size); } /* @@ -346,7 +432,8 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a goto exit; } if (namespace_exists) { - snprintf(llookup_cmd, llookup_cmd_size, "lookup:all:%s.%s%s\r\n", atkey->key, atkey->namespace_str, sender_atsign_with_at); + snprintf(llookup_cmd, llookup_cmd_size, "lookup:all:%s.%s%s\r\n", atkey->key, atkey->namespace_str, + sender_atsign_with_at); } else { snprintf(llookup_cmd, llookup_cmd_size, "lookup:all:%s%s\r\n", atkey->key, sender_atsign_with_at); } @@ -356,8 +443,8 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a */ memset(recv, 0, sizeof(unsigned char) * recv_size); size_t recv_len = 0; - if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, llookup_cmd_size - 1, recv, - recv_size, &recv_len)) != 0) { + if ((ret = atclient_connection_send(&(atclient->atserver_connection), (unsigned char *)llookup_cmd, + llookup_cmd_size - 1, recv, recv_size, &recv_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); goto exit; } @@ -382,16 +469,16 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a goto exit; } - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { + cJSON *metadata_json = cJSON_GetObjectItem(root, "metaData"); + if (metadata_json == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); goto exit; } - metadata_str = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata_json); - if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + if ((ret = atclient_atkey_metadata_from_json_str(&metadata, metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -406,20 +493,19 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a /* * 8. Set IV */ - if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, - iv_size, NULL)) != 0) { + if (atclient_atkey_metadata_is_iv_nonce_initialized(&metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)metadata.iv_nonce, strlen(metadata.iv_nonce), iv, iv_size, + NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } } else { - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); + memset(iv, 0, sizeof(unsigned char) * iv_size); } /* * 9. Decrypt data */ - const unsigned char *value_raw_encrypted_base64 = (unsigned char *)data->valuestring; const size_t value_raw_encrypted_base64_len = strlen(data->valuestring); @@ -448,8 +534,8 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a memset(value_raw, 0, sizeof(unsigned char) * value_raw_size); size_t value_raw_len = 0; if ((ret = atchops_aes_ctr_decrypt(shared_encryption_key_to_use, ATCHOPS_AES_256, iv, value_raw_encryted, - value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, - &value_raw_len)) != 0) { + value_raw_encryted_len, (unsigned char *)value_raw, value_raw_size, + &value_raw_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); goto exit; } @@ -457,8 +543,24 @@ static int atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, a /* * 10. Set value */ - memcpy(value, value_raw, value_raw_len); - *value_len = value_raw_len; + if (request_options != NULL && + atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(request_options) && + request_options->store_atkey_metadata) { + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); + goto exit; + } + } + + if(value != NULL) { + if ((*value = malloc(sizeof(char) * (value_raw_len + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; + } + memcpy(*value, value_raw, value_raw_len); + (*value)[value_raw_len] = '\0'; + } ret = 0; goto exit; @@ -470,89 +572,7 @@ exit: { free(llookup_cmd); free(metadata_str); cJSON_Delete(root); + atclient_atkey_metadata_free(&metadata); return ret; } } - -static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t value_size, const size_t *value_len, - const unsigned char *shared_encryption_key) { - int ret = 1; - - if (atclient == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - goto exit; - } - - if (!atclient_is_atserver_connection_started(atclient)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection was not started\n"); - goto exit; - } - - if (!atclient_is_atsign_initialized(atclient)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_is_atsign_initialized was not set\n"); - goto exit; - } - - if (atclient->async_read) { - ret = 1; - atlogger_log( - TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_shared_key cannot be called from an async_read atclient, it will cause a race condition\n"); - goto exit; - } - - if (atkey == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); - goto exit; - } - - if (atclient_atkey_get_type(atkey) != ATCLIENT_ATKEY_TYPE_SHARED_KEY) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is not a shared key\n"); - goto exit; - } - - if (!atclient_atkey_is_key_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is not initialized or is empty\n"); - goto exit; - } - - if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_by is not initialized or is empty\n"); - goto exit; - } - - if (!atclient_atkey_is_shared_with_initialized(atkey) || strlen(atkey->key) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "shared_with is not initialized or is empty\n"); - goto exit; - } - - if (value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - goto exit; - } - - if (value_size == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); - goto exit; - } - - if (value_len == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); - goto exit; - } - - ret = 0; -exit: { return ret; } -} From 5c952f4c5d5627a905574db53e2996a766e88dbb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:31:37 -0400 Subject: [PATCH 158/193] refactor: --- packages/atclient/include/atclient/atclient.h | 4 +- .../atclient/src/atclient_get_shared_key.c | 54 +++++++++---------- .../tests/test_atclient_sharedkey.c | 9 ++-- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 92533e77..2c4074df 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -248,11 +248,11 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val * * @param atclient The atclient context (must satisfy the two conditions stated above) * @param atkey The populated atkey to get the value from (must satisfy the two conditions stated above) - * @param request_options The options for the get operation, can be NULL if you don't need to set any options * @param value A pointer that will be allocated for you to hold value gotten from atServer, can be NULL if you don't need the value, if it is non-null, caller is responsible for freeing the memory + * @param request_options The options for the get operation, can be NULL if you don't need to set any options * @return int 0 on success */ -int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, const atclient_get_shared_key_request_options *request_options, char **value); +int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_shared_key_request_options *request_options); /** * @brief Delete an atkey from your atserver diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index 68491185..711b3d91 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -13,18 +13,16 @@ #define TAG "atclient_get_sharedkey" static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - const char **value); + const char **value, + const atclient_get_shared_key_request_options *request_options); static int -atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - char **value); +atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, char **value, + const atclient_get_shared_key_request_options *request_options); static int -atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - char **value); +atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char **value, + const atclient_get_shared_key_request_options *request_options); int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, const atclient_get_shared_key_request_options *request_options, char **value) { @@ -49,12 +47,12 @@ int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, } if (strcmp(sharedby_atsign_with_at, client_atsign_with_at) != 0) { - if ((ret = atclient_get_shared_key_shared_by_other_with_me(atclient, atkey, request_options, value)) != 0) { + if ((ret = atclient_get_shared_key_shared_by_other_with_me(atclient, atkey, value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_other_with_me: %d\n", ret); goto exit; } } else { - if ((ret = atclient_get_shared_key_shared_by_me_with_other(atclient, atkey, request_options, value)) != 0) { + if ((ret = atclient_get_shared_key_shared_by_me_with_other(atclient, atkey, value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_shared_by_me_with_other: %d\n", ret); goto exit; } @@ -69,8 +67,8 @@ exit: { } static int atclient_get_shared_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - const char **value) { + const char **value, + const atclient_get_shared_key_request_options *request_options) { int ret = 1; if (atclient == NULL) { @@ -133,10 +131,9 @@ static int atclient_get_shared_key_validate_arguments(const atclient *atclient, exit: { return ret; } } -static int -atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - char **value) { +static int atclient_get_shared_key_shared_by_me_with_other( + atclient *atclient, atclient_atkey *atkey, + char **value, const atclient_get_shared_key_request_options *request_options) { int ret = 1; /* @@ -190,10 +187,12 @@ atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atk /* * 4. Get shared_encryption_key, if necessary */ - if(request_options != NULL && atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { + if (request_options != NULL && + atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(request_options)) { memcpy(shared_encryption_key_to_use, request_options->shared_encryption_key, shared_encryption_key_to_use_size); } else { - if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->shared_with, shared_encryption_key_to_use)) != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_me(atclient, atkey->shared_with, + shared_encryption_key_to_use)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); goto exit; } @@ -273,8 +272,8 @@ atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atk * 8. Set IV in the AtKey */ if (atclient_atkey_metadata_is_iv_nonce_initialized(&metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)metadata.iv_nonce, strlen(metadata.iv_nonce), iv, - iv_size, NULL)) != 0) { + if ((ret = atchops_base64_decode((unsigned char *)metadata.iv_nonce, strlen(metadata.iv_nonce), iv, iv_size, + NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } @@ -321,14 +320,16 @@ atclient_get_shared_key_shared_by_me_with_other(atclient *atclient, atclient_atk * 10. Set value */ - if(request_options != NULL && atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(request_options) && request_options->store_atkey_metadata) { - if((ret = atclient_atkey_metadata_from_json_str(&atkey->metadata, metadata_str)) != 0) { + if (request_options != NULL && + atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(request_options) && + request_options->store_atkey_metadata) { + if ((ret = atclient_atkey_metadata_from_json_str(&atkey->metadata, metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_copy: %d\n", ret); goto exit; } } - if(value != NULL) { + if (value != NULL) { if ((*value = malloc(sizeof(char) * (value_raw_len + 1))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); @@ -354,9 +355,8 @@ exit: { } static int -atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, - char **value) { +atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atkey *atkey, char **value, + const atclient_get_shared_key_request_options *request_options) { int ret = 1; /* @@ -552,7 +552,7 @@ atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atk } } - if(value != NULL) { + if (value != NULL) { if ((*value = malloc(sizeof(char) * (value_raw_len + 1))) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 3927f40f..18e3de55 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -150,10 +150,7 @@ static int test_2_get_as_sharedby(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { @@ -161,7 +158,7 @@ static int test_2_get_as_sharedby(atclient *atclient) { goto exit; } - if ((ret = atclient_get_shared_key(atclient, &atkey, value, valuesize, &valuelen, NULL)) != 0) { + if ((ret = atclient_get_shared_key(atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -217,7 +214,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { goto exit; } - if ((ret = atclient_get_shared_key(atclient2, &atkey, value, valuesize, &valuelen, NULL)) != 0) { + if ((ret = atclient_get_shared_key(atclient2, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } From d7deb9c91c16e16fe1865ab60a027f074cfc52f8 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:40:46 -0400 Subject: [PATCH 159/193] feat: atclient_get_public_key new function signature --- examples/desktop/crud/get_publickey.c | 2 +- examples/desktop/crud/get_sharedkey.c | 2 +- examples/desktop/crud/put_publickey.c | 2 +- packages/atclient/include/atclient/atclient.h | 10 +- .../include/atclient/request_options.h | 43 ++-- .../atclient/src/atclient_get_public_key.c | 69 +++---- packages/atclient/src/request_options.c | 189 +++++++++--------- .../tests/test_atclient_publickey.c | 4 +- 8 files changed, 153 insertions(+), 168 deletions(-) diff --git a/examples/desktop/crud/get_publickey.c b/examples/desktop/crud/get_publickey.c index 03362ef6..6f28569b 100644 --- a/examples/desktop/crud/get_publickey.c +++ b/examples/desktop/crud/get_publickey.c @@ -77,7 +77,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - if ((ret = atclient_get_public_key(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { + if ((ret = atclient_get_public_key(&atclient, &atkey, value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } diff --git a/examples/desktop/crud/get_sharedkey.c b/examples/desktop/crud/get_sharedkey.c index 7ae1d8b4..5d57c37d 100644 --- a/examples/desktop/crud/get_sharedkey.c +++ b/examples/desktop/crud/get_sharedkey.c @@ -93,7 +93,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)atkeystrlen, atkeystr); - ret = atclient_get_shared_key(&atclient, &atkey, value, valuesize, valuelen, NULL); + ret = atclient_get_shared_key(&atclient, &atkey, value, NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared key"); goto exit; diff --git a/examples/desktop/crud/put_publickey.c b/examples/desktop/crud/put_publickey.c index dc8b4544..b93bf1ee 100644 --- a/examples/desktop/crud/put_publickey.c +++ b/examples/desktop/crud/put_publickey.c @@ -83,7 +83,7 @@ int main() { } // atclient_get_public_key - if ((ret = atclient_get_public_key(&atclient, &atkey, value, valuelen, &valueolen, true)) != 0) { + if ((ret = atclient_get_public_key(&atclient, &atkey, value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index 2c4074df..c5c5f83b 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -225,15 +225,11 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) - * @param value the buffer to hold value gotten from atServer - * @param value_size the buffer length allocated for the value - * @param value_len the output length of the value gotten from atServer - * @param bypass_cache true if you want to bypass the cached publickey, that might be on your atServer, and get the most - * up-to-date value straight from the atServer that the publickey sits on, false otherwise + * @param value double pointer to hold value gotten from atServer, can be NULL if you don't need the value, if it is non-null, caller is responsible for freeing the memory + * @param request_options the options for the get operation, can be NULL if you don't need to set any options * @return int 0 on success */ -int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, bool bypass_cache); +int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **value, atclient_get_public_key_request_options *request_options); /** * @brief Get a sharedkey either shared by you or shared with you and receive the decrypted plaintext value. diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index 6ca05766..ae706d28 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -15,9 +15,6 @@ #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 -#define ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 0) - #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) @@ -28,8 +25,10 @@ #define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 -#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) #define ATCLIENT_DELETE_REQUEST_OPTIONS_INDEX 0 #define ATCLIENT_DELETE_REQUEST_OPTIONS_INITIALIZED (VALUE_INITIALIZED << 0) @@ -62,7 +61,8 @@ typedef struct atclient_put_shared_key_request_options { * 1C. Put PublicKey */ typedef struct atclient_put_public_key_request_options { - bool bypass_cache; + // empty for now + // kept for future proofing uint8_t _initialized_fields[1]; } atclient_put_public_key_request_options; @@ -87,6 +87,7 @@ typedef struct atclient_get_shared_key_request_options { * 2C. Get PublicKey */ typedef struct atclient_get_public_key_request_options { + bool bypass_cache; bool store_atkey_metadata; uint8_t _initialized_fields[1]; } atclient_get_public_key_request_options; @@ -105,9 +106,9 @@ typedef struct atclient_delete_request_options { */ typedef struct atclient_get_atkeys_request_options { - char *regex; - bool show_hidden; - uint8_t _initialized_fields[1]; + char *regex; + bool show_hidden; + uint8_t _initialized_fields[1]; } atclient_get_atkeys_request_options; /* @@ -145,14 +146,6 @@ void atclient_put_shared_key_request_options_unset_bypass_cache(atclient_put_sha void atclient_put_public_key_request_options_init(atclient_put_public_key_request_options *options); void atclient_put_public_key_request_options_free(atclient_put_public_key_request_options *options); -bool atclient_put_public_key_request_options_is_bypass_cache_initialized( - const atclient_put_public_key_request_options *options); -void atclient_put_public_key_request_options_set_bypass_cache_initialized( - atclient_put_public_key_request_options *options, const bool initialized); -int atclient_put_public_key_request_options_set_bypass_cache(atclient_put_public_key_request_options *options, - const bool bypass_cache); -void atclient_put_public_key_request_options_unset_bypass_cache(atclient_put_public_key_request_options *options); - /* * 2A. Get SelfKey */ @@ -197,6 +190,14 @@ void atclient_get_shared_key_request_options_unset_store_atkey_metadata( void atclient_get_public_key_request_options_init(atclient_get_public_key_request_options *options); void atclient_get_public_key_request_options_free(atclient_get_public_key_request_options *options); +bool atclient_get_public_key_request_options_is_bypass_cache_initialized( + const atclient_get_public_key_request_options *options); +void atclient_get_public_key_request_options_set_bypass_cache_initialized( + atclient_get_public_key_request_options *options, const bool initialized); +int atclient_get_public_key_request_options_set_bypass_cache(atclient_get_public_key_request_options *options, + const bool bypass_cache); +void atclient_get_public_key_request_options_unset_bypass_cache(atclient_get_public_key_request_options *options); + bool atclient_get_public_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_public_key_request_options *options); void atclient_get_public_key_request_options_set_store_atkey_metadata_initialized( @@ -212,7 +213,6 @@ void atclient_get_public_key_request_options_unset_store_atkey_metadata( void atclient_delete_request_options_init(atclient_delete_request_options *options); void atclient_delete_request_options_free(atclient_delete_request_options *options); - /* * 4. Get_AtKeys Request Options */ @@ -220,18 +220,17 @@ void atclient_delete_request_options_free(atclient_delete_request_options *optio void atclient_get_atkeys_request_options_init(atclient_get_atkeys_request_options *options); void atclient_get_atkeys_request_options_free(atclient_get_atkeys_request_options *options); -bool atclient_get_atkeys_request_options_is_regex_initialized( - const atclient_get_atkeys_request_options *options); +bool atclient_get_atkeys_request_options_is_regex_initialized(const atclient_get_atkeys_request_options *options); void atclient_get_atkeys_request_options_set_regex_initialized(atclient_get_atkeys_request_options *options, const bool initialized); int atclient_get_atkeys_request_options_set_regex(atclient_get_atkeys_request_options *options, const char *regex); void atclient_get_atkeys_request_options_unset_regex(atclient_get_atkeys_request_options *options); -bool atclient_get_atkeys_request_options_is_show_hidden_initialized( - const atclient_get_atkeys_request_options *options); +bool atclient_get_atkeys_request_options_is_show_hidden_initialized(const atclient_get_atkeys_request_options *options); void atclient_get_atkeys_request_options_set_show_hidden_initialized(atclient_get_atkeys_request_options *options, const bool initialized); -int atclient_get_atkeys_request_options_set_show_hidden(atclient_get_atkeys_request_options *options, const bool show_hidden); +int atclient_get_atkeys_request_options_set_show_hidden(atclient_get_atkeys_request_options *options, + const bool show_hidden); void atclient_get_atkeys_request_options_unset_show_hidden(atclient_get_atkeys_request_options *options); #endif diff --git a/packages/atclient/src/atclient_get_public_key.c b/packages/atclient/src/atclient_get_public_key.c index 5b6bb065..2a67ca7c 100644 --- a/packages/atclient/src/atclient_get_public_key.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -8,17 +8,15 @@ #define TAG "atclient_get_public_key" -static int atclient_get_public_key_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, bool bypass_cache); +static int atclient_get_public_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char **value, const atclient_get_public_key_request_options *request_options); -int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len, bool bypass_cache) { +int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **value, atclient_get_public_key_request_options *request_options) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_public_key_validate_arguments(atclient, atkey, value, value_size, value_len, bypass_cache)) != + if ((ret = atclient_get_public_key_validate_arguments(atclient, atkey, value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_key_validate_arguments: %d\n", ret); return ret; @@ -29,7 +27,7 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val */ char *atkey_str = NULL; - const size_t recv_size = value_size; + const size_t recv_size = 8192; // TODO use atclient_connection_read to adaptively read for us unsigned char recv[recv_size]; memset(recv, 0, sizeof(unsigned char) * recv_size); size_t recv_len = 0; @@ -56,10 +54,11 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val goto exit; } + const bool bypass_cache = request_options != NULL && atclient_get_public_key_request_options_is_bypass_cache_initialized(request_options) && request_options->bypass_cache; + const size_t plookup_cmd_size = strlen("plookup:all:\r\n") + (bypass_cache ? strlen("bypassCache:true:") : 0) + strlen(atkey_str_without_public) + 1; - plookup_cmd = malloc(sizeof(char) * plookup_cmd_size); - if (plookup_cmd == NULL) { + if ((plookup_cmd = malloc(sizeof(char) * plookup_cmd_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for plookup_cmd\n"); goto exit; @@ -91,8 +90,7 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val char *response_without_data = response + 5; - root = cJSON_Parse(response_without_data); - if (root == NULL) { + if ((root = cJSON_Parse(response_without_data)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret); goto exit; @@ -108,22 +106,32 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char *val /* * 6. Return data to caller */ - memcpy(value, data->valuestring, strlen(data->valuestring)); - *value_len = strlen(value); - // 6b. write to atkey->metadata - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); - goto exit; - } + bool write_metadata_to_atkey = request_options != NULL && atclient_get_public_key_request_options_is_store_atkey_metadata_initialized(request_options) && request_options->store_atkey_metadata; + if(write_metadata_to_atkey) { + cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); + if (metadata == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); + goto exit; + } - metadata_str = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); - goto exit; + if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); + goto exit; + } + } + + if(value != NULL) { + if ((*value = malloc(sizeof(char) * (strlen(data->valuestring) + 1))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; + } + memcpy(*value, data->valuestring, strlen(data->valuestring)); + (*value)[strlen(data->valuestring)] = '\0'; } ret = 0; @@ -139,8 +147,7 @@ exit: { } } -static int atclient_get_public_key_validate_arguments(atclient *atclient, atclient_atkey *atkey, char *value, - const size_t value_size, size_t *value_len, bool bypass_cache) { +static int atclient_get_public_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char **value, const atclient_get_public_key_request_options *request_options) { int ret = 1; if (atclient == NULL) { @@ -169,18 +176,6 @@ static int atclient_get_public_key_validate_arguments(atclient *atclient, atclie goto exit; } - if (value_size == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); - goto exit; - } - - if (value_len == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); - goto exit; - } - if (!atclient_is_atserver_connection_started(atclient)) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 8e1d2fed..a3f5ecc9 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -332,103 +332,6 @@ void atclient_put_public_key_request_options_free(atclient_put_public_key_reques /* * 2. Free the options */ - if (atclient_put_public_key_request_options_is_bypass_cache_initialized(options)) { - atclient_put_public_key_request_options_unset_bypass_cache(options); - } -} - -bool atclient_put_public_key_request_options_is_bypass_cache_initialized( - const atclient_put_public_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_public_key_request_options_is_bypass_cache_initialized: " - "Invalid arguments\n"); - return false; - } - - /* - * 2. Check if the bypass cache is initialized - */ - return options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & - ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; -} - -void atclient_put_public_key_request_options_set_bypass_cache_initialized( - atclient_put_public_key_request_options *options, const bool initialized) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_public_key_request_options_set_bypass_cache_initialized: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Set the bypass cache initialized - */ - if (initialized) { - options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= - ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; - } else { - options->_initialized_fields[ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= - ~ATCLIENT_PUT_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; - } -} - -int atclient_put_public_key_request_options_set_bypass_cache(atclient_put_public_key_request_options *options, - const bool bypass_cache) { - int ret = 1; - - /* - * 1. Validate arguments - */ - if (options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_public_key_request_options_set_bypass_cache: " - "Invalid arguments\n"); - goto exit; - } - - /* - * 2. Unset the bypass cache, if necessary - */ - if (atclient_put_public_key_request_options_is_bypass_cache_initialized(options)) { - atclient_put_public_key_request_options_unset_bypass_cache(options); - } - - /* - * 3. Set the bypass cache - */ - options->bypass_cache = bypass_cache; - atclient_put_public_key_request_options_set_bypass_cache_initialized(options, true); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_put_public_key_request_options_unset_bypass_cache(atclient_put_public_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_public_key_request_options_unset_bypass_cache: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Unset the bypass cache - */ - options->bypass_cache = false; - atclient_put_public_key_request_options_set_bypass_cache_initialized(options, false); } /* @@ -844,6 +747,98 @@ void atclient_get_public_key_request_options_free(atclient_get_public_key_reques } } +bool atclient_get_public_key_request_options_is_bypass_cache_initialized( + const atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_is_bypass_cache_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the bypass cache is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & + ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; +} + +void atclient_get_public_key_request_options_set_bypass_cache_initialized(atclient_get_public_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_set_bypass_cache_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the bypass cache initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= + ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= + ~ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } +} + +int atclient_get_public_key_request_options_set_bypass_cache(atclient_get_public_key_request_options *options, const bool bypass_cache) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_set_bypass_cache: " + "Invalid arguments\n"); + return ret; + } + + /* + * 2. Unset the bypass cache, if necessary + */ + if (atclient_get_public_key_request_options_is_bypass_cache_initialized(options)) { + atclient_get_public_key_request_options_unset_bypass_cache(options); + } + + /* + * 3. Set the bypass cache + */ + options->bypass_cache = bypass_cache; + atclient_get_public_key_request_options_set_bypass_cache_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_public_key_request_options_unset_bypass_cache(atclient_get_public_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_public_key_request_options_unset_bypass_cache: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the bypass cache + */ + options->bypass_cache = false; + atclient_get_public_key_request_options_set_bypass_cache_initialized(options, false); +} + bool atclient_get_public_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_public_key_request_options *options) { /* diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index 9ed8dd41..b1610f96 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -149,7 +149,7 @@ static int test_2_get(atclient *atclient) { goto exit; } - if ((ret = atclient_get_public_key(atclient, &atkey, value, valuesize, &valuelen, false)) != 0) { + if ((ret = atclient_get_public_key(atclient, &atkey, value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } @@ -271,7 +271,7 @@ static int test_6_get_with_metadata(atclient *atclient) { goto exit; } - if ((ret = atclient_get_public_key(atclient, &atkey, value, valuesize, &valuelen, true)) != 0) { + if ((ret = atclient_get_public_key(atclient, &atkey, value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } From 03a716cd2af1a98a04924c7131c4193452d77be9 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:42:58 -0400 Subject: [PATCH 160/193] fix: --- packages/atclient/src/atclient_get_shared_key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index 711b3d91..c7edcad6 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -25,7 +25,7 @@ atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atk const atclient_get_shared_key_request_options *request_options); int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, - const atclient_get_shared_key_request_options *request_options, char **value) { + char **value, const atclient_get_shared_key_request_options *request_options) { int ret = 1; if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, request_options, value)) != 0) { From d4cfffc2ecc7eba29651fb951c9c12a621c7a5cb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:44:03 -0400 Subject: [PATCH 161/193] fix: --- .../tests/test_atclient_monitor.c | 4 ++-- .../tests/test_atclient_publickey.c | 15 +++++---------- .../tests/test_atclient_sharedkey.c | 6 ++---- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index e1a96f2b..f71bca69 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -319,8 +319,8 @@ static int test_4_re_pkam_auth_and_start_monitor(atclient *monitor_conn) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_4_re_pkam_auth_and_start_monitor Start\n"); - const char *atserver_host = strdup(monitor_conn->atserver_connection.host); - const int atserver_port = monitor_conn->atserver_connection.port; + 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) { diff --git a/tests/functional_tests/tests/test_atclient_publickey.c b/tests/functional_tests/tests/test_atclient_publickey.c index b1610f96..48281a23 100644 --- a/tests/functional_tests/tests/test_atclient_publickey.c +++ b/tests/functional_tests/tests/test_atclient_publickey.c @@ -139,17 +139,14 @@ static int test_2_get(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } - if ((ret = atclient_get_public_key(atclient, &atkey, value, NULL)) != 0) { + if ((ret = atclient_get_public_key(atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } @@ -261,17 +258,14 @@ static int test_6_get_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; if ((ret = atclient_atkey_create_public_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_atkey_create_public_key\n"); goto exit; } - if ((ret = atclient_get_public_key(atclient, &atkey, value, NULL)) != 0) { + if ((ret = atclient_get_public_key(atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed atclient_get_public_key\n"); goto exit; } @@ -321,6 +315,7 @@ static int test_6_get_with_metadata(atclient *atclient) { goto exit; exit: { + free(value); atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_6_get_with_metadata End (%d)\n", ret); return ret; diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 18e3de55..64e2a86d 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -200,10 +200,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith Begin\n"); - const size_t valuesize = 1024; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -245,6 +242,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { goto exit; exit: { + free(value); atclient_atkey_free(&atkey); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith End (%d)\n", ret); return ret; From aecd3b7efda142e96c5fac27b5aee0df98939634 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:46:14 -0400 Subject: [PATCH 162/193] fix: --- .../tests/test_atclient_sharedkey.c | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_sharedkey.c b/tests/functional_tests/tests/test_atclient_sharedkey.c index 64e2a86d..39d0ac77 100644 --- a/tests/functional_tests/tests/test_atclient_sharedkey.c +++ b/tests/functional_tests/tests/test_atclient_sharedkey.c @@ -150,6 +150,9 @@ static int test_2_get_as_sharedby(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); + atclient_get_shared_key_request_options request_options; + atclient_get_shared_key_request_options_init(&request_options); + char *value = NULL; if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != @@ -158,7 +161,12 @@ static int test_2_get_as_sharedby(atclient *atclient) { goto exit; } - if ((ret = atclient_get_shared_key(atclient, &atkey, &value, NULL)) != 0) { + if((ret = atclient_get_shared_key_request_options_set_store_atkey_metadata(&request_options, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_request_options_set_store_atkey_metadata: %d\n", ret); + goto exit; + } + + if ((ret = atclient_get_shared_key(atclient, &atkey, &value, &request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -190,6 +198,8 @@ static int test_2_get_as_sharedby(atclient *atclient) { goto exit; exit: { atclient_atkey_free(&atkey); + free(value); + atclient_get_shared_key_request_options_free(&request_options); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_get_as_sharedby End (%d)\n", ret); return ret; } @@ -205,13 +215,21 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { atclient_atkey atkey; atclient_atkey_init(&atkey); + atclient_get_shared_key_request_options request_options; + atclient_get_shared_key_request_options_init(&request_options); + if ((ret = atclient_atkey_create_shared_key(&atkey, ATKEY_KEY, ATKEY_SHAREDBY, ATKEY_SHAREDWITH, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret); goto exit; } - if ((ret = atclient_get_shared_key(atclient2, &atkey, &value, NULL)) != 0) { + if((ret = atclient_get_shared_key_request_options_set_store_atkey_metadata(&request_options, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_request_options_set_store_atkey_metadata: %d\n", ret); + goto exit; + } + + if ((ret = atclient_get_shared_key(atclient2, &atkey, &value, &request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -224,7 +242,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value matched: %s == %s\n", value, ATKEY_VALUE); - if (atkey.metadata.ttl != ATKEY_TTL) { + if (atclient_atkey_metadata_is_ttl_initialized(&atkey.metadata) && atkey.metadata.ttl != ATKEY_TTL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttl mismatch. Expected %d, got %d\n", ATKEY_TTL, atkey.metadata.ttl); ret = 1; @@ -232,7 +250,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "ttl matched: %d\n", atkey.metadata.ttl); - if (atkey.metadata.ttr != ATKEY_TTR) { + if (atclient_atkey_metadata_is_ttr_initialized(&atkey.metadata) && atkey.metadata.ttr != ATKEY_TTR) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ttr mismatch. Expected %d, got %d\n", ATKEY_TTR, atkey.metadata.ttr); ret = 1; @@ -244,6 +262,7 @@ static int test_3_get_as_sharedwith(atclient *atclient2) { exit: { free(value); atclient_atkey_free(&atkey); + atclient_get_shared_key_request_options_free(&request_options); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_3_get_as_sharedwith End (%d)\n", ret); return ret; } From e01158b3ca95a2c887a2ed09a743e9ef4d407190 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 19:47:02 -0400 Subject: [PATCH 163/193] fix: --- packages/atclient/src/atclient_get_shared_key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index c7edcad6..eaba42da 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -28,7 +28,7 @@ int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_shared_key_request_options *request_options) { int ret = 1; - if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, request_options, value)) != 0) { + if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_validate_arguments: %d\n", ret); return ret; } From d6f9c240fbdefd784d9150d2381f3e712f5c6161 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 20:00:31 -0400 Subject: [PATCH 164/193] feat: atclient_get_self_key new function signature --- packages/atclient/include/atclient/atclient.h | 8 +- packages/atclient/src/atclient_get_atkeys.c | 2 +- .../atclient/src/atclient_get_public_key.c | 2 +- packages/atclient/src/atclient_get_self_key.c | 113 +++++++----------- .../atclient/src/atclient_get_shared_key.c | 2 +- packages/atclient/src/atclient_put.c | 4 +- packages/atclient/src/atclient_put_self_key.c | 4 +- .../atclient/src/atclient_put_shared_key.c | 2 +- packages/atclient/src/monitor.c | 18 +-- .../tests/test_atclient_selfkey.c | 18 +-- 10 files changed, 65 insertions(+), 108 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index c5c5f83b..e9c78e8e 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -204,13 +204,11 @@ int atclient_put_public_key(atclient *ctx, atclient_atkey *atkey, const char *va * * @param atclient the atclient context (must satisfy the two conditions stated above) * @param atkey the populated atkey to get the value from (must satisfy the two conditions stated above) - * @param value the buffer to hold value gotten from atServer - * @param value_size the buffer length allocated for the value - * @param value_len the output length of the value gotten from atServer + * @param value double pointer to hold value gotten from atServer, can be NULL if you don't need the value, if it is non-null, caller is responsible for freeing the memory + * @param value_len the size of the buffer to hold the value gotten from atServer * @return int 0 on success */ -int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len); +int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_self_key_request_options *request_options); /** * @brief Get a publickey from your atServer or another atServer diff --git a/packages/atclient/src/atclient_get_atkeys.c b/packages/atclient/src/atclient_get_atkeys.c index 8500c5e2..80f90d96 100644 --- a/packages/atclient/src/atclient_get_atkeys.c +++ b/packages/atclient/src/atclient_get_atkeys.c @@ -15,7 +15,7 @@ int atclient_get_atkeys(atclient *atclient, atclient_atkey **atkey, size_t *outp /* * 1. Validate arguments */ - if ((ret = atclient_get_atkeys_validate_arguments(atclient, atkey, output_array_len, request_options)) != 0) { + if ((ret = atclient_get_atkeys_validate_arguments(atclient, (const atclient_atkey **) atkey, output_array_len, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_atkeys_validate_arguments: %d\n", ret); return ret; } diff --git a/packages/atclient/src/atclient_get_public_key.c b/packages/atclient/src/atclient_get_public_key.c index 2a67ca7c..081e067b 100644 --- a/packages/atclient/src/atclient_get_public_key.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -16,7 +16,7 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va /* * 1. Validate arguments */ - if ((ret = atclient_get_public_key_validate_arguments(atclient, atkey, value, request_options)) != + if ((ret = atclient_get_public_key_validate_arguments(atclient, atkey, (const char **) value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_key_validate_arguments: %d\n", ret); return ret; diff --git a/packages/atclient/src/atclient_get_self_key.c b/packages/atclient/src/atclient_get_self_key.c index 91d26b34..9b4e38bb 100644 --- a/packages/atclient/src/atclient_get_self_key.c +++ b/packages/atclient/src/atclient_get_self_key.c @@ -15,16 +15,15 @@ #define TAG "atclient_get_selfkey" static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t value_size, const size_t *value_len); + const char **value, const atclient_get_self_key_request_options *request_options); -int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value, const size_t value_size, - size_t *value_len) { +int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_self_key_request_options *request_options) { int ret = 1; /* * 1. Validate arguments */ - if ((ret = atclient_get_self_key_valid_arguments(atclient, atkey, value, value_size, value_len)) != 0) { + if ((ret = atclient_get_self_key_valid_arguments(atclient, atkey, (const char **) value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_valid_arguments: %d\n", ret); return ret; } @@ -34,7 +33,7 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value */ char *atkey_str = NULL; - const size_t recv_size = value_size; + const size_t recv_size = 8192; // TODO use atclient_connection_read to adaptively read for us unsigned char recv[recv_size]; memset(recv, 0, sizeof(unsigned char) * recv_size); size_t recv_len = 0; @@ -49,9 +48,12 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value // free later cJSON *root = NULL; char *llookup_cmd = NULL; + char *value_raw_encrypted = NULL; char *value_raw = NULL; char *metadata_str = NULL; + atclient_atkey_metadata metadata; + /* * 3. Build `llookup:` command */ @@ -101,16 +103,16 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value goto exit; } - cJSON *metadata = cJSON_GetObjectItem(root, "metaData"); - if (metadata == NULL) { + cJSON *metadata_json = cJSON_GetObjectItem(root, "metaData"); + if (metadata_json == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_GetObjectItem: %d\n", ret); goto exit; } - metadata_str = cJSON_Print(metadata); + metadata_str = cJSON_Print(metadata_json); - if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + if ((ret = atclient_atkey_metadata_from_json_str(&metadata, metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -118,8 +120,8 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value /** * 6. Decrypt value */ - if (atclient_atkey_metadata_is_iv_nonce_initialized(&atkey->metadata)) { - if ((ret = atchops_base64_decode((unsigned char *)atkey->metadata.iv_nonce, strlen(atkey->metadata.iv_nonce), iv, + if (atclient_atkey_metadata_is_iv_nonce_initialized(&metadata)) { + if ((ret = atchops_base64_decode((unsigned char *)metadata.iv_nonce, strlen(metadata.iv_nonce), iv, ATCHOPS_IV_BUFFER_SIZE, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; @@ -144,58 +146,52 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char *value } // holds base64 decoded value. Once decoded, it is encrypted cipher text bytes that need to be decrypted - const size_t value_raw_size = atchops_base64_decoded_size(strlen(data->valuestring)); - if ((value_raw = (char *)malloc(sizeof(char) * value_raw_size)) == NULL) { + const size_t value_raw_encrypted_size = atchops_base64_decoded_size(strlen(data->valuestring)); + if ((value_raw_encrypted = (char *)malloc(sizeof(char) * value_raw_encrypted_size)) == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); goto exit; } - memset(value_raw, 0, sizeof(char) * value_raw_size); - size_t value_raw_len = 0; - - // log data->valuestring - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "data->valuestring: %s\n", data->valuestring); - - // log data->valuestring length - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "data->valuestring length: %zu\n", strlen(data->valuestring)); + memset(value_raw_encrypted, 0, sizeof(char) * value_raw_encrypted_size); + size_t value_raw_encrypted_len = 0; if ((ret = atchops_base64_decode((unsigned char *)data->valuestring, strlen(data->valuestring), - (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { + (unsigned char *)value_raw_encrypted, value_raw_encrypted_size, &value_raw_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); goto exit; } - // log self_encryption_Key - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "self_encryption_key: "); - for (size_t i = 0; i < self_encryption_size; i++) { - printf("%02x ", self_encryption_key[i]); + const size_t value_raw_size = atchops_aes_ctr_plaintext_size(value_raw_encrypted_len); + if ((value_raw = (char *)malloc(sizeof(char) * value_raw_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value_raw\n"); + goto exit; } - printf("\n"); - - // log iv - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "iv: "); - for (size_t i = 0; i < ATCHOPS_IV_BUFFER_SIZE; i++) { - printf("%02x ", iv[i]); + memset(value_raw, 0, sizeof(char) * value_raw_size); + size_t value_raw_len = 0; + if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw_encrypted, value_raw_encrypted_len, + (unsigned char *)value_raw, value_raw_size, &value_raw_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); + goto exit; } - printf("\n"); - // log value_raw - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_raw: "); - for (size_t i = 0; i < value_raw_len; i++) { - printf("%02x ", value_raw[i]); + if(request_options != NULL && atclient_get_self_key_request_options_is_store_atkey_metadata_initialized(request_options) && request_options->store_atkey_metadata) { + if((ret = atclient_atkey_metadata_from_json_str(&atkey->metadata, metadata_str)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); + goto exit; + } } - printf("\n"); - // log value_raw_len - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value_raw_len: %zu\n", value_raw_len); - - // log value %p, value_size, and value_len - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "value: %p, value_size: %zu, value_len: %zu\n", value, value_size, - *value_len); - if ((ret = atchops_aes_ctr_decrypt(self_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value_raw, value_raw_len, - (unsigned char *)value, value_size, value_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_decrypt: %d\n", ret); - goto exit; + if(value != NULL) { + const size_t value_len = value_raw_len; + const size_t value_size = value_len + 1; + if((*value = (char *)malloc(sizeof(char) * (value_size))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for value\n"); + goto exit; + } + memcpy(*value, value_raw, value_len); + (*value)[value_len] = '\0'; } ret = 0; @@ -208,13 +204,14 @@ exit: { free(value_raw); free(llookup_cmd); free(atkey_str); + free(value_raw_encrypted); free(metadata_str); return ret; } } static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, - const char *value, const size_t value_size, const size_t *value_len) { + const char **value, const atclient_get_self_key_request_options *request_options) { int ret = 1; if (atclient == NULL) { @@ -259,24 +256,6 @@ static int atclient_get_self_key_valid_arguments(const atclient *atclient, const goto exit; } - if (value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - goto exit; - } - - if (value_size == 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_size is 0\n"); - goto exit; - } - - if (value_len == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is NULL\n"); - goto exit; - } - ret = 0; exit: { return ret; } } \ No newline at end of file diff --git a/packages/atclient/src/atclient_get_shared_key.c b/packages/atclient/src/atclient_get_shared_key.c index eaba42da..1bd56c7f 100644 --- a/packages/atclient/src/atclient_get_shared_key.c +++ b/packages/atclient/src/atclient_get_shared_key.c @@ -28,7 +28,7 @@ int atclient_get_shared_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_shared_key_request_options *request_options) { int ret = 1; - if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, value, request_options)) != 0) { + if ((ret = atclient_get_shared_key_validate_arguments(atclient, atkey, (const char **) value, request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key_validate_arguments: %d\n", ret); return ret; } diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c index ce521ca7..125746ad 100644 --- a/packages/atclient/src/atclient_put.c +++ b/packages/atclient/src/atclient_put.c @@ -15,7 +15,7 @@ #define TAG "atclient_put" static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const int **commit_id); + const int *commit_id); int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int *commit_id) { int ret = 1; @@ -252,7 +252,7 @@ exit: { } static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const int **commit_id) { + const int *commit_id) { int ret = 1; if (ctx == NULL) { ret = 1; diff --git a/packages/atclient/src/atclient_put_self_key.c b/packages/atclient/src/atclient_put_self_key.c index 7e227d6a..078650dd 100644 --- a/packages/atclient/src/atclient_put_self_key.c +++ b/packages/atclient/src/atclient_put_self_key.c @@ -88,7 +88,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu } memset(iv_base64, 0, sizeof(unsigned char) * iv_base64_size); - if ((ret = atchops_base64_encode(iv, iv_size, iv_base64, iv_base64_size, NULL)) != 0) { + if ((ret = atchops_base64_encode(iv, iv_size, (unsigned char *) iv_base64, iv_base64_size, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; } @@ -121,7 +121,7 @@ int atclient_put_self_key(atclient *ctx, atclient_atkey *atkey, const char *valu size_t value_encrypted_base64_len = 0; memset(value_encrypted_base64, 0, sizeof(char) * value_encrypted_base64_size); - if ((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, value_encrypted_base64, + if ((ret = atchops_base64_encode(value_encrypted, value_encrypted_len, (unsigned char *) value_encrypted_base64, value_encrypted_base64_size, &value_encrypted_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); goto exit; diff --git a/packages/atclient/src/atclient_put_shared_key.c b/packages/atclient/src/atclient_put_shared_key.c index 82618d06..afda4616 100644 --- a/packages/atclient/src/atclient_put_shared_key.c +++ b/packages/atclient/src/atclient_put_shared_key.c @@ -120,7 +120,7 @@ int atclient_put_shared_key(atclient *ctx, atclient_atkey *atkey, const char *va */ size_t value_encrypted_len = 0; memset(value_encrypted, 0, sizeof(unsigned char) * value_encrypted_size); - if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, value, value_len, value_encrypted, + if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (const unsigned char *) value, value_len, value_encrypted, value_encrypted_size, &value_encrypted_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); goto exit; diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 8fc63844..f157f301 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -206,7 +206,7 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } } else { - atclient_atnotification_set_decrypted_value(&(message->notification), (unsigned char *)message->notification.value); + atclient_atnotification_set_decrypted_value(&(message->notification), message->notification.value); } } else if (strcmp(messagetype, "data") == 0) { message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE; @@ -567,24 +567,10 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 3. get shared encryption key to decrypt - ret = atclient_get_shared_encryption_key_shared_by_other(atclient, from_atsign, (char *)sharedenckeybase64); - if (ret != 0) { + if ((ret = atclient_get_shared_encryption_key_shared_by_other(atclient, from_atsign, sharedenckey)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get shared encryption key\n"); goto exit; } - sharedenckeybase64len = strlen((char *)sharedenckeybase64); - - ret = atchops_base64_decode(sharedenckeybase64, sharedenckeybase64len, sharedenckey, sharedenckeysize, - &sharedenckeylen); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to decode shared encryption key\n"); - goto exit; - } - - if (sharedenckeylen != sharedenckeysize) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Invalid shared encryption key length was decoded.\n"); - goto exit; - } // 4. decrypt value ret = atchops_base64_decode((unsigned char *)notification->value, strlen(notification->value), ciphertext, diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index 61fe5a44..2c4f7bf6 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -161,21 +161,18 @@ static int test_3_get(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - const size_t valuesize = 512; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } - if ((ret = atclient_get_self_key(atclient, &atkey, value, valuesize, &valuelen)) != 0) { + if ((ret = atclient_get_self_key(atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%.*s\"\n", (int)valuelen, value); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); if (memcmp(value, ATKEY_VALUE, strlen(ATKEY_VALUE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.value: \"%s\" != \"%s\"\n", value, ATKEY_VALUE); @@ -281,21 +278,18 @@ static int test_7_get_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); - const size_t valuesize = 512; - char value[valuesize]; - memset(value, 0, sizeof(char) * valuesize); - size_t valuelen = 0; + char *value = NULL; if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } - if ((ret = atclient_get_self_key(atclient, &atkey, value, valuesize, &valuelen)) != 0) { + if ((ret = atclient_get_self_key(atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%.*s\"\n", (int)valuelen, value); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "value: \"%s\"\n", value); if (memcmp(value, ATKEY_VALUE, strlen(ATKEY_VALUE)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.value: \"%s\" != \"%s\"\n", value, ATKEY_VALUE); From 536954ed22b60e7eb01deeae9d7e8ce62cdd4b31 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 20:00:43 -0400 Subject: [PATCH 165/193] fix: repl example --- examples/desktop/repl/src/main.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/desktop/repl/src/main.c b/examples/desktop/repl/src/main.c index 8abc384b..34df897f 100644 --- a/examples/desktop/repl/src/main.c +++ b/examples/desktop/repl/src/main.c @@ -135,6 +135,7 @@ int main(int argc, char *argv[]) { continue; } atkeystr[strcspn(atkeystr, "\n")] = 0; + char *value = NULL; atclient_atkey atkey; atclient_atkey_init(&atkey); @@ -152,7 +153,7 @@ int main(int argc, char *argv[]) { goto get_end; } case ATCLIENT_ATKEY_TYPE_PUBLIC_KEY: { - if ((ret = atclient_get_public_key(&atclient, &atkey, recv, recvsize, &recvlen, true)) != 0) { + if ((ret = atclient_get_public_key(&atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_public_key: %d | failed to get public key\n", ret); goto get_end; @@ -161,7 +162,7 @@ int main(int argc, char *argv[]) { break; } case ATCLIENT_ATKEY_TYPE_SELF_KEY: { - if ((ret = atclient_get_self_key(&atclient, &atkey, recv, recvsize, &recvlen)) != 0) { + if ((ret = atclient_get_self_key(&atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key: %d | failed to get self key\n", ret); goto get_end; } @@ -169,17 +170,19 @@ int main(int argc, char *argv[]) { break; } case ATCLIENT_ATKEY_TYPE_SHARED_KEY: { - if ((ret = atclient_get_shared_key(&atclient, &atkey, recv, recvsize, &recvlen, NULL)) != 0) { + if ((ret = atclient_get_shared_key(&atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_shared_key: %d | failed to get shared key\n", ret); goto get_end; } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Value: \"%s\"\n", recv); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Value: \"%s\"\n", value); break; } } - get_end: { atclient_atkey_free(&atkey); } + get_end: { + free(value); + atclient_atkey_free(&atkey); } } else if (strcmp(command, "/scan") == 0) { atclient_get_atkeys_request_options request_options; atclient_get_atkeys_request_options_init(&request_options); From e690cb8fdc46993add7c0e132ccabcd81b34cd12 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 20:02:57 -0400 Subject: [PATCH 166/193] fix: test --- .../tests/test_atclient_selfkey.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/functional_tests/tests/test_atclient_selfkey.c b/tests/functional_tests/tests/test_atclient_selfkey.c index 2c4f7bf6..85f1a99c 100644 --- a/tests/functional_tests/tests/test_atclient_selfkey.c +++ b/tests/functional_tests/tests/test_atclient_selfkey.c @@ -278,6 +278,9 @@ static int test_7_get_with_metadata(atclient *atclient) { atclient_atkey atkey; atclient_atkey_init(&atkey); + atclient_get_self_key_request_options request_options; + atclient_get_self_key_request_options_init(&request_options); + char *value = NULL; if ((ret = atclient_atkey_create_self_key(&atkey, ATKEY_NAME, ATKEY_SHAREDBY, ATKEY_NAMESPACE)) != 0) { @@ -285,7 +288,12 @@ static int test_7_get_with_metadata(atclient *atclient) { goto exit; } - if ((ret = atclient_get_self_key(atclient, &atkey, &value, NULL)) != 0) { + if((ret = atclient_get_self_key_request_options_set_store_atkey_metadata(&request_options, true)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_request_options_set_store_atkey_metadata: %d\n", ret); + goto exit; + } + + if ((ret = atclient_get_self_key(atclient, &atkey, &value, &request_options)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get: %d\n", ret); goto exit; } @@ -305,7 +313,7 @@ static int test_7_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.ttl: %d\n", atkey.metadata.ttl); - if (atkey.metadata.is_encrypted != ATKEY_ISENCRYPTED) { + if (atclient_atkey_metadata_is_is_encrypted_initialized(&atkey.metadata) && atkey.metadata.is_encrypted != ATKEY_ISENCRYPTED) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_encrypted: %d != %d\n", atkey.metadata.is_encrypted, true); ret = 1; @@ -313,7 +321,7 @@ static int test_7_get_with_metadata(atclient *atclient) { } atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atkey.metadata.is_encrypted: %d\n", atkey.metadata.is_encrypted); - if (atkey.metadata.is_binary != ATKEY_ISBINARY) { + if (atclient_atkey_metadata_is_is_binary_initialized(&atkey.metadata) && atkey.metadata.is_binary != ATKEY_ISBINARY) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.is_binary: %d != %d\n", atkey.metadata.is_binary, false); ret = 1; @@ -325,6 +333,7 @@ static int test_7_get_with_metadata(atclient *atclient) { goto exit; exit: { atclient_atkey_free(&atkey); + atclient_get_self_key_request_options_free(&request_options); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_7_get_with_metadata End (%d)\n", ret); return ret; } From 7fdf730defcf6cc3f802850bb9e534a7802efb5d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 20:04:33 -0400 Subject: [PATCH 167/193] fix: crud example --- examples/desktop/crud/get_selfkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/desktop/crud/get_selfkey.c b/examples/desktop/crud/get_selfkey.c index afdfd0b6..08649034 100644 --- a/examples/desktop/crud/get_selfkey.c +++ b/examples/desktop/crud/get_selfkey.c @@ -78,7 +78,7 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkeystr.str (%lu): \"%.*s\"\n", atkeystrlen, (int)(atkeystrlen), atkeystr); - ret = atclient_get_self_key(&atclient, &atkey, value, valuelen, &(valueolen)); + ret = atclient_get_self_key(&atclient, &atkey, &value, NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get self key"); goto exit; From 261f9c829dd588af83ff2f8861031bdf2ec27452 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Fri, 26 Jul 2024 20:06:35 -0400 Subject: [PATCH 168/193] fix: --- examples/desktop/crud/put_selfkey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/desktop/crud/put_selfkey.c b/examples/desktop/crud/put_selfkey.c index 2fe461b5..1e6e967a 100644 --- a/examples/desktop/crud/put_selfkey.c +++ b/examples/desktop/crud/put_selfkey.c @@ -81,7 +81,7 @@ int main() { goto exit; } - if ((ret = atclient_get_self_key(&atclient, &atkey, value, valuelen, &valueolen)) != 0) { + if ((ret = atclient_get_self_key(&atclient, &atkey, &value, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get public key"); goto exit; } From d082fc2e67fe17531d2b2fa71dad6168e5a4ea85 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 10:45:15 -0400 Subject: [PATCH 169/193] feat: connection_hooks.h/.c --- .../atclient/include/atclient/connection.h | 125 +-- .../include/atclient/connection_hooks.h | 64 ++ packages/atclient/src/connection.c | 820 ++++++++++++------ packages/atclient/src/connection_hooks.c | 393 +++++++++ .../tests/test_atclient_connection.c | 14 +- 5 files changed, 1051 insertions(+), 365 deletions(-) create mode 100644 packages/atclient/include/atclient/connection_hooks.h create mode 100644 packages/atclient/src/connection_hooks.c diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index d3930c90..5b48160a 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -7,8 +7,7 @@ #include #include #include - -#define ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE 128 // the size of the buffer for the host name +#include "atclient/connection_hooks.h" // represents the type of connection typedef enum atclient_connection_type { @@ -16,39 +15,21 @@ typedef enum atclient_connection_type { ATCLIENT_CONNECTION_TYPE_ATSERVER // uses 'noop:0\r\n' to check if it is connected } atclient_connection_type; -typedef int(atclient_connection_send_hook)(const unsigned char *src, const size_t src_len, unsigned char *recv, - const size_t recv_size, size_t *recv_len); - -typedef enum atclient_connection_hook_type { - ATCLIENT_CONNECTION_HOOK_TYPE_NONE = 0, - ATCLIENT_CONNECTION_HOOK_TYPE_PRE_SEND, - ATCLIENT_CONNECTION_HOOK_TYPE_POST_SEND, - ATCLIENT_CONNECTION_HOOK_TYPE_PRE_RECV, - ATCLIENT_CONNECTION_HOOK_TYPE_POST_RECV, -} atclient_connection_hook_type; - -typedef struct atclient_connection_hooks { - bool _is_nested_call; // internal variable for preventing infinite recursion (hooks cannot trigger other hooks in - // their nested calls) - atclient_connection_send_hook *pre_send; - atclient_connection_send_hook *post_send; - atclient_connection_send_hook *pre_recv; - atclient_connection_send_hook *post_recv; - bool readonly_src; -} atclient_connection_hooks; - typedef struct atclient_connection { - atclient_connection_type type; + atclient_connection_type type; // set in atclient_connection_init + + bool _is_host_initialized: 1; + char *host; // example: "root.atsign.org" - char host[ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE]; - int port; // example: 64 + bool _is_port_initialized: 1; + uint16_t port; // example: 64 // atclient_connection_connect sets this to true and atclient_connection_disconnect sets this to false // this does not mean that the connection is still alive, it just means that the connection was established at least // once, at some point, check atclient_connection_is_connected for a live status on the connection - // _should_be_connected also serves as an internal boolean to check if the following mbedlts contexts have been + // _is_connection_enabled also serves as an internal boolean to check if the following mbedlts contexts have been // initialized and need to be freed at the end - bool _should_be_connected; + bool _is_connection_enabled: 1; mbedtls_net_context net; mbedtls_ssl_context ssl; mbedtls_ssl_config ssl_config; @@ -56,7 +37,7 @@ typedef struct atclient_connection { mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; - bool _is_hooks_enabled; + bool _is_hooks_enabled: 1; atclient_connection_hooks *hooks; } atclient_connection; @@ -70,6 +51,13 @@ typedef struct atclient_connection { */ void atclient_connection_init(atclient_connection *ctx, atclient_connection_type type); +/** + * @brief free memory allocated by the init function + * + * @param ctx the struct which was previously initialized + */ +void atclient_connection_free(atclient_connection *ctx); + /** * @brief after initializing a connection context, connect to a host and port * @@ -78,7 +66,28 @@ void atclient_connection_init(atclient_connection *ctx, atclient_connection_type * @param port the port to connect to * @return int 0 on success, otherwise error */ -int atclient_connection_connect(atclient_connection *ctx, const char *host, const int port); +int atclient_connection_connect(atclient_connection *ctx, const char *host, const uint16_t port); + +/** + * @brief Reads data from the connection + * + * @param ctx the connection initialized and connected using atclient_connection_init and atclient_connection_connect + * @param value a double pointer that will be allocated by the function to the data read + * @param value_len the length of the data read, will be set by the function + * @param value_max_len the maximum length of the data to read, setting this to 0 means no limit + * @return int 0 on success + */ +int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len); + +/** + * @brief Write data to the connection + * + * @param ctx connection initialized and connected using atclient_connection_init and atclient_connection_connect + * @param value the data to write + * @param value_len the length of the data to write + * @return int 0 on success + */ +int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len); /** * @brief send data to the connection @@ -112,60 +121,4 @@ int atclient_connection_disconnect(atclient_connection *ctx); */ bool atclient_connection_is_connected(atclient_connection *ctx); -/** - * @brief free memory allocated by the init function - * - * @param ctx the struct which was previously initialized - */ -void atclient_connection_free(atclient_connection *ctx); - -/** - * @brief Initialize the hooks memory allocation - * - * @param ctx the struct for the connection - */ -void atclient_connection_enable_hooks(atclient_connection *ctx); - -/** - * @brief Add a hook to be called during the connection lifecycle - * - * @param ctx the struct for the connection - * @param type the hook type you want to add - * @param hook the hook function itself - * - * @return int 0 on success, otherwise error - */ -int atclient_connection_hooks_set(atclient_connection *ctx, atclient_connection_hook_type type, void *hook); - -/** - * @brief Set whether the readonly_src status for all hooks - * - * @param ctx the struct for the connection - * @param readonly_src the new state for readonly_src - * - * @note For performance, keep readonly_src set to true if you don't need to write access to src - */ -void atclient_connection_hooks_set_readonly_src(atclient_connection *ctx, bool readonly_src); - -/** - * @brief Write data to the connection - * - * @param ctx connection initialized and connected using atclient_connection_init and atclient_connection_connect - * @param value the data to write - * @param value_len the length of the data to write - * @return int 0 on success - */ -int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len); - -/** - * @brief Reads data from the connection - * - * @param ctx the connection initialized and connected using atclient_connection_init and atclient_connection_connect - * @param value a double pointer that will be allocated by the function to the data read - * @param value_len the length of the data read, will be set by the function - * @param value_max_len the maximum length of the data to read, setting this to 0 means no limit - * @return int 0 on success - */ -int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len); - #endif diff --git a/packages/atclient/include/atclient/connection_hooks.h b/packages/atclient/include/atclient/connection_hooks.h new file mode 100644 index 00000000..04a48ba4 --- /dev/null +++ b/packages/atclient/include/atclient/connection_hooks.h @@ -0,0 +1,64 @@ +#ifndef ATCLIENT_CONNECTION_HOOKS_H +#define ATCLIENT_CONNECTION_HOOKS_H + +#include +#include +#include + +#define VALUE_INITIALIZED 0b00000001 + +#define ATCLIENT_CONNECTION_HOOKS_PRE_READ_INDEX 0 +#define ATCLIENT_CONNECTION_HOOKS_POST_READ_INDEX 0 +#define ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INDEX 0 +#define ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INDEX 0 + +#define ATCLIENT_CONNECTION_HOOKS_PRE_READ_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_CONNECTION_HOOKS_POST_READ_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INITIALIZED (VALUE_INITIALIZED << 3) + +typedef struct atclient_connection atclient_connection; + +typedef struct atclient_connection_hook_params { + unsigned char *src; + size_t src_len; + unsigned char *recv; + size_t recv_size; + size_t *recv_len; +} atclient_connection_hook_params; + +typedef int(atclient_connection_hook)(atclient_connection_hook_params *params); + +typedef enum atclient_connection_hook_type { + ATCLIENT_CONNECTION_HOOK_TYPE_NONE = 0, + ATCLIENT_CONNECTION_HOOK_TYPE_PRE_READ, + ATCLIENT_CONNECTION_HOOK_TYPE_POST_READ, + ATCLIENT_CONNECTION_HOOK_TYPE_PRE_WRITE, + ATCLIENT_CONNECTION_HOOK_TYPE_POST_WRITE, +} atclient_connection_hook_type; + +typedef struct atclient_connection_hooks { + bool _is_nested_call; + bool readonly_src; + atclient_connection_hook *pre_read; + atclient_connection_hook *post_read; + atclient_connection_hook *pre_write; + atclient_connection_hook *post_write; + uint8_t _initialized_fields[1]; +} atclient_connection_hooks; + +bool atclient_connection_hooks_is_enabled(atclient_connection *ctx); +int atclient_connection_hooks_enable(atclient_connection *conn); +void atclient_connection_hooks_disable(atclient_connection *conn); + +// Q. Why is hook a void pointer? +// A. In case we want to add future hook types which use a different function signature +int atclient_connection_hooks_set(atclient_connection *ctx, const atclient_connection_hook_type type, void *hook); + +bool atclient_connection_hooks_is_pre_read_initialized(const atclient_connection *ctx); +bool atclient_connection_hooks_is_post_read_initialized(const atclient_connection *ctx); +bool atclient_connection_hooks_is_pre_write_initialized(const atclient_connection *ctx); +bool atclient_connection_hooks_is_post_write_initialized(const atclient_connection *ctx); + + +#endif \ No newline at end of file diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 971d187c..64016a4b 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -16,113 +16,125 @@ #define TAG "connection" /* Concatenation of all available CA certificates in PEM format */ -const char cas_pem[] = LETS_ENCRYPT_ROOT GOOGLE_GLOBAL_SIGN GOOGLE_GTS_ROOT_R1 GOOGLE_GTS_ROOT_R2 GOOGLE_GTS_ROOT_R3 - GOOGLE_GTS_ROOT_R4 ZEROSSL_INTERMEDIATE ""; -const size_t cas_pem_len = sizeof(cas_pem); +static const char cas_pem[] = LETS_ENCRYPT_ROOT GOOGLE_GLOBAL_SIGN GOOGLE_GTS_ROOT_R1 GOOGLE_GTS_ROOT_R2 + GOOGLE_GTS_ROOT_R3 GOOGLE_GTS_ROOT_R4 ZEROSSL_INTERMEDIATE ""; +static const size_t cas_pem_len = sizeof(cas_pem); -static void my_debug(void *ctx, int level, const char *file, int line, const char *str) { - ((void)level); - fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str); - fflush((FILE *)ctx); -} +static void my_debug(void *ctx, int level, const char *file, int line, const char *str); -static void init_contexts(atclient_connection *ctx) { - mbedtls_net_init(&(ctx->net)); - mbedtls_ssl_init(&(ctx->ssl)); - mbedtls_ssl_config_init(&(ctx->ssl_config)); - mbedtls_x509_crt_init(&(ctx->cacert)); - mbedtls_entropy_init(&(ctx->entropy)); - mbedtls_ctr_drbg_init(&(ctx->ctr_drbg)); -} +static void atclient_connection_set_is_connection_enabled(atclient_connection *ctx, const bool should_be_connected); +static bool atclient_connection_is_connection_enabled(const atclient_connection *ctx); +static void atclient_connection_enable_connection(atclient_connection *ctx); +static void atclient_connection_disable_connection(atclient_connection *ctx); -static void free_contexts(atclient_connection *ctx) { - mbedtls_net_free(&(ctx->net)); - mbedtls_ssl_free(&(ctx->ssl)); - mbedtls_ssl_config_free(&(ctx->ssl_config)); - mbedtls_x509_crt_free(&(ctx->cacert)); - mbedtls_entropy_free(&(ctx->entropy)); - mbedtls_ctr_drbg_free(&(ctx->ctr_drbg)); -} +static void atclient_connection_set_is_host_initialized(atclient_connection *ctx, const bool is_host_initialized); +static bool atclient_connection_is_host_initialized(const atclient_connection *ctx); +static int atclient_connection_set_host(atclient_connection *ctx, const char *host); +static void atclient_connection_unset_host(atclient_connection *ctx); -static void free_connection_hooks(atclient_connection *ctx) { - if (ctx->hooks != NULL && ctx->_is_hooks_enabled) { - free(ctx->hooks); - ctx->hooks = NULL; - ctx->_is_hooks_enabled = false; - } -} +static void atclient_connection_set_is_port_initialized(atclient_connection *ctx, const bool is_port_initialized); +static bool atclient_connection_is_port_initialized(const atclient_connection *ctx); +static int atclient_connection_set_port(atclient_connection *ctx, const uint16_t port); +static void atclient_connection_unset_port(atclient_connection *ctx); void atclient_connection_init(atclient_connection *ctx, atclient_connection_type type) { memset(ctx, 0, sizeof(atclient_connection)); ctx->type = type; - memset(ctx->host, 0, ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE); - ctx->port = -1; - ctx->_should_be_connected = false; - ctx->hooks = NULL; + ctx->_is_host_initialized = false; + ctx->host = NULL; + ctx->_is_port_initialized = false; + ctx->port = 0; + ctx->_is_connection_enabled = false; ctx->_is_hooks_enabled = false; + ctx->hooks = NULL; +} + +void atclient_connection_free(atclient_connection *ctx) { + if (atclient_connection_is_connection_enabled(ctx)) { + atclient_connection_disable_connection(ctx); + } + if (atclient_connection_hooks_is_enabled(ctx)) { + atclient_connection_hooks_disable(ctx); + } + if (atclient_connection_is_host_initialized(ctx)) { + atclient_connection_unset_host(ctx); + } + if (atclient_connection_is_port_initialized(ctx)) { + atclient_connection_unset_port(ctx); + } + memset(ctx, 0, sizeof(atclient_connection)); } -int atclient_connection_connect(atclient_connection *ctx, const char *host, const int port) { +int atclient_connection_connect(atclient_connection *ctx, const char *host, const uint16_t port) { int ret = 1; - if (ctx->_should_be_connected) { - if((ret = atclient_connection_disconnect(ctx)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_WARN, "atclient_connection_disconnect failed with exit code: %d. Continuing connection anyways..\n", ret); - } + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } + + if (host == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "host is NULL\n"); + return ret; } - init_contexts(ctx); - ctx->_should_be_connected = true; + /* + * 2. Variables + */ + const size_t recv_size = 256; + unsigned char recv[recv_size]; + memset(recv, 0, sizeof(unsigned char) * recv_size); + size_t recv_len = 0; - const size_t readbufsize = 1024; - unsigned char readbuf[readbufsize]; - memset(readbuf, 0, sizeof(unsigned char) * readbufsize); - size_t readbuflen = 0; + const size_t port_str_size = 6; + char port_str[port_str_size]; /* - * 1. Set the ctx->host and ctx->port + * 3. Disable and Reenable connection */ - memcpy(ctx->host, host, strlen(host)); // assume null terminated, example: "root.atsign.org" - ctx->port = port; // example: 64 + if (atclient_connection_is_connection_enabled(ctx)) { + atclient_connection_disable_connection(ctx); + } - char portstr[6]; - sprintf(portstr, "%d", ctx->port); + atclient_connection_enable_connection(ctx); /* - * 2. Parse CA certs + * 3. Parse CA certs */ - ret = mbedtls_x509_crt_parse(&(ctx->cacert), (unsigned char *)cas_pem, cas_pem_len); - if (ret != 0) { + if ((ret = mbedtls_x509_crt_parse(&(ctx->cacert), (unsigned char *)cas_pem, cas_pem_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_x509_crt_parse failed with exit code: %d\n", ret); goto exit; } /* - * 3. Seed the random number generator + * 4. Seed the random number generator */ - - ret = mbedtls_ctr_drbg_seed(&(ctx->ctr_drbg), mbedtls_entropy_func, &(ctx->entropy), - (unsigned char *)ATCHOPS_RNG_PERSONALIZATION, strlen(ATCHOPS_RNG_PERSONALIZATION)); - if (ret != 0) { + if ((ret = mbedtls_ctr_drbg_seed(&(ctx->ctr_drbg), mbedtls_entropy_func, &(ctx->entropy), + (unsigned char *)ATCHOPS_RNG_PERSONALIZATION, + strlen(ATCHOPS_RNG_PERSONALIZATION))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ctr_drbg_seed failed with exit code: %d\n", ret); goto exit; } /* - * 4. Start the socket connection + * 5. Start the socket connection */ - ret = mbedtls_net_connect(&(ctx->net), host, portstr, MBEDTLS_NET_PROTO_TCP); - if (ret != 0) { + snprintf(port_str, port_str_size, "%d", port); + if ((ret = mbedtls_net_connect(&(ctx->net), host, port_str, MBEDTLS_NET_PROTO_TCP)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_net_connect failed with exit code: %d\n", ret); goto exit; } /* - * 5. Prepare the SSL connection + * 6. Prepare the SSL connection */ - ret = mbedtls_ssl_config_defaults(&(ctx->ssl_config), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, - MBEDTLS_SSL_PRESET_DEFAULT); - if (ret != 0) { + if ((ret = mbedtls_ssl_config_defaults(&(ctx->ssl_config), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_config_defaults failed with exit code: %d\n", ret); goto exit; } @@ -134,14 +146,12 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons mbedtls_ssl_conf_read_timeout(&(ctx->ssl_config), ATCLIENT_CLIENT_READ_TIMEOUT_MS); // recv will timeout after X seconds - ret = mbedtls_ssl_setup(&(ctx->ssl), &(ctx->ssl_config)); - if (ret != 0) { + if ((ret = mbedtls_ssl_setup(&(ctx->ssl), &(ctx->ssl_config))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_setup failed with exit code: %d\n", ret); goto exit; } - ret = mbedtls_ssl_set_hostname(&(ctx->ssl), host); - if (ret != 0) { + if ((ret = mbedtls_ssl_set_hostname(&(ctx->ssl), host)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_set_hostname failed with exit code: %d\n", ret); goto exit; } @@ -149,10 +159,9 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons mbedtls_ssl_set_bio(&(ctx->ssl), &(ctx->net), mbedtls_net_send, NULL, mbedtls_net_recv_timeout); /* - * 6. Perform the SSL handshake + * 7. Perform the SSL handshake */ - ret = mbedtls_ssl_handshake(&(ctx->ssl)); - if (ret != 0) { + if ((ret = mbedtls_ssl_handshake(&(ctx->ssl))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_handshake failed with exit code: %d\n", ret); goto exit; } @@ -160,8 +169,7 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons /* * 7. Verify the server certificate */ - ret = mbedtls_ssl_get_verify_result(&(ctx->ssl)); - if (ret != 0) { + if ((ret = mbedtls_ssl_get_verify_result(&(ctx->ssl))) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_get_verify_result failed with exit code: %d\n", ret); goto exit; } @@ -171,59 +179,167 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons // =============== // read anything that was already sent - ret = mbedtls_ssl_read(&(ctx->ssl), readbuf, readbufsize); - if (ret < 0) { + if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv, recv_size)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_read failed with exit code: %d\n", ret); goto exit; } // press enter - ret = mbedtls_ssl_write(&(ctx->ssl), (const unsigned char *)"\r\n", 2); - if (ret < 0) { + if ((ret = mbedtls_ssl_write(&(ctx->ssl), (const unsigned char *)"\r\n", strlen("\r\n"))) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); goto exit; } // read anything that was sent - ret = mbedtls_ssl_read(&(ctx->ssl), readbuf, readbufsize); - if (ret < 0) { + memset(recv, 0, sizeof(unsigned char) * recv_size); + if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv, recv_size)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_read failed with exit code: %d\n", ret); goto exit; } // now we are guaranteed a blank canvas - if (ret > 0) { - ret = 0; // a positive exit code is not an error + if((ret = atclient_connection_set_host(ctx, host)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_set_host failed with exit code: %d\n", ret); + goto exit; + } + + if((ret = atclient_connection_set_port(ctx, port)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_set_port failed with exit code: %d\n", ret); + goto exit; } + + ret = 0; goto exit; exit: { if (ret != 0) { - // undo what we set - memset(ctx->host, 0, ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE); - ctx->port = -1; + atclient_connection_disable_connection(ctx); } return ret; } } +int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (value == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + goto exit; + } + + if (value_len == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is 0\n"); + goto exit; + } + + if (!atclient_connection_is_connection_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); + goto exit; + } + + /* + * 2. Write the value + */ + if ((ret = mbedtls_ssl_write(&(ctx->ssl), value, value_len)) <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); + goto exit; + } + + /* + * 3. Print debug log + */ + if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { + unsigned char *valuecopy = malloc(sizeof(unsigned char) * value_len); + if (valuecopy != NULL) { + memcpy(valuecopy, value, value_len); + atlogger_fix_stdout_buffer((char *)valuecopy, value_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, value_len, valuecopy, + reset); + free(valuecopy); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "Failed to allocate memory to pretty print the network sent transmission\n"); + } + } + + /* + * 4. Call hooks, if they exist + */ + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; + if (try_hooks && ctx->hooks->post_write != NULL) { + ctx->hooks->_is_nested_call = true; + atclient_connection_hook_params params; + params.src = (unsigned char *)value; + params.src_len = value_len; + params.recv = NULL; + params.recv_size = 0; + params.recv_len = NULL; + ret = ctx->hooks->post_write(¶ms); + if (ctx->hooks != NULL) { + ctx->hooks->_is_nested_call = false; + } + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_recv hook failed with exit code: %d\n", ret); + goto exit; + } + } + + ret = 0; + goto exit; +exit: { return ret; } +} + int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_r, const size_t srclen_r, unsigned char *recv, const size_t recvsize_r, size_t *recvlen) { int ret = 1; + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + goto exit; + } + + if (src_r == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is NULL\n"); + goto exit; + } + + if (srclen_r == 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "srclen is 0\n"); + goto exit; + } + + if(!atclient_connection_is_connection_enabled(ctx)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); + goto exit; + } + + /* + * 2. Prep hook stuff + */ // Clone readonly inputs so it is editable by the hooks size_t srclen = srclen_r; size_t recvsize = recvsize_r; - bool try_hooks = ctx->hooks != NULL && !ctx->hooks->_is_nested_call; + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; bool allocate_src = try_hooks && ctx->hooks->readonly_src == false; - unsigned char *src; + unsigned char *src = NULL; if (allocate_src) { - src = malloc(sizeof(unsigned char) * srclen); - if (src == NULL) { + if ((src = malloc(sizeof(unsigned char) * srclen)) == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for src\n"); allocate_src = false; // don't try to free since the memory failed to be allocated goto exit; @@ -233,16 +349,18 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ src = (unsigned char *)src_r; } - if (!ctx->_should_be_connected) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "ctx->_should_be_connected should be true, but is false. You are trying to send messages to a " - "non-connected connection.\n"); - goto exit; - } - - if (try_hooks && ctx->hooks->pre_send != NULL) { + /* + * 3. Call pre_send hook, if it exists + */ + if (try_hooks && atclient_connection_hooks_is_pre_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; - ret = ctx->hooks->pre_send(src, srclen, recv, recvsize, recvlen); + atclient_connection_hook_params params; + params.src = src; + params.src_len = srclen; + params.recv = recv; + params.recv_size = recvsize; + params.recv_len = recvlen; + ret = ctx->hooks->pre_write(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; } @@ -252,15 +370,25 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } - ret = mbedtls_ssl_write(&(ctx->ssl), src, srclen); - if (ret <= 0) { + /* + * 4. Write the value + */ + if ((ret = mbedtls_ssl_write(&(ctx->ssl), src, srclen)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); goto exit; } - if (try_hooks && ctx->hooks->post_send != NULL) { + /* + * 5. Call post_send hook, if it exists + */ + if (try_hooks && atclient_connection_hooks_is_post_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; - ret = ctx->hooks->post_send(src, srclen, recv, recvsize, recvlen); + atclient_connection_hook_params params; + params.src = src; + params.src_len = srclen; + params.recv = recv; + params.recv_size = recvsize; + ret = ctx->hooks->post_write(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; } @@ -270,10 +398,12 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } - unsigned char *srccopy; + /* + * 6. Print debug log + */ if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG && ret == srclen) { - srccopy = malloc(sizeof(unsigned char) * srclen); - if (srccopy != NULL) { + unsigned char *srccopy = NULL; + if ((srccopy = malloc(sizeof(unsigned char) * srclen)) != NULL) { memcpy(srccopy, src, srclen); atlogger_fix_stdout_buffer((char *)srccopy, srclen); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, strlen((char *)srccopy), @@ -285,16 +415,27 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } + /* + * 7. Exit if recv is NULL + */ if (recv == NULL) { ret = 0; goto exit; } + /* + * 8. Run pre read hook, if it exists + */ memset(recv, 0, sizeof(unsigned char) * recvsize); - - if (try_hooks && ctx->hooks->pre_recv != NULL) { + if (try_hooks && atclient_connection_hooks_is_pre_read_initialized(ctx)) { ctx->hooks->_is_nested_call = true; - ret = ctx->hooks->pre_recv(src, srclen, recv, recvsize, recvlen); + atclient_connection_hook_params params; + params.src = src; + params.src_len = srclen; + params.recv = recv; + params.recv_size = recvsize; + params.recv_len = recvlen; + ret = ctx->hooks->pre_read(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; } @@ -304,28 +445,19 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } + /* + * 9. Read the value + */ int tries = 0; bool found = false; size_t l = 0; do { - ret = mbedtls_ssl_read(&(ctx->ssl), recv + l, recvsize - l); - if (ret <= 0) { + if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv + l, recvsize - l)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_read failed with exit code: %d\n", ret); goto exit; } - if (ret == 0) { - tries++; - if (tries >= ATCLIENT_CONNECTION_MAX_READ_TRIES) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "mbedtls_ssl_read tried to read %d times and found nothing: %d\n", tries, ret); - ret = 1; - goto exit; - } - } l = l + ret; - for (int i = l; i >= l - ret && i >= 0; i--) { - // printf("i: %d c: %.2x\n", i, (unsigned char) *(recv + i)); if (*(recv + i) == '\n' || *(recv + i) == '\r') { *recvlen = i; found = true; @@ -335,16 +467,36 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ if (found) { break; } - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == 0 || !found); - - // atlogger_fix_stdout_buffer((char *)recv, *recvlen); recv[*recvlen] = '\0'; // null terminate the string - unsigned char *recvcopy; + /* + * 10. Run post read hook, if it exists + */ + if (try_hooks && atclient_connection_hooks_is_post_read_initialized(ctx)) { + ctx->hooks->_is_nested_call = true; + atclient_connection_hook_params params; + params.src = src; + params.src_len = srclen; + params.recv = recv; + params.recv_size = recvsize; + params.recv_len = recvlen; + ret = ctx->hooks->post_read(¶ms); + if (ctx->hooks != NULL) { + ctx->hooks->_is_nested_call = false; + } + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_recv hook failed with exit code: %d\n", ret); + goto exit; + } + } + + /* + * 11. Print debug log + */ if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { - recvcopy = malloc(sizeof(unsigned char) * (*recvlen)); - if (recvcopy != NULL) { + unsigned char *recvcopy = NULL; + if ((recvcopy = malloc(sizeof(unsigned char) * (*recvlen))) != NULL) { memcpy(recvcopy, recv, *recvlen); atlogger_fix_stdout_buffer((char *)recvcopy, *recvlen); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, *recvlen, recvcopy, @@ -356,18 +508,6 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } - if (try_hooks && ctx->hooks->post_recv != NULL) { - ctx->hooks->_is_nested_call = true; - ret = ctx->hooks->post_recv(src, srclen, recv, recvsize, recvlen); - if (ctx->hooks != NULL) { - ctx->hooks->_is_nested_call = false; - } - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_recv hook failed with exit code: %d\n", ret); - goto exit; - } - } - ret = 0; goto exit; exit: { @@ -381,29 +521,42 @@ exit: { int atclient_connection_disconnect(atclient_connection *ctx) { int ret = 1; - if (!ctx->_should_be_connected) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "ctx->_should_be_connected should be true, but is false, it was never connected in the first place!\n"); - goto exit; + /* + * 1. Validate arguments + */ + if(ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } + + if(!atclient_connection_is_connection_enabled(ctx)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); + return ret; } do { ret = mbedtls_ssl_close_notify(&(ctx->ssl)); } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ || ret != 0); - free_contexts(ctx); - ctx->_should_be_connected = false; + atclient_connection_disable_connection(ctx); ret = 0; - goto exit; exit: { return ret; } } bool atclient_connection_is_connected(atclient_connection *ctx) { - if (!ctx->_should_be_connected) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx->_should_be_connected should be true, but is false\n"); + /* + * 1. Validate arguments + */ + if(ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL, of course it's not connected lol\n"); + return false; + } + + if(!atclient_connection_is_connection_enabled(ctx)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); return false; } @@ -438,138 +591,261 @@ bool atclient_connection_is_connected(atclient_connection *ctx) { return true; } -void atclient_connection_free(atclient_connection *ctx) { - if (ctx->_should_be_connected) { - free_contexts(ctx); +int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, + const size_t value_max_len) { + int ret = 1; +} + +static void my_debug(void *ctx, int level, const char *file, int line, const char *str) { + ((void)level); + fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str); + fflush((FILE *)ctx); +} + +static void atclient_connection_set_is_connection_enabled(atclient_connection *ctx, const bool should_be_connected) { + ctx->_is_connection_enabled = should_be_connected; +} + +static bool atclient_connection_is_connection_enabled(const atclient_connection *ctx) { + return ctx->_is_connection_enabled; +} + +static void atclient_connection_enable_connection(atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return; } - if (ctx->hooks != NULL) { - free(ctx->hooks); + + /* + * 2. Disable connection, if necessary + */ + if (atclient_connection_is_connection_enabled(ctx)) { + atclient_connection_disable_connection(ctx); } - memset(ctx, 0, sizeof(atclient_connection)); - memset(ctx->host, 0, ATCLIENT_CONSTANTS_HOST_BUFFER_SIZE); - ctx->port = -1; - ctx->_should_be_connected = false; + + /* + * 3. Enable the connection + */ + mbedtls_net_init(&(ctx->net)); + mbedtls_ssl_init(&(ctx->ssl)); + mbedtls_ssl_config_init(&(ctx->ssl_config)); + mbedtls_x509_crt_init(&(ctx->cacert)); + mbedtls_entropy_init(&(ctx->entropy)); + mbedtls_ctr_drbg_init(&(ctx->ctr_drbg)); + + /* + * 4. Set the connection enabled flag + */ + atclient_connection_set_is_connection_enabled(ctx, true); } -void atclient_connection_enable_hooks(atclient_connection *ctx) { - ctx->hooks = malloc(sizeof(atclient_connection_hooks)); // TODO handle malloc failure - memset(ctx->hooks, 0, sizeof(atclient_connection_hooks)); - ctx->hooks->readonly_src = true; - ctx->_is_hooks_enabled = true; +static void atclient_connection_disable_connection(atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return; + } + + /* + * 2. Free the contexts + */ + if (atclient_connection_is_connection_enabled(ctx)) { + mbedtls_net_free(&(ctx->net)); + mbedtls_ssl_free(&(ctx->ssl)); + mbedtls_ssl_config_free(&(ctx->ssl_config)); + mbedtls_x509_crt_free(&(ctx->cacert)); + mbedtls_entropy_free(&(ctx->entropy)); + mbedtls_ctr_drbg_free(&(ctx->ctr_drbg)); + } + + /* + * 3. Set the connection disabled flag + */ + atclient_connection_set_is_connection_enabled(ctx, false); } -// Q. Why is hook a void pointer? -// A. In case we want to add future hook types which use a different function signature -int atclient_connection_hooks_set(atclient_connection *ctx, atclient_connection_hook_type type, void *hook) { - atclient_connection_hooks *hooks = ctx->hooks; - if (hooks == NULL || !ctx->_is_hooks_enabled) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "Make sure to enable hooks struct before trying to set a hook\n"); - return -1; - } - - switch (type) { - case ATCLIENT_CONNECTION_HOOK_TYPE_NONE: - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Received 'NONE' hook as hook set input type\n"); - return 1; - case ATCLIENT_CONNECTION_HOOK_TYPE_PRE_SEND: - hooks->pre_send = (atclient_connection_send_hook *)hook; - break; - case ATCLIENT_CONNECTION_HOOK_TYPE_POST_SEND: - hooks->post_send = (atclient_connection_send_hook *)hook; - break; - case ATCLIENT_CONNECTION_HOOK_TYPE_PRE_RECV: - hooks->pre_recv = (atclient_connection_send_hook *)hook; - break; - case ATCLIENT_CONNECTION_HOOK_TYPE_POST_RECV: - hooks->post_recv = (atclient_connection_send_hook *)hook; - break; - } - - return 0; +static void atclient_connection_set_is_host_initialized(atclient_connection *ctx, const bool is_host_initialized) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return; + } + + /* + * 2. Set the host initialized flag + */ + ctx->_is_host_initialized = is_host_initialized; } -void atclient_connection_hooks_set_readonly_src(atclient_connection *ctx, bool readonly_src) { - if (ctx->hooks == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "Make sure to enable hooks struct before trying to set readonly_src\n"); +static bool atclient_connection_is_host_initialized(const atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return false; + } + + /* + * 2. Return the host initialized flag + */ + return ctx->_is_host_initialized; +} + +static int atclient_connection_set_host(atclient_connection *ctx, const char *host) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } + + if (host == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "host is NULL\n"); + return ret; + } + + /* + * 2. Allocate memory for the host + */ + const size_t host_len = strlen(host); + const size_t host_size = host_len + 1; + if ((ctx->host = malloc(sizeof(char) * host_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for host\n"); + goto exit; + } + + /* + * 3. Copy the host + */ + memcpy(ctx->host, host, host_len); + ctx->host[host_len] = '\0'; + + /* + * 4. Set the host initialized flag + */ + atclient_connection_set_is_host_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_unset_host(atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return; + } + + /* + * 2. Free the host + */ + if (atclient_connection_is_host_initialized(ctx)) { + free(ctx->host); + } + ctx->host = NULL; + + /* + * 3. Unset the host initialized flag + */ + atclient_connection_set_is_host_initialized(ctx, false); +} + +static void atclient_connection_set_is_port_initialized(atclient_connection *ctx, const bool is_port_initialized) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); return; } - ctx->hooks->readonly_src = readonly_src; + + /* + * 2. Set the port initialized flag + */ + ctx->_is_port_initialized = is_port_initialized; } -// int atclient_connection_write(atclient_connection *ctx, const unsigned char *value, const size_t value_len) { -// int ret = 1; - -// /* -// * 1. Validate arguments -// */ -// if(ctx == NULL) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); -// goto exit; -// } - -// if(value == NULL) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); -// goto exit; -// } - -// if(value_len == 0) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value_len is 0\n"); -// goto exit; -// } - -// if (!ctx->_should_be_connected) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, -// "ctx->_should_be_connected should be true, but is false, you are trying to write to a non-connected " -// "connection\n"); -// goto exit; -// } - -// /* -// * 2. Write the value -// */ -// if ((ret = mbedtls_ssl_write(&(ctx->ssl), value, value_len)) <= 0) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); -// goto exit; -// } - -// /* -// * 3. Print debug log -// */ -// if(atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { -// unsigned char *valuecopy = malloc(sizeof(unsigned char) * value_len); -// if (valuecopy != NULL) { -// memcpy(valuecopy, value, value_len); -// atlogger_fix_stdout_buffer((char *)valuecopy, value_len); -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, value_len, valuecopy, reset); -// free(valuecopy); -// } else { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory to pretty print the network sent transmission\n"); -// } -// } - -// /* -// * 4. Call hooks, if they exist -// */ -// bool try_hooks = ctx->hooks != NULL && !ctx->hooks->_is_nested_call; -// if (try_hooks && ctx->hooks->post_recv != NULL) { -// ctx->hooks->_is_nested_call = true; -// ret = ctx->hooks->post_recv(src, srclen, recv, recvsize, recvlen) -// if (ctx->hooks != NULL) { -// ctx->hooks->_is_nested_call = false; -// } -// if (ret != 0) { -// atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_recv hook failed with exit code: %d\n", ret); -// goto exit; -// } -// } - -// ret = 0; -// goto exit; -// exit: { -// return ret; -// } -// } - -// int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len); \ No newline at end of file +static bool atclient_connection_is_port_initialized(const atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return false; + } + + /* + * 2. Return the port initialized flag + */ + return ctx->_is_port_initialized; +} + +static int atclient_connection_set_port(atclient_connection *ctx, const uint16_t port) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } + + if (port < 0) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "port is less than 0\n"); + return ret; + } + + /* + * 2. Set the port + */ + ctx->port = port; + + /* + * 3. Set the port initialized flag + */ + atclient_connection_set_is_port_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_unset_port(atclient_connection *ctx) { + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return; + } + + /* + * 2. Unset the port + */ + ctx->port = 0; + + /* + * 3. Unset the port initialized flag + */ + atclient_connection_set_is_port_initialized(ctx, false); +} diff --git a/packages/atclient/src/connection_hooks.c b/packages/atclient/src/connection_hooks.c new file mode 100644 index 00000000..64e7b2e0 --- /dev/null +++ b/packages/atclient/src/connection_hooks.c @@ -0,0 +1,393 @@ +#include "atclient/connection.h" +#include +#include +#include +#include "atclient/connection_hooks.h" +#include + +#define TAG "connection_hooks" + +static void atclient_connection_hooks_set_is_enabled(atclient_connection *ctx, const bool enabled); + +static void atclient_connection_hooks_set_is_pre_read_initialized(atclient_connection *ctx, const bool initialized); +static int atclient_connection_hooks_set_pre_read(atclient_connection *ctx, atclient_connection_hook *hook); +static void atclient_connection_hooks_unset_pre_read(atclient_connection *ctx); + +static void atclient_connection_hooks_set_is_post_read_initialized(atclient_connection *ctx, const bool initialized); +static int atclient_connection_hooks_set_post_read(atclient_connection *ctx, atclient_connection_hook *hook); +static void atclient_connection_hooks_unset_post_read(atclient_connection *ctx); + +static void atclient_connection_hooks_set_is_pre_write_initialized(atclient_connection *ctx, const bool initialized); +static int atclient_connection_hooks_set_pre_write(atclient_connection *ctx, atclient_connection_hook *hook); +static void atclient_connection_hooks_unset_pre_write(atclient_connection *ctx); + +static void atclient_connection_hooks_set_is_post_write_initialized(atclient_connection *ctx, const bool initialized); +static int atclient_connection_hooks_set_post_write(atclient_connection *ctx, atclient_connection_hook *hook); +static void atclient_connection_hooks_unset_post_write(atclient_connection *ctx); + +bool atclient_connection_hooks_is_enabled(atclient_connection *ctx) { + if (ctx->hooks == NULL) { + return false; + } + return ctx->_is_hooks_enabled; +} + +int atclient_connection_hooks_enable(atclient_connection *conn) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (conn == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + /* + * 2. Disable hooks if they are already enabled + */ + if (atclient_connection_hooks_is_enabled(conn)) { + atclient_connection_hooks_disable(conn); + } + + /* + * 3. Allocate memory for the hooks struct + */ + if ((conn->hooks = malloc(sizeof(atclient_connection_hooks))) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for connection hooks\n"); + goto exit; + } + memset(conn->hooks, 0, sizeof(atclient_connection_hooks)); + atclient_connection_hooks_set_is_enabled(conn, true); + + /* + * 4. Set any defaults + */ + conn->hooks->readonly_src = true; + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_connection_hooks_disable(atclient_connection *conn) { + /* + * 1. Validate arguments + */ + if (conn == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return; + } + + /* + * 2. Free the hooks struct + */ + if (atclient_connection_hooks_is_enabled(conn)) { + free(conn->hooks); + } + atclient_connection_hooks_set_is_enabled(conn, false); + conn->hooks = NULL; +} + +int atclient_connection_hooks_set(atclient_connection *ctx, const atclient_connection_hook_type type, void *hook) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + if (type == ATCLIENT_CONNECTION_HOOK_TYPE_NONE) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Received 'NONE' hook as hook set input type\n"); + return ret; + } + + if (hook == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Hook is NULL\n"); + return ret; + } + + if (!atclient_connection_hooks_is_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Make sure to enable hooks struct before trying to set a hook\n"); + return ret; + } + + /* + * 2. Set the hook + */ + switch (type) { + case ATCLIENT_CONNECTION_HOOK_TYPE_NONE: + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Received 'NONE' hook as hook set input type\n"); + goto exit; + case ATCLIENT_CONNECTION_HOOK_TYPE_PRE_READ: { + if((ret = atclient_connection_hooks_set_pre_read(ctx, (atclient_connection_hook *)hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set pre read hook\n"); + goto exit; + } + break; + } + case ATCLIENT_CONNECTION_HOOK_TYPE_POST_READ: { + if((ret = atclient_connection_hooks_set_post_read(ctx, (atclient_connection_hook *)hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set post read hook\n"); + goto exit; + } + break; + } + case ATCLIENT_CONNECTION_HOOK_TYPE_PRE_WRITE: { + if((ret = atclient_connection_hooks_set_pre_write(ctx, (atclient_connection_hook *)hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set pre write hook\n"); + goto exit; + } + break; + } + case ATCLIENT_CONNECTION_HOOK_TYPE_POST_WRITE: { + if((ret = atclient_connection_hooks_set_post_write(ctx, (atclient_connection_hook *)hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set post write hook\n"); + goto exit; + } + break; + } + } + + ret = 0; + goto exit; +exit: { return ret; } +} + +bool atclient_connection_hooks_is_pre_read_initialized(const atclient_connection *ctx) { + return ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_READ_INDEX] & + ATCLIENT_CONNECTION_HOOKS_PRE_READ_INITIALIZED; +} + +bool atclient_connection_hooks_is_post_read_initialized(const atclient_connection *ctx) { + return ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_READ_INDEX] & + ATCLIENT_CONNECTION_HOOKS_POST_READ_INITIALIZED; +} + +bool atclient_connection_hooks_is_pre_write_initialized(const atclient_connection *ctx) { + return ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INDEX] & + ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INITIALIZED; +} + +bool atclient_connection_hooks_is_post_write_initialized(const atclient_connection *ctx) { + return ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INDEX] & + ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INITIALIZED; +} + +static void atclient_connection_hooks_set_is_enabled(atclient_connection *ctx, const bool enabled) { + ctx->_is_hooks_enabled = enabled; +} + +static void atclient_connection_hooks_set_is_pre_read_initialized(atclient_connection *ctx, const bool initialized) { + if (initialized) { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_READ_INDEX] |= + ATCLIENT_CONNECTION_HOOKS_PRE_READ_INITIALIZED; + } else { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_READ_INDEX] &= + ~ATCLIENT_CONNECTION_HOOKS_PRE_READ_INITIALIZED; + } +} + +static int atclient_connection_hooks_set_pre_read(atclient_connection *ctx, atclient_connection_hook *hook) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + if (!atclient_connection_hooks_is_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Make sure to enable hooks struct before trying to set a hook\n"); + return ret; + } + + if (hook == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Hook is NULL\n"); + return ret; + } + + if (atclient_connection_hooks_is_pre_read_initialized(ctx)) { + atclient_connection_hooks_unset_pre_read(ctx); + } + + ctx->hooks->pre_read = hook; + atclient_connection_hooks_set_is_pre_read_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_hooks_unset_pre_read(atclient_connection *ctx) { + ctx->hooks->pre_read = NULL; + atclient_connection_hooks_set_is_pre_read_initialized(ctx, false); +} + +static void atclient_connection_hooks_set_is_post_read_initialized(atclient_connection *ctx, const bool initialized) { + if (initialized) { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_READ_INDEX] |= + ATCLIENT_CONNECTION_HOOKS_POST_READ_INITIALIZED; + } else { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_READ_INDEX] &= + ~ATCLIENT_CONNECTION_HOOKS_POST_READ_INITIALIZED; + } +} + +static int atclient_connection_hooks_set_post_read(atclient_connection *ctx, atclient_connection_hook *hook) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + if (!atclient_connection_hooks_is_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Make sure to enable hooks struct before trying to set a hook\n"); + return ret; + } + + if (hook == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Hook is NULL\n"); + return ret; + } + + if (atclient_connection_hooks_is_post_read_initialized(ctx)) { + atclient_connection_hooks_unset_post_read(ctx); + } + + ctx->hooks->post_read = hook; + atclient_connection_hooks_set_is_post_read_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_hooks_unset_post_read(atclient_connection *ctx) { + ctx->hooks->post_read = NULL; + atclient_connection_hooks_set_is_post_read_initialized(ctx, false); +} + +static void atclient_connection_hooks_set_is_pre_write_initialized(atclient_connection *ctx, const bool initialized) { + if (initialized) { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INDEX] |= + ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INITIALIZED; + } else { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INDEX] &= + ~ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INITIALIZED; + } +} + +static int atclient_connection_hooks_set_pre_write(atclient_connection *ctx, atclient_connection_hook *hook) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + if (!atclient_connection_hooks_is_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Make sure to enable hooks struct before trying to set a hook\n"); + return ret; + } + + if (hook == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Hook is NULL\n"); + return ret; + } + + if (atclient_connection_hooks_is_pre_write_initialized(ctx)) { + atclient_connection_hooks_unset_pre_write(ctx); + } + + ctx->hooks->pre_write = hook; + atclient_connection_hooks_set_is_pre_write_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_hooks_unset_pre_write(atclient_connection *ctx) { + ctx->hooks->pre_write = NULL; + atclient_connection_hooks_set_is_pre_write_initialized(ctx, false); +} + +static void atclient_connection_hooks_set_is_post_write_initialized(atclient_connection *ctx, const bool initialized) { + if (initialized) { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INDEX] |= + ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INITIALIZED; + } else { + ctx->hooks->_initialized_fields[ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INDEX] &= + ~ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INITIALIZED; + } +} + +static int atclient_connection_hooks_set_post_write(atclient_connection *ctx, atclient_connection_hook *hook) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is NULL\n"); + return ret; + } + + if (!atclient_connection_hooks_is_enabled(ctx)) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Make sure to enable hooks struct before trying to set a hook\n"); + return ret; + } + + if (hook == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Hook is NULL\n"); + return ret; + } + + if (atclient_connection_hooks_is_post_write_initialized(ctx)) { + atclient_connection_hooks_unset_post_write(ctx); + } + + ctx->hooks->post_write = hook; + atclient_connection_hooks_set_is_post_write_initialized(ctx, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +static void atclient_connection_hooks_unset_post_write(atclient_connection *ctx) { + ctx->hooks->post_write = NULL; + atclient_connection_hooks_set_is_post_write_initialized(ctx, false); +} \ No newline at end of file diff --git a/tests/functional_tests/tests/test_atclient_connection.c b/tests/functional_tests/tests/test_atclient_connection.c index e72581df..fde2f855 100644 --- a/tests/functional_tests/tests/test_atclient_connection.c +++ b/tests/functional_tests/tests/test_atclient_connection.c @@ -142,7 +142,7 @@ static int test_1_initialize(atclient_connection *conn) { atclient_connection_init(conn, ATCLIENT_CONNECTION_TYPE_ATDIRECTORY); - if ((ret = assert_equals(conn->_should_be_connected, false)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "conn->_should_be_connected should be false, but is true\n"); goto exit; } @@ -166,7 +166,7 @@ static int test_2_connect(atclient_connection *conn) { goto exit; } - if ((ret = assert_equals(conn->_should_be_connected, true)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "root_conn._should_be_connected should be true, but is false\n"); goto exit; } @@ -234,7 +234,7 @@ static int test_5_disconnect(atclient_connection *conn) { goto exit; } - if ((ret = assert_equals(conn->_should_be_connected, false)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "root_conn._should_be_connected should be false, but is true\n"); goto exit; } @@ -308,7 +308,7 @@ static int test_8_reconnect(atclient_connection *conn) { goto exit; } - if (!conn->_should_be_connected) { + if (!conn->_is_connection_enabled) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx->_should_be_connected should be true, but is false\n"); ret = 1; goto exit; @@ -345,7 +345,7 @@ static int test_10_free(atclient_connection *conn) { atclient_connection_free(conn); - if ((ret = assert_equals(conn->_should_be_connected, false)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "conn->_should_be_connected should be false, but is true\n"); goto exit; } @@ -363,7 +363,7 @@ static int test_11_initialize(atclient_connection *conn) { atclient_connection_init(conn, ATCLIENT_CONNECTION_TYPE_ATDIRECTORY); - if ((ret = assert_equals(conn->_should_be_connected, false)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "conn->_should_be_connected should be false, but is true\n"); goto exit; } @@ -491,7 +491,7 @@ static int test_17_should_be_connected_should_be_true(atclient_connection *conn) int ret = 1; - if ((ret = assert_equals(conn->_should_be_connected, true)) != 0) { + if ((ret = assert_equals(conn->_is_connection_enabled, true)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "conn->_should_be_connected should be true, but is false\n"); goto exit; } From efe874135c7aa53b9eb20b17567ceacb711165ac Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 10:48:47 -0400 Subject: [PATCH 170/193] fix: atclient_connection_free --- packages/atclient/src/connection.c | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 64016a4b..f44f6e3f 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -1,4 +1,5 @@ #include "atclient/connection.h" +#include "atclient/connection_hooks.h" #include "atchops/constants.h" #include "atclient/cacerts.h" #include "atclient/constants.h" From c1ec3ac4965e8d6368663f826b170e4a381dd237 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 10:57:43 -0400 Subject: [PATCH 171/193] fix: --- packages/atclient/CMakeLists.txt | 1 + packages/atclient/src/connection.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index a8f3b5ba..37a08de0 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -20,6 +20,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c ${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c ${CMAKE_CURRENT_LIST_DIR}/src/atnotification.c + ${CMAKE_CURRENT_LIST_DIR}/src/connection_hooks.c ${CMAKE_CURRENT_LIST_DIR}/src/connection.c ${CMAKE_CURRENT_LIST_DIR}/src/encryption_key_helpers.c ${CMAKE_CURRENT_LIST_DIR}/src/metadata.c diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index f44f6e3f..4937bfd3 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -1,7 +1,7 @@ #include "atclient/connection.h" -#include "atclient/connection_hooks.h" #include "atchops/constants.h" #include "atclient/cacerts.h" +#include "atclient/connection_hooks.h" #include "atclient/constants.h" #include "atlogger/atlogger.h" #include @@ -200,12 +200,12 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons // now we are guaranteed a blank canvas - if((ret = atclient_connection_set_host(ctx, host)) != 0) { + if ((ret = atclient_connection_set_host(ctx, host)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_set_host failed with exit code: %d\n", ret); goto exit; } - if((ret = atclient_connection_set_port(ctx, port)) != 0) { + if ((ret = atclient_connection_set_port(ctx, port)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_set_port failed with exit code: %d\n", ret); goto exit; } @@ -276,7 +276,7 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val /* * 4. Call hooks, if they exist */ - bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && ctx->hooks != NULL && !ctx->hooks->_is_nested_call; if (try_hooks && ctx->hooks->post_write != NULL) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; @@ -322,7 +322,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ goto exit; } - 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"); goto exit; } @@ -525,12 +525,12 @@ int atclient_connection_disconnect(atclient_connection *ctx) { /* * 1. Validate arguments */ - if(ctx == NULL) { + if (ctx == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); 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; } @@ -551,12 +551,12 @@ bool atclient_connection_is_connected(atclient_connection *ctx) { /* * 1. Validate arguments */ - if(ctx == NULL) { + if (ctx == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL, of course it's not connected lol\n"); return false; } - 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 false; } From 07ca1721b292c2abbaa931ef6f384e2f4e379939 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 12:53:46 -0400 Subject: [PATCH 172/193] fix: --- packages/atclient/src/connection.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 4937bfd3..fccc7155 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -309,22 +309,22 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ */ if (ctx == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); - goto exit; + return ret; } if (src_r == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is NULL\n"); - goto exit; + return ret; } if (srclen_r == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "srclen is 0\n"); - goto exit; + return ret; } if (!atclient_connection_is_connection_enabled(ctx)) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); - goto exit; + return ret; } /* From 0328b732a9c3037bb23077d0e2261e3d93b8c55b Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 17:30:12 -0400 Subject: [PATCH 173/193] feat: connection_hooks example --- examples/desktop/connection_hooks/.gitignore | 2 + .../desktop/connection_hooks/CMakeLists.txt | 10 + examples/desktop/connection_hooks/main.c | 163 +++++++++++++++++ examples/desktop/connection_hooks/run.sh | 25 +++ .../sample_cmake_project/CMakeLists.txt | 4 +- .../desktop/sample_cmake_project/README.md | 6 +- examples/desktop/temp/CMakeLists.txt | 10 - .../atclient/include/atclient/connection.h | 4 +- packages/atclient/src/connection.c | 173 +++++++++++++++++- 9 files changed, 378 insertions(+), 19 deletions(-) create mode 100644 examples/desktop/connection_hooks/.gitignore create mode 100644 examples/desktop/connection_hooks/CMakeLists.txt create mode 100644 examples/desktop/connection_hooks/main.c create mode 100755 examples/desktop/connection_hooks/run.sh delete mode 100644 examples/desktop/temp/CMakeLists.txt diff --git a/examples/desktop/connection_hooks/.gitignore b/examples/desktop/connection_hooks/.gitignore new file mode 100644 index 00000000..c43d399e --- /dev/null +++ b/examples/desktop/connection_hooks/.gitignore @@ -0,0 +1,2 @@ +/*build*/ +/*bin*/ \ No newline at end of file diff --git a/examples/desktop/connection_hooks/CMakeLists.txt b/examples/desktop/connection_hooks/CMakeLists.txt new file mode 100644 index 00000000..c85afca6 --- /dev/null +++ b/examples/desktop/connection_hooks/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.24) + +project(temp) + +find_package(atsdk REQUIRED) + +add_executable(main ${CMAKE_CURRENT_LIST_DIR}/main.c) +target_link_libraries(main atsdk::atclient) + +install(TARGETS main DESTINATION ${CMAKE_CURRENT_LIST_DIR}/bin) diff --git a/examples/desktop/connection_hooks/main.c b/examples/desktop/connection_hooks/main.c new file mode 100644 index 00000000..d4f7f01d --- /dev/null +++ b/examples/desktop/connection_hooks/main.c @@ -0,0 +1,163 @@ +#include +#include +#include +#include + +#define TAG "main" + +#define HOST "3b419d7a-2fee-5080-9289-f0e1853abb47.swarm0002.atsign.zone" +#define PORT 5770 + +void *pre_read_hook(atclient_connection_hook_params *params) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "pre_read_hook was called\n"); + // log params + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src (%p): \"%s\"\n", params->recv, (char *)params->src); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src_len: %d\n", params->src_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv (%p): %s\n", params->recv, (char *)params->recv); + if (params->recv_len != NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: %d\n", *params->recv_len); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: NULL\n"); + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_size: %d\n", params->recv_size); + return NULL; +} + +void *post_read_hook(atclient_connection_hook_params *params) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "post_read_hook was called\n"); + // log params + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src (%p): \"%s\"\n", params->recv, (char *)params->src); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src_len: %d\n", params->src_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv (%p): %s\n", params->recv, (char *)params->recv); + if (params->recv_len != NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: %d\n", *params->recv_len); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: NULL\n"); + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_size: %d\n", params->recv_size); + + return NULL; +} + +void *pre_write_hook(atclient_connection_hook_params *params) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "pre_write_hook was called\n"); + // log params + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src (%p): \"%s\"\n", params->recv, (char *)params->src); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src_len: %d\n", params->src_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv (%p): %s\n", params->recv, (char *)params->recv); + if (params->recv_len != NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: %d\n", *params->recv_len); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: NULL\n"); + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_size: %d\n", params->recv_size); + return NULL; +} + +void *post_write_hook(atclient_connection_hook_params *params) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "post_write_hook was called\n"); + // log params + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src (%p): \"%s\"\n", params->recv, (char *)params->src); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "src_len: %d\n", params->src_len); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv (%p): %s\n", params->recv, (char *)params->recv); + if (params->recv_len != NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: %d\n", *params->recv_len); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_len: NULL\n"); + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "recv_size: %d\n", params->recv_size); + return NULL; +} + +int main(int argc, char *argv[]) { + int ret = 1; + + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); + + atclient_connection conn; + atclient_connection_init(&conn, ATCLIENT_CONNECTION_TYPE_ATSERVER); + + const unsigned char *src = "from:12alpaca\r\n"; + const size_t src_len = strlen(src); + unsigned char *recv = NULL; + size_t recv_len = 0; + + if ((ret = atclient_connection_hooks_enable(&conn)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to enable hooks\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Enabled hooks\n"); + + if ((ret = atclient_connection_hooks_set(&conn, ATCLIENT_CONNECTION_HOOK_TYPE_PRE_READ, pre_read_hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set pre_read hook\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Set pre_read_hook successfully\n"); + + if ((ret = atclient_connection_hooks_set(&conn, ATCLIENT_CONNECTION_HOOK_TYPE_POST_READ, post_read_hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set post_read hook\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Set post_read_hook successfully\n"); + + if ((ret = atclient_connection_hooks_set(&conn, ATCLIENT_CONNECTION_HOOK_TYPE_PRE_WRITE, pre_write_hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set pre_write hook\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Set pre_write_hook successfully\n"); + + if ((ret = atclient_connection_hooks_set(&conn, ATCLIENT_CONNECTION_HOOK_TYPE_POST_WRITE, post_write_hook)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set post_write hook\n"); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Set post_write_hook successfully\n"); + + if ((ret = atclient_connection_connect(&conn, HOST, PORT)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to connect to %s:%d\n", HOST, PORT); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Connected to %s:%d\n", HOST, PORT); + + if ((ret = atclient_connection_write(&conn, (const unsigned char *)src, src_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to write to %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_write(&conn, (const unsigned char *)src, src_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to write to %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 20)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 20)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 20)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 0)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); + goto exit; + } + + if ((ret = atclient_connection_disconnect(&conn)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to disconnect from %s:%d\n", HOST, PORT); + goto exit; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Disconnected from %s:%d\n", HOST, PORT); + + ret = 0; + goto exit; +exit: { + atclient_connection_free(&conn); + return ret; +} +} \ No newline at end of file diff --git a/examples/desktop/connection_hooks/run.sh b/examples/desktop/connection_hooks/run.sh new file mode 100755 index 00000000..c29ca904 --- /dev/null +++ b/examples/desktop/connection_hooks/run.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -eu + +# Clean +rm -f build/CMakeCache.txt +rm -f bin/main + +# Install dependencies +FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[0]}")" +SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")" + +# Install atsdk +"$SCRIPT_DIRECTORY/../../../tools/install.sh" + +# Build +cd "$SCRIPT_DIRECTORY" + +## CMake configure +cmake -S . -B build + +## CMake build +cmake --build build --target install + +# Run +./bin/main $@ diff --git a/examples/desktop/sample_cmake_project/CMakeLists.txt b/examples/desktop/sample_cmake_project/CMakeLists.txt index fa4b7b83..deb9d4b5 100644 --- a/examples/desktop/sample_cmake_project/CMakeLists.txt +++ b/examples/desktop/sample_cmake_project/CMakeLists.txt @@ -4,5 +4,5 @@ project(sample_cmake_project) find_package(atsdk REQUIRED CONFIG) message(STATUS "[atsdk] Found package!") -add_executable(exec ${CMAKE_CURRENT_LIST_DIR}/main.c) -target_link_libraries(exec PRIVATE atsdk::atclient) +add_executable(main ${CMAKE_CURRENT_LIST_DIR}/main.c) +target_link_libraries(main PRIVATE atsdk::atclient) diff --git a/examples/desktop/sample_cmake_project/README.md b/examples/desktop/sample_cmake_project/README.md index 33d0717f..f5464588 100644 --- a/examples/desktop/sample_cmake_project/README.md +++ b/examples/desktop/sample_cmake_project/README.md @@ -29,8 +29,8 @@ project(sample_cmake_project) find_package(atsdk REQUIRED CONFIG) message(STATUS "[atsdk] Found package!") -add_executable(exec ${CMAKE_CURRENT_LIST_DIR}/main.c) -target_link_libraries(exec PRIVATE atsdk::atclient) +add_executable(main ${CMAKE_CURRENT_LIST_DIR}/main.c) +target_link_libraries(main PRIVATE atsdk::atclient) ``` Next, you can build your project: @@ -45,5 +45,5 @@ This will build your project with the atclient library. To run the project: ```bash -./build/exec +./build/main ``` diff --git a/examples/desktop/temp/CMakeLists.txt b/examples/desktop/temp/CMakeLists.txt deleted file mode 100644 index 4c5ec7a3..00000000 --- a/examples/desktop/temp/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -cmake_minimum_required(VERSION 3.24) - -project (temp) - -find_package (atsdk REQUIRED) - -add_executable(main main.c) - -target_link_libraries(main atsdk::atclient) - diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index 5b48160a..4201413c 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -72,8 +72,8 @@ int atclient_connection_connect(atclient_connection *ctx, const char *host, cons * @brief Reads data from the connection * * @param ctx the connection initialized and connected using atclient_connection_init and atclient_connection_connect - * @param value a double pointer that will be allocated by the function to the data read - * @param value_len the length of the data read, will be set by the function + * @param value a double pointer that will be allocated by the function to the data read, assumed to be non-null and a null pointer + * @param value_len the length of the data read, will be set by the function, setting this to NULL will skip setting the length * @param value_max_len the maximum length of the data to read, setting this to 0 means no limit * @return int 0 on success */ diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index fccc7155..22fd15eb 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -248,6 +248,28 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val goto exit; } + /* + * 2. Call pre_write hook, if it exists + */ + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && ctx->hooks != NULL && !ctx->hooks->_is_nested_call; + 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_len = value_len; + params.recv = NULL; + params.recv_size = 0; + params.recv_len = NULL; + ret = ctx->hooks->pre_write(¶ms); + if (ctx->hooks != NULL) { + ctx->hooks->_is_nested_call = false; + } + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pre_write hook failed with exit code: %d\n", ret); + goto exit; + } + } + /* * 2. Write the value */ @@ -276,8 +298,7 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val /* * 4. Call hooks, if they exist */ - bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && ctx->hooks != NULL && !ctx->hooks->_is_nested_call; - if (try_hooks && ctx->hooks->post_write != NULL) { + if (try_hooks && atclient_connection_hooks_is_post_write_initialized(ctx) && ctx->hooks->post_write != NULL) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; params.src = (unsigned char *)value; @@ -595,6 +616,154 @@ bool atclient_connection_is_connected(atclient_connection *ctx) { int atclient_connection_read(atclient_connection *ctx, unsigned char **value, size_t *value_len, const size_t value_max_len) { int ret = 1; + + /* + * 1. Validate arguments + */ + if (ctx == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "ctx is NULL\n"); + return ret; + } + + if (value == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); + return ret; + } + + if(!atclient_connection_is_connection_enabled(ctx)) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Connection is not enabled\n"); + return ret; + } + + /* + * 2. Variables + */ + size_t recv_size; + if(value_max_len == 0) { + // we read 4 KB at a time + recv_size = 2; + } else { + recv_size = value_max_len; + } + unsigned char *recv = malloc(sizeof(unsigned char) * recv_size); + + /* + * 3. Call pre_read hook, if it exists + */ + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && ctx->hooks != NULL && !ctx->hooks->_is_nested_call; + if (try_hooks && atclient_connection_hooks_is_pre_read_initialized(ctx) && ctx->hooks->pre_read != NULL) { + ctx->hooks->_is_nested_call = true; + atclient_connection_hook_params params; + params.src = NULL; + params.src_len = 0; + params.recv = NULL; + params.recv_size = 0; + params.recv_len = NULL; + ret = ctx->hooks->pre_read(¶ms); + if (ctx->hooks != NULL) { + ctx->hooks->_is_nested_call = false; + } + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "pre_read hook failed with exit code: %d\n", ret); + goto exit; + } + } + + /* + * 4. Read the value + */ + bool found_end = false; + size_t pos = 0; + size_t recv_len = 0; + do { + 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; + } + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "pos: %d, ret: %d\n", pos, ret); + pos += ret; + + // check if we found the end of the message + int i = pos; + while(!found_end && i-- > 0) { + found_end = recv[i] == '\n' || recv[i] == '\r'; + } + + 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); + recv_len = value_max_len; + break; + } else { + recv = realloc(recv, sizeof(unsigned char) * (pos + recv_size)); + recv_size += recv_size; + } + } + + } 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) { + unsigned char *recvcopy = 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); + free(recvcopy); + } else { + 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) { + 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'; + } + + /* + * 7. Call post_read hook, if it exists + */ + if (try_hooks && atclient_connection_hooks_is_post_read_initialized(ctx) && ctx->hooks->post_read != NULL) { + ctx->hooks->_is_nested_call = true; + atclient_connection_hook_params params; + params.src = NULL; + params.src_len = 0; + params.recv = recv; + params.recv_size = recv_size; + params.recv_len = &recv_len; + ret = ctx->hooks->post_read(¶ms); + if (ctx->hooks != NULL) { + ctx->hooks->_is_nested_call = false; + } + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "post_read hook failed with exit code: %d\n", ret); + goto exit; + } + } + + ret = 0; + goto exit; +exit: { return ret; } } static void my_debug(void *ctx, int level, const char *file, int line, const char *str) { From 3b34ce31f55f7205d5397d61e50c34ad0248cd6d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 17:30:20 -0400 Subject: [PATCH 174/193] fix: sample_cmake_project CI --- .github/workflows/build_source_tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_source_tests.yaml b/.github/workflows/build_source_tests.yaml index 0d6ed657..aad98d3b 100644 --- a/.github/workflows/build_source_tests.yaml +++ b/.github/workflows/build_source_tests.yaml @@ -29,5 +29,5 @@ jobs: - name: Run sample_cmake_project executable working-directory: examples/desktop/sample_cmake_project run: | - ./build/exec + ./build/main From d65c1ca2ec541aa7c4267b7b6c4e337222f84571 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 20:07:58 -0400 Subject: [PATCH 175/193] fix: --- examples/desktop/connection_hooks/main.c | 23 ++++++++ examples/desktop/connection_hooks/run.sh | 2 +- packages/atclient/src/connection.c | 73 ++++++++++++------------ 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/examples/desktop/connection_hooks/main.c b/examples/desktop/connection_hooks/main.c index d4f7f01d..d46aa1e3 100644 --- a/examples/desktop/connection_hooks/main.c +++ b/examples/desktop/connection_hooks/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #define TAG "main" @@ -132,21 +133,42 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); goto exit; } + free(recv); + recv = NULL; if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 20)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); goto exit; } + free(recv); + recv = NULL; if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 20)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); goto exit; } + free(recv); + recv = NULL; if ((ret = atclient_connection_read(&conn, &recv, &recv_len, 0)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read from %s:%d\n", HOST, PORT); goto exit; } + free(recv); + recv = NULL; + + size_t recv_size = 4096; + recv = malloc(sizeof(unsigned char) * recv_size); + if(recv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to malloc\n"); + goto exit; + } + + if((ret = atclient_connection_send(&conn, src, src_len, recv, recv_size, &recv_len)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); + goto exit; + } if ((ret = atclient_connection_disconnect(&conn)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to disconnect from %s:%d\n", HOST, PORT); @@ -157,6 +179,7 @@ int main(int argc, char *argv[]) { ret = 0; goto exit; exit: { + free(recv); atclient_connection_free(&conn); return ret; } diff --git a/examples/desktop/connection_hooks/run.sh b/examples/desktop/connection_hooks/run.sh index c29ca904..02faf11b 100755 --- a/examples/desktop/connection_hooks/run.sh +++ b/examples/desktop/connection_hooks/run.sh @@ -16,7 +16,7 @@ SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")" cd "$SCRIPT_DIRECTORY" ## CMake configure -cmake -S . -B build +cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug ## CMake build cmake --build build --target install diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 22fd15eb..2ee43a66 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -321,8 +321,8 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val exit: { return ret; } } -int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_r, const size_t srclen_r, - unsigned char *recv, const size_t recvsize_r, size_t *recvlen) { +int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_r, const size_t src_len_r, + unsigned char *recv, const size_t recv_size_r, size_t *recv_len) { int ret = 1; /* @@ -338,7 +338,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ return ret; } - if (srclen_r == 0) { + if (src_len_r == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "srclen is 0\n"); return ret; } @@ -352,8 +352,8 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ * 2. Prep hook stuff */ // Clone readonly inputs so it is editable by the hooks - size_t srclen = srclen_r; - size_t recvsize = recvsize_r; + size_t srclen = src_len_r; + size_t recvsize = recv_size_r; bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; bool allocate_src = try_hooks && ctx->hooks->readonly_src == false; @@ -381,7 +381,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ params.src_len = srclen; params.recv = recv; params.recv_size = recvsize; - params.recv_len = recvlen; + params.recv_len = recv_len; ret = ctx->hooks->pre_write(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; @@ -401,7 +401,24 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } /* - * 5. Call post_send hook, if it exists + * 5. Print debug log + */ + if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG && ret == srclen) { + unsigned char *srccopy = NULL; + if ((srccopy = malloc(sizeof(unsigned char) * srclen)) != NULL) { + memcpy(srccopy, src, srclen); + atlogger_fix_stdout_buffer((char *)srccopy, srclen); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, strlen((char *)srccopy), + srccopy, reset); + free(srccopy); + } else { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "Failed to allocate memory to pretty print the network sent transmission\n"); + } + } + + /* + * 6. Call post_send hook, if it exists */ if (try_hooks && atclient_connection_hooks_is_post_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; @@ -410,6 +427,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ params.src_len = srclen; params.recv = recv; params.recv_size = recvsize; + params.recv_len = recv_len; ret = ctx->hooks->post_write(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; @@ -420,23 +438,6 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ } } - /* - * 6. Print debug log - */ - if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG && ret == srclen) { - unsigned char *srccopy = NULL; - if ((srccopy = malloc(sizeof(unsigned char) * srclen)) != NULL) { - memcpy(srccopy, src, srclen); - atlogger_fix_stdout_buffer((char *)srccopy, srclen); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, strlen((char *)srccopy), - srccopy, reset); - free(srccopy); - } else { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "Failed to allocate memory to pretty print the network sent transmission\n"); - } - } - /* * 7. Exit if recv is NULL */ @@ -456,7 +457,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ params.src_len = srclen; params.recv = recv; params.recv_size = recvsize; - params.recv_len = recvlen; + params.recv_len = recv_len; ret = ctx->hooks->pre_read(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; @@ -481,7 +482,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ l = l + ret; for (int i = l; i >= l - ret && i >= 0; i--) { if (*(recv + i) == '\n' || *(recv + i) == '\r') { - *recvlen = i; + *recv_len = i; found = true; break; } @@ -490,7 +491,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ break; } } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == 0 || !found); - recv[*recvlen] = '\0'; // null terminate the string + recv[*recv_len] = '\0'; // null terminate the string /* * 10. Run post read hook, if it exists @@ -502,7 +503,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ params.src_len = srclen; params.recv = recv; params.recv_size = recvsize; - params.recv_len = recvlen; + params.recv_len = recv_len; ret = ctx->hooks->post_read(¶ms); if (ctx->hooks != NULL) { ctx->hooks->_is_nested_call = false; @@ -518,10 +519,10 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ */ if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG) { unsigned char *recvcopy = NULL; - if ((recvcopy = malloc(sizeof(unsigned char) * (*recvlen))) != NULL) { - memcpy(recvcopy, recv, *recvlen); - atlogger_fix_stdout_buffer((char *)recvcopy, *recvlen); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sRECV: %s\"%.*s\"%s\n", BMAG, HMAG, *recvlen, recvcopy, + 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); free(recvcopy); } else { @@ -597,16 +598,16 @@ bool atclient_connection_is_connected(atclient_connection *ctx) { const size_t recvsize = 64; unsigned char recv[recvsize]; - size_t recvlen; + size_t recv_len; - int ret = atclient_connection_send(ctx, (unsigned char *)command, commandlen, recv, recvsize, &recvlen); + int ret = atclient_connection_send(ctx, (unsigned char *)command, commandlen, recv, recvsize, &recv_len); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to send \"%s\" to connection: %d\n", command, ret); return false; } - if (recvlen <= 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recvlen is <= 0, connection did not respond to \"%s\"\n", command); + if (recv_len <= 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv_len is <= 0, connection did not respond to \"%s\"\n", command); return false; } From e6d2ab7ebc7af8f80e1f42c6b9ad2301151431bd Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 20:46:55 -0400 Subject: [PATCH 176/193] fix: --- .github/workflows/codeql.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 2b052e94..1db7809e 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -78,7 +78,7 @@ jobs: - name: Run sample_cmake_project executable working-directory: examples/desktop/sample_cmake_project run: | - ./build/exec + ./build/main - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@b611370bb5703a7efb587f9d136a52ea24c5c38c # v3.25.11 From 3d5643bdd6a0bb303092111a157b2a615680c7cd Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 20:57:25 -0400 Subject: [PATCH 177/193] feat: remove readonly_src --- .../include/atclient/connection_hooks.h | 1 - packages/atclient/src/connection.c | 71 ++++++------------- packages/atclient/src/connection_hooks.c | 1 - 3 files changed, 23 insertions(+), 50 deletions(-) diff --git a/packages/atclient/include/atclient/connection_hooks.h b/packages/atclient/include/atclient/connection_hooks.h index 04a48ba4..6f50953e 100644 --- a/packages/atclient/include/atclient/connection_hooks.h +++ b/packages/atclient/include/atclient/connection_hooks.h @@ -39,7 +39,6 @@ typedef enum atclient_connection_hook_type { typedef struct atclient_connection_hooks { bool _is_nested_call; - bool readonly_src; atclient_connection_hook *pre_read; atclient_connection_hook *post_read; atclient_connection_hook *pre_write; diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index 2ee43a66..d14a3b99 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -255,7 +255,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 = value; params.src_len = value_len; params.recv = NULL; params.recv_size = 0; @@ -321,8 +321,8 @@ int atclient_connection_write(atclient_connection *ctx, const unsigned char *val exit: { return ret; } } -int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_r, const size_t src_len_r, - unsigned char *recv, const size_t recv_size_r, size_t *recv_len) { +int atclient_connection_send(atclient_connection *ctx, const unsigned char *src, const size_t src_len, + unsigned char *recv, const size_t recv_size, size_t *recv_len) { int ret = 1; /* @@ -333,12 +333,12 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ return ret; } - if (src_r == NULL) { + if (src == NULL) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "src is NULL\n"); return ret; } - if (src_len_r == 0) { + if (src_len == 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "srclen is 0\n"); return ret; } @@ -348,39 +348,17 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ return ret; } - /* - * 2. Prep hook stuff - */ - // Clone readonly inputs so it is editable by the hooks - size_t srclen = src_len_r; - size_t recvsize = recv_size_r; - - bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; - bool allocate_src = try_hooks && ctx->hooks->readonly_src == false; - - unsigned char *src = NULL; - - if (allocate_src) { - if ((src = malloc(sizeof(unsigned char) * srclen)) == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for src\n"); - allocate_src = false; // don't try to free since the memory failed to be allocated - goto exit; - } - memcpy(src, src_r, srclen); - } else { - src = (unsigned char *)src_r; - } - /* * 3. Call pre_send hook, if it exists */ + bool try_hooks = atclient_connection_hooks_is_enabled(ctx) && !ctx->hooks->_is_nested_call; if (try_hooks && atclient_connection_hooks_is_pre_write_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; params.src = src; - params.src_len = srclen; + params.src_len = src_len; params.recv = recv; - params.recv_size = recvsize; + params.recv_size = recv_size; params.recv_len = recv_len; ret = ctx->hooks->pre_write(¶ms); if (ctx->hooks != NULL) { @@ -395,7 +373,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ /* * 4. Write the value */ - if ((ret = mbedtls_ssl_write(&(ctx->ssl), src, srclen)) <= 0) { + if ((ret = mbedtls_ssl_write(&(ctx->ssl), src, src_len)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_write failed with exit code: %d\n", ret); goto exit; } @@ -403,11 +381,11 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ /* * 5. Print debug log */ - if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG && ret == srclen) { + if (atlogger_get_logging_level() >= ATLOGGER_LOGGING_LEVEL_DEBUG && ret == src_len) { unsigned char *srccopy = NULL; - if ((srccopy = malloc(sizeof(unsigned char) * srclen)) != NULL) { - memcpy(srccopy, src, srclen); - atlogger_fix_stdout_buffer((char *)srccopy, srclen); + if ((srccopy = malloc(sizeof(unsigned char) * src_len)) != NULL) { + memcpy(srccopy, src, src_len); + atlogger_fix_stdout_buffer((char *)srccopy, src_len); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "\t%sSENT: %s\"%.*s\"%s\n", BBLU, HCYN, strlen((char *)srccopy), srccopy, reset); free(srccopy); @@ -424,9 +402,9 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; params.src = src; - params.src_len = srclen; + params.src_len = src_len; params.recv = recv; - params.recv_size = recvsize; + params.recv_size = recv_size; params.recv_len = recv_len; ret = ctx->hooks->post_write(¶ms); if (ctx->hooks != NULL) { @@ -449,14 +427,14 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ /* * 8. Run pre read hook, if it exists */ - memset(recv, 0, sizeof(unsigned char) * recvsize); + memset(recv, 0, sizeof(unsigned char) * recv_size); if (try_hooks && atclient_connection_hooks_is_pre_read_initialized(ctx)) { ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; params.src = src; - params.src_len = srclen; + params.src_len = src_len; params.recv = recv; - params.recv_size = recvsize; + params.recv_size = recv_size; params.recv_len = recv_len; ret = ctx->hooks->pre_read(¶ms); if (ctx->hooks != NULL) { @@ -475,7 +453,7 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ bool found = false; size_t l = 0; do { - if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv + l, recvsize - l)) <= 0) { + if ((ret = mbedtls_ssl_read(&(ctx->ssl), recv + l, recv_size - l)) <= 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "mbedtls_ssl_read failed with exit code: %d\n", ret); goto exit; } @@ -500,9 +478,9 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ ctx->hooks->_is_nested_call = true; atclient_connection_hook_params params; params.src = src; - params.src_len = srclen; + params.src_len = src_len; params.recv = recv; - params.recv_size = recvsize; + params.recv_size = recv_size; params.recv_len = recv_len; ret = ctx->hooks->post_read(¶ms); if (ctx->hooks != NULL) { @@ -534,9 +512,6 @@ int atclient_connection_send(atclient_connection *ctx, const unsigned char *src_ ret = 0; goto exit; exit: { - if (allocate_src) { - free(src); - } return ret; } } @@ -641,8 +616,8 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si */ size_t recv_size; if(value_max_len == 0) { - // we read 4 KB at a time - recv_size = 2; + // we read 4 KB at a time, TODO: make a constant + recv_size = 4096; } else { recv_size = value_max_len; } diff --git a/packages/atclient/src/connection_hooks.c b/packages/atclient/src/connection_hooks.c index 64e7b2e0..9b7f7b89 100644 --- a/packages/atclient/src/connection_hooks.c +++ b/packages/atclient/src/connection_hooks.c @@ -65,7 +65,6 @@ int atclient_connection_hooks_enable(atclient_connection *conn) { /* * 4. Set any defaults */ - conn->hooks->readonly_src = true; ret = 0; goto exit; From 021db4a93adf04eeab0d981e8847d5e5fffe5ce1 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:14:58 -0400 Subject: [PATCH 178/193] fix: --- examples/desktop/connection_hooks/main.c | 4 ++-- packages/atclient/include/atclient/connection.h | 3 ++- packages/atclient/include/atclient/connection_hooks.h | 4 +--- packages/atclient/src/connection_hooks.c | 2 +- packages/atclient/src/monitor.c | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/desktop/connection_hooks/main.c b/examples/desktop/connection_hooks/main.c index d46aa1e3..23466466 100644 --- a/examples/desktop/connection_hooks/main.c +++ b/examples/desktop/connection_hooks/main.c @@ -78,8 +78,8 @@ int main(int argc, char *argv[]) { atclient_connection conn; atclient_connection_init(&conn, ATCLIENT_CONNECTION_TYPE_ATSERVER); - const unsigned char *src = "from:12alpaca\r\n"; - const size_t src_len = strlen(src); + const unsigned char *src = (const unsigned char *) "from:12alpaca\r\n"; + const size_t src_len = strlen((char *) src); unsigned char *recv = NULL; size_t recv_len = 0; diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index 4201413c..a29ceb23 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -7,7 +7,8 @@ #include #include #include -#include "atclient/connection_hooks.h" + +typedef struct atclient_connection_hooks atclient_connection_hooks; // represents the type of connection typedef enum atclient_connection_type { diff --git a/packages/atclient/include/atclient/connection_hooks.h b/packages/atclient/include/atclient/connection_hooks.h index 6f50953e..301c4b9e 100644 --- a/packages/atclient/include/atclient/connection_hooks.h +++ b/packages/atclient/include/atclient/connection_hooks.h @@ -1,7 +1,7 @@ #ifndef ATCLIENT_CONNECTION_HOOKS_H #define ATCLIENT_CONNECTION_HOOKS_H -#include +#include "atclient/connection.h" #include #include @@ -17,8 +17,6 @@ #define ATCLIENT_CONNECTION_HOOKS_PRE_WRITE_INITIALIZED (VALUE_INITIALIZED << 2) #define ATCLIENT_CONNECTION_HOOKS_POST_WRITE_INITIALIZED (VALUE_INITIALIZED << 3) -typedef struct atclient_connection atclient_connection; - typedef struct atclient_connection_hook_params { unsigned char *src; size_t src_len; diff --git a/packages/atclient/src/connection_hooks.c b/packages/atclient/src/connection_hooks.c index 9b7f7b89..0537deb2 100644 --- a/packages/atclient/src/connection_hooks.c +++ b/packages/atclient/src/connection_hooks.c @@ -1,8 +1,8 @@ #include "atclient/connection.h" +#include "atclient/connection_hooks.h" #include #include #include -#include "atclient/connection_hooks.h" #include #define TAG "connection_hooks" diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index f157f301..0fac70a1 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -593,7 +593,7 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not } // 5. set decrypted value - atclient_atnotification_set_decrypted_value(notification, decryptedvaluetemp); + atclient_atnotification_set_decrypted_value(notification, (const char *) decryptedvaluetemp); ret = 0; goto exit; From 1c01b9d796e4e59dd7a4a0e6cb263563c4c54740 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:22:42 -0400 Subject: [PATCH 179/193] feat: atchops/mbedtls.h, atclient/mbedtls.h, atclient/cjson.h --- packages/atchops/include/atchops/constants.h | 4 ++-- packages/atchops/include/atchops/mbedtls.h | 13 +++++++++++++ packages/atchops/src/aes.c | 3 +-- packages/atchops/src/aes_ctr.c | 4 +--- packages/atchops/src/base64.c | 2 +- packages/atchops/src/iv.c | 3 +-- packages/atchops/src/rsa.c | 7 +------ packages/atchops/src/rsa_key.c | 2 +- packages/atchops/src/sha.c | 2 +- packages/atclient/include/atclient/cjson.h | 6 ++++++ packages/atclient/include/atclient/connection.h | 5 +---- packages/atclient/include/atclient/mbedtls.h | 10 ++++++++++ packages/atclient/include/atclient/metadata.h | 2 +- packages/atclient/src/atclient.c | 2 +- packages/atclient/src/connection.c | 5 +---- packages/atclient/src/monitor.c | 4 ++-- packages/atclient/src/notify.c | 11 ++++++----- 17 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 packages/atchops/include/atchops/mbedtls.h create mode 100644 packages/atclient/include/atclient/cjson.h create mode 100644 packages/atclient/include/atclient/mbedtls.h diff --git a/packages/atchops/include/atchops/constants.h b/packages/atchops/include/atchops/constants.h index 7fbca54e..a1511d6f 100644 --- a/packages/atchops/include/atchops/constants.h +++ b/packages/atchops/include/atchops/constants.h @@ -1,9 +1,9 @@ #ifndef ATCHOPS_CONSTANTS_H #define ATCHOPS_CONSTANTS_H -#define ATCHOPS_RNG_PERSONALIZATION "@atchops12345" +#include "atchops/mbedtls.h" -#include "mbedtls/md.h" +#define ATCHOPS_RNG_PERSONALIZATION "@atchops12345" typedef enum atchops_md_type { ATCHOPS_MD_NONE = 0, diff --git a/packages/atchops/include/atchops/mbedtls.h b/packages/atchops/include/atchops/mbedtls.h new file mode 100644 index 00000000..186b8efe --- /dev/null +++ b/packages/atchops/include/atchops/mbedtls.h @@ -0,0 +1,13 @@ +#ifndef ATCHOPS_MBEDTLS_H +#define ATCHOPS_MBEDTLS_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/packages/atchops/src/aes.c b/packages/atchops/src/aes.c index 9ec7148a..5371eb0b 100644 --- a/packages/atchops/src/aes.c +++ b/packages/atchops/src/aes.c @@ -1,8 +1,7 @@ #include "atchops/aes.h" #include "atchops/base64.h" #include "atchops/constants.h" -#include -#include +#include "atchops/mbedtls.h" #include #include #include diff --git a/packages/atchops/src/aes_ctr.c b/packages/atchops/src/aes_ctr.c index 3b458e81..d62f64b4 100644 --- a/packages/atchops/src/aes_ctr.c +++ b/packages/atchops/src/aes_ctr.c @@ -1,8 +1,6 @@ #include "atchops/aes_ctr.h" #include -#include -#include -#include +#include "atchops/mbedtls.h" #include #include #include diff --git a/packages/atchops/src/base64.c b/packages/atchops/src/base64.c index 5a7bbb3e..637d24fc 100644 --- a/packages/atchops/src/base64.c +++ b/packages/atchops/src/base64.c @@ -1,6 +1,6 @@ #include "atchops/base64.h" #include -#include +#include "atchops/mbedtls.h" #include #define TAG "base64" diff --git a/packages/atchops/src/iv.c b/packages/atchops/src/iv.c index 25fab70e..0c9800eb 100644 --- a/packages/atchops/src/iv.c +++ b/packages/atchops/src/iv.c @@ -2,8 +2,7 @@ #include "atchops/base64.h" #include "atchops/constants.h" #include -#include -#include +#include "atchops/mbedtls.h" #include #include diff --git a/packages/atchops/src/rsa.c b/packages/atchops/src/rsa.c index 2d2e31a3..723e11a1 100644 --- a/packages/atchops/src/rsa.c +++ b/packages/atchops/src/rsa.c @@ -2,12 +2,7 @@ #include "atchops/rsa_key.h" #include "atchops/sha.h" #include "atlogger/atlogger.h" -#include -#include -#include -#include -#include -#include +#include "atchops/mbedtls.h" #include #include #include diff --git a/packages/atchops/src/rsa_key.c b/packages/atchops/src/rsa_key.c index 2741fc0f..b6bf8bb4 100644 --- a/packages/atchops/src/rsa_key.c +++ b/packages/atchops/src/rsa_key.c @@ -1,7 +1,7 @@ #include "atchops/rsa_key.h" #include "atchops/base64.h" +#include "atchops/mbedtls.h" #include -#include #include #include #include diff --git a/packages/atchops/src/sha.c b/packages/atchops/src/sha.c index ca70c035..75ac6668 100644 --- a/packages/atchops/src/sha.c +++ b/packages/atchops/src/sha.c @@ -1,6 +1,6 @@ #include "atchops/sha.h" +#include "atchops/mbedtls.h" #include -#include #include #define TAG "sha" diff --git a/packages/atclient/include/atclient/cjson.h b/packages/atclient/include/atclient/cjson.h new file mode 100644 index 00000000..b1d310e7 --- /dev/null +++ b/packages/atclient/include/atclient/cjson.h @@ -0,0 +1,6 @@ +#ifndef ATCLIENT_CJSON_H +#define ATCLIENT_CJSON_H + +#include + +#endif diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index a29ceb23..cea68cb5 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -1,10 +1,7 @@ #ifndef ATCLIENT_CONNECTION_H #define ATCLIENT_CONNECTION_H -#include -#include -#include -#include +#include "atchops/mbedtls.h" #include #include diff --git a/packages/atclient/include/atclient/mbedtls.h b/packages/atclient/include/atclient/mbedtls.h new file mode 100644 index 00000000..2053980e --- /dev/null +++ b/packages/atclient/include/atclient/mbedtls.h @@ -0,0 +1,10 @@ +#ifndef ATCLIENT_MBEDTLS_H +#define ATCLIENT_MBEDTLS_H + +#include +#include +#include +#include +#include + +#endif diff --git a/packages/atclient/include/atclient/metadata.h b/packages/atclient/include/atclient/metadata.h index 8bf3edd3..93a48a69 100644 --- a/packages/atclient/include/atclient/metadata.h +++ b/packages/atclient/include/atclient/metadata.h @@ -2,7 +2,7 @@ #ifndef ATCLIENT_METADATA_H #define ATCLIENT_METADATA_H -#include "cJSON.h" +#include "atclient/cjson.h" #include #include #include // IWYU pragma: keep (for uint8_t) diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 945b6c57..37835247 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -9,7 +9,7 @@ #include "atclient/string_utils.h" #include "atlogger/atlogger.h" #include -#include +#include "atclient/mbedtls.h" #include #include #include diff --git a/packages/atclient/src/connection.c b/packages/atclient/src/connection.c index d14a3b99..7a1e16dc 100644 --- a/packages/atclient/src/connection.c +++ b/packages/atclient/src/connection.c @@ -4,10 +4,7 @@ #include "atclient/connection_hooks.h" #include "atclient/constants.h" #include "atlogger/atlogger.h" -#include -#include -#include -#include +#include "atclient/mbedtls.h" #include #include #include diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 0fac70a1..0e5fb194 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -5,14 +5,14 @@ #include "atclient/constants.h" #include "atclient/encryption_key_helpers.h" #include "atclient/string_utils.h" -#include "cJSON.h" +#include "atclient/cjson.h" #include #include #include #include #include #include -#include +#include "atclient/mbedtls.h" #include #include #include diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 3d647e92..946ab4f5 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -2,6 +2,7 @@ #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 @@ -9,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -102,12 +102,13 @@ int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notifi ret = atclient_atkey_metadata_set_iv_nonce(&(params->atkey->metadata), (char *)ivbase64); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce failed with code %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_iv_nonce failed with code %d\n", + ret); return ret; } ret = atchops_aes_ctr_encrypt(sharedenckey, ATCHOPS_AES_256, iv, (unsigned char *)params->value, - strlen(params->value), ciphertext, ciphertextsize, &ciphertextlen); + strlen(params->value), ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt failed with code %d\n", ret); return ret; @@ -244,7 +245,6 @@ static size_t calculate_cmd_size(const atclient_notify_params *params, const siz cmdsize += 1; // null terminator - return cmdsize; } @@ -295,7 +295,8 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval off += strlen(":") + strlen(atclient_notify_operation_str[params->operation]); } - if (atclient_notify_params_is_message_type_initialized(params) && params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { + if (atclient_notify_params_is_message_type_initialized(params) && + params->message_type != ATCLIENT_NOTIFY_MESSAGE_TYPE_NONE) { snprintf(cmd + off, cmdsize - off, ":message_type:%s", atclient_notify_message_type_str[params->message_type]); off += strlen(":message_type:") + strlen(atclient_notify_message_type_str[params->message_type]); } From dabf4ee17c11ce16cdea1182e6598eac626cbc96 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:30:30 -0400 Subject: [PATCH 180/193] fix: --- packages/atchops/CMakeLists.txt | 2 +- packages/atchops/include/atchops/constants.h | 19 ------------------- packages/atchops/include/atchops/mbedtls.h | 5 +++++ packages/atchops/include/atchops/rsa.h | 1 + packages/atchops/include/atchops/sha.h | 15 +++++++++++++++ .../atchops/src/{constants.c => mbedtls.c} | 3 ++- .../atclient/include/atclient/connection.h | 3 +++ packages/atclient/src/atclient.c | 2 +- .../atclient/src/atclient_put_public_key.c | 2 +- packages/atclient/src/atkeys.c | 2 +- 10 files changed, 30 insertions(+), 24 deletions(-) rename packages/atchops/src/{constants.c => mbedtls.c} (91%) diff --git a/packages/atchops/CMakeLists.txt b/packages/atchops/CMakeLists.txt index 9b135eb0..01fed864 100644 --- a/packages/atchops/CMakeLists.txt +++ b/packages/atchops/CMakeLists.txt @@ -8,7 +8,7 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/aes_ctr.c ${CMAKE_CURRENT_LIST_DIR}/src/aes.c ${CMAKE_CURRENT_LIST_DIR}/src/base64.c - ${CMAKE_CURRENT_LIST_DIR}/src/constants.c + ${CMAKE_CURRENT_LIST_DIR}/src/mbedtls.c ${CMAKE_CURRENT_LIST_DIR}/src/iv.c ${CMAKE_CURRENT_LIST_DIR}/src/rsa_key.c ${CMAKE_CURRENT_LIST_DIR}/src/rsa.c diff --git a/packages/atchops/include/atchops/constants.h b/packages/atchops/include/atchops/constants.h index a1511d6f..6f6be074 100644 --- a/packages/atchops/include/atchops/constants.h +++ b/packages/atchops/include/atchops/constants.h @@ -1,25 +1,6 @@ #ifndef ATCHOPS_CONSTANTS_H #define ATCHOPS_CONSTANTS_H -#include "atchops/mbedtls.h" - #define ATCHOPS_RNG_PERSONALIZATION "@atchops12345" -typedef enum atchops_md_type { - ATCHOPS_MD_NONE = 0, - ATCHOPS_MD_MD5, - ATCHOPS_MD_RIPEMD160, - ATCHOPS_MD_SHA1, - ATCHOPS_MD_SHA224, - ATCHOPS_MD_SHA256, - ATCHOPS_MD_SHA384, - ATCHOPS_MD_SHA512, - ATCHOPS_MD_SHA3_224, - ATCHOPS_MD_SHA3_256, - ATCHOPS_MD_SHA3_384, - ATCHOPS_MD_SHA3_512, -} atchops_md_type; - -extern const mbedtls_md_type_t atchops_mbedtls_md_map[]; - #endif diff --git a/packages/atchops/include/atchops/mbedtls.h b/packages/atchops/include/atchops/mbedtls.h index 186b8efe..e21e7b10 100644 --- a/packages/atchops/include/atchops/mbedtls.h +++ b/packages/atchops/include/atchops/mbedtls.h @@ -9,5 +9,10 @@ #include #include #include +#include +#include +#include + +extern const mbedtls_md_type_t atchops_mbedtls_md_map[]; #endif diff --git a/packages/atchops/include/atchops/rsa.h b/packages/atchops/include/atchops/rsa.h index e8e72296..668b3ddc 100644 --- a/packages/atchops/include/atchops/rsa.h +++ b/packages/atchops/include/atchops/rsa.h @@ -3,6 +3,7 @@ #include "atchops/constants.h" #include "atchops/rsa_key.h" +#include "atchops/sha.h" #include /** diff --git a/packages/atchops/include/atchops/sha.h b/packages/atchops/include/atchops/sha.h index 8a0cc0bd..e2bb3be7 100644 --- a/packages/atchops/include/atchops/sha.h +++ b/packages/atchops/include/atchops/sha.h @@ -4,6 +4,21 @@ #include #include +typedef enum atchops_md_type { + ATCHOPS_MD_NONE = 0, + ATCHOPS_MD_MD5, + ATCHOPS_MD_RIPEMD160, + ATCHOPS_MD_SHA1, + ATCHOPS_MD_SHA224, + ATCHOPS_MD_SHA256, + ATCHOPS_MD_SHA384, + ATCHOPS_MD_SHA512, + ATCHOPS_MD_SHA3_224, + ATCHOPS_MD_SHA3_256, + ATCHOPS_MD_SHA3_384, + ATCHOPS_MD_SHA3_512, +} atchops_md_type; + /** * @brief SHA Hash an input buffer * diff --git a/packages/atchops/src/constants.c b/packages/atchops/src/mbedtls.c similarity index 91% rename from packages/atchops/src/constants.c rename to packages/atchops/src/mbedtls.c index 0b46b58c..72bae052 100644 --- a/packages/atchops/src/constants.c +++ b/packages/atchops/src/mbedtls.c @@ -1,4 +1,5 @@ -#include "atchops/constants.h" +#include "atchops/mbedtls.h" +#include "atchops/sha.h" const mbedtls_md_type_t atchops_mbedtls_md_map[] = { [ATCHOPS_MD_NONE] = MBEDTLS_MD_NONE, [ATCHOPS_MD_MD5] = MBEDTLS_MD_MD5, diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index cea68cb5..621de794 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -5,7 +5,10 @@ #include #include +#ifndef ATCLIENT_CONNECTION_HOOKS_H typedef struct atclient_connection_hooks atclient_connection_hooks; +#endif + // represents the type of connection typedef enum atclient_connection_type { diff --git a/packages/atclient/src/atclient.c b/packages/atclient/src/atclient.c index 37835247..42333a2a 100644 --- a/packages/atclient/src/atclient.c +++ b/packages/atclient/src/atclient.c @@ -5,6 +5,7 @@ #include "atclient/atclient_utils.h" #include "atclient/atkeys.h" #include "atclient/connection.h" +#include "atclient/connection_hooks.h" #include "atclient/constants.h" #include "atclient/string_utils.h" #include "atlogger/atlogger.h" @@ -82,7 +83,6 @@ int atclient_start_atserver_connection(atclient *ctx, const char *secondary_host // remove hooks to preserve them across resets atclient_connection_hooks *conn_hooks = ctx->atserver_connection.hooks; - ctx->atserver_connection.hooks = NULL; // clear the atserver connection atclient_stop_atserver_connection(ctx); diff --git a/packages/atclient/src/atclient_put_public_key.c b/packages/atclient/src/atclient_put_public_key.c index 8d849c58..d75ec307 100644 --- a/packages/atclient/src/atclient_put_public_key.c +++ b/packages/atclient/src/atclient_put_public_key.c @@ -144,7 +144,7 @@ static int atclient_put_public_key_validate_arguments(const atclient *ctx, const goto exit; } - const atkey_type = atclient_atkey_get_type(atkey); + const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); if (atkey_type != ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { ret = 1; diff --git a/packages/atclient/src/atkeys.c b/packages/atclient/src/atkeys.c index 0f3cee02..3ad7aae8 100644 --- a/packages/atclient/src/atkeys.c +++ b/packages/atclient/src/atkeys.c @@ -267,7 +267,7 @@ 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)) != + 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); From 7307b33bd0caba954e328a5f5de7b2844fc774e2 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:37:45 -0400 Subject: [PATCH 181/193] refactor: atclient_monitor_response_init/free, remove hooks from atclient_monitor_read, and made atclient_notify_params const in atclient_notify function signature --- examples/desktop/at_talk/src/main.c | 4 +-- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- packages/atclient/include/atclient/monitor.h | 13 +++++---- packages/atclient/include/atclient/notify.h | 2 +- packages/atclient/src/monitor.c | 27 +++---------------- packages/atclient/src/notify.c | 2 +- .../tests/test_atclient_monitor.c | 6 ++--- 8 files changed, 19 insertions(+), 39 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index aa7a51ad..fae34a34 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -260,7 +260,7 @@ static void *monitor_handler(void *xargs) { while (true) { atclient_monitor_response message; - atclient_monitor_message_init(&message); + atclient_monitor_response_init(&message); pthread_mutex_lock(&monitor_mutex); pthread_mutex_lock(&client_mutex); @@ -334,7 +334,7 @@ static void *monitor_handler(void *xargs) { } } - atclient_monitor_message_free(&message); + atclient_monitor_response_free(&message); usleep(100); } goto exit; diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index 728e42b1..ba950528 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -126,7 +126,7 @@ exit: { atclient_atkeys_free(&atkeys); free(atsign); atclient_monitor_free(&monitor_conn); - atclient_monitor_message_free(&message); + atclient_monitor_response_free(&message); return ret; } } diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index 31887631..e5ac5f32 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -159,7 +159,7 @@ exit: { atclient_atkeys_free(&atkeys); free(atserver_host); atclient_monitor_free(&monitor_conn); - atclient_monitor_message_free(message); + atclient_monitor_response_free(message); atclient_free(&atclient2); return ret; } diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 5344e065..7c1ac1b3 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -60,19 +60,19 @@ typedef struct atclient_monitor_hooks { * * Example: * atclient_monitor_response message; - * atclient_monitor_message_init(&message); + * atclient_monitor_response_init(&message); * * @param message the message to initialize, it is assumed that the memory for this struct has already been allocated */ -void atclient_monitor_message_init(atclient_monitor_response *message); +void atclient_monitor_response_init(atclient_monitor_response *message); /** * @brief Initializes the monitor message to a default state, ready for use in other functions. * * @param message the message to free, it is assumed that the memory for this struct has already been allocated and was - * previous called with atclient_monitor_message_init + * previous called with atclient_monitor_response_init */ -void atclient_monitor_message_free(atclient_monitor_response *message); +void atclient_monitor_response_free(atclient_monitor_response *message); /** * @brief Initializes the monitor connection. It is recommended that this be called before any other monitor functions. @@ -133,7 +133,7 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size * separate connection from the monitor_conn to avoid colliding messages when reading. it is assumed that this is * initialized and pkam authenticated. * @param message A pointer to the initialized atclient_monitor_response. It is up to - * the caller to allocate memory to this struct, call atclient_monitor_message_init before passing to this function, + * the caller to allocate memory to this struct, call atclient_monitor_response_init before passing to this function, * then call atclient_monitor_free use. This function populates the message struct with the notification, data response, * or error response read from the monitor connection. * @return 0 on success, non-zero on error @@ -141,8 +141,7 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size * @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); /** * @brief Check if the monitor connection is still established (client is listening for notifications, and the server diff --git a/packages/atclient/include/atclient/notify.h b/packages/atclient/include/atclient/notify.h index 4f406d3b..e2e002aa 100644 --- a/packages/atclient/include/atclient/notify.h +++ b/packages/atclient/include/atclient/notify.h @@ -4,6 +4,6 @@ #include "atclient/atclient.h" #include "atclient/notify_params.h" -int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notification_id); +int atclient_notify(atclient *ctx, const atclient_notify_params *params, char **notification_id); #endif // ATCLIENT_NOTIFY_H diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 0e5fb194..b710e7de 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -24,11 +24,11 @@ static int parse_message(char *original, char **message_type, char **message_bod static int parse_notification(atclient_atnotification *notification, const char *messagebody); static int decrypt_notification(atclient *monitor_conn, atclient_atnotification *notification); -void atclient_monitor_message_init(atclient_monitor_response *message) { +void atclient_monitor_response_init(atclient_monitor_response *message) { memset(message, 0, sizeof(atclient_monitor_response)); } -void atclient_monitor_message_free(atclient_monitor_response *message) { +void atclient_monitor_response_free(atclient_monitor_response *message) { if (message->type == ATCLIENT_MONITOR_MESSAGE_TYPE_NOTIFICATION) { atclient_atnotification_free(&(message->notification)); } else if (message->type == ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE) { @@ -104,8 +104,7 @@ exit: { } } -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) { int ret = -1; char *buffertemp = NULL; @@ -181,26 +180,8 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m ret = 0; goto exit; } - if (hooks != NULL && hooks->pre_decrypt_notification != NULL) { - ret = hooks->pre_decrypt_notification(); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to call pre decrypt notification hook\n"); - goto exit; - } - } - - int decrypt_ret = decrypt_notification(atclient, &(message->notification)); - - if (hooks != NULL && hooks->post_decrypt_notification != NULL) { - ret = hooks->post_decrypt_notification(decrypt_ret); - if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to call post decrypt notification hook\n"); - goto exit; - } - } - ret = decrypt_ret; - if (ret != 0) { + if ((ret = decrypt_notification(atclient, &(message->notification))) != 0) { message->type = ATCLIENT_MONITOR_ERROR_DECRYPT_NOTIFICATION; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to decrypt notification\n"); goto exit; diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index 946ab4f5..eb182ae8 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -23,7 +23,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval static size_t calculate_cmd_size(const atclient_notify_params *params, const size_t cmdvaluelen, size_t *atkeyolen, size_t *medatastrolen); -int atclient_notify(atclient *ctx, atclient_notify_params *params, char **notification_id) { +int atclient_notify(atclient *ctx, const atclient_notify_params *params, char **notification_id) { int ret = 1; if (ctx->async_read) { diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index f71bca69..43963ebc 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -213,13 +213,13 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) int ret = 1; atclient_monitor_response message; - atclient_monitor_message_init(&message); + atclient_monitor_response_init(&message); const int max_tries = 10; int tries = 1; while (tries <= max_tries) { - if ((ret = atclient_monitor_read(monitor_conn, atclient2, &message, NULL)) != 0) { + if ((ret = atclient_monitor_read(monitor_conn, atclient2, &message)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read monitor message: %d\n", ret); tries++; continue; @@ -253,7 +253,7 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) ret = 1; goto exit; exit: { - atclient_monitor_message_free(&message); + atclient_monitor_response_free(&message); return ret; } } From 56503c7835cfe9c4f8ed29888a33eb4aff7ab827 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:39:55 -0400 Subject: [PATCH 182/193] fix: examples --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index fae34a34..55cd956a 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -264,7 +264,7 @@ static void *monitor_handler(void *xargs) { pthread_mutex_lock(&monitor_mutex); pthread_mutex_lock(&client_mutex); - ret = atclient_monitor_read(monitor, ctx, &message, NULL); + ret = atclient_monitor_read(monitor, ctx, &message); pthread_mutex_unlock(&monitor_mutex); pthread_mutex_unlock(&client_mutex); diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index ba950528..fb568973 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Starting main monitor loop...\n"); while (true) { - ret = atclient_monitor_read(&monitor_conn, &atclient2, &message, NULL); + ret = atclient_monitor_read(&monitor_conn, &atclient2, &message); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read monitor message: %d\n", ret); continue; diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index e5ac5f32..ba2add8a 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) { size_t max_tries = 10; while (true) { - ret = atclient_monitor_read(&monitor_conn, &atclient2, &message, NULL); + ret = atclient_monitor_read(&monitor_conn, &atclient2, &message); switch (message->type) { case ATCLIENT_MONITOR_MESSAGE_TYPE_NONE: { From 302870a159e52d2681ac3f4fff821d3412c7f313 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Mon, 29 Jul 2024 21:45:14 -0400 Subject: [PATCH 183/193] chore: --- packages/atclient/src/atclient_get_self_key.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/atclient/src/atclient_get_self_key.c b/packages/atclient/src/atclient_get_self_key.c index 9b4e38bb..0eef62e9 100644 --- a/packages/atclient/src/atclient_get_self_key.c +++ b/packages/atclient/src/atclient_get_self_key.c @@ -14,7 +14,7 @@ #define TAG "atclient_get_selfkey" -static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_self_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char **value, const atclient_get_self_key_request_options *request_options); int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char **value, const atclient_get_self_key_request_options *request_options) { @@ -23,8 +23,8 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char **valu /* * 1. Validate arguments */ - if ((ret = atclient_get_self_key_valid_arguments(atclient, atkey, (const char **) value, request_options)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_valid_arguments: %d\n", ret); + if ((ret = atclient_get_self_key_validate_arguments(atclient, atkey, (const char **) value, request_options)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_get_self_key_validate_arguments: %d\n", ret); return ret; } @@ -210,7 +210,7 @@ exit: { } } -static int atclient_get_self_key_valid_arguments(const atclient *atclient, const atclient_atkey *atkey, +static int atclient_get_self_key_validate_arguments(const atclient *atclient, const atclient_atkey *atkey, const char **value, const atclient_get_self_key_request_options *request_options) { int ret = 1; From 91d4a6e47de4a867baff05f3aa617f386d43ced9 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:34:48 -0400 Subject: [PATCH 184/193] docs: base64.h --- packages/atchops/include/atchops/base64.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/atchops/include/atchops/base64.h b/packages/atchops/include/atchops/base64.h index 0fc90975..e837b745 100644 --- a/packages/atchops/include/atchops/base64.h +++ b/packages/atchops/include/atchops/base64.h @@ -10,7 +10,7 @@ * @param srcsize the length of the src bytes * @param dst the buffer where the base64 encoded result will be * @param dstsize the buffer size, you can use atchops_base64_encoded_size to calculate the optimal size - * @param dstlen the length of the result after operation + * @param dstlen the length of the result after operation, NULLable if you don't want the length * @return int 0 on success */ int atchops_base64_encode(const unsigned char *src, const size_t srcsize, unsigned char *dst, @@ -23,7 +23,7 @@ int atchops_base64_encode(const unsigned char *src, const size_t srcsize, unsign * @param srcsize the length of the src bytes * @param dst the buffer where the base64 decoded result will be * @param dstsize the buffer size, you may use atchops_base64_decoded_size to calculate the buffer size - * @param dstlen the length of the result after operation + * @param dstlen the length of the result after operation, NULLable if you don't want the length * @return int 0 on success */ int atchops_base64_decode(const unsigned char *src, const size_t srcsize, unsigned char *dst, From 0e7ee5ac461ceb174fb99572ac6ab3436cf3d754 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:34:53 -0400 Subject: [PATCH 185/193] chore: remove generic atclient_put --- packages/atclient/include/atclient/atclient.h | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/packages/atclient/include/atclient/atclient.h b/packages/atclient/include/atclient/atclient.h index e9c78e8e..76c13758 100644 --- a/packages/atclient/include/atclient/atclient.h +++ b/packages/atclient/include/atclient/atclient.h @@ -23,7 +23,6 @@ * TODO: more documentation */ typedef struct atclient { - char *atsign; atclient_connection atserver_connection; atclient_atkeys atkeys; @@ -127,26 +126,6 @@ void atclient_stop_atserver_connection(atclient *ctx); int atclient_pkam_authenticate(atclient *ctx, const char *atserver_host, const int atserver_port, const atclient_atkeys *atkeys, const char *atsign); -/** - * @brief Put a string value into your atServer. - * `atclient` must satisfy two conditions before calling this function: - * 1. initialized with atclient_init() - * 2. authenticated via atclient_pkam_authenticate() - * - * `atkey` must satisfy the following condition before calling this function: - * 1. initialized with atclient_atkey_init() - * 2. have populated values (such as a name, shared_by, shared_with, etc,.) depending on what kind of atkey you want - * to be associated with your value. - * - * @param atclient the atclient context (must satisfy the two conditions stated above) - * @param atkey the populated atkey to put the value into (must satisfy the two conditions stated above) - * @param value the value to put into atServer, assumed to be non-null and null-terminated - * @param commit_id (optional) the output commit_id of the put operation that the atServer returns, can be NULL if you - * don't care about the commit_id - * @return int 0 on success - */ -int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int *commit_id); - /** * @brief Put a string value into a self key into your atServer. Putting a self key is a private value and is encrypted only for you * From 7096c090da507dfe52b721eb0216e4a2ab8d4dc4 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:39:07 -0400 Subject: [PATCH 186/193] feat: more request options --- .../atclient/include/atclient/connection.h | 2 +- .../include/atclient/request_options.h | 33 +- packages/atclient/src/request_options.c | 291 ++++++++++++------ 3 files changed, 219 insertions(+), 107 deletions(-) diff --git a/packages/atclient/include/atclient/connection.h b/packages/atclient/include/atclient/connection.h index 621de794..d4e96542 100644 --- a/packages/atclient/include/atclient/connection.h +++ b/packages/atclient/include/atclient/connection.h @@ -82,7 +82,7 @@ int atclient_connection_read(atclient_connection *ctx, unsigned char **value, si /** * @brief Write data to the connection - * + * * @param ctx connection initialized and connected using atclient_connection_init and atclient_connection_connect * @param value the data to write * @param value_len the length of the data to write diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index ae706d28..8d0a4a53 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -11,9 +11,7 @@ #define ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 -#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 1) #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 @@ -21,9 +19,13 @@ #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) #define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX 1 +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 #define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 #define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) -#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED (VALUE_INITIALIZED << 1) +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 2) +#define ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 3) #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 @@ -53,7 +55,6 @@ typedef struct atclient_put_self_key_request_options { typedef struct atclient_put_shared_key_request_options { unsigned char *shared_encryption_key; unsigned char *iv; - bool bypass_cache; uint8_t _initialized_fields[1]; } atclient_put_shared_key_request_options; @@ -79,6 +80,8 @@ typedef struct atclient_get_self_key_request_options { */ typedef struct atclient_get_shared_key_request_options { unsigned char *shared_encryption_key; + unsigned char *iv; + bool bypass_cache; bool store_atkey_metadata; uint8_t _initialized_fields[1]; } atclient_get_shared_key_request_options; @@ -132,14 +135,6 @@ int atclient_put_shared_key_request_options_set_shared_encryption_key(atclient_p void atclient_put_shared_key_request_options_unset_shared_encryption_key( atclient_put_shared_key_request_options *options); -bool atclient_put_shared_key_request_options_is_bypass_cache_initialized( - const atclient_put_shared_key_request_options *options); -void atclient_put_shared_key_request_options_set_bypass_cache_initialized( - atclient_put_shared_key_request_options *options, const bool initialized); -int atclient_put_shared_key_request_options_set_bypass_cache(atclient_put_shared_key_request_options *options, - const bool bypass_cache); -void atclient_put_shared_key_request_options_unset_bypass_cache(atclient_put_shared_key_request_options *options); - /* * 1C. Put PublicKey */ @@ -175,6 +170,20 @@ int atclient_get_shared_key_request_options_set_shared_encryption_key(atclient_g void atclient_get_shared_key_request_options_unset_shared_encryption_key( atclient_get_shared_key_request_options *options); +bool atclient_get_shared_key_request_options_is_iv_initialized(const atclient_get_shared_key_request_options *options); +void atclient_get_shared_key_request_options_set_iv_initialized(atclient_get_shared_key_request_options *options, + const bool initialized); +int atclient_get_shared_key_request_options_set_iv(atclient_get_shared_key_request_options *options, const unsigned char *iv); +void atclient_get_shared_key_request_options_unset_iv(atclient_get_shared_key_request_options *options); + +bool atclient_get_shared_key_request_options_is_bypass_cache_initialized( + const atclient_get_shared_key_request_options *options); +void atclient_get_shared_key_request_options_set_bypass_cache_initialized( + atclient_get_shared_key_request_options *options, const bool initialized); +int atclient_get_shared_key_request_options_set_bypass_cache(atclient_get_shared_key_request_options *options, + const bool bypass_cache); +void atclient_get_shared_key_request_options_unset_bypass_cache(atclient_get_shared_key_request_options *options); + bool atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_shared_key_request_options *options); void atclient_get_shared_key_request_options_set_store_atkey_metadata_initialized( diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index a3f5ecc9..2055d832 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -204,100 +204,6 @@ void atclient_put_shared_key_request_options_unset_shared_encryption_key( atclient_put_shared_key_request_options_set_shared_encryption_key_initialized(options, false); } -bool atclient_put_shared_key_request_options_is_bypass_cache_initialized( - const atclient_put_shared_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_shared_key_request_options_is_bypass_cache_initialized: " - "Invalid arguments\n"); - return false; - } - - /* - * 2. Check if the bypass cache is initialized - */ - return options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & - ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; -} - -void atclient_put_shared_key_request_options_set_bypass_cache_initialized( - atclient_put_shared_key_request_options *options, const bool initialized) { - /* - * 1. Validate arguments - */ - - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_shared_key_request_options_set_bypass_cache_initialized: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Set the bypass cache initialized - */ - if (initialized) { - options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= - ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; - } else { - options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= - ~ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; - } -} - -int atclient_put_shared_key_request_options_set_bypass_cache(atclient_put_shared_key_request_options *options, - const bool bypass_cache) { - int ret = 1; - /* - * 1. Validate arguments - */ - if (options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_shared_key_request_options_set_bypass_cache: " - "Invalid arguments\n"); - goto exit; - } - - /* - * 2. Unset the bypass cache, if necessary - */ - if (atclient_put_shared_key_request_options_is_bypass_cache_initialized(options)) { - atclient_put_shared_key_request_options_unset_bypass_cache(options); - } - - /* - * 3. Set the bypass cache - */ - options->bypass_cache = bypass_cache; - atclient_put_shared_key_request_options_set_bypass_cache_initialized(options, true); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_put_shared_key_request_options_unset_bypass_cache(atclient_put_shared_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put_shared_key_request_options_unset_bypass_cache: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Unset the bypass cache - */ - options->bypass_cache = false; - atclient_put_shared_key_request_options_set_bypass_cache_initialized(options, false); -} - /* * ================= * 1C. Put PublicKey @@ -516,6 +422,7 @@ bool atclient_get_shared_key_request_options_is_shared_encryption_key_initialize return options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX] & ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED; } + void atclient_get_shared_key_request_options_set_shared_encryption_key_initialized( atclient_get_shared_key_request_options *options, const bool initialized) { /* @@ -613,6 +520,202 @@ void atclient_get_shared_key_request_options_unset_shared_encryption_key( atclient_get_shared_key_request_options_set_shared_encryption_key_initialized(options, false); } +bool atclient_get_shared_key_request_options_is_iv_initialized(const atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_is_iv_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the IV is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] & + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; +} + +void atclient_get_shared_key_request_options_set_iv_initialized(atclient_get_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_iv_initialized: Invalid arguments\n"); + return; + } + + /* + * 2. Set the IV initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] |= + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] &= + ~ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; + } +} + +int atclient_get_shared_key_request_options_set_iv(atclient_get_shared_key_request_options *options, const unsigned char *iv) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_iv: Invalid arguments\n"); + goto exit; + } + + if (iv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_iv: Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the IV, if necessary + */ + if (atclient_get_shared_key_request_options_is_iv_initialized(options)) { + atclient_get_shared_key_request_options_unset_iv(options); + } + + /* + * 3. Set the IV + */ + const size_t iv_size = ATCHOPS_AES_256 / 8; + if ((options->iv = (unsigned char *)malloc(sizeof(unsigned char) * iv_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_iv: Failed to allocate memory for IV\n"); + goto exit; + } + + atclient_get_shared_key_request_options_set_iv_initialized(options, true); + memcpy(options->iv, iv, iv_size); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_shared_key_request_options_unset_iv(atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_unset_iv: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the IV + */ + if (atclient_get_shared_key_request_options_is_iv_initialized(options)) { + free(options->iv); + } + options->iv = NULL; + atclient_get_shared_key_request_options_set_iv_initialized(options, false); +} + +bool atclient_get_shared_key_request_options_is_bypass_cache_initialized(const atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_is_bypass_cache_initialized: " + "Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the bypass cache is initialized + */ + return options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] & + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; +} + +void atclient_get_shared_key_request_options_set_bypass_cache_initialized(atclient_get_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_bypass_cache_initialized: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Set the bypass cache initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] |= + ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX] &= + ~ATCLIENT_GET_SHARED_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED; + } +} + +int atclient_get_shared_key_request_options_set_bypass_cache(atclient_get_shared_key_request_options *options, const bool bypass_cache) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_set_bypass_cache: " + "Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the bypass cache, if necessary + */ + if (atclient_get_shared_key_request_options_is_bypass_cache_initialized(options)) { + atclient_get_shared_key_request_options_unset_bypass_cache(options); + } + + /* + * 3. Set the bypass cache + */ + options->bypass_cache = bypass_cache; + atclient_get_shared_key_request_options_set_bypass_cache_initialized(options, true); + + ret = 0; + goto exit; +exit: { return ret; } +} + +void atclient_get_shared_key_request_options_unset_bypass_cache(atclient_get_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_get_shared_key_request_options_unset_bypass_cache: " + "Invalid arguments\n"); + return; + } + + /* + * 2. Unset the bypass cache + */ + options->bypass_cache = false; + atclient_get_shared_key_request_options_set_bypass_cache_initialized(options, false); +} + bool atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized( const atclient_get_shared_key_request_options *options) { /* From 531de8cdfecccc5f3c44e366dfa9343cf67ad522 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:39:19 -0400 Subject: [PATCH 187/193] chore: delete atclient_put.c --- packages/atclient/CMakeLists.txt | 1 - packages/atclient/src/atclient_put.c | 307 --------------------------- 2 files changed, 308 deletions(-) delete mode 100644 packages/atclient/src/atclient_put.c diff --git a/packages/atclient/CMakeLists.txt b/packages/atclient/CMakeLists.txt index 37a08de0..486e94f8 100644 --- a/packages/atclient/CMakeLists.txt +++ b/packages/atclient/CMakeLists.txt @@ -13,7 +13,6 @@ set( ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_public_key.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_self_key.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put_shared_key.c - ${CMAKE_CURRENT_LIST_DIR}/src/atclient_put.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient_utils.c ${CMAKE_CURRENT_LIST_DIR}/src/atclient.c ${CMAKE_CURRENT_LIST_DIR}/src/atkey.c diff --git a/packages/atclient/src/atclient_put.c b/packages/atclient/src/atclient_put.c deleted file mode 100644 index 125746ad..00000000 --- a/packages/atclient/src/atclient_put.c +++ /dev/null @@ -1,307 +0,0 @@ -#include "atclient/atclient.h" -#include "atclient/atkey.h" -#include "atclient/constants.h" -#include "atclient/encryption_key_helpers.h" -#include "atclient/string_utils.h" -#include "atlogger/atlogger.h" -#include -#include -#include -#include -#include -#include -#include - -#define TAG "atclient_put" - -static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const int *commit_id); - -int atclient_put(atclient *ctx, atclient_atkey *atkey, const char *value, int *commit_id) { - int ret = 1; - - /* - * 1. Check if valid arguments were passed - */ - if ((ret = atclient_put_validate_args(ctx, atkey, value, commit_id)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_put_validate_args: %d\n", ret); - return ret; - } - - /* - * 2. Allocate variables - */ - const size_t value_len = strlen(value); - - char *atkey_str = NULL; - char *update_cmd = NULL; - char *metadata_protocol_str = NULL; - - const short iv_size = ATCHOPS_IV_BUFFER_SIZE; - unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; - memset(iv, 0, sizeof(unsigned char) * iv_size); - - const size_t iv_base64_size = atchops_base64_encoded_size(iv_size) + 1; - char iv_base64[iv_base64_size]; - memset(iv_base64, 0, sizeof(char) * iv_base64_size); - - const size_t ciphertext_size = atchops_aes_ctr_ciphertext_size(value_len) + 1; - unsigned char ciphertext[ciphertext_size]; - memset(ciphertext, 0, sizeof(unsigned char) * ciphertext_size); - size_t ciphertext_len = 0; - - const size_t ciphertext_base64_size = atchops_base64_encoded_size(ciphertext_size) + 1; - char ciphertext_base64[ciphertext_base64_size]; - memset(ciphertext_base64, 0, sizeof(char) * ciphertext_base64_size); - size_t ciphertextbase64_len = 0; - - const size_t shared_encryption_key_size = ATCHOPS_AES_256 / 8; - unsigned char shared_encryption_key[shared_encryption_key_size]; - memset(shared_encryption_key, 0, sizeof(unsigned char) * shared_encryption_key_size); - - const size_t shared_encryption_key_base64_size = atchops_base64_encoded_size(shared_encryption_key_size) + 1; - char shared_encryption_key_base64[shared_encryption_key_base64_size]; - memset(shared_encryption_key_base64, 0, sizeof(char) * shared_encryption_key_base64_size); - size_t shared_encryption_key_base64_len = 0; - - const size_t recv_size = 4096; // sufficient buffer size to 1. receive data from a `llookup:shared_key@<>` and 2. to - // receive commmit id from `update:` - unsigned char *recv = NULL; - if (!ctx->async_read) { - recv = malloc(sizeof(unsigned char) * recv_size); - if (recv == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for recv\n"); - goto exit; - } - memset(recv, 0, sizeof(unsigned char) * recv_size); - } - size_t recv_len = 0; - - /* - * 3. Build `update:` command - * 3a. Encrypt the value, if needed. - * 3a.1 If the AtKey is a publickey, no encryption is needed. - * 3a.2 If the AtKey is a selfkey, encrypt with self encryption key. - * 3a.3 If the AtKey is a sharedkey, encrypt with shared encryption key. - * If the shared encryption key doesn't exist, create one for us and one for the other person. - * If the shared encryption key does exist, encrypt with it. - * 3b. Build the command - */ - - const atclient_atkey_type atkey_type = atclient_atkey_get_type(atkey); - - if (atkey_type == ATCLIENT_ATKEY_TYPE_PUBLIC_KEY) { - // 3a.1 no encryption - memcpy(ciphertext_base64, value, value_len); - ciphertextbase64_len = value_len; - } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SELF_KEY) { - // 3a.2 encrypt with self encryption key - const size_t selfencryptionkeysize = ATCHOPS_AES_256 / 8; - unsigned char selfencryptionkey[selfencryptionkeysize]; - memset(selfencryptionkey, 0, sizeof(unsigned char) * selfencryptionkeysize); - size_t selfencryptionkeylen = 0; - - if ((ret = atchops_base64_decode((const unsigned char *)ctx->atkeys.self_encryption_key_base64, - strlen(ctx->atkeys.self_encryption_key_base64), selfencryptionkey, - selfencryptionkeysize, &selfencryptionkeylen)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: %d\n", ret); - goto exit; - } - - if ((ret = atchops_aes_ctr_encrypt(selfencryptionkey, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, - ciphertext, ciphertext_size, &ciphertext_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); - goto exit; - } - - if ((ret = atchops_base64_encode(ciphertext, ciphertext_len, (unsigned char *)ciphertext_base64, - ciphertext_base64_size, &ciphertextbase64_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto exit; - } - } else if (atkey_type == ATCLIENT_ATKEY_TYPE_SHARED_KEY) { - // 3aA.3 encrypt with shared encryption key - - // get our AES shared key - // if it doesn't exist, create one for us and create one for the other person - // create one for us -> encrypted with our self encryption key - // create one for the other person -> encrypted with their public encryption key - char *recipient_atsign_with_at = NULL; - - if ((ret = atclient_string_utils_atsign_with_at(atkey->shared_with, &recipient_atsign_with_at)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_stringutils_concat_at_sign_with_at: %d\n", ret); - goto shared_key_exit; - } - - if ((ret = atclient_get_shared_encryption_key_shared_by_me(ctx, recipient_atsign_with_at, shared_encryption_key)) != - 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_get_shared_encryption_key_shared_by_me: %d\n", ret); - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Creating shared encryption key\n"); - if ((ret = atclient_create_shared_encryption_key_pair_for_me_and_other(ctx, recipient_atsign_with_at, - shared_encryption_key)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_create_shared_encryption_key_pair_for_me_and_other: %d\n", ret); - goto shared_key_exit; - } - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Created shared encryption key successfully\n"); - } - - if ((ret = atchops_iv_generate(iv)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_iv_generate: %d\n", ret); - goto shared_key_exit; - } - - if ((ret = atchops_base64_encode(iv, ATCHOPS_IV_BUFFER_SIZE, (unsigned char *)iv_base64, iv_base64_size, NULL)) != - 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto shared_key_exit; - } - - if ((ret = atclient_atkey_metadata_set_iv_nonce(&(atkey->metadata), iv_base64)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_set_ivnonce: %d\n", ret); - goto shared_key_exit; - } - - if ((ret = atchops_aes_ctr_encrypt(shared_encryption_key, ATCHOPS_AES_256, iv, (unsigned char *)value, value_len, - ciphertext, ciphertext_size, &ciphertext_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_aes_ctr_encrypt: %d\n", ret); - goto shared_key_exit; - } - - if ((ret = atchops_base64_encode(ciphertext, ciphertext_len, (unsigned char *)ciphertext_base64, - ciphertext_base64_size, &ciphertextbase64_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: %d\n", ret); - goto shared_key_exit; - } - - shared_key_exit: { - free(recipient_atsign_with_at); - if (ret != 0) { - goto exit; - } - } - } - - // 3b. Build the command - - if ((ret = atclient_atkey_to_string(atkey, &atkey_str)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_to_string: %d\n", ret); - goto exit; - } - const size_t atkey_strlen = strlen(atkey_str); - - if ((ret = atclient_atkey_metadata_to_protocol_str(&(atkey->metadata), &(metadata_protocol_str))) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_to_protocolstr: %d\n", ret); - goto exit; - } - const size_t metadataprotocolstrlen = strlen(metadata_protocol_str); - - const size_t update_cmd_size = strlen("update: \r\n") + metadataprotocolstrlen + atkey_strlen + ciphertextbase64_len + - 1; // + 1 for null terminator - update_cmd = malloc(sizeof(char) * update_cmd_size); - if (update_cmd == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for update_cmd\n"); - goto exit; - } - memset(update_cmd, 0, sizeof(char) * update_cmd_size); - snprintf(update_cmd, update_cmd_size, "update%.*s:%.*s %.*s\r\n", (int)metadataprotocolstrlen, metadata_protocol_str, - (int)atkey_strlen, atkey_str, (int)ciphertextbase64_len, ciphertext_base64); - - /* - * 4. Send the command - */ - if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)update_cmd, update_cmd_size - 1, - recv, recv_size, &recv_len)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_connection_send: %d\n", ret); - goto exit; - } - - if (ctx->async_read) { - goto exit; - } - - if (!atclient_string_utils_starts_with((char *)recv, "data:")) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n", - (int)recv_len, recv); - goto exit; - } - - /* - * 5. Return the commit id. - */ - - if (commit_id != NULL) { - char *recvwithoutdata = (char *)recv + 5; - *commit_id = atoi(recvwithoutdata); - } - - ret = 0; - goto exit; -exit: { - if (!ctx->async_read) { - free(recv); - } - free(update_cmd); - free(metadata_protocol_str); - free(atkey_str); - return ret; -} -} - -static int atclient_put_validate_args(const atclient *ctx, const atclient_atkey *atkey, const char *value, - const int *commit_id) { - int ret = 1; - if (ctx == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n"); - goto exit; - } - - if (!atclient_is_atserver_connection_started(ctx)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver connection not started\n"); - goto exit; - } - - if (!atclient_is_atsign_initialized(ctx)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign is not allocated. Make sure to PKAM authenticate first\n"); - goto exit; - } - - if (atkey == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); - goto exit; - } - - if (value == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "value is NULL\n"); - goto exit; - } - - if (!atclient_atkey_is_shared_by_initialized(atkey) || strlen(atkey->shared_by) <= 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's shared_by is not initialized or is empty\n"); - goto exit; - } - - if (strcmp(atkey->shared_by, ctx->atsign) != 0) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey's shared_by is not atclient's atsign\n"); - goto exit; - } - - if (ctx->async_read) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_put cannot be called from an async_read atclient, it will cause a race condition\n"); - goto exit; - } - ret = 0; - goto exit; -exit: { return ret; } -} \ No newline at end of file From 3ac6b2212c29c3980056c6f40317dbfee8f8cbbb Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:48:47 -0400 Subject: [PATCH 188/193] chore: add atclient_monitor_read hooks back --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- packages/atclient/include/atclient/monitor.h | 3 ++- packages/atclient/src/monitor.c | 24 +++++++++++++++++-- .../tests/test_atclient_monitor.c | 2 +- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 55cd956a..fae34a34 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -264,7 +264,7 @@ static void *monitor_handler(void *xargs) { pthread_mutex_lock(&monitor_mutex); pthread_mutex_lock(&client_mutex); - ret = atclient_monitor_read(monitor, ctx, &message); + ret = atclient_monitor_read(monitor, ctx, &message, NULL); pthread_mutex_unlock(&monitor_mutex); pthread_mutex_unlock(&client_mutex); diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index fb568973..ba950528 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Starting main monitor loop...\n"); while (true) { - ret = atclient_monitor_read(&monitor_conn, &atclient2, &message); + ret = atclient_monitor_read(&monitor_conn, &atclient2, &message, NULL); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read monitor message: %d\n", ret); continue; diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index ba2add8a..e5ac5f32 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) { size_t max_tries = 10; while (true) { - ret = atclient_monitor_read(&monitor_conn, &atclient2, &message); + ret = atclient_monitor_read(&monitor_conn, &atclient2, &message, NULL); switch (message->type) { case ATCLIENT_MONITOR_MESSAGE_TYPE_NONE: { diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 7c1ac1b3..d8d9c8c2 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -136,12 +136,13 @@ int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size * the caller to allocate memory to this struct, call atclient_monitor_response_init before passing to this function, * then call atclient_monitor_free use. This function populates the message struct with the notification, data response, * or error response read from the monitor connection. + * @param hooks the hooks to use for the monitor connection, can be NULL if no hooks are needed * @return 0 on success, non-zero on error * * @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); +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/src/monitor.c b/packages/atclient/src/monitor.c index b710e7de..e410f994 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -104,7 +104,7 @@ exit: { } } -int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message) { +int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_monitor_response *message, atclient_monitor_hooks *hooks) { int ret = -1; char *buffertemp = NULL; @@ -181,7 +181,27 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m goto exit; } - if ((ret = decrypt_notification(atclient, &(message->notification))) != 0) { + if(hooks != NULL && hooks->pre_decrypt_notification != NULL) { + ret = hooks->pre_decrypt_notification(); + if (ret != 0) { + message->type = ATCLIENT_MONITOR_ERROR_DECRYPT_NOTIFICATION; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to run pre_decrypt_notification hook\n"); + goto exit; + } + } + + ret = decrypt_notification(atclient, &(message->notification)); + + if(hooks != NULL && hooks->post_decrypt_notification != NULL) { + ret = hooks->post_decrypt_notification(ret); + if (ret != 0) { + message->type = ATCLIENT_MONITOR_ERROR_DECRYPT_NOTIFICATION; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to run post_decrypt_notification hook\n"); + goto exit; + } + } + + if (ret != 0) { message->type = ATCLIENT_MONITOR_ERROR_DECRYPT_NOTIFICATION; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to decrypt notification\n"); goto exit; diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index 43963ebc..dd274d90 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -219,7 +219,7 @@ static int monitor_for_notification(atclient *monitor_conn, atclient *atclient2) int tries = 1; while (tries <= max_tries) { - if ((ret = atclient_monitor_read(monitor_conn, atclient2, &message)) != 0) { + if ((ret = atclient_monitor_read(monitor_conn, atclient2, &message, NULL)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read monitor message: %d\n", ret); tries++; continue; From ad9c9eeeff7f742237127359d0dd7e885b5fa909 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:48:54 -0400 Subject: [PATCH 189/193] fix: request_options stuff --- .../include/atclient/request_options.h | 8 ++ packages/atclient/src/request_options.c | 128 +++++++++++++++++- 2 files changed, 134 insertions(+), 2 deletions(-) diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index 8d0a4a53..fd620090 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -11,7 +11,9 @@ #define ATCLIENT_PUT_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX 1 #define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 0) +#define ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED (VALUE_INITIALIZED << 1) #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_SHARED_ENCRYPTION_KEY_INDEX 0 #define ATCLIENT_GET_SELF_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 @@ -135,6 +137,12 @@ int atclient_put_shared_key_request_options_set_shared_encryption_key(atclient_p void atclient_put_shared_key_request_options_unset_shared_encryption_key( atclient_put_shared_key_request_options *options); +bool atclient_put_shared_key_request_options_is_iv_initialized(const atclient_put_shared_key_request_options *options); +void atclient_put_shared_key_request_options_set_iv_initialized(atclient_put_shared_key_request_options *options, + const bool initialized); +int atclient_put_shared_key_request_options_set_iv(atclient_put_shared_key_request_options *options, const unsigned char *iv); +void atclient_put_shared_key_request_options_unset_iv(atclient_put_shared_key_request_options *options); + /* * 1C. Put PublicKey */ diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index 2055d832..aede1d83 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -81,8 +81,8 @@ void atclient_put_shared_key_request_options_free(atclient_put_shared_key_reques atclient_put_shared_key_request_options_unset_shared_encryption_key(options); } - if (atclient_put_shared_key_request_options_is_bypass_cache_initialized(options)) { - atclient_put_shared_key_request_options_unset_bypass_cache(options); + if(atclient_put_shared_key_request_options_is_iv_initialized(options)) { + atclient_put_shared_key_request_options_unset_iv(options); } } @@ -204,6 +204,113 @@ void atclient_put_shared_key_request_options_unset_shared_encryption_key( atclient_put_shared_key_request_options_set_shared_encryption_key_initialized(options, false); } +bool atclient_put_shared_key_request_options_is_iv_initialized(const atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_is_iv_initialized: Invalid arguments\n"); + return false; + } + + /* + * 2. Check if the IV is initialized + */ + return options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] & + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; +} + +void atclient_put_shared_key_request_options_set_iv_initialized(atclient_put_shared_key_request_options *options, const bool initialized) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_iv_initialized: Invalid arguments\n"); + return; + } + + /* + * 2. Set the IV initialized + */ + if (initialized) { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] |= + ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; + } else { + options->_initialized_fields[ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INDEX] &= + ~ATCLIENT_PUT_SHARED_KEY_REQUEST_OPTIONS_IV_INITIALIZED; + } +} + +int atclient_put_shared_key_request_options_set_iv(atclient_put_shared_key_request_options *options, const unsigned char *iv) { + int ret = 1; + + /* + * 1. Validate arguments + */ + if (options == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_iv: Invalid arguments\n"); + goto exit; + } + + if (iv == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_iv: Invalid arguments\n"); + goto exit; + } + + /* + * 2. Unset the IV, if necessary + */ + if (atclient_put_shared_key_request_options_is_iv_initialized(options)) { + atclient_put_shared_key_request_options_unset_iv(options); + } + + /* + * 3. Set the IV + */ + const size_t iv_size = ATCHOPS_AES_256 / 8; + if ((options->iv = (unsigned char *)malloc(sizeof(unsigned char) * iv_size)) == NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_set_iv: Failed to allocate memory for IV\n"); + goto exit; + } + + atclient_put_shared_key_request_options_set_iv_initialized(options, true); + memcpy(options->iv, iv, iv_size); + + ret = 0; + goto exit; +exit: { + return ret; +} +} + +void atclient_put_shared_key_request_options_unset_iv(atclient_put_shared_key_request_options *options) { + /* + * 1. Validate arguments + */ + if (options == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_put_shared_key_request_options_unset_iv: Invalid arguments\n"); + return; + } + + /* + * 2. Unset the IV + */ + if (atclient_put_shared_key_request_options_is_iv_initialized(options)) { + free(options->iv); + } + options->iv = NULL; + atclient_put_shared_key_request_options_set_iv_initialized(options, false); +} + /* * ================= * 1C. Put PublicKey @@ -238,6 +345,7 @@ void atclient_put_public_key_request_options_free(atclient_put_public_key_reques /* * 2. Free the options */ + } /* @@ -403,6 +511,18 @@ void atclient_get_shared_key_request_options_free(atclient_get_shared_key_reques if (atclient_get_shared_key_request_options_is_shared_encryption_key_initialized(options)) { atclient_get_shared_key_request_options_unset_shared_encryption_key(options); } + + if (atclient_get_shared_key_request_options_is_iv_initialized(options)) { + atclient_get_shared_key_request_options_unset_iv(options); + } + + if (atclient_get_shared_key_request_options_is_bypass_cache_initialized(options)) { + atclient_get_shared_key_request_options_unset_bypass_cache(options); + } + + if (atclient_get_shared_key_request_options_is_store_atkey_metadata_initialized(options)) { + atclient_get_shared_key_request_options_unset_store_atkey_metadata(options); + } } bool atclient_get_shared_key_request_options_is_shared_encryption_key_initialized( @@ -848,6 +968,10 @@ void atclient_get_public_key_request_options_free(atclient_get_public_key_reques if (atclient_get_public_key_request_options_is_store_atkey_metadata_initialized(options)) { atclient_get_public_key_request_options_unset_store_atkey_metadata(options); } + + if (atclient_get_public_key_request_options_is_bypass_cache_initialized(options)) { + atclient_get_public_key_request_options_unset_bypass_cache(options); + } } bool atclient_get_public_key_request_options_is_bypass_cache_initialized( From e4a47319ee6fa506ccdab896f699af0863bbee64 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 11:50:32 -0400 Subject: [PATCH 190/193] chore: move constants --- packages/atclient/include/atclient/atkey.h | 7 +++++ .../atclient/include/atclient/constants.h | 29 +++++++------------ .../atclient/include/atclient/notify_params.h | 2 ++ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/atclient/include/atclient/atkey.h b/packages/atclient/include/atclient/atkey.h index 5e13b778..f3b25d69 100644 --- a/packages/atclient/include/atclient/atkey.h +++ b/packages/atclient/include/atclient/atkey.h @@ -5,6 +5,13 @@ #include #include +#define ATCLIENT_ATKEY_KEY_LEN 55 // 55 utf7 chars +#define ATCLIENT_ATKEY_NAMESPACE_LEN 55 // 55 utf7 chars +#define ATCLIENT_ATKEY_COMPOSITE_LEN (ATCLIENT_ATKEY_KEY_LEN + 1 + ATCLIENT_ATKEY_NAMESPACE_LEN) // {key}.{namespace} +#define ATCLIENT_ATKEY_FULL_LEN \ + (ATCLIENT_ATSIGN_FULL_LEN + 1 + ATCLIENT_ATKEY_COMPOSITE_LEN + \ + ATCLIENT_ATSIGN_FULL_LEN) // {full_atsign}:{composite_key}{full_atsign} + #define VALUE_INITIALIZED 0b00000001 #define ATCLIENT_ATKEY_KEY_INDEX 0 diff --git a/packages/atclient/include/atclient/constants.h b/packages/atclient/include/atclient/constants.h index 6105056f..6ceffb85 100644 --- a/packages/atclient/include/atclient/constants.h +++ b/packages/atclient/include/atclient/constants.h @@ -6,19 +6,12 @@ #define ATCLIENT_ATSIGN_INNER_LEN 55 // 55 utf7 chars #define ATCLIENT_ATSIGN_FULL_LEN (1 + ATCLIENT_ATSIGN_INNER_LEN) // '@' + 55 utf7 chars -#define ATCLIENT_ATKEY_KEY_LEN 55 // 55 utf7 chars -#define ATCLIENT_ATKEY_NAMESPACE_LEN 55 // 55 utf7 chars -#define ATCLIENT_ATKEY_COMPOSITE_LEN (ATCLIENT_ATKEY_KEY_LEN + 1 + ATCLIENT_ATKEY_NAMESPACE_LEN) // {key}.{namespace} -#define ATCLIENT_ATKEY_FULL_LEN \ - (ATCLIENT_ATSIGN_FULL_LEN + 1 + ATCLIENT_ATKEY_COMPOSITE_LEN + \ - ATCLIENT_ATSIGN_FULL_LEN) // {full_atsign}:{composite_key}{full_atsign} -#define ATCLIENT_DEFAULT_NOTIFIER "SYSTEM" +#define ATCLIENT_CLIENT_READ_TIMEOUT_MS 3 * 1000 // 3 seconds to time out if no data is available +#define ATCLIENT_MONITOR_READ_TIMEOUT_MS 3 * 1000 // 3 seconds to time out if no data is available -#define ATCLIENT_CLIENT_READ_TIMEOUT_MS 3*1000 // 3 seconds to time out if no data is available -#define ATCLIENT_MONITOR_READ_TIMEOUT_MS 3*1000 // 3 seconds to time out if no data is available - -#define ATCLIENT_CONNECTION_MAX_READ_TRIES 50 // if 0 bytes are read after 10 consecutive retries, the read is unsuccessful. See connection.c +#define ATCLIENT_CONNECTION_MAX_READ_TRIES \ + 50 // if 0 bytes are read after 10 consecutive retries, the read is unsuccessful. See connection.c #define ATCLIENT_MONITOR_BUFFER_LEN 4096 // max chunk size monitor can read at once @@ -33,7 +26,7 @@ #define CYN "\e[0;36m" #define WHT "\e[0;37m" -//Regular bold text +// Regular bold text #define BBLK "\e[1;30m" #define BRED "\e[1;31m" #define BGRN "\e[1;32m" @@ -43,7 +36,7 @@ #define BCYN "\e[1;36m" #define BWHT "\e[1;37m" -//Regular underline text +// Regular underline text #define UBLK "\e[4;30m" #define URED "\e[4;31m" #define UGRN "\e[4;32m" @@ -53,7 +46,7 @@ #define UCYN "\e[4;36m" #define UWHT "\e[4;37m" -//Regular background +// Regular background #define BLKB "\e[40m" #define REDB "\e[41m" #define GRNB "\e[42m" @@ -63,7 +56,7 @@ #define CYNB "\e[46m" #define WHTB "\e[47m" -//High intensty background +// High intensty background #define BLKHB "\e[0;100m" #define REDHB "\e[0;101m" #define GRNHB "\e[0;102m" @@ -73,7 +66,7 @@ #define CYNHB "\e[0;106m" #define WHTHB "\e[0;107m" -//High intensty text +// High intensty text #define HBLK "\e[0;90m" #define HRED "\e[0;91m" #define HGRN "\e[0;92m" @@ -83,7 +76,7 @@ #define HCYN "\e[0;96m" #define HWHT "\e[0;97m" -//Bold high intensity text +// Bold high intensity text #define BHBLK "\e[1;90m" #define BHRED "\e[1;91m" #define BHGRN "\e[1;92m" @@ -93,7 +86,7 @@ #define BHCYN "\e[1;96m" #define BHWHT "\e[1;97m" -//Reset +// Reset #define reset "\e[0m" #define CRESET "\e[0m" #define COLOR_RESET "\e[0m" diff --git a/packages/atclient/include/atclient/notify_params.h b/packages/atclient/include/atclient/notify_params.h index 69c81878..7a83adc6 100644 --- a/packages/atclient/include/atclient/notify_params.h +++ b/packages/atclient/include/atclient/notify_params.h @@ -4,6 +4,8 @@ #include "atclient/atclient.h" #include "atclient/atkey.h" +#define ATCLIENT_DEFAULT_NOTIFIER "SYSTEM" + #define VALUE_INITIALIZED 0b00000001 #define ATCLIENT_NOTIFY_PARAMS_ID_INDEX 0 From 0d3ffe44226010f192364ba84c57c57677240f33 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 12:49:10 -0400 Subject: [PATCH 191/193] chore: remove regexlen from atclient_monitor_start --- examples/desktop/at_talk/src/main.c | 4 ++-- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 4 ++-- packages/atclient/include/atclient/monitor.h | 3 +-- packages/atclient/src/monitor.c | 4 +++- tests/functional_tests/tests/test_atclient_monitor.c | 4 ++-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index fae34a34..14f90a0d 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -250,7 +250,7 @@ static void *monitor_handler(void *xargs) { goto exit; } atclient_monitor_set_read_timeout(monitor, READ_TIMEOUT); // blocking read takes 1 second to timeout - if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) { + if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); goto exit; } @@ -372,7 +372,7 @@ static int reconnect_clients(atclient *monitor, atclient *ctx, const char *atser * 3. Start monitor */ atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Restarting monitor...\n"); - if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) { + if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); return ret; } diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index ba950528..bbd5cdbf 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_monitor_start(&monitor_conn, ".*", strlen(".*"))) != 0) { + if ((ret = atclient_monitor_start(&monitor_conn, ".*") != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Monitor crashed\n"); goto exit; } diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index e5ac5f32..f387c8b2 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) { &monitor_conn, 1 * 1000); // monitor read will wait at most 1 second for a message. As soon bytes are read, it // will return. If no bytes are read, it will return after 3 seconds. - if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) { + if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Monitor crashed\n"); goto exit; } @@ -140,7 +140,7 @@ int main(int argc, char *argv[]) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n"); continue; } - if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) { + if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Monitor crashed\n"); continue; } diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index d8d9c8c2..179ba72f 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -120,10 +120,9 @@ void atclient_monitor_set_read_timeout(atclient *monitor_conn, const int timeout * * @param monitor_conn ctx the atclient context for the monitor connection, must be pkam_authenticated already * @param regex the regex to monitor for - * @param regexlen the length of the regex string, most people will typically use strlen(regex) * @return int 0 on success, non-zero on error */ -int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size_t regexlen); +int atclient_monitor_start(atclient *monitor_conn, const char *regex); /** * @brief Read a notification from the monitor connection into message diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index e410f994..a5dc63fa 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -60,12 +60,14 @@ void atclient_monitor_set_read_timeout(atclient *monitor_conn, const int timeout mbedtls_ssl_conf_read_timeout(&(monitor_conn->atserver_connection.ssl_config), timeoutms); } -int atclient_monitor_start(atclient *monitor_conn, const char *regex, const size_t regexlen) { +int atclient_monitor_start(atclient *monitor_conn, const char *regex) { int ret = 1; size_t cmdsize = 0; char *cmd = NULL; + const size_t regexlen = strlen(regex); + // log building command... (Debug) // atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Building monitor command...\n"); diff --git a/tests/functional_tests/tests/test_atclient_monitor.c b/tests/functional_tests/tests/test_atclient_monitor.c index dd274d90..2fa711b3 100644 --- a/tests/functional_tests/tests/test_atclient_monitor.c +++ b/tests/functional_tests/tests/test_atclient_monitor.c @@ -263,7 +263,7 @@ static int test_1_start_monitor(atclient *monitor_conn) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_1_start_monitor Start\n"); - ret = atclient_monitor_start(monitor_conn, MONITOR_REGEX, strlen(MONITOR_REGEX)); + ret = atclient_monitor_start(monitor_conn, MONITOR_REGEX); if (ret != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); goto exit; @@ -328,7 +328,7 @@ static int test_4_re_pkam_auth_and_start_monitor(atclient *monitor_conn) { goto exit; } - if ((ret = atclient_monitor_start(monitor_conn, MONITOR_REGEX, strlen(MONITOR_REGEX))) != 0) { + if ((ret = atclient_monitor_start(monitor_conn, MONITOR_REGEX)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); goto exit; } From 55efad4bda3d8d26a50e9d7a3508f84290d24d2d Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 12:52:27 -0400 Subject: [PATCH 192/193] fix: examples --- examples/desktop/at_talk/src/main.c | 2 +- examples/desktop/events/monitor.c | 2 +- examples/desktop/events/resilient_monitor.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index 14f90a0d..d5fa90f3 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -372,7 +372,7 @@ static int reconnect_clients(atclient *monitor, atclient *ctx, const char *atser * 3. Start monitor */ atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Restarting monitor...\n"); - if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX) != 0) { + if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); return ret; } diff --git a/examples/desktop/events/monitor.c b/examples/desktop/events/monitor.c index bbd5cdbf..f7c430e5 100644 --- a/examples/desktop/events/monitor.c +++ b/examples/desktop/events/monitor.c @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) { goto exit; } - if ((ret = atclient_monitor_start(&monitor_conn, ".*") != 0) { + if ((ret = atclient_monitor_start(&monitor_conn, ".*")) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Monitor crashed\n"); goto exit; } diff --git a/examples/desktop/events/resilient_monitor.c b/examples/desktop/events/resilient_monitor.c index f387c8b2..5116bcd8 100644 --- a/examples/desktop/events/resilient_monitor.c +++ b/examples/desktop/events/resilient_monitor.c @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) { &monitor_conn, 1 * 1000); // monitor read will wait at most 1 second for a message. As soon bytes are read, it // will return. If no bytes are read, it will return after 3 seconds. - if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX) != 0) { + if ((ret = atclient_monitor_start(&monitor_conn, MONITOR_REGEX)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Monitor crashed\n"); goto exit; } From 5da81ffbf8f0020eb59ead3e4760e05f6cf95329 Mon Sep 17 00:00:00 2001 From: Jeremy Tubongbanua Date: Tue, 30 Jul 2024 12:53:39 -0400 Subject: [PATCH 193/193] fix: --- examples/desktop/at_talk/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/desktop/at_talk/src/main.c b/examples/desktop/at_talk/src/main.c index d5fa90f3..b76752f9 100644 --- a/examples/desktop/at_talk/src/main.c +++ b/examples/desktop/at_talk/src/main.c @@ -250,7 +250,7 @@ static void *monitor_handler(void *xargs) { goto exit; } atclient_monitor_set_read_timeout(monitor, READ_TIMEOUT); // blocking read takes 1 second to timeout - if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX) != 0) { + if ((ret = atclient_monitor_start(monitor, MONITOR_REGEX)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start monitor: %d\n", ret); goto exit; }