def transfer_key(key_id, trusted_idurl, include_private=False, include_signature=False, timeout=10, result=None): """ Actually sending given key to remote user. """ if _Debug: lg.out(_DebugLevel, 'key_ring.transfer_key %s -> %s' % (key_id, trusted_idurl)) if not result: result = Deferred() recipient_id_obj = identitycache.FromCache(trusted_idurl) if not recipient_id_obj: lg.warn('not found "%s" in identity cache' % trusted_idurl) result.errback(Exception('not found "%s" in identity cache' % trusted_idurl)) return result key_alias, creator_idurl = my_keys.split_key_id(key_id) if not key_alias or not creator_idurl: lg.warn('wrong key_id') result.errback(Exception('wrong key_id')) return result if not my_keys.is_key_registered(key_id): lg.warn('unknown key: "%s"' % key_id) result.errback(Exception('unknown key: "%s"' % key_id)) return result key_object = my_keys.key_obj(key_id) try: key_json = my_keys.make_key_info( key_object, key_id=key_id, include_private=include_private, include_signature=include_signature, ) except Exception as exc: lg.exc() result.errback(exc) return result key_data = serialization.DictToBytes(key_json, values_to_text=True) block = encrypted.Block( BackupID=key_id, Data=key_data, SessionKey=key.NewSessionKey(session_key_type=key.SessionKeyType()), SessionKeyType=key.SessionKeyType(), # encrypt data using public key of recipient EncryptKey=lambda inp: recipient_id_obj.encrypt(inp), ) encrypted_key_data = block.Serialize() p2p_service.SendKey( remote_idurl=recipient_id_obj.getIDURL(), encrypted_key_data=encrypted_key_data, packet_id=key_id, callbacks={ commands.Ack(): lambda response, info: _on_transfer_key_response(response, info, key_id, result), commands.Fail(): lambda response, info: _on_transfer_key_response(response, info, key_id, result), None: lambda pkt_out: _on_transfer_key_response(None, None, key_id, result), }, timeout=timeout, ) return result
def transfer_private_key(key_id, idurl): if _Debug: lg.out(_DebugLevel, 'key_ring.transfer_private_key %s -> %s' % (key_id, idurl)) result = Deferred() recipient_id_obj = identitycache.FromCache(idurl) if not recipient_id_obj: lg.warn('not found "%s" in identity cache' % idurl) result.errback(Exception('not found "%s" in identity cache' % idurl)) return result key_alias, creator_idurl = my_keys.split_key_id(key_id) if not key_alias or not creator_idurl: lg.warn('wrong key_id') result.errback(Exception('wrong key_id')) return result key_object = my_keys.known_keys().get(key_id) if key_object is None: lg.warn('unknown key: "%s"' % key_id) result.errback(Exception('unknown key: "%s"' % key_id)) return result key_json = { 'key_id': key_id, 'alias': key_alias, 'creator': creator_idurl, 'fingerprint': str(key_object.fingerprint()), 'type': str(key_object.type()), 'ssh_type': str(key_object.sshType()), 'size': str(key_object.size()), 'public': str(key_object.public().toString('openssh')), 'private': str(key_object.toString('openssh')), } key_data = json.dumps(key_json) block = encrypted.Block( BackupID=key_id, Data=key_data, SessionKey=key.NewSessionKey(), # encrypt data using public key of recipient EncryptKey=lambda inp: recipient_id_obj.encrypt(inp), ) encrypted_key_data = block.Serialize() p2p_service.SendKey( remote_idurl=recipient_id_obj.getIDURL(), encrypted_key_data=encrypted_key_data, packet_id=key_id, callbacks={ commands.Ack(): lambda response, info: result.callback(response), commands.Fail(): lambda response, info: result.errback(Exception(response)), }, ) return result
def _do_transfer_key(key_id, idurl, include_private=False, timeout=10, result=None): if _Debug: lg.out(_DebugLevel, 'key_ring.transfer_key %s -> %s' % (key_id, idurl)) if not result: result = Deferred() recipient_id_obj = identitycache.FromCache(idurl) if not recipient_id_obj: lg.warn('not found "%s" in identity cache' % idurl) result.errback(Exception('not found "%s" in identity cache' % idurl)) return result key_alias, creator_idurl = my_keys.split_key_id(key_id) if not key_alias or not creator_idurl: lg.warn('wrong key_id') result.errback(Exception('wrong key_id')) return result key_object = my_keys.known_keys().get(key_id) if key_object is None: lg.warn('unknown key: "%s"' % key_id) result.errback(Exception('unknown key: "%s"' % key_id)) return result try: key_json = my_keys.make_key_info(key_object, key_id=key_id, include_private=include_private) except Exception as exc: lg.exc() result.errback(exc) return result key_data = json.dumps(key_json) block = encrypted.Block( BackupID=key_id, Data=key_data, SessionKey=key.NewSessionKey(), # encrypt data using public key of recipient EncryptKey=lambda inp: recipient_id_obj.encrypt(inp), ) encrypted_key_data = block.Serialize() p2p_service.SendKey( remote_idurl=recipient_id_obj.getIDURL(), encrypted_key_data=encrypted_key_data, packet_id=key_id, callbacks={ commands.Ack(): lambda response, info: _on_transfer_key_response( response, info, key_id, result), commands.Fail(): lambda response, info: _on_transfer_key_response( response, info, key_id, result), # commands.Ack(): lambda response, info: result.callback(response), # commands.Fail(): lambda response, info: result.errback(Exception(response)), None: lambda pkt_out: _on_transfer_key_response(None, None, key_id, result), }, timeout=timeout, ) return result