Exemple #1
0
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;
}
Exemple #2
0
int
auth1_set_rand(auth1_t *inp, size_t idx, uint8_t elt)
{
  trunnel_assert(idx < 24);
  inp->rand[idx] = elt;
  return 0;
}
Exemple #3
0
int
auth1_set_tlssecrets(auth1_t *inp, size_t idx, uint8_t elt)
{
  trunnel_assert(idx < 32);
  inp->tlssecrets[idx] = elt;
  return 0;
}
Exemple #4
0
int
auth1_set_scert(auth1_t *inp, size_t idx, uint8_t elt)
{
  trunnel_assert(idx < 32);
  inp->scert[idx] = elt;
  return 0;
}
Exemple #5
0
int
auth1_set_type(auth1_t *inp, size_t idx, uint8_t elt)
{
  trunnel_assert(idx < 8);
  inp->type[idx] = elt;
  return 0;
}
Exemple #6
0
ssize_t
trn_cell_rendezvous1_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous1_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_cell_rendezvous1_encoded_len(obj);
#endif

  if (NULL != (msg = trn_cell_rendezvous1_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  trunnel_assert(written <= avail);
  if (avail - written < TRUNNEL_REND_COOKIE_LEN)
    goto truncated;
  memcpy(ptr, obj->rendezvous_cookie, TRUNNEL_REND_COOKIE_LEN);
  written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;

  /* Encode u8 handshake_info[] */
  {
    size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->handshake_info);
    trunnel_assert(written <= avail);
    if (avail - written < elt_len)
      goto truncated;
    if (elt_len)
      memcpy(ptr, obj->handshake_info.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;
}
Exemple #7
0
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;
}
Exemple #8
0
ssize_t
trn_cell_introduce_ack_encode(uint8_t *output, const size_t avail, const trn_cell_introduce_ack_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_cell_introduce_ack_encoded_len(obj);
#endif

  if (NULL != (msg = trn_cell_introduce_ack_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u16 status IN [0, 1, 2] */
  trunnel_assert(written <= avail);
  if (avail - written < 2)
    goto truncated;
  trunnel_set_uint16(ptr, trunnel_htons(obj->status));
  written += 2; ptr += 2;

  /* Encode struct trn_cell_extension extensions */
  trunnel_assert(written <= avail);
  result = trn_cell_extension_encode(ptr, avail - written, obj->extensions);
  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;
}
Exemple #9
0
/** As trn_cell_rendezvous2_parse(), but do not allocate the output
 * object.
 */
static ssize_t
trn_cell_rendezvous2_parse_into(trn_cell_rendezvous2_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 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */
  CHECK_REMAINING(TRUNNEL_HANDSHAKE_INFO_LEN, truncated);
  memcpy(obj->handshake_info, ptr, TRUNNEL_HANDSHAKE_INFO_LEN);
  remaining -= TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_LEN;
  trunnel_assert(ptr + remaining == input + len_in);
  return len_in - remaining;

 truncated:
  return -2;
}
Exemple #10
0
ssize_t
trn_cell_rendezvous2_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous2_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_cell_rendezvous2_encoded_len(obj);
#endif

  if (NULL != (msg = trn_cell_rendezvous2_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */
  trunnel_assert(written <= avail);
  if (avail - written < TRUNNEL_HANDSHAKE_INFO_LEN)
    goto truncated;
  memcpy(ptr, obj->handshake_info, TRUNNEL_HANDSHAKE_INFO_LEN);
  written += TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_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;
}
Exemple #11
0
/** 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;
}
Exemple #12
0
/** 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;
}
Exemple #13
0
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;
    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;
}
Exemple #14
0
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];
}
Exemple #15
0
uint8_t
auth_challenge_cell_get_challenge(const auth_challenge_cell_t *inp, size_t idx)
{
  trunnel_assert(idx < 32);
  return inp->challenge[idx];
}
Exemple #16
0
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;
    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;
}
Exemple #17
0
uint8_t
trn_cell_rendezvous1_get_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx)
{
  trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  return inp->rendezvous_cookie[idx];
}
Exemple #18
0
ssize_t
trn_cell_introduce1_encode(uint8_t *output, const size_t avail, const trn_cell_introduce1_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_cell_introduce1_encoded_len(obj);
#endif

  if (NULL != (msg = trn_cell_introduce1_check(obj)))
    goto check_failed;

#ifdef TRUNNEL_CHECK_ENCODED_LEN
  trunnel_assert(encoded_len >= 0);
#endif

  /* Encode u8 legacy_key_id[TRUNNEL_SHA1_LEN] */
  trunnel_assert(written <= avail);
  if (avail - written < TRUNNEL_SHA1_LEN)
    goto truncated;
  memcpy(ptr, obj->legacy_key_id, TRUNNEL_SHA1_LEN);
  written += TRUNNEL_SHA1_LEN; ptr += TRUNNEL_SHA1_LEN;

  /* Encode u8 auth_key_type IN [0, 1, 2] */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->auth_key_type));
  written += 1; ptr += 1;

  /* Encode u16 auth_key_len */
  trunnel_assert(written <= avail);
  if (avail - written < 2)
    goto truncated;
  trunnel_set_uint16(ptr, trunnel_htons(obj->auth_key_len));
  written += 2; ptr += 2;

  /* Encode u8 auth_key[auth_key_len] */
  {
    size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->auth_key);
    trunnel_assert(obj->auth_key_len == elt_len);
    trunnel_assert(written <= avail);
    if (avail - written < elt_len)
      goto truncated;
    if (elt_len)
      memcpy(ptr, obj->auth_key.elts_, elt_len);
    written += elt_len; ptr += elt_len;
  }

  /* Encode struct trn_cell_extension extensions */
  trunnel_assert(written <= avail);
  result = trn_cell_extension_encode(ptr, avail - written, obj->extensions);
  if (result < 0)
    goto fail; /* XXXXXXX !*/
  written += result; ptr += result;

  /* Encode u8 encrypted[] */
  {
    size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->encrypted);
    trunnel_assert(written <= avail);
    if (avail - written < elt_len)
      goto truncated;
    if (elt_len)
      memcpy(ptr, obj->encrypted.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;
}
Exemple #19
0
uint8_t
trn_cell_introduce_encrypted_get_rend_cookie(trn_cell_introduce_encrypted_t *inp, size_t idx)
{
  trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  return inp->rend_cookie[idx];
}
Exemple #20
0
uint8_t
auth1_get_type(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 8);
  return inp->type[idx];
}
Exemple #21
0
uint8_t
auth1_get_rand(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 24);
  return inp->rand[idx];
}
Exemple #22
0
uint8_t
auth1_get_tlssecrets(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 32);
  return inp->tlssecrets[idx];
}
Exemple #23
0
uint8_t
auth1_get_slog(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 32);
  return inp->slog[idx];
}
Exemple #24
0
uint8_t
auth1_get_u1_sid_ed(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 32);
  return inp->u1_sid_ed[idx];
}
Exemple #25
0
uint8_t
auth1_get_cid(const auth1_t *inp, size_t idx)
{
  trunnel_assert(idx < 32);
  return inp->cid[idx];
}
Exemple #26
0
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;
    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;
}
Exemple #27
0
/** 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;
}
Exemple #28
0
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;
}
Exemple #29
0
uint8_t
trn_cell_introduce1_get_legacy_key_id(trn_cell_introduce1_t *inp, size_t idx)
{
  trunnel_assert(idx < TRUNNEL_SHA1_LEN);
  return inp->legacy_key_id[idx];
}
Exemple #30
0
uint8_t
trn_cell_rendezvous2_get_handshake_info(trn_cell_rendezvous2_t *inp, size_t idx)
{
  trunnel_assert(idx < TRUNNEL_HANDSHAKE_INFO_LEN);
  return inp->handshake_info[idx];
}