Example #1
0
 def __init__(self, filename=None):
     from pskc.encryption import Encryption
     from pskc.mac import MAC
     self.version = None
     self.id = None
     self.encryption = Encryption(self)
     self.mac = MAC(self)
     self.devices = []
     if filename is not None:
         from pskc.parser import PSKCParser
         PSKCParser.parse_file(self, filename)
     else:
         self.version = '1.0'
Example #2
0
 def __init__(self, filename=None):
     from pskc.encryption import Encryption
     from pskc.exceptions import ParseError
     from pskc.mac import MAC
     self.version = None
     self.id = None
     self.encryption = Encryption()
     self.mac = MAC(self)
     self.keys = []
     if filename is not None:
         from pskc.xml import parse
         try:
             tree = parse(filename)
         except Exception:
             raise ParseError('Error parsing XML')
         self.parse(tree.getroot())
     else:
         self.version = '1.0'
Example #3
0
class PSKC(object):
    """Wrapper module for parsing a PSKC file.

    Instances of this class provide the following attributes:

      version: the PSKC format version used (1.0)
      id: identifier
      encryption: information on used encryption (Encryption instance)
      mac: information on used MAC method (MAC instance)
      keys: list of keys (Key instances)
    """

    def __init__(self, filename=None):
        from pskc.encryption import Encryption
        from pskc.exceptions import ParseError
        from pskc.mac import MAC
        self.version = None
        self.id = None
        self.encryption = Encryption()
        self.mac = MAC(self)
        self.keys = []
        if filename is not None:
            from pskc.xml import parse
            try:
                tree = parse(filename)
            except Exception:
                raise ParseError('Error parsing XML')
            self.parse(tree.getroot())
        else:
            self.version = '1.0'

    def parse(self, container):
        """Read information from the provided <KeyContainer> tree."""
        from pskc.exceptions import ParseError
        from pskc.key import Key
        from pskc.xml import find, findall
        if not container.tag.endswith('KeyContainer'):
            raise ParseError('Missing KeyContainer')
        # the version of the PSKC schema
        self.version = container.get('Version')
        if self.version != '1.0':
            raise ParseError('Unsupported version %r' % self.version)
        # unique identifier for the container
        self.id = container.get('Id')
        # handle EncryptionKey entries
        self.encryption.parse(find(container, 'pskc:EncryptionKey'))
        # handle MACMethod entries
        self.mac.parse(find(container, 'pskc:MACMethod'))
        # handle KeyPackage entries
        for key_package in findall(container, 'pskc:KeyPackage'):
            self.keys.append(Key(self, key_package))

    def make_xml(self):
        from pskc.xml import mk_elem
        container = mk_elem('pskc:KeyContainer', Version=self.version,
                            Id=self.id)
        for key in self.keys:
            key.make_xml(container)
        return container

    def add_key(self, **kwargs):
        """Create a new key instance for the PSKC file.

        The new key is initialised with properties from the provided keyword
        arguments if any."""
        from pskc.key import Key
        key = Key(self)
        self.keys.append(key)
        # assign the kwargs as key properties
        for k, v in kwargs.items():
            if not hasattr(key, k):
                raise AttributeError()
            setattr(key, k, v)
        return key

    def write(self, filename):
        """Write the PSKC file to the provided file."""
        from pskc.xml import tostring
        if hasattr(filename, 'write'):
            filename.write(tostring(self.make_xml()))
        else:
            with open(filename, 'wb') as output:
                self.write(output)