Reset the *correct* seqnos.
[tinc] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5
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.
10
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.
15
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.
19
20     $Id: protocol_key.c,v 1.1.4.11 2002/09/06 14:31:12 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31
32 #include <utils.h>
33 #include <xalloc.h>
34 #include <avl_tree.h>
35
36 #include "conf.h"
37 #include "net.h"
38 #include "netutl.h"
39 #include "protocol.h"
40 #include "meta.h"
41 #include "connection.h"
42 #include "node.h"
43
44 #include "system.h"
45
46 int mykeyused = 0;
47
48 int send_key_changed(connection_t *c, node_t *n)
49 {
50 cp
51   /* Only send this message if some other daemon requested our key previously.
52      This reduces unnecessary key_changed broadcasts.
53   */
54
55   if(n == myself && !mykeyused)
56     return 0;
57 cp
58   return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
59 }
60
61 int key_changed_h(connection_t *c)
62 {
63   char name[MAX_STRING_SIZE];
64   node_t *n;
65 cp
66   if(sscanf(c->buffer, "%*d %*x "MAX_STRING, name) != 1)
67     {
68       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
69              c->name, c->hostname);
70       return -1;
71     }
72
73   if(seen_request(c->buffer))
74     return 0;
75
76   n = lookup_node(name);
77
78   if(!n)
79     {
80       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
81              c->name, c->hostname, name);
82       return -1;
83     }
84
85   n->status.validkey = 0;
86   n->status.waitingforkey = 0;
87
88   /* Tell the others */
89
90   forward_request(c);
91 cp
92   return 0;
93 }
94
95 int send_req_key(connection_t *c, node_t *from, node_t *to)
96 {
97 cp
98   return send_request(c, "%d %s %s", REQ_KEY,
99                       from->name, to->name);
100 }
101
102 int req_key_h(connection_t *c)
103 {
104   char from_name[MAX_STRING_SIZE];
105   char to_name[MAX_STRING_SIZE];
106   node_t *from, *to;
107 cp
108   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
109     {
110        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
111               c->name, c->hostname);
112        return -1;
113     }
114
115   from = lookup_node(from_name);
116
117   if(!from)
118     {
119       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
120              c->name, c->hostname, from_name);
121       return -1;
122     }
123
124   to = lookup_node(to_name);
125   
126   if(!to)
127     {
128       syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
129              c->name, c->hostname, to_name);
130       return -1;
131     }
132
133   /* Check if this key request is for us */
134
135   if(to == myself)      /* Yes, send our own key back */
136     {
137       mykeyused = 1;
138       from->received_seqno = 0;
139       send_ans_key(c, myself, from);
140     }
141   else
142     {
143 /* Proxy keys
144       if(to->status.validkey)
145         {
146           send_ans_key(c, to, from);
147         }
148       else
149 */
150         send_req_key(to->nexthop->connection, from, to);
151     }
152
153 cp
154   return 0;
155 }
156
157 int send_ans_key(connection_t *c, node_t *from, node_t *to)
158 {
159   char key[MAX_STRING_SIZE];
160 cp
161   bin2hex(from->key, key, from->keylength);
162   key[from->keylength * 2] = '\0';
163 cp
164   return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
165                       from->name, to->name, key, from->cipher?from->cipher->nid:0, from->digest?from->digest->type:0, from->maclength, from->compression);
166 }
167
168 int ans_key_h(connection_t *c)
169 {
170   char from_name[MAX_STRING_SIZE];
171   char to_name[MAX_STRING_SIZE];
172   char key[MAX_STRING_SIZE];
173   int cipher, digest, maclength, compression;
174   node_t *from, *to;
175 cp
176   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d", from_name, to_name, key, &cipher, &digest, &maclength, &compression) != 7)
177     {
178        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
179               c->name, c->hostname);
180        return -1;
181     }
182
183   from = lookup_node(from_name);
184
185   if(!from)
186     {
187       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
188              c->name, c->hostname, from_name);
189       return -1;
190     }
191
192   to = lookup_node(to_name);
193
194   if(!to)
195     {
196       syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
197              c->name, c->hostname, to_name);
198       return -1;
199     }
200
201   /* Forward it if necessary */
202
203   if(to != myself)
204     {
205       return send_request(to->nexthop->connection, "%s", c->buffer);
206     }
207
208   /* Update our copy of the origin's packet key */
209
210   if(from->key)
211     free(from->key);
212
213   from->key = xstrdup(key);
214   from->keylength = strlen(key) / 2;
215   hex2bin(from->key, from->key, from->keylength);
216   from->key[from->keylength] = '\0';
217
218   from->status.validkey = 1;
219   from->status.waitingforkey = 0;
220   from->sent_seqno = 0;
221     
222   /* Check and lookup cipher and digest algorithms */
223
224   if(cipher)
225     {
226       from->cipher = EVP_get_cipherbynid(cipher);
227       if(!from->cipher)
228         {
229           syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
230           return -1;
231         }
232       if(from->keylength != from->cipher->key_len + from->cipher->iv_len)
233         {
234           syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
235           return -1;
236         }
237     }
238   else
239     {
240       from->cipher = NULL;
241     }
242
243   from->maclength = maclength;
244
245   if(digest)
246     {
247       from->digest = EVP_get_digestbynid(digest);
248       if(!from->digest)
249         {
250           syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
251           return -1;
252         }
253       if(from->maclength > from->digest->md_size || from->maclength < 0)
254         {
255           syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
256           return -1;
257         }
258     }
259   else
260     {
261       from->digest = NULL;
262     }
263
264   from->compression = compression;
265   
266   flush_queue(from);
267 cp
268   return 0;
269 }