xref: /optee_os/ta/pkcs11/src/pkcs11_helpers.c (revision 7fb525f1f8a6709ba8adc89b1d2f2995dae26803)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2018-2020, Linaro Limited
4  */
5 
6 #include <pkcs11_ta.h>
7 #include <string.h>
8 #include <tee_internal_api.h>
9 #include <util.h>
10 
11 #include "pkcs11_helpers.h"
12 
13 static const char __maybe_unused unknown[] = "<unknown-identifier>";
14 
15 struct any_id {
16 	uint32_t id;
17 #if CFG_TEE_TA_LOG_LEVEL > 0
18 	const char *string;
19 #endif
20 };
21 
22 /*
23  * Macro PKCS11_ID() can be used to define cells in ID list arrays
24  * or ID/string conversion arrays.
25  */
26 #if CFG_TEE_TA_LOG_LEVEL > 0
27 #define PKCS11_ID(_id)		{ .id = _id, .string = #_id }
28 #else
29 #define PKCS11_ID(_id)		{ .id = _id }
30 #endif
31 
32 #define ID2STR(id, table, prefix)	\
33 	id2str(id, table, ARRAY_SIZE(table), prefix)
34 
35 #if CFG_TEE_TA_LOG_LEVEL > 0
36 /* Convert a PKCS11 ID into its label string */
37 static const char *id2str(uint32_t id, const struct any_id *table,
38 			  size_t count, const char *prefix)
39 {
40 	size_t n = 0;
41 	const char *str = NULL;
42 
43 	for (n = 0; n < count; n++) {
44 		if (id != table[n].id)
45 			continue;
46 
47 		str = table[n].string;
48 
49 		/* Skip prefix provided matches found */
50 		if (prefix && !TEE_MemCompare(str, prefix, strlen(prefix)))
51 			str += strlen(prefix);
52 
53 		return str;
54 	}
55 
56 	return unknown;
57 }
58 #endif /* CFG_TEE_TA_LOG_LEVEL > 0 */
59 
60 /*
61  * TA command IDs: used only as ID/string conversion for debug trace support
62  */
63 static const struct any_id __maybe_unused string_ta_cmd[] = {
64 	PKCS11_ID(PKCS11_CMD_PING),
65 };
66 
67 #if CFG_TEE_TA_LOG_LEVEL > 0
68 const char *id2str_ta_cmd(uint32_t id)
69 {
70 	return ID2STR(id, string_ta_cmd, NULL);
71 }
72 #endif /*CFG_TEE_TA_LOG_LEVEL*/
73