1313#include <zephyr/sys/util.h>
1414#include <zephyr/ztest.h>
1515
16+ #include <mbedtls/x509.h>
17+
1618LOG_MODULE_REGISTER (tls_test , CONFIG_NET_SOCKETS_LOG_LEVEL );
1719
1820/**
@@ -147,6 +149,7 @@ static const unsigned char client_privkey[] = {
147149static void server_thread_fn (void * arg0 , void * arg1 , void * arg2 )
148150{
149151 const int server_fd = POINTER_TO_INT (arg0 );
152+ const int echo = POINTER_TO_INT (arg1 );
150153
151154 int r ;
152155 int client_fd ;
@@ -176,22 +179,27 @@ static void server_thread_fn(void *arg0, void *arg1, void *arg2)
176179 NET_DBG ("accepted connection from [%s]:%d as fd %d" ,
177180 addrstr , ntohs (sa .sin_port ), client_fd );
178181
179- NET_DBG ("calling recv()" );
180- r = recv (client_fd , addrstr , sizeof (addrstr ), 0 );
181- zassert_not_equal (r , -1 , "recv() failed (%d)" , errno );
182- zassert_equal (r , SECRET_SIZE , "expected: %zu actual: %d" , SECRET_SIZE , r );
183-
184- NET_DBG ("calling send()" );
185- r = send (client_fd , SECRET , SECRET_SIZE , 0 );
186- zassert_not_equal (r , -1 , "send() failed (%d)" , errno );
187- zassert_equal (r , SECRET_SIZE , "expected: %zu actual: %d" , SECRET_SIZE , r );
182+ if (echo ) {
183+ NET_DBG ("calling recv()" );
184+ r = recv (client_fd , addrstr , sizeof (addrstr ), 0 );
185+ zassert_not_equal (r , -1 , "recv() failed (%d)" , errno );
186+ zassert_equal (r , SECRET_SIZE , "expected: %zu actual: %d" ,
187+ SECRET_SIZE , r );
188+
189+ NET_DBG ("calling send()" );
190+ r = send (client_fd , SECRET , SECRET_SIZE , 0 );
191+ zassert_not_equal (r , -1 , "send() failed (%d)" , errno );
192+ zassert_equal (r , SECRET_SIZE , "expected: %zu actual: %d" ,
193+ SECRET_SIZE , r );
194+ }
188195
189196 NET_DBG ("closing client fd" );
190197 r = close (client_fd );
191198 zassert_not_equal (r , -1 , "close() failed on the server fd (%d)" , errno );
192199}
193200
194- static int test_configure_server (k_tid_t * server_thread_id , int peer_verify )
201+ static int test_configure_server (k_tid_t * server_thread_id , int peer_verify ,
202+ int echo )
195203{
196204 static const sec_tag_t server_tag_list_verify_none [] = {
197205 SERVER_CERTIFICATE_TAG ,
@@ -273,7 +281,8 @@ static int test_configure_server(k_tid_t *server_thread_id, int peer_verify)
273281 NET_DBG ("Creating server thread" );
274282 * server_thread_id = k_thread_create (& server_thread , server_stack ,
275283 STACK_SIZE , server_thread_fn ,
276- INT_TO_POINTER (server_fd ), NULL , NULL ,
284+ INT_TO_POINTER (server_fd ),
285+ INT_TO_POINTER (echo ), NULL ,
277286 K_PRIO_PREEMPT (8 ), 0 , K_NO_WAIT );
278287
279288 r = k_sem_take (& server_sem , K_MSEC (TIMEOUT ));
@@ -282,7 +291,8 @@ static int test_configure_server(k_tid_t *server_thread_id, int peer_verify)
282291 return server_fd ;
283292}
284293
285- static int test_configure_client (struct sockaddr_in * sa , bool own_cert )
294+ static int test_configure_client (struct sockaddr_in * sa , bool own_cert ,
295+ const char * hostname )
286296{
287297 static const sec_tag_t client_tag_list_verify_none [] = {
288298 CA_CERTIFICATE_TAG ,
@@ -319,8 +329,8 @@ static int test_configure_client(struct sockaddr_in *sa, bool own_cert)
319329 sec_tag_list , sec_tag_list_size );
320330 zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" , errno );
321331
322- r = setsockopt (client_fd , SOL_TLS , TLS_HOSTNAME , "localhost" ,
323- sizeof ( "localhost" ) );
332+ r = setsockopt (client_fd , SOL_TLS , TLS_HOSTNAME , hostname ,
333+ strlen ( hostname ) + 1 );
324334 zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" , errno );
325335
326336 sa -> sin_family = AF_INET ;
@@ -370,12 +380,13 @@ static void test_common(int peer_verify)
370380 /*
371381 * Server socket setup
372382 */
373- server_fd = test_configure_server (& server_thread_id , peer_verify );
383+ server_fd = test_configure_server (& server_thread_id , peer_verify , true );
374384
375385 /*
376386 * Client socket setup
377387 */
378- client_fd = test_configure_client (& sa , peer_verify != TLS_PEER_VERIFY_NONE );
388+ client_fd = test_configure_client (& sa , peer_verify != TLS_PEER_VERIFY_NONE ,
389+ "localhost" );
379390
380391 /*
381392 * The main part of the test
@@ -418,6 +429,50 @@ ZTEST(net_socket_tls_api_extension, test_tls_peer_verify_required)
418429 test_common (TLS_PEER_VERIFY_REQUIRED );
419430}
420431
432+ static void test_tls_cert_verify_result_opt_common (uint32_t expect )
433+ {
434+ int server_fd , client_fd , ret ;
435+ k_tid_t server_thread_id ;
436+ struct sockaddr_in sa ;
437+ uint32_t optval ;
438+ socklen_t optlen = sizeof (optval );
439+ const char * hostname = "localhost" ;
440+ int peer_verify = TLS_PEER_VERIFY_OPTIONAL ;
441+
442+ if (expect == MBEDTLS_X509_BADCERT_CN_MISMATCH ) {
443+ hostname = "dummy" ;
444+ }
445+
446+ server_fd = test_configure_server (& server_thread_id , TLS_PEER_VERIFY_NONE ,
447+ false);
448+ client_fd = test_configure_client (& sa , false, hostname );
449+
450+ ret = zsock_setsockopt (client_fd , SOL_TLS , TLS_PEER_VERIFY ,
451+ & peer_verify , sizeof (peer_verify ));
452+ zassert_ok (ret , "failed to set TLS_PEER_VERIFY (%d)" , errno );
453+
454+ ret = zsock_connect (client_fd , (struct sockaddr * )& sa , sizeof (sa ));
455+ zassert_not_equal (ret , -1 , "failed to connect (%d)" , errno );
456+
457+ ret = zsock_getsockopt (client_fd , SOL_TLS , TLS_CERT_VERIFY_RESULT ,
458+ & optval , & optlen );
459+ zassert_equal (ret , 0 , "getsockopt failed (%d)" , errno );
460+ zassert_equal (optval , expect , "getsockopt got invalid verify result %d" ,
461+ optval );
462+
463+ test_shutdown (client_fd , server_fd , server_thread_id );
464+ }
465+
466+ ZTEST (net_socket_tls_api_extension , test_tls_cert_verify_result_opt_ok )
467+ {
468+ test_tls_cert_verify_result_opt_common (0 );
469+ }
470+
471+ ZTEST (net_socket_tls_api_extension , test_tls_cert_verify_result_opt_bad_cn )
472+ {
473+ test_tls_cert_verify_result_opt_common (MBEDTLS_X509_BADCERT_CN_MISMATCH );
474+ }
475+
421476static void * setup (void )
422477{
423478 int r ;
0 commit comments