Remove redundant spaces.
[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.14 2002/09/09 22:33:03 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                 send_ans_key(c, myself, from);
139         } else {
140                 send_req_key(to->nexthop->connection, from, to);
141         }
142
143         return 0;
144 }
145
146 int send_ans_key(connection_t *c, node_t *from, node_t *to)
147 {
148         char key[MAX_STRING_SIZE];
149
150         cp();
151
152         bin2hex(from->key, key, from->keylength);
153         key[from->keylength * 2] = '\0';
154
155         return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
156                                                 from->name, to->name, key,
157                                                 from->cipher ? from->cipher->nid : 0,
158                                                 from->digest ? from->digest->type : 0, from->maclength,
159                                                 from->compression);
160 }
161
162 int ans_key_h(connection_t *c)
163 {
164         char from_name[MAX_STRING_SIZE];
165         char to_name[MAX_STRING_SIZE];
166         char key[MAX_STRING_SIZE];
167         int cipher, digest, maclength, compression;
168         node_t *from, *to;
169
170         cp();
171
172         if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
173                 from_name, to_name, key, &cipher, &digest, &maclength,
174                 &compression) != 7) {
175                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
176                            c->hostname);
177                 return -1;
178         }
179
180         from = lookup_node(from_name);
181
182         if(!from) {
183                 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
184                            "ANS_KEY", c->name, c->hostname, from_name);
185                 return -1;
186         }
187
188         to = lookup_node(to_name);
189
190         if(!to) {
191                 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
192                            "ANS_KEY", c->name, c->hostname, to_name);
193                 return -1;
194         }
195
196         /* Forward it if necessary */
197
198         if(to != myself) {
199                 return send_request(to->nexthop->connection, "%s", c->buffer);
200         }
201
202         /* Update our copy of the origin's packet key */
203
204         if(from->key)
205                 free(from->key);
206
207         from->key = xstrdup(key);
208         from->keylength = strlen(key) / 2;
209         hex2bin(from->key, from->key, from->keylength);
210         from->key[from->keylength] = '\0';
211
212         from->status.validkey = 1;
213         from->status.waitingforkey = 0;
214         from->sent_seqno = 0;
215
216         /* Check and lookup cipher and digest algorithms */
217
218         if(cipher) {
219                 from->cipher = EVP_get_cipherbynid(cipher);
220
221                 if(!from->cipher) {
222                         syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
223                                    from->hostname);
224                         return -1;
225                 }
226
227                 if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
228                         syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
229                                    from->hostname);
230                         return -1;
231                 }
232         } else {
233                 from->cipher = NULL;
234         }
235
236         from->maclength = maclength;
237
238         if(digest) {
239                 from->digest = EVP_get_digestbynid(digest);
240
241                 if(!from->digest) {
242                         syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
243                                    from->hostname);
244                         return -1;
245                 }
246
247                 if(from->maclength > from->digest->md_size || from->maclength < 0) {
248                         syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
249                                    from->name, from->hostname);
250                         return -1;
251                 }
252         } else {
253                 from->digest = NULL;
254         }
255
256         from->compression = compression;
257
258         flush_queue(from);
259
260         return 0;
261 }