link_handshake.c 44 KB

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