23
23
24
24
static const char * TAG = "Dynamic Impl" ;
25
25
26
+ static void esp_mbedtls_set_buf_state (unsigned char * buf , esp_mbedtls_ssl_buf_states state )
27
+ {
28
+ struct esp_mbedtls_ssl_buf * temp = __containerof (buf , struct esp_mbedtls_ssl_buf , buf [0 ]);
29
+ temp -> state = state ;
30
+ }
31
+
32
+ static esp_mbedtls_ssl_buf_states esp_mbedtls_get_buf_state (unsigned char * buf )
33
+ {
34
+ struct esp_mbedtls_ssl_buf * temp = __containerof (buf , struct esp_mbedtls_ssl_buf , buf [0 ]);
35
+ return temp -> state ;
36
+ }
37
+
38
+ void esp_mbedtls_free_buf (unsigned char * buf )
39
+ {
40
+ struct esp_mbedtls_ssl_buf * temp = __containerof (buf , struct esp_mbedtls_ssl_buf , buf [0 ]);
41
+ ESP_LOGV (TAG , "free buffer @ %p" , temp );
42
+ mbedtls_free (temp );
43
+ }
44
+
45
+ static void esp_mbedtls_init_ssl_buf (struct esp_mbedtls_ssl_buf * buf , unsigned int len )
46
+ {
47
+ if (buf ) {
48
+ buf -> state = ESP_MBEDTLS_SSL_BUF_CACHED ;
49
+ buf -> len = len ;
50
+ }
51
+ }
52
+
26
53
static void esp_mbedtls_parse_record_header (mbedtls_ssl_context * ssl )
27
54
{
28
55
ssl -> in_msgtype = ssl -> in_hdr [0 ];
@@ -118,29 +145,30 @@ static void init_rx_buffer(mbedtls_ssl_context *ssl, unsigned char *buf)
118
145
119
146
static int esp_mbedtls_alloc_tx_buf (mbedtls_ssl_context * ssl , int len )
120
147
{
121
- unsigned char * buf ;
148
+ struct esp_mbedtls_ssl_buf * esp_buf ;
122
149
123
150
if (ssl -> out_buf ) {
124
- mbedtls_free (ssl -> out_buf );
151
+ esp_mbedtls_free_buf (ssl -> out_buf );
125
152
ssl -> out_buf = NULL ;
126
153
}
127
154
128
- buf = mbedtls_calloc (1 , len );
129
- if (!buf ) {
130
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , len );
155
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + len );
156
+ if (!esp_buf ) {
157
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + len );
131
158
return MBEDTLS_ERR_SSL_ALLOC_FAILED ;
132
159
}
133
160
134
- ESP_LOGV (TAG , "add out buffer %d bytes @ %p" , len , buf );
161
+ ESP_LOGV (TAG , "add out buffer %d bytes @ %p" , len , esp_buf -> buf );
135
162
163
+ esp_mbedtls_init_ssl_buf (esp_buf , len );
136
164
/**
137
165
* Mark the out_msg offset from ssl->out_buf.
138
- *
166
+ *
139
167
* In mbedtls, ssl->out_msg = ssl->out_buf + offset;
140
168
*/
141
169
ssl -> out_msg = (unsigned char * )MBEDTLS_SSL_HEADER_LEN ;
142
170
143
- init_tx_buffer (ssl , buf );
171
+ init_tx_buffer (ssl , esp_buf -> buf );
144
172
145
173
return 0 ;
146
174
}
@@ -150,7 +178,7 @@ int esp_mbedtls_setup_tx_buffer(mbedtls_ssl_context *ssl)
150
178
CHECK_OK (esp_mbedtls_alloc_tx_buf (ssl , TX_IDLE_BUFFER_SIZE ));
151
179
152
180
/* mark the out buffer has no data cached */
153
- ssl -> out_iv = NULL ;
181
+ esp_mbedtls_set_buf_state ( ssl -> out_buf , ESP_MBEDTLS_SSL_BUF_NO_CACHED ) ;
154
182
155
183
return 0 ;
156
184
}
@@ -168,10 +196,7 @@ int esp_mbedtls_reset_add_tx_buffer(mbedtls_ssl_context *ssl)
168
196
169
197
int esp_mbedtls_reset_free_tx_buffer (mbedtls_ssl_context * ssl )
170
198
{
171
- ESP_LOGV (TAG , "free out buffer @ %p" , ssl -> out_buf );
172
-
173
- mbedtls_free (ssl -> out_buf );
174
-
199
+ esp_mbedtls_free_buf (ssl -> out_buf );
175
200
init_tx_buffer (ssl , NULL );
176
201
177
202
CHECK_OK (esp_mbedtls_setup_tx_buffer (ssl ));
@@ -181,76 +206,75 @@ int esp_mbedtls_reset_free_tx_buffer(mbedtls_ssl_context *ssl)
181
206
182
207
int esp_mbedtls_reset_add_rx_buffer (mbedtls_ssl_context * ssl )
183
208
{
184
- unsigned char * buf ;
209
+ struct esp_mbedtls_ssl_buf * esp_buf ;
185
210
186
211
if (ssl -> in_buf ) {
187
- mbedtls_free (ssl -> in_buf );
212
+ esp_mbedtls_free_buf (ssl -> in_buf );
188
213
ssl -> in_buf = NULL ;
189
214
}
190
215
191
- buf = mbedtls_calloc (1 , MBEDTLS_SSL_IN_BUFFER_LEN );
192
- if (!buf ) {
193
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , MBEDTLS_SSL_IN_BUFFER_LEN );
216
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + MBEDTLS_SSL_IN_BUFFER_LEN );
217
+ if (!esp_buf ) {
218
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + MBEDTLS_SSL_IN_BUFFER_LEN );
194
219
return MBEDTLS_ERR_SSL_ALLOC_FAILED ;
195
220
}
196
221
197
- ESP_LOGV (TAG , "add in buffer %d bytes @ %p" , MBEDTLS_SSL_IN_BUFFER_LEN , buf );
222
+ ESP_LOGV (TAG , "add in buffer %d bytes @ %p" , MBEDTLS_SSL_IN_BUFFER_LEN , esp_buf -> buf );
198
223
224
+ esp_mbedtls_init_ssl_buf (esp_buf , MBEDTLS_SSL_IN_BUFFER_LEN );
199
225
/**
200
226
* Mark the in_msg offset from ssl->in_buf.
201
- *
227
+ *
202
228
* In mbedtls, ssl->in_msg = ssl->in_buf + offset;
203
229
*/
204
230
ssl -> in_msg = (unsigned char * )MBEDTLS_SSL_HEADER_LEN ;
205
231
206
- init_rx_buffer (ssl , buf );
232
+ init_rx_buffer (ssl , esp_buf -> buf );
207
233
208
- return 0 ;
234
+ return 0 ;
209
235
}
210
236
211
237
void esp_mbedtls_reset_free_rx_buffer (mbedtls_ssl_context * ssl )
212
238
{
213
- ESP_LOGV (TAG , "free in buffer @ %p" , ssl -> in_buf );
214
-
215
- mbedtls_free (ssl -> in_buf );
216
-
217
- init_rx_buffer (ssl , NULL );
239
+ esp_mbedtls_free_buf (ssl -> in_buf );
240
+ init_rx_buffer (ssl , NULL );
218
241
}
219
242
220
243
int esp_mbedtls_add_tx_buffer (mbedtls_ssl_context * ssl , size_t buffer_len )
221
244
{
222
245
int ret = 0 ;
223
246
int cached = 0 ;
224
- unsigned char * buf ;
247
+ struct esp_mbedtls_ssl_buf * esp_buf ;
225
248
unsigned char cache_buf [CACHE_BUFFER_SIZE ];
226
249
227
250
ESP_LOGV (TAG , "--> add out" );
228
251
229
252
if (ssl -> out_buf ) {
230
- if (ssl -> out_iv ) {
253
+ if (esp_mbedtls_get_buf_state ( ssl -> out_buf ) == ESP_MBEDTLS_SSL_BUF_CACHED ) {
231
254
ESP_LOGV (TAG , "out buffer is not empty" );
232
255
ret = 0 ;
233
256
goto exit ;
234
257
} else {
235
258
memcpy (cache_buf , ssl -> out_buf , CACHE_BUFFER_SIZE );
236
-
237
- mbedtls_free (ssl -> out_buf );
259
+ esp_mbedtls_free_buf (ssl -> out_buf );
238
260
init_tx_buffer (ssl , NULL );
239
261
cached = 1 ;
240
262
}
241
263
}
242
264
243
265
buffer_len = tx_buffer_len (ssl , buffer_len );
244
266
245
- buf = mbedtls_calloc (1 , buffer_len );
246
- if (!buf ) {
247
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , buffer_len );
267
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + buffer_len );
268
+ if (!esp_buf ) {
269
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + buffer_len );
248
270
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED ;
249
271
goto exit ;
250
272
}
251
273
252
- ESP_LOGV (TAG , "add out buffer %d bytes @ %p" , buffer_len , buf );
253
- init_tx_buffer (ssl , buf );
274
+ ESP_LOGV (TAG , "add out buffer %d bytes @ %p" , buffer_len , esp_buf -> buf );
275
+
276
+ esp_mbedtls_init_ssl_buf (esp_buf , buffer_len );
277
+ init_tx_buffer (ssl , esp_buf -> buf );
254
278
255
279
if (cached ) {
256
280
memcpy (ssl -> out_ctr , cache_buf , COUNTER_SIZE );
@@ -270,34 +294,31 @@ int esp_mbedtls_free_tx_buffer(mbedtls_ssl_context *ssl)
270
294
{
271
295
int ret = 0 ;
272
296
unsigned char buf [CACHE_BUFFER_SIZE ];
273
- unsigned char * pdata ;
297
+ struct esp_mbedtls_ssl_buf * esp_buf ;
274
298
275
299
ESP_LOGV (TAG , "--> free out" );
276
300
277
- if (!ssl -> out_buf || (ssl -> out_buf && ! ssl -> out_iv )) {
301
+ if (!ssl -> out_buf || (ssl -> out_buf && ( esp_mbedtls_get_buf_state ( ssl -> out_buf ) == ESP_MBEDTLS_SSL_BUF_NO_CACHED ) )) {
278
302
ret = 0 ;
279
303
goto exit ;
280
304
}
281
305
282
306
memcpy (buf , ssl -> out_ctr , COUNTER_SIZE );
283
307
memcpy (buf + COUNTER_SIZE , ssl -> out_iv , CACHE_IV_SIZE );
284
308
285
- ESP_LOGV (TAG , "free out buffer @ %p" , ssl -> out_buf );
286
-
287
- mbedtls_free (ssl -> out_buf );
288
-
309
+ esp_mbedtls_free_buf (ssl -> out_buf );
289
310
init_tx_buffer (ssl , NULL );
290
311
291
- pdata = mbedtls_calloc (1 , TX_IDLE_BUFFER_SIZE );
292
- if (!pdata ) {
293
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , TX_IDLE_BUFFER_SIZE );
312
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + TX_IDLE_BUFFER_SIZE );
313
+ if (!esp_buf ) {
314
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + TX_IDLE_BUFFER_SIZE );
294
315
return MBEDTLS_ERR_SSL_ALLOC_FAILED ;
295
316
}
296
317
297
- memcpy ( pdata , buf , CACHE_BUFFER_SIZE );
298
- init_tx_buffer ( ssl , pdata );
299
- ssl -> out_iv = NULL ;
300
-
318
+ esp_mbedtls_init_ssl_buf ( esp_buf , TX_IDLE_BUFFER_SIZE );
319
+ memcpy ( esp_buf -> buf , buf , CACHE_BUFFER_SIZE );
320
+ init_tx_buffer ( ssl , esp_buf -> buf ) ;
321
+ esp_mbedtls_set_buf_state ( ssl -> out_buf , ESP_MBEDTLS_SSL_BUF_NO_CACHED );
301
322
exit :
302
323
ESP_LOGV (TAG , "<-- free out" );
303
324
@@ -309,23 +330,19 @@ int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl)
309
330
int cached = 0 ;
310
331
int ret = 0 ;
311
332
int buffer_len ;
312
- unsigned char * buf ;
333
+ struct esp_mbedtls_ssl_buf * esp_buf ;
313
334
unsigned char cache_buf [16 ];
314
335
unsigned char msg_head [5 ];
315
336
size_t in_msglen , in_left ;
316
337
317
338
ESP_LOGV (TAG , "--> add rx" );
318
339
319
340
if (ssl -> in_buf ) {
320
- if (ssl -> in_iv ) {
341
+ if (esp_mbedtls_get_buf_state ( ssl -> in_buf ) == ESP_MBEDTLS_SSL_BUF_CACHED ) {
321
342
ESP_LOGV (TAG , "in buffer is not empty" );
322
343
ret = 0 ;
323
344
goto exit ;
324
345
} else {
325
- memcpy (cache_buf , ssl -> in_buf , 16 );
326
-
327
- mbedtls_free (ssl -> in_buf );
328
- init_rx_buffer (ssl , NULL );
329
346
cached = 1 ;
330
347
}
331
348
}
@@ -341,7 +358,7 @@ int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl)
341
358
} else {
342
359
ESP_LOGE (TAG , "mbedtls_ssl_fetch_input error=-0x%x" , - ret );
343
360
}
344
-
361
+
345
362
goto exit ;
346
363
}
347
364
@@ -354,16 +371,23 @@ int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl)
354
371
ESP_LOGV (TAG , "message length is %d RX buffer length should be %d left is %d" ,
355
372
(int )in_msglen , (int )buffer_len , (int )ssl -> in_left );
356
373
357
- buf = mbedtls_calloc (1 , buffer_len );
358
- if (!buf ) {
359
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , buffer_len );
374
+ if (cached ) {
375
+ memcpy (cache_buf , ssl -> in_buf , 16 );
376
+ esp_mbedtls_free_buf (ssl -> in_buf );
377
+ init_rx_buffer (ssl , NULL );
378
+ }
379
+
380
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + buffer_len );
381
+ if (!esp_buf ) {
382
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + buffer_len );
360
383
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED ;
361
384
goto exit ;
362
385
}
363
386
364
- ESP_LOGV (TAG , "add in buffer %d bytes @ %p" , buffer_len , buf );
387
+ ESP_LOGV (TAG , "add in buffer %d bytes @ %p" , buffer_len , esp_buf -> buf );
365
388
366
- init_rx_buffer (ssl , buf );
389
+ esp_mbedtls_init_ssl_buf (esp_buf , buffer_len );
390
+ init_rx_buffer (ssl , esp_buf -> buf );
367
391
368
392
if (cached ) {
369
393
memcpy (ssl -> in_ctr , cache_buf , 8 );
@@ -377,22 +401,22 @@ int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl)
377
401
exit :
378
402
ESP_LOGV (TAG , "<-- add rx" );
379
403
380
- return ret ;
404
+ return ret ;
381
405
}
382
406
383
407
int esp_mbedtls_free_rx_buffer (mbedtls_ssl_context * ssl )
384
408
{
385
409
int ret = 0 ;
386
410
unsigned char buf [16 ];
387
- unsigned char * pdata ;
411
+ struct esp_mbedtls_ssl_buf * esp_buf ;
388
412
389
413
ESP_LOGV (TAG , "--> free rx" );
390
414
391
415
/**
392
416
* When have read multi messages once, can't free the input buffer directly.
393
417
*/
394
418
if (!ssl -> in_buf || (ssl -> in_hslen && (ssl -> in_hslen < ssl -> in_msglen )) ||
395
- (ssl -> in_buf && ! ssl -> in_iv )) {
419
+ (ssl -> in_buf && ( esp_mbedtls_get_buf_state ( ssl -> in_buf ) == ESP_MBEDTLS_SSL_BUF_NO_CACHED ) )) {
396
420
ret = 0 ;
397
421
goto exit ;
398
422
}
@@ -407,23 +431,20 @@ int esp_mbedtls_free_rx_buffer(mbedtls_ssl_context *ssl)
407
431
memcpy (buf , ssl -> in_ctr , 8 );
408
432
memcpy (buf + 8 , ssl -> in_iv , 8 );
409
433
410
- ESP_LOGV (TAG , "free in buffer @ %p" , ssl -> out_buf );
411
-
412
- mbedtls_free (ssl -> in_buf );
413
-
434
+ esp_mbedtls_free_buf (ssl -> in_buf );
414
435
init_rx_buffer (ssl , NULL );
415
436
416
- pdata = mbedtls_calloc (1 , 16 );
417
- if (!pdata ) {
418
- ESP_LOGE (TAG , "alloc(%d bytes) failed" , 16 );
437
+ esp_buf = mbedtls_calloc (1 , SSL_BUF_HEAD_OFFSET_SIZE + 16 );
438
+ if (!esp_buf ) {
439
+ ESP_LOGE (TAG , "alloc(%d bytes) failed" , SSL_BUF_HEAD_OFFSET_SIZE + 16 );
419
440
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED ;
420
441
goto exit ;
421
442
}
422
443
423
- memcpy ( pdata , buf , 16 );
424
- init_rx_buffer ( ssl , pdata );
425
- ssl -> in_iv = NULL ;
426
-
444
+ esp_mbedtls_init_ssl_buf ( esp_buf , 16 );
445
+ memcpy ( esp_buf -> buf , buf , 16 );
446
+ init_rx_buffer ( ssl , esp_buf -> buf ) ;
447
+ esp_mbedtls_set_buf_state ( ssl -> in_buf , ESP_MBEDTLS_SSL_BUF_NO_CACHED );
427
448
exit :
428
449
ESP_LOGV (TAG , "<-- free rx" );
429
450
@@ -438,7 +459,7 @@ size_t esp_mbedtls_get_crt_size(mbedtls_x509_crt *cert, size_t *num)
438
459
while (cert ) {
439
460
bytes += cert -> raw .len ;
440
461
n ++ ;
441
-
462
+
442
463
cert = cert -> next ;
443
464
}
444
465
0 commit comments