A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

Revision:
10:e269fd7b9500
Parent:
9:f354b4859b0b
Child:
12:2c342345b3db
--- a/tls/tls-request.c	Wed Sep 11 07:24:21 2019 +0000
+++ b/tls/tls-request.c	Tue Sep 24 18:11:02 2019 +0000
@@ -33,15 +33,28 @@
     uint8_t* pSessionId = p;
     
     //Handle the parameters
-    pConnection->session = -1;
-    if (sessionIdLength == 1) pConnection->session = *pSessionId;
-    struct TlsSession* pSession = TlsSessionOrNull(pConnection->session);
-    if (!pSession || pSession->state != TLS_SESSION_VALID)
+    if (sessionIdLength == 4)
     {
-        pSession = TlsSessionGetOldest();
-        pSession->state = TLS_SESSION_STARTED;
+        pConnection->sessionId  = *p++;
+        pConnection->sessionId <<= 8;
+        pConnection->sessionId |= *p++;
+        pConnection->sessionId <<= 8;
+        pConnection->sessionId |= *p++;
+        pConnection->sessionId <<= 8;
+        pConnection->sessionId |= *p++;
     }
-    pConnection->session = TlsSessionGetIndex(pSession);
+    else
+    {
+        pConnection->sessionId = 0;
+        p += sessionIdLength;
+    }
+//    struct TlsSession* pSession = TlsSessionOrNull(pConnection->sessionId);
+    struct TlsSession* pSession = NULL;
+    if (!pSession || !pSession->valid)
+    {
+        pSession = TlsSessionNew();
+        pConnection->sessionId = pSession->id;
+    }
 
     pSession->lastUsed = MsTimerCount;
     
@@ -51,16 +64,16 @@
         LogF("- client version HH:LL: %02x:%02x\r\n", versionH, versionL);
         Log ("- client random:\r\n");     LogBytesAsHex(pConnection->clientRandom, 32); Log("\r\n");
         Log ("- client session id:\r\n"); LogBytesAsHex(pSessionId, sessionIdLength); Log("\r\n");
-        LogF("- session index: %d\r\n",  pConnection->session);
+        LogF("- session index: %u\r\n",  pConnection->sessionId);
     }
     return 0;
 }
 static int handleClientKeyExchange(int length, uint8_t* pBuffer, struct TlsConnection* pConnection) //returns 0 on success; -1 on error
 {
-    struct TlsSession* pSession = TlsSessionOrNull(pConnection->session);
+    struct TlsSession* pSession = TlsSessionOrNull(pConnection->sessionId);
     if (!pSession)
     {
-        LogTimeF("handleClientKeyExchange - invalid session %d\r\n", pConnection->session);
+        LogTimeF("handleClientKeyExchange - invalid session %u\r\n", pConnection->sessionId);
         return -1;
     }
     
@@ -89,10 +102,10 @@
 }
 static int handleClientFinished(int length, uint8_t* pBuffer, struct TlsConnection* pConnection) //returns 0 on success; -1 on error
 {
-    struct TlsSession* pSession = TlsSessionOrNull(pConnection->session);
+    struct TlsSession* pSession = TlsSessionOrNull(pConnection->sessionId);
     if (!pSession)
     {
-        LogTimeF("handleClientKeyExchange - invalid session %d\r\n", pConnection->session);
+        LogTimeF("handleClientKeyExchange - invalid session %u\r\n", pConnection->sessionId);
         return -1;
     }
     
@@ -140,7 +153,7 @@
         LogBytesAsHex(pBuffer, length);
         Log("\r\n");
     }
-    HttpFunctionRequest(pConnection->id, length, (char*)pBuffer, 0);
+    HttpRequest(pConnection->id, length, (char*)pBuffer, 0);
 }
 
 static void handleHandshake(int length, uint8_t* pBuffer, struct TlsConnection* pConnection)
@@ -208,10 +221,10 @@
     if (versionH    != 3                    ) return overallLen;
     if (overallLen  > available             ) return overallLen;
     
-    struct TlsSession* pSession = TlsSessionOrNull(pConnection->session);
+    struct TlsSession* pSession = TlsSessionOrNull(pConnection->sessionId);
     if (!pSession)
     {
-        LogTimeF("TlsRequestAsync - invalid session %d\r\n", pConnection->session);
+        LogTimeF("TlsRequestAsync - invalid session %u\r\n", pConnection->sessionId);
         return overallLen;
     }
     
@@ -325,14 +338,14 @@
     
     if (pConnection->toDo != DO_WAIT_DECRYPT_MASTER_SECRET) return;
     
-    struct TlsSession* pSession = TlsSessionOrNull(pConnection->session);
+    struct TlsSession* pSession = TlsSessionOrNull(pConnection->sessionId);
     if (!pSession)
     {
-        LogTimeF("TlsRequestAsync - invalid session %d\r\n", pConnection->session);
+        LogTimeF("TlsRequestAsync - invalid session %u\r\n", pConnection->sessionId);
         return;
     }
     
-    if (pSession->state == TLS_SESSION_VALID) return;
+    if (pSession->valid) return;
     
     if (!PriKeyDecryptFinished(pSession->slotPriKeyDecryption)) return;
 
@@ -351,7 +364,7 @@
                                                                                               pConnection->clientWriteKey,
                                                                                               pConnection->serverWriteKey);
         
-    pSession->state = TLS_SESSION_VALID;
+    pSession->valid = true;
     
     LogTime("Sending deferred encrypted bytes\r\n"); LogBytesAsHex(pConnection->deferredContent, TLS_DEFERRED_CONTENT_SIZE); Log("\r\n");
     
@@ -360,4 +373,5 @@
 void TlsReset(int connectionId)
 {
     TlsConnectionReset(connectionId);
+    HttpReset(connectionId);
 }