Selaa lähdekoodia

fix ssl_server

balbekova 7 vuotta sitten
vanhempi
commit
2e3bc21ce3
3 muutettua tiedostoa jossa 76 lisäystä ja 27 poistoa
  1. 1 0
      modules/Ethernet/lwipopts.h
  2. 47 25
      modules/HTTP_Server/http_server.c
  3. 28 2
      modules/mbedtls_api/net_sockets.c

+ 1 - 0
modules/Ethernet/lwipopts.h

@@ -152,6 +152,7 @@ a lot of data that needs to be copied, this should be set high. */
 #define UDP_TTL                 255
 
 #define LWIP_SO_RCVTIMEO        1 // Эта опция необходима для netconn_set_recvtimeout в модуле udp_netsetting.c
+#define LWIP_SO_SNDTIMEO        1 //
 
 /* ---------- Statistics options ---------- */
 #define LWIP_STATS                      1

+ 47 - 25
modules/HTTP_Server/http_server.c

@@ -64,7 +64,7 @@ uint16_t receivedBufLen = 0;
 
 #define MAX_POST_REQ_LEN 256
 char post_req_data[MAX_POST_REQ_LEN];
-uint32_t post_data_count;
+uint32_t post_data_count = 0;
 uint32_t log_post_reqn;
 
 /* Logout timeout, 30 minutes */
@@ -2846,7 +2846,7 @@ mbedtls_ssl_config conf;
 mbedtls_x509_crt srvcert;
 mbedtls_pk_context pkey;
 
-char CookieBuf[50];
+char CookieBuf[51];
 char *CookiePtr = NULL;
 char name[MAX_WEB_COOKIE_LEN];
 char id[MAX_WEB_COOKIE_LEN];
@@ -2857,23 +2857,36 @@ struct fs_file file = {0, 0};
 //
 void Cockie(void)
 {
+  char* endPtr;
+  uint32_t len;
   receiveBuf[receivedBufLen] = '\0';
-  //  printf("receive %s \r\n", receiveBuf);
+    printf("receive %s \r\n", receiveBuf);
   // Get cookie "uname" value 
+  memset(CookieBuf, 0, sizeof(CookieBuf));
   CookiePtr = strstr(receiveBuf, "uname=");
-  strncpy(CookieBuf, CookiePtr, 50);
-  //printf("********CookieBuf1= %s\r\n", CookieBuf);
+  endPtr = strstr(CookiePtr, "\r");
+  len =  (endPtr - CookiePtr)+2;
+  if(len < 50)
+	  strncpy(CookieBuf, CookiePtr,len);
+  else
+	  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);
+//  printf("********CookieBuf2= %s\r\n", CookieBuf);
+ // printf("********uname= %s\r\n", name);
 
+  memset(CookieBuf, 0, sizeof(CookieBuf));
   // Get cookie "id" value 
-  CookiePtr = strstr(receiveBuf, "id=");
-  strncpy(CookieBuf, CookiePtr, 50);
-  //printf("********CookieBuf1= %s\r\n", CookieBuf);
+  CookiePtr = strstr(receiveBuf, " id=");
+  if(len < 50)
+ 	  strncpy(CookieBuf, CookiePtr,len);
+   else
+ 	  strncpy(CookieBuf, CookiePtr, 50);
+//  printf("********CookieBuf1= %s\r\n", CookieBuf);
   memset(id, 0, MAX_WEB_COOKIE_LEN);
   GetCookieValue(CookieBuf, "id=", id, &idLen);
+ // printf("********ID= %s\r\n", id);
 }
 
 //
@@ -2991,7 +3004,7 @@ void ssl_server(void *pvParameters)
       }
       else {
         //mbedtls_ssl_init(&ssl);
-        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_net_recv
         mbedtls_printf( " ok\r\n" );
         
         ssl_state = SSL_HANDSHAKE;
@@ -3022,7 +3035,7 @@ void ssl_server(void *pvParameters)
     break;
     
     case SSL_READ :
-      
+    	printf("SSL_READ\r\n");
       if (SSL_ReadRoutine(&ssl, (unsigned char*)receiveBuf) <= 0)
         ssl_state = SSL_ERROR;
       else
@@ -3030,7 +3043,7 @@ void ssl_server(void *pvParameters)
     break;
       
     case SSL_PROCESSING :
-
+    	 printf("SSL_PROCESSING\r\n");
       sendPtr = SSL_ProcessingRoutine(&sendBufLoadLen);
       if (sendPtr)
         ssl_state = SSL_WRITE;
@@ -3040,11 +3053,14 @@ void ssl_server(void *pvParameters)
     break;
     
     case SSL_WRITE :
-      if (SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen) == SSL_WRITE_OK)
+    	 printf("SSL_WRITE\r\n");
+    	SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen);
+     // if (SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen) == SSL_WRITE_OK)
         ssl_state = SSL_CLOSE;
     break;
     
     case SSL_CLOSE :
+    	printf("SSL_CLOSE\r\n");
       mbedtls_ssl_close_notify(&ssl);
       mbedtls_net_free(&client_fd);
       //mbedtls_ssl_free( &ssl );
@@ -3052,6 +3068,7 @@ void ssl_server(void *pvParameters)
     break;
     
     case SSL_ERROR :
+    	 printf("SSL_ERROR\r\n");
       //mbedtls_net_free(&client_fd);
       //mbedtls_ssl_free( &ssl );
       mbedtls_net_free(&client_fd);
@@ -3059,7 +3076,7 @@ void ssl_server(void *pvParameters)
     break;
     
     case SSL_CRITICAL_ERROR:
-      
+    	 printf("SSL_CRITICAL_ERROR\r\n");
       mbedtls_x509_crt_free( &srvcert );
       mbedtls_pk_free( &pkey );
       mbedtls_ssl_free( &ssl );
@@ -3123,7 +3140,7 @@ int SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
           break;
 
         default:
-          mbedtls_printf( " mbedtls_ssl_read returned -0x%x\r\n", -ret );
+          mbedtls_printf( "mbedtls_ssl_read returned -0x%x\r\n", -ret );
         break;
       }
       return ret;
@@ -3132,9 +3149,9 @@ int SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
     receivedBufLen = ret;
     //mbedtls_printf( " %d bytes read\r\n%s", len, (char *) receiveBuf );
     //mbedtls_printf( " %d bytes read\r\n", receivedBufLen);
-
-    //printf(receiveBuf);
-    //printf("\r\n");
+ /*   printf("\r\n\r\n");
+    printf(receiveBuf);
+    printf("\r\n");*/
 
   } while(0);
 }
@@ -3148,10 +3165,12 @@ SSL_SERVER_STATE SSL_WriteRoutine(mbedtls_ssl_context *ssl, char *data, int data
 //
 char* SSL_ProcessingRoutine(uint16_t* sendLen)
 {  
+	/**sendLen = strlen(HTTP_RESPONSE);
+	return HTTP_RESPONSE;*/
   Cockie();
-  getAuthenticatedState();
- 
-  if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
+ getAuthenticatedState();
+
+   if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
   { 	
     HTTP_LOGIN(sendBuf, sendLen);  
     return sendBuf;
@@ -3160,10 +3179,11 @@ char* SSL_ProcessingRoutine(uint16_t* sendLen)
   {
     return AuthenticatedFalseRoutine(sendLen);
   }
-  else 
+  else
   {
     return AuthenticatedTrueRoutine(sendLen);
-  }  
+  }
+
 }
 
 //
@@ -3221,6 +3241,7 @@ char* AuthenticatedFalseRoutine(uint16_t* sendLen)
                     strncat(post_req_data, offset, post_data_count);
                     if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), sendLen) == SEND_REQUIRED_YES) 
                     {
+                    	post_data_count = 0;
                         return sendBuf;
                     }
                     else 
@@ -3231,9 +3252,10 @@ char* AuthenticatedFalseRoutine(uint16_t* sendLen)
                         fs_open("/login.html", &file);
                         ssl_sendframes(&ssl, file.data, file.len);*/
                         //ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
+                    	post_data_count = 0;
                         return sendBuf;
                     } 
-                    post_data_count = 0;
+
                 }
                 else
                 {

+ 28 - 2
modules/mbedtls_api/net_sockets.c

@@ -73,6 +73,9 @@
 
 #include "main.h"
 
+#include "FreeRTOS.h"
+#include "task.h"
+
 static int initialized = 0;
 struct sockaddr_storage client_addr;
 
@@ -161,7 +164,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
             continue;
         }
 
-        n = 1;
+       n = 1;
         if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
                         (const char *) &n, sizeof( n ) ) != 0 )
         {
@@ -346,6 +349,18 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
     return MBEDTLS_ERR_NET_INVALID_CONTEXT;
   }
  
+  struct timeval timeout;
+
+  timeout.tv_sec = 3;
+  timeout.tv_usec = 0;
+
+  if( setsockopt( fd, SOL_SOCKET, SO_RCVTIMEO,
+                (const char *) &timeout, sizeof( timeout ) ) != 0 )
+  {
+    close( fd );
+    ret = MBEDTLS_ERR_NET_INVALID_CONTEXT;
+  }
+
   ret = (int32_t) read( fd, buf, len );
 
   if( ret < 0 )
@@ -420,7 +435,18 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
   {
     return MBEDTLS_ERR_NET_INVALID_CONTEXT;
   }
-  
+  struct timeval timeout;
+
+    timeout.tv_sec = 3;
+    timeout.tv_usec = 0;
+
+	if( setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO,
+				  (const char *) &timeout, sizeof( timeout ) ) != 0 )
+	{
+	  close( fd );
+	  ret = MBEDTLS_ERR_NET_INVALID_CONTEXT;
+	}
+
   ret = (int32_t) write(fd, buf, len);
 
   if( ret < 0 )