4343#include  "esp_tls.h" 
4444
4545/* Constants that aren't configurable in menuconfig */ 
46- #define  WEB_SERVER  "www.howsmyssl .com"
47- #define  WEB_PORT  " 443" 
48- #define  WEB_URL  "https://www.howsmyssl .com/a/check "
46+ #define  WEB_SERVER  "api.github .com"
47+ #define  WEB_PORT  ( 443) 
48+ #define  WEB_URL  "https://api.github .com/zen "
4949
5050static  const  char  * TAG  =  "example" ;
5151
5252static  const  char  * REQUEST  =  "GET "  WEB_URL  " HTTP/1.0\r\n" 
53-     "Host: " WEB_SERVER "\r\n" 
54-     "User-Agent: esp-idf/1.0 esp32\r\n" 
55-     "\r\n" ;
53+                               "Host: " WEB_SERVER "\r\n" 
54+                               "User-Agent: esp-idf/1.0 esp32\r\n" 
55+                               "\r\n" ;
5656
57- /* Root cert for howsmyssl .com, taken from server_root_cert.pem 
57+ /* Root cert for api.github .com, taken from server_root_cert.pem 
5858
5959   The PEM file was extracted from the output of this command: 
60-    openssl s_client -showcerts -connect www.howsmyssl .com:443 </dev/null 
60+    openssl s_client -showcerts -connect www.api.github .com:443 </dev/null 
6161
6262   The CA root cert is the last cert given in the chain of certs. 
6363
@@ -67,80 +67,97 @@ static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
6767extern  const  uint8_t  server_root_cert_pem_start [] asm("_binary_server_root_cert_pem_start" );
6868extern  const  uint8_t  server_root_cert_pem_end []   asm("_binary_server_root_cert_pem_end" );
6969
70+ /* 
71+  * NOTE: To turn on debug logs for wolfSSL component and this example, uncomment 
72+  * #define DEBUF_WOLFSSL in file components/wolfssl/port/user_settings.h 
73+  */ 
74+ /* 
75+  * NOTE: To turn on TLS 1.3 only mode for wolfSSL component, uncomment 
76+  * #define WOLFSSL_TLS13 in file ../components/wolfssl/port/user_settings.h 
77+  */ 
7078
7179static  void  https_get_task (void  * pvParameters )
7280{
7381    char  buf [512 ];
7482    int  ret , len ;
83+     esp_tls_t  * tls  =  NULL ;
7584
76-     while (1 ) {
85+     while   (1 ) {
7786        esp_tls_cfg_t  cfg  =  {
7887            .cacert_buf   =  server_root_cert_pem_start ,
7988            .cacert_bytes  =  server_root_cert_pem_end  -  server_root_cert_pem_start ,
8089        };
81-         
82-         struct  esp_tls  * tls  =  esp_tls_conn_http_new (WEB_URL , & cfg );
83-         
84-         if (tls  !=  NULL ) {
90+ 
91+ #if  ESP_IDF_VERSION  >= ESP_IDF_VERSION_VAL (5 , 0 , 0 )
92+         tls  =  esp_tls_init ();
93+         if  (!tls ) {
94+             ESP_LOGE (TAG , "Failed to allocate esp_tls handle!" );
95+             goto exit ;
96+         }
97+ 
98+         if  (esp_tls_conn_http_new_sync (WEB_URL , & cfg , tls ) ==  1 ) {
99+             ESP_LOGI (TAG , "Connection established..." );
100+         } else  {
101+             ESP_LOGE (TAG , "Connection failed..." );
102+             goto cleanup ;
103+         }
104+ #else  // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) 
105+         tls  =  esp_tls_conn_http_new (WEB_URL , & cfg );
106+         if  (tls  !=  NULL ) {
85107            ESP_LOGI (TAG , "Connection established..." );
86108        } else  {
87109            ESP_LOGE (TAG , "Connection failed..." );
88110            goto exit ;
89111        }
90-         
112+ #endif  //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) 
113+ 
91114        size_t  written_bytes  =  0 ;
92115        do  {
93-             ret  =  esp_tls_conn_write (tls ,  
94-                                      REQUEST  +  written_bytes ,  
116+             ret  =  esp_tls_conn_write (tls ,
117+                                      REQUEST  +  written_bytes ,
95118                                     strlen (REQUEST ) -  written_bytes );
96119            if  (ret  >= 0 ) {
97120                ESP_LOGI (TAG , "%d bytes written" , ret );
98121                written_bytes  +=  ret ;
99122            } else  if  (ret  !=  ESP_TLS_ERR_SSL_WANT_READ   &&  ret  !=  ESP_TLS_ERR_SSL_WANT_WRITE ) {
100123                ESP_LOGE (TAG , "esp_tls_conn_write  returned 0x%x" , ret );
101-                 goto exit ;
124+                 goto cleanup ;
102125            }
103-         } while (written_bytes  <  strlen (REQUEST ));
126+         } while   (written_bytes  <  strlen (REQUEST ));
104127
105128        ESP_LOGI (TAG , "Reading HTTP response..." );
106129
107-         do 
108-         {
130+         do  {
109131            len  =  sizeof (buf ) -  1 ;
110-             bzero (buf , sizeof (buf ));
132+             memset (buf , 0x00 , sizeof (buf ));
133+ 
111134            ret  =  esp_tls_conn_read (tls , (char  * )buf , len );
112-             
113-             if (ret  ==  ESP_TLS_ERR_SSL_WANT_WRITE   ||  ret  ==  ESP_TLS_ERR_SSL_WANT_READ )
135+             if  (ret  ==  ESP_TLS_ERR_SSL_WANT_WRITE   ||  ret  ==  ESP_TLS_ERR_SSL_WANT_READ ) {
114136                continue ;
115-             
116-             if (ret  <  0 )
117-            {
137+             } else  if  (ret  <  0 ) {
118138                ESP_LOGE (TAG , "esp_tls_conn_read  returned -0x%x" , - ret );
119139                break ;
120-             }
121- 
122-             if (ret  ==  0 )
123-             {
140+             } else  if  (ret  ==  0 ) {
124141                ESP_LOGI (TAG , "connection closed" );
125142                break ;
126143            }
127144
128145            len  =  ret ;
129146            ESP_LOGD (TAG , "%d bytes read" , len );
130147            /* Print response directly to stdout as it is read */ 
131-             for (int  i  =  0 ; i  <  len ; i ++ ) {
148+             for   (int  i  =  0 ; i  <  len ; i ++ ) {
132149                putchar (buf [i ]);
133150            }
134-         } while (1 );
135- 
136-     exit :
137-         esp_tls_conn_delete (tls );    
138-         putchar ('\n' ); // JSON output doesn't have a newline at end 
151+             putchar ('\n' ); // JSON output doesn't have a newline at end 
152+         } while  (1 );
139153
140-         static  int  request_count ;
154+ cleanup :
155+         esp_tls_conn_destroy (tls );
156+ exit :;
157+         static  int  request_count  =  0 ;
141158        ESP_LOGI (TAG , "Completed %d requests" , ++ request_count );
142159
143-         for (int  countdown  =  10 ; countdown  >= 0 ; countdown -- ) {
160+         for   (int  countdown  =  10 ; countdown  >= 0 ; countdown -- ) {
144161            ESP_LOGI (TAG , "%d..." , countdown );
145162            vTaskDelay (1000  / portTICK_PERIOD_MS );
146163        }
@@ -150,7 +167,7 @@ static void https_get_task(void *pvParameters)
150167
151168void  app_main (void )
152169{
153-     ESP_ERROR_CHECK (  nvs_flash_init ()  );
170+     ESP_ERROR_CHECK (nvs_flash_init ());
154171    ESP_ERROR_CHECK (esp_netif_init ());
155172    ESP_ERROR_CHECK (esp_event_loop_create_default ());
156173
0 commit comments