xref: /optee_os/lib/libutee/tee_api_property.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1*1bb92983SJerome 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  * All rights reserved.
6b0104773SPascal Brand  *
7b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
8b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
9b0104773SPascal Brand  *
10b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
11b0104773SPascal Brand  * this list of conditions and the following disclaimer.
12b0104773SPascal Brand  *
13b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
14b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
15b0104773SPascal Brand  * and/or other materials provided with the distribution.
16b0104773SPascal Brand  *
17b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
28b0104773SPascal Brand  */
29a32a96edSJens Wiklander #include <printk.h>
30a32a96edSJens Wiklander #include <stdio.h>
31b0104773SPascal Brand #include <stdlib.h>
32b0104773SPascal Brand #include <string.h>
33b0104773SPascal Brand #include <tee_api_defines.h>
34a32a96edSJens Wiklander #include <tee_api.h>
35b0104773SPascal Brand #include <tee_api_types.h>
36647f9c76SJerome Forissier #include <tee_arith_internal.h>
37a32a96edSJens Wiklander #include <tee_internal_api_extensions.h>
38a32a96edSJens Wiklander #include <tee_isocket.h>
39a32a96edSJens Wiklander #include <user_ta_header.h>
40b0104773SPascal Brand #include <utee_syscalls.h>
41a32a96edSJens Wiklander #include <util.h>
42b0104773SPascal Brand 
43b0104773SPascal Brand #include "string_ext.h"
44b0104773SPascal Brand #include "base64.h"
45b0104773SPascal Brand 
46b0104773SPascal Brand #define PROP_STR_MAX    80
47b0104773SPascal Brand 
48b0104773SPascal Brand #define PROP_ENUMERATOR_NOT_STARTED 0xffffffff
49b0104773SPascal Brand 
50b0104773SPascal Brand struct prop_enumerator {
5164a5011eSPascal Brand 	uint32_t idx;			/* current index */
5264a5011eSPascal Brand 	TEE_PropSetHandle prop_set;	/* part of TEE_PROPSET_xxx */
53b0104773SPascal Brand };
54b0104773SPascal Brand 
5564a5011eSPascal Brand const struct user_ta_property tee_props[] = {
5664a5011eSPascal Brand 	{
5764a5011eSPascal Brand 		"gpd.tee.arith.maxBigIntSize",
5864a5011eSPascal Brand 		USER_TA_PROP_TYPE_U32,
5964a5011eSPascal Brand 		&(const uint32_t){TEE_MAX_NUMBER_OF_SUPPORTED_BITS}
6064a5011eSPascal Brand 	},
61a32a96edSJens Wiklander 	{
62a32a96edSJens Wiklander 		"gpd.tee.sockets.version",
63a32a96edSJens Wiklander 		USER_TA_PROP_TYPE_U32,
64a32a96edSJens Wiklander 		&(const uint32_t){TEE_ISOCKET_VERSION}
65a32a96edSJens Wiklander 	},
66a32a96edSJens Wiklander 	{
67a32a96edSJens Wiklander 		"gpd.tee.sockets.tcp.version",
68a32a96edSJens Wiklander 		USER_TA_PROP_TYPE_U32,
69a32a96edSJens Wiklander 		&(const uint32_t){TEE_ISOCKET_VERSION}
70a32a96edSJens Wiklander 	},
71b0104773SPascal Brand };
72b0104773SPascal Brand 
7364a5011eSPascal Brand static TEE_Result propset_get(TEE_PropSetHandle h,
74b0104773SPascal Brand 			      const struct user_ta_property **eps,
75b0104773SPascal Brand 			      size_t *eps_len)
76b0104773SPascal Brand {
77b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA) {
78b0104773SPascal Brand 		*eps = ta_props;
79b0104773SPascal Brand 		*eps_len = ta_num_props;
80b0104773SPascal Brand 	} else if (h == TEE_PROPSET_CURRENT_CLIENT) {
81b0104773SPascal Brand 		*eps = NULL;
82b0104773SPascal Brand 		*eps_len = 0;
83b0104773SPascal Brand 	} else if (h == TEE_PROPSET_TEE_IMPLEMENTATION) {
8464a5011eSPascal Brand 		*eps = tee_props;
8564a5011eSPascal Brand 		*eps_len = ARRAY_SIZE(tee_props);
86b0104773SPascal Brand 	} else {
87b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
88b0104773SPascal Brand 	}
89b0104773SPascal Brand 
90b0104773SPascal Brand 	return TEE_SUCCESS;
91b0104773SPascal Brand }
92b0104773SPascal Brand 
93b0104773SPascal Brand static TEE_Result propget_get_ext_prop(const struct user_ta_property *ep,
94ff857a3aSPascal Brand 				       enum user_ta_prop_type *type,
95ff857a3aSPascal Brand 				       void *buf, uint32_t *len)
96b0104773SPascal Brand {
97b0104773SPascal Brand 	size_t l;
98b0104773SPascal Brand 
99ff857a3aSPascal Brand 	*type = ep->type;
100ff857a3aSPascal Brand 	switch (*type) {
101b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
102d5d50c3cSJens Wiklander 		l = sizeof(bool);
103b0104773SPascal Brand 		break;
104b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
105b0104773SPascal Brand 		l = sizeof(uint32_t);
106b0104773SPascal Brand 		break;
107b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
108b0104773SPascal Brand 		l = sizeof(TEE_UUID);
109b0104773SPascal Brand 		break;
110b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
111b0104773SPascal Brand 		l = sizeof(TEE_Identity);
112b0104773SPascal Brand 		break;
113b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
114ff857a3aSPascal Brand 		/* take the leading 0 into account */
115ff857a3aSPascal Brand 		l = strlen(ep->value) + 1;
116ff857a3aSPascal Brand 		break;
117b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
118ff857a3aSPascal Brand 		/*
119ff857a3aSPascal Brand 		 * in case of TA property, a binary block is provided as a
120ff857a3aSPascal Brand 		 * string, which is base64 encoded. We must first decode it,
121ff857a3aSPascal Brand 		 * without taking into account the zero termination of the
122ff857a3aSPascal Brand 		 * string
123ff857a3aSPascal Brand 		 */
124ff857a3aSPascal Brand 		l = *len;
125ff857a3aSPascal Brand 		if (!base64_dec(ep->value, strlen(ep->value), buf, &l) &&
126ff857a3aSPascal Brand 		    (l <= *len))
127ff857a3aSPascal Brand 			return TEE_ERROR_GENERIC;
128ff857a3aSPascal Brand 		if (*len < l) {
129ff857a3aSPascal Brand 			*len = l;
130ff857a3aSPascal Brand 			return TEE_ERROR_SHORT_BUFFER;
131ff857a3aSPascal Brand 		}
132ff857a3aSPascal Brand 
133ff857a3aSPascal Brand 		*len = l;
134b0104773SPascal Brand 		return TEE_SUCCESS;
135b0104773SPascal Brand 	default:
136b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
137b0104773SPascal Brand 	}
138ff857a3aSPascal Brand 
139ff857a3aSPascal Brand 	if (*len < l) {
140ff857a3aSPascal Brand 		*len = l;
141ff857a3aSPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
142ff857a3aSPascal Brand 	}
143ff857a3aSPascal Brand 
144ff857a3aSPascal Brand 	*len = l;
145ff857a3aSPascal Brand 	memcpy(buf, ep->value, l);
146b0104773SPascal Brand 	return TEE_SUCCESS;
147b0104773SPascal Brand }
148b0104773SPascal Brand 
1498f07fe6fSJerome Forissier static TEE_Result propget_get_property(TEE_PropSetHandle h, const char *name,
150ff857a3aSPascal Brand 				       enum user_ta_prop_type *type,
151ff857a3aSPascal Brand 				       void *buf, uint32_t *len)
152b0104773SPascal Brand {
153b0104773SPascal Brand 	TEE_Result res;
154b0104773SPascal Brand 	const struct user_ta_property *eps;
155b0104773SPascal Brand 	size_t eps_len;
15664a5011eSPascal Brand 	uint32_t prop_type;
15764a5011eSPascal Brand 	uint32_t index;
158b0104773SPascal Brand 
159b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA || h == TEE_PROPSET_CURRENT_CLIENT ||
160b0104773SPascal Brand 	    h == TEE_PROPSET_TEE_IMPLEMENTATION) {
161b0104773SPascal Brand 		size_t n;
162b0104773SPascal Brand 
16364a5011eSPascal Brand 		res = propset_get(h, &eps, &eps_len);
164b0104773SPascal Brand 		if (res != TEE_SUCCESS)
165b0104773SPascal Brand 			return res;
166b0104773SPascal Brand 
167b0104773SPascal Brand 		for (n = 0; n < eps_len; n++) {
16864a5011eSPascal Brand 			if (!strcmp(name, eps[n].name))
169ff857a3aSPascal Brand 				return propget_get_ext_prop(eps + n, type,
170ff857a3aSPascal Brand 							    buf, len);
171b0104773SPascal Brand 		}
17264a5011eSPascal Brand 
17364a5011eSPascal Brand 		/* get the index from the name */
17464a5011eSPascal Brand 		res = utee_get_property_name_to_index((unsigned long)h, name,
17564a5011eSPascal Brand 						strlen(name) + 1, &index);
17664a5011eSPascal Brand 		if (res != TEE_SUCCESS)
17764a5011eSPascal Brand 			return res;
178ff857a3aSPascal Brand 		res = utee_get_property((unsigned long)h, index, NULL, NULL,
179ff857a3aSPascal Brand 					buf, len, &prop_type);
180b0104773SPascal Brand 	} else {
181b0104773SPascal Brand 		struct prop_enumerator *pe = (struct prop_enumerator *)h;
182b0104773SPascal Brand 		uint32_t idx = pe->idx;
183b0104773SPascal Brand 
184b0104773SPascal Brand 		if (idx == PROP_ENUMERATOR_NOT_STARTED)
185b0104773SPascal Brand 			return TEE_ERROR_ITEM_NOT_FOUND;
186b0104773SPascal Brand 
18764a5011eSPascal Brand 		res = propset_get(pe->prop_set, &eps, &eps_len);
188b0104773SPascal Brand 		if (res != TEE_SUCCESS)
189b0104773SPascal Brand 			return res;
190b0104773SPascal Brand 
191b0104773SPascal Brand 		if (idx < eps_len)
192ff857a3aSPascal Brand 			return propget_get_ext_prop(eps + idx, type, buf, len);
19364a5011eSPascal Brand 		idx -= eps_len;
194b0104773SPascal Brand 
19564a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set, idx,
196ff857a3aSPascal Brand 					NULL, NULL, buf, len, &prop_type);
19764a5011eSPascal Brand 		if (res == TEE_ERROR_ITEM_NOT_FOUND)
19864a5011eSPascal Brand 			res = TEE_ERROR_BAD_PARAMETERS;
199b0104773SPascal Brand 	}
20064a5011eSPascal Brand 
201ff857a3aSPascal Brand 	*type = prop_type;
20264a5011eSPascal Brand 	return res;
203b0104773SPascal Brand }
204b0104773SPascal Brand 
205b0104773SPascal Brand TEE_Result TEE_GetPropertyAsString(TEE_PropSetHandle propsetOrEnumerator,
2068f07fe6fSJerome Forissier 				   const char *name, char *value,
2070dd3f3a4SPascal Brand 				   uint32_t *value_len)
208b0104773SPascal Brand {
209b0104773SPascal Brand 	TEE_Result res;
210b0104773SPascal Brand 	size_t l;
211ff857a3aSPascal Brand 	enum user_ta_prop_type type;
212ff857a3aSPascal Brand 	void *tmp_buf = 0;
213ff857a3aSPascal Brand 	uint32_t tmp_len;
214ff857a3aSPascal Brand 	uint32_t uint32_val;
215d5d50c3cSJens Wiklander 	bool bool_val;
216ff857a3aSPascal Brand 	TEE_Identity *p_identity_val;
217b0104773SPascal Brand 
2180dd3f3a4SPascal Brand 	if (!value || !value_len) {
219ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
220ff857a3aSPascal Brand 		goto out;
221ba675d69SCedric Chaumont 	}
222b0104773SPascal Brand 
2230dd3f3a4SPascal Brand 	tmp_len = *value_len;
224ff857a3aSPascal Brand 	if (tmp_len < sizeof(TEE_Identity))
225ff857a3aSPascal Brand 		tmp_len = sizeof(TEE_Identity);
226ff857a3aSPascal Brand 	tmp_buf = TEE_Malloc(tmp_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);
227ff857a3aSPascal Brand 	if (!tmp_buf) {
228ff857a3aSPascal Brand 		res = TEE_ERROR_OUT_OF_MEMORY;
229ff857a3aSPascal Brand 		goto out;
230ff857a3aSPascal Brand 	}
231b0104773SPascal Brand 
232ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
233ff857a3aSPascal Brand 				   tmp_buf, &tmp_len);
234ff857a3aSPascal Brand 	if (res != TEE_SUCCESS) {
235254e1d58SPascal Brand 		if (res == TEE_ERROR_SHORT_BUFFER) {
236254e1d58SPascal Brand 			if (type == USER_TA_PROP_TYPE_BINARY_BLOCK) {
237254e1d58SPascal Brand 				/*
238254e1d58SPascal Brand 				 * in this case, we must enlarge the buffer
239254e1d58SPascal Brand 				 * with the size of the of the base64 encoded
240254e1d58SPascal Brand 				 * see base64_enc() function
241254e1d58SPascal Brand 				 */
242254e1d58SPascal Brand 				tmp_len = base64_enc_len(tmp_len);
243254e1d58SPascal Brand 			}
2440dd3f3a4SPascal Brand 			*value_len = tmp_len;
245254e1d58SPascal Brand 		}
246ff857a3aSPascal Brand 		goto out;
247ff857a3aSPascal Brand 	}
248b0104773SPascal Brand 
249ff857a3aSPascal Brand 	switch (type) {
250b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
251d5d50c3cSJens Wiklander 		bool_val = *((bool *)tmp_buf);
252d5d50c3cSJens Wiklander 		l = strlcpy(value, (bool_val ? "true" : "false"), *value_len);
253b0104773SPascal Brand 		break;
254b0104773SPascal Brand 
255b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
256ff857a3aSPascal Brand 		uint32_val = *((uint32_t *)tmp_buf);
2570dd3f3a4SPascal Brand 		l = snprintf(value, *value_len, "%u", uint32_val);
258b0104773SPascal Brand 		break;
259b0104773SPascal Brand 
260b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
2610dd3f3a4SPascal Brand 		l = snprintk(value, *value_len, "%pUl", tmp_buf);
262b0104773SPascal Brand 		break;
263b0104773SPascal Brand 
264b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
265ff857a3aSPascal Brand 		p_identity_val = ((TEE_Identity *)tmp_buf);
2660dd3f3a4SPascal Brand 		l = snprintk(value, *value_len, "%u:%pUl",
267ff857a3aSPascal Brand 			     p_identity_val->login,
268ff857a3aSPascal Brand 			     (void *)(&(p_identity_val->uuid)));
269b0104773SPascal Brand 		break;
270b0104773SPascal Brand 
271b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
2720dd3f3a4SPascal Brand 		l = strlcpy(value, tmp_buf, *value_len);
273ff857a3aSPascal Brand 		break;
274ff857a3aSPascal Brand 
275b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
2760dd3f3a4SPascal Brand 		l = *value_len;	/* l includes the zero-termination */
2770dd3f3a4SPascal Brand 		if (!base64_enc(tmp_buf, tmp_len, value, &l) &&
2780dd3f3a4SPascal Brand 		    (l <= *value_len)) {
279ff857a3aSPascal Brand 			res = TEE_ERROR_GENERIC;
280ff857a3aSPascal Brand 			goto out;
281ff857a3aSPascal Brand 		}
282ff857a3aSPascal Brand 		l--;	/* remove the zero-termination that is added later */
283b0104773SPascal Brand 		break;
284b0104773SPascal Brand 
285b0104773SPascal Brand 	default:
286ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_FORMAT;
287ba675d69SCedric Chaumont 		goto out;
288ff857a3aSPascal Brand 	}
289ba675d69SCedric Chaumont 
290ff857a3aSPascal Brand 	l++;	/* include zero termination */
291ff857a3aSPascal Brand 
2920dd3f3a4SPascal Brand 	if (l > *value_len)
293ff857a3aSPascal Brand 		res = TEE_ERROR_SHORT_BUFFER;
2940dd3f3a4SPascal Brand 	*value_len = l;
295ff857a3aSPascal Brand 
296ba675d69SCedric Chaumont out:
297ff857a3aSPascal Brand 	if (tmp_buf)
298ff857a3aSPascal Brand 		TEE_Free(tmp_buf);
299ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
300ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
301ff857a3aSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
302ff857a3aSPascal Brand 		TEE_Panic(0);
303ff857a3aSPascal Brand 
304ff857a3aSPascal Brand 	return res;
305b0104773SPascal Brand }
306b0104773SPascal Brand 
307b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBool(TEE_PropSetHandle propsetOrEnumerator,
3088f07fe6fSJerome Forissier 				 const char *name, bool *value)
309b0104773SPascal Brand {
310b0104773SPascal Brand 	TEE_Result res;
311ff857a3aSPascal Brand 	enum user_ta_prop_type type;
312d5d50c3cSJens Wiklander 	uint32_t bool_len = sizeof(bool);
313ba675d69SCedric Chaumont 	if (value == NULL) {
314ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
315ff857a3aSPascal Brand 		goto out;
316ba675d69SCedric Chaumont 	}
317b0104773SPascal Brand 
318ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_BOOL;
319ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
320d5d50c3cSJens Wiklander 				   value, &bool_len);
321ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_BOOL)
322ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_FORMAT;
323ff857a3aSPascal Brand 	if (res != TEE_SUCCESS)
324ba675d69SCedric Chaumont 		goto out;
325ba675d69SCedric Chaumont 
326ba675d69SCedric Chaumont out:
327ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
328ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
329ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
330ff857a3aSPascal Brand 		TEE_Panic(0);
331ff857a3aSPascal Brand 
332ff857a3aSPascal Brand 	return res;
333b0104773SPascal Brand }
334b0104773SPascal Brand 
335b0104773SPascal Brand TEE_Result TEE_GetPropertyAsU32(TEE_PropSetHandle propsetOrEnumerator,
3368f07fe6fSJerome Forissier 				const char *name, uint32_t *value)
337b0104773SPascal Brand {
338b0104773SPascal Brand 	TEE_Result res;
339ff857a3aSPascal Brand 	enum user_ta_prop_type type;
340ff857a3aSPascal Brand 	uint32_t uint32_len = sizeof(uint32_t);
341b0104773SPascal Brand 
3420dd3f3a4SPascal Brand 	if (!value) {
343ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
344ba675d69SCedric Chaumont 		goto out;
345ff857a3aSPascal Brand 	}
346ba675d69SCedric Chaumont 
347ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_U32;
348ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
349ff857a3aSPascal Brand 				   value, &uint32_len);
350ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_U32)
351ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
352ff857a3aSPascal Brand 
353ba675d69SCedric Chaumont out:
354ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
355ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
356ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
357ff857a3aSPascal Brand 		TEE_Panic(0);
358ff857a3aSPascal Brand 
359ff857a3aSPascal Brand 	return res;
360b0104773SPascal Brand }
361b0104773SPascal Brand 
362b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBinaryBlock(TEE_PropSetHandle propsetOrEnumerator,
3638f07fe6fSJerome Forissier 					const char *name, void *value,
3640dd3f3a4SPascal Brand 					uint32_t *value_len)
365b0104773SPascal Brand {
366b0104773SPascal Brand 	TEE_Result res;
367ff857a3aSPascal Brand 	enum user_ta_prop_type type;
368b0104773SPascal Brand 
3690dd3f3a4SPascal Brand 	if (!value || !value_len) {
370ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
371ba675d69SCedric Chaumont 		goto out;
372ff857a3aSPascal Brand 	}
373ba675d69SCedric Chaumont 
374ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_BINARY_BLOCK;
375ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
3760dd3f3a4SPascal Brand 				   value, value_len);
377ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_BINARY_BLOCK)
378ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
379ff857a3aSPascal Brand 
380ba675d69SCedric Chaumont out:
381ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
382ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
383ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT &&
384ff857a3aSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
385ff857a3aSPascal Brand 		TEE_Panic(0);
386ff857a3aSPascal Brand 
387ff857a3aSPascal Brand 	return res;
388b0104773SPascal Brand }
389b0104773SPascal Brand 
390b0104773SPascal Brand TEE_Result TEE_GetPropertyAsUUID(TEE_PropSetHandle propsetOrEnumerator,
3918f07fe6fSJerome Forissier 				 const char *name, TEE_UUID *value)
392b0104773SPascal Brand {
393b0104773SPascal Brand 	TEE_Result res;
394ff857a3aSPascal Brand 	enum user_ta_prop_type type;
395ff857a3aSPascal Brand 	uint32_t uuid_len = sizeof(TEE_UUID);
396b0104773SPascal Brand 
3970dd3f3a4SPascal Brand 	if (!value) {
398ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
399ba675d69SCedric Chaumont 		goto out;
400ff857a3aSPascal Brand 	}
401ba675d69SCedric Chaumont 
402ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_UUID;
403ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
404ff857a3aSPascal Brand 				   value, &uuid_len);
405ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_UUID)
406ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
407ff857a3aSPascal Brand 
408ba675d69SCedric Chaumont out:
409ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
410ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
411ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
412ff857a3aSPascal Brand 		TEE_Panic(0);
413ff857a3aSPascal Brand 
414ff857a3aSPascal Brand 	return res;
415b0104773SPascal Brand }
416b0104773SPascal Brand 
417b0104773SPascal Brand TEE_Result TEE_GetPropertyAsIdentity(TEE_PropSetHandle propsetOrEnumerator,
4188f07fe6fSJerome Forissier 				     const char *name, TEE_Identity *value)
419b0104773SPascal Brand {
420b0104773SPascal Brand 	TEE_Result res;
421ff857a3aSPascal Brand 	enum user_ta_prop_type type;
422ff857a3aSPascal Brand 	uint32_t identity_len = sizeof(TEE_Identity);
423b0104773SPascal Brand 
4240dd3f3a4SPascal Brand 	if (!value) {
425ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
426ba675d69SCedric Chaumont 		goto out;
427ff857a3aSPascal Brand 	}
428ba675d69SCedric Chaumont 
429ff857a3aSPascal Brand 	type = USER_TA_PROP_TYPE_IDENTITY;
430ff857a3aSPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &type,
431ff857a3aSPascal Brand 				   value, &identity_len);
432ff857a3aSPascal Brand 	if (type != USER_TA_PROP_TYPE_IDENTITY)
433ff857a3aSPascal Brand 		res = TEE_ERROR_BAD_FORMAT;
434ff857a3aSPascal Brand 
435ba675d69SCedric Chaumont out:
436ff857a3aSPascal Brand 	if (res != TEE_SUCCESS &&
437ff857a3aSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
438ff857a3aSPascal Brand 	    res != TEE_ERROR_BAD_FORMAT)
439ff857a3aSPascal Brand 		TEE_Panic(0);
440ff857a3aSPascal Brand 
441ff857a3aSPascal Brand 	return res;
442b0104773SPascal Brand }
443b0104773SPascal Brand 
444b0104773SPascal Brand TEE_Result TEE_AllocatePropertyEnumerator(TEE_PropSetHandle *enumerator)
445b0104773SPascal Brand {
446ba675d69SCedric Chaumont 	TEE_Result res;
447b0104773SPascal Brand 	struct prop_enumerator *pe;
448b0104773SPascal Brand 
4490dd3f3a4SPascal Brand 	if (!enumerator) {
450ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
451ba675d69SCedric Chaumont 		goto err;
452ba675d69SCedric Chaumont 	}
453b0104773SPascal Brand 
454b0104773SPascal Brand 	pe = TEE_Malloc(sizeof(struct prop_enumerator),
455b0104773SPascal Brand 			TEE_USER_MEM_HINT_NO_FILL_ZERO);
456ba675d69SCedric Chaumont 	if (pe == NULL) {
457ba675d69SCedric Chaumont 		res = TEE_ERROR_OUT_OF_MEMORY;
458ba675d69SCedric Chaumont 		goto err;
459ba675d69SCedric Chaumont 	}
460b0104773SPascal Brand 
461b0104773SPascal Brand 	*enumerator = (TEE_PropSetHandle) pe;
462b0104773SPascal Brand 	TEE_ResetPropertyEnumerator(*enumerator);
463ba675d69SCedric Chaumont 
464ba675d69SCedric Chaumont 	goto out;
465ba675d69SCedric Chaumont 
466ba675d69SCedric Chaumont err:
467ba675d69SCedric Chaumont 	if (res == TEE_ERROR_OUT_OF_MEMORY)
468ba675d69SCedric Chaumont 		return res;
469ba675d69SCedric Chaumont 	TEE_Panic(0);
470ba675d69SCedric Chaumont out:
471b0104773SPascal Brand 	return TEE_SUCCESS;
472b0104773SPascal Brand }
473b0104773SPascal Brand 
474b0104773SPascal Brand void TEE_ResetPropertyEnumerator(TEE_PropSetHandle enumerator)
475b0104773SPascal Brand {
476b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
477b0104773SPascal Brand 
478b0104773SPascal Brand 	pe->idx = PROP_ENUMERATOR_NOT_STARTED;
479b0104773SPascal Brand }
480b0104773SPascal Brand 
481b0104773SPascal Brand void TEE_FreePropertyEnumerator(TEE_PropSetHandle enumerator)
482b0104773SPascal Brand {
483b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
484b0104773SPascal Brand 
485b0104773SPascal Brand 	TEE_Free(pe);
486b0104773SPascal Brand }
487b0104773SPascal Brand 
488b0104773SPascal Brand void TEE_StartPropertyEnumerator(TEE_PropSetHandle enumerator,
489b0104773SPascal Brand 				 TEE_PropSetHandle propSet)
490b0104773SPascal Brand {
491b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
492b0104773SPascal Brand 
4930dd3f3a4SPascal Brand 	if (!pe)
494b0104773SPascal Brand 		return;
495b0104773SPascal Brand 
496b0104773SPascal Brand 	pe->idx = 0;
497b0104773SPascal Brand 	pe->prop_set = propSet;
498b0104773SPascal Brand }
499b0104773SPascal Brand 
500b0104773SPascal Brand TEE_Result TEE_GetPropertyName(TEE_PropSetHandle enumerator,
5010dd3f3a4SPascal Brand 			       void *name, uint32_t *name_len)
502b0104773SPascal Brand {
503b0104773SPascal Brand 	TEE_Result res;
504b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
505b0104773SPascal Brand 	const struct user_ta_property *eps;
506b0104773SPascal Brand 	size_t eps_len;
507b0104773SPascal Brand 	const char *str;
508b0104773SPascal Brand 	size_t bufferlen;
509b0104773SPascal Brand 
5100dd3f3a4SPascal Brand 	if (!pe || !name || !name_len) {
511ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
512ba675d69SCedric Chaumont 		goto err;
513ba675d69SCedric Chaumont 	}
514b0104773SPascal Brand 
5150dd3f3a4SPascal Brand 	bufferlen = *name_len;
51664a5011eSPascal Brand 	res = propset_get(pe->prop_set, &eps, &eps_len);
517b0104773SPascal Brand 	if (res != TEE_SUCCESS)
518ba675d69SCedric Chaumont 		goto err;
519b0104773SPascal Brand 
52064a5011eSPascal Brand 	if (pe->idx < eps_len) {
52164a5011eSPascal Brand 		str = eps[pe->idx].name;
5220dd3f3a4SPascal Brand 		bufferlen = strlcpy(name, str, *name_len) + 1;
5230dd3f3a4SPascal Brand 		if (bufferlen > *name_len)
524ba675d69SCedric Chaumont 			res = TEE_ERROR_SHORT_BUFFER;
5250dd3f3a4SPascal Brand 		*name_len = bufferlen;
52664a5011eSPascal Brand 	} else {
52764a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set,
52864a5011eSPascal Brand 					pe->idx - eps_len,
5290dd3f3a4SPascal Brand 					name, name_len, NULL, NULL, NULL);
53064a5011eSPascal Brand 		if (res != TEE_SUCCESS)
531ba675d69SCedric Chaumont 			goto err;
532ba675d69SCedric Chaumont 	}
533b0104773SPascal Brand 
534ba675d69SCedric Chaumont err:
53564a5011eSPascal Brand 	if (res != TEE_SUCCESS &&
53664a5011eSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND &&
53764a5011eSPascal Brand 	    res != TEE_ERROR_SHORT_BUFFER)
538ba675d69SCedric Chaumont 		TEE_Panic(0);
53964a5011eSPascal Brand 	return res;
540b0104773SPascal Brand }
541b0104773SPascal Brand 
542b0104773SPascal Brand TEE_Result TEE_GetNextProperty(TEE_PropSetHandle enumerator)
543b0104773SPascal Brand {
544b0104773SPascal Brand 	TEE_Result res;
545b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
546b0104773SPascal Brand 	uint32_t next_idx;
547b0104773SPascal Brand 	const struct user_ta_property *eps;
548b0104773SPascal Brand 	size_t eps_len;
549b0104773SPascal Brand 
5500dd3f3a4SPascal Brand 	if (!pe) {
551ba675d69SCedric Chaumont 		res = TEE_ERROR_BAD_PARAMETERS;
55264a5011eSPascal Brand 		goto out;
553ba675d69SCedric Chaumont 	}
554b0104773SPascal Brand 
555ba675d69SCedric Chaumont 	if (pe->idx == PROP_ENUMERATOR_NOT_STARTED) {
556ba675d69SCedric Chaumont 		res = TEE_ERROR_ITEM_NOT_FOUND;
55764a5011eSPascal Brand 		goto out;
558ba675d69SCedric Chaumont 	}
559b0104773SPascal Brand 
56064a5011eSPascal Brand 	res = propset_get(pe->prop_set, &eps, &eps_len);
561b0104773SPascal Brand 	if (res != TEE_SUCCESS)
56264a5011eSPascal Brand 		goto out;
563b0104773SPascal Brand 
564b0104773SPascal Brand 	next_idx = pe->idx + 1;
565b0104773SPascal Brand 	pe->idx = next_idx;
56664a5011eSPascal Brand 	if (next_idx < eps_len)
56764a5011eSPascal Brand 		res = TEE_SUCCESS;
56864a5011eSPascal Brand 	else
56964a5011eSPascal Brand 		res = utee_get_property((unsigned long)pe->prop_set,
570ff857a3aSPascal Brand 					next_idx - eps_len,
571ff857a3aSPascal Brand 					NULL, NULL, NULL, NULL, NULL);
572b0104773SPascal Brand 
573ba675d69SCedric Chaumont out:
57464a5011eSPascal Brand 	if (res != TEE_SUCCESS &&
57564a5011eSPascal Brand 	    res != TEE_ERROR_ITEM_NOT_FOUND)
57664a5011eSPascal Brand 		TEE_Panic(0);
57764a5011eSPascal Brand 	return res;
578b0104773SPascal Brand }
579