From f2bc5ddbeca144fa79208a5ac6a029da6ed5c10c Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 31 Mar 2022 13:09:12 +0200 Subject: vconn bugfix --- ecp/util/mknode.c | 20 ++++++++++---- ecp/util/util.c | 80 ++++++++++++++++++++++++++++++++++++------------------- ecp/util/util.h | 11 +++++--- 3 files changed, 74 insertions(+), 37 deletions(-) (limited to 'ecp/util') diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c index 885a4c0..764ac18 100644 --- a/ecp/util/mknode.c +++ b/ecp/util/mknode.c @@ -4,7 +4,8 @@ #include #include -#include "core.h" +#include + #include "util.h" #define FN_LEN 256 @@ -18,12 +19,16 @@ static void usage(char *arg) { } int main(int argc, char *argv[]) { + char *addr; ECPDHKey key; ECPNode node; int rv; if ((argc < 2) || (argc > 3)) usage(argv[0]); + addr = NULL; + if (argc == 3) addr = argv[2]; + if (strlen(argv[1]) > FN_LEN - 6) usage(argv[0]); strcpy(fn_node, argv[1]); strcpy(fn_key, argv[1]); @@ -33,14 +38,19 @@ int main(int argc, char *argv[]) { rv = ecp_dhkey_gen(&key); if (rv) goto err; - rv = ecp_node_init(&node, &key.public, (argc == 3) ? argv[2] : NULL); + rv = ecp_node_init(&node, &key.public, addr); if (rv) goto err; - rv = ecp_util_key_save(&key, fn_key); + rv = ecp_util_save_key(&key.public, &key.private, fn_key); if (rv) goto err; - rv = ecp_util_node_save(&node, fn_node); - if (rv) goto err; + if (addr) { + rv = ecp_util_save_node(&node, fn_node); + if (rv) goto err; + } else { + rv = ecp_util_save_pub(&key.public, fn_node); + if (rv) goto err; + } return 0; diff --git a/ecp/util/util.c b/ecp/util/util.c index ee51c73..2b67127 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -3,24 +3,22 @@ #include #include -#include "core.h" -#include "cr.h" +#include + #include "util.h" -int ecp_util_key_save(ECPDHKey *key, char *filename) { +int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { int fd; ssize_t rv; - if (!key->valid) return ECP_ERR; - - if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, &key->public, sizeof(key->public)); - if (rv != sizeof(key->public)) { + if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; + rv = read(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = write(fd, &key->private, sizeof(key->private)); - if (rv != sizeof(key->private)) { + rv = read(fd, private, sizeof(ecp_ecdh_private_t)); + if (rv != sizeof(ecp_ecdh_private_t)) { close(fd); return ECP_ERR; } @@ -28,41 +26,46 @@ int ecp_util_key_save(ECPDHKey *key, char *filename) { return ECP_OK; } -int ecp_util_key_load(ECPDHKey *key, char *filename) { +int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { int fd; ssize_t rv; - if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; - rv = read(fd, &key->public, sizeof(key->public)); - if (rv != sizeof(key->public)) { + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + rv = write(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = read(fd, &key->private, sizeof(key->private)); - if (rv != sizeof(key->private)) { + rv = write(fd, private, sizeof(ecp_ecdh_private_t)); + if (rv != sizeof(ecp_ecdh_private_t)) { close(fd); return ECP_ERR; } close(fd); - - key->valid = 1; return ECP_OK; } -int ecp_util_node_save(ECPNode *node, char *filename) { +int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename) { int fd; ssize_t rv; - if (!node->key_perma.valid) return ECP_ERR; - - if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); - if (rv != sizeof(node->key_perma.public)) { + if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; + rv = read(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = write(fd, &node->addr, sizeof(node->addr)); - if (rv != sizeof(node->addr)) { + close(fd); + return ECP_OK; +} + +int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename) { + int fd; + ssize_t rv; + + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + rv = write(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } @@ -70,7 +73,7 @@ int ecp_util_node_save(ECPNode *node, char *filename) { return ECP_OK; } -int ecp_util_node_load(ECPNode *node, char *filename) { +int ecp_util_load_node(ECPNode *node, char *filename) { int fd; ssize_t rv; @@ -89,4 +92,25 @@ int ecp_util_node_load(ECPNode *node, char *filename) { node->key_perma.valid = 1; return ECP_OK; -} \ No newline at end of file +} + +int ecp_util_save_node(ECPNode *node, char *filename) { + int fd; + ssize_t rv; + + if (!node->key_perma.valid) return ECP_ERR; + + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); + if (rv != sizeof(node->key_perma.public)) { + close(fd); + return ECP_ERR; + } + rv = write(fd, &node->addr, sizeof(node->addr)); + if (rv != sizeof(node->addr)) { + close(fd); + return ECP_ERR; + } + close(fd); + return ECP_OK; +} diff --git a/ecp/util/util.h b/ecp/util/util.h index 631a808..83c4c14 100644 --- a/ecp/util/util.h +++ b/ecp/util/util.h @@ -1,5 +1,8 @@ -int ecp_util_key_save(ECPDHKey *key, char *filename); -int ecp_util_key_load(ECPDHKey *key, char *filename); +int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); +int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); -int ecp_util_node_save(ECPNode *node, char *filename); -int ecp_util_node_load(ECPNode *node, char *filename); \ No newline at end of file +int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename); +int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename); + +int ecp_util_load_node(ECPNode *node, char *filename); +int ecp_util_save_node(ECPNode *node, char *filename); -- cgit v1.2.3