def parseHashOrName(buf): try: o = spki.parseText(buf) except sexp.ParseError: pass else: return o # apparently it wasn't a hash... base = getDefaultKey() return spki.FullyQualifiedName(base, (buf,))
def getConfFile(self): self.super_getConfFile() self.config.read("ttls.conf") self.acl = database.ACL(self.config.get('spki', 'acl')) self.port = self.config.getint('DEFAULT', 'port') self.host = self.config.get('server', 'host') if self.isClient: self.hash = spki.parseText(self.config.get('client', 'key')) else: private = self.config.get('server', 'key') self.hash = spkilib.config.parseHashOrName(private) # and muck with a class variable here if self.config.getboolean('DEFAULT', 'verbose'): self.verbose = 1 if self.verbose == 1: sexpsocket.SexpSocket.VERBOSE = 1
def parseKeyIdInput(buf, keystore, parseName=True): """Parses a string into a spki.Hash object String could be a sexp, a base 64 encoded version of the hash or a name Taken from the spkitool.py in pisces but doesn't use global variables and raises different exceptions. Args: buf: String to be parsed. keystore: KeyStore object. parseName: Bool. Returns: spki.Hash object Raises: ValueError: Raised if buf fails to parse NameError: Raised if buf is an unbound name. """ try: p = spki.parseText(buf) except sexp.ParseError: # It wasnt an sexp, try next potential format pass else: if spki.isa(p, spki.Hash): return p # Parse an MD5 hash in B64 representation # Will always be 24 chars long and end in == if len(buf) == 24 and buf[-2:] == '==': try: digest = sexp.b64_to_str(buf) p = spki.Hash('md5', digest) except binascii.Error: pass else: return p if not parseName: raise ValueError("Unable to parse %s to hash" % buf) ns = keystore.getDefaultKey() if ns is None: raise ValueError('No default key specified') certs = keystore.lookupName(buf, ns) matches = [] for seq in certs: for elt in seq: if isinstance(elt, spki.Cert) and elt.isNameCert(): subj = elt.getSubject().getPrincipal() if subj not in matches: matches.append(subj) l = len(matches) if l == 0: raise NameError('No key bound to name: %s' % buf) if l != 1: raise NameError('Ambiguous name: %s matches %d keys' % (buf, l)) p = matches[0] return p