]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/asymmetric_keys/pkcs7_key_type.c
Merge tag 'mmc-v4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[mirror_ubuntu-bionic-kernel.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>
e68503bd 16#include <linux/verification.h>
22d01afb 17#include <linux/key-type.h>
22d01afb 18#include <keys/user-type.h>
22d01afb 19
772111ab
DH
20MODULE_LICENSE("GPL");
21MODULE_DESCRIPTION("PKCS#7 testing key type");
1e684d38 22MODULE_AUTHOR("Red Hat, Inc.");
772111ab 23
99db4435
DH
24static unsigned pkcs7_usage;
25module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
26MODULE_PARM_DESC(pkcs7_usage,
27 "Usage to specify when verifying the PKCS#7 message");
28
22d01afb 29/*
e68503bd 30 * Retrieve the PKCS#7 message content.
22d01afb 31 */
e68503bd
DH
32static int pkcs7_view_content(void *ctx, const void *data, size_t len,
33 size_t asn1hdrlen)
22d01afb 34{
e68503bd
DH
35 struct key_preparsed_payload *prep = ctx;
36 const void *saved_prep_data;
37 size_t saved_prep_datalen;
22d01afb
DH
38 int ret;
39
22d01afb
DH
40 saved_prep_data = prep->data;
41 saved_prep_datalen = prep->datalen;
22d01afb 42 prep->data = data;
e68503bd
DH
43 prep->datalen = len;
44
1ca72c96 45 ret = user_preparse(prep);
e68503bd 46
22d01afb
DH
47 prep->data = saved_prep_data;
48 prep->datalen = saved_prep_datalen;
22d01afb
DH
49 return ret;
50}
51
e68503bd
DH
52/*
53 * Preparse a PKCS#7 wrapped and validated data blob.
54 */
55static int pkcs7_preparse(struct key_preparsed_payload *prep)
56{
57 enum key_being_used_for usage = pkcs7_usage;
58
59 if (usage >= NR__KEY_BEING_USED_FOR) {
60 pr_err("Invalid usage type %d\n", usage);
61 return -EINVAL;
62 }
63
64 return verify_pkcs7_signature(NULL, 0,
65 prep->data, prep->datalen,
3c8f2278 66 (void *)1UL, usage,
e68503bd
DH
67 pkcs7_view_content, prep);
68}
69
22d01afb
DH
70/*
71 * user defined keys take an arbitrary string as the description and an
72 * arbitrary blob of data as the payload
73 */
63d2551e 74static 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 */
88static int __init pkcs7_key_init(void)
89{
90 return register_key_type(&key_type_pkcs7);
91}
92
93static void __exit pkcs7_key_cleanup(void)
94{
95 unregister_key_type(&key_type_pkcs7);
96}
97
98module_init(pkcs7_key_init);
99module_exit(pkcs7_key_cleanup);