Ver Fonte

SSL - тестирование функций передачи нескольких фреймов

TelenkovDmitry há 7 anos atrás
pai
commit
204d8fc00c

+ 246 - 181
modules/HTTP_Server/http_server.c

@@ -2810,10 +2810,9 @@ void HTTPS_Init()
 #include <stdlib.h>
 #include <string.h>
 
-#define HTTP_RESPONSE \
-    "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
-    "<h2>mbed TLS Test Server</h2>\r\n" \
-    "<p>Successful connection using: %s</p>\r\n"
+char HTTP_RESPONSE[] = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n \
+                       <h2>mbed TLS Test Server</h2>\r\n \
+                       <p>Successful connection using: %s</p>\r\n";
 
 static mbedtls_net_context listen_fd, client_fd;
 static const uint8_t *pers = (uint8_t *)("ssl_server");
@@ -2830,6 +2829,7 @@ 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     
+struct fs_file file = {0, 0};
 
 #define HEAP_SIZE       (1u << 14)  // 16k
 unsigned char malloc_buf[HEAP_SIZE];
@@ -2842,16 +2842,16 @@ void Cockie(void)
   // Get cookie "uname" value 
   CookiePtr = strstr(receiveBuf, "uname=");
   strncpy(CookieBuf, CookiePtr, 50);
-  printf("********CookieBuf1= %s\r\n", CookieBuf);
+  //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);
 
   // Get cookie "id" value 
   CookiePtr = strstr(receiveBuf, "id=");
   strncpy(CookieBuf, CookiePtr, 50);
-  printf("********CookieBuf1= %s\r\n", CookieBuf);
+  //printf("********CookieBuf1= %s\r\n", CookieBuf);
   memset(id, 0, MAX_WEB_COOKIE_LEN);
   GetCookieValue(CookieBuf, "id=", id, &idLen);
 }
@@ -3007,13 +3007,13 @@ void ssl_server(void *pvParameters)
       
     case SSL_PROCESSING :
 
-      SSL_ProcessingRoutine(&sendPtr, &sendBufSize);
+      sendPtr = SSL_ProcessingRoutine(&sendBufLoadLen);
       ssl_state = SSL_WRITE;
       
     break;
     
     case SSL_WRITE :
-      ssl_state = SSL_WriteRoutine(&ssl, sendPtr, sendBufSize);
+      ssl_state = SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen);
     break;
     
     case SSL_CRITICAL_ERROR:
@@ -3032,135 +3032,6 @@ void ssl_server(void *pvParameters)
   }
   
   
-#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 ...\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 );
-    goto exit;
-  }
-
-  mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
-  mbedtls_printf( " ok\r\n" );
-  
-  // 6. 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 );
-      goto reset;
-    }
-  }
-  mbedtls_printf( " ok\r\n" );
-  
-  // 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, (unsigned char*)receiveBuf, 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);  
-  
-  
-  Cockie();
-  getAuthenticatedState();
-  
-  // 8. Write the 200 Response
-  mbedtls_printf( "  > Write to client:" );
-  //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, (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 )
-    {
-      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;
-    }
-  }
-*/
-  
-  len = ret;
-  //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 )
-  {
-    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 );
-      goto reset;
-    }
-  }
-
-  mbedtls_printf( " ok\r\n" );
-
-  ret = 0;
-  goto reset;  
-      
-exit:
-  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);
-#endif
 }
 
 
@@ -3230,55 +3101,167 @@ void SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
 //
 SSL_SERVER_STATE SSL_WriteRoutine(mbedtls_ssl_context *ssl, char *data, int datalen)
 {
-  int ret;
+    return SSL_SendFrames(ssl, data, datalen);
+}
+
+//
+char* SSL_ProcessingRoutine(uint16_t* sendLen)
+{  
+  char* ptr;
+  Cockie();
+  getAuthenticatedState();
   
-  mbedtls_printf( "  > Write to client:" );
-    
-  while( ( ret = mbedtls_ssl_write(ssl, data, datalen) ) <= 0 )
+  //HTTP_LOGIN(send, sendLen);
+  //fs_open("/index.html", &file); // +
+  //fs_open("/settings.html", &file);
+  //*sendLen = file.len;
+  //return  file.data; 
+
+  fs_open("/main.css", &file);
+                *sendLen = file.len;
+                return file.data;
+                
+#if 0
+  if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
+  { 	
+    HTTP_LOGIN(sendBuf, sendLen);  
+    return sendBuf;
+  }
+  else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
   {
-    if( ret == MBEDTLS_ERR_NET_CONN_RESET )
-    {
-      mbedtls_printf( " failed\r\n  ! peer closed the connection\r\n" );
-      return SSL_ACCEPT;
-    }
+            if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
+            {
+                fs_open("/main.css", &file);
+                *sendLen = file.len;
+                return file.data;
+            }
+            else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
+            {
+                fs_open("/rotek.png", &file);
+                send = file.data; *sendLen = file.len;
+            }
+            else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
+            {
+                fs_open("/favicon.ico", &file);
+                send = file.data; *sendLen = file.len;
+            }
+            else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
+            {
+                fs_open("/role.js", &file);
+                send = file.data; *sendLen = file.len;
+            }
+            else if (strncmp(receiveBuf, "POST /login.cgi", 15) == 0)
+            {
+                uint32_t req_data_received = 0;
+                char *offset;
 
-    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..." );
+                offset = 0;
+                
+                post_data_count = Parse_Content_Length(receiveBuf, receivedBufLen);
+			  
+                if (post_data_count < MAX_POST_REQ_LEN) 
+                {
+                    memset(post_req_data, 0, MAX_POST_REQ_LEN);
 
-  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;
-    }
-  }
+                    offset = (strstr(receiveBuf, "\r\n\r\n")) + 4;
+                    req_data_received = receivedBufLen - (offset - &receiveBuf[0]);
 
-  mbedtls_printf( " ok\r\n" );
+					if (offset != 0) 
+                    {
+                        if (req_data_received < post_data_count) 
+                        {
+                            snprintf(post_req_data, req_data_received, "%s", receiveBuf);
+                            post_data_count -= req_data_received;
 
-  ret = 0;
-  return SSL_ACCEPT;
-}
+                            //ssl_server_read();
+                            SSL_ReadRoutine(&ssl, (unsigned char*)receiveBuf);
+
+                            offset = receiveBuf;
+                        }
+
+                        if(strlen(receiveBuf) != 0)
+                        {
+
+                            strncat(post_req_data, offset, post_data_count);
+                            if (HTTP_ConfirmWebPwd(post_req_data, send, strlen(post_req_data), sendLen) == SEND_REQUIRED_YES) 
+                            {
+                                //ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
+                            }
+                            else 
+                            {
+                                /*if(sSettings.sRADIUS.RDSEnable == true)
+                            		fs_open("/rslogin.html", &file);
+                            	else
+                            		fs_open("/login.html", &file);
+                                ssl_sendframes(&ssl, file.data, file.len);*/
+                            	//ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
+                            }
+                            post_data_count = 0;
+                        }
+                        else{
+    						// Redirect to login page
+    						if((sSettings.sRADIUS.RDSEnable == true) && (fl_raddius_net_err == false))
+    							fs_open("/rslogin.html", &file);
+    						else
+    							fs_open("/login.html", &file);
+    						send = file.data; *sendLen = file.len;
+    						// End reqest 
+    						post_data_count = 0;
+                        }
+					}
+					/* request was fragmented before "\r\n\r\n" */
+					else 
+                    {
+						/* Redirect to login page */
+						if((sSettings.sRADIUS.RDSEnable == true) && (fl_raddius_net_err == false))
+							fs_open("/rslogin.html", &file);
+						else
+							fs_open("/login.html", &file);
+						send = file.data; *sendLen = file.len;
+						/* End reqest */
+						post_data_count = 0;
+					}
+                }
+                else 
+                {
+                    //printf("Too long POST request!\r\n");
+                    // Ignore request 
+                    post_data_count = 0;
+
+                    // Redirect to login page 
+                    if((sSettings.sRADIUS.RDSEnable == true) && (fl_raddius_net_err == false))
+						fs_open("/rslogin.html", &file);
+					else
+						fs_open("/login.html", &file);
+                    send = file.data; *sendLen = file.len;
+                }
+            }
+			else if (post_data_count > 0)
+			{
+                strncat(post_req_data, receiveBuf, post_data_count);
+
+                if (HTTP_ConfirmWebPwd(post_req_data, send, strlen(post_req_data), sendLen) == SEND_REQUIRED_YES) 
+                {
+                    //ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
+                }
+				else 
+                {
+					//ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
+				}
+                post_data_count = 0;
+				log_post_reqn = 0;
+			}
+            else
+            {
+            	if((sSettings.sRADIUS.RDSEnable == true) && (fl_raddius_net_err == false))
+					fs_open("/rslogin.html", &file);
+				else
+					fs_open("/login.html", &file);
+                send = file.data; *sendLen = file.len;
+			}
+  }
+#endif  
 
-//
-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;
 }
 
 //
@@ -3325,3 +3308,85 @@ void SSL_Send( mbedtls_ssl_context *ssl, char *data, int datalen )
 }
 #endif
 
+#define FRAME_SIZE   (1000)
+SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datalen)
+{
+  SSL_SERVER_STATE ret;
+  int retClose;
+  int index = 0;
+  int k = 0;
+  int lastframe, nbrframes;
+
+  nbrframes = datalen / FRAME_SIZE; 
+  
+  while(nbrframes > 0)
+  {
+    index = k * FRAME_SIZE;
+    
+    if (SSL_Write(ssl, (data + index), FRAME_SIZE ) == SSL_CRITICAL_ERROR)
+      return SSL_CRITICAL_ERROR;
+        
+    nbrframes--;
+    k++;
+  }
+
+  index = k * FRAME_SIZE;
+  lastframe = datalen % FRAME_SIZE ;
+  if (SSL_Write(ssl, (data + index), lastframe ) == SSL_CRITICAL_ERROR)
+      return SSL_CRITICAL_ERROR;    
+  
+  mbedtls_printf( "  . Closing the connection..." );
+
+  while( ( retClose = mbedtls_ssl_close_notify( ssl ) ) < 0 )
+  {
+    if( retClose != MBEDTLS_ERR_SSL_WANT_READ && retClose != MBEDTLS_ERR_SSL_WANT_WRITE )
+    {
+      mbedtls_printf( " failed\r\n  ! mbedtls_ssl_close_notify returned %d\r\n", ret );
+      
+    }
+  }
+
+  mbedtls_printf( " ok\r\n" );
+  
+  return SSL_ACCEPT;
+}
+
+SSL_SERVER_STATE SSL_Write(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;
+}

+ 4 - 2
modules/HTTP_Server/http_server.h

@@ -3,7 +3,7 @@
 #include "lwip/api.h"
 
 #include "mbedtls/ssl.h"
-
+     
 #define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
 #define FRAME_SIZE      (1000)
 
@@ -165,9 +165,11 @@ 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);
+char* SSL_ProcessingRoutine(uint16_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);
+SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datalen);
+SSL_SERVER_STATE SSL_Write(mbedtls_ssl_context *ssl, char *data, int datalen);
 
 #ifdef __cplusplus
 }

+ 4 - 2
modules/mbedtls_api/mbedtls_config.h

@@ -51,7 +51,9 @@
 #define MBEDTLS_ECP_DP_SECP384R1_ENABLED
 #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
 
-#define MBEDTLS_SSL_PROTO_TLS1_2
+//#define MBEDTLS_SSL_PROTO_TLS1_2
+#define MBEDTLS_SSL_PROTO_SSL3
+#define MBEDTLS_SSL_PROTO_TLS1
 
 /* mbed TLS modules */
 #define MBEDTLS_AES_C
@@ -146,7 +148,7 @@
 #define MBEDTLS_NO_PLATFORM_ENTROPY
 #define MBEDTLS_ENTROPY_HARDWARE_ALT
 #define MBEDTLS_PLATFORM_TIME_ALT
-
+#define MBEDTLS_SSL_CBC_RECORD_SPLITTING
 // User debug options
 //#define MBEDTLS_DEBUG_C
 #define DEBUG_LEVEL   3  // 0 No debug