image-fit-sig.c 12 KB

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