blob: 5b2f6a2b5585578a03303b2f8099ed951cd97a2d [file] [log] [blame]
David Howells22d01af2014-07-01 19:06:18 +01001/* 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 Howells8f3438c2014-07-25 11:33:53 +010014#include <linux/err.h>
Paul Gortmaker88775582015-05-01 21:29:53 -040015#include <linux/module.h>
David Howellse68503b2016-04-06 16:14:24 +010016#include <linux/verification.h>
David Howells22d01af2014-07-01 19:06:18 +010017#include <linux/key-type.h>
David Howells22d01af2014-07-01 19:06:18 +010018#include <keys/user-type.h>
David Howells22d01af2014-07-01 19:06:18 +010019
David Howells772111a2015-08-13 02:51:33 +010020MODULE_LICENSE("GPL");
21MODULE_DESCRIPTION("PKCS#7 testing key type");
David Howells1e684d32017-11-15 16:38:45 +000022MODULE_AUTHOR("Red Hat, Inc.");
David Howells772111a2015-08-13 02:51:33 +010023
David Howells99db4432015-08-05 15:22:27 +010024static unsigned pkcs7_usage;
25module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
26MODULE_PARM_DESC(pkcs7_usage,
27 "Usage to specify when verifying the PKCS#7 message");
28
David Howells22d01af2014-07-01 19:06:18 +010029/*
David Howellse68503b2016-04-06 16:14:24 +010030 * Retrieve the PKCS#7 message content.
31 */
32static int pkcs7_view_content(void *ctx, const void *data, size_t len,
33 size_t asn1hdrlen)
34{
35 struct key_preparsed_payload *prep = ctx;
36 const void *saved_prep_data;
37 size_t saved_prep_datalen;
38 int ret;
39
40 saved_prep_data = prep->data;
41 saved_prep_datalen = prep->datalen;
42 prep->data = data;
43 prep->datalen = len;
44
45 ret = user_preparse(prep);
46
47 prep->data = saved_prep_data;
48 prep->datalen = saved_prep_datalen;
49 return ret;
50}
51
52/*
David Howells1ca72c92014-07-22 21:52:33 +010053 * Preparse a PKCS#7 wrapped and validated data blob.
David Howells22d01af2014-07-01 19:06:18 +010054 */
David Howells1ca72c92014-07-22 21:52:33 +010055static int pkcs7_preparse(struct key_preparsed_payload *prep)
David Howells22d01af2014-07-01 19:06:18 +010056{
David Howells99db4432015-08-05 15:22:27 +010057 enum key_being_used_for usage = pkcs7_usage;
David Howells22d01af2014-07-01 19:06:18 +010058
David Howells99db4432015-08-05 15:22:27 +010059 if (usage >= NR__KEY_BEING_USED_FOR) {
60 pr_err("Invalid usage type %d\n", usage);
61 return -EINVAL;
62 }
63
David Howellse68503b2016-04-06 16:14:24 +010064 return verify_pkcs7_signature(NULL, 0,
65 prep->data, prep->datalen,
Yannik Sembritzki817aef22018-08-16 14:05:10 +010066 VERIFY_USE_SECONDARY_KEYRING, usage,
David Howellse68503b2016-04-06 16:14:24 +010067 pkcs7_view_content, prep);
David Howells22d01af2014-07-01 19:06:18 +010068}
69
70/*
71 * user defined keys take an arbitrary string as the description and an
72 * arbitrary blob of data as the payload
73 */
Wei Yongjun63d25512014-07-28 21:17:12 +080074static struct key_type key_type_pkcs7 = {
David Howells22d01af2014-07-01 19:06:18 +010075 .name = "pkcs7_test",
David Howells1ca72c92014-07-22 21:52:33 +010076 .preparse = pkcs7_preparse,
77 .free_preparse = user_free_preparse,
78 .instantiate = generic_key_instantiate,
David Howells22d01af2014-07-01 19:06:18 +010079 .revoke = user_revoke,
80 .destroy = user_destroy,
81 .describe = user_describe,
82 .read = user_read,
83};
84
85/*
86 * Module stuff
87 */
88static int __init pkcs7_key_init(void)
89{
90 return register_key_type(&key_type_pkcs7);
91}
92
93static void __exit pkcs7_key_cleanup(void)
94{
95 unregister_key_type(&key_type_pkcs7);
96}
97
98module_init(pkcs7_key_init);
99module_exit(pkcs7_key_cleanup);