/** As trn_cell_introduce1_parse(), but do not allocate the output * object. */ static ssize_t trn_cell_introduce1_parse_into(trn_cell_introduce1_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 legacy_key_id[TRUNNEL_SHA1_LEN] */ CHECK_REMAINING(TRUNNEL_SHA1_LEN, truncated); memcpy(obj->legacy_key_id, ptr, TRUNNEL_SHA1_LEN); remaining -= TRUNNEL_SHA1_LEN; ptr += TRUNNEL_SHA1_LEN; /* Parse u8 auth_key_type IN [0, 1, 2] */ CHECK_REMAINING(1, truncated); obj->auth_key_type = (trunnel_get_uint8(ptr)); remaining -= 1; ptr += 1; if (! (obj->auth_key_type == 0 || obj->auth_key_type == 1 || obj->auth_key_type == 2)) goto fail; /* Parse u16 auth_key_len */ CHECK_REMAINING(2, truncated); obj->auth_key_len = trunnel_ntohs(trunnel_get_uint16(ptr)); remaining -= 2; ptr += 2; /* Parse u8 auth_key[auth_key_len] */ CHECK_REMAINING(obj->auth_key_len, truncated); TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->auth_key, obj->auth_key_len, {}); obj->auth_key.n_ = obj->auth_key_len; if (obj->auth_key_len) memcpy(obj->auth_key.elts_, ptr, obj->auth_key_len); ptr += obj->auth_key_len; remaining -= obj->auth_key_len; /* Parse struct trn_cell_extension extensions */ result = trn_cell_extension_parse(&obj->extensions, ptr, remaining); if (result < 0) goto relay_fail; trunnel_assert((size_t)result <= remaining); remaining -= result; ptr += result; /* Parse u8 encrypted[] */ TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->encrypted, remaining, {}); obj->encrypted.n_ = remaining; if (remaining) memcpy(obj->encrypted.elts_, ptr, remaining); ptr += remaining; remaining -= remaining; 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; fail: result = -1; return result; }
/** As trn_cell_rendezvous1_parse(), but do not allocate the output * object. */ static ssize_t trn_cell_rendezvous1_parse_into(trn_cell_rendezvous1_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 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */ CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated); memcpy(obj->rendezvous_cookie, ptr, TRUNNEL_REND_COOKIE_LEN); remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN; /* Parse u8 handshake_info[] */ TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->handshake_info, remaining, {}); obj->handshake_info.n_ = remaining; if (remaining) memcpy(obj->handshake_info.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; }
/** 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; 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; }
/** 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; }
/** 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; }
/** 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; 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; }
/** As pwbox_encoded_parse(), but do not allocate the output object. */ static ssize_t pwbox_encoded_parse_into(pwbox_encoded_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 u32 fixedbytes0 IN [PWBOX0_CONST0] */ CHECK_REMAINING(4, truncated); obj->fixedbytes0 = trunnel_ntohl(trunnel_get_uint32(ptr)); remaining -= 4; ptr += 4; if (! (obj->fixedbytes0 == PWBOX0_CONST0)) goto fail; /* Parse u32 fixedbytes1 IN [PWBOX0_CONST1] */ CHECK_REMAINING(4, truncated); obj->fixedbytes1 = trunnel_ntohl(trunnel_get_uint32(ptr)); remaining -= 4; ptr += 4; if (! (obj->fixedbytes1 == PWBOX0_CONST1)) goto fail; /* Parse u8 header_len */ CHECK_REMAINING(1, truncated); obj->header_len = (trunnel_get_uint8(ptr)); remaining -= 1; ptr += 1; /* Parse u8 skey_header[header_len] */ CHECK_REMAINING(obj->header_len, truncated); TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->skey_header, obj->header_len, {}); obj->skey_header.n_ = obj->header_len; memcpy(obj->skey_header.elts_, ptr, obj->header_len); ptr += obj->header_len; remaining -= obj->header_len; /* Parse u8 iv[16] */ CHECK_REMAINING(16, truncated); memcpy(obj->iv, ptr, 16); remaining -= 16; ptr += 16; { size_t remaining_after; CHECK_REMAINING(32, truncated); remaining_after = 32; remaining = remaining - 32; /* Parse u8 data[] */ TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->data, remaining, {}); obj->data.n_ = remaining; memcpy(obj->data.elts_, ptr, remaining); ptr += remaining; remaining -= remaining; if (remaining != 0) goto fail; remaining = remaining_after; } /* Parse u8 hmac[32] */ CHECK_REMAINING(32, truncated); memcpy(obj->hmac, ptr, 32); remaining -= 32; ptr += 32; trunnel_assert(ptr + remaining == input + len_in); return len_in - remaining; truncated: return -2; trunnel_alloc_failed: return -1; fail: result = -1; return result; }
/** As ed25519_cert_extension_parse(), but do not allocate the output * object. */ static ssize_t ed25519_cert_extension_parse_into(ed25519_cert_extension_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 u16 ext_length */ CHECK_REMAINING(2, truncated); obj->ext_length = trunnel_ntohs(trunnel_get_uint16(ptr)); remaining -= 2; ptr += 2; /* Parse u8 ext_type */ CHECK_REMAINING(1, truncated); obj->ext_type = (trunnel_get_uint8(ptr)); remaining -= 1; ptr += 1; /* Parse u8 ext_flags */ CHECK_REMAINING(1, truncated); obj->ext_flags = (trunnel_get_uint8(ptr)); remaining -= 1; ptr += 1; { size_t remaining_after; CHECK_REMAINING(obj->ext_length, truncated); remaining_after = remaining - obj->ext_length; remaining = obj->ext_length; /* Parse union un[ext_type] */ switch (obj->ext_type) { case CERTEXT_SIGNED_WITH_KEY: /* Parse u8 un_signing_key[32] */ CHECK_REMAINING(32, fail); memcpy(obj->un_signing_key, ptr, 32); remaining -= 32; ptr += 32; break; default: /* Parse u8 un_unparsed[] */ TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->un_unparsed, remaining, {}); obj->un_unparsed.n_ = remaining; memcpy(obj->un_unparsed.elts_, ptr, remaining); ptr += remaining; remaining -= remaining; break; } if (remaining != 0) goto fail; remaining = remaining_after; } trunnel_assert(ptr + remaining == input + len_in); return len_in - remaining; truncated: return -2; trunnel_alloc_failed: return -1; fail: result = -1; return result; }