def applied_cb(empty): def encpk_cb(empty): def remote_added_cb(empty): def local_added_cb(empty): logging.debug("IndxSync link_remote_box, local_added_cb") def added_indx_cb(empty): # start syncing/connecting using the new key self.sync_boxes([link_uri], include_push_all = True).addCallbacks(lambda empty: return_d.callback(remote_keys['public']), return_d.errback) self.database.save_linked_box(self.root_store.boxid).addCallbacks(added_indx_cb, return_d.errback) # add the local key to the local store self.keystore.put(local_keys, local_user, self.root_store.boxid).addCallbacks(local_added_cb, return_d.errback) # store in the local keystore self.keystore.put({"public": remote_keys['public'], "private": "", "public-hash": remote_keys['public-hash']}, local_user, self.root_store.boxid).addCallbacks(remote_added_cb, return_d.errback) # don't save the local encpk2 here, only give it to the remote server. # save the remote encpk2 self.database.save_encpk2(sha512_hash(remote_encpk), remote_encpk, remote_serverid).addCallbacks(encpk_cb, return_d.errback)
def generate_new_key(self, request, token): """ Generate a new key and store it in the keystore. Return the public and public-hash parts of the key. """ if not token: return self.return_forbidden(request) BoxHandler.log(logging.DEBUG, "BoxHandler generate_new_key", extra = {"request": request, "token": token}) is_linked = self.get_arg(request, "is-linked", default = False) remote_public = self.get_arg(request, "public") remote_hash = self.get_arg(request, "public-hash") remote_encpk2 = self.get_arg(request, "encpk2") remote_serverid = self.get_arg(request, "serverid") if not remote_public or not remote_hash or not remote_encpk2 or not remote_serverid: BoxHandler.log(logging.ERROR, "BoxHandler generate_new_key: public, public-hash, encpk2, or serverid was missing.", extra = {"request": request, "token": token}) return self.remote_bad_request(request, "public, public-hash, encpk2 or serverid was missing.") def err_cb(failure): failure.trap(Exception) BoxHandler.log(logging.ERROR, "BoxHandler generate_new_key err_cb: {0}".format(failure), extra = {"request": request, "token": token}) return self.return_internal_error(request) if type(remote_encpk2) != type(""): remote_encpk2 = json.dumps(remote_encpk2) remote_encpk2_hsh = sha512_hash(remote_encpk2) local_keys = generate_rsa_keypair(3072) def store_cb(store): def setacl_cb(empty): def created_cb(empty): def saved_cb(empty): def servervar_cb(empty): # encpk2 = rsa_encrypt(load_key(local_keys['public']), token.password) encpk2 = make_encpk2(local_keys, token.password) self.return_created(request, {"data": {"public": local_keys['public'], "public-hash": local_keys['public-hash'], "encpk2": encpk2, "serverid": self.webserver.server_id}}) if is_linked: self.database.save_linked_box(token.boxid).addCallbacks(servervar_cb, err_cb) else: servervar_cb(None) self.database.save_encpk2(remote_encpk2_hsh, remote_encpk2, remote_serverid).addCallbacks(saved_cb, err_cb) def new_key_added_cb(empty): self.webserver.keystore.put(local_keys, token.username, token.boxid).addCallbacks(created_cb, err_cb) # store in the local keystore remote_keys = {"public": remote_public, "public-hash": remote_hash, "private": ""} self.webserver.keystore.put(remote_keys, token.username, token.boxid).addCallbacks(new_key_added_cb, err_cb) user = IndxUser(self.database, token.username) user.set_acl(token.boxid, "@indx", {"read": True, "write": True, "control": False, "owner": False}).addCallbacks(setacl_cb, lambda failure: self.return_internal_error(request)) # store.apply_diff(remote_diff['data'], remote_diff['@commits']).addCallbacks(applied_cb, lambda failure: self.return_internal_error(request)) token.get_store().addCallbacks(store_cb, lambda failure: self.return_internal_error(request))