image-fit-sig.c 11 KB

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