2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2016 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) {
50 if(c->status.active && c->node && c->node->status.reachable) {
51 send_ans_key(c->node);
56 bool key_changed_h(connection_t *c) {
57 char name[MAX_STRING_SIZE];
60 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
61 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
62 c->name, c->hostname);
67 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
71 if(seen_request(c->buffer)) {
75 n = lookup_node(name);
78 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
79 "KEY_CHANGED", c->name, c->hostname, name);
83 n->status.validkey = false;
95 bool send_req_key(node_t *to) {
96 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
99 bool req_key_h(connection_t *c) {
100 char from_name[MAX_STRING_SIZE];
101 char to_name[MAX_STRING_SIZE];
104 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
105 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
110 if(!check_id(from_name) || !check_id(to_name)) {
111 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
115 from = lookup_node(from_name);
118 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
119 "REQ_KEY", c->name, c->hostname, from_name);
123 to = lookup_node(to_name);
126 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
127 "REQ_KEY", c->name, c->hostname, to_name);
131 /* Check if this key request is for us */
133 if(to == myself) { /* Yes, send our own key back */
134 if(!send_ans_key(from)) {
142 if(!to->status.reachable) {
143 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
144 "REQ_KEY", c->name, c->hostname, to_name);
148 send_request(to->nexthop->connection, "%s", c->buffer);
154 bool send_ans_key(node_t *to) {
155 // Set key parameters
156 to->incipher = myself->incipher;
157 to->inkeylength = myself->inkeylength;
158 to->indigest = myself->indigest;
159 to->inmaclength = myself->inmaclength;
160 to->incompression = myself->incompression;
162 // Allocate memory for key
163 to->inkey = xrealloc(to->inkey, to->inkeylength);
166 if(1 != RAND_bytes((unsigned char *)to->inkey, to->inkeylength)) {
167 int err = ERR_get_error();
168 logger(LOG_ERR, "Failed to generate random for key (%s)", ERR_error_string(err, NULL));
169 return false; // Do not send insecure keys, let connection attempt fail.
173 EVP_DecryptInit_ex(to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + EVP_CIPHER_key_length(to->incipher));
176 // Reset sequence number and late packet window
178 to->received_seqno = 0;
181 memset(to->late, 0, replaywin);
184 // Convert to hexadecimal and send
185 char key[2 * to->inkeylength + 1];
186 bin2hex(to->inkey, key, to->inkeylength);
187 key[to->inkeylength * 2] = '\0';
189 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
190 myself->name, to->name, key,
191 to->incipher ? EVP_CIPHER_nid(to->incipher) : 0,
192 to->indigest ? EVP_MD_type(to->indigest) : 0, to->inmaclength,
196 bool ans_key_h(connection_t *c) {
197 char from_name[MAX_STRING_SIZE];
198 char to_name[MAX_STRING_SIZE];
199 char key[MAX_STRING_SIZE];
200 char address[MAX_STRING_SIZE] = "";
201 char port[MAX_STRING_SIZE] = "";
202 int cipher, digest, maclength, compression;
205 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
206 from_name, to_name, key, &cipher, &digest, &maclength,
207 &compression, address, port) < 7) {
208 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
213 if(!check_id(from_name) || !check_id(to_name)) {
214 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
218 from = lookup_node(from_name);
221 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
222 "ANS_KEY", c->name, c->hostname, from_name);
226 to = lookup_node(to_name);
229 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
230 "ANS_KEY", c->name, c->hostname, to_name);
234 /* Forward it if necessary */
241 if(!to->status.reachable) {
242 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
243 "ANS_KEY", c->name, c->hostname, to_name);
247 if(!*address && from->address.sa.sa_family != AF_UNSPEC && to->minmtu) {
248 char *address, *port;
249 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
250 sockaddr2str(&from->address, &address, &port);
251 send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
257 return send_request(to->nexthop->connection, "%s", c->buffer);
260 /* Don't use key material until every check has passed. */
261 from->status.validkey = false;
263 /* Update our copy of the origin's packet key */
264 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
265 from->outkeylength = strlen(key) / 2;
267 if(!hex2bin(key, from->outkey, from->outkeylength)) {
268 logger(LOG_ERR, "Got bad %s from %s(%s): %s", "ANS_KEY", from->name, from->hostname, "invalid key");
272 /* Check and lookup cipher and digest algorithms */
275 from->outcipher = EVP_get_cipherbynid(cipher);
277 if(!from->outcipher) {
278 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
283 if(from->outkeylength != EVP_CIPHER_key_length(from->outcipher) + EVP_CIPHER_iv_length(from->outcipher)) {
284 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
289 if(from->outkeylength != 1) {
290 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
294 from->outcipher = NULL;
297 from->outmaclength = maclength;
300 from->outdigest = EVP_get_digestbynid(digest);
302 if(!from->outdigest) {
303 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
308 if(from->outmaclength > EVP_MD_size(from->outdigest) || from->outmaclength < 0) {
309 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
310 from->name, from->hostname);
314 from->outdigest = NULL;
317 if(compression < 0 || compression > 11) {
318 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
322 from->outcompression = compression;
325 if(!EVP_EncryptInit_ex(from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + EVP_CIPHER_key_length(from->outcipher))) {
326 logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
327 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
331 from->status.validkey = true;
332 from->sent_seqno = 0;
334 if(*address && *port) {
335 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
336 sockaddr_t sa = str2sockaddr(address, port);
337 update_node_udp(from, &sa);
340 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent) {
341 send_mtu_probe(from);