Common stuff for all my devices' web server pages: css, login, log, ipv4, ipv6, firmware update, clock, reset info etc.

Dependents:   oldheating gps motorhome heating

Security

A password has to be set whenever there has been a software reset. Resets following faults or power on do not require a new password as the hash is restored from the RTC GPREG register.

The password is not saved on the device; instead a 32 bit hash of the password is saved. It would take 2^31 attempts to brute force the password: this could be done in under a month if an attempt were possible every millisecond. To prevent this a 200 ms delay is introduced in the reply to the login form, that gives a more reasonable 13 years to brute force the password.

Once the password is accepted a random session id is created. This is 36 bit to give six base 64 characters but without an extra delay. If an attempt could be made every ms then this would still take over a year to brute force.

The most likely attack would to use a dictionary with, say, 10 million entries against the password which would still take 20 days to do.

Revision:
128:fc9708e1d17c
Parent:
127:bd6dd135009d
Child:
129:6d9bffc72676
diff -r bd6dd135009d -r fc9708e1d17c web.c
--- a/web.c	Wed Jul 31 15:09:15 2019 +0000
+++ b/web.c	Wed Aug 28 07:12:39 2019 +0000
@@ -3,19 +3,14 @@
 #include "web-server-this.h"
 #include "web.h"
 #include "web-pages-base.h"
+#include "web-connection.h"
 #include "mstimer.h"
+#include "log.h"
 
 #define LOGIN_DELAY_MS 200
 
 #define DO_LOGIN DO_SERVER + 0
 
-struct state
-{
-    int      toDo;
-    bool     postComplete;
-    uint32_t delayUntil;
-};
-
 static int decideWhatToDo(char *pPath, char* pLastModified)
 {
     if (HttpSameStr(pPath, "/login")) return DO_LOGIN;
@@ -55,12 +50,18 @@
     if (WebServerBaseReply(todo)) return;
     if (WebServerThisReply(todo)) return;
 }
-
-static void handleRequest(char* pStateData, int size, char* pRequestStream, uint32_t positionInRequestStream)
+static void handleRequest(int connectionId, int size, char* pRequestStream, uint32_t positionInRequestStream)
 {
-    struct state* pState = (struct state*)pStateData;
+    struct WebConnection* pConnection = NULL;
+    if (!positionInRequestStream) pConnection = WebConnectionNew(connectionId);
+    else                          pConnection = WebConnectionOrNull(connectionId);
+    if (!pConnection)
+    {
+        LogTimeF("WebRequest - no connection corresponds to id %d\r\n", connectionId);
+        return;
+    }
     
-    pState->delayUntil = MsTimerCount; //Default to no delay unless modified;
+    pConnection->delayUntil = MsTimerCount; //Default to no delay unless modified;
     
     //Handle request for the first packet of data received but leave todo the same after that.
     int contentLength = 0;
@@ -76,59 +77,60 @@
         contentStart = HttpRequestRead(pRequestStream, size, &pMethod, &pPath, &pQuery, &pLastModified, &pCookies, &contentLength);
         
         //Ask the web server what to do
-        pState->toDo = decideWhatToDo(pPath, pLastModified);
+        pConnection->toDo = decideWhatToDo(pPath, pLastModified);
         
         //If what to do is NOTHING, NOT_FOUND or NOT_MODIFIED then no query or post will be valid so stop now
-        if (pState->toDo < DO_LOGIN) { pState->postComplete = true; return; }
+        if (pConnection->toDo < DO_LOGIN) { pConnection->postComplete = true; return; }
         
         //If what to do is LOGIN then the user has just returned the login form
-        if (pState->toDo == DO_LOGIN)
+        if (pConnection->toDo == DO_LOGIN)
         {
-            handleQuery(pState->toDo, pQuery);                  //Read the password and the original location
+            handleQuery(pConnection->toDo, pQuery);                  //Read the password and the original location
             if (WebLoginQueryPasswordOk)
             {
                 if (!WebLoginSessionIdIsSet())           //If there isn't a session id already
                 {
                     WebLoginSessionIdNew();              //Create a new session id
                 }
-                pState->toDo =  WebLoginOriginalToDo;          //Load the original todo and SEND_SESSION_ID
-                pState->toDo += DO_SEND_SESSION_ID;
+                pConnection->toDo =  WebLoginOriginalToDo;          //Load the original todo and SEND_SESSION_ID
+                pConnection->toDo += DO_SEND_SESSION_ID;
             }
-            pState->delayUntil = MsTimerCount + LOGIN_DELAY_MS; //To prevent brute forcing the hash delay the reply to the login
-            pState->postComplete = true;
+            pConnection->delayUntil = MsTimerCount + LOGIN_DELAY_MS; //To prevent brute forcing the hash delay the reply to the login
+            pConnection->postComplete = true;
             return;                                       //Either way no query or post will be valid
         }
         
         //Have a normal request so authenticate
         if (!WebLoginCookiesContainValidSessionId(pCookies))
         {
-            WebLoginOriginalToDo = pState->toDo; //Record the original destination for redirection
-            pState->toDo = DO_LOGIN;
-            pState->postComplete = true;
+            WebLoginOriginalToDo = pConnection->toDo; //Record the original destination for redirection
+            pConnection->toDo = DO_LOGIN;
+            pConnection->postComplete = true;
             return; //Ignore any query or post as the user is not authenticated
         }
         
         //Handle the query
-        handleQuery(pState->toDo, pQuery);
+        handleQuery(pConnection->toDo, pQuery);
     }
     
     //Do the upload of anything that needs it. Todos it doesn't understand are ignored.
-    if (!pState->postComplete) handlePost(pState->toDo, contentLength, contentStart, size, pRequestStream, positionInRequestStream, &pState->postComplete);
+    if (!pConnection->postComplete) handlePost(pConnection->toDo, contentLength, contentStart, size, pRequestStream, positionInRequestStream, &pConnection->postComplete);
 }
 
-static bool pollSomethingToSend(char* pStateData, bool clientFinished) //returns true if finished; false if not; 
+static bool pollSomethingToSend(int connectionId, bool clientFinished) //returns true if finished; false if not; 
 {
-    struct state* pState = (struct state*)pStateData;
+    struct WebConnection* pConnection = WebConnectionOrNull(connectionId);
+    if (!pConnection) return false;
     
-    if (!pState->toDo)
+    if (!pConnection->toDo)
     {
         if (clientFinished) return true;  //The client hasn't requested anything and never will so finish
         else                return false; //The client hasn't requested anything yet but still could
     }
-    if (!pState->postComplete               ) return false; //Wait for the request (usually a POST) to finish
-    if (!MsTimerAbsolute(pState->delayUntil)) return false; //Wait a while (usually after a LOGIN attempt)
+    if (!pConnection->postComplete               ) return false; //Wait for the request (usually a POST) to finish
+    if (!MsTimerAbsolute(pConnection->delayUntil)) return false; //Wait a while (usually after a LOGIN attempt)
     
-    int todo = pState->toDo; //Make a copy so that we don't modify todo in the state
+    int todo = pConnection->toDo; //Make a copy so that we don't modify todo in the state
 
     //Check if todo includes the need to send a cookie
     if (todo >= DO_SEND_SESSION_ID)
@@ -152,8 +154,9 @@
 
 int WebInit()
 {
-    HttpRequestFunction = handleRequest;
-    HttpPollFunction    = pollSomethingToSend;
+    HttpFunctionReset   = WebConnectionReset;
+    HttpFunctionRequest = handleRequest;
+    HttpFunctionPoll    = pollSomethingToSend;
     
     WebLoginInit();