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 switch (session->processing->mecha_type) { 79 case PKCS11_CKM_AES_CTR: 80 tee_release_ctr_operation(session->processing); 81 break; 82 default: 83 break; 84 } 85 86 if (session->processing->tee_op_handle != TEE_HANDLE_NULL) { 87 TEE_FreeOperation(session->processing->tee_op_handle); 88 session->processing->tee_op_handle = TEE_HANDLE_NULL; 89 } 90 91 TEE_Free(session->processing->extra_ctx); 92 93 TEE_Free(session->processing); 94 session->processing = NULL; 95 } 96 97 size_t get_object_key_bit_size(struct pkcs11_object *obj) 98 { 99 uint32_t a_size = 0; 100 struct obj_attrs *attrs = obj->attributes; 101 102 switch (get_key_type(attrs)) { 103 case PKCS11_CKK_AES: 104 case PKCS11_CKK_GENERIC_SECRET: 105 case PKCS11_CKK_MD5_HMAC: 106 case PKCS11_CKK_SHA_1_HMAC: 107 case PKCS11_CKK_SHA224_HMAC: 108 case PKCS11_CKK_SHA256_HMAC: 109 case PKCS11_CKK_SHA384_HMAC: 110 case PKCS11_CKK_SHA512_HMAC: 111 if (get_attribute_ptr(attrs, PKCS11_CKA_VALUE, NULL, &a_size)) 112 return 0; 113 114 return a_size * 8; 115 default: 116 TEE_Panic(0); 117 return 0; 118 } 119 } 120 121 /* 122 * entry_processing_init - Generic entry for initializing a processing 123 * 124 * @client = client reference 125 * @ptype = Invocation parameter types 126 * @params = Invocation parameters reference 127 * @function - encrypt, decrypt, sign, verify, digest, ... 128 */ 129 enum pkcs11_rc entry_processing_init(struct pkcs11_client *client, 130 uint32_t ptypes, TEE_Param *params, 131 enum processing_func function) 132 { 133 const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT, 134 TEE_PARAM_TYPE_NONE, 135 TEE_PARAM_TYPE_NONE, 136 TEE_PARAM_TYPE_NONE); 137 TEE_Param *ctrl = params; 138 enum pkcs11_rc rc = PKCS11_CKR_OK; 139 struct serialargs ctrlargs = { }; 140 struct pkcs11_session *session = NULL; 141 struct pkcs11_attribute_head *proc_params = NULL; 142 uint32_t key_handle = 0; 143 struct pkcs11_object *obj = NULL; 144 145 if (!client || ptypes != exp_pt) 146 return PKCS11_CKR_ARGUMENTS_BAD; 147 148 serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size); 149 150 rc = serialargs_get_session_from_handle(&ctrlargs, client, &session); 151 if (rc) 152 return rc; 153 154 rc = serialargs_get(&ctrlargs, &key_handle, sizeof(uint32_t)); 155 if (rc) 156 return rc; 157 158 rc = serialargs_alloc_get_one_attribute(&ctrlargs, &proc_params); 159 if (rc) 160 return rc; 161 162 if (serialargs_remaining_bytes(&ctrlargs)) { 163 rc = PKCS11_CKR_ARGUMENTS_BAD; 164 goto out; 165 } 166 167 rc = get_ready_session(session); 168 if (rc) 169 goto out; 170 171 obj = pkcs11_handle2object(key_handle, session); 172 if (!obj) { 173 rc = PKCS11_CKR_KEY_HANDLE_INVALID; 174 goto out; 175 } 176 177 rc = set_processing_state(session, function, obj, NULL); 178 if (rc) 179 goto out; 180 181 rc = check_mechanism_against_processing(session, proc_params->id, 182 function, 183 PKCS11_FUNC_STEP_INIT); 184 if (rc) 185 goto out; 186 187 rc = check_parent_attrs_against_processing(proc_params->id, function, 188 obj->attributes); 189 if (rc) 190 goto out; 191 192 rc = check_access_attrs_against_token(session, obj->attributes); 193 if (rc) 194 goto out; 195 196 if (processing_is_tee_symm(proc_params->id)) 197 rc = init_symm_operation(session, function, proc_params, obj); 198 else 199 rc = PKCS11_CKR_MECHANISM_INVALID; 200 201 if (rc == PKCS11_CKR_OK) { 202 session->processing->mecha_type = proc_params->id; 203 DMSG("PKCS11 session %"PRIu32": init processing %s %s", 204 session->handle, id2str_proc(proc_params->id), 205 id2str_function(function)); 206 } 207 208 out: 209 if (rc && session) 210 release_active_processing(session); 211 212 TEE_Free(proc_params); 213 214 return rc; 215 } 216 217 /* 218 * entry_processing_step - Generic entry on active processing 219 * 220 * @client = client reference 221 * @ptype = Invocation parameter types 222 * @params = Invocation parameters reference 223 * @function - encrypt, decrypt, sign, verify, digest, ... 224 * @step - update, oneshot, final 225 */ 226 enum pkcs11_rc entry_processing_step(struct pkcs11_client *client, 227 uint32_t ptypes, TEE_Param *params, 228 enum processing_func function, 229 enum processing_step step) 230 { 231 TEE_Param *ctrl = params; 232 enum pkcs11_rc rc = PKCS11_CKR_OK; 233 struct serialargs ctrlargs = { }; 234 struct pkcs11_session *session = NULL; 235 enum pkcs11_mechanism_id mecha_type = PKCS11_CKM_UNDEFINED_ID; 236 237 if (!client || 238 TEE_PARAM_TYPE_GET(ptypes, 0) != TEE_PARAM_TYPE_MEMREF_INOUT) 239 return PKCS11_CKR_ARGUMENTS_BAD; 240 241 serialargs_init(&ctrlargs, ctrl->memref.buffer, ctrl->memref.size); 242 243 rc = serialargs_get_session_from_handle(&ctrlargs, client, &session); 244 if (rc) 245 return rc; 246 247 if (serialargs_remaining_bytes(&ctrlargs)) 248 return PKCS11_CKR_ARGUMENTS_BAD; 249 250 rc = get_active_session(session, function); 251 if (rc) 252 return rc; 253 254 mecha_type = session->processing->mecha_type; 255 rc = check_mechanism_against_processing(session, mecha_type, 256 function, step); 257 if (rc) 258 goto out; 259 260 if (processing_is_tee_symm(mecha_type)) 261 rc = step_symm_operation(session, function, step, 262 ptypes, params); 263 else 264 rc = PKCS11_CKR_MECHANISM_INVALID; 265 266 if (rc == PKCS11_CKR_OK && step == PKCS11_FUNC_STEP_UPDATE) { 267 session->processing->updated = true; 268 DMSG("PKCS11 session%"PRIu32": processing %s %s", 269 session->handle, id2str_proc(mecha_type), 270 id2str_function(function)); 271 } 272 273 out: 274 switch (step) { 275 case PKCS11_FUNC_STEP_UPDATE: 276 if (rc != PKCS11_CKR_OK && rc != PKCS11_CKR_BUFFER_TOO_SMALL) 277 release_active_processing(session); 278 break; 279 default: 280 /* ONESHOT and FINAL terminates processing on success */ 281 if (rc != PKCS11_CKR_BUFFER_TOO_SMALL) 282 release_active_processing(session); 283 break; 284 } 285 286 return rc; 287 } 288