Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions host/xtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set (SRC
xtest_helpers.c
xtest_main.c
xtest_test.c
xtest_uuid_helpers.c
)

set_source_files_properties(
Expand Down
3 changes: 2 additions & 1 deletion host/xtest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ srcs += adbg/src/adbg_case.c \
stats.c \
xtest_helpers.c \
xtest_main.c \
xtest_test.c
xtest_test.c \
xtest_uuid_helpers.c

ifeq ($(CFG_SECSTOR_TA_MGMT_PTA),y)
srcs += install_ta.c
Expand Down
143 changes: 143 additions & 0 deletions host/xtest/regression_1000.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "xtest_test.h"
#include "xtest_helpers.h"
#include "xtest_uuid_helpers.h"
#include <signed_hdr.h>
#include <util.h>

Expand Down Expand Up @@ -2036,3 +2037,145 @@ static void xtest_tee_test_1025(ADBG_Case_t *c)
}
ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
"Test memref NULL and/or 0 bytes size");

#define TEE_UUID_NS_NAME_SIZE 128

/*
* TEE Client UUID name space identifier (UUIDv4)
*
* Value here is random UUID that is allocated as name space identifier for
* forming Client UUID's for TEE environment using UUIDv5 scheme.
*/
static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";

/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
static TEEC_UUID client_uuid_public = { };

static void xtest_tee_test_1026(ADBG_Case_t *c)
{
TEEC_Result result = TEEC_ERROR_GENERIC;
uint32_t ret_orig = 0;
TEEC_Session session = { };
uint32_t login = UINT32_MAX;
TEEC_UUID client_uuid = { };

result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
Comment thread
vesajaaskelainen marked this conversation as resolved.

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

result = ta_os_test_cmd_client_identity(&session, &login,
&client_uuid);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
goto out;

ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);

ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
sizeof(TEEC_UUID));

out:
TEEC_CloseSession(&session);
}

ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
"Session: public login");

static void xtest_tee_test_1027(ADBG_Case_t *c)
{
TEEC_Result result = TEEC_ERROR_GENERIC;
uint32_t ret_orig = 0;
TEEC_Session session = { };
uint32_t login = UINT32_MAX;
TEEC_UUID client_uuid = { };
TEEC_UUID expected_client_uuid = { };
TEEC_UUID uuid_ns = { };
char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };

result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

sprintf(uuid_name, "uid=%x", geteuid());

result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
strlen(uuid_name));
if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
TEEC_LOGIN_USER, NULL, NULL, &ret_orig);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

result = ta_os_test_cmd_client_identity(&session, &login,
&client_uuid);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
goto out;

ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);

ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
sizeof(TEEC_UUID));

out:
TEEC_CloseSession(&session);
}

ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
"Session: user login for current user");

static void xtest_tee_test_1028(ADBG_Case_t *c)
{
TEEC_Result result = TEEC_ERROR_GENERIC;
uint32_t ret_orig = 0;
TEEC_Session session = { };
uint32_t login = UINT32_MAX;
TEEC_UUID client_uuid = { };
TEEC_UUID expected_client_uuid = { };
TEEC_UUID uuid_ns = { };
char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
uint32_t group = 0;

group = getegid();

result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

sprintf(uuid_name, "gid=%x", group);

result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
strlen(uuid_name));
if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
return;

result = ta_os_test_cmd_client_identity(&session, &login,
&client_uuid);

if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
goto out;

ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);

ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
sizeof(TEEC_UUID));

out:
TEEC_CloseSession(&session);
}

ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
"Session: group login for current user's effective group");
26 changes: 26 additions & 0 deletions host/xtest/xtest_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdio.h>
#include <string.h>
#include <ta_crypt.h>
#include <ta_os_test.h>
#include <utee_defines.h>

#include "xtest_helpers.h"
Expand Down Expand Up @@ -413,6 +414,31 @@ bool ta_crypt_cmd_is_algo_supported(ADBG_Case_t *c, TEEC_Session *s,
return false;
}

TEEC_Result ta_os_test_cmd_client_identity(TEEC_Session *session,
uint32_t *login,
TEEC_UUID *client_uuid)
{
TEEC_Operation operation = { };
TEEC_Result result = TEEC_ERROR_GENERIC;

operation.params[1].tmpref.buffer = client_uuid;
operation.params[1].tmpref.size = sizeof(*client_uuid);

operation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
TEEC_MEMREF_TEMP_OUTPUT,
TEEC_NONE, TEEC_NONE);

result = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_CLIENT_IDENTITY,
&operation, NULL);

if (result != TEEC_SUCCESS)
return result;

*login = operation.params[0].value.a;

return TEEC_SUCCESS;
}

void xtest_mutex_init(pthread_mutex_t *mutex)
{
int e = pthread_mutex_init(mutex, NULL);
Expand Down
4 changes: 4 additions & 0 deletions host/xtest/xtest_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ TEEC_Result ta_crypt_cmd_free_operation(ADBG_Case_t *c,
bool ta_crypt_cmd_is_algo_supported(ADBG_Case_t *c, TEEC_Session *s,
uint32_t alg, uint32_t element);

TEEC_Result ta_os_test_cmd_client_identity(TEEC_Session *session,
uint32_t *login,
TEEC_UUID *client_uuid);

void xtest_add_attr(size_t *attr_count, TEE_Attribute *attrs,
uint32_t attr_id, const void *buf, size_t len);
void xtest_add_attr_value(size_t *attr_count, TEE_Attribute *attrs,
Expand Down
149 changes: 149 additions & 0 deletions host/xtest/xtest_uuid_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2019, Linaro Limited
*/

#include <ctype.h>
#include <endian.h>
#include <openssl/evp.h>
Comment thread
vesajaaskelainen marked this conversation as resolved.
#include <stdint.h>
#include <string.h>
#include <tee_api_types.h>
#include <tee_client_api.h>
#include "xtest_uuid_helpers.h"

static int hex(char c)
{
char lc = tolower(c);

if (isdigit(lc))
return lc - '0';
if (isxdigit(lc))
return lc - 'a' + 10;
return -1;
}

static uint32_t parse_hex(const char *s, size_t nchars, uint32_t *res)
{
uint32_t v = 0;
size_t n = 0;
int c = 0;

for (n = 0; n < nchars; n++) {
c = hex(s[n]);
if (c == -1) {
*res = TEE_ERROR_BAD_FORMAT;
goto out;
}
v = (v << 4) + c;
}
*res = TEE_SUCCESS;
out:
return v;
}

TEEC_Result xtest_uuid_from_str(TEEC_UUID *uuid, const char *s)
{
TEEC_Result res = TEEC_SUCCESS;
TEEC_UUID u = { };
const char *p = s;
size_t i = 0;

if (!p || strnlen(p, 37) != 36)
return TEEC_ERROR_BAD_FORMAT;
if (p[8] != '-' || p[13] != '-' || p[18] != '-' || p[23] != '-')
return TEEC_ERROR_BAD_FORMAT;

u.timeLow = parse_hex(p, 8, &res);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest:

static TEEC_Result advance_dash(const char **s)
{
	if (**s != '-')
		return TEEC_ERROR_BAD_FORMAT;

	(**s)++;

	return TEEC_SUCCESS;
}

static TEEC_Result parse_hex32_and_adance(const char **s, size_t nchars, uint32_t *dst)
{
	uint32_t v = 0;
	size_t n = 0;
	int c = 0;

	for (n = 0; n < nchars; n++) {
		c = hex((*s)[n]);
		if (c == -1)
			return TEE_ERROR_BAD_FORMAT;

		v = (v << 4) + c;
	}

	*dest = v;
	*s += nchars;

	return TEE_SUCCESS;
}

TEEC_Result xtest_uuid_from_str(TEEC_UUID *uuid, const char *s)
{	TEEC_UUID u = { };
	const char *p = s;
	size_t i = 0;

	if (get_hex32_and_advance(&p, 8, &u.timeLow)
	    advance_dash(&p) ||
	    get_hex32_and_advance(&p, 8, &u.timeMid)
	    advance_dash(&p) ||
	    get_hex32_and_advance(&p, 4, &u.timeHiAndVersion)
	    advance_dash(&p))
		return TEEC_ERROR_BAD_FORMAT;

	for (i = 0; i < 8; i++) {
		if (get_hex32_and_advance(&p, 2, u.clockSeqAndNode + i))
			return TEEC_ERROR_BAD_FORMAT;

		if (i < 7 && advance_dash(&p))
			return TEEC_ERROR_BAD_FORMAT;
	}

	*uuid = u;

	return TEE_SUCCESS;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it easier to read? I don't think so. And, the commit simply imports existing code, there is no reason to change it IMO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left as is.

if (res)
goto out;
p += 9;
u.timeMid = parse_hex(p, 4, &res);
if (res)
goto out;
p += 5;
u.timeHiAndVersion = parse_hex(p, 4, &res);
if (res)
goto out;
p += 5;
for (i = 0; i < 8; i++) {
u.clockSeqAndNode[i] = parse_hex(p, 2, &res);
if (res)
goto out;
if (i == 1)
p += 3;
else
p += 2;
}
*uuid = u;
out:
return res;
}

TEEC_Result xtest_uuid_v5(TEEC_UUID *uuid, const TEEC_UUID *ns,
const void *name, size_t size)
{
TEEC_Result res = TEEC_SUCCESS;
EVP_MD_CTX *mdctx = NULL;
const EVP_MD *md = NULL;
unsigned char hash[EVP_MAX_MD_SIZE] = { };
unsigned int md_len = 0;
unsigned char nsbe[16] = { };
uint32_t be32 = 0;
uint16_t be16 = 0;
int ret = 0;

/* Convert from host to big endian */
be32 = htobe32(ns->timeLow);
memcpy(&nsbe[0], &be32, sizeof(be32));
be16 = htobe16(ns->timeMid);
memcpy(&nsbe[4], &be16, sizeof(be16));
be16 = htobe16(ns->timeHiAndVersion);
memcpy(&nsbe[6], &be16, sizeof(be16));
memcpy(&nsbe[8], &ns->clockSeqAndNode, sizeof(ns->clockSeqAndNode));

mdctx = EVP_MD_CTX_create();
if (!mdctx)
return TEEC_ERROR_OUT_OF_MEMORY;
md = EVP_sha1();
if (!md) {
res = TEEC_ERROR_NOT_SUPPORTED;
goto out;
}
ret = EVP_DigestInit_ex(mdctx, md, NULL);
if (!ret) {
res = TEEC_ERROR_GENERIC;
goto out;
}
ret = EVP_DigestUpdate(mdctx, nsbe, sizeof(nsbe));
if (!ret) {
res = TEEC_ERROR_GENERIC;
goto out;
}
ret = EVP_DigestUpdate(mdctx, name, size);
if (!ret) {
res = TEEC_ERROR_GENERIC;
goto out;
}
ret = EVP_DigestFinal_ex(mdctx, hash, &md_len);
if (!ret) {
res = TEEC_ERROR_GENERIC;
goto out;
}

/* Mark it as UUIDv5 */
hash[6] = (hash[6] & 0x0F) | 0x50;
hash[8] = (hash[8] & 0x3F) | 0x80;

/* Convert from big endian to host */
memcpy(&be32, &hash[0], sizeof(uint32_t));
uuid->timeLow = be32toh(be32);
memcpy(&be16, &hash[4], sizeof(uint16_t));
uuid->timeMid = be16toh(be16);
memcpy(&be16, &hash[6], sizeof(uint16_t));
uuid->timeHiAndVersion = be16toh(be16);
memcpy(uuid->clockSeqAndNode, &hash[8], sizeof(uuid->clockSeqAndNode));
out:
EVP_MD_CTX_destroy(mdctx);
return res;
}
23 changes: 23 additions & 0 deletions host/xtest/xtest_uuid_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (c) 2014, STMicroelectronics International N.V.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe * Copyright (c) 2019, Linaro Limited as per new source file xtest_uuid_helpers.c.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a copy from same project other git tree I would not like to change it. But in here I can put what ever is recommended action from project team. Probably should be own commit after the copy or so.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When copying an existing file I don't change the copyrights, I simply add mine if I modify the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left as is.

*/

#ifndef XTEST_UUID_HELPERS_H
#define XTEST_UUID_HELPERS_H

#include <tee_api_types.h>

/*
* Convert a UUID string @s into a TEEC_UUID @uuid
* Expected format for @s is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
* 'x' being any hexadecimal digit (0-9a-fA-F)
*/
TEEC_Result xtest_uuid_from_str(TEEC_UUID *uuid, const char *s);

/*
* Form UUIDv5 from given name space and name.
*/
TEEC_Result xtest_uuid_v5(TEEC_UUID *uuid, const TEEC_UUID *ns,
const void *name, size_t size);
#endif
Loading