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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | #endif /* !USE_HOSTCC*/ |
Masahiro Yamada | 6dd1052 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 16 | #include <fdt_region.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 17 | #include <image.h> |
| 18 | #include <u-boot/rsa.h> |
Alexandru Gagniuc | db182c4 | 2021-02-19 12:45:10 -0600 | [diff] [blame] | 19 | #include <u-boot/hash-checksum.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 20 | |
| 21 | #define IMAGE_MAX_HASHED_NODES 100 |
| 22 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 23 | /** |
| 24 | * fit_region_make_list() - Make a list of image regions |
| 25 | * |
| 26 | * Given a list of fdt_regions, create a list of image_regions. This is a |
| 27 | * simple conversion routine since the FDT and image code use different |
| 28 | * structures. |
| 29 | * |
| 30 | * @fit: FIT image |
| 31 | * @fdt_regions: Pointer to FDT regions |
| 32 | * @count: Number of FDT regions |
| 33 | * @region: Pointer to image regions, which must hold @count records. If |
| 34 | * region is NULL, then (except for an SPL build) the array will be |
| 35 | * allocated. |
| 36 | * @return: Pointer to image regions |
| 37 | */ |
| 38 | struct image_region *fit_region_make_list(const void *fit, |
| 39 | struct fdt_region *fdt_regions, |
| 40 | int count, |
| 41 | struct image_region *region) |
| 42 | { |
| 43 | int i; |
| 44 | |
| 45 | debug("Hash regions:\n"); |
| 46 | debug("%10s %10s\n", "Offset", "Size"); |
| 47 | |
| 48 | /* |
| 49 | * Use malloc() except in SPL (to save code size). In SPL the caller |
| 50 | * must allocate the array. |
| 51 | */ |
Simon Glass | c0cabbc | 2021-09-25 19:43:39 -0600 | [diff] [blame] | 52 | if (!IS_ENABLED(CONFIG_SPL_BUILD) && !region) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 53 | region = calloc(sizeof(*region), count); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 54 | if (!region) |
| 55 | return NULL; |
| 56 | for (i = 0; i < count; i++) { |
| 57 | debug("%10x %10x\n", fdt_regions[i].offset, |
| 58 | fdt_regions[i].size); |
| 59 | region[i].data = fit + fdt_regions[i].offset; |
| 60 | region[i].size = fdt_regions[i].size; |
| 61 | } |
| 62 | |
| 63 | return region; |
| 64 | } |
| 65 | |
| 66 | static int fit_image_setup_verify(struct image_sign_info *info, |
| 67 | const void *fit, int noffset, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 68 | const void *key_blob, int required_keynode, |
| 69 | char **err_msgp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 70 | { |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 71 | const char *algo_name; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 72 | const char *padding_name; |
| 73 | |
Simon Glass | c0cabbc | 2021-09-25 19:43:39 -0600 | [diff] [blame] | 74 | if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 75 | *err_msgp = "Total size too large"; |
| 76 | return 1; |
| 77 | } |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 78 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 79 | *err_msgp = "Can't get hash algo property"; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 84 | if (!padding_name) |
| 85 | padding_name = RSA_DEFAULT_PADDING_NAME; |
| 86 | |
| 87 | memset(info, '\0', sizeof(*info)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 88 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Hannu Lounento | e2b1e1f | 2021-10-18 08:49:03 +0300 | [diff] [blame] | 89 | info->fit = fit; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 90 | info->node_offset = noffset; |
| 91 | info->name = algo_name; |
| 92 | info->checksum = image_get_checksum_algo(algo_name); |
| 93 | info->crypto = image_get_crypto_algo(algo_name); |
| 94 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 95 | info->fdt_blob = key_blob; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 96 | info->required_keynode = required_keynode; |
| 97 | printf("%s:%s", algo_name, info->keyname); |
| 98 | |
| 99 | if (!info->checksum || !info->crypto || !info->padding) { |
| 100 | *err_msgp = "Unknown signature algorithm"; |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int fit_image_check_sig(const void *fit, int noffset, const void *data, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 108 | size_t size, const void *key_blob, int required_keynode, |
| 109 | char **err_msgp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 110 | { |
| 111 | struct image_sign_info info; |
| 112 | struct image_region region; |
| 113 | uint8_t *fit_value; |
| 114 | int fit_value_len; |
| 115 | |
| 116 | *err_msgp = NULL; |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 117 | if (fit_image_setup_verify(&info, fit, noffset, key_blob, |
| 118 | required_keynode, err_msgp)) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 119 | return -1; |
| 120 | |
| 121 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 122 | &fit_value_len)) { |
| 123 | *err_msgp = "Can't get hash value property"; |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | region.data = data; |
| 128 | region.size = size; |
| 129 | |
| 130 | if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) { |
| 131 | *err_msgp = "Verification failed"; |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int fit_image_verify_sig(const void *fit, int image_noffset, |
| 139 | const char *data, size_t size, |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 140 | const void *key_blob, int key_offset) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 141 | { |
| 142 | int noffset; |
| 143 | char *err_msg = ""; |
| 144 | int verified = 0; |
| 145 | int ret; |
| 146 | |
| 147 | /* Process all hash subnodes of the component image node */ |
| 148 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
| 149 | const char *name = fit_get_name(fit, noffset, NULL); |
| 150 | |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 151 | /* |
| 152 | * We don't support this since libfdt considers names with the |
| 153 | * name root but different @ suffix to be equal |
| 154 | */ |
| 155 | if (strchr(name, '@')) { |
| 156 | err_msg = "Node name contains @"; |
| 157 | goto error; |
| 158 | } |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 159 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 160 | strlen(FIT_SIG_NODENAME))) { |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 161 | ret = fit_image_check_sig(fit, noffset, data, size, |
| 162 | key_blob, -1, &err_msg); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 163 | if (ret) { |
| 164 | puts("- "); |
| 165 | } else { |
| 166 | puts("+ "); |
| 167 | verified = 1; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 174 | err_msg = "Corrupted or truncated tree"; |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | return verified ? 0 : -EPERM; |
| 179 | |
| 180 | error: |
| 181 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
| 182 | err_msg, fit_get_name(fit, noffset, NULL), |
| 183 | fit_get_name(fit, image_noffset, NULL)); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | int fit_image_verify_required_sigs(const void *fit, int image_noffset, |
| 188 | const char *data, size_t size, |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 189 | const void *key_blob, int *no_sigsp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 190 | { |
| 191 | int verify_count = 0; |
| 192 | int noffset; |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 193 | int key_node; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 194 | |
| 195 | /* Work out what we need to verify */ |
| 196 | *no_sigsp = 1; |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 197 | key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME); |
| 198 | if (key_node < 0) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 199 | debug("%s: No signature node found: %s\n", __func__, |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 200 | fdt_strerror(key_node)); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 204 | fdt_for_each_subnode(noffset, key_blob, key_node) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 205 | const char *required; |
| 206 | int ret; |
| 207 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 208 | required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED, |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 209 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 210 | if (!required || strcmp(required, "image")) |
| 211 | continue; |
| 212 | ret = fit_image_verify_sig(fit, image_noffset, data, size, |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 213 | key_blob, noffset); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 214 | if (ret) { |
| 215 | printf("Failed to verify required signature '%s'\n", |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 216 | fit_get_name(key_blob, noffset, NULL)); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | verify_count++; |
| 220 | } |
| 221 | |
| 222 | if (verify_count) |
| 223 | *no_sigsp = 0; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 228 | /** |
| 229 | * fit_config_check_sig() - Check the signature of a config |
| 230 | * |
Simon Glass | e3b2178 | 2021-11-12 12:28:09 -0700 | [diff] [blame] | 231 | * Here we are looking at a particular signature that needs verification (here |
| 232 | * signature-1): |
| 233 | * |
| 234 | * configurations { |
| 235 | * default = "conf-1"; |
| 236 | * conf-1 { |
| 237 | * kernel = "kernel-1"; |
| 238 | * fdt = "fdt-1"; |
| 239 | * signature-1 { |
| 240 | * algo = "sha1,rsa2048"; |
| 241 | * value = <...conf 1 signature...>; |
| 242 | * }; |
| 243 | * }; |
| 244 | * |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 245 | * @fit: FIT to check |
Simon Glass | e3b2178 | 2021-11-12 12:28:09 -0700 | [diff] [blame] | 246 | * @noffset: Offset of the signature node being checked (e.g. |
| 247 | * /configurations/conf-1/signature-1) |
| 248 | * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1) |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 249 | * @key_blob: Blob containing the keys to check against |
Simon Glass | e3b2178 | 2021-11-12 12:28:09 -0700 | [diff] [blame] | 250 | * @required_keynode: Offset in @key_blob of the required key node, |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 251 | * if any. If this is given, then the configuration wil not |
| 252 | * pass verification unless that key is used. If this is |
| 253 | * -1 then any signature will do. |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 254 | * @err_msgp: In the event of an error, this will be pointed to a |
| 255 | * help error string to display to the user. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 256 | * Return: 0 if all verified ok, <0 on error |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 257 | */ |
Simon Glass | e3b2178 | 2021-11-12 12:28:09 -0700 | [diff] [blame] | 258 | static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 259 | const void *key_blob, int required_keynode, |
| 260 | char **err_msgp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 261 | { |
John Keeping | 741fc33 | 2021-04-20 19:19:44 +0100 | [diff] [blame] | 262 | static char * const exc_prop[] = { |
Sean Anderson | 016f4d5 | 2022-10-20 15:41:10 -0400 | [diff] [blame^] | 263 | FIT_DATA_PROP, |
| 264 | FIT_DATA_SIZE_PROP, |
| 265 | FIT_DATA_POSITION_PROP, |
| 266 | FIT_DATA_OFFSET_PROP, |
John Keeping | 741fc33 | 2021-04-20 19:19:44 +0100 | [diff] [blame] | 267 | }; |
| 268 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 269 | const char *prop, *end, *name; |
| 270 | struct image_sign_info info; |
| 271 | const uint32_t *strings; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 272 | const char *config_name; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 273 | uint8_t *fit_value; |
| 274 | int fit_value_len; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 275 | bool found_config; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 276 | int max_regions; |
| 277 | int i, prop_len; |
| 278 | char path[200]; |
| 279 | int count; |
| 280 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 281 | config_name = fit_get_name(fit, conf_noffset, NULL); |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 282 | debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob, |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 283 | fit_get_name(fit, noffset, NULL), |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 284 | fit_get_name(key_blob, required_keynode, NULL)); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 285 | *err_msgp = NULL; |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 286 | if (fit_image_setup_verify(&info, fit, noffset, key_blob, |
| 287 | required_keynode, err_msgp)) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 288 | return -1; |
| 289 | |
| 290 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 291 | &fit_value_len)) { |
| 292 | *err_msgp = "Can't get hash value property"; |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | /* Count the number of strings in the property */ |
| 297 | prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len); |
| 298 | end = prop ? prop + prop_len : prop; |
| 299 | for (name = prop, count = 0; name < end; name++) |
| 300 | if (!*name) |
| 301 | count++; |
| 302 | if (!count) { |
| 303 | *err_msgp = "Can't get hashed-nodes property"; |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') { |
| 308 | *err_msgp = "hashed-nodes property must be null-terminated"; |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | /* Add a sanity check here since we are using the stack */ |
| 313 | if (count > IMAGE_MAX_HASHED_NODES) { |
| 314 | *err_msgp = "Number of hashed nodes exceeds maximum"; |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | /* Create a list of node names from those strings */ |
| 319 | char *node_inc[count]; |
| 320 | |
| 321 | debug("Hash nodes (%d):\n", count); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 322 | found_config = false; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 323 | for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) { |
| 324 | debug(" '%s'\n", name); |
| 325 | node_inc[i] = (char *)name; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 326 | if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) && |
| 327 | name[sizeof(FIT_CONFS_PATH) - 1] == '/' && |
| 328 | !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) { |
| 329 | debug(" (found config node %s)", config_name); |
| 330 | found_config = true; |
| 331 | } |
| 332 | } |
| 333 | if (!found_config) { |
| 334 | *err_msgp = "Selected config not in hashed nodes"; |
| 335 | return -1; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Each node can generate one region for each sub-node. Allow for |
| 340 | * 7 sub-nodes (hash-1, signature-1, etc.) and some extra. |
| 341 | */ |
| 342 | max_regions = 20 + count * 7; |
| 343 | struct fdt_region fdt_regions[max_regions]; |
| 344 | |
| 345 | /* Get a list of regions to hash */ |
| 346 | count = fdt_find_regions(fit, node_inc, count, |
| 347 | exc_prop, ARRAY_SIZE(exc_prop), |
| 348 | fdt_regions, max_regions - 1, |
| 349 | path, sizeof(path), 0); |
| 350 | if (count < 0) { |
| 351 | *err_msgp = "Failed to hash configuration"; |
| 352 | return -1; |
| 353 | } |
| 354 | if (count == 0) { |
| 355 | *err_msgp = "No data to hash"; |
| 356 | return -1; |
| 357 | } |
| 358 | if (count >= max_regions - 1) { |
| 359 | *err_msgp = "Too many hash regions"; |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | /* Add the strings */ |
| 364 | strings = fdt_getprop(fit, noffset, "hashed-strings", NULL); |
| 365 | if (strings) { |
| 366 | /* |
| 367 | * The strings region offset must be a static 0x0. |
| 368 | * This is set in tool/image-host.c |
| 369 | */ |
| 370 | fdt_regions[count].offset = fdt_off_dt_strings(fit); |
| 371 | fdt_regions[count].size = fdt32_to_cpu(strings[1]); |
| 372 | count++; |
| 373 | } |
| 374 | |
| 375 | /* Allocate the region list on the stack */ |
| 376 | struct image_region region[count]; |
| 377 | |
| 378 | fit_region_make_list(fit, fdt_regions, count, region); |
| 379 | if (info.crypto->verify(&info, region, count, fit_value, |
| 380 | fit_value_len)) { |
| 381 | *err_msgp = "Verification failed"; |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 388 | /** |
| 389 | * fit_config_verify_key() - Verify that a configuration is signed with a key |
| 390 | * |
| 391 | * Here we are looking at a particular configuration that needs verification: |
| 392 | * |
| 393 | * configurations { |
| 394 | * default = "conf-1"; |
| 395 | * conf-1 { |
| 396 | * kernel = "kernel-1"; |
| 397 | * fdt = "fdt-1"; |
| 398 | * signature-1 { |
| 399 | * algo = "sha1,rsa2048"; |
| 400 | * value = <...conf 1 signature...>; |
| 401 | * }; |
| 402 | * }; |
| 403 | * |
| 404 | * We must check each of the signature subnodes of conf-1. Hopefully one of them |
| 405 | * will match the key at key_offset. |
| 406 | * |
| 407 | * @fit: FIT to check |
| 408 | * @conf_noffset: Offset of the configuration node to check (e.g. |
| 409 | * /configurations/conf-1) |
| 410 | * @key_blob: Blob containing the keys to check against |
| 411 | * @key_offset: Offset of the key to check within @key_blob |
| 412 | * @return 0 if OK, -EPERM if any signatures did not verify, or the |
| 413 | * configuration node has an invalid name |
| 414 | */ |
| 415 | static int fit_config_verify_key(const void *fit, int conf_noffset, |
| 416 | const void *key_blob, int key_offset) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 417 | { |
| 418 | int noffset; |
Alexandru Gagniuc | 72fdd3a | 2021-01-11 08:46:58 -0600 | [diff] [blame] | 419 | char *err_msg = "No 'signature' subnode found"; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 420 | int verified = 0; |
| 421 | int ret; |
| 422 | |
| 423 | /* Process all hash subnodes of the component conf node */ |
| 424 | fdt_for_each_subnode(noffset, fit, conf_noffset) { |
| 425 | const char *name = fit_get_name(fit, noffset, NULL); |
| 426 | |
| 427 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 428 | strlen(FIT_SIG_NODENAME))) { |
Simon Glass | e3b2178 | 2021-11-12 12:28:09 -0700 | [diff] [blame] | 429 | ret = fit_config_check_sig(fit, noffset, conf_noffset, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 430 | key_blob, key_offset, |
| 431 | &err_msg); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 432 | if (ret) { |
| 433 | puts("- "); |
| 434 | } else { |
| 435 | puts("+ "); |
| 436 | verified = 1; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 443 | err_msg = "Corrupted or truncated tree"; |
| 444 | goto error; |
| 445 | } |
| 446 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 447 | if (verified) |
| 448 | return 0; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 449 | |
| 450 | error: |
| 451 | printf(" error!\n%s for '%s' hash node in '%s' config node\n", |
| 452 | err_msg, fit_get_name(fit, noffset, NULL), |
| 453 | fit_get_name(fit, conf_noffset, NULL)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 454 | return -EPERM; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 455 | } |
| 456 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 457 | /** |
| 458 | * fit_config_verify_required_keys() - verify any required signatures for config |
| 459 | * |
| 460 | * This looks through all the signatures we expect and verifies that at least |
| 461 | * all the required ones are valid signatures for the configuration |
| 462 | * |
| 463 | * @fit: FIT to check |
| 464 | * @conf_noffset: Offset of the configuration node to check (e.g. |
| 465 | * /configurations/conf-1) |
| 466 | * @key_blob: Blob containing the keys to check against |
| 467 | * @return 0 if OK, -EPERM if any signatures did not verify, or the |
| 468 | * configuration node has an invalid name |
| 469 | */ |
| 470 | static int fit_config_verify_required_keys(const void *fit, int conf_noffset, |
| 471 | const void *key_blob) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 472 | { |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 473 | const char *name = fit_get_name(fit, conf_noffset, NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 474 | int noffset; |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 475 | int key_node; |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 476 | int verified = 0; |
| 477 | int reqd_sigs = 0; |
| 478 | bool reqd_policy_all = true; |
| 479 | const char *reqd_mode; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 480 | |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 481 | /* |
| 482 | * We don't support this since libfdt considers names with the |
| 483 | * name root but different @ suffix to be equal |
| 484 | */ |
| 485 | if (strchr(name, '@')) { |
| 486 | printf("Configuration node '%s' contains '@'\n", name); |
| 487 | return -EPERM; |
| 488 | } |
| 489 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 490 | /* Work out what we need to verify */ |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 491 | key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME); |
| 492 | if (key_node < 0) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 493 | debug("%s: No signature node found: %s\n", __func__, |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 494 | fdt_strerror(key_node)); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 498 | /* Get required-mode policy property from DTB */ |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 499 | reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL); |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 500 | if (reqd_mode && !strcmp(reqd_mode, "any")) |
| 501 | reqd_policy_all = false; |
| 502 | |
| 503 | debug("%s: required-mode policy set to '%s'\n", __func__, |
| 504 | reqd_policy_all ? "all" : "any"); |
| 505 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 506 | /* |
| 507 | * The algorithm here is a little convoluted due to how we want it to |
| 508 | * work. Here we work through each of the signature nodes in the |
| 509 | * public-key area. These are in the U-Boot control devicetree. Each |
| 510 | * node was created by signing a configuration, so we check if it is |
| 511 | * 'required' and if so, request that it be verified. |
| 512 | */ |
| 513 | fdt_for_each_subnode(noffset, key_blob, key_node) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 514 | const char *required; |
| 515 | int ret; |
| 516 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 517 | required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED, |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 518 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 519 | if (!required || strcmp(required, "conf")) |
| 520 | continue; |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 521 | |
| 522 | reqd_sigs++; |
| 523 | |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 524 | ret = fit_config_verify_key(fit, conf_noffset, key_blob, |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 525 | noffset); |
| 526 | if (ret) { |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 527 | if (reqd_policy_all) { |
| 528 | printf("Failed to verify required signature '%s'\n", |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 529 | fit_get_name(key_blob, noffset, NULL)); |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 530 | return ret; |
| 531 | } |
| 532 | } else { |
| 533 | verified++; |
| 534 | if (!reqd_policy_all) |
| 535 | break; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Thirupathaiah Annapureddy | 77564b0 | 2020-08-16 23:01:09 -0700 | [diff] [blame] | 539 | if (reqd_sigs && !verified) { |
| 540 | printf("Failed to verify 'any' of the required signature(s)\n"); |
| 541 | return -EPERM; |
| 542 | } |
| 543 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int fit_config_verify(const void *fit, int conf_noffset) |
| 548 | { |
Simon Glass | 4c5ff06 | 2021-11-12 12:28:08 -0700 | [diff] [blame] | 549 | return fit_config_verify_required_keys(fit, conf_noffset, |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 550 | gd_fdt_blob()); |
| 551 | } |