Skip to content

Commit

Permalink
Merge pull request #238 from delme-imgtec/dtls
Browse files Browse the repository at this point in the history
Add support to reset DTLS sessions
  • Loading branch information
seank-img authored Jul 11, 2016
2 parents e6f239e + 762ea82 commit 518114d
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/src/client/lwm2m_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ static void SendBootStrapRequest(Lwm2mContextType * context, int shortServerID)
sprintf(uri, "%s%s%s", serverPath, uriPath, uriQuery);
Lwm2m_Info("Bootstrap with %s\n", uri);

coap_Reset(uri);

coap_PostRequest(context, uri, ContentType_None, NULL, 0, HandleBootstrapResponse);
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/client/lwm2m_registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ static void SendRegisterRequest(Lwm2mContextType * context, Lwm2mServerType * se
sprintf(uri, "%s%s%s", serverUri, uriPath, uriQuery);
Lwm2m_Debug("Register: POST %s\n", uri);

coap_Reset(uri);

coap_PostRequest(server, uri, ContentType_ApplicationLinkFormat, payload, strlen(payload), HandleRegisterResponse);
server->RegistrationState = Lwm2mRegistrationState_Registering;
}
Expand Down
1 change: 1 addition & 0 deletions core/src/common/coap_abstraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ extern const char * coap_LibraryName;

CoapInfo * coap_Init(const char * ipAddress, int port, bool secure, int logLevel);

void coap_Reset(const char * uri);
void coap_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format);
void coap_SetPSK(const char * identity, const uint8_t * key, int keyLength);

Expand Down
4 changes: 4 additions & 0 deletions core/src/common/coap_abstraction_contiki.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ static RequestHandler requestHandler = NULL;
int CurrentTransactionIndex = 0;
TransactionType CurrentTransaction[MAX_COAP_TRANSACTIONS] = {{0}, {0}};

void coap_Reset(const char * uri)
{
}

int coap_WaitMessage(int timeout, int fd)
{
// No wait in Contiki
Expand Down
10 changes: 10 additions & 0 deletions core/src/common/coap_abstraction_erbium.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ CoapInfo * coap_Init(const char * ipAddress, int port, bool secure, int logLevel
return &coapInfo;
}


void coap_Reset(const char * uri)
{
NetworkAddress * remoteAddress = NetworkAddress_New(uri, strlen(uri));
if (remoteAddress)
{
DTLS_Reset(remoteAddress);
}
}

void coap_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
NetworkSocket_SetCertificate(networkSocket, cert, certLength, format);
Expand Down
5 changes: 5 additions & 0 deletions core/src/common/coap_abstraction_libcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ static void * context = NULL;
static CoapInfo coapInfo;
static RequestHandler requestHandler = NULL;


void coap_Reset(const char * uri)
{
}

void coap_SetContext(void * ctxt)
{
context = ctxt;
Expand Down
11 changes: 6 additions & 5 deletions core/src/common/dtls_abstraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ void DTLS_Init(void);

void DTLS_Shutdown(void);

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format);
bool DTLS_Decrypt(NetworkAddress * sourceAddress, uint8_t * encrypted, int encryptedLength, uint8_t * decryptBuffer, int decryptBufferLength, int * decryptedLength, void *context);

void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback);
bool DTLS_Encrypt(NetworkAddress * destAddress, uint8_t * plainText, int plainTextLength, uint8_t * encryptedBuffer, int encryptedBufferLength, int * encryptedLength, void *context);

void DTLS_SetPSK(const char * identity, const uint8_t * key, int keyLength);
void DTLS_Reset(NetworkAddress * address);

bool DTLS_Decrypt(NetworkAddress * sourceAddress, uint8_t * encrypted, int encryptedLength, uint8_t * decryptBuffer, int decryptBufferLength, int * decryptedLength, void *context);
void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format);

bool DTLS_Encrypt(NetworkAddress * destAddress, uint8_t * plainText, int plainTextLength, uint8_t * encryptedBuffer, int encryptedBufferLength, int * encryptedLength, void *context);
void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback);

void DTLS_SetPSK(const char * identity, const uint8_t * key, int keyLength);

#ifdef __cplusplus
}
Expand Down
37 changes: 37 additions & 0 deletions core/src/common/dtls_abstraction_cyassl.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static DTLS_NetworkSendCallback NetworkSend = NULL;

static DTLS_Session * AllocateSession(NetworkAddress * address, bool client, void * context);
static DTLS_Session * GetSession(NetworkAddress * address);
static void FreeSession(DTLS_Session * session);
static void SetupNewSession(int index, NetworkAddress * networkAddress, bool client);
static int DecryptCallBack(CYASSL *sslSessioon, char *recieveBuffer, int receiveBufferLegth, void *vp);
static int EncryptCallBack(CYASSL *sslSessioon, char *sendBuffer, int sendBufferLength, void *vp);
Expand Down Expand Up @@ -155,9 +156,27 @@ void DTLS_Init(void)

void DTLS_Shutdown(void)
{
int index;
for (index = 0;index < MAX_DTLS_SESSIONS; index++)
{
if (sessions[index].Context)
{
FreeSession(&sessions[index]);
}
}
CyaSSL_Cleanup();
}


void DTLS_Reset(NetworkAddress * address)
{
DTLS_Session * session = GetSession(address);
if (session)
{
FreeSession(session);
}
}

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
certificate = (uint8_t *)cert;
Expand Down Expand Up @@ -289,6 +308,24 @@ static DTLS_Session * GetSession(NetworkAddress * address)
return result;
}

static void FreeSession(DTLS_Session * session)
{
if (session)
{
if (session->Session)
{
CyaSSL_shutdown(session->Session);
CyaSSL_free(session->Session);
}
if (session->Context)
{
CyaSSL_CTX_free(session->Context);
}
memset(session,0, sizeof(DTLS_Session));
}
}


static void SetupNewSession(int index, NetworkAddress * networkAddress, bool client)
{
DTLS_Session * session = &sessions[index];
Expand Down
4 changes: 4 additions & 0 deletions core/src/common/dtls_abstraction_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void DTLS_Shutdown(void)
{
}

void DTLS_Reset(NetworkAddress * address)
{
}

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
}
Expand Down
9 changes: 9 additions & 0 deletions core/src/common/dtls_abstraction_gnutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ void DTLS_Shutdown(void)
gnutls_global_deinit();
}

void DTLS_Reset(NetworkAddress * address)
{
DTLS_Session * session = GetSession(address);
if (session)
{
FreeSession(session);
}
}

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
certificate = (uint8_t *)cert;
Expand Down
9 changes: 9 additions & 0 deletions core/src/common/dtls_abstraction_tinydtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ void DTLS_Shutdown(void)
}
}

void DTLS_Reset(NetworkAddress * address)
{
DTLS_Session * session = GetSession(address);
if (session)
{
FreeSession(session);
}
}

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
certificate = (uint8_t *)cert;
Expand Down

0 comments on commit 518114d

Please sign in to comment.