13
13
#include <zephyr/sys/util.h>
14
14
#include <zephyr/ztest.h>
15
15
16
+ #include <mbedtls/x509.h>
17
+
16
18
LOG_MODULE_REGISTER (tls_test , CONFIG_NET_SOCKETS_LOG_LEVEL );
17
19
18
20
/**
@@ -147,6 +149,7 @@ static const unsigned char client_privkey[] = {
147
149
static void server_thread_fn (void * arg0 , void * arg1 , void * arg2 )
148
150
{
149
151
const int server_fd = POINTER_TO_INT (arg0 );
152
+ const int echo = POINTER_TO_INT (arg1 );
150
153
151
154
int r ;
152
155
int client_fd ;
@@ -176,22 +179,27 @@ static void server_thread_fn(void *arg0, void *arg1, void *arg2)
176
179
NET_DBG ("accepted connection from [%s]:%d as fd %d" ,
177
180
addrstr , ntohs (sa .sin_port ), client_fd );
178
181
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
+ }
188
195
189
196
NET_DBG ("closing client fd" );
190
197
r = close (client_fd );
191
198
zassert_not_equal (r , -1 , "close() failed on the server fd (%d)" , errno );
192
199
}
193
200
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 )
195
203
{
196
204
static const sec_tag_t server_tag_list_verify_none [] = {
197
205
SERVER_CERTIFICATE_TAG ,
@@ -273,7 +281,8 @@ static int test_configure_server(k_tid_t *server_thread_id, int peer_verify)
273
281
NET_DBG ("Creating server thread" );
274
282
* server_thread_id = k_thread_create (& server_thread , server_stack ,
275
283
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 ,
277
286
K_PRIO_PREEMPT (8 ), 0 , K_NO_WAIT );
278
287
279
288
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)
282
291
return server_fd ;
283
292
}
284
293
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 )
286
296
{
287
297
static const sec_tag_t client_tag_list_verify_none [] = {
288
298
CA_CERTIFICATE_TAG ,
@@ -319,8 +329,8 @@ static int test_configure_client(struct sockaddr_in *sa, bool own_cert)
319
329
sec_tag_list , sec_tag_list_size );
320
330
zassert_not_equal (r , -1 , "failed to set TLS_SEC_TAG_LIST (%d)" , errno );
321
331
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 );
324
334
zassert_not_equal (r , -1 , "failed to set TLS_HOSTNAME (%d)" , errno );
325
335
326
336
sa -> sin_family = AF_INET ;
@@ -370,12 +380,13 @@ static void test_common(int peer_verify)
370
380
/*
371
381
* Server socket setup
372
382
*/
373
- server_fd = test_configure_server (& server_thread_id , peer_verify );
383
+ server_fd = test_configure_server (& server_thread_id , peer_verify , true );
374
384
375
385
/*
376
386
* Client socket setup
377
387
*/
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" );
379
390
380
391
/*
381
392
* The main part of the test
@@ -418,6 +429,50 @@ ZTEST(net_socket_tls_api_extension, test_tls_peer_verify_required)
418
429
test_common (TLS_PEER_VERIFY_REQUIRED );
419
430
}
420
431
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
+
421
476
static void * setup (void )
422
477
{
423
478
int r ;
0 commit comments