From b3193f207c0e06721bd173a8edb5a8ce940ddc9f Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 11 Nov 2020 18:37:40 -0800 Subject: [PATCH 1/5] testutils: mark function as deprecated --- include/testutils/testutils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/testutils/testutils.h b/include/testutils/testutils.h index 5db0a14d..c7d3428e 100644 --- a/include/testutils/testutils.h +++ b/include/testutils/testutils.h @@ -2,4 +2,5 @@ #include "crypto/ecdsa.h" +/*! @note deprecated */ ecdsa_private_key_t *new_ecdsa_private_key(void); \ No newline at end of file From 63aae497253f736cdc50408c0fe77302692048ce Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 11 Nov 2020 18:38:01 -0800 Subject: [PATCH 2/5] crypto: add assertion ecdsa key gen func --- include/crypto/ecdsa.h | 7 ++++++- src/crypto/ecdsa.c | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/crypto/ecdsa.h b/include/crypto/ecdsa.h index 8cd55914..facf7c88 100644 --- a/include/crypto/ecdsa.h +++ b/include/crypto/ecdsa.h @@ -102,4 +102,9 @@ int libp2p_crypto_ecdsa_free(ecdsa_private_key_t *pk); * @return Success: pointer to an instance of an ecdsa_private_key_t type * @return Failure: NULL pointer */ -ecdsa_private_key_t *libp2p_crypto_ecdsa_private_key_from_file(char *path); \ No newline at end of file +ecdsa_private_key_t *libp2p_crypto_ecdsa_private_key_from_file(char *path); + +/*! + * @brief generates a new ecdsa key asserting there is no error +*/ +ecdsa_private_key_t *assert_new_ecdsa_private_key(void); \ No newline at end of file diff --git a/src/crypto/ecdsa.c b/src/crypto/ecdsa.c index 3d6b16a8..b230ae15 100644 --- a/src/crypto/ecdsa.c +++ b/src/crypto/ecdsa.c @@ -43,6 +43,7 @@ #include #include #include +#include /*! * @brief frees up resources allocated for the private key @@ -312,4 +313,14 @@ unsigned char *libp2p_crypto_ecdsa_private_key_to_pem(ecdsa_private_key_t *pk) { memcpy(ret_buf, buffer, strlen((char *)buffer)); return ret_buf; +} + +/*! + * @brief generates a new ecdsa key asserting there is no error +*/ +ecdsa_private_key_t *assert_new_ecdsa_private_key(void) { + ecdsa_private_key_t *pk = + libp2p_crypto_ecdsa_keypair_generation(MBEDTLS_ECP_DP_SECP256R1); + assert(pk != NULL); + return pk; } \ No newline at end of file From 40a9d0aa906dee16b791442b608aa25f8fe5ca33 Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 11 Nov 2020 18:38:17 -0800 Subject: [PATCH 3/5] tests: use assertion gen func --- tests/peerstore_test.c | 7 ++++--- tests/socket_test.c | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/peerstore_test.c b/tests/peerstore_test.c index c6b75ddb..c538f70c 100644 --- a/tests/peerstore_test.c +++ b/tests/peerstore_test.c @@ -1,5 +1,6 @@ #include "peerstore/peerstore.h" -#include "testutils/testutils.h" +// #include "testutils/testutils.h" +#include "crypto/ecdsa.h" #include #include #include @@ -50,7 +51,7 @@ void peerstore_test_insert_peer(void **state) { peerstore_t *pst = peerstore_new_assert(100); for (int i = 0; i < 100; i++) { - ecdsa_private_key_t *priv_key = new_ecdsa_private_key(); + ecdsa_private_key_t *priv_key = assert_new_ecdsa_private_key(); assert(priv_key != NULL); peer_id_t *pid = libp2p_crypto_ecdsa_keypair_peerid(priv_key); assert(pid != NULL); @@ -83,7 +84,7 @@ void peerstore_test_insert_peer(void **state) { assert(pst->num_peers == 100); - ecdsa_private_key_t *priv_key = new_ecdsa_private_key(); + ecdsa_private_key_t *priv_key = assert_new_ecdsa_private_key(); assert(priv_key != NULL); peer_id_t *pid = libp2p_crypto_ecdsa_keypair_peerid(priv_key); assert(pid != NULL); diff --git a/tests/socket_test.c b/tests/socket_test.c index 4c478367..c3041006 100644 --- a/tests/socket_test.c +++ b/tests/socket_test.c @@ -16,10 +16,11 @@ #include "network/messages.h" #include "network/socket_server.h" #include "multiaddr/multiaddr.h" +#include "crypto/ecdsa.h" #include #include #include -#include +#include #include #include #include @@ -51,8 +52,8 @@ void start_socker_server_wrapper(void *data) { * @warning this test is currently leaking about 24 bytes likely because we are using two thread pools which rely on some sort of global stuff */ void test_new_socket_server(void **state) { - ecdsa_private_key_t *server1_pk = new_ecdsa_private_key(); - ecdsa_private_key_t *server2_pk = new_ecdsa_private_key(); + ecdsa_private_key_t *server1_pk = assert_new_ecdsa_private_key(); + ecdsa_private_key_t *server2_pk = assert_new_ecdsa_private_key(); int rc = libp2p_crypto_ecdsa_private_key_save(server1_pk, "server1.pem"); assert(rc == 0); From 2cea5a1001de7c36608de7b50605615d5080a40e Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 11 Nov 2020 18:38:33 -0800 Subject: [PATCH 4/5] cli: add gen key func and enable loading private key --- src/cli/main.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/cli/main.c b/src/cli/main.c index c9a484b1..ad29f053 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -6,6 +6,7 @@ #include "thirdparty/argtable3/argtable3.h" #include "thirdparty/logger/colors.h" #include "thirdparty/logger/logger.h" +#include "crypto/ecdsa.h" #include #include #include @@ -21,11 +22,27 @@ void start_server_callback(int argc, char *argv[]); void test_server_callback(int argc, char *argv[]); +void gen_key_callback(int argc, char *argv[]); struct arg_str *listen_address_tcp; struct arg_str *listen_address_udp; struct arg_str *pem_file_path; +void gen_key_callback(int argc, char *argv[]) { + // ensure pem file path is present + if (pem_file_path->count == 0) { + return; + } + // generate the actual ecdsa key + ecdsa_private_key_t *pk = assert_new_ecdsa_private_key(); + int rc = libp2p_crypto_ecdsa_private_key_save(pk, (char *)*pem_file_path->sval); + if (rc != 0) { + printf("failed to save ecdsa key\n"); + return; + } + printf("generated ecdsa key and saved to %s\n", (char *)*pem_file_path->sval); +} + void test_server_callback(int argc, char *argv[]) { multi_addr_t *tcp_addr = NULL; multi_addr_t *udp_addr = NULL; @@ -123,6 +140,10 @@ void test_server_callback(int argc, char *argv[]) { } void start_server_callback(int argc, char *argv[]) { + if (pem_file_path->count == 0) { + printf("no key file path given\n"); + return; + } socket_server_config_t *config = new_socket_server_config(1); if (config == NULL) { printf("failed to initialize config\n"); @@ -141,7 +162,8 @@ void start_server_callback(int argc, char *argv[]) { config->addrs[0] = tcp_addr; config->fn_tcp = handle_inbound_rpc; config->recv_timeout_sec = 3; - + config->private_key_path = (char *)*pem_file_path->sval; + thread_logger *logger = new_thread_logger(true); if (logger == NULL) { free_socket_server_config(config); @@ -174,6 +196,18 @@ void start_server_callback(int argc, char *argv[]) { // displays the help command command_handler *new_socket_server_command(); command_handler *new_socket_server_test_command(); +command_handler *new_gen_key_command(); + +command_handler *new_gen_key_command() { + command_handler *handler = malloc(sizeof(command_handler)); + if (handler == NULL) { + printf("failed to malloc command_handler\n"); + return NULL; + } + handler->callback = gen_key_callback; + handler->name = "gen-key"; + return handler; +} command_handler *new_socket_server_command() { command_handler *handler = malloc(sizeof(command_handler)); @@ -247,6 +281,7 @@ int main(int argc, char *argv[]) { load_command(pcmd, new_socket_server_command()); load_command(pcmd, new_socket_server_test_command()); + load_command(pcmd, new_gen_key_command()); // END COMMAND INPUT PREPARATION int resp = execute(pcmd, (char *)*command_to_run->sval); From 374a0e6762dddb0a65de70a1df3549bdde8811f3 Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 11 Nov 2020 23:19:36 -0800 Subject: [PATCH 5/5] cli: free allocated resources once cmd done --- src/cli/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index ad29f053..4cb08424 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -35,12 +35,15 @@ void gen_key_callback(int argc, char *argv[]) { } // generate the actual ecdsa key ecdsa_private_key_t *pk = assert_new_ecdsa_private_key(); + // save the ecdsa key in pem format at path int rc = libp2p_crypto_ecdsa_private_key_save(pk, (char *)*pem_file_path->sval); if (rc != 0) { printf("failed to save ecdsa key\n"); - return; - } - printf("generated ecdsa key and saved to %s\n", (char *)*pem_file_path->sval); + } else { + printf("generated ecdsa key and saved to %s\n", (char *)*pem_file_path->sval); + } + // free up memory allocated to pk struct + libp2p_crypto_ecdsa_free(pk); } void test_server_callback(int argc, char *argv[]) {