From: Nick Mathewson Date: Mon, 2 Jun 2025 15:11:05 +0000 (-0400) Subject: Add a negotiation message for subprotocol requests. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9f05aaebf661024bb07e86b595d52869de3a77f;p=thirdparty%2Ftor.git Add a negotiation message for subprotocol requests. --- diff --git a/src/trunnel/include.am b/src/trunnel/include.am index 8154a5e932..8bb2c385e5 100644 --- a/src/trunnel/include.am +++ b/src/trunnel/include.am @@ -17,7 +17,8 @@ TRUNNELINPUTS = \ src/trunnel/congestion_control.trunnel \ src/trunnel/socks5.trunnel \ src/trunnel/circpad_negotiation.trunnel \ - src/trunnel/conflux.trunnel + src/trunnel/conflux.trunnel \ + src/trunnel/subproto_request.trunnel TRUNNELSOURCES = \ src/ext/trunnel/trunnel.c \ @@ -35,7 +36,8 @@ TRUNNELSOURCES = \ src/trunnel/socks5.c \ src/trunnel/netinfo.c \ src/trunnel/circpad_negotiation.c \ - src/trunnel/conflux.c + src/trunnel/conflux.c \ + src/trunnel/subproto_request.c TRUNNELHEADERS = \ src/ext/trunnel/trunnel.h \ @@ -55,7 +57,8 @@ TRUNNELHEADERS = \ src/trunnel/socks5.h \ src/trunnel/netinfo.h \ src/trunnel/circpad_negotiation.h \ - src/trunnel/conflux.h + src/trunnel/conflux.h \ + src/trunnel/subproto_request.h src_trunnel_libor_trunnel_a_SOURCES = $(TRUNNELSOURCES) src_trunnel_libor_trunnel_a_CPPFLAGS = \ diff --git a/src/trunnel/subproto_request.c b/src/trunnel/subproto_request.c new file mode 100644 index 0000000000..ff004556b8 --- /dev/null +++ b/src/trunnel/subproto_request.c @@ -0,0 +1,456 @@ +/* subproto_request.c -- generated by Trunnel v1.5.3. + * https://gitweb.torproject.org/trunnel.git + * You probably shouldn't edit this file. + */ +#include +#include "trunnel-impl.h" + +#include "subproto_request.h" + +#define TRUNNEL_SET_ERROR_CODE(obj) \ + do { \ + (obj)->trunnel_error_code_ = 1; \ + } while (0) + +#if defined(__COVERITY__) || defined(__clang_analyzer__) +/* If we're running a static analysis tool, we don't want it to complain + * that some of our remaining-bytes checks are dead-code. */ +int subprotorequest_deadcode_dummy__ = 0; +#define OR_DEADCODE_DUMMY || subprotorequest_deadcode_dummy__ +#else +#define OR_DEADCODE_DUMMY +#endif + +#define CHECK_REMAINING(nbytes, label) \ + do { \ + if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \ + goto label; \ + } \ + } while (0) + +trn_subproto_request_t * +trn_subproto_request_new(void) +{ + trn_subproto_request_t *val = trunnel_calloc(1, sizeof(trn_subproto_request_t)); + if (NULL == val) + return NULL; + return val; +} + +/** Release all storage held inside 'obj', but do not free 'obj'. + */ +static void +trn_subproto_request_clear(trn_subproto_request_t *obj) +{ + (void) obj; +} + +void +trn_subproto_request_free(trn_subproto_request_t *obj) +{ + if (obj == NULL) + return; + trn_subproto_request_clear(obj); + trunnel_memwipe(obj, sizeof(trn_subproto_request_t)); + trunnel_free_(obj); +} + +uint8_t +trn_subproto_request_get_protocol_id(const trn_subproto_request_t *inp) +{ + return inp->protocol_id; +} +int +trn_subproto_request_set_protocol_id(trn_subproto_request_t *inp, uint8_t val) +{ + inp->protocol_id = val; + return 0; +} +uint8_t +trn_subproto_request_get_proto_cap_number(const trn_subproto_request_t *inp) +{ + return inp->proto_cap_number; +} +int +trn_subproto_request_set_proto_cap_number(trn_subproto_request_t *inp, uint8_t val) +{ + inp->proto_cap_number = val; + return 0; +} +const char * +trn_subproto_request_check(const trn_subproto_request_t *obj) +{ + if (obj == NULL) + return "Object was NULL"; + if (obj->trunnel_error_code_) + return "A set function failed on this object"; + return NULL; +} + +ssize_t +trn_subproto_request_encoded_len(const trn_subproto_request_t *obj) +{ + ssize_t result = 0; + + if (NULL != trn_subproto_request_check(obj)) + return -1; + + + /* Length of u8 protocol_id */ + result += 1; + + /* Length of u8 proto_cap_number */ + result += 1; + return result; +} +int +trn_subproto_request_clear_errors(trn_subproto_request_t *obj) +{ + int r = obj->trunnel_error_code_; + obj->trunnel_error_code_ = 0; + return r; +} +ssize_t +trn_subproto_request_encode(uint8_t *output, const size_t avail, const trn_subproto_request_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 = trn_subproto_request_encoded_len(obj); +#endif + + if (NULL != (msg = trn_subproto_request_check(obj))) + goto check_failed; + +#ifdef TRUNNEL_CHECK_ENCODED_LEN + trunnel_assert(encoded_len >= 0); +#endif + + /* Encode u8 protocol_id */ + trunnel_assert(written <= avail); + if (avail - written < 1) + goto truncated; + trunnel_set_uint8(ptr, (obj->protocol_id)); + written += 1; ptr += 1; + + /* Encode u8 proto_cap_number */ + trunnel_assert(written <= avail); + if (avail - written < 1) + goto truncated; + trunnel_set_uint8(ptr, (obj->proto_cap_number)); + written += 1; ptr += 1; + + + 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 trn_subproto_request_parse(), but do not allocate the output + * object. + */ +static ssize_t +trn_subproto_request_parse_into(trn_subproto_request_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 protocol_id */ + CHECK_REMAINING(1, truncated); + obj->protocol_id = (trunnel_get_uint8(ptr)); + remaining -= 1; ptr += 1; + + /* Parse u8 proto_cap_number */ + CHECK_REMAINING(1, truncated); + obj->proto_cap_number = (trunnel_get_uint8(ptr)); + remaining -= 1; ptr += 1; + trunnel_assert(ptr + remaining == input + len_in); + return len_in - remaining; + + truncated: + return -2; +} + +ssize_t +trn_subproto_request_parse(trn_subproto_request_t **output, const uint8_t *input, const size_t len_in) +{ + ssize_t result; + *output = trn_subproto_request_new(); + if (NULL == *output) + return -1; + result = trn_subproto_request_parse_into(*output, input, len_in); + if (result < 0) { + trn_subproto_request_free(*output); + *output = NULL; + } + return result; +} +trn_subproto_request_ext_t * +trn_subproto_request_ext_new(void) +{ + trn_subproto_request_ext_t *val = trunnel_calloc(1, sizeof(trn_subproto_request_ext_t)); + if (NULL == val) + return NULL; + return val; +} + +/** Release all storage held inside 'obj', but do not free 'obj'. + */ +static void +trn_subproto_request_ext_clear(trn_subproto_request_ext_t *obj) +{ + (void) obj; + { + + unsigned idx; + for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->reqs); ++idx) { + trn_subproto_request_free(TRUNNEL_DYNARRAY_GET(&obj->reqs, idx)); + } + } + TRUNNEL_DYNARRAY_WIPE(&obj->reqs); + TRUNNEL_DYNARRAY_CLEAR(&obj->reqs); +} + +void +trn_subproto_request_ext_free(trn_subproto_request_ext_t *obj) +{ + if (obj == NULL) + return; + trn_subproto_request_ext_clear(obj); + trunnel_memwipe(obj, sizeof(trn_subproto_request_ext_t)); + trunnel_free_(obj); +} + +size_t +trn_subproto_request_ext_getlen_reqs(const trn_subproto_request_ext_t *inp) +{ + return TRUNNEL_DYNARRAY_LEN(&inp->reqs); +} + +struct trn_subproto_request_st * +trn_subproto_request_ext_get_reqs(trn_subproto_request_ext_t *inp, size_t idx) +{ + return TRUNNEL_DYNARRAY_GET(&inp->reqs, idx); +} + + const struct trn_subproto_request_st * +trn_subproto_request_ext_getconst_reqs(const trn_subproto_request_ext_t *inp, size_t idx) +{ + return trn_subproto_request_ext_get_reqs((trn_subproto_request_ext_t*)inp, idx); +} +int +trn_subproto_request_ext_set_reqs(trn_subproto_request_ext_t *inp, size_t idx, struct trn_subproto_request_st * elt) +{ + trn_subproto_request_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->reqs, idx); + if (oldval && oldval != elt) + trn_subproto_request_free(oldval); + return trn_subproto_request_ext_set0_reqs(inp, idx, elt); +} +int +trn_subproto_request_ext_set0_reqs(trn_subproto_request_ext_t *inp, size_t idx, struct trn_subproto_request_st * elt) +{ + TRUNNEL_DYNARRAY_SET(&inp->reqs, idx, elt); + return 0; +} +int +trn_subproto_request_ext_add_reqs(trn_subproto_request_ext_t *inp, struct trn_subproto_request_st * elt) +{ + TRUNNEL_DYNARRAY_ADD(struct trn_subproto_request_st *, &inp->reqs, elt, {}); + return 0; + trunnel_alloc_failed: + TRUNNEL_SET_ERROR_CODE(inp); + return -1; +} + +struct trn_subproto_request_st * * +trn_subproto_request_ext_getarray_reqs(trn_subproto_request_ext_t *inp) +{ + return inp->reqs.elts_; +} +const struct trn_subproto_request_st * const * +trn_subproto_request_ext_getconstarray_reqs(const trn_subproto_request_ext_t *inp) +{ + return (const struct trn_subproto_request_st * const *)trn_subproto_request_ext_getarray_reqs((trn_subproto_request_ext_t*)inp); +} +int +trn_subproto_request_ext_setlen_reqs(trn_subproto_request_ext_t *inp, size_t newlen) +{ + struct trn_subproto_request_st * *newptr; + newptr = trunnel_dynarray_setlen(&inp->reqs.allocated_, + &inp->reqs.n_, inp->reqs.elts_, newlen, + sizeof(inp->reqs.elts_[0]), (trunnel_free_fn_t) trn_subproto_request_free, + &inp->trunnel_error_code_); + if (newlen != 0 && newptr == NULL) + goto trunnel_alloc_failed; + inp->reqs.elts_ = newptr; + return 0; + trunnel_alloc_failed: + TRUNNEL_SET_ERROR_CODE(inp); + return -1; +} +const char * +trn_subproto_request_ext_check(const trn_subproto_request_ext_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->reqs); ++idx) { + if (NULL != (msg = trn_subproto_request_check(TRUNNEL_DYNARRAY_GET(&obj->reqs, idx)))) + return msg; + } + } + return NULL; +} + +ssize_t +trn_subproto_request_ext_encoded_len(const trn_subproto_request_ext_t *obj) +{ + ssize_t result = 0; + + if (NULL != trn_subproto_request_ext_check(obj)) + return -1; + + + /* Length of struct trn_subproto_request reqs[] */ + { + + unsigned idx; + for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->reqs); ++idx) { + result += trn_subproto_request_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->reqs, idx)); + } + } + return result; +} +int +trn_subproto_request_ext_clear_errors(trn_subproto_request_ext_t *obj) +{ + int r = obj->trunnel_error_code_; + obj->trunnel_error_code_ = 0; + return r; +} +ssize_t +trn_subproto_request_ext_encode(uint8_t *output, const size_t avail, const trn_subproto_request_ext_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 = trn_subproto_request_ext_encoded_len(obj); +#endif + + if (NULL != (msg = trn_subproto_request_ext_check(obj))) + goto check_failed; + +#ifdef TRUNNEL_CHECK_ENCODED_LEN + trunnel_assert(encoded_len >= 0); +#endif + + /* Encode struct trn_subproto_request reqs[] */ + { + + unsigned idx; + for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->reqs); ++idx) { + trunnel_assert(written <= avail); + result = trn_subproto_request_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->reqs, 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; + + check_failed: + (void)msg; + result = -1; + goto fail; + fail: + trunnel_assert(result < 0); + return result; +} + +/** As trn_subproto_request_ext_parse(), but do not allocate the + * output object. + */ +static ssize_t +trn_subproto_request_ext_parse_into(trn_subproto_request_ext_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 struct trn_subproto_request reqs[] */ + { + trn_subproto_request_t * elt; + while (remaining > 0) { + result = trn_subproto_request_parse(&elt, ptr, remaining); + if (result < 0) + goto fail; + trunnel_assert((size_t)result <= remaining); + remaining -= result; ptr += result; + TRUNNEL_DYNARRAY_ADD(trn_subproto_request_t *, &obj->reqs, elt, {trn_subproto_request_free(elt);}); + } + } + trunnel_assert(ptr + remaining == input + len_in); + return len_in - remaining; + + trunnel_alloc_failed: + return -1; + fail: + result = -1; + return result; +} + +ssize_t +trn_subproto_request_ext_parse(trn_subproto_request_ext_t **output, const uint8_t *input, const size_t len_in) +{ + ssize_t result; + *output = trn_subproto_request_ext_new(); + if (NULL == *output) + return -1; + result = trn_subproto_request_ext_parse_into(*output, input, len_in); + if (result < 0) { + trn_subproto_request_ext_free(*output); + *output = NULL; + } + return result; +} diff --git a/src/trunnel/subproto_request.h b/src/trunnel/subproto_request.h new file mode 100644 index 0000000000..d32f48d5e4 --- /dev/null +++ b/src/trunnel/subproto_request.h @@ -0,0 +1,157 @@ +/* subproto_request.h -- generated by Trunnel v1.5.3. + * https://gitweb.torproject.org/trunnel.git + * You probably shouldn't edit this file. + */ +#ifndef TRUNNEL_SUBPROTO_REQUEST_H +#define TRUNNEL_SUBPROTO_REQUEST_H + +#include +#include "trunnel.h" + +#if !defined(TRUNNEL_OPAQUE) && !defined(TRUNNEL_OPAQUE_TRN_SUBPROTO_REQUEST) +struct trn_subproto_request_st { + uint8_t protocol_id; + uint8_t proto_cap_number; + uint8_t trunnel_error_code_; +}; +#endif +typedef struct trn_subproto_request_st trn_subproto_request_t; +#if !defined(TRUNNEL_OPAQUE) && !defined(TRUNNEL_OPAQUE_TRN_SUBPROTO_REQUEST_EXT) +struct trn_subproto_request_ext_st { + TRUNNEL_DYNARRAY_HEAD(, struct trn_subproto_request_st *) reqs; + uint8_t trunnel_error_code_; +}; +#endif +typedef struct trn_subproto_request_ext_st trn_subproto_request_ext_t; +/** Return a newly allocated trn_subproto_request with all elements + * set to zero. + */ +trn_subproto_request_t *trn_subproto_request_new(void); +/** Release all storage held by the trn_subproto_request in 'victim'. + * (Do nothing if 'victim' is NULL.) + */ +void trn_subproto_request_free(trn_subproto_request_t *victim); +/** Try to parse a trn_subproto_request from the buffer in 'input', + * using up to 'len_in' bytes from the input buffer. On success, + * return the number of bytes consumed and set *output to the newly + * allocated trn_subproto_request_t. On failure, return -2 if the + * input appears truncated, and -1 if the input is otherwise invalid. + */ +ssize_t trn_subproto_request_parse(trn_subproto_request_t **output, const uint8_t *input, const size_t len_in); +/** Return the number of bytes we expect to need to encode the + * trn_subproto_request in 'obj'. On failure, return a negative value. + * Note that this value may be an overestimate, and can even be an + * underestimate for certain unencodeable objects. + */ +ssize_t trn_subproto_request_encoded_len(const trn_subproto_request_t *obj); +/** Try to encode the trn_subproto_request from 'input' into the + * buffer at 'output', using up to 'avail' bytes of the output buffer. + * On success, return the number of bytes used. On failure, return -2 + * if the buffer was not long enough, and -1 if the input was invalid. + */ +ssize_t trn_subproto_request_encode(uint8_t *output, size_t avail, const trn_subproto_request_t *input); +/** Check whether the internal state of the trn_subproto_request in + * 'obj' is consistent. Return NULL if it is, and a short message if + * it is not. + */ +const char *trn_subproto_request_check(const trn_subproto_request_t *obj); +/** Clear any errors that were set on the object 'obj' by its setter + * functions. Return true iff errors were cleared. + */ +int trn_subproto_request_clear_errors(trn_subproto_request_t *obj); +/** Return the value of the protocol_id field of the + * trn_subproto_request_t in 'inp' + */ +uint8_t trn_subproto_request_get_protocol_id(const trn_subproto_request_t *inp); +/** Set the value of the protocol_id field of the + * trn_subproto_request_t in 'inp' to 'val'. Return 0 on success; + * return -1 and set the error code on 'inp' on failure. + */ +int trn_subproto_request_set_protocol_id(trn_subproto_request_t *inp, uint8_t val); +/** Return the value of the proto_cap_number field of the + * trn_subproto_request_t in 'inp' + */ +uint8_t trn_subproto_request_get_proto_cap_number(const trn_subproto_request_t *inp); +/** Set the value of the proto_cap_number field of the + * trn_subproto_request_t in 'inp' to 'val'. Return 0 on success; + * return -1 and set the error code on 'inp' on failure. + */ +int trn_subproto_request_set_proto_cap_number(trn_subproto_request_t *inp, uint8_t val); +/** Return a newly allocated trn_subproto_request_ext with all + * elements set to zero. + */ +trn_subproto_request_ext_t *trn_subproto_request_ext_new(void); +/** Release all storage held by the trn_subproto_request_ext in + * 'victim'. (Do nothing if 'victim' is NULL.) + */ +void trn_subproto_request_ext_free(trn_subproto_request_ext_t *victim); +/** Try to parse a trn_subproto_request_ext from the buffer in + * 'input', using up to 'len_in' bytes from the input buffer. On + * success, return the number of bytes consumed and set *output to the + * newly allocated trn_subproto_request_ext_t. On failure, return -2 + * if the input appears truncated, and -1 if the input is otherwise + * invalid. + */ +ssize_t trn_subproto_request_ext_parse(trn_subproto_request_ext_t **output, const uint8_t *input, const size_t len_in); +/** Return the number of bytes we expect to need to encode the + * trn_subproto_request_ext in 'obj'. On failure, return a negative + * value. Note that this value may be an overestimate, and can even be + * an underestimate for certain unencodeable objects. + */ +ssize_t trn_subproto_request_ext_encoded_len(const trn_subproto_request_ext_t *obj); +/** Try to encode the trn_subproto_request_ext from 'input' into the + * buffer at 'output', using up to 'avail' bytes of the output buffer. + * On success, return the number of bytes used. On failure, return -2 + * if the buffer was not long enough, and -1 if the input was invalid. + */ +ssize_t trn_subproto_request_ext_encode(uint8_t *output, size_t avail, const trn_subproto_request_ext_t *input); +/** Check whether the internal state of the trn_subproto_request_ext + * in 'obj' is consistent. Return NULL if it is, and a short message + * if it is not. + */ +const char *trn_subproto_request_ext_check(const trn_subproto_request_ext_t *obj); +/** Clear any errors that were set on the object 'obj' by its setter + * functions. Return true iff errors were cleared. + */ +int trn_subproto_request_ext_clear_errors(trn_subproto_request_ext_t *obj); +/** Return the length of the dynamic array holding the reqs field of + * the trn_subproto_request_ext_t in 'inp'. + */ +size_t trn_subproto_request_ext_getlen_reqs(const trn_subproto_request_ext_t *inp); +/** Return the element at position 'idx' of the dynamic array field + * reqs of the trn_subproto_request_ext_t in 'inp'. + */ +struct trn_subproto_request_st * trn_subproto_request_ext_get_reqs(trn_subproto_request_ext_t *inp, size_t idx); +/** As trn_subproto_request_ext_get_reqs, but take and return a const + * pointer + */ + const struct trn_subproto_request_st * trn_subproto_request_ext_getconst_reqs(const trn_subproto_request_ext_t *inp, size_t idx); +/** Change the element at position 'idx' of the dynamic array field + * reqs of the trn_subproto_request_ext_t in 'inp', so that it will + * hold the value 'elt'. Free the previous value, if any. + */ +int trn_subproto_request_ext_set_reqs(trn_subproto_request_ext_t *inp, size_t idx, struct trn_subproto_request_st * elt); +/** As trn_subproto_request_ext_set_reqs, but does not free the + * previous value. + */ +int trn_subproto_request_ext_set0_reqs(trn_subproto_request_ext_t *inp, size_t idx, struct trn_subproto_request_st * elt); +/** Append a new element 'elt' to the dynamic array field reqs of the + * trn_subproto_request_ext_t in 'inp'. + */ +int trn_subproto_request_ext_add_reqs(trn_subproto_request_ext_t *inp, struct trn_subproto_request_st * elt); +/** Return a pointer to the variable-length array field reqs of 'inp'. + */ +struct trn_subproto_request_st * * trn_subproto_request_ext_getarray_reqs(trn_subproto_request_ext_t *inp); +/** As trn_subproto_request_ext_get_reqs, but take and return a const + * pointer + */ +const struct trn_subproto_request_st * const * trn_subproto_request_ext_getconstarray_reqs(const trn_subproto_request_ext_t *inp); +/** Change the length of the variable-length array field reqs of 'inp' + * to 'newlen'.Fill extra elements with NULL; free removed elements. + * Return 0 on success; return -1 and set the error code on 'inp' on + * failure. + */ +int trn_subproto_request_ext_setlen_reqs(trn_subproto_request_ext_t *inp, size_t newlen); + + +#endif diff --git a/src/trunnel/subproto_request.trunnel b/src/trunnel/subproto_request.trunnel new file mode 100644 index 0000000000..df72be729e --- /dev/null +++ b/src/trunnel/subproto_request.trunnel @@ -0,0 +1,9 @@ + +struct trn_subproto_request { + u8 protocol_id; + u8 proto_cap_number; +} + +struct trn_subproto_request_ext { + struct trn_subproto_request reqs[]; +}