2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2006 Guus Sliepen <guus@tinc-vpn.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.
25 #include <openssl/evp.h>
26 #include <openssl/err.h>
28 #include "splay_tree.h"
29 #include "connection.h"
38 bool mykeyused = false;
40 bool send_key_changed(connection_t *c, const node_t *n) {
43 /* Only send this message if some other daemon requested our key previously.
44 This reduces unnecessary key_changed broadcasts.
47 if(n == myself && !mykeyused)
50 return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
53 bool key_changed_h(connection_t *c) {
54 char name[MAX_STRING_SIZE];
59 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
60 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
61 c->name, c->hostname);
65 if(seen_request(c->buffer))
68 n = lookup_node(name);
71 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
72 "KEY_CHANGED", c->name, c->hostname, name);
76 n->status.validkey = false;
77 n->status.waitingforkey = false;
87 bool send_req_key(connection_t *c, const node_t *from, const node_t *to) {
90 return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
93 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);
133 send_req_key(to->nexthop->connection, from, to);
139 bool send_ans_key(connection_t *c, const node_t *from, const node_t *to) {
144 key = alloca(2 * from->keylength + 1);
145 bin2hex(from->key, key, from->keylength);
146 key[from->keylength * 2] = '\0';
148 return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
149 from->name, to->name, key,
150 from->cipher ? from->cipher->nid : 0,
151 from->digest ? from->digest->type : 0, from->maclength,
155 bool ans_key_h(connection_t *c) {
156 char from_name[MAX_STRING_SIZE];
157 char to_name[MAX_STRING_SIZE];
158 char key[MAX_STRING_SIZE];
159 int cipher, digest, maclength, compression;
164 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
165 from_name, to_name, key, &cipher, &digest, &maclength,
166 &compression) != 7) {
167 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
172 from = lookup_node(from_name);
175 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
176 "ANS_KEY", c->name, c->hostname, from_name);
180 to = lookup_node(to_name);
183 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
184 "ANS_KEY", c->name, c->hostname, to_name);
188 /* Forward it if necessary */
194 return send_request(to->nexthop->connection, "%s", c->buffer);
197 /* Update our copy of the origin's packet key */
202 from->key = xstrdup(key);
203 from->keylength = strlen(key) / 2;
204 hex2bin(from->key, from->key, from->keylength);
205 from->key[from->keylength] = '\0';
207 from->status.validkey = true;
208 from->status.waitingforkey = false;
209 from->sent_seqno = 0;
211 /* Check and lookup cipher and digest algorithms */
214 from->cipher = EVP_get_cipherbynid(cipher);
217 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
222 if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
223 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
231 from->maclength = maclength;
234 from->digest = EVP_get_digestbynid(digest);
237 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
242 if(from->maclength > from->digest->md_size || from->maclength < 0) {
243 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
244 from->name, from->hostname);
251 if(compression < 0 || compression > 11) {
252 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
256 from->compression = compression;
259 if(!EVP_EncryptInit_ex(&from->packet_ctx, from->cipher, NULL, (unsigned char *)from->key, (unsigned char *)from->key + from->cipher->key_len)) {
260 logger(LOG_ERR, _("Error during initialisation of key from %s (%s): %s"),
261 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
265 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
266 send_mtu_probe(from);