first commit for opensource

first commit for opensource
This commit is contained in:
supowang
2019-09-16 13:19:50 +08:00
parent 08ab013b8e
commit edb2879617
6303 changed files with 5472815 additions and 23 deletions

View File

@@ -0,0 +1,36 @@
#include "string.h"
#include "stdlib.h"
#include "mbedtls/entropy.h"
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
static int os_get_random(unsigned char *buf, size_t len)
{
int i, j;
unsigned long tmp;
for (i = 0; i < ((len + 3) & ~3) / 4; i++) {
tmp = rand();
for (j = 0; j < 4; j++) {
if ((i * 4 + j) < len) {
buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8));
} else {
break;
}
}
}
return 0;
}
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
{
os_get_random(output, len);
*olen = len;
return 0;
}
#endif

View File

@@ -0,0 +1,257 @@
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if !defined(MBEDTLS_NET_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#endif
#include "tos.h"
#include "mbedtls/net_sockets.h"
#include "sal_module_wrapper.h"
void mbedtls_net_init(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
ctx->fd = -1;
}
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
int fd;
char ip[16] = { 0 };
sal_proto_t sal_proto;
if (!ctx) {
return -1;
}
if (tos_sal_module_parse_domain(host, ip, sizeof(ip)) != 0) {
return -1;
}
sal_proto = (proto == MBEDTLS_NET_PROTO_UDP ? TOS_SAL_PROTO_UDP : TOS_SAL_PROTO_TCP);
fd = tos_sal_module_connect(ip, port, sal_proto);
if (fd < 0) {
return -1;
}
ctx->fd = fd;
return 0;
}
void mbedtls_net_usleep(unsigned long usec)
{
uint32_t ms;
k_tick_t tick;
ms = usec / 1000;
if (ms == 0) {
ms = 1;
}
tick = tos_millisec2tick(ms);
tos_sleep_ms(tick);
}
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_recv(fd, buf, len);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_recv_timeout(fd, buf, len, timeout);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_SEND_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_send(fd, buf, len);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return ret;
}
void mbedtls_net_free(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
tos_sal_module_close(ctx->fd);
}
//////////////////////////////////////////
// dtls
void mbedtls_dtls_net_init(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
ctx->fd = -1;
}
int mbedtls_dtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
int fd;
char ip[16] = { 0 };
sal_proto_t sal_proto;
if (!ctx) {
return -1;
}
if (tos_sal_module_parse_domain(host, ip, sizeof(ip)) != 0) {
return -1;
}
sal_proto = (proto == MBEDTLS_NET_PROTO_UDP ? TOS_SAL_PROTO_UDP : TOS_SAL_PROTO_TCP);
fd = tos_sal_module_connect(ip, port, sal_proto);
if (fd < 0) {
return -1;
}
ctx->fd = fd;
return 0;
}
void mbedtls_dtls_net_usleep(unsigned long usec)
{
uint32_t ms;
k_tick_t tick;
ms = usec / 1000;
if (ms == 0) {
ms = 1;
}
tick = tos_millisec2tick(ms);
tos_sleep_ms(tick);
}
int mbedtls_dtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_recvfrom(fd, buf, len);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
int mbedtls_dtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_recvfrom_timeout(fd, buf, len, timeout);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
int mbedtls_dtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
int fd, ret;
if (!ctx) {
return MBEDTLS_ERR_NET_SEND_FAILED;
}
fd = ((mbedtls_net_context *)ctx)->fd;
ret = tos_sal_module_sendto(fd, NULL, NULL, buf, len);
if (ret == 0) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
} else if (ret < 0) {
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return ret;
}
void mbedtls_dtls_net_free(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
tos_sal_module_close(ctx->fd);
}
#endif /* MBEDTLS_NET_C */

View File

@@ -0,0 +1,282 @@
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if !defined(MBEDTLS_NET_C)
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#endif
#include "tos.h"
#include "mbedtls/net_sockets.h"
#include "lwip/api.h"
#include "lwip/sockets.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
void mbedtls_net_init(mbedtls_net_context *ctx)
{
if (!ctx) {
return;
}
ctx->fd = -1;
}
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
int ret;
struct addrinfo hints, *addr_list, *cur;
#if 0
if ((ret = net_prepare() ) != 0) {
return ret;
}
#endif
/* Do name resolution with both IPv6 and IPv4 */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
return MBEDTLS_ERR_NET_UNKNOWN_HOST;
}
/* Try the sockaddrs until a connection succeeds */
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
ctx->fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
if (ctx->fd < 0) {
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue;
}
if (connect(ctx->fd, cur->ai_addr, cur->ai_addrlen) == 0) {
ret = 0;
break;
}
close(ctx->fd);
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
}
freeaddrinfo(addr_list);
return ret;
}
/*
* Check if the requested operation would be blocking on a non-blocking socket
* and thus 'failed' with a negative return value.
*
* Note: on a blocking socket this function always returns 0!
*/
static int net_would_block(const mbedtls_net_context *ctx)
{
int err = errno;
/*
* Never return 'WOULD BLOCK' on a non-blocking socket
*/
if ((fcntl(ctx->fd, F_GETFL, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
errno = err;
return 0;
}
switch (errno = err) {
#if defined EAGAIN
case EAGAIN:
#endif
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
return 1;
}
return 0;
}
/*
* Set the socket blocking or non-blocking
*/
int mbedtls_net_set_block( mbedtls_net_context *ctx )
{
return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL, F_GETFL) & ~O_NONBLOCK);
}
int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
{
return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL, F_GETFL) | O_NONBLOCK);
}
void mbedtls_net_usleep(unsigned long usec)
{
uint32_t ms;
k_tick_t tick;
ms = usec / 1000;
if (ms == 0) {
ms = 1;
}
tick = tos_millisec2tick(ms);
tos_sleep_ms(tick);
}
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int ret;
int fd = ((mbedtls_net_context *)ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
ret = read(fd, buf, len);
if (ret < 0) {
if (net_would_block(ctx) != 0) {
return MBEDTLS_ERR_SSL_WANT_READ;
}
if (errno == EPIPE || errno == ECONNRESET) {
return MBEDTLS_ERR_NET_CONN_RESET;
}
if (errno == EINTR) {
return MBEDTLS_ERR_SSL_WANT_READ;
}
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return ret;
}
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
int ret;
struct timeval tv;
fd_set read_fds;
int fd = ((mbedtls_net_context *)ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
/* Zero fds ready means we timed out */
if (ret == 0) {
return MBEDTLS_ERR_SSL_TIMEOUT;
}
if (ret < 0) {
if (errno == EINTR) {
return MBEDTLS_ERR_SSL_WANT_READ;
}
return MBEDTLS_ERR_NET_RECV_FAILED;
}
/* This call will not block */
return mbedtls_net_recv(ctx, buf, len);
}
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
int ret;
int fd = ((mbedtls_net_context *) ctx)->fd;
if (fd < 0) {
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
}
ret = write(fd, buf, len);
if (ret < 0) {
if (net_would_block( ctx ) != 0) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
if (errno == EPIPE || errno == ECONNRESET) {
return MBEDTLS_ERR_NET_CONN_RESET;
}
if (errno == EINTR) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return ret;
}
void mbedtls_net_free(mbedtls_net_context *ctx)
{
if (ctx->fd < 0) {
return;
}
shutdown(ctx->fd, 2);
close(ctx->fd);
ctx->fd = -1;
}
//////////////////////////////////////////
// dtls
void mbedtls_dtls_net_init(mbedtls_net_context *ctx)
{
mbedtls_net_init(ctx);
}
int mbedtls_dtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
{
return mbedtls_net_connect(ctx, host, port, proto);
}
void mbedtls_dtls_net_usleep(unsigned long usec)
{
mbedtls_net_usleep(usec);
}
int mbedtls_dtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
return mbedtls_net_recv(ctx, buf, len);
}
int mbedtls_dtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
{
return mbedtls_net_recv_timeout(ctx, buf, len, timeout);
}
int mbedtls_dtls_net_send(void *ctx, const unsigned char *buf, size_t len)
{
return mbedtls_net_send(ctx, buf, len);
}
void mbedtls_dtls_net_free(mbedtls_net_context *ctx)
{
mbedtls_net_free(ctx);
}
#endif /* MBEDTLS_NET_C */

View File

@@ -0,0 +1,74 @@
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "tos.h"
#include "timing_alt.h"
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
{
struct mbedtls_timing_hr_time now;
now.timer_ms = tos_systick_get();
if (reset) {
val->timer_ms = now.timer_ms;
}
return (unsigned long)(now.timer_ms - val->timer_ms);
}
/*
* Set delays to watch
*/
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
{
mbedtls_timing_delay_context *ctx;
if (!data) {
return;
}
ctx = (mbedtls_timing_delay_context*)data;
ctx->int_ms = int_ms;
ctx->fin_ms = fin_ms;
if (fin_ms != 0) {
(void)mbedtls_timing_get_timer(&ctx->timer, 1);
}
}
/*
* Get number of delays expired
*/
int mbedtls_timing_get_delay(void *data)
{
unsigned long elapsed_ms;
mbedtls_timing_delay_context *ctx;
if (!data) {
return -1;
}
ctx = (mbedtls_timing_delay_context*)data;
if (ctx->fin_ms == 0) {
return -1;
}
elapsed_ms = mbedtls_timing_get_timer(&ctx->timer, 0);
if (elapsed_ms >= ctx->fin_ms) {
return 2;
}
if (elapsed_ms >= ctx->int_ms) {
return 1;
}
return 0;
}