Parcourir la source

Рефакторинг web server

TelenkovDmitry il y a 7 ans
Parent
commit
dd3f1753c1

+ 317 - 96
modules/HTTP_Server/http_server.c

@@ -2791,31 +2791,24 @@ void HTTPS_Init()
 
 // -----------------------------------------------------------------------------
 
-#if defined(MBEDTLS_PLATFORM_C)
 #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/ctr_drbg.h"
 #include "mbedtls/certs.h"
 #include "mbedtls/x509.h"
-#include "mbedtls/ssl.h"
+//#include "mbedtls/ssl.h"
 #include "mbedtls/net_sockets.h"
 #include "mbedtls/error.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 \
     "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_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
 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)
 {
+  SSL_SERVER_STATE ssl_state = SSL_ACCEPT;
+  char* sendPtr;
+  uint32_t sendBufSize;
   int ret, len;
+  struct fs_file file = {0, 0};
   
 #ifdef MBEDTLS_MEMORY_BUFFER_ALLOC_C
   mbedtls_memory_buffer_alloc_init(malloc_buf, sizeof(malloc_buf));
 #endif
   mbedtls_net_init( &listen_fd );
   mbedtls_net_init( &client_fd );
-  
   mbedtls_ssl_init( &ssl );
   mbedtls_ssl_config_init( &conf );
-#if defined(MBEDTLS_SSL_CACHE_C)
-  mbedtls_ssl_cache_init( &cache );
-#endif
   mbedtls_x509_crt_init( &srvcert );
-  //mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
   mbedtls_pk_init( &pkey );
   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)
   mbedtls_debug_set_threshold(DEBUG_LEVEL);
-  mbedtls_ssl_conf_dbg(&conf, my_debug, NULL);
+  mbedtls_ssl_conf_dbg(&conf, MBEDTLS_Debug, NULL);
 #endif
-  
+   
   // 1. Load the certificates and private RSA 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 );
   if( ret != 0 )
   {
     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 );
   if( ret != 0 )
   {
     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" );
@@ -2904,67 +2922,123 @@ void ssl_server(void *pvParameters)
   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 );
-    goto exit;
+    ssl_state = SSL_CRITICAL_ERROR;
   }
 
   mbedtls_printf( " ok\r\n" );
   
   // 3. Seed the RNG
   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)
   {
     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" );
   
   // 4. Setup stuff
   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)
   {
     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);
 
-#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);
   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 );
-    goto exit;
+    ssl_state = SSL_CRITICAL_ERROR;
   }
 
   if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
   {
     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" );
   
-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_ssl_session_reset( &ssl );
   
   // 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)
   {
@@ -2973,7 +3047,6 @@ reset:
   }
 
   mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
-
   mbedtls_printf( " ok\r\n" );
   
   // 6. Handshake
@@ -2987,18 +3060,15 @@ reset:
       goto reset;
     }
   }
-
   mbedtls_printf( " ok\r\n" );
   
-  /*
-   * 7. Read the HTTP Request
-   */
+  // 7. Read the HTTP Request
   mbedtls_printf( "  < Read from client:" );
   do
   {
     len = sizeof( receiveBuf ) - 1;
     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 )
 	{
@@ -3025,19 +3095,28 @@ reset:
     }
 
     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 )
-	{
       break;
-	}
+
   } while(1);  
   
+  
+  Cockie();
+  getAuthenticatedState();
+  
   // 8. Write the 200 Response
   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 )
     {
@@ -3051,10 +3130,11 @@ reset:
       goto exit;
     }
   }
-
+*/
+  
   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..." );
 
   while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
@@ -3070,37 +3150,178 @@ reset:
 
   ret = 0;
   goto reset;  
-  
-  
-  
-  vTaskDelete(NULL);
-  
+      
 exit:
-  //mbedtls_net_free( &client_fd );
-  //mbedtls_net_free( &listen_fd );
-
   mbedtls_x509_crt_free( &srvcert );
   mbedtls_pk_free( &pkey );
   mbedtls_ssl_free( &ssl );
   mbedtls_ssl_config_free( &conf );
-#if defined(MBEDTLS_SSL_CACHE_C)
-  mbedtls_ssl_cache_free( &cache );
-#endif
   mbedtls_ctr_drbg_free( &ctr_drbg );
   mbedtls_entropy_free( &entropy );
-
   
   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
 

+ 24 - 7
modules/HTTP_Server/http_server.h

@@ -2,6 +2,22 @@
 #include "lwip/arch.h"
 #include "lwip/api.h"
 
+#include "mbedtls/ssl.h"
+
+#define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
+#define FRAME_SIZE      (1000)
+
+typedef enum
+{
+  SSL_ACCEPT = 0,
+  SSL_CRITICAL_ERROR,
+  SSL_HANDSHAKE,
+  SSL_READ,
+  SSL_PROCESSING,
+  SSL_WRITE,
+  
+} SSL_SERVER_STATE;
+
 typedef enum
 {
   SET_PAGE_IDLE = 0,
@@ -132,8 +148,6 @@ void HTTP_ReplaceSimbol(char *str, char sim1, char sim2);
  extern "C" {
 #endif
 
-/* Includes ------------------------------------------------------------------*/
-//#include "polarssl/ssl.h"
 
 /* Exported types ------------------------------------------------------------*/
 typedef struct
@@ -141,16 +155,19 @@ typedef struct
   uint32_t State;
 }rng_state;
 
-/* Exported constants --------------------------------------------------------*/
-/* Exported macro ------------------------------------------------------------*/
-/* Exported functions ------------------------------------------------------- */
+void Cockie(void);
+void getAuthenticatedState(void);
 void ssl_server(void *pvParameters);
-//void ssl_DynPage(ssl_context *ssl);
-//void ssl_sendframes(ssl_context *ssl, char *data, int datalen);
+
+
 int RandVal(void* arg);
 void HTTP_SendHistory(void);
 void HTTP_SendLog(void);
 void HTTPS_Init();
+void SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf);
+void SSL_ProcessingRoutine(char** send, uint32_t* sendLen);
+SSL_SERVER_STATE SSL_WriteRoutine(mbedtls_ssl_context *ssl, char *data, int datalen);
+void SSL_Send(mbedtls_ssl_context *ssl, char *data, int datalen);
 
 #ifdef __cplusplus
 }

+ 3 - 22
modules/mbedtls_api/mbedtls_config.h

@@ -50,12 +50,8 @@
 #define MBEDTLS_ECP_DP_SECP256R1_ENABLED
 #define MBEDTLS_ECP_DP_SECP384R1_ENABLED
 #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
-//#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
-//#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED      //ошибка на 11 этапе
 
 #define MBEDTLS_SSL_PROTO_TLS1_2
-//#define MBEDTLS_SSL_PROTO_TLS1 
-//#define MBEDTLS_SSL_PROTO_TLS1_1
 
 /* mbed TLS modules */
 #define MBEDTLS_AES_C
@@ -75,31 +71,21 @@
 #define MBEDTLS_PK_C
 #define MBEDTLS_PK_PARSE_C
 #define MBEDTLS_SHA256_C
-//#define MBEDTLS_SHA512_C
 #define MBEDTLS_SSL_CLI_C
 #define MBEDTLS_SSL_SRV_C
 #define MBEDTLS_SSL_TLS_C
 #define MBEDTLS_X509_CRT_PARSE_C
 #define MBEDTLS_X509_USE_C
-
 #define MBEDTLS_MD4_C
 #define MBEDTLS_X509_CRL_PARSE_C
-//#define MBEDTLS_FS_IO
 
 // User
-//#define MBEDTLS_RSA_C
-//#define MBEDTLS_RSA_NO_CRT
 #define MBEDTLS_PKCS1_V15
-//#define MBEDTLS_PKCS1_V21
 #define MBEDTLS_DHM_C
 #define MBEDTLS_SHA1_C
 #define MBEDTLS_MD5_C
 #define MBEDTLS_CIPHER_MODE_CBC
 
-//#define MBEDTLS_ENABLE_WEAK_CIPHERSUITES
-//#define MBEDTLS_CIPHER_MODE_STREAM
-//#define MBEDTLS_CIPHER_NULL_CIPHER
-
 #define MBEDTLS_DES_C
 
 /* For test certificates */
@@ -145,28 +131,24 @@
  */
 #define MBEDTLS_SSL_MAX_CONTENT_LEN             2048
 
-//#define MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE
 #define MBEDTLS_CAMELLIA_C
 #define MBEDTLS_BLOWFISH_C
 
-#ifdef USE_LCD
-//#include "lcd_log.h"
 #define MBEDTLS_PLATFORM_C
 #define MBEDTLS_MEMORY_BUFFER_ALLOC_C
 #define MBEDTLS_PLATFORM_MEMORY
 #define MBEDTLS_MEMORY_DEBUG
 #define MBEDTLS_SELF_TEST
-////#define MBEDTLS_PLATFORM_PRINTF_MACRO LCD_UsrLog
 #define MBEDTLS_PLATFORM_PRINTF_MACRO printf
-#endif
+
 
 /* Customize the entropy data generation */
 #define MBEDTLS_NO_PLATFORM_ENTROPY
 #define MBEDTLS_ENTROPY_HARDWARE_ALT
-//#define MBEDTLS_PLATFORM_TIME_ALT
+#define MBEDTLS_PLATFORM_TIME_ALT
 
 // User debug options
-#define MBEDTLS_DEBUG_C
+//#define MBEDTLS_DEBUG_C
 #define DEBUG_LEVEL   3  // 0 No debug
                          // 1 Error
                          // 2 State change
@@ -178,7 +160,6 @@
 #define MBEDTLS_X509_CREATE_C     
 #define MBEDTLS_PK_WRITE_C      
 #define MBEDTLS_PEM_WRITE_C        
-//#define MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO
       
 #define UNUSED(x) ((void)(x))      
       

+ 10 - 0
modules/mbedtls_api/mbedtls_debug.c

@@ -0,0 +1,10 @@
+#include "mbedtls_debug.h"
+#include "mbedtls/debug.h"
+
+#include <stdio.h>
+
+void MBEDTLS_Debug(void *ctx, int level, const char *file, int line, const char *str)
+{
+    ((void) level);
+    printf("%s:%04d: %s\r", file, line, str );
+}

+ 23 - 0
modules/mbedtls_api/mbedtls_debug.h

@@ -0,0 +1,23 @@
+/********************************* (C) РОТЕК ***********************************
+ * @module  
+ * @file    
+ * @version 1.0.0
+ * @date    XX.XX.XXXX
+ * $brief   
+ *******************************************************************************
+ * @history     Version  Author         Comment
+ * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
+ *******************************************************************************
+ */
+
+/* Define to prevent recursive  ----------------------------------------------*/
+#ifndef __MBEDTLS_DEBUG_H
+#define __MBEDTLS_DEBUG_H
+   
+#include "stm32f4xx.h"
+
+void MBEDTLS_Debug(void *ctx, int level, const char *file, int line, const char *str);
+
+#endif /* #ifndef __MBEDTLS_DEBUG_H */
+
+/********************************* (C) РОТЕК **********************************/

+ 11 - 0
modules/mbedtls_api/mbedtls_time.c

@@ -0,0 +1,11 @@
+#include "mbedtls/platform_time.h"
+#include "mbedtls_time.h"
+#include "rtc.h"
+
+uint32_t MBEDTLS_GetTime(mbedtls_time_t* time)
+{
+    TM_RTC_t currentTime;
+  
+    TM_RTC_GetDateTime(&currentTime, TM_RTC_Format_BIN);
+    return TM_RTC_GetUnixTimeStamp(&currentTime);
+}

+ 26 - 0
modules/mbedtls_api/mbedtls_time.h

@@ -0,0 +1,26 @@
+/********************************* (C) РОТЕК ***********************************
+ * @module  cert_req
+ * @file    cert_req
+ * @version 1.0.0
+ * @date    XX.XX.XXXX
+ * $brief   cert_req
+ *******************************************************************************
+ * @history     Version  Author         Comment
+ * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
+ *******************************************************************************
+ */
+
+/* Define to prevent recursive  ----------------------------------------------*/
+#ifndef __MBEDTLS_TIME_H
+#define __MBEDTLS_TIME_H
+   
+#include "stm32f4xx.h"
+
+uint32_t MBEDTLS_GetTime(mbedtls_time_t* time);
+
+
+
+
+#endif /* #ifndef __MBEDTLS_TIME_H */
+
+/********************************* (C) РОТЕК **********************************/

+ 0 - 58
modules/mbedtls_api/net_sockets.c

@@ -71,7 +71,6 @@
 
 #include "main.h"
 
-static struct netif netif;
 static int initialized = 0;
 struct sockaddr_storage client_addr;
 
@@ -81,64 +80,7 @@ static int net_would_block( const mbedtls_net_context *ctx );
  */
 void mbedtls_net_init( mbedtls_net_context *ctx )
 {
-  ip4_addr_t addr;
-  ip4_addr_t netmask;
-  ip4_addr_t gw;
-  uint32_t start;
- 
   ctx->fd = -1;
-# if 0 
-  if (initialized != 0)
-    return;
-  
-  tcpip_init(NULL, NULL);
-
-  /* IP default settings, to be overridden by DHCP */  
-
-  IP4_ADDR(&addr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
-  IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
-  IP4_ADDR(&netmask, MASK_ADDR0, MASK_ADDR1, MASK_ADDR2, MASK_ADDR3);
-  
-  /* add the network interface */    
-  netif_add(&netif, &addr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
-  
-  /* register the default network interface. */
-  netif_set_default(&netif);
- 
-  if (netif_is_link_up(&netif))
-  {
-    netif_set_up(&netif);
-  }
-  else
-  {
-    netif_set_down(&netif);
-  }
-#ifdef USE_DHCP
-  dhcp_start(&netif);
-#endif
-  osDelay(500);
-
-  start = HAL_GetTick();
-  
-  while((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
-  {
-  }
-  
-  if (netif.ip_addr.addr == 0)
-  {
-    printf(" Failed to get ip address! Please check your network configuration.\n");
-    Error_Handler();
-  }
-  else
-  {
-     printf("\nIpAdress = %lu.%lu.%lu.%lu\n", (netif.ip_addr.addr & 0xff), ((netif.ip_addr.addr >> 8) & 0xff)
-                                        , ((netif.ip_addr.addr >> 16) & 0xff), ((netif.ip_addr.addr >> 24)& 0xff));
-#ifdef USE_DHCP
-    dhcp_stop(&netif);
-#endif
-    initialized = 1;
-  }
-#endif  
 }
 
 /*

+ 5 - 2
projects/iar/bt-670x.ewp

@@ -2063,10 +2063,13 @@
         <name>$PROJ_DIR$\..\..\modules\mbedtls_api\mbedtls_config.h</name>
       </file>
       <file>
-        <name>$PROJ_DIR$\..\..\modules\mbedtls_api\net_sockets.c</name>
+        <name>$PROJ_DIR$\..\..\modules\mbedtls_api\mbedtls_debug.c</name>
+      </file>
+      <file>
+        <name>$PROJ_DIR$\..\..\modules\mbedtls_api\mbedtls_time.c</name>
       </file>
       <file>
-        <name>$PROJ_DIR$\..\..\modules\mbedtls_api\parse_rsa.c</name>
+        <name>$PROJ_DIR$\..\..\modules\mbedtls_api\net_sockets.c</name>
       </file>
     </group>
     <group>

+ 81 - 81
thirdparty/mbedTLS/include/mbedtls/platform_time.h

@@ -1,81 +1,81 @@
-/**
- * \file platform_time.h
- *
- * \brief mbed TLS Platform time abstraction
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  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.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
- */
-#ifndef MBEDTLS_PLATFORM_TIME_H
-#define MBEDTLS_PLATFORM_TIME_H
-
-#if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
-#else
-#include MBEDTLS_CONFIG_FILE
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \name SECTION: Module settings
- *
- * The configuration options you can set for this module are in this section.
- * Either change them in config.h or define them on the compiler command line.
- * \{
- */
-
-/*
- * The time_t datatype
- */
-#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
-typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
-#else
-/* For time_t */
-#include <time.h>
-typedef time_t mbedtls_time_t;
-#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
-
-/*
- * The function pointers for time
- */
-#if defined(MBEDTLS_PLATFORM_TIME_ALT)
-extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time );
-
-/**
- * \brief   Set your own time function pointer
- *
- * \param   time_func   the time function implementation
- *
- * \return              0
- */
-int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) );
-#else
-#if defined(MBEDTLS_PLATFORM_TIME_MACRO)
-#define mbedtls_time    MBEDTLS_PLATFORM_TIME_MACRO
-#else
-#define mbedtls_time   time
-#endif /* MBEDTLS_PLATFORM_TIME_MACRO */
-#endif /* MBEDTLS_PLATFORM_TIME_ALT */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* platform_time.h */
+/**
+ * \file platform_time.h
+ *
+ * \brief mbed TLS Platform time abstraction
+ *
+ *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
+ *
+ *  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.
+ *
+ *  This file is part of mbed TLS (https://tls.mbed.org)
+ */
+#ifndef MBEDTLS_PLATFORM_TIME_H
+#define MBEDTLS_PLATFORM_TIME_H
+
+#if !defined(MBEDTLS_CONFIG_FILE)
+#include "config.h"
+#else
+#include MBEDTLS_CONFIG_FILE
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \name SECTION: Module settings
+ *
+ * The configuration options you can set for this module are in this section.
+ * Either change them in config.h or define them on the compiler command line.
+ * \{
+ */
+
+/*
+ * The time_t datatype
+ */
+#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
+typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
+#else
+/* For time_t */
+#include <time.h>
+typedef time_t mbedtls_time_t;
+#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
+
+/*
+ * The function pointers for time
+ */
+#if defined(MBEDTLS_PLATFORM_TIME_ALT)
+extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time );
+
+/**
+ * \brief   Set your own time function pointer
+ *
+ * \param   time_func   the time function implementation
+ *
+ * \return              0
+ */
+int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) );
+#else
+#if defined(MBEDTLS_PLATFORM_TIME_MACRO)
+#define mbedtls_time    MBEDTLS_PLATFORM_TIME_MACRO
+#else
+#define mbedtls_time   time
+#endif /* MBEDTLS_PLATFORM_TIME_MACRO */
+#endif /* MBEDTLS_PLATFORM_TIME_ALT */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* platform_time.h */

+ 327 - 327
thirdparty/mbedTLS/library/platform.c

@@ -1,327 +1,327 @@
-/*
- *  Platform abstraction layer
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  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.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
- */
-
-#if !defined(MBEDTLS_CONFIG_FILE)
-#include "mbedtls/config.h"
-#else
-#include MBEDTLS_CONFIG_FILE
-#endif
-
-#if defined(MBEDTLS_PLATFORM_C)
-
-#include "mbedtls/platform.h"
-
-#if defined(MBEDTLS_PLATFORM_MEMORY)
-#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
-static void *platform_calloc_uninit( size_t n, size_t size )
-{
-    ((void) n);
-    ((void) size);
-    return( NULL );
-}
-
-#define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
-
-#if !defined(MBEDTLS_PLATFORM_STD_FREE)
-static void platform_free_uninit( void *ptr )
-{
-    ((void) ptr);
-}
-
-#define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_FREE */
-
-void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
-void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
-
-int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
-                              void (*free_func)( void * ) )
-{
-    mbedtls_calloc = calloc_func;
-    mbedtls_free = free_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_MEMORY */
-
-#if defined(_WIN32)
-#include <stdarg.h>
-int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
-{
-    int ret;
-    va_list argp;
-
-    /* Avoid calling the invalid parameter handler by checking ourselves */
-    if( s == NULL || n == 0 || fmt == NULL )
-        return( -1 );
-
-    va_start( argp, fmt );
-#if defined(_TRUNCATE)
-    ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
-#else
-    ret = _vsnprintf( s, n, fmt, argp );
-    if( ret < 0 || (size_t) ret == n )
-    {
-        s[n-1] = '\0';
-        ret = -1;
-    }
-#endif
-    va_end( argp );
-
-    return( ret );
-}
-#endif
-
-#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static int platform_snprintf_uninit( char * s, size_t n,
-                                     const char * format, ... )
-{
-    ((void) s);
-    ((void) n);
-    ((void) format);
-    return( 0 );
-}
-
-#define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
-
-int (*mbedtls_snprintf)( char * s, size_t n,
-                          const char * format,
-                          ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
-
-int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
-                                                 const char * format,
-                                                 ... ) )
-{
-    mbedtls_snprintf = snprintf_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
-
-#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static int platform_printf_uninit( const char *format, ... )
-{
-    ((void) format);
-    return( 0 );
-}
-
-#define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
-
-int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
-
-int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
-{
-    mbedtls_printf = printf_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
-
-#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
-{
-    ((void) stream);
-    ((void) format);
-    return( 0 );
-}
-
-#define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
-
-int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
-                                        MBEDTLS_PLATFORM_STD_FPRINTF;
-
-int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
-{
-    mbedtls_fprintf = fprintf_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
-
-#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static void platform_exit_uninit( int status )
-{
-    ((void) status);
-}
-
-#define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
-
-void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
-
-int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
-{
-    mbedtls_exit = exit_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
-
-#if defined(MBEDTLS_HAVE_TIME)
-
-#if defined(MBEDTLS_PLATFORM_TIME_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_TIME)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
-{
-    ((void) timer);
-    return( 0 );
-}
-
-#define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_TIME */
-
-mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
-
-int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
-{
-    mbedtls_time = time_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_TIME_ALT */
-
-#endif /* MBEDTLS_HAVE_TIME */
-
-#if defined(MBEDTLS_ENTROPY_NV_SEED)
-#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
-/* Default implementations for the platform independent seed functions use
- * standard libc file functions to read from and write to a pre-defined filename
- */
-int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
-{
-    FILE *file;
-    size_t n;
-
-    if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
-        return -1;
-
-    if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
-    {
-        fclose( file );
-        return -1;
-    }
-
-    fclose( file );
-    return( (int)n );
-}
-
-int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
-{
-    FILE *file;
-    size_t n;
-
-    if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
-        return -1;
-
-    if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
-    {
-        fclose( file );
-        return -1;
-    }
-
-    fclose( file );
-    return( (int)n );
-}
-#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
-
-#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
-#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
-{
-    ((void) buf);
-    ((void) buf_len);
-    return( -1 );
-}
-
-#define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
-
-#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
-/*
- * Make dummy function to prevent NULL pointer dereferences
- */
-static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
-{
-    ((void) buf);
-    ((void) buf_len);
-    return( -1 );
-}
-
-#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
-#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
-
-int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
-            MBEDTLS_PLATFORM_STD_NV_SEED_READ;
-int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
-            MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
-
-int mbedtls_platform_set_nv_seed(
-        int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
-        int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
-{
-    mbedtls_nv_seed_read = nv_seed_read_func;
-    mbedtls_nv_seed_write = nv_seed_write_func;
-    return( 0 );
-}
-#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
-#endif /* MBEDTLS_ENTROPY_NV_SEED */
-
-#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
-/*
- * Placeholder platform setup that does nothing by default
- */
-int mbedtls_platform_setup( mbedtls_platform_context *ctx )
-{
-    (void)ctx;
-
-    return( 0 );
-}
-
-/*
- * Placeholder platform teardown that does nothing by default
- */
-void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
-{
-    (void)ctx;
-}
-#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
-
-#endif /* MBEDTLS_PLATFORM_C */
+/*
+ *  Platform abstraction layer
+ *
+ *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
+ *
+ *  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.
+ *
+ *  This file is part of mbed TLS (https://tls.mbed.org)
+ */
+
+#if !defined(MBEDTLS_CONFIG_FILE)
+#include "mbedtls/config.h"
+#else
+#include MBEDTLS_CONFIG_FILE
+#endif
+
+#if defined(MBEDTLS_PLATFORM_C)
+
+#include "mbedtls/platform.h"
+
+#if defined(MBEDTLS_PLATFORM_MEMORY)
+#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
+static void *platform_calloc_uninit( size_t n, size_t size )
+{
+    ((void) n);
+    ((void) size);
+    return( NULL );
+}
+
+#define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
+
+#if !defined(MBEDTLS_PLATFORM_STD_FREE)
+static void platform_free_uninit( void *ptr )
+{
+    ((void) ptr);
+}
+
+#define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_FREE */
+
+void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
+void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
+
+int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
+                              void (*free_func)( void * ) )
+{
+    mbedtls_calloc = calloc_func;
+    mbedtls_free = free_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_MEMORY */
+
+#if defined(_WIN32)
+#include <stdarg.h>
+int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
+{
+    int ret;
+    va_list argp;
+
+    /* Avoid calling the invalid parameter handler by checking ourselves */
+    if( s == NULL || n == 0 || fmt == NULL )
+        return( -1 );
+
+    va_start( argp, fmt );
+#if defined(_TRUNCATE)
+    ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
+#else
+    ret = _vsnprintf( s, n, fmt, argp );
+    if( ret < 0 || (size_t) ret == n )
+    {
+        s[n-1] = '\0';
+        ret = -1;
+    }
+#endif
+    va_end( argp );
+
+    return( ret );
+}
+#endif
+
+#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static int platform_snprintf_uninit( char * s, size_t n,
+                                     const char * format, ... )
+{
+    ((void) s);
+    ((void) n);
+    ((void) format);
+    return( 0 );
+}
+
+#define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
+
+int (*mbedtls_snprintf)( char * s, size_t n,
+                          const char * format,
+                          ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
+
+int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
+                                                 const char * format,
+                                                 ... ) )
+{
+    mbedtls_snprintf = snprintf_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
+
+#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static int platform_printf_uninit( const char *format, ... )
+{
+    ((void) format);
+    return( 0 );
+}
+
+#define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
+
+int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
+
+int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
+{
+    mbedtls_printf = printf_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
+
+#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
+{
+    ((void) stream);
+    ((void) format);
+    return( 0 );
+}
+
+#define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
+
+int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
+                                        MBEDTLS_PLATFORM_STD_FPRINTF;
+
+int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
+{
+    mbedtls_fprintf = fprintf_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
+
+#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static void platform_exit_uninit( int status )
+{
+    ((void) status);
+}
+
+#define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
+
+void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
+
+int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
+{
+    mbedtls_exit = exit_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
+
+#if defined(MBEDTLS_HAVE_TIME)
+
+#if defined(MBEDTLS_PLATFORM_TIME_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_TIME)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
+{
+    ((void) timer);
+    return( 0 );
+}
+
+#define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_TIME */
+
+mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer );// = MBEDTLS_PLATFORM_STD_TIME;
+
+int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
+{
+    mbedtls_time = time_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_TIME_ALT */
+
+#endif /* MBEDTLS_HAVE_TIME */
+
+#if defined(MBEDTLS_ENTROPY_NV_SEED)
+#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
+/* Default implementations for the platform independent seed functions use
+ * standard libc file functions to read from and write to a pre-defined filename
+ */
+int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
+{
+    FILE *file;
+    size_t n;
+
+    if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
+        return -1;
+
+    if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
+    {
+        fclose( file );
+        return -1;
+    }
+
+    fclose( file );
+    return( (int)n );
+}
+
+int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
+{
+    FILE *file;
+    size_t n;
+
+    if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
+        return -1;
+
+    if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
+    {
+        fclose( file );
+        return -1;
+    }
+
+    fclose( file );
+    return( (int)n );
+}
+#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
+
+#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
+#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
+{
+    ((void) buf);
+    ((void) buf_len);
+    return( -1 );
+}
+
+#define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
+
+#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
+/*
+ * Make dummy function to prevent NULL pointer dereferences
+ */
+static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
+{
+    ((void) buf);
+    ((void) buf_len);
+    return( -1 );
+}
+
+#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
+#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
+
+int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
+            MBEDTLS_PLATFORM_STD_NV_SEED_READ;
+int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
+            MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
+
+int mbedtls_platform_set_nv_seed(
+        int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
+        int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
+{
+    mbedtls_nv_seed_read = nv_seed_read_func;
+    mbedtls_nv_seed_write = nv_seed_write_func;
+    return( 0 );
+}
+#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
+#endif /* MBEDTLS_ENTROPY_NV_SEED */
+
+#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
+/*
+ * Placeholder platform setup that does nothing by default
+ */
+int mbedtls_platform_setup( mbedtls_platform_context *ctx )
+{
+    (void)ctx;
+
+    return( 0 );
+}
+
+/*
+ * Placeholder platform teardown that does nothing by default
+ */
+void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
+{
+    (void)ctx;
+}
+#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
+
+#endif /* MBEDTLS_PLATFORM_C */

+ 33 - 58
user/init_task.c

@@ -56,12 +56,9 @@ extern bool dhcp;
   */
 extern SETTINGS_t sSettings;
 
-/**
-  * @brief Хендл для задачи vTaskLedBlink
-  */
-TaskHandle_t xTaskToKill;
-
+#ifdef DEBUG_FREERTOS
 static void vTaskDebug(void *pvParameters);
+#endif
 
 /**
   * @brief  Разовая синхронизация времени при старте контроллера
@@ -75,25 +72,22 @@ TaskHandle_t xHandleSntpOnceSinhro = NULL;
   */
 void InitTask(void *params)
 {
-// -----------------------------------------------------------------------------    
- // xTaskCreate(vTaskWdt, "WDT", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 // -----------------------------------------------------------------------------    
   InitUSART();
   ups_megatec_init();
-
   log_init(false);
+
 // -----------------------------------------------------------------------------    
- // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
-  SETTINGS_SetDefaultDebug();
+  //SETTINGS_SetDefaultDebug();
   //SETTINGS_SetAllDefault();
-  SETTINGS_Save();
-  //SETTINGS_Load();
-
-	if(set_mode_jumper()){
-		SETTINGS_SetServiceDef();
-		SETTINGS_Save();
-		log_event_data(LOG_PSW_CHANGE, "Сброс к заводскому");
-	}
+  //SETTINGS_Save();
+  SETTINGS_Load();
+
+  if(set_mode_jumper()){
+	SETTINGS_SetServiceDef();
+	SETTINGS_Save();
+	log_event_data(LOG_PSW_CHANGE, "Сброс к заводскому");
+  }
 // -----------------------------------------------------------------------------    
   
 // -----------------------------------------------------------------------------  
@@ -107,7 +101,7 @@ void InitTask(void *params)
 #ifdef LED_ENABLE
   LED_Init();
   /* Простая мигалка для подтверждения живучести контроллера */
-  xTaskCreate(vTaskLedBlink, "LED_Blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);//&xTaskToKill
+  xTaskCreate(vTaskLedBlink, "LED_Blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 #endif  
 // -----------------------------------------------------------------------------    
   
@@ -118,29 +112,27 @@ void InitTask(void *params)
 #endif
 // -----------------------------------------------------------------------------    
   
-  //xTaskCreate( d_inouts_task,  "inouts_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
- // 	xTaskCreate( d_inouts_test, "d_inouts_test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-
 // -----------------------------------------------------------------------------    
 #ifdef RTC_ENABLE  
-  TM_RTC_Init(TM_RTC_ClockSource_External);  // Так было
- // TM_RTC_Init(TM_RTC_ClockSource_Internal);  // TODO Уточнить источинк тактирования
+  TM_RTC_Init(TM_RTC_ClockSource_External);  
 #endif
 // -----------------------------------------------------------------------------      
   
-
 // -----------------------------------------------------------------------------
 #ifdef UPS_ENABLE
   xTaskCreate(UPS_Monitor, "UPS_Monitor", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 #endif
+// -----------------------------------------------------------------------------  
+  
 // -----------------------------------------------------------------------------    
-  /* Random number generator */
+/* Random number generator */
   RNG_Init();
 // -----------------------------------------------------------------------------  
+
+// -----------------------------------------------------------------------------
 #ifdef NET_ENABLE
   ETH_BSP_Config();
   LwIP_Init();
-
 /*  
 #ifdef WEB_SERVER_ENABLE
   if(strncmp(sSettings.sFlags.testState, "T2OK", 4)){
@@ -157,38 +149,25 @@ void InitTask(void *params)
 	   // UDP for net settings 
 	   UDP_netsetting_init();
   }
-*/
-  
-#define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
-	   xTaskCreate(ssl_server, "SSL", 10*configMINIMAL_STACK_SIZE, NULL, SSL_TASK_PRIO, NULL);  
-  //vTaskDelay(6000);
-  //SSL_Test();
-  //SSL_ParseRsaKey();
+*/  
+  HTTPS_Init();
+  xTaskCreate(ssl_server, "SSL", 10*configMINIMAL_STACK_SIZE, NULL, SSL_TASK_PRIO, NULL);  
+  UDP_netsetting_init();
   
-
 #ifdef SNMP_ENABLE
-
   SNMP_Init();
-	  
-	//xTaskCreate(SNMP_SysUpTimeTask, "snmpSysUpTime", configMINIMAL_STACK_SIZE,
-    //          NULL, tskIDLE_PRIORITY, NULL);
-	
-	xTaskCreate(snmp_trap_tread, "snmpTrapTest", 4*configMINIMAL_STACK_SIZE,
-              NULL, tskIDLE_PRIORITY, NULL);
-   
+  xTaskCreate(snmp_trap_tread, "snmpTrapTest", 4*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 #endif	
- 
-#endif  
+#endif
+  
 // -----------------------------------------------------------------------------      
-	 /* SNTP */
-	 SNTP_Init();
-	 xTaskCreate(vTaskOnceSynchro, "sntpOnceSinhro", 2*configMINIMAL_STACK_SIZE,
-				 NULL, tskIDLE_PRIORITY, &xHandleSntpOnceSinhro);
-	 xTaskCreate(vTaskPeriodicSynchro, "sntpPeriodicSinhro", 2*configMINIMAL_STACK_SIZE,
-				 NULL, tskIDLE_PRIORITY, NULL);
+/* SNTP */
+  SNTP_Init();
+  xTaskCreate(vTaskOnceSynchro, "sntpOnceSinhro", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandleSntpOnceSinhro);
+  xTaskCreate(vTaskPeriodicSynchro, "sntpPeriodicSinhro", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 // -----------------------------------------------------------------------------  
 
-
+// -----------------------------------------------------------------------------  
 // Тестирование
     
   // Тест таблицы трапов  
@@ -201,9 +180,6 @@ void InitTask(void *params)
 #endif  
 // -----------------------------------------------------------------------------      
 
-  //TEST_InitRS485();
-  //TEST_IO();
-  
   log_event_data(LOG_TURN_ON, " ");
   /* Контроль успешной загрузки. Сброс флага bootry */
   /* Сброс флага и сохранение нового значения во флеш памяти происходт после
@@ -211,8 +187,6 @@ void InitTask(void *params)
   vTaskDelay(4000);
  
   SETTINGS_ResetBootTry();
-
-  //RC_Login("test1", "12345");
   
   // Отправка трапа о перезагрузке в случае статического IP
  /* if (!dhcp)
@@ -224,6 +198,7 @@ void InitTask(void *params)
      taskYIELD();
 }
 
+#ifdef DEBUG_FREERTOS
 static void vTaskDebug(void *pvParameters) 
 {
   char msg[700];
@@ -239,7 +214,7 @@ static void vTaskDebug(void *pvParameters)
 	vTaskDelay(5000);
   }   
 }
-
+#endif
 
 
 /********************************* (C) РОТЕК **********************************/