Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 604f23d | 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 | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Masahiro Yamada | 64045a6 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 13 | #include <fdt_region.h> |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 16 | |
Philippe Reynes | 6e052d1 | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 17 | #include <openssl/pem.h> |
| 18 | #include <openssl/evp.h> |
| 19 | |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 20 | /** |
Simon Glass | b726091 | 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 | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 44 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | b726091 | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /** |
Simon Glass | 94e5fa4 | 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 | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 54 | * a hash of the supplied data and store it in the node. |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 55 | * |
| 56 | * @fit: pointer to the FIT format image header |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 57 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | 94e5fa4 | 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 | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 61 | * Return: 0 if ok, -1 on error |
Simon Glass | 94e5fa4 | 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 | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 67 | const char *node_name; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 68 | int value_len; |
Jan Kiszka | 4550ce9 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 69 | const char *algo; |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 70 | int ret; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 71 | |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 72 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 94e5fa4 | 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 | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 76 | node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 77 | return -ENOENT; |
Simon Glass | 94e5fa4 | 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 | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 82 | algo, node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 83 | return -EPROTONOSUPPORT; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | 1152a05 | 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 | 94e5fa4 | 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 | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 89 | node_name, image_name); |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 90 | return ret; |
Simon Glass | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 97 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 98 | * |
Simon Glass | 56518e7 | 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 | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 113 | int region_proplen, const char *cmdname, const char *algo_name) |
Simon Glass | 56518e7 | 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 | 795f452 | 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 | 7c39799 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 138 | uint32_t t = cpu_to_uimage(timestamp); |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 139 | |
Ming Liu | 7c39799 | 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 | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 142 | } |
Simon Glass | 56518e7 | 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 | 7346c1e | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 148 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | 56518e7 | 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 | 5902a39 | 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 | 56518e7 | 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 | 36bfcb6 | 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 | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 165 | const char *engine_id, const char *algo_name) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 166 | { |
| 167 | const char *node_name; |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 168 | const char *padding_name; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 169 | |
| 170 | node_name = fit_get_name(fit, noffset, NULL); |
Jan Kiszka | 5902a39 | 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 | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 179 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 180 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 181 | memset(info, '\0', sizeof(*info)); |
| 182 | info->keydir = keydir; |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 183 | info->keyfile = keyfile; |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 184 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 185 | info->fit = fit; |
| 186 | info->node_offset = noffset; |
Masahiro Yamada | 1d88a99 | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 187 | info->name = strdup(algo_name); |
Andrew Duda | 83dd98e | 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 | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 190 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 191 | info->require_keys = require_keys; |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 192 | info->engine_id = engine_id; |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 193 | if (!info->checksum || !info->crypto) { |
Simon Glass | 56518e7 | 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 | 70e6bcc | 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 | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 207 | * |
| 208 | * @keydir: Directory containing keys to use for signing |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 209 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 210 | * @fit: pointer to the FIT format image header |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 211 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | 56518e7 | 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 | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 217 | * @engine_id: Engine to use for signing |
Simon Glass | 9737c2d | 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 | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 220 | */ |
Alexandru Gagniuc | 36bfcb6 | 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 | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 223 | int noffset, const void *data, size_t size, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 224 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 225 | const char *cmdname, const char *algo_name) |
Simon Glass | 56518e7 | 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 | 36bfcb6 | 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 | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 236 | engine_id, algo_name)) |
Simon Glass | 56518e7 | 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 | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 242 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | 56518e7 | 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 | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 254 | NULL, 0, cmdname, algo_name); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 255 | if (ret) { |
Simon Glass | a946811 | 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 | 56518e7 | 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 | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 265 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 266 | |
mario.six@gdsys.cc | 713fb2d | 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 | c236ebd | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 269 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | 713fb2d | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 270 | * size values |
| 271 | */ |
Masahiro Yamada | 6793d01 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 272 | if (keydest) { |
| 273 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | c033dc8 | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 274 | if (ret < 0) { |
Masahiro Yamada | 6793d01 | 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 | 9737c2d | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 279 | /* Return the node that was written to */ |
| 280 | return ret; |
Masahiro Yamada | 6793d01 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 281 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Philippe Reynes | 7298e42 | 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 | 3311eda | 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 | 7298e42 | 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 | 2c6bcab | 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 | 3311eda | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 326 | filename, n, (long long)sbuf.st_size); |
Philippe Reynes | 7298e42 | 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 | a6982a6 | 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 | 7f0f4e1 | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 341 | int i, ret; |
Philippe Reynes | a6982a6 | 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 | 7f0f4e1 | 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 | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 353 | goto out; |
| 354 | } |
| 355 | |
Philippe Reynes | cc34f04 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 356 | srandom(date.tv_nsec); |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 357 | |
| 358 | for (i = 0; i < size; i++) { |
Philippe Reynes | cc34f04 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 359 | *tmp = random() & 0xff; |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 360 | tmp++; |
| 361 | } |
| 362 | |
| 363 | out: |
| 364 | return ret; |
| 365 | } |
| 366 | |
Philippe Reynes | 7298e42 | 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 | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 370 | int noffset) |
Philippe Reynes | 7298e42 | 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 | c520266 | 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 | 7298e42 | 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 | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 385 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 386 | if (!info->keyname) { |
Patrick Oppenlander | c520266 | 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 | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 389 | goto out; |
| 390 | } |
| 391 | |
Philippe Reynes | a6982a6 | 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 | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 398 | info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL); |
Philippe Reynes | 7298e42 | 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 | 7298e42 | 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 | a6982a6 | 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 | 7298e42 | 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 | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 452 | /* Replace data with ciphered data */ |
Philippe Reynes | 7298e42 | 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 | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 455 | if (ret == -FDT_ERR_NOSPACE) { |
| 456 | ret = -ENOSPC; |
| 457 | goto out; |
| 458 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 459 | if (ret) { |
Patrick Oppenlander | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 460 | printf("Can't replace data with ciphered data (err = %d)\n", ret); |
Philippe Reynes | 7298e42 | 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 | 04aeebb | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 466 | if (ret == -FDT_ERR_NOSPACE) { |
| 467 | ret = -ENOSPC; |
| 468 | goto out; |
| 469 | } |
Philippe Reynes | 7298e42 | 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 | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 482 | int node_noffset, const void *data, size_t size, |
Philippe Reynes | 7298e42 | 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 | c520266 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 493 | image_noffset, node_noffset); |
Philippe Reynes | 7298e42 | 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 | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 506 | * And, if needed, write the iv in the FIT file |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 507 | */ |
| 508 | if (keydest) { |
Philippe Reynes | a6982a6 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 509 | ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset); |
Philippe Reynes | 7298e42 | 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 | b33e5cc | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 536 | int cipher_node_offset, len; |
Philippe Reynes | 7298e42 | 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 | b33e5cc | 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 | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 564 | |
Patrick Oppenlander | c520266 | 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 | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 573 | } |
Patrick Oppenlander | c520266 | 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 | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Simon Glass | 56518e7 | 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 | bbb467d | 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 | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 588 | * |
| 589 | * Input component image node structure: |
| 590 | * |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 591 | * o image-1 (at image_noffset) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 592 | * | - data = [binary data] |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 593 | * o hash-1 |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 594 | * |- algo = "sha1" |
| 595 | * |
| 596 | * Output component image node structure: |
| 597 | * |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 598 | * o image-1 (at image_noffset) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 599 | * | - data = [binary data] |
Andre Przywara | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 600 | * o hash-1 |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 601 | * |- algo = "sha1" |
| 602 | * |- value = sha1(data) |
| 603 | * |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 604 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 605 | * |
Simon Glass | 56518e7 | 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 | bbb467d | 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 | 56518e7 | 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 | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 612 | * @engine_id: Engine to use for signing |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 613 | * @return: 0 on success, <0 on failure |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 614 | */ |
Alexandru Gagniuc | 36bfcb6 | 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 | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 618 | const char *cmdname, const char* algo_name) |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 619 | { |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 620 | const char *image_name; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 621 | const void *data; |
| 622 | size_t size; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 623 | int noffset; |
Simon Glass | 604f23d | 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 | 94e5fa4 | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 631 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 632 | |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 633 | /* Process all hash subnodes of the component image node */ |
Simon Glass | bbb467d | 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 | b2267e8 | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 643 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | bbb467d | 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 | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 650 | } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) && |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 651 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 652 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 653 | ret = fit_image_process_sig(keydir, keyfile, keydest, |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 654 | fit, image_name, noffset, data, size, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 655 | comment, require_keys, engine_id, cmdname, |
| 656 | algo_name); |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 657 | } |
Simon Glass | 9737c2d | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 658 | if (ret < 0) |
Simon Glass | 1152a05 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 659 | return ret; |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
Simon Glass | 4d09852 | 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 | 70e6bcc | 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 | 4d09852 | 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 | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 705 | /* If there is an "sign-image" property, use that */ |
Simon Glass | 4d09852 | 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 | 70e6bcc | 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 | 5a4116f | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 737 | { |
Simon Glass | 4842234 | 2021-11-12 12:28:07 -0700 | [diff] [blame] | 738 | char path[200]; |
Philippe Reynes | 5a4116f | 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 | 5a4116f | 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 | 70e6bcc | 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 | 4d09852 | 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 | 5a4116f | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 823 | char name[200]; |
Simon Glass | 4d09852 | 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 | 66b36f8 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 841 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 4d09852 | 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 | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 850 | int image_noffset; |
Philippe Reynes | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 851 | int index, max_index; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 852 | |
Philippe Reynes | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 853 | max_index = fdt_stringlist_count(fit, conf_noffset, iname); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 854 | |
Philippe Reynes | edfeba7 | 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; |
| 864 | |
| 865 | return -ENOENT; |
| 866 | } |
| 867 | |
Simon Glass | 70e6bcc | 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 | edfeba7 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | return ret; |
| 872 | |
| 873 | image_count++; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 874 | } |
Simon Glass | 4d09852 | 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 | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Simon Glass | 70e6bcc | 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 | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 917 | { |
Sean Anderson | 0abe332 | 2022-10-20 15:41:10 -0400 | [diff] [blame] | 918 | char * const exc_prop[] = { |
| 919 | FIT_DATA_PROP, |
| 920 | FIT_DATA_SIZE_PROP, |
| 921 | FIT_DATA_POSITION_PROP, |
| 922 | FIT_DATA_OFFSET_PROP, |
| 923 | }; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 924 | struct strlist node_inc; |
| 925 | struct image_region *region; |
| 926 | struct fdt_region fdt_regions[100]; |
| 927 | const char *conf_name, *sig_name; |
| 928 | char path[200]; |
| 929 | int count, i; |
| 930 | char *region_prop; |
| 931 | int ret, len; |
| 932 | |
| 933 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 934 | sig_name = fit_get_name(fit, sig_offset, NULL); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 935 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 936 | |
| 937 | /* Get a list of nodes we want to hash */ |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 938 | ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset, |
| 939 | &node_inc); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 940 | if (ret) |
| 941 | return ret; |
| 942 | |
| 943 | /* Get a list of regions to hash */ |
| 944 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 945 | exc_prop, ARRAY_SIZE(exc_prop), |
| 946 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 947 | path, sizeof(path), 1); |
| 948 | if (count < 0) { |
| 949 | printf("Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 950 | sig_name, fdt_strerror(ret)); |
| 951 | return -EIO; |
| 952 | } |
| 953 | if (count == 0) { |
| 954 | printf("No data to hash for configuration '%s/%s': %s\n", |
| 955 | conf_name, sig_name, fdt_strerror(ret)); |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | |
| 959 | /* Build our list of data blocks */ |
| 960 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 961 | if (!region) { |
| 962 | printf("Out of memory hashing configuration '%s/%s'\n", |
| 963 | conf_name, sig_name); |
| 964 | return -ENOMEM; |
| 965 | } |
| 966 | |
| 967 | /* Create a list of all hashed properties */ |
| 968 | debug("Hash nodes:\n"); |
| 969 | for (i = len = 0; i < node_inc.count; i++) { |
| 970 | debug(" %s\n", node_inc.strings[i]); |
| 971 | len += strlen(node_inc.strings[i]) + 1; |
| 972 | } |
| 973 | region_prop = malloc(len); |
| 974 | if (!region_prop) { |
| 975 | printf("Out of memory setting up regions for configuration '%s/%s'\n", |
| 976 | conf_name, sig_name); |
| 977 | return -ENOMEM; |
| 978 | } |
| 979 | for (i = len = 0; i < node_inc.count; |
| 980 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 981 | strcpy(region_prop + len, node_inc.strings[i]); |
| 982 | strlist_free(&node_inc); |
| 983 | |
| 984 | *region_countp = count; |
| 985 | *regionp = region; |
| 986 | *region_propp = region_prop; |
| 987 | *region_proplen = len; |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
Simon Glass | 9737c2d | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 992 | /** |
| 993 | * fit_config_process_sig - Process a single subnode of the configurations/ node |
| 994 | * |
| 995 | * Generate a signed hash of the supplied data and store it in the node. |
| 996 | * |
| 997 | * @keydir: Directory containing keys to use for signing |
| 998 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
| 999 | * @fit: pointer to the FIT format image header |
| 1000 | * @conf_name name of config being processed (used to display errors) |
| 1001 | * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1' |
| 1002 | * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1' |
| 1003 | * @comment: Comment to add to signature nodes |
| 1004 | * @require_keys: Mark all keys as 'required' |
| 1005 | * @engine_id: Engine to use for signing |
| 1006 | * @cmdname: Command name used when reporting errors |
| 1007 | * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code |
| 1008 | * on failure |
| 1009 | */ |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1010 | static int fit_config_process_sig(const char *keydir, const char *keyfile, |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1011 | void *keydest, void *fit, const char *conf_name, |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1012 | int conf_noffset, int noffset, const char *comment, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1013 | int require_keys, const char *engine_id, const char *cmdname, |
| 1014 | const char *algo_name) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1015 | { |
| 1016 | struct image_sign_info info; |
| 1017 | const char *node_name; |
| 1018 | struct image_region *region; |
| 1019 | char *region_prop; |
| 1020 | int region_proplen; |
| 1021 | int region_count; |
| 1022 | uint8_t *value; |
| 1023 | uint value_len; |
| 1024 | int ret; |
| 1025 | |
| 1026 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 70e6bcc | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1027 | if (fit_config_get_regions(fit, conf_noffset, noffset, ®ion, |
| 1028 | ®ion_count, ®ion_prop, |
| 1029 | ®ion_proplen)) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1030 | return -1; |
| 1031 | |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1032 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1033 | require_keys ? "conf" : NULL, engine_id, |
| 1034 | algo_name)) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1035 | return -1; |
| 1036 | |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1037 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 1038 | &value_len); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1039 | free(region); |
| 1040 | if (ret) { |
| 1041 | printf("Failed to sign '%s' signature node in '%s' conf node\n", |
| 1042 | node_name, conf_name); |
| 1043 | |
| 1044 | /* We allow keys to be missing */ |
| 1045 | if (ret == -ENOENT) |
| 1046 | return 0; |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1050 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1051 | region_prop, region_proplen, cmdname, |
| 1052 | algo_name); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1053 | if (ret) { |
| 1054 | if (ret == -FDT_ERR_NOSPACE) |
| 1055 | return -ENOSPC; |
| 1056 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 1057 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1058 | return -1; |
| 1059 | } |
| 1060 | free(value); |
| 1061 | free(region_prop); |
| 1062 | |
| 1063 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | 72188f5 | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 1064 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1065 | |
| 1066 | /* Write the public key into the supplied FDT file */ |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1067 | if (keydest) { |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1068 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | c033dc8 | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 1069 | if (ret < 0) { |
Masahiro Yamada | 76b9cba | 2017-10-27 15:04:21 +0900 | [diff] [blame] | 1070 | printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1071 | node_name, conf_name); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1072 | } |
Simon Glass | 9737c2d | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 1073 | return ret; |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1079 | static int fit_config_add_verification_data(const char *keydir, |
| 1080 | const char *keyfile, void *keydest, void *fit, int conf_noffset, |
| 1081 | const char *comment, int require_keys, const char *engine_id, |
Simon Glass | 2d2384b | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1082 | const char *cmdname, const char *algo_name, |
| 1083 | struct image_summary *summary) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1084 | { |
| 1085 | const char *conf_name; |
| 1086 | int noffset; |
| 1087 | |
| 1088 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 1089 | |
| 1090 | /* Process all hash subnodes of the configuration node */ |
| 1091 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 1092 | noffset >= 0; |
| 1093 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1094 | const char *node_name; |
| 1095 | int ret = 0; |
| 1096 | |
| 1097 | node_name = fit_get_name(fit, noffset, NULL); |
| 1098 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 1099 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1100 | ret = fit_config_process_sig(keydir, keyfile, keydest, |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1101 | fit, conf_name, conf_noffset, noffset, comment, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1102 | require_keys, engine_id, cmdname, algo_name); |
Simon Glass | 2d2384b | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1103 | if (ret < 0) |
| 1104 | return ret; |
| 1105 | |
| 1106 | summary->sig_offset = noffset; |
| 1107 | fdt_get_path(fit, noffset, summary->sig_path, |
| 1108 | sizeof(summary->sig_path)); |
| 1109 | |
| 1110 | if (keydest) { |
| 1111 | summary->keydest_offset = ret; |
| 1112 | fdt_get_path(keydest, ret, |
| 1113 | summary->keydest_path, |
| 1114 | sizeof(summary->keydest_path)); |
| 1115 | } |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1116 | } |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
Philippe Reynes | 6e052d1 | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1122 | /* |
| 1123 | * 0) open file (open) |
| 1124 | * 1) read certificate (PEM_read_X509) |
| 1125 | * 2) get public key (X509_get_pubkey) |
| 1126 | * 3) provide der format (d2i_RSAPublicKey) |
| 1127 | */ |
| 1128 | static int read_pub_key(const char *keydir, const void *name, |
| 1129 | unsigned char **pubkey, int *pubkey_len) |
| 1130 | { |
| 1131 | char path[1024]; |
| 1132 | EVP_PKEY *key = NULL; |
| 1133 | X509 *cert; |
| 1134 | FILE *f; |
| 1135 | int ret; |
| 1136 | |
| 1137 | memset(path, 0, 1024); |
| 1138 | snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name); |
| 1139 | |
| 1140 | /* Open certificate file */ |
| 1141 | f = fopen(path, "r"); |
| 1142 | if (!f) { |
| 1143 | fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", |
| 1144 | path, strerror(errno)); |
| 1145 | return -EACCES; |
| 1146 | } |
| 1147 | |
| 1148 | /* Read the certificate */ |
| 1149 | cert = NULL; |
| 1150 | if (!PEM_read_X509(f, &cert, NULL, NULL)) { |
| 1151 | printf("Couldn't read certificate"); |
| 1152 | ret = -EINVAL; |
| 1153 | goto err_cert; |
| 1154 | } |
| 1155 | |
| 1156 | /* Get the public key from the certificate. */ |
| 1157 | key = X509_get_pubkey(cert); |
| 1158 | if (!key) { |
| 1159 | printf("Couldn't read public key\n"); |
| 1160 | ret = -EINVAL; |
| 1161 | goto err_pubkey; |
| 1162 | } |
| 1163 | |
| 1164 | /* Get DER form */ |
| 1165 | ret = i2d_PublicKey(key, pubkey); |
| 1166 | if (ret < 0) { |
| 1167 | printf("Couldn't get DER form\n"); |
| 1168 | ret = -EINVAL; |
| 1169 | goto err_pubkey; |
| 1170 | } |
| 1171 | |
| 1172 | *pubkey_len = ret; |
| 1173 | ret = 0; |
| 1174 | |
| 1175 | err_pubkey: |
| 1176 | X509_free(cert); |
| 1177 | err_cert: |
| 1178 | fclose(f); |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | int fit_pre_load_data(const char *keydir, void *keydest, void *fit) |
| 1183 | { |
| 1184 | int pre_load_noffset; |
| 1185 | const void *algo_name; |
| 1186 | const void *key_name; |
| 1187 | unsigned char *pubkey = NULL; |
| 1188 | int ret, pubkey_len; |
| 1189 | |
| 1190 | if (!keydir || !keydest || !fit) |
| 1191 | return 0; |
| 1192 | |
| 1193 | /* Search node pre-load sig */ |
| 1194 | pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH); |
| 1195 | if (pre_load_noffset < 0) { |
| 1196 | ret = 0; |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
| 1200 | algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL); |
| 1201 | key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL); |
| 1202 | |
| 1203 | /* Check that all mandatory properties are present */ |
| 1204 | if (!algo_name || !key_name) { |
| 1205 | if (!algo_name) |
| 1206 | printf("The property algo-name is missing in the node %s\n", |
| 1207 | IMAGE_PRE_LOAD_PATH); |
| 1208 | if (!key_name) |
| 1209 | printf("The property key-name is missing in the node %s\n", |
| 1210 | IMAGE_PRE_LOAD_PATH); |
Mark Kettenis | 6165718 | 2022-04-26 19:24:38 +0200 | [diff] [blame] | 1211 | ret = -EINVAL; |
Philippe Reynes | 6e052d1 | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1212 | goto out; |
| 1213 | } |
| 1214 | |
| 1215 | /* Read public key */ |
| 1216 | ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len); |
| 1217 | if (ret < 0) |
| 1218 | goto out; |
| 1219 | |
| 1220 | /* Add the public key to the device tree */ |
| 1221 | ret = fdt_setprop(keydest, pre_load_noffset, "public-key", |
| 1222 | pubkey, pubkey_len); |
| 1223 | if (ret) |
| 1224 | printf("Can't set public-key in node %s (ret = %d)\n", |
| 1225 | IMAGE_PRE_LOAD_PATH, ret); |
| 1226 | |
| 1227 | out: |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1231 | int fit_cipher_data(const char *keydir, void *keydest, void *fit, |
| 1232 | const char *comment, int require_keys, |
| 1233 | const char *engine_id, const char *cmdname) |
| 1234 | { |
| 1235 | int images_noffset; |
| 1236 | int noffset; |
| 1237 | int ret; |
| 1238 | |
| 1239 | /* Find images parent node offset */ |
| 1240 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1241 | if (images_noffset < 0) { |
| 1242 | printf("Can't find images parent node '%s' (%s)\n", |
| 1243 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1244 | return images_noffset; |
| 1245 | } |
| 1246 | |
| 1247 | /* Process its subnodes, print out component images details */ |
| 1248 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1249 | noffset >= 0; |
| 1250 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1251 | /* |
| 1252 | * Direct child node of the images parent node, |
| 1253 | * i.e. component image node. |
| 1254 | */ |
| 1255 | ret = fit_image_cipher_data(keydir, keydest, |
| 1256 | fit, noffset, comment, |
| 1257 | require_keys, engine_id, |
| 1258 | cmdname); |
| 1259 | if (ret) |
| 1260 | return ret; |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1266 | int fit_add_verification_data(const char *keydir, const char *keyfile, |
| 1267 | void *keydest, void *fit, const char *comment, |
| 1268 | int require_keys, const char *engine_id, |
Simon Glass | 2d2384b | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1269 | const char *cmdname, const char *algo_name, |
| 1270 | struct image_summary *summary) |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1271 | { |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1272 | int images_noffset, confs_noffset; |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1273 | int noffset; |
| 1274 | int ret; |
| 1275 | |
| 1276 | /* Find images parent node offset */ |
| 1277 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1278 | if (images_noffset < 0) { |
| 1279 | printf("Can't find images parent node '%s' (%s)\n", |
| 1280 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1281 | return images_noffset; |
| 1282 | } |
| 1283 | |
| 1284 | /* Process its subnodes, print out component images details */ |
| 1285 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1286 | noffset >= 0; |
| 1287 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1288 | /* |
| 1289 | * Direct child node of the images parent node, |
| 1290 | * i.e. component image node. |
| 1291 | */ |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1292 | ret = fit_image_add_verification_data(keydir, keyfile, keydest, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1293 | fit, noffset, comment, require_keys, engine_id, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1294 | cmdname, algo_name); |
Simon Glass | bbb467d | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1295 | if (ret) |
| 1296 | return ret; |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1299 | /* If there are no keys, we can't sign configurations */ |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1300 | if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile)) |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1301 | return 0; |
| 1302 | |
| 1303 | /* Find configurations parent node offset */ |
| 1304 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1305 | if (confs_noffset < 0) { |
| 1306 | printf("Can't find images parent node '%s' (%s)\n", |
Heiko Schocher | 04a710a | 2014-08-11 11:02:17 +0200 | [diff] [blame] | 1307 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1308 | return -ENOENT; |
| 1309 | } |
| 1310 | |
| 1311 | /* Process its subnodes, print out component images details */ |
| 1312 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 1313 | noffset >= 0; |
| 1314 | noffset = fdt_next_subnode(fit, noffset)) { |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1315 | ret = fit_config_add_verification_data(keydir, keyfile, keydest, |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1316 | fit, noffset, comment, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1317 | require_keys, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1318 | engine_id, cmdname, |
Simon Glass | 2d2384b | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1319 | algo_name, summary); |
Simon Glass | 4d09852 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1320 | if (ret) |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1326 | |
| 1327 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1328 | int fit_check_sign(const void *fit, const void *key, |
| 1329 | const char *fit_uname_config) |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1330 | { |
| 1331 | int cfg_noffset; |
| 1332 | int ret; |
| 1333 | |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1334 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1335 | if (!cfg_noffset) |
| 1336 | return -1; |
| 1337 | |
Simon Glass | 382cf62 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 1338 | printf("Verifying Hash Integrity for node '%s'... ", |
| 1339 | fdt_get_name(fit, cfg_noffset, NULL)); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1340 | ret = fit_config_verify(fit, cfg_noffset); |
| 1341 | if (ret) |
| 1342 | return ret; |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1343 | printf("Verified OK, loading images\n"); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1344 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 1345 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1346 | return ret; |
| 1347 | } |
| 1348 | #endif |