Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
tcp/tls/tls.c
- Committer:
- andrewboyson
- Date:
- 2019-05-17
- Revision:
- 147:a6093b52e654
- Parent:
- 145:206bf0d073c7
- Child:
- 148:5489d36986e5
File content as of revision 147:a6093b52e654:
#include <stdbool.h>
#include "http.h"
#include "tcpbuf.h"
#include "action.h"
#include "net.h"
#include "log.h"
#include "led.h"
#include "restart.h"
#include "mstimer.h"
#define TLS_CONTENT_TYPE_ChangeCipher 20
#define TLS_CONTENT_TYPE_Alert 21
#define TLS_CONTENT_TYPE_Handshake 22
#define TLS_CONTENT_TYPE_Application 23
#define TLS_CONTENT_TYPE_Heartbeat 24
#define TLS_HANDSHAKE_HelloRequest 0
#define TLS_HANDSHAKE_ClientHello 1
#define TLS_HANDSHAKE_ServerHello 2
#define TLS_HANDSHAKE_NewSessionTicket 4
#define TLS_HANDSHAKE_EncryptedExtensions 8
#define TLS_HANDSHAKE_Certificate 11
#define TLS_HANDSHAKE_ServerKeyExchange 12
#define TLS_HANDSHAKE_CertificateRequest 13
#define TLS_HANDSHAKE_ServerHelloDone 14
#define TLS_HANDSHAKE_CertificateVerify 15
#define TLS_HANDSHAKE_ClientKeyExchange 16
#define TLS_HANDSHAKE_Finished 20
#define DO_NOTHING 0
#define DO_SERVER_HELLO 1
#define DO_APPLICATION 2
bool TlsTrace = true;
struct state
{
int toDo;
};
static void logContentType(char contentType)
{
switch (contentType)
{
case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher"); break;
case TLS_CONTENT_TYPE_Alert: Log ("Alert"); break;
case TLS_CONTENT_TYPE_Handshake: Log ("Handshake"); break;
case TLS_CONTENT_TYPE_Application: Log ("Application"); break;
case TLS_CONTENT_TYPE_Heartbeat: Log ("Heartbeat"); break;
default: LogF("%02hX", contentType); break;
}
}
static void logHandshakeType(char handshakeType)
{
switch (handshakeType)
{
case TLS_HANDSHAKE_HelloRequest: Log ("Hello request"); break;
case TLS_HANDSHAKE_ClientHello: Log ("Client hello"); break;
case TLS_HANDSHAKE_ServerHello: Log ("Server hello"); break;
case TLS_HANDSHAKE_NewSessionTicket: Log ("New session ticket"); break;
case TLS_HANDSHAKE_EncryptedExtensions: Log ("Encrypted extensions"); break;
case TLS_HANDSHAKE_Certificate: Log ("Certificate"); break;
case TLS_HANDSHAKE_ServerKeyExchange: Log ("Server key exchange"); break;
case TLS_HANDSHAKE_CertificateRequest: Log ("Certificate request"); break;
case TLS_HANDSHAKE_ServerHelloDone: Log ("Server hello done"); break;
case TLS_HANDSHAKE_CertificateVerify: Log ("Certificate verify"); break;
case TLS_HANDSHAKE_ClientKeyExchange: Log ("Client key exchange"); break;
case TLS_HANDSHAKE_Finished: Log ("Finished"); break;
default: LogF("%02hX", handshakeType); break;
}
}
void TlsRequest(char* pTlsState, char* pWebState, int size, char* pRequestStream, uint32_t positionInRequestStream)
{
struct state* pState = (struct state*)pTlsState;
if (TlsTrace) LogF("TLS <<< %d (%u)\r\n", size, positionInRequestStream);
if (size == 0) return;
if (positionInRequestStream != 0) return;
char contentType = pRequestStream[0];
if (TlsTrace) { Log(" content type: "); logContentType(contentType); Log("\r\n"); }
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;
}
case TLS_CONTENT_TYPE_Application:
{
pState->toDo = DO_APPLICATION;
return;
}
default:
Log("TLS - ignoring untreated content type\r\n");
pState->toDo = DO_NOTHING;
return;
}
//ECDHE-RSA-AES128-GCM-SHA256
}
static void sendServerHello()
{
Log(" sending server hello\r\n");
}
int TlsPoll(char* pTlsState, char* pWebState, bool clientFinished)
{
struct state* pState = (struct state*)pTlsState;
switch (pState->toDo)
{
case DO_NOTHING:
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
default: return 1; //The client has made a request so do it
}
}
bool TlsReply(char* pTlsState, char* pWebState)
{
struct state* pState = (struct state*)pTlsState;
if ( pState->toDo == DO_SERVER_HELLO) sendServerHello();
return false; //Finished
}
static char encrypt(char c)
{
return c; //Implement encryption
}
void TlsAddChar(char c)
{
char e = encrypt(c);
TcpBufAddChar(e);
}