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