diff options
Diffstat (limited to 'utils-python')
-rw-r--r-- | utils-python/Makefile.am | 22 | ||||
-rwxr-xr-x | utils-python/seppl-gen-key | 110 | ||||
-rwxr-xr-x | utils-python/seppl-ls | 198 | ||||
-rw-r--r-- | utils-python/seppl_common.py | 105 |
4 files changed, 435 insertions, 0 deletions
diff --git a/utils-python/Makefile.am b/utils-python/Makefile.am new file mode 100644 index 0000000..a16367f --- /dev/null +++ b/utils-python/Makefile.am @@ -0,0 +1,22 @@ +# $Id$ +# +# 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_SCRIPTS=seppl-ls seppl-gen-key +python_PYTHON=seppl_common.py + +EXTRA_DIST=seppl-ls seppl-gen-key diff --git a/utils-python/seppl-gen-key b/utils-python/seppl-gen-key new file mode 100755 index 0000000..1690bb1 --- /dev/null +++ b/utils-python/seppl-gen-key @@ -0,0 +1,110 @@ +#!/usr/bin/python + +# $Id$ +# +# 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. + +from seppl_common import * +import getopt, sys + +def genkey(a, name, f): + + n, b = find_alg_by_number(a) + + if b == 0: + return -1 + + fd = open("/dev/random", "r+") + key = fd.read(b/8) + fd.close() + + if (len(key) != b/8): + sys.stderr("ERROR: Cannot generate randomness.\n") + return + + if f == dump_key_xml: + print "<seppl-keyring>" + + f(a, name, key) + + if f == dump_key_xml: + print "</seppl-keyring>" + + return 0 + +algorithm = "aes" +bits = 128 +name = "def" +func = dump_key_xml + +def usage(): + global algorithm, bits, name, func + x = { dump_key_xml : "disable", dump_key_bin : "enable" } + + print "%s:" % sys.argv[0] + print " -h --help Show this help" + print " -x --no-xml No xml output (%s)" % x[func] + print " -a --algorithm ALGORITHM Specify algorithm (%s)" % algorithm + print " -b --bits BITS Specify key length (%s)" % bits + print " -n --name NAME Specify key name (%s)" % name + + +def main(): + global algorithm, bits, name, func + + try: + opts, args = getopt.getopt(sys.argv[1:], "hxa:b:n:", ["help", "no-xml", "algorithm=", "bits=", "name="]) + except getopt.GetoptError: + usage() + sys.exit(1) + + u = 0; + + for o, a in opts: + if o in ("-h", "--help"): + u = 1 + + if o in ("-x", "--no-xml"): + func = dump_key_bin + + if o in ("-a", "--algorithm"): + algorithm = a + + if o in ("-b", "--bits"): + bits = int(a) + + if o in ("-n", "--name"): + name = a[:7] + + if u: + usage() + sys.exit(0) + + + a = find_alg_by_name(algorithm, bits) + + if a == -1: + sys.stderr.write("ERROR: Cipher not available\n") + sys.exit(2) + + genkey(a, name, func) + +if __name__ == "__main__": + main() + + + diff --git a/utils-python/seppl-ls b/utils-python/seppl-ls new file mode 100755 index 0000000..0b0decb --- /dev/null +++ b/utils-python/seppl-ls @@ -0,0 +1,198 @@ +#!/usr/bin/python + +# $Id$ +# +# 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. + +import getopt, sys + +from seppl_common import * +import xml.sax, xml.sax.handler + +name = None +algorithm = None +bits = 0 +reverse = 0 +PROC_FILE_NAME = "/proc/net/seppl_keyring" +file = None + +class KeyContentHandler(xml.sax.handler.ContentHandler): + + last = None + + def startElement(self, name, attrs): + self.last = name + + if name != "key": + return + + self.name = "" + self.algorithm = "" + self.bits = "" + self.data = "" + + def endElement(self, n): + global name, algorithm, bits + self.last = None + + if n != "key": + return + + if self.name == "": + self.name = "def" + + if self.algorithm == "": + self.algorithm = "aes" + + if self.bits == "": + self.bits = 128 + else: + self.bits = int(self.bits) + + a = find_alg_by_name(self.algorithm, self.bits) + if a == -1: + raise xml.sax.SAXNotRecognizedException("Cipher not known") + + key = parse_key(self.data) + if key is None or len(key) != self.bits/8: + raise xml.sax.SAXNotRecognizedException("Could not parse key data.") + + if (name is None or name == self.name) and (algorithm is None or algorithm == self.algorithm) and (bits == 0 or bits == self.bits): + dump_key_bin(a, self.name, key) + + def characters(self, content): + content = content.strip() + + if len(content) == 0: + return + + if self.last == "name": + self.name += content + return + if self.last == "algorithm": + self.algorithm += content + return + if self.last == "bits": + self.bits += content + return + if self.last == "data": + self.data += content + return + + raise xml.sax.SAXNotRecognizedException("Malformed XML structure |%s|%s|" % (self.last, content)) + +def usage(): + global name, algorithm, bits, reverse, file + + print "%s:" % sys.argv[0] + print " -h --help Show this help" + print " -r --reverse Convert XML to binary, instead of the other way round (%s)" % { 0 : "disabled", 1 : "enabled" }[reverse] + print " -a --algorithm ALGORITHM Show only keys with algorithm (%s)" % algorithm + print " -b --bits BITS Show only keys with bit length (%s)" % bits + print " -n --name NAME Show only keys with name (%s)" % name + print " -f --file FILE Specify file (- for STDIN) (%s)" % file + pass + +def main(): + global name, algorithm, bits, reverse, file + + try: + opts, args = getopt.getopt(sys.argv[1:], "hn:a:b:rf:", ["help", "name=", "algorithm=", "bits=", "reverse", "file"]) + except getopt.GetoptError: + usage() + sys.exit(2) + + u = 0 + + for o, a in opts: + if o in ("-h", "--help"): + u = 1 + + if o in ("-n", "--name"): + name = a[:7] + + if o in ("-a", "--algorithm"): + algorithm = a + + if o in ("-b", "--bits"): + bits = int(a) + + if o in ("-r", "--reverse"): + reverse = 1 + + if o in ("-f", "--file"): + file = a + + if file is None: + if reverse: + file = "-" + else: + file = PROC_FILE_NAME + + if u: + usage() + sys.exit(0) + + + if file == "-": + buf = sys.stdin.read(); + else: + try: + fd = open(file, "r+") + buf = fd.read(); + fd.close() + except IOError, e: + sys.stderr.write("Could not open proc-file (%s).\n" % str(e)) + sys.exit(2) + + + if len(buf) == 0: + sys.exit(0) + + if buf[0] == '<': + reverse = 1 + + if not reverse: + + print "<seppl-keyring>" + while len(buf) >= 8: + a, _name = unpack("B7s", buf[:8]) + n, b = find_alg_by_number(a) + + if (b == 0): + sys.stderr.write("ERROR: Unknown cipher. Please update.\n") + break + + _name = _name.replace("\000", "") + + if (name is None or name == _name) and (algorithm is None or algorithm == n) and (bits == 0 or b == bits): + dump_key_xml(a, _name, buf[8:b/8+8]) + + buf = buf[8+b/8:] + print "</seppl-keyring>" + else: + try: + xml.sax.parseString(buf, KeyContentHandler()) + except xml.sax.SAXException, e: + sys.stderr.write("Parse error (%s)\n" %str(e)) + + +if __name__ == "__main__": + main() + + + diff --git a/utils-python/seppl_common.py b/utils-python/seppl_common.py new file mode 100644 index 0000000..59e7f96 --- /dev/null +++ b/utils-python/seppl_common.py @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +# $Id$ +# +# 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. + +from struct import * +import re, string, sys + +algs = { 0 : ('aes', 128), 1 : ('aes', 192) } + +def find_alg_by_number(a): + global algs + + if algs.has_key(a): + return algs[a]; + else: + return ('unknown', 0) + +def find_alg_by_name(n, b): + global algs + + for i in algs.keys(): + if algs[i][0] == n and algs[i][1] == b: + return i + + return -1 + +def strhex(s): + r = "" + for i in range(len(s)): + (b,) = unpack("B", s[i]) + r = "%s:%02x" % (r, b) + + return r[1:] + +def dump_key_xml(a, name, key): + alg, bits = find_alg_by_number(a) + + if bits == 0: + sys.stderr.write("ERROR: Algorithm not found\n") + return -1 + + print "<key>" + print " <name>%s</name>" % name + print " <algorithm>%s</algorithm>" % alg + print " <bits>%u</bits>" % bits + print " <data>%s</data>" % strhex(key) + print "</key>" + + return 0 + +def dump_key_bin(a, name, key): + alg, bits = find_alg_by_number(a) + + if bits == 0: + sys.stderr.write("ERROR: Algorithm not found\n") + return -1 + + if len(key) != bits/8: + sys.stderr.write("ERROR: Key has wrong size\n") + return -1 + + name = name.encode("iso8859-1", 'ignore') + + sys.stdout.write(pack("B7s", a, name[:7])) + sys.stdout.write(key) + sys.stdout.flush() + +def parse_key(data): + + k = "" + + r = re.compile("^([0-9A-Fa-f][0-9A-Fa-f])") + + while len(data) > 0: + m = r.match(data+":") + + if m == None: + return None + + k += pack("B", string.atoi(m.group(0), 16)) + + data = data[3:] + + return k + + + + + |