Beispiel #1
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;
}
Beispiel #2
0
/** 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;
}