Skip to content

Commit

Permalink
Merge pull request #55 from RTradeLtd/gen-key
Browse files Browse the repository at this point in the history
Add CLI Command To Generate ECDSA Key
  • Loading branch information
bonedaddy authored Nov 12, 2020
2 parents 432b5de + 374a0e6 commit d0f276a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
7 changes: 6 additions & 1 deletion include/crypto/ecdsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
1 change: 1 addition & 0 deletions include/testutils/testutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "crypto/ecdsa.h"

/*! @note deprecated */
ecdsa_private_key_t *new_ecdsa_private_key(void);
40 changes: 39 additions & 1 deletion src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "thirdparty/argtable3/argtable3.h"
#include "thirdparty/logger/colors.h"
#include "thirdparty/logger/logger.h"
#include "crypto/ecdsa.h"
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
Expand All @@ -21,11 +22,30 @@

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();
// 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");
} 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[]) {
multi_addr_t *tcp_addr = NULL;
multi_addr_t *udp_addr = NULL;
Expand Down Expand Up @@ -123,6 +143,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");
Expand All @@ -141,7 +165,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);
Expand Down Expand Up @@ -174,6 +199,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));
Expand Down Expand Up @@ -247,6 +284,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);
Expand Down
11 changes: 11 additions & 0 deletions src/crypto/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>

/*!
* @brief frees up resources allocated for the private key
Expand Down Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions tests/peerstore_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "peerstore/peerstore.h"
#include "testutils/testutils.h"
// #include "testutils/testutils.h"
#include "crypto/ecdsa.h"
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions tests/socket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#include "network/messages.h"
#include "network/socket_server.h"
#include "multiaddr/multiaddr.h"
#include "crypto/ecdsa.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d0f276a

Please sign in to comment.