Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Fri May 17 15:01:32 2019 +0000
Revision:
147:a6093b52e654
Parent:
145:206bf0d073c7
Child:
148:5489d36986e5
Split HttpPollReply into HttpPoll and HttpReply to allow TSL to work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 145:206bf0d073c7 1 #include <stdbool.h>
andrewboyson 145:206bf0d073c7 2
andrewboyson 147:a6093b52e654 3 #include "http.h"
andrewboyson 145:206bf0d073c7 4 #include "tcpbuf.h"
andrewboyson 145:206bf0d073c7 5 #include "action.h"
andrewboyson 147:a6093b52e654 6 #include "net.h"
andrewboyson 147:a6093b52e654 7 #include "log.h"
andrewboyson 147:a6093b52e654 8 #include "led.h"
andrewboyson 145:206bf0d073c7 9 #include "restart.h"
andrewboyson 145:206bf0d073c7 10 #include "mstimer.h"
andrewboyson 145:206bf0d073c7 11
andrewboyson 145:206bf0d073c7 12 #define TLS_CONTENT_TYPE_ChangeCipher 20
andrewboyson 145:206bf0d073c7 13 #define TLS_CONTENT_TYPE_Alert 21
andrewboyson 145:206bf0d073c7 14 #define TLS_CONTENT_TYPE_Handshake 22
andrewboyson 145:206bf0d073c7 15 #define TLS_CONTENT_TYPE_Application 23
andrewboyson 145:206bf0d073c7 16 #define TLS_CONTENT_TYPE_Heartbeat 24
andrewboyson 145:206bf0d073c7 17
andrewboyson 145:206bf0d073c7 18 #define TLS_HANDSHAKE_HelloRequest 0
andrewboyson 145:206bf0d073c7 19 #define TLS_HANDSHAKE_ClientHello 1
andrewboyson 145:206bf0d073c7 20 #define TLS_HANDSHAKE_ServerHello 2
andrewboyson 145:206bf0d073c7 21 #define TLS_HANDSHAKE_NewSessionTicket 4
andrewboyson 145:206bf0d073c7 22 #define TLS_HANDSHAKE_EncryptedExtensions 8
andrewboyson 145:206bf0d073c7 23 #define TLS_HANDSHAKE_Certificate 11
andrewboyson 145:206bf0d073c7 24 #define TLS_HANDSHAKE_ServerKeyExchange 12
andrewboyson 145:206bf0d073c7 25 #define TLS_HANDSHAKE_CertificateRequest 13
andrewboyson 145:206bf0d073c7 26 #define TLS_HANDSHAKE_ServerHelloDone 14
andrewboyson 145:206bf0d073c7 27 #define TLS_HANDSHAKE_CertificateVerify 15
andrewboyson 145:206bf0d073c7 28 #define TLS_HANDSHAKE_ClientKeyExchange 16
andrewboyson 145:206bf0d073c7 29 #define TLS_HANDSHAKE_Finished 20
andrewboyson 145:206bf0d073c7 30
andrewboyson 147:a6093b52e654 31 #define DO_NOTHING 0
andrewboyson 147:a6093b52e654 32 #define DO_SERVER_HELLO 1
andrewboyson 147:a6093b52e654 33 #define DO_APPLICATION 2
andrewboyson 147:a6093b52e654 34
andrewboyson 147:a6093b52e654 35 bool TlsTrace = true;
andrewboyson 145:206bf0d073c7 36
andrewboyson 147:a6093b52e654 37 struct state
andrewboyson 147:a6093b52e654 38 {
andrewboyson 147:a6093b52e654 39 int toDo;
andrewboyson 147:a6093b52e654 40 };
andrewboyson 147:a6093b52e654 41 static void logContentType(char contentType)
andrewboyson 147:a6093b52e654 42 {
andrewboyson 147:a6093b52e654 43 switch (contentType)
andrewboyson 147:a6093b52e654 44 {
andrewboyson 147:a6093b52e654 45 case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher"); break;
andrewboyson 147:a6093b52e654 46 case TLS_CONTENT_TYPE_Alert: Log ("Alert"); break;
andrewboyson 147:a6093b52e654 47 case TLS_CONTENT_TYPE_Handshake: Log ("Handshake"); break;
andrewboyson 147:a6093b52e654 48 case TLS_CONTENT_TYPE_Application: Log ("Application"); break;
andrewboyson 147:a6093b52e654 49 case TLS_CONTENT_TYPE_Heartbeat: Log ("Heartbeat"); break;
andrewboyson 147:a6093b52e654 50 default: LogF("%02hX", contentType); break;
andrewboyson 147:a6093b52e654 51 }
andrewboyson 147:a6093b52e654 52 }
andrewboyson 147:a6093b52e654 53 static void logHandshakeType(char handshakeType)
andrewboyson 145:206bf0d073c7 54 {
andrewboyson 145:206bf0d073c7 55 switch (handshakeType)
andrewboyson 145:206bf0d073c7 56 {
andrewboyson 145:206bf0d073c7 57 case TLS_HANDSHAKE_HelloRequest: Log ("Hello request"); break;
andrewboyson 145:206bf0d073c7 58 case TLS_HANDSHAKE_ClientHello: Log ("Client hello"); break;
andrewboyson 145:206bf0d073c7 59 case TLS_HANDSHAKE_ServerHello: Log ("Server hello"); break;
andrewboyson 145:206bf0d073c7 60 case TLS_HANDSHAKE_NewSessionTicket: Log ("New session ticket"); break;
andrewboyson 145:206bf0d073c7 61 case TLS_HANDSHAKE_EncryptedExtensions: Log ("Encrypted extensions"); break;
andrewboyson 145:206bf0d073c7 62 case TLS_HANDSHAKE_Certificate: Log ("Certificate"); break;
andrewboyson 145:206bf0d073c7 63 case TLS_HANDSHAKE_ServerKeyExchange: Log ("Server key exchange"); break;
andrewboyson 145:206bf0d073c7 64 case TLS_HANDSHAKE_CertificateRequest: Log ("Certificate request"); break;
andrewboyson 145:206bf0d073c7 65 case TLS_HANDSHAKE_ServerHelloDone: Log ("Server hello done"); break;
andrewboyson 145:206bf0d073c7 66 case TLS_HANDSHAKE_CertificateVerify: Log ("Certificate verify"); break;
andrewboyson 145:206bf0d073c7 67 case TLS_HANDSHAKE_ClientKeyExchange: Log ("Client key exchange"); break;
andrewboyson 145:206bf0d073c7 68 case TLS_HANDSHAKE_Finished: Log ("Finished"); break;
andrewboyson 145:206bf0d073c7 69 default: LogF("%02hX", handshakeType); break;
andrewboyson 145:206bf0d073c7 70 }
andrewboyson 145:206bf0d073c7 71 }
andrewboyson 147:a6093b52e654 72 void TlsRequest(char* pTlsState, char* pWebState, int size, char* pRequestStream, uint32_t positionInRequestStream)
andrewboyson 145:206bf0d073c7 73 {
andrewboyson 147:a6093b52e654 74 struct state* pState = (struct state*)pTlsState;
andrewboyson 147:a6093b52e654 75
andrewboyson 147:a6093b52e654 76 if (TlsTrace) LogF("TLS <<< %d (%u)\r\n", size, positionInRequestStream);
andrewboyson 145:206bf0d073c7 77
andrewboyson 145:206bf0d073c7 78 if (size == 0) return;
andrewboyson 145:206bf0d073c7 79 if (positionInRequestStream != 0) return;
andrewboyson 145:206bf0d073c7 80 char contentType = pRequestStream[0];
andrewboyson 147:a6093b52e654 81 if (TlsTrace) { Log(" content type: "); logContentType(contentType); Log("\r\n"); }
andrewboyson 145:206bf0d073c7 82 switch (contentType)
andrewboyson 145:206bf0d073c7 83 {
andrewboyson 145:206bf0d073c7 84 case TLS_CONTENT_TYPE_Handshake:
andrewboyson 145:206bf0d073c7 85 {
andrewboyson 145:206bf0d073c7 86 char handshakeType = pRequestStream[5];
andrewboyson 147:a6093b52e654 87 if (TlsTrace) { Log(" handshake type: "); logHandshakeType(handshakeType); Log("\r\n"); }
andrewboyson 147:a6093b52e654 88 pState->toDo = DO_SERVER_HELLO;
andrewboyson 147:a6093b52e654 89 return;
andrewboyson 147:a6093b52e654 90 }
andrewboyson 147:a6093b52e654 91 case TLS_CONTENT_TYPE_Application:
andrewboyson 147:a6093b52e654 92 {
andrewboyson 147:a6093b52e654 93 pState->toDo = DO_APPLICATION;
andrewboyson 145:206bf0d073c7 94 return;
andrewboyson 145:206bf0d073c7 95 }
andrewboyson 145:206bf0d073c7 96 default:
andrewboyson 147:a6093b52e654 97 Log("TLS - ignoring untreated content type\r\n");
andrewboyson 147:a6093b52e654 98 pState->toDo = DO_NOTHING;
andrewboyson 145:206bf0d073c7 99 return;
andrewboyson 145:206bf0d073c7 100 }
andrewboyson 145:206bf0d073c7 101 //ECDHE-RSA-AES128-GCM-SHA256
andrewboyson 145:206bf0d073c7 102 }
andrewboyson 147:a6093b52e654 103 static void sendServerHello()
andrewboyson 145:206bf0d073c7 104 {
andrewboyson 145:206bf0d073c7 105 Log(" sending server hello\r\n");
andrewboyson 145:206bf0d073c7 106 }
andrewboyson 147:a6093b52e654 107
andrewboyson 147:a6093b52e654 108 int TlsPoll(char* pTlsState, char* pWebState, bool clientFinished)
andrewboyson 145:206bf0d073c7 109 {
andrewboyson 147:a6093b52e654 110 struct state* pState = (struct state*)pTlsState;
andrewboyson 147:a6093b52e654 111
andrewboyson 147:a6093b52e654 112 switch (pState->toDo)
andrewboyson 147:a6093b52e654 113 {
andrewboyson 147:a6093b52e654 114 case DO_NOTHING:
andrewboyson 147:a6093b52e654 115 if (clientFinished) return -1; //The client hasn't made a request and never will so finish
andrewboyson 147:a6093b52e654 116 else return 0; //The client hasn't made a request yet but it could.
andrewboyson 147:a6093b52e654 117 case DO_APPLICATION: return HttpPollFunction(pWebState, clientFinished); //Return whatever HTTP would be
andrewboyson 147:a6093b52e654 118 default: return 1; //The client has made a request so do it
andrewboyson 147:a6093b52e654 119 }
andrewboyson 147:a6093b52e654 120 }
andrewboyson 147:a6093b52e654 121 bool TlsReply(char* pTlsState, char* pWebState)
andrewboyson 147:a6093b52e654 122 {
andrewboyson 147:a6093b52e654 123 struct state* pState = (struct state*)pTlsState;
andrewboyson 147:a6093b52e654 124
andrewboyson 147:a6093b52e654 125 if ( pState->toDo == DO_SERVER_HELLO) sendServerHello();
andrewboyson 145:206bf0d073c7 126
andrewboyson 147:a6093b52e654 127 return false; //Finished
andrewboyson 147:a6093b52e654 128 }
andrewboyson 147:a6093b52e654 129 static char encrypt(char c)
andrewboyson 147:a6093b52e654 130 {
andrewboyson 147:a6093b52e654 131 return c; //Implement encryption
andrewboyson 145:206bf0d073c7 132 }
andrewboyson 145:206bf0d073c7 133 void TlsAddChar(char c)
andrewboyson 145:206bf0d073c7 134 {
andrewboyson 147:a6093b52e654 135 char e = encrypt(c);
andrewboyson 147:a6093b52e654 136 TcpBufAddChar(e);
andrewboyson 145:206bf0d073c7 137 }