X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fsptps_keypair.c;h=7fcfee649b4c7722d36ad31ff920fba69badb35c;hb=48dd371b2ec69f02870aa5e2a67fc8adb4617ff1;hp=7e47d06f380464c93ee3e4351f4eed8eaedee5c4;hpb=ccbe79c03b8c64432bbf1ce82dc0e123cc02bd6d;p=tinc diff --git a/src/sptps_keypair.c b/src/sptps_keypair.c index 7e47d06f..7fcfee64 100644 --- a/src/sptps_keypair.c +++ b/src/sptps_keypair.c @@ -20,6 +20,7 @@ #include "system.h" #include "crypto.h" +#include "random.h" #include "ecdsagen.h" #include "logger.h" #include "names.h" @@ -49,6 +50,46 @@ static struct option const long_options[] = { {NULL, 0, NULL, 0} }; +static int generate_keypair(char *argv[]) { + ecdsa_t *key = ecdsa_generate(); + + if(!key) { + return 1; + } + + FILE *fp = fopen(argv[1], "w"); + + if(fp) { + if(!ecdsa_write_pem_private_key(key, fp)) { + fprintf(stderr, "Could not write ECDSA private key\n"); + ecdsa_free(key); + return 1; + } + + fclose(fp); + } else { + fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno)); + ecdsa_free(key); + return 1; + } + + fp = fopen(argv[2], "w"); + + if(fp) { + if(!ecdsa_write_pem_public_key(key, fp)) { + fprintf(stderr, "Could not write ECDSA public key\n"); + } + + ecdsa_free(key); + fclose(fp); + return 0; + } else { + fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno)); + ecdsa_free(key); + return 1; + } +} + int main(int argc, char *argv[]) { program_name = argv[0]; int r; @@ -81,43 +122,12 @@ int main(int argc, char *argv[]) { return 1; } + random_init(); crypto_init(); - ecdsa_t *key = ecdsa_generate(); + int result = generate_keypair(argv); - if(!key) { - return 1; - } - - FILE *fp = fopen(argv[1], "w"); + random_exit(); - if(fp) { - if(!ecdsa_write_pem_private_key(key, fp)) { - fprintf(stderr, "Could not write ECDSA private key\n"); - free(key); - return 1; - } - - fclose(fp); - } else { - fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno)); - free(key); - return 1; - } - - fp = fopen(argv[2], "w"); - - if(fp) { - if(!ecdsa_write_pem_public_key(key, fp)) { - fprintf(stderr, "Could not write ECDSA public key\n"); - } - - free(key); - fclose(fp); - return 0; - } else { - fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno)); - free(key); - return 1; - } + return result; }