-int metakey_h(connection_t *c)
-{
- char buffer[MAX_STRING_SIZE];
- int cipher, digest, maclength, compression;
- int len;
-cp
- if(sscanf(c->buffer, "%*d %d %d %d %d "MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
- return -1;
- }
-cp
- len = RSA_size(myself->connection->rsa_key);
-
- /* Check if the length of the meta key is all right */
-
- if(strlen(buffer) != len*2)
- {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
- return -1;
- }
-
- /* Allocate buffers for the meta key */
-cp
- if(!c->inkey)
- c->inkey = xmalloc(len);
-
- if(!c->inctx)
- c->inctx = xmalloc(sizeof(*c->inctx));
-
- /* Convert the challenge from hexadecimal back to binary */
-cp
- hex2bin(buffer,buffer,len);
-
- /* Decrypt the meta key */
-cp
- if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
- {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_SCARY_THINGS)
- {
- bin2hex(c->inkey, buffer, len);
- buffer[len*2] = '\0';
- syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
- }
-
- /* All incoming requests will now be encrypted. */
-cp
- /* Check and lookup cipher and digest algorithms */
-
- if(cipher)
- {
- c->incipher = EVP_get_cipherbynid(cipher);
- if(!c->incipher)
- {
- syslog(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
- return -1;
- }
-
- EVP_DecryptInit(c->inctx, c->incipher,
- c->inkey + len - c->incipher->key_len,
- c->inkey + len - c->incipher->key_len - c->incipher->iv_len);
-
- c->status.decryptin = 1;
- }
- else
- {
- c->incipher = NULL;
- }
-
- c->inmaclength = maclength;
-
- if(digest)
- {
- c->indigest = EVP_get_digestbynid(digest);
- if(!c->indigest)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
- return -1;
- }
-
- if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0)
- {
- syslog(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
- return -1;
- }
- }
- else
- {
- c->indigest = NULL;
- }
-
- c->incompression = compression;
-
- c->allow_request = CHALLENGE;
-cp
- return send_challenge(c);
+static bool metakey_ec_h(connection_t *c, const char *request) {
+ size_t siglen = ecdsa_size(&c->ecdsa);
+ char in[MAX_STRING_SIZE];
+ char key[MAX_STRING_SIZE];
+ char sig[siglen];
+
+ logger(LOG_DEBUG, "Got ECDH metakey from %s", c->name);
+
+ if(sscanf(request, "%*d " MAX_STRING, in) != 1) {
+ logger(LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
+ return false;
+ }
+
+ if(strlen(in) != (ECDH_SIZE + siglen) * 2) {
+ logger(LOG_ERR, "Possible intruder %s (%s): %s %d != %d", c->name, c->hostname, "wrong keylength", strlen(in) / 2, (ECDH_SIZE + siglen));
+ return false;
+ }
+
+ hex2bin(in, key, ECDH_SIZE);
+ hex2bin(in + ECDH_SIZE * 2, sig, siglen);
+
+ if(!ecdsa_verify(&c->ecdsa, key, ECDH_SIZE, sig)) {
+ logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "invalid ECDSA signature");
+ return false;
+ }
+
+ char shared[ECDH_SHARED_SIZE * 2 + 1];
+
+ if(!ecdh_compute_shared(&c->ecdh, key, shared))
+ return false;
+
+ /* Update our crypto end */
+
+ if(!cipher_open_by_name(&c->incipher, "aes-256-ofb"))
+ return false;
+ if(!digest_open_by_name(&c->indigest, "sha512", -1))
+ return false;
+ if(!cipher_open_by_name(&c->outcipher, "aes-256-ofb"))
+ return false;
+ if(!digest_open_by_name(&c->outdigest, "sha512", -1))
+ return false;
+
+ size_t mykeylen = cipher_keylength(&c->incipher);
+ size_t hiskeylen = cipher_keylength(&c->outcipher);
+
+ char *mykey;
+ char *hiskey;
+ char *seed;
+
+ if(strcmp(myself->name, c->name) < 0) {
+ mykey = key;
+ hiskey = key + mykeylen * 2;
+ xasprintf(&seed, "tinc TCP key expansion %s %s", myself->name, c->name);
+ } else {
+ mykey = key + hiskeylen * 2;
+ hiskey = key;
+ xasprintf(&seed, "tinc TCP key expansion %s %s", c->name, myself->name);
+ }
+
+ if(!prf(shared, ECDH_SHARED_SIZE, seed, strlen(seed), key, hiskeylen * 2 + mykeylen * 2))
+ return false;
+
+ free(seed);
+
+ bin2hex(shared, shared, ECDH_SHARED_SIZE);
+ shared[ECDH_SHARED_SIZE * 2] = 0;
+ logger(LOG_DEBUG, "Shared secret is %s", shared);
+
+ cipher_set_key(&c->incipher, mykey, true);
+ digest_set_key(&c->indigest, mykey + mykeylen, mykeylen);
+
+ cipher_set_key(&c->outcipher, hiskey, false);
+ digest_set_key(&c->outdigest, hiskey + hiskeylen, hiskeylen);
+
+ c->status.decryptin = true;
+ c->status.encryptout = true;
+ c->allow_request = CHALLENGE;
+
+ return send_challenge(c);