Example #1
0
void* run_client(void *arg)
{
	struct sockaddr_un addr = {0};
	themis_status_t status;
	int count = 10;
	ssize_t bytes_sent;

	addr.sun_family = AF_UNIX;
	memcpy(addr.sun_path, SOCKET_NAME, strlen(SOCKET_NAME) + 1);

	client.sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (-1 == client.sock)
	{
		return NULL;
	}

	//unlink(SOCKET_NAME);

	status = secure_session_init(&(client.ctx), CLIENT_ID, strlen(CLIENT_ID), client_priv, sizeof(client_priv), &clb);
	if (HERMES_SUCCESS != status)
	{
		return NULL;
	}

	if (-1 == connect(client.sock, (const struct sockaddr *)&addr, sizeof(addr)))
	{
		return NULL;
	}

	status = secure_session_connect(&(client.ctx));
	if (HERMES_SUCCESS != status)
	{
		return NULL;
	}

	while (count)
	{
		uint8_t buffer[BUF_SIZE];
		ssize_t bytes_received;
		const char *message = "This is a test message";

		bytes_sent = secure_session_send(&(client.ctx), message, strlen(message));
		if (bytes_sent < 0)
		{
			/* Some error, log and continue */
			printf("client send %d %d\n", __LINE__, (int)bytes_sent);
		}

		/* Wait for response */
		bytes_received = secure_session_receive(&(client.ctx), buffer, sizeof(buffer));
		if (bytes_received < 0)
		{
			/* Some error, log and continue */
			printf("client receive %d %d\n", __LINE__, (int)bytes_received);
			continue;
		}

		if (bytes_received > 0)
		{
			if (!memcmp(message, buffer, strlen(message)))
			{
				puts("client receive valid response");
			}
			count--;
		}
	}

	/* send "finish" message to server */
	bytes_sent = secure_session_send(&(client.ctx), "finish", strlen("finish"));
	if (bytes_sent < 0)
	{
		/* Some error, log and continue */
		printf("client send %d %d\n", __LINE__, (int)bytes_sent);
	}

	close(client.sock);
	return NULL;
}
Example #2
0
    void connect(){
      if(secure_session_connect(_session)!=THEMIS_SUCCESS)
	throw themispp::exception_t("Secure Session failed connecting");
    }
static int client_function(void)
{
    static bool connected = false;
    themis_status_t res;
    uint8_t recv_buf[2048];
    ssize_t bytes_received;
    ssize_t bytes_sent;

    /* Client is not connected yet. Initiate key agreement */
    if (!connected) {
        res = secure_session_connect((client.session));
        if (THEMIS_SUCCESS == res) {
            connected = true;
            return TEST_CONTINUE;
        }

        testsuite_fail_if(res, "secure_session_connect failed");
        return TEST_STOP_ERROR;
    }
    if (secure_session_is_established(client.session)) {
        size_t remote_id_length = 0;
        if (THEMIS_BUFFER_TOO_SMALL
            != secure_session_get_remote_id(client.session, NULL, &remote_id_length)) {
            testsuite_fail_if(true, "remote id getting failed (length_determination)");
            return TEST_STOP_ERROR;
        }
        uint8_t* remote_id = malloc(remote_id_length);
        assert(remote_id);
        if (THEMIS_SUCCESS
            != secure_session_get_remote_id(client.session, remote_id, &remote_id_length)) {
            testsuite_fail_if(true, "remote id getting failed");
            free(remote_id);
            return TEST_STOP_ERROR;
        }
        testsuite_fail_unless(remote_id_length == strlen(server.id)
                                  && 0 == memcmp(remote_id, server.id, strlen(server.id)),
                              "secure_session remote id getting");
        free(remote_id);

        static int messages_to_send = MESSAGES_TO_SEND;

        /* Connection is already established. */
        static uint8_t data_to_send[MAX_MESSAGE_SIZE];
        static size_t length_to_send;

        /* If there is anything to receive, receive it */
        if (current_length) {
            bytes_received = secure_session_receive(client.session, recv_buf, sizeof(recv_buf));
            if (bytes_received > 0) {
                /* The server should echo our previously sent data */
                testsuite_fail_unless((length_to_send == (size_t)bytes_received)
                                          && (!memcmp(recv_buf, data_to_send, bytes_received)),
                                      "secure_session message send/receive");
                messages_to_send--;

                if (!messages_to_send) {
                    return TEST_STOP_SUCCESS;
                }
            } else {
                /* We shoud receive something. */
                testsuite_fail_if(bytes_received, "secure_session_receive failed");
                return TEST_STOP_ERROR;
            }
        }

        length_to_send = rand_int(MAX_MESSAGE_SIZE);

        if (THEMIS_SUCCESS != soter_rand(data_to_send, length_to_send)) {
            testsuite_fail_if(true, "soter_rand failed");
            return TEST_STOP_ERROR;
        }

        bytes_sent = secure_session_send((client.session), data_to_send, length_to_send);
        if (bytes_sent > 0) {
            /* Check whether data was indeed encrypted (it should not be the same as in
             * data_to_send) */
            testsuite_fail_if((length_to_send == current_length)
                                  || (!memcmp(data_to_send, shared_mem, length_to_send)),
                              "secure_session client message wrap");
            return TEST_CONTINUE;
        }

        testsuite_fail_if(true, "secure_session_send failed");
        return TEST_STOP_ERROR;
    }
    /* Connection is not established. We should receive some key agreement data. */

    bytes_received = secure_session_receive((client.session), recv_buf, sizeof(recv_buf));
    /* When key agreement data is received and processed client gets 0 in return value (no data
     * for client is received) */
    if (bytes_received) {
        testsuite_fail_if(bytes_received, "secure_session_receive failed");
        return TEST_STOP_ERROR;
    }

    if (secure_session_is_established((client.session))) {
        /* Negotiation completed. Clear the shared memory. */
        current_length = 0;
    }

    return TEST_CONTINUE;
}