def parseHash(self, hashstr): """Create a hash object from user-supplied input""" if hashstr[0] == '(': hash = sexp.parseText(hashstr) if not isinstance(hash, spki.Hash): raise ValueError, "invalid hash object: %s" % hash else: digest = sexp.b64_to_str(hashstr) hash = spki.Hash('md5', digest) return hash
def _cleanup(elts): """Decode b64 encoding uses in test/sexps.py""" from pisces.spkilib import sexp clean = [] for elt in elts: if not sexp.atom(elt): elt = _cleanup(elt) if elt[0] == '|': elt = sexp.b64_to_str(elt) clean.append(elt) return clean
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