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