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> |
| 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> |
| 18 | #include <u-boot/rsa.h> |
| 19 | #include <u-boot/sha256.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 20 | |
| 21 | const efi_guid_t efi_guid_image_security_database = |
| 22 | EFI_IMAGE_SECURITY_DATABASE_GUID; |
| 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 | |
| 29 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 30 | |
| 31 | /** |
| 32 | * efi_hash_regions - calculate a hash value |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 33 | * @regs: Array of regions |
| 34 | * @count: Number of regions |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 35 | * @hash: Pointer to a pointer to buffer holding a hash value |
| 36 | * @size: Size of buffer to be returned |
| 37 | * |
| 38 | * Calculate a sha256 value of @regs and return a value in @hash. |
| 39 | * |
| 40 | * Return: true on success, false on error |
| 41 | */ |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 42 | static bool efi_hash_regions(struct image_region *regs, int count, |
| 43 | void **hash, size_t *size) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 44 | { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 45 | if (!*hash) { |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 46 | *hash = calloc(1, SHA256_SUM_LEN); |
| 47 | if (!*hash) { |
| 48 | EFI_PRINT("Out of memory\n"); |
| 49 | return false; |
| 50 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 51 | } |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 52 | if (size) |
| 53 | *size = SHA256_SUM_LEN; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 54 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 55 | hash_calculate("sha256", regs, count, *hash); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 56 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 57 | EFI_PRINT("hash calculated:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 58 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 59 | *hash, SHA256_SUM_LEN, false); |
| 60 | #endif |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | /** |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 66 | * efi_signature_lookup_digest - search for an image's digest in sigdb |
| 67 | * @regs: List of regions to be authenticated |
| 68 | * @db: Signature database for trusted certificates |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 69 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 70 | * A message digest of image pointed to by @regs is calculated and |
| 71 | * its hash value is compared to entries in signature database pointed |
| 72 | * to by @db. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 73 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 74 | * Return: true if found, false if not |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 75 | */ |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 76 | bool efi_signature_lookup_digest(struct efi_image_regions *regs, |
| 77 | struct efi_signature_store *db) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 78 | { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 79 | struct efi_signature_store *siglist; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 80 | struct efi_sig_data *sig_data; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 81 | void *hash = NULL; |
| 82 | size_t size = 0; |
| 83 | bool found = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 84 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 85 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, regs, db); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 86 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 87 | if (!regs || !db || !db->sig_data_list) |
| 88 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 89 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 90 | for (siglist = db; siglist; siglist = siglist->next) { |
| 91 | /* TODO: support other hash algorithms */ |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 92 | if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 93 | EFI_PRINT("Digest algorithm is not supported: %pUl\n", |
| 94 | &siglist->sig_type); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 95 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 96 | } |
| 97 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 98 | if (!efi_hash_regions(regs->reg, regs->num, &hash, &size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 99 | EFI_PRINT("Digesting an image failed\n"); |
| 100 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 101 | } |
| 102 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 103 | for (sig_data = siglist->sig_data_list; sig_data; |
| 104 | sig_data = sig_data->next) { |
| 105 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 106 | EFI_PRINT("Msg digest in database:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 107 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 108 | sig_data->data, sig_data->size, false); |
| 109 | #endif |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 110 | if (sig_data->size == size && |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 111 | !memcmp(sig_data->data, hash, size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 112 | found = true; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 113 | free(hash); |
| 114 | goto out; |
| 115 | } |
| 116 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 117 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 118 | free(hash); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 119 | hash = NULL; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 120 | } |
| 121 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 122 | out: |
| 123 | EFI_PRINT("%s: Exit, found: %d\n", __func__, found); |
| 124 | return found; |
| 125 | } |
| 126 | |
| 127 | /** |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 128 | * efi_lookup_certificate - find a certificate within db |
| 129 | * @msg: Signature |
| 130 | * @db: Signature database |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 131 | * |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 132 | * Search signature database pointed to by @db and find a certificate |
| 133 | * pointed to by @cert. |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 134 | * |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 135 | * Return: true if found, false otherwise. |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 136 | */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 137 | static bool efi_lookup_certificate(struct x509_certificate *cert, |
| 138 | struct efi_signature_store *db) |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 139 | { |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 140 | struct efi_signature_store *siglist; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 141 | struct efi_sig_data *sig_data; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 142 | struct image_region reg[1]; |
| 143 | void *hash = NULL, *hash_tmp = NULL; |
| 144 | size_t size = 0; |
| 145 | bool found = false; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 146 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 147 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, cert, db); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 148 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 149 | if (!cert || !db || !db->sig_data_list) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 150 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 151 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 152 | /* |
| 153 | * TODO: identify a certificate using sha256 digest |
| 154 | * Is there any better way? |
| 155 | */ |
| 156 | /* calculate hash of TBSCertificate */ |
| 157 | reg[0].data = cert->tbs; |
| 158 | reg[0].size = cert->tbs_size; |
| 159 | if (!efi_hash_regions(reg, 1, &hash, &size)) |
| 160 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 161 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 162 | EFI_PRINT("%s: searching for %s\n", __func__, cert->subject); |
| 163 | for (siglist = db; siglist; siglist = siglist->next) { |
| 164 | /* only with x509 certificate */ |
| 165 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) |
| 166 | continue; |
| 167 | |
| 168 | for (sig_data = siglist->sig_data_list; sig_data; |
| 169 | sig_data = sig_data->next) { |
| 170 | struct x509_certificate *cert_tmp; |
| 171 | |
| 172 | cert_tmp = x509_cert_parse(sig_data->data, |
| 173 | sig_data->size); |
| 174 | if (IS_ERR_OR_NULL(cert_tmp)) |
| 175 | continue; |
| 176 | |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 177 | EFI_PRINT("%s: against %s\n", __func__, |
| 178 | cert_tmp->subject); |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 179 | reg[0].data = cert_tmp->tbs; |
| 180 | reg[0].size = cert_tmp->tbs_size; |
| 181 | if (!efi_hash_regions(reg, 1, &hash_tmp, NULL)) |
| 182 | goto out; |
| 183 | |
| 184 | x509_free_certificate(cert_tmp); |
| 185 | |
| 186 | if (!memcmp(hash, hash_tmp, size)) { |
| 187 | found = true; |
| 188 | goto out; |
| 189 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 190 | } |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 191 | } |
| 192 | out: |
| 193 | free(hash); |
| 194 | free(hash_tmp); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 195 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 196 | EFI_PRINT("%s: Exit, found: %d\n", __func__, found); |
| 197 | return found; |
| 198 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 199 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 200 | /** |
| 201 | * efi_verify_certificate - verify certificate's signature with database |
| 202 | * @signer: Certificate |
| 203 | * @db: Signature database |
| 204 | * @root: Certificate to verify @signer |
| 205 | * |
| 206 | * Determine if certificate pointed to by @signer may be verified |
| 207 | * by one of certificates in signature database pointed to by @db. |
| 208 | * |
| 209 | * Return: true if certificate is verified, false otherwise. |
| 210 | */ |
| 211 | static bool efi_verify_certificate(struct x509_certificate *signer, |
| 212 | struct efi_signature_store *db, |
| 213 | struct x509_certificate **root) |
| 214 | { |
| 215 | struct efi_signature_store *siglist; |
| 216 | struct efi_sig_data *sig_data; |
| 217 | struct x509_certificate *cert; |
| 218 | bool verified = false; |
| 219 | int ret; |
| 220 | |
| 221 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, signer, db); |
| 222 | |
| 223 | if (!signer || !db || !db->sig_data_list) |
| 224 | goto out; |
| 225 | |
| 226 | for (siglist = db; siglist; siglist = siglist->next) { |
| 227 | /* only with x509 certificate */ |
| 228 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) |
| 229 | continue; |
| 230 | |
| 231 | for (sig_data = siglist->sig_data_list; sig_data; |
| 232 | sig_data = sig_data->next) { |
| 233 | cert = x509_cert_parse(sig_data->data, sig_data->size); |
| 234 | if (IS_ERR_OR_NULL(cert)) { |
| 235 | EFI_PRINT("Cannot parse x509 certificate\n"); |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | ret = public_key_verify_signature(cert->pub, |
| 240 | signer->sig); |
| 241 | if (!ret) { |
| 242 | verified = true; |
| 243 | if (root) |
| 244 | *root = cert; |
| 245 | else |
| 246 | x509_free_certificate(cert); |
| 247 | goto out; |
| 248 | } |
| 249 | x509_free_certificate(cert); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 250 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | out: |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 254 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 255 | return verified; |
| 256 | } |
| 257 | |
| 258 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 259 | * efi_signature_check_revocation - check revocation with dbx |
| 260 | * @sinfo: Signer's info |
| 261 | * @cert: x509 certificate |
| 262 | * @dbx: Revocation signature database |
| 263 | * |
| 264 | * Search revocation signature database pointed to by @dbx and find |
| 265 | * an entry matching to certificate pointed to by @cert. |
| 266 | * |
| 267 | * While this entry contains revocation time, we don't support timestamp |
| 268 | * protocol at this time and any image will be unconditionally revoked |
| 269 | * when this match occurs. |
| 270 | * |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 271 | * Return: true if check passed (not found), false otherwise. |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 272 | */ |
| 273 | static bool efi_signature_check_revocation(struct pkcs7_signed_info *sinfo, |
| 274 | struct x509_certificate *cert, |
| 275 | struct efi_signature_store *dbx) |
| 276 | { |
| 277 | struct efi_signature_store *siglist; |
| 278 | struct efi_sig_data *sig_data; |
| 279 | struct image_region reg[1]; |
| 280 | void *hash = NULL; |
| 281 | size_t size = 0; |
| 282 | time64_t revoc_time; |
| 283 | bool revoked = false; |
| 284 | |
| 285 | EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, sinfo, cert, dbx); |
| 286 | |
| 287 | if (!sinfo || !cert || !dbx || !dbx->sig_data_list) |
| 288 | goto out; |
| 289 | |
| 290 | EFI_PRINT("Checking revocation against %s\n", cert->subject); |
| 291 | for (siglist = dbx; siglist; siglist = siglist->next) { |
| 292 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256)) |
| 293 | continue; |
| 294 | |
| 295 | /* calculate hash of TBSCertificate */ |
| 296 | reg[0].data = cert->tbs; |
| 297 | reg[0].size = cert->tbs_size; |
| 298 | if (!efi_hash_regions(reg, 1, &hash, &size)) |
| 299 | goto out; |
| 300 | |
| 301 | for (sig_data = siglist->sig_data_list; sig_data; |
| 302 | sig_data = sig_data->next) { |
| 303 | /* |
| 304 | * struct efi_cert_x509_sha256 { |
| 305 | * u8 tbs_hash[256/8]; |
| 306 | * time64_t revocation_time; |
| 307 | * }; |
| 308 | */ |
| 309 | #ifdef DEBUG |
| 310 | if (sig_data->size >= size) { |
| 311 | EFI_PRINT("hash in db:\n"); |
| 312 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, |
| 313 | 16, 1, |
| 314 | sig_data->data, size, false); |
| 315 | } |
| 316 | #endif |
| 317 | if ((sig_data->size < size + sizeof(time64_t)) || |
| 318 | memcmp(sig_data->data, hash, size)) |
| 319 | continue; |
| 320 | |
| 321 | memcpy(&revoc_time, sig_data->data + size, |
| 322 | sizeof(revoc_time)); |
| 323 | EFI_PRINT("revocation time: 0x%llx\n", revoc_time); |
| 324 | /* |
| 325 | * TODO: compare signing timestamp in sinfo |
| 326 | * with revocation time |
| 327 | */ |
| 328 | |
| 329 | revoked = true; |
| 330 | free(hash); |
| 331 | goto out; |
| 332 | } |
| 333 | free(hash); |
| 334 | hash = NULL; |
| 335 | } |
| 336 | out: |
| 337 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 338 | return !revoked; |
| 339 | } |
| 340 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 341 | /* |
| 342 | * efi_signature_verify - verify signatures with db and dbx |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 343 | * @regs: List of regions to be authenticated |
| 344 | * @msg: Signature |
| 345 | * @db: Signature database for trusted certificates |
| 346 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 347 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 348 | * All the signature pointed to by @msg against image pointed to by @regs |
| 349 | * will be verified by signature database pointed to by @db and @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 350 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 351 | * Return: true if verification for all signatures passed, false otherwise |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 352 | */ |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 353 | bool efi_signature_verify(struct efi_image_regions *regs, |
| 354 | struct pkcs7_message *msg, |
| 355 | struct efi_signature_store *db, |
| 356 | struct efi_signature_store *dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 357 | { |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 358 | struct pkcs7_signed_info *sinfo; |
| 359 | struct x509_certificate *signer, *root; |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 360 | bool verified = false; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 361 | int ret; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 362 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 363 | 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] | 364 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 365 | if (!regs || !msg || !db || !db->sig_data_list) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 366 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 367 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 368 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 369 | EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n", |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 370 | sinfo->sig->hash_algo, sinfo->sig->pkey_algo); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 371 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 372 | /* |
| 373 | * only for authenticated variable. |
| 374 | * |
| 375 | * If this function is called for image, |
| 376 | * hash calculation will be done in |
| 377 | * pkcs7_verify_one(). |
| 378 | */ |
| 379 | if (!msg->data && |
| 380 | !efi_hash_regions(regs->reg, regs->num, |
| 381 | (void **)&sinfo->sig->digest, NULL)) { |
| 382 | EFI_PRINT("Digesting an image failed\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 383 | goto out; |
| 384 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 385 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 386 | EFI_PRINT("Verifying certificate chain\n"); |
| 387 | signer = NULL; |
| 388 | ret = pkcs7_verify_one(msg, sinfo, &signer); |
| 389 | if (ret == -ENOPKG) |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 390 | continue; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 391 | |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 392 | if (ret < 0 || !signer) |
| 393 | goto out; |
| 394 | |
| 395 | if (sinfo->blacklisted) |
| 396 | goto out; |
| 397 | |
| 398 | EFI_PRINT("Verifying last certificate in chain\n"); |
| 399 | if (signer->self_signed) { |
| 400 | if (efi_lookup_certificate(signer, db)) |
| 401 | if (efi_signature_check_revocation(sinfo, |
| 402 | signer, dbx)) |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 403 | break; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 404 | } else if (efi_verify_certificate(signer, db, &root)) { |
| 405 | bool check; |
| 406 | |
| 407 | check = efi_signature_check_revocation(sinfo, root, |
| 408 | dbx); |
| 409 | x509_free_certificate(root); |
| 410 | if (check) |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 411 | break; |
AKASHI Takahiro | 14afd06 | 2020-07-21 19:35:22 +0900 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | EFI_PRINT("Certificate chain didn't reach trusted CA\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 415 | } |
AKASHI Takahiro | 1641180 | 2020-08-14 14:39:23 +0900 | [diff] [blame] | 416 | if (sinfo) |
| 417 | verified = true; |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 418 | out: |
| 419 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
| 420 | return verified; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 424 | * efi_signature_check_signers - check revocation against all signers with dbx |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 425 | * @msg: Signature |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 426 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 427 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 428 | * Determine if none of signers' certificates in @msg are revoked |
| 429 | * by signature database pointed to by @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 430 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 431 | * Return: true if all signers passed, false otherwise. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 432 | */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 433 | bool efi_signature_check_signers(struct pkcs7_message *msg, |
| 434 | struct efi_signature_store *dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 435 | { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 436 | struct pkcs7_signed_info *sinfo; |
| 437 | bool revoked = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 438 | |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 439 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 440 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 441 | if (!msg || !dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 442 | goto out; |
| 443 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 444 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
| 445 | if (sinfo->signer && |
| 446 | !efi_signature_check_revocation(sinfo, sinfo->signer, |
| 447 | dbx)) { |
| 448 | revoked = true; |
| 449 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | out: |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 453 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 454 | return !revoked; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 458 | * efi_image_region_add() - add an entry of region |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 459 | * @regs: Pointer to array of regions |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 460 | * @start: Start address of region (included) |
| 461 | * @end: End address of region (excluded) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 462 | * @nocheck: flag against overlapped regions |
| 463 | * |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 464 | * Take one entry of region [@start, @end[ and insert it into the list. |
| 465 | * |
| 466 | * * If @nocheck is false, the list will be sorted ascending by address. |
| 467 | * Overlapping entries will not be allowed. |
| 468 | * |
| 469 | * * If @nocheck is true, the list will be sorted ascending by sequence |
| 470 | * of adding the entries. Overlapping is allowed. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 471 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 472 | * Return: status code |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 473 | */ |
| 474 | efi_status_t efi_image_region_add(struct efi_image_regions *regs, |
| 475 | const void *start, const void *end, |
| 476 | int nocheck) |
| 477 | { |
| 478 | struct image_region *reg; |
| 479 | int i, j; |
| 480 | |
| 481 | if (regs->num >= regs->max) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 482 | EFI_PRINT("%s: no more room for regions\n", __func__); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 483 | return EFI_OUT_OF_RESOURCES; |
| 484 | } |
| 485 | |
| 486 | if (end < start) |
| 487 | return EFI_INVALID_PARAMETER; |
| 488 | |
| 489 | for (i = 0; i < regs->num; i++) { |
| 490 | reg = ®s->reg[i]; |
| 491 | if (nocheck) |
| 492 | continue; |
| 493 | |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 494 | /* new data after registered region */ |
| 495 | if (start >= reg->data + reg->size) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 496 | continue; |
| 497 | |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 498 | /* new data preceding registered region */ |
| 499 | if (end <= reg->data) { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 500 | for (j = regs->num - 1; j >= i; j--) |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 501 | memcpy(®s->reg[j + 1], ®s->reg[j], |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 502 | sizeof(*reg)); |
| 503 | break; |
| 504 | } |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 505 | |
| 506 | /* new data overlapping registered region */ |
| 507 | EFI_PRINT("%s: new region already part of another\n", __func__); |
| 508 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | reg = ®s->reg[i]; |
| 512 | reg->data = start; |
| 513 | reg->size = end - start; |
| 514 | regs->num++; |
| 515 | |
| 516 | return EFI_SUCCESS; |
| 517 | } |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * efi_sigstore_free - free signature store |
| 521 | * @sigstore: Pointer to signature store structure |
| 522 | * |
| 523 | * Feee all the memories held in signature store and itself, |
| 524 | * which were allocated by efi_sigstore_parse_sigdb(). |
| 525 | */ |
| 526 | void efi_sigstore_free(struct efi_signature_store *sigstore) |
| 527 | { |
| 528 | struct efi_signature_store *sigstore_next; |
| 529 | struct efi_sig_data *sig_data, *sig_data_next; |
| 530 | |
| 531 | while (sigstore) { |
| 532 | sigstore_next = sigstore->next; |
| 533 | |
| 534 | sig_data = sigstore->sig_data_list; |
| 535 | while (sig_data) { |
| 536 | sig_data_next = sig_data->next; |
| 537 | free(sig_data->data); |
| 538 | free(sig_data); |
| 539 | sig_data = sig_data_next; |
| 540 | } |
| 541 | |
| 542 | free(sigstore); |
| 543 | sigstore = sigstore_next; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * efi_sigstore_parse_siglist - parse a signature list |
| 549 | * @name: Pointer to signature list |
| 550 | * |
| 551 | * Parse signature list and instantiate a signature store structure. |
| 552 | * Signature database is a simple concatenation of one or more |
| 553 | * signature list(s). |
| 554 | * |
| 555 | * Return: Pointer to signature store on success, NULL on error |
| 556 | */ |
| 557 | static struct efi_signature_store * |
| 558 | efi_sigstore_parse_siglist(struct efi_signature_list *esl) |
| 559 | { |
| 560 | struct efi_signature_store *siglist = NULL; |
| 561 | struct efi_sig_data *sig_data, *sig_data_next; |
| 562 | struct efi_signature_data *esd; |
| 563 | size_t left; |
| 564 | |
| 565 | /* |
| 566 | * UEFI specification defines certificate types: |
| 567 | * for non-signed images, |
| 568 | * EFI_CERT_SHA256_GUID |
| 569 | * EFI_CERT_RSA2048_GUID |
| 570 | * EFI_CERT_RSA2048_SHA256_GUID |
| 571 | * EFI_CERT_SHA1_GUID |
| 572 | * EFI_CERT_RSA2048_SHA_GUID |
| 573 | * EFI_CERT_SHA224_GUID |
| 574 | * EFI_CERT_SHA384_GUID |
| 575 | * EFI_CERT_SHA512_GUID |
| 576 | * |
| 577 | * for signed images, |
| 578 | * EFI_CERT_X509_GUID |
| 579 | * NOTE: Each certificate will normally be in a separate |
| 580 | * EFI_SIGNATURE_LIST as the size may vary depending on |
| 581 | * its algo's. |
| 582 | * |
| 583 | * for timestamp revocation of certificate, |
| 584 | * EFI_CERT_X509_SHA512_GUID |
| 585 | * EFI_CERT_X509_SHA256_GUID |
| 586 | * EFI_CERT_X509_SHA384_GUID |
| 587 | */ |
| 588 | |
| 589 | if (esl->signature_list_size |
| 590 | <= (sizeof(*esl) + esl->signature_header_size)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 591 | EFI_PRINT("Siglist in wrong format\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | /* Create a head */ |
| 596 | siglist = calloc(sizeof(*siglist), 1); |
| 597 | if (!siglist) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 598 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 599 | goto err; |
| 600 | } |
| 601 | memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t)); |
| 602 | |
| 603 | /* Go through the list */ |
| 604 | sig_data_next = NULL; |
| 605 | left = esl->signature_list_size |
| 606 | - (sizeof(*esl) + esl->signature_header_size); |
| 607 | esd = (struct efi_signature_data *) |
| 608 | ((u8 *)esl + sizeof(*esl) + esl->signature_header_size); |
| 609 | |
AKASHI Takahiro | a075aa3 | 2020-04-21 09:38:57 +0900 | [diff] [blame] | 610 | while (left > 0) { |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 611 | /* Signature must exist if there is remaining data. */ |
| 612 | if (left < esl->signature_size) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 613 | EFI_PRINT("Certificate is too small\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 614 | goto err; |
| 615 | } |
| 616 | |
| 617 | sig_data = calloc(esl->signature_size |
| 618 | - sizeof(esd->signature_owner), 1); |
| 619 | if (!sig_data) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 620 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 621 | goto err; |
| 622 | } |
| 623 | |
| 624 | /* Append signature data */ |
| 625 | memcpy(&sig_data->owner, &esd->signature_owner, |
| 626 | sizeof(efi_guid_t)); |
| 627 | sig_data->size = esl->signature_size |
| 628 | - sizeof(esd->signature_owner); |
| 629 | sig_data->data = malloc(sig_data->size); |
| 630 | if (!sig_data->data) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 631 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 632 | goto err; |
| 633 | } |
| 634 | memcpy(sig_data->data, esd->signature_data, sig_data->size); |
| 635 | |
| 636 | sig_data->next = sig_data_next; |
| 637 | sig_data_next = sig_data; |
| 638 | |
| 639 | /* Next */ |
| 640 | esd = (struct efi_signature_data *) |
| 641 | ((u8 *)esd + esl->signature_size); |
| 642 | left -= esl->signature_size; |
| 643 | } |
| 644 | siglist->sig_data_list = sig_data_next; |
| 645 | |
| 646 | return siglist; |
| 647 | |
| 648 | err: |
| 649 | efi_sigstore_free(siglist); |
| 650 | |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * efi_sigstore_parse_sigdb - parse a signature database variable |
| 656 | * @name: Variable's name |
| 657 | * |
| 658 | * Read in a value of signature database variable pointed to by |
| 659 | * @name, parse it and instantiate a signature store structure. |
| 660 | * |
| 661 | * Return: Pointer to signature store on success, NULL on error |
| 662 | */ |
| 663 | struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name) |
| 664 | { |
| 665 | struct efi_signature_store *sigstore = NULL, *siglist; |
| 666 | struct efi_signature_list *esl; |
| 667 | const efi_guid_t *vendor; |
| 668 | void *db; |
| 669 | efi_uintn_t db_size; |
| 670 | efi_status_t ret; |
| 671 | |
| 672 | if (!u16_strcmp(name, L"PK") || !u16_strcmp(name, L"KEK")) { |
| 673 | vendor = &efi_global_variable_guid; |
| 674 | } else if (!u16_strcmp(name, L"db") || !u16_strcmp(name, L"dbx")) { |
| 675 | vendor = &efi_guid_image_security_database; |
| 676 | } else { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 677 | EFI_PRINT("unknown signature database, %ls\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
| 680 | |
| 681 | /* retrieve variable data */ |
| 682 | db_size = 0; |
| 683 | ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, NULL)); |
| 684 | if (ret == EFI_NOT_FOUND) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 685 | EFI_PRINT("variable, %ls, not found\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 686 | sigstore = calloc(sizeof(*sigstore), 1); |
| 687 | return sigstore; |
| 688 | } else if (ret != EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 689 | EFI_PRINT("Getting variable, %ls, failed\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | db = malloc(db_size); |
| 694 | if (!db) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 695 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 696 | return NULL; |
| 697 | } |
| 698 | |
| 699 | ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, db)); |
| 700 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 701 | EFI_PRINT("Getting variable, %ls, failed\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 702 | goto err; |
| 703 | } |
| 704 | |
| 705 | /* Parse siglist list */ |
| 706 | esl = db; |
| 707 | while (db_size > 0) { |
| 708 | /* List must exist if there is remaining data. */ |
| 709 | if (db_size < sizeof(*esl)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 710 | EFI_PRINT("variable, %ls, in wrong format\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | if (db_size < esl->signature_list_size) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 715 | EFI_PRINT("variable, %ls, in wrong format\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 716 | goto err; |
| 717 | } |
| 718 | |
| 719 | /* Parse a single siglist. */ |
| 720 | siglist = efi_sigstore_parse_siglist(esl); |
| 721 | if (!siglist) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 722 | EFI_PRINT("Parsing signature list of %ls failed\n", |
| 723 | name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 724 | goto err; |
| 725 | } |
| 726 | |
| 727 | /* Append siglist */ |
| 728 | siglist->next = sigstore; |
| 729 | sigstore = siglist; |
| 730 | |
| 731 | /* Next */ |
| 732 | db_size -= esl->signature_list_size; |
| 733 | esl = (void *)esl + esl->signature_list_size; |
| 734 | } |
| 735 | free(db); |
| 736 | |
| 737 | return sigstore; |
| 738 | |
| 739 | err: |
| 740 | efi_sigstore_free(sigstore); |
| 741 | free(db); |
| 742 | |
| 743 | return NULL; |
| 744 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 745 | #endif /* CONFIG_EFI_SECURE_BOOT */ |