protocol_key.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. protocol_key.c -- handle the meta-protocol, key exchange
  3. Copyright (C) 1999-2005 Ivo Timmermans,
  4. 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include <openssl/evp.h>
  19. #include <openssl/err.h>
  20. #include <openssl/rand.h>
  21. #include "avl_tree.h"
  22. #include "connection.h"
  23. #include "logger.h"
  24. #include "net.h"
  25. #include "netutl.h"
  26. #include "node.h"
  27. #include "protocol.h"
  28. #include "utils.h"
  29. #include "xalloc.h"
  30. static bool mykeyused = false;
  31. void send_key_changed(void) {
  32. avl_node_t *node;
  33. connection_t *c;
  34. send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
  35. /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
  36. for(node = connection_tree->head; node; node = node->next) {
  37. c = node->data;
  38. if(c->status.active && c->node && c->node->status.reachable)
  39. send_ans_key(c->node);
  40. }
  41. }
  42. bool key_changed_h(connection_t *c) {
  43. char name[MAX_STRING_SIZE];
  44. node_t *n;
  45. if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
  46. logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
  47. c->name, c->hostname);
  48. return false;
  49. }
  50. if(!check_id(name)) {
  51. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
  52. return false;
  53. }
  54. if(seen_request(c->buffer))
  55. return true;
  56. n = lookup_node(name);
  57. if(!n) {
  58. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
  59. "KEY_CHANGED", c->name, c->hostname, name);
  60. return true;
  61. }
  62. n->status.validkey = false;
  63. n->last_req_key = 0;
  64. /* Tell the others */
  65. if(!tunnelserver)
  66. forward_request(c);
  67. return true;
  68. }
  69. bool send_req_key(node_t *to) {
  70. return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
  71. }
  72. bool req_key_h(connection_t *c) {
  73. char from_name[MAX_STRING_SIZE];
  74. char to_name[MAX_STRING_SIZE];
  75. node_t *from, *to;
  76. if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
  77. logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
  78. c->hostname);
  79. return false;
  80. }
  81. if(!check_id(from_name) || !check_id(to_name)) {
  82. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
  83. return false;
  84. }
  85. from = lookup_node(from_name);
  86. if(!from) {
  87. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
  88. "REQ_KEY", c->name, c->hostname, from_name);
  89. return true;
  90. }
  91. to = lookup_node(to_name);
  92. if(!to) {
  93. logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
  94. "REQ_KEY", c->name, c->hostname, to_name);
  95. return true;
  96. }
  97. /* Check if this key request is for us */
  98. if(to == myself) { /* Yes, send our own key back */
  99. send_ans_key(from);
  100. } else {
  101. if(tunnelserver)
  102. return true;
  103. if(!to->status.reachable) {
  104. logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
  105. "REQ_KEY", c->name, c->hostname, to_name);
  106. return true;
  107. }
  108. send_request(to->nexthop->connection, "%s", c->buffer);
  109. }
  110. return true;
  111. }
  112. bool send_ans_key(node_t *to) {
  113. // Set key parameters
  114. to->incipher = myself->incipher;
  115. to->inkeylength = myself->inkeylength;
  116. to->indigest = myself->indigest;
  117. to->inmaclength = myself->inmaclength;
  118. to->incompression = myself->incompression;
  119. // Allocate memory for key
  120. to->inkey = xrealloc(to->inkey, to->inkeylength);
  121. // Create a new key
  122. RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
  123. if(to->incipher)
  124. EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
  125. // Reset sequence number and late packet window
  126. mykeyused = true;
  127. to->received_seqno = 0;
  128. if(replaywin) memset(to->late, 0, replaywin);
  129. // Convert to hexadecimal and send
  130. char key[2 * to->inkeylength + 1];
  131. bin2hex(to->inkey, key, to->inkeylength);
  132. key[to->inkeylength * 2] = '\0';
  133. return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
  134. myself->name, to->name, key,
  135. to->incipher ? to->incipher->nid : 0,
  136. to->indigest ? to->indigest->type : 0, to->inmaclength,
  137. to->incompression);
  138. }
  139. bool ans_key_h(connection_t *c) {
  140. char from_name[MAX_STRING_SIZE];
  141. char to_name[MAX_STRING_SIZE];
  142. char key[MAX_STRING_SIZE];
  143. char address[MAX_STRING_SIZE] = "";
  144. char port[MAX_STRING_SIZE] = "";
  145. int cipher, digest, maclength, compression;
  146. node_t *from, *to;
  147. if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
  148. from_name, to_name, key, &cipher, &digest, &maclength,
  149. &compression, address, port) < 7) {
  150. logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
  151. c->hostname);
  152. return false;
  153. }
  154. if(!check_id(from_name) || !check_id(to_name)) {
  155. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
  156. return false;
  157. }
  158. from = lookup_node(from_name);
  159. if(!from) {
  160. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
  161. "ANS_KEY", c->name, c->hostname, from_name);
  162. return true;
  163. }
  164. to = lookup_node(to_name);
  165. if(!to) {
  166. logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
  167. "ANS_KEY", c->name, c->hostname, to_name);
  168. return true;
  169. }
  170. /* Forward it if necessary */
  171. if(to != myself) {
  172. if(tunnelserver)
  173. return true;
  174. if(!to->status.reachable) {
  175. logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
  176. "ANS_KEY", c->name, c->hostname, to_name);
  177. return true;
  178. }
  179. if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
  180. char *address, *port;
  181. ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
  182. sockaddr2str(&from->address, &address, &port);
  183. send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
  184. free(address);
  185. free(port);
  186. return true;
  187. }
  188. return send_request(to->nexthop->connection, "%s", c->buffer);
  189. }
  190. /* Update our copy of the origin's packet key */
  191. from->outkey = xrealloc(from->outkey, strlen(key) / 2);
  192. from->outkeylength = strlen(key) / 2;
  193. hex2bin(key, from->outkey, from->outkeylength);
  194. /* Check and lookup cipher and digest algorithms */
  195. if(cipher) {
  196. from->outcipher = EVP_get_cipherbynid(cipher);
  197. if(!from->outcipher) {
  198. logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
  199. from->hostname);
  200. return true;
  201. }
  202. if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
  203. logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
  204. from->hostname);
  205. return true;
  206. }
  207. } else {
  208. from->outcipher = NULL;
  209. }
  210. from->outmaclength = maclength;
  211. if(digest) {
  212. from->outdigest = EVP_get_digestbynid(digest);
  213. if(!from->outdigest) {
  214. logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
  215. from->hostname);
  216. return true;
  217. }
  218. if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
  219. logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
  220. from->name, from->hostname);
  221. return true;
  222. }
  223. } else {
  224. from->outdigest = NULL;
  225. }
  226. if(compression < 0 || compression > 11) {
  227. logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
  228. return true;
  229. }
  230. from->outcompression = compression;
  231. if(from->outcipher)
  232. if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
  233. logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
  234. from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
  235. return true;
  236. }
  237. from->status.validkey = true;
  238. from->sent_seqno = 0;
  239. if(*address && *port) {
  240. ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
  241. sockaddr_t sa = str2sockaddr(address, port);
  242. update_node_udp(from, &sa);
  243. }
  244. if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent)
  245. send_mtu_probe(from);
  246. return true;
  247. }