image-fit-sig.c 12 KB

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