Esempio n. 1
0
/* Decrypts the given data.
 * Returns the decrypted data length.
 *
 * The output is preallocated with the maximum allowed data size.
 */
int
_gnutls_decrypt(gnutls_session_t session,
		gnutls_datum_t * ciphertext,
		gnutls_datum_t * output,
		content_type_t type,
		record_parameters_st * params, uint64 * sequence)
{
	int ret;

	if (ciphertext->size == 0)
		return 0;

	if (is_read_comp_null(params) == 0) {
		ret =
		    ciphertext_to_compressed(session, ciphertext,
					     output, type, params,
					     sequence);
		if (ret < 0)
			return gnutls_assert_val(ret);

		return ret;
	} else {
		gnutls_datum_t tmp;

		tmp.size = output->size;
		tmp.data = gnutls_malloc(tmp.size);
		if (tmp.data == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		ret =
		    ciphertext_to_compressed(session, ciphertext,
					     &tmp, type, params,
					     sequence);
		if (ret < 0)
			goto leave;

		tmp.size = ret;

		if (ret != 0) {
			ret =
			    _gnutls_decompress(&params->read.
					       compression_state, tmp.data,
					       tmp.size, output->data,
					       output->size);
			if (ret < 0)
				goto leave;
		}

	      leave:
		gnutls_free(tmp.data);
		return ret;
	}
}
/* Decrypts the given data.
 * Returns the decrypted data length.
 */
int
_gnutls_decrypt (gnutls_session_t session, opaque * ciphertext,
                 size_t ciphertext_size, uint8_t * data,
                 size_t max_data_size, content_type_t type,
                 record_parameters_st * params, uint64 *sequence)
{
  gnutls_datum_t gcipher;
  int ret, data_size;

  if (ciphertext_size == 0)
    return 0;

  gcipher.size = ciphertext_size;
  gcipher.data = ciphertext;

  if (is_read_comp_null (params) == 0)
    {
      ret =
        ciphertext_to_compressed (session, &gcipher, data, max_data_size,
                                   type, params, sequence);
      if (ret < 0)
        return gnutls_assert_val(ret);
      
      return ret;
    }
  else
    {
      opaque* tmp_data;
      
      tmp_data = gnutls_malloc(max_data_size);
      if (tmp_data == NULL)
        return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
      
      ret =
        ciphertext_to_compressed (session, &gcipher, tmp_data, max_data_size,
                                   type, params, sequence);
      if (ret < 0)
        goto leave;
      
      data_size = ret;
        
      if (ret != 0)
        {
          ret = _gnutls_decompress( &params->read.compression_state, tmp_data, data_size, data, max_data_size);
          if (ret < 0)
            goto leave;
        }
        
leave:
      gnutls_free(tmp_data);
      return ret;
    }
}
Esempio n. 3
0
/* Decrypts the given data.
 * Returns the decrypted data length.
 */
int
_gnutls_decrypt (gnutls_session_t session, opaque * ciphertext,
                 size_t ciphertext_size, uint8_t * data,
                 size_t max_data_size, content_type_t type,
                 record_parameters_st * params)
{
  gnutls_datum_t gtxt;
  gnutls_datum_t gcipher;
  int ret;

  if (ciphertext_size == 0)
    return 0;

  gcipher.size = ciphertext_size;
  gcipher.data = ciphertext;

  ret =
    _gnutls_ciphertext2compressed (session, data, max_data_size,
                                   gcipher, type, params);
  if (ret < 0)
    {
      return ret;
    }

  if (ret == 0 || is_read_comp_null (session) == 0)
    {
      /* ret == ret */

    }
  else
    {
      gnutls_datum_t gcomp;

      /* compression has this malloc overhead.
       */

      gcomp.data = data;
      gcomp.size = ret;
      ret = _gnutls_m_compressed2plaintext (session, &gtxt, &gcomp, params);
      if (ret < 0)
        {
          return ret;
        }

      if (gtxt.size > MAX_RECORD_RECV_SIZE)
        {
          gnutls_assert ();
          _gnutls_free_datum (&gtxt);
          /* This shouldn't have happen and
           * is a TLS fatal error.
           */
          return GNUTLS_E_DECOMPRESSION_FAILED;
        }

      /* This check is not really needed */
      if (max_data_size < MAX_RECORD_RECV_SIZE)
        {
          gnutls_assert ();
          _gnutls_free_datum (&gtxt);
          return GNUTLS_E_INTERNAL_ERROR;
        }

      memcpy (data, gtxt.data, gtxt.size);
      ret = gtxt.size;

      _gnutls_free_datum (&gtxt);
    }

  return ret;
}