2016-11-18 05:22:00 +00:00
|
|
|
/* Copyright (C) 2015-2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#include <stdio.h>
|
2016-07-03 18:06:33 +00:00
|
|
|
#include <ctype.h>
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
#include "curve25519.h"
|
|
|
|
#include "base64.h"
|
2016-07-03 18:06:33 +00:00
|
|
|
#include "subcommands.h"
|
2015-06-05 13:58:00 +00:00
|
|
|
|
2016-07-03 18:06:33 +00:00
|
|
|
int pubkey_main(int argc, char *argv[])
|
2015-06-05 13:58:00 +00:00
|
|
|
{
|
|
|
|
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 };
|
2016-07-03 18:06:33 +00:00
|
|
|
int trailing_char;
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
fprintf(stderr, "Usage: %s %s\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
if (fread(private_key_base64, 1, sizeof(private_key_base64) - 1, stdin) != sizeof(private_key_base64) - 1) {
|
|
|
|
errno = EINVAL;
|
2016-07-03 18:06:33 +00:00
|
|
|
fprintf(stderr, "%s: Key is not the correct length or format\n", PROG_NAME);
|
2015-06-05 13:58:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-07-03 18:06:33 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-06-05 13:58:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
curve25519_generate_public(public_key, private_key);
|
2016-07-03 18:06:33 +00:00
|
|
|
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);
|
2015-06-05 13:58:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
puts(public_key_base64);
|
|
|
|
return 0;
|
|
|
|
}
|