18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <linux/kernel.h>
48c2ecf20Sopenharmony_ci#include <linux/sched.h>
58c2ecf20Sopenharmony_ci#include <linux/cred.h>
68c2ecf20Sopenharmony_ci#include <linux/err.h>
78c2ecf20Sopenharmony_ci#include <linux/efi.h>
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <keys/asymmetric-type.h>
108c2ecf20Sopenharmony_ci#include <keys/system_keyring.h>
118c2ecf20Sopenharmony_ci#include "../integrity.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic efi_guid_t efi_cert_x509_guid __initdata = EFI_CERT_X509_GUID;
148c2ecf20Sopenharmony_cistatic efi_guid_t efi_cert_x509_sha256_guid __initdata =
158c2ecf20Sopenharmony_ci	EFI_CERT_X509_SHA256_GUID;
168c2ecf20Sopenharmony_cistatic efi_guid_t efi_cert_sha256_guid __initdata = EFI_CERT_SHA256_GUID;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Blacklist a hash.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistatic __init void uefi_blacklist_hash(const char *source, const void *data,
228c2ecf20Sopenharmony_ci				       size_t len, const char *type,
238c2ecf20Sopenharmony_ci				       size_t type_len)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	char *hash, *p;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	hash = kmalloc(type_len + len * 2 + 1, GFP_KERNEL);
288c2ecf20Sopenharmony_ci	if (!hash)
298c2ecf20Sopenharmony_ci		return;
308c2ecf20Sopenharmony_ci	p = memcpy(hash, type, type_len);
318c2ecf20Sopenharmony_ci	p += type_len;
328c2ecf20Sopenharmony_ci	bin2hex(p, data, len);
338c2ecf20Sopenharmony_ci	p += len * 2;
348c2ecf20Sopenharmony_ci	*p = 0;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	mark_hash_blacklisted(hash);
378c2ecf20Sopenharmony_ci	kfree(hash);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Blacklist an X509 TBS hash.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic __init void uefi_blacklist_x509_tbs(const char *source,
448c2ecf20Sopenharmony_ci					   const void *data, size_t len)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	uefi_blacklist_hash(source, data, len, "tbs:", 4);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Blacklist the hash of an executable.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic __init void uefi_blacklist_binary(const char *source,
538c2ecf20Sopenharmony_ci					 const void *data, size_t len)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	uefi_blacklist_hash(source, data, len, "bin:", 4);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Add an X509 cert to the revocation list.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistatic __init void uefi_revocation_list_x509(const char *source,
628c2ecf20Sopenharmony_ci					     const void *data, size_t len)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	add_key_to_revocation_list(data, len);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Return the appropriate handler for particular signature list types found in
698c2ecf20Sopenharmony_ci * the UEFI db and MokListRT tables.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci__init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
748c2ecf20Sopenharmony_ci		return add_to_platform_keyring;
758c2ecf20Sopenharmony_ci	return 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * Return the appropriate handler for particular signature list types found in
808c2ecf20Sopenharmony_ci * the UEFI dbx and MokListXRT tables.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci__init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
858c2ecf20Sopenharmony_ci		return uefi_blacklist_x509_tbs;
868c2ecf20Sopenharmony_ci	if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
878c2ecf20Sopenharmony_ci		return uefi_blacklist_binary;
888c2ecf20Sopenharmony_ci	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
898c2ecf20Sopenharmony_ci		return uefi_revocation_list_x509;
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
92