Add --show-key and --machine-readable options

This commit is contained in:
Matthias Schiffer 2012-04-22 13:54:36 +02:00
parent f21a6e3cec
commit 1bbef32baa
4 changed files with 47 additions and 9 deletions

View file

@ -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", "<command>", "Sets a shell command to execute before interface destruction") \
OPTION_ARG(option_on_establish, "--on-establish", "<command>", "Sets a shell command to execute when a new connection is established") \
OPTION_ARG(option_on_disestablish, "--on-disestablish", "<command>", "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)

View file

@ -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);

View file

@ -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 {

View file

@ -696,6 +696,7 @@ static void protocol_generate_key(fastd_context *ctx) {
ecc_secret_key_256 secret_key;
ecc_public_key_256 public_key;
if (!ctx->conf->machine_readable)
pr_info(ctx, "Reading 32 bytes from /dev/random...");
fastd_random_bytes(ctx, secret_key.s, 32, true);
@ -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);
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,
};