PKCS#7: Appropriately restrict authenticated attributes and content type
[platform/kernel/linux-starfive.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/key-type.h>
17 #include <keys/asymmetric-type.h>
18 #include <crypto/pkcs7.h>
19 #include <keys/user-type.h>
20 #include <keys/system_keyring.h>
21 #include "pkcs7_parser.h"
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  * Preparse a PKCS#7 wrapped and validated data blob.
30  */
31 static int pkcs7_preparse(struct key_preparsed_payload *prep)
32 {
33         enum key_being_used_for usage = pkcs7_usage;
34         struct pkcs7_message *pkcs7;
35         const void *data, *saved_prep_data;
36         size_t datalen, saved_prep_datalen;
37         bool trusted;
38         int ret;
39
40         kenter("");
41
42         if (usage >= NR__KEY_BEING_USED_FOR) {
43                 pr_err("Invalid usage type %d\n", usage);
44                 return -EINVAL;
45         }
46
47         saved_prep_data = prep->data;
48         saved_prep_datalen = prep->datalen;
49         pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen);
50         if (IS_ERR(pkcs7)) {
51                 ret = PTR_ERR(pkcs7);
52                 goto error;
53         }
54
55         ret = pkcs7_verify(pkcs7, usage);
56         if (ret < 0)
57                 goto error_free;
58
59         ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
60         if (ret < 0)
61                 goto error_free;
62         if (!trusted)
63                 pr_warn("PKCS#7 message doesn't chain back to a trusted key\n");
64
65         ret = pkcs7_get_content_data(pkcs7, &data, &datalen, false);
66         if (ret < 0)
67                 goto error_free;
68
69         prep->data = data;
70         prep->datalen = datalen;
71         ret = user_preparse(prep);
72         prep->data = saved_prep_data;
73         prep->datalen = saved_prep_datalen;
74
75 error_free:
76         pkcs7_free_message(pkcs7);
77 error:
78         kleave(" = %d", ret);
79         return ret;
80 }
81
82 /*
83  * user defined keys take an arbitrary string as the description and an
84  * arbitrary blob of data as the payload
85  */
86 static struct key_type key_type_pkcs7 = {
87         .name                   = "pkcs7_test",
88         .preparse               = pkcs7_preparse,
89         .free_preparse          = user_free_preparse,
90         .instantiate            = generic_key_instantiate,
91         .revoke                 = user_revoke,
92         .destroy                = user_destroy,
93         .describe               = user_describe,
94         .read                   = user_read,
95 };
96
97 /*
98  * Module stuff
99  */
100 static int __init pkcs7_key_init(void)
101 {
102         return register_key_type(&key_type_pkcs7);
103 }
104
105 static void __exit pkcs7_key_cleanup(void)
106 {
107         unregister_key_type(&key_type_pkcs7);
108 }
109
110 module_init(pkcs7_key_init);
111 module_exit(pkcs7_key_cleanup);