image-fit-sig.c 11 KB

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