From b74869a61a63f2d33928ec232c249ebd90c7b4c0 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 10 Jul 2018 18:32:03 +0200 Subject: [PATCH] Moved contiki + uIP support in the library --- src/internal.c | 9 ++++ src/wolfio.c | 79 ++++++++++++++++++++++++++++++++++++ wolfssl/wolfcrypt/settings.h | 8 ++++ wolfssl/wolfio.h | 34 ++++++++++++++++ 4 files changed, 130 insertions(+) diff --git a/src/internal.c b/src/internal.c index a5d8be466..ef51a3937 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1410,6 +1410,15 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap) #error Micrium port does not support DTLS session export yet #endif #endif + #elif defined UIP + ctx->CBIORecv = uIPReceive; + ctx->CBIOSend = uIPSend; + #ifdef WOLFSSL_DTLS + if (method->version.major == DTLS_MAJOR) { + ctx->CBIOSendTo = uIPSendTo; + ctx->CBIORecvFrom = uIPRecvFrom; + } + #endif #else ctx->CBIORecv = EmbedReceive; ctx->CBIOSend = EmbedSend; diff --git a/src/wolfio.c b/src/wolfio.c index da6683e5e..3c90d5184 100644 --- a/src/wolfio.c +++ b/src/wolfio.c @@ -2068,4 +2068,83 @@ void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_so #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ +#ifdef UIP +#include + +#define SOCKLEN_UIP sizeof(struct sockaddr_uip) + +/* uIP TCP/IP port, using the native tcp/udp socket api. + * TCP and UDP are currently supported with the callbacks below. + * + */ +/* The uIP tcp send callback + * return : bytes sent, or error + */ +int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx) +{ + uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; + int ret; + (void)ssl; + ret = tcp_socket_send(&ctx->conn.tcp, (unsigned char *)buf, sz); + if (ret <= 0) + return WOLFSSL_CBIO_ERR_WANT_WRITE; + return ret; +} + +int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) +{ + uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; + int ret = 0; + (void)ssl; + ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port ); + if (ret <= 0) + return WOLFSSL_CBIO_ERR_WANT_WRITE; + return ret; +} + +/* The uIP uTCP/IP receive callback + * return : nb bytes read, or error + */ +int uIPReceive(WOLFSSL *ssl, char *buf, int sz, void *_ctx) +{ + uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; + (void)ssl; + if (ctx->ssl_rb_len > 0) { + if (sz > ctx->ssl_rb_len - ctx->ssl_rb_off) + sz = ctx->ssl_rb_len - ctx->ssl_rb_off; + memcpy(buf, ctx->ssl_recv_buffer + ctx->ssl_rb_off, sz); + ctx->ssl_rb_off += sz; + if (ctx->ssl_rb_off >= ctx->ssl_rb_len) { + ctx->ssl_rb_len = 0; + ctx->ssl_rb_off = 0; + } + return sz; + } else { + return WOLFSSL_CBIO_ERR_WANT_READ; + } +} + +/* uIP DTLS Generate Cookie callback + * return : number of bytes copied into buf, or error + */ +int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) +{ + uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)ctx; + byte token[32]; + byte digest[WC_SHA_DIGEST_SIZE]; + int ret = 0; + XMEMSET(token, 0, sizeof(token)); + XMEMCPY(token, &ctx->peer_addr, sizeof(uip_ipaddr_t)); + XMEMCPY(token + sizeof(uip_ipaddr_t), &ctx->peer_port, sizeof(word16)); + ret = wc_ShaHash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest); + if (ret != 0) + return ret; + if (sz > WC_SHA_DIGEST_SIZE) + sz = WC_SHA_DIGEST_SIZE; + XMEMCPY(buf, digest, sz); + return sz; +} + +#endif /* UIP */ + #endif /* WOLFCRYPT_ONLY */ diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 401195cc4..5f4cc38b6 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -225,10 +225,18 @@ #endif #if defined(WOLFSSL_CONTIKI) + #include + #define UIP #define NO_WRITEV #define SINGLE_THREADED #define WOLFSSL_USER_IO #define NO_FILESYSTEM + #define CUSTOM_RAND_TYPE uint16_t + #define CUSTOM_RAND_GENERATE random_rand + static inline unsigned int LowResTimer(void) + { + return clock_seconds(); + } static inline void* XREALLOC(void *p, size_t n, void* heap, int type) { diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index 75a03f457..b6136f84e 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -423,6 +423,40 @@ WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags); struct mn_sockaddr_in* mnSockAddrIn); #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ +#ifdef UIP +#define SSL_DATABUF_LEN 1460 + +struct uip_wolfssl_ctx { + union socket_connector { + struct tcp_socket tcp; + struct udp_socket udp; + } conn; + WOLFSSL_CTX *ctx; + WOLFSSL *ssl; + uint8_t input_databuf[SSL_DATABUF_LEN]; + uint8_t output_databuf[SSL_DATABUF_LEN]; + uint8_t ssl_recv_buffer[SSL_DATABUF_LEN]; + int ssl_rb_len; + int ssl_rb_off; + struct process *process; + tcp_socket_data_callback_t input_callback; + tcp_socket_event_callback_t event_callback; + int closing; + uip_ipaddr_t peer_addr; + uint16_t peer_port; +}; + +typedef struct uip_wolfssl_ctx uip_wolfssl_ctx; + + WOLFSSL_LOCAL int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* ctx); + WOLFSSL_LOCAL int uIPReceive(WOLFSSL* ssl, char* buf, int sz, + void* ctx); + WOLFSSL_LOCAL int uIPReceiveFrom(WOLFSSL* ssl, char* buf, int sz, + void* ctx); + WOLFSSL_LOCAL int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx); + +#endif + #ifdef WOLFSSL_DTLS typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz, void* ctx);