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