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 "splay_tree.h"
25 #include "connection.h"
35 static bool mykeyused = false;
37 bool send_key_changed() {
38 /* Only send this message if some other daemon requested our key previously.
39 This reduces unnecessary key_changed broadcasts.
45 return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
48 bool key_changed_h(connection_t *c, char *request) {
49 char name[MAX_STRING_SIZE];
52 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
53 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
54 c->name, c->hostname);
58 if(seen_request(request))
61 n = lookup_node(name);
64 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
65 "KEY_CHANGED", c->name, c->hostname, name);
69 n->status.validkey = false;
70 n->status.waitingforkey = false;
75 forward_request(c, request);
80 bool send_req_key(node_t *to) {
81 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
84 bool req_key_h(connection_t *c, char *request) {
85 char from_name[MAX_STRING_SIZE];
86 char to_name[MAX_STRING_SIZE];
89 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
90 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
95 from = lookup_node(from_name);
98 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
99 "REQ_KEY", c->name, c->hostname, from_name);
103 to = lookup_node(to_name);
106 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
107 "REQ_KEY", c->name, c->hostname, to_name);
111 /* Check if this key request is for us */
113 if(to == myself) { /* Yes, send our own key back */
120 if(!to->status.reachable) {
121 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
122 "REQ_KEY", c->name, c->hostname, to_name);
126 send_request(to->nexthop->connection, "%s", request);
132 bool send_ans_key(node_t *to) {
133 size_t keylen = cipher_keylength(&myself->incipher);
134 char key[keylen * 2 + 1];
136 cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
137 digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
138 to->incompression = myself->incompression;
140 randomize(key, keylen);
141 cipher_set_key(&to->incipher, key, true);
143 bin2hex(key, key, keylen);
144 key[keylen * 2] = '\0';
146 // Reset sequence number and late packet window
148 to->received_seqno = 0;
149 memset(to->late, 0, sizeof(to->late));
151 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
152 myself->name, to->name, key,
153 cipher_get_nid(&to->incipher),
154 digest_get_nid(&to->indigest),
155 digest_length(&to->indigest),
159 bool ans_key_h(connection_t *c, char *request) {
160 char from_name[MAX_STRING_SIZE];
161 char to_name[MAX_STRING_SIZE];
162 char key[MAX_STRING_SIZE];
163 int cipher, digest, maclength, compression;
166 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
167 from_name, to_name, key, &cipher, &digest, &maclength,
168 &compression) != 7) {
169 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
174 from = lookup_node(from_name);
177 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
178 "ANS_KEY", c->name, c->hostname, from_name);
182 to = lookup_node(to_name);
185 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
186 "ANS_KEY", c->name, c->hostname, to_name);
190 /* Forward it if necessary */
196 if(!to->status.reachable) {
197 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
198 "ANS_KEY", c->name, c->hostname, to_name);
202 return send_request(to->nexthop->connection, "%s", request);
205 /* Check and lookup cipher and digest algorithms */
207 if(!cipher_open_by_nid(&from->outcipher, cipher)) {
208 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
212 if(strlen(key) / 2 != cipher_keylength(&from->outcipher)) {
213 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
217 if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
218 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
222 if(maclength != digest_length(&from->outdigest)) {
223 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
227 if(compression < 0 || compression > 11) {
228 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
232 from->outcompression = compression;
234 /* Update our copy of the origin's packet key */
236 hex2bin(key, key, cipher_keylength(&from->outcipher));
237 cipher_set_key(&from->outcipher, key, false);
239 from->status.validkey = true;
240 from->status.waitingforkey = false;
241 from->sent_seqno = 0;
243 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
244 send_mtu_probe(from);