| 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 "stun.c" |
| 22 |
|
| 23 |
static pj_stun_msg* create1(pj_pool_t*); |
| 24 |
static int verify1(pj_stun_msg*); |
| 25 |
static int verify2(pj_stun_msg*); |
| 26 |
static int verify5(pj_stun_msg*); |
| 27 |
|
| 28 |
static struct test |
| 29 |
{ |
| 30 |
const char *title; |
| 31 |
char *pdu; |
| 32 |
unsigned pdu_len; |
| 33 |
pj_stun_msg* (*create)(pj_pool_t*); |
| 34 |
pj_status_t expected_status; |
| 35 |
int (*verify)(pj_stun_msg*); |
| 36 |
} tests[] = |
| 37 |
{ |
| 38 |
{ |
| 39 |
"Invalid message type", |
| 40 |
"\x11\x01\x00\x00\x21\x12\xa4\x42" |
| 41 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", |
| 42 |
20, |
| 43 |
NULL, |
| 44 |
PJNATH_EINSTUNMSGTYPE, |
| 45 |
NULL |
| 46 |
}, |
| 47 |
{ |
| 48 |
"Short message (1) (partial header)", |
| 49 |
"\x00\x01", |
| 50 |
2, |
| 51 |
NULL, |
| 52 |
PJNATH_EINSTUNMSGLEN, |
| 53 |
NULL |
| 54 |
}, |
| 55 |
{ |
| 56 |
"Short message (2) (partial header)", |
| 57 |
"\x00\x01\x00\x00\x21\x12\xa4\x42" |
| 58 |
"\x00\x00\x00\x00\x00\x00\x00\x00", |
| 59 |
16, |
| 60 |
NULL, |
| 61 |
PJNATH_EINSTUNMSGLEN, |
| 62 |
NULL |
| 63 |
}, |
| 64 |
{ |
| 65 |
"Short message (3), (missing attribute)", |
| 66 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 67 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", |
| 68 |
20, |
| 69 |
NULL, |
| 70 |
PJNATH_EINSTUNMSGLEN, |
| 71 |
NULL |
| 72 |
}, |
| 73 |
{ |
| 74 |
"Short message (4), (partial attribute header)", |
| 75 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 76 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 77 |
"\x80\x28", |
| 78 |
22, |
| 79 |
NULL, |
| 80 |
PJNATH_EINSTUNMSGLEN, |
| 81 |
NULL |
| 82 |
}, |
| 83 |
{ |
| 84 |
"Short message (5), (partial attribute header)", |
| 85 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 86 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 87 |
"\x80\x28\x00", |
| 88 |
23, |
| 89 |
NULL, |
| 90 |
PJNATH_EINSTUNMSGLEN, |
| 91 |
NULL |
| 92 |
}, |
| 93 |
{ |
| 94 |
"Short message (6), (partial attribute header)", |
| 95 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 96 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 97 |
"\x80\x28\x00\x04", |
| 98 |
24, |
| 99 |
NULL, |
| 100 |
PJNATH_EINSTUNMSGLEN, |
| 101 |
NULL |
| 102 |
}, |
| 103 |
{ |
| 104 |
"Short message (7), (partial attribute body)", |
| 105 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 106 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 107 |
"\x80\x28\x00\x04\x00\x00\x00", |
| 108 |
27, |
| 109 |
NULL, |
| 110 |
PJNATH_EINSTUNMSGLEN, |
| 111 |
NULL |
| 112 |
}, |
| 113 |
{ |
| 114 |
"Message length in header is too long", |
| 115 |
"\x00\x01\xff\xff\x21\x12\xa4\x42" |
| 116 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 117 |
"\x80\x28\x00\x04\x00\x00\x00", |
| 118 |
27, |
| 119 |
NULL, |
| 120 |
PJNATH_EINSTUNMSGLEN, |
| 121 |
NULL |
| 122 |
}, |
| 123 |
{ |
| 124 |
"Message length in header is shorter", |
| 125 |
"\x00\x01\x00\x04\x21\x12\xa4\x42" |
| 126 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 127 |
"\x80\x28\x00\x04\x00\x00\x00\x00", |
| 128 |
28, |
| 129 |
NULL, |
| 130 |
PJNATH_EINSTUNMSGLEN, |
| 131 |
NULL |
| 132 |
}, |
| 133 |
{ |
| 134 |
"Invalid magic", |
| 135 |
"\x00\x01\x00\x08\x00\x12\xa4\x42" |
| 136 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 137 |
"\x80\x28\x00\x04\x00\x00\x00\x00", |
| 138 |
28, |
| 139 |
NULL, |
| 140 |
PJ_SUCCESS, |
| 141 |
NULL |
| 142 |
}, |
| 143 |
{ |
| 144 |
"Character beyond message", |
| 145 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 146 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 147 |
"\x80\x28\x00\x04\x00\x00\x00\x00\x0a", |
| 148 |
29, |
| 149 |
NULL, |
| 150 |
PJNATH_EINSTUNMSGLEN, |
| 151 |
NULL |
| 152 |
}, |
| 153 |
{ |
| 154 |
"Respond unknown mandatory attribute with 420 and " |
| 155 |
"UNKNOWN-ATTRIBUTES attribute", |
| 156 |
NULL, |
| 157 |
0, |
| 158 |
&create1, |
| 159 |
0, |
| 160 |
&verify1 |
| 161 |
}, |
| 162 |
{ |
| 163 |
"Unknown but non-mandatory should be okay", |
| 164 |
"\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 165 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 166 |
"\x80\xff\x00\x03\x00\x00\x00\x00", |
| 167 |
28, |
| 168 |
NULL, |
| 169 |
PJ_SUCCESS, |
| 170 |
&verify2 |
| 171 |
}, |
| 172 |
{ |
| 173 |
"String attr length larger than message", |
| 174 |
"\x00\x01\x00\x08\x00\x12\xa4\x42" |
| 175 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 176 |
"\x00\x06\x00\xff\x00\x00\x00\x00", |
| 177 |
28, |
| 178 |
NULL, |
| 179 |
PJNATH_ESTUNINATTRLEN, |
| 180 |
NULL |
| 181 |
}, |
| 182 |
{ |
| 183 |
"Attribute other than FINGERPRINT after MESSAGE-INTEGRITY is allowed", |
| 184 |
"\x00\x01\x00\x20\x21\x12\xa4\x42" |
| 185 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 186 |
"\x00\x08\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 187 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 188 |
"\x80\x24\x00\x04\x00\x00\x00\x00", |
| 189 |
52, |
| 190 |
NULL, |
| 191 |
PJ_SUCCESS, |
| 192 |
NULL |
| 193 |
}, |
| 194 |
{ |
| 195 |
"Attribute between MESSAGE-INTEGRITY and FINGERPRINT is allowed", |
| 196 |
"\x00\x01\x00\x28\x21\x12\xa4\x42" |
| 197 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 198 |
"\x00\x08\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 199 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 200 |
"\x80\x24\x00\x04\x00\x00\x00\x00" |
| 201 |
"\x80\x28\x00\x04\xc7\xde\xdd\x65", |
| 202 |
60, |
| 203 |
NULL, |
| 204 |
PJ_SUCCESS, |
| 205 |
&verify5 |
| 206 |
}, |
| 207 |
{ |
| 208 |
"Attribute past FINGERPRINT is not allowed", |
| 209 |
"\x00\x01\x00\x10\x21\x12\xa4\x42" |
| 210 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 211 |
"\x80\x28\x00\x04\x00\x00\x00\x00" |
| 212 |
"\x80\x24\x00\x04\x00\x00\x00\x00", |
| 213 |
36, |
| 214 |
NULL, |
| 215 |
PJNATH_ESTUNFINGERPOS, |
| 216 |
NULL |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
static const char *err(pj_status_t status) |
| 221 |
{ |
| 222 |
static char errmsg[PJ_ERR_MSG_SIZE]; |
| 223 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 224 |
return errmsg; |
| 225 |
} |
| 226 |
|
| 227 |
static const pj_str_t USERNAME = {"user", 4}; |
| 228 |
static const pj_str_t PASSWORD = {"password", 8}; |
| 229 |
|
| 230 |
static int decode_test(void) |
| 231 |
{ |
| 232 |
unsigned i; |
| 233 |
pj_pool_t *pool; |
| 234 |
int rc = 0; |
| 235 |
|
| 236 |
pool = pj_pool_create(mem, "decode_test", 1024, 1024, NULL); |
| 237 |
|
| 238 |
PJ_LOG(3,(THIS_FILE, " STUN decode test")); |
| 239 |
|
| 240 |
for (i=0; i<PJ_ARRAY_SIZE(tests); ++i) { |
| 241 |
struct test *t = &tests[i]; |
| 242 |
pj_stun_msg *msg, *msg2; |
| 243 |
pj_uint8_t buf[1500]; |
| 244 |
pj_str_t key; |
| 245 |
pj_size_t len; |
| 246 |
pj_status_t status; |
| 247 |
|
| 248 |
PJ_LOG(3,(THIS_FILE, " %s", t->title)); |
| 249 |
|
| 250 |
if (t->pdu) { |
| 251 |
status = pj_stun_msg_decode(pool, (pj_uint8_t*)t->pdu, t->pdu_len, |
| 252 |
PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, |
| 253 |
&msg, NULL, NULL); |
| 254 |
|
| 255 |
|
| 256 |
if (t->expected_status != status) { |
| 257 |
PJ_LOG(1,(THIS_FILE, " expecting status %d, got %d", |
| 258 |
t->expected_status, status)); |
| 259 |
rc = -10; |
| 260 |
goto on_return; |
| 261 |
} |
| 262 |
|
| 263 |
} else { |
| 264 |
msg = t->create(pool); |
| 265 |
status = PJ_SUCCESS; |
| 266 |
} |
| 267 |
|
| 268 |
if (status != PJ_SUCCESS) |
| 269 |
continue; |
| 270 |
|
| 271 |
|
| 272 |
pj_stun_create_key(pool, &key, NULL, &USERNAME, PJ_STUN_PASSWD_PLAIN, &PASSWORD); |
| 273 |
status = pj_stun_msg_encode(msg, buf, sizeof(buf), 0, &key, &len); |
| 274 |
if (status != PJ_SUCCESS) { |
| 275 |
PJ_LOG(1,(THIS_FILE, " encode error: %s", err(status))); |
| 276 |
rc = -40; |
| 277 |
goto on_return; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
status = pj_stun_msg_decode(pool, buf, len, |
| 282 |
PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, |
| 283 |
&msg2, NULL, NULL); |
| 284 |
if (status != PJ_SUCCESS) { |
| 285 |
PJ_LOG(1,(THIS_FILE, " subsequent decoding failed: %s", err(status))); |
| 286 |
rc = -50; |
| 287 |
goto on_return; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
if (t->verify) { |
| 292 |
rc = t->verify(msg); |
| 293 |
if (rc != 0) { |
| 294 |
goto on_return; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
on_return: |
| 300 |
pj_pool_release(pool); |
| 301 |
if (rc == 0) |
| 302 |
PJ_LOG(3,(THIS_FILE, "...success!")); |
| 303 |
return rc; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
static pj_stun_msg* create1(pj_pool_t *pool) |
| 308 |
{ |
| 309 |
char *pdu = "\x00\x01\x00\x08\x21\x12\xa4\x42" |
| 310 |
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 311 |
"\x00\xff\x00\x04\x00\x00\x00\x00"; |
| 312 |
unsigned pdu_len = 28; |
| 313 |
pj_stun_msg *msg, *res; |
| 314 |
pj_status_t status; |
| 315 |
|
| 316 |
status = pj_stun_msg_decode(pool, (pj_uint8_t*)pdu, pdu_len, |
| 317 |
PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, |
| 318 |
&msg, NULL, &res); |
| 319 |
pj_assert(status != PJ_SUCCESS); |
| 320 |
PJ_UNUSED_ARG(status); |
| 321 |
pj_assert(res != NULL); |
| 322 |
|
| 323 |
return res; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
static int verify1(pj_stun_msg *msg) |
| 329 |
{ |
| 330 |
pj_stun_errcode_attr *aerr; |
| 331 |
pj_stun_unknown_attr *aunk; |
| 332 |
|
| 333 |
if (!PJ_STUN_IS_ERROR_RESPONSE(msg->hdr.type)) { |
| 334 |
PJ_LOG(1,(THIS_FILE, " expecting error message")); |
| 335 |
return -100; |
| 336 |
} |
| 337 |
|
| 338 |
aerr = (pj_stun_errcode_attr*) |
| 339 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_ERROR_CODE, 0); |
| 340 |
if (aerr == NULL) { |
| 341 |
PJ_LOG(1,(THIS_FILE, " missing ERROR-CODE attribute")); |
| 342 |
return -110; |
| 343 |
} |
| 344 |
|
| 345 |
if (aerr->err_code != 420) { |
| 346 |
PJ_LOG(1,(THIS_FILE, " expecting 420 error")); |
| 347 |
return -120; |
| 348 |
} |
| 349 |
|
| 350 |
aunk = (pj_stun_unknown_attr*) |
| 351 |
pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_UNKNOWN_ATTRIBUTES, 0); |
| 352 |
if (aunk == NULL) { |
| 353 |
PJ_LOG(1,(THIS_FILE, " missing UNKNOWN-ATTRIBUTE attribute")); |
| 354 |
return -130; |
| 355 |
} |
| 356 |
|
| 357 |
if (aunk->attr_count != 1) { |
| 358 |
PJ_LOG(1,(THIS_FILE, " expecting one unknown attribute")); |
| 359 |
return -140; |
| 360 |
} |
| 361 |
|
| 362 |
if (aunk->attrs[0] != 0xff) { |
| 363 |
PJ_LOG(1,(THIS_FILE, " expecting 0xff as unknown attribute")); |
| 364 |
return -150; |
| 365 |
} |
| 366 |
|
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
static int verify2(pj_stun_msg *msg) |
| 372 |
{ |
| 373 |
pj_stun_binary_attr *bin_attr; |
| 374 |
|
| 375 |
if (msg->attr_count != 1) { |
| 376 |
PJ_LOG(1,(THIS_FILE, " expecting one attribute count")); |
| 377 |
return -200; |
| 378 |
} |
| 379 |
|
| 380 |
bin_attr = (pj_stun_binary_attr*)msg->attr[0]; |
| 381 |
if (bin_attr->hdr.type != 0x80ff) { |
| 382 |
PJ_LOG(1,(THIS_FILE, " expecting attribute type 0x80ff")); |
| 383 |
return -210; |
| 384 |
} |
| 385 |
if (bin_attr->hdr.length != 3) { |
| 386 |
PJ_LOG(1,(THIS_FILE, " expecting attribute length = 4")); |
| 387 |
return -220; |
| 388 |
} |
| 389 |
if (bin_attr->magic != PJ_STUN_MAGIC) { |
| 390 |
PJ_LOG(1,(THIS_FILE, " expecting PJ_STUN_MAGIC for unknown attr")); |
| 391 |
return -230; |
| 392 |
} |
| 393 |
if (bin_attr->length != 3) { |
| 394 |
PJ_LOG(1,(THIS_FILE, " expecting data length 4")); |
| 395 |
return -240; |
| 396 |
} |
| 397 |
|
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static int verify5(pj_stun_msg *msg) |
| 404 |
{ |
| 405 |
if (msg->attr_count != 3) { |
| 406 |
PJ_LOG(1,(THIS_FILE, " expecting 3 attribute count")); |
| 407 |
return -500; |
| 408 |
} |
| 409 |
|
| 410 |
if (msg->attr[0]->type != PJ_STUN_ATTR_MESSAGE_INTEGRITY) { |
| 411 |
PJ_LOG(1,(THIS_FILE, " expecting MESSAGE-INTEGRITY")); |
| 412 |
return -510; |
| 413 |
} |
| 414 |
if (msg->attr[1]->type != PJ_STUN_ATTR_REFRESH_INTERVAL) { |
| 415 |
PJ_LOG(1,(THIS_FILE, " expecting REFRESH-INTERVAL")); |
| 416 |
return -520; |
| 417 |
} |
| 418 |
if (msg->attr[2]->type != PJ_STUN_ATTR_FINGERPRINT) { |
| 419 |
PJ_LOG(1,(THIS_FILE, " expecting FINGERPRINT")); |
| 420 |
return -530; |
| 421 |
} |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
static int decode_verify(void) |
| 428 |
{ |
| 429 |
|
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
typedef struct test_vector test_vector; |
| 438 |
|
| 439 |
static pj_stun_msg* create_msgint1(pj_pool_t *pool, test_vector *v); |
| 440 |
static pj_stun_msg* create_msgint2(pj_pool_t *pool, test_vector *v); |
| 441 |
static pj_stun_msg* create_msgint3(pj_pool_t *pool, test_vector *v); |
| 442 |
|
| 443 |
enum |
| 444 |
{ |
| 445 |
USE_MESSAGE_INTEGRITY = 1, |
| 446 |
USE_FINGERPRINT = 2 |
| 447 |
}; |
| 448 |
|
| 449 |
static struct test_vector |
| 450 |
{ |
| 451 |
unsigned msg_type; |
| 452 |
char *tsx_id; |
| 453 |
char *pdu; |
| 454 |
unsigned pdu_len; |
| 455 |
unsigned options; |
| 456 |
char *username; |
| 457 |
char *password; |
| 458 |
char *realm; |
| 459 |
char *nonce; |
| 460 |
pj_stun_msg* (*create)(pj_pool_t*, test_vector*); |
| 461 |
} test_vectors[] = |
| 462 |
{ |
| 463 |
{ |
| 464 |
PJ_STUN_BINDING_REQUEST, |
| 465 |
"\xb7\xe7\xa7\x01\xbc\x34\xd6\x86\xfa\x87\xdf\xae", |
| 466 |
"\x00\x01\x00\x44\x21\x12\xa4\x42\xb7\xe7" |
| 467 |
"\xa7\x01\xbc\x34\xd6\x86\xfa\x87\xdf\xae" |
| 468 |
"\x00\x24\x00\x04\x6e\x00\x01\xff\x80\x29" |
| 469 |
"\x00\x08\x93\x2f\xf9\xb1\x51\x26\x3b\x36" |
| 470 |
"\x00\x06\x00\x09\x65\x76\x74\x6a\x3a\x68" |
| 471 |
"\x36\x76\x59\x20\x20\x20\x00\x08\x00\x14" |
| 472 |
"\x62\x4e\xeb\xdc\x3c\xc9\x2d\xd8\x4b\x74" |
| 473 |
"\xbf\x85\xd1\xc0\xf5\xde\x36\x87\xbd\x33" |
| 474 |
"\x80\x28\x00\x04\xad\x8a\x85\xff", |
| 475 |
88, |
| 476 |
USE_MESSAGE_INTEGRITY | USE_FINGERPRINT, |
| 477 |
"evtj:h6vY", |
| 478 |
"VOkJxbRl1RmTxUk/WvJxBt", |
| 479 |
"", |
| 480 |
"", |
| 481 |
&create_msgint1 |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
static char* print_binary(const pj_uint8_t *data, unsigned data_len) |
| 555 |
{ |
| 556 |
static char buf[1500]; |
| 557 |
unsigned length = sizeof(buf); |
| 558 |
char *p = buf, *end = buf+sizeof(buf); |
| 559 |
unsigned i; |
| 560 |
|
| 561 |
for (i=0; i<data_len;) { |
| 562 |
unsigned j; |
| 563 |
|
| 564 |
pj_ansi_snprintf(p, end-p, |
| 565 |
"%04d-%04d ", |
| 566 |
i, (i+20 < data_len) ? i+20 : data_len); |
| 567 |
p += 12; |
| 568 |
|
| 569 |
for (j=0; j<20 && i<data_len && p<(buf+length-10); ++j, ++i) { |
| 570 |
pj_ansi_snprintf(p, end-p, "%02x ", (*data) & 0xFF); |
| 571 |
p += 3; |
| 572 |
data++; |
| 573 |
} |
| 574 |
|
| 575 |
pj_ansi_snprintf(p, end-p, "\n"); |
| 576 |
p++; |
| 577 |
} |
| 578 |
|
| 579 |
return buf; |
| 580 |
} |
| 581 |
|
| 582 |
static int cmp_buf(const pj_uint8_t *s1, const pj_uint8_t *s2, unsigned len) |
| 583 |
{ |
| 584 |
unsigned i; |
| 585 |
for (i=0; i<len; ++i) { |
| 586 |
if (s1[i] != s2[i]) |
| 587 |
return i; |
| 588 |
} |
| 589 |
|
| 590 |
return -1; |
| 591 |
} |
| 592 |
|
| 593 |
static int fingerprint_test_vector() |
| 594 |
{ |
| 595 |
pj_pool_t *pool; |
| 596 |
pj_status_t status; |
| 597 |
unsigned i; |
| 598 |
int rc = 0; |
| 599 |
|
| 600 |
|
| 601 |
(void)create_msgint2; |
| 602 |
(void)create_msgint3; |
| 603 |
|
| 604 |
PJ_LOG(3,(THIS_FILE, " draft-denis-behave-rfc3489bis-test-vectors-02")); |
| 605 |
|
| 606 |
pool = pj_pool_create(mem, "fingerprint", 1024, 1024, NULL); |
| 607 |
|
| 608 |
for (i=0; i<PJ_ARRAY_SIZE(test_vectors); ++i) { |
| 609 |
struct test_vector *v; |
| 610 |
pj_stun_msg *ref_msg, *msg; |
| 611 |
pj_size_t parsed_len; |
| 612 |
pj_size_t len; |
| 613 |
unsigned pos; |
| 614 |
pj_uint8_t buf[1500]; |
| 615 |
char print[1500]; |
| 616 |
pj_str_t key; |
| 617 |
|
| 618 |
PJ_LOG(3,(THIS_FILE, " Running test %d/%lu", i, |
| 619 |
PJ_ARRAY_SIZE(test_vectors))); |
| 620 |
|
| 621 |
v = &test_vectors[i]; |
| 622 |
|
| 623 |
|
| 624 |
PJ_LOG(4,(THIS_FILE, "Reference message PDU:\n%s", |
| 625 |
print_binary((pj_uint8_t*)v->pdu, v->pdu_len))); |
| 626 |
|
| 627 |
|
| 628 |
status = pj_stun_msg_decode(pool, (pj_uint8_t*)v->pdu, v->pdu_len, |
| 629 |
PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, |
| 630 |
&ref_msg, &parsed_len, NULL); |
| 631 |
if (status != PJ_SUCCESS) { |
| 632 |
PJ_LOG(1,(THIS_FILE, " Error decoding reference message")); |
| 633 |
rc = -1010; |
| 634 |
goto on_return; |
| 635 |
} |
| 636 |
|
| 637 |
if (parsed_len != v->pdu_len) { |
| 638 |
PJ_LOG(1,(THIS_FILE, " Parsed len error")); |
| 639 |
rc = -1020; |
| 640 |
goto on_return; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
pj_stun_msg_dump(ref_msg, print, sizeof(print), NULL); |
| 645 |
PJ_LOG(4,(THIS_FILE, "Reference message:\n%s", print)); |
| 646 |
|
| 647 |
|
| 648 |
msg = v->create(pool, v); |
| 649 |
if (msg == NULL) { |
| 650 |
PJ_LOG(1,(THIS_FILE, " Error creating stun message")); |
| 651 |
rc = -1030; |
| 652 |
goto on_return; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
if (v->options & USE_MESSAGE_INTEGRITY) { |
| 657 |
pj_str_t s1, s2, r; |
| 658 |
|
| 659 |
pj_stun_create_key(pool, &key, pj_cstr(&r, v->realm), |
| 660 |
pj_cstr(&s1, v->username), |
| 661 |
PJ_STUN_PASSWD_PLAIN, |
| 662 |
pj_cstr(&s2, v->password)); |
| 663 |
pj_stun_msg_encode(msg, buf, sizeof(buf), 0, &key, &len); |
| 664 |
|
| 665 |
} else { |
| 666 |
pj_stun_msg_encode(msg, buf, sizeof(buf), 0, NULL, &len); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
PJ_LOG(4,(THIS_FILE, "Message PDU:\n%s", |
| 671 |
print_binary((pj_uint8_t*)buf, (unsigned)len))); |
| 672 |
|
| 673 |
|
| 674 |
pj_stun_msg_dump(msg, print, sizeof(print), NULL); |
| 675 |
PJ_LOG(4,(THIS_FILE, "Message is:\n%s", print)); |
| 676 |
|
| 677 |
|
| 678 |
if (len != v->pdu_len) { |
| 679 |
PJ_LOG(1,(THIS_FILE, " Message length mismatch")); |
| 680 |
rc = -1050; |
| 681 |
goto on_return; |
| 682 |
} |
| 683 |
|
| 684 |
pos = cmp_buf(buf, (const pj_uint8_t*)v->pdu, (unsigned)len); |
| 685 |
if (pos != (unsigned)-1) { |
| 686 |
PJ_LOG(1,(THIS_FILE, " Message mismatch at byte %d", pos)); |
| 687 |
rc = -1060; |
| 688 |
goto on_return; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
if (v->options & USE_MESSAGE_INTEGRITY) { |
| 693 |
if (PJ_STUN_IS_REQUEST(msg->hdr.type)) { |
| 694 |
pj_stun_auth_cred cred; |
| 695 |
pj_status_t status2; |
| 696 |
|
| 697 |
pj_bzero(&cred, sizeof(cred)); |
| 698 |
cred.type = PJ_STUN_AUTH_CRED_STATIC; |
| 699 |
cred.data.static_cred.realm = pj_str(v->realm); |
| 700 |
cred.data.static_cred.username = pj_str(v->username); |
| 701 |
cred.data.static_cred.data = pj_str(v->password); |
| 702 |
cred.data.static_cred.nonce = pj_str(v->nonce); |
| 703 |
|
| 704 |
status2 = pj_stun_authenticate_request(buf, (unsigned)len, msg, |
| 705 |
&cred, pool, NULL, NULL); |
| 706 |
if (status2 != PJ_SUCCESS) { |
| 707 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 708 |
pj_strerror(status2, errmsg, sizeof(errmsg)); |
| 709 |
PJ_LOG(1,(THIS_FILE, |
| 710 |
" Request authentication failed: %s", |
| 711 |
errmsg)); |
| 712 |
rc = -1070; |
| 713 |
goto on_return; |
| 714 |
} |
| 715 |
|
| 716 |
} else if (PJ_STUN_IS_RESPONSE(msg->hdr.type)) { |
| 717 |
pj_status_t status2; |
| 718 |
status2 = pj_stun_authenticate_response(buf, (unsigned)len, |
| 719 |
msg, &key); |
| 720 |
if (status2 != PJ_SUCCESS) { |
| 721 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 722 |
pj_strerror(status2, errmsg, sizeof(errmsg)); |
| 723 |
PJ_LOG(1,(THIS_FILE, |
| 724 |
" Response authentication failed: %s", |
| 725 |
errmsg)); |
| 726 |
rc = -1080; |
| 727 |
goto on_return; |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
on_return: |
| 735 |
pj_pool_release(pool); |
| 736 |
return rc; |
| 737 |
} |
| 738 |
|
| 739 |
static pj_stun_msg* create_msgint1(pj_pool_t *pool, test_vector *v) |
| 740 |
{ |
| 741 |
pj_stun_msg *msg; |
| 742 |
pj_timestamp u64; |
| 743 |
pj_str_t s1; |
| 744 |
pj_status_t status; |
| 745 |
|
| 746 |
status = pj_stun_msg_create(pool, v->msg_type, PJ_STUN_MAGIC, |
| 747 |
(pj_uint8_t*)v->tsx_id, &msg); |
| 748 |
if (status != PJ_SUCCESS) |
| 749 |
goto on_error; |
| 750 |
|
| 751 |
status = pj_stun_msg_add_uint_attr(pool, msg, PJ_STUN_ATTR_PRIORITY, |
| 752 |
0x6e0001ff); |
| 753 |
if (status != PJ_SUCCESS) |
| 754 |
goto on_error; |
| 755 |
|
| 756 |
u64.u32.hi = 0x932ff9b1; |
| 757 |
u64.u32.lo = 0x51263b36; |
| 758 |
status = pj_stun_msg_add_uint64_attr(pool, msg, |
| 759 |
PJ_STUN_ATTR_ICE_CONTROLLED, &u64); |
| 760 |
if (status != PJ_SUCCESS) |
| 761 |
goto on_error; |
| 762 |
|
| 763 |
status = pj_stun_msg_add_string_attr(pool, msg, PJ_STUN_ATTR_USERNAME, |
| 764 |
pj_cstr(&s1, v->username)); |
| 765 |
if (status != PJ_SUCCESS) |
| 766 |
goto on_error; |
| 767 |
|
| 768 |
status = pj_stun_msg_add_msgint_attr(pool, msg); |
| 769 |
if (status != PJ_SUCCESS) |
| 770 |
goto on_error; |
| 771 |
|
| 772 |
status = pj_stun_msg_add_uint_attr(pool, msg, PJ_STUN_ATTR_FINGERPRINT, 0); |
| 773 |
if (status != PJ_SUCCESS) |
| 774 |
goto on_error; |
| 775 |
|
| 776 |
return msg; |
| 777 |
|
| 778 |
on_error: |
| 779 |
app_perror(" error: create_msgint1()", status); |
| 780 |
return NULL; |
| 781 |
} |
| 782 |
|
| 783 |
static pj_stun_msg* create_msgint2(pj_pool_t *pool, test_vector *v) |
| 784 |
{ |
| 785 |
pj_stun_msg *msg; |
| 786 |
pj_sockaddr_in mapped_addr; |
| 787 |
pj_str_t s1; |
| 788 |
pj_status_t status; |
| 789 |
|
| 790 |
status = pj_stun_msg_create(pool, v->msg_type, PJ_STUN_MAGIC, |
| 791 |
(pj_uint8_t*)v->tsx_id, &msg); |
| 792 |
if (status != PJ_SUCCESS) |
| 793 |
goto on_error; |
| 794 |
|
| 795 |
status = pj_stun_msg_add_string_attr(pool, msg, PJ_STUN_ATTR_SOFTWARE, |
| 796 |
pj_cstr(&s1, "test vector")); |
| 797 |
if (status != PJ_SUCCESS) |
| 798 |
goto on_error; |
| 799 |
|
| 800 |
status = pj_sockaddr_in_init(&mapped_addr, pj_cstr(&s1, "192.0.2.1"), |
| 801 |
32853); |
| 802 |
if (status != PJ_SUCCESS) |
| 803 |
goto on_error; |
| 804 |
|
| 805 |
status = pj_stun_msg_add_sockaddr_attr(pool, msg, |
| 806 |
PJ_STUN_ATTR_XOR_MAPPED_ADDR, |
| 807 |
PJ_TRUE, &mapped_addr, |
| 808 |
sizeof(pj_sockaddr_in)); |
| 809 |
if (status != PJ_SUCCESS) |
| 810 |
goto on_error; |
| 811 |
|
| 812 |
status = pj_stun_msg_add_msgint_attr(pool, msg); |
| 813 |
if (status != PJ_SUCCESS) |
| 814 |
goto on_error; |
| 815 |
|
| 816 |
status = pj_stun_msg_add_uint_attr(pool, msg, PJ_STUN_ATTR_FINGERPRINT, 0); |
| 817 |
if (status != PJ_SUCCESS) |
| 818 |
goto on_error; |
| 819 |
|
| 820 |
return msg; |
| 821 |
|
| 822 |
on_error: |
| 823 |
app_perror(" error: create_msgint2()", status); |
| 824 |
return NULL; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
static pj_stun_msg* create_msgint3(pj_pool_t *pool, test_vector *v) |
| 829 |
{ |
| 830 |
pj_stun_msg *msg; |
| 831 |
pj_sockaddr mapped_addr; |
| 832 |
pj_str_t s1; |
| 833 |
pj_status_t status; |
| 834 |
|
| 835 |
status = pj_stun_msg_create(pool, v->msg_type, PJ_STUN_MAGIC, |
| 836 |
(pj_uint8_t*)v->tsx_id, &msg); |
| 837 |
if (status != PJ_SUCCESS) |
| 838 |
goto on_error; |
| 839 |
|
| 840 |
status = pj_stun_msg_add_string_attr(pool, msg, PJ_STUN_ATTR_SOFTWARE, |
| 841 |
pj_cstr(&s1, "test vector")); |
| 842 |
if (status != PJ_SUCCESS) |
| 843 |
goto on_error; |
| 844 |
|
| 845 |
status = pj_sockaddr_init(pj_AF_INET6(), &mapped_addr, |
| 846 |
pj_cstr(&s1, "2001:db8:1234:5678:11:2233:4455:6677"), |
| 847 |
32853); |
| 848 |
if (status != PJ_SUCCESS) |
| 849 |
goto on_error; |
| 850 |
|
| 851 |
status = pj_stun_msg_add_sockaddr_attr(pool, msg, |
| 852 |
PJ_STUN_ATTR_XOR_MAPPED_ADDR, |
| 853 |
PJ_TRUE, &mapped_addr, |
| 854 |
sizeof(pj_sockaddr)); |
| 855 |
if (status != PJ_SUCCESS) |
| 856 |
goto on_error; |
| 857 |
|
| 858 |
status = pj_stun_msg_add_msgint_attr(pool, msg); |
| 859 |
if (status != PJ_SUCCESS) |
| 860 |
goto on_error; |
| 861 |
|
| 862 |
status = pj_stun_msg_add_uint_attr(pool, msg, PJ_STUN_ATTR_FINGERPRINT, 0); |
| 863 |
if (status != PJ_SUCCESS) |
| 864 |
goto on_error; |
| 865 |
|
| 866 |
return msg; |
| 867 |
|
| 868 |
on_error: |
| 869 |
app_perror(" error: create_msgint3()", status); |
| 870 |
return NULL; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
static int cmp_msg(const pj_stun_msg *msg1, const pj_stun_msg *msg2) |
| 876 |
{ |
| 877 |
unsigned i; |
| 878 |
|
| 879 |
if (msg1->hdr.type != msg2->hdr.type) |
| 880 |
return -10; |
| 881 |
if (msg1->hdr.length != msg2->hdr.length) |
| 882 |
return -20; |
| 883 |
if (msg1->hdr.magic != msg2->hdr.magic) |
| 884 |
return -30; |
| 885 |
if (pj_memcmp(msg1->hdr.tsx_id, msg2->hdr.tsx_id, sizeof(msg1->hdr.tsx_id))) |
| 886 |
return -40; |
| 887 |
if (msg1->attr_count != msg2->attr_count) |
| 888 |
return -50; |
| 889 |
|
| 890 |
for (i=0; i<msg1->attr_count; ++i) { |
| 891 |
const pj_stun_attr_hdr *a1 = msg1->attr[i]; |
| 892 |
const pj_stun_attr_hdr *a2 = msg2->attr[i]; |
| 893 |
|
| 894 |
if (a1->type != a2->type) |
| 895 |
return -60; |
| 896 |
if (a1->length != a2->length) |
| 897 |
return -70; |
| 898 |
} |
| 899 |
|
| 900 |
return 0; |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
static int handle_unknown_non_mandatory(void) |
| 905 |
{ |
| 906 |
pj_pool_t *pool = pj_pool_create(mem, NULL, 1000, 1000, NULL); |
| 907 |
pj_stun_msg *msg0, *msg1, *msg2; |
| 908 |
pj_uint8_t data[] = { 1, 2, 3, 4, 5, 6}; |
| 909 |
pj_uint8_t packet[500]; |
| 910 |
pj_stun_auth_cred cred; |
| 911 |
pj_size_t len; |
| 912 |
pj_status_t rc; |
| 913 |
|
| 914 |
PJ_LOG(3,(THIS_FILE, " handling unknown non-mandatory attr")); |
| 915 |
|
| 916 |
PJ_LOG(3,(THIS_FILE, " encoding")); |
| 917 |
rc = pj_stun_msg_create(pool, PJ_STUN_BINDING_REQUEST, PJ_STUN_MAGIC, NULL, &msg0); |
| 918 |
rc += pj_stun_msg_add_string_attr(pool, msg0, PJ_STUN_ATTR_USERNAME, &USERNAME); |
| 919 |
rc += pj_stun_msg_add_binary_attr(pool, msg0, 0x80ff, data, sizeof(data)); |
| 920 |
rc += pj_stun_msg_add_msgint_attr(pool, msg0); |
| 921 |
rc += pj_stun_msg_encode(msg0, packet, sizeof(packet), 0, &PASSWORD, &len); |
| 922 |
|
| 923 |
#if 0 |
| 924 |
if (1) { |
| 925 |
unsigned i; |
| 926 |
puts(""); |
| 927 |
printf("{ "); |
| 928 |
for (i=0; i<len; ++i) printf("0x%02x, ", packet[i] & 0xFF); |
| 929 |
puts(" }"); |
| 930 |
} |
| 931 |
#endif |
| 932 |
|
| 933 |
PJ_LOG(3,(THIS_FILE, " decoding")); |
| 934 |
rc += pj_stun_msg_decode(pool, packet, len, PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, |
| 935 |
&msg1, NULL, NULL); |
| 936 |
|
| 937 |
rc += cmp_msg(msg0, msg1); |
| 938 |
|
| 939 |
pj_bzero(&cred, sizeof(cred)); |
| 940 |
cred.type = PJ_STUN_AUTH_CRED_STATIC; |
| 941 |
cred.data.static_cred.username = USERNAME; |
| 942 |
cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; |
| 943 |
cred.data.static_cred.data = PASSWORD; |
| 944 |
|
| 945 |
PJ_LOG(3,(THIS_FILE, " authenticating")); |
| 946 |
rc += pj_stun_authenticate_request(packet, (unsigned)len, msg1, &cred, pool, |
| 947 |
NULL, NULL); |
| 948 |
|
| 949 |
PJ_LOG(3,(THIS_FILE, " clone")); |
| 950 |
msg2 = pj_stun_msg_clone(pool, msg1); |
| 951 |
rc += cmp_msg(msg0, msg2); |
| 952 |
|
| 953 |
pj_pool_release(pool); |
| 954 |
|
| 955 |
return rc==0 ? 0 : -4410; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
int stun_test(void) |
| 960 |
{ |
| 961 |
int pad, rc; |
| 962 |
|
| 963 |
pad = pj_stun_set_padding_char(32); |
| 964 |
|
| 965 |
rc = decode_test(); |
| 966 |
if (rc != 0) |
| 967 |
goto on_return; |
| 968 |
|
| 969 |
rc = decode_verify(); |
| 970 |
if (rc != 0) |
| 971 |
goto on_return; |
| 972 |
|
| 973 |
rc = fingerprint_test_vector(); |
| 974 |
if (rc != 0) |
| 975 |
goto on_return; |
| 976 |
|
| 977 |
rc = handle_unknown_non_mandatory(); |
| 978 |
if (rc != 0) |
| 979 |
goto on_return; |
| 980 |
|
| 981 |
on_return: |
| 982 |
pj_stun_set_padding_char(pad); |
| 983 |
return rc; |
| 984 |
} |
| 985 |
|