added auth
This commit is contained in:
parent
4ec9fecee5
commit
a787a18d21
|
@ -1,26 +1,3 @@
|
||||||
/* HTTPS GET Example using plain mbedTLS sockets
|
|
||||||
*
|
|
||||||
* Contacts the howsmyssl.com API via TLS v1.2 and reads a JSON
|
|
||||||
* response.
|
|
||||||
*
|
|
||||||
* Adapted from the ssl_client1 example in mbedtls.
|
|
||||||
*
|
|
||||||
* Original Copyright (C) 2006-2016, ARM Limited, All Rights Reserved, Apache 2.0 License.
|
|
||||||
* Additions Copyright (C) Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD, Apache 2.0 License.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
|
@ -60,6 +37,7 @@ static const char *TAG = "example";
|
||||||
static const char *REQUEST = "GET " WEB_URL " HTTP/1.1\r\n"
|
static const char *REQUEST = "GET " WEB_URL " HTTP/1.1\r\n"
|
||||||
"Host: "WEB_SERVER"\r\n"
|
"Host: "WEB_SERVER"\r\n"
|
||||||
"User-Agent: esp-idf/1.0 esp32\r\n"
|
"User-Agent: esp-idf/1.0 esp32\r\n"
|
||||||
|
"Authorization: Basic b3RhOnB3\r\n" //base64("username:password")
|
||||||
"\r\n";
|
"\r\n";
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,17 +97,9 @@ static void https_get_task(void *pvParameters)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* MBEDTLS_SSL_VERIFY_OPTIONAL is bad for security, in this example it will print
|
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||||
a warning if CA verification fails but it will continue to connect.
|
|
||||||
|
|
||||||
You should consider using MBEDTLS_SSL_VERIFY_REQUIRED in your own code.
|
|
||||||
*/
|
|
||||||
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
||||||
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
|
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
|
||||||
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||||
#ifdef CONFIG_MBEDTLS_DEBUG
|
|
||||||
mbedtls_esp_enable_debug_log(&conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
|
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
|
||||||
{
|
{
|
||||||
|
@ -182,7 +152,7 @@ static void https_get_task(void *pvParameters)
|
||||||
ESP_LOGI(TAG, "Writing HTTP request...");
|
ESP_LOGI(TAG, "Writing HTTP request...");
|
||||||
|
|
||||||
size_t written_bytes = 0;
|
size_t written_bytes = 0;
|
||||||
do {
|
while(written_bytes < strlen(REQUEST)) {
|
||||||
ret = mbedtls_ssl_write(&ssl,
|
ret = mbedtls_ssl_write(&ssl,
|
||||||
(const unsigned char *)REQUEST + written_bytes,
|
(const unsigned char *)REQUEST + written_bytes,
|
||||||
strlen(REQUEST) - written_bytes);
|
strlen(REQUEST) - written_bytes);
|
||||||
|
@ -193,7 +163,7 @@ static void https_get_task(void *pvParameters)
|
||||||
ESP_LOGE(TAG, "mbedtls_ssl_write returned -0x%x", -ret);
|
ESP_LOGE(TAG, "mbedtls_ssl_write returned -0x%x", -ret);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
} while(written_bytes < strlen(REQUEST));
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Reading HTTP response...");
|
ESP_LOGI(TAG, "Reading HTTP response...");
|
||||||
|
|
||||||
|
@ -223,17 +193,26 @@ static void https_get_task(void *pvParameters)
|
||||||
// break;
|
// break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGE(TAG, "mbedtls_ssl_read returned -0x%x", ret);
|
|
||||||
|
|
||||||
len = ret;
|
len = ret;
|
||||||
ESP_LOGD(TAG, "%d bytes read", len);
|
|
||||||
|
ESP_LOGI(TAG, "%d bytes read", len);
|
||||||
|
|
||||||
/* Print response directly to stdout as it is read */
|
/* Print response directly to stdout as it is read */
|
||||||
for(int i = 0; i < len; i++) {
|
for(int i = 0; i < len; i++) {
|
||||||
putchar(buf[i]);
|
putchar(buf[i]);
|
||||||
|
//printf("0x%x ", buf[i]);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
// } while(1);
|
// } while(1);
|
||||||
|
|
||||||
mbedtls_ssl_close_notify(&ssl);
|
ret = mbedtls_ssl_close_notify(&ssl);
|
||||||
|
|
||||||
|
if(ret != 0)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "mbedtls_ssl_close_notify returned 0x%x", ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
mbedtls_ssl_session_reset(&ssl);
|
mbedtls_ssl_session_reset(&ssl);
|
||||||
|
|
Loading…
Reference in New Issue