123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061 |
- /* link_handshake.c -- generated by Trunnel v1.5.1.
- * 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(auth_challenge_cell_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->challenge[idx];
- }
- uint8_t
- auth_challenge_cell_getconst_challenge(const auth_challenge_cell_t *inp, size_t idx)
- {
- return auth_challenge_cell_get_challenge((auth_challenge_cell_t*)inp, 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;
- }
- const uint8_t *
- auth_challenge_cell_getconstarray_challenge(const auth_challenge_cell_t *inp)
- {
- return (const uint8_t *)auth_challenge_cell_getarray_challenge((auth_challenge_cell_t*)inp);
- }
- uint16_t
- auth_challenge_cell_get_n_methods(const 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);
- }
- uint16_t
- auth_challenge_cell_getconst_methods(const auth_challenge_cell_t *inp, size_t idx)
- {
- return auth_challenge_cell_get_methods((auth_challenge_cell_t*)inp, 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_;
- }
- const uint16_t *
- auth_challenge_cell_getconstarray_methods(const auth_challenge_cell_t *inp)
- {
- return (const uint16_t *)auth_challenge_cell_getarray_methods((auth_challenge_cell_t*)inp);
- }
- 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(const 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(const 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(const 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);
- }
- uint8_t
- certs_cell_cert_getconst_body(const certs_cell_cert_t *inp, size_t idx)
- {
- return certs_cell_cert_get_body((certs_cell_cert_t*)inp, 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_;
- }
- const uint8_t *
- certs_cell_cert_getconstarray_body(const certs_cell_cert_t *inp)
- {
- return (const uint8_t *)certs_cell_cert_getarray_body((certs_cell_cert_t*)inp);
- }
- 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(rsa_ed_crosscert_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->ed_key[idx];
- }
- uint8_t
- rsa_ed_crosscert_getconst_ed_key(const rsa_ed_crosscert_t *inp, size_t idx)
- {
- return rsa_ed_crosscert_get_ed_key((rsa_ed_crosscert_t*)inp, 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;
- }
- const uint8_t *
- rsa_ed_crosscert_getconstarray_ed_key(const rsa_ed_crosscert_t *inp)
- {
- return (const uint8_t *)rsa_ed_crosscert_getarray_ed_key((rsa_ed_crosscert_t*)inp);
- }
- uint32_t
- rsa_ed_crosscert_get_expiration(const 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(const 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);
- }
- uint8_t
- rsa_ed_crosscert_getconst_sig(const rsa_ed_crosscert_t *inp, size_t idx)
- {
- return rsa_ed_crosscert_get_sig((rsa_ed_crosscert_t*)inp, 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_;
- }
- const uint8_t *
- rsa_ed_crosscert_getconstarray_sig(const rsa_ed_crosscert_t *inp)
- {
- return (const uint8_t *)rsa_ed_crosscert_getarray_sig((rsa_ed_crosscert_t*)inp);
- }
- 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(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 8);
- return inp->type[idx];
- }
- uint8_t
- auth1_getconst_type(const auth1_t *inp, size_t idx)
- {
- return auth1_get_type((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_type(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_type((auth1_t*)inp);
- }
- size_t
- auth1_getlen_cid(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_cid(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->cid[idx];
- }
- uint8_t
- auth1_getconst_cid(const auth1_t *inp, size_t idx)
- {
- return auth1_get_cid((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_cid(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_cid((auth1_t*)inp);
- }
- size_t
- auth1_getlen_sid(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_sid(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->sid[idx];
- }
- uint8_t
- auth1_getconst_sid(const auth1_t *inp, size_t idx)
- {
- return auth1_get_sid((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_sid(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_sid((auth1_t*)inp);
- }
- size_t
- auth1_getlen_u1_cid_ed(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_u1_cid_ed(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->u1_cid_ed[idx];
- }
- uint8_t
- auth1_getconst_u1_cid_ed(const auth1_t *inp, size_t idx)
- {
- return auth1_get_u1_cid_ed((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_u1_cid_ed(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_u1_cid_ed((auth1_t*)inp);
- }
- size_t
- auth1_getlen_u1_sid_ed(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_u1_sid_ed(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->u1_sid_ed[idx];
- }
- uint8_t
- auth1_getconst_u1_sid_ed(const auth1_t *inp, size_t idx)
- {
- return auth1_get_u1_sid_ed((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_u1_sid_ed(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_u1_sid_ed((auth1_t*)inp);
- }
- size_t
- auth1_getlen_slog(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_slog(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->slog[idx];
- }
- uint8_t
- auth1_getconst_slog(const auth1_t *inp, size_t idx)
- {
- return auth1_get_slog((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_slog(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_slog((auth1_t*)inp);
- }
- size_t
- auth1_getlen_clog(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_clog(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->clog[idx];
- }
- uint8_t
- auth1_getconst_clog(const auth1_t *inp, size_t idx)
- {
- return auth1_get_clog((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_clog(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_clog((auth1_t*)inp);
- }
- size_t
- auth1_getlen_scert(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_scert(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->scert[idx];
- }
- uint8_t
- auth1_getconst_scert(const auth1_t *inp, size_t idx)
- {
- return auth1_get_scert((auth1_t*)inp, 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;
- }
- const uint8_t *
- auth1_getconstarray_scert(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_scert((auth1_t*)inp);
- }
- size_t
- auth1_getlen_tlssecrets(const auth1_t *inp)
- {
- (void)inp; return 32;
- }
- uint8_t
- auth1_get_tlssecrets(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 32);
- return inp->tlssecrets[idx];
- }
- uint8_t
- auth1_getconst_tlssecrets(const auth1_t *inp, size_t idx)
- {
- return auth1_get_tlssecrets((auth1_t*)inp, 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_getconstarray_tlssecrets(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_tlssecrets((auth1_t*)inp);
- }
- 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(auth1_t *inp, size_t idx)
- {
- trunnel_assert(idx < 24);
- return inp->rand[idx];
- }
- uint8_t
- auth1_getconst_rand(const auth1_t *inp, size_t idx)
- {
- return auth1_get_rand((auth1_t*)inp, 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_getconstarray_rand(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_rand((auth1_t*)inp);
- }
- 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);
- }
- uint8_t
- auth1_getconst_sig(const auth1_t *inp, size_t idx)
- {
- return auth1_get_sig((auth1_t*)inp, 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_;
- }
- const uint8_t *
- auth1_getconstarray_sig(const auth1_t *inp)
- {
- return (const uint8_t *)auth1_getarray_sig((auth1_t*)inp);
- }
- 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(const 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);
- }
- const struct certs_cell_cert_st *
- certs_cell_getconst_certs(const certs_cell_t *inp, size_t idx)
- {
- return certs_cell_get_certs((certs_cell_t*)inp, 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_;
- }
- const struct certs_cell_cert_st * const *
- certs_cell_getconstarray_certs(const certs_cell_t *inp)
- {
- return (const struct certs_cell_cert_st * const *)certs_cell_getarray_certs((certs_cell_t*)inp);
- }
- 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;
- }
|