#!/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 "" 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 "" 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()