keyring_handler.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/sched.h>
  4. #include <linux/cred.h>
  5. #include <linux/err.h>
  6. #include <linux/efi.h>
  7. #include <linux/slab.h>
  8. #include <keys/asymmetric-type.h>
  9. #include <keys/system_keyring.h>
  10. #include "../integrity.h"
  11. #include "keyring_handler.h"
  12. static efi_guid_t efi_cert_x509_guid __initdata = EFI_CERT_X509_GUID;
  13. static efi_guid_t efi_cert_x509_sha256_guid __initdata =
  14. EFI_CERT_X509_SHA256_GUID;
  15. static efi_guid_t efi_cert_sha256_guid __initdata = EFI_CERT_SHA256_GUID;
  16. /*
  17. * Blacklist an X509 TBS hash.
  18. */
  19. static __init void uefi_blacklist_x509_tbs(const char *source,
  20. const void *data, size_t len)
  21. {
  22. mark_hash_blacklisted(data, len, BLACKLIST_HASH_X509_TBS);
  23. }
  24. /*
  25. * Blacklist the hash of an executable.
  26. */
  27. static __init void uefi_blacklist_binary(const char *source,
  28. const void *data, size_t len)
  29. {
  30. mark_hash_blacklisted(data, len, BLACKLIST_HASH_BINARY);
  31. }
  32. /*
  33. * Add an X509 cert to the revocation list.
  34. */
  35. static __init void uefi_revocation_list_x509(const char *source,
  36. const void *data, size_t len)
  37. {
  38. add_key_to_revocation_list(data, len);
  39. }
  40. /*
  41. * Return the appropriate handler for particular signature list types found in
  42. * the UEFI db tables.
  43. */
  44. __init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
  45. {
  46. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
  47. return add_to_platform_keyring;
  48. return NULL;
  49. }
  50. /*
  51. * Return the appropriate handler for particular signature list types found in
  52. * the MokListRT tables.
  53. */
  54. __init efi_element_handler_t get_handler_for_mok(const efi_guid_t *sig_type)
  55. {
  56. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0) {
  57. if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING) &&
  58. imputed_trust_enabled())
  59. return add_to_machine_keyring;
  60. else
  61. return add_to_platform_keyring;
  62. }
  63. return NULL;
  64. }
  65. __init efi_element_handler_t get_handler_for_ca_keys(const efi_guid_t *sig_type)
  66. {
  67. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
  68. return add_to_machine_keyring;
  69. return NULL;
  70. }
  71. __init efi_element_handler_t get_handler_for_code_signing_keys(const efi_guid_t *sig_type)
  72. {
  73. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
  74. return add_to_secondary_keyring;
  75. return NULL;
  76. }
  77. /*
  78. * Return the appropriate handler for particular signature list types found in
  79. * the UEFI dbx and MokListXRT tables.
  80. */
  81. __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
  82. {
  83. if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
  84. return uefi_blacklist_x509_tbs;
  85. if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
  86. return uefi_blacklist_binary;
  87. if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
  88. return uefi_revocation_list_x509;
  89. return NULL;
  90. }