tls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "core/private.h"
  22. /*
  23. * fakes POLLIN on all tls guys with buffered rx
  24. *
  25. * returns nonzero if any tls guys had POLLIN faked
  26. */
  27. int
  28. lws_tls_fake_POLLIN_for_buffered(struct lws_context_per_thread *pt)
  29. {
  30. struct lws *wsi, *wsi_next;
  31. int ret = 0;
  32. wsi = pt->tls.pending_read_list;
  33. while (wsi && wsi->position_in_fds_table != LWS_NO_FDS_POS) {
  34. wsi_next = wsi->tls.pending_read_list_next;
  35. pt->fds[wsi->position_in_fds_table].revents |=
  36. pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
  37. ret |= pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN;
  38. wsi = wsi_next;
  39. }
  40. return !!ret;
  41. }
  42. void
  43. __lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
  44. {
  45. struct lws_context *context = wsi->context;
  46. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  47. if (!wsi->tls.pending_read_list_prev &&
  48. !wsi->tls.pending_read_list_next &&
  49. pt->tls.pending_read_list != wsi)
  50. /* we are not on the list */
  51. return;
  52. /* point previous guy's next to our next */
  53. if (!wsi->tls.pending_read_list_prev)
  54. pt->tls.pending_read_list = wsi->tls.pending_read_list_next;
  55. else
  56. wsi->tls.pending_read_list_prev->tls.pending_read_list_next =
  57. wsi->tls.pending_read_list_next;
  58. /* point next guy's previous to our previous */
  59. if (wsi->tls.pending_read_list_next)
  60. wsi->tls.pending_read_list_next->tls.pending_read_list_prev =
  61. wsi->tls.pending_read_list_prev;
  62. wsi->tls.pending_read_list_prev = NULL;
  63. wsi->tls.pending_read_list_next = NULL;
  64. }
  65. void
  66. lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
  67. {
  68. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  69. lws_pt_lock(pt, __func__);
  70. __lws_ssl_remove_wsi_from_buffered_list(wsi);
  71. lws_pt_unlock(pt);
  72. }
  73. #if defined(LWS_WITH_ESP32)
  74. int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
  75. lws_filepos_t *amount)
  76. {
  77. nvs_handle nvh;
  78. size_t s;
  79. int n = 0;
  80. ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
  81. if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK) {
  82. n = 1;
  83. goto bail;
  84. }
  85. *buf = lws_malloc(s + 1, "alloc_file");
  86. if (!*buf) {
  87. n = 2;
  88. goto bail;
  89. }
  90. if (nvs_get_blob(nvh, filename, (char *)*buf, &s) != ESP_OK) {
  91. lws_free(*buf);
  92. n = 1;
  93. goto bail;
  94. }
  95. *amount = s;
  96. (*buf)[s] = '\0';
  97. lwsl_notice("%s: nvs: read %s, %d bytes\n", __func__, filename, (int)s);
  98. bail:
  99. nvs_close(nvh);
  100. return n;
  101. }
  102. #else
  103. int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
  104. lws_filepos_t *amount)
  105. {
  106. FILE *f;
  107. size_t s;
  108. int n = 0;
  109. f = fopen(filename, "rb");
  110. if (f == NULL) {
  111. n = 1;
  112. goto bail;
  113. }
  114. if (fseek(f, 0, SEEK_END) != 0) {
  115. n = 1;
  116. goto bail;
  117. }
  118. s = ftell(f);
  119. if (s == (size_t)-1) {
  120. n = 1;
  121. goto bail;
  122. }
  123. if (fseek(f, 0, SEEK_SET) != 0) {
  124. n = 1;
  125. goto bail;
  126. }
  127. *buf = lws_malloc(s, "alloc_file");
  128. if (!*buf) {
  129. n = 2;
  130. goto bail;
  131. }
  132. if (fread(*buf, s, 1, f) != 1) {
  133. lws_free(*buf);
  134. n = 1;
  135. goto bail;
  136. }
  137. *amount = s;
  138. bail:
  139. if (f)
  140. fclose(f);
  141. return n;
  142. }
  143. #endif
  144. int
  145. lws_tls_alloc_pem_to_der_file(struct lws_context *context, const char *filename,
  146. const char *inbuf, lws_filepos_t inlen,
  147. uint8_t **buf, lws_filepos_t *amount)
  148. {
  149. const uint8_t *pem, *p, *end;
  150. uint8_t *q;
  151. lws_filepos_t len;
  152. int n;
  153. if (filename) {
  154. n = alloc_file(context, filename, (uint8_t **)&pem, &len);
  155. if (n)
  156. return n;
  157. } else {
  158. pem = (const uint8_t *)inbuf;
  159. len = inlen;
  160. }
  161. /* trim the first line */
  162. p = pem;
  163. end = p + len;
  164. if (strncmp((char *)p, "-----", 5))
  165. goto bail;
  166. p += 5;
  167. while (p < end && *p != '\n' && *p != '-')
  168. p++;
  169. if (*p != '-')
  170. goto bail;
  171. while (p < end && *p != '\n')
  172. p++;
  173. if (p >= end)
  174. goto bail;
  175. p++;
  176. /* trim the last line */
  177. q = (uint8_t *)end - 2;
  178. while (q > pem && *q != '\n')
  179. q--;
  180. if (*q != '\n')
  181. goto bail;
  182. *q = '\0';
  183. *amount = lws_b64_decode_string((char *)p, (char *)pem,
  184. (int)(long long)len);
  185. *buf = (uint8_t *)pem;
  186. return 0;
  187. bail:
  188. lws_free((uint8_t *)pem);
  189. return 4;
  190. }
  191. int
  192. lws_tls_check_cert_lifetime(struct lws_vhost *v)
  193. {
  194. union lws_tls_cert_info_results ir;
  195. time_t now = (time_t)lws_now_secs(), life = 0;
  196. struct lws_acme_cert_aging_args caa;
  197. int n;
  198. if (v->tls.ssl_ctx && !v->tls.skipped_certs) {
  199. if (now < 1464083026) /* May 2016 */
  200. /* our clock is wrong and we can't judge the certs */
  201. return -1;
  202. n = lws_tls_vhost_cert_info(v, LWS_TLS_CERT_INFO_VALIDITY_TO, &ir, 0);
  203. if (n)
  204. return 1;
  205. life = (ir.time - now) / (24 * 3600);
  206. lwsl_notice(" vhost %s: cert expiry: %dd\n", v->name, (int)life);
  207. } else
  208. lwsl_notice(" vhost %s: no cert\n", v->name);
  209. memset(&caa, 0, sizeof(caa));
  210. caa.vh = v;
  211. lws_broadcast(v->context, LWS_CALLBACK_VHOST_CERT_AGING, (void *)&caa,
  212. (size_t)(ssize_t)life);
  213. return 0;
  214. }
  215. int
  216. lws_tls_check_all_cert_lifetimes(struct lws_context *context)
  217. {
  218. struct lws_vhost *v = context->vhost_list;
  219. while (v) {
  220. if (lws_tls_check_cert_lifetime(v) < 0)
  221. return -1;
  222. v = v->vhost_next;
  223. }
  224. return 0;
  225. }
  226. #if !defined(LWS_WITH_ESP32) && !defined(LWS_PLAT_OPTEE)
  227. static int
  228. lws_tls_extant(const char *name)
  229. {
  230. /* it exists if we can open it... */
  231. int fd = open(name, O_RDONLY), n;
  232. char buf[1];
  233. if (fd < 0)
  234. return 1;
  235. /* and we can read at least one byte out of it */
  236. n = read(fd, buf, 1);
  237. close(fd);
  238. return n != 1;
  239. }
  240. #endif
  241. /*
  242. * Returns 0 if the filepath "name" exists and can be read from.
  243. *
  244. * In addition, if "name".upd exists, backup "name" to "name.old.1"
  245. * and rename "name".upd to "name" before reporting its existence.
  246. *
  247. * There are four situations and three results possible:
  248. *
  249. * 1) LWS_TLS_EXTANT_NO: There are no certs at all (we are waiting for them to
  250. * be provisioned). We also feel like this if we need privs we don't have
  251. * any more to look in the directory.
  252. *
  253. * 2) There are provisioned certs written (xxx.upd) and we still have root
  254. * privs... in this case we rename any existing cert to have a backup name
  255. * and move the upd cert into place with the correct name. This then becomes
  256. * situation 4 for the caller.
  257. *
  258. * 3) LWS_TLS_EXTANT_ALTERNATIVE: There are provisioned certs written (xxx.upd)
  259. * but we no longer have the privs needed to read or rename them. In this
  260. * case, indicate that the caller should use temp copies if any we do have
  261. * rights to access. This is normal after we have updated the cert.
  262. *
  263. * But if we dropped privs, we can't detect the provisioned xxx.upd cert +
  264. * key, because we can't see in the dir. So we have to upgrade NO to
  265. * ALTERNATIVE when we actually have the in-memory alternative.
  266. *
  267. * 4) LWS_TLS_EXTANT_YES: The certs are present with the correct name and we
  268. * have the rights to read them.
  269. */
  270. enum lws_tls_extant
  271. lws_tls_use_any_upgrade_check_extant(const char *name)
  272. {
  273. #if !defined(LWS_PLAT_OPTEE)
  274. int n;
  275. #if !defined(LWS_WITH_ESP32)
  276. char buf[256];
  277. lws_snprintf(buf, sizeof(buf) - 1, "%s.upd", name);
  278. if (!lws_tls_extant(buf)) {
  279. /* ah there is an updated file... how about the desired file? */
  280. if (!lws_tls_extant(name)) {
  281. /* rename the desired file */
  282. for (n = 0; n < 50; n++) {
  283. lws_snprintf(buf, sizeof(buf) - 1,
  284. "%s.old.%d", name, n);
  285. if (!rename(name, buf))
  286. break;
  287. }
  288. if (n == 50) {
  289. lwsl_notice("unable to rename %s\n", name);
  290. return LWS_TLS_EXTANT_ALTERNATIVE;
  291. }
  292. lws_snprintf(buf, sizeof(buf) - 1, "%s.upd", name);
  293. }
  294. /* desired file is out of the way, rename the updated file */
  295. if (rename(buf, name)) {
  296. lwsl_notice("unable to rename %s to %s\n", buf, name);
  297. return LWS_TLS_EXTANT_ALTERNATIVE;
  298. }
  299. }
  300. if (lws_tls_extant(name))
  301. return LWS_TLS_EXTANT_NO;
  302. #else
  303. nvs_handle nvh;
  304. size_t s = 8192;
  305. if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
  306. lwsl_notice("%s: can't open nvs\n", __func__);
  307. return LWS_TLS_EXTANT_NO;
  308. }
  309. n = nvs_get_blob(nvh, name, NULL, &s);
  310. nvs_close(nvh);
  311. if (n)
  312. return LWS_TLS_EXTANT_NO;
  313. #endif
  314. #endif
  315. return LWS_TLS_EXTANT_YES;
  316. }
  317. /*
  318. * LWS_TLS_EXTANT_NO : skip adding the cert
  319. * LWS_TLS_EXTANT_YES : use the cert and private key paths normally
  320. * LWS_TLS_EXTANT_ALTERNATIVE: normal paths not usable, try alternate if poss
  321. */
  322. enum lws_tls_extant
  323. lws_tls_generic_cert_checks(struct lws_vhost *vhost, const char *cert,
  324. const char *private_key)
  325. {
  326. int n, m;
  327. /*
  328. * The user code can choose to either pass the cert and
  329. * key filepaths using the info members like this, or it can
  330. * leave them NULL; force the vhost SSL_CTX init using the info
  331. * options flag LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX; and
  332. * set up the cert himself using the user callback
  333. * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, which
  334. * happened just above and has the vhost SSL_CTX * in the user
  335. * parameter.
  336. */
  337. if (!cert || !private_key)
  338. return LWS_TLS_EXTANT_NO;
  339. n = lws_tls_use_any_upgrade_check_extant(cert);
  340. if (n == LWS_TLS_EXTANT_ALTERNATIVE)
  341. return LWS_TLS_EXTANT_ALTERNATIVE;
  342. m = lws_tls_use_any_upgrade_check_extant(private_key);
  343. if (m == LWS_TLS_EXTANT_ALTERNATIVE)
  344. return LWS_TLS_EXTANT_ALTERNATIVE;
  345. if ((n == LWS_TLS_EXTANT_NO || m == LWS_TLS_EXTANT_NO) &&
  346. (vhost->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT)) {
  347. lwsl_notice("Ignoring missing %s or %s\n", cert, private_key);
  348. vhost->tls.skipped_certs = 1;
  349. return LWS_TLS_EXTANT_NO;
  350. }
  351. /*
  352. * the cert + key exist
  353. */
  354. return LWS_TLS_EXTANT_YES;
  355. }
  356. #if !defined(LWS_NO_SERVER)
  357. /*
  358. * update the cert for every vhost using the given path
  359. */
  360. LWS_VISIBLE int
  361. lws_tls_cert_updated(struct lws_context *context, const char *certpath,
  362. const char *keypath,
  363. const char *mem_cert, size_t len_mem_cert,
  364. const char *mem_privkey, size_t len_mem_privkey)
  365. {
  366. struct lws wsi;
  367. wsi.context = context;
  368. lws_start_foreach_ll(struct lws_vhost *, v, context->vhost_list) {
  369. wsi.vhost = v;
  370. if (v->tls.alloc_cert_path && v->tls.key_path &&
  371. !strcmp(v->tls.alloc_cert_path, certpath) &&
  372. !strcmp(v->tls.key_path, keypath)) {
  373. lws_tls_server_certs_load(v, &wsi, certpath, keypath,
  374. mem_cert, len_mem_cert,
  375. mem_privkey, len_mem_privkey);
  376. if (v->tls.skipped_certs)
  377. lwsl_notice("%s: vhost %s: cert unset\n",
  378. __func__, v->name);
  379. }
  380. } lws_end_foreach_ll(v, vhost_next);
  381. return 0;
  382. }
  383. #endif
  384. int
  385. lws_gate_accepts(struct lws_context *context, int on)
  386. {
  387. struct lws_vhost *v = context->vhost_list;
  388. lwsl_notice("%s: on = %d\n", __func__, on);
  389. #if defined(LWS_WITH_STATS)
  390. context->updated = 1;
  391. #endif
  392. while (v) {
  393. if (v->tls.use_ssl && v->lserv_wsi &&
  394. lws_change_pollfd(v->lserv_wsi, (LWS_POLLIN) * !on,
  395. (LWS_POLLIN) * on))
  396. lwsl_notice("Unable to set accept POLLIN %d\n", on);
  397. v = v->vhost_next;
  398. }
  399. return 0;
  400. }
  401. /* comma-separated alpn list, like "h2,http/1.1" to openssl alpn format */
  402. int
  403. lws_alpn_comma_to_openssl(const char *comma, uint8_t *os, int len)
  404. {
  405. uint8_t *oos = os, *plen = NULL;
  406. while (*comma && len > 1) {
  407. if (!plen && *comma == ' ') {
  408. comma++;
  409. continue;
  410. }
  411. if (!plen) {
  412. plen = os++;
  413. len--;
  414. }
  415. if (*comma == ',') {
  416. *plen = lws_ptr_diff(os, plen + 1);
  417. plen = NULL;
  418. comma++;
  419. } else {
  420. *os++ = *comma++;
  421. len--;
  422. }
  423. }
  424. if (plen)
  425. *plen = lws_ptr_diff(os, plen + 1);
  426. return lws_ptr_diff(os, oos);
  427. }