Skip to content

Commit

Permalink
Merge pull request #213 from delme-imgtec/tinydtls
Browse files Browse the repository at this point in the history
Add Tinydtls support
  • Loading branch information
Rory Latchem authored Jun 30, 2016
2 parents 75a8cbe + 8c4f8cf commit adcd20f
Show file tree
Hide file tree
Showing 25 changed files with 983 additions and 17 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ option (WITH_LIBCOAP "Enable libcoap CoAP support)" ON)
option (WITH_ERBIUM "Enable Erbium CoAP support)" OFF)
option (WITH_GNUTLS "Enable GnuTLS DTLS support" OFF)
option (WITH_CYASSL "Enable CyaSSL DTLS support" OFF)
option (WITH_TINYDTLS "Enable TinyDTLS DTLS support" OFF)

if (WITH_ERBIUM)
message (WARNING "Disabling libcoap support")
set (WITH_LIBCOAP OFF)
endif ()

if (WITH_GNUTLS AND WITH_CYASSL)
message (FATAL_ERROR "WITH_GNUTLS and WITH_CYASSL are mutually exclusive and cannot be specified together." )
if (WITH_GNUTLS AND (WITH_CYASSL OR WITH_TINYDTLS))
message (FATAL_ERROR "WITH_GNUTLS, WITH_CYASSL and WITH_TINYDTLS are mutually exclusive and cannot be specified together." )
endif ()


Expand Down
43 changes: 43 additions & 0 deletions core/src/bootstrap/lwm2m_bootstrap_psk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/************************************************************************************************************************
Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/


#ifndef LWM2M_BOOTSTRAP_PSK_H_
#define LWM2M_BOOTSTRAP_PSK_H_

#ifdef __cplusplus
extern "C" {
#endif


const char * pskIdentity = "oFIrQFrW8EWcZ5u7eGfrkw";

const uint8_t pskKey[] = {
0x7C, 0xCD, 0xE1, 0x4A, 0x5C, 0xF3, 0xB7, 0x1C, 0x0C, 0x08, 0xC8, 0xB7, 0xF9, 0xE5
};


#ifdef __cplusplus
}
#endif

#endif /* LWM2M_BOOTSTRAP_PSK_H_ */
2 changes: 2 additions & 0 deletions core/src/bootstrap/lwm2m_bootstrap_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "lwm2m_object_defs.h"
#include "bootstrap/lwm2m_bootstrap.h"
#include "bootstrap/lwm2m_bootstrap_cert.h"
#include "bootstrap/lwm2m_bootstrap_psk.h"


#define DEFAULT_IP_ADDRESS "0.0.0.0"
Expand Down Expand Up @@ -201,6 +202,7 @@ static int Bootstrap_Start(Options * options)
if (options->Secure)
{
coap_SetCertificate(bootsrapCert, sizeof(bootsrapCert), CertificateFormat_PEM);
coap_SetPSK(pskIdentity, pskKey, sizeof(pskKey));
}

Lwm2mContextType * context = Lwm2mCore_Init(coap);
Expand Down
2 changes: 2 additions & 0 deletions core/src/client/lwm2m_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "lwm2m_xml_interface.h"
#include "lwm2m_object_defs.h"
#include "lwm2m_client_cert.h"
#include "lwm2m_client_psk.h"


#define DEFAULT_COAP_PORT (6000)
Expand Down Expand Up @@ -202,6 +203,7 @@ static int Lwm2mClient_Start(Options * options)
}

coap_SetCertificate(clientCert, sizeof(clientCert), CertificateFormat_PEM);
coap_SetPSK(pskIdentity, pskKey, sizeof(pskKey));

// if required read the bootstrap information from a file
const BootstrapInfo * factoryBootstrapInfo;
Expand Down
43 changes: 43 additions & 0 deletions core/src/client/lwm2m_client_psk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/************************************************************************************************************************
Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/


#ifndef LWM2M_CLIENT_PSK_H_
#define LWM2M_CLIENT_PSK_H_

#ifdef __cplusplus
extern "C" {
#endif


const char * pskIdentity = "oFIrQFrW8EWcZ5u7eGfrkw";

const uint8_t pskKey[] = {
0x7C, 0xCD, 0xE1, 0x4A, 0x5C, 0xF3, 0xB7, 0x1C, 0x0C, 0x08, 0xC8, 0xB7, 0xF9, 0xE5
};


#ifdef __cplusplus
}
#endif

#endif /* LWM2M_CLIENT_PSK_H_ */
16 changes: 15 additions & 1 deletion core/src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ if (WITH_CYASSL)
list (APPEND awa_common_SOURCES dtls_abstraction_cyassl.c)
endif ()

if (NOT WITH_GNUTLS AND NOT WITH_CYASSL)
if (WITH_TINYDTLS)
list (APPEND awa_common_SOURCES dtls_abstraction_tinydtls.c)
endif ()


if (NOT WITH_GNUTLS AND NOT WITH_CYASSL AND NOT WITH_TINYDTLS)
list (APPEND awa_common_SOURCES dtls_abstraction_dummy.c)
endif ()

Expand All @@ -52,6 +57,11 @@ if (WITH_ERBIUM)
list (APPEND awa_common_INCLUDE_DIRS ${LIBCOAP_INCLUDE_DIR})
endif ()

if (WITH_TINYDTLS)
list (APPEND awa_common_INCLUDE_DIRS ${TINYDTLS_INCLUDE_DIR})
endif ()


set (awa_common_LIBS
libxml_static
libb64_static
Expand Down Expand Up @@ -79,6 +89,10 @@ if (WITH_GNUTLS)
list (APPEND awa_common_LIBS gnutls)
endif ()

if (WITH_TINYDTLS)
list (APPEND awa_common_LIBS tinydtls_static)
endif ()

if (ENABLE_GCOV)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/coap_abstraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extern const char * coap_LibraryName;
CoapInfo * coap_Init(const char * ipAddress, int port, bool secure, int logLevel);

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

int coap_Destroy(void);
void coap_Process(void);
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/coap_abstraction_contiki.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ void coap_SetCertificate(const uint8_t * cert, int certLength, CertificateFormat
(void)format;
}

void coap_SetPSK(const char * identity, uint8_t * key, int keyLength)
void coap_SetPSK(const char * identity, const uint8_t * key, int keyLength)
{
(void)identity;
(void)key;
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/coap_abstraction_erbium.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void coap_SetCertificate(const uint8_t * cert, int certLength, CertificateFormat
NetworkSocket_SetCertificate(networkSocket, cert, certLength, format);
}

void coap_SetPSK(const char * identity, uint8_t * key, int keyLength)
void coap_SetPSK(const char * identity, const uint8_t * key, int keyLength)
{
NetworkSocket_SetPSK(networkSocket, identity, key, keyLength);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/coap_abstraction_libcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ void coap_SetCertificate(const uint8_t * cert, int certLength, CertificateFormat
(void)format;
}

void coap_SetPSK(const char * identity, uint8_t * key, int keyLength)
void coap_SetPSK(const char * identity, const uint8_t * key, int keyLength)
{
(void)identity;
(void)key;
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/dtls_abstraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void DTLS_SetCertificate(const uint8_t * cert, int certLength, CertificateFormat

void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback);

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

bool DTLS_Decrypt(NetworkAddress * sourceAddress, uint8_t * encrypted, int encryptedLength, uint8_t * decryptBuffer, int decryptBufferLength, int * decryptedLength, void *context);

Expand Down
6 changes: 3 additions & 3 deletions core/src/common/dtls_abstraction_cyassl.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ int certificateLength = 0;
CertificateFormat certificateFormat;

const char * pskIdentity = NULL;
uint8_t * pskKey;
int pskKeyLength;
const uint8_t * pskKey = NULL;
int pskKeyLength = 0;

DTLS_NetworkSendCallback NetworkSend = NULL;

Expand Down Expand Up @@ -129,7 +129,7 @@ void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback)
NetworkSend = sendCallback;
}

void DTLS_SetPSK(const char * identity, uint8_t * key, int keyLength)
void DTLS_SetPSK(const char * identity, const uint8_t * key, int keyLength)
{
pskIdentity = identity;
pskKey = key;
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/dtls_abstraction_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback)
{
}

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

Expand Down
4 changes: 2 additions & 2 deletions core/src/common/dtls_abstraction_gnutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ void DTLS_SetNetworkSendCallback(DTLS_NetworkSendCallback sendCallback)
NetworkSend = sendCallback;
}

void DTLS_SetPSK(const char * identity, uint8_t * key, int keyLength)
void DTLS_SetPSK(const char * identity, const uint8_t * key, int keyLength)
{
pskIdentity = identity;
pskKey.data = key;
pskKey.data = (unsigned char *)key;
pskKey.size = keyLength;
}

Expand Down
Loading

0 comments on commit adcd20f

Please sign in to comment.