1 // SPDX-License-Identifier: GPL-2.0+
2 /* EFI signature/key/certificate list parser
4 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "EFI: "fmt
9 #include <linux/module.h>
10 #include <linux/printk.h>
11 #include <linux/err.h>
12 #include <linux/efi.h>
15 * parse_efi_signature_list - Parse an EFI signature list for certificates
16 * @source: The source of the key
17 * @data: The data blob to parse
18 * @size: The size of the data blob
19 * @get_handler_for_guid: Get the handler func for the sig type (or NULL)
21 * Parse an EFI signature list looking for elements of interest. A list is
22 * made up of a series of sublists, where all the elements in a sublist are of
23 * the same type, but sublists can be of different types.
25 * For each sublist encountered, the @get_handler_for_guid function is called
26 * with the type specifier GUID and returns either a pointer to a function to
27 * handle elements of that type or NULL if the type is not of interest.
29 * If the sublist is of interest, each element is passed to the handler
32 * Error EBADMSG is returned if the list doesn't parse correctly and 0 is
33 * returned if the list was parsed correctly. No error can be returned from
34 * the @get_handler_for_guid function or the element handler function it
37 int __init parse_efi_signature_list(
39 const void *data, size_t size,
40 efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *))
42 efi_element_handler_t handler;
43 unsigned int offs = 0;
45 pr_devel("-->%s(,%zu)\n", __func__, size);
48 const efi_signature_data_t *elem;
49 efi_signature_list_t list;
50 size_t lsize, esize, hsize, elsize;
52 if (size < sizeof(list))
55 memcpy(&list, data, sizeof(list));
56 pr_devel("LIST[%04x] guid=%pUl ls=%x hs=%x ss=%x\n",
58 &list.signature_type, list.signature_list_size,
59 list.signature_header_size, list.signature_size);
61 lsize = list.signature_list_size;
62 hsize = list.signature_header_size;
63 esize = list.signature_size;
64 elsize = lsize - sizeof(list) - hsize;
67 pr_devel("<--%s() = -EBADMSG [overrun @%x]\n",
72 if (lsize < sizeof(list) ||
73 lsize - sizeof(list) < hsize ||
74 esize < sizeof(*elem) ||
76 elsize % esize != 0) {
77 pr_devel("- bad size combo @%x\n", offs);
81 handler = get_handler_for_guid(&list.signature_type);
89 data += sizeof(list) + hsize;
90 size -= sizeof(list) + hsize;
91 offs += sizeof(list) + hsize;
93 for (; elsize > 0; elsize -= esize) {
96 pr_devel("ELEM[%04x]\n", offs);
98 &elem->signature_data,
99 esize - sizeof(*elem));