tls_config.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * tls_config.h
  3. *
  4. * TLS Configuration API for protocol stacks using TCP/IP
  5. *
  6. * Copyright 2017-2022 Michael Zillgith
  7. *
  8. * Abstraction layer for configuration of different TLS implementations
  9. *
  10. */
  11. #ifndef _TLS_CONFIG_H_
  12. #define _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. typedef enum {
  44. TLS_VERSION_NOT_SELECTED = 0,
  45. TLS_VERSION_SSL_3_0 = 3,
  46. TLS_VERSION_TLS_1_0 = 4,
  47. TLS_VERSION_TLS_1_1 = 5,
  48. TLS_VERSION_TLS_1_2 = 6,
  49. TLS_VERSION_TLS_1_3 = 7
  50. } TLSConfigVersion;
  51. /**
  52. * \brief Convert TLS version number to string
  53. *
  54. * \param version TLS version number
  55. *
  56. * \return the TLS version as null terminated string
  57. */
  58. PAL_API const char*
  59. TLSConfigVersion_toString(TLSConfigVersion version);
  60. typedef enum {
  61. TLS_SEC_EVT_INFO = 0,
  62. TLS_SEC_EVT_WARNING = 1,
  63. TLS_SEC_EVT_INCIDENT = 2
  64. } TLSEventLevel;
  65. #define TLS_EVENT_CODE_ALM_ALGO_NOT_SUPPORTED 1
  66. #define TLS_EVENT_CODE_ALM_UNSECURE_COMMUNICATION 2
  67. #define TLS_EVENT_CODE_ALM_CERT_UNAVAILABLE 3
  68. #define TLS_EVENT_CODE_ALM_BAD_CERT 4
  69. #define TLS_EVENT_CODE_ALM_CERT_SIZE_EXCEEDED 5
  70. #define TLS_EVENT_CODE_ALM_CERT_VALIDATION_FAILED 6
  71. #define TLS_EVENT_CODE_ALM_CERT_REQUIRED 7
  72. #define TLS_EVENT_CODE_ALM_HANDSHAKE_FAILED_UNKNOWN_REASON 8
  73. #define TLS_EVENT_CODE_WRN_INSECURE_TLS_VERSION 9
  74. #define TLS_EVENT_CODE_INF_SESSION_RENEGOTIATION 10
  75. #define TLS_EVENT_CODE_ALM_CERT_EXPIRED 11
  76. #define TLS_EVENT_CODE_ALM_CERT_REVOKED 12
  77. #define TLS_EVENT_CODE_ALM_CERT_NOT_CONFIGURED 13
  78. #define TLS_EVENT_CODE_ALM_CERT_NOT_TRUSTED 14
  79. #define TLS_EVENT_CODE_ALM_NO_CIPHER 15
  80. typedef struct sTLSConnection* TLSConnection;
  81. /**
  82. * \brief Get the peer address of the TLS connection
  83. *
  84. * \param self the TLS connection instance
  85. * \param peerAddrBuf user provided buffer that can hold at least 60 characters, or NULL to allow the function to allocate the memory for the buffer
  86. *
  87. * \returns peer address:port as null terminated string
  88. */
  89. PAL_API char*
  90. TLSConnection_getPeerAddress(TLSConnection self, char* peerAddrBuf);
  91. /**
  92. * \brief Get the TLS certificate used by the peer
  93. *
  94. * \param self the TLS connection instance
  95. * \param certSize[OUT] the certificate size in bytes
  96. *
  97. * \return address of the certificate buffer
  98. */
  99. PAL_API uint8_t*
  100. TLSConnection_getPeerCertificate(TLSConnection self, int* certSize);
  101. /**
  102. * \brief Get the TLS version used by the connection
  103. *
  104. * \param self the TLS connection instance
  105. *
  106. * \return TLS version
  107. */
  108. PAL_API TLSConfigVersion
  109. TLSConnection_getTLSVersion(TLSConnection self);
  110. typedef void (*TLSConfiguration_EventHandler)(void* parameter, TLSEventLevel eventLevel, int eventCode, const char* message, TLSConnection con);
  111. /**
  112. * \brief Set the security event handler
  113. *
  114. * \param handler the security event callback handler
  115. * \param parameter user provided parameter to be passed to the callback handler
  116. */
  117. PAL_API void
  118. TLSConfiguration_setEventHandler(TLSConfiguration self, TLSConfiguration_EventHandler handler, void* parameter);
  119. /**
  120. * \brief enable or disable TLS session resumption (default: enabled)
  121. *
  122. * NOTE: Depending on the used TLS version this is implemented by
  123. * session IDs or by session tickets.
  124. *
  125. * \param enable true to enable session resumption, false otherwise
  126. */
  127. PAL_API void
  128. TLSConfiguration_enableSessionResumption(TLSConfiguration self, bool enable);
  129. /**
  130. * \brief Set the maximum life time of a cached TLS session for session resumption in seconds
  131. *
  132. * \param intervalInSeconds the maximum lifetime of a cached TLS session
  133. */
  134. PAL_API void
  135. TLSConfiguration_setSessionResumptionInterval(TLSConfiguration self, int intervalInSeconds);
  136. /**
  137. * \brief Enables the validation of the certificate trust chain (enabled by default)
  138. *
  139. * \param value true to enable chain validation, false to disable
  140. */
  141. PAL_API void
  142. TLSConfiguration_setChainValidation(TLSConfiguration self, bool value);
  143. /**
  144. * \brief Set if only known certificates are accepted.
  145. *
  146. * If set to true only known certificates are accepted. Connections with unknown certificates
  147. * are rejected even if they are signed by a trusted authority.
  148. *
  149. * \param value true to enable setting, false otherwise
  150. */
  151. PAL_API void
  152. TLSConfiguration_setAllowOnlyKnownCertificates(TLSConfiguration self, bool value);
  153. /**
  154. * \brief Set own certificate (identity) from a byte buffer
  155. *
  156. * \param certificate the certificate buffer
  157. * \param certLen the lenght of the certificate
  158. *
  159. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  160. */
  161. PAL_API bool
  162. TLSConfiguration_setOwnCertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  163. /**
  164. * \brief Set own certificate (identity) from a certificate file
  165. *
  166. * \param filename of the certificate file
  167. *
  168. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  169. */
  170. PAL_API bool
  171. TLSConfiguration_setOwnCertificateFromFile(TLSConfiguration self, const char* filename);
  172. /**
  173. * \brief Set the own private key from a byte buffer
  174. *
  175. * \param key the private key to use
  176. * \param keyLen the length of the key
  177. * \param password the password of the key or null if the key is not password protected
  178. *
  179. * \return true, when the key was set, false otherwise (e.g. unknown key format)
  180. */
  181. PAL_API bool
  182. TLSConfiguration_setOwnKey(TLSConfiguration self, uint8_t* key, int keyLen, const char* keyPassword);
  183. /**
  184. * \brief Set the own private key from a key file
  185. *
  186. * \param filename filename/path of the key file
  187. * \param password the password of the key or null if the key is not password protected
  188. *
  189. * \return true, when the key was set, false otherwise (e.g. unknown key format)
  190. */
  191. PAL_API bool
  192. TLSConfiguration_setOwnKeyFromFile(TLSConfiguration self, const char* filename, const char* keyPassword);
  193. /**
  194. * Add a certificate to the list of allowed peer certificates from a byte buffer
  195. *
  196. * \param certificate the certificate buffer
  197. * \param certLen the length of the certificate buffer
  198. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  199. */
  200. PAL_API bool
  201. TLSConfiguration_addAllowedCertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  202. /**
  203. * \brief Add a certificate to the list of allowed peer certificates
  204. *
  205. * \param filename filename of the certificate file
  206. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  207. */
  208. PAL_API bool
  209. TLSConfiguration_addAllowedCertificateFromFile(TLSConfiguration self, const char* filename);
  210. /**
  211. * \brief Add a CA certificate used to validate peer certificates from a byte buffer
  212. *
  213. * \param certificate the certificate buffer
  214. * \param certLen the length of the certificate buffer
  215. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  216. */
  217. PAL_API bool
  218. TLSConfiguration_addCACertificate(TLSConfiguration self, uint8_t* certificate, int certLen);
  219. /**
  220. * \brief Add a CA certificate used to validate peer certificates from a file
  221. *
  222. * \param filename filename of the certificate file
  223. * \return true, when the certificate was set, false otherwise (e.g. unknown certificate format)
  224. */
  225. PAL_API bool
  226. TLSConfiguration_addCACertificateFromFile(TLSConfiguration self, const char* filename);
  227. /**
  228. * \brief Set the renegotiation timeout.
  229. *
  230. * After the timeout elapsed a TLS session renegotiation has to occur.
  231. *
  232. * \param timeInMs session renegotiation timeout in milliseconds
  233. */
  234. PAL_API void
  235. TLSConfiguration_setRenegotiationTime(TLSConfiguration self, int timeInMs);
  236. /**
  237. * \brief Set minimal allowed TLS version to use
  238. */
  239. PAL_API void
  240. TLSConfiguration_setMinTlsVersion(TLSConfiguration self, TLSConfigVersion version);
  241. /**
  242. * \brief Set maximal allowed TLS version to use
  243. */
  244. PAL_API void
  245. TLSConfiguration_setMaxTlsVersion(TLSConfiguration self, TLSConfigVersion version);
  246. /**
  247. * \brief Add a CRL (certificate revocation list) from buffer
  248. *
  249. * \param crl the buffer containing the CRL
  250. * \param crlLen the length of the CRL buffer
  251. * \return true, when the CRL was imported, false otherwise (e.g. unknown format)
  252. */
  253. PAL_API bool
  254. TLSConfiguration_addCRL(TLSConfiguration self, uint8_t* crl, int crlLen);
  255. /**
  256. * \brief Add a CRL (certificate revocation list) from a file
  257. *
  258. * \param filename filename of the CRL file
  259. * \return true, when the CRL was imported, false otherwise (e.g. unknown format)
  260. */
  261. PAL_API bool
  262. TLSConfiguration_addCRLFromFile(TLSConfiguration self, const char* filename);
  263. /**
  264. * \brief Removes any CRL (certificate revocation list) currently in use
  265. */
  266. PAL_API void
  267. TLSConfiguration_resetCRL(TLSConfiguration self);
  268. /**
  269. * Release all resource allocated by the TLSConfiguration instance
  270. *
  271. * NOTE: Do not use the object after calling this function!
  272. */
  273. PAL_API void
  274. TLSConfiguration_destroy(TLSConfiguration self);
  275. /** @} */
  276. /** @} */
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif /* SRC_TLS_CONFIG_H_ */