cell_rendezvous.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* cell_rendezvous.c -- generated by Trunnel v1.5.2.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "cell_rendezvous.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're running a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int cellrendezvous_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || cellrendezvous_deadcode_dummy__
  17. #else
  18. #define OR_DEADCODE_DUMMY
  19. #endif
  20. #define CHECK_REMAINING(nbytes, label) \
  21. do { \
  22. if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
  23. goto label; \
  24. } \
  25. } while (0)
  26. trn_cell_rendezvous1_t *
  27. trn_cell_rendezvous1_new(void)
  28. {
  29. trn_cell_rendezvous1_t *val = trunnel_calloc(1, sizeof(trn_cell_rendezvous1_t));
  30. if (NULL == val)
  31. return NULL;
  32. return val;
  33. }
  34. /** Release all storage held inside 'obj', but do not free 'obj'.
  35. */
  36. static void
  37. trn_cell_rendezvous1_clear(trn_cell_rendezvous1_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->handshake_info);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->handshake_info);
  42. }
  43. void
  44. trn_cell_rendezvous1_free(trn_cell_rendezvous1_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. trn_cell_rendezvous1_clear(obj);
  49. trunnel_memwipe(obj, sizeof(trn_cell_rendezvous1_t));
  50. trunnel_free_(obj);
  51. }
  52. size_t
  53. trn_cell_rendezvous1_getlen_rendezvous_cookie(const trn_cell_rendezvous1_t *inp)
  54. {
  55. (void)inp; return TRUNNEL_REND_COOKIE_LEN;
  56. }
  57. uint8_t
  58. trn_cell_rendezvous1_get_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx)
  59. {
  60. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  61. return inp->rendezvous_cookie[idx];
  62. }
  63. uint8_t
  64. trn_cell_rendezvous1_getconst_rendezvous_cookie(const trn_cell_rendezvous1_t *inp, size_t idx)
  65. {
  66. return trn_cell_rendezvous1_get_rendezvous_cookie((trn_cell_rendezvous1_t*)inp, idx);
  67. }
  68. int
  69. trn_cell_rendezvous1_set_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt)
  70. {
  71. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  72. inp->rendezvous_cookie[idx] = elt;
  73. return 0;
  74. }
  75. uint8_t *
  76. trn_cell_rendezvous1_getarray_rendezvous_cookie(trn_cell_rendezvous1_t *inp)
  77. {
  78. return inp->rendezvous_cookie;
  79. }
  80. const uint8_t *
  81. trn_cell_rendezvous1_getconstarray_rendezvous_cookie(const trn_cell_rendezvous1_t *inp)
  82. {
  83. return (const uint8_t *)trn_cell_rendezvous1_getarray_rendezvous_cookie((trn_cell_rendezvous1_t*)inp);
  84. }
  85. size_t
  86. trn_cell_rendezvous1_getlen_handshake_info(const trn_cell_rendezvous1_t *inp)
  87. {
  88. return TRUNNEL_DYNARRAY_LEN(&inp->handshake_info);
  89. }
  90. uint8_t
  91. trn_cell_rendezvous1_get_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx)
  92. {
  93. return TRUNNEL_DYNARRAY_GET(&inp->handshake_info, idx);
  94. }
  95. uint8_t
  96. trn_cell_rendezvous1_getconst_handshake_info(const trn_cell_rendezvous1_t *inp, size_t idx)
  97. {
  98. return trn_cell_rendezvous1_get_handshake_info((trn_cell_rendezvous1_t*)inp, idx);
  99. }
  100. int
  101. trn_cell_rendezvous1_set_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt)
  102. {
  103. TRUNNEL_DYNARRAY_SET(&inp->handshake_info, idx, elt);
  104. return 0;
  105. }
  106. int
  107. trn_cell_rendezvous1_add_handshake_info(trn_cell_rendezvous1_t *inp, uint8_t elt)
  108. {
  109. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->handshake_info, elt, {});
  110. return 0;
  111. trunnel_alloc_failed:
  112. TRUNNEL_SET_ERROR_CODE(inp);
  113. return -1;
  114. }
  115. uint8_t *
  116. trn_cell_rendezvous1_getarray_handshake_info(trn_cell_rendezvous1_t *inp)
  117. {
  118. return inp->handshake_info.elts_;
  119. }
  120. const uint8_t *
  121. trn_cell_rendezvous1_getconstarray_handshake_info(const trn_cell_rendezvous1_t *inp)
  122. {
  123. return (const uint8_t *)trn_cell_rendezvous1_getarray_handshake_info((trn_cell_rendezvous1_t*)inp);
  124. }
  125. int
  126. trn_cell_rendezvous1_setlen_handshake_info(trn_cell_rendezvous1_t *inp, size_t newlen)
  127. {
  128. uint8_t *newptr;
  129. newptr = trunnel_dynarray_setlen(&inp->handshake_info.allocated_,
  130. &inp->handshake_info.n_, inp->handshake_info.elts_, newlen,
  131. sizeof(inp->handshake_info.elts_[0]), (trunnel_free_fn_t) NULL,
  132. &inp->trunnel_error_code_);
  133. if (newlen != 0 && newptr == NULL)
  134. goto trunnel_alloc_failed;
  135. inp->handshake_info.elts_ = newptr;
  136. return 0;
  137. trunnel_alloc_failed:
  138. TRUNNEL_SET_ERROR_CODE(inp);
  139. return -1;
  140. }
  141. const char *
  142. trn_cell_rendezvous1_check(const trn_cell_rendezvous1_t *obj)
  143. {
  144. if (obj == NULL)
  145. return "Object was NULL";
  146. if (obj->trunnel_error_code_)
  147. return "A set function failed on this object";
  148. return NULL;
  149. }
  150. ssize_t
  151. trn_cell_rendezvous1_encoded_len(const trn_cell_rendezvous1_t *obj)
  152. {
  153. ssize_t result = 0;
  154. if (NULL != trn_cell_rendezvous1_check(obj))
  155. return -1;
  156. /* Length of u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  157. result += TRUNNEL_REND_COOKIE_LEN;
  158. /* Length of u8 handshake_info[] */
  159. result += TRUNNEL_DYNARRAY_LEN(&obj->handshake_info);
  160. return result;
  161. }
  162. int
  163. trn_cell_rendezvous1_clear_errors(trn_cell_rendezvous1_t *obj)
  164. {
  165. int r = obj->trunnel_error_code_;
  166. obj->trunnel_error_code_ = 0;
  167. return r;
  168. }
  169. ssize_t
  170. trn_cell_rendezvous1_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous1_t *obj)
  171. {
  172. ssize_t result = 0;
  173. size_t written = 0;
  174. uint8_t *ptr = output;
  175. const char *msg;
  176. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  177. const ssize_t encoded_len = trn_cell_rendezvous1_encoded_len(obj);
  178. #endif
  179. if (NULL != (msg = trn_cell_rendezvous1_check(obj)))
  180. goto check_failed;
  181. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  182. trunnel_assert(encoded_len >= 0);
  183. #endif
  184. /* Encode u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  185. trunnel_assert(written <= avail);
  186. if (avail - written < TRUNNEL_REND_COOKIE_LEN)
  187. goto truncated;
  188. memcpy(ptr, obj->rendezvous_cookie, TRUNNEL_REND_COOKIE_LEN);
  189. written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  190. /* Encode u8 handshake_info[] */
  191. {
  192. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->handshake_info);
  193. trunnel_assert(written <= avail);
  194. if (avail - written < elt_len)
  195. goto truncated;
  196. if (elt_len)
  197. memcpy(ptr, obj->handshake_info.elts_, elt_len);
  198. written += elt_len; ptr += elt_len;
  199. }
  200. trunnel_assert(ptr == output + written);
  201. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  202. {
  203. trunnel_assert(encoded_len >= 0);
  204. trunnel_assert((size_t)encoded_len == written);
  205. }
  206. #endif
  207. return written;
  208. truncated:
  209. result = -2;
  210. goto fail;
  211. check_failed:
  212. (void)msg;
  213. result = -1;
  214. goto fail;
  215. fail:
  216. trunnel_assert(result < 0);
  217. return result;
  218. }
  219. /** As trn_cell_rendezvous1_parse(), but do not allocate the output
  220. * object.
  221. */
  222. static ssize_t
  223. trn_cell_rendezvous1_parse_into(trn_cell_rendezvous1_t *obj, const uint8_t *input, const size_t len_in)
  224. {
  225. const uint8_t *ptr = input;
  226. size_t remaining = len_in;
  227. ssize_t result = 0;
  228. (void)result;
  229. /* Parse u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  230. CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated);
  231. memcpy(obj->rendezvous_cookie, ptr, TRUNNEL_REND_COOKIE_LEN);
  232. remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  233. /* Parse u8 handshake_info[] */
  234. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->handshake_info, remaining, {});
  235. obj->handshake_info.n_ = remaining;
  236. if (remaining)
  237. memcpy(obj->handshake_info.elts_, ptr, remaining);
  238. ptr += remaining; remaining -= remaining;
  239. trunnel_assert(ptr + remaining == input + len_in);
  240. return len_in - remaining;
  241. truncated:
  242. return -2;
  243. trunnel_alloc_failed:
  244. return -1;
  245. }
  246. ssize_t
  247. trn_cell_rendezvous1_parse(trn_cell_rendezvous1_t **output, const uint8_t *input, const size_t len_in)
  248. {
  249. ssize_t result;
  250. *output = trn_cell_rendezvous1_new();
  251. if (NULL == *output)
  252. return -1;
  253. result = trn_cell_rendezvous1_parse_into(*output, input, len_in);
  254. if (result < 0) {
  255. trn_cell_rendezvous1_free(*output);
  256. *output = NULL;
  257. }
  258. return result;
  259. }
  260. trn_cell_rendezvous2_t *
  261. trn_cell_rendezvous2_new(void)
  262. {
  263. trn_cell_rendezvous2_t *val = trunnel_calloc(1, sizeof(trn_cell_rendezvous2_t));
  264. if (NULL == val)
  265. return NULL;
  266. return val;
  267. }
  268. /** Release all storage held inside 'obj', but do not free 'obj'.
  269. */
  270. static void
  271. trn_cell_rendezvous2_clear(trn_cell_rendezvous2_t *obj)
  272. {
  273. (void) obj;
  274. }
  275. void
  276. trn_cell_rendezvous2_free(trn_cell_rendezvous2_t *obj)
  277. {
  278. if (obj == NULL)
  279. return;
  280. trn_cell_rendezvous2_clear(obj);
  281. trunnel_memwipe(obj, sizeof(trn_cell_rendezvous2_t));
  282. trunnel_free_(obj);
  283. }
  284. size_t
  285. trn_cell_rendezvous2_getlen_handshake_info(const trn_cell_rendezvous2_t *inp)
  286. {
  287. (void)inp; return TRUNNEL_HANDSHAKE_INFO_LEN;
  288. }
  289. uint8_t
  290. trn_cell_rendezvous2_get_handshake_info(trn_cell_rendezvous2_t *inp, size_t idx)
  291. {
  292. trunnel_assert(idx < TRUNNEL_HANDSHAKE_INFO_LEN);
  293. return inp->handshake_info[idx];
  294. }
  295. uint8_t
  296. trn_cell_rendezvous2_getconst_handshake_info(const trn_cell_rendezvous2_t *inp, size_t idx)
  297. {
  298. return trn_cell_rendezvous2_get_handshake_info((trn_cell_rendezvous2_t*)inp, idx);
  299. }
  300. int
  301. trn_cell_rendezvous2_set_handshake_info(trn_cell_rendezvous2_t *inp, size_t idx, uint8_t elt)
  302. {
  303. trunnel_assert(idx < TRUNNEL_HANDSHAKE_INFO_LEN);
  304. inp->handshake_info[idx] = elt;
  305. return 0;
  306. }
  307. uint8_t *
  308. trn_cell_rendezvous2_getarray_handshake_info(trn_cell_rendezvous2_t *inp)
  309. {
  310. return inp->handshake_info;
  311. }
  312. const uint8_t *
  313. trn_cell_rendezvous2_getconstarray_handshake_info(const trn_cell_rendezvous2_t *inp)
  314. {
  315. return (const uint8_t *)trn_cell_rendezvous2_getarray_handshake_info((trn_cell_rendezvous2_t*)inp);
  316. }
  317. const char *
  318. trn_cell_rendezvous2_check(const trn_cell_rendezvous2_t *obj)
  319. {
  320. if (obj == NULL)
  321. return "Object was NULL";
  322. if (obj->trunnel_error_code_)
  323. return "A set function failed on this object";
  324. return NULL;
  325. }
  326. ssize_t
  327. trn_cell_rendezvous2_encoded_len(const trn_cell_rendezvous2_t *obj)
  328. {
  329. ssize_t result = 0;
  330. if (NULL != trn_cell_rendezvous2_check(obj))
  331. return -1;
  332. /* Length of u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */
  333. result += TRUNNEL_HANDSHAKE_INFO_LEN;
  334. return result;
  335. }
  336. int
  337. trn_cell_rendezvous2_clear_errors(trn_cell_rendezvous2_t *obj)
  338. {
  339. int r = obj->trunnel_error_code_;
  340. obj->trunnel_error_code_ = 0;
  341. return r;
  342. }
  343. ssize_t
  344. trn_cell_rendezvous2_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous2_t *obj)
  345. {
  346. ssize_t result = 0;
  347. size_t written = 0;
  348. uint8_t *ptr = output;
  349. const char *msg;
  350. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  351. const ssize_t encoded_len = trn_cell_rendezvous2_encoded_len(obj);
  352. #endif
  353. if (NULL != (msg = trn_cell_rendezvous2_check(obj)))
  354. goto check_failed;
  355. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  356. trunnel_assert(encoded_len >= 0);
  357. #endif
  358. /* Encode u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */
  359. trunnel_assert(written <= avail);
  360. if (avail - written < TRUNNEL_HANDSHAKE_INFO_LEN)
  361. goto truncated;
  362. memcpy(ptr, obj->handshake_info, TRUNNEL_HANDSHAKE_INFO_LEN);
  363. written += TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_LEN;
  364. trunnel_assert(ptr == output + written);
  365. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  366. {
  367. trunnel_assert(encoded_len >= 0);
  368. trunnel_assert((size_t)encoded_len == written);
  369. }
  370. #endif
  371. return written;
  372. truncated:
  373. result = -2;
  374. goto fail;
  375. check_failed:
  376. (void)msg;
  377. result = -1;
  378. goto fail;
  379. fail:
  380. trunnel_assert(result < 0);
  381. return result;
  382. }
  383. /** As trn_cell_rendezvous2_parse(), but do not allocate the output
  384. * object.
  385. */
  386. static ssize_t
  387. trn_cell_rendezvous2_parse_into(trn_cell_rendezvous2_t *obj, const uint8_t *input, const size_t len_in)
  388. {
  389. const uint8_t *ptr = input;
  390. size_t remaining = len_in;
  391. ssize_t result = 0;
  392. (void)result;
  393. /* Parse u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */
  394. CHECK_REMAINING(TRUNNEL_HANDSHAKE_INFO_LEN, truncated);
  395. memcpy(obj->handshake_info, ptr, TRUNNEL_HANDSHAKE_INFO_LEN);
  396. remaining -= TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_LEN;
  397. trunnel_assert(ptr + remaining == input + len_in);
  398. return len_in - remaining;
  399. truncated:
  400. return -2;
  401. }
  402. ssize_t
  403. trn_cell_rendezvous2_parse(trn_cell_rendezvous2_t **output, const uint8_t *input, const size_t len_in)
  404. {
  405. ssize_t result;
  406. *output = trn_cell_rendezvous2_new();
  407. if (NULL == *output)
  408. return -1;
  409. result = trn_cell_rendezvous2_parse_into(*output, input, len_in);
  410. if (result < 0) {
  411. trn_cell_rendezvous2_free(*output);
  412. *output = NULL;
  413. }
  414. return result;
  415. }