tls_config.h 9.9 KB

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