Exemplo n.º 1
0
LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout)
{
	if (!connection || (connection->ssl_data && !connection->ssl_data->session)) {
		return IDEVICE_E_INVALID_ARG;
	}

	if (connection->ssl_data) {
		uint32_t received = 0;
		while (received < len) {
#ifdef HAVE_OPENSSL
			int r = SSL_read(connection->ssl_data->session, (void*)((char*)(data+received)), (int)len-received);
#else
			ssize_t r = gnutls_record_recv(connection->ssl_data->session, (void*)(data+received), (size_t)len-received);
#endif
			if (r > 0) {
				received += r;
			} else {
				break;
			}
		}
		debug_info("SSL_read %d, received %d", len, received);
		if (received > 0) {
			*recv_bytes = received;
			return IDEVICE_E_SUCCESS;
		}
		*recv_bytes = 0;
		return IDEVICE_E_SSL_ERROR;
	}
	return internal_connection_receive_timeout(connection, data, len, recv_bytes, timeout);
}
Exemplo n.º 2
0
size_t TLSSocket_GnuTLS::receiveRaw(byte_t* buffer, const size_t count) {

	m_status &= ~(STATUS_WANT_WRITE | STATUS_WANT_READ);

	resetException();

	const ssize_t ret = gnutls_record_recv(
		*m_session->m_gnutlsSession,
		buffer, static_cast <size_t>(count)
	);

	throwException();

	if (ret < 0) {

		if (ret == GNUTLS_E_AGAIN) {

			if (gnutls_record_get_direction(*m_session->m_gnutlsSession) == 0) {
				m_status |= STATUS_WANT_READ;
			} else {
				m_status |= STATUS_WANT_WRITE;
			}

			return 0;
		}

		TLSSession_GnuTLS::throwTLSException("gnutls_record_recv", static_cast <int>(ret));
	}

	return static_cast <size_t>(ret);
}
Exemplo n.º 3
0
static ber_slen_t
tlsg_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
{
	struct tls_data		*p;
	ber_slen_t		ret;

	assert( sbiod != NULL );
	assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );

	p = (struct tls_data *)sbiod->sbiod_pvt;

	ret = gnutls_record_recv ( p->session->session, buf, len );
	switch (ret) {
	case GNUTLS_E_INTERRUPTED:
	case GNUTLS_E_AGAIN:
		sbiod->sbiod_sb->sb_trans_needs_read = 1;
		sock_errset(EWOULDBLOCK);
		ret = 0;
		break;
	case GNUTLS_E_REHANDSHAKE:
		for ( ret = gnutls_handshake ( p->session->session );
		      ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN;
		      ret = gnutls_handshake ( p->session->session ) );
		sbiod->sbiod_sb->sb_trans_needs_read = 1;
		ret = 0;
		break;
	default:
		sbiod->sbiod_sb->sb_trans_needs_read = 0;
	}
	return ret;
}
Exemplo n.º 4
0
int tls_connection_decrypt(void *ssl_ctx, struct tls_connection *conn,
			   const u8 *in_data, size_t in_len,
			   u8 *out_data, size_t out_len)
{
	ssize_t res;

	if (conn->pull_buf) {
		wpa_printf(MSG_DEBUG, "%s - %d bytes remaining in "
			   "pull_buf", __func__, conn->pull_buf_len);
		free(conn->pull_buf);
	}
	conn->pull_buf = malloc(in_len);
	if (conn->pull_buf == NULL)
		return -1;
	memcpy(conn->pull_buf, in_data, in_len);
	conn->pull_buf_offset = conn->pull_buf;
	conn->pull_buf_len = in_len;

	res = gnutls_record_recv(conn->session, out_data, out_len);
	if (res < 0) {
		wpa_printf(MSG_DEBUG, "%s - gnutls_record_recv failed: %d "
			   "(%s)", __func__, res, gnutls_strerror(res));
	}

	return res;
}
Exemplo n.º 5
0
static gint ssl_read(gnutls_session_t ssl, gchar *buf, gint len)
{
	gint r;

	if (gnutls_record_check_pending(ssl) == 0) {
		if (fd_check_io(GPOINTER_TO_INT(gnutls_transport_get_ptr(ssl)), G_IO_IN) < 0)
			return -1;
	}

	while (1) {
		r = gnutls_record_recv(ssl, buf, len);
		if (r > 0)
			return r;

		switch (r) {
		case 0: /* closed connection */
			return -1;

		case GNUTLS_E_REHANDSHAKE:
			do {
				r = gnutls_handshake(ssl);
			} while (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED);
			break; /* re-receive */
		case GNUTLS_E_AGAIN:
		case GNUTLS_E_INTERRUPTED:
			errno = EAGAIN;
			return -1;

		default:
			return -1;
		}
	}

}
Exemplo n.º 6
0
apr_status_t read_line(apr_pool_t *pool, socket_thread_data_t *td,
		char **buf, apr_size_t bufsize)
{
	char errbuf[ERRBUFLEN + 1];
	apr_status_t res;
	char *_buf;
	apr_size_t readsize = bufsize, i;
	int ret;

	if((*buf = apr_pcalloc(pool, bufsize))==NULL)
	{
		syslog(LOG_ERR, "read_line: apr_pcalloc failed");
		apr_pool_destroy(pool);
		return;
	}
	_buf = *buf;

	if(td->tls_session == NULL)
	{
		res = apr_socket_recv(td->socket, _buf, &readsize);

		if(res == APR_SUCCESS || res == APR_EOF)
		{
			_buf[readsize] = 0;
			for(i = 0; i < readsize; i++)
				if(_buf[i] == '\r' || _buf[i] == '\n')
					_buf[i] = 0;
		}
		else
			syslog(LOG_ERR, "unexpected result while reading socket: %s",
				apr_strerror(res, errbuf, ERRBUFLEN));
	}
	else
	{
		ret = gnutls_record_recv (*(td->tls_session), _buf, bufsize);
		if (ret == 0)
		{
			// Connection has been closed by peer
			_buf[0] = 0;
			res = APR_EOF;
		}
		else if (ret < 0)
		{
			syslog(LOG_ERR, "received corrupted "
					"data(GNUTLS error code = %d). Closing the connection", ret);
			res = APR_EGENERAL;
		}
		else if (ret > 0)
		{
			_buf[ret] = 0;
			for(i = 0; i < ret; i++)
				if(_buf[i] == '\r' || _buf[i] == '\n')
					_buf[i] = 0;
			res = APR_SUCCESS;
		}
	}

	return res;
}
Exemplo n.º 7
0
int tls_read(tls_t *tls, void * const buff, const size_t len)
{
    int ret;

    ret = gnutls_record_recv(tls->session, buff, len);

    return ret;
}
Exemplo n.º 8
0
Arquivo: tls-gnu.c Projeto: fanf2/exim
int
tls_getc(void)
{
if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
  {
  int inbytes;

  DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(%lx, %lx, %u)\n",
    (long) tls_session, (long) ssl_xfer_buffer, ssl_xfer_buffer_size);

  if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout);
  inbytes = gnutls_record_recv(tls_session, CS ssl_xfer_buffer,
    ssl_xfer_buffer_size);
  alarm(0);

  /* A zero-byte return appears to mean that the TLS session has been
     closed down, not that the socket itself has been closed down. Revert to
     non-TLS handling. */

  if (inbytes == 0)
    {
    DEBUG(D_tls) debug_printf("Got TLS_EOF\n");

    receive_getc = smtp_getc;
    receive_ungetc = smtp_ungetc;
    receive_feof = smtp_feof;
    receive_ferror = smtp_ferror;
    receive_smtp_buffered = smtp_buffered;

    gnutls_deinit(tls_session);
    tls_session = NULL;
    tls_active = -1;
    tls_cipher = NULL;
    tls_peerdn = NULL;

    return smtp_getc();
    }

  /* Handle genuine errors */

  else if (inbytes < 0)
    {
    record_io_error(inbytes, US"recv", NULL);
    ssl_xfer_error = 1;
    return EOF;
    }
#ifndef DISABLE_DKIM
  dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
#endif
  ssl_xfer_buffer_hwm = inbytes;
  ssl_xfer_buffer_lwm = 0;
  }


/* Something in the buffer; return next uschar */

return ssl_xfer_buffer[ssl_xfer_buffer_lwm++];
}
Exemplo n.º 9
0
/*****************************************************************************
 * tls_Recv:
 *****************************************************************************
 * Receives data through a TLS session
 *****************************************************************************/
static int
gnutls_Recv( tls_session_t *p_session, char *buf, int i_length )
{
    int val;

    val = gnutls_record_recv( *(gnutls_session *)(p_session->p_sys),
                              buf, i_length );
    return val < 0 ? -1 : val;
}
Exemplo n.º 10
0
ssize_t TCPStream::receive(char* buffer, size_t len)
{
    if(secure) {
        return gnutls_record_recv(*m_session, buffer, len);
    }
    else {
        return read(m_sd, buffer, len);
    }
}
Exemplo n.º 11
0
int tls_read(tls_t *tls, void * const buff, const size_t len)
{
    int ret;

    ret = gnutls_record_recv(tls->session, buff, len);
    tls->lasterror = ret < 0 ? ret : 0;

    return ret;
}
Exemplo n.º 12
0
/**
 * @short Method to read some HTTPS data.
 * @memberof onion_https_t
 * 
 * @param req to get data from
 * @param data where to store unencrypted data
 * @param Lenght of desired data
 * @returns Actual read data. 0 means EOF.
 */
static ssize_t onion_https_read(onion_request *req, char *data, size_t len){
	gnutls_session_t session=(gnutls_session_t)req->connection.user_data;
	ssize_t ret=gnutls_record_recv(session, data, len);
	ONION_DEBUG("Read! (%p), %d bytes", session, ret);
	if (ret<0){
	  ONION_ERROR("Reading data has failed (%s)", gnutls_strerror (ret));
	}
	return ret;
}
Exemplo n.º 13
0
test_code_t
test_bye (gnutls_session session)
{
  int ret;
  char data[20];
  int old, secs = 6;

#ifndef _WIN32
  signal (SIGALRM, got_alarm);
#endif

  ADD_ALL_CIPHERS (session);
  ADD_ALL_COMP (session);
  ADD_ALL_CERTTYPES (session);
  ADD_ALL_PROTOCOLS (session);
  ADD_ALL_MACS (session);
  ADD_ALL_KX (session);
  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);

  ret = do_handshake (session);
  if (ret == TEST_FAILED)
    return ret;

  ret = gnutls_bye (session, GNUTLS_SHUT_WR);
  if (ret < 0)
    return TEST_FAILED;

#ifndef _WIN32
  old = siginterrupt (SIGALRM, 1);
  alarm (secs);
#else
  setsockopt (gnutls_transport_get_ptr (session), SOL_SOCKET, SO_RCVTIMEO,
	      (char *) &secs, sizeof (int));
#endif

  do
    {
      ret = gnutls_record_recv (session, data, sizeof (data));
    }
  while (ret > 0);

#ifndef _WIN32
  siginterrupt (SIGALRM, old);
#else
  if (WSAGetLastError () == WSAETIMEDOUT ||
      WSAGetLastError () == WSAECONNABORTED)
    alrm = 1;
#endif
  if (ret == 0)
    return TEST_SUCCEED;

  if (alrm == 0)
    return TEST_UNSURE;

  return TEST_FAILED;
}
Exemplo n.º 14
0
test_code_t
test_bye (gnutls_session_t session)
{
  int ret;
  char data[20];
  int secs = 6;
#ifndef _WIN32
  int old;

  signal (SIGALRM, got_alarm);
#endif

  sprintf (prio_str, INIT_STR
           ALL_CIPHERS ":" ALL_COMP ":" ALL_CERTTYPES ":%s:" ALL_MACS ":"
           ALL_KX ":%s", protocol_str, rest);
  _gnutls_priority_set_direct (session, prio_str);

  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);

  ret = do_handshake (session);
  if (ret == TEST_FAILED)
    return ret;

  ret = gnutls_bye (session, GNUTLS_SHUT_WR);
  if (ret < 0)
    return TEST_FAILED;

#ifndef _WIN32
  old = siginterrupt (SIGALRM, 1);
  alarm (secs);
#else
  setsockopt ((int) gnutls_transport_get_ptr (session), SOL_SOCKET,
              SO_RCVTIMEO, (char *) &secs, sizeof (int));
#endif

  do
    {
      ret = gnutls_record_recv (session, data, sizeof (data));
    }
  while (ret > 0);

#ifndef _WIN32
  siginterrupt (SIGALRM, old);
#else
  if (WSAGetLastError () == WSAETIMEDOUT ||
      WSAGetLastError () == WSAECONNABORTED)
    alrm = 1;
#endif
  if (ret == 0)
    return TEST_SUCCEED;

  if (alrm == 0)
    return TEST_UNSURE;

  return TEST_FAILED;
}
Exemplo n.º 15
0
static int tls_read(URLContext *h, uint8_t *buf, int size)
{
    TLSContext *c = h->priv_data;
    int ret = gnutls_record_recv(c->session, buf, size);
    if (ret > 0)
        return ret;
    if (ret == 0)
        return AVERROR_EOF;
    return print_tls_error(h, ret);
}
Exemplo n.º 16
0
Arquivo: gnutls.c Projeto: Adatan/vlc
/**
 * Receives data through a TLS session.
 */
static int gnutls_Recv (void *opaque, void *buf, size_t length)
{
    assert (opaque != NULL);

    vlc_tls_t *tls = opaque;
    gnutls_session_t session = tls->sys;

    int val = gnutls_record_recv (session, buf, length);
    return (val < 0) ? gnutls_Error (tls, val) : val;
}
Exemplo n.º 17
0
void CTlsSocket::CheckResumeFailedReadWrite()
{
	if (m_lastWriteFailed)
	{
		int res = GNUTLS_E_AGAIN;
		while ((res == GNUTLS_E_INTERRUPTED || res == GNUTLS_E_AGAIN) && m_canWriteToSocket)
			res = gnutls_record_send(m_session, 0, 0);

		if (res == GNUTLS_E_INTERRUPTED || res == GNUTLS_E_AGAIN)
			return;

		if (res < 0)
		{
			Failure(res, ECONNABORTED);
			return;
		}

		m_writeSkip += res;
		m_lastWriteFailed = false;
		m_canTriggerWrite = true;

		wxASSERT(GNUTLS_E_INVALID_REQUEST == gnutls_record_send(m_session, 0, 0));
	}
	if (m_lastReadFailed)
	{
		wxASSERT(!m_peekData);

		m_peekDataLen = 65536;
		m_peekData = new char[m_peekDataLen];

		int res = gnutls_record_recv(m_session, m_peekData, m_peekDataLen);
		if (res < 0)
		{
			m_peekDataLen = 0;
			delete [] m_peekData;
			m_peekData = 0;
			if (res != GNUTLS_E_INTERRUPTED && res != GNUTLS_E_AGAIN)
				Failure(res, ECONNABORTED);
			return;
		}

		if (!res)
		{
			m_peekDataLen = 0;
			delete [] m_peekData;
			m_peekData = 0;
		}
		else
			m_peekDataLen = res;

		m_lastReadFailed = false;
		m_canTriggerRead = true;
	}
}
Exemplo n.º 18
0
static ssize_t receive_command(load_reference lLoad, socket_reference rReference,
remote_connection sSocket, char *dData, ssize_t sSize)
{
	gnutls_transport_set_ptr(get_session(rReference), (gnutls_transport_ptr_t) sSocket);
	ssize_t outcome = gnutls_record_recv(get_session(rReference), dData, sSize);
	//NOTE: set for 'buffered_common_input_nolex::read_binary_input'
	if (outcome == GNUTLS_E_AGAIN)       errno = EAGAIN;
	if (outcome == GNUTLS_E_INTERRUPTED) errno = EINTR;

	return outcome;
}
Exemplo n.º 19
0
ssize_t cstp_recv_nb(worker_st *ws, void *data, size_t data_size)
{
	int ret;

	if (ws->session != NULL) {
		ret = gnutls_record_recv(ws->session, data, data_size);
	} else {
		ret = recv(ws->conn_fd, data, data_size, 0);
	}

	return ret;
}
Exemplo n.º 20
0
size_t SSLBuffer::_pull(char *address, size_t size)
{
    if(!bio)
        return TCPBuffer::_pull(address, size);

    int result = gnutls_record_recv((SSL)ssl, address, size);
    if(result < 0) {
        result = 0;
        ioerr = EIO;
    }
    return (size_t)result;
}
Exemplo n.º 21
0
/* Expect status code. This can be done better. */
char* ssl_smtp_expect(gnutls_session_t session, char *str) {
	char buffer[BUFFER_SIZE];
	int len = strlen(str);
	int bytes = gnutls_record_recv(session, buffer, BUFFER_SIZE);
	if (bytes < len) goto fail;
	if (!strncmp(buffer,str,len)) {
		return NULL;
	}

	fail:;
	return strdup(buffer);
}
Exemplo n.º 22
0
static void *start_thread(void *arg)
{
	gnutls_session_t session = arg;
	int ret;
	char buf[64];

	if (debug)
		success("client: TLS version is: %s\n",
			gnutls_protocol_get_name
			(gnutls_protocol_get_version(session)));
	sleep(1);
	/* the server should reflect our messages */
	ret = gnutls_record_recv(session, buf, sizeof(buf));
	if (ret != sizeof(MSG)-1 || memcmp(buf, MSG, sizeof(MSG)-1) != 0) {
		fail("client: recv failed: %s\n", gnutls_strerror(ret));
		exit(1);
	}

	if (debug) {
		fprintf(stderr, "client received: %.*s\n", ret, buf);
	}

	ret = gnutls_record_recv(session, buf, sizeof(buf));
	if (ret != sizeof(MSG2)-1 || memcmp(buf, MSG2, sizeof(MSG2)-1) != 0) {
		fail("client: recv2 failed: %s\n", gnutls_strerror(ret));
		exit(1);
	}

	if (debug) {
		fprintf(stderr, "client received: %.*s\n", ret, buf);
	}

	ret = gnutls_record_recv(session, buf, sizeof(buf));
	if (ret != 0) {
		fail("client: recv3 failed: %s\n", gnutls_strerror(ret));
		exit(1);
	}

	pthread_exit(0);
}
Exemplo n.º 23
0
Arquivo: ebb.c Projeto: bakins/libebb
/* Internal callback 
 * called by connection->read_watcher
 */
static void 
on_readable(struct ev_loop *loop, ev_io *watcher, int revents)
{
  ebb_connection *connection = watcher->data;
  char recv_buffer[4096];
  ssize_t recved;

  //printf("on_readable\n");
  // TODO -- why is this broken?
  //assert(ev_is_active(&connection->timeout_watcher));
  assert(watcher == &connection->read_watcher);

  if(EV_ERROR & revents) {
    error("on_readable() got error event, closing connection.");
    goto error;
  }

#ifdef HAVE_GNUTLS
  assert(!ev_is_active(&connection->handshake_watcher));

  if(connection->server->secure) {
    recved = gnutls_record_recv( connection->session
                               , recv_buffer
                               , TCP_MAXWIN
                               );
    if(recved <= 0) {
      if(gnutls_error_is_fatal(recved)) goto error;
      if( (recved == GNUTLS_E_INTERRUPTED || recved == GNUTLS_E_AGAIN)
       && GNUTLS_NEED_WRITE
        ) ev_io_start(loop, &connection->write_watcher);
      return; 
    } 
  } else {
#endif /* HAVE_GNUTLS */

    recved = recv(connection->fd, recv_buffer, 4096, 0);
    if(recved <= 0) goto error;

#ifdef HAVE_GNUTLS
  }
#endif /* HAVE_GNUTLS */

  ebb_connection_reset_timeout(connection);

  ebb_request_parser_execute(&connection->parser, recv_buffer, recved);

  /* parse error? just drop the client. screw the 400 response */
  if(ebb_request_parser_has_error(&connection->parser)) goto error;
  return;
error:
  ebb_connection_schedule_close(connection);
}
Exemplo n.º 24
0
long long
network_recv(unsigned int *level,
             void **data,
             gnutls_session_t session)
{
	size_t r_len;
	packet_t packet;
	
	/* Get the metainformation packet. */
	r_len = gnutls_record_recv(session, &packet, sizeof(packet_t));
	if (r_len < sizeof(packet_t))
		return -1;
	if (packet.magic != PACKET_MAGIC) /* Corrupted package. */
		return -2;
	
	*data = malloc(packet.len);
	r_len = gnutls_record_recv(session, *data, packet.len);
	if (r_len < packet.len)
		return -3;
        *level = packet.level;	
	return r_len;
}
Exemplo n.º 25
0
void SocketRWCommon::readAction() {
	// Ensure we have at least 0x200 bytes space free
	// (Up this, maybe?)
	int bufSize = inputBuf.size();
	int requiredSize = bufSize + 0x200;
	if (requiredSize > inputBuf.capacity())
		inputBuf.setCapacity(requiredSize);

	ssize_t amount;

#ifdef USE_GNUTLS
	if (tlsActive) {
		amount = gnutls_record_recv(tls,
				&inputBuf.data()[bufSize],
				0x200);
	} else
#endif
	{
		amount = recv(sock,
				&inputBuf.data()[bufSize],
				0x200,
				0);
	}


	if (amount > 0) {
		// Yep, we have data
		printf("[fd=%d] Read %" PRIuPTR " bytes\n", sock, amount);
		inputBuf.resize(bufSize + amount);

		processReadBuffer();

	} else if (amount == 0) {
		printf("[fd=%d] Read 0! Socket closing.\n", sock);
		close();

	} else if (amount < 0) {
#ifdef USE_GNUTLS
		if (tlsActive) {
			if (gnutls_error_is_fatal(amount)) {
				printf("Error while reading [gnutls %" PRIuPTR "]!\n", amount);
				close();
			}
		} else
#endif
		{
			perror("Error while reading!");
			close();
		}
	}
}
Exemplo n.º 26
0
gboolean
on_listen(GIOChannel *ch, GIOCondition condition, gpointer data)
{
	int ssock, csock;
	unsigned laddr;
	struct sockaddr_in addr;
	char buf[MAXMSG];
	char CN[MAX_DN_LEN];
	ssize_t len;
	gnutls_session session;
	struct hb_quorumd_fns *fns;

	if (condition & G_IO_IN) {
		/* accept the connection */
		ssock = g_io_channel_unix_get_fd(ch);
		laddr = sizeof(addr);
		csock = accept(ssock, (struct sockaddr*)&addr, &laddr);
		if (csock == -1) {
			quorum_log(LOG_ERR, "%s accept socket failed", __FUNCTION__);
			return TRUE;
		}
		memset(CN, 0, MAX_DN_LEN);
		session = initialize_tls_session(csock, CN);
		if (session == NULL) {
			quorum_log(LOG_ERR, "%s tls handshake failed", __FUNCTION__);
			close(csock);
			return TRUE;
		}
		memset(buf,0,MAXMSG);
		len = gnutls_record_recv(session, buf, MAXMSG);		
		if (len <= 0) {
			quorum_log(LOG_ERR, "can't get version info");
			gnutls_bye (session, GNUTLS_SHUT_WR);
			gnutls_deinit (session);
			close(csock);
			return TRUE;
		}
		quorum_debug(LOG_DEBUG, "version:%s(%d)",buf,(int)len);
		fns = get_protocol(buf);
		if(fns != NULL) {
			fns->on_connect(csock,session,CN);
		}
		else {
			quorum_log(LOG_WARNING, "version %s is not supported", buf);
			gnutls_bye (session, GNUTLS_SHUT_WR);
			gnutls_deinit (session);
			close(csock);
		}
	}
	return TRUE;
}
Exemplo n.º 27
0
static int
goodread(TDSSOCKET * tds, unsigned char *buf, int buflen)
{
#ifdef NCBI_FTDS_ALLOW_TDS_80
#ifdef HAVE_GNUTLS
	if (tds->tls_session)
		return gnutls_record_recv(tds->tls_session, buf, buflen);
#elif defined(HAVE_OPENSSL)
	if (tds->tls_session)
		return SSL_read((SSL*) tds->tls_session, buf, buflen);
#endif
#endif
	return tds_goodread(tds, buf, buflen, 0);
}
Exemplo n.º 28
0
static GIOStatus g_io_gnutls_read(GIOChannel *channel, gchar *buf,
				gsize count, gsize *bytes_read, GError **err)
{
	GIOGnuTLSChannel *gnutls_channel = (GIOGnuTLSChannel *) channel;
	GIOStatus status;
	ssize_t result;

	DBG("channel %p count %zu", channel, count);

	*bytes_read = 0;

again:
	status = check_handshake(channel, err);
	if (status != G_IO_STATUS_NORMAL)
		return status;

	result = gnutls_record_recv(gnutls_channel->session, buf, count);

	DBG("result %zd", result);

	if (result == GNUTLS_E_REHANDSHAKE) {
		gnutls_channel->established = FALSE;
		goto again;
	}

	if (result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN) {
		GIOFlags flags = g_io_channel_get_flags(channel);

		if (gnutls_channel->again == TRUE)
			return G_IO_STATUS_AGAIN;

		if (flags & G_IO_FLAG_NONBLOCK)
			return G_IO_STATUS_AGAIN;

		goto again;
	}

	if (result == GNUTLS_E_UNEXPECTED_PACKET_LENGTH)
		return G_IO_STATUS_EOF;

	if (result < 0) {
		g_set_error(err, G_IO_CHANNEL_ERROR,
				G_IO_CHANNEL_ERROR_FAILED, "Stream corrupted");
		return G_IO_STATUS_ERROR;
	}

	*bytes_read = result;

	return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
}
Exemplo n.º 29
0
/* Read @bufsize bytes into @buf from @session. */
static void
server_read (gnutls_session_t session, char *buf, int bufsize)
{
	int total, nread;

	total = 0;
	while (total < bufsize) {
		nread = gnutls_record_recv (session, buf + total,
					    bufsize - total);
		if (nread <= 0)
			g_error ("server read failed at position %d", total);
		total += nread;
	}
}
Exemplo n.º 30
0
test_code_t
test_server (gnutls_session session)
{
  int ret, i = 0;
  char buf[5 * 1024];
  char *p;
  const char snd_buf[] = "GET / HTTP/1.0\n\n";

  if (verbose == 0)
    return TEST_UNSURE;

  buf[sizeof (buf) - 1] = 0;

  ADD_ALL_CIPHERS (session);
  ADD_ALL_COMP (session);
  ADD_ALL_CERTTYPES (session);
  ADD_ALL_PROTOCOLS (session);
  ADD_ALL_MACS (session);
  ADD_ALL_KX (session);

  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);

  ret = do_handshake (session);
  if (ret != TEST_SUCCEED)
    return TEST_FAILED;

  gnutls_record_send (session, snd_buf, sizeof (snd_buf) - 1);
  ret = gnutls_record_recv (session, buf, sizeof (buf) - 1);
  if (ret < 0)
    return TEST_FAILED;

  p = strstr (buf, "Server:");
  if (p != NULL)
    p = strchr (p, ':');
  if (p != NULL)
    {
      p++;
      while (*p != 0 && *p != '\r' && *p != '\n')
	{
	  putc (*p, stdout);
	  p++;
	  i++;
	  if (i > 128)
	    break;
	}
    }

  return TEST_SUCCEED;
}