ed25519_cert.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. /* ed25519_cert.c -- generated by Trunnel v1.5.
  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 runnning a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int 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. ed25519_cert_extension_t *
  27. ed25519_cert_extension_new(void)
  28. {
  29. ed25519_cert_extension_t *val = trunnel_calloc(1, sizeof(ed25519_cert_extension_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. ed25519_cert_extension_clear(ed25519_cert_extension_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->un_unparsed);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->un_unparsed);
  42. }
  43. void
  44. ed25519_cert_extension_free(ed25519_cert_extension_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. ed25519_cert_extension_clear(obj);
  49. trunnel_memwipe(obj, sizeof(ed25519_cert_extension_t));
  50. trunnel_free_(obj);
  51. }
  52. uint16_t
  53. ed25519_cert_extension_get_ext_length(ed25519_cert_extension_t *inp)
  54. {
  55. return inp->ext_length;
  56. }
  57. int
  58. ed25519_cert_extension_set_ext_length(ed25519_cert_extension_t *inp, uint16_t val)
  59. {
  60. inp->ext_length = val;
  61. return 0;
  62. }
  63. uint8_t
  64. ed25519_cert_extension_get_ext_type(ed25519_cert_extension_t *inp)
  65. {
  66. return inp->ext_type;
  67. }
  68. int
  69. ed25519_cert_extension_set_ext_type(ed25519_cert_extension_t *inp, uint8_t val)
  70. {
  71. inp->ext_type = val;
  72. return 0;
  73. }
  74. uint8_t
  75. ed25519_cert_extension_get_ext_flags(ed25519_cert_extension_t *inp)
  76. {
  77. return inp->ext_flags;
  78. }
  79. int
  80. ed25519_cert_extension_set_ext_flags(ed25519_cert_extension_t *inp, uint8_t val)
  81. {
  82. inp->ext_flags = val;
  83. return 0;
  84. }
  85. size_t
  86. ed25519_cert_extension_getlen_un_signing_key(const ed25519_cert_extension_t *inp)
  87. {
  88. (void)inp; return 32;
  89. }
  90. uint8_t
  91. ed25519_cert_extension_get_un_signing_key(ed25519_cert_extension_t *inp, size_t idx)
  92. {
  93. trunnel_assert(idx < 32);
  94. return inp->un_signing_key[idx];
  95. }
  96. uint8_t
  97. ed25519_cert_extension_getconst_un_signing_key(const ed25519_cert_extension_t *inp, size_t idx)
  98. {
  99. return ed25519_cert_extension_get_un_signing_key((ed25519_cert_extension_t*)inp, idx);
  100. }
  101. int
  102. ed25519_cert_extension_set_un_signing_key(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  103. {
  104. trunnel_assert(idx < 32);
  105. inp->un_signing_key[idx] = elt;
  106. return 0;
  107. }
  108. uint8_t *
  109. ed25519_cert_extension_getarray_un_signing_key(ed25519_cert_extension_t *inp)
  110. {
  111. return inp->un_signing_key;
  112. }
  113. const uint8_t *
  114. ed25519_cert_extension_getconstarray_un_signing_key(const ed25519_cert_extension_t *inp)
  115. {
  116. return (const uint8_t *)ed25519_cert_extension_getarray_un_signing_key((ed25519_cert_extension_t*)inp);
  117. }
  118. size_t
  119. ed25519_cert_extension_getlen_un_unparsed(const ed25519_cert_extension_t *inp)
  120. {
  121. return TRUNNEL_DYNARRAY_LEN(&inp->un_unparsed);
  122. }
  123. uint8_t
  124. ed25519_cert_extension_get_un_unparsed(ed25519_cert_extension_t *inp, size_t idx)
  125. {
  126. return TRUNNEL_DYNARRAY_GET(&inp->un_unparsed, idx);
  127. }
  128. uint8_t
  129. ed25519_cert_extension_getconst_un_unparsed(const ed25519_cert_extension_t *inp, size_t idx)
  130. {
  131. return ed25519_cert_extension_get_un_unparsed((ed25519_cert_extension_t*)inp, idx);
  132. }
  133. int
  134. ed25519_cert_extension_set_un_unparsed(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  135. {
  136. TRUNNEL_DYNARRAY_SET(&inp->un_unparsed, idx, elt);
  137. return 0;
  138. }
  139. int
  140. ed25519_cert_extension_add_un_unparsed(ed25519_cert_extension_t *inp, uint8_t elt)
  141. {
  142. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->un_unparsed, elt, {});
  143. return 0;
  144. trunnel_alloc_failed:
  145. TRUNNEL_SET_ERROR_CODE(inp);
  146. return -1;
  147. }
  148. uint8_t *
  149. ed25519_cert_extension_getarray_un_unparsed(ed25519_cert_extension_t *inp)
  150. {
  151. return inp->un_unparsed.elts_;
  152. }
  153. const uint8_t *
  154. ed25519_cert_extension_getconstarray_un_unparsed(const ed25519_cert_extension_t *inp)
  155. {
  156. return (const uint8_t *)ed25519_cert_extension_getarray_un_unparsed((ed25519_cert_extension_t*)inp);
  157. }
  158. int
  159. ed25519_cert_extension_setlen_un_unparsed(ed25519_cert_extension_t *inp, size_t newlen)
  160. {
  161. uint8_t *newptr;
  162. newptr = trunnel_dynarray_setlen(&inp->un_unparsed.allocated_,
  163. &inp->un_unparsed.n_, inp->un_unparsed.elts_, newlen,
  164. sizeof(inp->un_unparsed.elts_[0]), (trunnel_free_fn_t) NULL,
  165. &inp->trunnel_error_code_);
  166. if (newlen != 0 && newptr == NULL)
  167. goto trunnel_alloc_failed;
  168. inp->un_unparsed.elts_ = newptr;
  169. return 0;
  170. trunnel_alloc_failed:
  171. TRUNNEL_SET_ERROR_CODE(inp);
  172. return -1;
  173. }
  174. const char *
  175. ed25519_cert_extension_check(const ed25519_cert_extension_t *obj)
  176. {
  177. if (obj == NULL)
  178. return "Object was NULL";
  179. if (obj->trunnel_error_code_)
  180. return "A set function failed on this object";
  181. switch (obj->ext_type) {
  182. case CERTEXT_SIGNED_WITH_KEY:
  183. break;
  184. default:
  185. break;
  186. }
  187. return NULL;
  188. }
  189. ssize_t
  190. ed25519_cert_extension_encoded_len(const ed25519_cert_extension_t *obj)
  191. {
  192. ssize_t result = 0;
  193. if (NULL != ed25519_cert_extension_check(obj))
  194. return -1;
  195. /* Length of u16 ext_length */
  196. result += 2;
  197. /* Length of u8 ext_type */
  198. result += 1;
  199. /* Length of u8 ext_flags */
  200. result += 1;
  201. switch (obj->ext_type) {
  202. case CERTEXT_SIGNED_WITH_KEY:
  203. /* Length of u8 un_signing_key[32] */
  204. result += 32;
  205. break;
  206. default:
  207. /* Length of u8 un_unparsed[] */
  208. result += TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  209. break;
  210. }
  211. return result;
  212. }
  213. int
  214. ed25519_cert_extension_clear_errors(ed25519_cert_extension_t *obj)
  215. {
  216. int r = obj->trunnel_error_code_;
  217. obj->trunnel_error_code_ = 0;
  218. return r;
  219. }
  220. ssize_t
  221. ed25519_cert_extension_encode(uint8_t *output, const size_t avail, const ed25519_cert_extension_t *obj)
  222. {
  223. ssize_t result = 0;
  224. size_t written = 0;
  225. uint8_t *ptr = output;
  226. const char *msg;
  227. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  228. const ssize_t encoded_len = ed25519_cert_extension_encoded_len(obj);
  229. #endif
  230. uint8_t *backptr_ext_length = NULL;
  231. if (NULL != (msg = ed25519_cert_extension_check(obj)))
  232. goto check_failed;
  233. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  234. trunnel_assert(encoded_len >= 0);
  235. #endif
  236. /* Encode u16 ext_length */
  237. backptr_ext_length = ptr;
  238. trunnel_assert(written <= avail);
  239. if (avail - written < 2)
  240. goto truncated;
  241. trunnel_set_uint16(ptr, trunnel_htons(obj->ext_length));
  242. written += 2; ptr += 2;
  243. /* Encode u8 ext_type */
  244. trunnel_assert(written <= avail);
  245. if (avail - written < 1)
  246. goto truncated;
  247. trunnel_set_uint8(ptr, (obj->ext_type));
  248. written += 1; ptr += 1;
  249. /* Encode u8 ext_flags */
  250. trunnel_assert(written <= avail);
  251. if (avail - written < 1)
  252. goto truncated;
  253. trunnel_set_uint8(ptr, (obj->ext_flags));
  254. written += 1; ptr += 1;
  255. {
  256. size_t written_before_union = written;
  257. /* Encode union un[ext_type] */
  258. trunnel_assert(written <= avail);
  259. switch (obj->ext_type) {
  260. case CERTEXT_SIGNED_WITH_KEY:
  261. /* Encode u8 un_signing_key[32] */
  262. trunnel_assert(written <= avail);
  263. if (avail - written < 32)
  264. goto truncated;
  265. memcpy(ptr, obj->un_signing_key, 32);
  266. written += 32; ptr += 32;
  267. break;
  268. default:
  269. /* Encode u8 un_unparsed[] */
  270. {
  271. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  272. trunnel_assert(written <= avail);
  273. if (avail - written < elt_len)
  274. goto truncated;
  275. if (elt_len)
  276. memcpy(ptr, obj->un_unparsed.elts_, elt_len);
  277. written += elt_len; ptr += elt_len;
  278. }
  279. break;
  280. }
  281. /* Write the length field back to ext_length */
  282. trunnel_assert(written >= written_before_union);
  283. #if UINT16_MAX < SIZE_MAX
  284. if (written - written_before_union > UINT16_MAX)
  285. goto check_failed;
  286. #endif
  287. trunnel_set_uint16(backptr_ext_length, trunnel_htons(written - written_before_union));
  288. }
  289. trunnel_assert(ptr == output + written);
  290. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  291. {
  292. trunnel_assert(encoded_len >= 0);
  293. trunnel_assert((size_t)encoded_len == written);
  294. }
  295. #endif
  296. return written;
  297. truncated:
  298. result = -2;
  299. goto fail;
  300. check_failed:
  301. (void)msg;
  302. result = -1;
  303. goto fail;
  304. fail:
  305. trunnel_assert(result < 0);
  306. return result;
  307. }
  308. /** As ed25519_cert_extension_parse(), but do not allocate the output
  309. * object.
  310. */
  311. static ssize_t
  312. ed25519_cert_extension_parse_into(ed25519_cert_extension_t *obj, const uint8_t *input, const size_t len_in)
  313. {
  314. const uint8_t *ptr = input;
  315. size_t remaining = len_in;
  316. ssize_t result = 0;
  317. (void)result;
  318. /* Parse u16 ext_length */
  319. CHECK_REMAINING(2, truncated);
  320. obj->ext_length = trunnel_ntohs(trunnel_get_uint16(ptr));
  321. remaining -= 2; ptr += 2;
  322. /* Parse u8 ext_type */
  323. CHECK_REMAINING(1, truncated);
  324. obj->ext_type = (trunnel_get_uint8(ptr));
  325. remaining -= 1; ptr += 1;
  326. /* Parse u8 ext_flags */
  327. CHECK_REMAINING(1, truncated);
  328. obj->ext_flags = (trunnel_get_uint8(ptr));
  329. remaining -= 1; ptr += 1;
  330. {
  331. size_t remaining_after;
  332. CHECK_REMAINING(obj->ext_length, truncated);
  333. remaining_after = remaining - obj->ext_length;
  334. remaining = obj->ext_length;
  335. /* Parse union un[ext_type] */
  336. switch (obj->ext_type) {
  337. case CERTEXT_SIGNED_WITH_KEY:
  338. /* Parse u8 un_signing_key[32] */
  339. CHECK_REMAINING(32, fail);
  340. memcpy(obj->un_signing_key, ptr, 32);
  341. remaining -= 32; ptr += 32;
  342. break;
  343. default:
  344. /* Parse u8 un_unparsed[] */
  345. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unparsed, remaining, {});
  346. obj->un_unparsed.n_ = remaining;
  347. if (remaining)
  348. memcpy(obj->un_unparsed.elts_, ptr, remaining);
  349. ptr += remaining; remaining -= remaining;
  350. break;
  351. }
  352. if (remaining != 0)
  353. goto fail;
  354. remaining = remaining_after;
  355. }
  356. trunnel_assert(ptr + remaining == input + len_in);
  357. return len_in - remaining;
  358. truncated:
  359. return -2;
  360. trunnel_alloc_failed:
  361. return -1;
  362. fail:
  363. result = -1;
  364. return result;
  365. }
  366. ssize_t
  367. ed25519_cert_extension_parse(ed25519_cert_extension_t **output, const uint8_t *input, const size_t len_in)
  368. {
  369. ssize_t result;
  370. *output = ed25519_cert_extension_new();
  371. if (NULL == *output)
  372. return -1;
  373. result = ed25519_cert_extension_parse_into(*output, input, len_in);
  374. if (result < 0) {
  375. ed25519_cert_extension_free(*output);
  376. *output = NULL;
  377. }
  378. return result;
  379. }
  380. link_specifier_t *
  381. link_specifier_new(void)
  382. {
  383. link_specifier_t *val = trunnel_calloc(1, sizeof(link_specifier_t));
  384. if (NULL == val)
  385. return NULL;
  386. return val;
  387. }
  388. /** Release all storage held inside 'obj', but do not free 'obj'.
  389. */
  390. static void
  391. link_specifier_clear(link_specifier_t *obj)
  392. {
  393. (void) obj;
  394. TRUNNEL_DYNARRAY_WIPE(&obj->un_unrecognized);
  395. TRUNNEL_DYNARRAY_CLEAR(&obj->un_unrecognized);
  396. }
  397. void
  398. link_specifier_free(link_specifier_t *obj)
  399. {
  400. if (obj == NULL)
  401. return;
  402. link_specifier_clear(obj);
  403. trunnel_memwipe(obj, sizeof(link_specifier_t));
  404. trunnel_free_(obj);
  405. }
  406. uint8_t
  407. link_specifier_get_ls_type(link_specifier_t *inp)
  408. {
  409. return inp->ls_type;
  410. }
  411. int
  412. link_specifier_set_ls_type(link_specifier_t *inp, uint8_t val)
  413. {
  414. inp->ls_type = val;
  415. return 0;
  416. }
  417. uint8_t
  418. link_specifier_get_ls_len(link_specifier_t *inp)
  419. {
  420. return inp->ls_len;
  421. }
  422. int
  423. link_specifier_set_ls_len(link_specifier_t *inp, uint8_t val)
  424. {
  425. inp->ls_len = val;
  426. return 0;
  427. }
  428. uint32_t
  429. link_specifier_get_un_ipv4_addr(link_specifier_t *inp)
  430. {
  431. return inp->un_ipv4_addr;
  432. }
  433. int
  434. link_specifier_set_un_ipv4_addr(link_specifier_t *inp, uint32_t val)
  435. {
  436. inp->un_ipv4_addr = val;
  437. return 0;
  438. }
  439. uint16_t
  440. link_specifier_get_un_ipv4_port(link_specifier_t *inp)
  441. {
  442. return inp->un_ipv4_port;
  443. }
  444. int
  445. link_specifier_set_un_ipv4_port(link_specifier_t *inp, uint16_t val)
  446. {
  447. inp->un_ipv4_port = val;
  448. return 0;
  449. }
  450. size_t
  451. link_specifier_getlen_un_ipv6_addr(const link_specifier_t *inp)
  452. {
  453. (void)inp; return 16;
  454. }
  455. uint8_t
  456. link_specifier_get_un_ipv6_addr(link_specifier_t *inp, size_t idx)
  457. {
  458. trunnel_assert(idx < 16);
  459. return inp->un_ipv6_addr[idx];
  460. }
  461. uint8_t
  462. link_specifier_getconst_un_ipv6_addr(const link_specifier_t *inp, size_t idx)
  463. {
  464. return link_specifier_get_un_ipv6_addr((link_specifier_t*)inp, idx);
  465. }
  466. int
  467. link_specifier_set_un_ipv6_addr(link_specifier_t *inp, size_t idx, uint8_t elt)
  468. {
  469. trunnel_assert(idx < 16);
  470. inp->un_ipv6_addr[idx] = elt;
  471. return 0;
  472. }
  473. uint8_t *
  474. link_specifier_getarray_un_ipv6_addr(link_specifier_t *inp)
  475. {
  476. return inp->un_ipv6_addr;
  477. }
  478. const uint8_t *
  479. link_specifier_getconstarray_un_ipv6_addr(const link_specifier_t *inp)
  480. {
  481. return (const uint8_t *)link_specifier_getarray_un_ipv6_addr((link_specifier_t*)inp);
  482. }
  483. uint16_t
  484. link_specifier_get_un_ipv6_port(link_specifier_t *inp)
  485. {
  486. return inp->un_ipv6_port;
  487. }
  488. int
  489. link_specifier_set_un_ipv6_port(link_specifier_t *inp, uint16_t val)
  490. {
  491. inp->un_ipv6_port = val;
  492. return 0;
  493. }
  494. size_t
  495. link_specifier_getlen_un_legacy_id(const link_specifier_t *inp)
  496. {
  497. (void)inp; return 20;
  498. }
  499. uint8_t
  500. link_specifier_get_un_legacy_id(link_specifier_t *inp, size_t idx)
  501. {
  502. trunnel_assert(idx < 20);
  503. return inp->un_legacy_id[idx];
  504. }
  505. uint8_t
  506. link_specifier_getconst_un_legacy_id(const link_specifier_t *inp, size_t idx)
  507. {
  508. return link_specifier_get_un_legacy_id((link_specifier_t*)inp, idx);
  509. }
  510. int
  511. link_specifier_set_un_legacy_id(link_specifier_t *inp, size_t idx, uint8_t elt)
  512. {
  513. trunnel_assert(idx < 20);
  514. inp->un_legacy_id[idx] = elt;
  515. return 0;
  516. }
  517. uint8_t *
  518. link_specifier_getarray_un_legacy_id(link_specifier_t *inp)
  519. {
  520. return inp->un_legacy_id;
  521. }
  522. const uint8_t *
  523. link_specifier_getconstarray_un_legacy_id(const link_specifier_t *inp)
  524. {
  525. return (const uint8_t *)link_specifier_getarray_un_legacy_id((link_specifier_t*)inp);
  526. }
  527. size_t
  528. link_specifier_getlen_un_ed25519_id(const link_specifier_t *inp)
  529. {
  530. (void)inp; return 32;
  531. }
  532. uint8_t
  533. link_specifier_get_un_ed25519_id(link_specifier_t *inp, size_t idx)
  534. {
  535. trunnel_assert(idx < 32);
  536. return inp->un_ed25519_id[idx];
  537. }
  538. uint8_t
  539. link_specifier_getconst_un_ed25519_id(const link_specifier_t *inp, size_t idx)
  540. {
  541. return link_specifier_get_un_ed25519_id((link_specifier_t*)inp, idx);
  542. }
  543. int
  544. link_specifier_set_un_ed25519_id(link_specifier_t *inp, size_t idx, uint8_t elt)
  545. {
  546. trunnel_assert(idx < 32);
  547. inp->un_ed25519_id[idx] = elt;
  548. return 0;
  549. }
  550. uint8_t *
  551. link_specifier_getarray_un_ed25519_id(link_specifier_t *inp)
  552. {
  553. return inp->un_ed25519_id;
  554. }
  555. const uint8_t *
  556. link_specifier_getconstarray_un_ed25519_id(const link_specifier_t *inp)
  557. {
  558. return (const uint8_t *)link_specifier_getarray_un_ed25519_id((link_specifier_t*)inp);
  559. }
  560. size_t
  561. link_specifier_getlen_un_unrecognized(const link_specifier_t *inp)
  562. {
  563. return TRUNNEL_DYNARRAY_LEN(&inp->un_unrecognized);
  564. }
  565. uint8_t
  566. link_specifier_get_un_unrecognized(link_specifier_t *inp, size_t idx)
  567. {
  568. return TRUNNEL_DYNARRAY_GET(&inp->un_unrecognized, idx);
  569. }
  570. uint8_t
  571. link_specifier_getconst_un_unrecognized(const link_specifier_t *inp, size_t idx)
  572. {
  573. return link_specifier_get_un_unrecognized((link_specifier_t*)inp, idx);
  574. }
  575. int
  576. link_specifier_set_un_unrecognized(link_specifier_t *inp, size_t idx, uint8_t elt)
  577. {
  578. TRUNNEL_DYNARRAY_SET(&inp->un_unrecognized, idx, elt);
  579. return 0;
  580. }
  581. int
  582. link_specifier_add_un_unrecognized(link_specifier_t *inp, uint8_t elt)
  583. {
  584. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->un_unrecognized, elt, {});
  585. return 0;
  586. trunnel_alloc_failed:
  587. TRUNNEL_SET_ERROR_CODE(inp);
  588. return -1;
  589. }
  590. uint8_t *
  591. link_specifier_getarray_un_unrecognized(link_specifier_t *inp)
  592. {
  593. return inp->un_unrecognized.elts_;
  594. }
  595. const uint8_t *
  596. link_specifier_getconstarray_un_unrecognized(const link_specifier_t *inp)
  597. {
  598. return (const uint8_t *)link_specifier_getarray_un_unrecognized((link_specifier_t*)inp);
  599. }
  600. int
  601. link_specifier_setlen_un_unrecognized(link_specifier_t *inp, size_t newlen)
  602. {
  603. uint8_t *newptr;
  604. newptr = trunnel_dynarray_setlen(&inp->un_unrecognized.allocated_,
  605. &inp->un_unrecognized.n_, inp->un_unrecognized.elts_, newlen,
  606. sizeof(inp->un_unrecognized.elts_[0]), (trunnel_free_fn_t) NULL,
  607. &inp->trunnel_error_code_);
  608. if (newlen != 0 && newptr == NULL)
  609. goto trunnel_alloc_failed;
  610. inp->un_unrecognized.elts_ = newptr;
  611. return 0;
  612. trunnel_alloc_failed:
  613. TRUNNEL_SET_ERROR_CODE(inp);
  614. return -1;
  615. }
  616. const char *
  617. link_specifier_check(const link_specifier_t *obj)
  618. {
  619. if (obj == NULL)
  620. return "Object was NULL";
  621. if (obj->trunnel_error_code_)
  622. return "A set function failed on this object";
  623. switch (obj->ls_type) {
  624. case LS_IPV4:
  625. break;
  626. case LS_IPV6:
  627. break;
  628. case LS_LEGACY_ID:
  629. break;
  630. case LS_ED25519_ID:
  631. break;
  632. default:
  633. break;
  634. }
  635. return NULL;
  636. }
  637. ssize_t
  638. link_specifier_encoded_len(const link_specifier_t *obj)
  639. {
  640. ssize_t result = 0;
  641. if (NULL != link_specifier_check(obj))
  642. return -1;
  643. /* Length of u8 ls_type */
  644. result += 1;
  645. /* Length of u8 ls_len */
  646. result += 1;
  647. switch (obj->ls_type) {
  648. case LS_IPV4:
  649. /* Length of u32 un_ipv4_addr */
  650. result += 4;
  651. /* Length of u16 un_ipv4_port */
  652. result += 2;
  653. break;
  654. case LS_IPV6:
  655. /* Length of u8 un_ipv6_addr[16] */
  656. result += 16;
  657. /* Length of u16 un_ipv6_port */
  658. result += 2;
  659. break;
  660. case LS_LEGACY_ID:
  661. /* Length of u8 un_legacy_id[20] */
  662. result += 20;
  663. break;
  664. case LS_ED25519_ID:
  665. /* Length of u8 un_ed25519_id[32] */
  666. result += 32;
  667. break;
  668. default:
  669. /* Length of u8 un_unrecognized[] */
  670. result += TRUNNEL_DYNARRAY_LEN(&obj->un_unrecognized);
  671. break;
  672. }
  673. return result;
  674. }
  675. int
  676. link_specifier_clear_errors(link_specifier_t *obj)
  677. {
  678. int r = obj->trunnel_error_code_;
  679. obj->trunnel_error_code_ = 0;
  680. return r;
  681. }
  682. ssize_t
  683. link_specifier_encode(uint8_t *output, const size_t avail, const link_specifier_t *obj)
  684. {
  685. ssize_t result = 0;
  686. size_t written = 0;
  687. uint8_t *ptr = output;
  688. const char *msg;
  689. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  690. const ssize_t encoded_len = link_specifier_encoded_len(obj);
  691. #endif
  692. uint8_t *backptr_ls_len = NULL;
  693. if (NULL != (msg = link_specifier_check(obj)))
  694. goto check_failed;
  695. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  696. trunnel_assert(encoded_len >= 0);
  697. #endif
  698. /* Encode u8 ls_type */
  699. trunnel_assert(written <= avail);
  700. if (avail - written < 1)
  701. goto truncated;
  702. trunnel_set_uint8(ptr, (obj->ls_type));
  703. written += 1; ptr += 1;
  704. /* Encode u8 ls_len */
  705. backptr_ls_len = ptr;
  706. trunnel_assert(written <= avail);
  707. if (avail - written < 1)
  708. goto truncated;
  709. trunnel_set_uint8(ptr, (obj->ls_len));
  710. written += 1; ptr += 1;
  711. {
  712. size_t written_before_union = written;
  713. /* Encode union un[ls_type] */
  714. trunnel_assert(written <= avail);
  715. switch (obj->ls_type) {
  716. case LS_IPV4:
  717. /* Encode u32 un_ipv4_addr */
  718. trunnel_assert(written <= avail);
  719. if (avail - written < 4)
  720. goto truncated;
  721. trunnel_set_uint32(ptr, trunnel_htonl(obj->un_ipv4_addr));
  722. written += 4; ptr += 4;
  723. /* Encode u16 un_ipv4_port */
  724. trunnel_assert(written <= avail);
  725. if (avail - written < 2)
  726. goto truncated;
  727. trunnel_set_uint16(ptr, trunnel_htons(obj->un_ipv4_port));
  728. written += 2; ptr += 2;
  729. break;
  730. case LS_IPV6:
  731. /* Encode u8 un_ipv6_addr[16] */
  732. trunnel_assert(written <= avail);
  733. if (avail - written < 16)
  734. goto truncated;
  735. memcpy(ptr, obj->un_ipv6_addr, 16);
  736. written += 16; ptr += 16;
  737. /* Encode u16 un_ipv6_port */
  738. trunnel_assert(written <= avail);
  739. if (avail - written < 2)
  740. goto truncated;
  741. trunnel_set_uint16(ptr, trunnel_htons(obj->un_ipv6_port));
  742. written += 2; ptr += 2;
  743. break;
  744. case LS_LEGACY_ID:
  745. /* Encode u8 un_legacy_id[20] */
  746. trunnel_assert(written <= avail);
  747. if (avail - written < 20)
  748. goto truncated;
  749. memcpy(ptr, obj->un_legacy_id, 20);
  750. written += 20; ptr += 20;
  751. break;
  752. case LS_ED25519_ID:
  753. /* Encode u8 un_ed25519_id[32] */
  754. trunnel_assert(written <= avail);
  755. if (avail - written < 32)
  756. goto truncated;
  757. memcpy(ptr, obj->un_ed25519_id, 32);
  758. written += 32; ptr += 32;
  759. break;
  760. default:
  761. /* Encode u8 un_unrecognized[] */
  762. {
  763. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->un_unrecognized);
  764. trunnel_assert(written <= avail);
  765. if (avail - written < elt_len)
  766. goto truncated;
  767. if (elt_len)
  768. memcpy(ptr, obj->un_unrecognized.elts_, elt_len);
  769. written += elt_len; ptr += elt_len;
  770. }
  771. break;
  772. }
  773. /* Write the length field back to ls_len */
  774. trunnel_assert(written >= written_before_union);
  775. #if UINT8_MAX < SIZE_MAX
  776. if (written - written_before_union > UINT8_MAX)
  777. goto check_failed;
  778. #endif
  779. trunnel_set_uint8(backptr_ls_len, (written - written_before_union));
  780. }
  781. trunnel_assert(ptr == output + written);
  782. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  783. {
  784. trunnel_assert(encoded_len >= 0);
  785. trunnel_assert((size_t)encoded_len == written);
  786. }
  787. #endif
  788. return written;
  789. truncated:
  790. result = -2;
  791. goto fail;
  792. check_failed:
  793. (void)msg;
  794. result = -1;
  795. goto fail;
  796. fail:
  797. trunnel_assert(result < 0);
  798. return result;
  799. }
  800. /** As link_specifier_parse(), but do not allocate the output object.
  801. */
  802. static ssize_t
  803. link_specifier_parse_into(link_specifier_t *obj, const uint8_t *input, const size_t len_in)
  804. {
  805. const uint8_t *ptr = input;
  806. size_t remaining = len_in;
  807. ssize_t result = 0;
  808. (void)result;
  809. /* Parse u8 ls_type */
  810. CHECK_REMAINING(1, truncated);
  811. obj->ls_type = (trunnel_get_uint8(ptr));
  812. remaining -= 1; ptr += 1;
  813. /* Parse u8 ls_len */
  814. CHECK_REMAINING(1, truncated);
  815. obj->ls_len = (trunnel_get_uint8(ptr));
  816. remaining -= 1; ptr += 1;
  817. {
  818. size_t remaining_after;
  819. CHECK_REMAINING(obj->ls_len, truncated);
  820. remaining_after = remaining - obj->ls_len;
  821. remaining = obj->ls_len;
  822. /* Parse union un[ls_type] */
  823. switch (obj->ls_type) {
  824. case LS_IPV4:
  825. /* Parse u32 un_ipv4_addr */
  826. CHECK_REMAINING(4, fail);
  827. obj->un_ipv4_addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  828. remaining -= 4; ptr += 4;
  829. /* Parse u16 un_ipv4_port */
  830. CHECK_REMAINING(2, fail);
  831. obj->un_ipv4_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  832. remaining -= 2; ptr += 2;
  833. break;
  834. case LS_IPV6:
  835. /* Parse u8 un_ipv6_addr[16] */
  836. CHECK_REMAINING(16, fail);
  837. memcpy(obj->un_ipv6_addr, ptr, 16);
  838. remaining -= 16; ptr += 16;
  839. /* Parse u16 un_ipv6_port */
  840. CHECK_REMAINING(2, fail);
  841. obj->un_ipv6_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  842. remaining -= 2; ptr += 2;
  843. break;
  844. case LS_LEGACY_ID:
  845. /* Parse u8 un_legacy_id[20] */
  846. CHECK_REMAINING(20, fail);
  847. memcpy(obj->un_legacy_id, ptr, 20);
  848. remaining -= 20; ptr += 20;
  849. break;
  850. case LS_ED25519_ID:
  851. /* Parse u8 un_ed25519_id[32] */
  852. CHECK_REMAINING(32, fail);
  853. memcpy(obj->un_ed25519_id, ptr, 32);
  854. remaining -= 32; ptr += 32;
  855. break;
  856. default:
  857. /* Parse u8 un_unrecognized[] */
  858. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unrecognized, remaining, {});
  859. obj->un_unrecognized.n_ = remaining;
  860. if (remaining)
  861. memcpy(obj->un_unrecognized.elts_, ptr, remaining);
  862. ptr += remaining; remaining -= remaining;
  863. break;
  864. }
  865. if (remaining != 0)
  866. goto fail;
  867. remaining = remaining_after;
  868. }
  869. trunnel_assert(ptr + remaining == input + len_in);
  870. return len_in - remaining;
  871. truncated:
  872. return -2;
  873. trunnel_alloc_failed:
  874. return -1;
  875. fail:
  876. result = -1;
  877. return result;
  878. }
  879. ssize_t
  880. link_specifier_parse(link_specifier_t **output, const uint8_t *input, const size_t len_in)
  881. {
  882. ssize_t result;
  883. *output = link_specifier_new();
  884. if (NULL == *output)
  885. return -1;
  886. result = link_specifier_parse_into(*output, input, len_in);
  887. if (result < 0) {
  888. link_specifier_free(*output);
  889. *output = NULL;
  890. }
  891. return result;
  892. }
  893. ed25519_cert_t *
  894. ed25519_cert_new(void)
  895. {
  896. ed25519_cert_t *val = trunnel_calloc(1, sizeof(ed25519_cert_t));
  897. if (NULL == val)
  898. return NULL;
  899. val->version = 1;
  900. return val;
  901. }
  902. /** Release all storage held inside 'obj', but do not free 'obj'.
  903. */
  904. static void
  905. ed25519_cert_clear(ed25519_cert_t *obj)
  906. {
  907. (void) obj;
  908. {
  909. unsigned idx;
  910. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  911. ed25519_cert_extension_free(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  912. }
  913. }
  914. TRUNNEL_DYNARRAY_WIPE(&obj->ext);
  915. TRUNNEL_DYNARRAY_CLEAR(&obj->ext);
  916. }
  917. void
  918. ed25519_cert_free(ed25519_cert_t *obj)
  919. {
  920. if (obj == NULL)
  921. return;
  922. ed25519_cert_clear(obj);
  923. trunnel_memwipe(obj, sizeof(ed25519_cert_t));
  924. trunnel_free_(obj);
  925. }
  926. uint8_t
  927. ed25519_cert_get_version(ed25519_cert_t *inp)
  928. {
  929. return inp->version;
  930. }
  931. int
  932. ed25519_cert_set_version(ed25519_cert_t *inp, uint8_t val)
  933. {
  934. if (! ((val == 1))) {
  935. TRUNNEL_SET_ERROR_CODE(inp);
  936. return -1;
  937. }
  938. inp->version = val;
  939. return 0;
  940. }
  941. uint8_t
  942. ed25519_cert_get_cert_type(ed25519_cert_t *inp)
  943. {
  944. return inp->cert_type;
  945. }
  946. int
  947. ed25519_cert_set_cert_type(ed25519_cert_t *inp, uint8_t val)
  948. {
  949. inp->cert_type = val;
  950. return 0;
  951. }
  952. uint32_t
  953. ed25519_cert_get_exp_field(ed25519_cert_t *inp)
  954. {
  955. return inp->exp_field;
  956. }
  957. int
  958. ed25519_cert_set_exp_field(ed25519_cert_t *inp, uint32_t val)
  959. {
  960. inp->exp_field = val;
  961. return 0;
  962. }
  963. uint8_t
  964. ed25519_cert_get_cert_key_type(ed25519_cert_t *inp)
  965. {
  966. return inp->cert_key_type;
  967. }
  968. int
  969. ed25519_cert_set_cert_key_type(ed25519_cert_t *inp, uint8_t val)
  970. {
  971. inp->cert_key_type = val;
  972. return 0;
  973. }
  974. size_t
  975. ed25519_cert_getlen_certified_key(const ed25519_cert_t *inp)
  976. {
  977. (void)inp; return 32;
  978. }
  979. uint8_t
  980. ed25519_cert_get_certified_key(ed25519_cert_t *inp, size_t idx)
  981. {
  982. trunnel_assert(idx < 32);
  983. return inp->certified_key[idx];
  984. }
  985. uint8_t
  986. ed25519_cert_getconst_certified_key(const ed25519_cert_t *inp, size_t idx)
  987. {
  988. return ed25519_cert_get_certified_key((ed25519_cert_t*)inp, idx);
  989. }
  990. int
  991. ed25519_cert_set_certified_key(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  992. {
  993. trunnel_assert(idx < 32);
  994. inp->certified_key[idx] = elt;
  995. return 0;
  996. }
  997. uint8_t *
  998. ed25519_cert_getarray_certified_key(ed25519_cert_t *inp)
  999. {
  1000. return inp->certified_key;
  1001. }
  1002. const uint8_t *
  1003. ed25519_cert_getconstarray_certified_key(const ed25519_cert_t *inp)
  1004. {
  1005. return (const uint8_t *)ed25519_cert_getarray_certified_key((ed25519_cert_t*)inp);
  1006. }
  1007. uint8_t
  1008. ed25519_cert_get_n_extensions(ed25519_cert_t *inp)
  1009. {
  1010. return inp->n_extensions;
  1011. }
  1012. int
  1013. ed25519_cert_set_n_extensions(ed25519_cert_t *inp, uint8_t val)
  1014. {
  1015. inp->n_extensions = val;
  1016. return 0;
  1017. }
  1018. size_t
  1019. ed25519_cert_getlen_ext(const ed25519_cert_t *inp)
  1020. {
  1021. return TRUNNEL_DYNARRAY_LEN(&inp->ext);
  1022. }
  1023. struct ed25519_cert_extension_st *
  1024. ed25519_cert_get_ext(ed25519_cert_t *inp, size_t idx)
  1025. {
  1026. return TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  1027. }
  1028. const struct ed25519_cert_extension_st *
  1029. ed25519_cert_getconst_ext(const ed25519_cert_t *inp, size_t idx)
  1030. {
  1031. return ed25519_cert_get_ext((ed25519_cert_t*)inp, idx);
  1032. }
  1033. int
  1034. ed25519_cert_set_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  1035. {
  1036. ed25519_cert_extension_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  1037. if (oldval && oldval != elt)
  1038. ed25519_cert_extension_free(oldval);
  1039. return ed25519_cert_set0_ext(inp, idx, elt);
  1040. }
  1041. int
  1042. ed25519_cert_set0_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  1043. {
  1044. TRUNNEL_DYNARRAY_SET(&inp->ext, idx, elt);
  1045. return 0;
  1046. }
  1047. int
  1048. ed25519_cert_add_ext(ed25519_cert_t *inp, struct ed25519_cert_extension_st * elt)
  1049. {
  1050. #if SIZE_MAX >= UINT8_MAX
  1051. if (inp->ext.n_ == UINT8_MAX)
  1052. goto trunnel_alloc_failed;
  1053. #endif
  1054. TRUNNEL_DYNARRAY_ADD(struct ed25519_cert_extension_st *, &inp->ext, elt, {});
  1055. return 0;
  1056. trunnel_alloc_failed:
  1057. TRUNNEL_SET_ERROR_CODE(inp);
  1058. return -1;
  1059. }
  1060. struct ed25519_cert_extension_st * *
  1061. ed25519_cert_getarray_ext(ed25519_cert_t *inp)
  1062. {
  1063. return inp->ext.elts_;
  1064. }
  1065. const struct ed25519_cert_extension_st * const *
  1066. ed25519_cert_getconstarray_ext(const ed25519_cert_t *inp)
  1067. {
  1068. return (const struct ed25519_cert_extension_st * const *)ed25519_cert_getarray_ext((ed25519_cert_t*)inp);
  1069. }
  1070. int
  1071. ed25519_cert_setlen_ext(ed25519_cert_t *inp, size_t newlen)
  1072. {
  1073. struct ed25519_cert_extension_st * *newptr;
  1074. #if UINT8_MAX < SIZE_MAX
  1075. if (newlen > UINT8_MAX)
  1076. goto trunnel_alloc_failed;
  1077. #endif
  1078. newptr = trunnel_dynarray_setlen(&inp->ext.allocated_,
  1079. &inp->ext.n_, inp->ext.elts_, newlen,
  1080. sizeof(inp->ext.elts_[0]), (trunnel_free_fn_t) ed25519_cert_extension_free,
  1081. &inp->trunnel_error_code_);
  1082. if (newlen != 0 && newptr == NULL)
  1083. goto trunnel_alloc_failed;
  1084. inp->ext.elts_ = newptr;
  1085. return 0;
  1086. trunnel_alloc_failed:
  1087. TRUNNEL_SET_ERROR_CODE(inp);
  1088. return -1;
  1089. }
  1090. size_t
  1091. ed25519_cert_getlen_signature(const ed25519_cert_t *inp)
  1092. {
  1093. (void)inp; return 64;
  1094. }
  1095. uint8_t
  1096. ed25519_cert_get_signature(ed25519_cert_t *inp, size_t idx)
  1097. {
  1098. trunnel_assert(idx < 64);
  1099. return inp->signature[idx];
  1100. }
  1101. uint8_t
  1102. ed25519_cert_getconst_signature(const ed25519_cert_t *inp, size_t idx)
  1103. {
  1104. return ed25519_cert_get_signature((ed25519_cert_t*)inp, idx);
  1105. }
  1106. int
  1107. ed25519_cert_set_signature(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  1108. {
  1109. trunnel_assert(idx < 64);
  1110. inp->signature[idx] = elt;
  1111. return 0;
  1112. }
  1113. uint8_t *
  1114. ed25519_cert_getarray_signature(ed25519_cert_t *inp)
  1115. {
  1116. return inp->signature;
  1117. }
  1118. const uint8_t *
  1119. ed25519_cert_getconstarray_signature(const ed25519_cert_t *inp)
  1120. {
  1121. return (const uint8_t *)ed25519_cert_getarray_signature((ed25519_cert_t*)inp);
  1122. }
  1123. const char *
  1124. ed25519_cert_check(const ed25519_cert_t *obj)
  1125. {
  1126. if (obj == NULL)
  1127. return "Object was NULL";
  1128. if (obj->trunnel_error_code_)
  1129. return "A set function failed on this object";
  1130. if (! (obj->version == 1))
  1131. return "Integer out of bounds";
  1132. {
  1133. const char *msg;
  1134. unsigned idx;
  1135. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1136. if (NULL != (msg = ed25519_cert_extension_check(TRUNNEL_DYNARRAY_GET(&obj->ext, idx))))
  1137. return msg;
  1138. }
  1139. }
  1140. if (TRUNNEL_DYNARRAY_LEN(&obj->ext) != obj->n_extensions)
  1141. return "Length mismatch for ext";
  1142. return NULL;
  1143. }
  1144. ssize_t
  1145. ed25519_cert_encoded_len(const ed25519_cert_t *obj)
  1146. {
  1147. ssize_t result = 0;
  1148. if (NULL != ed25519_cert_check(obj))
  1149. return -1;
  1150. /* Length of u8 version IN [1] */
  1151. result += 1;
  1152. /* Length of u8 cert_type */
  1153. result += 1;
  1154. /* Length of u32 exp_field */
  1155. result += 4;
  1156. /* Length of u8 cert_key_type */
  1157. result += 1;
  1158. /* Length of u8 certified_key[32] */
  1159. result += 32;
  1160. /* Length of u8 n_extensions */
  1161. result += 1;
  1162. /* Length of struct ed25519_cert_extension ext[n_extensions] */
  1163. {
  1164. unsigned idx;
  1165. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1166. result += ed25519_cert_extension_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  1167. }
  1168. }
  1169. /* Length of u8 signature[64] */
  1170. result += 64;
  1171. return result;
  1172. }
  1173. int
  1174. ed25519_cert_clear_errors(ed25519_cert_t *obj)
  1175. {
  1176. int r = obj->trunnel_error_code_;
  1177. obj->trunnel_error_code_ = 0;
  1178. return r;
  1179. }
  1180. ssize_t
  1181. ed25519_cert_encode(uint8_t *output, const size_t avail, const ed25519_cert_t *obj)
  1182. {
  1183. ssize_t result = 0;
  1184. size_t written = 0;
  1185. uint8_t *ptr = output;
  1186. const char *msg;
  1187. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1188. const ssize_t encoded_len = ed25519_cert_encoded_len(obj);
  1189. #endif
  1190. if (NULL != (msg = ed25519_cert_check(obj)))
  1191. goto check_failed;
  1192. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1193. trunnel_assert(encoded_len >= 0);
  1194. #endif
  1195. /* Encode u8 version IN [1] */
  1196. trunnel_assert(written <= avail);
  1197. if (avail - written < 1)
  1198. goto truncated;
  1199. trunnel_set_uint8(ptr, (obj->version));
  1200. written += 1; ptr += 1;
  1201. /* Encode u8 cert_type */
  1202. trunnel_assert(written <= avail);
  1203. if (avail - written < 1)
  1204. goto truncated;
  1205. trunnel_set_uint8(ptr, (obj->cert_type));
  1206. written += 1; ptr += 1;
  1207. /* Encode u32 exp_field */
  1208. trunnel_assert(written <= avail);
  1209. if (avail - written < 4)
  1210. goto truncated;
  1211. trunnel_set_uint32(ptr, trunnel_htonl(obj->exp_field));
  1212. written += 4; ptr += 4;
  1213. /* Encode u8 cert_key_type */
  1214. trunnel_assert(written <= avail);
  1215. if (avail - written < 1)
  1216. goto truncated;
  1217. trunnel_set_uint8(ptr, (obj->cert_key_type));
  1218. written += 1; ptr += 1;
  1219. /* Encode u8 certified_key[32] */
  1220. trunnel_assert(written <= avail);
  1221. if (avail - written < 32)
  1222. goto truncated;
  1223. memcpy(ptr, obj->certified_key, 32);
  1224. written += 32; ptr += 32;
  1225. /* Encode u8 n_extensions */
  1226. trunnel_assert(written <= avail);
  1227. if (avail - written < 1)
  1228. goto truncated;
  1229. trunnel_set_uint8(ptr, (obj->n_extensions));
  1230. written += 1; ptr += 1;
  1231. /* Encode struct ed25519_cert_extension ext[n_extensions] */
  1232. {
  1233. unsigned idx;
  1234. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  1235. trunnel_assert(written <= avail);
  1236. result = ed25519_cert_extension_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  1237. if (result < 0)
  1238. goto fail; /* XXXXXXX !*/
  1239. written += result; ptr += result;
  1240. }
  1241. }
  1242. /* Encode u8 signature[64] */
  1243. trunnel_assert(written <= avail);
  1244. if (avail - written < 64)
  1245. goto truncated;
  1246. memcpy(ptr, obj->signature, 64);
  1247. written += 64; ptr += 64;
  1248. trunnel_assert(ptr == output + written);
  1249. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1250. {
  1251. trunnel_assert(encoded_len >= 0);
  1252. trunnel_assert((size_t)encoded_len == written);
  1253. }
  1254. #endif
  1255. return written;
  1256. truncated:
  1257. result = -2;
  1258. goto fail;
  1259. check_failed:
  1260. (void)msg;
  1261. result = -1;
  1262. goto fail;
  1263. fail:
  1264. trunnel_assert(result < 0);
  1265. return result;
  1266. }
  1267. /** As ed25519_cert_parse(), but do not allocate the output object.
  1268. */
  1269. static ssize_t
  1270. ed25519_cert_parse_into(ed25519_cert_t *obj, const uint8_t *input, const size_t len_in)
  1271. {
  1272. const uint8_t *ptr = input;
  1273. size_t remaining = len_in;
  1274. ssize_t result = 0;
  1275. (void)result;
  1276. /* Parse u8 version IN [1] */
  1277. CHECK_REMAINING(1, truncated);
  1278. obj->version = (trunnel_get_uint8(ptr));
  1279. remaining -= 1; ptr += 1;
  1280. if (! (obj->version == 1))
  1281. goto fail;
  1282. /* Parse u8 cert_type */
  1283. CHECK_REMAINING(1, truncated);
  1284. obj->cert_type = (trunnel_get_uint8(ptr));
  1285. remaining -= 1; ptr += 1;
  1286. /* Parse u32 exp_field */
  1287. CHECK_REMAINING(4, truncated);
  1288. obj->exp_field = trunnel_ntohl(trunnel_get_uint32(ptr));
  1289. remaining -= 4; ptr += 4;
  1290. /* Parse u8 cert_key_type */
  1291. CHECK_REMAINING(1, truncated);
  1292. obj->cert_key_type = (trunnel_get_uint8(ptr));
  1293. remaining -= 1; ptr += 1;
  1294. /* Parse u8 certified_key[32] */
  1295. CHECK_REMAINING(32, truncated);
  1296. memcpy(obj->certified_key, ptr, 32);
  1297. remaining -= 32; ptr += 32;
  1298. /* Parse u8 n_extensions */
  1299. CHECK_REMAINING(1, truncated);
  1300. obj->n_extensions = (trunnel_get_uint8(ptr));
  1301. remaining -= 1; ptr += 1;
  1302. /* Parse struct ed25519_cert_extension ext[n_extensions] */
  1303. TRUNNEL_DYNARRAY_EXPAND(ed25519_cert_extension_t *, &obj->ext, obj->n_extensions, {});
  1304. {
  1305. ed25519_cert_extension_t * elt;
  1306. unsigned idx;
  1307. for (idx = 0; idx < obj->n_extensions; ++idx) {
  1308. result = ed25519_cert_extension_parse(&elt, ptr, remaining);
  1309. if (result < 0)
  1310. goto relay_fail;
  1311. trunnel_assert((size_t)result <= remaining);
  1312. remaining -= result; ptr += result;
  1313. TRUNNEL_DYNARRAY_ADD(ed25519_cert_extension_t *, &obj->ext, elt, {ed25519_cert_extension_free(elt);});
  1314. }
  1315. }
  1316. /* Parse u8 signature[64] */
  1317. CHECK_REMAINING(64, truncated);
  1318. memcpy(obj->signature, ptr, 64);
  1319. remaining -= 64; ptr += 64;
  1320. trunnel_assert(ptr + remaining == input + len_in);
  1321. return len_in - remaining;
  1322. truncated:
  1323. return -2;
  1324. relay_fail:
  1325. trunnel_assert(result < 0);
  1326. return result;
  1327. trunnel_alloc_failed:
  1328. return -1;
  1329. fail:
  1330. result = -1;
  1331. return result;
  1332. }
  1333. ssize_t
  1334. ed25519_cert_parse(ed25519_cert_t **output, const uint8_t *input, const size_t len_in)
  1335. {
  1336. ssize_t result;
  1337. *output = ed25519_cert_new();
  1338. if (NULL == *output)
  1339. return -1;
  1340. result = ed25519_cert_parse_into(*output, input, len_in);
  1341. if (result < 0) {
  1342. ed25519_cert_free(*output);
  1343. *output = NULL;
  1344. }
  1345. return result;
  1346. }
  1347. link_specifier_list_t *
  1348. link_specifier_list_new(void)
  1349. {
  1350. link_specifier_list_t *val = trunnel_calloc(1, sizeof(link_specifier_list_t));
  1351. if (NULL == val)
  1352. return NULL;
  1353. return val;
  1354. }
  1355. /** Release all storage held inside 'obj', but do not free 'obj'.
  1356. */
  1357. static void
  1358. link_specifier_list_clear(link_specifier_list_t *obj)
  1359. {
  1360. (void) obj;
  1361. {
  1362. unsigned idx;
  1363. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  1364. link_specifier_free(TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  1365. }
  1366. }
  1367. TRUNNEL_DYNARRAY_WIPE(&obj->spec);
  1368. TRUNNEL_DYNARRAY_CLEAR(&obj->spec);
  1369. }
  1370. void
  1371. link_specifier_list_free(link_specifier_list_t *obj)
  1372. {
  1373. if (obj == NULL)
  1374. return;
  1375. link_specifier_list_clear(obj);
  1376. trunnel_memwipe(obj, sizeof(link_specifier_list_t));
  1377. trunnel_free_(obj);
  1378. }
  1379. uint8_t
  1380. link_specifier_list_get_n_spec(link_specifier_list_t *inp)
  1381. {
  1382. return inp->n_spec;
  1383. }
  1384. int
  1385. link_specifier_list_set_n_spec(link_specifier_list_t *inp, uint8_t val)
  1386. {
  1387. inp->n_spec = val;
  1388. return 0;
  1389. }
  1390. size_t
  1391. link_specifier_list_getlen_spec(const link_specifier_list_t *inp)
  1392. {
  1393. return TRUNNEL_DYNARRAY_LEN(&inp->spec);
  1394. }
  1395. struct link_specifier_st *
  1396. link_specifier_list_get_spec(link_specifier_list_t *inp, size_t idx)
  1397. {
  1398. return TRUNNEL_DYNARRAY_GET(&inp->spec, idx);
  1399. }
  1400. const struct link_specifier_st *
  1401. link_specifier_list_getconst_spec(const link_specifier_list_t *inp, size_t idx)
  1402. {
  1403. return link_specifier_list_get_spec((link_specifier_list_t*)inp, idx);
  1404. }
  1405. int
  1406. link_specifier_list_set_spec(link_specifier_list_t *inp, size_t idx, struct link_specifier_st * elt)
  1407. {
  1408. link_specifier_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->spec, idx);
  1409. if (oldval && oldval != elt)
  1410. link_specifier_free(oldval);
  1411. return link_specifier_list_set0_spec(inp, idx, elt);
  1412. }
  1413. int
  1414. link_specifier_list_set0_spec(link_specifier_list_t *inp, size_t idx, struct link_specifier_st * elt)
  1415. {
  1416. TRUNNEL_DYNARRAY_SET(&inp->spec, idx, elt);
  1417. return 0;
  1418. }
  1419. int
  1420. link_specifier_list_add_spec(link_specifier_list_t *inp, struct link_specifier_st * elt)
  1421. {
  1422. #if SIZE_MAX >= UINT8_MAX
  1423. if (inp->spec.n_ == UINT8_MAX)
  1424. goto trunnel_alloc_failed;
  1425. #endif
  1426. TRUNNEL_DYNARRAY_ADD(struct link_specifier_st *, &inp->spec, elt, {});
  1427. return 0;
  1428. trunnel_alloc_failed:
  1429. TRUNNEL_SET_ERROR_CODE(inp);
  1430. return -1;
  1431. }
  1432. struct link_specifier_st * *
  1433. link_specifier_list_getarray_spec(link_specifier_list_t *inp)
  1434. {
  1435. return inp->spec.elts_;
  1436. }
  1437. const struct link_specifier_st * const *
  1438. link_specifier_list_getconstarray_spec(const link_specifier_list_t *inp)
  1439. {
  1440. return (const struct link_specifier_st * const *)link_specifier_list_getarray_spec((link_specifier_list_t*)inp);
  1441. }
  1442. int
  1443. link_specifier_list_setlen_spec(link_specifier_list_t *inp, size_t newlen)
  1444. {
  1445. struct link_specifier_st * *newptr;
  1446. #if UINT8_MAX < SIZE_MAX
  1447. if (newlen > UINT8_MAX)
  1448. goto trunnel_alloc_failed;
  1449. #endif
  1450. newptr = trunnel_dynarray_setlen(&inp->spec.allocated_,
  1451. &inp->spec.n_, inp->spec.elts_, newlen,
  1452. sizeof(inp->spec.elts_[0]), (trunnel_free_fn_t) link_specifier_free,
  1453. &inp->trunnel_error_code_);
  1454. if (newlen != 0 && newptr == NULL)
  1455. goto trunnel_alloc_failed;
  1456. inp->spec.elts_ = newptr;
  1457. return 0;
  1458. trunnel_alloc_failed:
  1459. TRUNNEL_SET_ERROR_CODE(inp);
  1460. return -1;
  1461. }
  1462. const char *
  1463. link_specifier_list_check(const link_specifier_list_t *obj)
  1464. {
  1465. if (obj == NULL)
  1466. return "Object was NULL";
  1467. if (obj->trunnel_error_code_)
  1468. return "A set function failed on this object";
  1469. {
  1470. const char *msg;
  1471. unsigned idx;
  1472. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  1473. if (NULL != (msg = link_specifier_check(TRUNNEL_DYNARRAY_GET(&obj->spec, idx))))
  1474. return msg;
  1475. }
  1476. }
  1477. if (TRUNNEL_DYNARRAY_LEN(&obj->spec) != obj->n_spec)
  1478. return "Length mismatch for spec";
  1479. return NULL;
  1480. }
  1481. ssize_t
  1482. link_specifier_list_encoded_len(const link_specifier_list_t *obj)
  1483. {
  1484. ssize_t result = 0;
  1485. if (NULL != link_specifier_list_check(obj))
  1486. return -1;
  1487. /* Length of u8 n_spec */
  1488. result += 1;
  1489. /* Length of struct link_specifier spec[n_spec] */
  1490. {
  1491. unsigned idx;
  1492. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  1493. result += link_specifier_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  1494. }
  1495. }
  1496. return result;
  1497. }
  1498. int
  1499. link_specifier_list_clear_errors(link_specifier_list_t *obj)
  1500. {
  1501. int r = obj->trunnel_error_code_;
  1502. obj->trunnel_error_code_ = 0;
  1503. return r;
  1504. }
  1505. ssize_t
  1506. link_specifier_list_encode(uint8_t *output, const size_t avail, const link_specifier_list_t *obj)
  1507. {
  1508. ssize_t result = 0;
  1509. size_t written = 0;
  1510. uint8_t *ptr = output;
  1511. const char *msg;
  1512. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1513. const ssize_t encoded_len = link_specifier_list_encoded_len(obj);
  1514. #endif
  1515. if (NULL != (msg = link_specifier_list_check(obj)))
  1516. goto check_failed;
  1517. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1518. trunnel_assert(encoded_len >= 0);
  1519. #endif
  1520. /* Encode u8 n_spec */
  1521. trunnel_assert(written <= avail);
  1522. if (avail - written < 1)
  1523. goto truncated;
  1524. trunnel_set_uint8(ptr, (obj->n_spec));
  1525. written += 1; ptr += 1;
  1526. /* Encode struct link_specifier spec[n_spec] */
  1527. {
  1528. unsigned idx;
  1529. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->spec); ++idx) {
  1530. trunnel_assert(written <= avail);
  1531. result = link_specifier_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->spec, idx));
  1532. if (result < 0)
  1533. goto fail; /* XXXXXXX !*/
  1534. written += result; ptr += result;
  1535. }
  1536. }
  1537. trunnel_assert(ptr == output + written);
  1538. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1539. {
  1540. trunnel_assert(encoded_len >= 0);
  1541. trunnel_assert((size_t)encoded_len == written);
  1542. }
  1543. #endif
  1544. return written;
  1545. truncated:
  1546. result = -2;
  1547. goto fail;
  1548. check_failed:
  1549. (void)msg;
  1550. result = -1;
  1551. goto fail;
  1552. fail:
  1553. trunnel_assert(result < 0);
  1554. return result;
  1555. }
  1556. /** As link_specifier_list_parse(), but do not allocate the output
  1557. * object.
  1558. */
  1559. static ssize_t
  1560. link_specifier_list_parse_into(link_specifier_list_t *obj, const uint8_t *input, const size_t len_in)
  1561. {
  1562. const uint8_t *ptr = input;
  1563. size_t remaining = len_in;
  1564. ssize_t result = 0;
  1565. (void)result;
  1566. /* Parse u8 n_spec */
  1567. CHECK_REMAINING(1, truncated);
  1568. obj->n_spec = (trunnel_get_uint8(ptr));
  1569. remaining -= 1; ptr += 1;
  1570. /* Parse struct link_specifier spec[n_spec] */
  1571. TRUNNEL_DYNARRAY_EXPAND(link_specifier_t *, &obj->spec, obj->n_spec, {});
  1572. {
  1573. link_specifier_t * elt;
  1574. unsigned idx;
  1575. for (idx = 0; idx < obj->n_spec; ++idx) {
  1576. result = link_specifier_parse(&elt, ptr, remaining);
  1577. if (result < 0)
  1578. goto relay_fail;
  1579. trunnel_assert((size_t)result <= remaining);
  1580. remaining -= result; ptr += result;
  1581. TRUNNEL_DYNARRAY_ADD(link_specifier_t *, &obj->spec, elt, {link_specifier_free(elt);});
  1582. }
  1583. }
  1584. trunnel_assert(ptr + remaining == input + len_in);
  1585. return len_in - remaining;
  1586. truncated:
  1587. return -2;
  1588. relay_fail:
  1589. trunnel_assert(result < 0);
  1590. return result;
  1591. trunnel_alloc_failed:
  1592. return -1;
  1593. }
  1594. ssize_t
  1595. link_specifier_list_parse(link_specifier_list_t **output, const uint8_t *input, const size_t len_in)
  1596. {
  1597. ssize_t result;
  1598. *output = link_specifier_list_new();
  1599. if (NULL == *output)
  1600. return -1;
  1601. result = link_specifier_list_parse_into(*output, input, len_in);
  1602. if (result < 0) {
  1603. link_specifier_list_free(*output);
  1604. *output = NULL;
  1605. }
  1606. return result;
  1607. }