pkcs7_key_type.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Testing module to load key from trusted PKCS#7 message
  3. *
  4. * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "PKCS7key: "fmt
  8. #include <linux/key.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/verification.h>
  12. #include <linux/key-type.h>
  13. #include <keys/user-type.h>
  14. MODULE_LICENSE("GPL");
  15. MODULE_DESCRIPTION("PKCS#7 testing key type");
  16. MODULE_AUTHOR("Red Hat, Inc.");
  17. static unsigned pkcs7_usage;
  18. module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
  19. MODULE_PARM_DESC(pkcs7_usage,
  20. "Usage to specify when verifying the PKCS#7 message");
  21. /*
  22. * Retrieve the PKCS#7 message content.
  23. */
  24. static int pkcs7_view_content(void *ctx, const void *data, size_t len,
  25. size_t asn1hdrlen)
  26. {
  27. struct key_preparsed_payload *prep = ctx;
  28. const void *saved_prep_data;
  29. size_t saved_prep_datalen;
  30. int ret;
  31. saved_prep_data = prep->data;
  32. saved_prep_datalen = prep->datalen;
  33. prep->data = data;
  34. prep->datalen = len;
  35. ret = user_preparse(prep);
  36. prep->data = saved_prep_data;
  37. prep->datalen = saved_prep_datalen;
  38. return ret;
  39. }
  40. /*
  41. * Preparse a PKCS#7 wrapped and validated data blob.
  42. */
  43. static int pkcs7_preparse(struct key_preparsed_payload *prep)
  44. {
  45. enum key_being_used_for usage = pkcs7_usage;
  46. if (usage >= NR__KEY_BEING_USED_FOR) {
  47. pr_err("Invalid usage type %d\n", usage);
  48. return -EINVAL;
  49. }
  50. return verify_pkcs7_signature(NULL, 0,
  51. prep->data, prep->datalen,
  52. VERIFY_USE_SECONDARY_KEYRING, usage,
  53. pkcs7_view_content, prep);
  54. }
  55. /*
  56. * user defined keys take an arbitrary string as the description and an
  57. * arbitrary blob of data as the payload
  58. */
  59. static struct key_type key_type_pkcs7 = {
  60. .name = "pkcs7_test",
  61. .preparse = pkcs7_preparse,
  62. .free_preparse = user_free_preparse,
  63. .instantiate = generic_key_instantiate,
  64. .revoke = user_revoke,
  65. .destroy = user_destroy,
  66. .describe = user_describe,
  67. .read = user_read,
  68. };
  69. /*
  70. * Module stuff
  71. */
  72. static int __init pkcs7_key_init(void)
  73. {
  74. return register_key_type(&key_type_pkcs7);
  75. }
  76. static void __exit pkcs7_key_cleanup(void)
  77. {
  78. unregister_key_type(&key_type_pkcs7);
  79. }
  80. module_init(pkcs7_key_init);
  81. module_exit(pkcs7_key_cleanup);