def test_decode_b64_int(self): self.assertEqual(decode_b64_int(b'AA=='), 0) self.assertEqual(decode_b64_int(b'////'), 2 ** 24 - 1) self.assertRaises(TypeError, decode_b64_int, '') self.assertRaises(TypeError, decode_b64_int, None) self.assertRaises(TypeError, decode_b64_int, 123) self.assertRaises(ValueError, decode_b64_int, b'\x01')
def get_identities(self, fingerprints=None, time_cookie=None): """Get a list of all identities with specified fingerprints. If the parameter is None, all identities are returned. If a time cookie is specified, all identities in the database after the time specified by the time cookie will be returned. """ if fingerprints is not None and not hasattr(fingerprints, "__iter__"): raise TypeError with sqlite3.connect(self._db_file) as conn: c = conn.cursor() if time_cookie is not None: if not isinstance(time_cookie, bytes): raise TypeError if len(time_cookie) == 0: raise ValueError """Assert that time_cookie is a valid cookie""" if ( c.execute( "SELECT count(*) FROM time_cookies WHERE cookie = ?", (self._encode_id(time_cookie),) ).fetchone()[0] == 0 ): raise ValueError c.execute( """SELECT fingerprint, dsa_y, dsa_g, dsa_p, dsa_q, rsa_n, rsa_e, nick FROM identities JOIN time_cookies ON identities.cookieid = time_cookies.id WHERE time_cookies.id > (SELECT id FROM time_cookies WHERE cookie=?)""", (self._encode_id(time_cookie),), ) else: c.execute("""SELECT fingerprint, dsa_y, dsa_g, dsa_p, dsa_q, rsa_n, rsa_e, nick FROM identities""") id_rows = c.fetchall() current_tc = self._get_last_time_cookie(c) ids = [ Identity( DSA_key(decode_b64_int(id[1]), decode_b64_int(id[2]), decode_b64_int(id[3]), decode_b64_int(id[4])), RSA_key(decode_b64_int(id[5]), decode_b64_int(id[6])), ) for id in id_rows if id is not None ] if fingerprints is not None: ids = [id for id in ids if id.fingerprint in fingerprints] return (current_tc, ids)
def get_private_identity(self, fingerprint): """Get a private identity from the data base""" if not isinstance(fingerprint, bytes): raise TypeError with sqlite3.connect(self._db_file) as conn: c = conn.cursor() c.execute( """SELECT dsa_y, dsa_g, dsa_p, dsa_q, dsa_x, rsa_n, rsa_e, rsa_d FROM identities JOIN private_identities ON identities.fingerprint == private_identities.fingerprint WHERE private_identities.fingerprint == ?""", (self._encode_id(fingerprint),), ) id = c.fetchone() if id is None: raise ValueError return PrivateIdentity( DSA_key( decode_b64_int(id[0]), decode_b64_int(id[1]), decode_b64_int(id[2]), decode_b64_int(id[3]), decode_b64_int(id[4]), ), RSA_key(decode_b64_int(id[5]), decode_b64_int(id[6]), decode_b64_int(id[7])), )
def _string2message(mstr): """Parse the string and create a message""" TEXT_INDEX, TIMESTAMP_INDEX, RECEIVER_INDEX, SENDER_INDEX, SIGNATURE_INDEX = (0, 1, 2, 3, 4) mparts = mstr.split(_SUB_FIELD_SEPARATOR) if len(mparts) != 5: raise ProtocolParseError if (mparts[SENDER_INDEX] != '' and mparts[SIGNATURE_INDEX] == '') or (mparts[SENDER_INDEX] == '' and mparts[SIGNATURE_INDEX] != ''): raise ProtocolParseError receiver = None if mparts[RECEIVER_INDEX] == '' else decode_b64_bytes(mparts[RECEIVER_INDEX].encode()) text = decode_b64_bytes(mparts[TEXT_INDEX].encode()) timestamp = None if mparts[TIMESTAMP_INDEX] == '' else decode_b64_int(mparts[TIMESTAMP_INDEX].encode()) textstr = text.decode() if receiver is None else text # Decode unless encrypted sender = None if mparts[SENDER_INDEX] == '' else decode_b64_bytes(mparts[SENDER_INDEX].encode()) signature = None if mparts[SIGNATURE_INDEX] == '' else decode_b64_bytes(mparts[SIGNATURE_INDEX].encode()) return Message(textstr, timestamp, receiver, sender, signature)
def _string2identity(identitystr): """Parse the string and create a identity""" RSA_N_INDEX, RSA_E_INDEX, DSA_Y_INDEX, DSA_G_INDEX, DSA_P_INDEX, DSA_Q_INDEX = (0, 1, 2, 3, 4, 5) idparts = identitystr.split(_SUB_FIELD_SEPARATOR) if len(idparts) != 6: raise ProtocolParseError rsa_key = RSA_key(decode_b64_int(idparts[RSA_N_INDEX].encode()), decode_b64_int(idparts[RSA_E_INDEX].encode())) dsa_key = DSA_key(decode_b64_int(idparts[DSA_Y_INDEX].encode()), decode_b64_int(idparts[DSA_G_INDEX].encode()), decode_b64_int(idparts[DSA_P_INDEX].encode()), decode_b64_int(idparts[DSA_Q_INDEX].encode())) return Identity(dsa_key, rsa_key)