2018-09-20 01:49:47 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2019-01-02 01:58:10 +01:00
|
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 16:23:50 +01:00
|
|
|
*/
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2016-07-03 20:06:33 +02:00
|
|
|
#include <ctype.h>
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
#include "curve25519.h"
|
2017-05-15 23:24:48 +02:00
|
|
|
#include "encoding.h"
|
2016-07-03 20:06:33 +02:00
|
|
|
#include "subcommands.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2016-07-03 20:06:33 +02:00
|
|
|
int pubkey_main(int argc, char *argv[])
|
2015-06-05 15:58:00 +02:00
|
|
|
{
|
2018-12-20 18:30:21 +01:00
|
|
|
uint8_t key[WG_KEY_LEN] __attribute__((aligned(sizeof(uintptr_t))));
|
2017-04-16 01:20:43 +02:00
|
|
|
char base64[WG_KEY_LEN_BASE64];
|
2016-07-03 20:06:33 +02:00
|
|
|
int trailing_char;
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
fprintf(stderr, "Usage: %s %s\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
if (fread(base64, 1, sizeof(base64) - 1, stdin) != sizeof(base64) - 1) {
|
2015-06-05 15:58:00 +02:00
|
|
|
errno = EINVAL;
|
2016-07-03 20:06:33 +02:00
|
|
|
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
|
2015-06-05 15:58:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-16 01:20:43 +02:00
|
|
|
base64[WG_KEY_LEN_BASE64 - 1] = '\0';
|
2016-07-03 20:06:33 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-16 01:20:43 +02:00
|
|
|
if (!key_from_base64(key, base64)) {
|
2016-07-03 20:06:33 +02:00
|
|
|
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
|
2015-06-05 15:58:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-16 01:20:43 +02:00
|
|
|
curve25519_generate_public(key, key);
|
|
|
|
key_to_base64(base64, key);
|
|
|
|
puts(base64);
|
2015-06-05 15:58:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|