tls.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "private-lib-tls.h"
  26. #if defined(LWS_WITH_NETWORK)
  27. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  28. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  29. static int
  30. alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
  31. const unsigned char *in, unsigned int inlen, void *arg)
  32. {
  33. #if !defined(LWS_WITH_MBEDTLS)
  34. struct alpn_ctx *alpn_ctx = (struct alpn_ctx *)arg;
  35. if (SSL_select_next_proto((unsigned char **)out, outlen, alpn_ctx->data,
  36. alpn_ctx->len, in, inlen) !=
  37. OPENSSL_NPN_NEGOTIATED)
  38. return SSL_TLSEXT_ERR_NOACK;
  39. #endif
  40. return SSL_TLSEXT_ERR_OK;
  41. }
  42. #endif
  43. int
  44. lws_tls_restrict_borrow(struct lws_context *context)
  45. {
  46. if (!context->simultaneous_ssl_restriction)
  47. return 0;
  48. if (context->simultaneous_ssl >= context->simultaneous_ssl_restriction) {
  49. lwsl_notice("%s: tls connection limit %d\n", __func__,
  50. context->simultaneous_ssl);
  51. return 1;
  52. }
  53. if (++context->simultaneous_ssl == context->simultaneous_ssl_restriction)
  54. /* that was the last allowed SSL connection */
  55. lws_gate_accepts(context, 0);
  56. lwsl_info("%s: %d -> %d\n", __func__,
  57. context->simultaneous_ssl - 1,
  58. context->simultaneous_ssl);
  59. return 0;
  60. }
  61. void
  62. lws_tls_restrict_return(struct lws_context *context)
  63. {
  64. if (context->simultaneous_ssl_restriction) {
  65. if (context->simultaneous_ssl-- ==
  66. context->simultaneous_ssl_restriction)
  67. /* we made space and can do an accept */
  68. lws_gate_accepts(context, 1);
  69. lwsl_info("%s: %d -> %d\n", __func__,
  70. context->simultaneous_ssl + 1,
  71. context->simultaneous_ssl);
  72. }
  73. }
  74. void
  75. lws_context_init_alpn(struct lws_vhost *vhost)
  76. {
  77. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  78. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  79. const char *alpn_comma = vhost->context->tls.alpn_default;
  80. if (vhost->tls.alpn)
  81. alpn_comma = vhost->tls.alpn;
  82. lwsl_info(" Server '%s' advertising ALPN: %s\n",
  83. vhost->name, alpn_comma);
  84. vhost->tls.alpn_ctx.len = lws_alpn_comma_to_openssl(alpn_comma,
  85. vhost->tls.alpn_ctx.data,
  86. sizeof(vhost->tls.alpn_ctx.data) - 1);
  87. SSL_CTX_set_alpn_select_cb(vhost->tls.ssl_ctx, alpn_cb,
  88. &vhost->tls.alpn_ctx);
  89. #else
  90. lwsl_err(
  91. " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
  92. OPENSSL_VERSION_NUMBER);
  93. #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
  94. }
  95. int
  96. lws_tls_server_conn_alpn(struct lws *wsi)
  97. {
  98. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  99. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  100. const unsigned char *name = NULL;
  101. char cstr[10];
  102. unsigned len;
  103. if (!wsi->tls.ssl)
  104. return 0;
  105. SSL_get0_alpn_selected(wsi->tls.ssl, &name, &len);
  106. if (!len) {
  107. lwsl_info("no ALPN upgrade\n");
  108. return 0;
  109. }
  110. if (len > sizeof(cstr) - 1)
  111. len = sizeof(cstr) - 1;
  112. memcpy(cstr, name, len);
  113. cstr[len] = '\0';
  114. lwsl_info("negotiated '%s' using ALPN\n", cstr);
  115. wsi->tls.use_ssl |= LCCSCF_USE_SSL;
  116. return lws_role_call_alpn_negotiated(wsi, (const char *)cstr);
  117. #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
  118. return 0;
  119. }
  120. #endif
  121. #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_DEV_KIT)
  122. #if defined(LWS_PLAT_FREERTOS) && !defined(LWS_AMAZON_RTOS)
  123. int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
  124. lws_filepos_t *amount)
  125. {
  126. nvs_handle nvh;
  127. size_t s;
  128. int n = 0;
  129. ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
  130. if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK) {
  131. n = 1;
  132. goto bail;
  133. }
  134. *buf = lws_malloc(s + 1, "alloc_file");
  135. if (!*buf) {
  136. n = 2;
  137. goto bail;
  138. }
  139. if (nvs_get_blob(nvh, filename, (char *)*buf, &s) != ESP_OK) {
  140. lws_free(*buf);
  141. n = 1;
  142. goto bail;
  143. }
  144. *amount = s;
  145. (*buf)[s] = '\0';
  146. lwsl_notice("%s: nvs: read %s, %d bytes\n", __func__, filename, (int)s);
  147. bail:
  148. nvs_close(nvh);
  149. return n;
  150. }
  151. #else
  152. int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
  153. lws_filepos_t *amount)
  154. {
  155. FILE *f;
  156. size_t s;
  157. int n = 0;
  158. f = fopen(filename, "rb");
  159. if (f == NULL) {
  160. n = 1;
  161. goto bail;
  162. }
  163. if (fseek(f, 0, SEEK_END) != 0) {
  164. n = 1;
  165. goto bail;
  166. }
  167. s = ftell(f);
  168. if (s == (size_t)-1) {
  169. n = 1;
  170. goto bail;
  171. }
  172. if (fseek(f, 0, SEEK_SET) != 0) {
  173. n = 1;
  174. goto bail;
  175. }
  176. *buf = lws_malloc(s, "alloc_file");
  177. if (!*buf) {
  178. n = 2;
  179. goto bail;
  180. }
  181. if (fread(*buf, s, 1, f) != 1) {
  182. lws_free(*buf);
  183. n = 1;
  184. goto bail;
  185. }
  186. *amount = s;
  187. bail:
  188. if (f)
  189. fclose(f);
  190. return n;
  191. }
  192. #endif
  193. /*
  194. * filename: NULL means use buffer inbuf length inlen directly, otherwise
  195. * load the file "filename" into an allocated buffer.
  196. *
  197. * Allocates a separate DER output buffer if inbuf / inlen are the input,
  198. * since the
  199. *
  200. * Contents may be PEM or DER: returns with buf pointing to DER and amount
  201. * set to the DER length.
  202. */
  203. int
  204. lws_tls_alloc_pem_to_der_file(struct lws_context *context, const char *filename,
  205. const char *inbuf, lws_filepos_t inlen,
  206. uint8_t **buf, lws_filepos_t *amount)
  207. {
  208. uint8_t *pem = NULL, *p, *end, *opem;
  209. lws_filepos_t len;
  210. uint8_t *q;
  211. int n;
  212. if (filename) {
  213. n = alloc_file(context, filename, (uint8_t **)&pem, &len);
  214. if (n)
  215. return n;
  216. } else {
  217. pem = (uint8_t *)inbuf;
  218. len = inlen;
  219. }
  220. opem = p = pem;
  221. end = p + len;
  222. if (strncmp((char *)p, "-----", 5)) {
  223. /* take it as being already DER */
  224. pem = lws_malloc((size_t)inlen, "alloc_der");
  225. if (!pem)
  226. return 1;
  227. memcpy(pem, inbuf, (size_t)inlen);
  228. *buf = pem;
  229. *amount = inlen;
  230. return 0;
  231. }
  232. /* PEM -> DER */
  233. if (!filename) {
  234. /* we don't know if it's in const memory... alloc the output */
  235. pem = lws_malloc(((size_t)inlen * 3) / 4, "alloc_der");
  236. if (!pem) {
  237. lwsl_err("a\n");
  238. return 1;
  239. }
  240. } /* else overwrite the allocated, b64 input with decoded DER */
  241. /* trim the first line */
  242. p += 5;
  243. while (p < end && *p != '\n' && *p != '-')
  244. p++;
  245. if (*p != '-') {
  246. lwsl_err("b\n");
  247. goto bail;
  248. }
  249. while (p < end && *p != '\n')
  250. p++;
  251. if (p >= end) {
  252. lwsl_err("c\n");
  253. goto bail;
  254. }
  255. p++;
  256. /* trim the last line */
  257. q = (uint8_t *)end - 2;
  258. while (q > opem && *q != '\n')
  259. q--;
  260. if (*q != '\n') {
  261. lwsl_err("d\n");
  262. goto bail;
  263. }
  264. /* we can't write into the input buffer for mem, since it may be in RO
  265. * const segment
  266. */
  267. if (filename)
  268. *q = '\0';
  269. *amount = lws_b64_decode_string_len((char *)p, lws_ptr_diff(q, p),
  270. (char *)pem, (int)(long long)len);
  271. *buf = (uint8_t *)pem;
  272. return 0;
  273. bail:
  274. lws_free((uint8_t *)pem);
  275. return 4;
  276. }
  277. #endif
  278. #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_DEV_KIT)
  279. static int
  280. lws_tls_extant(const char *name)
  281. {
  282. /* it exists if we can open it... */
  283. int fd = open(name, O_RDONLY), n;
  284. char buf[1];
  285. if (fd < 0)
  286. return 1;
  287. /* and we can read at least one byte out of it */
  288. n = read(fd, buf, 1);
  289. close(fd);
  290. return n != 1;
  291. }
  292. #endif
  293. /*
  294. * Returns 0 if the filepath "name" exists and can be read from.
  295. *
  296. * In addition, if "name".upd exists, backup "name" to "name.old.1"
  297. * and rename "name".upd to "name" before reporting its existence.
  298. *
  299. * There are four situations and three results possible:
  300. *
  301. * 1) LWS_TLS_EXTANT_NO: There are no certs at all (we are waiting for them to
  302. * be provisioned). We also feel like this if we need privs we don't have
  303. * any more to look in the directory.
  304. *
  305. * 2) There are provisioned certs written (xxx.upd) and we still have root
  306. * privs... in this case we rename any existing cert to have a backup name
  307. * and move the upd cert into place with the correct name. This then becomes
  308. * situation 4 for the caller.
  309. *
  310. * 3) LWS_TLS_EXTANT_ALTERNATIVE: There are provisioned certs written (xxx.upd)
  311. * but we no longer have the privs needed to read or rename them. In this
  312. * case, indicate that the caller should use temp copies if any we do have
  313. * rights to access. This is normal after we have updated the cert.
  314. *
  315. * But if we dropped privs, we can't detect the provisioned xxx.upd cert +
  316. * key, because we can't see in the dir. So we have to upgrade NO to
  317. * ALTERNATIVE when we actually have the in-memory alternative.
  318. *
  319. * 4) LWS_TLS_EXTANT_YES: The certs are present with the correct name and we
  320. * have the rights to read them.
  321. */
  322. enum lws_tls_extant
  323. lws_tls_use_any_upgrade_check_extant(const char *name)
  324. {
  325. #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_AMAZON_RTOS)
  326. int n;
  327. #if !defined(LWS_PLAT_FREERTOS)
  328. char buf[256];
  329. lws_snprintf(buf, sizeof(buf) - 1, "%s.upd", name);
  330. if (!lws_tls_extant(buf)) {
  331. /* ah there is an updated file... how about the desired file? */
  332. if (!lws_tls_extant(name)) {
  333. /* rename the desired file */
  334. for (n = 0; n < 50; n++) {
  335. lws_snprintf(buf, sizeof(buf) - 1,
  336. "%s.old.%d", name, n);
  337. if (!rename(name, buf))
  338. break;
  339. }
  340. if (n == 50) {
  341. lwsl_notice("unable to rename %s\n", name);
  342. return LWS_TLS_EXTANT_ALTERNATIVE;
  343. }
  344. lws_snprintf(buf, sizeof(buf) - 1, "%s.upd", name);
  345. }
  346. /* desired file is out of the way, rename the updated file */
  347. if (rename(buf, name)) {
  348. lwsl_notice("unable to rename %s to %s\n", buf, name);
  349. return LWS_TLS_EXTANT_ALTERNATIVE;
  350. }
  351. }
  352. if (lws_tls_extant(name))
  353. return LWS_TLS_EXTANT_NO;
  354. #else
  355. nvs_handle nvh;
  356. size_t s = 8192;
  357. if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
  358. lwsl_notice("%s: can't open nvs\n", __func__);
  359. return LWS_TLS_EXTANT_NO;
  360. }
  361. n = nvs_get_blob(nvh, name, NULL, &s);
  362. nvs_close(nvh);
  363. if (n)
  364. return LWS_TLS_EXTANT_NO;
  365. #endif
  366. #endif
  367. return LWS_TLS_EXTANT_YES;
  368. }