- /* Convert the challenge from hexadecimal back to binary */
-
- int inlen = hex2bin(hexkey, enckey, sizeof enckey);
-
- /* Check if the length of the meta key is all right */
-
- if(inlen != len) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
+ // 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);
+
+ char filename[PATH_MAX], usedname[PATH_MAX];
+ snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookie);
+ snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, cookie);
+
+ // Atomically rename the invitation file
+ if(rename(filename, usedname)) {
+ if(errno == ENOENT)
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
+ else
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);