xref: /optee_os/lib/libutee/tee_api_property.c (revision 79a3c601fa657bb596483d8c215d659a6e9d0021)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
27b0104773SPascal Brand #include <tee_api.h>
28b0104773SPascal Brand 
29b0104773SPascal Brand #include <stdlib.h>
30b0104773SPascal Brand #include <string.h>
31b0104773SPascal Brand #include <stdio.h>
32b0104773SPascal Brand #include <tee_api_defines.h>
33b0104773SPascal Brand #include <tee_api_types.h>
34b0104773SPascal Brand #include <user_ta_header.h>
35b0104773SPascal Brand #include <tee_internal_api_extensions.h>
36647f9c76SJerome Forissier #include <tee_arith_internal.h>
37b0104773SPascal Brand 
38b0104773SPascal Brand #include <utee_syscalls.h>
39b0104773SPascal Brand 
40b0104773SPascal Brand #include "string_ext.h"
41b0104773SPascal Brand #include "base64.h"
42b0104773SPascal Brand 
43b0104773SPascal Brand #define PROP_STR_MAX    80
44b0104773SPascal Brand 
45b0104773SPascal Brand #define PROP_ENUMERATOR_NOT_STARTED 0xffffffff
46b0104773SPascal Brand 
47b0104773SPascal Brand struct prop_enumerator {
48b0104773SPascal Brand 	uint32_t idx;
49b0104773SPascal Brand 	TEE_PropSetHandle prop_set;
50b0104773SPascal Brand };
51b0104773SPascal Brand 
52b0104773SPascal Brand struct prop_value {
53b0104773SPascal Brand 	enum user_ta_prop_type type;
54b0104773SPascal Brand 	union {
55b0104773SPascal Brand 		bool bool_val;
56b0104773SPascal Brand 		uint32_t int_val;
57b0104773SPascal Brand 		TEE_UUID uuid_val;
58b0104773SPascal Brand 		TEE_Identity identity_val;
59b0104773SPascal Brand 		char str_val[PROP_STR_MAX];
60b0104773SPascal Brand 	} u;
61b0104773SPascal Brand };
62b0104773SPascal Brand 
63b0104773SPascal Brand typedef TEE_Result(*ta_propget_func_t) (struct prop_value *pv);
64b0104773SPascal Brand 
65b0104773SPascal Brand struct prop_set {
66b0104773SPascal Brand 	const char *str;
67b0104773SPascal Brand 	ta_propget_func_t get;
68b0104773SPascal Brand };
69b0104773SPascal Brand 
70b0104773SPascal Brand static TEE_Result propget_gpd_ta_app_id(struct prop_value *pv)
71b0104773SPascal Brand {
72b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_UUID;
73b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TA_APP_ID, &pv->u.uuid_val,
74b0104773SPascal Brand 				 sizeof(pv->u.uuid_val));
75b0104773SPascal Brand }
76b0104773SPascal Brand 
77b0104773SPascal Brand static TEE_Result propget_gpd_client_identity(struct prop_value *pv)
78b0104773SPascal Brand {
79b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_IDENTITY;
80b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_CLIENT_ID, &pv->u.identity_val,
81b0104773SPascal Brand 				 sizeof(pv->u.identity_val));
82b0104773SPascal Brand }
83b0104773SPascal Brand 
84b0104773SPascal Brand static TEE_Result propget_gpd_tee_api_version(struct prop_value *pv)
85b0104773SPascal Brand {
86b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_STRING;
87b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TEE_API_VERSION, &pv->u.str_val,
88b0104773SPascal Brand 				 sizeof(pv->u.str_val));
89b0104773SPascal Brand }
90b0104773SPascal Brand 
91b0104773SPascal Brand static TEE_Result propget_gpd_tee_description(struct prop_value *pv)
92b0104773SPascal Brand {
93b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_STRING;
94b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TEE_DESCR, &pv->u.str_val,
95b0104773SPascal Brand 				 sizeof(pv->u.str_val));
96b0104773SPascal Brand }
97b0104773SPascal Brand 
98b0104773SPascal Brand static TEE_Result propget_gpd_tee_device_id(struct prop_value *pv)
99b0104773SPascal Brand {
100b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_UUID;
101b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TEE_DEV_ID, &pv->u.uuid_val,
102b0104773SPascal Brand 				 sizeof(pv->u.uuid_val));
103b0104773SPascal Brand }
104b0104773SPascal Brand 
105b0104773SPascal Brand static TEE_Result propget_gpd_tee_sys_time_protection_level(struct prop_value
106b0104773SPascal Brand 							    *pv)
107b0104773SPascal Brand {
108b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_U32;
109b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL,
110b0104773SPascal Brand 				 &pv->u.int_val, sizeof(pv->u.int_val));
111b0104773SPascal Brand }
112b0104773SPascal Brand 
113b0104773SPascal Brand static TEE_Result propget_gpd_tee_ta_time_protection_level(struct prop_value
114b0104773SPascal Brand 							   *pv)
115b0104773SPascal Brand {
116b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_U32;
117b0104773SPascal Brand 	return utee_get_property(UTEE_PROP_TEE_TA_TIME_PROT_LEVEL,
118b0104773SPascal Brand 				 &pv->u.int_val, sizeof(pv->u.int_val));
119b0104773SPascal Brand }
120b0104773SPascal Brand 
121b0104773SPascal Brand static TEE_Result propget_gpd_tee_arith_max_big_int_size(struct prop_value *pv)
122b0104773SPascal Brand {
123b0104773SPascal Brand 	pv->type = USER_TA_PROP_TYPE_U32;
124647f9c76SJerome Forissier 	pv->u.int_val = TEE_MAX_NUMBER_OF_SUPPORTED_BITS;
125647f9c76SJerome Forissier 	return TEE_SUCCESS;
126b0104773SPascal Brand }
127b0104773SPascal Brand 
128b0104773SPascal Brand static const struct prop_set propset_current_ta[] = {
129b0104773SPascal Brand 	{"gpd.ta.appID", propget_gpd_ta_app_id},
130b0104773SPascal Brand };
131b0104773SPascal Brand 
132b0104773SPascal Brand static const size_t propset_current_ta_len =
133b0104773SPascal Brand 	sizeof(propset_current_ta) / sizeof(propset_current_ta[0]);
134b0104773SPascal Brand 
135b0104773SPascal Brand static const struct prop_set propset_current_client[] = {
136b0104773SPascal Brand 	{"gpd.client.identity", propget_gpd_client_identity},
137b0104773SPascal Brand };
138b0104773SPascal Brand 
139b0104773SPascal Brand static const size_t propset_current_client_len =
140b0104773SPascal Brand 	sizeof(propset_current_client) / sizeof(propset_current_client[0]);
141b0104773SPascal Brand 
142b0104773SPascal Brand static const struct prop_set propset_implementation[] = {
143b0104773SPascal Brand 	{"gpd.tee.apiversion", propget_gpd_tee_api_version},
144b0104773SPascal Brand 	{"gpd.tee.description", propget_gpd_tee_description},
145b0104773SPascal Brand 	{"gpd.tee.deviceID", propget_gpd_tee_device_id},
146b0104773SPascal Brand 	{"gpd.tee.systemTime.protectionLevel",
147b0104773SPascal Brand 	 propget_gpd_tee_sys_time_protection_level},
148b0104773SPascal Brand 	{"gpd.tee.TAPersistentTime.protectionLevel",
149b0104773SPascal Brand 	 propget_gpd_tee_ta_time_protection_level},
150b0104773SPascal Brand 	{"gpd.tee.arith.maxBigIntSize", propget_gpd_tee_arith_max_big_int_size},
151b0104773SPascal Brand };
152b0104773SPascal Brand 
153b0104773SPascal Brand static const size_t propset_implementation_len =
154b0104773SPascal Brand 	sizeof(propset_implementation) / sizeof(propset_implementation[0]);
155b0104773SPascal Brand 
156b0104773SPascal Brand static TEE_Result propset_get(TEE_PropSetHandle h, const struct prop_set **ps,
157b0104773SPascal Brand 			      size_t *ps_len,
158b0104773SPascal Brand 			      const struct user_ta_property **eps,
159b0104773SPascal Brand 			      size_t *eps_len)
160b0104773SPascal Brand {
161b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA) {
162b0104773SPascal Brand 		*ps = propset_current_ta;
163b0104773SPascal Brand 		*ps_len = propset_current_ta_len;
164b0104773SPascal Brand 		*eps = ta_props;
165b0104773SPascal Brand 		*eps_len = ta_num_props;
166b0104773SPascal Brand 	} else if (h == TEE_PROPSET_CURRENT_CLIENT) {
167b0104773SPascal Brand 		*ps = propset_current_client;
168b0104773SPascal Brand 		*ps_len = propset_current_client_len;
169b0104773SPascal Brand 		*eps = NULL;
170b0104773SPascal Brand 		*eps_len = 0;
171b0104773SPascal Brand 	} else if (h == TEE_PROPSET_TEE_IMPLEMENTATION) {
172b0104773SPascal Brand 		*ps = propset_implementation;
173b0104773SPascal Brand 		*ps_len = propset_implementation_len;
174b0104773SPascal Brand 		*eps = NULL;
175b0104773SPascal Brand 		*eps_len = 0;
176b0104773SPascal Brand 	} else {
177b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
178b0104773SPascal Brand 	}
179b0104773SPascal Brand 
180b0104773SPascal Brand 	return TEE_SUCCESS;
181b0104773SPascal Brand }
182b0104773SPascal Brand 
183b0104773SPascal Brand static TEE_Result propget_get_ext_prop(const struct user_ta_property *ep,
184b0104773SPascal Brand 				       struct prop_value *pv)
185b0104773SPascal Brand {
186b0104773SPascal Brand 	size_t l;
187b0104773SPascal Brand 
188b0104773SPascal Brand 	pv->type = ep->type;
189b0104773SPascal Brand 	switch (ep->type) {
190b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
191b0104773SPascal Brand 		l = sizeof(bool);
192b0104773SPascal Brand 		break;
193b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
194b0104773SPascal Brand 		l = sizeof(uint32_t);
195b0104773SPascal Brand 		break;
196b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
197b0104773SPascal Brand 		l = sizeof(TEE_UUID);
198b0104773SPascal Brand 		break;
199b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
200b0104773SPascal Brand 		l = sizeof(TEE_Identity);
201b0104773SPascal Brand 		break;
202b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
203b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
204b0104773SPascal Brand 		/* Handle too large strings by truncating them */
205b0104773SPascal Brand 		strlcpy(pv->u.str_val, ep->value, sizeof(pv->u.str_val));
206b0104773SPascal Brand 		return TEE_SUCCESS;
207b0104773SPascal Brand 	default:
208b0104773SPascal Brand 		return TEE_ERROR_GENERIC;
209b0104773SPascal Brand 	}
210b0104773SPascal Brand 	memcpy(&pv->u, ep->value, l);
211b0104773SPascal Brand 	return TEE_SUCCESS;
212b0104773SPascal Brand }
213b0104773SPascal Brand 
214b0104773SPascal Brand static TEE_Result propget_get_property(TEE_PropSetHandle h, char *name,
215b0104773SPascal Brand 				       struct prop_value *pv)
216b0104773SPascal Brand {
217b0104773SPascal Brand 	TEE_Result res;
218b0104773SPascal Brand 	const struct prop_set *ps;
219b0104773SPascal Brand 	size_t ps_len;
220b0104773SPascal Brand 	const struct user_ta_property *eps;
221b0104773SPascal Brand 	size_t eps_len;
222b0104773SPascal Brand 
223b0104773SPascal Brand 	if (h == TEE_PROPSET_CURRENT_TA || h == TEE_PROPSET_CURRENT_CLIENT ||
224b0104773SPascal Brand 	    h == TEE_PROPSET_TEE_IMPLEMENTATION) {
225b0104773SPascal Brand 		size_t n;
226b0104773SPascal Brand 
227b0104773SPascal Brand 		res = propset_get(h, &ps, &ps_len, &eps, &eps_len);
228b0104773SPascal Brand 		if (res != TEE_SUCCESS)
229b0104773SPascal Brand 			return res;
230b0104773SPascal Brand 
231b0104773SPascal Brand 		for (n = 0; n < ps_len; n++) {
232b0104773SPascal Brand 			if (strcmp(name, ps[n].str) == 0)
233b0104773SPascal Brand 				return ps[n].get(pv);
234b0104773SPascal Brand 		}
235b0104773SPascal Brand 		for (n = 0; n < eps_len; n++) {
236b0104773SPascal Brand 			if (strcmp(name, eps[n].name) == 0)
237b0104773SPascal Brand 				return propget_get_ext_prop(eps + n, pv);
238b0104773SPascal Brand 		}
239b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
240b0104773SPascal Brand 	} else {
241b0104773SPascal Brand 		struct prop_enumerator *pe = (struct prop_enumerator *)h;
242b0104773SPascal Brand 		uint32_t idx = pe->idx;
243b0104773SPascal Brand 
244b0104773SPascal Brand 		if (idx == PROP_ENUMERATOR_NOT_STARTED)
245b0104773SPascal Brand 			return TEE_ERROR_ITEM_NOT_FOUND;
246b0104773SPascal Brand 
247b0104773SPascal Brand 		res = propset_get(pe->prop_set, &ps, &ps_len, &eps, &eps_len);
248b0104773SPascal Brand 		if (res != TEE_SUCCESS)
249b0104773SPascal Brand 			return res;
250b0104773SPascal Brand 
251b0104773SPascal Brand 		if (idx < ps_len)
252b0104773SPascal Brand 			return ps[idx].get(pv);
253b0104773SPascal Brand 
254b0104773SPascal Brand 		idx -= ps_len;
255b0104773SPascal Brand 		if (idx < eps_len)
256b0104773SPascal Brand 			return propget_get_ext_prop(eps + idx, pv);
257b0104773SPascal Brand 
258b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
259b0104773SPascal Brand 	}
260b0104773SPascal Brand }
261b0104773SPascal Brand 
262b0104773SPascal Brand TEE_Result TEE_GetPropertyAsString(TEE_PropSetHandle propsetOrEnumerator,
263b0104773SPascal Brand 				   char *name, char *valueBuffer,
264*79a3c601SCedric Chaumont 				   uint32_t *valueBufferLen)
265b0104773SPascal Brand {
266b0104773SPascal Brand 	TEE_Result res;
267b0104773SPascal Brand 	struct prop_value pv;
268b0104773SPascal Brand 	size_t l;
269b0104773SPascal Brand 	size_t bufferlen;
270b0104773SPascal Brand 
271b0104773SPascal Brand 	if (valueBuffer == NULL || valueBufferLen == NULL)
272b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
273b0104773SPascal Brand 
274b0104773SPascal Brand 	bufferlen = *valueBufferLen;
275b0104773SPascal Brand 
276b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
277b0104773SPascal Brand 	if (res != TEE_SUCCESS)
278b0104773SPascal Brand 		return res;
279b0104773SPascal Brand 
280b0104773SPascal Brand 	switch (pv.type) {
281b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BOOL:
282b0104773SPascal Brand 		l = strlcpy(valueBuffer, pv.u.bool_val ? "true" : "false",
283b0104773SPascal Brand 			    bufferlen);
284b0104773SPascal Brand 		break;
285b0104773SPascal Brand 
286b0104773SPascal Brand 	case USER_TA_PROP_TYPE_U32:
287b0104773SPascal Brand 		l = snprintf(valueBuffer, bufferlen, "%u",
288b0104773SPascal Brand 			     (unsigned int)pv.u.int_val);
289b0104773SPascal Brand 		break;
290b0104773SPascal Brand 
291b0104773SPascal Brand 	case USER_TA_PROP_TYPE_UUID:
292b0104773SPascal Brand 		l = snprintf(valueBuffer, bufferlen,
293b0104773SPascal Brand 			     "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
294b0104773SPascal Brand 			     (unsigned int)pv.u.uuid_val.timeLow,
295b0104773SPascal Brand 			     pv.u.uuid_val.timeMid,
296b0104773SPascal Brand 			     pv.u.uuid_val.timeHiAndVersion,
297b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[0],
298b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[1],
299b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[2],
300b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[3],
301b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[4],
302b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[5],
303b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[6],
304b0104773SPascal Brand 			     pv.u.uuid_val.clockSeqAndNode[7]);
305b0104773SPascal Brand 		break;
306b0104773SPascal Brand 
307b0104773SPascal Brand 	case USER_TA_PROP_TYPE_IDENTITY:
308b0104773SPascal Brand 		l = snprintf(valueBuffer, bufferlen,
309b0104773SPascal Brand 			     "%u:%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
310b0104773SPascal Brand 			     (unsigned int)pv.u.identity_val.login,
311b0104773SPascal Brand 			     (unsigned int)pv.u.identity_val.uuid.timeLow,
312b0104773SPascal Brand 			     pv.u.identity_val.uuid.timeMid,
313b0104773SPascal Brand 			     pv.u.identity_val.uuid.timeHiAndVersion,
314b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[0],
315b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[1],
316b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[2],
317b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[3],
318b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[4],
319b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[5],
320b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[6],
321b0104773SPascal Brand 			     pv.u.identity_val.uuid.clockSeqAndNode[7]);
322b0104773SPascal Brand 		break;
323b0104773SPascal Brand 
324b0104773SPascal Brand 	case USER_TA_PROP_TYPE_STRING:
325b0104773SPascal Brand 	case USER_TA_PROP_TYPE_BINARY_BLOCK:
326b0104773SPascal Brand 		l = strlcpy(valueBuffer, pv.u.str_val, bufferlen);
327b0104773SPascal Brand 		break;
328b0104773SPascal Brand 
329b0104773SPascal Brand 	default:
330b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
331b0104773SPascal Brand 	}
332b0104773SPascal Brand 
333b0104773SPascal Brand 	/* The size "must account for the zero terminator" */
334b0104773SPascal Brand 	*valueBufferLen = l + 1;
335b0104773SPascal Brand 
336b0104773SPascal Brand 	if (l >= bufferlen)
337b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
338b0104773SPascal Brand 
339b0104773SPascal Brand 	return TEE_SUCCESS;
340b0104773SPascal Brand }
341b0104773SPascal Brand 
342b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBool(TEE_PropSetHandle propsetOrEnumerator,
343b0104773SPascal Brand 				 char *name, bool *value)
344b0104773SPascal Brand {
345b0104773SPascal Brand 	TEE_Result res;
346b0104773SPascal Brand 	struct prop_value pv;
347b0104773SPascal Brand 
348b0104773SPascal Brand 	if (value == NULL)
349b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
350b0104773SPascal Brand 
351b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
352b0104773SPascal Brand 	if (res != TEE_SUCCESS)
353b0104773SPascal Brand 		return res;
354b0104773SPascal Brand 
355b0104773SPascal Brand 	if (pv.type != USER_TA_PROP_TYPE_BOOL)
356b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
357b0104773SPascal Brand 
358b0104773SPascal Brand 	*value = pv.u.bool_val;
359b0104773SPascal Brand 	return TEE_SUCCESS;
360b0104773SPascal Brand }
361b0104773SPascal Brand 
362b0104773SPascal Brand TEE_Result TEE_GetPropertyAsU32(TEE_PropSetHandle propsetOrEnumerator,
363b0104773SPascal Brand 				char *name, uint32_t *value)
364b0104773SPascal Brand {
365b0104773SPascal Brand 	TEE_Result res;
366b0104773SPascal Brand 	struct prop_value pv;
367b0104773SPascal Brand 
368b0104773SPascal Brand 	if (value == NULL)
369b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
370b0104773SPascal Brand 
371b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
372b0104773SPascal Brand 	if (res != TEE_SUCCESS)
373b0104773SPascal Brand 		return res;
374b0104773SPascal Brand 
375b0104773SPascal Brand 	if (pv.type != USER_TA_PROP_TYPE_U32)
376b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
377b0104773SPascal Brand 
378b0104773SPascal Brand 	*value = pv.u.int_val;
379b0104773SPascal Brand 	return TEE_SUCCESS;
380b0104773SPascal Brand }
381b0104773SPascal Brand 
382b0104773SPascal Brand TEE_Result TEE_GetPropertyAsBinaryBlock(TEE_PropSetHandle propsetOrEnumerator,
383b0104773SPascal Brand 					char *name, void *valueBuffer,
384*79a3c601SCedric Chaumont 					uint32_t *valueBufferLen)
385b0104773SPascal Brand {
386b0104773SPascal Brand 	TEE_Result res;
387b0104773SPascal Brand 	struct prop_value pv;
388b0104773SPascal Brand 	void *val;
389b0104773SPascal Brand 	int val_len;
390b0104773SPascal Brand 
391b0104773SPascal Brand 	if (valueBuffer == NULL || valueBufferLen == NULL)
392b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
393b0104773SPascal Brand 
394b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
395b0104773SPascal Brand 	if (res != TEE_SUCCESS)
396b0104773SPascal Brand 		return res;
397b0104773SPascal Brand 
398b0104773SPascal Brand 	if (pv.type != USER_TA_PROP_TYPE_BINARY_BLOCK)
399b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
400b0104773SPascal Brand 
401b0104773SPascal Brand 	val = pv.u.str_val;
402b0104773SPascal Brand 	val_len = strlen(val);
403b0104773SPascal Brand 	if (!base64_dec(val, val_len, valueBuffer, valueBufferLen))
404b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
405b0104773SPascal Brand 	return TEE_SUCCESS;
406b0104773SPascal Brand }
407b0104773SPascal Brand 
408b0104773SPascal Brand TEE_Result TEE_GetPropertyAsUUID(TEE_PropSetHandle propsetOrEnumerator,
409b0104773SPascal Brand 				 char *name, TEE_UUID *value)
410b0104773SPascal Brand {
411b0104773SPascal Brand 	TEE_Result res;
412b0104773SPascal Brand 	struct prop_value pv;
413b0104773SPascal Brand 
414b0104773SPascal Brand 	if (value == NULL)
415b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
416b0104773SPascal Brand 
417b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
418b0104773SPascal Brand 	if (res != TEE_SUCCESS)
419b0104773SPascal Brand 		return res;
420b0104773SPascal Brand 
421b0104773SPascal Brand 	if (pv.type != USER_TA_PROP_TYPE_UUID)
422b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
423b0104773SPascal Brand 
424b0104773SPascal Brand 	*value = pv.u.uuid_val;	/* struct copy */
425b0104773SPascal Brand 	return TEE_SUCCESS;
426b0104773SPascal Brand }
427b0104773SPascal Brand 
428b0104773SPascal Brand TEE_Result TEE_GetPropertyAsIdentity(TEE_PropSetHandle propsetOrEnumerator,
429b0104773SPascal Brand 				     char *name, TEE_Identity *value)
430b0104773SPascal Brand {
431b0104773SPascal Brand 	TEE_Result res;
432b0104773SPascal Brand 	struct prop_value pv;
433b0104773SPascal Brand 
434b0104773SPascal Brand 	if (value == NULL)
435b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
436b0104773SPascal Brand 
437b0104773SPascal Brand 	res = propget_get_property(propsetOrEnumerator, name, &pv);
438b0104773SPascal Brand 	if (res != TEE_SUCCESS)
439b0104773SPascal Brand 		return res;
440b0104773SPascal Brand 
441b0104773SPascal Brand 	if (pv.type != USER_TA_PROP_TYPE_IDENTITY)
442b0104773SPascal Brand 		return TEE_ERROR_BAD_FORMAT;
443b0104773SPascal Brand 
444b0104773SPascal Brand 	*value = pv.u.identity_val;	/* struct copy */
445b0104773SPascal Brand 	return TEE_SUCCESS;
446b0104773SPascal Brand }
447b0104773SPascal Brand 
448b0104773SPascal Brand TEE_Result TEE_AllocatePropertyEnumerator(TEE_PropSetHandle *enumerator)
449b0104773SPascal Brand {
450b0104773SPascal Brand 	struct prop_enumerator *pe;
451b0104773SPascal Brand 
452b0104773SPascal Brand 	if (enumerator == NULL)
453b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
454b0104773SPascal Brand 
455b0104773SPascal Brand 	pe = TEE_Malloc(sizeof(struct prop_enumerator),
456b0104773SPascal Brand 			TEE_USER_MEM_HINT_NO_FILL_ZERO);
457b0104773SPascal Brand 	if (pe == NULL)
458b0104773SPascal Brand 		return TEE_ERROR_OUT_OF_MEMORY;
459b0104773SPascal Brand 
460b0104773SPascal Brand 	*enumerator = (TEE_PropSetHandle) pe;
461b0104773SPascal Brand 	TEE_ResetPropertyEnumerator(*enumerator);
462b0104773SPascal Brand 	return TEE_SUCCESS;
463b0104773SPascal Brand }
464b0104773SPascal Brand 
465b0104773SPascal Brand void TEE_ResetPropertyEnumerator(TEE_PropSetHandle enumerator)
466b0104773SPascal Brand {
467b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
468b0104773SPascal Brand 
469b0104773SPascal Brand 	pe->idx = PROP_ENUMERATOR_NOT_STARTED;
470b0104773SPascal Brand }
471b0104773SPascal Brand 
472b0104773SPascal Brand void TEE_FreePropertyEnumerator(TEE_PropSetHandle enumerator)
473b0104773SPascal Brand {
474b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
475b0104773SPascal Brand 
476b0104773SPascal Brand 	TEE_Free(pe);
477b0104773SPascal Brand }
478b0104773SPascal Brand 
479b0104773SPascal Brand void TEE_StartPropertyEnumerator(TEE_PropSetHandle enumerator,
480b0104773SPascal Brand 				 TEE_PropSetHandle propSet)
481b0104773SPascal Brand {
482b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
483b0104773SPascal Brand 
484b0104773SPascal Brand 	if (pe == NULL)
485b0104773SPascal Brand 		return;
486b0104773SPascal Brand 
487b0104773SPascal Brand 	pe->idx = 0;
488b0104773SPascal Brand 	pe->prop_set = propSet;
489b0104773SPascal Brand }
490b0104773SPascal Brand 
491b0104773SPascal Brand TEE_Result TEE_GetPropertyName(TEE_PropSetHandle enumerator,
492*79a3c601SCedric Chaumont 			       void *nameBuffer, uint32_t *nameBufferLen)
493b0104773SPascal Brand {
494b0104773SPascal Brand 	TEE_Result res;
495b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
496b0104773SPascal Brand 	const struct prop_set *ps;
497b0104773SPascal Brand 	size_t ps_len;
498b0104773SPascal Brand 	const struct user_ta_property *eps;
499b0104773SPascal Brand 	size_t eps_len;
500b0104773SPascal Brand 	size_t l;
501b0104773SPascal Brand 	const char *str;
502b0104773SPascal Brand 	size_t bufferlen;
503b0104773SPascal Brand 
504b0104773SPascal Brand 	if (pe == NULL || nameBuffer == NULL || nameBufferLen == NULL)
505b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
506b0104773SPascal Brand 
507b0104773SPascal Brand 	bufferlen = *nameBufferLen;
508b0104773SPascal Brand 	res = propset_get(pe->prop_set, &ps, &ps_len, &eps, &eps_len);
509b0104773SPascal Brand 	if (res != TEE_SUCCESS)
510b0104773SPascal Brand 		return res;
511b0104773SPascal Brand 
512b0104773SPascal Brand 	if (pe->idx < ps_len)
513b0104773SPascal Brand 		str = ps[pe->idx].str;
514b0104773SPascal Brand 	else if ((pe->idx - ps_len) < eps_len)
515b0104773SPascal Brand 		str = ta_props[pe->idx - ps_len].name;
516b0104773SPascal Brand 	else
517b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
518b0104773SPascal Brand 
519b0104773SPascal Brand 	l = strlcpy(nameBuffer, str, bufferlen);
520b0104773SPascal Brand 
521b0104773SPascal Brand 	/* The size "must account for the zero terminator" */
522b0104773SPascal Brand 	*nameBufferLen = l + 1;
523b0104773SPascal Brand 
524b0104773SPascal Brand 	if (l >= bufferlen)
525b0104773SPascal Brand 		return TEE_ERROR_SHORT_BUFFER;
526b0104773SPascal Brand 
527b0104773SPascal Brand 	return TEE_SUCCESS;
528b0104773SPascal Brand }
529b0104773SPascal Brand 
530b0104773SPascal Brand TEE_Result TEE_GetNextProperty(TEE_PropSetHandle enumerator)
531b0104773SPascal Brand {
532b0104773SPascal Brand 	TEE_Result res;
533b0104773SPascal Brand 	struct prop_enumerator *pe = (struct prop_enumerator *)enumerator;
534b0104773SPascal Brand 	uint32_t next_idx;
535b0104773SPascal Brand 	const struct prop_set *ps;
536b0104773SPascal Brand 	size_t ps_len;
537b0104773SPascal Brand 	const struct user_ta_property *eps;
538b0104773SPascal Brand 	size_t eps_len;
539b0104773SPascal Brand 
540b0104773SPascal Brand 	if (pe == NULL)
541b0104773SPascal Brand 		return TEE_ERROR_BAD_PARAMETERS;
542b0104773SPascal Brand 
543b0104773SPascal Brand 	if (pe->idx == PROP_ENUMERATOR_NOT_STARTED)
544b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
545b0104773SPascal Brand 
546b0104773SPascal Brand 	res = propset_get(pe->prop_set, &ps, &ps_len, &eps, &eps_len);
547b0104773SPascal Brand 	if (res != TEE_SUCCESS)
548b0104773SPascal Brand 		return res;
549b0104773SPascal Brand 
550b0104773SPascal Brand 	next_idx = pe->idx + 1;
551b0104773SPascal Brand 	pe->idx = next_idx;
552b0104773SPascal Brand 	if (next_idx >= (ps_len + eps_len))
553b0104773SPascal Brand 		return TEE_ERROR_ITEM_NOT_FOUND;
554b0104773SPascal Brand 
555b0104773SPascal Brand 	return TEE_SUCCESS;
556b0104773SPascal Brand }
557