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_RSA),
340 	PKCS11_ID(PKCS11_CKK_UNDEFINED_ID)
341 };
342 
343 static const struct any_id __maybe_unused string_certificate_type[] = {
344 	PKCS11_ID(PKCS11_CKC_X_509),
345 	PKCS11_ID(PKCS11_CKC_X_509_ATTR_CERT),
346 	PKCS11_ID(PKCS11_CKC_WTLS),
347 	PKCS11_ID(PKCS11_CKC_UNDEFINED_ID)
348 };
349 
350 /*
351  * Processing IDs not exported in the TA API.
352  * PKCS11_CKM_* mechanism IDs are looked up from mechanism_string_id().
353  */
354 static const struct any_id __maybe_unused string_internal_processing[] = {
355 	PKCS11_ID(PKCS11_PROCESSING_IMPORT),
356 };
357 
358 static const struct any_id __maybe_unused string_functions[] = {
359 	PKCS11_ID(PKCS11_FUNCTION_DIGEST),
360 	PKCS11_ID(PKCS11_FUNCTION_IMPORT),
361 	PKCS11_ID(PKCS11_FUNCTION_ENCRYPT),
362 	PKCS11_ID(PKCS11_FUNCTION_DECRYPT),
363 	PKCS11_ID(PKCS11_FUNCTION_SIGN),
364 	PKCS11_ID(PKCS11_FUNCTION_VERIFY),
365 	PKCS11_ID(PKCS11_FUNCTION_DERIVE),
366 	PKCS11_ID(PKCS11_FUNCTION_WRAP),
367 	PKCS11_ID(PKCS11_FUNCTION_UNWRAP),
368 };
369 
370 /*
371  * Conversion between PKCS11 TA and GPD TEE return codes
372  */
tee2pkcs_error(TEE_Result res)373 enum pkcs11_rc tee2pkcs_error(TEE_Result res)
374 {
375 	switch (res) {
376 	case TEE_SUCCESS:
377 		return PKCS11_CKR_OK;
378 
379 	case TEE_ERROR_BAD_PARAMETERS:
380 		return PKCS11_CKR_ARGUMENTS_BAD;
381 
382 	case TEE_ERROR_CIPHERTEXT_INVALID:
383 		return PKCS11_CKR_ENCRYPTED_DATA_INVALID;
384 
385 	case TEE_ERROR_OUT_OF_MEMORY:
386 		return PKCS11_CKR_DEVICE_MEMORY;
387 
388 	case TEE_ERROR_SHORT_BUFFER:
389 		return PKCS11_CKR_BUFFER_TOO_SMALL;
390 
391 	case TEE_ERROR_MAC_INVALID:
392 	case TEE_ERROR_SIGNATURE_INVALID:
393 		return PKCS11_CKR_SIGNATURE_INVALID;
394 
395 	default:
396 		return PKCS11_CKR_GENERAL_ERROR;
397 	}
398 }
399 
400 /*
401  * Helper functions to analyse PKCS11 identifiers
402  */
403 
404 /* Check attribute ID is known and size matches if fixed */
valid_pkcs11_attribute_id(uint32_t id,uint32_t size)405 bool valid_pkcs11_attribute_id(uint32_t id, uint32_t size)
406 {
407 	size_t n = 0;
408 
409 	for (n = 0; n < ARRAY_SIZE(attr_ids); n++)
410 		if (id == attr_ids[n].id)
411 			return !attr_ids[n].size || size == attr_ids[n].size;
412 
413 	return false;
414 }
415 
pkcs11_attr_is_type(uint32_t attribute_id)416 size_t pkcs11_attr_is_type(uint32_t attribute_id)
417 {
418 	enum pkcs11_attr_id id = attribute_id;
419 
420 	switch (id) {
421 	case PKCS11_CKA_KEY_TYPE:
422 	case PKCS11_CKA_MECHANISM_TYPE:
423 	case PKCS11_CKA_KEY_GEN_MECHANISM:
424 		return sizeof(uint32_t);
425 	default:
426 		return 0;
427 	}
428 }
429 
pkcs11_attr_has_indirect_attributes(uint32_t attribute_id)430 bool pkcs11_attr_has_indirect_attributes(uint32_t attribute_id)
431 {
432 	switch (attribute_id) {
433 	case PKCS11_CKA_WRAP_TEMPLATE:
434 	case PKCS11_CKA_UNWRAP_TEMPLATE:
435 	case PKCS11_CKA_DERIVE_TEMPLATE:
436 		return true;
437 	default:
438 		return false;
439 	}
440 }
441 
pkcs11_class_has_type(uint32_t class)442 bool pkcs11_class_has_type(uint32_t class)
443 {
444 	enum pkcs11_class_id class_id = class;
445 
446 	switch (class_id) {
447 	case PKCS11_CKO_CERTIFICATE:
448 	case PKCS11_CKO_PUBLIC_KEY:
449 	case PKCS11_CKO_PRIVATE_KEY:
450 	case PKCS11_CKO_SECRET_KEY:
451 	case PKCS11_CKO_MECHANISM:
452 	case PKCS11_CKO_HW_FEATURE:
453 		return true;
454 	default:
455 		return false;
456 	}
457 }
458 
pkcs11_attr_class_is_key(uint32_t class)459 bool pkcs11_attr_class_is_key(uint32_t class)
460 {
461 	enum pkcs11_class_id class_id = class;
462 
463 	switch (class_id) {
464 	case PKCS11_CKO_SECRET_KEY:
465 	case PKCS11_CKO_PUBLIC_KEY:
466 	case PKCS11_CKO_PRIVATE_KEY:
467 		return true;
468 	default:
469 		return false;
470 	}
471 }
472 
key_type_is_symm_key(uint32_t id)473 bool key_type_is_symm_key(uint32_t id)
474 {
475 	enum pkcs11_key_type key_type = id;
476 
477 	switch (key_type) {
478 	case PKCS11_CKK_AES:
479 	case PKCS11_CKK_GENERIC_SECRET:
480 	case PKCS11_CKK_MD5_HMAC:
481 	case PKCS11_CKK_SHA_1_HMAC:
482 	case PKCS11_CKK_SHA224_HMAC:
483 	case PKCS11_CKK_SHA256_HMAC:
484 	case PKCS11_CKK_SHA384_HMAC:
485 	case PKCS11_CKK_SHA512_HMAC:
486 		return true;
487 	default:
488 		return false;
489 	}
490 }
491 
key_type_is_asymm_key(uint32_t id)492 bool key_type_is_asymm_key(uint32_t id)
493 {
494 	enum pkcs11_key_type key_type = id;
495 
496 	switch (key_type) {
497 	case PKCS11_CKK_EC:
498 	case PKCS11_CKK_RSA:
499 		return true;
500 	default:
501 		return false;
502 	}
503 }
504 
505 /*
506  * Returns shift position or -1 on error.
507  * Mainly used when PKCS11_SHEAD_WITH_BOOLPROPS is enabled
508  */
pkcs11_attr2boolprop_shift(uint32_t attr)509 int pkcs11_attr2boolprop_shift(uint32_t attr)
510 {
511 	static const uint32_t bpa[] = {
512 		[BPA_TOKEN]		= PKCS11_CKA_TOKEN,
513 		[BPA_PRIVATE]		= PKCS11_CKA_PRIVATE,
514 		[BPA_TRUSTED]		= PKCS11_CKA_TRUSTED,
515 		[BPA_SENSITIVE]		= PKCS11_CKA_SENSITIVE,
516 		[BPA_ENCRYPT]		= PKCS11_CKA_ENCRYPT,
517 		[BPA_DECRYPT]		= PKCS11_CKA_DECRYPT,
518 		[BPA_WRAP]		= PKCS11_CKA_WRAP,
519 		[BPA_UNWRAP]		= PKCS11_CKA_UNWRAP,
520 		[BPA_SIGN]		= PKCS11_CKA_SIGN,
521 		[BPA_SIGN_RECOVER]	= PKCS11_CKA_SIGN_RECOVER,
522 		[BPA_VERIFY]		= PKCS11_CKA_VERIFY,
523 		[BPA_VERIFY_RECOVER]	= PKCS11_CKA_VERIFY_RECOVER,
524 		[BPA_DERIVE]		= PKCS11_CKA_DERIVE,
525 		[BPA_EXTRACTABLE]	= PKCS11_CKA_EXTRACTABLE,
526 		[BPA_LOCAL]		= PKCS11_CKA_LOCAL,
527 		[BPA_NEVER_EXTRACTABLE]	= PKCS11_CKA_NEVER_EXTRACTABLE,
528 		[BPA_ALWAYS_SENSITIVE]	= PKCS11_CKA_ALWAYS_SENSITIVE,
529 		[BPA_MODIFIABLE]	= PKCS11_CKA_MODIFIABLE,
530 		[BPA_COPYABLE]		= PKCS11_CKA_COPYABLE,
531 		[BPA_DESTROYABLE]	= PKCS11_CKA_DESTROYABLE,
532 		[BPA_ALWAYS_AUTHENTICATE] = PKCS11_CKA_ALWAYS_AUTHENTICATE,
533 		[BPA_WRAP_WITH_TRUSTED] = PKCS11_CKA_WRAP_WITH_TRUSTED,
534 	};
535 	size_t pos = 0;
536 
537 	for (pos = 0; pos < ARRAY_SIZE(bpa); pos++)
538 		if (bpa[pos] == attr)
539 			return (int)pos;
540 
541 	return -1;
542 }
543 
544 /* 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)545 bool pkcs2tee_load_attr(TEE_Attribute *tee_ref, uint32_t tee_id,
546 			struct pkcs11_object *obj,
547 			enum pkcs11_attr_id pkcs11_id)
548 {
549 	void *a_ptr = NULL;
550 	uint8_t *der_ptr = NULL;
551 	uint32_t a_size = 0;
552 	uint32_t data32 = 0;
553 	size_t hsize = 0;
554 	size_t qsize = 0;
555 
556 	switch (tee_id) {
557 	case TEE_ATTR_ECC_PUBLIC_VALUE_X:
558 	case TEE_ATTR_ECC_PUBLIC_VALUE_Y:
559 	case TEE_ATTR_ECC_CURVE:
560 		if (get_attribute_ptr(obj->attributes, PKCS11_CKA_EC_PARAMS,
561 				      &a_ptr, &a_size) || !a_ptr) {
562 			EMSG("Missing EC_PARAMS attribute");
563 			return false;
564 		}
565 
566 		if (tee_id == TEE_ATTR_ECC_CURVE) {
567 			data32 = ec_params2tee_curve(a_ptr, a_size);
568 			TEE_InitValueAttribute(tee_ref, TEE_ATTR_ECC_CURVE,
569 					       data32, 0);
570 			return true;
571 		}
572 
573 		data32 = (ec_params2tee_keysize(a_ptr, a_size) + 7) / 8;
574 
575 		if (get_attribute_ptr(obj->attributes, PKCS11_CKA_EC_POINT,
576 				      &a_ptr, &a_size)) {
577 			/*
578 			 * Public X/Y is required for both TEE keypair and
579 			 * public key, so abort if EC_POINT is not provided
580 			 * during object import.
581 			 */
582 
583 			EMSG("Missing EC_POINT attribute");
584 			return false;
585 		}
586 
587 		der_ptr = (uint8_t *)a_ptr;
588 
589 		if (der_ptr[0] != 0x04) {
590 			EMSG("Unsupported DER type");
591 			return false;
592 		}
593 
594 		if ((der_ptr[1] & 0x80) == 0) {
595 			/* DER short definitive form up to 127 bytes */
596 			qsize = der_ptr[1] & 0x7F;
597 			hsize = 2 /* der */ + 1 /* point compression */;
598 		} else if (der_ptr[1] == 0x81) {
599 			/* DER long definitive form up to 255 bytes */
600 			qsize = der_ptr[2];
601 			hsize = 3 /* der */ + 1 /* point compression */;
602 		} else {
603 			EMSG("Unsupported DER long form");
604 			return false;
605 		}
606 
607 		if (der_ptr[hsize - 1] != 0x04) {
608 			EMSG("Unsupported EC_POINT compression");
609 			return false;
610 		}
611 
612 		if (a_size != (hsize - 1) + qsize) {
613 			EMSG("Invalid EC_POINT attribute");
614 			return false;
615 		}
616 
617 		if (a_size != hsize + 2 * data32) {
618 			EMSG("Invalid EC_POINT attribute");
619 			return false;
620 		}
621 
622 		if (tee_id == TEE_ATTR_ECC_PUBLIC_VALUE_X)
623 			TEE_InitRefAttribute(tee_ref, tee_id,
624 					     der_ptr + hsize, data32);
625 		else
626 			TEE_InitRefAttribute(tee_ref, tee_id,
627 					     der_ptr + hsize + data32,
628 					     data32);
629 
630 		return true;
631 
632 	default:
633 		break;
634 	}
635 
636 	if (get_attribute_ptr(obj->attributes, pkcs11_id, &a_ptr, &a_size))
637 		return false;
638 
639 	TEE_InitRefAttribute(tee_ref, tee_id, a_ptr, a_size);
640 
641 	return true;
642 }
643 
644 /*
645  * Initialize a TEE attribute with hash of a target PKCS11 TA attribute
646  * in an object.
647  */
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)648 enum pkcs11_rc pkcs2tee_load_hashed_attr(TEE_Attribute *tee_ref,
649 					 uint32_t tee_id,
650 					 struct pkcs11_object *obj,
651 					 enum pkcs11_attr_id pkcs11_id,
652 					 uint32_t tee_algo, void *hash_ptr,
653 					 uint32_t *hash_size)
654 {
655 	TEE_OperationHandle handle = TEE_HANDLE_NULL;
656 	void *a_ptr = NULL;
657 	uint32_t a_size = 0;
658 	enum pkcs11_rc rc = PKCS11_CKR_OK;
659 	TEE_Result res = TEE_ERROR_GENERIC;
660 
661 	rc = get_attribute_ptr(obj->attributes, pkcs11_id, &a_ptr, &a_size);
662 	if (rc)
663 		return rc;
664 
665 	res = TEE_AllocateOperation(&handle, tee_algo, TEE_MODE_DIGEST, 0);
666 	if (res) {
667 		EMSG("TEE_AllocateOperation() failed %#"PRIx32, tee_algo);
668 		return tee2pkcs_error(res);
669 	}
670 
671 	res = TEE_DigestDoFinal(handle, a_ptr, a_size, hash_ptr, hash_size);
672 	TEE_FreeOperation(handle);
673 	if (res) {
674 		EMSG("TEE_DigestDoFinal() failed %#"PRIx32, tee_algo);
675 		return PKCS11_CKR_FUNCTION_FAILED;
676 	}
677 
678 	TEE_InitRefAttribute(tee_ref, tee_id, hash_ptr, *hash_size);
679 
680 	return PKCS11_CKR_OK;
681 }
682 
683 /* Easy conversion between PKCS11 TA function of TEE crypto mode */
pkcs2tee_mode(uint32_t * tee_id,enum processing_func function)684 void pkcs2tee_mode(uint32_t *tee_id, enum processing_func function)
685 {
686 	switch (function) {
687 	case PKCS11_FUNCTION_ENCRYPT:
688 		*tee_id = TEE_MODE_ENCRYPT;
689 		break;
690 	case PKCS11_FUNCTION_DECRYPT:
691 		*tee_id = TEE_MODE_DECRYPT;
692 		break;
693 	case PKCS11_FUNCTION_SIGN:
694 		*tee_id = TEE_MODE_SIGN;
695 		break;
696 	case PKCS11_FUNCTION_VERIFY:
697 		*tee_id = TEE_MODE_VERIFY;
698 		break;
699 	case PKCS11_FUNCTION_DERIVE:
700 		*tee_id = TEE_MODE_DERIVE;
701 		break;
702 	case PKCS11_FUNCTION_DIGEST:
703 		*tee_id = TEE_MODE_DIGEST;
704 		break;
705 	default:
706 		TEE_Panic(function);
707 	}
708 }
709 
710 #if CFG_TEE_TA_LOG_LEVEL > 0
id2str_rc(uint32_t id)711 const char *id2str_rc(uint32_t id)
712 {
713 	return ID2STR(id, string_rc, "PKCS11_CKR_");
714 }
715 
id2str_ta_cmd(uint32_t id)716 const char *id2str_ta_cmd(uint32_t id)
717 {
718 	return ID2STR(id, string_ta_cmd, NULL);
719 }
720 
id2str_slot_flag(uint32_t id)721 const char *id2str_slot_flag(uint32_t id)
722 {
723 	return ID2STR(id, string_slot_flags, "PKCS11_CKFS_");
724 }
725 
id2str_token_flag(uint32_t id)726 const char *id2str_token_flag(uint32_t id)
727 {
728 	return ID2STR(id, string_token_flags, "PKCS11_CKFT_");
729 }
730 
id2str_session_flag(uint32_t id)731 const char *id2str_session_flag(uint32_t id)
732 {
733 	return ID2STR(id, string_session_flags, "PKCS11_CKFSS_");
734 }
735 
id2str_session_state(uint32_t id)736 const char *id2str_session_state(uint32_t id)
737 {
738 	return ID2STR(id, string_session_state, "PKCS11_CKS_");
739 }
740 
id2str_attr(uint32_t id)741 const char *id2str_attr(uint32_t id)
742 {
743 	size_t n = 0;
744 
745 	for (n = 0; n < ARRAY_SIZE(attr_ids); n++) {
746 		if (id == attr_ids[n].id) {
747 			/* Skip PKCS11_CKA_ prefix */
748 			return attr_ids[n].string + strlen("PKCS11_CKA_");
749 		}
750 	}
751 
752 	return unknown;
753 }
754 
id2str_class(uint32_t id)755 const char *id2str_class(uint32_t id)
756 {
757 	return ID2STR(id, string_class, "PKCS11_CKO_");
758 }
759 
id2str_type(uint32_t id,uint32_t class)760 const char *id2str_type(uint32_t id, uint32_t class)
761 {
762 	enum pkcs11_class_id class_id = class;
763 	enum pkcs11_key_type key_type = id;
764 
765 	switch (class_id) {
766 	case PKCS11_CKO_SECRET_KEY:
767 	case PKCS11_CKO_PUBLIC_KEY:
768 	case PKCS11_CKO_PRIVATE_KEY:
769 		return id2str_key_type(key_type);
770 	default:
771 		return unknown;
772 	}
773 }
774 
id2str_key_type(uint32_t id)775 const char *id2str_key_type(uint32_t id)
776 {
777 	return ID2STR(id, string_key_type, "PKCS11_CKK_");
778 }
779 
id2str_certificate_type(uint32_t id)780 const char *id2str_certificate_type(uint32_t id)
781 {
782 	return ID2STR(id, string_certificate_type, "PKCS11_CKC_");
783 }
784 
id2str_attr_value(uint32_t id,size_t size,void * value)785 const char *id2str_attr_value(uint32_t id, size_t size, void *value)
786 {
787 	static const char str_true[] = "TRUE";
788 	static const char str_false[] = "FALSE";
789 	static const char str_unknown[] = "*";
790 	uint32_t type = 0;
791 
792 	if (pkcs11_attr2boolprop_shift(id) >= 0)
793 		return *(uint8_t *)value ? str_true : str_false;
794 
795 	if (size < sizeof(uint32_t))
796 		return str_unknown;
797 
798 	TEE_MemMove(&type, value, sizeof(uint32_t));
799 
800 	switch (id) {
801 	case PKCS11_CKA_CLASS:
802 		return id2str_class(type);
803 	case PKCS11_CKA_KEY_TYPE:
804 		return id2str_key_type(type);
805 	case PKCS11_CKA_MECHANISM_TYPE:
806 		return id2str_mechanism(type);
807 	default:
808 		return str_unknown;
809 	}
810 }
811 
id2str_proc(uint32_t id)812 const char *id2str_proc(uint32_t id)
813 {
814 	const char *str = ID2STR(id, string_internal_processing,
815 				 "PKCS11_PROCESSING_");
816 
817 	if (str != unknown)
818 		return str;
819 
820 	return id2str_mechanism(id);
821 }
822 
id2str_function(uint32_t id)823 const char *id2str_function(uint32_t id)
824 {
825 	return ID2STR(id, string_functions, "PKCS11_FUNCTION_");
826 }
827 #endif /*CFG_TEE_TA_LOG_LEVEL*/
828