mm: update min_free_kbytes from khugepaged after core initialization
[deliverable/linux.git] / crypto / asymmetric_keys / pkcs7_key_type.c
CommitLineData
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 16#include <linux/key-type.h>
99db4435 17#include <keys/asymmetric-type.h>
22d01afb
DH
18#include <crypto/pkcs7.h>
19#include <keys/user-type.h>
20#include <keys/system_keyring.h>
21#include "pkcs7_parser.h"
22
772111ab
DH
23MODULE_LICENSE("GPL");
24MODULE_DESCRIPTION("PKCS#7 testing key type");
25
99db4435
DH
26static unsigned pkcs7_usage;
27module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
28MODULE_PARM_DESC(pkcs7_usage,
29 "Usage to specify when verifying the PKCS#7 message");
30
22d01afb 31/*
1ca72c96 32 * Preparse a PKCS#7 wrapped and validated data blob.
22d01afb 33 */
1ca72c96 34static int pkcs7_preparse(struct key_preparsed_payload *prep)
22d01afb 35{
99db4435 36 enum key_being_used_for usage = pkcs7_usage;
22d01afb
DH
37 struct pkcs7_message *pkcs7;
38 const void *data, *saved_prep_data;
39 size_t datalen, saved_prep_datalen;
40 bool trusted;
41 int ret;
42
43 kenter("");
44
99db4435
DH
45 if (usage >= NR__KEY_BEING_USED_FOR) {
46 pr_err("Invalid usage type %d\n", usage);
47 return -EINVAL;
48 }
49
22d01afb
DH
50 saved_prep_data = prep->data;
51 saved_prep_datalen = prep->datalen;
52 pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen);
53 if (IS_ERR(pkcs7)) {
54 ret = PTR_ERR(pkcs7);
55 goto error;
56 }
57
99db4435 58 ret = pkcs7_verify(pkcs7, usage);
22d01afb
DH
59 if (ret < 0)
60 goto error_free;
61
62 ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
63 if (ret < 0)
64 goto error_free;
65 if (!trusted)
66 pr_warn("PKCS#7 message doesn't chain back to a trusted key\n");
67
68 ret = pkcs7_get_content_data(pkcs7, &data, &datalen, false);
69 if (ret < 0)
70 goto error_free;
71
72 prep->data = data;
73 prep->datalen = datalen;
1ca72c96 74 ret = user_preparse(prep);
22d01afb
DH
75 prep->data = saved_prep_data;
76 prep->datalen = saved_prep_datalen;
77
78error_free:
79 pkcs7_free_message(pkcs7);
80error:
81 kleave(" = %d", ret);
82 return ret;
83}
84
85/*
86 * user defined keys take an arbitrary string as the description and an
87 * arbitrary blob of data as the payload
88 */
63d2551e 89static struct key_type key_type_pkcs7 = {
22d01afb 90 .name = "pkcs7_test",
1ca72c96
DH
91 .preparse = pkcs7_preparse,
92 .free_preparse = user_free_preparse,
93 .instantiate = generic_key_instantiate,
22d01afb
DH
94 .revoke = user_revoke,
95 .destroy = user_destroy,
96 .describe = user_describe,
97 .read = user_read,
98};
99
100/*
101 * Module stuff
102 */
103static int __init pkcs7_key_init(void)
104{
105 return register_key_type(&key_type_pkcs7);
106}
107
108static void __exit pkcs7_key_cleanup(void)
109{
110 unregister_key_type(&key_type_pkcs7);
111}
112
113module_init(pkcs7_key_init);
114module_exit(pkcs7_key_cleanup);
This page took 0.082078 seconds and 5 git commands to generate.