From d8d1ab4ee1e92ec84fe9ea86eec2396275483a92 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 7 Jun 2015 22:50:05 +0200 Subject: [PATCH] Fix warnings about missing return value checks. In some harmless places, checks for the return value of ECDSA and RSA key generation and verification was omitted. Add them to keep the compiler happy and to warn end users in case something is wrong. --- src/invitation.c | 16 +++++++++++++--- src/sptps_keypair.c | 8 ++++++-- src/sptps_speed.c | 13 ++++++++++--- src/tincctl.c | 6 ++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/invitation.c b/src/invitation.c index 415c237..3102e41 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -335,7 +335,11 @@ int cmd_invite(int argc, char *argv[]) { return 1; } chmod(filename, 0600); - ecdsa_write_pem_private_key(key, f); + if(!ecdsa_write_pem_private_key(key, f)) { + fprintf(stderr, "Could not write ECDSA private key\n"); + fclose(f); + return 1; + } fclose(f); if(connect_tincd(false)) @@ -704,6 +708,8 @@ make_names: snprintf(filename, sizeof filename, "%s" SLASH "ed25519_key.priv", confbase); f = fopenmask(filename, "w", 0600); + if(!f) + return false; if(!ecdsa_write_pem_private_key(key, f)) { fprintf(stderr, "Error writing private key!\n"); @@ -725,10 +731,14 @@ make_names: snprintf(filename, sizeof filename, "%s" SLASH "rsa_key.priv", confbase); f = fopenmask(filename, "w", 0600); - rsa_write_pem_private_key(rsa, f); + if(!f || !rsa_write_pem_private_key(rsa, f)) { + fprintf(stderr, "Could not write private RSA key\n"); + } else if(!rsa_write_pem_public_key(rsa, fh)) { + fprintf(stderr, "Could not write public RSA key\n"); + } + fclose(f); - rsa_write_pem_public_key(rsa, fh); fclose(fh); rsa_free(rsa); diff --git a/src/sptps_keypair.c b/src/sptps_keypair.c index 399404e..fde8622 100644 --- a/src/sptps_keypair.c +++ b/src/sptps_keypair.c @@ -88,7 +88,10 @@ int main(int argc, char *argv[]) { FILE *fp = fopen(argv[1], "w"); if(fp) { - ecdsa_write_pem_private_key(key, fp); + if(!ecdsa_write_pem_private_key(key, fp)) { + fprintf(stderr, "Could not write ECDSA private key\n"); + return 1; + } fclose(fp); } else { fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno)); @@ -97,7 +100,8 @@ int main(int argc, char *argv[]) { fp = fopen(argv[2], "w"); if(fp) { - ecdsa_write_pem_public_key(key, fp); + if(!ecdsa_write_pem_public_key(key, fp)) + fprintf(stderr, "Could not write ECDSA public key\n"); fclose(fp); } else { fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno)); diff --git a/src/sptps_speed.c b/src/sptps_speed.c index d03246c..4cb1221 100644 --- a/src/sptps_speed.c +++ b/src/sptps_speed.c @@ -102,19 +102,26 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration); for(clock_start(); clock_countto(duration);) - ecdsa_sign(key1, buf1, 256, buf2); + if(!ecdsa_sign(key1, buf1, 256, buf2)) + return 1; fprintf(stderr, "%22.2lf op/s\n", rate); fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration); for(clock_start(); clock_countto(duration);) - ecdsa_verify(key1, buf1, 256, buf2); + if(!ecdsa_verify(key1, buf1, 256, buf2)) { + fprintf(stderr, "Signature verification failed\n"); + return 1; + } fprintf(stderr, "%20.2lf op/s\n", rate); ecdh1 = ecdh_generate_public(buf1); fprintf(stderr, "ECDH for %lg seconds: ", duration); for(clock_start(); clock_countto(duration);) { ecdh2 = ecdh_generate_public(buf2); - ecdh_compute_shared(ecdh2, buf1, buf3); + if(!ecdh2) + return 1; + if(!ecdh_compute_shared(ecdh2, buf1, buf3)) + return 1; } fprintf(stderr, "%28.2lf op/s\n", rate); ecdh_free(ecdh1); diff --git a/src/tincctl.c b/src/tincctl.c index c9d8c67..00757c4 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -233,6 +233,12 @@ FILE *fopenmask(const char *filename, const char *mode, mode_t perms) { perms &= ~mask; umask(~perms); FILE *f = fopen(filename, mode); + + if(!f) { + fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno)); + return NULL; + } + #ifdef HAVE_FCHMOD if((perms & 0444) && f) fchmod(fileno(f), perms); -- 2.25.1