-int send_metakey(connection_t *c)
-{
- char buffer[MAX_STRING_SIZE];
- int len, x;
-
- cp();
-
- len = RSA_size(c->rsa_key);
-
- /* Allocate buffers for the meta key */
-
- if(!c->outkey)
- c->outkey = xmalloc(len);
-
- if(!c->outctx)
- c->outctx = xmalloc_and_zero(sizeof(*c->outctx));
- cp();
- /* Copy random data to the buffer */
-
- RAND_bytes(c->outkey, len);
-
- /* The message we send must be smaller than the modulus of the RSA key.
- By definition, for a key of k bits, the following formula holds:
-
- 2^(k-1) <= modulus < 2^(k)
-
- Where ^ means "to the power of", not "xor".
- This means that to be sure, we must choose our message < 2^(k-1).
- This can be done by setting the most significant bit to zero.
- */
-
- c->outkey[0] &= 0x7F;
-
- if(debug_level >= DEBUG_SCARY_THINGS) {
- bin2hex(c->outkey, buffer, len);
- buffer[len * 2] = '\0';
- logger(DEBUG_ALWAYS, LOG_DEBUG, _("Generated random meta key (unencrypted): %s"),
- buffer);
- }
-
- /* Encrypt the random data
-
- We do not use one of the PKCS padding schemes here.
- This is allowed, because we encrypt a totally random string
- with a length equal to that of the modulus of the RSA key.
- */
-
- if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len) {
- logger(DEBUG_ALWAYS, LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
- c->name, c->hostname);
- return -1;
+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;