From e0d446edb3804757fedf17adb23214f9db2a91fe Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Wed, 6 Jul 2016 02:17:19 +0300 Subject: [PATCH] tools: sxtkeyinfo added; --- tools/.gitignore | 1 + tools/Makefile.am | 7 +- tools/misc.c | 70 +++++++++++++++++ tools/sxtkeygen.c | 31 +------- tools/sxtkeyinfo.c | 188 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 266 insertions(+), 31 deletions(-) create mode 100644 tools/misc.c create mode 100644 tools/sxtkeyinfo.c diff --git a/tools/.gitignore b/tools/.gitignore index 75848d7..3c77e9f 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1 +1,2 @@ sxtkeygen +sxtkeyinfo diff --git a/tools/Makefile.am b/tools/Makefile.am index 8af1fe5..ee1794e 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -13,8 +13,11 @@ AM_CFLAGS = -Wall -g # where to find libraries libslocal = ../sxt/.libs/libsxt.la -bin_PROGRAMS = sxtkeygen +bin_PROGRAMS = sxtkeygen sxtkeyinfo -sxtkeygen_SOURCES = sxtkeygen.c +sxtkeygen_SOURCES = misc.c sxtkeygen.c sxtkeygen_LDADD = $(OPENSSL_LIBS) $(LIBSXT_LIBS) $(libslocal) +sxtkeyinfo_SOURCES = misc.c sxtkeyinfo.c +sxtkeyinfo_LDADD = $(OPENSSL_LIBS) $(LIBSXT_LIBS) $(libslocal) + diff --git a/tools/misc.c b/tools/misc.c new file mode 100644 index 0000000..7d2a708 --- /dev/null +++ b/tools/misc.c @@ -0,0 +1,70 @@ +/* + * Secure X Message Passing Library tools. + * + * (c) Alexander Vdolainen 2016 + * + * 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 ."; + * + * support functions for sxmp tools + */ + +#include +#include +#define __USE_GNU +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +int passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv) +{ + char *promt = (char *)priv; + int sym, len = 0; + struct termios tio; + + /* print promt */ + fprintf(stdout, "%s", promt); + fflush(stdout); + + tcgetattr(STDIN_FILENO, &tio); + /* disable echo */ + tio.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &tio); + + while((sym = fgetc(stdin)) != '\n') { + passbuf[len] = sym; + if(len + 1 == p_len) break; + else len++; + } + + /* enable again */ + tio.c_lflag |= (ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &tio); + fprintf(stdout, "\n"); + + return SXT_SUCCESS; +} diff --git a/tools/sxtkeygen.c b/tools/sxtkeygen.c index a573d92..c0c30c3 100644 --- a/tools/sxtkeygen.c +++ b/tools/sxtkeygen.c @@ -78,34 +78,7 @@ static void __help_print(FILE *fso, const char *fmtname) return; } -static int __passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv) -{ - char *promt = (char *)priv; - int sym, len = 0; - struct termios tio; - - /* print promt */ - fprintf(stdout, "%s", promt); - fflush(stdout); - - tcgetattr(STDIN_FILENO, &tio); - /* disable echo */ - tio.c_lflag &= ~(ICANON | ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &tio); - - while((sym = fgetc(stdin)) != '\n') { - passbuf[len] = sym; - if(len + 1 == p_len) break; - else len++; - } - - /* enable again */ - tio.c_lflag |= (ICANON | ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &tio); - fprintf(stdout, "\n"); - - return SXT_SUCCESS; -} +extern int passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv); int main(int argc, char **argv) { @@ -262,7 +235,7 @@ int main(int argc, char **argv) if(!encrypt) { /* will not encrypt */ r = sxtkey_export_priv_file(privkey, fullpath, NULL, NULL, NULL); } else { - r = sxtkey_export_priv_file(privkey, fullpath, NULL, __passkey_promt, + r = sxtkey_export_priv_file(privkey, fullpath, NULL, passkey_promt, (void *)"Enter passkey phrase:"); } diff --git a/tools/sxtkeyinfo.c b/tools/sxtkeyinfo.c new file mode 100644 index 0000000..0a259e1 --- /dev/null +++ b/tools/sxtkeyinfo.c @@ -0,0 +1,188 @@ +/* + * Secure X Message Passing Library tools. + * + * (c) Alexander Vdolainen 2016 + * + * 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 ."; + * + * sxtkeyinfo - sxt key container information tool for get info on the keys + */ + +#include +#include +#define __USE_GNU +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#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 \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; +}