You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.9 KiB
C
191 lines
4.9 KiB
C
/*
|
|
* Secure X Message Passing Library tools.
|
|
*
|
|
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
|
|
*
|
|
* libsxmp is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* libsxmp is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
|
|
*
|
|
* sxtkeyinfo - sxt key container information tool for get info on the keys
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <dirent.h>
|
|
#define __USE_GNU
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdarg.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <limits.h>
|
|
#include <unistd.h>
|
|
#include <execinfo.h>
|
|
#include <getopt.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <termios.h>
|
|
|
|
#include <sxt/errno.h>
|
|
#include <sxt/safebuf.h>
|
|
#include <sxt/rdb.h>
|
|
#include <sxt/sxtkey.h>
|
|
#include <sxt/socket.h>
|
|
#include <sxt/sxt.h>
|
|
|
|
#define MAX_PATHNAME 4096
|
|
#define MAX_FNAME 128
|
|
#define MAX_NAMEPREFIX 90
|
|
|
|
#define FULL_PROGRAM_NAME "SXT key container information tool"
|
|
|
|
static void __help_print(FILE *fso, const char *fmtname)
|
|
{
|
|
fprintf(fso, "\n%s\n\n", FULL_PROGRAM_NAME);
|
|
|
|
/* usage options */
|
|
fprintf(fso, "Usage:\n");
|
|
fprintf(fso, "\t%s <key file>\n", fmtname);
|
|
|
|
/* defaults */
|
|
fprintf(fso, "\t%s -h | --help\n", fmtname);
|
|
fprintf(fso, "\t%s -v | --version\n", fmtname);
|
|
|
|
/* options description */
|
|
fprintf(fso, "\nOptions:\n");
|
|
|
|
fprintf(fso, "\t%-25s Show help screen.\n", "-h | --help");
|
|
fprintf(fso, "\t%-25s Print version information.\n", "-v | --version");
|
|
|
|
return;
|
|
}
|
|
|
|
static void __print_keyinfo(FILE *fmt, sxtkey_t *key, const char *fname)
|
|
{
|
|
fprintf(fmt, "Key file container '%s':\n", fname);
|
|
if(sxtkey_public(key)) {
|
|
fprintf(fmt, "\t * Public key\n");
|
|
} else {
|
|
fprintf(fmt, "\t * Private key\n");
|
|
}
|
|
fprintf(fmt, "\t * Type: %s\n", sxtkey_name(key->type));
|
|
fprintf(fmt, "\t * Contain optional hash: %lu\n", key->hash);
|
|
fprintf(fmt, "\n");
|
|
return;
|
|
}
|
|
|
|
extern int passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv);
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
sxtkey_t *key = NULL;
|
|
FILE *file = NULL;
|
|
char *rawbuf = NULL;
|
|
struct stat stb;
|
|
int opt, r, public;
|
|
|
|
while(1) {
|
|
int option_index = 0;
|
|
static struct option long_options[] = {
|
|
/* These options a generic ones. */
|
|
{"help", no_argument, NULL, 'h'}, /* print out help and version info */
|
|
{"version", no_argument, NULL, 'v'}, /* just out a version info */
|
|
/* termnil */
|
|
{NULL, 0, NULL, 0},
|
|
};
|
|
|
|
if((opt = getopt_long(argc, argv, "hv", long_options,
|
|
&option_index)) == -1) break;
|
|
|
|
switch(opt) {
|
|
case 'h':
|
|
__help_print(stdout, argv[0]);
|
|
return 0;
|
|
break;
|
|
case 'v':
|
|
/* TODO: add version output */
|
|
return 0;
|
|
break;
|
|
default:
|
|
fprintf(stderr, "Aborting.\n");
|
|
__help_print(stdout, argv[0]);
|
|
abort();
|
|
}
|
|
}
|
|
|
|
if(argv[1] == NULL) {
|
|
fprintf(stderr, "No input file.\n");
|
|
__help_print(stdout, argv[1]);
|
|
}
|
|
|
|
/* init library */
|
|
if((r = sxt_init())) {
|
|
fprintf(stderr, "Unable to init sxt library(%d).\nAborting.\n", r);
|
|
abort();
|
|
}
|
|
|
|
/* let's have a deal with a file */
|
|
if(stat(argv[1], &stb)) {
|
|
fprintf(stderr, "Unable to stat file '%s'\nAborting.\n", argv[1]);
|
|
abort();
|
|
}
|
|
if(!(file = fopen(argv[1], "r"))) {
|
|
fprintf(stderr, "Unable to open file '%s'\nAborting.\n", argv[1]);
|
|
abort();
|
|
}
|
|
if(!(rawbuf = malloc(SXT_PPKP_MAXCSIZE + sizeof(char)))) {
|
|
fclose(file);
|
|
fprintf(stderr, "Unable to allocate buffer.\nAborting.\n");
|
|
abort();
|
|
} else rawbuf[SXT_PPKP_MAXCSIZE] = '\0';
|
|
|
|
if(fread(rawbuf, stb.st_size, 1, file) != 1) {
|
|
fprintf(stderr, "Unable to read file '%s'\nAborting.\n", argv[1]);
|
|
free(rawbuf);
|
|
fclose(file);
|
|
abort();
|
|
}
|
|
|
|
/* check for public tuple */
|
|
if(strstr(rawbuf, "'public")) public = 1;
|
|
else public = 0;
|
|
|
|
/* determined give up */
|
|
memset(rawbuf, 0, stb.st_size);
|
|
free(rawbuf);
|
|
fclose(file);
|
|
|
|
if(public) {
|
|
r = sxtkey_import_public_file(argv[1], &key);
|
|
if(r != SXT_SUCCESS) {
|
|
fprintf(stderr, "Unable to import public key from '%s' (%d)\nAborting.\n",
|
|
argv[1], r);
|
|
abort();
|
|
}
|
|
} else {
|
|
r = sxtkey_import_priv_file(argv[1], NULL, passkey_promt,
|
|
"Private key encrypted.\nPasskey phrase:", &key);
|
|
if(r != SXT_SUCCESS) {
|
|
fprintf(stderr, "Unable to import private key from '%s' (%d)\nAborting.\n",
|
|
argv[1], r);
|
|
abort();
|
|
}
|
|
}
|
|
|
|
/* print key info */
|
|
__print_keyinfo(stdout, key, argv[1]);
|
|
/* free a key */
|
|
sxtkey_free(key);
|
|
|
|
return 0;
|
|
}
|