• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include "tee_v3_elf_verify.h"
13 #include "securec.h"
14 #include "tee_log.h"
15 #include "tee_perm_img.h"
16 #include "mem_ops.h"
17 #include "ta_load_config.h"
18 #include "ta_lib_img_unpack.h"
19 #include "target_type.h"
20 #include "ta_verify_key.h"
21 #include "tee_elf_verify.h"
22 #include "ta_load_key.h"
23 #include "tee_comm_elf_verify.h"
24 #include "tee_elf_verify_inner.h"
25 #include "tee_crypto_hal.h"
26 #include "dyn_conf_common.h"
27 #include "drv_dyn_conf_builder.h"
28 #include "drv_dyn_conf_builder.h"
29 #include "dyn_conf_dispatch_inf.h"
30 #include "drvcall_dyn_conf_builder.h"
31 #include "check_ta_version.h"
32 #include "tee_load_key_ops.h"
33 #include "tee_elf_verify_openssl.h"
34 #include "ipclib.h"
35 #include <sys/mman.h>
36 #if defined(OPENSSL_ENABLE) || defined(OPENSSL3_ENABLE)
37 #include <openssl/obj_mac.h>
38 #endif
39 
40 static uint32_t g_v3_cipher_layer_len = 0;
41 static bool g_is_encrypted_sec = true;
42 static ta_image_hdr_v3_t g_image_hdr_v3 = { { 0 }, 0, 0 };
43 static ta_cipher_layer_t g_ta_cipher_layer = { { 0 }, NULL, NULL };
44 
get_ta_cipher_layer(void)45 ta_cipher_layer_t *get_ta_cipher_layer(void)
46 {
47     return &g_ta_cipher_layer;
48 }
49 
get_v3_cipher_layer_len(void)50 uint32_t get_v3_cipher_layer_len(void)
51 {
52     return g_v3_cipher_layer_len;
53 }
54 
tee_secure_get_img_header_v3(const uint8_t * share_buf,uint32_t buf_len)55 static TEE_Result tee_secure_get_img_header_v3(const uint8_t *share_buf, uint32_t buf_len)
56 {
57     if (buf_len <= sizeof(ta_image_hdr_v3_t)) {
58         tloge("img buf len is 0x%x too small\n", buf_len);
59         return TEE_ERROR_GENERIC;
60     }
61     errno_t rc = memcpy_s(&g_image_hdr_v3, sizeof(g_image_hdr_v3), share_buf, sizeof(ta_image_hdr_v3_t));
62     if (rc != EOK) {
63         tloge("copy is failed\n");
64         return TEE_ERROR_SECURITY;
65     }
66 
67     return TEE_SUCCESS;
68 }
69 
tee_secure_img_get_signature_size(uint32_t signature_alg,const uint8_t * signature_buff,uint32_t signature_max_size)70 static uint32_t tee_secure_img_get_signature_size(uint32_t signature_alg,
71     const uint8_t *signature_buff, uint32_t signature_max_size)
72 {
73     (void)signature_buff;
74     (void)signature_max_size;
75     uint32_t size;
76 
77     switch (signature_alg & SIGN_ALG_MASK) {
78     case SIGN_ALGO_RSA_2048:
79         size = RSA2048_SIGNATURE_SIZE;
80         break;
81     case SIGN_ALGO_RSA_4096:
82         size = RSA4096_SIGNATURE_SIZE;
83         break;
84     case SIGN_ALGO_ECC_256:
85         size = ECC256_SIGNATURE_SIZE;
86         break;
87     default:
88         tloge("Invalid signature algorithm: 0x%x\n", signature_alg);
89         size = SIGNATURE_SIZE_INVALID;
90         break;
91     }
92 
93     return size;
94 }
95 
handle_cipher_layer_len(uint32_t cipher_layer_ver)96 static TEE_Result handle_cipher_layer_len(uint32_t cipher_layer_ver)
97 {
98     if (cipher_layer_ver <= CIPHER_LAYER_KEY_V1) {
99         g_v3_cipher_layer_len = CIPHER_LAYER_LEN_256;
100     } else if (cipher_layer_ver == CIPHER_LAYER_KEY_V2) {
101         g_v3_cipher_layer_len = CIPHER_LAYER_LEN_384;
102     } else {
103         tloge("error cipher layer key version:cipher layer ver=%u\n", cipher_layer_ver);
104         return TEE_ERROR_BAD_PARAMETERS;
105     }
106     return TEE_SUCCESS;
107 }
108 
tee_secure_img_header_check_v3(void)109 static TEE_Result tee_secure_img_header_check_v3(void)
110 {
111     uint32_t cipher_layer_ver;
112 
113     if (overflow_check(g_image_hdr_v3.context_len, sizeof(ta_image_hdr_v3_t)))
114         return TEE_ERROR_GENERIC;
115     if (g_image_hdr_v3.context_len + sizeof(ta_image_hdr_v3_t) > MAX_IMAGE_LEN) {
116         tloge("image hd err context len: 0x%x\n", g_image_hdr_v3.context_len);
117         tloge("image hd err ta hd len: 0x%x\n", sizeof(ta_image_hdr_v3_t));
118         return TEE_ERROR_GENERIC;
119     }
120 
121     if (g_image_hdr_v3.ta_key_version == KEY_VER_NOT_ENCRYPT) {
122         g_v3_cipher_layer_len = sizeof(ta_cipher_hdr_t);
123         g_is_encrypted_sec = false;
124         return TEE_SUCCESS;
125     } else if ((g_image_hdr_v3.ta_key_version & KEY_VER_MASK) != SEC_IMG_TA_KEY_VERSION) {
126         tloge("Invalid ta key version: 0x%x\n", g_image_hdr_v3.ta_key_version);
127         return TEE_ERROR_GENERIC;
128     }
129 
130     g_is_encrypted_sec = true;
131     cipher_layer_ver = ((g_image_hdr_v3.ta_key_version >> KEY_VER_BITE) & KEY_VER_MASK);
132     return handle_cipher_layer_len(cipher_layer_ver);
133 }
134 
process_header_v3(const uint8_t * share_buf,uint32_t buf_len)135 TEE_Result process_header_v3(const uint8_t *share_buf, uint32_t buf_len)
136 {
137     TEE_Result tee_ret;
138 
139     if (share_buf == NULL)
140         return TEE_ERROR_BAD_PARAMETERS;
141 
142     tee_ret = tee_secure_get_img_header_v3(share_buf, buf_len);
143     if (tee_ret != TEE_SUCCESS)
144         return tee_ret;
145 
146     tee_ret = tee_secure_img_header_check_v3();
147     if (tee_ret != TEE_SUCCESS) {
148         tloge("Failed to check image header");
149         return tee_ret;
150     }
151 
152     return TEE_SUCCESS;
153 }
154 
judge_rsa_key_type(uint32_t rsa_cipher_size,enum ta_type * type)155 TEE_Result judge_rsa_key_type(uint32_t rsa_cipher_size, enum ta_type *type)
156 {
157     if (type == NULL)
158         return TEE_ERROR_BAD_PARAMETERS;
159 
160     if (rsa_cipher_size == CIPHER_LAYER_LEN_256) {
161         *type = V3_TYPE_2048;
162     } else if (rsa_cipher_size == CIPHER_LAYER_LEN_384) {
163         *type = V3_TYPE_3072;
164     } else {
165         tloge("wrong rsa cipher size:%u\n", rsa_cipher_size);
166         return TEE_ERROR_BAD_PARAMETERS;
167     }
168     return TEE_SUCCESS;
169 }
170 
tee_secure_img_parse_cipher_layer(const uint8_t * plaintext_hdr,uint32_t plaintext_size,ta_cipher_layer_t * cipher_layer)171 static TEE_Result tee_secure_img_parse_cipher_layer(const uint8_t *plaintext_hdr, uint32_t plaintext_size,
172                                                     ta_cipher_layer_t *cipher_layer)
173 {
174     bool check = (plaintext_hdr == NULL || plaintext_size == 0 || cipher_layer == NULL);
175     if (check)
176         return TEE_ERROR_BAD_PARAMETERS;
177     uint32_t off_set = 0;
178     const uint8_t *iv = NULL;
179     const uint8_t *key = NULL;
180 
181     if (overflow_check(off_set, sizeof(ta_cipher_hdr_t)))
182         return TEE_ERROR_GENERIC;
183     if (boundary_check(plaintext_size, off_set + sizeof(ta_cipher_hdr_t)))
184         return TEE_ERROR_GENERIC;
185     errno_t rc = memcpy_s(&(cipher_layer->cipher_hdr), sizeof(ta_cipher_hdr_t), plaintext_hdr + off_set,
186                           sizeof(ta_cipher_hdr_t));
187     if (rc != EOK)
188         return TEE_ERROR_SECURITY;
189     off_set += sizeof(ta_cipher_hdr_t);
190 
191     if (cipher_layer->cipher_hdr.iv_size == 0 && cipher_layer->cipher_hdr.key_size == 0 && !g_is_encrypted_sec) {
192         tlogd("not encrypt, no need duplicate iv & key buff\n");
193         return TEE_SUCCESS;
194     }
195 
196     if (overflow_check(off_set, cipher_layer->cipher_hdr.key_size))
197         return TEE_ERROR_GENERIC;
198     if (boundary_check(plaintext_size, off_set + cipher_layer->cipher_hdr.key_size))
199         return TEE_ERROR_GENERIC;
200     key = plaintext_hdr + off_set;
201     off_set += cipher_layer->cipher_hdr.key_size;
202 
203     if (overflow_check(off_set, cipher_layer->cipher_hdr.iv_size))
204         return TEE_ERROR_GENERIC;
205     if (boundary_check(plaintext_size, off_set + cipher_layer->cipher_hdr.iv_size))
206         return TEE_ERROR_GENERIC;
207     iv = plaintext_hdr + off_set;
208 
209     TEE_Result ret = tee_secure_img_duplicate_buff(iv, cipher_layer->cipher_hdr.iv_size, &(cipher_layer->iv));
210     if (ret != TEE_SUCCESS) {
211         tloge("Failed to dump iv of TA image\n");
212         return ret;
213     }
214 
215     ret = tee_secure_img_duplicate_buff(key, cipher_layer->cipher_hdr.key_size, &(cipher_layer->key));
216     if (ret != TEE_SUCCESS) {
217         tloge("Failed to dump key of TA image\n");
218         TEE_Free(cipher_layer->iv);
219         cipher_layer->iv = NULL;
220         return ret;
221     }
222     return TEE_SUCCESS;
223 }
224 
225 #define OP_SIZE 1024
tee_sec_img_payload_decrypt_ops(TEE_ObjectHandle key_obj,const uint8_t * src,uint32_t src_len,uint8_t * dst,uint32_t * dst_len)226 static TEE_Result tee_sec_img_payload_decrypt_ops(TEE_ObjectHandle key_obj, const uint8_t *src, uint32_t src_len,
227                                                   uint8_t *dst, uint32_t *dst_len)
228 {
229     if (key_obj == NULL || src == NULL || dst == NULL || src_len == 0 || !(dst_len != NULL && *dst_len >= src_len))
230         return TEE_ERROR_BAD_PARAMETERS;
231     TEE_OperationHandle crypto_ops = NULL;
232     size_t op_size;
233     size_t left_size = src_len;
234     size_t out_size;
235     size_t total_size = 0;
236 
237     TEE_Result ret = TEE_AllocateOperation(&crypto_ops, TEE_ALG_AES_CBC_PKCS5, TEE_MODE_DECRYPT, KEY_SIZE_MAX);
238     if (ret != TEE_SUCCESS) {
239         tloge("Failed to allocate operation to decrypt TA image\n");
240         return ret;
241     }
242     ret = TEE_SetCryptoFlag(crypto_ops, SOFT_CRYPTO);
243     if (ret != TEE_SUCCESS)
244         goto out;
245 
246     ret = TEE_SetOperationKey(crypto_ops, key_obj);
247     if (ret != TEE_SUCCESS) {
248         tloge("Set Operation Key fail\n");
249         goto out;
250     }
251 
252     TEE_CipherInit(crypto_ops, g_ta_cipher_layer.iv, g_ta_cipher_layer.cipher_hdr.iv_size);
253 
254     while (left_size > OP_SIZE) {
255         op_size = OP_SIZE;
256         out_size = op_size;
257         ret = TEE_CipherUpdate(crypto_ops, src, op_size, dst, &out_size);
258         if (ret != TEE_SUCCESS) {
259             tloge("TEE Cipher Update fail\n");
260             goto out;
261         }
262         src += op_size;
263         left_size -= op_size;
264         dst += out_size;
265         total_size += out_size;
266     }
267 
268     /* update remain length for dst */
269     out_size = *dst_len - total_size;
270     ret = TEE_CipherDoFinal(crypto_ops, src, left_size, dst, &out_size);
271     if (ret != TEE_SUCCESS) {
272         tloge("Cipher Dofinal fail\n");
273         goto out;
274     }
275     total_size += out_size;
276     *dst_len = total_size;
277     ret = TEE_SUCCESS;
278 out:
279     TEE_FreeOperation(crypto_ops);
280     return ret;
281 }
282 
tee_secure_img_decrypt_payload(const uint8_t * ciphertext_payload,uint32_t ciphertext_size,uint8_t * plaintext_payload,uint32_t * plaintext_size)283 static TEE_Result tee_secure_img_decrypt_payload(const uint8_t *ciphertext_payload, uint32_t ciphertext_size,
284                                                  uint8_t *plaintext_payload, uint32_t *plaintext_size)
285 {
286     bool check = (ciphertext_payload == NULL || ciphertext_size == 0 || plaintext_payload == NULL ||
287         plaintext_size == NULL || *plaintext_size < ciphertext_size);
288     if (check)
289         return TEE_ERROR_BAD_PARAMETERS;
290     TEE_ObjectHandleVar key_obj = {0};
291 
292     key_obj.Attribute = (TEE_Attribute *)TEE_Malloc(sizeof(TEE_Attribute), 0);
293     if (key_obj.Attribute == NULL) {
294         tloge("Failed to allocate key attribute\n");
295         return TEE_ERROR_OUT_OF_MEMORY;
296     }
297 
298     key_obj.Attribute->content.ref.buffer = g_ta_cipher_layer.key;
299     key_obj.Attribute->content.ref.length = g_ta_cipher_layer.cipher_hdr.key_size;
300 
301     TEE_Result ret = tee_sec_img_payload_decrypt_ops(&key_obj, ciphertext_payload, ciphertext_size, plaintext_payload,
302                                                      plaintext_size);
303     TEE_Free(key_obj.Attribute);
304     key_obj.Attribute = NULL;
305     if (ret != TEE_SUCCESS) {
306         tloge("Failed to decrypted TA image body\n");
307         return ret;
308     }
309     return TEE_SUCCESS;
310 }
311 
alloc_name_buffer_copy_mani_conf(const uint8_t * manifest,uint32_t manifest_size)312 static TEE_Result alloc_name_buffer_copy_mani_conf(const uint8_t *manifest, uint32_t manifest_size)
313 {
314     uint32_t off_set = 0;
315     load_img_info *img_info = get_img_info();
316     ta_property_t *ta_property_ptr = get_ta_property_ptr();
317 
318     errno_t rc = memcpy_s(&img_info->manifest.srv_uuid,
319         sizeof(img_info->manifest.srv_uuid), manifest + off_set, sizeof(TEE_UUID));
320     if (rc != EOK) {
321         tloge("failed to copy uuid\n");
322         return TEE_ERROR_SECURITY;
323     }
324     off_set += sizeof(TEE_UUID);
325 
326     img_info->manifest.mani_info.service_name_len = manifest_size - sizeof(TEE_UUID) - sizeof(ta_property_t);
327     img_info->manifest.service_name = TEE_Malloc(img_info->manifest.mani_info.service_name_len + 1, 0);
328     if (img_info->manifest.service_name == NULL) {
329         tloge("failed to allocate memory for service_name\n");
330         return TEE_ERROR_OUT_OF_MEMORY;
331     }
332     rc = memcpy_s(img_info->manifest.service_name, img_info->manifest.mani_info.service_name_len + 1,
333         manifest + off_set, img_info->manifest.mani_info.service_name_len);
334     if (rc != EOK) {
335         TEE_Free(img_info->manifest.service_name);
336         img_info->manifest.service_name = NULL;
337         return TEE_ERROR_SECURITY;
338     }
339     off_set += img_info->manifest.mani_info.service_name_len;
340 
341     rc = memcpy_s(ta_property_ptr, sizeof(*ta_property_ptr), manifest + off_set,
342                   sizeof(ta_property_t));
343     if (rc != EOK) {
344         tloge("failed to copy manifest header\n");
345         TEE_Free(img_info->manifest.service_name);
346         img_info->manifest.service_name = NULL;
347         return TEE_ERROR_SECURITY;
348     }
349 
350     if (ta_property_ptr->single_instance == false) {
351         tloge("only support single Instance as true\n");
352         TEE_Free(img_info->manifest.service_name);
353         img_info->manifest.service_name = NULL;
354         return TEE_ERROR_BAD_FORMAT;
355     }
356 
357     return TEE_SUCCESS;
358 }
359 
handle_cryptomgr_drv_mani(struct drv_mani_t * drv_mani)360 static int32_t handle_cryptomgr_drv_mani(struct drv_mani_t *drv_mani)
361 {
362     if (drv_mani->hardware_type == HARDWARE_ENGINE_CRYPTO) {
363         if (memcpy_s(drv_mani->service_name, DRV_NAME_MAX_LEN, "crypto_mgr", strlen("crypto_mgr") + 1) != 0) {
364             tloge("memcpy service name failed\n");
365             return TEE_ERROR_GENERIC;
366         }
367         drv_mani->service_name_size = strlen(drv_mani->service_name);
368         drv_mani->srv_uuid = (struct tee_uuid)CRYPTOMGR;
369     }
370 
371     return 0;
372 }
373 
set_drv_manifest(struct drv_mani_t * drv_mani)374 static int32_t set_drv_manifest(struct drv_mani_t *drv_mani)
375 {
376     load_img_info *img_info = get_img_info();
377     ta_property_t *ta_property_ptr = get_ta_property_ptr();
378     /* 1.set service name */
379     if (img_info->manifest.service_name != NULL) {
380         if (memcpy_s(drv_mani->service_name, DRV_NAME_MAX_LEN,
381                      img_info->manifest.service_name, img_info->manifest.mani_info.service_name_len) != 0) {
382             tloge("memcpy service name failed\n");
383             return TEE_ERROR_GENERIC;
384         }
385     }
386     drv_mani->service_name_size = img_info->manifest.mani_info.service_name_len;
387 
388     /* 2.set uuid */
389     if (memcpy_s(&drv_mani->srv_uuid, sizeof(struct tee_uuid),
390                  &img_info->manifest.srv_uuid, sizeof(struct tee_uuid)) != 0) {
391         tloge("set uuid to drv conf failed\n");
392         return TEE_ERROR_GENERIC;
393     }
394 
395     /* 3.set keep alive */
396     if (ta_property_ptr->instance_keep_alive != 0)
397         drv_mani->keep_alive = true;
398     else
399         drv_mani->keep_alive = false;
400 
401     /* 4.set size */
402     drv_mani->data_size = ta_property_ptr->heap_size;
403     drv_mani->stack_size = ta_property_ptr->stack_size;
404     drv_mani->hardware_type = img_info->manifest.ext.hardware_type;
405 
406     if (handle_cryptomgr_drv_mani(drv_mani) != 0)
407         return TEE_ERROR_GENERIC;
408 
409     return TEE_SUCCESS;
410 }
411 
handle_dyn_conf_buffer(struct dyn_conf_t * dyn_conf,uint32_t * ext_size)412 static int32_t handle_dyn_conf_buffer(struct dyn_conf_t *dyn_conf, uint32_t *ext_size)
413 {
414     int32_t ret;
415     load_img_info *img_info = get_img_info();
416 
417     if (dyn_conf->dyn_conf_size >= *ext_size) {
418         tloge("dyn conf size is larger than ext size %u\n", dyn_conf->dyn_conf_size);
419         free(dyn_conf->dyn_conf_buffer);
420         dyn_conf->dyn_conf_buffer = NULL;
421         return TEE_ERROR_GENERIC;
422     }
423 
424     *ext_size -= dyn_conf->dyn_conf_size;
425 
426     struct drv_mani_t drv_mani;
427     if (set_drv_manifest(&drv_mani) != TEE_SUCCESS) {
428         free(dyn_conf->dyn_conf_buffer);
429         dyn_conf->dyn_conf_buffer = NULL;
430         return TEE_ERROR_GENERIC;
431     }
432 
433     if (img_info->manifest.ext.target_type == DRV_TARGET_TYPE) {
434         ret = register_conf(dyn_conf, install_drv_permission, &drv_mani, sizeof(drv_mani));
435     } else if (img_info->manifest.ext.target_type == TA_TARGET_TYPE ||
436         img_info->manifest.ext.target_type == SRV_TARGET_TYPE ||
437         img_info->manifest.ext.target_type == CLIENT_TARGET_TYPE) {
438         ret = register_conf(dyn_conf, install_drvcall_permission, &drv_mani.srv_uuid, sizeof(drv_mani.srv_uuid));
439     } else {
440         ret = TEE_ERROR_GENERIC;
441         tloge("unknown target type\n");
442     }
443 
444     free(dyn_conf->dyn_conf_buffer);
445     dyn_conf->dyn_conf_buffer = NULL;
446 
447     return ret;
448 }
449 #define MAX_SERVICE_NAME_SIZE 64
check_manifest_alloc_name(const uint8_t * manifest,uint32_t manifest_size)450 static TEE_Result check_manifest_alloc_name(const uint8_t *manifest, uint32_t manifest_size)
451 {
452     TEE_Result ret;
453     bool check = (manifest == NULL || (manifest_size < sizeof(TEE_UUID) + sizeof(ta_property_t)) ||
454         (manifest_size > sizeof(TEE_UUID) + sizeof(ta_property_t) + MAX_SERVICE_NAME_SIZE));
455     if (check)
456         return TEE_ERROR_BAD_PARAMETERS;
457 
458     ret = alloc_name_buffer_copy_mani_conf(manifest, manifest_size);
459     if (ret != TEE_SUCCESS)
460         return TEE_ERROR_BAD_PARAMETERS;
461     return TEE_SUCCESS;
462 }
463 
464 /*
465  * Process steps:
466  * 1, Get the manifest UUID,
467  * 2, Get the manifest stand config,
468  * 3, Get the TA service name,
469  * 4, Parse manifest extension config,
470  */
tee_secure_img_parse_manifest_v3(const uint8_t * manifest_ext,uint32_t * ext_size,bool control,const uint32_t config_target_type)471 TEE_Result tee_secure_img_parse_manifest_v3(const uint8_t *manifest_ext, uint32_t *ext_size,
472                                             bool control, const uint32_t config_target_type)
473 {
474     TEE_Result ret;
475     load_img_info *img_info = get_img_info();
476     bool check = ((ext_size == NULL) || !((manifest_ext != NULL && *ext_size > 0) || *ext_size == 0));
477     if (check)
478         return TEE_ERROR_BAD_PARAMETERS;
479 
480     if (!control) {
481         tlogd("check config and manifest target type\n");
482         if (config_target_type != img_info->manifest.ext.target_type) {
483             tloge("diff type con_type=%d mani_type=%d\n", config_target_type, img_info->manifest.ext.target_type);
484             return TEE_ERROR_BAD_PARAMETERS;
485         }
486     }
487     struct dyn_conf_t dyn_conf = { 0, NULL };
488     ret = tee_secure_img_manifest_extention_process(manifest_ext, *ext_size,
489         &img_info->manifest.ext, &dyn_conf);
490 
491     if (img_info->manifest.ext.api_level > API_LEVEL1_2) {
492         tloge("invalid ta api level:%u\n", img_info->manifest.ext.api_level);
493         ret = TEE_ERROR_BAD_PARAMETERS;
494     }
495 
496     if (ret != TEE_SUCCESS) {
497         tloge("Manifest extension configuration is invalid\n");
498         goto err;
499     }
500 
501     if (dyn_conf.dyn_conf_buffer != NULL) {
502         ret = (TEE_Result)handle_dyn_conf_buffer(&dyn_conf, ext_size);
503         if (ret != TEE_SUCCESS) {
504             tloge("register dyn conf for dyn perm failed\n");
505             goto err;
506         }
507         img_info->dyn_conf_registed = true;
508     }
509 
510     return ret;
511 err:
512     if (dyn_conf.dyn_conf_buffer != NULL)
513         free(dyn_conf.dyn_conf_buffer);
514     TEE_Free(img_info->manifest.service_name);
515     img_info->manifest.service_name = NULL;
516 
517     return ret;
518 }
519 
520 /*
521  * Process steps:
522  * 1, Get the payload header,
523  * 2, Get the manifest stand config,
524  * 3, Get the manifest extension config,
525  * 4, Get the TA ELF segment,
526  * 5, Get the TA config segment,
527  * 6, Parse manifest stand config & extension config,
528  */
tee_secure_img_parse_payload(uint8_t * plaintext_payload,uint32_t plaintext_size,ta_payload_layer_t * payload)529 static TEE_Result tee_secure_img_parse_payload(uint8_t *plaintext_payload, uint32_t plaintext_size,
530                                                ta_payload_layer_t *payload)
531 {
532     uint32_t off_set = 0;
533     uint8_t *mani_info = NULL;
534     uint8_t *mani_ext = NULL;
535 
536     if (plaintext_payload == NULL || payload == NULL)
537         return TEE_ERROR_BAD_PARAMETERS;
538 
539     if (overflow_check(off_set, sizeof(ta_payload_hdr_t)))
540         return TEE_ERROR_GENERIC;
541     if (boundary_check(plaintext_size, off_set + sizeof(ta_payload_hdr_t)))
542         return TEE_ERROR_GENERIC;
543     errno_t rc = memcpy_s(&(payload->payload_hdr), sizeof(ta_payload_hdr_t), plaintext_payload + off_set,
544                           sizeof(ta_payload_hdr_t));
545     if (rc != EOK)
546         return TEE_ERROR_SECURITY;
547     off_set += sizeof(ta_payload_hdr_t);
548 
549     if (overflow_check(off_set, payload->payload_hdr.mani_info_size))
550         return TEE_ERROR_GENERIC;
551     if (boundary_check(plaintext_size, off_set + payload->payload_hdr.mani_info_size))
552         return TEE_ERROR_GENERIC;
553     mani_info = plaintext_payload + off_set;
554     off_set += payload->payload_hdr.mani_info_size;
555 
556     if (overflow_check(off_set, payload->payload_hdr.mani_ext_size))
557         return TEE_ERROR_GENERIC;
558     if (boundary_check(plaintext_size, off_set + payload->payload_hdr.mani_ext_size))
559         return TEE_ERROR_GENERIC;
560     mani_ext = plaintext_payload + off_set;
561     off_set += payload->payload_hdr.mani_ext_size;
562 
563     if (overflow_check(off_set, payload->payload_hdr.ta_elf_size))
564         return TEE_ERROR_GENERIC;
565     if (boundary_check(plaintext_size, off_set + payload->payload_hdr.ta_elf_size))
566         return TEE_ERROR_GENERIC;
567     payload->ta_elf = plaintext_payload + off_set;
568     off_set += payload->payload_hdr.ta_elf_size;
569 
570     if (overflow_check(off_set, payload->payload_hdr.ta_conf_size))
571         return TEE_ERROR_GENERIC;
572     if (boundary_check(plaintext_size, off_set + payload->payload_hdr.ta_conf_size))
573         return TEE_ERROR_GENERIC;
574     payload->ta_conf = plaintext_payload + off_set;
575 
576     TEE_Result ret = check_manifest_alloc_name(mani_info, payload->payload_hdr.mani_info_size);
577     if (ret != TEE_SUCCESS)
578         return ret;
579 
580     ret = tee_secure_img_parse_manifest_v3(mani_ext, &(payload->payload_hdr.mani_ext_size), true, 0);
581     if (ret != TEE_SUCCESS)
582         return ret;
583 
584     return TEE_SUCCESS;
585 }
586 
get_sign_config(struct sign_config_t * config)587 void get_sign_config(struct sign_config_t *config)
588 {
589     if (config == NULL)
590         return;
591 
592     uint32_t sign_alg = g_ta_cipher_layer.cipher_hdr.signature_alg;
593 
594     switch (sign_alg & SIGN_ALG_KEY_LEN_MASK) {
595     case SIGN_ALGO_RSA_2048:
596         config->key_len = PUB_KEY_2048_BITS;
597         break;
598     case SIGN_ALGO_RSA_4096:
599         config->key_len = PUB_KEY_4096_BITS;
600         break;
601     case SIGN_ALGO_ECC_256:
602         config->key_len = PUB_KEY_256_BITS;
603         break;
604     default:
605         tloge("sign alg is invalid!");
606         return;
607     }
608     config->hash_size = ((sign_alg & SIGN_ALG_HASH_MASK) != 0) ? SHA512_LEN : SHA256_LEN;
609     config->hash_nid = (config->hash_size == SHA512_LEN) ? NID_sha512 : NID_sha256;
610     config->padding = ((sign_alg & SIGN_ALG_PADD_MASK) != 0) ? RSA_PKCS1_PSS_PADDING : RSA_PKCS1_PADDING;
611     config->key_style = ((sign_alg & SIGN_ALG_KEY_STYLE_MASK) == 0) ? PUB_KEY_DEBUG : PUB_KEY_RELEASE;
612     config->sign_ta_alg = (sign_alg >> SIGN_TA_ALG_BITS) & SIGN_ALG_TA_ALG_MASK;
613 }
614 
check_img_format_valid(struct sign_config_t * config)615 bool check_img_format_valid(struct sign_config_t *config)
616 {
617     if (config == NULL)
618         return false;
619 
620     ta_payload_layer_t *ta_payload = get_ta_payload();
621     if (config->key_style == PUB_KEY_RELEASE && !g_is_encrypted_sec &&
622         config->sign_ta_alg == SIGN_SEC_ALG_DEFAULT) {
623         tloge("Invalid ta key version 0, release key not support only sign for sec\n");
624         return false;
625     }
626 
627     if (ta_payload->payload_hdr.format_version != CIPHER_LAYER_VERSION) {
628         tloge("Invalid format version: 0x%x\n", ta_payload->payload_hdr.format_version);
629         return false;
630     }
631 
632     return true;
633 }
634 
get_signature_verify_key(void ** key,const struct sign_config_t * config,cert_param_t * cert_param)635 static TEE_Result get_signature_verify_key(void **key, const struct sign_config_t *config, cert_param_t *cert_param)
636 {
637     TEE_Result ret;
638     ta_payload_layer_t *ta_payload = get_ta_payload();
639 
640     struct ta_verify_key verify_key = { config->key_len, config->key_style, NULL };
641 
642     if (ta_payload->payload_hdr.ta_conf_size != 0) {
643         if (ta_local_sign_check()) {
644             *key = &(cert_param->public_key);
645             return TEE_SUCCESS;
646         } else {
647             if (cert_param->cert_type == TA_DEBUG_CERT) {
648                 *key = &(cert_param->public_key);
649                 return TEE_SUCCESS;
650             } else {
651                 ret = get_ta_verify_pubkey(&verify_key);
652             }
653         }
654     } else {
655         ret = get_ta_verify_pubkey(&verify_key);
656     }
657 
658     if (ret != TEE_SUCCESS)
659         return ret;
660 
661     if (verify_key.key == NULL)
662         return TEE_ERROR_BAD_PARAMETERS;
663 
664     if (is_keywest_signature() && ta_payload->payload_hdr.ta_conf_size == 0) {
665         *key = TEE_Malloc(sizeof(ecc_pub_key_t), 0);
666         if (*key == NULL) {
667             tloge("malloc failed!");
668             return TEE_ERROR_OUT_OF_MEMORY;
669         }
670         if (memcpy_s(*key, sizeof(ecc_pub_key_t), verify_key.key, sizeof(ecc_pub_key_t)) != 0) {
671             TEE_Free(*key);
672             *key = NULL;
673             return TEE_ERROR_GENERIC;
674         }
675     } else {
676         *key = (rsa_pub_key_t *)verify_key.key;
677     }
678     return TEE_SUCCESS;
679 }
680 
ecc_signature_verify(uint8_t * signature,uint32_t signature_size,uint8_t * hash,uint32_t hash_size,ecc_pub_key_t * key)681 static int32_t ecc_signature_verify(uint8_t *signature, uint32_t signature_size, uint8_t *hash,
682     uint32_t hash_size, ecc_pub_key_t *key)
683 {
684     uint32_t i = 0;
685     for (; i < signature_size; i++) {
686         if (signature[i] == 0x00)
687             continue;
688         break;
689     }
690     return ecc_verify_digest(signature + i, signature_size - i, hash, hash_size, key);
691 }
692 
693 #define SIGNATURE_OK 1
do_ta_image_verify(uint8_t * signature,uint32_t signature_size,uint8_t * hash,const struct sign_config_t * config,void ** key)694 static TEE_Result do_ta_image_verify(uint8_t *signature, uint32_t signature_size, uint8_t *hash,
695     const struct sign_config_t *config, void **key)
696 {
697     int32_t result;
698     ta_payload_layer_t *ta_payload = get_ta_payload();
699 
700     if (is_keywest_signature() || config->sign_ta_alg == SIGN_SEC_ALG_ECDSA) {
701         result = ecc_signature_verify(signature, signature_size, hash, config->hash_size, (ecc_pub_key_t *)*key);
702         if (ta_payload->payload_hdr.ta_conf_size == 0) {
703             TEE_Free(*key);
704             *key = NULL;
705         }
706         if (result != SIGNATURE_OK) {
707             tloge("verify digest failed:%d\n", result);
708             return TEE_ERROR_GENERIC;
709         }
710     } else {
711         result = rsa_verify_digest(signature, signature_size, hash, config->hash_size,
712             (rsa_pub_key_t *)*key, config->hash_size, config->hash_nid, config->padding);
713         if (result != 0) {
714             tloge("verify digest failed:%d\n", result);
715             return TEE_ERROR_GENERIC;
716         }
717     }
718 
719     tlogd("signature VerifyDigest success\n");
720     return TEE_SUCCESS;
721 }
722 
print_ta_sign_algorithm_info(const struct sign_config_t * config)723 static void print_ta_sign_algorithm_info(const struct sign_config_t *config)
724 {
725     if (config == NULL)
726         return;
727 
728     ta_cipher_layer_t *ta_cipher_layer = get_ta_cipher_layer();
729 
730     tloge("sec config info:sign_alg=0x%x, key_len=%u, hash_size=%zu, hash_padding=%s, key_style=%s\n",
731         ta_cipher_layer->cipher_hdr.signature_alg, config->key_len, config->hash_size,
732         config->padding == RSA_PKCS1_PSS_PADDING ? "PKCS1_PSS" : "PKCS1",
733         config->key_style == PUB_KEY_RELEASE ? "release" : "debug");
734 }
735 
tee_secure_img_signature_verify(const uint8_t * plaintext_payload,uint32_t plaintext_size,uint8_t * signature,uint32_t signature_size,elf_hash_data * hash_data)736 TEE_Result tee_secure_img_signature_verify(const uint8_t *plaintext_payload, uint32_t plaintext_size,
737     uint8_t *signature, uint32_t signature_size, elf_hash_data *hash_data)
738 {
739     if (plaintext_payload == NULL || signature == NULL || plaintext_size == 0 || signature_size == 0)
740         return TEE_ERROR_BAD_PARAMETERS;
741 
742     uint8_t hash[HASH_LEN_MAX] = {0};
743     struct sign_config_t config = {0};
744     void *key = NULL;
745     cert_param_t cert_param = {0};
746 
747     get_sign_config(&config);
748 
749     if (!check_img_format_valid(&config))
750         return TEE_ERROR_NOT_SUPPORTED;
751 
752     TEE_Result ret = tee_secure_img_hash_ops(plaintext_payload, plaintext_size, hash, config.hash_size);
753     if (ret != TEE_SUCCESS) {
754         print_ta_sign_algorithm_info(&config);
755         return ret;
756     }
757 
758     ret = get_config_cert_param(&cert_param, &config);
759     if (ret != TEE_SUCCESS) {
760         print_ta_sign_algorithm_info(&config);
761         return ret;
762     }
763 
764     /* This is for 3rd party to developing TA with signature check off */
765     if (get_ta_signature_ctrl()) {
766         tloge("DEBUG_VERSION: signature VerifyDigest is OFF\n");
767         return TEE_SUCCESS;
768     }
769 
770     ret = get_signature_verify_key(&key, &config, &cert_param);
771     if (ret != TEE_SUCCESS) {
772         print_ta_sign_algorithm_info(&config);
773         return ret;
774     }
775 
776     if (key == NULL)
777         return TEE_ERROR_IMG_VERIFY_FAIL;
778 
779     ret = do_ta_image_verify(signature, signature_size, hash, &config, &key);
780     if (ret != TEE_SUCCESS) {
781         print_ta_sign_algorithm_info(&config);
782         return ret;
783     }
784     /* copy hash data out of this func if hash_data buffer is not NULL */
785     copy_hash_data(hash_data, hash, config.hash_size);
786 
787     return TEE_SUCCESS;
788 }
789 
tee_secure_img_proc_cipher_layer(uint8_t * img_buf,uint32_t img_size,uint32_t * off_set,uint32_t * layer_size)790 static TEE_Result tee_secure_img_proc_cipher_layer(uint8_t *img_buf, uint32_t img_size,
791     uint32_t *off_set, uint32_t *layer_size)
792 {
793     /* Locate the position of cipher layer */
794     if (overflow_check(*off_set, g_v3_cipher_layer_len))
795         return TEE_ERROR_GENERIC;
796     if (boundary_check(img_size, *off_set + g_v3_cipher_layer_len))
797         return TEE_ERROR_GENERIC;
798 
799     uint8_t *cipher_layer = img_buf + *off_set;
800     TEE_Result ret;
801 
802     /* Decrypt cipher layer */
803     uint8_t *plaintext_layer = cipher_layer;
804     if (g_is_encrypted_sec) {
805         ret = tee_secure_img_decrypt_cipher_layer(cipher_layer, g_v3_cipher_layer_len, plaintext_layer, layer_size);
806         if (ret != TEE_SUCCESS)
807             return ret;
808     } else {
809         *layer_size = g_v3_cipher_layer_len;
810     }
811 
812     /* Parse cipher layer to get IV, AES key & signature algorithm */
813     ret = tee_secure_img_parse_cipher_layer(plaintext_layer, *layer_size, &g_ta_cipher_layer);
814     if (ret != TEE_SUCCESS)
815         return ret;
816 
817     *off_set += g_v3_cipher_layer_len;
818     return TEE_SUCCESS;
819 }
820 
tee_secure_img_proc_payload(uint8_t * img_buf,uint32_t img_size,uint32_t off_set,uint32_t layer_size,uint32_t * plaintext_size)821 static TEE_Result tee_secure_img_proc_payload(uint8_t *img_buf, uint32_t img_size,
822     uint32_t off_set, uint32_t layer_size, uint32_t *plaintext_size)
823 {
824     uint8_t *ciphertext_payload = NULL;
825     uint32_t ciphertext_size = *plaintext_size;
826     TEE_Result ret;
827     ta_payload_layer_t *ta_payload = get_ta_payload();
828 
829     /* Locate the position of image payload encrypted in AES algorithm */
830     if (overflow_check(off_set, ciphertext_size))
831         return TEE_ERROR_GENERIC;
832     if (boundary_check(img_size, off_set + ciphertext_size))
833         return TEE_ERROR_GENERIC;
834     ciphertext_payload = img_buf + off_set;
835 
836     /* Decrypt ciphertext payload */
837     uint8_t *plaintext_payload = ciphertext_payload;
838     if (g_is_encrypted_sec) {
839         ret = tee_secure_img_decrypt_payload(ciphertext_payload, ciphertext_size, plaintext_payload, plaintext_size);
840         if (ret != TEE_SUCCESS)
841             return TEE_ERROR_BAD_FORMAT;
842     } else {
843         *plaintext_size = ciphertext_size;
844     }
845 
846     /* Move identity layer, crypto layer & payload together to calculate the hash */
847     if (overflow_check(sizeof(ta_image_hdr_v3_t), layer_size))
848         return TEE_ERROR_GENERIC;
849     if (overflow_check(sizeof(ta_image_hdr_v3_t) + layer_size, *plaintext_size))
850         return TEE_ERROR_GENERIC;
851     if (boundary_check(img_size, sizeof(ta_image_hdr_v3_t) + layer_size + *plaintext_size))
852         return TEE_ERROR_GENERIC;
853     errno_t rc = memmove_s(img_buf + sizeof(ta_image_hdr_v3_t) + layer_size,
854         img_size - sizeof(ta_image_hdr_v3_t) - layer_size, plaintext_payload, *plaintext_size);
855     if (rc != EOK)
856         return TEE_ERROR_SECURITY;
857 
858     plaintext_payload = img_buf + sizeof(ta_image_hdr_v3_t) + layer_size;
859     /* Parse plaintext payload */
860     return tee_secure_img_parse_payload(plaintext_payload, *plaintext_size, ta_payload);
861 }
862 
863 /*
864  * Process steps:
865  * 1, find cipher layer,
866  * 2, decrypt cipher layer,
867  * 3, parse cipher layer
868  * 4, find and copy signature
869  * 5, find payload
870  * 6, decrypt payload
871  * 7, Move the hash context together
872  * 8, parse payload: including parse stand manifest & manifest extension
873  * 9, verify signature: including set config and get public key for V3.1
874  */
tee_secure_img_unpack_v3(uint32_t rsa_algo_byte_len,uint32_t ta_hd_len,uint8_t * img_buf,uint32_t img_size,elf_hash_data * hash_data)875 TEE_Result tee_secure_img_unpack_v3(uint32_t rsa_algo_byte_len,
876     uint32_t ta_hd_len, uint8_t *img_buf, uint32_t img_size, elf_hash_data *hash_data)
877 {
878     (void)rsa_algo_byte_len;
879     (void)ta_hd_len;
880     if (img_buf == NULL || hash_data == NULL)
881         return TEE_ERROR_BAD_PARAMETERS;
882 
883     uint8_t *signature = NULL;
884     uint32_t signature_size;
885     uint32_t layer_size = g_v3_cipher_layer_len;
886     uint32_t off_set = 0;
887 
888     /* Skip image identity layer */
889     if (overflow_check(off_set, sizeof(ta_image_hdr_v3_t)))
890         return TEE_ERROR_GENERIC;
891     if (boundary_check(img_size, off_set + sizeof(ta_image_hdr_v3_t)))
892         return TEE_ERROR_GENERIC;
893     off_set += sizeof(ta_image_hdr_v3_t);
894 
895     TEE_Result ret = tee_secure_img_proc_cipher_layer(img_buf, img_size, &off_set, &layer_size);
896     if (ret != TEE_SUCCESS)
897         return ret;
898 
899     /* Get signature size according to signature algorithm */
900     signature_size = tee_secure_img_get_signature_size(g_ta_cipher_layer.cipher_hdr.signature_alg,
901         img_buf + off_set, img_size - off_set);
902     if (signature_size == SIGNATURE_SIZE_INVALID)
903         return TEE_ERROR_BAD_FORMAT;
904 
905     /* Locate the position of signatue */
906     if (overflow_check(off_set, signature_size))
907         return TEE_ERROR_GENERIC;
908     if (boundary_check(img_size, off_set + signature_size))
909         return TEE_ERROR_GENERIC;
910     signature = TEE_Malloc(signature_size, 0);
911     if (signature == NULL) {
912         tloge("malloc signature failed, signature size = 0x%x\n", signature_size);
913         return TEE_ERROR_OUT_OF_MEMORY;
914     }
915     if (memcpy_s(signature, signature_size, img_buf + off_set, signature_size) != EOK) {
916         TEE_Free(signature);
917         return TEE_ERROR_SECURITY;
918     }
919     off_set += signature_size;
920 
921     /* Calculate the size of image payload */
922     uint32_t plaintext_size = g_image_hdr_v3.context_len - g_v3_cipher_layer_len - signature_size;
923     ret = tee_secure_img_proc_payload(img_buf, img_size, off_set, layer_size, &plaintext_size);
924     if (ret != TEE_SUCCESS) {
925         TEE_Free(signature);
926         return ret;
927     }
928 
929     /* Verify signature */
930     ret = tee_secure_img_signature_verify(img_buf, sizeof(ta_image_hdr_v3_t) + layer_size + plaintext_size,
931         signature, signature_size, hash_data);
932     TEE_Free(signature);
933     return ret;
934 }
935 
free_verify_v3(void)936 void free_verify_v3(void)
937 {
938     load_img_info *img_info = get_img_info();
939     ta_payload_layer_t *ta_payload = get_ta_payload();
940 
941     /* do NOT free, map from tafs */
942     if (img_info->img_buf != NULL) {
943         (void)munmap((void *)(uintptr_t)img_info->img_buf, get_img_size());
944         img_info->img_buf = NULL;
945     }
946 
947     if (img_info->manifest.service_name != NULL) {
948         TEE_Free(img_info->manifest.service_name);
949         img_info->manifest.service_name = NULL;
950     }
951 
952     if (g_ta_cipher_layer.key != NULL) {
953         (void)memset_s(g_ta_cipher_layer.key, g_ta_cipher_layer.cipher_hdr.key_size, 0,
954             g_ta_cipher_layer.cipher_hdr.key_size);
955         TEE_Free(g_ta_cipher_layer.key);
956         g_ta_cipher_layer.key = NULL;
957     }
958     if (g_ta_cipher_layer.iv != NULL) {
959         (void)memset_s(g_ta_cipher_layer.iv, g_ta_cipher_layer.cipher_hdr.iv_size, 0,
960             g_ta_cipher_layer.cipher_hdr.iv_size);
961         TEE_Free(g_ta_cipher_layer.iv);
962         g_ta_cipher_layer.iv = NULL;
963     }
964 
965     (void)memset_s(img_info, sizeof(load_img_info), 0, sizeof(load_img_info));
966     (void)memset_s(&g_image_hdr_v3, sizeof(ta_image_hdr_v3_t), 0, sizeof(ta_image_hdr_v3_t));
967     (void)memset_s(ta_payload, sizeof(ta_payload_layer_t), 0, sizeof(ta_payload_layer_t));
968     (void)memset_s(&g_ta_cipher_layer, sizeof(ta_cipher_layer_t), 0, sizeof(ta_cipher_layer_t));
969 }
970 
secure_img_copy_rsp_v3(elf_verify_reply * rep)971 TEE_Result secure_img_copy_rsp_v3(elf_verify_reply *rep)
972 {
973     if (rep == NULL)
974         return TEE_ERROR_BAD_PARAMETERS;
975 
976     load_img_info *img_info = get_img_info();
977     ta_payload_layer_t *ta_payload = get_ta_payload();
978 
979     rep->ta_property = img_info->manifest.mani_info.ta_property;
980     rep->service_name_len = img_info->manifest.mani_info.service_name_len;
981 
982     if (memcpy_s(rep->service_name, SERVICE_NAME_MAX_IN_MANIFEST,
983                  img_info->manifest.service_name, img_info->manifest.mani_info.service_name_len + 1) != 0) {
984         tloge("copy service name fail\n");
985         return TEE_ERROR_GENERIC;
986     }
987 
988     rep->payload_hdr = ta_payload->payload_hdr;
989     rep->mani_ext = img_info->manifest.ext;
990     rep->srv_uuid = img_info->manifest.srv_uuid;
991     rep->dyn_conf_registed = img_info->dyn_conf_registed;
992 
993     if (ta_payload->ta_elf)
994         rep->off_ta_elf = ((int8_t *)(ta_payload->ta_elf) - img_info->img_buf);
995     else
996         rep->off_ta_elf = INVALID_OFFSET;
997 
998     if (img_info->manifest_buf)
999         rep->off_manifest_buf = (img_info->manifest_buf - img_info->img_buf);
1000     else
1001         rep->off_manifest_buf = INVALID_OFFSET;
1002 
1003     return TEE_SUCCESS;
1004 }
1005 
1006