stun.c 28 KB

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