David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 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 | |
| 12 | #define pr_fmt(fmt) "PKCS7key: "fmt |
| 13 | #include <linux/key.h> |
David Howells | 8f3438c | 2014-07-25 11:33:53 +0100 | [diff] [blame] | 14 | #include <linux/err.h> |
Paul Gortmaker | 8877558 | 2015-05-01 21:29:53 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 16 | #include <linux/verification.h> |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 17 | #include <linux/key-type.h> |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 18 | #include <keys/user-type.h> |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 19 | |
David Howells | 772111a | 2015-08-13 02:51:33 +0100 | [diff] [blame] | 20 | MODULE_LICENSE("GPL"); |
| 21 | MODULE_DESCRIPTION("PKCS#7 testing key type"); |
| 22 | |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 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 | |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 28 | /* |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 29 | * Retrieve the PKCS#7 message content. |
| 30 | */ |
| 31 | static int pkcs7_view_content(void *ctx, const void *data, size_t len, |
| 32 | size_t asn1hdrlen) |
| 33 | { |
| 34 | struct key_preparsed_payload *prep = ctx; |
| 35 | const void *saved_prep_data; |
| 36 | size_t saved_prep_datalen; |
| 37 | int ret; |
| 38 | |
| 39 | saved_prep_data = prep->data; |
| 40 | saved_prep_datalen = prep->datalen; |
| 41 | prep->data = data; |
| 42 | prep->datalen = len; |
| 43 | |
| 44 | ret = user_preparse(prep); |
| 45 | |
| 46 | prep->data = saved_prep_data; |
| 47 | prep->datalen = saved_prep_datalen; |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | /* |
David Howells | 1ca72c9 | 2014-07-22 21:52:33 +0100 | [diff] [blame] | 52 | * Preparse a PKCS#7 wrapped and validated data blob. |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 53 | */ |
David Howells | 1ca72c9 | 2014-07-22 21:52:33 +0100 | [diff] [blame] | 54 | static int pkcs7_preparse(struct key_preparsed_payload *prep) |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 55 | { |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 56 | enum key_being_used_for usage = pkcs7_usage; |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 57 | |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 58 | if (usage >= NR__KEY_BEING_USED_FOR) { |
| 59 | pr_err("Invalid usage type %d\n", usage); |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 63 | return verify_pkcs7_signature(NULL, 0, |
| 64 | prep->data, prep->datalen, |
Yannik Sembritzki | 40b08cd | 2018-08-16 14:05:10 +0100 | [diff] [blame] | 65 | VERIFY_USE_SECONDARY_KEYRING, usage, |
David Howells | e68503b | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 66 | pkcs7_view_content, prep); |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * user defined keys take an arbitrary string as the description and an |
| 71 | * arbitrary blob of data as the payload |
| 72 | */ |
Wei Yongjun | 63d2551 | 2014-07-28 21:17:12 +0800 | [diff] [blame] | 73 | static struct key_type key_type_pkcs7 = { |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 74 | .name = "pkcs7_test", |
David Howells | 1ca72c9 | 2014-07-22 21:52:33 +0100 | [diff] [blame] | 75 | .preparse = pkcs7_preparse, |
| 76 | .free_preparse = user_free_preparse, |
| 77 | .instantiate = generic_key_instantiate, |
David Howells | 22d01af | 2014-07-01 19:06:18 +0100 | [diff] [blame] | 78 | .revoke = user_revoke, |
| 79 | .destroy = user_destroy, |
| 80 | .describe = user_describe, |
| 81 | .read = user_read, |
| 82 | }; |
| 83 | |
| 84 | /* |
| 85 | * Module stuff |
| 86 | */ |
| 87 | static int __init pkcs7_key_init(void) |
| 88 | { |
| 89 | return register_key_type(&key_type_pkcs7); |
| 90 | } |
| 91 | |
| 92 | static void __exit pkcs7_key_cleanup(void) |
| 93 | { |
| 94 | unregister_key_type(&key_type_pkcs7); |
| 95 | } |
| 96 | |
| 97 | module_init(pkcs7_key_init); |
| 98 | module_exit(pkcs7_key_cleanup); |