@@ -71,70 +71,136 @@ static int client_bytes;
7171static int client_write_idx ;
7272static 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 */
76141int 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 */
90157int 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 */
108174int 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 */
123191int 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
0 commit comments