X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fprotocol_auth.c;h=943040e9fd528d4c6894ce135997564ff28cd18b;hb=7a9d30b60af9d5e71f1bfee4dd976d5807402a15;hp=a0d350d5b9ff83700439b02802ad5882c739b668;hpb=b37cd7198f18ec5e98e2010e72b0407c49069ce7;p=meshlink-tiny diff --git a/src/protocol_auth.c b/src/protocol_auth.c index a0d350d..943040e 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -47,141 +47,6 @@ bool send_id(meshlink_handle_t *mesh, connection_t *c) { return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname); } -static bool commit_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) { - // Check if the node is known - node_t *n = lookup_node(mesh, c->name); - - if(n) { - if(n->status.blacklisted) { - logger(mesh, MESHLINK_ERROR, "Invitee %s is blacklisted", c->name); - } else { - logger(mesh, MESHLINK_ERROR, "Invitee %s already known", c->name); - } - - return false; - } - - // Create a new node - n = new_node(); - n->name = xstrdup(c->name); - n->devclass = DEV_CLASS_UNKNOWN; - n->ecdsa = ecdsa_set_public_key(data); - n->submesh = c->submesh; - - // Remember its current address - node_add_recent_address(mesh, n, &c->address); - - if(!node_write_config(mesh, n, true) || !config_sync(mesh, "current")) { - logger(mesh, MESHLINK_ERROR, "Error writing configuration file for invited node %s!\n", c->name); - free_node(n); - return false; - - } - - node_add(mesh, n); - - logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name); - - //TODO: callback to application to inform of an accepted invitation - - sptps_send_record(&c->sptps, 1, "", 0); - - return true; -} - -static bool process_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) { - // Recover the filename from the cookie and the key - char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key); - char hash[64]; - char hashbuf[18 + strlen(fingerprint)]; - char cookie[25]; - memcpy(hashbuf, data, 18); - memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18); - sha512(hashbuf, sizeof(hashbuf), hash); - b64encode_urlsafe(hash, cookie, 18); - free(fingerprint); - - config_t config; - - if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) { - logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n"); - return false; - } - - // Read the new node's Name from the file - packmsg_input_t in = {config.buf, config.len}; - packmsg_get_uint32(&in); // skip version - free(c->name); - c->name = packmsg_get_str_dup(&in); - - // Check if the file contains Sub-Mesh information - char *submesh_name = packmsg_get_str_dup(&in); - - if(!strcmp(submesh_name, CORE_MESH)) { - free(submesh_name); - c->submesh = NULL; - } else { - if(!check_id(submesh_name)) { - logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie); - free(submesh_name); - return false; - } - - c->submesh = lookup_or_create_submesh(mesh, submesh_name); - free(submesh_name); - - if(!c->submesh) { - logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie); - return false; - } - } - - if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) { - return false; - } - - if(mesh->inviter_commits_first) { - devtool_set_inviter_commits_first(true); - } - - // Send the node the contents of the invitation file - sptps_send_record(&c->sptps, 0, config.buf, config.len); - - config_free(&config); - - c->status.invitation_used = true; - - logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name); - return true; -} - -static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) { - connection_t *c = handle; - meshlink_handle_t *mesh = c->mesh; - - // Extend the time for the invitation exchange upon receiving a valid message - c->last_ping_time = mesh->loop.now.tv_sec; - - if(type == SPTPS_HANDSHAKE) { - // The peer should send its cookie first. - return true; - } - - if(mesh->inviter_commits_first) { - if(type == 2 && len == 18 + 32 && !c->status.invitation_used) { - return process_invitation(mesh, c, data); - } - } else { - if(type == 0 && len == 18 && !c->status.invitation_used) { - return process_invitation(mesh, c, data); - } else if(type == 1 && len == 32 && c->status.invitation_used) { - return commit_invitation(mesh, c, data); - } - } - - return false; -} - bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { assert(request); assert(*request); @@ -193,41 +58,6 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { return false; } - /* Check if this is an invitation */ - - if(name[0] == '?') { - if(!mesh->invitation_key) { - logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name); - return false; - } - - c->ecdsa = ecdsa_set_base64_public_key(name + 1); - - if(!c->ecdsa) { - logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name); - return false; - } - - c->status.invitation = true; - char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key); - - if(!mykey) { - return false; - } - - if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) { - return false; - } - - free(mykey); - - c->protocol_minor = 2; - c->allow_request = 1; - c->last_ping_time = mesh->loop.now.tv_sec; - - return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof(meshlink_invitation_label), send_meta_sptps, receive_invitation_sptps); - } - /* Check if identity is a valid name */ if(!check_id(name)) {