image-host.c 29 KB

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