2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2006 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"
36 static bool mykeyused = false;
38 bool send_key_changed(connection_t *c, const node_t *n) {
41 /* Only send this message if some other daemon requested our key previously.
42 This reduces unnecessary key_changed broadcasts.
45 if(n == myself && !mykeyused)
48 return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
51 bool key_changed_h(connection_t *c, char *request) {
52 char name[MAX_STRING_SIZE];
57 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
58 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
59 c->name, c->hostname);
63 if(seen_request(request))
66 n = lookup_node(name);
69 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
70 "KEY_CHANGED", c->name, c->hostname, name);
74 n->status.validkey = false;
75 n->status.waitingforkey = false;
80 forward_request(c, request);
85 bool send_req_key(connection_t *c, const node_t *from, const node_t *to) {
88 return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
91 bool req_key_h(connection_t *c, char *request) {
92 char from_name[MAX_STRING_SIZE];
93 char to_name[MAX_STRING_SIZE];
98 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
99 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
104 from = lookup_node(from_name);
107 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
108 "REQ_KEY", c->name, c->hostname, from_name);
112 to = lookup_node(to_name);
115 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
116 "REQ_KEY", c->name, c->hostname, to_name);
120 /* Check if this key request is for us */
122 if(to == myself) { /* Yes, send our own key back */
124 from->received_seqno = 0;
125 memset(from->late, 0, sizeof(from->late));
126 send_ans_key(c, myself, from);
131 send_req_key(to->nexthop->connection, from, to);
137 bool send_ans_key(connection_t *c, const node_t *from, const node_t *to) {
138 size_t keylen = cipher_keylength(&from->cipher);
143 cipher_get_key(&from->cipher, key);
144 bin2hex(key, key, keylen);
145 key[keylen * 2] = '\0';
147 return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
148 from->name, to->name, key,
149 cipher_get_nid(&from->cipher),
150 digest_get_nid(&from->digest), from->maclength,
154 bool ans_key_h(connection_t *c, char *request) {
155 char from_name[MAX_STRING_SIZE];
156 char to_name[MAX_STRING_SIZE];
157 char key[MAX_STRING_SIZE];
158 int cipher, digest, maclength, compression;
163 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
164 from_name, to_name, key, &cipher, &digest, &maclength,
165 &compression) != 7) {
166 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
171 from = lookup_node(from_name);
174 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
175 "ANS_KEY", c->name, c->hostname, from_name);
179 to = lookup_node(to_name);
182 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
183 "ANS_KEY", c->name, c->hostname, to_name);
187 /* Forward it if necessary */
193 return send_request(to->nexthop->connection, "%s", request);
196 /* Check and lookup cipher and digest algorithms */
198 if(!cipher_open_by_nid(&from->cipher, cipher)) {
199 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
203 if(strlen(key) / 2 != cipher_keylength(&from->cipher)) {
204 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
208 from->maclength = maclength;
210 if(!digest_open_by_nid(&from->digest, digest)) {
211 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
215 if(from->maclength > digest_length(&from->digest) || from->maclength < 0) {
216 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
220 if(compression < 0 || compression > 11) {
221 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
225 from->compression = compression;
227 /* Update our copy of the origin's packet key */
229 hex2bin(key, key, cipher_keylength(&from->cipher));
230 cipher_set_key(&from->cipher, key, false);
232 from->status.validkey = true;
233 from->status.waitingforkey = false;
234 from->sent_seqno = 0;
236 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
237 send_mtu_probe(from);