AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | */ |
| 5 | |
| 6 | #ifdef USE_HOSTCC |
| 7 | #include "mkimage.h" |
| 8 | #include <time.h> |
| 9 | #else |
| 10 | #include <common.h> |
| 11 | #include <malloc.h> |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | #endif /* !USE_HOSTCC*/ |
| 14 | #include <image.h> |
| 15 | #include <u-boot/rsa.h> |
| 16 | #include <u-boot/rsa-checksum.h> |
| 17 | |
| 18 | #define IMAGE_MAX_HASHED_NODES 100 |
| 19 | |
| 20 | #ifdef USE_HOSTCC |
| 21 | void *host_blob; |
| 22 | |
| 23 | void image_set_host_blob(void *blob) |
| 24 | { |
| 25 | host_blob = blob; |
| 26 | } |
| 27 | |
| 28 | void *image_get_host_blob(void) |
| 29 | { |
| 30 | return host_blob; |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | /** |
| 35 | * fit_region_make_list() - Make a list of image regions |
| 36 | * |
| 37 | * Given a list of fdt_regions, create a list of image_regions. This is a |
| 38 | * simple conversion routine since the FDT and image code use different |
| 39 | * structures. |
| 40 | * |
| 41 | * @fit: FIT image |
| 42 | * @fdt_regions: Pointer to FDT regions |
| 43 | * @count: Number of FDT regions |
| 44 | * @region: Pointer to image regions, which must hold @count records. If |
| 45 | * region is NULL, then (except for an SPL build) the array will be |
| 46 | * allocated. |
| 47 | * @return: Pointer to image regions |
| 48 | */ |
| 49 | struct image_region *fit_region_make_list(const void *fit, |
| 50 | struct fdt_region *fdt_regions, |
| 51 | int count, |
| 52 | struct image_region *region) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | debug("Hash regions:\n"); |
| 57 | debug("%10s %10s\n", "Offset", "Size"); |
| 58 | |
| 59 | /* |
| 60 | * Use malloc() except in SPL (to save code size). In SPL the caller |
| 61 | * must allocate the array. |
| 62 | */ |
| 63 | #ifndef CONFIG_SPL_BUILD |
| 64 | if (!region) |
| 65 | region = calloc(sizeof(*region), count); |
| 66 | #endif |
| 67 | if (!region) |
| 68 | return NULL; |
| 69 | for (i = 0; i < count; i++) { |
| 70 | debug("%10x %10x\n", fdt_regions[i].offset, |
| 71 | fdt_regions[i].size); |
| 72 | region[i].data = fit + fdt_regions[i].offset; |
| 73 | region[i].size = fdt_regions[i].size; |
| 74 | } |
| 75 | |
| 76 | return region; |
| 77 | } |
| 78 | |
| 79 | static int fit_image_setup_verify(struct image_sign_info *info, |
| 80 | const void *fit, int noffset, |
| 81 | int required_keynode, char **err_msgp) |
| 82 | { |
| 83 | char *algo_name; |
| 84 | const char *padding_name; |
| 85 | |
| 86 | if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) { |
| 87 | *err_msgp = "Total size too large"; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 92 | *err_msgp = "Can't get hash algo property"; |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 97 | if (!padding_name) |
| 98 | padding_name = RSA_DEFAULT_PADDING_NAME; |
| 99 | |
| 100 | memset(info, '\0', sizeof(*info)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 101 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 102 | info->fit = (void *)fit; |
| 103 | info->node_offset = noffset; |
| 104 | info->name = algo_name; |
| 105 | info->checksum = image_get_checksum_algo(algo_name); |
| 106 | info->crypto = image_get_crypto_algo(algo_name); |
| 107 | info->padding = image_get_padding_algo(padding_name); |
| 108 | info->fdt_blob = gd_fdt_blob(); |
| 109 | info->required_keynode = required_keynode; |
| 110 | printf("%s:%s", algo_name, info->keyname); |
| 111 | |
| 112 | if (!info->checksum || !info->crypto || !info->padding) { |
| 113 | *err_msgp = "Unknown signature algorithm"; |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int fit_image_check_sig(const void *fit, int noffset, const void *data, |
| 121 | size_t size, int required_keynode, char **err_msgp) |
| 122 | { |
| 123 | struct image_sign_info info; |
| 124 | struct image_region region; |
| 125 | uint8_t *fit_value; |
| 126 | int fit_value_len; |
| 127 | |
| 128 | *err_msgp = NULL; |
| 129 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 130 | err_msgp)) |
| 131 | return -1; |
| 132 | |
| 133 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 134 | &fit_value_len)) { |
| 135 | *err_msgp = "Can't get hash value property"; |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | region.data = data; |
| 140 | region.size = size; |
| 141 | |
| 142 | if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) { |
| 143 | *err_msgp = "Verification failed"; |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int fit_image_verify_sig(const void *fit, int image_noffset, |
| 151 | const char *data, size_t size, |
| 152 | const void *sig_blob, int sig_offset) |
| 153 | { |
| 154 | int noffset; |
| 155 | char *err_msg = ""; |
| 156 | int verified = 0; |
| 157 | int ret; |
| 158 | |
| 159 | /* Process all hash subnodes of the component image node */ |
| 160 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
| 161 | const char *name = fit_get_name(fit, noffset, NULL); |
| 162 | |
| 163 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 164 | strlen(FIT_SIG_NODENAME))) { |
| 165 | ret = fit_image_check_sig(fit, noffset, data, |
| 166 | size, -1, &err_msg); |
| 167 | if (ret) { |
| 168 | puts("- "); |
| 169 | } else { |
| 170 | puts("+ "); |
| 171 | verified = 1; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 178 | err_msg = "Corrupted or truncated tree"; |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | return verified ? 0 : -EPERM; |
| 183 | |
| 184 | error: |
| 185 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
| 186 | err_msg, fit_get_name(fit, noffset, NULL), |
| 187 | fit_get_name(fit, image_noffset, NULL)); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | int fit_image_verify_required_sigs(const void *fit, int image_noffset, |
| 192 | const char *data, size_t size, |
| 193 | const void *sig_blob, int *no_sigsp) |
| 194 | { |
| 195 | int verify_count = 0; |
| 196 | int noffset; |
| 197 | int sig_node; |
| 198 | |
| 199 | /* Work out what we need to verify */ |
| 200 | *no_sigsp = 1; |
| 201 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 202 | if (sig_node < 0) { |
| 203 | debug("%s: No signature node found: %s\n", __func__, |
| 204 | fdt_strerror(sig_node)); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
| 209 | const char *required; |
| 210 | int ret; |
| 211 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 212 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 213 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 214 | if (!required || strcmp(required, "image")) |
| 215 | continue; |
| 216 | ret = fit_image_verify_sig(fit, image_noffset, data, size, |
| 217 | sig_blob, noffset); |
| 218 | if (ret) { |
| 219 | printf("Failed to verify required signature '%s'\n", |
| 220 | fit_get_name(sig_blob, noffset, NULL)); |
| 221 | return ret; |
| 222 | } |
| 223 | verify_count++; |
| 224 | } |
| 225 | |
| 226 | if (verify_count) |
| 227 | *no_sigsp = 0; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 232 | /** |
| 233 | * fit_config_check_sig() - Check the signature of a config |
| 234 | * |
| 235 | * @fit: FIT to check |
| 236 | * @noffset: Offset of configuration node (e.g. /configurations/conf-1) |
| 237 | * @required_keynode: Offset in the control FDT of the required key node, |
| 238 | * if any. If this is given, then the configuration wil not |
| 239 | * pass verification unless that key is used. If this is |
| 240 | * -1 then any signature will do. |
| 241 | * @conf_noffset: Offset of the configuration subnode being checked (e.g. |
| 242 | * /configurations/conf-1/kernel) |
| 243 | * @err_msgp: In the event of an error, this will be pointed to a |
| 244 | * help error string to display to the user. |
| 245 | * @return 0 if all verified ok, <0 on error |
| 246 | */ |
| 247 | static int fit_config_check_sig(const void *fit, int noffset, |
| 248 | int required_keynode, int conf_noffset, |
| 249 | char **err_msgp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 250 | { |
| 251 | char * const exc_prop[] = {"data"}; |
| 252 | const char *prop, *end, *name; |
| 253 | struct image_sign_info info; |
| 254 | const uint32_t *strings; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 255 | const char *config_name; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 256 | uint8_t *fit_value; |
| 257 | int fit_value_len; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 258 | bool found_config; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 259 | int max_regions; |
| 260 | int i, prop_len; |
| 261 | char path[200]; |
| 262 | int count; |
| 263 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 264 | config_name = fit_get_name(fit, conf_noffset, NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 265 | debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(), |
| 266 | fit_get_name(fit, noffset, NULL), |
| 267 | fit_get_name(gd_fdt_blob(), required_keynode, NULL)); |
| 268 | *err_msgp = NULL; |
| 269 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 270 | err_msgp)) |
| 271 | return -1; |
| 272 | |
| 273 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 274 | &fit_value_len)) { |
| 275 | *err_msgp = "Can't get hash value property"; |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | /* Count the number of strings in the property */ |
| 280 | prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len); |
| 281 | end = prop ? prop + prop_len : prop; |
| 282 | for (name = prop, count = 0; name < end; name++) |
| 283 | if (!*name) |
| 284 | count++; |
| 285 | if (!count) { |
| 286 | *err_msgp = "Can't get hashed-nodes property"; |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') { |
| 291 | *err_msgp = "hashed-nodes property must be null-terminated"; |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | /* Add a sanity check here since we are using the stack */ |
| 296 | if (count > IMAGE_MAX_HASHED_NODES) { |
| 297 | *err_msgp = "Number of hashed nodes exceeds maximum"; |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | /* Create a list of node names from those strings */ |
| 302 | char *node_inc[count]; |
| 303 | |
| 304 | debug("Hash nodes (%d):\n", count); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 305 | found_config = false; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 306 | for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) { |
| 307 | debug(" '%s'\n", name); |
| 308 | node_inc[i] = (char *)name; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 309 | if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) && |
| 310 | name[sizeof(FIT_CONFS_PATH) - 1] == '/' && |
| 311 | !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) { |
| 312 | debug(" (found config node %s)", config_name); |
| 313 | found_config = true; |
| 314 | } |
| 315 | } |
| 316 | if (!found_config) { |
| 317 | *err_msgp = "Selected config not in hashed nodes"; |
| 318 | return -1; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Each node can generate one region for each sub-node. Allow for |
| 323 | * 7 sub-nodes (hash-1, signature-1, etc.) and some extra. |
| 324 | */ |
| 325 | max_regions = 20 + count * 7; |
| 326 | struct fdt_region fdt_regions[max_regions]; |
| 327 | |
| 328 | /* Get a list of regions to hash */ |
| 329 | count = fdt_find_regions(fit, node_inc, count, |
| 330 | exc_prop, ARRAY_SIZE(exc_prop), |
| 331 | fdt_regions, max_regions - 1, |
| 332 | path, sizeof(path), 0); |
| 333 | if (count < 0) { |
| 334 | *err_msgp = "Failed to hash configuration"; |
| 335 | return -1; |
| 336 | } |
| 337 | if (count == 0) { |
| 338 | *err_msgp = "No data to hash"; |
| 339 | return -1; |
| 340 | } |
| 341 | if (count >= max_regions - 1) { |
| 342 | *err_msgp = "Too many hash regions"; |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | /* Add the strings */ |
| 347 | strings = fdt_getprop(fit, noffset, "hashed-strings", NULL); |
| 348 | if (strings) { |
| 349 | /* |
| 350 | * The strings region offset must be a static 0x0. |
| 351 | * This is set in tool/image-host.c |
| 352 | */ |
| 353 | fdt_regions[count].offset = fdt_off_dt_strings(fit); |
| 354 | fdt_regions[count].size = fdt32_to_cpu(strings[1]); |
| 355 | count++; |
| 356 | } |
| 357 | |
| 358 | /* Allocate the region list on the stack */ |
| 359 | struct image_region region[count]; |
| 360 | |
| 361 | fit_region_make_list(fit, fdt_regions, count, region); |
| 362 | if (info.crypto->verify(&info, region, count, fit_value, |
| 363 | fit_value_len)) { |
| 364 | *err_msgp = "Verification failed"; |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int fit_config_verify_sig(const void *fit, int conf_noffset, |
| 372 | const void *sig_blob, int sig_offset) |
| 373 | { |
| 374 | int noffset; |
| 375 | char *err_msg = ""; |
| 376 | int verified = 0; |
| 377 | int ret; |
| 378 | |
| 379 | /* Process all hash subnodes of the component conf node */ |
| 380 | fdt_for_each_subnode(noffset, fit, conf_noffset) { |
| 381 | const char *name = fit_get_name(fit, noffset, NULL); |
| 382 | |
| 383 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 384 | strlen(FIT_SIG_NODENAME))) { |
| 385 | ret = fit_config_check_sig(fit, noffset, sig_offset, |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 386 | conf_noffset, &err_msg); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 387 | if (ret) { |
| 388 | puts("- "); |
| 389 | } else { |
| 390 | puts("+ "); |
| 391 | verified = 1; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 398 | err_msg = "Corrupted or truncated tree"; |
| 399 | goto error; |
| 400 | } |
| 401 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 402 | if (verified) |
| 403 | return 0; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 404 | |
| 405 | error: |
| 406 | printf(" error!\n%s for '%s' hash node in '%s' config node\n", |
| 407 | err_msg, fit_get_name(fit, noffset, NULL), |
| 408 | fit_get_name(fit, conf_noffset, NULL)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 409 | return -EPERM; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | int fit_config_verify_required_sigs(const void *fit, int conf_noffset, |
| 413 | const void *sig_blob) |
| 414 | { |
| 415 | int noffset; |
| 416 | int sig_node; |
| 417 | |
| 418 | /* Work out what we need to verify */ |
| 419 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 420 | if (sig_node < 0) { |
| 421 | debug("%s: No signature node found: %s\n", __func__, |
| 422 | fdt_strerror(sig_node)); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
| 427 | const char *required; |
| 428 | int ret; |
| 429 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 430 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 431 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 432 | if (!required || strcmp(required, "conf")) |
| 433 | continue; |
| 434 | ret = fit_config_verify_sig(fit, conf_noffset, sig_blob, |
| 435 | noffset); |
| 436 | if (ret) { |
| 437 | printf("Failed to verify required signature '%s'\n", |
| 438 | fit_get_name(sig_blob, noffset, NULL)); |
| 439 | return ret; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int fit_config_verify(const void *fit, int conf_noffset) |
| 447 | { |
| 448 | return fit_config_verify_required_sigs(fit, conf_noffset, |
| 449 | gd_fdt_blob()); |
| 450 | } |