5
5
#include <stdio.h>
6
6
7
7
#include "esp_http_client.h"
8
+ #include "esp_partition.h"
9
+ #include "esp_crt_bundle.h"
8
10
9
11
#define MAX_HTTP_RECV_BUFFER 128
10
12
@@ -18,10 +20,38 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
18
20
}
19
21
esp_http_client_config_t config = {
20
22
.url = url ,
21
- .cert_pem = cert_pem ,
22
23
.timeout_ms = 20000 ,
23
24
};
24
25
26
+ spi_flash_mmap_handle_t handle ;
27
+ const unsigned char * certs_data = NULL ;
28
+
29
+ if (cert_pem != NULL ) {
30
+ config .cert_pem = cert_pem ;
31
+ } else {
32
+ config .crt_bundle_attach = esp_crt_bundle_attach ;
33
+
34
+ const esp_partition_t * part = esp_partition_find_first (ESP_PARTITION_TYPE_DATA , ESP_PARTITION_SUBTYPE_ANY , "certs" );
35
+ if (part == NULL ) {
36
+ return 0 ;
37
+ }
38
+
39
+ int ret = esp_partition_mmap (part , 0 , part -> size , SPI_FLASH_MMAP_DATA , (const void * * )& certs_data , & handle );
40
+ if (ret != ESP_OK ) {
41
+ return 0 ;
42
+ }
43
+
44
+ ret = esp_crt_bundle_attach (& config );
45
+ if (ret != ESP_OK ) {
46
+ return 0 ;
47
+ }
48
+
49
+ ret = esp_crt_bundle_set (certs_data , CRT_BUNDLE_SIZE );
50
+ if (ret != ESP_OK ) {
51
+ return 0 ;
52
+ }
53
+ }
54
+
25
55
esp_http_client_handle_t client = esp_http_client_init (& config );
26
56
esp_err_t err ;
27
57
if ((err = esp_http_client_open (client , 0 )) != ESP_OK ) {
@@ -43,7 +73,7 @@ int downloadAndSaveFile(char * url, FILE * f, const char * cert_pem)
43
73
ESP_LOGV (TAG , "connection closed, cleaning up, total %d bytes received" , total_read_len );
44
74
esp_http_client_close (client );
45
75
esp_http_client_cleanup (client );
46
- free (buffer );
76
+ free (buffer );
47
77
48
78
return 0 ;
49
79
}
0 commit comments