tls_config.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * tls_config.h
  3. *
  4. * TLS Configuration API for protocol stacks using TCP/IP
  5. *
  6. * Copyright 2017-2021 Michael Zillgith
  7. *
  8. * Abstraction layer for configuration of different TLS implementations
  9. *
  10. */
  11. #ifndef SRC_TLS_CONFIG_H_
  12. #define SRC_TLS_CONFIG_H_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #include "hal_base.h"
  17. /**
  18. * \file tls_config.h
  19. * \brief TLS API functions
  20. */
  21. /*! \addtogroup hal Platform (Hardware/OS) abstraction layer
  22. *
  23. * @{
  24. */
  25. /**
  26. * @defgroup TLS_CONFIG_API TLS configuration
  27. *
  28. * @{
  29. */
  30. typedef struct sTLSConfiguration* TLSConfiguration;
  31. /**
  32. * \brief Create a new \ref TLSConfiguration object to represent TLS configuration and certificates
  33. *
  34. * WARNING: Configuration cannot be changed after using for the first time.
  35. *
  36. * \return the new TLS configuration
  37. */
  38. PAL_API TLSConfiguration
  39. TLSConfiguration_create(void);
  40. /* will be called by stack automatically when appropriate */
  41. PAL_API void
  42. TLSConfiguration_setClientMode(TLSConfiguration self);
  43. /**
  44. * \brief Enables the validation of the certificate trust chain (enabled by default)
  45. *
  46. * \param value true to enable chain validation, false to disable
  47. */
  48. PAL_API void
  49. TLSConfiguration_setChainValidation(TLSConfiguration self, bool value);
  50. /**
  51. * \brief Set if only known certificates are accepted.
  52. *
  53. * If set to true only known certificates are accepted. Connections with unknown certificates
  54. * are rejected even if they are signed by a trusted authority.
  55. *
  56. * \param value true to enable setting, false otherwise
  57. */
  58. PAL_API void
  59. TLSConfiguration_setAllowOnlyKnownCertificates(TLSConfiguration self, bool value);
  60. /**
  61. * \brief Set own certificate (identity) from a byte buffer
  62. *
  63. * \param certificate the certificate buffer
  64. * \param certLen the lenght of the certificate
  65. *
  66. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  67. */
  68. PAL_API bool
  69. TLSConfiguration_setOwnCertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  70. /**
  71. * \brief Set own certificate (identity) from a certificate file
  72. *
  73. * \param filename of the certificate file
  74. *
  75. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  76. */
  77. PAL_API bool
  78. TLSConfiguration_setOwnCertificateFromFile(TLSConfiguration self, const char* filename);
  79. /**
  80. * \brief Set the own private key from a byte buffer
  81. *
  82. * \param key the private key to use
  83. * \param keyLen the length of the key
  84. * \param password the password of the key or null if the key is not password protected
  85. *
  86. * \return true, when the key was set, false otherwise (e.g. unknown key format)
  87. */
  88. PAL_API bool
  89. TLSConfiguration_setOwnKey(TLSConfiguration self, uint8_t* key, int keyLen, const char* keyPassword);
  90. /**
  91. * \brief Set the own private key from a key file
  92. *
  93. * \param filename filename/path of the key file
  94. * \param password the password of the key or null if the key is not password protected
  95. *
  96. * \return true, when the key was set, false otherwise (e.g. unknown key format)
  97. */
  98. PAL_API bool
  99. TLSConfiguration_setOwnKeyFromFile(TLSConfiguration self, const char* filename, const char* keyPassword);
  100. /**
  101. * Add a certificate to the list of allowed peer certificates from a byte buffer
  102. *
  103. * \param certificate the certificate buffer
  104. * \param certLen the length of the certificate buffer
  105. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  106. */
  107. PAL_API bool
  108. TLSConfiguration_addAllowedCertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  109. /**
  110. * \brief Add a certificate to the list of allowed peer certificates
  111. *
  112. * \param filename filename of the certificate file
  113. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  114. */
  115. PAL_API bool
  116. TLSConfiguration_addAllowedCertificateFromFile(TLSConfiguration self, const char* filename);
  117. /**
  118. * \brief Add a CA certificate used to validate peer certificates from a byte buffer
  119. *
  120. * \param certificate the certificate buffer
  121. * \param certLen the length of the certificate buffer
  122. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  123. */
  124. PAL_API bool
  125. TLSConfiguration_addCACertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  126. /**
  127. * \brief Add a CA certificate used to validate peer certificates from a file
  128. *
  129. * \param filename filename of the certificate file
  130. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  131. */
  132. PAL_API bool
  133. TLSConfiguration_addCACertificateFromFile(TLSConfiguration self, const char* filename);
  134. /**
  135. * \brief Set the renegotiation timeout.
  136. *
  137. * After the timeout elapsed a TLS session renegotiation has to occur.
  138. *
  139. * \param timeInMs session renegotiation timeout in milliseconds
  140. */
  141. PAL_API void
  142. TLSConfiguration_setRenegotiationTime(TLSConfiguration self, int timeInMs);
  143. typedef enum {
  144. TLS_VERSION_NOT_SELECTED = 0,
  145. TLS_VERSION_SSL_3_0 = 3,
  146. TLS_VERSION_TLS_1_0 = 4,
  147. TLS_VERSION_TLS_1_1 = 5,
  148. TLS_VERSION_TLS_1_2 = 6,
  149. TLS_VERSION_TLS_1_3 = 7
  150. } TLSConfigVersion;
  151. /**
  152. * \brief Set minimal allowed TLS version to use
  153. */
  154. PAL_API void
  155. TLSConfiguration_setMinTlsVersion(TLSConfiguration self, TLSConfigVersion version);
  156. /**
  157. * \brief Set maximal allowed TLS version to use
  158. */
  159. PAL_API void
  160. TLSConfiguration_setMaxTlsVersion(TLSConfiguration self, TLSConfigVersion version);
  161. /**
  162. * \brief Add a CRL (certificate revocation list) from buffer
  163. *
  164. * \param crl the buffer containing the CRL
  165. * \param crlLen the length of the CRL buffer
  166. * \return true, when the CRL was imported, false otherwise (e.g. unknown format)
  167. */
  168. PAL_API bool
  169. TLSConfiguration_addCRL(TLSConfiguration self, uint8_t* crl, int crlLen);
  170. /**
  171. * \brief Add a CRL (certificate revocation list) from a file
  172. *
  173. * \param filename filename of the CRL file
  174. * \return true, when the CRL was imported, false otherwise (e.g. unknown format)
  175. */
  176. PAL_API bool
  177. TLSConfiguration_addCRLFromFile(TLSConfiguration self, const char* filename);
  178. /**
  179. * Release all resource allocated by the TLSConfiguration instance
  180. *
  181. * NOTE: Do not use the object after calling this function!
  182. */
  183. PAL_API void
  184. TLSConfiguration_destroy(TLSConfiguration self);
  185. /** @} */
  186. /** @} */
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif /* SRC_TLS_CONFIG_H_ */