def read_gateway_privkey( password, gateway_name ): path = gateway_privkey_path( gateway_name ) encrypted_privkey_json = storage.read_file( path, volume=None ) try: encrypted_privkey = storage.json_to_tuple( keys.EncryptedPrivateKey, encrypted_privkey_json ) except Exception, e: log.exception(e) log.error("Failed to unserialize encrypted private gateway key") return None
def read_contact_from_path( privkey_str, contact_path ): contact_json_str = storage.read_encrypted_file( privkey_str, contact_path ) if contact_json_str is None: log.error( "Could not read contact %s" % contact_path ) return None try: contact = storage.json_to_tuple( SyndicateContact, contact_json_str ) except Exception, e: log.error("Failed to load contact %s" % contact_path ) log.exception(e) return None
def read_incoming_message( privkey_str, msg_timestamp, msg_id, volume=None ): mpath = incoming_message_path( msg_timestamp, msg_id ) msg_json = storage.read_encrypted_file( privkey_str, mpath, volume=volume ) if msg_json is None: log.error("Failed to read incoming message %s" % message_handle( msg_timestamp, msg_id )) return None try: msg = storage.json_to_tuple( SyndicateIncomingMessage, msg_json ) except Exception, e: log.error("Failed to parse incoming message") log.exception(e) return None
def load_public_key( key_name, syndicate_user_pubkey ): key_path = make_key_local_path( key_name + ".pub" ) pubkey_json = storage.read_file( key_path, volume=None ) if pubkey_json is None: log.error("Failed to load public key") return False try: pubkey = storage.json_to_tuple( SignedPublicKey, pubkey_json ) except Exception, e: log.error("Failed to unserialize signed public key") log.exception(e) return False
def load_private_key_from_path( key_path, password, local ): encrypted_privkey_str = None if local: encrypted_privkey_str = storage.read_file( key_path, volume=None ) else: encrypted_privkey_str = storage.read_file( key_path ) if encrypted_privkey_str is None: log.error("Failed to load key from %s" % key_path ) return None try: encrypted_private_key = storage.json_to_tuple( EncryptedPrivateKey, encrypted_privkey_str ) except Exception, e: log.error("Failed to unserialize private key") return None
def read_stored_message( privkey_str, folder, msg_timestamp, msg_id, volume=None, receiver_pubkey_pem=None ): if receiver_pubkey_pem is None: pkey = CryptoKey.importKey( privkey_str ) receiver_pubkey_pem = pkey.publickey().exportKey() mpath = stored_message_path( receiver_pubkey_pem, folder, msg_timestamp, msg_id ) if not storage.path_exists( mpath, volume=volume ): log.error("No message at %s" % mpath ) return None msg_json = storage.read_encrypted_file( privkey_str, mpath, volume=volume ) if msg_json is None: log.error("Failed to read message") return None try: msg = storage.json_to_tuple( SyndicateMessage, msg_json ) except Exception, e: log.error("Failed to parse message") log.exception(e) return None
def validate_and_parse_incoming_message( pubkey_str, privkey_str, my_addr, encrypted_incoming_message ): if encrypted_incoming_message.receiver_addr != my_addr: log.error("Message is not for me") return False sender_addr = encrypted_incoming_message.sender_addr verified = False # do we have a contact? contact_rec = contact.read_contact( pubkey_str, privkey_str, sender_addr ) if contact_rec is None: # no contact log.warning("Message from %s could not be verified." % sender_addr ) raise Exception("FIXME: Get %s's public key here" % sender_addr ) verified = False else: # check signature verified = verify_message( contact_rec.pubkey_pem, EncryptedIncomingMessage, encrypted_incoming_message ) if not verified: raise Exception("Message is not authentically from %s" % contact_rec.addr) # attempt to decrypt incoming_message_json = storage.decrypt_data( contact_rec.pubkey_pem, privkey_str, encrypted_incoming_message.incoming_message_ciphertext ) if incoming_message_json is None: log.error("Failed to decrypt incoming message") return False # attempt to parse try: incoming_message = storage.json_to_tuple( SyndicateIncomingMessage, incoming_message_json ) except Exception, e: log.exception(e) log.error("Failed to unserialize message from %s" % sender_addr ) return False
try: msg_path = stored_message_path( receiver_pubkey_str, SENT_FOLDER, incoming_message.timestamp, incoming_message.id ) msg_json = storage.read_encrypted_file( receiver_privkey_str, msg_path, volume=vol_inst, sender_pubkey_pem=sender_pubkey_str ) except Exception, e: log.exception(e) log.error("Failed to read %s" % msg_path ) return None if msg_json is None: log.error("Failed to read message from Volume") return None # unserialize try: msg = storage.json_to_tuple( SyndicateMessage, msg_json ) except Exception, e: log.exception(e) log.error("Failed to parse %s" % msg_path ) return None return msg #------------------------- def sender_volume_from_incoming_message( incoming_message, gateway_privkey_pem, storage_root ): try: parsed_addr = contact.parse_addr( incoming_message.sender_addr ) except Exception, e: log.exception(e) log.error("Failed to parse %s" % incoming_message.sender_addr )