altcp_tls.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file
  3. * Application layered TCP/TLS connection API (to be used from TCPIP thread)
  4. *
  5. * @defgroup altcp_tls TLS layer
  6. * @ingroup altcp
  7. * This file contains function prototypes for a TLS layer.
  8. * A port to ARM mbedtls is provided in the apps/ tree
  9. * (LWIP_ALTCP_TLS_MBEDTLS option).
  10. */
  11. /*
  12. * Copyright (c) 2017 Simon Goldschmidt
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  29. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  31. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  34. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  35. * OF SUCH DAMAGE.
  36. *
  37. * This file is part of the lwIP TCP/IP stack.
  38. *
  39. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  40. *
  41. */
  42. #ifndef LWIP_HDR_ALTCP_TLS_H
  43. #define LWIP_HDR_ALTCP_TLS_H
  44. #include "lwip/opt.h"
  45. #if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */
  46. #if LWIP_ALTCP_TLS
  47. #include "lwip/altcp.h"
  48. /* check if mbedtls port is enabled */
  49. #include "lwip/apps/altcp_tls_mbedtls_opts.h"
  50. /* allow session structure to be fully defined when using mbedtls port */
  51. #if LWIP_ALTCP_TLS_MBEDTLS
  52. #include "mbedtls/ssl.h"
  53. #endif
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /** @ingroup altcp_tls
  58. * ALTCP_TLS configuration handle, content depends on port (e.g. mbedtls)
  59. */
  60. struct altcp_tls_config;
  61. /** @ingroup altcp_tls
  62. * Create an ALTCP_TLS server configuration handle prepared for multiple certificates
  63. */
  64. struct altcp_tls_config *altcp_tls_create_config_server(u8_t cert_count);
  65. /** @ingroup altcp_tls
  66. * Add a certificate to an ALTCP_TLS server configuration handle
  67. */
  68. err_t altcp_tls_config_server_add_privkey_cert(struct altcp_tls_config *config,
  69. const u8_t *privkey, size_t privkey_len,
  70. const u8_t *privkey_pass, size_t privkey_pass_len,
  71. const u8_t *cert, size_t cert_len);
  72. /** @ingroup altcp_tls
  73. * Create an ALTCP_TLS server configuration handle with one certificate
  74. * (short version of calling @ref altcp_tls_create_config_server and
  75. * @ref altcp_tls_config_server_add_privkey_cert)
  76. */
  77. struct altcp_tls_config *altcp_tls_create_config_server_privkey_cert(const u8_t *privkey, size_t privkey_len,
  78. const u8_t *privkey_pass, size_t privkey_pass_len,
  79. const u8_t *cert, size_t cert_len);
  80. /** @ingroup altcp_tls
  81. * Create an ALTCP_TLS client configuration handle
  82. */
  83. struct altcp_tls_config *altcp_tls_create_config_client(const u8_t *cert, size_t cert_len);
  84. /** @ingroup altcp_tls
  85. * Create an ALTCP_TLS client configuration handle with two-way server/client authentication
  86. */
  87. struct altcp_tls_config *altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_t *privkey, size_t privkey_len,
  88. const u8_t *privkey_pass, size_t privkey_pass_len,
  89. const u8_t *cert, size_t cert_len);
  90. /** @ingroup altcp_tls
  91. * Configure ALPN TLS extension
  92. * Example:<br>
  93. * static const char *g_alpn_protocols[] = { "x-amzn-mqtt-ca", NULL };<br>
  94. * tls_config = altcp_tls_create_config_client(ca, ca_len);<br>
  95. * altcp_tls_conf_alpn_protocols(tls_config, g_alpn_protocols);<br>
  96. */
  97. int altcp_tls_configure_alpn_protocols(struct altcp_tls_config *conf, const char **protos);
  98. /** @ingroup altcp_tls
  99. * Free an ALTCP_TLS configuration handle
  100. */
  101. void altcp_tls_free_config(struct altcp_tls_config *conf);
  102. /** @ingroup altcp_tls
  103. * Free an ALTCP_TLS global entropy instance.
  104. * All ALTCP_TLS configuration are linked to one altcp_tls_entropy_rng structure
  105. * that handle an unique system entropy & ctr_drbg instance.
  106. * This function allow application to free this altcp_tls_entropy_rng structure
  107. * when all configuration referencing it were destroyed.
  108. * This function does nothing if some ALTCP_TLS configuration handle are still
  109. * active.
  110. */
  111. void altcp_tls_free_entropy(void);
  112. /** @ingroup altcp_tls
  113. * Create new ALTCP_TLS layer wrapping an existing pcb as inner connection (e.g. TLS over TCP)
  114. */
  115. struct altcp_pcb *altcp_tls_wrap(struct altcp_tls_config *config, struct altcp_pcb *inner_pcb);
  116. /** @ingroup altcp_tls
  117. * Create new ALTCP_TLS pcb and its inner tcp pcb
  118. */
  119. struct altcp_pcb *altcp_tls_new(struct altcp_tls_config *config, u8_t ip_type);
  120. /** @ingroup altcp_tls
  121. * Create new ALTCP_TLS layer pcb and its inner tcp pcb.
  122. * Same as @ref altcp_tls_new but this allocator function fits to
  123. * @ref altcp_allocator_t / @ref altcp_new.<br>
  124. 'arg' must contain a struct altcp_tls_config *.
  125. */
  126. struct altcp_pcb *altcp_tls_alloc(void *arg, u8_t ip_type);
  127. /** @ingroup altcp_tls
  128. * Return pointer to internal TLS context so application can tweak it.
  129. * Real type depends on port (e.g. mbedtls)
  130. */
  131. void *altcp_tls_context(struct altcp_pcb *conn);
  132. /** @ingroup altcp_tls
  133. * ALTCP_TLS session handle, content depends on port (e.g. mbedtls)
  134. */
  135. struct altcp_tls_session
  136. #if LWIP_ALTCP_TLS_MBEDTLS
  137. {
  138. mbedtls_ssl_session data;
  139. }
  140. #endif
  141. ;
  142. /** @ingroup altcp_tls
  143. * Initialise a TLS session buffer.
  144. * Real type depends on port (e.g. mbedtls use mbedtls_ssl_session)
  145. */
  146. void altcp_tls_init_session(struct altcp_tls_session *dest);
  147. /** @ingroup altcp_tls
  148. * Save current connected session to reuse it later. Should be called after altcp_connect() succeeded.
  149. * Return error if saving session fail.
  150. * Real type depends on port (e.g. mbedtls use mbedtls_ssl_session)
  151. */
  152. err_t altcp_tls_get_session(struct altcp_pcb *conn, struct altcp_tls_session *dest);
  153. /** @ingroup altcp_tls
  154. * Restore a previously saved session. Must be called before altcp_connect().
  155. * Return error if cannot restore session.
  156. * Real type depends on port (e.g. mbedtls use mbedtls_ssl_session)
  157. */
  158. err_t altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session *from);
  159. /** @ingroup altcp_tls
  160. * Free allocated data inside a TLS session buffer.
  161. * Real type depends on port (e.g. mbedtls use mbedtls_ssl_session)
  162. */
  163. void altcp_tls_free_session(struct altcp_tls_session *dest);
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif /* LWIP_ALTCP_TLS */
  168. #endif /* LWIP_ALTCP */
  169. #endif /* LWIP_HDR_ALTCP_TLS_H */