diff --git a/README.md b/README.md index 6c13db72..ac25df45 100644 --- a/README.md +++ b/README.md @@ -8,124 +8,19 @@ ## Packages -- `atchops` stands for cryptographic and hashing operations catered for the atProtocol, uses [MbedTLS crypto](https://github.com/Mbed-TLS/mbedtls) as a dependency. -- `atclient` is the core dependency for anything Atsign technology related. atclient depends on [atchops](./packages/atchops/README.md) and [MbedTLS](https://github.com/Mbed-TLS/mbedtls) -- `repl` is a demo application using atclient +- [atchops](./packages/atchops/README.md) stands for cryptographic and hashing operations catered for the atProtocol, uses [MbedTLS crypto](https://github.com/Mbed-TLS/mbedtls) as a dependency. +- [atclient](./packages/atclient/README.md) is the core dependency for anything Atsign technology related. atclient depends on [atchops](./packages/atchops/README.md) and [MbedTLS](https://github.com/Mbed-TLS/mbedtls) +- [atclient_espidf](./packages/atclient_espidf/README.md) -## Building Source +## Examples -To build the source code you will need to have [CMake](https://cmake.org/) installed. - -Most of the following steps will work with `atchops` and `atclient`: - -- [Installing on Linux/MacOS](#installing-on-linuxmacos) -- [Running Tests on Linux/MacOS](#running-tests-on-linuxmacos) -- [Installing on Windows](#installing-on-windows) - -### Installing on Linux/MacOS - -1. Get ahold of the source code either via git clone or from downloading the source from our releases: - -```sh -git clone https://github.com/atsign-foundation/at_c.git -cd at_c/packages/atclient -``` - -2. CMake configure - -```sh -cmake -S . -B build -``` - -Alternatively, if you have installed MbedTLS and/or AtChops from source already, you can avoid fetching it everytime with `ATCLIENT_FETCH_MBEDTLS=OFF` and `ATCLIENT_FETCH_ATCHOPS=OFF` respectively. Doing this drastically reduces the time it takes to configure the project: - -```sh -cmake -S . -B build -DATCLIENT_FETCH_MBEDTLS=OFF -DATCLIENT_FETCH_ATCHOPS=OFF -``` - -3. Install - -```sh -cmake --build build --target install -``` - -4. Building the source code will allow you to use the `atclient` library in your own CMake projects: - -```cmake -find_package(atclient REQUIRED CONFIG) -target_link_libraries(myproj PRIVATE atclient::atclient) -``` - -### Running Tests on Linux/MacOS - -1. Get ahold of the source code either via git clone or from downloading the source from our releases: - -```sh -git clone https://github.com/atsign-foundation/at_c.git -cd at_c/packages/atclient -``` - -2. CMake configure with `-DATCLIENT_BUILD_TESTS=ON` - -```sh -cmake -S . -B build -DATCLIENT_BUILD_TESTS=ON -``` - -3. Build (target is all by default) - -```sh -cmake --build build -``` - -4. Run tests - -```sh -cd build/tests && ctest -V --output-on-failure --timeout 10 -``` - -`--timeout 10` times out tests after 10 seconds - -### Installing on Windows - -Coming Soon! +- [repl](./examples/repl/README.md) is a command line interface for interacting with the atProtocol. Works on Desktop Linux/MacOS. ## Contributing Read [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to properly fork and open a pull request. -When creating - -- [Creating Tests](#creating-tests) -- [Adding New Source Files](#adding-new-source-files) -- [Adding New Include Headers](#adding-new-include-headers) - -### Creating Tests - -If you want to add a test in atclient, simply add a `test_*.c` file in the `tests` directory. CMake will automatically detect it and add it to the test suite. Ensure that the test file is named `test_*.c` or else it will not be detected. - -Ensure the file has a `int main(int argc, char **argv)` function and returns 0 on success and not 0 on failure. - -### Adding New Source Files - -This one is a little more tricky. Adding a new source file to the project requires a few steps: - -Add the source file to the `CMakeLists.txt` file in the `src` directory. This is so that CMake knows to compile the file. - -Example: - -```cmake -target_sources(atclient PRIVATE - ... - ${CMAKE_CURRENT_LIST_DIR}/src/folder/new_file.c - ... -) -``` - -### Adding New Include Headers - -Simply add the header inside of the `include/` directory. CMake will automatically detect it and add it to the include path. - -If it is added in a subdirectory (like `include/atclient/`), then the include path will be `atclient/` (e.g. `#include `) +When creating source files, include headers, or tests to certain packages, please follow the documentation in their according README files. ## Maintainers diff --git a/examples/atclient_esp32/CMakeLists.txt b/examples/atclient_esp32/CMakeLists.txt deleted file mode 100644 index e98bb22e..00000000 --- a/examples/atclient_esp32/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -cmake_minimum_required(VERSION 3.19) - -set(EXTRA_COMPONENT_DIRS - ${CMAKE_SOURCE_DIR}/../../packages/atclient - ${CMAKE_SOURCE_DIR}/../../packages/atchops -) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - -project(atclient_esp32) \ No newline at end of file diff --git a/examples/atclient_esp32/main/main.c b/examples/atclient_esp32/main/main.c deleted file mode 100644 index 7caafa31..00000000 --- a/examples/atclient_esp32/main/main.c +++ /dev/null @@ -1,74 +0,0 @@ - -#include -#include -#include -#include -#include "atclient/connection.h" - -#define ATSIGN "@jeremy_0" - -static void *without_at_symbol(const char *atsign, char *buf) -{ - int i = 0; - while (atsign[i] != '\0') - { - buf[i] = atsign[i + 1]; - i++; - } - buf[i] = '\0'; - return buf; -} - -void app_main(void) -{ - esp_wifi_set_mode(WIFI_MODE_STA); - - // 1. initialize buffer to use throughout program - size_t recvlen = 32768; - unsigned char *recv = malloc(sizeof(unsigned char) * recvlen); - size_t olen = 0; - - // 2. connect to root and find secondary address - - // 2a. establish connection to root - atclient_connection_ctx root_connection; - atclient_connection_init(&root_connection); - atclient_connection_connect(&root_connection, "root.atsign.org", 64); - printf("Connected to root\n"); - - // 2b. send atsign without @ symbol to root - - char *atsign_without_at = malloc(sizeof(char) * 100); - memset(atsign_without_at, 0, 100); - without_at_symbol(ATSIGN, atsign_without_at); - strcat(atsign_without_at, "\r\n"); - size_t atsign_without_atlen = strlen(atsign_without_at); - - printf("Sending to root: \"%s\"\n", atsign_without_at); - atclient_connection_send(&root_connection, recv, recvlen, &olen, (unsigned char *)atsign_without_at, atsign_without_atlen); - printf("Received from root: \"%.*s\"\n", (int)olen, recv); - - // 2c. parse secondary address - const size_t secondary_len = 100; - char *secondary_host = malloc(sizeof(char) * secondary_len); - char *secondary_port = malloc(sizeof(char) * secondary_len); - - int i = 0, c; - while ((c = recv[i]) != ':' && i < olen) - { - secondary_host[i] = c; - i++; - } - secondary_host[i] = '\0'; - i++; - int j = 0; - while ((c = recv[i]) != '\0' && i < olen) - { - secondary_port[j] = c; - i++; - j++; - } - - printf("secondary_host: %s\n", secondary_host); - printf("secondary_port: %s\n", secondary_port); -} \ No newline at end of file diff --git a/examples/atclient_esp32_source/CMakeLists.txt b/examples/atclient_esp32_source/CMakeLists.txt new file mode 100644 index 00000000..33043421 --- /dev/null +++ b/examples/atclient_esp32_source/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.19) + +set(EXTRA_COMPONENT_DIRS + ${CMAKE_SOURCE_DIR}/../../packages/atclient # match this to be the path to the root CMakeLists.txt of atclient package + ${CMAKE_SOURCE_DIR}/../../packages/atchops # match this to be the path to the root CMakeLists.txt of atchops package +) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(atclient_esp32_source) \ No newline at end of file diff --git a/examples/atclient_esp32_source/README.md b/examples/atclient_esp32_source/README.md new file mode 100644 index 00000000..69f5c2bb --- /dev/null +++ b/examples/atclient_esp32_source/README.md @@ -0,0 +1,37 @@ +# atclient_esp32_source + +This example shows you how to use atclient/atchops in your own ESP-IDF project by providing the path to the source code. + +## How to Consume Via Source Code + +In `make/CMakeLists.txt`, be sure to add the atclient and atchops components to the REQUIRES list: + +```cmake +idf_component_register( + SRCS "main.c" + REQUIRES mbedtls atclient atchops +) +``` + +In `./CMakeLists.txt`, add the path to the atclient and atchops source code via the EXTRA_COMPONENT_DIRS variable: + +```cmake +set(EXTRA_COMPONENT_DIRS + ${CMAKE_SOURCE_DIR}/../../packages/atclient # match this to be the path to the root CMakeLists.txt of atclient package + ${CMAKE_SOURCE_DIR}/../../packages/atchops # match this to be the path to the root CMakeLists.txt of atchops package +) +``` + +## Running the Example + +To run the example, you will need the ESP-IDF toolchain installed. See [ESP-IDF's Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) for more information. Ensure that your ESP32 is plugged into your computer with a micro USB data cable. + +Running the example via `get_idf && idf.py build && idf.py flash monitor` will give you something similar to: + +```sh +atchops_base64_encode: 0 +src: Lemonade! +dst: TGVtb25hZGUh +dst bytes: +54 47 56 74 62 32 35 68 5a 47 55 68 +``` \ No newline at end of file diff --git a/examples/atclient_esp32/include/atchops/aes_ctr.h b/examples/atclient_esp32_source/include/atchops/aes_ctr.h similarity index 100% rename from examples/atclient_esp32/include/atchops/aes_ctr.h rename to examples/atclient_esp32_source/include/atchops/aes_ctr.h diff --git a/examples/atclient_esp32/include/atchops/base64.h b/examples/atclient_esp32_source/include/atchops/base64.h similarity index 100% rename from examples/atclient_esp32/include/atchops/base64.h rename to examples/atclient_esp32_source/include/atchops/base64.h diff --git a/examples/atclient_esp32/include/atchops/rsa.h b/examples/atclient_esp32_source/include/atchops/rsa.h similarity index 100% rename from examples/atclient_esp32/include/atchops/rsa.h rename to examples/atclient_esp32_source/include/atchops/rsa.h diff --git a/examples/atclient_esp32/include/atchops/sha.h b/examples/atclient_esp32_source/include/atchops/sha.h similarity index 100% rename from examples/atclient_esp32/include/atchops/sha.h rename to examples/atclient_esp32_source/include/atchops/sha.h diff --git a/examples/atclient_esp32/include/atclient/at_logger.h b/examples/atclient_esp32_source/include/atclient/at_logger.h similarity index 100% rename from examples/atclient_esp32/include/atclient/at_logger.h rename to examples/atclient_esp32_source/include/atclient/at_logger.h diff --git a/examples/atclient_esp32/include/atclient/atkeys_filereader.h b/examples/atclient_esp32_source/include/atclient/atkeys_filereader.h similarity index 100% rename from examples/atclient_esp32/include/atclient/atkeys_filereader.h rename to examples/atclient_esp32_source/include/atclient/atkeys_filereader.h diff --git a/examples/atclient_esp32/include/atclient/connection.h b/examples/atclient_esp32_source/include/atclient/connection.h similarity index 100% rename from examples/atclient_esp32/include/atclient/connection.h rename to examples/atclient_esp32_source/include/atclient/connection.h diff --git a/examples/atclient_esp32_source/lib/libatchops.a b/examples/atclient_esp32_source/lib/libatchops.a new file mode 100644 index 00000000..d7f89a82 Binary files /dev/null and b/examples/atclient_esp32_source/lib/libatchops.a differ diff --git a/examples/atclient_esp32_source/lib/libatclient.a b/examples/atclient_esp32_source/lib/libatclient.a new file mode 100644 index 00000000..052923dd Binary files /dev/null and b/examples/atclient_esp32_source/lib/libatclient.a differ diff --git a/examples/atclient_esp32_source/main/CMakeLists.txt b/examples/atclient_esp32_source/main/CMakeLists.txt new file mode 100644 index 00000000..9759a0d3 --- /dev/null +++ b/examples/atclient_esp32_source/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register( + SRCS "main.c" + REQUIRES mbedtls atclient atchops +) \ No newline at end of file diff --git a/examples/atclient_esp32_source/main/main.c b/examples/atclient_esp32_source/main/main.c new file mode 100644 index 00000000..da808d31 --- /dev/null +++ b/examples/atclient_esp32_source/main/main.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "atchops/base64.h" + +void app_main(void) +{ + const char *src = "Lemonade!"; + unsigned long srclen = strlen(src); + + const unsigned long dstlen = 2048; + const unsigned char *dst = malloc(sizeof(unsigned char) * dstlen); + memset(dst, 0, dstlen); + unsigned long dstolen = 0; // written length + + int ret = atchops_base64_encode((const unsigned char *) src, srclen, dst, dstlen, &dstolen); + + printf("atchops_base64_encode: %d\n", ret); + + printf("src: %s\n", src); + printf("dst: %.*s\n", (int) dstolen, dst); + printf("dst bytes: \n"); + for(int i = 0; i < dstolen; i++) + { + printf("%02x ", *(dst + i)); + } + printf("\n"); +} \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/CMakeLists.txt b/examples/atclient_esp32_static_components/CMakeLists.txt new file mode 100644 index 00000000..2f7f87a5 --- /dev/null +++ b/examples/atclient_esp32_static_components/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.19) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(atclient_esp32_static_components) \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/README.md b/examples/atclient_esp32_static_components/README.md new file mode 100644 index 00000000..9aa42a1f --- /dev/null +++ b/examples/atclient_esp32_static_components/README.md @@ -0,0 +1,91 @@ +# atclient_esp32_static + +This is an example of how to use the built static libraries from [atclient_espidf](../../packages/atclient_espidf/README.md). + +## Consuming AtClient/AtChops as Static Libraries + +You will need to have built the static libraries from [atclient_espidf](../../packages/atclient_espidf/README.md) before you can consume them in your own project. + +Once you have that, be sure to create the following directories in your project: + +- `components/atclient` +- `components/atchops` + +And in each directory, create a CMakeLists.txt. + +`components/atclient/CMakeLists.txt`: + +```cmake +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atclient ${CMAKE_CURRENT_LIST_DIR}/lib/libatclient.a REQUIRES mbedtls atchops) + +target_include_directories(atclient INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atclient) # add it to the component library +``` + +`components/atchops/CMakeLists.txt`: + +```cmake +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atchops ${CMAKE_CURRENT_LIST_DIR}/lib/libatchops.a REQUIRES mbedtls) + +target_include_directories(atchops INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atchops) # add it to the component library +``` + +Then in your `main/CMakeLists.txt`, be sure to REQUIRE the atclient and atchops components: + +```cmake +idf_component_register( + SRCS "main.c" + REQUIRES atclient atchops +) +``` + +Your root project `./CMakeLists.txt` can remain the same as any regular ESP-IDF root CMakeLists.txt. + +```cmake +cmake_minimum_required(VERSION 3.19) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(atclient_esp32) +``` + +## Running this example + +This example requires the ESP-IDF toolchain to be installed. See [ESP-IDF's Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) for more information. + +Ensure that your ESP32 is plugged into your computer with a micro USB data cable. + +1. Get IDF + +```sh +get_idf +``` + +2. Build + +```sh +idf.py build +``` + +3. Flash and Monitor to your ESP32 + +```sh +idf.py flash monitor +``` + +4. Your output will be similar to the following: + +```sh +atchops_base64_encode: 0 +src: Lemonade! +dst: TGVtb25hZGUh +dst bytes: +54 47 56 74 62 32 35 68 5a 47 55 68 +``` \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/components/atchops/CMakeLists.txt b/examples/atclient_esp32_static_components/components/atchops/CMakeLists.txt new file mode 100644 index 00000000..c8b1c2dd --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atchops/CMakeLists.txt @@ -0,0 +1,7 @@ +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atchops ${CMAKE_CURRENT_LIST_DIR}/lib/libatchops.a REQUIRES mbedtls) + +target_include_directories(atchops INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atchops) # add it to the component library \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/components/atchops/include/atchops/aes_ctr.h b/examples/atclient_esp32_static_components/components/atchops/include/atchops/aes_ctr.h new file mode 100644 index 00000000..ca9eb607 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atchops/include/atchops/aes_ctr.h @@ -0,0 +1,31 @@ +#pragma once + +typedef enum AESKeySize { + AES_128 = 128, // not tested + AES_192 = 192, // not tested + AES_256 = 256, +} AESKeySize; + +int atchops_aes_ctr_encrypt( + const char *keybase64, + const unsigned long keybase64len, + const AESKeySize keybits, + const unsigned char *iv, + const unsigned long ivlen, + const unsigned char *plaintext, + const unsigned long plaintextlen, + unsigned char *ciphertextbase64, + const unsigned long ciphertextbase64len, + unsigned long *ciphertextbase64olen); + +int atchops_aes_ctr_decrypt( + const char *keybase64, + const unsigned long keybase64len, + const AESKeySize keybits, + const unsigned char *iv, + const unsigned long ivlen, + const unsigned char *ciphertextbase64, + const unsigned long ciphertextbase64len, + unsigned char *plaintext, + const unsigned long plaintextlen, + unsigned long *plaintextolen); diff --git a/examples/atclient_esp32_static_components/components/atchops/include/atchops/base64.h b/examples/atclient_esp32_static_components/components/atchops/include/atchops/base64.h new file mode 100644 index 00000000..d1a9c227 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atchops/include/atchops/base64.h @@ -0,0 +1,25 @@ +#pragma once + +/** + * @brief Base64 encode some bytes + * + * @param src src bytes that you want to encode + * @param srclen the length of the src bytes + * @param dst the buffer where the base64 encoded result will be + * @param dstlen the buffer length + * @param writtenlen the length of the result after operation + * @return int 0 on success + */ +int atchops_base64_encode(const unsigned char *src, const unsigned long srclen, unsigned char *dst, unsigned long dstlen, unsigned long *writtenlen); + +/** + * @brief Base64 decode some bytes + * + * @param src src bytes that you want to decode + * @param srclen the length of the src bytes + * @param dst the buffer where the base64 decoded result will be + * @param dstlen the buffer length + * @param writtenlen the length of the result after operation + * @return int 0 on success + */ +int atchops_base64_decode(const unsigned char *src, const unsigned long srclen, unsigned char *dst, unsigned long dstlen, unsigned long *writtenlen); diff --git a/examples/atclient_esp32_static_components/components/atchops/include/atchops/rsa.h b/examples/atclient_esp32_static_components/components/atchops/include/atchops/rsa.h new file mode 100644 index 00000000..bffb64b4 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atchops/include/atchops/rsa.h @@ -0,0 +1,84 @@ +#pragma once + +#include "sha.h" + +typedef struct rsa_param +{ + unsigned long len; // length of the number in bytes + unsigned char *value; // hex byte array of the number +} rsa_param; + +typedef struct atchops_rsa_publickey +{ + rsa_param n; // modulus + rsa_param e; // public exponent +} atchops_rsa_publickey; + +typedef struct atchops_rsa_privatekey +{ + rsa_param n; // modulus + rsa_param e; // public exponent + rsa_param d; // private exponent + rsa_param p; // prime 1 + rsa_param q; // prime 2 +} atchops_rsa_privatekey; + +/** + * @brief Populate a public key struct from a base64 string + * + * @param publickeybase64 a base64 string representing an RSA 2048 Public Key + * @param publickeybase64len the length of the base64 string + * @param publickeystruct the public key struct to populate + * @return int 0 on success + */ +int atchops_rsa_populate_publickey(const char *publickeybase64, const unsigned long publickeybase64len, atchops_rsa_publickey *publickeystruct); + +/** + * @brief Populate a private key struct from a base64 string + * + * @param privatekeybase64 the base64 string representing an RSA 2048 Private Key + * @param privatekeybase64len the length of the base64 string + * @param privatekeystruct the private key struct to populate + * @return int 0 on success + */ +int atchops_rsa_populate_privatekey(const char *privatekeybase64, const unsigned long privatekeybase64len, atchops_rsa_privatekey *privatekeystruct); + +/** + * @brief Sign a message with an RSA private key + * + * @param privatekeystruct the private key struct to use for signing, see atchops_rsa_populate_privatekey + * @param mdtype the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256 + * @param message the message to sign + * @param messagelen the length of the message, most people use strlen() to find this length + * @param signature the signature buffer to populate + * @param signaturelen the length of the signature buffer + * @param signatureolen the length of the signature buffer after signing + * @return int 0 on success + */ +int atchops_rsa_sign(atchops_rsa_privatekey privatekeystruct, atchops_md_type mdtype, const unsigned char *message, const unsigned long messagelen, unsigned char *signature, const unsigned long signaturelen, unsigned long *signatureolen); + +/** + * @brief Encrypt bytes with an RSA public key + * + * @param publickeystruct the public key struct to use for encryption, see atchops_rsa_populate_publickey + * @param plaintext the plaintext to encrypt + * @param plaintextlen the length of the plaintext, most people use strlen() to find this length + * @param ciphertext the ciphertext buffer to populate + * @param ciphertextlen the length of the ciphertext buffer + * @param ciphertextolen the length of the ciphertext buffer after encryption + * @return int 0 on success + */ +int atchops_rsa_encrypt(atchops_rsa_publickey publickeystruct, const unsigned char *plaintext, const unsigned long plaintextlen, unsigned char *ciphertext, const unsigned long ciphertextlen, unsigned long *ciphertextolen); + +/** + * @brief Decrypt bytes with an RSA private key + * + * @param privatekeystruct the private key struct to use for decryption, see atchops_rsa_populate_privatekey + * @param ciphertextbase64 the ciphertext to decrypt, base64 encoded + * @param ciphertextbase64len the length of the ciphertext, most people use strlen() to find this length + * @param plaintext the plaintext buffer to populate + * @param plaintextlen the length of the plaintext buffer + * @param plaintextolen the length of the plaintext buffer after decryption + * @return int 0 on success + */ +int atchops_rsa_decrypt(atchops_rsa_privatekey privatekeystruct, const unsigned char *ciphertextbase64, const unsigned long ciphertextbase64len, unsigned char *plaintext, const unsigned long plaintextlen, unsigned long *plaintextolen); diff --git a/examples/atclient_esp32_static_components/components/atchops/include/atchops/sha.h b/examples/atclient_esp32_static_components/components/atchops/include/atchops/sha.h new file mode 100644 index 00000000..786522e5 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atchops/include/atchops/sha.h @@ -0,0 +1,14 @@ +#pragma once + +typedef enum { + ATCHOPS_MD_NONE=0, /**< None. */ + ATCHOPS_MD_MD5, /**< The MD5 message digest. */ + ATCHOPS_MD_SHA1, /**< The SHA-1 message digest. */ + ATCHOPS_MD_SHA224, /**< The SHA-224 message digest. */ + ATCHOPS_MD_SHA256, /**< The SHA-256 message digest. */ + ATCHOPS_MD_SHA384, /**< The SHA-384 message digest. */ + ATCHOPS_MD_SHA512, /**< The SHA-512 message digest. */ + ATCHOPS_MD_RIPEMD160, +} atchops_md_type; + +int atchops_sha_hash(atchops_md_type mdtype, const unsigned char *input, const unsigned long inputlen, unsigned char *output, unsigned long outputlen, unsigned long *outputolen); diff --git a/examples/atclient_esp32/lib/libatchops.a b/examples/atclient_esp32_static_components/components/atchops/lib/libatchops.a similarity index 100% rename from examples/atclient_esp32/lib/libatchops.a rename to examples/atclient_esp32_static_components/components/atchops/lib/libatchops.a diff --git a/examples/atclient_esp32_static_components/components/atclient/CMakeLists.txt b/examples/atclient_esp32_static_components/components/atclient/CMakeLists.txt new file mode 100644 index 00000000..aa5eea58 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atclient/CMakeLists.txt @@ -0,0 +1,7 @@ +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atclient ${CMAKE_CURRENT_LIST_DIR}/lib/libatclient.a REQUIRES mbedtls atchops) + +target_include_directories(atclient INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atclient) # add it to the component library \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/components/atclient/include/atclient/at_logger.h b/examples/atclient_esp32_static_components/components/atclient/include/atclient/at_logger.h new file mode 100644 index 00000000..c6be04fc --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atclient/include/atclient/at_logger.h @@ -0,0 +1,9 @@ +#ifndef ATLOGGER_H +#define ATLOGGER_H + +#include + +int atlogger_log(const char *title, const char *message); +int atlogger_logx(const char *title, const unsigned char *bytes, size_t byteslen); + +#endif // ATLOGGER_H diff --git a/examples/atclient_esp32_static_components/components/atclient/include/atclient/atkeys_filereader.h b/examples/atclient_esp32_static_components/components/atclient/include/atclient/atkeys_filereader.h new file mode 100644 index 00000000..e02fba54 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atclient/include/atclient/atkeys_filereader.h @@ -0,0 +1,59 @@ +#pragma once + +#define TOKEN_AES_PKAM_PUBLIC_KEY "aesPkamPublicKey" +#define TOKEN_AES_PKAM_PUBLIC_KEY_LEN 16 + +#define TOKEN_AES_PKAM_PRIVATE_KEY "aesPkamPrivateKey" +#define TOKEN_AES_PKAM_PRIVATE_KEY_LEN 17 + +#define TOKEN_AES_ENCRYPT_PUBLIC_KEY "aesEncryptPublicKey" +#define TOKEN_AES_ENCRYPT_PUBLIC_KEY_LEN 19 + +#define TOKEN_AES_ENCRYPT_PRIVATE_KEY "aesEncryptPrivateKey" +#define TOKEN_AES_ENCRYPT_PRIVATE_KEY_LEN 20 + +#define TOKEN_SELF_ENCRYPTION_KEY "selfEncryptionKey" +#define TOKEN_SELF_ENCRYPTION_KEY_LEN 17 + +typedef struct atclient_atkeysfile_entry{ + size_t len; + char *key; +} atclient_atkeysfile_entry; + +typedef struct atclient_atkeysfile { + atclient_atkeysfile_entry *aes_pkam_public_key; + atclient_atkeysfile_entry *aes_pkam_private_key; + atclient_atkeysfile_entry *aes_encrypt_public_key; + atclient_atkeysfile_entry *aes_encrypt_private_key; + atclient_atkeysfile_entry *self_encryption_key; +} atclient_atkeysfile; + +void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile); +int atclient_atkeysfile_read(const char *path, atclient_atkeysfile *atkeysfile); +int atclient_atkeysfile_write(const char *path, const char *atsign, atclient_atkeysfile *atkeysfile); +void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile); + +/** + * Usage example + * atclient_atkeysfile atkeysfile; + * atclient_atkeysfile_init(&atkeysfile); + * printf("done init...\n") + * + ret = atclient_atkeysfile_read(path, &atkeysfile); + if (ret != 0) + { + goto exit; + } + + printf("done read...\n"); + printf("aes_pkam_public_key: %s\n", atkeysfile.aes_pkam_public_key->key); + printf("aes_pkam_private_key: %s\n", atkeysfile.aes_pkam_private_key->key); + printf("aes_encrypt_public_key: %s\n", atkeysfile.aes_encrypt_public_key->key); + printf("aes_encrypt_private_key: %s\n", atkeysfile.aes_encrypt_private_key->key); + printf("self_encryption_key: %s\n", atkeysfile.self_encryption_key->key); + + printf("writing...\n"); + + ret = atclient_atkeysfile_write("/Users/jeremytubongbanua/.atsign/temp/@smoothalligator_key.atKeys", ATSIGN, &atkeysfile); + * + */ \ No newline at end of file diff --git a/examples/atclient_esp32_static_components/components/atclient/include/atclient/connection.h b/examples/atclient_esp32_static_components/components/atclient/include/atclient/connection.h new file mode 100644 index 00000000..4d0d2351 --- /dev/null +++ b/examples/atclient_esp32_static_components/components/atclient/include/atclient/connection.h @@ -0,0 +1,59 @@ +#pragma once + +#include + +#define HOST "root.atsign.org" +#define PORT 64 + +// #define HOST "245b44d4-a4bd-5f33-b077-c559f956486a.swarm0001.atsign.zone" +// #define PORT 1722 + +#define ROOT_CERT \ + "-----BEGIN CERTIFICATE-----\n" \ + "MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw\n" \ + "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ + "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw\n" \ + "WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\n" \ + "RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \ + "AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP\n" \ + "R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx\n" \ + "sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm\n" \ + "NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg\n" \ + "Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG\n" \ + "/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC\n" \ + "AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB\n" \ + "Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA\n" \ + "FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw\n" \ + "AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw\n" \ + "Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB\n" \ + "gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W\n" \ + "PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl\n" \ + "ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz\n" \ + "CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm\n" \ + "lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4\n" \ + "avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2\n" \ + "yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O\n" \ + "yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids\n" \ + "hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+\n" \ + "HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv\n" \ + "MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX\n" \ + "nLRbwHOoq7hHwg==\n" \ + "-----END CERTIFICATE-----\n" + +typedef struct atclient_connection_ctx { + char *host; // assume null terminated, example: "root.atsign.org" + int port; // example: 64 + char *cert_pem; // assume null terminated, example: "-----BEGIN CERTIFICATE-----\nMIIF..." + void *server_fd; + void *ssl; + void *conf; + void *cacert; + void *entropy; + void *ctr_drbg; + void *saved_session; +} atclient_connection_ctx; + +void atclient_connection_init(atclient_connection_ctx *ctx); +int atclient_connection_connect(atclient_connection_ctx *ctx, const char *host, const int port); +int atclient_connection_send(atclient_connection_ctx *ctx, unsigned char *recv, const size_t recvlen, size_t *olen, const unsigned char *src, const size_t srclen); +void atclient_connection_free(atclient_connection_ctx *ctx); diff --git a/examples/atclient_esp32/lib/libatclient.a b/examples/atclient_esp32_static_components/components/atclient/lib/libatclient.a similarity index 95% rename from examples/atclient_esp32/lib/libatclient.a rename to examples/atclient_esp32_static_components/components/atclient/lib/libatclient.a index baef866a..4cf4208d 100644 Binary files a/examples/atclient_esp32/lib/libatclient.a and b/examples/atclient_esp32_static_components/components/atclient/lib/libatclient.a differ diff --git a/examples/atclient_esp32/main/CMakeLists.txt b/examples/atclient_esp32_static_components/main/CMakeLists.txt similarity index 58% rename from examples/atclient_esp32/main/CMakeLists.txt rename to examples/atclient_esp32_static_components/main/CMakeLists.txt index 709b75c8..cb56fe94 100644 --- a/examples/atclient_esp32/main/CMakeLists.txt +++ b/examples/atclient_esp32_static_components/main/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( SRCS "main.c" - REQUIRES atclient esp_wifi + REQUIRES atclient atchops ) diff --git a/examples/atclient_esp32_static_components/main/main.c b/examples/atclient_esp32_static_components/main/main.c new file mode 100644 index 00000000..da808d31 --- /dev/null +++ b/examples/atclient_esp32_static_components/main/main.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "atchops/base64.h" + +void app_main(void) +{ + const char *src = "Lemonade!"; + unsigned long srclen = strlen(src); + + const unsigned long dstlen = 2048; + const unsigned char *dst = malloc(sizeof(unsigned char) * dstlen); + memset(dst, 0, dstlen); + unsigned long dstolen = 0; // written length + + int ret = atchops_base64_encode((const unsigned char *) src, srclen, dst, dstlen, &dstolen); + + printf("atchops_base64_encode: %d\n", ret); + + printf("src: %s\n", src); + printf("dst: %.*s\n", (int) dstolen, dst); + printf("dst bytes: \n"); + for(int i = 0; i < dstolen; i++) + { + printf("%02x ", *(dst + i)); + } + printf("\n"); +} \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/CMakeLists.txt b/examples/atclient_esp32_static_no_components/CMakeLists.txt new file mode 100644 index 00000000..02baa343 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.19) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(atclient_esp32_static_no_components) \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/main/CMakeLists.txt b/examples/atclient_esp32_static_no_components/main/CMakeLists.txt new file mode 100644 index 00000000..5e990671 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/CMakeLists.txt @@ -0,0 +1,11 @@ +idf_component_register( + SRCS "main.c" + INCLUDE_DIRS "atchops/include" "atclient/include" "." + REQUIRES mbedtls +) + +add_prebuilt_library(atchops "${CMAKE_CURRENT_LIST_DIR}/atchops/lib/libatchops.a" REQUIRES mbedtls) +target_link_libraries(${COMPONENT_LIB} INTERFACE atchops) + +add_prebuilt_library(atclient "${CMAKE_CURRENT_LIST_DIR}/atclient/lib/libatclient.a" REQUIRES mbedtls atchops) +target_link_libraries(${COMPONENT_LIB} INTERFACE atclient) \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/main/atchops/CMakeLists.txt b/examples/atclient_esp32_static_no_components/main/atchops/CMakeLists.txt new file mode 100644 index 00000000..c8b1c2dd --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atchops/CMakeLists.txt @@ -0,0 +1,7 @@ +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atchops ${CMAKE_CURRENT_LIST_DIR}/lib/libatchops.a REQUIRES mbedtls) + +target_include_directories(atchops INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atchops) # add it to the component library \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/aes_ctr.h b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/aes_ctr.h new file mode 100644 index 00000000..ca9eb607 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/aes_ctr.h @@ -0,0 +1,31 @@ +#pragma once + +typedef enum AESKeySize { + AES_128 = 128, // not tested + AES_192 = 192, // not tested + AES_256 = 256, +} AESKeySize; + +int atchops_aes_ctr_encrypt( + const char *keybase64, + const unsigned long keybase64len, + const AESKeySize keybits, + const unsigned char *iv, + const unsigned long ivlen, + const unsigned char *plaintext, + const unsigned long plaintextlen, + unsigned char *ciphertextbase64, + const unsigned long ciphertextbase64len, + unsigned long *ciphertextbase64olen); + +int atchops_aes_ctr_decrypt( + const char *keybase64, + const unsigned long keybase64len, + const AESKeySize keybits, + const unsigned char *iv, + const unsigned long ivlen, + const unsigned char *ciphertextbase64, + const unsigned long ciphertextbase64len, + unsigned char *plaintext, + const unsigned long plaintextlen, + unsigned long *plaintextolen); diff --git a/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/base64.h b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/base64.h new file mode 100644 index 00000000..d1a9c227 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/base64.h @@ -0,0 +1,25 @@ +#pragma once + +/** + * @brief Base64 encode some bytes + * + * @param src src bytes that you want to encode + * @param srclen the length of the src bytes + * @param dst the buffer where the base64 encoded result will be + * @param dstlen the buffer length + * @param writtenlen the length of the result after operation + * @return int 0 on success + */ +int atchops_base64_encode(const unsigned char *src, const unsigned long srclen, unsigned char *dst, unsigned long dstlen, unsigned long *writtenlen); + +/** + * @brief Base64 decode some bytes + * + * @param src src bytes that you want to decode + * @param srclen the length of the src bytes + * @param dst the buffer where the base64 decoded result will be + * @param dstlen the buffer length + * @param writtenlen the length of the result after operation + * @return int 0 on success + */ +int atchops_base64_decode(const unsigned char *src, const unsigned long srclen, unsigned char *dst, unsigned long dstlen, unsigned long *writtenlen); diff --git a/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/rsa.h b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/rsa.h new file mode 100644 index 00000000..bffb64b4 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/rsa.h @@ -0,0 +1,84 @@ +#pragma once + +#include "sha.h" + +typedef struct rsa_param +{ + unsigned long len; // length of the number in bytes + unsigned char *value; // hex byte array of the number +} rsa_param; + +typedef struct atchops_rsa_publickey +{ + rsa_param n; // modulus + rsa_param e; // public exponent +} atchops_rsa_publickey; + +typedef struct atchops_rsa_privatekey +{ + rsa_param n; // modulus + rsa_param e; // public exponent + rsa_param d; // private exponent + rsa_param p; // prime 1 + rsa_param q; // prime 2 +} atchops_rsa_privatekey; + +/** + * @brief Populate a public key struct from a base64 string + * + * @param publickeybase64 a base64 string representing an RSA 2048 Public Key + * @param publickeybase64len the length of the base64 string + * @param publickeystruct the public key struct to populate + * @return int 0 on success + */ +int atchops_rsa_populate_publickey(const char *publickeybase64, const unsigned long publickeybase64len, atchops_rsa_publickey *publickeystruct); + +/** + * @brief Populate a private key struct from a base64 string + * + * @param privatekeybase64 the base64 string representing an RSA 2048 Private Key + * @param privatekeybase64len the length of the base64 string + * @param privatekeystruct the private key struct to populate + * @return int 0 on success + */ +int atchops_rsa_populate_privatekey(const char *privatekeybase64, const unsigned long privatekeybase64len, atchops_rsa_privatekey *privatekeystruct); + +/** + * @brief Sign a message with an RSA private key + * + * @param privatekeystruct the private key struct to use for signing, see atchops_rsa_populate_privatekey + * @param mdtype the hash type to use, see atchops_md_type, e.g. ATCHOPS_MD_SHA256 + * @param message the message to sign + * @param messagelen the length of the message, most people use strlen() to find this length + * @param signature the signature buffer to populate + * @param signaturelen the length of the signature buffer + * @param signatureolen the length of the signature buffer after signing + * @return int 0 on success + */ +int atchops_rsa_sign(atchops_rsa_privatekey privatekeystruct, atchops_md_type mdtype, const unsigned char *message, const unsigned long messagelen, unsigned char *signature, const unsigned long signaturelen, unsigned long *signatureolen); + +/** + * @brief Encrypt bytes with an RSA public key + * + * @param publickeystruct the public key struct to use for encryption, see atchops_rsa_populate_publickey + * @param plaintext the plaintext to encrypt + * @param plaintextlen the length of the plaintext, most people use strlen() to find this length + * @param ciphertext the ciphertext buffer to populate + * @param ciphertextlen the length of the ciphertext buffer + * @param ciphertextolen the length of the ciphertext buffer after encryption + * @return int 0 on success + */ +int atchops_rsa_encrypt(atchops_rsa_publickey publickeystruct, const unsigned char *plaintext, const unsigned long plaintextlen, unsigned char *ciphertext, const unsigned long ciphertextlen, unsigned long *ciphertextolen); + +/** + * @brief Decrypt bytes with an RSA private key + * + * @param privatekeystruct the private key struct to use for decryption, see atchops_rsa_populate_privatekey + * @param ciphertextbase64 the ciphertext to decrypt, base64 encoded + * @param ciphertextbase64len the length of the ciphertext, most people use strlen() to find this length + * @param plaintext the plaintext buffer to populate + * @param plaintextlen the length of the plaintext buffer + * @param plaintextolen the length of the plaintext buffer after decryption + * @return int 0 on success + */ +int atchops_rsa_decrypt(atchops_rsa_privatekey privatekeystruct, const unsigned char *ciphertextbase64, const unsigned long ciphertextbase64len, unsigned char *plaintext, const unsigned long plaintextlen, unsigned long *plaintextolen); diff --git a/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/sha.h b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/sha.h new file mode 100644 index 00000000..786522e5 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atchops/include/atchops/sha.h @@ -0,0 +1,14 @@ +#pragma once + +typedef enum { + ATCHOPS_MD_NONE=0, /**< None. */ + ATCHOPS_MD_MD5, /**< The MD5 message digest. */ + ATCHOPS_MD_SHA1, /**< The SHA-1 message digest. */ + ATCHOPS_MD_SHA224, /**< The SHA-224 message digest. */ + ATCHOPS_MD_SHA256, /**< The SHA-256 message digest. */ + ATCHOPS_MD_SHA384, /**< The SHA-384 message digest. */ + ATCHOPS_MD_SHA512, /**< The SHA-512 message digest. */ + ATCHOPS_MD_RIPEMD160, +} atchops_md_type; + +int atchops_sha_hash(atchops_md_type mdtype, const unsigned char *input, const unsigned long inputlen, unsigned char *output, unsigned long outputlen, unsigned long *outputolen); diff --git a/examples/atclient_esp32_static_no_components/main/atchops/lib/libatchops.a b/examples/atclient_esp32_static_no_components/main/atchops/lib/libatchops.a new file mode 100644 index 00000000..c32da549 Binary files /dev/null and b/examples/atclient_esp32_static_no_components/main/atchops/lib/libatchops.a differ diff --git a/examples/atclient_esp32_static_no_components/main/atclient/CMakeLists.txt b/examples/atclient_esp32_static_no_components/main/atclient/CMakeLists.txt new file mode 100644 index 00000000..05516682 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atclient/CMakeLists.txt @@ -0,0 +1,7 @@ +# register this directory as a component +idf_component_register() + +add_prebuilt_library(atclient ${CMAKE_CURRENT_LIST_DIR}/lib/libatclient.a REQUIRES mbedtls) + +target_include_directories(atclient INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) +target_link_libraries(${COMPONENT_LIB} INTERFACE atclient) # add it to the component library \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/at_logger.h b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/at_logger.h new file mode 100644 index 00000000..c6be04fc --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/at_logger.h @@ -0,0 +1,9 @@ +#ifndef ATLOGGER_H +#define ATLOGGER_H + +#include + +int atlogger_log(const char *title, const char *message); +int atlogger_logx(const char *title, const unsigned char *bytes, size_t byteslen); + +#endif // ATLOGGER_H diff --git a/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/atkeys_filereader.h b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/atkeys_filereader.h new file mode 100644 index 00000000..e02fba54 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/atkeys_filereader.h @@ -0,0 +1,59 @@ +#pragma once + +#define TOKEN_AES_PKAM_PUBLIC_KEY "aesPkamPublicKey" +#define TOKEN_AES_PKAM_PUBLIC_KEY_LEN 16 + +#define TOKEN_AES_PKAM_PRIVATE_KEY "aesPkamPrivateKey" +#define TOKEN_AES_PKAM_PRIVATE_KEY_LEN 17 + +#define TOKEN_AES_ENCRYPT_PUBLIC_KEY "aesEncryptPublicKey" +#define TOKEN_AES_ENCRYPT_PUBLIC_KEY_LEN 19 + +#define TOKEN_AES_ENCRYPT_PRIVATE_KEY "aesEncryptPrivateKey" +#define TOKEN_AES_ENCRYPT_PRIVATE_KEY_LEN 20 + +#define TOKEN_SELF_ENCRYPTION_KEY "selfEncryptionKey" +#define TOKEN_SELF_ENCRYPTION_KEY_LEN 17 + +typedef struct atclient_atkeysfile_entry{ + size_t len; + char *key; +} atclient_atkeysfile_entry; + +typedef struct atclient_atkeysfile { + atclient_atkeysfile_entry *aes_pkam_public_key; + atclient_atkeysfile_entry *aes_pkam_private_key; + atclient_atkeysfile_entry *aes_encrypt_public_key; + atclient_atkeysfile_entry *aes_encrypt_private_key; + atclient_atkeysfile_entry *self_encryption_key; +} atclient_atkeysfile; + +void atclient_atkeysfile_init(atclient_atkeysfile *atkeysfile); +int atclient_atkeysfile_read(const char *path, atclient_atkeysfile *atkeysfile); +int atclient_atkeysfile_write(const char *path, const char *atsign, atclient_atkeysfile *atkeysfile); +void atclient_atkeysfile_free(atclient_atkeysfile *atkeysfile); + +/** + * Usage example + * atclient_atkeysfile atkeysfile; + * atclient_atkeysfile_init(&atkeysfile); + * printf("done init...\n") + * + ret = atclient_atkeysfile_read(path, &atkeysfile); + if (ret != 0) + { + goto exit; + } + + printf("done read...\n"); + printf("aes_pkam_public_key: %s\n", atkeysfile.aes_pkam_public_key->key); + printf("aes_pkam_private_key: %s\n", atkeysfile.aes_pkam_private_key->key); + printf("aes_encrypt_public_key: %s\n", atkeysfile.aes_encrypt_public_key->key); + printf("aes_encrypt_private_key: %s\n", atkeysfile.aes_encrypt_private_key->key); + printf("self_encryption_key: %s\n", atkeysfile.self_encryption_key->key); + + printf("writing...\n"); + + ret = atclient_atkeysfile_write("/Users/jeremytubongbanua/.atsign/temp/@smoothalligator_key.atKeys", ATSIGN, &atkeysfile); + * + */ \ No newline at end of file diff --git a/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/connection.h b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/connection.h new file mode 100644 index 00000000..4d0d2351 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/atclient/include/atclient/connection.h @@ -0,0 +1,59 @@ +#pragma once + +#include + +#define HOST "root.atsign.org" +#define PORT 64 + +// #define HOST "245b44d4-a4bd-5f33-b077-c559f956486a.swarm0001.atsign.zone" +// #define PORT 1722 + +#define ROOT_CERT \ + "-----BEGIN CERTIFICATE-----\n" \ + "MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw\n" \ + "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ + "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw\n" \ + "WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\n" \ + "RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \ + "AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP\n" \ + "R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx\n" \ + "sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm\n" \ + "NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg\n" \ + "Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG\n" \ + "/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC\n" \ + "AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB\n" \ + "Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA\n" \ + "FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw\n" \ + "AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw\n" \ + "Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB\n" \ + "gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W\n" \ + "PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl\n" \ + "ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz\n" \ + "CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm\n" \ + "lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4\n" \ + "avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2\n" \ + "yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O\n" \ + "yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids\n" \ + "hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+\n" \ + "HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv\n" \ + "MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX\n" \ + "nLRbwHOoq7hHwg==\n" \ + "-----END CERTIFICATE-----\n" + +typedef struct atclient_connection_ctx { + char *host; // assume null terminated, example: "root.atsign.org" + int port; // example: 64 + char *cert_pem; // assume null terminated, example: "-----BEGIN CERTIFICATE-----\nMIIF..." + void *server_fd; + void *ssl; + void *conf; + void *cacert; + void *entropy; + void *ctr_drbg; + void *saved_session; +} atclient_connection_ctx; + +void atclient_connection_init(atclient_connection_ctx *ctx); +int atclient_connection_connect(atclient_connection_ctx *ctx, const char *host, const int port); +int atclient_connection_send(atclient_connection_ctx *ctx, unsigned char *recv, const size_t recvlen, size_t *olen, const unsigned char *src, const size_t srclen); +void atclient_connection_free(atclient_connection_ctx *ctx); diff --git a/examples/atclient_esp32_static_no_components/main/atclient/lib/libatclient.a b/examples/atclient_esp32_static_no_components/main/atclient/lib/libatclient.a new file mode 100644 index 00000000..4cf4208d Binary files /dev/null and b/examples/atclient_esp32_static_no_components/main/atclient/lib/libatclient.a differ diff --git a/examples/atclient_esp32_static_no_components/main/main.c b/examples/atclient_esp32_static_no_components/main/main.c new file mode 100644 index 00000000..da808d31 --- /dev/null +++ b/examples/atclient_esp32_static_no_components/main/main.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include "atchops/base64.h" + +void app_main(void) +{ + const char *src = "Lemonade!"; + unsigned long srclen = strlen(src); + + const unsigned long dstlen = 2048; + const unsigned char *dst = malloc(sizeof(unsigned char) * dstlen); + memset(dst, 0, dstlen); + unsigned long dstolen = 0; // written length + + int ret = atchops_base64_encode((const unsigned char *) src, srclen, dst, dstlen, &dstolen); + + printf("atchops_base64_encode: %d\n", ret); + + printf("src: %s\n", src); + printf("dst: %.*s\n", (int) dstolen, dst); + printf("dst bytes: \n"); + for(int i = 0; i < dstolen; i++) + { + printf("%02x ", *(dst + i)); + } + printf("\n"); +} \ No newline at end of file