link_handshake.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /* link_handshake.c -- generated by Trunnel v1.4-pre.
  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 (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 (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. memcpy(ptr, obj->body.elts_, elt_len);
  484. written += elt_len; ptr += elt_len;
  485. }
  486. trunnel_assert(ptr == output + written);
  487. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  488. {
  489. trunnel_assert(encoded_len >= 0);
  490. trunnel_assert((size_t)encoded_len == written);
  491. }
  492. #endif
  493. return written;
  494. truncated:
  495. result = -2;
  496. goto fail;
  497. check_failed:
  498. (void)msg;
  499. result = -1;
  500. goto fail;
  501. fail:
  502. trunnel_assert(result < 0);
  503. return result;
  504. }
  505. /** As certs_cell_cert_parse(), but do not allocate the output object.
  506. */
  507. static ssize_t
  508. certs_cell_cert_parse_into(certs_cell_cert_t *obj, const uint8_t *input, const size_t len_in)
  509. {
  510. const uint8_t *ptr = input;
  511. size_t remaining = len_in;
  512. ssize_t result = 0;
  513. (void)result;
  514. /* Parse u8 cert_type */
  515. CHECK_REMAINING(1, truncated);
  516. obj->cert_type = (trunnel_get_uint8(ptr));
  517. remaining -= 1; ptr += 1;
  518. /* Parse u16 cert_len */
  519. CHECK_REMAINING(2, truncated);
  520. obj->cert_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  521. remaining -= 2; ptr += 2;
  522. /* Parse u8 body[cert_len] */
  523. CHECK_REMAINING(obj->cert_len, truncated);
  524. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->body, obj->cert_len, {});
  525. obj->body.n_ = obj->cert_len;
  526. memcpy(obj->body.elts_, ptr, obj->cert_len);
  527. ptr += obj->cert_len; remaining -= obj->cert_len;
  528. trunnel_assert(ptr + remaining == input + len_in);
  529. return len_in - remaining;
  530. truncated:
  531. return -2;
  532. trunnel_alloc_failed:
  533. return -1;
  534. }
  535. ssize_t
  536. certs_cell_cert_parse(certs_cell_cert_t **output, const uint8_t *input, const size_t len_in)
  537. {
  538. ssize_t result;
  539. *output = certs_cell_cert_new();
  540. if (NULL == *output)
  541. return -1;
  542. result = certs_cell_cert_parse_into(*output, input, len_in);
  543. if (result < 0) {
  544. certs_cell_cert_free(*output);
  545. *output = NULL;
  546. }
  547. return result;
  548. }
  549. rsa_ed_crosscert_t *
  550. rsa_ed_crosscert_new(void)
  551. {
  552. rsa_ed_crosscert_t *val = trunnel_calloc(1, sizeof(rsa_ed_crosscert_t));
  553. if (NULL == val)
  554. return NULL;
  555. return val;
  556. }
  557. /** Release all storage held inside 'obj', but do not free 'obj'.
  558. */
  559. static void
  560. rsa_ed_crosscert_clear(rsa_ed_crosscert_t *obj)
  561. {
  562. (void) obj;
  563. TRUNNEL_DYNARRAY_WIPE(&obj->sig);
  564. TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
  565. }
  566. void
  567. rsa_ed_crosscert_free(rsa_ed_crosscert_t *obj)
  568. {
  569. if (obj == NULL)
  570. return;
  571. rsa_ed_crosscert_clear(obj);
  572. trunnel_memwipe(obj, sizeof(rsa_ed_crosscert_t));
  573. trunnel_free_(obj);
  574. }
  575. size_t
  576. rsa_ed_crosscert_getlen_ed_key(const rsa_ed_crosscert_t *inp)
  577. {
  578. (void)inp; return 32;
  579. }
  580. uint8_t
  581. rsa_ed_crosscert_get_ed_key(const rsa_ed_crosscert_t *inp, size_t idx)
  582. {
  583. trunnel_assert(idx < 32);
  584. return inp->ed_key[idx];
  585. }
  586. int
  587. rsa_ed_crosscert_set_ed_key(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
  588. {
  589. trunnel_assert(idx < 32);
  590. inp->ed_key[idx] = elt;
  591. return 0;
  592. }
  593. uint8_t *
  594. rsa_ed_crosscert_getarray_ed_key(rsa_ed_crosscert_t *inp)
  595. {
  596. return inp->ed_key;
  597. }
  598. uint32_t
  599. rsa_ed_crosscert_get_expiration(rsa_ed_crosscert_t *inp)
  600. {
  601. return inp->expiration;
  602. }
  603. int
  604. rsa_ed_crosscert_set_expiration(rsa_ed_crosscert_t *inp, uint32_t val)
  605. {
  606. inp->expiration = val;
  607. return 0;
  608. }
  609. const uint8_t *
  610. rsa_ed_crosscert_get_end_of_signed(const rsa_ed_crosscert_t *inp)
  611. {
  612. return inp->end_of_signed;
  613. }
  614. uint8_t
  615. rsa_ed_crosscert_get_sig_len(rsa_ed_crosscert_t *inp)
  616. {
  617. return inp->sig_len;
  618. }
  619. int
  620. rsa_ed_crosscert_set_sig_len(rsa_ed_crosscert_t *inp, uint8_t val)
  621. {
  622. inp->sig_len = val;
  623. return 0;
  624. }
  625. size_t
  626. rsa_ed_crosscert_getlen_sig(const rsa_ed_crosscert_t *inp)
  627. {
  628. return TRUNNEL_DYNARRAY_LEN(&inp->sig);
  629. }
  630. uint8_t
  631. rsa_ed_crosscert_get_sig(rsa_ed_crosscert_t *inp, size_t idx)
  632. {
  633. return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
  634. }
  635. int
  636. rsa_ed_crosscert_set_sig(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
  637. {
  638. TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
  639. return 0;
  640. }
  641. int
  642. rsa_ed_crosscert_add_sig(rsa_ed_crosscert_t *inp, uint8_t elt)
  643. {
  644. #if SIZE_MAX >= UINT8_MAX
  645. if (inp->sig.n_ == UINT8_MAX)
  646. goto trunnel_alloc_failed;
  647. #endif
  648. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
  649. return 0;
  650. trunnel_alloc_failed:
  651. TRUNNEL_SET_ERROR_CODE(inp);
  652. return -1;
  653. }
  654. uint8_t *
  655. rsa_ed_crosscert_getarray_sig(rsa_ed_crosscert_t *inp)
  656. {
  657. return inp->sig.elts_;
  658. }
  659. int
  660. rsa_ed_crosscert_setlen_sig(rsa_ed_crosscert_t *inp, size_t newlen)
  661. {
  662. uint8_t *newptr;
  663. #if UINT8_MAX < SIZE_MAX
  664. if (newlen > UINT8_MAX)
  665. goto trunnel_alloc_failed;
  666. #endif
  667. newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
  668. &inp->sig.n_, inp->sig.elts_, newlen,
  669. sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
  670. &inp->trunnel_error_code_);
  671. if (newptr == NULL)
  672. goto trunnel_alloc_failed;
  673. inp->sig.elts_ = newptr;
  674. return 0;
  675. trunnel_alloc_failed:
  676. TRUNNEL_SET_ERROR_CODE(inp);
  677. return -1;
  678. }
  679. const char *
  680. rsa_ed_crosscert_check(const rsa_ed_crosscert_t *obj)
  681. {
  682. if (obj == NULL)
  683. return "Object was NULL";
  684. if (obj->trunnel_error_code_)
  685. return "A set function failed on this object";
  686. if (TRUNNEL_DYNARRAY_LEN(&obj->sig) != obj->sig_len)
  687. return "Length mismatch for sig";
  688. return NULL;
  689. }
  690. ssize_t
  691. rsa_ed_crosscert_encoded_len(const rsa_ed_crosscert_t *obj)
  692. {
  693. ssize_t result = 0;
  694. if (NULL != rsa_ed_crosscert_check(obj))
  695. return -1;
  696. /* Length of u8 ed_key[32] */
  697. result += 32;
  698. /* Length of u32 expiration */
  699. result += 4;
  700. /* Length of u8 sig_len */
  701. result += 1;
  702. /* Length of u8 sig[sig_len] */
  703. result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
  704. return result;
  705. }
  706. int
  707. rsa_ed_crosscert_clear_errors(rsa_ed_crosscert_t *obj)
  708. {
  709. int r = obj->trunnel_error_code_;
  710. obj->trunnel_error_code_ = 0;
  711. return r;
  712. }
  713. ssize_t
  714. rsa_ed_crosscert_encode(uint8_t *output, const size_t avail, const rsa_ed_crosscert_t *obj)
  715. {
  716. ssize_t result = 0;
  717. size_t written = 0;
  718. uint8_t *ptr = output;
  719. const char *msg;
  720. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  721. const ssize_t encoded_len = rsa_ed_crosscert_encoded_len(obj);
  722. #endif
  723. if (NULL != (msg = rsa_ed_crosscert_check(obj)))
  724. goto check_failed;
  725. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  726. trunnel_assert(encoded_len >= 0);
  727. #endif
  728. /* Encode u8 ed_key[32] */
  729. trunnel_assert(written <= avail);
  730. if (avail - written < 32)
  731. goto truncated;
  732. memcpy(ptr, obj->ed_key, 32);
  733. written += 32; ptr += 32;
  734. /* Encode u32 expiration */
  735. trunnel_assert(written <= avail);
  736. if (avail - written < 4)
  737. goto truncated;
  738. trunnel_set_uint32(ptr, trunnel_htonl(obj->expiration));
  739. written += 4; ptr += 4;
  740. /* Encode u8 sig_len */
  741. trunnel_assert(written <= avail);
  742. if (avail - written < 1)
  743. goto truncated;
  744. trunnel_set_uint8(ptr, (obj->sig_len));
  745. written += 1; ptr += 1;
  746. /* Encode u8 sig[sig_len] */
  747. {
  748. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
  749. trunnel_assert(obj->sig_len == elt_len);
  750. trunnel_assert(written <= avail);
  751. if (avail - written < elt_len)
  752. goto truncated;
  753. memcpy(ptr, obj->sig.elts_, elt_len);
  754. written += elt_len; ptr += elt_len;
  755. }
  756. trunnel_assert(ptr == output + written);
  757. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  758. {
  759. trunnel_assert(encoded_len >= 0);
  760. trunnel_assert((size_t)encoded_len == written);
  761. }
  762. #endif
  763. return written;
  764. truncated:
  765. result = -2;
  766. goto fail;
  767. check_failed:
  768. (void)msg;
  769. result = -1;
  770. goto fail;
  771. fail:
  772. trunnel_assert(result < 0);
  773. return result;
  774. }
  775. /** As rsa_ed_crosscert_parse(), but do not allocate the output
  776. * object.
  777. */
  778. static ssize_t
  779. rsa_ed_crosscert_parse_into(rsa_ed_crosscert_t *obj, const uint8_t *input, const size_t len_in)
  780. {
  781. const uint8_t *ptr = input;
  782. size_t remaining = len_in;
  783. ssize_t result = 0;
  784. (void)result;
  785. /* Parse u8 ed_key[32] */
  786. CHECK_REMAINING(32, truncated);
  787. memcpy(obj->ed_key, ptr, 32);
  788. remaining -= 32; ptr += 32;
  789. /* Parse u32 expiration */
  790. CHECK_REMAINING(4, truncated);
  791. obj->expiration = trunnel_ntohl(trunnel_get_uint32(ptr));
  792. remaining -= 4; ptr += 4;
  793. obj->end_of_signed = ptr;
  794. /* Parse u8 sig_len */
  795. CHECK_REMAINING(1, truncated);
  796. obj->sig_len = (trunnel_get_uint8(ptr));
  797. remaining -= 1; ptr += 1;
  798. /* Parse u8 sig[sig_len] */
  799. CHECK_REMAINING(obj->sig_len, truncated);
  800. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, obj->sig_len, {});
  801. obj->sig.n_ = obj->sig_len;
  802. memcpy(obj->sig.elts_, ptr, obj->sig_len);
  803. ptr += obj->sig_len; remaining -= obj->sig_len;
  804. trunnel_assert(ptr + remaining == input + len_in);
  805. return len_in - remaining;
  806. truncated:
  807. return -2;
  808. trunnel_alloc_failed:
  809. return -1;
  810. }
  811. ssize_t
  812. rsa_ed_crosscert_parse(rsa_ed_crosscert_t **output, const uint8_t *input, const size_t len_in)
  813. {
  814. ssize_t result;
  815. *output = rsa_ed_crosscert_new();
  816. if (NULL == *output)
  817. return -1;
  818. result = rsa_ed_crosscert_parse_into(*output, input, len_in);
  819. if (result < 0) {
  820. rsa_ed_crosscert_free(*output);
  821. *output = NULL;
  822. }
  823. return result;
  824. }
  825. auth1_t *
  826. auth1_new(void)
  827. {
  828. auth1_t *val = trunnel_calloc(1, sizeof(auth1_t));
  829. if (NULL == val)
  830. return NULL;
  831. return val;
  832. }
  833. /** Release all storage held inside 'obj', but do not free 'obj'.
  834. */
  835. static void
  836. auth1_clear(auth1_t *obj)
  837. {
  838. (void) obj;
  839. TRUNNEL_DYNARRAY_WIPE(&obj->sig);
  840. TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
  841. }
  842. void
  843. auth1_free(auth1_t *obj)
  844. {
  845. if (obj == NULL)
  846. return;
  847. auth1_clear(obj);
  848. trunnel_memwipe(obj, sizeof(auth1_t));
  849. trunnel_free_(obj);
  850. }
  851. size_t
  852. auth1_getlen_type(const auth1_t *inp)
  853. {
  854. (void)inp; return 8;
  855. }
  856. uint8_t
  857. auth1_get_type(const auth1_t *inp, size_t idx)
  858. {
  859. trunnel_assert(idx < 8);
  860. return inp->type[idx];
  861. }
  862. int
  863. auth1_set_type(auth1_t *inp, size_t idx, uint8_t elt)
  864. {
  865. trunnel_assert(idx < 8);
  866. inp->type[idx] = elt;
  867. return 0;
  868. }
  869. uint8_t *
  870. auth1_getarray_type(auth1_t *inp)
  871. {
  872. return inp->type;
  873. }
  874. size_t
  875. auth1_getlen_cid(const auth1_t *inp)
  876. {
  877. (void)inp; return 32;
  878. }
  879. uint8_t
  880. auth1_get_cid(const auth1_t *inp, size_t idx)
  881. {
  882. trunnel_assert(idx < 32);
  883. return inp->cid[idx];
  884. }
  885. int
  886. auth1_set_cid(auth1_t *inp, size_t idx, uint8_t elt)
  887. {
  888. trunnel_assert(idx < 32);
  889. inp->cid[idx] = elt;
  890. return 0;
  891. }
  892. uint8_t *
  893. auth1_getarray_cid(auth1_t *inp)
  894. {
  895. return inp->cid;
  896. }
  897. size_t
  898. auth1_getlen_sid(const auth1_t *inp)
  899. {
  900. (void)inp; return 32;
  901. }
  902. uint8_t
  903. auth1_get_sid(const auth1_t *inp, size_t idx)
  904. {
  905. trunnel_assert(idx < 32);
  906. return inp->sid[idx];
  907. }
  908. int
  909. auth1_set_sid(auth1_t *inp, size_t idx, uint8_t elt)
  910. {
  911. trunnel_assert(idx < 32);
  912. inp->sid[idx] = elt;
  913. return 0;
  914. }
  915. uint8_t *
  916. auth1_getarray_sid(auth1_t *inp)
  917. {
  918. return inp->sid;
  919. }
  920. size_t
  921. auth1_getlen_u1_cid_ed(const auth1_t *inp)
  922. {
  923. (void)inp; return 32;
  924. }
  925. uint8_t
  926. auth1_get_u1_cid_ed(const auth1_t *inp, size_t idx)
  927. {
  928. trunnel_assert(idx < 32);
  929. return inp->u1_cid_ed[idx];
  930. }
  931. int
  932. auth1_set_u1_cid_ed(auth1_t *inp, size_t idx, uint8_t elt)
  933. {
  934. trunnel_assert(idx < 32);
  935. inp->u1_cid_ed[idx] = elt;
  936. return 0;
  937. }
  938. uint8_t *
  939. auth1_getarray_u1_cid_ed(auth1_t *inp)
  940. {
  941. return inp->u1_cid_ed;
  942. }
  943. size_t
  944. auth1_getlen_u1_sid_ed(const auth1_t *inp)
  945. {
  946. (void)inp; return 32;
  947. }
  948. uint8_t
  949. auth1_get_u1_sid_ed(const auth1_t *inp, size_t idx)
  950. {
  951. trunnel_assert(idx < 32);
  952. return inp->u1_sid_ed[idx];
  953. }
  954. int
  955. auth1_set_u1_sid_ed(auth1_t *inp, size_t idx, uint8_t elt)
  956. {
  957. trunnel_assert(idx < 32);
  958. inp->u1_sid_ed[idx] = elt;
  959. return 0;
  960. }
  961. uint8_t *
  962. auth1_getarray_u1_sid_ed(auth1_t *inp)
  963. {
  964. return inp->u1_sid_ed;
  965. }
  966. size_t
  967. auth1_getlen_slog(const auth1_t *inp)
  968. {
  969. (void)inp; return 32;
  970. }
  971. uint8_t
  972. auth1_get_slog(const auth1_t *inp, size_t idx)
  973. {
  974. trunnel_assert(idx < 32);
  975. return inp->slog[idx];
  976. }
  977. int
  978. auth1_set_slog(auth1_t *inp, size_t idx, uint8_t elt)
  979. {
  980. trunnel_assert(idx < 32);
  981. inp->slog[idx] = elt;
  982. return 0;
  983. }
  984. uint8_t *
  985. auth1_getarray_slog(auth1_t *inp)
  986. {
  987. return inp->slog;
  988. }
  989. size_t
  990. auth1_getlen_clog(const auth1_t *inp)
  991. {
  992. (void)inp; return 32;
  993. }
  994. uint8_t
  995. auth1_get_clog(const auth1_t *inp, size_t idx)
  996. {
  997. trunnel_assert(idx < 32);
  998. return inp->clog[idx];
  999. }
  1000. int
  1001. auth1_set_clog(auth1_t *inp, size_t idx, uint8_t elt)
  1002. {
  1003. trunnel_assert(idx < 32);
  1004. inp->clog[idx] = elt;
  1005. return 0;
  1006. }
  1007. uint8_t *
  1008. auth1_getarray_clog(auth1_t *inp)
  1009. {
  1010. return inp->clog;
  1011. }
  1012. size_t
  1013. auth1_getlen_scert(const auth1_t *inp)
  1014. {
  1015. (void)inp; return 32;
  1016. }
  1017. uint8_t
  1018. auth1_get_scert(const auth1_t *inp, size_t idx)
  1019. {
  1020. trunnel_assert(idx < 32);
  1021. return inp->scert[idx];
  1022. }
  1023. int
  1024. auth1_set_scert(auth1_t *inp, size_t idx, uint8_t elt)
  1025. {
  1026. trunnel_assert(idx < 32);
  1027. inp->scert[idx] = elt;
  1028. return 0;
  1029. }
  1030. uint8_t *
  1031. auth1_getarray_scert(auth1_t *inp)
  1032. {
  1033. return inp->scert;
  1034. }
  1035. size_t
  1036. auth1_getlen_tlssecrets(const auth1_t *inp)
  1037. {
  1038. (void)inp; return 32;
  1039. }
  1040. uint8_t
  1041. auth1_get_tlssecrets(const auth1_t *inp, size_t idx)
  1042. {
  1043. trunnel_assert(idx < 32);
  1044. return inp->tlssecrets[idx];
  1045. }
  1046. int
  1047. auth1_set_tlssecrets(auth1_t *inp, size_t idx, uint8_t elt)
  1048. {
  1049. trunnel_assert(idx < 32);
  1050. inp->tlssecrets[idx] = elt;
  1051. return 0;
  1052. }
  1053. uint8_t *
  1054. auth1_getarray_tlssecrets(auth1_t *inp)
  1055. {
  1056. return inp->tlssecrets;
  1057. }
  1058. const uint8_t *
  1059. auth1_get_end_of_fixed_part(const auth1_t *inp)
  1060. {
  1061. return inp->end_of_fixed_part;
  1062. }
  1063. size_t
  1064. auth1_getlen_rand(const auth1_t *inp)
  1065. {
  1066. (void)inp; return 24;
  1067. }
  1068. uint8_t
  1069. auth1_get_rand(const auth1_t *inp, size_t idx)
  1070. {
  1071. trunnel_assert(idx < 24);
  1072. return inp->rand[idx];
  1073. }
  1074. int
  1075. auth1_set_rand(auth1_t *inp, size_t idx, uint8_t elt)
  1076. {
  1077. trunnel_assert(idx < 24);
  1078. inp->rand[idx] = elt;
  1079. return 0;
  1080. }
  1081. uint8_t *
  1082. auth1_getarray_rand(auth1_t *inp)
  1083. {
  1084. return inp->rand;
  1085. }
  1086. const uint8_t *
  1087. auth1_get_end_of_signed(const auth1_t *inp)
  1088. {
  1089. return inp->end_of_signed;
  1090. }
  1091. size_t
  1092. auth1_getlen_sig(const auth1_t *inp)
  1093. {
  1094. return TRUNNEL_DYNARRAY_LEN(&inp->sig);
  1095. }
  1096. uint8_t
  1097. auth1_get_sig(auth1_t *inp, size_t idx)
  1098. {
  1099. return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
  1100. }
  1101. int
  1102. auth1_set_sig(auth1_t *inp, size_t idx, uint8_t elt)
  1103. {
  1104. TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
  1105. return 0;
  1106. }
  1107. int
  1108. auth1_add_sig(auth1_t *inp, uint8_t elt)
  1109. {
  1110. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
  1111. return 0;
  1112. trunnel_alloc_failed:
  1113. TRUNNEL_SET_ERROR_CODE(inp);
  1114. return -1;
  1115. }
  1116. uint8_t *
  1117. auth1_getarray_sig(auth1_t *inp)
  1118. {
  1119. return inp->sig.elts_;
  1120. }
  1121. int
  1122. auth1_setlen_sig(auth1_t *inp, size_t newlen)
  1123. {
  1124. uint8_t *newptr;
  1125. newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
  1126. &inp->sig.n_, inp->sig.elts_, newlen,
  1127. sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
  1128. &inp->trunnel_error_code_);
  1129. if (newptr == NULL)
  1130. goto trunnel_alloc_failed;
  1131. inp->sig.elts_ = newptr;
  1132. return 0;
  1133. trunnel_alloc_failed:
  1134. TRUNNEL_SET_ERROR_CODE(inp);
  1135. return -1;
  1136. }
  1137. const char *
  1138. auth1_check(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1139. {
  1140. if (obj == NULL)
  1141. return "Object was NULL";
  1142. if (obj->trunnel_error_code_)
  1143. return "A set function failed on this object";
  1144. if (auth_ctx_ctx == NULL)
  1145. return "Context was NULL";
  1146. switch (auth_ctx_ctx->is_ed) {
  1147. case 0:
  1148. break;
  1149. case 1:
  1150. break;
  1151. default:
  1152. return "Bad tag for union";
  1153. break;
  1154. }
  1155. return NULL;
  1156. }
  1157. ssize_t
  1158. auth1_encoded_len(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1159. {
  1160. ssize_t result = 0;
  1161. if (NULL != auth1_check(obj, auth_ctx_ctx))
  1162. return -1;
  1163. /* Length of u8 type[8] */
  1164. result += 8;
  1165. /* Length of u8 cid[32] */
  1166. result += 32;
  1167. /* Length of u8 sid[32] */
  1168. result += 32;
  1169. switch (auth_ctx_ctx->is_ed) {
  1170. case 0:
  1171. break;
  1172. case 1:
  1173. /* Length of u8 u1_cid_ed[32] */
  1174. result += 32;
  1175. /* Length of u8 u1_sid_ed[32] */
  1176. result += 32;
  1177. break;
  1178. default:
  1179. trunnel_assert(0);
  1180. break;
  1181. }
  1182. /* Length of u8 slog[32] */
  1183. result += 32;
  1184. /* Length of u8 clog[32] */
  1185. result += 32;
  1186. /* Length of u8 scert[32] */
  1187. result += 32;
  1188. /* Length of u8 tlssecrets[32] */
  1189. result += 32;
  1190. /* Length of u8 rand[24] */
  1191. result += 24;
  1192. /* Length of u8 sig[] */
  1193. result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
  1194. return result;
  1195. }
  1196. int
  1197. auth1_clear_errors(auth1_t *obj)
  1198. {
  1199. int r = obj->trunnel_error_code_;
  1200. obj->trunnel_error_code_ = 0;
  1201. return r;
  1202. }
  1203. ssize_t
  1204. auth1_encode(uint8_t *output, const size_t avail, const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
  1205. {
  1206. ssize_t result = 0;
  1207. size_t written = 0;
  1208. uint8_t *ptr = output;
  1209. const char *msg;
  1210. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1211. const ssize_t encoded_len = auth1_encoded_len(obj, auth_ctx_ctx);
  1212. #endif
  1213. if (NULL != (msg = auth1_check(obj, auth_ctx_ctx)))
  1214. goto check_failed;
  1215. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1216. trunnel_assert(encoded_len >= 0);
  1217. #endif
  1218. /* Encode u8 type[8] */
  1219. trunnel_assert(written <= avail);
  1220. if (avail - written < 8)
  1221. goto truncated;
  1222. memcpy(ptr, obj->type, 8);
  1223. written += 8; ptr += 8;
  1224. /* Encode u8 cid[32] */
  1225. trunnel_assert(written <= avail);
  1226. if (avail - written < 32)
  1227. goto truncated;
  1228. memcpy(ptr, obj->cid, 32);
  1229. written += 32; ptr += 32;
  1230. /* Encode u8 sid[32] */
  1231. trunnel_assert(written <= avail);
  1232. if (avail - written < 32)
  1233. goto truncated;
  1234. memcpy(ptr, obj->sid, 32);
  1235. written += 32; ptr += 32;
  1236. /* Encode union u1[auth_ctx.is_ed] */
  1237. trunnel_assert(written <= avail);
  1238. switch (auth_ctx_ctx->is_ed) {
  1239. case 0:
  1240. break;
  1241. case 1:
  1242. /* Encode u8 u1_cid_ed[32] */
  1243. trunnel_assert(written <= avail);
  1244. if (avail - written < 32)
  1245. goto truncated;
  1246. memcpy(ptr, obj->u1_cid_ed, 32);
  1247. written += 32; ptr += 32;
  1248. /* Encode u8 u1_sid_ed[32] */
  1249. trunnel_assert(written <= avail);
  1250. if (avail - written < 32)
  1251. goto truncated;
  1252. memcpy(ptr, obj->u1_sid_ed, 32);
  1253. written += 32; ptr += 32;
  1254. break;
  1255. default:
  1256. trunnel_assert(0);
  1257. break;
  1258. }
  1259. /* Encode u8 slog[32] */
  1260. trunnel_assert(written <= avail);
  1261. if (avail - written < 32)
  1262. goto truncated;
  1263. memcpy(ptr, obj->slog, 32);
  1264. written += 32; ptr += 32;
  1265. /* Encode u8 clog[32] */
  1266. trunnel_assert(written <= avail);
  1267. if (avail - written < 32)
  1268. goto truncated;
  1269. memcpy(ptr, obj->clog, 32);
  1270. written += 32; ptr += 32;
  1271. /* Encode u8 scert[32] */
  1272. trunnel_assert(written <= avail);
  1273. if (avail - written < 32)
  1274. goto truncated;
  1275. memcpy(ptr, obj->scert, 32);
  1276. written += 32; ptr += 32;
  1277. /* Encode u8 tlssecrets[32] */
  1278. trunnel_assert(written <= avail);
  1279. if (avail - written < 32)
  1280. goto truncated;
  1281. memcpy(ptr, obj->tlssecrets, 32);
  1282. written += 32; ptr += 32;
  1283. /* Encode u8 rand[24] */
  1284. trunnel_assert(written <= avail);
  1285. if (avail - written < 24)
  1286. goto truncated;
  1287. memcpy(ptr, obj->rand, 24);
  1288. written += 24; ptr += 24;
  1289. /* Encode u8 sig[] */
  1290. {
  1291. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
  1292. trunnel_assert(written <= avail);
  1293. if (avail - written < elt_len)
  1294. goto truncated;
  1295. memcpy(ptr, obj->sig.elts_, elt_len);
  1296. written += elt_len; ptr += elt_len;
  1297. }
  1298. trunnel_assert(ptr == output + written);
  1299. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1300. {
  1301. trunnel_assert(encoded_len >= 0);
  1302. trunnel_assert((size_t)encoded_len == written);
  1303. }
  1304. #endif
  1305. return written;
  1306. truncated:
  1307. result = -2;
  1308. goto fail;
  1309. check_failed:
  1310. (void)msg;
  1311. result = -1;
  1312. goto fail;
  1313. fail:
  1314. trunnel_assert(result < 0);
  1315. return result;
  1316. }
  1317. /** As auth1_parse(), but do not allocate the output object.
  1318. */
  1319. static ssize_t
  1320. auth1_parse_into(auth1_t *obj, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
  1321. {
  1322. const uint8_t *ptr = input;
  1323. size_t remaining = len_in;
  1324. ssize_t result = 0;
  1325. (void)result;
  1326. if (auth_ctx_ctx == NULL)
  1327. return -1;
  1328. /* Parse u8 type[8] */
  1329. CHECK_REMAINING(8, truncated);
  1330. memcpy(obj->type, ptr, 8);
  1331. remaining -= 8; ptr += 8;
  1332. /* Parse u8 cid[32] */
  1333. CHECK_REMAINING(32, truncated);
  1334. memcpy(obj->cid, ptr, 32);
  1335. remaining -= 32; ptr += 32;
  1336. /* Parse u8 sid[32] */
  1337. CHECK_REMAINING(32, truncated);
  1338. memcpy(obj->sid, ptr, 32);
  1339. remaining -= 32; ptr += 32;
  1340. /* Parse union u1[auth_ctx.is_ed] */
  1341. switch (auth_ctx_ctx->is_ed) {
  1342. case 0:
  1343. break;
  1344. case 1:
  1345. /* Parse u8 u1_cid_ed[32] */
  1346. CHECK_REMAINING(32, truncated);
  1347. memcpy(obj->u1_cid_ed, ptr, 32);
  1348. remaining -= 32; ptr += 32;
  1349. /* Parse u8 u1_sid_ed[32] */
  1350. CHECK_REMAINING(32, truncated);
  1351. memcpy(obj->u1_sid_ed, ptr, 32);
  1352. remaining -= 32; ptr += 32;
  1353. break;
  1354. default:
  1355. goto fail;
  1356. break;
  1357. }
  1358. /* Parse u8 slog[32] */
  1359. CHECK_REMAINING(32, truncated);
  1360. memcpy(obj->slog, ptr, 32);
  1361. remaining -= 32; ptr += 32;
  1362. /* Parse u8 clog[32] */
  1363. CHECK_REMAINING(32, truncated);
  1364. memcpy(obj->clog, ptr, 32);
  1365. remaining -= 32; ptr += 32;
  1366. /* Parse u8 scert[32] */
  1367. CHECK_REMAINING(32, truncated);
  1368. memcpy(obj->scert, ptr, 32);
  1369. remaining -= 32; ptr += 32;
  1370. /* Parse u8 tlssecrets[32] */
  1371. CHECK_REMAINING(32, truncated);
  1372. memcpy(obj->tlssecrets, ptr, 32);
  1373. remaining -= 32; ptr += 32;
  1374. obj->end_of_fixed_part = ptr;
  1375. /* Parse u8 rand[24] */
  1376. CHECK_REMAINING(24, truncated);
  1377. memcpy(obj->rand, ptr, 24);
  1378. remaining -= 24; ptr += 24;
  1379. obj->end_of_signed = ptr;
  1380. /* Parse u8 sig[] */
  1381. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, remaining, {});
  1382. obj->sig.n_ = remaining;
  1383. memcpy(obj->sig.elts_, ptr, remaining);
  1384. ptr += remaining; remaining -= remaining;
  1385. trunnel_assert(ptr + remaining == input + len_in);
  1386. return len_in - remaining;
  1387. truncated:
  1388. return -2;
  1389. trunnel_alloc_failed:
  1390. return -1;
  1391. fail:
  1392. result = -1;
  1393. return result;
  1394. }
  1395. ssize_t
  1396. auth1_parse(auth1_t **output, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
  1397. {
  1398. ssize_t result;
  1399. *output = auth1_new();
  1400. if (NULL == *output)
  1401. return -1;
  1402. result = auth1_parse_into(*output, input, len_in, auth_ctx_ctx);
  1403. if (result < 0) {
  1404. auth1_free(*output);
  1405. *output = NULL;
  1406. }
  1407. return result;
  1408. }
  1409. certs_cell_t *
  1410. certs_cell_new(void)
  1411. {
  1412. certs_cell_t *val = trunnel_calloc(1, sizeof(certs_cell_t));
  1413. if (NULL == val)
  1414. return NULL;
  1415. return val;
  1416. }
  1417. /** Release all storage held inside 'obj', but do not free 'obj'.
  1418. */
  1419. static void
  1420. certs_cell_clear(certs_cell_t *obj)
  1421. {
  1422. (void) obj;
  1423. {
  1424. unsigned idx;
  1425. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1426. certs_cell_cert_free(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1427. }
  1428. }
  1429. TRUNNEL_DYNARRAY_WIPE(&obj->certs);
  1430. TRUNNEL_DYNARRAY_CLEAR(&obj->certs);
  1431. }
  1432. void
  1433. certs_cell_free(certs_cell_t *obj)
  1434. {
  1435. if (obj == NULL)
  1436. return;
  1437. certs_cell_clear(obj);
  1438. trunnel_memwipe(obj, sizeof(certs_cell_t));
  1439. trunnel_free_(obj);
  1440. }
  1441. uint8_t
  1442. certs_cell_get_n_certs(certs_cell_t *inp)
  1443. {
  1444. return inp->n_certs;
  1445. }
  1446. int
  1447. certs_cell_set_n_certs(certs_cell_t *inp, uint8_t val)
  1448. {
  1449. inp->n_certs = val;
  1450. return 0;
  1451. }
  1452. size_t
  1453. certs_cell_getlen_certs(const certs_cell_t *inp)
  1454. {
  1455. return TRUNNEL_DYNARRAY_LEN(&inp->certs);
  1456. }
  1457. struct certs_cell_cert_st *
  1458. certs_cell_get_certs(certs_cell_t *inp, size_t idx)
  1459. {
  1460. return TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
  1461. }
  1462. int
  1463. certs_cell_set_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
  1464. {
  1465. certs_cell_cert_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
  1466. if (oldval && oldval != elt)
  1467. certs_cell_cert_free(oldval);
  1468. return certs_cell_set0_certs(inp, idx, elt);
  1469. }
  1470. int
  1471. certs_cell_set0_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
  1472. {
  1473. TRUNNEL_DYNARRAY_SET(&inp->certs, idx, elt);
  1474. return 0;
  1475. }
  1476. int
  1477. certs_cell_add_certs(certs_cell_t *inp, struct certs_cell_cert_st * elt)
  1478. {
  1479. #if SIZE_MAX >= UINT8_MAX
  1480. if (inp->certs.n_ == UINT8_MAX)
  1481. goto trunnel_alloc_failed;
  1482. #endif
  1483. TRUNNEL_DYNARRAY_ADD(struct certs_cell_cert_st *, &inp->certs, elt, {});
  1484. return 0;
  1485. trunnel_alloc_failed:
  1486. TRUNNEL_SET_ERROR_CODE(inp);
  1487. return -1;
  1488. }
  1489. struct certs_cell_cert_st * *
  1490. certs_cell_getarray_certs(certs_cell_t *inp)
  1491. {
  1492. return inp->certs.elts_;
  1493. }
  1494. int
  1495. certs_cell_setlen_certs(certs_cell_t *inp, size_t newlen)
  1496. {
  1497. struct certs_cell_cert_st * *newptr;
  1498. #if UINT8_MAX < SIZE_MAX
  1499. if (newlen > UINT8_MAX)
  1500. goto trunnel_alloc_failed;
  1501. #endif
  1502. newptr = trunnel_dynarray_setlen(&inp->certs.allocated_,
  1503. &inp->certs.n_, inp->certs.elts_, newlen,
  1504. sizeof(inp->certs.elts_[0]), (trunnel_free_fn_t) certs_cell_cert_free,
  1505. &inp->trunnel_error_code_);
  1506. if (newptr == NULL)
  1507. goto trunnel_alloc_failed;
  1508. inp->certs.elts_ = newptr;
  1509. return 0;
  1510. trunnel_alloc_failed:
  1511. TRUNNEL_SET_ERROR_CODE(inp);
  1512. return -1;
  1513. }
  1514. const char *
  1515. certs_cell_check(const certs_cell_t *obj)
  1516. {
  1517. if (obj == NULL)
  1518. return "Object was NULL";
  1519. if (obj->trunnel_error_code_)
  1520. return "A set function failed on this object";
  1521. {
  1522. const char *msg;
  1523. unsigned idx;
  1524. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1525. if (NULL != (msg = certs_cell_cert_check(TRUNNEL_DYNARRAY_GET(&obj->certs, idx))))
  1526. return msg;
  1527. }
  1528. }
  1529. if (TRUNNEL_DYNARRAY_LEN(&obj->certs) != obj->n_certs)
  1530. return "Length mismatch for certs";
  1531. return NULL;
  1532. }
  1533. ssize_t
  1534. certs_cell_encoded_len(const certs_cell_t *obj)
  1535. {
  1536. ssize_t result = 0;
  1537. if (NULL != certs_cell_check(obj))
  1538. return -1;
  1539. /* Length of u8 n_certs */
  1540. result += 1;
  1541. /* Length of struct certs_cell_cert certs[n_certs] */
  1542. {
  1543. unsigned idx;
  1544. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1545. result += certs_cell_cert_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1546. }
  1547. }
  1548. return result;
  1549. }
  1550. int
  1551. certs_cell_clear_errors(certs_cell_t *obj)
  1552. {
  1553. int r = obj->trunnel_error_code_;
  1554. obj->trunnel_error_code_ = 0;
  1555. return r;
  1556. }
  1557. ssize_t
  1558. certs_cell_encode(uint8_t *output, const size_t avail, const certs_cell_t *obj)
  1559. {
  1560. ssize_t result = 0;
  1561. size_t written = 0;
  1562. uint8_t *ptr = output;
  1563. const char *msg;
  1564. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1565. const ssize_t encoded_len = certs_cell_encoded_len(obj);
  1566. #endif
  1567. if (NULL != (msg = certs_cell_check(obj)))
  1568. goto check_failed;
  1569. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1570. trunnel_assert(encoded_len >= 0);
  1571. #endif
  1572. /* Encode u8 n_certs */
  1573. trunnel_assert(written <= avail);
  1574. if (avail - written < 1)
  1575. goto truncated;
  1576. trunnel_set_uint8(ptr, (obj->n_certs));
  1577. written += 1; ptr += 1;
  1578. /* Encode struct certs_cell_cert certs[n_certs] */
  1579. {
  1580. unsigned idx;
  1581. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
  1582. trunnel_assert(written <= avail);
  1583. result = certs_cell_cert_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
  1584. if (result < 0)
  1585. goto fail; /* XXXXXXX !*/
  1586. written += result; ptr += result;
  1587. }
  1588. }
  1589. trunnel_assert(ptr == output + written);
  1590. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1591. {
  1592. trunnel_assert(encoded_len >= 0);
  1593. trunnel_assert((size_t)encoded_len == written);
  1594. }
  1595. #endif
  1596. return written;
  1597. truncated:
  1598. result = -2;
  1599. goto fail;
  1600. check_failed:
  1601. (void)msg;
  1602. result = -1;
  1603. goto fail;
  1604. fail:
  1605. trunnel_assert(result < 0);
  1606. return result;
  1607. }
  1608. /** As certs_cell_parse(), but do not allocate the output object.
  1609. */
  1610. static ssize_t
  1611. certs_cell_parse_into(certs_cell_t *obj, const uint8_t *input, const size_t len_in)
  1612. {
  1613. const uint8_t *ptr = input;
  1614. size_t remaining = len_in;
  1615. ssize_t result = 0;
  1616. (void)result;
  1617. /* Parse u8 n_certs */
  1618. CHECK_REMAINING(1, truncated);
  1619. obj->n_certs = (trunnel_get_uint8(ptr));
  1620. remaining -= 1; ptr += 1;
  1621. /* Parse struct certs_cell_cert certs[n_certs] */
  1622. TRUNNEL_DYNARRAY_EXPAND(certs_cell_cert_t *, &obj->certs, obj->n_certs, {});
  1623. {
  1624. certs_cell_cert_t * elt;
  1625. unsigned idx;
  1626. for (idx = 0; idx < obj->n_certs; ++idx) {
  1627. result = certs_cell_cert_parse(&elt, ptr, remaining);
  1628. if (result < 0)
  1629. goto relay_fail;
  1630. trunnel_assert((size_t)result <= remaining);
  1631. remaining -= result; ptr += result;
  1632. TRUNNEL_DYNARRAY_ADD(certs_cell_cert_t *, &obj->certs, elt, {certs_cell_cert_free(elt);});
  1633. }
  1634. }
  1635. trunnel_assert(ptr + remaining == input + len_in);
  1636. return len_in - remaining;
  1637. truncated:
  1638. return -2;
  1639. relay_fail:
  1640. if (result >= 0) result = -1;
  1641. return result;
  1642. trunnel_alloc_failed:
  1643. return -1;
  1644. }
  1645. ssize_t
  1646. certs_cell_parse(certs_cell_t **output, const uint8_t *input, const size_t len_in)
  1647. {
  1648. ssize_t result;
  1649. *output = certs_cell_new();
  1650. if (NULL == *output)
  1651. return -1;
  1652. result = certs_cell_parse_into(*output, input, len_in);
  1653. if (result < 0) {
  1654. certs_cell_free(*output);
  1655. *output = NULL;
  1656. }
  1657. return result;
  1658. }