ssl-server.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. extern int openssl_websocket_private_data_index,
  23. openssl_SSL_CTX_private_data_index;
  24. extern void
  25. lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, struct lws_context_creation_info *info);
  26. static int
  27. OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
  28. {
  29. SSL *ssl;
  30. int n;
  31. struct lws *wsi;
  32. ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
  33. SSL_get_ex_data_X509_STORE_CTX_idx());
  34. /*
  35. * !!! nasty openssl requires the index to come as a library-scope
  36. * static
  37. */
  38. wsi = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
  39. n = wsi->vhost->protocols[0].callback(wsi,
  40. LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
  41. x509_ctx, ssl, preverify_ok);
  42. /* convert return code from 0 = OK to 1 = OK */
  43. return !n;
  44. }
  45. static int
  46. lws_context_ssl_init_ecdh(struct lws_vhost *vhost)
  47. {
  48. #ifdef LWS_SSL_SERVER_WITH_ECDH_CERT
  49. EC_KEY *EC_key = NULL;
  50. EVP_PKEY *pkey;
  51. int KeyType;
  52. X509 *x;
  53. if (!lws_check_opt(vhost->context->options, LWS_SERVER_OPTION_SSL_ECDH))
  54. return 0;
  55. lwsl_notice(" Using ECDH certificate support\n");
  56. /* Get X509 certificate from ssl context */
  57. x = sk_X509_value(vhost->ssl_ctx->extra_certs, 0);
  58. if (!x) {
  59. lwsl_err("%s: x is NULL\n", __func__);
  60. return 1;
  61. }
  62. /* Get the public key from certificate */
  63. pkey = X509_get_pubkey(x);
  64. if (!pkey) {
  65. lwsl_err("%s: pkey is NULL\n", __func__);
  66. return 1;
  67. }
  68. /* Get the key type */
  69. KeyType = EVP_PKEY_type(pkey->type);
  70. if (EVP_PKEY_EC != KeyType) {
  71. lwsl_notice("Key type is not EC\n");
  72. return 0;
  73. }
  74. /* Get the key */
  75. EC_key = EVP_PKEY_get1_EC_KEY(pkey);
  76. /* Set ECDH parameter */
  77. if (!EC_key) {
  78. lwsl_err("%s: ECDH key is NULL \n", __func__);
  79. return 1;
  80. }
  81. SSL_CTX_set_tmp_ecdh(vhost->ssl_ctx, EC_key);
  82. EC_KEY_free(EC_key);
  83. #endif
  84. return 0;
  85. }
  86. static int
  87. lws_context_ssl_init_ecdh_curve(struct lws_context_creation_info *info,
  88. struct lws_vhost *vhost)
  89. {
  90. #ifdef LWS_HAVE_OPENSSL_ECDH_H
  91. EC_KEY *ecdh;
  92. int ecdh_nid;
  93. const char *ecdh_curve = "prime256v1";
  94. if (info->ecdh_curve)
  95. ecdh_curve = info->ecdh_curve;
  96. ecdh_nid = OBJ_sn2nid(ecdh_curve);
  97. if (NID_undef == ecdh_nid) {
  98. lwsl_err("SSL: Unknown curve name '%s'", ecdh_curve);
  99. return 1;
  100. }
  101. ecdh = EC_KEY_new_by_curve_name(ecdh_nid);
  102. if (NULL == ecdh) {
  103. lwsl_err("SSL: Unable to create curve '%s'", ecdh_curve);
  104. return 1;
  105. }
  106. SSL_CTX_set_tmp_ecdh(vhost->ssl_ctx, ecdh);
  107. EC_KEY_free(ecdh);
  108. SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_SINGLE_ECDH_USE);
  109. lwsl_notice(" SSL ECDH curve '%s'\n", ecdh_curve);
  110. #else
  111. lwsl_notice(" OpenSSL doesn't support ECDH\n");
  112. #endif
  113. return 0;
  114. }
  115. #ifndef OPENSSL_NO_TLSEXT
  116. static int
  117. lws_ssl_server_name_cb(SSL *ssl, int *ad, void *arg)
  118. {
  119. struct lws_context *context;
  120. struct lws_vhost *vhost, *vh;
  121. const char *servername;
  122. int port;
  123. if (!ssl)
  124. return SSL_TLSEXT_ERR_NOACK;
  125. context = (struct lws_context *)SSL_CTX_get_ex_data(
  126. SSL_get_SSL_CTX(ssl),
  127. openssl_SSL_CTX_private_data_index);
  128. /*
  129. * We can only get ssl accepted connections by using a vhost's ssl_ctx
  130. * find out which listening one took us and only match vhosts on the
  131. * same port.
  132. */
  133. vh = context->vhost_list;
  134. while (vh) {
  135. if (vh->ssl_ctx == SSL_get_SSL_CTX(ssl))
  136. break;
  137. vh = vh->vhost_next;
  138. }
  139. assert(vh); /* we cannot get an ssl without using a vhost ssl_ctx */
  140. port = vh->listen_port;
  141. servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  142. if (servername) {
  143. vhost = lws_select_vhost(context, port, servername);
  144. if (vhost) {
  145. lwsl_debug("SNI: Found: %s (port %d)\n",
  146. servername, port);
  147. SSL_set_SSL_CTX(ssl, vhost->ssl_ctx);
  148. return SSL_TLSEXT_ERR_OK;
  149. }
  150. lwsl_err("SNI: Unknown ServerName: %s\n", servername);
  151. }
  152. return SSL_TLSEXT_ERR_OK;
  153. }
  154. #endif
  155. LWS_VISIBLE int
  156. lws_context_init_server_ssl(struct lws_context_creation_info *info,
  157. struct lws_vhost *vhost)
  158. {
  159. struct lws_context *context = vhost->context;
  160. struct lws wsi;
  161. unsigned long error;
  162. int n;
  163. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
  164. vhost->use_ssl = 0;
  165. return 0;
  166. }
  167. if (info->port != CONTEXT_PORT_NO_LISTEN) {
  168. vhost->use_ssl = info->ssl_cert_filepath != NULL;
  169. if (vhost->use_ssl && info->ssl_cipher_list)
  170. lwsl_notice(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
  171. if (vhost->use_ssl)
  172. lwsl_notice(" Using SSL mode\n");
  173. else
  174. lwsl_notice(" Using non-SSL mode\n");
  175. }
  176. /*
  177. * give him a fake wsi with context + vhost set, so he can use
  178. * lws_get_context() in the callback
  179. */
  180. memset(&wsi, 0, sizeof(wsi));
  181. wsi.vhost = vhost;
  182. wsi.context = context;
  183. (void)n;
  184. (void)error;
  185. /*
  186. * Firefox insists on SSLv23 not SSLv3
  187. * Konq disables SSLv2 by default now, SSLv23 works
  188. *
  189. * SSLv23_server_method() is the openssl method for "allow all TLS
  190. * versions", compared to e.g. TLSv1_2_server_method() which only allows
  191. * tlsv1.2. Unwanted versions must be disabled using SSL_CTX_set_options()
  192. */
  193. {
  194. SSL_METHOD *method;
  195. method = (SSL_METHOD *)SSLv23_server_method();
  196. if (!method) {
  197. error = ERR_get_error();
  198. lwsl_err("problem creating ssl method %lu: %s\n",
  199. error, ERR_error_string(error,
  200. (char *)context->pt[0].serv_buf));
  201. return 1;
  202. }
  203. vhost->ssl_ctx = SSL_CTX_new(method); /* create context */
  204. if (!vhost->ssl_ctx) {
  205. error = ERR_get_error();
  206. lwsl_err("problem creating ssl context %lu: %s\n",
  207. error, ERR_error_string(error,
  208. (char *)context->pt[0].serv_buf));
  209. return 1;
  210. }
  211. }
  212. /* associate the lws context with the SSL_CTX */
  213. SSL_CTX_set_ex_data(vhost->ssl_ctx,
  214. openssl_SSL_CTX_private_data_index, vhost->context);
  215. /* Disable SSLv2 and SSLv3 */
  216. SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  217. #ifdef SSL_OP_NO_COMPRESSION
  218. SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_NO_COMPRESSION);
  219. #endif
  220. SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_SINGLE_DH_USE);
  221. SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
  222. if (info->ssl_cipher_list)
  223. SSL_CTX_set_cipher_list(vhost->ssl_ctx,
  224. info->ssl_cipher_list);
  225. /* as a server, are we requiring clients to identify themselves? */
  226. if (lws_check_opt(info->options,
  227. LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT)) {
  228. int verify_options = SSL_VERIFY_PEER;
  229. if (!lws_check_opt(info->options,
  230. LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED))
  231. verify_options |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  232. SSL_CTX_set_session_id_context(vhost->ssl_ctx,
  233. (unsigned char *)context, sizeof(void *));
  234. /* absolutely require the client cert */
  235. SSL_CTX_set_verify(vhost->ssl_ctx,
  236. verify_options, OpenSSL_verify_callback);
  237. }
  238. #ifndef OPENSSL_NO_TLSEXT
  239. SSL_CTX_set_tlsext_servername_callback(vhost->ssl_ctx,
  240. lws_ssl_server_name_cb);
  241. #endif
  242. /*
  243. * give user code a chance to load certs into the server
  244. * allowing it to verify incoming client certs
  245. */
  246. if (info->ssl_ca_filepath &&
  247. !SSL_CTX_load_verify_locations(vhost->ssl_ctx,
  248. info->ssl_ca_filepath, NULL)) {
  249. lwsl_err("%s: SSL_CTX_load_verify_locations unhappy\n", __func__);
  250. }
  251. if (vhost->use_ssl) {
  252. if (lws_context_ssl_init_ecdh_curve(info, vhost))
  253. return -1;
  254. vhost->protocols[0].callback(&wsi,
  255. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
  256. vhost->ssl_ctx, NULL, 0);
  257. }
  258. if (lws_check_opt(info->options, LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT))
  259. /* Normally SSL listener rejects non-ssl, optionally allow */
  260. vhost->allow_non_ssl_on_ssl_port = 1;
  261. if (info->ssl_options_set)
  262. SSL_CTX_set_options(vhost->ssl_ctx, info->ssl_options_set);
  263. /* SSL_clear_options introduced in 0.9.8m */
  264. #if (OPENSSL_VERSION_NUMBER >= 0x009080df) && !defined(USE_WOLFSSL)
  265. if (info->ssl_options_clear)
  266. SSL_CTX_clear_options(vhost->ssl_ctx, info->ssl_options_clear);
  267. #endif
  268. lwsl_info(" SSL options 0x%lX\n",
  269. SSL_CTX_get_options(vhost->ssl_ctx));
  270. if (vhost->use_ssl) {
  271. /* openssl init for server sockets */
  272. /* set the local certificate from CertFile */
  273. n = SSL_CTX_use_certificate_chain_file(vhost->ssl_ctx,
  274. info->ssl_cert_filepath);
  275. if (n != 1) {
  276. error = ERR_get_error();
  277. lwsl_err("problem getting cert '%s' %lu: %s\n",
  278. info->ssl_cert_filepath,
  279. error,
  280. ERR_error_string(error,
  281. (char *)context->pt[0].serv_buf));
  282. return 1;
  283. }
  284. lws_ssl_bind_passphrase(vhost->ssl_ctx, info);
  285. if (info->ssl_private_key_filepath != NULL) {
  286. /* set the private key from KeyFile */
  287. if (SSL_CTX_use_PrivateKey_file(vhost->ssl_ctx,
  288. info->ssl_private_key_filepath,
  289. SSL_FILETYPE_PEM) != 1) {
  290. error = ERR_get_error();
  291. lwsl_err("ssl problem getting key '%s' %lu: %s\n",
  292. info->ssl_private_key_filepath, error,
  293. ERR_error_string(error,
  294. (char *)context->pt[0].serv_buf));
  295. return 1;
  296. }
  297. } else
  298. if (vhost->protocols[0].callback(&wsi,
  299. LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
  300. vhost->ssl_ctx, NULL, 0)) {
  301. lwsl_err("ssl private key not set\n");
  302. return 1;
  303. }
  304. /* verify private key */
  305. if (!SSL_CTX_check_private_key(vhost->ssl_ctx)) {
  306. lwsl_err("Private SSL key doesn't match cert\n");
  307. return 1;
  308. }
  309. if (lws_context_ssl_init_ecdh(vhost))
  310. return 1;
  311. /*
  312. * SSL is happy and has a cert it's content with
  313. * If we're supporting HTTP2, initialize that
  314. */
  315. lws_context_init_http2_ssl(vhost);
  316. }
  317. return 0;
  318. }