1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (c) 2019 Linaro Limited, Author: AKASHI Takahiro
9 #include <efi_loader.h>
13 #include <crypto/pkcs7_parser.h>
14 #include <linux/compat.h>
15 #include <linux/oid_registry.h>
16 #include <u-boot/rsa.h>
17 #include <u-boot/sha256.h>
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;
26 #ifdef CONFIG_EFI_SECURE_BOOT
29 * efi_hash_regions - calculate a hash value
30 * @regs: List of regions
31 * @hash: Pointer to a pointer to buffer holding a hash value
32 * @size: Size of buffer to be returned
34 * Calculate a sha256 value of @regs and return a value in @hash.
36 * Return: true on success, false on error
38 static bool efi_hash_regions(struct efi_image_regions *regs, void **hash,
42 *hash = calloc(1, SHA256_SUM_LEN);
44 debug("Out of memory\n");
47 *size = SHA256_SUM_LEN;
49 hash_calculate("sha256", regs->reg, regs->num, *hash);
51 debug("hash calculated:\n");
52 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
53 *hash, SHA256_SUM_LEN, false);
60 * efi_hash_msg_content - calculate a hash value of contentInfo
62 * @hash: Pointer to a pointer to buffer holding a hash value
63 * @size: Size of buffer to be returned
65 * Calculate a sha256 value of contentInfo in @msg and return a value in @hash.
67 * Return: true on success, false on error
69 static bool efi_hash_msg_content(struct pkcs7_message *msg, void **hash,
72 struct image_region regtmp;
75 *hash = calloc(1, SHA256_SUM_LEN);
77 debug("Out of memory\n");
81 *size = SHA256_SUM_LEN;
83 regtmp.data = msg->data;
84 regtmp.size = msg->data_len;
86 hash_calculate("sha256", ®tmp, 1, *hash);
88 debug("hash calculated based on contentInfo:\n");
89 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
90 *hash, SHA256_SUM_LEN, false);
97 * efi_signature_verify - verify a signature with a certificate
98 * @regs: List of regions to be authenticated
99 * @signed_info: Pointer to PKCS7's signed_info
100 * @cert: x509 certificate
102 * Signature pointed to by @signed_info against image pointed to by @regs
103 * is verified by a certificate pointed to by @cert.
104 * @signed_info holds a signature, including a message digest which is to be
105 * compared with a hash value calculated from @regs.
107 * Return: true if signature is verified, false if not
109 static bool efi_signature_verify(struct efi_image_regions *regs,
110 struct pkcs7_message *msg,
111 struct pkcs7_signed_info *ps_info,
112 struct x509_certificate *cert)
114 struct image_sign_info info;
115 struct image_region regtmp[2];
121 debug("%s: Enter, %p, %p, %p(issuer: %s, subject: %s)\n", __func__,
122 regs, ps_info, cert, cert->issuer, cert->subject);
126 memset(&info, '\0', sizeof(info));
127 info.padding = image_get_padding_algo("pkcs-1.5");
129 * Note: image_get_[checksum|crypto]_algo takes an string
130 * argument like "<checksum>,<crypto>"
131 * TODO: support other hash algorithms
133 if (!strcmp(ps_info->sig->hash_algo, "sha1")) {
134 info.checksum = image_get_checksum_algo("sha1,rsa2048");
135 info.name = "sha1,rsa2048";
136 } else if (!strcmp(ps_info->sig->hash_algo, "sha256")) {
137 info.checksum = image_get_checksum_algo("sha256,rsa2048");
138 info.name = "sha256,rsa2048";
140 debug("unknown msg digest algo: %s\n", ps_info->sig->hash_algo);
143 info.crypto = image_get_crypto_algo(info.name);
145 info.key = cert->pub->key;
146 info.keylen = cert->pub->keylen;
148 /* verify signature */
149 debug("%s: crypto: %s, signature len:%x\n", __func__,
150 info.name, ps_info->sig->s_size);
151 if (ps_info->aa_set & (1UL << sinfo_has_message_digest)) {
152 debug("%s: RSA verify authentication attribute\n", __func__);
154 * NOTE: This path will be executed only for
155 * PE image authentication
158 /* check if hash matches digest first */
159 debug("checking msg digest first, len:0x%x\n",
160 ps_info->msgdigest_len);
163 debug("hash in database:\n");
164 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
165 ps_info->msgdigest, ps_info->msgdigest_len,
168 /* against contentInfo first */
169 if ((msg->data && efi_hash_msg_content(msg, &hash, &size)) ||
170 /* for signed image */
171 efi_hash_regions(regs, &hash, &size)) {
172 /* for authenticated variable */
173 if (ps_info->msgdigest_len != size ||
174 memcmp(hash, ps_info->msgdigest, size)) {
175 debug("Digest doesn't match\n");
182 debug("Digesting image failed\n");
190 regtmp[1].data = ps_info->authattrs;
191 regtmp[1].size = ps_info->authattrs_len;
193 if (!rsa_verify(&info, regtmp, 2,
194 ps_info->sig->s, ps_info->sig->s_size))
197 debug("%s: RSA verify content data\n", __func__);
198 /* against all data */
199 if (!rsa_verify(&info, regs->reg, regs->num,
200 ps_info->sig->s, ps_info->sig->s_size))
205 debug("%s: Exit, verified: %d\n", __func__, verified);
210 * efi_signature_verify_with_list - verify a signature with signature list
211 * @regs: List of regions to be authenticated
213 * @signed_info: Pointer to PKCS7's signed_info
214 * @siglist: Signature list for certificates
215 * @valid_cert: x509 certificate that verifies this signature
217 * Signature pointed to by @signed_info against image pointed to by @regs
218 * is verified by signature list pointed to by @siglist.
219 * Signature database is a simple concatenation of one or more
222 * Return: true if signature is verified, false if not
225 bool efi_signature_verify_with_list(struct efi_image_regions *regs,
226 struct pkcs7_message *msg,
227 struct pkcs7_signed_info *signed_info,
228 struct efi_signature_store *siglist,
229 struct x509_certificate **valid_cert)
231 struct x509_certificate *cert;
232 struct efi_sig_data *sig_data;
233 bool verified = false;
235 debug("%s: Enter, %p, %p, %p, %p\n", __func__,
236 regs, signed_info, siglist, valid_cert);
242 debug("%s: unsigned image\n", __func__);
244 * verify based on calculated hash value
245 * TODO: support other hash algorithms
247 if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) {
248 debug("Digest algorithm is not supported: %pUl\n",
253 if (!efi_hash_regions(regs, &hash, &size)) {
254 debug("Digesting unsigned image failed\n");
258 /* go through the list */
259 for (sig_data = siglist->sig_data_list; sig_data;
260 sig_data = sig_data->next) {
262 debug("Msg digest in database:\n");
263 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
264 sig_data->data, sig_data->size, false);
266 if ((sig_data->size == size) &&
267 !memcmp(sig_data->data, hash, size)) {
277 debug("%s: signed image\n", __func__);
278 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) {
279 debug("Signature type is not supported: %pUl\n",
284 /* go through the list */
285 for (sig_data = siglist->sig_data_list; sig_data;
286 sig_data = sig_data->next) {
287 /* TODO: support owner check based on policy */
289 cert = x509_cert_parse(sig_data->data, sig_data->size);
291 debug("Parsing x509 certificate failed\n");
295 verified = efi_signature_verify(regs, msg, signed_info, cert);
301 x509_free_certificate(cert);
304 x509_free_certificate(cert);
308 debug("%s: Exit, verified: %d\n", __func__, verified);
313 * efi_signature_verify_with_sigdb - verify a signature with db
314 * @regs: List of regions to be authenticated
316 * @db: Signature database for trusted certificates
317 * @cert: x509 certificate that verifies this signature
319 * Signature pointed to by @msg against image pointed to by @regs
320 * is verified by signature database pointed to by @db.
322 * Return: true if signature is verified, false if not
324 bool efi_signature_verify_with_sigdb(struct efi_image_regions *regs,
325 struct pkcs7_message *msg,
326 struct efi_signature_store *db,
327 struct x509_certificate **cert)
329 struct pkcs7_signed_info *info;
330 struct efi_signature_store *siglist;
331 bool verified = false;
333 debug("%s: Enter, %p, %p, %p, %p\n", __func__, regs, msg, db, cert);
338 if (!db->sig_data_list)
341 /* for unsigned image */
343 debug("%s: Verify unsigned image with db\n", __func__);
344 for (siglist = db; siglist; siglist = siglist->next)
345 if (efi_signature_verify_with_list(regs, NULL, NULL,
354 /* for signed image or variable */
355 debug("%s: Verify signed image with db\n", __func__);
356 for (info = msg->signed_infos; info; info = info->next) {
357 debug("Signed Info: digest algo: %s, pkey algo: %s\n",
358 info->sig->hash_algo, info->sig->pkey_algo);
360 for (siglist = db; siglist; siglist = siglist->next) {
361 if (efi_signature_verify_with_list(regs, msg, info,
370 debug("%s: Exit, verified: %d\n", __func__, verified);
375 * efi_search_siglist - search signature list for a certificate
376 * @cert: x509 certificate
377 * @siglist: Signature list
378 * @revoc_time: Pointer to buffer for revocation time
380 * Search signature list pointed to by @siglist and find a certificate
381 * pointed to by @cert.
382 * If found, revocation time that is specified in signature database is
383 * returned in @revoc_time.
385 * Return: true if certificate is found, false if not
387 static bool efi_search_siglist(struct x509_certificate *cert,
388 struct efi_signature_store *siglist,
389 time64_t *revoc_time)
391 struct image_region reg[1];
392 void *hash = NULL, *msg = NULL;
393 struct efi_sig_data *sig_data;
397 if (!siglist->sig_data_list)
400 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256)) {
401 /* TODO: other hash algos */
402 debug("Certificate's digest type is not supported: %pUl\n",
407 /* calculate hash of TBSCertificate */
408 msg = calloc(1, SHA256_SUM_LEN);
410 debug("Out of memory\n");
414 hash = calloc(1, SHA256_SUM_LEN);
416 debug("Out of memory\n");
420 reg[0].data = cert->tbs;
421 reg[0].size = cert->tbs_size;
422 hash_calculate("sha256", reg, 1, msg);
424 /* go through signature list */
425 for (sig_data = siglist->sig_data_list; sig_data;
426 sig_data = sig_data->next) {
428 * struct efi_cert_x509_sha256 {
429 * u8 tbs_hash[256/8];
430 * time64_t revocation_time;
433 if ((sig_data->size == SHA256_SUM_LEN) &&
434 !memcmp(sig_data->data, hash, SHA256_SUM_LEN)) {
435 memcpy(revoc_time, sig_data->data + SHA256_SUM_LEN,
436 sizeof(*revoc_time));
450 * efi_signature_verify_cert - verify a certificate with dbx
451 * @cert: x509 certificate
452 * @dbx: Signature database
454 * Search signature database pointed to by @dbx and find a certificate
455 * pointed to by @cert.
456 * This function is expected to be used against "dbx".
458 * Return: true if a certificate is not rejected, false otherwise.
460 bool efi_signature_verify_cert(struct x509_certificate *cert,
461 struct efi_signature_store *dbx)
463 struct efi_signature_store *siglist;
467 debug("%s: Enter, %p, %p\n", __func__, dbx, cert);
472 for (siglist = dbx; siglist; siglist = siglist->next) {
473 if (efi_search_siglist(cert, siglist, &revoc_time)) {
475 /* compare signing time with revocation time */
482 debug("%s: Exit, verified: %d\n", __func__, !found);
487 * efi_signature_verify_signers - verify signers' certificates with dbx
489 * @dbx: Signature database
491 * Determine if any of signers' certificates in @msg may be verified
492 * by any of certificates in signature database pointed to by @dbx.
493 * This function is expected to be used against "dbx".
495 * Return: true if none of certificates is rejected, false otherwise.
497 bool efi_signature_verify_signers(struct pkcs7_message *msg,
498 struct efi_signature_store *dbx)
500 struct pkcs7_signed_info *info;
503 debug("%s: Enter, %p, %p\n", __func__, msg, dbx);
508 for (info = msg->signed_infos; info; info = info->next) {
510 !efi_signature_verify_cert(info->signer, dbx)) {
516 debug("%s: Exit, verified: %d\n", __func__, !found);
521 * efi_image_region_add - add an entry of region
522 * @regs: Pointer to array of regions
523 * @start: Start address of region
524 * @end: End address of region
525 * @nocheck: flag against overlapped regions
527 * Take one entry of region [@start, @end] and append it to the list
528 * pointed to by @regs. If @nocheck is false, overlapping among entries
529 * will be checked first.
531 * Return: status code
533 efi_status_t efi_image_region_add(struct efi_image_regions *regs,
534 const void *start, const void *end,
537 struct image_region *reg;
540 if (regs->num >= regs->max) {
541 debug("%s: no more room for regions\n", __func__);
542 return EFI_OUT_OF_RESOURCES;
546 return EFI_INVALID_PARAMETER;
548 for (i = 0; i < regs->num; i++) {
553 if (start > reg->data + reg->size)
556 if ((start >= reg->data && start < reg->data + reg->size) ||
557 (end > reg->data && end < reg->data + reg->size)) {
558 debug("%s: new region already part of another\n",
560 return EFI_INVALID_PARAMETER;
563 if (start < reg->data && end < reg->data + reg->size) {
564 for (j = regs->num - 1; j >= i; j--)
565 memcpy(®s->reg[j], ®s->reg[j + 1],
573 reg->size = end - start;
580 * efi_sigstore_free - free signature store
581 * @sigstore: Pointer to signature store structure
583 * Feee all the memories held in signature store and itself,
584 * which were allocated by efi_sigstore_parse_sigdb().
586 void efi_sigstore_free(struct efi_signature_store *sigstore)
588 struct efi_signature_store *sigstore_next;
589 struct efi_sig_data *sig_data, *sig_data_next;
592 sigstore_next = sigstore->next;
594 sig_data = sigstore->sig_data_list;
596 sig_data_next = sig_data->next;
597 free(sig_data->data);
599 sig_data = sig_data_next;
603 sigstore = sigstore_next;
608 * efi_sigstore_parse_siglist - parse a signature list
609 * @name: Pointer to signature list
611 * Parse signature list and instantiate a signature store structure.
612 * Signature database is a simple concatenation of one or more
615 * Return: Pointer to signature store on success, NULL on error
617 static struct efi_signature_store *
618 efi_sigstore_parse_siglist(struct efi_signature_list *esl)
620 struct efi_signature_store *siglist = NULL;
621 struct efi_sig_data *sig_data, *sig_data_next;
622 struct efi_signature_data *esd;
626 * UEFI specification defines certificate types:
627 * for non-signed images,
628 * EFI_CERT_SHA256_GUID
629 * EFI_CERT_RSA2048_GUID
630 * EFI_CERT_RSA2048_SHA256_GUID
632 * EFI_CERT_RSA2048_SHA_GUID
633 * EFI_CERT_SHA224_GUID
634 * EFI_CERT_SHA384_GUID
635 * EFI_CERT_SHA512_GUID
639 * NOTE: Each certificate will normally be in a separate
640 * EFI_SIGNATURE_LIST as the size may vary depending on
643 * for timestamp revocation of certificate,
644 * EFI_CERT_X509_SHA512_GUID
645 * EFI_CERT_X509_SHA256_GUID
646 * EFI_CERT_X509_SHA384_GUID
649 if (esl->signature_list_size
650 <= (sizeof(*esl) + esl->signature_header_size)) {
651 debug("Siglist in wrong format\n");
656 siglist = calloc(sizeof(*siglist), 1);
658 debug("Out of memory\n");
661 memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t));
663 /* Go through the list */
664 sig_data_next = NULL;
665 left = esl->signature_list_size
666 - (sizeof(*esl) + esl->signature_header_size);
667 esd = (struct efi_signature_data *)
668 ((u8 *)esl + sizeof(*esl) + esl->signature_header_size);
671 /* Signature must exist if there is remaining data. */
672 if (left < esl->signature_size) {
673 debug("Certificate is too small\n");
677 sig_data = calloc(esl->signature_size
678 - sizeof(esd->signature_owner), 1);
680 debug("Out of memory\n");
684 /* Append signature data */
685 memcpy(&sig_data->owner, &esd->signature_owner,
687 sig_data->size = esl->signature_size
688 - sizeof(esd->signature_owner);
689 sig_data->data = malloc(sig_data->size);
690 if (!sig_data->data) {
691 debug("Out of memory\n");
694 memcpy(sig_data->data, esd->signature_data, sig_data->size);
696 sig_data->next = sig_data_next;
697 sig_data_next = sig_data;
700 esd = (struct efi_signature_data *)
701 ((u8 *)esd + esl->signature_size);
702 left -= esl->signature_size;
704 siglist->sig_data_list = sig_data_next;
709 efi_sigstore_free(siglist);
715 * efi_sigstore_parse_sigdb - parse a signature database variable
716 * @name: Variable's name
718 * Read in a value of signature database variable pointed to by
719 * @name, parse it and instantiate a signature store structure.
721 * Return: Pointer to signature store on success, NULL on error
723 struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
725 struct efi_signature_store *sigstore = NULL, *siglist;
726 struct efi_signature_list *esl;
727 const efi_guid_t *vendor;
732 if (!u16_strcmp(name, L"PK") || !u16_strcmp(name, L"KEK")) {
733 vendor = &efi_global_variable_guid;
734 } else if (!u16_strcmp(name, L"db") || !u16_strcmp(name, L"dbx")) {
735 vendor = &efi_guid_image_security_database;
737 debug("unknown signature database, %ls\n", name);
741 /* retrieve variable data */
743 ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, NULL));
744 if (ret == EFI_NOT_FOUND) {
745 debug("variable, %ls, not found\n", name);
746 sigstore = calloc(sizeof(*sigstore), 1);
748 } else if (ret != EFI_BUFFER_TOO_SMALL) {
749 debug("Getting variable, %ls, failed\n", name);
753 db = malloc(db_size);
755 debug("Out of memory\n");
759 ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, db));
760 if (ret != EFI_SUCCESS) {
761 debug("Getting variable, %ls, failed\n", name);
765 /* Parse siglist list */
767 while (db_size > 0) {
768 /* List must exist if there is remaining data. */
769 if (db_size < sizeof(*esl)) {
770 debug("variable, %ls, in wrong format\n", name);
774 if (db_size < esl->signature_list_size) {
775 debug("variable, %ls, in wrong format\n", name);
779 /* Parse a single siglist. */
780 siglist = efi_sigstore_parse_siglist(esl);
782 debug("Parsing signature list of %ls failed\n", name);
787 siglist->next = sigstore;
791 db_size -= esl->signature_list_size;
792 esl = (void *)esl + esl->signature_list_size;
799 efi_sigstore_free(sigstore);
804 #endif /* CONFIG_EFI_SECURE_BOOT */