2020-03-20 17:01:25 +00:00
|
|
|
/* Simple HTTP + SSL Server Example
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
#include <esp_event.h>
|
|
|
|
#include <esp_log.h>
|
|
|
|
#include <esp_system.h>
|
|
|
|
#include <nvs_flash.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "esp_console.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "url_decoder.h"
|
2020-03-27 15:58:02 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "ca.h"
|
2020-03-20 17:01:25 +00:00
|
|
|
|
|
|
|
#include <esp_https_server.h>
|
|
|
|
|
|
|
|
/* A simple example that demonstrates how to create GET and POST
|
|
|
|
* handlers and start an HTTPS server.
|
|
|
|
*/
|
2020-04-05 10:27:20 +00:00
|
|
|
/*
|
2020-03-27 15:58:02 +00:00
|
|
|
struct pass_args
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-05 10:27:20 +00:00
|
|
|
//struct pass_args global_arg_task;
|
|
|
|
*/
|
2020-03-20 17:01:25 +00:00
|
|
|
|
|
|
|
static const char *TAG = "server";
|
|
|
|
TaskHandle_t xHandleServer = NULL;
|
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
/* HTTP GET handler
|
|
|
|
formular pre vlozenie csr requestu
|
|
|
|
*/
|
2020-03-20 17:01:25 +00:00
|
|
|
static esp_err_t root_get_handler(httpd_req_t *req)
|
|
|
|
{
|
|
|
|
httpd_resp_set_type(req, "text/html");
|
|
|
|
httpd_resp_send(req, "<form action=\"/echo\" method=\"post\">\n"
|
2020-03-31 16:12:53 +00:00
|
|
|
" <label for=\"csr\">CSR:</label><br>\n"
|
|
|
|
" <input type=\"text\" id=\"csr\" name=\"csr\" value=\"\"><br>\n"
|
2020-03-20 17:01:25 +00:00
|
|
|
" <input type=\"submit\" value=\"Submit\">\n"
|
2020-03-31 16:12:53 +00:00
|
|
|
"</form>", -1); // -1 = pouziva strlen()
|
2020-03-20 17:01:25 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2020-03-31 16:12:53 +00:00
|
|
|
/* HTTP POST handler
|
|
|
|
spustenie po stlaceni Submit z root handleru
|
|
|
|
generuje subor .crt
|
2020-03-28 03:21:56 +00:00
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
*/
|
2020-03-20 17:01:25 +00:00
|
|
|
static esp_err_t echo_post_handler(httpd_req_t *req)
|
|
|
|
{
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Nacitanie vstupu*/
|
|
|
|
char buf[2000];
|
2020-03-20 17:01:25 +00:00
|
|
|
int ret, remaining = req->content_len;
|
|
|
|
memset(buf,'\0',sizeof(buf));
|
|
|
|
while (remaining > 0) {
|
|
|
|
/* Read the data for the request */
|
|
|
|
if ((ret = httpd_req_recv(req, buf,
|
|
|
|
MIN(remaining, sizeof(buf)))) <= 0) {
|
|
|
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
|
|
|
/* Retry receiving if timeout occurred */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
/* Vypis dat do konzoly */
|
2020-03-20 17:01:25 +00:00
|
|
|
ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
|
|
|
|
ESP_LOGI(TAG, "%.*s", ret, buf);
|
|
|
|
ESP_LOGI(TAG, "====================================");
|
2020-03-31 16:12:53 +00:00
|
|
|
/*odstranenie premennej z textu*/
|
2020-03-20 17:01:25 +00:00
|
|
|
const char *url = &buf[4];
|
|
|
|
char out[strlen(url) + 1];
|
2020-03-27 15:58:02 +00:00
|
|
|
|
2020-03-20 17:01:25 +00:00
|
|
|
|
|
|
|
printf("length: %d\n", decode(url, 0));
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Dekodovanie url textu do normalnej podoby*/
|
2020-03-20 17:01:25 +00:00
|
|
|
printf("%s\n",decode(url, out) < 0 ? "bad string" : out);
|
2020-03-27 15:58:02 +00:00
|
|
|
char buffered_out[strlen(url) + 1];
|
2020-03-31 16:12:53 +00:00
|
|
|
char formatted_out[strlen(url) + 1];read_file("/spiffs/user.crt");
|
2020-03-27 15:58:02 +00:00
|
|
|
memset(buffered_out, '\0', strlen(url));
|
|
|
|
memset(formatted_out, '\0', strlen(url));
|
|
|
|
|
|
|
|
char* csr_begin = "-----BEGIN CERTIFICATE REQUEST-----";
|
|
|
|
char* csr_end = "-----END CERTIFICATE REQUEST-----";
|
|
|
|
strncpy(buffered_out, &out[strlen(csr_begin)], strlen(out) - strlen(csr_begin) - strlen(csr_end));
|
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
/*formatovanie textu novým riadkom kvoli standardu*/
|
|
|
|
|
2020-03-27 15:58:02 +00:00
|
|
|
char* token;
|
|
|
|
token = strtok(buffered_out, " ");
|
|
|
|
strcat(formatted_out,csr_begin);
|
2020-03-28 03:21:56 +00:00
|
|
|
strcat(formatted_out,"\r\n");
|
2020-03-27 15:58:02 +00:00
|
|
|
|
2020-03-28 03:21:56 +00:00
|
|
|
|
2020-03-27 15:58:02 +00:00
|
|
|
while (token != NULL)
|
|
|
|
{
|
|
|
|
strcat(formatted_out,token);
|
2020-03-28 03:21:56 +00:00
|
|
|
strcat(formatted_out,"\r\n");
|
2020-03-27 15:58:02 +00:00
|
|
|
token = strtok(NULL, " ");
|
|
|
|
|
|
|
|
}
|
|
|
|
strcat(formatted_out,csr_end);
|
|
|
|
printf("%s",formatted_out);
|
2020-04-05 10:27:20 +00:00
|
|
|
//Hodnoty predavane ulohe cert_write pre generovanie certifikatov pod CA
|
2020-03-31 16:12:53 +00:00
|
|
|
const int argc = 5;
|
|
|
|
const char* argv[] = {"write_cert","request_file=/spiffs/certsignreq.csr","issuer_key=/spiffs/keyfile.key","issuer_crt=/spiffs/ca.crt","output_file=/spiffs/user.crt"};
|
2020-04-05 10:27:20 +00:00
|
|
|
//global_arg_task.argc = argc;
|
2020-03-31 16:12:53 +00:00
|
|
|
//global_arg_task.argv = argv;
|
|
|
|
/*
|
|
|
|
int ii;
|
|
|
|
global_arg_task.argv = malloc(argc * sizeof *global_arg_task.argv);
|
|
|
|
for(ii = 0; ii < argc; ii++) {
|
|
|
|
global_arg_task.argv[ii] = malloc(strlen(argv[ii])+1);
|
|
|
|
strcpy(global_arg_task.argv[ii], argv[ii]);
|
|
|
|
}
|
2020-04-05 10:27:20 +00:00
|
|
|
*/ /*Ulozenie CSR requestu*/
|
2020-03-27 15:58:02 +00:00
|
|
|
create_file("/spiffs/certsignreq.csr",formatted_out);
|
2020-04-05 10:27:20 +00:00
|
|
|
/*Volanie funkcie pre vytvorenie ulohy podpisu certifikatu*/
|
2020-03-28 23:50:58 +00:00
|
|
|
task_create_ca(argc,argv);
|
|
|
|
memset(buf,'\0',sizeof(buf)-1);
|
|
|
|
memset(buffered_out, '\0', strlen(url));
|
|
|
|
memset(formatted_out, '\0', strlen(url));
|
2020-03-31 16:12:53 +00:00
|
|
|
vTaskDelay(400);
|
2020-03-28 23:50:58 +00:00
|
|
|
|
2020-04-05 10:27:20 +00:00
|
|
|
/*nahradenie LF znaku za CRLF v certifikate kvoli HTTP serveru */
|
2020-03-28 23:50:58 +00:00
|
|
|
FILE* f = fopen("/spiffs/user.crt", "r");
|
2020-03-31 16:12:53 +00:00
|
|
|
if(f!=NULL){
|
|
|
|
while(fgets(buffered_out, sizeof(buffered_out)-1, f)){
|
2020-03-28 23:50:58 +00:00
|
|
|
strcat(buf,buffered_out);
|
|
|
|
}
|
2020-03-31 16:12:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 23:50:58 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2020-04-05 10:27:20 +00:00
|
|
|
/*Rozdelenie certifikatu do tokenov*/
|
2020-03-28 23:50:58 +00:00
|
|
|
token = NULL;
|
|
|
|
token = strtok(buf, "\n");
|
|
|
|
|
|
|
|
while (token != NULL)
|
|
|
|
{
|
|
|
|
strcat(formatted_out,token);
|
|
|
|
strcat(formatted_out,"\r\n");
|
|
|
|
token = strtok(NULL, "\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Nastavenie typu suboru*/
|
2020-03-28 03:21:56 +00:00
|
|
|
httpd_resp_set_type(req, "application/x-x509-user-cert");
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Odoslanie dat*/
|
2020-03-28 03:21:56 +00:00
|
|
|
httpd_resp_send_chunk(req, formatted_out, strlen(formatted_out));
|
2020-03-20 17:01:25 +00:00
|
|
|
remaining -= ret;
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Ukoncenie prenosu dat*/
|
2020-03-20 17:01:25 +00:00
|
|
|
httpd_resp_send_chunk(req, NULL, 0);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2020-03-31 16:12:53 +00:00
|
|
|
|
|
|
|
/*Registrovanie URI*/
|
2020-03-20 17:01:25 +00:00
|
|
|
static const httpd_uri_t echo = {
|
|
|
|
.uri = "/echo",
|
|
|
|
.method = HTTP_POST,
|
|
|
|
.handler = echo_post_handler,
|
|
|
|
.user_ctx = NULL
|
|
|
|
};
|
2020-04-05 10:27:20 +00:00
|
|
|
/*Registrovanie URI*/
|
2020-03-20 17:01:25 +00:00
|
|
|
static const httpd_uri_t root = {
|
|
|
|
.uri = "/",
|
|
|
|
.method = HTTP_GET,
|
|
|
|
.handler = root_get_handler
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static httpd_handle_t start_webserver(void)
|
|
|
|
{
|
|
|
|
httpd_handle_t server = NULL;
|
|
|
|
|
|
|
|
// Start the httpd server
|
|
|
|
ESP_LOGI(TAG, "Starting server");
|
2020-04-05 10:27:20 +00:00
|
|
|
//Inicializacia konfiguracie pre http server
|
2020-03-20 17:01:25 +00:00
|
|
|
httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
|
2020-03-31 16:12:53 +00:00
|
|
|
conf.httpd.stack_size = 12000;
|
2020-04-05 10:27:20 +00:00
|
|
|
//pridanie certifikatu do konfiguracie
|
2020-04-06 14:37:01 +00:00
|
|
|
unsigned char *cacert_pem_start = calloc(1200,sizeof(unsigned char));//[1025];
|
|
|
|
//memset(cacert_pem_start,'\0',1025);
|
|
|
|
//char line_buffer[66];
|
|
|
|
FILE* fd = fopen("/spiffs/server.crt", "r");
|
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
long int size = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
int bytes_read = fread(cacert_pem_start, sizeof(unsigned char), size, fd);
|
|
|
|
/*
|
|
|
|
if(fd!=NULL){
|
|
|
|
while(fgets(line_buffer, sizeof(line_buffer)-1, fd)){
|
|
|
|
strcat(cacert_pem_start,line_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
conf.cacert_pem = (const unsigned char*)cacert_pem_start;
|
|
|
|
printf("%s", conf.cacert_pem);
|
|
|
|
|
|
|
|
conf.cacert_len = size+1;
|
|
|
|
|
|
|
|
|
2020-04-05 10:27:20 +00:00
|
|
|
//pridanie sukromneho kluca do konfiguracie
|
2020-04-06 14:37:01 +00:00
|
|
|
unsigned char *prvtkey_pem_start =calloc(1800,sizeof(unsigned char));
|
|
|
|
|
|
|
|
//memset(prvtkey_pem_start,'\0',2050);
|
|
|
|
// memset(line_buffer,'\0',66);
|
|
|
|
fd = fopen("/spiffs/server_key.key", "rb");
|
|
|
|
/*
|
|
|
|
if(fd!=NULL){
|
|
|
|
while(fgets(line_buffer, sizeof(line_buffer)-1, fd)){
|
|
|
|
strcat((char*)prvtkey_pem_start,line_buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
size = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
bytes_read = fread(prvtkey_pem_start, sizeof(unsigned char), size, fd);
|
|
|
|
fclose(fd);
|
2020-03-20 17:01:25 +00:00
|
|
|
conf.prvtkey_pem = prvtkey_pem_start;
|
2020-04-06 14:37:01 +00:00
|
|
|
conf.prvtkey_len = size+1;
|
2020-04-05 10:27:20 +00:00
|
|
|
//vytvorenie HTTP + SSL servera
|
2020-03-20 17:01:25 +00:00
|
|
|
esp_err_t ret = httpd_ssl_start(&server, &conf);
|
|
|
|
if (ESP_OK != ret) {
|
|
|
|
ESP_LOGI(TAG, "Error starting server!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set URI handlers
|
|
|
|
ESP_LOGI(TAG, "Registering URI handlers");
|
|
|
|
httpd_register_uri_handler(server, &root);
|
|
|
|
httpd_register_uri_handler(server, &echo);
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_webserver(httpd_handle_t server)
|
|
|
|
{
|
|
|
|
// Stop the httpd server
|
|
|
|
httpd_ssl_stop(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
|
|
|
{
|
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server) {
|
|
|
|
stop_webserver(*server);
|
|
|
|
*server = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
|
|
|
{
|
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server == NULL) {
|
|
|
|
*server = start_webserver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect(void)
|
|
|
|
{
|
|
|
|
static httpd_handle_t server = NULL;
|
|
|
|
|
|
|
|
// ESP_ERROR_CHECK(nvs_flash_init());
|
|
|
|
// ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
// ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* Register event handlers to start server when Wi-Fi or Ethernet is connected,
|
|
|
|
* and stop server when disconnection happens.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
|
2020-03-31 16:12:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-20 17:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void server_off(){
|
|
|
|
vTaskDelete(xHandleServer);
|
|
|
|
}
|
2020-03-31 16:12:53 +00:00
|
|
|
/*Registrovanie prikazov pre konzolu*/
|
2020-03-20 17:01:25 +00:00
|
|
|
|
|
|
|
void register_server(void)
|
|
|
|
{
|
|
|
|
const esp_console_cmd_t webserver_on = {
|
|
|
|
.command = "server_on",
|
|
|
|
.help = "HTTPS server",
|
|
|
|
.hint = NULL,
|
|
|
|
.func = &connect,
|
|
|
|
.argtable = NULL
|
|
|
|
};
|
|
|
|
const esp_console_cmd_t webserver_off = {
|
|
|
|
.command = "server_off",
|
|
|
|
.help = "HTTPS server",
|
|
|
|
.hint = NULL,
|
|
|
|
.func = &server_off,
|
|
|
|
.argtable = NULL
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&webserver_on));
|
|
|
|
ESP_ERROR_CHECK(esp_console_cmd_register(&webserver_off));
|
|
|
|
}
|