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");
22 MODULE_AUTHOR("Red Hat, Inc.");
24 static unsigned pkcs7_usage;
25 module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
26 MODULE_PARM_DESC(pkcs7_usage,
27 "Usage to specify when verifying the PKCS#7 message");
30 * Retrieve the PKCS#7 message content.
32 static int pkcs7_view_content(void *ctx, const void *data, size_t len,
35 struct key_preparsed_payload *prep = ctx;
36 const void *saved_prep_data;
37 size_t saved_prep_datalen;
40 saved_prep_data = prep->data;
41 saved_prep_datalen = prep->datalen;
45 ret = user_preparse(prep);
47 prep->data = saved_prep_data;
48 prep->datalen = saved_prep_datalen;
53 * Preparse a PKCS#7 wrapped and validated data blob.
55 static int pkcs7_preparse(struct key_preparsed_payload *prep)
57 enum key_being_used_for usage = pkcs7_usage;
59 if (usage >= NR__KEY_BEING_USED_FOR) {
60 pr_err("Invalid usage type %d\n", usage);
64 return verify_pkcs7_signature(NULL, 0,
65 prep->data, prep->datalen,
67 pkcs7_view_content, prep);
71 * user defined keys take an arbitrary string as the description and an
72 * arbitrary blob of data as the payload
74 static struct key_type key_type_pkcs7 = {
76 .preparse = pkcs7_preparse,
77 .free_preparse = user_free_preparse,
78 .instantiate = generic_key_instantiate,
79 .revoke = user_revoke,
80 .destroy = user_destroy,
81 .describe = user_describe,
88 static int __init pkcs7_key_init(void)
90 return register_key_type(&key_type_pkcs7);
93 static void __exit pkcs7_key_cleanup(void)
95 unregister_key_type(&key_type_pkcs7);
98 module_init(pkcs7_key_init);
99 module_exit(pkcs7_key_cleanup);