cell_introduce1.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /* cell_introduce1.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_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 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 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 trn_cell_extension_st trn_cell_extension_t;
  27. trn_cell_extension_t *trn_cell_extension_new(void);
  28. void trn_cell_extension_free(trn_cell_extension_t *victim);
  29. ssize_t trn_cell_extension_parse(trn_cell_extension_t **output, const uint8_t *input, const size_t len_in);
  30. ssize_t trn_cell_extension_encoded_len(const trn_cell_extension_t *obj);
  31. ssize_t trn_cell_extension_encode(uint8_t *output, size_t avail, const trn_cell_extension_t *input);
  32. const char *trn_cell_extension_check(const trn_cell_extension_t *obj);
  33. int trn_cell_extension_clear_errors(trn_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. trn_cell_introduce1_t *
  43. trn_cell_introduce1_new(void)
  44. {
  45. trn_cell_introduce1_t *val = trunnel_calloc(1, sizeof(trn_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. trn_cell_introduce1_clear(trn_cell_introduce1_t *obj)
  54. {
  55. (void) obj;
  56. TRUNNEL_DYNARRAY_WIPE(&obj->auth_key);
  57. TRUNNEL_DYNARRAY_CLEAR(&obj->auth_key);
  58. trn_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. trn_cell_introduce1_free(trn_cell_introduce1_t *obj)
  65. {
  66. if (obj == NULL)
  67. return;
  68. trn_cell_introduce1_clear(obj);
  69. trunnel_memwipe(obj, sizeof(trn_cell_introduce1_t));
  70. trunnel_free_(obj);
  71. }
  72. size_t
  73. trn_cell_introduce1_getlen_legacy_key_id(const trn_cell_introduce1_t *inp)
  74. {
  75. (void)inp; return TRUNNEL_SHA1_LEN;
  76. }
  77. uint8_t
  78. trn_cell_introduce1_get_legacy_key_id(trn_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. trn_cell_introduce1_getconst_legacy_key_id(const trn_cell_introduce1_t *inp, size_t idx)
  85. {
  86. return trn_cell_introduce1_get_legacy_key_id((trn_cell_introduce1_t*)inp, idx);
  87. }
  88. int
  89. trn_cell_introduce1_set_legacy_key_id(trn_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. trn_cell_introduce1_getarray_legacy_key_id(trn_cell_introduce1_t *inp)
  97. {
  98. return inp->legacy_key_id;
  99. }
  100. const uint8_t *
  101. trn_cell_introduce1_getconstarray_legacy_key_id(const trn_cell_introduce1_t *inp)
  102. {
  103. return (const uint8_t *)trn_cell_introduce1_getarray_legacy_key_id((trn_cell_introduce1_t*)inp);
  104. }
  105. uint8_t
  106. trn_cell_introduce1_get_auth_key_type(const trn_cell_introduce1_t *inp)
  107. {
  108. return inp->auth_key_type;
  109. }
  110. int
  111. trn_cell_introduce1_set_auth_key_type(trn_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. trn_cell_introduce1_get_auth_key_len(const trn_cell_introduce1_t *inp)
  122. {
  123. return inp->auth_key_len;
  124. }
  125. int
  126. trn_cell_introduce1_set_auth_key_len(trn_cell_introduce1_t *inp, uint16_t val)
  127. {
  128. inp->auth_key_len = val;
  129. return 0;
  130. }
  131. size_t
  132. trn_cell_introduce1_getlen_auth_key(const trn_cell_introduce1_t *inp)
  133. {
  134. return TRUNNEL_DYNARRAY_LEN(&inp->auth_key);
  135. }
  136. uint8_t
  137. trn_cell_introduce1_get_auth_key(trn_cell_introduce1_t *inp, size_t idx)
  138. {
  139. return TRUNNEL_DYNARRAY_GET(&inp->auth_key, idx);
  140. }
  141. uint8_t
  142. trn_cell_introduce1_getconst_auth_key(const trn_cell_introduce1_t *inp, size_t idx)
  143. {
  144. return trn_cell_introduce1_get_auth_key((trn_cell_introduce1_t*)inp, idx);
  145. }
  146. int
  147. trn_cell_introduce1_set_auth_key(trn_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. trn_cell_introduce1_add_auth_key(trn_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. trn_cell_introduce1_getarray_auth_key(trn_cell_introduce1_t *inp)
  167. {
  168. return inp->auth_key.elts_;
  169. }
  170. const uint8_t *
  171. trn_cell_introduce1_getconstarray_auth_key(const trn_cell_introduce1_t *inp)
  172. {
  173. return (const uint8_t *)trn_cell_introduce1_getarray_auth_key((trn_cell_introduce1_t*)inp);
  174. }
  175. int
  176. trn_cell_introduce1_setlen_auth_key(trn_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 trn_cell_extension_st *
  196. trn_cell_introduce1_get_extensions(trn_cell_introduce1_t *inp)
  197. {
  198. return inp->extensions;
  199. }
  200. const struct trn_cell_extension_st *
  201. trn_cell_introduce1_getconst_extensions(const trn_cell_introduce1_t *inp)
  202. {
  203. return trn_cell_introduce1_get_extensions((trn_cell_introduce1_t*) inp);
  204. }
  205. int
  206. trn_cell_introduce1_set_extensions(trn_cell_introduce1_t *inp, struct trn_cell_extension_st *val)
  207. {
  208. if (inp->extensions && inp->extensions != val)
  209. trn_cell_extension_free(inp->extensions);
  210. return trn_cell_introduce1_set0_extensions(inp, val);
  211. }
  212. int
  213. trn_cell_introduce1_set0_extensions(trn_cell_introduce1_t *inp, struct trn_cell_extension_st *val)
  214. {
  215. inp->extensions = val;
  216. return 0;
  217. }
  218. size_t
  219. trn_cell_introduce1_getlen_encrypted(const trn_cell_introduce1_t *inp)
  220. {
  221. return TRUNNEL_DYNARRAY_LEN(&inp->encrypted);
  222. }
  223. uint8_t
  224. trn_cell_introduce1_get_encrypted(trn_cell_introduce1_t *inp, size_t idx)
  225. {
  226. return TRUNNEL_DYNARRAY_GET(&inp->encrypted, idx);
  227. }
  228. uint8_t
  229. trn_cell_introduce1_getconst_encrypted(const trn_cell_introduce1_t *inp, size_t idx)
  230. {
  231. return trn_cell_introduce1_get_encrypted((trn_cell_introduce1_t*)inp, idx);
  232. }
  233. int
  234. trn_cell_introduce1_set_encrypted(trn_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. trn_cell_introduce1_add_encrypted(trn_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. trn_cell_introduce1_getarray_encrypted(trn_cell_introduce1_t *inp)
  250. {
  251. return inp->encrypted.elts_;
  252. }
  253. const uint8_t *
  254. trn_cell_introduce1_getconstarray_encrypted(const trn_cell_introduce1_t *inp)
  255. {
  256. return (const uint8_t *)trn_cell_introduce1_getarray_encrypted((trn_cell_introduce1_t*)inp);
  257. }
  258. int
  259. trn_cell_introduce1_setlen_encrypted(trn_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. trn_cell_introduce1_check(const trn_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 = trn_cell_extension_check(obj->extensions)))
  288. return msg;
  289. }
  290. return NULL;
  291. }
  292. ssize_t
  293. trn_cell_introduce1_encoded_len(const trn_cell_introduce1_t *obj)
  294. {
  295. ssize_t result = 0;
  296. if (NULL != trn_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 trn_cell_extension extensions */
  307. result += trn_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. trn_cell_introduce1_clear_errors(trn_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. trn_cell_introduce1_encode(uint8_t *output, const size_t avail, const trn_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 = trn_cell_introduce1_encoded_len(obj);
  328. #endif
  329. if (NULL != (msg = trn_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 trn_cell_extension extensions */
  364. trunnel_assert(written <= avail);
  365. result = trn_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 trn_cell_introduce1_parse(), but do not allocate the output
  399. * object.
  400. */
  401. static ssize_t
  402. trn_cell_introduce1_parse_into(trn_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 trn_cell_extension extensions */
  430. result = trn_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. trn_cell_introduce1_parse(trn_cell_introduce1_t **output, const uint8_t *input, const size_t len_in)
  456. {
  457. ssize_t result;
  458. *output = trn_cell_introduce1_new();
  459. if (NULL == *output)
  460. return -1;
  461. result = trn_cell_introduce1_parse_into(*output, input, len_in);
  462. if (result < 0) {
  463. trn_cell_introduce1_free(*output);
  464. *output = NULL;
  465. }
  466. return result;
  467. }
  468. trn_cell_introduce_ack_t *
  469. trn_cell_introduce_ack_new(void)
  470. {
  471. trn_cell_introduce_ack_t *val = trunnel_calloc(1, sizeof(trn_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. trn_cell_introduce_ack_clear(trn_cell_introduce_ack_t *obj)
  480. {
  481. (void) obj;
  482. trn_cell_extension_free(obj->extensions);
  483. obj->extensions = NULL;
  484. }
  485. void
  486. trn_cell_introduce_ack_free(trn_cell_introduce_ack_t *obj)
  487. {
  488. if (obj == NULL)
  489. return;
  490. trn_cell_introduce_ack_clear(obj);
  491. trunnel_memwipe(obj, sizeof(trn_cell_introduce_ack_t));
  492. trunnel_free_(obj);
  493. }
  494. uint16_t
  495. trn_cell_introduce_ack_get_status(const trn_cell_introduce_ack_t *inp)
  496. {
  497. return inp->status;
  498. }
  499. int
  500. trn_cell_introduce_ack_set_status(trn_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 trn_cell_extension_st *
  510. trn_cell_introduce_ack_get_extensions(trn_cell_introduce_ack_t *inp)
  511. {
  512. return inp->extensions;
  513. }
  514. const struct trn_cell_extension_st *
  515. trn_cell_introduce_ack_getconst_extensions(const trn_cell_introduce_ack_t *inp)
  516. {
  517. return trn_cell_introduce_ack_get_extensions((trn_cell_introduce_ack_t*) inp);
  518. }
  519. int
  520. trn_cell_introduce_ack_set_extensions(trn_cell_introduce_ack_t *inp, struct trn_cell_extension_st *val)
  521. {
  522. if (inp->extensions && inp->extensions != val)
  523. trn_cell_extension_free(inp->extensions);
  524. return trn_cell_introduce_ack_set0_extensions(inp, val);
  525. }
  526. int
  527. trn_cell_introduce_ack_set0_extensions(trn_cell_introduce_ack_t *inp, struct trn_cell_extension_st *val)
  528. {
  529. inp->extensions = val;
  530. return 0;
  531. }
  532. const char *
  533. trn_cell_introduce_ack_check(const trn_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 = trn_cell_extension_check(obj->extensions)))
  544. return msg;
  545. }
  546. return NULL;
  547. }
  548. ssize_t
  549. trn_cell_introduce_ack_encoded_len(const trn_cell_introduce_ack_t *obj)
  550. {
  551. ssize_t result = 0;
  552. if (NULL != trn_cell_introduce_ack_check(obj))
  553. return -1;
  554. /* Length of u16 status IN [0, 1, 2] */
  555. result += 2;
  556. /* Length of struct trn_cell_extension extensions */
  557. result += trn_cell_extension_encoded_len(obj->extensions);
  558. return result;
  559. }
  560. int
  561. trn_cell_introduce_ack_clear_errors(trn_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. trn_cell_introduce_ack_encode(uint8_t *output, const size_t avail, const trn_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 = trn_cell_introduce_ack_encoded_len(obj);
  576. #endif
  577. if (NULL != (msg = trn_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 trn_cell_extension extensions */
  589. trunnel_assert(written <= avail);
  590. result = trn_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 trn_cell_introduce_ack_parse(), but do not allocate the output
  614. * object.
  615. */
  616. static ssize_t
  617. trn_cell_introduce_ack_parse_into(trn_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 trn_cell_extension extensions */
  630. result = trn_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. trn_cell_introduce_ack_parse(trn_cell_introduce_ack_t **output, const uint8_t *input, const size_t len_in)
  648. {
  649. ssize_t result;
  650. *output = trn_cell_introduce_ack_new();
  651. if (NULL == *output)
  652. return -1;
  653. result = trn_cell_introduce_ack_parse_into(*output, input, len_in);
  654. if (result < 0) {
  655. trn_cell_introduce_ack_free(*output);
  656. *output = NULL;
  657. }
  658. return result;
  659. }
  660. trn_cell_introduce_encrypted_t *
  661. trn_cell_introduce_encrypted_new(void)
  662. {
  663. trn_cell_introduce_encrypted_t *val = trunnel_calloc(1, sizeof(trn_cell_introduce_encrypted_t));
  664. if (NULL == val)
  665. return NULL;
  666. val->onion_key_type = 1;
  667. return val;
  668. }
  669. /** Release all storage held inside 'obj', but do not free 'obj'.
  670. */
  671. static void
  672. trn_cell_introduce_encrypted_clear(trn_cell_introduce_encrypted_t *obj)
  673. {
  674. (void) obj;
  675. trn_cell_extension_free(obj->extensions);
  676. obj->extensions = NULL;
  677. TRUNNEL_DYNARRAY_WIPE(&obj->onion_key);
  678. TRUNNEL_DYNARRAY_CLEAR(&obj->onion_key);
  679. {
  680. unsigned idx;
  681. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  682. link_specifier_free(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  683. }
  684. }
  685. TRUNNEL_DYNARRAY_WIPE(&obj->nspecs);
  686. TRUNNEL_DYNARRAY_CLEAR(&obj->nspecs);
  687. TRUNNEL_DYNARRAY_WIPE(&obj->pad);
  688. TRUNNEL_DYNARRAY_CLEAR(&obj->pad);
  689. }
  690. void
  691. trn_cell_introduce_encrypted_free(trn_cell_introduce_encrypted_t *obj)
  692. {
  693. if (obj == NULL)
  694. return;
  695. trn_cell_introduce_encrypted_clear(obj);
  696. trunnel_memwipe(obj, sizeof(trn_cell_introduce_encrypted_t));
  697. trunnel_free_(obj);
  698. }
  699. size_t
  700. trn_cell_introduce_encrypted_getlen_rend_cookie(const trn_cell_introduce_encrypted_t *inp)
  701. {
  702. (void)inp; return TRUNNEL_REND_COOKIE_LEN;
  703. }
  704. uint8_t
  705. trn_cell_introduce_encrypted_get_rend_cookie(trn_cell_introduce_encrypted_t *inp, size_t idx)
  706. {
  707. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  708. return inp->rend_cookie[idx];
  709. }
  710. uint8_t
  711. trn_cell_introduce_encrypted_getconst_rend_cookie(const trn_cell_introduce_encrypted_t *inp, size_t idx)
  712. {
  713. return trn_cell_introduce_encrypted_get_rend_cookie((trn_cell_introduce_encrypted_t*)inp, idx);
  714. }
  715. int
  716. trn_cell_introduce_encrypted_set_rend_cookie(trn_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  717. {
  718. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  719. inp->rend_cookie[idx] = elt;
  720. return 0;
  721. }
  722. uint8_t *
  723. trn_cell_introduce_encrypted_getarray_rend_cookie(trn_cell_introduce_encrypted_t *inp)
  724. {
  725. return inp->rend_cookie;
  726. }
  727. const uint8_t *
  728. trn_cell_introduce_encrypted_getconstarray_rend_cookie(const trn_cell_introduce_encrypted_t *inp)
  729. {
  730. return (const uint8_t *)trn_cell_introduce_encrypted_getarray_rend_cookie((trn_cell_introduce_encrypted_t*)inp);
  731. }
  732. struct trn_cell_extension_st *
  733. trn_cell_introduce_encrypted_get_extensions(trn_cell_introduce_encrypted_t *inp)
  734. {
  735. return inp->extensions;
  736. }
  737. const struct trn_cell_extension_st *
  738. trn_cell_introduce_encrypted_getconst_extensions(const trn_cell_introduce_encrypted_t *inp)
  739. {
  740. return trn_cell_introduce_encrypted_get_extensions((trn_cell_introduce_encrypted_t*) inp);
  741. }
  742. int
  743. trn_cell_introduce_encrypted_set_extensions(trn_cell_introduce_encrypted_t *inp, struct trn_cell_extension_st *val)
  744. {
  745. if (inp->extensions && inp->extensions != val)
  746. trn_cell_extension_free(inp->extensions);
  747. return trn_cell_introduce_encrypted_set0_extensions(inp, val);
  748. }
  749. int
  750. trn_cell_introduce_encrypted_set0_extensions(trn_cell_introduce_encrypted_t *inp, struct trn_cell_extension_st *val)
  751. {
  752. inp->extensions = val;
  753. return 0;
  754. }
  755. uint8_t
  756. trn_cell_introduce_encrypted_get_onion_key_type(const trn_cell_introduce_encrypted_t *inp)
  757. {
  758. return inp->onion_key_type;
  759. }
  760. int
  761. trn_cell_introduce_encrypted_set_onion_key_type(trn_cell_introduce_encrypted_t *inp, uint8_t val)
  762. {
  763. if (! ((val == 1))) {
  764. TRUNNEL_SET_ERROR_CODE(inp);
  765. return -1;
  766. }
  767. inp->onion_key_type = val;
  768. return 0;
  769. }
  770. uint16_t
  771. trn_cell_introduce_encrypted_get_onion_key_len(const trn_cell_introduce_encrypted_t *inp)
  772. {
  773. return inp->onion_key_len;
  774. }
  775. int
  776. trn_cell_introduce_encrypted_set_onion_key_len(trn_cell_introduce_encrypted_t *inp, uint16_t val)
  777. {
  778. inp->onion_key_len = val;
  779. return 0;
  780. }
  781. size_t
  782. trn_cell_introduce_encrypted_getlen_onion_key(const trn_cell_introduce_encrypted_t *inp)
  783. {
  784. return TRUNNEL_DYNARRAY_LEN(&inp->onion_key);
  785. }
  786. uint8_t
  787. trn_cell_introduce_encrypted_get_onion_key(trn_cell_introduce_encrypted_t *inp, size_t idx)
  788. {
  789. return TRUNNEL_DYNARRAY_GET(&inp->onion_key, idx);
  790. }
  791. uint8_t
  792. trn_cell_introduce_encrypted_getconst_onion_key(const trn_cell_introduce_encrypted_t *inp, size_t idx)
  793. {
  794. return trn_cell_introduce_encrypted_get_onion_key((trn_cell_introduce_encrypted_t*)inp, idx);
  795. }
  796. int
  797. trn_cell_introduce_encrypted_set_onion_key(trn_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  798. {
  799. TRUNNEL_DYNARRAY_SET(&inp->onion_key, idx, elt);
  800. return 0;
  801. }
  802. int
  803. trn_cell_introduce_encrypted_add_onion_key(trn_cell_introduce_encrypted_t *inp, uint8_t elt)
  804. {
  805. #if SIZE_MAX >= UINT16_MAX
  806. if (inp->onion_key.n_ == UINT16_MAX)
  807. goto trunnel_alloc_failed;
  808. #endif
  809. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->onion_key, elt, {});
  810. return 0;
  811. trunnel_alloc_failed:
  812. TRUNNEL_SET_ERROR_CODE(inp);
  813. return -1;
  814. }
  815. uint8_t *
  816. trn_cell_introduce_encrypted_getarray_onion_key(trn_cell_introduce_encrypted_t *inp)
  817. {
  818. return inp->onion_key.elts_;
  819. }
  820. const uint8_t *
  821. trn_cell_introduce_encrypted_getconstarray_onion_key(const trn_cell_introduce_encrypted_t *inp)
  822. {
  823. return (const uint8_t *)trn_cell_introduce_encrypted_getarray_onion_key((trn_cell_introduce_encrypted_t*)inp);
  824. }
  825. int
  826. trn_cell_introduce_encrypted_setlen_onion_key(trn_cell_introduce_encrypted_t *inp, size_t newlen)
  827. {
  828. uint8_t *newptr;
  829. #if UINT16_MAX < SIZE_MAX
  830. if (newlen > UINT16_MAX)
  831. goto trunnel_alloc_failed;
  832. #endif
  833. newptr = trunnel_dynarray_setlen(&inp->onion_key.allocated_,
  834. &inp->onion_key.n_, inp->onion_key.elts_, newlen,
  835. sizeof(inp->onion_key.elts_[0]), (trunnel_free_fn_t) NULL,
  836. &inp->trunnel_error_code_);
  837. if (newlen != 0 && newptr == NULL)
  838. goto trunnel_alloc_failed;
  839. inp->onion_key.elts_ = newptr;
  840. return 0;
  841. trunnel_alloc_failed:
  842. TRUNNEL_SET_ERROR_CODE(inp);
  843. return -1;
  844. }
  845. uint8_t
  846. trn_cell_introduce_encrypted_get_nspec(const trn_cell_introduce_encrypted_t *inp)
  847. {
  848. return inp->nspec;
  849. }
  850. int
  851. trn_cell_introduce_encrypted_set_nspec(trn_cell_introduce_encrypted_t *inp, uint8_t val)
  852. {
  853. inp->nspec = val;
  854. return 0;
  855. }
  856. size_t
  857. trn_cell_introduce_encrypted_getlen_nspecs(const trn_cell_introduce_encrypted_t *inp)
  858. {
  859. return TRUNNEL_DYNARRAY_LEN(&inp->nspecs);
  860. }
  861. struct link_specifier_st *
  862. trn_cell_introduce_encrypted_get_nspecs(trn_cell_introduce_encrypted_t *inp, size_t idx)
  863. {
  864. return TRUNNEL_DYNARRAY_GET(&inp->nspecs, idx);
  865. }
  866. const struct link_specifier_st *
  867. trn_cell_introduce_encrypted_getconst_nspecs(const trn_cell_introduce_encrypted_t *inp, size_t idx)
  868. {
  869. return trn_cell_introduce_encrypted_get_nspecs((trn_cell_introduce_encrypted_t*)inp, idx);
  870. }
  871. int
  872. trn_cell_introduce_encrypted_set_nspecs(trn_cell_introduce_encrypted_t *inp, size_t idx, struct link_specifier_st * elt)
  873. {
  874. link_specifier_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->nspecs, idx);
  875. if (oldval && oldval != elt)
  876. link_specifier_free(oldval);
  877. return trn_cell_introduce_encrypted_set0_nspecs(inp, idx, elt);
  878. }
  879. int
  880. trn_cell_introduce_encrypted_set0_nspecs(trn_cell_introduce_encrypted_t *inp, size_t idx, struct link_specifier_st * elt)
  881. {
  882. TRUNNEL_DYNARRAY_SET(&inp->nspecs, idx, elt);
  883. return 0;
  884. }
  885. int
  886. trn_cell_introduce_encrypted_add_nspecs(trn_cell_introduce_encrypted_t *inp, struct link_specifier_st * elt)
  887. {
  888. #if SIZE_MAX >= UINT8_MAX
  889. if (inp->nspecs.n_ == UINT8_MAX)
  890. goto trunnel_alloc_failed;
  891. #endif
  892. TRUNNEL_DYNARRAY_ADD(struct link_specifier_st *, &inp->nspecs, elt, {});
  893. return 0;
  894. trunnel_alloc_failed:
  895. TRUNNEL_SET_ERROR_CODE(inp);
  896. return -1;
  897. }
  898. struct link_specifier_st * *
  899. trn_cell_introduce_encrypted_getarray_nspecs(trn_cell_introduce_encrypted_t *inp)
  900. {
  901. return inp->nspecs.elts_;
  902. }
  903. const struct link_specifier_st * const *
  904. trn_cell_introduce_encrypted_getconstarray_nspecs(const trn_cell_introduce_encrypted_t *inp)
  905. {
  906. return (const struct link_specifier_st * const *)trn_cell_introduce_encrypted_getarray_nspecs((trn_cell_introduce_encrypted_t*)inp);
  907. }
  908. int
  909. trn_cell_introduce_encrypted_setlen_nspecs(trn_cell_introduce_encrypted_t *inp, size_t newlen)
  910. {
  911. struct link_specifier_st * *newptr;
  912. #if UINT8_MAX < SIZE_MAX
  913. if (newlen > UINT8_MAX)
  914. goto trunnel_alloc_failed;
  915. #endif
  916. newptr = trunnel_dynarray_setlen(&inp->nspecs.allocated_,
  917. &inp->nspecs.n_, inp->nspecs.elts_, newlen,
  918. sizeof(inp->nspecs.elts_[0]), (trunnel_free_fn_t) link_specifier_free,
  919. &inp->trunnel_error_code_);
  920. if (newlen != 0 && newptr == NULL)
  921. goto trunnel_alloc_failed;
  922. inp->nspecs.elts_ = newptr;
  923. return 0;
  924. trunnel_alloc_failed:
  925. TRUNNEL_SET_ERROR_CODE(inp);
  926. return -1;
  927. }
  928. size_t
  929. trn_cell_introduce_encrypted_getlen_pad(const trn_cell_introduce_encrypted_t *inp)
  930. {
  931. return TRUNNEL_DYNARRAY_LEN(&inp->pad);
  932. }
  933. uint8_t
  934. trn_cell_introduce_encrypted_get_pad(trn_cell_introduce_encrypted_t *inp, size_t idx)
  935. {
  936. return TRUNNEL_DYNARRAY_GET(&inp->pad, idx);
  937. }
  938. uint8_t
  939. trn_cell_introduce_encrypted_getconst_pad(const trn_cell_introduce_encrypted_t *inp, size_t idx)
  940. {
  941. return trn_cell_introduce_encrypted_get_pad((trn_cell_introduce_encrypted_t*)inp, idx);
  942. }
  943. int
  944. trn_cell_introduce_encrypted_set_pad(trn_cell_introduce_encrypted_t *inp, size_t idx, uint8_t elt)
  945. {
  946. TRUNNEL_DYNARRAY_SET(&inp->pad, idx, elt);
  947. return 0;
  948. }
  949. int
  950. trn_cell_introduce_encrypted_add_pad(trn_cell_introduce_encrypted_t *inp, uint8_t elt)
  951. {
  952. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->pad, elt, {});
  953. return 0;
  954. trunnel_alloc_failed:
  955. TRUNNEL_SET_ERROR_CODE(inp);
  956. return -1;
  957. }
  958. uint8_t *
  959. trn_cell_introduce_encrypted_getarray_pad(trn_cell_introduce_encrypted_t *inp)
  960. {
  961. return inp->pad.elts_;
  962. }
  963. const uint8_t *
  964. trn_cell_introduce_encrypted_getconstarray_pad(const trn_cell_introduce_encrypted_t *inp)
  965. {
  966. return (const uint8_t *)trn_cell_introduce_encrypted_getarray_pad((trn_cell_introduce_encrypted_t*)inp);
  967. }
  968. int
  969. trn_cell_introduce_encrypted_setlen_pad(trn_cell_introduce_encrypted_t *inp, size_t newlen)
  970. {
  971. uint8_t *newptr;
  972. newptr = trunnel_dynarray_setlen(&inp->pad.allocated_,
  973. &inp->pad.n_, inp->pad.elts_, newlen,
  974. sizeof(inp->pad.elts_[0]), (trunnel_free_fn_t) NULL,
  975. &inp->trunnel_error_code_);
  976. if (newlen != 0 && newptr == NULL)
  977. goto trunnel_alloc_failed;
  978. inp->pad.elts_ = newptr;
  979. return 0;
  980. trunnel_alloc_failed:
  981. TRUNNEL_SET_ERROR_CODE(inp);
  982. return -1;
  983. }
  984. const char *
  985. trn_cell_introduce_encrypted_check(const trn_cell_introduce_encrypted_t *obj)
  986. {
  987. if (obj == NULL)
  988. return "Object was NULL";
  989. if (obj->trunnel_error_code_)
  990. return "A set function failed on this object";
  991. {
  992. const char *msg;
  993. if (NULL != (msg = trn_cell_extension_check(obj->extensions)))
  994. return msg;
  995. }
  996. if (! (obj->onion_key_type == 1))
  997. return "Integer out of bounds";
  998. if (TRUNNEL_DYNARRAY_LEN(&obj->onion_key) != obj->onion_key_len)
  999. return "Length mismatch for onion_key";
  1000. {
  1001. const char *msg;
  1002. unsigned idx;
  1003. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  1004. if (NULL != (msg = link_specifier_check(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx))))
  1005. return msg;
  1006. }
  1007. }
  1008. if (TRUNNEL_DYNARRAY_LEN(&obj->nspecs) != obj->nspec)
  1009. return "Length mismatch for nspecs";
  1010. return NULL;
  1011. }
  1012. ssize_t
  1013. trn_cell_introduce_encrypted_encoded_len(const trn_cell_introduce_encrypted_t *obj)
  1014. {
  1015. ssize_t result = 0;
  1016. if (NULL != trn_cell_introduce_encrypted_check(obj))
  1017. return -1;
  1018. /* Length of u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1019. result += TRUNNEL_REND_COOKIE_LEN;
  1020. /* Length of struct trn_cell_extension extensions */
  1021. result += trn_cell_extension_encoded_len(obj->extensions);
  1022. /* Length of u8 onion_key_type IN [1] */
  1023. result += 1;
  1024. /* Length of u16 onion_key_len */
  1025. result += 2;
  1026. /* Length of u8 onion_key[onion_key_len] */
  1027. result += TRUNNEL_DYNARRAY_LEN(&obj->onion_key);
  1028. /* Length of u8 nspec */
  1029. result += 1;
  1030. /* Length of struct link_specifier nspecs[nspec] */
  1031. {
  1032. unsigned idx;
  1033. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  1034. result += link_specifier_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  1035. }
  1036. }
  1037. /* Length of u8 pad[] */
  1038. result += TRUNNEL_DYNARRAY_LEN(&obj->pad);
  1039. return result;
  1040. }
  1041. int
  1042. trn_cell_introduce_encrypted_clear_errors(trn_cell_introduce_encrypted_t *obj)
  1043. {
  1044. int r = obj->trunnel_error_code_;
  1045. obj->trunnel_error_code_ = 0;
  1046. return r;
  1047. }
  1048. ssize_t
  1049. trn_cell_introduce_encrypted_encode(uint8_t *output, const size_t avail, const trn_cell_introduce_encrypted_t *obj)
  1050. {
  1051. ssize_t result = 0;
  1052. size_t written = 0;
  1053. uint8_t *ptr = output;
  1054. const char *msg;
  1055. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1056. const ssize_t encoded_len = trn_cell_introduce_encrypted_encoded_len(obj);
  1057. #endif
  1058. if (NULL != (msg = trn_cell_introduce_encrypted_check(obj)))
  1059. goto check_failed;
  1060. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1061. trunnel_assert(encoded_len >= 0);
  1062. #endif
  1063. /* Encode u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1064. trunnel_assert(written <= avail);
  1065. if (avail - written < TRUNNEL_REND_COOKIE_LEN)
  1066. goto truncated;
  1067. memcpy(ptr, obj->rend_cookie, TRUNNEL_REND_COOKIE_LEN);
  1068. written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  1069. /* Encode struct trn_cell_extension extensions */
  1070. trunnel_assert(written <= avail);
  1071. result = trn_cell_extension_encode(ptr, avail - written, obj->extensions);
  1072. if (result < 0)
  1073. goto fail; /* XXXXXXX !*/
  1074. written += result; ptr += result;
  1075. /* Encode u8 onion_key_type IN [1] */
  1076. trunnel_assert(written <= avail);
  1077. if (avail - written < 1)
  1078. goto truncated;
  1079. trunnel_set_uint8(ptr, (obj->onion_key_type));
  1080. written += 1; ptr += 1;
  1081. /* Encode u16 onion_key_len */
  1082. trunnel_assert(written <= avail);
  1083. if (avail - written < 2)
  1084. goto truncated;
  1085. trunnel_set_uint16(ptr, trunnel_htons(obj->onion_key_len));
  1086. written += 2; ptr += 2;
  1087. /* Encode u8 onion_key[onion_key_len] */
  1088. {
  1089. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->onion_key);
  1090. trunnel_assert(obj->onion_key_len == elt_len);
  1091. trunnel_assert(written <= avail);
  1092. if (avail - written < elt_len)
  1093. goto truncated;
  1094. if (elt_len)
  1095. memcpy(ptr, obj->onion_key.elts_, elt_len);
  1096. written += elt_len; ptr += elt_len;
  1097. }
  1098. /* Encode u8 nspec */
  1099. trunnel_assert(written <= avail);
  1100. if (avail - written < 1)
  1101. goto truncated;
  1102. trunnel_set_uint8(ptr, (obj->nspec));
  1103. written += 1; ptr += 1;
  1104. /* Encode struct link_specifier nspecs[nspec] */
  1105. {
  1106. unsigned idx;
  1107. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->nspecs); ++idx) {
  1108. trunnel_assert(written <= avail);
  1109. result = link_specifier_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->nspecs, idx));
  1110. if (result < 0)
  1111. goto fail; /* XXXXXXX !*/
  1112. written += result; ptr += result;
  1113. }
  1114. }
  1115. /* Encode u8 pad[] */
  1116. {
  1117. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->pad);
  1118. trunnel_assert(written <= avail);
  1119. if (avail - written < elt_len)
  1120. goto truncated;
  1121. if (elt_len)
  1122. memcpy(ptr, obj->pad.elts_, elt_len);
  1123. written += elt_len; ptr += elt_len;
  1124. }
  1125. trunnel_assert(ptr == output + written);
  1126. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1127. {
  1128. trunnel_assert(encoded_len >= 0);
  1129. trunnel_assert((size_t)encoded_len == written);
  1130. }
  1131. #endif
  1132. return written;
  1133. truncated:
  1134. result = -2;
  1135. goto fail;
  1136. check_failed:
  1137. (void)msg;
  1138. result = -1;
  1139. goto fail;
  1140. fail:
  1141. trunnel_assert(result < 0);
  1142. return result;
  1143. }
  1144. /** As trn_cell_introduce_encrypted_parse(), but do not allocate the
  1145. * output object.
  1146. */
  1147. static ssize_t
  1148. trn_cell_introduce_encrypted_parse_into(trn_cell_introduce_encrypted_t *obj, const uint8_t *input, const size_t len_in)
  1149. {
  1150. const uint8_t *ptr = input;
  1151. size_t remaining = len_in;
  1152. ssize_t result = 0;
  1153. (void)result;
  1154. /* Parse u8 rend_cookie[TRUNNEL_REND_COOKIE_LEN] */
  1155. CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated);
  1156. memcpy(obj->rend_cookie, ptr, TRUNNEL_REND_COOKIE_LEN);
  1157. remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  1158. /* Parse struct trn_cell_extension extensions */
  1159. result = trn_cell_extension_parse(&obj->extensions, ptr, remaining);
  1160. if (result < 0)
  1161. goto relay_fail;
  1162. trunnel_assert((size_t)result <= remaining);
  1163. remaining -= result; ptr += result;
  1164. /* Parse u8 onion_key_type IN [1] */
  1165. CHECK_REMAINING(1, truncated);
  1166. obj->onion_key_type = (trunnel_get_uint8(ptr));
  1167. remaining -= 1; ptr += 1;
  1168. if (! (obj->onion_key_type == 1))
  1169. goto fail;
  1170. /* Parse u16 onion_key_len */
  1171. CHECK_REMAINING(2, truncated);
  1172. obj->onion_key_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  1173. remaining -= 2; ptr += 2;
  1174. /* Parse u8 onion_key[onion_key_len] */
  1175. CHECK_REMAINING(obj->onion_key_len, truncated);
  1176. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->onion_key, obj->onion_key_len, {});
  1177. obj->onion_key.n_ = obj->onion_key_len;
  1178. if (obj->onion_key_len)
  1179. memcpy(obj->onion_key.elts_, ptr, obj->onion_key_len);
  1180. ptr += obj->onion_key_len; remaining -= obj->onion_key_len;
  1181. /* Parse u8 nspec */
  1182. CHECK_REMAINING(1, truncated);
  1183. obj->nspec = (trunnel_get_uint8(ptr));
  1184. remaining -= 1; ptr += 1;
  1185. /* Parse struct link_specifier nspecs[nspec] */
  1186. TRUNNEL_DYNARRAY_EXPAND(link_specifier_t *, &obj->nspecs, obj->nspec, {});
  1187. {
  1188. link_specifier_t * elt;
  1189. unsigned idx;
  1190. for (idx = 0; idx < obj->nspec; ++idx) {
  1191. result = link_specifier_parse(&elt, ptr, remaining);
  1192. if (result < 0)
  1193. goto relay_fail;
  1194. trunnel_assert((size_t)result <= remaining);
  1195. remaining -= result; ptr += result;
  1196. TRUNNEL_DYNARRAY_ADD(link_specifier_t *, &obj->nspecs, elt, {link_specifier_free(elt);});
  1197. }
  1198. }
  1199. /* Parse u8 pad[] */
  1200. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->pad, remaining, {});
  1201. obj->pad.n_ = remaining;
  1202. if (remaining)
  1203. memcpy(obj->pad.elts_, ptr, remaining);
  1204. ptr += remaining; remaining -= remaining;
  1205. trunnel_assert(ptr + remaining == input + len_in);
  1206. return len_in - remaining;
  1207. truncated:
  1208. return -2;
  1209. relay_fail:
  1210. trunnel_assert(result < 0);
  1211. return result;
  1212. trunnel_alloc_failed:
  1213. return -1;
  1214. fail:
  1215. result = -1;
  1216. return result;
  1217. }
  1218. ssize_t
  1219. trn_cell_introduce_encrypted_parse(trn_cell_introduce_encrypted_t **output, const uint8_t *input, const size_t len_in)
  1220. {
  1221. ssize_t result;
  1222. *output = trn_cell_introduce_encrypted_new();
  1223. if (NULL == *output)
  1224. return -1;
  1225. result = trn_cell_introduce_encrypted_parse_into(*output, input, len_in);
  1226. if (result < 0) {
  1227. trn_cell_introduce_encrypted_free(*output);
  1228. *output = NULL;
  1229. }
  1230. return result;
  1231. }