ssl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2016 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 "private-libwebsockets.h"
  22. /* workaround for mingw */
  23. #if !defined(ECONNABORTED)
  24. #define ECONNABORTED 103
  25. #endif
  26. int openssl_websocket_private_data_index,
  27. openssl_SSL_CTX_private_data_index;
  28. int lws_ssl_get_error(struct lws *wsi, int n)
  29. {
  30. return SSL_get_error(wsi->ssl, n);
  31. }
  32. /* Copies a string describing the code returned by lws_ssl_get_error(),
  33. * which may also contain system error information in the case of SSL_ERROR_SYSCALL,
  34. * into buf up to len.
  35. * Returns a pointer to buf.
  36. *
  37. * Note: the lws_ssl_get_error() code is *not* an error code that can be passed
  38. * to ERR_error_string(),
  39. *
  40. * ret is the return value originally passed to lws_ssl_get_error(), needed to disambiguate
  41. * SYS_ERROR_SYSCALL.
  42. *
  43. * See man page for SSL_get_error().
  44. *
  45. * Not thread safe, uses strerror()
  46. */
  47. char* lws_ssl_get_error_string(int status, int ret, char *buf, size_t len) {
  48. switch (status) {
  49. case SSL_ERROR_NONE: return strncpy(buf, "SSL_ERROR_NONE", len);
  50. case SSL_ERROR_ZERO_RETURN: return strncpy(buf, "SSL_ERROR_ZERO_RETURN", len);
  51. case SSL_ERROR_WANT_READ: return strncpy(buf, "SSL_ERROR_WANT_READ", len);
  52. case SSL_ERROR_WANT_WRITE: return strncpy(buf, "SSL_ERROR_WANT_WRITE", len);
  53. case SSL_ERROR_WANT_CONNECT: return strncpy(buf, "SSL_ERROR_WANT_CONNECT", len);
  54. case SSL_ERROR_WANT_ACCEPT: return strncpy(buf, "SSL_ERROR_WANT_ACCEPT", len);
  55. case SSL_ERROR_WANT_X509_LOOKUP: return strncpy(buf, "SSL_ERROR_WANT_X509_LOOKUP", len);
  56. case SSL_ERROR_SYSCALL:
  57. switch (ret) {
  58. case 0:
  59. lws_snprintf(buf, len, "SSL_ERROR_SYSCALL: EOF");
  60. return buf;
  61. case -1:
  62. #ifndef LWS_PLAT_OPTEE
  63. lws_snprintf(buf, len, "SSL_ERROR_SYSCALL: %s", strerror(errno));
  64. #else
  65. lws_snprintf(buf, len, "SSL_ERROR_SYSCALL: %d", errno);
  66. #endif
  67. return buf;
  68. default:
  69. return strncpy(buf, "SSL_ERROR_SYSCALL", len);
  70. }
  71. case SSL_ERROR_SSL: return "SSL_ERROR_SSL";
  72. default: return "SSL_ERROR_UNKNOWN";
  73. }
  74. }
  75. void
  76. lws_ssl_elaborate_error(void)
  77. {
  78. char buf[256];
  79. u_long err;
  80. while ((err = ERR_get_error()) != 0) {
  81. ERR_error_string_n(err, buf, sizeof(buf));
  82. lwsl_err("*** %s\n", buf);
  83. }
  84. }
  85. static int
  86. lws_context_init_ssl_pem_passwd_cb(char * buf, int size, int rwflag, void *userdata)
  87. {
  88. struct lws_context_creation_info * info =
  89. (struct lws_context_creation_info *)userdata;
  90. strncpy(buf, info->ssl_private_key_password, size);
  91. buf[size - 1] = '\0';
  92. return strlen(buf);
  93. }
  94. void
  95. lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, struct lws_context_creation_info *info)
  96. {
  97. if (!info->ssl_private_key_password)
  98. return;
  99. /*
  100. * password provided, set ssl callback and user data
  101. * for checking password which will be trigered during
  102. * SSL_CTX_use_PrivateKey_file function
  103. */
  104. SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, (void *)info);
  105. SSL_CTX_set_default_passwd_cb(ssl_ctx, lws_context_init_ssl_pem_passwd_cb);
  106. }
  107. int
  108. lws_context_init_ssl_library(struct lws_context_creation_info *info)
  109. {
  110. #ifdef USE_WOLFSSL
  111. #ifdef USE_OLD_CYASSL
  112. lwsl_notice(" Compiled with CyaSSL support\n");
  113. #else
  114. lwsl_notice(" Compiled with wolfSSL support\n");
  115. #endif
  116. #else
  117. #if defined(LWS_USE_BORINGSSL)
  118. lwsl_notice(" Compiled with BoringSSL support\n");
  119. #else
  120. lwsl_notice(" Compiled with OpenSSL support\n");
  121. #endif
  122. #endif
  123. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
  124. lwsl_notice(" SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT\n");
  125. return 0;
  126. }
  127. /* basic openssl init */
  128. lwsl_notice("Doing SSL library init\n");
  129. SSL_library_init();
  130. OpenSSL_add_all_algorithms();
  131. SSL_load_error_strings();
  132. openssl_websocket_private_data_index =
  133. SSL_get_ex_new_index(0, "lws", NULL, NULL, NULL);
  134. openssl_SSL_CTX_private_data_index = SSL_CTX_get_ex_new_index(0,
  135. NULL, NULL, NULL, NULL);
  136. return 0;
  137. }
  138. LWS_VISIBLE void
  139. lws_ssl_destroy(struct lws_vhost *vhost)
  140. {
  141. if (!lws_check_opt(vhost->context->options,
  142. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
  143. return;
  144. if (vhost->ssl_ctx)
  145. SSL_CTX_free(vhost->ssl_ctx);
  146. if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
  147. SSL_CTX_free(vhost->ssl_client_ctx);
  148. // after 1.1.0 no need
  149. #if (OPENSSL_VERSION_NUMBER < 0x10100000)
  150. // <= 1.0.1f = old api, 1.0.1g+ = new api
  151. #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
  152. ERR_remove_state(0);
  153. #else
  154. #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
  155. !defined(LIBRESSL_VERSION_NUMBER) && \
  156. !defined(OPENSSL_IS_BORINGSSL)
  157. ERR_remove_thread_state();
  158. #else
  159. ERR_remove_thread_state(NULL);
  160. #endif
  161. #endif
  162. ERR_free_strings();
  163. EVP_cleanup();
  164. CRYPTO_cleanup_all_ex_data();
  165. #endif
  166. }
  167. LWS_VISIBLE void
  168. lws_decode_ssl_error(void)
  169. {
  170. char buf[256];
  171. u_long err;
  172. while ((err = ERR_get_error()) != 0) {
  173. ERR_error_string_n(err, buf, sizeof(buf));
  174. lwsl_err("*** %lu %s\n", err, buf);
  175. }
  176. }
  177. LWS_VISIBLE void
  178. lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
  179. {
  180. struct lws_context *context = wsi->context;
  181. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  182. if (!wsi->pending_read_list_prev &&
  183. !wsi->pending_read_list_next &&
  184. pt->pending_read_list != wsi)
  185. /* we are not on the list */
  186. return;
  187. /* point previous guy's next to our next */
  188. if (!wsi->pending_read_list_prev)
  189. pt->pending_read_list = wsi->pending_read_list_next;
  190. else
  191. wsi->pending_read_list_prev->pending_read_list_next =
  192. wsi->pending_read_list_next;
  193. /* point next guy's previous to our previous */
  194. if (wsi->pending_read_list_next)
  195. wsi->pending_read_list_next->pending_read_list_prev =
  196. wsi->pending_read_list_prev;
  197. wsi->pending_read_list_prev = NULL;
  198. wsi->pending_read_list_next = NULL;
  199. }
  200. LWS_VISIBLE int
  201. lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
  202. {
  203. struct lws_context *context = wsi->context;
  204. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  205. int n = 0;
  206. int ssl_read_errno = 0;
  207. if (!wsi->ssl)
  208. return lws_ssl_capable_read_no_ssl(wsi, buf, len);
  209. n = SSL_read(wsi->ssl, buf, len);
  210. /* manpage: returning 0 means connection shut down */
  211. if (!n) {
  212. n = lws_ssl_get_error(wsi, n);
  213. if (n == SSL_ERROR_ZERO_RETURN)
  214. return LWS_SSL_CAPABLE_ERROR;
  215. if (n == SSL_ERROR_SYSCALL) {
  216. int err = ERR_get_error();
  217. if (err == 0
  218. && (ssl_read_errno == EPIPE
  219. || ssl_read_errno == ECONNABORTED
  220. || ssl_read_errno == 0))
  221. return LWS_SSL_CAPABLE_ERROR;
  222. }
  223. lwsl_err("%s failed: %s\n",__func__,
  224. ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
  225. lws_decode_ssl_error();
  226. return LWS_SSL_CAPABLE_ERROR;
  227. }
  228. if (n < 0) {
  229. n = lws_ssl_get_error(wsi, n);
  230. if (n == SSL_ERROR_WANT_READ || n == SSL_ERROR_WANT_WRITE)
  231. return LWS_SSL_CAPABLE_MORE_SERVICE;
  232. lwsl_err("%s failed2: %s\n",__func__,
  233. ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
  234. lws_decode_ssl_error();
  235. return LWS_SSL_CAPABLE_ERROR;
  236. }
  237. if (wsi->vhost)
  238. wsi->vhost->conn_stats.rx += n;
  239. lws_restart_ws_ping_pong_timer(wsi);
  240. /*
  241. * if it was our buffer that limited what we read,
  242. * check if SSL has additional data pending inside SSL buffers.
  243. *
  244. * Because these won't signal at the network layer with POLLIN
  245. * and if we don't realize, this data will sit there forever
  246. */
  247. if (n != len)
  248. goto bail;
  249. if (!wsi->ssl)
  250. goto bail;
  251. if (!SSL_pending(wsi->ssl))
  252. goto bail;
  253. if (wsi->pending_read_list_next)
  254. return n;
  255. if (wsi->pending_read_list_prev)
  256. return n;
  257. if (pt->pending_read_list == wsi)
  258. return n;
  259. /* add us to the linked list of guys with pending ssl */
  260. if (pt->pending_read_list)
  261. pt->pending_read_list->pending_read_list_prev = wsi;
  262. wsi->pending_read_list_next = pt->pending_read_list;
  263. wsi->pending_read_list_prev = NULL;
  264. pt->pending_read_list = wsi;
  265. return n;
  266. bail:
  267. lws_ssl_remove_wsi_from_buffered_list(wsi);
  268. return n;
  269. }
  270. LWS_VISIBLE int
  271. lws_ssl_pending(struct lws *wsi)
  272. {
  273. if (!wsi->ssl)
  274. return 0;
  275. return SSL_pending(wsi->ssl);
  276. }
  277. LWS_VISIBLE int
  278. lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len)
  279. {
  280. int n;
  281. int ssl_read_errno = 0;
  282. if (!wsi->ssl)
  283. return lws_ssl_capable_write_no_ssl(wsi, buf, len);
  284. n = SSL_write(wsi->ssl, buf, len);
  285. if (n > 0)
  286. return n;
  287. n = lws_ssl_get_error(wsi, n);
  288. if (n == SSL_ERROR_WANT_READ || n == SSL_ERROR_WANT_WRITE) {
  289. if (n == SSL_ERROR_WANT_WRITE)
  290. lws_set_blocking_send(wsi);
  291. return LWS_SSL_CAPABLE_MORE_SERVICE;
  292. }
  293. if (n == SSL_ERROR_ZERO_RETURN)
  294. return LWS_SSL_CAPABLE_ERROR;
  295. if (n == SSL_ERROR_SYSCALL) {
  296. int err = ERR_get_error();
  297. if (err == 0
  298. && (ssl_read_errno == EPIPE
  299. || ssl_read_errno == ECONNABORTED
  300. || ssl_read_errno == 0))
  301. return LWS_SSL_CAPABLE_ERROR;
  302. }
  303. lwsl_err("%s failed: %s\n",__func__,
  304. ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
  305. lws_decode_ssl_error();
  306. return LWS_SSL_CAPABLE_ERROR;
  307. }
  308. LWS_VISIBLE int
  309. lws_ssl_close(struct lws *wsi)
  310. {
  311. int n;
  312. if (!wsi->ssl)
  313. return 0; /* not handled */
  314. n = SSL_get_fd(wsi->ssl);
  315. SSL_shutdown(wsi->ssl);
  316. compatible_close(n);
  317. SSL_free(wsi->ssl);
  318. wsi->ssl = NULL;
  319. return 1; /* handled */
  320. }
  321. /* leave all wsi close processing to the caller */
  322. LWS_VISIBLE int
  323. lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
  324. {
  325. struct lws_context *context = wsi->context;
  326. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  327. int n, m;
  328. #if !defined(USE_WOLFSSL)
  329. BIO *bio;
  330. #endif
  331. char buf[256];
  332. if (!LWS_SSL_ENABLED(wsi->vhost))
  333. return 0;
  334. switch (wsi->mode) {
  335. case LWSCM_SSL_INIT:
  336. case LWSCM_SSL_INIT_RAW:
  337. if (wsi->ssl)
  338. lwsl_err("%s: leaking ssl\n", __func__);
  339. if (accept_fd == LWS_SOCK_INVALID)
  340. assert(0);
  341. wsi->ssl = SSL_new(wsi->vhost->ssl_ctx);
  342. if (wsi->ssl == NULL) {
  343. lwsl_err("SSL_new failed: %s\n",
  344. ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
  345. lws_decode_ssl_error();
  346. if (accept_fd != LWS_SOCK_INVALID)
  347. compatible_close(accept_fd);
  348. goto fail;
  349. }
  350. SSL_set_ex_data(wsi->ssl,
  351. openssl_websocket_private_data_index, wsi);
  352. SSL_set_fd(wsi->ssl, accept_fd);
  353. #ifdef USE_WOLFSSL
  354. #ifdef USE_OLD_CYASSL
  355. CyaSSL_set_using_nonblock(wsi->ssl, 1);
  356. #else
  357. wolfSSL_set_using_nonblock(wsi->ssl, 1);
  358. #endif
  359. #else
  360. SSL_set_mode(wsi->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  361. bio = SSL_get_rbio(wsi->ssl);
  362. if (bio)
  363. BIO_set_nbio(bio, 1); /* nonblocking */
  364. else
  365. lwsl_notice("NULL rbio\n");
  366. bio = SSL_get_wbio(wsi->ssl);
  367. if (bio)
  368. BIO_set_nbio(bio, 1); /* nonblocking */
  369. else
  370. lwsl_notice("NULL rbio\n");
  371. #endif
  372. /*
  373. * we are not accepted yet, but we need to enter ourselves
  374. * as a live connection. That way we can retry when more
  375. * pieces come if we're not sorted yet
  376. */
  377. if (wsi->mode == LWSCM_SSL_INIT)
  378. wsi->mode = LWSCM_SSL_ACK_PENDING;
  379. else
  380. wsi->mode = LWSCM_SSL_ACK_PENDING_RAW;
  381. if (insert_wsi_socket_into_fds(context, wsi)) {
  382. lwsl_err("%s: failed to insert into fds\n", __func__);
  383. goto fail;
  384. }
  385. lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
  386. context->timeout_secs);
  387. lwsl_info("inserted SSL accept into fds, trying SSL_accept\n");
  388. /* fallthru */
  389. case LWSCM_SSL_ACK_PENDING:
  390. case LWSCM_SSL_ACK_PENDING_RAW:
  391. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  392. lwsl_err("%s: lws_change_pollfd failed\n", __func__);
  393. goto fail;
  394. }
  395. lws_latency_pre(context, wsi);
  396. n = recv(wsi->desc.sockfd, (char *)pt->serv_buf, context->pt_serv_buf_size,
  397. MSG_PEEK);
  398. /*
  399. * optionally allow non-SSL connect on SSL listening socket
  400. * This is disabled by default, if enabled it goes around any
  401. * SSL-level access control (eg, client-side certs) so leave
  402. * it disabled unless you know it's not a problem for you
  403. */
  404. if (wsi->vhost->allow_non_ssl_on_ssl_port) {
  405. if (n >= 1 && pt->serv_buf[0] >= ' ') {
  406. /*
  407. * TLS content-type for Handshake is 0x16, and
  408. * for ChangeCipherSpec Record, it's 0x14
  409. *
  410. * A non-ssl session will start with the HTTP
  411. * method in ASCII. If we see it's not a legit
  412. * SSL handshake kill the SSL for this
  413. * connection and try to handle as a HTTP
  414. * connection upgrade directly.
  415. */
  416. wsi->use_ssl = 0;
  417. SSL_shutdown(wsi->ssl);
  418. SSL_free(wsi->ssl);
  419. wsi->ssl = NULL;
  420. if (lws_check_opt(context->options,
  421. LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS))
  422. wsi->redirect_to_https = 1;
  423. goto accepted;
  424. }
  425. if (!n) /*
  426. * connection is gone, or nothing to read
  427. * if it's gone, we will timeout on
  428. * PENDING_TIMEOUT_SSL_ACCEPT
  429. */
  430. break;
  431. if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
  432. LWS_ERRNO == LWS_EWOULDBLOCK)) {
  433. /*
  434. * well, we get no way to know ssl or not
  435. * so go around again waiting for something
  436. * to come and give us a hint, or timeout the
  437. * connection.
  438. */
  439. m = SSL_ERROR_WANT_READ;
  440. goto go_again;
  441. }
  442. }
  443. /* normal SSL connection processing path */
  444. n = SSL_accept(wsi->ssl);
  445. lws_latency(context, wsi,
  446. "SSL_accept LWSCM_SSL_ACK_PENDING\n", n, n == 1);
  447. if (n == 1)
  448. goto accepted;
  449. m = lws_ssl_get_error(wsi, n);
  450. go_again:
  451. if (m == SSL_ERROR_WANT_READ) {
  452. if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
  453. lwsl_err("%s: WANT_READ change_pollfd failed\n", __func__);
  454. goto fail;
  455. }
  456. lwsl_info("SSL_ERROR_WANT_READ\n");
  457. break;
  458. }
  459. if (m == SSL_ERROR_WANT_WRITE) {
  460. if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
  461. lwsl_err("%s: WANT_WRITE change_pollfd failed\n", __func__);
  462. goto fail;
  463. }
  464. break;
  465. }
  466. lwsl_err("SSL_accept failed socket %u: %s\n", wsi->desc.sockfd,
  467. lws_ssl_get_error_string(m, n, buf, sizeof(buf)));
  468. lws_ssl_elaborate_error();
  469. goto fail;
  470. accepted:
  471. /* OK, we are accepted... give him some time to negotiate */
  472. lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
  473. context->timeout_secs);
  474. if (wsi->mode == LWSCM_SSL_ACK_PENDING_RAW)
  475. wsi->mode = LWSCM_RAW;
  476. else
  477. wsi->mode = LWSCM_HTTP_SERVING;
  478. lws_http2_configure_if_upgraded(wsi);
  479. lwsl_debug("accepted new SSL conn\n");
  480. break;
  481. }
  482. return 0;
  483. fail:
  484. return 1;
  485. }
  486. void
  487. lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
  488. {
  489. if (vhost->ssl_ctx)
  490. SSL_CTX_free(vhost->ssl_ctx);
  491. if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
  492. SSL_CTX_free(vhost->ssl_client_ctx);
  493. }
  494. void
  495. lws_ssl_context_destroy(struct lws_context *context)
  496. {
  497. // after 1.1.0 no need
  498. #if (OPENSSL_VERSION_NUMBER < 0x10100000)
  499. // <= 1.0.1f = old api, 1.0.1g+ = new api
  500. #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
  501. ERR_remove_state(0);
  502. #else
  503. #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
  504. !defined(LIBRESSL_VERSION_NUMBER) && \
  505. !defined(OPENSSL_IS_BORINGSSL)
  506. ERR_remove_thread_state();
  507. #else
  508. ERR_remove_thread_state(NULL);
  509. #endif
  510. #endif
  511. ERR_free_strings();
  512. EVP_cleanup();
  513. CRYPTO_cleanup_all_ex_data();
  514. #endif
  515. }