ed25519_cert.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /* ed25519_cert.c -- generated by Trunnel v1.4.3.
  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(const ed25519_cert_extension_t *inp, size_t idx)
  92. {
  93. trunnel_assert(idx < 32);
  94. return inp->un_signing_key[idx];
  95. }
  96. int
  97. ed25519_cert_extension_set_un_signing_key(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  98. {
  99. trunnel_assert(idx < 32);
  100. inp->un_signing_key[idx] = elt;
  101. return 0;
  102. }
  103. uint8_t *
  104. ed25519_cert_extension_getarray_un_signing_key(ed25519_cert_extension_t *inp)
  105. {
  106. return inp->un_signing_key;
  107. }
  108. size_t
  109. ed25519_cert_extension_getlen_un_unparsed(const ed25519_cert_extension_t *inp)
  110. {
  111. return TRUNNEL_DYNARRAY_LEN(&inp->un_unparsed);
  112. }
  113. uint8_t
  114. ed25519_cert_extension_get_un_unparsed(ed25519_cert_extension_t *inp, size_t idx)
  115. {
  116. return TRUNNEL_DYNARRAY_GET(&inp->un_unparsed, idx);
  117. }
  118. int
  119. ed25519_cert_extension_set_un_unparsed(ed25519_cert_extension_t *inp, size_t idx, uint8_t elt)
  120. {
  121. TRUNNEL_DYNARRAY_SET(&inp->un_unparsed, idx, elt);
  122. return 0;
  123. }
  124. int
  125. ed25519_cert_extension_add_un_unparsed(ed25519_cert_extension_t *inp, uint8_t elt)
  126. {
  127. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->un_unparsed, elt, {});
  128. return 0;
  129. trunnel_alloc_failed:
  130. TRUNNEL_SET_ERROR_CODE(inp);
  131. return -1;
  132. }
  133. uint8_t *
  134. ed25519_cert_extension_getarray_un_unparsed(ed25519_cert_extension_t *inp)
  135. {
  136. return inp->un_unparsed.elts_;
  137. }
  138. int
  139. ed25519_cert_extension_setlen_un_unparsed(ed25519_cert_extension_t *inp, size_t newlen)
  140. {
  141. uint8_t *newptr;
  142. newptr = trunnel_dynarray_setlen(&inp->un_unparsed.allocated_,
  143. &inp->un_unparsed.n_, inp->un_unparsed.elts_, newlen,
  144. sizeof(inp->un_unparsed.elts_[0]), (trunnel_free_fn_t) NULL,
  145. &inp->trunnel_error_code_);
  146. if (newptr == NULL)
  147. goto trunnel_alloc_failed;
  148. inp->un_unparsed.elts_ = newptr;
  149. return 0;
  150. trunnel_alloc_failed:
  151. TRUNNEL_SET_ERROR_CODE(inp);
  152. return -1;
  153. }
  154. const char *
  155. ed25519_cert_extension_check(const ed25519_cert_extension_t *obj)
  156. {
  157. if (obj == NULL)
  158. return "Object was NULL";
  159. if (obj->trunnel_error_code_)
  160. return "A set function failed on this object";
  161. switch (obj->ext_type) {
  162. case CERTEXT_SIGNED_WITH_KEY:
  163. break;
  164. default:
  165. break;
  166. }
  167. return NULL;
  168. }
  169. ssize_t
  170. ed25519_cert_extension_encoded_len(const ed25519_cert_extension_t *obj)
  171. {
  172. ssize_t result = 0;
  173. if (NULL != ed25519_cert_extension_check(obj))
  174. return -1;
  175. /* Length of u16 ext_length */
  176. result += 2;
  177. /* Length of u8 ext_type */
  178. result += 1;
  179. /* Length of u8 ext_flags */
  180. result += 1;
  181. switch (obj->ext_type) {
  182. case CERTEXT_SIGNED_WITH_KEY:
  183. /* Length of u8 un_signing_key[32] */
  184. result += 32;
  185. break;
  186. default:
  187. /* Length of u8 un_unparsed[] */
  188. result += TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  189. break;
  190. }
  191. return result;
  192. }
  193. int
  194. ed25519_cert_extension_clear_errors(ed25519_cert_extension_t *obj)
  195. {
  196. int r = obj->trunnel_error_code_;
  197. obj->trunnel_error_code_ = 0;
  198. return r;
  199. }
  200. ssize_t
  201. ed25519_cert_extension_encode(uint8_t *output, const size_t avail, const ed25519_cert_extension_t *obj)
  202. {
  203. ssize_t result = 0;
  204. size_t written = 0;
  205. uint8_t *ptr = output;
  206. const char *msg;
  207. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  208. const ssize_t encoded_len = ed25519_cert_extension_encoded_len(obj);
  209. #endif
  210. uint8_t *backptr_ext_length = NULL;
  211. if (NULL != (msg = ed25519_cert_extension_check(obj)))
  212. goto check_failed;
  213. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  214. trunnel_assert(encoded_len >= 0);
  215. #endif
  216. /* Encode u16 ext_length */
  217. backptr_ext_length = ptr;
  218. trunnel_assert(written <= avail);
  219. if (avail - written < 2)
  220. goto truncated;
  221. trunnel_set_uint16(ptr, trunnel_htons(obj->ext_length));
  222. written += 2; ptr += 2;
  223. /* Encode u8 ext_type */
  224. trunnel_assert(written <= avail);
  225. if (avail - written < 1)
  226. goto truncated;
  227. trunnel_set_uint8(ptr, (obj->ext_type));
  228. written += 1; ptr += 1;
  229. /* Encode u8 ext_flags */
  230. trunnel_assert(written <= avail);
  231. if (avail - written < 1)
  232. goto truncated;
  233. trunnel_set_uint8(ptr, (obj->ext_flags));
  234. written += 1; ptr += 1;
  235. {
  236. size_t written_before_union = written;
  237. /* Encode union un[ext_type] */
  238. trunnel_assert(written <= avail);
  239. switch (obj->ext_type) {
  240. case CERTEXT_SIGNED_WITH_KEY:
  241. /* Encode u8 un_signing_key[32] */
  242. trunnel_assert(written <= avail);
  243. if (avail - written < 32)
  244. goto truncated;
  245. memcpy(ptr, obj->un_signing_key, 32);
  246. written += 32; ptr += 32;
  247. break;
  248. default:
  249. /* Encode u8 un_unparsed[] */
  250. {
  251. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->un_unparsed);
  252. trunnel_assert(written <= avail);
  253. if (avail - written < elt_len)
  254. goto truncated;
  255. memcpy(ptr, obj->un_unparsed.elts_, elt_len);
  256. written += elt_len; ptr += elt_len;
  257. }
  258. break;
  259. }
  260. /* Write the length field back to ext_length */
  261. trunnel_assert(written >= written_before_union);
  262. #if UINT16_MAX < SIZE_MAX
  263. if (written - written_before_union > UINT16_MAX)
  264. goto check_failed;
  265. #endif
  266. trunnel_set_uint16(backptr_ext_length, trunnel_htons(written - written_before_union));
  267. }
  268. trunnel_assert(ptr == output + written);
  269. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  270. {
  271. trunnel_assert(encoded_len >= 0);
  272. trunnel_assert((size_t)encoded_len == written);
  273. }
  274. #endif
  275. return written;
  276. truncated:
  277. result = -2;
  278. goto fail;
  279. check_failed:
  280. (void)msg;
  281. result = -1;
  282. goto fail;
  283. fail:
  284. trunnel_assert(result < 0);
  285. return result;
  286. }
  287. /** As ed25519_cert_extension_parse(), but do not allocate the output
  288. * object.
  289. */
  290. static ssize_t
  291. ed25519_cert_extension_parse_into(ed25519_cert_extension_t *obj, const uint8_t *input, const size_t len_in)
  292. {
  293. const uint8_t *ptr = input;
  294. size_t remaining = len_in;
  295. ssize_t result = 0;
  296. (void)result;
  297. /* Parse u16 ext_length */
  298. CHECK_REMAINING(2, truncated);
  299. obj->ext_length = trunnel_ntohs(trunnel_get_uint16(ptr));
  300. remaining -= 2; ptr += 2;
  301. /* Parse u8 ext_type */
  302. CHECK_REMAINING(1, truncated);
  303. obj->ext_type = (trunnel_get_uint8(ptr));
  304. remaining -= 1; ptr += 1;
  305. /* Parse u8 ext_flags */
  306. CHECK_REMAINING(1, truncated);
  307. obj->ext_flags = (trunnel_get_uint8(ptr));
  308. remaining -= 1; ptr += 1;
  309. {
  310. size_t remaining_after;
  311. CHECK_REMAINING(obj->ext_length, truncated);
  312. remaining_after = remaining - obj->ext_length;
  313. remaining = obj->ext_length;
  314. /* Parse union un[ext_type] */
  315. switch (obj->ext_type) {
  316. case CERTEXT_SIGNED_WITH_KEY:
  317. /* Parse u8 un_signing_key[32] */
  318. CHECK_REMAINING(32, fail);
  319. memcpy(obj->un_signing_key, ptr, 32);
  320. remaining -= 32; ptr += 32;
  321. break;
  322. default:
  323. /* Parse u8 un_unparsed[] */
  324. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unparsed, remaining, {});
  325. obj->un_unparsed.n_ = remaining;
  326. memcpy(obj->un_unparsed.elts_, ptr, remaining);
  327. ptr += remaining; remaining -= remaining;
  328. break;
  329. }
  330. if (remaining != 0)
  331. goto fail;
  332. remaining = remaining_after;
  333. }
  334. trunnel_assert(ptr + remaining == input + len_in);
  335. return len_in - remaining;
  336. truncated:
  337. return -2;
  338. trunnel_alloc_failed:
  339. return -1;
  340. fail:
  341. result = -1;
  342. return result;
  343. }
  344. ssize_t
  345. ed25519_cert_extension_parse(ed25519_cert_extension_t **output, const uint8_t *input, const size_t len_in)
  346. {
  347. ssize_t result;
  348. *output = ed25519_cert_extension_new();
  349. if (NULL == *output)
  350. return -1;
  351. result = ed25519_cert_extension_parse_into(*output, input, len_in);
  352. if (result < 0) {
  353. ed25519_cert_extension_free(*output);
  354. *output = NULL;
  355. }
  356. return result;
  357. }
  358. ed25519_cert_t *
  359. ed25519_cert_new(void)
  360. {
  361. ed25519_cert_t *val = trunnel_calloc(1, sizeof(ed25519_cert_t));
  362. if (NULL == val)
  363. return NULL;
  364. val->version = 1;
  365. return val;
  366. }
  367. /** Release all storage held inside 'obj', but do not free 'obj'.
  368. */
  369. static void
  370. ed25519_cert_clear(ed25519_cert_t *obj)
  371. {
  372. (void) obj;
  373. {
  374. unsigned idx;
  375. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  376. ed25519_cert_extension_free(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  377. }
  378. }
  379. TRUNNEL_DYNARRAY_WIPE(&obj->ext);
  380. TRUNNEL_DYNARRAY_CLEAR(&obj->ext);
  381. }
  382. void
  383. ed25519_cert_free(ed25519_cert_t *obj)
  384. {
  385. if (obj == NULL)
  386. return;
  387. ed25519_cert_clear(obj);
  388. trunnel_memwipe(obj, sizeof(ed25519_cert_t));
  389. trunnel_free_(obj);
  390. }
  391. uint8_t
  392. ed25519_cert_get_version(ed25519_cert_t *inp)
  393. {
  394. return inp->version;
  395. }
  396. int
  397. ed25519_cert_set_version(ed25519_cert_t *inp, uint8_t val)
  398. {
  399. if (! ((val == 1))) {
  400. TRUNNEL_SET_ERROR_CODE(inp);
  401. return -1;
  402. }
  403. inp->version = val;
  404. return 0;
  405. }
  406. uint8_t
  407. ed25519_cert_get_cert_type(ed25519_cert_t *inp)
  408. {
  409. return inp->cert_type;
  410. }
  411. int
  412. ed25519_cert_set_cert_type(ed25519_cert_t *inp, uint8_t val)
  413. {
  414. inp->cert_type = val;
  415. return 0;
  416. }
  417. uint32_t
  418. ed25519_cert_get_exp_field(ed25519_cert_t *inp)
  419. {
  420. return inp->exp_field;
  421. }
  422. int
  423. ed25519_cert_set_exp_field(ed25519_cert_t *inp, uint32_t val)
  424. {
  425. inp->exp_field = val;
  426. return 0;
  427. }
  428. uint8_t
  429. ed25519_cert_get_cert_key_type(ed25519_cert_t *inp)
  430. {
  431. return inp->cert_key_type;
  432. }
  433. int
  434. ed25519_cert_set_cert_key_type(ed25519_cert_t *inp, uint8_t val)
  435. {
  436. inp->cert_key_type = val;
  437. return 0;
  438. }
  439. size_t
  440. ed25519_cert_getlen_certified_key(const ed25519_cert_t *inp)
  441. {
  442. (void)inp; return 32;
  443. }
  444. uint8_t
  445. ed25519_cert_get_certified_key(const ed25519_cert_t *inp, size_t idx)
  446. {
  447. trunnel_assert(idx < 32);
  448. return inp->certified_key[idx];
  449. }
  450. int
  451. ed25519_cert_set_certified_key(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  452. {
  453. trunnel_assert(idx < 32);
  454. inp->certified_key[idx] = elt;
  455. return 0;
  456. }
  457. uint8_t *
  458. ed25519_cert_getarray_certified_key(ed25519_cert_t *inp)
  459. {
  460. return inp->certified_key;
  461. }
  462. uint8_t
  463. ed25519_cert_get_n_extensions(ed25519_cert_t *inp)
  464. {
  465. return inp->n_extensions;
  466. }
  467. int
  468. ed25519_cert_set_n_extensions(ed25519_cert_t *inp, uint8_t val)
  469. {
  470. inp->n_extensions = val;
  471. return 0;
  472. }
  473. size_t
  474. ed25519_cert_getlen_ext(const ed25519_cert_t *inp)
  475. {
  476. return TRUNNEL_DYNARRAY_LEN(&inp->ext);
  477. }
  478. struct ed25519_cert_extension_st *
  479. ed25519_cert_get_ext(ed25519_cert_t *inp, size_t idx)
  480. {
  481. return TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  482. }
  483. int
  484. ed25519_cert_set_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  485. {
  486. ed25519_cert_extension_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->ext, idx);
  487. if (oldval && oldval != elt)
  488. ed25519_cert_extension_free(oldval);
  489. return ed25519_cert_set0_ext(inp, idx, elt);
  490. }
  491. int
  492. ed25519_cert_set0_ext(ed25519_cert_t *inp, size_t idx, struct ed25519_cert_extension_st * elt)
  493. {
  494. TRUNNEL_DYNARRAY_SET(&inp->ext, idx, elt);
  495. return 0;
  496. }
  497. int
  498. ed25519_cert_add_ext(ed25519_cert_t *inp, struct ed25519_cert_extension_st * elt)
  499. {
  500. #if SIZE_MAX >= UINT8_MAX
  501. if (inp->ext.n_ == UINT8_MAX)
  502. goto trunnel_alloc_failed;
  503. #endif
  504. TRUNNEL_DYNARRAY_ADD(struct ed25519_cert_extension_st *, &inp->ext, elt, {});
  505. return 0;
  506. trunnel_alloc_failed:
  507. TRUNNEL_SET_ERROR_CODE(inp);
  508. return -1;
  509. }
  510. struct ed25519_cert_extension_st * *
  511. ed25519_cert_getarray_ext(ed25519_cert_t *inp)
  512. {
  513. return inp->ext.elts_;
  514. }
  515. int
  516. ed25519_cert_setlen_ext(ed25519_cert_t *inp, size_t newlen)
  517. {
  518. struct ed25519_cert_extension_st * *newptr;
  519. #if UINT8_MAX < SIZE_MAX
  520. if (newlen > UINT8_MAX)
  521. goto trunnel_alloc_failed;
  522. #endif
  523. newptr = trunnel_dynarray_setlen(&inp->ext.allocated_,
  524. &inp->ext.n_, inp->ext.elts_, newlen,
  525. sizeof(inp->ext.elts_[0]), (trunnel_free_fn_t) ed25519_cert_extension_free,
  526. &inp->trunnel_error_code_);
  527. if (newptr == NULL)
  528. goto trunnel_alloc_failed;
  529. inp->ext.elts_ = newptr;
  530. return 0;
  531. trunnel_alloc_failed:
  532. TRUNNEL_SET_ERROR_CODE(inp);
  533. return -1;
  534. }
  535. size_t
  536. ed25519_cert_getlen_signature(const ed25519_cert_t *inp)
  537. {
  538. (void)inp; return 64;
  539. }
  540. uint8_t
  541. ed25519_cert_get_signature(const ed25519_cert_t *inp, size_t idx)
  542. {
  543. trunnel_assert(idx < 64);
  544. return inp->signature[idx];
  545. }
  546. int
  547. ed25519_cert_set_signature(ed25519_cert_t *inp, size_t idx, uint8_t elt)
  548. {
  549. trunnel_assert(idx < 64);
  550. inp->signature[idx] = elt;
  551. return 0;
  552. }
  553. uint8_t *
  554. ed25519_cert_getarray_signature(ed25519_cert_t *inp)
  555. {
  556. return inp->signature;
  557. }
  558. const char *
  559. ed25519_cert_check(const ed25519_cert_t *obj)
  560. {
  561. if (obj == NULL)
  562. return "Object was NULL";
  563. if (obj->trunnel_error_code_)
  564. return "A set function failed on this object";
  565. if (! (obj->version == 1))
  566. return "Integer out of bounds";
  567. {
  568. const char *msg;
  569. unsigned idx;
  570. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  571. if (NULL != (msg = ed25519_cert_extension_check(TRUNNEL_DYNARRAY_GET(&obj->ext, idx))))
  572. return msg;
  573. }
  574. }
  575. if (TRUNNEL_DYNARRAY_LEN(&obj->ext) != obj->n_extensions)
  576. return "Length mismatch for ext";
  577. return NULL;
  578. }
  579. ssize_t
  580. ed25519_cert_encoded_len(const ed25519_cert_t *obj)
  581. {
  582. ssize_t result = 0;
  583. if (NULL != ed25519_cert_check(obj))
  584. return -1;
  585. /* Length of u8 version IN [1] */
  586. result += 1;
  587. /* Length of u8 cert_type */
  588. result += 1;
  589. /* Length of u32 exp_field */
  590. result += 4;
  591. /* Length of u8 cert_key_type */
  592. result += 1;
  593. /* Length of u8 certified_key[32] */
  594. result += 32;
  595. /* Length of u8 n_extensions */
  596. result += 1;
  597. /* Length of struct ed25519_cert_extension ext[n_extensions] */
  598. {
  599. unsigned idx;
  600. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  601. result += ed25519_cert_extension_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  602. }
  603. }
  604. /* Length of u8 signature[64] */
  605. result += 64;
  606. return result;
  607. }
  608. int
  609. ed25519_cert_clear_errors(ed25519_cert_t *obj)
  610. {
  611. int r = obj->trunnel_error_code_;
  612. obj->trunnel_error_code_ = 0;
  613. return r;
  614. }
  615. ssize_t
  616. ed25519_cert_encode(uint8_t *output, const size_t avail, const ed25519_cert_t *obj)
  617. {
  618. ssize_t result = 0;
  619. size_t written = 0;
  620. uint8_t *ptr = output;
  621. const char *msg;
  622. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  623. const ssize_t encoded_len = ed25519_cert_encoded_len(obj);
  624. #endif
  625. if (NULL != (msg = ed25519_cert_check(obj)))
  626. goto check_failed;
  627. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  628. trunnel_assert(encoded_len >= 0);
  629. #endif
  630. /* Encode u8 version IN [1] */
  631. trunnel_assert(written <= avail);
  632. if (avail - written < 1)
  633. goto truncated;
  634. trunnel_set_uint8(ptr, (obj->version));
  635. written += 1; ptr += 1;
  636. /* Encode u8 cert_type */
  637. trunnel_assert(written <= avail);
  638. if (avail - written < 1)
  639. goto truncated;
  640. trunnel_set_uint8(ptr, (obj->cert_type));
  641. written += 1; ptr += 1;
  642. /* Encode u32 exp_field */
  643. trunnel_assert(written <= avail);
  644. if (avail - written < 4)
  645. goto truncated;
  646. trunnel_set_uint32(ptr, trunnel_htonl(obj->exp_field));
  647. written += 4; ptr += 4;
  648. /* Encode u8 cert_key_type */
  649. trunnel_assert(written <= avail);
  650. if (avail - written < 1)
  651. goto truncated;
  652. trunnel_set_uint8(ptr, (obj->cert_key_type));
  653. written += 1; ptr += 1;
  654. /* Encode u8 certified_key[32] */
  655. trunnel_assert(written <= avail);
  656. if (avail - written < 32)
  657. goto truncated;
  658. memcpy(ptr, obj->certified_key, 32);
  659. written += 32; ptr += 32;
  660. /* Encode u8 n_extensions */
  661. trunnel_assert(written <= avail);
  662. if (avail - written < 1)
  663. goto truncated;
  664. trunnel_set_uint8(ptr, (obj->n_extensions));
  665. written += 1; ptr += 1;
  666. /* Encode struct ed25519_cert_extension ext[n_extensions] */
  667. {
  668. unsigned idx;
  669. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->ext); ++idx) {
  670. trunnel_assert(written <= avail);
  671. result = ed25519_cert_extension_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->ext, idx));
  672. if (result < 0)
  673. goto fail; /* XXXXXXX !*/
  674. written += result; ptr += result;
  675. }
  676. }
  677. /* Encode u8 signature[64] */
  678. trunnel_assert(written <= avail);
  679. if (avail - written < 64)
  680. goto truncated;
  681. memcpy(ptr, obj->signature, 64);
  682. written += 64; ptr += 64;
  683. trunnel_assert(ptr == output + written);
  684. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  685. {
  686. trunnel_assert(encoded_len >= 0);
  687. trunnel_assert((size_t)encoded_len == written);
  688. }
  689. #endif
  690. return written;
  691. truncated:
  692. result = -2;
  693. goto fail;
  694. check_failed:
  695. (void)msg;
  696. result = -1;
  697. goto fail;
  698. fail:
  699. trunnel_assert(result < 0);
  700. return result;
  701. }
  702. /** As ed25519_cert_parse(), but do not allocate the output object.
  703. */
  704. static ssize_t
  705. ed25519_cert_parse_into(ed25519_cert_t *obj, const uint8_t *input, const size_t len_in)
  706. {
  707. const uint8_t *ptr = input;
  708. size_t remaining = len_in;
  709. ssize_t result = 0;
  710. (void)result;
  711. /* Parse u8 version IN [1] */
  712. CHECK_REMAINING(1, truncated);
  713. obj->version = (trunnel_get_uint8(ptr));
  714. remaining -= 1; ptr += 1;
  715. if (! (obj->version == 1))
  716. goto fail;
  717. /* Parse u8 cert_type */
  718. CHECK_REMAINING(1, truncated);
  719. obj->cert_type = (trunnel_get_uint8(ptr));
  720. remaining -= 1; ptr += 1;
  721. /* Parse u32 exp_field */
  722. CHECK_REMAINING(4, truncated);
  723. obj->exp_field = trunnel_ntohl(trunnel_get_uint32(ptr));
  724. remaining -= 4; ptr += 4;
  725. /* Parse u8 cert_key_type */
  726. CHECK_REMAINING(1, truncated);
  727. obj->cert_key_type = (trunnel_get_uint8(ptr));
  728. remaining -= 1; ptr += 1;
  729. /* Parse u8 certified_key[32] */
  730. CHECK_REMAINING(32, truncated);
  731. memcpy(obj->certified_key, ptr, 32);
  732. remaining -= 32; ptr += 32;
  733. /* Parse u8 n_extensions */
  734. CHECK_REMAINING(1, truncated);
  735. obj->n_extensions = (trunnel_get_uint8(ptr));
  736. remaining -= 1; ptr += 1;
  737. /* Parse struct ed25519_cert_extension ext[n_extensions] */
  738. TRUNNEL_DYNARRAY_EXPAND(ed25519_cert_extension_t *, &obj->ext, obj->n_extensions, {});
  739. {
  740. ed25519_cert_extension_t * elt;
  741. unsigned idx;
  742. for (idx = 0; idx < obj->n_extensions; ++idx) {
  743. result = ed25519_cert_extension_parse(&elt, ptr, remaining);
  744. if (result < 0)
  745. goto relay_fail;
  746. trunnel_assert((size_t)result <= remaining);
  747. remaining -= result; ptr += result;
  748. TRUNNEL_DYNARRAY_ADD(ed25519_cert_extension_t *, &obj->ext, elt, {ed25519_cert_extension_free(elt);});
  749. }
  750. }
  751. /* Parse u8 signature[64] */
  752. CHECK_REMAINING(64, truncated);
  753. memcpy(obj->signature, ptr, 64);
  754. remaining -= 64; ptr += 64;
  755. trunnel_assert(ptr + remaining == input + len_in);
  756. return len_in - remaining;
  757. truncated:
  758. return -2;
  759. relay_fail:
  760. trunnel_assert(result < 0);
  761. return result;
  762. trunnel_alloc_failed:
  763. return -1;
  764. fail:
  765. result = -1;
  766. return result;
  767. }
  768. ssize_t
  769. ed25519_cert_parse(ed25519_cert_t **output, const uint8_t *input, const size_t len_in)
  770. {
  771. ssize_t result;
  772. *output = ed25519_cert_new();
  773. if (NULL == *output)
  774. return -1;
  775. result = ed25519_cert_parse_into(*output, input, len_in);
  776. if (result < 0) {
  777. ed25519_cert_free(*output);
  778. *output = NULL;
  779. }
  780. return result;
  781. }