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