sigchk

sign and check files using ed25519
git clone git://z3bra.org/sick
Log | Files | Refs | Submodules | README | LICENSE

commit 1cbb809fd44a586205f99677cd30d9e4cb7c993d
parent 661b50826ce5c01385e203039db7ee96339f523d
Author: z3bra <willyatmailoodotorg>
Date:   Tue Apr 26 12:03:26 2016

Add error handling to genkey()

Returns with an error if writing to the file can't be done.
In case argument given is null, the DEFAULT_ALIAS is used to generate
the keys.

Diffstat:
 sick.c | 48 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 16 deletions(-)

diff --git a/sick.c b/sick.c @@ -12,6 +12,7 @@ #define PUBSIZ 32 #define PRIVSIZ ((PUBSIZ) * 2) +#define DEFAULT_ALIAS "ed25519" void usage(char *name); int genkey(char *alias); @@ -27,45 +28,60 @@ int genkey(char *alias) { int fd = 0; + size_t len = 0; char file[PATH_MAX]; unsigned char pub[PUBSIZ], priv[PRIVSIZ], seed[PUBSIZ]; + if (alias == NULL) { + alias = malloc(sizeof(DEFAULT_ALIAS)); + strncpy(alias, DEFAULT_ALIAS, sizeof(DEFAULT_ALIAS)); + } + + len = strnlen(alias, PATH_MAX); + ed25519_create_seed(seed); ed25519_create_keypair(pub, priv, seed); - /* write pubkey to a file name <alias>.pub */ memset(file, 0, PATH_MAX); - strncpy(file, alias, strnlen(alias, PATH_MAX)); - strncat(file, ".pub", PATH_MAX); + memcpy(file, alias, len); + + /* write pubkey to a file name <alias>.pub */ + memcpy(file+len, ".pub", 4); fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0644); - write(fd, pub, PUBSIZ); + if (fd < 0) { + perror(file); + return fd; + } + if (write(fd, pub, PUBSIZ) < PUBSIZ) { + perror(file); + return -1; + } close(fd); /* write privkey to a file name <alias>.key */ - memset(file, 0, PATH_MAX); - strncpy(file, alias, strnlen(alias, PATH_MAX)); - strncat(file, ".key", PATH_MAX); + memcpy(file+len, ".key", 4); fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0600); - write(fd, priv, PRIVSIZ); + if (fd < 0) { + perror(file); + return fd; + } + if (write(fd, priv, PRIVSIZ) < PRIVSIZ) { + perror(file); + return -1; + } close(fd); + return 0; } int main(int argc, char **argv) { - char *argf = NULL; char *argv0 = NULL; ARGBEGIN{ case 'g': - if (argc > 1) { - argf = ARGF(); - } else { - argf = malloc(sizeof("ed25519")); - strncpy(argf, "ed25519", sizeof("ed25519")); - } - genkey(argf); + genkey(argc > 1 ? ARGF() : NULL); break; default: usage(argv0);