1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "hvb_footer.h"
18 #include "hvb_crypto.h"
19 #include "hvb_ops.h"
20 #include "hvb_rvt.h"
21 #include "hvb_cert.h"
22 #include "hvb_sysdeps.h"
23 #include "hvb_util.h"
24 #include "hvb_cmdline.h"
25 #include "hvb.h"
26
hvb_init_verified_data(void)27 struct hvb_verified_data *hvb_init_verified_data(void)
28 {
29 struct hvb_verified_data *vd = NULL;
30
31 vd = hvb_calloc(sizeof(*vd));
32 if (!vd) {
33 hvb_print("malloc verified_data fail\n");
34 return NULL;
35 }
36
37 vd->certs = hvb_calloc(sizeof(struct hvb_cert_data) * HVB_MAX_NUMBER_OF_LOADED_CERTS);
38 if (!vd->certs) {
39 hvb_print("malloc certs fail\n");
40 hvb_free(vd);
41 return NULL;
42 }
43
44 vd->images = hvb_calloc(sizeof(struct hvb_image_data) * HVB_MAX_NUMBER_OF_LOADED_IMAGES);
45 if (!vd->images) {
46 hvb_print("malloc images fail\n");
47 hvb_free(vd->certs);
48 hvb_free(vd);
49 return NULL;
50 }
51
52 vd->num_loaded_certs = 0;
53 vd->num_loaded_images = 0;
54
55 vd->cmdline.buf = hvb_calloc(CMD_LINE_SIZE);
56 if (!vd->cmdline.buf) {
57 hvb_print("malloc cmdline fail\n");
58 hvb_free(vd->images);
59 hvb_free(vd->certs);
60 hvb_free(vd);
61 return NULL;
62 }
63
64 vd->cmdline.cur_pos = 0;
65 vd->cmdline.max_size = CMD_LINE_SIZE;
66
67 vd->key_len = 0;
68
69 return vd;
70 }
71
hvb_rvt_verify_root(struct hvb_ops * ops,const char * ptn,const char * const * ptn_list,struct hvb_verified_data * vd)72 static enum hvb_errno hvb_rvt_verify_root(struct hvb_ops *ops, const char *ptn,
73 const char *const *ptn_list,
74 struct hvb_verified_data *vd)
75 {
76 enum hvb_errno ret = HVB_OK;
77 enum hvb_io_errno io_ret = HVB_IO_OK;
78 bool is_trusted = false;
79 struct hvb_buf cert_pubk = {0};
80
81 ret = footer_init_desc(ops, ptn, ptn_list, &cert_pubk, vd);
82 if (ret != HVB_OK) {
83 hvb_printv("error verity partition: ", ptn, "\n", NULL);
84 goto fail;
85 }
86
87 io_ret = ops->valid_rvt_key(ops, cert_pubk.addr, cert_pubk.size, NULL, 0, &is_trusted);
88 if (io_ret != HVB_IO_OK) {
89 ret = HVB_ERROR_PUBLIC_KEY_REJECTED;
90 hvb_print("error, rvt public key invalid\n");
91 goto fail;
92 }
93
94 if (is_trusted == false) {
95 ret = HVB_ERROR_PUBLIC_KEY_REJECTED;
96 hvb_print("error, rvt public key rejected\n");
97 goto fail;
98 }
99
100 fail:
101 return ret;
102 }
103
hvb_get_partition_image(struct hvb_verified_data * vd,const char * ptn)104 static struct hvb_buf *hvb_get_partition_image(struct hvb_verified_data *vd, const char *ptn)
105 {
106 uint32_t name_len = hvb_strlen(ptn);
107 struct hvb_image_data *p = vd->images;
108 struct hvb_image_data *end = p + vd->num_loaded_images;
109
110 for (; p < end; p++) {
111 if (hvb_memcmp(ptn, p->partition_name, name_len) == 0)
112 return &p->data;
113 }
114
115 return NULL;
116 }
117
hvb_buf_equal(const struct hvb_buf * buf1,const struct hvb_buf * buf2)118 bool hvb_buf_equal(const struct hvb_buf *buf1, const struct hvb_buf *buf2)
119 {
120 return buf1->size == buf2->size && hvb_memcmp(buf1->addr, buf2->addr, buf1->size) == 0;
121 }
122
hvb_walk_verify_nodes(struct hvb_ops * ops,const char * const * ptn_list,struct hvb_buf * rvt,struct hvb_verified_data * vd)123 static enum hvb_errno hvb_walk_verify_nodes(struct hvb_ops *ops, const char *const *ptn_list,
124 struct hvb_buf *rvt, struct hvb_verified_data *vd)
125 {
126 enum hvb_errno ret = HVB_OK;
127 uint32_t i, nodes_num;
128 struct hvb_buf pubk_descs;
129 struct rvt_pubk_desc desc;
130 struct hvb_buf expected_pubk;
131 struct hvb_buf cert_pubk;
132 struct rvt_image_header header;
133 uint64_t desc_size;
134
135 desc_size = sizeof(desc) - (PUBKEY_LEN - vd->key_len);
136 ret = hvb_rvt_head_parser(rvt, &header, desc_size);
137 if (ret != HVB_OK) {
138 hvb_print("error, parse rvt header.\n");
139 goto fail;
140 }
141
142 nodes_num = header.verity_num;
143 ret = hvb_rvt_get_pubk_desc(rvt, &pubk_descs);
144 if (ret != HVB_OK) {
145 hvb_print("error, pubk descs.\n");
146 goto fail;
147 }
148
149 for (i = 0; i < nodes_num; i++) {
150 ret = hvb_rvt_pubk_desc_parser(&pubk_descs, &desc, desc_size);
151 if (ret != HVB_OK) {
152 hvb_print("errror, parser rvt k descs\n");
153 goto fail;
154 }
155
156 ret = hvb_rvt_get_pubk_buf(&expected_pubk, rvt, &desc);
157 if (ret != HVB_OK) {
158 hvb_print("errror, get pubk buf\n");
159 goto fail;
160 }
161
162 ret = footer_init_desc(ops, &desc.name[0], ptn_list, &cert_pubk, vd);
163 if (ret != HVB_OK) {
164 hvb_printv("error, verity partition: ", desc.name, "\n", NULL);
165 goto fail;
166 }
167
168 if (hvb_buf_equal(&expected_pubk, &cert_pubk) != true) {
169 ret = HVB_ERROR_PUBLIC_KEY_REJECTED;
170 hvb_print("error, compare public key\n");
171 goto fail;
172 }
173
174 pubk_descs.addr += desc_size;
175 }
176
177 fail:
178 return ret;
179 }
180
hash_ptn_list_add_rvt(const char * const * hash_ptn_list,const char * rvt_ptn)181 static char const **hash_ptn_list_add_rvt(const char *const *hash_ptn_list, const char *rvt_ptn)
182 {
183 size_t n;
184 bool need_add_rvt = true;
185 char const **ptn = NULL;
186 size_t num_parttions = 0;
187
188 if (hash_ptn_list != NULL) {
189 while (hash_ptn_list[num_parttions] != NULL) {
190 num_parttions++;
191 }
192 }
193
194 num_parttions += REQUEST_LIST_LEN;
195
196 ptn = (char const **)hvb_calloc(num_parttions * sizeof(char *));
197 if (ptn == NULL) {
198 hvb_print("error, alloc ptn\n");
199 return NULL;
200 }
201
202 for (n = 0; n < num_parttions - REQUEST_LIST_LEN; n++) {
203 ptn[n] = hash_ptn_list[n];
204 if (hvb_strcmp(ptn[n], rvt_ptn) == 0) {
205 need_add_rvt = false;
206 }
207 }
208
209 if (need_add_rvt) {
210 ptn[num_parttions - REQUEST_LIST_LEN] = rvt_ptn;
211 }
212
213 return ptn;
214 }
215
hvb_chain_verify(struct hvb_ops * ops,const char * rvt_ptn,const char * const * hash_ptn_list,struct hvb_verified_data ** out_vd)216 enum hvb_errno hvb_chain_verify(struct hvb_ops *ops,
217 const char *rvt_ptn,
218 const char *const *hash_ptn_list,
219 struct hvb_verified_data **out_vd)
220 {
221 enum hvb_errno ret = HVB_OK;
222 struct hvb_buf *rvt_image = NULL;
223 struct hvb_verified_data *vd = NULL;
224 char const **ptn_list = NULL;
225
226 if (out_vd != NULL) {
227 *out_vd = NULL;
228 }
229
230 hvb_return_hvb_err_if_null(ops);
231 hvb_return_hvb_err_if_null(rvt_ptn);
232
233 ptn_list = hash_ptn_list_add_rvt(hash_ptn_list, rvt_ptn);
234 if (ptn_list == NULL) {
235 hvb_print("error, add rvt\n");
236 ret = HVB_ERROR_OOM;
237 goto fail;
238 }
239
240 vd = hvb_init_verified_data();
241 if (!vd) {
242 hvb_print("malloc verified_data fail\n");
243 ret = HVB_ERROR_OOM;
244 goto fail;
245 }
246
247 /* verity rvt cert */
248 ret = hvb_rvt_verify_root(ops, rvt_ptn, ptn_list, vd);
249 if (ret != HVB_OK) {
250 hvb_print("error, verity rvt partition.\n");
251 goto fail;
252 }
253
254 /* get rvt image */
255 rvt_image = hvb_get_partition_image(vd, rvt_ptn);
256 if (!rvt_image) {
257 hvb_print("error, get rvt ptn.\n");
258 ret = HVB_ERROR_OOM;
259 goto fail;
260 }
261
262 /* walk verify all nodes from rvt */
263 ret = hvb_walk_verify_nodes(ops, ptn_list, rvt_image, vd);
264 if (ret != HVB_OK) {
265 hvb_print("error, walk nodes.\n");
266 goto fail;
267 }
268
269 /* creat cmdline info */
270 ret = hvb_creat_cmdline(ops, vd);
271 if (ret != HVB_OK) {
272 hvb_print("error, create cmdline.\n");
273 goto fail;
274 }
275
276 /* if out_vd is NULL, no need out slot data, we just hvb_free */
277 if (out_vd == NULL) {
278 hvb_chain_verify_data_free(vd);
279 } else {
280 *out_vd = vd;
281 }
282
283 fail:
284 if (vd != NULL && ret != HVB_OK) {
285 hvb_chain_verify_data_free(vd);
286 }
287
288 hvb_free(ptn_list);
289
290 return ret;
291 }
292
hvb_chain_verify_data_free(struct hvb_verified_data * vd)293 void hvb_chain_verify_data_free(struct hvb_verified_data *vd)
294 {
295 uint64_t n;
296
297 if (vd == NULL) {
298 hvb_print("vd is NULL, do nothing\n");
299 return;
300 }
301
302 for (n = 0; n < vd->num_loaded_certs && vd->certs; n++) {
303 if (vd->certs[n].data.addr != NULL)
304 hvb_free(vd->certs[n].data.addr);
305
306 if (vd->certs[n].partition_name != NULL) {
307 hvb_free(vd->certs[n].partition_name);
308 }
309 }
310
311 if (vd->certs != NULL) {
312 hvb_free(vd->certs);
313 }
314
315 for (n = 0; n < vd->num_loaded_images && vd->images; n++) {
316 if (vd->images[n].data.addr != NULL)
317 hvb_free(vd->images[n].data.addr);
318
319 if (vd->images[n].partition_name != NULL)
320 hvb_free(vd->images[n].partition_name);
321 }
322
323 if (vd->images != NULL) {
324 hvb_free(vd->images);
325 }
326
327 if (vd->cmdline.buf != NULL) {
328 hvb_free(vd->cmdline.buf);
329 }
330
331 hvb_memset((uint8_t *)vd, 0, sizeof(*vd));
332 }
333