2021-02-10 15:46:28 -05:00
|
|
|
/* OpenSSL Client Example
|
2016-11-17 21:07:34 -05:00
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2017-03-22 00:36:11 -04:00
|
|
|
#include "openssl_client_example.h"
|
2016-11-15 22:11:01 -05:00
|
|
|
|
|
|
|
#include "openssl/ssl.h"
|
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "lwip/sockets.h"
|
2016-11-15 22:11:01 -05:00
|
|
|
|
|
|
|
#include "nvs_flash.h"
|
2021-02-10 15:46:28 -05:00
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
2018-11-20 11:41:45 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2016-11-15 22:11:01 -05:00
|
|
|
|
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
static const char *TAG = "openssl_example";
|
2016-11-15 22:11:01 -05:00
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
static int open_connection(const char *host, char *port)
|
2016-11-15 22:11:01 -05:00
|
|
|
{
|
2021-02-10 15:46:28 -05:00
|
|
|
const struct addrinfo hints = {
|
|
|
|
.ai_family = AF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
};
|
|
|
|
struct addrinfo * res;
|
|
|
|
struct in_addr *addr;
|
|
|
|
int sd;
|
|
|
|
int err = getaddrinfo(host, port, &hints, &res);
|
|
|
|
if (err < 0) {
|
|
|
|
ESP_LOGE(TAG, "getaddrinfo() failed for IPV4 destination address. error: %d", err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res == 0) {
|
|
|
|
ESP_LOGE(TAG, "getaddrinfo() did not return any addresses");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
|
|
|
|
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));
|
|
|
|
sd = socket(res->ai_family, res->ai_socktype, 0);
|
|
|
|
if(sd < 0) {
|
|
|
|
ESP_LOGE(TAG, "Failed to allocate socket.");
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect(sd, res->ai_addr, res->ai_addrlen) != 0) {
|
|
|
|
ESP_LOGE(TAG, "Socket connect failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sd;
|
|
|
|
}
|
2016-11-15 22:11:01 -05:00
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
static SSL_CTX* init_contex(void)
|
|
|
|
{
|
2016-11-15 22:11:01 -05:00
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
#if CONFIG_EXAMPLE_OPENSSL_CLIENT_URI_FROM_STDIN
|
|
|
|
extern const unsigned char cacert_pem_start[] asm("_binary_ca_crt_start");
|
|
|
|
extern const unsigned char cacert_pem_end[] asm("_binary_ca_crt_end");
|
|
|
|
#else
|
|
|
|
extern const unsigned char cacert_pem_start[] asm("_binary_baidu_ca_crt_start");
|
|
|
|
extern const unsigned char cacert_pem_end[] asm("_binary_baidu_ca_crt_end");
|
|
|
|
#endif
|
|
|
|
const unsigned int cacert_pem_bytes = cacert_pem_end - cacert_pem_start;
|
|
|
|
|
|
|
|
const SSL_METHOD *mtd = TLSv1_1_client_method();
|
|
|
|
SSL_CTX *ctx = SSL_CTX_new(mtd); /* Create new context */
|
|
|
|
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
|
|
|
|
|
|
|
|
X509 *x = d2i_X509(NULL, cacert_pem_start, cacert_pem_bytes);
|
|
|
|
if(!x) {
|
|
|
|
ESP_LOGI(TAG,"Loading certs failed \n");
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|
2021-02-10 15:46:28 -05:00
|
|
|
SSL_CTX_add_client_CA(ctx, x);
|
2016-11-15 22:11:01 -05:00
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
return ctx;
|
|
|
|
}
|
2016-11-15 22:11:01 -05:00
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
static void start_example(const char *host, char *port)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx = NULL;
|
|
|
|
SSL *ssl = NULL;
|
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ctx = init_contex();
|
2016-11-15 22:11:01 -05:00
|
|
|
if (!ctx) {
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGE(TAG, "Failed");
|
2016-11-15 22:11:01 -05:00
|
|
|
goto failed1;
|
|
|
|
}
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGI(TAG, "Trying connect to %s port %s ...", host, port);
|
|
|
|
sockfd = open_connection(host, port);
|
|
|
|
if(sockfd < 0) {
|
|
|
|
ESP_LOGE(TAG,"Failed");
|
|
|
|
goto failed1;
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "OK");
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGI(TAG, "Create SSL obj");
|
2016-11-15 22:11:01 -05:00
|
|
|
ssl = SSL_new(ctx);
|
|
|
|
if (!ssl) {
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGE(TAG,"Failed");
|
|
|
|
goto failed2;
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "OK");
|
2017-11-28 05:23:39 -05:00
|
|
|
SSL_set_fd(ssl, sockfd);
|
2016-11-15 22:11:01 -05:00
|
|
|
ret = SSL_connect(ssl);
|
|
|
|
if (ret <= 0) {
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGE(TAG,"SSL Connection Failed");
|
|
|
|
goto failed3;
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|
2021-02-10 15:46:28 -05:00
|
|
|
ESP_LOGI(TAG,"SSL Connection Succeed");
|
|
|
|
failed3:
|
2016-11-15 22:11:01 -05:00
|
|
|
SSL_free(ssl);
|
|
|
|
ssl = NULL;
|
2021-02-10 15:46:28 -05:00
|
|
|
failed2:
|
2017-11-28 05:23:39 -05:00
|
|
|
close(sockfd);
|
|
|
|
sockfd = -1;
|
2021-02-10 15:46:28 -05:00
|
|
|
failed1:
|
2016-11-15 22:11:01 -05:00
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
ctx = NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
#if CONFIG_EXAMPLE_OPENSSL_CLIENT_URI_FROM_STDIN
|
|
|
|
static void get_string(char *line, size_t size)
|
2016-11-15 22:11:01 -05:00
|
|
|
{
|
2021-02-10 15:46:28 -05:00
|
|
|
int count = 0;
|
|
|
|
while (count < size) {
|
|
|
|
int c = fgetc(stdin);
|
|
|
|
if (c == '\n') {
|
|
|
|
line[count] = '\0';
|
|
|
|
break;
|
|
|
|
} else if (c > 0 && c < 127) {
|
|
|
|
line[count] = c;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 15:46:28 -05:00
|
|
|
#endif /* CONFIG_EXAMPLE_OPENSSL_CLIENT_URI_FROM_STDIN */
|
2016-11-15 22:11:01 -05:00
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2021-02-10 15:46:28 -05:00
|
|
|
char host[128] = EXAMPLE_OPENSSL_TARGET_DOMAIN;
|
|
|
|
char port[32] = EXAMPLE_OPENSSL_TARGET_PORT;
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "[APP] Startup..");
|
|
|
|
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
|
|
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
|
|
|
|
2018-11-20 11:41:45 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 11:41:45 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2021-02-10 15:46:28 -05:00
|
|
|
#if CONFIG_EXAMPLE_OPENSSL_CLIENT_URI_FROM_STDIN
|
|
|
|
char line[256] = "";
|
|
|
|
get_string(line, sizeof(line));
|
|
|
|
sscanf(line, "%s %s", host, port);
|
|
|
|
#endif /* CONFIG_EXAMPLE_OPENSSL_CLIENT_URI_FROM_STDIN */
|
|
|
|
start_example(host, port);
|
2016-11-15 22:11:01 -05:00
|
|
|
}
|