2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <openssl/evp.h>
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
28 #include "connection.h"
37 static bool mykeyused = false;
39 void send_key_changed(void) {
43 send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
45 /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
47 for(node = connection_tree->head; node; node = node->next) {
49 if(c->status.active && c->node && c->node->status.reachable)
50 send_ans_key(c->node);
54 bool key_changed_h(connection_t *c) {
55 char name[MAX_STRING_SIZE];
58 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
59 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
60 c->name, c->hostname);
65 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
69 if(seen_request(c->buffer))
72 n = lookup_node(name);
75 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
76 "KEY_CHANGED", c->name, c->hostname, name);
80 n->status.validkey = false;
91 bool send_req_key(node_t *to) {
92 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
95 bool req_key_h(connection_t *c) {
96 char from_name[MAX_STRING_SIZE];
97 char to_name[MAX_STRING_SIZE];
100 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
101 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
106 if(!check_id(from_name) || !check_id(to_name)) {
107 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
111 from = lookup_node(from_name);
114 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
115 "REQ_KEY", c->name, c->hostname, from_name);
119 to = lookup_node(to_name);
122 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
123 "REQ_KEY", c->name, c->hostname, to_name);
127 /* Check if this key request is for us */
129 if(to == myself) { /* Yes, send our own key back */
135 if(!to->status.reachable) {
136 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
137 "REQ_KEY", c->name, c->hostname, to_name);
141 send_request(to->nexthop->connection, "%s", c->buffer);
147 bool send_ans_key(node_t *to) {
148 // Set key parameters
149 to->incipher = myself->incipher;
150 to->inkeylength = myself->inkeylength;
151 to->indigest = myself->indigest;
152 to->inmaclength = myself->inmaclength;
153 to->incompression = myself->incompression;
155 // Allocate memory for key
156 to->inkey = xrealloc(to->inkey, to->inkeylength);
159 RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
161 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
163 // Reset sequence number and late packet window
165 to->received_seqno = 0;
166 if(replaywin) memset(to->late, 0, replaywin);
168 // Convert to hexadecimal and send
169 char key[2 * to->inkeylength + 1];
170 bin2hex(to->inkey, key, to->inkeylength);
171 key[to->inkeylength * 2] = '\0';
173 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
174 myself->name, to->name, key,
175 to->incipher ? to->incipher->nid : 0,
176 to->indigest ? to->indigest->type : 0, to->inmaclength,
180 bool ans_key_h(connection_t *c) {
181 char from_name[MAX_STRING_SIZE];
182 char to_name[MAX_STRING_SIZE];
183 char key[MAX_STRING_SIZE];
184 char address[MAX_STRING_SIZE] = "";
185 char port[MAX_STRING_SIZE] = "";
186 int cipher, digest, maclength, compression;
189 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
190 from_name, to_name, key, &cipher, &digest, &maclength,
191 &compression, address, port) < 7) {
192 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
197 if(!check_id(from_name) || !check_id(to_name)) {
198 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
202 from = lookup_node(from_name);
205 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
206 "ANS_KEY", c->name, c->hostname, from_name);
210 to = lookup_node(to_name);
213 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
214 "ANS_KEY", c->name, c->hostname, to_name);
218 /* Forward it if necessary */
224 if(!to->status.reachable) {
225 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
226 "ANS_KEY", c->name, c->hostname, to_name);
230 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
231 char *address, *port;
232 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
233 sockaddr2str(&from->address, &address, &port);
234 send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
240 return send_request(to->nexthop->connection, "%s", c->buffer);
243 /* Update our copy of the origin's packet key */
244 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
245 from->outkeylength = strlen(key) / 2;
246 hex2bin(key, from->outkey, from->outkeylength);
248 /* Check and lookup cipher and digest algorithms */
251 from->outcipher = EVP_get_cipherbynid(cipher);
253 if(!from->outcipher) {
254 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
259 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
260 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
265 from->outcipher = NULL;
268 from->outmaclength = maclength;
271 from->outdigest = EVP_get_digestbynid(digest);
273 if(!from->outdigest) {
274 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
279 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
280 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
281 from->name, from->hostname);
285 from->outdigest = NULL;
288 if(compression < 0 || compression > 11) {
289 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
293 from->outcompression = compression;
296 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
297 logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
298 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
302 from->status.validkey = true;
303 from->sent_seqno = 0;
305 if(*address && *port) {
306 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
307 sockaddr_t sa = str2sockaddr(address, port);
308 update_node_udp(from, &sa);
311 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent)
312 send_mtu_probe(from);