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 "splay_tree.h"
27 #include "connection.h"
37 static bool mykeyused = false;
39 bool send_key_changed() {
42 /* Only send this message if some other daemon requested our key previously.
43 This reduces unnecessary key_changed broadcasts.
49 return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
52 bool key_changed_h(connection_t *c, char *request) {
53 char name[MAX_STRING_SIZE];
58 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
59 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
60 c->name, c->hostname);
64 if(seen_request(request))
67 n = lookup_node(name);
70 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
71 "KEY_CHANGED", c->name, c->hostname, name);
75 n->status.validkey = false;
76 n->status.waitingforkey = false;
81 forward_request(c, request);
86 bool send_req_key(node_t *to) {
89 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
92 bool req_key_h(connection_t *c, char *request) {
93 char from_name[MAX_STRING_SIZE];
94 char to_name[MAX_STRING_SIZE];
99 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
100 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
105 from = lookup_node(from_name);
108 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
109 "REQ_KEY", c->name, c->hostname, from_name);
113 to = lookup_node(to_name);
116 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
117 "REQ_KEY", c->name, c->hostname, to_name);
121 /* Check if this key request is for us */
123 if(to == myself) { /* Yes, send our own key back */
130 if(!to->status.reachable) {
131 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
132 "REQ_KEY", c->name, c->hostname, to_name);
136 send_request(to->nexthop->connection, "%s", request);
142 bool send_ans_key(node_t *to) {
143 size_t keylen = cipher_keylength(&myself->incipher);
144 char key[keylen * 2 + 1];
148 cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
149 digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
150 to->incompression = myself->incompression;
152 randomize(key, keylen);
153 cipher_set_key(&to->incipher, key, true);
155 bin2hex(key, key, keylen);
156 key[keylen * 2] = '\0';
158 // Reset sequence number and late packet window
160 to->received_seqno = 0;
161 memset(to->late, 0, sizeof(to->late));
163 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
164 myself->name, to->name, key,
165 cipher_get_nid(&to->incipher),
166 digest_get_nid(&to->indigest),
167 digest_length(&to->indigest),
171 bool ans_key_h(connection_t *c, char *request) {
172 char from_name[MAX_STRING_SIZE];
173 char to_name[MAX_STRING_SIZE];
174 char key[MAX_STRING_SIZE];
175 int cipher, digest, maclength, compression;
180 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
181 from_name, to_name, key, &cipher, &digest, &maclength,
182 &compression) != 7) {
183 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
188 from = lookup_node(from_name);
191 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
192 "ANS_KEY", c->name, c->hostname, from_name);
196 to = lookup_node(to_name);
199 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
200 "ANS_KEY", c->name, c->hostname, to_name);
204 /* Forward it if necessary */
210 if(!to->status.reachable) {
211 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
212 "ANS_KEY", c->name, c->hostname, to_name);
216 return send_request(to->nexthop->connection, "%s", request);
219 /* Check and lookup cipher and digest algorithms */
221 if(!cipher_open_by_nid(&from->outcipher, cipher)) {
222 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
226 if(strlen(key) / 2 != cipher_keylength(&from->outcipher)) {
227 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
231 if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
232 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
236 if(maclength != digest_length(&from->outdigest)) {
237 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
241 if(compression < 0 || compression > 11) {
242 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
246 from->outcompression = compression;
248 /* Update our copy of the origin's packet key */
250 hex2bin(key, key, cipher_keylength(&from->outcipher));
251 cipher_set_key(&from->outcipher, key, false);
253 from->status.validkey = true;
254 from->status.waitingforkey = false;
255 from->sent_seqno = 0;
257 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
258 send_mtu_probe(from);