Merge tag 'gcc-plugins-v4.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / crypto / asymmetric_keys / pkcs7_key_type.c
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>
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>
19
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("PKCS#7 testing key type");
22
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 /*
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 /*
52  * Preparse a PKCS#7 wrapped and validated data blob.
53  */
54 static int pkcs7_preparse(struct key_preparsed_payload *prep)
55 {
56         enum key_being_used_for usage = pkcs7_usage;
57
58         if (usage >= NR__KEY_BEING_USED_FOR) {
59                 pr_err("Invalid usage type %d\n", usage);
60                 return -EINVAL;
61         }
62
63         return verify_pkcs7_signature(NULL, 0,
64                                       prep->data, prep->datalen,
65                                       (void *)1UL, usage,
66                                       pkcs7_view_content, prep);
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  */
73 static struct key_type key_type_pkcs7 = {
74         .name                   = "pkcs7_test",
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,
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);