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>
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.
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.
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.
20 $Id: protocol_key.c,v 1.1.4.3 2002/02/20 19:25:09 guus Exp $
41 #include "connection.h"
49 int send_key_changed(connection_t *c, node_t *n)
54 /* Only send this message if some other daemon requested our key previously.
55 This reduces unnecessary key_changed broadcasts.
58 if(n == myself && !mykeyused)
61 for(node = connection_tree->head; node; node = node->next)
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);
71 int key_changed_h(connection_t *c)
73 char name[MAX_STRING_SIZE];
76 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
78 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
79 c->name, c->hostname);
83 n = lookup_node(name);
87 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
88 c->name, c->hostname, name);
92 n->status.validkey = 0;
93 n->status.waitingforkey = 0;
96 send_key_changed(c, n);
101 int send_req_key(connection_t *c, node_t *from, node_t *to)
104 return send_request(c, "%d %s %s", REQ_KEY,
105 from->name, to->name);
108 int req_key_h(connection_t *c)
110 char from_name[MAX_STRING_SIZE];
111 char to_name[MAX_STRING_SIZE];
114 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
116 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
117 c->name, c->hostname);
121 from = lookup_node(from_name);
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);
130 to = lookup_node(to_name);
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);
139 /* Check if this key request is for us */
141 if(to == myself) /* Yes, send our own key back */
144 from->received_seqno = 0;
145 send_ans_key(c, myself, from);
150 if(to->status.validkey)
152 send_ans_key(c, to, from);
156 send_req_key(to->nexthop->connection, from, to);
163 int send_ans_key(connection_t *c, node_t *from, node_t *to)
165 char key[MAX_STRING_SIZE];
167 bin2hex(from->key, key, from->keylength);
168 key[from->keylength * 2] = '\0';
170 return send_request(c, "%d %s %s %s %d %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, from->compression);
174 int ans_key_h(connection_t *c)
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, compression;
182 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)
184 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
185 c->name, c->hostname);
189 from = lookup_node(from_name);
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);
198 to = lookup_node(to_name);
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);
207 /* Forward it if necessary */
211 return send_request(to->nexthop->connection, c->buffer);
214 /* Update our copy of the origin's packet key */
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';
224 from->status.validkey = 1;
225 from->status.waitingforkey = 0;
227 /* Check and lookup cipher and digest algorithms */
231 from->cipher = EVP_get_cipherbynid(cipher);
234 syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
237 if(from->keylength != from->cipher->key_len + from->cipher->iv_len)
239 syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
248 from->maclength = maclength;
252 from->digest = EVP_get_digestbynid(digest);
255 syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
258 if(from->maclength > from->digest->md_size || from->maclength < 0)
260 syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
269 from->compression = compression;