From 1bbef32baabfa9da3bb55754da06c0b05550aa46 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 22 Apr 2012 13:54:36 +0200 Subject: Add --show-key and --machine-readable options --- src/config.c | 23 +++++++++++++++++------ src/fastd.c | 10 ++++++++++ src/fastd.h | 3 +++ src/protocol_ec25519_fhmqvc.c | 20 +++++++++++++++++--- 4 files changed, 47 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index c5c4c65..f407239 100644 --- a/src/config.c +++ b/src/config.c @@ -88,7 +88,9 @@ static void default_config(fastd_config *conf) { conf->on_disestablish = NULL; conf->on_disestablish_dir = NULL; + conf->machine_readable = false; conf->generate_key = false; + conf->show_key = false; } static bool config_match(const char *opt, ...) { @@ -345,7 +347,9 @@ static void count_peers(fastd_context *ctx, fastd_config *conf) { OPTION_ARG(option_on_down, "--on-down", "", "Sets a shell command to execute before interface destruction") \ OPTION_ARG(option_on_establish, "--on-establish", "", "Sets a shell command to execute when a new connection is established") \ OPTION_ARG(option_on_disestablish, "--on-disestablish", "", "Sets a shell command to execute when a connection is lost") \ - OPTION(option_generate_key, "--generate-key", "Generates a new keypair") + OPTION(option_generate_key, "--generate-key", "Generates a new keypair") \ + OPTION(option_show_key, "--show-key", "Shows the public key corresponding to the configured secret") \ + OPTION(option_machine_readable, "--machine-readable", "Supresses output of explaining text in the --show-key and --generate-key commands") static void print_usage(const char *options, const char *message) { @@ -551,6 +555,16 @@ static void option_on_disestablish(fastd_context *ctx, fastd_config *conf, const static void option_generate_key(fastd_context *ctx, fastd_config *conf) { conf->generate_key = true; + conf->show_key = false; +} + +static void option_show_key(fastd_context *ctx, fastd_config *conf) { + conf->generate_key = false; + conf->show_key = true; +} + +static void option_machine_readable(fastd_context *ctx, fastd_config *conf) { + conf->machine_readable = true; } @@ -580,11 +594,8 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con exit_error(ctx, "config error: unknown option `%s'; see --help for usage", argv[i]); } - if (conf->generate_key) { - ctx->conf = conf; - conf->protocol->generate_key(ctx); - exit(0); - } + if (conf->generate_key || conf->show_key) + return; if (conf->mode == MODE_TUN) { if (!conf->peers || conf->peers->next) diff --git a/src/fastd.c b/src/fastd.c index 0c7e6f5..b35f70e 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -625,8 +625,18 @@ int main(int argc, char *argv[]) { fastd_configure(&ctx, &conf, argc, argv); ctx.conf = &conf; + if (conf.generate_key) { + conf.protocol->generate_key(&ctx); + exit(0); + } + conf.protocol_config = conf.protocol->init(&ctx); + if (conf.show_key) { + conf.protocol->show_key(&ctx); + exit(0); + } + update_time(&ctx); init_tuntap(&ctx); diff --git a/src/fastd.h b/src/fastd.h index 08ef666..df072a7 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -74,6 +74,7 @@ struct _fastd_protocol { void (*free_peer_state)(fastd_context *ctx, fastd_peer *peer); void (*generate_key)(fastd_context *ctx); + void (*show_key)(fastd_context *ctx); }; struct _fastd_method { @@ -157,7 +158,9 @@ struct _fastd_config { char *on_disestablish; char *on_disestablish_dir; + bool machine_readable; bool generate_key; + bool show_key; }; struct _fastd_context { diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 719482c..3cd25b8 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -696,7 +696,8 @@ static void protocol_generate_key(fastd_context *ctx) { ecc_secret_key_256 secret_key; ecc_public_key_256 public_key; - pr_info(ctx, "Reading 32 bytes from /dev/random..."); + if (!ctx->conf->machine_readable) + pr_info(ctx, "Reading 32 bytes from /dev/random..."); fastd_random_bytes(ctx, secret_key.s, 32, true); ecc_25519_secret_sanitize(&secret_key, &secret_key); @@ -705,8 +706,20 @@ static void protocol_generate_key(fastd_context *ctx) { ecc_25519_scalarmult_base(&work, &secret_key); ecc_25519_store(&public_key, &work); - hexdump("Secret: ", secret_key.s); - hexdump("Public: ", public_key.p); + if (ctx->conf->machine_readable) { + hexdump("", secret_key.s); + } + else { + hexdump("Secret: ", secret_key.s); + hexdump("Public: ", public_key.p); + } +} + +static void protocol_show_key(fastd_context *ctx) { + if (ctx->conf->machine_readable) + hexdump("", ctx->conf->protocol_config->public_key.p); + else + hexdump("Public: ", ctx->conf->protocol_config->public_key.p); } @@ -725,4 +738,5 @@ const fastd_protocol fastd_protocol_ec25519_fhmqvc = { .free_peer_state = protocol_free_peer_state, .generate_key = protocol_generate_key, + .show_key = protocol_show_key, }; -- cgit v1.2.3