2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.org>
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.22 2003/07/24 12:08:16 guus Exp $
26 #include "connection.h"
35 bool mykeyused = false;
37 bool send_key_changed(connection_t *c, const node_t *n)
41 /* Only send this message if some other daemon requested our key previously.
42 This reduces unnecessary key_changed broadcasts.
45 if(n == myself && !mykeyused)
48 return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
51 bool key_changed_h(connection_t *c)
53 char name[MAX_STRING_SIZE];
58 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
59 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
60 c->name, c->hostname);
64 if(seen_request(c->buffer))
67 n = lookup_node(name);
70 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
71 "KEY_CHANGED", c->name, c->hostname, name);
75 n->status.validkey = false;
76 n->status.waitingforkey = false;
85 bool send_req_key(connection_t *c, const node_t *from, const node_t *to)
89 return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
92 bool req_key_h(connection_t *c)
94 char from_name[MAX_STRING_SIZE];
95 char to_name[MAX_STRING_SIZE];
100 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
101 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
106 from = lookup_node(from_name);
109 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
110 "REQ_KEY", c->name, c->hostname, from_name);
114 to = lookup_node(to_name);
117 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
118 "REQ_KEY", c->name, c->hostname, to_name);
122 /* Check if this key request is for us */
124 if(to == myself) { /* Yes, send our own key back */
126 from->received_seqno = 0;
127 memset(from->late, 0, sizeof(from->late));
128 send_ans_key(c, myself, from);
130 send_req_key(to->nexthop->connection, from, to);
136 bool send_ans_key(connection_t *c, const node_t *from, const node_t *to)
138 char key[MAX_STRING_SIZE];
142 bin2hex(from->key, key, from->keylength);
143 key[from->keylength * 2] = '\0';
145 return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
146 from->name, to->name, key,
147 from->cipher ? from->cipher->nid : 0,
148 from->digest ? from->digest->type : 0, from->maclength,
152 bool ans_key_h(connection_t *c)
154 char from_name[MAX_STRING_SIZE];
155 char to_name[MAX_STRING_SIZE];
156 char key[MAX_STRING_SIZE];
157 int cipher, digest, maclength, compression;
162 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
163 from_name, to_name, key, &cipher, &digest, &maclength,
164 &compression) != 7) {
165 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
170 from = lookup_node(from_name);
173 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
174 "ANS_KEY", c->name, c->hostname, from_name);
178 to = lookup_node(to_name);
181 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
182 "ANS_KEY", c->name, c->hostname, to_name);
186 /* Forward it if necessary */
189 return send_request(to->nexthop->connection, "%s", c->buffer);
192 /* Update our copy of the origin's packet key */
197 from->key = xstrdup(key);
198 from->keylength = strlen(key) / 2;
199 hex2bin(from->key, from->key, from->keylength);
200 from->key[from->keylength] = '\0';
202 from->status.validkey = true;
203 from->status.waitingforkey = false;
204 from->sent_seqno = 0;
206 /* Check and lookup cipher and digest algorithms */
209 from->cipher = EVP_get_cipherbynid(cipher);
212 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
217 if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
218 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
226 from->maclength = maclength;
229 from->digest = EVP_get_digestbynid(digest);
232 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
237 if(from->maclength > from->digest->md_size || from->maclength < 0) {
238 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
239 from->name, from->hostname);
246 if(compression < 0 || compression > 11) {
247 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
251 from->compression = compression;
254 EVP_EncryptInit_ex(&from->packet_ctx, from->cipher, NULL, from->key, from->key + from->cipher->key_len);