From 6ebf83f6d60842f0c620789bc2da958bf5d3bced Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Dec 2003 21:53:17 +0000 Subject: included c sources git-svn-id: file:///home/lennart/svn/public/seppl/trunk@17 91a2fd9b-5dcb-0310-a70a-d71e310228e6 --- Makefile.am | 2 +- configure.ac | 2 +- utils-python/Makefile.am | 3 +- utils/Makefile.am | 21 +++++++ utils/seppl-gen-key.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++ utils/seppl_common.c | 123 ++++++++++++++++++++++++++++++++++++++++ utils/seppl_common.h | 36 ++++++++++++ 7 files changed, 329 insertions(+), 3 deletions(-) create mode 100644 utils/Makefile.am create mode 100644 utils/seppl-gen-key.c create mode 100644 utils/seppl_common.c create mode 100644 utils/seppl_common.h diff --git a/Makefile.am b/Makefile.am index 61456ee..7acd6a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,7 +19,7 @@ # This Makefile is NOT created by automake! EXTRA_DIST = bootstrap.sh README LICENSE -SUBDIRS=kernel libs utils-python conf doc +SUBDIRS=kernel libs utils-python utils conf doc MAINTAINERCLEANFILES = README noinst_DATA = README diff --git a/configure.ac b/configure.ac index aeceb3e..16307c1 100644 --- a/configure.ac +++ b/configure.ac @@ -178,5 +178,5 @@ fi AM_CONDITIONAL([USE_XMLTOMAN], [test "x$xmltoman" = xyes]) -AC_CONFIG_FILES([kernel/Makefile libs/Makefile Makefile utils-python/Makefile conf/Makefile doc/Makefile doc/README.html]) +AC_CONFIG_FILES([kernel/Makefile libs/Makefile Makefile utils-python/Makefile utils/Makefile conf/Makefile doc/Makefile doc/README.html]) AC_OUTPUT diff --git a/utils-python/Makefile.am b/utils-python/Makefile.am index a16367f..15eee8b 100644 --- a/utils-python/Makefile.am +++ b/utils-python/Makefile.am @@ -16,7 +16,8 @@ # along with seppl; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -sbin_SCRIPTS=seppl-ls seppl-gen-key +sbin_SCRIPTS=seppl-ls +noinst_SCRIPTS=seppl-gen-key python_PYTHON=seppl_common.py EXTRA_DIST=seppl-ls seppl-gen-key diff --git a/utils/Makefile.am b/utils/Makefile.am new file mode 100644 index 0000000..6f4d426 --- /dev/null +++ b/utils/Makefile.am @@ -0,0 +1,21 @@ +# $Id: Makefile.am 16 2003-12-04 21:09:48Z lennart $ +# +# This file is part of seppl. +# +# seppl is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# seppl 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 seppl; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +sbin_PROGRAMS=seppl-gen-key +seppl_gen_key_SOURCES=seppl-gen-key.c seppl_common.c seppl_common.h + diff --git a/utils/seppl-gen-key.c b/utils/seppl-gen-key.c new file mode 100644 index 0000000..fc9afc0 --- /dev/null +++ b/utils/seppl-gen-key.c @@ -0,0 +1,145 @@ +/* $Id: seppl.c 16 2003-12-04 21:09:48Z lennart $ */ + +/*** + This file is part of seppl + + seppl is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + seppl 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 seppl; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA +***/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "seppl_common.h" + +int genkey(int a, const char *keyname, int as_xml) { + struct Algs *alg; + int fd; + int count, sofar; + char *key; + + if (!(alg = find_alg_by_number(a))) { + fprintf(stderr, "Algorithm '%i' not found.\n", a); + return -1; + } + + if (!(key = (char*) malloc(alg->bits/8))) { + perror("Failed to allocate memory"); + return -1; + } + + if ((fd = open( "/dev/random", O_RDONLY)) < 0) { + perror("Failed to open /dev/random"); + free(key); + return -1; + } + + sofar = 0; + while (sofar < alg->bits/8 ) { + count = read( fd, &key[sofar], alg->bits/8 - sofar ); + if (count <= 0) { + fprintf(stderr, "Failed to read key data.\n"); + free(key); + close(fd); + return -1; + } + sofar += count; + } + + close(fd); + + if (as_xml) { + printf("\n"); + dump_key_xml(a, keyname, key); + printf("\n"); + } else + dump_key_bin(a, keyname, key); + + free(key); + + return 0; +} + +static struct option long_options[] = { + { "algorithm", 1, 0, 'a' }, + { "bits", 1, 0, 'b' }, + { "help", 0, 0, 'h' }, + { "name", 1, 0, 'n' }, + { "no-xml", 0, 0, 'x' }, +}; + +/* default values */ +char algorithm[32] = "aes"; +char keyname[8] = "def"; +int bits = 128; +int asxml = 1; + +void usage(void) { + printf("seppl-gen-key:\n" + " -h --help Show this help\n" + " -x --no-xml No xml output\n" + " -a --algorithm ALGORITHM Specify algorithm (%s)\n" + " -b --bits BITS Specify key length (%d)\n" + " -n --name NAME Specify key name (%s)\n", algorithm, bits, keyname); +} + +int main(int argc, char *argv[]) { + int c; + int option_index = 0; + int a; + + while((c = getopt_long( argc, argv, "hxa:b:n:", long_options, &option_index )) != -1) { + + switch( c ) { + case 'a': + /* algorithm */ + strncpy( algorithm, optarg, 31 ); + algorithm[31] = '\0'; + break; + case 'b': + /* bits */ + bits = atoi( optarg ); + break; + case 'h': + /* help */ + usage(); + return 1; + case 'n': + /* name */ + strncpy( keyname, optarg, 7 ); + keyname[7] = '\0'; + break; + case 'x': + /* no-xml */ + asxml = 0;; + break; + } + } + + + + if ((a = find_alg_by_name( algorithm, bits )) < 0) { + fprintf( stderr, "ERROR: Cipher not available\n" ); + return 2; + } + + return genkey(a, keyname, asxml) < 0 ? 3 : 0; +} diff --git a/utils/seppl_common.c b/utils/seppl_common.c new file mode 100644 index 0000000..1506a52 --- /dev/null +++ b/utils/seppl_common.c @@ -0,0 +1,123 @@ +/* $Id: seppl.c 16 2003-12-04 21:09:48Z lennart $ */ + +/*** + This file is part of seppl + + seppl is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + seppl 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 seppl; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA +***/ + +#include +#include +#include "seppl_common.h" + +#define NUMALGS 2 + +struct Algs algs[NUMALGS] = { + { 0, "aes", 128 }, + { 1, "aes", 192 }, +}; + +struct Algs *find_alg_by_number( int a ) { + if( a < 0 || a >= NUMALGS ) + return NULL; + + return &algs[a]; +} + +int find_alg_by_name(const char *n, int b) { + int i; + + for (i = 0; i < NUMALGS; i++) + if (!strcmp(n, algs[i].name) && b == algs[i].bits) + return i; + + return -1; +} + +int dump_key_xml(int a, const char *keyname, const unsigned char *key) { + struct Algs *alg; + int i; + int keylen; + const unsigned char *ch; + + if (!(alg = find_alg_by_number( a ))) { + fprintf( stderr, "Error: Algorithm not found.\n" ); + return -1; + } + + keylen = alg->bits / 8; + + printf("\n" + " "); + + for (ch = keyname; *ch; ch++) { /* escape XML-special chars */ + switch( *ch ) { + case '&': + printf( "&" ); + break; + case '<': + printf( "<" ); + break; + case '>': + printf( ">" ); + break; + default: + putchar(*ch); + break; + } + } + + printf("\n" + " %s\n" + " %u\n" + " ", alg->name, alg->bits); + + for (i = 0; i < keylen-1; i++) + printf("%02x:", key[i]); + + printf("%02x" + "\n" + "\n", key[keylen-1]); + + return 0; +} + +int dump_key_bin (int a, const char *keyname, const unsigned char *key) { + struct Algs *alg; + int i; + int keylen; + + if (!(alg = find_alg_by_number(a))) { + fprintf( stderr, "Error: Algorithm not found.\n" ); + return -1; + } + + keylen = alg->bits / 8; + + /* algorithm number */ + putchar(a); + + /* key name padded with '\0' characters */ + printf("%s", keyname); + for (i = strlen(keyname); i < 7; i++ ) + putchar(0); + + /* key data */ + for( i = 0; i < keylen; i++ ) + putchar(key[i]); + + return 0; +} diff --git a/utils/seppl_common.h b/utils/seppl_common.h new file mode 100644 index 0000000..0a64732 --- /dev/null +++ b/utils/seppl_common.h @@ -0,0 +1,36 @@ +#ifndef foosepplcommonhfoo +#define foosepplcommonhfoo + +/* $Id: seppl.c 16 2003-12-04 21:09:48Z lennart $ */ + +/*** + This file is part of seppl + + seppl is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + seppl 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 seppl; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA +***/ + +struct Algs { + int i; + char *name; + int bits; +}; + +struct Algs *find_alg_by_number(int a); +int find_alg_by_name(const char *n, int b); +int dump_key_xml(int a, const char *keyname, const unsigned char *key); +int dump_key_bin(int a, const char *keyname, const unsigned char *key); + +#endif -- cgit