tls-client.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. int
  26. lws_ssl_client_connect1(struct lws *wsi, char *errbuf, int len)
  27. {
  28. int n;
  29. n = lws_tls_client_connect(wsi, errbuf, len);
  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:
  39. case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
  40. lwsi_set_state(wsi, LRS_WAITING_SSL);
  41. break;
  42. }
  43. return 0; /* retry */
  44. }
  45. int
  46. lws_ssl_client_connect2(struct lws *wsi, char *errbuf, int len)
  47. {
  48. int n;
  49. if (lwsi_state(wsi) == LRS_WAITING_SSL) {
  50. n = lws_tls_client_connect(wsi, errbuf, len);
  51. lwsl_debug("%s: SSL_connect says %d\n", __func__, n);
  52. switch (n) {
  53. case LWS_SSL_CAPABLE_ERROR:
  54. // lws_snprintf(errbuf, len, "client connect failed");
  55. return -1;
  56. case LWS_SSL_CAPABLE_DONE:
  57. break; /* connected */
  58. case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
  59. lws_callback_on_writable(wsi);
  60. /* fallthru */
  61. case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
  62. lwsi_set_state(wsi, LRS_WAITING_SSL);
  63. /* fallthru */
  64. case LWS_SSL_CAPABLE_MORE_SERVICE:
  65. return 0;
  66. }
  67. }
  68. if (lws_tls_client_confirm_peer_cert(wsi, errbuf, len))
  69. return -1;
  70. return 1;
  71. }
  72. int lws_context_init_client_ssl(const struct lws_context_creation_info *info,
  73. struct lws_vhost *vhost)
  74. {
  75. const char *private_key_filepath = info->ssl_private_key_filepath;
  76. const char *cert_filepath = info->ssl_cert_filepath;
  77. const char *ca_filepath = info->ssl_ca_filepath;
  78. const char *cipher_list = info->ssl_cipher_list;
  79. lws_fakewsi_def_plwsa(&vhost->context->pt[0]);
  80. lws_fakewsi_prep_plwsa_ctx(vhost->context);
  81. if (vhost->options & LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG)
  82. return 0;
  83. if (vhost->tls.ssl_ctx) {
  84. cert_filepath = NULL;
  85. private_key_filepath = NULL;
  86. ca_filepath = NULL;
  87. }
  88. /*
  89. * for backwards-compatibility default to using ssl_... members, but
  90. * if the newer client-specific ones are given, use those
  91. */
  92. if (info->client_ssl_cipher_list)
  93. cipher_list = info->client_ssl_cipher_list;
  94. if (info->client_ssl_cert_filepath)
  95. cert_filepath = info->client_ssl_cert_filepath;
  96. if (info->client_ssl_private_key_filepath)
  97. private_key_filepath = info->client_ssl_private_key_filepath;
  98. if (info->client_ssl_ca_filepath)
  99. ca_filepath = info->client_ssl_ca_filepath;
  100. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
  101. return 0;
  102. if (vhost->tls.ssl_client_ctx)
  103. return 0;
  104. #if !defined(LWS_WITH_MBEDTLS)
  105. if (info->provided_client_ssl_ctx) {
  106. /* use the provided OpenSSL context if given one */
  107. vhost->tls.ssl_client_ctx = info->provided_client_ssl_ctx;
  108. /* nothing for lib to delete */
  109. vhost->tls.user_supplied_ssl_ctx = 1;
  110. return 0;
  111. }
  112. #endif
  113. if (lws_tls_client_create_vhost_context(vhost, info, cipher_list,
  114. ca_filepath,
  115. info->client_ssl_ca_mem,
  116. info->client_ssl_ca_mem_len,
  117. cert_filepath,
  118. info->client_ssl_cert_mem,
  119. info->client_ssl_cert_mem_len,
  120. private_key_filepath,
  121. info->client_ssl_key_mem,
  122. info->client_ssl_key_mem_len
  123. ))
  124. return 1;
  125. lwsl_info("created client ssl context for %s\n", vhost->name);
  126. /*
  127. * give him a fake wsi with context set, so he can use
  128. * lws_get_context() in the callback
  129. */
  130. plwsa->vhost = vhost; /* not a real bound wsi */
  131. vhost->protocols[0].callback((struct lws *)plwsa,
  132. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
  133. vhost->tls.ssl_client_ctx, NULL, 0);
  134. return 0;
  135. }