Skip to content

Commit

Permalink
Merge pull request #439 from atsign-foundation/xc-32bit-support
Browse files Browse the repository at this point in the history
fix: use sized types for more consistent cross-platform support
  • Loading branch information
XavierChanth authored Nov 4, 2024
2 parents 396d5ac + ad30146 commit 0438b3c
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 117 deletions.
3 changes: 1 addition & 2 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CompileFlags:
# Use gcc in development for better diagnostics
# & Ensure that compilation doesn't fail due to warnings
Compiler: gcc
Add: [-Wno-error]
Add: [-Wno-error, -xc, -std=c99]

Index:
# Enable background indexing for better symbol information
Expand All @@ -15,4 +15,3 @@ Diagnostics:
# Avoid running slow clang-tidy checks
ClangTidy:
FastCheckFilter: Loose

22 changes: 11 additions & 11 deletions packages/atchops/include/atchops/mbedtls.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#ifndef ATCHOPS_MBEDTLS_H
#define ATCHOPS_MBEDTLS_H

#include <mbedtls/aes.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/md.h>
#include <mbedtls/base64.h>
#include <mbedtls/asn1.h>
#include <mbedtls/md5.h>
#include <mbedtls/rsa.h>
#include <mbedtls/ssl.h>
#include <mbedtls/net_sockets.h>
#include <mbedtls/x509_crt.h>
#include <mbedtls/aes.h> // IWYU pragma: export
#include <mbedtls/asn1.h> // IWYU pragma: export
#include <mbedtls/base64.h> // IWYU pragma: export
#include <mbedtls/ctr_drbg.h> // IWYU pragma: export
#include <mbedtls/entropy.h> // IWYU pragma: export
#include <mbedtls/md.h> // IWYU pragma: export
#include <mbedtls/md5.h> // IWYU pragma: export
#include <mbedtls/net_sockets.h> // IWYU pragma: export
#include <mbedtls/rsa.h> // IWYU pragma: export
#include <mbedtls/ssl.h> // IWYU pragma: export
#include <mbedtls/x509_crt.h> // IWYU pragma: export

extern const mbedtls_md_type_t atchops_mbedtls_md_map[];

Expand Down
5 changes: 3 additions & 2 deletions packages/atchops/src/rsa_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ int atchops_rsa_key_generate_base64(unsigned char **public_key_base64_output,
size_t private_key_base64_pkcs8_len = 0;

// 8b. Encode the PKCS#8 formatted private key
if ((ret = atchops_base64_encode(private_key_pkcs8, 26 + private_key_non_base64_len, private_key_pkcs8_base64,
private_key_base64_pkcs8_size, &private_key_base64_pkcs8_len)) != 0) {
if ((ret = atchops_base64_encode(private_key_pkcs8, 26 + private_key_non_base64_len,
(unsigned char *)private_key_pkcs8_base64, private_key_base64_pkcs8_size,
&private_key_base64_pkcs8_len)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to encode private key\n");
goto exit;
}
Expand Down
15 changes: 13 additions & 2 deletions packages/atclient/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## v0.0.1
## 0.3.0

- **Breaking changes** to `atclient_atkey_metadata` and `atclient_notify_params`
- `long` and `int` type changed to `int64_t` (metadata: `ttb`, `ttr`, `ttl`; notify: `latest_n`)
- `unsigned long` type changed to `uint64_t` (notify: `notification_expiry`)

## 0.2.0

- New release to use MbedTLS 3.6.1 to resolve a bug when building NoPorts on arm64 with musl libc

## 0.1.0

- Initial MVP release

- Initial work in progress
1 change: 1 addition & 0 deletions packages/atclient/include/atclient/atkeys.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ATCLIENT_ATKEYS_H
#define ATCLIENT_ATKEYS_H

#include "atchops/rsa_key.h"
#include "atclient/atkeys_file.h"
#include <atchops/rsa.h>
#include <stddef.h>
Expand Down
4 changes: 2 additions & 2 deletions packages/atclient/include/atclient/cjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define ATCLIENT_CJSON_H

#if defined(CONFIG_IDF_TARGET_ESP32)
#include <cjson.h>
#include <cjson.h> // IWYU pragma: export
#else
#include <cJSON.h>
#include <cJSON.h> // IWYU pragma: export
#endif

#endif
26 changes: 13 additions & 13 deletions packages/atclient/include/atclient/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ 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 is_public : 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:'
// 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.

Expand All @@ -144,15 +144,15 @@ typedef struct atclient_atkey_metadata {
// Example ttl=86400 means the atkey will live for a day.
// This field is read from protocol string and and set by the developer.
// This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr)
long ttl;
uint64_t ttl;

// Time to birth in milliseconds
// Represents the amount of time it takes for atkey to exist.
// Example ttb=100 means the atkey will take 100 milliseconds to exist from the point the protocol command was sent
// and received by the atServer
// This field is read from protocol string and and set by the developer.
// This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr)
long ttb;
uint64_t ttb;

// Time to refresh in milliseconds
// Represents the amount of time the cached shared atkey will refresh and update to the latest data stored by the
Expand All @@ -161,7 +161,7 @@ typedef struct atclient_atkey_metadata {
// a ttr is not applicable to this type of atkey (because it may be a selfkey), which has the same effect as 0.
// This field is read from protocol string and and set by the developer.
// This field is written to protocol string by the SDK. (See atclient_atkey_metadata_to_protocolstr)
long ttr;
uint64_t ttr;

// Cascade Delete
// ccd=1 means this cached keys will be deleted upon the deletion of the original copy
Expand Down Expand Up @@ -221,11 +221,10 @@ 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 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)
// 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 *enc_key_name;

// The name of the algorithm used to encrypt the value. For data, the default algorithm is 'AES/SIC/PKCS7Padding', for
Expand Down Expand Up @@ -292,6 +291,7 @@ int atclient_atkey_metadata_from_json_str(atclient_atkey_metadata *metadata, con
* @param json the json object to populate from
* @return int 0 on success
*/

int atclient_atkey_metadata_from_cjson_node(atclient_atkey_metadata *metadata, const cJSON *json);

/**
Expand Down Expand Up @@ -387,9 +387,9 @@ bool atclient_atkey_metadata_is_ske_enc_algo_initialized(const atclient_atkey_me

int atclient_atkey_metadata_set_is_public(atclient_atkey_metadata *metadata, const bool is_public);
int atclient_atkey_metadata_set_is_cached(atclient_atkey_metadata *metadata, const bool is_cached);
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_ttl(atclient_atkey_metadata *metadata, const int64_t ttl);
int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const int64_t ttb);
int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const int64_t ttr);
int atclient_atkey_metadata_set_ccd(atclient_atkey_metadata *metadata, const bool ccd);
int atclient_atkey_metadata_set_is_binary(atclient_atkey_metadata *metadata, const bool is_binary);
int atclient_atkey_metadata_set_is_encrypted(atclient_atkey_metadata *metadata, const bool is_encrypted);
Expand Down
26 changes: 15 additions & 11 deletions packages/atclient/include/atclient/notify_params.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef ATCLIENT_NOTIFY_PARAMS_H
#define ATCLIENT_NOTIFY_PARAMS_H

#include "atclient/atclient.h"
#include "atclient/atkey.h"
#include <stdint.h>

#define ATCLIENT_DEFAULT_NOTIFIER "SYSTEM"

Expand Down Expand Up @@ -37,7 +37,7 @@
#define ATCLIENT_NOTIFY_PARAMS_SHARED_ENCRYPTION_KEY_INITIALIZED (VALUE_INITIALIZED << 3)

// default param values for atclient_notify_params
#define ATCLIENT_NOTIFY_PARAMS_DEFAULT_NOTIFICATION_EXPIRY 24 * 60 * 60 * 1000 //24 Hours in Milliseconds
#define ATCLIENT_NOTIFY_PARAMS_DEFAULT_NOTIFICATION_EXPIRY 24 * 60 * 60 * 1000 // 24 Hours in Milliseconds
#define ATCLIENT_NOTIFY_PARAMS_DEFAULT_LATEST_N 1
#define ATCLIENT_NOTIFY_PARAMS_DEFAULT_SHOULD_ENCRYPT true
#define ATCLIENT_NOTIFY_PARAMS_DEFAULT_OPERATION ATCLIENT_NOTIFY_OPERATION_NONE
Expand Down Expand Up @@ -100,9 +100,9 @@ typedef struct atclient_notify_params {
enum atclient_notify_message_type message_type;
enum atclient_notify_priority priority;
enum atclient_notify_strategy strategy;
int latest_n;
int64_t latest_n;
char *notifier;
unsigned long notification_expiry;
uint64_t notification_expiry;
unsigned char *shared_encryption_key;

uint8_t _initialized_fields[2];
Expand Down Expand Up @@ -135,20 +135,24 @@ void atclient_notify_params_set_strategy_initialized(atclient_notify_params *par
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);
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);
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);
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_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_latest_n(atclient_notify_params *params, const int64_t 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_notification_expiry(atclient_notify_params *params, const uint64_t notification_expiry);
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);
Expand All @@ -163,4 +167,4 @@ 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
#endif // ATCLIENT_NOTIFY_PARAMS_H
11 changes: 10 additions & 1 deletion packages/atclient/include/atclient/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

/**
* @brief trims leading/trailing whitespace/newline
Expand All @@ -15,7 +16,7 @@
* @return int 0 on success, non-zero on failure
*/
int atclient_string_utils_trim_whitespace(const char *string, const size_t stringlen, char *out, const size_t out_size,
size_t *out_len);
size_t *out_len);

/**
* @brief check if string starts with prefix
Expand Down Expand Up @@ -73,4 +74,12 @@ int atclient_string_utils_atsign_without_at(const char *original_atsign, char **
*/
int atclient_string_utils_long_strlen(long n);

/**
* @brief get the length of an int64_t if it were converted to a string
*
* @param n the int64_t to check the length of
* @return int the string length
*/
int atclient_string_utils_int64_strlen(int64_t n);

#endif
2 changes: 1 addition & 1 deletion packages/atclient/include/atclient/version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef ATCLIENT_VERSION_H
#define ATCLIENT_VERSION_H

#define ATCLIENT_ATSDK_VERSION "0.0.1"
#define ATCLIENT_ATSDK_VERSION "0.3.0"

#endif
33 changes: 17 additions & 16 deletions packages/atclient/src/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "atlogger/atlogger.h"
#include "cJSON.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -107,9 +108,9 @@ static int set_created_at(atclient_atkey_metadata *metadata, const char *created
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_ttl(atclient_atkey_metadata *metadata, const int64_t ttl);
static void set_ttb(atclient_atkey_metadata *metadata, const int64_t ttb);
static void set_ttr(atclient_atkey_metadata *metadata, const int64_t ttr);
static void set_ccd(atclient_atkey_metadata *metadata, const bool ccd);
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);
Expand Down Expand Up @@ -920,7 +921,7 @@ size_t atclient_atkey_metadata_protocol_strlen(const atclient_atkey_metadata *me
/*
* 2. Calculate length
*/
long len = 0;
int64_t len = 0;
if (atclient_atkey_metadata_is_ttl_initialized(metadata)) {
len += atclient_atkey_metadata_ttl_strlen(metadata);
}
Expand Down Expand Up @@ -1000,7 +1001,7 @@ size_t atclient_atkey_metadata_ttl_strlen(const atclient_atkey_metadata *metadat
return 0;
}
return strlen(":ttl:") // :ttl:
+ atclient_string_utils_long_strlen(metadata->ttl);
+ atclient_string_utils_int64_strlen(metadata->ttl);
}

size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadata) {
Expand All @@ -1011,7 +1012,7 @@ size_t atclient_atkey_metadata_ttb_strlen(const atclient_atkey_metadata *metadat
return 0;
}
return strlen(":ttb:") // :ttb:
+ atclient_string_utils_long_strlen(metadata->ttb);
+ atclient_string_utils_int64_strlen(metadata->ttb);
}

size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadata) {
Expand All @@ -1022,7 +1023,7 @@ size_t atclient_atkey_metadata_ttr_strlen(const atclient_atkey_metadata *metadat
return 0;
}
return strlen(":ttr:") // :ttr:
+ atclient_string_utils_long_strlen(metadata->ttr);
+ atclient_string_utils_int64_strlen(metadata->ttr);
}

size_t atclient_atkey_metadata_ccd_strlen(const atclient_atkey_metadata *metadata) {
Expand Down Expand Up @@ -1211,17 +1212,17 @@ int atclient_atkey_metadata_to_protocol_str(const atclient_atkey_metadata *metad
memset(*metadata_str, 0, sizeof(char) * metadata_str_size);

if (atclient_atkey_metadata_is_ttl_initialized(metadata)) {
sprintf((*metadata_str) + pos, ":ttl:%ld", metadata->ttl);
sprintf((*metadata_str) + pos, ":ttl:%lld", metadata->ttl);
pos += atclient_atkey_metadata_ttl_strlen(metadata);
}

if (atclient_atkey_metadata_is_ttb_initialized(metadata)) {
sprintf((*metadata_str) + pos, ":ttb:%ld", metadata->ttb);
sprintf((*metadata_str) + pos, ":ttb:%lld", metadata->ttb);
pos += atclient_atkey_metadata_ttb_strlen(metadata);
}

if (atclient_atkey_metadata_is_ttr_initialized(metadata)) {
sprintf((*metadata_str) + pos, ":ttr:%ld", metadata->ttr);
sprintf((*metadata_str) + pos, ":ttr:%lld", metadata->ttr);
pos += atclient_atkey_metadata_ttr_strlen(metadata);
}

Expand Down Expand Up @@ -1453,23 +1454,23 @@ int atclient_atkey_metadata_set_is_cached(atclient_atkey_metadata *metadata, con
return 0;
}

int atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const long ttl) {
int atclient_atkey_metadata_set_ttl(atclient_atkey_metadata *metadata, const int64_t ttl) {
if (is_ttl_initialized(metadata)) {
unset_ttl(metadata);
}
set_ttl(metadata, ttl);
return 0;
}

int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const long ttb) {
int atclient_atkey_metadata_set_ttb(atclient_atkey_metadata *metadata, const int64_t ttb) {
if (is_ttb_initialized(metadata)) {
unset_ttb(metadata);
}
set_ttb(metadata, ttb);
return 0;
}

int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const long ttr) {
int atclient_atkey_metadata_set_ttr(atclient_atkey_metadata *metadata, const int64_t ttr) {
if (is_ttr_initialized(metadata)) {
unset_ttr(metadata);
}
Expand Down Expand Up @@ -2438,17 +2439,17 @@ static void set_is_cached(atclient_atkey_metadata *metadata, const bool is_cache
set_is_is_cached_initialized(metadata, true);
}

static void set_ttl(atclient_atkey_metadata *metadata, const long ttl) {
static void set_ttl(atclient_atkey_metadata *metadata, const int64_t ttl) {
metadata->ttl = ttl;
set_is_ttl_initialized(metadata, true);
}

static void set_ttb(atclient_atkey_metadata *metadata, const long ttb) {
static void set_ttb(atclient_atkey_metadata *metadata, const int64_t ttb) {
metadata->ttb = ttb;
set_is_ttb_initialized(metadata, true);
}

static void set_ttr(atclient_atkey_metadata *metadata, const long ttr) {
static void set_ttr(atclient_atkey_metadata *metadata, const int64_t ttr) {
metadata->ttr = ttr;
set_is_ttr_initialized(metadata, true);
}
Expand Down
Loading

0 comments on commit 0438b3c

Please sign in to comment.