1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891 |
- /* link_handshake.c -- generated by Trunnel v1.4.6.
- * https://gitweb.torproject.org/trunnel.git
- * You probably shouldn't edit this file.
- */
- #include <stdlib.h>
- #include "trunnel-impl.h"
- #include "link_handshake.h"
- #define TRUNNEL_SET_ERROR_CODE(obj) \
- do { \
- (obj)->trunnel_error_code_ = 1; \
- } while (0)
- #if defined(__COVERITY__) || defined(__clang_analyzer__)
- /* If we're runnning a static analysis tool, we don't want it to complain
- * that some of our remaining-bytes checks are dead-code. */
- int linkhandshake_deadcode_dummy__ = 0;
- #define OR_DEADCODE_DUMMY || linkhandshake_deadcode_dummy__
- #else
- #define OR_DEADCODE_DUMMY
- #endif
- #define CHECK_REMAINING(nbytes, label) \
- do { \
- if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
- goto label; \
- } \
- } while (0)
- auth_challenge_cell_t *
- auth_challenge_cell_new(void)
- {
- auth_challenge_cell_t *val = trunnel_calloc(1, sizeof(auth_challenge_cell_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- auth_challenge_cell_clear(auth_challenge_cell_t *obj)
- {
- (void) obj;
- TRUNNEL_DYNARRAY_WIPE(&obj->methods);
- TRUNNEL_DYNARRAY_CLEAR(&obj->methods);
- }
- void
- auth_challenge_cell_free(auth_challenge_cell_t *obj)
- {
- if (obj == NULL)
- return;
- auth_challenge_cell_clear(obj);
- trunnel_memwipe(obj, sizeof(auth_challenge_cell_t));
- trunnel_free_(obj);
- }
- size_t
- auth_challenge_cell_getlen_challenge(const auth_challenge_cell_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth_challenge_cell_get_challenge(const auth_challenge_cell_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->challenge[idx];
- }
- int
- auth_challenge_cell_set_challenge(auth_challenge_cell_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->challenge[idx] = elt;
- return 0;
- }
- uint8_t *
- auth_challenge_cell_getarray_challenge(auth_challenge_cell_t *inp)
- {
- return inp->challenge;
- }
- uint16_t
- auth_challenge_cell_get_n_methods(auth_challenge_cell_t *inp)
- {
- return inp->n_methods;
- }
- int
- auth_challenge_cell_set_n_methods(auth_challenge_cell_t *inp, uint16_t val)
- {
- inp->n_methods = val;
- return 0;
- }
- size_t
- auth_challenge_cell_getlen_methods(const auth_challenge_cell_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->methods);
- }
- uint16_t
- auth_challenge_cell_get_methods(auth_challenge_cell_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->methods, idx);
- }
- int
- auth_challenge_cell_set_methods(auth_challenge_cell_t *inp, size_t idx, uint16_t elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->methods, idx, elt);
- return 0;
- }
- int
- auth_challenge_cell_add_methods(auth_challenge_cell_t *inp, uint16_t elt)
- {
- #if SIZE_MAX >= UINT16_MAX
- if (inp->methods.n_ == UINT16_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(uint16_t, &inp->methods, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- uint16_t *
- auth_challenge_cell_getarray_methods(auth_challenge_cell_t *inp)
- {
- return inp->methods.elts_;
- }
- int
- auth_challenge_cell_setlen_methods(auth_challenge_cell_t *inp, size_t newlen)
- {
- uint16_t *newptr;
- #if UINT16_MAX < SIZE_MAX
- if (newlen > UINT16_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->methods.allocated_,
- &inp->methods.n_, inp->methods.elts_, newlen,
- sizeof(inp->methods.elts_[0]), (trunnel_free_fn_t) NULL,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->methods.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- auth_challenge_cell_check(const auth_challenge_cell_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- if (TRUNNEL_DYNARRAY_LEN(&obj->methods) != obj->n_methods)
- return "Length mismatch for methods";
- return NULL;
- }
- ssize_t
- auth_challenge_cell_encoded_len(const auth_challenge_cell_t *obj)
- {
- ssize_t result = 0;
- if (NULL != auth_challenge_cell_check(obj))
- return -1;
- /* Length of u8 challenge[32] */
- result += 32;
- /* Length of u16 n_methods */
- result += 2;
- /* Length of u16 methods[n_methods] */
- result += 2 * TRUNNEL_DYNARRAY_LEN(&obj->methods);
- return result;
- }
- int
- auth_challenge_cell_clear_errors(auth_challenge_cell_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- auth_challenge_cell_encode(uint8_t *output, const size_t avail, const auth_challenge_cell_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = auth_challenge_cell_encoded_len(obj);
- #endif
- if (NULL != (msg = auth_challenge_cell_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 challenge[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->challenge, 32);
- written += 32; ptr += 32;
- /* Encode u16 n_methods */
- trunnel_assert(written <= avail);
- if (avail - written < 2)
- goto truncated;
- trunnel_set_uint16(ptr, trunnel_htons(obj->n_methods));
- written += 2; ptr += 2;
- /* Encode u16 methods[n_methods] */
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->methods); ++idx) {
- trunnel_assert(written <= avail);
- if (avail - written < 2)
- goto truncated;
- trunnel_set_uint16(ptr, trunnel_htons(TRUNNEL_DYNARRAY_GET(&obj->methods, idx)));
- written += 2; ptr += 2;
- }
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As auth_challenge_cell_parse(), but do not allocate the output
- * object.
- */
- static ssize_t
- auth_challenge_cell_parse_into(auth_challenge_cell_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 challenge[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->challenge, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u16 n_methods */
- CHECK_REMAINING(2, truncated);
- obj->n_methods = trunnel_ntohs(trunnel_get_uint16(ptr));
- remaining -= 2; ptr += 2;
- /* Parse u16 methods[n_methods] */
- TRUNNEL_DYNARRAY_EXPAND(uint16_t, &obj->methods, obj->n_methods, {});
- {
- uint16_t elt;
- unsigned idx;
- for (idx = 0; idx < obj->n_methods; ++idx) {
- CHECK_REMAINING(2, truncated);
- elt = trunnel_ntohs(trunnel_get_uint16(ptr));
- remaining -= 2; ptr += 2;
- TRUNNEL_DYNARRAY_ADD(uint16_t, &obj->methods, elt, {});
- }
- }
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- auth_challenge_cell_parse(auth_challenge_cell_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = auth_challenge_cell_new();
- if (NULL == *output)
- return -1;
- result = auth_challenge_cell_parse_into(*output, input, len_in);
- if (result < 0) {
- auth_challenge_cell_free(*output);
- *output = NULL;
- }
- return result;
- }
- auth_ctx_t *
- auth_ctx_new(void)
- {
- auth_ctx_t *val = trunnel_calloc(1, sizeof(auth_ctx_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- auth_ctx_clear(auth_ctx_t *obj)
- {
- (void) obj;
- }
- void
- auth_ctx_free(auth_ctx_t *obj)
- {
- if (obj == NULL)
- return;
- auth_ctx_clear(obj);
- trunnel_memwipe(obj, sizeof(auth_ctx_t));
- trunnel_free_(obj);
- }
- uint8_t
- auth_ctx_get_is_ed(auth_ctx_t *inp)
- {
- return inp->is_ed;
- }
- int
- auth_ctx_set_is_ed(auth_ctx_t *inp, uint8_t val)
- {
- inp->is_ed = val;
- return 0;
- }
- certs_cell_cert_t *
- certs_cell_cert_new(void)
- {
- certs_cell_cert_t *val = trunnel_calloc(1, sizeof(certs_cell_cert_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- certs_cell_cert_clear(certs_cell_cert_t *obj)
- {
- (void) obj;
- TRUNNEL_DYNARRAY_WIPE(&obj->body);
- TRUNNEL_DYNARRAY_CLEAR(&obj->body);
- }
- void
- certs_cell_cert_free(certs_cell_cert_t *obj)
- {
- if (obj == NULL)
- return;
- certs_cell_cert_clear(obj);
- trunnel_memwipe(obj, sizeof(certs_cell_cert_t));
- trunnel_free_(obj);
- }
- uint8_t
- certs_cell_cert_get_cert_type(certs_cell_cert_t *inp)
- {
- return inp->cert_type;
- }
- int
- certs_cell_cert_set_cert_type(certs_cell_cert_t *inp, uint8_t val)
- {
- inp->cert_type = val;
- return 0;
- }
- uint16_t
- certs_cell_cert_get_cert_len(certs_cell_cert_t *inp)
- {
- return inp->cert_len;
- }
- int
- certs_cell_cert_set_cert_len(certs_cell_cert_t *inp, uint16_t val)
- {
- inp->cert_len = val;
- return 0;
- }
- size_t
- certs_cell_cert_getlen_body(const certs_cell_cert_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->body);
- }
- uint8_t
- certs_cell_cert_get_body(certs_cell_cert_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->body, idx);
- }
- int
- certs_cell_cert_set_body(certs_cell_cert_t *inp, size_t idx, uint8_t elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->body, idx, elt);
- return 0;
- }
- int
- certs_cell_cert_add_body(certs_cell_cert_t *inp, uint8_t elt)
- {
- #if SIZE_MAX >= UINT16_MAX
- if (inp->body.n_ == UINT16_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->body, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- uint8_t *
- certs_cell_cert_getarray_body(certs_cell_cert_t *inp)
- {
- return inp->body.elts_;
- }
- int
- certs_cell_cert_setlen_body(certs_cell_cert_t *inp, size_t newlen)
- {
- uint8_t *newptr;
- #if UINT16_MAX < SIZE_MAX
- if (newlen > UINT16_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->body.allocated_,
- &inp->body.n_, inp->body.elts_, newlen,
- sizeof(inp->body.elts_[0]), (trunnel_free_fn_t) NULL,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->body.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- certs_cell_cert_check(const certs_cell_cert_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- if (TRUNNEL_DYNARRAY_LEN(&obj->body) != obj->cert_len)
- return "Length mismatch for body";
- return NULL;
- }
- ssize_t
- certs_cell_cert_encoded_len(const certs_cell_cert_t *obj)
- {
- ssize_t result = 0;
- if (NULL != certs_cell_cert_check(obj))
- return -1;
- /* Length of u8 cert_type */
- result += 1;
- /* Length of u16 cert_len */
- result += 2;
- /* Length of u8 body[cert_len] */
- result += TRUNNEL_DYNARRAY_LEN(&obj->body);
- return result;
- }
- int
- certs_cell_cert_clear_errors(certs_cell_cert_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- certs_cell_cert_encode(uint8_t *output, const size_t avail, const certs_cell_cert_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = certs_cell_cert_encoded_len(obj);
- #endif
- if (NULL != (msg = certs_cell_cert_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 cert_type */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->cert_type));
- written += 1; ptr += 1;
- /* Encode u16 cert_len */
- trunnel_assert(written <= avail);
- if (avail - written < 2)
- goto truncated;
- trunnel_set_uint16(ptr, trunnel_htons(obj->cert_len));
- written += 2; ptr += 2;
- /* Encode u8 body[cert_len] */
- {
- size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->body);
- trunnel_assert(obj->cert_len == elt_len);
- trunnel_assert(written <= avail);
- if (avail - written < elt_len)
- goto truncated;
- if (elt_len)
- memcpy(ptr, obj->body.elts_, elt_len);
- written += elt_len; ptr += elt_len;
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As certs_cell_cert_parse(), but do not allocate the output object.
- */
- static ssize_t
- certs_cell_cert_parse_into(certs_cell_cert_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 cert_type */
- CHECK_REMAINING(1, truncated);
- obj->cert_type = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse u16 cert_len */
- CHECK_REMAINING(2, truncated);
- obj->cert_len = trunnel_ntohs(trunnel_get_uint16(ptr));
- remaining -= 2; ptr += 2;
- /* Parse u8 body[cert_len] */
- CHECK_REMAINING(obj->cert_len, truncated);
- TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->body, obj->cert_len, {});
- obj->body.n_ = obj->cert_len;
- if (obj->cert_len)
- memcpy(obj->body.elts_, ptr, obj->cert_len);
- ptr += obj->cert_len; remaining -= obj->cert_len;
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- certs_cell_cert_parse(certs_cell_cert_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = certs_cell_cert_new();
- if (NULL == *output)
- return -1;
- result = certs_cell_cert_parse_into(*output, input, len_in);
- if (result < 0) {
- certs_cell_cert_free(*output);
- *output = NULL;
- }
- return result;
- }
- rsa_ed_crosscert_t *
- rsa_ed_crosscert_new(void)
- {
- rsa_ed_crosscert_t *val = trunnel_calloc(1, sizeof(rsa_ed_crosscert_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- rsa_ed_crosscert_clear(rsa_ed_crosscert_t *obj)
- {
- (void) obj;
- TRUNNEL_DYNARRAY_WIPE(&obj->sig);
- TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
- }
- void
- rsa_ed_crosscert_free(rsa_ed_crosscert_t *obj)
- {
- if (obj == NULL)
- return;
- rsa_ed_crosscert_clear(obj);
- trunnel_memwipe(obj, sizeof(rsa_ed_crosscert_t));
- trunnel_free_(obj);
- }
- size_t
- rsa_ed_crosscert_getlen_ed_key(const rsa_ed_crosscert_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- rsa_ed_crosscert_get_ed_key(const rsa_ed_crosscert_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->ed_key[idx];
- }
- int
- rsa_ed_crosscert_set_ed_key(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->ed_key[idx] = elt;
- return 0;
- }
- uint8_t *
- rsa_ed_crosscert_getarray_ed_key(rsa_ed_crosscert_t *inp)
- {
- return inp->ed_key;
- }
- uint32_t
- rsa_ed_crosscert_get_expiration(rsa_ed_crosscert_t *inp)
- {
- return inp->expiration;
- }
- int
- rsa_ed_crosscert_set_expiration(rsa_ed_crosscert_t *inp, uint32_t val)
- {
- inp->expiration = val;
- return 0;
- }
- const uint8_t *
- rsa_ed_crosscert_get_end_of_signed(const rsa_ed_crosscert_t *inp)
- {
- return inp->end_of_signed;
- }
- uint8_t
- rsa_ed_crosscert_get_sig_len(rsa_ed_crosscert_t *inp)
- {
- return inp->sig_len;
- }
- int
- rsa_ed_crosscert_set_sig_len(rsa_ed_crosscert_t *inp, uint8_t val)
- {
- inp->sig_len = val;
- return 0;
- }
- size_t
- rsa_ed_crosscert_getlen_sig(const rsa_ed_crosscert_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->sig);
- }
- uint8_t
- rsa_ed_crosscert_get_sig(rsa_ed_crosscert_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
- }
- int
- rsa_ed_crosscert_set_sig(rsa_ed_crosscert_t *inp, size_t idx, uint8_t elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
- return 0;
- }
- int
- rsa_ed_crosscert_add_sig(rsa_ed_crosscert_t *inp, uint8_t elt)
- {
- #if SIZE_MAX >= UINT8_MAX
- if (inp->sig.n_ == UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- uint8_t *
- rsa_ed_crosscert_getarray_sig(rsa_ed_crosscert_t *inp)
- {
- return inp->sig.elts_;
- }
- int
- rsa_ed_crosscert_setlen_sig(rsa_ed_crosscert_t *inp, size_t newlen)
- {
- uint8_t *newptr;
- #if UINT8_MAX < SIZE_MAX
- if (newlen > UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
- &inp->sig.n_, inp->sig.elts_, newlen,
- sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->sig.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- rsa_ed_crosscert_check(const rsa_ed_crosscert_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- if (TRUNNEL_DYNARRAY_LEN(&obj->sig) != obj->sig_len)
- return "Length mismatch for sig";
- return NULL;
- }
- ssize_t
- rsa_ed_crosscert_encoded_len(const rsa_ed_crosscert_t *obj)
- {
- ssize_t result = 0;
- if (NULL != rsa_ed_crosscert_check(obj))
- return -1;
- /* Length of u8 ed_key[32] */
- result += 32;
- /* Length of u32 expiration */
- result += 4;
- /* Length of u8 sig_len */
- result += 1;
- /* Length of u8 sig[sig_len] */
- result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
- return result;
- }
- int
- rsa_ed_crosscert_clear_errors(rsa_ed_crosscert_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- rsa_ed_crosscert_encode(uint8_t *output, const size_t avail, const rsa_ed_crosscert_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = rsa_ed_crosscert_encoded_len(obj);
- #endif
- if (NULL != (msg = rsa_ed_crosscert_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 ed_key[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->ed_key, 32);
- written += 32; ptr += 32;
- /* Encode u32 expiration */
- trunnel_assert(written <= avail);
- if (avail - written < 4)
- goto truncated;
- trunnel_set_uint32(ptr, trunnel_htonl(obj->expiration));
- written += 4; ptr += 4;
- /* Encode u8 sig_len */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->sig_len));
- written += 1; ptr += 1;
- /* Encode u8 sig[sig_len] */
- {
- size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
- trunnel_assert(obj->sig_len == elt_len);
- trunnel_assert(written <= avail);
- if (avail - written < elt_len)
- goto truncated;
- if (elt_len)
- memcpy(ptr, obj->sig.elts_, elt_len);
- written += elt_len; ptr += elt_len;
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As rsa_ed_crosscert_parse(), but do not allocate the output
- * object.
- */
- static ssize_t
- rsa_ed_crosscert_parse_into(rsa_ed_crosscert_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 ed_key[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->ed_key, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u32 expiration */
- CHECK_REMAINING(4, truncated);
- obj->expiration = trunnel_ntohl(trunnel_get_uint32(ptr));
- remaining -= 4; ptr += 4;
- obj->end_of_signed = ptr;
- /* Parse u8 sig_len */
- CHECK_REMAINING(1, truncated);
- obj->sig_len = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse u8 sig[sig_len] */
- CHECK_REMAINING(obj->sig_len, truncated);
- TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, obj->sig_len, {});
- obj->sig.n_ = obj->sig_len;
- if (obj->sig_len)
- memcpy(obj->sig.elts_, ptr, obj->sig_len);
- ptr += obj->sig_len; remaining -= obj->sig_len;
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- rsa_ed_crosscert_parse(rsa_ed_crosscert_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = rsa_ed_crosscert_new();
- if (NULL == *output)
- return -1;
- result = rsa_ed_crosscert_parse_into(*output, input, len_in);
- if (result < 0) {
- rsa_ed_crosscert_free(*output);
- *output = NULL;
- }
- return result;
- }
- auth1_t *
- auth1_new(void)
- {
- auth1_t *val = trunnel_calloc(1, sizeof(auth1_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- auth1_clear(auth1_t *obj)
- {
- (void) obj;
- TRUNNEL_DYNARRAY_WIPE(&obj->sig);
- TRUNNEL_DYNARRAY_CLEAR(&obj->sig);
- }
- void
- auth1_free(auth1_t *obj)
- {
- if (obj == NULL)
- return;
- auth1_clear(obj);
- trunnel_memwipe(obj, sizeof(auth1_t));
- trunnel_free_(obj);
- }
- size_t
- auth1_getlen_type(const auth1_t *inp)
- {
- (void)inp; return 8;
- }
- uint8_t
- auth1_get_type(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 8);
- return inp->type[idx];
- }
- int
- auth1_set_type(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 8);
- inp->type[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_type(auth1_t *inp)
- {
- return inp->type;
- }
- size_t
- auth1_getlen_cid(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_cid(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->cid[idx];
- }
- int
- auth1_set_cid(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->cid[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_cid(auth1_t *inp)
- {
- return inp->cid;
- }
- size_t
- auth1_getlen_sid(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_sid(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->sid[idx];
- }
- int
- auth1_set_sid(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->sid[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_sid(auth1_t *inp)
- {
- return inp->sid;
- }
- size_t
- auth1_getlen_u1_cid_ed(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_u1_cid_ed(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->u1_cid_ed[idx];
- }
- int
- auth1_set_u1_cid_ed(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->u1_cid_ed[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_u1_cid_ed(auth1_t *inp)
- {
- return inp->u1_cid_ed;
- }
- size_t
- auth1_getlen_u1_sid_ed(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_u1_sid_ed(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->u1_sid_ed[idx];
- }
- int
- auth1_set_u1_sid_ed(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->u1_sid_ed[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_u1_sid_ed(auth1_t *inp)
- {
- return inp->u1_sid_ed;
- }
- size_t
- auth1_getlen_slog(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_slog(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->slog[idx];
- }
- int
- auth1_set_slog(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->slog[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_slog(auth1_t *inp)
- {
- return inp->slog;
- }
- size_t
- auth1_getlen_clog(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_clog(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->clog[idx];
- }
- int
- auth1_set_clog(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->clog[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_clog(auth1_t *inp)
- {
- return inp->clog;
- }
- size_t
- auth1_getlen_scert(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_scert(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->scert[idx];
- }
- int
- auth1_set_scert(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->scert[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_scert(auth1_t *inp)
- {
- return inp->scert;
- }
- size_t
- auth1_getlen_tlssecrets(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_tlssecrets(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->tlssecrets[idx];
- }
- int
- auth1_set_tlssecrets(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 32);
- inp->tlssecrets[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_tlssecrets(auth1_t *inp)
- {
- return inp->tlssecrets;
- }
- const uint8_t *
- auth1_get_end_of_fixed_part(const auth1_t *inp)
- {
- return inp->end_of_fixed_part;
- }
- size_t
- auth1_getlen_rand(const auth1_t *inp)
- {
- (void)inp; return 24;
- }
- uint8_t
- auth1_get_rand(const auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 24);
- return inp->rand[idx];
- }
- int
- auth1_set_rand(auth1_t *inp, size_t idx, uint8_t elt)
- {
- trunnel_assert(idx < 24);
- inp->rand[idx] = elt;
- return 0;
- }
- uint8_t *
- auth1_getarray_rand(auth1_t *inp)
- {
- return inp->rand;
- }
- const uint8_t *
- auth1_get_end_of_signed(const auth1_t *inp)
- {
- return inp->end_of_signed;
- }
- size_t
- auth1_getlen_sig(const auth1_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->sig);
- }
- uint8_t
- auth1_get_sig(auth1_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->sig, idx);
- }
- int
- auth1_set_sig(auth1_t *inp, size_t idx, uint8_t elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->sig, idx, elt);
- return 0;
- }
- int
- auth1_add_sig(auth1_t *inp, uint8_t elt)
- {
- TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->sig, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- uint8_t *
- auth1_getarray_sig(auth1_t *inp)
- {
- return inp->sig.elts_;
- }
- int
- auth1_setlen_sig(auth1_t *inp, size_t newlen)
- {
- uint8_t *newptr;
- newptr = trunnel_dynarray_setlen(&inp->sig.allocated_,
- &inp->sig.n_, inp->sig.elts_, newlen,
- sizeof(inp->sig.elts_[0]), (trunnel_free_fn_t) NULL,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->sig.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- auth1_check(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- if (auth_ctx_ctx == NULL)
- return "Context was NULL";
- switch (auth_ctx_ctx->is_ed) {
- case 0:
- break;
- case 1:
- break;
- default:
- return "Bad tag for union";
- break;
- }
- return NULL;
- }
- ssize_t
- auth1_encoded_len(const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
- {
- ssize_t result = 0;
- if (NULL != auth1_check(obj, auth_ctx_ctx))
- return -1;
- /* Length of u8 type[8] */
- result += 8;
- /* Length of u8 cid[32] */
- result += 32;
- /* Length of u8 sid[32] */
- result += 32;
- switch (auth_ctx_ctx->is_ed) {
- case 0:
- break;
- case 1:
- /* Length of u8 u1_cid_ed[32] */
- result += 32;
- /* Length of u8 u1_sid_ed[32] */
- result += 32;
- break;
- default:
- trunnel_assert(0);
- break;
- }
- /* Length of u8 slog[32] */
- result += 32;
- /* Length of u8 clog[32] */
- result += 32;
- /* Length of u8 scert[32] */
- result += 32;
- /* Length of u8 tlssecrets[32] */
- result += 32;
- /* Length of u8 rand[24] */
- result += 24;
- /* Length of u8 sig[] */
- result += TRUNNEL_DYNARRAY_LEN(&obj->sig);
- return result;
- }
- int
- auth1_clear_errors(auth1_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- auth1_encode(uint8_t *output, const size_t avail, const auth1_t *obj, const auth_ctx_t *auth_ctx_ctx)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = auth1_encoded_len(obj, auth_ctx_ctx);
- #endif
- if (NULL != (msg = auth1_check(obj, auth_ctx_ctx)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 type[8] */
- trunnel_assert(written <= avail);
- if (avail - written < 8)
- goto truncated;
- memcpy(ptr, obj->type, 8);
- written += 8; ptr += 8;
- /* Encode u8 cid[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->cid, 32);
- written += 32; ptr += 32;
- /* Encode u8 sid[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->sid, 32);
- written += 32; ptr += 32;
- /* Encode union u1[auth_ctx.is_ed] */
- trunnel_assert(written <= avail);
- switch (auth_ctx_ctx->is_ed) {
- case 0:
- break;
- case 1:
- /* Encode u8 u1_cid_ed[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->u1_cid_ed, 32);
- written += 32; ptr += 32;
- /* Encode u8 u1_sid_ed[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->u1_sid_ed, 32);
- written += 32; ptr += 32;
- break;
- default:
- trunnel_assert(0);
- break;
- }
- /* Encode u8 slog[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->slog, 32);
- written += 32; ptr += 32;
- /* Encode u8 clog[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->clog, 32);
- written += 32; ptr += 32;
- /* Encode u8 scert[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->scert, 32);
- written += 32; ptr += 32;
- /* Encode u8 tlssecrets[32] */
- trunnel_assert(written <= avail);
- if (avail - written < 32)
- goto truncated;
- memcpy(ptr, obj->tlssecrets, 32);
- written += 32; ptr += 32;
- /* Encode u8 rand[24] */
- trunnel_assert(written <= avail);
- if (avail - written < 24)
- goto truncated;
- memcpy(ptr, obj->rand, 24);
- written += 24; ptr += 24;
- /* Encode u8 sig[] */
- {
- size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->sig);
- trunnel_assert(written <= avail);
- if (avail - written < elt_len)
- goto truncated;
- if (elt_len)
- memcpy(ptr, obj->sig.elts_, elt_len);
- written += elt_len; ptr += elt_len;
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As auth1_parse(), but do not allocate the output object.
- */
- static ssize_t
- auth1_parse_into(auth1_t *obj, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- if (auth_ctx_ctx == NULL)
- return -1;
- /* Parse u8 type[8] */
- CHECK_REMAINING(8, truncated);
- memcpy(obj->type, ptr, 8);
- remaining -= 8; ptr += 8;
- /* Parse u8 cid[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->cid, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u8 sid[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->sid, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse union u1[auth_ctx.is_ed] */
- switch (auth_ctx_ctx->is_ed) {
- case 0:
- break;
- case 1:
- /* Parse u8 u1_cid_ed[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->u1_cid_ed, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u8 u1_sid_ed[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->u1_sid_ed, ptr, 32);
- remaining -= 32; ptr += 32;
- break;
- default:
- goto fail;
- break;
- }
- /* Parse u8 slog[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->slog, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u8 clog[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->clog, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u8 scert[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->scert, ptr, 32);
- remaining -= 32; ptr += 32;
- /* Parse u8 tlssecrets[32] */
- CHECK_REMAINING(32, truncated);
- memcpy(obj->tlssecrets, ptr, 32);
- remaining -= 32; ptr += 32;
- obj->end_of_fixed_part = ptr;
- /* Parse u8 rand[24] */
- CHECK_REMAINING(24, truncated);
- memcpy(obj->rand, ptr, 24);
- remaining -= 24; ptr += 24;
- obj->end_of_signed = ptr;
- /* Parse u8 sig[] */
- TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->sig, remaining, {});
- obj->sig.n_ = remaining;
- if (remaining)
- memcpy(obj->sig.elts_, ptr, remaining);
- ptr += remaining; remaining -= remaining;
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- trunnel_alloc_failed:
- return -1;
- fail:
- result = -1;
- return result;
- }
- ssize_t
- auth1_parse(auth1_t **output, const uint8_t *input, const size_t len_in, const auth_ctx_t *auth_ctx_ctx)
- {
- ssize_t result;
- *output = auth1_new();
- if (NULL == *output)
- return -1;
- result = auth1_parse_into(*output, input, len_in, auth_ctx_ctx);
- if (result < 0) {
- auth1_free(*output);
- *output = NULL;
- }
- return result;
- }
- certs_cell_t *
- certs_cell_new(void)
- {
- certs_cell_t *val = trunnel_calloc(1, sizeof(certs_cell_t));
- if (NULL == val)
- return NULL;
- return val;
- }
- /** Release all storage held inside 'obj', but do not free 'obj'.
- */
- static void
- certs_cell_clear(certs_cell_t *obj)
- {
- (void) obj;
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
- certs_cell_cert_free(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
- }
- }
- TRUNNEL_DYNARRAY_WIPE(&obj->certs);
- TRUNNEL_DYNARRAY_CLEAR(&obj->certs);
- }
- void
- certs_cell_free(certs_cell_t *obj)
- {
- if (obj == NULL)
- return;
- certs_cell_clear(obj);
- trunnel_memwipe(obj, sizeof(certs_cell_t));
- trunnel_free_(obj);
- }
- uint8_t
- certs_cell_get_n_certs(certs_cell_t *inp)
- {
- return inp->n_certs;
- }
- int
- certs_cell_set_n_certs(certs_cell_t *inp, uint8_t val)
- {
- inp->n_certs = val;
- return 0;
- }
- size_t
- certs_cell_getlen_certs(const certs_cell_t *inp)
- {
- return TRUNNEL_DYNARRAY_LEN(&inp->certs);
- }
- struct certs_cell_cert_st *
- certs_cell_get_certs(certs_cell_t *inp, size_t idx)
- {
- return TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
- }
- int
- certs_cell_set_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
- {
- certs_cell_cert_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->certs, idx);
- if (oldval && oldval != elt)
- certs_cell_cert_free(oldval);
- return certs_cell_set0_certs(inp, idx, elt);
- }
- int
- certs_cell_set0_certs(certs_cell_t *inp, size_t idx, struct certs_cell_cert_st * elt)
- {
- TRUNNEL_DYNARRAY_SET(&inp->certs, idx, elt);
- return 0;
- }
- int
- certs_cell_add_certs(certs_cell_t *inp, struct certs_cell_cert_st * elt)
- {
- #if SIZE_MAX >= UINT8_MAX
- if (inp->certs.n_ == UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- TRUNNEL_DYNARRAY_ADD(struct certs_cell_cert_st *, &inp->certs, elt, {});
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- struct certs_cell_cert_st * *
- certs_cell_getarray_certs(certs_cell_t *inp)
- {
- return inp->certs.elts_;
- }
- int
- certs_cell_setlen_certs(certs_cell_t *inp, size_t newlen)
- {
- struct certs_cell_cert_st * *newptr;
- #if UINT8_MAX < SIZE_MAX
- if (newlen > UINT8_MAX)
- goto trunnel_alloc_failed;
- #endif
- newptr = trunnel_dynarray_setlen(&inp->certs.allocated_,
- &inp->certs.n_, inp->certs.elts_, newlen,
- sizeof(inp->certs.elts_[0]), (trunnel_free_fn_t) certs_cell_cert_free,
- &inp->trunnel_error_code_);
- if (newlen != 0 && newptr == NULL)
- goto trunnel_alloc_failed;
- inp->certs.elts_ = newptr;
- return 0;
- trunnel_alloc_failed:
- TRUNNEL_SET_ERROR_CODE(inp);
- return -1;
- }
- const char *
- certs_cell_check(const certs_cell_t *obj)
- {
- if (obj == NULL)
- return "Object was NULL";
- if (obj->trunnel_error_code_)
- return "A set function failed on this object";
- {
- const char *msg;
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
- if (NULL != (msg = certs_cell_cert_check(TRUNNEL_DYNARRAY_GET(&obj->certs, idx))))
- return msg;
- }
- }
- if (TRUNNEL_DYNARRAY_LEN(&obj->certs) != obj->n_certs)
- return "Length mismatch for certs";
- return NULL;
- }
- ssize_t
- certs_cell_encoded_len(const certs_cell_t *obj)
- {
- ssize_t result = 0;
- if (NULL != certs_cell_check(obj))
- return -1;
- /* Length of u8 n_certs */
- result += 1;
- /* Length of struct certs_cell_cert certs[n_certs] */
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
- result += certs_cell_cert_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
- }
- }
- return result;
- }
- int
- certs_cell_clear_errors(certs_cell_t *obj)
- {
- int r = obj->trunnel_error_code_;
- obj->trunnel_error_code_ = 0;
- return r;
- }
- ssize_t
- certs_cell_encode(uint8_t *output, const size_t avail, const certs_cell_t *obj)
- {
- ssize_t result = 0;
- size_t written = 0;
- uint8_t *ptr = output;
- const char *msg;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- const ssize_t encoded_len = certs_cell_encoded_len(obj);
- #endif
- if (NULL != (msg = certs_cell_check(obj)))
- goto check_failed;
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- trunnel_assert(encoded_len >= 0);
- #endif
- /* Encode u8 n_certs */
- trunnel_assert(written <= avail);
- if (avail - written < 1)
- goto truncated;
- trunnel_set_uint8(ptr, (obj->n_certs));
- written += 1; ptr += 1;
- /* Encode struct certs_cell_cert certs[n_certs] */
- {
- unsigned idx;
- for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->certs); ++idx) {
- trunnel_assert(written <= avail);
- result = certs_cell_cert_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->certs, idx));
- if (result < 0)
- goto fail; /* XXXXXXX !*/
- written += result; ptr += result;
- }
- }
- trunnel_assert(ptr == output + written);
- #ifdef TRUNNEL_CHECK_ENCODED_LEN
- {
- trunnel_assert(encoded_len >= 0);
- trunnel_assert((size_t)encoded_len == written);
- }
- #endif
- return written;
- truncated:
- result = -2;
- goto fail;
- check_failed:
- (void)msg;
- result = -1;
- goto fail;
- fail:
- trunnel_assert(result < 0);
- return result;
- }
- /** As certs_cell_parse(), but do not allocate the output object.
- */
- static ssize_t
- certs_cell_parse_into(certs_cell_t *obj, const uint8_t *input, const size_t len_in)
- {
- const uint8_t *ptr = input;
- size_t remaining = len_in;
- ssize_t result = 0;
- (void)result;
- /* Parse u8 n_certs */
- CHECK_REMAINING(1, truncated);
- obj->n_certs = (trunnel_get_uint8(ptr));
- remaining -= 1; ptr += 1;
- /* Parse struct certs_cell_cert certs[n_certs] */
- TRUNNEL_DYNARRAY_EXPAND(certs_cell_cert_t *, &obj->certs, obj->n_certs, {});
- {
- certs_cell_cert_t * elt;
- unsigned idx;
- for (idx = 0; idx < obj->n_certs; ++idx) {
- result = certs_cell_cert_parse(&elt, ptr, remaining);
- if (result < 0)
- goto relay_fail;
- trunnel_assert((size_t)result <= remaining);
- remaining -= result; ptr += result;
- TRUNNEL_DYNARRAY_ADD(certs_cell_cert_t *, &obj->certs, elt, {certs_cell_cert_free(elt);});
- }
- }
- trunnel_assert(ptr + remaining == input + len_in);
- return len_in - remaining;
- truncated:
- return -2;
- relay_fail:
- trunnel_assert(result < 0);
- return result;
- trunnel_alloc_failed:
- return -1;
- }
- ssize_t
- certs_cell_parse(certs_cell_t **output, const uint8_t *input, const size_t len_in)
- {
- ssize_t result;
- *output = certs_cell_new();
- if (NULL == *output)
- return -1;
- result = certs_cell_parse_into(*output, input, len_in);
- if (result < 0) {
- certs_cell_free(*output);
- *output = NULL;
- }
- return result;
- }
|