image-host.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. // SPDX-License-Identifier: GPL-2.0+
  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.
  9. */
  10. #include "mkimage.h"
  11. #include <bootm.h>
  12. #include <fdt_region.h>
  13. #include <image.h>
  14. #include <version.h>
  15. /**
  16. * fit_set_hash_value - set hash value in requested has node
  17. * @fit: pointer to the FIT format image header
  18. * @noffset: hash node offset
  19. * @value: hash value to be set
  20. * @value_len: hash value length
  21. *
  22. * fit_set_hash_value() attempts to set hash value in a node at offset
  23. * given and returns operation status to the caller.
  24. *
  25. * returns
  26. * 0, on success
  27. * -1, on failure
  28. */
  29. static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
  30. int value_len)
  31. {
  32. int ret;
  33. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  34. if (ret) {
  35. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  36. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  37. fdt_strerror(ret));
  38. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  39. }
  40. return 0;
  41. }
  42. /**
  43. * fit_image_process_hash - Process a single subnode of the images/ node
  44. *
  45. * Check each subnode and process accordingly. For hash nodes we generate
  46. * a hash of the supplised data and store it in the node.
  47. *
  48. * @fit: pointer to the FIT format image header
  49. * @image_name: name of image being processes (used to display errors)
  50. * @noffset: subnode offset
  51. * @data: data to process
  52. * @size: size of data in bytes
  53. * @return 0 if ok, -1 on error
  54. */
  55. static int fit_image_process_hash(void *fit, const char *image_name,
  56. int noffset, const void *data, size_t size)
  57. {
  58. uint8_t value[FIT_MAX_HASH_LEN];
  59. const char *node_name;
  60. int value_len;
  61. char *algo;
  62. int ret;
  63. node_name = fit_get_name(fit, noffset, NULL);
  64. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  65. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  66. node_name, image_name);
  67. return -ENOENT;
  68. }
  69. if (calculate_hash(data, size, algo, value, &value_len)) {
  70. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  71. algo, node_name, image_name);
  72. return -EPROTONOSUPPORT;
  73. }
  74. ret = fit_set_hash_value(fit, noffset, value, value_len);
  75. if (ret) {
  76. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  77. node_name, image_name);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * fit_image_write_sig() - write the signature to a FIT
  84. *
  85. * This writes the signature and signer data to the FIT.
  86. *
  87. * @fit: pointer to the FIT format image header
  88. * @noffset: hash node offset
  89. * @value: signature value to be set
  90. * @value_len: signature value length
  91. * @comment: Text comment to write (NULL for none)
  92. *
  93. * returns
  94. * 0, on success
  95. * -FDT_ERR_..., on failure
  96. */
  97. static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
  98. int value_len, const char *comment, const char *region_prop,
  99. int region_proplen, const char *cmdname)
  100. {
  101. int string_size;
  102. int ret;
  103. /*
  104. * Get the current string size, before we update the FIT and add
  105. * more
  106. */
  107. string_size = fdt_size_dt_strings(fit);
  108. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  109. if (!ret) {
  110. ret = fdt_setprop_string(fit, noffset, "signer-name",
  111. "mkimage");
  112. }
  113. if (!ret) {
  114. ret = fdt_setprop_string(fit, noffset, "signer-version",
  115. PLAIN_VERSION);
  116. }
  117. if (comment && !ret)
  118. ret = fdt_setprop_string(fit, noffset, "comment", comment);
  119. if (!ret) {
  120. time_t timestamp = imagetool_get_source_date(cmdname,
  121. time(NULL));
  122. uint32_t t = cpu_to_uimage(timestamp);
  123. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  124. sizeof(uint32_t));
  125. }
  126. if (region_prop && !ret) {
  127. uint32_t strdata[2];
  128. ret = fdt_setprop(fit, noffset, "hashed-nodes",
  129. region_prop, region_proplen);
  130. /* This is a legacy offset, it is unused, and must remain 0. */
  131. strdata[0] = 0;
  132. strdata[1] = cpu_to_fdt32(string_size);
  133. if (!ret) {
  134. ret = fdt_setprop(fit, noffset, "hashed-strings",
  135. strdata, sizeof(strdata));
  136. }
  137. }
  138. return ret;
  139. }
  140. static int fit_image_setup_sig(struct image_sign_info *info,
  141. const char *keydir, const char *keyfile, void *fit,
  142. const char *image_name, int noffset, const char *require_keys,
  143. const char *engine_id)
  144. {
  145. const char *node_name;
  146. char *algo_name;
  147. const char *padding_name;
  148. node_name = fit_get_name(fit, noffset, NULL);
  149. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  150. printf("Can't get algo property for '%s' signature node in '%s' image node\n",
  151. node_name, image_name);
  152. return -1;
  153. }
  154. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  155. memset(info, '\0', sizeof(*info));
  156. info->keydir = keydir;
  157. info->keyfile = keyfile;
  158. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  159. info->fit = fit;
  160. info->node_offset = noffset;
  161. info->name = strdup(algo_name);
  162. info->checksum = image_get_checksum_algo(algo_name);
  163. info->crypto = image_get_crypto_algo(algo_name);
  164. info->padding = image_get_padding_algo(padding_name);
  165. info->require_keys = require_keys;
  166. info->engine_id = engine_id;
  167. if (!info->checksum || !info->crypto) {
  168. printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
  169. algo_name, node_name, image_name);
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. /**
  175. * fit_image_process_sig- Process a single subnode of the images/ node
  176. *
  177. * Check each subnode and process accordingly. For signature nodes we
  178. * generate a signed hash of the supplised data and store it in the node.
  179. *
  180. * @keydir: Directory containing keys to use for signing
  181. * @keydest: Destination FDT blob to write public keys into
  182. * @fit: pointer to the FIT format image header
  183. * @image_name: name of image being processes (used to display errors)
  184. * @noffset: subnode offset
  185. * @data: data to process
  186. * @size: size of data in bytes
  187. * @comment: Comment to add to signature nodes
  188. * @require_keys: Mark all keys as 'required'
  189. * @engine_id: Engine to use for signing
  190. * @return 0 if ok, -1 on error
  191. */
  192. static int fit_image_process_sig(const char *keydir, const char *keyfile,
  193. void *keydest, void *fit, const char *image_name,
  194. int noffset, const void *data, size_t size,
  195. const char *comment, int require_keys, const char *engine_id,
  196. const char *cmdname)
  197. {
  198. struct image_sign_info info;
  199. struct image_region region;
  200. const char *node_name;
  201. uint8_t *value;
  202. uint value_len;
  203. int ret;
  204. if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
  205. noffset, require_keys ? "image" : NULL,
  206. engine_id))
  207. return -1;
  208. node_name = fit_get_name(fit, noffset, NULL);
  209. region.data = data;
  210. region.size = size;
  211. ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
  212. if (ret) {
  213. printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
  214. node_name, image_name, ret);
  215. /* We allow keys to be missing */
  216. if (ret == -ENOENT)
  217. return 0;
  218. return -1;
  219. }
  220. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  221. NULL, 0, cmdname);
  222. if (ret) {
  223. if (ret == -FDT_ERR_NOSPACE)
  224. return -ENOSPC;
  225. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  226. node_name, image_name, fdt_strerror(ret));
  227. return -1;
  228. }
  229. free(value);
  230. /* Get keyname again, as FDT has changed and invalidated our pointer */
  231. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  232. /*
  233. * Write the public key into the supplied FDT file; this might fail
  234. * several times, since we try signing with successively increasing
  235. * size values
  236. */
  237. if (keydest) {
  238. ret = info.crypto->add_verify_data(&info, keydest);
  239. if (ret) {
  240. printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
  241. node_name, image_name);
  242. return ret;
  243. }
  244. }
  245. return 0;
  246. }
  247. static int fit_image_read_data(char *filename, unsigned char *data,
  248. int expected_size)
  249. {
  250. struct stat sbuf;
  251. int fd, ret = -1;
  252. ssize_t n;
  253. /* Open file */
  254. fd = open(filename, O_RDONLY | O_BINARY);
  255. if (fd < 0) {
  256. printf("Can't open file %s (err=%d => %s)\n",
  257. filename, errno, strerror(errno));
  258. return -1;
  259. }
  260. /* Compute file size */
  261. if (fstat(fd, &sbuf) < 0) {
  262. printf("Can't fstat file %s (err=%d => %s)\n",
  263. filename, errno, strerror(errno));
  264. goto err;
  265. }
  266. /* Check file size */
  267. if (sbuf.st_size != expected_size) {
  268. printf("File %s don't have the expected size (size=%lld, expected=%d)\n",
  269. filename, (long long)sbuf.st_size, expected_size);
  270. goto err;
  271. }
  272. /* Read data */
  273. n = read(fd, data, sbuf.st_size);
  274. if (n < 0) {
  275. printf("Can't read file %s (err=%d => %s)\n",
  276. filename, errno, strerror(errno));
  277. goto err;
  278. }
  279. /* Check that we have read all the file */
  280. if (n != sbuf.st_size) {
  281. printf("Can't read all file %s (read %zd bytes, expexted %lld)\n",
  282. filename, n, (long long)sbuf.st_size);
  283. goto err;
  284. }
  285. ret = 0;
  286. err:
  287. close(fd);
  288. return ret;
  289. }
  290. static int get_random_data(void *data, int size)
  291. {
  292. unsigned char *tmp = data;
  293. struct timespec date;
  294. int i, ret;
  295. if (!tmp) {
  296. printf("%s: pointer data is NULL\n", __func__);
  297. ret = -1;
  298. goto out;
  299. }
  300. ret = clock_gettime(CLOCK_MONOTONIC, &date);
  301. if (ret) {
  302. printf("%s: clock_gettime has failed (%s)\n", __func__,
  303. strerror(errno));
  304. goto out;
  305. }
  306. srandom(date.tv_nsec);
  307. for (i = 0; i < size; i++) {
  308. *tmp = random() & 0xff;
  309. tmp++;
  310. }
  311. out:
  312. return ret;
  313. }
  314. static int fit_image_setup_cipher(struct image_cipher_info *info,
  315. const char *keydir, void *fit,
  316. const char *image_name, int image_noffset,
  317. int noffset)
  318. {
  319. char *algo_name;
  320. char filename[128];
  321. int ret = -1;
  322. if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
  323. printf("Can't get algo name for cipher in image '%s'\n",
  324. image_name);
  325. goto out;
  326. }
  327. info->keydir = keydir;
  328. /* Read the key name */
  329. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  330. if (!info->keyname) {
  331. printf("Can't get key name for cipher in image '%s'\n",
  332. image_name);
  333. goto out;
  334. }
  335. /*
  336. * Read the IV name
  337. *
  338. * If this property is not provided then mkimage will generate
  339. * a random IV and store it in the FIT image
  340. */
  341. info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
  342. info->fit = fit;
  343. info->node_noffset = noffset;
  344. info->name = algo_name;
  345. info->cipher = image_get_cipher_algo(algo_name);
  346. if (!info->cipher) {
  347. printf("Can't get algo for cipher '%s'\n", image_name);
  348. goto out;
  349. }
  350. /* Read the key in the file */
  351. snprintf(filename, sizeof(filename), "%s/%s%s",
  352. info->keydir, info->keyname, ".bin");
  353. info->key = malloc(info->cipher->key_len);
  354. if (!info->key) {
  355. printf("Can't allocate memory for key\n");
  356. ret = -1;
  357. goto out;
  358. }
  359. ret = fit_image_read_data(filename, (unsigned char *)info->key,
  360. info->cipher->key_len);
  361. if (ret < 0)
  362. goto out;
  363. info->iv = malloc(info->cipher->iv_len);
  364. if (!info->iv) {
  365. printf("Can't allocate memory for iv\n");
  366. ret = -1;
  367. goto out;
  368. }
  369. if (info->ivname) {
  370. /* Read the IV in the file */
  371. snprintf(filename, sizeof(filename), "%s/%s%s",
  372. info->keydir, info->ivname, ".bin");
  373. ret = fit_image_read_data(filename, (unsigned char *)info->iv,
  374. info->cipher->iv_len);
  375. } else {
  376. /* Generate an ramdom IV */
  377. ret = get_random_data((void *)info->iv, info->cipher->iv_len);
  378. }
  379. out:
  380. return ret;
  381. }
  382. int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
  383. const void *data, size_t size,
  384. unsigned char *data_ciphered, int data_ciphered_len)
  385. {
  386. int ret = -1;
  387. /* Replace data with ciphered data */
  388. ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
  389. data_ciphered, data_ciphered_len);
  390. if (ret == -FDT_ERR_NOSPACE) {
  391. ret = -ENOSPC;
  392. goto out;
  393. }
  394. if (ret) {
  395. printf("Can't replace data with ciphered data (err = %d)\n", ret);
  396. goto out;
  397. }
  398. /* add non ciphered data size */
  399. ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
  400. if (ret == -FDT_ERR_NOSPACE) {
  401. ret = -ENOSPC;
  402. goto out;
  403. }
  404. if (ret) {
  405. printf("Can't add unciphered data size (err = %d)\n", ret);
  406. goto out;
  407. }
  408. out:
  409. return ret;
  410. }
  411. static int
  412. fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
  413. const char *image_name, int image_noffset,
  414. int node_noffset, const void *data, size_t size,
  415. const char *cmdname)
  416. {
  417. struct image_cipher_info info;
  418. unsigned char *data_ciphered = NULL;
  419. int data_ciphered_len;
  420. int ret;
  421. memset(&info, 0, sizeof(info));
  422. ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
  423. image_noffset, node_noffset);
  424. if (ret)
  425. goto out;
  426. ret = info.cipher->encrypt(&info, data, size,
  427. &data_ciphered, &data_ciphered_len);
  428. if (ret)
  429. goto out;
  430. /*
  431. * Write the public key into the supplied FDT file; this might fail
  432. * several times, since we try signing with successively increasing
  433. * size values
  434. * And, if needed, write the iv in the FIT file
  435. */
  436. if (keydest) {
  437. ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
  438. if (ret) {
  439. printf("Failed to add verification data for cipher '%s' in image '%s'\n",
  440. info.keyname, image_name);
  441. goto out;
  442. }
  443. }
  444. ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
  445. data, size,
  446. data_ciphered, data_ciphered_len);
  447. out:
  448. free(data_ciphered);
  449. free((void *)info.key);
  450. free((void *)info.iv);
  451. return ret;
  452. }
  453. int fit_image_cipher_data(const char *keydir, void *keydest,
  454. void *fit, int image_noffset, const char *comment,
  455. int require_keys, const char *engine_id,
  456. const char *cmdname)
  457. {
  458. const char *image_name;
  459. const void *data;
  460. size_t size;
  461. int cipher_node_offset, len;
  462. /* Get image name */
  463. image_name = fit_get_name(fit, image_noffset, NULL);
  464. if (!image_name) {
  465. printf("Can't get image name\n");
  466. return -1;
  467. }
  468. /* Get image data and data length */
  469. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  470. printf("Can't get image data/size\n");
  471. return -1;
  472. }
  473. /*
  474. * Don't cipher ciphered data.
  475. *
  476. * If the data-size-unciphered property is present the data for this
  477. * image is already encrypted. This is important as 'mkimage -F' can be
  478. * run multiple times on a FIT image.
  479. */
  480. if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
  481. return 0;
  482. if (len != -FDT_ERR_NOTFOUND) {
  483. printf("Failure testing for data-size-unciphered\n");
  484. return -1;
  485. }
  486. /* Process cipher node if present */
  487. cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
  488. FIT_CIPHER_NODENAME);
  489. if (cipher_node_offset == -FDT_ERR_NOTFOUND)
  490. return 0;
  491. if (cipher_node_offset < 0) {
  492. printf("Failure getting cipher node\n");
  493. return -1;
  494. }
  495. if (!IMAGE_ENABLE_ENCRYPT || !keydir)
  496. return 0;
  497. return fit_image_process_cipher(keydir, keydest, fit, image_name,
  498. image_noffset, cipher_node_offset, data, size, cmdname);
  499. }
  500. /**
  501. * fit_image_add_verification_data() - calculate/set verig. data for image node
  502. *
  503. * This adds hash and signature values for an component image node.
  504. *
  505. * All existing hash subnodes are checked, if algorithm property is set to
  506. * one of the supported hash algorithms, hash value is computed and
  507. * corresponding hash node property is set, for example:
  508. *
  509. * Input component image node structure:
  510. *
  511. * o image-1 (at image_noffset)
  512. * | - data = [binary data]
  513. * o hash-1
  514. * |- algo = "sha1"
  515. *
  516. * Output component image node structure:
  517. *
  518. * o image-1 (at image_noffset)
  519. * | - data = [binary data]
  520. * o hash-1
  521. * |- algo = "sha1"
  522. * |- value = sha1(data)
  523. *
  524. * For signature details, please see doc/uImage.FIT/signature.txt
  525. *
  526. * @keydir Directory containing *.key and *.crt files (or NULL)
  527. * @keydest FDT Blob to write public keys into (NULL if none)
  528. * @fit: Pointer to the FIT format image header
  529. * @image_noffset: Requested component image node
  530. * @comment: Comment to add to signature nodes
  531. * @require_keys: Mark all keys as 'required'
  532. * @engine_id: Engine to use for signing
  533. * @return: 0 on success, <0 on failure
  534. */
  535. int fit_image_add_verification_data(const char *keydir, const char *keyfile,
  536. void *keydest, void *fit, int image_noffset,
  537. const char *comment, int require_keys, const char *engine_id,
  538. const char *cmdname)
  539. {
  540. const char *image_name;
  541. const void *data;
  542. size_t size;
  543. int noffset;
  544. /* Get image data and data length */
  545. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  546. printf("Can't get image data/size\n");
  547. return -1;
  548. }
  549. image_name = fit_get_name(fit, image_noffset, NULL);
  550. /* Process all hash subnodes of the component image node */
  551. for (noffset = fdt_first_subnode(fit, image_noffset);
  552. noffset >= 0;
  553. noffset = fdt_next_subnode(fit, noffset)) {
  554. const char *node_name;
  555. int ret = 0;
  556. /*
  557. * Check subnode name, must be equal to "hash" or "signature".
  558. * Multiple hash nodes require unique unit node
  559. * names, e.g. hash-1, hash-2, signature-1, etc.
  560. */
  561. node_name = fit_get_name(fit, noffset, NULL);
  562. if (!strncmp(node_name, FIT_HASH_NODENAME,
  563. strlen(FIT_HASH_NODENAME))) {
  564. ret = fit_image_process_hash(fit, image_name, noffset,
  565. data, size);
  566. } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
  567. !strncmp(node_name, FIT_SIG_NODENAME,
  568. strlen(FIT_SIG_NODENAME))) {
  569. ret = fit_image_process_sig(keydir, keyfile, keydest,
  570. fit, image_name, noffset, data, size,
  571. comment, require_keys, engine_id, cmdname);
  572. }
  573. if (ret)
  574. return ret;
  575. }
  576. return 0;
  577. }
  578. struct strlist {
  579. int count;
  580. char **strings;
  581. };
  582. static void strlist_init(struct strlist *list)
  583. {
  584. memset(list, '\0', sizeof(*list));
  585. }
  586. static void strlist_free(struct strlist *list)
  587. {
  588. int i;
  589. for (i = 0; i < list->count; i++)
  590. free(list->strings[i]);
  591. free(list->strings);
  592. }
  593. static int strlist_add(struct strlist *list, const char *str)
  594. {
  595. char *dup;
  596. dup = strdup(str);
  597. list->strings = realloc(list->strings,
  598. (list->count + 1) * sizeof(char *));
  599. if (!list || !str)
  600. return -1;
  601. list->strings[list->count++] = dup;
  602. return 0;
  603. }
  604. static const char *fit_config_get_image_list(void *fit, int noffset,
  605. int *lenp, int *allow_missingp)
  606. {
  607. static const char default_list[] = FIT_KERNEL_PROP "\0"
  608. FIT_FDT_PROP;
  609. const char *prop;
  610. /* If there is an "image" property, use that */
  611. prop = fdt_getprop(fit, noffset, "sign-images", lenp);
  612. if (prop) {
  613. *allow_missingp = 0;
  614. return *lenp ? prop : NULL;
  615. }
  616. /* Default image list */
  617. *allow_missingp = 1;
  618. *lenp = sizeof(default_list);
  619. return default_list;
  620. }
  621. static int fit_config_add_hash(void *fit, const char *conf_name, const char *sig_name,
  622. struct strlist *node_inc, const char *iname, int image_noffset)
  623. {
  624. char name[200], path[200];
  625. int noffset;
  626. int hash_count;
  627. int ret;
  628. ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
  629. if (ret < 0)
  630. goto err_path;
  631. if (strlist_add(node_inc, path))
  632. goto err_mem;
  633. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
  634. conf_name);
  635. /* Add all this image's hashes */
  636. hash_count = 0;
  637. for (noffset = fdt_first_subnode(fit, image_noffset);
  638. noffset >= 0;
  639. noffset = fdt_next_subnode(fit, noffset)) {
  640. const char *name = fit_get_name(fit, noffset, NULL);
  641. if (strncmp(name, FIT_HASH_NODENAME,
  642. strlen(FIT_HASH_NODENAME)))
  643. continue;
  644. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  645. if (ret < 0)
  646. goto err_path;
  647. if (strlist_add(node_inc, path))
  648. goto err_mem;
  649. hash_count++;
  650. }
  651. if (!hash_count) {
  652. printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
  653. conf_name, sig_name, iname);
  654. return -ENOMSG;
  655. }
  656. /* Add this image's cipher node if present */
  657. noffset = fdt_subnode_offset(fit, image_noffset,
  658. FIT_CIPHER_NODENAME);
  659. if (noffset != -FDT_ERR_NOTFOUND) {
  660. if (noffset < 0) {
  661. printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
  662. conf_name, sig_name, iname,
  663. fdt_strerror(noffset));
  664. return -EIO;
  665. }
  666. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  667. if (ret < 0)
  668. goto err_path;
  669. if (strlist_add(node_inc, path))
  670. goto err_mem;
  671. }
  672. return 0;
  673. err_mem:
  674. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  675. sig_name);
  676. return -ENOMEM;
  677. err_path:
  678. printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
  679. iname, conf_name, sig_name, fdt_strerror(ret));
  680. return -ENOENT;
  681. }
  682. static int fit_config_get_hash_list(void *fit, int conf_noffset,
  683. int sig_offset, struct strlist *node_inc)
  684. {
  685. int allow_missing;
  686. const char *prop, *iname, *end;
  687. const char *conf_name, *sig_name;
  688. char name[200];
  689. int image_count;
  690. int ret, len;
  691. conf_name = fit_get_name(fit, conf_noffset, NULL);
  692. sig_name = fit_get_name(fit, sig_offset, NULL);
  693. /*
  694. * Build a list of nodes we need to hash. We always need the root
  695. * node and the configuration.
  696. */
  697. strlist_init(node_inc);
  698. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
  699. if (strlist_add(node_inc, "/") ||
  700. strlist_add(node_inc, name))
  701. goto err_mem;
  702. /* Get a list of images that we intend to sign */
  703. prop = fit_config_get_image_list(fit, sig_offset, &len,
  704. &allow_missing);
  705. if (!prop)
  706. return 0;
  707. /* Locate the images */
  708. end = prop + len;
  709. image_count = 0;
  710. for (iname = prop; iname < end; iname += strlen(iname) + 1) {
  711. int image_noffset;
  712. int index, max_index;
  713. max_index = fdt_stringlist_count(fit, conf_noffset, iname);
  714. for (index = 0; index < max_index; index++) {
  715. image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
  716. iname, index);
  717. if (image_noffset < 0) {
  718. printf("Failed to find image '%s' in configuration '%s/%s'\n",
  719. iname, conf_name, sig_name);
  720. if (allow_missing)
  721. continue;
  722. return -ENOENT;
  723. }
  724. ret = fit_config_add_hash(fit, conf_name,
  725. sig_name, node_inc,
  726. iname, image_noffset);
  727. if (ret < 0)
  728. return ret;
  729. image_count++;
  730. }
  731. }
  732. if (!image_count) {
  733. printf("Failed to find any images for configuration '%s/%s'\n",
  734. conf_name, sig_name);
  735. return -ENOMSG;
  736. }
  737. return 0;
  738. err_mem:
  739. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  740. sig_name);
  741. return -ENOMEM;
  742. }
  743. static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
  744. struct image_region **regionp, int *region_countp,
  745. char **region_propp, int *region_proplen)
  746. {
  747. char * const exc_prop[] = {"data"};
  748. struct strlist node_inc;
  749. struct image_region *region;
  750. struct fdt_region fdt_regions[100];
  751. const char *conf_name, *sig_name;
  752. char path[200];
  753. int count, i;
  754. char *region_prop;
  755. int ret, len;
  756. conf_name = fit_get_name(fit, conf_noffset, NULL);
  757. sig_name = fit_get_name(fit, noffset, NULL);
  758. debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
  759. /* Get a list of nodes we want to hash */
  760. ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
  761. if (ret)
  762. return ret;
  763. /* Get a list of regions to hash */
  764. count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
  765. exc_prop, ARRAY_SIZE(exc_prop),
  766. fdt_regions, ARRAY_SIZE(fdt_regions),
  767. path, sizeof(path), 1);
  768. if (count < 0) {
  769. printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
  770. sig_name, fdt_strerror(ret));
  771. return -EIO;
  772. }
  773. if (count == 0) {
  774. printf("No data to hash for configuration '%s/%s': %s\n",
  775. conf_name, sig_name, fdt_strerror(ret));
  776. return -EINVAL;
  777. }
  778. /* Build our list of data blocks */
  779. region = fit_region_make_list(fit, fdt_regions, count, NULL);
  780. if (!region) {
  781. printf("Out of memory hashing configuration '%s/%s'\n",
  782. conf_name, sig_name);
  783. return -ENOMEM;
  784. }
  785. /* Create a list of all hashed properties */
  786. debug("Hash nodes:\n");
  787. for (i = len = 0; i < node_inc.count; i++) {
  788. debug(" %s\n", node_inc.strings[i]);
  789. len += strlen(node_inc.strings[i]) + 1;
  790. }
  791. region_prop = malloc(len);
  792. if (!region_prop) {
  793. printf("Out of memory setting up regions for configuration '%s/%s'\n",
  794. conf_name, sig_name);
  795. return -ENOMEM;
  796. }
  797. for (i = len = 0; i < node_inc.count;
  798. len += strlen(node_inc.strings[i]) + 1, i++)
  799. strcpy(region_prop + len, node_inc.strings[i]);
  800. strlist_free(&node_inc);
  801. *region_countp = count;
  802. *regionp = region;
  803. *region_propp = region_prop;
  804. *region_proplen = len;
  805. return 0;
  806. }
  807. static int fit_config_process_sig(const char *keydir, const char *keyfile,
  808. void *keydest, void *fit, const char *conf_name,
  809. int conf_noffset, int noffset, const char *comment,
  810. int require_keys, const char *engine_id, const char *cmdname)
  811. {
  812. struct image_sign_info info;
  813. const char *node_name;
  814. struct image_region *region;
  815. char *region_prop;
  816. int region_proplen;
  817. int region_count;
  818. uint8_t *value;
  819. uint value_len;
  820. int ret;
  821. node_name = fit_get_name(fit, noffset, NULL);
  822. if (fit_config_get_data(fit, conf_noffset, noffset, &region,
  823. &region_count, &region_prop, &region_proplen))
  824. return -1;
  825. if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
  826. require_keys ? "conf" : NULL, engine_id))
  827. return -1;
  828. ret = info.crypto->sign(&info, region, region_count, &value,
  829. &value_len);
  830. free(region);
  831. if (ret) {
  832. printf("Failed to sign '%s' signature node in '%s' conf node\n",
  833. node_name, conf_name);
  834. /* We allow keys to be missing */
  835. if (ret == -ENOENT)
  836. return 0;
  837. return -1;
  838. }
  839. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  840. region_prop, region_proplen, cmdname);
  841. if (ret) {
  842. if (ret == -FDT_ERR_NOSPACE)
  843. return -ENOSPC;
  844. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  845. node_name, conf_name, fdt_strerror(ret));
  846. return -1;
  847. }
  848. free(value);
  849. free(region_prop);
  850. /* Get keyname again, as FDT has changed and invalidated our pointer */
  851. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  852. /* Write the public key into the supplied FDT file */
  853. if (keydest) {
  854. ret = info.crypto->add_verify_data(&info, keydest);
  855. if (ret) {
  856. printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
  857. node_name, conf_name);
  858. }
  859. return ret;
  860. }
  861. return 0;
  862. }
  863. static int fit_config_add_verification_data(const char *keydir,
  864. const char *keyfile, void *keydest, void *fit, int conf_noffset,
  865. const char *comment, int require_keys, const char *engine_id,
  866. const char *cmdname)
  867. {
  868. const char *conf_name;
  869. int noffset;
  870. conf_name = fit_get_name(fit, conf_noffset, NULL);
  871. /* Process all hash subnodes of the configuration node */
  872. for (noffset = fdt_first_subnode(fit, conf_noffset);
  873. noffset >= 0;
  874. noffset = fdt_next_subnode(fit, noffset)) {
  875. const char *node_name;
  876. int ret = 0;
  877. node_name = fit_get_name(fit, noffset, NULL);
  878. if (!strncmp(node_name, FIT_SIG_NODENAME,
  879. strlen(FIT_SIG_NODENAME))) {
  880. ret = fit_config_process_sig(keydir, keyfile, keydest,
  881. fit, conf_name, conf_noffset, noffset, comment,
  882. require_keys, engine_id, cmdname);
  883. }
  884. if (ret)
  885. return ret;
  886. }
  887. return 0;
  888. }
  889. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  890. const char *comment, int require_keys,
  891. const char *engine_id, const char *cmdname)
  892. {
  893. int images_noffset;
  894. int noffset;
  895. int ret;
  896. /* Find images parent node offset */
  897. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  898. if (images_noffset < 0) {
  899. printf("Can't find images parent node '%s' (%s)\n",
  900. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  901. return images_noffset;
  902. }
  903. /* Process its subnodes, print out component images details */
  904. for (noffset = fdt_first_subnode(fit, images_noffset);
  905. noffset >= 0;
  906. noffset = fdt_next_subnode(fit, noffset)) {
  907. /*
  908. * Direct child node of the images parent node,
  909. * i.e. component image node.
  910. */
  911. ret = fit_image_cipher_data(keydir, keydest,
  912. fit, noffset, comment,
  913. require_keys, engine_id,
  914. cmdname);
  915. if (ret)
  916. return ret;
  917. }
  918. return 0;
  919. }
  920. int fit_add_verification_data(const char *keydir, const char *keyfile,
  921. void *keydest, void *fit, const char *comment,
  922. int require_keys, const char *engine_id,
  923. const char *cmdname)
  924. {
  925. int images_noffset, confs_noffset;
  926. int noffset;
  927. int ret;
  928. /* Find images parent node offset */
  929. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  930. if (images_noffset < 0) {
  931. printf("Can't find images parent node '%s' (%s)\n",
  932. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  933. return images_noffset;
  934. }
  935. /* Process its subnodes, print out component images details */
  936. for (noffset = fdt_first_subnode(fit, images_noffset);
  937. noffset >= 0;
  938. noffset = fdt_next_subnode(fit, noffset)) {
  939. /*
  940. * Direct child node of the images parent node,
  941. * i.e. component image node.
  942. */
  943. ret = fit_image_add_verification_data(keydir, keyfile, keydest,
  944. fit, noffset, comment, require_keys, engine_id,
  945. cmdname);
  946. if (ret)
  947. return ret;
  948. }
  949. /* If there are no keys, we can't sign configurations */
  950. if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
  951. return 0;
  952. /* Find configurations parent node offset */
  953. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  954. if (confs_noffset < 0) {
  955. printf("Can't find images parent node '%s' (%s)\n",
  956. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  957. return -ENOENT;
  958. }
  959. /* Process its subnodes, print out component images details */
  960. for (noffset = fdt_first_subnode(fit, confs_noffset);
  961. noffset >= 0;
  962. noffset = fdt_next_subnode(fit, noffset)) {
  963. ret = fit_config_add_verification_data(keydir, keyfile, keydest,
  964. fit, noffset, comment,
  965. require_keys,
  966. engine_id, cmdname);
  967. if (ret)
  968. return ret;
  969. }
  970. return 0;
  971. }
  972. #ifdef CONFIG_FIT_SIGNATURE
  973. int fit_check_sign(const void *fit, const void *key,
  974. const char *fit_uname_config)
  975. {
  976. int cfg_noffset;
  977. int ret;
  978. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  979. if (!cfg_noffset)
  980. return -1;
  981. printf("Verifying Hash Integrity for node '%s'... ",
  982. fdt_get_name(fit, cfg_noffset, NULL));
  983. ret = fit_config_verify(fit, cfg_noffset);
  984. if (ret)
  985. return ret;
  986. printf("Verified OK, loading images\n");
  987. ret = bootm_host_load_images(fit, cfg_noffset);
  988. return ret;
  989. }
  990. #endif