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