summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2016-07-03 20:06:33 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2016-07-03 20:45:48 +0200
commit62cc3a9c5af2fd7cc8847a577b6b911c79e110e7 (patch)
treeb11d05c7cd25c86aeb7cf8b3315e6f09f407a0f8 /src/tools
parenttai64n: don't forget to add 2^62, to be in spec (diff)
downloadwireguard-monolithic-historical-62cc3a9c5af2fd7cc8847a577b6b911c79e110e7.tar.xz
wireguard-monolithic-historical-62cc3a9c5af2fd7cc8847a577b6b911c79e110e7.zip
tools: improve error reporting and detection
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/config.c8
-rw-r--r--src/tools/genkey.c11
-rw-r--r--src/tools/pubkey.c33
-rw-r--r--src/tools/wg.c15
4 files changed, 43 insertions, 24 deletions
diff --git a/src/tools/config.c b/src/tools/config.c
index 0cec30e..9066178 100644
--- a/src/tools/config.c
+++ b/src/tools/config.c
@@ -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);
diff --git a/src/tools/genkey.c b/src/tools/genkey.c
index 1602ae1..8e63108 100644
--- a/src/tools/genkey.c
+++ b/src/tools/genkey.c
@@ -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;
}
diff --git a/src/tools/pubkey.c b/src/tools/pubkey.c
index d9a97d9..452c8fa 100644
--- a/src/tools/pubkey.c
+++ b/src/tools/pubkey.c
@@ -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);
diff --git a/src/tools/wg.c b/src/tools/wg.c
index d4d2965..ee19387 100644
--- a/src/tools/wg.c
+++ b/src/tools/wg.c
@@ -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;
}