2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2009 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <openssl/evp.h>
26 #include <openssl/err.h>
27 #include <openssl/rand.h>
30 #include "connection.h"
39 bool mykeyused = false;
41 bool send_key_changed()
45 /* Only send this message if some other daemon requested our key previously.
46 This reduces unnecessary key_changed broadcasts.
52 return send_request(broadcast, "%d %lx %s", KEY_CHANGED, random(), myself->name);
55 bool key_changed_h(connection_t *c)
57 char name[MAX_STRING_SIZE];
62 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
63 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
64 c->name, c->hostname);
68 if(seen_request(c->buffer))
71 n = lookup_node(name);
74 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
75 "KEY_CHANGED", c->name, c->hostname, name);
79 n->status.validkey = false;
80 n->status.waitingforkey = false;
90 bool send_req_key(node_t *to)
94 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
97 bool req_key_h(connection_t *c)
99 char from_name[MAX_STRING_SIZE];
100 char to_name[MAX_STRING_SIZE];
105 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
106 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->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)
153 // Set key parameters
154 to->incipher = myself->incipher;
155 to->inkeylength = myself->inkeylength;
156 to->indigest = myself->indigest;
157 to->incompression = myself->incompression;
159 // Allocate memory for key
160 to->inkey = xrealloc(to->inkey, to->inkeylength);
163 RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
165 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
167 // Reset sequence number and late packet window
169 to->received_seqno = 0;
170 memset(to->late, 0, sizeof(to->late));
172 // Convert to hexadecimal and send
173 key = alloca(2 * to->inkeylength + 1);
174 bin2hex(to->inkey, key, to->inkeylength);
175 key[to->inkeylength * 2] = '\0';
177 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
178 myself->name, to->name, key,
179 to->incipher ? to->incipher->nid : 0,
180 to->indigest ? to->indigest->type : 0, to->inmaclength,
184 bool ans_key_h(connection_t *c)
186 char from_name[MAX_STRING_SIZE];
187 char to_name[MAX_STRING_SIZE];
188 char key[MAX_STRING_SIZE];
189 int cipher, digest, maclength, compression;
194 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
195 from_name, to_name, key, &cipher, &digest, &maclength,
196 &compression) != 7) {
197 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->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 return send_request(to->nexthop->connection, "%s", c->buffer);
233 /* Update our copy of the origin's packet key */
234 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
236 from->outkey = xstrdup(key);
237 from->outkeylength = strlen(key) / 2;
238 hex2bin(key, from->outkey, from->outkeylength);
240 from->status.waitingforkey = false;
241 /* Check and lookup cipher and digest algorithms */
244 from->outcipher = EVP_get_cipherbynid(cipher);
246 if(!from->outcipher) {
247 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
252 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
253 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
258 from->outcipher = NULL;
261 from->outmaclength = maclength;
264 from->outdigest = EVP_get_digestbynid(digest);
266 if(!from->outdigest) {
267 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
272 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
273 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
274 from->name, from->hostname);
278 from->outdigest = NULL;
281 if(compression < 0 || compression > 11) {
282 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
286 from->outcompression = compression;
289 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
290 logger(LOG_ERR, _("Error during initialisation of key from %s (%s): %s"),
291 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
295 from->status.validkey = true;
296 from->sent_seqno = 0;
298 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
299 send_mtu_probe(from);