A simple library to support serving https.
Dependents: oldheating gps motorhome heating
Diff: tls/tls-request.c
- Revision:
- 4:6a1d887f1cad
- Parent:
- 2:82268409e83f
- Child:
- 5:ee5489ee1117
--- a/tls/tls-request.c Fri Aug 02 15:07:18 2019 +0000 +++ b/tls/tls-request.c Tue Aug 20 14:50:48 2019 +0000 @@ -8,25 +8,29 @@ #include "log.h" #include "pri-key.h" -static int handleClientHello(int length, char* pBuffer, struct TlsState* pState) //returns 0 on success; -1 on error -{ +static int handleClientHello(int length, uint8_t* pBuffer, struct TlsState* pState) //returns 0 on success; -1 on error +{ //Check things look ok - char* p = pBuffer; + uint8_t* p = pBuffer; if (length < 100) { LogF("TLS - %d byte client hello message is not at least 100 bytes long\r\n", length); return -1; } + //Start and add the handshake hash + Sha256Start(&pState->handshakeHash); + Sha256Add (&pState->handshakeHash, pBuffer, length); + //Read in the parameters - char versionH = *p++; - char versionL = *p++; + uint8_t versionH = *p++; + uint8_t versionL = *p++; - char* pRandom = p; + uint8_t* pRandom = p; p += 32; int sessionIdLength = *p++; - char* pSessionId = p; + uint8_t* pSessionId = p; //Handle the parameters pState->session = -1; @@ -53,10 +57,12 @@ } return 0; } -static int handleClientKeyExchange(int length, char* pBuffer, struct TlsState* pState) //returns 0 on success; -1 on error +static int handleClientKeyExchange(int length, uint8_t* pBuffer, struct TlsState* pState) //returns 0 on success; -1 on error { struct TlsSession* pSession = TlsSessionGetFromIndex(pState->session); + Sha256Add (&pState->handshakeHash, pBuffer, length); + if (length != 130) { LogF("TLS - %d byte client key exchange message is not 130 bytes long\r\n", length); @@ -68,7 +74,7 @@ LogF("TLS - %d byte encrypted pre master secret is not 128 bytes long\r\n", length); return -1; } - char* pEncryptedPreMasterSecret = pBuffer + 2; + uint8_t* pEncryptedPreMasterSecret = pBuffer + 2; pSession->slotPriKeyDecryption = PriKeyDecryptStart(pEncryptedPreMasterSecret); if (TlsTrace) @@ -80,12 +86,12 @@ return 0; } -static void handleHandshake(int length, char* pBuffer, struct TlsState* pState) +static void handleHandshake(int length, uint8_t* pBuffer, struct TlsState* pState) { - char* p = pBuffer; + uint8_t* p = pBuffer; while (p < pBuffer + length) { - char handshakeType = *p++; + uint8_t handshakeType = *p++; int handshakeLength = *p++ << 16; handshakeLength |= *p++ << 8; handshakeLength |= *p++ ; //Handshake length 3 bytes @@ -118,17 +124,17 @@ p += handshakeLength; } } -static void handleAlert(int length, char* pBuffer) +static void handleAlert(int length, uint8_t* pBuffer) { - char level = pBuffer[0]; - char description = pBuffer[1]; + uint8_t level = pBuffer[0]; + uint8_t description = pBuffer[1]; if (TlsTrace) { Log("- alert level: "); TlsLogAlertLevel (level); Log("\r\n"); Log("- alert description: "); TlsLogAlertDescription(description); Log("\r\n"); } } -static void handleApplication(int length, char* pBuffer) +static void handleApplication(int length, uint8_t* pBuffer) { if (TlsTrace) { @@ -137,7 +143,7 @@ Log("\r\n"); } } -void TlsRequest(char* pTlsState, char* pWebState, int size, char* pRequestStream, uint32_t positionInRequestStream) +void TlsRequest(char* pTlsState, char* pWebState, int size, uint8_t* pRequestStream, uint32_t positionInRequestStream) { struct TlsState* pState = (struct TlsState*)pTlsState; @@ -145,9 +151,9 @@ if (size == 0) return; //if (positionInRequestStream != 0) return; - char contentType = pRequestStream[0]; - char versionH = pRequestStream[1]; - char versionL = pRequestStream[2]; + uint8_t contentType = pRequestStream[0]; + uint8_t versionH = pRequestStream[1]; + uint8_t versionL = pRequestStream[2]; int length = pRequestStream[3] << 8 | pRequestStream[4]; //Length (2 bytes) if (TlsTrace) {