protocol_key.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. protocol_key.c -- handle the meta-protocol, key exchange
  3. Copyright (C) 1999-2005 Ivo Timmermans,
  4. 2000-2016 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. }
  43. bool key_changed_h(connection_t *c) {
  44. char name[MAX_STRING_SIZE];
  45. node_t *n;
  46. if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
  47. logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
  48. c->name, c->hostname);
  49. return false;
  50. }
  51. if(!check_id(name)) {
  52. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
  53. return false;
  54. }
  55. if(seen_request(c->buffer)) {
  56. return true;
  57. }
  58. n = lookup_node(name);
  59. if(!n) {
  60. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
  61. "KEY_CHANGED", c->name, c->hostname, name);
  62. return true;
  63. }
  64. n->status.validkey = false;
  65. n->last_req_key = 0;
  66. /* Tell the others */
  67. if(!tunnelserver) {
  68. forward_request(c);
  69. }
  70. return true;
  71. }
  72. bool send_req_key(node_t *to) {
  73. return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
  74. }
  75. bool req_key_h(connection_t *c) {
  76. char from_name[MAX_STRING_SIZE];
  77. char to_name[MAX_STRING_SIZE];
  78. node_t *from, *to;
  79. if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
  80. logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
  81. c->hostname);
  82. return false;
  83. }
  84. if(!check_id(from_name) || !check_id(to_name)) {
  85. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
  86. return false;
  87. }
  88. from = lookup_node(from_name);
  89. if(!from) {
  90. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
  91. "REQ_KEY", c->name, c->hostname, from_name);
  92. return true;
  93. }
  94. to = lookup_node(to_name);
  95. if(!to) {
  96. logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
  97. "REQ_KEY", c->name, c->hostname, to_name);
  98. return true;
  99. }
  100. /* Check if this key request is for us */
  101. if(to == myself) { /* Yes, send our own key back */
  102. if(!from->status.reachable) {
  103. logger(LOG_WARNING, "Got %s from %s (%s) origin %s which is not reachable",
  104. "REQ_KEY", c->name, c->hostname, from_name);
  105. return true;
  106. }
  107. if(!send_ans_key(from)) {
  108. return false;
  109. }
  110. } else {
  111. if(tunnelserver) {
  112. return true;
  113. }
  114. if(!to->status.reachable) {
  115. logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
  116. "REQ_KEY", c->name, c->hostname, to_name);
  117. return true;
  118. }
  119. send_request(to->nexthop->connection, "%s", c->buffer);
  120. }
  121. return true;
  122. }
  123. bool send_ans_key(node_t *to) {
  124. // Set key parameters
  125. to->incipher = myself->incipher;
  126. to->inkeylength = myself->inkeylength;
  127. to->indigest = myself->indigest;
  128. to->inmaclength = myself->inmaclength;
  129. to->incompression = myself->incompression;
  130. // Allocate memory for key
  131. to->inkey = xrealloc(to->inkey, to->inkeylength);
  132. // Create a new key
  133. if(1 != RAND_bytes((unsigned char *)to->inkey, to->inkeylength)) {
  134. int err = ERR_get_error();
  135. logger(LOG_ERR, "Failed to generate random for key (%s)", ERR_error_string(err, NULL));
  136. return false; // Do not send insecure keys, let connection attempt fail.
  137. }
  138. if(to->incipher) {
  139. EVP_DecryptInit_ex(to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + EVP_CIPHER_key_length(to->incipher));
  140. }
  141. // Reset sequence number and late packet window
  142. mykeyused = true;
  143. to->received_seqno = 0;
  144. if(replaywin) {
  145. memset(to->late, 0, replaywin);
  146. }
  147. // Convert to hexadecimal and send
  148. char key[2 * to->inkeylength + 1];
  149. bin2hex(to->inkey, key, to->inkeylength);
  150. key[to->inkeylength * 2] = '\0';
  151. return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
  152. myself->name, to->name, key,
  153. to->incipher ? EVP_CIPHER_nid(to->incipher) : 0,
  154. to->indigest ? EVP_MD_type(to->indigest) : 0, to->inmaclength,
  155. to->incompression);
  156. }
  157. bool ans_key_h(connection_t *c) {
  158. char from_name[MAX_STRING_SIZE];
  159. char to_name[MAX_STRING_SIZE];
  160. char key[MAX_STRING_SIZE];
  161. char address[MAX_STRING_SIZE] = "";
  162. char port[MAX_STRING_SIZE] = "";
  163. int cipher, digest, maclength, compression;
  164. node_t *from, *to;
  165. if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
  166. from_name, to_name, key, &cipher, &digest, &maclength,
  167. &compression, address, port) < 7) {
  168. logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
  169. c->hostname);
  170. return false;
  171. }
  172. if(!check_id(from_name) || !check_id(to_name)) {
  173. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
  174. return false;
  175. }
  176. from = lookup_node(from_name);
  177. if(!from) {
  178. logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
  179. "ANS_KEY", c->name, c->hostname, from_name);
  180. return true;
  181. }
  182. to = lookup_node(to_name);
  183. if(!to) {
  184. logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
  185. "ANS_KEY", c->name, c->hostname, to_name);
  186. return true;
  187. }
  188. /* Forward it if necessary */
  189. if(to != myself) {
  190. if(tunnelserver) {
  191. return true;
  192. }
  193. if(!to->status.reachable) {
  194. logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
  195. "ANS_KEY", c->name, c->hostname, to_name);
  196. return true;
  197. }
  198. if(!*address && from->address.sa.sa_family != AF_UNSPEC && to->minmtu) {
  199. char *address, *port;
  200. ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
  201. sockaddr2str(&from->address, &address, &port);
  202. send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
  203. free(address);
  204. free(port);
  205. return true;
  206. }
  207. return send_request(to->nexthop->connection, "%s", c->buffer);
  208. }
  209. /* Don't use key material until every check has passed. */
  210. from->status.validkey = false;
  211. /* Update our copy of the origin's packet key */
  212. from->outkey = xrealloc(from->outkey, strlen(key) / 2);
  213. from->outkeylength = strlen(key) / 2;
  214. if(!hex2bin(key, from->outkey, from->outkeylength)) {
  215. logger(LOG_ERR, "Got bad %s from %s(%s): %s", "ANS_KEY", from->name, from->hostname, "invalid key");
  216. return true;
  217. }
  218. /* Check and lookup cipher and digest algorithms */
  219. if(cipher) {
  220. from->outcipher = EVP_get_cipherbynid(cipher);
  221. if(!from->outcipher) {
  222. logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
  223. from->hostname);
  224. return true;
  225. }
  226. if(from->outkeylength != EVP_CIPHER_key_length(from->outcipher) + EVP_CIPHER_iv_length(from->outcipher)) {
  227. logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
  228. from->hostname);
  229. return true;
  230. }
  231. } else {
  232. if(from->outkeylength != 1) {
  233. logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
  234. return true;
  235. }
  236. from->outcipher = NULL;
  237. }
  238. from->outmaclength = maclength;
  239. if(digest) {
  240. from->outdigest = EVP_get_digestbynid(digest);
  241. if(!from->outdigest) {
  242. logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
  243. from->hostname);
  244. return true;
  245. }
  246. if(from->outmaclength > EVP_MD_size(from->outdigest) || from->outmaclength < 0) {
  247. logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
  248. from->name, from->hostname);
  249. return true;
  250. }
  251. } else {
  252. from->outdigest = NULL;
  253. }
  254. if(compression < 0 || compression > 11) {
  255. logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
  256. return true;
  257. }
  258. from->outcompression = compression;
  259. if(from->outcipher)
  260. if(!EVP_EncryptInit_ex(from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + EVP_CIPHER_key_length(from->outcipher))) {
  261. logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
  262. from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
  263. return true;
  264. }
  265. from->status.validkey = true;
  266. from->sent_seqno = 0;
  267. if(*address && *port) {
  268. ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
  269. sockaddr_t sa = str2sockaddr(address, port);
  270. update_node_udp(from, &sa);
  271. }
  272. if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent) {
  273. send_mtu_probe(from);
  274. }
  275. return true;
  276. }