image-host.c 29 KB

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