@@ -191,85 +191,65 @@ static void server_thread_fn(void *arg0, void *arg1, void *arg2)
191
191
zassert_not_equal (r , -1 , "close() failed on the server fd (%d)" , errno );
192
192
}
193
193
194
- static void test_common ( int peer_verify )
194
+ static int test_configure_server ( k_tid_t * server_thread_id , int peer_verify )
195
195
{
196
- const int yes = true;
196
+ static const sec_tag_t server_tag_list_verify_none [] = {
197
+ SERVER_CERTIFICATE_TAG ,
198
+ };
199
+
200
+ static const sec_tag_t server_tag_list_verify [] = {
201
+ CA_CERTIFICATE_TAG ,
202
+ SERVER_CERTIFICATE_TAG ,
203
+ };
197
204
198
- int r ;
199
- int server_fd ;
200
- int client_fd ;
201
- int proto = IPPROTO_TCP ;
202
- char * addrstrp ;
203
- k_tid_t server_thread_id ;
204
- struct sockaddr_in sa ;
205
205
char addrstr [INET_ADDRSTRLEN ];
206
+ const sec_tag_t * sec_tag_list ;
207
+ size_t sec_tag_list_size ;
208
+ struct sockaddr_in sa ;
209
+ const int yes = true;
210
+ char * addrstrp ;
211
+ int server_fd ;
212
+ int r ;
206
213
207
214
k_sem_init (& server_sem , 0 , 1 );
208
215
209
- /* set the common protocol for both client and server */
210
- if (IS_ENABLED (CONFIG_NET_SOCKETS_SOCKOPT_TLS )) {
211
- proto = IPPROTO_TLS_1_2 ;
212
- }
213
- /*
214
- * Server socket setup
215
- */
216
-
217
216
NET_DBG ("Creating server socket" );
218
- r = socket (AF_INET , SOCK_STREAM , proto );
217
+ r = socket (AF_INET , SOCK_STREAM , IPPROTO_TLS_1_2 );
219
218
zassert_not_equal (r , -1 , "failed to create server socket (%d)" , errno );
220
219
server_fd = r ;
221
220
222
221
r = setsockopt (server_fd , SOL_SOCKET , SO_REUSEADDR , & yes , sizeof (yes ));
223
222
zassert_not_equal (r , -1 , "failed to set SO_REUSEADDR (%d)" , errno );
224
223
225
- if (IS_ENABLED (CONFIG_TLS_CREDENTIALS )
226
- && IS_ENABLED (CONFIG_NET_SOCKETS_SOCKOPT_TLS )) {
227
-
228
- static const sec_tag_t server_tag_list_verify_none [] = {
229
- SERVER_CERTIFICATE_TAG ,
230
- };
231
-
232
- static const sec_tag_t server_tag_list_verify [] = {
233
- CA_CERTIFICATE_TAG ,
234
- SERVER_CERTIFICATE_TAG ,
235
- };
236
-
237
- const sec_tag_t * sec_tag_list ;
238
- size_t sec_tag_list_size ;
239
-
240
- switch (peer_verify ) {
241
- case TLS_PEER_VERIFY_NONE :
242
- sec_tag_list = server_tag_list_verify_none ;
243
- sec_tag_list_size = sizeof (server_tag_list_verify_none );
244
- break ;
245
- case TLS_PEER_VERIFY_OPTIONAL :
246
- case TLS_PEER_VERIFY_REQUIRED :
247
- sec_tag_list = server_tag_list_verify ;
248
- sec_tag_list_size = sizeof (server_tag_list_verify );
249
-
250
- r = setsockopt (server_fd , SOL_TLS , TLS_PEER_VERIFY ,
251
- & peer_verify , sizeof (peer_verify ));
252
- zassert_not_equal (r , -1 ,
253
- "failed to set TLS_PEER_VERIFY (%d)" , errno );
254
- break ;
255
- default :
256
- zassert_true (false,
257
- "unrecognized TLS peer verify type %d" ,
258
- peer_verify );
259
- return ;
260
- }
261
-
262
- r = setsockopt (server_fd , SOL_TLS , TLS_SEC_TAG_LIST ,
263
- sec_tag_list , sec_tag_list_size );
264
- zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" ,
265
- errno );
266
-
267
- r = setsockopt (server_fd , SOL_TLS , TLS_HOSTNAME , "localhost" ,
268
- sizeof ("localhost" ));
269
- zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" ,
224
+ switch (peer_verify ) {
225
+ case TLS_PEER_VERIFY_NONE :
226
+ sec_tag_list = server_tag_list_verify_none ;
227
+ sec_tag_list_size = sizeof (server_tag_list_verify_none );
228
+ break ;
229
+ case TLS_PEER_VERIFY_OPTIONAL :
230
+ case TLS_PEER_VERIFY_REQUIRED :
231
+ sec_tag_list = server_tag_list_verify ;
232
+ sec_tag_list_size = sizeof (server_tag_list_verify );
233
+
234
+ r = setsockopt (server_fd , SOL_TLS , TLS_PEER_VERIFY ,
235
+ & peer_verify , sizeof (peer_verify ));
236
+ zassert_not_equal (r , -1 , "failed to set TLS_PEER_VERIFY (%d)" ,
270
237
errno );
238
+ break ;
239
+ default :
240
+ zassert_true (false, "unrecognized TLS peer verify type %d" ,
241
+ peer_verify );
242
+ return -1 ;
271
243
}
272
244
245
+ r = setsockopt (server_fd , SOL_TLS , TLS_SEC_TAG_LIST ,
246
+ sec_tag_list , sec_tag_list_size );
247
+ zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" , errno );
248
+
249
+ r = setsockopt (server_fd , SOL_TLS , TLS_HOSTNAME , "localhost" ,
250
+ sizeof ("localhost" ));
251
+ zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" , errno );
252
+
273
253
memset (& sa , 0 , sizeof (sa ));
274
254
/* The server listens on all network interfaces */
275
255
sa .sin_addr .s_addr = INADDR_ANY ;
@@ -291,116 +271,136 @@ static void test_common(int peer_verify)
291
271
addrstr , ntohs (sa .sin_port ), server_fd );
292
272
293
273
NET_DBG ("Creating server thread" );
294
- server_thread_id = k_thread_create (& server_thread , server_stack ,
295
- STACK_SIZE , server_thread_fn ,
296
- INT_TO_POINTER (server_fd ), NULL , NULL ,
297
- K_PRIO_PREEMPT (8 ), 0 , K_NO_WAIT );
274
+ * server_thread_id = k_thread_create (& server_thread , server_stack ,
275
+ STACK_SIZE , server_thread_fn ,
276
+ INT_TO_POINTER (server_fd ), NULL , NULL ,
277
+ K_PRIO_PREEMPT (8 ), 0 , K_NO_WAIT );
298
278
299
279
r = k_sem_take (& server_sem , K_MSEC (TIMEOUT ));
300
280
zassert_equal (0 , r , "failed to synchronize with server thread (%d)" , r );
301
281
302
- /*
303
- * Client socket setup
304
- */
282
+ return server_fd ;
283
+ }
284
+
285
+ static int test_configure_client (struct sockaddr_in * sa , bool own_cert )
286
+ {
287
+ static const sec_tag_t client_tag_list_verify_none [] = {
288
+ CA_CERTIFICATE_TAG ,
289
+ };
290
+
291
+ static const sec_tag_t client_tag_list_verify [] = {
292
+ CA_CERTIFICATE_TAG ,
293
+ CLIENT_CERTIFICATE_TAG ,
294
+ };
295
+
296
+ char addrstr [INET_ADDRSTRLEN ];
297
+ const sec_tag_t * sec_tag_list ;
298
+ size_t sec_tag_list_size ;
299
+ char * addrstrp ;
300
+ int client_fd ;
301
+ int r ;
305
302
306
303
k_thread_name_set (k_current_get (), "client" );
307
304
308
305
NET_DBG ("Creating client socket" );
309
- r = socket (AF_INET , SOCK_STREAM , proto );
306
+ r = socket (AF_INET , SOCK_STREAM , IPPROTO_TLS_1_2 );
310
307
zassert_not_equal (r , -1 , "failed to create client socket (%d)" , errno );
311
308
client_fd = r ;
312
309
313
- if (IS_ENABLED (CONFIG_TLS_CREDENTIALS )
314
- && IS_ENABLED (CONFIG_NET_SOCKETS_SOCKOPT_TLS )) {
315
-
316
- static const sec_tag_t client_tag_list_verify_none [] = {
317
- CA_CERTIFICATE_TAG ,
318
- };
319
-
320
- static const sec_tag_t client_tag_list_verify [] = {
321
- CA_CERTIFICATE_TAG ,
322
- CLIENT_CERTIFICATE_TAG ,
323
- };
324
-
325
- const sec_tag_t * sec_tag_list ;
326
- size_t sec_tag_list_size ;
327
-
328
- switch (peer_verify ) {
329
- case TLS_PEER_VERIFY_NONE :
330
- sec_tag_list = client_tag_list_verify_none ;
331
- sec_tag_list_size = sizeof (client_tag_list_verify_none );
332
- break ;
333
- case TLS_PEER_VERIFY_OPTIONAL :
334
- case TLS_PEER_VERIFY_REQUIRED :
335
- sec_tag_list = client_tag_list_verify ;
336
- sec_tag_list_size = sizeof (client_tag_list_verify );
337
- break ;
338
- default :
339
- zassert_true (false, "unrecognized TLS peer verify type %d" ,
340
- peer_verify );
341
- return ;
342
- }
343
-
344
- r = setsockopt (client_fd , SOL_TLS , TLS_SEC_TAG_LIST ,
345
- sec_tag_list , sec_tag_list_size );
346
- zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" ,
347
- errno );
310
+ if (own_cert ) {
311
+ sec_tag_list = client_tag_list_verify ;
312
+ sec_tag_list_size = sizeof (client_tag_list_verify );
313
+ } else {
314
+ sec_tag_list = client_tag_list_verify_none ;
315
+ sec_tag_list_size = sizeof (client_tag_list_verify_none );
316
+ }
317
+
318
+ r = setsockopt (client_fd , SOL_TLS , TLS_SEC_TAG_LIST ,
319
+ sec_tag_list , sec_tag_list_size );
320
+ zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" , errno );
348
321
349
- r = setsockopt (client_fd , SOL_TLS , TLS_HOSTNAME , "localhost" ,
322
+ r = setsockopt (client_fd , SOL_TLS , TLS_HOSTNAME , "localhost" ,
350
323
sizeof ("localhost" ));
351
- zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" , errno );
352
- }
324
+ zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" , errno );
353
325
354
- r = inet_pton (AF_INET , MY_IPV4_ADDR , & sa .sin_addr .s_addr );
326
+ sa -> sin_family = AF_INET ;
327
+ sa -> sin_port = htons (PORT );
328
+ r = inet_pton (AF_INET , MY_IPV4_ADDR , & sa -> sin_addr .s_addr );
355
329
zassert_not_equal (-1 , r , "inet_pton() failed (%d)" , errno );
356
330
zassert_not_equal (0 , r , "%s is not a valid IPv4 address" , MY_IPV4_ADDR );
357
331
zassert_equal (1 , r , "inet_pton() failed to convert %s" , MY_IPV4_ADDR );
358
332
359
333
memset (addrstr , '\0' , sizeof (addrstr ));
360
- addrstrp = (char * )inet_ntop (AF_INET , & sa . sin_addr ,
334
+ addrstrp = (char * )inet_ntop (AF_INET , & sa -> sin_addr ,
361
335
addrstr , sizeof (addrstr ));
362
336
zassert_not_equal (addrstrp , NULL , "inet_ntop() failed (%d)" , errno );
363
337
364
338
NET_DBG ("connecting to [%s]:%d with fd %d" ,
365
- addrstr , ntohs (sa . sin_port ), client_fd );
339
+ addrstr , ntohs (sa -> sin_port ), client_fd );
366
340
367
- r = connect (client_fd , (struct sockaddr * )& sa , sizeof (sa ));
368
- zassert_not_equal (r , -1 , "failed to connect (%d)" , errno );
341
+ return client_fd ;
342
+ }
343
+ static void test_shutdown (int client_fd , int server_fd , k_tid_t server_thread_id )
344
+ {
345
+ int r ;
346
+
347
+ NET_DBG ("closing client fd" );
348
+ r = close (client_fd );
349
+ zassert_not_equal (-1 , r , "close() failed on the client fd (%d)" , errno );
350
+
351
+ NET_DBG ("closing server fd" );
352
+ r = close (server_fd );
353
+ zassert_not_equal (-1 , r , "close() failed on the server fd (%d)" , errno );
354
+
355
+ r = k_thread_join (& server_thread , K_FOREVER );
356
+ zassert_equal (0 , r , "k_thread_join() failed (%d)" , r );
357
+
358
+ k_yield ();
359
+ }
360
+
361
+ static void test_common (int peer_verify )
362
+ {
363
+ k_tid_t server_thread_id ;
364
+ struct sockaddr_in sa ;
365
+ uint8_t rx_buf [16 ];
366
+ int server_fd ;
367
+ int client_fd ;
368
+ int r ;
369
+
370
+ /*
371
+ * Server socket setup
372
+ */
373
+ server_fd = test_configure_server (& server_thread_id , peer_verify );
374
+
375
+ /*
376
+ * Client socket setup
377
+ */
378
+ client_fd = test_configure_client (& sa , peer_verify != TLS_PEER_VERIFY_NONE );
369
379
370
380
/*
371
381
* The main part of the test
372
382
*/
373
383
384
+ r = connect (client_fd , (struct sockaddr * )& sa , sizeof (sa ));
385
+ zassert_not_equal (r , -1 , "failed to connect (%d)" , errno );
386
+
374
387
NET_DBG ("Calling send()" );
375
388
r = send (client_fd , SECRET , SECRET_SIZE , 0 );
376
389
zassert_not_equal (r , -1 , "send() failed (%d)" , errno );
377
390
zassert_equal (SECRET_SIZE , r , "expected: %zu actual: %d" , SECRET_SIZE , r );
378
391
379
392
NET_DBG ("Calling recv()" );
380
- memset (addrstr , 0 , sizeof (addrstr ));
381
- r = recv (client_fd , addrstr , sizeof (addrstr ), 0 );
393
+ memset (rx_buf , 0 , sizeof (rx_buf ));
394
+ r = recv (client_fd , rx_buf , sizeof (rx_buf ), 0 );
382
395
zassert_not_equal (r , -1 , "recv() failed (%d)" , errno );
383
396
zassert_equal (SECRET_SIZE , r , "expected: %zu actual: %d" , SECRET_SIZE , r );
384
-
385
- zassert_mem_equal (SECRET , addrstr , SECRET_SIZE ,
386
- "expected: %s actual: %s" , SECRET , addrstr );
397
+ zassert_mem_equal (SECRET , rx_buf , SECRET_SIZE ,
398
+ "expected: %s actual: %s" , SECRET , rx_buf );
387
399
388
400
/*
389
401
* Cleanup resources
390
402
*/
391
-
392
- NET_DBG ("closing client fd" );
393
- r = close (client_fd );
394
- zassert_not_equal (-1 , r , "close() failed on the client fd (%d)" , errno );
395
-
396
- NET_DBG ("closing server fd" );
397
- r = close (server_fd );
398
- zassert_not_equal (-1 , r , "close() failed on the server fd (%d)" , errno );
399
-
400
- r = k_thread_join (& server_thread , K_FOREVER );
401
- zassert_equal (0 , r , "k_thread_join() failed (%d)" , r );
402
-
403
- k_yield ();
403
+ test_shutdown (client_fd , server_fd , server_thread_id );
404
404
}
405
405
406
406
ZTEST (net_socket_tls_api_extension , test_tls_peer_verify_none )
0 commit comments