1 /* Testing module to load key from trusted PKCS#7 message
3 * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) "PKCS7key: "fmt
13 #include <linux/key.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/verification.h>
17 #include <linux/key-type.h>
18 #include <keys/user-type.h>
20 MODULE_LICENSE("GPL");
21 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");
29 * Retrieve the PKCS#7 message content.
31 static int pkcs7_view_content(void *ctx, const void *data, size_t len,
34 struct key_preparsed_payload *prep = ctx;
35 const void *saved_prep_data;
36 size_t saved_prep_datalen;
39 saved_prep_data = prep->data;
40 saved_prep_datalen = prep->datalen;
44 ret = user_preparse(prep);
46 prep->data = saved_prep_data;
47 prep->datalen = saved_prep_datalen;
52 * Preparse a PKCS#7 wrapped and validated data blob.
54 static int pkcs7_preparse(struct key_preparsed_payload *prep)
56 enum key_being_used_for usage = pkcs7_usage;
58 if (usage >= NR__KEY_BEING_USED_FOR) {
59 pr_err("Invalid usage type %d\n", usage);
63 return verify_pkcs7_signature(NULL, 0,
64 prep->data, prep->datalen,
66 pkcs7_view_content, prep);
70 * user defined keys take an arbitrary string as the description and an
71 * arbitrary blob of data as the payload
73 static struct key_type key_type_pkcs7 = {
75 .preparse = pkcs7_preparse,
76 .free_preparse = user_free_preparse,
77 .instantiate = generic_key_instantiate,
78 .revoke = user_revoke,
79 .destroy = user_destroy,
80 .describe = user_describe,
87 static int __init pkcs7_key_init(void)
89 return register_key_type(&key_type_pkcs7);
92 static void __exit pkcs7_key_cleanup(void)
94 unregister_key_type(&key_type_pkcs7);
97 module_init(pkcs7_key_init);
98 module_exit(pkcs7_key_cleanup);