Skip to content

Commit

Permalink
Merge pull request #263 from delme-imgtec/dtls
Browse files Browse the repository at this point in the history
Wire up support for Certificate file for client daemon
  • Loading branch information
Rory Latchem authored Jul 27, 2016
2 parents fd30ce0 + 9f27382 commit 81f6ed3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
49 changes: 48 additions & 1 deletion core/src/client/lwm2m_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef struct
char * BootStrap;
char * PskIdentity;
char * PskKey;
char * CertificateFile;
const char * FactoryBootstrapFile;
const char * ObjDefsFiles[MAX_OBJDEFS_FILES];
AwaContentType DefaultContentType;
Expand All @@ -78,9 +79,11 @@ typedef struct
} Options;

static FILE * logFile = NULL;
static uint8_t * loadedClientCert = NULL;
static const char * version = VERSION; // from Makefile
static volatile int quit = 0;

static void LoadCertificateFile(char * certificateFilename);
static void PrintOptions(const Options * options);

static void Lwm2m_CtrlCSignalHandler(int dummy)
Expand Down Expand Up @@ -227,7 +230,12 @@ static int Lwm2mClient_Start(Options * options)
}

// always set key
coap_SetCertificate(clientCert, sizeof(clientCert), AwaCertificateFormat_PEM);
if (options->CertificateFile)
{
LoadCertificateFile(options->CertificateFile);
}
else
coap_SetCertificate(clientCert, sizeof(clientCert), AwaCertificateFormat_PEM);

if (options->PskIdentity && options->PskKey)
{
Expand Down Expand Up @@ -353,6 +361,10 @@ static int Lwm2mClient_Start(Options * options)
coap_Destroy();

error_close_log:
if (loadedClientCert)
{
free(loadedClientCert);
}
if (key)
{
free(key);
Expand All @@ -366,6 +378,37 @@ static int Lwm2mClient_Start(Options * options)
return result;
}

static void LoadCertificateFile(char * filename)
{
FILE * file = fopen(filename, "r");
if (file)
{
fseek(file, 0, SEEK_END);
long int fileSize = ftell (file);
if (fileSize > 0)
{
loadedClientCert = (uint8_t *)malloc(fileSize);
if (loadedClientCert)
{
rewind(file);
size_t totalRead = 0;
uint8_t * position = loadedClientCert;
size_t read;
do
{
read = fread(position, 1, fileSize-totalRead, file);
totalRead += read;
position += read;
} while ( (read != 0) && (totalRead < fileSize));
if (totalRead == fileSize)
coap_SetCertificate(loadedClientCert, fileSize, AwaCertificateFormat_PEM);
}
}
fclose(file);
}
}


static void PrintOptions(const Options * options)
{
printf("Options specified or defaulted:\n");
Expand Down Expand Up @@ -420,6 +463,9 @@ static int ParseOptions(int argc, char ** argv, struct gengetopt_args_info * ai,
if (ai->pskKey_given)
options->PskKey = ai->pskKey_arg;

if (ai->certificate_given)
options->CertificateFile = ai->certificate_arg;

if (ai->factoryBootstrap_given)
options->FactoryBootstrapFile = ai->factoryBootstrap_arg;
int i;
Expand Down Expand Up @@ -464,6 +510,7 @@ int main(int argc, char ** argv)
.BootStrap = NULL,
.PskIdentity = NULL,
.PskKey = NULL,
.CertificateFile = NULL,
.FactoryBootstrapFile = NULL,
.DefaultContentType = AwaContentType_ApplicationPlainText,
.ObjDefsFiles = {0},
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/dtls_abstraction_cyassl.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void DTLS_Reset(NetworkAddress * address)

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
if (certificateLength > 0)
if (certLength > 0)
{
certificate = (uint8_t *)cert;
certificateLength = certLength;
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/dtls_abstraction_gnutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void DTLS_Reset(NetworkAddress * address)

void DTLS_SetCertificate(const uint8_t * cert, int certLength, AwaCertificateFormat format)
{
if (certificateLength > 0)
if (certLength > 0)
{
certificate = (uint8_t *)cert;
certificateLength = certLength;
Expand Down
2 changes: 1 addition & 1 deletion core/src/common/dtls_abstraction_tinydtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void DTLS_Reset(NetworkAddress * address)

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

0 comments on commit 81f6ed3

Please sign in to comment.