xref: /optee_os/lib/libutee/tee_api_property.c (revision 614e8b8a370ed9c32a569d4c39f520fbb016044a)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4a32a96edSJens Wiklander  * Copyright (c) 2017, Linaro Limited
5b0104773SPascal Brand  */
6a32a96edSJens Wiklander #include <printk.h>
7a32a96edSJens Wiklander #include <stdio.h>
8b0104773SPascal Brand #include <stdlib.h>
9b0104773SPascal Brand #include <string.h>
10b0104773SPascal Brand #include <tee_api_defines.h>
11a32a96edSJens Wiklander #include <tee_api.h>
12b0104773SPascal Brand #include <tee_api_types.h>
13647f9c76SJerome Forissier #include <tee_arith_internal.h>
14a32a96edSJens Wiklander #include <tee_internal_api_extensions.h>
15a32a96edSJens Wiklander #include <tee_isocket.h>
16a32a96edSJens Wiklander #include <user_ta_header.h>
17b0104773SPascal Brand #include <utee_syscalls.h>
18a32a96edSJens Wiklander #include <util.h>
19b0104773SPascal Brand 
20b0104773SPascal Brand #include "string_ext.h"
21b0104773SPascal Brand #include "base64.h"
22b0104773SPascal Brand 
23b0104773SPascal Brand #define PROP_STR_MAX    80
24b0104773SPascal Brand 
25b0104773SPascal Brand #define PROP_ENUMERATOR_NOT_STARTED 0xffffffff
26b0104773SPascal Brand 
27b0104773SPascal Brand struct prop_enumerator {
2864a5011eSPascal Brand 	uint32_t idx;			/* current index */
2964a5011eSPascal Brand 	TEE_PropSetHandle prop_set;	/* part of TEE_PROPSET_xxx */
30b0104773SPascal Brand };
31b0104773SPascal Brand 
3264a5011eSPascal Brand const struct user_ta_property tee_props[] = {
3364a5011eSPascal Brand 	{
3464a5011eSPascal Brand 		"gpd.tee.arith.maxBigIntSize",
3564a5011eSPascal Brand 		USER_TA_PROP_TYPE_U32,
36e3458e03SJerome Forissier 		&(const uint32_t){CFG_TA_BIGNUM_MAX_BITS}
3764a5011eSPascal Brand 	},
38a32a96edSJens Wiklander 	{
39a32a96edSJens Wiklander 		"gpd.tee.sockets.version",
40a32a96edSJens Wiklander 		USER_TA_PROP_TYPE_U32,
41a32a96edSJens Wiklander 		&(const uint32_t){TEE_ISOCKET_VERSION}
42a32a96edSJens Wiklander 	},
43a32a96edSJens Wiklander 	{
44a32a96edSJens Wiklander 		"gpd.tee.sockets.tcp.version",
45a32a96edSJens Wiklander 		USER_TA_PROP_TYPE_U32,
46a32a96edSJens Wiklander 		&(const uint32_t){TEE_ISOCKET_VERSION}
47a32a96edSJens Wiklander 	},
48b0104773SPascal Brand };
49b0104773SPascal Brand 
5064a5011eSPascal Brand static TEE_Result propset_get(TEE_PropSetHandle h,
51b0104773SPascal Brand 			      const struct user_ta_property **eps,
52b0104773SPascal Brand 			      size_t *eps_len)
53b0104773SPascal Brand {
54b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA) {
55b0104773SPascal Brand 		*eps = ta_props;
56b0104773SPascal Brand 		*eps_len = ta_num_props;
57b0104773SPascal Brand 	} else if (h == TEE_PROPSET_CURRENT_CLIENT) {
58b0104773SPascal Brand 		*eps = NULL;
59b0104773SPascal Brand 		*eps_len = 0;
60b0104773SPascal Brand 	} else if (h == TEE_PROPSET_TEE_IMPLEMENTATION) {
6164a5011eSPascal Brand 		*eps = tee_props;
6264a5011eSPascal Brand 		*eps_len = ARRAY_SIZE(tee_props);
63b0104773SPascal Brand 	} else {
64b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
65b0104773SPascal Brand 	}
66b0104773SPascal Brand 
67b0104773SPascal Brand 	return TEE_SUCCESS;
68b0104773SPascal Brand }
69b0104773SPascal Brand 
70b0104773SPascal Brand static TEE_Result propget_get_ext_prop(const struct user_ta_property *ep,
71ff857a3aSPascal Brand 				       enum user_ta_prop_type *type,
72ff857a3aSPascal Brand 				       void *buf, uint32_t *len)
73b0104773SPascal Brand {
74b0104773SPascal Brand 	size_t l;
75b0104773SPascal Brand 
76ff857a3aSPascal Brand 	*type = ep->type;
77ff857a3aSPascal Brand 	switch (*type) {
78b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
79d5d50c3cSJens Wiklander 		l = sizeof(bool);
80b0104773SPascal Brand 		break;
81b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
82b0104773SPascal Brand 		l = sizeof(uint32_t);
83b0104773SPascal Brand 		break;
84b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
85b0104773SPascal Brand 		l = sizeof(TEE_UUID);
86b0104773SPascal Brand 		break;
87b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
88b0104773SPascal Brand 		l = sizeof(TEE_Identity);
89b0104773SPascal Brand 		break;
90b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
91ff857a3aSPascal Brand 		/* take the leading 0 into account */
92ff857a3aSPascal Brand 		l = strlen(ep->value) + 1;
93ff857a3aSPascal Brand 		break;
94b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
95ff857a3aSPascal Brand 		/*
96ff857a3aSPascal Brand 		 * in case of TA property, a binary block is provided as a
97ff857a3aSPascal Brand 		 * string, which is base64 encoded. We must first decode it,
98ff857a3aSPascal Brand 		 * without taking into account the zero termination of the
99ff857a3aSPascal Brand 		 * string
100ff857a3aSPascal Brand 		 */
101ff857a3aSPascal Brand 		l = *len;
102ff857a3aSPascal Brand 		if (!base64_dec(ep->value, strlen(ep->value), buf, &l) &&
103ff857a3aSPascal Brand 		    (l <= *len))
104ff857a3aSPascal Brand 			return TEE_ERROR_GENERIC;
105ff857a3aSPascal Brand 		if (*len < l) {
106ff857a3aSPascal Brand 			*len = l;
107ff857a3aSPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
108ff857a3aSPascal Brand 		}
109ff857a3aSPascal Brand 
110ff857a3aSPascal Brand 		*len = l;
111b0104773SPascal Brand 		return TEE_SUCCESS;
112b0104773SPascal Brand 	default:
113b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
114b0104773SPascal Brand 	}
115ff857a3aSPascal Brand 
116ff857a3aSPascal Brand 	if (*len < l) {
117ff857a3aSPascal Brand 		*len = l;
118ff857a3aSPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
119ff857a3aSPascal Brand 	}
120ff857a3aSPascal Brand 
121ff857a3aSPascal Brand 	*len = l;
122ff857a3aSPascal Brand 	memcpy(buf, ep->value, l);
123b0104773SPascal Brand 	return TEE_SUCCESS;
124b0104773SPascal Brand }
125b0104773SPascal Brand 
1268f07fe6fSJerome Forissier static TEE_Result propget_get_property(TEE_PropSetHandle h, const char *name,
127ff857a3aSPascal Brand 				       enum user_ta_prop_type *type,
128ff857a3aSPascal Brand 				       void *buf, uint32_t *len)
129b0104773SPascal Brand {
130b0104773SPascal Brand 	TEE_Result res;
131b0104773SPascal Brand 	const struct user_ta_property *eps;
132b0104773SPascal Brand 	size_t eps_len;
13364a5011eSPascal Brand 	uint32_t prop_type;
13464a5011eSPascal Brand 	uint32_t index;
135b0104773SPascal Brand 
136b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA || h == TEE_PROPSET_CURRENT_CLIENT ||
137b0104773SPascal Brand 	    h == TEE_PROPSET_TEE_IMPLEMENTATION) {
138b0104773SPascal Brand 		size_t n;
139b0104773SPascal Brand 
14064a5011eSPascal Brand 		res = propset_get(h, &eps, &eps_len);
141b0104773SPascal Brand 		if (res != TEE_SUCCESS)
142b0104773SPascal Brand 			return res;
143b0104773SPascal Brand 
144b0104773SPascal Brand 		for (n = 0; n < eps_len; n++) {
14564a5011eSPascal Brand 			if (!strcmp(name, eps[n].name))
146ff857a3aSPascal Brand 				return propget_get_ext_prop(eps + n, type,
147ff857a3aSPascal Brand 							    buf, len);
148b0104773SPascal Brand 		}
14964a5011eSPascal Brand 
15064a5011eSPascal Brand 		/* get the index from the name */
15164a5011eSPascal Brand 		res = utee_get_property_name_to_index((unsigned long)h, name,
15264a5011eSPascal Brand 						strlen(name) + 1, &index);
15364a5011eSPascal Brand 		if (res != TEE_SUCCESS)
15464a5011eSPascal Brand 			return res;
155ff857a3aSPascal Brand 		res = utee_get_property((unsigned long)h, index, NULL, NULL,
156ff857a3aSPascal Brand 					buf, len, &prop_type);
157b0104773SPascal Brand 	} else {
158b0104773SPascal Brand 		struct prop_enumerator *pe = (struct prop_enumerator *)h;
159b0104773SPascal Brand 		uint32_t idx = pe->idx;
160b0104773SPascal Brand 
161b0104773SPascal Brand 		if (idx == PROP_ENUMERATOR_NOT_STARTED)
162b0104773SPascal Brand 			return TEE_ERROR_ITEM_NOT_FOUND;
163b0104773SPascal Brand 
16464a5011eSPascal Brand 		res = propset_get(pe->prop_set, &eps, &eps_len);
165b0104773SPascal Brand 		if (res != TEE_SUCCESS)
166b0104773SPascal Brand 			return res;
167b0104773SPascal Brand 
168b0104773SPascal Brand 		if (idx < eps_len)
169ff857a3aSPascal Brand 			return propget_get_ext_prop(eps + idx, type, buf, len);
17064a5011eSPascal Brand 		idx -= eps_len;
171b0104773SPascal Brand 
17264a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set, idx,
173ff857a3aSPascal Brand 					NULL, NULL, buf, len, &prop_type);
17464a5011eSPascal Brand 		if (res == TEE_ERROR_ITEM_NOT_FOUND)
17564a5011eSPascal Brand 			res = TEE_ERROR_BAD_PARAMETERS;
176b0104773SPascal Brand 	}
17764a5011eSPascal Brand 
178ff857a3aSPascal Brand 	*type = prop_type;
17964a5011eSPascal Brand 	return res;
180b0104773SPascal Brand }
181b0104773SPascal Brand 
182b0104773SPascal Brand TEE_Result TEE_GetPropertyAsString(TEE_PropSetHandle propsetOrEnumerator,
1838f07fe6fSJerome Forissier 				   const char *name, char *value,
1840dd3f3a4SPascal Brand 				   uint32_t *value_len)
185b0104773SPascal Brand {
186b0104773SPascal Brand 	TEE_Result res;
187b0104773SPascal Brand 	size_t l;
188ff857a3aSPascal Brand 	enum user_ta_prop_type type;
189ff857a3aSPascal Brand 	void *tmp_buf = 0;
190ff857a3aSPascal Brand 	uint32_t tmp_len;
191ff857a3aSPascal Brand 	uint32_t uint32_val;
192d5d50c3cSJens Wiklander 	bool bool_val;
193ff857a3aSPascal Brand 	TEE_Identity *p_identity_val;
194b0104773SPascal Brand 
1950dd3f3a4SPascal Brand 	if (!value || !value_len) {
196ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
197ff857a3aSPascal Brand 		goto out;
198ba675d69SCedric Chaumont 	}
199b0104773SPascal Brand 
2000dd3f3a4SPascal Brand 	tmp_len = *value_len;
201ff857a3aSPascal Brand 	if (tmp_len < sizeof(TEE_Identity))
202ff857a3aSPascal Brand 		tmp_len = sizeof(TEE_Identity);
203ff857a3aSPascal Brand 	tmp_buf = TEE_Malloc(tmp_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);
204ff857a3aSPascal Brand 	if (!tmp_buf) {
205ff857a3aSPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
206ff857a3aSPascal Brand 		goto out;
207ff857a3aSPascal Brand 	}
208b0104773SPascal Brand 
209ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
210ff857a3aSPascal Brand 				   tmp_buf, &tmp_len);
211ff857a3aSPascal Brand 	if (res != TEE_SUCCESS) {
212254e1d58SPascal Brand 		if (res == TEE_ERROR_SHORT_BUFFER) {
213254e1d58SPascal Brand 			if (type == USER_TA_PROP_TYPE_BINARY_BLOCK) {
214254e1d58SPascal Brand 				/*
215254e1d58SPascal Brand 				 * in this case, we must enlarge the buffer
216254e1d58SPascal Brand 				 * with the size of the of the base64 encoded
217254e1d58SPascal Brand 				 * see base64_enc() function
218254e1d58SPascal Brand 				 */
219254e1d58SPascal Brand 				tmp_len = base64_enc_len(tmp_len);
220254e1d58SPascal Brand 			}
2210dd3f3a4SPascal Brand 			*value_len = tmp_len;
222254e1d58SPascal Brand 		}
223ff857a3aSPascal Brand 		goto out;
224ff857a3aSPascal Brand 	}
225b0104773SPascal Brand 
226ff857a3aSPascal Brand 	switch (type) {
227b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
228d5d50c3cSJens Wiklander 		bool_val = *((bool *)tmp_buf);
229d5d50c3cSJens Wiklander 		l = strlcpy(value, (bool_val ? "true" : "false"), *value_len);
230b0104773SPascal Brand 		break;
231b0104773SPascal Brand 
232b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
233ff857a3aSPascal Brand 		uint32_val = *((uint32_t *)tmp_buf);
2340dd3f3a4SPascal Brand 		l = snprintf(value, *value_len, "%u", uint32_val);
235b0104773SPascal Brand 		break;
236b0104773SPascal Brand 
237b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
2380dd3f3a4SPascal Brand 		l = snprintk(value, *value_len, "%pUl", tmp_buf);
239b0104773SPascal Brand 		break;
240b0104773SPascal Brand 
241b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
242ff857a3aSPascal Brand 		p_identity_val = ((TEE_Identity *)tmp_buf);
2430dd3f3a4SPascal Brand 		l = snprintk(value, *value_len, "%u:%pUl",
244ff857a3aSPascal Brand 			     p_identity_val->login,
245ff857a3aSPascal Brand 			     (void *)(&(p_identity_val->uuid)));
246b0104773SPascal Brand 		break;
247b0104773SPascal Brand 
248b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
2490dd3f3a4SPascal Brand 		l = strlcpy(value, tmp_buf, *value_len);
250ff857a3aSPascal Brand 		break;
251ff857a3aSPascal Brand 
252b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
2530dd3f3a4SPascal Brand 		l = *value_len;	/* l includes the zero-termination */
2540dd3f3a4SPascal Brand 		if (!base64_enc(tmp_buf, tmp_len, value, &l) &&
2550dd3f3a4SPascal Brand 		    (l <= *value_len)) {
256ff857a3aSPascal Brand 			res = TEE_ERROR_GENERIC;
257ff857a3aSPascal Brand 			goto out;
258ff857a3aSPascal Brand 		}
259ff857a3aSPascal Brand 		l--;	/* remove the zero-termination that is added later */
260b0104773SPascal Brand 		break;
261b0104773SPascal Brand 
262b0104773SPascal Brand 	default:
263ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_FORMAT;
264ba675d69SCedric Chaumont 		goto out;
265ff857a3aSPascal Brand 	}
266ba675d69SCedric Chaumont 
267ff857a3aSPascal Brand 	l++;	/* include zero termination */
268ff857a3aSPascal Brand 
2690dd3f3a4SPascal Brand 	if (l > *value_len)
270ff857a3aSPascal Brand 		res = TEE_ERROR_SHORT_BUFFER;
2710dd3f3a4SPascal Brand 	*value_len = l;
272ff857a3aSPascal Brand 
273ba675d69SCedric Chaumont out:
274ff857a3aSPascal Brand 	if (tmp_buf)
275ff857a3aSPascal Brand 		TEE_Free(tmp_buf);
276ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
277ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
278ff857a3aSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
279ff857a3aSPascal Brand 		TEE_Panic(0);
280ff857a3aSPascal Brand 
281ff857a3aSPascal Brand 	return res;
282b0104773SPascal Brand }
283b0104773SPascal Brand 
284b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBool(TEE_PropSetHandle propsetOrEnumerator,
2858f07fe6fSJerome Forissier 				 const char *name, bool *value)
286b0104773SPascal Brand {
287b0104773SPascal Brand 	TEE_Result res;
288ff857a3aSPascal Brand 	enum user_ta_prop_type type;
289d5d50c3cSJens Wiklander 	uint32_t bool_len = sizeof(bool);
290ba675d69SCedric Chaumont 	if (value == NULL) {
291ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
292ff857a3aSPascal Brand 		goto out;
293ba675d69SCedric Chaumont 	}
294b0104773SPascal Brand 
295ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_BOOL;
296ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
297d5d50c3cSJens Wiklander 				   value, &bool_len);
298ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_BOOL)
299ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_FORMAT;
300ff857a3aSPascal Brand 	if (res != TEE_SUCCESS)
301ba675d69SCedric Chaumont 		goto out;
302ba675d69SCedric Chaumont 
303ba675d69SCedric Chaumont out:
304ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
305ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
306ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
307ff857a3aSPascal Brand 		TEE_Panic(0);
308ff857a3aSPascal Brand 
309ff857a3aSPascal Brand 	return res;
310b0104773SPascal Brand }
311b0104773SPascal Brand 
312b0104773SPascal Brand TEE_Result TEE_GetPropertyAsU32(TEE_PropSetHandle propsetOrEnumerator,
3138f07fe6fSJerome Forissier 				const char *name, uint32_t *value)
314b0104773SPascal Brand {
315b0104773SPascal Brand 	TEE_Result res;
316ff857a3aSPascal Brand 	enum user_ta_prop_type type;
317ff857a3aSPascal Brand 	uint32_t uint32_len = sizeof(uint32_t);
318b0104773SPascal Brand 
3190dd3f3a4SPascal Brand 	if (!value) {
320ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
321ba675d69SCedric Chaumont 		goto out;
322ff857a3aSPascal Brand 	}
323ba675d69SCedric Chaumont 
324ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_U32;
325ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
326ff857a3aSPascal Brand 				   value, &uint32_len);
327ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_U32)
328ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
329ff857a3aSPascal Brand 
330ba675d69SCedric Chaumont out:
331ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
332ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
333ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
334ff857a3aSPascal Brand 		TEE_Panic(0);
335ff857a3aSPascal Brand 
336ff857a3aSPascal Brand 	return res;
337b0104773SPascal Brand }
338b0104773SPascal Brand 
339b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBinaryBlock(TEE_PropSetHandle propsetOrEnumerator,
3408f07fe6fSJerome Forissier 					const char *name, void *value,
3410dd3f3a4SPascal Brand 					uint32_t *value_len)
342b0104773SPascal Brand {
343b0104773SPascal Brand 	TEE_Result res;
344ff857a3aSPascal Brand 	enum user_ta_prop_type type;
345b0104773SPascal Brand 
346*614e8b8aSEtienne Carriere 	if (!value_len) {
347ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
348ba675d69SCedric Chaumont 		goto out;
349ff857a3aSPascal Brand 	}
350ba675d69SCedric Chaumont 
351ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_BINARY_BLOCK;
352ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
3530dd3f3a4SPascal Brand 				   value, value_len);
354ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_BINARY_BLOCK)
355ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
356ff857a3aSPascal Brand 
357ba675d69SCedric Chaumont out:
358ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
359ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
360ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT &&
361ff857a3aSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
362ff857a3aSPascal Brand 		TEE_Panic(0);
363ff857a3aSPascal Brand 
364ff857a3aSPascal Brand 	return res;
365b0104773SPascal Brand }
366b0104773SPascal Brand 
367b0104773SPascal Brand TEE_Result TEE_GetPropertyAsUUID(TEE_PropSetHandle propsetOrEnumerator,
3688f07fe6fSJerome Forissier 				 const char *name, TEE_UUID *value)
369b0104773SPascal Brand {
370b0104773SPascal Brand 	TEE_Result res;
371ff857a3aSPascal Brand 	enum user_ta_prop_type type;
372ff857a3aSPascal Brand 	uint32_t uuid_len = sizeof(TEE_UUID);
373b0104773SPascal Brand 
3740dd3f3a4SPascal Brand 	if (!value) {
375ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
376ba675d69SCedric Chaumont 		goto out;
377ff857a3aSPascal Brand 	}
378ba675d69SCedric Chaumont 
379ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_UUID;
380ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
381ff857a3aSPascal Brand 				   value, &uuid_len);
382ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_UUID)
383ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
384ff857a3aSPascal Brand 
385ba675d69SCedric Chaumont out:
386ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
387ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
388ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
389ff857a3aSPascal Brand 		TEE_Panic(0);
390ff857a3aSPascal Brand 
391ff857a3aSPascal Brand 	return res;
392b0104773SPascal Brand }
393b0104773SPascal Brand 
394b0104773SPascal Brand TEE_Result TEE_GetPropertyAsIdentity(TEE_PropSetHandle propsetOrEnumerator,
3958f07fe6fSJerome Forissier 				     const char *name, TEE_Identity *value)
396b0104773SPascal Brand {
397b0104773SPascal Brand 	TEE_Result res;
398ff857a3aSPascal Brand 	enum user_ta_prop_type type;
399ff857a3aSPascal Brand 	uint32_t identity_len = sizeof(TEE_Identity);
400b0104773SPascal Brand 
4010dd3f3a4SPascal Brand 	if (!value) {
402ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
403ba675d69SCedric Chaumont 		goto out;
404ff857a3aSPascal Brand 	}
405ba675d69SCedric Chaumont 
406ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_IDENTITY;
407ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
408ff857a3aSPascal Brand 				   value, &identity_len);
409ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_IDENTITY)
410ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
411ff857a3aSPascal Brand 
412ba675d69SCedric Chaumont out:
413ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
414ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
415ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
416ff857a3aSPascal Brand 		TEE_Panic(0);
417ff857a3aSPascal Brand 
418ff857a3aSPascal Brand 	return res;
419b0104773SPascal Brand }
420b0104773SPascal Brand 
421b0104773SPascal Brand TEE_Result TEE_AllocatePropertyEnumerator(TEE_PropSetHandle *enumerator)
422b0104773SPascal Brand {
423ba675d69SCedric Chaumont 	TEE_Result res;
424b0104773SPascal Brand 	struct prop_enumerator *pe;
425b0104773SPascal Brand 
4260dd3f3a4SPascal Brand 	if (!enumerator) {
427ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
428ba675d69SCedric Chaumont 		goto err;
429ba675d69SCedric Chaumont 	}
430b0104773SPascal Brand 
431b0104773SPascal Brand 	pe = TEE_Malloc(sizeof(struct prop_enumerator),
432b0104773SPascal Brand 			TEE_USER_MEM_HINT_NO_FILL_ZERO);
433ba675d69SCedric Chaumont 	if (pe == NULL) {
434ba675d69SCedric Chaumont 		res = TEE_ERROR_OUT_OF_MEMORY;
435ba675d69SCedric Chaumont 		goto err;
436ba675d69SCedric Chaumont 	}
437b0104773SPascal Brand 
438b0104773SPascal Brand 	*enumerator = (TEE_PropSetHandle) pe;
439b0104773SPascal Brand 	TEE_ResetPropertyEnumerator(*enumerator);
440ba675d69SCedric Chaumont 
441ba675d69SCedric Chaumont 	goto out;
442ba675d69SCedric Chaumont 
443ba675d69SCedric Chaumont err:
444ba675d69SCedric Chaumont 	if (res == TEE_ERROR_OUT_OF_MEMORY)
445ba675d69SCedric Chaumont 		return res;
446ba675d69SCedric Chaumont 	TEE_Panic(0);
447ba675d69SCedric Chaumont out:
448b0104773SPascal Brand 	return TEE_SUCCESS;
449b0104773SPascal Brand }
450b0104773SPascal Brand 
451b0104773SPascal Brand void TEE_ResetPropertyEnumerator(TEE_PropSetHandle enumerator)
452b0104773SPascal Brand {
453b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
454b0104773SPascal Brand 
455b0104773SPascal Brand 	pe->idx = PROP_ENUMERATOR_NOT_STARTED;
456b0104773SPascal Brand }
457b0104773SPascal Brand 
458b0104773SPascal Brand void TEE_FreePropertyEnumerator(TEE_PropSetHandle enumerator)
459b0104773SPascal Brand {
460b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
461b0104773SPascal Brand 
462b0104773SPascal Brand 	TEE_Free(pe);
463b0104773SPascal Brand }
464b0104773SPascal Brand 
465b0104773SPascal Brand void TEE_StartPropertyEnumerator(TEE_PropSetHandle enumerator,
466b0104773SPascal Brand 				 TEE_PropSetHandle propSet)
467b0104773SPascal Brand {
468b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
469b0104773SPascal Brand 
4700dd3f3a4SPascal Brand 	if (!pe)
471b0104773SPascal Brand 		return;
472b0104773SPascal Brand 
473b0104773SPascal Brand 	pe->idx = 0;
474b0104773SPascal Brand 	pe->prop_set = propSet;
475b0104773SPascal Brand }
476b0104773SPascal Brand 
477b0104773SPascal Brand TEE_Result TEE_GetPropertyName(TEE_PropSetHandle enumerator,
4780dd3f3a4SPascal Brand 			       void *name, uint32_t *name_len)
479b0104773SPascal Brand {
480b0104773SPascal Brand 	TEE_Result res;
481b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
482b0104773SPascal Brand 	const struct user_ta_property *eps;
483b0104773SPascal Brand 	size_t eps_len;
484b0104773SPascal Brand 	const char *str;
485b0104773SPascal Brand 	size_t bufferlen;
486b0104773SPascal Brand 
4870dd3f3a4SPascal Brand 	if (!pe || !name || !name_len) {
488ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
489ba675d69SCedric Chaumont 		goto err;
490ba675d69SCedric Chaumont 	}
491b0104773SPascal Brand 
4920dd3f3a4SPascal Brand 	bufferlen = *name_len;
49364a5011eSPascal Brand 	res = propset_get(pe->prop_set, &eps, &eps_len);
494b0104773SPascal Brand 	if (res != TEE_SUCCESS)
495ba675d69SCedric Chaumont 		goto err;
496b0104773SPascal Brand 
49764a5011eSPascal Brand 	if (pe->idx < eps_len) {
49864a5011eSPascal Brand 		str = eps[pe->idx].name;
4990dd3f3a4SPascal Brand 		bufferlen = strlcpy(name, str, *name_len) + 1;
5000dd3f3a4SPascal Brand 		if (bufferlen > *name_len)
501ba675d69SCedric Chaumont 			res = TEE_ERROR_SHORT_BUFFER;
5020dd3f3a4SPascal Brand 		*name_len = bufferlen;
50364a5011eSPascal Brand 	} else {
50464a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set,
50564a5011eSPascal Brand 					pe->idx - eps_len,
5060dd3f3a4SPascal Brand 					name, name_len, NULL, NULL, NULL);
50764a5011eSPascal Brand 		if (res != TEE_SUCCESS)
508ba675d69SCedric Chaumont 			goto err;
509ba675d69SCedric Chaumont 	}
510b0104773SPascal Brand 
511ba675d69SCedric Chaumont err:
51264a5011eSPascal Brand 	if (res != TEE_SUCCESS &&
51364a5011eSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
51464a5011eSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
515ba675d69SCedric Chaumont 		TEE_Panic(0);
51664a5011eSPascal Brand 	return res;
517b0104773SPascal Brand }
518b0104773SPascal Brand 
519b0104773SPascal Brand TEE_Result TEE_GetNextProperty(TEE_PropSetHandle enumerator)
520b0104773SPascal Brand {
521b0104773SPascal Brand 	TEE_Result res;
522b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
523b0104773SPascal Brand 	uint32_t next_idx;
524b0104773SPascal Brand 	const struct user_ta_property *eps;
525b0104773SPascal Brand 	size_t eps_len;
526b0104773SPascal Brand 
5270dd3f3a4SPascal Brand 	if (!pe) {
528ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
52964a5011eSPascal Brand 		goto out;
530ba675d69SCedric Chaumont 	}
531b0104773SPascal Brand 
532ba675d69SCedric Chaumont 	if (pe->idx == PROP_ENUMERATOR_NOT_STARTED) {
533ba675d69SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
53464a5011eSPascal Brand 		goto out;
535ba675d69SCedric Chaumont 	}
536b0104773SPascal Brand 
53764a5011eSPascal Brand 	res = propset_get(pe->prop_set, &eps, &eps_len);
538b0104773SPascal Brand 	if (res != TEE_SUCCESS)
53964a5011eSPascal Brand 		goto out;
540b0104773SPascal Brand 
541b0104773SPascal Brand 	next_idx = pe->idx + 1;
542b0104773SPascal Brand 	pe->idx = next_idx;
54364a5011eSPascal Brand 	if (next_idx < eps_len)
54464a5011eSPascal Brand 		res = TEE_SUCCESS;
54564a5011eSPascal Brand 	else
54664a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set,
547ff857a3aSPascal Brand 					next_idx - eps_len,
548ff857a3aSPascal Brand 					NULL, NULL, NULL, NULL, NULL);
549b0104773SPascal Brand 
550ba675d69SCedric Chaumont out:
55164a5011eSPascal Brand 	if (res != TEE_SUCCESS &&
55264a5011eSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND)
55364a5011eSPascal Brand 		TEE_Panic(0);
55464a5011eSPascal Brand 	return res;
555b0104773SPascal Brand }
556