ed25519_cert.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  1. /* ed25519_cert.c -- generated by Trunnel v1.5.2.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "ed25519_cert.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're running a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int edcert_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || edcert_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. create2_cell_body_t *
  27. create2_cell_body_new(void)
  28. {
  29. create2_cell_body_t *val = trunnel_calloc(1, sizeof(create2_cell_body_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. create2_cell_body_clear(create2_cell_body_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->handshake_data);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->handshake_data);
  42. }
  43. void
  44. create2_cell_body_free(create2_cell_body_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. create2_cell_body_clear(obj);
  49. trunnel_memwipe(obj, sizeof(create2_cell_body_t));
  50. trunnel_free_(obj);
  51. }
  52. uint16_t
  53. create2_cell_body_get_handshake_type(const create2_cell_body_t *inp)
  54. {
  55. return inp->handshake_type;
  56. }
  57. int
  58. create2_cell_body_set_handshake_type(create2_cell_body_t *inp, uint16_t val)
  59. {
  60. inp->handshake_type = val;
  61. return 0;
  62. }
  63. uint16_t
  64. create2_cell_body_get_handshake_len(const create2_cell_body_t *inp)
  65. {
  66. return inp->handshake_len;
  67. }
  68. int
  69. create2_cell_body_set_handshake_len(create2_cell_body_t *inp, uint16_t val)
  70. {
  71. inp->handshake_len = val;
  72. return 0;
  73. }
  74. size_t
  75. create2_cell_body_getlen_handshake_data(const create2_cell_body_t *inp)
  76. {
  77. return TRUNNEL_DYNARRAY_LEN(&inp->handshake_data);
  78. }
  79. uint8_t
  80. create2_cell_body_get_handshake_data(create2_cell_body_t *inp, size_t idx)
  81. {
  82. return TRUNNEL_DYNARRAY_GET(&inp->handshake_data, idx);
  83. }
  84. uint8_t
  85. create2_cell_body_getconst_handshake_data(const create2_cell_body_t *inp, size_t idx)
  86. {
  87. return create2_cell_body_get_handshake_data((create2_cell_body_t*)inp, idx);
  88. }
  89. int
  90. create2_cell_body_set_handshake_data(create2_cell_body_t *inp, size_t idx, uint8_t elt)
  91. {
  92. TRUNNEL_DYNARRAY_SET(&inp->handshake_data, idx, elt);
  93. return 0;
  94. }
  95. int
  96. create2_cell_body_add_handshake_data(create2_cell_body_t *inp, uint8_t elt)
  97. {
  98. #if SIZE_MAX >= UINT16_MAX
  99. if (inp->handshake_data.n_ == UINT16_MAX)
  100. goto trunnel_alloc_failed;
  101. #endif
  102. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->handshake_data, elt, {});
  103. return 0;
  104. trunnel_alloc_failed:
  105. TRUNNEL_SET_ERROR_CODE(inp);
  106. return -1;
  107. }
  108. uint8_t *
  109. create2_cell_body_getarray_handshake_data(create2_cell_body_t *inp)
  110. {
  111. return inp->handshake_data.elts_;
  112. }
  113. const uint8_t *
  114. create2_cell_body_getconstarray_handshake_data(const create2_cell_body_t *inp)
  115. {
  116. return (const uint8_t *)create2_cell_body_getarray_handshake_data((create2_cell_body_t*)inp);
  117. }
  118. int
  119. create2_cell_body_setlen_handshake_data(create2_cell_body_t *inp, size_t newlen)
  120. {
  121. uint8_t *newptr;
  122. #if UINT16_MAX < SIZE_MAX
  123. if (newlen > UINT16_MAX)
  124. goto trunnel_alloc_failed;
  125. #endif
  126. newptr = trunnel_dynarray_setlen(&inp->handshake_data.allocated_,
  127. &inp->handshake_data.n_, inp->handshake_data.elts_, newlen,
  128. sizeof(inp->handshake_data.elts_[0]), (trunnel_free_fn_t) NULL,
  129. &inp->trunnel_error_code_);
  130. if (newlen != 0 && newptr == NULL)
  131. goto trunnel_alloc_failed;
  132. inp->handshake_data.elts_ = newptr;
  133. return 0;
  134. trunnel_alloc_failed:
  135. TRUNNEL_SET_ERROR_CODE(inp);
  136. return -1;
  137. }
  138. const char *
  139. create2_cell_body_check(const create2_cell_body_t *obj)
  140. {
  141. if (obj == NULL)
  142. return "Object was NULL";
  143. if (obj->trunnel_error_code_)
  144. return "A set function failed on this object";
  145. if (TRUNNEL_DYNARRAY_LEN(&obj->handshake_data) != obj->handshake_len)
  146. return "Length mismatch for handshake_data";
  147. return NULL;
  148. }
  149. ssize_t
  150. create2_cell_body_encoded_len(const create2_cell_body_t *obj)
  151. {
  152. ssize_t result = 0;
  153. if (NULL != create2_cell_body_check(obj))
  154. return -1;
  155. /* Length of u16 handshake_type */
  156. result += 2;
  157. /* Length of u16 handshake_len */
  158. result += 2;
  159. /* Length of u8 handshake_data[handshake_len] */
  160. result += TRUNNEL_DYNARRAY_LEN(&obj->handshake_data);
  161. return result;
  162. }
  163. int
  164. create2_cell_body_clear_errors(create2_cell_body_t *obj)
  165. {
  166. int r = obj->trunnel_error_code_;
  167. obj->trunnel_error_code_ = 0;
  168. return r;
  169. }
  170. ssize_t
  171. create2_cell_body_encode(uint8_t *output, const size_t avail, const create2_cell_body_t *obj)
  172. {
  173. ssize_t result = 0;
  174. size_t written = 0;
  175. uint8_t *ptr = output;
  176. const char *msg;
  177. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  178. const ssize_t encoded_len = create2_cell_body_encoded_len(obj);
  179. #endif
  180. if (NULL != (msg = create2_cell_body_check(obj)))
  181. goto check_failed;
  182. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  183. trunnel_assert(encoded_len >= 0);
  184. #endif
  185. /* Encode u16 handshake_type */
  186. trunnel_assert(written <= avail);
  187. if (avail - written < 2)
  188. goto truncated;
  189. trunnel_set_uint16(ptr, trunnel_htons(obj->handshake_type));
  190. written += 2; ptr += 2;
  191. /* Encode u16 handshake_len */
  192. trunnel_assert(written <= avail);
  193. if (avail - written < 2)
  194. goto truncated;
  195. trunnel_set_uint16(ptr, trunnel_htons(obj->handshake_len));
  196. written += 2; ptr += 2;
  197. /* Encode u8 handshake_data[handshake_len] */
  198. {
  199. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->handshake_data);
  200. trunnel_assert(obj->handshake_len == elt_len);
  201. trunnel_assert(written <= avail);
  202. if (avail - written < elt_len)
  203. goto truncated;
  204. if (elt_len)
  205. memcpy(ptr, obj->handshake_data.elts_, elt_len);
  206. written += elt_len; ptr += elt_len;
  207. }
  208. trunnel_assert(ptr == output + written);
  209. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  210. {
  211. trunnel_assert(encoded_len >= 0);
  212. trunnel_assert((size_t)encoded_len == written);
  213. }
  214. #endif
  215. return written;
  216. truncated:
  217. result = -2;
  218. goto fail;
  219. check_failed:
  220. (void)msg;
  221. result = -1;
  222. goto fail;
  223. fail:
  224. trunnel_assert(result < 0);
  225. return result;
  226. }
  227. /** As create2_cell_body_parse(), but do not allocate the output
  228. * object.
  229. */
  230. static ssize_t
  231. create2_cell_body_parse_into(create2_cell_body_t *obj, const uint8_t *input, const size_t len_in)
  232. {
  233. const uint8_t *ptr = input;
  234. size_t remaining = len_in;
  235. ssize_t result = 0;
  236. (void)result;
  237. /* Parse u16 handshake_type */
  238. CHECK_REMAINING(2, truncated);
  239. obj->handshake_type = trunnel_ntohs(trunnel_get_uint16(ptr));
  240. remaining -= 2; ptr += 2;
  241. /* Parse u16 handshake_len */
  242. CHECK_REMAINING(2, truncated);
  243. obj->handshake_len = trunnel_ntohs(trunnel_get_uint16(ptr));
  244. remaining -= 2; ptr += 2;
  245. /* Parse u8 handshake_data[handshake_len] */
  246. CHECK_REMAINING(obj->handshake_len, truncated);
  247. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->handshake_data, obj->handshake_len, {});
  248. obj->handshake_data.n_ = obj->handshake_len;
  249. if (obj->handshake_len)
  250. memcpy(obj->handshake_data.elts_, ptr, obj->handshake_len);
  251. ptr += obj->handshake_len; remaining -= obj->handshake_len;
  252. trunnel_assert(ptr + remaining == input + len_in);
  253. return len_in - remaining;
  254. truncated:
  255. return -2;
  256. trunnel_alloc_failed:
  257. return -1;
  258. }
  259. ssize_t
  260. create2_cell_body_parse(create2_cell_body_t **output, const uint8_t *input, const size_t len_in)
  261. {
  262. ssize_t result;
  263. *output = create2_cell_body_new();
  264. if (NULL == *output)
  265. return -1;
  266. result = create2_cell_body_parse_into(*output, input, len_in);
  267. if (result < 0) {
  268. create2_cell_body_free(*output);
  269. *output = NULL;
  270. }
  271. return result;
  272. }
  273. ed25519_cert_extension_t *
  274. ed25519_cert_extension_new(void)
  275. {
  276. ed25519_cert_extension_t *val = trunnel_calloc(1, sizeof(ed25519_cert_extension_t));
  277. if (NULL == val)
  278. return NULL;
  279. return val;
  280. }
  281. /** Release all storage held inside 'obj', but do not free 'obj'.
  282. */
  283. static void
  284. ed25519_cert_extension_clear(ed25519_cert_extension_t *obj)
  285. {
  286. (void) obj;
  287. TRUNNEL_DYNARRAY_WIPE(&obj->un_unparsed);
  288. TRUNNEL_DYNARRAY_CLEAR(&obj->un_unparsed);
  289. }
  290. void
  291. ed25519_cert_extension_free(ed25519_cert_extension_t *obj)
  292. {
  293. if (obj == NULL)
  294. return;
  295. ed25519_cert_extension_clear(obj);
  296. trunnel_memwipe(obj, sizeof(ed25519_cert_extension_t));
  297. trunnel_free_(obj);
  298. }
  299. uint16_t
  300. ed25519_cert_extension_get_ext_length(const ed25519_cert_extension_t *inp)
  301. {
  302. return inp->ext_length;
  303. }
  304. int
  305. ed25519_cert_extension_set_ext_length(ed25519_cert_extension_t *inp, uint16_t val)
  306. {
  307. inp->ext_length = val;
  308. return 0;
  309. }
  310. uint8_t
  311. ed25519_cert_extension_get_ext_type(const ed25519_cert_extension_t *inp)
  312. {
  313. return inp->ext_type;
  314. }
  315. int
  316. ed25519_cert_extension_set_ext_type(ed25519_cert_extension_t *inp, uint8_t val)
  317. {
  318. inp->ext_type = val;
  319. return 0;
  320. }
  321. uint8_t
  322. ed25519_cert_extension_get_ext_flags(const ed25519_cert_extension_t *inp)
  323. {
  324. return inp->ext_flags;
  325. }
  326. int
  327. ed25519_cert_extension_set_ext_flags(ed25519_cert_extension_t *inp, uint8_t val)
  328. {
  329. inp->ext_flags = val;
  330. return 0;
  331. }
  332. size_t
  333. ed25519_cert_extension_getlen_un_signing_key(const ed25519_cert_extension_t *inp)
  334. {
  335. (void)inp; return 32;
  336. }
  337. uint8_t
  338. ed25519_cert_extension_get_un_signing_key(ed25519_cert_extension_t *inp, size_t idx)
  339. {
  340. trunnel_assert(idx < 32);
  341. return inp->un_signing_key[idx];
  342. }
  343. uint8_t
  344. ed25519_cert_extension_getconst_un_signing_key(const ed25519_cert_extension_t *inp, size_t idx)
  345. {
  346. return ed25519_cert_extension_get_un_signing_key((ed25519_cert_extension_t*)inp, idx);
  347. }
  348. int
  349. ed25519_cert_extension_set_un_signing_key(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  350. {
  351. trunnel_assert(idx < 32);
  352. inp->un_signing_key[idx] = elt;
  353. return 0;
  354. }
  355. uint8_t *
  356. ed25519_cert_extension_getarray_un_signing_key(ed25519_cert_extension_t *inp)
  357. {
  358. return inp->un_signing_key;
  359. }
  360. const uint8_t *
  361. ed25519_cert_extension_getconstarray_un_signing_key(const ed25519_cert_extension_t *inp)
  362. {
  363. return (const uint8_t *)ed25519_cert_extension_getarray_un_signing_key((ed25519_cert_extension_t*)inp);
  364. }
  365. size_t
  366. ed25519_cert_extension_getlen_un_unparsed(const ed25519_cert_extension_t *inp)
  367. {
  368. return TRUNNEL_DYNARRAY_LEN(&inp->un_unparsed);
  369. }
  370. uint8_t
  371. ed25519_cert_extension_get_un_unparsed(ed25519_cert_extension_t *inp, size_t idx)
  372. {
  373. return TRUNNEL_DYNARRAY_GET(&inp->un_unparsed, idx);
  374. }
  375. uint8_t
  376. ed25519_cert_extension_getconst_un_unparsed(const ed25519_cert_extension_t *inp, size_t idx)
  377. {
  378. return ed25519_cert_extension_get_un_unparsed((ed25519_cert_extension_t*)inp, idx);
  379. }
  380. int
  381. ed25519_cert_extension_set_un_unparsed(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  382. {
  383. TRUNNEL_DYNARRAY_SET(&inp->un_unparsed, idx, elt);
  384. return 0;
  385. }
  386. int
  387. ed25519_cert_extension_add_un_unparsed(ed25519_cert_extension_t *inp, uint8_t elt)
  388. {
  389. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->un_unparsed, elt, {});
  390. return 0;
  391. trunnel_alloc_failed:
  392. TRUNNEL_SET_ERROR_CODE(inp);
  393. return -1;
  394. }
  395. uint8_t *
  396. ed25519_cert_extension_getarray_un_unparsed(ed25519_cert_extension_t *inp)
  397. {
  398. return inp->un_unparsed.elts_;
  399. }
  400. const uint8_t *
  401. ed25519_cert_extension_getconstarray_un_unparsed(const ed25519_cert_extension_t *inp)
  402. {
  403. return (const uint8_t *)ed25519_cert_extension_getarray_un_unparsed((ed25519_cert_extension_t*)inp);
  404. }
  405. int
  406. ed25519_cert_extension_setlen_un_unparsed(ed25519_cert_extension_t *inp, size_t newlen)
  407. {
  408. uint8_t *newptr;
  409. newptr = trunnel_dynarray_setlen(&inp->un_unparsed.allocated_,
  410. &inp->un_unparsed.n_, inp->un_unparsed.elts_, newlen,
  411. sizeof(inp->un_unparsed.elts_[0]), (trunnel_free_fn_t) NULL,
  412. &inp->trunnel_error_code_);
  413. if (newlen != 0 && newptr == NULL)
  414. goto trunnel_alloc_failed;
  415. inp->un_unparsed.elts_ = newptr;
  416. return 0;
  417. trunnel_alloc_failed:
  418. TRUNNEL_SET_ERROR_CODE(inp);
  419. return -1;
  420. }
  421. const char *
  422. ed25519_cert_extension_check(const ed25519_cert_extension_t *obj)
  423. {
  424. if (obj == NULL)
  425. return "Object was NULL";
  426. if (obj->trunnel_error_code_)
  427. return "A set function failed on this object";
  428. switch (obj->ext_type) {
  429. case CERTEXT_SIGNED_WITH_KEY:
  430. break;
  431. default:
  432. break;
  433. }
  434. return NULL;
  435. }
  436. ssize_t
  437. ed25519_cert_extension_encoded_len(const ed25519_cert_extension_t *obj)
  438. {
  439. ssize_t result = 0;
  440. if (NULL != ed25519_cert_extension_check(obj))
  441. return -1;
  442. /* Length of u16 ext_length */
  443. result += 2;
  444. /* Length of u8 ext_type */
  445. result += 1;
  446. /* Length of u8 ext_flags */
  447. result += 1;
  448. switch (obj->ext_type) {
  449. case CERTEXT_SIGNED_WITH_KEY:
  450. /* Length of u8 un_signing_key[32] */
  451. result += 32;
  452. break;
  453. default:
  454. /* Length of u8 un_unparsed[] */
  455. result += TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  456. break;
  457. }
  458. return result;
  459. }
  460. int
  461. ed25519_cert_extension_clear_errors(ed25519_cert_extension_t *obj)
  462. {
  463. int r = obj->trunnel_error_code_;
  464. obj->trunnel_error_code_ = 0;
  465. return r;
  466. }
  467. ssize_t
  468. ed25519_cert_extension_encode(uint8_t *output, const size_t avail, const ed25519_cert_extension_t *obj)
  469. {
  470. ssize_t result = 0;
  471. size_t written = 0;
  472. uint8_t *ptr = output;
  473. const char *msg;
  474. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  475. const ssize_t encoded_len = ed25519_cert_extension_encoded_len(obj);
  476. #endif
  477. uint8_t *backptr_ext_length = NULL;
  478. if (NULL != (msg = ed25519_cert_extension_check(obj)))
  479. goto check_failed;
  480. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  481. trunnel_assert(encoded_len >= 0);
  482. #endif
  483. /* Encode u16 ext_length */
  484. backptr_ext_length = ptr;
  485. trunnel_assert(written <= avail);
  486. if (avail - written < 2)
  487. goto truncated;
  488. trunnel_set_uint16(ptr, trunnel_htons(obj->ext_length));
  489. written += 2; ptr += 2;
  490. /* Encode u8 ext_type */
  491. trunnel_assert(written <= avail);
  492. if (avail - written < 1)
  493. goto truncated;
  494. trunnel_set_uint8(ptr, (obj->ext_type));
  495. written += 1; ptr += 1;
  496. /* Encode u8 ext_flags */
  497. trunnel_assert(written <= avail);
  498. if (avail - written < 1)
  499. goto truncated;
  500. trunnel_set_uint8(ptr, (obj->ext_flags));
  501. written += 1; ptr += 1;
  502. {
  503. size_t written_before_union = written;
  504. /* Encode union un[ext_type] */
  505. trunnel_assert(written <= avail);
  506. switch (obj->ext_type) {
  507. case CERTEXT_SIGNED_WITH_KEY:
  508. /* Encode u8 un_signing_key[32] */
  509. trunnel_assert(written <= avail);
  510. if (avail - written < 32)
  511. goto truncated;
  512. memcpy(ptr, obj->un_signing_key, 32);
  513. written += 32; ptr += 32;
  514. break;
  515. default:
  516. /* Encode u8 un_unparsed[] */
  517. {
  518. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  519. trunnel_assert(written <= avail);
  520. if (avail - written < elt_len)
  521. goto truncated;
  522. if (elt_len)
  523. memcpy(ptr, obj->un_unparsed.elts_, elt_len);
  524. written += elt_len; ptr += elt_len;
  525. }
  526. break;
  527. }
  528. /* Write the length field back to ext_length */
  529. trunnel_assert(written >= written_before_union);
  530. #if UINT16_MAX < SIZE_MAX
  531. if (written - written_before_union > UINT16_MAX)
  532. goto check_failed;
  533. #endif
  534. trunnel_set_uint16(backptr_ext_length, trunnel_htons(written - written_before_union));
  535. }
  536. trunnel_assert(ptr == output + written);
  537. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  538. {
  539. trunnel_assert(encoded_len >= 0);
  540. trunnel_assert((size_t)encoded_len == written);
  541. }
  542. #endif
  543. return written;
  544. truncated:
  545. result = -2;
  546. goto fail;
  547. check_failed:
  548. (void)msg;
  549. result = -1;
  550. goto fail;
  551. fail:
  552. trunnel_assert(result < 0);
  553. return result;
  554. }
  555. /** As ed25519_cert_extension_parse(), but do not allocate the output
  556. * object.
  557. */
  558. static ssize_t
  559. ed25519_cert_extension_parse_into(ed25519_cert_extension_t *obj, const uint8_t *input, const size_t len_in)
  560. {
  561. const uint8_t *ptr = input;
  562. size_t remaining = len_in;
  563. ssize_t result = 0;
  564. (void)result;
  565. /* Parse u16 ext_length */
  566. CHECK_REMAINING(2, truncated);
  567. obj->ext_length = trunnel_ntohs(trunnel_get_uint16(ptr));
  568. remaining -= 2; ptr += 2;
  569. /* Parse u8 ext_type */
  570. CHECK_REMAINING(1, truncated);
  571. obj->ext_type = (trunnel_get_uint8(ptr));
  572. remaining -= 1; ptr += 1;
  573. /* Parse u8 ext_flags */
  574. CHECK_REMAINING(1, truncated);
  575. obj->ext_flags = (trunnel_get_uint8(ptr));
  576. remaining -= 1; ptr += 1;
  577. {
  578. size_t remaining_after;
  579. CHECK_REMAINING(obj->ext_length, truncated);
  580. remaining_after = remaining - obj->ext_length;
  581. remaining = obj->ext_length;
  582. /* Parse union un[ext_type] */
  583. switch (obj->ext_type) {
  584. case CERTEXT_SIGNED_WITH_KEY:
  585. /* Parse u8 un_signing_key[32] */
  586. CHECK_REMAINING(32, fail);
  587. memcpy(obj->un_signing_key, ptr, 32);
  588. remaining -= 32; ptr += 32;
  589. break;
  590. default:
  591. /* Parse u8 un_unparsed[] */
  592. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unparsed, remaining, {});
  593. obj->un_unparsed.n_ = remaining;
  594. if (remaining)
  595. memcpy(obj->un_unparsed.elts_, ptr, remaining);
  596. ptr += remaining; remaining -= remaining;
  597. break;
  598. }
  599. if (remaining != 0)
  600. goto fail;
  601. remaining = remaining_after;
  602. }
  603. trunnel_assert(ptr + remaining == input + len_in);
  604. return len_in - remaining;
  605. truncated:
  606. return -2;
  607. trunnel_alloc_failed:
  608. return -1;
  609. fail:
  610. result = -1;
  611. return result;
  612. }
  613. ssize_t
  614. ed25519_cert_extension_parse(ed25519_cert_extension_t **output, const uint8_t *input, const size_t len_in)
  615. {
  616. ssize_t result;
  617. *output = ed25519_cert_extension_new();
  618. if (NULL == *output)
  619. return -1;
  620. result = ed25519_cert_extension_parse_into(*output, input, len_in);
  621. if (result < 0) {
  622. ed25519_cert_extension_free(*output);
  623. *output = NULL;
  624. }
  625. return result;
  626. }
  627. extend1_cell_body_t *
  628. extend1_cell_body_new(void)
  629. {
  630. extend1_cell_body_t *val = trunnel_calloc(1, sizeof(extend1_cell_body_t));
  631. if (NULL == val)
  632. return NULL;
  633. return val;
  634. }
  635. /** Release all storage held inside 'obj', but do not free 'obj'.
  636. */
  637. static void
  638. extend1_cell_body_clear(extend1_cell_body_t *obj)
  639. {
  640. (void) obj;
  641. }
  642. void
  643. extend1_cell_body_free(extend1_cell_body_t *obj)
  644. {
  645. if (obj == NULL)
  646. return;
  647. extend1_cell_body_clear(obj);
  648. trunnel_memwipe(obj, sizeof(extend1_cell_body_t));
  649. trunnel_free_(obj);
  650. }
  651. uint32_t
  652. extend1_cell_body_get_ipv4addr(const extend1_cell_body_t *inp)
  653. {
  654. return inp->ipv4addr;
  655. }
  656. int
  657. extend1_cell_body_set_ipv4addr(extend1_cell_body_t *inp, uint32_t val)
  658. {
  659. inp->ipv4addr = val;
  660. return 0;
  661. }
  662. uint16_t
  663. extend1_cell_body_get_port(const extend1_cell_body_t *inp)
  664. {
  665. return inp->port;
  666. }
  667. int
  668. extend1_cell_body_set_port(extend1_cell_body_t *inp, uint16_t val)
  669. {
  670. inp->port = val;
  671. return 0;
  672. }
  673. size_t
  674. extend1_cell_body_getlen_onionskin(const extend1_cell_body_t *inp)
  675. {
  676. (void)inp; return 186;
  677. }
  678. uint8_t
  679. extend1_cell_body_get_onionskin(extend1_cell_body_t *inp, size_t idx)
  680. {
  681. trunnel_assert(idx < 186);
  682. return inp->onionskin[idx];
  683. }
  684. uint8_t
  685. extend1_cell_body_getconst_onionskin(const extend1_cell_body_t *inp, size_t idx)
  686. {
  687. return extend1_cell_body_get_onionskin((extend1_cell_body_t*)inp, idx);
  688. }
  689. int
  690. extend1_cell_body_set_onionskin(extend1_cell_body_t *inp, size_t idx, uint8_t elt)
  691. {
  692. trunnel_assert(idx < 186);
  693. inp->onionskin[idx] = elt;
  694. return 0;
  695. }
  696. uint8_t *
  697. extend1_cell_body_getarray_onionskin(extend1_cell_body_t *inp)
  698. {
  699. return inp->onionskin;
  700. }
  701. const uint8_t *
  702. extend1_cell_body_getconstarray_onionskin(const extend1_cell_body_t *inp)
  703. {
  704. return (const uint8_t *)extend1_cell_body_getarray_onionskin((extend1_cell_body_t*)inp);
  705. }
  706. size_t
  707. extend1_cell_body_getlen_identity(const extend1_cell_body_t *inp)
  708. {
  709. (void)inp; return 20;
  710. }
  711. uint8_t
  712. extend1_cell_body_get_identity(extend1_cell_body_t *inp, size_t idx)
  713. {
  714. trunnel_assert(idx < 20);
  715. return inp->identity[idx];
  716. }
  717. uint8_t
  718. extend1_cell_body_getconst_identity(const extend1_cell_body_t *inp, size_t idx)
  719. {
  720. return extend1_cell_body_get_identity((extend1_cell_body_t*)inp, idx);
  721. }
  722. int
  723. extend1_cell_body_set_identity(extend1_cell_body_t *inp, size_t idx, uint8_t elt)
  724. {
  725. trunnel_assert(idx < 20);
  726. inp->identity[idx] = elt;
  727. return 0;
  728. }
  729. uint8_t *
  730. extend1_cell_body_getarray_identity(extend1_cell_body_t *inp)
  731. {
  732. return inp->identity;
  733. }
  734. const uint8_t *
  735. extend1_cell_body_getconstarray_identity(const extend1_cell_body_t *inp)
  736. {
  737. return (const uint8_t *)extend1_cell_body_getarray_identity((extend1_cell_body_t*)inp);
  738. }
  739. const char *
  740. extend1_cell_body_check(const extend1_cell_body_t *obj)
  741. {
  742. if (obj == NULL)
  743. return "Object was NULL";
  744. if (obj->trunnel_error_code_)
  745. return "A set function failed on this object";
  746. return NULL;
  747. }
  748. ssize_t
  749. extend1_cell_body_encoded_len(const extend1_cell_body_t *obj)
  750. {
  751. ssize_t result = 0;
  752. if (NULL != extend1_cell_body_check(obj))
  753. return -1;
  754. /* Length of u32 ipv4addr */
  755. result += 4;
  756. /* Length of u16 port */
  757. result += 2;
  758. /* Length of u8 onionskin[186] */
  759. result += 186;
  760. /* Length of u8 identity[20] */
  761. result += 20;
  762. return result;
  763. }
  764. int
  765. extend1_cell_body_clear_errors(extend1_cell_body_t *obj)
  766. {
  767. int r = obj->trunnel_error_code_;
  768. obj->trunnel_error_code_ = 0;
  769. return r;
  770. }
  771. ssize_t
  772. extend1_cell_body_encode(uint8_t *output, const size_t avail, const extend1_cell_body_t *obj)
  773. {
  774. ssize_t result = 0;
  775. size_t written = 0;
  776. uint8_t *ptr = output;
  777. const char *msg;
  778. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  779. const ssize_t encoded_len = extend1_cell_body_encoded_len(obj);
  780. #endif
  781. if (NULL != (msg = extend1_cell_body_check(obj)))
  782. goto check_failed;
  783. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  784. trunnel_assert(encoded_len >= 0);
  785. #endif
  786. /* Encode u32 ipv4addr */
  787. trunnel_assert(written <= avail);
  788. if (avail - written < 4)
  789. goto truncated;
  790. trunnel_set_uint32(ptr, trunnel_htonl(obj->ipv4addr));
  791. written += 4; ptr += 4;
  792. /* Encode u16 port */
  793. trunnel_assert(written <= avail);
  794. if (avail - written < 2)
  795. goto truncated;
  796. trunnel_set_uint16(ptr, trunnel_htons(obj->port));
  797. written += 2; ptr += 2;
  798. /* Encode u8 onionskin[186] */
  799. trunnel_assert(written <= avail);
  800. if (avail - written < 186)
  801. goto truncated;
  802. memcpy(ptr, obj->onionskin, 186);
  803. written += 186; ptr += 186;
  804. /* Encode u8 identity[20] */
  805. trunnel_assert(written <= avail);
  806. if (avail - written < 20)
  807. goto truncated;
  808. memcpy(ptr, obj->identity, 20);
  809. written += 20; ptr += 20;
  810. trunnel_assert(ptr == output + written);
  811. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  812. {
  813. trunnel_assert(encoded_len >= 0);
  814. trunnel_assert((size_t)encoded_len == written);
  815. }
  816. #endif
  817. return written;
  818. truncated:
  819. result = -2;
  820. goto fail;
  821. check_failed:
  822. (void)msg;
  823. result = -1;
  824. goto fail;
  825. fail:
  826. trunnel_assert(result < 0);
  827. return result;
  828. }
  829. /** As extend1_cell_body_parse(), but do not allocate the output
  830. * object.
  831. */
  832. static ssize_t
  833. extend1_cell_body_parse_into(extend1_cell_body_t *obj, const uint8_t *input, const size_t len_in)
  834. {
  835. const uint8_t *ptr = input;
  836. size_t remaining = len_in;
  837. ssize_t result = 0;
  838. (void)result;
  839. /* Parse u32 ipv4addr */
  840. CHECK_REMAINING(4, truncated);
  841. obj->ipv4addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  842. remaining -= 4; ptr += 4;
  843. /* Parse u16 port */
  844. CHECK_REMAINING(2, truncated);
  845. obj->port = trunnel_ntohs(trunnel_get_uint16(ptr));
  846. remaining -= 2; ptr += 2;
  847. /* Parse u8 onionskin[186] */
  848. CHECK_REMAINING(186, truncated);
  849. memcpy(obj->onionskin, ptr, 186);
  850. remaining -= 186; ptr += 186;
  851. /* Parse u8 identity[20] */
  852. CHECK_REMAINING(20, truncated);
  853. memcpy(obj->identity, ptr, 20);
  854. remaining -= 20; ptr += 20;
  855. trunnel_assert(ptr + remaining == input + len_in);
  856. return len_in - remaining;
  857. truncated:
  858. return -2;
  859. }
  860. ssize_t
  861. extend1_cell_body_parse(extend1_cell_body_t **output, const uint8_t *input, const size_t len_in)
  862. {
  863. ssize_t result;
  864. *output = extend1_cell_body_new();
  865. if (NULL == *output)
  866. return -1;
  867. result = extend1_cell_body_parse_into(*output, input, len_in);
  868. if (result < 0) {
  869. extend1_cell_body_free(*output);
  870. *output = NULL;
  871. }
  872. return result;
  873. }
  874. link_specifier_t *
  875. link_specifier_new(void)
  876. {
  877. link_specifier_t *val = trunnel_calloc(1, sizeof(link_specifier_t));
  878. if (NULL == val)
  879. return NULL;
  880. return val;
  881. }
  882. /** Release all storage held inside 'obj', but do not free 'obj'.
  883. */
  884. static void
  885. link_specifier_clear(link_specifier_t *obj)
  886. {
  887. (void) obj;
  888. TRUNNEL_DYNARRAY_WIPE(&obj->un_unrecognized);
  889. TRUNNEL_DYNARRAY_CLEAR(&obj->un_unrecognized);
  890. }
  891. void
  892. link_specifier_free(link_specifier_t *obj)
  893. {
  894. if (obj == NULL)
  895. return;
  896. link_specifier_clear(obj);
  897. trunnel_memwipe(obj, sizeof(link_specifier_t));
  898. trunnel_free_(obj);
  899. }
  900. uint8_t
  901. link_specifier_get_ls_type(const link_specifier_t *inp)
  902. {
  903. return inp->ls_type;
  904. }
  905. int
  906. link_specifier_set_ls_type(link_specifier_t *inp, uint8_t val)
  907. {
  908. inp->ls_type = val;
  909. return 0;
  910. }
  911. uint8_t
  912. link_specifier_get_ls_len(const link_specifier_t *inp)
  913. {
  914. return inp->ls_len;
  915. }
  916. int
  917. link_specifier_set_ls_len(link_specifier_t *inp, uint8_t val)
  918. {
  919. inp->ls_len = val;
  920. return 0;
  921. }
  922. uint32_t
  923. link_specifier_get_un_ipv4_addr(const link_specifier_t *inp)
  924. {
  925. return inp->un_ipv4_addr;
  926. }
  927. int
  928. link_specifier_set_un_ipv4_addr(link_specifier_t *inp, uint32_t val)
  929. {
  930. inp->un_ipv4_addr = val;
  931. return 0;
  932. }
  933. uint16_t
  934. link_specifier_get_un_ipv4_port(const link_specifier_t *inp)
  935. {
  936. return inp->un_ipv4_port;
  937. }
  938. int
  939. link_specifier_set_un_ipv4_port(link_specifier_t *inp, uint16_t val)
  940. {
  941. inp->un_ipv4_port = val;
  942. return 0;
  943. }
  944. size_t
  945. link_specifier_getlen_un_ipv6_addr(const link_specifier_t *inp)
  946. {
  947. (void)inp; return 16;
  948. }
  949. uint8_t
  950. link_specifier_get_un_ipv6_addr(link_specifier_t *inp, size_t idx)
  951. {
  952. trunnel_assert(idx < 16);
  953. return inp->un_ipv6_addr[idx];
  954. }
  955. uint8_t
  956. link_specifier_getconst_un_ipv6_addr(const link_specifier_t *inp, size_t idx)
  957. {
  958. return link_specifier_get_un_ipv6_addr((link_specifier_t*)inp, idx);
  959. }
  960. int
  961. link_specifier_set_un_ipv6_addr(link_specifier_t *inp, size_t idx, uint8_t elt)
  962. {
  963. trunnel_assert(idx < 16);
  964. inp->un_ipv6_addr[idx] = elt;
  965. return 0;
  966. }
  967. uint8_t *
  968. link_specifier_getarray_un_ipv6_addr(link_specifier_t *inp)
  969. {
  970. return inp->un_ipv6_addr;
  971. }
  972. const uint8_t *
  973. link_specifier_getconstarray_un_ipv6_addr(const link_specifier_t *inp)
  974. {
  975. return (const uint8_t *)link_specifier_getarray_un_ipv6_addr((link_specifier_t*)inp);
  976. }
  977. uint16_t
  978. link_specifier_get_un_ipv6_port(const link_specifier_t *inp)
  979. {
  980. return inp->un_ipv6_port;
  981. }
  982. int
  983. link_specifier_set_un_ipv6_port(link_specifier_t *inp, uint16_t val)
  984. {
  985. inp->un_ipv6_port = val;
  986. return 0;
  987. }
  988. size_t
  989. link_specifier_getlen_un_legacy_id(const link_specifier_t *inp)
  990. {
  991. (void)inp; return 20;
  992. }
  993. uint8_t
  994. link_specifier_get_un_legacy_id(link_specifier_t *inp, size_t idx)
  995. {
  996. trunnel_assert(idx < 20);
  997. return inp->un_legacy_id[idx];
  998. }
  999. uint8_t
  1000. link_specifier_getconst_un_legacy_id(const link_specifier_t *inp, size_t idx)
  1001. {
  1002. return link_specifier_get_un_legacy_id((link_specifier_t*)inp, idx);
  1003. }
  1004. int
  1005. link_specifier_set_un_legacy_id(link_specifier_t *inp, size_t idx, uint8_t elt)
  1006. {
  1007. trunnel_assert(idx < 20);
  1008. inp->un_legacy_id[idx] = elt;
  1009. return 0;
  1010. }
  1011. uint8_t *
  1012. link_specifier_getarray_un_legacy_id(link_specifier_t *inp)
  1013. {
  1014. return inp->un_legacy_id;
  1015. }
  1016. const uint8_t *
  1017. link_specifier_getconstarray_un_legacy_id(const link_specifier_t *inp)
  1018. {
  1019. return (const uint8_t *)link_specifier_getarray_un_legacy_id((link_specifier_t*)inp);
  1020. }
  1021. size_t
  1022. link_specifier_getlen_un_ed25519_id(const link_specifier_t *inp)
  1023. {
  1024. (void)inp; return 32;
  1025. }
  1026. uint8_t
  1027. link_specifier_get_un_ed25519_id(link_specifier_t *inp, size_t idx)
  1028. {
  1029. trunnel_assert(idx < 32);
  1030. return inp->un_ed25519_id[idx];
  1031. }
  1032. uint8_t
  1033. link_specifier_getconst_un_ed25519_id(const link_specifier_t *inp, size_t idx)
  1034. {
  1035. return link_specifier_get_un_ed25519_id((link_specifier_t*)inp, idx);
  1036. }
  1037. int
  1038. link_specifier_set_un_ed25519_id(link_specifier_t *inp, size_t idx, uint8_t elt)
  1039. {
  1040. trunnel_assert(idx < 32);
  1041. inp->un_ed25519_id[idx] = elt;
  1042. return 0;
  1043. }
  1044. uint8_t *
  1045. link_specifier_getarray_un_ed25519_id(link_specifier_t *inp)
  1046. {
  1047. return inp->un_ed25519_id;
  1048. }
  1049. const uint8_t *
  1050. link_specifier_getconstarray_un_ed25519_id(const link_specifier_t *inp)
  1051. {
  1052. return (const uint8_t *)link_specifier_getarray_un_ed25519_id((link_specifier_t*)inp);
  1053. }
  1054. size_t
  1055. link_specifier_getlen_un_unrecognized(const link_specifier_t *inp)
  1056. {
  1057. return TRUNNEL_DYNARRAY_LEN(&inp->un_unrecognized);
  1058. }
  1059. uint8_t
  1060. link_specifier_get_un_unrecognized(link_specifier_t *inp, size_t idx)
  1061. {
  1062. return TRUNNEL_DYNARRAY_GET(&inp->un_unrecognized, idx);
  1063. }
  1064. uint8_t
  1065. link_specifier_getconst_un_unrecognized(const link_specifier_t *inp, size_t idx)
  1066. {
  1067. return link_specifier_get_un_unrecognized((link_specifier_t*)inp, idx);
  1068. }
  1069. int
  1070. link_specifier_set_un_unrecognized(link_specifier_t *inp, size_t idx, uint8_t elt)
  1071. {
  1072. TRUNNEL_DYNARRAY_SET(&inp->un_unrecognized, idx, elt);
  1073. return 0;
  1074. }
  1075. int
  1076. link_specifier_add_un_unrecognized(link_specifier_t *inp, uint8_t elt)
  1077. {
  1078. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->un_unrecognized, elt, {});
  1079. return 0;
  1080. trunnel_alloc_failed:
  1081. TRUNNEL_SET_ERROR_CODE(inp);
  1082. return -1;
  1083. }
  1084. uint8_t *
  1085. link_specifier_getarray_un_unrecognized(link_specifier_t *inp)
  1086. {
  1087. return inp->un_unrecognized.elts_;
  1088. }
  1089. const uint8_t *
  1090. link_specifier_getconstarray_un_unrecognized(const link_specifier_t *inp)
  1091. {
  1092. return (const uint8_t *)link_specifier_getarray_un_unrecognized((link_specifier_t*)inp);
  1093. }
  1094. int
  1095. link_specifier_setlen_un_unrecognized(link_specifier_t *inp, size_t newlen)
  1096. {
  1097. uint8_t *newptr;
  1098. newptr = trunnel_dynarray_setlen(&inp->un_unrecognized.allocated_,
  1099. &inp->un_unrecognized.n_, inp->un_unrecognized.elts_, newlen,
  1100. sizeof(inp->un_unrecognized.elts_[0]), (trunnel_free_fn_t) NULL,
  1101. &inp->trunnel_error_code_);
  1102. if (newlen != 0 && newptr == NULL)
  1103. goto trunnel_alloc_failed;
  1104. inp->un_unrecognized.elts_ = newptr;
  1105. return 0;
  1106. trunnel_alloc_failed:
  1107. TRUNNEL_SET_ERROR_CODE(inp);
  1108. return -1;
  1109. }
  1110. const char *
  1111. link_specifier_check(const link_specifier_t *obj)
  1112. {
  1113. if (obj == NULL)
  1114. return "Object was NULL";
  1115. if (obj->trunnel_error_code_)
  1116. return "A set function failed on this object";
  1117. switch (obj->ls_type) {
  1118. case LS_IPV4:
  1119. break;
  1120. case LS_IPV6:
  1121. break;
  1122. case LS_LEGACY_ID:
  1123. break;
  1124. case LS_ED25519_ID:
  1125. break;
  1126. default:
  1127. break;
  1128. }
  1129. return NULL;
  1130. }
  1131. ssize_t
  1132. link_specifier_encoded_len(const link_specifier_t *obj)
  1133. {
  1134. ssize_t result = 0;
  1135. if (NULL != link_specifier_check(obj))
  1136. return -1;
  1137. /* Length of u8 ls_type */
  1138. result += 1;
  1139. /* Length of u8 ls_len */
  1140. result += 1;
  1141. switch (obj->ls_type) {
  1142. case LS_IPV4:
  1143. /* Length of u32 un_ipv4_addr */
  1144. result += 4;
  1145. /* Length of u16 un_ipv4_port */
  1146. result += 2;
  1147. break;
  1148. case LS_IPV6:
  1149. /* Length of u8 un_ipv6_addr[16] */
  1150. result += 16;
  1151. /* Length of u16 un_ipv6_port */
  1152. result += 2;
  1153. break;
  1154. case LS_LEGACY_ID:
  1155. /* Length of u8 un_legacy_id[20] */
  1156. result += 20;
  1157. break;
  1158. case LS_ED25519_ID:
  1159. /* Length of u8 un_ed25519_id[32] */
  1160. result += 32;
  1161. break;
  1162. default:
  1163. /* Length of u8 un_unrecognized[] */
  1164. result += TRUNNEL_DYNARRAY_LEN(&obj->un_unrecognized);
  1165. break;
  1166. }
  1167. return result;
  1168. }
  1169. int
  1170. link_specifier_clear_errors(link_specifier_t *obj)
  1171. {
  1172. int r = obj->trunnel_error_code_;
  1173. obj->trunnel_error_code_ = 0;
  1174. return r;
  1175. }
  1176. ssize_t
  1177. link_specifier_encode(uint8_t *output, const size_t avail, const link_specifier_t *obj)
  1178. {
  1179. ssize_t result = 0;
  1180. size_t written = 0;
  1181. uint8_t *ptr = output;
  1182. const char *msg;
  1183. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1184. const ssize_t encoded_len = link_specifier_encoded_len(obj);
  1185. #endif
  1186. uint8_t *backptr_ls_len = NULL;
  1187. if (NULL != (msg = link_specifier_check(obj)))
  1188. goto check_failed;
  1189. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1190. trunnel_assert(encoded_len >= 0);
  1191. #endif
  1192. /* Encode u8 ls_type */
  1193. trunnel_assert(written <= avail);
  1194. if (avail - written < 1)
  1195. goto truncated;
  1196. trunnel_set_uint8(ptr, (obj->ls_type));
  1197. written += 1; ptr += 1;
  1198. /* Encode u8 ls_len */
  1199. backptr_ls_len = ptr;
  1200. trunnel_assert(written <= avail);
  1201. if (avail - written < 1)
  1202. goto truncated;
  1203. trunnel_set_uint8(ptr, (obj->ls_len));
  1204. written += 1; ptr += 1;
  1205. {
  1206. size_t written_before_union = written;
  1207. /* Encode union un[ls_type] */
  1208. trunnel_assert(written <= avail);
  1209. switch (obj->ls_type) {
  1210. case LS_IPV4:
  1211. /* Encode u32 un_ipv4_addr */
  1212. trunnel_assert(written <= avail);
  1213. if (avail - written < 4)
  1214. goto truncated;
  1215. trunnel_set_uint32(ptr, trunnel_htonl(obj->un_ipv4_addr));
  1216. written += 4; ptr += 4;
  1217. /* Encode u16 un_ipv4_port */
  1218. trunnel_assert(written <= avail);
  1219. if (avail - written < 2)
  1220. goto truncated;
  1221. trunnel_set_uint16(ptr, trunnel_htons(obj->un_ipv4_port));
  1222. written += 2; ptr += 2;
  1223. break;
  1224. case LS_IPV6:
  1225. /* Encode u8 un_ipv6_addr[16] */
  1226. trunnel_assert(written <= avail);
  1227. if (avail - written < 16)
  1228. goto truncated;
  1229. memcpy(ptr, obj->un_ipv6_addr, 16);
  1230. written += 16; ptr += 16;
  1231. /* Encode u16 un_ipv6_port */
  1232. trunnel_assert(written <= avail);
  1233. if (avail - written < 2)
  1234. goto truncated;
  1235. trunnel_set_uint16(ptr, trunnel_htons(obj->un_ipv6_port));
  1236. written += 2; ptr += 2;
  1237. break;
  1238. case LS_LEGACY_ID:
  1239. /* Encode u8 un_legacy_id[20] */
  1240. trunnel_assert(written <= avail);
  1241. if (avail - written < 20)
  1242. goto truncated;
  1243. memcpy(ptr, obj->un_legacy_id, 20);
  1244. written += 20; ptr += 20;
  1245. break;
  1246. case LS_ED25519_ID:
  1247. /* Encode u8 un_ed25519_id[32] */
  1248. trunnel_assert(written <= avail);
  1249. if (avail - written < 32)
  1250. goto truncated;
  1251. memcpy(ptr, obj->un_ed25519_id, 32);
  1252. written += 32; ptr += 32;
  1253. break;
  1254. default:
  1255. /* Encode u8 un_unrecognized[] */
  1256. {
  1257. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->un_unrecognized);
  1258. trunnel_assert(written <= avail);
  1259. if (avail - written < elt_len)
  1260. goto truncated;
  1261. if (elt_len)
  1262. memcpy(ptr, obj->un_unrecognized.elts_, elt_len);
  1263. written += elt_len; ptr += elt_len;
  1264. }
  1265. break;
  1266. }
  1267. /* Write the length field back to ls_len */
  1268. trunnel_assert(written >= written_before_union);
  1269. #if UINT8_MAX < SIZE_MAX
  1270. if (written - written_before_union > UINT8_MAX)
  1271. goto check_failed;
  1272. #endif
  1273. trunnel_set_uint8(backptr_ls_len, (written - written_before_union));
  1274. }
  1275. trunnel_assert(ptr == output + written);
  1276. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1277. {
  1278. trunnel_assert(encoded_len >= 0);
  1279. trunnel_assert((size_t)encoded_len == written);
  1280. }
  1281. #endif
  1282. return written;
  1283. truncated:
  1284. result = -2;
  1285. goto fail;
  1286. check_failed:
  1287. (void)msg;
  1288. result = -1;
  1289. goto fail;
  1290. fail:
  1291. trunnel_assert(result < 0);
  1292. return result;
  1293. }
  1294. /** As link_specifier_parse(), but do not allocate the output object.
  1295. */
  1296. static ssize_t
  1297. link_specifier_parse_into(link_specifier_t *obj, const uint8_t *input, const size_t len_in)
  1298. {
  1299. const uint8_t *ptr = input;
  1300. size_t remaining = len_in;
  1301. ssize_t result = 0;
  1302. (void)result;
  1303. /* Parse u8 ls_type */
  1304. CHECK_REMAINING(1, truncated);
  1305. obj->ls_type = (trunnel_get_uint8(ptr));
  1306. remaining -= 1; ptr += 1;
  1307. /* Parse u8 ls_len */
  1308. CHECK_REMAINING(1, truncated);
  1309. obj->ls_len = (trunnel_get_uint8(ptr));
  1310. remaining -= 1; ptr += 1;
  1311. {
  1312. size_t remaining_after;
  1313. CHECK_REMAINING(obj->ls_len, truncated);
  1314. remaining_after = remaining - obj->ls_len;
  1315. remaining = obj->ls_len;
  1316. /* Parse union un[ls_type] */
  1317. switch (obj->ls_type) {
  1318. case LS_IPV4:
  1319. /* Parse u32 un_ipv4_addr */
  1320. CHECK_REMAINING(4, fail);
  1321. obj->un_ipv4_addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  1322. remaining -= 4; ptr += 4;
  1323. /* Parse u16 un_ipv4_port */
  1324. CHECK_REMAINING(2, fail);
  1325. obj->un_ipv4_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  1326. remaining -= 2; ptr += 2;
  1327. break;
  1328. case LS_IPV6:
  1329. /* Parse u8 un_ipv6_addr[16] */
  1330. CHECK_REMAINING(16, fail);
  1331. memcpy(obj->un_ipv6_addr, ptr, 16);
  1332. remaining -= 16; ptr += 16;
  1333. /* Parse u16 un_ipv6_port */
  1334. CHECK_REMAINING(2, fail);
  1335. obj->un_ipv6_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  1336. remaining -= 2; ptr += 2;
  1337. break;
  1338. case LS_LEGACY_ID:
  1339. /* Parse u8 un_legacy_id[20] */
  1340. CHECK_REMAINING(20, fail);
  1341. memcpy(obj->un_legacy_id, ptr, 20);
  1342. remaining -= 20; ptr += 20;
  1343. break;
  1344. case LS_ED25519_ID:
  1345. /* Parse u8 un_ed25519_id[32] */
  1346. CHECK_REMAINING(32, fail);
  1347. memcpy(obj->un_ed25519_id, ptr, 32);
  1348. remaining -= 32; ptr += 32;
  1349. break;
  1350. default:
  1351. /* Parse u8 un_unrecognized[] */
  1352. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unrecognized, remaining, {});
  1353. obj->un_unrecognized.n_ = remaining;
  1354. if (remaining)
  1355. memcpy(obj->un_unrecognized.elts_, ptr, remaining);
  1356. ptr += remaining; remaining -= remaining;
  1357. break;
  1358. }
  1359. if (remaining != 0)
  1360. goto fail;
  1361. remaining = remaining_after;
  1362. }
  1363. trunnel_assert(ptr + remaining == input + len_in);
  1364. return len_in - remaining;
  1365. truncated:
  1366. return -2;
  1367. trunnel_alloc_failed:
  1368. return -1;
  1369. fail:
  1370. result = -1;
  1371. return result;
  1372. }
  1373. ssize_t
  1374. link_specifier_parse(link_specifier_t **output, const uint8_t *input, const size_t len_in)
  1375. {
  1376. ssize_t result;
  1377. *output = link_specifier_new();
  1378. if (NULL == *output)
  1379. return -1;
  1380. result = link_specifier_parse_into(*output, input, len_in);
  1381. if (result < 0) {
  1382. link_specifier_free(*output);
  1383. *output = NULL;
  1384. }
  1385. return result;
  1386. }
  1387. ed25519_cert_t *
  1388. ed25519_cert_new(void)
  1389. {
  1390. ed25519_cert_t *val = trunnel_calloc(1, sizeof(ed25519_cert_t));
  1391. if (NULL == val)
  1392. return NULL;
  1393. val->version = 1;
  1394. return val;
  1395. }
  1396. /** Release all storage held inside 'obj', but do not free 'obj'.
  1397. */
  1398. static void
  1399. ed25519_cert_clear(ed25519_cert_t *obj)
  1400. {
  1401. (void) obj;
  1402. {
  1403. unsigned idx;
  1404. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1405. ed25519_cert_extension_free(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  1406. }
  1407. }
  1408. TRUNNEL_DYNARRAY_WIPE(&obj->ext);
  1409. TRUNNEL_DYNARRAY_CLEAR(&obj->ext);
  1410. }
  1411. void
  1412. ed25519_cert_free(ed25519_cert_t *obj)
  1413. {
  1414. if (obj == NULL)
  1415. return;
  1416. ed25519_cert_clear(obj);
  1417. trunnel_memwipe(obj, sizeof(ed25519_cert_t));
  1418. trunnel_free_(obj);
  1419. }
  1420. uint8_t
  1421. ed25519_cert_get_version(const ed25519_cert_t *inp)
  1422. {
  1423. return inp->version;
  1424. }
  1425. int
  1426. ed25519_cert_set_version(ed25519_cert_t *inp, uint8_t val)
  1427. {
  1428. if (! ((val == 1))) {
  1429. TRUNNEL_SET_ERROR_CODE(inp);
  1430. return -1;
  1431. }
  1432. inp->version = val;
  1433. return 0;
  1434. }
  1435. uint8_t
  1436. ed25519_cert_get_cert_type(const ed25519_cert_t *inp)
  1437. {
  1438. return inp->cert_type;
  1439. }
  1440. int
  1441. ed25519_cert_set_cert_type(ed25519_cert_t *inp, uint8_t val)
  1442. {
  1443. inp->cert_type = val;
  1444. return 0;
  1445. }
  1446. uint32_t
  1447. ed25519_cert_get_exp_field(const ed25519_cert_t *inp)
  1448. {
  1449. return inp->exp_field;
  1450. }
  1451. int
  1452. ed25519_cert_set_exp_field(ed25519_cert_t *inp, uint32_t val)
  1453. {
  1454. inp->exp_field = val;
  1455. return 0;
  1456. }
  1457. uint8_t
  1458. ed25519_cert_get_cert_key_type(const ed25519_cert_t *inp)
  1459. {
  1460. return inp->cert_key_type;
  1461. }
  1462. int
  1463. ed25519_cert_set_cert_key_type(ed25519_cert_t *inp, uint8_t val)
  1464. {
  1465. inp->cert_key_type = val;
  1466. return 0;
  1467. }
  1468. size_t
  1469. ed25519_cert_getlen_certified_key(const ed25519_cert_t *inp)
  1470. {
  1471. (void)inp; return 32;
  1472. }
  1473. uint8_t
  1474. ed25519_cert_get_certified_key(ed25519_cert_t *inp, size_t idx)
  1475. {
  1476. trunnel_assert(idx < 32);
  1477. return inp->certified_key[idx];
  1478. }
  1479. uint8_t
  1480. ed25519_cert_getconst_certified_key(const ed25519_cert_t *inp, size_t idx)
  1481. {
  1482. return ed25519_cert_get_certified_key((ed25519_cert_t*)inp, idx);
  1483. }
  1484. int
  1485. ed25519_cert_set_certified_key(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  1486. {
  1487. trunnel_assert(idx < 32);
  1488. inp->certified_key[idx] = elt;
  1489. return 0;
  1490. }
  1491. uint8_t *
  1492. ed25519_cert_getarray_certified_key(ed25519_cert_t *inp)
  1493. {
  1494. return inp->certified_key;
  1495. }
  1496. const uint8_t *
  1497. ed25519_cert_getconstarray_certified_key(const ed25519_cert_t *inp)
  1498. {
  1499. return (const uint8_t *)ed25519_cert_getarray_certified_key((ed25519_cert_t*)inp);
  1500. }
  1501. uint8_t
  1502. ed25519_cert_get_n_extensions(const ed25519_cert_t *inp)
  1503. {
  1504. return inp->n_extensions;
  1505. }
  1506. int
  1507. ed25519_cert_set_n_extensions(ed25519_cert_t *inp, uint8_t val)
  1508. {
  1509. inp->n_extensions = val;
  1510. return 0;
  1511. }
  1512. size_t
  1513. ed25519_cert_getlen_ext(const ed25519_cert_t *inp)
  1514. {
  1515. return TRUNNEL_DYNARRAY_LEN(&inp->ext);
  1516. }
  1517. struct ed25519_cert_extension_st *
  1518. ed25519_cert_get_ext(ed25519_cert_t *inp, size_t idx)
  1519. {
  1520. return TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  1521. }
  1522. const struct ed25519_cert_extension_st *
  1523. ed25519_cert_getconst_ext(const ed25519_cert_t *inp, size_t idx)
  1524. {
  1525. return ed25519_cert_get_ext((ed25519_cert_t*)inp, idx);
  1526. }
  1527. int
  1528. ed25519_cert_set_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  1529. {
  1530. ed25519_cert_extension_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  1531. if (oldval && oldval != elt)
  1532. ed25519_cert_extension_free(oldval);
  1533. return ed25519_cert_set0_ext(inp, idx, elt);
  1534. }
  1535. int
  1536. ed25519_cert_set0_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  1537. {
  1538. TRUNNEL_DYNARRAY_SET(&inp->ext, idx, elt);
  1539. return 0;
  1540. }
  1541. int
  1542. ed25519_cert_add_ext(ed25519_cert_t *inp, struct ed25519_cert_extension_st * elt)
  1543. {
  1544. #if SIZE_MAX >= UINT8_MAX
  1545. if (inp->ext.n_ == UINT8_MAX)
  1546. goto trunnel_alloc_failed;
  1547. #endif
  1548. TRUNNEL_DYNARRAY_ADD(struct ed25519_cert_extension_st *, &inp->ext, elt, {});
  1549. return 0;
  1550. trunnel_alloc_failed:
  1551. TRUNNEL_SET_ERROR_CODE(inp);
  1552. return -1;
  1553. }
  1554. struct ed25519_cert_extension_st * *
  1555. ed25519_cert_getarray_ext(ed25519_cert_t *inp)
  1556. {
  1557. return inp->ext.elts_;
  1558. }
  1559. const struct ed25519_cert_extension_st * const *
  1560. ed25519_cert_getconstarray_ext(const ed25519_cert_t *inp)
  1561. {
  1562. return (const struct ed25519_cert_extension_st * const *)ed25519_cert_getarray_ext((ed25519_cert_t*)inp);
  1563. }
  1564. int
  1565. ed25519_cert_setlen_ext(ed25519_cert_t *inp, size_t newlen)
  1566. {
  1567. struct ed25519_cert_extension_st * *newptr;
  1568. #if UINT8_MAX < SIZE_MAX
  1569. if (newlen > UINT8_MAX)
  1570. goto trunnel_alloc_failed;
  1571. #endif
  1572. newptr = trunnel_dynarray_setlen(&inp->ext.allocated_,
  1573. &inp->ext.n_, inp->ext.elts_, newlen,
  1574. sizeof(inp->ext.elts_[0]), (trunnel_free_fn_t) ed25519_cert_extension_free,
  1575. &inp->trunnel_error_code_);
  1576. if (newlen != 0 && newptr == NULL)
  1577. goto trunnel_alloc_failed;
  1578. inp->ext.elts_ = newptr;
  1579. return 0;
  1580. trunnel_alloc_failed:
  1581. TRUNNEL_SET_ERROR_CODE(inp);
  1582. return -1;
  1583. }
  1584. size_t
  1585. ed25519_cert_getlen_signature(const ed25519_cert_t *inp)
  1586. {
  1587. (void)inp; return 64;
  1588. }
  1589. uint8_t
  1590. ed25519_cert_get_signature(ed25519_cert_t *inp, size_t idx)
  1591. {
  1592. trunnel_assert(idx < 64);
  1593. return inp->signature[idx];
  1594. }
  1595. uint8_t
  1596. ed25519_cert_getconst_signature(const ed25519_cert_t *inp, size_t idx)
  1597. {
  1598. return ed25519_cert_get_signature((ed25519_cert_t*)inp, idx);
  1599. }
  1600. int
  1601. ed25519_cert_set_signature(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  1602. {
  1603. trunnel_assert(idx < 64);
  1604. inp->signature[idx] = elt;
  1605. return 0;
  1606. }
  1607. uint8_t *
  1608. ed25519_cert_getarray_signature(ed25519_cert_t *inp)
  1609. {
  1610. return inp->signature;
  1611. }
  1612. const uint8_t *
  1613. ed25519_cert_getconstarray_signature(const ed25519_cert_t *inp)
  1614. {
  1615. return (const uint8_t *)ed25519_cert_getarray_signature((ed25519_cert_t*)inp);
  1616. }
  1617. const char *
  1618. ed25519_cert_check(const ed25519_cert_t *obj)
  1619. {
  1620. if (obj == NULL)
  1621. return "Object was NULL";
  1622. if (obj->trunnel_error_code_)
  1623. return "A set function failed on this object";
  1624. if (! (obj->version == 1))
  1625. return "Integer out of bounds";
  1626. {
  1627. const char *msg;
  1628. unsigned idx;
  1629. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1630. if (NULL != (msg = ed25519_cert_extension_check(TRUNNEL_DYNARRAY_GET(&obj->ext, idx))))
  1631. return msg;
  1632. }
  1633. }
  1634. if (TRUNNEL_DYNARRAY_LEN(&obj->ext) != obj->n_extensions)
  1635. return "Length mismatch for ext";
  1636. return NULL;
  1637. }
  1638. ssize_t
  1639. ed25519_cert_encoded_len(const ed25519_cert_t *obj)
  1640. {
  1641. ssize_t result = 0;
  1642. if (NULL != ed25519_cert_check(obj))
  1643. return -1;
  1644. /* Length of u8 version IN [1] */
  1645. result += 1;
  1646. /* Length of u8 cert_type */
  1647. result += 1;
  1648. /* Length of u32 exp_field */
  1649. result += 4;
  1650. /* Length of u8 cert_key_type */
  1651. result += 1;
  1652. /* Length of u8 certified_key[32] */
  1653. result += 32;
  1654. /* Length of u8 n_extensions */
  1655. result += 1;
  1656. /* Length of struct ed25519_cert_extension ext[n_extensions] */
  1657. {
  1658. unsigned idx;
  1659. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1660. result += ed25519_cert_extension_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  1661. }
  1662. }
  1663. /* Length of u8 signature[64] */
  1664. result += 64;
  1665. return result;
  1666. }
  1667. int
  1668. ed25519_cert_clear_errors(ed25519_cert_t *obj)
  1669. {
  1670. int r = obj->trunnel_error_code_;
  1671. obj->trunnel_error_code_ = 0;
  1672. return r;
  1673. }
  1674. ssize_t
  1675. ed25519_cert_encode(uint8_t *output, const size_t avail, const ed25519_cert_t *obj)
  1676. {
  1677. ssize_t result = 0;
  1678. size_t written = 0;
  1679. uint8_t *ptr = output;
  1680. const char *msg;
  1681. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1682. const ssize_t encoded_len = ed25519_cert_encoded_len(obj);
  1683. #endif
  1684. if (NULL != (msg = ed25519_cert_check(obj)))
  1685. goto check_failed;
  1686. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1687. trunnel_assert(encoded_len >= 0);
  1688. #endif
  1689. /* Encode u8 version IN [1] */
  1690. trunnel_assert(written <= avail);
  1691. if (avail - written < 1)
  1692. goto truncated;
  1693. trunnel_set_uint8(ptr, (obj->version));
  1694. written += 1; ptr += 1;
  1695. /* Encode u8 cert_type */
  1696. trunnel_assert(written <= avail);
  1697. if (avail - written < 1)
  1698. goto truncated;
  1699. trunnel_set_uint8(ptr, (obj->cert_type));
  1700. written += 1; ptr += 1;
  1701. /* Encode u32 exp_field */
  1702. trunnel_assert(written <= avail);
  1703. if (avail - written < 4)
  1704. goto truncated;
  1705. trunnel_set_uint32(ptr, trunnel_htonl(obj->exp_field));
  1706. written += 4; ptr += 4;
  1707. /* Encode u8 cert_key_type */
  1708. trunnel_assert(written <= avail);
  1709. if (avail - written < 1)
  1710. goto truncated;
  1711. trunnel_set_uint8(ptr, (obj->cert_key_type));
  1712. written += 1; ptr += 1;
  1713. /* Encode u8 certified_key[32] */
  1714. trunnel_assert(written <= avail);
  1715. if (avail - written < 32)
  1716. goto truncated;
  1717. memcpy(ptr, obj->certified_key, 32);
  1718. written += 32; ptr += 32;
  1719. /* Encode u8 n_extensions */
  1720. trunnel_assert(written <= avail);
  1721. if (avail - written < 1)
  1722. goto truncated;
  1723. trunnel_set_uint8(ptr, (obj->n_extensions));
  1724. written += 1; ptr += 1;
  1725. /* Encode struct ed25519_cert_extension ext[n_extensions] */
  1726. {
  1727. unsigned idx;
  1728. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1729. trunnel_assert(written <= avail);
  1730. result = ed25519_cert_extension_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  1731. if (result < 0)
  1732. goto fail; /* XXXXXXX !*/
  1733. written += result; ptr += result;
  1734. }
  1735. }
  1736. /* Encode u8 signature[64] */
  1737. trunnel_assert(written <= avail);
  1738. if (avail - written < 64)
  1739. goto truncated;
  1740. memcpy(ptr, obj->signature, 64);
  1741. written += 64; ptr += 64;
  1742. trunnel_assert(ptr == output + written);
  1743. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1744. {
  1745. trunnel_assert(encoded_len >= 0);
  1746. trunnel_assert((size_t)encoded_len == written);
  1747. }
  1748. #endif
  1749. return written;
  1750. truncated:
  1751. result = -2;
  1752. goto fail;
  1753. check_failed:
  1754. (void)msg;
  1755. result = -1;
  1756. goto fail;
  1757. fail:
  1758. trunnel_assert(result < 0);
  1759. return result;
  1760. }
  1761. /** As ed25519_cert_parse(), but do not allocate the output object.
  1762. */
  1763. static ssize_t
  1764. ed25519_cert_parse_into(ed25519_cert_t *obj, const uint8_t *input, const size_t len_in)
  1765. {
  1766. const uint8_t *ptr = input;
  1767. size_t remaining = len_in;
  1768. ssize_t result = 0;
  1769. (void)result;
  1770. /* Parse u8 version IN [1] */
  1771. CHECK_REMAINING(1, truncated);
  1772. obj->version = (trunnel_get_uint8(ptr));
  1773. remaining -= 1; ptr += 1;
  1774. if (! (obj->version == 1))
  1775. goto fail;
  1776. /* Parse u8 cert_type */
  1777. CHECK_REMAINING(1, truncated);
  1778. obj->cert_type = (trunnel_get_uint8(ptr));
  1779. remaining -= 1; ptr += 1;
  1780. /* Parse u32 exp_field */
  1781. CHECK_REMAINING(4, truncated);
  1782. obj->exp_field = trunnel_ntohl(trunnel_get_uint32(ptr));
  1783. remaining -= 4; ptr += 4;
  1784. /* Parse u8 cert_key_type */
  1785. CHECK_REMAINING(1, truncated);
  1786. obj->cert_key_type = (trunnel_get_uint8(ptr));
  1787. remaining -= 1; ptr += 1;
  1788. /* Parse u8 certified_key[32] */
  1789. CHECK_REMAINING(32, truncated);
  1790. memcpy(obj->certified_key, ptr, 32);
  1791. remaining -= 32; ptr += 32;
  1792. /* Parse u8 n_extensions */
  1793. CHECK_REMAINING(1, truncated);
  1794. obj->n_extensions = (trunnel_get_uint8(ptr));
  1795. remaining -= 1; ptr += 1;
  1796. /* Parse struct ed25519_cert_extension ext[n_extensions] */
  1797. TRUNNEL_DYNARRAY_EXPAND(ed25519_cert_extension_t *, &obj->ext, obj->n_extensions, {});
  1798. {
  1799. ed25519_cert_extension_t * elt;
  1800. unsigned idx;
  1801. for (idx = 0; idx < obj->n_extensions; ++idx) {
  1802. result = ed25519_cert_extension_parse(&elt, ptr, remaining);
  1803. if (result < 0)
  1804. goto relay_fail;
  1805. trunnel_assert((size_t)result <= remaining);
  1806. remaining -= result; ptr += result;
  1807. TRUNNEL_DYNARRAY_ADD(ed25519_cert_extension_t *, &obj->ext, elt, {ed25519_cert_extension_free(elt);});
  1808. }
  1809. }
  1810. /* Parse u8 signature[64] */
  1811. CHECK_REMAINING(64, truncated);
  1812. memcpy(obj->signature, ptr, 64);
  1813. remaining -= 64; ptr += 64;
  1814. trunnel_assert(ptr + remaining == input + len_in);
  1815. return len_in - remaining;
  1816. truncated:
  1817. return -2;
  1818. relay_fail:
  1819. trunnel_assert(result < 0);
  1820. return result;
  1821. trunnel_alloc_failed:
  1822. return -1;
  1823. fail:
  1824. result = -1;
  1825. return result;
  1826. }
  1827. ssize_t
  1828. ed25519_cert_parse(ed25519_cert_t **output, const uint8_t *input, const size_t len_in)
  1829. {
  1830. ssize_t result;
  1831. *output = ed25519_cert_new();
  1832. if (NULL == *output)
  1833. return -1;
  1834. result = ed25519_cert_parse_into(*output, input, len_in);
  1835. if (result < 0) {
  1836. ed25519_cert_free(*output);
  1837. *output = NULL;
  1838. }
  1839. return result;
  1840. }
  1841. extend2_cell_body_t *
  1842. extend2_cell_body_new(void)
  1843. {
  1844. extend2_cell_body_t *val = trunnel_calloc(1, sizeof(extend2_cell_body_t));
  1845. if (NULL == val)
  1846. return NULL;
  1847. return val;
  1848. }
  1849. /** Release all storage held inside 'obj', but do not free 'obj'.
  1850. */
  1851. static void
  1852. extend2_cell_body_clear(extend2_cell_body_t *obj)
  1853. {
  1854. (void) obj;
  1855. {
  1856. unsigned idx;
  1857. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ls); ++idx) {
  1858. link_specifier_free(TRUNNEL_DYNARRAY_GET(&obj->ls, idx));
  1859. }
  1860. }
  1861. TRUNNEL_DYNARRAY_WIPE(&obj->ls);
  1862. TRUNNEL_DYNARRAY_CLEAR(&obj->ls);
  1863. create2_cell_body_free(obj->create2);
  1864. obj->create2 = NULL;
  1865. }
  1866. void
  1867. extend2_cell_body_free(extend2_cell_body_t *obj)
  1868. {
  1869. if (obj == NULL)
  1870. return;
  1871. extend2_cell_body_clear(obj);
  1872. trunnel_memwipe(obj, sizeof(extend2_cell_body_t));
  1873. trunnel_free_(obj);
  1874. }
  1875. uint8_t
  1876. extend2_cell_body_get_n_spec(const extend2_cell_body_t *inp)
  1877. {
  1878. return inp->n_spec;
  1879. }
  1880. int
  1881. extend2_cell_body_set_n_spec(extend2_cell_body_t *inp, uint8_t val)
  1882. {
  1883. inp->n_spec = val;
  1884. return 0;
  1885. }
  1886. size_t
  1887. extend2_cell_body_getlen_ls(const extend2_cell_body_t *inp)
  1888. {
  1889. return TRUNNEL_DYNARRAY_LEN(&inp->ls);
  1890. }
  1891. struct link_specifier_st *
  1892. extend2_cell_body_get_ls(extend2_cell_body_t *inp, size_t idx)
  1893. {
  1894. return TRUNNEL_DYNARRAY_GET(&inp->ls, idx);
  1895. }
  1896. const struct link_specifier_st *
  1897. extend2_cell_body_getconst_ls(const extend2_cell_body_t *inp, size_t idx)
  1898. {
  1899. return extend2_cell_body_get_ls((extend2_cell_body_t*)inp, idx);
  1900. }
  1901. int
  1902. extend2_cell_body_set_ls(extend2_cell_body_t *inp, size_t idx, struct link_specifier_st * elt)
  1903. {
  1904. link_specifier_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->ls, idx);
  1905. if (oldval && oldval != elt)
  1906. link_specifier_free(oldval);
  1907. return extend2_cell_body_set0_ls(inp, idx, elt);
  1908. }
  1909. int
  1910. extend2_cell_body_set0_ls(extend2_cell_body_t *inp, size_t idx, struct link_specifier_st * elt)
  1911. {
  1912. TRUNNEL_DYNARRAY_SET(&inp->ls, idx, elt);
  1913. return 0;
  1914. }
  1915. int
  1916. extend2_cell_body_add_ls(extend2_cell_body_t *inp, struct link_specifier_st * elt)
  1917. {
  1918. #if SIZE_MAX >= UINT8_MAX
  1919. if (inp->ls.n_ == UINT8_MAX)
  1920. goto trunnel_alloc_failed;
  1921. #endif
  1922. TRUNNEL_DYNARRAY_ADD(struct link_specifier_st *, &inp->ls, elt, {});
  1923. return 0;
  1924. trunnel_alloc_failed:
  1925. TRUNNEL_SET_ERROR_CODE(inp);
  1926. return -1;
  1927. }
  1928. struct link_specifier_st * *
  1929. extend2_cell_body_getarray_ls(extend2_cell_body_t *inp)
  1930. {
  1931. return inp->ls.elts_;
  1932. }
  1933. const struct link_specifier_st * const *
  1934. extend2_cell_body_getconstarray_ls(const extend2_cell_body_t *inp)
  1935. {
  1936. return (const struct link_specifier_st * const *)extend2_cell_body_getarray_ls((extend2_cell_body_t*)inp);
  1937. }
  1938. int
  1939. extend2_cell_body_setlen_ls(extend2_cell_body_t *inp, size_t newlen)
  1940. {
  1941. struct link_specifier_st * *newptr;
  1942. #if UINT8_MAX < SIZE_MAX
  1943. if (newlen > UINT8_MAX)
  1944. goto trunnel_alloc_failed;
  1945. #endif
  1946. newptr = trunnel_dynarray_setlen(&inp->ls.allocated_,
  1947. &inp->ls.n_, inp->ls.elts_, newlen,
  1948. sizeof(inp->ls.elts_[0]), (trunnel_free_fn_t) link_specifier_free,
  1949. &inp->trunnel_error_code_);
  1950. if (newlen != 0 && newptr == NULL)
  1951. goto trunnel_alloc_failed;
  1952. inp->ls.elts_ = newptr;
  1953. return 0;
  1954. trunnel_alloc_failed:
  1955. TRUNNEL_SET_ERROR_CODE(inp);
  1956. return -1;
  1957. }
  1958. struct create2_cell_body_st *
  1959. extend2_cell_body_get_create2(extend2_cell_body_t *inp)
  1960. {
  1961. return inp->create2;
  1962. }
  1963. const struct create2_cell_body_st *
  1964. extend2_cell_body_getconst_create2(const extend2_cell_body_t *inp)
  1965. {
  1966. return extend2_cell_body_get_create2((extend2_cell_body_t*) inp);
  1967. }
  1968. int
  1969. extend2_cell_body_set_create2(extend2_cell_body_t *inp, struct create2_cell_body_st *val)
  1970. {
  1971. if (inp->create2 && inp->create2 != val)
  1972. create2_cell_body_free(inp->create2);
  1973. return extend2_cell_body_set0_create2(inp, val);
  1974. }
  1975. int
  1976. extend2_cell_body_set0_create2(extend2_cell_body_t *inp, struct create2_cell_body_st *val)
  1977. {
  1978. inp->create2 = val;
  1979. return 0;
  1980. }
  1981. const char *
  1982. extend2_cell_body_check(const extend2_cell_body_t *obj)
  1983. {
  1984. if (obj == NULL)
  1985. return "Object was NULL";
  1986. if (obj->trunnel_error_code_)
  1987. return "A set function failed on this object";
  1988. {
  1989. const char *msg;
  1990. unsigned idx;
  1991. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ls); ++idx) {
  1992. if (NULL != (msg = link_specifier_check(TRUNNEL_DYNARRAY_GET(&obj->ls, idx))))
  1993. return msg;
  1994. }
  1995. }
  1996. if (TRUNNEL_DYNARRAY_LEN(&obj->ls) != obj->n_spec)
  1997. return "Length mismatch for ls";
  1998. {
  1999. const char *msg;
  2000. if (NULL != (msg = create2_cell_body_check(obj->create2)))
  2001. return msg;
  2002. }
  2003. return NULL;
  2004. }
  2005. ssize_t
  2006. extend2_cell_body_encoded_len(const extend2_cell_body_t *obj)
  2007. {
  2008. ssize_t result = 0;
  2009. if (NULL != extend2_cell_body_check(obj))
  2010. return -1;
  2011. /* Length of u8 n_spec */
  2012. result += 1;
  2013. /* Length of struct link_specifier ls[n_spec] */
  2014. {
  2015. unsigned idx;
  2016. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ls); ++idx) {
  2017. result += link_specifier_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->ls, idx));
  2018. }
  2019. }
  2020. /* Length of struct create2_cell_body create2 */
  2021. result += create2_cell_body_encoded_len(obj->create2);
  2022. return result;
  2023. }
  2024. int
  2025. extend2_cell_body_clear_errors(extend2_cell_body_t *obj)
  2026. {
  2027. int r = obj->trunnel_error_code_;
  2028. obj->trunnel_error_code_ = 0;
  2029. return r;
  2030. }
  2031. ssize_t
  2032. extend2_cell_body_encode(uint8_t *output, const size_t avail, const extend2_cell_body_t *obj)
  2033. {
  2034. ssize_t result = 0;
  2035. size_t written = 0;
  2036. uint8_t *ptr = output;
  2037. const char *msg;
  2038. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2039. const ssize_t encoded_len = extend2_cell_body_encoded_len(obj);
  2040. #endif
  2041. if (NULL != (msg = extend2_cell_body_check(obj)))
  2042. goto check_failed;
  2043. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2044. trunnel_assert(encoded_len >= 0);
  2045. #endif
  2046. /* Encode u8 n_spec */
  2047. trunnel_assert(written <= avail);
  2048. if (avail - written < 1)
  2049. goto truncated;
  2050. trunnel_set_uint8(ptr, (obj->n_spec));
  2051. written += 1; ptr += 1;
  2052. /* Encode struct link_specifier ls[n_spec] */
  2053. {
  2054. unsigned idx;
  2055. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ls); ++idx) {
  2056. trunnel_assert(written <= avail);
  2057. result = link_specifier_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->ls, idx));
  2058. if (result < 0)
  2059. goto fail; /* XXXXXXX !*/
  2060. written += result; ptr += result;
  2061. }
  2062. }
  2063. /* Encode struct create2_cell_body create2 */
  2064. trunnel_assert(written <= avail);
  2065. result = create2_cell_body_encode(ptr, avail - written, obj->create2);
  2066. if (result < 0)
  2067. goto fail; /* XXXXXXX !*/
  2068. written += result; ptr += result;
  2069. trunnel_assert(ptr == output + written);
  2070. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2071. {
  2072. trunnel_assert(encoded_len >= 0);
  2073. trunnel_assert((size_t)encoded_len == written);
  2074. }
  2075. #endif
  2076. return written;
  2077. truncated:
  2078. result = -2;
  2079. goto fail;
  2080. check_failed:
  2081. (void)msg;
  2082. result = -1;
  2083. goto fail;
  2084. fail:
  2085. trunnel_assert(result < 0);
  2086. return result;
  2087. }
  2088. /** As extend2_cell_body_parse(), but do not allocate the output
  2089. * object.
  2090. */
  2091. static ssize_t
  2092. extend2_cell_body_parse_into(extend2_cell_body_t *obj, const uint8_t *input, const size_t len_in)
  2093. {
  2094. const uint8_t *ptr = input;
  2095. size_t remaining = len_in;
  2096. ssize_t result = 0;
  2097. (void)result;
  2098. /* Parse u8 n_spec */
  2099. CHECK_REMAINING(1, truncated);
  2100. obj->n_spec = (trunnel_get_uint8(ptr));
  2101. remaining -= 1; ptr += 1;
  2102. /* Parse struct link_specifier ls[n_spec] */
  2103. TRUNNEL_DYNARRAY_EXPAND(link_specifier_t *, &obj->ls, obj->n_spec, {});
  2104. {
  2105. link_specifier_t * elt;
  2106. unsigned idx;
  2107. for (idx = 0; idx < obj->n_spec; ++idx) {
  2108. result = link_specifier_parse(&elt, ptr, remaining);
  2109. if (result < 0)
  2110. goto relay_fail;
  2111. trunnel_assert((size_t)result <= remaining);
  2112. remaining -= result; ptr += result;
  2113. TRUNNEL_DYNARRAY_ADD(link_specifier_t *, &obj->ls, elt, {link_specifier_free(elt);});
  2114. }
  2115. }
  2116. /* Parse struct create2_cell_body create2 */
  2117. result = create2_cell_body_parse(&obj->create2, ptr, remaining);
  2118. if (result < 0)
  2119. goto relay_fail;
  2120. trunnel_assert((size_t)result <= remaining);
  2121. remaining -= result; ptr += result;
  2122. trunnel_assert(ptr + remaining == input + len_in);
  2123. return len_in - remaining;
  2124. truncated:
  2125. return -2;
  2126. relay_fail:
  2127. trunnel_assert(result < 0);
  2128. return result;
  2129. trunnel_alloc_failed:
  2130. return -1;
  2131. }
  2132. ssize_t
  2133. extend2_cell_body_parse(extend2_cell_body_t **output, const uint8_t *input, const size_t len_in)
  2134. {
  2135. ssize_t result;
  2136. *output = extend2_cell_body_new();
  2137. if (NULL == *output)
  2138. return -1;
  2139. result = extend2_cell_body_parse_into(*output, input, len_in);
  2140. if (result < 0) {
  2141. extend2_cell_body_free(*output);
  2142. *output = NULL;
  2143. }
  2144. return result;
  2145. }
  2146. link_specifier_list_t *
  2147. link_specifier_list_new(void)
  2148. {
  2149. link_specifier_list_t *val = trunnel_calloc(1, sizeof(link_specifier_list_t));
  2150. if (NULL == val)
  2151. return NULL;
  2152. return val;
  2153. }
  2154. /** Release all storage held inside 'obj', but do not free 'obj'.
  2155. */
  2156. static void
  2157. link_specifier_list_clear(link_specifier_list_t *obj)
  2158. {
  2159. (void) obj;
  2160. {
  2161. unsigned idx;
  2162. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  2163. link_specifier_free(TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  2164. }
  2165. }
  2166. TRUNNEL_DYNARRAY_WIPE(&obj->spec);
  2167. TRUNNEL_DYNARRAY_CLEAR(&obj->spec);
  2168. }
  2169. void
  2170. link_specifier_list_free(link_specifier_list_t *obj)
  2171. {
  2172. if (obj == NULL)
  2173. return;
  2174. link_specifier_list_clear(obj);
  2175. trunnel_memwipe(obj, sizeof(link_specifier_list_t));
  2176. trunnel_free_(obj);
  2177. }
  2178. uint8_t
  2179. link_specifier_list_get_n_spec(const link_specifier_list_t *inp)
  2180. {
  2181. return inp->n_spec;
  2182. }
  2183. int
  2184. link_specifier_list_set_n_spec(link_specifier_list_t *inp, uint8_t val)
  2185. {
  2186. inp->n_spec = val;
  2187. return 0;
  2188. }
  2189. size_t
  2190. link_specifier_list_getlen_spec(const link_specifier_list_t *inp)
  2191. {
  2192. return TRUNNEL_DYNARRAY_LEN(&inp->spec);
  2193. }
  2194. struct link_specifier_st *
  2195. link_specifier_list_get_spec(link_specifier_list_t *inp, size_t idx)
  2196. {
  2197. return TRUNNEL_DYNARRAY_GET(&inp->spec, idx);
  2198. }
  2199. const struct link_specifier_st *
  2200. link_specifier_list_getconst_spec(const link_specifier_list_t *inp, size_t idx)
  2201. {
  2202. return link_specifier_list_get_spec((link_specifier_list_t*)inp, idx);
  2203. }
  2204. int
  2205. link_specifier_list_set_spec(link_specifier_list_t *inp, size_t idx, struct link_specifier_st * elt)
  2206. {
  2207. link_specifier_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->spec, idx);
  2208. if (oldval && oldval != elt)
  2209. link_specifier_free(oldval);
  2210. return link_specifier_list_set0_spec(inp, idx, elt);
  2211. }
  2212. int
  2213. link_specifier_list_set0_spec(link_specifier_list_t *inp, size_t idx, struct link_specifier_st * elt)
  2214. {
  2215. TRUNNEL_DYNARRAY_SET(&inp->spec, idx, elt);
  2216. return 0;
  2217. }
  2218. int
  2219. link_specifier_list_add_spec(link_specifier_list_t *inp, struct link_specifier_st * elt)
  2220. {
  2221. #if SIZE_MAX >= UINT8_MAX
  2222. if (inp->spec.n_ == UINT8_MAX)
  2223. goto trunnel_alloc_failed;
  2224. #endif
  2225. TRUNNEL_DYNARRAY_ADD(struct link_specifier_st *, &inp->spec, elt, {});
  2226. return 0;
  2227. trunnel_alloc_failed:
  2228. TRUNNEL_SET_ERROR_CODE(inp);
  2229. return -1;
  2230. }
  2231. struct link_specifier_st * *
  2232. link_specifier_list_getarray_spec(link_specifier_list_t *inp)
  2233. {
  2234. return inp->spec.elts_;
  2235. }
  2236. const struct link_specifier_st * const *
  2237. link_specifier_list_getconstarray_spec(const link_specifier_list_t *inp)
  2238. {
  2239. return (const struct link_specifier_st * const *)link_specifier_list_getarray_spec((link_specifier_list_t*)inp);
  2240. }
  2241. int
  2242. link_specifier_list_setlen_spec(link_specifier_list_t *inp, size_t newlen)
  2243. {
  2244. struct link_specifier_st * *newptr;
  2245. #if UINT8_MAX < SIZE_MAX
  2246. if (newlen > UINT8_MAX)
  2247. goto trunnel_alloc_failed;
  2248. #endif
  2249. newptr = trunnel_dynarray_setlen(&inp->spec.allocated_,
  2250. &inp->spec.n_, inp->spec.elts_, newlen,
  2251. sizeof(inp->spec.elts_[0]), (trunnel_free_fn_t) link_specifier_free,
  2252. &inp->trunnel_error_code_);
  2253. if (newlen != 0 && newptr == NULL)
  2254. goto trunnel_alloc_failed;
  2255. inp->spec.elts_ = newptr;
  2256. return 0;
  2257. trunnel_alloc_failed:
  2258. TRUNNEL_SET_ERROR_CODE(inp);
  2259. return -1;
  2260. }
  2261. const char *
  2262. link_specifier_list_check(const link_specifier_list_t *obj)
  2263. {
  2264. if (obj == NULL)
  2265. return "Object was NULL";
  2266. if (obj->trunnel_error_code_)
  2267. return "A set function failed on this object";
  2268. {
  2269. const char *msg;
  2270. unsigned idx;
  2271. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  2272. if (NULL != (msg = link_specifier_check(TRUNNEL_DYNARRAY_GET(&obj->spec, idx))))
  2273. return msg;
  2274. }
  2275. }
  2276. if (TRUNNEL_DYNARRAY_LEN(&obj->spec) != obj->n_spec)
  2277. return "Length mismatch for spec";
  2278. return NULL;
  2279. }
  2280. ssize_t
  2281. link_specifier_list_encoded_len(const link_specifier_list_t *obj)
  2282. {
  2283. ssize_t result = 0;
  2284. if (NULL != link_specifier_list_check(obj))
  2285. return -1;
  2286. /* Length of u8 n_spec */
  2287. result += 1;
  2288. /* Length of struct link_specifier spec[n_spec] */
  2289. {
  2290. unsigned idx;
  2291. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  2292. result += link_specifier_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  2293. }
  2294. }
  2295. return result;
  2296. }
  2297. int
  2298. link_specifier_list_clear_errors(link_specifier_list_t *obj)
  2299. {
  2300. int r = obj->trunnel_error_code_;
  2301. obj->trunnel_error_code_ = 0;
  2302. return r;
  2303. }
  2304. ssize_t
  2305. link_specifier_list_encode(uint8_t *output, const size_t avail, const link_specifier_list_t *obj)
  2306. {
  2307. ssize_t result = 0;
  2308. size_t written = 0;
  2309. uint8_t *ptr = output;
  2310. const char *msg;
  2311. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2312. const ssize_t encoded_len = link_specifier_list_encoded_len(obj);
  2313. #endif
  2314. if (NULL != (msg = link_specifier_list_check(obj)))
  2315. goto check_failed;
  2316. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2317. trunnel_assert(encoded_len >= 0);
  2318. #endif
  2319. /* Encode u8 n_spec */
  2320. trunnel_assert(written <= avail);
  2321. if (avail - written < 1)
  2322. goto truncated;
  2323. trunnel_set_uint8(ptr, (obj->n_spec));
  2324. written += 1; ptr += 1;
  2325. /* Encode struct link_specifier spec[n_spec] */
  2326. {
  2327. unsigned idx;
  2328. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  2329. trunnel_assert(written <= avail);
  2330. result = link_specifier_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  2331. if (result < 0)
  2332. goto fail; /* XXXXXXX !*/
  2333. written += result; ptr += result;
  2334. }
  2335. }
  2336. trunnel_assert(ptr == output + written);
  2337. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2338. {
  2339. trunnel_assert(encoded_len >= 0);
  2340. trunnel_assert((size_t)encoded_len == written);
  2341. }
  2342. #endif
  2343. return written;
  2344. truncated:
  2345. result = -2;
  2346. goto fail;
  2347. check_failed:
  2348. (void)msg;
  2349. result = -1;
  2350. goto fail;
  2351. fail:
  2352. trunnel_assert(result < 0);
  2353. return result;
  2354. }
  2355. /** As link_specifier_list_parse(), but do not allocate the output
  2356. * object.
  2357. */
  2358. static ssize_t
  2359. link_specifier_list_parse_into(link_specifier_list_t *obj, const uint8_t *input, const size_t len_in)
  2360. {
  2361. const uint8_t *ptr = input;
  2362. size_t remaining = len_in;
  2363. ssize_t result = 0;
  2364. (void)result;
  2365. /* Parse u8 n_spec */
  2366. CHECK_REMAINING(1, truncated);
  2367. obj->n_spec = (trunnel_get_uint8(ptr));
  2368. remaining -= 1; ptr += 1;
  2369. /* Parse struct link_specifier spec[n_spec] */
  2370. TRUNNEL_DYNARRAY_EXPAND(link_specifier_t *, &obj->spec, obj->n_spec, {});
  2371. {
  2372. link_specifier_t * elt;
  2373. unsigned idx;
  2374. for (idx = 0; idx < obj->n_spec; ++idx) {
  2375. result = link_specifier_parse(&elt, ptr, remaining);
  2376. if (result < 0)
  2377. goto relay_fail;
  2378. trunnel_assert((size_t)result <= remaining);
  2379. remaining -= result; ptr += result;
  2380. TRUNNEL_DYNARRAY_ADD(link_specifier_t *, &obj->spec, elt, {link_specifier_free(elt);});
  2381. }
  2382. }
  2383. trunnel_assert(ptr + remaining == input + len_in);
  2384. return len_in - remaining;
  2385. truncated:
  2386. return -2;
  2387. relay_fail:
  2388. trunnel_assert(result < 0);
  2389. return result;
  2390. trunnel_alloc_failed:
  2391. return -1;
  2392. }
  2393. ssize_t
  2394. link_specifier_list_parse(link_specifier_list_t **output, const uint8_t *input, const size_t len_in)
  2395. {
  2396. ssize_t result;
  2397. *output = link_specifier_list_new();
  2398. if (NULL == *output)
  2399. return -1;
  2400. result = link_specifier_list_parse_into(*output, input, len_in);
  2401. if (result < 0) {
  2402. link_specifier_list_free(*output);
  2403. *output = NULL;
  2404. }
  2405. return result;
  2406. }