xref: /optee_os/ta/pkcs11/src/pkcs11_helpers.c (revision ddf63ac313b98ca919f23b8bfc88cce2a16c29b4)
1f7d7fcd9SEtienne Carriere // SPDX-License-Identifier: BSD-2-Clause
2f7d7fcd9SEtienne Carriere /*
3f7d7fcd9SEtienne Carriere  * Copyright (c) 2018-2020, Linaro Limited
4f7d7fcd9SEtienne Carriere  */
5f7d7fcd9SEtienne Carriere 
6f7d7fcd9SEtienne Carriere #include <pkcs11_ta.h>
7f7d7fcd9SEtienne Carriere #include <string.h>
8f7d7fcd9SEtienne Carriere #include <tee_internal_api.h>
9f7d7fcd9SEtienne Carriere #include <util.h>
10f7d7fcd9SEtienne Carriere 
11f7d7fcd9SEtienne Carriere #include "pkcs11_helpers.h"
12f7d7fcd9SEtienne Carriere 
13f7d7fcd9SEtienne Carriere static const char __maybe_unused unknown[] = "<unknown-identifier>";
14f7d7fcd9SEtienne Carriere 
15f7d7fcd9SEtienne Carriere struct any_id {
16f7d7fcd9SEtienne Carriere 	uint32_t id;
17f7d7fcd9SEtienne Carriere #if CFG_TEE_TA_LOG_LEVEL > 0
18f7d7fcd9SEtienne Carriere 	const char *string;
19f7d7fcd9SEtienne Carriere #endif
20f7d7fcd9SEtienne Carriere };
21f7d7fcd9SEtienne Carriere 
22f7d7fcd9SEtienne Carriere /*
23f7d7fcd9SEtienne Carriere  * Macro PKCS11_ID() can be used to define cells in ID list arrays
24f7d7fcd9SEtienne Carriere  * or ID/string conversion arrays.
25f7d7fcd9SEtienne Carriere  */
26f7d7fcd9SEtienne Carriere #if CFG_TEE_TA_LOG_LEVEL > 0
27f7d7fcd9SEtienne Carriere #define PKCS11_ID(_id)		{ .id = _id, .string = #_id }
28f7d7fcd9SEtienne Carriere #else
29f7d7fcd9SEtienne Carriere #define PKCS11_ID(_id)		{ .id = _id }
30f7d7fcd9SEtienne Carriere #endif
31f7d7fcd9SEtienne Carriere 
32f7d7fcd9SEtienne Carriere #define ID2STR(id, table, prefix)	\
33f7d7fcd9SEtienne Carriere 	id2str(id, table, ARRAY_SIZE(table), prefix)
34f7d7fcd9SEtienne Carriere 
3560290f69SEtienne Carriere #if CFG_TEE_TA_LOG_LEVEL > 0
36f7d7fcd9SEtienne Carriere /* Convert a PKCS11 ID into its label string */
3760290f69SEtienne Carriere static const char *id2str(uint32_t id, const struct any_id *table,
38f7d7fcd9SEtienne Carriere 			  size_t count, const char *prefix)
39f7d7fcd9SEtienne Carriere {
40f7d7fcd9SEtienne Carriere 	size_t n = 0;
41f7d7fcd9SEtienne Carriere 	const char *str = NULL;
42f7d7fcd9SEtienne Carriere 
43f7d7fcd9SEtienne Carriere 	for (n = 0; n < count; n++) {
44f7d7fcd9SEtienne Carriere 		if (id != table[n].id)
45f7d7fcd9SEtienne Carriere 			continue;
46f7d7fcd9SEtienne Carriere 
47f7d7fcd9SEtienne Carriere 		str = table[n].string;
48f7d7fcd9SEtienne Carriere 
49f7d7fcd9SEtienne Carriere 		/* Skip prefix provided matches found */
50f7d7fcd9SEtienne Carriere 		if (prefix && !TEE_MemCompare(str, prefix, strlen(prefix)))
51f7d7fcd9SEtienne Carriere 			str += strlen(prefix);
52f7d7fcd9SEtienne Carriere 
53f7d7fcd9SEtienne Carriere 		return str;
54f7d7fcd9SEtienne Carriere 	}
55f7d7fcd9SEtienne Carriere 
56f7d7fcd9SEtienne Carriere 	return unknown;
57f7d7fcd9SEtienne Carriere }
5860290f69SEtienne Carriere #endif /* CFG_TEE_TA_LOG_LEVEL > 0 */
59f7d7fcd9SEtienne Carriere 
60f7d7fcd9SEtienne Carriere /*
61f7d7fcd9SEtienne Carriere  * TA command IDs: used only as ID/string conversion for debug trace support
62f7d7fcd9SEtienne Carriere  */
63f7d7fcd9SEtienne Carriere static const struct any_id __maybe_unused string_ta_cmd[] = {
64f7d7fcd9SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_PING),
65a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_SLOT_LIST),
66a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_SLOT_INFO),
67a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_TOKEN_INFO),
68d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_MECHANISM_IDS),
69d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_MECHANISM_INFO),
70d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_OPEN_SESSION),
71d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_SESSION_INFO),
72d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_CLOSE_SESSION),
73d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_CLOSE_ALL_SESSIONS),
74*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_INIT_TOKEN),
75*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_INIT_PIN),
76*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_SET_PIN),
77*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_LOGIN),
78*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_LOGOUT),
79*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_CREATE_OBJECT),
80*ddf63ac3SEtienne Carriere 	PKCS11_ID(PKCS11_CMD_DESTROY_OBJECT),
81a67dc424SEtienne Carriere };
82a67dc424SEtienne Carriere 
83a67dc424SEtienne Carriere static const struct any_id __maybe_unused string_slot_flags[] = {
84a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFS_TOKEN_PRESENT),
85a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFS_REMOVABLE_DEVICE),
86a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFS_HW_SLOT),
87a67dc424SEtienne Carriere };
88a67dc424SEtienne Carriere 
89a67dc424SEtienne Carriere static const struct any_id __maybe_unused string_token_flags[] = {
90a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_RNG),
91a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_WRITE_PROTECTED),
92a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_LOGIN_REQUIRED),
93a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_USER_PIN_INITIALIZED),
94a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_RESTORE_KEY_NOT_NEEDED),
95a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_CLOCK_ON_TOKEN),
96a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_PROTECTED_AUTHENTICATION_PATH),
97a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_DUAL_CRYPTO_OPERATIONS),
98a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_TOKEN_INITIALIZED),
99a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_USER_PIN_COUNT_LOW),
100a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_USER_PIN_FINAL_TRY),
101a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_USER_PIN_LOCKED),
102a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_USER_PIN_TO_BE_CHANGED),
103a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_SO_PIN_COUNT_LOW),
104a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_SO_PIN_FINAL_TRY),
105a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_SO_PIN_LOCKED),
106a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_SO_PIN_TO_BE_CHANGED),
107a67dc424SEtienne Carriere 	PKCS11_ID(PKCS11_CKFT_ERROR_STATE),
108f7d7fcd9SEtienne Carriere };
109f7d7fcd9SEtienne Carriere 
110d21ec5f4SEtienne Carriere static const struct any_id __maybe_unused string_session_flags[] = {
111d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKFSS_RW_SESSION),
112d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKFSS_SERIAL_SESSION),
113d21ec5f4SEtienne Carriere };
114d21ec5f4SEtienne Carriere 
115d21ec5f4SEtienne Carriere static const struct any_id __maybe_unused string_session_state[] = {
116d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKS_RO_PUBLIC_SESSION),
117d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKS_RO_USER_FUNCTIONS),
118d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKS_RW_PUBLIC_SESSION),
119d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKS_RW_USER_FUNCTIONS),
120d21ec5f4SEtienne Carriere 	PKCS11_ID(PKCS11_CKS_RW_SO_FUNCTIONS),
121d21ec5f4SEtienne Carriere };
122d21ec5f4SEtienne Carriere 
123d34f3266SEtienne Carriere static const struct any_id __maybe_unused string_rc[] = {
124d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_OK),
125d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_GENERAL_ERROR),
126d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_DEVICE_MEMORY),
127d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_ARGUMENTS_BAD),
128d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_BUFFER_TOO_SMALL),
129d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_FUNCTION_FAILED),
130d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SIGNATURE_INVALID),
131d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_TYPE_INVALID),
132d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_VALUE_INVALID),
133d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_OBJECT_HANDLE_INVALID),
134d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_KEY_HANDLE_INVALID),
135d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_MECHANISM_INVALID),
136d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SESSION_HANDLE_INVALID),
137d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SLOT_ID_INVALID),
138d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_MECHANISM_PARAM_INVALID),
139d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_TEMPLATE_INCONSISTENT),
140d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_TEMPLATE_INCOMPLETE),
141d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_INCORRECT),
142d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_LOCKED),
143d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_EXPIRED),
144d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_INVALID),
145d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_LEN_RANGE),
146d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SESSION_EXISTS),
147d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SESSION_READ_ONLY),
148d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SESSION_READ_WRITE_SO_EXISTS),
149d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_OPERATION_ACTIVE),
150d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED),
151d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_OPERATION_NOT_INITIALIZED),
152d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_TOKEN_WRITE_PROTECTED),
153d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_TOKEN_NOT_PRESENT),
154d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_TOKEN_NOT_RECOGNIZED),
155d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_ACTION_PROHIBITED),
156d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_READ_ONLY),
157d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_PIN_TOO_WEAK),
158d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_CURVE_NOT_SUPPORTED),
159d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_DOMAIN_PARAMS_INVALID),
160d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_ALREADY_LOGGED_IN),
161d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_ANOTHER_ALREADY_LOGGED_IN),
162d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_NOT_LOGGED_IN),
163d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_PIN_NOT_INITIALIZED),
164d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_TOO_MANY_TYPES),
165d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_USER_TYPE_INVALID),
166d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_CKR_SESSION_READ_ONLY_EXISTS),
167d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_RV_NOT_FOUND),
168d34f3266SEtienne Carriere 	PKCS11_ID(PKCS11_RV_NOT_IMPLEMENTED),
169d34f3266SEtienne Carriere };
170d34f3266SEtienne Carriere 
1718e03579eSJens Wiklander /*
1728e03579eSJens Wiklander  * Conversion between PKCS11 TA and GPD TEE return codes
1738e03579eSJens Wiklander  */
1748e03579eSJens Wiklander enum pkcs11_rc tee2pkcs_error(TEE_Result res)
1758e03579eSJens Wiklander {
1768e03579eSJens Wiklander 	switch (res) {
1778e03579eSJens Wiklander 	case TEE_SUCCESS:
1788e03579eSJens Wiklander 		return PKCS11_CKR_OK;
1798e03579eSJens Wiklander 
1808e03579eSJens Wiklander 	case TEE_ERROR_BAD_PARAMETERS:
1818e03579eSJens Wiklander 		return PKCS11_CKR_ARGUMENTS_BAD;
1828e03579eSJens Wiklander 
1838e03579eSJens Wiklander 	case TEE_ERROR_OUT_OF_MEMORY:
1848e03579eSJens Wiklander 		return PKCS11_CKR_DEVICE_MEMORY;
1858e03579eSJens Wiklander 
1868e03579eSJens Wiklander 	case TEE_ERROR_SHORT_BUFFER:
1878e03579eSJens Wiklander 		return PKCS11_CKR_BUFFER_TOO_SMALL;
1888e03579eSJens Wiklander 
1898e03579eSJens Wiklander 	case TEE_ERROR_MAC_INVALID:
1908e03579eSJens Wiklander 	case TEE_ERROR_SIGNATURE_INVALID:
1918e03579eSJens Wiklander 		return PKCS11_CKR_SIGNATURE_INVALID;
1928e03579eSJens Wiklander 
1938e03579eSJens Wiklander 	default:
1948e03579eSJens Wiklander 		return PKCS11_CKR_GENERAL_ERROR;
1958e03579eSJens Wiklander 	}
1968e03579eSJens Wiklander }
1978e03579eSJens Wiklander 
198f7d7fcd9SEtienne Carriere #if CFG_TEE_TA_LOG_LEVEL > 0
199d34f3266SEtienne Carriere const char *id2str_rc(uint32_t id)
200d34f3266SEtienne Carriere {
201d34f3266SEtienne Carriere 	return ID2STR(id, string_rc, "PKCS11_CKR_");
202d34f3266SEtienne Carriere }
203d34f3266SEtienne Carriere 
204f7d7fcd9SEtienne Carriere const char *id2str_ta_cmd(uint32_t id)
205f7d7fcd9SEtienne Carriere {
206f7d7fcd9SEtienne Carriere 	return ID2STR(id, string_ta_cmd, NULL);
207f7d7fcd9SEtienne Carriere }
208a67dc424SEtienne Carriere 
209a67dc424SEtienne Carriere const char *id2str_slot_flag(uint32_t id)
210a67dc424SEtienne Carriere {
211a67dc424SEtienne Carriere 	return ID2STR(id, string_slot_flags, "PKCS11_CKFS_");
212a67dc424SEtienne Carriere }
213a67dc424SEtienne Carriere 
214a67dc424SEtienne Carriere const char *id2str_token_flag(uint32_t id)
215a67dc424SEtienne Carriere {
216a67dc424SEtienne Carriere 	return ID2STR(id, string_token_flags, "PKCS11_CKFT_");
217a67dc424SEtienne Carriere }
218d21ec5f4SEtienne Carriere 
219d21ec5f4SEtienne Carriere const char *id2str_session_flag(uint32_t id)
220d21ec5f4SEtienne Carriere {
221d21ec5f4SEtienne Carriere 	return ID2STR(id, string_session_flags, "PKCS11_CKFSS_");
222d21ec5f4SEtienne Carriere }
223d21ec5f4SEtienne Carriere 
224d21ec5f4SEtienne Carriere const char *id2str_session_state(uint32_t id)
225d21ec5f4SEtienne Carriere {
226d21ec5f4SEtienne Carriere 	return ID2STR(id, string_session_state, "PKCS11_CKS_");
227d21ec5f4SEtienne Carriere }
228f7d7fcd9SEtienne Carriere #endif /*CFG_TEE_TA_LOG_LEVEL*/
229