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 "attributes.h"
12 #include "object.h"
13 #include "pkcs11_attributes.h"
14 #include "pkcs11_helpers.h"
15 #include "processing.h"
16 
17 static const char __maybe_unused unknown[] = "<unknown-identifier>";
18 
19 struct attr_size {
20 	uint32_t id;
21 	uint32_t size;
22 #if CFG_TEE_TA_LOG_LEVEL > 0
23 	const char *string;
24 #endif
25 };
26 
27 #if CFG_TEE_TA_LOG_LEVEL > 0
28 #define PKCS11_ID_SZ(_id, _sz)	\
29 			{ .id = (uint32_t)(_id), .size = (_sz), .string = #_id }
30 #else
31 #define PKCS11_ID_SZ(_id, _sz)	\
32 			{ .id = (uint32_t)(_id), .size = (_sz) }
33 #endif
34 
35 static const struct attr_size attr_ids[] = {
36 	PKCS11_ID_SZ(PKCS11_CKA_CLASS, 4),
37 	PKCS11_ID_SZ(PKCS11_CKA_KEY_TYPE, 4),
38 	PKCS11_ID_SZ(PKCS11_CKA_VALUE, 0),
39 	PKCS11_ID_SZ(PKCS11_CKA_VALUE_LEN, 4),
40 	PKCS11_ID_SZ(PKCS11_CKA_KEY_GEN_MECHANISM, 4),
41 	PKCS11_ID_SZ(PKCS11_CKA_LABEL, 0),
42 	PKCS11_ID_SZ(PKCS11_CKA_CERTIFICATE_TYPE, 4),
43 	PKCS11_ID_SZ(PKCS11_CKA_ISSUER, 0),
44 	PKCS11_ID_SZ(PKCS11_CKA_SERIAL_NUMBER, 0),
45 	PKCS11_ID_SZ(PKCS11_CKA_CERTIFICATE_CATEGORY, 4),
46 	PKCS11_ID_SZ(PKCS11_CKA_URL, 0),
47 	PKCS11_ID_SZ(PKCS11_CKA_HASH_OF_SUBJECT_PUBLIC_KEY, 0),
48 	PKCS11_ID_SZ(PKCS11_CKA_HASH_OF_ISSUER_PUBLIC_KEY, 0),
49 	PKCS11_ID_SZ(PKCS11_CKA_JAVA_MIDP_SECURITY_DOMAIN, 4),
50 	PKCS11_ID_SZ(PKCS11_CKA_NAME_HASH_ALGORITHM, 4),
51 	PKCS11_ID_SZ(PKCS11_CKA_CHECK_VALUE, 0),
52 	PKCS11_ID_SZ(PKCS11_CKA_WRAP_TEMPLATE, 0),
53 	PKCS11_ID_SZ(PKCS11_CKA_UNWRAP_TEMPLATE, 0),
54 	PKCS11_ID_SZ(PKCS11_CKA_DERIVE_TEMPLATE, 0),
55 	PKCS11_ID_SZ(PKCS11_CKA_START_DATE, 4),
56 	PKCS11_ID_SZ(PKCS11_CKA_END_DATE, 4),
57 	PKCS11_ID_SZ(PKCS11_CKA_OBJECT_ID, 0),
58 	PKCS11_ID_SZ(PKCS11_CKA_APPLICATION, 0),
59 	PKCS11_ID_SZ(PKCS11_CKA_MECHANISM_TYPE, 4),
60 	PKCS11_ID_SZ(PKCS11_CKA_ID, 0),
61 	PKCS11_ID_SZ(PKCS11_CKA_ALLOWED_MECHANISMS, 0),
62 	PKCS11_ID_SZ(PKCS11_CKA_EC_POINT, 0),
63 	PKCS11_ID_SZ(PKCS11_CKA_EC_PARAMS, 0),
64 	PKCS11_ID_SZ(PKCS11_CKA_MODULUS, 0),
65 	PKCS11_ID_SZ(PKCS11_CKA_MODULUS_BITS, 4),
66 	PKCS11_ID_SZ(PKCS11_CKA_PUBLIC_EXPONENT, 0),
67 	PKCS11_ID_SZ(PKCS11_CKA_PRIVATE_EXPONENT, 0),
68 	PKCS11_ID_SZ(PKCS11_CKA_PRIME_1, 0),
69 	PKCS11_ID_SZ(PKCS11_CKA_PRIME_2, 0),
70 	PKCS11_ID_SZ(PKCS11_CKA_EXPONENT_1, 0),
71 	PKCS11_ID_SZ(PKCS11_CKA_EXPONENT_2, 0),
72 	PKCS11_ID_SZ(PKCS11_CKA_COEFFICIENT, 0),
73 	PKCS11_ID_SZ(PKCS11_CKA_SUBJECT, 0),
74 	PKCS11_ID_SZ(PKCS11_CKA_PUBLIC_KEY_INFO, 0),
75 	PKCS11_ID_SZ(PKCS11_CKA_KEY_GEN_MECHANISM, 4),
76 	/* Below are boolean attributes */
77 	PKCS11_ID_SZ(PKCS11_CKA_TOKEN, 1),
78 	PKCS11_ID_SZ(PKCS11_CKA_PRIVATE, 1),
79 	PKCS11_ID_SZ(PKCS11_CKA_TRUSTED, 1),
80 	PKCS11_ID_SZ(PKCS11_CKA_SENSITIVE, 1),
81 	PKCS11_ID_SZ(PKCS11_CKA_ENCRYPT, 1),
82 	PKCS11_ID_SZ(PKCS11_CKA_DECRYPT, 1),
83 	PKCS11_ID_SZ(PKCS11_CKA_WRAP, 1),
84 	PKCS11_ID_SZ(PKCS11_CKA_UNWRAP, 1),
85 	PKCS11_ID_SZ(PKCS11_CKA_SIGN, 1),
86 	PKCS11_ID_SZ(PKCS11_CKA_SIGN_RECOVER, 1),
87 	PKCS11_ID_SZ(PKCS11_CKA_VERIFY, 1),
88 	PKCS11_ID_SZ(PKCS11_CKA_VERIFY_RECOVER, 1),
89 	PKCS11_ID_SZ(PKCS11_CKA_DERIVE, 1),
90 	PKCS11_ID_SZ(PKCS11_CKA_EXTRACTABLE, 1),
91 	PKCS11_ID_SZ(PKCS11_CKA_LOCAL, 1),
92 	PKCS11_ID_SZ(PKCS11_CKA_NEVER_EXTRACTABLE, 1),
93 	PKCS11_ID_SZ(PKCS11_CKA_ALWAYS_SENSITIVE, 1),
94 	PKCS11_ID_SZ(PKCS11_CKA_MODIFIABLE, 1),
95 	PKCS11_ID_SZ(PKCS11_CKA_COPYABLE, 1),
96 	PKCS11_ID_SZ(PKCS11_CKA_DESTROYABLE, 1),
97 	PKCS11_ID_SZ(PKCS11_CKA_ALWAYS_AUTHENTICATE, 1),
98 	PKCS11_ID_SZ(PKCS11_CKA_WRAP_WITH_TRUSTED, 1),
99 	/* Specific PKCS11 TA internal attribute ID */
100 	PKCS11_ID_SZ(PKCS11_CKA_UNDEFINED_ID, 0),
101 };
102 
103 struct any_id {
104 	uint32_t id;
105 #if CFG_TEE_TA_LOG_LEVEL > 0
106 	const char *string;
107 #endif
108 };
109 
110 /*
111  * Macro PKCS11_ID() can be used to define cells in ID list arrays
112  * or ID/string conversion arrays.
113  */
114 #if CFG_TEE_TA_LOG_LEVEL > 0
115 #define PKCS11_ID(_id)		{ .id = _id, .string = #_id }
116 #else
117 #define PKCS11_ID(_id)		{ .id = _id }
118 #endif
119 
120 #define ID2STR(id, table, prefix)	\
121 	id2str(id, table, ARRAY_SIZE(table), prefix)
122 
123 #if CFG_TEE_TA_LOG_LEVEL > 0
124 /* Convert a PKCS11 ID into its label string */
id2str(uint32_t id,const struct any_id * table,size_t count,const char * prefix)125 static const char *id2str(uint32_t id, const struct any_id *table,
126 			  size_t count, const char *prefix)
127 {
128 	size_t n = 0;
129 	const char *str = NULL;
130 
131 	for (n = 0; n < count; n++) {
132 		if (id != table[n].id)
133 			continue;
134 
135 		str = table[n].string;
136 
137 		/* Skip prefix provided matches found */
138 		if (prefix && !TEE_MemCompare(str, prefix, strlen(prefix)))
139 			str += strlen(prefix);
140 
141 		return str;
142 	}
143 
144 	return unknown;
145 }
146 #endif /* CFG_TEE_TA_LOG_LEVEL > 0 */
147 
148 /*
149  * TA command IDs: used only as ID/string conversion for debug trace support
150  */
151 static const struct any_id __maybe_unused string_ta_cmd[] = {
152 	PKCS11_ID(PKCS11_CMD_PING),
153 	PKCS11_ID(PKCS11_CMD_SLOT_LIST),
154 	PKCS11_ID(PKCS11_CMD_SLOT_INFO),
155 	PKCS11_ID(PKCS11_CMD_TOKEN_INFO),
156 	PKCS11_ID(PKCS11_CMD_MECHANISM_IDS),
157 	PKCS11_ID(PKCS11_CMD_MECHANISM_INFO),
158 	PKCS11_ID(PKCS11_CMD_OPEN_SESSION),
159 	PKCS11_ID(PKCS11_CMD_SESSION_INFO),
160 	PKCS11_ID(PKCS11_CMD_CLOSE_SESSION),
161 	PKCS11_ID(PKCS11_CMD_CLOSE_ALL_SESSIONS),
162 	PKCS11_ID(PKCS11_CMD_INIT_TOKEN),
163 	PKCS11_ID(PKCS11_CMD_INIT_PIN),
164 	PKCS11_ID(PKCS11_CMD_SET_PIN),
165 	PKCS11_ID(PKCS11_CMD_LOGIN),
166 	PKCS11_ID(PKCS11_CMD_LOGOUT),
167 	PKCS11_ID(PKCS11_CMD_CREATE_OBJECT),
168 	PKCS11_ID(PKCS11_CMD_DESTROY_OBJECT),
169 	PKCS11_ID(PKCS11_CMD_ENCRYPT_INIT),
170 	PKCS11_ID(PKCS11_CMD_DECRYPT_INIT),
171 	PKCS11_ID(PKCS11_CMD_ENCRYPT_UPDATE),
172 	PKCS11_ID(PKCS11_CMD_DECRYPT_UPDATE),
173 	PKCS11_ID(PKCS11_CMD_ENCRYPT_FINAL),
174 	PKCS11_ID(PKCS11_CMD_DECRYPT_FINAL),
175 	PKCS11_ID(PKCS11_CMD_ENCRYPT_ONESHOT),
176 	PKCS11_ID(PKCS11_CMD_DECRYPT_ONESHOT),
177 	PKCS11_ID(PKCS11_CMD_SIGN_INIT),
178 	PKCS11_ID(PKCS11_CMD_VERIFY_INIT),
179 	PKCS11_ID(PKCS11_CMD_SIGN_UPDATE),
180 	PKCS11_ID(PKCS11_CMD_VERIFY_UPDATE),
181 	PKCS11_ID(PKCS11_CMD_SIGN_FINAL),
182 	PKCS11_ID(PKCS11_CMD_VERIFY_FINAL),
183 	PKCS11_ID(PKCS11_CMD_SIGN_ONESHOT),
184 	PKCS11_ID(PKCS11_CMD_VERIFY_ONESHOT),
185 	PKCS11_ID(PKCS11_CMD_GENERATE_KEY),
186 	PKCS11_ID(PKCS11_CMD_FIND_OBJECTS_INIT),
187 	PKCS11_ID(PKCS11_CMD_FIND_OBJECTS),
188 	PKCS11_ID(PKCS11_CMD_FIND_OBJECTS_FINAL),
189 	PKCS11_ID(PKCS11_CMD_GET_OBJECT_SIZE),
190 	PKCS11_ID(PKCS11_CMD_GET_ATTRIBUTE_VALUE),
191 	PKCS11_ID(PKCS11_CMD_SET_ATTRIBUTE_VALUE),
192 	PKCS11_ID(PKCS11_CMD_COPY_OBJECT),
193 	PKCS11_ID(PKCS11_CMD_SEED_RANDOM),
194 	PKCS11_ID(PKCS11_CMD_GENERATE_RANDOM),
195 	PKCS11_ID(PKCS11_CMD_DERIVE_KEY),
196 	PKCS11_ID(PKCS11_CMD_RELEASE_ACTIVE_PROCESSING),
197 	PKCS11_ID(PKCS11_CMD_DIGEST_INIT),
198 	PKCS11_ID(PKCS11_CMD_DIGEST_UPDATE),
199 	PKCS11_ID(PKCS11_CMD_DIGEST_KEY),
200 	PKCS11_ID(PKCS11_CMD_DIGEST_ONESHOT),
201 	PKCS11_ID(PKCS11_CMD_DIGEST_FINAL),
202 	PKCS11_ID(PKCS11_CMD_GENERATE_KEY_PAIR),
203 	PKCS11_ID(PKCS11_CMD_WRAP_KEY),
204 	PKCS11_ID(PKCS11_CMD_UNWRAP_KEY),
205 };
206 
207 static const struct any_id __maybe_unused string_slot_flags[] = {
208 	PKCS11_ID(PKCS11_CKFS_TOKEN_PRESENT),
209 	PKCS11_ID(PKCS11_CKFS_REMOVABLE_DEVICE),
210 	PKCS11_ID(PKCS11_CKFS_HW_SLOT),
211 };
212 
213 static const struct any_id __maybe_unused string_token_flags[] = {
214 	PKCS11_ID(PKCS11_CKFT_RNG),
215 	PKCS11_ID(PKCS11_CKFT_WRITE_PROTECTED),
216 	PKCS11_ID(PKCS11_CKFT_LOGIN_REQUIRED),
217 	PKCS11_ID(PKCS11_CKFT_USER_PIN_INITIALIZED),
218 	PKCS11_ID(PKCS11_CKFT_RESTORE_KEY_NOT_NEEDED),
219 	PKCS11_ID(PKCS11_CKFT_CLOCK_ON_TOKEN),
220 	PKCS11_ID(PKCS11_CKFT_PROTECTED_AUTHENTICATION_PATH),
221 	PKCS11_ID(PKCS11_CKFT_DUAL_CRYPTO_OPERATIONS),
222 	PKCS11_ID(PKCS11_CKFT_TOKEN_INITIALIZED),
223 	PKCS11_ID(PKCS11_CKFT_USER_PIN_COUNT_LOW),
224 	PKCS11_ID(PKCS11_CKFT_USER_PIN_FINAL_TRY),
225 	PKCS11_ID(PKCS11_CKFT_USER_PIN_LOCKED),
226 	PKCS11_ID(PKCS11_CKFT_USER_PIN_TO_BE_CHANGED),
227 	PKCS11_ID(PKCS11_CKFT_SO_PIN_COUNT_LOW),
228 	PKCS11_ID(PKCS11_CKFT_SO_PIN_FINAL_TRY),
229 	PKCS11_ID(PKCS11_CKFT_SO_PIN_LOCKED),
230 	PKCS11_ID(PKCS11_CKFT_SO_PIN_TO_BE_CHANGED),
231 	PKCS11_ID(PKCS11_CKFT_ERROR_STATE),
232 };
233 
234 static const struct any_id __maybe_unused string_session_flags[] = {
235 	PKCS11_ID(PKCS11_CKFSS_RW_SESSION),
236 	PKCS11_ID(PKCS11_CKFSS_SERIAL_SESSION),
237 };
238 
239 static const struct any_id __maybe_unused string_session_state[] = {
240 	PKCS11_ID(PKCS11_CKS_RO_PUBLIC_SESSION),
241 	PKCS11_ID(PKCS11_CKS_RO_USER_FUNCTIONS),
242 	PKCS11_ID(PKCS11_CKS_RW_PUBLIC_SESSION),
243 	PKCS11_ID(PKCS11_CKS_RW_USER_FUNCTIONS),
244 	PKCS11_ID(PKCS11_CKS_RW_SO_FUNCTIONS),
245 };
246 
247 static const struct any_id __maybe_unused string_rc[] = {
248 	PKCS11_ID(PKCS11_CKR_OK),
249 	PKCS11_ID(PKCS11_CKR_SLOT_ID_INVALID),
250 	PKCS11_ID(PKCS11_CKR_GENERAL_ERROR),
251 	PKCS11_ID(PKCS11_CKR_FUNCTION_FAILED),
252 	PKCS11_ID(PKCS11_CKR_ARGUMENTS_BAD),
253 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_READ_ONLY),
254 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_SENSITIVE),
255 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_TYPE_INVALID),
256 	PKCS11_ID(PKCS11_CKR_ATTRIBUTE_VALUE_INVALID),
257 	PKCS11_ID(PKCS11_CKR_ACTION_PROHIBITED),
258 	PKCS11_ID(PKCS11_CKR_DATA_LEN_RANGE),
259 	PKCS11_ID(PKCS11_CKR_DEVICE_MEMORY),
260 	PKCS11_ID(PKCS11_CKR_ENCRYPTED_DATA_INVALID),
261 	PKCS11_ID(PKCS11_CKR_ENCRYPTED_DATA_LEN_RANGE),
262 	PKCS11_ID(PKCS11_CKR_KEY_HANDLE_INVALID),
263 	PKCS11_ID(PKCS11_CKR_KEY_SIZE_RANGE),
264 	PKCS11_ID(PKCS11_CKR_KEY_TYPE_INCONSISTENT),
265 	PKCS11_ID(PKCS11_CKR_KEY_INDIGESTIBLE),
266 	PKCS11_ID(PKCS11_CKR_KEY_FUNCTION_NOT_PERMITTED),
267 	PKCS11_ID(PKCS11_CKR_KEY_NOT_WRAPPABLE),
268 	PKCS11_ID(PKCS11_CKR_KEY_UNEXTRACTABLE),
269 	PKCS11_ID(PKCS11_CKR_MECHANISM_INVALID),
270 	PKCS11_ID(PKCS11_CKR_MECHANISM_PARAM_INVALID),
271 	PKCS11_ID(PKCS11_CKR_OBJECT_HANDLE_INVALID),
272 	PKCS11_ID(PKCS11_CKR_OPERATION_ACTIVE),
273 	PKCS11_ID(PKCS11_CKR_OPERATION_NOT_INITIALIZED),
274 	PKCS11_ID(PKCS11_CKR_PIN_INCORRECT),
275 	PKCS11_ID(PKCS11_CKR_PIN_INVALID),
276 	PKCS11_ID(PKCS11_CKR_PIN_LEN_RANGE),
277 	PKCS11_ID(PKCS11_CKR_PIN_EXPIRED),
278 	PKCS11_ID(PKCS11_CKR_PIN_LOCKED),
279 	PKCS11_ID(PKCS11_CKR_SESSION_HANDLE_INVALID),
280 	PKCS11_ID(PKCS11_CKR_SESSION_PARALLEL_NOT_SUPPORTED),
281 	PKCS11_ID(PKCS11_CKR_SESSION_READ_ONLY),
282 	PKCS11_ID(PKCS11_CKR_SESSION_EXISTS),
283 	PKCS11_ID(PKCS11_CKR_SESSION_READ_ONLY_EXISTS),
284 	PKCS11_ID(PKCS11_CKR_SESSION_READ_WRITE_SO_EXISTS),
285 	PKCS11_ID(PKCS11_CKR_SIGNATURE_INVALID),
286 	PKCS11_ID(PKCS11_CKR_SIGNATURE_LEN_RANGE),
287 	PKCS11_ID(PKCS11_CKR_TEMPLATE_INCOMPLETE),
288 	PKCS11_ID(PKCS11_CKR_TEMPLATE_INCONSISTENT),
289 	PKCS11_ID(PKCS11_CKR_TOKEN_NOT_PRESENT),
290 	PKCS11_ID(PKCS11_CKR_TOKEN_NOT_RECOGNIZED),
291 	PKCS11_ID(PKCS11_CKR_TOKEN_WRITE_PROTECTED),
292 	PKCS11_ID(PKCS11_CKR_UNWRAPPING_KEY_HANDLE_INVALID),
293 	PKCS11_ID(PKCS11_CKR_UNWRAPPING_KEY_SIZE_RANGE),
294 	PKCS11_ID(PKCS11_CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT),
295 	PKCS11_ID(PKCS11_CKR_USER_ALREADY_LOGGED_IN),
296 	PKCS11_ID(PKCS11_CKR_USER_NOT_LOGGED_IN),
297 	PKCS11_ID(PKCS11_CKR_USER_PIN_NOT_INITIALIZED),
298 	PKCS11_ID(PKCS11_CKR_USER_TYPE_INVALID),
299 	PKCS11_ID(PKCS11_CKR_USER_ANOTHER_ALREADY_LOGGED_IN),
300 	PKCS11_ID(PKCS11_CKR_USER_TOO_MANY_TYPES),
301 	PKCS11_ID(PKCS11_CKR_WRAPPED_KEY_INVALID),
302 	PKCS11_ID(PKCS11_CKR_WRAPPED_KEY_LEN_RANGE),
303 	PKCS11_ID(PKCS11_CKR_WRAPPING_KEY_HANDLE_INVALID),
304 	PKCS11_ID(PKCS11_CKR_WRAPPING_KEY_SIZE_RANGE),
305 	PKCS11_ID(PKCS11_CKR_WRAPPING_KEY_TYPE_INCONSISTENT),
306 	PKCS11_ID(PKCS11_CKR_RANDOM_SEED_NOT_SUPPORTED),
307 	PKCS11_ID(PKCS11_CKR_RANDOM_NO_RNG),
308 	PKCS11_ID(PKCS11_CKR_DOMAIN_PARAMS_INVALID),
309 	PKCS11_ID(PKCS11_CKR_CURVE_NOT_SUPPORTED),
310 	PKCS11_ID(PKCS11_CKR_BUFFER_TOO_SMALL),
311 	PKCS11_ID(PKCS11_CKR_PIN_TOO_WEAK),
312 	PKCS11_ID(PKCS11_RV_NOT_FOUND),
313 	PKCS11_ID(PKCS11_RV_NOT_IMPLEMENTED),
314 };
315 
316 static const struct any_id __maybe_unused string_class[] = {
317 	PKCS11_ID(PKCS11_CKO_SECRET_KEY),
318 	PKCS11_ID(PKCS11_CKO_PUBLIC_KEY),
319 	PKCS11_ID(PKCS11_CKO_PRIVATE_KEY),
320 	PKCS11_ID(PKCS11_CKO_OTP_KEY),
321 	PKCS11_ID(PKCS11_CKO_CERTIFICATE),
322 	PKCS11_ID(PKCS11_CKO_DATA),
323 	PKCS11_ID(PKCS11_CKO_DOMAIN_PARAMETERS),
324 	PKCS11_ID(PKCS11_CKO_HW_FEATURE),
325 	PKCS11_ID(PKCS11_CKO_MECHANISM),
326 	PKCS11_ID(PKCS11_CKO_UNDEFINED_ID)
327 };
328 
329 static const struct any_id __maybe_unused string_key_type[] = {
330 	PKCS11_ID(PKCS11_CKK_AES),
331 	PKCS11_ID(PKCS11_CKK_GENERIC_SECRET),
332 	PKCS11_ID(PKCS11_CKK_MD5_HMAC),
333 	PKCS11_ID(PKCS11_CKK_SHA_1_HMAC),
334 	PKCS11_ID(PKCS11_CKK_SHA224_HMAC),
335 	PKCS11_ID(PKCS11_CKK_SHA256_HMAC),
336 	PKCS11_ID(PKCS11_CKK_SHA384_HMAC),
337 	PKCS11_ID(PKCS11_CKK_SHA512_HMAC),
338 	PKCS11_ID(PKCS11_CKK_EC),
339 	PKCS11_ID(PKCS11_CKK_EC_EDWARDS),
340 	PKCS11_ID(PKCS11_CKK_EDDSA),
341 	PKCS11_ID(PKCS11_CKK_RSA),
342 	PKCS11_ID(PKCS11_CKK_UNDEFINED_ID)
343 };
344 
345 static const struct any_id __maybe_unused string_certificate_type[] = {
346 	PKCS11_ID(PKCS11_CKC_X_509),
347 	PKCS11_ID(PKCS11_CKC_X_509_ATTR_CERT),
348 	PKCS11_ID(PKCS11_CKC_WTLS),
349 	PKCS11_ID(PKCS11_CKC_UNDEFINED_ID)
350 };
351 
352 /*
353  * Processing IDs not exported in the TA API.
354  * PKCS11_CKM_* mechanism IDs are looked up from mechanism_string_id().
355  */
356 static const struct any_id __maybe_unused string_internal_processing[] = {
357 	PKCS11_ID(PKCS11_PROCESSING_IMPORT),
358 };
359 
360 static const struct any_id __maybe_unused string_functions[] = {
361 	PKCS11_ID(PKCS11_FUNCTION_DIGEST),
362 	PKCS11_ID(PKCS11_FUNCTION_IMPORT),
363 	PKCS11_ID(PKCS11_FUNCTION_ENCRYPT),
364 	PKCS11_ID(PKCS11_FUNCTION_DECRYPT),
365 	PKCS11_ID(PKCS11_FUNCTION_SIGN),
366 	PKCS11_ID(PKCS11_FUNCTION_VERIFY),
367 	PKCS11_ID(PKCS11_FUNCTION_DERIVE),
368 	PKCS11_ID(PKCS11_FUNCTION_WRAP),
369 	PKCS11_ID(PKCS11_FUNCTION_UNWRAP),
370 };
371 
372 /*
373  * Conversion between PKCS11 TA and GPD TEE return codes
374  */
tee2pkcs_error(TEE_Result res)375 enum pkcs11_rc tee2pkcs_error(TEE_Result res)
376 {
377 	switch (res) {
378 	case TEE_SUCCESS:
379 		return PKCS11_CKR_OK;
380 
381 	case TEE_ERROR_BAD_PARAMETERS:
382 		return PKCS11_CKR_ARGUMENTS_BAD;
383 
384 	case TEE_ERROR_CIPHERTEXT_INVALID:
385 		return PKCS11_CKR_ENCRYPTED_DATA_INVALID;
386 
387 	case TEE_ERROR_OUT_OF_MEMORY:
388 		return PKCS11_CKR_DEVICE_MEMORY;
389 
390 	case TEE_ERROR_SHORT_BUFFER:
391 		return PKCS11_CKR_BUFFER_TOO_SMALL;
392 
393 	case TEE_ERROR_MAC_INVALID:
394 	case TEE_ERROR_SIGNATURE_INVALID:
395 		return PKCS11_CKR_SIGNATURE_INVALID;
396 
397 	default:
398 		return PKCS11_CKR_GENERAL_ERROR;
399 	}
400 }
401 
402 /*
403  * Helper functions to analyse PKCS11 identifiers
404  */
405 
406 /* Check attribute ID is known and size matches if fixed */
valid_pkcs11_attribute_id(uint32_t id,uint32_t size)407 bool valid_pkcs11_attribute_id(uint32_t id, uint32_t size)
408 {
409 	size_t n = 0;
410 
411 	for (n = 0; n < ARRAY_SIZE(attr_ids); n++)
412 		if (id == attr_ids[n].id)
413 			return !attr_ids[n].size || size == attr_ids[n].size;
414 
415 	return false;
416 }
417 
pkcs11_attr_is_type(uint32_t attribute_id)418 size_t pkcs11_attr_is_type(uint32_t attribute_id)
419 {
420 	enum pkcs11_attr_id id = attribute_id;
421 
422 	switch (id) {
423 	case PKCS11_CKA_KEY_TYPE:
424 	case PKCS11_CKA_MECHANISM_TYPE:
425 	case PKCS11_CKA_KEY_GEN_MECHANISM:
426 		return sizeof(uint32_t);
427 	default:
428 		return 0;
429 	}
430 }
431 
pkcs11_attr_has_indirect_attributes(uint32_t attribute_id)432 bool pkcs11_attr_has_indirect_attributes(uint32_t attribute_id)
433 {
434 	switch (attribute_id) {
435 	case PKCS11_CKA_WRAP_TEMPLATE:
436 	case PKCS11_CKA_UNWRAP_TEMPLATE:
437 	case PKCS11_CKA_DERIVE_TEMPLATE:
438 		return true;
439 	default:
440 		return false;
441 	}
442 }
443 
pkcs11_class_has_type(uint32_t class)444 bool pkcs11_class_has_type(uint32_t class)
445 {
446 	enum pkcs11_class_id class_id = class;
447 
448 	switch (class_id) {
449 	case PKCS11_CKO_CERTIFICATE:
450 	case PKCS11_CKO_PUBLIC_KEY:
451 	case PKCS11_CKO_PRIVATE_KEY:
452 	case PKCS11_CKO_SECRET_KEY:
453 	case PKCS11_CKO_MECHANISM:
454 	case PKCS11_CKO_HW_FEATURE:
455 		return true;
456 	default:
457 		return false;
458 	}
459 }
460 
pkcs11_attr_class_is_key(uint32_t class)461 bool pkcs11_attr_class_is_key(uint32_t class)
462 {
463 	enum pkcs11_class_id class_id = class;
464 
465 	switch (class_id) {
466 	case PKCS11_CKO_SECRET_KEY:
467 	case PKCS11_CKO_PUBLIC_KEY:
468 	case PKCS11_CKO_PRIVATE_KEY:
469 		return true;
470 	default:
471 		return false;
472 	}
473 }
474 
key_type_is_symm_key(uint32_t id)475 bool key_type_is_symm_key(uint32_t id)
476 {
477 	enum pkcs11_key_type key_type = id;
478 
479 	switch (key_type) {
480 	case PKCS11_CKK_AES:
481 	case PKCS11_CKK_GENERIC_SECRET:
482 	case PKCS11_CKK_MD5_HMAC:
483 	case PKCS11_CKK_SHA_1_HMAC:
484 	case PKCS11_CKK_SHA224_HMAC:
485 	case PKCS11_CKK_SHA256_HMAC:
486 	case PKCS11_CKK_SHA384_HMAC:
487 	case PKCS11_CKK_SHA512_HMAC:
488 		return true;
489 	default:
490 		return false;
491 	}
492 }
493 
key_type_is_asymm_key(uint32_t id)494 bool key_type_is_asymm_key(uint32_t id)
495 {
496 	enum pkcs11_key_type key_type = id;
497 
498 	switch (key_type) {
499 	case PKCS11_CKK_EC:
500 	case PKCS11_CKK_EC_EDWARDS:
501 	case PKCS11_CKK_RSA:
502 		return true;
503 	default:
504 		return false;
505 	}
506 }
507 
508 /*
509  * Returns shift position or -1 on error.
510  * Mainly used when PKCS11_SHEAD_WITH_BOOLPROPS is enabled
511  */
pkcs11_attr2boolprop_shift(uint32_t attr)512 int pkcs11_attr2boolprop_shift(uint32_t attr)
513 {
514 	static const uint32_t bpa[] = {
515 		[BPA_TOKEN]		= PKCS11_CKA_TOKEN,
516 		[BPA_PRIVATE]		= PKCS11_CKA_PRIVATE,
517 		[BPA_TRUSTED]		= PKCS11_CKA_TRUSTED,
518 		[BPA_SENSITIVE]		= PKCS11_CKA_SENSITIVE,
519 		[BPA_ENCRYPT]		= PKCS11_CKA_ENCRYPT,
520 		[BPA_DECRYPT]		= PKCS11_CKA_DECRYPT,
521 		[BPA_WRAP]		= PKCS11_CKA_WRAP,
522 		[BPA_UNWRAP]		= PKCS11_CKA_UNWRAP,
523 		[BPA_SIGN]		= PKCS11_CKA_SIGN,
524 		[BPA_SIGN_RECOVER]	= PKCS11_CKA_SIGN_RECOVER,
525 		[BPA_VERIFY]		= PKCS11_CKA_VERIFY,
526 		[BPA_VERIFY_RECOVER]	= PKCS11_CKA_VERIFY_RECOVER,
527 		[BPA_DERIVE]		= PKCS11_CKA_DERIVE,
528 		[BPA_EXTRACTABLE]	= PKCS11_CKA_EXTRACTABLE,
529 		[BPA_LOCAL]		= PKCS11_CKA_LOCAL,
530 		[BPA_NEVER_EXTRACTABLE]	= PKCS11_CKA_NEVER_EXTRACTABLE,
531 		[BPA_ALWAYS_SENSITIVE]	= PKCS11_CKA_ALWAYS_SENSITIVE,
532 		[BPA_MODIFIABLE]	= PKCS11_CKA_MODIFIABLE,
533 		[BPA_COPYABLE]		= PKCS11_CKA_COPYABLE,
534 		[BPA_DESTROYABLE]	= PKCS11_CKA_DESTROYABLE,
535 		[BPA_ALWAYS_AUTHENTICATE] = PKCS11_CKA_ALWAYS_AUTHENTICATE,
536 		[BPA_WRAP_WITH_TRUSTED] = PKCS11_CKA_WRAP_WITH_TRUSTED,
537 	};
538 	size_t pos = 0;
539 
540 	for (pos = 0; pos < ARRAY_SIZE(bpa); pos++)
541 		if (bpa[pos] == attr)
542 			return (int)pos;
543 
544 	return -1;
545 }
546 
547 /* Initialize a TEE attribute for a target PKCS11 TA attribute in an object */
pkcs2tee_load_attr(TEE_Attribute * tee_ref,uint32_t tee_id,struct pkcs11_object * obj,enum pkcs11_attr_id pkcs11_id)548 bool pkcs2tee_load_attr(TEE_Attribute *tee_ref, uint32_t tee_id,
549 			struct pkcs11_object *obj,
550 			enum pkcs11_attr_id pkcs11_id)
551 {
552 	void *a_ptr = NULL;
553 	uint8_t *der_ptr = NULL;
554 	uint32_t a_size = 0;
555 	uint32_t data32 = 0;
556 	size_t hsize = 0;
557 	size_t qsize = 0;
558 
559 	switch (tee_id) {
560 	case TEE_ATTR_ECC_PUBLIC_VALUE_X:
561 	case TEE_ATTR_ECC_PUBLIC_VALUE_Y:
562 	case TEE_ATTR_ECC_CURVE:
563 		if (get_attribute_ptr(obj->attributes, PKCS11_CKA_EC_PARAMS,
564 				      &a_ptr, &a_size) || !a_ptr) {
565 			EMSG("Missing EC_PARAMS attribute");
566 			return false;
567 		}
568 
569 		if (tee_id == TEE_ATTR_ECC_CURVE) {
570 			data32 = ec_params2tee_curve(a_ptr, a_size);
571 			TEE_InitValueAttribute(tee_ref, TEE_ATTR_ECC_CURVE,
572 					       data32, 0);
573 			return true;
574 		}
575 
576 		data32 = (ec_params2tee_keysize(a_ptr, a_size) + 7) / 8;
577 
578 		if (get_attribute_ptr(obj->attributes, PKCS11_CKA_EC_POINT,
579 				      &a_ptr, &a_size)) {
580 			/*
581 			 * Public X/Y is required for both TEE keypair and
582 			 * public key, so abort if EC_POINT is not provided
583 			 * during object import.
584 			 */
585 
586 			EMSG("Missing EC_POINT attribute");
587 			return false;
588 		}
589 
590 		der_ptr = (uint8_t *)a_ptr;
591 
592 		if (der_ptr[0] != 0x04) {
593 			EMSG("Unsupported DER type");
594 			return false;
595 		}
596 
597 		if ((der_ptr[1] & 0x80) == 0) {
598 			/* DER short definitive form up to 127 bytes */
599 			qsize = der_ptr[1] & 0x7F;
600 			hsize = 2 /* der */ + 1 /* point compression */;
601 		} else if (der_ptr[1] == 0x81) {
602 			/* DER long definitive form up to 255 bytes */
603 			qsize = der_ptr[2];
604 			hsize = 3 /* der */ + 1 /* point compression */;
605 		} else {
606 			EMSG("Unsupported DER long form");
607 			return false;
608 		}
609 
610 		if (der_ptr[hsize - 1] != 0x04) {
611 			EMSG("Unsupported EC_POINT compression");
612 			return false;
613 		}
614 
615 		if (a_size != (hsize - 1) + qsize) {
616 			EMSG("Invalid EC_POINT attribute");
617 			return false;
618 		}
619 
620 		if (a_size != hsize + 2 * data32) {
621 			EMSG("Invalid EC_POINT attribute");
622 			return false;
623 		}
624 
625 		if (tee_id == TEE_ATTR_ECC_PUBLIC_VALUE_X)
626 			TEE_InitRefAttribute(tee_ref, tee_id,
627 					     der_ptr + hsize, data32);
628 		else
629 			TEE_InitRefAttribute(tee_ref, tee_id,
630 					     der_ptr + hsize + data32,
631 					     data32);
632 
633 		return true;
634 
635 	default:
636 		break;
637 	}
638 
639 	if (get_attribute_ptr(obj->attributes, pkcs11_id, &a_ptr, &a_size))
640 		return false;
641 
642 	TEE_InitRefAttribute(tee_ref, tee_id, a_ptr, a_size);
643 
644 	return true;
645 }
646 
647 /*
648  * Initialize a TEE attribute with hash of a target PKCS11 TA attribute
649  * in an object.
650  */
pkcs2tee_load_hashed_attr(TEE_Attribute * tee_ref,uint32_t tee_id,struct pkcs11_object * obj,enum pkcs11_attr_id pkcs11_id,uint32_t tee_algo,void * hash_ptr,uint32_t * hash_size)651 enum pkcs11_rc pkcs2tee_load_hashed_attr(TEE_Attribute *tee_ref,
652 					 uint32_t tee_id,
653 					 struct pkcs11_object *obj,
654 					 enum pkcs11_attr_id pkcs11_id,
655 					 uint32_t tee_algo, void *hash_ptr,
656 					 uint32_t *hash_size)
657 {
658 	TEE_OperationHandle handle = TEE_HANDLE_NULL;
659 	void *a_ptr = NULL;
660 	uint32_t a_size = 0;
661 	enum pkcs11_rc rc = PKCS11_CKR_OK;
662 	TEE_Result res = TEE_ERROR_GENERIC;
663 
664 	rc = get_attribute_ptr(obj->attributes, pkcs11_id, &a_ptr, &a_size);
665 	if (rc)
666 		return rc;
667 
668 	res = TEE_AllocateOperation(&handle, tee_algo, TEE_MODE_DIGEST, 0);
669 	if (res) {
670 		EMSG("TEE_AllocateOperation() failed %#"PRIx32, tee_algo);
671 		return tee2pkcs_error(res);
672 	}
673 
674 	res = TEE_DigestDoFinal(handle, a_ptr, a_size, hash_ptr, hash_size);
675 	TEE_FreeOperation(handle);
676 	if (res) {
677 		EMSG("TEE_DigestDoFinal() failed %#"PRIx32, tee_algo);
678 		return PKCS11_CKR_FUNCTION_FAILED;
679 	}
680 
681 	TEE_InitRefAttribute(tee_ref, tee_id, hash_ptr, *hash_size);
682 
683 	return PKCS11_CKR_OK;
684 }
685 
686 /* Easy conversion between PKCS11 TA function of TEE crypto mode */
pkcs2tee_mode(uint32_t * tee_id,enum processing_func function)687 void pkcs2tee_mode(uint32_t *tee_id, enum processing_func function)
688 {
689 	switch (function) {
690 	case PKCS11_FUNCTION_ENCRYPT:
691 		*tee_id = TEE_MODE_ENCRYPT;
692 		break;
693 	case PKCS11_FUNCTION_DECRYPT:
694 		*tee_id = TEE_MODE_DECRYPT;
695 		break;
696 	case PKCS11_FUNCTION_SIGN:
697 		*tee_id = TEE_MODE_SIGN;
698 		break;
699 	case PKCS11_FUNCTION_VERIFY:
700 		*tee_id = TEE_MODE_VERIFY;
701 		break;
702 	case PKCS11_FUNCTION_DERIVE:
703 		*tee_id = TEE_MODE_DERIVE;
704 		break;
705 	case PKCS11_FUNCTION_DIGEST:
706 		*tee_id = TEE_MODE_DIGEST;
707 		break;
708 	default:
709 		TEE_Panic(function);
710 	}
711 }
712 
713 #if CFG_TEE_TA_LOG_LEVEL > 0
id2str_rc(uint32_t id)714 const char *id2str_rc(uint32_t id)
715 {
716 	return ID2STR(id, string_rc, "PKCS11_CKR_");
717 }
718 
id2str_ta_cmd(uint32_t id)719 const char *id2str_ta_cmd(uint32_t id)
720 {
721 	return ID2STR(id, string_ta_cmd, NULL);
722 }
723 
id2str_slot_flag(uint32_t id)724 const char *id2str_slot_flag(uint32_t id)
725 {
726 	return ID2STR(id, string_slot_flags, "PKCS11_CKFS_");
727 }
728 
id2str_token_flag(uint32_t id)729 const char *id2str_token_flag(uint32_t id)
730 {
731 	return ID2STR(id, string_token_flags, "PKCS11_CKFT_");
732 }
733 
id2str_session_flag(uint32_t id)734 const char *id2str_session_flag(uint32_t id)
735 {
736 	return ID2STR(id, string_session_flags, "PKCS11_CKFSS_");
737 }
738 
id2str_session_state(uint32_t id)739 const char *id2str_session_state(uint32_t id)
740 {
741 	return ID2STR(id, string_session_state, "PKCS11_CKS_");
742 }
743 
id2str_attr(uint32_t id)744 const char *id2str_attr(uint32_t id)
745 {
746 	size_t n = 0;
747 
748 	for (n = 0; n < ARRAY_SIZE(attr_ids); n++) {
749 		if (id == attr_ids[n].id) {
750 			/* Skip PKCS11_CKA_ prefix */
751 			return attr_ids[n].string + strlen("PKCS11_CKA_");
752 		}
753 	}
754 
755 	return unknown;
756 }
757 
id2str_class(uint32_t id)758 const char *id2str_class(uint32_t id)
759 {
760 	return ID2STR(id, string_class, "PKCS11_CKO_");
761 }
762 
id2str_type(uint32_t id,uint32_t class)763 const char *id2str_type(uint32_t id, uint32_t class)
764 {
765 	enum pkcs11_class_id class_id = class;
766 	enum pkcs11_key_type key_type = id;
767 
768 	switch (class_id) {
769 	case PKCS11_CKO_SECRET_KEY:
770 	case PKCS11_CKO_PUBLIC_KEY:
771 	case PKCS11_CKO_PRIVATE_KEY:
772 		return id2str_key_type(key_type);
773 	default:
774 		return unknown;
775 	}
776 }
777 
id2str_key_type(uint32_t id)778 const char *id2str_key_type(uint32_t id)
779 {
780 	return ID2STR(id, string_key_type, "PKCS11_CKK_");
781 }
782 
id2str_certificate_type(uint32_t id)783 const char *id2str_certificate_type(uint32_t id)
784 {
785 	return ID2STR(id, string_certificate_type, "PKCS11_CKC_");
786 }
787 
id2str_attr_value(uint32_t id,size_t size,void * value)788 const char *id2str_attr_value(uint32_t id, size_t size, void *value)
789 {
790 	static const char str_true[] = "TRUE";
791 	static const char str_false[] = "FALSE";
792 	static const char str_unknown[] = "*";
793 	uint32_t type = 0;
794 
795 	if (pkcs11_attr2boolprop_shift(id) >= 0)
796 		return *(uint8_t *)value ? str_true : str_false;
797 
798 	if (size < sizeof(uint32_t))
799 		return str_unknown;
800 
801 	TEE_MemMove(&type, value, sizeof(uint32_t));
802 
803 	switch (id) {
804 	case PKCS11_CKA_CLASS:
805 		return id2str_class(type);
806 	case PKCS11_CKA_KEY_TYPE:
807 		return id2str_key_type(type);
808 	case PKCS11_CKA_MECHANISM_TYPE:
809 		return id2str_mechanism(type);
810 	default:
811 		return str_unknown;
812 	}
813 }
814 
id2str_proc(uint32_t id)815 const char *id2str_proc(uint32_t id)
816 {
817 	const char *str = ID2STR(id, string_internal_processing,
818 				 "PKCS11_PROCESSING_");
819 
820 	if (str != unknown)
821 		return str;
822 
823 	return id2str_mechanism(id);
824 }
825 
id2str_function(uint32_t id)826 const char *id2str_function(uint32_t id)
827 {
828 	return ID2STR(id, string_functions, "PKCS11_FUNCTION_");
829 }
830 #endif /*CFG_TEE_TA_LOG_LEVEL*/
831