Beispiel #1
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;
}
Beispiel #2
0
ssize_t
pwbox_encoded_encode(uint8_t *output, size_t avail, const pwbox_encoded_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 = pwbox_encoded_encoded_len(obj);
#endif
  int enforce_avail = 0;
  const size_t avail_orig = avail;

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

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

  /* Encode u32 fixedbytes0 IN [PWBOX0_CONST0] */
  trunnel_assert(written <= avail);
  if (avail - written < 4)
    goto truncated;
  trunnel_set_uint32(ptr, trunnel_htonl(obj->fixedbytes0));
  written += 4; ptr += 4;

  /* Encode u32 fixedbytes1 IN [PWBOX0_CONST1] */
  trunnel_assert(written <= avail);
  if (avail - written < 4)
    goto truncated;
  trunnel_set_uint32(ptr, trunnel_htonl(obj->fixedbytes1));
  written += 4; ptr += 4;

  /* Encode u8 header_len */
  trunnel_assert(written <= avail);
  if (avail - written < 1)
    goto truncated;
  trunnel_set_uint8(ptr, (obj->header_len));
  written += 1; ptr += 1;

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

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

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

  /* Encode u8 hmac[32] */
  trunnel_assert(written <= avail);
  if (avail - written < 32) {
    if (avail_orig - written < 32)
      goto truncated;
    else
      goto check_failed;
  }
  memcpy(ptr, obj->hmac, 32);
  written += 32; ptr += 32;


  trunnel_assert(ptr == output + written);
  if (enforce_avail && avail != written)
    goto check_failed;
#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;
}