aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/pubkey.c
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/pubkey.c
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/pubkey.c')
-rw-r--r--src/tools/pubkey.c33
1 files changed, 25 insertions, 8 deletions
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);