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