-int ans_key_h(connection_t *c)
-{
- char from_name[MAX_STRING_SIZE];
- char to_name[MAX_STRING_SIZE];
- char key[MAX_STRING_SIZE];
- int cipher, digest, maclength, compression;
- node_t *from, *to;
-cp
- if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d", from_name, to_name, key, &cipher, &digest, &maclength, &compression) != 7)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
- c->name, c->hostname);
- return -1;
- }
-
- from = lookup_node(from_name);
-
- if(!from)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
- c->name, c->hostname, from_name);
- return -1;
- }
-
- to = lookup_node(to_name);
-
- if(!to)
- {
- syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
- c->name, c->hostname, to_name);
- return -1;
- }
-
- /* Forward it if necessary */
-
- if(to != myself)
- {
- return send_request(to->nexthop->connection, "%s", c->buffer);
- }
-
- /* Update our copy of the origin's packet key */
-
- if(from->key)
- free(from->key);
-
- from->key = xstrdup(key);
- from->keylength = strlen(key) / 2;
- hex2bin(from->key, from->key, from->keylength);
- from->key[from->keylength] = '\0';
-
- from->status.validkey = 1;
- from->status.waitingforkey = 0;
- from->received_seqno = 0;
-
- /* Check and lookup cipher and digest algorithms */
-
- if(cipher)
- {
- from->cipher = EVP_get_cipherbynid(cipher);
- if(!from->cipher)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
- return -1;
- }
- if(from->keylength != from->cipher->key_len + from->cipher->iv_len)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
- return -1;
- }
- }
- else
- {
- from->cipher = NULL;
- }
-
- from->maclength = maclength;
-
- if(digest)
- {
- from->digest = EVP_get_digestbynid(digest);
- if(!from->digest)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
- return -1;
- }
- if(from->maclength > from->digest->md_size || from->maclength < 0)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
- return -1;
- }
- }
- else
- {
- from->digest = NULL;
- }
-
- from->compression = compression;
-
- flush_queue(from);
-cp
- return 0;
+bool send_ans_key(node_t *to) {
+ if(to->status.sptps) {
+ abort();
+ }
+
+#ifdef DISABLE_LEGACY
+ return false;
+#else
+ size_t keylen = myself->incipher ? cipher_keylength(myself->incipher) : 1;
+ char key[keylen * 2 + 1];
+
+ randomize(key, keylen);
+
+ cipher_free(&to->incipher);
+ digest_free(&to->indigest);
+
+ if(myself->incipher) {
+ to->incipher = cipher_alloc();
+
+ if(!cipher_open_by_nid(to->incipher, cipher_get_nid(myself->incipher))) {
+ abort();
+ }
+
+ if(!cipher_set_key(to->incipher, key, false)) {
+ abort();
+ }
+ }
+
+ if(myself->indigest) {
+ to->indigest = digest_alloc();
+
+ if(!digest_open_by_nid(to->indigest,
+ digest_get_nid(myself->indigest),
+ digest_length(myself->indigest))) {
+ abort();
+ }
+
+ if(!digest_set_key(to->indigest, key, keylen)) {
+ abort();
+ }
+ }
+
+ to->incompression = myself->incompression;
+
+ bin2hex(key, key, keylen);
+
+ // Reset sequence number and late packet window
+ to->received_seqno = 0;
+ to->received = 0;
+
+ if(replaywin) {
+ memset(to->late, 0, replaywin);
+ }
+
+ to->status.validkey_in = true;
+
+ return send_request(to->nexthop->connection, "%d %s %s %s %d %d %lu %d", ANS_KEY,
+ myself->name, to->name, key,
+ cipher_get_nid(to->incipher),
+ digest_get_nid(to->indigest),
+ (unsigned long)digest_length(to->indigest),
+ to->incompression);
+#endif
+}
+
+bool ans_key_h(connection_t *c, const char *request) {
+ char from_name[MAX_STRING_SIZE];
+ char to_name[MAX_STRING_SIZE];
+ char key[MAX_STRING_SIZE];
+ char address[MAX_STRING_SIZE] = "";
+ char port[MAX_STRING_SIZE] = "";
+ int cipher, digest;
+ unsigned long maclength;
+ int compression;
+ node_t *from, *to;
+
+ if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %lu %d "MAX_STRING" "MAX_STRING,
+ from_name, to_name, key, &cipher, &digest, &maclength,
+ &compression, address, port) < 7) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
+ c->hostname);
+ return false;
+ }
+
+ if(!check_id(from_name) || !check_id(to_name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
+ return false;
+ }
+
+ from = lookup_node(from_name);
+
+ if(!from) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
+ "ANS_KEY", c->name, c->hostname, from_name);
+ return true;
+ }
+
+ to = lookup_node(to_name);
+
+ if(!to) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
+ "ANS_KEY", c->name, c->hostname, to_name);
+ return true;
+ }
+
+ /* Forward it if necessary */
+
+ if(to != myself) {
+ if(tunnelserver) {
+ return true;
+ }
+
+ if(!to->status.reachable) {
+ logger(DEBUG_ALWAYS, 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 && to->minmtu) {
+ char *address, *port;
+ logger(DEBUG_PROTOCOL, 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);
+ }
+
+#ifndef DISABLE_LEGACY
+ /* Don't use key material until every check has passed. */
+ cipher_free(&from->outcipher);
+ digest_free(&from->outdigest);
+#endif
+
+ if(!from->status.sptps) {
+ from->status.validkey = false;
+ }
+
+ switch(compression) {
+ case COMPRESS_LZ4:
+#ifdef HAVE_LZ4
+ break;
+#else
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
+ logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
+ return true;
+#endif
+
+ case COMPRESS_LZO_HI:
+ case COMPRESS_LZO_LO:
+#ifdef HAVE_LZO
+ break;
+#else
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
+ logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
+ return true;
+#endif
+
+ case COMPRESS_ZLIB_9:
+ case COMPRESS_ZLIB_8:
+ case COMPRESS_ZLIB_7:
+ case COMPRESS_ZLIB_6:
+ case COMPRESS_ZLIB_5:
+ case COMPRESS_ZLIB_4:
+ case COMPRESS_ZLIB_3:
+ case COMPRESS_ZLIB_2:
+ case COMPRESS_ZLIB_1:
+#ifdef HAVE_ZLIB
+ break;
+#else
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
+ logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
+ return true;
+#endif
+
+ case COMPRESS_NONE:
+ break;
+
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
+ logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", compression);
+ return true;
+ }
+
+ from->outcompression = compression;
+
+ /* SPTPS or old-style key exchange? */
+
+ if(from->status.sptps) {
+ uint8_t buf[strlen(key)];
+ size_t len = b64decode_tinc(key, buf, strlen(key));
+
+ if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
+ /* Uh-oh. It might be that the tunnel is stuck in some corrupted state,
+ so let's restart SPTPS in case that helps. But don't do that too often
+ to prevent storms.
+ Note that simply relying on handshake timeout is not enough, because
+ that doesn't apply to key regeneration. */
+ if(from->last_req_key < now.tv_sec - 10) {
+ logger(DEBUG_PROTOCOL, LOG_ERR, "Failed to decode handshake TCP packet from %s (%s), restarting SPTPS", from->name, from->hostname);
+ send_req_key(from);
+ }
+
+ return true;
+ }
+
+ if(from->status.validkey) {
+ if(*address && *port) {
+ logger(DEBUG_PROTOCOL, 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);
+ }
+ }
+
+ send_mtu_info(myself, from, MTU);
+
+ return true;
+ }
+
+#ifdef DISABLE_LEGACY
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses legacy protocol!", from->name, from->hostname);
+ return false;
+#else
+ /* Check and lookup cipher and digest algorithms */
+
+ if(cipher) {
+ from->outcipher = cipher_alloc();
+
+ if(!cipher_open_by_nid(from->outcipher, cipher)) {
+ cipher_free(&from->outcipher);
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
+ return false;
+ }
+ } else {
+ from->outcipher = NULL;
+ }
+
+ if(digest) {
+ from->outdigest = digest_alloc();
+
+ if(!digest_open_by_nid(from->outdigest, digest, maclength)) {
+ digest_free(&from->outdigest);
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
+ return false;
+ }
+ } else {
+ from->outdigest = NULL;
+ }
+
+ if(maclength != digest_length(from->outdigest)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
+ return false;
+ }
+
+ /* Process key */
+
+ size_t keylen = hex2bin(key, key, sizeof(key));
+
+ if(keylen != (from->outcipher ? cipher_keylength(from->outcipher) : 1)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
+ return true;
+ }
+
+ /* Update our copy of the origin's packet key */
+
+ if(from->outcipher && !cipher_set_key(from->outcipher, key, true)) {
+ return false;
+ }
+
+ if(from->outdigest && !digest_set_key(from->outdigest, key, keylen)) {
+ return false;
+ }
+
+ from->status.validkey = true;
+ from->sent_seqno = 0;
+
+ if(*address && *port) {
+ logger(DEBUG_PROTOCOL, 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);
+ }
+
+ return true;
+#endif