2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2009 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <openssl/evp.h>
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
28 #include "connection.h"
37 bool mykeyused = false;
39 bool send_key_changed() {
40 /* Only send this message if some other daemon requested our key previously.
41 This reduces unnecessary key_changed broadcasts.
47 return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
50 bool key_changed_h(connection_t *c) {
51 char name[MAX_STRING_SIZE];
54 if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
55 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
56 c->name, c->hostname);
60 if(seen_request(c->buffer))
63 n = lookup_node(name);
66 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
67 "KEY_CHANGED", c->name, c->hostname, name);
71 n->status.validkey = false;
72 n->status.waitingforkey = false;
82 bool send_req_key(node_t *to) {
83 return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
86 bool req_key_h(connection_t *c) {
87 char from_name[MAX_STRING_SIZE];
88 char to_name[MAX_STRING_SIZE];
91 if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
92 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
97 from = lookup_node(from_name);
100 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
101 "REQ_KEY", c->name, c->hostname, from_name);
105 to = lookup_node(to_name);
108 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
109 "REQ_KEY", c->name, c->hostname, to_name);
113 /* Check if this key request is for us */
115 if(to == myself) { /* Yes, send our own key back */
121 if(!to->status.reachable) {
122 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
123 "REQ_KEY", c->name, c->hostname, to_name);
127 send_request(to->nexthop->connection, "%s", c->buffer);
133 bool send_ans_key(node_t *to) {
136 // Set key parameters
137 to->incipher = myself->incipher;
138 to->inkeylength = myself->inkeylength;
139 to->indigest = myself->indigest;
140 to->inmaclength = myself->inmaclength;
141 to->incompression = myself->incompression;
143 // Allocate memory for key
144 to->inkey = xrealloc(to->inkey, to->inkeylength);
147 RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
149 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
151 // Reset sequence number and late packet window
153 to->received_seqno = 0;
154 memset(to->late, 0, sizeof(to->late));
156 // Convert to hexadecimal and send
157 key = alloca(2 * to->inkeylength + 1);
158 bin2hex(to->inkey, key, to->inkeylength);
159 key[to->inkeylength * 2] = '\0';
161 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
162 myself->name, to->name, key,
163 to->incipher ? to->incipher->nid : 0,
164 to->indigest ? to->indigest->type : 0, to->inmaclength,
168 bool ans_key_h(connection_t *c) {
169 char from_name[MAX_STRING_SIZE];
170 char to_name[MAX_STRING_SIZE];
171 char key[MAX_STRING_SIZE];
172 int cipher, digest, maclength, compression;
175 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
176 from_name, to_name, key, &cipher, &digest, &maclength,
177 &compression) != 7) {
178 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
183 from = lookup_node(from_name);
186 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
187 "ANS_KEY", c->name, c->hostname, from_name);
191 to = lookup_node(to_name);
194 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
195 "ANS_KEY", c->name, c->hostname, to_name);
199 /* Forward it if necessary */
205 if(!to->status.reachable) {
206 logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
207 "ANS_KEY", c->name, c->hostname, to_name);
211 return send_request(to->nexthop->connection, "%s", c->buffer);
214 /* Update our copy of the origin's packet key */
215 from->outkey = xrealloc(from->outkey, strlen(key) / 2);
217 from->outkey = xstrdup(key);
218 from->outkeylength = strlen(key) / 2;
219 hex2bin(key, from->outkey, from->outkeylength);
221 from->status.waitingforkey = false;
222 /* Check and lookup cipher and digest algorithms */
225 from->outcipher = EVP_get_cipherbynid(cipher);
227 if(!from->outcipher) {
228 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
233 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
234 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
239 from->outcipher = NULL;
242 from->outmaclength = maclength;
245 from->outdigest = EVP_get_digestbynid(digest);
247 if(!from->outdigest) {
248 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
253 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
254 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
255 from->name, from->hostname);
259 from->outdigest = NULL;
262 if(compression < 0 || compression > 11) {
263 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
267 from->outcompression = compression;
270 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
271 logger(LOG_ERR, _("Error during initialisation of key from %s (%s): %s"),
272 from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
276 from->status.validkey = true;
277 from->sent_seqno = 0;
279 if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
280 send_mtu_probe(from);