openssl-dtls.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2016 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 <unistd.h>
  20. #include <sys/types.h>
  21. #include <fcntl.h>
  22. #ifndef _WIN32
  23. #include <netinet/in.h>
  24. #include <sys/socket.h>
  25. #endif
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. /* In the very early days there were cases where this wasn't found in
  31. * the header files but it did still work somehow. I forget the details
  32. * now but I was definitely avoiding using the macro. Let's just define
  33. * it for ourselves instead.*/
  34. #ifndef DTLS1_BAD_VER
  35. #define DTLS1_BAD_VER 0x100
  36. #endif
  37. #ifdef HAVE_DTLS1_STOP_TIMER
  38. /* OpenSSL doesn't deliberately export this, but we need it to
  39. workaround a DTLS bug in versions < 1.0.0e */
  40. extern void dtls1_stop_timer(SSL *);
  41. #endif
  42. #ifndef DTLS_get_data_mtu
  43. /* This equivalent functionality was submitted for OpenSSL 1.1.1+ in
  44. * https://github.com/openssl/openssl/pull/1666 */
  45. static int dtls_get_data_mtu(struct openconnect_info *vpninfo, int mtu)
  46. {
  47. int ivlen, maclen, blocksize = 0, pad = 0;
  48. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  49. const SSL_CIPHER *s_ciph = SSL_get_current_cipher(vpninfo->dtls_ssl);
  50. int cipher_nid;
  51. const EVP_CIPHER *e_ciph;
  52. const EVP_MD *e_md;
  53. char wtf[128];
  54. cipher_nid = SSL_CIPHER_get_cipher_nid(s_ciph);
  55. if (cipher_nid == NID_chacha20_poly1305) {
  56. ivlen = 0; /* Automatically derived from handshake and seqno */
  57. maclen = 16; /* Poly1305 */
  58. } else {
  59. e_ciph = EVP_get_cipherbynid(cipher_nid);
  60. switch (EVP_CIPHER_mode(e_ciph)) {
  61. case EVP_CIPH_GCM_MODE:
  62. ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  63. maclen = EVP_GCM_TLS_TAG_LEN;
  64. break;
  65. case EVP_CIPH_CCM_MODE:
  66. ivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
  67. SSL_CIPHER_description(s_ciph, wtf, sizeof(wtf));
  68. if (strstr(wtf, "CCM8"))
  69. maclen = 8;
  70. else
  71. maclen = 16;
  72. break;
  73. case EVP_CIPH_CBC_MODE:
  74. blocksize = EVP_CIPHER_block_size(e_ciph);
  75. ivlen = EVP_CIPHER_iv_length(e_ciph);
  76. pad = 1;
  77. e_md = EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(s_ciph));
  78. maclen = EVP_MD_size(e_md);
  79. break;
  80. default:
  81. vpn_progress(vpninfo, PRG_ERR,
  82. _("Unable to calculate DTLS overhead for %s\n"),
  83. SSL_CIPHER_get_name(s_ciph));
  84. ivlen = 0;
  85. maclen = DTLS_OVERHEAD;
  86. break;
  87. }
  88. }
  89. #else
  90. /* OpenSSL <= 1.0.2 only supports CBC ciphers with PSK */
  91. ivlen = EVP_CIPHER_iv_length(EVP_CIPHER_CTX_cipher(vpninfo->dtls_ssl->enc_read_ctx));
  92. maclen = EVP_MD_CTX_size(vpninfo->dtls_ssl->read_hash);
  93. blocksize = ivlen;
  94. pad = 1;
  95. #endif
  96. /* Even when it pretended to, OpenSSL never did encrypt-then-mac.
  97. * So the MAC is *inside* the encryption, unconditionally.
  98. * https://github.com/openssl/openssl/pull/1705 */
  99. if (mtu < DTLS1_RT_HEADER_LENGTH + ivlen)
  100. return 0;
  101. mtu -= DTLS1_RT_HEADER_LENGTH + ivlen;
  102. /* For CBC mode round down to blocksize */
  103. if (blocksize)
  104. mtu -= mtu % blocksize;
  105. /* Finally, CBC modes require at least one byte to indicate
  106. * padding length, as well as the MAC. */
  107. if (mtu < pad + maclen)
  108. return 0;
  109. mtu -= pad + maclen;
  110. return mtu;
  111. }
  112. #endif /* !DTLS_get_data_mtu */
  113. /* sets the DTLS MTU and returns the actual tunnel MTU */
  114. unsigned dtls_set_mtu(struct openconnect_info *vpninfo, unsigned mtu)
  115. {
  116. /* This is the record MTU (not the link MTU, which includes
  117. * IP+UDP headers, and not the payload MTU */
  118. SSL_set_mtu(vpninfo->dtls_ssl, mtu);
  119. #ifdef DTLS_get_data_mtu
  120. return DTLS_get_data_mtu(vpninfo->dtls_ssl);
  121. #else
  122. return dtls_get_data_mtu(vpninfo, mtu);
  123. #endif
  124. }
  125. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  126. /* Since OpenSSL 1.1, the SSL_SESSION structure is opaque and we can't
  127. * just fill it in directly. So we have to generate the OpenSSL ASN.1
  128. * representation of the SSL_SESSION, and use d2i_SSL_SESSION() to
  129. * create the SSL_SESSION from that. */
  130. static void buf_append_INTEGER(struct oc_text_buf *buf, uint32_t datum)
  131. {
  132. int l;
  133. /* We only handle positive integers up to INT_MAX */
  134. if (datum < 0x80)
  135. l = 1;
  136. else if (datum < 0x8000)
  137. l = 2;
  138. else if (datum < 0x800000)
  139. l = 3;
  140. else
  141. l = 4;
  142. if (buf_ensure_space(buf, 2 + l))
  143. return;
  144. buf->data[buf->pos++] = 0x02;
  145. buf->data[buf->pos++] = l;
  146. while (l--)
  147. buf->data[buf->pos++] = datum >> (l * 8);
  148. }
  149. static void buf_append_OCTET_STRING(struct oc_text_buf *buf, void *data, int len)
  150. {
  151. /* We only (need to) cope with length < 0x80 for now */
  152. if (len >= 0x80) {
  153. buf->error = -EINVAL;
  154. return;
  155. }
  156. if (buf_ensure_space(buf, 2 + len))
  157. return;
  158. buf->data[buf->pos++] = 0x04;
  159. buf->data[buf->pos++] = len;
  160. memcpy(buf->data + buf->pos, data, len);
  161. buf->pos += len;
  162. }
  163. static SSL_SESSION *generate_dtls_session(struct openconnect_info *vpninfo,
  164. int dtlsver, const SSL_CIPHER *cipher,
  165. unsigned rnd_key)
  166. {
  167. struct oc_text_buf *buf = buf_alloc();
  168. SSL_SESSION *dtls_session;
  169. const unsigned char *asn;
  170. uint16_t cid;
  171. uint8_t rnd_secret[TLS_MASTER_KEY_SIZE];
  172. buf_append_bytes(buf, "\x30\x80", 2); // SEQUENCE, indeterminate length
  173. buf_append_INTEGER(buf, 1 /* SSL_SESSION_ASN1_VERSION */);
  174. buf_append_INTEGER(buf, dtlsver);
  175. store_be16(&cid, SSL_CIPHER_get_id(cipher) & 0xffff);
  176. buf_append_OCTET_STRING(buf, &cid, 2);
  177. if (rnd_key) {
  178. buf_append_OCTET_STRING(buf, vpninfo->dtls_app_id,
  179. vpninfo->dtls_app_id_size);
  180. if (openconnect_random(rnd_secret, sizeof(rnd_secret))) {
  181. vpn_progress(vpninfo, PRG_ERR,
  182. _("Failed to generate random key\n"));
  183. buf_free(buf);
  184. return NULL;
  185. }
  186. buf_append_OCTET_STRING(buf, rnd_secret, sizeof(rnd_secret));
  187. } else {
  188. buf_append_OCTET_STRING(buf, vpninfo->dtls_session_id,
  189. sizeof(vpninfo->dtls_session_id));
  190. buf_append_OCTET_STRING(buf, vpninfo->dtls_secret,
  191. sizeof(vpninfo->dtls_secret));
  192. }
  193. /* If the length actually fits in one byte (which it should), do
  194. * it that way. Else, leave it indeterminate and add two
  195. * end-of-contents octets to mark the end of the SEQUENCE. */
  196. if (!buf_error(buf) && buf->pos <= 0x80)
  197. buf->data[1] = buf->pos - 2;
  198. else
  199. buf_append_bytes(buf, "\0\0", 2);
  200. if (buf_error(buf)) {
  201. vpn_progress(vpninfo, PRG_ERR,
  202. _("Failed to create SSL_SESSION ASN.1 for OpenSSL: %s\n"),
  203. strerror(-buf_error(buf)));
  204. buf_free(buf);
  205. return NULL;
  206. }
  207. asn = (void *)buf->data;
  208. dtls_session = d2i_SSL_SESSION(NULL, &asn, buf->pos);
  209. buf_free(buf);
  210. if (!dtls_session) {
  211. vpn_progress(vpninfo, PRG_ERR,
  212. _("OpenSSL failed to parse SSL_SESSION ASN.1\n"));
  213. openconnect_report_ssl_errors(vpninfo);
  214. return NULL;
  215. }
  216. return dtls_session;
  217. }
  218. #else /* OpenSSL before 1.1 */
  219. static SSL_SESSION *generate_dtls_session(struct openconnect_info *vpninfo,
  220. int dtlsver, const SSL_CIPHER *cipher,
  221. unsigned rnd_key)
  222. {
  223. SSL_SESSION *dtls_session = SSL_SESSION_new();
  224. if (!dtls_session) {
  225. vpn_progress(vpninfo, PRG_ERR,
  226. _("Initialise DTLSv1 session failed\n"));
  227. return NULL;
  228. }
  229. dtls_session->ssl_version = dtlsver;
  230. dtls_session->master_key_length = TLS_MASTER_KEY_SIZE;
  231. if (rnd_key) {
  232. if (openconnect_random(dtls_session->master_key, TLS_MASTER_KEY_SIZE)) {
  233. vpn_progress(vpninfo, PRG_ERR,
  234. _("Failed to generate random key\n"));
  235. return NULL;
  236. }
  237. if (vpninfo->dtls_app_id_size > sizeof(dtls_session->session_id)) {
  238. vpn_progress(vpninfo, PRG_ERR,
  239. _("Too large application ID size\n"));
  240. return NULL;
  241. }
  242. dtls_session->session_id_length = vpninfo->dtls_app_id_size;
  243. memcpy(dtls_session->session_id, vpninfo->dtls_app_id,
  244. vpninfo->dtls_app_id_size);
  245. } else {
  246. memcpy(dtls_session->master_key, vpninfo->dtls_secret,
  247. sizeof(vpninfo->dtls_secret));
  248. dtls_session->session_id_length = sizeof(vpninfo->dtls_session_id);
  249. memcpy(dtls_session->session_id, vpninfo->dtls_session_id,
  250. sizeof(vpninfo->dtls_session_id));
  251. }
  252. dtls_session->cipher = (SSL_CIPHER *)cipher;
  253. dtls_session->cipher_id = cipher->id;
  254. return dtls_session;
  255. }
  256. #endif
  257. #if defined (HAVE_DTLS12) && !defined(OPENSSL_NO_PSK)
  258. static unsigned int psk_callback(SSL *ssl, const char *hint, char *identity,
  259. unsigned int max_identity_len, unsigned char *psk,
  260. unsigned int max_psk_len)
  261. {
  262. struct openconnect_info *vpninfo = SSL_get_app_data(ssl);
  263. if (!vpninfo || max_identity_len < 4 || max_psk_len < PSK_KEY_SIZE)
  264. return 0;
  265. vpn_progress(vpninfo, PRG_TRACE, _("PSK callback\n"));
  266. snprintf(identity, max_psk_len, "psk");
  267. memcpy(psk, vpninfo->dtls_secret, PSK_KEY_SIZE);
  268. return PSK_KEY_SIZE;
  269. }
  270. #endif
  271. #ifndef HAVE_SSL_CIPHER_FIND
  272. static const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
  273. {
  274. return ssl->method->get_cipher_by_char(ptr);
  275. }
  276. #endif
  277. int start_dtls_handshake(struct openconnect_info *vpninfo, int dtls_fd)
  278. {
  279. method_const SSL_METHOD *dtls_method;
  280. SSL_SESSION *dtls_session;
  281. SSL *dtls_ssl;
  282. BIO *dtls_bio;
  283. int dtlsver = DTLS1_BAD_VER;
  284. const char *cipher = vpninfo->dtls_cipher;
  285. if (!cipher) {
  286. dtlsver = 0;
  287. #ifdef HAVE_DTLS12
  288. /* The F5 BIG-IP server before v16, will crap itself if we
  289. * even *try* to do DTLSv1.2 */
  290. if (!vpninfo->dtls12)
  291. dtlsver = DTLS1_VERSION;
  292. /* These things should never happen unless they're supported */
  293. } else if (vpninfo->dtls12) {
  294. dtlsver = DTLS1_2_VERSION;
  295. } else if (!strcmp(cipher, "OC-DTLS1_2-AES128-GCM")) {
  296. dtlsver = DTLS1_2_VERSION;
  297. cipher = "AES128-GCM-SHA256";
  298. } else if (!strcmp(cipher, "OC-DTLS1_2-AES256-GCM")) {
  299. dtlsver = DTLS1_2_VERSION;
  300. cipher = "AES256-GCM-SHA384";
  301. #ifndef OPENSSL_NO_PSK
  302. } else if (!strcmp(cipher, "PSK-NEGOTIATE")) {
  303. dtlsver = 0; /* Let it negotiate */
  304. #endif
  305. #endif
  306. }
  307. if (!vpninfo->dtls_ctx) {
  308. #ifdef HAVE_DTLS12
  309. /* If we can use SSL_CTX_set_min_proto_version, do so. */
  310. dtls_method = DTLS_client_method();
  311. #elif !defined(HAVE_SSL_CTX_PROTOVER)
  312. dtls_method = DTLSv1_client_method();
  313. #else
  314. #error "This can't happen as DTLS1.2 came before SSL_CTX_set_mXX_proto_version()"
  315. #endif
  316. vpninfo->dtls_ctx = SSL_CTX_new(dtls_method);
  317. if (!vpninfo->dtls_ctx) {
  318. vpn_progress(vpninfo, PRG_ERR,
  319. _("Initialise DTLSv1 CTX failed\n"));
  320. openconnect_report_ssl_errors(vpninfo);
  321. vpninfo->dtls_attempt_period = 0;
  322. return -EINVAL;
  323. }
  324. #ifdef HAVE_SSL_CTX_PROTOVER
  325. if (dtlsver &&
  326. (!SSL_CTX_set_min_proto_version(vpninfo->dtls_ctx, dtlsver) ||
  327. !SSL_CTX_set_max_proto_version(vpninfo->dtls_ctx, dtlsver))) {
  328. vpn_progress(vpninfo, PRG_ERR,
  329. _("Set DTLS CTX version failed\n"));
  330. openconnect_report_ssl_errors(vpninfo);
  331. SSL_CTX_free(vpninfo->dtls_ctx);
  332. vpninfo->dtls_ctx = NULL;
  333. vpninfo->dtls_attempt_period = 0;
  334. return -EINVAL;
  335. }
  336. #else /* !HAVE_SSL_CTX_PROTOVER */
  337. /* If we used the legacy version-specific methods, we need the special
  338. * way to make TLSv1_client_method() do DTLS1_BAD_VER. */
  339. if (dtlsver == DTLS1_BAD_VER)
  340. SSL_CTX_set_options(vpninfo->dtls_ctx, SSL_OP_CISCO_ANYCONNECT);
  341. #endif
  342. /* If we don't readahead, then we do short reads and throw
  343. away the tail of data packets. */
  344. SSL_CTX_set_read_ahead(vpninfo->dtls_ctx, 1);
  345. if (vpninfo->proto->proto == PROTO_ANYCONNECT) {
  346. /* All the AnyConnect hackery about saved sessions and PSK */
  347. #if defined (HAVE_DTLS12) && !defined(OPENSSL_NO_PSK)
  348. if (!dtlsver) {
  349. SSL_CTX_set_psk_client_callback(vpninfo->dtls_ctx, psk_callback);
  350. /* For PSK we override the DTLS master secret with one derived
  351. * from the HTTPS session. */
  352. if (!SSL_export_keying_material(vpninfo->https_ssl,
  353. vpninfo->dtls_secret, PSK_KEY_SIZE,
  354. PSK_LABEL, PSK_LABEL_SIZE, NULL, 0, 0)) {
  355. vpn_progress(vpninfo, PRG_ERR,
  356. _("Failed to generate DTLS key\n"));
  357. openconnect_report_ssl_errors(vpninfo);
  358. SSL_CTX_free(vpninfo->dtls_ctx);
  359. vpninfo->dtls_ctx = NULL;
  360. vpninfo->dtls_attempt_period = 0;
  361. return -EINVAL;
  362. }
  363. /* For SSL_CTX_set_cipher_list() */
  364. cipher = "PSK";
  365. }
  366. #endif /* OPENSSL_NO_PSK */
  367. #ifdef SSL_OP_NO_ENCRYPT_THEN_MAC
  368. /*
  369. * I'm fairly sure I wasn't lying when I said I had tested
  370. * https://github.com/openssl/openssl/commit/e23d5071ec4c7aa6bb2b
  371. * against GnuTLS both with and without EtM in 2016.
  372. *
  373. * Nevertheless, in 2019 it seems to be failing to negotiate
  374. * at least for DTLS1_BAD_VER against ocserv with GnuTLS 3.6.7:
  375. * https://gitlab.com/gnutls/gnutls/issues/139 — I think because
  376. * GnuTLS isn't actually doing EtM after negotiating it (like
  377. * OpenSSL 1.1.0 used to).
  378. *
  379. * Just turn it off. Real Cisco servers don't do it for
  380. * DTLS1_BAD_VER, and against ocserv (and newer Cisco) we should
  381. * be using DTLSv1.2 with AEAD ciphersuites anyway so EtM is
  382. * irrelevant.
  383. */
  384. SSL_CTX_set_options(vpninfo->dtls_ctx, SSL_OP_NO_ENCRYPT_THEN_MAC);
  385. #endif
  386. #ifdef SSL_OP_LEGACY_SERVER_CONNECT
  387. /*
  388. * Since https://github.com/openssl/openssl/pull/15127, OpenSSL
  389. * *requires* secure renegotiation support by default. For interop
  390. * with Cisco's resumed DTLS sessions, we have to turn that off.
  391. */
  392. if (dtlsver)
  393. SSL_CTX_set_options(vpninfo->dtls_ctx, SSL_OP_LEGACY_SERVER_CONNECT);
  394. #endif
  395. #ifdef SSL_OP_NO_EXTENDED_MASTER_SECRET
  396. /* RFC7627 says:
  397. *
  398. * If the original session did not use the "extended_master_secret"
  399. * extension but the new ClientHello contains the extension, then the
  400. * server MUST NOT perform the abbreviated handshake. Instead, it
  401. * SHOULD continue with a full handshake (as described in
  402. * Section 5.2) to negotiate a new session.
  403. *
  404. * Now that would be distinctly suboptimal, since we have no way to do
  405. * a full handshake (we even explicitly protect against it, in case a
  406. * MITM server attempts to hijack our deliberately-resumed session).
  407. *
  408. * So where OpenSSL provides the choice, tell it not to use extms on
  409. * resumed sessions.
  410. */
  411. if (dtlsver)
  412. SSL_CTX_set_options(vpninfo->dtls_ctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  413. #endif
  414. if (!SSL_CTX_set_cipher_list(vpninfo->dtls_ctx, cipher)) {
  415. vpn_progress(vpninfo, PRG_ERR,
  416. _("Set DTLS cipher list failed\n"));
  417. SSL_CTX_free(vpninfo->dtls_ctx);
  418. vpninfo->dtls_ctx = NULL;
  419. vpninfo->dtls_attempt_period = 0;
  420. return -EINVAL;
  421. }
  422. } else {
  423. /* Protocols other than AnyConnect are plain DTLS and do
  424. * need to check the server certificate properly (which
  425. * AnyConnect can skip because it all depends on PSK or
  426. * session resumption anyway */
  427. int ret = openconnect_install_ctx_verify(vpninfo, vpninfo->dtls_ctx);
  428. if (ret) {
  429. SSL_CTX_free(vpninfo->dtls_ctx);
  430. vpninfo->dtls_ctx = NULL;
  431. vpninfo->dtls_attempt_period = 0;
  432. return ret;
  433. }
  434. }
  435. }
  436. dtls_ssl = SSL_new(vpninfo->dtls_ctx);
  437. SSL_set_connect_state(dtls_ssl);
  438. SSL_set_app_data(dtls_ssl, vpninfo);
  439. /*
  440. * AnyConnect always sets cipher; nothing else does. Checking this
  441. * instead of vpninfo->proto->proto == PROTO_ANYCONNECT makes the
  442. * static analyser less unhappy because it doesn't know that, so it
  443. * would think we can dereference cipher when it's NULL.
  444. */
  445. if (!cipher) {
  446. /* Non-AnyConnect protocols need to verify the peer */
  447. SSL_set_verify(dtls_ssl, SSL_VERIFY_PEER, NULL);
  448. /* Where they only do DTLSv1, they also don't cope with secure renegotiation */
  449. if (dtlsver == DTLS1_VERSION)
  450. SSL_set_options(dtls_ssl, SSL_OP_LEGACY_SERVER_CONNECT);
  451. } else if (dtlsver) {
  452. /* This is the actual Cisco AnyConnect method, using session resume */
  453. STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(dtls_ssl);
  454. const SSL_CIPHER *ssl_ciph = NULL;
  455. int i;
  456. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  457. ssl_ciph = sk_SSL_CIPHER_value(ciphers, i);
  458. /* For PSK-NEGOTIATE just use the first one we find */
  459. if (!strcmp(SSL_CIPHER_get_name(ssl_ciph), cipher))
  460. break;
  461. }
  462. if (i == sk_SSL_CIPHER_num(ciphers)) {
  463. vpn_progress(vpninfo, PRG_ERR, _("DTLS cipher '%s' not found\n"),
  464. cipher);
  465. SSL_CTX_free(vpninfo->dtls_ctx);
  466. SSL_free(dtls_ssl);
  467. vpninfo->dtls_ctx = NULL;
  468. vpninfo->dtls_attempt_period = 0;
  469. return -EINVAL;
  470. }
  471. /* We're going to "resume" a session which never existed. Fake it... */
  472. dtls_session = generate_dtls_session(vpninfo, dtlsver, ssl_ciph, 0);
  473. if (!dtls_session) {
  474. SSL_CTX_free(vpninfo->dtls_ctx);
  475. SSL_free(dtls_ssl);
  476. vpninfo->dtls_ctx = NULL;
  477. vpninfo->dtls_attempt_period = 0;
  478. return -EINVAL;
  479. }
  480. if (!SSL_set_session(dtls_ssl, dtls_session)) {
  481. vpn_progress(vpninfo, PRG_ERR,
  482. _("SSL_set_session() failed with DTLS protocol version 0x%x\n"
  483. "Are you using a version of OpenSSL older than 0.9.8m?\n"
  484. "See %s\n"
  485. "Use the --no-dtls command line option to avoid this message\n"),
  486. dtlsver, "http://rt.openssl.org/Ticket/Display.html?id=1751");
  487. SSL_CTX_free(vpninfo->dtls_ctx);
  488. SSL_free(dtls_ssl);
  489. vpninfo->dtls_ctx = NULL;
  490. vpninfo->dtls_attempt_period = 0;
  491. SSL_SESSION_free(dtls_session);
  492. return -EINVAL;
  493. }
  494. /* We don't need our own refcount on it any more */
  495. SSL_SESSION_free(dtls_session);
  496. } else if (vpninfo->dtls_app_id_size > 0) {
  497. /*
  498. * For ocserv PSK-NEGOTIATE we abuse the session resume
  499. * protocol just to pass an 'App ID' in our ClientHello
  500. * in the session identifier. This session doesn't exist
  501. * and isn't actually going to be resumed at all.
  502. */
  503. const uint8_t cs[2] = {0x00, 0x2F}; /* RSA-AES-128 */
  504. dtls_session = generate_dtls_session(vpninfo, DTLS1_VERSION,
  505. SSL_CIPHER_find(dtls_ssl, cs),
  506. 1);
  507. if (!dtls_session) {
  508. SSL_CTX_free(vpninfo->dtls_ctx);
  509. SSL_free(dtls_ssl);
  510. vpninfo->dtls_ctx = NULL;
  511. vpninfo->dtls_attempt_period = 0;
  512. return -EINVAL;
  513. }
  514. if (!SSL_set_session(dtls_ssl, dtls_session)) {
  515. vpn_progress(vpninfo, PRG_ERR,
  516. _("SSL_set_session() failed\n"));
  517. SSL_CTX_free(vpninfo->dtls_ctx);
  518. SSL_free(dtls_ssl);
  519. vpninfo->dtls_ctx = NULL;
  520. vpninfo->dtls_attempt_period = 0;
  521. SSL_SESSION_free(dtls_session);
  522. return -EINVAL;
  523. }
  524. /* We don't need our own refcount on it any more */
  525. SSL_SESSION_free(dtls_session);
  526. } /* else it's ocserv PSK-NEGOTIATE without an App-ID */
  527. dtls_bio = BIO_new_dgram(dtls_fd, BIO_NOCLOSE);
  528. if (!dtls_bio || !BIO_ctrl(dtls_bio, BIO_CTRL_DGRAM_SET_CONNECTED,
  529. 0, (char *)vpninfo->dtls_addr)) {
  530. vpn_progress(vpninfo, PRG_ERR,
  531. _("Create DTLS dgram BIO failed\n"));
  532. if (dtls_bio)
  533. BIO_free(dtls_bio);
  534. SSL_CTX_free(vpninfo->dtls_ctx);
  535. SSL_free(dtls_ssl);
  536. vpninfo->dtls_ctx = NULL;
  537. vpninfo->dtls_attempt_period = 0;
  538. return -EIO;
  539. }
  540. /* Set non-blocking */
  541. BIO_set_nbio(dtls_bio, 1);
  542. SSL_set_bio(dtls_ssl, dtls_bio, dtls_bio);
  543. vpninfo->dtls_ssl = dtls_ssl;
  544. return 0;
  545. }
  546. int dtls_try_handshake(struct openconnect_info *vpninfo, int *timeout)
  547. {
  548. int ret = SSL_do_handshake(vpninfo->dtls_ssl);
  549. if (ret == 1) {
  550. const char *c;
  551. if (!vpninfo->dtls_cipher) {
  552. /* Anonymous DTLS (PPP protocols) will set vpninfo->ip_info.mtu
  553. * in PPP negotiation.
  554. *
  555. * XX: Needs forthcoming overhaul to detect MTU correctly and offer
  556. * reasonable MRU values during PPP negotiation.
  557. */
  558. int data_mtu = vpninfo->cstp_basemtu = 1500;
  559. if (vpninfo->peer_addr->sa_family == IPPROTO_IPV6)
  560. data_mtu -= 40; /* IPv6 header */
  561. else
  562. data_mtu -= 20; /* Legacy IP header */
  563. data_mtu -= 8; /* UDP header */
  564. dtls_set_mtu(vpninfo, data_mtu);
  565. } else if (!strcmp(vpninfo->dtls_cipher, "PSK-NEGOTIATE")) {
  566. /* For PSK-NEGOTIATE, we have to determine the tunnel MTU
  567. * for ourselves based on the base MTU */
  568. int data_mtu = vpninfo->cstp_basemtu;
  569. if (vpninfo->peer_addr->sa_family == AF_INET6)
  570. data_mtu -= 40; /* IPv6 header */
  571. else
  572. data_mtu -= 20; /* Legacy IP header */
  573. data_mtu -= 8; /* UDP header */
  574. if (data_mtu < 0) {
  575. vpn_progress(vpninfo, PRG_ERR,
  576. _("Peer MTU %d too small to allow DTLS\n"),
  577. vpninfo->cstp_basemtu);
  578. goto nodtls;
  579. }
  580. /* Reduce it by one because that's the payload header *inside*
  581. * the encryption */
  582. data_mtu = dtls_set_mtu(vpninfo, data_mtu) - 1;
  583. if (data_mtu < 0)
  584. goto nodtls;
  585. if (data_mtu < vpninfo->ip_info.mtu) {
  586. vpn_progress(vpninfo, PRG_INFO,
  587. _("DTLS MTU reduced to %d\n"),
  588. data_mtu);
  589. vpninfo->ip_info.mtu = data_mtu;
  590. }
  591. } else if (!SSL_session_reused(vpninfo->dtls_ssl)) {
  592. /* Someone attempting to hijack the DTLS session?
  593. * A real server would never allow a full session
  594. * establishment instead of the agreed resume. */
  595. vpn_progress(vpninfo, PRG_ERR,
  596. _("DTLS session resume failed; possible MITM attack. Disabling DTLS.\n"));
  597. nodtls:
  598. dtls_close(vpninfo);
  599. SSL_CTX_free(vpninfo->dtls_ctx);
  600. vpninfo->dtls_ctx = NULL;
  601. vpninfo->dtls_attempt_period = 0;
  602. vpninfo->dtls_state = DTLS_DISABLED;
  603. return -EIO;
  604. }
  605. vpninfo->dtls_state = DTLS_CONNECTED;
  606. vpn_progress(vpninfo, PRG_INFO,
  607. _("Established DTLS connection (using OpenSSL). Ciphersuite %s-%s.\n"),
  608. SSL_get_version(vpninfo->dtls_ssl), SSL_get_cipher(vpninfo->dtls_ssl));
  609. c = openconnect_get_dtls_compression(vpninfo);
  610. if (c) {
  611. vpn_progress(vpninfo, PRG_INFO,
  612. _("DTLS connection compression using %s.\n"), c);
  613. }
  614. vpninfo->dtls_times.last_rekey = vpninfo->dtls_times.last_rx =
  615. vpninfo->dtls_times.last_tx = time(NULL);
  616. /* From about 8.4.1(11) onwards, the ASA seems to get
  617. very unhappy if we resend ChangeCipherSpec messages
  618. after the initial setup. This was "fixed" in OpenSSL
  619. 1.0.0e for RT#2505, but it's not clear if that was
  620. the right fix. What happens if the original packet
  621. *does* get lost? Surely we *wanted* the retransmits,
  622. because without them the server will never be able
  623. to decrypt anything we send?
  624. Oh well, our retransmitted packets upset the server
  625. because we don't get the Cisco-compatibility right
  626. (this is one of the areas in which Cisco's DTLS differs
  627. from the RFC4347 spec), and DPD should help us notice
  628. if *nothing* is getting through. */
  629. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  630. /* OpenSSL 1.1.0 or above. Do nothing. The SSLeay() function
  631. got renamed, and it's a pointless check in this case
  632. anyway because there's *no* chance that we linked against
  633. 1.1.0 and are running against something older than 1.0.0e. */
  634. #elif OPENSSL_VERSION_NUMBER >= 0x1000005fL
  635. /* OpenSSL 1.0.0e or above doesn't resend anyway; do nothing.
  636. However, if we were *built* against 1.0.0e or newer, but at
  637. runtime we find that we are being run against an older
  638. version, warn about it. */
  639. if (SSLeay() < 0x1000005fL) {
  640. vpn_progress(vpninfo, PRG_ERR,
  641. _("Your OpenSSL is older than the one you built against, so DTLS may fail!\n"));
  642. }
  643. #elif defined(HAVE_DTLS1_STOP_TIMER)
  644. /*
  645. * This works for any normal OpenSSL that supports
  646. * Cisco DTLS compatibility (0.9.8m to 1.0.0d inclusive,
  647. * and even later versions although it isn't needed there.
  648. */
  649. dtls1_stop_timer(vpninfo->dtls_ssl);
  650. #elif defined(BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT)
  651. /*
  652. * Debian restricts visibility of dtls1_stop_timer()
  653. * so do it manually. This version also works on all
  654. * sane versions of OpenSSL:
  655. */
  656. memset(&(vpninfo->dtls_ssl->d1->next_timeout), 0,
  657. sizeof((vpninfo->dtls_ssl->d1->next_timeout)));
  658. vpninfo->dtls_ssl->d1->timeout_duration = 1;
  659. BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl),
  660. BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
  661. &(vpninfo->dtls_ssl->d1->next_timeout));
  662. #elif defined(BIO_CTRL_DGRAM_SET_TIMEOUT)
  663. /*
  664. * OK, here it gets more fun... this should handle the case
  665. * of older OpenSSL which has the Cisco DTLS compatibility
  666. * backported, but *not* the fix for RT#1922.
  667. */
  668. BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl),
  669. BIO_CTRL_DGRAM_SET_TIMEOUT, 0, NULL);
  670. #else
  671. /*
  672. * And if they don't have any of the above, they probably
  673. * don't have RT#1829 fixed either, but that's OK because
  674. * that's the "fix" that *introduces* the timeout we're
  675. * trying to disable. So do nothing...
  676. */
  677. #endif
  678. dtls_detect_mtu(vpninfo);
  679. return 0;
  680. }
  681. ret = SSL_get_error(vpninfo->dtls_ssl, ret);
  682. if (ret == SSL_ERROR_WANT_WRITE || ret == SSL_ERROR_WANT_READ) {
  683. int quit_time = vpninfo->new_dtls_started + 12 - time(NULL);
  684. if (quit_time > 0) {
  685. if (timeout) {
  686. struct timeval tv;
  687. if (SSL_ctrl(vpninfo->dtls_ssl, DTLS_CTRL_GET_TIMEOUT, 0, &tv)) {
  688. unsigned timeout_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  689. if (*timeout > timeout_ms)
  690. *timeout = timeout_ms;
  691. }
  692. if (*timeout > quit_time * 1000)
  693. *timeout = quit_time * 1000;
  694. }
  695. return 0;
  696. }
  697. static int badossl_bitched; /* static variable initialised to 0 */
  698. if (((OPENSSL_VERSION_NUMBER >= 0x100000b0L && OPENSSL_VERSION_NUMBER <= 0x100000c0L) ||
  699. (OPENSSL_VERSION_NUMBER >= 0x10001040L && OPENSSL_VERSION_NUMBER <= 0x10001060L) ||
  700. OPENSSL_VERSION_NUMBER == 0x10002000L) && !badossl_bitched) {
  701. badossl_bitched = 1;
  702. vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake timed out\n"));
  703. vpn_progress(vpninfo, PRG_ERR, _("This is probably because your OpenSSL is broken\n"
  704. "See http://rt.openssl.org/Ticket/Display.html?id=2984\n"));
  705. } else {
  706. vpn_progress(vpninfo, PRG_DEBUG, _("DTLS handshake timed out\n"));
  707. }
  708. }
  709. vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake failed: %d\n"), ret);
  710. openconnect_report_ssl_errors(vpninfo);
  711. dtls_close(vpninfo);
  712. vpninfo->dtls_state = DTLS_SLEEPING;
  713. time(&vpninfo->new_dtls_started);
  714. if (timeout && *timeout > vpninfo->dtls_attempt_period * 1000)
  715. *timeout = vpninfo->dtls_attempt_period * 1000;
  716. return -EINVAL;
  717. }
  718. void dtls_shutdown(struct openconnect_info *vpninfo)
  719. {
  720. dtls_close(vpninfo);
  721. SSL_CTX_free(vpninfo->dtls_ctx);
  722. }
  723. void dtls_ssl_free(struct openconnect_info *vpninfo)
  724. {
  725. /* We are only ever called when this is non-NULL */
  726. SSL_free(vpninfo->dtls_ssl);
  727. }
  728. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  729. void gather_dtls_ciphers(struct openconnect_info *vpninfo, struct oc_text_buf *buf,
  730. struct oc_text_buf *buf12)
  731. {
  732. #ifdef HAVE_DTLS12
  733. #ifndef OPENSSL_NO_PSK
  734. buf_append(buf, "PSK-NEGOTIATE:");
  735. #endif
  736. buf_append(buf, "OC-DTLS1_2-AES256-GCM:OC-DTLS1_2-AES128-GCM:");
  737. buf_append(buf12, "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384\r\n");
  738. #endif
  739. buf_append(buf, "DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:");
  740. buf_append(buf, "AES256-SHA:AES128-SHA:DES-CBC3-SHA:DES-CBC-SHA");
  741. }
  742. #else
  743. void gather_dtls_ciphers(struct openconnect_info *vpninfo, struct oc_text_buf *buf,
  744. struct oc_text_buf *buf12)
  745. {
  746. method_const SSL_METHOD *dtls_method;
  747. SSL_CTX *ctx;
  748. SSL *ssl;
  749. STACK_OF(SSL_CIPHER) *ciphers;
  750. int i;
  751. dtls_method = DTLS_client_method();
  752. ctx = SSL_CTX_new(dtls_method);
  753. if (!ctx)
  754. return;
  755. ssl = SSL_new(ctx);
  756. if (!ssl) {
  757. SSL_CTX_free(ctx);
  758. return;
  759. }
  760. int aes128_gcm = 0, aes256_gcm = 0;
  761. ciphers = SSL_get1_supported_ciphers(ssl);
  762. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  763. const SSL_CIPHER *ciph = sk_SSL_CIPHER_value(ciphers, i);
  764. const char *name = SSL_CIPHER_get_name(ciph);
  765. const char *vers = SSL_CIPHER_get_version(ciph);
  766. if (!strcmp(vers, "SSLv3") || !strcmp(vers, "TLSv1.0") ||
  767. !strcmp(vers, "TLSv1/SSLv3")) {
  768. buf_append(buf, "%s%s",
  769. (buf_error(buf) || !buf->pos) ? "" : ":",
  770. name);
  771. } else if (!strcmp(vers, "TLSv1.2")) {
  772. buf_append(buf12, "%s%s",
  773. (buf_error(buf12) || !buf12->pos) ? "" : ":",
  774. name);
  775. /* The OC-specific names for the DTLSv1.2 AES-GCM ciphersuites
  776. * need to be added to the X-DTLS-CipherSuite: header too. */
  777. if (!strcmp(name, "AES128-GCM-SHA256")) {
  778. aes128_gcm = 1;
  779. } else if (!strcmp(name, "AES256-GCM-SHA384")) {
  780. aes256_gcm = 1;
  781. }
  782. }
  783. }
  784. sk_SSL_CIPHER_free(ciphers);
  785. SSL_free(ssl);
  786. SSL_CTX_free(ctx);
  787. /* All DTLSv1 suites are also supported in DTLSv1.2 */
  788. if (!buf_error(buf))
  789. buf_append(buf12, ":%s", buf->data);
  790. if (aes128_gcm)
  791. buf_append(buf, ":OC-DTLS1_2-AES128-GCM");
  792. if (aes256_gcm)
  793. buf_append(buf, ":OC-DTLS1_2-AES256-GCM");
  794. #ifndef OPENSSL_NO_PSK
  795. buf_append(buf, ":PSK-NEGOTIATE");
  796. #endif
  797. }
  798. #endif