esp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include "lzo.h"
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. int print_esp_keys(struct openconnect_info *vpninfo, const char *name, struct esp *esp)
  27. {
  28. int i;
  29. const char *enctype, *mactype;
  30. char enckey[256], mackey[256];
  31. switch(vpninfo->esp_enc) {
  32. case ENC_AES_128_CBC:
  33. enctype = "AES-128-CBC (RFC3602)";
  34. break;
  35. case ENC_AES_256_CBC:
  36. enctype = "AES-256-CBC (RFC3602)";
  37. break;
  38. default:
  39. return -EINVAL;
  40. }
  41. switch(vpninfo->esp_hmac) {
  42. case HMAC_MD5:
  43. mactype = "HMAC-MD5-96 (RFC2403)";
  44. break;
  45. case HMAC_SHA1:
  46. mactype = "HMAC-SHA-1-96 (RFC2404)";
  47. break;
  48. case HMAC_SHA256:
  49. mactype = "HMAC-SHA-256-128 (RFC4868)";
  50. break;
  51. default:
  52. return -EINVAL;
  53. }
  54. for (i = 0; i < vpninfo->enc_key_len; i++)
  55. sprintf(enckey + (2 * i), "%02x", esp->enc_key[i]);
  56. for (i = 0; i < vpninfo->hmac_key_len; i++)
  57. sprintf(mackey + (2 * i), "%02x", esp->hmac_key[i]);
  58. vpn_progress(vpninfo, PRG_TRACE,
  59. _("Parameters for %s ESP: SPI 0x%08x\n"),
  60. name, (unsigned)ntohl(esp->spi));
  61. vpn_progress(vpninfo, PRG_TRACE,
  62. _("ESP encryption type %s key 0x%s\n"),
  63. enctype, enckey);
  64. vpn_progress(vpninfo, PRG_TRACE,
  65. _("ESP authentication type %s key 0x%s\n"),
  66. mactype, mackey);
  67. return 0;
  68. }
  69. int esp_setup(struct openconnect_info *vpninfo)
  70. {
  71. if (vpninfo->dtls_state == DTLS_DISABLED ||
  72. vpninfo->dtls_state == DTLS_NOSECRET)
  73. return -EINVAL;
  74. /* XX: set ESP DPD interval if not already set */
  75. if (!vpninfo->dtls_times.dpd) {
  76. if (vpninfo->esp_ssl_fallback)
  77. vpninfo->dtls_times.dpd = vpninfo->esp_ssl_fallback;
  78. else
  79. vpninfo->dtls_times.dpd = vpninfo->dtls_attempt_period;
  80. }
  81. print_esp_keys(vpninfo, _("incoming"), &vpninfo->esp_in[vpninfo->current_esp_in]);
  82. print_esp_keys(vpninfo, _("outgoing"), &vpninfo->esp_out);
  83. vpn_progress(vpninfo, PRG_DEBUG, _("Send ESP probes\n"));
  84. if (vpninfo->proto->udp_send_probes)
  85. vpninfo->proto->udp_send_probes(vpninfo);
  86. return 0;
  87. }
  88. int construct_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt, uint8_t next_hdr)
  89. {
  90. const int blksize = 16;
  91. int i, padlen, ret;
  92. if (!next_hdr) {
  93. if ((pkt->data[0] & 0xf0) == 0x60) /* iph->ip_v */
  94. next_hdr = IPPROTO_IPV6;
  95. else
  96. next_hdr = IPPROTO_IPIP;
  97. }
  98. /* This gets much more fun if the IV is variable-length */
  99. pkt->esp.spi = vpninfo->esp_out.spi;
  100. pkt->esp.seq = htonl(vpninfo->esp_out.seq++);
  101. padlen = blksize - 1 - ((pkt->len + 1) % blksize);
  102. for (i=0; i<padlen; i++)
  103. pkt->data[pkt->len + i] = i + 1;
  104. pkt->data[pkt->len + padlen] = padlen;
  105. pkt->data[pkt->len + padlen + 1] = next_hdr;
  106. memcpy(pkt->esp.iv, vpninfo->esp_out.iv, sizeof(pkt->esp.iv));
  107. ret = encrypt_esp_packet(vpninfo, pkt, pkt->len + padlen + 2);
  108. if (ret)
  109. return ret;
  110. return sizeof(pkt->esp) + pkt->len + padlen + 2 + vpninfo->hmac_out_len;
  111. }
  112. int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
  113. {
  114. struct esp *esp = &vpninfo->esp_in[vpninfo->current_esp_in];
  115. struct esp *old_esp = &vpninfo->esp_in[vpninfo->current_esp_in ^ 1];
  116. struct pkt *this;
  117. int work_done = 0;
  118. int ret;
  119. /* Some servers send us packets that are larger than negotiated
  120. MTU, or lack the ability to negotiate MTU (see gpst.c). We
  121. reserve some extra space to handle that */
  122. int receive_mtu = MAX(2048, vpninfo->ip_info.mtu + 256);
  123. if (vpninfo->dtls_state == DTLS_SLEEPING) {
  124. if (ka_check_deadline(timeout, time(NULL), vpninfo->new_dtls_started + vpninfo->dtls_attempt_period)
  125. || vpninfo->dtls_need_reconnect) {
  126. vpn_progress(vpninfo, PRG_DEBUG, _("Send ESP probes\n"));
  127. if (vpninfo->proto->udp_send_probes)
  128. vpninfo->proto->udp_send_probes(vpninfo);
  129. }
  130. }
  131. if (vpninfo->dtls_fd == -1)
  132. return 0;
  133. while (readable) {
  134. int len = receive_mtu + vpninfo->pkt_trailer;
  135. int i;
  136. struct pkt *pkt;
  137. if (!vpninfo->dtls_pkt) {
  138. vpninfo->dtls_pkt = alloc_pkt(vpninfo, len);
  139. if (!vpninfo->dtls_pkt) {
  140. vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
  141. break;
  142. }
  143. }
  144. pkt = vpninfo->dtls_pkt;
  145. len = recv(vpninfo->dtls_fd, (void *)&pkt->esp, len + sizeof(pkt->esp), 0);
  146. if (!len)
  147. break;
  148. if (len < 0) {
  149. #ifdef _WIN32
  150. int err = WSAGetLastError();
  151. if (err == WSAEWOULDBLOCK)
  152. break;
  153. char *errstr = openconnect__win32_strerror(err);
  154. vpn_progress(vpninfo, PRG_ERR,
  155. _("ESP receive error: %s\n"),
  156. errstr);
  157. free(errstr);
  158. #else
  159. if (errno == EAGAIN || errno == EWOULDBLOCK)
  160. break;
  161. vpn_progress(vpninfo, PRG_ERR,
  162. _("ESP receive error: %s\n"),
  163. strerror(errno));
  164. #endif
  165. /* On *real* errors, close the UDP socket and try again later. */
  166. vpninfo->proto->udp_close(vpninfo);
  167. return 0;
  168. }
  169. work_done = 1;
  170. /* both supported algos (SHA1 and MD5) have 12-byte MAC lengths (RFC2403 and RFC2404) */
  171. if (len <= sizeof(pkt->esp) + vpninfo->hmac_out_len)
  172. continue;
  173. len -= sizeof(pkt->esp) + vpninfo->hmac_out_len;
  174. pkt->len = len;
  175. if (pkt->esp.spi == esp->spi) {
  176. if (decrypt_esp_packet(vpninfo, esp, pkt))
  177. continue;
  178. } else if (pkt->esp.spi == old_esp->spi &&
  179. ntohl(pkt->esp.seq) + esp->seq < vpninfo->old_esp_maxseq) {
  180. vpn_progress(vpninfo, PRG_TRACE,
  181. _("Received ESP packet from old SPI 0x%x, seq %u\n"),
  182. (unsigned)ntohl(old_esp->spi), (unsigned)ntohl(pkt->esp.seq));
  183. if (decrypt_esp_packet(vpninfo, old_esp, pkt))
  184. continue;
  185. } else {
  186. vpn_progress(vpninfo, PRG_DEBUG,
  187. _("Received ESP packet with invalid SPI 0x%08x\n"),
  188. (unsigned)ntohl(pkt->esp.spi));
  189. continue;
  190. }
  191. /* Possible values of the Next Header field are:
  192. 0x04: IP[v4]-in-IP
  193. 0x05: supposed to mean Internet Stream Protocol
  194. (XXX: but used for LZO compressed IPv4 packets by Juniper)
  195. 0x29: IPv6 encapsulation */
  196. if (pkt->data[len - 1] == 0x04)
  197. vpn_progress(vpninfo, PRG_TRACE, _("Received ESP Legacy IP packet of %d bytes\n"),
  198. len);
  199. else if (pkt->data[len - 1] == 0x05)
  200. vpn_progress(vpninfo, PRG_TRACE, _("Received ESP Legacy IP packet of %d bytes (LZO-compressed)\n"),
  201. len);
  202. else if (pkt->data[len - 1] == 0x29)
  203. vpn_progress(vpninfo, PRG_TRACE, _("Received ESP IPv6 packet of %d bytes\n"),
  204. len);
  205. else {
  206. vpn_progress(vpninfo, PRG_ERR,
  207. _("Received ESP packet of %d bytes with unrecognised payload type %02x\n"),
  208. len, pkt->data[len-1]);
  209. continue;
  210. }
  211. if (len <= 2 + pkt->data[len - 2]) {
  212. vpn_progress(vpninfo, PRG_ERR,
  213. _("Invalid padding length %02x in ESP\n"),
  214. pkt->data[len - 2]);
  215. continue;
  216. }
  217. pkt->len = len - 2 - pkt->data[len - 2];
  218. for (i = 0 ; i < pkt->data[len - 2]; i++) {
  219. if (pkt->data[pkt->len + i] != i + 1)
  220. break; /* We can't just 'continue' here because it
  221. * would only break out of this 'for' loop */
  222. }
  223. if (i != pkt->data[len - 2]) {
  224. vpn_progress(vpninfo, PRG_ERR,
  225. _("Invalid padding bytes in ESP\n"));
  226. continue; /* We can here, though */
  227. }
  228. vpninfo->dtls_times.last_rx = time(NULL);
  229. if (vpninfo->proto->udp_catch_probe) {
  230. if (vpninfo->proto->udp_catch_probe(vpninfo, pkt)) {
  231. if (vpninfo->dtls_state == DTLS_SLEEPING) {
  232. vpn_progress(vpninfo, PRG_INFO,
  233. _("ESP session established with server\n"));
  234. vpninfo->dtls_state = DTLS_CONNECTED;
  235. }
  236. continue;
  237. }
  238. }
  239. if (pkt->data[len - 1] == 0x05) {
  240. struct pkt *newpkt = alloc_pkt(vpninfo, receive_mtu + vpninfo->pkt_trailer);
  241. int newlen = receive_mtu;
  242. if (!newpkt) {
  243. vpn_progress(vpninfo, PRG_ERR,
  244. _("Failed to allocate memory to decrypt ESP packet\n"));
  245. continue;
  246. }
  247. if (av_lzo1x_decode(newpkt->data, &newlen,
  248. pkt->data, &pkt->len) || pkt->len) {
  249. vpn_progress(vpninfo, PRG_ERR,
  250. _("LZO decompression of ESP packet failed\n"));
  251. free_pkt(vpninfo, newpkt);
  252. continue;
  253. }
  254. newpkt->len = receive_mtu - newlen;
  255. vpn_progress(vpninfo, PRG_TRACE,
  256. _("LZO decompressed %d bytes into %d\n"),
  257. len - 2 - pkt->data[len-2], newpkt->len);
  258. queue_packet(&vpninfo->incoming_queue, newpkt);
  259. } else {
  260. queue_packet(&vpninfo->incoming_queue, pkt);
  261. vpninfo->dtls_pkt = NULL;
  262. }
  263. }
  264. if (vpninfo->dtls_state != DTLS_ESTABLISHED)
  265. return 0;
  266. switch (keepalive_action(&vpninfo->dtls_times, timeout)) {
  267. case KA_REKEY:
  268. vpn_progress(vpninfo, PRG_ERR, _("Rekey not implemented for ESP\n"));
  269. break;
  270. case KA_DPD_DEAD:
  271. vpn_progress(vpninfo, PRG_ERR, _("ESP detected dead peer\n"));
  272. if (vpninfo->proto->udp_close)
  273. vpninfo->proto->udp_close(vpninfo);
  274. if (vpninfo->proto->udp_send_probes)
  275. vpninfo->proto->udp_send_probes(vpninfo);
  276. return 1;
  277. case KA_DPD:
  278. vpn_progress(vpninfo, PRG_DEBUG, _("Send ESP probes for DPD\n"));
  279. if (vpninfo->proto->udp_send_probes)
  280. vpninfo->proto->udp_send_probes(vpninfo);
  281. work_done = 1;
  282. break;
  283. case KA_KEEPALIVE:
  284. vpn_progress(vpninfo, PRG_ERR, _("Keepalive not implemented for ESP\n"));
  285. break;
  286. case KA_NONE:
  287. break;
  288. }
  289. while (1) {
  290. int len;
  291. int ip_version;
  292. if (vpninfo->deflate_pkt) {
  293. this = vpninfo->deflate_pkt;
  294. len = this->len;
  295. ip_version = this->data[0] >> 4;
  296. } else {
  297. this = dequeue_packet(&vpninfo->outgoing_queue);
  298. if (!this)
  299. break;
  300. ip_version = this->data[0] >> 4;
  301. if (vpninfo->proto->proto == PROTO_PULSE) {
  302. uint8_t dontsend;
  303. /* Pulse can only accept ESP of the same protocol as the one
  304. * you connected to it with. The other has to go over IF-T/TLS. */
  305. if (vpninfo->dtls_addr->sa_family == AF_INET6)
  306. dontsend = 4;
  307. else
  308. dontsend = 6;
  309. if (ip_version == dontsend) {
  310. store_be32(&this->pulse.vendor, 0xa4c);
  311. store_be32(&this->pulse.type, 4);
  312. store_be32(&this->pulse.len, this->len + 16);
  313. queue_packet(&vpninfo->tcp_control_queue, this);
  314. work_done = 1;
  315. continue;
  316. }
  317. } else if (vpninfo->proto->proto == PROTO_NC &&
  318. vpninfo->dtls_addr->sa_family == AF_INET6) {
  319. /* Juniper/NC cannot do ESP-over-IPv6, and it cannot send
  320. * tunneled IPv6 packets at all; they just get dropped.
  321. * It shouldn't even send any ESP options/keys when connecting
  322. * to the server via IPv6. This should never happen.
  323. */
  324. vpninfo->quit_reason = "Juniper/NC ESP tunnel over IPv6 should never happen.";
  325. return 1;
  326. }
  327. /* XX: Must precede in-place encryption of the packet, because
  328. IP header fields (version and TOS) are garbled afterward.
  329. If TOS optname is set, we want to copy the TOS/TCLASS header
  330. to the outer UDP packet */
  331. if (vpninfo->dtls_tos_optname)
  332. udp_tos_update(vpninfo, this);
  333. len = construct_esp_packet(vpninfo, this, 0);
  334. if (len < 0) {
  335. /* Should we disable ESP? */
  336. free_pkt(vpninfo, this);
  337. work_done = 1;
  338. continue;
  339. }
  340. }
  341. ret = send(vpninfo->dtls_fd, (void *)&this->esp, len, 0);
  342. if (ret < 0) {
  343. /* Not that this is likely to happen with UDP, but... */
  344. if (errno == ENOBUFS || errno == EAGAIN || errno == EWOULDBLOCK) {
  345. vpninfo->deflate_pkt = this;
  346. this->len = len;
  347. vpn_progress(vpninfo, PRG_DEBUG,
  348. _("Requeueing failed ESP send: %s\n"),
  349. strerror(errno));
  350. monitor_write_fd(vpninfo, dtls);
  351. return work_done;
  352. } else {
  353. /* A real error in sending. Fall back to TCP? */
  354. vpn_progress(vpninfo, PRG_ERR,
  355. _("Failed to send ESP packet: %s\n"),
  356. strerror(errno));
  357. }
  358. } else {
  359. vpninfo->dtls_times.last_tx = time(NULL);
  360. vpn_progress(vpninfo, PRG_TRACE, _("Sent ESP IPv%d packet of %d bytes\n"),
  361. ip_version, len);
  362. }
  363. if (this == vpninfo->deflate_pkt) {
  364. unmonitor_write_fd(vpninfo, dtls);
  365. vpninfo->deflate_pkt = NULL;
  366. }
  367. free_pkt(vpninfo, this);
  368. work_done = 1;
  369. }
  370. return work_done;
  371. }
  372. void esp_close(struct openconnect_info *vpninfo)
  373. {
  374. /* We close and reopen the socket in case we roamed and our
  375. local IP address has changed. */
  376. if (vpninfo->dtls_fd != -1) {
  377. unmonitor_fd(vpninfo, dtls);
  378. closesocket(vpninfo->dtls_fd);
  379. vpninfo->dtls_fd = -1;
  380. }
  381. if (vpninfo->dtls_state > DTLS_DISABLED)
  382. vpninfo->dtls_state = DTLS_SLEEPING;
  383. if (vpninfo->deflate_pkt) {
  384. free_pkt(vpninfo, vpninfo->deflate_pkt);
  385. vpninfo->deflate_pkt = NULL;
  386. }
  387. }
  388. void esp_shutdown(struct openconnect_info *vpninfo)
  389. {
  390. destroy_esp_ciphers(&vpninfo->esp_in[0]);
  391. destroy_esp_ciphers(&vpninfo->esp_in[1]);
  392. destroy_esp_ciphers(&vpninfo->esp_out);
  393. if (vpninfo->proto->udp_close)
  394. vpninfo->proto->udp_close(vpninfo);
  395. if (vpninfo->dtls_state != DTLS_DISABLED)
  396. vpninfo->dtls_state = DTLS_NOSECRET;
  397. }
  398. int openconnect_setup_esp_keys(struct openconnect_info *vpninfo, int new_keys)
  399. {
  400. struct esp *esp_in;
  401. int ret;
  402. if (vpninfo->dtls_state == DTLS_DISABLED)
  403. return -EOPNOTSUPP;
  404. if (!vpninfo->dtls_addr)
  405. return -EINVAL;
  406. if (vpninfo->esp_hmac == HMAC_SHA256)
  407. vpninfo->hmac_out_len = 16;
  408. else /* MD5 and SHA1 */
  409. vpninfo->hmac_out_len = 12;
  410. if (new_keys) {
  411. vpninfo->old_esp_maxseq = vpninfo->esp_in[vpninfo->current_esp_in].seq + 32;
  412. vpninfo->current_esp_in ^= 1;
  413. }
  414. esp_in = &vpninfo->esp_in[vpninfo->current_esp_in];
  415. if (new_keys) {
  416. if (openconnect_random(&esp_in->spi, sizeof(esp_in->spi)) ||
  417. openconnect_random((void *)&esp_in->enc_key, vpninfo->enc_key_len) ||
  418. openconnect_random((void *)&esp_in->hmac_key, vpninfo->hmac_key_len)) {
  419. vpn_progress(vpninfo, PRG_ERR,
  420. _("Failed to generate random keys for ESP\n"));
  421. return -EIO;
  422. }
  423. }
  424. if (openconnect_random(vpninfo->esp_out.iv, sizeof(vpninfo->esp_out.iv))) {
  425. vpn_progress(vpninfo, PRG_ERR,
  426. _("Failed to generate initial IV for ESP\n"));
  427. return -EIO;
  428. }
  429. /* This is the minimum; some implementations may increase it */
  430. vpninfo->pkt_trailer = MAX_ESP_PAD + MAX_IV_SIZE + MAX_HMAC_SIZE;
  431. vpninfo->esp_out.seq = vpninfo->esp_out.seq_backlog = 0;
  432. esp_in->seq = esp_in->seq_backlog = 0;
  433. ret = init_esp_ciphers(vpninfo, &vpninfo->esp_out, esp_in);
  434. if (ret)
  435. return ret;
  436. if (vpninfo->dtls_state == DTLS_NOSECRET)
  437. vpninfo->dtls_state = DTLS_SECRET;
  438. return 0;
  439. }