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