configs: meson64: Remove the duplicated defined ENV_SETTING
[platform/kernel/u-boot.git] / tools / image-host.c
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
11 #include "mkimage.h"
12 #include <bootm.h>
13 #include <fdt_region.h>
14 #include <image.h>
15 #include <version.h>
16
17 #include <openssl/pem.h>
18 #include <openssl/evp.h>
19
20 /**
21  * fit_set_hash_value - set hash value in requested has node
22  * @fit: pointer to the FIT format image header
23  * @noffset: hash node offset
24  * @value: hash value to be set
25  * @value_len: hash value length
26  *
27  * fit_set_hash_value() attempts to set hash value in a node at offset
28  * given and returns operation status to the caller.
29  *
30  * returns
31  *     0, on success
32  *     -1, on failure
33  */
34 static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
35                                 int value_len)
36 {
37         int ret;
38
39         ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
40         if (ret) {
41                 printf("Can't set hash '%s' property for '%s' node(%s)\n",
42                        FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
43                        fdt_strerror(ret));
44                 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
45         }
46
47         return 0;
48 }
49
50 /**
51  * fit_image_process_hash - Process a single subnode of the images/ node
52  *
53  * Check each subnode and process accordingly. For hash nodes we generate
54  * a hash of the supplied data and store it in the node.
55  *
56  * @fit:        pointer to the FIT format image header
57  * @image_name: name of image being processed (used to display errors)
58  * @noffset:    subnode offset
59  * @data:       data to process
60  * @size:       size of data in bytes
61  * Return: 0 if ok, -1 on error
62  */
63 static int fit_image_process_hash(void *fit, const char *image_name,
64                 int noffset, const void *data, size_t size)
65 {
66         uint8_t value[FIT_MAX_HASH_LEN];
67         const char *node_name;
68         int value_len;
69         const char *algo;
70         int ret;
71
72         node_name = fit_get_name(fit, noffset, NULL);
73
74         if (fit_image_hash_get_algo(fit, noffset, &algo)) {
75                 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
76                        node_name, image_name);
77                 return -ENOENT;
78         }
79
80         if (calculate_hash(data, size, algo, value, &value_len)) {
81                 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
82                        algo, node_name, image_name);
83                 return -EPROTONOSUPPORT;
84         }
85
86         ret = fit_set_hash_value(fit, noffset, value, value_len);
87         if (ret) {
88                 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
89                        node_name, image_name);
90                 return ret;
91         }
92
93         return 0;
94 }
95
96 /**
97  * fit_image_write_sig() - write the signature to a FIT
98  *
99  * This writes the signature and signer data to the FIT.
100  *
101  * @fit: pointer to the FIT format image header
102  * @noffset: hash node offset
103  * @value: signature value to be set
104  * @value_len: signature value length
105  * @comment: Text comment to write (NULL for none)
106  *
107  * returns
108  *     0, on success
109  *     -FDT_ERR_..., on failure
110  */
111 static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
112                 int value_len, const char *comment, const char *region_prop,
113                 int region_proplen, const char *cmdname, const char *algo_name)
114 {
115         int string_size;
116         int ret;
117
118         /*
119          * Get the current string size, before we update the FIT and add
120          * more
121          */
122         string_size = fdt_size_dt_strings(fit);
123
124         ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
125         if (!ret) {
126                 ret = fdt_setprop_string(fit, noffset, "signer-name",
127                                          "mkimage");
128         }
129         if (!ret) {
130                 ret = fdt_setprop_string(fit, noffset, "signer-version",
131                                   PLAIN_VERSION);
132         }
133         if (comment && !ret)
134                 ret = fdt_setprop_string(fit, noffset, "comment", comment);
135         if (!ret) {
136                 time_t timestamp = imagetool_get_source_date(cmdname,
137                                                              time(NULL));
138                 uint32_t t = cpu_to_uimage(timestamp);
139
140                 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
141                         sizeof(uint32_t));
142         }
143         if (region_prop && !ret) {
144                 uint32_t strdata[2];
145
146                 ret = fdt_setprop(fit, noffset, "hashed-nodes",
147                                    region_prop, region_proplen);
148                 /* This is a legacy offset, it is unused, and must remain 0. */
149                 strdata[0] = 0;
150                 strdata[1] = cpu_to_fdt32(string_size);
151                 if (!ret) {
152                         ret = fdt_setprop(fit, noffset, "hashed-strings",
153                                           strdata, sizeof(strdata));
154                 }
155         }
156         if (algo_name && !ret)
157                 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
158
159         return ret;
160 }
161
162 static int fit_image_setup_sig(struct image_sign_info *info,
163                 const char *keydir, const char *keyfile, void *fit,
164                 const char *image_name, int noffset, const char *require_keys,
165                 const char *engine_id, const char *algo_name)
166 {
167         const char *node_name;
168         const char *padding_name;
169
170         node_name = fit_get_name(fit, noffset, NULL);
171         if (!algo_name) {
172                 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
173                         printf("Can't get algo property for '%s' signature node in '%s' image node\n",
174                                node_name, image_name);
175                         return -1;
176                 }
177         }
178
179         padding_name = fdt_getprop(fit, noffset, "padding", NULL);
180
181         memset(info, '\0', sizeof(*info));
182         info->keydir = keydir;
183         info->keyfile = keyfile;
184         info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
185         info->fit = fit;
186         info->node_offset = noffset;
187         info->name = strdup(algo_name);
188         info->checksum = image_get_checksum_algo(algo_name);
189         info->crypto = image_get_crypto_algo(algo_name);
190         info->padding = image_get_padding_algo(padding_name);
191         info->require_keys = require_keys;
192         info->engine_id = engine_id;
193         if (!info->checksum || !info->crypto) {
194                 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
195                        algo_name, node_name, image_name);
196                 return -1;
197         }
198
199         return 0;
200 }
201
202 /**
203  * fit_image_process_sig- Process a single subnode of the images/ node
204  *
205  * Check each subnode and process accordingly. For signature nodes we
206  * generate a signed hash of the supplied data and store it in the node.
207  *
208  * @keydir:     Directory containing keys to use for signing
209  * @keydest:    Destination FDT blob to write public keys into (NULL if none)
210  * @fit:        pointer to the FIT format image header
211  * @image_name: name of image being processed (used to display errors)
212  * @noffset:    subnode offset
213  * @data:       data to process
214  * @size:       size of data in bytes
215  * @comment:    Comment to add to signature nodes
216  * @require_keys: Mark all keys as 'required'
217  * @engine_id:  Engine to use for signing
218  * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code
219  *      on failure
220  */
221 static int fit_image_process_sig(const char *keydir, const char *keyfile,
222                 void *keydest, void *fit, const char *image_name,
223                 int noffset, const void *data, size_t size,
224                 const char *comment, int require_keys, const char *engine_id,
225                 const char *cmdname, const char *algo_name)
226 {
227         struct image_sign_info info;
228         struct image_region region;
229         const char *node_name;
230         uint8_t *value;
231         uint value_len;
232         int ret;
233
234         if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
235                                 noffset, require_keys ? "image" : NULL,
236                                 engine_id, algo_name))
237                 return -1;
238
239         node_name = fit_get_name(fit, noffset, NULL);
240         region.data = data;
241         region.size = size;
242         ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
243         if (ret) {
244                 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
245                        node_name, image_name, ret);
246
247                 /* We allow keys to be missing */
248                 if (ret == -ENOENT)
249                         return 0;
250                 return -1;
251         }
252
253         ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
254                         NULL, 0, cmdname, algo_name);
255         if (ret) {
256                 if (ret == -FDT_ERR_NOSPACE)
257                         return -ENOSPC;
258                 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
259                        node_name, image_name, fdt_strerror(ret));
260                 return -1;
261         }
262         free(value);
263
264         /* Get keyname again, as FDT has changed and invalidated our pointer */
265         info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
266
267         /*
268          * Write the public key into the supplied FDT file; this might fail
269          * several times, since we try signing with successively increasing
270          * size values
271          */
272         if (keydest) {
273                 ret = info.crypto->add_verify_data(&info, keydest);
274                 if (ret < 0) {
275                         printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
276                                node_name, image_name);
277                         return ret;
278                 }
279                 /* Return the node that was written to */
280                 return ret;
281         }
282
283         return 0;
284 }
285
286 static int fit_image_read_data(char *filename, unsigned char *data,
287                                int expected_size)
288 {
289         struct stat sbuf;
290         int fd, ret = -1;
291         ssize_t n;
292
293         /* Open file */
294         fd = open(filename, O_RDONLY | O_BINARY);
295         if (fd < 0) {
296                 printf("Can't open file %s (err=%d => %s)\n",
297                        filename, errno, strerror(errno));
298                 return -1;
299         }
300
301         /* Compute file size */
302         if (fstat(fd, &sbuf) < 0) {
303                 printf("Can't fstat file %s (err=%d => %s)\n",
304                        filename, errno, strerror(errno));
305                 goto err;
306         }
307
308         /* Check file size */
309         if (sbuf.st_size != expected_size) {
310                 printf("File %s don't have the expected size (size=%lld, expected=%d)\n",
311                        filename, (long long)sbuf.st_size, expected_size);
312                 goto err;
313         }
314
315         /* Read data */
316         n = read(fd, data, sbuf.st_size);
317         if (n < 0) {
318                 printf("Can't read file %s (err=%d => %s)\n",
319                        filename, errno, strerror(errno));
320                 goto err;
321         }
322
323         /* Check that we have read all the file */
324         if (n != sbuf.st_size) {
325                 printf("Can't read all file %s (read %zd bytes, expected %lld)\n",
326                        filename, n, (long long)sbuf.st_size);
327                 goto err;
328         }
329
330         ret = 0;
331
332 err:
333         close(fd);
334         return ret;
335 }
336
337 static int get_random_data(void *data, int size)
338 {
339         unsigned char *tmp = data;
340         struct timespec date;
341         int i, ret;
342
343         if (!tmp) {
344                 printf("%s: pointer data is NULL\n", __func__);
345                 ret = -1;
346                 goto out;
347         }
348
349         ret = clock_gettime(CLOCK_MONOTONIC, &date);
350         if (ret) {
351                 printf("%s: clock_gettime has failed (%s)\n", __func__,
352                        strerror(errno));
353                 goto out;
354         }
355
356         srandom(date.tv_nsec);
357
358         for (i = 0; i < size; i++) {
359                 *tmp = random() & 0xff;
360                 tmp++;
361         }
362
363  out:
364         return ret;
365 }
366
367 static int fit_image_setup_cipher(struct image_cipher_info *info,
368                                   const char *keydir, void *fit,
369                                   const char *image_name, int image_noffset,
370                                   int noffset)
371 {
372         char *algo_name;
373         char filename[128];
374         int ret = -1;
375
376         if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
377                 printf("Can't get algo name for cipher in image '%s'\n",
378                        image_name);
379                 goto out;
380         }
381
382         info->keydir = keydir;
383
384         /* Read the key name */
385         info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
386         if (!info->keyname) {
387                 printf("Can't get key name for cipher in image '%s'\n",
388                        image_name);
389                 goto out;
390         }
391
392         /*
393          * Read the IV name
394          *
395          * If this property is not provided then mkimage will generate
396          * a random IV and store it in the FIT image
397          */
398         info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
399
400         info->fit = fit;
401         info->node_noffset = noffset;
402         info->name = algo_name;
403
404         info->cipher = image_get_cipher_algo(algo_name);
405         if (!info->cipher) {
406                 printf("Can't get algo for cipher '%s'\n", image_name);
407                 goto out;
408         }
409
410         /* Read the key in the file */
411         snprintf(filename, sizeof(filename), "%s/%s%s",
412                  info->keydir, info->keyname, ".bin");
413         info->key = malloc(info->cipher->key_len);
414         if (!info->key) {
415                 printf("Can't allocate memory for key\n");
416                 ret = -1;
417                 goto out;
418         }
419         ret = fit_image_read_data(filename, (unsigned char *)info->key,
420                                   info->cipher->key_len);
421         if (ret < 0)
422                 goto out;
423
424         info->iv = malloc(info->cipher->iv_len);
425         if (!info->iv) {
426                 printf("Can't allocate memory for iv\n");
427                 ret = -1;
428                 goto out;
429         }
430
431         if (info->ivname) {
432                 /* Read the IV in the file */
433                 snprintf(filename, sizeof(filename), "%s/%s%s",
434                          info->keydir, info->ivname, ".bin");
435                 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
436                                           info->cipher->iv_len);
437         } else {
438                 /* Generate an ramdom IV */
439                 ret = get_random_data((void *)info->iv, info->cipher->iv_len);
440         }
441
442  out:
443         return ret;
444 }
445
446 int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
447                            const void *data, size_t size,
448                            unsigned char *data_ciphered, int data_ciphered_len)
449 {
450         int ret = -1;
451
452         /* Replace data with ciphered data */
453         ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
454                           data_ciphered, data_ciphered_len);
455         if (ret == -FDT_ERR_NOSPACE) {
456                 ret = -ENOSPC;
457                 goto out;
458         }
459         if (ret) {
460                 printf("Can't replace data with ciphered data (err = %d)\n", ret);
461                 goto out;
462         }
463
464         /* add non ciphered data size */
465         ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
466         if (ret == -FDT_ERR_NOSPACE) {
467                 ret = -ENOSPC;
468                 goto out;
469         }
470         if (ret) {
471                 printf("Can't add unciphered data size (err = %d)\n", ret);
472                 goto out;
473         }
474
475  out:
476         return ret;
477 }
478
479 static int
480 fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
481                          const char *image_name, int image_noffset,
482                          int node_noffset, const void *data, size_t size,
483                          const char *cmdname)
484 {
485         struct image_cipher_info info;
486         unsigned char *data_ciphered = NULL;
487         int data_ciphered_len;
488         int ret;
489
490         memset(&info, 0, sizeof(info));
491
492         ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
493                                      image_noffset, node_noffset);
494         if (ret)
495                 goto out;
496
497         ret = info.cipher->encrypt(&info, data, size,
498                                     &data_ciphered, &data_ciphered_len);
499         if (ret)
500                 goto out;
501
502         /*
503          * Write the public key into the supplied FDT file; this might fail
504          * several times, since we try signing with successively increasing
505          * size values
506          * And, if needed, write the iv in the FIT file
507          */
508         if (keydest) {
509                 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
510                 if (ret) {
511                         printf("Failed to add verification data for cipher '%s' in image '%s'\n",
512                                info.keyname, image_name);
513                         goto out;
514                 }
515         }
516
517         ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
518                                      data, size,
519                                      data_ciphered, data_ciphered_len);
520
521  out:
522         free(data_ciphered);
523         free((void *)info.key);
524         free((void *)info.iv);
525         return ret;
526 }
527
528 int fit_image_cipher_data(const char *keydir, void *keydest,
529                           void *fit, int image_noffset, const char *comment,
530                           int require_keys, const char *engine_id,
531                           const char *cmdname)
532 {
533         const char *image_name;
534         const void *data;
535         size_t size;
536         int cipher_node_offset, len;
537
538         /* Get image name */
539         image_name = fit_get_name(fit, image_noffset, NULL);
540         if (!image_name) {
541                 printf("Can't get image name\n");
542                 return -1;
543         }
544
545         /* Get image data and data length */
546         if (fit_image_get_data(fit, image_noffset, &data, &size)) {
547                 printf("Can't get image data/size\n");
548                 return -1;
549         }
550
551         /*
552          * Don't cipher ciphered data.
553          *
554          * If the data-size-unciphered property is present the data for this
555          * image is already encrypted. This is important as 'mkimage -F' can be
556          * run multiple times on a FIT image.
557          */
558         if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
559                 return 0;
560         if (len != -FDT_ERR_NOTFOUND) {
561                 printf("Failure testing for data-size-unciphered\n");
562                 return -1;
563         }
564
565         /* Process cipher node if present */
566         cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
567                                                 FIT_CIPHER_NODENAME);
568         if (cipher_node_offset == -FDT_ERR_NOTFOUND)
569                 return 0;
570         if (cipher_node_offset < 0) {
571                 printf("Failure getting cipher node\n");
572                 return -1;
573         }
574         if (!IMAGE_ENABLE_ENCRYPT || !keydir)
575                 return 0;
576         return fit_image_process_cipher(keydir, keydest, fit, image_name,
577                 image_noffset, cipher_node_offset, data, size, cmdname);
578 }
579
580 /**
581  * fit_image_add_verification_data() - calculate/set verig. data for image node
582  *
583  * This adds hash and signature values for an component image node.
584  *
585  * All existing hash subnodes are checked, if algorithm property is set to
586  * one of the supported hash algorithms, hash value is computed and
587  * corresponding hash node property is set, for example:
588  *
589  * Input component image node structure:
590  *
591  * o image-1 (at image_noffset)
592  *   | - data = [binary data]
593  *   o hash-1
594  *     |- algo = "sha1"
595  *
596  * Output component image node structure:
597  *
598  * o image-1 (at image_noffset)
599  *   | - data = [binary data]
600  *   o hash-1
601  *     |- algo = "sha1"
602  *     |- value = sha1(data)
603  *
604  * For signature details, please see doc/uImage.FIT/signature.txt
605  *
606  * @keydir      Directory containing *.key and *.crt files (or NULL)
607  * @keydest     FDT Blob to write public keys into (NULL if none)
608  * @fit:        Pointer to the FIT format image header
609  * @image_noffset: Requested component image node
610  * @comment:    Comment to add to signature nodes
611  * @require_keys: Mark all keys as 'required'
612  * @engine_id:  Engine to use for signing
613  * @return: 0 on success, <0 on failure
614  */
615 int fit_image_add_verification_data(const char *keydir, const char *keyfile,
616                 void *keydest, void *fit, int image_noffset,
617                 const char *comment, int require_keys, const char *engine_id,
618                 const char *cmdname, const char* algo_name)
619 {
620         const char *image_name;
621         const void *data;
622         size_t size;
623         int noffset;
624
625         /* Get image data and data length */
626         if (fit_image_get_data(fit, image_noffset, &data, &size)) {
627                 printf("Can't get image data/size\n");
628                 return -1;
629         }
630
631         image_name = fit_get_name(fit, image_noffset, NULL);
632
633         /* Process all hash subnodes of the component image node */
634         for (noffset = fdt_first_subnode(fit, image_noffset);
635              noffset >= 0;
636              noffset = fdt_next_subnode(fit, noffset)) {
637                 const char *node_name;
638                 int ret = 0;
639
640                 /*
641                  * Check subnode name, must be equal to "hash" or "signature".
642                  * Multiple hash nodes require unique unit node
643                  * names, e.g. hash-1, hash-2, signature-1, etc.
644                  */
645                 node_name = fit_get_name(fit, noffset, NULL);
646                 if (!strncmp(node_name, FIT_HASH_NODENAME,
647                              strlen(FIT_HASH_NODENAME))) {
648                         ret = fit_image_process_hash(fit, image_name, noffset,
649                                                 data, size);
650                 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
651                            !strncmp(node_name, FIT_SIG_NODENAME,
652                                 strlen(FIT_SIG_NODENAME))) {
653                         ret = fit_image_process_sig(keydir, keyfile, keydest,
654                                 fit, image_name, noffset, data, size,
655                                 comment, require_keys, engine_id, cmdname,
656                                 algo_name);
657                 }
658                 if (ret < 0)
659                         return ret;
660         }
661
662         return 0;
663 }
664
665 struct strlist {
666         int count;
667         char **strings;
668 };
669
670 static void strlist_init(struct strlist *list)
671 {
672         memset(list, '\0', sizeof(*list));
673 }
674
675 static void strlist_free(struct strlist *list)
676 {
677         int i;
678
679         for (i = 0; i < list->count; i++)
680                 free(list->strings[i]);
681         free(list->strings);
682 }
683
684 static int strlist_add(struct strlist *list, const char *str)
685 {
686         char *dup;
687
688         dup = strdup(str);
689         list->strings = realloc(list->strings,
690                                 (list->count + 1) * sizeof(char *));
691         if (!list || !str)
692                 return -1;
693         list->strings[list->count++] = dup;
694
695         return 0;
696 }
697
698 static const char *fit_config_get_image_list(const void *fit, int noffset,
699                                              int *lenp, int *allow_missingp)
700 {
701         static const char default_list[] = FIT_KERNEL_PROP "\0"
702                         FIT_FDT_PROP;
703         const char *prop;
704
705         /* If there is an "sign-image" property, use that */
706         prop = fdt_getprop(fit, noffset, "sign-images", lenp);
707         if (prop) {
708                 *allow_missingp = 0;
709                 return *lenp ? prop : NULL;
710         }
711
712         /* Default image list */
713         *allow_missingp = 1;
714         *lenp = sizeof(default_list);
715
716         return default_list;
717 }
718
719 /**
720  * fit_config_add_hash() - Add a list of nodes to hash for an image
721  *
722  * This adds a list of paths to image nodes (as referred to by a particular
723  * offset) that need to be hashed, to protect a configuration
724  *
725  * @fit:        Pointer to the FIT format image header
726  * @image_noffset: Offset of image to process (e.g. /images/kernel-1)
727  * @node_inc:   List of nodes to add to
728  * @conf_name   Configuration-node name, child of /configurations node (only
729  *      used for error messages)
730  * @sig_name    Signature-node name (only used for error messages)
731  * @iname:      Name of image being processed (e.g. "kernel-1" (only used
732  *      for error messages)
733  */
734 static int fit_config_add_hash(const void *fit, int image_noffset,
735                                struct strlist *node_inc, const char *conf_name,
736                                const char *sig_name, const char *iname)
737 {
738         char path[200];
739         int noffset;
740         int hash_count;
741         int ret;
742
743         ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
744         if (ret < 0)
745                 goto err_path;
746         if (strlist_add(node_inc, path))
747                 goto err_mem;
748
749         /* Add all this image's hashes */
750         hash_count = 0;
751         for (noffset = fdt_first_subnode(fit, image_noffset);
752              noffset >= 0;
753              noffset = fdt_next_subnode(fit, noffset)) {
754                 const char *name = fit_get_name(fit, noffset, NULL);
755
756                 if (strncmp(name, FIT_HASH_NODENAME,
757                             strlen(FIT_HASH_NODENAME)))
758                         continue;
759                 ret = fdt_get_path(fit, noffset, path, sizeof(path));
760                 if (ret < 0)
761                         goto err_path;
762                 if (strlist_add(node_inc, path))
763                         goto err_mem;
764                 hash_count++;
765         }
766
767         if (!hash_count) {
768                 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
769                        conf_name, sig_name, iname);
770                 return -ENOMSG;
771         }
772
773         /* Add this image's cipher node if present */
774         noffset = fdt_subnode_offset(fit, image_noffset,
775                                      FIT_CIPHER_NODENAME);
776         if (noffset != -FDT_ERR_NOTFOUND) {
777                 if (noffset < 0) {
778                         printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
779                                conf_name, sig_name, iname,
780                                fdt_strerror(noffset));
781                         return -EIO;
782                 }
783                 ret = fdt_get_path(fit, noffset, path, sizeof(path));
784                 if (ret < 0)
785                         goto err_path;
786                 if (strlist_add(node_inc, path))
787                         goto err_mem;
788         }
789
790         return 0;
791
792 err_mem:
793         printf("Out of memory processing configuration '%s/%s'\n", conf_name,
794                sig_name);
795         return -ENOMEM;
796
797 err_path:
798         printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
799                iname, conf_name, sig_name, fdt_strerror(ret));
800         return -ENOENT;
801 }
802
803 /**
804  * fit_config_get_hash_list() - Get the regions to sign
805  *
806  * This calculates a list of nodes to hash for this particular configuration,
807  * returning it as a string list (struct strlist, not a devicetree string list)
808  *
809  * @fit:        Pointer to the FIT format image header
810  * @conf_noffset: Offset of configuration node to sign (child of
811  *      /configurations node)
812  * @sig_offset: Offset of signature node containing info about how to sign it
813  *      (child of 'signatures' node)
814  * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot
815  *      be found, -ENOMSG if ther were no images in the configuration
816  */
817 static int fit_config_get_hash_list(const void *fit, int conf_noffset,
818                                     int sig_offset, struct strlist *node_inc)
819 {
820         int allow_missing;
821         const char *prop, *iname, *end;
822         const char *conf_name, *sig_name;
823         char name[200];
824         int image_count;
825         int ret, len;
826
827         conf_name = fit_get_name(fit, conf_noffset, NULL);
828         sig_name = fit_get_name(fit, sig_offset, NULL);
829
830         /*
831          * Build a list of nodes we need to hash. We always need the root
832          * node and the configuration.
833          */
834         strlist_init(node_inc);
835         snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
836         if (strlist_add(node_inc, "/") ||
837             strlist_add(node_inc, name))
838                 goto err_mem;
839
840         /* Get a list of images that we intend to sign */
841         prop = fit_config_get_image_list(fit, sig_offset, &len,
842                                         &allow_missing);
843         if (!prop)
844                 return 0;
845
846         /* Locate the images */
847         end = prop + len;
848         image_count = 0;
849         for (iname = prop; iname < end; iname += strlen(iname) + 1) {
850                 int image_noffset;
851                 int index, max_index;
852
853                 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
854
855                 for (index = 0; index < max_index; index++) {
856                         image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
857                                                                      iname, index);
858
859                         if (image_noffset < 0) {
860                                 printf("Failed to find image '%s' in  configuration '%s/%s'\n",
861                                        iname, conf_name, sig_name);
862                                 if (allow_missing)
863                                         continue;
864
865                                 return -ENOENT;
866                         }
867
868                         ret = fit_config_add_hash(fit, image_noffset, node_inc,
869                                                   conf_name, sig_name, iname);
870                         if (ret < 0)
871                                 return ret;
872
873                         image_count++;
874                 }
875         }
876
877         if (!image_count) {
878                 printf("Failed to find any images for configuration '%s/%s'\n",
879                        conf_name, sig_name);
880                 return -ENOMSG;
881         }
882
883         return 0;
884
885 err_mem:
886         printf("Out of memory processing configuration '%s/%s'\n", conf_name,
887                sig_name);
888         return -ENOMEM;
889 }
890
891 /**
892  * fit_config_get_regions() - Get the regions to sign
893  *
894  * This calculates a list of node to hash for this particular configuration,
895  * then finds which regions of the devicetree they correspond to.
896  *
897  * @fit:        Pointer to the FIT format image header
898  * @conf_noffset: Offset of configuration node to sign (child of
899  *      /configurations node)
900  * @sig_offset: Offset of signature node containing info about how to sign it
901  *      (child of 'signatures' node)
902  * @regionp: Returns list of regions that need to be hashed (allocated; must be
903  *      freed by the caller)
904  * @region_count: Returns number of regions
905  * @region_propp: Returns string-list property containing the list of nodes
906  *      that correspond to the regions. Each entry is a full path to the node.
907  *      This is in devicetree format, i.e. a \0 between each string. This is
908  *      allocated and must be freed by the caller.
909  * @region_proplen: Returns length of *@@region_propp in bytes
910  * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could
911  * not be found, -EINVAL if no registers were found to hash
912  */
913 static int fit_config_get_regions(const void *fit, int conf_noffset,
914                                   int sig_offset, struct image_region **regionp,
915                                   int *region_countp, char **region_propp,
916                                   int *region_proplen)
917 {
918         char * const exc_prop[] = {
919                 FIT_DATA_PROP,
920                 FIT_DATA_SIZE_PROP,
921                 FIT_DATA_POSITION_PROP,
922                 FIT_DATA_OFFSET_PROP,
923         };
924         struct strlist node_inc;
925         struct image_region *region;
926         struct fdt_region fdt_regions[100];
927         const char *conf_name, *sig_name;
928         char path[200];
929         int count, i;
930         char *region_prop;
931         int ret, len;
932
933         conf_name = fit_get_name(fit, conf_noffset, NULL);
934         sig_name = fit_get_name(fit, sig_offset, NULL);
935         debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
936
937         /* Get a list of nodes we want to hash */
938         ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
939                                        &node_inc);
940         if (ret)
941                 return ret;
942
943         /* Get a list of regions to hash */
944         count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
945                         exc_prop, ARRAY_SIZE(exc_prop),
946                         fdt_regions, ARRAY_SIZE(fdt_regions),
947                         path, sizeof(path), 1);
948         if (count < 0) {
949                 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
950                        sig_name, fdt_strerror(ret));
951                 return -EIO;
952         }
953         if (count == 0) {
954                 printf("No data to hash for configuration '%s/%s': %s\n",
955                        conf_name, sig_name, fdt_strerror(ret));
956                 return -EINVAL;
957         }
958
959         /* Build our list of data blocks */
960         region = fit_region_make_list(fit, fdt_regions, count, NULL);
961         if (!region) {
962                 printf("Out of memory hashing configuration '%s/%s'\n",
963                        conf_name, sig_name);
964                 return -ENOMEM;
965         }
966
967         /* Create a list of all hashed properties */
968         debug("Hash nodes:\n");
969         for (i = len = 0; i < node_inc.count; i++) {
970                 debug("   %s\n", node_inc.strings[i]);
971                 len += strlen(node_inc.strings[i]) + 1;
972         }
973         region_prop = malloc(len);
974         if (!region_prop) {
975                 printf("Out of memory setting up regions for configuration '%s/%s'\n",
976                        conf_name, sig_name);
977                 return -ENOMEM;
978         }
979         for (i = len = 0; i < node_inc.count;
980              len += strlen(node_inc.strings[i]) + 1, i++)
981                 strcpy(region_prop + len, node_inc.strings[i]);
982         strlist_free(&node_inc);
983
984         *region_countp = count;
985         *regionp = region;
986         *region_propp = region_prop;
987         *region_proplen = len;
988
989         return 0;
990 }
991
992 /**
993  * fit_config_process_sig - Process a single subnode of the configurations/ node
994  *
995  * Generate a signed hash of the supplied data and store it in the node.
996  *
997  * @keydir:     Directory containing keys to use for signing
998  * @keydest:    Destination FDT blob to write public keys into (NULL if none)
999  * @fit:        pointer to the FIT format image header
1000  * @conf_name   name of config being processed (used to display errors)
1001  * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1'
1002  * @noffset:    subnode offset, e.g. '/configurations/conf-1/sig-1'
1003  * @comment:    Comment to add to signature nodes
1004  * @require_keys: Mark all keys as 'required'
1005  * @engine_id:  Engine to use for signing
1006  * @cmdname:    Command name used when reporting errors
1007  * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code
1008  *      on failure
1009  */
1010 static int fit_config_process_sig(const char *keydir, const char *keyfile,
1011                 void *keydest, void *fit, const char *conf_name,
1012                 int conf_noffset, int noffset, const char *comment,
1013                 int require_keys, const char *engine_id, const char *cmdname,
1014                 const char *algo_name)
1015 {
1016         struct image_sign_info info;
1017         const char *node_name;
1018         struct image_region *region;
1019         char *region_prop;
1020         int region_proplen;
1021         int region_count;
1022         uint8_t *value;
1023         uint value_len;
1024         int ret;
1025
1026         node_name = fit_get_name(fit, noffset, NULL);
1027         if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1028                                    &region_count, &region_prop,
1029                                    &region_proplen))
1030                 return -1;
1031
1032         if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
1033                                 require_keys ? "conf" : NULL, engine_id,
1034                                 algo_name))
1035                 return -1;
1036
1037         ret = info.crypto->sign(&info, region, region_count, &value,
1038                                 &value_len);
1039         free(region);
1040         if (ret) {
1041                 printf("Failed to sign '%s' signature node in '%s' conf node\n",
1042                        node_name, conf_name);
1043
1044                 /* We allow keys to be missing */
1045                 if (ret == -ENOENT)
1046                         return 0;
1047                 return -1;
1048         }
1049
1050         ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
1051                                   region_prop, region_proplen, cmdname,
1052                                   algo_name);
1053         if (ret) {
1054                 if (ret == -FDT_ERR_NOSPACE)
1055                         return -ENOSPC;
1056                 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
1057                        node_name, conf_name, fdt_strerror(ret));
1058                 return -1;
1059         }
1060         free(value);
1061         free(region_prop);
1062
1063         /* Get keyname again, as FDT has changed and invalidated our pointer */
1064         info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
1065
1066         /* Write the public key into the supplied FDT file */
1067         if (keydest) {
1068                 ret = info.crypto->add_verify_data(&info, keydest);
1069                 if (ret < 0) {
1070                         printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
1071                                node_name, conf_name);
1072                 }
1073                 return ret;
1074         }
1075
1076         return 0;
1077 }
1078
1079 static int fit_config_add_verification_data(const char *keydir,
1080                 const char *keyfile, void *keydest, void *fit, int conf_noffset,
1081                 const char *comment, int require_keys, const char *engine_id,
1082                 const char *cmdname, const char *algo_name,
1083                 struct image_summary *summary)
1084 {
1085         const char *conf_name;
1086         int noffset;
1087
1088         conf_name = fit_get_name(fit, conf_noffset, NULL);
1089
1090         /* Process all hash subnodes of the configuration node */
1091         for (noffset = fdt_first_subnode(fit, conf_noffset);
1092              noffset >= 0;
1093              noffset = fdt_next_subnode(fit, noffset)) {
1094                 const char *node_name;
1095                 int ret = 0;
1096
1097                 node_name = fit_get_name(fit, noffset, NULL);
1098                 if (!strncmp(node_name, FIT_SIG_NODENAME,
1099                              strlen(FIT_SIG_NODENAME))) {
1100                         ret = fit_config_process_sig(keydir, keyfile, keydest,
1101                                 fit, conf_name, conf_noffset, noffset, comment,
1102                                 require_keys, engine_id, cmdname, algo_name);
1103                         if (ret < 0)
1104                                 return ret;
1105
1106                         summary->sig_offset = noffset;
1107                         fdt_get_path(fit, noffset, summary->sig_path,
1108                                      sizeof(summary->sig_path));
1109
1110                         if (keydest) {
1111                                 summary->keydest_offset = ret;
1112                                 fdt_get_path(keydest, ret,
1113                                              summary->keydest_path,
1114                                              sizeof(summary->keydest_path));
1115                         }
1116                 }
1117         }
1118
1119         return 0;
1120 }
1121
1122 /*
1123  * 0) open file (open)
1124  * 1) read certificate (PEM_read_X509)
1125  * 2) get public key (X509_get_pubkey)
1126  * 3) provide der format (d2i_RSAPublicKey)
1127  */
1128 static int read_pub_key(const char *keydir, const void *name,
1129                         unsigned char **pubkey, int *pubkey_len)
1130 {
1131         char path[1024];
1132         EVP_PKEY *key = NULL;
1133         X509 *cert;
1134         FILE *f;
1135         int ret;
1136
1137         memset(path, 0, 1024);
1138         snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
1139
1140         /* Open certificate file */
1141         f = fopen(path, "r");
1142         if (!f) {
1143                 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
1144                         path, strerror(errno));
1145                 return -EACCES;
1146         }
1147
1148         /* Read the certificate */
1149         cert = NULL;
1150         if (!PEM_read_X509(f, &cert, NULL, NULL)) {
1151                 printf("Couldn't read certificate");
1152                 ret = -EINVAL;
1153                 goto err_cert;
1154         }
1155
1156         /* Get the public key from the certificate. */
1157         key = X509_get_pubkey(cert);
1158         if (!key) {
1159                 printf("Couldn't read public key\n");
1160                 ret = -EINVAL;
1161                 goto err_pubkey;
1162         }
1163
1164         /* Get DER form */
1165         ret = i2d_PublicKey(key, pubkey);
1166         if (ret < 0) {
1167                 printf("Couldn't get DER form\n");
1168                 ret = -EINVAL;
1169                 goto err_pubkey;
1170         }
1171
1172         *pubkey_len = ret;
1173         ret = 0;
1174
1175 err_pubkey:
1176         X509_free(cert);
1177 err_cert:
1178         fclose(f);
1179         return ret;
1180 }
1181
1182 int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
1183 {
1184         int pre_load_noffset;
1185         const void *algo_name;
1186         const void *key_name;
1187         unsigned char *pubkey = NULL;
1188         int ret, pubkey_len;
1189
1190         if (!keydir || !keydest || !fit)
1191                 return 0;
1192
1193         /* Search node pre-load sig */
1194         pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
1195         if (pre_load_noffset < 0) {
1196                 ret = 0;
1197                 goto out;
1198         }
1199
1200         algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
1201         key_name  = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
1202
1203         /* Check that all mandatory properties are present */
1204         if (!algo_name || !key_name) {
1205                 if (!algo_name)
1206                         printf("The property algo-name is missing in the node %s\n",
1207                                IMAGE_PRE_LOAD_PATH);
1208                 if (!key_name)
1209                         printf("The property key-name is missing in the node %s\n",
1210                                IMAGE_PRE_LOAD_PATH);
1211                 ret = -EINVAL;
1212                 goto out;
1213         }
1214
1215         /* Read public key */
1216         ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
1217         if (ret < 0)
1218                 goto out;
1219
1220         /* Add the public key to the device tree */
1221         ret = fdt_setprop(keydest, pre_load_noffset, "public-key",
1222                           pubkey, pubkey_len);
1223         if (ret)
1224                 printf("Can't set public-key in node %s (ret = %d)\n",
1225                        IMAGE_PRE_LOAD_PATH, ret);
1226
1227  out:
1228         return ret;
1229 }
1230
1231 int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1232                     const char *comment, int require_keys,
1233                     const char *engine_id, const char *cmdname)
1234 {
1235         int images_noffset;
1236         int noffset;
1237         int ret;
1238
1239         /* Find images parent node offset */
1240         images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1241         if (images_noffset < 0) {
1242                 printf("Can't find images parent node '%s' (%s)\n",
1243                        FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1244                 return images_noffset;
1245         }
1246
1247         /* Process its subnodes, print out component images details */
1248         for (noffset = fdt_first_subnode(fit, images_noffset);
1249              noffset >= 0;
1250              noffset = fdt_next_subnode(fit, noffset)) {
1251                 /*
1252                  * Direct child node of the images parent node,
1253                  * i.e. component image node.
1254                  */
1255                 ret = fit_image_cipher_data(keydir, keydest,
1256                                             fit, noffset, comment,
1257                                             require_keys, engine_id,
1258                                             cmdname);
1259                 if (ret)
1260                         return ret;
1261         }
1262
1263         return 0;
1264 }
1265
1266 int fit_add_verification_data(const char *keydir, const char *keyfile,
1267                               void *keydest, void *fit, const char *comment,
1268                               int require_keys, const char *engine_id,
1269                               const char *cmdname, const char *algo_name,
1270                               struct image_summary *summary)
1271 {
1272         int images_noffset, confs_noffset;
1273         int noffset;
1274         int ret;
1275
1276         /* Find images parent node offset */
1277         images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1278         if (images_noffset < 0) {
1279                 printf("Can't find images parent node '%s' (%s)\n",
1280                        FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1281                 return images_noffset;
1282         }
1283
1284         /* Process its subnodes, print out component images details */
1285         for (noffset = fdt_first_subnode(fit, images_noffset);
1286              noffset >= 0;
1287              noffset = fdt_next_subnode(fit, noffset)) {
1288                 /*
1289                  * Direct child node of the images parent node,
1290                  * i.e. component image node.
1291                  */
1292                 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
1293                                 fit, noffset, comment, require_keys, engine_id,
1294                                 cmdname, algo_name);
1295                 if (ret) {
1296                         printf("Can't add verification data for node '%s' (%s)\n",
1297                                fdt_get_name(fit, noffset, NULL),
1298                                fdt_strerror(ret));
1299                         return ret;
1300                 }
1301         }
1302
1303         /* If there are no keys, we can't sign configurations */
1304         if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
1305                 return 0;
1306
1307         /* Find configurations parent node offset */
1308         confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1309         if (confs_noffset < 0) {
1310                 printf("Can't find images parent node '%s' (%s)\n",
1311                        FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1312                 return -ENOENT;
1313         }
1314
1315         /* Process its subnodes, print out component images details */
1316         for (noffset = fdt_first_subnode(fit, confs_noffset);
1317              noffset >= 0;
1318              noffset = fdt_next_subnode(fit, noffset)) {
1319                 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
1320                                                        fit, noffset, comment,
1321                                                        require_keys,
1322                                                        engine_id, cmdname,
1323                                                        algo_name, summary);
1324                 if (ret)
1325                         return ret;
1326         }
1327
1328         return 0;
1329 }
1330
1331 #ifdef CONFIG_FIT_SIGNATURE
1332 int fit_check_sign(const void *fit, const void *key,
1333                    const char *fit_uname_config)
1334 {
1335         int cfg_noffset;
1336         int ret;
1337
1338         cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
1339         if (!cfg_noffset)
1340                 return -1;
1341
1342         printf("Verifying Hash Integrity for node '%s'... ",
1343                fdt_get_name(fit, cfg_noffset, NULL));
1344         ret = fit_config_verify(fit, cfg_noffset);
1345         if (ret)
1346                 return ret;
1347         printf("Verified OK, loading images\n");
1348         ret = bootm_host_load_images(fit, cfg_noffset);
1349
1350         return ret;
1351 }
1352 #endif