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 | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 13 | #include <crypto/pkcs7_parser.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 14 | #include <linux/compat.h> |
| 15 | #include <linux/oid_registry.h> |
| 16 | #include <u-boot/rsa.h> |
| 17 | #include <u-boot/sha256.h> |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 18 | |
| 19 | const efi_guid_t efi_guid_image_security_database = |
| 20 | EFI_IMAGE_SECURITY_DATABASE_GUID; |
| 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; |
AKASHI Takahiro | 5ed9ada | 2020-05-29 15:41:18 +0900 | [diff] [blame] | 25 | 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] | 26 | |
| 27 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 28 | |
| 29 | /** |
| 30 | * efi_hash_regions - calculate a hash value |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 31 | * @regs: Array of regions |
| 32 | * @count: Number of regions |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 33 | * @hash: Pointer to a pointer to buffer holding a hash value |
| 34 | * @size: Size of buffer to be returned |
| 35 | * |
| 36 | * Calculate a sha256 value of @regs and return a value in @hash. |
| 37 | * |
| 38 | * Return: true on success, false on error |
| 39 | */ |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 40 | static bool efi_hash_regions(struct image_region *regs, int count, |
| 41 | void **hash, size_t *size) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 42 | { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 43 | if (!*hash) { |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 44 | *hash = calloc(1, SHA256_SUM_LEN); |
| 45 | if (!*hash) { |
| 46 | EFI_PRINT("Out of memory\n"); |
| 47 | return false; |
| 48 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 49 | } |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 50 | if (size) |
| 51 | *size = SHA256_SUM_LEN; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 52 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 53 | hash_calculate("sha256", regs, count, *hash); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 54 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 55 | EFI_PRINT("hash calculated:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 56 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 57 | *hash, SHA256_SUM_LEN, false); |
| 58 | #endif |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * efi_hash_msg_content - calculate a hash value of contentInfo |
| 65 | * @msg: Signature |
| 66 | * @hash: Pointer to a pointer to buffer holding a hash value |
| 67 | * @size: Size of buffer to be returned |
| 68 | * |
| 69 | * Calculate a sha256 value of contentInfo in @msg and return a value in @hash. |
| 70 | * |
| 71 | * Return: true on success, false on error |
| 72 | */ |
| 73 | static bool efi_hash_msg_content(struct pkcs7_message *msg, void **hash, |
| 74 | size_t *size) |
| 75 | { |
| 76 | struct image_region regtmp; |
| 77 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 78 | regtmp.data = msg->data; |
| 79 | regtmp.size = msg->data_len; |
| 80 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 81 | return efi_hash_regions(®tmp, 1, hash, size); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * efi_signature_verify - verify a signature with a certificate |
| 86 | * @regs: List of regions to be authenticated |
| 87 | * @signed_info: Pointer to PKCS7's signed_info |
| 88 | * @cert: x509 certificate |
| 89 | * |
| 90 | * Signature pointed to by @signed_info against image pointed to by @regs |
| 91 | * is verified by a certificate pointed to by @cert. |
| 92 | * @signed_info holds a signature, including a message digest which is to be |
| 93 | * compared with a hash value calculated from @regs. |
| 94 | * |
| 95 | * Return: true if signature is verified, false if not |
| 96 | */ |
| 97 | static bool efi_signature_verify(struct efi_image_regions *regs, |
| 98 | struct pkcs7_message *msg, |
| 99 | struct pkcs7_signed_info *ps_info, |
| 100 | struct x509_certificate *cert) |
| 101 | { |
| 102 | struct image_sign_info info; |
| 103 | struct image_region regtmp[2]; |
| 104 | void *hash; |
| 105 | size_t size; |
| 106 | char c; |
| 107 | bool verified; |
| 108 | |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 109 | EFI_PRINT("%s: Enter, %p, %p, %p(issuer: %s, subject: %s)\n", __func__, |
| 110 | regs, ps_info, cert, cert->issuer, cert->subject); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 111 | |
| 112 | verified = false; |
| 113 | |
| 114 | memset(&info, '\0', sizeof(info)); |
| 115 | info.padding = image_get_padding_algo("pkcs-1.5"); |
| 116 | /* |
| 117 | * Note: image_get_[checksum|crypto]_algo takes an string |
| 118 | * argument like "<checksum>,<crypto>" |
| 119 | * TODO: support other hash algorithms |
| 120 | */ |
| 121 | if (!strcmp(ps_info->sig->hash_algo, "sha1")) { |
| 122 | info.checksum = image_get_checksum_algo("sha1,rsa2048"); |
| 123 | info.name = "sha1,rsa2048"; |
| 124 | } else if (!strcmp(ps_info->sig->hash_algo, "sha256")) { |
| 125 | info.checksum = image_get_checksum_algo("sha256,rsa2048"); |
| 126 | info.name = "sha256,rsa2048"; |
| 127 | } else { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 128 | EFI_PRINT("unknown msg digest algo: %s\n", |
| 129 | ps_info->sig->hash_algo); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 130 | goto out; |
| 131 | } |
| 132 | info.crypto = image_get_crypto_algo(info.name); |
| 133 | |
| 134 | info.key = cert->pub->key; |
| 135 | info.keylen = cert->pub->keylen; |
| 136 | |
| 137 | /* verify signature */ |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 138 | EFI_PRINT("%s: crypto: %s, signature len:%x\n", __func__, |
| 139 | info.name, ps_info->sig->s_size); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 140 | if (ps_info->aa_set & (1UL << sinfo_has_message_digest)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 141 | EFI_PRINT("%s: RSA verify authentication attribute\n", |
| 142 | __func__); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 143 | /* |
| 144 | * NOTE: This path will be executed only for |
| 145 | * PE image authentication |
| 146 | */ |
| 147 | |
| 148 | /* check if hash matches digest first */ |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 149 | EFI_PRINT("checking msg digest first, len:0x%x\n", |
| 150 | ps_info->msgdigest_len); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 151 | |
| 152 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 153 | EFI_PRINT("hash in database:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 154 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 155 | ps_info->msgdigest, ps_info->msgdigest_len, |
| 156 | false); |
| 157 | #endif |
| 158 | /* against contentInfo first */ |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 159 | hash = NULL; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 160 | if ((msg->data && efi_hash_msg_content(msg, &hash, &size)) || |
| 161 | /* for signed image */ |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 162 | efi_hash_regions(regs->reg, regs->num, &hash, &size)) { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 163 | /* for authenticated variable */ |
| 164 | if (ps_info->msgdigest_len != size || |
| 165 | memcmp(hash, ps_info->msgdigest, size)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 166 | EFI_PRINT("Digest doesn't match\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 167 | free(hash); |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | free(hash); |
| 172 | } else { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 173 | EFI_PRINT("Digesting image failed\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | /* against digest */ |
| 178 | c = 0x31; |
| 179 | regtmp[0].data = &c; |
| 180 | regtmp[0].size = 1; |
| 181 | regtmp[1].data = ps_info->authattrs; |
| 182 | regtmp[1].size = ps_info->authattrs_len; |
| 183 | |
| 184 | if (!rsa_verify(&info, regtmp, 2, |
| 185 | ps_info->sig->s, ps_info->sig->s_size)) |
| 186 | verified = true; |
| 187 | } else { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 188 | EFI_PRINT("%s: RSA verify content data\n", __func__); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 189 | /* against all data */ |
| 190 | if (!rsa_verify(&info, regs->reg, regs->num, |
| 191 | ps_info->sig->s, ps_info->sig->s_size)) |
| 192 | verified = true; |
| 193 | } |
| 194 | |
| 195 | out: |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 196 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 197 | return verified; |
| 198 | } |
| 199 | |
| 200 | /** |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 201 | * efi_signature_lookup_digest - search for an image's digest in sigdb |
| 202 | * @regs: List of regions to be authenticated |
| 203 | * @db: Signature database for trusted certificates |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 204 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 205 | * A message digest of image pointed to by @regs is calculated and |
| 206 | * its hash value is compared to entries in signature database pointed |
| 207 | * to by @db. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 208 | * |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 209 | * Return: true if found, false if not |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 210 | */ |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 211 | bool efi_signature_lookup_digest(struct efi_image_regions *regs, |
| 212 | struct efi_signature_store *db) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 213 | { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 214 | struct efi_signature_store *siglist; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 215 | struct efi_sig_data *sig_data; |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 216 | void *hash = NULL; |
| 217 | size_t size = 0; |
| 218 | bool found = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 219 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 220 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, regs, db); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 221 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 222 | if (!regs || !db || !db->sig_data_list) |
| 223 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 224 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 225 | for (siglist = db; siglist; siglist = siglist->next) { |
| 226 | /* TODO: support other hash algorithms */ |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 227 | if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 228 | EFI_PRINT("Digest algorithm is not supported: %pUl\n", |
| 229 | &siglist->sig_type); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 230 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 231 | } |
| 232 | |
AKASHI Takahiro | ddbfa61 | 2020-07-08 14:01:55 +0900 | [diff] [blame] | 233 | if (!efi_hash_regions(regs->reg, regs->num, &hash, &size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 234 | EFI_PRINT("Digesting an image failed\n"); |
| 235 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 236 | } |
| 237 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 238 | for (sig_data = siglist->sig_data_list; sig_data; |
| 239 | sig_data = sig_data->next) { |
| 240 | #ifdef DEBUG |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 241 | EFI_PRINT("Msg digest in database:\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 242 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 243 | sig_data->data, sig_data->size, false); |
| 244 | #endif |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 245 | if (sig_data->size == size && |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 246 | !memcmp(sig_data->data, hash, size)) { |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 247 | found = true; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 248 | free(hash); |
| 249 | goto out; |
| 250 | } |
| 251 | } |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 252 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 253 | free(hash); |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 254 | hash = NULL; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 255 | } |
| 256 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 257 | out: |
| 258 | EFI_PRINT("%s: Exit, found: %d\n", __func__, found); |
| 259 | return found; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * efi_signature_verify_with_list - verify a signature with signature list |
| 264 | * @regs: List of regions to be authenticated |
| 265 | * @msg: Signature |
| 266 | * @signed_info: Pointer to PKCS7's signed_info |
| 267 | * @siglist: Signature list for certificates |
| 268 | * @valid_cert: x509 certificate that verifies this signature |
| 269 | * |
| 270 | * Signature pointed to by @signed_info against image pointed to by @regs |
| 271 | * is verified by signature list pointed to by @siglist. |
| 272 | * Signature database is a simple concatenation of one or more |
| 273 | * signature list(s). |
| 274 | * |
| 275 | * Return: true if signature is verified, false if not |
| 276 | */ |
| 277 | static |
| 278 | bool efi_signature_verify_with_list(struct efi_image_regions *regs, |
| 279 | struct pkcs7_message *msg, |
| 280 | struct pkcs7_signed_info *signed_info, |
| 281 | struct efi_signature_store *siglist, |
| 282 | struct x509_certificate **valid_cert) |
| 283 | { |
| 284 | struct x509_certificate *cert; |
| 285 | struct efi_sig_data *sig_data; |
| 286 | bool verified = false; |
| 287 | |
| 288 | EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__, |
| 289 | regs, signed_info, siglist, valid_cert); |
| 290 | |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 291 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 292 | EFI_PRINT("Signature type is not supported: %pUl\n", |
| 293 | &siglist->sig_type); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | /* go through the list */ |
| 298 | for (sig_data = siglist->sig_data_list; sig_data; |
| 299 | sig_data = sig_data->next) { |
| 300 | /* TODO: support owner check based on policy */ |
| 301 | |
| 302 | cert = x509_cert_parse(sig_data->data, sig_data->size); |
| 303 | if (IS_ERR(cert)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 304 | EFI_PRINT("Parsing x509 certificate failed\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 305 | goto out; |
| 306 | } |
| 307 | |
| 308 | verified = efi_signature_verify(regs, msg, signed_info, cert); |
| 309 | |
| 310 | if (verified) { |
| 311 | if (valid_cert) |
| 312 | *valid_cert = cert; |
| 313 | else |
| 314 | x509_free_certificate(cert); |
| 315 | break; |
| 316 | } |
| 317 | x509_free_certificate(cert); |
| 318 | } |
| 319 | |
| 320 | out: |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 321 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 322 | return verified; |
| 323 | } |
| 324 | |
| 325 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 326 | * efi_signature_check_revocation - check revocation with dbx |
| 327 | * @sinfo: Signer's info |
| 328 | * @cert: x509 certificate |
| 329 | * @dbx: Revocation signature database |
| 330 | * |
| 331 | * Search revocation signature database pointed to by @dbx and find |
| 332 | * an entry matching to certificate pointed to by @cert. |
| 333 | * |
| 334 | * While this entry contains revocation time, we don't support timestamp |
| 335 | * protocol at this time and any image will be unconditionally revoked |
| 336 | * when this match occurs. |
| 337 | * |
| 338 | * Return: true if check passed, false otherwise. |
| 339 | */ |
| 340 | static bool efi_signature_check_revocation(struct pkcs7_signed_info *sinfo, |
| 341 | struct x509_certificate *cert, |
| 342 | struct efi_signature_store *dbx) |
| 343 | { |
| 344 | struct efi_signature_store *siglist; |
| 345 | struct efi_sig_data *sig_data; |
| 346 | struct image_region reg[1]; |
| 347 | void *hash = NULL; |
| 348 | size_t size = 0; |
| 349 | time64_t revoc_time; |
| 350 | bool revoked = false; |
| 351 | |
| 352 | EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, sinfo, cert, dbx); |
| 353 | |
| 354 | if (!sinfo || !cert || !dbx || !dbx->sig_data_list) |
| 355 | goto out; |
| 356 | |
| 357 | EFI_PRINT("Checking revocation against %s\n", cert->subject); |
| 358 | for (siglist = dbx; siglist; siglist = siglist->next) { |
| 359 | if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256)) |
| 360 | continue; |
| 361 | |
| 362 | /* calculate hash of TBSCertificate */ |
| 363 | reg[0].data = cert->tbs; |
| 364 | reg[0].size = cert->tbs_size; |
| 365 | if (!efi_hash_regions(reg, 1, &hash, &size)) |
| 366 | goto out; |
| 367 | |
| 368 | for (sig_data = siglist->sig_data_list; sig_data; |
| 369 | sig_data = sig_data->next) { |
| 370 | /* |
| 371 | * struct efi_cert_x509_sha256 { |
| 372 | * u8 tbs_hash[256/8]; |
| 373 | * time64_t revocation_time; |
| 374 | * }; |
| 375 | */ |
| 376 | #ifdef DEBUG |
| 377 | if (sig_data->size >= size) { |
| 378 | EFI_PRINT("hash in db:\n"); |
| 379 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, |
| 380 | 16, 1, |
| 381 | sig_data->data, size, false); |
| 382 | } |
| 383 | #endif |
| 384 | if ((sig_data->size < size + sizeof(time64_t)) || |
| 385 | memcmp(sig_data->data, hash, size)) |
| 386 | continue; |
| 387 | |
| 388 | memcpy(&revoc_time, sig_data->data + size, |
| 389 | sizeof(revoc_time)); |
| 390 | EFI_PRINT("revocation time: 0x%llx\n", revoc_time); |
| 391 | /* |
| 392 | * TODO: compare signing timestamp in sinfo |
| 393 | * with revocation time |
| 394 | */ |
| 395 | |
| 396 | revoked = true; |
| 397 | free(hash); |
| 398 | goto out; |
| 399 | } |
| 400 | free(hash); |
| 401 | hash = NULL; |
| 402 | } |
| 403 | out: |
| 404 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 405 | return !revoked; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * efi_signature_verify_one - verify signatures with database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 410 | * @regs: List of regions to be authenticated |
| 411 | * @msg: Signature |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 412 | * @db: Signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 413 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 414 | * All the signature pointed to by @msg against image pointed to by @regs |
| 415 | * will be verified by signature database pointed to by @db. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 416 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 417 | * Return: true if verification for one of signatures passed, false |
| 418 | * otherwise |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 419 | */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 420 | bool efi_signature_verify_one(struct efi_image_regions *regs, |
| 421 | struct pkcs7_message *msg, |
| 422 | struct efi_signature_store *db) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 423 | { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 424 | struct pkcs7_signed_info *sinfo; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 425 | struct efi_signature_store *siglist; |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 426 | struct x509_certificate *cert; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 427 | bool verified = false; |
| 428 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 429 | EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, regs, msg, db); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 430 | |
| 431 | if (!db) |
| 432 | goto out; |
| 433 | |
| 434 | if (!db->sig_data_list) |
| 435 | goto out; |
| 436 | |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 437 | EFI_PRINT("%s: Verify signed image with db\n", __func__); |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 438 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 439 | EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n", |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 440 | sinfo->sig->hash_algo, sinfo->sig->pkey_algo); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 441 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 442 | for (siglist = db; siglist; siglist = siglist->next) |
| 443 | if (efi_signature_verify_with_list(regs, msg, sinfo, |
| 444 | siglist, &cert)) { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 445 | verified = true; |
| 446 | goto out; |
| 447 | } |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 448 | EFI_PRINT("Valid certificate not in \"db\"\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | out: |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 452 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 453 | return verified; |
| 454 | } |
| 455 | |
| 456 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 457 | * efi_signature_verify_with_sigdb - verify signatures with db and dbx |
| 458 | * @regs: List of regions to be authenticated |
| 459 | * @msg: Signature |
| 460 | * @db: Signature database for trusted certificates |
| 461 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 462 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 463 | * All the signature pointed to by @msg against image pointed to by @regs |
| 464 | * will be verified by signature database pointed to by @db and @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 465 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 466 | * Return: true if verification for all signatures passed, false otherwise |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 467 | */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 468 | bool efi_signature_verify_with_sigdb(struct efi_image_regions *regs, |
| 469 | struct pkcs7_message *msg, |
| 470 | struct efi_signature_store *db, |
| 471 | struct efi_signature_store *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 | struct pkcs7_signed_info *info; |
| 474 | struct efi_signature_store *siglist; |
| 475 | struct x509_certificate *cert; |
| 476 | bool verified = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 477 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 478 | 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] | 479 | |
AKASHI Takahiro | de92407 | 2020-07-08 14:01:57 +0900 | [diff] [blame] | 480 | if (!regs || !msg || !db || !db->sig_data_list) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 481 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 482 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 483 | for (info = msg->signed_infos; info; info = info->next) { |
| 484 | EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n", |
| 485 | info->sig->hash_algo, info->sig->pkey_algo); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 486 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 487 | for (siglist = db; siglist; siglist = siglist->next) { |
| 488 | if (efi_signature_verify_with_list(regs, msg, info, |
| 489 | siglist, &cert)) |
| 490 | break; |
| 491 | } |
| 492 | if (!siglist) { |
| 493 | EFI_PRINT("Valid certificate not in \"db\"\n"); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 494 | goto out; |
| 495 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 496 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 497 | if (!dbx || efi_signature_check_revocation(info, cert, dbx)) |
| 498 | continue; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 499 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 500 | EFI_PRINT("Certificate in \"dbx\"\n"); |
| 501 | goto out; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 502 | } |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 503 | verified = true; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 504 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 505 | out: |
| 506 | EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified); |
| 507 | return verified; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 511 | * efi_signature_check_signers - check revocation against all signers with dbx |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 512 | * @msg: Signature |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 513 | * @dbx: Revocation signature database |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 514 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 515 | * Determine if none of signers' certificates in @msg are revoked |
| 516 | * by signature database pointed to by @dbx. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 517 | * |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 518 | * Return: true if all signers passed, false otherwise. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 519 | */ |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 520 | bool efi_signature_check_signers(struct pkcs7_message *msg, |
| 521 | struct efi_signature_store *dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 522 | { |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 523 | struct pkcs7_signed_info *sinfo; |
| 524 | bool revoked = false; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 525 | |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 526 | EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 527 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 528 | if (!msg || !dbx) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 529 | goto out; |
| 530 | |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 531 | for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) { |
| 532 | if (sinfo->signer && |
| 533 | !efi_signature_check_revocation(sinfo, sinfo->signer, |
| 534 | dbx)) { |
| 535 | revoked = true; |
| 536 | break; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | out: |
AKASHI Takahiro | 4154b64 | 2020-07-08 14:01:56 +0900 | [diff] [blame] | 540 | EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked); |
| 541 | return !revoked; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 545 | * efi_image_region_add() - add an entry of region |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 546 | * @regs: Pointer to array of regions |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 547 | * @start: Start address of region (included) |
| 548 | * @end: End address of region (excluded) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 549 | * @nocheck: flag against overlapped regions |
| 550 | * |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 551 | * Take one entry of region [@start, @end[ and insert it into the list. |
| 552 | * |
| 553 | * * If @nocheck is false, the list will be sorted ascending by address. |
| 554 | * Overlapping entries will not be allowed. |
| 555 | * |
| 556 | * * If @nocheck is true, the list will be sorted ascending by sequence |
| 557 | * of adding the entries. Overlapping is allowed. |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 558 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 559 | * Return: status code |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 560 | */ |
| 561 | efi_status_t efi_image_region_add(struct efi_image_regions *regs, |
| 562 | const void *start, const void *end, |
| 563 | int nocheck) |
| 564 | { |
| 565 | struct image_region *reg; |
| 566 | int i, j; |
| 567 | |
| 568 | if (regs->num >= regs->max) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 569 | EFI_PRINT("%s: no more room for regions\n", __func__); |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 570 | return EFI_OUT_OF_RESOURCES; |
| 571 | } |
| 572 | |
| 573 | if (end < start) |
| 574 | return EFI_INVALID_PARAMETER; |
| 575 | |
| 576 | for (i = 0; i < regs->num; i++) { |
| 577 | reg = ®s->reg[i]; |
| 578 | if (nocheck) |
| 579 | continue; |
| 580 | |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 581 | /* new data after registered region */ |
| 582 | if (start >= reg->data + reg->size) |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 583 | continue; |
| 584 | |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 585 | /* new data preceding registered region */ |
| 586 | if (end <= reg->data) { |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 587 | for (j = regs->num - 1; j >= i; j--) |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 588 | memcpy(®s->reg[j + 1], ®s->reg[j], |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 589 | sizeof(*reg)); |
| 590 | break; |
| 591 | } |
Heinrich Schuchardt | 623e5e5 | 2020-07-01 20:01:52 +0200 | [diff] [blame] | 592 | |
| 593 | /* new data overlapping registered region */ |
| 594 | EFI_PRINT("%s: new region already part of another\n", __func__); |
| 595 | return EFI_INVALID_PARAMETER; |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | reg = ®s->reg[i]; |
| 599 | reg->data = start; |
| 600 | reg->size = end - start; |
| 601 | regs->num++; |
| 602 | |
| 603 | return EFI_SUCCESS; |
| 604 | } |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 605 | |
| 606 | /** |
| 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 | /** |
| 742 | * efi_sigstore_parse_sigdb - parse a signature database variable |
| 743 | * @name: Variable's name |
| 744 | * |
| 745 | * Read in a value of signature database variable pointed to by |
| 746 | * @name, parse it and instantiate a signature store structure. |
| 747 | * |
| 748 | * Return: Pointer to signature store on success, NULL on error |
| 749 | */ |
| 750 | struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name) |
| 751 | { |
| 752 | struct efi_signature_store *sigstore = NULL, *siglist; |
| 753 | struct efi_signature_list *esl; |
| 754 | const efi_guid_t *vendor; |
| 755 | void *db; |
| 756 | efi_uintn_t db_size; |
| 757 | efi_status_t ret; |
| 758 | |
| 759 | if (!u16_strcmp(name, L"PK") || !u16_strcmp(name, L"KEK")) { |
| 760 | vendor = &efi_global_variable_guid; |
| 761 | } else if (!u16_strcmp(name, L"db") || !u16_strcmp(name, L"dbx")) { |
| 762 | vendor = &efi_guid_image_security_database; |
| 763 | } else { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 764 | EFI_PRINT("unknown signature database, %ls\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | /* retrieve variable data */ |
| 769 | db_size = 0; |
| 770 | ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, NULL)); |
| 771 | if (ret == EFI_NOT_FOUND) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 772 | EFI_PRINT("variable, %ls, not found\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 773 | sigstore = calloc(sizeof(*sigstore), 1); |
| 774 | return sigstore; |
| 775 | } else if (ret != EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 776 | EFI_PRINT("Getting variable, %ls, failed\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | db = malloc(db_size); |
| 781 | if (!db) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 782 | EFI_PRINT("Out of memory\n"); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
| 785 | |
| 786 | ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, db)); |
| 787 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 788 | EFI_PRINT("Getting variable, %ls, failed\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 789 | goto err; |
| 790 | } |
| 791 | |
| 792 | /* Parse siglist list */ |
| 793 | esl = db; |
| 794 | while (db_size > 0) { |
| 795 | /* List must exist if there is remaining data. */ |
| 796 | if (db_size < sizeof(*esl)) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 797 | EFI_PRINT("variable, %ls, in wrong format\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 798 | goto err; |
| 799 | } |
| 800 | |
| 801 | if (db_size < esl->signature_list_size) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 802 | EFI_PRINT("variable, %ls, in wrong format\n", name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 803 | goto err; |
| 804 | } |
| 805 | |
| 806 | /* Parse a single siglist. */ |
| 807 | siglist = efi_sigstore_parse_siglist(esl); |
| 808 | if (!siglist) { |
AKASHI Takahiro | 10a9fa8 | 2020-06-09 14:09:33 +0900 | [diff] [blame] | 809 | EFI_PRINT("Parsing signature list of %ls failed\n", |
| 810 | name); |
AKASHI Takahiro | d2cefc8 | 2020-04-14 11:51:40 +0900 | [diff] [blame] | 811 | goto err; |
| 812 | } |
| 813 | |
| 814 | /* Append siglist */ |
| 815 | siglist->next = sigstore; |
| 816 | sigstore = siglist; |
| 817 | |
| 818 | /* Next */ |
| 819 | db_size -= esl->signature_list_size; |
| 820 | esl = (void *)esl + esl->signature_list_size; |
| 821 | } |
| 822 | free(db); |
| 823 | |
| 824 | return sigstore; |
| 825 | |
| 826 | err: |
| 827 | efi_sigstore_free(sigstore); |
| 828 | free(db); |
| 829 | |
| 830 | return NULL; |
| 831 | } |
AKASHI Takahiro | 1faaca4 | 2020-04-14 11:51:39 +0900 | [diff] [blame] | 832 | #endif /* CONFIG_EFI_SECURE_BOOT */ |