Riot-OS/GNRC support: reworked after reviewers' comments

This commit is contained in:
Daniele Lacamera
2019-07-23 08:47:31 +02:00
parent 1db036eb75
commit e77161ae9a
5 changed files with 31 additions and 12 deletions

View File

@ -1706,6 +1706,9 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap)
#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) #elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
ctx->CBIORecv = Mynewt_Receive; ctx->CBIORecv = Mynewt_Receive;
ctx->CBIOSend = Mynewt_Send; ctx->CBIOSend = Mynewt_Send;
#elif defined(WOLFSSL_GNRC)
ctx->CBIORecv = GNRC_Receive;
ctx->CBIOSend = GNRC_Send;
#endif #endif
#ifdef HAVE_NTRU #ifdef HAVE_NTRU
@ -5021,8 +5024,10 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
} }
ssl->IOCB_ReadCtx = ssl->mnCtx; /* default Mynewt IO ctx, same for read */ ssl->IOCB_ReadCtx = ssl->mnCtx; /* default Mynewt IO ctx, same for read */
ssl->IOCB_WriteCtx = ssl->mnCtx; /* and write */ ssl->IOCB_WriteCtx = ssl->mnCtx; /* and write */
#elif defined (WOLFSSL_GNRC)
ssl->IOCB_ReadCtx = ssl->gnrcCtx;
ssl->IOCB_WriteCtx = ssl->gnrcCtx;
#endif #endif
/* initialize states */ /* initialize states */
ssl->options.serverState = NULL_STATE; ssl->options.serverState = NULL_STATE;
ssl->options.clientState = NULL_STATE; ssl->options.clientState = NULL_STATE;

View File

@ -2164,6 +2164,8 @@ int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
break; break;
total_written += ret; total_written += ret;
} while(total_written < sz); } while(total_written < sz);
if (total_written == 0)
return WOLFSSL_CBIO_ERR_WANT_WRITE;
return total_written; return total_written;
} }
@ -2173,8 +2175,8 @@ int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
int ret = 0; int ret = 0;
(void)ssl; (void)ssl;
ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port ); ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port );
if (ret <= 0) if (ret == 0)
return 0; return WOLFSSL_CBIO_ERR_WANT_WRITE;
return ret; return ret;
} }
@ -2239,14 +2241,14 @@ int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
* return : bytes sent, or error * return : bytes sent, or error
*/ */
int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) int GNRC_Send(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
{ {
sock_tls_t *ctx = (sock_tls_t *)_ctx; sock_tls_t *ctx = (sock_tls_t *)_ctx;
int ret = 0; int ret = 0;
(void)ssl; (void)ssl;
ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr); ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr);
if (ret <= 0) if (ret == 0)
return 0; return WOLFSSL_CBIO_ERR_WANT_WRITE;
return ret; return ret;
} }
@ -2260,7 +2262,7 @@ int GNRC_Receive(WOLFSSL *ssl, char *buf, int sz, void *_ctx)
uint32_t timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000; uint32_t timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000;
sock_tls_t *ctx = (sock_tls_t *)_ctx; sock_tls_t *ctx = (sock_tls_t *)_ctx;
if (!ctx) if (!ctx)
return -1; return WOLFSSL_CBIO_ERR_GENERAL;
(void)ssl; (void)ssl;
if (wolfSSL_get_using_nonblock(ctx->ssl)) { if (wolfSSL_get_using_nonblock(ctx->ssl)) {
timeout = 0; timeout = 0;
@ -2279,16 +2281,22 @@ int GNRC_Receive(WOLFSSL *ssl, char *buf, int sz, void *_ctx)
/* GNRC DTLS Generate Cookie callback /* GNRC DTLS Generate Cookie callback
* return : number of bytes copied into buf, or error * return : number of bytes copied into buf, or error
*/ */
#define GNRC_MAX_TOKEN_SIZE (32)
int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
{ {
sock_tls_t *ctx = (sock_tls_t *)_ctx; sock_tls_t *ctx = (sock_tls_t *)_ctx;
byte token[32]; if (!ctx)
return WOLFSSL_CBIO_ERR_GENERAL;
byte token[GNRC_MAX_TOKEN_SIZE];
byte digest[WC_SHA_DIGEST_SIZE]; byte digest[WC_SHA_DIGEST_SIZE];
int ret = 0; int ret = 0;
size_t token_size = sizeof(sock_udp_ep_t);
(void)ssl; (void)ssl;
XMEMSET(token, 0, sizeof(token)); if (token_size > GNRC_MAX_TOKEN_SIZE)
XMEMCPY(token, &ctx->peer_addr, sizeof(sock_udp_ep_t)); token_size = GNRC_MAX_TOKEN_SIZE;
ret = wc_ShaHash(token, sizeof(sock_udp_ep_t), digest); XMEMSET(token, 0, GNRC_MAX_TOKEN_SIZE);
XMEMCPY(token, &ctx->peer_addr, token_size);
ret = wc_ShaHash(token, token_size, digest);
if (ret != 0) if (ret != 0)
return ret; return ret;
if (sz > WC_SHA_DIGEST_SIZE) if (sz > WC_SHA_DIGEST_SIZE)

View File

@ -3924,6 +3924,9 @@ struct WOLFSSL {
#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
void* mnCtx; /* mynewt mn_socket IO Context */ void* mnCtx; /* mynewt mn_socket IO Context */
#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */
#ifdef WOLFSSL_GNRC
struct gnrc_wolfssl_ctx *gnrcCtx; /* Riot-OS GNRC UDP/IP context */
#endif
#ifdef SESSION_INDEX #ifdef SESSION_INDEX
int sessionIndex; /* Session's location in the cache. */ int sessionIndex; /* Session's location in the cache. */
#endif #endif

View File

@ -529,6 +529,9 @@
#define TFM_NO_ASM #define TFM_NO_ASM
#define NO_FILESYSTEM #define NO_FILESYSTEM
#define USE_CERT_BUFFERS_2048 #define USE_CERT_BUFFERS_2048
#if defined(WOLFSSL_GNRC) && !defined(WOLFSSL_DTLS)
#define WOLFSSL_DTLS
#endif
#endif #endif
#ifdef WOLFSSL_CHIBIOS #ifdef WOLFSSL_CHIBIOS

View File

@ -518,7 +518,7 @@ WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
WOLFSSL_LOCAL int GNRC_Receive(WOLFSSL* ssl, char* buf, int sz, WOLFSSL_LOCAL int GNRC_Receive(WOLFSSL* ssl, char* buf, int sz,
void* ctx); void* ctx);
WOLFSSL_LOCAL int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx); WOLFSSL_LOCAL int GNRC_Send(WOLFSSL* ssl, char* buf, int sz, void* ctx);
#endif #endif