Skip to content

Commit

Permalink
feat: c at_activate and required components (#423)
Browse files Browse the repository at this point in the history
* feat: create atcommons and introduce enroll related deps

* feat: introduce arg parser for at_activate

* feat: add apkam_symmetric_key to atkeys_file

* feat: add apkam_symmetric_key to atkeys

* feat: introduce atactivate + hex, base64 and utf8 utils

* feat: cmakeLists update + default homedir impl + more atactivate code

* fix: import platform dependent header limits.

* chore: rename all instance of "atclient_pkam_authenticate_options_*" -> "atclient_authenticate_options_*"

* test: atcommons unit tests

* chore(deps): bump github/codeql-action in the github-actions group

Bumps the github-actions group with 1 update: [github/codeql-action](https://github.com/github/codeql-action).

Updates `github/codeql-action` from 3.27.1 to 3.27.3
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@4f3212b...396bb3e)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps): bump github/codeql-action in the github-actions group

Bumps the github-actions group with 1 update: [github/codeql-action](https://github.com/github/codeql-action).

Updates `github/codeql-action` from 3.27.3 to 3.27.4
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@396bb3e...ea9e4e3)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps): bump github/codeql-action in the github-actions group

Bumps the github-actions group with 1 update: [github/codeql-action](https://github.com/github/codeql-action).

Updates `github/codeql-action` from 3.27.4 to 3.27.5
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@ea9e4e3...f09c1c0)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions
...

Signed-off-by: dependabot[bot] <[email protected]>

* feat: introduce auth_cli

* Revert "feat: introduce auth_cli"

This reverts commit a3d043a.
This reverts commit d646b46.
This reverts commit aff312e.
This reverts commit e8c7b71.

* fix: address review comments

* chore: revert changes reg recursive write in atclient/connection.c

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: xavierchanth <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored and JeremyTubongbanua committed Dec 12, 2024
1 parent 7a6127c commit 54c6264
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 96 deletions.
2 changes: 1 addition & 1 deletion packages/atclient/include/atclient/atclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ void atclient_stop_atserver_connection(atclient *ctx);
* the pkam private key and atclient context is connected to the root server
*
* @param ctx initialized atclient context
* @param atsign the atsign the atkeys belong to, this string is assumed to be null terminated
* @param atkeys populated atkeys, especially with the pkam private key
* @param atsign the atsign the atkeys belong to, this string is assumed to be null terminated
* @param options pointer to an initialized atclient_authenticate_options struct that stored auth params
* @return int 0 on success, non-zero on error
*/
Expand Down
62 changes: 24 additions & 38 deletions packages/atclient/src/atclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
/*
* 2. Initialize variables
*/
char *atdirectory_host = NULL;
int atdirectory_port = 0;

// free later
char *root_cmd = NULL;
char *from_cmd = NULL;
char *pkam_cmd = NULL;
char *atsign_with_at = NULL;

char *atserver_host = NULL;
int atserver_port = 0;
Expand All @@ -229,12 +233,6 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
memset(signature_base64, 0, sizeof(unsigned char) * signature_base64_size);
size_t signature_base64_len = 0;

// free later
char *root_cmd = NULL;
char *from_cmd = NULL;
char *pkam_cmd = NULL;
char *atsign_with_at = NULL;

/*
* 3. Ensure that the atsign has the @ symbol.
*/
Expand All @@ -246,34 +244,22 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
const char *atsign_without_at = atsign_with_at + 1;

/*
* 4. Get atdirectory_host and atdirectory_port
*/
if(options != NULL && atclient_authenticate_options_is_atdirectory_host_initialized(options) && options->atdirectory_host != NULL &&
atclient_authenticate_options_is_atdirectory_port_initialized(options) && options->atdirectory_port != 0) {
atdirectory_host = options->atdirectory_host;
atdirectory_port = options->atdirectory_port;
} else {
atdirectory_host = ATCLIENT_ATDIRECTORY_PRODUCTION_HOST;
atdirectory_port = ATCLIENT_ATDIRECTORY_PRODUCTION_PORT;
}

/*
* 5. Get atserver_host and atserver_port
* 4. Get atserver_host and atserver_port
*/
bool should_free_atserver_host;
if (options != NULL && atclient_authenticate_options_is_atserver_host_initialized(options) &&
options->atserver_host != NULL && atclient_authenticate_options_is_atserver_port_initialized(options) &&
options->atserver_port != 0) {
atserver_host = options->atserver_host;
atserver_port = options->atserver_port;
should_free_atserver_host = true;
bool should_free_atserver_host = false;
if (options != NULL) {
if (atclient_authenticate_options_is_atdirectory_host_initialized(options) &&
atclient_authenticate_options_is_atdirectory_port_initialized(options)) {
atserver_host = options->atdirectory_host;
atserver_port = options->atdirectory_port;
}
}

if (atserver_host == NULL || atserver_port == 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO,
"Missing atServer host or port. Using %s:%lu atDirectory to find atServer address\n", atdirectory_host, atdirectory_port);
if ((ret = atclient_utils_find_atserver_address(atdirectory_host,
atdirectory_port, atsign, &atserver_host,
"Missing atServer host or port. Using production atDirectory to look up atServer host and port\n");
if ((ret = atclient_utils_find_atserver_address(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST,
ATCLIENT_ATDIRECTORY_PRODUCTION_PORT, atsign, &atserver_host,
&atserver_port)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret);
goto exit;
Expand All @@ -283,15 +269,15 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
}

/*
* 6. Start atServer connection (kill the existing connection if it exists)
* 5. Start atServer connection (kill the existing connection if it exists)
*/
if ((ret = atclient_start_atserver_connection(ctx, atserver_host, atserver_port)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_start_atserver_connection: %d\n", ret);
goto exit;
}

/*
* 7a. Build `from:` cmd
* 6a. Build `from:` cmd
*/
const size_t from_cmd_size =
strlen("from:") + strlen(atsign_without_at) + strlen("\r\n") + 1; // "from:" has a length of 5
Expand All @@ -302,7 +288,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
snprintf(from_cmd, from_cmd_size, "from:%s\r\n", atsign_without_at);

/*
* 7b. Send `from:` cmd
* 6b. Send `from:` cmd
*/
if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)from_cmd, from_cmd_size - 1, recv,
recvsize, &recv_len)) != 0) {
Expand All @@ -319,7 +305,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
}

/*
* 8. We got `data:<challenge>`
* 7. We got `data:<challenge>`
* Let us sign the challenge with RSA-2048 PKAM Private Key and Base64 Encode it
*/

Expand All @@ -341,7 +327,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
}

/*
* 9a. Build `pkam:` noop_cmd
* 8a. Build `pkam:` noop_cmd
*/
size_t pkam_cmd_size = strlen("pkam:");
if (atclient_atkeys_is_enrollment_id_initialized((atclient_atkeys *)atkeys) && atkeys->enrollment_id != NULL) {
Expand All @@ -362,7 +348,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
pos += snprintf(pkam_cmd + pos, pkam_cmd_size - pos, "%s\r\n", signature_base64);

/*
* 9b. Send `pkam:` noop_cmd
* 8b. Send `pkam:` noop_cmd
*/
memset(recv, 0, sizeof(unsigned char) * recvsize);
if ((ret = atclient_connection_send(&(ctx->atserver_connection), (unsigned char *)pkam_cmd, pkam_cmd_size - 1, recv,
Expand All @@ -380,7 +366,7 @@ int atclient_pkam_authenticate(atclient *ctx, const char *atsign, const atclient
}

/*
* 10. Set up the atclient context
* 9. Set up the atclient context
*/

// initialize ctx->atsign.atsign and ctx->atsign.withour_prefix_str to the newly authenticated atSign
Expand Down
114 changes: 57 additions & 57 deletions tests/functional_tests/tests/test_atclient_pkam_authenticate.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "atclient/request_options.h"
#include "functional_tests/config.h"
#include "functional_tests/helpers.h"
#include <atclient/atclient.h>
#include <atclient/atclient_utils.h>
#include <atclient/atkeys_file.h>
Expand All @@ -10,29 +9,42 @@
#include <stdlib.h>
#include <string.h>

#define ATSIGN FIRST_ATSIGN
static const size_t atkeyspathsize = 1024;
static char atkeyspath[1024];
static char *atsign;

static int test_1_pkam_with_null_options();
static int test_2_pkam_with_options();
static int test1_pkam_no_options();
static int test2_pkam_with_options();
// TODO: add apkam enrollment
// - can't do this as a unit test until we have at_activate in C
// static int test3_apkam_enrollment();

int main() {
int ret = 0;

atsign = FIRST_ATSIGN;

atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG);

ret += test_1_pkam_with_null_options();
ret += test_2_pkam_with_options();
memset(atkeyspath, 0, atkeyspathsize);
size_t atkeyspathlen = 0;

exit: { return ret; }
}
if ((ret = functional_tests_get_atkeys_path(atsign, strlen(atsign), atkeyspath, atkeyspathsize, &atkeyspathlen)) !=
0) {
atlogger_log("pkam_authenticate main", ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atkeys_sharedwith path: %d\n",
ret);
return ret;
}

static int test_1_pkam_with_null_options() {
int ret = 1;
ret += test1_pkam_no_options();
ret += test2_pkam_with_options();

const char *tag = "test_1_pkam_with_null_options";
return ret;
}

static int test1_pkam_no_options() {
const char *tag = "test1_pkam_no_options";
int ret = 0;

atclient_atkeys_file atkeys_file;
atclient_atkeys_file_init(&atkeys_file);
Expand All @@ -43,76 +55,64 @@ static int test_1_pkam_with_null_options() {
atclient atclient;
atclient_init(&atclient);

if ((ret = functional_tests_set_up_atkeys(&atkeys, ATSIGN)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to get atkeys_sharedwith path: %d\n", ret);
goto exit;
if ((ret = atclient_atkeys_file_from_path(&atkeys_file, atkeyspath)) != 0) {
return ret;
}
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_file_from_string: %d\n", ret);

if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, NULL)) != 0) {
if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) {
return ret;
}
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret);

if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n");
goto exit;
return ret;
} else {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n");
}

ret = 0;
exit: { return ret; }
return ret;
}

static int test_2_pkam_with_options() {
int ret = 1;


const char *tag = "test_2_pkam_with_options";
static int test2_pkam_with_options() {
const char *tag = "test2_pkam_with_options";
int ret = 0;

atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "Begin test_2_pkam_with_options\n");
atclient_atkeys_file atkeys_file;
atclient_atkeys_file_init(&atkeys_file);

atclient_atkeys atkeys;
atclient_atkeys_init(&atkeys);

atclient atclient;
atclient_init(&atclient);

atclient_authenticate_options options;
atclient_authenticate_options_init(&options);

char *atserver_host = NULL;
int atserver_port = 0;

if ((ret = functional_tests_set_up_atkeys(&atkeys, ATSIGN)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to populate atkeys_sharedwith from path: %d\n", ret);
goto exit;
if ((ret = atclient_atkeys_file_from_path(&atkeys_file, atkeyspath)) != 0) {
return ret;
}
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_file_from_string: %d\n", ret);

if ((ret = atclient_utils_find_atserver_address(ATDIRECTORY_HOST, ATDIRECTORY_PORT, ATSIGN, &atserver_host,
&atserver_port)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret);
goto exit;
if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) {
return ret;
}
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret);

if ((ret = atclient_authenticate_options_set_atserver_host(&options, atserver_host)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_authenticate_options_set_at_directory_host: %d\n",
ret);
goto exit;
}
atclient_authenticate_options options;
atclient_authenticate_options_init(&options);

if ((ret = atclient_authenticate_options_set_atserver_port(&options, atserver_port)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_authenticate_options_set_at_directory_port: %d\n",
ret);
goto exit;
if ((ret = atclient_utils_find_atserver_address(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST,
ATCLIENT_ATDIRECTORY_PRODUCTION_PORT, atsign, &options.atserver_host,
&options.atserver_port)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret);
return ret;
}

if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, &options) != 0)) {
if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n");
goto exit;
return ret;
} else {
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_DEBUG, "Authenticated\n");
}

ret = 0;

exit: {
atclient_authenticate_options_free(&options);
atclient_atkeys_free(&atkeys);
atclient_free(&atclient);
atlogger_log(tag, ATLOGGER_LOGGING_LEVEL_INFO, "End test_2_pkam_with_options: %d\n", ret);
return ret; }
return ret;
}

0 comments on commit 54c6264

Please sign in to comment.