image-host.c 28 KB

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