Commit | Line | Data |
---|---|---|
22d01afb DH |
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> | |
8f3438cc | 14 | #include <linux/err.h> |
88775588 | 15 | #include <linux/module.h> |
22d01afb DH |
16 | #include <linux/key-type.h> |
17 | #include <crypto/pkcs7.h> | |
18 | #include <keys/user-type.h> | |
19 | #include <keys/system_keyring.h> | |
20 | #include "pkcs7_parser.h" | |
21 | ||
22 | /* | |
1ca72c96 | 23 | * Preparse a PKCS#7 wrapped and validated data blob. |
22d01afb | 24 | */ |
1ca72c96 | 25 | static int pkcs7_preparse(struct key_preparsed_payload *prep) |
22d01afb DH |
26 | { |
27 | struct pkcs7_message *pkcs7; | |
28 | const void *data, *saved_prep_data; | |
29 | size_t datalen, saved_prep_datalen; | |
30 | bool trusted; | |
31 | int ret; | |
32 | ||
33 | kenter(""); | |
34 | ||
35 | saved_prep_data = prep->data; | |
36 | saved_prep_datalen = prep->datalen; | |
37 | pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen); | |
38 | if (IS_ERR(pkcs7)) { | |
39 | ret = PTR_ERR(pkcs7); | |
40 | goto error; | |
41 | } | |
42 | ||
43 | ret = pkcs7_verify(pkcs7); | |
44 | if (ret < 0) | |
45 | goto error_free; | |
46 | ||
47 | ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted); | |
48 | if (ret < 0) | |
49 | goto error_free; | |
50 | if (!trusted) | |
51 | pr_warn("PKCS#7 message doesn't chain back to a trusted key\n"); | |
52 | ||
53 | ret = pkcs7_get_content_data(pkcs7, &data, &datalen, false); | |
54 | if (ret < 0) | |
55 | goto error_free; | |
56 | ||
57 | prep->data = data; | |
58 | prep->datalen = datalen; | |
1ca72c96 | 59 | ret = user_preparse(prep); |
22d01afb DH |
60 | prep->data = saved_prep_data; |
61 | prep->datalen = saved_prep_datalen; | |
62 | ||
63 | error_free: | |
64 | pkcs7_free_message(pkcs7); | |
65 | error: | |
66 | kleave(" = %d", ret); | |
67 | return ret; | |
68 | } | |
69 | ||
70 | /* | |
71 | * user defined keys take an arbitrary string as the description and an | |
72 | * arbitrary blob of data as the payload | |
73 | */ | |
63d2551e | 74 | static struct key_type key_type_pkcs7 = { |
22d01afb | 75 | .name = "pkcs7_test", |
1ca72c96 DH |
76 | .preparse = pkcs7_preparse, |
77 | .free_preparse = user_free_preparse, | |
78 | .instantiate = generic_key_instantiate, | |
22d01afb DH |
79 | .revoke = user_revoke, |
80 | .destroy = user_destroy, | |
81 | .describe = user_describe, | |
82 | .read = user_read, | |
83 | }; | |
84 | ||
85 | /* | |
86 | * Module stuff | |
87 | */ | |
88 | static int __init pkcs7_key_init(void) | |
89 | { | |
90 | return register_key_type(&key_type_pkcs7); | |
91 | } | |
92 | ||
93 | static void __exit pkcs7_key_cleanup(void) | |
94 | { | |
95 | unregister_key_type(&key_type_pkcs7); | |
96 | } | |
97 | ||
98 | module_init(pkcs7_key_init); | |
99 | module_exit(pkcs7_key_cleanup); |