private-lib-tls.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * This is included from private-lib-core.h if LWS_WITH_TLS
  25. */
  26. #if !defined(__LWS_TLS_PRIVATE_H__)
  27. #define __LWS_TLS_PRIVATE_H__
  28. #if defined(LWS_WITH_TLS)
  29. #if defined(USE_WOLFSSL)
  30. #if defined(USE_OLD_CYASSL)
  31. #if defined(_WIN32)
  32. #include <IDE/WIN/user_settings.h>
  33. #include <cyassl/ctaocrypt/settings.h>
  34. #else
  35. #include <cyassl/options.h>
  36. #endif
  37. #include <cyassl/openssl/ssl.h>
  38. #include <cyassl/error-ssl.h>
  39. #else
  40. #if defined(_WIN32)
  41. #include <IDE/WIN/user_settings.h>
  42. #include <wolfssl/wolfcrypt/settings.h>
  43. #else
  44. #include <wolfssl/options.h>
  45. #endif
  46. #include <wolfssl/openssl/ssl.h>
  47. #include <wolfssl/error-ssl.h>
  48. #define OPENSSL_NO_TLSEXT
  49. #endif /* not USE_OLD_CYASSL */
  50. #else /* WOLFSSL */
  51. #if defined(LWS_PLAT_FREERTOS)
  52. #define OPENSSL_NO_TLSEXT
  53. #if !defined(LWS_AMAZON_RTOS)
  54. /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
  55. #undef MBEDTLS_CONFIG_FILE
  56. #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
  57. #endif
  58. #include <mbedtls/ssl.h>
  59. #include <mbedtls/aes.h>
  60. #include <mbedtls/gcm.h>
  61. #include <mbedtls/x509_crt.h>
  62. #include "ssl.h" /* wrapper !!!! */
  63. #else /* not esp32 */
  64. #if defined(LWS_WITH_MBEDTLS)
  65. #include <mbedtls/ssl.h>
  66. #include <mbedtls/aes.h>
  67. #include <mbedtls/gcm.h>
  68. #include <mbedtls/x509_crt.h>
  69. #include <mbedtls/x509_csr.h>
  70. #include <mbedtls/ecp.h>
  71. #include <mbedtls/ecdsa.h>
  72. #if defined(LWS_AMAZON_LINUX)
  73. #include "ssl.h" /* wrapper !!!! */
  74. #else
  75. #include "openssl/ssl.h" /* wrapper !!!! */
  76. #endif
  77. #else
  78. #include <openssl/ssl.h>
  79. #include <openssl/evp.h>
  80. #include <openssl/err.h>
  81. #include <openssl/md5.h>
  82. #include <openssl/sha.h>
  83. #include <openssl/rsa.h>
  84. #include <openssl/bn.h>
  85. #include <openssl/aes.h>
  86. #ifdef LWS_HAVE_OPENSSL_ECDH_H
  87. #include <openssl/ecdh.h>
  88. #endif
  89. #if !defined(LWS_HAVE_EVP_MD_CTX_free) && !defined(USE_WOLFSSL)
  90. #define EVP_MD_CTX_free EVP_MD_CTX_destroy
  91. #endif
  92. #include <openssl/x509v3.h>
  93. #endif /* not mbedtls */
  94. #if defined(OPENSSL_VERSION_NUMBER)
  95. #if (OPENSSL_VERSION_NUMBER < 0x0009080afL)
  96. /*
  97. * later openssl defines this to negate the presence of tlsext... but it was
  98. * only introduced at 0.9.8j. Earlier versions don't know it exists so don't
  99. * define it... making it look like the feature exists...
  100. */
  101. #define OPENSSL_NO_TLSEXT
  102. #endif
  103. #endif
  104. #endif /* not ESP32 */
  105. #endif /* not USE_WOLFSSL */
  106. #endif /* LWS_WITH_TLS */
  107. enum lws_tls_extant {
  108. LWS_TLS_EXTANT_NO,
  109. LWS_TLS_EXTANT_YES,
  110. LWS_TLS_EXTANT_ALTERNATIVE
  111. };
  112. #if defined(LWS_WITH_TLS)
  113. int
  114. lws_tls_restrict_borrow(struct lws_context *context);
  115. void
  116. lws_tls_restrict_return(struct lws_context *context);
  117. typedef SSL lws_tls_conn;
  118. typedef SSL_CTX lws_tls_ctx;
  119. typedef BIO lws_tls_bio;
  120. typedef X509 lws_tls_x509;
  121. #if defined(LWS_WITH_NETWORK)
  122. #include "private-network.h"
  123. #endif
  124. LWS_EXTERN int
  125. lws_context_init_ssl_library(const struct lws_context_creation_info *info);
  126. LWS_EXTERN void
  127. lws_context_deinit_ssl_library(struct lws_context *context);
  128. #define LWS_SSL_ENABLED(vh) (vh && vh->tls.use_ssl)
  129. extern const struct lws_tls_ops tls_ops_openssl, tls_ops_mbedtls;
  130. struct lws_ec_valid_curves {
  131. int id;
  132. const char *jwa_name; /* list terminates with NULL jwa_name */
  133. };
  134. LWS_EXTERN enum lws_tls_extant
  135. lws_tls_use_any_upgrade_check_extant(const char *name);
  136. LWS_EXTERN int openssl_websocket_private_data_index;
  137. LWS_EXTERN void
  138. lws_tls_err_describe_clear(void);
  139. LWS_EXTERN int
  140. lws_tls_openssl_cert_info(X509 *x509, enum lws_tls_cert_info type,
  141. union lws_tls_cert_info_results *buf, size_t len);
  142. LWS_EXTERN int
  143. lws_tls_check_all_cert_lifetimes(struct lws_context *context);
  144. LWS_EXTERN int
  145. lws_tls_alloc_pem_to_der_file(struct lws_context *context, const char *filename,
  146. const char *inbuf, lws_filepos_t inlen,
  147. uint8_t **buf, lws_filepos_t *amount);
  148. LWS_EXTERN char *
  149. lws_ssl_get_error_string(int status, int ret, char *buf, size_t len);
  150. int
  151. lws_gencrypto_bits_to_bytes(int bits);
  152. void
  153. lws_gencrypto_destroy_elements(struct lws_gencrypto_keyelem *el, int m);
  154. /* genec */
  155. struct lws_gencrypto_keyelem;
  156. struct lws_ec_curves;
  157. LWS_EXTERN const struct lws_ec_curves lws_ec_curves[4];
  158. const struct lws_ec_curves *
  159. lws_genec_curve(const struct lws_ec_curves *table, const char *name);
  160. LWS_VISIBLE void
  161. lws_genec_destroy_elements(struct lws_gencrypto_keyelem *el);
  162. int
  163. lws_gencrypto_mbedtls_rngf(void *context, unsigned char *buf, size_t len);
  164. int
  165. lws_genec_confirm_curve_allowed_by_tls_id(const char *allowed, int id,
  166. struct lws_jwk *jwk);
  167. #else /* ! WITH_TLS */
  168. #define lws_tls_restrict_borrow(xxx) (0)
  169. #define lws_tls_restrict_return(xxx)
  170. #endif
  171. #endif