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 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 bool send_key_changed()
43 /* Only send this message if some other daemon requested our key previously.
44 This reduces unnecessary key_changed broadcasts.
50 return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
53 bool key_changed_h(connection_t *c)
55 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);
66 if(seen_request(c->buffer))
69 n = lookup_node(name);
72 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
73 "KEY_CHANGED", c->name, c->hostname, name);
77 n->status.validkey = false;
78 n->status.waitingforkey = false;
88 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)
97 char from_name[MAX_STRING_SIZE];
98 char to_name[MAX_STRING_SIZE];
103 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
104 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
109 from = lookup_node(from_name);
112 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
113 "REQ_KEY", c->name, c->hostname, from_name);
117 to = lookup_node(to_name);
120 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
121 "REQ_KEY", c->name, c->hostname, to_name);
125 /* Check if this key request is for us */
127 if(to == myself) { /* Yes, send our own key back */
133 if(!to->status.reachable) {
134 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
135 "REQ_KEY", c->name, c->hostname, to_name);
139 send_request(to->nexthop->connection, "%s", c->buffer);
145 bool send_ans_key(node_t *to)
151 // Set key parameters
152 to->incipher = myself->incipher;
153 to->inkeylength = myself->inkeylength;
154 to->indigest = myself->indigest;
155 to->inmaclength = myself->inmaclength;
156 to->incompression = myself->incompression;
158 // Allocate memory for key
159 to->inkey = xrealloc(to->inkey, to->inkeylength);
162 RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
164 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
166 // Reset sequence number and late packet window
168 to->received_seqno = 0;
169 memset(to->late, 0, sizeof(to->late));
171 // Convert to hexadecimal and send
172 key = alloca(2 * to->inkeylength + 1);
173 bin2hex(to->inkey, key, to->inkeylength);
174 key[to->inkeylength * 2] = '\0';
176 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
177 myself->name, to->name, key,
178 to->incipher ? to->incipher->nid : 0,
179 to->indigest ? to->indigest->type : 0, to->inmaclength,
183 bool ans_key_h(connection_t *c)
185 char from_name[MAX_STRING_SIZE];
186 char to_name[MAX_STRING_SIZE];
187 char key[MAX_STRING_SIZE];
188 int cipher, digest, maclength, compression;
193 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
194 from_name, to_name, key, &cipher, &digest, &maclength,
195 &compression) != 7) {
196 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
201 from = lookup_node(from_name);
204 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
205 "ANS_KEY", c->name, c->hostname, from_name);
209 to = lookup_node(to_name);
212 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
213 "ANS_KEY", c->name, c->hostname, to_name);
217 /* Forward it if necessary */
223 if(!to->status.reachable) {
224 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
225 "ANS_KEY", c->name, c->hostname, to_name);
229 return send_request(to->nexthop->connection, "%s", c->buffer);
232 /* Update our copy of the origin's packet key */
233 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
235 from->outkey = xstrdup(key);
236 from->outkeylength = strlen(key) / 2;
237 hex2bin(key, from->outkey, from->outkeylength);
239 from->status.waitingforkey = false;
240 /* Check and lookup cipher and digest algorithms */
243 from->outcipher = EVP_get_cipherbynid(cipher);
245 if(!from->outcipher) {
246 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
251 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
252 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
257 from->outcipher = NULL;
260 from->outmaclength = maclength;
263 from->outdigest = EVP_get_digestbynid(digest);
265 if(!from->outdigest) {
266 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
271 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
272 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
273 from->name, from->hostname);
277 from->outdigest = NULL;
280 if(compression < 0 || compression > 11) {
281 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
285 from->outcompression = compression;
288 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
289 logger(LOG_ERR, _("Error during initialisation of key from %s (%s): %s"),
290 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
294 from->status.validkey = true;
295 from->sent_seqno = 0;
297 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
298 send_mtu_probe(from);