Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
147:a6093b52e654
Parent:
145:206bf0d073c7
Child:
148:5489d36986e5
--- a/tcp/tls/tls.c	Wed May 15 15:33:15 2019 +0000
+++ b/tcp/tls/tls.c	Fri May 17 15:01:32 2019 +0000
@@ -1,12 +1,11 @@
 #include <stdbool.h>
 
-#include   "http.h"
-#include "tcp.h"
+#include "http.h"
 #include "tcpbuf.h"
 #include "action.h"
-#include    "net.h"
-#include    "log.h"
-#include    "led.h"
+#include "net.h"
+#include "log.h"
+#include "led.h"
 #include "restart.h"
 #include "mstimer.h"
 
@@ -29,9 +28,29 @@
 #define TLS_HANDSHAKE_ClientKeyExchange    16
 #define TLS_HANDSHAKE_Finished             20
 
-#define DO_SERVER_HELLO 100
+#define DO_NOTHING      0
+#define DO_SERVER_HELLO 1
+#define DO_APPLICATION  2
+
+bool TlsTrace = true;
 
-/*static void logHandshakeType(char handshakeType)
+struct state
+{
+    int      toDo;
+};
+static void logContentType(char contentType)
+{
+    switch (contentType)
+    {
+        case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher");      break;
+        case TLS_CONTENT_TYPE_Alert:        Log ("Alert");              break;
+        case TLS_CONTENT_TYPE_Handshake:    Log ("Handshake");          break;
+        case TLS_CONTENT_TYPE_Application:  Log ("Application");        break;
+        case TLS_CONTENT_TYPE_Heartbeat:    Log ("Heartbeat");          break;
+        default:                            LogF("%02hX", contentType); break;
+    }
+}
+static void logHandshakeType(char handshakeType)
 {
     switch (handshakeType)
     {
@@ -50,69 +69,69 @@
         default:                                LogF("%02hX", handshakeType); break;
     }
 }
-static void logContentType(char contentType)
+void TlsRequest(char* pTlsState, char* pWebState, int size, char* pRequestStream, uint32_t positionInRequestStream)
 {
-    switch (contentType)
-    {
-        case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher");      break;
-        case TLS_CONTENT_TYPE_Alert:        Log ("Alert");              break;
-        case TLS_CONTENT_TYPE_Handshake:    Log ("Handshake");          break;
-        case TLS_CONTENT_TYPE_Application:  Log ("Application");        break;
-        case TLS_CONTENT_TYPE_Heartbeat:    Log ("Heartbeat");          break;
-        default:                            LogF("%02hX", contentType); break;
-    }
-}
-*/
+    struct state* pState = (struct state*)pTlsState;
+    
+    if (TlsTrace) LogF("TLS <<< %d (%u)\r\n", size, positionInRequestStream);
 
-void TlsRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, void* pData)
-{
-    /*
-    if (HttpsTrace)
-    {
-        LogF("HTTPS <<< %d (%u)\r\n", size, positionInRequestStream);
-    }
-    //Handle request for the first packet of data received but leave todo the same after that.
     if (size == 0) return;
     if (positionInRequestStream != 0) return;
     char contentType = pRequestStream[0];
-    if (HttpsTrace) Log("      content type: "); logContentType(contentType); Log("\r\n");
+    if (TlsTrace) { Log("      content type: "); logContentType(contentType); Log("\r\n"); }
     switch (contentType)
     {
         case TLS_CONTENT_TYPE_Handshake:
             {
                 char handshakeType = pRequestStream[5];
-                if (HttpsTrace) Log("      handshake type: "); logHandshakeType(handshakeType); Log("\r\n");
-                *pToDo = DO_SERVER_HELLO;
+                if (TlsTrace) { Log("      handshake type: "); logHandshakeType(handshakeType); Log("\r\n"); }
+                pState->toDo = DO_SERVER_HELLO;
+                return;
+            }
+        case TLS_CONTENT_TYPE_Application:
+            {
+                pState->toDo = DO_APPLICATION;
                 return;
             }
         default:
-            Log("HTTPS - ignoring untreated content type\r\n");
-            *pToDo = 0;
+            Log("TLS - ignoring untreated content type\r\n");
+            pState->toDo = DO_NOTHING;
             return;
     }
     //ECDHE-RSA-AES128-GCM-SHA256
-    */
 }
-/*static void sendServerHello()
+static void sendServerHello()
 {
     Log("     sending server hello\r\n");
 }
-*/
-bool TlsReplyPoll(char* pState, bool clientFinished)
+
+int TlsPoll(char* pTlsState, char* pWebState, bool clientFinished)
 {
-    /*
-    TcpBufStart(positionInReplyStream, mss, pReplyStream);
-    if (todo == DO_SERVER_HELLO) sendServerHello();
-    *pSize = TcpBufLength();
+    struct state* pState = (struct state*)pTlsState;
+    
+    switch (pState->toDo)
+    {
+        case DO_NOTHING:
+            if (clientFinished) return -1; //The client hasn't made a request and never will so finish
+            else                return  0; //The client hasn't made a request yet but it could.
+        case DO_APPLICATION:    return HttpPollFunction(pWebState, clientFinished); //Return whatever HTTP would be
+        default:                return  1; //The client has made a request so do it
+    }
+}
+bool TlsReply(char* pTlsState, char* pWebState)
+{
+    struct state* pState = (struct state*)pTlsState;
+    
+    if ( pState->toDo == DO_SERVER_HELLO) sendServerHello();
 
-    if (HttpsTrace)
-    {
-        LogF("HTTPS >>> %d (%d)\r\n", *pSize, positionInReplyStream);
-    }
-    */
-    return true; //Finished
+    return false; //Finished
+}
+static char encrypt(char c)
+{
+    return c; //Implement encryption
 }
 void TlsAddChar(char c)
 {
-    TcpBufAddChar(c);
+    char e = encrypt(c);
+    TcpBufAddChar(e);
 }
\ No newline at end of file