cell_introduce1.c 38 KB

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