mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
feature/openssl: fixup the file and code style
This commit is contained in:
parent
656543c5ca
commit
2ed9e2d9a8
@ -6,8 +6,8 @@ First you should config the project by "make menuconfig":
|
|||||||
Example Configuration ->
|
Example Configuration ->
|
||||||
1. Target Domain : the domain that you want to connect to, and default is "www.baidu.com".
|
1. Target Domain : the domain that you want to connect to, and default is "www.baidu.com".
|
||||||
2. Target port number : the port number of the target domain, and default is 443.
|
2. Target port number : the port number of the target domain, and default is 443.
|
||||||
3. WiFi SSID : you own wifi, which is connected to the Internet, and default is "myssid".
|
3. WIFI SSID : your own WIFI, which is connected to the Internet, and default is "myssid".
|
||||||
4. WiFi Password : wifi password, and default is "mypassword"
|
4. WIFI Password : WIFI password, and default is "mypassword"
|
||||||
|
|
||||||
If you want to test the OpenSSL client demo:
|
If you want to test the OpenSSL client demo:
|
||||||
1. compile the code and load the firmware
|
1. compile the code and load the firmware
|
||||||
|
@ -1,234 +1,230 @@
|
|||||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "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 not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "openssl_client.h"
|
#include "openssl_client.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "openssl/ssl.h"
|
#include "openssl/ssl.h"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
|
|
||||||
#include "esp_types.h"
|
#include "esp_log.h"
|
||||||
#include "esp_log.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_system.h"
|
#include "esp_event_loop.h"
|
||||||
#include "esp_wifi.h"
|
|
||||||
#include "esp_event_loop.h"
|
#include "nvs_flash.h"
|
||||||
#include "esp_log.h"
|
|
||||||
|
#include "lwip/sockets.h"
|
||||||
#include "nvs_flash.h"
|
#include "lwip/netdb.h"
|
||||||
#include "tcpip_adapter.h"
|
|
||||||
|
static EventGroupHandle_t wifi_event_group;
|
||||||
#include "lwip/sockets.h"
|
|
||||||
#include "lwip/netdb.h"
|
/* The event group allows multiple bits for each event,
|
||||||
|
but we only care about one event - are we connected
|
||||||
static EventGroupHandle_t wifi_event_group;
|
to the AP with an IP? */
|
||||||
|
const static int CONNECTED_BIT = BIT0;
|
||||||
/* The event group allows multiple bits for each event,
|
|
||||||
but we only care about one event - are we connected
|
const static char *TAG = "Openssl_demo";
|
||||||
to the AP with an IP? */
|
|
||||||
const static int CONNECTED_BIT = BIT0;
|
void openssl_demo_thread(void *p)
|
||||||
|
{
|
||||||
const static char *TAG = "Openssl_demo";
|
int ret;
|
||||||
|
SSL_CTX *ctx;
|
||||||
void openssl_demo_thread(void *p)
|
SSL *ssl;
|
||||||
{
|
int socket;
|
||||||
int ret;
|
struct sockaddr_in sock_addr;
|
||||||
SSL_CTX *ctx;
|
struct hostent *hp;
|
||||||
SSL *ssl;
|
struct ip4_addr *ip4_addr;
|
||||||
int socket;
|
|
||||||
struct sockaddr_in sock_addr;
|
int recv_bytes = 0;
|
||||||
struct hostent *hp;
|
char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
|
||||||
struct ip4_addr *ip4_addr;
|
|
||||||
|
const char send_data[] = OPENSSL_DEMO_REQUEST;
|
||||||
int recv_bytes = 0;
|
const int send_bytes = sizeof(send_data);
|
||||||
char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
|
|
||||||
|
ESP_LOGI(TAG, "OpenSSL demo thread start OK");
|
||||||
const char send_data[] = OPENSSL_DEMO_REQUEST;
|
|
||||||
const int send_bytes = sizeof(send_data);
|
ESP_LOGI(TAG, "get target IP address");
|
||||||
|
hp = gethostbyname(OPENSSL_DEMO_TARGET_NAME);
|
||||||
ESP_LOGI(TAG, "OpenSSL demo thread start OK");
|
if (!hp) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "get target IP address");
|
goto failed1;
|
||||||
hp = gethostbyname(OPENSSL_DEMO_TARGET_NAME);
|
}
|
||||||
if (!hp) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed1;
|
ip4_addr = (struct ip4_addr *)hp->h_addr;
|
||||||
}
|
ESP_LOGI(TAG, IPSTR, IP2STR(ip4_addr));
|
||||||
ESP_LOGI(TAG, "OK");
|
|
||||||
|
ESP_LOGI(TAG, "create SSL context ......");
|
||||||
ip4_addr = (struct ip4_addr *)hp->h_addr;
|
ctx = SSL_CTX_new(TLSv1_1_client_method());
|
||||||
ESP_LOGI(TAG, IPSTR, IP2STR(ip4_addr));
|
if (!ctx) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "create SSL context ......");
|
goto failed1;
|
||||||
ctx = SSL_CTX_new(TLSv1_1_client_method());
|
}
|
||||||
if (!ctx) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed1;
|
ESP_LOGI(TAG, "create socket ......");
|
||||||
}
|
socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (socket < 0) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "create socket ......");
|
goto failed2;
|
||||||
socket = socket(AF_INET, SOCK_STREAM, 0);
|
}
|
||||||
if (socket < 0) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed2;
|
ESP_LOGI(TAG, "bind socket ......");
|
||||||
}
|
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||||
ESP_LOGI(TAG, "OK");
|
sock_addr.sin_family = AF_INET;
|
||||||
|
sock_addr.sin_addr.s_addr = 0;
|
||||||
ESP_LOGI(TAG, "bind socket ......");
|
sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
|
||||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
||||||
sock_addr.sin_family = AF_INET;
|
if (ret) {
|
||||||
sock_addr.sin_addr.s_addr = 0;
|
ESP_LOGI(TAG, "failed");
|
||||||
sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
|
goto failed3;
|
||||||
ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
}
|
||||||
if (ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
ESP_LOGI(TAG, "socket connect to remote %s ......", OPENSSL_DEMO_TARGET_NAME);
|
||||||
}
|
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||||
ESP_LOGI(TAG, "OK");
|
sock_addr.sin_family = AF_INET;
|
||||||
|
sock_addr.sin_addr.s_addr = ip4_addr->addr;
|
||||||
ESP_LOGI(TAG, "socket connect to remote %s ......", OPENSSL_DEMO_TARGET_NAME);
|
sock_addr.sin_port = htons(OPENSSL_DEMO_TARGET_TCP_PORT);
|
||||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
ret = connect(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
||||||
sock_addr.sin_family = AF_INET;
|
if (ret) {
|
||||||
sock_addr.sin_addr.s_addr = ip4_addr->addr;
|
ESP_LOGI(TAG, "failed");
|
||||||
sock_addr.sin_port = htons(OPENSSL_DEMO_TARGET_TCP_PORT);
|
goto failed3;
|
||||||
ret = connect(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
}
|
||||||
if (ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
ESP_LOGI(TAG, "create SSL ......");
|
||||||
}
|
ssl = SSL_new(ctx);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (!ssl) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "create SSL ......");
|
goto failed3;
|
||||||
ssl = SSL_new(ctx);
|
}
|
||||||
if (!ssl) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
SSL_set_fd(ssl, socket);
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "OK");
|
ESP_LOGI(TAG, "SSL connected to %s port %d ......",
|
||||||
|
OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
|
||||||
SSL_set_fd(ssl, socket);
|
ret = SSL_connect(ssl);
|
||||||
|
if (!ret) {
|
||||||
ESP_LOGI(TAG, "SSL connected to %s port %d ......",
|
ESP_LOGI(TAG, "failed " );
|
||||||
OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
|
goto failed4;
|
||||||
ret = SSL_connect(ssl);
|
}
|
||||||
if (!ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed " );
|
|
||||||
goto failed4;
|
ESP_LOGI(TAG, "send https request to %s port %d ......",
|
||||||
}
|
OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
|
||||||
ESP_LOGI(TAG, "OK");
|
ret = SSL_write(ssl, send_data, send_bytes);
|
||||||
|
if (ret <= 0) {
|
||||||
ESP_LOGI(TAG, "send https request to %s port %d ......",
|
ESP_LOGI(TAG, "failed");
|
||||||
OPENSSL_DEMO_TARGET_NAME, OPENSSL_DEMO_TARGET_TCP_PORT);
|
goto failed5;
|
||||||
ret = SSL_write(ssl, send_data, send_bytes);
|
}
|
||||||
if (ret <= 0) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed5;
|
do {
|
||||||
}
|
ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (ret <= 0) {
|
||||||
|
break;
|
||||||
do {
|
}
|
||||||
ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
|
recv_bytes += ret;
|
||||||
if (ret <= 0) {
|
ESP_LOGI(TAG, "%s", recv_buf);
|
||||||
break;
|
} while (1);
|
||||||
}
|
|
||||||
recv_bytes += ret;
|
ESP_LOGI(TAG, "totaly read %d bytes data from %s ......", recv_bytes, OPENSSL_DEMO_TARGET_NAME);
|
||||||
ESP_LOGI(TAG, "%s", recv_buf);
|
|
||||||
} while (1);
|
failed5:
|
||||||
|
SSL_shutdown(ssl);
|
||||||
ESP_LOGI(TAG, "totaly read %d bytes data from %s ......", recv_bytes, OPENSSL_DEMO_TARGET_NAME);
|
failed4:
|
||||||
|
SSL_free(ssl);
|
||||||
failed5:
|
ssl = NULL;
|
||||||
SSL_shutdown(ssl);
|
failed3:
|
||||||
failed4:
|
close(socket);
|
||||||
SSL_free(ssl);
|
socket = -1;
|
||||||
ssl = NULL;
|
failed2:
|
||||||
failed3:
|
SSL_CTX_free(ctx);
|
||||||
close(socket);
|
ctx = NULL;
|
||||||
socket = -1;
|
failed1:
|
||||||
failed2:
|
vTaskDelete(NULL);
|
||||||
SSL_CTX_free(ctx);
|
return ;
|
||||||
ctx = NULL;
|
}
|
||||||
failed1:
|
|
||||||
vTaskDelete(NULL);
|
static void openssl_client_init(void)
|
||||||
return ;
|
{
|
||||||
}
|
int ret;
|
||||||
|
xTaskHandle openssl_handle;
|
||||||
static void openssl_client_init(void)
|
|
||||||
{
|
ret = xTaskCreate(openssl_demo_thread,
|
||||||
int ret;
|
OPENSSL_DEMO_THREAD_NAME,
|
||||||
xTaskHandle openssl_handle;
|
OPENSSL_DEMO_THREAD_STACK_WORDS,
|
||||||
|
NULL,
|
||||||
ret = xTaskCreate(openssl_demo_thread,
|
OPENSSL_DEMO_THREAD_PRORIOTY,
|
||||||
OPENSSL_DEMO_THREAD_NAME,
|
&openssl_handle);
|
||||||
OPENSSL_DEMO_THREAD_STACK_WORDS,
|
|
||||||
NULL,
|
if (ret != pdPASS) {
|
||||||
OPENSSL_DEMO_THREAD_PRORIOTY,
|
ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
|
||||||
&openssl_handle);
|
}
|
||||||
|
}
|
||||||
if (ret != pdPASS) {
|
|
||||||
ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
|
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||||
}
|
{
|
||||||
}
|
switch(event->event_id) {
|
||||||
|
case SYSTEM_EVENT_STA_START:
|
||||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
esp_wifi_connect();
|
||||||
{
|
break;
|
||||||
switch(event->event_id) {
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
case SYSTEM_EVENT_STA_START:
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
esp_wifi_connect();
|
openssl_client_init();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||||
openssl_client_init();
|
auto-reassociate. */
|
||||||
break;
|
esp_wifi_connect();
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
break;
|
||||||
auto-reassociate. */
|
default:
|
||||||
esp_wifi_connect();
|
break;
|
||||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
}
|
||||||
break;
|
return ESP_OK;
|
||||||
default:
|
}
|
||||||
break;
|
|
||||||
}
|
static void wifi_conn_init(void)
|
||||||
return ESP_OK;
|
{
|
||||||
}
|
tcpip_adapter_init();
|
||||||
|
wifi_event_group = xEventGroupCreate();
|
||||||
static void wifi_conn_init(void)
|
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
|
||||||
{
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
tcpip_adapter_init();
|
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||||
wifi_event_group = xEventGroupCreate();
|
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||||
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
|
wifi_config_t wifi_config = {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
.sta = {
|
||||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
.ssid = EXAMPLE_WIFI_SSID,
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
.password = EXAMPLE_WIFI_PASS,
|
||||||
wifi_config_t wifi_config = {
|
},
|
||||||
.sta = {
|
};
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||||
},
|
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
|
||||||
};
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
}
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
|
||||||
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
|
void app_main(void)
|
||||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
{
|
||||||
}
|
nvs_flash_init();
|
||||||
|
wifi_conn_init();
|
||||||
void app_main(void)
|
}
|
||||||
{
|
|
||||||
nvs_flash_init();
|
|
||||||
wifi_conn_init();
|
|
||||||
}
|
|
||||||
|
@ -4,8 +4,8 @@ The Example contains of OpenSSL server demo.
|
|||||||
|
|
||||||
First you should configure the project by "make menuconfig":
|
First you should configure the project by "make menuconfig":
|
||||||
Example Configuration ->
|
Example Configuration ->
|
||||||
1. WiFi SSID: WiFi network to which your PC is also connected to.
|
1. WIFI SSID: WIFI network to which your PC is also connected to.
|
||||||
1. WiFi Password: wifi password
|
1. WIFI Password: WIFI password
|
||||||
|
|
||||||
IF you want to test the OpenSSL server demo:
|
IF you want to test the OpenSSL server demo:
|
||||||
1. compile the code and load the firmware
|
1. compile the code and load the firmware
|
||||||
@ -15,6 +15,7 @@ IF you want to test the OpenSSL server demo:
|
|||||||
|
|
||||||
Note:
|
Note:
|
||||||
The private key and certification at the example are not trusted by web browser, because they are not created by CA official, just by ourselves.
|
The private key and certification at the example are not trusted by web browser, because they are not created by CA official, just by ourselves.
|
||||||
You can alse create your own private key and ceritification by "openssl at ubuntu or others".
|
You can alse create your own private key and ceritification by "openssl at ubuntu or others".
|
||||||
|
We have the document of "ESP8266_SDKSSL_User_Manual_EN_v1.4.pdf" at "http://www.espressif.com/en/support/download/documents". By it you can gernerate the private key and certification with the fomate of ".pem"
|
||||||
|
|
||||||
See the README.md file in the upper level 'examples' directory for more information about examples.
|
See the README.md file in the upper level 'examples' directory for more information about examples.
|
||||||
|
@ -1,257 +1,253 @@
|
|||||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "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 not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "openssl_server.h"
|
#include "openssl_server.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "openssl/ssl.h"
|
#include "openssl/ssl.h"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
|
|
||||||
#include "esp_types.h"
|
#include "esp_log.h"
|
||||||
#include "esp_log.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_system.h"
|
#include "esp_event_loop.h"
|
||||||
#include "esp_wifi.h"
|
|
||||||
#include "esp_event_loop.h"
|
#include "nvs_flash.h"
|
||||||
#include "esp_log.h"
|
|
||||||
|
#include "lwip/sockets.h"
|
||||||
#include "nvs_flash.h"
|
#include "lwip/netdb.h"
|
||||||
#include "tcpip_adapter.h"
|
|
||||||
|
static EventGroupHandle_t wifi_event_group;
|
||||||
#include "lwip/sockets.h"
|
|
||||||
#include "lwip/netdb.h"
|
/* The event group allows multiple bits for each event,
|
||||||
|
but we only care about one event - are we connected
|
||||||
static EventGroupHandle_t wifi_event_group;
|
to the AP with an IP? */
|
||||||
|
const static int CONNECTED_BIT = BIT0;
|
||||||
/* The event group allows multiple bits for each event,
|
|
||||||
but we only care about one event - are we connected
|
const static char *TAG = "Openssl_demo";
|
||||||
to the AP with an IP? */
|
|
||||||
const static int CONNECTED_BIT = BIT0;
|
#define OPENSSL_DEMO_SERVER_ACK "HTTP/1.1 200 OK\r\n" \
|
||||||
|
"Content-Type: text/html\r\n" \
|
||||||
const static char *TAG = "Openssl_demo";
|
"Content-Length: 98\r\n" \
|
||||||
|
"<html>\r\n" \
|
||||||
#define OPENSSL_DEMO_SERVER_ACK "HTTP/1.1 200 OK\r\n" \
|
"<head>\r\n" \
|
||||||
"Content-Type: text/html\r\n" \
|
"<title>OpenSSL demo</title></head><body>\r\n" \
|
||||||
"Content-Length: 98\r\n" \
|
"OpenSSL server demo!\r\n" \
|
||||||
"<html>\r\n" \
|
"</body>\r\n" \
|
||||||
"<head>\r\n" \
|
"</html>\r\n"
|
||||||
"<title>OpenSSL demo</title></head><body>\r\n" \
|
|
||||||
"OpenSSL server demo!\r\n" \
|
static void openssl_demo_thread(void *p)
|
||||||
"</body>\r\n" \
|
{
|
||||||
"</html>\r\n"
|
int ret;
|
||||||
|
|
||||||
static void openssl_demo_thread(void *p)
|
SSL_CTX *ctx;
|
||||||
{
|
SSL *ssl;
|
||||||
int ret;
|
|
||||||
|
int socket, new_socket;
|
||||||
SSL_CTX *ctx;
|
socklen_t addr_len;
|
||||||
SSL *ssl;
|
struct sockaddr_in sock_addr;
|
||||||
|
|
||||||
int socket, new_socket;
|
char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
|
||||||
socklen_t addr_len;
|
|
||||||
struct sockaddr_in sock_addr;
|
const char send_data[] = OPENSSL_DEMO_SERVER_ACK;
|
||||||
|
const int send_bytes = sizeof(send_data);
|
||||||
char recv_buf[OPENSSL_DEMO_RECV_BUF_LEN];
|
|
||||||
|
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
|
||||||
const char send_data[] = OPENSSL_DEMO_SERVER_ACK;
|
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
|
||||||
const int send_bytes = sizeof(send_data);
|
const unsigned int cacert_pem_bytes = cacert_pem_end - cacert_pem_start;
|
||||||
|
|
||||||
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
|
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
|
||||||
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
|
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
|
||||||
const unsigned int cacert_pem_bytes = cacert_pem_end - cacert_pem_start;
|
const unsigned int prvtkey_pem_bytes = prvtkey_pem_end - prvtkey_pem_start;
|
||||||
|
|
||||||
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
|
ESP_LOGI(TAG, "SSL server context create ......");
|
||||||
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
|
ctx = SSL_CTX_new(SSLv3_server_method());
|
||||||
const unsigned int prvtkey_pem_bytes = prvtkey_pem_end - prvtkey_pem_start;
|
if (!ctx) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server context create ......");
|
goto failed1;
|
||||||
ctx = SSL_CTX_new(SSLv3_server_method());
|
}
|
||||||
if (!ctx) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed1;
|
ESP_LOGI(TAG, "SSL server context set own certification......");
|
||||||
}
|
ret = SSL_CTX_use_certificate_ASN1(ctx, cacert_pem_bytes, cacert_pem_start);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (!ret) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server context set own certification......");
|
goto failed2;
|
||||||
ret = SSL_CTX_use_certificate_ASN1(ctx, cacert_pem_bytes, cacert_pem_start);
|
}
|
||||||
if (!ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed2;
|
ESP_LOGI(TAG, "SSL server context set private key......");
|
||||||
}
|
ret = SSL_CTX_use_PrivateKey_ASN1(0, ctx, prvtkey_pem_start, prvtkey_pem_bytes);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (!ret) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server context set private key......");
|
goto failed2;
|
||||||
ret = SSL_CTX_use_PrivateKey_ASN1(0, ctx, prvtkey_pem_start, prvtkey_pem_bytes);
|
}
|
||||||
if (!ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed2;
|
ESP_LOGI(TAG, "SSL server create socket ......");
|
||||||
}
|
socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (socket < 0) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server create socket ......");
|
goto failed2;
|
||||||
socket = socket(AF_INET, SOCK_STREAM, 0);
|
}
|
||||||
if (socket < 0) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed2;
|
ESP_LOGI(TAG, "SSL server socket bind ......");
|
||||||
}
|
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||||
ESP_LOGI(TAG, "OK");
|
sock_addr.sin_family = AF_INET;
|
||||||
|
sock_addr.sin_addr.s_addr = 0;
|
||||||
ESP_LOGI(TAG, "SSL server socket bind ......");
|
sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
|
||||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
||||||
sock_addr.sin_family = AF_INET;
|
if (ret) {
|
||||||
sock_addr.sin_addr.s_addr = 0;
|
ESP_LOGI(TAG, "failed");
|
||||||
sock_addr.sin_port = htons(OPENSSL_DEMO_LOCAL_TCP_PORT);
|
goto failed3;
|
||||||
ret = bind(socket, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
|
}
|
||||||
if (ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
ESP_LOGI(TAG, "SSL server socket listen ......");
|
||||||
}
|
ret = listen(socket, 32);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (ret) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server socket listen ......");
|
goto failed3;
|
||||||
ret = listen(socket, 32);
|
}
|
||||||
if (ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
reconnect:
|
||||||
}
|
ESP_LOGI(TAG, "SSL server create ......");
|
||||||
ESP_LOGI(TAG, "OK");
|
ssl = SSL_new(ctx);
|
||||||
|
if (!ssl) {
|
||||||
reconnect:
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server create ......");
|
goto failed3;
|
||||||
ssl = SSL_new(ctx);
|
}
|
||||||
if (!ssl) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed3;
|
ESP_LOGI(TAG, "SSL server socket accept client ......");
|
||||||
}
|
new_socket = accept(socket, (struct sockaddr *)&sock_addr, &addr_len);
|
||||||
ESP_LOGI(TAG, "OK");
|
if (new_socket < 0) {
|
||||||
|
ESP_LOGI(TAG, "failed" );
|
||||||
ESP_LOGI(TAG, "SSL server socket accept client ......");
|
goto failed4;
|
||||||
new_socket = accept(socket, (struct sockaddr *)&sock_addr, &addr_len);
|
}
|
||||||
if (new_socket < 0) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed" );
|
|
||||||
goto failed4;
|
SSL_set_fd(ssl, new_socket);
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "OK");
|
ESP_LOGI(TAG, "SSL server accept client ......");
|
||||||
|
ret = SSL_accept(ssl);
|
||||||
SSL_set_fd(ssl, new_socket);
|
if (!ret) {
|
||||||
|
ESP_LOGI(TAG, "failed");
|
||||||
ESP_LOGI(TAG, "SSL server accept client ......");
|
goto failed5;
|
||||||
ret = SSL_accept(ssl);
|
}
|
||||||
if (!ret) {
|
ESP_LOGI(TAG, "OK");
|
||||||
ESP_LOGI(TAG, "failed");
|
|
||||||
goto failed5;
|
ESP_LOGI(TAG, "SSL server read message ......");
|
||||||
}
|
do {
|
||||||
ESP_LOGI(TAG, "OK");
|
memset(recv_buf, 0, OPENSSL_DEMO_RECV_BUF_LEN);
|
||||||
|
ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
|
||||||
ESP_LOGI(TAG, "SSL server read message ......");
|
if (ret <= 0) {
|
||||||
do {
|
break;
|
||||||
memset(recv_buf, 0, OPENSSL_DEMO_RECV_BUF_LEN);
|
}
|
||||||
ret = SSL_read(ssl, recv_buf, OPENSSL_DEMO_RECV_BUF_LEN - 1);
|
if (strstr(recv_buf, "GET / HTTP/1.1")) {
|
||||||
if (ret <= 0) {
|
SSL_write(ssl, send_data, send_bytes);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strstr(recv_buf, "GET / HTTP/1.1")) {
|
} while (1);
|
||||||
SSL_write(ssl, send_data, send_bytes);
|
|
||||||
break;
|
ESP_LOGI(TAG, "result %d", ret);
|
||||||
}
|
|
||||||
} while (1);
|
SSL_shutdown(ssl);
|
||||||
|
failed5:
|
||||||
ESP_LOGI(TAG, "result %d", ret);
|
close(new_socket);
|
||||||
|
new_socket = -1;
|
||||||
SSL_shutdown(ssl);
|
failed4:
|
||||||
failed5:
|
SSL_free(ssl);
|
||||||
close(new_socket);
|
ssl = NULL;
|
||||||
new_socket = -1;
|
goto reconnect;
|
||||||
failed4:
|
failed3:
|
||||||
SSL_free(ssl);
|
close(socket);
|
||||||
ssl = NULL;
|
socket = -1;
|
||||||
goto reconnect;
|
failed2:
|
||||||
failed3:
|
SSL_CTX_free(ctx);
|
||||||
close(socket);
|
ctx = NULL;
|
||||||
socket = -1;
|
failed1:
|
||||||
failed2:
|
vTaskDelete(NULL);
|
||||||
SSL_CTX_free(ctx);
|
return ;
|
||||||
ctx = NULL;
|
}
|
||||||
failed1:
|
|
||||||
vTaskDelete(NULL);
|
static void openssl_client_init(void)
|
||||||
return ;
|
{
|
||||||
}
|
int ret;
|
||||||
|
xTaskHandle openssl_handle;
|
||||||
static void openssl_client_init(void)
|
|
||||||
{
|
ret = xTaskCreate(openssl_demo_thread,
|
||||||
int ret;
|
OPENSSL_DEMO_THREAD_NAME,
|
||||||
xTaskHandle openssl_handle;
|
OPENSSL_DEMO_THREAD_STACK_WORDS,
|
||||||
|
NULL,
|
||||||
ret = xTaskCreate(openssl_demo_thread,
|
OPENSSL_DEMO_THREAD_PRORIOTY,
|
||||||
OPENSSL_DEMO_THREAD_NAME,
|
&openssl_handle);
|
||||||
OPENSSL_DEMO_THREAD_STACK_WORDS,
|
|
||||||
NULL,
|
if (ret != pdPASS) {
|
||||||
OPENSSL_DEMO_THREAD_PRORIOTY,
|
ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
|
||||||
&openssl_handle);
|
}
|
||||||
|
}
|
||||||
if (ret != pdPASS) {
|
|
||||||
ESP_LOGI(TAG, "create thread %s failed", OPENSSL_DEMO_THREAD_NAME);
|
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||||
}
|
{
|
||||||
}
|
switch(event->event_id) {
|
||||||
|
case SYSTEM_EVENT_STA_START:
|
||||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
esp_wifi_connect();
|
||||||
{
|
break;
|
||||||
switch(event->event_id) {
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
case SYSTEM_EVENT_STA_START:
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||||
esp_wifi_connect();
|
openssl_client_init();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||||
openssl_client_init();
|
auto-reassociate. */
|
||||||
break;
|
esp_wifi_connect();
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
break;
|
||||||
auto-reassociate. */
|
default:
|
||||||
esp_wifi_connect();
|
break;
|
||||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
}
|
||||||
break;
|
return ESP_OK;
|
||||||
default:
|
}
|
||||||
break;
|
|
||||||
}
|
static void wifi_conn_init(void)
|
||||||
return ESP_OK;
|
{
|
||||||
}
|
tcpip_adapter_init();
|
||||||
|
wifi_event_group = xEventGroupCreate();
|
||||||
static void wifi_conn_init(void)
|
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
|
||||||
{
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
tcpip_adapter_init();
|
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||||
wifi_event_group = xEventGroupCreate();
|
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||||
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
|
wifi_config_t wifi_config = {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
.sta = {
|
||||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
.ssid = EXAMPLE_WIFI_SSID,
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
.password = EXAMPLE_WIFI_PASS,
|
||||||
wifi_config_t wifi_config = {
|
},
|
||||||
.sta = {
|
};
|
||||||
.ssid = EXAMPLE_WIFI_SSID,
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||||
.password = EXAMPLE_WIFI_PASS,
|
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||||
},
|
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
|
||||||
};
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
}
|
||||||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
|
||||||
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
|
void app_main(void)
|
||||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
{
|
||||||
}
|
nvs_flash_init();
|
||||||
|
wifi_conn_init();
|
||||||
void app_main(void)
|
}
|
||||||
{
|
|
||||||
nvs_flash_init();
|
|
||||||
wifi_conn_init();
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user