Skip to content

Commit

Permalink
fix: implment new method of trimming 'data:' from server responses (#385
Browse files Browse the repository at this point in the history
)

* feat: intro method to fetch substring pos and replace it with startsWith 'data:' check

* fix: change logger level 'ERRO' to 'ERROR'

* fix: update method definition of atclient_pkam_authenticate_validate_arguments()
  • Loading branch information
srieteja authored Sep 11, 2024
1 parent 0b0be7b commit 541a670
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 70 deletions.
2 changes: 2 additions & 0 deletions packages/atclient/include/atclient/constants.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_HOST "root.atsign.org"
#define ATCLIENT_PKAM_AUTHENTICATE_DEFAULT_AT_DIRECTORY_PORT 64

#define DATA_TOKEN "data:"

#define BLK "\e[0;30m"
#define RED "\e[0;31m"
#define GRN "\e[0;32m"
Expand Down
10 changes: 10 additions & 0 deletions packages/atclient/include/atclient/string_utils.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ bool atclient_string_utils_starts_with(const char *string, const char *prefix);
*/
bool atclient_string_utils_ends_with(const char *string, const char *suffix);

/**
* @brief locates the position of a substring in a given string
*
* @param string the string to check (main-string)
* @param substring the substring to look for in the main-string
* @param position where the pointer to the position of substring is stored
* @return
*/
int atclient_string_utils_get_substring_position(const char *string, const char *substring, char **position);

/**
* @brief generate a new string with the atsign and the guaranteed @ symbol
*
Expand Down
20 changes: 10 additions & 10 deletions packages/atclient/src/atclient.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void atclient_free(atclient *ctx) {
atclient_unset_atsign(ctx);
}

if (atclient_is_atserver_connection_started(ctx)) {
if(atclient_is_atserver_connection_started(ctx)) {
atclient_stop_atserver_connection(ctx);
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
int ret = 1; // error by default
if(options == NULL){
atclient_pkam_authenticate_options options;
atclient_pkam_authenticate_options_init(&options);
atclient_pkam_authenticate_options_init(&options);
}

/*
Expand Down Expand Up @@ -259,7 +259,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
// Check if the atserver host and atserver port are initialized
if (atclient_pkam_authenticate_options_is_atserver_host_initialized(options) != 0 ||
atclient_pkam_authenticate_options_is_atserver_port_initialized(options) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver host and port are not set: %d\n", ret);
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver host and port are not set: %d\n", ret);
goto exit;
}
/*
Expand Down Expand Up @@ -290,7 +290,8 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
goto exit;
}

if (!atclient_string_utils_starts_with((char *)recv, "data:")) {
char *str_with_data_prefix = NULL;
if (atclient_string_utils_get_substring_position((char *)recv, DATA_TOKEN, &str_with_data_prefix) != 0) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "recv was \"%.*s\" and did not have prefix \"data:\"\n",
(int)recv_len, recv);
Expand All @@ -301,10 +302,9 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
* 6. We got `data:<challenge>`
* Let us sign the challenge with RSA-2048 PKAM Private Key and Base64 Encode it
*/
memcpy(challenge, recv, recv_len);
// remove data:
memmove(challenge, challenge + 5, recv_len - 5);
challenge_len = recv_len - 5;

challenge_len = strlen(str_with_data_prefix) - strlen(DATA_TOKEN);
memcpy(challenge, str_with_data_prefix + strlen(DATA_TOKEN), challenge_len); // +5 to skip the 'data:' prefix

// sign
if ((ret = atchops_rsa_sign(&atkeys->pkam_private_key, ATCHOPS_MD_SHA256, (unsigned char *)challenge, challenge_len,
Expand Down Expand Up @@ -432,8 +432,8 @@ int atclient_send_heartbeat(atclient *heartbeat_conn) {
/*
* 3. Parse response
*/
if (!atclient_string_utils_starts_with((const char *)ptr, "data:ok") &&
!atclient_string_utils_ends_with((const char *)ptr, "data:ok")) {
// how about just doing ptr == "data:ok" ?
if (strcmp(ptr, "data:ok") != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to receive heartbeat response\n");
ret = -1;
goto exit;
Expand Down
12 changes: 6 additions & 6 deletions packages/atclient/src/atclient_delete.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ int atclient_delete(atclient *atclient, const atclient_atkey *atkey, const atcli
goto exit;
}

char *respose = (char *)recv;

if (!atclient_string_utils_starts_with(respose, "data:")) {
char *response = (char *)recv;
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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 = respose + strlen("data:");
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

if(commit_id != NULL) {
*commit_id = atoi(response_without_data);
*commit_id = atoi(response_trimmed);
}

ret = 0;
Expand Down
11 changes: 7 additions & 4 deletions packages/atclient/src/atclient_get_atkeys.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atclient/constants.h>

#define TAG "atclient_get_atkeys"

Expand Down Expand Up @@ -84,16 +85,18 @@ int atclient_get_atkeys(atclient *atclient, atclient_atkey **atkey, size_t *outp
/*
* 5. Parse response
*/
if (!atclient_string_utils_starts_with((char *)recv, "data:")) {
char *response = (char *)recv;
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

char *recvwithoutdata = (char *)recv + 5;

root = cJSON_Parse(recvwithoutdata);
root = cJSON_Parse(response_trimmed);
if (root == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse failed\n");
Expand Down
9 changes: 5 additions & 4 deletions packages/atclient/src/atclient_get_public_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va
* 5. Parse response
*/
char *response = (char *)recv;
if (!atclient_string_utils_starts_with(response, "data:")) {
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

char *response_without_data = response + 5;

if ((root = cJSON_Parse(response_without_data)) == NULL) {
if ((root = cJSON_Parse(response_trimmed)) == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret);
goto exit;
Expand Down
9 changes: 5 additions & 4 deletions packages/atclient/src/atclient_get_self_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ int atclient_get_self_key(atclient *atclient, atclient_atkey *atkey, char **valu
* 5. Parse response
*/
char *response = (char *)recv;
if (!atclient_string_utils_starts_with(response, "data:")) {
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

char *response_without_data = (char *)recv + 5;

if ((root = cJSON_Parse(response_without_data)) == NULL) {
if ((root = cJSON_Parse(response_trimmed)) == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret);
goto exit;
Expand Down
28 changes: 15 additions & 13 deletions packages/atclient/src/atclient_get_shared_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <atclient/constants.h>

#define TAG "atclient_get_sharedkey"

Expand Down Expand Up @@ -226,16 +227,17 @@ static int atclient_get_shared_key_shared_by_me_with_other(
* 7. Parse response
*/
char *response = (char *)recv;

if (!atclient_string_utils_starts_with(response, "data:")) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n");
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

char *response_without_data = response + 5;

if ((root = cJSON_Parse(response_without_data)) == NULL) {
if ((root = cJSON_Parse(response_trimmed)) == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret);
goto exit;
Expand Down Expand Up @@ -451,17 +453,17 @@ atclient_get_shared_key_shared_by_other_with_me(atclient *atclient, atclient_atk
* 7. Parse response
*/
char *response = (char *)recv;

// Truncate response : "data:"
if (!atclient_string_utils_starts_with(response, "data:")) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "response does not start with 'data:'\n");
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

char *response_without_data = response + 5;

if ((root = cJSON_Parse(response_without_data)) == NULL) {
if ((root = cJSON_Parse(response_trimmed)) == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "cJSON_Parse: %d\n", ret);
goto exit;
Expand Down
11 changes: 6 additions & 5 deletions packages/atclient/src/atclient_put_public_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <atlogger/atlogger.h>
#include <stdlib.h>
#include <string.h>
#include <atclient/constants.h>

#define TAG "atclient_put_public_key"

Expand Down Expand Up @@ -85,21 +86,21 @@ 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:")) {
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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:");
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

/*
* 5. Receive commit id
*/
if (commit_id != NULL) {
*commit_id = atoi(response_without_data);
*commit_id = atoi(response_trimmed);
}

ret = 0;
Expand Down
11 changes: 6 additions & 5 deletions packages/atclient/src/atclient_put_self_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <atlogger/atlogger.h>
#include <stdlib.h>
#include <string.h>
#include <atclient/constants.h>

#define TAG "atclient_put_self_key"

Expand Down Expand Up @@ -158,21 +159,21 @@ 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:")) {
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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:");
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

/*
* 5. Receive commit id
*/
if (commit_id != NULL) {
*commit_id = atoi(response_without_data);
*commit_id = atoi(response_trimmed);
}

ret = 0;
Expand Down
13 changes: 6 additions & 7 deletions packages/atclient/src/atclient_put_shared_key.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,20 @@ 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:")) {
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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;
}

char *response_without_data = response + strlen("data:");

response_trimmed = response_trimmed + strlen(DATA_TOKEN);
/*
* 8. Return commit id
*/
if (commit_id != NULL) {
*commit_id = atoi(response_without_data);
*commit_id = atoi(response_trimmed);
}

ret = 0;
Expand Down
21 changes: 10 additions & 11 deletions packages/atclient/src/encryption_key_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,28 @@ int atclient_get_public_encryption_key(atclient *ctx, const char *atsign, char *
* 5. Parse repsonse
*/
char *response = (char *)recv;

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;
}
char *response_trimmed = NULL;
// below method points the response_trimmed variable to the position of 'data:' substring
if(atclient_string_utils_get_substring_position(response, DATA_TOKEN, &response_trimmed) != 0) {
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 + 5; // skip "data:"
response_trimmed = response_trimmed + strlen(DATA_TOKEN);

/*
* 6. Allocate memory for public_encryption_key and give output to caller
*/
const size_t public_encryption_key_len = strlen(response_without_data);
const size_t public_encryption_key_len = strlen(response_trimmed);
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_len);
memcpy(*public_encryption_key, response_trimmed, public_encryption_key_len);
(*public_encryption_key)[public_encryption_key_len] = '\0';

ret = 0;
Expand Down
Loading

0 comments on commit 541a670

Please sign in to comment.