tools: sxtkeyinfo added;

master
Alexander Vdolainen 8 years ago
parent e5a6b97dcb
commit e0d446edb3

1
tools/.gitignore vendored

@ -1 +1,2 @@
sxtkeygen sxtkeygen
sxtkeyinfo

@ -13,8 +13,11 @@ AM_CFLAGS = -Wall -g
# where to find libraries # where to find libraries
libslocal = ../sxt/.libs/libsxt.la 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) sxtkeygen_LDADD = $(OPENSSL_LIBS) $(LIBSXT_LIBS) $(libslocal)
sxtkeyinfo_SOURCES = misc.c sxtkeyinfo.c
sxtkeyinfo_LDADD = $(OPENSSL_LIBS) $(LIBSXT_LIBS) $(libslocal)

@ -0,0 +1,70 @@
/*
* 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/>.";
*
* support functions for sxmp tools
*/
#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/rdb.h>
#include <sxt/sxtkey.h>
#include <sxt/sxt.h>
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;
}

@ -78,34 +78,7 @@ static void __help_print(FILE *fso, const char *fmtname)
return; return;
} }
static int __passkey_promt(char *passbuf, size_t p_len, int cnf, void *priv) extern 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;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -262,7 +235,7 @@ int main(int argc, char **argv)
if(!encrypt) { /* will not encrypt */ if(!encrypt) { /* will not encrypt */
r = sxtkey_export_priv_file(privkey, fullpath, NULL, NULL, NULL); r = sxtkey_export_priv_file(privkey, fullpath, NULL, NULL, NULL);
} else { } 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:"); (void *)"Enter passkey phrase:");
} }

@ -0,0 +1,188 @@
/*
* 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/rdb.h>
#include <sxt/sxtkey.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;
}
Loading…
Cancel
Save