-int metakey_h(connection_t *c)
-{
- char buffer[MAX_STRING_SIZE];
- int len;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
- {
- 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
- EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
- c->inkey + len - EVP_bf_cfb()->key_len,
- c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
-
- c->status.decryptin = 1;
-
- c->allow_request = CHALLENGE;
-cp
- return send_challenge(c);
+bool id_h(connection_t *c, const char *request) {
+ char name[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
+ c->hostname);
+ return false;
+ }
+
+ /* Check if this is a control connection */
+
+ if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
+ c->status.control = true;
+ c->allow_request = CONTROL;
+ c->last_ping_time = now.tv_sec + 3600;
+
+ free(c->name);
+ c->name = xstrdup("<control>");
+
+ return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
+ }
+
+ if(name[0] == '?') {
+ if(!invitation_key) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
+ return false;
+ }
+
+ c->ecdsa = ecdsa_set_base64_public_key(name + 1);
+ if(!c->ecdsa) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
+ return false;
+ }
+
+ c->status.invitation = true;
+ char *mykey = ecdsa_get_base64_public_key(invitation_key);
+ if(!mykey)
+ return false;
+ if(!send_request(c, "%d %s", ACK, mykey))
+ return false;
+ free(mykey);
+
+ c->protocol_minor = 2;
+
+ return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
+ }
+
+ /* Check if identity is a valid name */
+
+ if(!check_id(name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
+ c->hostname, "invalid name");
+ return false;
+ }
+
+ /* If this is an outgoing connection, make sure we are connected to the right host */
+
+ if(c->outgoing) {
+ if(strcmp(c->name, name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
+ c->name);
+ return false;
+ }
+ } else {
+ if(c->name)
+ free(c->name);
+ c->name = xstrdup(name);
+ }
+
+ /* Check if version matches */
+
+ if(c->protocol_major != myself->connection->protocol_major) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
+ c->name, c->hostname, c->protocol_major, c->protocol_minor);
+ return false;
+ }
+
+ if(bypass_security) {
+ if(!c->config_tree)
+ init_configuration(&c->config_tree);
+ c->allow_request = ACK;
+ return send_ack(c);
+ }
+
+ if(!experimental)
+ c->protocol_minor = 0;
+
+ if(!c->config_tree) {
+ init_configuration(&c->config_tree);
+
+ if(!read_host_config(c->config_tree, c->name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
+ return false;
+ }
+
+ if(experimental)
+ read_ecdsa_public_key(c);
+ /* Ignore failures if no key known yet */
+ }
+
+ if(c->protocol_minor && !ecdsa_active(c->ecdsa))
+ c->protocol_minor = 1;
+
+ /* Forbid version rollback for nodes whose Ed25519 key we know */
+
+ if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
+ c->name, c->hostname, c->protocol_major, c->protocol_minor);
+ return false;
+ }
+
+ c->allow_request = METAKEY;
+
+ if(c->protocol_minor >= 2) {
+ c->allow_request = ACK;
+ char label[25 + strlen(myself->name) + strlen(c->name)];
+
+ if(c->outgoing)
+ snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
+ else
+ snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
+
+ return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
+ } else {
+ return send_metakey(c);
+ }
+}
+
+bool send_metakey(connection_t *c) {
+#ifdef DISABLE_LEGACY
+ return false;
+#else
+ if(!myself->connection->rsa) {
+ logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
+ return false;
+ }
+
+ if(!read_rsa_public_key(c))
+ return false;
+
+ /* We need to use a stream mode for the meta protocol. Use AES for this,
+ but try to match the key size with the one from the cipher selected
+ by Cipher.
+ */
+
+ int keylen = cipher_keylength(myself->incipher);
+ if(keylen <= 16)
+ c->outcipher = cipher_open_by_name("aes-128-cfb");
+ else if(keylen <= 24)
+ c->outcipher = cipher_open_by_name("aes-192-cfb");
+ else
+ c->outcipher = cipher_open_by_name("aes-256-cfb");
+ if(!c)
+ return false;
+
+ c->outbudget = cipher_budget(c->outcipher);
+
+ if(!(c->outdigest = digest_open_by_name("sha256", -1)))
+ return false;
+
+ const size_t len = rsa_size(c->rsa);
+ char key[len];
+ char enckey[len];
+ char hexkey[2 * len + 1];
+
+ /* Create a random key */
+
+ randomize(key, 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.
+ */
+
+ key[0] &= 0x7F;
+
+ if(!cipher_set_key_from_rsa(c->outcipher, key, len, true))
+ return false;
+
+ if(debug_level >= DEBUG_SCARY_THINGS) {
+ bin2hex(key, hexkey, len);
+ logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
+ }
+
+ /* 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(c->rsa, key, len, enckey)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
+ return false;
+ }
+
+ /* Convert the encrypted random data to a hexadecimal formatted string */
+
+ bin2hex(enckey, hexkey, len);
+
+ /* Send the meta key */
+
+ bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
+ cipher_get_nid(c->outcipher),
+ digest_get_nid(c->outdigest), c->outmaclength,
+ c->outcompression, hexkey);
+
+ c->status.encryptout = true;
+ return result;
+#endif