2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2010 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 bool mykeyused = false;
39 void send_key_changed() {
43 send_request(broadcast, "%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) {
150 // Set key parameters
151 to->incipher = myself->incipher;
152 to->inkeylength = myself->inkeylength;
153 to->indigest = myself->indigest;
154 to->inmaclength = myself->inmaclength;
155 to->incompression = myself->incompression;
157 // Allocate memory for key
158 to->inkey = xrealloc(to->inkey, to->inkeylength);
161 RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
163 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
165 // Reset sequence number and late packet window
167 to->received_seqno = 0;
168 memset(to->late, 0, sizeof(to->late));
170 // Convert to hexadecimal and send
171 key = alloca(2 * to->inkeylength + 1);
172 bin2hex(to->inkey, key, to->inkeylength);
173 key[to->inkeylength * 2] = '\0';
175 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
176 myself->name, to->name, key,
177 to->incipher ? to->incipher->nid : 0,
178 to->indigest ? to->indigest->type : 0, to->inmaclength,
182 bool ans_key_h(connection_t *c) {
183 char from_name[MAX_STRING_SIZE];
184 char to_name[MAX_STRING_SIZE];
185 char key[MAX_STRING_SIZE];
186 char address[MAX_STRING_SIZE] = "";
187 char port[MAX_STRING_SIZE] = "";
188 int cipher, digest, maclength, compression;
191 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
192 from_name, to_name, key, &cipher, &digest, &maclength,
193 &compression, address, port) < 7) {
194 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
199 if(!check_id(from_name) || !check_id(to_name)) {
200 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
204 from = lookup_node(from_name);
207 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
208 "ANS_KEY", c->name, c->hostname, from_name);
212 to = lookup_node(to_name);
215 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
216 "ANS_KEY", c->name, c->hostname, to_name);
220 /* Forward it if necessary */
226 if(!to->status.reachable) {
227 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
228 "ANS_KEY", c->name, c->hostname, to_name);
233 char *address, *port;
234 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
235 sockaddr2str(&from->address, &address, &port);
236 send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
242 return send_request(to->nexthop->connection, "%s", c->buffer);
245 /* Update our copy of the origin's packet key */
246 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
248 from->outkey = xstrdup(key);
249 from->outkeylength = strlen(key) / 2;
250 hex2bin(key, from->outkey, from->outkeylength);
252 /* Check and lookup cipher and digest algorithms */
255 from->outcipher = EVP_get_cipherbynid(cipher);
257 if(!from->outcipher) {
258 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
263 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
264 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
269 from->outcipher = NULL;
272 from->outmaclength = maclength;
275 from->outdigest = EVP_get_digestbynid(digest);
277 if(!from->outdigest) {
278 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
283 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
284 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
285 from->name, from->hostname);
289 from->outdigest = NULL;
292 if(compression < 0 || compression > 11) {
293 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
297 from->outcompression = compression;
300 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
301 logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
302 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
306 from->status.validkey = true;
307 from->sent_seqno = 0;
309 if(*address && *port) {
310 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
311 sockaddr_t sa = str2sockaddr(address, port);
312 update_node_udp(from, &sa);
315 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
316 send_mtu_probe(from);