Debian Codegraph @

Xpjproject/pjnath/src/pjnath-test/sess_auth.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21 #define THIS_FILE "sess_auth.c"
22
23 #define REALM "STUN session test"
24 #define USERNAME "theusername"
25 #define PASSWORD "thepassword"
26 #define NONCE "thenonce"
27
28
29 /* STUN config */
30 static pj_stun_config stun_cfg;
31
32
33 //////////////////////////////////////////////////////////////////////////////////////////
34 //
35 // SERVER PART
36 //
37
38
39 /* Server instance */
40 static struct server
41 {
42 pj_pool_t *pool;
43 pj_sockaddr addr;
44 pj_stun_session *sess;
45
46 pj_bool_t responding;
47 unsigned recv_count;
48 pj_stun_auth_type auth_type;
49
50 pj_sock_t sock;
51
52 pj_bool_t quit;
53 pj_thread_t *thread;
54 } *server;
55
56
57 static pj_status_t server_send_msg(pj_stun_session *sess,
58 void *token,
59 const void *pkt,
60 pj_size_t pkt_size,
61 const pj_sockaddr_t *dst_addr,
62 unsigned addr_len)
63 {
64 pj_ssize_t len = pkt_size;
65
66 PJ_UNUSED_ARG(sess);
67 PJ_UNUSED_ARG(token);
68
69 return pj_sock_sendto(server->sock, pkt, &len, 0, dst_addr, addr_len);
70 }
71
72 static pj_status_t server_on_rx_request(pj_stun_session *sess,
73 const pj_uint8_t *pkt,
74 unsigned pkt_len,
75 const pj_stun_rx_data *rdata,
76 void *token,
77 const pj_sockaddr_t *src_addr,
78 unsigned src_addr_len)
79 {
80 PJ_UNUSED_ARG(pkt);
81 PJ_UNUSED_ARG(pkt_len);
82 PJ_UNUSED_ARG(token);
83
84 return pj_stun_session_respond(sess, rdata, 0, NULL, NULL, PJ_TRUE,
85 src_addr, src_addr_len);
86 }
87
88
89 static pj_status_t server_get_auth(void *user_data,
90 pj_pool_t *pool,
91 pj_str_t *realm,
92 pj_str_t *nonce)
93 {
94 PJ_UNUSED_ARG(user_data);
95 PJ_UNUSED_ARG(pool);
96
97 if (server->auth_type == PJ_STUN_AUTH_SHORT_TERM) {
98 realm->slen = nonce->slen = 0;
99 } else {
100 *realm = pj_str(REALM);
101 *nonce = pj_str(NONCE);
102 }
103
104 return PJ_SUCCESS;
105 }
106
107
108 static pj_status_t server_get_password( const pj_stun_msg *msg,
109 void *user_data,
110 const pj_str_t *realm,
111 const pj_str_t *username,
112 pj_pool_t *pool,
113 pj_stun_passwd_type *data_type,
114 pj_str_t *data)
115 {
116 PJ_UNUSED_ARG(msg);
117 PJ_UNUSED_ARG(user_data);
118 PJ_UNUSED_ARG(pool);
119
120 if (server->auth_type == PJ_STUN_AUTH_SHORT_TERM) {
121 if (realm && realm->slen) {
122 PJ_LOG(4,(THIS_FILE, " server expecting short term"));
123 return -1;
124 }
125 } else {
126 if (realm==NULL || realm->slen==0) {
127 PJ_LOG(4,(THIS_FILE, " realm not present"));
128 return -1;
129 }
130 }
131
132 if (pj_strcmp2(username, USERNAME) != 0) {
133 PJ_LOG(4,(THIS_FILE, " wrong username"));
134 return -1;
135 }
136
137 *data_type = PJ_STUN_PASSWD_PLAIN;
138 *data = pj_str(PASSWORD);
139
140 return PJ_SUCCESS;
141 }
142
143
144 static pj_bool_t server_verify_nonce(const pj_stun_msg *msg,
145 void *user_data,
146 const pj_str_t *realm,
147 const pj_str_t *username,
148 const pj_str_t *nonce)
149 {
150 PJ_UNUSED_ARG(msg);
151 PJ_UNUSED_ARG(user_data);
152 PJ_UNUSED_ARG(realm);
153 PJ_UNUSED_ARG(username);
154
155 if (pj_strcmp2(nonce, NONCE) != 0)
156 return PJ_FALSE;
157
158 return PJ_TRUE;
159 }
160
161
162 static int server_thread(void *unused)
163 {
164 PJ_UNUSED_ARG(unused);
165
166 PJ_LOG(5,("", " server thread started"));
167
168 while (!server->quit) {
169 pj_fd_set_t readset;
170 pj_time_val delay = {0, 10};
171
172 PJ_FD_ZERO(&readset);
173 PJ_FD_SET(server->sock, &readset);
174
175 if (pj_sock_select((int)server->sock+1, &readset, NULL, NULL, &delay)==1
176 && PJ_FD_ISSET(server->sock, &readset))
177 {
178 char pkt[1000];
179 pj_ssize_t len;
180 pj_status_t status;
181 pj_sockaddr src_addr;
182 int src_addr_len;
183
184 len = sizeof(pkt);
185 src_addr_len = sizeof(src_addr);
186
187 status = pj_sock_recvfrom(server->sock, pkt, &len, 0, &src_addr, &src_addr_len);
188 if (status != PJ_SUCCESS)
189 continue;
190
191 /* Increment server's receive count */
192 server->recv_count++;
193
194 /* Only pass to server if we allow to respond */
195 if (!server->responding)
196 continue;
197
198 pj_stun_session_on_rx_pkt(server->sess, pkt, len,
199 PJ_STUN_CHECK_PACKET | PJ_STUN_IS_DATAGRAM,
200 NULL, NULL, &src_addr, src_addr_len);
201 }
202 }
203
204 return 0;
205 }
206
207
208 /* Destroy server */
209 static void destroy_server(void)
210 {
211 if (server->thread) {
212 server->quit = PJ_TRUE;
213 pj_thread_join(server->thread);
214 pj_thread_destroy(server->thread);
215 }
216
217 if (server->sock) {
218 pj_sock_close(server->sock);
219 }
220
221 if (server->sess) {
222 pj_stun_session_destroy(server->sess);
223 }
224
225 pj_pool_release(server->pool);
226 server = NULL;
227 }
228
229 /* Instantiate standard server */
230 static int create_std_server(pj_stun_auth_type auth_type,
231 pj_bool_t responding,
232 pj_bool_t use_ipv6)
233 {
234 pj_pool_t *pool;
235 pj_stun_session_cb sess_cb;
236 pj_stun_auth_cred cred;
237 pj_status_t status;
238
239 /* Create server */
240 pool = pj_pool_create(mem, "server", 1000, 1000, NULL);
241 server = PJ_POOL_ZALLOC_T(pool, struct server);
242 server->pool = pool;
243 server->auth_type = auth_type;
244 server->responding = responding;
245
246 /* Create STUN session */
247 pj_bzero(&sess_cb, sizeof(sess_cb));
248 sess_cb.on_rx_request = &server_on_rx_request;
249 sess_cb.on_send_msg = &server_send_msg;
250 status = pj_stun_session_create(&stun_cfg, "server", &sess_cb, PJ_FALSE, NULL, &server->sess);
251 if (status != PJ_SUCCESS) {
252 destroy_server();
253 return -10;
254 }
255
256 /* Configure credential */
257 pj_bzero(&cred, sizeof(cred));
258 cred.type = PJ_STUN_AUTH_CRED_DYNAMIC;
259 cred.data.dyn_cred.get_auth = &server_get_auth;
260 cred.data.dyn_cred.get_password = &server_get_password;
261 cred.data.dyn_cred.verify_nonce = &server_verify_nonce;
262 status = pj_stun_session_set_credential(server->sess, auth_type, &cred);
263 if (status != PJ_SUCCESS) {
264 destroy_server();
265 return -20;
266 }
267
268 /* Create socket */
269 status = pj_sock_socket(GET_AF(use_ipv6), pj_SOCK_DGRAM(), 0, &server->sock);
270 if (status != PJ_SUCCESS) {
271 destroy_server();
272 return -30;
273 }
274
275 /* Bind */
276 pj_sockaddr_init(GET_AF(use_ipv6), &server->addr, NULL, 0);
277 status = pj_sock_bind(server->sock, &server->addr, pj_sockaddr_get_len(&server->addr));
278 if (status != PJ_SUCCESS) {
279 destroy_server();
280 return -40;
281 } else {
282 /* Get the bound IP address */
283 int namelen = sizeof(server->addr);
284 pj_sockaddr addr;
285
286 status = pj_sock_getsockname(server->sock, &server->addr, &namelen);
287 if (status != PJ_SUCCESS) {
288 destroy_server();
289 return -43;
290 }
291
292 if (use_ipv6) {
293 /* pj_gethostip() may return IPv6 link-local and currently it will cause
294 * 'no route to host' error, so let's just hardcode to [::1]
295 */
296 pj_sockaddr_init(pj_AF_INET6(), &addr, NULL, 0);
297 addr.ipv6.sin6_addr.s6_addr[15] = 1;
298 } else {
299 status = pj_gethostip(GET_AF(use_ipv6), &addr);
300 if (status != PJ_SUCCESS) {
301 destroy_server();
302 return -45;
303 }
304 }
305
306 pj_sockaddr_copy_addr(&server->addr, &addr);
307 }
308
309
310 /* Create worker thread */
311 status = pj_thread_create(pool, "server", &server_thread, 0, 0, 0, &server->thread);
312 if (status != PJ_SUCCESS) {
313 destroy_server();
314 return -30;
315 }
316
317 return 0;
318 }
319
320
321 //////////////////////////////////////////////////////////////////////////////////////////
322 //
323 // CLIENT PART
324 //
325
326 static struct client
327 {
328 pj_pool_t *pool;
329 pj_stun_session *sess;
330 pj_sem_t *test_complete;
331 pj_sock_t sock;
332
333 pj_bool_t responding;
334 unsigned recv_count;
335
336 pj_status_t response_status;
337 pj_stun_msg *response;
338
339 pj_bool_t quit;
340 pj_thread_t *thread;
341 } *client;
342
343
344 static pj_status_t client_send_msg(pj_stun_session *sess,
345 void *token,
346 const void *pkt,
347 pj_size_t pkt_size,
348 const pj_sockaddr_t *dst_addr,
349 unsigned addr_len)
350 {
351 pj_ssize_t len = pkt_size;
352
353 PJ_UNUSED_ARG(sess);
354 PJ_UNUSED_ARG(token);
355
356 return pj_sock_sendto(client->sock, pkt, &len, 0, dst_addr, addr_len);
357 }
358
359
360 static void client_on_request_complete( pj_stun_session *sess,
361 pj_status_t status,
362 void *token,
363 pj_stun_tx_data *tdata,
364 const pj_stun_msg *response,
365 const pj_sockaddr_t *src_addr,
366 unsigned src_addr_len)
367 {
368 PJ_UNUSED_ARG(sess);
369 PJ_UNUSED_ARG(token);
370 PJ_UNUSED_ARG(tdata);
371 PJ_UNUSED_ARG(src_addr);
372 PJ_UNUSED_ARG(src_addr_len);
373
374 client->response_status = status;
375 if (response)
376 client->response = pj_stun_msg_clone(client->pool, response);
377
378 pj_sem_post(client->test_complete);
379 }
380
381
382 static int client_thread(void *unused)
383 {
384 PJ_UNUSED_ARG(unused);
385
386 while (!client->quit) {
387 pj_fd_set_t readset;
388 pj_time_val delay = {0, 10};
389
390 /* Also poll the timer heap */
391 pj_timer_heap_poll(stun_cfg.timer_heap, NULL);
392
393 /* Poll client socket */
394 PJ_FD_ZERO(&readset);
395 PJ_FD_SET(client->sock, &readset);
396
397 if (pj_sock_select((int)client->sock+1, &readset, NULL, NULL, &delay)==1
398 && PJ_FD_ISSET(client->sock, &readset))
399 {
400 char pkt[1000];
401 pj_ssize_t len;
402 pj_status_t status;
403 pj_sockaddr src_addr;
404 int src_addr_len;
405
406 len = sizeof(pkt);
407 src_addr_len = sizeof(src_addr);
408
409 status = pj_sock_recvfrom(client->sock, pkt, &len, 0, &src_addr, &src_addr_len);
410 if (status != PJ_SUCCESS)
411 continue;
412
413 /* Increment client's receive count */
414 client->recv_count++;
415
416 /* Only pass to client if we allow to respond */
417 if (!client->responding)
418 continue;
419
420 pj_stun_session_on_rx_pkt(client->sess, pkt, len,
421 PJ_STUN_CHECK_PACKET | PJ_STUN_IS_DATAGRAM,
422 NULL, NULL, &src_addr, src_addr_len);
423 }
424
425 }
426
427 return 0;
428 }
429
430
431 static void destroy_client_server(void)
432 {
433 if (client->thread) {
434 client->quit = 1;
435 pj_thread_join(client->thread);
436 pj_thread_destroy(client->thread);
437 }
438
439 if (client->sess)
440 pj_stun_session_destroy(client->sess);
441
442 if (client->sock)
443 pj_sock_close(client->sock);
444
445 if (client->test_complete)
446 pj_sem_destroy(client->test_complete);
447
448 if (server)
449 destroy_server();
450 }
451
452 static int run_client_test(const char *title,
453
454 pj_bool_t server_responding,
455 pj_stun_auth_type server_auth_type,
456
457 pj_stun_auth_type client_auth_type,
458 const char *realm,
459 const char *username,
460 const char *nonce,
461 const char *password,
462 pj_bool_t dummy_mi,
463 pj_bool_t use_ipv6,
464 pj_bool_t expected_error,
465 pj_status_t expected_code,
466 const char *expected_realm,
467 const char *expected_nonce,
468
469 int (*more_check)(void))
470 {
471 pj_pool_t *pool;
472 pj_stun_session_cb sess_cb;
473 pj_stun_auth_cred cred;
474 pj_stun_tx_data *tdata;
475 pj_status_t status;
476 pj_sockaddr addr;
477 int rc = 0;
478
479 PJ_LOG(3,(THIS_FILE, " %s test (%s)", title, use_ipv6?"IPv6":"IPv4"));
480
481 /* Create client */
482 pool = pj_pool_create(mem, "client", 1000, 1000, NULL);
483 client = PJ_POOL_ZALLOC_T(pool, struct client);
484 client->pool = pool;
485 client->responding = PJ_TRUE;
486
487 /* Create STUN session */
488 pj_bzero(&sess_cb, sizeof(sess_cb));
489 sess_cb.on_request_complete = &client_on_request_complete;
490 sess_cb.on_send_msg = &client_send_msg;
491 status = pj_stun_session_create(&stun_cfg, "client", &sess_cb, PJ_FALSE, NULL, &client->sess);
492 if (status != PJ_SUCCESS) {
493 destroy_client_server();
494 return -200;
495 }
496
497 /* Create semaphore */
498 status = pj_sem_create(pool, "client", 0, 1, &client->test_complete);
499 if (status != PJ_SUCCESS) {
500 destroy_client_server();
501 return -205;
502 }
503
504 /* Create client socket */
505 status = pj_sock_socket(GET_AF(use_ipv6), pj_SOCK_DGRAM(), 0, &client->sock);
506 if (status != PJ_SUCCESS) {
507 destroy_client_server();
508 return -210;
509 }
510
511 /* Bind client socket */
512 pj_sockaddr_init(GET_AF(use_ipv6), &addr, NULL, 0);
513 status = pj_sock_bind(client->sock, &addr, pj_sockaddr_get_len(&addr));
514 if (status != PJ_SUCCESS) {
515 destroy_client_server();
516 return -220;
517 }
518
519 /* Create client thread */
520 status = pj_thread_create(pool, "client", &client_thread, NULL, 0, 0, &client->thread);
521 if (status != PJ_SUCCESS) {
522 destroy_client_server();
523 return -230;
524 }
525
526 /* Initialize credential */
527 pj_bzero(&cred, sizeof(cred));
528 cred.type = PJ_STUN_AUTH_CRED_STATIC;
529 if (realm) cred.data.static_cred.realm = pj_str((char*)realm);
530 if (username) cred.data.static_cred.username = pj_str((char*)username);
531 if (nonce) cred.data.static_cred.nonce = pj_str((char*)nonce);
532 if (password) cred.data.static_cred.data = pj_str((char*)password);
533 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
534 status = pj_stun_session_set_credential(client->sess, client_auth_type, &cred);
535 if (status != PJ_SUCCESS) {
536 destroy_client_server();
537 return -240;
538 }
539
540 /* Create the server */
541 status = create_std_server(server_auth_type, server_responding, use_ipv6);
542 if (status != 0) {
543 destroy_client_server();
544 return status;
545 }
546
547 /* Create request */
548 status = pj_stun_session_create_req(client->sess, PJ_STUN_BINDING_REQUEST,
549 PJ_STUN_MAGIC, NULL, &tdata);
550 if (status != PJ_SUCCESS) {
551 destroy_client_server();
552 return -250;
553 }
554
555 /* Add our own attributes if client authentication is set to none */
556 if (client_auth_type == PJ_STUN_AUTH_NONE) {
557 pj_str_t tmp;
558 if (realm)
559 pj_stun_msg_add_string_attr(tdata->pool, tdata->msg, PJ_STUN_ATTR_REALM, pj_cstr(&tmp, realm));
560 if (username)
561 pj_stun_msg_add_string_attr(tdata->pool, tdata->msg, PJ_STUN_ATTR_USERNAME, pj_cstr(&tmp, username));
562 if (nonce)
563 pj_stun_msg_add_string_attr(tdata->pool, tdata->msg, PJ_STUN_ATTR_NONCE, pj_cstr(&tmp, nonce));
564 if (password) {
565 // ignored
566 }
567 if (dummy_mi) {
568 pj_stun_msgint_attr *mi;
569
570 pj_stun_msgint_attr_create(tdata->pool, &mi);
571 pj_stun_msg_add_attr(tdata->msg, &mi->hdr);
572 }
573
574 }
575
576 /* Send the request */
577 status = pj_stun_session_send_msg(client->sess, NULL, PJ_FALSE, PJ_TRUE, &server->addr,
578 pj_sockaddr_get_len(&server->addr), tdata);
579 if (status != PJ_SUCCESS) {
580 destroy_client_server();
581 return -270;
582 }
583
584 /* Wait until test complete */
585 pj_sem_wait(client->test_complete);
586
587
588 /* Verify response */
589 if (expected_error) {
590 if (expected_code != client->response_status) {
591 char e1[PJ_ERR_MSG_SIZE], e2[PJ_ERR_MSG_SIZE];
592
593 pj_strerror(expected_code, e1, sizeof(e1));
594 pj_strerror(client->response_status, e2, sizeof(e2));
595
596 PJ_LOG(3,(THIS_FILE, " err: expecting %d (%s) but got %d (%s) response",
597 expected_code, e1, client->response_status, e2));
598 rc = -500;
599 }
600
601 } else {
602 int res_code = 0;
603 pj_stun_realm_attr *arealm;
604 pj_stun_nonce_attr *anonce;
605
606 if (client->response_status != 0) {
607 PJ_LOG(3,(THIS_FILE, " err: expecting successful operation but got error %d",
608 client->response_status));
609 rc = -600;
610 goto done;
611 }
612
613 if (PJ_STUN_IS_ERROR_RESPONSE(client->response->hdr.type)) {
614 pj_stun_errcode_attr *aerr = NULL;
615
616 aerr = (pj_stun_errcode_attr*)
617 pj_stun_msg_find_attr(client->response,
618 PJ_STUN_ATTR_ERROR_CODE, 0);
619 if (aerr == NULL) {
620 PJ_LOG(3,(THIS_FILE, " err: received error response without ERROR-CODE"));
621 rc = -610;
622 goto done;
623 }
624
625 res_code = aerr->err_code;
626 } else {
627 res_code = 0;
628 }
629
630 /* Check that code matches */
631 if (expected_code != res_code) {
632 PJ_LOG(3,(THIS_FILE, " err: expecting response code %d but got %d",
633 expected_code, res_code));
634 rc = -620;
635 goto done;
636 }
637
638 /* Find REALM and NONCE attributes */
639 arealm = (pj_stun_realm_attr*)
640 pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_REALM, 0);
641 anonce = (pj_stun_nonce_attr*)
642 pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_NONCE, 0);
643
644 if (expected_realm) {
645 if (arealm == NULL) {
646 PJ_LOG(3,(THIS_FILE, " err: expecting REALM in esponse"));
647 rc = -630;
648 goto done;
649 }
650 if (pj_strcmp2(&arealm->value, expected_realm)!=0) {
651 PJ_LOG(3,(THIS_FILE, " err: REALM mismatch in response"));
652 rc = -640;
653 goto done;
654 }
655 } else {
656 if (arealm != NULL) {
657 PJ_LOG(3,(THIS_FILE, " err: non expecting REALM in response"));
658 rc = -650;
659 goto done;
660 }
661 }
662
663 if (expected_nonce) {
664 if (anonce == NULL) {
665 PJ_LOG(3,(THIS_FILE, " err: expecting NONCE in esponse"));
666 rc = -660;
667 goto done;
668 }
669 if (pj_strcmp2(&anonce->value, expected_nonce)!=0) {
670 PJ_LOG(3,(THIS_FILE, " err: NONCE mismatch in response"));
671 rc = -670;
672 goto done;
673 }
674 } else {
675 if (anonce != NULL) {
676 PJ_LOG(3,(THIS_FILE, " err: non expecting NONCE in response"));
677 rc = -680;
678 goto done;
679 }
680 }
681 }
682
683 /* Our tests are okay so far. Let caller do some more tests if
684 * it wants to.
685 */
686 if (rc==0 && more_check) {
687 rc = (*more_check)();
688 }
689
690
691 done:
692 destroy_client_server();
693
694 /* If IPv6 is enabled, test again for IPv4. */
695 if ((rc == 0) && use_ipv6) {
696 rc = run_client_test(title,
697 server_responding,
698 server_auth_type,
699 client_auth_type,
700 realm,
701 username,
702 nonce,
703 password,
704 dummy_mi,
705 0,
706 expected_error,
707 expected_code,
708 expected_realm,
709 expected_nonce,
710 more_check);
711 }
712
713 return rc;
714 }
715
716
717 //////////////////////////////////////////////////////////////////////////////////////////
718 //
719 // More verification
720 //
721
722 /* Retransmission test */
723 static int retransmit_check(void)
724 {
725
726 if (server->recv_count != PJ_STUN_MAX_TRANSMIT_COUNT) {
727 PJ_LOG(3,("", " expecting %d retransmissions, got %d",
728 PJ_STUN_MAX_TRANSMIT_COUNT, server->recv_count));
729 return -700;
730 }
731 if (client->recv_count != 0)
732 return -710;
733
734 return 0;
735 }
736
737 static int long_term_check1(void)
738 {
739 /* SHOULD NOT contain USERNAME or MESSAGE-INTEGRITY */
740 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_USERNAME, 0))
741 return -800;
742 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0))
743 return -800;
744
745 return 0;
746 }
747
748 static int long_term_check2(void)
749 {
750 /* response SHOULD NOT include a USERNAME, NONCE, REALM or
751 * MESSAGE-INTEGRITY attribute.
752 */
753 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_USERNAME, 0))
754 return -900;
755 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_NONCE, 0))
756 return -910;
757 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_REALM, 0))
758 return -920;
759 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0))
760 return -930;
761
762 return 0;
763 }
764
765 static int long_term_check3(void)
766 {
767 /* response SHOULD NOT include a USERNAME, NONCE, and REALM */
768 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_USERNAME, 0))
769 return -1000;
770 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_NONCE, 0))
771 return -1010;
772 if (pj_stun_msg_find_attr(client->response, PJ_STUN_ATTR_REALM, 0))
773 return -1020;
774
775 return 0;
776 }
777
778 //////////////////////////////////////////////////////////////////////////////////////////
779 //
780 // TEST MAIN
781 //
782
783
784 int sess_auth_test(void)
785 {
786 pj_pool_t *pool;
787 int rc;
788
789 PJ_LOG(3,(THIS_FILE, " STUN session authentication test"));
790
791 /* Init STUN config */
792 pj_stun_config_init(&stun_cfg, mem, 0, NULL, NULL);
793
794 /* Create pool and timer heap */
795 pool = pj_pool_create(mem, "authtest", 200, 200, NULL);
796 if (pj_timer_heap_create(pool, 20, &stun_cfg.timer_heap)) {
797 pj_pool_release(pool);
798 return -5;
799 }
800
801 /* Basic retransmission test */
802 rc = run_client_test("Retransmission", // title
803 PJ_FALSE, // server responding
804 PJ_STUN_AUTH_NONE, // server auth
805 PJ_STUN_AUTH_NONE, // client auth
806 NULL, // realm
807 NULL, // username
808 NULL, // nonce
809 NULL, // password
810 PJ_FALSE, // dummy MI
811 USE_IPV6, // use IPv6
812 PJ_TRUE, // expected error
813 PJNATH_ESTUNTIMEDOUT,// expected code
814 NULL, // expected realm
815 NULL, // expected nonce
816 &retransmit_check // more check
817 );
818 if (rc != 0) {
819 goto done;
820 }
821
822 /*
823 * Short term credential.
824 * draft-ietf-behave-rfc3489bis-15#section-10.1.2
825 */
826
827 /*
828 * If the message does not contain both a MESSAGE-INTEGRITY and a
829 * USERNAME attribute, If the message is a request, the server MUST
830 * reject the request with an error response. This response MUST
831 * use an error code of 400 (Bad Request).
832 */
833 rc = run_client_test("Missing MESSAGE-INTEGRITY (short term)", // title
834 PJ_TRUE, // server responding
835 PJ_STUN_AUTH_SHORT_TERM, // server auth
836 PJ_STUN_AUTH_NONE, // client auth
837 NULL, // realm
838 NULL, // username
839 NULL, // nonce
840 NULL, // password
841 PJ_FALSE, // dummy MI
842 USE_IPV6, // use IPv6
843 PJ_TRUE, // expected error
844 PJ_STATUS_FROM_STUN_CODE(400),// expected code
845 NULL, // expected realm
846 NULL, // expected nonce
847 NULL // more check
848 );
849 if (rc != 0) {
850 goto done;
851 }
852
853 /* If the USERNAME does not contain a username value currently valid
854 * within the server: If the message is a request, the server MUST
855 * reject the request with an error response. This response MUST use
856 * an error code of 401 (Unauthorized).
857 */
858 rc = run_client_test("USERNAME mismatch (short term)", // title
859 PJ_TRUE, // server responding
860 PJ_STUN_AUTH_SHORT_TERM, // server auth
861 PJ_STUN_AUTH_SHORT_TERM, // client auth
862 NULL, // realm
863 "anotheruser", // username
864 NULL, // nonce
865 "anotherpass", // password
866 PJ_FALSE, // dummy MI
867 USE_IPV6, // use IPv6
868 PJ_TRUE, // expected error
869 PJ_STATUS_FROM_STUN_CODE(401),// expected code
870 NULL, // expected realm
871 NULL, // expected nonce
872 NULL // more check
873 );
874 if (rc != 0) {
875 goto done;
876 }
877
878 /* Using the password associated with the username, compute the value
879 * for the message-integrity as described in Section 15.4. If the
880 * resulting value does not match the contents of the MESSAGE-
881 * INTEGRITY attribute:
882 *
883 * - If the message is a request, the server MUST reject the request
884 * with an error response. This response MUST use an error code
885 * of 401 (Unauthorized).
886 */
887 rc = run_client_test("MESSAGE-INTEGRITY mismatch (short term)", // title
888 PJ_TRUE, // server responding
889 PJ_STUN_AUTH_SHORT_TERM, // server auth
890 PJ_STUN_AUTH_SHORT_TERM, // client auth
891 NULL, // realm
892 USERNAME, // username
893 NULL, // nonce
894 "anotherpass", // password
895 PJ_FALSE, // dummy MI
896 USE_IPV6, // use IPv6
897 PJ_TRUE, // expected error
898 PJ_STATUS_FROM_STUN_CODE(401),// expected code
899 NULL, // expected realm
900 NULL, // expected nonce
901 NULL // more check
902 );
903 if (rc != 0) {
904 goto done;
905 }
906
907 /* USERNAME is not present, server must respond with 400 (Bad
908 * Request).
909 */
910 rc = run_client_test("Missing USERNAME (short term)",// title
911 PJ_TRUE, // server responding
912 PJ_STUN_AUTH_SHORT_TERM, // server auth
913 PJ_STUN_AUTH_NONE, // client auth
914 NULL, // realm
915 NULL, // username
916 NULL, // nonce
917 NULL, // password
918 PJ_TRUE, // dummy MI
919 USE_IPV6, // use IPv6
920 PJ_TRUE, // expected error
921 PJ_STATUS_FROM_STUN_CODE(400), // expected code
922 NULL, // expected realm
923 NULL, // expected nonce
924 NULL // more check
925 );
926 if (rc != 0) {
927 goto done;
928 }
929
930 /* Successful short term authentication */
931 rc = run_client_test("Successful scenario (short term)", // title
932 PJ_TRUE, // server responding
933 PJ_STUN_AUTH_SHORT_TERM, // server auth
934 PJ_STUN_AUTH_SHORT_TERM, // client auth
935 NULL, // realm
936 USERNAME, // username
937 NULL, // nonce
938 PASSWORD, // password
939 PJ_FALSE, // dummy MI
940 USE_IPV6, // use IPv6
941 PJ_FALSE, // expected error
942 PJ_SUCCESS, // expected code
943 NULL, // expected realm
944 NULL, // expected nonce
945 NULL // more check
946 );
947 if (rc != 0) {
948 goto done;
949 }
950
951 /*
952 * (our own) Extended tests for long term credential
953 */
954
955 /* When server wants to use short term credential, but request has
956 * REALM, reject with .... 401 ???
957 */
958 rc = run_client_test("Unwanted REALM (short term)", // title
959 PJ_TRUE, // server responding
960 PJ_STUN_AUTH_SHORT_TERM, // server auth
961 PJ_STUN_AUTH_NONE, // client auth
962 REALM, // realm
963 USERNAME, // username
964 NULL, // nonce
965 PASSWORD, // password
966 PJ_TRUE, // dummy MI
967 USE_IPV6, // use IPv6
968 PJ_TRUE, // expected error
969 PJ_STATUS_FROM_STUN_CODE(401), // expected code
970 NULL, // expected realm
971 NULL, // expected nonce
972 &long_term_check2 // more check
973 );
974 if (rc != 0) {
975 goto done;
976 }
977
978
979 /*
980 * Long term credential.
981 * draft-ietf-behave-rfc3489bis-15#section-10.2.2
982 */
983
984 /* If the message does not contain a MESSAGE-INTEGRITY attribute, the
985 * server MUST generate an error response with an error code of 401
986 * (Unauthorized). This response MUST include a REALM value. It is
987 * RECOMMENDED that the REALM value be the domain name of the
988 * provider of the STUN server. The response MUST include a NONCE,
989 * selected by the server. The response SHOULD NOT contain a
990 * USERNAME or MESSAGE-INTEGRITY attribute.
991 */
992 rc = run_client_test("Missing M-I (long term)", // title
993 PJ_TRUE, // server responding
994 PJ_STUN_AUTH_LONG_TERM, // server auth
995 PJ_STUN_AUTH_NONE, // client auth
996 NULL, // client realm
997 NULL, // client username
998 NULL, // client nonce
999 NULL, // client password
1000 PJ_FALSE, // client dummy MI
1001 USE_IPV6, // use IPv6
1002 PJ_TRUE, // expected error
1003 PJ_STATUS_FROM_STUN_CODE(401), // expected code
1004 REALM, // expected realm
1005 NONCE, // expected nonce
1006 &long_term_check1 // more check
1007 );
1008 if (rc != 0) {
1009 goto done;
1010 }
1011
1012 /* If the message contains a MESSAGE-INTEGRITY attribute, but is
1013 * missing the USERNAME, REALM or NONCE attributes, the server MUST
1014 * generate an error response with an error code of 400 (Bad
1015 * Request). This response SHOULD NOT include a USERNAME, NONCE,
1016 * REALM or MESSAGE-INTEGRITY attribute.
1017 */
1018 /* Missing USERNAME */
1019 rc = run_client_test("Missing USERNAME (long term)", // title
1020 PJ_TRUE, // server responding
1021 PJ_STUN_AUTH_LONG_TERM, // server auth
1022 PJ_STUN_AUTH_NONE, // client auth
1023 REALM, // client realm
1024 NULL, // client username
1025 NONCE, // client nonce
1026 PASSWORD, // client password
1027 PJ_TRUE, // client dummy MI
1028 USE_IPV6, // use IPv6
1029 PJ_TRUE, // expected error
1030 PJ_STATUS_FROM_STUN_CODE(400), // expected code
1031 NULL, // expected realm
1032 NULL, // expected nonce
1033 &long_term_check2 // more check
1034 );
1035 if (rc != 0) {
1036 goto done;
1037 }
1038
1039 /* Missing REALM */
1040 rc = run_client_test("Missing REALM (long term)", // title
1041 PJ_TRUE, // server responding
1042 PJ_STUN_AUTH_LONG_TERM, // server auth
1043 PJ_STUN_AUTH_NONE, // client auth
1044 NULL, // client realm
1045 USERNAME, // client username
1046 NONCE, // client nonce
1047 PASSWORD, // client password
1048 PJ_TRUE, // client dummy MI
1049 USE_IPV6, // use IPv6
1050 PJ_TRUE, // expected error
1051 PJ_STATUS_FROM_STUN_CODE(400), // expected code
1052 NULL, // expected realm
1053 NULL, // expected nonce
1054 &long_term_check2 // more check
1055 );
1056 if (rc != 0) {
1057 goto done;
1058 }
1059
1060 /* Missing NONCE */
1061 rc = run_client_test("Missing NONCE (long term)", // title
1062 PJ_TRUE, // server responding
1063 PJ_STUN_AUTH_LONG_TERM, // server auth
1064 PJ_STUN_AUTH_NONE, // client auth
1065 REALM, // client realm
1066 USERNAME, // client username
1067 NULL, // client nonce
1068 PASSWORD, // client password
1069 PJ_TRUE, // client dummy MI
1070 USE_IPV6, // use IPv6
1071 PJ_TRUE, // expected error
1072 PJ_STATUS_FROM_STUN_CODE(400), // expected code
1073 NULL, // expected realm
1074 NULL, // expected nonce
1075 &long_term_check2 // more check
1076 );
1077 if (rc != 0) {
1078 goto done;
1079 }
1080
1081 /* If the NONCE is no longer valid, the server MUST generate an error
1082 * response with an error code of 438 (Stale Nonce). This response
1083 * MUST include a NONCE and REALM attribute and SHOULD NOT incude the
1084 * USERNAME or MESSAGE-INTEGRITY attribute. Servers can invalidate
1085 * nonces in order to provide additional security. See Section 4.3
1086 * of [RFC2617] for guidelines.
1087 */
1088 // how??
1089
1090 /* If the username in the USERNAME attribute is not valid, the server
1091 * MUST generate an error response with an error code of 401
1092 * (Unauthorized). This response MUST include a REALM value. It is
1093 * RECOMMENDED that the REALM value be the domain name of the
1094 * provider of the STUN server. The response MUST include a NONCE,
1095 * selected by the server. The response SHOULD NOT contain a
1096 * USERNAME or MESSAGE-INTEGRITY attribute.
1097 */
1098 rc = run_client_test("Invalid username (long term)", // title
1099 PJ_TRUE, // server responding
1100 PJ_STUN_AUTH_LONG_TERM, // server auth
1101 PJ_STUN_AUTH_LONG_TERM, // client auth
1102 REALM, // client realm
1103 "anotheruser", // client username
1104 "a nonce", // client nonce
1105 "somepassword", // client password
1106 PJ_FALSE, // client dummy MI
1107 USE_IPV6, // use IPv6
1108 PJ_TRUE, // expected error
1109 PJ_STATUS_FROM_STUN_CODE(401), // expected code
1110 REALM, // expected realm
1111 NONCE, // expected nonce
1112 &long_term_check1 // more check
1113 );
1114 if (rc != 0) {
1115 goto done;
1116 }
1117
1118 /* Successful long term authentication */
1119 rc = run_client_test("Successful scenario (long term)", // title
1120 PJ_TRUE, // server responding
1121 PJ_STUN_AUTH_LONG_TERM, // server auth
1122 PJ_STUN_AUTH_LONG_TERM, // client auth
1123 REALM, // client realm
1124 USERNAME, // client username
1125 "anothernonce", // client nonce
1126 PASSWORD, // client password
1127 PJ_FALSE, // client dummy MI
1128 USE_IPV6, // use IPv6
1129 PJ_FALSE, // expected error
1130 0, // expected code
1131 NULL, // expected realm
1132 NULL, // expected nonce
1133 &long_term_check3 // more check
1134 );
1135 if (rc != 0) {
1136 goto done;
1137 }
1138
1139 /*
1140 * (our own) Extended tests for long term credential
1141 */
1142
1143 /* If REALM doesn't match, server must respond with 401
1144 */
1145 #if 0
1146 // STUN session now will just use the realm sent in the
1147 // response, so this test will fail because it will
1148 // authenticate successfully.
1149
1150 rc = run_client_test("Invalid REALM (long term)", // title
1151 PJ_TRUE, // server responding
1152 PJ_STUN_AUTH_LONG_TERM, // server auth
1153 PJ_STUN_AUTH_LONG_TERM, // client auth
1154 "anotherrealm", // client realm
1155 USERNAME, // client username
1156 NONCE, // client nonce
1157 PASSWORD, // client password
1158 PJ_FALSE, // client dummy MI
1159 USE_IPV6, // use IPv6
1160 PJ_TRUE, // expected error
1161 PJ_STATUS_FROM_STUN_CODE(401), // expected code
1162 REALM, // expected realm
1163 NONCE, // expected nonce
1164 &long_term_check1 // more check
1165 );
1166 if (rc != 0) {
1167 goto done;
1168 }
1169 #endif
1170
1171 /* Invalid HMAC */
1172
1173 /* Valid static short term, without NONCE */
1174
1175 /* Valid static short term, WITH NONCE */
1176
1177 /* Valid static long term (with NONCE */
1178
1179 /* Valid dynamic short term (without NONCE) */
1180
1181 /* Valid dynamic short term (with NONCE) */
1182
1183 /* Valid dynamic long term (with NONCE) */
1184
1185
1186 done:
1187 pj_timer_heap_destroy(stun_cfg.timer_heap);
1188 pj_pool_release(pool);
1189 return rc;
1190 }