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