Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Mon May 27 10:12:33 2019 +0000
Parent:
147:a6093b52e654
Child:
149:39d1ba392f4b
Commit message:
Continued set up of https in the TLS module.

Changed in this revision

tcp/tls/tls.c Show annotated file Show diff for this revision Revisions of this file
--- a/tcp/tls/tls.c	Fri May 17 15:01:32 2019 +0000
+++ b/tcp/tls/tls.c	Mon May 27 10:12:33 2019 +0000
@@ -28,9 +28,11 @@
 #define TLS_HANDSHAKE_ClientKeyExchange    16
 #define TLS_HANDSHAKE_Finished             20
 
-#define DO_NOTHING      0
-#define DO_SERVER_HELLO 1
-#define DO_APPLICATION  2
+#define DO_WAIT_CLIENT_HELLO  0
+#define DO_SEND_SERVER_HELLO  1
+#define DO_WAIT_CLIENT_CHANGE 2
+#define DO_SEND_SERVER_CHANGE 3
+#define DO_APPLICATION        4
 
 bool TlsTrace = true;
 
@@ -78,31 +80,56 @@
     if (size == 0) return;
     if (positionInRequestStream != 0) return;
     char contentType = pRequestStream[0];
-    if (TlsTrace) { Log("      content type: "); logContentType(contentType); Log("\r\n"); }
+    uint16_t version = pRequestStream[1] << 8 | pRequestStream[2];
+    uint16_t length  = pRequestStream[3] << 8 | pRequestStream[4];
+    if (TlsTrace)
+    {
+        Log ("      content type: "); logContentType(contentType); Log("\r\n");
+        LogF("      version:      %04x\r\n", version);
+        LogF("      length:       %d\r\n"  , length );
+    }
     switch (contentType)
     {
         case TLS_CONTENT_TYPE_Handshake:
-            {
-                char handshakeType = pRequestStream[5];
-                if (TlsTrace) { Log("      handshake type: "); logHandshakeType(handshakeType); Log("\r\n"); }
-                pState->toDo = DO_SERVER_HELLO;
-                return;
-            }
+        {
+            char handshakeType = pRequestStream[5];
+            if (TlsTrace) { Log("      handshake type: "); logHandshakeType(handshakeType); Log("\r\n"); }
+            pState->toDo = DO_SEND_SERVER_HELLO;
+            return;
+        }
         case TLS_CONTENT_TYPE_Application:
-            {
-                pState->toDo = DO_APPLICATION;
-                return;
-            }
+        {
+            pState->toDo = DO_APPLICATION;
+            return;
+        }
+        
         default:
             Log("TLS - ignoring untreated content type\r\n");
-            pState->toDo = DO_NOTHING;
+            pState->toDo = DO_WAIT_CLIENT_HELLO;
             return;
     }
     //ECDHE-RSA-AES128-GCM-SHA256
 }
 static void sendServerHello()
 {
+    /*
+    ProtocolVersion server_version;
+          Random            random; 4 byte unix time + 28 byte random
+          SessionID         session_id;
+          CipherSuite       cipher_suite;
+          CompressionMethod compression_method;
+    */
     Log("     sending server hello\r\n");
+    TcpBufAddChar(TLS_CONTENT_TYPE_Handshake);
+    TcpBufAddChar(0x03); TcpBufAddChar(0x01); //TLS
+    TcpBufAddChar(0x01); TcpBufAddChar(0x00); //Length
+    TcpBufAddChar(TLS_HANDSHAKE_ServerHello);
+    TcpBufAddChar(0x01); TcpBufAddChar(0x00); //Length 85
+    TcpBufAddChar(0x03); TcpBufAddChar(0x03); //TLS 1.2
+    
+    TcpBufAddChar(0x00);                      //SessionId length 0
+    TcpBufAddChar(0x00); TcpBufAddChar(0x2f); //Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA (0x002f)
+    TcpBufAddChar(0x00);                      //Compression type null
 }
 
 int TlsPoll(char* pTlsState, char* pWebState, bool clientFinished)
@@ -111,7 +138,7 @@
     
     switch (pState->toDo)
     {
-        case DO_NOTHING:
+        case DO_WAIT_CLIENT_HELLO:
             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
@@ -122,9 +149,11 @@
 {
     struct state* pState = (struct state*)pTlsState;
     
-    if ( pState->toDo == DO_SERVER_HELLO) sendServerHello();
-
-    return false; //Finished
+    switch(pState->toDo)
+    {
+        case DO_SEND_SERVER_HELLO: sendServerHello(); return true;
+        default:                                      return true; //Finished
+    }
 }
 static char encrypt(char c)
 {