Skip to content

Commit f8bc0ce

Browse files
authored
Merge pull request #10457 from danielinux/iotsafe-fix-tls-wrapper
IDE/iotsafe: fix memory TLS wrapper and example build
2 parents 15f3f7b + 7003f6b commit f8bc0ce

4 files changed

Lines changed: 114 additions & 48 deletions

File tree

IDE/iotsafe/devices.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@ void * _sbrk(unsigned int incr)
240240
heap += incr;
241241
return old_heap;
242242
}
243-
void * _sbrk_r(unsigned int incr)
244-
{
245-
static unsigned char *heap = NULL;
246-
void *old_heap = heap;
247-
if (((incr >> 2) << 2) != incr)
248-
incr = ((incr >> 2) + 1) << 2;
249-
if (old_heap == NULL)
250-
old_heap = heap = (unsigned char *)&_start_heap;
251-
heap += incr;
252-
return old_heap;
253-
}
254243

255244
int _close(int fd)
256245
{

IDE/iotsafe/devices.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef STM32L496_DEVICES
2626
#define STM32L496_DEVICES
2727

28+
#include <stdint.h>
29+
2830
/* CPU clock speed */
2931
//#define CLOCK_SPEED 14200000
3032
//#define CLOCK_SPEED 6000000

IDE/iotsafe/memory-tls.c

Lines changed: 111 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,70 +71,136 @@ static int client_bytes;
7171
static int client_write_idx;
7272
static int client_read_idx;
7373

74+
static int mem_send(unsigned char* dst, int* write_idx, int* bytes,
75+
const char* buf, int sz)
76+
{
77+
int available;
78+
79+
if (buf == NULL || dst == NULL || write_idx == NULL || bytes == NULL ||
80+
sz <= 0) {
81+
return WOLFSSL_CBIO_ERR_GENERAL;
82+
}
83+
84+
if (*write_idx < 0 || *write_idx > TLS_BUFFERS_SZ ||
85+
*bytes < 0 || *bytes > TLS_BUFFERS_SZ) {
86+
return WOLFSSL_CBIO_ERR_GENERAL;
87+
}
88+
89+
available = TLS_BUFFERS_SZ - *write_idx;
90+
if (available <= 0) {
91+
return WOLFSSL_CBIO_ERR_WANT_WRITE;
92+
}
93+
if (sz > available) {
94+
sz = available;
95+
}
96+
97+
XMEMCPY(&dst[*write_idx], buf, sz);
98+
*write_idx += sz;
99+
*bytes += sz;
100+
101+
return sz;
102+
}
103+
104+
static int mem_recv(char* buf, int sz, unsigned char* src, int* read_idx,
105+
int* write_idx, int* bytes)
106+
{
107+
int available;
108+
109+
if (buf == NULL || src == NULL || read_idx == NULL || write_idx == NULL ||
110+
bytes == NULL || sz <= 0) {
111+
return WOLFSSL_CBIO_ERR_GENERAL;
112+
}
113+
114+
if (*read_idx < 0 || *write_idx < *read_idx || *write_idx > TLS_BUFFERS_SZ ||
115+
*bytes < 0 || *bytes > TLS_BUFFERS_SZ) {
116+
return WOLFSSL_CBIO_ERR_GENERAL;
117+
}
118+
119+
available = *write_idx - *read_idx;
120+
if (available <= 0) {
121+
return WOLFSSL_CBIO_ERR_WANT_READ;
122+
}
123+
if (sz > available) {
124+
sz = available;
125+
}
126+
127+
XMEMCPY(buf, &src[*read_idx], sz);
128+
*read_idx += sz;
129+
*bytes -= sz;
130+
131+
if (*read_idx == *write_idx) {
132+
*read_idx = 0;
133+
*write_idx = 0;
134+
}
135+
136+
return sz;
137+
}
138+
74139

75140
/* server send callback */
76141
int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
77142
{
78-
if (client_write_idx + sz > TLS_BUFFERS_SZ) {
79-
return WOLFSSL_CBIO_ERR_WANT_WRITE;
143+
int ret;
144+
145+
(void)ssl;
146+
(void)ctx;
147+
148+
ret = mem_send(to_client, &client_write_idx, &client_bytes, buf, sz);
149+
if (ret > 0) {
150+
printf("=== Srv-Cli: %d\n", ret);
80151
}
81-
printf("=== Srv-Cli: %d\n", sz);
82-
XMEMCPY(&to_client[client_write_idx], buf, sz);
83-
client_write_idx += sz;
84-
client_bytes += sz;
85-
return sz;
152+
return ret;
86153
}
87154

88155

89156
/* server recv callback */
90157
int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
91158
{
92-
if (server_bytes - server_read_idx < sz) {
93-
return WOLFSSL_CBIO_ERR_WANT_READ;
94-
}
95-
XMEMCPY(buf, &to_server[server_read_idx], sz);
96-
server_read_idx += sz;
159+
int ret;
160+
161+
(void)ssl;
162+
(void)ctx;
97163

98-
if (server_read_idx == server_write_idx) {
99-
server_read_idx = server_write_idx = 0;
100-
server_bytes = 0;
164+
ret = mem_recv(buf, sz, to_server, &server_read_idx, &server_write_idx,
165+
&server_bytes);
166+
if (ret > 0) {
167+
printf("=== Srv RX: %d\n", ret);
101168
}
102-
printf("=== Srv RX: %d\n", sz);
103-
return sz;
169+
return ret;
104170
}
105171

106172

107173
/* client send callback */
108174
int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
109175
{
110-
if (server_write_idx + sz > TLS_BUFFERS_SZ)
111-
return WOLFSSL_CBIO_ERR_WANT_WRITE;
176+
int ret;
112177

113-
printf("=== Cli->Srv: %d\n", sz);
114-
XMEMCPY(&to_server[server_write_idx], buf, sz);
115-
server_write_idx += sz;
116-
server_bytes += sz;
178+
(void)ssl;
179+
(void)ctx;
117180

118-
return sz;
181+
ret = mem_send(to_server, &server_write_idx, &server_bytes, buf, sz);
182+
if (ret > 0) {
183+
printf("=== Cli->Srv: %d\n", ret);
184+
}
185+
186+
return ret;
119187
}
120188

121189

122190
/* client recv callback */
123191
int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
124192
{
125-
if (client_bytes - client_read_idx < sz) {
126-
return WOLFSSL_CBIO_ERR_WANT_READ;
127-
}
193+
int ret;
128194

129-
XMEMCPY(buf, &to_client[client_read_idx], sz);
130-
client_read_idx += sz;
195+
(void)ssl;
196+
(void)ctx;
131197

132-
if (client_read_idx == client_write_idx) {
133-
client_read_idx = client_write_idx = 0;
134-
client_bytes = 0;
198+
ret = mem_recv(buf, sz, to_client, &client_read_idx, &client_write_idx,
199+
&client_bytes);
200+
if (ret > 0) {
201+
printf("=== Cli RX: %d\n", ret);
135202
}
136-
printf("=== Cli RX: %d\n", sz);
137-
return sz;
203+
return ret;
138204
}
139205

140206
/* wolfSSL Client loop */
@@ -175,7 +241,11 @@ static int client_loop(void)
175241
return 0;
176242
}
177243
printf("Client: Enabling IoT Safe in CTX\n");
178-
wolfSSL_CTX_iotsafe_enable(cli_ctx);
244+
ret = wolfSSL_CTX_iotsafe_enable(cli_ctx);
245+
if (ret != WOLFSSL_SUCCESS) {
246+
printf("Cannot enable IoT-Safe in client ctx: %d\n", ret);
247+
return -1;
248+
}
179249

180250
printf("Loading CA\n");
181251
#ifdef SOFT_SERVER_CA
@@ -261,8 +331,12 @@ static int client_loop(void)
261331

262332
printf("Setting TLS options: turn on IoT-safe for this socket\n");
263333

264-
wolfSSL_iotsafe_on_ex(cli_ssl, &privkey_id, &keypair_id,
334+
ret = wolfSSL_iotsafe_on_ex(cli_ssl, &privkey_id, &keypair_id,
265335
&peer_pubkey_id, &peer_cert_id, IOTSAFE_ID_SIZE);
336+
if (ret != WOLFSSL_SUCCESS) {
337+
printf("Cannot enable IoT-Safe on client ssl: %d\n", ret);
338+
return -1;
339+
}
266340

267341
#ifdef WOLFSSL_TLS13
268342
printf("Setting TLSv1.3 for SECP256R1 key share\n");
@@ -390,6 +464,7 @@ static int server_loop(void)
390464
return -1;
391465
}
392466
if (ret > 0) {
467+
buf[ret] = '\0';
393468
printf("++++++ Server received msg from client: '%s'\n", buf);
394469
printf("IoT-Safe TEST SUCCESSFUL\n");
395470

IDE/iotsafe/user_settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static inline long XTIME(long *x) { return jiffies;}
119119
/* Math */
120120
#define TFM_TIMING_RESISTANT
121121
#define TFM_ARM
122-
#define WOLFSSL_SP_MATH
123122
#define WOLFSSL_SP_MATH_ALL
124123
#define WOLFSSL_SP_SMALL
125124
#define WOLFSSL_HAVE_SP_DH
@@ -171,6 +170,7 @@ static inline long XTIME(long *x) { return jiffies;}
171170
/* Disable Features */
172171
#define NO_WRITEV
173172
#define NO_FILESYSTEM
173+
#define WOLFSSL_NO_SOCK
174174
#define NO_MAIN_DRIVER
175175
//#define NO_ERROR_STRINGS
176176

0 commit comments

Comments
 (0)