@@ -23,6 +23,16 @@ typedef struct httpd_ssl_transport_ctx {
23
23
httpd_ssl_ctx_t * global_ctx ;
24
24
} httpd_ssl_transport_ctx_t ;
25
25
26
+ ESP_EVENT_DEFINE_BASE (ESP_HTTPS_SERVER_EVENT );
27
+
28
+ static void http_dispatch_event_to_event_loop (int32_t event_id , const void * event_data , size_t event_data_size )
29
+ {
30
+ esp_err_t err = esp_event_post (ESP_HTTPS_SERVER_EVENT , event_id , event_data , event_data_size , portMAX_DELAY );
31
+ if (err != ESP_OK ) {
32
+ ESP_LOGE (TAG , "Failed to post http_client event: %" PRId32 ", error: %s" , event_id , esp_err_to_name (err ));
33
+ }
34
+ }
35
+
26
36
/**
27
37
* SSL socket close handler
28
38
*
@@ -46,6 +56,7 @@ static void httpd_ssl_close(void *ctx)
46
56
esp_tls_server_session_delete (tls );
47
57
free (ctx );
48
58
ESP_LOGD (TAG , "Secure socket closed" );
59
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_DISCONNECTED , NULL , 0 );
49
60
}
50
61
51
62
/**
@@ -61,7 +72,16 @@ static int httpd_ssl_pending(httpd_handle_t server, int sockfd)
61
72
assert (transport_ctx != NULL );
62
73
esp_tls_t * tls = transport_ctx -> tls ;
63
74
assert (tls != NULL );
64
- return esp_tls_get_bytes_avail (tls );
75
+ int ret = esp_tls_get_bytes_avail (tls );
76
+ if (ret < 0 ) {
77
+ esp_tls_error_handle_t error_handle ;
78
+ if (esp_tls_get_error_handle (tls , & error_handle ) == ESP_OK ) {
79
+ esp_https_server_last_error_t last_error = {0 };
80
+ last_error .last_error = esp_tls_get_and_clear_last_error (error_handle , & last_error .esp_tls_error_code , & last_error .esp_tls_flags );
81
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
82
+ }
83
+ }
84
+ return ret ;
65
85
}
66
86
67
87
/**
@@ -80,7 +100,18 @@ static int httpd_ssl_recv(httpd_handle_t server, int sockfd, char *buf, size_t b
80
100
assert (transport_ctx != NULL );
81
101
esp_tls_t * tls = transport_ctx -> tls ;
82
102
assert (tls != NULL );
83
- return esp_tls_conn_read (tls , buf , buf_len );
103
+ int ret = esp_tls_conn_read (tls , buf , buf_len );
104
+ if (ret < 0 ) {
105
+ esp_tls_error_handle_t error_handle ;
106
+ if (esp_tls_get_error_handle (tls , & error_handle ) == ESP_OK ) {
107
+ esp_https_server_last_error_t last_error = {0 };
108
+ last_error .last_error = esp_tls_get_and_clear_last_error (error_handle , & last_error .esp_tls_error_code , & last_error .esp_tls_flags );
109
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
110
+ }
111
+ } else {
112
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ON_DATA , & ret , sizeof (int ));
113
+ }
114
+ return ret ;
84
115
}
85
116
86
117
/**
@@ -99,7 +130,18 @@ static int httpd_ssl_send(httpd_handle_t server, int sockfd, const char *buf, si
99
130
assert (transport_ctx != NULL );
100
131
esp_tls_t * tls = transport_ctx -> tls ;
101
132
assert (tls != NULL );
102
- return esp_tls_conn_write (tls , buf , buf_len );
133
+ int ret = esp_tls_conn_write (tls , buf , buf_len );
134
+ if (ret < 0 ) {
135
+ esp_tls_error_handle_t error_handle ;
136
+ if (esp_tls_get_error_handle (tls , & error_handle ) == ESP_OK ) {
137
+ esp_https_server_last_error_t last_error = {0 };
138
+ last_error .last_error = esp_tls_get_and_clear_last_error (error_handle , & last_error .esp_tls_error_code , & last_error .esp_tls_flags );
139
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
140
+ }
141
+ } else {
142
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_SENT_DATA , NULL , 0 );
143
+ }
144
+ return ret ;
103
145
}
104
146
105
147
/**
@@ -120,12 +162,15 @@ static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
120
162
121
163
esp_tls_t * tls = esp_tls_init ();
122
164
if (!tls ) {
165
+ esp_https_server_last_error_t last_error = {0 };
166
+ last_error .last_error = ESP_ERR_NO_MEM ;
167
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
123
168
return ESP_ERR_NO_MEM ;
124
169
}
125
170
ESP_LOGI (TAG , "performing session handshake" );
126
171
int ret = esp_tls_server_session_create (global_ctx -> tls_cfg , sockfd , tls );
127
172
if (ret != 0 ) {
128
- ESP_LOGE (TAG , "esp_tls_create_server_session failed" );
173
+ ESP_LOGE (TAG , "esp_tls_create_server_session failed, 0x%04x" , - ret );
129
174
goto fail ;
130
175
}
131
176
@@ -134,6 +179,9 @@ static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
134
179
// NOTE: allocated memory will be freed by httpd_ssl_close
135
180
httpd_ssl_transport_ctx_t * transport_ctx = (httpd_ssl_transport_ctx_t * )calloc (1 , sizeof (httpd_ssl_transport_ctx_t ));
136
181
if (!transport_ctx ) {
182
+ esp_https_server_last_error_t last_error = {0 };
183
+ last_error .last_error = ESP_ERR_NO_MEM ;
184
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
137
185
return ESP_ERR_NO_MEM ;
138
186
}
139
187
transport_ctx -> tls = tls ;
@@ -160,10 +208,18 @@ static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
160
208
user_cb_data .tls = tls ;
161
209
(global_ctx -> user_cb )((void * )& user_cb_data );
162
210
}
163
-
211
+ http_dispatch_event_to_event_loop ( HTTPS_SERVER_EVENT_ON_CONNECTED , NULL , 0 );
164
212
return ESP_OK ;
165
213
fail :
166
- esp_tls_server_session_delete (tls );
214
+ {
215
+ esp_tls_error_handle_t error_handle ;
216
+ if (esp_tls_get_error_handle (tls , & error_handle ) == ESP_OK ) {
217
+ esp_https_server_last_error_t last_error = {0 };
218
+ last_error .last_error = esp_tls_get_and_clear_last_error (error_handle , & last_error .esp_tls_error_code , & last_error .esp_tls_flags );
219
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_ERROR , & last_error , sizeof (last_error ));
220
+ }
221
+ esp_tls_server_session_delete (tls );
222
+ }
167
223
return ESP_FAIL ;
168
224
}
169
225
@@ -192,35 +248,35 @@ static void free_secure_context(void *ctx)
192
248
free (ssl_ctx );
193
249
}
194
250
195
- static httpd_ssl_ctx_t * create_secure_context (const struct httpd_ssl_config * config )
251
+ static esp_err_t create_secure_context (const struct httpd_ssl_config * config , httpd_ssl_ctx_t * * ssl_ctx )
196
252
{
197
- httpd_ssl_ctx_t * ssl_ctx = calloc (1 , sizeof (httpd_ssl_ctx_t ));
198
- if (!ssl_ctx ) {
199
- return NULL ;
253
+ if (!ssl_ctx || !* ssl_ctx ) {
254
+ return ESP_ERR_INVALID_ARG ;
200
255
}
256
+ esp_err_t ret = ESP_OK ;
201
257
esp_tls_cfg_server_t * cfg = (esp_tls_cfg_server_t * )calloc (1 , sizeof (esp_tls_cfg_server_t ));
202
258
if (!cfg ) {
259
+ ret = ESP_ERR_NO_MEM ;
203
260
goto exit ;
204
261
}
205
262
206
263
if (config -> session_tickets ) {
207
- if ( esp_tls_cfg_server_session_tickets_init (cfg ) != ESP_OK ) {
208
- ESP_LOGE (TAG , "Failed to init session ticket support" );
264
+ ret = esp_tls_cfg_server_session_tickets_init (cfg );
265
+ if ( ret != ESP_OK ) {
266
+ ESP_LOGE (TAG , "Failed to init session ticket support. error: %s" , esp_err_to_name (ret ));
209
267
goto exit ;
210
268
}
211
269
}
212
270
213
271
cfg -> userdata = config -> ssl_userdata ;
214
-
215
-
216
272
cfg -> alpn_protos = config -> alpn_protos ;
217
273
218
274
#if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK )
219
275
cfg -> cert_select_cb = config -> cert_select_cb ;
220
276
#endif
221
277
222
- ssl_ctx -> tls_cfg = cfg ;
223
- ssl_ctx -> user_cb = config -> user_cb ;
278
+ ( * ssl_ctx ) -> tls_cfg = cfg ;
279
+ ( * ssl_ctx ) -> user_cb = config -> user_cb ;
224
280
225
281
/* cacert = CA which signs client cert, or client cert itself */
226
282
if (config -> cacert_pem != NULL && config -> cacert_len > 0 ) {
@@ -231,6 +287,7 @@ static httpd_ssl_ctx_t *create_secure_context(const struct httpd_ssl_config *con
231
287
cfg -> cacert_bytes = config -> cacert_len ;
232
288
} else {
233
289
ESP_LOGE (TAG , "Could not allocate memory for client certificate authority" );
290
+ ret = ESP_ERR_NO_MEM ;
234
291
goto exit ;
235
292
}
236
293
}
@@ -244,13 +301,15 @@ static httpd_ssl_ctx_t *create_secure_context(const struct httpd_ssl_config *con
244
301
cfg -> servercert_bytes = config -> servercert_len ;
245
302
} else {
246
303
ESP_LOGE (TAG , "Could not allocate memory for server certificate" );
304
+ ret = ESP_ERR_NO_MEM ;
247
305
goto exit ;
248
306
}
249
307
} else {
250
308
#if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK )
251
309
if (config -> cert_select_cb == NULL ) {
252
310
#endif
253
311
ESP_LOGE (TAG , "No Server certificate supplied" );
312
+ ret = ESP_ERR_INVALID_ARG ;
254
313
goto exit ;
255
314
#if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK )
256
315
} else {
@@ -264,8 +323,8 @@ static httpd_ssl_ctx_t *create_secure_context(const struct httpd_ssl_config *con
264
323
if (!cfg -> use_secure_element ) {
265
324
if (config -> use_ecdsa_peripheral ) {
266
325
#ifdef CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
267
- ssl_ctx -> tls_cfg -> use_ecdsa_peripheral = config -> use_ecdsa_peripheral ;
268
- ssl_ctx -> tls_cfg -> ecdsa_key_efuse_blk = config -> ecdsa_key_efuse_blk ;
326
+ ( * ssl_ctx ) -> tls_cfg -> use_ecdsa_peripheral = config -> use_ecdsa_peripheral ;
327
+ ( * ssl_ctx ) -> tls_cfg -> ecdsa_key_efuse_blk = config -> ecdsa_key_efuse_blk ;
269
328
#else
270
329
ESP_LOGE (TAG , "Please enable the support for signing using ECDSA peripheral in menuconfig." );
271
330
goto exit ;
@@ -278,33 +337,36 @@ static httpd_ssl_ctx_t *create_secure_context(const struct httpd_ssl_config *con
278
337
cfg -> serverkey_bytes = config -> prvtkey_len ;
279
338
} else {
280
339
ESP_LOGE (TAG , "Could not allocate memory for server key" );
340
+ ret = ESP_ERR_NO_MEM ;
281
341
goto exit ;
282
342
}
283
343
} else {
284
344
#if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK )
285
345
if (config -> cert_select_cb == NULL ) {
286
346
ESP_LOGE (TAG , "No Server key supplied and no certificate selection hook is present" );
347
+ ret = ESP_ERR_INVALID_ARG ;
287
348
goto exit ;
288
349
} else {
289
350
ESP_LOGW (TAG , "Server key not supplied, make sure to supply it in the certificate selection hook" );
290
351
}
291
352
#else
292
353
ESP_LOGE (TAG , "No Server key supplied" );
354
+ ret = ESP_ERR_INVALID_ARG ;
293
355
goto exit ;
294
356
#endif
295
357
}
296
358
}
297
359
298
- return ssl_ctx ;
360
+ return ret ;
299
361
300
362
exit :
301
363
if (cfg ) {
302
364
free ((void * ) cfg -> servercert_buf );
303
365
free ((void * ) cfg -> cacert_buf );
304
366
}
305
367
free (cfg );
306
- free (ssl_ctx );
307
- return NULL ;
368
+ free (* ssl_ctx );
369
+ return ret ;
308
370
}
309
371
310
372
/** Start the server */
@@ -315,11 +377,16 @@ esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *conf
315
377
316
378
ESP_LOGI (TAG , "Starting server" );
317
379
380
+ esp_err_t ret = ESP_OK ;
318
381
if (HTTPD_SSL_TRANSPORT_SECURE == config -> transport_mode ) {
319
-
320
- httpd_ssl_ctx_t * ssl_ctx = create_secure_context (config );
382
+ httpd_ssl_ctx_t * ssl_ctx = calloc (1 , sizeof (httpd_ssl_ctx_t ));
321
383
if (!ssl_ctx ) {
322
- return -1 ;
384
+ return ESP_ERR_NO_MEM ;
385
+ }
386
+
387
+ ret = create_secure_context (config , & ssl_ctx );
388
+ if (ret != ESP_OK ) {
389
+ return ret ;
323
390
}
324
391
325
392
ESP_LOGD (TAG , "SSL context ready" );
@@ -342,17 +409,22 @@ esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *conf
342
409
343
410
httpd_handle_t handle = NULL ;
344
411
345
- esp_err_t ret = httpd_start (& handle , & config -> httpd );
412
+ ret = httpd_start (& handle , & config -> httpd );
346
413
if (ret != ESP_OK ) return ret ;
347
414
348
415
* pHandle = handle ;
349
416
350
417
ESP_LOGI (TAG , "Server listening on port %d" , config -> httpd .server_port );
418
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_START , NULL , 0 );
351
419
return ESP_OK ;
352
420
}
353
421
354
422
/** Stop the server */
355
423
esp_err_t httpd_ssl_stop (httpd_handle_t handle )
356
424
{
357
- return httpd_stop (handle );
425
+ esp_err_t ret = httpd_stop (handle );
426
+ if (ret == ESP_OK ) {
427
+ http_dispatch_event_to_event_loop (HTTPS_SERVER_EVENT_STOP , NULL , 0 );
428
+ }
429
+ return ret ;
358
430
}
0 commit comments