| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 30 |
static pj_stun_config stun_cfg; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 192 |
server->recv_count++; |
| 193 |
|
| 194 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 294 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 391 |
pj_timer_heap_poll(stun_cfg.timer_heap, NULL); |
| 392 |
|
| 393 |
|
| 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 |
|
| 414 |
client->recv_count++; |
| 415 |
|
| 416 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 585 |
pj_sem_wait(client->test_complete); |
| 586 |
|
| 587 |
|
| 588 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 684 |
|
| 685 |
|
| 686 |
if (rc==0 && more_check) { |
| 687 |
rc = (*more_check)(); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
done: |
| 692 |
destroy_client_server(); |
| 693 |
|
| 694 |
|
| 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 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 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 |
|
| 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 |
|
| 751 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 792 |
pj_stun_config_init(&stun_cfg, mem, 0, NULL, NULL); |
| 793 |
|
| 794 |
|
| 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 |
|
| 802 |
rc = run_client_test("Retransmission", |
| 803 |
PJ_FALSE, |
| 804 |
PJ_STUN_AUTH_NONE, |
| 805 |
PJ_STUN_AUTH_NONE, |
| 806 |
NULL, |
| 807 |
NULL, |
| 808 |
NULL, |
| 809 |
NULL, |
| 810 |
PJ_FALSE, |
| 811 |
USE_IPV6, |
| 812 |
PJ_TRUE, |
| 813 |
PJNATH_ESTUNTIMEDOUT, |
| 814 |
NULL, |
| 815 |
NULL, |
| 816 |
&retransmit_check |
| 817 |
); |
| 818 |
if (rc != 0) { |
| 819 |
goto done; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
rc = run_client_test("Missing MESSAGE-INTEGRITY (short term)", |
| 834 |
PJ_TRUE, |
| 835 |
PJ_STUN_AUTH_SHORT_TERM, |
| 836 |
PJ_STUN_AUTH_NONE, |
| 837 |
NULL, |
| 838 |
NULL, |
| 839 |
NULL, |
| 840 |
NULL, |
| 841 |
PJ_FALSE, |
| 842 |
USE_IPV6, |
| 843 |
PJ_TRUE, |
| 844 |
PJ_STATUS_FROM_STUN_CODE(400), |
| 845 |
NULL, |
| 846 |
NULL, |
| 847 |
NULL |
| 848 |
); |
| 849 |
if (rc != 0) { |
| 850 |
goto done; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
rc = run_client_test("USERNAME mismatch (short term)", |
| 859 |
PJ_TRUE, |
| 860 |
PJ_STUN_AUTH_SHORT_TERM, |
| 861 |
PJ_STUN_AUTH_SHORT_TERM, |
| 862 |
NULL, |
| 863 |
"anotheruser", |
| 864 |
NULL, |
| 865 |
"anotherpass", |
| 866 |
PJ_FALSE, |
| 867 |
USE_IPV6, |
| 868 |
PJ_TRUE, |
| 869 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 870 |
NULL, |
| 871 |
NULL, |
| 872 |
NULL |
| 873 |
); |
| 874 |
if (rc != 0) { |
| 875 |
goto done; |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
rc = run_client_test("MESSAGE-INTEGRITY mismatch (short term)", |
| 888 |
PJ_TRUE, |
| 889 |
PJ_STUN_AUTH_SHORT_TERM, |
| 890 |
PJ_STUN_AUTH_SHORT_TERM, |
| 891 |
NULL, |
| 892 |
USERNAME, |
| 893 |
NULL, |
| 894 |
"anotherpass", |
| 895 |
PJ_FALSE, |
| 896 |
USE_IPV6, |
| 897 |
PJ_TRUE, |
| 898 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 899 |
NULL, |
| 900 |
NULL, |
| 901 |
NULL |
| 902 |
); |
| 903 |
if (rc != 0) { |
| 904 |
goto done; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
rc = run_client_test("Missing USERNAME (short term)", |
| 911 |
PJ_TRUE, |
| 912 |
PJ_STUN_AUTH_SHORT_TERM, |
| 913 |
PJ_STUN_AUTH_NONE, |
| 914 |
NULL, |
| 915 |
NULL, |
| 916 |
NULL, |
| 917 |
NULL, |
| 918 |
PJ_TRUE, |
| 919 |
USE_IPV6, |
| 920 |
PJ_TRUE, |
| 921 |
PJ_STATUS_FROM_STUN_CODE(400), |
| 922 |
NULL, |
| 923 |
NULL, |
| 924 |
NULL |
| 925 |
); |
| 926 |
if (rc != 0) { |
| 927 |
goto done; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
rc = run_client_test("Successful scenario (short term)", |
| 932 |
PJ_TRUE, |
| 933 |
PJ_STUN_AUTH_SHORT_TERM, |
| 934 |
PJ_STUN_AUTH_SHORT_TERM, |
| 935 |
NULL, |
| 936 |
USERNAME, |
| 937 |
NULL, |
| 938 |
PASSWORD, |
| 939 |
PJ_FALSE, |
| 940 |
USE_IPV6, |
| 941 |
PJ_FALSE, |
| 942 |
PJ_SUCCESS, |
| 943 |
NULL, |
| 944 |
NULL, |
| 945 |
NULL |
| 946 |
); |
| 947 |
if (rc != 0) { |
| 948 |
goto done; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
rc = run_client_test("Unwanted REALM (short term)", |
| 959 |
PJ_TRUE, |
| 960 |
PJ_STUN_AUTH_SHORT_TERM, |
| 961 |
PJ_STUN_AUTH_NONE, |
| 962 |
REALM, |
| 963 |
USERNAME, |
| 964 |
NULL, |
| 965 |
PASSWORD, |
| 966 |
PJ_TRUE, |
| 967 |
USE_IPV6, |
| 968 |
PJ_TRUE, |
| 969 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 970 |
NULL, |
| 971 |
NULL, |
| 972 |
&long_term_check2 |
| 973 |
); |
| 974 |
if (rc != 0) { |
| 975 |
goto done; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
rc = run_client_test("Missing M-I (long term)", |
| 993 |
PJ_TRUE, |
| 994 |
PJ_STUN_AUTH_LONG_TERM, |
| 995 |
PJ_STUN_AUTH_NONE, |
| 996 |
NULL, |
| 997 |
NULL, |
| 998 |
NULL, |
| 999 |
NULL, |
| 1000 |
PJ_FALSE, |
| 1001 |
USE_IPV6, |
| 1002 |
PJ_TRUE, |
| 1003 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 1004 |
REALM, |
| 1005 |
NONCE, |
| 1006 |
&long_term_check1 |
| 1007 |
); |
| 1008 |
if (rc != 0) { |
| 1009 |
goto done; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
rc = run_client_test("Missing USERNAME (long term)", |
| 1020 |
PJ_TRUE, |
| 1021 |
PJ_STUN_AUTH_LONG_TERM, |
| 1022 |
PJ_STUN_AUTH_NONE, |
| 1023 |
REALM, |
| 1024 |
NULL, |
| 1025 |
NONCE, |
| 1026 |
PASSWORD, |
| 1027 |
PJ_TRUE, |
| 1028 |
USE_IPV6, |
| 1029 |
PJ_TRUE, |
| 1030 |
PJ_STATUS_FROM_STUN_CODE(400), |
| 1031 |
NULL, |
| 1032 |
NULL, |
| 1033 |
&long_term_check2 |
| 1034 |
); |
| 1035 |
if (rc != 0) { |
| 1036 |
goto done; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
rc = run_client_test("Missing REALM (long term)", |
| 1041 |
PJ_TRUE, |
| 1042 |
PJ_STUN_AUTH_LONG_TERM, |
| 1043 |
PJ_STUN_AUTH_NONE, |
| 1044 |
NULL, |
| 1045 |
USERNAME, |
| 1046 |
NONCE, |
| 1047 |
PASSWORD, |
| 1048 |
PJ_TRUE, |
| 1049 |
USE_IPV6, |
| 1050 |
PJ_TRUE, |
| 1051 |
PJ_STATUS_FROM_STUN_CODE(400), |
| 1052 |
NULL, |
| 1053 |
NULL, |
| 1054 |
&long_term_check2 |
| 1055 |
); |
| 1056 |
if (rc != 0) { |
| 1057 |
goto done; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
rc = run_client_test("Missing NONCE (long term)", |
| 1062 |
PJ_TRUE, |
| 1063 |
PJ_STUN_AUTH_LONG_TERM, |
| 1064 |
PJ_STUN_AUTH_NONE, |
| 1065 |
REALM, |
| 1066 |
USERNAME, |
| 1067 |
NULL, |
| 1068 |
PASSWORD, |
| 1069 |
PJ_TRUE, |
| 1070 |
USE_IPV6, |
| 1071 |
PJ_TRUE, |
| 1072 |
PJ_STATUS_FROM_STUN_CODE(400), |
| 1073 |
NULL, |
| 1074 |
NULL, |
| 1075 |
&long_term_check2 |
| 1076 |
); |
| 1077 |
if (rc != 0) { |
| 1078 |
goto done; |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
rc = run_client_test("Invalid username (long term)", |
| 1099 |
PJ_TRUE, |
| 1100 |
PJ_STUN_AUTH_LONG_TERM, |
| 1101 |
PJ_STUN_AUTH_LONG_TERM, |
| 1102 |
REALM, |
| 1103 |
"anotheruser", |
| 1104 |
"a nonce", |
| 1105 |
"somepassword", |
| 1106 |
PJ_FALSE, |
| 1107 |
USE_IPV6, |
| 1108 |
PJ_TRUE, |
| 1109 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 1110 |
REALM, |
| 1111 |
NONCE, |
| 1112 |
&long_term_check1 |
| 1113 |
); |
| 1114 |
if (rc != 0) { |
| 1115 |
goto done; |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
rc = run_client_test("Successful scenario (long term)", |
| 1120 |
PJ_TRUE, |
| 1121 |
PJ_STUN_AUTH_LONG_TERM, |
| 1122 |
PJ_STUN_AUTH_LONG_TERM, |
| 1123 |
REALM, |
| 1124 |
USERNAME, |
| 1125 |
"anothernonce", |
| 1126 |
PASSWORD, |
| 1127 |
PJ_FALSE, |
| 1128 |
USE_IPV6, |
| 1129 |
PJ_FALSE, |
| 1130 |
0, |
| 1131 |
NULL, |
| 1132 |
NULL, |
| 1133 |
&long_term_check3 |
| 1134 |
); |
| 1135 |
if (rc != 0) { |
| 1136 |
goto done; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
#if 0 |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
rc = run_client_test("Invalid REALM (long term)", |
| 1151 |
PJ_TRUE, |
| 1152 |
PJ_STUN_AUTH_LONG_TERM, |
| 1153 |
PJ_STUN_AUTH_LONG_TERM, |
| 1154 |
"anotherrealm", |
| 1155 |
USERNAME, |
| 1156 |
NONCE, |
| 1157 |
PASSWORD, |
| 1158 |
PJ_FALSE, |
| 1159 |
USE_IPV6, |
| 1160 |
PJ_TRUE, |
| 1161 |
PJ_STATUS_FROM_STUN_CODE(401), |
| 1162 |
REALM, |
| 1163 |
NONCE, |
| 1164 |
&long_term_check1 |
| 1165 |
); |
| 1166 |
if (rc != 0) { |
| 1167 |
goto done; |
| 1168 |
} |
| 1169 |
#endif |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
done: |
| 1187 |
pj_timer_heap_destroy(stun_cfg.timer_heap); |
| 1188 |
pj_pool_release(pool); |
| 1189 |
return rc; |
| 1190 |
} |