link_handshake.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /* link_handshake.c -- generated by Trunnel v1.4.6.
  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 "link_handshake.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're runnning a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int linkhandshake_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || linkhandshake_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. auth_challenge_cell_t *
  27. auth_challenge_cell_new(void)
  28. {
  29. auth_challenge_cell_t *val = trunnel_calloc(1, sizeof(auth_challenge_cell_t));
  30. if (NULL == val)
  31. return NULL;
  32. return val;
  33. }
  34. /** Release all storage held inside 'obj', but do not free 'obj'.
  35. */
  36. static void
  37. auth_challenge_cell_clear(auth_challenge_cell_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->methods);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->methods);
  42. }
  43. void
  44. auth_challenge_cell_free(auth_challenge_cell_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. auth_challenge_cell_clear(obj);
  49. trunnel_memwipe(obj, sizeof(auth_challenge_cell_t));
  50. trunnel_free_(obj);
  51. }
  52. size_t
  53. auth_challenge_cell_getlen_challenge(const auth_challenge_cell_t *inp)
  54. {
  55. (void)inp; return 32;
  56. }
  57. uint8_t
  58. auth_challenge_cell_get_challenge(const auth_challenge_cell_t *inp, size_t idx)
  59. {
  60. trunnel_assert(idx < 32);
  61. return inp->challenge[idx];
  62. }
  63. int
  64. auth_challenge_cell_set_challenge(auth_challenge_cell_t *inp, size_t idx, uint8_t elt)
  65. {
  66. trunnel_assert(idx < 32);
  67. inp->challenge[idx] = elt;
  68. return 0;
  69. }
  70. uint8_t *
  71. auth_challenge_cell_getarray_challenge(auth_challenge_cell_t *inp)
  72. {
  73. return inp->challenge;
  74. }
  75. uint16_t
  76. auth_challenge_cell_get_n_methods(auth_challenge_cell_t *inp)
  77. {
  78. return inp->n_methods;
  79. }
  80. int
  81. auth_challenge_cell_set_n_methods(auth_challenge_cell_t *inp, uint16_t val)
  82. {
  83. inp->n_methods = val;
  84. return 0;
  85. }
  86. size_t
  87. auth_challenge_cell_getlen_methods(const auth_challenge_cell_t *inp)
  88. {
  89. return TRUNNEL_DYNARRAY_LEN(&inp->methods);
  90. }
  91. uint16_t
  92. auth_challenge_cell_get_methods(auth_challenge_cell_t *inp, size_t idx)
  93. {
  94. return TRUNNEL_DYNARRAY_GET(&inp->methods, idx);
  95. }
  96. int
  97. auth_challenge_cell_set_methods(auth_challenge_cell_t *inp, size_t idx, uint16_t elt)
  98. {
  99. TRUNNEL_DYNARRAY_SET(&inp->methods, idx, elt);
  100. return 0;
  101. }
  102. int
  103. auth_challenge_cell_add_methods(auth_challenge_cell_t *inp, uint16_t elt)
  104. {
  105. #if SIZE_MAX >= UINT16_MAX
  106. if (inp->methods.n_ == UINT16_MAX)
  107. goto trunnel_alloc_failed;
  108. #endif
  109. TRUNNEL_DYNARRAY_ADD(uint16_t, &inp->methods, elt, {});
  110. return 0;
  111. trunnel_alloc_failed:
  112. TRUNNEL_SET_ERROR_CODE(inp);
  113. return -1;
  114. }
  115. uint16_t *
  116. auth_challenge_cell_getarray_methods(auth_challenge_cell_t *inp)
  117. {
  118. return inp->methods.elts_;
  119. }
  120. int
  121. auth_challenge_cell_setlen_methods(auth_challenge_cell_t *inp, size_t newlen)
  122. {
  123. uint16_t *newptr;
  124. #if UINT16_MAX < SIZE_MAX
  125. if (newlen > UINT16_MAX)
  126. goto trunnel_alloc_failed;
  127. #endif
  128. newptr = trunnel_dynarray_setlen(&inp->methods.allocated_,
  129. &inp->methods.n_, inp->methods.elts_, newlen,
  130. sizeof(inp->methods.elts_[0]), (trunnel_free_fn_t) NULL,
  131. &inp->trunnel_error_code_);
  132. if (newlen != 0 && newptr == NULL)
  133. goto trunnel_alloc_failed;
  134. inp->methods.elts_ = newptr;
  135. return 0;
  136. trunnel_alloc_failed:
  137. TRUNNEL_SET_ERROR_CODE(inp);
  138. return -1;
  139. }
  140. const char *
  141. auth_challenge_cell_check(const auth_challenge_cell_t *obj)
  142. {
  143. if (obj == NULL)
  144. return "Object was NULL";
  145. if (obj->trunnel_error_code_)
  146. return "A set function failed on this object";
  147. if (TRUNNEL_DYNARRAY_LEN(&obj->methods) != obj->n_methods)
  148. return "Length mismatch for methods";
  149. return NULL;
  150. }
  151. ssize_t
  152. auth_challenge_cell_encoded_len(const auth_challenge_cell_t *obj)
  153. {
  154. ssize_t result = 0;
  155. if (NULL != auth_challenge_cell_check(obj))
  156. return -1;
  157. /* Length of u8 challenge[32] */
  158. result += 32;
  159. /* Length of u16 n_methods */
  160. result += 2;
  161. /* Length of u16 methods[n_methods] */
  162. result += 2 * TRUNNEL_DYNARRAY_LEN(&obj->methods);
  163. return result;
  164. }
  165. int
  166. auth_challenge_cell_clear_errors(auth_challenge_cell_t *obj)
  167. {
  168. int r = obj->trunnel_error_code_;
  169. obj->trunnel_error_code_ = 0;
  170. return r;
  171. }
  172. ssize_t
  173. auth_challenge_cell_encode(uint8_t *output, const size_t avail, const auth_challenge_cell_t *obj)
  174. {
  175. ssize_t result = 0;
  176. size_t written = 0;
  177. uint8_t *ptr = output;
  178. const char *msg;
  179. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  180. const ssize_t encoded_len = auth_challenge_cell_encoded_len(obj);
  181. #endif
  182. if (NULL != (msg = auth_challenge_cell_check(obj)))
  183. goto check_failed;
  184. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  185. trunnel_assert(encoded_len >= 0);
  186. #endif
  187. /* Encode u8 challenge[32] */
  188. trunnel_assert(written <= avail);
  189. if (avail - written < 32)
  190. goto truncated;
  191. memcpy(ptr, obj->challenge, 32);
  192. written += 32; ptr += 32;
  193. /* Encode u16 n_methods */
  194. trunnel_assert(written <= avail);
  195. if (avail - written < 2)
  196. goto truncated;
  197. trunnel_set_uint16(ptr, trunnel_htons(obj->n_methods));
  198. written += 2; ptr += 2;
  199. /* Encode u16 methods[n_methods] */
  200. {
  201. unsigned idx;
  202. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->methods); ++idx) {
  203. trunnel_assert(written <= avail);
  204. if (avail - written < 2)
  205. goto truncated;
  206. trunnel_set_uint16(ptr, trunnel_htons(TRUNNEL_DYNARRAY_GET(&obj->methods, idx)));
  207. written += 2; ptr += 2;
  208. }
  209. }
  210. trunnel_assert(ptr == output + written);
  211. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  212. {
  213. trunnel_assert(encoded_len >= 0);
  214. trunnel_assert((size_t)encoded_len == written);
  215. }
  216. #endif
  217. return written;
  218. truncated:
  219. result = -2;
  220. goto fail;
  221. check_failed:
  222. (void)msg;
  223. result = -1;
  224. goto fail;
  225. fail:
  226. trunnel_assert(result < 0);
  227. return result;
  228. }
  229. /** As auth_challenge_cell_parse(), but do not allocate the output
  230. * object.
  231. */
  232. static ssize_t
  233. auth_challenge_cell_parse_into(auth_challenge_cell_t *obj, const uint8_t *input, const size_t len_in)
  234. {
  235. const uint8_t *ptr = input;
  236. size_t remaining = len_in;
  237. ssize_t result = 0;
  238. (void)result;
  239. /* Parse u8 challenge[32] */
  240. CHECK_REMAINING(32, truncated);
  241. memcpy(obj->challenge, ptr, 32);
  242. remaining -= 32; ptr += 32;
  243. /* Parse u16 n_methods */
  244. CHECK_REMAINING(2, truncated);
  245. obj->n_methods = trunnel_ntohs(trunnel_get_uint16(ptr));
  246. remaining -= 2; ptr += 2;
  247. /* Parse u16 methods[n_methods] */
  248. TRUNNEL_DYNARRAY_EXPAND(uint16_t, &obj->methods, obj->n_methods, {});
  249. {
  250. uint16_t elt;
  251. unsigned idx;
  252. for (idx = 0; idx < obj->n_methods; ++idx) {
  253. CHECK_REMAINING(2, truncated);
  254. elt = trunnel_ntohs(trunnel_get_uint16(ptr));
  255. remaining -= 2; ptr += 2;
  256. TRUNNEL_DYNARRAY_ADD(uint16_t, &obj->methods, elt, {});
  257. }
  258. }
  259. trunnel_assert(ptr + remaining == input + len_in);
  260. return len_in - remaining;
  261. truncated:
  262. return -2;
  263. trunnel_alloc_failed:
  264. return -1;
  265. }
  266. ssize_t
  267. auth_challenge_cell_parse(auth_challenge_cell_t **output, const uint8_t *input, const size_t len_in)
  268. {
  269. ssize_t result;
  270. *output = auth_challenge_cell_new();
  271. if (NULL == *output)
  272. return -1;
  273. result = auth_challenge_cell_parse_into(*output, input, len_in);
  274. if (result < 0) {
  275. auth_challenge_cell_free(*output);
  276. *output = NULL;
  277. }
  278. return result;
  279. }
  280. auth_ctx_t *
  281. auth_ctx_new(void)
  282. {
  283. auth_ctx_t *val = trunnel_calloc(1, sizeof(auth_ctx_t));
  284. if (NULL == val)
  285. return NULL;
  286. return val;
  287. }
  288. /** Release all storage held inside 'obj', but do not free 'obj'.
  289. */
  290. static void
  291. auth_ctx_clear(auth_ctx_t *obj)
  292. {
  293. (void) obj;
  294. }
  295. void
  296. auth_ctx_free(auth_ctx_t *obj)
  297. {
  298. if (obj == NULL)
  299. return;
  300. auth_ctx_clear(obj);
  301. trunnel_memwipe(obj, sizeof(auth_ctx_t));
  302. trunnel_free_(obj);
  303. }
  304. uint8_t
  305. auth_ctx_get_is_ed(auth_ctx_t *inp)
  306. {
  307. return inp->is_ed;
  308. }
  309. int
  310. auth_ctx_set_is_ed(auth_ctx_t *inp, uint8_t val)
  311. {
  312. inp->is_ed = val;
  313. return 0;
  314. }
  315. certs_cell_cert_t *
  316. certs_cell_cert_new(void)
  317. {
  318. certs_cell_cert_t *val = trunnel_calloc(1, sizeof(certs_cell_cert_t));
  319. if (NULL == val)
  320. return NULL;
  321. return val;
  322. }
  323. /** Release all storage held inside 'obj', but do not free 'obj'.
  324. */
  325. static void
  326. certs_cell_cert_clear(certs_cell_cert_t *obj)
  327. {
  328. (void) obj;
  329. TRUNNEL_DYNARRAY_WIPE(&obj->body);
  330. TRUNNEL_DYNARRAY_CLEAR(&obj->body);
  331. }
  332. void
  333. certs_cell_cert_free(certs_cell_cert_t *obj)
  334. {
  335. if (obj == NULL)
  336. return;
  337. certs_cell_cert_clear(obj);
  338. trunnel_memwipe(obj, sizeof(certs_cell_cert_t));
  339. trunnel_free_(obj);
  340. }
  341. uint8_t
  342. certs_cell_cert_get_cert_type(certs_cell_cert_t *inp)
  343. {
  344. return inp->cert_type;
  345. }
  346. int
  347. certs_cell_cert_set_cert_type(certs_cell_cert_t *inp, uint8_t val)
  348. {
  349. inp->cert_type = val;
  350. return 0;
  351. }
  352. uint16_t
  353. certs_cell_cert_get_cert_len(certs_cell_cert_t *inp)
  354. {
  355. return inp->cert_len;
  356. }
  357. int
  358. certs_cell_cert_set_cert_len(certs_cell_cert_t *inp, uint16_t val)
  359. {
  360. inp->cert_len = val;
  361. return 0;
  362. }
  363. size_t
  364. certs_cell_cert_getlen_body(const certs_cell_cert_t *inp)
  365. {
  366. return TRUNNEL_DYNARRAY_LEN(&inp->body);
  367. }
  368. uint8_t
  369. certs_cell_cert_get_body(certs_cell_cert_t *inp, size_t idx)
  370. {
  371. return TRUNNEL_DYNARRAY_GET(&inp->body, idx);
  372. }
  373. int
  374. certs_cell_cert_set_body(certs_cell_cert_t *inp, size_t idx, uint8_t elt)
  375. {
  376. TRUNNEL_DYNARRAY_SET(&inp->body, idx, elt);
  377. return 0;
  378. }
  379. int
  380. certs_cell_cert_add_body(certs_cell_cert_t *inp, uint8_t elt)
  381. {
  382. #if SIZE_MAX >= UINT16_MAX
  383. if (inp->body.n_ == UINT16_MAX)
  384. goto trunnel_alloc_failed;
  385. #endif
  386. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->body, elt, {});
  387. return 0;
  388. trunnel_alloc_failed:
  389. TRUNNEL_SET_ERROR_CODE(inp);
  390. return -1;
  391. }
  392. uint8_t *
  393. certs_cell_cert_getarray_body(certs_cell_cert_t *inp)
  394. {
  395. return inp->body.elts_;
  396. }
  397. int
  398. certs_cell_cert_setlen_body(certs_cell_cert_t *inp, size_t newlen)
  399. {
  400. uint8_t *newptr;
  401. #if UINT16_MAX < SIZE_MAX
  402. if (newlen > UINT16_MAX)
  403. goto trunnel_alloc_failed;
  404. #endif
  405. newptr = trunnel_dynarray_setlen(&inp->body.allocated_,
  406. &inp->body.n_, inp->body.elts_, newlen,
  407. sizeof(inp->body.elts_[0]), (trunnel_free_fn_t) NULL,
  408. &inp->trunnel_error_code_);
  409. if (newlen != 0 && newptr == NULL)
  410. goto trunnel_alloc_failed;
  411. inp->body.elts_ = newptr;
  412. return 0;
  413. trunnel_alloc_failed:
  414. TRUNNEL_SET_ERROR_CODE(inp);
  415. return -1;
  416. }
  417. const char *
  418. certs_cell_cert_check(const certs_cell_cert_t *obj)
  419. {
  420. if (obj == NULL)
  421. return "Object was NULL";
  422. if (obj->trunnel_error_code_)
  423. return "A set function failed on this object";
  424. if (TRUNNEL_DYNARRAY_LEN(&obj->body) != obj->cert_len)
  425. return "Length mismatch for body";
  426. return NULL;
  427. }
  428. ssize_t
  429. certs_cell_cert_encoded_len(const certs_cell_cert_t *obj)
  430. {
  431. ssize_t result = 0;
  432. if (NULL != certs_cell_cert_check(obj))
  433. return -1;
  434. /* Length of u8 cert_type */
  435. result += 1;
  436. /* Length of u16 cert_len */
  437. result += 2;
  438. /* Length of u8 body[cert_len] */
  439. result += TRUNNEL_DYNARRAY_LEN(&obj->body);
  440. return result;
  441. }
  442. int
  443. certs_cell_cert_clear_errors(certs_cell_cert_t *obj)
  444. {
  445. int r = obj->trunnel_error_code_;
  446. obj->trunnel_error_code_ = 0;
  447. return r;
  448. }
  449. ssize_t
  450. certs_cell_cert_encode(uint8_t *output, const size_t avail, const certs_cell_cert_t *obj)
  451. {
  452. ssize_t result = 0;
  453. size_t written = 0;
  454. uint8_t *ptr = output;
  455. const char *msg;
  456. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  457. const ssize_t encoded_len = certs_cell_cert_encoded_len(obj);
  458. #endif
  459. if (NULL != (msg = certs_cell_cert_check(obj)))
  460. goto check_failed;
  461. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  462. trunnel_assert(encoded_len >= 0);
  463. #endif
  464. /* Encode u8 cert_type */
  465. trunnel_assert(written <= avail);
  466. if (avail - written < 1)
  467. goto truncated;
  468. trunnel_set_uint8(ptr, (obj->cert_type));
  469. written += 1; ptr += 1;
  470. /* Encode u16 cert_len */
  471. trunnel_assert(written <= avail);
  472. if (avail - written < 2)
  473. goto truncated;
  474. trunnel_set_uint16(ptr, trunnel_htons(obj->cert_len));
  475. written += 2; ptr += 2;
  476. /* Encode u8 body[cert_len] */
  477. {
  478. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->body);
  479. trunnel_assert(obj->cert_len == elt_len);
  480. trunnel_assert(written <= avail);
  481. if (avail - written < elt_len)
  482. goto truncated;
  483. if (elt_len)
  484. memcpy(ptr, obj->body.elts_, elt_len);
  485. written += elt_len; ptr += elt_len;
  486. }
  487. trunnel_assert(ptr == output + written);
  488. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  489. {
  490. trunnel_assert(encoded_len >= 0);
  491. trunnel_assert((size_t)encoded_len == written);
  492. }
  493. #endif
  494. return written;
  495. truncated:
  496. result = -2;
  497. goto fail;
  498. check_failed:
  499. (void)msg;
  500. result = -1;
  501. goto fail;
  502. fail:
  503. trunnel_assert(result < 0);
  504. return result;
  505. }
  506. /** As certs_cell_cert_parse(), but do not allocate the output object.
  507. */
  508. static ssize_t
  509. certs_cell_cert_parse_into(certs_cell_cert_t *obj, const uint8_t *input, const size_t len_in)
  510. {
  511. const uint8_t *ptr = input;
  512. size_t remaining = len_in;
  513. ssize_t result = 0;
  514. (void)result;
  515. /* Parse u8 cert_type */
  516. CHECK_REMAINING(1, truncated);
  517. obj->cert_type = (trunnel_get_uint8(ptr));
  518. remaining -= 1; ptr += 1;
  519. /* Parse u16 cert_len */
  520. CHECK_REMAINING(2, truncated);
  521. obj->cert_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  522. remaining -= 2; ptr += 2;
  523. /* Parse u8 body[cert_len] */
  524. CHECK_REMAINING(obj->cert_len, truncated);
  525. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->body, obj->cert_len, {});
  526. obj->body.n_ = obj->cert_len;
  527. if (obj->cert_len)
  528. memcpy(obj->body.elts_, ptr, obj->cert_len);
  529. ptr += obj->cert_len; remaining -= obj->cert_len;
  530. trunnel_assert(ptr + remaining == input + len_in);
  531. return len_in - remaining;
  532. truncated:
  533. return -2;
  534. trunnel_alloc_failed:
  535. return -1;
  536. }
  537. ssize_t
  538. certs_cell_cert_parse(certs_cell_cert_t **output, const uint8_t *input, const size_t len_in)
  539. {
  540. ssize_t result;
  541. *output = certs_cell_cert_new();
  542. if (NULL == *output)
  543. return -1;
  544. result = certs_cell_cert_parse_into(*output, input, len_in);
  545. if (result < 0) {
  546. certs_cell_cert_free(*output);
  547. *output = NULL;
  548. }
  549. return result;
  550. }
  551. rsa_ed_crosscert_t *
  552. rsa_ed_crosscert_new(void)
  553. {
  554. rsa_ed_crosscert_t *val = trunnel_calloc(1, sizeof(rsa_ed_crosscert_t));
  555. if (NULL == val)
  556. return NULL;
  557. return val;
  558. }
  559. /** Release all storage held inside 'obj', but do not free 'obj'.
  560. */
  561. static void
  562. rsa_ed_crosscert_clear(rsa_ed_crosscert_t *obj)
  563. {
  564. (void) obj;
  565. TRUNNEL_DYNARRAY_WIPE(&obj->sig);
  566. TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
  567. }
  568. void
  569. rsa_ed_crosscert_free(rsa_ed_crosscert_t *obj)
  570. {
  571. if (obj == NULL)
  572. return;
  573. rsa_ed_crosscert_clear(obj);
  574. trunnel_memwipe(obj, sizeof(rsa_ed_crosscert_t));
  575. trunnel_free_(obj);
  576. }
  577. size_t
  578. rsa_ed_crosscert_getlen_ed_key(const rsa_ed_crosscert_t *inp)
  579. {
  580. (void)inp; return 32;
  581. }
  582. uint8_t
  583. rsa_ed_crosscert_get_ed_key(const rsa_ed_crosscert_t *inp, size_t idx)
  584. {
  585. trunnel_assert(idx < 32);
  586. return inp->ed_key[idx];
  587. }
  588. int
  589. rsa_ed_crosscert_set_ed_key(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
  590. {
  591. trunnel_assert(idx < 32);
  592. inp->ed_key[idx] = elt;
  593. return 0;
  594. }
  595. uint8_t *
  596. rsa_ed_crosscert_getarray_ed_key(rsa_ed_crosscert_t *inp)
  597. {
  598. return inp->ed_key;
  599. }
  600. uint32_t
  601. rsa_ed_crosscert_get_expiration(rsa_ed_crosscert_t *inp)
  602. {
  603. return inp->expiration;
  604. }
  605. int
  606. rsa_ed_crosscert_set_expiration(rsa_ed_crosscert_t *inp, uint32_t val)
  607. {
  608. inp->expiration = val;
  609. return 0;
  610. }
  611. const uint8_t *
  612. rsa_ed_crosscert_get_end_of_signed(const rsa_ed_crosscert_t *inp)
  613. {
  614. return inp->end_of_signed;
  615. }
  616. uint8_t
  617. rsa_ed_crosscert_get_sig_len(rsa_ed_crosscert_t *inp)
  618. {
  619. return inp->sig_len;
  620. }
  621. int
  622. rsa_ed_crosscert_set_sig_len(rsa_ed_crosscert_t *inp, uint8_t val)
  623. {
  624. inp->sig_len = val;
  625. return 0;
  626. }
  627. size_t
  628. rsa_ed_crosscert_getlen_sig(const rsa_ed_crosscert_t *inp)
  629. {
  630. return TRUNNEL_DYNARRAY_LEN(&inp->sig);
  631. }
  632. uint8_t
  633. rsa_ed_crosscert_get_sig(rsa_ed_crosscert_t *inp, size_t idx)
  634. {
  635. return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
  636. }
  637. int
  638. rsa_ed_crosscert_set_sig(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
  639. {
  640. TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
  641. return 0;
  642. }
  643. int
  644. rsa_ed_crosscert_add_sig(rsa_ed_crosscert_t *inp, uint8_t elt)
  645. {
  646. #if SIZE_MAX >= UINT8_MAX
  647. if (inp->sig.n_ == UINT8_MAX)
  648. goto trunnel_alloc_failed;
  649. #endif
  650. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
  651. return 0;
  652. trunnel_alloc_failed:
  653. TRUNNEL_SET_ERROR_CODE(inp);
  654. return -1;
  655. }
  656. uint8_t *
  657. rsa_ed_crosscert_getarray_sig(rsa_ed_crosscert_t *inp)
  658. {
  659. return inp->sig.elts_;
  660. }
  661. int
  662. rsa_ed_crosscert_setlen_sig(rsa_ed_crosscert_t *inp, size_t newlen)
  663. {
  664. uint8_t *newptr;
  665. #if UINT8_MAX < SIZE_MAX
  666. if (newlen > UINT8_MAX)
  667. goto trunnel_alloc_failed;
  668. #endif
  669. newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
  670. &inp->sig.n_, inp->sig.elts_, newlen,
  671. sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
  672. &inp->trunnel_error_code_);
  673. if (newlen != 0 && newptr == NULL)
  674. goto trunnel_alloc_failed;
  675. inp->sig.elts_ = newptr;
  676. return 0;
  677. trunnel_alloc_failed:
  678. TRUNNEL_SET_ERROR_CODE(inp);
  679. return -1;
  680. }
  681. const char *
  682. rsa_ed_crosscert_check(const rsa_ed_crosscert_t *obj)
  683. {
  684. if (obj == NULL)
  685. return "Object was NULL";
  686. if (obj->trunnel_error_code_)
  687. return "A set function failed on this object";
  688. if (TRUNNEL_DYNARRAY_LEN(&obj->sig) != obj->sig_len)
  689. return "Length mismatch for sig";
  690. return NULL;
  691. }
  692. ssize_t
  693. rsa_ed_crosscert_encoded_len(const rsa_ed_crosscert_t *obj)
  694. {
  695. ssize_t result = 0;
  696. if (NULL != rsa_ed_crosscert_check(obj))
  697. return -1;
  698. /* Length of u8 ed_key[32] */
  699. result += 32;
  700. /* Length of u32 expiration */
  701. result += 4;
  702. /* Length of u8 sig_len */
  703. result += 1;
  704. /* Length of u8 sig[sig_len] */
  705. result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
  706. return result;
  707. }
  708. int
  709. rsa_ed_crosscert_clear_errors(rsa_ed_crosscert_t *obj)
  710. {
  711. int r = obj->trunnel_error_code_;
  712. obj->trunnel_error_code_ = 0;
  713. return r;
  714. }
  715. ssize_t
  716. rsa_ed_crosscert_encode(uint8_t *output, const size_t avail, const rsa_ed_crosscert_t *obj)
  717. {
  718. ssize_t result = 0;
  719. size_t written = 0;
  720. uint8_t *ptr = output;
  721. const char *msg;
  722. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  723. const ssize_t encoded_len = rsa_ed_crosscert_encoded_len(obj);
  724. #endif
  725. if (NULL != (msg = rsa_ed_crosscert_check(obj)))
  726. goto check_failed;
  727. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  728. trunnel_assert(encoded_len >= 0);
  729. #endif
  730. /* Encode u8 ed_key[32] */
  731. trunnel_assert(written <= avail);
  732. if (avail - written < 32)
  733. goto truncated;
  734. memcpy(ptr, obj->ed_key, 32);
  735. written += 32; ptr += 32;
  736. /* Encode u32 expiration */
  737. trunnel_assert(written <= avail);
  738. if (avail - written < 4)
  739. goto truncated;
  740. trunnel_set_uint32(ptr, trunnel_htonl(obj->expiration));
  741. written += 4; ptr += 4;
  742. /* Encode u8 sig_len */
  743. trunnel_assert(written <= avail);
  744. if (avail - written < 1)
  745. goto truncated;
  746. trunnel_set_uint8(ptr, (obj->sig_len));
  747. written += 1; ptr += 1;
  748. /* Encode u8 sig[sig_len] */
  749. {
  750. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
  751. trunnel_assert(obj->sig_len == elt_len);
  752. trunnel_assert(written <= avail);
  753. if (avail - written < elt_len)
  754. goto truncated;
  755. if (elt_len)
  756. memcpy(ptr, obj->sig.elts_, elt_len);
  757. written += elt_len; ptr += elt_len;
  758. }
  759. trunnel_assert(ptr == output + written);
  760. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  761. {
  762. trunnel_assert(encoded_len >= 0);
  763. trunnel_assert((size_t)encoded_len == written);
  764. }
  765. #endif
  766. return written;
  767. truncated:
  768. result = -2;
  769. goto fail;
  770. check_failed:
  771. (void)msg;
  772. result = -1;
  773. goto fail;
  774. fail:
  775. trunnel_assert(result < 0);
  776. return result;
  777. }
  778. /** As rsa_ed_crosscert_parse(), but do not allocate the output
  779. * object.
  780. */
  781. static ssize_t
  782. rsa_ed_crosscert_parse_into(rsa_ed_crosscert_t *obj, const uint8_t *input, const size_t len_in)
  783. {
  784. const uint8_t *ptr = input;
  785. size_t remaining = len_in;
  786. ssize_t result = 0;
  787. (void)result;
  788. /* Parse u8 ed_key[32] */
  789. CHECK_REMAINING(32, truncated);
  790. memcpy(obj->ed_key, ptr, 32);
  791. remaining -= 32; ptr += 32;
  792. /* Parse u32 expiration */
  793. CHECK_REMAINING(4, truncated);
  794. obj->expiration = trunnel_ntohl(trunnel_get_uint32(ptr));
  795. remaining -= 4; ptr += 4;
  796. obj->end_of_signed = ptr;
  797. /* Parse u8 sig_len */
  798. CHECK_REMAINING(1, truncated);
  799. obj->sig_len = (trunnel_get_uint8(ptr));
  800. remaining -= 1; ptr += 1;
  801. /* Parse u8 sig[sig_len] */
  802. CHECK_REMAINING(obj->sig_len, truncated);
  803. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, obj->sig_len, {});
  804. obj->sig.n_ = obj->sig_len;
  805. if (obj->sig_len)
  806. memcpy(obj->sig.elts_, ptr, obj->sig_len);
  807. ptr += obj->sig_len; remaining -= obj->sig_len;
  808. trunnel_assert(ptr + remaining == input + len_in);
  809. return len_in - remaining;
  810. truncated:
  811. return -2;
  812. trunnel_alloc_failed:
  813. return -1;
  814. }
  815. ssize_t
  816. rsa_ed_crosscert_parse(rsa_ed_crosscert_t **output, const uint8_t *input, const size_t len_in)
  817. {
  818. ssize_t result;
  819. *output = rsa_ed_crosscert_new();
  820. if (NULL == *output)
  821. return -1;
  822. result = rsa_ed_crosscert_parse_into(*output, input, len_in);
  823. if (result < 0) {
  824. rsa_ed_crosscert_free(*output);
  825. *output = NULL;
  826. }
  827. return result;
  828. }
  829. auth1_t *
  830. auth1_new(void)
  831. {
  832. auth1_t *val = trunnel_calloc(1, sizeof(auth1_t));
  833. if (NULL == val)
  834. return NULL;
  835. return val;
  836. }
  837. /** Release all storage held inside 'obj', but do not free 'obj'.
  838. */
  839. static void
  840. auth1_clear(auth1_t *obj)
  841. {
  842. (void) obj;
  843. TRUNNEL_DYNARRAY_WIPE(&obj->sig);
  844. TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
  845. }
  846. void
  847. auth1_free(auth1_t *obj)
  848. {
  849. if (obj == NULL)
  850. return;
  851. auth1_clear(obj);
  852. trunnel_memwipe(obj, sizeof(auth1_t));
  853. trunnel_free_(obj);
  854. }
  855. size_t
  856. auth1_getlen_type(const auth1_t *inp)
  857. {
  858. (void)inp; return 8;
  859. }
  860. uint8_t
  861. auth1_get_type(const auth1_t *inp, size_t idx)
  862. {
  863. trunnel_assert(idx < 8);
  864. return inp->type[idx];
  865. }
  866. int
  867. auth1_set_type(auth1_t *inp, size_t idx, uint8_t elt)
  868. {
  869. trunnel_assert(idx < 8);
  870. inp->type[idx] = elt;
  871. return 0;
  872. }
  873. uint8_t *
  874. auth1_getarray_type(auth1_t *inp)
  875. {
  876. return inp->type;
  877. }
  878. size_t
  879. auth1_getlen_cid(const auth1_t *inp)
  880. {
  881. (void)inp; return 32;
  882. }
  883. uint8_t
  884. auth1_get_cid(const auth1_t *inp, size_t idx)
  885. {
  886. trunnel_assert(idx < 32);
  887. return inp->cid[idx];
  888. }
  889. int
  890. auth1_set_cid(auth1_t *inp, size_t idx, uint8_t elt)
  891. {
  892. trunnel_assert(idx < 32);
  893. inp->cid[idx] = elt;
  894. return 0;
  895. }
  896. uint8_t *
  897. auth1_getarray_cid(auth1_t *inp)
  898. {
  899. return inp->cid;
  900. }
  901. size_t
  902. auth1_getlen_sid(const auth1_t *inp)
  903. {
  904. (void)inp; return 32;
  905. }
  906. uint8_t
  907. auth1_get_sid(const auth1_t *inp, size_t idx)
  908. {
  909. trunnel_assert(idx < 32);
  910. return inp->sid[idx];
  911. }
  912. int
  913. auth1_set_sid(auth1_t *inp, size_t idx, uint8_t elt)
  914. {
  915. trunnel_assert(idx < 32);
  916. inp->sid[idx] = elt;
  917. return 0;
  918. }
  919. uint8_t *
  920. auth1_getarray_sid(auth1_t *inp)
  921. {
  922. return inp->sid;
  923. }
  924. size_t
  925. auth1_getlen_u1_cid_ed(const auth1_t *inp)
  926. {
  927. (void)inp; return 32;
  928. }
  929. uint8_t
  930. auth1_get_u1_cid_ed(const auth1_t *inp, size_t idx)
  931. {
  932. trunnel_assert(idx < 32);
  933. return inp->u1_cid_ed[idx];
  934. }
  935. int
  936. auth1_set_u1_cid_ed(auth1_t *inp, size_t idx, uint8_t elt)
  937. {
  938. trunnel_assert(idx < 32);
  939. inp->u1_cid_ed[idx] = elt;
  940. return 0;
  941. }
  942. uint8_t *
  943. auth1_getarray_u1_cid_ed(auth1_t *inp)
  944. {
  945. return inp->u1_cid_ed;
  946. }
  947. size_t
  948. auth1_getlen_u1_sid_ed(const auth1_t *inp)
  949. {
  950. (void)inp; return 32;
  951. }
  952. uint8_t
  953. auth1_get_u1_sid_ed(const auth1_t *inp, size_t idx)
  954. {
  955. trunnel_assert(idx < 32);
  956. return inp->u1_sid_ed[idx];
  957. }
  958. int
  959. auth1_set_u1_sid_ed(auth1_t *inp, size_t idx, uint8_t elt)
  960. {
  961. trunnel_assert(idx < 32);
  962. inp->u1_sid_ed[idx] = elt;
  963. return 0;
  964. }
  965. uint8_t *
  966. auth1_getarray_u1_sid_ed(auth1_t *inp)
  967. {
  968. return inp->u1_sid_ed;
  969. }
  970. size_t
  971. auth1_getlen_slog(const auth1_t *inp)
  972. {
  973. (void)inp; return 32;
  974. }
  975. uint8_t
  976. auth1_get_slog(const auth1_t *inp, size_t idx)
  977. {
  978. trunnel_assert(idx < 32);
  979. return inp->slog[idx];
  980. }
  981. int
  982. auth1_set_slog(auth1_t *inp, size_t idx, uint8_t elt)
  983. {
  984. trunnel_assert(idx < 32);
  985. inp->slog[idx] = elt;
  986. return 0;
  987. }
  988. uint8_t *
  989. auth1_getarray_slog(auth1_t *inp)
  990. {
  991. return inp->slog;
  992. }
  993. size_t
  994. auth1_getlen_clog(const auth1_t *inp)
  995. {
  996. (void)inp; return 32;
  997. }
  998. uint8_t
  999. auth1_get_clog(const auth1_t *inp, size_t idx)
  1000. {
  1001. trunnel_assert(idx < 32);
  1002. return inp->clog[idx];
  1003. }
  1004. int
  1005. auth1_set_clog(auth1_t *inp, size_t idx, uint8_t elt)
  1006. {
  1007. trunnel_assert(idx < 32);
  1008. inp->clog[idx] = elt;
  1009. return 0;
  1010. }
  1011. uint8_t *
  1012. auth1_getarray_clog(auth1_t *inp)
  1013. {
  1014. return inp->clog;
  1015. }
  1016. size_t
  1017. auth1_getlen_scert(const auth1_t *inp)
  1018. {
  1019. (void)inp; return 32;
  1020. }
  1021. uint8_t
  1022. auth1_get_scert(const auth1_t *inp, size_t idx)
  1023. {
  1024. trunnel_assert(idx < 32);
  1025. return inp->scert[idx];
  1026. }
  1027. int
  1028. auth1_set_scert(auth1_t *inp, size_t idx, uint8_t elt)
  1029. {
  1030. trunnel_assert(idx < 32);
  1031. inp->scert[idx] = elt;
  1032. return 0;
  1033. }
  1034. uint8_t *
  1035. auth1_getarray_scert(auth1_t *inp)
  1036. {
  1037. return inp->scert;
  1038. }
  1039. size_t
  1040. auth1_getlen_tlssecrets(const auth1_t *inp)
  1041. {
  1042. (void)inp; return 32;
  1043. }
  1044. uint8_t
  1045. auth1_get_tlssecrets(const auth1_t *inp, size_t idx)
  1046. {
  1047. trunnel_assert(idx < 32);
  1048. return inp->tlssecrets[idx];
  1049. }
  1050. int
  1051. auth1_set_tlssecrets(auth1_t *inp, size_t idx, uint8_t elt)
  1052. {
  1053. trunnel_assert(idx < 32);
  1054. inp->tlssecrets[idx] = elt;
  1055. return 0;
  1056. }
  1057. uint8_t *
  1058. auth1_getarray_tlssecrets(auth1_t *inp)
  1059. {
  1060. return inp->tlssecrets;
  1061. }
  1062. const uint8_t *
  1063. auth1_get_end_of_fixed_part(const auth1_t *inp)
  1064. {
  1065. return inp->end_of_fixed_part;
  1066. }
  1067. size_t
  1068. auth1_getlen_rand(const auth1_t *inp)
  1069. {
  1070. (void)inp; return 24;
  1071. }
  1072. uint8_t
  1073. auth1_get_rand(const auth1_t *inp, size_t idx)
  1074. {
  1075. trunnel_assert(idx < 24);
  1076. return inp->rand[idx];
  1077. }
  1078. int
  1079. auth1_set_rand(auth1_t *inp, size_t idx, uint8_t elt)
  1080. {
  1081. trunnel_assert(idx < 24);
  1082. inp->rand[idx] = elt;
  1083. return 0;
  1084. }
  1085. uint8_t *
  1086. auth1_getarray_rand(auth1_t *inp)
  1087. {
  1088. return inp->rand;
  1089. }
  1090. const uint8_t *
  1091. auth1_get_end_of_signed(const auth1_t *inp)
  1092. {
  1093. return inp->end_of_signed;
  1094. }
  1095. size_t
  1096. auth1_getlen_sig(const auth1_t *inp)
  1097. {
  1098. return TRUNNEL_DYNARRAY_LEN(&inp->sig);
  1099. }
  1100. uint8_t
  1101. auth1_get_sig(auth1_t *inp, size_t idx)
  1102. {
  1103. return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
  1104. }
  1105. int
  1106. auth1_set_sig(auth1_t *inp, size_t idx, uint8_t elt)
  1107. {
  1108. TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
  1109. return 0;
  1110. }
  1111. int
  1112. auth1_add_sig(auth1_t *inp, uint8_t elt)
  1113. {
  1114. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
  1115. return 0;
  1116. trunnel_alloc_failed:
  1117. TRUNNEL_SET_ERROR_CODE(inp);
  1118. return -1;
  1119. }
  1120. uint8_t *
  1121. auth1_getarray_sig(auth1_t *inp)
  1122. {
  1123. return inp->sig.elts_;
  1124. }
  1125. int
  1126. auth1_setlen_sig(auth1_t *inp, size_t newlen)
  1127. {
  1128. uint8_t *newptr;
  1129. newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
  1130. &inp->sig.n_, inp->sig.elts_, newlen,
  1131. sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
  1132. &inp->trunnel_error_code_);
  1133. if (newlen != 0 && newptr == NULL)
  1134. goto trunnel_alloc_failed;
  1135. inp->sig.elts_ = newptr;
  1136. return 0;
  1137. trunnel_alloc_failed:
  1138. TRUNNEL_SET_ERROR_CODE(inp);
  1139. return -1;
  1140. }
  1141. const char *
  1142. auth1_check(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1143. {
  1144. if (obj == NULL)
  1145. return "Object was NULL";
  1146. if (obj->trunnel_error_code_)
  1147. return "A set function failed on this object";
  1148. if (auth_ctx_ctx == NULL)
  1149. return "Context was NULL";
  1150. switch (auth_ctx_ctx->is_ed) {
  1151. case 0:
  1152. break;
  1153. case 1:
  1154. break;
  1155. default:
  1156. return "Bad tag for union";
  1157. break;
  1158. }
  1159. return NULL;
  1160. }
  1161. ssize_t
  1162. auth1_encoded_len(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1163. {
  1164. ssize_t result = 0;
  1165. if (NULL != auth1_check(obj, auth_ctx_ctx))
  1166. return -1;
  1167. /* Length of u8 type[8] */
  1168. result += 8;
  1169. /* Length of u8 cid[32] */
  1170. result += 32;
  1171. /* Length of u8 sid[32] */
  1172. result += 32;
  1173. switch (auth_ctx_ctx->is_ed) {
  1174. case 0:
  1175. break;
  1176. case 1:
  1177. /* Length of u8 u1_cid_ed[32] */
  1178. result += 32;
  1179. /* Length of u8 u1_sid_ed[32] */
  1180. result += 32;
  1181. break;
  1182. default:
  1183. trunnel_assert(0);
  1184. break;
  1185. }
  1186. /* Length of u8 slog[32] */
  1187. result += 32;
  1188. /* Length of u8 clog[32] */
  1189. result += 32;
  1190. /* Length of u8 scert[32] */
  1191. result += 32;
  1192. /* Length of u8 tlssecrets[32] */
  1193. result += 32;
  1194. /* Length of u8 rand[24] */
  1195. result += 24;
  1196. /* Length of u8 sig[] */
  1197. result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
  1198. return result;
  1199. }
  1200. int
  1201. auth1_clear_errors(auth1_t *obj)
  1202. {
  1203. int r = obj->trunnel_error_code_;
  1204. obj->trunnel_error_code_ = 0;
  1205. return r;
  1206. }
  1207. ssize_t
  1208. auth1_encode(uint8_t *output, const size_t avail, const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1209. {
  1210. ssize_t result = 0;
  1211. size_t written = 0;
  1212. uint8_t *ptr = output;
  1213. const char *msg;
  1214. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1215. const ssize_t encoded_len = auth1_encoded_len(obj, auth_ctx_ctx);
  1216. #endif
  1217. if (NULL != (msg = auth1_check(obj, auth_ctx_ctx)))
  1218. goto check_failed;
  1219. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1220. trunnel_assert(encoded_len >= 0);
  1221. #endif
  1222. /* Encode u8 type[8] */
  1223. trunnel_assert(written <= avail);
  1224. if (avail - written < 8)
  1225. goto truncated;
  1226. memcpy(ptr, obj->type, 8);
  1227. written += 8; ptr += 8;
  1228. /* Encode u8 cid[32] */
  1229. trunnel_assert(written <= avail);
  1230. if (avail - written < 32)
  1231. goto truncated;
  1232. memcpy(ptr, obj->cid, 32);
  1233. written += 32; ptr += 32;
  1234. /* Encode u8 sid[32] */
  1235. trunnel_assert(written <= avail);
  1236. if (avail - written < 32)
  1237. goto truncated;
  1238. memcpy(ptr, obj->sid, 32);
  1239. written += 32; ptr += 32;
  1240. /* Encode union u1[auth_ctx.is_ed] */
  1241. trunnel_assert(written <= avail);
  1242. switch (auth_ctx_ctx->is_ed) {
  1243. case 0:
  1244. break;
  1245. case 1:
  1246. /* Encode u8 u1_cid_ed[32] */
  1247. trunnel_assert(written <= avail);
  1248. if (avail - written < 32)
  1249. goto truncated;
  1250. memcpy(ptr, obj->u1_cid_ed, 32);
  1251. written += 32; ptr += 32;
  1252. /* Encode u8 u1_sid_ed[32] */
  1253. trunnel_assert(written <= avail);
  1254. if (avail - written < 32)
  1255. goto truncated;
  1256. memcpy(ptr, obj->u1_sid_ed, 32);
  1257. written += 32; ptr += 32;
  1258. break;
  1259. default:
  1260. trunnel_assert(0);
  1261. break;
  1262. }
  1263. /* Encode u8 slog[32] */
  1264. trunnel_assert(written <= avail);
  1265. if (avail - written < 32)
  1266. goto truncated;
  1267. memcpy(ptr, obj->slog, 32);
  1268. written += 32; ptr += 32;
  1269. /* Encode u8 clog[32] */
  1270. trunnel_assert(written <= avail);
  1271. if (avail - written < 32)
  1272. goto truncated;
  1273. memcpy(ptr, obj->clog, 32);
  1274. written += 32; ptr += 32;
  1275. /* Encode u8 scert[32] */
  1276. trunnel_assert(written <= avail);
  1277. if (avail - written < 32)
  1278. goto truncated;
  1279. memcpy(ptr, obj->scert, 32);
  1280. written += 32; ptr += 32;
  1281. /* Encode u8 tlssecrets[32] */
  1282. trunnel_assert(written <= avail);
  1283. if (avail - written < 32)
  1284. goto truncated;
  1285. memcpy(ptr, obj->tlssecrets, 32);
  1286. written += 32; ptr += 32;
  1287. /* Encode u8 rand[24] */
  1288. trunnel_assert(written <= avail);
  1289. if (avail - written < 24)
  1290. goto truncated;
  1291. memcpy(ptr, obj->rand, 24);
  1292. written += 24; ptr += 24;
  1293. /* Encode u8 sig[] */
  1294. {
  1295. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
  1296. trunnel_assert(written <= avail);
  1297. if (avail - written < elt_len)
  1298. goto truncated;
  1299. if (elt_len)
  1300. memcpy(ptr, obj->sig.elts_, elt_len);
  1301. written += elt_len; ptr += elt_len;
  1302. }
  1303. trunnel_assert(ptr == output + written);
  1304. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1305. {
  1306. trunnel_assert(encoded_len >= 0);
  1307. trunnel_assert((size_t)encoded_len == written);
  1308. }
  1309. #endif
  1310. return written;
  1311. truncated:
  1312. result = -2;
  1313. goto fail;
  1314. check_failed:
  1315. (void)msg;
  1316. result = -1;
  1317. goto fail;
  1318. fail:
  1319. trunnel_assert(result < 0);
  1320. return result;
  1321. }
  1322. /** As auth1_parse(), but do not allocate the output object.
  1323. */
  1324. static ssize_t
  1325. auth1_parse_into(auth1_t *obj, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
  1326. {
  1327. const uint8_t *ptr = input;
  1328. size_t remaining = len_in;
  1329. ssize_t result = 0;
  1330. (void)result;
  1331. if (auth_ctx_ctx == NULL)
  1332. return -1;
  1333. /* Parse u8 type[8] */
  1334. CHECK_REMAINING(8, truncated);
  1335. memcpy(obj->type, ptr, 8);
  1336. remaining -= 8; ptr += 8;
  1337. /* Parse u8 cid[32] */
  1338. CHECK_REMAINING(32, truncated);
  1339. memcpy(obj->cid, ptr, 32);
  1340. remaining -= 32; ptr += 32;
  1341. /* Parse u8 sid[32] */
  1342. CHECK_REMAINING(32, truncated);
  1343. memcpy(obj->sid, ptr, 32);
  1344. remaining -= 32; ptr += 32;
  1345. /* Parse union u1[auth_ctx.is_ed] */
  1346. switch (auth_ctx_ctx->is_ed) {
  1347. case 0:
  1348. break;
  1349. case 1:
  1350. /* Parse u8 u1_cid_ed[32] */
  1351. CHECK_REMAINING(32, truncated);
  1352. memcpy(obj->u1_cid_ed, ptr, 32);
  1353. remaining -= 32; ptr += 32;
  1354. /* Parse u8 u1_sid_ed[32] */
  1355. CHECK_REMAINING(32, truncated);
  1356. memcpy(obj->u1_sid_ed, ptr, 32);
  1357. remaining -= 32; ptr += 32;
  1358. break;
  1359. default:
  1360. goto fail;
  1361. break;
  1362. }
  1363. /* Parse u8 slog[32] */
  1364. CHECK_REMAINING(32, truncated);
  1365. memcpy(obj->slog, ptr, 32);
  1366. remaining -= 32; ptr += 32;
  1367. /* Parse u8 clog[32] */
  1368. CHECK_REMAINING(32, truncated);
  1369. memcpy(obj->clog, ptr, 32);
  1370. remaining -= 32; ptr += 32;
  1371. /* Parse u8 scert[32] */
  1372. CHECK_REMAINING(32, truncated);
  1373. memcpy(obj->scert, ptr, 32);
  1374. remaining -= 32; ptr += 32;
  1375. /* Parse u8 tlssecrets[32] */
  1376. CHECK_REMAINING(32, truncated);
  1377. memcpy(obj->tlssecrets, ptr, 32);
  1378. remaining -= 32; ptr += 32;
  1379. obj->end_of_fixed_part = ptr;
  1380. /* Parse u8 rand[24] */
  1381. CHECK_REMAINING(24, truncated);
  1382. memcpy(obj->rand, ptr, 24);
  1383. remaining -= 24; ptr += 24;
  1384. obj->end_of_signed = ptr;
  1385. /* Parse u8 sig[] */
  1386. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, remaining, {});
  1387. obj->sig.n_ = remaining;
  1388. if (remaining)
  1389. memcpy(obj->sig.elts_, ptr, remaining);
  1390. ptr += remaining; remaining -= remaining;
  1391. trunnel_assert(ptr + remaining == input + len_in);
  1392. return len_in - remaining;
  1393. truncated:
  1394. return -2;
  1395. trunnel_alloc_failed:
  1396. return -1;
  1397. fail:
  1398. result = -1;
  1399. return result;
  1400. }
  1401. ssize_t
  1402. auth1_parse(auth1_t **output, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
  1403. {
  1404. ssize_t result;
  1405. *output = auth1_new();
  1406. if (NULL == *output)
  1407. return -1;
  1408. result = auth1_parse_into(*output, input, len_in, auth_ctx_ctx);
  1409. if (result < 0) {
  1410. auth1_free(*output);
  1411. *output = NULL;
  1412. }
  1413. return result;
  1414. }
  1415. certs_cell_t *
  1416. certs_cell_new(void)
  1417. {
  1418. certs_cell_t *val = trunnel_calloc(1, sizeof(certs_cell_t));
  1419. if (NULL == val)
  1420. return NULL;
  1421. return val;
  1422. }
  1423. /** Release all storage held inside 'obj', but do not free 'obj'.
  1424. */
  1425. static void
  1426. certs_cell_clear(certs_cell_t *obj)
  1427. {
  1428. (void) obj;
  1429. {
  1430. unsigned idx;
  1431. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1432. certs_cell_cert_free(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1433. }
  1434. }
  1435. TRUNNEL_DYNARRAY_WIPE(&obj->certs);
  1436. TRUNNEL_DYNARRAY_CLEAR(&obj->certs);
  1437. }
  1438. void
  1439. certs_cell_free(certs_cell_t *obj)
  1440. {
  1441. if (obj == NULL)
  1442. return;
  1443. certs_cell_clear(obj);
  1444. trunnel_memwipe(obj, sizeof(certs_cell_t));
  1445. trunnel_free_(obj);
  1446. }
  1447. uint8_t
  1448. certs_cell_get_n_certs(certs_cell_t *inp)
  1449. {
  1450. return inp->n_certs;
  1451. }
  1452. int
  1453. certs_cell_set_n_certs(certs_cell_t *inp, uint8_t val)
  1454. {
  1455. inp->n_certs = val;
  1456. return 0;
  1457. }
  1458. size_t
  1459. certs_cell_getlen_certs(const certs_cell_t *inp)
  1460. {
  1461. return TRUNNEL_DYNARRAY_LEN(&inp->certs);
  1462. }
  1463. struct certs_cell_cert_st *
  1464. certs_cell_get_certs(certs_cell_t *inp, size_t idx)
  1465. {
  1466. return TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
  1467. }
  1468. int
  1469. certs_cell_set_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
  1470. {
  1471. certs_cell_cert_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
  1472. if (oldval && oldval != elt)
  1473. certs_cell_cert_free(oldval);
  1474. return certs_cell_set0_certs(inp, idx, elt);
  1475. }
  1476. int
  1477. certs_cell_set0_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
  1478. {
  1479. TRUNNEL_DYNARRAY_SET(&inp->certs, idx, elt);
  1480. return 0;
  1481. }
  1482. int
  1483. certs_cell_add_certs(certs_cell_t *inp, struct certs_cell_cert_st * elt)
  1484. {
  1485. #if SIZE_MAX >= UINT8_MAX
  1486. if (inp->certs.n_ == UINT8_MAX)
  1487. goto trunnel_alloc_failed;
  1488. #endif
  1489. TRUNNEL_DYNARRAY_ADD(struct certs_cell_cert_st *, &inp->certs, elt, {});
  1490. return 0;
  1491. trunnel_alloc_failed:
  1492. TRUNNEL_SET_ERROR_CODE(inp);
  1493. return -1;
  1494. }
  1495. struct certs_cell_cert_st * *
  1496. certs_cell_getarray_certs(certs_cell_t *inp)
  1497. {
  1498. return inp->certs.elts_;
  1499. }
  1500. int
  1501. certs_cell_setlen_certs(certs_cell_t *inp, size_t newlen)
  1502. {
  1503. struct certs_cell_cert_st * *newptr;
  1504. #if UINT8_MAX < SIZE_MAX
  1505. if (newlen > UINT8_MAX)
  1506. goto trunnel_alloc_failed;
  1507. #endif
  1508. newptr = trunnel_dynarray_setlen(&inp->certs.allocated_,
  1509. &inp->certs.n_, inp->certs.elts_, newlen,
  1510. sizeof(inp->certs.elts_[0]), (trunnel_free_fn_t) certs_cell_cert_free,
  1511. &inp->trunnel_error_code_);
  1512. if (newlen != 0 && newptr == NULL)
  1513. goto trunnel_alloc_failed;
  1514. inp->certs.elts_ = newptr;
  1515. return 0;
  1516. trunnel_alloc_failed:
  1517. TRUNNEL_SET_ERROR_CODE(inp);
  1518. return -1;
  1519. }
  1520. const char *
  1521. certs_cell_check(const certs_cell_t *obj)
  1522. {
  1523. if (obj == NULL)
  1524. return "Object was NULL";
  1525. if (obj->trunnel_error_code_)
  1526. return "A set function failed on this object";
  1527. {
  1528. const char *msg;
  1529. unsigned idx;
  1530. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1531. if (NULL != (msg = certs_cell_cert_check(TRUNNEL_DYNARRAY_GET(&obj->certs, idx))))
  1532. return msg;
  1533. }
  1534. }
  1535. if (TRUNNEL_DYNARRAY_LEN(&obj->certs) != obj->n_certs)
  1536. return "Length mismatch for certs";
  1537. return NULL;
  1538. }
  1539. ssize_t
  1540. certs_cell_encoded_len(const certs_cell_t *obj)
  1541. {
  1542. ssize_t result = 0;
  1543. if (NULL != certs_cell_check(obj))
  1544. return -1;
  1545. /* Length of u8 n_certs */
  1546. result += 1;
  1547. /* Length of struct certs_cell_cert certs[n_certs] */
  1548. {
  1549. unsigned idx;
  1550. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1551. result += certs_cell_cert_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1552. }
  1553. }
  1554. return result;
  1555. }
  1556. int
  1557. certs_cell_clear_errors(certs_cell_t *obj)
  1558. {
  1559. int r = obj->trunnel_error_code_;
  1560. obj->trunnel_error_code_ = 0;
  1561. return r;
  1562. }
  1563. ssize_t
  1564. certs_cell_encode(uint8_t *output, const size_t avail, const certs_cell_t *obj)
  1565. {
  1566. ssize_t result = 0;
  1567. size_t written = 0;
  1568. uint8_t *ptr = output;
  1569. const char *msg;
  1570. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1571. const ssize_t encoded_len = certs_cell_encoded_len(obj);
  1572. #endif
  1573. if (NULL != (msg = certs_cell_check(obj)))
  1574. goto check_failed;
  1575. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1576. trunnel_assert(encoded_len >= 0);
  1577. #endif
  1578. /* Encode u8 n_certs */
  1579. trunnel_assert(written <= avail);
  1580. if (avail - written < 1)
  1581. goto truncated;
  1582. trunnel_set_uint8(ptr, (obj->n_certs));
  1583. written += 1; ptr += 1;
  1584. /* Encode struct certs_cell_cert certs[n_certs] */
  1585. {
  1586. unsigned idx;
  1587. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1588. trunnel_assert(written <= avail);
  1589. result = certs_cell_cert_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1590. if (result < 0)
  1591. goto fail; /* XXXXXXX !*/
  1592. written += result; ptr += result;
  1593. }
  1594. }
  1595. trunnel_assert(ptr == output + written);
  1596. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1597. {
  1598. trunnel_assert(encoded_len >= 0);
  1599. trunnel_assert((size_t)encoded_len == written);
  1600. }
  1601. #endif
  1602. return written;
  1603. truncated:
  1604. result = -2;
  1605. goto fail;
  1606. check_failed:
  1607. (void)msg;
  1608. result = -1;
  1609. goto fail;
  1610. fail:
  1611. trunnel_assert(result < 0);
  1612. return result;
  1613. }
  1614. /** As certs_cell_parse(), but do not allocate the output object.
  1615. */
  1616. static ssize_t
  1617. certs_cell_parse_into(certs_cell_t *obj, const uint8_t *input, const size_t len_in)
  1618. {
  1619. const uint8_t *ptr = input;
  1620. size_t remaining = len_in;
  1621. ssize_t result = 0;
  1622. (void)result;
  1623. /* Parse u8 n_certs */
  1624. CHECK_REMAINING(1, truncated);
  1625. obj->n_certs = (trunnel_get_uint8(ptr));
  1626. remaining -= 1; ptr += 1;
  1627. /* Parse struct certs_cell_cert certs[n_certs] */
  1628. TRUNNEL_DYNARRAY_EXPAND(certs_cell_cert_t *, &obj->certs, obj->n_certs, {});
  1629. {
  1630. certs_cell_cert_t * elt;
  1631. unsigned idx;
  1632. for (idx = 0; idx < obj->n_certs; ++idx) {
  1633. result = certs_cell_cert_parse(&elt, ptr, remaining);
  1634. if (result < 0)
  1635. goto relay_fail;
  1636. trunnel_assert((size_t)result <= remaining);
  1637. remaining -= result; ptr += result;
  1638. TRUNNEL_DYNARRAY_ADD(certs_cell_cert_t *, &obj->certs, elt, {certs_cell_cert_free(elt);});
  1639. }
  1640. }
  1641. trunnel_assert(ptr + remaining == input + len_in);
  1642. return len_in - remaining;
  1643. truncated:
  1644. return -2;
  1645. relay_fail:
  1646. trunnel_assert(result < 0);
  1647. return result;
  1648. trunnel_alloc_failed:
  1649. return -1;
  1650. }
  1651. ssize_t
  1652. certs_cell_parse(certs_cell_t **output, const uint8_t *input, const size_t len_in)
  1653. {
  1654. ssize_t result;
  1655. *output = certs_cell_new();
  1656. if (NULL == *output)
  1657. return -1;
  1658. result = certs_cell_parse_into(*output, input, len_in);
  1659. if (result < 0) {
  1660. certs_cell_free(*output);
  1661. *output = NULL;
  1662. }
  1663. return result;
  1664. }