wg: improve error reporting and detection

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2016-07-03 20:06:33 +02:00
parent 1a64438b21
commit 742f038fc2
4 changed files with 43 additions and 24 deletions

View File

@ -93,12 +93,8 @@ static inline uint16_t parse_port(const char *value)
static inline bool parse_key(uint8_t key[WG_KEY_LEN], const char *value)
{
uint8_t tmp[WG_KEY_LEN + 1];
if (strlen(value) != b64_len(WG_KEY_LEN) - 1) {
fprintf(stderr, "Key is not the correct length: `%s`\n", value);
return false;
}
if (b64_pton(value, tmp, WG_KEY_LEN + 1) < 0) {
fprintf(stderr, "Could not parse base64 key: `%s`\n", value);
if (strlen(value) != b64_len(WG_KEY_LEN) - 1 || b64_pton(value, tmp, WG_KEY_LEN + 1) != WG_KEY_LEN) {
fprintf(stderr, "Key is not the correct length or format: `%s`\n", value);
return false;
}
memcpy(key, tmp, WG_KEY_LEN);

View File

@ -11,6 +11,7 @@
#include "curve25519.h"
#include "base64.h"
#include "subcommands.h"
#ifdef __NR_getrandom
static inline ssize_t get_random_bytes(uint8_t *out, size_t len)
@ -37,6 +38,11 @@ int genkey_main(int argc, char *argv[])
char private_key_base64[b64_len(CURVE25519_POINT_SIZE)];
struct stat stat;
if (argc != 1) {
fprintf(stderr, "Usage: %s %s\n", PROG_NAME, argv[0]);
return 1;
}
if (!fstat(STDOUT_FILENO, &stat) && S_ISREG(stat.st_mode) && stat.st_mode & S_IRWXO)
fputs("Warning: writing to world accessible file.\nConsider setting the umask to 077 and trying again.\n", stderr);
@ -47,9 +53,8 @@ int genkey_main(int argc, char *argv[])
if (argc && !strcmp(argv[0], "genkey"))
curve25519_normalize_secret(private_key);
if (b64_ntop(private_key, sizeof(private_key), private_key_base64, sizeof(private_key_base64)) < 0) {
errno = EINVAL;
perror("b64");
if (b64_ntop(private_key, sizeof(private_key), private_key_base64, sizeof(private_key_base64)) != sizeof(private_key_base64) - 1) {
fprintf(stderr, "%s: Could not convert key to base64\n", PROG_NAME);
return 1;
}

View File

@ -3,29 +3,46 @@
#include <errno.h>
#include <resolv.h>
#include <stdio.h>
#include <ctype.h>
#include "curve25519.h"
#include "base64.h"
#include "subcommands.h"
int pubkey_main(__attribute__((unused)) int argc, __attribute__((unused)) char *argv[])
int pubkey_main(int argc, char *argv[])
{
unsigned char private_key[CURVE25519_POINT_SIZE + 1] = { 0 }, public_key[CURVE25519_POINT_SIZE] = { 0 };
char private_key_base64[b64_len(CURVE25519_POINT_SIZE)] = { 0 }, public_key_base64[b64_len(CURVE25519_POINT_SIZE)] = { 0 };
int trailing_char;
if (argc != 1) {
fprintf(stderr, "Usage: %s %s\n", PROG_NAME, argv[0]);
return 1;
}
if (fread(private_key_base64, 1, sizeof(private_key_base64) - 1, stdin) != sizeof(private_key_base64) - 1) {
errno = EINVAL;
perror("fread(private key)");
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
return 1;
}
if (b64_pton(private_key_base64, private_key, sizeof(private_key)) < 0) {
errno = EINVAL;
perror("b64");
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 (b64_pton(private_key_base64, private_key, sizeof(private_key)) != sizeof(private_key) - 1) {
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
return 1;
}
curve25519_generate_public(public_key, private_key);
if (b64_ntop(public_key, sizeof(public_key), public_key_base64, sizeof(public_key_base64)) < 0) {
errno = EINVAL;
perror("b64");
if (b64_ntop(public_key, sizeof(public_key), public_key_base64, sizeof(public_key_base64)) != sizeof(public_key_base64) - 1) {
fprintf(stderr, "%s: Could not convert key to base64\n", PROG_NAME);
return 1;
}
puts(public_key_base64);

View File

@ -23,12 +23,13 @@ static const struct {
{ "pubkey", pubkey_main, "Reads a private key from stdin and writes a public key to stdout" }
};
static void show_usage(void)
static void show_usage(FILE *file)
{
fprintf(stderr, "Usage: %s <cmd> [<args>]\n\n", PROG_NAME);
fprintf(stderr, "Available subcommands:\n");
fprintf(file, "Usage: %s <cmd> [<args>]\n\n", PROG_NAME);
fprintf(file, "Available subcommands:\n");
for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i)
fprintf(stderr, " %s: %s\n", subcommands[i].subcommand, subcommands[i].description);
fprintf(file, " %s: %s\n", subcommands[i].subcommand, subcommands[i].description);
fprintf(file, "You may pass `--help' to any of these subcommands to view usage.\n");
}
int main(int argc, char *argv[])
@ -37,8 +38,8 @@ int main(int argc, char *argv[])
PROG_NAME = argv[0];
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help"))) {
show_usage();
return 1;
show_usage(stdout);
return 0;
}
if (argc == 1) {
@ -61,6 +62,6 @@ findsubcommand:
}
fprintf(stderr, "Invalid subcommand: `%s`\n", argv[1]);
show_usage();
show_usage(stderr);
return 1;
}