+
+ /* Forward it if necessary */
+
+ if(to != myself) {
+ if(tunnelserver)
+ return true;
+
+ if(!to->status.reachable) {
+ logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
+ "ANS_KEY", c->name, c->hostname, to_name);
+ return true;
+ }
+
+ if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
+ char *address, *port;
+ ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
+ sockaddr2str(&from->address, &address, &port);
+ send_request(to->nexthop->connection, "%s %s %s", request, address, port);
+ free(address);
+ free(port);
+ return true;
+ }
+
+ return send_request(to->nexthop->connection, "%s", request);
+ }
+
+ /* Check and lookup cipher and digest algorithms */
+
+ if(!cipher_open_by_nid(&from->outcipher, cipher)) {
+ logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
+ return false;
+ }
+
+ if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
+ logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
+ return false;
+ }
+
+ if(maclength != digest_length(&from->outdigest)) {
+ logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
+ return false;
+ }
+
+ if(compression < 0 || compression > 11) {
+ logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
+ return true;
+ }
+
+ from->outcompression = compression;
+
+ /* ECDH or old-style key exchange? */
+ /* TODO: look at SSH and TLS to see how they derive cipher and HMAC keys from shared secret properly */
+
+ if(!strncmp(key, "ECDH:", 5)) {
+ logger(LOG_DEBUG, "Got ECDH key from %s", from->name);
+
+ keylen = (strlen(key) - 5) / 2;
+
+ if(keylen != ECDH_SIZE) {
+ logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
+ return false;
+ }
+
+ if(ECDH_SHARED_SIZE < cipher_keylength(&from->outcipher)) {
+ logger(LOG_ERR, "ECDH key too short for cipher of %s!", from->name);
+ return false;
+ }
+
+ if(!from->ecdh) {
+ logger(LOG_DEBUG, "Woops, we didn't generate our public key yet");
+ from->status.ecdh = true;
+ if(!send_ans_key(from))
+ return false;
+ }
+
+ char shared[ECDH_SHARED_SIZE * 2 + 1];
+ char hex[ECDH_SHARED_SIZE * 2 + 1];
+ hex2bin(key + 5, key + 5, keylen);
+
+ if(!ecdh_compute_shared(&from->ecdh, key + 5, shared))
+ return false;
+
+ /* Update our crypto end */
+
+ char *mykey;
+ size_t mykeylen = cipher_keylength(&myself->incipher);
+ keylen = cipher_keylength(&from->outcipher);
+
+ if(ECDH_SHARED_SIZE < mykeylen) {
+ logger(LOG_ERR, "ECDH key too short for cipher of MYSELF!");
+ return false;
+ }
+
+ if(strcmp(myself->name, from->name) < 0) {
+ logger(LOG_DEBUG, "Using left half of shared secret");
+ mykey = shared;
+ memcpy(key, shared + ECDH_SHARED_SIZE - keylen, keylen);
+ } else {
+ logger(LOG_DEBUG, "Using right half of shared secret");
+ mykey = shared + ECDH_SHARED_SIZE - mykeylen;
+ memcpy(key, shared, keylen);
+ }
+
+ cipher_open_by_nid(&from->incipher, cipher_get_nid(&myself->incipher));
+ digest_open_by_nid(&from->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
+ from->incompression = myself->incompression;
+
+ cipher_set_key(&from->incipher, mykey, true);
+ digest_set_key(&from->indigest, mykey, mykeylen);
+
+ // Reset sequence number and late packet window
+ mykeyused = true;
+ from->received_seqno = 0;
+ if(replaywin)
+ memset(from->late, 0, replaywin);
+
+ bin2hex(shared, hex, ECDH_SHARED_SIZE);
+ hex[ECDH_SHARED_SIZE * 2] = 0;
+ logger(LOG_DEBUG, "Shared secret was %s", hex);
+
+ bin2hex(mykey, hex, mykeylen);
+ hex[mykeylen * 2] = 0;
+ logger(LOG_DEBUG, "My part is: %s (%d)", hex, mykeylen);
+
+ bin2hex(key, hex, keylen);
+ hex[keylen * 2] = 0;
+ logger(LOG_DEBUG, "His part is: %s (%d)", hex, keylen);
+ } else {
+ keylen = strlen(key) / 2;
+ hex2bin(key, key, keylen);
+ }
+
+ /* Update our copy of the origin's packet key */
+
+ if(keylen != cipher_keylength(&from->outcipher)) {
+ logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
+ return false;
+ }
+
+ cipher_set_key(&from->outcipher, key, false);
+ digest_set_key(&from->outdigest, key, keylen);
+
+ from->status.validkey = true;
+ from->sent_seqno = 0;
+
+ if(*address && *port) {
+ ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
+ sockaddr_t sa = str2sockaddr(address, port);
+ update_node_udp(from, &sa);
+ }
+
+ if(from->options & OPTION_PMTU_DISCOVERY)
+ send_mtu_probe(from);
+
+ return true;