2 protocol_auth.c -- handle the meta-protocol, authentication
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
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 gettimeofday(&c->start, NULL);
45 return send_request(c, "%d %s %d", ID, myself->connection->name,
46 myself->connection->protocol_version);
49 bool id_h(connection_t *c, char *request) {
50 char name[MAX_STRING_SIZE];
54 if(sscanf(request, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
55 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
60 /* Check if identity is a valid name */
63 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
64 c->hostname, "invalid name");
68 /* If this is an outgoing connection, make sure we are connected to the right host */
71 if(strcmp(c->name, name)) {
72 logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
79 c->name = xstrdup(name);
82 /* Check if version matches */
84 if(c->protocol_version != myself->connection->protocol_version) {
85 logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
86 c->name, c->hostname, c->protocol_version);
92 init_configuration(&c->config_tree);
93 c->allow_request = ACK;
98 init_configuration(&c->config_tree);
100 if(!read_connection_config(c)) {
101 logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
107 if(!read_rsa_public_key(c)) {
111 c->allow_request = METAKEY;
113 return send_metakey(c);
116 bool send_metakey(connection_t *c) {
117 size_t len = rsa_size(&c->rsa);
120 char hexkey[2 * len + 1];
124 if(!cipher_open_blowfish_ofb(&c->outcipher))
127 if(!digest_open_sha1(&c->outdigest, -1))
130 /* Create a random key */
134 /* The message we send must be smaller than the modulus of the RSA key.
135 By definition, for a key of k bits, the following formula holds:
137 2^(k-1) <= modulus < 2^(k)
139 Where ^ means "to the power of", not "xor".
140 This means that to be sure, we must choose our message < 2^(k-1).
141 This can be done by setting the most significant bit to zero.
146 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
148 ifdebug(SCARY_THINGS) {
149 bin2hex(key, hexkey, len);
150 hexkey[len * 2] = '\0';
151 logger(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), hexkey);
154 /* Encrypt the random data
156 We do not use one of the PKCS padding schemes here.
157 This is allowed, because we encrypt a totally random string
158 with a length equal to that of the modulus of the RSA key.
161 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
162 logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
166 /* Convert the encrypted random data to a hexadecimal formatted string */
168 bin2hex(enckey, hexkey, len);
169 hexkey[len * 2] = '\0';
171 /* Send the meta key */
173 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
174 cipher_get_nid(&c->outcipher),
175 digest_get_nid(&c->outdigest), c->outmaclength,
176 c->outcompression, hexkey);
178 c->status.encryptout = true;
182 bool metakey_h(connection_t *c, char *request) {
183 char hexkey[MAX_STRING_SIZE];
184 int cipher, digest, maclength, compression;
185 size_t len = rsa_size(&myself->connection->rsa);
191 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
192 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
196 /* Check if the length of the meta key is all right */
198 if(strlen(hexkey) != len * 2) {
199 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
203 /* Convert the challenge from hexadecimal back to binary */
205 hex2bin(hexkey, enckey, len);
207 /* Decrypt the meta key */
209 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
210 logger(LOG_ERR, _("Error during decryption of meta key for %s (%s)"), c->name, c->hostname);
214 ifdebug(SCARY_THINGS) {
215 bin2hex(key, hexkey, len);
216 hexkey[len * 2] = '\0';
217 logger(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), hexkey);
220 /* Check and lookup cipher and digest algorithms */
222 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
223 logger(LOG_ERR, _("Error during initialisation of cipher from %s (%s)"), c->name, c->hostname);
227 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
228 logger(LOG_ERR, _("Error during initialisation of digest from %s (%s)"), c->name, c->hostname);
232 c->status.decryptin = true;
234 c->allow_request = CHALLENGE;
236 return send_challenge(c);
239 bool send_challenge(connection_t *c) {
240 size_t len = rsa_size(&c->rsa);
241 char buffer[len * 2 + 1];
246 c->hischallenge = xrealloc(c->hischallenge, len);
248 /* Copy random data to the buffer */
250 randomize(c->hischallenge, len);
254 bin2hex(c->hischallenge, buffer, len);
255 buffer[len * 2] = '\0';
257 /* Send the challenge */
259 return send_request(c, "%d %s", CHALLENGE, buffer);
262 bool challenge_h(connection_t *c, char *request) {
263 char buffer[MAX_STRING_SIZE];
264 size_t len = rsa_size(&myself->connection->rsa);
265 size_t digestlen = digest_length(&c->outdigest);
266 char digest[digestlen];
270 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
271 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
275 /* Check if the length of the challenge is all right */
277 if(strlen(buffer) != len * 2) {
278 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
282 /* Convert the challenge from hexadecimal back to binary */
284 hex2bin(buffer, buffer, len);
286 c->allow_request = CHAL_REPLY;
290 /* Calculate the hash from the challenge we received */
292 digest_create(&c->indigest, buffer, len, digest);
294 /* Convert the hash to a hexadecimal formatted string */
296 bin2hex(digest, buffer, digestlen);
297 buffer[digestlen * 2] = '\0';
301 return send_request(c, "%d %s", CHAL_REPLY, buffer);
304 bool chal_reply_h(connection_t *c, char *request) {
305 char hishash[MAX_STRING_SIZE];
309 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
310 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
315 /* Check if the length of the hash is all right */
317 if(strlen(hishash) != digest_length(&c->outdigest) * 2) {
318 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
322 /* Convert the hash to binary format */
324 hex2bin(hishash, hishash, digest_length(&c->outdigest));
326 /* Verify the hash */
328 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
329 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
333 /* Identity has now been positively verified.
334 Send an acknowledgement with the rest of the information needed.
337 free(c->hischallenge);
338 c->hischallenge = NULL;
339 c->allow_request = ACK;
344 bool send_ack(connection_t *c) {
345 /* ACK message contains rest of the information the other end needs
346 to create node_t and edge_t structures. */
353 /* Estimate weight */
355 gettimeofday(&now, NULL);
356 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
358 /* Check some options */
360 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
361 c->options |= OPTION_INDIRECT;
363 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
364 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
366 if(myself->options & OPTION_PMTU_DISCOVERY)
367 c->options |= OPTION_PMTU_DISCOVERY;
369 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
371 return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options);
374 static void send_everything(connection_t *c) {
375 splay_node_t *node, *node2;
380 /* Send all known subnets and edges */
383 for(node = myself->subnet_tree->head; node; node = node->next) {
385 send_add_subnet(c, s);
391 for(node = node_tree->head; node; node = node->next) {
394 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
396 send_add_subnet(c, s);
399 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
406 bool ack_h(connection_t *c, char *request) {
407 char hisport[MAX_STRING_SIZE];
408 char *hisaddress, *dummy;
415 if(sscanf(request, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
416 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
421 /* Check if we already have a node_t for him */
423 n = lookup_node(c->name);
427 n->name = xstrdup(c->name);
431 /* Oh dear, we already have a connection to this node. */
432 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->connection->name, n->connection->hostname);
434 if(n->connection->outgoing) {
436 logger(LOG_WARNING, _("Two outgoing connections to the same node!"));
438 c->outgoing = n->connection->outgoing;
440 n->connection->outgoing = NULL;
443 terminate_connection(n->connection, false);
444 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
451 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
452 c->options &= ~OPTION_PMTU_DISCOVERY;
453 options &= ~OPTION_PMTU_DISCOVERY;
455 c->options |= options;
457 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
460 if(get_config_int(lookup_config(myself->connection->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
463 /* Activate this connection */
465 c->allow_request = ALL;
466 c->status.active = true;
468 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
471 /* Send him everything we know */
475 /* Create an edge_t for this connection */
477 c->edge = new_edge();
479 c->edge->from = myself;
481 sockaddr2str(&c->address, &hisaddress, &dummy);
482 c->edge->address = str2sockaddr(hisaddress, hisport);
485 c->edge->weight = (weight + c->estimated_weight) / 2;
486 c->edge->connection = c;
487 c->edge->options = c->options;
491 /* Notify everyone of the new edge */
494 send_add_edge(c, c->edge);
496 send_add_edge(broadcast, c->edge);
498 /* Run MST and SSSP algorithms */