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>
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.16 2003/05/06 21:13:18 guus Exp $
41 #include "connection.h"
48 int send_key_changed(connection_t *c, node_t *n)
52 /* Only send this message if some other daemon requested our key previously.
53 This reduces unnecessary key_changed broadcasts.
56 if(n == myself && !mykeyused)
59 return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
62 int key_changed_h(connection_t *c)
64 char name[MAX_STRING_SIZE];
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);
75 if(seen_request(c->buffer))
78 n = lookup_node(name);
81 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
82 "KEY_CHANGED", c->name, c->hostname, name);
86 n->status.validkey = 0;
87 n->status.waitingforkey = 0;
96 int send_req_key(connection_t *c, node_t *from, node_t *to)
100 return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
103 int req_key_h(connection_t *c)
105 char from_name[MAX_STRING_SIZE];
106 char to_name[MAX_STRING_SIZE];
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,
117 from = lookup_node(from_name);
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);
125 to = lookup_node(to_name);
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);
133 /* Check if this key request is for us */
135 if(to == myself) { /* Yes, send our own key back */
137 from->received_seqno = 0;
138 memset(from->late, 0, sizeof(from->late));
139 send_ans_key(c, myself, from);
141 send_req_key(to->nexthop->connection, from, to);
147 int send_ans_key(connection_t *c, node_t *from, node_t *to)
149 char key[MAX_STRING_SIZE];
153 bin2hex(from->key, key, from->keylength);
154 key[from->keylength * 2] = '\0';
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,
163 int ans_key_h(connection_t *c)
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;
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,
181 from = lookup_node(from_name);
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);
189 to = lookup_node(to_name);
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);
197 /* Forward it if necessary */
200 return send_request(to->nexthop->connection, "%s", c->buffer);
203 /* Update our copy of the origin's packet key */
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';
213 from->status.validkey = 1;
214 from->status.waitingforkey = 0;
215 from->sent_seqno = 0;
217 /* Check and lookup cipher and digest algorithms */
220 from->cipher = EVP_get_cipherbynid(cipher);
223 syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
228 if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
229 syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
237 from->maclength = maclength;
240 from->digest = EVP_get_digestbynid(digest);
243 syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
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);
257 if(compression < 0 || compression > 11) {
258 syslog(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
262 from->compression = compression;
264 EVP_EncryptInit_ex(&from->packet_ctx, from->cipher, NULL, from->key, from->key + from->cipher->key_len);