Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private mbedtls in communications library #940

Merged
merged 2 commits into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions communication/src/dtls_session_persist.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,39 @@
*/
#pragma once

#define SessionPersistBaseSize 196

#include "mbedtls/ssl.h"
#include "stddef.h"

typedef struct __attribute__((packed)) SessionPersistOpaque
// The size of the persisted data
#define SessionPersistBaseSize 196
// variable size due to int/size_t members
#define SessionPersistVariableSize (sizeof(int)+sizeof(int)+sizeof(size_t))

/**
* An entirely opaque version of SessionPersistData for use with C.
*/
typedef struct __attribute__((packed)) SessionPersistDataOpaque
{
uint16_t size;
// uint8_t data[SessionPersistBaseSize-2+sizeof(mbedtls_ssl_session::ciphersuite)+sizeof(mbedtls_ssl_session::id_len)+sizeof(mbedtls_ssl_session::compression)];
uint8_t data[SessionPersistBaseSize-2+sizeof(int)+sizeof(int)+sizeof(size_t)];
} SessionPersistOpaque;
// uint8_t data[SessionPersistBaseSize-sizeof(uint16_t)+sizeof(mbedtls_ssl_session::ciphersuite)+sizeof(mbedtls_ssl_session::id_len)+sizeof(mbedtls_ssl_session::compression)];
uint8_t data[SessionPersistBaseSize-sizeof(uint16_t)+SessionPersistVariableSize];
} SessionPersistDataOpaque;



#ifdef __cplusplus
#include "coap.h"
#include "spark_protocol_functions.h" // for SparkCallbacks

#ifdef MBEDTLS_SSL_H
#include "dtls_message_channel.h"
#include "spark_protocol_functions.h" // for SparkCallbacks
#endif


namespace particle { namespace protocol {

/**
* A simple POD for the persisted session data.
*/
struct __attribute__((packed)) SessionPersistData
{
uint16_t size;
Expand All @@ -53,6 +67,7 @@ struct __attribute__((packed)) SessionPersistData
// constant. Add more members at the end of the struct.
uint8_t connection[32];
uint32_t keys_checksum;
#ifdef MBEDTLS_SSL_H
uint8_t randbytes[sizeof(mbedtls_ssl_handshake_params::randbytes)];
decltype(mbedtls_ssl_session::ciphersuite) ciphersuite;
decltype(mbedtls_ssl_session::compression) compression;
Expand All @@ -63,10 +78,34 @@ struct __attribute__((packed)) SessionPersistData
unsigned char out_ctr[8];
// application data
message_id_t next_coap_id;
#else
// when the mbedtls headers aren't available, just pad with the requisite size
uint8_t opaque_ssl[64+sizeof(int)+sizeof(int)+sizeof(size_t)+32+48+2+8+2];
#endif

};

class __attribute__((packed)) SessionPersistOpaque : public SessionPersistData
{
public:

SessionPersistOpaque()
{
size = 0; persistent = 0;
}

bool is_valid() { return size==sizeof(*this); }

uint8_t* connection_data() { return connection; }

void invalidate() { size = 0; }

};

class __attribute__((packed)) SessionPersist : SessionPersistData

#ifdef MBEDTLS_SSL_H

class __attribute__((packed)) SessionPersist : SessionPersistOpaque
{
public:

Expand Down Expand Up @@ -121,15 +160,6 @@ class __attribute__((packed)) SessionPersist : SessionPersistData

public:

SessionPersist()
{
size = 0; persistent = 0;
}

bool is_valid() { return size==sizeof(*this); }

void invalidate() { size = 0; }

void clear(save_fn_t saver)
{
persistent = 1; // ensure it is saved
Expand Down Expand Up @@ -192,19 +222,22 @@ class __attribute__((packed)) SessionPersist : SessionPersistData
*/
RestoreStatus restore(mbedtls_ssl_context* context, bool renegotiate, uint32_t keys_checksum, message_id_t* message, restore_fn_t restorer);

uint8_t* connection_data() { return connection; }

};

static_assert(sizeof(SessionPersist)==SessionPersistBaseSize+sizeof(mbedtls_ssl_session::ciphersuite)+sizeof(mbedtls_ssl_session::id_len)+sizeof(mbedtls_ssl_session::compression), "SessionPersist size");
static_assert(sizeof(SessionPersist)==sizeof(SessionPersistOpaque), "SessionPersistOpaque size == sizeof(SessionPersistQueue)");
static_assert(sizeof(SessionPersist)==sizeof(SessionPersistDataOpaque), "SessionPersistDataOpaque size == sizeof(SessionPersist)");

#endif

// the connection buffer is used by external code to store connection data in the session
// it must be binary compatible with previous releases
static_assert(offsetof(SessionPersistData, connection)==4, "internal layout of public member has changed.");
static_assert(sizeof(SessionPersistData)==sizeof(SessionPersist), "session persist data and the subclass should be the same size.");
static_assert((sizeof(SessionPersistData)==sizeof(SessionPersistDataOpaque)), "session persist data and the subclass should be the same size.");

}}

static_assert(sizeof(SessionPersistDataOpaque)==SessionPersistBaseSize+SessionPersistVariableSize, "SessionPersistDataOpque size should be SessionPersistBaseSize+SessionPersistVariableSize");

#endif


Expand Down
8 changes: 4 additions & 4 deletions hal/src/gcc/core_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ bool HAL_Feature_Get(HAL_Feature feature)
#if HAL_PLATFORM_CLOUD_UDP

#include "dtls_session_persist.h"
SessionPersistOpaque session;
SessionPersistDataOpaque session;

int HAL_System_Backup_Save(size_t offset, const void* buffer, size_t length, void* reserved)
{
if (offset==0 && length==sizeof(SessionPersistOpaque))
if (offset==0 && length==sizeof(SessionPersistDataOpaque))
{
memcpy(&session, buffer, length);
return 0;
Expand All @@ -322,9 +322,9 @@ int HAL_System_Backup_Save(size_t offset, const void* buffer, size_t length, voi

int HAL_System_Backup_Restore(size_t offset, void* buffer, size_t max_length, size_t* length, void* reserved)
{
if (offset==0 && max_length>=sizeof(SessionPersistOpaque) && session.size==sizeof(SessionPersistOpaque))
if (offset==0 && max_length>=sizeof(SessionPersistDataOpaque) && session.size==sizeof(SessionPersistDataOpaque))
{
*length = sizeof(SessionPersistOpaque);
*length = sizeof(SessionPersistDataOpaque);
memcpy(buffer, &session, sizeof(session));
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions hal/src/stm32f2xx/core_hal_stm32f2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,11 @@ bool HAL_Feature_Get(HAL_Feature feature)
#include "deepsleep_hal_impl.h"
#include <string.h>

retained_system SessionPersistOpaque session;
retained_system SessionPersistDataOpaque session;

int HAL_System_Backup_Save(size_t offset, const void* buffer, size_t length, void* reserved)
{
if (offset==0 && length==sizeof(SessionPersistOpaque))
if (offset==0 && length==sizeof(SessionPersistDataOpaque))
{
memcpy(&session, buffer, length);
return 0;
Expand All @@ -1080,9 +1080,9 @@ int HAL_System_Backup_Save(size_t offset, const void* buffer, size_t length, voi

int HAL_System_Backup_Restore(size_t offset, void* buffer, size_t max_length, size_t* length, void* reserved)
{
if (offset==0 && max_length>=sizeof(SessionPersistOpaque) && session.size==sizeof(SessionPersistOpaque))
if (offset==0 && max_length>=sizeof(SessionPersistDataOpaque) && session.size==sizeof(SessionPersistDataOpaque))
{
*length = sizeof(SessionPersistOpaque);
*length = sizeof(SessionPersistDataOpaque);
memcpy(buffer, &session, sizeof(session));
return 0;
}
Expand Down
1 change: 0 additions & 1 deletion system/src/system_cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "system_cloud_internal.h"
#include "string_convert.h"
#include "spark_protocol_functions.h"
#include "spark_protocol.h"
#include "events.h"
#include "deviceid_hal.h"
#include "system_mode.h"
Expand Down
11 changes: 6 additions & 5 deletions system/src/system_cloud_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "system_user.h"
#include "spark_wiring_string.h"
#include "spark_protocol_functions.h"
#include "spark_protocol.h"
#include "append_list.h"
#include "core_hal.h"
#include "deviceid_hal.h"
Expand Down Expand Up @@ -405,15 +404,17 @@ void SystemEvents(const char* name, const char* data)
}
}

using particle::protocol::SessionPersistOpaque;

#if HAL_PLATFORM_CLOUD_UDP
int Spark_Save(const void* buffer, size_t length, uint8_t type, void* reserved)
{
if (type==SparkCallbacks::PERSIST_SESSION)
{
static_assert(sizeof(SessionPersistData::connection)>=sizeof(cloud_endpoint),"connection space in session is not large enough");
static_assert(sizeof(SessionPersistOpaque::connection)>=sizeof(cloud_endpoint),"connection space in session is not large enough");

// save the current connection to the persisted session
SessionPersist* persist = (SessionPersist*)buffer;
SessionPersistOpaque* persist = (SessionPersistOpaque*)buffer;
if (persist->is_valid())
{
memcpy(persist->connection_data(), &cloud_endpoint, sizeof(cloud_endpoint));
Expand Down Expand Up @@ -584,7 +585,7 @@ int Spark_Handshake(bool presence_announce)
spark_protocol_send_time_request(sp);
Spark_Process_Events();
}
if (err==SESSION_RESUMED)
if (err==particle::protocol::SESSION_RESUMED)
{
DEBUG("cloud connected from existing session.");
err = 0;
Expand Down Expand Up @@ -712,7 +713,7 @@ uint32_t compute_session_checksum(ServerAddress& addr)
*/
int determine_session_connection_address(IPAddress& ip_addr, uint16_t& port, ServerAddress& server_addr)
{
SessionPersist persist;
SessionPersistOpaque persist;
if (Spark_Restore(&persist, sizeof(persist), SparkCallbacks::PERSIST_SESSION, nullptr)==sizeof(persist) && persist.is_valid())
{
SessionConnection* connection = (SessionConnection*)persist.connection_data();
Expand Down
2 changes: 1 addition & 1 deletion system/src/system_network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ class ManagedIPNetworkInterface : public ManagedNetworkInterface

public:

void get_ipconfig(IPConfig* config)
void get_ipconfig(IPConfig* config) override
{
update_config(true);
memcpy(config, this->config(), config->size);
Expand Down