1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2020, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <pkcs11_ta.h> 8 #include <string.h> 9 #include <tee_api_defines.h> 10 #include <tee_internal_api.h> 11 #include <tee_internal_api_extensions.h> 12 #include <util.h> 13 14 #include "attributes.h" 15 #include "object.h" 16 #include "pkcs11_attributes.h" 17 #include "pkcs11_helpers.h" 18 #include "pkcs11_token.h" 19 #include "processing.h" 20 #include "serializer.h" 21 22 static enum pkcs11_rc get_ready_session(struct pkcs11_session *session) 23 { 24 if (session_is_active(session)) 25 return PKCS11_CKR_OPERATION_ACTIVE; 26 27 return PKCS11_CKR_OK; 28 } 29 30 static bool func_matches_state(enum processing_func function, 31 enum pkcs11_proc_state state) 32 { 33 switch (function) { 34 case PKCS11_FUNCTION_ENCRYPT: 35 return state == PKCS11_SESSION_ENCRYPTING || 36 state == PKCS11_SESSION_DIGESTING_ENCRYPTING || 37 state == PKCS11_SESSION_SIGNING_ENCRYPTING; 38 case PKCS11_FUNCTION_DECRYPT: 39 return state == PKCS11_SESSION_DECRYPTING || 40 state == PKCS11_SESSION_DECRYPTING_DIGESTING || 41 state == PKCS11_SESSION_DECRYPTING_VERIFYING; 42 case PKCS11_FUNCTION_DIGEST: 43 return state == PKCS11_SESSION_DIGESTING || 44 state == PKCS11_SESSION_DIGESTING_ENCRYPTING; 45 case PKCS11_FUNCTION_SIGN: 46 return state == PKCS11_SESSION_SIGNING || 47 state == PKCS11_SESSION_SIGNING_ENCRYPTING; 48 case PKCS11_FUNCTION_VERIFY: 49 return state == PKCS11_SESSION_VERIFYING || 50 state == PKCS11_SESSION_DECRYPTING_VERIFYING; 51 case PKCS11_FUNCTION_SIGN_RECOVER: 52 return state == PKCS11_SESSION_SIGNING_RECOVER; 53 case PKCS11_FUNCTION_VERIFY_RECOVER: 54 return state == PKCS11_SESSION_SIGNING_RECOVER; 55 default: 56 TEE_Panic(function); 57 return false; 58 } 59 } 60 61 static enum pkcs11_rc get_active_session(struct pkcs11_session *session, 62 enum processing_func function) 63 { 64 enum pkcs11_rc rc = PKCS11_CKR_OPERATION_NOT_INITIALIZED; 65 66 if (session->processing && 67 func_matches_state(function, session->processing->state)) 68 rc = PKCS11_CKR_OK; 69 70 return rc; 71 } 72 73 void release_active_processing(struct pkcs11_session *session) 74 { 75 if (!session->processing) 76 return; 77 78 if (session->processing->tee_op_handle != TEE_HANDLE_NULL) { 79 TEE_FreeOperation(session->processing->tee_op_handle); 80 session->processing->tee_op_handle = TEE_HANDLE_NULL; 81 } 82 83 TEE_Free(session->processing->extra_ctx); 84 85 TEE_Free(session->processing); 86 session->processing = NULL; 87 } 88 89 size_t get_object_key_bit_size(struct pkcs11_object *obj) 90 { 91 uint32_t a_size = 0; 92 struct obj_attrs *attrs = obj->attributes; 93 94 switch (get_key_type(attrs)) { 95 case PKCS11_CKK_AES: 96 case PKCS11_CKK_GENERIC_SECRET: 97 case PKCS11_CKK_MD5_HMAC: 98 case PKCS11_CKK_SHA_1_HMAC: 99 case PKCS11_CKK_SHA224_HMAC: 100 case PKCS11_CKK_SHA256_HMAC: 101 case PKCS11_CKK_SHA384_HMAC: 102 case PKCS11_CKK_SHA512_HMAC: 103 if (get_attribute_ptr(attrs, PKCS11_CKA_VALUE, NULL, &a_size)) 104 return 0; 105 106 return a_size * 8; 107 default: 108 TEE_Panic(0); 109 return 0; 110 } 111 } 112 113 static enum pkcs11_rc generate_random_key_value(struct obj_attrs **head) 114 { 115 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 116 void *data = NULL; 117 uint32_t data_size = 0; 118 uint32_t value_len = 0; 119 void *value = NULL; 120 121 if (!*head) 122 return PKCS11_CKR_TEMPLATE_INCONSISTENT; 123 124 rc = get_attribute_ptr(*head, PKCS11_CKA_VALUE_LEN, &data, &data_size); 125 if (rc || data_size != sizeof(uint32_t)) { 126 DMSG("%s", rc ? "No attribute value_len found" : 127 "Invalid size for attribute VALUE_LEN"); 128 129 return PKCS11_CKR_ATTRIBUTE_VALUE_INVALID; 130 } 131 TEE_MemMove(&value_len, data, data_size); 132 133 if (get_key_type(*head) == PKCS11_CKK_GENERIC_SECRET) 134 value_len = (value_len + 7) / 8; 135 136 /* Remove the default empty value attribute if found */ 137 rc = remove_empty_attribute(head, PKCS11_CKA_VALUE); 138 if (rc != PKCS11_CKR_OK && rc != PKCS11_RV_NOT_FOUND) 139 return PKCS11_CKR_GENERAL_ERROR; 140 141 value = TEE_Malloc(value_len, TEE_USER_MEM_HINT_NO_FILL_ZERO); 142 if (!value) 143 return PKCS11_CKR_DEVICE_MEMORY; 144 145 TEE_GenerateRandom(value, value_len); 146 147 rc = add_attribute(head, PKCS11_CKA_VALUE, value, value_len); 148 149 TEE_Free(value); 150 151 return rc; 152 } 153 154 enum pkcs11_rc entry_generate_secret(struct pkcs11_client *client, 155 uint32_t ptypes, TEE_Param *params) 156 { 157 const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT, 158 TEE_PARAM_TYPE_NONE, 159 TEE_PARAM_TYPE_MEMREF_OUTPUT, 160 TEE_PARAM_TYPE_NONE); 161 TEE_Param *ctrl = params; 162 TEE_Param *out = params + 2; 163 enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR; 164 struct serialargs ctrlargs = { }; 165 struct pkcs11_session *session = NULL; 166 struct pkcs11_attribute_head *proc_params = NULL; 167 struct obj_attrs *head = NULL; 168 struct pkcs11_object_head *template = NULL; 169 size_t template_size = 0; 170 uint32_t obj_handle = 0; 171 172 if (!client || ptypes != exp_pt || 173 out->memref.size != sizeof(obj_handle)) 174 return PKCS11_CKR_ARGUMENTS_BAD; 175 176 serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size); 177 178 rc = serialargs_get_session_from_handle(&ctrlargs, client, &session); 179 if (rc) 180 return rc; 181 182 rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params); 183 if (rc) 184 goto out; 185 186 rc = serialargs_alloc_get_attributes(&ctrlargs, &template); 187 if (rc) 188 goto out; 189 190 if (serialargs_remaining_bytes(&ctrlargs)) { 191 rc = PKCS11_CKR_ARGUMENTS_BAD; 192 goto out; 193 } 194 195 rc = get_ready_session(session); 196 if (rc) 197 goto out; 198 199 template_size = sizeof(*template) + template->attrs_size; 200 201 rc = check_mechanism_against_processing(session, proc_params->id, 202 PKCS11_FUNCTION_GENERATE, 203 PKCS11_FUNC_STEP_INIT); 204 if (rc) { 205 DMSG("Invalid mechanism %#"PRIx32": %#x", proc_params->id, rc); 206 goto out; 207 } 208 209 /* 210 * Prepare a clean initial state for the requested object attributes. 211 * Free temporary template once done. 212 */ 213 rc = create_attributes_from_template(&head, template, template_size, 214 NULL, PKCS11_FUNCTION_GENERATE, 215 proc_params->id); 216 if (rc) 217 goto out; 218 219 TEE_Free(template); 220 template = NULL; 221 222 rc = check_created_attrs(head, NULL); 223 if (rc) 224 goto out; 225 226 rc = check_created_attrs_against_processing(proc_params->id, head); 227 if (rc) 228 goto out; 229 230 rc = check_created_attrs_against_token(session, head); 231 if (rc) 232 goto out; 233 234 /* 235 * Execute target processing and add value as attribute 236 * PKCS11_CKA_VALUE. Symm key generation: depends on target 237 * processing to be used. 238 */ 239 switch (proc_params->id) { 240 case PKCS11_CKM_GENERIC_SECRET_KEY_GEN: 241 case PKCS11_CKM_AES_KEY_GEN: 242 /* Generate random of size specified by attribute VALUE_LEN */ 243 rc = generate_random_key_value(&head); 244 if (rc) 245 goto out; 246 break; 247 248 default: 249 rc = PKCS11_CKR_MECHANISM_INVALID; 250 goto out; 251 } 252 253 TEE_Free(proc_params); 254 proc_params = NULL; 255 256 /* 257 * Object is ready, register it and return a handle. 258 */ 259 rc = create_object(session, head, &obj_handle); 260 if (rc) 261 goto out; 262 263 /* 264 * Now obj_handle (through the related struct pkcs11_object instance) 265 * owns the serialized buffer that holds the object attributes. 266 * We reset head to NULL as it is no more the buffer owner and would 267 * be freed at function out. 268 */ 269 head = NULL; 270 271 TEE_MemMove(out->memref.buffer, &obj_handle, sizeof(obj_handle)); 272 out->memref.size = sizeof(obj_handle); 273 274 DMSG("PKCS11 session %"PRIu32": generate secret %#"PRIx32, 275 session->handle, obj_handle); 276 277 out: 278 TEE_Free(proc_params); 279 TEE_Free(template); 280 TEE_Free(head); 281 282 return rc; 283 } 284 285 /* 286 * entry_processing_init - Generic entry for initializing a processing 287 * 288 * @client = client reference 289 * @ptype = Invocation parameter types 290 * @params = Invocation parameters reference 291 * @function - encrypt, decrypt, sign, verify, digest, ... 292 */ 293 enum pkcs11_rc entry_processing_init(struct pkcs11_client *client, 294 uint32_t ptypes, TEE_Param *params, 295 enum processing_func function) 296 { 297 const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT, 298 TEE_PARAM_TYPE_NONE, 299 TEE_PARAM_TYPE_NONE, 300 TEE_PARAM_TYPE_NONE); 301 TEE_Param *ctrl = params; 302 enum pkcs11_rc rc = PKCS11_CKR_OK; 303 struct serialargs ctrlargs = { }; 304 struct pkcs11_session *session = NULL; 305 struct pkcs11_attribute_head *proc_params = NULL; 306 uint32_t key_handle = 0; 307 struct pkcs11_object *obj = NULL; 308 309 if (!client || ptypes != exp_pt) 310 return PKCS11_CKR_ARGUMENTS_BAD; 311 312 serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size); 313 314 rc = serialargs_get_session_from_handle(&ctrlargs, client, &session); 315 if (rc) 316 return rc; 317 318 rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t)); 319 if (rc) 320 return rc; 321 322 rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params); 323 if (rc) 324 return rc; 325 326 if (serialargs_remaining_bytes(&ctrlargs)) { 327 rc = PKCS11_CKR_ARGUMENTS_BAD; 328 goto out; 329 } 330 331 rc = get_ready_session(session); 332 if (rc) 333 goto out; 334 335 obj = pkcs11_handle2object(key_handle, session); 336 if (!obj) { 337 rc = PKCS11_CKR_KEY_HANDLE_INVALID; 338 goto out; 339 } 340 341 rc = set_processing_state(session, function, obj, NULL); 342 if (rc) 343 goto out; 344 345 rc = check_mechanism_against_processing(session, proc_params->id, 346 function, 347 PKCS11_FUNC_STEP_INIT); 348 if (rc) 349 goto out; 350 351 rc = check_parent_attrs_against_processing(proc_params->id, function, 352 obj->attributes); 353 if (rc) 354 goto out; 355 356 rc = check_access_attrs_against_token(session, obj->attributes); 357 if (rc) 358 goto out; 359 360 if (processing_is_tee_symm(proc_params->id)) 361 rc = init_symm_operation(session, function, proc_params, obj); 362 else 363 rc = PKCS11_CKR_MECHANISM_INVALID; 364 365 if (rc == PKCS11_CKR_OK) { 366 session->processing->mecha_type = proc_params->id; 367 DMSG("PKCS11 session %"PRIu32": init processing %s %s", 368 session->handle, id2str_proc(proc_params->id), 369 id2str_function(function)); 370 } 371 372 out: 373 if (rc && session) 374 release_active_processing(session); 375 376 TEE_Free(proc_params); 377 378 return rc; 379 } 380 381 /* 382 * entry_processing_step - Generic entry on active processing 383 * 384 * @client = client reference 385 * @ptype = Invocation parameter types 386 * @params = Invocation parameters reference 387 * @function - encrypt, decrypt, sign, verify, digest, ... 388 * @step - update, oneshot, final 389 */ 390 enum pkcs11_rc entry_processing_step(struct pkcs11_client *client, 391 uint32_t ptypes, TEE_Param *params, 392 enum processing_func function, 393 enum processing_step step) 394 { 395 TEE_Param *ctrl = params; 396 enum pkcs11_rc rc = PKCS11_CKR_OK; 397 struct serialargs ctrlargs = { }; 398 struct pkcs11_session *session = NULL; 399 enum pkcs11_mechanism_id mecha_type = PKCS11_CKM_UNDEFINED_ID; 400 401 if (!client || 402 TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT) 403 return PKCS11_CKR_ARGUMENTS_BAD; 404 405 serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size); 406 407 rc = serialargs_get_session_from_handle(&ctrlargs, client, &session); 408 if (rc) 409 return rc; 410 411 if (serialargs_remaining_bytes(&ctrlargs)) 412 return PKCS11_CKR_ARGUMENTS_BAD; 413 414 rc = get_active_session(session, function); 415 if (rc) 416 return rc; 417 418 mecha_type = session->processing->mecha_type; 419 rc = check_mechanism_against_processing(session, mecha_type, 420 function, step); 421 if (rc) 422 goto out; 423 424 if (processing_is_tee_symm(mecha_type)) 425 rc = step_symm_operation(session, function, step, 426 ptypes, params); 427 else 428 rc = PKCS11_CKR_MECHANISM_INVALID; 429 430 if (rc == PKCS11_CKR_OK && step == PKCS11_FUNC_STEP_UPDATE) { 431 session->processing->updated = true; 432 DMSG("PKCS11 session%"PRIu32": processing %s %s", 433 session->handle, id2str_proc(mecha_type), 434 id2str_function(function)); 435 } 436 437 out: 438 switch (step) { 439 case PKCS11_FUNC_STEP_UPDATE: 440 if (rc != PKCS11_CKR_OK && rc != PKCS11_CKR_BUFFER_TOO_SMALL) 441 release_active_processing(session); 442 break; 443 default: 444 /* ONESHOT and FINAL terminates processing on success */ 445 if (rc != PKCS11_CKR_BUFFER_TOO_SMALL) 446 release_active_processing(session); 447 break; 448 } 449 450 return rc; 451 } 452