A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

Revision:
16:7eeb5f6626ad
Parent:
15:4ddb73b5fea1
Child:
17:93feb2a51d58
diff -r 4ddb73b5fea1 -r 7eeb5f6626ad tls/tls-response.c
--- a/tls/tls-response.c	Fri Oct 04 18:25:55 2019 +0000
+++ b/tls/tls-response.c	Sun Oct 06 08:00:30 2019 +0000
@@ -37,12 +37,16 @@
     encryptIvPointer = p;
     for (int i = 0; i < AES_BLOCKLEN; i++) *p++ = RandomGetByte();
     
+    encryptPayloadPointer = p;
+    
     *pp = p;
 }
 static void encryptAddMac(uint8_t** pp, struct TlsConnection* pConnection, uint8_t contentType)
 {
     uint8_t* p = *pp;
     
+    encryptPayloadSize = p - encryptPayloadPointer;
+    
     //Add the MAC
     TlsMacSha1(TLS_KEY_SIZE_MAC,
                pConnection->serverMacKey,
@@ -157,10 +161,8 @@
     uint8_t* p = *pp;
     
     encryptAddIv(&p);
-    encryptPayloadPointer = p;
-    encryptPayloadSize = 16;
     
-    //Make the 'finished' handshake which is the payload to be encrypted
+    //Make the 'finished' handshake which is part of the payload to be encrypted
     *p++ = TLS_HANDSHAKE_FINISHED;
     *p++ = 0x00;
     *p++ = 0x00;
@@ -301,33 +303,26 @@
     encryptAddIv(&p);
 
     //Add the plain payload
-    encryptPayloadPointer = p;
-    encryptPayloadSize = *pWindowSize - CONTENT_MAX_OVERHEAD;
-    LogF("- available payload size %d\r\n", encryptPayloadSize);
+    int payloadSize = *pWindowSize - CONTENT_MAX_OVERHEAD;
+    LogF("- available payload size %d\r\n", payloadSize);
     uint32_t positionOfPayloadInStream = positionOfWindowInStream - pConnection->serverPositionInStreamOffset;
     LogF("- position of payload in stream %d\r\n", positionOfPayloadInStream);
-    bool finished = HttpAdd(pConnection->id, &encryptPayloadSize, (char*)p, positionOfPayloadInStream); //Return whatever HTTP would be
-    LogF("- resulting payload size %d\r\n", encryptPayloadSize);
-    p += encryptPayloadSize;
+    bool finished = HttpAdd(pConnection->id, &payloadSize, (char*)p, positionOfPayloadInStream); //Return whatever HTTP would be
+    LogF("- resulting payload size %d\r\n", payloadSize);
+    p += payloadSize;
     
     encryptAddMac    (&p, pConnection, TLS_CONTENT_TYPE_APPLICATION);
     encryptAddPadding(&p);
+    encryptPayload   ( p, pConnection);
     
     //Backfill the size
     backfillSize(p, pBackfillSize);
     
-    //Calculate the resulting window size
+    //Finalise
+    pConnection->serverSequence++;
     *pWindowSize = p - pWindow;
     LogF("- resulting window size %d\r\n", *pWindowSize);
-    
-    //Log the plain content
-    Log("- plain content\r\n"); LogBytesAsHex(pWindow, *pWindowSize); Log("\r\n");
-    
-    encryptPayload(p, pConnection);
-    
-    //Finalise
-    pConnection->serverSequence++;
-    pConnection->serverPositionInStreamOffset += *pWindowSize - encryptPayloadSize;
+    pConnection->serverPositionInStreamOffset += *pWindowSize - payloadSize;
     
     return finished;
 }