wireguard-tools/src/pubkey.c
Jason A. Donenfeld 945fae0c7c wg: support text-based ipc
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2017-05-17 18:13:14 +02:00

48 lines
1.1 KiB
C

/* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include "curve25519.h"
#include "encoding.h"
#include "subcommands.h"
int pubkey_main(int argc, char *argv[])
{
uint8_t key[WG_KEY_LEN];
char base64[WG_KEY_LEN_BASE64];
int trailing_char;
if (argc != 1) {
fprintf(stderr, "Usage: %s %s\n", PROG_NAME, argv[0]);
return 1;
}
if (fread(base64, 1, sizeof(base64) - 1, stdin) != sizeof(base64) - 1) {
errno = EINVAL;
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
return 1;
}
base64[WG_KEY_LEN_BASE64 - 1] = '\0';
for (;;) {
trailing_char = getc(stdin);
if (!trailing_char || isspace(trailing_char) || isblank(trailing_char))
continue;
if (trailing_char == EOF)
break;
fprintf(stderr, "%s: Trailing characters found after key\n", PROG_NAME);
return 1;
}
if (!key_from_base64(key, base64)) {
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
return 1;
}
curve25519_generate_public(key, key);
key_to_base64(base64, key);
puts(base64);
return 0;
}