AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2018 Patrick Wildt <patrick@blueri.se> |
| 4 | * Copyright (c) 2019 Linaro Limited, Author: AKASHI Takahiro |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <charset.h> |
| 9 | #include <efi_loader.h> |
Heinrich Schuchardt | caeb73b | 2021-09-09 08:25:08 +0200 | [diff] [blame] | 10 | #include <efi_variable.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 11 | #include <image.h> |
| 12 | #include <hexdump.h> |
| 13 | #include <malloc.h> |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 14 | #include <crypto/pkcs7.h> |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 15 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 16 | #include <crypto/public_key.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 17 | #include <linux/compat.h> |
| 18 | #include <linux/oid_registry.h> |
Masahisa Kojima | 915e427 | 2021-05-14 09:53:36 +0900 | [diff] [blame] | 19 | #include <u-boot/hash-checksum.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 20 | #include <u-boot/rsa.h> |
| 21 | #include <u-boot/sha256.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 22 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 23 | const efi_guid_t efi_guid_sha256 = EFI_CERT_SHA256_GUID; |
| 24 | const efi_guid_t efi_guid_cert_rsa2048 = EFI_CERT_RSA2048_GUID; |
| 25 | const efi_guid_t efi_guid_cert_x509 = EFI_CERT_X509_GUID; |
| 26 | const efi_guid_t efi_guid_cert_x509_sha256 = EFI_CERT_X509_SHA256_GUID; |
AKASHI Takahiro | 5ed9ada | 2020-05-29 15:41:18 +0900 | [diff] [blame] | 27 | const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 28 | |
Sughosh Ganu | ea35008 | 2020-12-30 19:27:07 +0530 | [diff] [blame] | 29 | static u8 pkcs7_hdr[] = { |
| 30 | /* SEQUENCE */ |
| 31 | 0x30, 0x82, 0x05, 0xc7, |
| 32 | /* OID: pkcs7-signedData */ |
| 33 | 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, |
| 34 | /* Context Structured? */ |
| 35 | 0xa0, 0x82, 0x05, 0xb8, |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * efi_parse_pkcs7_header - parse a signature in payload |
| 40 | * @buf: Pointer to payload's value |
| 41 | * @buflen: Length of @buf |
| 42 | * @tmpbuf: Pointer to temporary buffer |
| 43 | * |
| 44 | * Parse a signature embedded in payload's value and instantiate |
| 45 | * a pkcs7_message structure. Since pkcs7_parse_message() accepts only |
| 46 | * pkcs7's signedData, some header needed be prepended for correctly |
| 47 | * parsing authentication data |
| 48 | * A temporary buffer will be allocated if needed, and it should be |
| 49 | * kept valid during the authentication because some data in the buffer |
| 50 | * will be referenced by efi_signature_verify(). |
| 51 | * |
| 52 | * Return: Pointer to pkcs7_message structure on success, NULL on error |
| 53 | */ |
| 54 | struct pkcs7_message *efi_parse_pkcs7_header(const void *buf, |
| 55 | size_t buflen, |
| 56 | u8 **tmpbuf) |
| 57 | { |
| 58 | u8 *ebuf; |
| 59 | size_t ebuflen, len; |
| 60 | struct pkcs7_message *msg; |
| 61 | |
| 62 | /* |
| 63 | * This is the best assumption to check if the binary is |
| 64 | * already in a form of pkcs7's signedData. |
| 65 | */ |
| 66 | if (buflen > sizeof(pkcs7_hdr) && |
| 67 | !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) { |
| 68 | msg = pkcs7_parse_message(buf, buflen); |
| 69 | if (IS_ERR(msg)) |
| 70 | return NULL; |
| 71 | return msg; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Otherwise, we should add a dummy prefix sequence for pkcs7 |
| 76 | * message parser to be able to process. |
| 77 | * NOTE: EDK2 also uses similar hack in WrapPkcs7Data() |
| 78 | * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c |
| 79 | * TODO: |
| 80 | * The header should be composed in a more refined manner. |
| 81 | */ |
| 82 | EFI_PRINT("Makeshift prefix added to authentication data\n"); |
| 83 | ebuflen = sizeof(pkcs7_hdr) + buflen; |
| 84 | if (ebuflen <= 0x7f) { |
| 85 | EFI_PRINT("Data is too short\n"); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | ebuf = malloc(ebuflen); |
| 90 | if (!ebuf) { |
| 91 | EFI_PRINT("Out of memory\n"); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr)); |
| 96 | memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen); |
| 97 | len = ebuflen - 4; |
| 98 | ebuf[2] = (len >> 8) & 0xff; |
| 99 | ebuf[3] = len & 0xff; |
| 100 | len = ebuflen - 0x13; |
| 101 | ebuf[0x11] = (len >> 8) & 0xff; |
| 102 | ebuf[0x12] = len & 0xff; |
| 103 | |
| 104 | msg = pkcs7_parse_message(ebuf, ebuflen); |
| 105 | |
| 106 | if (IS_ERR(msg)) { |
| 107 | free(ebuf); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | *tmpbuf = ebuf; |
| 112 | return msg; |
| 113 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * efi_hash_regions - calculate a hash value |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 117 | * @regs: Array of regions |
| 118 | * @count: Number of regions |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 119 | * @hash: Pointer to a pointer to buffer holding a hash value |
| 120 | * @size: Size of buffer to be returned |
| 121 | * |
| 122 | * Calculate a sha256 value of @regs and return a value in @hash. |
| 123 | * |
| 124 | * Return: true on success, false on error |
| 125 | */ |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 126 | static bool efi_hash_regions(struct image_region *regs, int count, |
| 127 | void **hash, size_t *size) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 128 | { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 129 | if (!*hash) { |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 130 | *hash = calloc(1, SHA256_SUM_LEN); |
| 131 | if (!*hash) { |
| 132 | EFI_PRINT("Out of memory\n"); |
| 133 | return false; |
| 134 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 135 | } |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 136 | if (size) |
| 137 | *size = SHA256_SUM_LEN; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 138 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 139 | hash_calculate("sha256", regs, count, *hash); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 140 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 141 | EFI_PRINT("hash calculated:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 142 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 143 | *hash, SHA256_SUM_LEN, false); |
| 144 | #endif |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | /** |
Ilias Apalodimas | 71eae98 | 2022-01-29 00:20:31 +0200 | [diff] [blame] | 150 | * hash_algo_supported - check if the requested hash algorithm is supported |
| 151 | * @guid: guid of the algorithm |
| 152 | * |
| 153 | * Return: true if supported false otherwise |
| 154 | */ |
| 155 | static bool hash_algo_supported(const efi_guid_t guid) |
| 156 | { |
| 157 | int i; |
| 158 | const efi_guid_t unsupported_hashes[] = { |
| 159 | EFI_CERT_SHA1_GUID, |
| 160 | EFI_CERT_SHA224_GUID, |
| 161 | EFI_CERT_SHA384_GUID, |
| 162 | EFI_CERT_SHA512_GUID, |
| 163 | }; |
| 164 | |
| 165 | for (i = 0; i < ARRAY_SIZE(unsupported_hashes); i++) { |
| 166 | if (!guidcmp(&unsupported_hashes[i], &guid)) |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /** |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 174 | * efi_signature_lookup_digest - search for an image's digest in sigdb |
| 175 | * @regs: List of regions to be authenticated |
| 176 | * @db: Signature database for trusted certificates |
Ilias Apalodimas | 71eae98 | 2022-01-29 00:20:31 +0200 | [diff] [blame] | 177 | * @dbx Caller needs to set this to true if he is searching dbx |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 178 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 179 | * A message digest of image pointed to by @regs is calculated and |
| 180 | * its hash value is compared to entries in signature database pointed |
| 181 | * to by @db. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 182 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 183 | * Return: true if found, false if not |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 184 | */ |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 185 | bool efi_signature_lookup_digest(struct efi_image_regions *regs, |
Ilias Apalodimas | 71eae98 | 2022-01-29 00:20:31 +0200 | [diff] [blame] | 186 | struct efi_signature_store *db, |
| 187 | bool dbx) |
| 188 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 189 | { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 190 | struct efi_signature_store *siglist; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 191 | struct efi_sig_data *sig_data; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 192 | void *hash = NULL; |
| 193 | size_t size = 0; |
| 194 | bool found = false; |
Ilias Apalodimas | 81adfff | 2022-01-29 00:20:32 +0200 | [diff] [blame] | 195 | bool hash_done = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 196 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 197 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, regs, db); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 198 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 199 | if (!regs || !db || !db->sig_data_list) |
| 200 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 201 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 202 | for (siglist = db; siglist; siglist = siglist->next) { |
Ilias Apalodimas | 71eae98 | 2022-01-29 00:20:31 +0200 | [diff] [blame] | 203 | /* |
| 204 | * if the hash algorithm is unsupported and we get an entry in |
| 205 | * dbx reject the image |
| 206 | */ |
| 207 | if (dbx && !hash_algo_supported(siglist->sig_type)) { |
| 208 | found = true; |
| 209 | continue; |
| 210 | }; |
| 211 | /* |
| 212 | * Only support sha256 for now, that's what |
| 213 | * hash-to-efi-sig-list produces |
| 214 | */ |
| 215 | if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) |
| 216 | continue; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 217 | |
Ilias Apalodimas | 81adfff | 2022-01-29 00:20:32 +0200 | [diff] [blame] | 218 | if (!hash_done && |
| 219 | !efi_hash_regions(regs->reg, regs->num, &hash, &size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 220 | EFI_PRINT("Digesting an image failed\n"); |
| 221 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 222 | } |
Ilias Apalodimas | 81adfff | 2022-01-29 00:20:32 +0200 | [diff] [blame] | 223 | hash_done = true; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 224 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 225 | for (sig_data = siglist->sig_data_list; sig_data; |
| 226 | sig_data = sig_data->next) { |
| 227 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 228 | EFI_PRINT("Msg digest in database:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 229 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 230 | sig_data->data, sig_data->size, false); |
| 231 | #endif |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 232 | if (sig_data->size == size && |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 233 | !memcmp(sig_data->data, hash, size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 234 | found = true; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 235 | free(hash); |
| 236 | goto out; |
| 237 | } |
| 238 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 239 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 240 | free(hash); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 241 | hash = NULL; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 242 | } |
| 243 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 244 | out: |
| 245 | EFI_PRINT("%s: Exit, found: %d\n", __func__, found); |
| 246 | return found; |
| 247 | } |
| 248 | |
| 249 | /** |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 250 | * efi_lookup_certificate - find a certificate within db |
| 251 | * @msg: Signature |
| 252 | * @db: Signature database |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 253 | * |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 254 | * Search signature database pointed to by @db and find a certificate |
| 255 | * pointed to by @cert. |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 256 | * |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 257 | * Return: true if found, false otherwise. |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 258 | */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 259 | static bool efi_lookup_certificate(struct x509_certificate *cert, |
| 260 | struct efi_signature_store *db) |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 261 | { |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 262 | struct efi_signature_store *siglist; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 263 | struct efi_sig_data *sig_data; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 264 | struct image_region reg[1]; |
| 265 | void *hash = NULL, *hash_tmp = NULL; |
| 266 | size_t size = 0; |
| 267 | bool found = false; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 268 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 269 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, cert, db); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 270 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 271 | if (!cert || !db || !db->sig_data_list) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 272 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 273 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 274 | /* |
| 275 | * TODO: identify a certificate using sha256 digest |
| 276 | * Is there any better way? |
| 277 | */ |
| 278 | /* calculate hash of TBSCertificate */ |
| 279 | reg[0].data = cert->tbs; |
| 280 | reg[0].size = cert->tbs_size; |
| 281 | if (!efi_hash_regions(reg, 1, &hash, &size)) |
| 282 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 283 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 284 | EFI_PRINT("%s: searching for %s\n", __func__, cert->subject); |
| 285 | for (siglist = db; siglist; siglist = siglist->next) { |
| 286 | /* only with x509 certificate */ |
| 287 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) |
| 288 | continue; |
| 289 | |
| 290 | for (sig_data = siglist->sig_data_list; sig_data; |
| 291 | sig_data = sig_data->next) { |
| 292 | struct x509_certificate *cert_tmp; |
| 293 | |
| 294 | cert_tmp = x509_cert_parse(sig_data->data, |
| 295 | sig_data->size); |
| 296 | if (IS_ERR_OR_NULL(cert_tmp)) |
| 297 | continue; |
| 298 | |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 299 | EFI_PRINT("%s: against %s\n", __func__, |
| 300 | cert_tmp->subject); |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 301 | reg[0].data = cert_tmp->tbs; |
| 302 | reg[0].size = cert_tmp->tbs_size; |
| 303 | if (!efi_hash_regions(reg, 1, &hash_tmp, NULL)) |
| 304 | goto out; |
| 305 | |
| 306 | x509_free_certificate(cert_tmp); |
| 307 | |
| 308 | if (!memcmp(hash, hash_tmp, size)) { |
| 309 | found = true; |
| 310 | goto out; |
| 311 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 312 | } |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 313 | } |
| 314 | out: |
| 315 | free(hash); |
| 316 | free(hash_tmp); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 317 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 318 | EFI_PRINT("%s: Exit, found: %d\n", __func__, found); |
| 319 | return found; |
| 320 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 321 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 322 | /** |
| 323 | * efi_verify_certificate - verify certificate's signature with database |
| 324 | * @signer: Certificate |
| 325 | * @db: Signature database |
| 326 | * @root: Certificate to verify @signer |
| 327 | * |
| 328 | * Determine if certificate pointed to by @signer may be verified |
| 329 | * by one of certificates in signature database pointed to by @db. |
| 330 | * |
| 331 | * Return: true if certificate is verified, false otherwise. |
| 332 | */ |
| 333 | static bool efi_verify_certificate(struct x509_certificate *signer, |
| 334 | struct efi_signature_store *db, |
| 335 | struct x509_certificate **root) |
| 336 | { |
| 337 | struct efi_signature_store *siglist; |
| 338 | struct efi_sig_data *sig_data; |
| 339 | struct x509_certificate *cert; |
| 340 | bool verified = false; |
| 341 | int ret; |
| 342 | |
| 343 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, signer, db); |
| 344 | |
| 345 | if (!signer || !db || !db->sig_data_list) |
| 346 | goto out; |
| 347 | |
| 348 | for (siglist = db; siglist; siglist = siglist->next) { |
| 349 | /* only with x509 certificate */ |
| 350 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) |
| 351 | continue; |
| 352 | |
| 353 | for (sig_data = siglist->sig_data_list; sig_data; |
| 354 | sig_data = sig_data->next) { |
| 355 | cert = x509_cert_parse(sig_data->data, sig_data->size); |
| 356 | if (IS_ERR_OR_NULL(cert)) { |
| 357 | EFI_PRINT("Cannot parse x509 certificate\n"); |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | ret = public_key_verify_signature(cert->pub, |
| 362 | signer->sig); |
| 363 | if (!ret) { |
| 364 | verified = true; |
| 365 | if (root) |
| 366 | *root = cert; |
| 367 | else |
| 368 | x509_free_certificate(cert); |
| 369 | goto out; |
| 370 | } |
| 371 | x509_free_certificate(cert); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 372 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | out: |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 376 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 377 | return verified; |
| 378 | } |
| 379 | |
| 380 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 381 | * efi_signature_check_revocation - check revocation with dbx |
| 382 | * @sinfo: Signer's info |
| 383 | * @cert: x509 certificate |
| 384 | * @dbx: Revocation signature database |
| 385 | * |
| 386 | * Search revocation signature database pointed to by @dbx and find |
| 387 | * an entry matching to certificate pointed to by @cert. |
| 388 | * |
| 389 | * While this entry contains revocation time, we don't support timestamp |
| 390 | * protocol at this time and any image will be unconditionally revoked |
| 391 | * when this match occurs. |
| 392 | * |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 393 | * Return: true if check passed (not found), false otherwise. |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 394 | */ |
| 395 | static bool efi_signature_check_revocation(struct pkcs7_signed_info *sinfo, |
| 396 | struct x509_certificate *cert, |
| 397 | struct efi_signature_store *dbx) |
| 398 | { |
| 399 | struct efi_signature_store *siglist; |
| 400 | struct efi_sig_data *sig_data; |
| 401 | struct image_region reg[1]; |
| 402 | void *hash = NULL; |
| 403 | size_t size = 0; |
| 404 | time64_t revoc_time; |
| 405 | bool revoked = false; |
| 406 | |
| 407 | EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, sinfo, cert, dbx); |
| 408 | |
| 409 | if (!sinfo || !cert || !dbx || !dbx->sig_data_list) |
| 410 | goto out; |
| 411 | |
| 412 | EFI_PRINT("Checking revocation against %s\n", cert->subject); |
| 413 | for (siglist = dbx; siglist; siglist = siglist->next) { |
| 414 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256)) |
| 415 | continue; |
| 416 | |
| 417 | /* calculate hash of TBSCertificate */ |
| 418 | reg[0].data = cert->tbs; |
| 419 | reg[0].size = cert->tbs_size; |
| 420 | if (!efi_hash_regions(reg, 1, &hash, &size)) |
| 421 | goto out; |
| 422 | |
| 423 | for (sig_data = siglist->sig_data_list; sig_data; |
| 424 | sig_data = sig_data->next) { |
| 425 | /* |
| 426 | * struct efi_cert_x509_sha256 { |
| 427 | * u8 tbs_hash[256/8]; |
| 428 | * time64_t revocation_time; |
| 429 | * }; |
| 430 | */ |
| 431 | #ifdef DEBUG |
| 432 | if (sig_data->size >= size) { |
| 433 | EFI_PRINT("hash in db:\n"); |
| 434 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, |
| 435 | 16, 1, |
| 436 | sig_data->data, size, false); |
| 437 | } |
| 438 | #endif |
| 439 | if ((sig_data->size < size + sizeof(time64_t)) || |
| 440 | memcmp(sig_data->data, hash, size)) |
| 441 | continue; |
| 442 | |
| 443 | memcpy(&revoc_time, sig_data->data + size, |
| 444 | sizeof(revoc_time)); |
| 445 | EFI_PRINT("revocation time: 0x%llx\n", revoc_time); |
| 446 | /* |
| 447 | * TODO: compare signing timestamp in sinfo |
| 448 | * with revocation time |
| 449 | */ |
| 450 | |
| 451 | revoked = true; |
| 452 | free(hash); |
| 453 | goto out; |
| 454 | } |
| 455 | free(hash); |
| 456 | hash = NULL; |
| 457 | } |
| 458 | out: |
| 459 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 460 | return !revoked; |
| 461 | } |
| 462 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 463 | /* |
| 464 | * efi_signature_verify - verify signatures with db and dbx |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 465 | * @regs: List of regions to be authenticated |
| 466 | * @msg: Signature |
| 467 | * @db: Signature database for trusted certificates |
| 468 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 469 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 470 | * All the signature pointed to by @msg against image pointed to by @regs |
| 471 | * will be verified by signature database pointed to by @db and @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 472 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 473 | * Return: true if verification for all signatures passed, false otherwise |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 474 | */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 475 | bool efi_signature_verify(struct efi_image_regions *regs, |
| 476 | struct pkcs7_message *msg, |
| 477 | struct efi_signature_store *db, |
| 478 | struct efi_signature_store *dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 479 | { |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 480 | struct pkcs7_signed_info *sinfo; |
| 481 | struct x509_certificate *signer, *root; |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 482 | bool verified = false; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 483 | int ret; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 484 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 485 | EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__, regs, msg, db, dbx); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 486 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 487 | if (!regs || !msg || !db || !db->sig_data_list) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 488 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 489 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 490 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 491 | EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n", |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 492 | sinfo->sig->hash_algo, sinfo->sig->pkey_algo); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 493 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 494 | /* |
| 495 | * only for authenticated variable. |
| 496 | * |
| 497 | * If this function is called for image, |
| 498 | * hash calculation will be done in |
| 499 | * pkcs7_verify_one(). |
| 500 | */ |
| 501 | if (!msg->data && |
| 502 | !efi_hash_regions(regs->reg, regs->num, |
| 503 | (void **)&sinfo->sig->digest, NULL)) { |
| 504 | EFI_PRINT("Digesting an image failed\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 505 | goto out; |
| 506 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 507 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 508 | EFI_PRINT("Verifying certificate chain\n"); |
| 509 | signer = NULL; |
| 510 | ret = pkcs7_verify_one(msg, sinfo, &signer); |
| 511 | if (ret == -ENOPKG) |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 512 | continue; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 513 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 514 | if (ret < 0 || !signer) |
| 515 | goto out; |
| 516 | |
| 517 | if (sinfo->blacklisted) |
| 518 | goto out; |
| 519 | |
| 520 | EFI_PRINT("Verifying last certificate in chain\n"); |
Ilias Apalodimas | 1ee9c48 | 2022-02-14 11:14:22 +0200 | [diff] [blame] | 521 | if (efi_lookup_certificate(signer, db)) |
| 522 | if (efi_signature_check_revocation(sinfo, signer, dbx)) |
| 523 | break; |
| 524 | if (!signer->self_signed && |
| 525 | efi_verify_certificate(signer, db, &root)) { |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 526 | bool check; |
| 527 | |
| 528 | check = efi_signature_check_revocation(sinfo, root, |
| 529 | dbx); |
| 530 | x509_free_certificate(root); |
| 531 | if (check) |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 532 | break; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | EFI_PRINT("Certificate chain didn't reach trusted CA\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 536 | } |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 537 | if (sinfo) |
| 538 | verified = true; |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 539 | out: |
| 540 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
| 541 | return verified; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 545 | * efi_signature_check_signers - check revocation against all signers with dbx |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 546 | * @msg: Signature |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 547 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 548 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 549 | * Determine if none of signers' certificates in @msg are revoked |
| 550 | * by signature database pointed to by @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 551 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 552 | * Return: true if all signers passed, false otherwise. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 553 | */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 554 | bool efi_signature_check_signers(struct pkcs7_message *msg, |
| 555 | struct efi_signature_store *dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 556 | { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 557 | struct pkcs7_signed_info *sinfo; |
| 558 | bool revoked = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 559 | |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 560 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 561 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 562 | if (!msg || !dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 563 | goto out; |
| 564 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 565 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
| 566 | if (sinfo->signer && |
| 567 | !efi_signature_check_revocation(sinfo, sinfo->signer, |
| 568 | dbx)) { |
| 569 | revoked = true; |
| 570 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | out: |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 574 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 575 | return !revoked; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 579 | * efi_sigstore_free - free signature store |
| 580 | * @sigstore: Pointer to signature store structure |
| 581 | * |
| 582 | * Feee all the memories held in signature store and itself, |
| 583 | * which were allocated by efi_sigstore_parse_sigdb(). |
| 584 | */ |
| 585 | void efi_sigstore_free(struct efi_signature_store *sigstore) |
| 586 | { |
| 587 | struct efi_signature_store *sigstore_next; |
| 588 | struct efi_sig_data *sig_data, *sig_data_next; |
| 589 | |
| 590 | while (sigstore) { |
| 591 | sigstore_next = sigstore->next; |
| 592 | |
| 593 | sig_data = sigstore->sig_data_list; |
| 594 | while (sig_data) { |
| 595 | sig_data_next = sig_data->next; |
| 596 | free(sig_data->data); |
| 597 | free(sig_data); |
| 598 | sig_data = sig_data_next; |
| 599 | } |
| 600 | |
| 601 | free(sigstore); |
| 602 | sigstore = sigstore_next; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * efi_sigstore_parse_siglist - parse a signature list |
| 608 | * @name: Pointer to signature list |
| 609 | * |
| 610 | * Parse signature list and instantiate a signature store structure. |
| 611 | * Signature database is a simple concatenation of one or more |
| 612 | * signature list(s). |
| 613 | * |
| 614 | * Return: Pointer to signature store on success, NULL on error |
| 615 | */ |
| 616 | static struct efi_signature_store * |
| 617 | efi_sigstore_parse_siglist(struct efi_signature_list *esl) |
| 618 | { |
| 619 | struct efi_signature_store *siglist = NULL; |
| 620 | struct efi_sig_data *sig_data, *sig_data_next; |
| 621 | struct efi_signature_data *esd; |
| 622 | size_t left; |
| 623 | |
| 624 | /* |
| 625 | * UEFI specification defines certificate types: |
| 626 | * for non-signed images, |
| 627 | * EFI_CERT_SHA256_GUID |
| 628 | * EFI_CERT_RSA2048_GUID |
| 629 | * EFI_CERT_RSA2048_SHA256_GUID |
| 630 | * EFI_CERT_SHA1_GUID |
| 631 | * EFI_CERT_RSA2048_SHA_GUID |
| 632 | * EFI_CERT_SHA224_GUID |
| 633 | * EFI_CERT_SHA384_GUID |
| 634 | * EFI_CERT_SHA512_GUID |
| 635 | * |
| 636 | * for signed images, |
| 637 | * EFI_CERT_X509_GUID |
| 638 | * NOTE: Each certificate will normally be in a separate |
| 639 | * EFI_SIGNATURE_LIST as the size may vary depending on |
| 640 | * its algo's. |
| 641 | * |
| 642 | * for timestamp revocation of certificate, |
| 643 | * EFI_CERT_X509_SHA512_GUID |
| 644 | * EFI_CERT_X509_SHA256_GUID |
| 645 | * EFI_CERT_X509_SHA384_GUID |
| 646 | */ |
| 647 | |
| 648 | if (esl->signature_list_size |
| 649 | <= (sizeof(*esl) + esl->signature_header_size)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 650 | EFI_PRINT("Siglist in wrong format\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | /* Create a head */ |
| 655 | siglist = calloc(sizeof(*siglist), 1); |
| 656 | if (!siglist) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 657 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 658 | goto err; |
| 659 | } |
| 660 | memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t)); |
| 661 | |
| 662 | /* Go through the list */ |
| 663 | sig_data_next = NULL; |
| 664 | left = esl->signature_list_size |
| 665 | - (sizeof(*esl) + esl->signature_header_size); |
| 666 | esd = (struct efi_signature_data *) |
| 667 | ((u8 *)esl + sizeof(*esl) + esl->signature_header_size); |
| 668 | |
AKASHI Takahiro | a075aa3 | 2020-04-21 09:38:57 +0900 | [diff] [blame] | 669 | while (left > 0) { |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 670 | /* Signature must exist if there is remaining data. */ |
| 671 | if (left < esl->signature_size) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 672 | EFI_PRINT("Certificate is too small\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 673 | goto err; |
| 674 | } |
| 675 | |
| 676 | sig_data = calloc(esl->signature_size |
| 677 | - sizeof(esd->signature_owner), 1); |
| 678 | if (!sig_data) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 679 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 680 | goto err; |
| 681 | } |
| 682 | |
| 683 | /* Append signature data */ |
| 684 | memcpy(&sig_data->owner, &esd->signature_owner, |
| 685 | sizeof(efi_guid_t)); |
| 686 | sig_data->size = esl->signature_size |
| 687 | - sizeof(esd->signature_owner); |
| 688 | sig_data->data = malloc(sig_data->size); |
| 689 | if (!sig_data->data) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 690 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 691 | goto err; |
| 692 | } |
| 693 | memcpy(sig_data->data, esd->signature_data, sig_data->size); |
| 694 | |
| 695 | sig_data->next = sig_data_next; |
| 696 | sig_data_next = sig_data; |
| 697 | |
| 698 | /* Next */ |
| 699 | esd = (struct efi_signature_data *) |
| 700 | ((u8 *)esd + esl->signature_size); |
| 701 | left -= esl->signature_size; |
| 702 | } |
| 703 | siglist->sig_data_list = sig_data_next; |
| 704 | |
| 705 | return siglist; |
| 706 | |
| 707 | err: |
| 708 | efi_sigstore_free(siglist); |
| 709 | |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | /** |
Sughosh Ganu | fa0faa6 | 2020-12-30 19:27:08 +0530 | [diff] [blame] | 714 | * efi_sigstore_parse_sigdb - parse the signature list and populate |
| 715 | * the signature store |
| 716 | * |
| 717 | * @sig_list: Pointer to the signature list |
| 718 | * @size: Size of the signature list |
| 719 | * |
| 720 | * Parse the efi signature list and instantiate a signature store |
| 721 | * structure. |
| 722 | * |
| 723 | * Return: Pointer to signature store on success, NULL on error |
| 724 | */ |
| 725 | struct efi_signature_store *efi_build_signature_store(void *sig_list, |
| 726 | efi_uintn_t size) |
| 727 | { |
| 728 | struct efi_signature_list *esl; |
| 729 | struct efi_signature_store *sigstore = NULL, *siglist; |
| 730 | |
| 731 | esl = sig_list; |
| 732 | while (size > 0) { |
| 733 | /* List must exist if there is remaining data. */ |
| 734 | if (size < sizeof(*esl)) { |
| 735 | EFI_PRINT("Signature list in wrong format\n"); |
| 736 | goto err; |
| 737 | } |
| 738 | |
| 739 | if (size < esl->signature_list_size) { |
| 740 | EFI_PRINT("Signature list in wrong format\n"); |
| 741 | goto err; |
| 742 | } |
| 743 | |
| 744 | /* Parse a single siglist. */ |
| 745 | siglist = efi_sigstore_parse_siglist(esl); |
| 746 | if (!siglist) { |
| 747 | EFI_PRINT("Parsing of signature list of failed\n"); |
| 748 | goto err; |
| 749 | } |
| 750 | |
| 751 | /* Append siglist */ |
| 752 | siglist->next = sigstore; |
| 753 | sigstore = siglist; |
| 754 | |
| 755 | /* Next */ |
| 756 | size -= esl->signature_list_size; |
| 757 | esl = (void *)esl + esl->signature_list_size; |
| 758 | } |
| 759 | free(sig_list); |
| 760 | |
| 761 | return sigstore; |
| 762 | |
| 763 | err: |
| 764 | efi_sigstore_free(sigstore); |
| 765 | free(sig_list); |
| 766 | |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | /** |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 771 | * efi_sigstore_parse_sigdb - parse a signature database variable |
| 772 | * @name: Variable's name |
| 773 | * |
| 774 | * Read in a value of signature database variable pointed to by |
| 775 | * @name, parse it and instantiate a signature store structure. |
| 776 | * |
| 777 | * Return: Pointer to signature store on success, NULL on error |
| 778 | */ |
| 779 | struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name) |
| 780 | { |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 781 | const efi_guid_t *vendor; |
| 782 | void *db; |
| 783 | efi_uintn_t db_size; |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 784 | |
Heinrich Schuchardt | caeb73b | 2021-09-09 08:25:08 +0200 | [diff] [blame] | 785 | vendor = efi_auth_var_get_guid(name); |
| 786 | db = efi_get_var(name, vendor, &db_size); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 787 | if (!db) { |
Heinrich Schuchardt | caeb73b | 2021-09-09 08:25:08 +0200 | [diff] [blame] | 788 | EFI_PRINT("variable, %ls, not found\n", name); |
| 789 | return calloc(sizeof(struct efi_signature_store), 1); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 790 | } |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 791 | |
Sughosh Ganu | fa0faa6 | 2020-12-30 19:27:08 +0530 | [diff] [blame] | 792 | return efi_build_signature_store(db, db_size); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 793 | } |