pkcs7_key_type.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Testing module to load key from trusted PKCS#7 message
  2. *
  3. * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "PKCS7key: "fmt
  12. #include <linux/key.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/key-type.h>
  16. #include <keys/asymmetric-type.h>
  17. #include <crypto/pkcs7.h>
  18. #include <keys/user-type.h>
  19. #include <keys/system_keyring.h>
  20. #include "pkcs7_parser.h"
  21. MODULE_LICENSE("GPL");
  22. MODULE_DESCRIPTION("PKCS#7 testing key type");
  23. static unsigned pkcs7_usage;
  24. module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
  25. MODULE_PARM_DESC(pkcs7_usage,
  26. "Usage to specify when verifying the PKCS#7 message");
  27. /*
  28. * Preparse a PKCS#7 wrapped and validated data blob.
  29. */
  30. static int pkcs7_preparse(struct key_preparsed_payload *prep)
  31. {
  32. enum key_being_used_for usage = pkcs7_usage;
  33. struct pkcs7_message *pkcs7;
  34. const void *data, *saved_prep_data;
  35. size_t datalen, saved_prep_datalen;
  36. bool trusted;
  37. int ret;
  38. kenter("");
  39. if (usage >= NR__KEY_BEING_USED_FOR) {
  40. pr_err("Invalid usage type %d\n", usage);
  41. return -EINVAL;
  42. }
  43. saved_prep_data = prep->data;
  44. saved_prep_datalen = prep->datalen;
  45. pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen);
  46. if (IS_ERR(pkcs7)) {
  47. ret = PTR_ERR(pkcs7);
  48. goto error;
  49. }
  50. ret = pkcs7_verify(pkcs7, usage);
  51. if (ret < 0)
  52. goto error_free;
  53. ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
  54. if (ret < 0)
  55. goto error_free;
  56. if (!trusted)
  57. pr_warn("PKCS#7 message doesn't chain back to a trusted key\n");
  58. ret = pkcs7_get_content_data(pkcs7, &data, &datalen, false);
  59. if (ret < 0)
  60. goto error_free;
  61. prep->data = data;
  62. prep->datalen = datalen;
  63. ret = user_preparse(prep);
  64. prep->data = saved_prep_data;
  65. prep->datalen = saved_prep_datalen;
  66. error_free:
  67. pkcs7_free_message(pkcs7);
  68. error:
  69. kleave(" = %d", ret);
  70. return ret;
  71. }
  72. /*
  73. * user defined keys take an arbitrary string as the description and an
  74. * arbitrary blob of data as the payload
  75. */
  76. static struct key_type key_type_pkcs7 = {
  77. .name = "pkcs7_test",
  78. .preparse = pkcs7_preparse,
  79. .free_preparse = user_free_preparse,
  80. .instantiate = generic_key_instantiate,
  81. .revoke = user_revoke,
  82. .destroy = user_destroy,
  83. .describe = user_describe,
  84. .read = user_read,
  85. };
  86. /*
  87. * Module stuff
  88. */
  89. static int __init pkcs7_key_init(void)
  90. {
  91. return register_key_type(&key_type_pkcs7);
  92. }
  93. static void __exit pkcs7_key_cleanup(void)
  94. {
  95. unregister_key_type(&key_type_pkcs7);
  96. }
  97. module_init(pkcs7_key_init);
  98. module_exit(pkcs7_key_cleanup);