1 /*
2  * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _DYNAMIC_IMPL_H_
8 #define _DYNAMIC_IMPL_H_
9 
10 #include <stddef.h>
11 #include <string.h>
12 #include "mbedtls/ssl.h"
13 #include "mbedtls/ssl_internal.h"
14 #include "mbedtls/platform.h"
15 #include "esp_log.h"
16 
17 #define TRACE_CHECK(_fn, _state) \
18 ({ \
19     ESP_LOGV(TAG, "%d " _state " to do \"%s\"", __LINE__, # _fn); \
20 })
21 
22 #define CHECK_OK(_fn) \
23 ({ \
24     int _ret; \
25  \
26     TRACE_CHECK(_fn, "state"); \
27  \
28     if ((_ret = _fn) != 0) { \
29         ESP_LOGV(TAG, "\"%s\" result is %d", # _fn, -_ret); \
30         TRACE_CHECK(_fn, "fail"); \
31         return _ret; \
32     } \
33  \
34     TRACE_CHECK(_fn, "end"); \
35  \
36 })
37 
38 typedef enum {
39     ESP_MBEDTLS_SSL_BUF_CACHED,
40     ESP_MBEDTLS_SSL_BUF_NO_CACHED,
41 } esp_mbedtls_ssl_buf_states;
42 
43 struct esp_mbedtls_ssl_buf {
44     esp_mbedtls_ssl_buf_states state;
45     unsigned int len;
46     unsigned char buf[];
47 };
48 
49 #define SSL_BUF_HEAD_OFFSET_SIZE ((int)offsetof(struct esp_mbedtls_ssl_buf, buf))
50 
51 void esp_mbedtls_free_buf(unsigned char *buf);
52 
53 int esp_mbedtls_setup_tx_buffer(mbedtls_ssl_context *ssl);
54 
55 void esp_mbedtls_setup_rx_buffer(mbedtls_ssl_context *ssl);
56 
57 int esp_mbedtls_reset_add_tx_buffer(mbedtls_ssl_context *ssl);
58 
59 int esp_mbedtls_reset_add_rx_buffer(mbedtls_ssl_context *ssl);
60 
61 int esp_mbedtls_reset_free_tx_buffer(mbedtls_ssl_context *ssl);
62 
63 void esp_mbedtls_reset_free_rx_buffer(mbedtls_ssl_context *ssl);
64 
65 int esp_mbedtls_add_tx_buffer(mbedtls_ssl_context *ssl, size_t buffer_len);
66 
67 int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl);
68 
69 int esp_mbedtls_free_tx_buffer(mbedtls_ssl_context *ssl);
70 
71 int esp_mbedtls_free_rx_buffer(mbedtls_ssl_context *ssl);
72 
73 size_t esp_mbedtls_get_crt_size(mbedtls_x509_crt *cert, size_t *num);
74 
75 #ifdef CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA
76 void esp_mbedtls_free_dhm(mbedtls_ssl_context *ssl);
77 
78 void esp_mbedtls_free_keycert(mbedtls_ssl_context *ssl);
79 
80 void esp_mbedtls_free_keycert_cert(mbedtls_ssl_context *ssl);
81 
82 void esp_mbedtls_free_keycert_key(mbedtls_ssl_context *ssl);
83 #endif
84 
85 #ifdef CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT
86 void esp_mbedtls_free_cacert(mbedtls_ssl_context *ssl);
87 #endif
88 
89 #endif /* _DYNAMIC_IMPL_H_ */
90