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