cell_introduce1.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* cell_introduce1.c -- generated by Trunnel v1.5.
  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_introduce1.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 runnning a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int cellintroduce_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || cellintroduce_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. typedef struct cell_extension_st cell_extension_t;
  27. cell_extension_t *cell_extension_new(void);
  28. void cell_extension_free(cell_extension_t *victim);
  29. ssize_t cell_extension_parse(cell_extension_t **output, const uint8_t *input, const size_t len_in);
  30. ssize_t cell_extension_encoded_len(const cell_extension_t *obj);
  31. ssize_t cell_extension_encode(uint8_t *output, size_t avail, const cell_extension_t *input);
  32. const char *cell_extension_check(const cell_extension_t *obj);
  33. int cell_extension_clear_errors(cell_extension_t *obj);
  34. typedef struct link_specifier_st link_specifier_t;
  35. link_specifier_t *link_specifier_new(void);
  36. void link_specifier_free(link_specifier_t *victim);
  37. ssize_t link_specifier_parse(link_specifier_t **output, const uint8_t *input, const size_t len_in);
  38. ssize_t link_specifier_encoded_len(const link_specifier_t *obj);
  39. ssize_t link_specifier_encode(uint8_t *output, size_t avail, const link_specifier_t *input);
  40. const char *link_specifier_check(const link_specifier_t *obj);
  41. int link_specifier_clear_errors(link_specifier_t *obj);
  42. hs_cell_introduce1_t *
  43. hs_cell_introduce1_new(void)
  44. {
  45. hs_cell_introduce1_t *val = trunnel_calloc(1, sizeof(hs_cell_introduce1_t));
  46. if (NULL == val)
  47. return NULL;
  48. return val;
  49. }
  50. /** Release all storage held inside 'obj', but do not free 'obj'.
  51. */
  52. static void
  53. hs_cell_introduce1_clear(hs_cell_introduce1_t *obj)
  54. {
  55. (void) obj;
  56. TRUNNEL_DYNARRAY_WIPE(&obj->auth_key);
  57. TRUNNEL_DYNARRAY_CLEAR(&obj->auth_key);
  58. cell_extension_free(obj->extensions);
  59. obj->extensions = NULL;
  60. TRUNNEL_DYNARRAY_WIPE(&obj->encrypted);
  61. TRUNNEL_DYNARRAY_CLEAR(&obj->encrypted);
  62. }
  63. void
  64. hs_cell_introduce1_free(hs_cell_introduce1_t *obj)
  65. {
  66. if (obj == NULL)
  67. return;
  68. hs_cell_introduce1_clear(obj);
  69. trunnel_memwipe(obj, sizeof(hs_cell_introduce1_t));
  70. trunnel_free_(obj);
  71. }
  72. size_t
  73. hs_cell_introduce1_getlen_legacy_key_id(const hs_cell_introduce1_t *inp)
  74. {
  75. (void)inp; return TRUNNEL_SHA1_LEN;
  76. }
  77. uint8_t
  78. hs_cell_introduce1_get_legacy_key_id(hs_cell_introduce1_t *inp, size_t idx)
  79. {
  80. trunnel_assert(idx < TRUNNEL_SHA1_LEN);
  81. return inp->legacy_key_id[idx];
  82. }
  83. uint8_t
  84. hs_cell_introduce1_getconst_legacy_key_id(const hs_cell_introduce1_t *inp, size_t idx)
  85. {
  86. return hs_cell_introduce1_get_legacy_key_id((hs_cell_introduce1_t*)inp, idx);
  87. }
  88. int
  89. hs_cell_introduce1_set_legacy_key_id(hs_cell_introduce1_t *inp, size_t idx, uint8_t elt)
  90. {
  91. trunnel_assert(idx < TRUNNEL_SHA1_LEN);
  92. inp->legacy_key_id[idx] = elt;
  93. return 0;
  94. }
  95. uint8_t *
  96. hs_cell_introduce1_getarray_legacy_key_id(hs_cell_introduce1_t *inp)
  97. {
  98. return inp->legacy_key_id;
  99. }
  100. const uint8_t *
  101. hs_cell_introduce1_getconstarray_legacy_key_id(const hs_cell_introduce1_t *inp)
  102. {
  103. return (const uint8_t *)hs_cell_introduce1_getarray_legacy_key_id((hs_cell_introduce1_t*)inp);
  104. }
  105. uint8_t
  106. hs_cell_introduce1_get_auth_key_type(hs_cell_introduce1_t *inp)
  107. {
  108. return inp->auth_key_type;
  109. }
  110. int
  111. hs_cell_introduce1_set_auth_key_type(hs_cell_introduce1_t *inp, uint8_t val)
  112. {
  113. if (! ((val == 0 || val == 1 || val == 2))) {
  114. TRUNNEL_SET_ERROR_CODE(inp);
  115. return -1;
  116. }
  117. inp->auth_key_type = val;
  118. return 0;
  119. }
  120. uint16_t
  121. hs_cell_introduce1_get_auth_key_len(hs_cell_introduce1_t *inp)
  122. {
  123. return inp->auth_key_len;
  124. }
  125. int
  126. hs_cell_introduce1_set_auth_key_len(hs_cell_introduce1_t *inp, uint16_t val)
  127. {
  128. inp->auth_key_len = val;
  129. return 0;
  130. }
  131. size_t
  132. hs_cell_introduce1_getlen_auth_key(const hs_cell_introduce1_t *inp)
  133. {
  134. return TRUNNEL_DYNARRAY_LEN(&inp->auth_key);
  135. }
  136. uint8_t
  137. hs_cell_introduce1_get_auth_key(hs_cell_introduce1_t *inp, size_t idx)
  138. {
  139. return TRUNNEL_DYNARRAY_GET(&inp->auth_key, idx);
  140. }
  141. uint8_t
  142. hs_cell_introduce1_getconst_auth_key(const hs_cell_introduce1_t *inp, size_t idx)
  143. {
  144. return hs_cell_introduce1_get_auth_key((hs_cell_introduce1_t*)inp, idx);
  145. }
  146. int
  147. hs_cell_introduce1_set_auth_key(hs_cell_introduce1_t *inp, size_t idx, uint8_t elt)
  148. {
  149. TRUNNEL_DYNARRAY_SET(&inp->auth_key, idx, elt);
  150. return 0;
  151. }
  152. int
  153. hs_cell_introduce1_add_auth_key(hs_cell_introduce1_t *inp, uint8_t elt)
  154. {
  155. #if SIZE_MAX >= UINT16_MAX
  156. if (inp->auth_key.n_ == UINT16_MAX)
  157. goto trunnel_alloc_failed;
  158. #endif
  159. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->auth_key, elt, {});
  160. return 0;
  161. trunnel_alloc_failed:
  162. TRUNNEL_SET_ERROR_CODE(inp);
  163. return -1;
  164. }
  165. uint8_t *
  166. hs_cell_introduce1_getarray_auth_key(hs_cell_introduce1_t *inp)
  167. {
  168. return inp->auth_key.elts_;
  169. }
  170. const uint8_t *
  171. hs_cell_introduce1_getconstarray_auth_key(const hs_cell_introduce1_t *inp)
  172. {
  173. return (const uint8_t *)hs_cell_introduce1_getarray_auth_key((hs_cell_introduce1_t*)inp);
  174. }
  175. int
  176. hs_cell_introduce1_setlen_auth_key(hs_cell_introduce1_t *inp, size_t newlen)
  177. {
  178. uint8_t *newptr;
  179. #if UINT16_MAX < SIZE_MAX
  180. if (newlen > UINT16_MAX)
  181. goto trunnel_alloc_failed;
  182. #endif
  183. newptr = trunnel_dynarray_setlen(&inp->auth_key.allocated_,
  184. &inp->auth_key.n_, inp->auth_key.elts_, newlen,
  185. sizeof(inp->auth_key.elts_[0]), (trunnel_free_fn_t) NULL,
  186. &inp->trunnel_error_code_);
  187. if (newlen != 0 && newptr == NULL)
  188. goto trunnel_alloc_failed;
  189. inp->auth_key.elts_ = newptr;
  190. return 0;
  191. trunnel_alloc_failed:
  192. TRUNNEL_SET_ERROR_CODE(inp);
  193. return -1;
  194. }
  195. struct cell_extension_st *
  196. hs_cell_introduce1_get_extensions(hs_cell_introduce1_t *inp)
  197. {
  198. return inp->extensions;
  199. }
  200. const struct cell_extension_st *
  201. hs_cell_introduce1_getconst_extensions(const hs_cell_introduce1_t *inp)
  202. {
  203. return hs_cell_introduce1_get_extensions((hs_cell_introduce1_t*) inp);
  204. }
  205. int
  206. hs_cell_introduce1_set_extensions(hs_cell_introduce1_t *inp, struct cell_extension_st *val)
  207. {
  208. if (inp->extensions && inp->extensions != val)
  209. cell_extension_free(inp->extensions);
  210. return hs_cell_introduce1_set0_extensions(inp, val);
  211. }
  212. int
  213. hs_cell_introduce1_set0_extensions(hs_cell_introduce1_t *inp, struct cell_extension_st *val)
  214. {
  215. inp->extensions = val;
  216. return 0;
  217. }
  218. size_t
  219. hs_cell_introduce1_getlen_encrypted(const hs_cell_introduce1_t *inp)
  220. {
  221. return TRUNNEL_DYNARRAY_LEN(&inp->encrypted);
  222. }
  223. uint8_t
  224. hs_cell_introduce1_get_encrypted(hs_cell_introduce1_t *inp, size_t idx)
  225. {
  226. return TRUNNEL_DYNARRAY_GET(&inp->encrypted, idx);
  227. }
  228. uint8_t
  229. hs_cell_introduce1_getconst_encrypted(const hs_cell_introduce1_t *inp, size_t idx)
  230. {
  231. return hs_cell_introduce1_get_encrypted((hs_cell_introduce1_t*)inp, idx);
  232. }
  233. int
  234. hs_cell_introduce1_set_encrypted(hs_cell_introduce1_t *inp, size_t idx, uint8_t elt)
  235. {
  236. TRUNNEL_DYNARRAY_SET(&inp->encrypted, idx, elt);
  237. return 0;
  238. }
  239. int
  240. hs_cell_introduce1_add_encrypted(hs_cell_introduce1_t *inp, uint8_t elt)
  241. {
  242. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->encrypted, elt, {});
  243. return 0;
  244. trunnel_alloc_failed:
  245. TRUNNEL_SET_ERROR_CODE(inp);
  246. return -1;
  247. }
  248. uint8_t *
  249. hs_cell_introduce1_getarray_encrypted(hs_cell_introduce1_t *inp)
  250. {
  251. return inp->encrypted.elts_;
  252. }
  253. const uint8_t *
  254. hs_cell_introduce1_getconstarray_encrypted(const hs_cell_introduce1_t *inp)
  255. {
  256. return (const uint8_t *)hs_cell_introduce1_getarray_encrypted((hs_cell_introduce1_t*)inp);
  257. }
  258. int
  259. hs_cell_introduce1_setlen_encrypted(hs_cell_introduce1_t *inp, size_t newlen)
  260. {
  261. uint8_t *newptr;
  262. newptr = trunnel_dynarray_setlen(&inp->encrypted.allocated_,
  263. &inp->encrypted.n_, inp->encrypted.elts_, newlen,
  264. sizeof(inp->encrypted.elts_[0]), (trunnel_free_fn_t) NULL,
  265. &inp->trunnel_error_code_);
  266. if (newlen != 0 && newptr == NULL)
  267. goto trunnel_alloc_failed;
  268. inp->encrypted.elts_ = newptr;
  269. return 0;
  270. trunnel_alloc_failed:
  271. TRUNNEL_SET_ERROR_CODE(inp);
  272. return -1;
  273. }
  274. const char *
  275. hs_cell_introduce1_check(const hs_cell_introduce1_t *obj)
  276. {
  277. if (obj == NULL)
  278. return "Object was NULL";
  279. if (obj->trunnel_error_code_)
  280. return "A set function failed on this object";
  281. if (! (obj->auth_key_type == 0 || obj->auth_key_type == 1 || obj->auth_key_type == 2))
  282. return "Integer out of bounds";
  283. if (TRUNNEL_DYNARRAY_LEN(&obj->auth_key) != obj->auth_key_len)
  284. return "Length mismatch for auth_key";
  285. {
  286. const char *msg;
  287. if (NULL != (msg = cell_extension_check(obj->extensions)))
  288. return msg;
  289. }
  290. return NULL;
  291. }
  292. ssize_t
  293. hs_cell_introduce1_encoded_len(const hs_cell_introduce1_t *obj)
  294. {
  295. ssize_t result = 0;
  296. if (NULL != hs_cell_introduce1_check(obj))
  297. return -1;
  298. /* Length of u8 legacy_key_id[TRUNNEL_SHA1_LEN] */
  299. result += TRUNNEL_SHA1_LEN;
  300. /* Length of u8 auth_key_type IN [0, 1, 2] */
  301. result += 1;
  302. /* Length of u16 auth_key_len */
  303. result += 2;
  304. /* Length of u8 auth_key[auth_key_len] */
  305. result += TRUNNEL_DYNARRAY_LEN(&obj->auth_key);
  306. /* Length of struct cell_extension extensions */
  307. result += cell_extension_encoded_len(obj->extensions);
  308. /* Length of u8 encrypted[] */
  309. result += TRUNNEL_DYNARRAY_LEN(&obj->encrypted);
  310. return result;
  311. }
  312. int
  313. hs_cell_introduce1_clear_errors(hs_cell_introduce1_t *obj)
  314. {
  315. int r = obj->trunnel_error_code_;
  316. obj->trunnel_error_code_ = 0;
  317. return r;
  318. }
  319. ssize_t
  320. hs_cell_introduce1_encode(uint8_t *output, const size_t avail, const hs_cell_introduce1_t *obj)
  321. {
  322. ssize_t result = 0;
  323. size_t written = 0;
  324. uint8_t *ptr = output;
  325. const char *msg;
  326. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  327. const ssize_t encoded_len = hs_cell_introduce1_encoded_len(obj);
  328. #endif
  329. if (NULL != (msg = hs_cell_introduce1_check(obj)))
  330. goto check_failed;
  331. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  332. trunnel_assert(encoded_len >= 0);
  333. #endif
  334. /* Encode u8 legacy_key_id[TRUNNEL_SHA1_LEN] */
  335. trunnel_assert(written <= avail);
  336. if (avail - written < TRUNNEL_SHA1_LEN)
  337. goto truncated;
  338. memcpy(ptr, obj->legacy_key_id, TRUNNEL_SHA1_LEN);
  339. written += TRUNNEL_SHA1_LEN; ptr += TRUNNEL_SHA1_LEN;
  340. /* Encode u8 auth_key_type IN [0, 1, 2] */
  341. trunnel_assert(written <= avail);
  342. if (avail - written < 1)
  343. goto truncated;
  344. trunnel_set_uint8(ptr, (obj->auth_key_type));
  345. written += 1; ptr += 1;
  346. /* Encode u16 auth_key_len */
  347. trunnel_assert(written <= avail);
  348. if (avail - written < 2)
  349. goto truncated;
  350. trunnel_set_uint16(ptr, trunnel_htons(obj->auth_key_len));
  351. written += 2; ptr += 2;
  352. /* Encode u8 auth_key[auth_key_len] */
  353. {
  354. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->auth_key);
  355. trunnel_assert(obj->auth_key_len == elt_len);
  356. trunnel_assert(written <= avail);
  357. if (avail - written < elt_len)
  358. goto truncated;
  359. if (elt_len)
  360. memcpy(ptr, obj->auth_key.elts_, elt_len);
  361. written += elt_len; ptr += elt_len;
  362. }
  363. /* Encode struct cell_extension extensions */
  364. trunnel_assert(written <= avail);
  365. result = cell_extension_encode(ptr, avail - written, obj->extensions);
  366. if (result < 0)
  367. goto fail; /* XXXXXXX !*/
  368. written += result; ptr += result;
  369. /* Encode u8 encrypted[] */
  370. {
  371. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->encrypted);
  372. trunnel_assert(written <= avail);
  373. if (avail - written < elt_len)
  374. goto truncated;
  375. if (elt_len)
  376. memcpy(ptr, obj->encrypted.elts_, elt_len);
  377. written += elt_len; ptr += elt_len;
  378. }
  379. trunnel_assert(ptr == output + written);
  380. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  381. {
  382. trunnel_assert(encoded_len >= 0);
  383. trunnel_assert((size_t)encoded_len == written);
  384. }
  385. #endif
  386. return written;
  387. truncated:
  388. result = -2;
  389. goto fail;
  390. check_failed:
  391. (void)msg;
  392. result = -1;
  393. goto fail;
  394. fail:
  395. trunnel_assert(result < 0);
  396. return result;
  397. }
  398. /** As hs_cell_introduce1_parse(), but do not allocate the output
  399. * object.
  400. */
  401. static ssize_t
  402. hs_cell_introduce1_parse_into(hs_cell_introduce1_t *obj, const uint8_t *input, const size_t len_in)
  403. {
  404. const uint8_t *ptr = input;
  405. size_t remaining = len_in;
  406. ssize_t result = 0;
  407. (void)result;
  408. /* Parse u8 legacy_key_id[TRUNNEL_SHA1_LEN] */
  409. CHECK_REMAINING(TRUNNEL_SHA1_LEN, truncated);
  410. memcpy(obj->legacy_key_id, ptr, TRUNNEL_SHA1_LEN);
  411. remaining -= TRUNNEL_SHA1_LEN; ptr += TRUNNEL_SHA1_LEN;
  412. /* Parse u8 auth_key_type IN [0, 1, 2] */
  413. CHECK_REMAINING(1, truncated);
  414. obj->auth_key_type = (trunnel_get_uint8(ptr));
  415. remaining -= 1; ptr += 1;
  416. if (! (obj->auth_key_type == 0 || obj->auth_key_type == 1 || obj->auth_key_type == 2))
  417. goto fail;
  418. /* Parse u16 auth_key_len */
  419. CHECK_REMAINING(2, truncated);
  420. obj->auth_key_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  421. remaining -= 2; ptr += 2;
  422. /* Parse u8 auth_key[auth_key_len] */
  423. CHECK_REMAINING(obj->auth_key_len, truncated);
  424. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->auth_key, obj->auth_key_len, {});
  425. obj->auth_key.n_ = obj->auth_key_len;
  426. if (obj->auth_key_len)
  427. memcpy(obj->auth_key.elts_, ptr, obj->auth_key_len);
  428. ptr += obj->auth_key_len; remaining -= obj->auth_key_len;
  429. /* Parse struct cell_extension extensions */
  430. result = cell_extension_parse(&obj->extensions, ptr, remaining);
  431. if (result < 0)
  432. goto relay_fail;
  433. trunnel_assert((size_t)result <= remaining);
  434. remaining -= result; ptr += result;
  435. /* Parse u8 encrypted[] */
  436. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->encrypted, remaining, {});
  437. obj->encrypted.n_ = remaining;
  438. if (remaining)
  439. memcpy(obj->encrypted.elts_, ptr, remaining);
  440. ptr += remaining; remaining -= remaining;
  441. trunnel_assert(ptr + remaining == input + len_in);
  442. return len_in - remaining;
  443. truncated:
  444. return -2;
  445. relay_fail:
  446. trunnel_assert(result < 0);
  447. return result;
  448. trunnel_alloc_failed:
  449. return -1;
  450. fail:
  451. result = -1;
  452. return result;
  453. }
  454. ssize_t
  455. hs_cell_introduce1_parse(hs_cell_introduce1_t **output, const uint8_t *input, const size_t len_in)
  456. {
  457. ssize_t result;
  458. *output = hs_cell_introduce1_new();
  459. if (NULL == *output)
  460. return -1;
  461. result = hs_cell_introduce1_parse_into(*output, input, len_in);
  462. if (result < 0) {
  463. hs_cell_introduce1_free(*output);
  464. *output = NULL;
  465. }
  466. return result;
  467. }
  468. hs_cell_introduce_ack_t *
  469. hs_cell_introduce_ack_new(void)
  470. {
  471. hs_cell_introduce_ack_t *val = trunnel_calloc(1, sizeof(hs_cell_introduce_ack_t));
  472. if (NULL == val)
  473. return NULL;
  474. return val;
  475. }
  476. /** Release all storage held inside 'obj', but do not free 'obj'.
  477. */
  478. static void
  479. hs_cell_introduce_ack_clear(hs_cell_introduce_ack_t *obj)
  480. {
  481. (void) obj;
  482. cell_extension_free(obj->extensions);
  483. obj->extensions = NULL;
  484. }
  485. void
  486. hs_cell_introduce_ack_free(hs_cell_introduce_ack_t *obj)
  487. {
  488. if (obj == NULL)
  489. return;
  490. hs_cell_introduce_ack_clear(obj);
  491. trunnel_memwipe(obj, sizeof(hs_cell_introduce_ack_t));
  492. trunnel_free_(obj);
  493. }
  494. uint16_t
  495. hs_cell_introduce_ack_get_status(hs_cell_introduce_ack_t *inp)
  496. {
  497. return inp->status;
  498. }
  499. int
  500. hs_cell_introduce_ack_set_status(hs_cell_introduce_ack_t *inp, uint16_t val)
  501. {
  502. if (! ((val == 0 || val == 1 || val == 2))) {
  503. TRUNNEL_SET_ERROR_CODE(inp);
  504. return -1;
  505. }
  506. inp->status = val;
  507. return 0;
  508. }
  509. struct cell_extension_st *
  510. hs_cell_introduce_ack_get_extensions(hs_cell_introduce_ack_t *inp)
  511. {
  512. return inp->extensions;
  513. }
  514. const struct cell_extension_st *
  515. hs_cell_introduce_ack_getconst_extensions(const hs_cell_introduce_ack_t *inp)
  516. {
  517. return hs_cell_introduce_ack_get_extensions((hs_cell_introduce_ack_t*) inp);
  518. }
  519. int
  520. hs_cell_introduce_ack_set_extensions(hs_cell_introduce_ack_t *inp, struct cell_extension_st *val)
  521. {
  522. if (inp->extensions && inp->extensions != val)
  523. cell_extension_free(inp->extensions);
  524. return hs_cell_introduce_ack_set0_extensions(inp, val);
  525. }
  526. int
  527. hs_cell_introduce_ack_set0_extensions(hs_cell_introduce_ack_t *inp, struct cell_extension_st *val)
  528. {
  529. inp->extensions = val;
  530. return 0;
  531. }
  532. const char *
  533. hs_cell_introduce_ack_check(const hs_cell_introduce_ack_t *obj)
  534. {
  535. if (obj == NULL)
  536. return "Object was NULL";
  537. if (obj->trunnel_error_code_)
  538. return "A set function failed on this object";
  539. if (! (obj->status == 0 || obj->status == 1 || obj->status == 2))
  540. return "Integer out of bounds";
  541. {
  542. const char *msg;
  543. if (NULL != (msg = cell_extension_check(obj->extensions)))
  544. return msg;
  545. }
  546. return NULL;
  547. }
  548. ssize_t
  549. hs_cell_introduce_ack_encoded_len(const hs_cell_introduce_ack_t *obj)
  550. {
  551. ssize_t result = 0;
  552. if (NULL != hs_cell_introduce_ack_check(obj))
  553. return -1;
  554. /* Length of u16 status IN [0, 1, 2] */
  555. result += 2;
  556. /* Length of struct cell_extension extensions */
  557. result += cell_extension_encoded_len(obj->extensions);
  558. return result;
  559. }
  560. int
  561. hs_cell_introduce_ack_clear_errors(hs_cell_introduce_ack_t *obj)
  562. {
  563. int r = obj->trunnel_error_code_;
  564. obj->trunnel_error_code_ = 0;
  565. return r;
  566. }
  567. ssize_t
  568. hs_cell_introduce_ack_encode(uint8_t *output, const size_t avail, const hs_cell_introduce_ack_t *obj)
  569. {
  570. ssize_t result = 0;
  571. size_t written = 0;
  572. uint8_t *ptr = output;
  573. const char *msg;
  574. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  575. const ssize_t encoded_len = hs_cell_introduce_ack_encoded_len(obj);
  576. #endif
  577. if (NULL != (msg = hs_cell_introduce_ack_check(obj)))
  578. goto check_failed;
  579. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  580. trunnel_assert(encoded_len >= 0);
  581. #endif
  582. /* Encode u16 status IN [0, 1, 2] */
  583. trunnel_assert(written <= avail);
  584. if (avail - written < 2)
  585. goto truncated;
  586. trunnel_set_uint16(ptr, trunnel_htons(obj->status));
  587. written += 2; ptr += 2;
  588. /* Encode struct cell_extension extensions */
  589. trunnel_assert(written <= avail);
  590. result = cell_extension_encode(ptr, avail - written, obj->extensions);
  591. if (result < 0)
  592. goto fail; /* XXXXXXX !*/
  593. written += result; ptr += result;
  594. trunnel_assert(ptr == output + written);
  595. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  596. {
  597. trunnel_assert(encoded_len >= 0);
  598. trunnel_assert((size_t)encoded_len == written);
  599. }
  600. #endif
  601. return written;
  602. truncated:
  603. result = -2;
  604. goto fail;
  605. check_failed:
  606. (void)msg;
  607. result = -1;
  608. goto fail;
  609. fail:
  610. trunnel_assert(result < 0);
  611. return result;
  612. }
  613. /** As hs_cell_introduce_ack_parse(), but do not allocate the output
  614. * object.
  615. */
  616. static ssize_t
  617. hs_cell_introduce_ack_parse_into(hs_cell_introduce_ack_t *obj, const uint8_t *input, const size_t len_in)
  618. {
  619. const uint8_t *ptr = input;
  620. size_t remaining = len_in;
  621. ssize_t result = 0;
  622. (void)result;
  623. /* Parse u16 status IN [0, 1, 2] */
  624. CHECK_REMAINING(2, truncated);
  625. obj->status = trunnel_ntohs(trunnel_get_uint16(ptr));
  626. remaining -= 2; ptr += 2;
  627. if (! (obj->status == 0 || obj->status == 1 || obj->status == 2))
  628. goto fail;
  629. /* Parse struct cell_extension extensions */
  630. result = cell_extension_parse(&obj->extensions, ptr, remaining);
  631. if (result < 0)
  632. goto relay_fail;
  633. trunnel_assert((size_t)result <= remaining);
  634. remaining -= result; ptr += result;
  635. trunnel_assert(ptr + remaining == input + len_in);
  636. return len_in - remaining;
  637. truncated:
  638. return -2;
  639. relay_fail:
  640. trunnel_assert(result < 0);
  641. return result;
  642. fail:
  643. result = -1;
  644. return result;
  645. }
  646. ssize_t
  647. hs_cell_introduce_ack_parse(hs_cell_introduce_ack_t **output, const uint8_t *input, const size_t len_in)
  648. {
  649. ssize_t result;
  650. *output = hs_cell_introduce_ack_new();
  651. if (NULL == *output)
  652. return -1;
  653. result = hs_cell_introduce_ack_parse_into(*output, input, len_in);
  654. if (result < 0) {
  655. hs_cell_introduce_ack_free(*output);
  656. *output = NULL;
  657. }
  658. return result;
  659. }
  660. hs_cell_introduce_encrypted_t *
  661. hs_cell_introduce_encrypted_new(void)
  662. {
  663. hs_cell_introduce_encrypted_t *val = trunnel_calloc(1, sizeof(hs_cell_introduce_encrypted_t));
  664. if (NULL == val)
  665. return NULL;
  666. return val;
  667. }
  668. /** Release all storage held inside 'obj', but do not free 'obj'.
  669. */
  670. static void
  671. hs_cell_introduce_encrypted_clear(hs_cell_introduce_encrypted_t *obj)
  672. {
  673. (void) obj;
  674. cell_extension_free(obj->extensions);
  675. obj->extensions = NULL;
  676. TRUNNEL_DYNARRAY_WIPE(&obj->onion_key);
  677. TRUNNEL_DYNARRAY_CLEAR(&obj->onion_key);
  678. {
  679. unsigned idx;
  680. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  681. link_specifier_free(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  682. }
  683. }
  684. TRUNNEL_DYNARRAY_WIPE(&obj->nspecs);
  685. TRUNNEL_DYNARRAY_CLEAR(&obj->nspecs);
  686. TRUNNEL_DYNARRAY_WIPE(&obj->pad);
  687. TRUNNEL_DYNARRAY_CLEAR(&obj->pad);
  688. }
  689. void
  690. hs_cell_introduce_encrypted_free(hs_cell_introduce_encrypted_t *obj)
  691. {
  692. if (obj == NULL)
  693. return;
  694. hs_cell_introduce_encrypted_clear(obj);
  695. trunnel_memwipe(obj, sizeof(hs_cell_introduce_encrypted_t));
  696. trunnel_free_(obj);
  697. }
  698. size_t
  699. hs_cell_introduce_encrypted_getlen_rend_cookie(const hs_cell_introduce_encrypted_t *inp)
  700. {
  701. (void)inp; return TRUNNEL_REND_COOKIE_LEN;
  702. }
  703. uint8_t
  704. hs_cell_introduce_encrypted_get_rend_cookie(hs_cell_introduce_encrypted_t *inp, size_t idx)
  705. {
  706. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  707. return inp->rend_cookie[idx];
  708. }
  709. uint8_t
  710. hs_cell_introduce_encrypted_getconst_rend_cookie(const hs_cell_introduce_encrypted_t *inp, size_t idx)
  711. {
  712. return hs_cell_introduce_encrypted_get_rend_cookie((hs_cell_introduce_encrypted_t*)inp, idx);
  713. }
  714. int
  715. hs_cell_introduce_encrypted_set_rend_cookie(hs_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  716. {
  717. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  718. inp->rend_cookie[idx] = elt;
  719. return 0;
  720. }
  721. uint8_t *
  722. hs_cell_introduce_encrypted_getarray_rend_cookie(hs_cell_introduce_encrypted_t *inp)
  723. {
  724. return inp->rend_cookie;
  725. }
  726. const uint8_t *
  727. hs_cell_introduce_encrypted_getconstarray_rend_cookie(const hs_cell_introduce_encrypted_t *inp)
  728. {
  729. return (const uint8_t *)hs_cell_introduce_encrypted_getarray_rend_cookie((hs_cell_introduce_encrypted_t*)inp);
  730. }
  731. struct cell_extension_st *
  732. hs_cell_introduce_encrypted_get_extensions(hs_cell_introduce_encrypted_t *inp)
  733. {
  734. return inp->extensions;
  735. }
  736. const struct cell_extension_st *
  737. hs_cell_introduce_encrypted_getconst_extensions(const hs_cell_introduce_encrypted_t *inp)
  738. {
  739. return hs_cell_introduce_encrypted_get_extensions((hs_cell_introduce_encrypted_t*) inp);
  740. }
  741. int
  742. hs_cell_introduce_encrypted_set_extensions(hs_cell_introduce_encrypted_t *inp, struct cell_extension_st *val)
  743. {
  744. if (inp->extensions && inp->extensions != val)
  745. cell_extension_free(inp->extensions);
  746. return hs_cell_introduce_encrypted_set0_extensions(inp, val);
  747. }
  748. int
  749. hs_cell_introduce_encrypted_set0_extensions(hs_cell_introduce_encrypted_t *inp, struct cell_extension_st *val)
  750. {
  751. inp->extensions = val;
  752. return 0;
  753. }
  754. uint8_t
  755. hs_cell_introduce_encrypted_get_onion_key_type(hs_cell_introduce_encrypted_t *inp)
  756. {
  757. return inp->onion_key_type;
  758. }
  759. int
  760. hs_cell_introduce_encrypted_set_onion_key_type(hs_cell_introduce_encrypted_t *inp, uint8_t val)
  761. {
  762. inp->onion_key_type = val;
  763. return 0;
  764. }
  765. uint16_t
  766. hs_cell_introduce_encrypted_get_onion_key_len(hs_cell_introduce_encrypted_t *inp)
  767. {
  768. return inp->onion_key_len;
  769. }
  770. int
  771. hs_cell_introduce_encrypted_set_onion_key_len(hs_cell_introduce_encrypted_t *inp, uint16_t val)
  772. {
  773. inp->onion_key_len = val;
  774. return 0;
  775. }
  776. size_t
  777. hs_cell_introduce_encrypted_getlen_onion_key(const hs_cell_introduce_encrypted_t *inp)
  778. {
  779. return TRUNNEL_DYNARRAY_LEN(&inp->onion_key);
  780. }
  781. uint8_t
  782. hs_cell_introduce_encrypted_get_onion_key(hs_cell_introduce_encrypted_t *inp, size_t idx)
  783. {
  784. return TRUNNEL_DYNARRAY_GET(&inp->onion_key, idx);
  785. }
  786. uint8_t
  787. hs_cell_introduce_encrypted_getconst_onion_key(const hs_cell_introduce_encrypted_t *inp, size_t idx)
  788. {
  789. return hs_cell_introduce_encrypted_get_onion_key((hs_cell_introduce_encrypted_t*)inp, idx);
  790. }
  791. int
  792. hs_cell_introduce_encrypted_set_onion_key(hs_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  793. {
  794. TRUNNEL_DYNARRAY_SET(&inp->onion_key, idx, elt);
  795. return 0;
  796. }
  797. int
  798. hs_cell_introduce_encrypted_add_onion_key(hs_cell_introduce_encrypted_t *inp, uint8_t elt)
  799. {
  800. #if SIZE_MAX >= UINT16_MAX
  801. if (inp->onion_key.n_ == UINT16_MAX)
  802. goto trunnel_alloc_failed;
  803. #endif
  804. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->onion_key, elt, {});
  805. return 0;
  806. trunnel_alloc_failed:
  807. TRUNNEL_SET_ERROR_CODE(inp);
  808. return -1;
  809. }
  810. uint8_t *
  811. hs_cell_introduce_encrypted_getarray_onion_key(hs_cell_introduce_encrypted_t *inp)
  812. {
  813. return inp->onion_key.elts_;
  814. }
  815. const uint8_t *
  816. hs_cell_introduce_encrypted_getconstarray_onion_key(const hs_cell_introduce_encrypted_t *inp)
  817. {
  818. return (const uint8_t *)hs_cell_introduce_encrypted_getarray_onion_key((hs_cell_introduce_encrypted_t*)inp);
  819. }
  820. int
  821. hs_cell_introduce_encrypted_setlen_onion_key(hs_cell_introduce_encrypted_t *inp, size_t newlen)
  822. {
  823. uint8_t *newptr;
  824. #if UINT16_MAX < SIZE_MAX
  825. if (newlen > UINT16_MAX)
  826. goto trunnel_alloc_failed;
  827. #endif
  828. newptr = trunnel_dynarray_setlen(&inp->onion_key.allocated_,
  829. &inp->onion_key.n_, inp->onion_key.elts_, newlen,
  830. sizeof(inp->onion_key.elts_[0]), (trunnel_free_fn_t) NULL,
  831. &inp->trunnel_error_code_);
  832. if (newlen != 0 && newptr == NULL)
  833. goto trunnel_alloc_failed;
  834. inp->onion_key.elts_ = newptr;
  835. return 0;
  836. trunnel_alloc_failed:
  837. TRUNNEL_SET_ERROR_CODE(inp);
  838. return -1;
  839. }
  840. uint8_t
  841. hs_cell_introduce_encrypted_get_nspec(hs_cell_introduce_encrypted_t *inp)
  842. {
  843. return inp->nspec;
  844. }
  845. int
  846. hs_cell_introduce_encrypted_set_nspec(hs_cell_introduce_encrypted_t *inp, uint8_t val)
  847. {
  848. inp->nspec = val;
  849. return 0;
  850. }
  851. size_t
  852. hs_cell_introduce_encrypted_getlen_nspecs(const hs_cell_introduce_encrypted_t *inp)
  853. {
  854. return TRUNNEL_DYNARRAY_LEN(&inp->nspecs);
  855. }
  856. struct link_specifier_st *
  857. hs_cell_introduce_encrypted_get_nspecs(hs_cell_introduce_encrypted_t *inp, size_t idx)
  858. {
  859. return TRUNNEL_DYNARRAY_GET(&inp->nspecs, idx);
  860. }
  861. const struct link_specifier_st *
  862. hs_cell_introduce_encrypted_getconst_nspecs(const hs_cell_introduce_encrypted_t *inp, size_t idx)
  863. {
  864. return hs_cell_introduce_encrypted_get_nspecs((hs_cell_introduce_encrypted_t*)inp, idx);
  865. }
  866. int
  867. hs_cell_introduce_encrypted_set_nspecs(hs_cell_introduce_encrypted_t *inp, size_t idx, struct link_specifier_st * elt)
  868. {
  869. link_specifier_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->nspecs, idx);
  870. if (oldval && oldval != elt)
  871. link_specifier_free(oldval);
  872. return hs_cell_introduce_encrypted_set0_nspecs(inp, idx, elt);
  873. }
  874. int
  875. hs_cell_introduce_encrypted_set0_nspecs(hs_cell_introduce_encrypted_t *inp, size_t idx, struct link_specifier_st * elt)
  876. {
  877. TRUNNEL_DYNARRAY_SET(&inp->nspecs, idx, elt);
  878. return 0;
  879. }
  880. int
  881. hs_cell_introduce_encrypted_add_nspecs(hs_cell_introduce_encrypted_t *inp, struct link_specifier_st * elt)
  882. {
  883. #if SIZE_MAX >= UINT8_MAX
  884. if (inp->nspecs.n_ == UINT8_MAX)
  885. goto trunnel_alloc_failed;
  886. #endif
  887. TRUNNEL_DYNARRAY_ADD(struct link_specifier_st *, &inp->nspecs, elt, {});
  888. return 0;
  889. trunnel_alloc_failed:
  890. TRUNNEL_SET_ERROR_CODE(inp);
  891. return -1;
  892. }
  893. struct link_specifier_st * *
  894. hs_cell_introduce_encrypted_getarray_nspecs(hs_cell_introduce_encrypted_t *inp)
  895. {
  896. return inp->nspecs.elts_;
  897. }
  898. const struct link_specifier_st * const *
  899. hs_cell_introduce_encrypted_getconstarray_nspecs(const hs_cell_introduce_encrypted_t *inp)
  900. {
  901. return (const struct link_specifier_st * const *)hs_cell_introduce_encrypted_getarray_nspecs((hs_cell_introduce_encrypted_t*)inp);
  902. }
  903. int
  904. hs_cell_introduce_encrypted_setlen_nspecs(hs_cell_introduce_encrypted_t *inp, size_t newlen)
  905. {
  906. struct link_specifier_st * *newptr;
  907. #if UINT8_MAX < SIZE_MAX
  908. if (newlen > UINT8_MAX)
  909. goto trunnel_alloc_failed;
  910. #endif
  911. newptr = trunnel_dynarray_setlen(&inp->nspecs.allocated_,
  912. &inp->nspecs.n_, inp->nspecs.elts_, newlen,
  913. sizeof(inp->nspecs.elts_[0]), (trunnel_free_fn_t) link_specifier_free,
  914. &inp->trunnel_error_code_);
  915. if (newlen != 0 && newptr == NULL)
  916. goto trunnel_alloc_failed;
  917. inp->nspecs.elts_ = newptr;
  918. return 0;
  919. trunnel_alloc_failed:
  920. TRUNNEL_SET_ERROR_CODE(inp);
  921. return -1;
  922. }
  923. size_t
  924. hs_cell_introduce_encrypted_getlen_pad(const hs_cell_introduce_encrypted_t *inp)
  925. {
  926. return TRUNNEL_DYNARRAY_LEN(&inp->pad);
  927. }
  928. uint8_t
  929. hs_cell_introduce_encrypted_get_pad(hs_cell_introduce_encrypted_t *inp, size_t idx)
  930. {
  931. return TRUNNEL_DYNARRAY_GET(&inp->pad, idx);
  932. }
  933. uint8_t
  934. hs_cell_introduce_encrypted_getconst_pad(const hs_cell_introduce_encrypted_t *inp, size_t idx)
  935. {
  936. return hs_cell_introduce_encrypted_get_pad((hs_cell_introduce_encrypted_t*)inp, idx);
  937. }
  938. int
  939. hs_cell_introduce_encrypted_set_pad(hs_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  940. {
  941. TRUNNEL_DYNARRAY_SET(&inp->pad, idx, elt);
  942. return 0;
  943. }
  944. int
  945. hs_cell_introduce_encrypted_add_pad(hs_cell_introduce_encrypted_t *inp, uint8_t elt)
  946. {
  947. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->pad, elt, {});
  948. return 0;
  949. trunnel_alloc_failed:
  950. TRUNNEL_SET_ERROR_CODE(inp);
  951. return -1;
  952. }
  953. uint8_t *
  954. hs_cell_introduce_encrypted_getarray_pad(hs_cell_introduce_encrypted_t *inp)
  955. {
  956. return inp->pad.elts_;
  957. }
  958. const uint8_t *
  959. hs_cell_introduce_encrypted_getconstarray_pad(const hs_cell_introduce_encrypted_t *inp)
  960. {
  961. return (const uint8_t *)hs_cell_introduce_encrypted_getarray_pad((hs_cell_introduce_encrypted_t*)inp);
  962. }
  963. int
  964. hs_cell_introduce_encrypted_setlen_pad(hs_cell_introduce_encrypted_t *inp, size_t newlen)
  965. {
  966. uint8_t *newptr;
  967. newptr = trunnel_dynarray_setlen(&inp->pad.allocated_,
  968. &inp->pad.n_, inp->pad.elts_, newlen,
  969. sizeof(inp->pad.elts_[0]), (trunnel_free_fn_t) NULL,
  970. &inp->trunnel_error_code_);
  971. if (newlen != 0 && newptr == NULL)
  972. goto trunnel_alloc_failed;
  973. inp->pad.elts_ = newptr;
  974. return 0;
  975. trunnel_alloc_failed:
  976. TRUNNEL_SET_ERROR_CODE(inp);
  977. return -1;
  978. }
  979. const char *
  980. hs_cell_introduce_encrypted_check(const hs_cell_introduce_encrypted_t *obj)
  981. {
  982. if (obj == NULL)
  983. return "Object was NULL";
  984. if (obj->trunnel_error_code_)
  985. return "A set function failed on this object";
  986. {
  987. const char *msg;
  988. if (NULL != (msg = cell_extension_check(obj->extensions)))
  989. return msg;
  990. }
  991. if (TRUNNEL_DYNARRAY_LEN(&obj->onion_key) != obj->onion_key_len)
  992. return "Length mismatch for onion_key";
  993. {
  994. const char *msg;
  995. unsigned idx;
  996. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  997. if (NULL != (msg = link_specifier_check(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx))))
  998. return msg;
  999. }
  1000. }
  1001. if (TRUNNEL_DYNARRAY_LEN(&obj->nspecs) != obj->nspec)
  1002. return "Length mismatch for nspecs";
  1003. return NULL;
  1004. }
  1005. ssize_t
  1006. hs_cell_introduce_encrypted_encoded_len(const hs_cell_introduce_encrypted_t *obj)
  1007. {
  1008. ssize_t result = 0;
  1009. if (NULL != hs_cell_introduce_encrypted_check(obj))
  1010. return -1;
  1011. /* Length of u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1012. result += TRUNNEL_REND_COOKIE_LEN;
  1013. /* Length of struct cell_extension extensions */
  1014. result += cell_extension_encoded_len(obj->extensions);
  1015. /* Length of u8 onion_key_type */
  1016. result += 1;
  1017. /* Length of u16 onion_key_len */
  1018. result += 2;
  1019. /* Length of u8 onion_key[onion_key_len] */
  1020. result += TRUNNEL_DYNARRAY_LEN(&obj->onion_key);
  1021. /* Length of u8 nspec */
  1022. result += 1;
  1023. /* Length of struct link_specifier nspecs[nspec] */
  1024. {
  1025. unsigned idx;
  1026. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  1027. result += link_specifier_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  1028. }
  1029. }
  1030. /* Length of u8 pad[] */
  1031. result += TRUNNEL_DYNARRAY_LEN(&obj->pad);
  1032. return result;
  1033. }
  1034. int
  1035. hs_cell_introduce_encrypted_clear_errors(hs_cell_introduce_encrypted_t *obj)
  1036. {
  1037. int r = obj->trunnel_error_code_;
  1038. obj->trunnel_error_code_ = 0;
  1039. return r;
  1040. }
  1041. ssize_t
  1042. hs_cell_introduce_encrypted_encode(uint8_t *output, const size_t avail, const hs_cell_introduce_encrypted_t *obj)
  1043. {
  1044. ssize_t result = 0;
  1045. size_t written = 0;
  1046. uint8_t *ptr = output;
  1047. const char *msg;
  1048. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1049. const ssize_t encoded_len = hs_cell_introduce_encrypted_encoded_len(obj);
  1050. #endif
  1051. if (NULL != (msg = hs_cell_introduce_encrypted_check(obj)))
  1052. goto check_failed;
  1053. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1054. trunnel_assert(encoded_len >= 0);
  1055. #endif
  1056. /* Encode u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1057. trunnel_assert(written <= avail);
  1058. if (avail - written < TRUNNEL_REND_COOKIE_LEN)
  1059. goto truncated;
  1060. memcpy(ptr, obj->rend_cookie, TRUNNEL_REND_COOKIE_LEN);
  1061. written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  1062. /* Encode struct cell_extension extensions */
  1063. trunnel_assert(written <= avail);
  1064. result = cell_extension_encode(ptr, avail - written, obj->extensions);
  1065. if (result < 0)
  1066. goto fail; /* XXXXXXX !*/
  1067. written += result; ptr += result;
  1068. /* Encode u8 onion_key_type */
  1069. trunnel_assert(written <= avail);
  1070. if (avail - written < 1)
  1071. goto truncated;
  1072. trunnel_set_uint8(ptr, (obj->onion_key_type));
  1073. written += 1; ptr += 1;
  1074. /* Encode u16 onion_key_len */
  1075. trunnel_assert(written <= avail);
  1076. if (avail - written < 2)
  1077. goto truncated;
  1078. trunnel_set_uint16(ptr, trunnel_htons(obj->onion_key_len));
  1079. written += 2; ptr += 2;
  1080. /* Encode u8 onion_key[onion_key_len] */
  1081. {
  1082. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->onion_key);
  1083. trunnel_assert(obj->onion_key_len == elt_len);
  1084. trunnel_assert(written <= avail);
  1085. if (avail - written < elt_len)
  1086. goto truncated;
  1087. if (elt_len)
  1088. memcpy(ptr, obj->onion_key.elts_, elt_len);
  1089. written += elt_len; ptr += elt_len;
  1090. }
  1091. /* Encode u8 nspec */
  1092. trunnel_assert(written <= avail);
  1093. if (avail - written < 1)
  1094. goto truncated;
  1095. trunnel_set_uint8(ptr, (obj->nspec));
  1096. written += 1; ptr += 1;
  1097. /* Encode struct link_specifier nspecs[nspec] */
  1098. {
  1099. unsigned idx;
  1100. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  1101. trunnel_assert(written <= avail);
  1102. result = link_specifier_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  1103. if (result < 0)
  1104. goto fail; /* XXXXXXX !*/
  1105. written += result; ptr += result;
  1106. }
  1107. }
  1108. /* Encode u8 pad[] */
  1109. {
  1110. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->pad);
  1111. trunnel_assert(written <= avail);
  1112. if (avail - written < elt_len)
  1113. goto truncated;
  1114. if (elt_len)
  1115. memcpy(ptr, obj->pad.elts_, elt_len);
  1116. written += elt_len; ptr += elt_len;
  1117. }
  1118. trunnel_assert(ptr == output + written);
  1119. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1120. {
  1121. trunnel_assert(encoded_len >= 0);
  1122. trunnel_assert((size_t)encoded_len == written);
  1123. }
  1124. #endif
  1125. return written;
  1126. truncated:
  1127. result = -2;
  1128. goto fail;
  1129. check_failed:
  1130. (void)msg;
  1131. result = -1;
  1132. goto fail;
  1133. fail:
  1134. trunnel_assert(result < 0);
  1135. return result;
  1136. }
  1137. /** As hs_cell_introduce_encrypted_parse(), but do not allocate the
  1138. * output object.
  1139. */
  1140. static ssize_t
  1141. hs_cell_introduce_encrypted_parse_into(hs_cell_introduce_encrypted_t *obj, const uint8_t *input, const size_t len_in)
  1142. {
  1143. const uint8_t *ptr = input;
  1144. size_t remaining = len_in;
  1145. ssize_t result = 0;
  1146. (void)result;
  1147. /* Parse u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1148. CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated);
  1149. memcpy(obj->rend_cookie, ptr, TRUNNEL_REND_COOKIE_LEN);
  1150. remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  1151. /* Parse struct cell_extension extensions */
  1152. result = cell_extension_parse(&obj->extensions, ptr, remaining);
  1153. if (result < 0)
  1154. goto relay_fail;
  1155. trunnel_assert((size_t)result <= remaining);
  1156. remaining -= result; ptr += result;
  1157. /* Parse u8 onion_key_type */
  1158. CHECK_REMAINING(1, truncated);
  1159. obj->onion_key_type = (trunnel_get_uint8(ptr));
  1160. remaining -= 1; ptr += 1;
  1161. /* Parse u16 onion_key_len */
  1162. CHECK_REMAINING(2, truncated);
  1163. obj->onion_key_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  1164. remaining -= 2; ptr += 2;
  1165. /* Parse u8 onion_key[onion_key_len] */
  1166. CHECK_REMAINING(obj->onion_key_len, truncated);
  1167. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->onion_key, obj->onion_key_len, {});
  1168. obj->onion_key.n_ = obj->onion_key_len;
  1169. if (obj->onion_key_len)
  1170. memcpy(obj->onion_key.elts_, ptr, obj->onion_key_len);
  1171. ptr += obj->onion_key_len; remaining -= obj->onion_key_len;
  1172. /* Parse u8 nspec */
  1173. CHECK_REMAINING(1, truncated);
  1174. obj->nspec = (trunnel_get_uint8(ptr));
  1175. remaining -= 1; ptr += 1;
  1176. /* Parse struct link_specifier nspecs[nspec] */
  1177. TRUNNEL_DYNARRAY_EXPAND(link_specifier_t *, &obj->nspecs, obj->nspec, {});
  1178. {
  1179. link_specifier_t * elt;
  1180. unsigned idx;
  1181. for (idx = 0; idx < obj->nspec; ++idx) {
  1182. result = link_specifier_parse(&elt, ptr, remaining);
  1183. if (result < 0)
  1184. goto relay_fail;
  1185. trunnel_assert((size_t)result <= remaining);
  1186. remaining -= result; ptr += result;
  1187. TRUNNEL_DYNARRAY_ADD(link_specifier_t *, &obj->nspecs, elt, {link_specifier_free(elt);});
  1188. }
  1189. }
  1190. /* Parse u8 pad[] */
  1191. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->pad, remaining, {});
  1192. obj->pad.n_ = remaining;
  1193. if (remaining)
  1194. memcpy(obj->pad.elts_, ptr, remaining);
  1195. ptr += remaining; remaining -= remaining;
  1196. trunnel_assert(ptr + remaining == input + len_in);
  1197. return len_in - remaining;
  1198. truncated:
  1199. return -2;
  1200. relay_fail:
  1201. trunnel_assert(result < 0);
  1202. return result;
  1203. trunnel_alloc_failed:
  1204. return -1;
  1205. }
  1206. ssize_t
  1207. hs_cell_introduce_encrypted_parse(hs_cell_introduce_encrypted_t **output, const uint8_t *input, const size_t len_in)
  1208. {
  1209. ssize_t result;
  1210. *output = hs_cell_introduce_encrypted_new();
  1211. if (NULL == *output)
  1212. return -1;
  1213. result = hs_cell_introduce_encrypted_parse_into(*output, input, len_in);
  1214. if (result < 0) {
  1215. hs_cell_introduce_encrypted_free(*output);
  1216. *output = NULL;
  1217. }
  1218. return result;
  1219. }