esp_tls_mbedtls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netdb.h>
  20. #include <http_parser.h>
  21. #include "esp_tls_mbedtls.h"
  22. #include "esp_tls_error_capture_internal.h"
  23. #include <errno.h>
  24. #include "esp_log.h"
  25. #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
  26. #include "esp_crt_bundle.h"
  27. #endif
  28. #ifdef CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  29. #define ATECC608A_TNG_SLAVE_ADDR 0x6A
  30. #define ATECC608A_TFLEX_SLAVE_ADDR 0x6C
  31. #define ATECC608A_TCUSTOM_SLAVE_ADDR 0xC0
  32. /* cryptoauthlib includes */
  33. #include "mbedtls/atca_mbedtls_wrap.h"
  34. #include "tng_atca.h"
  35. #include "cryptoauthlib.h"
  36. static const atcacert_def_t* cert_def = NULL;
  37. /* Prototypes for functions */
  38. static esp_err_t esp_set_atecc608a_pki_context(esp_tls_t *tls, esp_tls_cfg_t *cfg);
  39. #endif /* CONFIG_ESP_TLS_USE_SECURE_ELEMENT */
  40. static const char *TAG = "esp-tls-mbedtls";
  41. static mbedtls_x509_crt *global_cacert = NULL;
  42. typedef struct esp_tls_pki_t {
  43. mbedtls_x509_crt *public_cert;
  44. mbedtls_pk_context *pk_key;
  45. const unsigned char *publiccert_pem_buf;
  46. unsigned int publiccert_pem_bytes;
  47. const unsigned char *privkey_pem_buf;
  48. unsigned int privkey_pem_bytes;
  49. const unsigned char *privkey_password;
  50. unsigned int privkey_password_len;
  51. } esp_tls_pki_t;
  52. esp_err_t esp_create_mbedtls_handle(const char *hostname, size_t hostlen, const void *cfg, esp_tls_t *tls)
  53. {
  54. assert(cfg != NULL);
  55. assert(tls != NULL);
  56. int ret;
  57. esp_err_t esp_ret = ESP_FAIL;
  58. tls->server_fd.fd = tls->sockfd;
  59. mbedtls_ssl_init(&tls->ssl);
  60. mbedtls_ctr_drbg_init(&tls->ctr_drbg);
  61. mbedtls_ssl_config_init(&tls->conf);
  62. mbedtls_entropy_init(&tls->entropy);
  63. if (tls->role == ESP_TLS_CLIENT) {
  64. esp_ret = set_client_config(hostname, hostlen, (esp_tls_cfg_t *)cfg, tls);
  65. if (esp_ret != ESP_OK) {
  66. ESP_LOGE(TAG, "Failed to set client configurations");
  67. goto exit;
  68. }
  69. } else if (tls->role == ESP_TLS_SERVER) {
  70. #ifdef CONFIG_ESP_TLS_SERVER
  71. esp_ret = set_server_config((esp_tls_cfg_server_t *) cfg, tls);
  72. if (esp_ret != 0) {
  73. ESP_LOGE(TAG, "Failed to set server configurations");
  74. goto exit;
  75. }
  76. #else
  77. ESP_LOGE(TAG, "ESP_TLS_SERVER Not enabled in Kconfig");
  78. goto exit;
  79. #endif
  80. }
  81. if ((ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg,
  82. mbedtls_entropy_func, &tls->entropy, NULL, 0)) != 0) {
  83. ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned -0x%x", -ret);
  84. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  85. esp_ret = ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED;
  86. goto exit;
  87. }
  88. mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg);
  89. #ifdef CONFIG_MBEDTLS_DEBUG
  90. mbedtls_esp_enable_debug_log(&tls->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
  91. #endif
  92. if ((ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
  93. ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x", -ret);
  94. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  95. esp_ret = ESP_ERR_MBEDTLS_SSL_SETUP_FAILED;
  96. goto exit;
  97. }
  98. mbedtls_ssl_set_bio(&tls->ssl, &tls->server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  99. return ESP_OK;
  100. exit:
  101. esp_mbedtls_cleanup(tls);
  102. return esp_ret;
  103. }
  104. int esp_mbedtls_handshake(esp_tls_t *tls, const esp_tls_cfg_t *cfg)
  105. {
  106. int ret;
  107. ret = mbedtls_ssl_handshake(&tls->ssl);
  108. if (ret == 0) {
  109. tls->conn_state = ESP_TLS_DONE;
  110. return 1;
  111. } else {
  112. if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
  113. ESP_LOGE(TAG, "mbedtls_ssl_handshake returned -0x%x", -ret);
  114. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  115. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED);
  116. if (cfg->cacert_buf != NULL || cfg->use_global_ca_store == true) {
  117. /* This is to check whether handshake failed due to invalid certificate*/
  118. esp_mbedtls_verify_certificate(tls);
  119. }
  120. tls->conn_state = ESP_TLS_FAIL;
  121. return -1;
  122. }
  123. /* Irrespective of blocking or non-blocking I/O, we return on getting ESP_TLS_ERR_SSL_WANT_READ
  124. or ESP_TLS_ERR_SSL_WANT_WRITE during handshake */
  125. return 0;
  126. }
  127. }
  128. ssize_t esp_mbedtls_read(esp_tls_t *tls, char *data, size_t datalen)
  129. {
  130. ssize_t ret = mbedtls_ssl_read(&tls->ssl, (unsigned char *)data, datalen);
  131. if (ret < 0) {
  132. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  133. return 0;
  134. }
  135. if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
  136. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  137. ESP_LOGE(TAG, "read error :%d:", ret);
  138. }
  139. }
  140. return ret;
  141. }
  142. ssize_t esp_mbedtls_write(esp_tls_t *tls, const char *data, size_t datalen)
  143. {
  144. size_t written = 0;
  145. size_t write_len = datalen;
  146. while (written < datalen) {
  147. if (write_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  148. write_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
  149. }
  150. if (datalen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  151. ESP_LOGD(TAG, "Fragmenting data of excessive size :%d, offset: %d, size %d", datalen, written, write_len);
  152. }
  153. ssize_t ret = mbedtls_ssl_write(&tls->ssl, (unsigned char*) data + written, write_len);
  154. if (ret <= 0) {
  155. if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE && ret != 0) {
  156. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  157. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_MBEDTLS_SSL_WRITE_FAILED);
  158. ESP_LOGE(TAG, "write error :%d:", ret);
  159. return ret;
  160. } else {
  161. // Exitting the tls-write process as less than desired datalen are writable
  162. ESP_LOGD(TAG, "mbedtls_ssl_write() returned %d, already written %d, exitting...", ret, written);
  163. return written;
  164. }
  165. }
  166. written += ret;
  167. write_len = datalen - written;
  168. }
  169. return written;
  170. }
  171. void esp_mbedtls_conn_delete(esp_tls_t *tls)
  172. {
  173. if (tls != NULL) {
  174. esp_mbedtls_cleanup(tls);
  175. if (tls->is_tls) {
  176. mbedtls_net_free(&tls->server_fd);
  177. tls->sockfd = tls->server_fd.fd;
  178. }
  179. }
  180. }
  181. void esp_mbedtls_verify_certificate(esp_tls_t *tls)
  182. {
  183. int flags;
  184. char buf[100];
  185. if ((flags = mbedtls_ssl_get_verify_result(&tls->ssl)) != 0) {
  186. ESP_LOGI(TAG, "Failed to verify peer certificate!");
  187. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS_CERT_FLAGS, flags);
  188. bzero(buf, sizeof(buf));
  189. mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags);
  190. ESP_LOGI(TAG, "verification info: %s", buf);
  191. } else {
  192. ESP_LOGI(TAG, "Certificate verified.");
  193. }
  194. }
  195. ssize_t esp_mbedtls_get_bytes_avail(esp_tls_t *tls)
  196. {
  197. if (!tls) {
  198. ESP_LOGE(TAG, "empty arg passed to esp_tls_get_bytes_avail()");
  199. return ESP_FAIL;
  200. }
  201. return mbedtls_ssl_get_bytes_avail(&tls->ssl);
  202. }
  203. void esp_mbedtls_cleanup(esp_tls_t *tls)
  204. {
  205. if (!tls) {
  206. return;
  207. }
  208. if (tls->cacert_ptr != global_cacert) {
  209. mbedtls_x509_crt_free(tls->cacert_ptr);
  210. }
  211. tls->cacert_ptr = NULL;
  212. #ifdef CONFIG_ESP_TLS_SERVER
  213. mbedtls_x509_crt_free(&tls->servercert);
  214. mbedtls_pk_free(&tls->serverkey);
  215. #endif
  216. mbedtls_x509_crt_free(&tls->cacert);
  217. mbedtls_x509_crt_free(&tls->clientcert);
  218. mbedtls_pk_free(&tls->clientkey);
  219. mbedtls_entropy_free(&tls->entropy);
  220. mbedtls_ssl_config_free(&tls->conf);
  221. mbedtls_ctr_drbg_free(&tls->ctr_drbg);
  222. mbedtls_ssl_free(&tls->ssl);
  223. #ifdef CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  224. atcab_release();
  225. #endif
  226. }
  227. static esp_err_t set_ca_cert(esp_tls_t *tls, const unsigned char *cacert, size_t cacert_len)
  228. {
  229. assert(tls);
  230. tls->cacert_ptr = &tls->cacert;
  231. mbedtls_x509_crt_init(tls->cacert_ptr);
  232. int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len);
  233. if (ret < 0) {
  234. ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
  235. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  236. return ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED;
  237. }
  238. if (ret > 0) {
  239. /* This will happen if the CA chain contains one or more invalid certs, going ahead as the hadshake
  240. * may still succeed if the other certificates in the CA chain are enough for the authentication */
  241. ESP_LOGW(TAG, "mbedtls_x509_crt_parse was partly successful. No. of failed certificates: %d", ret);
  242. }
  243. mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
  244. mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
  245. return ESP_OK;
  246. }
  247. static esp_err_t set_pki_context(esp_tls_t *tls, const esp_tls_pki_t *pki)
  248. {
  249. assert(tls);
  250. assert(pki);
  251. int ret;
  252. if (pki->publiccert_pem_buf != NULL &&
  253. pki->privkey_pem_buf != NULL &&
  254. pki->public_cert != NULL &&
  255. pki->pk_key != NULL) {
  256. mbedtls_x509_crt_init(pki->public_cert);
  257. mbedtls_pk_init(pki->pk_key);
  258. ret = mbedtls_x509_crt_parse(pki->public_cert, pki->publiccert_pem_buf, pki->publiccert_pem_bytes);
  259. if (ret < 0) {
  260. ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
  261. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  262. return ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED;
  263. }
  264. ret = mbedtls_pk_parse_key(pki->pk_key, pki->privkey_pem_buf, pki->privkey_pem_bytes,
  265. pki->privkey_password, pki->privkey_password_len);
  266. if (ret < 0) {
  267. ESP_LOGE(TAG, "mbedtls_pk_parse_keyfile returned -0x%x", -ret);
  268. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  269. return ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED;
  270. }
  271. ret = mbedtls_ssl_conf_own_cert(&tls->conf, pki->public_cert, pki->pk_key);
  272. if (ret < 0) {
  273. ESP_LOGE(TAG, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret);
  274. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  275. return ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED;
  276. }
  277. } else {
  278. return ESP_ERR_INVALID_ARG;
  279. }
  280. return ESP_OK;
  281. }
  282. static esp_err_t set_global_ca_store(esp_tls_t *tls)
  283. {
  284. assert(tls);
  285. if (global_cacert == NULL) {
  286. ESP_LOGE(TAG, "global_cacert is NULL");
  287. return ESP_ERR_INVALID_STATE;
  288. }
  289. tls->cacert_ptr = global_cacert;
  290. mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
  291. mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
  292. return ESP_OK;
  293. }
  294. #ifdef CONFIG_ESP_TLS_SERVER
  295. esp_err_t set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
  296. {
  297. assert(cfg != NULL);
  298. assert(tls != NULL);
  299. int ret;
  300. esp_err_t esp_ret;
  301. if ((ret = mbedtls_ssl_config_defaults(&tls->conf,
  302. MBEDTLS_SSL_IS_SERVER,
  303. MBEDTLS_SSL_TRANSPORT_STREAM,
  304. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  305. ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret);
  306. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  307. return ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED;
  308. }
  309. #ifdef CONFIG_MBEDTLS_SSL_ALPN
  310. if (cfg->alpn_protos) {
  311. mbedtls_ssl_conf_alpn_protocols(&tls->conf, cfg->alpn_protos);
  312. }
  313. #endif
  314. if (cfg->cacert_buf != NULL) {
  315. esp_ret = set_ca_cert(tls, cfg->cacert_buf, cfg->cacert_bytes);
  316. if (esp_ret != ESP_OK) {
  317. return esp_ret;
  318. }
  319. } else {
  320. mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
  321. }
  322. if (cfg->servercert_buf != NULL && cfg->serverkey_buf != NULL) {
  323. esp_tls_pki_t pki = {
  324. .public_cert = &tls->servercert,
  325. .pk_key = &tls->serverkey,
  326. .publiccert_pem_buf = cfg->servercert_buf,
  327. .publiccert_pem_bytes = cfg->servercert_bytes,
  328. .privkey_pem_buf = cfg->serverkey_buf,
  329. .privkey_pem_bytes = cfg->serverkey_bytes,
  330. .privkey_password = cfg->serverkey_password,
  331. .privkey_password_len = cfg->serverkey_password_len,
  332. };
  333. esp_ret = set_pki_context(tls, &pki);
  334. if (esp_ret != ESP_OK) {
  335. ESP_LOGE(TAG, "Failed to set server pki context");
  336. return esp_ret;
  337. }
  338. } else {
  339. ESP_LOGE(TAG, "Missing server certificate and/or key");
  340. return ESP_ERR_INVALID_STATE;
  341. }
  342. return ESP_OK;
  343. }
  344. #endif /* ! CONFIG_ESP_TLS_SERVER */
  345. esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t *cfg, esp_tls_t *tls)
  346. {
  347. assert(cfg != NULL);
  348. assert(tls != NULL);
  349. int ret;
  350. if (!cfg->skip_common_name) {
  351. char *use_host = NULL;
  352. if (cfg->common_name != NULL) {
  353. use_host = strndup(cfg->common_name, strlen(cfg->common_name));
  354. } else {
  355. use_host = strndup(hostname, hostlen);
  356. }
  357. if (use_host == NULL) {
  358. return ESP_ERR_NO_MEM;
  359. }
  360. /* Hostname set here should match CN in server certificate */
  361. if ((ret = mbedtls_ssl_set_hostname(&tls->ssl, use_host)) != 0) {
  362. ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret);
  363. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  364. free(use_host);
  365. return ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED;
  366. }
  367. free(use_host);
  368. }
  369. if ((ret = mbedtls_ssl_config_defaults(&tls->conf,
  370. MBEDTLS_SSL_IS_CLIENT,
  371. MBEDTLS_SSL_TRANSPORT_STREAM,
  372. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  373. ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned -0x%x", -ret);
  374. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  375. return ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED;
  376. }
  377. #ifdef CONFIG_MBEDTLS_SSL_RENEGOTIATION
  378. mbedtls_ssl_conf_renegotiation(&tls->conf, MBEDTLS_SSL_RENEGOTIATION_ENABLED);
  379. #endif
  380. if (cfg->alpn_protos) {
  381. #ifdef CONFIG_MBEDTLS_SSL_ALPN
  382. if ((ret = mbedtls_ssl_conf_alpn_protocols(&tls->conf, cfg->alpn_protos)) != 0) {
  383. ESP_LOGE(TAG, "mbedtls_ssl_conf_alpn_protocols returned -0x%x", -ret);
  384. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  385. return ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED;
  386. }
  387. #else
  388. ESP_LOGE(TAG, "alpn_protos configured but not enabled in menuconfig: Please enable MBEDTLS_SSL_ALPN option");
  389. return ESP_ERR_INVALID_STATE;
  390. #endif
  391. }
  392. if (cfg->crt_bundle_attach != NULL) {
  393. #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
  394. ESP_LOGD(TAG, "Use certificate bundle");
  395. mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
  396. cfg->crt_bundle_attach(&tls->conf);
  397. #else //CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
  398. ESP_LOGE(TAG, "use_crt_bundle configured but not enabled in menuconfig: Please enable MBEDTLS_CERTIFICATE_BUNDLE option");
  399. return ESP_ERR_INVALID_STATE;
  400. #endif
  401. } else if (cfg->use_global_ca_store == true) {
  402. esp_err_t esp_ret = set_global_ca_store(tls);
  403. if (esp_ret != ESP_OK) {
  404. return esp_ret;
  405. }
  406. } else if (cfg->cacert_buf != NULL) {
  407. esp_err_t esp_ret = set_ca_cert(tls, cfg->cacert_buf, cfg->cacert_bytes);
  408. if (esp_ret != ESP_OK) {
  409. return esp_ret;
  410. }
  411. mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
  412. } else if (cfg->psk_hint_key) {
  413. #if defined(CONFIG_ESP_TLS_PSK_VERIFICATION)
  414. //
  415. // PSK encryption mode is configured only if no certificate supplied and psk pointer not null
  416. ESP_LOGD(TAG, "ssl psk authentication");
  417. ret = mbedtls_ssl_conf_psk(&tls->conf, cfg->psk_hint_key->key, cfg->psk_hint_key->key_size,
  418. (const unsigned char *)cfg->psk_hint_key->hint, strlen(cfg->psk_hint_key->hint));
  419. if (ret != 0) {
  420. ESP_LOGE(TAG, "mbedtls_ssl_conf_psk returned -0x%x", -ret);
  421. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  422. return ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED;
  423. }
  424. #else
  425. ESP_LOGE(TAG, "psk_hint_key configured but not enabled in menuconfig: Please enable ESP_TLS_PSK_VERIFICATION option");
  426. return ESP_ERR_INVALID_STATE;
  427. #endif
  428. } else {
  429. mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
  430. }
  431. if (cfg->use_secure_element) {
  432. #ifdef CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  433. ret = esp_set_atecc608a_pki_context(tls, (esp_tls_cfg_t *)cfg);
  434. if (ret != ESP_OK) {
  435. return ret;
  436. }
  437. #else
  438. ESP_LOGE(TAG, "Please enable secure element support for ESP-TLS in menuconfig");
  439. return ESP_FAIL;
  440. #endif /* CONFIG_ESP_TLS_USE_SECURE_ELEMENT */
  441. } else if (cfg->clientcert_pem_buf != NULL && cfg->clientkey_pem_buf != NULL) {
  442. esp_tls_pki_t pki = {
  443. .public_cert = &tls->clientcert,
  444. .pk_key = &tls->clientkey,
  445. .publiccert_pem_buf = cfg->clientcert_buf,
  446. .publiccert_pem_bytes = cfg->clientcert_bytes,
  447. .privkey_pem_buf = cfg->clientkey_buf,
  448. .privkey_pem_bytes = cfg->clientkey_bytes,
  449. .privkey_password = cfg->clientkey_password,
  450. .privkey_password_len = cfg->clientkey_password_len,
  451. };
  452. esp_err_t esp_ret = set_pki_context(tls, &pki);
  453. if (esp_ret != ESP_OK) {
  454. ESP_LOGE(TAG, "Failed to set client pki context");
  455. return esp_ret;
  456. }
  457. } else if (cfg->clientcert_buf != NULL || cfg->clientkey_buf != NULL) {
  458. ESP_LOGE(TAG, "You have to provide both clientcert_buf and clientkey_buf for mutual authentication");
  459. return ESP_ERR_INVALID_STATE;
  460. }
  461. return ESP_OK;
  462. }
  463. #ifdef CONFIG_ESP_TLS_SERVER
  464. /**
  465. * @brief Create TLS/SSL server session
  466. */
  467. int esp_mbedtls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp_tls_t *tls)
  468. {
  469. if (tls == NULL || cfg == NULL) {
  470. return -1;
  471. }
  472. tls->role = ESP_TLS_SERVER;
  473. tls->sockfd = sockfd;
  474. esp_err_t esp_ret = esp_create_mbedtls_handle(NULL, 0, cfg, tls);
  475. if (esp_ret != ESP_OK) {
  476. ESP_LOGE(TAG, "create_ssl_handle failed");
  477. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, esp_ret);
  478. tls->conn_state = ESP_TLS_FAIL;
  479. return -1;
  480. }
  481. tls->read = esp_mbedtls_read;
  482. tls->write = esp_mbedtls_write;
  483. int ret;
  484. while ((ret = mbedtls_ssl_handshake(&tls->ssl)) != 0) {
  485. if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
  486. ESP_LOGE(TAG, "mbedtls_ssl_handshake returned %d", ret);
  487. tls->conn_state = ESP_TLS_FAIL;
  488. return ret;
  489. }
  490. }
  491. return 0;
  492. }
  493. /**
  494. * @brief Close the server side TLS/SSL connection and free any allocated resources.
  495. */
  496. void esp_mbedtls_server_session_delete(esp_tls_t *tls)
  497. {
  498. if (tls != NULL) {
  499. esp_mbedtls_cleanup(tls);
  500. free(tls);
  501. }
  502. };
  503. #endif /* ! CONFIG_ESP_TLS_SERVER */
  504. esp_err_t esp_mbedtls_init_global_ca_store(void)
  505. {
  506. if (global_cacert == NULL) {
  507. global_cacert = (mbedtls_x509_crt *)calloc(1, sizeof(mbedtls_x509_crt));
  508. if (global_cacert == NULL) {
  509. ESP_LOGE(TAG, "global_cacert not allocated");
  510. return ESP_ERR_NO_MEM;
  511. }
  512. mbedtls_x509_crt_init(global_cacert);
  513. }
  514. return ESP_OK;
  515. }
  516. esp_err_t esp_mbedtls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes)
  517. {
  518. #ifdef CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT
  519. ESP_LOGE(TAG, "Please disable dynamic freeing of ca cert in mbedtls (CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT)\n in order to use the global ca_store");
  520. return ESP_FAIL;
  521. #endif
  522. if (cacert_pem_buf == NULL) {
  523. ESP_LOGE(TAG, "cacert_pem_buf is null");
  524. return ESP_ERR_INVALID_ARG;
  525. }
  526. int ret;
  527. if (global_cacert == NULL) {
  528. ret = esp_mbedtls_init_global_ca_store();
  529. if (ret != ESP_OK) {
  530. return ret;
  531. }
  532. }
  533. ret = mbedtls_x509_crt_parse(global_cacert, cacert_pem_buf, cacert_pem_bytes);
  534. if (ret < 0) {
  535. ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
  536. mbedtls_x509_crt_free(global_cacert);
  537. free(global_cacert);
  538. global_cacert = NULL;
  539. return ESP_FAIL;
  540. } else if (ret > 0) {
  541. ESP_LOGE(TAG, "mbedtls_x509_crt_parse was partly successful. No. of failed certificates: %d", ret);
  542. return ESP_ERR_MBEDTLS_CERT_PARTLY_OK;
  543. }
  544. return ESP_OK;
  545. }
  546. mbedtls_x509_crt *esp_mbedtls_get_global_ca_store(void)
  547. {
  548. return global_cacert;
  549. }
  550. void esp_mbedtls_free_global_ca_store(void)
  551. {
  552. if (global_cacert) {
  553. mbedtls_x509_crt_free(global_cacert);
  554. free(global_cacert);
  555. global_cacert = NULL;
  556. }
  557. }
  558. #ifdef CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  559. static esp_err_t esp_init_atecc608a(uint8_t slave_addr)
  560. {
  561. cfg_ateccx08a_i2c_default.atcai2c.slave_address = slave_addr;
  562. int ret = atcab_init(&cfg_ateccx08a_i2c_default);
  563. if(ret != 0) {
  564. ESP_LOGE(TAG, "Failed\n !atcab_init returned %02x", ret);
  565. return ESP_FAIL;
  566. }
  567. return ESP_OK;
  568. }
  569. static esp_err_t esp_set_atecc608a_pki_context(esp_tls_t *tls, esp_tls_cfg_t *cfg)
  570. {
  571. int ret = 0;
  572. int esp_ret = ESP_FAIL;
  573. ESP_LOGI(TAG, "Initialize the ATECC interface...");
  574. #if defined(CONFIG_ATECC608A_TNG) || defined(CONFIG_ATECC608A_TFLEX)
  575. #ifdef CONFIG_ATECC608A_TNG
  576. esp_ret = esp_init_atecc608a(ATECC608A_TNG_SLAVE_ADDR);
  577. if (ret != ESP_OK) {
  578. return ESP_ERR_ESP_TLS_SE_FAILED;
  579. }
  580. #elif CONFIG_ATECC608A_TFLEX /* CONFIG_ATECC608A_TNG */
  581. esp_ret = esp_init_atecc608a(ATECC608A_TFLEX_SLAVE_ADDR);
  582. if (ret != ESP_OK) {
  583. return ESP_ERR_ESP_TLS_SE_FAILED;
  584. }
  585. #endif /* CONFIG_ATECC608A_TFLEX */
  586. mbedtls_x509_crt_init(&tls->clientcert);
  587. ret = tng_get_device_cert_def(&cert_def);
  588. if (ret != 0) {
  589. ESP_LOGE(TAG, "Failed to get device cert def");
  590. return ESP_ERR_ESP_TLS_SE_FAILED;
  591. }
  592. /* Extract the device certificate and convert to mbedtls cert */
  593. ret = atca_mbedtls_cert_add(&tls->clientcert, cert_def);
  594. if (ret != 0) {
  595. ESP_LOGE(TAG, "Failed to parse cert from device, return %02x", ret);
  596. return ESP_ERR_ESP_TLS_SE_FAILED;
  597. }
  598. #elif CONFIG_ATECC608A_TCUSTOM
  599. esp_ret = esp_init_atecc608a(ATECC608A_TCUSTOM_SLAVE_ADDR);
  600. if (ret != ESP_OK) {
  601. return ESP_ERR_ESP_TLS_SE_FAILED;
  602. }
  603. mbedtls_x509_crt_init(&tls->clientcert);
  604. if(cfg->clientcert_buf != NULL) {
  605. ret = mbedtls_x509_crt_parse(&tls->clientcert, (const unsigned char*)cfg->clientcert_buf, cfg->clientcert_bytes);
  606. if (ret < 0) {
  607. ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
  608. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  609. return ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED;
  610. }
  611. } else {
  612. ESP_LOGE(TAG, "Device certificate must be provided for TrustCustom Certs");
  613. return ESP_FAIL;
  614. }
  615. #endif /* CONFIG_ATECC608A_TCUSTOM */
  616. ret = atca_mbedtls_pk_init(&tls->clientkey, 0);
  617. if (ret != 0) {
  618. ESP_LOGE(TAG, "Failed to parse key from device");
  619. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  620. return ESP_ERR_ESP_TLS_SE_FAILED;
  621. }
  622. ret = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->clientcert, &tls->clientkey);
  623. if (ret != 0) {
  624. ESP_LOGE(TAG, "Failed\n ! mbedtls_ssl_conf_own_cert returned -0x%x", ret);
  625. ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
  626. return ESP_ERR_ESP_TLS_SE_FAILED;
  627. }
  628. return ESP_OK;
  629. }
  630. #endif /* CONFIG_ESP_TLS_USE_SECURE_ELEMENT */