tls-client.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * libwebsockets - client-related ssl code independent of backend
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  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. int
  23. lws_ssl_client_connect1(struct lws *wsi)
  24. {
  25. struct lws_context *context = wsi->context;
  26. int n = 0;
  27. lws_latency_pre(context, wsi);
  28. n = lws_tls_client_connect(wsi);
  29. lws_latency(context, wsi, "SSL_connect hs", n, n > 0);
  30. switch (n) {
  31. case LWS_SSL_CAPABLE_ERROR:
  32. return -1;
  33. case LWS_SSL_CAPABLE_DONE:
  34. return 1; /* connected */
  35. case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
  36. lws_callback_on_writable(wsi);
  37. /* fallthru */
  38. case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
  39. lwsi_set_state(wsi, LRS_WAITING_SSL);
  40. break;
  41. case LWS_SSL_CAPABLE_MORE_SERVICE:
  42. break;
  43. }
  44. return 0; /* retry */
  45. }
  46. int
  47. lws_ssl_client_connect2(struct lws *wsi, char *errbuf, int len)
  48. {
  49. int n = 0;
  50. if (lwsi_state(wsi) == LRS_WAITING_SSL) {
  51. lws_latency_pre(wsi->context, wsi);
  52. n = lws_tls_client_connect(wsi);
  53. lwsl_debug("%s: SSL_connect says %d\n", __func__, n);
  54. lws_latency(wsi->context, wsi,
  55. "SSL_connect LRS_WAITING_SSL", n, n > 0);
  56. switch (n) {
  57. case LWS_SSL_CAPABLE_ERROR:
  58. lws_snprintf(errbuf, len, "client connect failed");
  59. return -1;
  60. case LWS_SSL_CAPABLE_DONE:
  61. break; /* connected */
  62. case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
  63. lws_callback_on_writable(wsi);
  64. /* fallthru */
  65. case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
  66. lwsi_set_state(wsi, LRS_WAITING_SSL);
  67. /* fallthru */
  68. case LWS_SSL_CAPABLE_MORE_SERVICE:
  69. return 0;
  70. }
  71. }
  72. if (lws_tls_client_confirm_peer_cert(wsi, errbuf, len))
  73. return -1;
  74. return 1;
  75. }
  76. int lws_context_init_client_ssl(const struct lws_context_creation_info *info,
  77. struct lws_vhost *vhost)
  78. {
  79. const char *private_key_filepath = info->ssl_private_key_filepath;
  80. const char *cert_filepath = info->ssl_cert_filepath;
  81. const char *ca_filepath = info->ssl_ca_filepath;
  82. const char *cipher_list = info->ssl_cipher_list;
  83. struct lws wsi;
  84. if (vhost->options & LWS_SERVER_OPTION_ONLY_RAW)
  85. return 0;
  86. /*
  87. * for backwards-compatibility default to using ssl_... members, but
  88. * if the newer client-specific ones are given, use those
  89. */
  90. if (info->client_ssl_cipher_list)
  91. cipher_list = info->client_ssl_cipher_list;
  92. if (info->client_ssl_cert_filepath)
  93. cert_filepath = info->client_ssl_cert_filepath;
  94. if (info->client_ssl_private_key_filepath)
  95. private_key_filepath = info->client_ssl_private_key_filepath;
  96. if (info->client_ssl_ca_filepath)
  97. ca_filepath = info->client_ssl_ca_filepath;
  98. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
  99. return 0;
  100. if (vhost->tls.ssl_client_ctx)
  101. return 0;
  102. if (info->provided_client_ssl_ctx) {
  103. /* use the provided OpenSSL context if given one */
  104. vhost->tls.ssl_client_ctx = info->provided_client_ssl_ctx;
  105. /* nothing for lib to delete */
  106. vhost->tls.user_supplied_ssl_ctx = 1;
  107. return 0;
  108. }
  109. if (lws_tls_client_create_vhost_context(vhost, info, cipher_list,
  110. ca_filepath,
  111. info->client_ssl_ca_mem,
  112. info->client_ssl_ca_mem_len,
  113. cert_filepath,
  114. private_key_filepath))
  115. return 1;
  116. lwsl_notice("created client ssl context for %s\n", vhost->name);
  117. /*
  118. * give him a fake wsi with context set, so he can use
  119. * lws_get_context() in the callback
  120. */
  121. memset(&wsi, 0, sizeof(wsi));
  122. wsi.vhost = vhost; /* not a real bound wsi */
  123. wsi.context = vhost->context;
  124. vhost->protocols[0].callback(&wsi,
  125. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
  126. vhost->tls.ssl_client_ctx, NULL, 0);
  127. return 0;
  128. }