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
13 #include "set_teeos_key.h"
14
15 #include <string.h>
16 #include <securec.h>
17
18 #include "tlv_sharedmem.h"
19 #include "teeos_uuid.h"
20 #include "set_teeos_cfg.h"
21 #include "img_load.h"
22
23 #define KEY_INFO_MAIGIC_BASE 0x5a5aa501
24 #define CA_RSA_MAGIC 0x5a5aa501
25 #define TA_RSA_MAGIC 0x5a5aa502
26 #define TA_CERT_MAGIC 0x5a5aa503
27 #define TA_CONFIG_MAGIC 0x5a5aa504
28 #define TA_ECIES_MAGIC 0x5a5aa505
29 #define TA_WB_MAGIC 0x5a5aa506
30
31 struct key_tag_info {
32 char tag[MAX_TAG_LEN];
33 uint32_t magic;
34 };
35
36 static struct key_tag_info g_teeos_key_tag[] = {
37 {"ca_rsa_pub_key", CA_RSA_MAGIC},
38 {"ta_rsa_pub_2048release", TA_RSA_MAGIC},
39 {"ta_root_pub_key", TA_CERT_MAGIC},
40 {"ta_config_pub_key", TA_CONFIG_MAGIC},
41 {"ta_decrypt_key_eciesv3_3072", TA_ECIES_MAGIC},
42 {"ta_decrypt_key_wbv3_3072", TA_WB_MAGIC},
43 };
44
45 static TEE_UUID g_key_perm_uuid[] = {
46 TEE_SERVICE_SYSTEM,
47 };
48
49 #define OEMKEY_MAGIC 0x55AA55AA
50 #define OEMKEY_TAG "oemkey"
51 #define RES_NUM 52
52
53 struct oemkey_info {
54 uint32_t head_magic;
55 uint8_t oemkey[OEMKEY_SIZE];
56 uint8_t reserved[RES_NUM];
57 uint32_t tail_magic;
58 } __attribute__((__packed__));
59
trans_key_info_to_share_mem(struct asym_key_t * asym_key_info,void * header,struct key_tag_info * tag_info)60 static int32_t trans_key_info_to_share_mem(struct asym_key_t *asym_key_info,
61 void *header, struct key_tag_info *tag_info)
62 {
63 char *buffer = header;
64 int32_t ret;
65 struct tlv_item_data tlv_item_data;
66 if (asym_key_info->key_magic != tag_info->magic) {
67 teelog("asym_key_info->key_magic is %x, tag_info->magic is %x\n",
68 asym_key_info->key_magic, tag_info->magic);
69 return -1;
70 }
71
72 tlv_item_data.type = tag_info->tag;
73 tlv_item_data.type_size = strlen(tag_info->tag);
74 tlv_item_data.owner_list = g_key_perm_uuid;
75 tlv_item_data.owner_len = (uint32_t)sizeof(g_key_perm_uuid);
76 tlv_item_data.value = buffer + asym_key_info->key_offset;
77 tlv_item_data.value_len = asym_key_info->key_size;
78
79 ret = put_tlv_shared_mem(tlv_item_data);
80 return ret;
81 }
82
load_teeos_key_info(void * image)83 int32_t load_teeos_key_info(void *image)
84 {
85 struct secure_img_header *img_header = image;
86 struct asym_key_t *asym_key_info = img_header->teeos_key_info;
87 uint32_t i;
88
89 for (i = 0; i < ARRAY_SIZE(g_teeos_key_tag); i++) {
90 if (trans_key_info_to_share_mem(asym_key_info, image, &g_teeos_key_tag[i]) != 0)
91 teelog("trans %s sharemem failed, id is %d\n", g_teeos_key_tag[i].tag, i);
92 asym_key_info++;
93 }
94
95 return 0;
96 }
97
trans_oemkey(uint8_t * oemkey,uint32_t oemkey_size)98 int32_t trans_oemkey(uint8_t *oemkey, uint32_t oemkey_size)
99 {
100 if (oemkey_size != OEMKEY_SIZE || oemkey == NULL) {
101 teelog("oemkey_size error\n");
102 return -1;
103 }
104 struct oemkey_info oemkey_info;
105 oemkey_info.head_magic = OEMKEY_MAGIC;
106 oemkey_info.tail_magic = OEMKEY_MAGIC;
107
108 if (memcpy_s(oemkey_info.oemkey, OEMKEY_SIZE, oemkey, OEMKEY_SIZE) != EOK) {
109 teelog("oemkey_size error\n");
110 return -1;
111 }
112
113 char oemkey_tag[OEMKEY_SIZE] = OEMKEY_TAG;
114 struct tlv_item_data tlv_item_data;
115 tlv_item_data.type = oemkey_tag;
116 tlv_item_data.type_size = strlen(oemkey_tag);
117 tlv_item_data.owner_list = g_key_perm_uuid;
118 tlv_item_data.owner_len = (uint32_t)sizeof(g_key_perm_uuid);
119 tlv_item_data.value = &oemkey_info;
120 tlv_item_data.value_len = sizeof(oemkey_info);
121
122 if (put_tlv_shared_mem(tlv_item_data) != 0) {
123 teelog("put oemkey tlv failed\n");
124 return -1;
125 }
126
127 return 0;
128 }
129