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