2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2007 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 "splay_tree.h"
27 #include "connection.h"
40 bool send_id(connection_t *c) {
43 return send_request(c, "%d %s %d", ID, myself->connection->name,
44 myself->connection->protocol_version);
47 bool id_h(connection_t *c, char *request) {
48 char name[MAX_STRING_SIZE];
52 if(sscanf(request, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
53 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
58 /* Check if identity is a valid name */
61 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
62 c->hostname, "invalid name");
66 /* If this is an outgoing connection, make sure we are connected to the right host */
69 if(strcmp(c->name, name)) {
70 logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
77 c->name = xstrdup(name);
80 /* Check if version matches */
82 if(c->protocol_version != myself->connection->protocol_version) {
83 logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
84 c->name, c->hostname, c->protocol_version);
90 init_configuration(&c->config_tree);
91 c->allow_request = ACK;
96 init_configuration(&c->config_tree);
98 if(!read_connection_config(c)) {
99 logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
105 if(!read_rsa_public_key(c)) {
109 c->allow_request = METAKEY;
111 return send_metakey(c);
114 bool send_metakey(connection_t *c) {
115 size_t len = rsa_size(&c->rsa);
118 char hexkey[2 * len + 1];
122 if(!cipher_open_blowfish_ofb(&c->outcipher))
125 if(!digest_open_sha1(&c->outdigest))
128 /* Create a random key */
132 /* The message we send must be smaller than the modulus of the RSA key.
133 By definition, for a key of k bits, the following formula holds:
135 2^(k-1) <= modulus < 2^(k)
137 Where ^ means "to the power of", not "xor".
138 This means that to be sure, we must choose our message < 2^(k-1).
139 This can be done by setting the most significant bit to zero.
144 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
146 ifdebug(SCARY_THINGS) {
147 bin2hex(key, hexkey, len);
148 hexkey[len * 2] = '\0';
149 logger(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), hexkey);
152 /* Encrypt the random data
154 We do not use one of the PKCS padding schemes here.
155 This is allowed, because we encrypt a totally random string
156 with a length equal to that of the modulus of the RSA key.
159 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
160 logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
164 /* Convert the encrypted random data to a hexadecimal formatted string */
166 bin2hex(enckey, hexkey, len);
167 hexkey[len * 2] = '\0';
169 /* Send the meta key */
171 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
172 cipher_get_nid(&c->outcipher),
173 digest_get_nid(&c->outdigest), c->outmaclength,
174 c->outcompression, hexkey);
176 c->status.encryptout = true;
180 bool metakey_h(connection_t *c, char *request) {
181 char hexkey[MAX_STRING_SIZE];
182 int cipher, digest, maclength, compression;
183 size_t len = rsa_size(&myself->connection->rsa);
189 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
190 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
194 /* Check if the length of the meta key is all right */
196 if(strlen(hexkey) != len * 2) {
197 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
201 /* Convert the challenge from hexadecimal back to binary */
203 hex2bin(hexkey, enckey, len);
205 /* Decrypt the meta key */
207 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
208 logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
212 ifdebug(SCARY_THINGS) {
213 bin2hex(key, hexkey, len);
214 hexkey[len * 2] = '\0';
215 logger(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), hexkey);
218 /* Check and lookup cipher and digest algorithms */
220 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
221 logger(LOG_ERR, _("Error during initialisation of cipher from %s (%s)"), c->name, c->hostname);
225 if(!digest_open_by_nid(&c->indigest, digest)) {
226 logger(LOG_ERR, _("Error during initialisation of digest from %s (%s)"), c->name, c->hostname);
230 c->status.decryptin = true;
232 c->allow_request = CHALLENGE;
234 return send_challenge(c);
237 bool send_challenge(connection_t *c) {
238 size_t len = rsa_size(&c->rsa);
239 char buffer[len * 2 + 1];
244 c->hischallenge = xmalloc(len);
246 /* Copy random data to the buffer */
248 randomize(c->hischallenge, len);
252 bin2hex(c->hischallenge, buffer, len);
253 buffer[len * 2] = '\0';
255 /* Send the challenge */
257 return send_request(c, "%d %s", CHALLENGE, buffer);
260 bool challenge_h(connection_t *c, char *request) {
261 char buffer[MAX_STRING_SIZE];
262 size_t len = rsa_size(&myself->connection->rsa);
263 size_t digestlen = digest_length(&c->outdigest);
264 char digest[digestlen];
268 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
269 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
273 /* Check if the length of the challenge is all right */
275 if(strlen(buffer) != len * 2) {
276 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
280 /* Convert the challenge from hexadecimal back to binary */
282 hex2bin(buffer, buffer, len);
284 c->allow_request = CHAL_REPLY;
288 /* Calculate the hash from the challenge we received */
290 digest_create(&c->indigest, buffer, len, digest);
292 /* Convert the hash to a hexadecimal formatted string */
294 bin2hex(digest, buffer, digestlen);
295 buffer[digestlen * 2] = '\0';
299 return send_request(c, "%d %s", CHAL_REPLY, buffer);
302 bool chal_reply_h(connection_t *c, char *request) {
303 char hishash[MAX_STRING_SIZE];
307 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
308 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
313 /* Check if the length of the hash is all right */
315 if(strlen(hishash) != digest_length(&c->outdigest) * 2) {
316 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
320 /* Convert the hash to binary format */
322 hex2bin(hishash, hishash, digest_length(&c->outdigest));
324 /* Verify the hash */
326 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
327 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
331 /* Identity has now been positively verified.
332 Send an acknowledgement with the rest of the information needed.
335 free(c->hischallenge);
336 c->allow_request = ACK;
341 bool send_ack(connection_t *c) {
342 /* ACK message contains rest of the information the other end needs
343 to create node_t and edge_t structures. */
350 /* Estimate weight */
352 gettimeofday(&now, NULL);
353 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
355 /* Check some options */
357 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
358 c->options |= OPTION_INDIRECT;
360 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
361 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
363 if((get_config_bool(lookup_config(c->config_tree, "PMTUDiscovery"), &choice) && choice) || myself->options & OPTION_PMTU_DISCOVERY)
364 c->options |= OPTION_PMTU_DISCOVERY;
366 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
368 return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options);
371 static void send_everything(connection_t *c) {
372 splay_node_t *node, *node2;
377 /* Send all known subnets and edges */
380 for(node = myself->subnet_tree->head; node; node = node->next) {
382 send_add_subnet(c, s);
388 for(node = node_tree->head; node; node = node->next) {
391 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
393 send_add_subnet(c, s);
396 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
403 bool ack_h(connection_t *c, char *request) {
404 char hisport[MAX_STRING_SIZE];
405 char *hisaddress, *dummy;
412 if(sscanf(request, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
413 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
418 /* Check if we already have a node_t for him */
420 n = lookup_node(c->name);
424 n->name = xstrdup(c->name);
428 /* Oh dear, we already have a connection to this node. */
429 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->connection->name, n->connection->hostname);
431 if(n->connection->outgoing) {
433 logger(LOG_WARNING, _("Two outgoing connections to the same node!"));
435 c->outgoing = n->connection->outgoing;
437 n->connection->outgoing = NULL;
440 terminate_connection(n->connection, false);
441 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
448 c->options |= options;
450 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
453 if(get_config_int(lookup_config(myself->connection->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
456 /* Activate this connection */
458 c->allow_request = ALL;
459 c->status.active = true;
461 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
464 /* Send him everything we know */
468 /* Create an edge_t for this connection */
470 c->edge = new_edge();
472 c->edge->from = myself;
474 sockaddr2str(&c->address, &hisaddress, &dummy);
475 c->edge->address = str2sockaddr(hisaddress, hisport);
478 c->edge->weight = (weight + c->estimated_weight) / 2;
479 c->edge->connection = c;
480 c->edge->options = c->options;
484 /* Notify everyone of the new edge */
487 send_add_edge(c, c->edge);
489 send_add_edge(broadcast, c->edge);
491 /* Run MST and SSSP algorithms */