Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | * |
| 5 | * (C) Copyright 2008 Semihalf |
| 6 | * |
| 7 | * (C) Copyright 2000-2006 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Masahiro Yamada | 6dd1052 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 13 | #include <fdt_region.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 16 | |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 17 | #include <openssl/pem.h> |
| 18 | #include <openssl/evp.h> |
| 19 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 20 | /** |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 21 | * fit_set_hash_value - set hash value in requested has node |
| 22 | * @fit: pointer to the FIT format image header |
| 23 | * @noffset: hash node offset |
| 24 | * @value: hash value to be set |
| 25 | * @value_len: hash value length |
| 26 | * |
| 27 | * fit_set_hash_value() attempts to set hash value in a node at offset |
| 28 | * given and returns operation status to the caller. |
| 29 | * |
| 30 | * returns |
| 31 | * 0, on success |
| 32 | * -1, on failure |
| 33 | */ |
| 34 | static int fit_set_hash_value(void *fit, int noffset, uint8_t *value, |
| 35 | int value_len) |
| 36 | { |
| 37 | int ret; |
| 38 | |
| 39 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 40 | if (ret) { |
| 41 | printf("Can't set hash '%s' property for '%s' node(%s)\n", |
| 42 | FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL), |
| 43 | fdt_strerror(ret)); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 44 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /** |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 51 | * fit_image_process_hash - Process a single subnode of the images/ node |
| 52 | * |
| 53 | * Check each subnode and process accordingly. For hash nodes we generate |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 54 | * a hash of the supplied data and store it in the node. |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 55 | * |
| 56 | * @fit: pointer to the FIT format image header |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 57 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 58 | * @noffset: subnode offset |
| 59 | * @data: data to process |
| 60 | * @size: size of data in bytes |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 61 | * Return: 0 if ok, -1 on error |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 62 | */ |
| 63 | static int fit_image_process_hash(void *fit, const char *image_name, |
| 64 | int noffset, const void *data, size_t size) |
| 65 | { |
| 66 | uint8_t value[FIT_MAX_HASH_LEN]; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 67 | const char *node_name; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 68 | int value_len; |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 69 | const char *algo; |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 70 | int ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 71 | |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 72 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 73 | |
| 74 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 75 | printf("Can't get hash algo property for '%s' hash node in '%s' image node\n", |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 76 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 77 | return -ENOENT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (calculate_hash(data, size, algo, value, &value_len)) { |
| 81 | printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n", |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 82 | algo, node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 83 | return -EPROTONOSUPPORT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 86 | ret = fit_set_hash_value(fit, noffset, value, value_len); |
| 87 | if (ret) { |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 88 | printf("Can't set hash value for '%s' hash node in '%s' image node\n", |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 89 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 90 | return ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 97 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 98 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 99 | * This writes the signature and signer data to the FIT. |
| 100 | * |
| 101 | * @fit: pointer to the FIT format image header |
| 102 | * @noffset: hash node offset |
| 103 | * @value: signature value to be set |
| 104 | * @value_len: signature value length |
| 105 | * @comment: Text comment to write (NULL for none) |
| 106 | * |
| 107 | * returns |
| 108 | * 0, on success |
| 109 | * -FDT_ERR_..., on failure |
| 110 | */ |
| 111 | static int fit_image_write_sig(void *fit, int noffset, uint8_t *value, |
| 112 | int value_len, const char *comment, const char *region_prop, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 113 | int region_proplen, const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 114 | { |
| 115 | int string_size; |
| 116 | int ret; |
| 117 | |
| 118 | /* |
| 119 | * Get the current string size, before we update the FIT and add |
| 120 | * more |
| 121 | */ |
| 122 | string_size = fdt_size_dt_strings(fit); |
| 123 | |
| 124 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 125 | if (!ret) { |
| 126 | ret = fdt_setprop_string(fit, noffset, "signer-name", |
| 127 | "mkimage"); |
| 128 | } |
| 129 | if (!ret) { |
| 130 | ret = fdt_setprop_string(fit, noffset, "signer-version", |
| 131 | PLAIN_VERSION); |
| 132 | } |
| 133 | if (comment && !ret) |
| 134 | ret = fdt_setprop_string(fit, noffset, "comment", comment); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 135 | if (!ret) { |
| 136 | time_t timestamp = imagetool_get_source_date(cmdname, |
| 137 | time(NULL)); |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 138 | uint32_t t = cpu_to_uimage(timestamp); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 139 | |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 140 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 141 | sizeof(uint32_t)); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 142 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 143 | if (region_prop && !ret) { |
| 144 | uint32_t strdata[2]; |
| 145 | |
| 146 | ret = fdt_setprop(fit, noffset, "hashed-nodes", |
| 147 | region_prop, region_proplen); |
Teddy Reed | 9767bf4 | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 148 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 149 | strdata[0] = 0; |
| 150 | strdata[1] = cpu_to_fdt32(string_size); |
| 151 | if (!ret) { |
| 152 | ret = fdt_setprop(fit, noffset, "hashed-strings", |
| 153 | strdata, sizeof(strdata)); |
| 154 | } |
| 155 | } |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 156 | if (algo_name && !ret) |
| 157 | ret = fdt_setprop_string(fit, noffset, "algo", algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int fit_image_setup_sig(struct image_sign_info *info, |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 163 | const char *keydir, const char *keyfile, void *fit, |
| 164 | const char *image_name, int noffset, const char *require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 165 | const char *engine_id, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 166 | { |
| 167 | const char *node_name; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 168 | const char *padding_name; |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 169 | |
| 170 | node_name = fit_get_name(fit, noffset, NULL); |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 171 | if (!algo_name) { |
| 172 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 173 | printf("Can't get algo property for '%s' signature node in '%s' image node\n", |
| 174 | node_name, image_name); |
| 175 | return -1; |
| 176 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 179 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 180 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 181 | memset(info, '\0', sizeof(*info)); |
| 182 | info->keydir = keydir; |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 183 | info->keyfile = keyfile; |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 184 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 185 | info->fit = fit; |
| 186 | info->node_offset = noffset; |
Masahiro Yamada | c1f137b | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 187 | info->name = strdup(algo_name); |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 188 | info->checksum = image_get_checksum_algo(algo_name); |
| 189 | info->crypto = image_get_crypto_algo(algo_name); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 190 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 191 | info->require_keys = require_keys; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 192 | info->engine_id = engine_id; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 193 | if (!info->checksum || !info->crypto) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 194 | printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n", |
| 195 | algo_name, node_name, image_name); |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * fit_image_process_sig- Process a single subnode of the images/ node |
| 204 | * |
| 205 | * Check each subnode and process accordingly. For signature nodes we |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 206 | * generate a signed hash of the supplied data and store it in the node. |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 207 | * |
| 208 | * @keydir: Directory containing keys to use for signing |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 209 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 210 | * @fit: pointer to the FIT format image header |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 211 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 212 | * @noffset: subnode offset |
| 213 | * @data: data to process |
| 214 | * @size: size of data in bytes |
| 215 | * @comment: Comment to add to signature nodes |
| 216 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 217 | * @engine_id: Engine to use for signing |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 218 | * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code |
| 219 | * on failure |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 220 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 221 | static int fit_image_process_sig(const char *keydir, const char *keyfile, |
| 222 | void *keydest, void *fit, const char *image_name, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 223 | int noffset, const void *data, size_t size, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 224 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 225 | const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 226 | { |
| 227 | struct image_sign_info info; |
| 228 | struct image_region region; |
| 229 | const char *node_name; |
| 230 | uint8_t *value; |
| 231 | uint value_len; |
| 232 | int ret; |
| 233 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 234 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name, |
| 235 | noffset, require_keys ? "image" : NULL, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 236 | engine_id, algo_name)) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 237 | return -1; |
| 238 | |
| 239 | node_name = fit_get_name(fit, noffset, NULL); |
| 240 | region.data = data; |
| 241 | region.size = size; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 242 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 243 | if (ret) { |
| 244 | printf("Failed to sign '%s' signature node in '%s' image node: %d\n", |
| 245 | node_name, image_name, ret); |
| 246 | |
| 247 | /* We allow keys to be missing */ |
| 248 | if (ret == -ENOENT) |
| 249 | return 0; |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 254 | NULL, 0, cmdname, algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 255 | if (ret) { |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 256 | if (ret == -FDT_ERR_NOSPACE) |
| 257 | return -ENOSPC; |
| 258 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 259 | node_name, image_name, fdt_strerror(ret)); |
| 260 | return -1; |
| 261 | } |
| 262 | free(value); |
| 263 | |
| 264 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 265 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 266 | |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 267 | /* |
| 268 | * Write the public key into the supplied FDT file; this might fail |
mario.six@gdsys.cc | d019e15 | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 269 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 270 | * size values |
| 271 | */ |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 272 | if (keydest) { |
| 273 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 274 | if (ret < 0) { |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 275 | printf("Failed to add verification data for '%s' signature node in '%s' image node\n", |
| 276 | node_name, image_name); |
| 277 | return ret; |
| 278 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 279 | /* Return the node that was written to */ |
| 280 | return ret; |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 281 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 286 | static int fit_image_read_data(char *filename, unsigned char *data, |
| 287 | int expected_size) |
| 288 | { |
| 289 | struct stat sbuf; |
| 290 | int fd, ret = -1; |
| 291 | ssize_t n; |
| 292 | |
| 293 | /* Open file */ |
| 294 | fd = open(filename, O_RDONLY | O_BINARY); |
| 295 | if (fd < 0) { |
| 296 | printf("Can't open file %s (err=%d => %s)\n", |
| 297 | filename, errno, strerror(errno)); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | /* Compute file size */ |
| 302 | if (fstat(fd, &sbuf) < 0) { |
| 303 | printf("Can't fstat file %s (err=%d => %s)\n", |
| 304 | filename, errno, strerror(errno)); |
| 305 | goto err; |
| 306 | } |
| 307 | |
| 308 | /* Check file size */ |
| 309 | if (sbuf.st_size != expected_size) { |
Heinrich Schuchardt | d697a2d | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 310 | printf("File %s don't have the expected size (size=%lld, expected=%d)\n", |
| 311 | filename, (long long)sbuf.st_size, expected_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | |
| 315 | /* Read data */ |
| 316 | n = read(fd, data, sbuf.st_size); |
| 317 | if (n < 0) { |
| 318 | printf("Can't read file %s (err=%d => %s)\n", |
| 319 | filename, errno, strerror(errno)); |
| 320 | goto err; |
| 321 | } |
| 322 | |
| 323 | /* Check that we have read all the file */ |
| 324 | if (n != sbuf.st_size) { |
Vagrant Cascadian | 396e4ed | 2021-09-28 10:11:46 -0700 | [diff] [blame] | 325 | printf("Can't read all file %s (read %zd bytes, expected %lld)\n", |
Heinrich Schuchardt | d697a2d | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 326 | filename, n, (long long)sbuf.st_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 327 | goto err; |
| 328 | } |
| 329 | |
| 330 | ret = 0; |
| 331 | |
| 332 | err: |
| 333 | close(fd); |
| 334 | return ret; |
| 335 | } |
| 336 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 337 | static int get_random_data(void *data, int size) |
| 338 | { |
| 339 | unsigned char *tmp = data; |
| 340 | struct timespec date; |
Simon Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 341 | int i, ret; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 342 | |
| 343 | if (!tmp) { |
| 344 | printf("%s: pointer data is NULL\n", __func__); |
| 345 | ret = -1; |
| 346 | goto out; |
| 347 | } |
| 348 | |
| 349 | ret = clock_gettime(CLOCK_MONOTONIC, &date); |
Simon Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 350 | if (ret) { |
| 351 | printf("%s: clock_gettime has failed (%s)\n", __func__, |
| 352 | strerror(errno)); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 353 | goto out; |
| 354 | } |
| 355 | |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 356 | srandom(date.tv_nsec); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 357 | |
| 358 | for (i = 0; i < size; i++) { |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 359 | *tmp = random() & 0xff; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 360 | tmp++; |
| 361 | } |
| 362 | |
| 363 | out: |
| 364 | return ret; |
| 365 | } |
| 366 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 367 | static int fit_image_setup_cipher(struct image_cipher_info *info, |
| 368 | const char *keydir, void *fit, |
| 369 | const char *image_name, int image_noffset, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 370 | int noffset) |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 371 | { |
| 372 | char *algo_name; |
| 373 | char filename[128]; |
| 374 | int ret = -1; |
| 375 | |
| 376 | if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) { |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 377 | printf("Can't get algo name for cipher in image '%s'\n", |
| 378 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | info->keydir = keydir; |
| 383 | |
| 384 | /* Read the key name */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 385 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 386 | if (!info->keyname) { |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 387 | printf("Can't get key name for cipher in image '%s'\n", |
| 388 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 389 | goto out; |
| 390 | } |
| 391 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 392 | /* |
| 393 | * Read the IV name |
| 394 | * |
| 395 | * If this property is not provided then mkimage will generate |
| 396 | * a random IV and store it in the FIT image |
| 397 | */ |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 398 | info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 399 | |
| 400 | info->fit = fit; |
| 401 | info->node_noffset = noffset; |
| 402 | info->name = algo_name; |
| 403 | |
| 404 | info->cipher = image_get_cipher_algo(algo_name); |
| 405 | if (!info->cipher) { |
| 406 | printf("Can't get algo for cipher '%s'\n", image_name); |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | /* Read the key in the file */ |
| 411 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 412 | info->keydir, info->keyname, ".bin"); |
| 413 | info->key = malloc(info->cipher->key_len); |
| 414 | if (!info->key) { |
| 415 | printf("Can't allocate memory for key\n"); |
| 416 | ret = -1; |
| 417 | goto out; |
| 418 | } |
| 419 | ret = fit_image_read_data(filename, (unsigned char *)info->key, |
| 420 | info->cipher->key_len); |
| 421 | if (ret < 0) |
| 422 | goto out; |
| 423 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 424 | info->iv = malloc(info->cipher->iv_len); |
| 425 | if (!info->iv) { |
| 426 | printf("Can't allocate memory for iv\n"); |
| 427 | ret = -1; |
| 428 | goto out; |
| 429 | } |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 430 | |
| 431 | if (info->ivname) { |
| 432 | /* Read the IV in the file */ |
| 433 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 434 | info->keydir, info->ivname, ".bin"); |
| 435 | ret = fit_image_read_data(filename, (unsigned char *)info->iv, |
| 436 | info->cipher->iv_len); |
| 437 | } else { |
| 438 | /* Generate an ramdom IV */ |
| 439 | ret = get_random_data((void *)info->iv, info->cipher->iv_len); |
| 440 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 441 | |
| 442 | out: |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | int fit_image_write_cipher(void *fit, int image_noffset, int noffset, |
| 447 | const void *data, size_t size, |
| 448 | unsigned char *data_ciphered, int data_ciphered_len) |
| 449 | { |
| 450 | int ret = -1; |
| 451 | |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 452 | /* Replace data with ciphered data */ |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 453 | ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP, |
| 454 | data_ciphered, data_ciphered_len); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 455 | if (ret == -FDT_ERR_NOSPACE) { |
| 456 | ret = -ENOSPC; |
| 457 | goto out; |
| 458 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 459 | if (ret) { |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 460 | printf("Can't replace data with ciphered data (err = %d)\n", ret); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 461 | goto out; |
| 462 | } |
| 463 | |
| 464 | /* add non ciphered data size */ |
| 465 | ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 466 | if (ret == -FDT_ERR_NOSPACE) { |
| 467 | ret = -ENOSPC; |
| 468 | goto out; |
| 469 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 470 | if (ret) { |
| 471 | printf("Can't add unciphered data size (err = %d)\n", ret); |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | out: |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | static int |
| 480 | fit_image_process_cipher(const char *keydir, void *keydest, void *fit, |
| 481 | const char *image_name, int image_noffset, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 482 | int node_noffset, const void *data, size_t size, |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 483 | const char *cmdname) |
| 484 | { |
| 485 | struct image_cipher_info info; |
| 486 | unsigned char *data_ciphered = NULL; |
| 487 | int data_ciphered_len; |
| 488 | int ret; |
| 489 | |
| 490 | memset(&info, 0, sizeof(info)); |
| 491 | |
| 492 | ret = fit_image_setup_cipher(&info, keydir, fit, image_name, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 493 | image_noffset, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 494 | if (ret) |
| 495 | goto out; |
| 496 | |
| 497 | ret = info.cipher->encrypt(&info, data, size, |
| 498 | &data_ciphered, &data_ciphered_len); |
| 499 | if (ret) |
| 500 | goto out; |
| 501 | |
| 502 | /* |
| 503 | * Write the public key into the supplied FDT file; this might fail |
| 504 | * several times, since we try signing with successively increasing |
| 505 | * size values |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 506 | * And, if needed, write the iv in the FIT file |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 507 | */ |
| 508 | if (keydest) { |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 509 | ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 510 | if (ret) { |
| 511 | printf("Failed to add verification data for cipher '%s' in image '%s'\n", |
| 512 | info.keyname, image_name); |
| 513 | goto out; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | ret = fit_image_write_cipher(fit, image_noffset, node_noffset, |
| 518 | data, size, |
| 519 | data_ciphered, data_ciphered_len); |
| 520 | |
| 521 | out: |
| 522 | free(data_ciphered); |
| 523 | free((void *)info.key); |
| 524 | free((void *)info.iv); |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | int fit_image_cipher_data(const char *keydir, void *keydest, |
| 529 | void *fit, int image_noffset, const char *comment, |
| 530 | int require_keys, const char *engine_id, |
| 531 | const char *cmdname) |
| 532 | { |
| 533 | const char *image_name; |
| 534 | const void *data; |
| 535 | size_t size; |
Patrick Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 536 | int cipher_node_offset, len; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 537 | |
| 538 | /* Get image name */ |
| 539 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 540 | if (!image_name) { |
| 541 | printf("Can't get image name\n"); |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | /* Get image data and data length */ |
| 546 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 547 | printf("Can't get image data/size\n"); |
| 548 | return -1; |
| 549 | } |
| 550 | |
Patrick Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 551 | /* |
| 552 | * Don't cipher ciphered data. |
| 553 | * |
| 554 | * If the data-size-unciphered property is present the data for this |
| 555 | * image is already encrypted. This is important as 'mkimage -F' can be |
| 556 | * run multiple times on a FIT image. |
| 557 | */ |
| 558 | if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len)) |
| 559 | return 0; |
| 560 | if (len != -FDT_ERR_NOTFOUND) { |
| 561 | printf("Failure testing for data-size-unciphered\n"); |
| 562 | return -1; |
| 563 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 564 | |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 565 | /* Process cipher node if present */ |
| 566 | cipher_node_offset = fdt_subnode_offset(fit, image_noffset, |
| 567 | FIT_CIPHER_NODENAME); |
| 568 | if (cipher_node_offset == -FDT_ERR_NOTFOUND) |
| 569 | return 0; |
| 570 | if (cipher_node_offset < 0) { |
| 571 | printf("Failure getting cipher node\n"); |
| 572 | return -1; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 573 | } |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 574 | if (!IMAGE_ENABLE_ENCRYPT || !keydir) |
| 575 | return 0; |
| 576 | return fit_image_process_cipher(keydir, keydest, fit, image_name, |
| 577 | image_noffset, cipher_node_offset, data, size, cmdname); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 580 | /** |
| 581 | * fit_image_add_verification_data() - calculate/set verig. data for image node |
| 582 | * |
| 583 | * This adds hash and signature values for an component image node. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 584 | * |
| 585 | * All existing hash subnodes are checked, if algorithm property is set to |
| 586 | * one of the supported hash algorithms, hash value is computed and |
| 587 | * corresponding hash node property is set, for example: |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 588 | * |
| 589 | * Input component image node structure: |
| 590 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 591 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 592 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 593 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 594 | * |- algo = "sha1" |
| 595 | * |
| 596 | * Output component image node structure: |
| 597 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 598 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 599 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 600 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 601 | * |- algo = "sha1" |
| 602 | * |- value = sha1(data) |
| 603 | * |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 604 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 605 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 606 | * @keydir Directory containing *.key and *.crt files (or NULL) |
| 607 | * @keydest FDT Blob to write public keys into (NULL if none) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 608 | * @fit: Pointer to the FIT format image header |
| 609 | * @image_noffset: Requested component image node |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 610 | * @comment: Comment to add to signature nodes |
| 611 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 612 | * @engine_id: Engine to use for signing |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 613 | * @return: 0 on success, <0 on failure |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 614 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 615 | int fit_image_add_verification_data(const char *keydir, const char *keyfile, |
| 616 | void *keydest, void *fit, int image_noffset, |
| 617 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 618 | const char *cmdname, const char* algo_name) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 619 | { |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 620 | const char *image_name; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 621 | const void *data; |
| 622 | size_t size; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 623 | int noffset; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 624 | |
| 625 | /* Get image data and data length */ |
| 626 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 627 | printf("Can't get image data/size\n"); |
| 628 | return -1; |
| 629 | } |
| 630 | |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 631 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 632 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 633 | /* Process all hash subnodes of the component image node */ |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 634 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 635 | noffset >= 0; |
| 636 | noffset = fdt_next_subnode(fit, noffset)) { |
| 637 | const char *node_name; |
| 638 | int ret = 0; |
| 639 | |
| 640 | /* |
| 641 | * Check subnode name, must be equal to "hash" or "signature". |
| 642 | * Multiple hash nodes require unique unit node |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 643 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 644 | */ |
| 645 | node_name = fit_get_name(fit, noffset, NULL); |
| 646 | if (!strncmp(node_name, FIT_HASH_NODENAME, |
| 647 | strlen(FIT_HASH_NODENAME))) { |
| 648 | ret = fit_image_process_hash(fit, image_name, noffset, |
| 649 | data, size); |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 650 | } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) && |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 651 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 652 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 653 | ret = fit_image_process_sig(keydir, keyfile, keydest, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 654 | fit, image_name, noffset, data, size, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 655 | comment, require_keys, engine_id, cmdname, |
| 656 | algo_name); |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 657 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 658 | if (ret < 0) |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 659 | return ret; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 665 | struct strlist { |
| 666 | int count; |
| 667 | char **strings; |
| 668 | }; |
| 669 | |
| 670 | static void strlist_init(struct strlist *list) |
| 671 | { |
| 672 | memset(list, '\0', sizeof(*list)); |
| 673 | } |
| 674 | |
| 675 | static void strlist_free(struct strlist *list) |
| 676 | { |
| 677 | int i; |
| 678 | |
| 679 | for (i = 0; i < list->count; i++) |
| 680 | free(list->strings[i]); |
| 681 | free(list->strings); |
| 682 | } |
| 683 | |
| 684 | static int strlist_add(struct strlist *list, const char *str) |
| 685 | { |
| 686 | char *dup; |
| 687 | |
| 688 | dup = strdup(str); |
| 689 | list->strings = realloc(list->strings, |
| 690 | (list->count + 1) * sizeof(char *)); |
| 691 | if (!list || !str) |
| 692 | return -1; |
| 693 | list->strings[list->count++] = dup; |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 698 | static const char *fit_config_get_image_list(const void *fit, int noffset, |
| 699 | int *lenp, int *allow_missingp) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 700 | { |
| 701 | static const char default_list[] = FIT_KERNEL_PROP "\0" |
| 702 | FIT_FDT_PROP; |
| 703 | const char *prop; |
| 704 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 705 | /* If there is an "sign-image" property, use that */ |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 706 | prop = fdt_getprop(fit, noffset, "sign-images", lenp); |
| 707 | if (prop) { |
| 708 | *allow_missingp = 0; |
| 709 | return *lenp ? prop : NULL; |
| 710 | } |
| 711 | |
| 712 | /* Default image list */ |
| 713 | *allow_missingp = 1; |
| 714 | *lenp = sizeof(default_list); |
| 715 | |
| 716 | return default_list; |
| 717 | } |
| 718 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 719 | /** |
| 720 | * fit_config_add_hash() - Add a list of nodes to hash for an image |
| 721 | * |
| 722 | * This adds a list of paths to image nodes (as referred to by a particular |
| 723 | * offset) that need to be hashed, to protect a configuration |
| 724 | * |
| 725 | * @fit: Pointer to the FIT format image header |
| 726 | * @image_noffset: Offset of image to process (e.g. /images/kernel-1) |
| 727 | * @node_inc: List of nodes to add to |
| 728 | * @conf_name Configuration-node name, child of /configurations node (only |
| 729 | * used for error messages) |
| 730 | * @sig_name Signature-node name (only used for error messages) |
| 731 | * @iname: Name of image being processed (e.g. "kernel-1" (only used |
| 732 | * for error messages) |
| 733 | */ |
| 734 | static int fit_config_add_hash(const void *fit, int image_noffset, |
| 735 | struct strlist *node_inc, const char *conf_name, |
| 736 | const char *sig_name, const char *iname) |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 737 | { |
Simon Glass | 888c0c5 | 2021-11-12 12:28:07 -0700 | [diff] [blame] | 738 | char path[200]; |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 739 | int noffset; |
| 740 | int hash_count; |
| 741 | int ret; |
| 742 | |
| 743 | ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); |
| 744 | if (ret < 0) |
| 745 | goto err_path; |
| 746 | if (strlist_add(node_inc, path)) |
| 747 | goto err_mem; |
| 748 | |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 749 | /* Add all this image's hashes */ |
| 750 | hash_count = 0; |
| 751 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 752 | noffset >= 0; |
| 753 | noffset = fdt_next_subnode(fit, noffset)) { |
| 754 | const char *name = fit_get_name(fit, noffset, NULL); |
| 755 | |
| 756 | if (strncmp(name, FIT_HASH_NODENAME, |
| 757 | strlen(FIT_HASH_NODENAME))) |
| 758 | continue; |
| 759 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 760 | if (ret < 0) |
| 761 | goto err_path; |
| 762 | if (strlist_add(node_inc, path)) |
| 763 | goto err_mem; |
| 764 | hash_count++; |
| 765 | } |
| 766 | |
| 767 | if (!hash_count) { |
| 768 | printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", |
| 769 | conf_name, sig_name, iname); |
| 770 | return -ENOMSG; |
| 771 | } |
| 772 | |
| 773 | /* Add this image's cipher node if present */ |
| 774 | noffset = fdt_subnode_offset(fit, image_noffset, |
| 775 | FIT_CIPHER_NODENAME); |
| 776 | if (noffset != -FDT_ERR_NOTFOUND) { |
| 777 | if (noffset < 0) { |
| 778 | printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n", |
| 779 | conf_name, sig_name, iname, |
| 780 | fdt_strerror(noffset)); |
| 781 | return -EIO; |
| 782 | } |
| 783 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 784 | if (ret < 0) |
| 785 | goto err_path; |
| 786 | if (strlist_add(node_inc, path)) |
| 787 | goto err_mem; |
| 788 | } |
| 789 | |
| 790 | return 0; |
| 791 | |
| 792 | err_mem: |
| 793 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 794 | sig_name); |
| 795 | return -ENOMEM; |
| 796 | |
| 797 | err_path: |
| 798 | printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n", |
| 799 | iname, conf_name, sig_name, fdt_strerror(ret)); |
| 800 | return -ENOENT; |
| 801 | } |
| 802 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 803 | /** |
| 804 | * fit_config_get_hash_list() - Get the regions to sign |
| 805 | * |
| 806 | * This calculates a list of nodes to hash for this particular configuration, |
| 807 | * returning it as a string list (struct strlist, not a devicetree string list) |
| 808 | * |
| 809 | * @fit: Pointer to the FIT format image header |
| 810 | * @conf_noffset: Offset of configuration node to sign (child of |
| 811 | * /configurations node) |
| 812 | * @sig_offset: Offset of signature node containing info about how to sign it |
| 813 | * (child of 'signatures' node) |
| 814 | * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot |
| 815 | * be found, -ENOMSG if ther were no images in the configuration |
| 816 | */ |
| 817 | static int fit_config_get_hash_list(const void *fit, int conf_noffset, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 818 | int sig_offset, struct strlist *node_inc) |
| 819 | { |
| 820 | int allow_missing; |
| 821 | const char *prop, *iname, *end; |
| 822 | const char *conf_name, *sig_name; |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 823 | char name[200]; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 824 | int image_count; |
| 825 | int ret, len; |
| 826 | |
| 827 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 828 | sig_name = fit_get_name(fit, sig_offset, NULL); |
| 829 | |
| 830 | /* |
| 831 | * Build a list of nodes we need to hash. We always need the root |
| 832 | * node and the configuration. |
| 833 | */ |
| 834 | strlist_init(node_inc); |
| 835 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); |
| 836 | if (strlist_add(node_inc, "/") || |
| 837 | strlist_add(node_inc, name)) |
| 838 | goto err_mem; |
| 839 | |
| 840 | /* Get a list of images that we intend to sign */ |
Heiko Schocher | fdfd520 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 841 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 842 | &allow_missing); |
| 843 | if (!prop) |
| 844 | return 0; |
| 845 | |
| 846 | /* Locate the images */ |
| 847 | end = prop + len; |
| 848 | image_count = 0; |
| 849 | for (iname = prop; iname < end; iname += strlen(iname) + 1) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 850 | int image_noffset; |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 851 | int index, max_index; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 852 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 853 | max_index = fdt_stringlist_count(fit, conf_noffset, iname); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 854 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 855 | for (index = 0; index < max_index; index++) { |
| 856 | image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset, |
| 857 | iname, index); |
| 858 | |
| 859 | if (image_noffset < 0) { |
| 860 | printf("Failed to find image '%s' in configuration '%s/%s'\n", |
| 861 | iname, conf_name, sig_name); |
| 862 | if (allow_missing) |
| 863 | continue; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 864 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 865 | return -ENOENT; |
| 866 | } |
Patrick Oppenlander | 5964c3f | 2020-07-30 14:30:47 +1000 | [diff] [blame] | 867 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 868 | ret = fit_config_add_hash(fit, image_noffset, node_inc, |
| 869 | conf_name, sig_name, iname); |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | return ret; |
| 872 | |
| 873 | image_count++; |
| 874 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | if (!image_count) { |
| 878 | printf("Failed to find any images for configuration '%s/%s'\n", |
| 879 | conf_name, sig_name); |
| 880 | return -ENOMSG; |
| 881 | } |
| 882 | |
| 883 | return 0; |
| 884 | |
| 885 | err_mem: |
| 886 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 887 | sig_name); |
| 888 | return -ENOMEM; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 891 | /** |
| 892 | * fit_config_get_regions() - Get the regions to sign |
| 893 | * |
| 894 | * This calculates a list of node to hash for this particular configuration, |
| 895 | * then finds which regions of the devicetree they correspond to. |
| 896 | * |
| 897 | * @fit: Pointer to the FIT format image header |
| 898 | * @conf_noffset: Offset of configuration node to sign (child of |
| 899 | * /configurations node) |
| 900 | * @sig_offset: Offset of signature node containing info about how to sign it |
| 901 | * (child of 'signatures' node) |
| 902 | * @regionp: Returns list of regions that need to be hashed (allocated; must be |
| 903 | * freed by the caller) |
| 904 | * @region_count: Returns number of regions |
| 905 | * @region_propp: Returns string-list property containing the list of nodes |
| 906 | * that correspond to the regions. Each entry is a full path to the node. |
| 907 | * This is in devicetree format, i.e. a \0 between each string. This is |
| 908 | * allocated and must be freed by the caller. |
| 909 | * @region_proplen: Returns length of *@@region_propp in bytes |
| 910 | * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could |
| 911 | * not be found, -EINVAL if no registers were found to hash |
| 912 | */ |
| 913 | static int fit_config_get_regions(const void *fit, int conf_noffset, |
| 914 | int sig_offset, struct image_region **regionp, |
| 915 | int *region_countp, char **region_propp, |
| 916 | int *region_proplen) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 917 | { |
| 918 | char * const exc_prop[] = {"data"}; |
| 919 | struct strlist node_inc; |
| 920 | struct image_region *region; |
| 921 | struct fdt_region fdt_regions[100]; |
| 922 | const char *conf_name, *sig_name; |
| 923 | char path[200]; |
| 924 | int count, i; |
| 925 | char *region_prop; |
| 926 | int ret, len; |
| 927 | |
| 928 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 929 | sig_name = fit_get_name(fit, sig_offset, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 930 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 931 | |
| 932 | /* Get a list of nodes we want to hash */ |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 933 | ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset, |
| 934 | &node_inc); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 935 | if (ret) |
| 936 | return ret; |
| 937 | |
| 938 | /* Get a list of regions to hash */ |
| 939 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 940 | exc_prop, ARRAY_SIZE(exc_prop), |
| 941 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 942 | path, sizeof(path), 1); |
| 943 | if (count < 0) { |
| 944 | printf("Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 945 | sig_name, fdt_strerror(ret)); |
| 946 | return -EIO; |
| 947 | } |
| 948 | if (count == 0) { |
| 949 | printf("No data to hash for configuration '%s/%s': %s\n", |
| 950 | conf_name, sig_name, fdt_strerror(ret)); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
| 954 | /* Build our list of data blocks */ |
| 955 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 956 | if (!region) { |
| 957 | printf("Out of memory hashing configuration '%s/%s'\n", |
| 958 | conf_name, sig_name); |
| 959 | return -ENOMEM; |
| 960 | } |
| 961 | |
| 962 | /* Create a list of all hashed properties */ |
| 963 | debug("Hash nodes:\n"); |
| 964 | for (i = len = 0; i < node_inc.count; i++) { |
| 965 | debug(" %s\n", node_inc.strings[i]); |
| 966 | len += strlen(node_inc.strings[i]) + 1; |
| 967 | } |
| 968 | region_prop = malloc(len); |
| 969 | if (!region_prop) { |
| 970 | printf("Out of memory setting up regions for configuration '%s/%s'\n", |
| 971 | conf_name, sig_name); |
| 972 | return -ENOMEM; |
| 973 | } |
| 974 | for (i = len = 0; i < node_inc.count; |
| 975 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 976 | strcpy(region_prop + len, node_inc.strings[i]); |
| 977 | strlist_free(&node_inc); |
| 978 | |
| 979 | *region_countp = count; |
| 980 | *regionp = region; |
| 981 | *region_propp = region_prop; |
| 982 | *region_proplen = len; |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 987 | /** |
| 988 | * fit_config_process_sig - Process a single subnode of the configurations/ node |
| 989 | * |
| 990 | * Generate a signed hash of the supplied data and store it in the node. |
| 991 | * |
| 992 | * @keydir: Directory containing keys to use for signing |
| 993 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
| 994 | * @fit: pointer to the FIT format image header |
| 995 | * @conf_name name of config being processed (used to display errors) |
| 996 | * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1' |
| 997 | * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1' |
| 998 | * @comment: Comment to add to signature nodes |
| 999 | * @require_keys: Mark all keys as 'required' |
| 1000 | * @engine_id: Engine to use for signing |
| 1001 | * @cmdname: Command name used when reporting errors |
| 1002 | * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code |
| 1003 | * on failure |
| 1004 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1005 | static int fit_config_process_sig(const char *keydir, const char *keyfile, |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1006 | void *keydest, void *fit, const char *conf_name, |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1007 | int conf_noffset, int noffset, const char *comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1008 | int require_keys, const char *engine_id, const char *cmdname, |
| 1009 | const char *algo_name) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1010 | { |
| 1011 | struct image_sign_info info; |
| 1012 | const char *node_name; |
| 1013 | struct image_region *region; |
| 1014 | char *region_prop; |
| 1015 | int region_proplen; |
| 1016 | int region_count; |
| 1017 | uint8_t *value; |
| 1018 | uint value_len; |
| 1019 | int ret; |
| 1020 | |
| 1021 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1022 | if (fit_config_get_regions(fit, conf_noffset, noffset, ®ion, |
| 1023 | ®ion_count, ®ion_prop, |
| 1024 | ®ion_proplen)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1025 | return -1; |
| 1026 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1027 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1028 | require_keys ? "conf" : NULL, engine_id, |
| 1029 | algo_name)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1030 | return -1; |
| 1031 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1032 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 1033 | &value_len); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1034 | free(region); |
| 1035 | if (ret) { |
| 1036 | printf("Failed to sign '%s' signature node in '%s' conf node\n", |
| 1037 | node_name, conf_name); |
| 1038 | |
| 1039 | /* We allow keys to be missing */ |
| 1040 | if (ret == -ENOENT) |
| 1041 | return 0; |
| 1042 | return -1; |
| 1043 | } |
| 1044 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1045 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1046 | region_prop, region_proplen, cmdname, |
| 1047 | algo_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1048 | if (ret) { |
| 1049 | if (ret == -FDT_ERR_NOSPACE) |
| 1050 | return -ENOSPC; |
| 1051 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 1052 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
| 1055 | free(value); |
| 1056 | free(region_prop); |
| 1057 | |
| 1058 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 1059 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1060 | |
| 1061 | /* Write the public key into the supplied FDT file */ |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1062 | if (keydest) { |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1063 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 1064 | if (ret < 0) { |
Masahiro Yamada | fe391a7 | 2017-10-27 15:04:21 +0900 | [diff] [blame] | 1065 | printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1066 | node_name, conf_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1067 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 1068 | return ret; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1074 | static int fit_config_add_verification_data(const char *keydir, |
| 1075 | const char *keyfile, void *keydest, void *fit, int conf_noffset, |
| 1076 | const char *comment, int require_keys, const char *engine_id, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1077 | const char *cmdname, const char *algo_name, |
| 1078 | struct image_summary *summary) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1079 | { |
| 1080 | const char *conf_name; |
| 1081 | int noffset; |
| 1082 | |
| 1083 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 1084 | |
| 1085 | /* Process all hash subnodes of the configuration node */ |
| 1086 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 1087 | noffset >= 0; |
| 1088 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1089 | const char *node_name; |
| 1090 | int ret = 0; |
| 1091 | |
| 1092 | node_name = fit_get_name(fit, noffset, NULL); |
| 1093 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 1094 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1095 | ret = fit_config_process_sig(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1096 | fit, conf_name, conf_noffset, noffset, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1097 | require_keys, engine_id, cmdname, algo_name); |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1098 | if (ret < 0) |
| 1099 | return ret; |
| 1100 | |
| 1101 | summary->sig_offset = noffset; |
| 1102 | fdt_get_path(fit, noffset, summary->sig_path, |
| 1103 | sizeof(summary->sig_path)); |
| 1104 | |
| 1105 | if (keydest) { |
| 1106 | summary->keydest_offset = ret; |
| 1107 | fdt_get_path(keydest, ret, |
| 1108 | summary->keydest_path, |
| 1109 | sizeof(summary->keydest_path)); |
| 1110 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1111 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1117 | /* |
| 1118 | * 0) open file (open) |
| 1119 | * 1) read certificate (PEM_read_X509) |
| 1120 | * 2) get public key (X509_get_pubkey) |
| 1121 | * 3) provide der format (d2i_RSAPublicKey) |
| 1122 | */ |
| 1123 | static int read_pub_key(const char *keydir, const void *name, |
| 1124 | unsigned char **pubkey, int *pubkey_len) |
| 1125 | { |
| 1126 | char path[1024]; |
| 1127 | EVP_PKEY *key = NULL; |
| 1128 | X509 *cert; |
| 1129 | FILE *f; |
| 1130 | int ret; |
| 1131 | |
| 1132 | memset(path, 0, 1024); |
| 1133 | snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name); |
| 1134 | |
| 1135 | /* Open certificate file */ |
| 1136 | f = fopen(path, "r"); |
| 1137 | if (!f) { |
| 1138 | fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", |
| 1139 | path, strerror(errno)); |
| 1140 | return -EACCES; |
| 1141 | } |
| 1142 | |
| 1143 | /* Read the certificate */ |
| 1144 | cert = NULL; |
| 1145 | if (!PEM_read_X509(f, &cert, NULL, NULL)) { |
| 1146 | printf("Couldn't read certificate"); |
| 1147 | ret = -EINVAL; |
| 1148 | goto err_cert; |
| 1149 | } |
| 1150 | |
| 1151 | /* Get the public key from the certificate. */ |
| 1152 | key = X509_get_pubkey(cert); |
| 1153 | if (!key) { |
| 1154 | printf("Couldn't read public key\n"); |
| 1155 | ret = -EINVAL; |
| 1156 | goto err_pubkey; |
| 1157 | } |
| 1158 | |
| 1159 | /* Get DER form */ |
| 1160 | ret = i2d_PublicKey(key, pubkey); |
| 1161 | if (ret < 0) { |
| 1162 | printf("Couldn't get DER form\n"); |
| 1163 | ret = -EINVAL; |
| 1164 | goto err_pubkey; |
| 1165 | } |
| 1166 | |
| 1167 | *pubkey_len = ret; |
| 1168 | ret = 0; |
| 1169 | |
| 1170 | err_pubkey: |
| 1171 | X509_free(cert); |
| 1172 | err_cert: |
| 1173 | fclose(f); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | int fit_pre_load_data(const char *keydir, void *keydest, void *fit) |
| 1178 | { |
| 1179 | int pre_load_noffset; |
| 1180 | const void *algo_name; |
| 1181 | const void *key_name; |
| 1182 | unsigned char *pubkey = NULL; |
| 1183 | int ret, pubkey_len; |
| 1184 | |
| 1185 | if (!keydir || !keydest || !fit) |
| 1186 | return 0; |
| 1187 | |
| 1188 | /* Search node pre-load sig */ |
| 1189 | pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH); |
| 1190 | if (pre_load_noffset < 0) { |
| 1191 | ret = 0; |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL); |
| 1196 | key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL); |
| 1197 | |
| 1198 | /* Check that all mandatory properties are present */ |
| 1199 | if (!algo_name || !key_name) { |
| 1200 | if (!algo_name) |
| 1201 | printf("The property algo-name is missing in the node %s\n", |
| 1202 | IMAGE_PRE_LOAD_PATH); |
| 1203 | if (!key_name) |
| 1204 | printf("The property key-name is missing in the node %s\n", |
| 1205 | IMAGE_PRE_LOAD_PATH); |
Mark Kettenis | d4ba0b4 | 2022-04-26 19:24:38 +0200 | [diff] [blame] | 1206 | ret = -EINVAL; |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1207 | goto out; |
| 1208 | } |
| 1209 | |
| 1210 | /* Read public key */ |
| 1211 | ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len); |
| 1212 | if (ret < 0) |
| 1213 | goto out; |
| 1214 | |
| 1215 | /* Add the public key to the device tree */ |
| 1216 | ret = fdt_setprop(keydest, pre_load_noffset, "public-key", |
| 1217 | pubkey, pubkey_len); |
| 1218 | if (ret) |
| 1219 | printf("Can't set public-key in node %s (ret = %d)\n", |
| 1220 | IMAGE_PRE_LOAD_PATH, ret); |
| 1221 | |
| 1222 | out: |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1226 | int fit_cipher_data(const char *keydir, void *keydest, void *fit, |
| 1227 | const char *comment, int require_keys, |
| 1228 | const char *engine_id, const char *cmdname) |
| 1229 | { |
| 1230 | int images_noffset; |
| 1231 | int noffset; |
| 1232 | int ret; |
| 1233 | |
| 1234 | /* Find images parent node offset */ |
| 1235 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1236 | if (images_noffset < 0) { |
| 1237 | printf("Can't find images parent node '%s' (%s)\n", |
| 1238 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1239 | return images_noffset; |
| 1240 | } |
| 1241 | |
| 1242 | /* Process its subnodes, print out component images details */ |
| 1243 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1244 | noffset >= 0; |
| 1245 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1246 | /* |
| 1247 | * Direct child node of the images parent node, |
| 1248 | * i.e. component image node. |
| 1249 | */ |
| 1250 | ret = fit_image_cipher_data(keydir, keydest, |
| 1251 | fit, noffset, comment, |
| 1252 | require_keys, engine_id, |
| 1253 | cmdname); |
| 1254 | if (ret) |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1261 | int fit_add_verification_data(const char *keydir, const char *keyfile, |
| 1262 | void *keydest, void *fit, const char *comment, |
| 1263 | int require_keys, const char *engine_id, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1264 | const char *cmdname, const char *algo_name, |
| 1265 | struct image_summary *summary) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1266 | { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1267 | int images_noffset, confs_noffset; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1268 | int noffset; |
| 1269 | int ret; |
| 1270 | |
| 1271 | /* Find images parent node offset */ |
| 1272 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1273 | if (images_noffset < 0) { |
| 1274 | printf("Can't find images parent node '%s' (%s)\n", |
| 1275 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1276 | return images_noffset; |
| 1277 | } |
| 1278 | |
| 1279 | /* Process its subnodes, print out component images details */ |
| 1280 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1281 | noffset >= 0; |
| 1282 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1283 | /* |
| 1284 | * Direct child node of the images parent node, |
| 1285 | * i.e. component image node. |
| 1286 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1287 | ret = fit_image_add_verification_data(keydir, keyfile, keydest, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1288 | fit, noffset, comment, require_keys, engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1289 | cmdname, algo_name); |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1290 | if (ret) |
| 1291 | return ret; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1294 | /* If there are no keys, we can't sign configurations */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1295 | if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1296 | return 0; |
| 1297 | |
| 1298 | /* Find configurations parent node offset */ |
| 1299 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1300 | if (confs_noffset < 0) { |
| 1301 | printf("Can't find images parent node '%s' (%s)\n", |
Heiko Schocher | 441f48d | 2014-08-11 11:02:17 +0200 | [diff] [blame] | 1302 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1303 | return -ENOENT; |
| 1304 | } |
| 1305 | |
| 1306 | /* Process its subnodes, print out component images details */ |
| 1307 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 1308 | noffset >= 0; |
| 1309 | noffset = fdt_next_subnode(fit, noffset)) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1310 | ret = fit_config_add_verification_data(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1311 | fit, noffset, comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1312 | require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1313 | engine_id, cmdname, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1314 | algo_name, summary); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1315 | if (ret) |
| 1316 | return ret; |
| 1317 | } |
| 1318 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1321 | |
| 1322 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1323 | int fit_check_sign(const void *fit, const void *key, |
| 1324 | const char *fit_uname_config) |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1325 | { |
| 1326 | int cfg_noffset; |
| 1327 | int ret; |
| 1328 | |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1329 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1330 | if (!cfg_noffset) |
| 1331 | return -1; |
| 1332 | |
Simon Glass | a559bb2 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 1333 | printf("Verifying Hash Integrity for node '%s'... ", |
| 1334 | fdt_get_name(fit, cfg_noffset, NULL)); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1335 | ret = fit_config_verify(fit, cfg_noffset); |
| 1336 | if (ret) |
| 1337 | return ret; |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1338 | printf("Verified OK, loading images\n"); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1339 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 1340 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1341 | return ret; |
| 1342 | } |
| 1343 | #endif |