|
@@ -2791,31 +2791,24 @@ void HTTPS_Init()
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
-#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
#include "mbedtls/platform.h"
|
|
-#else
|
|
|
|
-#include <stdio.h>
|
|
|
|
-#define mbedtls_time time
|
|
|
|
-#define mbedtls_time_t time_t
|
|
|
|
-#define mbedtls_fprintf fprintf
|
|
|
|
-#define mbedtls_printf printf
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
-#include <stdlib.h>
|
|
|
|
-#include <string.h>
|
|
|
|
-
|
|
|
|
#include "mbedtls/entropy.h"
|
|
#include "mbedtls/entropy.h"
|
|
#include "mbedtls/ctr_drbg.h"
|
|
#include "mbedtls/ctr_drbg.h"
|
|
#include "mbedtls/certs.h"
|
|
#include "mbedtls/certs.h"
|
|
#include "mbedtls/x509.h"
|
|
#include "mbedtls/x509.h"
|
|
-#include "mbedtls/ssl.h"
|
|
|
|
|
|
+//#include "mbedtls/ssl.h"
|
|
#include "mbedtls/net_sockets.h"
|
|
#include "mbedtls/net_sockets.h"
|
|
#include "mbedtls/error.h"
|
|
#include "mbedtls/error.h"
|
|
#include "mbedtls/debug.h"
|
|
#include "mbedtls/debug.h"
|
|
|
|
+#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
+#include "mbedtls_time.h"
|
|
|
|
+#include "mbedtls_debug.h"
|
|
|
|
|
|
-#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
-#include "mbedtls/ssl_cache.h"
|
|
|
|
-#endif
|
|
|
|
|
|
+#include "FreeRTOS.h"
|
|
|
|
+#include "task.h"
|
|
|
|
+
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <string.h>
|
|
|
|
|
|
#define HTTP_RESPONSE \
|
|
#define HTTP_RESPONSE \
|
|
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
|
|
"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
|
|
@@ -2831,69 +2824,94 @@ mbedtls_ssl_config conf;
|
|
mbedtls_x509_crt srvcert;
|
|
mbedtls_x509_crt srvcert;
|
|
mbedtls_pk_context pkey;
|
|
mbedtls_pk_context pkey;
|
|
|
|
|
|
|
|
+char CookieBuf[50];
|
|
|
|
+char *CookiePtr = NULL;
|
|
|
|
+char name[MAX_WEB_COOKIE_LEN];
|
|
|
|
+char id[MAX_WEB_COOKIE_LEN];
|
|
|
|
+uint8_t nameLen = 0, idLen = 0;
|
|
|
|
+uint8_t user_id; // Id of currently logged-in user
|
|
|
|
+
|
|
#define HEAP_SIZE (1u << 14) // 16k
|
|
#define HEAP_SIZE (1u << 14) // 16k
|
|
unsigned char malloc_buf[HEAP_SIZE];
|
|
unsigned char malloc_buf[HEAP_SIZE];
|
|
|
|
|
|
|
|
+//
|
|
|
|
+void Cockie(void)
|
|
|
|
+{
|
|
|
|
+ receiveBuf[receivedBufLen] = '\0';
|
|
|
|
+ // printf("receive %s \r\n", receiveBuf);
|
|
|
|
+ // Get cookie "uname" value
|
|
|
|
+ CookiePtr = strstr(receiveBuf, "uname=");
|
|
|
|
+ strncpy(CookieBuf, CookiePtr, 50);
|
|
|
|
+ printf("********CookieBuf1= %s\r\n", CookieBuf);
|
|
|
|
+ memset(name, 0, MAX_WEB_COOKIE_LEN);
|
|
|
|
+ GetCookieValue(CookieBuf, "uname=", name, &nameLen);
|
|
|
|
+ printf("********CookieBuf2= %s\r\n", CookieBuf);
|
|
|
|
+ printf("********uname= %s\r\n", name);
|
|
|
|
+
|
|
|
|
+ // Get cookie "id" value
|
|
|
|
+ CookiePtr = strstr(receiveBuf, "id=");
|
|
|
|
+ strncpy(CookieBuf, CookiePtr, 50);
|
|
|
|
+ printf("********CookieBuf1= %s\r\n", CookieBuf);
|
|
|
|
+ memset(id, 0, MAX_WEB_COOKIE_LEN);
|
|
|
|
+ GetCookieValue(CookieBuf, "id=", id, &idLen);
|
|
|
|
+}
|
|
|
|
|
|
|
|
+//
|
|
|
|
+void getAuthenticatedState(void)
|
|
|
|
+{
|
|
|
|
+ seclevel = 0xFF;
|
|
|
|
+ for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
|
|
|
|
+ HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
|
|
|
|
+ if (strncmp(id, CookieBuf, idLen) == 0 ) {
|
|
|
|
+ GetUserLevelInt(user_id, &seclevel);
|
|
|
|
+ Authenticated = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ Authenticated = false;
|
|
|
|
+ seclevel = 0xFF;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
-static void my_debug(void *ctx, int level, const char *file, int line, const char *str);
|
|
|
|
-
|
|
|
|
|
|
+//
|
|
void ssl_server(void *pvParameters)
|
|
void ssl_server(void *pvParameters)
|
|
{
|
|
{
|
|
|
|
+ SSL_SERVER_STATE ssl_state = SSL_ACCEPT;
|
|
|
|
+ char* sendPtr;
|
|
|
|
+ uint32_t sendBufSize;
|
|
int ret, len;
|
|
int ret, len;
|
|
|
|
+ struct fs_file file = {0, 0};
|
|
|
|
|
|
#ifdef MBEDTLS_MEMORY_BUFFER_ALLOC_C
|
|
#ifdef MBEDTLS_MEMORY_BUFFER_ALLOC_C
|
|
mbedtls_memory_buffer_alloc_init(malloc_buf, sizeof(malloc_buf));
|
|
mbedtls_memory_buffer_alloc_init(malloc_buf, sizeof(malloc_buf));
|
|
#endif
|
|
#endif
|
|
mbedtls_net_init( &listen_fd );
|
|
mbedtls_net_init( &listen_fd );
|
|
mbedtls_net_init( &client_fd );
|
|
mbedtls_net_init( &client_fd );
|
|
-
|
|
|
|
mbedtls_ssl_init( &ssl );
|
|
mbedtls_ssl_init( &ssl );
|
|
mbedtls_ssl_config_init( &conf );
|
|
mbedtls_ssl_config_init( &conf );
|
|
-#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
- mbedtls_ssl_cache_init( &cache );
|
|
|
|
-#endif
|
|
|
|
mbedtls_x509_crt_init( &srvcert );
|
|
mbedtls_x509_crt_init( &srvcert );
|
|
- //mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
|
|
|
mbedtls_pk_init( &pkey );
|
|
mbedtls_pk_init( &pkey );
|
|
mbedtls_entropy_init( &entropy );
|
|
mbedtls_entropy_init( &entropy );
|
|
- //mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
|
|
-
|
|
|
|
|
|
+ mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
|
|
+ mbedtls_platform_set_time(&MBEDTLS_GetTime);
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
mbedtls_debug_set_threshold(DEBUG_LEVEL);
|
|
mbedtls_debug_set_threshold(DEBUG_LEVEL);
|
|
- mbedtls_ssl_conf_dbg(&conf, my_debug, NULL);
|
|
|
|
|
|
+ mbedtls_ssl_conf_dbg(&conf, MBEDTLS_Debug, NULL);
|
|
#endif
|
|
#endif
|
|
-
|
|
|
|
|
|
+
|
|
// 1. Load the certificates and private RSA key
|
|
// 1. Load the certificates and private RSA key
|
|
mbedtls_printf( "\r\n . Loading the server cert. and key..." );
|
|
mbedtls_printf( "\r\n . Loading the server cert. and key..." );
|
|
-
|
|
|
|
- // This demonstration program uses embedded test certificates.
|
|
|
|
- // Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
|
|
|
|
- // server and CA certificates, as well as mbedtls_pk_parse_keyfile().
|
|
|
|
- //ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_ca_crt, mbedtls_test_ca_crt_len );
|
|
|
|
- //ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt, strlen(test_srv_crt) );
|
|
|
|
- //ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) sSettings.our_srv_crt, strlen(sSettings.our_srv_crt) );
|
|
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, mbedtls_test_srv_crt_len );
|
|
ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt, mbedtls_test_srv_crt_len );
|
|
if( ret != 0 )
|
|
if( ret != 0 )
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_x509_crt_parse returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_x509_crt_parse returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
- }
|
|
|
|
-/*
|
|
|
|
- ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem, mbedtls_test_cas_pem_len );
|
|
|
|
- if( ret != 0 )
|
|
|
|
- {
|
|
|
|
- mbedtls_printf( " failed\r\n ! mbedtls_x509_crt_parse returned %d\r\n", ret );
|
|
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
- */
|
|
|
|
- //ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_ca_key, mbedtls_test_ca_key_len, NULL, 0 );
|
|
|
|
- //ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) test_srv_key, strlen(test_srv_key), NULL, 0 );
|
|
|
|
|
|
+
|
|
ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, mbedtls_test_srv_key_len, NULL, 0 );
|
|
ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key, mbedtls_test_srv_key_len, NULL, 0 );
|
|
if( ret != 0 )
|
|
if( ret != 0 )
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_pk_parse_key returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_pk_parse_key returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
@@ -2904,67 +2922,123 @@ void ssl_server(void *pvParameters)
|
|
if((ret = mbedtls_net_bind(&listen_fd, NULL, "443", MBEDTLS_NET_PROTO_TCP )) != 0)
|
|
if((ret = mbedtls_net_bind(&listen_fd, NULL, "443", MBEDTLS_NET_PROTO_TCP )) != 0)
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
// 3. Seed the RNG
|
|
// 3. Seed the RNG
|
|
mbedtls_printf( " . Seeding the random number generator..." );
|
|
mbedtls_printf( " . Seeding the random number generator..." );
|
|
-
|
|
|
|
if((ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( (char *)pers))) != 0)
|
|
if((ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( (char *)pers))) != 0)
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ctr_drbg_seed returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ctr_drbg_seed returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
-
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
// 4. Setup stuff
|
|
// 4. Setup stuff
|
|
mbedtls_printf( " . Setting up the SSL data...." );
|
|
mbedtls_printf( " . Setting up the SSL data...." );
|
|
-
|
|
|
|
if( ( ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
if( ( ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_config_defaults returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_config_defaults returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
|
|
|
-#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
- mbedtls_ssl_conf_session_cache(&conf, &cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set);
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
|
|
mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
|
|
if( ( ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey ) ) != 0)
|
|
if( ( ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey ) ) != 0)
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_conf_own_cert returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_conf_own_cert returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
|
|
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
|
|
{
|
|
{
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_setup returned %d\r\n", ret );
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_setup returned %d\r\n", ret );
|
|
- goto exit;
|
|
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
}
|
|
}
|
|
-
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
-reset:
|
|
|
|
-#ifdef MBEDTLS_ERROR_C
|
|
|
|
- if( ret != 0 )
|
|
|
|
|
|
+ for (;;) {
|
|
|
|
+ switch (ssl_state)
|
|
{
|
|
{
|
|
- uint8_t error_buf[100];
|
|
|
|
- mbedtls_strerror( ret, (char *)error_buf, 100 );
|
|
|
|
- mbedtls_printf("Last error was: %d - %s\r\n", ret, error_buf );
|
|
|
|
|
|
+ case SSL_ACCEPT :
|
|
|
|
+
|
|
|
|
+ mbedtls_net_free( &client_fd );
|
|
|
|
+ mbedtls_ssl_session_reset( &ssl );
|
|
|
|
+ mbedtls_printf( " . Waiting for a remote connection ...\r\n" );
|
|
|
|
+
|
|
|
|
+ if((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) != 0) {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! mbedtls_net_accept returned %d\r\n", ret );
|
|
|
|
+ ssl_state = SSL_CRITICAL_ERROR;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
|
|
+ mbedtls_printf( " ok\r\n" );
|
|
|
|
+ ssl_state = SSL_HANDSHAKE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSL_HANDSHAKE :
|
|
|
|
+
|
|
|
|
+ mbedtls_printf( " . Performing the SSL/TLS handshake..." );
|
|
|
|
+ while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
|
|
|
|
+ {
|
|
|
|
+ if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! mbedtls_ssl_handshake returned %d\r\n", ret );
|
|
|
|
+ ssl_state = SSL_ACCEPT;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ mbedtls_printf( " ok\r\n" );
|
|
|
|
+ ssl_state = SSL_READ;
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSL_READ :
|
|
|
|
+
|
|
|
|
+ SSL_ReadRoutine(&ssl, (unsigned char*)receiveBuf);
|
|
|
|
+ ssl_state = SSL_PROCESSING;
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSL_PROCESSING :
|
|
|
|
+
|
|
|
|
+ SSL_ProcessingRoutine(&sendPtr, &sendBufSize);
|
|
|
|
+ ssl_state = SSL_WRITE;
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSL_WRITE :
|
|
|
|
+ ssl_state = SSL_WriteRoutine(&ssl, sendPtr, sendBufSize);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSL_CRITICAL_ERROR:
|
|
|
|
+
|
|
|
|
+ mbedtls_x509_crt_free( &srvcert );
|
|
|
|
+ mbedtls_pk_free( &pkey );
|
|
|
|
+ mbedtls_ssl_free( &ssl );
|
|
|
|
+ mbedtls_ssl_config_free( &conf );
|
|
|
|
+ mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
|
|
+ mbedtls_entropy_free( &entropy );
|
|
|
|
+
|
|
|
|
+ vTaskDelete(NULL);
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+#if 0
|
|
|
|
+reset:
|
|
mbedtls_net_free( &client_fd );
|
|
mbedtls_net_free( &client_fd );
|
|
mbedtls_ssl_session_reset( &ssl );
|
|
mbedtls_ssl_session_reset( &ssl );
|
|
|
|
|
|
// 5. Wait until a client connects
|
|
// 5. Wait until a client connects
|
|
- mbedtls_printf( " . Waiting for a remote connection ..." );
|
|
|
|
|
|
+ mbedtls_printf( " . Waiting for a remote connection ...\r\n" );
|
|
|
|
|
|
if((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) != 0)
|
|
if((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) != 0)
|
|
{
|
|
{
|
|
@@ -2973,7 +3047,6 @@ reset:
|
|
}
|
|
}
|
|
|
|
|
|
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
-
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
// 6. Handshake
|
|
// 6. Handshake
|
|
@@ -2987,18 +3060,15 @@ reset:
|
|
goto reset;
|
|
goto reset;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
- /*
|
|
|
|
- * 7. Read the HTTP Request
|
|
|
|
- */
|
|
|
|
|
|
+ // 7. Read the HTTP Request
|
|
mbedtls_printf( " < Read from client:" );
|
|
mbedtls_printf( " < Read from client:" );
|
|
do
|
|
do
|
|
{
|
|
{
|
|
len = sizeof( receiveBuf ) - 1;
|
|
len = sizeof( receiveBuf ) - 1;
|
|
memset( receiveBuf, 0, sizeof( receiveBuf ) );
|
|
memset( receiveBuf, 0, sizeof( receiveBuf ) );
|
|
- ret = mbedtls_ssl_read( &ssl, receiveBuf, len );
|
|
|
|
|
|
+ ret = mbedtls_ssl_read( &ssl, (unsigned char*)receiveBuf, len );
|
|
|
|
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
{
|
|
{
|
|
@@ -3025,19 +3095,28 @@ reset:
|
|
}
|
|
}
|
|
|
|
|
|
len = ret;
|
|
len = ret;
|
|
- mbedtls_printf( " %d bytes read\r\n%s", len, (char *) receiveBuf );
|
|
|
|
|
|
+ //mbedtls_printf( " %d bytes read\r\n%s", len, (char *) receiveBuf );
|
|
|
|
+ mbedtls_printf( " %d bytes read\r\n", len);
|
|
|
|
|
|
if( ret > 0 )
|
|
if( ret > 0 )
|
|
- {
|
|
|
|
break;
|
|
break;
|
|
- }
|
|
|
|
|
|
+
|
|
} while(1);
|
|
} while(1);
|
|
|
|
|
|
|
|
+
|
|
|
|
+ Cockie();
|
|
|
|
+ getAuthenticatedState();
|
|
|
|
+
|
|
// 8. Write the 200 Response
|
|
// 8. Write the 200 Response
|
|
mbedtls_printf( " > Write to client:" );
|
|
mbedtls_printf( " > Write to client:" );
|
|
- len = sprintf( (char *) receiveBuf, HTTP_RESPONSE, mbedtls_ssl_get_ciphersuite( &ssl ) );
|
|
|
|
|
|
+ //len = sprintf( (char *) receiveBuf, HTTP_RESPONSE, mbedtls_ssl_get_ciphersuite( &ssl ) );
|
|
|
|
+
|
|
|
|
+ fs_open("/index.html", &file);
|
|
|
|
+ SSL_Send(&ssl, file.data, file.len);
|
|
|
|
|
|
- while( ( ret = mbedtls_ssl_write( &ssl, receiveBuf, len ) ) <= 0 )
|
|
|
|
|
|
+/*
|
|
|
|
+ //while( ( ret = mbedtls_ssl_write( &ssl, (unsigned char*)receiveBuf, len ) ) <= 0 )
|
|
|
|
+ while( ( ret = mbedtls_ssl_write( &ssl, (unsigned char*)file.data, file.len ) ) <= 0 )
|
|
{
|
|
{
|
|
if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
|
if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
|
{
|
|
{
|
|
@@ -3051,10 +3130,11 @@ reset:
|
|
goto exit;
|
|
goto exit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+*/
|
|
|
|
+
|
|
len = ret;
|
|
len = ret;
|
|
- mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) receiveBuf );
|
|
|
|
-
|
|
|
|
|
|
+ //mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) receiveBuf );
|
|
|
|
+ mbedtls_printf( " %d bytes written\r\n", len);
|
|
mbedtls_printf( " . Closing the connection..." );
|
|
mbedtls_printf( " . Closing the connection..." );
|
|
|
|
|
|
while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
|
|
while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
|
|
@@ -3070,37 +3150,178 @@ reset:
|
|
|
|
|
|
ret = 0;
|
|
ret = 0;
|
|
goto reset;
|
|
goto reset;
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- vTaskDelete(NULL);
|
|
|
|
-
|
|
|
|
|
|
+
|
|
exit:
|
|
exit:
|
|
- //mbedtls_net_free( &client_fd );
|
|
|
|
- //mbedtls_net_free( &listen_fd );
|
|
|
|
-
|
|
|
|
mbedtls_x509_crt_free( &srvcert );
|
|
mbedtls_x509_crt_free( &srvcert );
|
|
mbedtls_pk_free( &pkey );
|
|
mbedtls_pk_free( &pkey );
|
|
mbedtls_ssl_free( &ssl );
|
|
mbedtls_ssl_free( &ssl );
|
|
mbedtls_ssl_config_free( &conf );
|
|
mbedtls_ssl_config_free( &conf );
|
|
-#if defined(MBEDTLS_SSL_CACHE_C)
|
|
|
|
- mbedtls_ssl_cache_free( &cache );
|
|
|
|
-#endif
|
|
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
mbedtls_entropy_free( &entropy );
|
|
mbedtls_entropy_free( &entropy );
|
|
-
|
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
|
vTaskDelete(NULL);
|
|
|
|
+#endif
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @brief Initialize the HTTPS server (start its thread)
|
|
|
|
+ */
|
|
|
|
+void HTTPS_Init()
|
|
|
|
+{
|
|
|
|
+ char buf[MAX_WEB_COOKIE_LEN];
|
|
|
|
+ uint8_t user_id;
|
|
|
|
+
|
|
|
|
+ for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
|
|
|
|
+ // Flush user cookie by random value
|
|
|
|
+ sprintf(buf, "%X", (unsigned int)GetRandomNumber());
|
|
|
|
+ HTTP_SetUserCookie(buf, user_id);
|
|
|
|
+ // Create user logout timers
|
|
|
|
+ users[user_id].LogoutTimer = xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
|
|
|
|
+ }
|
|
|
|
+ RepeatLoginTimer = xTimerCreate("LoginTmr", REPEAT_LOGIN_TIME, pdFALSE, ( void * ) 0, LoginTimerCallback);
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
//
|
|
-static void my_debug(void *ctx, int level, const char *file, int line, const char *str)
|
|
|
|
|
|
+void SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
|
|
{
|
|
{
|
|
- ((void) level);
|
|
|
|
- printf("%s:%04d: %s\r", file, line, str );
|
|
|
|
|
|
+ int ret, len;
|
|
|
|
+
|
|
|
|
+ mbedtls_printf( " < Read from client:" );
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ len = sizeof( recvBuf ) - 1;
|
|
|
|
+ memset( recvBuf, 0, sizeof( recvBuf ) );
|
|
|
|
+ ret = mbedtls_ssl_read(ssl, recvBuf, len );
|
|
|
|
+
|
|
|
|
+ if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
|
+ {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if( ret <= 0 )
|
|
|
|
+ {
|
|
|
|
+ switch( ret )
|
|
|
|
+ {
|
|
|
|
+ case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
|
|
|
|
+ mbedtls_printf( " connection was closed gracefully\r\n" );
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case MBEDTLS_ERR_NET_CONN_RESET:
|
|
|
|
+ mbedtls_printf( " connection was reset by peer\r\n" );
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ mbedtls_printf( " mbedtls_ssl_read returned -0x%x\r\n", -ret );
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ len = ret;
|
|
|
|
+ //mbedtls_printf( " %d bytes read\r\n%s", len, (char *) receiveBuf );
|
|
|
|
+ mbedtls_printf( " %d bytes read\r\n", len);
|
|
|
|
+
|
|
|
|
+ if( ret > 0 )
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ } while(1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+//
|
|
|
|
+SSL_SERVER_STATE SSL_WriteRoutine(mbedtls_ssl_context *ssl, char *data, int datalen)
|
|
|
|
+{
|
|
|
|
+ int ret;
|
|
|
|
+
|
|
|
|
+ mbedtls_printf( " > Write to client:" );
|
|
|
|
+
|
|
|
|
+ while( ( ret = mbedtls_ssl_write(ssl, data, datalen) ) <= 0 )
|
|
|
|
+ {
|
|
|
|
+ if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! peer closed the connection\r\n" );
|
|
|
|
+ return SSL_ACCEPT;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! mbedtls_ssl_write returned %d\r\n", ret );
|
|
|
|
+ return SSL_CRITICAL_ERROR;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mbedtls_printf( " %d bytes written\r\n", ret);
|
|
|
|
+ mbedtls_printf( " . Closing the connection..." );
|
|
|
|
|
|
|
|
+ while( ( ret = mbedtls_ssl_close_notify( ssl ) ) < 0 )
|
|
|
|
+ {
|
|
|
|
+ if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! mbedtls_ssl_close_notify returned %d\r\n", ret );
|
|
|
|
+ return SSL_ACCEPT;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mbedtls_printf( " ok\r\n" );
|
|
|
|
+
|
|
|
|
+ ret = 0;
|
|
|
|
+ return SSL_ACCEPT;
|
|
|
|
+}
|
|
|
|
|
|
|
|
+//
|
|
|
|
+void SSL_ProcessingRoutine(char** send, uint32_t* sendLen)
|
|
|
|
+{
|
|
|
|
+ struct fs_file file = {0, 0};
|
|
|
|
+
|
|
|
|
+ Cockie();
|
|
|
|
+ getAuthenticatedState();
|
|
|
|
+
|
|
|
|
+ fs_open("/index.html", &file);
|
|
|
|
+
|
|
|
|
+ *send = file.data;
|
|
|
|
+ *sendLen = file.len;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+#if 1
|
|
|
|
+void SSL_Send( mbedtls_ssl_context *ssl, char *data, int datalen )
|
|
|
|
+{
|
|
|
|
+ //int index = 0;
|
|
|
|
+ //int k = 0;
|
|
|
|
+ //int lastframe, nbrframes;
|
|
|
|
+ int ret, len;
|
|
|
|
+/*
|
|
|
|
+ nbrframes = datalen / FRAME_SIZE;
|
|
|
|
+
|
|
|
|
+ // Send nbrframes frames
|
|
|
|
+ while(nbrframes > 0)
|
|
|
|
+ {
|
|
|
|
+ index = k * FRAME_SIZE;
|
|
|
|
+ ssl_write( ssl, (unsigned char *)(data + index), FRAME_SIZE );
|
|
|
|
+ nbrframes--;
|
|
|
|
+ k++;
|
|
|
|
+ }
|
|
|
|
+ // Send the last frame
|
|
|
|
+ index = k * FRAME_SIZE;
|
|
|
|
+ lastframe = datalen % FRAME_SIZE ;
|
|
|
|
+ ssl_write( ssl, (unsigned char *)(data + index), lastframe );
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ while( ( ret = mbedtls_ssl_write( ssl, (unsigned char*)data, datalen ) ) <= 0 )
|
|
|
|
+ {
|
|
|
|
+ if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! peer closed the connection\r\n" );
|
|
|
|
+ goto reset;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
|
+ {
|
|
|
|
+ mbedtls_printf( " failed\r\n ! mbedtls_ssl_write returned %d\r\n", ret );
|
|
|
|
+ goto exit;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+*/
|
|
|
|
+}
|
|
|
|
+#endif
|
|
|
|
|